1/******************************************************************************
2** This file is an amalgamation of many separate C source files from SQLite
3** version 3.26.0. By combining all the individual C code files into this
4** single large file, the entire code can be compiled as a single translation
5** unit. This allows many compilers to do optimizations that would not be
6** possible if the files were compiled separately. Performance improvements
7** of 5% or more are commonly seen when SQLite is compiled as a single
8** translation unit.
9**
10** This file is all you need to compile SQLite. To use SQLite in other
11** programs, you need this file and the "sqlite3.h" header file that defines
12** the programming interface to the SQLite library. (If you do not have
13** the "sqlite3.h" header file at hand, you will find a copy embedded within
14** the text of this file. Search for "Begin file sqlite3.h" to find the start
15** of the embedded sqlite3.h header file.) Additional code files may be needed
16** if you want a wrapper to interface SQLite with your choice of programming
17** language. The code for the "sqlite3" command-line shell is also in a
18** separate file. This file contains only code for the core SQLite library.
19*/
20#define SQLITE_CORE 1
21#define SQLITE_AMALGAMATION 1
22#ifndef SQLITE_PRIVATE
23# define SQLITE_PRIVATE static
24#endif
25/************** Begin file ctime.c *******************************************/
26/*
27** 2010 February 23
28**
29** The author disclaims copyright to this source code. In place of
30** a legal notice, here is a blessing:
31**
32** May you do good and not evil.
33** May you find forgiveness for yourself and forgive others.
34** May you share freely, never taking more than you give.
35**
36*************************************************************************
37**
38** This file implements routines used to report what compile-time options
39** SQLite was built with.
40*/
41
42#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
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_CEROD
222 "ENABLE_CEROD=" CTIMEOPT_VAL(SQLITE_ENABLE_CEROD),
223#endif
224#if SQLITE_ENABLE_COLUMN_METADATA
225 "ENABLE_COLUMN_METADATA",
226#endif
227#if SQLITE_ENABLE_COLUMN_USED_MASK
228 "ENABLE_COLUMN_USED_MASK",
229#endif
230#if SQLITE_ENABLE_COSTMULT
231 "ENABLE_COSTMULT",
232#endif
233#if SQLITE_ENABLE_CURSOR_HINTS
234 "ENABLE_CURSOR_HINTS",
235#endif
236#if SQLITE_ENABLE_DBSTAT_VTAB
237 "ENABLE_DBSTAT_VTAB",
238#endif
239#if SQLITE_ENABLE_EXPENSIVE_ASSERT
240 "ENABLE_EXPENSIVE_ASSERT",
241#endif
242#if SQLITE_ENABLE_FTS1
243 "ENABLE_FTS1",
244#endif
245#if SQLITE_ENABLE_FTS2
246 "ENABLE_FTS2",
247#endif
248#if SQLITE_ENABLE_FTS3
249 "ENABLE_FTS3",
250#endif
251#if SQLITE_ENABLE_FTS3_PARENTHESIS
252 "ENABLE_FTS3_PARENTHESIS",
253#endif
254#if SQLITE_ENABLE_FTS3_TOKENIZER
255 "ENABLE_FTS3_TOKENIZER",
256#endif
257#if SQLITE_ENABLE_FTS4
258 "ENABLE_FTS4",
259#endif
260#if SQLITE_ENABLE_FTS5
261 "ENABLE_FTS5",
262#endif
263#if SQLITE_ENABLE_GEOPOLY
264 "ENABLE_GEOPOLY",
265#endif
266#if SQLITE_ENABLE_HIDDEN_COLUMNS
267 "ENABLE_HIDDEN_COLUMNS",
268#endif
269#if SQLITE_ENABLE_ICU
270 "ENABLE_ICU",
271#endif
272#if SQLITE_ENABLE_IOTRACE
273 "ENABLE_IOTRACE",
274#endif
275#if SQLITE_ENABLE_JSON1
276 "ENABLE_JSON1",
277#endif
278#if SQLITE_ENABLE_LOAD_EXTENSION
279 "ENABLE_LOAD_EXTENSION",
280#endif
281#ifdef SQLITE_ENABLE_LOCKING_STYLE
282 "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE),
283#endif
284#if SQLITE_ENABLE_MEMORY_MANAGEMENT
285 "ENABLE_MEMORY_MANAGEMENT",
286#endif
287#if SQLITE_ENABLE_MEMSYS3
288 "ENABLE_MEMSYS3",
289#endif
290#if SQLITE_ENABLE_MEMSYS5
291 "ENABLE_MEMSYS5",
292#endif
293#if SQLITE_ENABLE_MULTIPLEX
294 "ENABLE_MULTIPLEX",
295#endif
296#if SQLITE_ENABLE_NORMALIZE
297 "ENABLE_NORMALIZE",
298#endif
299#if SQLITE_ENABLE_NULL_TRIM
300 "ENABLE_NULL_TRIM",
301#endif
302#if SQLITE_ENABLE_OVERSIZE_CELL_CHECK
303 "ENABLE_OVERSIZE_CELL_CHECK",
304#endif
305#if SQLITE_ENABLE_PREUPDATE_HOOK
306 "ENABLE_PREUPDATE_HOOK",
307#endif
308#if SQLITE_ENABLE_QPSG
309 "ENABLE_QPSG",
310#endif
311#if SQLITE_ENABLE_RBU
312 "ENABLE_RBU",
313#endif
314#if SQLITE_ENABLE_RTREE
315 "ENABLE_RTREE",
316#endif
317#if SQLITE_ENABLE_SELECTTRACE
318 "ENABLE_SELECTTRACE",
319#endif
320#if SQLITE_ENABLE_SESSION
321 "ENABLE_SESSION",
322#endif
323#if SQLITE_ENABLE_SNAPSHOT
324 "ENABLE_SNAPSHOT",
325#endif
326#if SQLITE_ENABLE_SORTER_REFERENCES
327 "ENABLE_SORTER_REFERENCES",
328#endif
329#if SQLITE_ENABLE_SQLLOG
330 "ENABLE_SQLLOG",
331#endif
332#if defined(SQLITE_ENABLE_STAT4)
333 "ENABLE_STAT4",
334#elif defined(SQLITE_ENABLE_STAT3)
335 "ENABLE_STAT3",
336#endif
337#if SQLITE_ENABLE_STMTVTAB
338 "ENABLE_STMTVTAB",
339#endif
340#if SQLITE_ENABLE_STMT_SCANSTATUS
341 "ENABLE_STMT_SCANSTATUS",
342#endif
343#if SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
344 "ENABLE_UNKNOWN_SQL_FUNCTION",
345#endif
346#if SQLITE_ENABLE_UNLOCK_NOTIFY
347 "ENABLE_UNLOCK_NOTIFY",
348#endif
349#if SQLITE_ENABLE_UPDATE_DELETE_LIMIT
350 "ENABLE_UPDATE_DELETE_LIMIT",
351#endif
352#if SQLITE_ENABLE_URI_00_ERROR
353 "ENABLE_URI_00_ERROR",
354#endif
355#if SQLITE_ENABLE_VFSTRACE
356 "ENABLE_VFSTRACE",
357#endif
358#if SQLITE_ENABLE_WHERETRACE
359 "ENABLE_WHERETRACE",
360#endif
361#if SQLITE_ENABLE_ZIPVFS
362 "ENABLE_ZIPVFS",
363#endif
364#if SQLITE_EXPLAIN_ESTIMATED_ROWS
365 "EXPLAIN_ESTIMATED_ROWS",
366#endif
367#if SQLITE_EXTRA_IFNULLROW
368 "EXTRA_IFNULLROW",
369#endif
370#ifdef SQLITE_EXTRA_INIT
371 "EXTRA_INIT=" CTIMEOPT_VAL(SQLITE_EXTRA_INIT),
372#endif
373#ifdef SQLITE_EXTRA_SHUTDOWN
374 "EXTRA_SHUTDOWN=" CTIMEOPT_VAL(SQLITE_EXTRA_SHUTDOWN),
375#endif
376#ifdef SQLITE_FTS3_MAX_EXPR_DEPTH
377 "FTS3_MAX_EXPR_DEPTH=" CTIMEOPT_VAL(SQLITE_FTS3_MAX_EXPR_DEPTH),
378#endif
379#if SQLITE_FTS5_ENABLE_TEST_MI
380 "FTS5_ENABLE_TEST_MI",
381#endif
382#if SQLITE_FTS5_NO_WITHOUT_ROWID
383 "FTS5_NO_WITHOUT_ROWID",
384#endif
385#if SQLITE_HAS_CODEC
386 "HAS_CODEC",
387#endif
388#if HAVE_ISNAN || SQLITE_HAVE_ISNAN
389 "HAVE_ISNAN",
390#endif
391#if SQLITE_HOMEGROWN_RECURSIVE_MUTEX
392 "HOMEGROWN_RECURSIVE_MUTEX",
393#endif
394#if SQLITE_IGNORE_AFP_LOCK_ERRORS
395 "IGNORE_AFP_LOCK_ERRORS",
396#endif
397#if SQLITE_IGNORE_FLOCK_LOCK_ERRORS
398 "IGNORE_FLOCK_LOCK_ERRORS",
399#endif
400#if SQLITE_INLINE_MEMCPY
401 "INLINE_MEMCPY",
402#endif
403#if SQLITE_INT64_TYPE
404 "INT64_TYPE",
405#endif
406#ifdef SQLITE_INTEGRITY_CHECK_ERROR_MAX
407 "INTEGRITY_CHECK_ERROR_MAX=" CTIMEOPT_VAL(SQLITE_INTEGRITY_CHECK_ERROR_MAX),
408#endif
409#if SQLITE_LIKE_DOESNT_MATCH_BLOBS
410 "LIKE_DOESNT_MATCH_BLOBS",
411#endif
412#if SQLITE_LOCK_TRACE
413 "LOCK_TRACE",
414#endif
415#if SQLITE_LOG_CACHE_SPILL
416 "LOG_CACHE_SPILL",
417#endif
418#ifdef SQLITE_MALLOC_SOFT_LIMIT
419 "MALLOC_SOFT_LIMIT=" CTIMEOPT_VAL(SQLITE_MALLOC_SOFT_LIMIT),
420#endif
421#ifdef SQLITE_MAX_ATTACHED
422 "MAX_ATTACHED=" CTIMEOPT_VAL(SQLITE_MAX_ATTACHED),
423#endif
424#ifdef SQLITE_MAX_COLUMN
425 "MAX_COLUMN=" CTIMEOPT_VAL(SQLITE_MAX_COLUMN),
426#endif
427#ifdef SQLITE_MAX_COMPOUND_SELECT
428 "MAX_COMPOUND_SELECT=" CTIMEOPT_VAL(SQLITE_MAX_COMPOUND_SELECT),
429#endif
430#ifdef SQLITE_MAX_DEFAULT_PAGE_SIZE
431 "MAX_DEFAULT_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_DEFAULT_PAGE_SIZE),
432#endif
433#ifdef SQLITE_MAX_EXPR_DEPTH
434 "MAX_EXPR_DEPTH=" CTIMEOPT_VAL(SQLITE_MAX_EXPR_DEPTH),
435#endif
436#ifdef SQLITE_MAX_FUNCTION_ARG
437 "MAX_FUNCTION_ARG=" CTIMEOPT_VAL(SQLITE_MAX_FUNCTION_ARG),
438#endif
439#ifdef SQLITE_MAX_LENGTH
440 "MAX_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_LENGTH),
441#endif
442#ifdef SQLITE_MAX_LIKE_PATTERN_LENGTH
443 "MAX_LIKE_PATTERN_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_LIKE_PATTERN_LENGTH),
444#endif
445#ifdef SQLITE_MAX_MEMORY
446 "MAX_MEMORY=" CTIMEOPT_VAL(SQLITE_MAX_MEMORY),
447#endif
448#ifdef SQLITE_MAX_MMAP_SIZE
449 "MAX_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE),
450#endif
451#ifdef SQLITE_MAX_MMAP_SIZE_
452 "MAX_MMAP_SIZE_=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE_),
453#endif
454#ifdef SQLITE_MAX_PAGE_COUNT
455 "MAX_PAGE_COUNT=" CTIMEOPT_VAL(SQLITE_MAX_PAGE_COUNT),
456#endif
457#ifdef SQLITE_MAX_PAGE_SIZE
458 "MAX_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_PAGE_SIZE),
459#endif
460#ifdef SQLITE_MAX_SCHEMA_RETRY
461 "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY),
462#endif
463#ifdef SQLITE_MAX_SQL_LENGTH
464 "MAX_SQL_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_SQL_LENGTH),
465#endif
466#ifdef SQLITE_MAX_TRIGGER_DEPTH
467 "MAX_TRIGGER_DEPTH=" CTIMEOPT_VAL(SQLITE_MAX_TRIGGER_DEPTH),
468#endif
469#ifdef SQLITE_MAX_VARIABLE_NUMBER
470 "MAX_VARIABLE_NUMBER=" CTIMEOPT_VAL(SQLITE_MAX_VARIABLE_NUMBER),
471#endif
472#ifdef SQLITE_MAX_VDBE_OP
473 "MAX_VDBE_OP=" CTIMEOPT_VAL(SQLITE_MAX_VDBE_OP),
474#endif
475#ifdef SQLITE_MAX_WORKER_THREADS
476 "MAX_WORKER_THREADS=" CTIMEOPT_VAL(SQLITE_MAX_WORKER_THREADS),
477#endif
478#if SQLITE_MEMDEBUG
479 "MEMDEBUG",
480#endif
481#if SQLITE_MIXED_ENDIAN_64BIT_FLOAT
482 "MIXED_ENDIAN_64BIT_FLOAT",
483#endif
484#if SQLITE_MMAP_READWRITE
485 "MMAP_READWRITE",
486#endif
487#if SQLITE_MUTEX_NOOP
488 "MUTEX_NOOP",
489#endif
490#if SQLITE_MUTEX_NREF
491 "MUTEX_NREF",
492#endif
493#if SQLITE_MUTEX_OMIT
494 "MUTEX_OMIT",
495#endif
496#if SQLITE_MUTEX_PTHREADS
497 "MUTEX_PTHREADS",
498#endif
499#if SQLITE_MUTEX_W32
500 "MUTEX_W32",
501#endif
502#if SQLITE_NEED_ERR_NAME
503 "NEED_ERR_NAME",
504#endif
505#if SQLITE_NOINLINE
506 "NOINLINE",
507#endif
508#if SQLITE_NO_SYNC
509 "NO_SYNC",
510#endif
511#if SQLITE_OMIT_ALTERTABLE
512 "OMIT_ALTERTABLE",
513#endif
514#if SQLITE_OMIT_ANALYZE
515 "OMIT_ANALYZE",
516#endif
517#if SQLITE_OMIT_ATTACH
518 "OMIT_ATTACH",
519#endif
520#if SQLITE_OMIT_AUTHORIZATION
521 "OMIT_AUTHORIZATION",
522#endif
523#if SQLITE_OMIT_AUTOINCREMENT
524 "OMIT_AUTOINCREMENT",
525#endif
526#if SQLITE_OMIT_AUTOINIT
527 "OMIT_AUTOINIT",
528#endif
529#if SQLITE_OMIT_AUTOMATIC_INDEX
530 "OMIT_AUTOMATIC_INDEX",
531#endif
532#if SQLITE_OMIT_AUTORESET
533 "OMIT_AUTORESET",
534#endif
535#if SQLITE_OMIT_AUTOVACUUM
536 "OMIT_AUTOVACUUM",
537#endif
538#if SQLITE_OMIT_BETWEEN_OPTIMIZATION
539 "OMIT_BETWEEN_OPTIMIZATION",
540#endif
541#if SQLITE_OMIT_BLOB_LITERAL
542 "OMIT_BLOB_LITERAL",
543#endif
544#if SQLITE_OMIT_BTREECOUNT
545 "OMIT_BTREECOUNT",
546#endif
547#if SQLITE_OMIT_CAST
548 "OMIT_CAST",
549#endif
550#if SQLITE_OMIT_CHECK
551 "OMIT_CHECK",
552#endif
553#if SQLITE_OMIT_COMPLETE
554 "OMIT_COMPLETE",
555#endif
556#if SQLITE_OMIT_COMPOUND_SELECT
557 "OMIT_COMPOUND_SELECT",
558#endif
559#if SQLITE_OMIT_CONFLICT_CLAUSE
560 "OMIT_CONFLICT_CLAUSE",
561#endif
562#if SQLITE_OMIT_CTE
563 "OMIT_CTE",
564#endif
565#if SQLITE_OMIT_DATETIME_FUNCS
566 "OMIT_DATETIME_FUNCS",
567#endif
568#if SQLITE_OMIT_DECLTYPE
569 "OMIT_DECLTYPE",
570#endif
571#if SQLITE_OMIT_DEPRECATED
572 "OMIT_DEPRECATED",
573#endif
574#if SQLITE_OMIT_DISKIO
575 "OMIT_DISKIO",
576#endif
577#if SQLITE_OMIT_EXPLAIN
578 "OMIT_EXPLAIN",
579#endif
580#if SQLITE_OMIT_FLAG_PRAGMAS
581 "OMIT_FLAG_PRAGMAS",
582#endif
583#if SQLITE_OMIT_FLOATING_POINT
584 "OMIT_FLOATING_POINT",
585#endif
586#if SQLITE_OMIT_FOREIGN_KEY
587 "OMIT_FOREIGN_KEY",
588#endif
589#if SQLITE_OMIT_GET_TABLE
590 "OMIT_GET_TABLE",
591#endif
592#if SQLITE_OMIT_HEX_INTEGER
593 "OMIT_HEX_INTEGER",
594#endif
595#if SQLITE_OMIT_INCRBLOB
596 "OMIT_INCRBLOB",
597#endif
598#if SQLITE_OMIT_INTEGRITY_CHECK
599 "OMIT_INTEGRITY_CHECK",
600#endif
601#if SQLITE_OMIT_LIKE_OPTIMIZATION
602 "OMIT_LIKE_OPTIMIZATION",
603#endif
604#if SQLITE_OMIT_LOAD_EXTENSION
605 "OMIT_LOAD_EXTENSION",
606#endif
607#if SQLITE_OMIT_LOCALTIME
608 "OMIT_LOCALTIME",
609#endif
610#if SQLITE_OMIT_LOOKASIDE
611 "OMIT_LOOKASIDE",
612#endif
613#if SQLITE_OMIT_MEMORYDB
614 "OMIT_MEMORYDB",
615#endif
616#if SQLITE_OMIT_OR_OPTIMIZATION
617 "OMIT_OR_OPTIMIZATION",
618#endif
619#if SQLITE_OMIT_PAGER_PRAGMAS
620 "OMIT_PAGER_PRAGMAS",
621#endif
622#if SQLITE_OMIT_PARSER_TRACE
623 "OMIT_PARSER_TRACE",
624#endif
625#if SQLITE_OMIT_POPEN
626 "OMIT_POPEN",
627#endif
628#if SQLITE_OMIT_PRAGMA
629 "OMIT_PRAGMA",
630#endif
631#if SQLITE_OMIT_PROGRESS_CALLBACK
632 "OMIT_PROGRESS_CALLBACK",
633#endif
634#if SQLITE_OMIT_QUICKBALANCE
635 "OMIT_QUICKBALANCE",
636#endif
637#if SQLITE_OMIT_REINDEX
638 "OMIT_REINDEX",
639#endif
640#if SQLITE_OMIT_SCHEMA_PRAGMAS
641 "OMIT_SCHEMA_PRAGMAS",
642#endif
643#if SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
644 "OMIT_SCHEMA_VERSION_PRAGMAS",
645#endif
646#if SQLITE_OMIT_SHARED_CACHE
647 "OMIT_SHARED_CACHE",
648#endif
649#if SQLITE_OMIT_SHUTDOWN_DIRECTORIES
650 "OMIT_SHUTDOWN_DIRECTORIES",
651#endif
652#if SQLITE_OMIT_SUBQUERY
653 "OMIT_SUBQUERY",
654#endif
655#if SQLITE_OMIT_TCL_VARIABLE
656 "OMIT_TCL_VARIABLE",
657#endif
658#if SQLITE_OMIT_TEMPDB
659 "OMIT_TEMPDB",
660#endif
661#if SQLITE_OMIT_TEST_CONTROL
662 "OMIT_TEST_CONTROL",
663#endif
664#if SQLITE_OMIT_TRACE
665 "OMIT_TRACE",
666#endif
667#if SQLITE_OMIT_TRIGGER
668 "OMIT_TRIGGER",
669#endif
670#if SQLITE_OMIT_TRUNCATE_OPTIMIZATION
671 "OMIT_TRUNCATE_OPTIMIZATION",
672#endif
673#if SQLITE_OMIT_UTF16
674 "OMIT_UTF16",
675#endif
676#if SQLITE_OMIT_VACUUM
677 "OMIT_VACUUM",
678#endif
679#if SQLITE_OMIT_VIEW
680 "OMIT_VIEW",
681#endif
682#if SQLITE_OMIT_VIRTUALTABLE
683 "OMIT_VIRTUALTABLE",
684#endif
685#if SQLITE_OMIT_WAL
686 "OMIT_WAL",
687#endif
688#if SQLITE_OMIT_WSD
689 "OMIT_WSD",
690#endif
691#if SQLITE_OMIT_XFER_OPT
692 "OMIT_XFER_OPT",
693#endif
694#if SQLITE_PCACHE_SEPARATE_HEADER
695 "PCACHE_SEPARATE_HEADER",
696#endif
697#if SQLITE_PERFORMANCE_TRACE
698 "PERFORMANCE_TRACE",
699#endif
700#if SQLITE_POWERSAFE_OVERWRITE
701 "POWERSAFE_OVERWRITE",
702#endif
703#if SQLITE_PREFER_PROXY_LOCKING
704 "PREFER_PROXY_LOCKING",
705#endif
706#if SQLITE_PROXY_DEBUG
707 "PROXY_DEBUG",
708#endif
709#if SQLITE_REVERSE_UNORDERED_SELECTS
710 "REVERSE_UNORDERED_SELECTS",
711#endif
712#if SQLITE_RTREE_INT_ONLY
713 "RTREE_INT_ONLY",
714#endif
715#if SQLITE_SECURE_DELETE
716 "SECURE_DELETE",
717#endif
718#if SQLITE_SMALL_STACK
719 "SMALL_STACK",
720#endif
721#ifdef SQLITE_SORTER_PMASZ
722 "SORTER_PMASZ=" CTIMEOPT_VAL(SQLITE_SORTER_PMASZ),
723#endif
724#if SQLITE_SOUNDEX
725 "SOUNDEX",
726#endif
727#ifdef SQLITE_STAT4_SAMPLES
728 "STAT4_SAMPLES=" CTIMEOPT_VAL(SQLITE_STAT4_SAMPLES),
729#endif
730#ifdef SQLITE_STMTJRNL_SPILL
731 "STMTJRNL_SPILL=" CTIMEOPT_VAL(SQLITE_STMTJRNL_SPILL),
732#endif
733#if SQLITE_SUBSTR_COMPATIBILITY
734 "SUBSTR_COMPATIBILITY",
735#endif
736#if SQLITE_SYSTEM_MALLOC
737 "SYSTEM_MALLOC",
738#endif
739#if SQLITE_TCL
740 "TCL",
741#endif
742#ifdef SQLITE_TEMP_STORE
743 "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
744#endif
745#if SQLITE_TEST
746 "TEST",
747#endif
748#if defined(SQLITE_THREADSAFE)
749 "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
750#elif defined(THREADSAFE)
751 "THREADSAFE=" CTIMEOPT_VAL(THREADSAFE),
752#else
753 "THREADSAFE=1",
754#endif
755#if SQLITE_UNLINK_AFTER_CLOSE
756 "UNLINK_AFTER_CLOSE",
757#endif
758#if SQLITE_UNTESTABLE
759 "UNTESTABLE",
760#endif
761#if SQLITE_USER_AUTHENTICATION
762 "USER_AUTHENTICATION",
763#endif
764#if SQLITE_USE_ALLOCA
765 "USE_ALLOCA",
766#endif
767#if SQLITE_USE_FCNTL_TRACE
768 "USE_FCNTL_TRACE",
769#endif
770#if SQLITE_USE_URI
771 "USE_URI",
772#endif
773#if SQLITE_VDBE_COVERAGE
774 "VDBE_COVERAGE",
775#endif
776#if SQLITE_WIN32_MALLOC
777 "WIN32_MALLOC",
778#endif
779#if SQLITE_ZERO_MALLOC
780 "ZERO_MALLOC",
781#endif
782/*
783** END CODE GENERATED BY tool/mkctime.tcl
784*/
785};
786
787SQLITE_PRIVATE const char **sqlite3CompileOptions(int *pnOpt){
788 *pnOpt = sizeof(sqlite3azCompileOpt) / sizeof(sqlite3azCompileOpt[0]);
789 return (const char**)sqlite3azCompileOpt;
790}
791
792#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
793
794/************** End of ctime.c ***********************************************/
795/************** Begin file sqliteInt.h ***************************************/
796/*
797** 2001 September 15
798**
799** The author disclaims copyright to this source code. In place of
800** a legal notice, here is a blessing:
801**
802** May you do good and not evil.
803** May you find forgiveness for yourself and forgive others.
804** May you share freely, never taking more than you give.
805**
806*************************************************************************
807** Internal interface definitions for SQLite.
808**
809*/
810#ifndef SQLITEINT_H
811#define SQLITEINT_H
812
813/* Special Comments:
814**
815** Some comments have special meaning to the tools that measure test
816** coverage:
817**
818** NO_TEST - The branches on this line are not
819** measured by branch coverage. This is
820** used on lines of code that actually
821** implement parts of coverage testing.
822**
823** OPTIMIZATION-IF-TRUE - This branch is allowed to alway be false
824** and the correct answer is still obtained,
825** though perhaps more slowly.
826**
827** OPTIMIZATION-IF-FALSE - This branch is allowed to alway be true
828** and the correct answer is still obtained,
829** though perhaps more slowly.
830**
831** PREVENTS-HARMLESS-OVERREAD - This branch prevents a buffer overread
832** that would be harmless and undetectable
833** if it did occur.
834**
835** In all cases, the special comment must be enclosed in the usual
836** slash-asterisk...asterisk-slash comment marks, with no spaces between the
837** asterisks and the comment text.
838*/
839
840/*
841** Make sure the Tcl calling convention macro is defined. This macro is
842** only used by test code and Tcl integration code.
843*/
844#ifndef SQLITE_TCLAPI
845# define SQLITE_TCLAPI
846#endif
847
848/*
849** Include the header file used to customize the compiler options for MSVC.
850** This should be done first so that it can successfully prevent spurious
851** compiler warnings due to subsequent content in this file and other files
852** that are included by this file.
853*/
854/************** Include msvc.h in the middle of sqliteInt.h ******************/
855/************** Begin file msvc.h ********************************************/
856/*
857** 2015 January 12
858**
859** The author disclaims copyright to this source code. In place of
860** a legal notice, here is a blessing:
861**
862** May you do good and not evil.
863** May you find forgiveness for yourself and forgive others.
864** May you share freely, never taking more than you give.
865**
866******************************************************************************
867**
868** This file contains code that is specific to MSVC.
869*/
870#ifndef SQLITE_MSVC_H
871#define SQLITE_MSVC_H
872
873#if defined(_MSC_VER)
874#pragma warning(disable : 4054)
875#pragma warning(disable : 4055)
876#pragma warning(disable : 4100)
877#pragma warning(disable : 4127)
878#pragma warning(disable : 4130)
879#pragma warning(disable : 4152)
880#pragma warning(disable : 4189)
881#pragma warning(disable : 4206)
882#pragma warning(disable : 4210)
883#pragma warning(disable : 4232)
884#pragma warning(disable : 4244)
885#pragma warning(disable : 4305)
886#pragma warning(disable : 4306)
887#pragma warning(disable : 4702)
888#pragma warning(disable : 4706)
889#endif /* defined(_MSC_VER) */
890
891#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.26.0"
1166#define SQLITE_VERSION_NUMBER 3026000
1167#define SQLITE_SOURCE_ID "2018-12-01 12:34:55 bf8c1b2b7a5960c282e543b9c293686dccff272512d08865f4600fb58238b4f9"
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#endif
1232
1233/*
1234** CAPI3REF: Test To See If The Library Is Threadsafe
1235**
1236** ^The sqlite3_threadsafe() function returns zero if and only if
1237** SQLite was compiled with mutexing code omitted due to the
1238** [SQLITE_THREADSAFE] compile-time option being set to 0.
1239**
1240** SQLite can be compiled with or without mutexes. When
1241** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
1242** are enabled and SQLite is threadsafe. When the
1243** [SQLITE_THREADSAFE] macro is 0,
1244** the mutexes are omitted. Without the mutexes, it is not safe
1245** to use SQLite concurrently from more than one thread.
1246**
1247** Enabling mutexes incurs a measurable performance penalty.
1248** So if speed is of utmost importance, it makes sense to disable
1249** the mutexes. But for maximum safety, mutexes should be enabled.
1250** ^The default behavior is for mutexes to be enabled.
1251**
1252** This interface can be used by an application to make sure that the
1253** version of SQLite that it is linking against was compiled with
1254** the desired setting of the [SQLITE_THREADSAFE] macro.
1255**
1256** This interface only reports on the compile-time mutex setting
1257** of the [SQLITE_THREADSAFE] flag. If SQLite is compiled with
1258** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but
1259** can be fully or partially disabled using a call to [sqlite3_config()]
1260** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
1261** or [SQLITE_CONFIG_SERIALIZED]. ^(The return value of the
1262** sqlite3_threadsafe() function shows only the compile-time setting of
1263** thread safety, not any run-time changes to that setting made by
1264** sqlite3_config(). In other words, the return value from sqlite3_threadsafe()
1265** is unchanged by calls to sqlite3_config().)^
1266**
1267** See the [threading mode] documentation for additional information.
1268*/
1269SQLITE_API int sqlite3_threadsafe(void);
1270
1271/*
1272** CAPI3REF: Database Connection Handle
1273** KEYWORDS: {database connection} {database connections}
1274**
1275** Each open SQLite database is represented by a pointer to an instance of
1276** the opaque structure named "sqlite3". It is useful to think of an sqlite3
1277** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and
1278** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
1279** and [sqlite3_close_v2()] are its destructors. There are many other
1280** interfaces (such as
1281** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
1282** [sqlite3_busy_timeout()] to name but three) that are methods on an
1283** sqlite3 object.
1284*/
1285typedef struct sqlite3 sqlite3;
1286
1287/*
1288** CAPI3REF: 64-Bit Integer Types
1289** KEYWORDS: sqlite_int64 sqlite_uint64
1290**
1291** Because there is no cross-platform way to specify 64-bit integer types
1292** SQLite includes typedefs for 64-bit signed and unsigned integers.
1293**
1294** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
1295** The sqlite_int64 and sqlite_uint64 types are supported for backwards
1296** compatibility only.
1297**
1298** ^The sqlite3_int64 and sqlite_int64 types can store integer values
1299** between -9223372036854775808 and +9223372036854775807 inclusive. ^The
1300** sqlite3_uint64 and sqlite_uint64 types can store integer values
1301** between 0 and +18446744073709551615 inclusive.
1302*/
1303#ifdef SQLITE_INT64_TYPE
1304 typedef SQLITE_INT64_TYPE sqlite_int64;
1305# ifdef SQLITE_UINT64_TYPE
1306 typedef SQLITE_UINT64_TYPE sqlite_uint64;
1307# else
1308 typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
1309# endif
1310#elif defined(_MSC_VER) || defined(__BORLANDC__)
1311 typedef __int64 sqlite_int64;
1312 typedef unsigned __int64 sqlite_uint64;
1313#else
1314 typedef long long int sqlite_int64;
1315 typedef unsigned long long int sqlite_uint64;
1316#endif
1317typedef sqlite_int64 sqlite3_int64;
1318typedef sqlite_uint64 sqlite3_uint64;
1319
1320/*
1321** If compiling for a processor that lacks floating point support,
1322** substitute integer for floating-point.
1323*/
1324#ifdef SQLITE_OMIT_FLOATING_POINT
1325# define double sqlite3_int64
1326#endif
1327
1328/*
1329** CAPI3REF: Closing A Database Connection
1330** DESTRUCTOR: sqlite3
1331**
1332** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
1333** for the [sqlite3] object.
1334** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if
1335** the [sqlite3] object is successfully destroyed and all associated
1336** resources are deallocated.
1337**
1338** ^If the database connection is associated with unfinalized prepared
1339** statements or unfinished sqlite3_backup objects then sqlite3_close()
1340** will leave the database connection open and return [SQLITE_BUSY].
1341** ^If sqlite3_close_v2() is called with unfinalized prepared statements
1342** and/or unfinished sqlite3_backups, then the database connection becomes
1343** an unusable "zombie" which will automatically be deallocated when the
1344** last prepared statement is finalized or the last sqlite3_backup is
1345** finished. The sqlite3_close_v2() interface is intended for use with
1346** host languages that are garbage collected, and where the order in which
1347** destructors are called is arbitrary.
1348**
1349** Applications should [sqlite3_finalize | finalize] all [prepared statements],
1350** [sqlite3_blob_close | close] all [BLOB handles], and
1351** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
1352** with the [sqlite3] object prior to attempting to close the object. ^If
1353** sqlite3_close_v2() is called on a [database connection] that still has
1354** outstanding [prepared statements], [BLOB handles], and/or
1355** [sqlite3_backup] objects then it returns [SQLITE_OK] and the deallocation
1356** of resources is deferred until all [prepared statements], [BLOB handles],
1357** and [sqlite3_backup] objects are also destroyed.
1358**
1359** ^If an [sqlite3] object is destroyed while a transaction is open,
1360** the transaction is automatically rolled back.
1361**
1362** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)]
1363** must be either a NULL
1364** pointer or an [sqlite3] object pointer obtained
1365** from [sqlite3_open()], [sqlite3_open16()], or
1366** [sqlite3_open_v2()], and not previously closed.
1367** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
1368** argument is a harmless no-op.
1369*/
1370SQLITE_API int sqlite3_close(sqlite3*);
1371SQLITE_API int sqlite3_close_v2(sqlite3*);
1372
1373/*
1374** The type for a callback function.
1375** This is legacy and deprecated. It is included for historical
1376** compatibility and is not documented.
1377*/
1378typedef int (*sqlite3_callback)(void*,int,char**, char**);
1379
1380/*
1381** CAPI3REF: One-Step Query Execution Interface
1382** METHOD: sqlite3
1383**
1384** The sqlite3_exec() interface is a convenience wrapper around
1385** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
1386** that allows an application to run multiple statements of SQL
1387** without having to use a lot of C code.
1388**
1389** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
1390** semicolon-separate SQL statements passed into its 2nd argument,
1391** in the context of the [database connection] passed in as its 1st
1392** argument. ^If the callback function of the 3rd argument to
1393** sqlite3_exec() is not NULL, then it is invoked for each result row
1394** coming out of the evaluated SQL statements. ^The 4th argument to
1395** sqlite3_exec() is relayed through to the 1st argument of each
1396** callback invocation. ^If the callback pointer to sqlite3_exec()
1397** is NULL, then no callback is ever invoked and result rows are
1398** ignored.
1399**
1400** ^If an error occurs while evaluating the SQL statements passed into
1401** sqlite3_exec(), then execution of the current statement stops and
1402** subsequent statements are skipped. ^If the 5th parameter to sqlite3_exec()
1403** is not NULL then any error message is written into memory obtained
1404** from [sqlite3_malloc()] and passed back through the 5th parameter.
1405** To avoid memory leaks, the application should invoke [sqlite3_free()]
1406** on error message strings returned through the 5th parameter of
1407** sqlite3_exec() after the error message string is no longer needed.
1408** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
1409** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
1410** NULL before returning.
1411**
1412** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
1413** routine returns SQLITE_ABORT without invoking the callback again and
1414** without running any subsequent SQL statements.
1415**
1416** ^The 2nd argument to the sqlite3_exec() callback function is the
1417** number of columns in the result. ^The 3rd argument to the sqlite3_exec()
1418** callback is an array of pointers to strings obtained as if from
1419** [sqlite3_column_text()], one for each column. ^If an element of a
1420** result row is NULL then the corresponding string pointer for the
1421** sqlite3_exec() callback is a NULL pointer. ^The 4th argument to the
1422** sqlite3_exec() callback is an array of pointers to strings where each
1423** entry represents the name of corresponding result column as obtained
1424** from [sqlite3_column_name()].
1425**
1426** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
1427** to an empty string, or a pointer that contains only whitespace and/or
1428** SQL comments, then no SQL statements are evaluated and the database
1429** is not changed.
1430**
1431** Restrictions:
1432**
1433** <ul>
1434** <li> The application must ensure that the 1st parameter to sqlite3_exec()
1435** is a valid and open [database connection].
1436** <li> The application must not close the [database connection] specified by
1437** the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
1438** <li> The application must not modify the SQL statement text passed into
1439** the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
1440** </ul>
1441*/
1442SQLITE_API int sqlite3_exec(
1443 sqlite3*, /* An open database */
1444 const char *sql, /* SQL to be evaluated */
1445 int (*callback)(void*,int,char**,char**), /* Callback function */
1446 void *, /* 1st argument to callback */
1447 char **errmsg /* Error msg written here */
1448);
1449
1450/*
1451** CAPI3REF: Result Codes
1452** KEYWORDS: {result code definitions}
1453**
1454** Many SQLite functions return an integer result code from the set shown
1455** here in order to indicate success or failure.
1456**
1457** New error codes may be added in future versions of SQLite.
1458**
1459** See also: [extended result code definitions]
1460*/
1461#define SQLITE_OK 0 /* Successful result */
1462/* beginning-of-error-codes */
1463#define SQLITE_ERROR 1 /* Generic error */
1464#define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */
1465#define SQLITE_PERM 3 /* Access permission denied */
1466#define SQLITE_ABORT 4 /* Callback routine requested an abort */
1467#define SQLITE_BUSY 5 /* The database file is locked */
1468#define SQLITE_LOCKED 6 /* A table in the database is locked */
1469#define SQLITE_NOMEM 7 /* A malloc() failed */
1470#define SQLITE_READONLY 8 /* Attempt to write a readonly database */
1471#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/
1472#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
1473#define SQLITE_CORRUPT 11 /* The database disk image is malformed */
1474#define SQLITE_NOTFOUND 12 /* Unknown opcode in sqlite3_file_control() */
1475#define SQLITE_FULL 13 /* Insertion failed because database is full */
1476#define SQLITE_CANTOPEN 14 /* Unable to open the database file */
1477#define SQLITE_PROTOCOL 15 /* Database lock protocol error */
1478#define SQLITE_EMPTY 16 /* Internal use only */
1479#define SQLITE_SCHEMA 17 /* The database schema changed */
1480#define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */
1481#define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */
1482#define SQLITE_MISMATCH 20 /* Data type mismatch */
1483#define SQLITE_MISUSE 21 /* Library used incorrectly */
1484#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */
1485#define SQLITE_AUTH 23 /* Authorization denied */
1486#define SQLITE_FORMAT 24 /* Not used */
1487#define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */
1488#define SQLITE_NOTADB 26 /* File opened that is not a database file */
1489#define SQLITE_NOTICE 27 /* Notifications from sqlite3_log() */
1490#define SQLITE_WARNING 28 /* Warnings from sqlite3_log() */
1491#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */
1492#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
1493/* end-of-error-codes */
1494
1495/*
1496** CAPI3REF: Extended Result Codes
1497** KEYWORDS: {extended result code definitions}
1498**
1499** In its default configuration, SQLite API routines return one of 30 integer
1500** [result codes]. However, experience has shown that many of
1501** these result codes are too coarse-grained. They do not provide as
1502** much information about problems as programmers might like. In an effort to
1503** address this, newer versions of SQLite (version 3.3.8 [dateof:3.3.8]
1504** and later) include
1505** support for additional result codes that provide more detailed information
1506** about errors. These [extended result codes] are enabled or disabled
1507** on a per database connection basis using the
1508** [sqlite3_extended_result_codes()] API. Or, the extended code for
1509** the most recent error can be obtained using
1510** [sqlite3_extended_errcode()].
1511*/
1512#define SQLITE_ERROR_MISSING_COLLSEQ (SQLITE_ERROR | (1<<8))
1513#define SQLITE_ERROR_RETRY (SQLITE_ERROR | (2<<8))
1514#define SQLITE_ERROR_SNAPSHOT (SQLITE_ERROR | (3<<8))
1515#define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8))
1516#define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8))
1517#define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8))
1518#define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8))
1519#define SQLITE_IOERR_DIR_FSYNC (SQLITE_IOERR | (5<<8))
1520#define SQLITE_IOERR_TRUNCATE (SQLITE_IOERR | (6<<8))
1521#define SQLITE_IOERR_FSTAT (SQLITE_IOERR | (7<<8))
1522#define SQLITE_IOERR_UNLOCK (SQLITE_IOERR | (8<<8))
1523#define SQLITE_IOERR_RDLOCK (SQLITE_IOERR | (9<<8))
1524#define SQLITE_IOERR_DELETE (SQLITE_IOERR | (10<<8))
1525#define SQLITE_IOERR_BLOCKED (SQLITE_IOERR | (11<<8))
1526#define SQLITE_IOERR_NOMEM (SQLITE_IOERR | (12<<8))
1527#define SQLITE_IOERR_ACCESS (SQLITE_IOERR | (13<<8))
1528#define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
1529#define SQLITE_IOERR_LOCK (SQLITE_IOERR | (15<<8))
1530#define SQLITE_IOERR_CLOSE (SQLITE_IOERR | (16<<8))
1531#define SQLITE_IOERR_DIR_CLOSE (SQLITE_IOERR | (17<<8))
1532#define SQLITE_IOERR_SHMOPEN (SQLITE_IOERR | (18<<8))
1533#define SQLITE_IOERR_SHMSIZE (SQLITE_IOERR | (19<<8))
1534#define SQLITE_IOERR_SHMLOCK (SQLITE_IOERR | (20<<8))
1535#define SQLITE_IOERR_SHMMAP (SQLITE_IOERR | (21<<8))
1536#define SQLITE_IOERR_SEEK (SQLITE_IOERR | (22<<8))
1537#define SQLITE_IOERR_DELETE_NOENT (SQLITE_IOERR | (23<<8))
1538#define SQLITE_IOERR_MMAP (SQLITE_IOERR | (24<<8))
1539#define SQLITE_IOERR_GETTEMPPATH (SQLITE_IOERR | (25<<8))
1540#define SQLITE_IOERR_CONVPATH (SQLITE_IOERR | (26<<8))
1541#define SQLITE_IOERR_VNODE (SQLITE_IOERR | (27<<8))
1542#define SQLITE_IOERR_AUTH (SQLITE_IOERR | (28<<8))
1543#define SQLITE_IOERR_BEGIN_ATOMIC (SQLITE_IOERR | (29<<8))
1544#define SQLITE_IOERR_COMMIT_ATOMIC (SQLITE_IOERR | (30<<8))
1545#define SQLITE_IOERR_ROLLBACK_ATOMIC (SQLITE_IOERR | (31<<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_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
1551#define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8))
1552#define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8))
1553#define SQLITE_CANTOPEN_CONVPATH (SQLITE_CANTOPEN | (4<<8))
1554#define SQLITE_CANTOPEN_DIRTYWAL (SQLITE_CANTOPEN | (5<<8)) /* Not Used */
1555#define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8))
1556#define SQLITE_CORRUPT_SEQUENCE (SQLITE_CORRUPT | (2<<8))
1557#define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8))
1558#define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8))
1559#define SQLITE_READONLY_ROLLBACK (SQLITE_READONLY | (3<<8))
1560#define SQLITE_READONLY_DBMOVED (SQLITE_READONLY | (4<<8))
1561#define SQLITE_READONLY_CANTINIT (SQLITE_READONLY | (5<<8))
1562#define SQLITE_READONLY_DIRECTORY (SQLITE_READONLY | (6<<8))
1563#define SQLITE_ABORT_ROLLBACK (SQLITE_ABORT | (2<<8))
1564#define SQLITE_CONSTRAINT_CHECK (SQLITE_CONSTRAINT | (1<<8))
1565#define SQLITE_CONSTRAINT_COMMITHOOK (SQLITE_CONSTRAINT | (2<<8))
1566#define SQLITE_CONSTRAINT_FOREIGNKEY (SQLITE_CONSTRAINT | (3<<8))
1567#define SQLITE_CONSTRAINT_FUNCTION (SQLITE_CONSTRAINT | (4<<8))
1568#define SQLITE_CONSTRAINT_NOTNULL (SQLITE_CONSTRAINT | (5<<8))
1569#define SQLITE_CONSTRAINT_PRIMARYKEY (SQLITE_CONSTRAINT | (6<<8))
1570#define SQLITE_CONSTRAINT_TRIGGER (SQLITE_CONSTRAINT | (7<<8))
1571#define SQLITE_CONSTRAINT_UNIQUE (SQLITE_CONSTRAINT | (8<<8))
1572#define SQLITE_CONSTRAINT_VTAB (SQLITE_CONSTRAINT | (9<<8))
1573#define SQLITE_CONSTRAINT_ROWID (SQLITE_CONSTRAINT |(10<<8))
1574#define SQLITE_NOTICE_RECOVER_WAL (SQLITE_NOTICE | (1<<8))
1575#define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8))
1576#define SQLITE_WARNING_AUTOINDEX (SQLITE_WARNING | (1<<8))
1577#define SQLITE_AUTH_USER (SQLITE_AUTH | (1<<8))
1578#define SQLITE_OK_LOAD_PERMANENTLY (SQLITE_OK | (1<<8))
1579
1580/*
1581** CAPI3REF: Flags For File Open Operations
1582**
1583** These bit values are intended for use in the
1584** 3rd parameter to the [sqlite3_open_v2()] interface and
1585** in the 4th parameter to the [sqlite3_vfs.xOpen] method.
1586*/
1587#define SQLITE_OPEN_READONLY 0x00000001 /* Ok for sqlite3_open_v2() */
1588#define SQLITE_OPEN_READWRITE 0x00000002 /* Ok for sqlite3_open_v2() */
1589#define SQLITE_OPEN_CREATE 0x00000004 /* Ok for sqlite3_open_v2() */
1590#define SQLITE_OPEN_DELETEONCLOSE 0x00000008 /* VFS only */
1591#define SQLITE_OPEN_EXCLUSIVE 0x00000010 /* VFS only */
1592#define SQLITE_OPEN_AUTOPROXY 0x00000020 /* VFS only */
1593#define SQLITE_OPEN_URI 0x00000040 /* Ok for sqlite3_open_v2() */
1594#define SQLITE_OPEN_MEMORY 0x00000080 /* Ok for sqlite3_open_v2() */
1595#define SQLITE_OPEN_MAIN_DB 0x00000100 /* VFS only */
1596#define SQLITE_OPEN_TEMP_DB 0x00000200 /* VFS only */
1597#define SQLITE_OPEN_TRANSIENT_DB 0x00000400 /* VFS only */
1598#define SQLITE_OPEN_MAIN_JOURNAL 0x00000800 /* VFS only */
1599#define SQLITE_OPEN_TEMP_JOURNAL 0x00001000 /* VFS only */
1600#define SQLITE_OPEN_SUBJOURNAL 0x00002000 /* VFS only */
1601#define SQLITE_OPEN_MASTER_JOURNAL 0x00004000 /* VFS only */
1602#define SQLITE_OPEN_NOMUTEX 0x00008000 /* Ok for sqlite3_open_v2() */
1603#define SQLITE_OPEN_FULLMUTEX 0x00010000 /* Ok for sqlite3_open_v2() */
1604#define SQLITE_OPEN_SHAREDCACHE 0x00020000 /* Ok for sqlite3_open_v2() */
1605#define SQLITE_OPEN_PRIVATECACHE 0x00040000 /* Ok for sqlite3_open_v2() */
1606#define SQLITE_OPEN_WAL 0x00080000 /* VFS only */
1607
1608/* Reserved: 0x00F00000 */
1609
1610/*
1611** CAPI3REF: Device Characteristics
1612**
1613** The xDeviceCharacteristics method of the [sqlite3_io_methods]
1614** object returns an integer which is a vector of these
1615** bit values expressing I/O characteristics of the mass storage
1616** device that holds the file that the [sqlite3_io_methods]
1617** refers to.
1618**
1619** The SQLITE_IOCAP_ATOMIC property means that all writes of
1620** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
1621** mean that writes of blocks that are nnn bytes in size and
1622** are aligned to an address which is an integer multiple of
1623** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
1624** that when data is appended to a file, the data is appended
1625** first then the size of the file is extended, never the other
1626** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
1627** information is written to disk in the same order as calls
1628** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
1629** after reboot following a crash or power loss, the only bytes in a
1630** file that were written at the application level might have changed
1631** and that adjacent bytes, even bytes within the same sector are
1632** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
1633** flag indicates that a file cannot be deleted when open. The
1634** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
1635** read-only media and cannot be changed even by processes with
1636** elevated privileges.
1637**
1638** The SQLITE_IOCAP_BATCH_ATOMIC property means that the underlying
1639** filesystem supports doing multiple write operations atomically when those
1640** write operations are bracketed by [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] and
1641** [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE].
1642*/
1643#define SQLITE_IOCAP_ATOMIC 0x00000001
1644#define SQLITE_IOCAP_ATOMIC512 0x00000002
1645#define SQLITE_IOCAP_ATOMIC1K 0x00000004
1646#define SQLITE_IOCAP_ATOMIC2K 0x00000008
1647#define SQLITE_IOCAP_ATOMIC4K 0x00000010
1648#define SQLITE_IOCAP_ATOMIC8K 0x00000020
1649#define SQLITE_IOCAP_ATOMIC16K 0x00000040
1650#define SQLITE_IOCAP_ATOMIC32K 0x00000080
1651#define SQLITE_IOCAP_ATOMIC64K 0x00000100
1652#define SQLITE_IOCAP_SAFE_APPEND 0x00000200
1653#define SQLITE_IOCAP_SEQUENTIAL 0x00000400
1654#define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800
1655#define SQLITE_IOCAP_POWERSAFE_OVERWRITE 0x00001000
1656#define SQLITE_IOCAP_IMMUTABLE 0x00002000
1657#define SQLITE_IOCAP_BATCH_ATOMIC 0x00004000
1658
1659/*
1660** CAPI3REF: File Locking Levels
1661**
1662** SQLite uses one of these integer values as the second
1663** argument to calls it makes to the xLock() and xUnlock() methods
1664** of an [sqlite3_io_methods] object.
1665*/
1666#define SQLITE_LOCK_NONE 0
1667#define SQLITE_LOCK_SHARED 1
1668#define SQLITE_LOCK_RESERVED 2
1669#define SQLITE_LOCK_PENDING 3
1670#define SQLITE_LOCK_EXCLUSIVE 4
1671
1672/*
1673** CAPI3REF: Synchronization Type Flags
1674**
1675** When SQLite invokes the xSync() method of an
1676** [sqlite3_io_methods] object it uses a combination of
1677** these integer values as the second argument.
1678**
1679** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
1680** sync operation only needs to flush data to mass storage. Inode
1681** information need not be flushed. If the lower four bits of the flag
1682** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics.
1683** If the lower four bits equal SQLITE_SYNC_FULL, that means
1684** to use Mac OS X style fullsync instead of fsync().
1685**
1686** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags
1687** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL
1688** settings. The [synchronous pragma] determines when calls to the
1689** xSync VFS method occur and applies uniformly across all platforms.
1690** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how
1691** energetic or rigorous or forceful the sync operations are and
1692** only make a difference on Mac OSX for the default SQLite code.
1693** (Third-party VFS implementations might also make the distinction
1694** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the
1695** operating systems natively supported by SQLite, only Mac OSX
1696** cares about the difference.)
1697*/
1698#define SQLITE_SYNC_NORMAL 0x00002
1699#define SQLITE_SYNC_FULL 0x00003
1700#define SQLITE_SYNC_DATAONLY 0x00010
1701
1702/*
1703** CAPI3REF: OS Interface Open File Handle
1704**
1705** An [sqlite3_file] object represents an open file in the
1706** [sqlite3_vfs | OS interface layer]. Individual OS interface
1707** implementations will
1708** want to subclass this object by appending additional fields
1709** for their own use. The pMethods entry is a pointer to an
1710** [sqlite3_io_methods] object that defines methods for performing
1711** I/O operations on the open file.
1712*/
1713typedef struct sqlite3_file sqlite3_file;
1714struct sqlite3_file {
1715 const struct sqlite3_io_methods *pMethods; /* Methods for an open file */
1716};
1717
1718/*
1719** CAPI3REF: OS Interface File Virtual Methods Object
1720**
1721** Every file opened by the [sqlite3_vfs.xOpen] method populates an
1722** [sqlite3_file] object (or, more commonly, a subclass of the
1723** [sqlite3_file] object) with a pointer to an instance of this object.
1724** This object defines the methods used to perform various operations
1725** against the open file represented by the [sqlite3_file] object.
1726**
1727** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element
1728** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
1729** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed. The
1730** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen]
1731** is for the [sqlite3_vfs.xOpen] to set the sqlite3_file.pMethods element
1732** to NULL.
1733**
1734** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
1735** [SQLITE_SYNC_FULL]. The first choice is the normal fsync().
1736** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY]
1737** flag may be ORed in to indicate that only the data of the file
1738** and not its inode needs to be synced.
1739**
1740** The integer values to xLock() and xUnlock() are one of
1741** <ul>
1742** <li> [SQLITE_LOCK_NONE],
1743** <li> [SQLITE_LOCK_SHARED],
1744** <li> [SQLITE_LOCK_RESERVED],
1745** <li> [SQLITE_LOCK_PENDING], or
1746** <li> [SQLITE_LOCK_EXCLUSIVE].
1747** </ul>
1748** xLock() increases the lock. xUnlock() decreases the lock.
1749** The xCheckReservedLock() method checks whether any database connection,
1750** either in this process or in some other process, is holding a RESERVED,
1751** PENDING, or EXCLUSIVE lock on the file. It returns true
1752** if such a lock exists and false otherwise.
1753**
1754** The xFileControl() method is a generic interface that allows custom
1755** VFS implementations to directly control an open file using the
1756** [sqlite3_file_control()] interface. The second "op" argument is an
1757** integer opcode. The third argument is a generic pointer intended to
1758** point to a structure that may contain arguments or space in which to
1759** write return values. Potential uses for xFileControl() might be
1760** functions to enable blocking locks with timeouts, to change the
1761** locking strategy (for example to use dot-file locks), to inquire
1762** about the status of a lock, or to break stale locks. The SQLite
1763** core reserves all opcodes less than 100 for its own use.
1764** A [file control opcodes | list of opcodes] less than 100 is available.
1765** Applications that define a custom xFileControl method should use opcodes
1766** greater than 100 to avoid conflicts. VFS implementations should
1767** return [SQLITE_NOTFOUND] for file control opcodes that they do not
1768** recognize.
1769**
1770** The xSectorSize() method returns the sector size of the
1771** device that underlies the file. The sector size is the
1772** minimum write that can be performed without disturbing
1773** other bytes in the file. The xDeviceCharacteristics()
1774** method returns a bit vector describing behaviors of the
1775** underlying device:
1776**
1777** <ul>
1778** <li> [SQLITE_IOCAP_ATOMIC]
1779** <li> [SQLITE_IOCAP_ATOMIC512]
1780** <li> [SQLITE_IOCAP_ATOMIC1K]
1781** <li> [SQLITE_IOCAP_ATOMIC2K]
1782** <li> [SQLITE_IOCAP_ATOMIC4K]
1783** <li> [SQLITE_IOCAP_ATOMIC8K]
1784** <li> [SQLITE_IOCAP_ATOMIC16K]
1785** <li> [SQLITE_IOCAP_ATOMIC32K]
1786** <li> [SQLITE_IOCAP_ATOMIC64K]
1787** <li> [SQLITE_IOCAP_SAFE_APPEND]
1788** <li> [SQLITE_IOCAP_SEQUENTIAL]
1789** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
1790** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
1791** <li> [SQLITE_IOCAP_IMMUTABLE]
1792** <li> [SQLITE_IOCAP_BATCH_ATOMIC]
1793** </ul>
1794**
1795** The SQLITE_IOCAP_ATOMIC property means that all writes of
1796** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
1797** mean that writes of blocks that are nnn bytes in size and
1798** are aligned to an address which is an integer multiple of
1799** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
1800** that when data is appended to a file, the data is appended
1801** first then the size of the file is extended, never the other
1802** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
1803** information is written to disk in the same order as calls
1804** to xWrite().
1805**
1806** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
1807** in the unread portions of the buffer with zeros. A VFS that
1808** fails to zero-fill short reads might seem to work. However,
1809** failure to zero-fill short reads will eventually lead to
1810** database corruption.
1811*/
1812typedef struct sqlite3_io_methods sqlite3_io_methods;
1813struct sqlite3_io_methods {
1814 int iVersion;
1815 int (*xClose)(sqlite3_file*);
1816 int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
1817 int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
1818 int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
1819 int (*xSync)(sqlite3_file*, int flags);
1820 int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
1821 int (*xLock)(sqlite3_file*, int);
1822 int (*xUnlock)(sqlite3_file*, int);
1823 int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
1824 int (*xFileControl)(sqlite3_file*, int op, void *pArg);
1825 int (*xSectorSize)(sqlite3_file*);
1826 int (*xDeviceCharacteristics)(sqlite3_file*);
1827 /* Methods above are valid for version 1 */
1828 int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
1829 int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
1830 void (*xShmBarrier)(sqlite3_file*);
1831 int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
1832 /* Methods above are valid for version 2 */
1833 int (*xFetch)(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
1834 int (*xUnfetch)(sqlite3_file*, sqlite3_int64 iOfst, void *p);
1835 /* Methods above are valid for version 3 */
1836 /* Additional methods may be added in future releases */
1837};
1838
1839/*
1840** CAPI3REF: Standard File Control Opcodes
1841** KEYWORDS: {file control opcodes} {file control opcode}
1842**
1843** These integer constants are opcodes for the xFileControl method
1844** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
1845** interface.
1846**
1847** <ul>
1848** <li>[[SQLITE_FCNTL_LOCKSTATE]]
1849** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This
1850** opcode causes the xFileControl method to write the current state of
1851** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
1852** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
1853** into an integer that the pArg argument points to. This capability
1854** is used during testing and is only available when the SQLITE_TEST
1855** compile-time option is used.
1856**
1857** <li>[[SQLITE_FCNTL_SIZE_HINT]]
1858** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
1859** layer a hint of how large the database file will grow to be during the
1860** current transaction. This hint is not guaranteed to be accurate but it
1861** is often close. The underlying VFS might choose to preallocate database
1862** file space based on this hint in order to help writes to the database
1863** file run faster.
1864**
1865** <li>[[SQLITE_FCNTL_CHUNK_SIZE]]
1866** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
1867** extends and truncates the database file in chunks of a size specified
1868** by the user. The fourth argument to [sqlite3_file_control()] should
1869** point to an integer (type int) containing the new chunk-size to use
1870** for the nominated database. Allocating database file space in large
1871** chunks (say 1MB at a time), may reduce file-system fragmentation and
1872** improve performance on some systems.
1873**
1874** <li>[[SQLITE_FCNTL_FILE_POINTER]]
1875** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
1876** to the [sqlite3_file] object associated with a particular database
1877** connection. See also [SQLITE_FCNTL_JOURNAL_POINTER].
1878**
1879** <li>[[SQLITE_FCNTL_JOURNAL_POINTER]]
1880** The [SQLITE_FCNTL_JOURNAL_POINTER] opcode is used to obtain a pointer
1881** to the [sqlite3_file] object associated with the journal file (either
1882** the [rollback journal] or the [write-ahead log]) for a particular database
1883** connection. See also [SQLITE_FCNTL_FILE_POINTER].
1884**
1885** <li>[[SQLITE_FCNTL_SYNC_OMITTED]]
1886** No longer in use.
1887**
1888** <li>[[SQLITE_FCNTL_SYNC]]
1889** The [SQLITE_FCNTL_SYNC] opcode is generated internally by SQLite and
1890** sent to the VFS immediately before the xSync method is invoked on a
1891** database file descriptor. Or, if the xSync method is not invoked
1892** because the user has configured SQLite with
1893** [PRAGMA synchronous | PRAGMA synchronous=OFF] it is invoked in place
1894** of the xSync method. In most cases, the pointer argument passed with
1895** this file-control is NULL. However, if the database file is being synced
1896** as part of a multi-database commit, the argument points to a nul-terminated
1897** string containing the transactions master-journal file name. VFSes that
1898** do not need this signal should silently ignore this opcode. Applications
1899** should not call [sqlite3_file_control()] with this opcode as doing so may
1900** disrupt the operation of the specialized VFSes that do require it.
1901**
1902** <li>[[SQLITE_FCNTL_COMMIT_PHASETWO]]
1903** The [SQLITE_FCNTL_COMMIT_PHASETWO] opcode is generated internally by SQLite
1904** and sent to the VFS after a transaction has been committed immediately
1905** but before the database is unlocked. VFSes that do not need this signal
1906** should silently ignore this opcode. Applications should not call
1907** [sqlite3_file_control()] with this opcode as doing so may disrupt the
1908** operation of the specialized VFSes that do require it.
1909**
1910** <li>[[SQLITE_FCNTL_WIN32_AV_RETRY]]
1911** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
1912** retry counts and intervals for certain disk I/O operations for the
1913** windows [VFS] in order to provide robustness in the presence of
1914** anti-virus programs. By default, the windows VFS will retry file read,
1915** file write, and file delete operations up to 10 times, with a delay
1916** of 25 milliseconds before the first retry and with the delay increasing
1917** by an additional 25 milliseconds with each subsequent retry. This
1918** opcode allows these two values (10 retries and 25 milliseconds of delay)
1919** to be adjusted. The values are changed for all database connections
1920** within the same process. The argument is a pointer to an array of two
1921** integers where the first integer is the new retry count and the second
1922** integer is the delay. If either integer is negative, then the setting
1923** is not changed but instead the prior value of that setting is written
1924** into the array entry, allowing the current retry settings to be
1925** interrogated. The zDbName parameter is ignored.
1926**
1927** <li>[[SQLITE_FCNTL_PERSIST_WAL]]
1928** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
1929** persistent [WAL | Write Ahead Log] setting. By default, the auxiliary
1930** write ahead log ([WAL file]) and shared memory
1931** files used for transaction control
1932** are automatically deleted when the latest connection to the database
1933** closes. Setting persistent WAL mode causes those files to persist after
1934** close. Persisting the files is useful when other processes that do not
1935** have write permission on the directory containing the database file want
1936** to read the database file, as the WAL and shared memory files must exist
1937** in order for the database to be readable. The fourth parameter to
1938** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1939** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
1940** WAL mode. If the integer is -1, then it is overwritten with the current
1941** WAL persistence setting.
1942**
1943** <li>[[SQLITE_FCNTL_POWERSAFE_OVERWRITE]]
1944** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the
1945** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting
1946** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the
1947** xDeviceCharacteristics methods. The fourth parameter to
1948** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1949** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
1950** mode. If the integer is -1, then it is overwritten with the current
1951** zero-damage mode setting.
1952**
1953** <li>[[SQLITE_FCNTL_OVERWRITE]]
1954** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
1955** a write transaction to indicate that, unless it is rolled back for some
1956** reason, the entire database file will be overwritten by the current
1957** transaction. This is used by VACUUM operations.
1958**
1959** <li>[[SQLITE_FCNTL_VFSNAME]]
1960** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of
1961** all [VFSes] in the VFS stack. The names are of all VFS shims and the
1962** final bottom-level VFS are written into memory obtained from
1963** [sqlite3_malloc()] and the result is stored in the char* variable
1964** that the fourth parameter of [sqlite3_file_control()] points to.
1965** The caller is responsible for freeing the memory when done. As with
1966** all file-control actions, there is no guarantee that this will actually
1967** do anything. Callers should initialize the char* variable to a NULL
1968** pointer in case this file-control is not implemented. This file-control
1969** is intended for diagnostic use only.
1970**
1971** <li>[[SQLITE_FCNTL_VFS_POINTER]]
1972** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level
1973** [VFSes] currently in use. ^(The argument X in
1974** sqlite3_file_control(db,SQLITE_FCNTL_VFS_POINTER,X) must be
1975** of type "[sqlite3_vfs] **". This opcodes will set *X
1976** to a pointer to the top-level VFS.)^
1977** ^When there are multiple VFS shims in the stack, this opcode finds the
1978** upper-most shim only.
1979**
1980** <li>[[SQLITE_FCNTL_PRAGMA]]
1981** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA]
1982** file control is sent to the open [sqlite3_file] object corresponding
1983** to the database file to which the pragma statement refers. ^The argument
1984** to the [SQLITE_FCNTL_PRAGMA] file control is an array of
1985** pointers to strings (char**) in which the second element of the array
1986** is the name of the pragma and the third element is the argument to the
1987** pragma or NULL if the pragma has no argument. ^The handler for an
1988** [SQLITE_FCNTL_PRAGMA] file control can optionally make the first element
1989** of the char** argument point to a string obtained from [sqlite3_mprintf()]
1990** or the equivalent and that string will become the result of the pragma or
1991** the error message if the pragma fails. ^If the
1992** [SQLITE_FCNTL_PRAGMA] file control returns [SQLITE_NOTFOUND], then normal
1993** [PRAGMA] processing continues. ^If the [SQLITE_FCNTL_PRAGMA]
1994** file control returns [SQLITE_OK], then the parser assumes that the
1995** VFS has handled the PRAGMA itself and the parser generates a no-op
1996** prepared statement if result string is NULL, or that returns a copy
1997** of the result string if the string is non-NULL.
1998** ^If the [SQLITE_FCNTL_PRAGMA] file control returns
1999** any result code other than [SQLITE_OK] or [SQLITE_NOTFOUND], that means
2000** that the VFS encountered an error while handling the [PRAGMA] and the
2001** compilation of the PRAGMA fails with an error. ^The [SQLITE_FCNTL_PRAGMA]
2002** file control occurs at the beginning of pragma statement analysis and so
2003** it is able to override built-in [PRAGMA] statements.
2004**
2005** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
2006** ^The [SQLITE_FCNTL_BUSYHANDLER]
2007** file-control may be invoked by SQLite on the database file handle
2008** shortly after it is opened in order to provide a custom VFS with access
2009** to the connections busy-handler callback. The argument is of type (void **)
2010** - an array of two (void *) values. The first (void *) actually points
2011** to a function of type (int (*)(void *)). In order to invoke the connections
2012** busy-handler, this function should be invoked with the second (void *) in
2013** the array as the only argument. If it returns non-zero, then the operation
2014** should be retried. If it returns zero, the custom VFS should abandon the
2015** current operation.
2016**
2017** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
2018** ^Application can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
2019** to have SQLite generate a
2020** temporary filename using the same algorithm that is followed to generate
2021** temporary filenames for TEMP tables and other internal uses. The
2022** argument should be a char** which will be filled with the filename
2023** written into memory obtained from [sqlite3_malloc()]. The caller should
2024** invoke [sqlite3_free()] on the result to avoid a memory leak.
2025**
2026** <li>[[SQLITE_FCNTL_MMAP_SIZE]]
2027** The [SQLITE_FCNTL_MMAP_SIZE] file control is used to query or set the
2028** maximum number of bytes that will be used for memory-mapped I/O.
2029** The argument is a pointer to a value of type sqlite3_int64 that
2030** is an advisory maximum number of bytes in the file to memory map. The
2031** pointer is overwritten with the old value. The limit is not changed if
2032** the value originally pointed to is negative, and so the current limit
2033** can be queried by passing in a pointer to a negative number. This
2034** file-control is used internally to implement [PRAGMA mmap_size].
2035**
2036** <li>[[SQLITE_FCNTL_TRACE]]
2037** The [SQLITE_FCNTL_TRACE] file control provides advisory information
2038** to the VFS about what the higher layers of the SQLite stack are doing.
2039** This file control is used by some VFS activity tracing [shims].
2040** The argument is a zero-terminated string. Higher layers in the
2041** SQLite stack may generate instances of this file control if
2042** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
2043**
2044** <li>[[SQLITE_FCNTL_HAS_MOVED]]
2045** The [SQLITE_FCNTL_HAS_MOVED] file control interprets its argument as a
2046** pointer to an integer and it writes a boolean into that integer depending
2047** on whether or not the file has been renamed, moved, or deleted since it
2048** was first opened.
2049**
2050** <li>[[SQLITE_FCNTL_WIN32_GET_HANDLE]]
2051** The [SQLITE_FCNTL_WIN32_GET_HANDLE] opcode can be used to obtain the
2052** underlying native file handle associated with a file handle. This file
2053** control interprets its argument as a pointer to a native file handle and
2054** writes the resulting value there.
2055**
2056** <li>[[SQLITE_FCNTL_WIN32_SET_HANDLE]]
2057** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
2058** opcode causes the xFileControl method to swap the file handle with the one
2059** pointed to by the pArg argument. This capability is used during testing
2060** and only needs to be supported when SQLITE_TEST is defined.
2061**
2062** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
2063** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
2064** be advantageous to block on the next WAL lock if the lock is not immediately
2065** available. The WAL subsystem issues this signal during rare
2066** circumstances in order to fix a problem with priority inversion.
2067** Applications should <em>not</em> use this file-control.
2068**
2069** <li>[[SQLITE_FCNTL_ZIPVFS]]
2070** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
2071** VFS should return SQLITE_NOTFOUND for this opcode.
2072**
2073** <li>[[SQLITE_FCNTL_RBU]]
2074** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by
2075** the RBU extension only. All other VFS should return SQLITE_NOTFOUND for
2076** this opcode.
2077**
2078** <li>[[SQLITE_FCNTL_BEGIN_ATOMIC_WRITE]]
2079** If the [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] opcode returns SQLITE_OK, then
2080** the file descriptor is placed in "batch write mode", which
2081** means all subsequent write operations will be deferred and done
2082** atomically at the next [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]. Systems
2083** that do not support batch atomic writes will return SQLITE_NOTFOUND.
2084** ^Following a successful SQLITE_FCNTL_BEGIN_ATOMIC_WRITE and prior to
2085** the closing [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] or
2086** [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE], SQLite will make
2087** no VFS interface calls on the same [sqlite3_file] file descriptor
2088** except for calls to the xWrite method and the xFileControl method
2089** with [SQLITE_FCNTL_SIZE_HINT].
2090**
2091** <li>[[SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]]
2092** The [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] opcode causes all write
2093** operations since the previous successful call to
2094** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be performed atomically.
2095** This file control returns [SQLITE_OK] if and only if the writes were
2096** all performed successfully and have been committed to persistent storage.
2097** ^Regardless of whether or not it is successful, this file control takes
2098** the file descriptor out of batch write mode so that all subsequent
2099** write operations are independent.
2100** ^SQLite will never invoke SQLITE_FCNTL_COMMIT_ATOMIC_WRITE without
2101** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
2102**
2103** <li>[[SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE]]
2104** The [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE] opcode causes all write
2105** operations since the previous successful call to
2106** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
2107** ^This file control takes the file descriptor out of batch write mode
2108** so that all subsequent write operations are independent.
2109** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
2110** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
2111**
2112** <li>[[SQLITE_FCNTL_LOCK_TIMEOUT]]
2113** The [SQLITE_FCNTL_LOCK_TIMEOUT] opcode causes attempts to obtain
2114** a file lock using the xLock or xShmLock methods of the VFS to wait
2115** for up to M milliseconds before failing, where M is the single
2116** unsigned integer parameter.
2117**
2118** <li>[[SQLITE_FCNTL_DATA_VERSION]]
2119** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to
2120** a database file. The argument is a pointer to a 32-bit unsigned integer.
2121** The "data version" for the pager is written into the pointer. The
2122** "data version" changes whenever any change occurs to the corresponding
2123** database file, either through SQL statements on the same database
2124** connection or through transactions committed by separate database
2125** connections possibly in other processes. The [sqlite3_total_changes()]
2126** interface can be used to find if any database on the connection has changed,
2127** but that interface responds to changes on TEMP as well as MAIN and does
2128** not provide a mechanism to detect changes to MAIN only. Also, the
2129** [sqlite3_total_changes()] interface responds to internal changes only and
2130** omits changes made by other database connections. The
2131** [PRAGMA data_version] command provide a mechanism to detect changes to
2132** a single attached database that occur due to other database connections,
2133** but omits changes implemented by the database connection on which it is
2134** called. This file control is the only mechanism to detect changes that
2135** happen either internally or externally and that are associated with
2136** a particular attached database.
2137** </ul>
2138*/
2139#define SQLITE_FCNTL_LOCKSTATE 1
2140#define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
2141#define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
2142#define SQLITE_FCNTL_LAST_ERRNO 4
2143#define SQLITE_FCNTL_SIZE_HINT 5
2144#define SQLITE_FCNTL_CHUNK_SIZE 6
2145#define SQLITE_FCNTL_FILE_POINTER 7
2146#define SQLITE_FCNTL_SYNC_OMITTED 8
2147#define SQLITE_FCNTL_WIN32_AV_RETRY 9
2148#define SQLITE_FCNTL_PERSIST_WAL 10
2149#define SQLITE_FCNTL_OVERWRITE 11
2150#define SQLITE_FCNTL_VFSNAME 12
2151#define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13
2152#define SQLITE_FCNTL_PRAGMA 14
2153#define SQLITE_FCNTL_BUSYHANDLER 15
2154#define SQLITE_FCNTL_TEMPFILENAME 16
2155#define SQLITE_FCNTL_MMAP_SIZE 18
2156#define SQLITE_FCNTL_TRACE 19
2157#define SQLITE_FCNTL_HAS_MOVED 20
2158#define SQLITE_FCNTL_SYNC 21
2159#define SQLITE_FCNTL_COMMIT_PHASETWO 22
2160#define SQLITE_FCNTL_WIN32_SET_HANDLE 23
2161#define SQLITE_FCNTL_WAL_BLOCK 24
2162#define SQLITE_FCNTL_ZIPVFS 25
2163#define SQLITE_FCNTL_RBU 26
2164#define SQLITE_FCNTL_VFS_POINTER 27
2165#define SQLITE_FCNTL_JOURNAL_POINTER 28
2166#define SQLITE_FCNTL_WIN32_GET_HANDLE 29
2167#define SQLITE_FCNTL_PDB 30
2168#define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE 31
2169#define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE 32
2170#define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE 33
2171#define SQLITE_FCNTL_LOCK_TIMEOUT 34
2172#define SQLITE_FCNTL_DATA_VERSION 35
2173
2174/* deprecated names */
2175#define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
2176#define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
2177#define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
2178
2179
2180/*
2181** CAPI3REF: Mutex Handle
2182**
2183** The mutex module within SQLite defines [sqlite3_mutex] to be an
2184** abstract type for a mutex object. The SQLite core never looks
2185** at the internal representation of an [sqlite3_mutex]. It only
2186** deals with pointers to the [sqlite3_mutex] object.
2187**
2188** Mutexes are created using [sqlite3_mutex_alloc()].
2189*/
2190typedef struct sqlite3_mutex sqlite3_mutex;
2191
2192/*
2193** CAPI3REF: Loadable Extension Thunk
2194**
2195** A pointer to the opaque sqlite3_api_routines structure is passed as
2196** the third parameter to entry points of [loadable extensions]. This
2197** structure must be typedefed in order to work around compiler warnings
2198** on some platforms.
2199*/
2200typedef struct sqlite3_api_routines sqlite3_api_routines;
2201
2202/*
2203** CAPI3REF: OS Interface Object
2204**
2205** An instance of the sqlite3_vfs object defines the interface between
2206** the SQLite core and the underlying operating system. The "vfs"
2207** in the name of the object stands for "virtual file system". See
2208** the [VFS | VFS documentation] for further information.
2209**
2210** The VFS interface is sometimes extended by adding new methods onto
2211** the end. Each time such an extension occurs, the iVersion field
2212** is incremented. The iVersion value started out as 1 in
2213** SQLite [version 3.5.0] on [dateof:3.5.0], then increased to 2
2214** with SQLite [version 3.7.0] on [dateof:3.7.0], and then increased
2215** to 3 with SQLite [version 3.7.6] on [dateof:3.7.6]. Additional fields
2216** may be appended to the sqlite3_vfs object and the iVersion value
2217** may increase again in future versions of SQLite.
2218** Note that the structure
2219** of the sqlite3_vfs object changes in the transition from
2220** SQLite [version 3.5.9] to [version 3.6.0] on [dateof:3.6.0]
2221** and yet the iVersion field was not modified.
2222**
2223** The szOsFile field is the size of the subclassed [sqlite3_file]
2224** structure used by this VFS. mxPathname is the maximum length of
2225** a pathname in this VFS.
2226**
2227** Registered sqlite3_vfs objects are kept on a linked list formed by
2228** the pNext pointer. The [sqlite3_vfs_register()]
2229** and [sqlite3_vfs_unregister()] interfaces manage this list
2230** in a thread-safe way. The [sqlite3_vfs_find()] interface
2231** searches the list. Neither the application code nor the VFS
2232** implementation should use the pNext pointer.
2233**
2234** The pNext field is the only field in the sqlite3_vfs
2235** structure that SQLite will ever modify. SQLite will only access
2236** or modify this field while holding a particular static mutex.
2237** The application should never modify anything within the sqlite3_vfs
2238** object once the object has been registered.
2239**
2240** The zName field holds the name of the VFS module. The name must
2241** be unique across all VFS modules.
2242**
2243** [[sqlite3_vfs.xOpen]]
2244** ^SQLite guarantees that the zFilename parameter to xOpen
2245** is either a NULL pointer or string obtained
2246** from xFullPathname() with an optional suffix added.
2247** ^If a suffix is added to the zFilename parameter, it will
2248** consist of a single "-" character followed by no more than
2249** 11 alphanumeric and/or "-" characters.
2250** ^SQLite further guarantees that
2251** the string will be valid and unchanged until xClose() is
2252** called. Because of the previous sentence,
2253** the [sqlite3_file] can safely store a pointer to the
2254** filename if it needs to remember the filename for some reason.
2255** If the zFilename parameter to xOpen is a NULL pointer then xOpen
2256** must invent its own temporary name for the file. ^Whenever the
2257** xFilename parameter is NULL it will also be the case that the
2258** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
2259**
2260** The flags argument to xOpen() includes all bits set in
2261** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()]
2262** or [sqlite3_open16()] is used, then flags includes at least
2263** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE].
2264** If xOpen() opens a file read-only then it sets *pOutFlags to
2265** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be set.
2266**
2267** ^(SQLite will also add one of the following flags to the xOpen()
2268** call, depending on the object being opened:
2269**
2270** <ul>
2271** <li> [SQLITE_OPEN_MAIN_DB]
2272** <li> [SQLITE_OPEN_MAIN_JOURNAL]
2273** <li> [SQLITE_OPEN_TEMP_DB]
2274** <li> [SQLITE_OPEN_TEMP_JOURNAL]
2275** <li> [SQLITE_OPEN_TRANSIENT_DB]
2276** <li> [SQLITE_OPEN_SUBJOURNAL]
2277** <li> [SQLITE_OPEN_MASTER_JOURNAL]
2278** <li> [SQLITE_OPEN_WAL]
2279** </ul>)^
2280**
2281** The file I/O implementation can use the object type flags to
2282** change the way it deals with files. For example, an application
2283** that does not care about crash recovery or rollback might make
2284** the open of a journal file a no-op. Writes to this journal would
2285** also be no-ops, and any attempt to read the journal would return
2286** SQLITE_IOERR. Or the implementation might recognize that a database
2287** file will be doing page-aligned sector reads and writes in a random
2288** order and set up its I/O subsystem accordingly.
2289**
2290** SQLite might also add one of the following flags to the xOpen method:
2291**
2292** <ul>
2293** <li> [SQLITE_OPEN_DELETEONCLOSE]
2294** <li> [SQLITE_OPEN_EXCLUSIVE]
2295** </ul>
2296**
2297** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
2298** deleted when it is closed. ^The [SQLITE_OPEN_DELETEONCLOSE]
2299** will be set for TEMP databases and their journals, transient
2300** databases, and subjournals.
2301**
2302** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
2303** with the [SQLITE_OPEN_CREATE] flag, which are both directly
2304** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
2305** API. The SQLITE_OPEN_EXCLUSIVE flag, when paired with the
2306** SQLITE_OPEN_CREATE, is used to indicate that file should always
2307** be created, and that it is an error if it already exists.
2308** It is <i>not</i> used to indicate the file should be opened
2309** for exclusive access.
2310**
2311** ^At least szOsFile bytes of memory are allocated by SQLite
2312** to hold the [sqlite3_file] structure passed as the third
2313** argument to xOpen. The xOpen method does not have to
2314** allocate the structure; it should just fill it in. Note that
2315** the xOpen method must set the sqlite3_file.pMethods to either
2316** a valid [sqlite3_io_methods] object or to NULL. xOpen must do
2317** this even if the open fails. SQLite expects that the sqlite3_file.pMethods
2318** element will be valid after xOpen returns regardless of the success
2319** or failure of the xOpen call.
2320**
2321** [[sqlite3_vfs.xAccess]]
2322** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
2323** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
2324** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
2325** to test whether a file is at least readable. The file can be a
2326** directory.
2327**
2328** ^SQLite will always allocate at least mxPathname+1 bytes for the
2329** output buffer xFullPathname. The exact size of the output buffer
2330** is also passed as a parameter to both methods. If the output buffer
2331** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is
2332** handled as a fatal error by SQLite, vfs implementations should endeavor
2333** to prevent this by setting mxPathname to a sufficiently large value.
2334**
2335** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64()
2336** interfaces are not strictly a part of the filesystem, but they are
2337** included in the VFS structure for completeness.
2338** The xRandomness() function attempts to return nBytes bytes
2339** of good-quality randomness into zOut. The return value is
2340** the actual number of bytes of randomness obtained.
2341** The xSleep() method causes the calling thread to sleep for at
2342** least the number of microseconds given. ^The xCurrentTime()
2343** method returns a Julian Day Number for the current date and time as
2344** a floating point value.
2345** ^The xCurrentTimeInt64() method returns, as an integer, the Julian
2346** Day Number multiplied by 86400000 (the number of milliseconds in
2347** a 24-hour day).
2348** ^SQLite will use the xCurrentTimeInt64() method to get the current
2349** date and time if that method is available (if iVersion is 2 or
2350** greater and the function pointer is not NULL) and will fall back
2351** to xCurrentTime() if xCurrentTimeInt64() is unavailable.
2352**
2353** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces
2354** are not used by the SQLite core. These optional interfaces are provided
2355** by some VFSes to facilitate testing of the VFS code. By overriding
2356** system calls with functions under its control, a test program can
2357** simulate faults and error conditions that would otherwise be difficult
2358** or impossible to induce. The set of system calls that can be overridden
2359** varies from one VFS to another, and from one version of the same VFS to the
2360** next. Applications that use these interfaces must be prepared for any
2361** or all of these interfaces to be NULL or for their behavior to change
2362** from one release to the next. Applications must not attempt to access
2363** any of these methods if the iVersion of the VFS is less than 3.
2364*/
2365typedef struct sqlite3_vfs sqlite3_vfs;
2366typedef void (*sqlite3_syscall_ptr)(void);
2367struct sqlite3_vfs {
2368 int iVersion; /* Structure version number (currently 3) */
2369 int szOsFile; /* Size of subclassed sqlite3_file */
2370 int mxPathname; /* Maximum file pathname length */
2371 sqlite3_vfs *pNext; /* Next registered VFS */
2372 const char *zName; /* Name of this virtual file system */
2373 void *pAppData; /* Pointer to application-specific data */
2374 int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
2375 int flags, int *pOutFlags);
2376 int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
2377 int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
2378 int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
2379 void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
2380 void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
2381 void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
2382 void (*xDlClose)(sqlite3_vfs*, void*);
2383 int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
2384 int (*xSleep)(sqlite3_vfs*, int microseconds);
2385 int (*xCurrentTime)(sqlite3_vfs*, double*);
2386 int (*xGetLastError)(sqlite3_vfs*, int, char *);
2387 /*
2388 ** The methods above are in version 1 of the sqlite_vfs object
2389 ** definition. Those that follow are added in version 2 or later
2390 */
2391 int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
2392 /*
2393 ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
2394 ** Those below are for version 3 and greater.
2395 */
2396 int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
2397 sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
2398 const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
2399 /*
2400 ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
2401 ** New fields may be appended in future versions. The iVersion
2402 ** value will increment whenever this happens.
2403 */
2404};
2405
2406/*
2407** CAPI3REF: Flags for the xAccess VFS method
2408**
2409** These integer constants can be used as the third parameter to
2410** the xAccess method of an [sqlite3_vfs] object. They determine
2411** what kind of permissions the xAccess method is looking for.
2412** With SQLITE_ACCESS_EXISTS, the xAccess method
2413** simply checks whether the file exists.
2414** With SQLITE_ACCESS_READWRITE, the xAccess method
2415** checks whether the named directory is both readable and writable
2416** (in other words, if files can be added, removed, and renamed within
2417** the directory).
2418** The SQLITE_ACCESS_READWRITE constant is currently used only by the
2419** [temp_store_directory pragma], though this could change in a future
2420** release of SQLite.
2421** With SQLITE_ACCESS_READ, the xAccess method
2422** checks whether the file is readable. The SQLITE_ACCESS_READ constant is
2423** currently unused, though it might be used in a future release of
2424** SQLite.
2425*/
2426#define SQLITE_ACCESS_EXISTS 0
2427#define SQLITE_ACCESS_READWRITE 1 /* Used by PRAGMA temp_store_directory */
2428#define SQLITE_ACCESS_READ 2 /* Unused */
2429
2430/*
2431** CAPI3REF: Flags for the xShmLock VFS method
2432**
2433** These integer constants define the various locking operations
2434** allowed by the xShmLock method of [sqlite3_io_methods]. The
2435** following are the only legal combinations of flags to the
2436** xShmLock method:
2437**
2438** <ul>
2439** <li> SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
2440** <li> SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
2441** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
2442** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
2443** </ul>
2444**
2445** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
2446** was given on the corresponding lock.
2447**
2448** The xShmLock method can transition between unlocked and SHARED or
2449** between unlocked and EXCLUSIVE. It cannot transition between SHARED
2450** and EXCLUSIVE.
2451*/
2452#define SQLITE_SHM_UNLOCK 1
2453#define SQLITE_SHM_LOCK 2
2454#define SQLITE_SHM_SHARED 4
2455#define SQLITE_SHM_EXCLUSIVE 8
2456
2457/*
2458** CAPI3REF: Maximum xShmLock index
2459**
2460** The xShmLock method on [sqlite3_io_methods] may use values
2461** between 0 and this upper bound as its "offset" argument.
2462** The SQLite core will never attempt to acquire or release a
2463** lock outside of this range
2464*/
2465#define SQLITE_SHM_NLOCK 8
2466
2467
2468/*
2469** CAPI3REF: Initialize The SQLite Library
2470**
2471** ^The sqlite3_initialize() routine initializes the
2472** SQLite library. ^The sqlite3_shutdown() routine
2473** deallocates any resources that were allocated by sqlite3_initialize().
2474** These routines are designed to aid in process initialization and
2475** shutdown on embedded systems. Workstation applications using
2476** SQLite normally do not need to invoke either of these routines.
2477**
2478** A call to sqlite3_initialize() is an "effective" call if it is
2479** the first time sqlite3_initialize() is invoked during the lifetime of
2480** the process, or if it is the first time sqlite3_initialize() is invoked
2481** following a call to sqlite3_shutdown(). ^(Only an effective call
2482** of sqlite3_initialize() does any initialization. All other calls
2483** are harmless no-ops.)^
2484**
2485** A call to sqlite3_shutdown() is an "effective" call if it is the first
2486** call to sqlite3_shutdown() since the last sqlite3_initialize(). ^(Only
2487** an effective call to sqlite3_shutdown() does any deinitialization.
2488** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
2489**
2490** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown()
2491** is not. The sqlite3_shutdown() interface must only be called from a
2492** single thread. All open [database connections] must be closed and all
2493** other SQLite resources must be deallocated prior to invoking
2494** sqlite3_shutdown().
2495**
2496** Among other things, ^sqlite3_initialize() will invoke
2497** sqlite3_os_init(). Similarly, ^sqlite3_shutdown()
2498** will invoke sqlite3_os_end().
2499**
2500** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success.
2501** ^If for some reason, sqlite3_initialize() is unable to initialize
2502** the library (perhaps it is unable to allocate a needed resource such
2503** as a mutex) it returns an [error code] other than [SQLITE_OK].
2504**
2505** ^The sqlite3_initialize() routine is called internally by many other
2506** SQLite interfaces so that an application usually does not need to
2507** invoke sqlite3_initialize() directly. For example, [sqlite3_open()]
2508** calls sqlite3_initialize() so the SQLite library will be automatically
2509** initialized when [sqlite3_open()] is called if it has not be initialized
2510** already. ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT]
2511** compile-time option, then the automatic calls to sqlite3_initialize()
2512** are omitted and the application must call sqlite3_initialize() directly
2513** prior to using any other SQLite interface. For maximum portability,
2514** it is recommended that applications always invoke sqlite3_initialize()
2515** directly prior to using any other SQLite interface. Future releases
2516** of SQLite may require this. In other words, the behavior exhibited
2517** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the
2518** default behavior in some future release of SQLite.
2519**
2520** The sqlite3_os_init() routine does operating-system specific
2521** initialization of the SQLite library. The sqlite3_os_end()
2522** routine undoes the effect of sqlite3_os_init(). Typical tasks
2523** performed by these routines include allocation or deallocation
2524** of static resources, initialization of global variables,
2525** setting up a default [sqlite3_vfs] module, or setting up
2526** a default configuration using [sqlite3_config()].
2527**
2528** The application should never invoke either sqlite3_os_init()
2529** or sqlite3_os_end() directly. The application should only invoke
2530** sqlite3_initialize() and sqlite3_shutdown(). The sqlite3_os_init()
2531** interface is called automatically by sqlite3_initialize() and
2532** sqlite3_os_end() is called by sqlite3_shutdown(). Appropriate
2533** implementations for sqlite3_os_init() and sqlite3_os_end()
2534** are built into SQLite when it is compiled for Unix, Windows, or OS/2.
2535** When [custom builds | built for other platforms]
2536** (using the [SQLITE_OS_OTHER=1] compile-time
2537** option) the application must supply a suitable implementation for
2538** sqlite3_os_init() and sqlite3_os_end(). An application-supplied
2539** implementation of sqlite3_os_init() or sqlite3_os_end()
2540** must return [SQLITE_OK] on success and some other [error code] upon
2541** failure.
2542*/
2543SQLITE_API int sqlite3_initialize(void);
2544SQLITE_API int sqlite3_shutdown(void);
2545SQLITE_API int sqlite3_os_init(void);
2546SQLITE_API int sqlite3_os_end(void);
2547
2548/*
2549** CAPI3REF: Configuring The SQLite Library
2550**
2551** The sqlite3_config() interface is used to make global configuration
2552** changes to SQLite in order to tune SQLite to the specific needs of
2553** the application. The default configuration is recommended for most
2554** applications and so this routine is usually not necessary. It is
2555** provided to support rare applications with unusual needs.
2556**
2557** <b>The sqlite3_config() interface is not threadsafe. The application
2558** must ensure that no other SQLite interfaces are invoked by other
2559** threads while sqlite3_config() is running.</b>
2560**
2561** The sqlite3_config() interface
2562** may only be invoked prior to library initialization using
2563** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
2564** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
2565** [sqlite3_shutdown()] then it will return SQLITE_MISUSE.
2566** Note, however, that ^sqlite3_config() can be called as part of the
2567** implementation of an application-defined [sqlite3_os_init()].
2568**
2569** The first argument to sqlite3_config() is an integer
2570** [configuration option] that determines
2571** what property of SQLite is to be configured. Subsequent arguments
2572** vary depending on the [configuration option]
2573** in the first argument.
2574**
2575** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
2576** ^If the option is unknown or SQLite is unable to set the option
2577** then this routine returns a non-zero [error code].
2578*/
2579SQLITE_API int sqlite3_config(int, ...);
2580
2581/*
2582** CAPI3REF: Configure database connections
2583** METHOD: sqlite3
2584**
2585** The sqlite3_db_config() interface is used to make configuration
2586** changes to a [database connection]. The interface is similar to
2587** [sqlite3_config()] except that the changes apply to a single
2588** [database connection] (specified in the first argument).
2589**
2590** The second argument to sqlite3_db_config(D,V,...) is the
2591** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
2592** that indicates what aspect of the [database connection] is being configured.
2593** Subsequent arguments vary depending on the configuration verb.
2594**
2595** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
2596** the call is considered successful.
2597*/
2598SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
2599
2600/*
2601** CAPI3REF: Memory Allocation Routines
2602**
2603** An instance of this object defines the interface between SQLite
2604** and low-level memory allocation routines.
2605**
2606** This object is used in only one place in the SQLite interface.
2607** A pointer to an instance of this object is the argument to
2608** [sqlite3_config()] when the configuration option is
2609** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC].
2610** By creating an instance of this object
2611** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC])
2612** during configuration, an application can specify an alternative
2613** memory allocation subsystem for SQLite to use for all of its
2614** dynamic memory needs.
2615**
2616** Note that SQLite comes with several [built-in memory allocators]
2617** that are perfectly adequate for the overwhelming majority of applications
2618** and that this object is only useful to a tiny minority of applications
2619** with specialized memory allocation requirements. This object is
2620** also used during testing of SQLite in order to specify an alternative
2621** memory allocator that simulates memory out-of-memory conditions in
2622** order to verify that SQLite recovers gracefully from such
2623** conditions.
2624**
2625** The xMalloc, xRealloc, and xFree methods must work like the
2626** malloc(), realloc() and free() functions from the standard C library.
2627** ^SQLite guarantees that the second argument to
2628** xRealloc is always a value returned by a prior call to xRoundup.
2629**
2630** xSize should return the allocated size of a memory allocation
2631** previously obtained from xMalloc or xRealloc. The allocated size
2632** is always at least as big as the requested size but may be larger.
2633**
2634** The xRoundup method returns what would be the allocated size of
2635** a memory allocation given a particular requested size. Most memory
2636** allocators round up memory allocations at least to the next multiple
2637** of 8. Some allocators round up to a larger multiple or to a power of 2.
2638** Every memory allocation request coming in through [sqlite3_malloc()]
2639** or [sqlite3_realloc()] first calls xRoundup. If xRoundup returns 0,
2640** that causes the corresponding memory allocation to fail.
2641**
2642** The xInit method initializes the memory allocator. For example,
2643** it might allocate any require mutexes or initialize internal data
2644** structures. The xShutdown method is invoked (indirectly) by
2645** [sqlite3_shutdown()] and should deallocate any resources acquired
2646** by xInit. The pAppData pointer is used as the only parameter to
2647** xInit and xShutdown.
2648**
2649** SQLite holds the [SQLITE_MUTEX_STATIC_MASTER] mutex when it invokes
2650** the xInit method, so the xInit method need not be threadsafe. The
2651** xShutdown method is only called from [sqlite3_shutdown()] so it does
2652** not need to be threadsafe either. For all other methods, SQLite
2653** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the
2654** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which
2655** it is by default) and so the methods are automatically serialized.
2656** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other
2657** methods must be threadsafe or else make their own arrangements for
2658** serialization.
2659**
2660** SQLite will never invoke xInit() more than once without an intervening
2661** call to xShutdown().
2662*/
2663typedef struct sqlite3_mem_methods sqlite3_mem_methods;
2664struct sqlite3_mem_methods {
2665 void *(*xMalloc)(int); /* Memory allocation function */
2666 void (*xFree)(void*); /* Free a prior allocation */
2667 void *(*xRealloc)(void*,int); /* Resize an allocation */
2668 int (*xSize)(void*); /* Return the size of an allocation */
2669 int (*xRoundup)(int); /* Round up request size to allocation size */
2670 int (*xInit)(void*); /* Initialize the memory allocator */
2671 void (*xShutdown)(void*); /* Deinitialize the memory allocator */
2672 void *pAppData; /* Argument to xInit() and xShutdown() */
2673};
2674
2675/*
2676** CAPI3REF: Configuration Options
2677** KEYWORDS: {configuration option}
2678**
2679** These constants are the available integer configuration options that
2680** can be passed as the first argument to the [sqlite3_config()] interface.
2681**
2682** New configuration options may be added in future releases of SQLite.
2683** Existing configuration options might be discontinued. Applications
2684** should check the return code from [sqlite3_config()] to make sure that
2685** the call worked. The [sqlite3_config()] interface will return a
2686** non-zero [error code] if a discontinued or unsupported configuration option
2687** is invoked.
2688**
2689** <dl>
2690** [[SQLITE_CONFIG_SINGLETHREAD]] <dt>SQLITE_CONFIG_SINGLETHREAD</dt>
2691** <dd>There are no arguments to this option. ^This option sets the
2692** [threading mode] to Single-thread. In other words, it disables
2693** all mutexing and puts SQLite into a mode where it can only be used
2694** by a single thread. ^If SQLite is compiled with
2695** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2696** it is not possible to change the [threading mode] from its default
2697** value of Single-thread and so [sqlite3_config()] will return
2698** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD
2699** configuration option.</dd>
2700**
2701** [[SQLITE_CONFIG_MULTITHREAD]] <dt>SQLITE_CONFIG_MULTITHREAD</dt>
2702** <dd>There are no arguments to this option. ^This option sets the
2703** [threading mode] to Multi-thread. In other words, it disables
2704** mutexing on [database connection] and [prepared statement] objects.
2705** The application is responsible for serializing access to
2706** [database connections] and [prepared statements]. But other mutexes
2707** are enabled so that SQLite will be safe to use in a multi-threaded
2708** environment as long as no two threads attempt to use the same
2709** [database connection] at the same time. ^If SQLite is compiled with
2710** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2711** it is not possible to set the Multi-thread [threading mode] and
2712** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
2713** SQLITE_CONFIG_MULTITHREAD configuration option.</dd>
2714**
2715** [[SQLITE_CONFIG_SERIALIZED]] <dt>SQLITE_CONFIG_SERIALIZED</dt>
2716** <dd>There are no arguments to this option. ^This option sets the
2717** [threading mode] to Serialized. In other words, this option enables
2718** all mutexes including the recursive
2719** mutexes on [database connection] and [prepared statement] objects.
2720** In this mode (which is the default when SQLite is compiled with
2721** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access
2722** to [database connections] and [prepared statements] so that the
2723** application is free to use the same [database connection] or the
2724** same [prepared statement] in different threads at the same time.
2725** ^If SQLite is compiled with
2726** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2727** it is not possible to set the Serialized [threading mode] and
2728** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
2729** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
2730**
2731** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
2732** <dd> ^(The SQLITE_CONFIG_MALLOC option takes a single argument which is
2733** a pointer to an instance of the [sqlite3_mem_methods] structure.
2734** The argument specifies
2735** alternative low-level memory allocation routines to be used in place of
2736** the memory allocation routines built into SQLite.)^ ^SQLite makes
2737** its own private copy of the content of the [sqlite3_mem_methods] structure
2738** before the [sqlite3_config()] call returns.</dd>
2739**
2740** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
2741** <dd> ^(The SQLITE_CONFIG_GETMALLOC option takes a single argument which
2742** is a pointer to an instance of the [sqlite3_mem_methods] structure.
2743** The [sqlite3_mem_methods]
2744** structure is filled with the currently defined memory allocation routines.)^
2745** This option can be used to overload the default memory allocation
2746** routines with a wrapper that simulations memory allocation failure or
2747** tracks memory usage, for example. </dd>
2748**
2749** [[SQLITE_CONFIG_SMALL_MALLOC]] <dt>SQLITE_CONFIG_SMALL_MALLOC</dt>
2750** <dd> ^The SQLITE_CONFIG_SMALL_MALLOC option takes single argument of
2751** type int, interpreted as a boolean, which if true provides a hint to
2752** SQLite that it should avoid large memory allocations if possible.
2753** SQLite will run faster if it is free to make large memory allocations,
2754** but some application might prefer to run slower in exchange for
2755** guarantees about memory fragmentation that are possible if large
2756** allocations are avoided. This hint is normally off.
2757** </dd>
2758**
2759** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
2760** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int,
2761** interpreted as a boolean, which enables or disables the collection of
2762** memory allocation statistics. ^(When memory allocation statistics are
2763** disabled, the following SQLite interfaces become non-operational:
2764** <ul>
2765** <li> [sqlite3_memory_used()]
2766** <li> [sqlite3_memory_highwater()]
2767** <li> [sqlite3_soft_heap_limit64()]
2768** <li> [sqlite3_status64()]
2769** </ul>)^
2770** ^Memory allocation statistics are enabled by default unless SQLite is
2771** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
2772** allocation statistics are disabled by default.
2773** </dd>
2774**
2775** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
2776** <dd> The SQLITE_CONFIG_SCRATCH option is no longer used.
2777** </dd>
2778**
2779** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
2780** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a memory pool
2781** that SQLite can use for the database page cache with the default page
2782** cache implementation.
2783** This configuration option is a no-op if an application-define page
2784** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2].
2785** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to
2786** 8-byte aligned memory (pMem), the size of each page cache line (sz),
2787** and the number of cache lines (N).
2788** The sz argument should be the size of the largest database page
2789** (a power of two between 512 and 65536) plus some extra bytes for each
2790** page header. ^The number of extra bytes needed by the page header
2791** can be determined using [SQLITE_CONFIG_PCACHE_HDRSZ].
2792** ^It is harmless, apart from the wasted memory,
2793** for the sz parameter to be larger than necessary. The pMem
2794** argument must be either a NULL pointer or a pointer to an 8-byte
2795** aligned block of memory of at least sz*N bytes, otherwise
2796** subsequent behavior is undefined.
2797** ^When pMem is not NULL, SQLite will strive to use the memory provided
2798** to satisfy page cache needs, falling back to [sqlite3_malloc()] if
2799** a page cache line is larger than sz bytes or if all of the pMem buffer
2800** is exhausted.
2801** ^If pMem is NULL and N is non-zero, then each database connection
2802** does an initial bulk allocation for page cache memory
2803** from [sqlite3_malloc()] sufficient for N cache lines if N is positive or
2804** of -1024*N bytes if N is negative, . ^If additional
2805** page cache memory is needed beyond what is provided by the initial
2806** allocation, then SQLite goes to [sqlite3_malloc()] separately for each
2807** additional cache line. </dd>
2808**
2809** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
2810** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer
2811** that SQLite will use for all of its dynamic memory allocation needs
2812** beyond those provided for by [SQLITE_CONFIG_PAGECACHE].
2813** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled
2814** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns
2815** [SQLITE_ERROR] if invoked otherwise.
2816** ^There are three arguments to SQLITE_CONFIG_HEAP:
2817** An 8-byte aligned pointer to the memory,
2818** the number of bytes in the memory buffer, and the minimum allocation size.
2819** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
2820** to using its default memory allocator (the system malloc() implementation),
2821** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the
2822** memory pointer is not NULL then the alternative memory
2823** allocator is engaged to handle all of SQLites memory allocation needs.
2824** The first pointer (the memory pointer) must be aligned to an 8-byte
2825** boundary or subsequent behavior of SQLite will be undefined.
2826** The minimum allocation size is capped at 2**12. Reasonable values
2827** for the minimum allocation size are 2**5 through 2**8.</dd>
2828**
2829** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
2830** <dd> ^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a
2831** pointer to an instance of the [sqlite3_mutex_methods] structure.
2832** The argument specifies alternative low-level mutex routines to be used
2833** in place the mutex routines built into SQLite.)^ ^SQLite makes a copy of
2834** the content of the [sqlite3_mutex_methods] structure before the call to
2835** [sqlite3_config()] returns. ^If SQLite is compiled with
2836** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2837** the entire mutexing subsystem is omitted from the build and hence calls to
2838** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
2839** return [SQLITE_ERROR].</dd>
2840**
2841** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
2842** <dd> ^(The SQLITE_CONFIG_GETMUTEX option takes a single argument which
2843** is a pointer to an instance of the [sqlite3_mutex_methods] structure. The
2844** [sqlite3_mutex_methods]
2845** structure is filled with the currently defined mutex routines.)^
2846** This option can be used to overload the default mutex allocation
2847** routines with a wrapper used to track mutex usage for performance
2848** profiling or testing, for example. ^If SQLite is compiled with
2849** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2850** the entire mutexing subsystem is omitted from the build and hence calls to
2851** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
2852** return [SQLITE_ERROR].</dd>
2853**
2854** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
2855** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine
2856** the default size of lookaside memory on each [database connection].
2857** The first argument is the
2858** size of each lookaside buffer slot and the second is the number of
2859** slots allocated to each database connection.)^ ^(SQLITE_CONFIG_LOOKASIDE
2860** sets the <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
2861** option to [sqlite3_db_config()] can be used to change the lookaside
2862** configuration on individual connections.)^ </dd>
2863**
2864** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
2865** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is
2866** a pointer to an [sqlite3_pcache_methods2] object. This object specifies
2867** the interface to a custom page cache implementation.)^
2868** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.</dd>
2869**
2870** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
2871** <dd> ^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which
2872** is a pointer to an [sqlite3_pcache_methods2] object. SQLite copies of
2873** the current page cache implementation into that object.)^ </dd>
2874**
2875** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
2876** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite
2877** global [error log].
2878** (^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
2879** function with a call signature of void(*)(void*,int,const char*),
2880** and a pointer to void. ^If the function pointer is not NULL, it is
2881** invoked by [sqlite3_log()] to process each logging event. ^If the
2882** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
2883** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is
2884** passed through as the first parameter to the application-defined logger
2885** function whenever that function is invoked. ^The second parameter to
2886** the logger function is a copy of the first parameter to the corresponding
2887** [sqlite3_log()] call and is intended to be a [result code] or an
2888** [extended result code]. ^The third parameter passed to the logger is
2889** log message after formatting via [sqlite3_snprintf()].
2890** The SQLite logging interface is not reentrant; the logger function
2891** supplied by the application must not invoke any SQLite interface.
2892** In a multi-threaded application, the application-defined logger
2893** function must be threadsafe. </dd>
2894**
2895** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
2896** <dd>^(The SQLITE_CONFIG_URI option takes a single argument of type int.
2897** If non-zero, then URI handling is globally enabled. If the parameter is zero,
2898** then URI handling is globally disabled.)^ ^If URI handling is globally
2899** enabled, all filenames passed to [sqlite3_open()], [sqlite3_open_v2()],
2900** [sqlite3_open16()] or
2901** specified as part of [ATTACH] commands are interpreted as URIs, regardless
2902** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
2903** connection is opened. ^If it is globally disabled, filenames are
2904** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
2905** database connection is opened. ^(By default, URI handling is globally
2906** disabled. The default value may be changed by compiling with the
2907** [SQLITE_USE_URI] symbol defined.)^
2908**
2909** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
2910** <dd>^The SQLITE_CONFIG_COVERING_INDEX_SCAN option takes a single integer
2911** argument which is interpreted as a boolean in order to enable or disable
2912** the use of covering indices for full table scans in the query optimizer.
2913** ^The default setting is determined
2914** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
2915** if that compile-time option is omitted.
2916** The ability to disable the use of covering indices for full table scans
2917** is because some incorrectly coded legacy applications might malfunction
2918** when the optimization is enabled. Providing the ability to
2919** disable the optimization allows the older, buggy application code to work
2920** without change even with newer versions of SQLite.
2921**
2922** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
2923** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
2924** <dd> These options are obsolete and should not be used by new code.
2925** They are retained for backwards compatibility but are now no-ops.
2926** </dd>
2927**
2928** [[SQLITE_CONFIG_SQLLOG]]
2929** <dt>SQLITE_CONFIG_SQLLOG
2930** <dd>This option is only available if sqlite is compiled with the
2931** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
2932** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
2933** The second should be of type (void*). The callback is invoked by the library
2934** in three separate circumstances, identified by the value passed as the
2935** fourth parameter. If the fourth parameter is 0, then the database connection
2936** passed as the second argument has just been opened. The third argument
2937** points to a buffer containing the name of the main database file. If the
2938** fourth parameter is 1, then the SQL statement that the third parameter
2939** points to has just been executed. Or, if the fourth parameter is 2, then
2940** the connection being passed as the second parameter is being closed. The
2941** third parameter is passed NULL In this case. An example of using this
2942** configuration option can be seen in the "test_sqllog.c" source file in
2943** the canonical SQLite source tree.</dd>
2944**
2945** [[SQLITE_CONFIG_MMAP_SIZE]]
2946** <dt>SQLITE_CONFIG_MMAP_SIZE
2947** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
2948** that are the default mmap size limit (the default setting for
2949** [PRAGMA mmap_size]) and the maximum allowed mmap size limit.
2950** ^The default setting can be overridden by each database connection using
2951** either the [PRAGMA mmap_size] command, or by using the
2952** [SQLITE_FCNTL_MMAP_SIZE] file control. ^(The maximum allowed mmap size
2953** will be silently truncated if necessary so that it does not exceed the
2954** compile-time maximum mmap size set by the
2955** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
2956** ^If either argument to this option is negative, then that argument is
2957** changed to its compile-time default.
2958**
2959** [[SQLITE_CONFIG_WIN32_HEAPSIZE]]
2960** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE
2961** <dd>^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is
2962** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro
2963** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
2964** that specifies the maximum size of the created heap.
2965**
2966** [[SQLITE_CONFIG_PCACHE_HDRSZ]]
2967** <dt>SQLITE_CONFIG_PCACHE_HDRSZ
2968** <dd>^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which
2969** is a pointer to an integer and writes into that integer the number of extra
2970** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE].
2971** The amount of extra space required can change depending on the compiler,
2972** target platform, and SQLite version.
2973**
2974** [[SQLITE_CONFIG_PMASZ]]
2975** <dt>SQLITE_CONFIG_PMASZ
2976** <dd>^The SQLITE_CONFIG_PMASZ option takes a single parameter which
2977** is an unsigned integer and sets the "Minimum PMA Size" for the multithreaded
2978** sorter to that integer. The default minimum PMA Size is set by the
2979** [SQLITE_SORTER_PMASZ] compile-time option. New threads are launched
2980** to help with sort operations when multithreaded sorting
2981** is enabled (using the [PRAGMA threads] command) and the amount of content
2982** to be sorted exceeds the page size times the minimum of the
2983** [PRAGMA cache_size] setting and this value.
2984**
2985** [[SQLITE_CONFIG_STMTJRNL_SPILL]]
2986** <dt>SQLITE_CONFIG_STMTJRNL_SPILL
2987** <dd>^The SQLITE_CONFIG_STMTJRNL_SPILL option takes a single parameter which
2988** becomes the [statement journal] spill-to-disk threshold.
2989** [Statement journals] are held in memory until their size (in bytes)
2990** exceeds this threshold, at which point they are written to disk.
2991** Or if the threshold is -1, statement journals are always held
2992** exclusively in memory.
2993** Since many statement journals never become large, setting the spill
2994** threshold to a value such as 64KiB can greatly reduce the amount of
2995** I/O required to support statement rollback.
2996** The default value for this setting is controlled by the
2997** [SQLITE_STMTJRNL_SPILL] compile-time option.
2998**
2999** [[SQLITE_CONFIG_SORTERREF_SIZE]]
3000** <dt>SQLITE_CONFIG_SORTERREF_SIZE
3001** <dd>The SQLITE_CONFIG_SORTERREF_SIZE option accepts a single parameter
3002** of type (int) - the new value of the sorter-reference size threshold.
3003** Usually, when SQLite uses an external sort to order records according
3004** to an ORDER BY clause, all fields required by the caller are present in the
3005** sorted records. However, if SQLite determines based on the declared type
3006** of a table column that its values are likely to be very large - larger
3007** than the configured sorter-reference size threshold - then a reference
3008** is stored in each sorted record and the required column values loaded
3009** from the database as records are returned in sorted order. The default
3010** value for this option is to never use this optimization. Specifying a
3011** negative value for this option restores the default behaviour.
3012** This option is only available if SQLite is compiled with the
3013** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option.
3014** </dl>
3015*/
3016#define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
3017#define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
3018#define SQLITE_CONFIG_SERIALIZED 3 /* nil */
3019#define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */
3020#define SQLITE_CONFIG_GETMALLOC 5 /* sqlite3_mem_methods* */
3021#define SQLITE_CONFIG_SCRATCH 6 /* No longer used */
3022#define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */
3023#define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */
3024#define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */
3025#define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */
3026#define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */
3027/* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
3028#define SQLITE_CONFIG_LOOKASIDE 13 /* int int */
3029#define SQLITE_CONFIG_PCACHE 14 /* no-op */
3030#define SQLITE_CONFIG_GETPCACHE 15 /* no-op */
3031#define SQLITE_CONFIG_LOG 16 /* xFunc, void* */
3032#define SQLITE_CONFIG_URI 17 /* int */
3033#define SQLITE_CONFIG_PCACHE2 18 /* sqlite3_pcache_methods2* */
3034#define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */
3035#define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */
3036#define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */
3037#define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */
3038#define SQLITE_CONFIG_WIN32_HEAPSIZE 23 /* int nByte */
3039#define SQLITE_CONFIG_PCACHE_HDRSZ 24 /* int *psz */
3040#define SQLITE_CONFIG_PMASZ 25 /* unsigned int szPma */
3041#define SQLITE_CONFIG_STMTJRNL_SPILL 26 /* int nByte */
3042#define SQLITE_CONFIG_SMALL_MALLOC 27 /* boolean */
3043#define SQLITE_CONFIG_SORTERREF_SIZE 28 /* int nByte */
3044
3045/*
3046** CAPI3REF: Database Connection Configuration Options
3047**
3048** These constants are the available integer configuration options that
3049** can be passed as the second argument to the [sqlite3_db_config()] interface.
3050**
3051** New configuration options may be added in future releases of SQLite.
3052** Existing configuration options might be discontinued. Applications
3053** should check the return code from [sqlite3_db_config()] to make sure that
3054** the call worked. ^The [sqlite3_db_config()] interface will return a
3055** non-zero [error code] if a discontinued or unsupported configuration option
3056** is invoked.
3057**
3058** <dl>
3059** [[SQLITE_DBCONFIG_LOOKASIDE]]
3060** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
3061** <dd> ^This option takes three additional arguments that determine the
3062** [lookaside memory allocator] configuration for the [database connection].
3063** ^The first argument (the third parameter to [sqlite3_db_config()] is a
3064** pointer to a memory buffer to use for lookaside memory.
3065** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
3066** may be NULL in which case SQLite will allocate the
3067** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
3068** size of each lookaside buffer slot. ^The third argument is the number of
3069** slots. The size of the buffer in the first argument must be greater than
3070** or equal to the product of the second and third arguments. The buffer
3071** must be aligned to an 8-byte boundary. ^If the second argument to
3072** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
3073** rounded down to the next smaller multiple of 8. ^(The lookaside memory
3074** configuration for a database connection can only be changed when that
3075** connection is not currently using lookaside memory, or in other words
3076** when the "current value" returned by
3077** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
3078** Any attempt to change the lookaside memory configuration when lookaside
3079** memory is in use leaves the configuration unchanged and returns
3080** [SQLITE_BUSY].)^</dd>
3081**
3082** [[SQLITE_DBCONFIG_ENABLE_FKEY]]
3083** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
3084** <dd> ^This option is used to enable or disable the enforcement of
3085** [foreign key constraints]. There should be two additional arguments.
3086** The first argument is an integer which is 0 to disable FK enforcement,
3087** positive to enable FK enforcement or negative to leave FK enforcement
3088** unchanged. The second parameter is a pointer to an integer into which
3089** is written 0 or 1 to indicate whether FK enforcement is off or on
3090** following this call. The second parameter may be a NULL pointer, in
3091** which case the FK enforcement setting is not reported back. </dd>
3092**
3093** [[SQLITE_DBCONFIG_ENABLE_TRIGGER]]
3094** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
3095** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
3096** There should be two additional arguments.
3097** The first argument is an integer which is 0 to disable triggers,
3098** positive to enable triggers or negative to leave the setting unchanged.
3099** The second parameter is a pointer to an integer into which
3100** is written 0 or 1 to indicate whether triggers are disabled or enabled
3101** following this call. The second parameter may be a NULL pointer, in
3102** which case the trigger setting is not reported back. </dd>
3103**
3104** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]]
3105** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
3106** <dd> ^This option is used to enable or disable the two-argument
3107** version of the [fts3_tokenizer()] function which is part of the
3108** [FTS3] full-text search engine extension.
3109** There should be two additional arguments.
3110** The first argument is an integer which is 0 to disable fts3_tokenizer() or
3111** positive to enable fts3_tokenizer() or negative to leave the setting
3112** unchanged.
3113** The second parameter is a pointer to an integer into which
3114** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled
3115** following this call. The second parameter may be a NULL pointer, in
3116** which case the new setting is not reported back. </dd>
3117**
3118** [[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION]]
3119** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt>
3120** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()]
3121** interface independently of the [load_extension()] SQL function.
3122** The [sqlite3_enable_load_extension()] API enables or disables both the
3123** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
3124** There should be two additional arguments.
3125** When the first argument to this interface is 1, then only the C-API is
3126** enabled and the SQL function remains disabled. If the first argument to
3127** this interface is 0, then both the C-API and the SQL function are disabled.
3128** If the first argument is -1, then no changes are made to state of either the
3129** C-API or the SQL function.
3130** The second parameter is a pointer to an integer into which
3131** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface
3132** is disabled or enabled following this call. The second parameter may
3133** be a NULL pointer, in which case the new setting is not reported back.
3134** </dd>
3135**
3136** [[SQLITE_DBCONFIG_MAINDBNAME]] <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
3137** <dd> ^This option is used to change the name of the "main" database
3138** schema. ^The sole argument is a pointer to a constant UTF8 string
3139** which will become the new schema name in place of "main". ^SQLite
3140** does not make a copy of the new main schema name string, so the application
3141** must ensure that the argument passed into this DBCONFIG option is unchanged
3142** until after the database connection closes.
3143** </dd>
3144**
3145** [[SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE]]
3146** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt>
3147** <dd> Usually, when a database in wal mode is closed or detached from a
3148** database handle, SQLite checks if this will mean that there are now no
3149** connections at all to the database. If so, it performs a checkpoint
3150** operation before closing the connection. This option may be used to
3151** override this behaviour. The first parameter passed to this operation
3152** is an integer - positive to disable checkpoints-on-close, or zero (the
3153** default) to enable them, and negative to leave the setting unchanged.
3154** The second parameter is a pointer to an integer
3155** into which is written 0 or 1 to indicate whether checkpoints-on-close
3156** have been disabled - 0 if they are not disabled, 1 if they are.
3157** </dd>
3158**
3159** [[SQLITE_DBCONFIG_ENABLE_QPSG]] <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
3160** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
3161** the [query planner stability guarantee] (QPSG). When the QPSG is active,
3162** a single SQL query statement will always use the same algorithm regardless
3163** of values of [bound parameters].)^ The QPSG disables some query optimizations
3164** that look at the values of bound parameters, which can make some queries
3165** slower. But the QPSG has the advantage of more predictable behavior. With
3166** the QPSG active, SQLite will always use the same query plan in the field as
3167** was used during testing in the lab.
3168** The first argument to this setting is an integer which is 0 to disable
3169** the QPSG, positive to enable QPSG, or negative to leave the setting
3170** unchanged. The second parameter is a pointer to an integer into which
3171** is written 0 or 1 to indicate whether the QPSG is disabled or enabled
3172** following this call.
3173** </dd>
3174**
3175** [[SQLITE_DBCONFIG_TRIGGER_EQP]] <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
3176** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not
3177** include output for any operations performed by trigger programs. This
3178** option is used to set or clear (the default) a flag that governs this
3179** behavior. The first parameter passed to this operation is an integer -
3180** positive to enable output for trigger programs, or zero to disable it,
3181** or negative to leave the setting unchanged.
3182** The second parameter is a pointer to an integer into which is written
3183** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
3184** it is not disabled, 1 if it is.
3185** </dd>
3186**
3187** [[SQLITE_DBCONFIG_RESET_DATABASE]] <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
3188** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run
3189** [VACUUM] in order to reset a database back to an empty database
3190** with no schema and no content. The following process works even for
3191** a badly corrupted database file:
3192** <ol>
3193** <li> If the database connection is newly opened, make sure it has read the
3194** database schema by preparing then discarding some query against the
3195** database, or calling sqlite3_table_column_metadata(), ignoring any
3196** errors. This step is only necessary if the application desires to keep
3197** the database in WAL mode after the reset if it was in WAL mode before
3198** the reset.
3199** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
3200** <li> [sqlite3_exec](db, "[VACUUM]", 0, 0, 0);
3201** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
3202** </ol>
3203** Because resetting a database is destructive and irreversible, the
3204** process requires the use of this obscure API and multiple steps to help
3205** ensure that it does not happen by accident.
3206**
3207** [[SQLITE_DBCONFIG_DEFENSIVE]] <dt>SQLITE_DBCONFIG_DEFENSIVE</dt>
3208** <dd>The SQLITE_DBCONFIG_DEFENSIVE option activates or deactivates the
3209** "defensive" flag for a database connection. When the defensive
3210** flag is enabled, language features that allow ordinary SQL to
3211** deliberately corrupt the database file are disabled. The disabled
3212** features include but are not limited to the following:
3213** <ul>
3214** <li> The [PRAGMA writable_schema=ON] statement.
3215** <li> Writes to the [sqlite_dbpage] virtual table.
3216** <li> Direct writes to [shadow tables].
3217** </ul>
3218** </dd>
3219** </dl>
3220*/
3221#define SQLITE_DBCONFIG_MAINDBNAME 1000 /* const char* */
3222#define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */
3223#define SQLITE_DBCONFIG_ENABLE_FKEY 1002 /* int int* */
3224#define SQLITE_DBCONFIG_ENABLE_TRIGGER 1003 /* int int* */
3225#define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
3226#define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
3227#define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE 1006 /* int int* */
3228#define SQLITE_DBCONFIG_ENABLE_QPSG 1007 /* int int* */
3229#define SQLITE_DBCONFIG_TRIGGER_EQP 1008 /* int int* */
3230#define SQLITE_DBCONFIG_RESET_DATABASE 1009 /* int int* */
3231#define SQLITE_DBCONFIG_DEFENSIVE 1010 /* int int* */
3232#define SQLITE_DBCONFIG_MAX 1010 /* Largest DBCONFIG */
3233
3234/*
3235** CAPI3REF: Enable Or Disable Extended Result Codes
3236** METHOD: sqlite3
3237**
3238** ^The sqlite3_extended_result_codes() routine enables or disables the
3239** [extended result codes] feature of SQLite. ^The extended result
3240** codes are disabled by default for historical compatibility.
3241*/
3242SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
3243
3244/*
3245** CAPI3REF: Last Insert Rowid
3246** METHOD: sqlite3
3247**
3248** ^Each entry in most SQLite tables (except for [WITHOUT ROWID] tables)
3249** has a unique 64-bit signed
3250** integer key called the [ROWID | "rowid"]. ^The rowid is always available
3251** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
3252** names are not also used by explicitly declared columns. ^If
3253** the table has a column of type [INTEGER PRIMARY KEY] then that column
3254** is another alias for the rowid.
3255**
3256** ^The sqlite3_last_insert_rowid(D) interface usually returns the [rowid] of
3257** the most recent successful [INSERT] into a rowid table or [virtual table]
3258** on database connection D. ^Inserts into [WITHOUT ROWID] tables are not
3259** recorded. ^If no successful [INSERT]s into rowid tables have ever occurred
3260** on the database connection D, then sqlite3_last_insert_rowid(D) returns
3261** zero.
3262**
3263** As well as being set automatically as rows are inserted into database
3264** tables, the value returned by this function may be set explicitly by
3265** [sqlite3_set_last_insert_rowid()]
3266**
3267** Some virtual table implementations may INSERT rows into rowid tables as
3268** part of committing a transaction (e.g. to flush data accumulated in memory
3269** to disk). In this case subsequent calls to this function return the rowid
3270** associated with these internal INSERT operations, which leads to
3271** unintuitive results. Virtual table implementations that do write to rowid
3272** tables in this way can avoid this problem by restoring the original
3273** rowid value using [sqlite3_set_last_insert_rowid()] before returning
3274** control to the user.
3275**
3276** ^(If an [INSERT] occurs within a trigger then this routine will
3277** return the [rowid] of the inserted row as long as the trigger is
3278** running. Once the trigger program ends, the value returned
3279** by this routine reverts to what it was before the trigger was fired.)^
3280**
3281** ^An [INSERT] that fails due to a constraint violation is not a
3282** successful [INSERT] and does not change the value returned by this
3283** routine. ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
3284** and INSERT OR ABORT make no changes to the return value of this
3285** routine when their insertion fails. ^(When INSERT OR REPLACE
3286** encounters a constraint violation, it does not fail. The
3287** INSERT continues to completion after deleting rows that caused
3288** the constraint problem so INSERT OR REPLACE will always change
3289** the return value of this interface.)^
3290**
3291** ^For the purposes of this routine, an [INSERT] is considered to
3292** be successful even if it is subsequently rolled back.
3293**
3294** This function is accessible to SQL statements via the
3295** [last_insert_rowid() SQL function].
3296**
3297** If a separate thread performs a new [INSERT] on the same
3298** database connection while the [sqlite3_last_insert_rowid()]
3299** function is running and thus changes the last insert [rowid],
3300** then the value returned by [sqlite3_last_insert_rowid()] is
3301** unpredictable and might not equal either the old or the new
3302** last insert [rowid].
3303*/
3304SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
3305
3306/*
3307** CAPI3REF: Set the Last Insert Rowid value.
3308** METHOD: sqlite3
3309**
3310** The sqlite3_set_last_insert_rowid(D, R) method allows the application to
3311** set the value returned by calling sqlite3_last_insert_rowid(D) to R
3312** without inserting a row into the database.
3313*/
3314SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3*,sqlite3_int64);
3315
3316/*
3317** CAPI3REF: Count The Number Of Rows Modified
3318** METHOD: sqlite3
3319**
3320** ^This function returns the number of rows modified, inserted or
3321** deleted by the most recently completed INSERT, UPDATE or DELETE
3322** statement on the database connection specified by the only parameter.
3323** ^Executing any other type of SQL statement does not modify the value
3324** returned by this function.
3325**
3326** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are
3327** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
3328** [foreign key actions] or [REPLACE] constraint resolution are not counted.
3329**
3330** Changes to a view that are intercepted by
3331** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value
3332** returned by sqlite3_changes() immediately after an INSERT, UPDATE or
3333** DELETE statement run on a view is always zero. Only changes made to real
3334** tables are counted.
3335**
3336** Things are more complicated if the sqlite3_changes() function is
3337** executed while a trigger program is running. This may happen if the
3338** program uses the [changes() SQL function], or if some other callback
3339** function invokes sqlite3_changes() directly. Essentially:
3340**
3341** <ul>
3342** <li> ^(Before entering a trigger program the value returned by
3343** sqlite3_changes() function is saved. After the trigger program
3344** has finished, the original value is restored.)^
3345**
3346** <li> ^(Within a trigger program each INSERT, UPDATE and DELETE
3347** statement sets the value returned by sqlite3_changes()
3348** upon completion as normal. Of course, this value will not include
3349** any changes performed by sub-triggers, as the sqlite3_changes()
3350** value will be saved and restored after each sub-trigger has run.)^
3351** </ul>
3352**
3353** ^This means that if the changes() SQL function (or similar) is used
3354** by the first INSERT, UPDATE or DELETE statement within a trigger, it
3355** returns the value as set when the calling statement began executing.
3356** ^If it is used by the second or subsequent such statement within a trigger
3357** program, the value returned reflects the number of rows modified by the
3358** previous INSERT, UPDATE or DELETE statement within the same trigger.
3359**
3360** If a separate thread makes changes on the same database connection
3361** while [sqlite3_changes()] is running then the value returned
3362** is unpredictable and not meaningful.
3363**
3364** See also:
3365** <ul>
3366** <li> the [sqlite3_total_changes()] interface
3367** <li> the [count_changes pragma]
3368** <li> the [changes() SQL function]
3369** <li> the [data_version pragma]
3370** </ul>
3371*/
3372SQLITE_API int sqlite3_changes(sqlite3*);
3373
3374/*
3375** CAPI3REF: Total Number Of Rows Modified
3376** METHOD: sqlite3
3377**
3378** ^This function returns the total number of rows inserted, modified or
3379** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed
3380** since the database connection was opened, including those executed as
3381** part of trigger programs. ^Executing any other type of SQL statement
3382** does not affect the value returned by sqlite3_total_changes().
3383**
3384** ^Changes made as part of [foreign key actions] are included in the
3385** count, but those made as part of REPLACE constraint resolution are
3386** not. ^Changes to a view that are intercepted by INSTEAD OF triggers
3387** are not counted.
3388**
3389** This the [sqlite3_total_changes(D)] interface only reports the number
3390** of rows that changed due to SQL statement run against database
3391** connection D. Any changes by other database connections are ignored.
3392** To detect changes against a database file from other database
3393** connections use the [PRAGMA data_version] command or the
3394** [SQLITE_FCNTL_DATA_VERSION] [file control].
3395**
3396** If a separate thread makes changes on the same database connection
3397** while [sqlite3_total_changes()] is running then the value
3398** returned is unpredictable and not meaningful.
3399**
3400** See also:
3401** <ul>
3402** <li> the [sqlite3_changes()] interface
3403** <li> the [count_changes pragma]
3404** <li> the [changes() SQL function]
3405** <li> the [data_version pragma]
3406** <li> the [SQLITE_FCNTL_DATA_VERSION] [file control]
3407** </ul>
3408*/
3409SQLITE_API int sqlite3_total_changes(sqlite3*);
3410
3411/*
3412** CAPI3REF: Interrupt A Long-Running Query
3413** METHOD: sqlite3
3414**
3415** ^This function causes any pending database operation to abort and
3416** return at its earliest opportunity. This routine is typically
3417** called in response to a user action such as pressing "Cancel"
3418** or Ctrl-C where the user wants a long query operation to halt
3419** immediately.
3420**
3421** ^It is safe to call this routine from a thread different from the
3422** thread that is currently running the database operation. But it
3423** is not safe to call this routine with a [database connection] that
3424** is closed or might close before sqlite3_interrupt() returns.
3425**
3426** ^If an SQL operation is very nearly finished at the time when
3427** sqlite3_interrupt() is called, then it might not have an opportunity
3428** to be interrupted and might continue to completion.
3429**
3430** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT].
3431** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE
3432** that is inside an explicit transaction, then the entire transaction
3433** will be rolled back automatically.
3434**
3435** ^The sqlite3_interrupt(D) call is in effect until all currently running
3436** SQL statements on [database connection] D complete. ^Any new SQL statements
3437** that are started after the sqlite3_interrupt() call and before the
3438** running statements reaches zero are interrupted as if they had been
3439** running prior to the sqlite3_interrupt() call. ^New SQL statements
3440** that are started after the running statement count reaches zero are
3441** not effected by the sqlite3_interrupt().
3442** ^A call to sqlite3_interrupt(D) that occurs when there are no running
3443** SQL statements is a no-op and has no effect on SQL statements
3444** that are started after the sqlite3_interrupt() call returns.
3445*/
3446SQLITE_API void sqlite3_interrupt(sqlite3*);
3447
3448/*
3449** CAPI3REF: Determine If An SQL Statement Is Complete
3450**
3451** These routines are useful during command-line input to determine if the
3452** currently entered text seems to form a complete SQL statement or
3453** if additional input is needed before sending the text into
3454** SQLite for parsing. ^These routines return 1 if the input string
3455** appears to be a complete SQL statement. ^A statement is judged to be
3456** complete if it ends with a semicolon token and is not a prefix of a
3457** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within
3458** string literals or quoted identifier names or comments are not
3459** independent tokens (they are part of the token in which they are
3460** embedded) and thus do not count as a statement terminator. ^Whitespace
3461** and comments that follow the final semicolon are ignored.
3462**
3463** ^These routines return 0 if the statement is incomplete. ^If a
3464** memory allocation fails, then SQLITE_NOMEM is returned.
3465**
3466** ^These routines do not parse the SQL statements thus
3467** will not detect syntactically incorrect SQL.
3468**
3469** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior
3470** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked
3471** automatically by sqlite3_complete16(). If that initialization fails,
3472** then the return value from sqlite3_complete16() will be non-zero
3473** regardless of whether or not the input SQL is complete.)^
3474**
3475** The input to [sqlite3_complete()] must be a zero-terminated
3476** UTF-8 string.
3477**
3478** The input to [sqlite3_complete16()] must be a zero-terminated
3479** UTF-16 string in native byte order.
3480*/
3481SQLITE_API int sqlite3_complete(const char *sql);
3482SQLITE_API int sqlite3_complete16(const void *sql);
3483
3484/*
3485** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
3486** KEYWORDS: {busy-handler callback} {busy handler}
3487** METHOD: sqlite3
3488**
3489** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X
3490** that might be invoked with argument P whenever
3491** an attempt is made to access a database table associated with
3492** [database connection] D when another thread
3493** or process has the table locked.
3494** The sqlite3_busy_handler() interface is used to implement
3495** [sqlite3_busy_timeout()] and [PRAGMA busy_timeout].
3496**
3497** ^If the busy callback is NULL, then [SQLITE_BUSY]
3498** is returned immediately upon encountering the lock. ^If the busy callback
3499** is not NULL, then the callback might be invoked with two arguments.
3500**
3501** ^The first argument to the busy handler is a copy of the void* pointer which
3502** is the third argument to sqlite3_busy_handler(). ^The second argument to
3503** the busy handler callback is the number of times that the busy handler has
3504** been invoked previously for the same locking event. ^If the
3505** busy callback returns 0, then no additional attempts are made to
3506** access the database and [SQLITE_BUSY] is returned
3507** to the application.
3508** ^If the callback returns non-zero, then another attempt
3509** is made to access the database and the cycle repeats.
3510**
3511** The presence of a busy handler does not guarantee that it will be invoked
3512** when there is lock contention. ^If SQLite determines that invoking the busy
3513** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
3514** to the application instead of invoking the
3515** busy handler.
3516** Consider a scenario where one process is holding a read lock that
3517** it is trying to promote to a reserved lock and
3518** a second process is holding a reserved lock that it is trying
3519** to promote to an exclusive lock. The first process cannot proceed
3520** because it is blocked by the second and the second process cannot
3521** proceed because it is blocked by the first. If both processes
3522** invoke the busy handlers, neither will make any progress. Therefore,
3523** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
3524** will induce the first process to release its read lock and allow
3525** the second process to proceed.
3526**
3527** ^The default busy callback is NULL.
3528**
3529** ^(There can only be a single busy handler defined for each
3530** [database connection]. Setting a new busy handler clears any
3531** previously set handler.)^ ^Note that calling [sqlite3_busy_timeout()]
3532** or evaluating [PRAGMA busy_timeout=N] will change the
3533** busy handler and thus clear any previously set busy handler.
3534**
3535** The busy callback should not take any actions which modify the
3536** database connection that invoked the busy handler. In other words,
3537** the busy handler is not reentrant. Any such actions
3538** result in undefined behavior.
3539**
3540** A busy handler must not close the database connection
3541** or [prepared statement] that invoked the busy handler.
3542*/
3543SQLITE_API int sqlite3_busy_handler(sqlite3*,int(*)(void*,int),void*);
3544
3545/*
3546** CAPI3REF: Set A Busy Timeout
3547** METHOD: sqlite3
3548**
3549** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
3550** for a specified amount of time when a table is locked. ^The handler
3551** will sleep multiple times until at least "ms" milliseconds of sleeping
3552** have accumulated. ^After at least "ms" milliseconds of sleeping,
3553** the handler returns 0 which causes [sqlite3_step()] to return
3554** [SQLITE_BUSY].
3555**
3556** ^Calling this routine with an argument less than or equal to zero
3557** turns off all busy handlers.
3558**
3559** ^(There can only be a single busy handler for a particular
3560** [database connection] at any given moment. If another busy handler
3561** was defined (using [sqlite3_busy_handler()]) prior to calling
3562** this routine, that other busy handler is cleared.)^
3563**
3564** See also: [PRAGMA busy_timeout]
3565*/
3566SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
3567
3568/*
3569** CAPI3REF: Convenience Routines For Running Queries
3570** METHOD: sqlite3
3571**
3572** This is a legacy interface that is preserved for backwards compatibility.
3573** Use of this interface is not recommended.
3574**
3575** Definition: A <b>result table</b> is memory data structure created by the
3576** [sqlite3_get_table()] interface. A result table records the
3577** complete query results from one or more queries.
3578**
3579** The table conceptually has a number of rows and columns. But
3580** these numbers are not part of the result table itself. These
3581** numbers are obtained separately. Let N be the number of rows
3582** and M be the number of columns.
3583**
3584** A result table is an array of pointers to zero-terminated UTF-8 strings.
3585** There are (N+1)*M elements in the array. The first M pointers point
3586** to zero-terminated strings that contain the names of the columns.
3587** The remaining entries all point to query results. NULL values result
3588** in NULL pointers. All other values are in their UTF-8 zero-terminated
3589** string representation as returned by [sqlite3_column_text()].
3590**
3591** A result table might consist of one or more memory allocations.
3592** It is not safe to pass a result table directly to [sqlite3_free()].
3593** A result table should be deallocated using [sqlite3_free_table()].
3594**
3595** ^(As an example of the result table format, suppose a query result
3596** is as follows:
3597**
3598** <blockquote><pre>
3599** Name | Age
3600** -----------------------
3601** Alice | 43
3602** Bob | 28
3603** Cindy | 21
3604** </pre></blockquote>
3605**
3606** There are two column (M==2) and three rows (N==3). Thus the
3607** result table has 8 entries. Suppose the result table is stored
3608** in an array names azResult. Then azResult holds this content:
3609**
3610** <blockquote><pre>
3611** azResult&#91;0] = "Name";
3612** azResult&#91;1] = "Age";
3613** azResult&#91;2] = "Alice";
3614** azResult&#91;3] = "43";
3615** azResult&#91;4] = "Bob";
3616** azResult&#91;5] = "28";
3617** azResult&#91;6] = "Cindy";
3618** azResult&#91;7] = "21";
3619** </pre></blockquote>)^
3620**
3621** ^The sqlite3_get_table() function evaluates one or more
3622** semicolon-separated SQL statements in the zero-terminated UTF-8
3623** string of its 2nd parameter and returns a result table to the
3624** pointer given in its 3rd parameter.
3625**
3626** After the application has finished with the result from sqlite3_get_table(),
3627** it must pass the result table pointer to sqlite3_free_table() in order to
3628** release the memory that was malloced. Because of the way the
3629** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling
3630** function must not try to call [sqlite3_free()] directly. Only
3631** [sqlite3_free_table()] is able to release the memory properly and safely.
3632**
3633** The sqlite3_get_table() interface is implemented as a wrapper around
3634** [sqlite3_exec()]. The sqlite3_get_table() routine does not have access
3635** to any internal data structures of SQLite. It uses only the public
3636** interface defined here. As a consequence, errors that occur in the
3637** wrapper layer outside of the internal [sqlite3_exec()] call are not
3638** reflected in subsequent calls to [sqlite3_errcode()] or
3639** [sqlite3_errmsg()].
3640*/
3641SQLITE_API int sqlite3_get_table(
3642 sqlite3 *db, /* An open database */
3643 const char *zSql, /* SQL to be evaluated */
3644 char ***pazResult, /* Results of the query */
3645 int *pnRow, /* Number of result rows written here */
3646 int *pnColumn, /* Number of result columns written here */
3647 char **pzErrmsg /* Error msg written here */
3648);
3649SQLITE_API void sqlite3_free_table(char **result);
3650
3651/*
3652** CAPI3REF: Formatted String Printing Functions
3653**
3654** These routines are work-alikes of the "printf()" family of functions
3655** from the standard C library.
3656** These routines understand most of the common formatting options from
3657** the standard library printf()
3658** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
3659** See the [built-in printf()] documentation for details.
3660**
3661** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
3662** results into memory obtained from [sqlite3_malloc64()].
3663** The strings returned by these two routines should be
3664** released by [sqlite3_free()]. ^Both routines return a
3665** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough
3666** memory to hold the resulting string.
3667**
3668** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
3669** the standard C library. The result is written into the
3670** buffer supplied as the second parameter whose size is given by
3671** the first parameter. Note that the order of the
3672** first two parameters is reversed from snprintf().)^ This is an
3673** historical accident that cannot be fixed without breaking
3674** backwards compatibility. ^(Note also that sqlite3_snprintf()
3675** returns a pointer to its buffer instead of the number of
3676** characters actually written into the buffer.)^ We admit that
3677** the number of characters written would be a more useful return
3678** value but we cannot change the implementation of sqlite3_snprintf()
3679** now without breaking compatibility.
3680**
3681** ^As long as the buffer size is greater than zero, sqlite3_snprintf()
3682** guarantees that the buffer is always zero-terminated. ^The first
3683** parameter "n" is the total size of the buffer, including space for
3684** the zero terminator. So the longest string that can be completely
3685** written will be n-1 characters.
3686**
3687** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
3688**
3689** See also: [built-in printf()], [printf() SQL function]
3690*/
3691SQLITE_API char *sqlite3_mprintf(const char*,...);
3692SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
3693SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
3694SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
3695
3696/*
3697** CAPI3REF: Memory Allocation Subsystem
3698**
3699** The SQLite core uses these three routines for all of its own
3700** internal memory allocation needs. "Core" in the previous sentence
3701** does not include operating-system specific VFS implementation. The
3702** Windows VFS uses native malloc() and free() for some operations.
3703**
3704** ^The sqlite3_malloc() routine returns a pointer to a block
3705** of memory at least N bytes in length, where N is the parameter.
3706** ^If sqlite3_malloc() is unable to obtain sufficient free
3707** memory, it returns a NULL pointer. ^If the parameter N to
3708** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
3709** a NULL pointer.
3710**
3711** ^The sqlite3_malloc64(N) routine works just like
3712** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead
3713** of a signed 32-bit integer.
3714**
3715** ^Calling sqlite3_free() with a pointer previously returned
3716** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
3717** that it might be reused. ^The sqlite3_free() routine is
3718** a no-op if is called with a NULL pointer. Passing a NULL pointer
3719** to sqlite3_free() is harmless. After being freed, memory
3720** should neither be read nor written. Even reading previously freed
3721** memory might result in a segmentation fault or other severe error.
3722** Memory corruption, a segmentation fault, or other severe error
3723** might result if sqlite3_free() is called with a non-NULL pointer that
3724** was not obtained from sqlite3_malloc() or sqlite3_realloc().
3725**
3726** ^The sqlite3_realloc(X,N) interface attempts to resize a
3727** prior memory allocation X to be at least N bytes.
3728** ^If the X parameter to sqlite3_realloc(X,N)
3729** is a NULL pointer then its behavior is identical to calling
3730** sqlite3_malloc(N).
3731** ^If the N parameter to sqlite3_realloc(X,N) is zero or
3732** negative then the behavior is exactly the same as calling
3733** sqlite3_free(X).
3734** ^sqlite3_realloc(X,N) returns a pointer to a memory allocation
3735** of at least N bytes in size or NULL if insufficient memory is available.
3736** ^If M is the size of the prior allocation, then min(N,M) bytes
3737** of the prior allocation are copied into the beginning of buffer returned
3738** by sqlite3_realloc(X,N) and the prior allocation is freed.
3739** ^If sqlite3_realloc(X,N) returns NULL and N is positive, then the
3740** prior allocation is not freed.
3741**
3742** ^The sqlite3_realloc64(X,N) interfaces works the same as
3743** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead
3744** of a 32-bit signed integer.
3745**
3746** ^If X is a memory allocation previously obtained from sqlite3_malloc(),
3747** sqlite3_malloc64(), sqlite3_realloc(), or sqlite3_realloc64(), then
3748** sqlite3_msize(X) returns the size of that memory allocation in bytes.
3749** ^The value returned by sqlite3_msize(X) might be larger than the number
3750** of bytes requested when X was allocated. ^If X is a NULL pointer then
3751** sqlite3_msize(X) returns zero. If X points to something that is not
3752** the beginning of memory allocation, or if it points to a formerly
3753** valid memory allocation that has now been freed, then the behavior
3754** of sqlite3_msize(X) is undefined and possibly harmful.
3755**
3756** ^The memory returned by sqlite3_malloc(), sqlite3_realloc(),
3757** sqlite3_malloc64(), and sqlite3_realloc64()
3758** is always aligned to at least an 8 byte boundary, or to a
3759** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
3760** option is used.
3761**
3762** In SQLite version 3.5.0 and 3.5.1, it was possible to define
3763** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in
3764** implementation of these routines to be omitted. That capability
3765** is no longer provided. Only built-in memory allocators can be used.
3766**
3767** Prior to SQLite version 3.7.10, the Windows OS interface layer called
3768** the system malloc() and free() directly when converting
3769** filenames between the UTF-8 encoding used by SQLite
3770** and whatever filename encoding is used by the particular Windows
3771** installation. Memory allocation errors were detected, but
3772** they were reported back as [SQLITE_CANTOPEN] or
3773** [SQLITE_IOERR] rather than [SQLITE_NOMEM].
3774**
3775** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()]
3776** must be either NULL or else pointers obtained from a prior
3777** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have
3778** not yet been released.
3779**
3780** The application must not read or write any part of
3781** a block of memory after it has been released using
3782** [sqlite3_free()] or [sqlite3_realloc()].
3783*/
3784SQLITE_API void *sqlite3_malloc(int);
3785SQLITE_API void *sqlite3_malloc64(sqlite3_uint64);
3786SQLITE_API void *sqlite3_realloc(void*, int);
3787SQLITE_API void *sqlite3_realloc64(void*, sqlite3_uint64);
3788SQLITE_API void sqlite3_free(void*);
3789SQLITE_API sqlite3_uint64 sqlite3_msize(void*);
3790
3791/*
3792** CAPI3REF: Memory Allocator Statistics
3793**
3794** SQLite provides these two interfaces for reporting on the status
3795** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
3796** routines, which form the built-in memory allocation subsystem.
3797**
3798** ^The [sqlite3_memory_used()] routine returns the number of bytes
3799** of memory currently outstanding (malloced but not freed).
3800** ^The [sqlite3_memory_highwater()] routine returns the maximum
3801** value of [sqlite3_memory_used()] since the high-water mark
3802** was last reset. ^The values returned by [sqlite3_memory_used()] and
3803** [sqlite3_memory_highwater()] include any overhead
3804** added by SQLite in its implementation of [sqlite3_malloc()],
3805** but not overhead added by the any underlying system library
3806** routines that [sqlite3_malloc()] may call.
3807**
3808** ^The memory high-water mark is reset to the current value of
3809** [sqlite3_memory_used()] if and only if the parameter to
3810** [sqlite3_memory_highwater()] is true. ^The value returned
3811** by [sqlite3_memory_highwater(1)] is the high-water mark
3812** prior to the reset.
3813*/
3814SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
3815SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
3816
3817/*
3818** CAPI3REF: Pseudo-Random Number Generator
3819**
3820** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
3821** select random [ROWID | ROWIDs] when inserting new records into a table that
3822** already uses the largest possible [ROWID]. The PRNG is also used for
3823** the build-in random() and randomblob() SQL functions. This interface allows
3824** applications to access the same PRNG for other purposes.
3825**
3826** ^A call to this routine stores N bytes of randomness into buffer P.
3827** ^The P parameter can be a NULL pointer.
3828**
3829** ^If this routine has not been previously called or if the previous
3830** call had N less than one or a NULL pointer for P, then the PRNG is
3831** seeded using randomness obtained from the xRandomness method of
3832** the default [sqlite3_vfs] object.
3833** ^If the previous call to this routine had an N of 1 or more and a
3834** non-NULL P then the pseudo-randomness is generated
3835** internally and without recourse to the [sqlite3_vfs] xRandomness
3836** method.
3837*/
3838SQLITE_API void sqlite3_randomness(int N, void *P);
3839
3840/*
3841** CAPI3REF: Compile-Time Authorization Callbacks
3842** METHOD: sqlite3
3843** KEYWORDS: {authorizer callback}
3844**
3845** ^This routine registers an authorizer callback with a particular
3846** [database connection], supplied in the first argument.
3847** ^The authorizer callback is invoked as SQL statements are being compiled
3848** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
3849** [sqlite3_prepare_v3()], [sqlite3_prepare16()], [sqlite3_prepare16_v2()],
3850** and [sqlite3_prepare16_v3()]. ^At various
3851** points during the compilation process, as logic is being created
3852** to perform various actions, the authorizer callback is invoked to
3853** see if those actions are allowed. ^The authorizer callback should
3854** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the
3855** specific action but allow the SQL statement to continue to be
3856** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
3857** rejected with an error. ^If the authorizer callback returns
3858** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY]
3859** then the [sqlite3_prepare_v2()] or equivalent call that triggered
3860** the authorizer will fail with an error message.
3861**
3862** When the callback returns [SQLITE_OK], that means the operation
3863** requested is ok. ^When the callback returns [SQLITE_DENY], the
3864** [sqlite3_prepare_v2()] or equivalent call that triggered the
3865** authorizer will fail with an error message explaining that
3866** access is denied.
3867**
3868** ^The first parameter to the authorizer callback is a copy of the third
3869** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
3870** to the callback is an integer [SQLITE_COPY | action code] that specifies
3871** the particular action to be authorized. ^The third through sixth parameters
3872** to the callback are either NULL pointers or zero-terminated strings
3873** that contain additional details about the action to be authorized.
3874** Applications must always be prepared to encounter a NULL pointer in any
3875** of the third through the sixth parameters of the authorization callback.
3876**
3877** ^If the action code is [SQLITE_READ]
3878** and the callback returns [SQLITE_IGNORE] then the
3879** [prepared statement] statement is constructed to substitute
3880** a NULL value in place of the table column that would have
3881** been read if [SQLITE_OK] had been returned. The [SQLITE_IGNORE]
3882** return can be used to deny an untrusted user access to individual
3883** columns of a table.
3884** ^When a table is referenced by a [SELECT] but no column values are
3885** extracted from that table (for example in a query like
3886** "SELECT count(*) FROM tab") then the [SQLITE_READ] authorizer callback
3887** is invoked once for that table with a column name that is an empty string.
3888** ^If the action code is [SQLITE_DELETE] and the callback returns
3889** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
3890** [truncate optimization] is disabled and all rows are deleted individually.
3891**
3892** An authorizer is used when [sqlite3_prepare | preparing]
3893** SQL statements from an untrusted source, to ensure that the SQL statements
3894** do not try to access data they are not allowed to see, or that they do not
3895** try to execute malicious statements that damage the database. For
3896** example, an application may allow a user to enter arbitrary
3897** SQL queries for evaluation by a database. But the application does
3898** not want the user to be able to make arbitrary changes to the
3899** database. An authorizer could then be put in place while the
3900** user-entered SQL is being [sqlite3_prepare | prepared] that
3901** disallows everything except [SELECT] statements.
3902**
3903** Applications that need to process SQL from untrusted sources
3904** might also consider lowering resource limits using [sqlite3_limit()]
3905** and limiting database size using the [max_page_count] [PRAGMA]
3906** in addition to using an authorizer.
3907**
3908** ^(Only a single authorizer can be in place on a database connection
3909** at a time. Each call to sqlite3_set_authorizer overrides the
3910** previous call.)^ ^Disable the authorizer by installing a NULL callback.
3911** The authorizer is disabled by default.
3912**
3913** The authorizer callback must not do anything that will modify
3914** the database connection that invoked the authorizer callback.
3915** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
3916** database connections for the meaning of "modify" in this paragraph.
3917**
3918** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the
3919** statement might be re-prepared during [sqlite3_step()] due to a
3920** schema change. Hence, the application should ensure that the
3921** correct authorizer callback remains in place during the [sqlite3_step()].
3922**
3923** ^Note that the authorizer callback is invoked only during
3924** [sqlite3_prepare()] or its variants. Authorization is not
3925** performed during statement evaluation in [sqlite3_step()], unless
3926** as stated in the previous paragraph, sqlite3_step() invokes
3927** sqlite3_prepare_v2() to reprepare a statement after a schema change.
3928*/
3929SQLITE_API int sqlite3_set_authorizer(
3930 sqlite3*,
3931 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
3932 void *pUserData
3933);
3934
3935/*
3936** CAPI3REF: Authorizer Return Codes
3937**
3938** The [sqlite3_set_authorizer | authorizer callback function] must
3939** return either [SQLITE_OK] or one of these two constants in order
3940** to signal SQLite whether or not the action is permitted. See the
3941** [sqlite3_set_authorizer | authorizer documentation] for additional
3942** information.
3943**
3944** Note that SQLITE_IGNORE is also used as a [conflict resolution mode]
3945** returned from the [sqlite3_vtab_on_conflict()] interface.
3946*/
3947#define SQLITE_DENY 1 /* Abort the SQL statement with an error */
3948#define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */
3949
3950/*
3951** CAPI3REF: Authorizer Action Codes
3952**
3953** The [sqlite3_set_authorizer()] interface registers a callback function
3954** that is invoked to authorize certain SQL statement actions. The
3955** second parameter to the callback is an integer code that specifies
3956** what action is being authorized. These are the integer action codes that
3957** the authorizer callback may be passed.
3958**
3959** These action code values signify what kind of operation is to be
3960** authorized. The 3rd and 4th parameters to the authorization
3961** callback function will be parameters or NULL depending on which of these
3962** codes is used as the second parameter. ^(The 5th parameter to the
3963** authorizer callback is the name of the database ("main", "temp",
3964** etc.) if applicable.)^ ^The 6th parameter to the authorizer callback
3965** is the name of the inner-most trigger or view that is responsible for
3966** the access attempt or NULL if this access attempt is directly from
3967** top-level SQL code.
3968*/
3969/******************************************* 3rd ************ 4th ***********/
3970#define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */
3971#define SQLITE_CREATE_TABLE 2 /* Table Name NULL */
3972#define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */
3973#define SQLITE_CREATE_TEMP_TABLE 4 /* Table Name NULL */
3974#define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */
3975#define SQLITE_CREATE_TEMP_VIEW 6 /* View Name NULL */
3976#define SQLITE_CREATE_TRIGGER 7 /* Trigger Name Table Name */
3977#define SQLITE_CREATE_VIEW 8 /* View Name NULL */
3978#define SQLITE_DELETE 9 /* Table Name NULL */
3979#define SQLITE_DROP_INDEX 10 /* Index Name Table Name */
3980#define SQLITE_DROP_TABLE 11 /* Table Name NULL */
3981#define SQLITE_DROP_TEMP_INDEX 12 /* Index Name Table Name */
3982#define SQLITE_DROP_TEMP_TABLE 13 /* Table Name NULL */
3983#define SQLITE_DROP_TEMP_TRIGGER 14 /* Trigger Name Table Name */
3984#define SQLITE_DROP_TEMP_VIEW 15 /* View Name NULL */
3985#define SQLITE_DROP_TRIGGER 16 /* Trigger Name Table Name */
3986#define SQLITE_DROP_VIEW 17 /* View Name NULL */
3987#define SQLITE_INSERT 18 /* Table Name NULL */
3988#define SQLITE_PRAGMA 19 /* Pragma Name 1st arg or NULL */
3989#define SQLITE_READ 20 /* Table Name Column Name */
3990#define SQLITE_SELECT 21 /* NULL NULL */
3991#define SQLITE_TRANSACTION 22 /* Operation NULL */
3992#define SQLITE_UPDATE 23 /* Table Name Column Name */
3993#define SQLITE_ATTACH 24 /* Filename NULL */
3994#define SQLITE_DETACH 25 /* Database Name NULL */
3995#define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */
3996#define SQLITE_REINDEX 27 /* Index Name NULL */
3997#define SQLITE_ANALYZE 28 /* Table Name NULL */
3998#define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */
3999#define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */
4000#define SQLITE_FUNCTION 31 /* NULL Function Name */
4001#define SQLITE_SAVEPOINT 32 /* Operation Savepoint Name */
4002#define SQLITE_COPY 0 /* No longer used */
4003#define SQLITE_RECURSIVE 33 /* NULL NULL */
4004
4005/*
4006** CAPI3REF: Tracing And Profiling Functions
4007** METHOD: sqlite3
4008**
4009** These routines are deprecated. Use the [sqlite3_trace_v2()] interface
4010** instead of the routines described here.
4011**
4012** These routines register callback functions that can be used for
4013** tracing and profiling the execution of SQL statements.
4014**
4015** ^The callback function registered by sqlite3_trace() is invoked at
4016** various times when an SQL statement is being run by [sqlite3_step()].
4017** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
4018** SQL statement text as the statement first begins executing.
4019** ^(Additional sqlite3_trace() callbacks might occur
4020** as each triggered subprogram is entered. The callbacks for triggers
4021** contain a UTF-8 SQL comment that identifies the trigger.)^
4022**
4023** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
4024** the length of [bound parameter] expansion in the output of sqlite3_trace().
4025**
4026** ^The callback function registered by sqlite3_profile() is invoked
4027** as each SQL statement finishes. ^The profile callback contains
4028** the original statement text and an estimate of wall-clock time
4029** of how long that statement took to run. ^The profile callback
4030** time is in units of nanoseconds, however the current implementation
4031** is only capable of millisecond resolution so the six least significant
4032** digits in the time are meaningless. Future versions of SQLite
4033** might provide greater resolution on the profiler callback. The
4034** sqlite3_profile() function is considered experimental and is
4035** subject to change in future versions of SQLite.
4036*/
4037SQLITE_API SQLITE_DEPRECATED void *sqlite3_trace(sqlite3*,
4038 void(*xTrace)(void*,const char*), void*);
4039SQLITE_API SQLITE_DEPRECATED void *sqlite3_profile(sqlite3*,
4040 void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
4041
4042/*
4043** CAPI3REF: SQL Trace Event Codes
4044** KEYWORDS: SQLITE_TRACE
4045**
4046** These constants identify classes of events that can be monitored
4047** using the [sqlite3_trace_v2()] tracing logic. The M argument
4048** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
4049** the following constants. ^The first argument to the trace callback
4050** is one of the following constants.
4051**
4052** New tracing constants may be added in future releases.
4053**
4054** ^A trace callback has four arguments: xCallback(T,C,P,X).
4055** ^The T argument is one of the integer type codes above.
4056** ^The C argument is a copy of the context pointer passed in as the
4057** fourth argument to [sqlite3_trace_v2()].
4058** The P and X arguments are pointers whose meanings depend on T.
4059**
4060** <dl>
4061** [[SQLITE_TRACE_STMT]] <dt>SQLITE_TRACE_STMT</dt>
4062** <dd>^An SQLITE_TRACE_STMT callback is invoked when a prepared statement
4063** first begins running and possibly at other times during the
4064** execution of the prepared statement, such as at the start of each
4065** trigger subprogram. ^The P argument is a pointer to the
4066** [prepared statement]. ^The X argument is a pointer to a string which
4067** is the unexpanded SQL text of the prepared statement or an SQL comment
4068** that indicates the invocation of a trigger. ^The callback can compute
4069** the same text that would have been returned by the legacy [sqlite3_trace()]
4070** interface by using the X argument when X begins with "--" and invoking
4071** [sqlite3_expanded_sql(P)] otherwise.
4072**
4073** [[SQLITE_TRACE_PROFILE]] <dt>SQLITE_TRACE_PROFILE</dt>
4074** <dd>^An SQLITE_TRACE_PROFILE callback provides approximately the same
4075** information as is provided by the [sqlite3_profile()] callback.
4076** ^The P argument is a pointer to the [prepared statement] and the
4077** X argument points to a 64-bit integer which is the estimated of
4078** the number of nanosecond that the prepared statement took to run.
4079** ^The SQLITE_TRACE_PROFILE callback is invoked when the statement finishes.
4080**
4081** [[SQLITE_TRACE_ROW]] <dt>SQLITE_TRACE_ROW</dt>
4082** <dd>^An SQLITE_TRACE_ROW callback is invoked whenever a prepared
4083** statement generates a single row of result.
4084** ^The P argument is a pointer to the [prepared statement] and the
4085** X argument is unused.
4086**
4087** [[SQLITE_TRACE_CLOSE]] <dt>SQLITE_TRACE_CLOSE</dt>
4088** <dd>^An SQLITE_TRACE_CLOSE callback is invoked when a database
4089** connection closes.
4090** ^The P argument is a pointer to the [database connection] object
4091** and the X argument is unused.
4092** </dl>
4093*/
4094#define SQLITE_TRACE_STMT 0x01
4095#define SQLITE_TRACE_PROFILE 0x02
4096#define SQLITE_TRACE_ROW 0x04
4097#define SQLITE_TRACE_CLOSE 0x08
4098
4099/*
4100** CAPI3REF: SQL Trace Hook
4101** METHOD: sqlite3
4102**
4103** ^The sqlite3_trace_v2(D,M,X,P) interface registers a trace callback
4104** function X against [database connection] D, using property mask M
4105** and context pointer P. ^If the X callback is
4106** NULL or if the M mask is zero, then tracing is disabled. The
4107** M argument should be the bitwise OR-ed combination of
4108** zero or more [SQLITE_TRACE] constants.
4109**
4110** ^Each call to either sqlite3_trace() or sqlite3_trace_v2() overrides
4111** (cancels) any prior calls to sqlite3_trace() or sqlite3_trace_v2().
4112**
4113** ^The X callback is invoked whenever any of the events identified by
4114** mask M occur. ^The integer return value from the callback is currently
4115** ignored, though this may change in future releases. Callback
4116** implementations should return zero to ensure future compatibility.
4117**
4118** ^A trace callback is invoked with four arguments: callback(T,C,P,X).
4119** ^The T argument is one of the [SQLITE_TRACE]
4120** constants to indicate why the callback was invoked.
4121** ^The C argument is a copy of the context pointer.
4122** The P and X arguments are pointers whose meanings depend on T.
4123**
4124** The sqlite3_trace_v2() interface is intended to replace the legacy
4125** interfaces [sqlite3_trace()] and [sqlite3_profile()], both of which
4126** are deprecated.
4127*/
4128SQLITE_API int sqlite3_trace_v2(
4129 sqlite3*,
4130 unsigned uMask,
4131 int(*xCallback)(unsigned,void*,void*,void*),
4132 void *pCtx
4133);
4134
4135/*
4136** CAPI3REF: Query Progress Callbacks
4137** METHOD: sqlite3
4138**
4139** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
4140** function X to be invoked periodically during long running calls to
4141** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for
4142** database connection D. An example use for this
4143** interface is to keep a GUI updated during a large query.
4144**
4145** ^The parameter P is passed through as the only parameter to the
4146** callback function X. ^The parameter N is the approximate number of
4147** [virtual machine instructions] that are evaluated between successive
4148** invocations of the callback X. ^If N is less than one then the progress
4149** handler is disabled.
4150**
4151** ^Only a single progress handler may be defined at one time per
4152** [database connection]; setting a new progress handler cancels the
4153** old one. ^Setting parameter X to NULL disables the progress handler.
4154** ^The progress handler is also disabled by setting N to a value less
4155** than 1.
4156**
4157** ^If the progress callback returns non-zero, the operation is
4158** interrupted. This feature can be used to implement a
4159** "Cancel" button on a GUI progress dialog box.
4160**
4161** The progress handler callback must not do anything that will modify
4162** the database connection that invoked the progress handler.
4163** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
4164** database connections for the meaning of "modify" in this paragraph.
4165**
4166*/
4167SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
4168
4169/*
4170** CAPI3REF: Opening A New Database Connection
4171** CONSTRUCTOR: sqlite3
4172**
4173** ^These routines open an SQLite database file as specified by the
4174** filename argument. ^The filename argument is interpreted as UTF-8 for
4175** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
4176** order for sqlite3_open16(). ^(A [database connection] handle is usually
4177** returned in *ppDb, even if an error occurs. The only exception is that
4178** if SQLite is unable to allocate memory to hold the [sqlite3] object,
4179** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
4180** object.)^ ^(If the database is opened (and/or created) successfully, then
4181** [SQLITE_OK] is returned. Otherwise an [error code] is returned.)^ ^The
4182** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
4183** an English language description of the error following a failure of any
4184** of the sqlite3_open() routines.
4185**
4186** ^The default encoding will be UTF-8 for databases created using
4187** sqlite3_open() or sqlite3_open_v2(). ^The default encoding for databases
4188** created using sqlite3_open16() will be UTF-16 in the native byte order.
4189**
4190** Whether or not an error occurs when it is opened, resources
4191** associated with the [database connection] handle should be released by
4192** passing it to [sqlite3_close()] when it is no longer required.
4193**
4194** The sqlite3_open_v2() interface works like sqlite3_open()
4195** except that it accepts two additional parameters for additional control
4196** over the new database connection. ^(The flags parameter to
4197** sqlite3_open_v2() can take one of
4198** the following three values, optionally combined with the
4199** [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], [SQLITE_OPEN_SHAREDCACHE],
4200** [SQLITE_OPEN_PRIVATECACHE], and/or [SQLITE_OPEN_URI] flags:)^
4201**
4202** <dl>
4203** ^(<dt>[SQLITE_OPEN_READONLY]</dt>
4204** <dd>The database is opened in read-only mode. If the database does not
4205** already exist, an error is returned.</dd>)^
4206**
4207** ^(<dt>[SQLITE_OPEN_READWRITE]</dt>
4208** <dd>The database is opened for reading and writing if possible, or reading
4209** only if the file is write protected by the operating system. In either
4210** case the database must already exist, otherwise an error is returned.</dd>)^
4211**
4212** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
4213** <dd>The database is opened for reading and writing, and is created if
4214** it does not already exist. This is the behavior that is always used for
4215** sqlite3_open() and sqlite3_open16().</dd>)^
4216** </dl>
4217**
4218** If the 3rd parameter to sqlite3_open_v2() is not one of the
4219** combinations shown above optionally combined with other
4220** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
4221** then the behavior is undefined.
4222**
4223** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection
4224** opens in the multi-thread [threading mode] as long as the single-thread
4225** mode has not been set at compile-time or start-time. ^If the
4226** [SQLITE_OPEN_FULLMUTEX] flag is set then the database connection opens
4227** in the serialized [threading mode] unless single-thread was
4228** previously selected at compile-time or start-time.
4229** ^The [SQLITE_OPEN_SHAREDCACHE] flag causes the database connection to be
4230** eligible to use [shared cache mode], regardless of whether or not shared
4231** cache is enabled using [sqlite3_enable_shared_cache()]. ^The
4232** [SQLITE_OPEN_PRIVATECACHE] flag causes the database connection to not
4233** participate in [shared cache mode] even if it is enabled.
4234**
4235** ^The fourth parameter to sqlite3_open_v2() is the name of the
4236** [sqlite3_vfs] object that defines the operating system interface that
4237** the new database connection should use. ^If the fourth parameter is
4238** a NULL pointer then the default [sqlite3_vfs] object is used.
4239**
4240** ^If the filename is ":memory:", then a private, temporary in-memory database
4241** is created for the connection. ^This in-memory database will vanish when
4242** the database connection is closed. Future versions of SQLite might
4243** make use of additional special filenames that begin with the ":" character.
4244** It is recommended that when a database filename actually does begin with
4245** a ":" character you should prefix the filename with a pathname such as
4246** "./" to avoid ambiguity.
4247**
4248** ^If the filename is an empty string, then a private, temporary
4249** on-disk database will be created. ^This private database will be
4250** automatically deleted as soon as the database connection is closed.
4251**
4252** [[URI filenames in sqlite3_open()]] <h3>URI Filenames</h3>
4253**
4254** ^If [URI filename] interpretation is enabled, and the filename argument
4255** begins with "file:", then the filename is interpreted as a URI. ^URI
4256** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is
4257** set in the third argument to sqlite3_open_v2(), or if it has
4258** been enabled globally using the [SQLITE_CONFIG_URI] option with the
4259** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
4260** URI filename interpretation is turned off
4261** by default, but future releases of SQLite might enable URI filename
4262** interpretation by default. See "[URI filenames]" for additional
4263** information.
4264**
4265** URI filenames are parsed according to RFC 3986. ^If the URI contains an
4266** authority, then it must be either an empty string or the string
4267** "localhost". ^If the authority is not an empty string or "localhost", an
4268** error is returned to the caller. ^The fragment component of a URI, if
4269** present, is ignored.
4270**
4271** ^SQLite uses the path component of the URI as the name of the disk file
4272** which contains the database. ^If the path begins with a '/' character,
4273** then it is interpreted as an absolute path. ^If the path does not begin
4274** with a '/' (meaning that the authority section is omitted from the URI)
4275** then the path is interpreted as a relative path.
4276** ^(On windows, the first component of an absolute path
4277** is a drive specification (e.g. "C:").)^
4278**
4279** [[core URI query parameters]]
4280** The query component of a URI may contain parameters that are interpreted
4281** either by SQLite itself, or by a [VFS | custom VFS implementation].
4282** SQLite and its built-in [VFSes] interpret the
4283** following query parameters:
4284**
4285** <ul>
4286** <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
4287** a VFS object that provides the operating system interface that should
4288** be used to access the database file on disk. ^If this option is set to
4289** an empty string the default VFS object is used. ^Specifying an unknown
4290** VFS is an error. ^If sqlite3_open_v2() is used and the vfs option is
4291** present, then the VFS specified by the option takes precedence over
4292** the value passed as the fourth parameter to sqlite3_open_v2().
4293**
4294** <li> <b>mode</b>: ^(The mode parameter may be set to either "ro", "rw",
4295** "rwc", or "memory". Attempting to set it to any other value is
4296** an error)^.
4297** ^If "ro" is specified, then the database is opened for read-only
4298** access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the
4299** third argument to sqlite3_open_v2(). ^If the mode option is set to
4300** "rw", then the database is opened for read-write (but not create)
4301** access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had
4302** been set. ^Value "rwc" is equivalent to setting both
4303** SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE. ^If the mode option is
4304** set to "memory" then a pure [in-memory database] that never reads
4305** or writes from disk is used. ^It is an error to specify a value for
4306** the mode parameter that is less restrictive than that specified by
4307** the flags passed in the third parameter to sqlite3_open_v2().
4308**
4309** <li> <b>cache</b>: ^The cache parameter may be set to either "shared" or
4310** "private". ^Setting it to "shared" is equivalent to setting the
4311** SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to
4312** sqlite3_open_v2(). ^Setting the cache parameter to "private" is
4313** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
4314** ^If sqlite3_open_v2() is used and the "cache" parameter is present in
4315** a URI filename, its value overrides any behavior requested by setting
4316** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
4317**
4318** <li> <b>psow</b>: ^The psow parameter indicates whether or not the
4319** [powersafe overwrite] property does or does not apply to the
4320** storage media on which the database file resides.
4321**
4322** <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
4323** which if set disables file locking in rollback journal modes. This
4324** is useful for accessing a database on a filesystem that does not
4325** support locking. Caution: Database corruption might result if two
4326** or more processes write to the same database and any one of those
4327** processes uses nolock=1.
4328**
4329** <li> <b>immutable</b>: ^The immutable parameter is a boolean query
4330** parameter that indicates that the database file is stored on
4331** read-only media. ^When immutable is set, SQLite assumes that the
4332** database file cannot be changed, even by a process with higher
4333** privilege, and so the database is opened read-only and all locking
4334** and change detection is disabled. Caution: Setting the immutable
4335** property on a database file that does in fact change can result
4336** in incorrect query results and/or [SQLITE_CORRUPT] errors.
4337** See also: [SQLITE_IOCAP_IMMUTABLE].
4338**
4339** </ul>
4340**
4341** ^Specifying an unknown parameter in the query component of a URI is not an
4342** error. Future versions of SQLite might understand additional query
4343** parameters. See "[query parameters with special meaning to SQLite]" for
4344** additional information.
4345**
4346** [[URI filename examples]] <h3>URI filename examples</h3>
4347**
4348** <table border="1" align=center cellpadding=5>
4349** <tr><th> URI filenames <th> Results
4350** <tr><td> file:data.db <td>
4351** Open the file "data.db" in the current directory.
4352** <tr><td> file:/home/fred/data.db<br>
4353** file:///home/fred/data.db <br>
4354** file://localhost/home/fred/data.db <br> <td>
4355** Open the database file "/home/fred/data.db".
4356** <tr><td> file://darkstar/home/fred/data.db <td>
4357** An error. "darkstar" is not a recognized authority.
4358** <tr><td style="white-space:nowrap">
4359** file:///C:/Documents%20and%20Settings/fred/Desktop/data.db
4360** <td> Windows only: Open the file "data.db" on fred's desktop on drive
4361** C:. Note that the %20 escaping in this example is not strictly
4362** necessary - space characters can be used literally
4363** in URI filenames.
4364** <tr><td> file:data.db?mode=ro&cache=private <td>
4365** Open file "data.db" in the current directory for read-only access.
4366** Regardless of whether or not shared-cache mode is enabled by
4367** default, use a private cache.
4368** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
4369** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
4370** that uses dot-files in place of posix advisory locking.
4371** <tr><td> file:data.db?mode=readonly <td>
4372** An error. "readonly" is not a valid option for the "mode" parameter.
4373** </table>
4374**
4375** ^URI hexadecimal escape sequences (%HH) are supported within the path and
4376** query components of a URI. A hexadecimal escape sequence consists of a
4377** percent sign - "%" - followed by exactly two hexadecimal digits
4378** specifying an octet value. ^Before the path or query components of a
4379** URI filename are interpreted, they are encoded using UTF-8 and all
4380** hexadecimal escape sequences replaced by a single byte containing the
4381** corresponding octet. If this process generates an invalid UTF-8 encoding,
4382** the results are undefined.
4383**
4384** <b>Note to Windows users:</b> The encoding used for the filename argument
4385** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
4386** codepage is currently defined. Filenames containing international
4387** characters must be converted to UTF-8 prior to passing them into
4388** sqlite3_open() or sqlite3_open_v2().
4389**
4390** <b>Note to Windows Runtime users:</b> The temporary directory must be set
4391** prior to calling sqlite3_open() or sqlite3_open_v2(). Otherwise, various
4392** features that require the use of temporary files may fail.
4393**
4394** See also: [sqlite3_temp_directory]
4395*/
4396SQLITE_API int sqlite3_open(
4397 const char *filename, /* Database filename (UTF-8) */
4398 sqlite3 **ppDb /* OUT: SQLite db handle */
4399);
4400SQLITE_API int sqlite3_open16(
4401 const void *filename, /* Database filename (UTF-16) */
4402 sqlite3 **ppDb /* OUT: SQLite db handle */
4403);
4404SQLITE_API int sqlite3_open_v2(
4405 const char *filename, /* Database filename (UTF-8) */
4406 sqlite3 **ppDb, /* OUT: SQLite db handle */
4407 int flags, /* Flags */
4408 const char *zVfs /* Name of VFS module to use */
4409);
4410
4411/*
4412** CAPI3REF: Obtain Values For URI Parameters
4413**
4414** These are utility routines, useful to VFS implementations, that check
4415** to see if a database file was a URI that contained a specific query
4416** parameter, and if so obtains the value of that query parameter.
4417**
4418** If F is the database filename pointer passed into the xOpen() method of
4419** a VFS implementation when the flags parameter to xOpen() has one or
4420** more of the [SQLITE_OPEN_URI] or [SQLITE_OPEN_MAIN_DB] bits set and
4421** P is the name of the query parameter, then
4422** sqlite3_uri_parameter(F,P) returns the value of the P
4423** parameter if it exists or a NULL pointer if P does not appear as a
4424** query parameter on F. If P is a query parameter of F
4425** has no explicit value, then sqlite3_uri_parameter(F,P) returns
4426** a pointer to an empty string.
4427**
4428** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean
4429** parameter and returns true (1) or false (0) according to the value
4430** of P. The sqlite3_uri_boolean(F,P,B) routine returns true (1) if the
4431** value of query parameter P is one of "yes", "true", or "on" in any
4432** case or if the value begins with a non-zero number. The
4433** sqlite3_uri_boolean(F,P,B) routines returns false (0) if the value of
4434** query parameter P is one of "no", "false", or "off" in any case or
4435** if the value begins with a numeric zero. If P is not a query
4436** parameter on F or if the value of P is does not match any of the
4437** above, then sqlite3_uri_boolean(F,P,B) returns (B!=0).
4438**
4439** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a
4440** 64-bit signed integer and returns that integer, or D if P does not
4441** exist. If the value of P is something other than an integer, then
4442** zero is returned.
4443**
4444** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and
4445** sqlite3_uri_boolean(F,P,B) returns B. If F is not a NULL pointer and
4446** is not a database file pathname pointer that SQLite passed into the xOpen
4447** VFS method, then the behavior of this routine is undefined and probably
4448** undesirable.
4449*/
4450SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
4451SQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
4452SQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
4453
4454
4455/*
4456** CAPI3REF: Error Codes And Messages
4457** METHOD: sqlite3
4458**
4459** ^If the most recent sqlite3_* API call associated with
4460** [database connection] D failed, then the sqlite3_errcode(D) interface
4461** returns the numeric [result code] or [extended result code] for that
4462** API call.
4463** ^The sqlite3_extended_errcode()
4464** interface is the same except that it always returns the
4465** [extended result code] even when extended result codes are
4466** disabled.
4467**
4468** The values returned by sqlite3_errcode() and/or
4469** sqlite3_extended_errcode() might change with each API call.
4470** Except, there are some interfaces that are guaranteed to never
4471** change the value of the error code. The error-code preserving
4472** interfaces are:
4473**
4474** <ul>
4475** <li> sqlite3_errcode()
4476** <li> sqlite3_extended_errcode()
4477** <li> sqlite3_errmsg()
4478** <li> sqlite3_errmsg16()
4479** </ul>
4480**
4481** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
4482** text that describes the error, as either UTF-8 or UTF-16 respectively.
4483** ^(Memory to hold the error message string is managed internally.
4484** The application does not need to worry about freeing the result.
4485** However, the error string might be overwritten or deallocated by
4486** subsequent calls to other SQLite interface functions.)^
4487**
4488** ^The sqlite3_errstr() interface returns the English-language text
4489** that describes the [result code], as UTF-8.
4490** ^(Memory to hold the error message string is managed internally
4491** and must not be freed by the application)^.
4492**
4493** When the serialized [threading mode] is in use, it might be the
4494** case that a second error occurs on a separate thread in between
4495** the time of the first error and the call to these interfaces.
4496** When that happens, the second error will be reported since these
4497** interfaces always report the most recent result. To avoid
4498** this, each thread can obtain exclusive use of the [database connection] D
4499** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning
4500** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after
4501** all calls to the interfaces listed here are completed.
4502**
4503** If an interface fails with SQLITE_MISUSE, that means the interface
4504** was invoked incorrectly by the application. In that case, the
4505** error code and message may or may not be set.
4506*/
4507SQLITE_API int sqlite3_errcode(sqlite3 *db);
4508SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
4509SQLITE_API const char *sqlite3_errmsg(sqlite3*);
4510SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
4511SQLITE_API const char *sqlite3_errstr(int);
4512
4513/*
4514** CAPI3REF: Prepared Statement Object
4515** KEYWORDS: {prepared statement} {prepared statements}
4516**
4517** An instance of this object represents a single SQL statement that
4518** has been compiled into binary form and is ready to be evaluated.
4519**
4520** Think of each SQL statement as a separate computer program. The
4521** original SQL text is source code. A prepared statement object
4522** is the compiled object code. All SQL must be converted into a
4523** prepared statement before it can be run.
4524**
4525** The life-cycle of a prepared statement object usually goes like this:
4526**
4527** <ol>
4528** <li> Create the prepared statement object using [sqlite3_prepare_v2()].
4529** <li> Bind values to [parameters] using the sqlite3_bind_*()
4530** interfaces.
4531** <li> Run the SQL by calling [sqlite3_step()] one or more times.
4532** <li> Reset the prepared statement using [sqlite3_reset()] then go back
4533** to step 2. Do this zero or more times.
4534** <li> Destroy the object using [sqlite3_finalize()].
4535** </ol>
4536*/
4537typedef struct sqlite3_stmt sqlite3_stmt;
4538
4539/*
4540** CAPI3REF: Run-time Limits
4541** METHOD: sqlite3
4542**
4543** ^(This interface allows the size of various constructs to be limited
4544** on a connection by connection basis. The first parameter is the
4545** [database connection] whose limit is to be set or queried. The
4546** second parameter is one of the [limit categories] that define a
4547** class of constructs to be size limited. The third parameter is the
4548** new limit for that construct.)^
4549**
4550** ^If the new limit is a negative number, the limit is unchanged.
4551** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a
4552** [limits | hard upper bound]
4553** set at compile-time by a C preprocessor macro called
4554** [limits | SQLITE_MAX_<i>NAME</i>].
4555** (The "_LIMIT_" in the name is changed to "_MAX_".))^
4556** ^Attempts to increase a limit above its hard upper bound are
4557** silently truncated to the hard upper bound.
4558**
4559** ^Regardless of whether or not the limit was changed, the
4560** [sqlite3_limit()] interface returns the prior value of the limit.
4561** ^Hence, to find the current value of a limit without changing it,
4562** simply invoke this interface with the third parameter set to -1.
4563**
4564** Run-time limits are intended for use in applications that manage
4565** both their own internal database and also databases that are controlled
4566** by untrusted external sources. An example application might be a
4567** web browser that has its own databases for storing history and
4568** separate databases controlled by JavaScript applications downloaded
4569** off the Internet. The internal databases can be given the
4570** large, default limits. Databases managed by external sources can
4571** be given much smaller limits designed to prevent a denial of service
4572** attack. Developers might also want to use the [sqlite3_set_authorizer()]
4573** interface to further control untrusted SQL. The size of the database
4574** created by an untrusted script can be contained using the
4575** [max_page_count] [PRAGMA].
4576**
4577** New run-time limit categories may be added in future releases.
4578*/
4579SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
4580
4581/*
4582** CAPI3REF: Run-Time Limit Categories
4583** KEYWORDS: {limit category} {*limit categories}
4584**
4585** These constants define various performance limits
4586** that can be lowered at run-time using [sqlite3_limit()].
4587** The synopsis of the meanings of the various limits is shown below.
4588** Additional information is available at [limits | Limits in SQLite].
4589**
4590** <dl>
4591** [[SQLITE_LIMIT_LENGTH]] ^(<dt>SQLITE_LIMIT_LENGTH</dt>
4592** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^
4593**
4594** [[SQLITE_LIMIT_SQL_LENGTH]] ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt>
4595** <dd>The maximum length of an SQL statement, in bytes.</dd>)^
4596**
4597** [[SQLITE_LIMIT_COLUMN]] ^(<dt>SQLITE_LIMIT_COLUMN</dt>
4598** <dd>The maximum number of columns in a table definition or in the
4599** result set of a [SELECT] or the maximum number of columns in an index
4600** or in an ORDER BY or GROUP BY clause.</dd>)^
4601**
4602** [[SQLITE_LIMIT_EXPR_DEPTH]] ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt>
4603** <dd>The maximum depth of the parse tree on any expression.</dd>)^
4604**
4605** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
4606** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
4607**
4608** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
4609** <dd>The maximum number of instructions in a virtual machine program
4610** used to implement an SQL statement. If [sqlite3_prepare_v2()] or
4611** the equivalent tries to allocate space for more than this many opcodes
4612** in a single prepared statement, an SQLITE_NOMEM error is returned.</dd>)^
4613**
4614** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
4615** <dd>The maximum number of arguments on a function.</dd>)^
4616**
4617** [[SQLITE_LIMIT_ATTACHED]] ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
4618** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
4619**
4620** [[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]]
4621** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt>
4622** <dd>The maximum length of the pattern argument to the [LIKE] or
4623** [GLOB] operators.</dd>)^
4624**
4625** [[SQLITE_LIMIT_VARIABLE_NUMBER]]
4626** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt>
4627** <dd>The maximum index number of any [parameter] in an SQL statement.)^
4628**
4629** [[SQLITE_LIMIT_TRIGGER_DEPTH]] ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt>
4630** <dd>The maximum depth of recursion for triggers.</dd>)^
4631**
4632** [[SQLITE_LIMIT_WORKER_THREADS]] ^(<dt>SQLITE_LIMIT_WORKER_THREADS</dt>
4633** <dd>The maximum number of auxiliary worker threads that a single
4634** [prepared statement] may start.</dd>)^
4635** </dl>
4636*/
4637#define SQLITE_LIMIT_LENGTH 0
4638#define SQLITE_LIMIT_SQL_LENGTH 1
4639#define SQLITE_LIMIT_COLUMN 2
4640#define SQLITE_LIMIT_EXPR_DEPTH 3
4641#define SQLITE_LIMIT_COMPOUND_SELECT 4
4642#define SQLITE_LIMIT_VDBE_OP 5
4643#define SQLITE_LIMIT_FUNCTION_ARG 6
4644#define SQLITE_LIMIT_ATTACHED 7
4645#define SQLITE_LIMIT_LIKE_PATTERN_LENGTH 8
4646#define SQLITE_LIMIT_VARIABLE_NUMBER 9
4647#define SQLITE_LIMIT_TRIGGER_DEPTH 10
4648#define SQLITE_LIMIT_WORKER_THREADS 11
4649
4650/*
4651** CAPI3REF: Prepare Flags
4652**
4653** These constants define various flags that can be passed into
4654** "prepFlags" parameter of the [sqlite3_prepare_v3()] and
4655** [sqlite3_prepare16_v3()] interfaces.
4656**
4657** New flags may be added in future releases of SQLite.
4658**
4659** <dl>
4660** [[SQLITE_PREPARE_PERSISTENT]] ^(<dt>SQLITE_PREPARE_PERSISTENT</dt>
4661** <dd>The SQLITE_PREPARE_PERSISTENT flag is a hint to the query planner
4662** that the prepared statement will be retained for a long time and
4663** probably reused many times.)^ ^Without this flag, [sqlite3_prepare_v3()]
4664** and [sqlite3_prepare16_v3()] assume that the prepared statement will
4665** be used just once or at most a few times and then destroyed using
4666** [sqlite3_finalize()] relatively soon. The current implementation acts
4667** on this hint by avoiding the use of [lookaside memory] so as not to
4668** deplete the limited store of lookaside memory. Future versions of
4669** SQLite may act on this hint differently.
4670**
4671** [[SQLITE_PREPARE_NORMALIZE]] ^(<dt>SQLITE_PREPARE_NORMALIZE</dt>
4672** <dd>The SQLITE_PREPARE_NORMALIZE flag indicates that a normalized
4673** representation of the SQL statement should be calculated and then
4674** associated with the prepared statement, which can be obtained via
4675** the [sqlite3_normalized_sql()] interface.)^ The semantics used to
4676** normalize a SQL statement are unspecified and subject to change.
4677** At a minimum, literal values will be replaced with suitable
4678** placeholders.
4679** </dl>
4680*/
4681#define SQLITE_PREPARE_PERSISTENT 0x01
4682#define SQLITE_PREPARE_NORMALIZE 0x02
4683
4684/*
4685** CAPI3REF: Compiling An SQL Statement
4686** KEYWORDS: {SQL statement compiler}
4687** METHOD: sqlite3
4688** CONSTRUCTOR: sqlite3_stmt
4689**
4690** To execute an SQL statement, it must first be compiled into a byte-code
4691** program using one of these routines. Or, in other words, these routines
4692** are constructors for the [prepared statement] object.
4693**
4694** The preferred routine to use is [sqlite3_prepare_v2()]. The
4695** [sqlite3_prepare()] interface is legacy and should be avoided.
4696** [sqlite3_prepare_v3()] has an extra "prepFlags" option that is used
4697** for special purposes.
4698**
4699** The use of the UTF-8 interfaces is preferred, as SQLite currently
4700** does all parsing using UTF-8. The UTF-16 interfaces are provided
4701** as a convenience. The UTF-16 interfaces work by converting the
4702** input text into UTF-8, then invoking the corresponding UTF-8 interface.
4703**
4704** The first argument, "db", is a [database connection] obtained from a
4705** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
4706** [sqlite3_open16()]. The database connection must not have been closed.
4707**
4708** The second argument, "zSql", is the statement to be compiled, encoded
4709** as either UTF-8 or UTF-16. The sqlite3_prepare(), sqlite3_prepare_v2(),
4710** and sqlite3_prepare_v3()
4711** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
4712** and sqlite3_prepare16_v3() use UTF-16.
4713**
4714** ^If the nByte argument is negative, then zSql is read up to the
4715** first zero terminator. ^If nByte is positive, then it is the
4716** number of bytes read from zSql. ^If nByte is zero, then no prepared
4717** statement is generated.
4718** If the caller knows that the supplied string is nul-terminated, then
4719** there is a small performance advantage to passing an nByte parameter that
4720** is the number of bytes in the input string <i>including</i>
4721** the nul-terminator.
4722**
4723** ^If pzTail is not NULL then *pzTail is made to point to the first byte
4724** past the end of the first SQL statement in zSql. These routines only
4725** compile the first statement in zSql, so *pzTail is left pointing to
4726** what remains uncompiled.
4727**
4728** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
4729** executed using [sqlite3_step()]. ^If there is an error, *ppStmt is set
4730** to NULL. ^If the input text contains no SQL (if the input is an empty
4731** string or a comment) then *ppStmt is set to NULL.
4732** The calling procedure is responsible for deleting the compiled
4733** SQL statement using [sqlite3_finalize()] after it has finished with it.
4734** ppStmt may not be NULL.
4735**
4736** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
4737** otherwise an [error code] is returned.
4738**
4739** The sqlite3_prepare_v2(), sqlite3_prepare_v3(), sqlite3_prepare16_v2(),
4740** and sqlite3_prepare16_v3() interfaces are recommended for all new programs.
4741** The older interfaces (sqlite3_prepare() and sqlite3_prepare16())
4742** are retained for backwards compatibility, but their use is discouraged.
4743** ^In the "vX" interfaces, the prepared statement
4744** that is returned (the [sqlite3_stmt] object) contains a copy of the
4745** original SQL text. This causes the [sqlite3_step()] interface to
4746** behave differently in three ways:
4747**
4748** <ol>
4749** <li>
4750** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
4751** always used to do, [sqlite3_step()] will automatically recompile the SQL
4752** statement and try to run it again. As many as [SQLITE_MAX_SCHEMA_RETRY]
4753** retries will occur before sqlite3_step() gives up and returns an error.
4754** </li>
4755**
4756** <li>
4757** ^When an error occurs, [sqlite3_step()] will return one of the detailed
4758** [error codes] or [extended error codes]. ^The legacy behavior was that
4759** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
4760** and the application would have to make a second call to [sqlite3_reset()]
4761** in order to find the underlying cause of the problem. With the "v2" prepare
4762** interfaces, the underlying reason for the error is returned immediately.
4763** </li>
4764**
4765** <li>
4766** ^If the specific value bound to [parameter | host parameter] in the
4767** WHERE clause might influence the choice of query plan for a statement,
4768** then the statement will be automatically recompiled, as if there had been
4769** a schema change, on the first [sqlite3_step()] call following any change
4770** to the [sqlite3_bind_text | bindings] of that [parameter].
4771** ^The specific value of WHERE-clause [parameter] might influence the
4772** choice of query plan if the parameter is the left-hand side of a [LIKE]
4773** or [GLOB] operator or if the parameter is compared to an indexed column
4774** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
4775** </li>
4776** </ol>
4777**
4778** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
4779** the extra prepFlags parameter, which is a bit array consisting of zero or
4780** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags. ^The
4781** sqlite3_prepare_v2() interface works exactly the same as
4782** sqlite3_prepare_v3() with a zero prepFlags parameter.
4783*/
4784SQLITE_API int sqlite3_prepare(
4785 sqlite3 *db, /* Database handle */
4786 const char *zSql, /* SQL statement, UTF-8 encoded */
4787 int nByte, /* Maximum length of zSql in bytes. */
4788 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4789 const char **pzTail /* OUT: Pointer to unused portion of zSql */
4790);
4791SQLITE_API int sqlite3_prepare_v2(
4792 sqlite3 *db, /* Database handle */
4793 const char *zSql, /* SQL statement, UTF-8 encoded */
4794 int nByte, /* Maximum length of zSql in bytes. */
4795 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4796 const char **pzTail /* OUT: Pointer to unused portion of zSql */
4797);
4798SQLITE_API int sqlite3_prepare_v3(
4799 sqlite3 *db, /* Database handle */
4800 const char *zSql, /* SQL statement, UTF-8 encoded */
4801 int nByte, /* Maximum length of zSql in bytes. */
4802 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
4803 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4804 const char **pzTail /* OUT: Pointer to unused portion of zSql */
4805);
4806SQLITE_API int sqlite3_prepare16(
4807 sqlite3 *db, /* Database handle */
4808 const void *zSql, /* SQL statement, UTF-16 encoded */
4809 int nByte, /* Maximum length of zSql in bytes. */
4810 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4811 const void **pzTail /* OUT: Pointer to unused portion of zSql */
4812);
4813SQLITE_API int sqlite3_prepare16_v2(
4814 sqlite3 *db, /* Database handle */
4815 const void *zSql, /* SQL statement, UTF-16 encoded */
4816 int nByte, /* Maximum length of zSql in bytes. */
4817 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4818 const void **pzTail /* OUT: Pointer to unused portion of zSql */
4819);
4820SQLITE_API int sqlite3_prepare16_v3(
4821 sqlite3 *db, /* Database handle */
4822 const void *zSql, /* SQL statement, UTF-16 encoded */
4823 int nByte, /* Maximum length of zSql in bytes. */
4824 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
4825 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4826 const void **pzTail /* OUT: Pointer to unused portion of zSql */
4827);
4828
4829/*
4830** CAPI3REF: Retrieving Statement SQL
4831** METHOD: sqlite3_stmt
4832**
4833** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
4834** SQL text used to create [prepared statement] P if P was
4835** created by [sqlite3_prepare_v2()], [sqlite3_prepare_v3()],
4836** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
4837** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
4838** string containing the SQL text of prepared statement P with
4839** [bound parameters] expanded.
4840** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8
4841** string containing the normalized SQL text of prepared statement P. The
4842** semantics used to normalize a SQL statement are unspecified and subject
4843** to change. At a minimum, literal values will be replaced with suitable
4844** placeholders.
4845**
4846** ^(For example, if a prepared statement is created using the SQL
4847** text "SELECT $abc,:xyz" and if parameter $abc is bound to integer 2345
4848** and parameter :xyz is unbound, then sqlite3_sql() will return
4849** the original string, "SELECT $abc,:xyz" but sqlite3_expanded_sql()
4850** will return "SELECT 2345,NULL".)^
4851**
4852** ^The sqlite3_expanded_sql() interface returns NULL if insufficient memory
4853** is available to hold the result, or if the result would exceed the
4854** the maximum string length determined by the [SQLITE_LIMIT_LENGTH].
4855**
4856** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
4857** bound parameter expansions. ^The [SQLITE_OMIT_TRACE] compile-time
4858** option causes sqlite3_expanded_sql() to always return NULL.
4859**
4860** ^The strings returned by sqlite3_sql(P) and sqlite3_normalized_sql(P)
4861** are managed by SQLite and are automatically freed when the prepared
4862** statement is finalized.
4863** ^The string returned by sqlite3_expanded_sql(P), on the other hand,
4864** is obtained from [sqlite3_malloc()] and must be free by the application
4865** by passing it to [sqlite3_free()].
4866*/
4867SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
4868SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt);
4869SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt);
4870
4871/*
4872** CAPI3REF: Determine If An SQL Statement Writes The Database
4873** METHOD: sqlite3_stmt
4874**
4875** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
4876** and only if the [prepared statement] X makes no direct changes to
4877** the content of the database file.
4878**
4879** Note that [application-defined SQL functions] or
4880** [virtual tables] might change the database indirectly as a side effect.
4881** ^(For example, if an application defines a function "eval()" that
4882** calls [sqlite3_exec()], then the following SQL statement would
4883** change the database file through side-effects:
4884**
4885** <blockquote><pre>
4886** SELECT eval('DELETE FROM t1') FROM t2;
4887** </pre></blockquote>
4888**
4889** But because the [SELECT] statement does not change the database file
4890** directly, sqlite3_stmt_readonly() would still return true.)^
4891**
4892** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK],
4893** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true,
4894** since the statements themselves do not actually modify the database but
4895** rather they control the timing of when other statements modify the
4896** database. ^The [ATTACH] and [DETACH] statements also cause
4897** sqlite3_stmt_readonly() to return true since, while those statements
4898** change the configuration of a database connection, they do not make
4899** changes to the content of the database files on disk.
4900** ^The sqlite3_stmt_readonly() interface returns true for [BEGIN] since
4901** [BEGIN] merely sets internal flags, but the [BEGIN|BEGIN IMMEDIATE] and
4902** [BEGIN|BEGIN EXCLUSIVE] commands do touch the database and so
4903** sqlite3_stmt_readonly() returns false for those commands.
4904*/
4905SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
4906
4907/*
4908** CAPI3REF: Determine If A Prepared Statement Has Been Reset
4909** METHOD: sqlite3_stmt
4910**
4911** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
4912** [prepared statement] S has been stepped at least once using
4913** [sqlite3_step(S)] but has neither run to completion (returned
4914** [SQLITE_DONE] from [sqlite3_step(S)]) nor
4915** been reset using [sqlite3_reset(S)]. ^The sqlite3_stmt_busy(S)
4916** interface returns false if S is a NULL pointer. If S is not a
4917** NULL pointer and is not a pointer to a valid [prepared statement]
4918** object, then the behavior is undefined and probably undesirable.
4919**
4920** This interface can be used in combination [sqlite3_next_stmt()]
4921** to locate all prepared statements associated with a database
4922** connection that are in need of being reset. This can be used,
4923** for example, in diagnostic routines to search for prepared
4924** statements that are holding a transaction open.
4925*/
4926SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*);
4927
4928/*
4929** CAPI3REF: Dynamically Typed Value Object
4930** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
4931**
4932** SQLite uses the sqlite3_value object to represent all values
4933** that can be stored in a database table. SQLite uses dynamic typing
4934** for the values it stores. ^Values stored in sqlite3_value objects
4935** can be integers, floating point values, strings, BLOBs, or NULL.
4936**
4937** An sqlite3_value object may be either "protected" or "unprotected".
4938** Some interfaces require a protected sqlite3_value. Other interfaces
4939** will accept either a protected or an unprotected sqlite3_value.
4940** Every interface that accepts sqlite3_value arguments specifies
4941** whether or not it requires a protected sqlite3_value. The
4942** [sqlite3_value_dup()] interface can be used to construct a new
4943** protected sqlite3_value from an unprotected sqlite3_value.
4944**
4945** The terms "protected" and "unprotected" refer to whether or not
4946** a mutex is held. An internal mutex is held for a protected
4947** sqlite3_value object but no mutex is held for an unprotected
4948** sqlite3_value object. If SQLite is compiled to be single-threaded
4949** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
4950** or if SQLite is run in one of reduced mutex modes
4951** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
4952** then there is no distinction between protected and unprotected
4953** sqlite3_value objects and they can be used interchangeably. However,
4954** for maximum code portability it is recommended that applications
4955** still make the distinction between protected and unprotected
4956** sqlite3_value objects even when not strictly required.
4957**
4958** ^The sqlite3_value objects that are passed as parameters into the
4959** implementation of [application-defined SQL functions] are protected.
4960** ^The sqlite3_value object returned by
4961** [sqlite3_column_value()] is unprotected.
4962** Unprotected sqlite3_value objects may only be used as arguments
4963** to [sqlite3_result_value()], [sqlite3_bind_value()], and
4964** [sqlite3_value_dup()].
4965** The [sqlite3_value_blob | sqlite3_value_type()] family of
4966** interfaces require protected sqlite3_value objects.
4967*/
4968typedef struct sqlite3_value sqlite3_value;
4969
4970/*
4971** CAPI3REF: SQL Function Context Object
4972**
4973** The context in which an SQL function executes is stored in an
4974** sqlite3_context object. ^A pointer to an sqlite3_context object
4975** is always first parameter to [application-defined SQL functions].
4976** The application-defined SQL function implementation will pass this
4977** pointer through into calls to [sqlite3_result_int | sqlite3_result()],
4978** [sqlite3_aggregate_context()], [sqlite3_user_data()],
4979** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()],
4980** and/or [sqlite3_set_auxdata()].
4981*/
4982typedef struct sqlite3_context sqlite3_context;
4983
4984/*
4985** CAPI3REF: Binding Values To Prepared Statements
4986** KEYWORDS: {host parameter} {host parameters} {host parameter name}
4987** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
4988** METHOD: sqlite3_stmt
4989**
4990** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
4991** literals may be replaced by a [parameter] that matches one of following
4992** templates:
4993**
4994** <ul>
4995** <li> ?
4996** <li> ?NNN
4997** <li> :VVV
4998** <li> @VVV
4999** <li> $VVV
5000** </ul>
5001**
5002** In the templates above, NNN represents an integer literal,
5003** and VVV represents an alphanumeric identifier.)^ ^The values of these
5004** parameters (also called "host parameter names" or "SQL parameters")
5005** can be set using the sqlite3_bind_*() routines defined here.
5006**
5007** ^The first argument to the sqlite3_bind_*() routines is always
5008** a pointer to the [sqlite3_stmt] object returned from
5009** [sqlite3_prepare_v2()] or its variants.
5010**
5011** ^The second argument is the index of the SQL parameter to be set.
5012** ^The leftmost SQL parameter has an index of 1. ^When the same named
5013** SQL parameter is used more than once, second and subsequent
5014** occurrences have the same index as the first occurrence.
5015** ^The index for named parameters can be looked up using the
5016** [sqlite3_bind_parameter_index()] API if desired. ^The index
5017** for "?NNN" parameters is the value of NNN.
5018** ^The NNN value must be between 1 and the [sqlite3_limit()]
5019** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 999).
5020**
5021** ^The third argument is the value to bind to the parameter.
5022** ^If the third parameter to sqlite3_bind_text() or sqlite3_bind_text16()
5023** or sqlite3_bind_blob() is a NULL pointer then the fourth parameter
5024** is ignored and the end result is the same as sqlite3_bind_null().
5025**
5026** ^(In those routines that have a fourth argument, its value is the
5027** number of bytes in the parameter. To be clear: the value is the
5028** number of <u>bytes</u> in the value, not the number of characters.)^
5029** ^If the fourth parameter to sqlite3_bind_text() or sqlite3_bind_text16()
5030** is negative, then the length of the string is
5031** the number of bytes up to the first zero terminator.
5032** If the fourth parameter to sqlite3_bind_blob() is negative, then
5033** the behavior is undefined.
5034** If a non-negative fourth parameter is provided to sqlite3_bind_text()
5035** or sqlite3_bind_text16() or sqlite3_bind_text64() then
5036** that parameter must be the byte offset
5037** where the NUL terminator would occur assuming the string were NUL
5038** terminated. If any NUL characters occur at byte offsets less than
5039** the value of the fourth parameter then the resulting string value will
5040** contain embedded NULs. The result of expressions involving strings
5041** with embedded NULs is undefined.
5042**
5043** ^The fifth argument to the BLOB and string binding interfaces
5044** is a destructor used to dispose of the BLOB or
5045** string after SQLite has finished with it. ^The destructor is called
5046** to dispose of the BLOB or string even if the call to bind API fails.
5047** ^If the fifth argument is
5048** the special value [SQLITE_STATIC], then SQLite assumes that the
5049** information is in static, unmanaged space and does not need to be freed.
5050** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
5051** SQLite makes its own private copy of the data immediately, before
5052** the sqlite3_bind_*() routine returns.
5053**
5054** ^The sixth argument to sqlite3_bind_text64() must be one of
5055** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]
5056** to specify the encoding of the text in the third parameter. If
5057** the sixth argument to sqlite3_bind_text64() is not one of the
5058** allowed values shown above, or if the text encoding is different
5059** from the encoding specified by the sixth parameter, then the behavior
5060** is undefined.
5061**
5062** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
5063** is filled with zeroes. ^A zeroblob uses a fixed amount of memory
5064** (just an integer to hold its size) while it is being processed.
5065** Zeroblobs are intended to serve as placeholders for BLOBs whose
5066** content is later written using
5067** [sqlite3_blob_open | incremental BLOB I/O] routines.
5068** ^A negative value for the zeroblob results in a zero-length BLOB.
5069**
5070** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in
5071** [prepared statement] S to have an SQL value of NULL, but to also be
5072** associated with the pointer P of type T. ^D is either a NULL pointer or
5073** a pointer to a destructor function for P. ^SQLite will invoke the
5074** destructor D with a single argument of P when it is finished using
5075** P. The T parameter should be a static string, preferably a string
5076** literal. The sqlite3_bind_pointer() routine is part of the
5077** [pointer passing interface] added for SQLite 3.20.0.
5078**
5079** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
5080** for the [prepared statement] or with a prepared statement for which
5081** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
5082** then the call will return [SQLITE_MISUSE]. If any sqlite3_bind_()
5083** routine is passed a [prepared statement] that has been finalized, the
5084** result is undefined and probably harmful.
5085**
5086** ^Bindings are not cleared by the [sqlite3_reset()] routine.
5087** ^Unbound parameters are interpreted as NULL.
5088**
5089** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
5090** [error code] if anything goes wrong.
5091** ^[SQLITE_TOOBIG] might be returned if the size of a string or BLOB
5092** exceeds limits imposed by [sqlite3_limit]([SQLITE_LIMIT_LENGTH]) or
5093** [SQLITE_MAX_LENGTH].
5094** ^[SQLITE_RANGE] is returned if the parameter
5095** index is out of range. ^[SQLITE_NOMEM] is returned if malloc() fails.
5096**
5097** See also: [sqlite3_bind_parameter_count()],
5098** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
5099*/
5100SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
5101SQLITE_API int sqlite3_bind_blob64(sqlite3_stmt*, int, const void*, sqlite3_uint64,
5102 void(*)(void*));
5103SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
5104SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
5105SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
5106SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
5107SQLITE_API int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
5108SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
5109SQLITE_API int sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64,
5110 void(*)(void*), unsigned char encoding);
5111SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
5112SQLITE_API int sqlite3_bind_pointer(sqlite3_stmt*, int, void*, const char*,void(*)(void*));
5113SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
5114SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt*, int, sqlite3_uint64);
5115
5116/*
5117** CAPI3REF: Number Of SQL Parameters
5118** METHOD: sqlite3_stmt
5119**
5120** ^This routine can be used to find the number of [SQL parameters]
5121** in a [prepared statement]. SQL parameters are tokens of the
5122** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
5123** placeholders for values that are [sqlite3_bind_blob | bound]
5124** to the parameters at a later time.
5125**
5126** ^(This routine actually returns the index of the largest (rightmost)
5127** parameter. For all forms except ?NNN, this will correspond to the
5128** number of unique parameters. If parameters of the ?NNN form are used,
5129** there may be gaps in the list.)^
5130**
5131** See also: [sqlite3_bind_blob|sqlite3_bind()],
5132** [sqlite3_bind_parameter_name()], and
5133** [sqlite3_bind_parameter_index()].
5134*/
5135SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
5136
5137/*
5138** CAPI3REF: Name Of A Host Parameter
5139** METHOD: sqlite3_stmt
5140**
5141** ^The sqlite3_bind_parameter_name(P,N) interface returns
5142** the name of the N-th [SQL parameter] in the [prepared statement] P.
5143** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
5144** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
5145** respectively.
5146** In other words, the initial ":" or "$" or "@" or "?"
5147** is included as part of the name.)^
5148** ^Parameters of the form "?" without a following integer have no name
5149** and are referred to as "nameless" or "anonymous parameters".
5150**
5151** ^The first host parameter has an index of 1, not 0.
5152**
5153** ^If the value N is out of range or if the N-th parameter is
5154** nameless, then NULL is returned. ^The returned string is
5155** always in UTF-8 encoding even if the named parameter was
5156** originally specified as UTF-16 in [sqlite3_prepare16()],
5157** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
5158**
5159** See also: [sqlite3_bind_blob|sqlite3_bind()],
5160** [sqlite3_bind_parameter_count()], and
5161** [sqlite3_bind_parameter_index()].
5162*/
5163SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
5164
5165/*
5166** CAPI3REF: Index Of A Parameter With A Given Name
5167** METHOD: sqlite3_stmt
5168**
5169** ^Return the index of an SQL parameter given its name. ^The
5170** index value returned is suitable for use as the second
5171** parameter to [sqlite3_bind_blob|sqlite3_bind()]. ^A zero
5172** is returned if no matching parameter is found. ^The parameter
5173** name must be given in UTF-8 even if the original statement
5174** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or
5175** [sqlite3_prepare16_v3()].
5176**
5177** See also: [sqlite3_bind_blob|sqlite3_bind()],
5178** [sqlite3_bind_parameter_count()], and
5179** [sqlite3_bind_parameter_name()].
5180*/
5181SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
5182
5183/*
5184** CAPI3REF: Reset All Bindings On A Prepared Statement
5185** METHOD: sqlite3_stmt
5186**
5187** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
5188** the [sqlite3_bind_blob | bindings] on a [prepared statement].
5189** ^Use this routine to reset all host parameters to NULL.
5190*/
5191SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
5192
5193/*
5194** CAPI3REF: Number Of Columns In A Result Set
5195** METHOD: sqlite3_stmt
5196**
5197** ^Return the number of columns in the result set returned by the
5198** [prepared statement]. ^If this routine returns 0, that means the
5199** [prepared statement] returns no data (for example an [UPDATE]).
5200** ^However, just because this routine returns a positive number does not
5201** mean that one or more rows of data will be returned. ^A SELECT statement
5202** will always have a positive sqlite3_column_count() but depending on the
5203** WHERE clause constraints and the table content, it might return no rows.
5204**
5205** See also: [sqlite3_data_count()]
5206*/
5207SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
5208
5209/*
5210** CAPI3REF: Column Names In A Result Set
5211** METHOD: sqlite3_stmt
5212**
5213** ^These routines return the name assigned to a particular column
5214** in the result set of a [SELECT] statement. ^The sqlite3_column_name()
5215** interface returns a pointer to a zero-terminated UTF-8 string
5216** and sqlite3_column_name16() returns a pointer to a zero-terminated
5217** UTF-16 string. ^The first parameter is the [prepared statement]
5218** that implements the [SELECT] statement. ^The second parameter is the
5219** column number. ^The leftmost column is number 0.
5220**
5221** ^The returned string pointer is valid until either the [prepared statement]
5222** is destroyed by [sqlite3_finalize()] or until the statement is automatically
5223** reprepared by the first call to [sqlite3_step()] for a particular run
5224** or until the next call to
5225** sqlite3_column_name() or sqlite3_column_name16() on the same column.
5226**
5227** ^If sqlite3_malloc() fails during the processing of either routine
5228** (for example during a conversion from UTF-8 to UTF-16) then a
5229** NULL pointer is returned.
5230**
5231** ^The name of a result column is the value of the "AS" clause for
5232** that column, if there is an AS clause. If there is no AS clause
5233** then the name of the column is unspecified and may change from
5234** one release of SQLite to the next.
5235*/
5236SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
5237SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
5238
5239/*
5240** CAPI3REF: Source Of Data In A Query Result
5241** METHOD: sqlite3_stmt
5242**
5243** ^These routines provide a means to determine the database, table, and
5244** table column that is the origin of a particular result column in
5245** [SELECT] statement.
5246** ^The name of the database or table or column can be returned as
5247** either a UTF-8 or UTF-16 string. ^The _database_ routines return
5248** the database name, the _table_ routines return the table name, and
5249** the origin_ routines return the column name.
5250** ^The returned string is valid until the [prepared statement] is destroyed
5251** using [sqlite3_finalize()] or until the statement is automatically
5252** reprepared by the first call to [sqlite3_step()] for a particular run
5253** or until the same information is requested
5254** again in a different encoding.
5255**
5256** ^The names returned are the original un-aliased names of the
5257** database, table, and column.
5258**
5259** ^The first argument to these interfaces is a [prepared statement].
5260** ^These functions return information about the Nth result column returned by
5261** the statement, where N is the second function argument.
5262** ^The left-most column is column 0 for these routines.
5263**
5264** ^If the Nth column returned by the statement is an expression or
5265** subquery and is not a column value, then all of these functions return
5266** NULL. ^These routine might also return NULL if a memory allocation error
5267** occurs. ^Otherwise, they return the name of the attached database, table,
5268** or column that query result column was extracted from.
5269**
5270** ^As with all other SQLite APIs, those whose names end with "16" return
5271** UTF-16 encoded strings and the other functions return UTF-8.
5272**
5273** ^These APIs are only available if the library was compiled with the
5274** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
5275**
5276** If two or more threads call one or more of these routines against the same
5277** prepared statement and column at the same time then the results are
5278** undefined.
5279**
5280** If two or more threads call one or more
5281** [sqlite3_column_database_name | column metadata interfaces]
5282** for the same [prepared statement] and result column
5283** at the same time then the results are undefined.
5284*/
5285SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
5286SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
5287SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
5288SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
5289SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
5290SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
5291
5292/*
5293** CAPI3REF: Declared Datatype Of A Query Result
5294** METHOD: sqlite3_stmt
5295**
5296** ^(The first parameter is a [prepared statement].
5297** If this statement is a [SELECT] statement and the Nth column of the
5298** returned result set of that [SELECT] is a table column (not an
5299** expression or subquery) then the declared type of the table
5300** column is returned.)^ ^If the Nth column of the result set is an
5301** expression or subquery, then a NULL pointer is returned.
5302** ^The returned string is always UTF-8 encoded.
5303**
5304** ^(For example, given the database schema:
5305**
5306** CREATE TABLE t1(c1 VARIANT);
5307**
5308** and the following statement to be compiled:
5309**
5310** SELECT c1 + 1, c1 FROM t1;
5311**
5312** this routine would return the string "VARIANT" for the second result
5313** column (i==1), and a NULL pointer for the first result column (i==0).)^
5314**
5315** ^SQLite uses dynamic run-time typing. ^So just because a column
5316** is declared to contain a particular type does not mean that the
5317** data stored in that column is of the declared type. SQLite is
5318** strongly typed, but the typing is dynamic not static. ^Type
5319** is associated with individual values, not with the containers
5320** used to hold those values.
5321*/
5322SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
5323SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
5324
5325/*
5326** CAPI3REF: Evaluate An SQL Statement
5327** METHOD: sqlite3_stmt
5328**
5329** After a [prepared statement] has been prepared using any of
5330** [sqlite3_prepare_v2()], [sqlite3_prepare_v3()], [sqlite3_prepare16_v2()],
5331** or [sqlite3_prepare16_v3()] or one of the legacy
5332** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
5333** must be called one or more times to evaluate the statement.
5334**
5335** The details of the behavior of the sqlite3_step() interface depend
5336** on whether the statement was prepared using the newer "vX" interfaces
5337** [sqlite3_prepare_v3()], [sqlite3_prepare_v2()], [sqlite3_prepare16_v3()],
5338** [sqlite3_prepare16_v2()] or the older legacy
5339** interfaces [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the
5340** new "vX" interface is recommended for new applications but the legacy
5341** interface will continue to be supported.
5342**
5343** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
5344** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
5345** ^With the "v2" interface, any of the other [result codes] or
5346** [extended result codes] might be returned as well.
5347**
5348** ^[SQLITE_BUSY] means that the database engine was unable to acquire the
5349** database locks it needs to do its job. ^If the statement is a [COMMIT]
5350** or occurs outside of an explicit transaction, then you can retry the
5351** statement. If the statement is not a [COMMIT] and occurs within an
5352** explicit transaction then you should rollback the transaction before
5353** continuing.
5354**
5355** ^[SQLITE_DONE] means that the statement has finished executing
5356** successfully. sqlite3_step() should not be called again on this virtual
5357** machine without first calling [sqlite3_reset()] to reset the virtual
5358** machine back to its initial state.
5359**
5360** ^If the SQL statement being executed returns any data, then [SQLITE_ROW]
5361** is returned each time a new row of data is ready for processing by the
5362** caller. The values may be accessed using the [column access functions].
5363** sqlite3_step() is called again to retrieve the next row of data.
5364**
5365** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
5366** violation) has occurred. sqlite3_step() should not be called again on
5367** the VM. More information may be found by calling [sqlite3_errmsg()].
5368** ^With the legacy interface, a more specific error code (for example,
5369** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
5370** can be obtained by calling [sqlite3_reset()] on the
5371** [prepared statement]. ^In the "v2" interface,
5372** the more specific error code is returned directly by sqlite3_step().
5373**
5374** [SQLITE_MISUSE] means that the this routine was called inappropriately.
5375** Perhaps it was called on a [prepared statement] that has
5376** already been [sqlite3_finalize | finalized] or on one that had
5377** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could
5378** be the case that the same database connection is being used by two or
5379** more threads at the same moment in time.
5380**
5381** For all versions of SQLite up to and including 3.6.23.1, a call to
5382** [sqlite3_reset()] was required after sqlite3_step() returned anything
5383** other than [SQLITE_ROW] before any subsequent invocation of
5384** sqlite3_step(). Failure to reset the prepared statement using
5385** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
5386** sqlite3_step(). But after [version 3.6.23.1] ([dateof:3.6.23.1],
5387** sqlite3_step() began
5388** calling [sqlite3_reset()] automatically in this circumstance rather
5389** than returning [SQLITE_MISUSE]. This is not considered a compatibility
5390** break because any application that ever receives an SQLITE_MISUSE error
5391** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
5392** can be used to restore the legacy behavior.
5393**
5394** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
5395** API always returns a generic error code, [SQLITE_ERROR], following any
5396** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call
5397** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
5398** specific [error codes] that better describes the error.
5399** We admit that this is a goofy design. The problem has been fixed
5400** with the "v2" interface. If you prepare all of your SQL statements
5401** using [sqlite3_prepare_v3()] or [sqlite3_prepare_v2()]
5402** or [sqlite3_prepare16_v2()] or [sqlite3_prepare16_v3()] instead
5403** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
5404** then the more specific [error codes] are returned directly
5405** by sqlite3_step(). The use of the "vX" interfaces is recommended.
5406*/
5407SQLITE_API int sqlite3_step(sqlite3_stmt*);
5408
5409/*
5410** CAPI3REF: Number of columns in a result set
5411** METHOD: sqlite3_stmt
5412**
5413** ^The sqlite3_data_count(P) interface returns the number of columns in the
5414** current row of the result set of [prepared statement] P.
5415** ^If prepared statement P does not have results ready to return
5416** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
5417** interfaces) then sqlite3_data_count(P) returns 0.
5418** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
5419** ^The sqlite3_data_count(P) routine returns 0 if the previous call to
5420** [sqlite3_step](P) returned [SQLITE_DONE]. ^The sqlite3_data_count(P)
5421** will return non-zero if previous call to [sqlite3_step](P) returned
5422** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum]
5423** where it always returns zero since each step of that multi-step
5424** pragma returns 0 columns of data.
5425**
5426** See also: [sqlite3_column_count()]
5427*/
5428SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
5429
5430/*
5431** CAPI3REF: Fundamental Datatypes
5432** KEYWORDS: SQLITE_TEXT
5433**
5434** ^(Every value in SQLite has one of five fundamental datatypes:
5435**
5436** <ul>
5437** <li> 64-bit signed integer
5438** <li> 64-bit IEEE floating point number
5439** <li> string
5440** <li> BLOB
5441** <li> NULL
5442** </ul>)^
5443**
5444** These constants are codes for each of those types.
5445**
5446** Note that the SQLITE_TEXT constant was also used in SQLite version 2
5447** for a completely different meaning. Software that links against both
5448** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not
5449** SQLITE_TEXT.
5450*/
5451#define SQLITE_INTEGER 1
5452#define SQLITE_FLOAT 2
5453#define SQLITE_BLOB 4
5454#define SQLITE_NULL 5
5455#ifdef SQLITE_TEXT
5456# undef SQLITE_TEXT
5457#else
5458# define SQLITE_TEXT 3
5459#endif
5460#define SQLITE3_TEXT 3
5461
5462/*
5463** CAPI3REF: Result Values From A Query
5464** KEYWORDS: {column access functions}
5465** METHOD: sqlite3_stmt
5466**
5467** <b>Summary:</b>
5468** <blockquote><table border=0 cellpadding=0 cellspacing=0>
5469** <tr><td><b>sqlite3_column_blob</b><td>&rarr;<td>BLOB result
5470** <tr><td><b>sqlite3_column_double</b><td>&rarr;<td>REAL result
5471** <tr><td><b>sqlite3_column_int</b><td>&rarr;<td>32-bit INTEGER result
5472** <tr><td><b>sqlite3_column_int64</b><td>&rarr;<td>64-bit INTEGER result
5473** <tr><td><b>sqlite3_column_text</b><td>&rarr;<td>UTF-8 TEXT result
5474** <tr><td><b>sqlite3_column_text16</b><td>&rarr;<td>UTF-16 TEXT result
5475** <tr><td><b>sqlite3_column_value</b><td>&rarr;<td>The result as an
5476** [sqlite3_value|unprotected sqlite3_value] object.
5477** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
5478** <tr><td><b>sqlite3_column_bytes</b><td>&rarr;<td>Size of a BLOB
5479** or a UTF-8 TEXT result in bytes
5480** <tr><td><b>sqlite3_column_bytes16&nbsp;&nbsp;</b>
5481** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
5482** TEXT in bytes
5483** <tr><td><b>sqlite3_column_type</b><td>&rarr;<td>Default
5484** datatype of the result
5485** </table></blockquote>
5486**
5487** <b>Details:</b>
5488**
5489** ^These routines return information about a single column of the current
5490** result row of a query. ^In every case the first argument is a pointer
5491** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
5492** that was returned from [sqlite3_prepare_v2()] or one of its variants)
5493** and the second argument is the index of the column for which information
5494** should be returned. ^The leftmost column of the result set has the index 0.
5495** ^The number of columns in the result can be determined using
5496** [sqlite3_column_count()].
5497**
5498** If the SQL statement does not currently point to a valid row, or if the
5499** column index is out of range, the result is undefined.
5500** These routines may only be called when the most recent call to
5501** [sqlite3_step()] has returned [SQLITE_ROW] and neither
5502** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently.
5503** If any of these routines are called after [sqlite3_reset()] or
5504** [sqlite3_finalize()] or after [sqlite3_step()] has returned
5505** something other than [SQLITE_ROW], the results are undefined.
5506** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
5507** are called from a different thread while any of these routines
5508** are pending, then the results are undefined.
5509**
5510** The first six interfaces (_blob, _double, _int, _int64, _text, and _text16)
5511** each return the value of a result column in a specific data format. If
5512** the result column is not initially in the requested format (for example,
5513** if the query returns an integer but the sqlite3_column_text() interface
5514** is used to extract the value) then an automatic type conversion is performed.
5515**
5516** ^The sqlite3_column_type() routine returns the
5517** [SQLITE_INTEGER | datatype code] for the initial data type
5518** of the result column. ^The returned value is one of [SQLITE_INTEGER],
5519** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].
5520** The return value of sqlite3_column_type() can be used to decide which
5521** of the first six interface should be used to extract the column value.
5522** The value returned by sqlite3_column_type() is only meaningful if no
5523** automatic type conversions have occurred for the value in question.
5524** After a type conversion, the result of calling sqlite3_column_type()
5525** is undefined, though harmless. Future
5526** versions of SQLite may change the behavior of sqlite3_column_type()
5527** following a type conversion.
5528**
5529** If the result is a BLOB or a TEXT string, then the sqlite3_column_bytes()
5530** or sqlite3_column_bytes16() interfaces can be used to determine the size
5531** of that BLOB or string.
5532**
5533** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
5534** routine returns the number of bytes in that BLOB or string.
5535** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
5536** the string to UTF-8 and then returns the number of bytes.
5537** ^If the result is a numeric value then sqlite3_column_bytes() uses
5538** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
5539** the number of bytes in that string.
5540** ^If the result is NULL, then sqlite3_column_bytes() returns zero.
5541**
5542** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
5543** routine returns the number of bytes in that BLOB or string.
5544** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
5545** the string to UTF-16 and then returns the number of bytes.
5546** ^If the result is a numeric value then sqlite3_column_bytes16() uses
5547** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
5548** the number of bytes in that string.
5549** ^If the result is NULL, then sqlite3_column_bytes16() returns zero.
5550**
5551** ^The values returned by [sqlite3_column_bytes()] and
5552** [sqlite3_column_bytes16()] do not include the zero terminators at the end
5553** of the string. ^For clarity: the values returned by
5554** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
5555** bytes in the string, not the number of characters.
5556**
5557** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
5558** even empty strings, are always zero-terminated. ^The return
5559** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
5560**
5561** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an
5562** [unprotected sqlite3_value] object. In a multithreaded environment,
5563** an unprotected sqlite3_value object may only be used safely with
5564** [sqlite3_bind_value()] and [sqlite3_result_value()].
5565** If the [unprotected sqlite3_value] object returned by
5566** [sqlite3_column_value()] is used in any other way, including calls
5567** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
5568** or [sqlite3_value_bytes()], the behavior is not threadsafe.
5569** Hence, the sqlite3_column_value() interface
5570** is normally only useful within the implementation of
5571** [application-defined SQL functions] or [virtual tables], not within
5572** top-level application code.
5573**
5574** The these routines may attempt to convert the datatype of the result.
5575** ^For example, if the internal representation is FLOAT and a text result
5576** is requested, [sqlite3_snprintf()] is used internally to perform the
5577** conversion automatically. ^(The following table details the conversions
5578** that are applied:
5579**
5580** <blockquote>
5581** <table border="1">
5582** <tr><th> Internal<br>Type <th> Requested<br>Type <th> Conversion
5583**
5584** <tr><td> NULL <td> INTEGER <td> Result is 0
5585** <tr><td> NULL <td> FLOAT <td> Result is 0.0
5586** <tr><td> NULL <td> TEXT <td> Result is a NULL pointer
5587** <tr><td> NULL <td> BLOB <td> Result is a NULL pointer
5588** <tr><td> INTEGER <td> FLOAT <td> Convert from integer to float
5589** <tr><td> INTEGER <td> TEXT <td> ASCII rendering of the integer
5590** <tr><td> INTEGER <td> BLOB <td> Same as INTEGER->TEXT
5591** <tr><td> FLOAT <td> INTEGER <td> [CAST] to INTEGER
5592** <tr><td> FLOAT <td> TEXT <td> ASCII rendering of the float
5593** <tr><td> FLOAT <td> BLOB <td> [CAST] to BLOB
5594** <tr><td> TEXT <td> INTEGER <td> [CAST] to INTEGER
5595** <tr><td> TEXT <td> FLOAT <td> [CAST] to REAL
5596** <tr><td> TEXT <td> BLOB <td> No change
5597** <tr><td> BLOB <td> INTEGER <td> [CAST] to INTEGER
5598** <tr><td> BLOB <td> FLOAT <td> [CAST] to REAL
5599** <tr><td> BLOB <td> TEXT <td> Add a zero terminator if needed
5600** </table>
5601** </blockquote>)^
5602**
5603** Note that when type conversions occur, pointers returned by prior
5604** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
5605** sqlite3_column_text16() may be invalidated.
5606** Type conversions and pointer invalidations might occur
5607** in the following cases:
5608**
5609** <ul>
5610** <li> The initial content is a BLOB and sqlite3_column_text() or
5611** sqlite3_column_text16() is called. A zero-terminator might
5612** need to be added to the string.</li>
5613** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
5614** sqlite3_column_text16() is called. The content must be converted
5615** to UTF-16.</li>
5616** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
5617** sqlite3_column_text() is called. The content must be converted
5618** to UTF-8.</li>
5619** </ul>
5620**
5621** ^Conversions between UTF-16be and UTF-16le are always done in place and do
5622** not invalidate a prior pointer, though of course the content of the buffer
5623** that the prior pointer references will have been modified. Other kinds
5624** of conversion are done in place when it is possible, but sometimes they
5625** are not possible and in those cases prior pointers are invalidated.
5626**
5627** The safest policy is to invoke these routines
5628** in one of the following ways:
5629**
5630** <ul>
5631** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
5632** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
5633** <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
5634** </ul>
5635**
5636** In other words, you should call sqlite3_column_text(),
5637** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result
5638** into the desired format, then invoke sqlite3_column_bytes() or
5639** sqlite3_column_bytes16() to find the size of the result. Do not mix calls
5640** to sqlite3_column_text() or sqlite3_column_blob() with calls to
5641** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
5642** with calls to sqlite3_column_bytes().
5643**
5644** ^The pointers returned are valid until a type conversion occurs as
5645** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
5646** [sqlite3_finalize()] is called. ^The memory space used to hold strings
5647** and BLOBs is freed automatically. Do not pass the pointers returned
5648** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
5649** [sqlite3_free()].
5650**
5651** As long as the input parameters are correct, these routines will only
5652** fail if an out-of-memory error occurs during a format conversion.
5653** Only the following subset of interfaces are subject to out-of-memory
5654** errors:
5655**
5656** <ul>
5657** <li> sqlite3_column_blob()
5658** <li> sqlite3_column_text()
5659** <li> sqlite3_column_text16()
5660** <li> sqlite3_column_bytes()
5661** <li> sqlite3_column_bytes16()
5662** </ul>
5663**
5664** If an out-of-memory error occurs, then the return value from these
5665** routines is the same as if the column had contained an SQL NULL value.
5666** Valid SQL NULL returns can be distinguished from out-of-memory errors
5667** by invoking the [sqlite3_errcode()] immediately after the suspect
5668** return value is obtained and before any
5669** other SQLite interface is called on the same [database connection].
5670*/
5671SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
5672SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
5673SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
5674SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
5675SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
5676SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
5677SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
5678SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
5679SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
5680SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
5681
5682/*
5683** CAPI3REF: Destroy A Prepared Statement Object
5684** DESTRUCTOR: sqlite3_stmt
5685**
5686** ^The sqlite3_finalize() function is called to delete a [prepared statement].
5687** ^If the most recent evaluation of the statement encountered no errors
5688** or if the statement is never been evaluated, then sqlite3_finalize() returns
5689** SQLITE_OK. ^If the most recent evaluation of statement S failed, then
5690** sqlite3_finalize(S) returns the appropriate [error code] or
5691** [extended error code].
5692**
5693** ^The sqlite3_finalize(S) routine can be called at any point during
5694** the life cycle of [prepared statement] S:
5695** before statement S is ever evaluated, after
5696** one or more calls to [sqlite3_reset()], or after any call
5697** to [sqlite3_step()] regardless of whether or not the statement has
5698** completed execution.
5699**
5700** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
5701**
5702** The application must finalize every [prepared statement] in order to avoid
5703** resource leaks. It is a grievous error for the application to try to use
5704** a prepared statement after it has been finalized. Any use of a prepared
5705** statement after it has been finalized can result in undefined and
5706** undesirable behavior such as segfaults and heap corruption.
5707*/
5708SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
5709
5710/*
5711** CAPI3REF: Reset A Prepared Statement Object
5712** METHOD: sqlite3_stmt
5713**
5714** The sqlite3_reset() function is called to reset a [prepared statement]
5715** object back to its initial state, ready to be re-executed.
5716** ^Any SQL statement variables that had values bound to them using
5717** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
5718** Use [sqlite3_clear_bindings()] to reset the bindings.
5719**
5720** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
5721** back to the beginning of its program.
5722**
5723** ^If the most recent call to [sqlite3_step(S)] for the
5724** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE],
5725** or if [sqlite3_step(S)] has never before been called on S,
5726** then [sqlite3_reset(S)] returns [SQLITE_OK].
5727**
5728** ^If the most recent call to [sqlite3_step(S)] for the
5729** [prepared statement] S indicated an error, then
5730** [sqlite3_reset(S)] returns an appropriate [error code].
5731**
5732** ^The [sqlite3_reset(S)] interface does not change the values
5733** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
5734*/
5735SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
5736
5737/*
5738** CAPI3REF: Create Or Redefine SQL Functions
5739** KEYWORDS: {function creation routines}
5740** KEYWORDS: {application-defined SQL function}
5741** KEYWORDS: {application-defined SQL functions}
5742** METHOD: sqlite3
5743**
5744** ^These functions (collectively known as "function creation routines")
5745** are used to add SQL functions or aggregates or to redefine the behavior
5746** of existing SQL functions or aggregates. The only differences between
5747** the three "sqlite3_create_function*" routines are the text encoding
5748** expected for the second parameter (the name of the function being
5749** created) and the presence or absence of a destructor callback for
5750** the application data pointer. Function sqlite3_create_window_function()
5751** is similar, but allows the user to supply the extra callback functions
5752** needed by [aggregate window functions].
5753**
5754** ^The first parameter is the [database connection] to which the SQL
5755** function is to be added. ^If an application uses more than one database
5756** connection then application-defined SQL functions must be added
5757** to each database connection separately.
5758**
5759** ^The second parameter is the name of the SQL function to be created or
5760** redefined. ^The length of the name is limited to 255 bytes in a UTF-8
5761** representation, exclusive of the zero-terminator. ^Note that the name
5762** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
5763** ^Any attempt to create a function with a longer name
5764** will result in [SQLITE_MISUSE] being returned.
5765**
5766** ^The third parameter (nArg)
5767** is the number of arguments that the SQL function or
5768** aggregate takes. ^If this parameter is -1, then the SQL function or
5769** aggregate may take any number of arguments between 0 and the limit
5770** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]). If the third
5771** parameter is less than -1 or greater than 127 then the behavior is
5772** undefined.
5773**
5774** ^The fourth parameter, eTextRep, specifies what
5775** [SQLITE_UTF8 | text encoding] this SQL function prefers for
5776** its parameters. The application should set this parameter to
5777** [SQLITE_UTF16LE] if the function implementation invokes
5778** [sqlite3_value_text16le()] on an input, or [SQLITE_UTF16BE] if the
5779** implementation invokes [sqlite3_value_text16be()] on an input, or
5780** [SQLITE_UTF16] if [sqlite3_value_text16()] is used, or [SQLITE_UTF8]
5781** otherwise. ^The same SQL function may be registered multiple times using
5782** different preferred text encodings, with different implementations for
5783** each encoding.
5784** ^When multiple implementations of the same function are available, SQLite
5785** will pick the one that involves the least amount of data conversion.
5786**
5787** ^The fourth parameter may optionally be ORed with [SQLITE_DETERMINISTIC]
5788** to signal that the function will always return the same result given
5789** the same inputs within a single SQL statement. Most SQL functions are
5790** deterministic. The built-in [random()] SQL function is an example of a
5791** function that is not deterministic. The SQLite query planner is able to
5792** perform additional optimizations on deterministic functions, so use
5793** of the [SQLITE_DETERMINISTIC] flag is recommended where possible.
5794**
5795** ^(The fifth parameter is an arbitrary pointer. The implementation of the
5796** function can gain access to this pointer using [sqlite3_user_data()].)^
5797**
5798** ^The sixth, seventh and eighth parameters passed to the three
5799** "sqlite3_create_function*" functions, xFunc, xStep and xFinal, are
5800** pointers to C-language functions that implement the SQL function or
5801** aggregate. ^A scalar SQL function requires an implementation of the xFunc
5802** callback only; NULL pointers must be passed as the xStep and xFinal
5803** parameters. ^An aggregate SQL function requires an implementation of xStep
5804** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
5805** SQL function or aggregate, pass NULL pointers for all three function
5806** callbacks.
5807**
5808** ^The sixth, seventh, eighth and ninth parameters (xStep, xFinal, xValue
5809** and xInverse) passed to sqlite3_create_window_function are pointers to
5810** C-language callbacks that implement the new function. xStep and xFinal
5811** must both be non-NULL. xValue and xInverse may either both be NULL, in
5812** which case a regular aggregate function is created, or must both be
5813** non-NULL, in which case the new function may be used as either an aggregate
5814** or aggregate window function. More details regarding the implementation
5815** of aggregate window functions are
5816** [user-defined window functions|available here].
5817**
5818** ^(If the final parameter to sqlite3_create_function_v2() or
5819** sqlite3_create_window_function() is not NULL, then it is destructor for
5820** the application data pointer. The destructor is invoked when the function
5821** is deleted, either by being overloaded or when the database connection
5822** closes.)^ ^The destructor is also invoked if the call to
5823** sqlite3_create_function_v2() fails. ^When the destructor callback is
5824** invoked, it is passed a single argument which is a copy of the application
5825** data pointer which was the fifth parameter to sqlite3_create_function_v2().
5826**
5827** ^It is permitted to register multiple implementations of the same
5828** functions with the same name but with either differing numbers of
5829** arguments or differing preferred text encodings. ^SQLite will use
5830** the implementation that most closely matches the way in which the
5831** SQL function is used. ^A function implementation with a non-negative
5832** nArg parameter is a better match than a function implementation with
5833** a negative nArg. ^A function where the preferred text encoding
5834** matches the database encoding is a better
5835** match than a function where the encoding is different.
5836** ^A function where the encoding difference is between UTF16le and UTF16be
5837** is a closer match than a function where the encoding difference is
5838** between UTF8 and UTF16.
5839**
5840** ^Built-in functions may be overloaded by new application-defined functions.
5841**
5842** ^An application-defined function is permitted to call other
5843** SQLite interfaces. However, such calls must not
5844** close the database connection nor finalize or reset the prepared
5845** statement in which the function is running.
5846*/
5847SQLITE_API int sqlite3_create_function(
5848 sqlite3 *db,
5849 const char *zFunctionName,
5850 int nArg,
5851 int eTextRep,
5852 void *pApp,
5853 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5854 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5855 void (*xFinal)(sqlite3_context*)
5856);
5857SQLITE_API int sqlite3_create_function16(
5858 sqlite3 *db,
5859 const void *zFunctionName,
5860 int nArg,
5861 int eTextRep,
5862 void *pApp,
5863 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5864 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5865 void (*xFinal)(sqlite3_context*)
5866);
5867SQLITE_API int sqlite3_create_function_v2(
5868 sqlite3 *db,
5869 const char *zFunctionName,
5870 int nArg,
5871 int eTextRep,
5872 void *pApp,
5873 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5874 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5875 void (*xFinal)(sqlite3_context*),
5876 void(*xDestroy)(void*)
5877);
5878SQLITE_API int sqlite3_create_window_function(
5879 sqlite3 *db,
5880 const char *zFunctionName,
5881 int nArg,
5882 int eTextRep,
5883 void *pApp,
5884 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5885 void (*xFinal)(sqlite3_context*),
5886 void (*xValue)(sqlite3_context*),
5887 void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
5888 void(*xDestroy)(void*)
5889);
5890
5891/*
5892** CAPI3REF: Text Encodings
5893**
5894** These constant define integer codes that represent the various
5895** text encodings supported by SQLite.
5896*/
5897#define SQLITE_UTF8 1 /* IMP: R-37514-35566 */
5898#define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */
5899#define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */
5900#define SQLITE_UTF16 4 /* Use native byte order */
5901#define SQLITE_ANY 5 /* Deprecated */
5902#define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */
5903
5904/*
5905** CAPI3REF: Function Flags
5906**
5907** These constants may be ORed together with the
5908** [SQLITE_UTF8 | preferred text encoding] as the fourth argument
5909** to [sqlite3_create_function()], [sqlite3_create_function16()], or
5910** [sqlite3_create_function_v2()].
5911*/
5912#define SQLITE_DETERMINISTIC 0x800
5913
5914/*
5915** CAPI3REF: Deprecated Functions
5916** DEPRECATED
5917**
5918** These functions are [deprecated]. In order to maintain
5919** backwards compatibility with older code, these functions continue
5920** to be supported. However, new applications should avoid
5921** the use of these functions. To encourage programmers to avoid
5922** these functions, we will not explain what they do.
5923*/
5924#ifndef SQLITE_OMIT_DEPRECATED
5925SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
5926SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
5927SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
5928SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
5929SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
5930SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
5931 void*,sqlite3_int64);
5932#endif
5933
5934/*
5935** CAPI3REF: Obtaining SQL Values
5936** METHOD: sqlite3_value
5937**
5938** <b>Summary:</b>
5939** <blockquote><table border=0 cellpadding=0 cellspacing=0>
5940** <tr><td><b>sqlite3_value_blob</b><td>&rarr;<td>BLOB value
5941** <tr><td><b>sqlite3_value_double</b><td>&rarr;<td>REAL value
5942** <tr><td><b>sqlite3_value_int</b><td>&rarr;<td>32-bit INTEGER value
5943** <tr><td><b>sqlite3_value_int64</b><td>&rarr;<td>64-bit INTEGER value
5944** <tr><td><b>sqlite3_value_pointer</b><td>&rarr;<td>Pointer value
5945** <tr><td><b>sqlite3_value_text</b><td>&rarr;<td>UTF-8 TEXT value
5946** <tr><td><b>sqlite3_value_text16</b><td>&rarr;<td>UTF-16 TEXT value in
5947** the native byteorder
5948** <tr><td><b>sqlite3_value_text16be</b><td>&rarr;<td>UTF-16be TEXT value
5949** <tr><td><b>sqlite3_value_text16le</b><td>&rarr;<td>UTF-16le TEXT value
5950** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
5951** <tr><td><b>sqlite3_value_bytes</b><td>&rarr;<td>Size of a BLOB
5952** or a UTF-8 TEXT in bytes
5953** <tr><td><b>sqlite3_value_bytes16&nbsp;&nbsp;</b>
5954** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
5955** TEXT in bytes
5956** <tr><td><b>sqlite3_value_type</b><td>&rarr;<td>Default
5957** datatype of the value
5958** <tr><td><b>sqlite3_value_numeric_type&nbsp;&nbsp;</b>
5959** <td>&rarr;&nbsp;&nbsp;<td>Best numeric datatype of the value
5960** <tr><td><b>sqlite3_value_nochange&nbsp;&nbsp;</b>
5961** <td>&rarr;&nbsp;&nbsp;<td>True if the column is unchanged in an UPDATE
5962** against a virtual table.
5963** </table></blockquote>
5964**
5965** <b>Details:</b>
5966**
5967** These routines extract type, size, and content information from
5968** [protected sqlite3_value] objects. Protected sqlite3_value objects
5969** are used to pass parameter information into implementation of
5970** [application-defined SQL functions] and [virtual tables].
5971**
5972** These routines work only with [protected sqlite3_value] objects.
5973** Any attempt to use these routines on an [unprotected sqlite3_value]
5974** is not threadsafe.
5975**
5976** ^These routines work just like the corresponding [column access functions]
5977** except that these routines take a single [protected sqlite3_value] object
5978** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
5979**
5980** ^The sqlite3_value_text16() interface extracts a UTF-16 string
5981** in the native byte-order of the host machine. ^The
5982** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
5983** extract UTF-16 strings as big-endian and little-endian respectively.
5984**
5985** ^If [sqlite3_value] object V was initialized
5986** using [sqlite3_bind_pointer(S,I,P,X,D)] or [sqlite3_result_pointer(C,P,X,D)]
5987** and if X and Y are strings that compare equal according to strcmp(X,Y),
5988** then sqlite3_value_pointer(V,Y) will return the pointer P. ^Otherwise,
5989** sqlite3_value_pointer(V,Y) returns a NULL. The sqlite3_bind_pointer()
5990** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
5991**
5992** ^(The sqlite3_value_type(V) interface returns the
5993** [SQLITE_INTEGER | datatype code] for the initial datatype of the
5994** [sqlite3_value] object V. The returned value is one of [SQLITE_INTEGER],
5995** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].)^
5996** Other interfaces might change the datatype for an sqlite3_value object.
5997** For example, if the datatype is initially SQLITE_INTEGER and
5998** sqlite3_value_text(V) is called to extract a text value for that
5999** integer, then subsequent calls to sqlite3_value_type(V) might return
6000** SQLITE_TEXT. Whether or not a persistent internal datatype conversion
6001** occurs is undefined and may change from one release of SQLite to the next.
6002**
6003** ^(The sqlite3_value_numeric_type() interface attempts to apply
6004** numeric affinity to the value. This means that an attempt is
6005** made to convert the value to an integer or floating point. If
6006** such a conversion is possible without loss of information (in other
6007** words, if the value is a string that looks like a number)
6008** then the conversion is performed. Otherwise no conversion occurs.
6009** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
6010**
6011** ^Within the [xUpdate] method of a [virtual table], the
6012** sqlite3_value_nochange(X) interface returns true if and only if
6013** the column corresponding to X is unchanged by the UPDATE operation
6014** that the xUpdate method call was invoked to implement and if
6015** and the prior [xColumn] method call that was invoked to extracted
6016** the value for that column returned without setting a result (probably
6017** because it queried [sqlite3_vtab_nochange()] and found that the column
6018** was unchanging). ^Within an [xUpdate] method, any value for which
6019** sqlite3_value_nochange(X) is true will in all other respects appear
6020** to be a NULL value. If sqlite3_value_nochange(X) is invoked anywhere other
6021** than within an [xUpdate] method call for an UPDATE statement, then
6022** the return value is arbitrary and meaningless.
6023**
6024** Please pay particular attention to the fact that the pointer returned
6025** from [sqlite3_value_blob()], [sqlite3_value_text()], or
6026** [sqlite3_value_text16()] can be invalidated by a subsequent call to
6027** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
6028** or [sqlite3_value_text16()].
6029**
6030** These routines must be called from the same thread as
6031** the SQL function that supplied the [sqlite3_value*] parameters.
6032**
6033** As long as the input parameter is correct, these routines can only
6034** fail if an out-of-memory error occurs during a format conversion.
6035** Only the following subset of interfaces are subject to out-of-memory
6036** errors:
6037**
6038** <ul>
6039** <li> sqlite3_value_blob()
6040** <li> sqlite3_value_text()
6041** <li> sqlite3_value_text16()
6042** <li> sqlite3_value_text16le()
6043** <li> sqlite3_value_text16be()
6044** <li> sqlite3_value_bytes()
6045** <li> sqlite3_value_bytes16()
6046** </ul>
6047**
6048** If an out-of-memory error occurs, then the return value from these
6049** routines is the same as if the column had contained an SQL NULL value.
6050** Valid SQL NULL returns can be distinguished from out-of-memory errors
6051** by invoking the [sqlite3_errcode()] immediately after the suspect
6052** return value is obtained and before any
6053** other SQLite interface is called on the same [database connection].
6054*/
6055SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
6056SQLITE_API double sqlite3_value_double(sqlite3_value*);
6057SQLITE_API int sqlite3_value_int(sqlite3_value*);
6058SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
6059SQLITE_API void *sqlite3_value_pointer(sqlite3_value*, const char*);
6060SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
6061SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
6062SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
6063SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
6064SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
6065SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
6066SQLITE_API int sqlite3_value_type(sqlite3_value*);
6067SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
6068SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
6069
6070/*
6071** CAPI3REF: Finding The Subtype Of SQL Values
6072** METHOD: sqlite3_value
6073**
6074** The sqlite3_value_subtype(V) function returns the subtype for
6075** an [application-defined SQL function] argument V. The subtype
6076** information can be used to pass a limited amount of context from
6077** one SQL function to another. Use the [sqlite3_result_subtype()]
6078** routine to set the subtype for the return value of an SQL function.
6079*/
6080SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
6081
6082/*
6083** CAPI3REF: Copy And Free SQL Values
6084** METHOD: sqlite3_value
6085**
6086** ^The sqlite3_value_dup(V) interface makes a copy of the [sqlite3_value]
6087** object D and returns a pointer to that copy. ^The [sqlite3_value] returned
6088** is a [protected sqlite3_value] object even if the input is not.
6089** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a
6090** memory allocation fails.
6091**
6092** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object
6093** previously obtained from [sqlite3_value_dup()]. ^If V is a NULL pointer
6094** then sqlite3_value_free(V) is a harmless no-op.
6095*/
6096SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value*);
6097SQLITE_API void sqlite3_value_free(sqlite3_value*);
6098
6099/*
6100** CAPI3REF: Obtain Aggregate Function Context
6101** METHOD: sqlite3_context
6102**
6103** Implementations of aggregate SQL functions use this
6104** routine to allocate memory for storing their state.
6105**
6106** ^The first time the sqlite3_aggregate_context(C,N) routine is called
6107** for a particular aggregate function, SQLite
6108** allocates N of memory, zeroes out that memory, and returns a pointer
6109** to the new memory. ^On second and subsequent calls to
6110** sqlite3_aggregate_context() for the same aggregate function instance,
6111** the same buffer is returned. Sqlite3_aggregate_context() is normally
6112** called once for each invocation of the xStep callback and then one
6113** last time when the xFinal callback is invoked. ^(When no rows match
6114** an aggregate query, the xStep() callback of the aggregate function
6115** implementation is never called and xFinal() is called exactly once.
6116** In those cases, sqlite3_aggregate_context() might be called for the
6117** first time from within xFinal().)^
6118**
6119** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer
6120** when first called if N is less than or equal to zero or if a memory
6121** allocate error occurs.
6122**
6123** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
6124** determined by the N parameter on first successful call. Changing the
6125** value of N in subsequent call to sqlite3_aggregate_context() within
6126** the same aggregate function instance will not resize the memory
6127** allocation.)^ Within the xFinal callback, it is customary to set
6128** N=0 in calls to sqlite3_aggregate_context(C,N) so that no
6129** pointless memory allocations occur.
6130**
6131** ^SQLite automatically frees the memory allocated by
6132** sqlite3_aggregate_context() when the aggregate query concludes.
6133**
6134** The first parameter must be a copy of the
6135** [sqlite3_context | SQL function context] that is the first parameter
6136** to the xStep or xFinal callback routine that implements the aggregate
6137** function.
6138**
6139** This routine must be called from the same thread in which
6140** the aggregate SQL function is running.
6141*/
6142SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
6143
6144/*
6145** CAPI3REF: User Data For Functions
6146** METHOD: sqlite3_context
6147**
6148** ^The sqlite3_user_data() interface returns a copy of
6149** the pointer that was the pUserData parameter (the 5th parameter)
6150** of the [sqlite3_create_function()]
6151** and [sqlite3_create_function16()] routines that originally
6152** registered the application defined function.
6153**
6154** This routine must be called from the same thread in which
6155** the application-defined function is running.
6156*/
6157SQLITE_API void *sqlite3_user_data(sqlite3_context*);
6158
6159/*
6160** CAPI3REF: Database Connection For Functions
6161** METHOD: sqlite3_context
6162**
6163** ^The sqlite3_context_db_handle() interface returns a copy of
6164** the pointer to the [database connection] (the 1st parameter)
6165** of the [sqlite3_create_function()]
6166** and [sqlite3_create_function16()] routines that originally
6167** registered the application defined function.
6168*/
6169SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
6170
6171/*
6172** CAPI3REF: Function Auxiliary Data
6173** METHOD: sqlite3_context
6174**
6175** These functions may be used by (non-aggregate) SQL functions to
6176** associate metadata with argument values. If the same value is passed to
6177** multiple invocations of the same SQL function during query execution, under
6178** some circumstances the associated metadata may be preserved. An example
6179** of where this might be useful is in a regular-expression matching
6180** function. The compiled version of the regular expression can be stored as
6181** metadata associated with the pattern string.
6182** Then as long as the pattern string remains the same,
6183** the compiled regular expression can be reused on multiple
6184** invocations of the same function.
6185**
6186** ^The sqlite3_get_auxdata(C,N) interface returns a pointer to the metadata
6187** associated by the sqlite3_set_auxdata(C,N,P,X) function with the Nth argument
6188** value to the application-defined function. ^N is zero for the left-most
6189** function argument. ^If there is no metadata
6190** associated with the function argument, the sqlite3_get_auxdata(C,N) interface
6191** returns a NULL pointer.
6192**
6193** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th
6194** argument of the application-defined function. ^Subsequent
6195** calls to sqlite3_get_auxdata(C,N) return P from the most recent
6196** sqlite3_set_auxdata(C,N,P,X) call if the metadata is still valid or
6197** NULL if the metadata has been discarded.
6198** ^After each call to sqlite3_set_auxdata(C,N,P,X) where X is not NULL,
6199** SQLite will invoke the destructor function X with parameter P exactly
6200** once, when the metadata is discarded.
6201** SQLite is free to discard the metadata at any time, including: <ul>
6202** <li> ^(when the corresponding function parameter changes)^, or
6203** <li> ^(when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
6204** SQL statement)^, or
6205** <li> ^(when sqlite3_set_auxdata() is invoked again on the same
6206** parameter)^, or
6207** <li> ^(during the original sqlite3_set_auxdata() call when a memory
6208** allocation error occurs.)^ </ul>
6209**
6210** Note the last bullet in particular. The destructor X in
6211** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
6212** sqlite3_set_auxdata() interface even returns. Hence sqlite3_set_auxdata()
6213** should be called near the end of the function implementation and the
6214** function implementation should not make any use of P after
6215** sqlite3_set_auxdata() has been called.
6216**
6217** ^(In practice, metadata is preserved between function calls for
6218** function parameters that are compile-time constants, including literal
6219** values and [parameters] and expressions composed from the same.)^
6220**
6221** The value of the N parameter to these interfaces should be non-negative.
6222** Future enhancements may make use of negative N values to define new
6223** kinds of function caching behavior.
6224**
6225** These routines must be called from the same thread in which
6226** the SQL function is running.
6227*/
6228SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
6229SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
6230
6231
6232/*
6233** CAPI3REF: Constants Defining Special Destructor Behavior
6234**
6235** These are special values for the destructor that is passed in as the
6236** final argument to routines like [sqlite3_result_blob()]. ^If the destructor
6237** argument is SQLITE_STATIC, it means that the content pointer is constant
6238** and will never change. It does not need to be destroyed. ^The
6239** SQLITE_TRANSIENT value means that the content will likely change in
6240** the near future and that SQLite should make its own private copy of
6241** the content before returning.
6242**
6243** The typedef is necessary to work around problems in certain
6244** C++ compilers.
6245*/
6246typedef void (*sqlite3_destructor_type)(void*);
6247#define SQLITE_STATIC ((sqlite3_destructor_type)0)
6248#define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
6249
6250/*
6251** CAPI3REF: Setting The Result Of An SQL Function
6252** METHOD: sqlite3_context
6253**
6254** These routines are used by the xFunc or xFinal callbacks that
6255** implement SQL functions and aggregates. See
6256** [sqlite3_create_function()] and [sqlite3_create_function16()]
6257** for additional information.
6258**
6259** These functions work very much like the [parameter binding] family of
6260** functions used to bind values to host parameters in prepared statements.
6261** Refer to the [SQL parameter] documentation for additional information.
6262**
6263** ^The sqlite3_result_blob() interface sets the result from
6264** an application-defined function to be the BLOB whose content is pointed
6265** to by the second parameter and which is N bytes long where N is the
6266** third parameter.
6267**
6268** ^The sqlite3_result_zeroblob(C,N) and sqlite3_result_zeroblob64(C,N)
6269** interfaces set the result of the application-defined function to be
6270** a BLOB containing all zero bytes and N bytes in size.
6271**
6272** ^The sqlite3_result_double() interface sets the result from
6273** an application-defined function to be a floating point value specified
6274** by its 2nd argument.
6275**
6276** ^The sqlite3_result_error() and sqlite3_result_error16() functions
6277** cause the implemented SQL function to throw an exception.
6278** ^SQLite uses the string pointed to by the
6279** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16()
6280** as the text of an error message. ^SQLite interprets the error
6281** message string from sqlite3_result_error() as UTF-8. ^SQLite
6282** interprets the string from sqlite3_result_error16() as UTF-16 in native
6283** byte order. ^If the third parameter to sqlite3_result_error()
6284** or sqlite3_result_error16() is negative then SQLite takes as the error
6285** message all text up through the first zero character.
6286** ^If the third parameter to sqlite3_result_error() or
6287** sqlite3_result_error16() is non-negative then SQLite takes that many
6288** bytes (not characters) from the 2nd parameter as the error message.
6289** ^The sqlite3_result_error() and sqlite3_result_error16()
6290** routines make a private copy of the error message text before
6291** they return. Hence, the calling function can deallocate or
6292** modify the text after they return without harm.
6293** ^The sqlite3_result_error_code() function changes the error code
6294** returned by SQLite as a result of an error in a function. ^By default,
6295** the error code is SQLITE_ERROR. ^A subsequent call to sqlite3_result_error()
6296** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
6297**
6298** ^The sqlite3_result_error_toobig() interface causes SQLite to throw an
6299** error indicating that a string or BLOB is too long to represent.
6300**
6301** ^The sqlite3_result_error_nomem() interface causes SQLite to throw an
6302** error indicating that a memory allocation failed.
6303**
6304** ^The sqlite3_result_int() interface sets the return value
6305** of the application-defined function to be the 32-bit signed integer
6306** value given in the 2nd argument.
6307** ^The sqlite3_result_int64() interface sets the return value
6308** of the application-defined function to be the 64-bit signed integer
6309** value given in the 2nd argument.
6310**
6311** ^The sqlite3_result_null() interface sets the return value
6312** of the application-defined function to be NULL.
6313**
6314** ^The sqlite3_result_text(), sqlite3_result_text16(),
6315** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
6316** set the return value of the application-defined function to be
6317** a text string which is represented as UTF-8, UTF-16 native byte order,
6318** UTF-16 little endian, or UTF-16 big endian, respectively.
6319** ^The sqlite3_result_text64() interface sets the return value of an
6320** application-defined function to be a text string in an encoding
6321** specified by the fifth (and last) parameter, which must be one
6322** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE].
6323** ^SQLite takes the text result from the application from
6324** the 2nd parameter of the sqlite3_result_text* interfaces.
6325** ^If the 3rd parameter to the sqlite3_result_text* interfaces
6326** is negative, then SQLite takes result text from the 2nd parameter
6327** through the first zero character.
6328** ^If the 3rd parameter to the sqlite3_result_text* interfaces
6329** is non-negative, then as many bytes (not characters) of the text
6330** pointed to by the 2nd parameter are taken as the application-defined
6331** function result. If the 3rd parameter is non-negative, then it
6332** must be the byte offset into the string where the NUL terminator would
6333** appear if the string where NUL terminated. If any NUL characters occur
6334** in the string at a byte offset that is less than the value of the 3rd
6335** parameter, then the resulting string will contain embedded NULs and the
6336** result of expressions operating on strings with embedded NULs is undefined.
6337** ^If the 4th parameter to the sqlite3_result_text* interfaces
6338** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
6339** function as the destructor on the text or BLOB result when it has
6340** finished using that result.
6341** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
6342** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
6343** assumes that the text or BLOB result is in constant space and does not
6344** copy the content of the parameter nor call a destructor on the content
6345** when it has finished using that result.
6346** ^If the 4th parameter to the sqlite3_result_text* interfaces
6347** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
6348** then SQLite makes a copy of the result into space obtained
6349** from [sqlite3_malloc()] before it returns.
6350**
6351** ^The sqlite3_result_value() interface sets the result of
6352** the application-defined function to be a copy of the
6353** [unprotected sqlite3_value] object specified by the 2nd parameter. ^The
6354** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
6355** so that the [sqlite3_value] specified in the parameter may change or
6356** be deallocated after sqlite3_result_value() returns without harm.
6357** ^A [protected sqlite3_value] object may always be used where an
6358** [unprotected sqlite3_value] object is required, so either
6359** kind of [sqlite3_value] object can be used with this interface.
6360**
6361** ^The sqlite3_result_pointer(C,P,T,D) interface sets the result to an
6362** SQL NULL value, just like [sqlite3_result_null(C)], except that it
6363** also associates the host-language pointer P or type T with that
6364** NULL value such that the pointer can be retrieved within an
6365** [application-defined SQL function] using [sqlite3_value_pointer()].
6366** ^If the D parameter is not NULL, then it is a pointer to a destructor
6367** for the P parameter. ^SQLite invokes D with P as its only argument
6368** when SQLite is finished with P. The T parameter should be a static
6369** string and preferably a string literal. The sqlite3_result_pointer()
6370** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
6371**
6372** If these routines are called from within the different thread
6373** than the one containing the application-defined function that received
6374** the [sqlite3_context] pointer, the results are undefined.
6375*/
6376SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
6377SQLITE_API void sqlite3_result_blob64(sqlite3_context*,const void*,
6378 sqlite3_uint64,void(*)(void*));
6379SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
6380SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
6381SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
6382SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
6383SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
6384SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
6385SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
6386SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
6387SQLITE_API void sqlite3_result_null(sqlite3_context*);
6388SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
6389SQLITE_API void sqlite3_result_text64(sqlite3_context*, const char*,sqlite3_uint64,
6390 void(*)(void*), unsigned char encoding);
6391SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
6392SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
6393SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
6394SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
6395SQLITE_API void sqlite3_result_pointer(sqlite3_context*, void*,const char*,void(*)(void*));
6396SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
6397SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n);
6398
6399
6400/*
6401** CAPI3REF: Setting The Subtype Of An SQL Function
6402** METHOD: sqlite3_context
6403**
6404** The sqlite3_result_subtype(C,T) function causes the subtype of
6405** the result from the [application-defined SQL function] with
6406** [sqlite3_context] C to be the value T. Only the lower 8 bits
6407** of the subtype T are preserved in current versions of SQLite;
6408** higher order bits are discarded.
6409** The number of subtype bytes preserved by SQLite might increase
6410** in future releases of SQLite.
6411*/
6412SQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int);
6413
6414/*
6415** CAPI3REF: Define New Collating Sequences
6416** METHOD: sqlite3
6417**
6418** ^These functions add, remove, or modify a [collation] associated
6419** with the [database connection] specified as the first argument.
6420**
6421** ^The name of the collation is a UTF-8 string
6422** for sqlite3_create_collation() and sqlite3_create_collation_v2()
6423** and a UTF-16 string in native byte order for sqlite3_create_collation16().
6424** ^Collation names that compare equal according to [sqlite3_strnicmp()] are
6425** considered to be the same name.
6426**
6427** ^(The third argument (eTextRep) must be one of the constants:
6428** <ul>
6429** <li> [SQLITE_UTF8],
6430** <li> [SQLITE_UTF16LE],
6431** <li> [SQLITE_UTF16BE],
6432** <li> [SQLITE_UTF16], or
6433** <li> [SQLITE_UTF16_ALIGNED].
6434** </ul>)^
6435** ^The eTextRep argument determines the encoding of strings passed
6436** to the collating function callback, xCallback.
6437** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep
6438** force strings to be UTF16 with native byte order.
6439** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin
6440** on an even byte address.
6441**
6442** ^The fourth argument, pArg, is an application data pointer that is passed
6443** through as the first argument to the collating function callback.
6444**
6445** ^The fifth argument, xCallback, is a pointer to the collating function.
6446** ^Multiple collating functions can be registered using the same name but
6447** with different eTextRep parameters and SQLite will use whichever
6448** function requires the least amount of data transformation.
6449** ^If the xCallback argument is NULL then the collating function is
6450** deleted. ^When all collating functions having the same name are deleted,
6451** that collation is no longer usable.
6452**
6453** ^The collating function callback is invoked with a copy of the pArg
6454** application data pointer and with two strings in the encoding specified
6455** by the eTextRep argument. The collating function must return an
6456** integer that is negative, zero, or positive
6457** if the first string is less than, equal to, or greater than the second,
6458** respectively. A collating function must always return the same answer
6459** given the same inputs. If two or more collating functions are registered
6460** to the same collation name (using different eTextRep values) then all
6461** must give an equivalent answer when invoked with equivalent strings.
6462** The collating function must obey the following properties for all
6463** strings A, B, and C:
6464**
6465** <ol>
6466** <li> If A==B then B==A.
6467** <li> If A==B and B==C then A==C.
6468** <li> If A&lt;B THEN B&gt;A.
6469** <li> If A&lt;B and B&lt;C then A&lt;C.
6470** </ol>
6471**
6472** If a collating function fails any of the above constraints and that
6473** collating function is registered and used, then the behavior of SQLite
6474** is undefined.
6475**
6476** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation()
6477** with the addition that the xDestroy callback is invoked on pArg when
6478** the collating function is deleted.
6479** ^Collating functions are deleted when they are overridden by later
6480** calls to the collation creation functions or when the
6481** [database connection] is closed using [sqlite3_close()].
6482**
6483** ^The xDestroy callback is <u>not</u> called if the
6484** sqlite3_create_collation_v2() function fails. Applications that invoke
6485** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
6486** check the return code and dispose of the application data pointer
6487** themselves rather than expecting SQLite to deal with it for them.
6488** This is different from every other SQLite interface. The inconsistency
6489** is unfortunate but cannot be changed without breaking backwards
6490** compatibility.
6491**
6492** See also: [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
6493*/
6494SQLITE_API int sqlite3_create_collation(
6495 sqlite3*,
6496 const char *zName,
6497 int eTextRep,
6498 void *pArg,
6499 int(*xCompare)(void*,int,const void*,int,const void*)
6500);
6501SQLITE_API int sqlite3_create_collation_v2(
6502 sqlite3*,
6503 const char *zName,
6504 int eTextRep,
6505 void *pArg,
6506 int(*xCompare)(void*,int,const void*,int,const void*),
6507 void(*xDestroy)(void*)
6508);
6509SQLITE_API int sqlite3_create_collation16(
6510 sqlite3*,
6511 const void *zName,
6512 int eTextRep,
6513 void *pArg,
6514 int(*xCompare)(void*,int,const void*,int,const void*)
6515);
6516
6517/*
6518** CAPI3REF: Collation Needed Callbacks
6519** METHOD: sqlite3
6520**
6521** ^To avoid having to register all collation sequences before a database
6522** can be used, a single callback function may be registered with the
6523** [database connection] to be invoked whenever an undefined collation
6524** sequence is required.
6525**
6526** ^If the function is registered using the sqlite3_collation_needed() API,
6527** then it is passed the names of undefined collation sequences as strings
6528** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
6529** the names are passed as UTF-16 in machine native byte order.
6530** ^A call to either function replaces the existing collation-needed callback.
6531**
6532** ^(When the callback is invoked, the first argument passed is a copy
6533** of the second argument to sqlite3_collation_needed() or
6534** sqlite3_collation_needed16(). The second argument is the database
6535** connection. The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE],
6536** or [SQLITE_UTF16LE], indicating the most desirable form of the collation
6537** sequence function required. The fourth parameter is the name of the
6538** required collation sequence.)^
6539**
6540** The callback function should register the desired collation using
6541** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
6542** [sqlite3_create_collation_v2()].
6543*/
6544SQLITE_API int sqlite3_collation_needed(
6545 sqlite3*,
6546 void*,
6547 void(*)(void*,sqlite3*,int eTextRep,const char*)
6548);
6549SQLITE_API int sqlite3_collation_needed16(
6550 sqlite3*,
6551 void*,
6552 void(*)(void*,sqlite3*,int eTextRep,const void*)
6553);
6554
6555#ifdef SQLITE_HAS_CODEC
6556/*
6557** Specify the key for an encrypted database. This routine should be
6558** called right after sqlite3_open().
6559**
6560** The code to implement this API is not available in the public release
6561** of SQLite.
6562*/
6563SQLITE_API int sqlite3_key(
6564 sqlite3 *db, /* Database to be rekeyed */
6565 const void *pKey, int nKey /* The key */
6566);
6567SQLITE_API int sqlite3_key_v2(
6568 sqlite3 *db, /* Database to be rekeyed */
6569 const char *zDbName, /* Name of the database */
6570 const void *pKey, int nKey /* The key */
6571);
6572
6573/*
6574** Change the key on an open database. If the current database is not
6575** encrypted, this routine will encrypt it. If pNew==0 or nNew==0, the
6576** database is decrypted.
6577**
6578** The code to implement this API is not available in the public release
6579** of SQLite.
6580*/
6581SQLITE_API int sqlite3_rekey(
6582 sqlite3 *db, /* Database to be rekeyed */
6583 const void *pKey, int nKey /* The new key */
6584);
6585SQLITE_API int sqlite3_rekey_v2(
6586 sqlite3 *db, /* Database to be rekeyed */
6587 const char *zDbName, /* Name of the database */
6588 const void *pKey, int nKey /* The new key */
6589);
6590
6591/*
6592** Specify the activation key for a SEE database. Unless
6593** activated, none of the SEE routines will work.
6594*/
6595SQLITE_API void sqlite3_activate_see(
6596 const char *zPassPhrase /* Activation phrase */
6597);
6598#endif
6599
6600#ifdef SQLITE_ENABLE_CEROD
6601/*
6602** Specify the activation key for a CEROD database. Unless
6603** activated, none of the CEROD routines will work.
6604*/
6605SQLITE_API void sqlite3_activate_cerod(
6606 const char *zPassPhrase /* Activation phrase */
6607);
6608#endif
6609
6610/*
6611** CAPI3REF: Suspend Execution For A Short Time
6612**
6613** The sqlite3_sleep() function causes the current thread to suspend execution
6614** for at least a number of milliseconds specified in its parameter.
6615**
6616** If the operating system does not support sleep requests with
6617** millisecond time resolution, then the time will be rounded up to
6618** the nearest second. The number of milliseconds of sleep actually
6619** requested from the operating system is returned.
6620**
6621** ^SQLite implements this interface by calling the xSleep()
6622** method of the default [sqlite3_vfs] object. If the xSleep() method
6623** of the default VFS is not implemented correctly, or not implemented at
6624** all, then the behavior of sqlite3_sleep() may deviate from the description
6625** in the previous paragraphs.
6626*/
6627SQLITE_API int sqlite3_sleep(int);
6628
6629/*
6630** CAPI3REF: Name Of The Folder Holding Temporary Files
6631**
6632** ^(If this global variable is made to point to a string which is
6633** the name of a folder (a.k.a. directory), then all temporary files
6634** created by SQLite when using a built-in [sqlite3_vfs | VFS]
6635** will be placed in that directory.)^ ^If this variable
6636** is a NULL pointer, then SQLite performs a search for an appropriate
6637** temporary file directory.
6638**
6639** Applications are strongly discouraged from using this global variable.
6640** It is required to set a temporary folder on Windows Runtime (WinRT).
6641** But for all other platforms, it is highly recommended that applications
6642** neither read nor write this variable. This global variable is a relic
6643** that exists for backwards compatibility of legacy applications and should
6644** be avoided in new projects.
6645**
6646** It is not safe to read or modify this variable in more than one
6647** thread at a time. It is not safe to read or modify this variable
6648** if a [database connection] is being used at the same time in a separate
6649** thread.
6650** It is intended that this variable be set once
6651** as part of process initialization and before any SQLite interface
6652** routines have been called and that this variable remain unchanged
6653** thereafter.
6654**
6655** ^The [temp_store_directory pragma] may modify this variable and cause
6656** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore,
6657** the [temp_store_directory pragma] always assumes that any string
6658** that this variable points to is held in memory obtained from
6659** [sqlite3_malloc] and the pragma may attempt to free that memory
6660** using [sqlite3_free].
6661** Hence, if this variable is modified directly, either it should be
6662** made NULL or made to point to memory obtained from [sqlite3_malloc]
6663** or else the use of the [temp_store_directory pragma] should be avoided.
6664** Except when requested by the [temp_store_directory pragma], SQLite
6665** does not free the memory that sqlite3_temp_directory points to. If
6666** the application wants that memory to be freed, it must do
6667** so itself, taking care to only do so after all [database connection]
6668** objects have been destroyed.
6669**
6670** <b>Note to Windows Runtime users:</b> The temporary directory must be set
6671** prior to calling [sqlite3_open] or [sqlite3_open_v2]. Otherwise, various
6672** features that require the use of temporary files may fail. Here is an
6673** example of how to do this using C++ with the Windows Runtime:
6674**
6675** <blockquote><pre>
6676** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
6677** &nbsp; TemporaryFolder->Path->Data();
6678** char zPathBuf&#91;MAX_PATH + 1&#93;;
6679** memset(zPathBuf, 0, sizeof(zPathBuf));
6680** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
6681** &nbsp; NULL, NULL);
6682** sqlite3_temp_directory = sqlite3_mprintf("%s", zPathBuf);
6683** </pre></blockquote>
6684*/
6685SQLITE_API char *sqlite3_temp_directory;
6686
6687/*
6688** CAPI3REF: Name Of The Folder Holding Database Files
6689**
6690** ^(If this global variable is made to point to a string which is
6691** the name of a folder (a.k.a. directory), then all database files
6692** specified with a relative pathname and created or accessed by
6693** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
6694** to be relative to that directory.)^ ^If this variable is a NULL
6695** pointer, then SQLite assumes that all database files specified
6696** with a relative pathname are relative to the current directory
6697** for the process. Only the windows VFS makes use of this global
6698** variable; it is ignored by the unix VFS.
6699**
6700** Changing the value of this variable while a database connection is
6701** open can result in a corrupt database.
6702**
6703** It is not safe to read or modify this variable in more than one
6704** thread at a time. It is not safe to read or modify this variable
6705** if a [database connection] is being used at the same time in a separate
6706** thread.
6707** It is intended that this variable be set once
6708** as part of process initialization and before any SQLite interface
6709** routines have been called and that this variable remain unchanged
6710** thereafter.
6711**
6712** ^The [data_store_directory pragma] may modify this variable and cause
6713** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore,
6714** the [data_store_directory pragma] always assumes that any string
6715** that this variable points to is held in memory obtained from
6716** [sqlite3_malloc] and the pragma may attempt to free that memory
6717** using [sqlite3_free].
6718** Hence, if this variable is modified directly, either it should be
6719** made NULL or made to point to memory obtained from [sqlite3_malloc]
6720** or else the use of the [data_store_directory pragma] should be avoided.
6721*/
6722SQLITE_API char *sqlite3_data_directory;
6723
6724/*
6725** CAPI3REF: Win32 Specific Interface
6726**
6727** These interfaces are available only on Windows. The
6728** [sqlite3_win32_set_directory] interface is used to set the value associated
6729** with the [sqlite3_temp_directory] or [sqlite3_data_directory] variable, to
6730** zValue, depending on the value of the type parameter. The zValue parameter
6731** should be NULL to cause the previous value to be freed via [sqlite3_free];
6732** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
6733** prior to being used. The [sqlite3_win32_set_directory] interface returns
6734** [SQLITE_OK] to indicate success, [SQLITE_ERROR] if the type is unsupported,
6735** or [SQLITE_NOMEM] if memory could not be allocated. The value of the
6736** [sqlite3_data_directory] variable is intended to act as a replacement for
6737** the current directory on the sub-platforms of Win32 where that concept is
6738** not present, e.g. WinRT and UWP. The [sqlite3_win32_set_directory8] and
6739** [sqlite3_win32_set_directory16] interfaces behave exactly the same as the
6740** sqlite3_win32_set_directory interface except the string parameter must be
6741** UTF-8 or UTF-16, respectively.
6742*/
6743SQLITE_API int sqlite3_win32_set_directory(
6744 unsigned long type, /* Identifier for directory being set or reset */
6745 void *zValue /* New value for directory being set or reset */
6746);
6747SQLITE_API int sqlite3_win32_set_directory8(unsigned long type, const char *zValue);
6748SQLITE_API int sqlite3_win32_set_directory16(unsigned long type, const void *zValue);
6749
6750/*
6751** CAPI3REF: Win32 Directory Types
6752**
6753** These macros are only available on Windows. They define the allowed values
6754** for the type argument to the [sqlite3_win32_set_directory] interface.
6755*/
6756#define SQLITE_WIN32_DATA_DIRECTORY_TYPE 1
6757#define SQLITE_WIN32_TEMP_DIRECTORY_TYPE 2
6758
6759/*
6760** CAPI3REF: Test For Auto-Commit Mode
6761** KEYWORDS: {autocommit mode}
6762** METHOD: sqlite3
6763**
6764** ^The sqlite3_get_autocommit() interface returns non-zero or
6765** zero if the given database connection is or is not in autocommit mode,
6766** respectively. ^Autocommit mode is on by default.
6767** ^Autocommit mode is disabled by a [BEGIN] statement.
6768** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
6769**
6770** If certain kinds of errors occur on a statement within a multi-statement
6771** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR],
6772** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
6773** transaction might be rolled back automatically. The only way to
6774** find out whether SQLite automatically rolled back the transaction after
6775** an error is to use this function.
6776**
6777** If another thread changes the autocommit status of the database
6778** connection while this routine is running, then the return value
6779** is undefined.
6780*/
6781SQLITE_API int sqlite3_get_autocommit(sqlite3*);
6782
6783/*
6784** CAPI3REF: Find The Database Handle Of A Prepared Statement
6785** METHOD: sqlite3_stmt
6786**
6787** ^The sqlite3_db_handle interface returns the [database connection] handle
6788** to which a [prepared statement] belongs. ^The [database connection]
6789** returned by sqlite3_db_handle is the same [database connection]
6790** that was the first argument
6791** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
6792** create the statement in the first place.
6793*/
6794SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
6795
6796/*
6797** CAPI3REF: Return The Filename For A Database Connection
6798** METHOD: sqlite3
6799**
6800** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename
6801** associated with database N of connection D. ^The main database file
6802** has the name "main". If there is no attached database N on the database
6803** connection D, or if database N is a temporary or in-memory database, then
6804** a NULL pointer is returned.
6805**
6806** ^The filename returned by this function is the output of the
6807** xFullPathname method of the [VFS]. ^In other words, the filename
6808** will be an absolute pathname, even if the filename used
6809** to open the database originally was a URI or relative pathname.
6810*/
6811SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
6812
6813/*
6814** CAPI3REF: Determine if a database is read-only
6815** METHOD: sqlite3
6816**
6817** ^The sqlite3_db_readonly(D,N) interface returns 1 if the database N
6818** of connection D is read-only, 0 if it is read/write, or -1 if N is not
6819** the name of a database on connection D.
6820*/
6821SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
6822
6823/*
6824** CAPI3REF: Find the next prepared statement
6825** METHOD: sqlite3
6826**
6827** ^This interface returns a pointer to the next [prepared statement] after
6828** pStmt associated with the [database connection] pDb. ^If pStmt is NULL
6829** then this interface returns a pointer to the first prepared statement
6830** associated with the database connection pDb. ^If no prepared statement
6831** satisfies the conditions of this routine, it returns NULL.
6832**
6833** The [database connection] pointer D in a call to
6834** [sqlite3_next_stmt(D,S)] must refer to an open database
6835** connection and in particular must not be a NULL pointer.
6836*/
6837SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
6838
6839/*
6840** CAPI3REF: Commit And Rollback Notification Callbacks
6841** METHOD: sqlite3
6842**
6843** ^The sqlite3_commit_hook() interface registers a callback
6844** function to be invoked whenever a transaction is [COMMIT | committed].
6845** ^Any callback set by a previous call to sqlite3_commit_hook()
6846** for the same database connection is overridden.
6847** ^The sqlite3_rollback_hook() interface registers a callback
6848** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
6849** ^Any callback set by a previous call to sqlite3_rollback_hook()
6850** for the same database connection is overridden.
6851** ^The pArg argument is passed through to the callback.
6852** ^If the callback on a commit hook function returns non-zero,
6853** then the commit is converted into a rollback.
6854**
6855** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
6856** return the P argument from the previous call of the same function
6857** on the same [database connection] D, or NULL for
6858** the first call for each function on D.
6859**
6860** The commit and rollback hook callbacks are not reentrant.
6861** The callback implementation must not do anything that will modify
6862** the database connection that invoked the callback. Any actions
6863** to modify the database connection must be deferred until after the
6864** completion of the [sqlite3_step()] call that triggered the commit
6865** or rollback hook in the first place.
6866** Note that running any other SQL statements, including SELECT statements,
6867** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify
6868** the database connections for the meaning of "modify" in this paragraph.
6869**
6870** ^Registering a NULL function disables the callback.
6871**
6872** ^When the commit hook callback routine returns zero, the [COMMIT]
6873** operation is allowed to continue normally. ^If the commit hook
6874** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
6875** ^The rollback hook is invoked on a rollback that results from a commit
6876** hook returning non-zero, just as it would be with any other rollback.
6877**
6878** ^For the purposes of this API, a transaction is said to have been
6879** rolled back if an explicit "ROLLBACK" statement is executed, or
6880** an error or constraint causes an implicit rollback to occur.
6881** ^The rollback callback is not invoked if a transaction is
6882** automatically rolled back because the database connection is closed.
6883**
6884** See also the [sqlite3_update_hook()] interface.
6885*/
6886SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
6887SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
6888
6889/*
6890** CAPI3REF: Data Change Notification Callbacks
6891** METHOD: sqlite3
6892**
6893** ^The sqlite3_update_hook() interface registers a callback function
6894** with the [database connection] identified by the first argument
6895** to be invoked whenever a row is updated, inserted or deleted in
6896** a [rowid table].
6897** ^Any callback set by a previous call to this function
6898** for the same database connection is overridden.
6899**
6900** ^The second argument is a pointer to the function to invoke when a
6901** row is updated, inserted or deleted in a rowid table.
6902** ^The first argument to the callback is a copy of the third argument
6903** to sqlite3_update_hook().
6904** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
6905** or [SQLITE_UPDATE], depending on the operation that caused the callback
6906** to be invoked.
6907** ^The third and fourth arguments to the callback contain pointers to the
6908** database and table name containing the affected row.
6909** ^The final callback parameter is the [rowid] of the row.
6910** ^In the case of an update, this is the [rowid] after the update takes place.
6911**
6912** ^(The update hook is not invoked when internal system tables are
6913** modified (i.e. sqlite_master and sqlite_sequence).)^
6914** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
6915**
6916** ^In the current implementation, the update hook
6917** is not invoked when conflicting rows are deleted because of an
6918** [ON CONFLICT | ON CONFLICT REPLACE] clause. ^Nor is the update hook
6919** invoked when rows are deleted using the [truncate optimization].
6920** The exceptions defined in this paragraph might change in a future
6921** release of SQLite.
6922**
6923** The update hook implementation must not do anything that will modify
6924** the database connection that invoked the update hook. Any actions
6925** to modify the database connection must be deferred until after the
6926** completion of the [sqlite3_step()] call that triggered the update hook.
6927** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
6928** database connections for the meaning of "modify" in this paragraph.
6929**
6930** ^The sqlite3_update_hook(D,C,P) function
6931** returns the P argument from the previous call
6932** on the same [database connection] D, or NULL for
6933** the first call on D.
6934**
6935** See also the [sqlite3_commit_hook()], [sqlite3_rollback_hook()],
6936** and [sqlite3_preupdate_hook()] interfaces.
6937*/
6938SQLITE_API void *sqlite3_update_hook(
6939 sqlite3*,
6940 void(*)(void *,int ,char const *,char const *,sqlite3_int64),
6941 void*
6942);
6943
6944/*
6945** CAPI3REF: Enable Or Disable Shared Pager Cache
6946**
6947** ^(This routine enables or disables the sharing of the database cache
6948** and schema data structures between [database connection | connections]
6949** to the same database. Sharing is enabled if the argument is true
6950** and disabled if the argument is false.)^
6951**
6952** ^Cache sharing is enabled and disabled for an entire process.
6953** This is a change as of SQLite [version 3.5.0] ([dateof:3.5.0]).
6954** In prior versions of SQLite,
6955** sharing was enabled or disabled for each thread separately.
6956**
6957** ^(The cache sharing mode set by this interface effects all subsequent
6958** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
6959** Existing database connections continue use the sharing mode
6960** that was in effect at the time they were opened.)^
6961**
6962** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled
6963** successfully. An [error code] is returned otherwise.)^
6964**
6965** ^Shared cache is disabled by default. But this might change in
6966** future releases of SQLite. Applications that care about shared
6967** cache setting should set it explicitly.
6968**
6969** Note: This method is disabled on MacOS X 10.7 and iOS version 5.0
6970** and will always return SQLITE_MISUSE. On those systems,
6971** shared cache mode should be enabled per-database connection via
6972** [sqlite3_open_v2()] with [SQLITE_OPEN_SHAREDCACHE].
6973**
6974** This interface is threadsafe on processors where writing a
6975** 32-bit integer is atomic.
6976**
6977** See Also: [SQLite Shared-Cache Mode]
6978*/
6979SQLITE_API int sqlite3_enable_shared_cache(int);
6980
6981/*
6982** CAPI3REF: Attempt To Free Heap Memory
6983**
6984** ^The sqlite3_release_memory() interface attempts to free N bytes
6985** of heap memory by deallocating non-essential memory allocations
6986** held by the database library. Memory used to cache database
6987** pages to improve performance is an example of non-essential memory.
6988** ^sqlite3_release_memory() returns the number of bytes actually freed,
6989** which might be more or less than the amount requested.
6990** ^The sqlite3_release_memory() routine is a no-op returning zero
6991** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
6992**
6993** See also: [sqlite3_db_release_memory()]
6994*/
6995SQLITE_API int sqlite3_release_memory(int);
6996
6997/*
6998** CAPI3REF: Free Memory Used By A Database Connection
6999** METHOD: sqlite3
7000**
7001** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
7002** memory as possible from database connection D. Unlike the
7003** [sqlite3_release_memory()] interface, this interface is in effect even
7004** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
7005** omitted.
7006**
7007** See also: [sqlite3_release_memory()]
7008*/
7009SQLITE_API int sqlite3_db_release_memory(sqlite3*);
7010
7011/*
7012** CAPI3REF: Impose A Limit On Heap Size
7013**
7014** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
7015** soft limit on the amount of heap memory that may be allocated by SQLite.
7016** ^SQLite strives to keep heap memory utilization below the soft heap
7017** limit by reducing the number of pages held in the page cache
7018** as heap memory usages approaches the limit.
7019** ^The soft heap limit is "soft" because even though SQLite strives to stay
7020** below the limit, it will exceed the limit rather than generate
7021** an [SQLITE_NOMEM] error. In other words, the soft heap limit
7022** is advisory only.
7023**
7024** ^The return value from sqlite3_soft_heap_limit64() is the size of
7025** the soft heap limit prior to the call, or negative in the case of an
7026** error. ^If the argument N is negative
7027** then no change is made to the soft heap limit. Hence, the current
7028** size of the soft heap limit can be determined by invoking
7029** sqlite3_soft_heap_limit64() with a negative argument.
7030**
7031** ^If the argument N is zero then the soft heap limit is disabled.
7032**
7033** ^(The soft heap limit is not enforced in the current implementation
7034** if one or more of following conditions are true:
7035**
7036** <ul>
7037** <li> The soft heap limit is set to zero.
7038** <li> Memory accounting is disabled using a combination of the
7039** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
7040** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
7041** <li> An alternative page cache implementation is specified using
7042** [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...).
7043** <li> The page cache allocates from its own memory pool supplied
7044** by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
7045** from the heap.
7046** </ul>)^
7047**
7048** Beginning with SQLite [version 3.7.3] ([dateof:3.7.3]),
7049** the soft heap limit is enforced
7050** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT]
7051** compile-time option is invoked. With [SQLITE_ENABLE_MEMORY_MANAGEMENT],
7052** the soft heap limit is enforced on every memory allocation. Without
7053** [SQLITE_ENABLE_MEMORY_MANAGEMENT], the soft heap limit is only enforced
7054** when memory is allocated by the page cache. Testing suggests that because
7055** the page cache is the predominate memory user in SQLite, most
7056** applications will achieve adequate soft heap limit enforcement without
7057** the use of [SQLITE_ENABLE_MEMORY_MANAGEMENT].
7058**
7059** The circumstances under which SQLite will enforce the soft heap limit may
7060** changes in future releases of SQLite.
7061*/
7062SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N);
7063
7064/*
7065** CAPI3REF: Deprecated Soft Heap Limit Interface
7066** DEPRECATED
7067**
7068** This is a deprecated version of the [sqlite3_soft_heap_limit64()]
7069** interface. This routine is provided for historical compatibility
7070** only. All new applications should use the
7071** [sqlite3_soft_heap_limit64()] interface rather than this one.
7072*/
7073SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
7074
7075
7076/*
7077** CAPI3REF: Extract Metadata About A Column Of A Table
7078** METHOD: sqlite3
7079**
7080** ^(The sqlite3_table_column_metadata(X,D,T,C,....) routine returns
7081** information about column C of table T in database D
7082** on [database connection] X.)^ ^The sqlite3_table_column_metadata()
7083** interface returns SQLITE_OK and fills in the non-NULL pointers in
7084** the final five arguments with appropriate values if the specified
7085** column exists. ^The sqlite3_table_column_metadata() interface returns
7086** SQLITE_ERROR and if the specified column does not exist.
7087** ^If the column-name parameter to sqlite3_table_column_metadata() is a
7088** NULL pointer, then this routine simply checks for the existence of the
7089** table and returns SQLITE_OK if the table exists and SQLITE_ERROR if it
7090** does not. If the table name parameter T in a call to
7091** sqlite3_table_column_metadata(X,D,T,C,...) is NULL then the result is
7092** undefined behavior.
7093**
7094** ^The column is identified by the second, third and fourth parameters to
7095** this function. ^(The second parameter is either the name of the database
7096** (i.e. "main", "temp", or an attached database) containing the specified
7097** table or NULL.)^ ^If it is NULL, then all attached databases are searched
7098** for the table using the same algorithm used by the database engine to
7099** resolve unqualified table references.
7100**
7101** ^The third and fourth parameters to this function are the table and column
7102** name of the desired column, respectively.
7103**
7104** ^Metadata is returned by writing to the memory locations passed as the 5th
7105** and subsequent parameters to this function. ^Any of these arguments may be
7106** NULL, in which case the corresponding element of metadata is omitted.
7107**
7108** ^(<blockquote>
7109** <table border="1">
7110** <tr><th> Parameter <th> Output<br>Type <th> Description
7111**
7112** <tr><td> 5th <td> const char* <td> Data type
7113** <tr><td> 6th <td> const char* <td> Name of default collation sequence
7114** <tr><td> 7th <td> int <td> True if column has a NOT NULL constraint
7115** <tr><td> 8th <td> int <td> True if column is part of the PRIMARY KEY
7116** <tr><td> 9th <td> int <td> True if column is [AUTOINCREMENT]
7117** </table>
7118** </blockquote>)^
7119**
7120** ^The memory pointed to by the character pointers returned for the
7121** declaration type and collation sequence is valid until the next
7122** call to any SQLite API function.
7123**
7124** ^If the specified table is actually a view, an [error code] is returned.
7125**
7126** ^If the specified column is "rowid", "oid" or "_rowid_" and the table
7127** is not a [WITHOUT ROWID] table and an
7128** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output
7129** parameters are set for the explicitly declared column. ^(If there is no
7130** [INTEGER PRIMARY KEY] column, then the outputs
7131** for the [rowid] are set as follows:
7132**
7133** <pre>
7134** data type: "INTEGER"
7135** collation sequence: "BINARY"
7136** not null: 0
7137** primary key: 1
7138** auto increment: 0
7139** </pre>)^
7140**
7141** ^This function causes all database schemas to be read from disk and
7142** parsed, if that has not already been done, and returns an error if
7143** any errors are encountered while loading the schema.
7144*/
7145SQLITE_API int sqlite3_table_column_metadata(
7146 sqlite3 *db, /* Connection handle */
7147 const char *zDbName, /* Database name or NULL */
7148 const char *zTableName, /* Table name */
7149 const char *zColumnName, /* Column name */
7150 char const **pzDataType, /* OUTPUT: Declared data type */
7151 char const **pzCollSeq, /* OUTPUT: Collation sequence name */
7152 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
7153 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
7154 int *pAutoinc /* OUTPUT: True if column is auto-increment */
7155);
7156
7157/*
7158** CAPI3REF: Load An Extension
7159** METHOD: sqlite3
7160**
7161** ^This interface loads an SQLite extension library from the named file.
7162**
7163** ^The sqlite3_load_extension() interface attempts to load an
7164** [SQLite extension] library contained in the file zFile. If
7165** the file cannot be loaded directly, attempts are made to load
7166** with various operating-system specific extensions added.
7167** So for example, if "samplelib" cannot be loaded, then names like
7168** "samplelib.so" or "samplelib.dylib" or "samplelib.dll" might
7169** be tried also.
7170**
7171** ^The entry point is zProc.
7172** ^(zProc may be 0, in which case SQLite will try to come up with an
7173** entry point name on its own. It first tries "sqlite3_extension_init".
7174** If that does not work, it constructs a name "sqlite3_X_init" where the
7175** X is consists of the lower-case equivalent of all ASCII alphabetic
7176** characters in the filename from the last "/" to the first following
7177** "." and omitting any initial "lib".)^
7178** ^The sqlite3_load_extension() interface returns
7179** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
7180** ^If an error occurs and pzErrMsg is not 0, then the
7181** [sqlite3_load_extension()] interface shall attempt to
7182** fill *pzErrMsg with error message text stored in memory
7183** obtained from [sqlite3_malloc()]. The calling function
7184** should free this memory by calling [sqlite3_free()].
7185**
7186** ^Extension loading must be enabled using
7187** [sqlite3_enable_load_extension()] or
7188** [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],1,NULL)
7189** prior to calling this API,
7190** otherwise an error will be returned.
7191**
7192** <b>Security warning:</b> It is recommended that the
7193** [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method be used to enable only this
7194** interface. The use of the [sqlite3_enable_load_extension()] interface
7195** should be avoided. This will keep the SQL function [load_extension()]
7196** disabled and prevent SQL injections from giving attackers
7197** access to extension loading capabilities.
7198**
7199** See also the [load_extension() SQL function].
7200*/
7201SQLITE_API int sqlite3_load_extension(
7202 sqlite3 *db, /* Load the extension into this database connection */
7203 const char *zFile, /* Name of the shared library containing extension */
7204 const char *zProc, /* Entry point. Derived from zFile if 0 */
7205 char **pzErrMsg /* Put error message here if not 0 */
7206);
7207
7208/*
7209** CAPI3REF: Enable Or Disable Extension Loading
7210** METHOD: sqlite3
7211**
7212** ^So as not to open security holes in older applications that are
7213** unprepared to deal with [extension loading], and as a means of disabling
7214** [extension loading] while evaluating user-entered SQL, the following API
7215** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
7216**
7217** ^Extension loading is off by default.
7218** ^Call the sqlite3_enable_load_extension() routine with onoff==1
7219** to turn extension loading on and call it with onoff==0 to turn
7220** it back off again.
7221**
7222** ^This interface enables or disables both the C-API
7223** [sqlite3_load_extension()] and the SQL function [load_extension()].
7224** ^(Use [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],..)
7225** to enable or disable only the C-API.)^
7226**
7227** <b>Security warning:</b> It is recommended that extension loading
7228** be disabled using the [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method
7229** rather than this interface, so the [load_extension()] SQL function
7230** remains disabled. This will prevent SQL injections from giving attackers
7231** access to extension loading capabilities.
7232*/
7233SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
7234
7235/*
7236** CAPI3REF: Automatically Load Statically Linked Extensions
7237**
7238** ^This interface causes the xEntryPoint() function to be invoked for
7239** each new [database connection] that is created. The idea here is that
7240** xEntryPoint() is the entry point for a statically linked [SQLite extension]
7241** that is to be automatically loaded into all new database connections.
7242**
7243** ^(Even though the function prototype shows that xEntryPoint() takes
7244** no arguments and returns void, SQLite invokes xEntryPoint() with three
7245** arguments and expects an integer result as if the signature of the
7246** entry point where as follows:
7247**
7248** <blockquote><pre>
7249** &nbsp; int xEntryPoint(
7250** &nbsp; sqlite3 *db,
7251** &nbsp; const char **pzErrMsg,
7252** &nbsp; const struct sqlite3_api_routines *pThunk
7253** &nbsp; );
7254** </pre></blockquote>)^
7255**
7256** If the xEntryPoint routine encounters an error, it should make *pzErrMsg
7257** point to an appropriate error message (obtained from [sqlite3_mprintf()])
7258** and return an appropriate [error code]. ^SQLite ensures that *pzErrMsg
7259** is NULL before calling the xEntryPoint(). ^SQLite will invoke
7260** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns. ^If any
7261** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()],
7262** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail.
7263**
7264** ^Calling sqlite3_auto_extension(X) with an entry point X that is already
7265** on the list of automatic extensions is a harmless no-op. ^No entry point
7266** will be called more than once for each database connection that is opened.
7267**
7268** See also: [sqlite3_reset_auto_extension()]
7269** and [sqlite3_cancel_auto_extension()]
7270*/
7271SQLITE_API int sqlite3_auto_extension(void(*xEntryPoint)(void));
7272
7273/*
7274** CAPI3REF: Cancel Automatic Extension Loading
7275**
7276** ^The [sqlite3_cancel_auto_extension(X)] interface unregisters the
7277** initialization routine X that was registered using a prior call to
7278** [sqlite3_auto_extension(X)]. ^The [sqlite3_cancel_auto_extension(X)]
7279** routine returns 1 if initialization routine X was successfully
7280** unregistered and it returns 0 if X was not on the list of initialization
7281** routines.
7282*/
7283SQLITE_API int sqlite3_cancel_auto_extension(void(*xEntryPoint)(void));
7284
7285/*
7286** CAPI3REF: Reset Automatic Extension Loading
7287**
7288** ^This interface disables all automatic extensions previously
7289** registered using [sqlite3_auto_extension()].
7290*/
7291SQLITE_API void sqlite3_reset_auto_extension(void);
7292
7293/*
7294** The interface to the virtual-table mechanism is currently considered
7295** to be experimental. The interface might change in incompatible ways.
7296** If this is a problem for you, do not use the interface at this time.
7297**
7298** When the virtual-table mechanism stabilizes, we will declare the
7299** interface fixed, support it indefinitely, and remove this comment.
7300*/
7301
7302/*
7303** Structures used by the virtual table interface
7304*/
7305typedef struct sqlite3_vtab sqlite3_vtab;
7306typedef struct sqlite3_index_info sqlite3_index_info;
7307typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
7308typedef struct sqlite3_module sqlite3_module;
7309
7310/*
7311** CAPI3REF: Virtual Table Object
7312** KEYWORDS: sqlite3_module {virtual table module}
7313**
7314** This structure, sometimes called a "virtual table module",
7315** defines the implementation of a [virtual tables].
7316** This structure consists mostly of methods for the module.
7317**
7318** ^A virtual table module is created by filling in a persistent
7319** instance of this structure and passing a pointer to that instance
7320** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
7321** ^The registration remains valid until it is replaced by a different
7322** module or until the [database connection] closes. The content
7323** of this structure must not change while it is registered with
7324** any database connection.
7325*/
7326struct sqlite3_module {
7327 int iVersion;
7328 int (*xCreate)(sqlite3*, void *pAux,
7329 int argc, const char *const*argv,
7330 sqlite3_vtab **ppVTab, char**);
7331 int (*xConnect)(sqlite3*, void *pAux,
7332 int argc, const char *const*argv,
7333 sqlite3_vtab **ppVTab, char**);
7334 int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
7335 int (*xDisconnect)(sqlite3_vtab *pVTab);
7336 int (*xDestroy)(sqlite3_vtab *pVTab);
7337 int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
7338 int (*xClose)(sqlite3_vtab_cursor*);
7339 int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
7340 int argc, sqlite3_value **argv);
7341 int (*xNext)(sqlite3_vtab_cursor*);
7342 int (*xEof)(sqlite3_vtab_cursor*);
7343 int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
7344 int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
7345 int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
7346 int (*xBegin)(sqlite3_vtab *pVTab);
7347 int (*xSync)(sqlite3_vtab *pVTab);
7348 int (*xCommit)(sqlite3_vtab *pVTab);
7349 int (*xRollback)(sqlite3_vtab *pVTab);
7350 int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
7351 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
7352 void **ppArg);
7353 int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
7354 /* The methods above are in version 1 of the sqlite_module object. Those
7355 ** below are for version 2 and greater. */
7356 int (*xSavepoint)(sqlite3_vtab *pVTab, int);
7357 int (*xRelease)(sqlite3_vtab *pVTab, int);
7358 int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
7359 /* The methods above are in versions 1 and 2 of the sqlite_module object.
7360 ** Those below are for version 3 and greater. */
7361 int (*xShadowName)(const char*);
7362};
7363
7364/*
7365** CAPI3REF: Virtual Table Indexing Information
7366** KEYWORDS: sqlite3_index_info
7367**
7368** The sqlite3_index_info structure and its substructures is used as part
7369** of the [virtual table] interface to
7370** pass information into and receive the reply from the [xBestIndex]
7371** method of a [virtual table module]. The fields under **Inputs** are the
7372** inputs to xBestIndex and are read-only. xBestIndex inserts its
7373** results into the **Outputs** fields.
7374**
7375** ^(The aConstraint[] array records WHERE clause constraints of the form:
7376**
7377** <blockquote>column OP expr</blockquote>
7378**
7379** where OP is =, &lt;, &lt;=, &gt;, or &gt;=.)^ ^(The particular operator is
7380** stored in aConstraint[].op using one of the
7381** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^
7382** ^(The index of the column is stored in
7383** aConstraint[].iColumn.)^ ^(aConstraint[].usable is TRUE if the
7384** expr on the right-hand side can be evaluated (and thus the constraint
7385** is usable) and false if it cannot.)^
7386**
7387** ^The optimizer automatically inverts terms of the form "expr OP column"
7388** and makes other simplifications to the WHERE clause in an attempt to
7389** get as many WHERE clause terms into the form shown above as possible.
7390** ^The aConstraint[] array only reports WHERE clause terms that are
7391** relevant to the particular virtual table being queried.
7392**
7393** ^Information about the ORDER BY clause is stored in aOrderBy[].
7394** ^Each term of aOrderBy records a column of the ORDER BY clause.
7395**
7396** The colUsed field indicates which columns of the virtual table may be
7397** required by the current scan. Virtual table columns are numbered from
7398** zero in the order in which they appear within the CREATE TABLE statement
7399** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62),
7400** the corresponding bit is set within the colUsed mask if the column may be
7401** required by SQLite. If the table has at least 64 columns and any column
7402** to the right of the first 63 is required, then bit 63 of colUsed is also
7403** set. In other words, column iCol may be required if the expression
7404** (colUsed & ((sqlite3_uint64)1 << (iCol>=63 ? 63 : iCol))) evaluates to
7405** non-zero.
7406**
7407** The [xBestIndex] method must fill aConstraintUsage[] with information
7408** about what parameters to pass to xFilter. ^If argvIndex>0 then
7409** the right-hand side of the corresponding aConstraint[] is evaluated
7410** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit
7411** is true, then the constraint is assumed to be fully handled by the
7412** virtual table and is not checked again by SQLite.)^
7413**
7414** ^The idxNum and idxPtr values are recorded and passed into the
7415** [xFilter] method.
7416** ^[sqlite3_free()] is used to free idxPtr if and only if
7417** needToFreeIdxPtr is true.
7418**
7419** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in
7420** the correct order to satisfy the ORDER BY clause so that no separate
7421** sorting step is required.
7422**
7423** ^The estimatedCost value is an estimate of the cost of a particular
7424** strategy. A cost of N indicates that the cost of the strategy is similar
7425** to a linear scan of an SQLite table with N rows. A cost of log(N)
7426** indicates that the expense of the operation is similar to that of a
7427** binary search on a unique indexed field of an SQLite table with N rows.
7428**
7429** ^The estimatedRows value is an estimate of the number of rows that
7430** will be returned by the strategy.
7431**
7432** The xBestIndex method may optionally populate the idxFlags field with a
7433** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag -
7434** SQLITE_INDEX_SCAN_UNIQUE. If the xBestIndex method sets this flag, SQLite
7435** assumes that the strategy may visit at most one row.
7436**
7437** Additionally, if xBestIndex sets the SQLITE_INDEX_SCAN_UNIQUE flag, then
7438** SQLite also assumes that if a call to the xUpdate() method is made as
7439** part of the same statement to delete or update a virtual table row and the
7440** implementation returns SQLITE_CONSTRAINT, then there is no need to rollback
7441** any database changes. In other words, if the xUpdate() returns
7442** SQLITE_CONSTRAINT, the database contents must be exactly as they were
7443** before xUpdate was called. By contrast, if SQLITE_INDEX_SCAN_UNIQUE is not
7444** set and xUpdate returns SQLITE_CONSTRAINT, any database changes made by
7445** the xUpdate method are automatically rolled back by SQLite.
7446**
7447** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info
7448** structure for SQLite [version 3.8.2] ([dateof:3.8.2]).
7449** If a virtual table extension is
7450** used with an SQLite version earlier than 3.8.2, the results of attempting
7451** to read or write the estimatedRows field are undefined (but are likely
7452** to included crashing the application). The estimatedRows field should
7453** therefore only be used if [sqlite3_libversion_number()] returns a
7454** value greater than or equal to 3008002. Similarly, the idxFlags field
7455** was added for [version 3.9.0] ([dateof:3.9.0]).
7456** It may therefore only be used if
7457** sqlite3_libversion_number() returns a value greater than or equal to
7458** 3009000.
7459*/
7460struct sqlite3_index_info {
7461 /* Inputs */
7462 int nConstraint; /* Number of entries in aConstraint */
7463 struct sqlite3_index_constraint {
7464 int iColumn; /* Column constrained. -1 for ROWID */
7465 unsigned char op; /* Constraint operator */
7466 unsigned char usable; /* True if this constraint is usable */
7467 int iTermOffset; /* Used internally - xBestIndex should ignore */
7468 } *aConstraint; /* Table of WHERE clause constraints */
7469 int nOrderBy; /* Number of terms in the ORDER BY clause */
7470 struct sqlite3_index_orderby {
7471 int iColumn; /* Column number */
7472 unsigned char desc; /* True for DESC. False for ASC. */
7473 } *aOrderBy; /* The ORDER BY clause */
7474 /* Outputs */
7475 struct sqlite3_index_constraint_usage {
7476 int argvIndex; /* if >0, constraint is part of argv to xFilter */
7477 unsigned char omit; /* Do not code a test for this constraint */
7478 } *aConstraintUsage;
7479 int idxNum; /* Number used to identify the index */
7480 char *idxStr; /* String, possibly obtained from sqlite3_malloc */
7481 int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */
7482 int orderByConsumed; /* True if output is already ordered */
7483 double estimatedCost; /* Estimated cost of using this index */
7484 /* Fields below are only available in SQLite 3.8.2 and later */
7485 sqlite3_int64 estimatedRows; /* Estimated number of rows returned */
7486 /* Fields below are only available in SQLite 3.9.0 and later */
7487 int idxFlags; /* Mask of SQLITE_INDEX_SCAN_* flags */
7488 /* Fields below are only available in SQLite 3.10.0 and later */
7489 sqlite3_uint64 colUsed; /* Input: Mask of columns used by statement */
7490};
7491
7492/*
7493** CAPI3REF: Virtual Table Scan Flags
7494**
7495** Virtual table implementations are allowed to set the
7496** [sqlite3_index_info].idxFlags field to some combination of
7497** these bits.
7498*/
7499#define SQLITE_INDEX_SCAN_UNIQUE 1 /* Scan visits at most 1 row */
7500
7501/*
7502** CAPI3REF: Virtual Table Constraint Operator Codes
7503**
7504** These macros defined the allowed values for the
7505** [sqlite3_index_info].aConstraint[].op field. Each value represents
7506** an operator that is part of a constraint term in the wHERE clause of
7507** a query that uses a [virtual table].
7508*/
7509#define SQLITE_INDEX_CONSTRAINT_EQ 2
7510#define SQLITE_INDEX_CONSTRAINT_GT 4
7511#define SQLITE_INDEX_CONSTRAINT_LE 8
7512#define SQLITE_INDEX_CONSTRAINT_LT 16
7513#define SQLITE_INDEX_CONSTRAINT_GE 32
7514#define SQLITE_INDEX_CONSTRAINT_MATCH 64
7515#define SQLITE_INDEX_CONSTRAINT_LIKE 65
7516#define SQLITE_INDEX_CONSTRAINT_GLOB 66
7517#define SQLITE_INDEX_CONSTRAINT_REGEXP 67
7518#define SQLITE_INDEX_CONSTRAINT_NE 68
7519#define SQLITE_INDEX_CONSTRAINT_ISNOT 69
7520#define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70
7521#define SQLITE_INDEX_CONSTRAINT_ISNULL 71
7522#define SQLITE_INDEX_CONSTRAINT_IS 72
7523#define SQLITE_INDEX_CONSTRAINT_FUNCTION 150
7524
7525/*
7526** CAPI3REF: Register A Virtual Table Implementation
7527** METHOD: sqlite3
7528**
7529** ^These routines are used to register a new [virtual table module] name.
7530** ^Module names must be registered before
7531** creating a new [virtual table] using the module and before using a
7532** preexisting [virtual table] for the module.
7533**
7534** ^The module name is registered on the [database connection] specified
7535** by the first parameter. ^The name of the module is given by the
7536** second parameter. ^The third parameter is a pointer to
7537** the implementation of the [virtual table module]. ^The fourth
7538** parameter is an arbitrary client data pointer that is passed through
7539** into the [xCreate] and [xConnect] methods of the virtual table module
7540** when a new virtual table is be being created or reinitialized.
7541**
7542** ^The sqlite3_create_module_v2() interface has a fifth parameter which
7543** is a pointer to a destructor for the pClientData. ^SQLite will
7544** invoke the destructor function (if it is not NULL) when SQLite
7545** no longer needs the pClientData pointer. ^The destructor will also
7546** be invoked if the call to sqlite3_create_module_v2() fails.
7547** ^The sqlite3_create_module()
7548** interface is equivalent to sqlite3_create_module_v2() with a NULL
7549** destructor.
7550*/
7551SQLITE_API int sqlite3_create_module(
7552 sqlite3 *db, /* SQLite connection to register module with */
7553 const char *zName, /* Name of the module */
7554 const sqlite3_module *p, /* Methods for the module */
7555 void *pClientData /* Client data for xCreate/xConnect */
7556);
7557SQLITE_API int sqlite3_create_module_v2(
7558 sqlite3 *db, /* SQLite connection to register module with */
7559 const char *zName, /* Name of the module */
7560 const sqlite3_module *p, /* Methods for the module */
7561 void *pClientData, /* Client data for xCreate/xConnect */
7562 void(*xDestroy)(void*) /* Module destructor function */
7563);
7564
7565/*
7566** CAPI3REF: Virtual Table Instance Object
7567** KEYWORDS: sqlite3_vtab
7568**
7569** Every [virtual table module] implementation uses a subclass
7570** of this object to describe a particular instance
7571** of the [virtual table]. Each subclass will
7572** be tailored to the specific needs of the module implementation.
7573** The purpose of this superclass is to define certain fields that are
7574** common to all module implementations.
7575**
7576** ^Virtual tables methods can set an error message by assigning a
7577** string obtained from [sqlite3_mprintf()] to zErrMsg. The method should
7578** take care that any prior string is freed by a call to [sqlite3_free()]
7579** prior to assigning a new string to zErrMsg. ^After the error message
7580** is delivered up to the client application, the string will be automatically
7581** freed by sqlite3_free() and the zErrMsg field will be zeroed.
7582*/
7583struct sqlite3_vtab {
7584 const sqlite3_module *pModule; /* The module for this virtual table */
7585 int nRef; /* Number of open cursors */
7586 char *zErrMsg; /* Error message from sqlite3_mprintf() */
7587 /* Virtual table implementations will typically add additional fields */
7588};
7589
7590/*
7591** CAPI3REF: Virtual Table Cursor Object
7592** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
7593**
7594** Every [virtual table module] implementation uses a subclass of the
7595** following structure to describe cursors that point into the
7596** [virtual table] and are used
7597** to loop through the virtual table. Cursors are created using the
7598** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed
7599** by the [sqlite3_module.xClose | xClose] method. Cursors are used
7600** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods
7601** of the module. Each module implementation will define
7602** the content of a cursor structure to suit its own needs.
7603**
7604** This superclass exists in order to define fields of the cursor that
7605** are common to all implementations.
7606*/
7607struct sqlite3_vtab_cursor {
7608 sqlite3_vtab *pVtab; /* Virtual table of this cursor */
7609 /* Virtual table implementations will typically add additional fields */
7610};
7611
7612/*
7613** CAPI3REF: Declare The Schema Of A Virtual Table
7614**
7615** ^The [xCreate] and [xConnect] methods of a
7616** [virtual table module] call this interface
7617** to declare the format (the names and datatypes of the columns) of
7618** the virtual tables they implement.
7619*/
7620SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
7621
7622/*
7623** CAPI3REF: Overload A Function For A Virtual Table
7624** METHOD: sqlite3
7625**
7626** ^(Virtual tables can provide alternative implementations of functions
7627** using the [xFindFunction] method of the [virtual table module].
7628** But global versions of those functions
7629** must exist in order to be overloaded.)^
7630**
7631** ^(This API makes sure a global version of a function with a particular
7632** name and number of parameters exists. If no such function exists
7633** before this API is called, a new function is created.)^ ^The implementation
7634** of the new function always causes an exception to be thrown. So
7635** the new function is not good for anything by itself. Its only
7636** purpose is to be a placeholder function that can be overloaded
7637** by a [virtual table].
7638*/
7639SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
7640
7641/*
7642** The interface to the virtual-table mechanism defined above (back up
7643** to a comment remarkably similar to this one) is currently considered
7644** to be experimental. The interface might change in incompatible ways.
7645** If this is a problem for you, do not use the interface at this time.
7646**
7647** When the virtual-table mechanism stabilizes, we will declare the
7648** interface fixed, support it indefinitely, and remove this comment.
7649*/
7650
7651/*
7652** CAPI3REF: A Handle To An Open BLOB
7653** KEYWORDS: {BLOB handle} {BLOB handles}
7654**
7655** An instance of this object represents an open BLOB on which
7656** [sqlite3_blob_open | incremental BLOB I/O] can be performed.
7657** ^Objects of this type are created by [sqlite3_blob_open()]
7658** and destroyed by [sqlite3_blob_close()].
7659** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
7660** can be used to read or write small subsections of the BLOB.
7661** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
7662*/
7663typedef struct sqlite3_blob sqlite3_blob;
7664
7665/*
7666** CAPI3REF: Open A BLOB For Incremental I/O
7667** METHOD: sqlite3
7668** CONSTRUCTOR: sqlite3_blob
7669**
7670** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
7671** in row iRow, column zColumn, table zTable in database zDb;
7672** in other words, the same BLOB that would be selected by:
7673**
7674** <pre>
7675** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
7676** </pre>)^
7677**
7678** ^(Parameter zDb is not the filename that contains the database, but
7679** rather the symbolic name of the database. For attached databases, this is
7680** the name that appears after the AS keyword in the [ATTACH] statement.
7681** For the main database file, the database name is "main". For TEMP
7682** tables, the database name is "temp".)^
7683**
7684** ^If the flags parameter is non-zero, then the BLOB is opened for read
7685** and write access. ^If the flags parameter is zero, the BLOB is opened for
7686** read-only access.
7687**
7688** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is stored
7689** in *ppBlob. Otherwise an [error code] is returned and, unless the error
7690** code is SQLITE_MISUSE, *ppBlob is set to NULL.)^ ^This means that, provided
7691** the API is not misused, it is always safe to call [sqlite3_blob_close()]
7692** on *ppBlob after this function it returns.
7693**
7694** This function fails with SQLITE_ERROR if any of the following are true:
7695** <ul>
7696** <li> ^(Database zDb does not exist)^,
7697** <li> ^(Table zTable does not exist within database zDb)^,
7698** <li> ^(Table zTable is a WITHOUT ROWID table)^,
7699** <li> ^(Column zColumn does not exist)^,
7700** <li> ^(Row iRow is not present in the table)^,
7701** <li> ^(The specified column of row iRow contains a value that is not
7702** a TEXT or BLOB value)^,
7703** <li> ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE
7704** constraint and the blob is being opened for read/write access)^,
7705** <li> ^([foreign key constraints | Foreign key constraints] are enabled,
7706** column zColumn is part of a [child key] definition and the blob is
7707** being opened for read/write access)^.
7708** </ul>
7709**
7710** ^Unless it returns SQLITE_MISUSE, this function sets the
7711** [database connection] error code and message accessible via
7712** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
7713**
7714** A BLOB referenced by sqlite3_blob_open() may be read using the
7715** [sqlite3_blob_read()] interface and modified by using
7716** [sqlite3_blob_write()]. The [BLOB handle] can be moved to a
7717** different row of the same table using the [sqlite3_blob_reopen()]
7718** interface. However, the column, table, or database of a [BLOB handle]
7719** cannot be changed after the [BLOB handle] is opened.
7720**
7721** ^(If the row that a BLOB handle points to is modified by an
7722** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
7723** then the BLOB handle is marked as "expired".
7724** This is true if any column of the row is changed, even a column
7725** other than the one the BLOB handle is open on.)^
7726** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
7727** an expired BLOB handle fail with a return code of [SQLITE_ABORT].
7728** ^(Changes written into a BLOB prior to the BLOB expiring are not
7729** rolled back by the expiration of the BLOB. Such changes will eventually
7730** commit if the transaction continues to completion.)^
7731**
7732** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
7733** the opened blob. ^The size of a blob may not be changed by this
7734** interface. Use the [UPDATE] SQL command to change the size of a
7735** blob.
7736**
7737** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
7738** and the built-in [zeroblob] SQL function may be used to create a
7739** zero-filled blob to read or write using the incremental-blob interface.
7740**
7741** To avoid a resource leak, every open [BLOB handle] should eventually
7742** be released by a call to [sqlite3_blob_close()].
7743**
7744** See also: [sqlite3_blob_close()],
7745** [sqlite3_blob_reopen()], [sqlite3_blob_read()],
7746** [sqlite3_blob_bytes()], [sqlite3_blob_write()].
7747*/
7748SQLITE_API int sqlite3_blob_open(
7749 sqlite3*,
7750 const char *zDb,
7751 const char *zTable,
7752 const char *zColumn,
7753 sqlite3_int64 iRow,
7754 int flags,
7755 sqlite3_blob **ppBlob
7756);
7757
7758/*
7759** CAPI3REF: Move a BLOB Handle to a New Row
7760** METHOD: sqlite3_blob
7761**
7762** ^This function is used to move an existing [BLOB handle] so that it points
7763** to a different row of the same database table. ^The new row is identified
7764** by the rowid value passed as the second argument. Only the row can be
7765** changed. ^The database, table and column on which the blob handle is open
7766** remain the same. Moving an existing [BLOB handle] to a new row is
7767** faster than closing the existing handle and opening a new one.
7768**
7769** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
7770** it must exist and there must be either a blob or text value stored in
7771** the nominated column.)^ ^If the new row is not present in the table, or if
7772** it does not contain a blob or text value, or if another error occurs, an
7773** SQLite error code is returned and the blob handle is considered aborted.
7774** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or
7775** [sqlite3_blob_reopen()] on an aborted blob handle immediately return
7776** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
7777** always returns zero.
7778**
7779** ^This function sets the database handle error code and message.
7780*/
7781SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
7782
7783/*
7784** CAPI3REF: Close A BLOB Handle
7785** DESTRUCTOR: sqlite3_blob
7786**
7787** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed
7788** unconditionally. Even if this routine returns an error code, the
7789** handle is still closed.)^
7790**
7791** ^If the blob handle being closed was opened for read-write access, and if
7792** the database is in auto-commit mode and there are no other open read-write
7793** blob handles or active write statements, the current transaction is
7794** committed. ^If an error occurs while committing the transaction, an error
7795** code is returned and the transaction rolled back.
7796**
7797** Calling this function with an argument that is not a NULL pointer or an
7798** open blob handle results in undefined behaviour. ^Calling this routine
7799** with a null pointer (such as would be returned by a failed call to
7800** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
7801** is passed a valid open blob handle, the values returned by the
7802** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning.
7803*/
7804SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
7805
7806/*
7807** CAPI3REF: Return The Size Of An Open BLOB
7808** METHOD: sqlite3_blob
7809**
7810** ^Returns the size in bytes of the BLOB accessible via the
7811** successfully opened [BLOB handle] in its only argument. ^The
7812** incremental blob I/O routines can only read or overwriting existing
7813** blob content; they cannot change the size of a blob.
7814**
7815** This routine only works on a [BLOB handle] which has been created
7816** by a prior successful call to [sqlite3_blob_open()] and which has not
7817** been closed by [sqlite3_blob_close()]. Passing any other pointer in
7818** to this routine results in undefined and probably undesirable behavior.
7819*/
7820SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
7821
7822/*
7823** CAPI3REF: Read Data From A BLOB Incrementally
7824** METHOD: sqlite3_blob
7825**
7826** ^(This function is used to read data from an open [BLOB handle] into a
7827** caller-supplied buffer. N bytes of data are copied into buffer Z
7828** from the open BLOB, starting at offset iOffset.)^
7829**
7830** ^If offset iOffset is less than N bytes from the end of the BLOB,
7831** [SQLITE_ERROR] is returned and no data is read. ^If N or iOffset is
7832** less than zero, [SQLITE_ERROR] is returned and no data is read.
7833** ^The size of the blob (and hence the maximum value of N+iOffset)
7834** can be determined using the [sqlite3_blob_bytes()] interface.
7835**
7836** ^An attempt to read from an expired [BLOB handle] fails with an
7837** error code of [SQLITE_ABORT].
7838**
7839** ^(On success, sqlite3_blob_read() returns SQLITE_OK.
7840** Otherwise, an [error code] or an [extended error code] is returned.)^
7841**
7842** This routine only works on a [BLOB handle] which has been created
7843** by a prior successful call to [sqlite3_blob_open()] and which has not
7844** been closed by [sqlite3_blob_close()]. Passing any other pointer in
7845** to this routine results in undefined and probably undesirable behavior.
7846**
7847** See also: [sqlite3_blob_write()].
7848*/
7849SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
7850
7851/*
7852** CAPI3REF: Write Data Into A BLOB Incrementally
7853** METHOD: sqlite3_blob
7854**
7855** ^(This function is used to write data into an open [BLOB handle] from a
7856** caller-supplied buffer. N bytes of data are copied from the buffer Z
7857** into the open BLOB, starting at offset iOffset.)^
7858**
7859** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
7860** Otherwise, an [error code] or an [extended error code] is returned.)^
7861** ^Unless SQLITE_MISUSE is returned, this function sets the
7862** [database connection] error code and message accessible via
7863** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
7864**
7865** ^If the [BLOB handle] passed as the first argument was not opened for
7866** writing (the flags parameter to [sqlite3_blob_open()] was zero),
7867** this function returns [SQLITE_READONLY].
7868**
7869** This function may only modify the contents of the BLOB; it is
7870** not possible to increase the size of a BLOB using this API.
7871** ^If offset iOffset is less than N bytes from the end of the BLOB,
7872** [SQLITE_ERROR] is returned and no data is written. The size of the
7873** BLOB (and hence the maximum value of N+iOffset) can be determined
7874** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less
7875** than zero [SQLITE_ERROR] is returned and no data is written.
7876**
7877** ^An attempt to write to an expired [BLOB handle] fails with an
7878** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred
7879** before the [BLOB handle] expired are not rolled back by the
7880** expiration of the handle, though of course those changes might
7881** have been overwritten by the statement that expired the BLOB handle
7882** or by other independent statements.
7883**
7884** This routine only works on a [BLOB handle] which has been created
7885** by a prior successful call to [sqlite3_blob_open()] and which has not
7886** been closed by [sqlite3_blob_close()]. Passing any other pointer in
7887** to this routine results in undefined and probably undesirable behavior.
7888**
7889** See also: [sqlite3_blob_read()].
7890*/
7891SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
7892
7893/*
7894** CAPI3REF: Virtual File System Objects
7895**
7896** A virtual filesystem (VFS) is an [sqlite3_vfs] object
7897** that SQLite uses to interact
7898** with the underlying operating system. Most SQLite builds come with a
7899** single default VFS that is appropriate for the host computer.
7900** New VFSes can be registered and existing VFSes can be unregistered.
7901** The following interfaces are provided.
7902**
7903** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
7904** ^Names are case sensitive.
7905** ^Names are zero-terminated UTF-8 strings.
7906** ^If there is no match, a NULL pointer is returned.
7907** ^If zVfsName is NULL then the default VFS is returned.
7908**
7909** ^New VFSes are registered with sqlite3_vfs_register().
7910** ^Each new VFS becomes the default VFS if the makeDflt flag is set.
7911** ^The same VFS can be registered multiple times without injury.
7912** ^To make an existing VFS into the default VFS, register it again
7913** with the makeDflt flag set. If two different VFSes with the
7914** same name are registered, the behavior is undefined. If a
7915** VFS is registered with a name that is NULL or an empty string,
7916** then the behavior is undefined.
7917**
7918** ^Unregister a VFS with the sqlite3_vfs_unregister() interface.
7919** ^(If the default VFS is unregistered, another VFS is chosen as
7920** the default. The choice for the new VFS is arbitrary.)^
7921*/
7922SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
7923SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
7924SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
7925
7926/*
7927** CAPI3REF: Mutexes
7928**
7929** The SQLite core uses these routines for thread
7930** synchronization. Though they are intended for internal
7931** use by SQLite, code that links against SQLite is
7932** permitted to use any of these routines.
7933**
7934** The SQLite source code contains multiple implementations
7935** of these mutex routines. An appropriate implementation
7936** is selected automatically at compile-time. The following
7937** implementations are available in the SQLite core:
7938**
7939** <ul>
7940** <li> SQLITE_MUTEX_PTHREADS
7941** <li> SQLITE_MUTEX_W32
7942** <li> SQLITE_MUTEX_NOOP
7943** </ul>
7944**
7945** The SQLITE_MUTEX_NOOP implementation is a set of routines
7946** that does no real locking and is appropriate for use in
7947** a single-threaded application. The SQLITE_MUTEX_PTHREADS and
7948** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix
7949** and Windows.
7950**
7951** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
7952** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
7953** implementation is included with the library. In this case the
7954** application must supply a custom mutex implementation using the
7955** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
7956** before calling sqlite3_initialize() or any other public sqlite3_
7957** function that calls sqlite3_initialize().
7958**
7959** ^The sqlite3_mutex_alloc() routine allocates a new
7960** mutex and returns a pointer to it. ^The sqlite3_mutex_alloc()
7961** routine returns NULL if it is unable to allocate the requested
7962** mutex. The argument to sqlite3_mutex_alloc() must one of these
7963** integer constants:
7964**
7965** <ul>
7966** <li> SQLITE_MUTEX_FAST
7967** <li> SQLITE_MUTEX_RECURSIVE
7968** <li> SQLITE_MUTEX_STATIC_MASTER
7969** <li> SQLITE_MUTEX_STATIC_MEM
7970** <li> SQLITE_MUTEX_STATIC_OPEN
7971** <li> SQLITE_MUTEX_STATIC_PRNG
7972** <li> SQLITE_MUTEX_STATIC_LRU
7973** <li> SQLITE_MUTEX_STATIC_PMEM
7974** <li> SQLITE_MUTEX_STATIC_APP1
7975** <li> SQLITE_MUTEX_STATIC_APP2
7976** <li> SQLITE_MUTEX_STATIC_APP3
7977** <li> SQLITE_MUTEX_STATIC_VFS1
7978** <li> SQLITE_MUTEX_STATIC_VFS2
7979** <li> SQLITE_MUTEX_STATIC_VFS3
7980** </ul>
7981**
7982** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
7983** cause sqlite3_mutex_alloc() to create
7984** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
7985** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
7986** The mutex implementation does not need to make a distinction
7987** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
7988** not want to. SQLite will only request a recursive mutex in
7989** cases where it really needs one. If a faster non-recursive mutex
7990** implementation is available on the host platform, the mutex subsystem
7991** might return such a mutex in response to SQLITE_MUTEX_FAST.
7992**
7993** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other
7994** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return
7995** a pointer to a static preexisting mutex. ^Nine static mutexes are
7996** used by the current version of SQLite. Future versions of SQLite
7997** may add additional static mutexes. Static mutexes are for internal
7998** use by SQLite only. Applications that use SQLite mutexes should
7999** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
8000** SQLITE_MUTEX_RECURSIVE.
8001**
8002** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
8003** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
8004** returns a different mutex on every call. ^For the static
8005** mutex types, the same mutex is returned on every call that has
8006** the same type number.
8007**
8008** ^The sqlite3_mutex_free() routine deallocates a previously
8009** allocated dynamic mutex. Attempting to deallocate a static
8010** mutex results in undefined behavior.
8011**
8012** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
8013** to enter a mutex. ^If another thread is already within the mutex,
8014** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
8015** SQLITE_BUSY. ^The sqlite3_mutex_try() interface returns [SQLITE_OK]
8016** upon successful entry. ^(Mutexes created using
8017** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread.
8018** In such cases, the
8019** mutex must be exited an equal number of times before another thread
8020** can enter.)^ If the same thread tries to enter any mutex other
8021** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined.
8022**
8023** ^(Some systems (for example, Windows 95) do not support the operation
8024** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try()
8025** will always return SQLITE_BUSY. The SQLite core only ever uses
8026** sqlite3_mutex_try() as an optimization so this is acceptable
8027** behavior.)^
8028**
8029** ^The sqlite3_mutex_leave() routine exits a mutex that was
8030** previously entered by the same thread. The behavior
8031** is undefined if the mutex is not currently entered by the
8032** calling thread or is not currently allocated.
8033**
8034** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or
8035** sqlite3_mutex_leave() is a NULL pointer, then all three routines
8036** behave as no-ops.
8037**
8038** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
8039*/
8040SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
8041SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
8042SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
8043SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
8044SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
8045
8046/*
8047** CAPI3REF: Mutex Methods Object
8048**
8049** An instance of this structure defines the low-level routines
8050** used to allocate and use mutexes.
8051**
8052** Usually, the default mutex implementations provided by SQLite are
8053** sufficient, however the application has the option of substituting a custom
8054** implementation for specialized deployments or systems for which SQLite
8055** does not provide a suitable implementation. In this case, the application
8056** creates and populates an instance of this structure to pass
8057** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option.
8058** Additionally, an instance of this structure can be used as an
8059** output variable when querying the system for the current mutex
8060** implementation, using the [SQLITE_CONFIG_GETMUTEX] option.
8061**
8062** ^The xMutexInit method defined by this structure is invoked as
8063** part of system initialization by the sqlite3_initialize() function.
8064** ^The xMutexInit routine is called by SQLite exactly once for each
8065** effective call to [sqlite3_initialize()].
8066**
8067** ^The xMutexEnd method defined by this structure is invoked as
8068** part of system shutdown by the sqlite3_shutdown() function. The
8069** implementation of this method is expected to release all outstanding
8070** resources obtained by the mutex methods implementation, especially
8071** those obtained by the xMutexInit method. ^The xMutexEnd()
8072** interface is invoked exactly once for each call to [sqlite3_shutdown()].
8073**
8074** ^(The remaining seven methods defined by this structure (xMutexAlloc,
8075** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
8076** xMutexNotheld) implement the following interfaces (respectively):
8077**
8078** <ul>
8079** <li> [sqlite3_mutex_alloc()] </li>
8080** <li> [sqlite3_mutex_free()] </li>
8081** <li> [sqlite3_mutex_enter()] </li>
8082** <li> [sqlite3_mutex_try()] </li>
8083** <li> [sqlite3_mutex_leave()] </li>
8084** <li> [sqlite3_mutex_held()] </li>
8085** <li> [sqlite3_mutex_notheld()] </li>
8086** </ul>)^
8087**
8088** The only difference is that the public sqlite3_XXX functions enumerated
8089** above silently ignore any invocations that pass a NULL pointer instead
8090** of a valid mutex handle. The implementations of the methods defined
8091** by this structure are not required to handle this case, the results
8092** of passing a NULL pointer instead of a valid mutex handle are undefined
8093** (i.e. it is acceptable to provide an implementation that segfaults if
8094** it is passed a NULL pointer).
8095**
8096** The xMutexInit() method must be threadsafe. It must be harmless to
8097** invoke xMutexInit() multiple times within the same process and without
8098** intervening calls to xMutexEnd(). Second and subsequent calls to
8099** xMutexInit() must be no-ops.
8100**
8101** xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
8102** and its associates). Similarly, xMutexAlloc() must not use SQLite memory
8103** allocation for a static mutex. ^However xMutexAlloc() may use SQLite
8104** memory allocation for a fast or recursive mutex.
8105**
8106** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
8107** called, but only if the prior call to xMutexInit returned SQLITE_OK.
8108** If xMutexInit fails in any way, it is expected to clean up after itself
8109** prior to returning.
8110*/
8111typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
8112struct sqlite3_mutex_methods {
8113 int (*xMutexInit)(void);
8114 int (*xMutexEnd)(void);
8115 sqlite3_mutex *(*xMutexAlloc)(int);
8116 void (*xMutexFree)(sqlite3_mutex *);
8117 void (*xMutexEnter)(sqlite3_mutex *);
8118 int (*xMutexTry)(sqlite3_mutex *);
8119 void (*xMutexLeave)(sqlite3_mutex *);
8120 int (*xMutexHeld)(sqlite3_mutex *);
8121 int (*xMutexNotheld)(sqlite3_mutex *);
8122};
8123
8124/*
8125** CAPI3REF: Mutex Verification Routines
8126**
8127** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
8128** are intended for use inside assert() statements. The SQLite core
8129** never uses these routines except inside an assert() and applications
8130** are advised to follow the lead of the core. The SQLite core only
8131** provides implementations for these routines when it is compiled
8132** with the SQLITE_DEBUG flag. External mutex implementations
8133** are only required to provide these routines if SQLITE_DEBUG is
8134** defined and if NDEBUG is not defined.
8135**
8136** These routines should return true if the mutex in their argument
8137** is held or not held, respectively, by the calling thread.
8138**
8139** The implementation is not required to provide versions of these
8140** routines that actually work. If the implementation does not provide working
8141** versions of these routines, it should at least provide stubs that always
8142** return true so that one does not get spurious assertion failures.
8143**
8144** If the argument to sqlite3_mutex_held() is a NULL pointer then
8145** the routine should return 1. This seems counter-intuitive since
8146** clearly the mutex cannot be held if it does not exist. But
8147** the reason the mutex does not exist is because the build is not
8148** using mutexes. And we do not want the assert() containing the
8149** call to sqlite3_mutex_held() to fail, so a non-zero return is
8150** the appropriate thing to do. The sqlite3_mutex_notheld()
8151** interface should also return 1 when given a NULL pointer.
8152*/
8153#ifndef NDEBUG
8154SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
8155SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
8156#endif
8157
8158/*
8159** CAPI3REF: Mutex Types
8160**
8161** The [sqlite3_mutex_alloc()] interface takes a single argument
8162** which is one of these integer constants.
8163**
8164** The set of static mutexes may change from one SQLite release to the
8165** next. Applications that override the built-in mutex logic must be
8166** prepared to accommodate additional static mutexes.
8167*/
8168#define SQLITE_MUTEX_FAST 0
8169#define SQLITE_MUTEX_RECURSIVE 1
8170#define SQLITE_MUTEX_STATIC_MASTER 2
8171#define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */
8172#define SQLITE_MUTEX_STATIC_MEM2 4 /* NOT USED */
8173#define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */
8174#define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_randomness() */
8175#define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */
8176#define SQLITE_MUTEX_STATIC_LRU2 7 /* NOT USED */
8177#define SQLITE_MUTEX_STATIC_PMEM 7 /* sqlite3PageMalloc() */
8178#define SQLITE_MUTEX_STATIC_APP1 8 /* For use by application */
8179#define SQLITE_MUTEX_STATIC_APP2 9 /* For use by application */
8180#define SQLITE_MUTEX_STATIC_APP3 10 /* For use by application */
8181#define SQLITE_MUTEX_STATIC_VFS1 11 /* For use by built-in VFS */
8182#define SQLITE_MUTEX_STATIC_VFS2 12 /* For use by extension VFS */
8183#define SQLITE_MUTEX_STATIC_VFS3 13 /* For use by application VFS */
8184
8185/*
8186** CAPI3REF: Retrieve the mutex for a database connection
8187** METHOD: sqlite3
8188**
8189** ^This interface returns a pointer the [sqlite3_mutex] object that
8190** serializes access to the [database connection] given in the argument
8191** when the [threading mode] is Serialized.
8192** ^If the [threading mode] is Single-thread or Multi-thread then this
8193** routine returns a NULL pointer.
8194*/
8195SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
8196
8197/*
8198** CAPI3REF: Low-Level Control Of Database Files
8199** METHOD: sqlite3
8200** KEYWORDS: {file control}
8201**
8202** ^The [sqlite3_file_control()] interface makes a direct call to the
8203** xFileControl method for the [sqlite3_io_methods] object associated
8204** with a particular database identified by the second argument. ^The
8205** name of the database is "main" for the main database or "temp" for the
8206** TEMP database, or the name that appears after the AS keyword for
8207** databases that are added using the [ATTACH] SQL command.
8208** ^A NULL pointer can be used in place of "main" to refer to the
8209** main database file.
8210** ^The third and fourth parameters to this routine
8211** are passed directly through to the second and third parameters of
8212** the xFileControl method. ^The return value of the xFileControl
8213** method becomes the return value of this routine.
8214**
8215** A few opcodes for [sqlite3_file_control()] are handled directly
8216** by the SQLite core and never invoke the
8217** sqlite3_io_methods.xFileControl method.
8218** ^The [SQLITE_FCNTL_FILE_POINTER] value for the op parameter causes
8219** a pointer to the underlying [sqlite3_file] object to be written into
8220** the space pointed to by the 4th parameter. The
8221** [SQLITE_FCNTL_JOURNAL_POINTER] works similarly except that it returns
8222** the [sqlite3_file] object associated with the journal file instead of
8223** the main database. The [SQLITE_FCNTL_VFS_POINTER] opcode returns
8224** a pointer to the underlying [sqlite3_vfs] object for the file.
8225** The [SQLITE_FCNTL_DATA_VERSION] returns the data version counter
8226** from the pager.
8227**
8228** ^If the second parameter (zDbName) does not match the name of any
8229** open database file, then SQLITE_ERROR is returned. ^This error
8230** code is not remembered and will not be recalled by [sqlite3_errcode()]
8231** or [sqlite3_errmsg()]. The underlying xFileControl method might
8232** also return SQLITE_ERROR. There is no way to distinguish between
8233** an incorrect zDbName and an SQLITE_ERROR return from the underlying
8234** xFileControl method.
8235**
8236** See also: [file control opcodes]
8237*/
8238SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
8239
8240/*
8241** CAPI3REF: Testing Interface
8242**
8243** ^The sqlite3_test_control() interface is used to read out internal
8244** state of SQLite and to inject faults into SQLite for testing
8245** purposes. ^The first parameter is an operation code that determines
8246** the number, meaning, and operation of all subsequent parameters.
8247**
8248** This interface is not for use by applications. It exists solely
8249** for verifying the correct operation of the SQLite library. Depending
8250** on how the SQLite library is compiled, this interface might not exist.
8251**
8252** The details of the operation codes, their meanings, the parameters
8253** they take, and what they do are all subject to change without notice.
8254** Unlike most of the SQLite API, this function is not guaranteed to
8255** operate consistently from one release to the next.
8256*/
8257SQLITE_API int sqlite3_test_control(int op, ...);
8258
8259/*
8260** CAPI3REF: Testing Interface Operation Codes
8261**
8262** These constants are the valid operation code parameters used
8263** as the first argument to [sqlite3_test_control()].
8264**
8265** These parameters and their meanings are subject to change
8266** without notice. These values are for testing purposes only.
8267** Applications should not use any of these parameters or the
8268** [sqlite3_test_control()] interface.
8269*/
8270#define SQLITE_TESTCTRL_FIRST 5
8271#define SQLITE_TESTCTRL_PRNG_SAVE 5
8272#define SQLITE_TESTCTRL_PRNG_RESTORE 6
8273#define SQLITE_TESTCTRL_PRNG_RESET 7
8274#define SQLITE_TESTCTRL_BITVEC_TEST 8
8275#define SQLITE_TESTCTRL_FAULT_INSTALL 9
8276#define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS 10
8277#define SQLITE_TESTCTRL_PENDING_BYTE 11
8278#define SQLITE_TESTCTRL_ASSERT 12
8279#define SQLITE_TESTCTRL_ALWAYS 13
8280#define SQLITE_TESTCTRL_RESERVE 14
8281#define SQLITE_TESTCTRL_OPTIMIZATIONS 15
8282#define SQLITE_TESTCTRL_ISKEYWORD 16 /* NOT USED */
8283#define SQLITE_TESTCTRL_SCRATCHMALLOC 17 /* NOT USED */
8284#define SQLITE_TESTCTRL_INTERNAL_FUNCTIONS 17
8285#define SQLITE_TESTCTRL_LOCALTIME_FAULT 18
8286#define SQLITE_TESTCTRL_EXPLAIN_STMT 19 /* NOT USED */
8287#define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD 19
8288#define SQLITE_TESTCTRL_NEVER_CORRUPT 20
8289#define SQLITE_TESTCTRL_VDBE_COVERAGE 21
8290#define SQLITE_TESTCTRL_BYTEORDER 22
8291#define SQLITE_TESTCTRL_ISINIT 23
8292#define SQLITE_TESTCTRL_SORTER_MMAP 24
8293#define SQLITE_TESTCTRL_IMPOSTER 25
8294#define SQLITE_TESTCTRL_PARSER_COVERAGE 26
8295#define SQLITE_TESTCTRL_LAST 26 /* Largest TESTCTRL */
8296
8297/*
8298** CAPI3REF: SQL Keyword Checking
8299**
8300** These routines provide access to the set of SQL language keywords
8301** recognized by SQLite. Applications can uses these routines to determine
8302** whether or not a specific identifier needs to be escaped (for example,
8303** by enclosing in double-quotes) so as not to confuse the parser.
8304**
8305** The sqlite3_keyword_count() interface returns the number of distinct
8306** keywords understood by SQLite.
8307**
8308** The sqlite3_keyword_name(N,Z,L) interface finds the N-th keyword and
8309** makes *Z point to that keyword expressed as UTF8 and writes the number
8310** of bytes in the keyword into *L. The string that *Z points to is not
8311** zero-terminated. The sqlite3_keyword_name(N,Z,L) routine returns
8312** SQLITE_OK if N is within bounds and SQLITE_ERROR if not. If either Z
8313** or L are NULL or invalid pointers then calls to
8314** sqlite3_keyword_name(N,Z,L) result in undefined behavior.
8315**
8316** The sqlite3_keyword_check(Z,L) interface checks to see whether or not
8317** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero
8318** if it is and zero if not.
8319**
8320** The parser used by SQLite is forgiving. It is often possible to use
8321** a keyword as an identifier as long as such use does not result in a
8322** parsing ambiguity. For example, the statement
8323** "CREATE TABLE BEGIN(REPLACE,PRAGMA,END);" is accepted by SQLite, and
8324** creates a new table named "BEGIN" with three columns named
8325** "REPLACE", "PRAGMA", and "END". Nevertheless, best practice is to avoid
8326** using keywords as identifiers. Common techniques used to avoid keyword
8327** name collisions include:
8328** <ul>
8329** <li> Put all identifier names inside double-quotes. This is the official
8330** SQL way to escape identifier names.
8331** <li> Put identifier names inside &#91;...&#93;. This is not standard SQL,
8332** but it is what SQL Server does and so lots of programmers use this
8333** technique.
8334** <li> Begin every identifier with the letter "Z" as no SQL keywords start
8335** with "Z".
8336** <li> Include a digit somewhere in every identifier name.
8337** </ul>
8338**
8339** Note that the number of keywords understood by SQLite can depend on
8340** compile-time options. For example, "VACUUM" is not a keyword if
8341** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option. Also,
8342** new keywords may be added to future releases of SQLite.
8343*/
8344SQLITE_API int sqlite3_keyword_count(void);
8345SQLITE_API int sqlite3_keyword_name(int,const char**,int*);
8346SQLITE_API int sqlite3_keyword_check(const char*,int);
8347
8348/*
8349** CAPI3REF: Dynamic String Object
8350** KEYWORDS: {dynamic string}
8351**
8352** An instance of the sqlite3_str object contains a dynamically-sized
8353** string under construction.
8354**
8355** The lifecycle of an sqlite3_str object is as follows:
8356** <ol>
8357** <li> ^The sqlite3_str object is created using [sqlite3_str_new()].
8358** <li> ^Text is appended to the sqlite3_str object using various
8359** methods, such as [sqlite3_str_appendf()].
8360** <li> ^The sqlite3_str object is destroyed and the string it created
8361** is returned using the [sqlite3_str_finish()] interface.
8362** </ol>
8363*/
8364typedef struct sqlite3_str sqlite3_str;
8365
8366/*
8367** CAPI3REF: Create A New Dynamic String Object
8368** CONSTRUCTOR: sqlite3_str
8369**
8370** ^The [sqlite3_str_new(D)] interface allocates and initializes
8371** a new [sqlite3_str] object. To avoid memory leaks, the object returned by
8372** [sqlite3_str_new()] must be freed by a subsequent call to
8373** [sqlite3_str_finish(X)].
8374**
8375** ^The [sqlite3_str_new(D)] interface always returns a pointer to a
8376** valid [sqlite3_str] object, though in the event of an out-of-memory
8377** error the returned object might be a special singleton that will
8378** silently reject new text, always return SQLITE_NOMEM from
8379** [sqlite3_str_errcode()], always return 0 for
8380** [sqlite3_str_length()], and always return NULL from
8381** [sqlite3_str_finish(X)]. It is always safe to use the value
8382** returned by [sqlite3_str_new(D)] as the sqlite3_str parameter
8383** to any of the other [sqlite3_str] methods.
8384**
8385** The D parameter to [sqlite3_str_new(D)] may be NULL. If the
8386** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum
8387** length of the string contained in the [sqlite3_str] object will be
8388** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead
8389** of [SQLITE_MAX_LENGTH].
8390*/
8391SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3*);
8392
8393/*
8394** CAPI3REF: Finalize A Dynamic String
8395** DESTRUCTOR: sqlite3_str
8396**
8397** ^The [sqlite3_str_finish(X)] interface destroys the sqlite3_str object X
8398** and returns a pointer to a memory buffer obtained from [sqlite3_malloc64()]
8399** that contains the constructed string. The calling application should
8400** pass the returned value to [sqlite3_free()] to avoid a memory leak.
8401** ^The [sqlite3_str_finish(X)] interface may return a NULL pointer if any
8402** errors were encountered during construction of the string. ^The
8403** [sqlite3_str_finish(X)] interface will also return a NULL pointer if the
8404** string in [sqlite3_str] object X is zero bytes long.
8405*/
8406SQLITE_API char *sqlite3_str_finish(sqlite3_str*);
8407
8408/*
8409** CAPI3REF: Add Content To A Dynamic String
8410** METHOD: sqlite3_str
8411**
8412** These interfaces add content to an sqlite3_str object previously obtained
8413** from [sqlite3_str_new()].
8414**
8415** ^The [sqlite3_str_appendf(X,F,...)] and
8416** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf]
8417** functionality of SQLite to append formatted text onto the end of
8418** [sqlite3_str] object X.
8419**
8420** ^The [sqlite3_str_append(X,S,N)] method appends exactly N bytes from string S
8421** onto the end of the [sqlite3_str] object X. N must be non-negative.
8422** S must contain at least N non-zero bytes of content. To append a
8423** zero-terminated string in its entirety, use the [sqlite3_str_appendall()]
8424** method instead.
8425**
8426** ^The [sqlite3_str_appendall(X,S)] method appends the complete content of
8427** zero-terminated string S onto the end of [sqlite3_str] object X.
8428**
8429** ^The [sqlite3_str_appendchar(X,N,C)] method appends N copies of the
8430** single-byte character C onto the end of [sqlite3_str] object X.
8431** ^This method can be used, for example, to add whitespace indentation.
8432**
8433** ^The [sqlite3_str_reset(X)] method resets the string under construction
8434** inside [sqlite3_str] object X back to zero bytes in length.
8435**
8436** These methods do not return a result code. ^If an error occurs, that fact
8437** is recorded in the [sqlite3_str] object and can be recovered by a
8438** subsequent call to [sqlite3_str_errcode(X)].
8439*/
8440SQLITE_API void sqlite3_str_appendf(sqlite3_str*, const char *zFormat, ...);
8441SQLITE_API void sqlite3_str_vappendf(sqlite3_str*, const char *zFormat, va_list);
8442SQLITE_API void sqlite3_str_append(sqlite3_str*, const char *zIn, int N);
8443SQLITE_API void sqlite3_str_appendall(sqlite3_str*, const char *zIn);
8444SQLITE_API void sqlite3_str_appendchar(sqlite3_str*, int N, char C);
8445SQLITE_API void sqlite3_str_reset(sqlite3_str*);
8446
8447/*
8448** CAPI3REF: Status Of A Dynamic String
8449** METHOD: sqlite3_str
8450**
8451** These interfaces return the current status of an [sqlite3_str] object.
8452**
8453** ^If any prior errors have occurred while constructing the dynamic string
8454** in sqlite3_str X, then the [sqlite3_str_errcode(X)] method will return
8455** an appropriate error code. ^The [sqlite3_str_errcode(X)] method returns
8456** [SQLITE_NOMEM] following any out-of-memory error, or
8457** [SQLITE_TOOBIG] if the size of the dynamic string exceeds
8458** [SQLITE_MAX_LENGTH], or [SQLITE_OK] if there have been no errors.
8459**
8460** ^The [sqlite3_str_length(X)] method returns the current length, in bytes,
8461** of the dynamic string under construction in [sqlite3_str] object X.
8462** ^The length returned by [sqlite3_str_length(X)] does not include the
8463** zero-termination byte.
8464**
8465** ^The [sqlite3_str_value(X)] method returns a pointer to the current
8466** content of the dynamic string under construction in X. The value
8467** returned by [sqlite3_str_value(X)] is managed by the sqlite3_str object X
8468** and might be freed or altered by any subsequent method on the same
8469** [sqlite3_str] object. Applications must not used the pointer returned
8470** [sqlite3_str_value(X)] after any subsequent method call on the same
8471** object. ^Applications may change the content of the string returned
8472** by [sqlite3_str_value(X)] as long as they do not write into any bytes
8473** outside the range of 0 to [sqlite3_str_length(X)] and do not read or
8474** write any byte after any subsequent sqlite3_str method call.
8475*/
8476SQLITE_API int sqlite3_str_errcode(sqlite3_str*);
8477SQLITE_API int sqlite3_str_length(sqlite3_str*);
8478SQLITE_API char *sqlite3_str_value(sqlite3_str*);
8479
8480/*
8481** CAPI3REF: SQLite Runtime Status
8482**
8483** ^These interfaces are used to retrieve runtime status information
8484** about the performance of SQLite, and optionally to reset various
8485** highwater marks. ^The first argument is an integer code for
8486** the specific parameter to measure. ^(Recognized integer codes
8487** are of the form [status parameters | SQLITE_STATUS_...].)^
8488** ^The current value of the parameter is returned into *pCurrent.
8489** ^The highest recorded value is returned in *pHighwater. ^If the
8490** resetFlag is true, then the highest record value is reset after
8491** *pHighwater is written. ^(Some parameters do not record the highest
8492** value. For those parameters
8493** nothing is written into *pHighwater and the resetFlag is ignored.)^
8494** ^(Other parameters record only the highwater mark and not the current
8495** value. For these latter parameters nothing is written into *pCurrent.)^
8496**
8497** ^The sqlite3_status() and sqlite3_status64() routines return
8498** SQLITE_OK on success and a non-zero [error code] on failure.
8499**
8500** If either the current value or the highwater mark is too large to
8501** be represented by a 32-bit integer, then the values returned by
8502** sqlite3_status() are undefined.
8503**
8504** See also: [sqlite3_db_status()]
8505*/
8506SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
8507SQLITE_API int sqlite3_status64(
8508 int op,
8509 sqlite3_int64 *pCurrent,
8510 sqlite3_int64 *pHighwater,
8511 int resetFlag
8512);
8513
8514
8515/*
8516** CAPI3REF: Status Parameters
8517** KEYWORDS: {status parameters}
8518**
8519** These integer constants designate various run-time status parameters
8520** that can be returned by [sqlite3_status()].
8521**
8522** <dl>
8523** [[SQLITE_STATUS_MEMORY_USED]] ^(<dt>SQLITE_STATUS_MEMORY_USED</dt>
8524** <dd>This parameter is the current amount of memory checked out
8525** using [sqlite3_malloc()], either directly or indirectly. The
8526** figure includes calls made to [sqlite3_malloc()] by the application
8527** and internal memory usage by the SQLite library. Auxiliary page-cache
8528** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
8529** this parameter. The amount returned is the sum of the allocation
8530** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
8531**
8532** [[SQLITE_STATUS_MALLOC_SIZE]] ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt>
8533** <dd>This parameter records the largest memory allocation request
8534** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
8535** internal equivalents). Only the value returned in the
8536** *pHighwater parameter to [sqlite3_status()] is of interest.
8537** The value written into the *pCurrent parameter is undefined.</dd>)^
8538**
8539** [[SQLITE_STATUS_MALLOC_COUNT]] ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt>
8540** <dd>This parameter records the number of separate memory allocations
8541** currently checked out.</dd>)^
8542**
8543** [[SQLITE_STATUS_PAGECACHE_USED]] ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt>
8544** <dd>This parameter returns the number of pages used out of the
8545** [pagecache memory allocator] that was configured using
8546** [SQLITE_CONFIG_PAGECACHE]. The
8547** value returned is in pages, not in bytes.</dd>)^
8548**
8549** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]]
8550** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt>
8551** <dd>This parameter returns the number of bytes of page cache
8552** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE]
8553** buffer and where forced to overflow to [sqlite3_malloc()]. The
8554** returned value includes allocations that overflowed because they
8555** where too large (they were larger than the "sz" parameter to
8556** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because
8557** no space was left in the page cache.</dd>)^
8558**
8559** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
8560** <dd>This parameter records the largest memory allocation request
8561** handed to [pagecache memory allocator]. Only the value returned in the
8562** *pHighwater parameter to [sqlite3_status()] is of interest.
8563** The value written into the *pCurrent parameter is undefined.</dd>)^
8564**
8565** [[SQLITE_STATUS_SCRATCH_USED]] <dt>SQLITE_STATUS_SCRATCH_USED</dt>
8566** <dd>No longer used.</dd>
8567**
8568** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
8569** <dd>No longer used.</dd>
8570**
8571** [[SQLITE_STATUS_SCRATCH_SIZE]] <dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
8572** <dd>No longer used.</dd>
8573**
8574** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
8575** <dd>The *pHighwater parameter records the deepest parser stack.
8576** The *pCurrent value is undefined. The *pHighwater value is only
8577** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
8578** </dl>
8579**
8580** New status parameters may be added from time to time.
8581*/
8582#define SQLITE_STATUS_MEMORY_USED 0
8583#define SQLITE_STATUS_PAGECACHE_USED 1
8584#define SQLITE_STATUS_PAGECACHE_OVERFLOW 2
8585#define SQLITE_STATUS_SCRATCH_USED 3 /* NOT USED */
8586#define SQLITE_STATUS_SCRATCH_OVERFLOW 4 /* NOT USED */
8587#define SQLITE_STATUS_MALLOC_SIZE 5
8588#define SQLITE_STATUS_PARSER_STACK 6
8589#define SQLITE_STATUS_PAGECACHE_SIZE 7
8590#define SQLITE_STATUS_SCRATCH_SIZE 8 /* NOT USED */
8591#define SQLITE_STATUS_MALLOC_COUNT 9
8592
8593/*
8594** CAPI3REF: Database Connection Status
8595** METHOD: sqlite3
8596**
8597** ^This interface is used to retrieve runtime status information
8598** about a single [database connection]. ^The first argument is the
8599** database connection object to be interrogated. ^The second argument
8600** is an integer constant, taken from the set of
8601** [SQLITE_DBSTATUS options], that
8602** determines the parameter to interrogate. The set of
8603** [SQLITE_DBSTATUS options] is likely
8604** to grow in future releases of SQLite.
8605**
8606** ^The current value of the requested parameter is written into *pCur
8607** and the highest instantaneous value is written into *pHiwtr. ^If
8608** the resetFlg is true, then the highest instantaneous value is
8609** reset back down to the current value.
8610**
8611** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
8612** non-zero [error code] on failure.
8613**
8614** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
8615*/
8616SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
8617
8618/*
8619** CAPI3REF: Status Parameters for database connections
8620** KEYWORDS: {SQLITE_DBSTATUS options}
8621**
8622** These constants are the available integer "verbs" that can be passed as
8623** the second argument to the [sqlite3_db_status()] interface.
8624**
8625** New verbs may be added in future releases of SQLite. Existing verbs
8626** might be discontinued. Applications should check the return code from
8627** [sqlite3_db_status()] to make sure that the call worked.
8628** The [sqlite3_db_status()] interface will return a non-zero error code
8629** if a discontinued or unsupported verb is invoked.
8630**
8631** <dl>
8632** [[SQLITE_DBSTATUS_LOOKASIDE_USED]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
8633** <dd>This parameter returns the number of lookaside memory slots currently
8634** checked out.</dd>)^
8635**
8636** [[SQLITE_DBSTATUS_LOOKASIDE_HIT]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
8637** <dd>This parameter returns the number malloc attempts that were
8638** satisfied using lookaside memory. Only the high-water value is meaningful;
8639** the current value is always zero.)^
8640**
8641** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE]]
8642** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
8643** <dd>This parameter returns the number malloc attempts that might have
8644** been satisfied using lookaside memory but failed due to the amount of
8645** memory requested being larger than the lookaside slot size.
8646** Only the high-water value is meaningful;
8647** the current value is always zero.)^
8648**
8649** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL]]
8650** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
8651** <dd>This parameter returns the number malloc attempts that might have
8652** been satisfied using lookaside memory but failed due to all lookaside
8653** memory already being in use.
8654** Only the high-water value is meaningful;
8655** the current value is always zero.)^
8656**
8657** [[SQLITE_DBSTATUS_CACHE_USED]] ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
8658** <dd>This parameter returns the approximate number of bytes of heap
8659** memory used by all pager caches associated with the database connection.)^
8660** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
8661**
8662** [[SQLITE_DBSTATUS_CACHE_USED_SHARED]]
8663** ^(<dt>SQLITE_DBSTATUS_CACHE_USED_SHARED</dt>
8664** <dd>This parameter is similar to DBSTATUS_CACHE_USED, except that if a
8665** pager cache is shared between two or more connections the bytes of heap
8666** memory used by that pager cache is divided evenly between the attached
8667** connections.)^ In other words, if none of the pager caches associated
8668** with the database connection are shared, this request returns the same
8669** value as DBSTATUS_CACHE_USED. Or, if one or more or the pager caches are
8670** shared, the value returned by this call will be smaller than that returned
8671** by DBSTATUS_CACHE_USED. ^The highwater mark associated with
8672** SQLITE_DBSTATUS_CACHE_USED_SHARED is always 0.
8673**
8674** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
8675** <dd>This parameter returns the approximate number of bytes of heap
8676** memory used to store the schema for all databases associated
8677** with the connection - main, temp, and any [ATTACH]-ed databases.)^
8678** ^The full amount of memory used by the schemas is reported, even if the
8679** schema memory is shared with other database connections due to
8680** [shared cache mode] being enabled.
8681** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0.
8682**
8683** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
8684** <dd>This parameter returns the approximate number of bytes of heap
8685** and lookaside memory used by all prepared statements associated with
8686** the database connection.)^
8687** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
8688** </dd>
8689**
8690** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
8691** <dd>This parameter returns the number of pager cache hits that have
8692** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT
8693** is always 0.
8694** </dd>
8695**
8696** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
8697** <dd>This parameter returns the number of pager cache misses that have
8698** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS
8699** is always 0.
8700** </dd>
8701**
8702** [[SQLITE_DBSTATUS_CACHE_WRITE]] ^(<dt>SQLITE_DBSTATUS_CACHE_WRITE</dt>
8703** <dd>This parameter returns the number of dirty cache entries that have
8704** been written to disk. Specifically, the number of pages written to the
8705** wal file in wal mode databases, or the number of pages written to the
8706** database file in rollback mode databases. Any pages written as part of
8707** transaction rollback or database recovery operations are not included.
8708** If an IO or other error occurs while writing a page to disk, the effect
8709** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
8710** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
8711** </dd>
8712**
8713** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(<dt>SQLITE_DBSTATUS_CACHE_SPILL</dt>
8714** <dd>This parameter returns the number of dirty cache entries that have
8715** been written to disk in the middle of a transaction due to the page
8716** cache overflowing. Transactions are more efficient if they are written
8717** to disk all at once. When pages spill mid-transaction, that introduces
8718** additional overhead. This parameter can be used help identify
8719** inefficiencies that can be resolve by increasing the cache size.
8720** </dd>
8721**
8722** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
8723** <dd>This parameter returns zero for the current value if and only if
8724** all foreign key constraints (deferred or immediate) have been
8725** resolved.)^ ^The highwater mark is always 0.
8726** </dd>
8727** </dl>
8728*/
8729#define SQLITE_DBSTATUS_LOOKASIDE_USED 0
8730#define SQLITE_DBSTATUS_CACHE_USED 1
8731#define SQLITE_DBSTATUS_SCHEMA_USED 2
8732#define SQLITE_DBSTATUS_STMT_USED 3
8733#define SQLITE_DBSTATUS_LOOKASIDE_HIT 4
8734#define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE 5
8735#define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL 6
8736#define SQLITE_DBSTATUS_CACHE_HIT 7
8737#define SQLITE_DBSTATUS_CACHE_MISS 8
8738#define SQLITE_DBSTATUS_CACHE_WRITE 9
8739#define SQLITE_DBSTATUS_DEFERRED_FKS 10
8740#define SQLITE_DBSTATUS_CACHE_USED_SHARED 11
8741#define SQLITE_DBSTATUS_CACHE_SPILL 12
8742#define SQLITE_DBSTATUS_MAX 12 /* Largest defined DBSTATUS */
8743
8744
8745/*
8746** CAPI3REF: Prepared Statement Status
8747** METHOD: sqlite3_stmt
8748**
8749** ^(Each prepared statement maintains various
8750** [SQLITE_STMTSTATUS counters] that measure the number
8751** of times it has performed specific operations.)^ These counters can
8752** be used to monitor the performance characteristics of the prepared
8753** statements. For example, if the number of table steps greatly exceeds
8754** the number of table searches or result rows, that would tend to indicate
8755** that the prepared statement is using a full table scan rather than
8756** an index.
8757**
8758** ^(This interface is used to retrieve and reset counter values from
8759** a [prepared statement]. The first argument is the prepared statement
8760** object to be interrogated. The second argument
8761** is an integer code for a specific [SQLITE_STMTSTATUS counter]
8762** to be interrogated.)^
8763** ^The current value of the requested counter is returned.
8764** ^If the resetFlg is true, then the counter is reset to zero after this
8765** interface call returns.
8766**
8767** See also: [sqlite3_status()] and [sqlite3_db_status()].
8768*/
8769SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
8770
8771/*
8772** CAPI3REF: Status Parameters for prepared statements
8773** KEYWORDS: {SQLITE_STMTSTATUS counter} {SQLITE_STMTSTATUS counters}
8774**
8775** These preprocessor macros define integer codes that name counter
8776** values associated with the [sqlite3_stmt_status()] interface.
8777** The meanings of the various counters are as follows:
8778**
8779** <dl>
8780** [[SQLITE_STMTSTATUS_FULLSCAN_STEP]] <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt>
8781** <dd>^This is the number of times that SQLite has stepped forward in
8782** a table as part of a full table scan. Large numbers for this counter
8783** may indicate opportunities for performance improvement through
8784** careful use of indices.</dd>
8785**
8786** [[SQLITE_STMTSTATUS_SORT]] <dt>SQLITE_STMTSTATUS_SORT</dt>
8787** <dd>^This is the number of sort operations that have occurred.
8788** A non-zero value in this counter may indicate an opportunity to
8789** improvement performance through careful use of indices.</dd>
8790**
8791** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
8792** <dd>^This is the number of rows inserted into transient indices that
8793** were created automatically in order to help joins run faster.
8794** A non-zero value in this counter may indicate an opportunity to
8795** improvement performance by adding permanent indices that do not
8796** need to be reinitialized each time the statement is run.</dd>
8797**
8798** [[SQLITE_STMTSTATUS_VM_STEP]] <dt>SQLITE_STMTSTATUS_VM_STEP</dt>
8799** <dd>^This is the number of virtual machine operations executed
8800** by the prepared statement if that number is less than or equal
8801** to 2147483647. The number of virtual machine operations can be
8802** used as a proxy for the total work done by the prepared statement.
8803** If the number of virtual machine operations exceeds 2147483647
8804** then the value returned by this statement status code is undefined.
8805**
8806** [[SQLITE_STMTSTATUS_REPREPARE]] <dt>SQLITE_STMTSTATUS_REPREPARE</dt>
8807** <dd>^This is the number of times that the prepare statement has been
8808** automatically regenerated due to schema changes or change to
8809** [bound parameters] that might affect the query plan.
8810**
8811** [[SQLITE_STMTSTATUS_RUN]] <dt>SQLITE_STMTSTATUS_RUN</dt>
8812** <dd>^This is the number of times that the prepared statement has
8813** been run. A single "run" for the purposes of this counter is one
8814** or more calls to [sqlite3_step()] followed by a call to [sqlite3_reset()].
8815** The counter is incremented on the first [sqlite3_step()] call of each
8816** cycle.
8817**
8818** [[SQLITE_STMTSTATUS_MEMUSED]] <dt>SQLITE_STMTSTATUS_MEMUSED</dt>
8819** <dd>^This is the approximate number of bytes of heap memory
8820** used to store the prepared statement. ^This value is not actually
8821** a counter, and so the resetFlg parameter to sqlite3_stmt_status()
8822** is ignored when the opcode is SQLITE_STMTSTATUS_MEMUSED.
8823** </dd>
8824** </dl>
8825*/
8826#define SQLITE_STMTSTATUS_FULLSCAN_STEP 1
8827#define SQLITE_STMTSTATUS_SORT 2
8828#define SQLITE_STMTSTATUS_AUTOINDEX 3
8829#define SQLITE_STMTSTATUS_VM_STEP 4
8830#define SQLITE_STMTSTATUS_REPREPARE 5
8831#define SQLITE_STMTSTATUS_RUN 6
8832#define SQLITE_STMTSTATUS_MEMUSED 99
8833
8834/*
8835** CAPI3REF: Custom Page Cache Object
8836**
8837** The sqlite3_pcache type is opaque. It is implemented by
8838** the pluggable module. The SQLite core has no knowledge of
8839** its size or internal structure and never deals with the
8840** sqlite3_pcache object except by holding and passing pointers
8841** to the object.
8842**
8843** See [sqlite3_pcache_methods2] for additional information.
8844*/
8845typedef struct sqlite3_pcache sqlite3_pcache;
8846
8847/*
8848** CAPI3REF: Custom Page Cache Object
8849**
8850** The sqlite3_pcache_page object represents a single page in the
8851** page cache. The page cache will allocate instances of this
8852** object. Various methods of the page cache use pointers to instances
8853** of this object as parameters or as their return value.
8854**
8855** See [sqlite3_pcache_methods2] for additional information.
8856*/
8857typedef struct sqlite3_pcache_page sqlite3_pcache_page;
8858struct sqlite3_pcache_page {
8859 void *pBuf; /* The content of the page */
8860 void *pExtra; /* Extra information associated with the page */
8861};
8862
8863/*
8864** CAPI3REF: Application Defined Page Cache.
8865** KEYWORDS: {page cache}
8866**
8867** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can
8868** register an alternative page cache implementation by passing in an
8869** instance of the sqlite3_pcache_methods2 structure.)^
8870** In many applications, most of the heap memory allocated by
8871** SQLite is used for the page cache.
8872** By implementing a
8873** custom page cache using this API, an application can better control
8874** the amount of memory consumed by SQLite, the way in which
8875** that memory is allocated and released, and the policies used to
8876** determine exactly which parts of a database file are cached and for
8877** how long.
8878**
8879** The alternative page cache mechanism is an
8880** extreme measure that is only needed by the most demanding applications.
8881** The built-in page cache is recommended for most uses.
8882**
8883** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an
8884** internal buffer by SQLite within the call to [sqlite3_config]. Hence
8885** the application may discard the parameter after the call to
8886** [sqlite3_config()] returns.)^
8887**
8888** [[the xInit() page cache method]]
8889** ^(The xInit() method is called once for each effective
8890** call to [sqlite3_initialize()])^
8891** (usually only once during the lifetime of the process). ^(The xInit()
8892** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^
8893** The intent of the xInit() method is to set up global data structures
8894** required by the custom page cache implementation.
8895** ^(If the xInit() method is NULL, then the
8896** built-in default page cache is used instead of the application defined
8897** page cache.)^
8898**
8899** [[the xShutdown() page cache method]]
8900** ^The xShutdown() method is called by [sqlite3_shutdown()].
8901** It can be used to clean up
8902** any outstanding resources before process shutdown, if required.
8903** ^The xShutdown() method may be NULL.
8904**
8905** ^SQLite automatically serializes calls to the xInit method,
8906** so the xInit method need not be threadsafe. ^The
8907** xShutdown method is only called from [sqlite3_shutdown()] so it does
8908** not need to be threadsafe either. All other methods must be threadsafe
8909** in multithreaded applications.
8910**
8911** ^SQLite will never invoke xInit() more than once without an intervening
8912** call to xShutdown().
8913**
8914** [[the xCreate() page cache methods]]
8915** ^SQLite invokes the xCreate() method to construct a new cache instance.
8916** SQLite will typically create one cache instance for each open database file,
8917** though this is not guaranteed. ^The
8918** first parameter, szPage, is the size in bytes of the pages that must
8919** be allocated by the cache. ^szPage will always a power of two. ^The
8920** second parameter szExtra is a number of bytes of extra storage
8921** associated with each page cache entry. ^The szExtra parameter will
8922** a number less than 250. SQLite will use the
8923** extra szExtra bytes on each page to store metadata about the underlying
8924** database page on disk. The value passed into szExtra depends
8925** on the SQLite version, the target platform, and how SQLite was compiled.
8926** ^The third argument to xCreate(), bPurgeable, is true if the cache being
8927** created will be used to cache database pages of a file stored on disk, or
8928** false if it is used for an in-memory database. The cache implementation
8929** does not have to do anything special based with the value of bPurgeable;
8930** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will
8931** never invoke xUnpin() except to deliberately delete a page.
8932** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
8933** false will always have the "discard" flag set to true.
8934** ^Hence, a cache created with bPurgeable false will
8935** never contain any unpinned pages.
8936**
8937** [[the xCachesize() page cache method]]
8938** ^(The xCachesize() method may be called at any time by SQLite to set the
8939** suggested maximum cache-size (number of pages stored by) the cache
8940** instance passed as the first argument. This is the value configured using
8941** the SQLite "[PRAGMA cache_size]" command.)^ As with the bPurgeable
8942** parameter, the implementation is not required to do anything with this
8943** value; it is advisory only.
8944**
8945** [[the xPagecount() page cache methods]]
8946** The xPagecount() method must return the number of pages currently
8947** stored in the cache, both pinned and unpinned.
8948**
8949** [[the xFetch() page cache methods]]
8950** The xFetch() method locates a page in the cache and returns a pointer to
8951** an sqlite3_pcache_page object associated with that page, or a NULL pointer.
8952** The pBuf element of the returned sqlite3_pcache_page object will be a
8953** pointer to a buffer of szPage bytes used to store the content of a
8954** single database page. The pExtra element of sqlite3_pcache_page will be
8955** a pointer to the szExtra bytes of extra storage that SQLite has requested
8956** for each entry in the page cache.
8957**
8958** The page to be fetched is determined by the key. ^The minimum key value
8959** is 1. After it has been retrieved using xFetch, the page is considered
8960** to be "pinned".
8961**
8962** If the requested page is already in the page cache, then the page cache
8963** implementation must return a pointer to the page buffer with its content
8964** intact. If the requested page is not already in the cache, then the
8965** cache implementation should use the value of the createFlag
8966** parameter to help it determined what action to take:
8967**
8968** <table border=1 width=85% align=center>
8969** <tr><th> createFlag <th> Behavior when page is not already in cache
8970** <tr><td> 0 <td> Do not allocate a new page. Return NULL.
8971** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
8972** Otherwise return NULL.
8973** <tr><td> 2 <td> Make every effort to allocate a new page. Only return
8974** NULL if allocating a new page is effectively impossible.
8975** </table>
8976**
8977** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1. SQLite
8978** will only use a createFlag of 2 after a prior call with a createFlag of 1
8979** failed.)^ In between the to xFetch() calls, SQLite may
8980** attempt to unpin one or more cache pages by spilling the content of
8981** pinned pages to disk and synching the operating system disk cache.
8982**
8983** [[the xUnpin() page cache method]]
8984** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
8985** as its second argument. If the third parameter, discard, is non-zero,
8986** then the page must be evicted from the cache.
8987** ^If the discard parameter is
8988** zero, then the page may be discarded or retained at the discretion of
8989** page cache implementation. ^The page cache implementation
8990** may choose to evict unpinned pages at any time.
8991**
8992** The cache must not perform any reference counting. A single
8993** call to xUnpin() unpins the page regardless of the number of prior calls
8994** to xFetch().
8995**
8996** [[the xRekey() page cache methods]]
8997** The xRekey() method is used to change the key value associated with the
8998** page passed as the second argument. If the cache
8999** previously contains an entry associated with newKey, it must be
9000** discarded. ^Any prior cache entry associated with newKey is guaranteed not
9001** to be pinned.
9002**
9003** When SQLite calls the xTruncate() method, the cache must discard all
9004** existing cache entries with page numbers (keys) greater than or equal
9005** to the value of the iLimit parameter passed to xTruncate(). If any
9006** of these pages are pinned, they are implicitly unpinned, meaning that
9007** they can be safely discarded.
9008**
9009** [[the xDestroy() page cache method]]
9010** ^The xDestroy() method is used to delete a cache allocated by xCreate().
9011** All resources associated with the specified cache should be freed. ^After
9012** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
9013** handle invalid, and will not use it with any other sqlite3_pcache_methods2
9014** functions.
9015**
9016** [[the xShrink() page cache method]]
9017** ^SQLite invokes the xShrink() method when it wants the page cache to
9018** free up as much of heap memory as possible. The page cache implementation
9019** is not obligated to free any memory, but well-behaved implementations should
9020** do their best.
9021*/
9022typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2;
9023struct sqlite3_pcache_methods2 {
9024 int iVersion;
9025 void *pArg;
9026 int (*xInit)(void*);
9027 void (*xShutdown)(void*);
9028 sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
9029 void (*xCachesize)(sqlite3_pcache*, int nCachesize);
9030 int (*xPagecount)(sqlite3_pcache*);
9031 sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
9032 void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
9033 void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*,
9034 unsigned oldKey, unsigned newKey);
9035 void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
9036 void (*xDestroy)(sqlite3_pcache*);
9037 void (*xShrink)(sqlite3_pcache*);
9038};
9039
9040/*
9041** This is the obsolete pcache_methods object that has now been replaced
9042** by sqlite3_pcache_methods2. This object is not used by SQLite. It is
9043** retained in the header file for backwards compatibility only.
9044*/
9045typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
9046struct sqlite3_pcache_methods {
9047 void *pArg;
9048 int (*xInit)(void*);
9049 void (*xShutdown)(void*);
9050 sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
9051 void (*xCachesize)(sqlite3_pcache*, int nCachesize);
9052 int (*xPagecount)(sqlite3_pcache*);
9053 void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
9054 void (*xUnpin)(sqlite3_pcache*, void*, int discard);
9055 void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
9056 void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
9057 void (*xDestroy)(sqlite3_pcache*);
9058};
9059
9060
9061/*
9062** CAPI3REF: Online Backup Object
9063**
9064** The sqlite3_backup object records state information about an ongoing
9065** online backup operation. ^The sqlite3_backup object is created by
9066** a call to [sqlite3_backup_init()] and is destroyed by a call to
9067** [sqlite3_backup_finish()].
9068**
9069** See Also: [Using the SQLite Online Backup API]
9070*/
9071typedef struct sqlite3_backup sqlite3_backup;
9072
9073/*
9074** CAPI3REF: Online Backup API.
9075**
9076** The backup API copies the content of one database into another.
9077** It is useful either for creating backups of databases or
9078** for copying in-memory databases to or from persistent files.
9079**
9080** See Also: [Using the SQLite Online Backup API]
9081**
9082** ^SQLite holds a write transaction open on the destination database file
9083** for the duration of the backup operation.
9084** ^The source database is read-locked only while it is being read;
9085** it is not locked continuously for the entire backup operation.
9086** ^Thus, the backup may be performed on a live source database without
9087** preventing other database connections from
9088** reading or writing to the source database while the backup is underway.
9089**
9090** ^(To perform a backup operation:
9091** <ol>
9092** <li><b>sqlite3_backup_init()</b> is called once to initialize the
9093** backup,
9094** <li><b>sqlite3_backup_step()</b> is called one or more times to transfer
9095** the data between the two databases, and finally
9096** <li><b>sqlite3_backup_finish()</b> is called to release all resources
9097** associated with the backup operation.
9098** </ol>)^
9099** There should be exactly one call to sqlite3_backup_finish() for each
9100** successful call to sqlite3_backup_init().
9101**
9102** [[sqlite3_backup_init()]] <b>sqlite3_backup_init()</b>
9103**
9104** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the
9105** [database connection] associated with the destination database
9106** and the database name, respectively.
9107** ^The database name is "main" for the main database, "temp" for the
9108** temporary database, or the name specified after the AS keyword in
9109** an [ATTACH] statement for an attached database.
9110** ^The S and M arguments passed to
9111** sqlite3_backup_init(D,N,S,M) identify the [database connection]
9112** and database name of the source database, respectively.
9113** ^The source and destination [database connections] (parameters S and D)
9114** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
9115** an error.
9116**
9117** ^A call to sqlite3_backup_init() will fail, returning NULL, if
9118** there is already a read or read-write transaction open on the
9119** destination database.
9120**
9121** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
9122** returned and an error code and error message are stored in the
9123** destination [database connection] D.
9124** ^The error code and message for the failed call to sqlite3_backup_init()
9125** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or
9126** [sqlite3_errmsg16()] functions.
9127** ^A successful call to sqlite3_backup_init() returns a pointer to an
9128** [sqlite3_backup] object.
9129** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and
9130** sqlite3_backup_finish() functions to perform the specified backup
9131** operation.
9132**
9133** [[sqlite3_backup_step()]] <b>sqlite3_backup_step()</b>
9134**
9135** ^Function sqlite3_backup_step(B,N) will copy up to N pages between
9136** the source and destination databases specified by [sqlite3_backup] object B.
9137** ^If N is negative, all remaining source pages are copied.
9138** ^If sqlite3_backup_step(B,N) successfully copies N pages and there
9139** are still more pages to be copied, then the function returns [SQLITE_OK].
9140** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages
9141** from source to destination, then it returns [SQLITE_DONE].
9142** ^If an error occurs while running sqlite3_backup_step(B,N),
9143** then an [error code] is returned. ^As well as [SQLITE_OK] and
9144** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
9145** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
9146** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
9147**
9148** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
9149** <ol>
9150** <li> the destination database was opened read-only, or
9151** <li> the destination database is using write-ahead-log journaling
9152** and the destination and source page sizes differ, or
9153** <li> the destination database is an in-memory database and the
9154** destination and source page sizes differ.
9155** </ol>)^
9156**
9157** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
9158** the [sqlite3_busy_handler | busy-handler function]
9159** is invoked (if one is specified). ^If the
9160** busy-handler returns non-zero before the lock is available, then
9161** [SQLITE_BUSY] is returned to the caller. ^In this case the call to
9162** sqlite3_backup_step() can be retried later. ^If the source
9163** [database connection]
9164** is being used to write to the source database when sqlite3_backup_step()
9165** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this
9166** case the call to sqlite3_backup_step() can be retried later on. ^(If
9167** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or
9168** [SQLITE_READONLY] is returned, then
9169** there is no point in retrying the call to sqlite3_backup_step(). These
9170** errors are considered fatal.)^ The application must accept
9171** that the backup operation has failed and pass the backup operation handle
9172** to the sqlite3_backup_finish() to release associated resources.
9173**
9174** ^The first call to sqlite3_backup_step() obtains an exclusive lock
9175** on the destination file. ^The exclusive lock is not released until either
9176** sqlite3_backup_finish() is called or the backup operation is complete
9177** and sqlite3_backup_step() returns [SQLITE_DONE]. ^Every call to
9178** sqlite3_backup_step() obtains a [shared lock] on the source database that
9179** lasts for the duration of the sqlite3_backup_step() call.
9180** ^Because the source database is not locked between calls to
9181** sqlite3_backup_step(), the source database may be modified mid-way
9182** through the backup process. ^If the source database is modified by an
9183** external process or via a database connection other than the one being
9184** used by the backup operation, then the backup will be automatically
9185** restarted by the next call to sqlite3_backup_step(). ^If the source
9186** database is modified by the using the same database connection as is used
9187** by the backup operation, then the backup database is automatically
9188** updated at the same time.
9189**
9190** [[sqlite3_backup_finish()]] <b>sqlite3_backup_finish()</b>
9191**
9192** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the
9193** application wishes to abandon the backup operation, the application
9194** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish().
9195** ^The sqlite3_backup_finish() interfaces releases all
9196** resources associated with the [sqlite3_backup] object.
9197** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any
9198** active write-transaction on the destination database is rolled back.
9199** The [sqlite3_backup] object is invalid
9200** and may not be used following a call to sqlite3_backup_finish().
9201**
9202** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no
9203** sqlite3_backup_step() errors occurred, regardless or whether or not
9204** sqlite3_backup_step() completed.
9205** ^If an out-of-memory condition or IO error occurred during any prior
9206** sqlite3_backup_step() call on the same [sqlite3_backup] object, then
9207** sqlite3_backup_finish() returns the corresponding [error code].
9208**
9209** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step()
9210** is not a permanent error and does not affect the return value of
9211** sqlite3_backup_finish().
9212**
9213** [[sqlite3_backup_remaining()]] [[sqlite3_backup_pagecount()]]
9214** <b>sqlite3_backup_remaining() and sqlite3_backup_pagecount()</b>
9215**
9216** ^The sqlite3_backup_remaining() routine returns the number of pages still
9217** to be backed up at the conclusion of the most recent sqlite3_backup_step().
9218** ^The sqlite3_backup_pagecount() routine returns the total number of pages
9219** in the source database at the conclusion of the most recent
9220** sqlite3_backup_step().
9221** ^(The values returned by these functions are only updated by
9222** sqlite3_backup_step(). If the source database is modified in a way that
9223** changes the size of the source database or the number of pages remaining,
9224** those changes are not reflected in the output of sqlite3_backup_pagecount()
9225** and sqlite3_backup_remaining() until after the next
9226** sqlite3_backup_step().)^
9227**
9228** <b>Concurrent Usage of Database Handles</b>
9229**
9230** ^The source [database connection] may be used by the application for other
9231** purposes while a backup operation is underway or being initialized.
9232** ^If SQLite is compiled and configured to support threadsafe database
9233** connections, then the source database connection may be used concurrently
9234** from within other threads.
9235**
9236** However, the application must guarantee that the destination
9237** [database connection] is not passed to any other API (by any thread) after
9238** sqlite3_backup_init() is called and before the corresponding call to
9239** sqlite3_backup_finish(). SQLite does not currently check to see
9240** if the application incorrectly accesses the destination [database connection]
9241** and so no error code is reported, but the operations may malfunction
9242** nevertheless. Use of the destination database connection while a
9243** backup is in progress might also also cause a mutex deadlock.
9244**
9245** If running in [shared cache mode], the application must
9246** guarantee that the shared cache used by the destination database
9247** is not accessed while the backup is running. In practice this means
9248** that the application must guarantee that the disk file being
9249** backed up to is not accessed by any connection within the process,
9250** not just the specific connection that was passed to sqlite3_backup_init().
9251**
9252** The [sqlite3_backup] object itself is partially threadsafe. Multiple
9253** threads may safely make multiple concurrent calls to sqlite3_backup_step().
9254** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
9255** APIs are not strictly speaking threadsafe. If they are invoked at the
9256** same time as another thread is invoking sqlite3_backup_step() it is
9257** possible that they return invalid values.
9258*/
9259SQLITE_API sqlite3_backup *sqlite3_backup_init(
9260 sqlite3 *pDest, /* Destination database handle */
9261 const char *zDestName, /* Destination database name */
9262 sqlite3 *pSource, /* Source database handle */
9263 const char *zSourceName /* Source database name */
9264);
9265SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
9266SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
9267SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
9268SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
9269
9270/*
9271** CAPI3REF: Unlock Notification
9272** METHOD: sqlite3
9273**
9274** ^When running in shared-cache mode, a database operation may fail with
9275** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
9276** individual tables within the shared-cache cannot be obtained. See
9277** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
9278** ^This API may be used to register a callback that SQLite will invoke
9279** when the connection currently holding the required lock relinquishes it.
9280** ^This API is only available if the library was compiled with the
9281** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
9282**
9283** See Also: [Using the SQLite Unlock Notification Feature].
9284**
9285** ^Shared-cache locks are released when a database connection concludes
9286** its current transaction, either by committing it or rolling it back.
9287**
9288** ^When a connection (known as the blocked connection) fails to obtain a
9289** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
9290** identity of the database connection (the blocking connection) that
9291** has locked the required resource is stored internally. ^After an
9292** application receives an SQLITE_LOCKED error, it may call the
9293** sqlite3_unlock_notify() method with the blocked connection handle as
9294** the first argument to register for a callback that will be invoked
9295** when the blocking connections current transaction is concluded. ^The
9296** callback is invoked from within the [sqlite3_step] or [sqlite3_close]
9297** call that concludes the blocking connections transaction.
9298**
9299** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
9300** there is a chance that the blocking connection will have already
9301** concluded its transaction by the time sqlite3_unlock_notify() is invoked.
9302** If this happens, then the specified callback is invoked immediately,
9303** from within the call to sqlite3_unlock_notify().)^
9304**
9305** ^If the blocked connection is attempting to obtain a write-lock on a
9306** shared-cache table, and more than one other connection currently holds
9307** a read-lock on the same table, then SQLite arbitrarily selects one of
9308** the other connections to use as the blocking connection.
9309**
9310** ^(There may be at most one unlock-notify callback registered by a
9311** blocked connection. If sqlite3_unlock_notify() is called when the
9312** blocked connection already has a registered unlock-notify callback,
9313** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
9314** called with a NULL pointer as its second argument, then any existing
9315** unlock-notify callback is canceled. ^The blocked connections
9316** unlock-notify callback may also be canceled by closing the blocked
9317** connection using [sqlite3_close()].
9318**
9319** The unlock-notify callback is not reentrant. If an application invokes
9320** any sqlite3_xxx API functions from within an unlock-notify callback, a
9321** crash or deadlock may be the result.
9322**
9323** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always
9324** returns SQLITE_OK.
9325**
9326** <b>Callback Invocation Details</b>
9327**
9328** When an unlock-notify callback is registered, the application provides a
9329** single void* pointer that is passed to the callback when it is invoked.
9330** However, the signature of the callback function allows SQLite to pass
9331** it an array of void* context pointers. The first argument passed to
9332** an unlock-notify callback is a pointer to an array of void* pointers,
9333** and the second is the number of entries in the array.
9334**
9335** When a blocking connections transaction is concluded, there may be
9336** more than one blocked connection that has registered for an unlock-notify
9337** callback. ^If two or more such blocked connections have specified the
9338** same callback function, then instead of invoking the callback function
9339** multiple times, it is invoked once with the set of void* context pointers
9340** specified by the blocked connections bundled together into an array.
9341** This gives the application an opportunity to prioritize any actions
9342** related to the set of unblocked database connections.
9343**
9344** <b>Deadlock Detection</b>
9345**
9346** Assuming that after registering for an unlock-notify callback a
9347** database waits for the callback to be issued before taking any further
9348** action (a reasonable assumption), then using this API may cause the
9349** application to deadlock. For example, if connection X is waiting for
9350** connection Y's transaction to be concluded, and similarly connection
9351** Y is waiting on connection X's transaction, then neither connection
9352** will proceed and the system may remain deadlocked indefinitely.
9353**
9354** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
9355** detection. ^If a given call to sqlite3_unlock_notify() would put the
9356** system in a deadlocked state, then SQLITE_LOCKED is returned and no
9357** unlock-notify callback is registered. The system is said to be in
9358** a deadlocked state if connection A has registered for an unlock-notify
9359** callback on the conclusion of connection B's transaction, and connection
9360** B has itself registered for an unlock-notify callback when connection
9361** A's transaction is concluded. ^Indirect deadlock is also detected, so
9362** the system is also considered to be deadlocked if connection B has
9363** registered for an unlock-notify callback on the conclusion of connection
9364** C's transaction, where connection C is waiting on connection A. ^Any
9365** number of levels of indirection are allowed.
9366**
9367** <b>The "DROP TABLE" Exception</b>
9368**
9369** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost
9370** always appropriate to call sqlite3_unlock_notify(). There is however,
9371** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
9372** SQLite checks if there are any currently executing SELECT statements
9373** that belong to the same connection. If there are, SQLITE_LOCKED is
9374** returned. In this case there is no "blocking connection", so invoking
9375** sqlite3_unlock_notify() results in the unlock-notify callback being
9376** invoked immediately. If the application then re-attempts the "DROP TABLE"
9377** or "DROP INDEX" query, an infinite loop might be the result.
9378**
9379** One way around this problem is to check the extended error code returned
9380** by an sqlite3_step() call. ^(If there is a blocking connection, then the
9381** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
9382** the special "DROP TABLE/INDEX" case, the extended error code is just
9383** SQLITE_LOCKED.)^
9384*/
9385SQLITE_API int sqlite3_unlock_notify(
9386 sqlite3 *pBlocked, /* Waiting connection */
9387 void (*xNotify)(void **apArg, int nArg), /* Callback function to invoke */
9388 void *pNotifyArg /* Argument to pass to xNotify */
9389);
9390
9391
9392/*
9393** CAPI3REF: String Comparison
9394**
9395** ^The [sqlite3_stricmp()] and [sqlite3_strnicmp()] APIs allow applications
9396** and extensions to compare the contents of two buffers containing UTF-8
9397** strings in a case-independent fashion, using the same definition of "case
9398** independence" that SQLite uses internally when comparing identifiers.
9399*/
9400SQLITE_API int sqlite3_stricmp(const char *, const char *);
9401SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
9402
9403/*
9404** CAPI3REF: String Globbing
9405*
9406** ^The [sqlite3_strglob(P,X)] interface returns zero if and only if
9407** string X matches the [GLOB] pattern P.
9408** ^The definition of [GLOB] pattern matching used in
9409** [sqlite3_strglob(P,X)] is the same as for the "X GLOB P" operator in the
9410** SQL dialect understood by SQLite. ^The [sqlite3_strglob(P,X)] function
9411** is case sensitive.
9412**
9413** Note that this routine returns zero on a match and non-zero if the strings
9414** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
9415**
9416** See also: [sqlite3_strlike()].
9417*/
9418SQLITE_API int sqlite3_strglob(const char *zGlob, const char *zStr);
9419
9420/*
9421** CAPI3REF: String LIKE Matching
9422*
9423** ^The [sqlite3_strlike(P,X,E)] interface returns zero if and only if
9424** string X matches the [LIKE] pattern P with escape character E.
9425** ^The definition of [LIKE] pattern matching used in
9426** [sqlite3_strlike(P,X,E)] is the same as for the "X LIKE P ESCAPE E"
9427** operator in the SQL dialect understood by SQLite. ^For "X LIKE P" without
9428** the ESCAPE clause, set the E parameter of [sqlite3_strlike(P,X,E)] to 0.
9429** ^As with the LIKE operator, the [sqlite3_strlike(P,X,E)] function is case
9430** insensitive - equivalent upper and lower case ASCII characters match
9431** one another.
9432**
9433** ^The [sqlite3_strlike(P,X,E)] function matches Unicode characters, though
9434** only ASCII characters are case folded.
9435**
9436** Note that this routine returns zero on a match and non-zero if the strings
9437** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
9438**
9439** See also: [sqlite3_strglob()].
9440*/
9441SQLITE_API int sqlite3_strlike(const char *zGlob, const char *zStr, unsigned int cEsc);
9442
9443/*
9444** CAPI3REF: Error Logging Interface
9445**
9446** ^The [sqlite3_log()] interface writes a message into the [error log]
9447** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
9448** ^If logging is enabled, the zFormat string and subsequent arguments are
9449** used with [sqlite3_snprintf()] to generate the final output string.
9450**
9451** The sqlite3_log() interface is intended for use by extensions such as
9452** virtual tables, collating functions, and SQL functions. While there is
9453** nothing to prevent an application from calling sqlite3_log(), doing so
9454** is considered bad form.
9455**
9456** The zFormat string must not be NULL.
9457**
9458** To avoid deadlocks and other threading problems, the sqlite3_log() routine
9459** will not use dynamically allocated memory. The log message is stored in
9460** a fixed-length buffer on the stack. If the log message is longer than
9461** a few hundred characters, it will be truncated to the length of the
9462** buffer.
9463*/
9464SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
9465
9466/*
9467** CAPI3REF: Write-Ahead Log Commit Hook
9468** METHOD: sqlite3
9469**
9470** ^The [sqlite3_wal_hook()] function is used to register a callback that
9471** is invoked each time data is committed to a database in wal mode.
9472**
9473** ^(The callback is invoked by SQLite after the commit has taken place and
9474** the associated write-lock on the database released)^, so the implementation
9475** may read, write or [checkpoint] the database as required.
9476**
9477** ^The first parameter passed to the callback function when it is invoked
9478** is a copy of the third parameter passed to sqlite3_wal_hook() when
9479** registering the callback. ^The second is a copy of the database handle.
9480** ^The third parameter is the name of the database that was written to -
9481** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
9482** is the number of pages currently in the write-ahead log file,
9483** including those that were just committed.
9484**
9485** The callback function should normally return [SQLITE_OK]. ^If an error
9486** code is returned, that error will propagate back up through the
9487** SQLite code base to cause the statement that provoked the callback
9488** to report an error, though the commit will have still occurred. If the
9489** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
9490** that does not correspond to any valid SQLite error code, the results
9491** are undefined.
9492**
9493** A single database handle may have at most a single write-ahead log callback
9494** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
9495** previously registered write-ahead log callback. ^Note that the
9496** [sqlite3_wal_autocheckpoint()] interface and the
9497** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
9498** overwrite any prior [sqlite3_wal_hook()] settings.
9499*/
9500SQLITE_API void *sqlite3_wal_hook(
9501 sqlite3*,
9502 int(*)(void *,sqlite3*,const char*,int),
9503 void*
9504);
9505
9506/*
9507** CAPI3REF: Configure an auto-checkpoint
9508** METHOD: sqlite3
9509**
9510** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
9511** [sqlite3_wal_hook()] that causes any database on [database connection] D
9512** to automatically [checkpoint]
9513** after committing a transaction if there are N or
9514** more frames in the [write-ahead log] file. ^Passing zero or
9515** a negative value as the nFrame parameter disables automatic
9516** checkpoints entirely.
9517**
9518** ^The callback registered by this function replaces any existing callback
9519** registered using [sqlite3_wal_hook()]. ^Likewise, registering a callback
9520** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
9521** configured by this function.
9522**
9523** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
9524** from SQL.
9525**
9526** ^Checkpoints initiated by this mechanism are
9527** [sqlite3_wal_checkpoint_v2|PASSIVE].
9528**
9529** ^Every new [database connection] defaults to having the auto-checkpoint
9530** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
9531** pages. The use of this interface
9532** is only necessary if the default setting is found to be suboptimal
9533** for a particular application.
9534*/
9535SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
9536
9537/*
9538** CAPI3REF: Checkpoint a database
9539** METHOD: sqlite3
9540**
9541** ^(The sqlite3_wal_checkpoint(D,X) is equivalent to
9542** [sqlite3_wal_checkpoint_v2](D,X,[SQLITE_CHECKPOINT_PASSIVE],0,0).)^
9543**
9544** In brief, sqlite3_wal_checkpoint(D,X) causes the content in the
9545** [write-ahead log] for database X on [database connection] D to be
9546** transferred into the database file and for the write-ahead log to
9547** be reset. See the [checkpointing] documentation for addition
9548** information.
9549**
9550** This interface used to be the only way to cause a checkpoint to
9551** occur. But then the newer and more powerful [sqlite3_wal_checkpoint_v2()]
9552** interface was added. This interface is retained for backwards
9553** compatibility and as a convenience for applications that need to manually
9554** start a callback but which do not need the full power (and corresponding
9555** complication) of [sqlite3_wal_checkpoint_v2()].
9556*/
9557SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
9558
9559/*
9560** CAPI3REF: Checkpoint a database
9561** METHOD: sqlite3
9562**
9563** ^(The sqlite3_wal_checkpoint_v2(D,X,M,L,C) interface runs a checkpoint
9564** operation on database X of [database connection] D in mode M. Status
9565** information is written back into integers pointed to by L and C.)^
9566** ^(The M parameter must be a valid [checkpoint mode]:)^
9567**
9568** <dl>
9569** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
9570** ^Checkpoint as many frames as possible without waiting for any database
9571** readers or writers to finish, then sync the database file if all frames
9572** in the log were checkpointed. ^The [busy-handler callback]
9573** is never invoked in the SQLITE_CHECKPOINT_PASSIVE mode.
9574** ^On the other hand, passive mode might leave the checkpoint unfinished
9575** if there are concurrent readers or writers.
9576**
9577** <dt>SQLITE_CHECKPOINT_FULL<dd>
9578** ^This mode blocks (it invokes the
9579** [sqlite3_busy_handler|busy-handler callback]) until there is no
9580** database writer and all readers are reading from the most recent database
9581** snapshot. ^It then checkpoints all frames in the log file and syncs the
9582** database file. ^This mode blocks new database writers while it is pending,
9583** but new database readers are allowed to continue unimpeded.
9584**
9585** <dt>SQLITE_CHECKPOINT_RESTART<dd>
9586** ^This mode works the same way as SQLITE_CHECKPOINT_FULL with the addition
9587** that after checkpointing the log file it blocks (calls the
9588** [busy-handler callback])
9589** until all readers are reading from the database file only. ^This ensures
9590** that the next writer will restart the log file from the beginning.
9591** ^Like SQLITE_CHECKPOINT_FULL, this mode blocks new
9592** database writer attempts while it is pending, but does not impede readers.
9593**
9594** <dt>SQLITE_CHECKPOINT_TRUNCATE<dd>
9595** ^This mode works the same way as SQLITE_CHECKPOINT_RESTART with the
9596** addition that it also truncates the log file to zero bytes just prior
9597** to a successful return.
9598** </dl>
9599**
9600** ^If pnLog is not NULL, then *pnLog is set to the total number of frames in
9601** the log file or to -1 if the checkpoint could not run because
9602** of an error or because the database is not in [WAL mode]. ^If pnCkpt is not
9603** NULL,then *pnCkpt is set to the total number of checkpointed frames in the
9604** log file (including any that were already checkpointed before the function
9605** was called) or to -1 if the checkpoint could not run due to an error or
9606** because the database is not in WAL mode. ^Note that upon successful
9607** completion of an SQLITE_CHECKPOINT_TRUNCATE, the log file will have been
9608** truncated to zero bytes and so both *pnLog and *pnCkpt will be set to zero.
9609**
9610** ^All calls obtain an exclusive "checkpoint" lock on the database file. ^If
9611** any other process is running a checkpoint operation at the same time, the
9612** lock cannot be obtained and SQLITE_BUSY is returned. ^Even if there is a
9613** busy-handler configured, it will not be invoked in this case.
9614**
9615** ^The SQLITE_CHECKPOINT_FULL, RESTART and TRUNCATE modes also obtain the
9616** exclusive "writer" lock on the database file. ^If the writer lock cannot be
9617** obtained immediately, and a busy-handler is configured, it is invoked and
9618** the writer lock retried until either the busy-handler returns 0 or the lock
9619** is successfully obtained. ^The busy-handler is also invoked while waiting for
9620** database readers as described above. ^If the busy-handler returns 0 before
9621** the writer lock is obtained or while waiting for database readers, the
9622** checkpoint operation proceeds from that point in the same way as
9623** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
9624** without blocking any further. ^SQLITE_BUSY is returned in this case.
9625**
9626** ^If parameter zDb is NULL or points to a zero length string, then the
9627** specified operation is attempted on all WAL databases [attached] to
9628** [database connection] db. In this case the
9629** values written to output parameters *pnLog and *pnCkpt are undefined. ^If
9630** an SQLITE_BUSY error is encountered when processing one or more of the
9631** attached WAL databases, the operation is still attempted on any remaining
9632** attached databases and SQLITE_BUSY is returned at the end. ^If any other
9633** error occurs while processing an attached database, processing is abandoned
9634** and the error code is returned to the caller immediately. ^If no error
9635** (SQLITE_BUSY or otherwise) is encountered while processing the attached
9636** databases, SQLITE_OK is returned.
9637**
9638** ^If database zDb is the name of an attached database that is not in WAL
9639** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If
9640** zDb is not NULL (or a zero length string) and is not the name of any
9641** attached database, SQLITE_ERROR is returned to the caller.
9642**
9643** ^Unless it returns SQLITE_MISUSE,
9644** the sqlite3_wal_checkpoint_v2() interface
9645** sets the error information that is queried by
9646** [sqlite3_errcode()] and [sqlite3_errmsg()].
9647**
9648** ^The [PRAGMA wal_checkpoint] command can be used to invoke this interface
9649** from SQL.
9650*/
9651SQLITE_API int sqlite3_wal_checkpoint_v2(
9652 sqlite3 *db, /* Database handle */
9653 const char *zDb, /* Name of attached database (or NULL) */
9654 int eMode, /* SQLITE_CHECKPOINT_* value */
9655 int *pnLog, /* OUT: Size of WAL log in frames */
9656 int *pnCkpt /* OUT: Total number of frames checkpointed */
9657);
9658
9659/*
9660** CAPI3REF: Checkpoint Mode Values
9661** KEYWORDS: {checkpoint mode}
9662**
9663** These constants define all valid values for the "checkpoint mode" passed
9664** as the third parameter to the [sqlite3_wal_checkpoint_v2()] interface.
9665** See the [sqlite3_wal_checkpoint_v2()] documentation for details on the
9666** meaning of each of these checkpoint modes.
9667*/
9668#define SQLITE_CHECKPOINT_PASSIVE 0 /* Do as much as possible w/o blocking */
9669#define SQLITE_CHECKPOINT_FULL 1 /* Wait for writers, then checkpoint */
9670#define SQLITE_CHECKPOINT_RESTART 2 /* Like FULL but wait for for readers */
9671#define SQLITE_CHECKPOINT_TRUNCATE 3 /* Like RESTART but also truncate WAL */
9672
9673/*
9674** CAPI3REF: Virtual Table Interface Configuration
9675**
9676** This function may be called by either the [xConnect] or [xCreate] method
9677** of a [virtual table] implementation to configure
9678** various facets of the virtual table interface.
9679**
9680** If this interface is invoked outside the context of an xConnect or
9681** xCreate virtual table method then the behavior is undefined.
9682**
9683** At present, there is only one option that may be configured using
9684** this function. (See [SQLITE_VTAB_CONSTRAINT_SUPPORT].) Further options
9685** may be added in the future.
9686*/
9687SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...);
9688
9689/*
9690** CAPI3REF: Virtual Table Configuration Options
9691**
9692** These macros define the various options to the
9693** [sqlite3_vtab_config()] interface that [virtual table] implementations
9694** can use to customize and optimize their behavior.
9695**
9696** <dl>
9697** [[SQLITE_VTAB_CONSTRAINT_SUPPORT]]
9698** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT
9699** <dd>Calls of the form
9700** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
9701** where X is an integer. If X is zero, then the [virtual table] whose
9702** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
9703** support constraints. In this configuration (which is the default) if
9704** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
9705** statement is rolled back as if [ON CONFLICT | OR ABORT] had been
9706** specified as part of the users SQL statement, regardless of the actual
9707** ON CONFLICT mode specified.
9708**
9709** If X is non-zero, then the virtual table implementation guarantees
9710** that if [xUpdate] returns [SQLITE_CONSTRAINT], it will do so before
9711** any modifications to internal or persistent data structures have been made.
9712** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite
9713** is able to roll back a statement or database transaction, and abandon
9714** or continue processing the current SQL statement as appropriate.
9715** If the ON CONFLICT mode is REPLACE and the [xUpdate] method returns
9716** [SQLITE_CONSTRAINT], SQLite handles this as if the ON CONFLICT mode
9717** had been ABORT.
9718**
9719** Virtual table implementations that are required to handle OR REPLACE
9720** must do so within the [xUpdate] method. If a call to the
9721** [sqlite3_vtab_on_conflict()] function indicates that the current ON
9722** CONFLICT policy is REPLACE, the virtual table implementation should
9723** silently replace the appropriate rows within the xUpdate callback and
9724** return SQLITE_OK. Or, if this is not possible, it may return
9725** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT
9726** constraint handling.
9727** </dl>
9728*/
9729#define SQLITE_VTAB_CONSTRAINT_SUPPORT 1
9730
9731/*
9732** CAPI3REF: Determine The Virtual Table Conflict Policy
9733**
9734** This function may only be called from within a call to the [xUpdate] method
9735** of a [virtual table] implementation for an INSERT or UPDATE operation. ^The
9736** value returned is one of [SQLITE_ROLLBACK], [SQLITE_IGNORE], [SQLITE_FAIL],
9737** [SQLITE_ABORT], or [SQLITE_REPLACE], according to the [ON CONFLICT] mode
9738** of the SQL statement that triggered the call to the [xUpdate] method of the
9739** [virtual table].
9740*/
9741SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
9742
9743/*
9744** CAPI3REF: Determine If Virtual Table Column Access Is For UPDATE
9745**
9746** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn]
9747** method of a [virtual table], then it returns true if and only if the
9748** column is being fetched as part of an UPDATE operation during which the
9749** column value will not change. Applications might use this to substitute
9750** a return value that is less expensive to compute and that the corresponding
9751** [xUpdate] method understands as a "no-change" value.
9752**
9753** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that
9754** the column is not changed by the UPDATE statement, then the xColumn
9755** method can optionally return without setting a result, without calling
9756** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces].
9757** In that case, [sqlite3_value_nochange(X)] will return true for the
9758** same column in the [xUpdate] method.
9759*/
9760SQLITE_API int sqlite3_vtab_nochange(sqlite3_context*);
9761
9762/*
9763** CAPI3REF: Determine The Collation For a Virtual Table Constraint
9764**
9765** This function may only be called from within a call to the [xBestIndex]
9766** method of a [virtual table].
9767**
9768** The first argument must be the sqlite3_index_info object that is the
9769** first parameter to the xBestIndex() method. The second argument must be
9770** an index into the aConstraint[] array belonging to the sqlite3_index_info
9771** structure passed to xBestIndex. This function returns a pointer to a buffer
9772** containing the name of the collation sequence for the corresponding
9773** constraint.
9774*/
9775SQLITE_API SQLITE_EXPERIMENTAL const char *sqlite3_vtab_collation(sqlite3_index_info*,int);
9776
9777/*
9778** CAPI3REF: Conflict resolution modes
9779** KEYWORDS: {conflict resolution mode}
9780**
9781** These constants are returned by [sqlite3_vtab_on_conflict()] to
9782** inform a [virtual table] implementation what the [ON CONFLICT] mode
9783** is for the SQL statement being evaluated.
9784**
9785** Note that the [SQLITE_IGNORE] constant is also used as a potential
9786** return value from the [sqlite3_set_authorizer()] callback and that
9787** [SQLITE_ABORT] is also a [result code].
9788*/
9789#define SQLITE_ROLLBACK 1
9790/* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
9791#define SQLITE_FAIL 3
9792/* #define SQLITE_ABORT 4 // Also an error code */
9793#define SQLITE_REPLACE 5
9794
9795/*
9796** CAPI3REF: Prepared Statement Scan Status Opcodes
9797** KEYWORDS: {scanstatus options}
9798**
9799** The following constants can be used for the T parameter to the
9800** [sqlite3_stmt_scanstatus(S,X,T,V)] interface. Each constant designates a
9801** different metric for sqlite3_stmt_scanstatus() to return.
9802**
9803** When the value returned to V is a string, space to hold that string is
9804** managed by the prepared statement S and will be automatically freed when
9805** S is finalized.
9806**
9807** <dl>
9808** [[SQLITE_SCANSTAT_NLOOP]] <dt>SQLITE_SCANSTAT_NLOOP</dt>
9809** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be
9810** set to the total number of times that the X-th loop has run.</dd>
9811**
9812** [[SQLITE_SCANSTAT_NVISIT]] <dt>SQLITE_SCANSTAT_NVISIT</dt>
9813** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set
9814** to the total number of rows examined by all iterations of the X-th loop.</dd>
9815**
9816** [[SQLITE_SCANSTAT_EST]] <dt>SQLITE_SCANSTAT_EST</dt>
9817** <dd>^The "double" variable pointed to by the T parameter will be set to the
9818** query planner's estimate for the average number of rows output from each
9819** iteration of the X-th loop. If the query planner's estimates was accurate,
9820** then this value will approximate the quotient NVISIT/NLOOP and the
9821** product of this value for all prior loops with the same SELECTID will
9822** be the NLOOP value for the current loop.
9823**
9824** [[SQLITE_SCANSTAT_NAME]] <dt>SQLITE_SCANSTAT_NAME</dt>
9825** <dd>^The "const char *" variable pointed to by the T parameter will be set
9826** to a zero-terminated UTF-8 string containing the name of the index or table
9827** used for the X-th loop.
9828**
9829** [[SQLITE_SCANSTAT_EXPLAIN]] <dt>SQLITE_SCANSTAT_EXPLAIN</dt>
9830** <dd>^The "const char *" variable pointed to by the T parameter will be set
9831** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN]
9832** description for the X-th loop.
9833**
9834** [[SQLITE_SCANSTAT_SELECTID]] <dt>SQLITE_SCANSTAT_SELECT</dt>
9835** <dd>^The "int" variable pointed to by the T parameter will be set to the
9836** "select-id" for the X-th loop. The select-id identifies which query or
9837** subquery the loop is part of. The main query has a select-id of zero.
9838** The select-id is the same value as is output in the first column
9839** of an [EXPLAIN QUERY PLAN] query.
9840** </dl>
9841*/
9842#define SQLITE_SCANSTAT_NLOOP 0
9843#define SQLITE_SCANSTAT_NVISIT 1
9844#define SQLITE_SCANSTAT_EST 2
9845#define SQLITE_SCANSTAT_NAME 3
9846#define SQLITE_SCANSTAT_EXPLAIN 4
9847#define SQLITE_SCANSTAT_SELECTID 5
9848
9849/*
9850** CAPI3REF: Prepared Statement Scan Status
9851** METHOD: sqlite3_stmt
9852**
9853** This interface returns information about the predicted and measured
9854** performance for pStmt. Advanced applications can use this
9855** interface to compare the predicted and the measured performance and
9856** issue warnings and/or rerun [ANALYZE] if discrepancies are found.
9857**
9858** Since this interface is expected to be rarely used, it is only
9859** available if SQLite is compiled using the [SQLITE_ENABLE_STMT_SCANSTATUS]
9860** compile-time option.
9861**
9862** The "iScanStatusOp" parameter determines which status information to return.
9863** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior
9864** of this interface is undefined.
9865** ^The requested measurement is written into a variable pointed to by
9866** the "pOut" parameter.
9867** Parameter "idx" identifies the specific loop to retrieve statistics for.
9868** Loops are numbered starting from zero. ^If idx is out of range - less than
9869** zero or greater than or equal to the total number of loops used to implement
9870** the statement - a non-zero value is returned and the variable that pOut
9871** points to is unchanged.
9872**
9873** ^Statistics might not be available for all loops in all statements. ^In cases
9874** where there exist loops with no available statistics, this function behaves
9875** as if the loop did not exist - it returns non-zero and leave the variable
9876** that pOut points to unchanged.
9877**
9878** See also: [sqlite3_stmt_scanstatus_reset()]
9879*/
9880SQLITE_API int sqlite3_stmt_scanstatus(
9881 sqlite3_stmt *pStmt, /* Prepared statement for which info desired */
9882 int idx, /* Index of loop to report on */
9883 int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */
9884 void *pOut /* Result written here */
9885);
9886
9887/*
9888** CAPI3REF: Zero Scan-Status Counters
9889** METHOD: sqlite3_stmt
9890**
9891** ^Zero all [sqlite3_stmt_scanstatus()] related event counters.
9892**
9893** This API is only available if the library is built with pre-processor
9894** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
9895*/
9896SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
9897
9898/*
9899** CAPI3REF: Flush caches to disk mid-transaction
9900**
9901** ^If a write-transaction is open on [database connection] D when the
9902** [sqlite3_db_cacheflush(D)] interface invoked, any dirty
9903** pages in the pager-cache that are not currently in use are written out
9904** to disk. A dirty page may be in use if a database cursor created by an
9905** active SQL statement is reading from it, or if it is page 1 of a database
9906** file (page 1 is always "in use"). ^The [sqlite3_db_cacheflush(D)]
9907** interface flushes caches for all schemas - "main", "temp", and
9908** any [attached] databases.
9909**
9910** ^If this function needs to obtain extra database locks before dirty pages
9911** can be flushed to disk, it does so. ^If those locks cannot be obtained
9912** immediately and there is a busy-handler callback configured, it is invoked
9913** in the usual manner. ^If the required lock still cannot be obtained, then
9914** the database is skipped and an attempt made to flush any dirty pages
9915** belonging to the next (if any) database. ^If any databases are skipped
9916** because locks cannot be obtained, but no other error occurs, this
9917** function returns SQLITE_BUSY.
9918**
9919** ^If any other error occurs while flushing dirty pages to disk (for
9920** example an IO error or out-of-memory condition), then processing is
9921** abandoned and an SQLite [error code] is returned to the caller immediately.
9922**
9923** ^Otherwise, if no error occurs, [sqlite3_db_cacheflush()] returns SQLITE_OK.
9924**
9925** ^This function does not set the database handle error code or message
9926** returned by the [sqlite3_errcode()] and [sqlite3_errmsg()] functions.
9927*/
9928SQLITE_API int sqlite3_db_cacheflush(sqlite3*);
9929
9930/*
9931** CAPI3REF: The pre-update hook.
9932**
9933** ^These interfaces are only available if SQLite is compiled using the
9934** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
9935**
9936** ^The [sqlite3_preupdate_hook()] interface registers a callback function
9937** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
9938** on a database table.
9939** ^At most one preupdate hook may be registered at a time on a single
9940** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
9941** the previous setting.
9942** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
9943** with a NULL pointer as the second parameter.
9944** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
9945** the first parameter to callbacks.
9946**
9947** ^The preupdate hook only fires for changes to real database tables; the
9948** preupdate hook is not invoked for changes to [virtual tables] or to
9949** system tables like sqlite_master or sqlite_stat1.
9950**
9951** ^The second parameter to the preupdate callback is a pointer to
9952** the [database connection] that registered the preupdate hook.
9953** ^The third parameter to the preupdate callback is one of the constants
9954** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the
9955** kind of update operation that is about to occur.
9956** ^(The fourth parameter to the preupdate callback is the name of the
9957** database within the database connection that is being modified. This
9958** will be "main" for the main database or "temp" for TEMP tables or
9959** the name given after the AS keyword in the [ATTACH] statement for attached
9960** databases.)^
9961** ^The fifth parameter to the preupdate callback is the name of the
9962** table that is being modified.
9963**
9964** For an UPDATE or DELETE operation on a [rowid table], the sixth
9965** parameter passed to the preupdate callback is the initial [rowid] of the
9966** row being modified or deleted. For an INSERT operation on a rowid table,
9967** or any operation on a WITHOUT ROWID table, the value of the sixth
9968** parameter is undefined. For an INSERT or UPDATE on a rowid table the
9969** seventh parameter is the final rowid value of the row being inserted
9970** or updated. The value of the seventh parameter passed to the callback
9971** function is not defined for operations on WITHOUT ROWID tables, or for
9972** INSERT operations on rowid tables.
9973**
9974** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
9975** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
9976** provide additional information about a preupdate event. These routines
9977** may only be called from within a preupdate callback. Invoking any of
9978** these routines from outside of a preupdate callback or with a
9979** [database connection] pointer that is different from the one supplied
9980** to the preupdate callback results in undefined and probably undesirable
9981** behavior.
9982**
9983** ^The [sqlite3_preupdate_count(D)] interface returns the number of columns
9984** in the row that is being inserted, updated, or deleted.
9985**
9986** ^The [sqlite3_preupdate_old(D,N,P)] interface writes into P a pointer to
9987** a [protected sqlite3_value] that contains the value of the Nth column of
9988** the table row before it is updated. The N parameter must be between 0
9989** and one less than the number of columns or the behavior will be
9990** undefined. This must only be used within SQLITE_UPDATE and SQLITE_DELETE
9991** preupdate callbacks; if it is used by an SQLITE_INSERT callback then the
9992** behavior is undefined. The [sqlite3_value] that P points to
9993** will be destroyed when the preupdate callback returns.
9994**
9995** ^The [sqlite3_preupdate_new(D,N,P)] interface writes into P a pointer to
9996** a [protected sqlite3_value] that contains the value of the Nth column of
9997** the table row after it is updated. The N parameter must be between 0
9998** and one less than the number of columns or the behavior will be
9999** undefined. This must only be used within SQLITE_INSERT and SQLITE_UPDATE
10000** preupdate callbacks; if it is used by an SQLITE_DELETE callback then the
10001** behavior is undefined. The [sqlite3_value] that P points to
10002** will be destroyed when the preupdate callback returns.
10003**
10004** ^The [sqlite3_preupdate_depth(D)] interface returns 0 if the preupdate
10005** callback was invoked as a result of a direct insert, update, or delete
10006** operation; or 1 for inserts, updates, or deletes invoked by top-level
10007** triggers; or 2 for changes resulting from triggers called by top-level
10008** triggers; and so forth.
10009**
10010** See also: [sqlite3_update_hook()]
10011*/
10012#if defined(SQLITE_ENABLE_PREUPDATE_HOOK)
10013SQLITE_API void *sqlite3_preupdate_hook(
10014 sqlite3 *db,
10015 void(*xPreUpdate)(
10016 void *pCtx, /* Copy of third arg to preupdate_hook() */
10017 sqlite3 *db, /* Database handle */
10018 int op, /* SQLITE_UPDATE, DELETE or INSERT */
10019 char const *zDb, /* Database name */
10020 char const *zName, /* Table name */
10021 sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */
10022 sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */
10023 ),
10024 void*
10025);
10026SQLITE_API int sqlite3_preupdate_old(sqlite3 *, int, sqlite3_value **);
10027SQLITE_API int sqlite3_preupdate_count(sqlite3 *);
10028SQLITE_API int sqlite3_preupdate_depth(sqlite3 *);
10029SQLITE_API int sqlite3_preupdate_new(sqlite3 *, int, sqlite3_value **);
10030#endif
10031
10032/*
10033** CAPI3REF: Low-level system error code
10034**
10035** ^Attempt to return the underlying operating system error code or error
10036** number that caused the most recent I/O error or failure to open a file.
10037** The return value is OS-dependent. For example, on unix systems, after
10038** [sqlite3_open_v2()] returns [SQLITE_CANTOPEN], this interface could be
10039** called to get back the underlying "errno" that caused the problem, such
10040** as ENOSPC, EAUTH, EISDIR, and so forth.
10041*/
10042SQLITE_API int sqlite3_system_errno(sqlite3*);
10043
10044/*
10045** CAPI3REF: Database Snapshot
10046** KEYWORDS: {snapshot} {sqlite3_snapshot}
10047**
10048** An instance of the snapshot object records the state of a [WAL mode]
10049** database for some specific point in history.
10050**
10051** In [WAL mode], multiple [database connections] that are open on the
10052** same database file can each be reading a different historical version
10053** of the database file. When a [database connection] begins a read
10054** transaction, that connection sees an unchanging copy of the database
10055** as it existed for the point in time when the transaction first started.
10056** Subsequent changes to the database from other connections are not seen
10057** by the reader until a new read transaction is started.
10058**
10059** The sqlite3_snapshot object records state information about an historical
10060** version of the database file so that it is possible to later open a new read
10061** transaction that sees that historical version of the database rather than
10062** the most recent version.
10063*/
10064typedef struct sqlite3_snapshot {
10065 unsigned char hidden[48];
10066} sqlite3_snapshot;
10067
10068/*
10069** CAPI3REF: Record A Database Snapshot
10070** CONSTRUCTOR: sqlite3_snapshot
10071**
10072** ^The [sqlite3_snapshot_get(D,S,P)] interface attempts to make a
10073** new [sqlite3_snapshot] object that records the current state of
10074** schema S in database connection D. ^On success, the
10075** [sqlite3_snapshot_get(D,S,P)] interface writes a pointer to the newly
10076** created [sqlite3_snapshot] object into *P and returns SQLITE_OK.
10077** If there is not already a read-transaction open on schema S when
10078** this function is called, one is opened automatically.
10079**
10080** The following must be true for this function to succeed. If any of
10081** the following statements are false when sqlite3_snapshot_get() is
10082** called, SQLITE_ERROR is returned. The final value of *P is undefined
10083** in this case.
10084**
10085** <ul>
10086** <li> The database handle must not be in [autocommit mode].
10087**
10088** <li> Schema S of [database connection] D must be a [WAL mode] database.
10089**
10090** <li> There must not be a write transaction open on schema S of database
10091** connection D.
10092**
10093** <li> One or more transactions must have been written to the current wal
10094** file since it was created on disk (by any connection). This means
10095** that a snapshot cannot be taken on a wal mode database with no wal
10096** file immediately after it is first opened. At least one transaction
10097** must be written to it first.
10098** </ul>
10099**
10100** This function may also return SQLITE_NOMEM. If it is called with the
10101** database handle in autocommit mode but fails for some other reason,
10102** whether or not a read transaction is opened on schema S is undefined.
10103**
10104** The [sqlite3_snapshot] object returned from a successful call to
10105** [sqlite3_snapshot_get()] must be freed using [sqlite3_snapshot_free()]
10106** to avoid a memory leak.
10107**
10108** The [sqlite3_snapshot_get()] interface is only available when the
10109** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10110*/
10111SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_get(
10112 sqlite3 *db,
10113 const char *zSchema,
10114 sqlite3_snapshot **ppSnapshot
10115);
10116
10117/*
10118** CAPI3REF: Start a read transaction on an historical snapshot
10119** METHOD: sqlite3_snapshot
10120**
10121** ^The [sqlite3_snapshot_open(D,S,P)] interface either starts a new read
10122** transaction or upgrades an existing one for schema S of
10123** [database connection] D such that the read transaction refers to
10124** historical [snapshot] P, rather than the most recent change to the
10125** database. ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK
10126** on success or an appropriate [error code] if it fails.
10127**
10128** ^In order to succeed, the database connection must not be in
10129** [autocommit mode] when [sqlite3_snapshot_open(D,S,P)] is called. If there
10130** is already a read transaction open on schema S, then the database handle
10131** must have no active statements (SELECT statements that have been passed
10132** to sqlite3_step() but not sqlite3_reset() or sqlite3_finalize()).
10133** SQLITE_ERROR is returned if either of these conditions is violated, or
10134** if schema S does not exist, or if the snapshot object is invalid.
10135**
10136** ^A call to sqlite3_snapshot_open() will fail to open if the specified
10137** snapshot has been overwritten by a [checkpoint]. In this case
10138** SQLITE_ERROR_SNAPSHOT is returned.
10139**
10140** If there is already a read transaction open when this function is
10141** invoked, then the same read transaction remains open (on the same
10142** database snapshot) if SQLITE_ERROR, SQLITE_BUSY or SQLITE_ERROR_SNAPSHOT
10143** is returned. If another error code - for example SQLITE_PROTOCOL or an
10144** SQLITE_IOERR error code - is returned, then the final state of the
10145** read transaction is undefined. If SQLITE_OK is returned, then the
10146** read transaction is now open on database snapshot P.
10147**
10148** ^(A call to [sqlite3_snapshot_open(D,S,P)] will fail if the
10149** database connection D does not know that the database file for
10150** schema S is in [WAL mode]. A database connection might not know
10151** that the database file is in [WAL mode] if there has been no prior
10152** I/O on that database connection, or if the database entered [WAL mode]
10153** after the most recent I/O on the database connection.)^
10154** (Hint: Run "[PRAGMA application_id]" against a newly opened
10155** database connection in order to make it ready to use snapshots.)
10156**
10157** The [sqlite3_snapshot_open()] interface is only available when the
10158** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10159*/
10160SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_open(
10161 sqlite3 *db,
10162 const char *zSchema,
10163 sqlite3_snapshot *pSnapshot
10164);
10165
10166/*
10167** CAPI3REF: Destroy a snapshot
10168** DESTRUCTOR: sqlite3_snapshot
10169**
10170** ^The [sqlite3_snapshot_free(P)] interface destroys [sqlite3_snapshot] P.
10171** The application must eventually free every [sqlite3_snapshot] object
10172** using this routine to avoid a memory leak.
10173**
10174** The [sqlite3_snapshot_free()] interface is only available when the
10175** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10176*/
10177SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_snapshot_free(sqlite3_snapshot*);
10178
10179/*
10180** CAPI3REF: Compare the ages of two snapshot handles.
10181** METHOD: sqlite3_snapshot
10182**
10183** The sqlite3_snapshot_cmp(P1, P2) interface is used to compare the ages
10184** of two valid snapshot handles.
10185**
10186** If the two snapshot handles are not associated with the same database
10187** file, the result of the comparison is undefined.
10188**
10189** Additionally, the result of the comparison is only valid if both of the
10190** snapshot handles were obtained by calling sqlite3_snapshot_get() since the
10191** last time the wal file was deleted. The wal file is deleted when the
10192** database is changed back to rollback mode or when the number of database
10193** clients drops to zero. If either snapshot handle was obtained before the
10194** wal file was last deleted, the value returned by this function
10195** is undefined.
10196**
10197** Otherwise, this API returns a negative value if P1 refers to an older
10198** snapshot than P2, zero if the two handles refer to the same database
10199** snapshot, and a positive value if P1 is a newer snapshot than P2.
10200**
10201** This interface is only available if SQLite is compiled with the
10202** [SQLITE_ENABLE_SNAPSHOT] option.
10203*/
10204SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_cmp(
10205 sqlite3_snapshot *p1,
10206 sqlite3_snapshot *p2
10207);
10208
10209/*
10210** CAPI3REF: Recover snapshots from a wal file
10211** METHOD: sqlite3_snapshot
10212**
10213** If a [WAL file] remains on disk after all database connections close
10214** (either through the use of the [SQLITE_FCNTL_PERSIST_WAL] [file control]
10215** or because the last process to have the database opened exited without
10216** calling [sqlite3_close()]) and a new connection is subsequently opened
10217** on that database and [WAL file], the [sqlite3_snapshot_open()] interface
10218** will only be able to open the last transaction added to the WAL file
10219** even though the WAL file contains other valid transactions.
10220**
10221** This function attempts to scan the WAL file associated with database zDb
10222** of database handle db and make all valid snapshots available to
10223** sqlite3_snapshot_open(). It is an error if there is already a read
10224** transaction open on the database, or if the database is not a WAL mode
10225** database.
10226**
10227** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
10228**
10229** This interface is only available if SQLite is compiled with the
10230** [SQLITE_ENABLE_SNAPSHOT] option.
10231*/
10232SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
10233
10234/*
10235** CAPI3REF: Serialize a database
10236**
10237** The sqlite3_serialize(D,S,P,F) interface returns a pointer to memory
10238** that is a serialization of the S database on [database connection] D.
10239** If P is not a NULL pointer, then the size of the database in bytes
10240** is written into *P.
10241**
10242** For an ordinary on-disk database file, the serialization is just a
10243** copy of the disk file. For an in-memory database or a "TEMP" database,
10244** the serialization is the same sequence of bytes which would be written
10245** to disk if that database where backed up to disk.
10246**
10247** The usual case is that sqlite3_serialize() copies the serialization of
10248** the database into memory obtained from [sqlite3_malloc64()] and returns
10249** a pointer to that memory. The caller is responsible for freeing the
10250** returned value to avoid a memory leak. However, if the F argument
10251** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations
10252** are made, and the sqlite3_serialize() function will return a pointer
10253** to the contiguous memory representation of the database that SQLite
10254** is currently using for that database, or NULL if the no such contiguous
10255** memory representation of the database exists. A contiguous memory
10256** representation of the database will usually only exist if there has
10257** been a prior call to [sqlite3_deserialize(D,S,...)] with the same
10258** values of D and S.
10259** The size of the database is written into *P even if the
10260** SQLITE_SERIALIZE_NOCOPY bit is set but no contiguous copy
10261** of the database exists.
10262**
10263** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
10264** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
10265** allocation error occurs.
10266**
10267** This interface is only available if SQLite is compiled with the
10268** [SQLITE_ENABLE_DESERIALIZE] option.
10269*/
10270SQLITE_API unsigned char *sqlite3_serialize(
10271 sqlite3 *db, /* The database connection */
10272 const char *zSchema, /* Which DB to serialize. ex: "main", "temp", ... */
10273 sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */
10274 unsigned int mFlags /* Zero or more SQLITE_SERIALIZE_* flags */
10275);
10276
10277/*
10278** CAPI3REF: Flags for sqlite3_serialize
10279**
10280** Zero or more of the following constants can be OR-ed together for
10281** the F argument to [sqlite3_serialize(D,S,P,F)].
10282**
10283** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return
10284** a pointer to contiguous in-memory database that it is currently using,
10285** without making a copy of the database. If SQLite is not currently using
10286** a contiguous in-memory database, then this option causes
10287** [sqlite3_serialize()] to return a NULL pointer. SQLite will only be
10288** using a contiguous in-memory database if it has been initialized by a
10289** prior call to [sqlite3_deserialize()].
10290*/
10291#define SQLITE_SERIALIZE_NOCOPY 0x001 /* Do no memory allocations */
10292
10293/*
10294** CAPI3REF: Deserialize a database
10295**
10296** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the
10297** [database connection] D to disconnect from database S and then
10298** reopen S as an in-memory database based on the serialization contained
10299** in P. The serialized database P is N bytes in size. M is the size of
10300** the buffer P, which might be larger than N. If M is larger than N, and
10301** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
10302** permitted to add content to the in-memory database as long as the total
10303** size does not exceed M bytes.
10304**
10305** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will
10306** invoke sqlite3_free() on the serialization buffer when the database
10307** connection closes. If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then
10308** SQLite will try to increase the buffer size using sqlite3_realloc64()
10309** if writes on the database cause it to grow larger than M bytes.
10310**
10311** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the
10312** database is currently in a read transaction or is involved in a backup
10313** operation.
10314**
10315** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the
10316** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then
10317** [sqlite3_free()] is invoked on argument P prior to returning.
10318**
10319** This interface is only available if SQLite is compiled with the
10320** [SQLITE_ENABLE_DESERIALIZE] option.
10321*/
10322SQLITE_API int sqlite3_deserialize(
10323 sqlite3 *db, /* The database connection */
10324 const char *zSchema, /* Which DB to reopen with the deserialization */
10325 unsigned char *pData, /* The serialized database content */
10326 sqlite3_int64 szDb, /* Number bytes in the deserialization */
10327 sqlite3_int64 szBuf, /* Total size of buffer pData[] */
10328 unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */
10329);
10330
10331/*
10332** CAPI3REF: Flags for sqlite3_deserialize()
10333**
10334** The following are allowed values for 6th argument (the F argument) to
10335** the [sqlite3_deserialize(D,S,P,N,M,F)] interface.
10336**
10337** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
10338** in the P argument is held in memory obtained from [sqlite3_malloc64()]
10339** and that SQLite should take ownership of this memory and automatically
10340** free it when it has finished using it. Without this flag, the caller
10341** is responsible for freeing any dynamically allocated memory.
10342**
10343** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
10344** grow the size of the database using calls to [sqlite3_realloc64()]. This
10345** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
10346** Without this flag, the deserialized database cannot increase in size beyond
10347** the number of bytes specified by the M parameter.
10348**
10349** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database
10350** should be treated as read-only.
10351*/
10352#define SQLITE_DESERIALIZE_FREEONCLOSE 1 /* Call sqlite3_free() on close */
10353#define SQLITE_DESERIALIZE_RESIZEABLE 2 /* Resize using sqlite3_realloc64() */
10354#define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */
10355
10356/*
10357** Undo the hack that converts floating point types to integer for
10358** builds on processors without floating point support.
10359*/
10360#ifdef SQLITE_OMIT_FLOATING_POINT
10361# undef double
10362#endif
10363
10364#if 0
10365} /* End of the 'extern "C"' block */
10366#endif
10367#endif /* SQLITE3_H */
10368
10369/******** Begin file sqlite3rtree.h *********/
10370/*
10371** 2010 August 30
10372**
10373** The author disclaims copyright to this source code. In place of
10374** a legal notice, here is a blessing:
10375**
10376** May you do good and not evil.
10377** May you find forgiveness for yourself and forgive others.
10378** May you share freely, never taking more than you give.
10379**
10380*************************************************************************
10381*/
10382
10383#ifndef _SQLITE3RTREE_H_
10384#define _SQLITE3RTREE_H_
10385
10386
10387#if 0
10388extern "C" {
10389#endif
10390
10391typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
10392typedef struct sqlite3_rtree_query_info sqlite3_rtree_query_info;
10393
10394/* The double-precision datatype used by RTree depends on the
10395** SQLITE_RTREE_INT_ONLY compile-time option.
10396*/
10397#ifdef SQLITE_RTREE_INT_ONLY
10398 typedef sqlite3_int64 sqlite3_rtree_dbl;
10399#else
10400 typedef double sqlite3_rtree_dbl;
10401#endif
10402
10403/*
10404** Register a geometry callback named zGeom that can be used as part of an
10405** R-Tree geometry query as follows:
10406**
10407** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
10408*/
10409SQLITE_API int sqlite3_rtree_geometry_callback(
10410 sqlite3 *db,
10411 const char *zGeom,
10412 int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*),
10413 void *pContext
10414);
10415
10416
10417/*
10418** A pointer to a structure of the following type is passed as the first
10419** argument to callbacks registered using rtree_geometry_callback().
10420*/
10421struct sqlite3_rtree_geometry {
10422 void *pContext; /* Copy of pContext passed to s_r_g_c() */
10423 int nParam; /* Size of array aParam[] */
10424 sqlite3_rtree_dbl *aParam; /* Parameters passed to SQL geom function */
10425 void *pUser; /* Callback implementation user data */
10426 void (*xDelUser)(void *); /* Called by SQLite to clean up pUser */
10427};
10428
10429/*
10430** Register a 2nd-generation geometry callback named zScore that can be
10431** used as part of an R-Tree geometry query as follows:
10432**
10433** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zQueryFunc(... params ...)
10434*/
10435SQLITE_API int sqlite3_rtree_query_callback(
10436 sqlite3 *db,
10437 const char *zQueryFunc,
10438 int (*xQueryFunc)(sqlite3_rtree_query_info*),
10439 void *pContext,
10440 void (*xDestructor)(void*)
10441);
10442
10443
10444/*
10445** A pointer to a structure of the following type is passed as the
10446** argument to scored geometry callback registered using
10447** sqlite3_rtree_query_callback().
10448**
10449** Note that the first 5 fields of this structure are identical to
10450** sqlite3_rtree_geometry. This structure is a subclass of
10451** sqlite3_rtree_geometry.
10452*/
10453struct sqlite3_rtree_query_info {
10454 void *pContext; /* pContext from when function registered */
10455 int nParam; /* Number of function parameters */
10456 sqlite3_rtree_dbl *aParam; /* value of function parameters */
10457 void *pUser; /* callback can use this, if desired */
10458 void (*xDelUser)(void*); /* function to free pUser */
10459 sqlite3_rtree_dbl *aCoord; /* Coordinates of node or entry to check */
10460 unsigned int *anQueue; /* Number of pending entries in the queue */
10461 int nCoord; /* Number of coordinates */
10462 int iLevel; /* Level of current node or entry */
10463 int mxLevel; /* The largest iLevel value in the tree */
10464 sqlite3_int64 iRowid; /* Rowid for current entry */
10465 sqlite3_rtree_dbl rParentScore; /* Score of parent node */
10466 int eParentWithin; /* Visibility of parent node */
10467 int eWithin; /* OUT: Visibility */
10468 sqlite3_rtree_dbl rScore; /* OUT: Write the score here */
10469 /* The following fields are only available in 3.8.11 and later */
10470 sqlite3_value **apSqlParam; /* Original SQL values of parameters */
10471};
10472
10473/*
10474** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
10475*/
10476#define NOT_WITHIN 0 /* Object completely outside of query region */
10477#define PARTLY_WITHIN 1 /* Object partially overlaps query region */
10478#define FULLY_WITHIN 2 /* Object fully contained within query region */
10479
10480
10481#if 0
10482} /* end of the 'extern "C"' block */
10483#endif
10484
10485#endif /* ifndef _SQLITE3RTREE_H_ */
10486
10487/******** End of sqlite3rtree.h *********/
10488/******** Begin file sqlite3session.h *********/
10489
10490#if !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION)
10491#define __SQLITESESSION_H_ 1
10492
10493/*
10494** Make sure we can call this stuff from C++.
10495*/
10496#if 0
10497extern "C" {
10498#endif
10499
10500
10501/*
10502** CAPI3REF: Session Object Handle
10503**
10504** An instance of this object is a [session] that can be used to
10505** record changes to a database.
10506*/
10507typedef struct sqlite3_session sqlite3_session;
10508
10509/*
10510** CAPI3REF: Changeset Iterator Handle
10511**
10512** An instance of this object acts as a cursor for iterating
10513** over the elements of a [changeset] or [patchset].
10514*/
10515typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
10516
10517/*
10518** CAPI3REF: Create A New Session Object
10519** CONSTRUCTOR: sqlite3_session
10520**
10521** Create a new session object attached to database handle db. If successful,
10522** a pointer to the new object is written to *ppSession and SQLITE_OK is
10523** returned. If an error occurs, *ppSession is set to NULL and an SQLite
10524** error code (e.g. SQLITE_NOMEM) is returned.
10525**
10526** It is possible to create multiple session objects attached to a single
10527** database handle.
10528**
10529** Session objects created using this function should be deleted using the
10530** [sqlite3session_delete()] function before the database handle that they
10531** are attached to is itself closed. If the database handle is closed before
10532** the session object is deleted, then the results of calling any session
10533** module function, including [sqlite3session_delete()] on the session object
10534** are undefined.
10535**
10536** Because the session module uses the [sqlite3_preupdate_hook()] API, it
10537** is not possible for an application to register a pre-update hook on a
10538** database handle that has one or more session objects attached. Nor is
10539** it possible to create a session object attached to a database handle for
10540** which a pre-update hook is already defined. The results of attempting
10541** either of these things are undefined.
10542**
10543** The session object will be used to create changesets for tables in
10544** database zDb, where zDb is either "main", or "temp", or the name of an
10545** attached database. It is not an error if database zDb is not attached
10546** to the database when the session object is created.
10547*/
10548SQLITE_API int sqlite3session_create(
10549 sqlite3 *db, /* Database handle */
10550 const char *zDb, /* Name of db (e.g. "main") */
10551 sqlite3_session **ppSession /* OUT: New session object */
10552);
10553
10554/*
10555** CAPI3REF: Delete A Session Object
10556** DESTRUCTOR: sqlite3_session
10557**
10558** Delete a session object previously allocated using
10559** [sqlite3session_create()]. Once a session object has been deleted, the
10560** results of attempting to use pSession with any other session module
10561** function are undefined.
10562**
10563** Session objects must be deleted before the database handle to which they
10564** are attached is closed. Refer to the documentation for
10565** [sqlite3session_create()] for details.
10566*/
10567SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
10568
10569
10570/*
10571** CAPI3REF: Enable Or Disable A Session Object
10572** METHOD: sqlite3_session
10573**
10574** Enable or disable the recording of changes by a session object. When
10575** enabled, a session object records changes made to the database. When
10576** disabled - it does not. A newly created session object is enabled.
10577** Refer to the documentation for [sqlite3session_changeset()] for further
10578** details regarding how enabling and disabling a session object affects
10579** the eventual changesets.
10580**
10581** Passing zero to this function disables the session. Passing a value
10582** greater than zero enables it. Passing a value less than zero is a
10583** no-op, and may be used to query the current state of the session.
10584**
10585** The return value indicates the final state of the session object: 0 if
10586** the session is disabled, or 1 if it is enabled.
10587*/
10588SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
10589
10590/*
10591** CAPI3REF: Set Or Clear the Indirect Change Flag
10592** METHOD: sqlite3_session
10593**
10594** Each change recorded by a session object is marked as either direct or
10595** indirect. A change is marked as indirect if either:
10596**
10597** <ul>
10598** <li> The session object "indirect" flag is set when the change is
10599** made, or
10600** <li> The change is made by an SQL trigger or foreign key action
10601** instead of directly as a result of a users SQL statement.
10602** </ul>
10603**
10604** If a single row is affected by more than one operation within a session,
10605** then the change is considered indirect if all operations meet the criteria
10606** for an indirect change above, or direct otherwise.
10607**
10608** This function is used to set, clear or query the session object indirect
10609** flag. If the second argument passed to this function is zero, then the
10610** indirect flag is cleared. If it is greater than zero, the indirect flag
10611** is set. Passing a value less than zero does not modify the current value
10612** of the indirect flag, and may be used to query the current state of the
10613** indirect flag for the specified session object.
10614**
10615** The return value indicates the final state of the indirect flag: 0 if
10616** it is clear, or 1 if it is set.
10617*/
10618SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
10619
10620/*
10621** CAPI3REF: Attach A Table To A Session Object
10622** METHOD: sqlite3_session
10623**
10624** If argument zTab is not NULL, then it is the name of a table to attach
10625** to the session object passed as the first argument. All subsequent changes
10626** made to the table while the session object is enabled will be recorded. See
10627** documentation for [sqlite3session_changeset()] for further details.
10628**
10629** Or, if argument zTab is NULL, then changes are recorded for all tables
10630** in the database. If additional tables are added to the database (by
10631** executing "CREATE TABLE" statements) after this call is made, changes for
10632** the new tables are also recorded.
10633**
10634** Changes can only be recorded for tables that have a PRIMARY KEY explicitly
10635** defined as part of their CREATE TABLE statement. It does not matter if the
10636** PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) or not. The PRIMARY
10637** KEY may consist of a single column, or may be a composite key.
10638**
10639** It is not an error if the named table does not exist in the database. Nor
10640** is it an error if the named table does not have a PRIMARY KEY. However,
10641** no changes will be recorded in either of these scenarios.
10642**
10643** Changes are not recorded for individual rows that have NULL values stored
10644** in one or more of their PRIMARY KEY columns.
10645**
10646** SQLITE_OK is returned if the call completes without error. Or, if an error
10647** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
10648**
10649** <h3>Special sqlite_stat1 Handling</h3>
10650**
10651** As of SQLite version 3.22.0, the "sqlite_stat1" table is an exception to
10652** some of the rules above. In SQLite, the schema of sqlite_stat1 is:
10653** <pre>
10654** &nbsp; CREATE TABLE sqlite_stat1(tbl,idx,stat)
10655** </pre>
10656**
10657** Even though sqlite_stat1 does not have a PRIMARY KEY, changes are
10658** recorded for it as if the PRIMARY KEY is (tbl,idx). Additionally, changes
10659** are recorded for rows for which (idx IS NULL) is true. However, for such
10660** rows a zero-length blob (SQL value X'') is stored in the changeset or
10661** patchset instead of a NULL value. This allows such changesets to be
10662** manipulated by legacy implementations of sqlite3changeset_invert(),
10663** concat() and similar.
10664**
10665** The sqlite3changeset_apply() function automatically converts the
10666** zero-length blob back to a NULL value when updating the sqlite_stat1
10667** table. However, if the application calls sqlite3changeset_new(),
10668** sqlite3changeset_old() or sqlite3changeset_conflict on a changeset
10669** iterator directly (including on a changeset iterator passed to a
10670** conflict-handler callback) then the X'' value is returned. The application
10671** must translate X'' to NULL itself if required.
10672**
10673** Legacy (older than 3.22.0) versions of the sessions module cannot capture
10674** changes made to the sqlite_stat1 table. Legacy versions of the
10675** sqlite3changeset_apply() function silently ignore any modifications to the
10676** sqlite_stat1 table that are part of a changeset or patchset.
10677*/
10678SQLITE_API int sqlite3session_attach(
10679 sqlite3_session *pSession, /* Session object */
10680 const char *zTab /* Table name */
10681);
10682
10683/*
10684** CAPI3REF: Set a table filter on a Session Object.
10685** METHOD: sqlite3_session
10686**
10687** The second argument (xFilter) is the "filter callback". For changes to rows
10688** in tables that are not attached to the Session object, the filter is called
10689** to determine whether changes to the table's rows should be tracked or not.
10690** If xFilter returns 0, changes is not tracked. Note that once a table is
10691** attached, xFilter will not be called again.
10692*/
10693SQLITE_API void sqlite3session_table_filter(
10694 sqlite3_session *pSession, /* Session object */
10695 int(*xFilter)(
10696 void *pCtx, /* Copy of third arg to _filter_table() */
10697 const char *zTab /* Table name */
10698 ),
10699 void *pCtx /* First argument passed to xFilter */
10700);
10701
10702/*
10703** CAPI3REF: Generate A Changeset From A Session Object
10704** METHOD: sqlite3_session
10705**
10706** Obtain a changeset containing changes to the tables attached to the
10707** session object passed as the first argument. If successful,
10708** set *ppChangeset to point to a buffer containing the changeset
10709** and *pnChangeset to the size of the changeset in bytes before returning
10710** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
10711** zero and return an SQLite error code.
10712**
10713** A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes,
10714** each representing a change to a single row of an attached table. An INSERT
10715** change contains the values of each field of a new database row. A DELETE
10716** contains the original values of each field of a deleted database row. An
10717** UPDATE change contains the original values of each field of an updated
10718** database row along with the updated values for each updated non-primary-key
10719** column. It is not possible for an UPDATE change to represent a change that
10720** modifies the values of primary key columns. If such a change is made, it
10721** is represented in a changeset as a DELETE followed by an INSERT.
10722**
10723** Changes are not recorded for rows that have NULL values stored in one or
10724** more of their PRIMARY KEY columns. If such a row is inserted or deleted,
10725** no corresponding change is present in the changesets returned by this
10726** function. If an existing row with one or more NULL values stored in
10727** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
10728** only an INSERT is appears in the changeset. Similarly, if an existing row
10729** with non-NULL PRIMARY KEY values is updated so that one or more of its
10730** PRIMARY KEY columns are set to NULL, the resulting changeset contains a
10731** DELETE change only.
10732**
10733** The contents of a changeset may be traversed using an iterator created
10734** using the [sqlite3changeset_start()] API. A changeset may be applied to
10735** a database with a compatible schema using the [sqlite3changeset_apply()]
10736** API.
10737**
10738** Within a changeset generated by this function, all changes related to a
10739** single table are grouped together. In other words, when iterating through
10740** a changeset or when applying a changeset to a database, all changes related
10741** to a single table are processed before moving on to the next table. Tables
10742** are sorted in the same order in which they were attached (or auto-attached)
10743** to the sqlite3_session object. The order in which the changes related to
10744** a single table are stored is undefined.
10745**
10746** Following a successful call to this function, it is the responsibility of
10747** the caller to eventually free the buffer that *ppChangeset points to using
10748** [sqlite3_free()].
10749**
10750** <h3>Changeset Generation</h3>
10751**
10752** Once a table has been attached to a session object, the session object
10753** records the primary key values of all new rows inserted into the table.
10754** It also records the original primary key and other column values of any
10755** deleted or updated rows. For each unique primary key value, data is only
10756** recorded once - the first time a row with said primary key is inserted,
10757** updated or deleted in the lifetime of the session.
10758**
10759** There is one exception to the previous paragraph: when a row is inserted,
10760** updated or deleted, if one or more of its primary key columns contain a
10761** NULL value, no record of the change is made.
10762**
10763** The session object therefore accumulates two types of records - those
10764** that consist of primary key values only (created when the user inserts
10765** a new record) and those that consist of the primary key values and the
10766** original values of other table columns (created when the users deletes
10767** or updates a record).
10768**
10769** When this function is called, the requested changeset is created using
10770** both the accumulated records and the current contents of the database
10771** file. Specifically:
10772**
10773** <ul>
10774** <li> For each record generated by an insert, the database is queried
10775** for a row with a matching primary key. If one is found, an INSERT
10776** change is added to the changeset. If no such row is found, no change
10777** is added to the changeset.
10778**
10779** <li> For each record generated by an update or delete, the database is
10780** queried for a row with a matching primary key. If such a row is
10781** found and one or more of the non-primary key fields have been
10782** modified from their original values, an UPDATE change is added to
10783** the changeset. Or, if no such row is found in the table, a DELETE
10784** change is added to the changeset. If there is a row with a matching
10785** primary key in the database, but all fields contain their original
10786** values, no change is added to the changeset.
10787** </ul>
10788**
10789** This means, amongst other things, that if a row is inserted and then later
10790** deleted while a session object is active, neither the insert nor the delete
10791** will be present in the changeset. Or if a row is deleted and then later a
10792** row with the same primary key values inserted while a session object is
10793** active, the resulting changeset will contain an UPDATE change instead of
10794** a DELETE and an INSERT.
10795**
10796** When a session object is disabled (see the [sqlite3session_enable()] API),
10797** it does not accumulate records when rows are inserted, updated or deleted.
10798** This may appear to have some counter-intuitive effects if a single row
10799** is written to more than once during a session. For example, if a row
10800** is inserted while a session object is enabled, then later deleted while
10801** the same session object is disabled, no INSERT record will appear in the
10802** changeset, even though the delete took place while the session was disabled.
10803** Or, if one field of a row is updated while a session is disabled, and
10804** another field of the same row is updated while the session is enabled, the
10805** resulting changeset will contain an UPDATE change that updates both fields.
10806*/
10807SQLITE_API int sqlite3session_changeset(
10808 sqlite3_session *pSession, /* Session object */
10809 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
10810 void **ppChangeset /* OUT: Buffer containing changeset */
10811);
10812
10813/*
10814** CAPI3REF: Load The Difference Between Tables Into A Session
10815** METHOD: sqlite3_session
10816**
10817** If it is not already attached to the session object passed as the first
10818** argument, this function attaches table zTbl in the same manner as the
10819** [sqlite3session_attach()] function. If zTbl does not exist, or if it
10820** does not have a primary key, this function is a no-op (but does not return
10821** an error).
10822**
10823** Argument zFromDb must be the name of a database ("main", "temp" etc.)
10824** attached to the same database handle as the session object that contains
10825** a table compatible with the table attached to the session by this function.
10826** A table is considered compatible if it:
10827**
10828** <ul>
10829** <li> Has the same name,
10830** <li> Has the same set of columns declared in the same order, and
10831** <li> Has the same PRIMARY KEY definition.
10832** </ul>
10833**
10834** If the tables are not compatible, SQLITE_SCHEMA is returned. If the tables
10835** are compatible but do not have any PRIMARY KEY columns, it is not an error
10836** but no changes are added to the session object. As with other session
10837** APIs, tables without PRIMARY KEYs are simply ignored.
10838**
10839** This function adds a set of changes to the session object that could be
10840** used to update the table in database zFrom (call this the "from-table")
10841** so that its content is the same as the table attached to the session
10842** object (call this the "to-table"). Specifically:
10843**
10844** <ul>
10845** <li> For each row (primary key) that exists in the to-table but not in
10846** the from-table, an INSERT record is added to the session object.
10847**
10848** <li> For each row (primary key) that exists in the to-table but not in
10849** the from-table, a DELETE record is added to the session object.
10850**
10851** <li> For each row (primary key) that exists in both tables, but features
10852** different non-PK values in each, an UPDATE record is added to the
10853** session.
10854** </ul>
10855**
10856** To clarify, if this function is called and then a changeset constructed
10857** using [sqlite3session_changeset()], then after applying that changeset to
10858** database zFrom the contents of the two compatible tables would be
10859** identical.
10860**
10861** It an error if database zFrom does not exist or does not contain the
10862** required compatible table.
10863**
10864** If the operation successful, SQLITE_OK is returned. Otherwise, an SQLite
10865** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
10866** may be set to point to a buffer containing an English language error
10867** message. It is the responsibility of the caller to free this buffer using
10868** sqlite3_free().
10869*/
10870SQLITE_API int sqlite3session_diff(
10871 sqlite3_session *pSession,
10872 const char *zFromDb,
10873 const char *zTbl,
10874 char **pzErrMsg
10875);
10876
10877
10878/*
10879** CAPI3REF: Generate A Patchset From A Session Object
10880** METHOD: sqlite3_session
10881**
10882** The differences between a patchset and a changeset are that:
10883**
10884** <ul>
10885** <li> DELETE records consist of the primary key fields only. The
10886** original values of other fields are omitted.
10887** <li> The original values of any modified fields are omitted from
10888** UPDATE records.
10889** </ul>
10890**
10891** A patchset blob may be used with up to date versions of all
10892** sqlite3changeset_xxx API functions except for sqlite3changeset_invert(),
10893** which returns SQLITE_CORRUPT if it is passed a patchset. Similarly,
10894** attempting to use a patchset blob with old versions of the
10895** sqlite3changeset_xxx APIs also provokes an SQLITE_CORRUPT error.
10896**
10897** Because the non-primary key "old.*" fields are omitted, no
10898** SQLITE_CHANGESET_DATA conflicts can be detected or reported if a patchset
10899** is passed to the sqlite3changeset_apply() API. Other conflict types work
10900** in the same way as for changesets.
10901**
10902** Changes within a patchset are ordered in the same way as for changesets
10903** generated by the sqlite3session_changeset() function (i.e. all changes for
10904** a single table are grouped together, tables appear in the order in which
10905** they were attached to the session object).
10906*/
10907SQLITE_API int sqlite3session_patchset(
10908 sqlite3_session *pSession, /* Session object */
10909 int *pnPatchset, /* OUT: Size of buffer at *ppPatchset */
10910 void **ppPatchset /* OUT: Buffer containing patchset */
10911);
10912
10913/*
10914** CAPI3REF: Test if a changeset has recorded any changes.
10915**
10916** Return non-zero if no changes to attached tables have been recorded by
10917** the session object passed as the first argument. Otherwise, if one or
10918** more changes have been recorded, return zero.
10919**
10920** Even if this function returns zero, it is possible that calling
10921** [sqlite3session_changeset()] on the session handle may still return a
10922** changeset that contains no changes. This can happen when a row in
10923** an attached table is modified and then later on the original values
10924** are restored. However, if this function returns non-zero, then it is
10925** guaranteed that a call to sqlite3session_changeset() will return a
10926** changeset containing zero changes.
10927*/
10928SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
10929
10930/*
10931** CAPI3REF: Create An Iterator To Traverse A Changeset
10932** CONSTRUCTOR: sqlite3_changeset_iter
10933**
10934** Create an iterator used to iterate through the contents of a changeset.
10935** If successful, *pp is set to point to the iterator handle and SQLITE_OK
10936** is returned. Otherwise, if an error occurs, *pp is set to zero and an
10937** SQLite error code is returned.
10938**
10939** The following functions can be used to advance and query a changeset
10940** iterator created by this function:
10941**
10942** <ul>
10943** <li> [sqlite3changeset_next()]
10944** <li> [sqlite3changeset_op()]
10945** <li> [sqlite3changeset_new()]
10946** <li> [sqlite3changeset_old()]
10947** </ul>
10948**
10949** It is the responsibility of the caller to eventually destroy the iterator
10950** by passing it to [sqlite3changeset_finalize()]. The buffer containing the
10951** changeset (pChangeset) must remain valid until after the iterator is
10952** destroyed.
10953**
10954** Assuming the changeset blob was created by one of the
10955** [sqlite3session_changeset()], [sqlite3changeset_concat()] or
10956** [sqlite3changeset_invert()] functions, all changes within the changeset
10957** that apply to a single table are grouped together. This means that when
10958** an application iterates through a changeset using an iterator created by
10959** this function, all changes that relate to a single table are visited
10960** consecutively. There is no chance that the iterator will visit a change
10961** the applies to table X, then one for table Y, and then later on visit
10962** another change for table X.
10963**
10964** The behavior of sqlite3changeset_start_v2() and its streaming equivalent
10965** may be modified by passing a combination of
10966** [SQLITE_CHANGESETSTART_INVERT | supported flags] as the 4th parameter.
10967**
10968** Note that the sqlite3changeset_start_v2() API is still <b>experimental</b>
10969** and therefore subject to change.
10970*/
10971SQLITE_API int sqlite3changeset_start(
10972 sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */
10973 int nChangeset, /* Size of changeset blob in bytes */
10974 void *pChangeset /* Pointer to blob containing changeset */
10975);
10976SQLITE_API int sqlite3changeset_start_v2(
10977 sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */
10978 int nChangeset, /* Size of changeset blob in bytes */
10979 void *pChangeset, /* Pointer to blob containing changeset */
10980 int flags /* SESSION_CHANGESETSTART_* flags */
10981);
10982
10983/*
10984** CAPI3REF: Flags for sqlite3changeset_start_v2
10985**
10986** The following flags may passed via the 4th parameter to
10987** [sqlite3changeset_start_v2] and [sqlite3changeset_start_v2_strm]:
10988**
10989** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
10990** Invert the changeset while iterating through it. This is equivalent to
10991** inverting a changeset using sqlite3changeset_invert() before applying it.
10992** It is an error to specify this flag with a patchset.
10993*/
10994#define SQLITE_CHANGESETSTART_INVERT 0x0002
10995
10996
10997/*
10998** CAPI3REF: Advance A Changeset Iterator
10999** METHOD: sqlite3_changeset_iter
11000**
11001** This function may only be used with iterators created by function
11002** [sqlite3changeset_start()]. If it is called on an iterator passed to
11003** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
11004** is returned and the call has no effect.
11005**
11006** Immediately after an iterator is created by sqlite3changeset_start(), it
11007** does not point to any change in the changeset. Assuming the changeset
11008** is not empty, the first call to this function advances the iterator to
11009** point to the first change in the changeset. Each subsequent call advances
11010** the iterator to point to the next change in the changeset (if any). If
11011** no error occurs and the iterator points to a valid change after a call
11012** to sqlite3changeset_next() has advanced it, SQLITE_ROW is returned.
11013** Otherwise, if all changes in the changeset have already been visited,
11014** SQLITE_DONE is returned.
11015**
11016** If an error occurs, an SQLite error code is returned. Possible error
11017** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or
11018** SQLITE_NOMEM.
11019*/
11020SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
11021
11022/*
11023** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
11024** METHOD: sqlite3_changeset_iter
11025**
11026** The pIter argument passed to this function may either be an iterator
11027** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11028** created by [sqlite3changeset_start()]. In the latter case, the most recent
11029** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
11030** is not the case, this function returns [SQLITE_MISUSE].
11031**
11032** If argument pzTab is not NULL, then *pzTab is set to point to a
11033** nul-terminated utf-8 encoded string containing the name of the table
11034** affected by the current change. The buffer remains valid until either
11035** sqlite3changeset_next() is called on the iterator or until the
11036** conflict-handler function returns. If pnCol is not NULL, then *pnCol is
11037** set to the number of columns in the table affected by the change. If
11038** pbIncorrect is not NULL, then *pbIndirect is set to true (1) if the change
11039** is an indirect change, or false (0) otherwise. See the documentation for
11040** [sqlite3session_indirect()] for a description of direct and indirect
11041** changes. Finally, if pOp is not NULL, then *pOp is set to one of
11042** [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], depending on the
11043** type of change that the iterator currently points to.
11044**
11045** If no error occurs, SQLITE_OK is returned. If an error does occur, an
11046** SQLite error code is returned. The values of the output variables may not
11047** be trusted in this case.
11048*/
11049SQLITE_API int sqlite3changeset_op(
11050 sqlite3_changeset_iter *pIter, /* Iterator object */
11051 const char **pzTab, /* OUT: Pointer to table name */
11052 int *pnCol, /* OUT: Number of columns in table */
11053 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
11054 int *pbIndirect /* OUT: True for an 'indirect' change */
11055);
11056
11057/*
11058** CAPI3REF: Obtain The Primary Key Definition Of A Table
11059** METHOD: sqlite3_changeset_iter
11060**
11061** For each modified table, a changeset includes the following:
11062**
11063** <ul>
11064** <li> The number of columns in the table, and
11065** <li> Which of those columns make up the tables PRIMARY KEY.
11066** </ul>
11067**
11068** This function is used to find which columns comprise the PRIMARY KEY of
11069** the table modified by the change that iterator pIter currently points to.
11070** If successful, *pabPK is set to point to an array of nCol entries, where
11071** nCol is the number of columns in the table. Elements of *pabPK are set to
11072** 0x01 if the corresponding column is part of the tables primary key, or
11073** 0x00 if it is not.
11074**
11075** If argument pnCol is not NULL, then *pnCol is set to the number of columns
11076** in the table.
11077**
11078** If this function is called when the iterator does not point to a valid
11079** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
11080** SQLITE_OK is returned and the output variables populated as described
11081** above.
11082*/
11083SQLITE_API int sqlite3changeset_pk(
11084 sqlite3_changeset_iter *pIter, /* Iterator object */
11085 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
11086 int *pnCol /* OUT: Number of entries in output array */
11087);
11088
11089/*
11090** CAPI3REF: Obtain old.* Values From A Changeset Iterator
11091** METHOD: sqlite3_changeset_iter
11092**
11093** The pIter argument passed to this function may either be an iterator
11094** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11095** created by [sqlite3changeset_start()]. In the latter case, the most recent
11096** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
11097** Furthermore, it may only be called if the type of change that the iterator
11098** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
11099** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
11100**
11101** Argument iVal must be greater than or equal to 0, and less than the number
11102** of columns in the table affected by the current change. Otherwise,
11103** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
11104**
11105** If successful, this function sets *ppValue to point to a protected
11106** sqlite3_value object containing the iVal'th value from the vector of
11107** original row values stored as part of the UPDATE or DELETE change and
11108** returns SQLITE_OK. The name of the function comes from the fact that this
11109** is similar to the "old.*" columns available to update or delete triggers.
11110**
11111** If some other error occurs (e.g. an OOM condition), an SQLite error code
11112** is returned and *ppValue is set to NULL.
11113*/
11114SQLITE_API int sqlite3changeset_old(
11115 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11116 int iVal, /* Column number */
11117 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
11118);
11119
11120/*
11121** CAPI3REF: Obtain new.* Values From A Changeset Iterator
11122** METHOD: sqlite3_changeset_iter
11123**
11124** The pIter argument passed to this function may either be an iterator
11125** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11126** created by [sqlite3changeset_start()]. In the latter case, the most recent
11127** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
11128** Furthermore, it may only be called if the type of change that the iterator
11129** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
11130** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
11131**
11132** Argument iVal must be greater than or equal to 0, and less than the number
11133** of columns in the table affected by the current change. Otherwise,
11134** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
11135**
11136** If successful, this function sets *ppValue to point to a protected
11137** sqlite3_value object containing the iVal'th value from the vector of
11138** new row values stored as part of the UPDATE or INSERT change and
11139** returns SQLITE_OK. If the change is an UPDATE and does not include
11140** a new value for the requested column, *ppValue is set to NULL and
11141** SQLITE_OK returned. The name of the function comes from the fact that
11142** this is similar to the "new.*" columns available to update or delete
11143** triggers.
11144**
11145** If some other error occurs (e.g. an OOM condition), an SQLite error code
11146** is returned and *ppValue is set to NULL.
11147*/
11148SQLITE_API int sqlite3changeset_new(
11149 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11150 int iVal, /* Column number */
11151 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
11152);
11153
11154/*
11155** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
11156** METHOD: sqlite3_changeset_iter
11157**
11158** This function should only be used with iterator objects passed to a
11159** conflict-handler callback by [sqlite3changeset_apply()] with either
11160** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
11161** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
11162** is set to NULL.
11163**
11164** Argument iVal must be greater than or equal to 0, and less than the number
11165** of columns in the table affected by the current change. Otherwise,
11166** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
11167**
11168** If successful, this function sets *ppValue to point to a protected
11169** sqlite3_value object containing the iVal'th value from the
11170** "conflicting row" associated with the current conflict-handler callback
11171** and returns SQLITE_OK.
11172**
11173** If some other error occurs (e.g. an OOM condition), an SQLite error code
11174** is returned and *ppValue is set to NULL.
11175*/
11176SQLITE_API int sqlite3changeset_conflict(
11177 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11178 int iVal, /* Column number */
11179 sqlite3_value **ppValue /* OUT: Value from conflicting row */
11180);
11181
11182/*
11183** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
11184** METHOD: sqlite3_changeset_iter
11185**
11186** This function may only be called with an iterator passed to an
11187** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
11188** it sets the output variable to the total number of known foreign key
11189** violations in the destination database and returns SQLITE_OK.
11190**
11191** In all other cases this function returns SQLITE_MISUSE.
11192*/
11193SQLITE_API int sqlite3changeset_fk_conflicts(
11194 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11195 int *pnOut /* OUT: Number of FK violations */
11196);
11197
11198
11199/*
11200** CAPI3REF: Finalize A Changeset Iterator
11201** METHOD: sqlite3_changeset_iter
11202**
11203** This function is used to finalize an iterator allocated with
11204** [sqlite3changeset_start()].
11205**
11206** This function should only be called on iterators created using the
11207** [sqlite3changeset_start()] function. If an application calls this
11208** function with an iterator passed to a conflict-handler by
11209** [sqlite3changeset_apply()], [SQLITE_MISUSE] is immediately returned and the
11210** call has no effect.
11211**
11212** If an error was encountered within a call to an sqlite3changeset_xxx()
11213** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an
11214** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
11215** to that error is returned by this function. Otherwise, SQLITE_OK is
11216** returned. This is to allow the following pattern (pseudo-code):
11217**
11218** <pre>
11219** sqlite3changeset_start();
11220** while( SQLITE_ROW==sqlite3changeset_next() ){
11221** // Do something with change.
11222** }
11223** rc = sqlite3changeset_finalize();
11224** if( rc!=SQLITE_OK ){
11225** // An error has occurred
11226** }
11227** </pre>
11228*/
11229SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
11230
11231/*
11232** CAPI3REF: Invert A Changeset
11233**
11234** This function is used to "invert" a changeset object. Applying an inverted
11235** changeset to a database reverses the effects of applying the uninverted
11236** changeset. Specifically:
11237**
11238** <ul>
11239** <li> Each DELETE change is changed to an INSERT, and
11240** <li> Each INSERT change is changed to a DELETE, and
11241** <li> For each UPDATE change, the old.* and new.* values are exchanged.
11242** </ul>
11243**
11244** This function does not change the order in which changes appear within
11245** the changeset. It merely reverses the sense of each individual change.
11246**
11247** If successful, a pointer to a buffer containing the inverted changeset
11248** is stored in *ppOut, the size of the same buffer is stored in *pnOut, and
11249** SQLITE_OK is returned. If an error occurs, both *pnOut and *ppOut are
11250** zeroed and an SQLite error code returned.
11251**
11252** It is the responsibility of the caller to eventually call sqlite3_free()
11253** on the *ppOut pointer to free the buffer allocation following a successful
11254** call to this function.
11255**
11256** WARNING/TODO: This function currently assumes that the input is a valid
11257** changeset. If it is not, the results are undefined.
11258*/
11259SQLITE_API int sqlite3changeset_invert(
11260 int nIn, const void *pIn, /* Input changeset */
11261 int *pnOut, void **ppOut /* OUT: Inverse of input */
11262);
11263
11264/*
11265** CAPI3REF: Concatenate Two Changeset Objects
11266**
11267** This function is used to concatenate two changesets, A and B, into a
11268** single changeset. The result is a changeset equivalent to applying
11269** changeset A followed by changeset B.
11270**
11271** This function combines the two input changesets using an
11272** sqlite3_changegroup object. Calling it produces similar results as the
11273** following code fragment:
11274**
11275** <pre>
11276** sqlite3_changegroup *pGrp;
11277** rc = sqlite3_changegroup_new(&pGrp);
11278** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
11279** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
11280** if( rc==SQLITE_OK ){
11281** rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
11282** }else{
11283** *ppOut = 0;
11284** *pnOut = 0;
11285** }
11286** </pre>
11287**
11288** Refer to the sqlite3_changegroup documentation below for details.
11289*/
11290SQLITE_API int sqlite3changeset_concat(
11291 int nA, /* Number of bytes in buffer pA */
11292 void *pA, /* Pointer to buffer containing changeset A */
11293 int nB, /* Number of bytes in buffer pB */
11294 void *pB, /* Pointer to buffer containing changeset B */
11295 int *pnOut, /* OUT: Number of bytes in output changeset */
11296 void **ppOut /* OUT: Buffer containing output changeset */
11297);
11298
11299
11300/*
11301** CAPI3REF: Changegroup Handle
11302**
11303** A changegroup is an object used to combine two or more
11304** [changesets] or [patchsets]
11305*/
11306typedef struct sqlite3_changegroup sqlite3_changegroup;
11307
11308/*
11309** CAPI3REF: Create A New Changegroup Object
11310** CONSTRUCTOR: sqlite3_changegroup
11311**
11312** An sqlite3_changegroup object is used to combine two or more changesets
11313** (or patchsets) into a single changeset (or patchset). A single changegroup
11314** object may combine changesets or patchsets, but not both. The output is
11315** always in the same format as the input.
11316**
11317** If successful, this function returns SQLITE_OK and populates (*pp) with
11318** a pointer to a new sqlite3_changegroup object before returning. The caller
11319** should eventually free the returned object using a call to
11320** sqlite3changegroup_delete(). If an error occurs, an SQLite error code
11321** (i.e. SQLITE_NOMEM) is returned and *pp is set to NULL.
11322**
11323** The usual usage pattern for an sqlite3_changegroup object is as follows:
11324**
11325** <ul>
11326** <li> It is created using a call to sqlite3changegroup_new().
11327**
11328** <li> Zero or more changesets (or patchsets) are added to the object
11329** by calling sqlite3changegroup_add().
11330**
11331** <li> The result of combining all input changesets together is obtained
11332** by the application via a call to sqlite3changegroup_output().
11333**
11334** <li> The object is deleted using a call to sqlite3changegroup_delete().
11335** </ul>
11336**
11337** Any number of calls to add() and output() may be made between the calls to
11338** new() and delete(), and in any order.
11339**
11340** As well as the regular sqlite3changegroup_add() and
11341** sqlite3changegroup_output() functions, also available are the streaming
11342** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
11343*/
11344SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
11345
11346/*
11347** CAPI3REF: Add A Changeset To A Changegroup
11348** METHOD: sqlite3_changegroup
11349**
11350** Add all changes within the changeset (or patchset) in buffer pData (size
11351** nData bytes) to the changegroup.
11352**
11353** If the buffer contains a patchset, then all prior calls to this function
11354** on the same changegroup object must also have specified patchsets. Or, if
11355** the buffer contains a changeset, so must have the earlier calls to this
11356** function. Otherwise, SQLITE_ERROR is returned and no changes are added
11357** to the changegroup.
11358**
11359** Rows within the changeset and changegroup are identified by the values in
11360** their PRIMARY KEY columns. A change in the changeset is considered to
11361** apply to the same row as a change already present in the changegroup if
11362** the two rows have the same primary key.
11363**
11364** Changes to rows that do not already appear in the changegroup are
11365** simply copied into it. Or, if both the new changeset and the changegroup
11366** contain changes that apply to a single row, the final contents of the
11367** changegroup depends on the type of each change, as follows:
11368**
11369** <table border=1 style="margin-left:8ex;margin-right:8ex">
11370** <tr><th style="white-space:pre">Existing Change </th>
11371** <th style="white-space:pre">New Change </th>
11372** <th>Output Change
11373** <tr><td>INSERT <td>INSERT <td>
11374** The new change is ignored. This case does not occur if the new
11375** changeset was recorded immediately after the changesets already
11376** added to the changegroup.
11377** <tr><td>INSERT <td>UPDATE <td>
11378** The INSERT change remains in the changegroup. The values in the
11379** INSERT change are modified as if the row was inserted by the
11380** existing change and then updated according to the new change.
11381** <tr><td>INSERT <td>DELETE <td>
11382** The existing INSERT is removed from the changegroup. The DELETE is
11383** not added.
11384** <tr><td>UPDATE <td>INSERT <td>
11385** The new change is ignored. This case does not occur if the new
11386** changeset was recorded immediately after the changesets already
11387** added to the changegroup.
11388** <tr><td>UPDATE <td>UPDATE <td>
11389** The existing UPDATE remains within the changegroup. It is amended
11390** so that the accompanying values are as if the row was updated once
11391** by the existing change and then again by the new change.
11392** <tr><td>UPDATE <td>DELETE <td>
11393** The existing UPDATE is replaced by the new DELETE within the
11394** changegroup.
11395** <tr><td>DELETE <td>INSERT <td>
11396** If one or more of the column values in the row inserted by the
11397** new change differ from those in the row deleted by the existing
11398** change, the existing DELETE is replaced by an UPDATE within the
11399** changegroup. Otherwise, if the inserted row is exactly the same
11400** as the deleted row, the existing DELETE is simply discarded.
11401** <tr><td>DELETE <td>UPDATE <td>
11402** The new change is ignored. This case does not occur if the new
11403** changeset was recorded immediately after the changesets already
11404** added to the changegroup.
11405** <tr><td>DELETE <td>DELETE <td>
11406** The new change is ignored. This case does not occur if the new
11407** changeset was recorded immediately after the changesets already
11408** added to the changegroup.
11409** </table>
11410**
11411** If the new changeset contains changes to a table that is already present
11412** in the changegroup, then the number of columns and the position of the
11413** primary key columns for the table must be consistent. If this is not the
11414** case, this function fails with SQLITE_SCHEMA. If the input changeset
11415** appears to be corrupt and the corruption is detected, SQLITE_CORRUPT is
11416** returned. Or, if an out-of-memory condition occurs during processing, this
11417** function returns SQLITE_NOMEM. In all cases, if an error occurs the
11418** final contents of the changegroup is undefined.
11419**
11420** If no error occurs, SQLITE_OK is returned.
11421*/
11422SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
11423
11424/*
11425** CAPI3REF: Obtain A Composite Changeset From A Changegroup
11426** METHOD: sqlite3_changegroup
11427**
11428** Obtain a buffer containing a changeset (or patchset) representing the
11429** current contents of the changegroup. If the inputs to the changegroup
11430** were themselves changesets, the output is a changeset. Or, if the
11431** inputs were patchsets, the output is also a patchset.
11432**
11433** As with the output of the sqlite3session_changeset() and
11434** sqlite3session_patchset() functions, all changes related to a single
11435** table are grouped together in the output of this function. Tables appear
11436** in the same order as for the very first changeset added to the changegroup.
11437** If the second or subsequent changesets added to the changegroup contain
11438** changes for tables that do not appear in the first changeset, they are
11439** appended onto the end of the output changeset, again in the order in
11440** which they are first encountered.
11441**
11442** If an error occurs, an SQLite error code is returned and the output
11443** variables (*pnData) and (*ppData) are set to 0. Otherwise, SQLITE_OK
11444** is returned and the output variables are set to the size of and a
11445** pointer to the output buffer, respectively. In this case it is the
11446** responsibility of the caller to eventually free the buffer using a
11447** call to sqlite3_free().
11448*/
11449SQLITE_API int sqlite3changegroup_output(
11450 sqlite3_changegroup*,
11451 int *pnData, /* OUT: Size of output buffer in bytes */
11452 void **ppData /* OUT: Pointer to output buffer */
11453);
11454
11455/*
11456** CAPI3REF: Delete A Changegroup Object
11457** DESTRUCTOR: sqlite3_changegroup
11458*/
11459SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
11460
11461/*
11462** CAPI3REF: Apply A Changeset To A Database
11463**
11464** Apply a changeset or patchset to a database. These functions attempt to
11465** update the "main" database attached to handle db with the changes found in
11466** the changeset passed via the second and third arguments.
11467**
11468** The fourth argument (xFilter) passed to these functions is the "filter
11469** callback". If it is not NULL, then for each table affected by at least one
11470** change in the changeset, the filter callback is invoked with
11471** the table name as the second argument, and a copy of the context pointer
11472** passed as the sixth argument as the first. If the "filter callback"
11473** returns zero, then no attempt is made to apply any changes to the table.
11474** Otherwise, if the return value is non-zero or the xFilter argument to
11475** is NULL, all changes related to the table are attempted.
11476**
11477** For each table that is not excluded by the filter callback, this function
11478** tests that the target database contains a compatible table. A table is
11479** considered compatible if all of the following are true:
11480**
11481** <ul>
11482** <li> The table has the same name as the name recorded in the
11483** changeset, and
11484** <li> The table has at least as many columns as recorded in the
11485** changeset, and
11486** <li> The table has primary key columns in the same position as
11487** recorded in the changeset.
11488** </ul>
11489**
11490** If there is no compatible table, it is not an error, but none of the
11491** changes associated with the table are applied. A warning message is issued
11492** via the sqlite3_log() mechanism with the error code SQLITE_SCHEMA. At most
11493** one such warning is issued for each table in the changeset.
11494**
11495** For each change for which there is a compatible table, an attempt is made
11496** to modify the table contents according to the UPDATE, INSERT or DELETE
11497** change. If a change cannot be applied cleanly, the conflict handler
11498** function passed as the fifth argument to sqlite3changeset_apply() may be
11499** invoked. A description of exactly when the conflict handler is invoked for
11500** each type of change is below.
11501**
11502** Unlike the xFilter argument, xConflict may not be passed NULL. The results
11503** of passing anything other than a valid function pointer as the xConflict
11504** argument are undefined.
11505**
11506** Each time the conflict handler function is invoked, it must return one
11507** of [SQLITE_CHANGESET_OMIT], [SQLITE_CHANGESET_ABORT] or
11508** [SQLITE_CHANGESET_REPLACE]. SQLITE_CHANGESET_REPLACE may only be returned
11509** if the second argument passed to the conflict handler is either
11510** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
11511** returns an illegal value, any changes already made are rolled back and
11512** the call to sqlite3changeset_apply() returns SQLITE_MISUSE. Different
11513** actions are taken by sqlite3changeset_apply() depending on the value
11514** returned by each invocation of the conflict-handler function. Refer to
11515** the documentation for the three
11516** [SQLITE_CHANGESET_OMIT|available return values] for details.
11517**
11518** <dl>
11519** <dt>DELETE Changes<dd>
11520** For each DELETE change, the function checks if the target database
11521** contains a row with the same primary key value (or values) as the
11522** original row values stored in the changeset. If it does, and the values
11523** stored in all non-primary key columns also match the values stored in
11524** the changeset the row is deleted from the target database.
11525**
11526** If a row with matching primary key values is found, but one or more of
11527** the non-primary key fields contains a value different from the original
11528** row value stored in the changeset, the conflict-handler function is
11529** invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the
11530** database table has more columns than are recorded in the changeset,
11531** only the values of those non-primary key fields are compared against
11532** the current database contents - any trailing database table columns
11533** are ignored.
11534**
11535** If no row with matching primary key values is found in the database,
11536** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
11537** passed as the second argument.
11538**
11539** If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT
11540** (which can only happen if a foreign key constraint is violated), the
11541** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
11542** passed as the second argument. This includes the case where the DELETE
11543** operation is attempted because an earlier call to the conflict handler
11544** function returned [SQLITE_CHANGESET_REPLACE].
11545**
11546** <dt>INSERT Changes<dd>
11547** For each INSERT change, an attempt is made to insert the new row into
11548** the database. If the changeset row contains fewer fields than the
11549** database table, the trailing fields are populated with their default
11550** values.
11551**
11552** If the attempt to insert the row fails because the database already
11553** contains a row with the same primary key values, the conflict handler
11554** function is invoked with the second argument set to
11555** [SQLITE_CHANGESET_CONFLICT].
11556**
11557** If the attempt to insert the row fails because of some other constraint
11558** violation (e.g. NOT NULL or UNIQUE), the conflict handler function is
11559** invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT].
11560** This includes the case where the INSERT operation is re-attempted because
11561** an earlier call to the conflict handler function returned
11562** [SQLITE_CHANGESET_REPLACE].
11563**
11564** <dt>UPDATE Changes<dd>
11565** For each UPDATE change, the function checks if the target database
11566** contains a row with the same primary key value (or values) as the
11567** original row values stored in the changeset. If it does, and the values
11568** stored in all modified non-primary key columns also match the values
11569** stored in the changeset the row is updated within the target database.
11570**
11571** If a row with matching primary key values is found, but one or more of
11572** the modified non-primary key fields contains a value different from an
11573** original row value stored in the changeset, the conflict-handler function
11574** is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
11575** UPDATE changes only contain values for non-primary key fields that are
11576** to be modified, only those fields need to match the original values to
11577** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
11578**
11579** If no row with matching primary key values is found in the database,
11580** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
11581** passed as the second argument.
11582**
11583** If the UPDATE operation is attempted, but SQLite returns
11584** SQLITE_CONSTRAINT, the conflict-handler function is invoked with
11585** [SQLITE_CHANGESET_CONSTRAINT] passed as the second argument.
11586** This includes the case where the UPDATE operation is attempted after
11587** an earlier call to the conflict handler function returned
11588** [SQLITE_CHANGESET_REPLACE].
11589** </dl>
11590**
11591** It is safe to execute SQL statements, including those that write to the
11592** table that the callback related to, from within the xConflict callback.
11593** This can be used to further customize the applications conflict
11594** resolution strategy.
11595**
11596** All changes made by these functions are enclosed in a savepoint transaction.
11597** If any other error (aside from a constraint failure when attempting to
11598** write to the target database) occurs, then the savepoint transaction is
11599** rolled back, restoring the target database to its original state, and an
11600** SQLite error code returned.
11601**
11602** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
11603** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2()
11604** may set (*ppRebase) to point to a "rebase" that may be used with the
11605** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase)
11606** is set to the size of the buffer in bytes. It is the responsibility of the
11607** caller to eventually free any such buffer using sqlite3_free(). The buffer
11608** is only allocated and populated if one or more conflicts were encountered
11609** while applying the patchset. See comments surrounding the sqlite3_rebaser
11610** APIs for further details.
11611**
11612** The behavior of sqlite3changeset_apply_v2() and its streaming equivalent
11613** may be modified by passing a combination of
11614** [SQLITE_CHANGESETAPPLY_NOSAVEPOINT | supported flags] as the 9th parameter.
11615**
11616** Note that the sqlite3changeset_apply_v2() API is still <b>experimental</b>
11617** and therefore subject to change.
11618*/
11619SQLITE_API int sqlite3changeset_apply(
11620 sqlite3 *db, /* Apply change to "main" db of this handle */
11621 int nChangeset, /* Size of changeset in bytes */
11622 void *pChangeset, /* Changeset blob */
11623 int(*xFilter)(
11624 void *pCtx, /* Copy of sixth arg to _apply() */
11625 const char *zTab /* Table name */
11626 ),
11627 int(*xConflict)(
11628 void *pCtx, /* Copy of sixth arg to _apply() */
11629 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
11630 sqlite3_changeset_iter *p /* Handle describing change and conflict */
11631 ),
11632 void *pCtx /* First argument passed to xConflict */
11633);
11634SQLITE_API int sqlite3changeset_apply_v2(
11635 sqlite3 *db, /* Apply change to "main" db of this handle */
11636 int nChangeset, /* Size of changeset in bytes */
11637 void *pChangeset, /* Changeset blob */
11638 int(*xFilter)(
11639 void *pCtx, /* Copy of sixth arg to _apply() */
11640 const char *zTab /* Table name */
11641 ),
11642 int(*xConflict)(
11643 void *pCtx, /* Copy of sixth arg to _apply() */
11644 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
11645 sqlite3_changeset_iter *p /* Handle describing change and conflict */
11646 ),
11647 void *pCtx, /* First argument passed to xConflict */
11648 void **ppRebase, int *pnRebase, /* OUT: Rebase data */
11649 int flags /* SESSION_CHANGESETAPPLY_* flags */
11650);
11651
11652/*
11653** CAPI3REF: Flags for sqlite3changeset_apply_v2
11654**
11655** The following flags may passed via the 9th parameter to
11656** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]:
11657**
11658** <dl>
11659** <dt>SQLITE_CHANGESETAPPLY_NOSAVEPOINT <dd>
11660** Usually, the sessions module encloses all operations performed by
11661** a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The
11662** SAVEPOINT is committed if the changeset or patchset is successfully
11663** applied, or rolled back if an error occurs. Specifying this flag
11664** causes the sessions module to omit this savepoint. In this case, if the
11665** caller has an open transaction or savepoint when apply_v2() is called,
11666** it may revert the partially applied changeset by rolling it back.
11667**
11668** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
11669** Invert the changeset before applying it. This is equivalent to inverting
11670** a changeset using sqlite3changeset_invert() before applying it. It is
11671** an error to specify this flag with a patchset.
11672*/
11673#define SQLITE_CHANGESETAPPLY_NOSAVEPOINT 0x0001
11674#define SQLITE_CHANGESETAPPLY_INVERT 0x0002
11675
11676/*
11677** CAPI3REF: Constants Passed To The Conflict Handler
11678**
11679** Values that may be passed as the second argument to a conflict-handler.
11680**
11681** <dl>
11682** <dt>SQLITE_CHANGESET_DATA<dd>
11683** The conflict handler is invoked with CHANGESET_DATA as the second argument
11684** when processing a DELETE or UPDATE change if a row with the required
11685** PRIMARY KEY fields is present in the database, but one or more other
11686** (non primary-key) fields modified by the update do not contain the
11687** expected "before" values.
11688**
11689** The conflicting row, in this case, is the database row with the matching
11690** primary key.
11691**
11692** <dt>SQLITE_CHANGESET_NOTFOUND<dd>
11693** The conflict handler is invoked with CHANGESET_NOTFOUND as the second
11694** argument when processing a DELETE or UPDATE change if a row with the
11695** required PRIMARY KEY fields is not present in the database.
11696**
11697** There is no conflicting row in this case. The results of invoking the
11698** sqlite3changeset_conflict() API are undefined.
11699**
11700** <dt>SQLITE_CHANGESET_CONFLICT<dd>
11701** CHANGESET_CONFLICT is passed as the second argument to the conflict
11702** handler while processing an INSERT change if the operation would result
11703** in duplicate primary key values.
11704**
11705** The conflicting row in this case is the database row with the matching
11706** primary key.
11707**
11708** <dt>SQLITE_CHANGESET_FOREIGN_KEY<dd>
11709** If foreign key handling is enabled, and applying a changeset leaves the
11710** database in a state containing foreign key violations, the conflict
11711** handler is invoked with CHANGESET_FOREIGN_KEY as the second argument
11712** exactly once before the changeset is committed. If the conflict handler
11713** returns CHANGESET_OMIT, the changes, including those that caused the
11714** foreign key constraint violation, are committed. Or, if it returns
11715** CHANGESET_ABORT, the changeset is rolled back.
11716**
11717** No current or conflicting row information is provided. The only function
11718** it is possible to call on the supplied sqlite3_changeset_iter handle
11719** is sqlite3changeset_fk_conflicts().
11720**
11721** <dt>SQLITE_CHANGESET_CONSTRAINT<dd>
11722** If any other constraint violation occurs while applying a change (i.e.
11723** a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is
11724** invoked with CHANGESET_CONSTRAINT as the second argument.
11725**
11726** There is no conflicting row in this case. The results of invoking the
11727** sqlite3changeset_conflict() API are undefined.
11728**
11729** </dl>
11730*/
11731#define SQLITE_CHANGESET_DATA 1
11732#define SQLITE_CHANGESET_NOTFOUND 2
11733#define SQLITE_CHANGESET_CONFLICT 3
11734#define SQLITE_CHANGESET_CONSTRAINT 4
11735#define SQLITE_CHANGESET_FOREIGN_KEY 5
11736
11737/*
11738** CAPI3REF: Constants Returned By The Conflict Handler
11739**
11740** A conflict handler callback must return one of the following three values.
11741**
11742** <dl>
11743** <dt>SQLITE_CHANGESET_OMIT<dd>
11744** If a conflict handler returns this value no special action is taken. The
11745** change that caused the conflict is not applied. The session module
11746** continues to the next change in the changeset.
11747**
11748** <dt>SQLITE_CHANGESET_REPLACE<dd>
11749** This value may only be returned if the second argument to the conflict
11750** handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this
11751** is not the case, any changes applied so far are rolled back and the
11752** call to sqlite3changeset_apply() returns SQLITE_MISUSE.
11753**
11754** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict
11755** handler, then the conflicting row is either updated or deleted, depending
11756** on the type of change.
11757**
11758** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict
11759** handler, then the conflicting row is removed from the database and a
11760** second attempt to apply the change is made. If this second attempt fails,
11761** the original row is restored to the database before continuing.
11762**
11763** <dt>SQLITE_CHANGESET_ABORT<dd>
11764** If this value is returned, any changes applied so far are rolled back
11765** and the call to sqlite3changeset_apply() returns SQLITE_ABORT.
11766** </dl>
11767*/
11768#define SQLITE_CHANGESET_OMIT 0
11769#define SQLITE_CHANGESET_REPLACE 1
11770#define SQLITE_CHANGESET_ABORT 2
11771
11772/*
11773** CAPI3REF: Rebasing changesets
11774** EXPERIMENTAL
11775**
11776** Suppose there is a site hosting a database in state S0. And that
11777** modifications are made that move that database to state S1 and a
11778** changeset recorded (the "local" changeset). Then, a changeset based
11779** on S0 is received from another site (the "remote" changeset) and
11780** applied to the database. The database is then in state
11781** (S1+"remote"), where the exact state depends on any conflict
11782** resolution decisions (OMIT or REPLACE) made while applying "remote".
11783** Rebasing a changeset is to update it to take those conflict
11784** resolution decisions into account, so that the same conflicts
11785** do not have to be resolved elsewhere in the network.
11786**
11787** For example, if both the local and remote changesets contain an
11788** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)":
11789**
11790** local: INSERT INTO t1 VALUES(1, 'v1');
11791** remote: INSERT INTO t1 VALUES(1, 'v2');
11792**
11793** and the conflict resolution is REPLACE, then the INSERT change is
11794** removed from the local changeset (it was overridden). Or, if the
11795** conflict resolution was "OMIT", then the local changeset is modified
11796** to instead contain:
11797**
11798** UPDATE t1 SET b = 'v2' WHERE a=1;
11799**
11800** Changes within the local changeset are rebased as follows:
11801**
11802** <dl>
11803** <dt>Local INSERT<dd>
11804** This may only conflict with a remote INSERT. If the conflict
11805** resolution was OMIT, then add an UPDATE change to the rebased
11806** changeset. Or, if the conflict resolution was REPLACE, add
11807** nothing to the rebased changeset.
11808**
11809** <dt>Local DELETE<dd>
11810** This may conflict with a remote UPDATE or DELETE. In both cases the
11811** only possible resolution is OMIT. If the remote operation was a
11812** DELETE, then add no change to the rebased changeset. If the remote
11813** operation was an UPDATE, then the old.* fields of change are updated
11814** to reflect the new.* values in the UPDATE.
11815**
11816** <dt>Local UPDATE<dd>
11817** This may conflict with a remote UPDATE or DELETE. If it conflicts
11818** with a DELETE, and the conflict resolution was OMIT, then the update
11819** is changed into an INSERT. Any undefined values in the new.* record
11820** from the update change are filled in using the old.* values from
11821** the conflicting DELETE. Or, if the conflict resolution was REPLACE,
11822** the UPDATE change is simply omitted from the rebased changeset.
11823**
11824** If conflict is with a remote UPDATE and the resolution is OMIT, then
11825** the old.* values are rebased using the new.* values in the remote
11826** change. Or, if the resolution is REPLACE, then the change is copied
11827** into the rebased changeset with updates to columns also updated by
11828** the conflicting remote UPDATE removed. If this means no columns would
11829** be updated, the change is omitted.
11830** </dl>
11831**
11832** A local change may be rebased against multiple remote changes
11833** simultaneously. If a single key is modified by multiple remote
11834** changesets, they are combined as follows before the local changeset
11835** is rebased:
11836**
11837** <ul>
11838** <li> If there has been one or more REPLACE resolutions on a
11839** key, it is rebased according to a REPLACE.
11840**
11841** <li> If there have been no REPLACE resolutions on a key, then
11842** the local changeset is rebased according to the most recent
11843** of the OMIT resolutions.
11844** </ul>
11845**
11846** Note that conflict resolutions from multiple remote changesets are
11847** combined on a per-field basis, not per-row. This means that in the
11848** case of multiple remote UPDATE operations, some fields of a single
11849** local change may be rebased for REPLACE while others are rebased for
11850** OMIT.
11851**
11852** In order to rebase a local changeset, the remote changeset must first
11853** be applied to the local database using sqlite3changeset_apply_v2() and
11854** the buffer of rebase information captured. Then:
11855**
11856** <ol>
11857** <li> An sqlite3_rebaser object is created by calling
11858** sqlite3rebaser_create().
11859** <li> The new object is configured with the rebase buffer obtained from
11860** sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure().
11861** If the local changeset is to be rebased against multiple remote
11862** changesets, then sqlite3rebaser_configure() should be called
11863** multiple times, in the same order that the multiple
11864** sqlite3changeset_apply_v2() calls were made.
11865** <li> Each local changeset is rebased by calling sqlite3rebaser_rebase().
11866** <li> The sqlite3_rebaser object is deleted by calling
11867** sqlite3rebaser_delete().
11868** </ol>
11869*/
11870typedef struct sqlite3_rebaser sqlite3_rebaser;
11871
11872/*
11873** CAPI3REF: Create a changeset rebaser object.
11874** EXPERIMENTAL
11875**
11876** Allocate a new changeset rebaser object. If successful, set (*ppNew) to
11877** point to the new object and return SQLITE_OK. Otherwise, if an error
11878** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew)
11879** to NULL.
11880*/
11881SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew);
11882
11883/*
11884** CAPI3REF: Configure a changeset rebaser object.
11885** EXPERIMENTAL
11886**
11887** Configure the changeset rebaser object to rebase changesets according
11888** to the conflict resolutions described by buffer pRebase (size nRebase
11889** bytes), which must have been obtained from a previous call to
11890** sqlite3changeset_apply_v2().
11891*/
11892SQLITE_API int sqlite3rebaser_configure(
11893 sqlite3_rebaser*,
11894 int nRebase, const void *pRebase
11895);
11896
11897/*
11898** CAPI3REF: Rebase a changeset
11899** EXPERIMENTAL
11900**
11901** Argument pIn must point to a buffer containing a changeset nIn bytes
11902** in size. This function allocates and populates a buffer with a copy
11903** of the changeset rebased rebased according to the configuration of the
11904** rebaser object passed as the first argument. If successful, (*ppOut)
11905** is set to point to the new buffer containing the rebased changset and
11906** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
11907** responsibility of the caller to eventually free the new buffer using
11908** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
11909** are set to zero and an SQLite error code returned.
11910*/
11911SQLITE_API int sqlite3rebaser_rebase(
11912 sqlite3_rebaser*,
11913 int nIn, const void *pIn,
11914 int *pnOut, void **ppOut
11915);
11916
11917/*
11918** CAPI3REF: Delete a changeset rebaser object.
11919** EXPERIMENTAL
11920**
11921** Delete the changeset rebaser object and all associated resources. There
11922** should be one call to this function for each successful invocation
11923** of sqlite3rebaser_create().
11924*/
11925SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p);
11926
11927/*
11928** CAPI3REF: Streaming Versions of API functions.
11929**
11930** The six streaming API xxx_strm() functions serve similar purposes to the
11931** corresponding non-streaming API functions:
11932**
11933** <table border=1 style="margin-left:8ex;margin-right:8ex">
11934** <tr><th>Streaming function<th>Non-streaming equivalent</th>
11935** <tr><td>sqlite3changeset_apply_strm<td>[sqlite3changeset_apply]
11936** <tr><td>sqlite3changeset_apply_strm_v2<td>[sqlite3changeset_apply_v2]
11937** <tr><td>sqlite3changeset_concat_strm<td>[sqlite3changeset_concat]
11938** <tr><td>sqlite3changeset_invert_strm<td>[sqlite3changeset_invert]
11939** <tr><td>sqlite3changeset_start_strm<td>[sqlite3changeset_start]
11940** <tr><td>sqlite3session_changeset_strm<td>[sqlite3session_changeset]
11941** <tr><td>sqlite3session_patchset_strm<td>[sqlite3session_patchset]
11942** </table>
11943**
11944** Non-streaming functions that accept changesets (or patchsets) as input
11945** require that the entire changeset be stored in a single buffer in memory.
11946** Similarly, those that return a changeset or patchset do so by returning
11947** a pointer to a single large buffer allocated using sqlite3_malloc().
11948** Normally this is convenient. However, if an application running in a
11949** low-memory environment is required to handle very large changesets, the
11950** large contiguous memory allocations required can become onerous.
11951**
11952** In order to avoid this problem, instead of a single large buffer, input
11953** is passed to a streaming API functions by way of a callback function that
11954** the sessions module invokes to incrementally request input data as it is
11955** required. In all cases, a pair of API function parameters such as
11956**
11957** <pre>
11958** &nbsp; int nChangeset,
11959** &nbsp; void *pChangeset,
11960** </pre>
11961**
11962** Is replaced by:
11963**
11964** <pre>
11965** &nbsp; int (*xInput)(void *pIn, void *pData, int *pnData),
11966** &nbsp; void *pIn,
11967** </pre>
11968**
11969** Each time the xInput callback is invoked by the sessions module, the first
11970** argument passed is a copy of the supplied pIn context pointer. The second
11971** argument, pData, points to a buffer (*pnData) bytes in size. Assuming no
11972** error occurs the xInput method should copy up to (*pnData) bytes of data
11973** into the buffer and set (*pnData) to the actual number of bytes copied
11974** before returning SQLITE_OK. If the input is completely exhausted, (*pnData)
11975** should be set to zero to indicate this. Or, if an error occurs, an SQLite
11976** error code should be returned. In all cases, if an xInput callback returns
11977** an error, all processing is abandoned and the streaming API function
11978** returns a copy of the error code to the caller.
11979**
11980** In the case of sqlite3changeset_start_strm(), the xInput callback may be
11981** invoked by the sessions module at any point during the lifetime of the
11982** iterator. If such an xInput callback returns an error, the iterator enters
11983** an error state, whereby all subsequent calls to iterator functions
11984** immediately fail with the same error code as returned by xInput.
11985**
11986** Similarly, streaming API functions that return changesets (or patchsets)
11987** return them in chunks by way of a callback function instead of via a
11988** pointer to a single large buffer. In this case, a pair of parameters such
11989** as:
11990**
11991** <pre>
11992** &nbsp; int *pnChangeset,
11993** &nbsp; void **ppChangeset,
11994** </pre>
11995**
11996** Is replaced by:
11997**
11998** <pre>
11999** &nbsp; int (*xOutput)(void *pOut, const void *pData, int nData),
12000** &nbsp; void *pOut
12001** </pre>
12002**
12003** The xOutput callback is invoked zero or more times to return data to
12004** the application. The first parameter passed to each call is a copy of the
12005** pOut pointer supplied by the application. The second parameter, pData,
12006** points to a buffer nData bytes in size containing the chunk of output
12007** data being returned. If the xOutput callback successfully processes the
12008** supplied data, it should return SQLITE_OK to indicate success. Otherwise,
12009** it should return some other SQLite error code. In this case processing
12010** is immediately abandoned and the streaming API function returns a copy
12011** of the xOutput error code to the application.
12012**
12013** The sessions module never invokes an xOutput callback with the third
12014** parameter set to a value less than or equal to zero. Other than this,
12015** no guarantees are made as to the size of the chunks of data returned.
12016*/
12017SQLITE_API int sqlite3changeset_apply_strm(
12018 sqlite3 *db, /* Apply change to "main" db of this handle */
12019 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
12020 void *pIn, /* First arg for xInput */
12021 int(*xFilter)(
12022 void *pCtx, /* Copy of sixth arg to _apply() */
12023 const char *zTab /* Table name */
12024 ),
12025 int(*xConflict)(
12026 void *pCtx, /* Copy of sixth arg to _apply() */
12027 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
12028 sqlite3_changeset_iter *p /* Handle describing change and conflict */
12029 ),
12030 void *pCtx /* First argument passed to xConflict */
12031);
12032SQLITE_API int sqlite3changeset_apply_v2_strm(
12033 sqlite3 *db, /* Apply change to "main" db of this handle */
12034 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
12035 void *pIn, /* First arg for xInput */
12036 int(*xFilter)(
12037 void *pCtx, /* Copy of sixth arg to _apply() */
12038 const char *zTab /* Table name */
12039 ),
12040 int(*xConflict)(
12041 void *pCtx, /* Copy of sixth arg to _apply() */
12042 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
12043 sqlite3_changeset_iter *p /* Handle describing change and conflict */
12044 ),
12045 void *pCtx, /* First argument passed to xConflict */
12046 void **ppRebase, int *pnRebase,
12047 int flags
12048);
12049SQLITE_API int sqlite3changeset_concat_strm(
12050 int (*xInputA)(void *pIn, void *pData, int *pnData),
12051 void *pInA,
12052 int (*xInputB)(void *pIn, void *pData, int *pnData),
12053 void *pInB,
12054 int (*xOutput)(void *pOut, const void *pData, int nData),
12055 void *pOut
12056);
12057SQLITE_API int sqlite3changeset_invert_strm(
12058 int (*xInput)(void *pIn, void *pData, int *pnData),
12059 void *pIn,
12060 int (*xOutput)(void *pOut, const void *pData, int nData),
12061 void *pOut
12062);
12063SQLITE_API int sqlite3changeset_start_strm(
12064 sqlite3_changeset_iter **pp,
12065 int (*xInput)(void *pIn, void *pData, int *pnData),
12066 void *pIn
12067);
12068SQLITE_API int sqlite3changeset_start_v2_strm(
12069 sqlite3_changeset_iter **pp,
12070 int (*xInput)(void *pIn, void *pData, int *pnData),
12071 void *pIn,
12072 int flags
12073);
12074SQLITE_API int sqlite3session_changeset_strm(
12075 sqlite3_session *pSession,
12076 int (*xOutput)(void *pOut, const void *pData, int nData),
12077 void *pOut
12078);
12079SQLITE_API int sqlite3session_patchset_strm(
12080 sqlite3_session *pSession,
12081 int (*xOutput)(void *pOut, const void *pData, int nData),
12082 void *pOut
12083);
12084SQLITE_API int sqlite3changegroup_add_strm(sqlite3_changegroup*,
12085 int (*xInput)(void *pIn, void *pData, int *pnData),
12086 void *pIn
12087);
12088SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
12089 int (*xOutput)(void *pOut, const void *pData, int nData),
12090 void *pOut
12091);
12092SQLITE_API int sqlite3rebaser_rebase_strm(
12093 sqlite3_rebaser *pRebaser,
12094 int (*xInput)(void *pIn, void *pData, int *pnData),
12095 void *pIn,
12096 int (*xOutput)(void *pOut, const void *pData, int nData),
12097 void *pOut
12098);
12099
12100/*
12101** CAPI3REF: Configure global parameters
12102**
12103** The sqlite3session_config() interface is used to make global configuration
12104** changes to the sessions module in order to tune it to the specific needs
12105** of the application.
12106**
12107** The sqlite3session_config() interface is not threadsafe. If it is invoked
12108** while any other thread is inside any other sessions method then the
12109** results are undefined. Furthermore, if it is invoked after any sessions
12110** related objects have been created, the results are also undefined.
12111**
12112** The first argument to the sqlite3session_config() function must be one
12113** of the SQLITE_SESSION_CONFIG_XXX constants defined below. The
12114** interpretation of the (void*) value passed as the second parameter and
12115** the effect of calling this function depends on the value of the first
12116** parameter.
12117**
12118** <dl>
12119** <dt>SQLITE_SESSION_CONFIG_STRMSIZE<dd>
12120** By default, the sessions module streaming interfaces attempt to input
12121** and output data in approximately 1 KiB chunks. This operand may be used
12122** to set and query the value of this configuration setting. The pointer
12123** passed as the second argument must point to a value of type (int).
12124** If this value is greater than 0, it is used as the new streaming data
12125** chunk size for both input and output. Before returning, the (int) value
12126** pointed to by pArg is set to the final value of the streaming interface
12127** chunk size.
12128** </dl>
12129**
12130** This function returns SQLITE_OK if successful, or an SQLite error code
12131** otherwise.
12132*/
12133SQLITE_API int sqlite3session_config(int op, void *pArg);
12134
12135/*
12136** CAPI3REF: Values for sqlite3session_config().
12137*/
12138#define SQLITE_SESSION_CONFIG_STRMSIZE 1
12139
12140/*
12141** Make sure we can call this stuff from C++.
12142*/
12143#if 0
12144}
12145#endif
12146
12147#endif /* !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) */
12148
12149/******** End of sqlite3session.h *********/
12150/******** Begin file fts5.h *********/
12151/*
12152** 2014 May 31
12153**
12154** The author disclaims copyright to this source code. In place of
12155** a legal notice, here is a blessing:
12156**
12157** May you do good and not evil.
12158** May you find forgiveness for yourself and forgive others.
12159** May you share freely, never taking more than you give.
12160**
12161******************************************************************************
12162**
12163** Interfaces to extend FTS5. Using the interfaces defined in this file,
12164** FTS5 may be extended with:
12165**
12166** * custom tokenizers, and
12167** * custom auxiliary functions.
12168*/
12169
12170
12171#ifndef _FTS5_H
12172#define _FTS5_H
12173
12174
12175#if 0
12176extern "C" {
12177#endif
12178
12179/*************************************************************************
12180** CUSTOM AUXILIARY FUNCTIONS
12181**
12182** Virtual table implementations may overload SQL functions by implementing
12183** the sqlite3_module.xFindFunction() method.
12184*/
12185
12186typedef struct Fts5ExtensionApi Fts5ExtensionApi;
12187typedef struct Fts5Context Fts5Context;
12188typedef struct Fts5PhraseIter Fts5PhraseIter;
12189
12190typedef void (*fts5_extension_function)(
12191 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
12192 Fts5Context *pFts, /* First arg to pass to pApi functions */
12193 sqlite3_context *pCtx, /* Context for returning result/error */
12194 int nVal, /* Number of values in apVal[] array */
12195 sqlite3_value **apVal /* Array of trailing arguments */
12196);
12197
12198struct Fts5PhraseIter {
12199 const unsigned char *a;
12200 const unsigned char *b;
12201};
12202
12203/*
12204** EXTENSION API FUNCTIONS
12205**
12206** xUserData(pFts):
12207** Return a copy of the context pointer the extension function was
12208** registered with.
12209**
12210** xColumnTotalSize(pFts, iCol, pnToken):
12211** If parameter iCol is less than zero, set output variable *pnToken
12212** to the total number of tokens in the FTS5 table. Or, if iCol is
12213** non-negative but less than the number of columns in the table, return
12214** the total number of tokens in column iCol, considering all rows in
12215** the FTS5 table.
12216**
12217** If parameter iCol is greater than or equal to the number of columns
12218** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
12219** an OOM condition or IO error), an appropriate SQLite error code is
12220** returned.
12221**
12222** xColumnCount(pFts):
12223** Return the number of columns in the table.
12224**
12225** xColumnSize(pFts, iCol, pnToken):
12226** If parameter iCol is less than zero, set output variable *pnToken
12227** to the total number of tokens in the current row. Or, if iCol is
12228** non-negative but less than the number of columns in the table, set
12229** *pnToken to the number of tokens in column iCol of the current row.
12230**
12231** If parameter iCol is greater than or equal to the number of columns
12232** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
12233** an OOM condition or IO error), an appropriate SQLite error code is
12234** returned.
12235**
12236** This function may be quite inefficient if used with an FTS5 table
12237** created with the "columnsize=0" option.
12238**
12239** xColumnText:
12240** This function attempts to retrieve the text of column iCol of the
12241** current document. If successful, (*pz) is set to point to a buffer
12242** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
12243** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
12244** if an error occurs, an SQLite error code is returned and the final values
12245** of (*pz) and (*pn) are undefined.
12246**
12247** xPhraseCount:
12248** Returns the number of phrases in the current query expression.
12249**
12250** xPhraseSize:
12251** Returns the number of tokens in phrase iPhrase of the query. Phrases
12252** are numbered starting from zero.
12253**
12254** xInstCount:
12255** Set *pnInst to the total number of occurrences of all phrases within
12256** the query within the current row. Return SQLITE_OK if successful, or
12257** an error code (i.e. SQLITE_NOMEM) if an error occurs.
12258**
12259** This API can be quite slow if used with an FTS5 table created with the
12260** "detail=none" or "detail=column" option. If the FTS5 table is created
12261** with either "detail=none" or "detail=column" and "content=" option
12262** (i.e. if it is a contentless table), then this API always returns 0.
12263**
12264** xInst:
12265** Query for the details of phrase match iIdx within the current row.
12266** Phrase matches are numbered starting from zero, so the iIdx argument
12267** should be greater than or equal to zero and smaller than the value
12268** output by xInstCount().
12269**
12270** Usually, output parameter *piPhrase is set to the phrase number, *piCol
12271** to the column in which it occurs and *piOff the token offset of the
12272** first token of the phrase. The exception is if the table was created
12273** with the offsets=0 option specified. In this case *piOff is always
12274** set to -1.
12275**
12276** Returns SQLITE_OK if successful, or an error code (i.e. SQLITE_NOMEM)
12277** if an error occurs.
12278**
12279** This API can be quite slow if used with an FTS5 table created with the
12280** "detail=none" or "detail=column" option.
12281**
12282** xRowid:
12283** Returns the rowid of the current row.
12284**
12285** xTokenize:
12286** Tokenize text using the tokenizer belonging to the FTS5 table.
12287**
12288** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
12289** This API function is used to query the FTS table for phrase iPhrase
12290** of the current query. Specifically, a query equivalent to:
12291**
12292** ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
12293**
12294** with $p set to a phrase equivalent to the phrase iPhrase of the
12295** current query is executed. Any column filter that applies to
12296** phrase iPhrase of the current query is included in $p. For each
12297** row visited, the callback function passed as the fourth argument
12298** is invoked. The context and API objects passed to the callback
12299** function may be used to access the properties of each matched row.
12300** Invoking Api.xUserData() returns a copy of the pointer passed as
12301** the third argument to pUserData.
12302**
12303** If the callback function returns any value other than SQLITE_OK, the
12304** query is abandoned and the xQueryPhrase function returns immediately.
12305** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
12306** Otherwise, the error code is propagated upwards.
12307**
12308** If the query runs to completion without incident, SQLITE_OK is returned.
12309** Or, if some error occurs before the query completes or is aborted by
12310** the callback, an SQLite error code is returned.
12311**
12312**
12313** xSetAuxdata(pFts5, pAux, xDelete)
12314**
12315** Save the pointer passed as the second argument as the extension functions
12316** "auxiliary data". The pointer may then be retrieved by the current or any
12317** future invocation of the same fts5 extension function made as part of
12318** of the same MATCH query using the xGetAuxdata() API.
12319**
12320** Each extension function is allocated a single auxiliary data slot for
12321** each FTS query (MATCH expression). If the extension function is invoked
12322** more than once for a single FTS query, then all invocations share a
12323** single auxiliary data context.
12324**
12325** If there is already an auxiliary data pointer when this function is
12326** invoked, then it is replaced by the new pointer. If an xDelete callback
12327** was specified along with the original pointer, it is invoked at this
12328** point.
12329**
12330** The xDelete callback, if one is specified, is also invoked on the
12331** auxiliary data pointer after the FTS5 query has finished.
12332**
12333** If an error (e.g. an OOM condition) occurs within this function, an
12334** the auxiliary data is set to NULL and an error code returned. If the
12335** xDelete parameter was not NULL, it is invoked on the auxiliary data
12336** pointer before returning.
12337**
12338**
12339** xGetAuxdata(pFts5, bClear)
12340**
12341** Returns the current auxiliary data pointer for the fts5 extension
12342** function. See the xSetAuxdata() method for details.
12343**
12344** If the bClear argument is non-zero, then the auxiliary data is cleared
12345** (set to NULL) before this function returns. In this case the xDelete,
12346** if any, is not invoked.
12347**
12348**
12349** xRowCount(pFts5, pnRow)
12350**
12351** This function is used to retrieve the total number of rows in the table.
12352** In other words, the same value that would be returned by:
12353**
12354** SELECT count(*) FROM ftstable;
12355**
12356** xPhraseFirst()
12357** This function is used, along with type Fts5PhraseIter and the xPhraseNext
12358** method, to iterate through all instances of a single query phrase within
12359** the current row. This is the same information as is accessible via the
12360** xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient
12361** to use, this API may be faster under some circumstances. To iterate
12362** through instances of phrase iPhrase, use the following code:
12363**
12364** Fts5PhraseIter iter;
12365** int iCol, iOff;
12366** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
12367** iCol>=0;
12368** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
12369** ){
12370** // An instance of phrase iPhrase at offset iOff of column iCol
12371** }
12372**
12373** The Fts5PhraseIter structure is defined above. Applications should not
12374** modify this structure directly - it should only be used as shown above
12375** with the xPhraseFirst() and xPhraseNext() API methods (and by
12376** xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below).
12377**
12378** This API can be quite slow if used with an FTS5 table created with the
12379** "detail=none" or "detail=column" option. If the FTS5 table is created
12380** with either "detail=none" or "detail=column" and "content=" option
12381** (i.e. if it is a contentless table), then this API always iterates
12382** through an empty set (all calls to xPhraseFirst() set iCol to -1).
12383**
12384** xPhraseNext()
12385** See xPhraseFirst above.
12386**
12387** xPhraseFirstColumn()
12388** This function and xPhraseNextColumn() are similar to the xPhraseFirst()
12389** and xPhraseNext() APIs described above. The difference is that instead
12390** of iterating through all instances of a phrase in the current row, these
12391** APIs are used to iterate through the set of columns in the current row
12392** that contain one or more instances of a specified phrase. For example:
12393**
12394** Fts5PhraseIter iter;
12395** int iCol;
12396** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
12397** iCol>=0;
12398** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
12399** ){
12400** // Column iCol contains at least one instance of phrase iPhrase
12401** }
12402**
12403** This API can be quite slow if used with an FTS5 table created with the
12404** "detail=none" option. If the FTS5 table is created with either
12405** "detail=none" "content=" option (i.e. if it is a contentless table),
12406** then this API always iterates through an empty set (all calls to
12407** xPhraseFirstColumn() set iCol to -1).
12408**
12409** The information accessed using this API and its companion
12410** xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext
12411** (or xInst/xInstCount). The chief advantage of this API is that it is
12412** significantly more efficient than those alternatives when used with
12413** "detail=column" tables.
12414**
12415** xPhraseNextColumn()
12416** See xPhraseFirstColumn above.
12417*/
12418struct Fts5ExtensionApi {
12419 int iVersion; /* Currently always set to 3 */
12420
12421 void *(*xUserData)(Fts5Context*);
12422
12423 int (*xColumnCount)(Fts5Context*);
12424 int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
12425 int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
12426
12427 int (*xTokenize)(Fts5Context*,
12428 const char *pText, int nText, /* Text to tokenize */
12429 void *pCtx, /* Context passed to xToken() */
12430 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
12431 );
12432
12433 int (*xPhraseCount)(Fts5Context*);
12434 int (*xPhraseSize)(Fts5Context*, int iPhrase);
12435
12436 int (*xInstCount)(Fts5Context*, int *pnInst);
12437 int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
12438
12439 sqlite3_int64 (*xRowid)(Fts5Context*);
12440 int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
12441 int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
12442
12443 int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
12444 int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
12445 );
12446 int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
12447 void *(*xGetAuxdata)(Fts5Context*, int bClear);
12448
12449 int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
12450 void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
12451
12452 int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
12453 void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
12454};
12455
12456/*
12457** CUSTOM AUXILIARY FUNCTIONS
12458*************************************************************************/
12459
12460/*************************************************************************
12461** CUSTOM TOKENIZERS
12462**
12463** Applications may also register custom tokenizer types. A tokenizer
12464** is registered by providing fts5 with a populated instance of the
12465** following structure. All structure methods must be defined, setting
12466** any member of the fts5_tokenizer struct to NULL leads to undefined
12467** behaviour. The structure methods are expected to function as follows:
12468**
12469** xCreate:
12470** This function is used to allocate and initialize a tokenizer instance.
12471** A tokenizer instance is required to actually tokenize text.
12472**
12473** The first argument passed to this function is a copy of the (void*)
12474** pointer provided by the application when the fts5_tokenizer object
12475** was registered with FTS5 (the third argument to xCreateTokenizer()).
12476** The second and third arguments are an array of nul-terminated strings
12477** containing the tokenizer arguments, if any, specified following the
12478** tokenizer name as part of the CREATE VIRTUAL TABLE statement used
12479** to create the FTS5 table.
12480**
12481** The final argument is an output variable. If successful, (*ppOut)
12482** should be set to point to the new tokenizer handle and SQLITE_OK
12483** returned. If an error occurs, some value other than SQLITE_OK should
12484** be returned. In this case, fts5 assumes that the final value of *ppOut
12485** is undefined.
12486**
12487** xDelete:
12488** This function is invoked to delete a tokenizer handle previously
12489** allocated using xCreate(). Fts5 guarantees that this function will
12490** be invoked exactly once for each successful call to xCreate().
12491**
12492** xTokenize:
12493** This function is expected to tokenize the nText byte string indicated
12494** by argument pText. pText may or may not be nul-terminated. The first
12495** argument passed to this function is a pointer to an Fts5Tokenizer object
12496** returned by an earlier call to xCreate().
12497**
12498** The second argument indicates the reason that FTS5 is requesting
12499** tokenization of the supplied text. This is always one of the following
12500** four values:
12501**
12502** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
12503** or removed from the FTS table. The tokenizer is being invoked to
12504** determine the set of tokens to add to (or delete from) the
12505** FTS index.
12506**
12507** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
12508** against the FTS index. The tokenizer is being called to tokenize
12509** a bareword or quoted string specified as part of the query.
12510**
12511** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
12512** FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is
12513** followed by a "*" character, indicating that the last token
12514** returned by the tokenizer will be treated as a token prefix.
12515**
12516** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
12517** satisfy an fts5_api.xTokenize() request made by an auxiliary
12518** function. Or an fts5_api.xColumnSize() request made by the same
12519** on a columnsize=0 database.
12520** </ul>
12521**
12522** For each token in the input string, the supplied callback xToken() must
12523** be invoked. The first argument to it should be a copy of the pointer
12524** passed as the second argument to xTokenize(). The third and fourth
12525** arguments are a pointer to a buffer containing the token text, and the
12526** size of the token in bytes. The 4th and 5th arguments are the byte offsets
12527** of the first byte of and first byte immediately following the text from
12528** which the token is derived within the input.
12529**
12530** The second argument passed to the xToken() callback ("tflags") should
12531** normally be set to 0. The exception is if the tokenizer supports
12532** synonyms. In this case see the discussion below for details.
12533**
12534** FTS5 assumes the xToken() callback is invoked for each token in the
12535** order that they occur within the input text.
12536**
12537** If an xToken() callback returns any value other than SQLITE_OK, then
12538** the tokenization should be abandoned and the xTokenize() method should
12539** immediately return a copy of the xToken() return value. Or, if the
12540** input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally,
12541** if an error occurs with the xTokenize() implementation itself, it
12542** may abandon the tokenization and return any error code other than
12543** SQLITE_OK or SQLITE_DONE.
12544**
12545** SYNONYM SUPPORT
12546**
12547** Custom tokenizers may also support synonyms. Consider a case in which a
12548** user wishes to query for a phrase such as "first place". Using the
12549** built-in tokenizers, the FTS5 query 'first + place' will match instances
12550** of "first place" within the document set, but not alternative forms
12551** such as "1st place". In some applications, it would be better to match
12552** all instances of "first place" or "1st place" regardless of which form
12553** the user specified in the MATCH query text.
12554**
12555** There are several ways to approach this in FTS5:
12556**
12557** <ol><li> By mapping all synonyms to a single token. In this case, the
12558** In the above example, this means that the tokenizer returns the
12559** same token for inputs "first" and "1st". Say that token is in
12560** fact "first", so that when the user inserts the document "I won
12561** 1st place" entries are added to the index for tokens "i", "won",
12562** "first" and "place". If the user then queries for '1st + place',
12563** the tokenizer substitutes "first" for "1st" and the query works
12564** as expected.
12565**
12566** <li> By adding multiple synonyms for a single term to the FTS index.
12567** In this case, when tokenizing query text, the tokenizer may
12568** provide multiple synonyms for a single term within the document.
12569** FTS5 then queries the index for each synonym individually. For
12570** example, faced with the query:
12571**
12572** <codeblock>
12573** ... MATCH 'first place'</codeblock>
12574**
12575** the tokenizer offers both "1st" and "first" as synonyms for the
12576** first token in the MATCH query and FTS5 effectively runs a query
12577** similar to:
12578**
12579** <codeblock>
12580** ... MATCH '(first OR 1st) place'</codeblock>
12581**
12582** except that, for the purposes of auxiliary functions, the query
12583** still appears to contain just two phrases - "(first OR 1st)"
12584** being treated as a single phrase.
12585**
12586** <li> By adding multiple synonyms for a single term to the FTS index.
12587** Using this method, when tokenizing document text, the tokenizer
12588** provides multiple synonyms for each token. So that when a
12589** document such as "I won first place" is tokenized, entries are
12590** added to the FTS index for "i", "won", "first", "1st" and
12591** "place".
12592**
12593** This way, even if the tokenizer does not provide synonyms
12594** when tokenizing query text (it should not - to do would be
12595** inefficient), it doesn't matter if the user queries for
12596** 'first + place' or '1st + place', as there are entries in the
12597** FTS index corresponding to both forms of the first token.
12598** </ol>
12599**
12600** Whether it is parsing document or query text, any call to xToken that
12601** specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
12602** is considered to supply a synonym for the previous token. For example,
12603** when parsing the document "I won first place", a tokenizer that supports
12604** synonyms would call xToken() 5 times, as follows:
12605**
12606** <codeblock>
12607** xToken(pCtx, 0, "i", 1, 0, 1);
12608** xToken(pCtx, 0, "won", 3, 2, 5);
12609** xToken(pCtx, 0, "first", 5, 6, 11);
12610** xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3, 6, 11);
12611** xToken(pCtx, 0, "place", 5, 12, 17);
12612**</codeblock>
12613**
12614** It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time
12615** xToken() is called. Multiple synonyms may be specified for a single token
12616** by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence.
12617** There is no limit to the number of synonyms that may be provided for a
12618** single token.
12619**
12620** In many cases, method (1) above is the best approach. It does not add
12621** extra data to the FTS index or require FTS5 to query for multiple terms,
12622** so it is efficient in terms of disk space and query speed. However, it
12623** does not support prefix queries very well. If, as suggested above, the
12624** token "first" is substituted for "1st" by the tokenizer, then the query:
12625**
12626** <codeblock>
12627** ... MATCH '1s*'</codeblock>
12628**
12629** will not match documents that contain the token "1st" (as the tokenizer
12630** will probably not map "1s" to any prefix of "first").
12631**
12632** For full prefix support, method (3) may be preferred. In this case,
12633** because the index contains entries for both "first" and "1st", prefix
12634** queries such as 'fi*' or '1s*' will match correctly. However, because
12635** extra entries are added to the FTS index, this method uses more space
12636** within the database.
12637**
12638** Method (2) offers a midpoint between (1) and (3). Using this method,
12639** a query such as '1s*' will match documents that contain the literal
12640** token "1st", but not "first" (assuming the tokenizer is not able to
12641** provide synonyms for prefixes). However, a non-prefix query like '1st'
12642** will match against "1st" and "first". This method does not require
12643** extra disk space, as no extra entries are added to the FTS index.
12644** On the other hand, it may require more CPU cycles to run MATCH queries,
12645** as separate queries of the FTS index are required for each synonym.
12646**
12647** When using methods (2) or (3), it is important that the tokenizer only
12648** provide synonyms when tokenizing document text (method (2)) or query
12649** text (method (3)), not both. Doing so will not cause any errors, but is
12650** inefficient.
12651*/
12652typedef struct Fts5Tokenizer Fts5Tokenizer;
12653typedef struct fts5_tokenizer fts5_tokenizer;
12654struct fts5_tokenizer {
12655 int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
12656 void (*xDelete)(Fts5Tokenizer*);
12657 int (*xTokenize)(Fts5Tokenizer*,
12658 void *pCtx,
12659 int flags, /* Mask of FTS5_TOKENIZE_* flags */
12660 const char *pText, int nText,
12661 int (*xToken)(
12662 void *pCtx, /* Copy of 2nd argument to xTokenize() */
12663 int tflags, /* Mask of FTS5_TOKEN_* flags */
12664 const char *pToken, /* Pointer to buffer containing token */
12665 int nToken, /* Size of token in bytes */
12666 int iStart, /* Byte offset of token within input text */
12667 int iEnd /* Byte offset of end of token within input text */
12668 )
12669 );
12670};
12671
12672/* Flags that may be passed as the third argument to xTokenize() */
12673#define FTS5_TOKENIZE_QUERY 0x0001
12674#define FTS5_TOKENIZE_PREFIX 0x0002
12675#define FTS5_TOKENIZE_DOCUMENT 0x0004
12676#define FTS5_TOKENIZE_AUX 0x0008
12677
12678/* Flags that may be passed by the tokenizer implementation back to FTS5
12679** as the third argument to the supplied xToken callback. */
12680#define FTS5_TOKEN_COLOCATED 0x0001 /* Same position as prev. token */
12681
12682/*
12683** END OF CUSTOM TOKENIZERS
12684*************************************************************************/
12685
12686/*************************************************************************
12687** FTS5 EXTENSION REGISTRATION API
12688*/
12689typedef struct fts5_api fts5_api;
12690struct fts5_api {
12691 int iVersion; /* Currently always set to 2 */
12692
12693 /* Create a new tokenizer */
12694 int (*xCreateTokenizer)(
12695 fts5_api *pApi,
12696 const char *zName,
12697 void *pContext,
12698 fts5_tokenizer *pTokenizer,
12699 void (*xDestroy)(void*)
12700 );
12701
12702 /* Find an existing tokenizer */
12703 int (*xFindTokenizer)(
12704 fts5_api *pApi,
12705 const char *zName,
12706 void **ppContext,
12707 fts5_tokenizer *pTokenizer
12708 );
12709
12710 /* Create a new auxiliary function */
12711 int (*xCreateFunction)(
12712 fts5_api *pApi,
12713 const char *zName,
12714 void *pContext,
12715 fts5_extension_function xFunction,
12716 void (*xDestroy)(void*)
12717 );
12718};
12719
12720/*
12721** END OF REGISTRATION API
12722*************************************************************************/
12723
12724#if 0
12725} /* end of the 'extern "C"' block */
12726#endif
12727
12728#endif /* _FTS5_H */
12729
12730/******** End of fts5.h *********/
12731
12732/************** End of sqlite3.h *********************************************/
12733/************** Continuing where we left off in sqliteInt.h ******************/
12734
12735/*
12736** Include the configuration header output by 'configure' if we're using the
12737** autoconf-based build
12738*/
12739#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
12740/* #include "config.h" */
12741#define SQLITECONFIG_H 1
12742#endif
12743
12744/************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
12745/************** Begin file sqliteLimit.h *************************************/
12746/*
12747** 2007 May 7
12748**
12749** The author disclaims copyright to this source code. In place of
12750** a legal notice, here is a blessing:
12751**
12752** May you do good and not evil.
12753** May you find forgiveness for yourself and forgive others.
12754** May you share freely, never taking more than you give.
12755**
12756*************************************************************************
12757**
12758** This file defines various limits of what SQLite can process.
12759*/
12760
12761/*
12762** The maximum length of a TEXT or BLOB in bytes. This also
12763** limits the size of a row in a table or index.
12764**
12765** The hard limit is the ability of a 32-bit signed integer
12766** to count the size: 2^31-1 or 2147483647.
12767*/
12768#ifndef SQLITE_MAX_LENGTH
12769# define SQLITE_MAX_LENGTH 1000000000
12770#endif
12771
12772/*
12773** This is the maximum number of
12774**
12775** * Columns in a table
12776** * Columns in an index
12777** * Columns in a view
12778** * Terms in the SET clause of an UPDATE statement
12779** * Terms in the result set of a SELECT statement
12780** * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
12781** * Terms in the VALUES clause of an INSERT statement
12782**
12783** The hard upper limit here is 32676. Most database people will
12784** tell you that in a well-normalized database, you usually should
12785** not have more than a dozen or so columns in any table. And if
12786** that is the case, there is no point in having more than a few
12787** dozen values in any of the other situations described above.
12788*/
12789#ifndef SQLITE_MAX_COLUMN
12790# define SQLITE_MAX_COLUMN 2000
12791#endif
12792
12793/*
12794** The maximum length of a single SQL statement in bytes.
12795**
12796** It used to be the case that setting this value to zero would
12797** turn the limit off. That is no longer true. It is not possible
12798** to turn this limit off.
12799*/
12800#ifndef SQLITE_MAX_SQL_LENGTH
12801# define SQLITE_MAX_SQL_LENGTH 1000000000
12802#endif
12803
12804/*
12805** The maximum depth of an expression tree. This is limited to
12806** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might
12807** want to place more severe limits on the complexity of an
12808** expression.
12809**
12810** A value of 0 used to mean that the limit was not enforced.
12811** But that is no longer true. The limit is now strictly enforced
12812** at all times.
12813*/
12814#ifndef SQLITE_MAX_EXPR_DEPTH
12815# define SQLITE_MAX_EXPR_DEPTH 1000
12816#endif
12817
12818/*
12819** The maximum number of terms in a compound SELECT statement.
12820** The code generator for compound SELECT statements does one
12821** level of recursion for each term. A stack overflow can result
12822** if the number of terms is too large. In practice, most SQL
12823** never has more than 3 or 4 terms. Use a value of 0 to disable
12824** any limit on the number of terms in a compount SELECT.
12825*/
12826#ifndef SQLITE_MAX_COMPOUND_SELECT
12827# define SQLITE_MAX_COMPOUND_SELECT 500
12828#endif
12829
12830/*
12831** The maximum number of opcodes in a VDBE program.
12832** Not currently enforced.
12833*/
12834#ifndef SQLITE_MAX_VDBE_OP
12835# define SQLITE_MAX_VDBE_OP 250000000
12836#endif
12837
12838/*
12839** The maximum number of arguments to an SQL function.
12840*/
12841#ifndef SQLITE_MAX_FUNCTION_ARG
12842# define SQLITE_MAX_FUNCTION_ARG 127
12843#endif
12844
12845/*
12846** The suggested maximum number of in-memory pages to use for
12847** the main database table and for temporary tables.
12848**
12849** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000,
12850** which means the cache size is limited to 2048000 bytes of memory.
12851** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be
12852** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options.
12853*/
12854#ifndef SQLITE_DEFAULT_CACHE_SIZE
12855# define SQLITE_DEFAULT_CACHE_SIZE -2000
12856#endif
12857
12858/*
12859** The default number of frames to accumulate in the log file before
12860** checkpointing the database in WAL mode.
12861*/
12862#ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
12863# define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT 1000
12864#endif
12865
12866/*
12867** The maximum number of attached databases. This must be between 0
12868** and 125. The upper bound of 125 is because the attached databases are
12869** counted using a signed 8-bit integer which has a maximum value of 127
12870** and we have to allow 2 extra counts for the "main" and "temp" databases.
12871*/
12872#ifndef SQLITE_MAX_ATTACHED
12873# define SQLITE_MAX_ATTACHED 10
12874#endif
12875
12876
12877/*
12878** The maximum value of a ?nnn wildcard that the parser will accept.
12879*/
12880#ifndef SQLITE_MAX_VARIABLE_NUMBER
12881# define SQLITE_MAX_VARIABLE_NUMBER 999
12882#endif
12883
12884/* Maximum page size. The upper bound on this value is 65536. This a limit
12885** imposed by the use of 16-bit offsets within each page.
12886**
12887** Earlier versions of SQLite allowed the user to change this value at
12888** compile time. This is no longer permitted, on the grounds that it creates
12889** a library that is technically incompatible with an SQLite library
12890** compiled with a different limit. If a process operating on a database
12891** with a page-size of 65536 bytes crashes, then an instance of SQLite
12892** compiled with the default page-size limit will not be able to rollback
12893** the aborted transaction. This could lead to database corruption.
12894*/
12895#ifdef SQLITE_MAX_PAGE_SIZE
12896# undef SQLITE_MAX_PAGE_SIZE
12897#endif
12898#define SQLITE_MAX_PAGE_SIZE 65536
12899
12900
12901/*
12902** The default size of a database page.
12903*/
12904#ifndef SQLITE_DEFAULT_PAGE_SIZE
12905# define SQLITE_DEFAULT_PAGE_SIZE 4096
12906#endif
12907#if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
12908# undef SQLITE_DEFAULT_PAGE_SIZE
12909# define SQLITE_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
12910#endif
12911
12912/*
12913** Ordinarily, if no value is explicitly provided, SQLite creates databases
12914** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
12915** device characteristics (sector-size and atomic write() support),
12916** SQLite may choose a larger value. This constant is the maximum value
12917** SQLite will choose on its own.
12918*/
12919#ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE
12920# define SQLITE_MAX_DEFAULT_PAGE_SIZE 8192
12921#endif
12922#if SQLITE_MAX_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
12923# undef SQLITE_MAX_DEFAULT_PAGE_SIZE
12924# define SQLITE_MAX_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
12925#endif
12926
12927
12928/*
12929** Maximum number of pages in one database file.
12930**
12931** This is really just the default value for the max_page_count pragma.
12932** This value can be lowered (or raised) at run-time using that the
12933** max_page_count macro.
12934*/
12935#ifndef SQLITE_MAX_PAGE_COUNT
12936# define SQLITE_MAX_PAGE_COUNT 1073741823
12937#endif
12938
12939/*
12940** Maximum length (in bytes) of the pattern in a LIKE or GLOB
12941** operator.
12942*/
12943#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
12944# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
12945#endif
12946
12947/*
12948** Maximum depth of recursion for triggers.
12949**
12950** A value of 1 means that a trigger program will not be able to itself
12951** fire any triggers. A value of 0 means that no trigger programs at all
12952** may be executed.
12953*/
12954#ifndef SQLITE_MAX_TRIGGER_DEPTH
12955# define SQLITE_MAX_TRIGGER_DEPTH 1000
12956#endif
12957
12958/************** End of sqliteLimit.h *****************************************/
12959/************** Continuing where we left off in sqliteInt.h ******************/
12960
12961/* Disable nuisance warnings on Borland compilers */
12962#if defined(__BORLANDC__)
12963#pragma warn -rch /* unreachable code */
12964#pragma warn -ccc /* Condition is always true or false */
12965#pragma warn -aus /* Assigned value is never used */
12966#pragma warn -csu /* Comparing signed and unsigned */
12967#pragma warn -spa /* Suspicious pointer arithmetic */
12968#endif
12969
12970/*
12971** Include standard header files as necessary
12972*/
12973#ifdef HAVE_STDINT_H
12974#include <stdint.h>
12975#endif
12976#ifdef HAVE_INTTYPES_H
12977#include <inttypes.h>
12978#endif
12979
12980/*
12981** The following macros are used to cast pointers to integers and
12982** integers to pointers. The way you do this varies from one compiler
12983** to the next, so we have developed the following set of #if statements
12984** to generate appropriate macros for a wide range of compilers.
12985**
12986** The correct "ANSI" way to do this is to use the intptr_t type.
12987** Unfortunately, that typedef is not available on all compilers, or
12988** if it is available, it requires an #include of specific headers
12989** that vary from one machine to the next.
12990**
12991** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
12992** the ((void*)&((char*)0)[X]) construct. But MSVC chokes on ((void*)(X)).
12993** So we have to define the macros in different ways depending on the
12994** compiler.
12995*/
12996#if defined(__PTRDIFF_TYPE__) /* This case should work for GCC */
12997# define SQLITE_INT_TO_PTR(X) ((void*)(__PTRDIFF_TYPE__)(X))
12998# define SQLITE_PTR_TO_INT(X) ((int)(__PTRDIFF_TYPE__)(X))
12999#elif !defined(__GNUC__) /* Works for compilers other than LLVM */
13000# define SQLITE_INT_TO_PTR(X) ((void*)&((char*)0)[X])
13001# define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0))
13002#elif defined(HAVE_STDINT_H) /* Use this case if we have ANSI headers */
13003# define SQLITE_INT_TO_PTR(X) ((void*)(intptr_t)(X))
13004# define SQLITE_PTR_TO_INT(X) ((int)(intptr_t)(X))
13005#else /* Generates a warning - but it always works */
13006# define SQLITE_INT_TO_PTR(X) ((void*)(X))
13007# define SQLITE_PTR_TO_INT(X) ((int)(X))
13008#endif
13009
13010/*
13011** A macro to hint to the compiler that a function should not be
13012** inlined.
13013*/
13014#if defined(__GNUC__)
13015# define SQLITE_NOINLINE __attribute__((noinline))
13016#elif defined(_MSC_VER) && _MSC_VER>=1310
13017# define SQLITE_NOINLINE __declspec(noinline)
13018#else
13019# define SQLITE_NOINLINE
13020#endif
13021
13022/*
13023** Make sure that the compiler intrinsics we desire are enabled when
13024** compiling with an appropriate version of MSVC unless prevented by
13025** the SQLITE_DISABLE_INTRINSIC define.
13026*/
13027#if !defined(SQLITE_DISABLE_INTRINSIC)
13028# if defined(_MSC_VER) && _MSC_VER>=1400
13029# if !defined(_WIN32_WCE)
13030# include <intrin.h>
13031# pragma intrinsic(_byteswap_ushort)
13032# pragma intrinsic(_byteswap_ulong)
13033# pragma intrinsic(_byteswap_uint64)
13034# pragma intrinsic(_ReadWriteBarrier)
13035# else
13036# include <cmnintrin.h>
13037# endif
13038# endif
13039#endif
13040
13041/*
13042** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
13043** 0 means mutexes are permanently disable and the library is never
13044** threadsafe. 1 means the library is serialized which is the highest
13045** level of threadsafety. 2 means the library is multithreaded - multiple
13046** threads can use SQLite as long as no two threads try to use the same
13047** database connection at the same time.
13048**
13049** Older versions of SQLite used an optional THREADSAFE macro.
13050** We support that for legacy.
13051**
13052** To ensure that the correct value of "THREADSAFE" is reported when querying
13053** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this
13054** logic is partially replicated in ctime.c. If it is updated here, it should
13055** also be updated there.
13056*/
13057#if !defined(SQLITE_THREADSAFE)
13058# if defined(THREADSAFE)
13059# define SQLITE_THREADSAFE THREADSAFE
13060# else
13061# define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
13062# endif
13063#endif
13064
13065/*
13066** Powersafe overwrite is on by default. But can be turned off using
13067** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
13068*/
13069#ifndef SQLITE_POWERSAFE_OVERWRITE
13070# define SQLITE_POWERSAFE_OVERWRITE 1
13071#endif
13072
13073/*
13074** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
13075** default unless SQLite is compiled with SQLITE_DEFAULT_MEMSTATUS=0 in
13076** which case memory allocation statistics are disabled by default.
13077*/
13078#if !defined(SQLITE_DEFAULT_MEMSTATUS)
13079# define SQLITE_DEFAULT_MEMSTATUS 1
13080#endif
13081
13082/*
13083** Exactly one of the following macros must be defined in order to
13084** specify which memory allocation subsystem to use.
13085**
13086** SQLITE_SYSTEM_MALLOC // Use normal system malloc()
13087** SQLITE_WIN32_MALLOC // Use Win32 native heap API
13088** SQLITE_ZERO_MALLOC // Use a stub allocator that always fails
13089** SQLITE_MEMDEBUG // Debugging version of system malloc()
13090**
13091** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
13092** assert() macro is enabled, each call into the Win32 native heap subsystem
13093** will cause HeapValidate to be called. If heap validation should fail, an
13094** assertion will be triggered.
13095**
13096** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
13097** the default.
13098*/
13099#if defined(SQLITE_SYSTEM_MALLOC) \
13100 + defined(SQLITE_WIN32_MALLOC) \
13101 + defined(SQLITE_ZERO_MALLOC) \
13102 + defined(SQLITE_MEMDEBUG)>1
13103# error "Two or more of the following compile-time configuration options\
13104 are defined but at most one is allowed:\
13105 SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\
13106 SQLITE_ZERO_MALLOC"
13107#endif
13108#if defined(SQLITE_SYSTEM_MALLOC) \
13109 + defined(SQLITE_WIN32_MALLOC) \
13110 + defined(SQLITE_ZERO_MALLOC) \
13111 + defined(SQLITE_MEMDEBUG)==0
13112# define SQLITE_SYSTEM_MALLOC 1
13113#endif
13114
13115/*
13116** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
13117** sizes of memory allocations below this value where possible.
13118*/
13119#if !defined(SQLITE_MALLOC_SOFT_LIMIT)
13120# define SQLITE_MALLOC_SOFT_LIMIT 1024
13121#endif
13122
13123/*
13124** We need to define _XOPEN_SOURCE as follows in order to enable
13125** recursive mutexes on most Unix systems and fchmod() on OpenBSD.
13126** But _XOPEN_SOURCE define causes problems for Mac OS X, so omit
13127** it.
13128*/
13129#if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__)
13130# define _XOPEN_SOURCE 600
13131#endif
13132
13133/*
13134** NDEBUG and SQLITE_DEBUG are opposites. It should always be true that
13135** defined(NDEBUG)==!defined(SQLITE_DEBUG). If this is not currently true,
13136** make it true by defining or undefining NDEBUG.
13137**
13138** Setting NDEBUG makes the code smaller and faster by disabling the
13139** assert() statements in the code. So we want the default action
13140** to be for NDEBUG to be set and NDEBUG to be undefined only if SQLITE_DEBUG
13141** is set. Thus NDEBUG becomes an opt-in rather than an opt-out
13142** feature.
13143*/
13144#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
13145# define NDEBUG 1
13146#endif
13147#if defined(NDEBUG) && defined(SQLITE_DEBUG)
13148# undef NDEBUG
13149#endif
13150
13151/*
13152** Enable SQLITE_ENABLE_EXPLAIN_COMMENTS if SQLITE_DEBUG is turned on.
13153*/
13154#if !defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) && defined(SQLITE_DEBUG)
13155# define SQLITE_ENABLE_EXPLAIN_COMMENTS 1
13156#endif
13157
13158/*
13159** The testcase() macro is used to aid in coverage testing. When
13160** doing coverage testing, the condition inside the argument to
13161** testcase() must be evaluated both true and false in order to
13162** get full branch coverage. The testcase() macro is inserted
13163** to help ensure adequate test coverage in places where simple
13164** condition/decision coverage is inadequate. For example, testcase()
13165** can be used to make sure boundary values are tested. For
13166** bitmask tests, testcase() can be used to make sure each bit
13167** is significant and used at least once. On switch statements
13168** where multiple cases go to the same block of code, testcase()
13169** can insure that all cases are evaluated.
13170**
13171*/
13172#ifdef SQLITE_COVERAGE_TEST
13173SQLITE_PRIVATE void sqlite3Coverage(int);
13174# define testcase(X) if( X ){ sqlite3Coverage(__LINE__); }
13175#else
13176# define testcase(X)
13177#endif
13178
13179/*
13180** The TESTONLY macro is used to enclose variable declarations or
13181** other bits of code that are needed to support the arguments
13182** within testcase() and assert() macros.
13183*/
13184#if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
13185# define TESTONLY(X) X
13186#else
13187# define TESTONLY(X)
13188#endif
13189
13190/*
13191** Sometimes we need a small amount of code such as a variable initialization
13192** to setup for a later assert() statement. We do not want this code to
13193** appear when assert() is disabled. The following macro is therefore
13194** used to contain that setup code. The "VVA" acronym stands for
13195** "Verification, Validation, and Accreditation". In other words, the
13196** code within VVA_ONLY() will only run during verification processes.
13197*/
13198#ifndef NDEBUG
13199# define VVA_ONLY(X) X
13200#else
13201# define VVA_ONLY(X)
13202#endif
13203
13204/*
13205** The ALWAYS and NEVER macros surround boolean expressions which
13206** are intended to always be true or false, respectively. Such
13207** expressions could be omitted from the code completely. But they
13208** are included in a few cases in order to enhance the resilience
13209** of SQLite to unexpected behavior - to make the code "self-healing"
13210** or "ductile" rather than being "brittle" and crashing at the first
13211** hint of unplanned behavior.
13212**
13213** In other words, ALWAYS and NEVER are added for defensive code.
13214**
13215** When doing coverage testing ALWAYS and NEVER are hard-coded to
13216** be true and false so that the unreachable code they specify will
13217** not be counted as untested code.
13218*/
13219#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
13220# define ALWAYS(X) (1)
13221# define NEVER(X) (0)
13222#elif !defined(NDEBUG)
13223# define ALWAYS(X) ((X)?1:(assert(0),0))
13224# define NEVER(X) ((X)?(assert(0),1):0)
13225#else
13226# define ALWAYS(X) (X)
13227# define NEVER(X) (X)
13228#endif
13229
13230/*
13231** Some conditionals are optimizations only. In other words, if the
13232** conditionals are replaced with a constant 1 (true) or 0 (false) then
13233** the correct answer is still obtained, though perhaps not as quickly.
13234**
13235** The following macros mark these optimizations conditionals.
13236*/
13237#if defined(SQLITE_MUTATION_TEST)
13238# define OK_IF_ALWAYS_TRUE(X) (1)
13239# define OK_IF_ALWAYS_FALSE(X) (0)
13240#else
13241# define OK_IF_ALWAYS_TRUE(X) (X)
13242# define OK_IF_ALWAYS_FALSE(X) (X)
13243#endif
13244
13245/*
13246** Some malloc failures are only possible if SQLITE_TEST_REALLOC_STRESS is
13247** defined. We need to defend against those failures when testing with
13248** SQLITE_TEST_REALLOC_STRESS, but we don't want the unreachable branches
13249** during a normal build. The following macro can be used to disable tests
13250** that are always false except when SQLITE_TEST_REALLOC_STRESS is set.
13251*/
13252#if defined(SQLITE_TEST_REALLOC_STRESS)
13253# define ONLY_IF_REALLOC_STRESS(X) (X)
13254#elif !defined(NDEBUG)
13255# define ONLY_IF_REALLOC_STRESS(X) ((X)?(assert(0),1):0)
13256#else
13257# define ONLY_IF_REALLOC_STRESS(X) (0)
13258#endif
13259
13260/*
13261** Declarations used for tracing the operating system interfaces.
13262*/
13263#if defined(SQLITE_FORCE_OS_TRACE) || defined(SQLITE_TEST) || \
13264 (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
13265 extern int sqlite3OSTrace;
13266# define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
13267# define SQLITE_HAVE_OS_TRACE
13268#else
13269# define OSTRACE(X)
13270# undef SQLITE_HAVE_OS_TRACE
13271#endif
13272
13273/*
13274** Is the sqlite3ErrName() function needed in the build? Currently,
13275** it is needed by "mutex_w32.c" (when debugging), "os_win.c" (when
13276** OSTRACE is enabled), and by several "test*.c" files (which are
13277** compiled using SQLITE_TEST).
13278*/
13279#if defined(SQLITE_HAVE_OS_TRACE) || defined(SQLITE_TEST) || \
13280 (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
13281# define SQLITE_NEED_ERR_NAME
13282#else
13283# undef SQLITE_NEED_ERR_NAME
13284#endif
13285
13286/*
13287** SQLITE_ENABLE_EXPLAIN_COMMENTS is incompatible with SQLITE_OMIT_EXPLAIN
13288*/
13289#ifdef SQLITE_OMIT_EXPLAIN
13290# undef SQLITE_ENABLE_EXPLAIN_COMMENTS
13291#endif
13292
13293/*
13294** Return true (non-zero) if the input is an integer that is too large
13295** to fit in 32-bits. This macro is used inside of various testcase()
13296** macros to verify that we have tested SQLite for large-file support.
13297*/
13298#define IS_BIG_INT(X) (((X)&~(i64)0xffffffff)!=0)
13299
13300/*
13301** The macro unlikely() is a hint that surrounds a boolean
13302** expression that is usually false. Macro likely() surrounds
13303** a boolean expression that is usually true. These hints could,
13304** in theory, be used by the compiler to generate better code, but
13305** currently they are just comments for human readers.
13306*/
13307#define likely(X) (X)
13308#define unlikely(X) (X)
13309
13310/************** Include hash.h in the middle of sqliteInt.h ******************/
13311/************** Begin file hash.h ********************************************/
13312/*
13313** 2001 September 22
13314**
13315** The author disclaims copyright to this source code. In place of
13316** a legal notice, here is a blessing:
13317**
13318** May you do good and not evil.
13319** May you find forgiveness for yourself and forgive others.
13320** May you share freely, never taking more than you give.
13321**
13322*************************************************************************
13323** This is the header file for the generic hash-table implementation
13324** used in SQLite.
13325*/
13326#ifndef SQLITE_HASH_H
13327#define SQLITE_HASH_H
13328
13329/* Forward declarations of structures. */
13330typedef struct Hash Hash;
13331typedef struct HashElem HashElem;
13332
13333/* A complete hash table is an instance of the following structure.
13334** The internals of this structure are intended to be opaque -- client
13335** code should not attempt to access or modify the fields of this structure
13336** directly. Change this structure only by using the routines below.
13337** However, some of the "procedures" and "functions" for modifying and
13338** accessing this structure are really macros, so we can't really make
13339** this structure opaque.
13340**
13341** All elements of the hash table are on a single doubly-linked list.
13342** Hash.first points to the head of this list.
13343**
13344** There are Hash.htsize buckets. Each bucket points to a spot in
13345** the global doubly-linked list. The contents of the bucket are the
13346** element pointed to plus the next _ht.count-1 elements in the list.
13347**
13348** Hash.htsize and Hash.ht may be zero. In that case lookup is done
13349** by a linear search of the global list. For small tables, the
13350** Hash.ht table is never allocated because if there are few elements
13351** in the table, it is faster to do a linear search than to manage
13352** the hash table.
13353*/
13354struct Hash {
13355 unsigned int htsize; /* Number of buckets in the hash table */
13356 unsigned int count; /* Number of entries in this table */
13357 HashElem *first; /* The first element of the array */
13358 struct _ht { /* the hash table */
13359 int count; /* Number of entries with this hash */
13360 HashElem *chain; /* Pointer to first entry with this hash */
13361 } *ht;
13362};
13363
13364/* Each element in the hash table is an instance of the following
13365** structure. All elements are stored on a single doubly-linked list.
13366**
13367** Again, this structure is intended to be opaque, but it can't really
13368** be opaque because it is used by macros.
13369*/
13370struct HashElem {
13371 HashElem *next, *prev; /* Next and previous elements in the table */
13372 void *data; /* Data associated with this element */
13373 const char *pKey; /* Key associated with this element */
13374};
13375
13376/*
13377** Access routines. To delete, insert a NULL pointer.
13378*/
13379SQLITE_PRIVATE void sqlite3HashInit(Hash*);
13380SQLITE_PRIVATE void *sqlite3HashInsert(Hash*, const char *pKey, void *pData);
13381SQLITE_PRIVATE void *sqlite3HashFind(const Hash*, const char *pKey);
13382SQLITE_PRIVATE void sqlite3HashClear(Hash*);
13383
13384/*
13385** Macros for looping over all elements of a hash table. The idiom is
13386** like this:
13387**
13388** Hash h;
13389** HashElem *p;
13390** ...
13391** for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
13392** SomeStructure *pData = sqliteHashData(p);
13393** // do something with pData
13394** }
13395*/
13396#define sqliteHashFirst(H) ((H)->first)
13397#define sqliteHashNext(E) ((E)->next)
13398#define sqliteHashData(E) ((E)->data)
13399/* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */
13400/* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */
13401
13402/*
13403** Number of entries in a hash table
13404*/
13405/* #define sqliteHashCount(H) ((H)->count) // NOT USED */
13406
13407#endif /* SQLITE_HASH_H */
13408
13409/************** End of hash.h ************************************************/
13410/************** Continuing where we left off in sqliteInt.h ******************/
13411/************** Include parse.h in the middle of sqliteInt.h *****************/
13412/************** Begin file parse.h *******************************************/
13413#define TK_SEMI 1
13414#define TK_EXPLAIN 2
13415#define TK_QUERY 3
13416#define TK_PLAN 4
13417#define TK_BEGIN 5
13418#define TK_TRANSACTION 6
13419#define TK_DEFERRED 7
13420#define TK_IMMEDIATE 8
13421#define TK_EXCLUSIVE 9
13422#define TK_COMMIT 10
13423#define TK_END 11
13424#define TK_ROLLBACK 12
13425#define TK_SAVEPOINT 13
13426#define TK_RELEASE 14
13427#define TK_TO 15
13428#define TK_TABLE 16
13429#define TK_CREATE 17
13430#define TK_IF 18
13431#define TK_NOT 19
13432#define TK_EXISTS 20
13433#define TK_TEMP 21
13434#define TK_LP 22
13435#define TK_RP 23
13436#define TK_AS 24
13437#define TK_WITHOUT 25
13438#define TK_COMMA 26
13439#define TK_ABORT 27
13440#define TK_ACTION 28
13441#define TK_AFTER 29
13442#define TK_ANALYZE 30
13443#define TK_ASC 31
13444#define TK_ATTACH 32
13445#define TK_BEFORE 33
13446#define TK_BY 34
13447#define TK_CASCADE 35
13448#define TK_CAST 36
13449#define TK_CONFLICT 37
13450#define TK_DATABASE 38
13451#define TK_DESC 39
13452#define TK_DETACH 40
13453#define TK_EACH 41
13454#define TK_FAIL 42
13455#define TK_OR 43
13456#define TK_AND 44
13457#define TK_IS 45
13458#define TK_MATCH 46
13459#define TK_LIKE_KW 47
13460#define TK_BETWEEN 48
13461#define TK_IN 49
13462#define TK_ISNULL 50
13463#define TK_NOTNULL 51
13464#define TK_NE 52
13465#define TK_EQ 53
13466#define TK_GT 54
13467#define TK_LE 55
13468#define TK_LT 56
13469#define TK_GE 57
13470#define TK_ESCAPE 58
13471#define TK_ID 59
13472#define TK_COLUMNKW 60
13473#define TK_DO 61
13474#define TK_FOR 62
13475#define TK_IGNORE 63
13476#define TK_INITIALLY 64
13477#define TK_INSTEAD 65
13478#define TK_NO 66
13479#define TK_KEY 67
13480#define TK_OF 68
13481#define TK_OFFSET 69
13482#define TK_PRAGMA 70
13483#define TK_RAISE 71
13484#define TK_RECURSIVE 72
13485#define TK_REPLACE 73
13486#define TK_RESTRICT 74
13487#define TK_ROW 75
13488#define TK_ROWS 76
13489#define TK_TRIGGER 77
13490#define TK_VACUUM 78
13491#define TK_VIEW 79
13492#define TK_VIRTUAL 80
13493#define TK_WITH 81
13494#define TK_CURRENT 82
13495#define TK_FOLLOWING 83
13496#define TK_PARTITION 84
13497#define TK_PRECEDING 85
13498#define TK_RANGE 86
13499#define TK_UNBOUNDED 87
13500#define TK_REINDEX 88
13501#define TK_RENAME 89
13502#define TK_CTIME_KW 90
13503#define TK_ANY 91
13504#define TK_BITAND 92
13505#define TK_BITOR 93
13506#define TK_LSHIFT 94
13507#define TK_RSHIFT 95
13508#define TK_PLUS 96
13509#define TK_MINUS 97
13510#define TK_STAR 98
13511#define TK_SLASH 99
13512#define TK_REM 100
13513#define TK_CONCAT 101
13514#define TK_COLLATE 102
13515#define TK_BITNOT 103
13516#define TK_ON 104
13517#define TK_INDEXED 105
13518#define TK_STRING 106
13519#define TK_JOIN_KW 107
13520#define TK_CONSTRAINT 108
13521#define TK_DEFAULT 109
13522#define TK_NULL 110
13523#define TK_PRIMARY 111
13524#define TK_UNIQUE 112
13525#define TK_CHECK 113
13526#define TK_REFERENCES 114
13527#define TK_AUTOINCR 115
13528#define TK_INSERT 116
13529#define TK_DELETE 117
13530#define TK_UPDATE 118
13531#define TK_SET 119
13532#define TK_DEFERRABLE 120
13533#define TK_FOREIGN 121
13534#define TK_DROP 122
13535#define TK_UNION 123
13536#define TK_ALL 124
13537#define TK_EXCEPT 125
13538#define TK_INTERSECT 126
13539#define TK_SELECT 127
13540#define TK_VALUES 128
13541#define TK_DISTINCT 129
13542#define TK_DOT 130
13543#define TK_FROM 131
13544#define TK_JOIN 132
13545#define TK_USING 133
13546#define TK_ORDER 134
13547#define TK_GROUP 135
13548#define TK_HAVING 136
13549#define TK_LIMIT 137
13550#define TK_WHERE 138
13551#define TK_INTO 139
13552#define TK_NOTHING 140
13553#define TK_FLOAT 141
13554#define TK_BLOB 142
13555#define TK_INTEGER 143
13556#define TK_VARIABLE 144
13557#define TK_CASE 145
13558#define TK_WHEN 146
13559#define TK_THEN 147
13560#define TK_ELSE 148
13561#define TK_INDEX 149
13562#define TK_ALTER 150
13563#define TK_ADD 151
13564#define TK_WINDOW 152
13565#define TK_OVER 153
13566#define TK_FILTER 154
13567#define TK_TRUEFALSE 155
13568#define TK_ISNOT 156
13569#define TK_FUNCTION 157
13570#define TK_COLUMN 158
13571#define TK_AGG_FUNCTION 159
13572#define TK_AGG_COLUMN 160
13573#define TK_UMINUS 161
13574#define TK_UPLUS 162
13575#define TK_TRUTH 163
13576#define TK_REGISTER 164
13577#define TK_VECTOR 165
13578#define TK_SELECT_COLUMN 166
13579#define TK_IF_NULL_ROW 167
13580#define TK_ASTERISK 168
13581#define TK_SPAN 169
13582#define TK_END_OF_FILE 170
13583#define TK_UNCLOSED_STRING 171
13584#define TK_SPACE 172
13585#define TK_ILLEGAL 173
13586
13587/* The token codes above must all fit in 8 bits */
13588#define TKFLG_MASK 0xff
13589
13590/* Flags that can be added to a token code when it is not
13591** being stored in a u8: */
13592#define TKFLG_DONTFOLD 0x100 /* Omit constant folding optimizations */
13593
13594/************** End of parse.h ***********************************************/
13595/************** Continuing where we left off in sqliteInt.h ******************/
13596#include <stdio.h>
13597#include <stdlib.h>
13598#include <string.h>
13599#include <assert.h>
13600#include <stddef.h>
13601
13602/*
13603** Use a macro to replace memcpy() if compiled with SQLITE_INLINE_MEMCPY.
13604** This allows better measurements of where memcpy() is used when running
13605** cachegrind. But this macro version of memcpy() is very slow so it
13606** should not be used in production. This is a performance measurement
13607** hack only.
13608*/
13609#ifdef SQLITE_INLINE_MEMCPY
13610# define memcpy(D,S,N) {char*xxd=(char*)(D);const char*xxs=(const char*)(S);\
13611 int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
13612#endif
13613
13614/*
13615** If compiling for a processor that lacks floating point support,
13616** substitute integer for floating-point
13617*/
13618#ifdef SQLITE_OMIT_FLOATING_POINT
13619# define double sqlite_int64
13620# define float sqlite_int64
13621# define LONGDOUBLE_TYPE sqlite_int64
13622# ifndef SQLITE_BIG_DBL
13623# define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50)
13624# endif
13625# define SQLITE_OMIT_DATETIME_FUNCS 1
13626# define SQLITE_OMIT_TRACE 1
13627# undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
13628# undef SQLITE_HAVE_ISNAN
13629#endif
13630#ifndef SQLITE_BIG_DBL
13631# define SQLITE_BIG_DBL (1e99)
13632#endif
13633
13634/*
13635** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
13636** afterward. Having this macro allows us to cause the C compiler
13637** to omit code used by TEMP tables without messy #ifndef statements.
13638*/
13639#ifdef SQLITE_OMIT_TEMPDB
13640#define OMIT_TEMPDB 1
13641#else
13642#define OMIT_TEMPDB 0
13643#endif
13644
13645/*
13646** The "file format" number is an integer that is incremented whenever
13647** the VDBE-level file format changes. The following macros define the
13648** the default file format for new databases and the maximum file format
13649** that the library can read.
13650*/
13651#define SQLITE_MAX_FILE_FORMAT 4
13652#ifndef SQLITE_DEFAULT_FILE_FORMAT
13653# define SQLITE_DEFAULT_FILE_FORMAT 4
13654#endif
13655
13656/*
13657** Determine whether triggers are recursive by default. This can be
13658** changed at run-time using a pragma.
13659*/
13660#ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS
13661# define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0
13662#endif
13663
13664/*
13665** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
13666** on the command-line
13667*/
13668#ifndef SQLITE_TEMP_STORE
13669# define SQLITE_TEMP_STORE 1
13670#endif
13671
13672/*
13673** If no value has been provided for SQLITE_MAX_WORKER_THREADS, or if
13674** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it
13675** to zero.
13676*/
13677#if SQLITE_TEMP_STORE==3 || SQLITE_THREADSAFE==0
13678# undef SQLITE_MAX_WORKER_THREADS
13679# define SQLITE_MAX_WORKER_THREADS 0
13680#endif
13681#ifndef SQLITE_MAX_WORKER_THREADS
13682# define SQLITE_MAX_WORKER_THREADS 8
13683#endif
13684#ifndef SQLITE_DEFAULT_WORKER_THREADS
13685# define SQLITE_DEFAULT_WORKER_THREADS 0
13686#endif
13687#if SQLITE_DEFAULT_WORKER_THREADS>SQLITE_MAX_WORKER_THREADS
13688# undef SQLITE_MAX_WORKER_THREADS
13689# define SQLITE_MAX_WORKER_THREADS SQLITE_DEFAULT_WORKER_THREADS
13690#endif
13691
13692/*
13693** The default initial allocation for the pagecache when using separate
13694** pagecaches for each database connection. A positive number is the
13695** number of pages. A negative number N translations means that a buffer
13696** of -1024*N bytes is allocated and used for as many pages as it will hold.
13697**
13698** The default value of "20" was choosen to minimize the run-time of the
13699** speedtest1 test program with options: --shrink-memory --reprepare
13700*/
13701#ifndef SQLITE_DEFAULT_PCACHE_INITSZ
13702# define SQLITE_DEFAULT_PCACHE_INITSZ 20
13703#endif
13704
13705/*
13706** Default value for the SQLITE_CONFIG_SORTERREF_SIZE option.
13707*/
13708#ifndef SQLITE_DEFAULT_SORTERREF_SIZE
13709# define SQLITE_DEFAULT_SORTERREF_SIZE 0x7fffffff
13710#endif
13711
13712/*
13713** The compile-time options SQLITE_MMAP_READWRITE and
13714** SQLITE_ENABLE_BATCH_ATOMIC_WRITE are not compatible with one another.
13715** You must choose one or the other (or neither) but not both.
13716*/
13717#if defined(SQLITE_MMAP_READWRITE) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
13718#error Cannot use both SQLITE_MMAP_READWRITE and SQLITE_ENABLE_BATCH_ATOMIC_WRITE
13719#endif
13720
13721/*
13722** GCC does not define the offsetof() macro so we'll have to do it
13723** ourselves.
13724*/
13725#ifndef offsetof
13726#define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
13727#endif
13728
13729/*
13730** Macros to compute minimum and maximum of two numbers.
13731*/
13732#ifndef MIN
13733# define MIN(A,B) ((A)<(B)?(A):(B))
13734#endif
13735#ifndef MAX
13736# define MAX(A,B) ((A)>(B)?(A):(B))
13737#endif
13738
13739/*
13740** Swap two objects of type TYPE.
13741*/
13742#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
13743
13744/*
13745** Check to see if this machine uses EBCDIC. (Yes, believe it or
13746** not, there are still machines out there that use EBCDIC.)
13747*/
13748#if 'A' == '\301'
13749# define SQLITE_EBCDIC 1
13750#else
13751# define SQLITE_ASCII 1
13752#endif
13753
13754/*
13755** Integers of known sizes. These typedefs might change for architectures
13756** where the sizes very. Preprocessor macros are available so that the
13757** types can be conveniently redefined at compile-type. Like this:
13758**
13759** cc '-DUINTPTR_TYPE=long long int' ...
13760*/
13761#ifndef UINT32_TYPE
13762# ifdef HAVE_UINT32_T
13763# define UINT32_TYPE uint32_t
13764# else
13765# define UINT32_TYPE unsigned int
13766# endif
13767#endif
13768#ifndef UINT16_TYPE
13769# ifdef HAVE_UINT16_T
13770# define UINT16_TYPE uint16_t
13771# else
13772# define UINT16_TYPE unsigned short int
13773# endif
13774#endif
13775#ifndef INT16_TYPE
13776# ifdef HAVE_INT16_T
13777# define INT16_TYPE int16_t
13778# else
13779# define INT16_TYPE short int
13780# endif
13781#endif
13782#ifndef UINT8_TYPE
13783# ifdef HAVE_UINT8_T
13784# define UINT8_TYPE uint8_t
13785# else
13786# define UINT8_TYPE unsigned char
13787# endif
13788#endif
13789#ifndef INT8_TYPE
13790# ifdef HAVE_INT8_T
13791# define INT8_TYPE int8_t
13792# else
13793# define INT8_TYPE signed char
13794# endif
13795#endif
13796#ifndef LONGDOUBLE_TYPE
13797# define LONGDOUBLE_TYPE long double
13798#endif
13799typedef sqlite_int64 i64; /* 8-byte signed integer */
13800typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
13801typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
13802typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
13803typedef INT16_TYPE i16; /* 2-byte signed integer */
13804typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
13805typedef INT8_TYPE i8; /* 1-byte signed integer */
13806
13807/*
13808** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
13809** that can be stored in a u32 without loss of data. The value
13810** is 0x00000000ffffffff. But because of quirks of some compilers, we
13811** have to specify the value in the less intuitive manner shown:
13812*/
13813#define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
13814
13815/*
13816** The datatype used to store estimates of the number of rows in a
13817** table or index. This is an unsigned integer type. For 99.9% of
13818** the world, a 32-bit integer is sufficient. But a 64-bit integer
13819** can be used at compile-time if desired.
13820*/
13821#ifdef SQLITE_64BIT_STATS
13822 typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */
13823#else
13824 typedef u32 tRowcnt; /* 32-bit is the default */
13825#endif
13826
13827/*
13828** Estimated quantities used for query planning are stored as 16-bit
13829** logarithms. For quantity X, the value stored is 10*log2(X). This
13830** gives a possible range of values of approximately 1.0e986 to 1e-986.
13831** But the allowed values are "grainy". Not every value is representable.
13832** For example, quantities 16 and 17 are both represented by a LogEst
13833** of 40. However, since LogEst quantities are suppose to be estimates,
13834** not exact values, this imprecision is not a problem.
13835**
13836** "LogEst" is short for "Logarithmic Estimate".
13837**
13838** Examples:
13839** 1 -> 0 20 -> 43 10000 -> 132
13840** 2 -> 10 25 -> 46 25000 -> 146
13841** 3 -> 16 100 -> 66 1000000 -> 199
13842** 4 -> 20 1000 -> 99 1048576 -> 200
13843** 10 -> 33 1024 -> 100 4294967296 -> 320
13844**
13845** The LogEst can be negative to indicate fractional values.
13846** Examples:
13847**
13848** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
13849*/
13850typedef INT16_TYPE LogEst;
13851
13852/*
13853** Set the SQLITE_PTRSIZE macro to the number of bytes in a pointer
13854*/
13855#ifndef SQLITE_PTRSIZE
13856# if defined(__SIZEOF_POINTER__)
13857# define SQLITE_PTRSIZE __SIZEOF_POINTER__
13858# elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \
13859 defined(_M_ARM) || defined(__arm__) || defined(__x86) || \
13860 (defined(__TOS_AIX__) && !defined(__64BIT__))
13861# define SQLITE_PTRSIZE 4
13862# else
13863# define SQLITE_PTRSIZE 8
13864# endif
13865#endif
13866
13867/* The uptr type is an unsigned integer large enough to hold a pointer
13868*/
13869#if defined(HAVE_STDINT_H)
13870 typedef uintptr_t uptr;
13871#elif SQLITE_PTRSIZE==4
13872 typedef u32 uptr;
13873#else
13874 typedef u64 uptr;
13875#endif
13876
13877/*
13878** The SQLITE_WITHIN(P,S,E) macro checks to see if pointer P points to
13879** something between S (inclusive) and E (exclusive).
13880**
13881** In other words, S is a buffer and E is a pointer to the first byte after
13882** the end of buffer S. This macro returns true if P points to something
13883** contained within the buffer S.
13884*/
13885#define SQLITE_WITHIN(P,S,E) (((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E)))
13886
13887
13888/*
13889** Macros to determine whether the machine is big or little endian,
13890** and whether or not that determination is run-time or compile-time.
13891**
13892** For best performance, an attempt is made to guess at the byte-order
13893** using C-preprocessor macros. If that is unsuccessful, or if
13894** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
13895** at run-time.
13896*/
13897#ifndef SQLITE_BYTEORDER
13898# if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
13899 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
13900 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
13901 defined(__arm__) || defined(_M_ARM64)
13902# define SQLITE_BYTEORDER 1234
13903# elif defined(sparc) || defined(__ppc__)
13904# define SQLITE_BYTEORDER 4321
13905# else
13906# define SQLITE_BYTEORDER 0
13907# endif
13908#endif
13909#if SQLITE_BYTEORDER==4321
13910# define SQLITE_BIGENDIAN 1
13911# define SQLITE_LITTLEENDIAN 0
13912# define SQLITE_UTF16NATIVE SQLITE_UTF16BE
13913#elif SQLITE_BYTEORDER==1234
13914# define SQLITE_BIGENDIAN 0
13915# define SQLITE_LITTLEENDIAN 1
13916# define SQLITE_UTF16NATIVE SQLITE_UTF16LE
13917#else
13918# ifdef SQLITE_AMALGAMATION
13919 const int sqlite3one = 1;
13920# else
13921 extern const int sqlite3one;
13922# endif
13923# define SQLITE_BIGENDIAN (*(char *)(&sqlite3one)==0)
13924# define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
13925# define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
13926#endif
13927
13928/*
13929** Constants for the largest and smallest possible 64-bit signed integers.
13930** These macros are designed to work correctly on both 32-bit and 64-bit
13931** compilers.
13932*/
13933#define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
13934#define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
13935
13936/*
13937** Round up a number to the next larger multiple of 8. This is used
13938** to force 8-byte alignment on 64-bit architectures.
13939*/
13940#define ROUND8(x) (((x)+7)&~7)
13941
13942/*
13943** Round down to the nearest multiple of 8
13944*/
13945#define ROUNDDOWN8(x) ((x)&~7)
13946
13947/*
13948** Assert that the pointer X is aligned to an 8-byte boundary. This
13949** macro is used only within assert() to verify that the code gets
13950** all alignment restrictions correct.
13951**
13952** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
13953** underlying malloc() implementation might return us 4-byte aligned
13954** pointers. In that case, only verify 4-byte alignment.
13955*/
13956#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
13957# define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&3)==0)
13958#else
13959# define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&7)==0)
13960#endif
13961
13962/*
13963** Disable MMAP on platforms where it is known to not work
13964*/
13965#if defined(__OpenBSD__) || defined(__QNXNTO__)
13966# undef SQLITE_MAX_MMAP_SIZE
13967# define SQLITE_MAX_MMAP_SIZE 0
13968#endif
13969
13970/*
13971** Default maximum size of memory used by memory-mapped I/O in the VFS
13972*/
13973#ifdef __APPLE__
13974# include <TargetConditionals.h>
13975#endif
13976#ifndef SQLITE_MAX_MMAP_SIZE
13977# if defined(__linux__) \
13978 || defined(_WIN32) \
13979 || (defined(__APPLE__) && defined(__MACH__)) \
13980 || defined(__sun) \
13981 || defined(__FreeBSD__) \
13982 || defined(__DragonFly__)
13983# define SQLITE_MAX_MMAP_SIZE 0x7fff0000 /* 2147418112 */
13984# else
13985# define SQLITE_MAX_MMAP_SIZE 0
13986# endif
13987#endif
13988
13989/*
13990** The default MMAP_SIZE is zero on all platforms. Or, even if a larger
13991** default MMAP_SIZE is specified at compile-time, make sure that it does
13992** not exceed the maximum mmap size.
13993*/
13994#ifndef SQLITE_DEFAULT_MMAP_SIZE
13995# define SQLITE_DEFAULT_MMAP_SIZE 0
13996#endif
13997#if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE
13998# undef SQLITE_DEFAULT_MMAP_SIZE
13999# define SQLITE_DEFAULT_MMAP_SIZE SQLITE_MAX_MMAP_SIZE
14000#endif
14001
14002/*
14003** Only one of SQLITE_ENABLE_STAT3 or SQLITE_ENABLE_STAT4 can be defined.
14004** Priority is given to SQLITE_ENABLE_STAT4. If either are defined, also
14005** define SQLITE_ENABLE_STAT3_OR_STAT4
14006*/
14007#ifdef SQLITE_ENABLE_STAT4
14008# undef SQLITE_ENABLE_STAT3
14009# define SQLITE_ENABLE_STAT3_OR_STAT4 1
14010#elif SQLITE_ENABLE_STAT3
14011# define SQLITE_ENABLE_STAT3_OR_STAT4 1
14012#elif SQLITE_ENABLE_STAT3_OR_STAT4
14013# undef SQLITE_ENABLE_STAT3_OR_STAT4
14014#endif
14015
14016/*
14017** SELECTTRACE_ENABLED will be either 1 or 0 depending on whether or not
14018** the Select query generator tracing logic is turned on.
14019*/
14020#if defined(SQLITE_ENABLE_SELECTTRACE)
14021# define SELECTTRACE_ENABLED 1
14022#else
14023# define SELECTTRACE_ENABLED 0
14024#endif
14025
14026/*
14027** An instance of the following structure is used to store the busy-handler
14028** callback for a given sqlite handle.
14029**
14030** The sqlite.busyHandler member of the sqlite struct contains the busy
14031** callback for the database handle. Each pager opened via the sqlite
14032** handle is passed a pointer to sqlite.busyHandler. The busy-handler
14033** callback is currently invoked only from within pager.c.
14034*/
14035typedef struct BusyHandler BusyHandler;
14036struct BusyHandler {
14037 int (*xBusyHandler)(void *,int); /* The busy callback */
14038 void *pBusyArg; /* First arg to busy callback */
14039 int nBusy; /* Incremented with each busy call */
14040 u8 bExtraFileArg; /* Include sqlite3_file as callback arg */
14041};
14042
14043/*
14044** Name of the master database table. The master database table
14045** is a special table that holds the names and attributes of all
14046** user tables and indices.
14047*/
14048#define MASTER_NAME "sqlite_master"
14049#define TEMP_MASTER_NAME "sqlite_temp_master"
14050
14051/*
14052** The root-page of the master database table.
14053*/
14054#define MASTER_ROOT 1
14055
14056/*
14057** The name of the schema table.
14058*/
14059#define SCHEMA_TABLE(x) ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME)
14060
14061/*
14062** A convenience macro that returns the number of elements in
14063** an array.
14064*/
14065#define ArraySize(X) ((int)(sizeof(X)/sizeof(X[0])))
14066
14067/*
14068** Determine if the argument is a power of two
14069*/
14070#define IsPowerOfTwo(X) (((X)&((X)-1))==0)
14071
14072/*
14073** The following value as a destructor means to use sqlite3DbFree().
14074** The sqlite3DbFree() routine requires two parameters instead of the
14075** one parameter that destructors normally want. So we have to introduce
14076** this magic value that the code knows to handle differently. Any
14077** pointer will work here as long as it is distinct from SQLITE_STATIC
14078** and SQLITE_TRANSIENT.
14079*/
14080#define SQLITE_DYNAMIC ((sqlite3_destructor_type)sqlite3MallocSize)
14081
14082/*
14083** When SQLITE_OMIT_WSD is defined, it means that the target platform does
14084** not support Writable Static Data (WSD) such as global and static variables.
14085** All variables must either be on the stack or dynamically allocated from
14086** the heap. When WSD is unsupported, the variable declarations scattered
14087** throughout the SQLite code must become constants instead. The SQLITE_WSD
14088** macro is used for this purpose. And instead of referencing the variable
14089** directly, we use its constant as a key to lookup the run-time allocated
14090** buffer that holds real variable. The constant is also the initializer
14091** for the run-time allocated buffer.
14092**
14093** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
14094** macros become no-ops and have zero performance impact.
14095*/
14096#ifdef SQLITE_OMIT_WSD
14097 #define SQLITE_WSD const
14098 #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
14099 #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config)
14100SQLITE_API int sqlite3_wsd_init(int N, int J);
14101SQLITE_API void *sqlite3_wsd_find(void *K, int L);
14102#else
14103 #define SQLITE_WSD
14104 #define GLOBAL(t,v) v
14105 #define sqlite3GlobalConfig sqlite3Config
14106#endif
14107
14108/*
14109** The following macros are used to suppress compiler warnings and to
14110** make it clear to human readers when a function parameter is deliberately
14111** left unused within the body of a function. This usually happens when
14112** a function is called via a function pointer. For example the
14113** implementation of an SQL aggregate step callback may not use the
14114** parameter indicating the number of arguments passed to the aggregate,
14115** if it knows that this is enforced elsewhere.
14116**
14117** When a function parameter is not used at all within the body of a function,
14118** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
14119** However, these macros may also be used to suppress warnings related to
14120** parameters that may or may not be used depending on compilation options.
14121** For example those parameters only used in assert() statements. In these
14122** cases the parameters are named as per the usual conventions.
14123*/
14124#define UNUSED_PARAMETER(x) (void)(x)
14125#define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y)
14126
14127/*
14128** Forward references to structures
14129*/
14130typedef struct AggInfo AggInfo;
14131typedef struct AuthContext AuthContext;
14132typedef struct AutoincInfo AutoincInfo;
14133typedef struct Bitvec Bitvec;
14134typedef struct CollSeq CollSeq;
14135typedef struct Column Column;
14136typedef struct Db Db;
14137typedef struct Schema Schema;
14138typedef struct Expr Expr;
14139typedef struct ExprList ExprList;
14140typedef struct FKey FKey;
14141typedef struct FuncDestructor FuncDestructor;
14142typedef struct FuncDef FuncDef;
14143typedef struct FuncDefHash FuncDefHash;
14144typedef struct IdList IdList;
14145typedef struct Index Index;
14146typedef struct IndexSample IndexSample;
14147typedef struct KeyClass KeyClass;
14148typedef struct KeyInfo KeyInfo;
14149typedef struct Lookaside Lookaside;
14150typedef struct LookasideSlot LookasideSlot;
14151typedef struct Module Module;
14152typedef struct NameContext NameContext;
14153typedef struct Parse Parse;
14154typedef struct PreUpdate PreUpdate;
14155typedef struct PrintfArguments PrintfArguments;
14156typedef struct RenameToken RenameToken;
14157typedef struct RowSet RowSet;
14158typedef struct Savepoint Savepoint;
14159typedef struct Select Select;
14160typedef struct SQLiteThread SQLiteThread;
14161typedef struct SelectDest SelectDest;
14162typedef struct SrcList SrcList;
14163typedef struct sqlite3_str StrAccum; /* Internal alias for sqlite3_str */
14164typedef struct Table Table;
14165typedef struct TableLock TableLock;
14166typedef struct Token Token;
14167typedef struct TreeView TreeView;
14168typedef struct Trigger Trigger;
14169typedef struct TriggerPrg TriggerPrg;
14170typedef struct TriggerStep TriggerStep;
14171typedef struct UnpackedRecord UnpackedRecord;
14172typedef struct Upsert Upsert;
14173typedef struct VTable VTable;
14174typedef struct VtabCtx VtabCtx;
14175typedef struct Walker Walker;
14176typedef struct WhereInfo WhereInfo;
14177typedef struct Window Window;
14178typedef struct With With;
14179
14180
14181/*
14182** The bitmask datatype defined below is used for various optimizations.
14183**
14184** Changing this from a 64-bit to a 32-bit type limits the number of
14185** tables in a join to 32 instead of 64. But it also reduces the size
14186** of the library by 738 bytes on ix86.
14187*/
14188#ifdef SQLITE_BITMASK_TYPE
14189 typedef SQLITE_BITMASK_TYPE Bitmask;
14190#else
14191 typedef u64 Bitmask;
14192#endif
14193
14194/*
14195** The number of bits in a Bitmask. "BMS" means "BitMask Size".
14196*/
14197#define BMS ((int)(sizeof(Bitmask)*8))
14198
14199/*
14200** A bit in a Bitmask
14201*/
14202#define MASKBIT(n) (((Bitmask)1)<<(n))
14203#define MASKBIT32(n) (((unsigned int)1)<<(n))
14204#define ALLBITS ((Bitmask)-1)
14205
14206/* A VList object records a mapping between parameters/variables/wildcards
14207** in the SQL statement (such as $abc, @pqr, or :xyz) and the integer
14208** variable number associated with that parameter. See the format description
14209** on the sqlite3VListAdd() routine for more information. A VList is really
14210** just an array of integers.
14211*/
14212typedef int VList;
14213
14214/*
14215** Defer sourcing vdbe.h and btree.h until after the "u8" and
14216** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
14217** pointer types (i.e. FuncDef) defined above.
14218*/
14219/************** Include btree.h in the middle of sqliteInt.h *****************/
14220/************** Begin file btree.h *******************************************/
14221/*
14222** 2001 September 15
14223**
14224** The author disclaims copyright to this source code. In place of
14225** a legal notice, here is a blessing:
14226**
14227** May you do good and not evil.
14228** May you find forgiveness for yourself and forgive others.
14229** May you share freely, never taking more than you give.
14230**
14231*************************************************************************
14232** This header file defines the interface that the sqlite B-Tree file
14233** subsystem. See comments in the source code for a detailed description
14234** of what each interface routine does.
14235*/
14236#ifndef SQLITE_BTREE_H
14237#define SQLITE_BTREE_H
14238
14239/* TODO: This definition is just included so other modules compile. It
14240** needs to be revisited.
14241*/
14242#define SQLITE_N_BTREE_META 16
14243
14244/*
14245** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
14246** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
14247*/
14248#ifndef SQLITE_DEFAULT_AUTOVACUUM
14249 #define SQLITE_DEFAULT_AUTOVACUUM 0
14250#endif
14251
14252#define BTREE_AUTOVACUUM_NONE 0 /* Do not do auto-vacuum */
14253#define BTREE_AUTOVACUUM_FULL 1 /* Do full auto-vacuum */
14254#define BTREE_AUTOVACUUM_INCR 2 /* Incremental vacuum */
14255
14256/*
14257** Forward declarations of structure
14258*/
14259typedef struct Btree Btree;
14260typedef struct BtCursor BtCursor;
14261typedef struct BtShared BtShared;
14262typedef struct BtreePayload BtreePayload;
14263
14264
14265SQLITE_PRIVATE int sqlite3BtreeOpen(
14266 sqlite3_vfs *pVfs, /* VFS to use with this b-tree */
14267 const char *zFilename, /* Name of database file to open */
14268 sqlite3 *db, /* Associated database connection */
14269 Btree **ppBtree, /* Return open Btree* here */
14270 int flags, /* Flags */
14271 int vfsFlags /* Flags passed through to VFS open */
14272);
14273
14274/* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
14275** following values.
14276**
14277** NOTE: These values must match the corresponding PAGER_ values in
14278** pager.h.
14279*/
14280#define BTREE_OMIT_JOURNAL 1 /* Do not create or use a rollback journal */
14281#define BTREE_MEMORY 2 /* This is an in-memory DB */
14282#define BTREE_SINGLE 4 /* The file contains at most 1 b-tree */
14283#define BTREE_UNORDERED 8 /* Use of a hash implementation is OK */
14284
14285SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
14286SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
14287SQLITE_PRIVATE int sqlite3BtreeSetSpillSize(Btree*,int);
14288#if SQLITE_MAX_MMAP_SIZE>0
14289SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
14290#endif
14291SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
14292SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
14293SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
14294SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
14295SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*);
14296SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int);
14297SQLITE_PRIVATE int sqlite3BtreeGetOptimalReserve(Btree*);
14298SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p);
14299SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
14300SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
14301SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int,int*);
14302SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
14303SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
14304SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
14305SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*,int,int);
14306SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
14307SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
14308SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
14309SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
14310SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
14311SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
14312SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree);
14313#ifndef SQLITE_OMIT_SHARED_CACHE
14314SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
14315#endif
14316SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int);
14317
14318SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *);
14319SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *);
14320SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *);
14321
14322SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *);
14323
14324/* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
14325** of the flags shown below.
14326**
14327** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
14328** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
14329** is stored in the leaves. (BTREE_INTKEY is used for SQL tables.) With
14330** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
14331** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL
14332** indices.)
14333*/
14334#define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */
14335#define BTREE_BLOBKEY 2 /* Table has keys only - no data */
14336
14337SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
14338SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*);
14339SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor*);
14340SQLITE_PRIVATE int sqlite3BtreeTripAllCursors(Btree*, int, int);
14341
14342SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
14343SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
14344
14345SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p);
14346
14347/*
14348** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
14349** should be one of the following values. The integer values are assigned
14350** to constants so that the offset of the corresponding field in an
14351** SQLite database header may be found using the following formula:
14352**
14353** offset = 36 + (idx * 4)
14354**
14355** For example, the free-page-count field is located at byte offset 36 of
14356** the database file header. The incr-vacuum-flag field is located at
14357** byte offset 64 (== 36+4*7).
14358**
14359** The BTREE_DATA_VERSION value is not really a value stored in the header.
14360** It is a read-only number computed by the pager. But we merge it with
14361** the header value access routines since its access pattern is the same.
14362** Call it a "virtual meta value".
14363*/
14364#define BTREE_FREE_PAGE_COUNT 0
14365#define BTREE_SCHEMA_VERSION 1
14366#define BTREE_FILE_FORMAT 2
14367#define BTREE_DEFAULT_CACHE_SIZE 3
14368#define BTREE_LARGEST_ROOT_PAGE 4
14369#define BTREE_TEXT_ENCODING 5
14370#define BTREE_USER_VERSION 6
14371#define BTREE_INCR_VACUUM 7
14372#define BTREE_APPLICATION_ID 8
14373#define BTREE_DATA_VERSION 15 /* A virtual meta-value */
14374
14375/*
14376** Kinds of hints that can be passed into the sqlite3BtreeCursorHint()
14377** interface.
14378**
14379** BTREE_HINT_RANGE (arguments: Expr*, Mem*)
14380**
14381** The first argument is an Expr* (which is guaranteed to be constant for
14382** the lifetime of the cursor) that defines constraints on which rows
14383** might be fetched with this cursor. The Expr* tree may contain
14384** TK_REGISTER nodes that refer to values stored in the array of registers
14385** passed as the second parameter. In other words, if Expr.op==TK_REGISTER
14386** then the value of the node is the value in Mem[pExpr.iTable]. Any
14387** TK_COLUMN node in the expression tree refers to the Expr.iColumn-th
14388** column of the b-tree of the cursor. The Expr tree will not contain
14389** any function calls nor subqueries nor references to b-trees other than
14390** the cursor being hinted.
14391**
14392** The design of the _RANGE hint is aid b-tree implementations that try
14393** to prefetch content from remote machines - to provide those
14394** implementations with limits on what needs to be prefetched and thereby
14395** reduce network bandwidth.
14396**
14397** Note that BTREE_HINT_FLAGS with BTREE_BULKLOAD is the only hint used by
14398** standard SQLite. The other hints are provided for extentions that use
14399** the SQLite parser and code generator but substitute their own storage
14400** engine.
14401*/
14402#define BTREE_HINT_RANGE 0 /* Range constraints on queries */
14403
14404/*
14405** Values that may be OR'd together to form the argument to the
14406** BTREE_HINT_FLAGS hint for sqlite3BtreeCursorHint():
14407**
14408** The BTREE_BULKLOAD flag is set on index cursors when the index is going
14409** to be filled with content that is already in sorted order.
14410**
14411** The BTREE_SEEK_EQ flag is set on cursors that will get OP_SeekGE or
14412** OP_SeekLE opcodes for a range search, but where the range of entries
14413** selected will all have the same key. In other words, the cursor will
14414** be used only for equality key searches.
14415**
14416*/
14417#define BTREE_BULKLOAD 0x00000001 /* Used to full index in sorted order */
14418#define BTREE_SEEK_EQ 0x00000002 /* EQ seeks only - no range seeks */
14419
14420/*
14421** Flags passed as the third argument to sqlite3BtreeCursor().
14422**
14423** For read-only cursors the wrFlag argument is always zero. For read-write
14424** cursors it may be set to either (BTREE_WRCSR|BTREE_FORDELETE) or just
14425** (BTREE_WRCSR). If the BTREE_FORDELETE bit is set, then the cursor will
14426** only be used by SQLite for the following:
14427**
14428** * to seek to and then delete specific entries, and/or
14429**
14430** * to read values that will be used to create keys that other
14431** BTREE_FORDELETE cursors will seek to and delete.
14432**
14433** The BTREE_FORDELETE flag is an optimization hint. It is not used by
14434** by this, the native b-tree engine of SQLite, but it is available to
14435** alternative storage engines that might be substituted in place of this
14436** b-tree system. For alternative storage engines in which a delete of
14437** the main table row automatically deletes corresponding index rows,
14438** the FORDELETE flag hint allows those alternative storage engines to
14439** skip a lot of work. Namely: FORDELETE cursors may treat all SEEK
14440** and DELETE operations as no-ops, and any READ operation against a
14441** FORDELETE cursor may return a null row: 0x01 0x00.
14442*/
14443#define BTREE_WRCSR 0x00000004 /* read-write cursor */
14444#define BTREE_FORDELETE 0x00000008 /* Cursor is for seek/delete only */
14445
14446SQLITE_PRIVATE int sqlite3BtreeCursor(
14447 Btree*, /* BTree containing table to open */
14448 int iTable, /* Index of root page */
14449 int wrFlag, /* 1 for writing. 0 for read-only */
14450 struct KeyInfo*, /* First argument to compare function */
14451 BtCursor *pCursor /* Space to write cursor structure */
14452);
14453SQLITE_PRIVATE BtCursor *sqlite3BtreeFakeValidCursor(void);
14454SQLITE_PRIVATE int sqlite3BtreeCursorSize(void);
14455SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor*);
14456SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor*, unsigned);
14457#ifdef SQLITE_ENABLE_CURSOR_HINTS
14458SQLITE_PRIVATE void sqlite3BtreeCursorHint(BtCursor*, int, ...);
14459#endif
14460
14461SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*);
14462SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
14463 BtCursor*,
14464 UnpackedRecord *pUnKey,
14465 i64 intKey,
14466 int bias,
14467 int *pRes
14468);
14469SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*);
14470SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor*, int*);
14471SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*, u8 flags);
14472
14473/* Allowed flags for sqlite3BtreeDelete() and sqlite3BtreeInsert() */
14474#define BTREE_SAVEPOSITION 0x02 /* Leave cursor pointing at NEXT or PREV */
14475#define BTREE_AUXDELETE 0x04 /* not the primary delete operation */
14476#define BTREE_APPEND 0x08 /* Insert is likely an append */
14477
14478/* An instance of the BtreePayload object describes the content of a single
14479** entry in either an index or table btree.
14480**
14481** Index btrees (used for indexes and also WITHOUT ROWID tables) contain
14482** an arbitrary key and no data. These btrees have pKey,nKey set to the
14483** key and the pData,nData,nZero fields are uninitialized. The aMem,nMem
14484** fields give an array of Mem objects that are a decomposition of the key.
14485** The nMem field might be zero, indicating that no decomposition is available.
14486**
14487** Table btrees (used for rowid tables) contain an integer rowid used as
14488** the key and passed in the nKey field. The pKey field is zero.
14489** pData,nData hold the content of the new entry. nZero extra zero bytes
14490** are appended to the end of the content when constructing the entry.
14491** The aMem,nMem fields are uninitialized for table btrees.
14492**
14493** Field usage summary:
14494**
14495** Table BTrees Index Btrees
14496**
14497** pKey always NULL encoded key
14498** nKey the ROWID length of pKey
14499** pData data not used
14500** aMem not used decomposed key value
14501** nMem not used entries in aMem
14502** nData length of pData not used
14503** nZero extra zeros after pData not used
14504**
14505** This object is used to pass information into sqlite3BtreeInsert(). The
14506** same information used to be passed as five separate parameters. But placing
14507** the information into this object helps to keep the interface more
14508** organized and understandable, and it also helps the resulting code to
14509** run a little faster by using fewer registers for parameter passing.
14510*/
14511struct BtreePayload {
14512 const void *pKey; /* Key content for indexes. NULL for tables */
14513 sqlite3_int64 nKey; /* Size of pKey for indexes. PRIMARY KEY for tabs */
14514 const void *pData; /* Data for tables. */
14515 sqlite3_value *aMem; /* First of nMem value in the unpacked pKey */
14516 u16 nMem; /* Number of aMem[] value. Might be zero */
14517 int nData; /* Size of pData. 0 if none. */
14518 int nZero; /* Extra zero data appended after pData,nData */
14519};
14520
14521SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
14522 int flags, int seekResult);
14523SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
14524#ifndef SQLITE_OMIT_WINDOWFUNC
14525SQLITE_PRIVATE void sqlite3BtreeSkipNext(BtCursor*);
14526#endif
14527SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
14528SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int flags);
14529SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
14530SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int flags);
14531SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor*);
14532#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
14533SQLITE_PRIVATE i64 sqlite3BtreeOffset(BtCursor*);
14534#endif
14535SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor*, u32 offset, u32 amt, void*);
14536SQLITE_PRIVATE const void *sqlite3BtreePayloadFetch(BtCursor*, u32 *pAmt);
14537SQLITE_PRIVATE u32 sqlite3BtreePayloadSize(BtCursor*);
14538
14539SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
14540SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
14541SQLITE_PRIVATE i64 sqlite3BtreeRowCountEst(BtCursor*);
14542
14543#ifndef SQLITE_OMIT_INCRBLOB
14544SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor*, u32 offset, u32 amt, void*);
14545SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
14546SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *);
14547#endif
14548SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
14549SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
14550SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask);
14551SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt);
14552SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void);
14553
14554#ifndef NDEBUG
14555SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
14556#endif
14557SQLITE_PRIVATE int sqlite3BtreeCursorIsValidNN(BtCursor*);
14558
14559#ifndef SQLITE_OMIT_BTREECOUNT
14560SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *, i64 *);
14561#endif
14562
14563#ifdef SQLITE_TEST
14564SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
14565SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
14566#endif
14567
14568#ifndef SQLITE_OMIT_WAL
14569SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
14570#endif
14571
14572/*
14573** If we are not using shared cache, then there is no need to
14574** use mutexes to access the BtShared structures. So make the
14575** Enter and Leave procedures no-ops.
14576*/
14577#ifndef SQLITE_OMIT_SHARED_CACHE
14578SQLITE_PRIVATE void sqlite3BtreeEnter(Btree*);
14579SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3*);
14580SQLITE_PRIVATE int sqlite3BtreeSharable(Btree*);
14581SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor*);
14582SQLITE_PRIVATE int sqlite3BtreeConnectionCount(Btree*);
14583#else
14584# define sqlite3BtreeEnter(X)
14585# define sqlite3BtreeEnterAll(X)
14586# define sqlite3BtreeSharable(X) 0
14587# define sqlite3BtreeEnterCursor(X)
14588# define sqlite3BtreeConnectionCount(X) 1
14589#endif
14590
14591#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
14592SQLITE_PRIVATE void sqlite3BtreeLeave(Btree*);
14593SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor*);
14594SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3*);
14595#ifndef NDEBUG
14596 /* These routines are used inside assert() statements only. */
14597SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree*);
14598SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3*);
14599SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
14600#endif
14601#else
14602
14603# define sqlite3BtreeLeave(X)
14604# define sqlite3BtreeLeaveCursor(X)
14605# define sqlite3BtreeLeaveAll(X)
14606
14607# define sqlite3BtreeHoldsMutex(X) 1
14608# define sqlite3BtreeHoldsAllMutexes(X) 1
14609# define sqlite3SchemaMutexHeld(X,Y,Z) 1
14610#endif
14611
14612
14613#endif /* SQLITE_BTREE_H */
14614
14615/************** End of btree.h ***********************************************/
14616/************** Continuing where we left off in sqliteInt.h ******************/
14617/************** Include vdbe.h in the middle of sqliteInt.h ******************/
14618/************** Begin file vdbe.h ********************************************/
14619/*
14620** 2001 September 15
14621**
14622** The author disclaims copyright to this source code. In place of
14623** a legal notice, here is a blessing:
14624**
14625** May you do good and not evil.
14626** May you find forgiveness for yourself and forgive others.
14627** May you share freely, never taking more than you give.
14628**
14629*************************************************************************
14630** Header file for the Virtual DataBase Engine (VDBE)
14631**
14632** This header defines the interface to the virtual database engine
14633** or VDBE. The VDBE implements an abstract machine that runs a
14634** simple program to access and modify the underlying database.
14635*/
14636#ifndef SQLITE_VDBE_H
14637#define SQLITE_VDBE_H
14638/* #include <stdio.h> */
14639
14640/*
14641** A single VDBE is an opaque structure named "Vdbe". Only routines
14642** in the source file sqliteVdbe.c are allowed to see the insides
14643** of this structure.
14644*/
14645typedef struct Vdbe Vdbe;
14646
14647/*
14648** The names of the following types declared in vdbeInt.h are required
14649** for the VdbeOp definition.
14650*/
14651typedef struct sqlite3_value Mem;
14652typedef struct SubProgram SubProgram;
14653
14654/*
14655** A single instruction of the virtual machine has an opcode
14656** and as many as three operands. The instruction is recorded
14657** as an instance of the following structure:
14658*/
14659struct VdbeOp {
14660 u8 opcode; /* What operation to perform */
14661 signed char p4type; /* One of the P4_xxx constants for p4 */
14662 u16 p5; /* Fifth parameter is an unsigned 16-bit integer */
14663 int p1; /* First operand */
14664 int p2; /* Second parameter (often the jump destination) */
14665 int p3; /* The third parameter */
14666 union p4union { /* fourth parameter */
14667 int i; /* Integer value if p4type==P4_INT32 */
14668 void *p; /* Generic pointer */
14669 char *z; /* Pointer to data for string (char array) types */
14670 i64 *pI64; /* Used when p4type is P4_INT64 */
14671 double *pReal; /* Used when p4type is P4_REAL */
14672 FuncDef *pFunc; /* Used when p4type is P4_FUNCDEF */
14673 sqlite3_context *pCtx; /* Used when p4type is P4_FUNCCTX */
14674 CollSeq *pColl; /* Used when p4type is P4_COLLSEQ */
14675 Mem *pMem; /* Used when p4type is P4_MEM */
14676 VTable *pVtab; /* Used when p4type is P4_VTAB */
14677 KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
14678 int *ai; /* Used when p4type is P4_INTARRAY */
14679 SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */
14680 Table *pTab; /* Used when p4type is P4_TABLE */
14681#ifdef SQLITE_ENABLE_CURSOR_HINTS
14682 Expr *pExpr; /* Used when p4type is P4_EXPR */
14683#endif
14684 int (*xAdvance)(BtCursor *, int);
14685 } p4;
14686#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
14687 char *zComment; /* Comment to improve readability */
14688#endif
14689#ifdef VDBE_PROFILE
14690 u32 cnt; /* Number of times this instruction was executed */
14691 u64 cycles; /* Total time spent executing this instruction */
14692#endif
14693#ifdef SQLITE_VDBE_COVERAGE
14694 u32 iSrcLine; /* Source-code line that generated this opcode
14695 ** with flags in the upper 8 bits */
14696#endif
14697};
14698typedef struct VdbeOp VdbeOp;
14699
14700
14701/*
14702** A sub-routine used to implement a trigger program.
14703*/
14704struct SubProgram {
14705 VdbeOp *aOp; /* Array of opcodes for sub-program */
14706 int nOp; /* Elements in aOp[] */
14707 int nMem; /* Number of memory cells required */
14708 int nCsr; /* Number of cursors required */
14709 u8 *aOnce; /* Array of OP_Once flags */
14710 void *token; /* id that may be used to recursive triggers */
14711 SubProgram *pNext; /* Next sub-program already visited */
14712};
14713
14714/*
14715** A smaller version of VdbeOp used for the VdbeAddOpList() function because
14716** it takes up less space.
14717*/
14718struct VdbeOpList {
14719 u8 opcode; /* What operation to perform */
14720 signed char p1; /* First operand */
14721 signed char p2; /* Second parameter (often the jump destination) */
14722 signed char p3; /* Third parameter */
14723};
14724typedef struct VdbeOpList VdbeOpList;
14725
14726/*
14727** Allowed values of VdbeOp.p4type
14728*/
14729#define P4_NOTUSED 0 /* The P4 parameter is not used */
14730#define P4_TRANSIENT 0 /* P4 is a pointer to a transient string */
14731#define P4_STATIC (-1) /* Pointer to a static string */
14732#define P4_COLLSEQ (-2) /* P4 is a pointer to a CollSeq structure */
14733#define P4_INT32 (-3) /* P4 is a 32-bit signed integer */
14734#define P4_SUBPROGRAM (-4) /* P4 is a pointer to a SubProgram structure */
14735#define P4_ADVANCE (-5) /* P4 is a pointer to BtreeNext() or BtreePrev() */
14736#define P4_TABLE (-6) /* P4 is a pointer to a Table structure */
14737/* Above do not own any resources. Must free those below */
14738#define P4_FREE_IF_LE (-7)
14739#define P4_DYNAMIC (-7) /* Pointer to memory from sqliteMalloc() */
14740#define P4_FUNCDEF (-8) /* P4 is a pointer to a FuncDef structure */
14741#define P4_KEYINFO (-9) /* P4 is a pointer to a KeyInfo structure */
14742#define P4_EXPR (-10) /* P4 is a pointer to an Expr tree */
14743#define P4_MEM (-11) /* P4 is a pointer to a Mem* structure */
14744#define P4_VTAB (-12) /* P4 is a pointer to an sqlite3_vtab structure */
14745#define P4_REAL (-13) /* P4 is a 64-bit floating point value */
14746#define P4_INT64 (-14) /* P4 is a 64-bit signed integer */
14747#define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
14748#define P4_FUNCCTX (-16) /* P4 is a pointer to an sqlite3_context object */
14749#define P4_DYNBLOB (-17) /* Pointer to memory from sqliteMalloc() */
14750
14751/* Error message codes for OP_Halt */
14752#define P5_ConstraintNotNull 1
14753#define P5_ConstraintUnique 2
14754#define P5_ConstraintCheck 3
14755#define P5_ConstraintFK 4
14756
14757/*
14758** The Vdbe.aColName array contains 5n Mem structures, where n is the
14759** number of columns of data returned by the statement.
14760*/
14761#define COLNAME_NAME 0
14762#define COLNAME_DECLTYPE 1
14763#define COLNAME_DATABASE 2
14764#define COLNAME_TABLE 3
14765#define COLNAME_COLUMN 4
14766#ifdef SQLITE_ENABLE_COLUMN_METADATA
14767# define COLNAME_N 5 /* Number of COLNAME_xxx symbols */
14768#else
14769# ifdef SQLITE_OMIT_DECLTYPE
14770# define COLNAME_N 1 /* Store only the name */
14771# else
14772# define COLNAME_N 2 /* Store the name and decltype */
14773# endif
14774#endif
14775
14776/*
14777** The following macro converts a relative address in the p2 field
14778** of a VdbeOp structure into a negative number so that
14779** sqlite3VdbeAddOpList() knows that the address is relative. Calling
14780** the macro again restores the address.
14781*/
14782#define ADDR(X) (-1-(X))
14783
14784/*
14785** The makefile scans the vdbe.c source file and creates the "opcodes.h"
14786** header file that defines a number for each opcode used by the VDBE.
14787*/
14788/************** Include opcodes.h in the middle of vdbe.h ********************/
14789/************** Begin file opcodes.h *****************************************/
14790/* Automatically generated. Do not edit */
14791/* See the tool/mkopcodeh.tcl script for details */
14792#define OP_Savepoint 0
14793#define OP_AutoCommit 1
14794#define OP_Transaction 2
14795#define OP_SorterNext 3 /* jump */
14796#define OP_Prev 4 /* jump */
14797#define OP_Next 5 /* jump */
14798#define OP_Checkpoint 6
14799#define OP_JournalMode 7
14800#define OP_Vacuum 8
14801#define OP_VFilter 9 /* jump, synopsis: iplan=r[P3] zplan='P4' */
14802#define OP_VUpdate 10 /* synopsis: data=r[P3@P2] */
14803#define OP_Goto 11 /* jump */
14804#define OP_Gosub 12 /* jump */
14805#define OP_InitCoroutine 13 /* jump */
14806#define OP_Yield 14 /* jump */
14807#define OP_MustBeInt 15 /* jump */
14808#define OP_Jump 16 /* jump */
14809#define OP_Once 17 /* jump */
14810#define OP_If 18 /* jump */
14811#define OP_Not 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */
14812#define OP_IfNot 20 /* jump */
14813#define OP_IfNullRow 21 /* jump, synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */
14814#define OP_SeekLT 22 /* jump, synopsis: key=r[P3@P4] */
14815#define OP_SeekLE 23 /* jump, synopsis: key=r[P3@P4] */
14816#define OP_SeekGE 24 /* jump, synopsis: key=r[P3@P4] */
14817#define OP_SeekGT 25 /* jump, synopsis: key=r[P3@P4] */
14818#define OP_IfNoHope 26 /* jump, synopsis: key=r[P3@P4] */
14819#define OP_NoConflict 27 /* jump, synopsis: key=r[P3@P4] */
14820#define OP_NotFound 28 /* jump, synopsis: key=r[P3@P4] */
14821#define OP_Found 29 /* jump, synopsis: key=r[P3@P4] */
14822#define OP_SeekRowid 30 /* jump, synopsis: intkey=r[P3] */
14823#define OP_NotExists 31 /* jump, synopsis: intkey=r[P3] */
14824#define OP_Last 32 /* jump */
14825#define OP_IfSmaller 33 /* jump */
14826#define OP_SorterSort 34 /* jump */
14827#define OP_Sort 35 /* jump */
14828#define OP_Rewind 36 /* jump */
14829#define OP_IdxLE 37 /* jump, synopsis: key=r[P3@P4] */
14830#define OP_IdxGT 38 /* jump, synopsis: key=r[P3@P4] */
14831#define OP_IdxLT 39 /* jump, synopsis: key=r[P3@P4] */
14832#define OP_IdxGE 40 /* jump, synopsis: key=r[P3@P4] */
14833#define OP_RowSetRead 41 /* jump, synopsis: r[P3]=rowset(P1) */
14834#define OP_RowSetTest 42 /* jump, synopsis: if r[P3] in rowset(P1) goto P2 */
14835#define OP_Or 43 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
14836#define OP_And 44 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
14837#define OP_Program 45 /* jump */
14838#define OP_FkIfZero 46 /* jump, synopsis: if fkctr[P1]==0 goto P2 */
14839#define OP_IfPos 47 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
14840#define OP_IfNotZero 48 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
14841#define OP_DecrJumpZero 49 /* jump, synopsis: if (--r[P1])==0 goto P2 */
14842#define OP_IsNull 50 /* jump, same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
14843#define OP_NotNull 51 /* jump, same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
14844#define OP_Ne 52 /* jump, same as TK_NE, synopsis: IF r[P3]!=r[P1] */
14845#define OP_Eq 53 /* jump, same as TK_EQ, synopsis: IF r[P3]==r[P1] */
14846#define OP_Gt 54 /* jump, same as TK_GT, synopsis: IF r[P3]>r[P1] */
14847#define OP_Le 55 /* jump, same as TK_LE, synopsis: IF r[P3]<=r[P1] */
14848#define OP_Lt 56 /* jump, same as TK_LT, synopsis: IF r[P3]<r[P1] */
14849#define OP_Ge 57 /* jump, same as TK_GE, synopsis: IF r[P3]>=r[P1] */
14850#define OP_ElseNotEq 58 /* jump, same as TK_ESCAPE */
14851#define OP_IncrVacuum 59 /* jump */
14852#define OP_VNext 60 /* jump */
14853#define OP_Init 61 /* jump, synopsis: Start at P2 */
14854#define OP_PureFunc0 62
14855#define OP_Function0 63 /* synopsis: r[P3]=func(r[P2@P5]) */
14856#define OP_PureFunc 64
14857#define OP_Function 65 /* synopsis: r[P3]=func(r[P2@P5]) */
14858#define OP_Return 66
14859#define OP_EndCoroutine 67
14860#define OP_HaltIfNull 68 /* synopsis: if r[P3]=null halt */
14861#define OP_Halt 69
14862#define OP_Integer 70 /* synopsis: r[P2]=P1 */
14863#define OP_Int64 71 /* synopsis: r[P2]=P4 */
14864#define OP_String 72 /* synopsis: r[P2]='P4' (len=P1) */
14865#define OP_Null 73 /* synopsis: r[P2..P3]=NULL */
14866#define OP_SoftNull 74 /* synopsis: r[P1]=NULL */
14867#define OP_Blob 75 /* synopsis: r[P2]=P4 (len=P1) */
14868#define OP_Variable 76 /* synopsis: r[P2]=parameter(P1,P4) */
14869#define OP_Move 77 /* synopsis: r[P2@P3]=r[P1@P3] */
14870#define OP_Copy 78 /* synopsis: r[P2@P3+1]=r[P1@P3+1] */
14871#define OP_SCopy 79 /* synopsis: r[P2]=r[P1] */
14872#define OP_IntCopy 80 /* synopsis: r[P2]=r[P1] */
14873#define OP_ResultRow 81 /* synopsis: output=r[P1@P2] */
14874#define OP_CollSeq 82
14875#define OP_AddImm 83 /* synopsis: r[P1]=r[P1]+P2 */
14876#define OP_RealAffinity 84
14877#define OP_Cast 85 /* synopsis: affinity(r[P1]) */
14878#define OP_Permutation 86
14879#define OP_Compare 87 /* synopsis: r[P1@P3] <-> r[P2@P3] */
14880#define OP_IsTrue 88 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */
14881#define OP_Offset 89 /* synopsis: r[P3] = sqlite_offset(P1) */
14882#define OP_Column 90 /* synopsis: r[P3]=PX */
14883#define OP_Affinity 91 /* synopsis: affinity(r[P1@P2]) */
14884#define OP_BitAnd 92 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
14885#define OP_BitOr 93 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
14886#define OP_ShiftLeft 94 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
14887#define OP_ShiftRight 95 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
14888#define OP_Add 96 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
14889#define OP_Subtract 97 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
14890#define OP_Multiply 98 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
14891#define OP_Divide 99 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
14892#define OP_Remainder 100 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
14893#define OP_Concat 101 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
14894#define OP_MakeRecord 102 /* synopsis: r[P3]=mkrec(r[P1@P2]) */
14895#define OP_BitNot 103 /* same as TK_BITNOT, synopsis: r[P2]= ~r[P1] */
14896#define OP_Count 104 /* synopsis: r[P2]=count() */
14897#define OP_ReadCookie 105
14898#define OP_String8 106 /* same as TK_STRING, synopsis: r[P2]='P4' */
14899#define OP_SetCookie 107
14900#define OP_ReopenIdx 108 /* synopsis: root=P2 iDb=P3 */
14901#define OP_OpenRead 109 /* synopsis: root=P2 iDb=P3 */
14902#define OP_OpenWrite 110 /* synopsis: root=P2 iDb=P3 */
14903#define OP_OpenDup 111
14904#define OP_OpenAutoindex 112 /* synopsis: nColumn=P2 */
14905#define OP_OpenEphemeral 113 /* synopsis: nColumn=P2 */
14906#define OP_SorterOpen 114
14907#define OP_SequenceTest 115 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */
14908#define OP_OpenPseudo 116 /* synopsis: P3 columns in r[P2] */
14909#define OP_Close 117
14910#define OP_ColumnsUsed 118
14911#define OP_SeekHit 119 /* synopsis: seekHit=P2 */
14912#define OP_Sequence 120 /* synopsis: r[P2]=cursor[P1].ctr++ */
14913#define OP_NewRowid 121 /* synopsis: r[P2]=rowid */
14914#define OP_Insert 122 /* synopsis: intkey=r[P3] data=r[P2] */
14915#define OP_InsertInt 123 /* synopsis: intkey=P3 data=r[P2] */
14916#define OP_Delete 124
14917#define OP_ResetCount 125
14918#define OP_SorterCompare 126 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
14919#define OP_SorterData 127 /* synopsis: r[P2]=data */
14920#define OP_RowData 128 /* synopsis: r[P2]=data */
14921#define OP_Rowid 129 /* synopsis: r[P2]=rowid */
14922#define OP_NullRow 130
14923#define OP_SeekEnd 131
14924#define OP_SorterInsert 132 /* synopsis: key=r[P2] */
14925#define OP_IdxInsert 133 /* synopsis: key=r[P2] */
14926#define OP_IdxDelete 134 /* synopsis: key=r[P2@P3] */
14927#define OP_DeferredSeek 135 /* synopsis: Move P3 to P1.rowid if needed */
14928#define OP_IdxRowid 136 /* synopsis: r[P2]=rowid */
14929#define OP_Destroy 137
14930#define OP_Clear 138
14931#define OP_ResetSorter 139
14932#define OP_CreateBtree 140 /* synopsis: r[P2]=root iDb=P1 flags=P3 */
14933#define OP_Real 141 /* same as TK_FLOAT, synopsis: r[P2]=P4 */
14934#define OP_SqlExec 142
14935#define OP_ParseSchema 143
14936#define OP_LoadAnalysis 144
14937#define OP_DropTable 145
14938#define OP_DropIndex 146
14939#define OP_DropTrigger 147
14940#define OP_IntegrityCk 148
14941#define OP_RowSetAdd 149 /* synopsis: rowset(P1)=r[P2] */
14942#define OP_Param 150
14943#define OP_FkCounter 151 /* synopsis: fkctr[P1]+=P2 */
14944#define OP_MemMax 152 /* synopsis: r[P1]=max(r[P1],r[P2]) */
14945#define OP_OffsetLimit 153 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
14946#define OP_AggInverse 154 /* synopsis: accum=r[P3] inverse(r[P2@P5]) */
14947#define OP_AggStep 155 /* synopsis: accum=r[P3] step(r[P2@P5]) */
14948#define OP_AggStep1 156 /* synopsis: accum=r[P3] step(r[P2@P5]) */
14949#define OP_AggValue 157 /* synopsis: r[P3]=value N=P2 */
14950#define OP_AggFinal 158 /* synopsis: accum=r[P1] N=P2 */
14951#define OP_Expire 159
14952#define OP_TableLock 160 /* synopsis: iDb=P1 root=P2 write=P3 */
14953#define OP_VBegin 161
14954#define OP_VCreate 162
14955#define OP_VDestroy 163
14956#define OP_VOpen 164
14957#define OP_VColumn 165 /* synopsis: r[P3]=vcolumn(P2) */
14958#define OP_VRename 166
14959#define OP_Pagecount 167
14960#define OP_MaxPgcnt 168
14961#define OP_Trace 169
14962#define OP_CursorHint 170
14963#define OP_Noop 171
14964#define OP_Explain 172
14965#define OP_Abortable 173
14966
14967/* Properties such as "out2" or "jump" that are specified in
14968** comments following the "case" for each opcode in the vdbe.c
14969** are encoded into bitvectors as follows:
14970*/
14971#define OPFLG_JUMP 0x01 /* jump: P2 holds jmp target */
14972#define OPFLG_IN1 0x02 /* in1: P1 is an input */
14973#define OPFLG_IN2 0x04 /* in2: P2 is an input */
14974#define OPFLG_IN3 0x08 /* in3: P3 is an input */
14975#define OPFLG_OUT2 0x10 /* out2: P2 is an output */
14976#define OPFLG_OUT3 0x20 /* out3: P3 is an output */
14977#define OPFLG_INITIALIZER {\
14978/* 0 */ 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x10,\
14979/* 8 */ 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03,\
14980/* 16 */ 0x01, 0x01, 0x03, 0x12, 0x03, 0x01, 0x09, 0x09,\
14981/* 24 */ 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,\
14982/* 32 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\
14983/* 40 */ 0x01, 0x23, 0x0b, 0x26, 0x26, 0x01, 0x01, 0x03,\
14984/* 48 */ 0x03, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
14985/* 56 */ 0x0b, 0x0b, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,\
14986/* 64 */ 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10, 0x10,\
14987/* 72 */ 0x10, 0x10, 0x00, 0x10, 0x10, 0x00, 0x00, 0x10,\
14988/* 80 */ 0x10, 0x00, 0x00, 0x02, 0x02, 0x02, 0x00, 0x00,\
14989/* 88 */ 0x12, 0x20, 0x00, 0x00, 0x26, 0x26, 0x26, 0x26,\
14990/* 96 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00, 0x12,\
14991/* 104 */ 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
14992/* 112 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
14993/* 120 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
14994/* 128 */ 0x00, 0x10, 0x00, 0x00, 0x04, 0x04, 0x00, 0x00,\
14995/* 136 */ 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
14996/* 144 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x10, 0x00,\
14997/* 152 */ 0x04, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
14998/* 160 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\
14999/* 168 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,}
15000
15001/* The sqlite3P2Values() routine is able to run faster if it knows
15002** the value of the largest JUMP opcode. The smaller the maximum
15003** JUMP opcode the better, so the mkopcodeh.tcl script that
15004** generated this include file strives to group all JUMP opcodes
15005** together near the beginning of the list.
15006*/
15007#define SQLITE_MX_JUMP_OPCODE 61 /* Maximum JUMP opcode */
15008
15009/************** End of opcodes.h *********************************************/
15010/************** Continuing where we left off in vdbe.h ***********************/
15011
15012/*
15013** Additional non-public SQLITE_PREPARE_* flags
15014*/
15015#define SQLITE_PREPARE_SAVESQL 0x80 /* Preserve SQL text */
15016#define SQLITE_PREPARE_MASK 0x0f /* Mask of public flags */
15017
15018/*
15019** Prototypes for the VDBE interface. See comments on the implementation
15020** for a description of what each of these routines does.
15021*/
15022SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse*);
15023SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
15024SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
15025SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
15026SQLITE_PRIVATE int sqlite3VdbeGoto(Vdbe*,int);
15027SQLITE_PRIVATE int sqlite3VdbeLoadString(Vdbe*,int,const char*);
15028SQLITE_PRIVATE void sqlite3VdbeMultiLoad(Vdbe*,int,const char*,...);
15029SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
15030SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
15031SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int);
15032SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
15033SQLITE_PRIVATE void sqlite3VdbeEndCoroutine(Vdbe*,int);
15034#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
15035SQLITE_PRIVATE void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N);
15036SQLITE_PRIVATE void sqlite3VdbeVerifyNoResultRow(Vdbe *p);
15037#else
15038# define sqlite3VdbeVerifyNoMallocRequired(A,B)
15039# define sqlite3VdbeVerifyNoResultRow(A)
15040#endif
15041#if defined(SQLITE_DEBUG)
15042SQLITE_PRIVATE void sqlite3VdbeVerifyAbortable(Vdbe *p, int);
15043#else
15044# define sqlite3VdbeVerifyAbortable(A,B)
15045#endif
15046SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp,int iLineno);
15047#ifndef SQLITE_OMIT_EXPLAIN
15048SQLITE_PRIVATE void sqlite3VdbeExplain(Parse*,u8,const char*,...);
15049SQLITE_PRIVATE void sqlite3VdbeExplainPop(Parse*);
15050SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse*);
15051# define ExplainQueryPlan(P) sqlite3VdbeExplain P
15052# define ExplainQueryPlanPop(P) sqlite3VdbeExplainPop(P)
15053# define ExplainQueryPlanParent(P) sqlite3VdbeExplainParent(P)
15054#else
15055# define ExplainQueryPlan(P)
15056# define ExplainQueryPlanPop(P)
15057# define ExplainQueryPlanParent(P) 0
15058#endif
15059SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
15060SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, u32 addr, u8);
15061SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
15062SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
15063SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
15064SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u16 P5);
15065SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
15066SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
15067SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
15068SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
15069SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type);
15070SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
15071SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
15072SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
15073SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*);
15074SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
15075SQLITE_PRIVATE void sqlite3VdbeReusable(Vdbe*);
15076SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
15077SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3*,Vdbe*);
15078SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*);
15079SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*);
15080SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int);
15081SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*);
15082#ifdef SQLITE_DEBUG
15083SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *, int);
15084#endif
15085SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe*);
15086SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe*);
15087SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*);
15088SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int);
15089SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
15090SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*);
15091SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*);
15092SQLITE_PRIVATE u8 sqlite3VdbePrepareFlags(Vdbe*);
15093SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, u8);
15094SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*);
15095SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
15096SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
15097SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
15098#ifndef SQLITE_OMIT_TRACE
15099SQLITE_PRIVATE char *sqlite3VdbeExpandSql(Vdbe*, const char*);
15100#endif
15101SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
15102SQLITE_PRIVATE int sqlite3BlobCompare(const Mem*, const Mem*);
15103
15104SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
15105SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
15106SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(int, const void *, UnpackedRecord *, int);
15107SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo*);
15108
15109typedef int (*RecordCompare)(int,const void*,UnpackedRecord*);
15110SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
15111
15112#ifndef SQLITE_OMIT_TRIGGER
15113SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
15114#endif
15115
15116SQLITE_PRIVATE int sqlite3NotPureFunc(sqlite3_context*);
15117
15118/* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on
15119** each VDBE opcode.
15120**
15121** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
15122** comments in VDBE programs that show key decision points in the code
15123** generator.
15124*/
15125#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
15126SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe*, const char*, ...);
15127# define VdbeComment(X) sqlite3VdbeComment X
15128SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
15129# define VdbeNoopComment(X) sqlite3VdbeNoopComment X
15130# ifdef SQLITE_ENABLE_MODULE_COMMENTS
15131# define VdbeModuleComment(X) sqlite3VdbeNoopComment X
15132# else
15133# define VdbeModuleComment(X)
15134# endif
15135#else
15136# define VdbeComment(X)
15137# define VdbeNoopComment(X)
15138# define VdbeModuleComment(X)
15139#endif
15140
15141/*
15142** The VdbeCoverage macros are used to set a coverage testing point
15143** for VDBE branch instructions. The coverage testing points are line
15144** numbers in the sqlite3.c source file. VDBE branch coverage testing
15145** only works with an amalagmation build. That's ok since a VDBE branch
15146** coverage build designed for testing the test suite only. No application
15147** should ever ship with VDBE branch coverage measuring turned on.
15148**
15149** VdbeCoverage(v) // Mark the previously coded instruction
15150** // as a branch
15151**
15152** VdbeCoverageIf(v, conditional) // Mark previous if conditional true
15153**
15154** VdbeCoverageAlwaysTaken(v) // Previous branch is always taken
15155**
15156** VdbeCoverageNeverTaken(v) // Previous branch is never taken
15157**
15158** VdbeCoverageNeverNull(v) // Previous three-way branch is only
15159** // taken on the first two ways. The
15160** // NULL option is not possible
15161**
15162** VdbeCoverageEqNe(v) // Previous OP_Jump is only interested
15163** // in distingishing equal and not-equal.
15164**
15165** Every VDBE branch operation must be tagged with one of the macros above.
15166** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
15167** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
15168** routine in vdbe.c, alerting the developer to the missed tag.
15169**
15170** During testing, the test application will invoke
15171** sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE,...) to set a callback
15172** routine that is invoked as each bytecode branch is taken. The callback
15173** contains the sqlite3.c source line number ov the VdbeCoverage macro and
15174** flags to indicate whether or not the branch was taken. The test application
15175** is responsible for keeping track of this and reporting byte-code branches
15176** that are never taken.
15177**
15178** See the VdbeBranchTaken() macro and vdbeTakeBranch() function in the
15179** vdbe.c source file for additional information.
15180*/
15181#ifdef SQLITE_VDBE_COVERAGE
15182SQLITE_PRIVATE void sqlite3VdbeSetLineNumber(Vdbe*,int);
15183# define VdbeCoverage(v) sqlite3VdbeSetLineNumber(v,__LINE__)
15184# define VdbeCoverageIf(v,x) if(x)sqlite3VdbeSetLineNumber(v,__LINE__)
15185# define VdbeCoverageAlwaysTaken(v) \
15186 sqlite3VdbeSetLineNumber(v,__LINE__|0x5000000);
15187# define VdbeCoverageNeverTaken(v) \
15188 sqlite3VdbeSetLineNumber(v,__LINE__|0x6000000);
15189# define VdbeCoverageNeverNull(v) \
15190 sqlite3VdbeSetLineNumber(v,__LINE__|0x4000000);
15191# define VdbeCoverageNeverNullIf(v,x) \
15192 if(x)sqlite3VdbeSetLineNumber(v,__LINE__|0x4000000);
15193# define VdbeCoverageEqNe(v) \
15194 sqlite3VdbeSetLineNumber(v,__LINE__|0x8000000);
15195# define VDBE_OFFSET_LINENO(x) (__LINE__+x)
15196#else
15197# define VdbeCoverage(v)
15198# define VdbeCoverageIf(v,x)
15199# define VdbeCoverageAlwaysTaken(v)
15200# define VdbeCoverageNeverTaken(v)
15201# define VdbeCoverageNeverNull(v)
15202# define VdbeCoverageNeverNullIf(v,x)
15203# define VdbeCoverageEqNe(v)
15204# define VDBE_OFFSET_LINENO(x) 0
15205#endif
15206
15207#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
15208SQLITE_PRIVATE void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const char*);
15209#else
15210# define sqlite3VdbeScanStatus(a,b,c,d,e)
15211#endif
15212
15213#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
15214SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, VdbeOp*);
15215#endif
15216
15217#endif /* SQLITE_VDBE_H */
15218
15219/************** End of vdbe.h ************************************************/
15220/************** Continuing where we left off in sqliteInt.h ******************/
15221/************** Include pager.h in the middle of sqliteInt.h *****************/
15222/************** Begin file pager.h *******************************************/
15223/*
15224** 2001 September 15
15225**
15226** The author disclaims copyright to this source code. In place of
15227** a legal notice, here is a blessing:
15228**
15229** May you do good and not evil.
15230** May you find forgiveness for yourself and forgive others.
15231** May you share freely, never taking more than you give.
15232**
15233*************************************************************************
15234** This header file defines the interface that the sqlite page cache
15235** subsystem. The page cache subsystem reads and writes a file a page
15236** at a time and provides a journal for rollback.
15237*/
15238
15239#ifndef SQLITE_PAGER_H
15240#define SQLITE_PAGER_H
15241
15242/*
15243** Default maximum size for persistent journal files. A negative
15244** value means no limit. This value may be overridden using the
15245** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
15246*/
15247#ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
15248 #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
15249#endif
15250
15251/*
15252** The type used to represent a page number. The first page in a file
15253** is called page 1. 0 is used to represent "not a page".
15254*/
15255typedef u32 Pgno;
15256
15257/*
15258** Each open file is managed by a separate instance of the "Pager" structure.
15259*/
15260typedef struct Pager Pager;
15261
15262/*
15263** Handle type for pages.
15264*/
15265typedef struct PgHdr DbPage;
15266
15267/*
15268** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
15269** reserved for working around a windows/posix incompatibility). It is
15270** used in the journal to signify that the remainder of the journal file
15271** is devoted to storing a master journal name - there are no more pages to
15272** roll back. See comments for function writeMasterJournal() in pager.c
15273** for details.
15274*/
15275#define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
15276
15277/*
15278** Allowed values for the flags parameter to sqlite3PagerOpen().
15279**
15280** NOTE: These values must match the corresponding BTREE_ values in btree.h.
15281*/
15282#define PAGER_OMIT_JOURNAL 0x0001 /* Do not use a rollback journal */
15283#define PAGER_MEMORY 0x0002 /* In-memory database */
15284
15285/*
15286** Valid values for the second argument to sqlite3PagerLockingMode().
15287*/
15288#define PAGER_LOCKINGMODE_QUERY -1
15289#define PAGER_LOCKINGMODE_NORMAL 0
15290#define PAGER_LOCKINGMODE_EXCLUSIVE 1
15291
15292/*
15293** Numeric constants that encode the journalmode.
15294**
15295** The numeric values encoded here (other than PAGER_JOURNALMODE_QUERY)
15296** are exposed in the API via the "PRAGMA journal_mode" command and
15297** therefore cannot be changed without a compatibility break.
15298*/
15299#define PAGER_JOURNALMODE_QUERY (-1) /* Query the value of journalmode */
15300#define PAGER_JOURNALMODE_DELETE 0 /* Commit by deleting journal file */
15301#define PAGER_JOURNALMODE_PERSIST 1 /* Commit by zeroing journal header */
15302#define PAGER_JOURNALMODE_OFF 2 /* Journal omitted. */
15303#define PAGER_JOURNALMODE_TRUNCATE 3 /* Commit by truncating journal */
15304#define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
15305#define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
15306
15307/*
15308** Flags that make up the mask passed to sqlite3PagerGet().
15309*/
15310#define PAGER_GET_NOCONTENT 0x01 /* Do not load data from disk */
15311#define PAGER_GET_READONLY 0x02 /* Read-only page is acceptable */
15312
15313/*
15314** Flags for sqlite3PagerSetFlags()
15315**
15316** Value constraints (enforced via assert()):
15317** PAGER_FULLFSYNC == SQLITE_FullFSync
15318** PAGER_CKPT_FULLFSYNC == SQLITE_CkptFullFSync
15319** PAGER_CACHE_SPILL == SQLITE_CacheSpill
15320*/
15321#define PAGER_SYNCHRONOUS_OFF 0x01 /* PRAGMA synchronous=OFF */
15322#define PAGER_SYNCHRONOUS_NORMAL 0x02 /* PRAGMA synchronous=NORMAL */
15323#define PAGER_SYNCHRONOUS_FULL 0x03 /* PRAGMA synchronous=FULL */
15324#define PAGER_SYNCHRONOUS_EXTRA 0x04 /* PRAGMA synchronous=EXTRA */
15325#define PAGER_SYNCHRONOUS_MASK 0x07 /* Mask for four values above */
15326#define PAGER_FULLFSYNC 0x08 /* PRAGMA fullfsync=ON */
15327#define PAGER_CKPT_FULLFSYNC 0x10 /* PRAGMA checkpoint_fullfsync=ON */
15328#define PAGER_CACHESPILL 0x20 /* PRAGMA cache_spill=ON */
15329#define PAGER_FLAGS_MASK 0x38 /* All above except SYNCHRONOUS */
15330
15331/*
15332** The remainder of this file contains the declarations of the functions
15333** that make up the Pager sub-system API. See source code comments for
15334** a detailed description of each routine.
15335*/
15336
15337/* Open and close a Pager connection. */
15338SQLITE_PRIVATE int sqlite3PagerOpen(
15339 sqlite3_vfs*,
15340 Pager **ppPager,
15341 const char*,
15342 int,
15343 int,
15344 int,
15345 void(*)(DbPage*)
15346);
15347SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3*);
15348SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
15349
15350/* Functions used to configure a Pager object. */
15351SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(Pager*, int(*)(void *), void *);
15352SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
15353#ifdef SQLITE_HAS_CODEC
15354SQLITE_PRIVATE void sqlite3PagerAlignReserve(Pager*,Pager*);
15355#endif
15356SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
15357SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
15358SQLITE_PRIVATE int sqlite3PagerSetSpillsize(Pager*, int);
15359SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *, sqlite3_int64);
15360SQLITE_PRIVATE void sqlite3PagerShrink(Pager*);
15361SQLITE_PRIVATE void sqlite3PagerSetFlags(Pager*,unsigned);
15362SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
15363SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
15364SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
15365SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
15366SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
15367SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
15368SQLITE_PRIVATE int sqlite3PagerFlush(Pager*);
15369
15370/* Functions used to obtain and release page references. */
15371SQLITE_PRIVATE int sqlite3PagerGet(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
15372SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
15373SQLITE_PRIVATE void sqlite3PagerRef(DbPage*);
15374SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*);
15375SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage*);
15376SQLITE_PRIVATE void sqlite3PagerUnrefPageOne(DbPage*);
15377
15378/* Operations on page references. */
15379SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
15380SQLITE_PRIVATE void sqlite3PagerDontWrite(DbPage*);
15381SQLITE_PRIVATE int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
15382SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*);
15383SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *);
15384SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *);
15385
15386/* Functions used to manage pager transactions and savepoints. */
15387SQLITE_PRIVATE void sqlite3PagerPagecount(Pager*, int*);
15388SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
15389SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int);
15390SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*);
15391SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster);
15392SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
15393SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
15394SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
15395SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
15396SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
15397
15398#ifndef SQLITE_OMIT_WAL
15399SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*);
15400SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager);
15401SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager);
15402SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
15403SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3*);
15404# ifdef SQLITE_ENABLE_SNAPSHOT
15405SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot);
15406SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot);
15407SQLITE_PRIVATE int sqlite3PagerSnapshotRecover(Pager *pPager);
15408SQLITE_PRIVATE int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot);
15409SQLITE_PRIVATE void sqlite3PagerSnapshotUnlock(Pager *pPager);
15410# endif
15411#endif
15412
15413#ifdef SQLITE_DIRECT_OVERFLOW_READ
15414SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno);
15415#endif
15416
15417#ifdef SQLITE_ENABLE_ZIPVFS
15418SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager);
15419#endif
15420
15421/* Functions used to query pager state and configuration. */
15422SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
15423SQLITE_PRIVATE u32 sqlite3PagerDataVersion(Pager*);
15424#ifdef SQLITE_DEBUG
15425SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
15426#endif
15427SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
15428SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*, int);
15429SQLITE_PRIVATE sqlite3_vfs *sqlite3PagerVfs(Pager*);
15430SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
15431SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager*);
15432SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
15433SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
15434SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
15435SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
15436SQLITE_PRIVATE void sqlite3PagerClearCache(Pager*);
15437SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
15438#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
15439SQLITE_PRIVATE void sqlite3PagerResetLockTimeout(Pager *pPager);
15440#else
15441# define sqlite3PagerResetLockTimeout(X)
15442#endif
15443
15444/* Functions used to truncate the database file. */
15445SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
15446
15447SQLITE_PRIVATE void sqlite3PagerRekey(DbPage*, Pgno, u16);
15448
15449#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
15450SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *);
15451#endif
15452
15453/* Functions to support testing and debugging. */
15454#if !defined(NDEBUG) || defined(SQLITE_TEST)
15455SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage*);
15456SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage*);
15457#endif
15458#ifdef SQLITE_TEST
15459SQLITE_PRIVATE int *sqlite3PagerStats(Pager*);
15460SQLITE_PRIVATE void sqlite3PagerRefdump(Pager*);
15461 void disable_simulated_io_errors(void);
15462 void enable_simulated_io_errors(void);
15463#else
15464# define disable_simulated_io_errors()
15465# define enable_simulated_io_errors()
15466#endif
15467
15468#endif /* SQLITE_PAGER_H */
15469
15470/************** End of pager.h ***********************************************/
15471/************** Continuing where we left off in sqliteInt.h ******************/
15472/************** Include pcache.h in the middle of sqliteInt.h ****************/
15473/************** Begin file pcache.h ******************************************/
15474/*
15475** 2008 August 05
15476**
15477** The author disclaims copyright to this source code. In place of
15478** a legal notice, here is a blessing:
15479**
15480** May you do good and not evil.
15481** May you find forgiveness for yourself and forgive others.
15482** May you share freely, never taking more than you give.
15483**
15484*************************************************************************
15485** This header file defines the interface that the sqlite page cache
15486** subsystem.
15487*/
15488
15489#ifndef _PCACHE_H_
15490
15491typedef struct PgHdr PgHdr;
15492typedef struct PCache PCache;
15493
15494/*
15495** Every page in the cache is controlled by an instance of the following
15496** structure.
15497*/
15498struct PgHdr {
15499 sqlite3_pcache_page *pPage; /* Pcache object page handle */
15500 void *pData; /* Page data */
15501 void *pExtra; /* Extra content */
15502 PCache *pCache; /* PRIVATE: Cache that owns this page */
15503 PgHdr *pDirty; /* Transient list of dirty sorted by pgno */
15504 Pager *pPager; /* The pager this page is part of */
15505 Pgno pgno; /* Page number for this page */
15506#ifdef SQLITE_CHECK_PAGES
15507 u32 pageHash; /* Hash of page content */
15508#endif
15509 u16 flags; /* PGHDR flags defined below */
15510
15511 /**********************************************************************
15512 ** Elements above, except pCache, are public. All that follow are
15513 ** private to pcache.c and should not be accessed by other modules.
15514 ** pCache is grouped with the public elements for efficiency.
15515 */
15516 i16 nRef; /* Number of users of this page */
15517 PgHdr *pDirtyNext; /* Next element in list of dirty pages */
15518 PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */
15519 /* NB: pDirtyNext and pDirtyPrev are undefined if the
15520 ** PgHdr object is not dirty */
15521};
15522
15523/* Bit values for PgHdr.flags */
15524#define PGHDR_CLEAN 0x001 /* Page not on the PCache.pDirty list */
15525#define PGHDR_DIRTY 0x002 /* Page is on the PCache.pDirty list */
15526#define PGHDR_WRITEABLE 0x004 /* Journaled and ready to modify */
15527#define PGHDR_NEED_SYNC 0x008 /* Fsync the rollback journal before
15528 ** writing this page to the database */
15529#define PGHDR_DONT_WRITE 0x010 /* Do not write content to disk */
15530#define PGHDR_MMAP 0x020 /* This is an mmap page object */
15531
15532#define PGHDR_WAL_APPEND 0x040 /* Appended to wal file */
15533
15534/* Initialize and shutdown the page cache subsystem */
15535SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
15536SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
15537
15538/* Page cache buffer management:
15539** These routines implement SQLITE_CONFIG_PAGECACHE.
15540*/
15541SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n);
15542
15543/* Create a new pager cache.
15544** Under memory stress, invoke xStress to try to make pages clean.
15545** Only clean and unpinned pages can be reclaimed.
15546*/
15547SQLITE_PRIVATE int sqlite3PcacheOpen(
15548 int szPage, /* Size of every page */
15549 int szExtra, /* Extra space associated with each page */
15550 int bPurgeable, /* True if pages are on backing store */
15551 int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
15552 void *pStress, /* Argument to xStress */
15553 PCache *pToInit /* Preallocated space for the PCache */
15554);
15555
15556/* Modify the page-size after the cache has been created. */
15557SQLITE_PRIVATE int sqlite3PcacheSetPageSize(PCache *, int);
15558
15559/* Return the size in bytes of a PCache object. Used to preallocate
15560** storage space.
15561*/
15562SQLITE_PRIVATE int sqlite3PcacheSize(void);
15563
15564/* One release per successful fetch. Page is pinned until released.
15565** Reference counted.
15566*/
15567SQLITE_PRIVATE sqlite3_pcache_page *sqlite3PcacheFetch(PCache*, Pgno, int createFlag);
15568SQLITE_PRIVATE int sqlite3PcacheFetchStress(PCache*, Pgno, sqlite3_pcache_page**);
15569SQLITE_PRIVATE PgHdr *sqlite3PcacheFetchFinish(PCache*, Pgno, sqlite3_pcache_page *pPage);
15570SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr*);
15571
15572SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*); /* Remove page from cache */
15573SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*); /* Make sure page is marked dirty */
15574SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*); /* Mark a single page as clean */
15575SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*); /* Mark all dirty list pages as clean */
15576SQLITE_PRIVATE void sqlite3PcacheClearWritable(PCache*);
15577
15578/* Change a page number. Used by incr-vacuum. */
15579SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno);
15580
15581/* Remove all pages with pgno>x. Reset the cache if x==0 */
15582SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache*, Pgno x);
15583
15584/* Get a list of all dirty pages in the cache, sorted by page number */
15585SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache*);
15586
15587/* Reset and close the cache object */
15588SQLITE_PRIVATE void sqlite3PcacheClose(PCache*);
15589
15590/* Clear flags from pages of the page cache */
15591SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *);
15592
15593/* Discard the contents of the cache */
15594SQLITE_PRIVATE void sqlite3PcacheClear(PCache*);
15595
15596/* Return the total number of outstanding page references */
15597SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache*);
15598
15599/* Increment the reference count of an existing page */
15600SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr*);
15601
15602SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr*);
15603
15604/* Return the total number of pages stored in the cache */
15605SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
15606
15607#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
15608/* Iterate through all dirty pages currently stored in the cache. This
15609** interface is only available if SQLITE_CHECK_PAGES is defined when the
15610** library is built.
15611*/
15612SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
15613#endif
15614
15615#if defined(SQLITE_DEBUG)
15616/* Check invariants on a PgHdr object */
15617SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr*);
15618#endif
15619
15620/* Set and get the suggested cache-size for the specified pager-cache.
15621**
15622** If no global maximum is configured, then the system attempts to limit
15623** the total number of pages cached by purgeable pager-caches to the sum
15624** of the suggested cache-sizes.
15625*/
15626SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
15627#ifdef SQLITE_TEST
15628SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
15629#endif
15630
15631/* Set or get the suggested spill-size for the specified pager-cache.
15632**
15633** The spill-size is the minimum number of pages in cache before the cache
15634** will attempt to spill dirty pages by calling xStress.
15635*/
15636SQLITE_PRIVATE int sqlite3PcacheSetSpillsize(PCache *, int);
15637
15638/* Free up as much memory as possible from the page cache */
15639SQLITE_PRIVATE void sqlite3PcacheShrink(PCache*);
15640
15641#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
15642/* Try to return memory used by the pcache module to the main memory heap */
15643SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
15644#endif
15645
15646#ifdef SQLITE_TEST
15647SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
15648#endif
15649
15650SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
15651
15652/* Return the header size */
15653SQLITE_PRIVATE int sqlite3HeaderSizePcache(void);
15654SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void);
15655
15656/* Number of dirty pages as a percentage of the configured cache size */
15657SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache*);
15658
15659#ifdef SQLITE_DIRECT_OVERFLOW_READ
15660SQLITE_PRIVATE int sqlite3PCacheIsDirty(PCache *pCache);
15661#endif
15662
15663#endif /* _PCACHE_H_ */
15664
15665/************** End of pcache.h **********************************************/
15666/************** Continuing where we left off in sqliteInt.h ******************/
15667/************** Include os.h in the middle of sqliteInt.h ********************/
15668/************** Begin file os.h **********************************************/
15669/*
15670** 2001 September 16
15671**
15672** The author disclaims copyright to this source code. In place of
15673** a legal notice, here is a blessing:
15674**
15675** May you do good and not evil.
15676** May you find forgiveness for yourself and forgive others.
15677** May you share freely, never taking more than you give.
15678**
15679******************************************************************************
15680**
15681** This header file (together with is companion C source-code file
15682** "os.c") attempt to abstract the underlying operating system so that
15683** the SQLite library will work on both POSIX and windows systems.
15684**
15685** This header file is #include-ed by sqliteInt.h and thus ends up
15686** being included by every source file.
15687*/
15688#ifndef _SQLITE_OS_H_
15689#define _SQLITE_OS_H_
15690
15691/*
15692** Attempt to automatically detect the operating system and setup the
15693** necessary pre-processor macros for it.
15694*/
15695/************** Include os_setup.h in the middle of os.h *********************/
15696/************** Begin file os_setup.h ****************************************/
15697/*
15698** 2013 November 25
15699**
15700** The author disclaims copyright to this source code. In place of
15701** a legal notice, here is a blessing:
15702**
15703** May you do good and not evil.
15704** May you find forgiveness for yourself and forgive others.
15705** May you share freely, never taking more than you give.
15706**
15707******************************************************************************
15708**
15709** This file contains pre-processor directives related to operating system
15710** detection and/or setup.
15711*/
15712#ifndef SQLITE_OS_SETUP_H
15713#define SQLITE_OS_SETUP_H
15714
15715/*
15716** Figure out if we are dealing with Unix, Windows, or some other operating
15717** system.
15718**
15719** After the following block of preprocess macros, all of SQLITE_OS_UNIX,
15720** SQLITE_OS_WIN, and SQLITE_OS_OTHER will defined to either 1 or 0. One of
15721** the three will be 1. The other two will be 0.
15722*/
15723#if defined(SQLITE_OS_OTHER)
15724# if SQLITE_OS_OTHER==1
15725# undef SQLITE_OS_UNIX
15726# define SQLITE_OS_UNIX 0
15727# undef SQLITE_OS_WIN
15728# define SQLITE_OS_WIN 0
15729# else
15730# undef SQLITE_OS_OTHER
15731# endif
15732#endif
15733#if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
15734# define SQLITE_OS_OTHER 0
15735# ifndef SQLITE_OS_WIN
15736# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
15737 defined(__MINGW32__) || defined(__BORLANDC__)
15738# define SQLITE_OS_WIN 1
15739# define SQLITE_OS_UNIX 0
15740# else
15741# define SQLITE_OS_WIN 0
15742# define SQLITE_OS_UNIX 1
15743# endif
15744# else
15745# define SQLITE_OS_UNIX 0
15746# endif
15747#else
15748# ifndef SQLITE_OS_WIN
15749# define SQLITE_OS_WIN 0
15750# endif
15751#endif
15752
15753#endif /* SQLITE_OS_SETUP_H */
15754
15755/************** End of os_setup.h ********************************************/
15756/************** Continuing where we left off in os.h *************************/
15757
15758/* If the SET_FULLSYNC macro is not defined above, then make it
15759** a no-op
15760*/
15761#ifndef SET_FULLSYNC
15762# define SET_FULLSYNC(x,y)
15763#endif
15764
15765/*
15766** The default size of a disk sector
15767*/
15768#ifndef SQLITE_DEFAULT_SECTOR_SIZE
15769# define SQLITE_DEFAULT_SECTOR_SIZE 4096
15770#endif
15771
15772/*
15773** Temporary files are named starting with this prefix followed by 16 random
15774** alphanumeric characters, and no file extension. They are stored in the
15775** OS's standard temporary file directory, and are deleted prior to exit.
15776** If sqlite is being embedded in another program, you may wish to change the
15777** prefix to reflect your program's name, so that if your program exits
15778** prematurely, old temporary files can be easily identified. This can be done
15779** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
15780**
15781** 2006-10-31: The default prefix used to be "sqlite_". But then
15782** Mcafee started using SQLite in their anti-virus product and it
15783** started putting files with the "sqlite" name in the c:/temp folder.
15784** This annoyed many windows users. Those users would then do a
15785** Google search for "sqlite", find the telephone numbers of the
15786** developers and call to wake them up at night and complain.
15787** For this reason, the default name prefix is changed to be "sqlite"
15788** spelled backwards. So the temp files are still identified, but
15789** anybody smart enough to figure out the code is also likely smart
15790** enough to know that calling the developer will not help get rid
15791** of the file.
15792*/
15793#ifndef SQLITE_TEMP_FILE_PREFIX
15794# define SQLITE_TEMP_FILE_PREFIX "etilqs_"
15795#endif
15796
15797/*
15798** The following values may be passed as the second argument to
15799** sqlite3OsLock(). The various locks exhibit the following semantics:
15800**
15801** SHARED: Any number of processes may hold a SHARED lock simultaneously.
15802** RESERVED: A single process may hold a RESERVED lock on a file at
15803** any time. Other processes may hold and obtain new SHARED locks.
15804** PENDING: A single process may hold a PENDING lock on a file at
15805** any one time. Existing SHARED locks may persist, but no new
15806** SHARED locks may be obtained by other processes.
15807** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
15808**
15809** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
15810** process that requests an EXCLUSIVE lock may actually obtain a PENDING
15811** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
15812** sqlite3OsLock().
15813*/
15814#define NO_LOCK 0
15815#define SHARED_LOCK 1
15816#define RESERVED_LOCK 2
15817#define PENDING_LOCK 3
15818#define EXCLUSIVE_LOCK 4
15819
15820/*
15821** File Locking Notes: (Mostly about windows but also some info for Unix)
15822**
15823** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
15824** those functions are not available. So we use only LockFile() and
15825** UnlockFile().
15826**
15827** LockFile() prevents not just writing but also reading by other processes.
15828** A SHARED_LOCK is obtained by locking a single randomly-chosen
15829** byte out of a specific range of bytes. The lock byte is obtained at
15830** random so two separate readers can probably access the file at the
15831** same time, unless they are unlucky and choose the same lock byte.
15832** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
15833** There can only be one writer. A RESERVED_LOCK is obtained by locking
15834** a single byte of the file that is designated as the reserved lock byte.
15835** A PENDING_LOCK is obtained by locking a designated byte different from
15836** the RESERVED_LOCK byte.
15837**
15838** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
15839** which means we can use reader/writer locks. When reader/writer locks
15840** are used, the lock is placed on the same range of bytes that is used
15841** for probabilistic locking in Win95/98/ME. Hence, the locking scheme
15842** will support two or more Win95 readers or two or more WinNT readers.
15843** But a single Win95 reader will lock out all WinNT readers and a single
15844** WinNT reader will lock out all other Win95 readers.
15845**
15846** The following #defines specify the range of bytes used for locking.
15847** SHARED_SIZE is the number of bytes available in the pool from which
15848** a random byte is selected for a shared lock. The pool of bytes for
15849** shared locks begins at SHARED_FIRST.
15850**
15851** The same locking strategy and
15852** byte ranges are used for Unix. This leaves open the possibility of having
15853** clients on win95, winNT, and unix all talking to the same shared file
15854** and all locking correctly. To do so would require that samba (or whatever
15855** tool is being used for file sharing) implements locks correctly between
15856** windows and unix. I'm guessing that isn't likely to happen, but by
15857** using the same locking range we are at least open to the possibility.
15858**
15859** Locking in windows is manditory. For this reason, we cannot store
15860** actual data in the bytes used for locking. The pager never allocates
15861** the pages involved in locking therefore. SHARED_SIZE is selected so
15862** that all locks will fit on a single page even at the minimum page size.
15863** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE
15864** is set high so that we don't have to allocate an unused page except
15865** for very large databases. But one should test the page skipping logic
15866** by setting PENDING_BYTE low and running the entire regression suite.
15867**
15868** Changing the value of PENDING_BYTE results in a subtly incompatible
15869** file format. Depending on how it is changed, you might not notice
15870** the incompatibility right away, even running a full regression test.
15871** The default location of PENDING_BYTE is the first byte past the
15872** 1GB boundary.
15873**
15874*/
15875#ifdef SQLITE_OMIT_WSD
15876# define PENDING_BYTE (0x40000000)
15877#else
15878# define PENDING_BYTE sqlite3PendingByte
15879#endif
15880#define RESERVED_BYTE (PENDING_BYTE+1)
15881#define SHARED_FIRST (PENDING_BYTE+2)
15882#define SHARED_SIZE 510
15883
15884/*
15885** Wrapper around OS specific sqlite3_os_init() function.
15886*/
15887SQLITE_PRIVATE int sqlite3OsInit(void);
15888
15889/*
15890** Functions for accessing sqlite3_file methods
15891*/
15892SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file*);
15893SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
15894SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
15895SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
15896SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
15897SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
15898SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
15899SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
15900SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
15901SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
15902SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
15903#define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
15904SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
15905SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
15906#ifndef SQLITE_OMIT_WAL
15907SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
15908SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
15909SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
15910SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
15911#endif /* SQLITE_OMIT_WAL */
15912SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
15913SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
15914
15915
15916/*
15917** Functions for accessing sqlite3_vfs methods
15918*/
15919SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
15920SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
15921SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
15922SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
15923#ifndef SQLITE_OMIT_LOAD_EXTENSION
15924SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
15925SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
15926SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
15927SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
15928#endif /* SQLITE_OMIT_LOAD_EXTENSION */
15929SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
15930SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
15931SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs*);
15932SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
15933
15934/*
15935** Convenience functions for opening and closing files using
15936** sqlite3_malloc() to obtain space for the file-handle structure.
15937*/
15938SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
15939SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *);
15940
15941#endif /* _SQLITE_OS_H_ */
15942
15943/************** End of os.h **************************************************/
15944/************** Continuing where we left off in sqliteInt.h ******************/
15945/************** Include mutex.h in the middle of sqliteInt.h *****************/
15946/************** Begin file mutex.h *******************************************/
15947/*
15948** 2007 August 28
15949**
15950** The author disclaims copyright to this source code. In place of
15951** a legal notice, here is a blessing:
15952**
15953** May you do good and not evil.
15954** May you find forgiveness for yourself and forgive others.
15955** May you share freely, never taking more than you give.
15956**
15957*************************************************************************
15958**
15959** This file contains the common header for all mutex implementations.
15960** The sqliteInt.h header #includes this file so that it is available
15961** to all source files. We break it out in an effort to keep the code
15962** better organized.
15963**
15964** NOTE: source files should *not* #include this header file directly.
15965** Source files should #include the sqliteInt.h file and let that file
15966** include this one indirectly.
15967*/
15968
15969
15970/*
15971** Figure out what version of the code to use. The choices are
15972**
15973** SQLITE_MUTEX_OMIT No mutex logic. Not even stubs. The
15974** mutexes implementation cannot be overridden
15975** at start-time.
15976**
15977** SQLITE_MUTEX_NOOP For single-threaded applications. No
15978** mutual exclusion is provided. But this
15979** implementation can be overridden at
15980** start-time.
15981**
15982** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
15983**
15984** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
15985*/
15986#if !SQLITE_THREADSAFE
15987# define SQLITE_MUTEX_OMIT
15988#endif
15989#if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP)
15990# if SQLITE_OS_UNIX
15991# define SQLITE_MUTEX_PTHREADS
15992# elif SQLITE_OS_WIN
15993# define SQLITE_MUTEX_W32
15994# else
15995# define SQLITE_MUTEX_NOOP
15996# endif
15997#endif
15998
15999#ifdef SQLITE_MUTEX_OMIT
16000/*
16001** If this is a no-op implementation, implement everything as macros.
16002*/
16003#define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8)
16004#define sqlite3_mutex_free(X)
16005#define sqlite3_mutex_enter(X)
16006#define sqlite3_mutex_try(X) SQLITE_OK
16007#define sqlite3_mutex_leave(X)
16008#define sqlite3_mutex_held(X) ((void)(X),1)
16009#define sqlite3_mutex_notheld(X) ((void)(X),1)
16010#define sqlite3MutexAlloc(X) ((sqlite3_mutex*)8)
16011#define sqlite3MutexInit() SQLITE_OK
16012#define sqlite3MutexEnd()
16013#define MUTEX_LOGIC(X)
16014#else
16015#define MUTEX_LOGIC(X) X
16016#endif /* defined(SQLITE_MUTEX_OMIT) */
16017
16018/************** End of mutex.h ***********************************************/
16019/************** Continuing where we left off in sqliteInt.h ******************/
16020
16021/* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
16022** synchronous setting to EXTRA. It is no longer supported.
16023*/
16024#ifdef SQLITE_EXTRA_DURABLE
16025# warning Use SQLITE_DEFAULT_SYNCHRONOUS=3 instead of SQLITE_EXTRA_DURABLE
16026# define SQLITE_DEFAULT_SYNCHRONOUS 3
16027#endif
16028
16029/*
16030** Default synchronous levels.
16031**
16032** Note that (for historcal reasons) the PAGER_SYNCHRONOUS_* macros differ
16033** from the SQLITE_DEFAULT_SYNCHRONOUS value by 1.
16034**
16035** PAGER_SYNCHRONOUS DEFAULT_SYNCHRONOUS
16036** OFF 1 0
16037** NORMAL 2 1
16038** FULL 3 2
16039** EXTRA 4 3
16040**
16041** The "PRAGMA synchronous" statement also uses the zero-based numbers.
16042** In other words, the zero-based numbers are used for all external interfaces
16043** and the one-based values are used internally.
16044*/
16045#ifndef SQLITE_DEFAULT_SYNCHRONOUS
16046# define SQLITE_DEFAULT_SYNCHRONOUS 2
16047#endif
16048#ifndef SQLITE_DEFAULT_WAL_SYNCHRONOUS
16049# define SQLITE_DEFAULT_WAL_SYNCHRONOUS SQLITE_DEFAULT_SYNCHRONOUS
16050#endif
16051
16052/*
16053** Each database file to be accessed by the system is an instance
16054** of the following structure. There are normally two of these structures
16055** in the sqlite.aDb[] array. aDb[0] is the main database file and
16056** aDb[1] is the database file used to hold temporary tables. Additional
16057** databases may be attached.
16058*/
16059struct Db {
16060 char *zDbSName; /* Name of this database. (schema name, not filename) */
16061 Btree *pBt; /* The B*Tree structure for this database file */
16062 u8 safety_level; /* How aggressive at syncing data to disk */
16063 u8 bSyncSet; /* True if "PRAGMA synchronous=N" has been run */
16064 Schema *pSchema; /* Pointer to database schema (possibly shared) */
16065};
16066
16067/*
16068** An instance of the following structure stores a database schema.
16069**
16070** Most Schema objects are associated with a Btree. The exception is
16071** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
16072** In shared cache mode, a single Schema object can be shared by multiple
16073** Btrees that refer to the same underlying BtShared object.
16074**
16075** Schema objects are automatically deallocated when the last Btree that
16076** references them is destroyed. The TEMP Schema is manually freed by
16077** sqlite3_close().
16078*
16079** A thread must be holding a mutex on the corresponding Btree in order
16080** to access Schema content. This implies that the thread must also be
16081** holding a mutex on the sqlite3 connection pointer that owns the Btree.
16082** For a TEMP Schema, only the connection mutex is required.
16083*/
16084struct Schema {
16085 int schema_cookie; /* Database schema version number for this file */
16086 int iGeneration; /* Generation counter. Incremented with each change */
16087 Hash tblHash; /* All tables indexed by name */
16088 Hash idxHash; /* All (named) indices indexed by name */
16089 Hash trigHash; /* All triggers indexed by name */
16090 Hash fkeyHash; /* All foreign keys by referenced table name */
16091 Table *pSeqTab; /* The sqlite_sequence table used by AUTOINCREMENT */
16092 u8 file_format; /* Schema format version for this file */
16093 u8 enc; /* Text encoding used by this database */
16094 u16 schemaFlags; /* Flags associated with this schema */
16095 int cache_size; /* Number of pages to use in the cache */
16096};
16097
16098/*
16099** These macros can be used to test, set, or clear bits in the
16100** Db.pSchema->flags field.
16101*/
16102#define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
16103#define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
16104#define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags|=(P)
16105#define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags&=~(P)
16106
16107/*
16108** Allowed values for the DB.pSchema->flags field.
16109**
16110** The DB_SchemaLoaded flag is set after the database schema has been
16111** read into internal hash tables.
16112**
16113** DB_UnresetViews means that one or more views have column names that
16114** have been filled out. If the schema changes, these column names might
16115** changes and so the view will need to be reset.
16116*/
16117#define DB_SchemaLoaded 0x0001 /* The schema has been loaded */
16118#define DB_UnresetViews 0x0002 /* Some views have defined column names */
16119#define DB_Empty 0x0004 /* The file is empty (length 0 bytes) */
16120#define DB_ResetWanted 0x0008 /* Reset the schema when nSchemaLock==0 */
16121
16122/*
16123** The number of different kinds of things that can be limited
16124** using the sqlite3_limit() interface.
16125*/
16126#define SQLITE_N_LIMIT (SQLITE_LIMIT_WORKER_THREADS+1)
16127
16128/*
16129** Lookaside malloc is a set of fixed-size buffers that can be used
16130** to satisfy small transient memory allocation requests for objects
16131** associated with a particular database connection. The use of
16132** lookaside malloc provides a significant performance enhancement
16133** (approx 10%) by avoiding numerous malloc/free requests while parsing
16134** SQL statements.
16135**
16136** The Lookaside structure holds configuration information about the
16137** lookaside malloc subsystem. Each available memory allocation in
16138** the lookaside subsystem is stored on a linked list of LookasideSlot
16139** objects.
16140**
16141** Lookaside allocations are only allowed for objects that are associated
16142** with a particular database connection. Hence, schema information cannot
16143** be stored in lookaside because in shared cache mode the schema information
16144** is shared by multiple database connections. Therefore, while parsing
16145** schema information, the Lookaside.bEnabled flag is cleared so that
16146** lookaside allocations are not used to construct the schema objects.
16147*/
16148struct Lookaside {
16149 u32 bDisable; /* Only operate the lookaside when zero */
16150 u16 sz; /* Size of each buffer in bytes */
16151 u8 bMalloced; /* True if pStart obtained from sqlite3_malloc() */
16152 u32 nSlot; /* Number of lookaside slots allocated */
16153 u32 anStat[3]; /* 0: hits. 1: size misses. 2: full misses */
16154 LookasideSlot *pInit; /* List of buffers not previously used */
16155 LookasideSlot *pFree; /* List of available buffers */
16156 void *pStart; /* First byte of available memory space */
16157 void *pEnd; /* First byte past end of available space */
16158};
16159struct LookasideSlot {
16160 LookasideSlot *pNext; /* Next buffer in the list of free buffers */
16161};
16162
16163/*
16164** A hash table for built-in function definitions. (Application-defined
16165** functions use a regular table table from hash.h.)
16166**
16167** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
16168** Collisions are on the FuncDef.u.pHash chain. Use the SQLITE_FUNC_HASH()
16169** macro to compute a hash on the function name.
16170*/
16171#define SQLITE_FUNC_HASH_SZ 23
16172struct FuncDefHash {
16173 FuncDef *a[SQLITE_FUNC_HASH_SZ]; /* Hash table for functions */
16174};
16175#define SQLITE_FUNC_HASH(C,L) (((C)+(L))%SQLITE_FUNC_HASH_SZ)
16176
16177#ifdef SQLITE_USER_AUTHENTICATION
16178/*
16179** Information held in the "sqlite3" database connection object and used
16180** to manage user authentication.
16181*/
16182typedef struct sqlite3_userauth sqlite3_userauth;
16183struct sqlite3_userauth {
16184 u8 authLevel; /* Current authentication level */
16185 int nAuthPW; /* Size of the zAuthPW in bytes */
16186 char *zAuthPW; /* Password used to authenticate */
16187 char *zAuthUser; /* User name used to authenticate */
16188};
16189
16190/* Allowed values for sqlite3_userauth.authLevel */
16191#define UAUTH_Unknown 0 /* Authentication not yet checked */
16192#define UAUTH_Fail 1 /* User authentication failed */
16193#define UAUTH_User 2 /* Authenticated as a normal user */
16194#define UAUTH_Admin 3 /* Authenticated as an administrator */
16195
16196/* Functions used only by user authorization logic */
16197SQLITE_PRIVATE int sqlite3UserAuthTable(const char*);
16198SQLITE_PRIVATE int sqlite3UserAuthCheckLogin(sqlite3*,const char*,u8*);
16199SQLITE_PRIVATE void sqlite3UserAuthInit(sqlite3*);
16200SQLITE_PRIVATE void sqlite3CryptFunc(sqlite3_context*,int,sqlite3_value**);
16201
16202#endif /* SQLITE_USER_AUTHENTICATION */
16203
16204/*
16205** typedef for the authorization callback function.
16206*/
16207#ifdef SQLITE_USER_AUTHENTICATION
16208 typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
16209 const char*, const char*);
16210#else
16211 typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
16212 const char*);
16213#endif
16214
16215#ifndef SQLITE_OMIT_DEPRECATED
16216/* This is an extra SQLITE_TRACE macro that indicates "legacy" tracing
16217** in the style of sqlite3_trace()
16218*/
16219#define SQLITE_TRACE_LEGACY 0x80
16220#else
16221#define SQLITE_TRACE_LEGACY 0
16222#endif /* SQLITE_OMIT_DEPRECATED */
16223
16224
16225/*
16226** Each database connection is an instance of the following structure.
16227*/
16228struct sqlite3 {
16229 sqlite3_vfs *pVfs; /* OS Interface */
16230 struct Vdbe *pVdbe; /* List of active virtual machines */
16231 CollSeq *pDfltColl; /* The default collating sequence (BINARY) */
16232 sqlite3_mutex *mutex; /* Connection mutex */
16233 Db *aDb; /* All backends */
16234 int nDb; /* Number of backends currently in use */
16235 u32 mDbFlags; /* flags recording internal state */
16236 u64 flags; /* flags settable by pragmas. See below */
16237 i64 lastRowid; /* ROWID of most recent insert (see above) */
16238 i64 szMmap; /* Default mmap_size setting */
16239 u32 nSchemaLock; /* Do not reset the schema when non-zero */
16240 unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
16241 int errCode; /* Most recent error code (SQLITE_*) */
16242 int errMask; /* & result codes with this before returning */
16243 int iSysErrno; /* Errno value from last system error */
16244 u16 dbOptFlags; /* Flags to enable/disable optimizations */
16245 u8 enc; /* Text encoding */
16246 u8 autoCommit; /* The auto-commit flag. */
16247 u8 temp_store; /* 1: file 2: memory 0: default */
16248 u8 mallocFailed; /* True if we have seen a malloc failure */
16249 u8 bBenignMalloc; /* Do not require OOMs if true */
16250 u8 dfltLockMode; /* Default locking-mode for attached dbs */
16251 signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
16252 u8 suppressErr; /* Do not issue error messages if true */
16253 u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */
16254 u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
16255 u8 mTrace; /* zero or more SQLITE_TRACE flags */
16256 u8 noSharedCache; /* True if no shared-cache backends */
16257 u8 nSqlExec; /* Number of pending OP_SqlExec opcodes */
16258 int nextPagesize; /* Pagesize after VACUUM if >0 */
16259 u32 magic; /* Magic number for detect library misuse */
16260 int nChange; /* Value returned by sqlite3_changes() */
16261 int nTotalChange; /* Value returned by sqlite3_total_changes() */
16262 int aLimit[SQLITE_N_LIMIT]; /* Limits */
16263 int nMaxSorterMmap; /* Maximum size of regions mapped by sorter */
16264 struct sqlite3InitInfo { /* Information used during initialization */
16265 int newTnum; /* Rootpage of table being initialized */
16266 u8 iDb; /* Which db file is being initialized */
16267 u8 busy; /* TRUE if currently initializing */
16268 unsigned orphanTrigger : 1; /* Last statement is orphaned TEMP trigger */
16269 unsigned imposterTable : 1; /* Building an imposter table */
16270 unsigned reopenMemdb : 1; /* ATTACH is really a reopen using MemDB */
16271 } init;
16272 int nVdbeActive; /* Number of VDBEs currently running */
16273 int nVdbeRead; /* Number of active VDBEs that read or write */
16274 int nVdbeWrite; /* Number of active VDBEs that read and write */
16275 int nVdbeExec; /* Number of nested calls to VdbeExec() */
16276 int nVDestroy; /* Number of active OP_VDestroy operations */
16277 int nExtension; /* Number of loaded extensions */
16278 void **aExtension; /* Array of shared library handles */
16279 int (*xTrace)(u32,void*,void*,void*); /* Trace function */
16280 void *pTraceArg; /* Argument to the trace function */
16281 void (*xProfile)(void*,const char*,u64); /* Profiling function */
16282 void *pProfileArg; /* Argument to profile function */
16283 void *pCommitArg; /* Argument to xCommitCallback() */
16284 int (*xCommitCallback)(void*); /* Invoked at every commit. */
16285 void *pRollbackArg; /* Argument to xRollbackCallback() */
16286 void (*xRollbackCallback)(void*); /* Invoked at every commit. */
16287 void *pUpdateArg;
16288 void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
16289#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
16290 void *pPreUpdateArg; /* First argument to xPreUpdateCallback */
16291 void (*xPreUpdateCallback)( /* Registered using sqlite3_preupdate_hook() */
16292 void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64
16293 );
16294 PreUpdate *pPreUpdate; /* Context for active pre-update callback */
16295#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
16296#ifndef SQLITE_OMIT_WAL
16297 int (*xWalCallback)(void *, sqlite3 *, const char *, int);
16298 void *pWalArg;
16299#endif
16300 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
16301 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
16302 void *pCollNeededArg;
16303 sqlite3_value *pErr; /* Most recent error message */
16304 union {
16305 volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
16306 double notUsed1; /* Spacer */
16307 } u1;
16308 Lookaside lookaside; /* Lookaside malloc configuration */
16309#ifndef SQLITE_OMIT_AUTHORIZATION
16310 sqlite3_xauth xAuth; /* Access authorization function */
16311 void *pAuthArg; /* 1st argument to the access auth function */
16312#endif
16313#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
16314 int (*xProgress)(void *); /* The progress callback */
16315 void *pProgressArg; /* Argument to the progress callback */
16316 unsigned nProgressOps; /* Number of opcodes for progress callback */
16317#endif
16318#ifndef SQLITE_OMIT_VIRTUALTABLE
16319 int nVTrans; /* Allocated size of aVTrans */
16320 Hash aModule; /* populated by sqlite3_create_module() */
16321 VtabCtx *pVtabCtx; /* Context for active vtab connect/create */
16322 VTable **aVTrans; /* Virtual tables with open transactions */
16323 VTable *pDisconnect; /* Disconnect these in next sqlite3_prepare() */
16324#endif
16325 Hash aFunc; /* Hash table of connection functions */
16326 Hash aCollSeq; /* All collating sequences */
16327 BusyHandler busyHandler; /* Busy callback */
16328 Db aDbStatic[2]; /* Static space for the 2 default backends */
16329 Savepoint *pSavepoint; /* List of active savepoints */
16330 int busyTimeout; /* Busy handler timeout, in msec */
16331 int nSavepoint; /* Number of non-transaction savepoints */
16332 int nStatement; /* Number of nested statement-transactions */
16333 i64 nDeferredCons; /* Net deferred constraints this transaction. */
16334 i64 nDeferredImmCons; /* Net deferred immediate constraints */
16335 int *pnBytesFreed; /* If not NULL, increment this in DbFree() */
16336#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
16337 /* The following variables are all protected by the STATIC_MASTER
16338 ** mutex, not by sqlite3.mutex. They are used by code in notify.c.
16339 **
16340 ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
16341 ** unlock so that it can proceed.
16342 **
16343 ** When X.pBlockingConnection==Y, that means that something that X tried
16344 ** tried to do recently failed with an SQLITE_LOCKED error due to locks
16345 ** held by Y.
16346 */
16347 sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
16348 sqlite3 *pUnlockConnection; /* Connection to watch for unlock */
16349 void *pUnlockArg; /* Argument to xUnlockNotify */
16350 void (*xUnlockNotify)(void **, int); /* Unlock notify callback */
16351 sqlite3 *pNextBlocked; /* Next in list of all blocked connections */
16352#endif
16353#ifdef SQLITE_USER_AUTHENTICATION
16354 sqlite3_userauth auth; /* User authentication information */
16355#endif
16356};
16357
16358/*
16359** A macro to discover the encoding of a database.
16360*/
16361#define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc)
16362#define ENC(db) ((db)->enc)
16363
16364/*
16365** Possible values for the sqlite3.flags.
16366**
16367** Value constraints (enforced via assert()):
16368** SQLITE_FullFSync == PAGER_FULLFSYNC
16369** SQLITE_CkptFullFSync == PAGER_CKPT_FULLFSYNC
16370** SQLITE_CacheSpill == PAGER_CACHE_SPILL
16371*/
16372#define SQLITE_WriteSchema 0x00000001 /* OK to update SQLITE_MASTER */
16373#define SQLITE_LegacyFileFmt 0x00000002 /* Create new databases in format 1 */
16374#define SQLITE_FullColNames 0x00000004 /* Show full column names on SELECT */
16375#define SQLITE_FullFSync 0x00000008 /* Use full fsync on the backend */
16376#define SQLITE_CkptFullFSync 0x00000010 /* Use full fsync for checkpoint */
16377#define SQLITE_CacheSpill 0x00000020 /* OK to spill pager cache */
16378#define SQLITE_ShortColNames 0x00000040 /* Show short columns names */
16379#define SQLITE_CountRows 0x00000080 /* Count rows changed by INSERT, */
16380 /* DELETE, or UPDATE and return */
16381 /* the count using a callback. */
16382#define SQLITE_NullCallback 0x00000100 /* Invoke the callback once if the */
16383 /* result set is empty */
16384#define SQLITE_IgnoreChecks 0x00000200 /* Do not enforce check constraints */
16385#define SQLITE_ReadUncommit 0x00000400 /* READ UNCOMMITTED in shared-cache */
16386#define SQLITE_NoCkptOnClose 0x00000800 /* No checkpoint on close()/DETACH */
16387#define SQLITE_ReverseOrder 0x00001000 /* Reverse unordered SELECTs */
16388#define SQLITE_RecTriggers 0x00002000 /* Enable recursive triggers */
16389#define SQLITE_ForeignKeys 0x00004000 /* Enforce foreign key constraints */
16390#define SQLITE_AutoIndex 0x00008000 /* Enable automatic indexes */
16391#define SQLITE_LoadExtension 0x00010000 /* Enable load_extension */
16392#define SQLITE_LoadExtFunc 0x00020000 /* Enable load_extension() SQL func */
16393#define SQLITE_EnableTrigger 0x00040000 /* True to enable triggers */
16394#define SQLITE_DeferFKs 0x00080000 /* Defer all FK constraints */
16395#define SQLITE_QueryOnly 0x00100000 /* Disable database changes */
16396#define SQLITE_CellSizeCk 0x00200000 /* Check btree cell sizes on load */
16397#define SQLITE_Fts3Tokenizer 0x00400000 /* Enable fts3_tokenizer(2) */
16398#define SQLITE_EnableQPSG 0x00800000 /* Query Planner Stability Guarantee*/
16399#define SQLITE_TriggerEQP 0x01000000 /* Show trigger EXPLAIN QUERY PLAN */
16400#define SQLITE_ResetDatabase 0x02000000 /* Reset the database */
16401#define SQLITE_LegacyAlter 0x04000000 /* Legacy ALTER TABLE behaviour */
16402#define SQLITE_NoSchemaError 0x08000000 /* Do not report schema parse errors*/
16403#define SQLITE_Defensive 0x10000000 /* Input SQL is likely hostile */
16404
16405/* Flags used only if debugging */
16406#define HI(X) ((u64)(X)<<32)
16407#ifdef SQLITE_DEBUG
16408#define SQLITE_SqlTrace HI(0x0001) /* Debug print SQL as it executes */
16409#define SQLITE_VdbeListing HI(0x0002) /* Debug listings of VDBE progs */
16410#define SQLITE_VdbeTrace HI(0x0004) /* True to trace VDBE execution */
16411#define SQLITE_VdbeAddopTrace HI(0x0008) /* Trace sqlite3VdbeAddOp() calls */
16412#define SQLITE_VdbeEQP HI(0x0010) /* Debug EXPLAIN QUERY PLAN */
16413#endif
16414
16415/*
16416** Allowed values for sqlite3.mDbFlags
16417*/
16418#define DBFLAG_SchemaChange 0x0001 /* Uncommitted Hash table changes */
16419#define DBFLAG_PreferBuiltin 0x0002 /* Preference to built-in funcs */
16420#define DBFLAG_Vacuum 0x0004 /* Currently in a VACUUM */
16421#define DBFLAG_SchemaKnownOk 0x0008 /* Schema is known to be valid */
16422
16423/*
16424** Bits of the sqlite3.dbOptFlags field that are used by the
16425** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
16426** selectively disable various optimizations.
16427*/
16428#define SQLITE_QueryFlattener 0x0001 /* Query flattening */
16429 /* 0x0002 available for reuse */
16430#define SQLITE_GroupByOrder 0x0004 /* GROUPBY cover of ORDERBY */
16431#define SQLITE_FactorOutConst 0x0008 /* Constant factoring */
16432#define SQLITE_DistinctOpt 0x0010 /* DISTINCT using indexes */
16433#define SQLITE_CoverIdxScan 0x0020 /* Covering index scans */
16434#define SQLITE_OrderByIdxJoin 0x0040 /* ORDER BY of joins via index */
16435#define SQLITE_Transitive 0x0080 /* Transitive constraints */
16436#define SQLITE_OmitNoopJoin 0x0100 /* Omit unused tables in joins */
16437#define SQLITE_CountOfView 0x0200 /* The count-of-view optimization */
16438#define SQLITE_CursorHints 0x0400 /* Add OP_CursorHint opcodes */
16439#define SQLITE_Stat34 0x0800 /* Use STAT3 or STAT4 data */
16440 /* TH3 expects the Stat34 ^^^^^^ value to be 0x0800. Don't change it */
16441#define SQLITE_PushDown 0x1000 /* The push-down optimization */
16442#define SQLITE_SimplifyJoin 0x2000 /* Convert LEFT JOIN to JOIN */
16443#define SQLITE_SkipScan 0x4000 /* Skip-scans */
16444#define SQLITE_PropagateConst 0x8000 /* The constant propagation opt */
16445#define SQLITE_AllOpts 0xffff /* All optimizations */
16446
16447/*
16448** Macros for testing whether or not optimizations are enabled or disabled.
16449*/
16450#define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0)
16451#define OptimizationEnabled(db, mask) (((db)->dbOptFlags&(mask))==0)
16452
16453/*
16454** Return true if it OK to factor constant expressions into the initialization
16455** code. The argument is a Parse object for the code generator.
16456*/
16457#define ConstFactorOk(P) ((P)->okConstFactor)
16458
16459/*
16460** Possible values for the sqlite.magic field.
16461** The numbers are obtained at random and have no special meaning, other
16462** than being distinct from one another.
16463*/
16464#define SQLITE_MAGIC_OPEN 0xa029a697 /* Database is open */
16465#define SQLITE_MAGIC_CLOSED 0x9f3c2d33 /* Database is closed */
16466#define SQLITE_MAGIC_SICK 0x4b771290 /* Error and awaiting close */
16467#define SQLITE_MAGIC_BUSY 0xf03b7906 /* Database currently in use */
16468#define SQLITE_MAGIC_ERROR 0xb5357930 /* An SQLITE_MISUSE error occurred */
16469#define SQLITE_MAGIC_ZOMBIE 0x64cffc7f /* Close with last statement close */
16470
16471/*
16472** Each SQL function is defined by an instance of the following
16473** structure. For global built-in functions (ex: substr(), max(), count())
16474** a pointer to this structure is held in the sqlite3BuiltinFunctions object.
16475** For per-connection application-defined functions, a pointer to this
16476** structure is held in the db->aHash hash table.
16477**
16478** The u.pHash field is used by the global built-ins. The u.pDestructor
16479** field is used by per-connection app-def functions.
16480*/
16481struct FuncDef {
16482 i8 nArg; /* Number of arguments. -1 means unlimited */
16483 u32 funcFlags; /* Some combination of SQLITE_FUNC_* */
16484 void *pUserData; /* User data parameter */
16485 FuncDef *pNext; /* Next function with same name */
16486 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
16487 void (*xFinalize)(sqlite3_context*); /* Agg finalizer */
16488 void (*xValue)(sqlite3_context*); /* Current agg value */
16489 void (*xInverse)(sqlite3_context*,int,sqlite3_value**); /* inverse agg-step */
16490 const char *zName; /* SQL name of the function. */
16491 union {
16492 FuncDef *pHash; /* Next with a different name but the same hash */
16493 FuncDestructor *pDestructor; /* Reference counted destructor function */
16494 } u;
16495};
16496
16497/*
16498** This structure encapsulates a user-function destructor callback (as
16499** configured using create_function_v2()) and a reference counter. When
16500** create_function_v2() is called to create a function with a destructor,
16501** a single object of this type is allocated. FuncDestructor.nRef is set to
16502** the number of FuncDef objects created (either 1 or 3, depending on whether
16503** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
16504** member of each of the new FuncDef objects is set to point to the allocated
16505** FuncDestructor.
16506**
16507** Thereafter, when one of the FuncDef objects is deleted, the reference
16508** count on this object is decremented. When it reaches 0, the destructor
16509** is invoked and the FuncDestructor structure freed.
16510*/
16511struct FuncDestructor {
16512 int nRef;
16513 void (*xDestroy)(void *);
16514 void *pUserData;
16515};
16516
16517/*
16518** Possible values for FuncDef.flags. Note that the _LENGTH and _TYPEOF
16519** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG. And
16520** SQLITE_FUNC_CONSTANT must be the same as SQLITE_DETERMINISTIC. There
16521** are assert() statements in the code to verify this.
16522**
16523** Value constraints (enforced via assert()):
16524** SQLITE_FUNC_MINMAX == NC_MinMaxAgg == SF_MinMaxAgg
16525** SQLITE_FUNC_LENGTH == OPFLAG_LENGTHARG
16526** SQLITE_FUNC_TYPEOF == OPFLAG_TYPEOFARG
16527** SQLITE_FUNC_CONSTANT == SQLITE_DETERMINISTIC from the API
16528** SQLITE_FUNC_ENCMASK depends on SQLITE_UTF* macros in the API
16529*/
16530#define SQLITE_FUNC_ENCMASK 0x0003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */
16531#define SQLITE_FUNC_LIKE 0x0004 /* Candidate for the LIKE optimization */
16532#define SQLITE_FUNC_CASE 0x0008 /* Case-sensitive LIKE-type function */
16533#define SQLITE_FUNC_EPHEM 0x0010 /* Ephemeral. Delete with VDBE */
16534#define SQLITE_FUNC_NEEDCOLL 0x0020 /* sqlite3GetFuncCollSeq() might be called*/
16535#define SQLITE_FUNC_LENGTH 0x0040 /* Built-in length() function */
16536#define SQLITE_FUNC_TYPEOF 0x0080 /* Built-in typeof() function */
16537#define SQLITE_FUNC_COUNT 0x0100 /* Built-in count(*) aggregate */
16538#define SQLITE_FUNC_COALESCE 0x0200 /* Built-in coalesce() or ifnull() */
16539#define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
16540#define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
16541#define SQLITE_FUNC_MINMAX 0x1000 /* True for min() and max() aggregates */
16542#define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a
16543 ** single query - might change over time */
16544#define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */
16545#define SQLITE_FUNC_OFFSET 0x8000 /* Built-in sqlite_offset() function */
16546#define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */
16547#define SQLITE_FUNC_WINDOW_SIZE 0x20000 /* Requires partition size as arg. */
16548#define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */
16549
16550/*
16551** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
16552** used to create the initializers for the FuncDef structures.
16553**
16554** FUNCTION(zName, nArg, iArg, bNC, xFunc)
16555** Used to create a scalar function definition of a function zName
16556** implemented by C function xFunc that accepts nArg arguments. The
16557** value passed as iArg is cast to a (void*) and made available
16558** as the user-data (sqlite3_user_data()) for the function. If
16559** argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
16560**
16561** VFUNCTION(zName, nArg, iArg, bNC, xFunc)
16562** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag.
16563**
16564** DFUNCTION(zName, nArg, iArg, bNC, xFunc)
16565** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and
16566** adds the SQLITE_FUNC_SLOCHNG flag. Used for date & time functions
16567** and functions like sqlite_version() that can change, but not during
16568** a single query. The iArg is ignored. The user-data is always set
16569** to a NULL pointer. The bNC parameter is not used.
16570**
16571** PURE_DATE(zName, nArg, iArg, bNC, xFunc)
16572** Used for "pure" date/time functions, this macro is like DFUNCTION
16573** except that it does set the SQLITE_FUNC_CONSTANT flags. iArg is
16574** ignored and the user-data for these functions is set to an
16575** arbitrary non-NULL pointer. The bNC parameter is not used.
16576**
16577** AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
16578** Used to create an aggregate function definition implemented by
16579** the C functions xStep and xFinal. The first four parameters
16580** are interpreted in the same way as the first 4 parameters to
16581** FUNCTION().
16582**
16583** WFUNCTION(zName, nArg, iArg, xStep, xFinal, xValue, xInverse)
16584** Used to create an aggregate function definition implemented by
16585** the C functions xStep and xFinal. The first four parameters
16586** are interpreted in the same way as the first 4 parameters to
16587** FUNCTION().
16588**
16589** LIKEFUNC(zName, nArg, pArg, flags)
16590** Used to create a scalar function definition of a function zName
16591** that accepts nArg arguments and is implemented by a call to C
16592** function likeFunc. Argument pArg is cast to a (void *) and made
16593** available as the function user-data (sqlite3_user_data()). The
16594** FuncDef.flags variable is set to the value passed as the flags
16595** parameter.
16596*/
16597#define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
16598 {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
16599 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
16600#define VFUNCTION(zName, nArg, iArg, bNC, xFunc) \
16601 {nArg, SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
16602 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
16603#define DFUNCTION(zName, nArg, iArg, bNC, xFunc) \
16604 {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8, \
16605 0, 0, xFunc, 0, 0, 0, #zName, {0} }
16606#define PURE_DATE(zName, nArg, iArg, bNC, xFunc) \
16607 {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|SQLITE_FUNC_CONSTANT, \
16608 (void*)&sqlite3Config, 0, xFunc, 0, 0, 0, #zName, {0} }
16609#define FUNCTION2(zName, nArg, iArg, bNC, xFunc, extraFlags) \
16610 {nArg,SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL)|extraFlags,\
16611 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
16612#define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
16613 {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
16614 pArg, 0, xFunc, 0, 0, 0, #zName, }
16615#define LIKEFUNC(zName, nArg, arg, flags) \
16616 {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|flags, \
16617 (void *)arg, 0, likeFunc, 0, 0, 0, #zName, {0} }
16618#define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal, xValue) \
16619 {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL), \
16620 SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xValue,0,#zName, {0}}
16621#define AGGREGATE2(zName, nArg, arg, nc, xStep, xFinal, extraFlags) \
16622 {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|extraFlags, \
16623 SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xFinal,0,#zName, {0}}
16624#define WAGGREGATE(zName, nArg, arg, nc, xStep, xFinal, xValue, xInverse, f) \
16625 {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|f, \
16626 SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xValue,xInverse,#zName, {0}}
16627#define INTERNAL_FUNCTION(zName, nArg, xFunc) \
16628 {nArg, SQLITE_FUNC_INTERNAL|SQLITE_UTF8|SQLITE_FUNC_CONSTANT, \
16629 0, 0, xFunc, 0, 0, 0, #zName, {0} }
16630
16631
16632/*
16633** All current savepoints are stored in a linked list starting at
16634** sqlite3.pSavepoint. The first element in the list is the most recently
16635** opened savepoint. Savepoints are added to the list by the vdbe
16636** OP_Savepoint instruction.
16637*/
16638struct Savepoint {
16639 char *zName; /* Savepoint name (nul-terminated) */
16640 i64 nDeferredCons; /* Number of deferred fk violations */
16641 i64 nDeferredImmCons; /* Number of deferred imm fk. */
16642 Savepoint *pNext; /* Parent savepoint (if any) */
16643};
16644
16645/*
16646** The following are used as the second parameter to sqlite3Savepoint(),
16647** and as the P1 argument to the OP_Savepoint instruction.
16648*/
16649#define SAVEPOINT_BEGIN 0
16650#define SAVEPOINT_RELEASE 1
16651#define SAVEPOINT_ROLLBACK 2
16652
16653
16654/*
16655** Each SQLite module (virtual table definition) is defined by an
16656** instance of the following structure, stored in the sqlite3.aModule
16657** hash table.
16658*/
16659struct Module {
16660 const sqlite3_module *pModule; /* Callback pointers */
16661 const char *zName; /* Name passed to create_module() */
16662 void *pAux; /* pAux passed to create_module() */
16663 void (*xDestroy)(void *); /* Module destructor function */
16664 Table *pEpoTab; /* Eponymous table for this module */
16665};
16666
16667/*
16668** information about each column of an SQL table is held in an instance
16669** of this structure.
16670*/
16671struct Column {
16672 char *zName; /* Name of this column, \000, then the type */
16673 Expr *pDflt; /* Default value of this column */
16674 char *zColl; /* Collating sequence. If NULL, use the default */
16675 u8 notNull; /* An OE_ code for handling a NOT NULL constraint */
16676 char affinity; /* One of the SQLITE_AFF_... values */
16677 u8 szEst; /* Estimated size of value in this column. sizeof(INT)==1 */
16678 u8 colFlags; /* Boolean properties. See COLFLAG_ defines below */
16679};
16680
16681/* Allowed values for Column.colFlags:
16682*/
16683#define COLFLAG_PRIMKEY 0x0001 /* Column is part of the primary key */
16684#define COLFLAG_HIDDEN 0x0002 /* A hidden column in a virtual table */
16685#define COLFLAG_HASTYPE 0x0004 /* Type name follows column name */
16686#define COLFLAG_UNIQUE 0x0008 /* Column def contains "UNIQUE" or "PK" */
16687#define COLFLAG_SORTERREF 0x0010 /* Use sorter-refs with this column */
16688
16689/*
16690** A "Collating Sequence" is defined by an instance of the following
16691** structure. Conceptually, a collating sequence consists of a name and
16692** a comparison routine that defines the order of that sequence.
16693**
16694** If CollSeq.xCmp is NULL, it means that the
16695** collating sequence is undefined. Indices built on an undefined
16696** collating sequence may not be read or written.
16697*/
16698struct CollSeq {
16699 char *zName; /* Name of the collating sequence, UTF-8 encoded */
16700 u8 enc; /* Text encoding handled by xCmp() */
16701 void *pUser; /* First argument to xCmp() */
16702 int (*xCmp)(void*,int, const void*, int, const void*);
16703 void (*xDel)(void*); /* Destructor for pUser */
16704};
16705
16706/*
16707** A sort order can be either ASC or DESC.
16708*/
16709#define SQLITE_SO_ASC 0 /* Sort in ascending order */
16710#define SQLITE_SO_DESC 1 /* Sort in ascending order */
16711#define SQLITE_SO_UNDEFINED -1 /* No sort order specified */
16712
16713/*
16714** Column affinity types.
16715**
16716** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
16717** 't' for SQLITE_AFF_TEXT. But we can save a little space and improve
16718** the speed a little by numbering the values consecutively.
16719**
16720** But rather than start with 0 or 1, we begin with 'A'. That way,
16721** when multiple affinity types are concatenated into a string and
16722** used as the P4 operand, they will be more readable.
16723**
16724** Note also that the numeric types are grouped together so that testing
16725** for a numeric type is a single comparison. And the BLOB type is first.
16726*/
16727#define SQLITE_AFF_BLOB 'A'
16728#define SQLITE_AFF_TEXT 'B'
16729#define SQLITE_AFF_NUMERIC 'C'
16730#define SQLITE_AFF_INTEGER 'D'
16731#define SQLITE_AFF_REAL 'E'
16732
16733#define sqlite3IsNumericAffinity(X) ((X)>=SQLITE_AFF_NUMERIC)
16734
16735/*
16736** The SQLITE_AFF_MASK values masks off the significant bits of an
16737** affinity value.
16738*/
16739#define SQLITE_AFF_MASK 0x47
16740
16741/*
16742** Additional bit values that can be ORed with an affinity without
16743** changing the affinity.
16744**
16745** The SQLITE_NOTNULL flag is a combination of NULLEQ and JUMPIFNULL.
16746** It causes an assert() to fire if either operand to a comparison
16747** operator is NULL. It is added to certain comparison operators to
16748** prove that the operands are always NOT NULL.
16749*/
16750#define SQLITE_KEEPNULL 0x08 /* Used by vector == or <> */
16751#define SQLITE_JUMPIFNULL 0x10 /* jumps if either operand is NULL */
16752#define SQLITE_STOREP2 0x20 /* Store result in reg[P2] rather than jump */
16753#define SQLITE_NULLEQ 0x80 /* NULL=NULL */
16754#define SQLITE_NOTNULL 0x90 /* Assert that operands are never NULL */
16755
16756/*
16757** An object of this type is created for each virtual table present in
16758** the database schema.
16759**
16760** If the database schema is shared, then there is one instance of this
16761** structure for each database connection (sqlite3*) that uses the shared
16762** schema. This is because each database connection requires its own unique
16763** instance of the sqlite3_vtab* handle used to access the virtual table
16764** implementation. sqlite3_vtab* handles can not be shared between
16765** database connections, even when the rest of the in-memory database
16766** schema is shared, as the implementation often stores the database
16767** connection handle passed to it via the xConnect() or xCreate() method
16768** during initialization internally. This database connection handle may
16769** then be used by the virtual table implementation to access real tables
16770** within the database. So that they appear as part of the callers
16771** transaction, these accesses need to be made via the same database
16772** connection as that used to execute SQL operations on the virtual table.
16773**
16774** All VTable objects that correspond to a single table in a shared
16775** database schema are initially stored in a linked-list pointed to by
16776** the Table.pVTable member variable of the corresponding Table object.
16777** When an sqlite3_prepare() operation is required to access the virtual
16778** table, it searches the list for the VTable that corresponds to the
16779** database connection doing the preparing so as to use the correct
16780** sqlite3_vtab* handle in the compiled query.
16781**
16782** When an in-memory Table object is deleted (for example when the
16783** schema is being reloaded for some reason), the VTable objects are not
16784** deleted and the sqlite3_vtab* handles are not xDisconnect()ed
16785** immediately. Instead, they are moved from the Table.pVTable list to
16786** another linked list headed by the sqlite3.pDisconnect member of the
16787** corresponding sqlite3 structure. They are then deleted/xDisconnected
16788** next time a statement is prepared using said sqlite3*. This is done
16789** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
16790** Refer to comments above function sqlite3VtabUnlockList() for an
16791** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
16792** list without holding the corresponding sqlite3.mutex mutex.
16793**
16794** The memory for objects of this type is always allocated by
16795** sqlite3DbMalloc(), using the connection handle stored in VTable.db as
16796** the first argument.
16797*/
16798struct VTable {
16799 sqlite3 *db; /* Database connection associated with this table */
16800 Module *pMod; /* Pointer to module implementation */
16801 sqlite3_vtab *pVtab; /* Pointer to vtab instance */
16802 int nRef; /* Number of pointers to this structure */
16803 u8 bConstraint; /* True if constraints are supported */
16804 int iSavepoint; /* Depth of the SAVEPOINT stack */
16805 VTable *pNext; /* Next in linked list (see above) */
16806};
16807
16808/*
16809** The schema for each SQL table and view is represented in memory
16810** by an instance of the following structure.
16811*/
16812struct Table {
16813 char *zName; /* Name of the table or view */
16814 Column *aCol; /* Information about each column */
16815#ifdef SQLITE_ENABLE_NORMALIZE
16816 Hash *pColHash; /* All columns indexed by name */
16817#endif
16818 Index *pIndex; /* List of SQL indexes on this table. */
16819 Select *pSelect; /* NULL for tables. Points to definition if a view. */
16820 FKey *pFKey; /* Linked list of all foreign keys in this table */
16821 char *zColAff; /* String defining the affinity of each column */
16822 ExprList *pCheck; /* All CHECK constraints */
16823 /* ... also used as column name list in a VIEW */
16824 int tnum; /* Root BTree page for this table */
16825 u32 nTabRef; /* Number of pointers to this Table */
16826 u32 tabFlags; /* Mask of TF_* values */
16827 i16 iPKey; /* If not negative, use aCol[iPKey] as the rowid */
16828 i16 nCol; /* Number of columns in this table */
16829 LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */
16830 LogEst szTabRow; /* Estimated size of each table row in bytes */
16831#ifdef SQLITE_ENABLE_COSTMULT
16832 LogEst costMult; /* Cost multiplier for using this table */
16833#endif
16834 u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
16835#ifndef SQLITE_OMIT_ALTERTABLE
16836 int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */
16837#endif
16838#ifndef SQLITE_OMIT_VIRTUALTABLE
16839 int nModuleArg; /* Number of arguments to the module */
16840 char **azModuleArg; /* 0: module 1: schema 2: vtab name 3...: args */
16841 VTable *pVTable; /* List of VTable objects. */
16842#endif
16843 Trigger *pTrigger; /* List of triggers stored in pSchema */
16844 Schema *pSchema; /* Schema that contains this table */
16845 Table *pNextZombie; /* Next on the Parse.pZombieTab list */
16846};
16847
16848/*
16849** Allowed values for Table.tabFlags.
16850**
16851** TF_OOOHidden applies to tables or view that have hidden columns that are
16852** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING
16853** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden,
16854** the TF_OOOHidden attribute would apply in this case. Such tables require
16855** special handling during INSERT processing.
16856*/
16857#define TF_Readonly 0x0001 /* Read-only system table */
16858#define TF_Ephemeral 0x0002 /* An ephemeral table */
16859#define TF_HasPrimaryKey 0x0004 /* Table has a primary key */
16860#define TF_Autoincrement 0x0008 /* Integer primary key is autoincrement */
16861#define TF_HasStat1 0x0010 /* nRowLogEst set from sqlite_stat1 */
16862#define TF_WithoutRowid 0x0020 /* No rowid. PRIMARY KEY is the key */
16863#define TF_NoVisibleRowid 0x0040 /* No user-visible "rowid" column */
16864#define TF_OOOHidden 0x0080 /* Out-of-Order hidden columns */
16865#define TF_StatsUsed 0x0100 /* Query planner decisions affected by
16866 ** Index.aiRowLogEst[] values */
16867#define TF_HasNotNull 0x0200 /* Contains NOT NULL constraints */
16868#define TF_Shadow 0x0400 /* True for a shadow table */
16869
16870/*
16871** Test to see whether or not a table is a virtual table. This is
16872** done as a macro so that it will be optimized out when virtual
16873** table support is omitted from the build.
16874*/
16875#ifndef SQLITE_OMIT_VIRTUALTABLE
16876# define IsVirtual(X) ((X)->nModuleArg)
16877#else
16878# define IsVirtual(X) 0
16879#endif
16880
16881/*
16882** Macros to determine if a column is hidden. IsOrdinaryHiddenColumn()
16883** only works for non-virtual tables (ordinary tables and views) and is
16884** always false unless SQLITE_ENABLE_HIDDEN_COLUMNS is defined. The
16885** IsHiddenColumn() macro is general purpose.
16886*/
16887#if defined(SQLITE_ENABLE_HIDDEN_COLUMNS)
16888# define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
16889# define IsOrdinaryHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
16890#elif !defined(SQLITE_OMIT_VIRTUALTABLE)
16891# define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
16892# define IsOrdinaryHiddenColumn(X) 0
16893#else
16894# define IsHiddenColumn(X) 0
16895# define IsOrdinaryHiddenColumn(X) 0
16896#endif
16897
16898
16899/* Does the table have a rowid */
16900#define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0)
16901#define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
16902
16903/*
16904** Each foreign key constraint is an instance of the following structure.
16905**
16906** A foreign key is associated with two tables. The "from" table is
16907** the table that contains the REFERENCES clause that creates the foreign
16908** key. The "to" table is the table that is named in the REFERENCES clause.
16909** Consider this example:
16910**
16911** CREATE TABLE ex1(
16912** a INTEGER PRIMARY KEY,
16913** b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
16914** );
16915**
16916** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
16917** Equivalent names:
16918**
16919** from-table == child-table
16920** to-table == parent-table
16921**
16922** Each REFERENCES clause generates an instance of the following structure
16923** which is attached to the from-table. The to-table need not exist when
16924** the from-table is created. The existence of the to-table is not checked.
16925**
16926** The list of all parents for child Table X is held at X.pFKey.
16927**
16928** A list of all children for a table named Z (which might not even exist)
16929** is held in Schema.fkeyHash with a hash key of Z.
16930*/
16931struct FKey {
16932 Table *pFrom; /* Table containing the REFERENCES clause (aka: Child) */
16933 FKey *pNextFrom; /* Next FKey with the same in pFrom. Next parent of pFrom */
16934 char *zTo; /* Name of table that the key points to (aka: Parent) */
16935 FKey *pNextTo; /* Next with the same zTo. Next child of zTo. */
16936 FKey *pPrevTo; /* Previous with the same zTo */
16937 int nCol; /* Number of columns in this key */
16938 /* EV: R-30323-21917 */
16939 u8 isDeferred; /* True if constraint checking is deferred till COMMIT */
16940 u8 aAction[2]; /* ON DELETE and ON UPDATE actions, respectively */
16941 Trigger *apTrigger[2];/* Triggers for aAction[] actions */
16942 struct sColMap { /* Mapping of columns in pFrom to columns in zTo */
16943 int iFrom; /* Index of column in pFrom */
16944 char *zCol; /* Name of column in zTo. If NULL use PRIMARY KEY */
16945 } aCol[1]; /* One entry for each of nCol columns */
16946};
16947
16948/*
16949** SQLite supports many different ways to resolve a constraint
16950** error. ROLLBACK processing means that a constraint violation
16951** causes the operation in process to fail and for the current transaction
16952** to be rolled back. ABORT processing means the operation in process
16953** fails and any prior changes from that one operation are backed out,
16954** but the transaction is not rolled back. FAIL processing means that
16955** the operation in progress stops and returns an error code. But prior
16956** changes due to the same operation are not backed out and no rollback
16957** occurs. IGNORE means that the particular row that caused the constraint
16958** error is not inserted or updated. Processing continues and no error
16959** is returned. REPLACE means that preexisting database rows that caused
16960** a UNIQUE constraint violation are removed so that the new insert or
16961** update can proceed. Processing continues and no error is reported.
16962**
16963** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys.
16964** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
16965** same as ROLLBACK for DEFERRED keys. SETNULL means that the foreign
16966** key is set to NULL. CASCADE means that a DELETE or UPDATE of the
16967** referenced table row is propagated into the row that holds the
16968** foreign key.
16969**
16970** The following symbolic values are used to record which type
16971** of action to take.
16972*/
16973#define OE_None 0 /* There is no constraint to check */
16974#define OE_Rollback 1 /* Fail the operation and rollback the transaction */
16975#define OE_Abort 2 /* Back out changes but do no rollback transaction */
16976#define OE_Fail 3 /* Stop the operation but leave all prior changes */
16977#define OE_Ignore 4 /* Ignore the error. Do not do the INSERT or UPDATE */
16978#define OE_Replace 5 /* Delete existing record, then do INSERT or UPDATE */
16979#define OE_Update 6 /* Process as a DO UPDATE in an upsert */
16980#define OE_Restrict 7 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
16981#define OE_SetNull 8 /* Set the foreign key value to NULL */
16982#define OE_SetDflt 9 /* Set the foreign key value to its default */
16983#define OE_Cascade 10 /* Cascade the changes */
16984#define OE_Default 11 /* Do whatever the default action is */
16985
16986
16987/*
16988** An instance of the following structure is passed as the first
16989** argument to sqlite3VdbeKeyCompare and is used to control the
16990** comparison of the two index keys.
16991**
16992** Note that aSortOrder[] and aColl[] have nField+1 slots. There
16993** are nField slots for the columns of an index then one extra slot
16994** for the rowid at the end.
16995*/
16996struct KeyInfo {
16997 u32 nRef; /* Number of references to this KeyInfo object */
16998 u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
16999 u16 nKeyField; /* Number of key columns in the index */
17000 u16 nAllField; /* Total columns, including key plus others */
17001 sqlite3 *db; /* The database connection */
17002 u8 *aSortOrder; /* Sort order for each column. */
17003 CollSeq *aColl[1]; /* Collating sequence for each term of the key */
17004};
17005
17006/*
17007** This object holds a record which has been parsed out into individual
17008** fields, for the purposes of doing a comparison.
17009**
17010** A record is an object that contains one or more fields of data.
17011** Records are used to store the content of a table row and to store
17012** the key of an index. A blob encoding of a record is created by
17013** the OP_MakeRecord opcode of the VDBE and is disassembled by the
17014** OP_Column opcode.
17015**
17016** An instance of this object serves as a "key" for doing a search on
17017** an index b+tree. The goal of the search is to find the entry that
17018** is closed to the key described by this object. This object might hold
17019** just a prefix of the key. The number of fields is given by
17020** pKeyInfo->nField.
17021**
17022** The r1 and r2 fields are the values to return if this key is less than
17023** or greater than a key in the btree, respectively. These are normally
17024** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree
17025** is in DESC order.
17026**
17027** The key comparison functions actually return default_rc when they find
17028** an equals comparison. default_rc can be -1, 0, or +1. If there are
17029** multiple entries in the b-tree with the same key (when only looking
17030** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
17031** cause the search to find the last match, or +1 to cause the search to
17032** find the first match.
17033**
17034** The key comparison functions will set eqSeen to true if they ever
17035** get and equal results when comparing this structure to a b-tree record.
17036** When default_rc!=0, the search might end up on the record immediately
17037** before the first match or immediately after the last match. The
17038** eqSeen field will indicate whether or not an exact match exists in the
17039** b-tree.
17040*/
17041struct UnpackedRecord {
17042 KeyInfo *pKeyInfo; /* Collation and sort-order information */
17043 Mem *aMem; /* Values */
17044 u16 nField; /* Number of entries in apMem[] */
17045 i8 default_rc; /* Comparison result if keys are equal */
17046 u8 errCode; /* Error detected by xRecordCompare (CORRUPT or NOMEM) */
17047 i8 r1; /* Value to return if (lhs < rhs) */
17048 i8 r2; /* Value to return if (lhs > rhs) */
17049 u8 eqSeen; /* True if an equality comparison has been seen */
17050};
17051
17052
17053/*
17054** Each SQL index is represented in memory by an
17055** instance of the following structure.
17056**
17057** The columns of the table that are to be indexed are described
17058** by the aiColumn[] field of this structure. For example, suppose
17059** we have the following table and index:
17060**
17061** CREATE TABLE Ex1(c1 int, c2 int, c3 text);
17062** CREATE INDEX Ex2 ON Ex1(c3,c1);
17063**
17064** In the Table structure describing Ex1, nCol==3 because there are
17065** three columns in the table. In the Index structure describing
17066** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
17067** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the
17068** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
17069** The second column to be indexed (c1) has an index of 0 in
17070** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
17071**
17072** The Index.onError field determines whether or not the indexed columns
17073** must be unique and what to do if they are not. When Index.onError=OE_None,
17074** it means this is not a unique index. Otherwise it is a unique index
17075** and the value of Index.onError indicate the which conflict resolution
17076** algorithm to employ whenever an attempt is made to insert a non-unique
17077** element.
17078**
17079** While parsing a CREATE TABLE or CREATE INDEX statement in order to
17080** generate VDBE code (as opposed to parsing one read from an sqlite_master
17081** table as part of parsing an existing database schema), transient instances
17082** of this structure may be created. In this case the Index.tnum variable is
17083** used to store the address of a VDBE instruction, not a database page
17084** number (it cannot - the database page is not allocated until the VDBE
17085** program is executed). See convertToWithoutRowidTable() for details.
17086*/
17087struct Index {
17088 char *zName; /* Name of this index */
17089 i16 *aiColumn; /* Which columns are used by this index. 1st is 0 */
17090 LogEst *aiRowLogEst; /* From ANALYZE: Est. rows selected by each column */
17091 Table *pTable; /* The SQL table being indexed */
17092 char *zColAff; /* String defining the affinity of each column */
17093 Index *pNext; /* The next index associated with the same table */
17094 Schema *pSchema; /* Schema containing this index */
17095 u8 *aSortOrder; /* for each column: True==DESC, False==ASC */
17096 const char **azColl; /* Array of collation sequence names for index */
17097 Expr *pPartIdxWhere; /* WHERE clause for partial indices */
17098 ExprList *aColExpr; /* Column expressions */
17099 int tnum; /* DB Page containing root of this index */
17100 LogEst szIdxRow; /* Estimated average row size in bytes */
17101 u16 nKeyCol; /* Number of columns forming the key */
17102 u16 nColumn; /* Number of columns stored in the index */
17103 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
17104 unsigned idxType:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
17105 unsigned bUnordered:1; /* Use this index for == or IN queries only */
17106 unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
17107 unsigned isResized:1; /* True if resizeIndexObject() has been called */
17108 unsigned isCovering:1; /* True if this is a covering index */
17109 unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
17110 unsigned hasStat1:1; /* aiRowLogEst values come from sqlite_stat1 */
17111 unsigned bNoQuery:1; /* Do not use this index to optimize queries */
17112#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
17113 int nSample; /* Number of elements in aSample[] */
17114 int nSampleCol; /* Size of IndexSample.anEq[] and so on */
17115 tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */
17116 IndexSample *aSample; /* Samples of the left-most key */
17117 tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
17118 tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
17119#endif
17120 Bitmask colNotIdxed; /* 0 for unindexed columns in pTab */
17121};
17122
17123/*
17124** Allowed values for Index.idxType
17125*/
17126#define SQLITE_IDXTYPE_APPDEF 0 /* Created using CREATE INDEX */
17127#define SQLITE_IDXTYPE_UNIQUE 1 /* Implements a UNIQUE constraint */
17128#define SQLITE_IDXTYPE_PRIMARYKEY 2 /* Is the PRIMARY KEY for the table */
17129
17130/* Return true if index X is a PRIMARY KEY index */
17131#define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
17132
17133/* Return true if index X is a UNIQUE index */
17134#define IsUniqueIndex(X) ((X)->onError!=OE_None)
17135
17136/* The Index.aiColumn[] values are normally positive integer. But
17137** there are some negative values that have special meaning:
17138*/
17139#define XN_ROWID (-1) /* Indexed column is the rowid */
17140#define XN_EXPR (-2) /* Indexed column is an expression */
17141
17142/*
17143** Each sample stored in the sqlite_stat3 table is represented in memory
17144** using a structure of this type. See documentation at the top of the
17145** analyze.c source file for additional information.
17146*/
17147struct IndexSample {
17148 void *p; /* Pointer to sampled record */
17149 int n; /* Size of record in bytes */
17150 tRowcnt *anEq; /* Est. number of rows where the key equals this sample */
17151 tRowcnt *anLt; /* Est. number of rows where key is less than this sample */
17152 tRowcnt *anDLt; /* Est. number of distinct keys less than this sample */
17153};
17154
17155/*
17156** Possible values to use within the flags argument to sqlite3GetToken().
17157*/
17158#define SQLITE_TOKEN_QUOTED 0x1 /* Token is a quoted identifier. */
17159#define SQLITE_TOKEN_KEYWORD 0x2 /* Token is a keyword. */
17160
17161/*
17162** Each token coming out of the lexer is an instance of
17163** this structure. Tokens are also used as part of an expression.
17164**
17165** The memory that "z" points to is owned by other objects. Take care
17166** that the owner of the "z" string does not deallocate the string before
17167** the Token goes out of scope! Very often, the "z" points to some place
17168** in the middle of the Parse.zSql text. But it might also point to a
17169** static string.
17170*/
17171struct Token {
17172 const char *z; /* Text of the token. Not NULL-terminated! */
17173 unsigned int n; /* Number of characters in this token */
17174};
17175
17176/*
17177** An instance of this structure contains information needed to generate
17178** code for a SELECT that contains aggregate functions.
17179**
17180** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
17181** pointer to this structure. The Expr.iColumn field is the index in
17182** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
17183** code for that node.
17184**
17185** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
17186** original Select structure that describes the SELECT statement. These
17187** fields do not need to be freed when deallocating the AggInfo structure.
17188*/
17189struct AggInfo {
17190 u8 directMode; /* Direct rendering mode means take data directly
17191 ** from source tables rather than from accumulators */
17192 u8 useSortingIdx; /* In direct mode, reference the sorting index rather
17193 ** than the source table */
17194 int sortingIdx; /* Cursor number of the sorting index */
17195 int sortingIdxPTab; /* Cursor number of pseudo-table */
17196 int nSortingColumn; /* Number of columns in the sorting index */
17197 int mnReg, mxReg; /* Range of registers allocated for aCol and aFunc */
17198 ExprList *pGroupBy; /* The group by clause */
17199 struct AggInfo_col { /* For each column used in source tables */
17200 Table *pTab; /* Source table */
17201 int iTable; /* Cursor number of the source table */
17202 int iColumn; /* Column number within the source table */
17203 int iSorterColumn; /* Column number in the sorting index */
17204 int iMem; /* Memory location that acts as accumulator */
17205 Expr *pExpr; /* The original expression */
17206 } *aCol;
17207 int nColumn; /* Number of used entries in aCol[] */
17208 int nAccumulator; /* Number of columns that show through to the output.
17209 ** Additional columns are used only as parameters to
17210 ** aggregate functions */
17211 struct AggInfo_func { /* For each aggregate function */
17212 Expr *pExpr; /* Expression encoding the function */
17213 FuncDef *pFunc; /* The aggregate function implementation */
17214 int iMem; /* Memory location that acts as accumulator */
17215 int iDistinct; /* Ephemeral table used to enforce DISTINCT */
17216 } *aFunc;
17217 int nFunc; /* Number of entries in aFunc[] */
17218};
17219
17220/*
17221** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
17222** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
17223** than 32767 we have to make it 32-bit. 16-bit is preferred because
17224** it uses less memory in the Expr object, which is a big memory user
17225** in systems with lots of prepared statements. And few applications
17226** need more than about 10 or 20 variables. But some extreme users want
17227** to have prepared statements with over 32767 variables, and for them
17228** the option is available (at compile-time).
17229*/
17230#if SQLITE_MAX_VARIABLE_NUMBER<=32767
17231typedef i16 ynVar;
17232#else
17233typedef int ynVar;
17234#endif
17235
17236/*
17237** Each node of an expression in the parse tree is an instance
17238** of this structure.
17239**
17240** Expr.op is the opcode. The integer parser token codes are reused
17241** as opcodes here. For example, the parser defines TK_GE to be an integer
17242** code representing the ">=" operator. This same integer code is reused
17243** to represent the greater-than-or-equal-to operator in the expression
17244** tree.
17245**
17246** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB,
17247** or TK_STRING), then Expr.token contains the text of the SQL literal. If
17248** the expression is a variable (TK_VARIABLE), then Expr.token contains the
17249** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
17250** then Expr.token contains the name of the function.
17251**
17252** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
17253** binary operator. Either or both may be NULL.
17254**
17255** Expr.x.pList is a list of arguments if the expression is an SQL function,
17256** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
17257** Expr.x.pSelect is used if the expression is a sub-select or an expression of
17258** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
17259** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is
17260** valid.
17261**
17262** An expression of the form ID or ID.ID refers to a column in a table.
17263** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
17264** the integer cursor number of a VDBE cursor pointing to that table and
17265** Expr.iColumn is the column number for the specific column. If the
17266** expression is used as a result in an aggregate SELECT, then the
17267** value is also stored in the Expr.iAgg column in the aggregate so that
17268** it can be accessed after all aggregates are computed.
17269**
17270** If the expression is an unbound variable marker (a question mark
17271** character '?' in the original SQL) then the Expr.iTable holds the index
17272** number for that variable.
17273**
17274** If the expression is a subquery then Expr.iColumn holds an integer
17275** register number containing the result of the subquery. If the
17276** subquery gives a constant result, then iTable is -1. If the subquery
17277** gives a different answer at different times during statement processing
17278** then iTable is the address of a subroutine that computes the subquery.
17279**
17280** If the Expr is of type OP_Column, and the table it is selecting from
17281** is a disk table or the "old.*" pseudo-table, then pTab points to the
17282** corresponding table definition.
17283**
17284** ALLOCATION NOTES:
17285**
17286** Expr objects can use a lot of memory space in database schema. To
17287** help reduce memory requirements, sometimes an Expr object will be
17288** truncated. And to reduce the number of memory allocations, sometimes
17289** two or more Expr objects will be stored in a single memory allocation,
17290** together with Expr.zToken strings.
17291**
17292** If the EP_Reduced and EP_TokenOnly flags are set when
17293** an Expr object is truncated. When EP_Reduced is set, then all
17294** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
17295** are contained within the same memory allocation. Note, however, that
17296** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
17297** allocated, regardless of whether or not EP_Reduced is set.
17298*/
17299struct Expr {
17300 u8 op; /* Operation performed by this node */
17301 char affinity; /* The affinity of the column or 0 if not a column */
17302 u32 flags; /* Various flags. EP_* See below */
17303 union {
17304 char *zToken; /* Token value. Zero terminated and dequoted */
17305 int iValue; /* Non-negative integer value if EP_IntValue */
17306 } u;
17307
17308 /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
17309 ** space is allocated for the fields below this point. An attempt to
17310 ** access them will result in a segfault or malfunction.
17311 *********************************************************************/
17312
17313 Expr *pLeft; /* Left subnode */
17314 Expr *pRight; /* Right subnode */
17315 union {
17316 ExprList *pList; /* op = IN, EXISTS, SELECT, CASE, FUNCTION, BETWEEN */
17317 Select *pSelect; /* EP_xIsSelect and op = IN, EXISTS, SELECT */
17318 } x;
17319
17320 /* If the EP_Reduced flag is set in the Expr.flags mask, then no
17321 ** space is allocated for the fields below this point. An attempt to
17322 ** access them will result in a segfault or malfunction.
17323 *********************************************************************/
17324
17325#if SQLITE_MAX_EXPR_DEPTH>0
17326 int nHeight; /* Height of the tree headed by this node */
17327#endif
17328 int iTable; /* TK_COLUMN: cursor number of table holding column
17329 ** TK_REGISTER: register number
17330 ** TK_TRIGGER: 1 -> new, 0 -> old
17331 ** EP_Unlikely: 134217728 times likelihood
17332 ** TK_SELECT: 1st register of result vector */
17333 ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
17334 ** TK_VARIABLE: variable number (always >= 1).
17335 ** TK_SELECT_COLUMN: column of the result vector */
17336 i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
17337 i16 iRightJoinTable; /* If EP_FromJoin, the right table of the join */
17338 u8 op2; /* TK_REGISTER: original value of Expr.op
17339 ** TK_COLUMN: the value of p5 for OP_Column
17340 ** TK_AGG_FUNCTION: nesting depth */
17341 AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
17342 union {
17343 Table *pTab; /* TK_COLUMN: Table containing column. Can be NULL
17344 ** for a column of an index on an expression */
17345 Window *pWin; /* TK_FUNCTION: Window definition for the func */
17346 } y;
17347};
17348
17349/*
17350** The following are the meanings of bits in the Expr.flags field.
17351*/
17352#define EP_FromJoin 0x000001 /* Originates in ON/USING clause of outer join */
17353#define EP_Agg 0x000002 /* Contains one or more aggregate functions */
17354#define EP_HasFunc 0x000004 /* Contains one or more functions of any kind */
17355#define EP_FixedCol 0x000008 /* TK_Column with a known fixed value */
17356#define EP_Distinct 0x000010 /* Aggregate function with DISTINCT keyword */
17357#define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
17358#define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
17359#define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
17360#define EP_Collate 0x000100 /* Tree contains a TK_COLLATE operator */
17361#define EP_Generic 0x000200 /* Ignore COLLATE or affinity on this tree */
17362#define EP_IntValue 0x000400 /* Integer value contained in u.iValue */
17363#define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */
17364#define EP_Skip 0x001000 /* COLLATE, AS, or UNLIKELY */
17365#define EP_Reduced 0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
17366#define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
17367#define EP_Static 0x008000 /* Held in memory not obtained from malloc() */
17368#define EP_MemToken 0x010000 /* Need to sqlite3DbFree() Expr.zToken */
17369#define EP_NoReduce 0x020000 /* Cannot EXPRDUP_REDUCE this Expr */
17370#define EP_Unlikely 0x040000 /* unlikely() or likelihood() function */
17371#define EP_ConstFunc 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
17372#define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */
17373#define EP_Subquery 0x200000 /* Tree contains a TK_SELECT operator */
17374#define EP_Alias 0x400000 /* Is an alias for a result set column */
17375#define EP_Leaf 0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
17376#define EP_WinFunc 0x1000000 /* TK_FUNCTION with Expr.y.pWin set */
17377
17378/*
17379** The EP_Propagate mask is a set of properties that automatically propagate
17380** upwards into parent nodes.
17381*/
17382#define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc)
17383
17384/*
17385** These macros can be used to test, set, or clear bits in the
17386** Expr.flags field.
17387*/
17388#define ExprHasProperty(E,P) (((E)->flags&(P))!=0)
17389#define ExprHasAllProperty(E,P) (((E)->flags&(P))==(P))
17390#define ExprSetProperty(E,P) (E)->flags|=(P)
17391#define ExprClearProperty(E,P) (E)->flags&=~(P)
17392
17393/* The ExprSetVVAProperty() macro is used for Verification, Validation,
17394** and Accreditation only. It works like ExprSetProperty() during VVA
17395** processes but is a no-op for delivery.
17396*/
17397#ifdef SQLITE_DEBUG
17398# define ExprSetVVAProperty(E,P) (E)->flags|=(P)
17399#else
17400# define ExprSetVVAProperty(E,P)
17401#endif
17402
17403/*
17404** Macros to determine the number of bytes required by a normal Expr
17405** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
17406** and an Expr struct with the EP_TokenOnly flag set.
17407*/
17408#define EXPR_FULLSIZE sizeof(Expr) /* Full size */
17409#define EXPR_REDUCEDSIZE offsetof(Expr,iTable) /* Common features */
17410#define EXPR_TOKENONLYSIZE offsetof(Expr,pLeft) /* Fewer features */
17411
17412/*
17413** Flags passed to the sqlite3ExprDup() function. See the header comment
17414** above sqlite3ExprDup() for details.
17415*/
17416#define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */
17417
17418/*
17419** A list of expressions. Each expression may optionally have a
17420** name. An expr/name combination can be used in several ways, such
17421** as the list of "expr AS ID" fields following a "SELECT" or in the
17422** list of "ID = expr" items in an UPDATE. A list of expressions can
17423** also be used as the argument to a function, in which case the a.zName
17424** field is not used.
17425**
17426** By default the Expr.zSpan field holds a human-readable description of
17427** the expression that is used in the generation of error messages and
17428** column labels. In this case, Expr.zSpan is typically the text of a
17429** column expression as it exists in a SELECT statement. However, if
17430** the bSpanIsTab flag is set, then zSpan is overloaded to mean the name
17431** of the result column in the form: DATABASE.TABLE.COLUMN. This later
17432** form is used for name resolution with nested FROM clauses.
17433*/
17434struct ExprList {
17435 int nExpr; /* Number of expressions on the list */
17436 struct ExprList_item { /* For each expression in the list */
17437 Expr *pExpr; /* The parse tree for this expression */
17438 char *zName; /* Token associated with this expression */
17439 char *zSpan; /* Original text of the expression */
17440 u8 sortOrder; /* 1 for DESC or 0 for ASC */
17441 unsigned done :1; /* A flag to indicate when processing is finished */
17442 unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */
17443 unsigned reusable :1; /* Constant expression is reusable */
17444 unsigned bSorterRef :1; /* Defer evaluation until after sorting */
17445 union {
17446 struct {
17447 u16 iOrderByCol; /* For ORDER BY, column number in result set */
17448 u16 iAlias; /* Index into Parse.aAlias[] for zName */
17449 } x;
17450 int iConstExprReg; /* Register in which Expr value is cached */
17451 } u;
17452 } a[1]; /* One slot for each expression in the list */
17453};
17454
17455/*
17456** An instance of this structure can hold a simple list of identifiers,
17457** such as the list "a,b,c" in the following statements:
17458**
17459** INSERT INTO t(a,b,c) VALUES ...;
17460** CREATE INDEX idx ON t(a,b,c);
17461** CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
17462**
17463** The IdList.a.idx field is used when the IdList represents the list of
17464** column names after a table name in an INSERT statement. In the statement
17465**
17466** INSERT INTO t(a,b,c) ...
17467**
17468** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
17469*/
17470struct IdList {
17471 struct IdList_item {
17472 char *zName; /* Name of the identifier */
17473 int idx; /* Index in some Table.aCol[] of a column named zName */
17474 } *a;
17475 int nId; /* Number of identifiers on the list */
17476};
17477
17478/*
17479** The following structure describes the FROM clause of a SELECT statement.
17480** Each table or subquery in the FROM clause is a separate element of
17481** the SrcList.a[] array.
17482**
17483** With the addition of multiple database support, the following structure
17484** can also be used to describe a particular table such as the table that
17485** is modified by an INSERT, DELETE, or UPDATE statement. In standard SQL,
17486** such a table must be a simple name: ID. But in SQLite, the table can
17487** now be identified by a database name, a dot, then the table name: ID.ID.
17488**
17489** The jointype starts out showing the join type between the current table
17490** and the next table on the list. The parser builds the list this way.
17491** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
17492** jointype expresses the join between the table and the previous table.
17493**
17494** In the colUsed field, the high-order bit (bit 63) is set if the table
17495** contains more than 63 columns and the 64-th or later column is used.
17496*/
17497struct SrcList {
17498 int nSrc; /* Number of tables or subqueries in the FROM clause */
17499 u32 nAlloc; /* Number of entries allocated in a[] below */
17500 struct SrcList_item {
17501 Schema *pSchema; /* Schema to which this item is fixed */
17502 char *zDatabase; /* Name of database holding this table */
17503 char *zName; /* Name of the table */
17504 char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */
17505 Table *pTab; /* An SQL table corresponding to zName */
17506 Select *pSelect; /* A SELECT statement used in place of a table name */
17507 int addrFillSub; /* Address of subroutine to manifest a subquery */
17508 int regReturn; /* Register holding return address of addrFillSub */
17509 int regResult; /* Registers holding results of a co-routine */
17510 struct {
17511 u8 jointype; /* Type of join between this table and the previous */
17512 unsigned notIndexed :1; /* True if there is a NOT INDEXED clause */
17513 unsigned isIndexedBy :1; /* True if there is an INDEXED BY clause */
17514 unsigned isTabFunc :1; /* True if table-valued-function syntax */
17515 unsigned isCorrelated :1; /* True if sub-query is correlated */
17516 unsigned viaCoroutine :1; /* Implemented as a co-routine */
17517 unsigned isRecursive :1; /* True for recursive reference in WITH */
17518 } fg;
17519 int iCursor; /* The VDBE cursor number used to access this table */
17520 Expr *pOn; /* The ON clause of a join */
17521 IdList *pUsing; /* The USING clause of a join */
17522 Bitmask colUsed; /* Bit N (1<<N) set if column N of pTab is used */
17523 union {
17524 char *zIndexedBy; /* Identifier from "INDEXED BY <zIndex>" clause */
17525 ExprList *pFuncArg; /* Arguments to table-valued-function */
17526 } u1;
17527 Index *pIBIndex; /* Index structure corresponding to u1.zIndexedBy */
17528 } a[1]; /* One entry for each identifier on the list */
17529};
17530
17531/*
17532** Permitted values of the SrcList.a.jointype field
17533*/
17534#define JT_INNER 0x0001 /* Any kind of inner or cross join */
17535#define JT_CROSS 0x0002 /* Explicit use of the CROSS keyword */
17536#define JT_NATURAL 0x0004 /* True for a "natural" join */
17537#define JT_LEFT 0x0008 /* Left outer join */
17538#define JT_RIGHT 0x0010 /* Right outer join */
17539#define JT_OUTER 0x0020 /* The "OUTER" keyword is present */
17540#define JT_ERROR 0x0040 /* unknown or unsupported join type */
17541
17542
17543/*
17544** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
17545** and the WhereInfo.wctrlFlags member.
17546**
17547** Value constraints (enforced via assert()):
17548** WHERE_USE_LIMIT == SF_FixedLimit
17549*/
17550#define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */
17551#define WHERE_ORDERBY_MIN 0x0001 /* ORDER BY processing for min() func */
17552#define WHERE_ORDERBY_MAX 0x0002 /* ORDER BY processing for max() func */
17553#define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
17554#define WHERE_ONEPASS_MULTIROW 0x0008 /* ONEPASS is ok with multiple rows */
17555#define WHERE_DUPLICATES_OK 0x0010 /* Ok to return a row more than once */
17556#define WHERE_OR_SUBCLAUSE 0x0020 /* Processing a sub-WHERE as part of
17557 ** the OR optimization */
17558#define WHERE_GROUPBY 0x0040 /* pOrderBy is really a GROUP BY */
17559#define WHERE_DISTINCTBY 0x0080 /* pOrderby is really a DISTINCT clause */
17560#define WHERE_WANT_DISTINCT 0x0100 /* All output needs to be distinct */
17561#define WHERE_SORTBYGROUP 0x0200 /* Support sqlite3WhereIsSorted() */
17562#define WHERE_SEEK_TABLE 0x0400 /* Do not defer seeks on main table */
17563#define WHERE_ORDERBY_LIMIT 0x0800 /* ORDERBY+LIMIT on the inner loop */
17564#define WHERE_SEEK_UNIQ_TABLE 0x1000 /* Do not defer seeks if unique */
17565 /* 0x2000 not currently used */
17566#define WHERE_USE_LIMIT 0x4000 /* Use the LIMIT in cost estimates */
17567 /* 0x8000 not currently used */
17568
17569/* Allowed return values from sqlite3WhereIsDistinct()
17570*/
17571#define WHERE_DISTINCT_NOOP 0 /* DISTINCT keyword not used */
17572#define WHERE_DISTINCT_UNIQUE 1 /* No duplicates */
17573#define WHERE_DISTINCT_ORDERED 2 /* All duplicates are adjacent */
17574#define WHERE_DISTINCT_UNORDERED 3 /* Duplicates are scattered */
17575
17576/*
17577** A NameContext defines a context in which to resolve table and column
17578** names. The context consists of a list of tables (the pSrcList) field and
17579** a list of named expression (pEList). The named expression list may
17580** be NULL. The pSrc corresponds to the FROM clause of a SELECT or
17581** to the table being operated on by INSERT, UPDATE, or DELETE. The
17582** pEList corresponds to the result set of a SELECT and is NULL for
17583** other statements.
17584**
17585** NameContexts can be nested. When resolving names, the inner-most
17586** context is searched first. If no match is found, the next outer
17587** context is checked. If there is still no match, the next context
17588** is checked. This process continues until either a match is found
17589** or all contexts are check. When a match is found, the nRef member of
17590** the context containing the match is incremented.
17591**
17592** Each subquery gets a new NameContext. The pNext field points to the
17593** NameContext in the parent query. Thus the process of scanning the
17594** NameContext list corresponds to searching through successively outer
17595** subqueries looking for a match.
17596*/
17597struct NameContext {
17598 Parse *pParse; /* The parser */
17599 SrcList *pSrcList; /* One or more tables used to resolve names */
17600 union {
17601 ExprList *pEList; /* Optional list of result-set columns */
17602 AggInfo *pAggInfo; /* Information about aggregates at this level */
17603 Upsert *pUpsert; /* ON CONFLICT clause information from an upsert */
17604 } uNC;
17605 NameContext *pNext; /* Next outer name context. NULL for outermost */
17606 int nRef; /* Number of names resolved by this context */
17607 int nErr; /* Number of errors encountered while resolving names */
17608 u16 ncFlags; /* Zero or more NC_* flags defined below */
17609 Select *pWinSelect; /* SELECT statement for any window functions */
17610};
17611
17612/*
17613** Allowed values for the NameContext, ncFlags field.
17614**
17615** Value constraints (all checked via assert()):
17616** NC_HasAgg == SF_HasAgg
17617** NC_MinMaxAgg == SF_MinMaxAgg == SQLITE_FUNC_MINMAX
17618**
17619*/
17620#define NC_AllowAgg 0x0001 /* Aggregate functions are allowed here */
17621#define NC_PartIdx 0x0002 /* True if resolving a partial index WHERE */
17622#define NC_IsCheck 0x0004 /* True if resolving names in a CHECK constraint */
17623#define NC_InAggFunc 0x0008 /* True if analyzing arguments to an agg func */
17624#define NC_HasAgg 0x0010 /* One or more aggregate functions seen */
17625#define NC_IdxExpr 0x0020 /* True if resolving columns of CREATE INDEX */
17626#define NC_VarSelect 0x0040 /* A correlated subquery has been seen */
17627#define NC_UEList 0x0080 /* True if uNC.pEList is used */
17628#define NC_UAggInfo 0x0100 /* True if uNC.pAggInfo is used */
17629#define NC_UUpsert 0x0200 /* True if uNC.pUpsert is used */
17630#define NC_MinMaxAgg 0x1000 /* min/max aggregates seen. See note above */
17631#define NC_Complex 0x2000 /* True if a function or subquery seen */
17632#define NC_AllowWin 0x4000 /* Window functions are allowed here */
17633
17634/*
17635** An instance of the following object describes a single ON CONFLICT
17636** clause in an upsert.
17637**
17638** The pUpsertTarget field is only set if the ON CONFLICT clause includes
17639** conflict-target clause. (In "ON CONFLICT(a,b)" the "(a,b)" is the
17640** conflict-target clause.) The pUpsertTargetWhere is the optional
17641** WHERE clause used to identify partial unique indexes.
17642**
17643** pUpsertSet is the list of column=expr terms of the UPDATE statement.
17644** The pUpsertSet field is NULL for a ON CONFLICT DO NOTHING. The
17645** pUpsertWhere is the WHERE clause for the UPDATE and is NULL if the
17646** WHERE clause is omitted.
17647*/
17648struct Upsert {
17649 ExprList *pUpsertTarget; /* Optional description of conflicting index */
17650 Expr *pUpsertTargetWhere; /* WHERE clause for partial index targets */
17651 ExprList *pUpsertSet; /* The SET clause from an ON CONFLICT UPDATE */
17652 Expr *pUpsertWhere; /* WHERE clause for the ON CONFLICT UPDATE */
17653 /* The fields above comprise the parse tree for the upsert clause.
17654 ** The fields below are used to transfer information from the INSERT
17655 ** processing down into the UPDATE processing while generating code.
17656 ** Upsert owns the memory allocated above, but not the memory below. */
17657 Index *pUpsertIdx; /* Constraint that pUpsertTarget identifies */
17658 SrcList *pUpsertSrc; /* Table to be updated */
17659 int regData; /* First register holding array of VALUES */
17660 int iDataCur; /* Index of the data cursor */
17661 int iIdxCur; /* Index of the first index cursor */
17662};
17663
17664/*
17665** An instance of the following structure contains all information
17666** needed to generate code for a single SELECT statement.
17667**
17668** See the header comment on the computeLimitRegisters() routine for a
17669** detailed description of the meaning of the iLimit and iOffset fields.
17670**
17671** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
17672** These addresses must be stored so that we can go back and fill in
17673** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor
17674** the number of columns in P2 can be computed at the same time
17675** as the OP_OpenEphm instruction is coded because not
17676** enough information about the compound query is known at that point.
17677** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
17678** for the result set. The KeyInfo for addrOpenEphm[2] contains collating
17679** sequences for the ORDER BY clause.
17680*/
17681struct Select {
17682 ExprList *pEList; /* The fields of the result */
17683 u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
17684 LogEst nSelectRow; /* Estimated number of result rows */
17685 u32 selFlags; /* Various SF_* values */
17686 int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
17687 u32 selId; /* Unique identifier number for this SELECT */
17688 int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */
17689 SrcList *pSrc; /* The FROM clause */
17690 Expr *pWhere; /* The WHERE clause */
17691 ExprList *pGroupBy; /* The GROUP BY clause */
17692 Expr *pHaving; /* The HAVING clause */
17693 ExprList *pOrderBy; /* The ORDER BY clause */
17694 Select *pPrior; /* Prior select in a compound select statement */
17695 Select *pNext; /* Next select to the left in a compound */
17696 Expr *pLimit; /* LIMIT expression. NULL means not used. */
17697 With *pWith; /* WITH clause attached to this select. Or NULL. */
17698#ifndef SQLITE_OMIT_WINDOWFUNC
17699 Window *pWin; /* List of window functions */
17700 Window *pWinDefn; /* List of named window definitions */
17701#endif
17702};
17703
17704/*
17705** Allowed values for Select.selFlags. The "SF" prefix stands for
17706** "Select Flag".
17707**
17708** Value constraints (all checked via assert())
17709** SF_HasAgg == NC_HasAgg
17710** SF_MinMaxAgg == NC_MinMaxAgg == SQLITE_FUNC_MINMAX
17711** SF_FixedLimit == WHERE_USE_LIMIT
17712*/
17713#define SF_Distinct 0x00001 /* Output should be DISTINCT */
17714#define SF_All 0x00002 /* Includes the ALL keyword */
17715#define SF_Resolved 0x00004 /* Identifiers have been resolved */
17716#define SF_Aggregate 0x00008 /* Contains agg functions or a GROUP BY */
17717#define SF_HasAgg 0x00010 /* Contains aggregate functions */
17718#define SF_UsesEphemeral 0x00020 /* Uses the OpenEphemeral opcode */
17719#define SF_Expanded 0x00040 /* sqlite3SelectExpand() called on this */
17720#define SF_HasTypeInfo 0x00080 /* FROM subqueries have Table metadata */
17721#define SF_Compound 0x00100 /* Part of a compound query */
17722#define SF_Values 0x00200 /* Synthesized from VALUES clause */
17723#define SF_MultiValue 0x00400 /* Single VALUES term with multiple rows */
17724#define SF_NestedFrom 0x00800 /* Part of a parenthesized FROM clause */
17725#define SF_MinMaxAgg 0x01000 /* Aggregate containing min() or max() */
17726#define SF_Recursive 0x02000 /* The recursive part of a recursive CTE */
17727#define SF_FixedLimit 0x04000 /* nSelectRow set by a constant LIMIT */
17728#define SF_MaybeConvert 0x08000 /* Need convertCompoundSelectToSubquery() */
17729#define SF_Converted 0x10000 /* By convertCompoundSelectToSubquery() */
17730#define SF_IncludeHidden 0x20000 /* Include hidden columns in output */
17731#define SF_ComplexResult 0x40000 /* Result contains subquery or function */
17732
17733/*
17734** The results of a SELECT can be distributed in several ways, as defined
17735** by one of the following macros. The "SRT" prefix means "SELECT Result
17736** Type".
17737**
17738** SRT_Union Store results as a key in a temporary index
17739** identified by pDest->iSDParm.
17740**
17741** SRT_Except Remove results from the temporary index pDest->iSDParm.
17742**
17743** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result
17744** set is not empty.
17745**
17746** SRT_Discard Throw the results away. This is used by SELECT
17747** statements within triggers whose only purpose is
17748** the side-effects of functions.
17749**
17750** All of the above are free to ignore their ORDER BY clause. Those that
17751** follow must honor the ORDER BY clause.
17752**
17753** SRT_Output Generate a row of output (using the OP_ResultRow
17754** opcode) for each row in the result set.
17755**
17756** SRT_Mem Only valid if the result is a single column.
17757** Store the first column of the first result row
17758** in register pDest->iSDParm then abandon the rest
17759** of the query. This destination implies "LIMIT 1".
17760**
17761** SRT_Set The result must be a single column. Store each
17762** row of result as the key in table pDest->iSDParm.
17763** Apply the affinity pDest->affSdst before storing
17764** results. Used to implement "IN (SELECT ...)".
17765**
17766** SRT_EphemTab Create an temporary table pDest->iSDParm and store
17767** the result there. The cursor is left open after
17768** returning. This is like SRT_Table except that
17769** this destination uses OP_OpenEphemeral to create
17770** the table first.
17771**
17772** SRT_Coroutine Generate a co-routine that returns a new row of
17773** results each time it is invoked. The entry point
17774** of the co-routine is stored in register pDest->iSDParm
17775** and the result row is stored in pDest->nDest registers
17776** starting with pDest->iSdst.
17777**
17778** SRT_Table Store results in temporary table pDest->iSDParm.
17779** SRT_Fifo This is like SRT_EphemTab except that the table
17780** is assumed to already be open. SRT_Fifo has
17781** the additional property of being able to ignore
17782** the ORDER BY clause.
17783**
17784** SRT_DistFifo Store results in a temporary table pDest->iSDParm.
17785** But also use temporary table pDest->iSDParm+1 as
17786** a record of all prior results and ignore any duplicate
17787** rows. Name means: "Distinct Fifo".
17788**
17789** SRT_Queue Store results in priority queue pDest->iSDParm (really
17790** an index). Append a sequence number so that all entries
17791** are distinct.
17792**
17793** SRT_DistQueue Store results in priority queue pDest->iSDParm only if
17794** the same record has never been stored before. The
17795** index at pDest->iSDParm+1 hold all prior stores.
17796*/
17797#define SRT_Union 1 /* Store result as keys in an index */
17798#define SRT_Except 2 /* Remove result from a UNION index */
17799#define SRT_Exists 3 /* Store 1 if the result is not empty */
17800#define SRT_Discard 4 /* Do not save the results anywhere */
17801#define SRT_Fifo 5 /* Store result as data with an automatic rowid */
17802#define SRT_DistFifo 6 /* Like SRT_Fifo, but unique results only */
17803#define SRT_Queue 7 /* Store result in an queue */
17804#define SRT_DistQueue 8 /* Like SRT_Queue, but unique results only */
17805
17806/* The ORDER BY clause is ignored for all of the above */
17807#define IgnorableOrderby(X) ((X->eDest)<=SRT_DistQueue)
17808
17809#define SRT_Output 9 /* Output each row of result */
17810#define SRT_Mem 10 /* Store result in a memory cell */
17811#define SRT_Set 11 /* Store results as keys in an index */
17812#define SRT_EphemTab 12 /* Create transient tab and store like SRT_Table */
17813#define SRT_Coroutine 13 /* Generate a single row of result */
17814#define SRT_Table 14 /* Store result as data with an automatic rowid */
17815
17816/*
17817** An instance of this object describes where to put of the results of
17818** a SELECT statement.
17819*/
17820struct SelectDest {
17821 u8 eDest; /* How to dispose of the results. On of SRT_* above. */
17822 int iSDParm; /* A parameter used by the eDest disposal method */
17823 int iSdst; /* Base register where results are written */
17824 int nSdst; /* Number of registers allocated */
17825 char *zAffSdst; /* Affinity used when eDest==SRT_Set */
17826 ExprList *pOrderBy; /* Key columns for SRT_Queue and SRT_DistQueue */
17827};
17828
17829/*
17830** During code generation of statements that do inserts into AUTOINCREMENT
17831** tables, the following information is attached to the Table.u.autoInc.p
17832** pointer of each autoincrement table to record some side information that
17833** the code generator needs. We have to keep per-table autoincrement
17834** information in case inserts are done within triggers. Triggers do not
17835** normally coordinate their activities, but we do need to coordinate the
17836** loading and saving of autoincrement information.
17837*/
17838struct AutoincInfo {
17839 AutoincInfo *pNext; /* Next info block in a list of them all */
17840 Table *pTab; /* Table this info block refers to */
17841 int iDb; /* Index in sqlite3.aDb[] of database holding pTab */
17842 int regCtr; /* Memory register holding the rowid counter */
17843};
17844
17845/*
17846** At least one instance of the following structure is created for each
17847** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
17848** statement. All such objects are stored in the linked list headed at
17849** Parse.pTriggerPrg and deleted once statement compilation has been
17850** completed.
17851**
17852** A Vdbe sub-program that implements the body and WHEN clause of trigger
17853** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
17854** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
17855** The Parse.pTriggerPrg list never contains two entries with the same
17856** values for both pTrigger and orconf.
17857**
17858** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
17859** accessed (or set to 0 for triggers fired as a result of INSERT
17860** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
17861** a mask of new.* columns used by the program.
17862*/
17863struct TriggerPrg {
17864 Trigger *pTrigger; /* Trigger this program was coded from */
17865 TriggerPrg *pNext; /* Next entry in Parse.pTriggerPrg list */
17866 SubProgram *pProgram; /* Program implementing pTrigger/orconf */
17867 int orconf; /* Default ON CONFLICT policy */
17868 u32 aColmask[2]; /* Masks of old.*, new.* columns accessed */
17869};
17870
17871/*
17872** The yDbMask datatype for the bitmask of all attached databases.
17873*/
17874#if SQLITE_MAX_ATTACHED>30
17875 typedef unsigned char yDbMask[(SQLITE_MAX_ATTACHED+9)/8];
17876# define DbMaskTest(M,I) (((M)[(I)/8]&(1<<((I)&7)))!=0)
17877# define DbMaskZero(M) memset((M),0,sizeof(M))
17878# define DbMaskSet(M,I) (M)[(I)/8]|=(1<<((I)&7))
17879# define DbMaskAllZero(M) sqlite3DbMaskAllZero(M)
17880# define DbMaskNonZero(M) (sqlite3DbMaskAllZero(M)==0)
17881#else
17882 typedef unsigned int yDbMask;
17883# define DbMaskTest(M,I) (((M)&(((yDbMask)1)<<(I)))!=0)
17884# define DbMaskZero(M) (M)=0
17885# define DbMaskSet(M,I) (M)|=(((yDbMask)1)<<(I))
17886# define DbMaskAllZero(M) (M)==0
17887# define DbMaskNonZero(M) (M)!=0
17888#endif
17889
17890/*
17891** An SQL parser context. A copy of this structure is passed through
17892** the parser and down into all the parser action routine in order to
17893** carry around information that is global to the entire parse.
17894**
17895** The structure is divided into two parts. When the parser and code
17896** generate call themselves recursively, the first part of the structure
17897** is constant but the second part is reset at the beginning and end of
17898** each recursion.
17899**
17900** The nTableLock and aTableLock variables are only used if the shared-cache
17901** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
17902** used to store the set of table-locks required by the statement being
17903** compiled. Function sqlite3TableLock() is used to add entries to the
17904** list.
17905*/
17906struct Parse {
17907 sqlite3 *db; /* The main database structure */
17908 char *zErrMsg; /* An error message */
17909 Vdbe *pVdbe; /* An engine for executing database bytecode */
17910 int rc; /* Return code from execution */
17911 u8 colNamesSet; /* TRUE after OP_ColumnName has been issued to pVdbe */
17912 u8 checkSchema; /* Causes schema cookie check after an error */
17913 u8 nested; /* Number of nested calls to the parser/code generator */
17914 u8 nTempReg; /* Number of temporary registers in aTempReg[] */
17915 u8 isMultiWrite; /* True if statement may modify/insert multiple rows */
17916 u8 mayAbort; /* True if statement may throw an ABORT exception */
17917 u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */
17918 u8 okConstFactor; /* OK to factor out constants */
17919 u8 disableLookaside; /* Number of times lookaside has been disabled */
17920 int nRangeReg; /* Size of the temporary register block */
17921 int iRangeReg; /* First register in temporary register block */
17922 int nErr; /* Number of errors seen */
17923 int nTab; /* Number of previously allocated VDBE cursors */
17924 int nMem; /* Number of memory cells used so far */
17925 int nOpAlloc; /* Number of slots allocated for Vdbe.aOp[] */
17926 int szOpAlloc; /* Bytes of memory space allocated for Vdbe.aOp[] */
17927 int iSelfTab; /* Table associated with an index on expr, or negative
17928 ** of the base register during check-constraint eval */
17929 int nLabel; /* Number of labels used */
17930 int *aLabel; /* Space to hold the labels */
17931 ExprList *pConstExpr;/* Constant expressions */
17932 Token constraintName;/* Name of the constraint currently being parsed */
17933 yDbMask writeMask; /* Start a write transaction on these databases */
17934 yDbMask cookieMask; /* Bitmask of schema verified databases */
17935 int regRowid; /* Register holding rowid of CREATE TABLE entry */
17936 int regRoot; /* Register holding root page number for new objects */
17937 int nMaxArg; /* Max args passed to user function by sub-program */
17938 int nSelect; /* Number of SELECT stmts. Counter for Select.selId */
17939#ifndef SQLITE_OMIT_SHARED_CACHE
17940 int nTableLock; /* Number of locks in aTableLock */
17941 TableLock *aTableLock; /* Required table locks for shared-cache mode */
17942#endif
17943 AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */
17944 Parse *pToplevel; /* Parse structure for main program (or NULL) */
17945 Table *pTriggerTab; /* Table triggers are being coded for */
17946 int addrCrTab; /* Address of OP_CreateBtree opcode on CREATE TABLE */
17947 u32 nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */
17948 u32 oldmask; /* Mask of old.* columns referenced */
17949 u32 newmask; /* Mask of new.* columns referenced */
17950 u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */
17951 u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */
17952 u8 disableTriggers; /* True to disable triggers */
17953
17954 /**************************************************************************
17955 ** Fields above must be initialized to zero. The fields that follow,
17956 ** down to the beginning of the recursive section, do not need to be
17957 ** initialized as they will be set before being used. The boundary is
17958 ** determined by offsetof(Parse,aTempReg).
17959 **************************************************************************/
17960
17961 int aTempReg[8]; /* Holding area for temporary registers */
17962 Token sNameToken; /* Token with unqualified schema object name */
17963
17964 /************************************************************************
17965 ** Above is constant between recursions. Below is reset before and after
17966 ** each recursion. The boundary between these two regions is determined
17967 ** using offsetof(Parse,sLastToken) so the sLastToken field must be the
17968 ** first field in the recursive region.
17969 ************************************************************************/
17970
17971 Token sLastToken; /* The last token parsed */
17972 ynVar nVar; /* Number of '?' variables seen in the SQL so far */
17973 u8 iPkSortOrder; /* ASC or DESC for INTEGER PRIMARY KEY */
17974 u8 explain; /* True if the EXPLAIN flag is found on the query */
17975#if !(defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_OMIT_ALTERTABLE))
17976 u8 eParseMode; /* PARSE_MODE_XXX constant */
17977#endif
17978#ifndef SQLITE_OMIT_VIRTUALTABLE
17979 int nVtabLock; /* Number of virtual tables to lock */
17980#endif
17981 int nHeight; /* Expression tree height of current sub-select */
17982#ifndef SQLITE_OMIT_EXPLAIN
17983 int addrExplain; /* Address of current OP_Explain opcode */
17984#endif
17985 VList *pVList; /* Mapping between variable names and numbers */
17986 Vdbe *pReprepare; /* VM being reprepared (sqlite3Reprepare()) */
17987 const char *zTail; /* All SQL text past the last semicolon parsed */
17988 Table *pNewTable; /* A table being constructed by CREATE TABLE */
17989 Index *pNewIndex; /* An index being constructed by CREATE INDEX */
17990 Trigger *pNewTrigger; /* Trigger under construct by a CREATE TRIGGER */
17991 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
17992#ifndef SQLITE_OMIT_VIRTUALTABLE
17993 Token sArg; /* Complete text of a module argument */
17994 Table **apVtabLock; /* Pointer to virtual tables needing locking */
17995#endif
17996 Table *pZombieTab; /* List of Table objects to delete after code gen */
17997 TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */
17998 With *pWith; /* Current WITH clause, or NULL */
17999 With *pWithToFree; /* Free this WITH object at the end of the parse */
18000#ifndef SQLITE_OMIT_ALTERTABLE
18001 RenameToken *pRename; /* Tokens subject to renaming by ALTER TABLE */
18002#endif
18003};
18004
18005#define PARSE_MODE_NORMAL 0
18006#define PARSE_MODE_DECLARE_VTAB 1
18007#define PARSE_MODE_RENAME_COLUMN 2
18008#define PARSE_MODE_RENAME_TABLE 3
18009
18010/*
18011** Sizes and pointers of various parts of the Parse object.
18012*/
18013#define PARSE_HDR_SZ offsetof(Parse,aTempReg) /* Recursive part w/o aColCache*/
18014#define PARSE_RECURSE_SZ offsetof(Parse,sLastToken) /* Recursive part */
18015#define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */
18016#define PARSE_TAIL(X) (((char*)(X))+PARSE_RECURSE_SZ) /* Pointer to tail */
18017
18018/*
18019** Return true if currently inside an sqlite3_declare_vtab() call.
18020*/
18021#ifdef SQLITE_OMIT_VIRTUALTABLE
18022 #define IN_DECLARE_VTAB 0
18023#else
18024 #define IN_DECLARE_VTAB (pParse->eParseMode==PARSE_MODE_DECLARE_VTAB)
18025#endif
18026
18027#if defined(SQLITE_OMIT_ALTERTABLE)
18028 #define IN_RENAME_OBJECT 0
18029#else
18030 #define IN_RENAME_OBJECT (pParse->eParseMode>=PARSE_MODE_RENAME_COLUMN)
18031#endif
18032
18033#if defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_OMIT_ALTERTABLE)
18034 #define IN_SPECIAL_PARSE 0
18035#else
18036 #define IN_SPECIAL_PARSE (pParse->eParseMode!=PARSE_MODE_NORMAL)
18037#endif
18038
18039/*
18040** An instance of the following structure can be declared on a stack and used
18041** to save the Parse.zAuthContext value so that it can be restored later.
18042*/
18043struct AuthContext {
18044 const char *zAuthContext; /* Put saved Parse.zAuthContext here */
18045 Parse *pParse; /* The Parse structure */
18046};
18047
18048/*
18049** Bitfield flags for P5 value in various opcodes.
18050**
18051** Value constraints (enforced via assert()):
18052** OPFLAG_LENGTHARG == SQLITE_FUNC_LENGTH
18053** OPFLAG_TYPEOFARG == SQLITE_FUNC_TYPEOF
18054** OPFLAG_BULKCSR == BTREE_BULKLOAD
18055** OPFLAG_SEEKEQ == BTREE_SEEK_EQ
18056** OPFLAG_FORDELETE == BTREE_FORDELETE
18057** OPFLAG_SAVEPOSITION == BTREE_SAVEPOSITION
18058** OPFLAG_AUXDELETE == BTREE_AUXDELETE
18059*/
18060#define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
18061 /* Also used in P2 (not P5) of OP_Delete */
18062#define OPFLAG_NOCHNG 0x01 /* OP_VColumn nochange for UPDATE */
18063#define OPFLAG_EPHEM 0x01 /* OP_Column: Ephemeral output is ok */
18064#define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */
18065#define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */
18066#define OPFLAG_APPEND 0x08 /* This is likely to be an append */
18067#define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */
18068#define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */
18069#define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */
18070#define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */
18071#define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */
18072#define OPFLAG_SEEKEQ 0x02 /* OP_Open** cursor uses EQ seek only */
18073#define OPFLAG_FORDELETE 0x08 /* OP_Open should use BTREE_FORDELETE */
18074#define OPFLAG_P2ISREG 0x10 /* P2 to OP_Open** is a register number */
18075#define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */
18076#define OPFLAG_SAVEPOSITION 0x02 /* OP_Delete/Insert: save cursor pos */
18077#define OPFLAG_AUXDELETE 0x04 /* OP_Delete: index in a DELETE op */
18078#define OPFLAG_NOCHNG_MAGIC 0x6d /* OP_MakeRecord: serialtype 10 is ok */
18079
18080/*
18081 * Each trigger present in the database schema is stored as an instance of
18082 * struct Trigger.
18083 *
18084 * Pointers to instances of struct Trigger are stored in two ways.
18085 * 1. In the "trigHash" hash table (part of the sqlite3* that represents the
18086 * database). This allows Trigger structures to be retrieved by name.
18087 * 2. All triggers associated with a single table form a linked list, using the
18088 * pNext member of struct Trigger. A pointer to the first element of the
18089 * linked list is stored as the "pTrigger" member of the associated
18090 * struct Table.
18091 *
18092 * The "step_list" member points to the first element of a linked list
18093 * containing the SQL statements specified as the trigger program.
18094 */
18095struct Trigger {
18096 char *zName; /* The name of the trigger */
18097 char *table; /* The table or view to which the trigger applies */
18098 u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT */
18099 u8 tr_tm; /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
18100 Expr *pWhen; /* The WHEN clause of the expression (may be NULL) */
18101 IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
18102 the <column-list> is stored here */
18103 Schema *pSchema; /* Schema containing the trigger */
18104 Schema *pTabSchema; /* Schema containing the table */
18105 TriggerStep *step_list; /* Link list of trigger program steps */
18106 Trigger *pNext; /* Next trigger associated with the table */
18107};
18108
18109/*
18110** A trigger is either a BEFORE or an AFTER trigger. The following constants
18111** determine which.
18112**
18113** If there are multiple triggers, you might of some BEFORE and some AFTER.
18114** In that cases, the constants below can be ORed together.
18115*/
18116#define TRIGGER_BEFORE 1
18117#define TRIGGER_AFTER 2
18118
18119/*
18120 * An instance of struct TriggerStep is used to store a single SQL statement
18121 * that is a part of a trigger-program.
18122 *
18123 * Instances of struct TriggerStep are stored in a singly linked list (linked
18124 * using the "pNext" member) referenced by the "step_list" member of the
18125 * associated struct Trigger instance. The first element of the linked list is
18126 * the first step of the trigger-program.
18127 *
18128 * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
18129 * "SELECT" statement. The meanings of the other members is determined by the
18130 * value of "op" as follows:
18131 *
18132 * (op == TK_INSERT)
18133 * orconf -> stores the ON CONFLICT algorithm
18134 * pSelect -> If this is an INSERT INTO ... SELECT ... statement, then
18135 * this stores a pointer to the SELECT statement. Otherwise NULL.
18136 * zTarget -> Dequoted name of the table to insert into.
18137 * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
18138 * this stores values to be inserted. Otherwise NULL.
18139 * pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ...
18140 * statement, then this stores the column-names to be
18141 * inserted into.
18142 *
18143 * (op == TK_DELETE)
18144 * zTarget -> Dequoted name of the table to delete from.
18145 * pWhere -> The WHERE clause of the DELETE statement if one is specified.
18146 * Otherwise NULL.
18147 *
18148 * (op == TK_UPDATE)
18149 * zTarget -> Dequoted name of the table to update.
18150 * pWhere -> The WHERE clause of the UPDATE statement if one is specified.
18151 * Otherwise NULL.
18152 * pExprList -> A list of the columns to update and the expressions to update
18153 * them to. See sqlite3Update() documentation of "pChanges"
18154 * argument.
18155 *
18156 */
18157struct TriggerStep {
18158 u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
18159 u8 orconf; /* OE_Rollback etc. */
18160 Trigger *pTrig; /* The trigger that this step is a part of */
18161 Select *pSelect; /* SELECT statement or RHS of INSERT INTO SELECT ... */
18162 char *zTarget; /* Target table for DELETE, UPDATE, INSERT */
18163 Expr *pWhere; /* The WHERE clause for DELETE or UPDATE steps */
18164 ExprList *pExprList; /* SET clause for UPDATE */
18165 IdList *pIdList; /* Column names for INSERT */
18166 Upsert *pUpsert; /* Upsert clauses on an INSERT */
18167 char *zSpan; /* Original SQL text of this command */
18168 TriggerStep *pNext; /* Next in the link-list */
18169 TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */
18170};
18171
18172/*
18173** The following structure contains information used by the sqliteFix...
18174** routines as they walk the parse tree to make database references
18175** explicit.
18176*/
18177typedef struct DbFixer DbFixer;
18178struct DbFixer {
18179 Parse *pParse; /* The parsing context. Error messages written here */
18180 Schema *pSchema; /* Fix items to this schema */
18181 int bVarOnly; /* Check for variable references only */
18182 const char *zDb; /* Make sure all objects are contained in this database */
18183 const char *zType; /* Type of the container - used for error messages */
18184 const Token *pName; /* Name of the container - used for error messages */
18185};
18186
18187/*
18188** An objected used to accumulate the text of a string where we
18189** do not necessarily know how big the string will be in the end.
18190*/
18191struct sqlite3_str {
18192 sqlite3 *db; /* Optional database for lookaside. Can be NULL */
18193 char *zText; /* The string collected so far */
18194 u32 nAlloc; /* Amount of space allocated in zText */
18195 u32 mxAlloc; /* Maximum allowed allocation. 0 for no malloc usage */
18196 u32 nChar; /* Length of the string so far */
18197 u8 accError; /* SQLITE_NOMEM or SQLITE_TOOBIG */
18198 u8 printfFlags; /* SQLITE_PRINTF flags below */
18199};
18200#define SQLITE_PRINTF_INTERNAL 0x01 /* Internal-use-only converters allowed */
18201#define SQLITE_PRINTF_SQLFUNC 0x02 /* SQL function arguments to VXPrintf */
18202#define SQLITE_PRINTF_MALLOCED 0x04 /* True if xText is allocated space */
18203
18204#define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
18205
18206
18207/*
18208** A pointer to this structure is used to communicate information
18209** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
18210*/
18211typedef struct {
18212 sqlite3 *db; /* The database being initialized */
18213 char **pzErrMsg; /* Error message stored here */
18214 int iDb; /* 0 for main database. 1 for TEMP, 2.. for ATTACHed */
18215 int rc; /* Result code stored here */
18216 u32 mInitFlags; /* Flags controlling error messages */
18217} InitData;
18218
18219/*
18220** Allowed values for mInitFlags
18221*/
18222#define INITFLAG_AlterTable 0x0001 /* This is a reparse after ALTER TABLE */
18223
18224/*
18225** Structure containing global configuration data for the SQLite library.
18226**
18227** This structure also contains some state information.
18228*/
18229struct Sqlite3Config {
18230 int bMemstat; /* True to enable memory status */
18231 int bCoreMutex; /* True to enable core mutexing */
18232 int bFullMutex; /* True to enable full mutexing */
18233 int bOpenUri; /* True to interpret filenames as URIs */
18234 int bUseCis; /* Use covering indices for full-scans */
18235 int bSmallMalloc; /* Avoid large memory allocations if true */
18236 int mxStrlen; /* Maximum string length */
18237 int neverCorrupt; /* Database is always well-formed */
18238 int szLookaside; /* Default lookaside buffer size */
18239 int nLookaside; /* Default lookaside buffer count */
18240 int nStmtSpill; /* Stmt-journal spill-to-disk threshold */
18241 sqlite3_mem_methods m; /* Low-level memory allocation interface */
18242 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
18243 sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
18244 void *pHeap; /* Heap storage space */
18245 int nHeap; /* Size of pHeap[] */
18246 int mnReq, mxReq; /* Min and max heap requests sizes */
18247 sqlite3_int64 szMmap; /* mmap() space per open file */
18248 sqlite3_int64 mxMmap; /* Maximum value for szMmap */
18249 void *pPage; /* Page cache memory */
18250 int szPage; /* Size of each page in pPage[] */
18251 int nPage; /* Number of pages in pPage[] */
18252 int mxParserStack; /* maximum depth of the parser stack */
18253 int sharedCacheEnabled; /* true if shared-cache mode enabled */
18254 u32 szPma; /* Maximum Sorter PMA size */
18255 /* The above might be initialized to non-zero. The following need to always
18256 ** initially be zero, however. */
18257 int isInit; /* True after initialization has finished */
18258 int inProgress; /* True while initialization in progress */
18259 int isMutexInit; /* True after mutexes are initialized */
18260 int isMallocInit; /* True after malloc is initialized */
18261 int isPCacheInit; /* True after malloc is initialized */
18262 int nRefInitMutex; /* Number of users of pInitMutex */
18263 sqlite3_mutex *pInitMutex; /* Mutex used by sqlite3_initialize() */
18264 void (*xLog)(void*,int,const char*); /* Function for logging */
18265 void *pLogArg; /* First argument to xLog() */
18266#ifdef SQLITE_ENABLE_SQLLOG
18267 void(*xSqllog)(void*,sqlite3*,const char*, int);
18268 void *pSqllogArg;
18269#endif
18270#ifdef SQLITE_VDBE_COVERAGE
18271 /* The following callback (if not NULL) is invoked on every VDBE branch
18272 ** operation. Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE.
18273 */
18274 void (*xVdbeBranch)(void*,unsigned iSrcLine,u8 eThis,u8 eMx); /* Callback */
18275 void *pVdbeBranchArg; /* 1st argument */
18276#endif
18277#ifndef SQLITE_UNTESTABLE
18278 int (*xTestCallback)(int); /* Invoked by sqlite3FaultSim() */
18279#endif
18280 int bLocaltimeFault; /* True to fail localtime() calls */
18281 int bInternalFunctions; /* Internal SQL functions are visible */
18282 int iOnceResetThreshold; /* When to reset OP_Once counters */
18283 u32 szSorterRef; /* Min size in bytes to use sorter-refs */
18284};
18285
18286/*
18287** This macro is used inside of assert() statements to indicate that
18288** the assert is only valid on a well-formed database. Instead of:
18289**
18290** assert( X );
18291**
18292** One writes:
18293**
18294** assert( X || CORRUPT_DB );
18295**
18296** CORRUPT_DB is true during normal operation. CORRUPT_DB does not indicate
18297** that the database is definitely corrupt, only that it might be corrupt.
18298** For most test cases, CORRUPT_DB is set to false using a special
18299** sqlite3_test_control(). This enables assert() statements to prove
18300** things that are always true for well-formed databases.
18301*/
18302#define CORRUPT_DB (sqlite3Config.neverCorrupt==0)
18303
18304/*
18305** Context pointer passed down through the tree-walk.
18306*/
18307struct Walker {
18308 Parse *pParse; /* Parser context. */
18309 int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */
18310 int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */
18311 void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */
18312 int walkerDepth; /* Number of subqueries */
18313 u8 eCode; /* A small processing code */
18314 union { /* Extra data for callback */
18315 NameContext *pNC; /* Naming context */
18316 int n; /* A counter */
18317 int iCur; /* A cursor number */
18318 SrcList *pSrcList; /* FROM clause */
18319 struct SrcCount *pSrcCount; /* Counting column references */
18320 struct CCurHint *pCCurHint; /* Used by codeCursorHint() */
18321 int *aiCol; /* array of column indexes */
18322 struct IdxCover *pIdxCover; /* Check for index coverage */
18323 struct IdxExprTrans *pIdxTrans; /* Convert idxed expr to column */
18324 ExprList *pGroupBy; /* GROUP BY clause */
18325 Select *pSelect; /* HAVING to WHERE clause ctx */
18326 struct WindowRewrite *pRewrite; /* Window rewrite context */
18327 struct WhereConst *pConst; /* WHERE clause constants */
18328 struct RenameCtx *pRename; /* RENAME COLUMN context */
18329 } u;
18330};
18331
18332/* Forward declarations */
18333SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
18334SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
18335SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
18336SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*);
18337SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*);
18338SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker*, Expr*);
18339SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker*, Select*);
18340SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker*, Select*);
18341#ifdef SQLITE_DEBUG
18342SQLITE_PRIVATE void sqlite3SelectWalkAssert2(Walker*, Select*);
18343#endif
18344
18345/*
18346** Return code from the parse-tree walking primitives and their
18347** callbacks.
18348*/
18349#define WRC_Continue 0 /* Continue down into children */
18350#define WRC_Prune 1 /* Omit children but continue walking siblings */
18351#define WRC_Abort 2 /* Abandon the tree walk */
18352
18353/*
18354** An instance of this structure represents a set of one or more CTEs
18355** (common table expressions) created by a single WITH clause.
18356*/
18357struct With {
18358 int nCte; /* Number of CTEs in the WITH clause */
18359 With *pOuter; /* Containing WITH clause, or NULL */
18360 struct Cte { /* For each CTE in the WITH clause.... */
18361 char *zName; /* Name of this CTE */
18362 ExprList *pCols; /* List of explicit column names, or NULL */
18363 Select *pSelect; /* The definition of this CTE */
18364 const char *zCteErr; /* Error message for circular references */
18365 } a[1];
18366};
18367
18368#ifdef SQLITE_DEBUG
18369/*
18370** An instance of the TreeView object is used for printing the content of
18371** data structures on sqlite3DebugPrintf() using a tree-like view.
18372*/
18373struct TreeView {
18374 int iLevel; /* Which level of the tree we are on */
18375 u8 bLine[100]; /* Draw vertical in column i if bLine[i] is true */
18376};
18377#endif /* SQLITE_DEBUG */
18378
18379/*
18380** This object is used in varioius ways, all related to window functions
18381**
18382** (1) A single instance of this structure is attached to the
18383** the Expr.pWin field for each window function in an expression tree.
18384** This object holds the information contained in the OVER clause,
18385** plus additional fields used during code generation.
18386**
18387** (2) All window functions in a single SELECT form a linked-list
18388** attached to Select.pWin. The Window.pFunc and Window.pExpr
18389** fields point back to the expression that is the window function.
18390**
18391** (3) The terms of the WINDOW clause of a SELECT are instances of this
18392** object on a linked list attached to Select.pWinDefn.
18393**
18394** The uses (1) and (2) are really the same Window object that just happens
18395** to be accessible in two different ways. Use (3) is are separate objects.
18396*/
18397struct Window {
18398 char *zName; /* Name of window (may be NULL) */
18399 ExprList *pPartition; /* PARTITION BY clause */
18400 ExprList *pOrderBy; /* ORDER BY clause */
18401 u8 eType; /* TK_RANGE or TK_ROWS */
18402 u8 eStart; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
18403 u8 eEnd; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
18404 Expr *pStart; /* Expression for "<expr> PRECEDING" */
18405 Expr *pEnd; /* Expression for "<expr> FOLLOWING" */
18406 Window *pNextWin; /* Next window function belonging to this SELECT */
18407 Expr *pFilter; /* The FILTER expression */
18408 FuncDef *pFunc; /* The function */
18409 int iEphCsr; /* Partition buffer or Peer buffer */
18410 int regAccum;
18411 int regResult;
18412 int csrApp; /* Function cursor (used by min/max) */
18413 int regApp; /* Function register (also used by min/max) */
18414 int regPart; /* First in a set of registers holding PARTITION BY
18415 ** and ORDER BY values for the window */
18416 Expr *pOwner; /* Expression object this window is attached to */
18417 int nBufferCol; /* Number of columns in buffer table */
18418 int iArgCol; /* Offset of first argument for this function */
18419};
18420
18421#ifndef SQLITE_OMIT_WINDOWFUNC
18422SQLITE_PRIVATE void sqlite3WindowDelete(sqlite3*, Window*);
18423SQLITE_PRIVATE void sqlite3WindowListDelete(sqlite3 *db, Window *p);
18424SQLITE_PRIVATE Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*);
18425SQLITE_PRIVATE void sqlite3WindowAttach(Parse*, Expr*, Window*);
18426SQLITE_PRIVATE int sqlite3WindowCompare(Parse*, Window*, Window*);
18427SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse*, Window*);
18428SQLITE_PRIVATE void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int);
18429SQLITE_PRIVATE int sqlite3WindowRewrite(Parse*, Select*);
18430SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse*, struct SrcList_item*);
18431SQLITE_PRIVATE void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*);
18432SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p);
18433SQLITE_PRIVATE Window *sqlite3WindowListDup(sqlite3 *db, Window *p);
18434SQLITE_PRIVATE void sqlite3WindowFunctions(void);
18435#else
18436# define sqlite3WindowDelete(a,b)
18437# define sqlite3WindowFunctions()
18438# define sqlite3WindowAttach(a,b,c)
18439#endif
18440
18441/*
18442** Assuming zIn points to the first byte of a UTF-8 character,
18443** advance zIn to point to the first byte of the next UTF-8 character.
18444*/
18445#define SQLITE_SKIP_UTF8(zIn) { \
18446 if( (*(zIn++))>=0xc0 ){ \
18447 while( (*zIn & 0xc0)==0x80 ){ zIn++; } \
18448 } \
18449}
18450
18451/*
18452** The SQLITE_*_BKPT macros are substitutes for the error codes with
18453** the same name but without the _BKPT suffix. These macros invoke
18454** routines that report the line-number on which the error originated
18455** using sqlite3_log(). The routines also provide a convenient place
18456** to set a debugger breakpoint.
18457*/
18458SQLITE_PRIVATE int sqlite3ReportError(int iErr, int lineno, const char *zType);
18459SQLITE_PRIVATE int sqlite3CorruptError(int);
18460SQLITE_PRIVATE int sqlite3MisuseError(int);
18461SQLITE_PRIVATE int sqlite3CantopenError(int);
18462#define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
18463#define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
18464#define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
18465#ifdef SQLITE_DEBUG
18466SQLITE_PRIVATE int sqlite3NomemError(int);
18467SQLITE_PRIVATE int sqlite3IoerrnomemError(int);
18468SQLITE_PRIVATE int sqlite3CorruptPgnoError(int,Pgno);
18469# define SQLITE_NOMEM_BKPT sqlite3NomemError(__LINE__)
18470# define SQLITE_IOERR_NOMEM_BKPT sqlite3IoerrnomemError(__LINE__)
18471# define SQLITE_CORRUPT_PGNO(P) sqlite3CorruptPgnoError(__LINE__,(P))
18472#else
18473# define SQLITE_NOMEM_BKPT SQLITE_NOMEM
18474# define SQLITE_IOERR_NOMEM_BKPT SQLITE_IOERR_NOMEM
18475# define SQLITE_CORRUPT_PGNO(P) sqlite3CorruptError(__LINE__)
18476#endif
18477
18478/*
18479** FTS3 and FTS4 both require virtual table support
18480*/
18481#if defined(SQLITE_OMIT_VIRTUALTABLE)
18482# undef SQLITE_ENABLE_FTS3
18483# undef SQLITE_ENABLE_FTS4
18484#endif
18485
18486/*
18487** FTS4 is really an extension for FTS3. It is enabled using the
18488** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also call
18489** the SQLITE_ENABLE_FTS4 macro to serve as an alias for SQLITE_ENABLE_FTS3.
18490*/
18491#if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
18492# define SQLITE_ENABLE_FTS3 1
18493#endif
18494
18495/*
18496** The ctype.h header is needed for non-ASCII systems. It is also
18497** needed by FTS3 when FTS3 is included in the amalgamation.
18498*/
18499#if !defined(SQLITE_ASCII) || \
18500 (defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_AMALGAMATION))
18501# include <ctype.h>
18502#endif
18503
18504/*
18505** The following macros mimic the standard library functions toupper(),
18506** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
18507** sqlite versions only work for ASCII characters, regardless of locale.
18508*/
18509#ifdef SQLITE_ASCII
18510# define sqlite3Toupper(x) ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
18511# define sqlite3Isspace(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
18512# define sqlite3Isalnum(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
18513# define sqlite3Isalpha(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
18514# define sqlite3Isdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
18515# define sqlite3Isxdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
18516# define sqlite3Tolower(x) (sqlite3UpperToLower[(unsigned char)(x)])
18517# define sqlite3Isquote(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x80)
18518#else
18519# define sqlite3Toupper(x) toupper((unsigned char)(x))
18520# define sqlite3Isspace(x) isspace((unsigned char)(x))
18521# define sqlite3Isalnum(x) isalnum((unsigned char)(x))
18522# define sqlite3Isalpha(x) isalpha((unsigned char)(x))
18523# define sqlite3Isdigit(x) isdigit((unsigned char)(x))
18524# define sqlite3Isxdigit(x) isxdigit((unsigned char)(x))
18525# define sqlite3Tolower(x) tolower((unsigned char)(x))
18526# define sqlite3Isquote(x) ((x)=='"'||(x)=='\''||(x)=='['||(x)=='`')
18527#endif
18528SQLITE_PRIVATE int sqlite3IsIdChar(u8);
18529
18530/*
18531** Internal function prototypes
18532*/
18533SQLITE_PRIVATE int sqlite3StrICmp(const char*,const char*);
18534SQLITE_PRIVATE int sqlite3Strlen30(const char*);
18535#define sqlite3Strlen30NN(C) (strlen(C)&0x3fffffff)
18536SQLITE_PRIVATE char *sqlite3ColumnType(Column*,char*);
18537#define sqlite3StrNICmp sqlite3_strnicmp
18538
18539SQLITE_PRIVATE int sqlite3MallocInit(void);
18540SQLITE_PRIVATE void sqlite3MallocEnd(void);
18541SQLITE_PRIVATE void *sqlite3Malloc(u64);
18542SQLITE_PRIVATE void *sqlite3MallocZero(u64);
18543SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, u64);
18544SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, u64);
18545SQLITE_PRIVATE void *sqlite3DbMallocRawNN(sqlite3*, u64);
18546SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*);
18547SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, u64);
18548SQLITE_PRIVATE char *sqlite3DbSpanDup(sqlite3*,const char*,const char*);
18549SQLITE_PRIVATE void *sqlite3Realloc(void*, u64);
18550SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
18551SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, u64);
18552SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*);
18553SQLITE_PRIVATE void sqlite3DbFreeNN(sqlite3*, void*);
18554SQLITE_PRIVATE int sqlite3MallocSize(void*);
18555SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, void*);
18556SQLITE_PRIVATE void *sqlite3PageMalloc(int);
18557SQLITE_PRIVATE void sqlite3PageFree(void*);
18558SQLITE_PRIVATE void sqlite3MemSetDefault(void);
18559#ifndef SQLITE_UNTESTABLE
18560SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
18561#endif
18562SQLITE_PRIVATE int sqlite3HeapNearlyFull(void);
18563
18564/*
18565** On systems with ample stack space and that support alloca(), make
18566** use of alloca() to obtain space for large automatic objects. By default,
18567** obtain space from malloc().
18568**
18569** The alloca() routine never returns NULL. This will cause code paths
18570** that deal with sqlite3StackAlloc() failures to be unreachable.
18571*/
18572#ifdef SQLITE_USE_ALLOCA
18573# define sqlite3StackAllocRaw(D,N) alloca(N)
18574# define sqlite3StackAllocZero(D,N) memset(alloca(N), 0, N)
18575# define sqlite3StackFree(D,P)
18576#else
18577# define sqlite3StackAllocRaw(D,N) sqlite3DbMallocRaw(D,N)
18578# define sqlite3StackAllocZero(D,N) sqlite3DbMallocZero(D,N)
18579# define sqlite3StackFree(D,P) sqlite3DbFree(D,P)
18580#endif
18581
18582/* Do not allow both MEMSYS5 and MEMSYS3 to be defined together. If they
18583** are, disable MEMSYS3
18584*/
18585#ifdef SQLITE_ENABLE_MEMSYS5
18586SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
18587#undef SQLITE_ENABLE_MEMSYS3
18588#endif
18589#ifdef SQLITE_ENABLE_MEMSYS3
18590SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
18591#endif
18592
18593
18594#ifndef SQLITE_MUTEX_OMIT
18595SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
18596SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void);
18597SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int);
18598SQLITE_PRIVATE int sqlite3MutexInit(void);
18599SQLITE_PRIVATE int sqlite3MutexEnd(void);
18600#endif
18601#if !defined(SQLITE_MUTEX_OMIT) && !defined(SQLITE_MUTEX_NOOP)
18602SQLITE_PRIVATE void sqlite3MemoryBarrier(void);
18603#else
18604# define sqlite3MemoryBarrier()
18605#endif
18606
18607SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int);
18608SQLITE_PRIVATE void sqlite3StatusUp(int, int);
18609SQLITE_PRIVATE void sqlite3StatusDown(int, int);
18610SQLITE_PRIVATE void sqlite3StatusHighwater(int, int);
18611SQLITE_PRIVATE int sqlite3LookasideUsed(sqlite3*,int*);
18612
18613/* Access to mutexes used by sqlite3_status() */
18614SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void);
18615SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void);
18616
18617#if defined(SQLITE_ENABLE_MULTITHREADED_CHECKS) && !defined(SQLITE_MUTEX_OMIT)
18618SQLITE_PRIVATE void sqlite3MutexWarnOnContention(sqlite3_mutex*);
18619#else
18620# define sqlite3MutexWarnOnContention(x)
18621#endif
18622
18623#ifndef SQLITE_OMIT_FLOATING_POINT
18624SQLITE_PRIVATE int sqlite3IsNaN(double);
18625#else
18626# define sqlite3IsNaN(X) 0
18627#endif
18628
18629/*
18630** An instance of the following structure holds information about SQL
18631** functions arguments that are the parameters to the printf() function.
18632*/
18633struct PrintfArguments {
18634 int nArg; /* Total number of arguments */
18635 int nUsed; /* Number of arguments used so far */
18636 sqlite3_value **apArg; /* The argument values */
18637};
18638
18639SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
18640SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
18641#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
18642SQLITE_PRIVATE void sqlite3DebugPrintf(const char*, ...);
18643#endif
18644#if defined(SQLITE_TEST)
18645SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*);
18646#endif
18647
18648#if defined(SQLITE_DEBUG)
18649SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
18650SQLITE_PRIVATE void sqlite3TreeViewBareExprList(TreeView*, const ExprList*, const char*);
18651SQLITE_PRIVATE void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
18652SQLITE_PRIVATE void sqlite3TreeViewSrcList(TreeView*, const SrcList*);
18653SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
18654SQLITE_PRIVATE void sqlite3TreeViewWith(TreeView*, const With*, u8);
18655#ifndef SQLITE_OMIT_WINDOWFUNC
18656SQLITE_PRIVATE void sqlite3TreeViewWindow(TreeView*, const Window*, u8);
18657SQLITE_PRIVATE void sqlite3TreeViewWinFunc(TreeView*, const Window*, u8);
18658#endif
18659#endif
18660
18661
18662SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*);
18663SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
18664SQLITE_PRIVATE void sqlite3Dequote(char*);
18665SQLITE_PRIVATE void sqlite3TokenInit(Token*,char*);
18666SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
18667SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
18668SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
18669SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
18670SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
18671SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
18672SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
18673SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse*);
18674#ifdef SQLITE_DEBUG
18675SQLITE_PRIVATE int sqlite3NoTempsInRange(Parse*,int,int);
18676#endif
18677SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
18678SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
18679SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
18680SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*);
18681SQLITE_PRIVATE void sqlite3PExprAddSelect(Parse*, Expr*, Select*);
18682SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
18683SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*, int);
18684SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32);
18685SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*);
18686SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
18687SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*);
18688SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList*,int);
18689SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
18690SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,const char*,const char*);
18691SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*);
18692SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList*);
18693SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
18694SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**);
18695SQLITE_PRIVATE int sqlite3InitOne(sqlite3*, int, char**, u32);
18696SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
18697#ifndef SQLITE_OMIT_VIRTUALTABLE
18698SQLITE_PRIVATE Module *sqlite3PragmaVtabRegister(sqlite3*,const char *zName);
18699#endif
18700SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3*);
18701SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3*,int);
18702SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3*);
18703SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*);
18704SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3*,Table*);
18705SQLITE_PRIVATE int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**);
18706SQLITE_PRIVATE void sqlite3SelectAddColumnTypeAndCollation(Parse*,Table*,Select*);
18707SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*);
18708SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *, int);
18709SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table*);
18710SQLITE_PRIVATE i16 sqlite3ColumnOfIndex(Index*, i16);
18711SQLITE_PRIVATE void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
18712#if SQLITE_ENABLE_HIDDEN_COLUMNS
18713SQLITE_PRIVATE void sqlite3ColumnPropertiesFromName(Table*, Column*);
18714#else
18715# define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
18716#endif
18717SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*,Token*);
18718SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int);
18719SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
18720SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*);
18721SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,Expr*,const char*,const char*);
18722SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
18723SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*);
18724SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
18725 sqlite3_vfs**,char**,char **);
18726SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
18727
18728#ifdef SQLITE_UNTESTABLE
18729# define sqlite3FaultSim(X) SQLITE_OK
18730#else
18731SQLITE_PRIVATE int sqlite3FaultSim(int);
18732#endif
18733
18734SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
18735SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
18736SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec*, u32);
18737SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
18738SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
18739SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*);
18740SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*);
18741#ifndef SQLITE_UNTESTABLE
18742SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
18743#endif
18744
18745SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*);
18746SQLITE_PRIVATE void sqlite3RowSetDelete(void*);
18747SQLITE_PRIVATE void sqlite3RowSetClear(void*);
18748SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
18749SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, int iBatch, i64);
18750SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*);
18751
18752SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,ExprList*,Select*,int,int);
18753
18754#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
18755SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse*,Table*);
18756#else
18757# define sqlite3ViewGetColumnNames(A,B) 0
18758#endif
18759
18760#if SQLITE_MAX_ATTACHED>30
18761SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask);
18762#endif
18763SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
18764SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
18765SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
18766SQLITE_PRIVATE void sqlite3FreeIndex(sqlite3*, Index*);
18767#ifndef SQLITE_OMIT_AUTOINCREMENT
18768SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse);
18769SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse);
18770#else
18771# define sqlite3AutoincrementBegin(X)
18772# define sqlite3AutoincrementEnd(X)
18773#endif
18774SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int, Upsert*);
18775SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
18776SQLITE_PRIVATE IdList *sqlite3IdListAppend(Parse*, IdList*, Token*);
18777SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
18778SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
18779SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
18780SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
18781 Token*, Select*, Expr*, IdList*);
18782SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
18783SQLITE_PRIVATE void sqlite3SrcListFuncArgs(Parse*, SrcList*, ExprList*);
18784SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
18785SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList*);
18786SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
18787SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
18788SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
18789SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**);
18790SQLITE_PRIVATE void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
18791 Expr*, int, int, u8);
18792SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
18793SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
18794SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
18795 Expr*,ExprList*,u32,Expr*);
18796SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
18797SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
18798SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
18799SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
18800#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
18801SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,char*);
18802#endif
18803SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*);
18804SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*,
18805 Upsert*);
18806SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
18807SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
18808SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo*);
18809SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
18810SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
18811SQLITE_PRIVATE int sqlite3WhereOrderByLimitOptLabel(WhereInfo*);
18812SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo*);
18813SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo*);
18814SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo*);
18815SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo*, int*);
18816#define ONEPASS_OFF 0 /* Use of ONEPASS not allowed */
18817#define ONEPASS_SINGLE 1 /* ONEPASS valid for a single row update */
18818#define ONEPASS_MULTI 2 /* ONEPASS is valid for multiple rows */
18819SQLITE_PRIVATE void sqlite3ExprCodeLoadIndexColumn(Parse*, Index*, int, int, int);
18820SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
18821SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
18822SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
18823SQLITE_PRIVATE void sqlite3ExprCode(Parse*, Expr*, int);
18824SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, Expr*, int);
18825SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
18826SQLITE_PRIVATE int sqlite3ExprCodeAtInit(Parse*, Expr*, int);
18827SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
18828SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
18829SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse*, Expr*, int);
18830SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
18831#define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */
18832#define SQLITE_ECEL_FACTOR 0x02 /* Factor out constant terms */
18833#define SQLITE_ECEL_REF 0x04 /* Use ExprList.u.x.iOrderByCol */
18834#define SQLITE_ECEL_OMITREF 0x08 /* Omit if ExprList.u.x.iOrderByCol */
18835SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
18836SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
18837SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int);
18838SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
18839#define LOCATE_VIEW 0x01
18840#define LOCATE_NOERR 0x02
18841SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,u32 flags,const char*, const char*);
18842SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,u32 flags,struct SrcList_item *);
18843SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
18844SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
18845SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
18846SQLITE_PRIVATE void sqlite3Vacuum(Parse*,Token*);
18847SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*, int);
18848SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
18849SQLITE_PRIVATE int sqlite3ExprCompare(Parse*,Expr*, Expr*, int);
18850SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr*, Expr*, int);
18851SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*, int);
18852SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Parse*,Expr*, Expr*, int);
18853SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr*,int);
18854SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
18855SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
18856SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx);
18857SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
18858SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
18859#ifndef SQLITE_UNTESTABLE
18860SQLITE_PRIVATE void sqlite3PrngSaveState(void);
18861SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
18862#endif
18863SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*,int);
18864SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
18865SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
18866SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
18867SQLITE_PRIVATE void sqlite3EndTransaction(Parse*,int);
18868SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
18869SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
18870SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
18871SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr*);
18872SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr*);
18873SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
18874SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
18875SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8);
18876SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
18877SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr*,int);
18878#ifdef SQLITE_ENABLE_CURSOR_HINTS
18879SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr*);
18880#endif
18881SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
18882SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
18883SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
18884SQLITE_PRIVATE int sqlite3IsRowid(const char*);
18885#ifdef SQLITE_ENABLE_NORMALIZE
18886SQLITE_PRIVATE int sqlite3IsRowidN(const char*, int);
18887#endif
18888SQLITE_PRIVATE void sqlite3GenerateRowDelete(
18889 Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
18890SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
18891SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
18892SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
18893SQLITE_PRIVATE int sqlite3ExprReferencesUpdatedColumn(Expr*,int*,int);
18894SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
18895 u8,u8,int,int*,int*,Upsert*);
18896#ifdef SQLITE_ENABLE_NULL_TRIM
18897SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe*,Table*);
18898#else
18899# define sqlite3SetMakeRecordP5(A,B)
18900#endif
18901SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
18902SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
18903SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
18904SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
18905SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
18906SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8);
18907SQLITE_PRIVATE void sqlite3UniqueConstraint(Parse*, int, Index*);
18908SQLITE_PRIVATE void sqlite3RowidConstraint(Parse*, int, Table*);
18909SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
18910SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
18911SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
18912SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
18913SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
18914#ifdef SQLITE_ENABLE_NORMALIZE
18915SQLITE_PRIVATE FuncDef *sqlite3FunctionSearchN(int,const char*,int);
18916#endif
18917SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(FuncDef*,int);
18918SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
18919SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void);
18920SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
18921SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
18922SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
18923SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
18924SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
18925
18926#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
18927SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, ExprList*,Expr*,int);
18928#endif
18929
18930#ifndef SQLITE_OMIT_TRIGGER
18931SQLITE_PRIVATE void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
18932 Expr*,int, int);
18933SQLITE_PRIVATE void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
18934SQLITE_PRIVATE void sqlite3DropTrigger(Parse*, SrcList*, int);
18935SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse*, Trigger*);
18936SQLITE_PRIVATE Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
18937SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *, Table *);
18938SQLITE_PRIVATE void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
18939 int, int, int);
18940SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
18941 void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
18942SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
18943SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*,
18944 const char*,const char*);
18945SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(Parse*,Token*, IdList*,
18946 Select*,u8,Upsert*,
18947 const char*,const char*);
18948SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(Parse*,Token*,ExprList*, Expr*, u8,
18949 const char*,const char*);
18950SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(Parse*,Token*, Expr*,
18951 const char*,const char*);
18952SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3*, Trigger*);
18953SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
18954SQLITE_PRIVATE u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
18955# define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
18956# define sqlite3IsToplevel(p) ((p)->pToplevel==0)
18957#else
18958# define sqlite3TriggersExist(B,C,D,E,F) 0
18959# define sqlite3DeleteTrigger(A,B)
18960# define sqlite3DropTriggerPtr(A,B)
18961# define sqlite3UnlinkAndDeleteTrigger(A,B,C)
18962# define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
18963# define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
18964# define sqlite3TriggerList(X, Y) 0
18965# define sqlite3ParseToplevel(p) p
18966# define sqlite3IsToplevel(p) 1
18967# define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
18968#endif
18969
18970SQLITE_PRIVATE int sqlite3JoinType(Parse*, Token*, Token*, Token*);
18971SQLITE_PRIVATE void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
18972SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse*, int);
18973#ifndef SQLITE_OMIT_AUTHORIZATION
18974SQLITE_PRIVATE void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
18975SQLITE_PRIVATE int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
18976SQLITE_PRIVATE void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
18977SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext*);
18978SQLITE_PRIVATE int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
18979#else
18980# define sqlite3AuthRead(a,b,c,d)
18981# define sqlite3AuthCheck(a,b,c,d,e) SQLITE_OK
18982# define sqlite3AuthContextPush(a,b,c)
18983# define sqlite3AuthContextPop(a) ((void)(a))
18984#endif
18985SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
18986SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*);
18987SQLITE_PRIVATE void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
18988SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
18989SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
18990SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
18991SQLITE_PRIVATE int sqlite3FixExprList(DbFixer*, ExprList*);
18992SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
18993SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8);
18994SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
18995SQLITE_PRIVATE int sqlite3Atoi(const char*);
18996#ifndef SQLITE_OMIT_UTF16
18997SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
18998#endif
18999SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
19000SQLITE_PRIVATE u32 sqlite3Utf8Read(const u8**);
19001SQLITE_PRIVATE LogEst sqlite3LogEst(u64);
19002SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst,LogEst);
19003#ifndef SQLITE_OMIT_VIRTUALTABLE
19004SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double);
19005#endif
19006#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
19007 defined(SQLITE_ENABLE_STAT3_OR_STAT4) || \
19008 defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
19009SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst);
19010#endif
19011SQLITE_PRIVATE VList *sqlite3VListAdd(sqlite3*,VList*,const char*,int,int);
19012SQLITE_PRIVATE const char *sqlite3VListNumToName(VList*,int);
19013SQLITE_PRIVATE int sqlite3VListNameToNum(VList*,const char*,int);
19014
19015/*
19016** Routines to read and write variable-length integers. These used to
19017** be defined locally, but now we use the varint routines in the util.c
19018** file.
19019*/
19020SQLITE_PRIVATE int sqlite3PutVarint(unsigned char*, u64);
19021SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *, u64 *);
19022SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *, u32 *);
19023SQLITE_PRIVATE int sqlite3VarintLen(u64 v);
19024
19025/*
19026** The common case is for a varint to be a single byte. They following
19027** macros handle the common case without a procedure call, but then call
19028** the procedure for larger varints.
19029*/
19030#define getVarint32(A,B) \
19031 (u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32((A),(u32 *)&(B)))
19032#define putVarint32(A,B) \
19033 (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\
19034 sqlite3PutVarint((A),(B)))
19035#define getVarint sqlite3GetVarint
19036#define putVarint sqlite3PutVarint
19037
19038
19039SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3*, Index*);
19040SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int);
19041SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
19042SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
19043SQLITE_PRIVATE char sqlite3TableColumnAffinity(Table*,int);
19044SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
19045SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
19046SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char*, i64*);
19047SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
19048SQLITE_PRIVATE void sqlite3Error(sqlite3*,int);
19049SQLITE_PRIVATE void sqlite3SystemError(sqlite3*,int);
19050SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
19051SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
19052SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
19053
19054#if defined(SQLITE_NEED_ERR_NAME)
19055SQLITE_PRIVATE const char *sqlite3ErrName(int);
19056#endif
19057
19058#ifdef SQLITE_ENABLE_DESERIALIZE
19059SQLITE_PRIVATE int sqlite3MemdbInit(void);
19060#endif
19061
19062SQLITE_PRIVATE const char *sqlite3ErrStr(int);
19063SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
19064SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
19065SQLITE_PRIVATE int sqlite3IsBinary(const CollSeq*);
19066SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
19067SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
19068SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr);
19069SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse*,Expr*,Expr*);
19070SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int);
19071SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
19072SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
19073SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
19074SQLITE_PRIVATE int sqlite3WritableSchema(sqlite3*);
19075SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
19076SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
19077SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
19078SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64);
19079SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64);
19080SQLITE_PRIVATE int sqlite3AbsInt32(int);
19081#ifdef SQLITE_ENABLE_8_3_NAMES
19082SQLITE_PRIVATE void sqlite3FileSuffix3(const char*, char*);
19083#else
19084# define sqlite3FileSuffix3(X,Y)
19085#endif
19086SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z,u8);
19087
19088SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
19089SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
19090SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
19091 void(*)(void*));
19092SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value*);
19093SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
19094SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
19095#ifndef SQLITE_OMIT_UTF16
19096SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
19097#endif
19098SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
19099SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
19100#ifndef SQLITE_AMALGAMATION
19101SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
19102SQLITE_PRIVATE const char sqlite3StrBINARY[];
19103SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
19104SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[];
19105SQLITE_PRIVATE const Token sqlite3IntTokens[];
19106SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
19107SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions;
19108#ifndef SQLITE_OMIT_WSD
19109SQLITE_PRIVATE int sqlite3PendingByte;
19110#endif
19111#endif
19112#ifdef VDBE_PROFILE
19113SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt;
19114#endif
19115SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
19116SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
19117SQLITE_PRIVATE void sqlite3AlterFunctions(void);
19118SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
19119SQLITE_PRIVATE void sqlite3AlterRenameColumn(Parse*, SrcList*, Token*, Token*);
19120SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
19121#ifdef SQLITE_ENABLE_NORMALIZE
19122SQLITE_PRIVATE int sqlite3GetTokenNormalized(const unsigned char *, int *, int *);
19123#endif
19124SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
19125SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*, int);
19126SQLITE_PRIVATE int sqlite3CodeSubselect(Parse*, Expr *, int, int);
19127SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
19128SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
19129SQLITE_PRIVATE int sqlite3MatchSpanName(const char*, const char*, const char*, const char*);
19130SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
19131SQLITE_PRIVATE int sqlite3ResolveExprListNames(NameContext*, ExprList*);
19132SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
19133SQLITE_PRIVATE void sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
19134SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
19135SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
19136SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
19137SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
19138SQLITE_PRIVATE void *sqlite3RenameTokenMap(Parse*, void*, Token*);
19139SQLITE_PRIVATE void sqlite3RenameTokenRemap(Parse*, void *pTo, void *pFrom);
19140SQLITE_PRIVATE void sqlite3RenameExprUnmap(Parse*, Expr*);
19141SQLITE_PRIVATE void sqlite3RenameExprlistUnmap(Parse*, ExprList*);
19142SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
19143SQLITE_PRIVATE char sqlite3AffinityType(const char*, Column*);
19144SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
19145SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*, sqlite3_file*);
19146SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
19147SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
19148SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
19149SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
19150SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
19151SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
19152SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
19153SQLITE_PRIVATE void sqlite3SchemaClear(void *);
19154SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
19155SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
19156SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int);
19157SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo*);
19158SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoRef(KeyInfo*);
19159SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse*, Index*);
19160SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoFromExprList(Parse*, ExprList*, int, int);
19161
19162#ifdef SQLITE_DEBUG
19163SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo*);
19164#endif
19165SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
19166 void (*)(sqlite3_context*,int,sqlite3_value **),
19167 void (*)(sqlite3_context*,int,sqlite3_value **),
19168 void (*)(sqlite3_context*),
19169 void (*)(sqlite3_context*),
19170 void (*)(sqlite3_context*,int,sqlite3_value **),
19171 FuncDestructor *pDestructor
19172);
19173SQLITE_PRIVATE void sqlite3NoopDestructor(void*);
19174SQLITE_PRIVATE void sqlite3OomFault(sqlite3*);
19175SQLITE_PRIVATE void sqlite3OomClear(sqlite3*);
19176SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
19177SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
19178
19179SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
19180SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
19181SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
19182SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
19183
19184SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
19185SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
19186
19187#ifndef SQLITE_OMIT_SUBQUERY
19188SQLITE_PRIVATE int sqlite3ExprCheckIN(Parse*, Expr*);
19189#else
19190# define sqlite3ExprCheckIN(x,y) SQLITE_OK
19191#endif
19192
19193#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
19194SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void);
19195SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
19196 Parse*,Index*,UnpackedRecord**,Expr*,int,int,int*);
19197SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**);
19198SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord*);
19199SQLITE_PRIVATE int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
19200SQLITE_PRIVATE char sqlite3IndexColumnAffinity(sqlite3*, Index*, int);
19201#endif
19202
19203/*
19204** The interface to the LEMON-generated parser
19205*/
19206#ifndef SQLITE_AMALGAMATION
19207SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(u64), Parse*);
19208SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
19209#endif
19210SQLITE_PRIVATE void sqlite3Parser(void*, int, Token);
19211SQLITE_PRIVATE int sqlite3ParserFallback(int);
19212#ifdef YYTRACKMAXSTACKDEPTH
19213SQLITE_PRIVATE int sqlite3ParserStackPeak(void*);
19214#endif
19215
19216SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3*);
19217#ifndef SQLITE_OMIT_LOAD_EXTENSION
19218SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3*);
19219#else
19220# define sqlite3CloseExtensions(X)
19221#endif
19222
19223#ifndef SQLITE_OMIT_SHARED_CACHE
19224SQLITE_PRIVATE void sqlite3TableLock(Parse *, int, int, u8, const char *);
19225#else
19226 #define sqlite3TableLock(v,w,x,y,z)
19227#endif
19228
19229#ifdef SQLITE_TEST
19230SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char*);
19231#endif
19232
19233#ifdef SQLITE_OMIT_VIRTUALTABLE
19234# define sqlite3VtabClear(Y)
19235# define sqlite3VtabSync(X,Y) SQLITE_OK
19236# define sqlite3VtabRollback(X)
19237# define sqlite3VtabCommit(X)
19238# define sqlite3VtabInSync(db) 0
19239# define sqlite3VtabLock(X)
19240# define sqlite3VtabUnlock(X)
19241# define sqlite3VtabUnlockList(X)
19242# define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
19243# define sqlite3GetVTable(X,Y) ((VTable*)0)
19244#else
19245SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table*);
19246SQLITE_PRIVATE void sqlite3VtabDisconnect(sqlite3 *db, Table *p);
19247SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, Vdbe*);
19248SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db);
19249SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db);
19250SQLITE_PRIVATE void sqlite3VtabLock(VTable *);
19251SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *);
19252SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3*);
19253SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *, int, int);
19254SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe*, sqlite3_vtab*);
19255SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
19256SQLITE_PRIVATE Module *sqlite3VtabCreateModule(
19257 sqlite3*,
19258 const char*,
19259 const sqlite3_module*,
19260 void*,
19261 void(*)(void*)
19262 );
19263# define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
19264#endif
19265SQLITE_PRIVATE int sqlite3VtabEponymousTableInit(Parse*,Module*);
19266SQLITE_PRIVATE void sqlite3VtabEponymousTableClear(sqlite3*,Module*);
19267SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
19268SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int);
19269SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*);
19270SQLITE_PRIVATE void sqlite3VtabArgInit(Parse*);
19271SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse*, Token*);
19272SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
19273SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
19274SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
19275SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
19276SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
19277SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
19278SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
19279SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
19280SQLITE_PRIVATE void sqlite3ParserReset(Parse*);
19281#ifdef SQLITE_ENABLE_NORMALIZE
19282SQLITE_PRIVATE void sqlite3Normalize(Vdbe*, const char*, int, u8);
19283#endif
19284SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
19285SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
19286SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
19287SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
19288SQLITE_PRIVATE const char *sqlite3JournalModename(int);
19289#ifndef SQLITE_OMIT_WAL
19290SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
19291SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
19292#endif
19293#ifndef SQLITE_OMIT_CTE
19294SQLITE_PRIVATE With *sqlite3WithAdd(Parse*,With*,Token*,ExprList*,Select*);
19295SQLITE_PRIVATE void sqlite3WithDelete(sqlite3*,With*);
19296SQLITE_PRIVATE void sqlite3WithPush(Parse*, With*, u8);
19297#else
19298#define sqlite3WithPush(x,y,z)
19299#define sqlite3WithDelete(x,y)
19300#endif
19301#ifndef SQLITE_OMIT_UPSERT
19302SQLITE_PRIVATE Upsert *sqlite3UpsertNew(sqlite3*,ExprList*,Expr*,ExprList*,Expr*);
19303SQLITE_PRIVATE void sqlite3UpsertDelete(sqlite3*,Upsert*);
19304SQLITE_PRIVATE Upsert *sqlite3UpsertDup(sqlite3*,Upsert*);
19305SQLITE_PRIVATE int sqlite3UpsertAnalyzeTarget(Parse*,SrcList*,Upsert*);
19306SQLITE_PRIVATE void sqlite3UpsertDoUpdate(Parse*,Upsert*,Table*,Index*,int);
19307#else
19308#define sqlite3UpsertNew(v,w,x,y,z) ((Upsert*)0)
19309#define sqlite3UpsertDelete(x,y)
19310#define sqlite3UpsertDup(x,y) ((Upsert*)0)
19311#endif
19312
19313
19314/* Declarations for functions in fkey.c. All of these are replaced by
19315** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
19316** key functionality is available. If OMIT_TRIGGER is defined but
19317** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
19318** this case foreign keys are parsed, but no other functionality is
19319** provided (enforcement of FK constraints requires the triggers sub-system).
19320*/
19321#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
19322SQLITE_PRIVATE void sqlite3FkCheck(Parse*, Table*, int, int, int*, int);
19323SQLITE_PRIVATE void sqlite3FkDropTable(Parse*, SrcList *, Table*);
19324SQLITE_PRIVATE void sqlite3FkActions(Parse*, Table*, ExprList*, int, int*, int);
19325SQLITE_PRIVATE int sqlite3FkRequired(Parse*, Table*, int*, int);
19326SQLITE_PRIVATE u32 sqlite3FkOldmask(Parse*, Table*);
19327SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *);
19328#else
19329 #define sqlite3FkActions(a,b,c,d,e,f)
19330 #define sqlite3FkCheck(a,b,c,d,e,f)
19331 #define sqlite3FkDropTable(a,b,c)
19332 #define sqlite3FkOldmask(a,b) 0
19333 #define sqlite3FkRequired(a,b,c,d) 0
19334 #define sqlite3FkReferences(a) 0
19335#endif
19336#ifndef SQLITE_OMIT_FOREIGN_KEY
19337SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *, Table*);
19338SQLITE_PRIVATE int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
19339#else
19340 #define sqlite3FkDelete(a,b)
19341 #define sqlite3FkLocateIndex(a,b,c,d,e)
19342#endif
19343
19344
19345/*
19346** Available fault injectors. Should be numbered beginning with 0.
19347*/
19348#define SQLITE_FAULTINJECTOR_MALLOC 0
19349#define SQLITE_FAULTINJECTOR_COUNT 1
19350
19351/*
19352** The interface to the code in fault.c used for identifying "benign"
19353** malloc failures. This is only present if SQLITE_UNTESTABLE
19354** is not defined.
19355*/
19356#ifndef SQLITE_UNTESTABLE
19357SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void);
19358SQLITE_PRIVATE void sqlite3EndBenignMalloc(void);
19359#else
19360 #define sqlite3BeginBenignMalloc()
19361 #define sqlite3EndBenignMalloc()
19362#endif
19363
19364/*
19365** Allowed return values from sqlite3FindInIndex()
19366*/
19367#define IN_INDEX_ROWID 1 /* Search the rowid of the table */
19368#define IN_INDEX_EPH 2 /* Search an ephemeral b-tree */
19369#define IN_INDEX_INDEX_ASC 3 /* Existing index ASCENDING */
19370#define IN_INDEX_INDEX_DESC 4 /* Existing index DESCENDING */
19371#define IN_INDEX_NOOP 5 /* No table available. Use comparisons */
19372/*
19373** Allowed flags for the 3rd parameter to sqlite3FindInIndex().
19374*/
19375#define IN_INDEX_NOOP_OK 0x0001 /* OK to return IN_INDEX_NOOP */
19376#define IN_INDEX_MEMBERSHIP 0x0002 /* IN operator used for membership test */
19377#define IN_INDEX_LOOP 0x0004 /* IN operator used as a loop */
19378SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, u32, int*, int*);
19379
19380SQLITE_PRIVATE int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
19381SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *);
19382#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
19383 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
19384SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *);
19385#endif
19386
19387SQLITE_PRIVATE int sqlite3JournalIsInMemory(sqlite3_file *p);
19388SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *);
19389
19390SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p);
19391#if SQLITE_MAX_EXPR_DEPTH>0
19392SQLITE_PRIVATE int sqlite3SelectExprHeight(Select *);
19393SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse*, int);
19394#else
19395 #define sqlite3SelectExprHeight(x) 0
19396 #define sqlite3ExprCheckHeight(x,y)
19397#endif
19398
19399SQLITE_PRIVATE u32 sqlite3Get4byte(const u8*);
19400SQLITE_PRIVATE void sqlite3Put4byte(u8*, u32);
19401
19402#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
19403SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
19404SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db);
19405SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db);
19406#else
19407 #define sqlite3ConnectionBlocked(x,y)
19408 #define sqlite3ConnectionUnlocked(x)
19409 #define sqlite3ConnectionClosed(x)
19410#endif
19411
19412#ifdef SQLITE_DEBUG
19413SQLITE_PRIVATE void sqlite3ParserTrace(FILE*, char *);
19414#endif
19415#if defined(YYCOVERAGE)
19416SQLITE_PRIVATE int sqlite3ParserCoverage(FILE*);
19417#endif
19418
19419/*
19420** If the SQLITE_ENABLE IOTRACE exists then the global variable
19421** sqlite3IoTrace is a pointer to a printf-like routine used to
19422** print I/O tracing messages.
19423*/
19424#ifdef SQLITE_ENABLE_IOTRACE
19425# define IOTRACE(A) if( sqlite3IoTrace ){ sqlite3IoTrace A; }
19426SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe*);
19427SQLITE_API SQLITE_EXTERN void (SQLITE_CDECL *sqlite3IoTrace)(const char*,...);
19428#else
19429# define IOTRACE(A)
19430# define sqlite3VdbeIOTraceSql(X)
19431#endif
19432
19433/*
19434** These routines are available for the mem2.c debugging memory allocator
19435** only. They are used to verify that different "types" of memory
19436** allocations are properly tracked by the system.
19437**
19438** sqlite3MemdebugSetType() sets the "type" of an allocation to one of
19439** the MEMTYPE_* macros defined below. The type must be a bitmask with
19440** a single bit set.
19441**
19442** sqlite3MemdebugHasType() returns true if any of the bits in its second
19443** argument match the type set by the previous sqlite3MemdebugSetType().
19444** sqlite3MemdebugHasType() is intended for use inside assert() statements.
19445**
19446** sqlite3MemdebugNoType() returns true if none of the bits in its second
19447** argument match the type set by the previous sqlite3MemdebugSetType().
19448**
19449** Perhaps the most important point is the difference between MEMTYPE_HEAP
19450** and MEMTYPE_LOOKASIDE. If an allocation is MEMTYPE_LOOKASIDE, that means
19451** it might have been allocated by lookaside, except the allocation was
19452** too large or lookaside was already full. It is important to verify
19453** that allocations that might have been satisfied by lookaside are not
19454** passed back to non-lookaside free() routines. Asserts such as the
19455** example above are placed on the non-lookaside free() routines to verify
19456** this constraint.
19457**
19458** All of this is no-op for a production build. It only comes into
19459** play when the SQLITE_MEMDEBUG compile-time option is used.
19460*/
19461#ifdef SQLITE_MEMDEBUG
19462SQLITE_PRIVATE void sqlite3MemdebugSetType(void*,u8);
19463SQLITE_PRIVATE int sqlite3MemdebugHasType(void*,u8);
19464SQLITE_PRIVATE int sqlite3MemdebugNoType(void*,u8);
19465#else
19466# define sqlite3MemdebugSetType(X,Y) /* no-op */
19467# define sqlite3MemdebugHasType(X,Y) 1
19468# define sqlite3MemdebugNoType(X,Y) 1
19469#endif
19470#define MEMTYPE_HEAP 0x01 /* General heap allocations */
19471#define MEMTYPE_LOOKASIDE 0x02 /* Heap that might have been lookaside */
19472#define MEMTYPE_PCACHE 0x04 /* Page cache allocations */
19473
19474/*
19475** Threading interface
19476*/
19477#if SQLITE_MAX_WORKER_THREADS>0
19478SQLITE_PRIVATE int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*);
19479SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread*, void**);
19480#endif
19481
19482#if defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)
19483SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3*);
19484#endif
19485#if defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)
19486SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3*);
19487#endif
19488
19489SQLITE_PRIVATE int sqlite3ExprVectorSize(Expr *pExpr);
19490SQLITE_PRIVATE int sqlite3ExprIsVector(Expr *pExpr);
19491SQLITE_PRIVATE Expr *sqlite3VectorFieldSubexpr(Expr*, int);
19492SQLITE_PRIVATE Expr *sqlite3ExprForVectorField(Parse*,Expr*,int);
19493SQLITE_PRIVATE void sqlite3VectorErrorMsg(Parse*, Expr*);
19494
19495#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
19496SQLITE_PRIVATE const char **sqlite3CompileOptions(int *pnOpt);
19497#endif
19498
19499#endif /* SQLITEINT_H */
19500
19501/************** End of sqliteInt.h *******************************************/
19502/************** Begin file global.c ******************************************/
19503/*
19504** 2008 June 13
19505**
19506** The author disclaims copyright to this source code. In place of
19507** a legal notice, here is a blessing:
19508**
19509** May you do good and not evil.
19510** May you find forgiveness for yourself and forgive others.
19511** May you share freely, never taking more than you give.
19512**
19513*************************************************************************
19514**
19515** This file contains definitions of global variables and constants.
19516*/
19517/* #include "sqliteInt.h" */
19518
19519/* An array to map all upper-case characters into their corresponding
19520** lower-case character.
19521**
19522** SQLite only considers US-ASCII (or EBCDIC) characters. We do not
19523** handle case conversions for the UTF character set since the tables
19524** involved are nearly as big or bigger than SQLite itself.
19525*/
19526SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[] = {
19527#ifdef SQLITE_ASCII
19528 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
19529 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
19530 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
19531 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
19532 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
19533 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
19534 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
19535 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
19536 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
19537 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
19538 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
19539 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
19540 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
19541 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
19542 252,253,254,255
19543#endif
19544#ifdef SQLITE_EBCDIC
19545 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 0x */
19546 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
19547 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
19548 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
19549 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
19550 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
19551 96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, /* 6x */
19552 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, /* 7x */
19553 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
19554 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, /* 9x */
19555 160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
19556 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
19557 192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
19558 208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
19559 224,225,162,163,164,165,166,167,168,169,234,235,236,237,238,239, /* Ex */
19560 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255, /* Fx */
19561#endif
19562};
19563
19564/*
19565** The following 256 byte lookup table is used to support SQLites built-in
19566** equivalents to the following standard library functions:
19567**
19568** isspace() 0x01
19569** isalpha() 0x02
19570** isdigit() 0x04
19571** isalnum() 0x06
19572** isxdigit() 0x08
19573** toupper() 0x20
19574** SQLite identifier character 0x40
19575** Quote character 0x80
19576**
19577** Bit 0x20 is set if the mapped character requires translation to upper
19578** case. i.e. if the character is a lower-case ASCII character.
19579** If x is a lower-case ASCII character, then its upper-case equivalent
19580** is (x - 0x20). Therefore toupper() can be implemented as:
19581**
19582** (x & ~(map[x]&0x20))
19583**
19584** The equivalent of tolower() is implemented using the sqlite3UpperToLower[]
19585** array. tolower() is used more often than toupper() by SQLite.
19586**
19587** Bit 0x40 is set if the character is non-alphanumeric and can be used in an
19588** SQLite identifier. Identifiers are alphanumerics, "_", "$", and any
19589** non-ASCII UTF character. Hence the test for whether or not a character is
19590** part of an identifier is 0x46.
19591*/
19592#ifdef SQLITE_ASCII
19593SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = {
19594 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00..07 ........ */
19595 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, /* 08..0f ........ */
19596 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10..17 ........ */
19597 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 18..1f ........ */
19598 0x01, 0x00, 0x80, 0x00, 0x40, 0x00, 0x00, 0x80, /* 20..27 !"#$%&' */
19599 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28..2f ()*+,-./ */
19600 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, /* 30..37 01234567 */
19601 0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 38..3f 89:;<=>? */
19602
19603 0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02, /* 40..47 @ABCDEFG */
19604 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 48..4f HIJKLMNO */
19605 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 50..57 PQRSTUVW */
19606 0x02, 0x02, 0x02, 0x80, 0x00, 0x00, 0x00, 0x40, /* 58..5f XYZ[\]^_ */
19607 0x80, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22, /* 60..67 `abcdefg */
19608 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 68..6f hijklmno */
19609 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 70..77 pqrstuvw */
19610 0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, /* 78..7f xyz{|}~. */
19611
19612 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 80..87 ........ */
19613 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 88..8f ........ */
19614 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 90..97 ........ */
19615 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 98..9f ........ */
19616 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* a0..a7 ........ */
19617 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* a8..af ........ */
19618 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* b0..b7 ........ */
19619 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* b8..bf ........ */
19620
19621 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* c0..c7 ........ */
19622 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* c8..cf ........ */
19623 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* d0..d7 ........ */
19624 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* d8..df ........ */
19625 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* e0..e7 ........ */
19626 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* e8..ef ........ */
19627 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* f0..f7 ........ */
19628 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40 /* f8..ff ........ */
19629};
19630#endif
19631
19632/* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards
19633** compatibility for legacy applications, the URI filename capability is
19634** disabled by default.
19635**
19636** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
19637** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
19638**
19639** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
19640** disabled. The default value may be changed by compiling with the
19641** SQLITE_USE_URI symbol defined.
19642**
19643** URI filenames are enabled by default if SQLITE_HAS_CODEC is
19644** enabled.
19645*/
19646#ifndef SQLITE_USE_URI
19647# ifdef SQLITE_HAS_CODEC
19648# define SQLITE_USE_URI 1
19649# else
19650# define SQLITE_USE_URI 0
19651# endif
19652#endif
19653
19654/* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
19655** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if
19656** that compile-time option is omitted.
19657*/
19658#ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN
19659# define SQLITE_ALLOW_COVERING_INDEX_SCAN 1
19660#endif
19661
19662/* The minimum PMA size is set to this value multiplied by the database
19663** page size in bytes.
19664*/
19665#ifndef SQLITE_SORTER_PMASZ
19666# define SQLITE_SORTER_PMASZ 250
19667#endif
19668
19669/* Statement journals spill to disk when their size exceeds the following
19670** threshold (in bytes). 0 means that statement journals are created and
19671** written to disk immediately (the default behavior for SQLite versions
19672** before 3.12.0). -1 means always keep the entire statement journal in
19673** memory. (The statement journal is also always held entirely in memory
19674** if journal_mode=MEMORY or if temp_store=MEMORY, regardless of this
19675** setting.)
19676*/
19677#ifndef SQLITE_STMTJRNL_SPILL
19678# define SQLITE_STMTJRNL_SPILL (64*1024)
19679#endif
19680
19681/*
19682** The default lookaside-configuration, the format "SZ,N". SZ is the
19683** number of bytes in each lookaside slot (should be a multiple of 8)
19684** and N is the number of slots. The lookaside-configuration can be
19685** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
19686** or at run-time for an individual database connection using
19687** sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE);
19688*/
19689#ifndef SQLITE_DEFAULT_LOOKASIDE
19690# define SQLITE_DEFAULT_LOOKASIDE 1200,100
19691#endif
19692
19693
19694/*
19695** The following singleton contains the global configuration for
19696** the SQLite library.
19697*/
19698SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
19699 SQLITE_DEFAULT_MEMSTATUS, /* bMemstat */
19700 1, /* bCoreMutex */
19701 SQLITE_THREADSAFE==1, /* bFullMutex */
19702 SQLITE_USE_URI, /* bOpenUri */
19703 SQLITE_ALLOW_COVERING_INDEX_SCAN, /* bUseCis */
19704 0, /* bSmallMalloc */
19705 0x7ffffffe, /* mxStrlen */
19706 0, /* neverCorrupt */
19707 SQLITE_DEFAULT_LOOKASIDE, /* szLookaside, nLookaside */
19708 SQLITE_STMTJRNL_SPILL, /* nStmtSpill */
19709 {0,0,0,0,0,0,0,0}, /* m */
19710 {0,0,0,0,0,0,0,0,0}, /* mutex */
19711 {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
19712 (void*)0, /* pHeap */
19713 0, /* nHeap */
19714 0, 0, /* mnHeap, mxHeap */
19715 SQLITE_DEFAULT_MMAP_SIZE, /* szMmap */
19716 SQLITE_MAX_MMAP_SIZE, /* mxMmap */
19717 (void*)0, /* pPage */
19718 0, /* szPage */
19719 SQLITE_DEFAULT_PCACHE_INITSZ, /* nPage */
19720 0, /* mxParserStack */
19721 0, /* sharedCacheEnabled */
19722 SQLITE_SORTER_PMASZ, /* szPma */
19723 /* All the rest should always be initialized to zero */
19724 0, /* isInit */
19725 0, /* inProgress */
19726 0, /* isMutexInit */
19727 0, /* isMallocInit */
19728 0, /* isPCacheInit */
19729 0, /* nRefInitMutex */
19730 0, /* pInitMutex */
19731 0, /* xLog */
19732 0, /* pLogArg */
19733#ifdef SQLITE_ENABLE_SQLLOG
19734 0, /* xSqllog */
19735 0, /* pSqllogArg */
19736#endif
19737#ifdef SQLITE_VDBE_COVERAGE
19738 0, /* xVdbeBranch */
19739 0, /* pVbeBranchArg */
19740#endif
19741#ifndef SQLITE_UNTESTABLE
19742 0, /* xTestCallback */
19743#endif
19744 0, /* bLocaltimeFault */
19745 0, /* bInternalFunctions */
19746 0x7ffffffe, /* iOnceResetThreshold */
19747 SQLITE_DEFAULT_SORTERREF_SIZE /* szSorterRef */
19748};
19749
19750/*
19751** Hash table for global functions - functions common to all
19752** database connections. After initialization, this table is
19753** read-only.
19754*/
19755SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions;
19756
19757/*
19758** Constant tokens for values 0 and 1.
19759*/
19760SQLITE_PRIVATE const Token sqlite3IntTokens[] = {
19761 { "0", 1 },
19762 { "1", 1 }
19763};
19764
19765#ifdef VDBE_PROFILE
19766/*
19767** The following performance counter can be used in place of
19768** sqlite3Hwtime() for profiling. This is a no-op on standard builds.
19769*/
19770SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt = 0;
19771#endif
19772
19773/*
19774** The value of the "pending" byte must be 0x40000000 (1 byte past the
19775** 1-gibabyte boundary) in a compatible database. SQLite never uses
19776** the database page that contains the pending byte. It never attempts
19777** to read or write that page. The pending byte page is set aside
19778** for use by the VFS layers as space for managing file locks.
19779**
19780** During testing, it is often desirable to move the pending byte to
19781** a different position in the file. This allows code that has to
19782** deal with the pending byte to run on files that are much smaller
19783** than 1 GiB. The sqlite3_test_control() interface can be used to
19784** move the pending byte.
19785**
19786** IMPORTANT: Changing the pending byte to any value other than
19787** 0x40000000 results in an incompatible database file format!
19788** Changing the pending byte during operation will result in undefined
19789** and incorrect behavior.
19790*/
19791#ifndef SQLITE_OMIT_WSD
19792SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
19793#endif
19794
19795/* #include "opcodes.h" */
19796/*
19797** Properties of opcodes. The OPFLG_INITIALIZER macro is
19798** created by mkopcodeh.awk during compilation. Data is obtained
19799** from the comments following the "case OP_xxxx:" statements in
19800** the vdbe.c file.
19801*/
19802SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER;
19803
19804/*
19805** Name of the default collating sequence
19806*/
19807SQLITE_PRIVATE const char sqlite3StrBINARY[] = "BINARY";
19808
19809/************** End of global.c **********************************************/
19810/************** Begin file status.c ******************************************/
19811/*
19812** 2008 June 18
19813**
19814** The author disclaims copyright to this source code. In place of
19815** a legal notice, here is a blessing:
19816**
19817** May you do good and not evil.
19818** May you find forgiveness for yourself and forgive others.
19819** May you share freely, never taking more than you give.
19820**
19821*************************************************************************
19822**
19823** This module implements the sqlite3_status() interface and related
19824** functionality.
19825*/
19826/* #include "sqliteInt.h" */
19827/************** Include vdbeInt.h in the middle of status.c ******************/
19828/************** Begin file vdbeInt.h *****************************************/
19829/*
19830** 2003 September 6
19831**
19832** The author disclaims copyright to this source code. In place of
19833** a legal notice, here is a blessing:
19834**
19835** May you do good and not evil.
19836** May you find forgiveness for yourself and forgive others.
19837** May you share freely, never taking more than you give.
19838**
19839*************************************************************************
19840** This is the header file for information that is private to the
19841** VDBE. This information used to all be at the top of the single
19842** source code file "vdbe.c". When that file became too big (over
19843** 6000 lines long) it was split up into several smaller files and
19844** this header information was factored out.
19845*/
19846#ifndef SQLITE_VDBEINT_H
19847#define SQLITE_VDBEINT_H
19848
19849/*
19850** The maximum number of times that a statement will try to reparse
19851** itself before giving up and returning SQLITE_SCHEMA.
19852*/
19853#ifndef SQLITE_MAX_SCHEMA_RETRY
19854# define SQLITE_MAX_SCHEMA_RETRY 50
19855#endif
19856
19857/*
19858** VDBE_DISPLAY_P4 is true or false depending on whether or not the
19859** "explain" P4 display logic is enabled.
19860*/
19861#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
19862 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
19863# define VDBE_DISPLAY_P4 1
19864#else
19865# define VDBE_DISPLAY_P4 0
19866#endif
19867
19868/*
19869** SQL is translated into a sequence of instructions to be
19870** executed by a virtual machine. Each instruction is an instance
19871** of the following structure.
19872*/
19873typedef struct VdbeOp Op;
19874
19875/*
19876** Boolean values
19877*/
19878typedef unsigned Bool;
19879
19880/* Opaque type used by code in vdbesort.c */
19881typedef struct VdbeSorter VdbeSorter;
19882
19883/* Elements of the linked list at Vdbe.pAuxData */
19884typedef struct AuxData AuxData;
19885
19886/* Types of VDBE cursors */
19887#define CURTYPE_BTREE 0
19888#define CURTYPE_SORTER 1
19889#define CURTYPE_VTAB 2
19890#define CURTYPE_PSEUDO 3
19891
19892/*
19893** A VdbeCursor is an superclass (a wrapper) for various cursor objects:
19894**
19895** * A b-tree cursor
19896** - In the main database or in an ephemeral database
19897** - On either an index or a table
19898** * A sorter
19899** * A virtual table
19900** * A one-row "pseudotable" stored in a single register
19901*/
19902typedef struct VdbeCursor VdbeCursor;
19903struct VdbeCursor {
19904 u8 eCurType; /* One of the CURTYPE_* values above */
19905 i8 iDb; /* Index of cursor database in db->aDb[] (or -1) */
19906 u8 nullRow; /* True if pointing to a row with no data */
19907 u8 deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */
19908 u8 isTable; /* True for rowid tables. False for indexes */
19909#ifdef SQLITE_DEBUG
19910 u8 seekOp; /* Most recent seek operation on this cursor */
19911 u8 wrFlag; /* The wrFlag argument to sqlite3BtreeCursor() */
19912#endif
19913 Bool isEphemeral:1; /* True for an ephemeral table */
19914 Bool useRandomRowid:1; /* Generate new record numbers semi-randomly */
19915 Bool isOrdered:1; /* True if the table is not BTREE_UNORDERED */
19916 Bool seekHit:1; /* See the OP_SeekHit and OP_IfNoHope opcodes */
19917 Btree *pBtx; /* Separate file holding temporary table */
19918 i64 seqCount; /* Sequence counter */
19919 int *aAltMap; /* Mapping from table to index column numbers */
19920
19921 /* Cached OP_Column parse information is only valid if cacheStatus matches
19922 ** Vdbe.cacheCtr. Vdbe.cacheCtr will never take on the value of
19923 ** CACHE_STALE (0) and so setting cacheStatus=CACHE_STALE guarantees that
19924 ** the cache is out of date. */
19925 u32 cacheStatus; /* Cache is valid if this matches Vdbe.cacheCtr */
19926 int seekResult; /* Result of previous sqlite3BtreeMoveto() or 0
19927 ** if there have been no prior seeks on the cursor. */
19928 /* seekResult does not distinguish between "no seeks have ever occurred
19929 ** on this cursor" and "the most recent seek was an exact match".
19930 ** For CURTYPE_PSEUDO, seekResult is the register holding the record */
19931
19932 /* When a new VdbeCursor is allocated, only the fields above are zeroed.
19933 ** The fields that follow are uninitialized, and must be individually
19934 ** initialized prior to first use. */
19935 VdbeCursor *pAltCursor; /* Associated index cursor from which to read */
19936 union {
19937 BtCursor *pCursor; /* CURTYPE_BTREE or _PSEUDO. Btree cursor */
19938 sqlite3_vtab_cursor *pVCur; /* CURTYPE_VTAB. Vtab cursor */
19939 VdbeSorter *pSorter; /* CURTYPE_SORTER. Sorter object */
19940 } uc;
19941 KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */
19942 u32 iHdrOffset; /* Offset to next unparsed byte of the header */
19943 Pgno pgnoRoot; /* Root page of the open btree cursor */
19944 i16 nField; /* Number of fields in the header */
19945 u16 nHdrParsed; /* Number of header fields parsed so far */
19946 i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
19947 u32 *aOffset; /* Pointer to aType[nField] */
19948 const u8 *aRow; /* Data for the current row, if all on one page */
19949 u32 payloadSize; /* Total number of bytes in the record */
19950 u32 szRow; /* Byte available in aRow */
19951#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
19952 u64 maskUsed; /* Mask of columns used by this cursor */
19953#endif
19954
19955 /* 2*nField extra array elements allocated for aType[], beyond the one
19956 ** static element declared in the structure. nField total array slots for
19957 ** aType[] and nField+1 array slots for aOffset[] */
19958 u32 aType[1]; /* Type values record decode. MUST BE LAST */
19959};
19960
19961
19962/*
19963** A value for VdbeCursor.cacheStatus that means the cache is always invalid.
19964*/
19965#define CACHE_STALE 0
19966
19967/*
19968** When a sub-program is executed (OP_Program), a structure of this type
19969** is allocated to store the current value of the program counter, as
19970** well as the current memory cell array and various other frame specific
19971** values stored in the Vdbe struct. When the sub-program is finished,
19972** these values are copied back to the Vdbe from the VdbeFrame structure,
19973** restoring the state of the VM to as it was before the sub-program
19974** began executing.
19975**
19976** The memory for a VdbeFrame object is allocated and managed by a memory
19977** cell in the parent (calling) frame. When the memory cell is deleted or
19978** overwritten, the VdbeFrame object is not freed immediately. Instead, it
19979** is linked into the Vdbe.pDelFrame list. The contents of the Vdbe.pDelFrame
19980** list is deleted when the VM is reset in VdbeHalt(). The reason for doing
19981** this instead of deleting the VdbeFrame immediately is to avoid recursive
19982** calls to sqlite3VdbeMemRelease() when the memory cells belonging to the
19983** child frame are released.
19984**
19985** The currently executing frame is stored in Vdbe.pFrame. Vdbe.pFrame is
19986** set to NULL if the currently executing frame is the main program.
19987*/
19988typedef struct VdbeFrame VdbeFrame;
19989struct VdbeFrame {
19990 Vdbe *v; /* VM this frame belongs to */
19991 VdbeFrame *pParent; /* Parent of this frame, or NULL if parent is main */
19992 Op *aOp; /* Program instructions for parent frame */
19993 i64 *anExec; /* Event counters from parent frame */
19994 Mem *aMem; /* Array of memory cells for parent frame */
19995 VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */
19996 u8 *aOnce; /* Bitmask used by OP_Once */
19997 void *token; /* Copy of SubProgram.token */
19998 i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */
19999 AuxData *pAuxData; /* Linked list of auxdata allocations */
20000#if SQLITE_DEBUG
20001 u32 iFrameMagic; /* magic number for sanity checking */
20002#endif
20003 int nCursor; /* Number of entries in apCsr */
20004 int pc; /* Program Counter in parent (calling) frame */
20005 int nOp; /* Size of aOp array */
20006 int nMem; /* Number of entries in aMem */
20007 int nChildMem; /* Number of memory cells for child frame */
20008 int nChildCsr; /* Number of cursors for child frame */
20009 int nChange; /* Statement changes (Vdbe.nChange) */
20010 int nDbChange; /* Value of db->nChange */
20011};
20012
20013/* Magic number for sanity checking on VdbeFrame objects */
20014#define SQLITE_FRAME_MAGIC 0x879fb71e
20015
20016/*
20017** Return a pointer to the array of registers allocated for use
20018** by a VdbeFrame.
20019*/
20020#define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))])
20021
20022/*
20023** Internally, the vdbe manipulates nearly all SQL values as Mem
20024** structures. Each Mem struct may cache multiple representations (string,
20025** integer etc.) of the same value.
20026*/
20027struct sqlite3_value {
20028 union MemValue {
20029 double r; /* Real value used when MEM_Real is set in flags */
20030 i64 i; /* Integer value used when MEM_Int is set in flags */
20031 int nZero; /* Extra zero bytes when MEM_Zero and MEM_Blob set */
20032 const char *zPType; /* Pointer type when MEM_Term|MEM_Subtype|MEM_Null */
20033 FuncDef *pDef; /* Used only when flags==MEM_Agg */
20034 } u;
20035 u16 flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
20036 u8 enc; /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
20037 u8 eSubtype; /* Subtype for this value */
20038 int n; /* Number of characters in string value, excluding '\0' */
20039 char *z; /* String or BLOB value */
20040 /* ShallowCopy only needs to copy the information above */
20041 char *zMalloc; /* Space to hold MEM_Str or MEM_Blob if szMalloc>0 */
20042 int szMalloc; /* Size of the zMalloc allocation */
20043 u32 uTemp; /* Transient storage for serial_type in OP_MakeRecord */
20044 sqlite3 *db; /* The associated database connection */
20045 void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
20046#ifdef SQLITE_DEBUG
20047 Mem *pScopyFrom; /* This Mem is a shallow copy of pScopyFrom */
20048 u16 mScopyFlags; /* flags value immediately after the shallow copy */
20049#endif
20050};
20051
20052/*
20053** Size of struct Mem not including the Mem.zMalloc member or anything that
20054** follows.
20055*/
20056#define MEMCELLSIZE offsetof(Mem,zMalloc)
20057
20058/* One or more of the following flags are set to indicate the validOK
20059** representations of the value stored in the Mem struct.
20060**
20061** If the MEM_Null flag is set, then the value is an SQL NULL value.
20062** For a pointer type created using sqlite3_bind_pointer() or
20063** sqlite3_result_pointer() the MEM_Term and MEM_Subtype flags are also set.
20064**
20065** If the MEM_Str flag is set then Mem.z points at a string representation.
20066** Usually this is encoded in the same unicode encoding as the main
20067** database (see below for exceptions). If the MEM_Term flag is also
20068** set, then the string is nul terminated. The MEM_Int and MEM_Real
20069** flags may coexist with the MEM_Str flag.
20070*/
20071#define MEM_Null 0x0001 /* Value is NULL (or a pointer) */
20072#define MEM_Str 0x0002 /* Value is a string */
20073#define MEM_Int 0x0004 /* Value is an integer */
20074#define MEM_Real 0x0008 /* Value is a real number */
20075#define MEM_Blob 0x0010 /* Value is a BLOB */
20076#define MEM_AffMask 0x001f /* Mask of affinity bits */
20077/* Available 0x0020 */
20078/* Available 0x0040 */
20079#define MEM_Undefined 0x0080 /* Value is undefined */
20080#define MEM_Cleared 0x0100 /* NULL set by OP_Null, not from data */
20081#define MEM_TypeMask 0xc1ff /* Mask of type bits */
20082
20083
20084/* Whenever Mem contains a valid string or blob representation, one of
20085** the following flags must be set to determine the memory management
20086** policy for Mem.z. The MEM_Term flag tells us whether or not the
20087** string is \000 or \u0000 terminated
20088*/
20089#define MEM_Term 0x0200 /* String in Mem.z is zero terminated */
20090#define MEM_Dyn 0x0400 /* Need to call Mem.xDel() on Mem.z */
20091#define MEM_Static 0x0800 /* Mem.z points to a static string */
20092#define MEM_Ephem 0x1000 /* Mem.z points to an ephemeral string */
20093#define MEM_Agg 0x2000 /* Mem.z points to an agg function context */
20094#define MEM_Zero 0x4000 /* Mem.i contains count of 0s appended to blob */
20095#define MEM_Subtype 0x8000 /* Mem.eSubtype is valid */
20096#ifdef SQLITE_OMIT_INCRBLOB
20097 #undef MEM_Zero
20098 #define MEM_Zero 0x0000
20099#endif
20100
20101/* Return TRUE if Mem X contains dynamically allocated content - anything
20102** that needs to be deallocated to avoid a leak.
20103*/
20104#define VdbeMemDynamic(X) \
20105 (((X)->flags&(MEM_Agg|MEM_Dyn))!=0)
20106
20107/*
20108** Clear any existing type flags from a Mem and replace them with f
20109*/
20110#define MemSetTypeFlag(p, f) \
20111 ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
20112
20113/*
20114** Return true if a memory cell is not marked as invalid. This macro
20115** is for use inside assert() statements only.
20116*/
20117#ifdef SQLITE_DEBUG
20118#define memIsValid(M) ((M)->flags & MEM_Undefined)==0
20119#endif
20120
20121/*
20122** Each auxiliary data pointer stored by a user defined function
20123** implementation calling sqlite3_set_auxdata() is stored in an instance
20124** of this structure. All such structures associated with a single VM
20125** are stored in a linked list headed at Vdbe.pAuxData. All are destroyed
20126** when the VM is halted (if not before).
20127*/
20128struct AuxData {
20129 int iAuxOp; /* Instruction number of OP_Function opcode */
20130 int iAuxArg; /* Index of function argument. */
20131 void *pAux; /* Aux data pointer */
20132 void (*xDeleteAux)(void*); /* Destructor for the aux data */
20133 AuxData *pNextAux; /* Next element in list */
20134};
20135
20136/*
20137** The "context" argument for an installable function. A pointer to an
20138** instance of this structure is the first argument to the routines used
20139** implement the SQL functions.
20140**
20141** There is a typedef for this structure in sqlite.h. So all routines,
20142** even the public interface to SQLite, can use a pointer to this structure.
20143** But this file is the only place where the internal details of this
20144** structure are known.
20145**
20146** This structure is defined inside of vdbeInt.h because it uses substructures
20147** (Mem) which are only defined there.
20148*/
20149struct sqlite3_context {
20150 Mem *pOut; /* The return value is stored here */
20151 FuncDef *pFunc; /* Pointer to function information */
20152 Mem *pMem; /* Memory cell used to store aggregate context */
20153 Vdbe *pVdbe; /* The VM that owns this context */
20154 int iOp; /* Instruction number of OP_Function */
20155 int isError; /* Error code returned by the function. */
20156 u8 skipFlag; /* Skip accumulator loading if true */
20157 u8 argc; /* Number of arguments */
20158 sqlite3_value *argv[1]; /* Argument set */
20159};
20160
20161/* A bitfield type for use inside of structures. Always follow with :N where
20162** N is the number of bits.
20163*/
20164typedef unsigned bft; /* Bit Field Type */
20165
20166typedef struct ScanStatus ScanStatus;
20167struct ScanStatus {
20168 int addrExplain; /* OP_Explain for loop */
20169 int addrLoop; /* Address of "loops" counter */
20170 int addrVisit; /* Address of "rows visited" counter */
20171 int iSelectID; /* The "Select-ID" for this loop */
20172 LogEst nEst; /* Estimated output rows per loop */
20173 char *zName; /* Name of table or index */
20174};
20175
20176/*
20177** An instance of the virtual machine. This structure contains the complete
20178** state of the virtual machine.
20179**
20180** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
20181** is really a pointer to an instance of this structure.
20182*/
20183struct Vdbe {
20184 sqlite3 *db; /* The database connection that owns this statement */
20185 Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */
20186 Parse *pParse; /* Parsing context used to create this Vdbe */
20187 ynVar nVar; /* Number of entries in aVar[] */
20188 u32 magic; /* Magic number for sanity checking */
20189 int nMem; /* Number of memory locations currently allocated */
20190 int nCursor; /* Number of slots in apCsr[] */
20191 u32 cacheCtr; /* VdbeCursor row cache generation counter */
20192 int pc; /* The program counter */
20193 int rc; /* Value to return */
20194 int nChange; /* Number of db changes made since last reset */
20195 int iStatement; /* Statement number (or 0 if has not opened stmt) */
20196 i64 iCurrentTime; /* Value of julianday('now') for this statement */
20197 i64 nFkConstraint; /* Number of imm. FK constraints this VM */
20198 i64 nStmtDefCons; /* Number of def. constraints when stmt started */
20199 i64 nStmtDefImmCons; /* Number of def. imm constraints when stmt started */
20200
20201 /* When allocating a new Vdbe object, all of the fields below should be
20202 ** initialized to zero or NULL */
20203
20204 Op *aOp; /* Space to hold the virtual machine's program */
20205 Mem *aMem; /* The memory locations */
20206 Mem **apArg; /* Arguments to currently executing user function */
20207 Mem *aColName; /* Column names to return */
20208 Mem *pResultSet; /* Pointer to an array of results */
20209 char *zErrMsg; /* Error message written here */
20210 VdbeCursor **apCsr; /* One element of this array for each open cursor */
20211 Mem *aVar; /* Values for the OP_Variable opcode. */
20212 VList *pVList; /* Name of variables */
20213#ifndef SQLITE_OMIT_TRACE
20214 i64 startTime; /* Time when query started - used for profiling */
20215#endif
20216 int nOp; /* Number of instructions in the program */
20217#ifdef SQLITE_DEBUG
20218 int rcApp; /* errcode set by sqlite3_result_error_code() */
20219 u32 nWrite; /* Number of write operations that have occurred */
20220#endif
20221 u16 nResColumn; /* Number of columns in one row of the result set */
20222 u8 errorAction; /* Recovery action to do in case of an error */
20223 u8 minWriteFileFormat; /* Minimum file format for writable database files */
20224 u8 prepFlags; /* SQLITE_PREPARE_* flags */
20225 bft expired:2; /* 1: recompile VM immediately 2: when convenient */
20226 bft explain:2; /* True if EXPLAIN present on SQL command */
20227 bft doingRerun:1; /* True if rerunning after an auto-reprepare */
20228 bft changeCntOn:1; /* True to update the change-counter */
20229 bft runOnlyOnce:1; /* Automatically expire on reset */
20230 bft usesStmtJournal:1; /* True if uses a statement journal */
20231 bft readOnly:1; /* True for statements that do not write */
20232 bft bIsReader:1; /* True for statements that read */
20233 yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
20234 yDbMask lockMask; /* Subset of btreeMask that requires a lock */
20235 u32 aCounter[7]; /* Counters used by sqlite3_stmt_status() */
20236 char *zSql; /* Text of the SQL statement that generated this */
20237#ifdef SQLITE_ENABLE_NORMALIZE
20238 char *zNormSql; /* Normalization of the associated SQL statement */
20239#endif
20240 void *pFree; /* Free this when deleting the vdbe */
20241 VdbeFrame *pFrame; /* Parent frame */
20242 VdbeFrame *pDelFrame; /* List of frame objects to free on VM reset */
20243 int nFrame; /* Number of frames in pFrame list */
20244 u32 expmask; /* Binding to these vars invalidates VM */
20245 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
20246 AuxData *pAuxData; /* Linked list of auxdata allocations */
20247#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
20248 i64 *anExec; /* Number of times each op has been executed */
20249 int nScan; /* Entries in aScan[] */
20250 ScanStatus *aScan; /* Scan definitions for sqlite3_stmt_scanstatus() */
20251#endif
20252};
20253
20254/*
20255** The following are allowed values for Vdbe.magic
20256*/
20257#define VDBE_MAGIC_INIT 0x16bceaa5 /* Building a VDBE program */
20258#define VDBE_MAGIC_RUN 0x2df20da3 /* VDBE is ready to execute */
20259#define VDBE_MAGIC_HALT 0x319c2973 /* VDBE has completed execution */
20260#define VDBE_MAGIC_RESET 0x48fa9f76 /* Reset and ready to run again */
20261#define VDBE_MAGIC_DEAD 0x5606c3c8 /* The VDBE has been deallocated */
20262
20263/*
20264** Structure used to store the context required by the
20265** sqlite3_preupdate_*() API functions.
20266*/
20267struct PreUpdate {
20268 Vdbe *v;
20269 VdbeCursor *pCsr; /* Cursor to read old values from */
20270 int op; /* One of SQLITE_INSERT, UPDATE, DELETE */
20271 u8 *aRecord; /* old.* database record */
20272 KeyInfo keyinfo;
20273 UnpackedRecord *pUnpacked; /* Unpacked version of aRecord[] */
20274 UnpackedRecord *pNewUnpacked; /* Unpacked version of new.* record */
20275 int iNewReg; /* Register for new.* values */
20276 i64 iKey1; /* First key value passed to hook */
20277 i64 iKey2; /* Second key value passed to hook */
20278 Mem *aNew; /* Array of new.* values */
20279 Table *pTab; /* Schema object being upated */
20280 Index *pPk; /* PK index if pTab is WITHOUT ROWID */
20281};
20282
20283/*
20284** Function prototypes
20285*/
20286SQLITE_PRIVATE void sqlite3VdbeError(Vdbe*, const char *, ...);
20287SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
20288void sqliteVdbePopStack(Vdbe*,int);
20289SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor**, int*);
20290SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor*);
20291SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
20292SQLITE_PRIVATE u8 sqlite3VdbeOneByteSerialTypeLen(u8);
20293SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int, u32*);
20294SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
20295SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
20296SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3*, AuxData**, int, int);
20297
20298int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
20299SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*);
20300SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*);
20301SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
20302#ifndef SQLITE_OMIT_EXPLAIN
20303SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
20304#endif
20305SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
20306SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
20307SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
20308SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*);
20309SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
20310SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*);
20311SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*);
20312SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*));
20313SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64);
20314#ifdef SQLITE_OMIT_FLOATING_POINT
20315# define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
20316#else
20317SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem*, double);
20318#endif
20319SQLITE_PRIVATE void sqlite3VdbeMemSetPointer(Mem*, void*, const char*, void(*)(void*));
20320SQLITE_PRIVATE void sqlite3VdbeMemInit(Mem*,sqlite3*,u16);
20321SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem*);
20322SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int);
20323#ifdef SQLITE_DEBUG
20324SQLITE_PRIVATE int sqlite3VdbeMemIsRowSet(const Mem*);
20325#endif
20326SQLITE_PRIVATE int sqlite3VdbeMemSetRowSet(Mem*);
20327SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
20328SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, u8, u8);
20329SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*);
20330SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
20331SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
20332SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem*, int ifNull);
20333SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
20334SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
20335SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
20336SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem*,u8,u8);
20337SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*);
20338SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
20339SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
20340#ifndef SQLITE_OMIT_WINDOWFUNC
20341SQLITE_PRIVATE int sqlite3VdbeMemAggValue(Mem*, Mem*, FuncDef*);
20342#endif
20343#ifndef SQLITE_OMIT_EXPLAIN
20344SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
20345#endif
20346SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
20347SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int n);
20348SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
20349#ifdef SQLITE_DEBUG
20350SQLITE_PRIVATE int sqlite3VdbeFrameIsValid(VdbeFrame*);
20351#endif
20352SQLITE_PRIVATE void sqlite3VdbeFrameMemDel(void*); /* Destructor on Mem */
20353SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*); /* Actually deletes the Frame */
20354SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
20355#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
20356SQLITE_PRIVATE void sqlite3VdbePreUpdateHook(Vdbe*,VdbeCursor*,int,const char*,Table*,i64,int);
20357#endif
20358SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p);
20359
20360SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, int, VdbeCursor *);
20361SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *, VdbeSorter *);
20362SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
20363SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
20364SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *);
20365SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *, int *);
20366SQLITE_PRIVATE int sqlite3VdbeSorterWrite(const VdbeCursor *, Mem *);
20367SQLITE_PRIVATE int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *);
20368
20369#ifdef SQLITE_DEBUG
20370SQLITE_PRIVATE void sqlite3VdbeIncrWriteCounter(Vdbe*, VdbeCursor*);
20371SQLITE_PRIVATE void sqlite3VdbeAssertAbortable(Vdbe*);
20372#else
20373# define sqlite3VdbeIncrWriteCounter(V,C)
20374# define sqlite3VdbeAssertAbortable(V)
20375#endif
20376
20377#if !defined(SQLITE_OMIT_SHARED_CACHE)
20378SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe*);
20379#else
20380# define sqlite3VdbeEnter(X)
20381#endif
20382
20383#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
20384SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe*);
20385#else
20386# define sqlite3VdbeLeave(X)
20387#endif
20388
20389#ifdef SQLITE_DEBUG
20390SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
20391SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem*);
20392#endif
20393
20394#ifndef SQLITE_OMIT_FOREIGN_KEY
20395SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
20396#else
20397# define sqlite3VdbeCheckFk(p,i) 0
20398#endif
20399
20400#ifdef SQLITE_DEBUG
20401SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe*);
20402SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
20403#endif
20404#ifndef SQLITE_OMIT_UTF16
20405SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8);
20406SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem);
20407#endif
20408
20409#ifndef SQLITE_OMIT_INCRBLOB
20410SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *);
20411 #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
20412#else
20413 #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
20414 #define ExpandBlob(P) SQLITE_OK
20415#endif
20416
20417#endif /* !defined(SQLITE_VDBEINT_H) */
20418
20419/************** End of vdbeInt.h *********************************************/
20420/************** Continuing where we left off in status.c *********************/
20421
20422/*
20423** Variables in which to record status information.
20424*/
20425#if SQLITE_PTRSIZE>4
20426typedef sqlite3_int64 sqlite3StatValueType;
20427#else
20428typedef u32 sqlite3StatValueType;
20429#endif
20430typedef struct sqlite3StatType sqlite3StatType;
20431static SQLITE_WSD struct sqlite3StatType {
20432 sqlite3StatValueType nowValue[10]; /* Current value */
20433 sqlite3StatValueType mxValue[10]; /* Maximum value */
20434} sqlite3Stat = { {0,}, {0,} };
20435
20436/*
20437** Elements of sqlite3Stat[] are protected by either the memory allocator
20438** mutex, or by the pcache1 mutex. The following array determines which.
20439*/
20440static const char statMutex[] = {
20441 0, /* SQLITE_STATUS_MEMORY_USED */
20442 1, /* SQLITE_STATUS_PAGECACHE_USED */
20443 1, /* SQLITE_STATUS_PAGECACHE_OVERFLOW */
20444 0, /* SQLITE_STATUS_SCRATCH_USED */
20445 0, /* SQLITE_STATUS_SCRATCH_OVERFLOW */
20446 0, /* SQLITE_STATUS_MALLOC_SIZE */
20447 0, /* SQLITE_STATUS_PARSER_STACK */
20448 1, /* SQLITE_STATUS_PAGECACHE_SIZE */
20449 0, /* SQLITE_STATUS_SCRATCH_SIZE */
20450 0, /* SQLITE_STATUS_MALLOC_COUNT */
20451};
20452
20453
20454/* The "wsdStat" macro will resolve to the status information
20455** state vector. If writable static data is unsupported on the target,
20456** we have to locate the state vector at run-time. In the more common
20457** case where writable static data is supported, wsdStat can refer directly
20458** to the "sqlite3Stat" state vector declared above.
20459*/
20460#ifdef SQLITE_OMIT_WSD
20461# define wsdStatInit sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)
20462# define wsdStat x[0]
20463#else
20464# define wsdStatInit
20465# define wsdStat sqlite3Stat
20466#endif
20467
20468/*
20469** Return the current value of a status parameter. The caller must
20470** be holding the appropriate mutex.
20471*/
20472SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int op){
20473 wsdStatInit;
20474 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
20475 assert( op>=0 && op<ArraySize(statMutex) );
20476 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
20477 : sqlite3MallocMutex()) );
20478 return wsdStat.nowValue[op];
20479}
20480
20481/*
20482** Add N to the value of a status record. The caller must hold the
20483** appropriate mutex. (Locking is checked by assert()).
20484**
20485** The StatusUp() routine can accept positive or negative values for N.
20486** The value of N is added to the current status value and the high-water
20487** mark is adjusted if necessary.
20488**
20489** The StatusDown() routine lowers the current value by N. The highwater
20490** mark is unchanged. N must be non-negative for StatusDown().
20491*/
20492SQLITE_PRIVATE void sqlite3StatusUp(int op, int N){
20493 wsdStatInit;
20494 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
20495 assert( op>=0 && op<ArraySize(statMutex) );
20496 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
20497 : sqlite3MallocMutex()) );
20498 wsdStat.nowValue[op] += N;
20499 if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
20500 wsdStat.mxValue[op] = wsdStat.nowValue[op];
20501 }
20502}
20503SQLITE_PRIVATE void sqlite3StatusDown(int op, int N){
20504 wsdStatInit;
20505 assert( N>=0 );
20506 assert( op>=0 && op<ArraySize(statMutex) );
20507 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
20508 : sqlite3MallocMutex()) );
20509 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
20510 wsdStat.nowValue[op] -= N;
20511}
20512
20513/*
20514** Adjust the highwater mark if necessary.
20515** The caller must hold the appropriate mutex.
20516*/
20517SQLITE_PRIVATE void sqlite3StatusHighwater(int op, int X){
20518 sqlite3StatValueType newValue;
20519 wsdStatInit;
20520 assert( X>=0 );
20521 newValue = (sqlite3StatValueType)X;
20522 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
20523 assert( op>=0 && op<ArraySize(statMutex) );
20524 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
20525 : sqlite3MallocMutex()) );
20526 assert( op==SQLITE_STATUS_MALLOC_SIZE
20527 || op==SQLITE_STATUS_PAGECACHE_SIZE
20528 || op==SQLITE_STATUS_PARSER_STACK );
20529 if( newValue>wsdStat.mxValue[op] ){
20530 wsdStat.mxValue[op] = newValue;
20531 }
20532}
20533
20534/*
20535** Query status information.
20536*/
20537SQLITE_API int sqlite3_status64(
20538 int op,
20539 sqlite3_int64 *pCurrent,
20540 sqlite3_int64 *pHighwater,
20541 int resetFlag
20542){
20543 sqlite3_mutex *pMutex;
20544 wsdStatInit;
20545 if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
20546 return SQLITE_MISUSE_BKPT;
20547 }
20548#ifdef SQLITE_ENABLE_API_ARMOR
20549 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
20550#endif
20551 pMutex = statMutex[op] ? sqlite3Pcache1Mutex() : sqlite3MallocMutex();
20552 sqlite3_mutex_enter(pMutex);
20553 *pCurrent = wsdStat.nowValue[op];
20554 *pHighwater = wsdStat.mxValue[op];
20555 if( resetFlag ){
20556 wsdStat.mxValue[op] = wsdStat.nowValue[op];
20557 }
20558 sqlite3_mutex_leave(pMutex);
20559 (void)pMutex; /* Prevent warning when SQLITE_THREADSAFE=0 */
20560 return SQLITE_OK;
20561}
20562SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
20563 sqlite3_int64 iCur = 0, iHwtr = 0;
20564 int rc;
20565#ifdef SQLITE_ENABLE_API_ARMOR
20566 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
20567#endif
20568 rc = sqlite3_status64(op, &iCur, &iHwtr, resetFlag);
20569 if( rc==0 ){
20570 *pCurrent = (int)iCur;
20571 *pHighwater = (int)iHwtr;
20572 }
20573 return rc;
20574}
20575
20576/*
20577** Return the number of LookasideSlot elements on the linked list
20578*/
20579static u32 countLookasideSlots(LookasideSlot *p){
20580 u32 cnt = 0;
20581 while( p ){
20582 p = p->pNext;
20583 cnt++;
20584 }
20585 return cnt;
20586}
20587
20588/*
20589** Count the number of slots of lookaside memory that are outstanding
20590*/
20591SQLITE_PRIVATE int sqlite3LookasideUsed(sqlite3 *db, int *pHighwater){
20592 u32 nInit = countLookasideSlots(db->lookaside.pInit);
20593 u32 nFree = countLookasideSlots(db->lookaside.pFree);
20594 if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit;
20595 return db->lookaside.nSlot - (nInit+nFree);
20596}
20597
20598/*
20599** Query status information for a single database connection
20600*/
20601SQLITE_API int sqlite3_db_status(
20602 sqlite3 *db, /* The database connection whose status is desired */
20603 int op, /* Status verb */
20604 int *pCurrent, /* Write current value here */
20605 int *pHighwater, /* Write high-water mark here */
20606 int resetFlag /* Reset high-water mark if true */
20607){
20608 int rc = SQLITE_OK; /* Return code */
20609#ifdef SQLITE_ENABLE_API_ARMOR
20610 if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){
20611 return SQLITE_MISUSE_BKPT;
20612 }
20613#endif
20614 sqlite3_mutex_enter(db->mutex);
20615 switch( op ){
20616 case SQLITE_DBSTATUS_LOOKASIDE_USED: {
20617 *pCurrent = sqlite3LookasideUsed(db, pHighwater);
20618 if( resetFlag ){
20619 LookasideSlot *p = db->lookaside.pFree;
20620 if( p ){
20621 while( p->pNext ) p = p->pNext;
20622 p->pNext = db->lookaside.pInit;
20623 db->lookaside.pInit = db->lookaside.pFree;
20624 db->lookaside.pFree = 0;
20625 }
20626 }
20627 break;
20628 }
20629
20630 case SQLITE_DBSTATUS_LOOKASIDE_HIT:
20631 case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE:
20632 case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: {
20633 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
20634 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
20635 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
20636 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
20637 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
20638 *pCurrent = 0;
20639 *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
20640 if( resetFlag ){
20641 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
20642 }
20643 break;
20644 }
20645
20646 /*
20647 ** Return an approximation for the amount of memory currently used
20648 ** by all pagers associated with the given database connection. The
20649 ** highwater mark is meaningless and is returned as zero.
20650 */
20651 case SQLITE_DBSTATUS_CACHE_USED_SHARED:
20652 case SQLITE_DBSTATUS_CACHE_USED: {
20653 int totalUsed = 0;
20654 int i;
20655 sqlite3BtreeEnterAll(db);
20656 for(i=0; i<db->nDb; i++){
20657 Btree *pBt = db->aDb[i].pBt;
20658 if( pBt ){
20659 Pager *pPager = sqlite3BtreePager(pBt);
20660 int nByte = sqlite3PagerMemUsed(pPager);
20661 if( op==SQLITE_DBSTATUS_CACHE_USED_SHARED ){
20662 nByte = nByte / sqlite3BtreeConnectionCount(pBt);
20663 }
20664 totalUsed += nByte;
20665 }
20666 }
20667 sqlite3BtreeLeaveAll(db);
20668 *pCurrent = totalUsed;
20669 *pHighwater = 0;
20670 break;
20671 }
20672
20673 /*
20674 ** *pCurrent gets an accurate estimate of the amount of memory used
20675 ** to store the schema for all databases (main, temp, and any ATTACHed
20676 ** databases. *pHighwater is set to zero.
20677 */
20678 case SQLITE_DBSTATUS_SCHEMA_USED: {
20679 int i; /* Used to iterate through schemas */
20680 int nByte = 0; /* Used to accumulate return value */
20681
20682 sqlite3BtreeEnterAll(db);
20683 db->pnBytesFreed = &nByte;
20684 for(i=0; i<db->nDb; i++){
20685 Schema *pSchema = db->aDb[i].pSchema;
20686 if( ALWAYS(pSchema!=0) ){
20687 HashElem *p;
20688
20689 nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * (
20690 pSchema->tblHash.count
20691 + pSchema->trigHash.count
20692 + pSchema->idxHash.count
20693 + pSchema->fkeyHash.count
20694 );
20695 nByte += sqlite3_msize(pSchema->tblHash.ht);
20696 nByte += sqlite3_msize(pSchema->trigHash.ht);
20697 nByte += sqlite3_msize(pSchema->idxHash.ht);
20698 nByte += sqlite3_msize(pSchema->fkeyHash.ht);
20699
20700 for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
20701 sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
20702 }
20703 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
20704 sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
20705 }
20706 }
20707 }
20708 db->pnBytesFreed = 0;
20709 sqlite3BtreeLeaveAll(db);
20710
20711 *pHighwater = 0;
20712 *pCurrent = nByte;
20713 break;
20714 }
20715
20716 /*
20717 ** *pCurrent gets an accurate estimate of the amount of memory used
20718 ** to store all prepared statements.
20719 ** *pHighwater is set to zero.
20720 */
20721 case SQLITE_DBSTATUS_STMT_USED: {
20722 struct Vdbe *pVdbe; /* Used to iterate through VMs */
20723 int nByte = 0; /* Used to accumulate return value */
20724
20725 db->pnBytesFreed = &nByte;
20726 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
20727 sqlite3VdbeClearObject(db, pVdbe);
20728 sqlite3DbFree(db, pVdbe);
20729 }
20730 db->pnBytesFreed = 0;
20731
20732 *pHighwater = 0; /* IMP: R-64479-57858 */
20733 *pCurrent = nByte;
20734
20735 break;
20736 }
20737
20738 /*
20739 ** Set *pCurrent to the total cache hits or misses encountered by all
20740 ** pagers the database handle is connected to. *pHighwater is always set
20741 ** to zero.
20742 */
20743 case SQLITE_DBSTATUS_CACHE_SPILL:
20744 op = SQLITE_DBSTATUS_CACHE_WRITE+1;
20745 /* Fall through into the next case */
20746 case SQLITE_DBSTATUS_CACHE_HIT:
20747 case SQLITE_DBSTATUS_CACHE_MISS:
20748 case SQLITE_DBSTATUS_CACHE_WRITE:{
20749 int i;
20750 int nRet = 0;
20751 assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
20752 assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
20753
20754 for(i=0; i<db->nDb; i++){
20755 if( db->aDb[i].pBt ){
20756 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
20757 sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
20758 }
20759 }
20760 *pHighwater = 0; /* IMP: R-42420-56072 */
20761 /* IMP: R-54100-20147 */
20762 /* IMP: R-29431-39229 */
20763 *pCurrent = nRet;
20764 break;
20765 }
20766
20767 /* Set *pCurrent to non-zero if there are unresolved deferred foreign
20768 ** key constraints. Set *pCurrent to zero if all foreign key constraints
20769 ** have been satisfied. The *pHighwater is always set to zero.
20770 */
20771 case SQLITE_DBSTATUS_DEFERRED_FKS: {
20772 *pHighwater = 0; /* IMP: R-11967-56545 */
20773 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
20774 break;
20775 }
20776
20777 default: {
20778 rc = SQLITE_ERROR;
20779 }
20780 }
20781 sqlite3_mutex_leave(db->mutex);
20782 return rc;
20783}
20784
20785/************** End of status.c **********************************************/
20786/************** Begin file date.c ********************************************/
20787/*
20788** 2003 October 31
20789**
20790** The author disclaims copyright to this source code. In place of
20791** a legal notice, here is a blessing:
20792**
20793** May you do good and not evil.
20794** May you find forgiveness for yourself and forgive others.
20795** May you share freely, never taking more than you give.
20796**
20797*************************************************************************
20798** This file contains the C functions that implement date and time
20799** functions for SQLite.
20800**
20801** There is only one exported symbol in this file - the function
20802** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
20803** All other code has file scope.
20804**
20805** SQLite processes all times and dates as julian day numbers. The
20806** dates and times are stored as the number of days since noon
20807** in Greenwich on November 24, 4714 B.C. according to the Gregorian
20808** calendar system.
20809**
20810** 1970-01-01 00:00:00 is JD 2440587.5
20811** 2000-01-01 00:00:00 is JD 2451544.5
20812**
20813** This implementation requires years to be expressed as a 4-digit number
20814** which means that only dates between 0000-01-01 and 9999-12-31 can
20815** be represented, even though julian day numbers allow a much wider
20816** range of dates.
20817**
20818** The Gregorian calendar system is used for all dates and times,
20819** even those that predate the Gregorian calendar. Historians usually
20820** use the julian calendar for dates prior to 1582-10-15 and for some
20821** dates afterwards, depending on locale. Beware of this difference.
20822**
20823** The conversion algorithms are implemented based on descriptions
20824** in the following text:
20825**
20826** Jean Meeus
20827** Astronomical Algorithms, 2nd Edition, 1998
20828** ISBN 0-943396-61-1
20829** Willmann-Bell, Inc
20830** Richmond, Virginia (USA)
20831*/
20832/* #include "sqliteInt.h" */
20833/* #include <stdlib.h> */
20834/* #include <assert.h> */
20835#include <time.h>
20836
20837#ifndef SQLITE_OMIT_DATETIME_FUNCS
20838
20839/*
20840** The MSVC CRT on Windows CE may not have a localtime() function.
20841** So declare a substitute. The substitute function itself is
20842** defined in "os_win.c".
20843*/
20844#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
20845 (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
20846struct tm *__cdecl localtime(const time_t *);
20847#endif
20848
20849/*
20850** A structure for holding a single date and time.
20851*/
20852typedef struct DateTime DateTime;
20853struct DateTime {
20854 sqlite3_int64 iJD; /* The julian day number times 86400000 */
20855 int Y, M, D; /* Year, month, and day */
20856 int h, m; /* Hour and minutes */
20857 int tz; /* Timezone offset in minutes */
20858 double s; /* Seconds */
20859 char validJD; /* True (1) if iJD is valid */
20860 char rawS; /* Raw numeric value stored in s */
20861 char validYMD; /* True (1) if Y,M,D are valid */
20862 char validHMS; /* True (1) if h,m,s are valid */
20863 char validTZ; /* True (1) if tz is valid */
20864 char tzSet; /* Timezone was set explicitly */
20865 char isError; /* An overflow has occurred */
20866};
20867
20868
20869/*
20870** Convert zDate into one or more integers according to the conversion
20871** specifier zFormat.
20872**
20873** zFormat[] contains 4 characters for each integer converted, except for
20874** the last integer which is specified by three characters. The meaning
20875** of a four-character format specifiers ABCD is:
20876**
20877** A: number of digits to convert. Always "2" or "4".
20878** B: minimum value. Always "0" or "1".
20879** C: maximum value, decoded as:
20880** a: 12
20881** b: 14
20882** c: 24
20883** d: 31
20884** e: 59
20885** f: 9999
20886** D: the separator character, or \000 to indicate this is the
20887** last number to convert.
20888**
20889** Example: To translate an ISO-8601 date YYYY-MM-DD, the format would
20890** be "40f-21a-20c". The "40f-" indicates the 4-digit year followed by "-".
20891** The "21a-" indicates the 2-digit month followed by "-". The "20c" indicates
20892** the 2-digit day which is the last integer in the set.
20893**
20894** The function returns the number of successful conversions.
20895*/
20896static int getDigits(const char *zDate, const char *zFormat, ...){
20897 /* The aMx[] array translates the 3rd character of each format
20898 ** spec into a max size: a b c d e f */
20899 static const u16 aMx[] = { 12, 14, 24, 31, 59, 9999 };
20900 va_list ap;
20901 int cnt = 0;
20902 char nextC;
20903 va_start(ap, zFormat);
20904 do{
20905 char N = zFormat[0] - '0';
20906 char min = zFormat[1] - '0';
20907 int val = 0;
20908 u16 max;
20909
20910 assert( zFormat[2]>='a' && zFormat[2]<='f' );
20911 max = aMx[zFormat[2] - 'a'];
20912 nextC = zFormat[3];
20913 val = 0;
20914 while( N-- ){
20915 if( !sqlite3Isdigit(*zDate) ){
20916 goto end_getDigits;
20917 }
20918 val = val*10 + *zDate - '0';
20919 zDate++;
20920 }
20921 if( val<(int)min || val>(int)max || (nextC!=0 && nextC!=*zDate) ){
20922 goto end_getDigits;
20923 }
20924 *va_arg(ap,int*) = val;
20925 zDate++;
20926 cnt++;
20927 zFormat += 4;
20928 }while( nextC );
20929end_getDigits:
20930 va_end(ap);
20931 return cnt;
20932}
20933
20934/*
20935** Parse a timezone extension on the end of a date-time.
20936** The extension is of the form:
20937**
20938** (+/-)HH:MM
20939**
20940** Or the "zulu" notation:
20941**
20942** Z
20943**
20944** If the parse is successful, write the number of minutes
20945** of change in p->tz and return 0. If a parser error occurs,
20946** return non-zero.
20947**
20948** A missing specifier is not considered an error.
20949*/
20950static int parseTimezone(const char *zDate, DateTime *p){
20951 int sgn = 0;
20952 int nHr, nMn;
20953 int c;
20954 while( sqlite3Isspace(*zDate) ){ zDate++; }
20955 p->tz = 0;
20956 c = *zDate;
20957 if( c=='-' ){
20958 sgn = -1;
20959 }else if( c=='+' ){
20960 sgn = +1;
20961 }else if( c=='Z' || c=='z' ){
20962 zDate++;
20963 goto zulu_time;
20964 }else{
20965 return c!=0;
20966 }
20967 zDate++;
20968 if( getDigits(zDate, "20b:20e", &nHr, &nMn)!=2 ){
20969 return 1;
20970 }
20971 zDate += 5;
20972 p->tz = sgn*(nMn + nHr*60);
20973zulu_time:
20974 while( sqlite3Isspace(*zDate) ){ zDate++; }
20975 p->tzSet = 1;
20976 return *zDate!=0;
20977}
20978
20979/*
20980** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
20981** The HH, MM, and SS must each be exactly 2 digits. The
20982** fractional seconds FFFF can be one or more digits.
20983**
20984** Return 1 if there is a parsing error and 0 on success.
20985*/
20986static int parseHhMmSs(const char *zDate, DateTime *p){
20987 int h, m, s;
20988 double ms = 0.0;
20989 if( getDigits(zDate, "20c:20e", &h, &m)!=2 ){
20990 return 1;
20991 }
20992 zDate += 5;
20993 if( *zDate==':' ){
20994 zDate++;
20995 if( getDigits(zDate, "20e", &s)!=1 ){
20996 return 1;
20997 }
20998 zDate += 2;
20999 if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){
21000 double rScale = 1.0;
21001 zDate++;
21002 while( sqlite3Isdigit(*zDate) ){
21003 ms = ms*10.0 + *zDate - '0';
21004 rScale *= 10.0;
21005 zDate++;
21006 }
21007 ms /= rScale;
21008 }
21009 }else{
21010 s = 0;
21011 }
21012 p->validJD = 0;
21013 p->rawS = 0;
21014 p->validHMS = 1;
21015 p->h = h;
21016 p->m = m;
21017 p->s = s + ms;
21018 if( parseTimezone(zDate, p) ) return 1;
21019 p->validTZ = (p->tz!=0)?1:0;
21020 return 0;
21021}
21022
21023/*
21024** Put the DateTime object into its error state.
21025*/
21026static void datetimeError(DateTime *p){
21027 memset(p, 0, sizeof(*p));
21028 p->isError = 1;
21029}
21030
21031/*
21032** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
21033** that the YYYY-MM-DD is according to the Gregorian calendar.
21034**
21035** Reference: Meeus page 61
21036*/
21037static void computeJD(DateTime *p){
21038 int Y, M, D, A, B, X1, X2;
21039
21040 if( p->validJD ) return;
21041 if( p->validYMD ){
21042 Y = p->Y;
21043 M = p->M;
21044 D = p->D;
21045 }else{
21046 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */
21047 M = 1;
21048 D = 1;
21049 }
21050 if( Y<-4713 || Y>9999 || p->rawS ){
21051 datetimeError(p);
21052 return;
21053 }
21054 if( M<=2 ){
21055 Y--;
21056 M += 12;
21057 }
21058 A = Y/100;
21059 B = 2 - A + (A/4);
21060 X1 = 36525*(Y+4716)/100;
21061 X2 = 306001*(M+1)/10000;
21062 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
21063 p->validJD = 1;
21064 if( p->validHMS ){
21065 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
21066 if( p->validTZ ){
21067 p->iJD -= p->tz*60000;
21068 p->validYMD = 0;
21069 p->validHMS = 0;
21070 p->validTZ = 0;
21071 }
21072 }
21073}
21074
21075/*
21076** Parse dates of the form
21077**
21078** YYYY-MM-DD HH:MM:SS.FFF
21079** YYYY-MM-DD HH:MM:SS
21080** YYYY-MM-DD HH:MM
21081** YYYY-MM-DD
21082**
21083** Write the result into the DateTime structure and return 0
21084** on success and 1 if the input string is not a well-formed
21085** date.
21086*/
21087static int parseYyyyMmDd(const char *zDate, DateTime *p){
21088 int Y, M, D, neg;
21089
21090 if( zDate[0]=='-' ){
21091 zDate++;
21092 neg = 1;
21093 }else{
21094 neg = 0;
21095 }
21096 if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){
21097 return 1;
21098 }
21099 zDate += 10;
21100 while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
21101 if( parseHhMmSs(zDate, p)==0 ){
21102 /* We got the time */
21103 }else if( *zDate==0 ){
21104 p->validHMS = 0;
21105 }else{
21106 return 1;
21107 }
21108 p->validJD = 0;
21109 p->validYMD = 1;
21110 p->Y = neg ? -Y : Y;
21111 p->M = M;
21112 p->D = D;
21113 if( p->validTZ ){
21114 computeJD(p);
21115 }
21116 return 0;
21117}
21118
21119/*
21120** Set the time to the current time reported by the VFS.
21121**
21122** Return the number of errors.
21123*/
21124static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
21125 p->iJD = sqlite3StmtCurrentTime(context);
21126 if( p->iJD>0 ){
21127 p->validJD = 1;
21128 return 0;
21129 }else{
21130 return 1;
21131 }
21132}
21133
21134/*
21135** Input "r" is a numeric quantity which might be a julian day number,
21136** or the number of seconds since 1970. If the value if r is within
21137** range of a julian day number, install it as such and set validJD.
21138** If the value is a valid unix timestamp, put it in p->s and set p->rawS.
21139*/
21140static void setRawDateNumber(DateTime *p, double r){
21141 p->s = r;
21142 p->rawS = 1;
21143 if( r>=0.0 && r<5373484.5 ){
21144 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
21145 p->validJD = 1;
21146 }
21147}
21148
21149/*
21150** Attempt to parse the given string into a julian day number. Return
21151** the number of errors.
21152**
21153** The following are acceptable forms for the input string:
21154**
21155** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
21156** DDDD.DD
21157** now
21158**
21159** In the first form, the +/-HH:MM is always optional. The fractional
21160** seconds extension (the ".FFF") is optional. The seconds portion
21161** (":SS.FFF") is option. The year and date can be omitted as long
21162** as there is a time string. The time string can be omitted as long
21163** as there is a year and date.
21164*/
21165static int parseDateOrTime(
21166 sqlite3_context *context,
21167 const char *zDate,
21168 DateTime *p
21169){
21170 double r;
21171 if( parseYyyyMmDd(zDate,p)==0 ){
21172 return 0;
21173 }else if( parseHhMmSs(zDate, p)==0 ){
21174 return 0;
21175 }else if( sqlite3StrICmp(zDate,"now")==0 && sqlite3NotPureFunc(context) ){
21176 return setDateTimeToCurrent(context, p);
21177 }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){
21178 setRawDateNumber(p, r);
21179 return 0;
21180 }
21181 return 1;
21182}
21183
21184/* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999.
21185** Multiplying this by 86400000 gives 464269060799999 as the maximum value
21186** for DateTime.iJD.
21187**
21188** But some older compilers (ex: gcc 4.2.1 on older Macs) cannot deal with
21189** such a large integer literal, so we have to encode it.
21190*/
21191#define INT_464269060799999 ((((i64)0x1a640)<<32)|0x1072fdff)
21192
21193/*
21194** Return TRUE if the given julian day number is within range.
21195**
21196** The input is the JulianDay times 86400000.
21197*/
21198static int validJulianDay(sqlite3_int64 iJD){
21199 return iJD>=0 && iJD<=INT_464269060799999;
21200}
21201
21202/*
21203** Compute the Year, Month, and Day from the julian day number.
21204*/
21205static void computeYMD(DateTime *p){
21206 int Z, A, B, C, D, E, X1;
21207 if( p->validYMD ) return;
21208 if( !p->validJD ){
21209 p->Y = 2000;
21210 p->M = 1;
21211 p->D = 1;
21212 }else if( !validJulianDay(p->iJD) ){
21213 datetimeError(p);
21214 return;
21215 }else{
21216 Z = (int)((p->iJD + 43200000)/86400000);
21217 A = (int)((Z - 1867216.25)/36524.25);
21218 A = Z + 1 + A - (A/4);
21219 B = A + 1524;
21220 C = (int)((B - 122.1)/365.25);
21221 D = (36525*(C&32767))/100;
21222 E = (int)((B-D)/30.6001);
21223 X1 = (int)(30.6001*E);
21224 p->D = B - D - X1;
21225 p->M = E<14 ? E-1 : E-13;
21226 p->Y = p->M>2 ? C - 4716 : C - 4715;
21227 }
21228 p->validYMD = 1;
21229}
21230
21231/*
21232** Compute the Hour, Minute, and Seconds from the julian day number.
21233*/
21234static void computeHMS(DateTime *p){
21235 int s;
21236 if( p->validHMS ) return;
21237 computeJD(p);
21238 s = (int)((p->iJD + 43200000) % 86400000);
21239 p->s = s/1000.0;
21240 s = (int)p->s;
21241 p->s -= s;
21242 p->h = s/3600;
21243 s -= p->h*3600;
21244 p->m = s/60;
21245 p->s += s - p->m*60;
21246 p->rawS = 0;
21247 p->validHMS = 1;
21248}
21249
21250/*
21251** Compute both YMD and HMS
21252*/
21253static void computeYMD_HMS(DateTime *p){
21254 computeYMD(p);
21255 computeHMS(p);
21256}
21257
21258/*
21259** Clear the YMD and HMS and the TZ
21260*/
21261static void clearYMD_HMS_TZ(DateTime *p){
21262 p->validYMD = 0;
21263 p->validHMS = 0;
21264 p->validTZ = 0;
21265}
21266
21267#ifndef SQLITE_OMIT_LOCALTIME
21268/*
21269** On recent Windows platforms, the localtime_s() function is available
21270** as part of the "Secure CRT". It is essentially equivalent to
21271** localtime_r() available under most POSIX platforms, except that the
21272** order of the parameters is reversed.
21273**
21274** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
21275**
21276** If the user has not indicated to use localtime_r() or localtime_s()
21277** already, check for an MSVC build environment that provides
21278** localtime_s().
21279*/
21280#if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S \
21281 && defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
21282#undef HAVE_LOCALTIME_S
21283#define HAVE_LOCALTIME_S 1
21284#endif
21285
21286/*
21287** The following routine implements the rough equivalent of localtime_r()
21288** using whatever operating-system specific localtime facility that
21289** is available. This routine returns 0 on success and
21290** non-zero on any kind of error.
21291**
21292** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this
21293** routine will always fail.
21294**
21295** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
21296** library function localtime_r() is used to assist in the calculation of
21297** local time.
21298*/
21299static int osLocaltime(time_t *t, struct tm *pTm){
21300 int rc;
21301#if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S
21302 struct tm *pX;
21303#if SQLITE_THREADSAFE>0
21304 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
21305#endif
21306 sqlite3_mutex_enter(mutex);
21307 pX = localtime(t);
21308#ifndef SQLITE_UNTESTABLE
21309 if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0;
21310#endif
21311 if( pX ) *pTm = *pX;
21312 sqlite3_mutex_leave(mutex);
21313 rc = pX==0;
21314#else
21315#ifndef SQLITE_UNTESTABLE
21316 if( sqlite3GlobalConfig.bLocaltimeFault ) return 1;
21317#endif
21318#if HAVE_LOCALTIME_R
21319 rc = localtime_r(t, pTm)==0;
21320#else
21321 rc = localtime_s(pTm, t);
21322#endif /* HAVE_LOCALTIME_R */
21323#endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */
21324 return rc;
21325}
21326#endif /* SQLITE_OMIT_LOCALTIME */
21327
21328
21329#ifndef SQLITE_OMIT_LOCALTIME
21330/*
21331** Compute the difference (in milliseconds) between localtime and UTC
21332** (a.k.a. GMT) for the time value p where p is in UTC. If no error occurs,
21333** return this value and set *pRc to SQLITE_OK.
21334**
21335** Or, if an error does occur, set *pRc to SQLITE_ERROR. The returned value
21336** is undefined in this case.
21337*/
21338static sqlite3_int64 localtimeOffset(
21339 DateTime *p, /* Date at which to calculate offset */
21340 sqlite3_context *pCtx, /* Write error here if one occurs */
21341 int *pRc /* OUT: Error code. SQLITE_OK or ERROR */
21342){
21343 DateTime x, y;
21344 time_t t;
21345 struct tm sLocal;
21346
21347 /* Initialize the contents of sLocal to avoid a compiler warning. */
21348 memset(&sLocal, 0, sizeof(sLocal));
21349
21350 x = *p;
21351 computeYMD_HMS(&x);
21352 if( x.Y<1971 || x.Y>=2038 ){
21353 /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only
21354 ** works for years between 1970 and 2037. For dates outside this range,
21355 ** SQLite attempts to map the year into an equivalent year within this
21356 ** range, do the calculation, then map the year back.
21357 */
21358 x.Y = 2000;
21359 x.M = 1;
21360 x.D = 1;
21361 x.h = 0;
21362 x.m = 0;
21363 x.s = 0.0;
21364 } else {
21365 int s = (int)(x.s + 0.5);
21366 x.s = s;
21367 }
21368 x.tz = 0;
21369 x.validJD = 0;
21370 computeJD(&x);
21371 t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
21372 if( osLocaltime(&t, &sLocal) ){
21373 sqlite3_result_error(pCtx, "local time unavailable", -1);
21374 *pRc = SQLITE_ERROR;
21375 return 0;
21376 }
21377 y.Y = sLocal.tm_year + 1900;
21378 y.M = sLocal.tm_mon + 1;
21379 y.D = sLocal.tm_mday;
21380 y.h = sLocal.tm_hour;
21381 y.m = sLocal.tm_min;
21382 y.s = sLocal.tm_sec;
21383 y.validYMD = 1;
21384 y.validHMS = 1;
21385 y.validJD = 0;
21386 y.rawS = 0;
21387 y.validTZ = 0;
21388 y.isError = 0;
21389 computeJD(&y);
21390 *pRc = SQLITE_OK;
21391 return y.iJD - x.iJD;
21392}
21393#endif /* SQLITE_OMIT_LOCALTIME */
21394
21395/*
21396** The following table defines various date transformations of the form
21397**
21398** 'NNN days'
21399**
21400** Where NNN is an arbitrary floating-point number and "days" can be one
21401** of several units of time.
21402*/
21403static const struct {
21404 u8 eType; /* Transformation type code */
21405 u8 nName; /* Length of th name */
21406 char *zName; /* Name of the transformation */
21407 double rLimit; /* Maximum NNN value for this transform */
21408 double rXform; /* Constant used for this transform */
21409} aXformType[] = {
21410 { 0, 6, "second", 464269060800.0, 86400000.0/(24.0*60.0*60.0) },
21411 { 0, 6, "minute", 7737817680.0, 86400000.0/(24.0*60.0) },
21412 { 0, 4, "hour", 128963628.0, 86400000.0/24.0 },
21413 { 0, 3, "day", 5373485.0, 86400000.0 },
21414 { 1, 5, "month", 176546.0, 30.0*86400000.0 },
21415 { 2, 4, "year", 14713.0, 365.0*86400000.0 },
21416};
21417
21418/*
21419** Process a modifier to a date-time stamp. The modifiers are
21420** as follows:
21421**
21422** NNN days
21423** NNN hours
21424** NNN minutes
21425** NNN.NNNN seconds
21426** NNN months
21427** NNN years
21428** start of month
21429** start of year
21430** start of week
21431** start of day
21432** weekday N
21433** unixepoch
21434** localtime
21435** utc
21436**
21437** Return 0 on success and 1 if there is any kind of error. If the error
21438** is in a system call (i.e. localtime()), then an error message is written
21439** to context pCtx. If the error is an unrecognized modifier, no error is
21440** written to pCtx.
21441*/
21442static int parseModifier(
21443 sqlite3_context *pCtx, /* Function context */
21444 const char *z, /* The text of the modifier */
21445 int n, /* Length of zMod in bytes */
21446 DateTime *p /* The date/time value to be modified */
21447){
21448 int rc = 1;
21449 double r;
21450 switch(sqlite3UpperToLower[(u8)z[0]] ){
21451#ifndef SQLITE_OMIT_LOCALTIME
21452 case 'l': {
21453 /* localtime
21454 **
21455 ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
21456 ** show local time.
21457 */
21458 if( sqlite3_stricmp(z, "localtime")==0 && sqlite3NotPureFunc(pCtx) ){
21459 computeJD(p);
21460 p->iJD += localtimeOffset(p, pCtx, &rc);
21461 clearYMD_HMS_TZ(p);
21462 }
21463 break;
21464 }
21465#endif
21466 case 'u': {
21467 /*
21468 ** unixepoch
21469 **
21470 ** Treat the current value of p->s as the number of
21471 ** seconds since 1970. Convert to a real julian day number.
21472 */
21473 if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){
21474 r = p->s*1000.0 + 210866760000000.0;
21475 if( r>=0.0 && r<464269060800000.0 ){
21476 clearYMD_HMS_TZ(p);
21477 p->iJD = (sqlite3_int64)r;
21478 p->validJD = 1;
21479 p->rawS = 0;
21480 rc = 0;
21481 }
21482 }
21483#ifndef SQLITE_OMIT_LOCALTIME
21484 else if( sqlite3_stricmp(z, "utc")==0 && sqlite3NotPureFunc(pCtx) ){
21485 if( p->tzSet==0 ){
21486 sqlite3_int64 c1;
21487 computeJD(p);
21488 c1 = localtimeOffset(p, pCtx, &rc);
21489 if( rc==SQLITE_OK ){
21490 p->iJD -= c1;
21491 clearYMD_HMS_TZ(p);
21492 p->iJD += c1 - localtimeOffset(p, pCtx, &rc);
21493 }
21494 p->tzSet = 1;
21495 }else{
21496 rc = SQLITE_OK;
21497 }
21498 }
21499#endif
21500 break;
21501 }
21502 case 'w': {
21503 /*
21504 ** weekday N
21505 **
21506 ** Move the date to the same time on the next occurrence of
21507 ** weekday N where 0==Sunday, 1==Monday, and so forth. If the
21508 ** date is already on the appropriate weekday, this is a no-op.
21509 */
21510 if( sqlite3_strnicmp(z, "weekday ", 8)==0
21511 && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)
21512 && (n=(int)r)==r && n>=0 && r<7 ){
21513 sqlite3_int64 Z;
21514 computeYMD_HMS(p);
21515 p->validTZ = 0;
21516 p->validJD = 0;
21517 computeJD(p);
21518 Z = ((p->iJD + 129600000)/86400000) % 7;
21519 if( Z>n ) Z -= 7;
21520 p->iJD += (n - Z)*86400000;
21521 clearYMD_HMS_TZ(p);
21522 rc = 0;
21523 }
21524 break;
21525 }
21526 case 's': {
21527 /*
21528 ** start of TTTTT
21529 **
21530 ** Move the date backwards to the beginning of the current day,
21531 ** or month or year.
21532 */
21533 if( sqlite3_strnicmp(z, "start of ", 9)!=0 ) break;
21534 if( !p->validJD && !p->validYMD && !p->validHMS ) break;
21535 z += 9;
21536 computeYMD(p);
21537 p->validHMS = 1;
21538 p->h = p->m = 0;
21539 p->s = 0.0;
21540 p->rawS = 0;
21541 p->validTZ = 0;
21542 p->validJD = 0;
21543 if( sqlite3_stricmp(z,"month")==0 ){
21544 p->D = 1;
21545 rc = 0;
21546 }else if( sqlite3_stricmp(z,"year")==0 ){
21547 p->M = 1;
21548 p->D = 1;
21549 rc = 0;
21550 }else if( sqlite3_stricmp(z,"day")==0 ){
21551 rc = 0;
21552 }
21553 break;
21554 }
21555 case '+':
21556 case '-':
21557 case '0':
21558 case '1':
21559 case '2':
21560 case '3':
21561 case '4':
21562 case '5':
21563 case '6':
21564 case '7':
21565 case '8':
21566 case '9': {
21567 double rRounder;
21568 int i;
21569 for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){}
21570 if( !sqlite3AtoF(z, &r, n, SQLITE_UTF8) ){
21571 rc = 1;
21572 break;
21573 }
21574 if( z[n]==':' ){
21575 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
21576 ** specified number of hours, minutes, seconds, and fractional seconds
21577 ** to the time. The ".FFF" may be omitted. The ":SS.FFF" may be
21578 ** omitted.
21579 */
21580 const char *z2 = z;
21581 DateTime tx;
21582 sqlite3_int64 day;
21583 if( !sqlite3Isdigit(*z2) ) z2++;
21584 memset(&tx, 0, sizeof(tx));
21585 if( parseHhMmSs(z2, &tx) ) break;
21586 computeJD(&tx);
21587 tx.iJD -= 43200000;
21588 day = tx.iJD/86400000;
21589 tx.iJD -= day*86400000;
21590 if( z[0]=='-' ) tx.iJD = -tx.iJD;
21591 computeJD(p);
21592 clearYMD_HMS_TZ(p);
21593 p->iJD += tx.iJD;
21594 rc = 0;
21595 break;
21596 }
21597
21598 /* If control reaches this point, it means the transformation is
21599 ** one of the forms like "+NNN days". */
21600 z += n;
21601 while( sqlite3Isspace(*z) ) z++;
21602 n = sqlite3Strlen30(z);
21603 if( n>10 || n<3 ) break;
21604 if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--;
21605 computeJD(p);
21606 rc = 1;
21607 rRounder = r<0 ? -0.5 : +0.5;
21608 for(i=0; i<ArraySize(aXformType); i++){
21609 if( aXformType[i].nName==n
21610 && sqlite3_strnicmp(aXformType[i].zName, z, n)==0
21611 && r>-aXformType[i].rLimit && r<aXformType[i].rLimit
21612 ){
21613 switch( aXformType[i].eType ){
21614 case 1: { /* Special processing to add months */
21615 int x;
21616 computeYMD_HMS(p);
21617 p->M += (int)r;
21618 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
21619 p->Y += x;
21620 p->M -= x*12;
21621 p->validJD = 0;
21622 r -= (int)r;
21623 break;
21624 }
21625 case 2: { /* Special processing to add years */
21626 int y = (int)r;
21627 computeYMD_HMS(p);
21628 p->Y += y;
21629 p->validJD = 0;
21630 r -= (int)r;
21631 break;
21632 }
21633 }
21634 computeJD(p);
21635 p->iJD += (sqlite3_int64)(r*aXformType[i].rXform + rRounder);
21636 rc = 0;
21637 break;
21638 }
21639 }
21640 clearYMD_HMS_TZ(p);
21641 break;
21642 }
21643 default: {
21644 break;
21645 }
21646 }
21647 return rc;
21648}
21649
21650/*
21651** Process time function arguments. argv[0] is a date-time stamp.
21652** argv[1] and following are modifiers. Parse them all and write
21653** the resulting time into the DateTime structure p. Return 0
21654** on success and 1 if there are any errors.
21655**
21656** If there are zero parameters (if even argv[0] is undefined)
21657** then assume a default value of "now" for argv[0].
21658*/
21659static int isDate(
21660 sqlite3_context *context,
21661 int argc,
21662 sqlite3_value **argv,
21663 DateTime *p
21664){
21665 int i, n;
21666 const unsigned char *z;
21667 int eType;
21668 memset(p, 0, sizeof(*p));
21669 if( argc==0 ){
21670 return setDateTimeToCurrent(context, p);
21671 }
21672 if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
21673 || eType==SQLITE_INTEGER ){
21674 setRawDateNumber(p, sqlite3_value_double(argv[0]));
21675 }else{
21676 z = sqlite3_value_text(argv[0]);
21677 if( !z || parseDateOrTime(context, (char*)z, p) ){
21678 return 1;
21679 }
21680 }
21681 for(i=1; i<argc; i++){
21682 z = sqlite3_value_text(argv[i]);
21683 n = sqlite3_value_bytes(argv[i]);
21684 if( z==0 || parseModifier(context, (char*)z, n, p) ) return 1;
21685 }
21686 computeJD(p);
21687 if( p->isError || !validJulianDay(p->iJD) ) return 1;
21688 return 0;
21689}
21690
21691
21692/*
21693** The following routines implement the various date and time functions
21694** of SQLite.
21695*/
21696
21697/*
21698** julianday( TIMESTRING, MOD, MOD, ...)
21699**
21700** Return the julian day number of the date specified in the arguments
21701*/
21702static void juliandayFunc(
21703 sqlite3_context *context,
21704 int argc,
21705 sqlite3_value **argv
21706){
21707 DateTime x;
21708 if( isDate(context, argc, argv, &x)==0 ){
21709 computeJD(&x);
21710 sqlite3_result_double(context, x.iJD/86400000.0);
21711 }
21712}
21713
21714/*
21715** datetime( TIMESTRING, MOD, MOD, ...)
21716**
21717** Return YYYY-MM-DD HH:MM:SS
21718*/
21719static void datetimeFunc(
21720 sqlite3_context *context,
21721 int argc,
21722 sqlite3_value **argv
21723){
21724 DateTime x;
21725 if( isDate(context, argc, argv, &x)==0 ){
21726 char zBuf[100];
21727 computeYMD_HMS(&x);
21728 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
21729 x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
21730 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
21731 }
21732}
21733
21734/*
21735** time( TIMESTRING, MOD, MOD, ...)
21736**
21737** Return HH:MM:SS
21738*/
21739static void timeFunc(
21740 sqlite3_context *context,
21741 int argc,
21742 sqlite3_value **argv
21743){
21744 DateTime x;
21745 if( isDate(context, argc, argv, &x)==0 ){
21746 char zBuf[100];
21747 computeHMS(&x);
21748 sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
21749 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
21750 }
21751}
21752
21753/*
21754** date( TIMESTRING, MOD, MOD, ...)
21755**
21756** Return YYYY-MM-DD
21757*/
21758static void dateFunc(
21759 sqlite3_context *context,
21760 int argc,
21761 sqlite3_value **argv
21762){
21763 DateTime x;
21764 if( isDate(context, argc, argv, &x)==0 ){
21765 char zBuf[100];
21766 computeYMD(&x);
21767 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
21768 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
21769 }
21770}
21771
21772/*
21773** strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
21774**
21775** Return a string described by FORMAT. Conversions as follows:
21776**
21777** %d day of month
21778** %f ** fractional seconds SS.SSS
21779** %H hour 00-24
21780** %j day of year 000-366
21781** %J ** julian day number
21782** %m month 01-12
21783** %M minute 00-59
21784** %s seconds since 1970-01-01
21785** %S seconds 00-59
21786** %w day of week 0-6 sunday==0
21787** %W week of year 00-53
21788** %Y year 0000-9999
21789** %% %
21790*/
21791static void strftimeFunc(
21792 sqlite3_context *context,
21793 int argc,
21794 sqlite3_value **argv
21795){
21796 DateTime x;
21797 u64 n;
21798 size_t i,j;
21799 char *z;
21800 sqlite3 *db;
21801 const char *zFmt;
21802 char zBuf[100];
21803 if( argc==0 ) return;
21804 zFmt = (const char*)sqlite3_value_text(argv[0]);
21805 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
21806 db = sqlite3_context_db_handle(context);
21807 for(i=0, n=1; zFmt[i]; i++, n++){
21808 if( zFmt[i]=='%' ){
21809 switch( zFmt[i+1] ){
21810 case 'd':
21811 case 'H':
21812 case 'm':
21813 case 'M':
21814 case 'S':
21815 case 'W':
21816 n++;
21817 /* fall thru */
21818 case 'w':
21819 case '%':
21820 break;
21821 case 'f':
21822 n += 8;
21823 break;
21824 case 'j':
21825 n += 3;
21826 break;
21827 case 'Y':
21828 n += 8;
21829 break;
21830 case 's':
21831 case 'J':
21832 n += 50;
21833 break;
21834 default:
21835 return; /* ERROR. return a NULL */
21836 }
21837 i++;
21838 }
21839 }
21840 testcase( n==sizeof(zBuf)-1 );
21841 testcase( n==sizeof(zBuf) );
21842 testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
21843 testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] );
21844 if( n<sizeof(zBuf) ){
21845 z = zBuf;
21846 }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
21847 sqlite3_result_error_toobig(context);
21848 return;
21849 }else{
21850 z = sqlite3DbMallocRawNN(db, (int)n);
21851 if( z==0 ){
21852 sqlite3_result_error_nomem(context);
21853 return;
21854 }
21855 }
21856 computeJD(&x);
21857 computeYMD_HMS(&x);
21858 for(i=j=0; zFmt[i]; i++){
21859 if( zFmt[i]!='%' ){
21860 z[j++] = zFmt[i];
21861 }else{
21862 i++;
21863 switch( zFmt[i] ){
21864 case 'd': sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
21865 case 'f': {
21866 double s = x.s;
21867 if( s>59.999 ) s = 59.999;
21868 sqlite3_snprintf(7, &z[j],"%06.3f", s);
21869 j += sqlite3Strlen30(&z[j]);
21870 break;
21871 }
21872 case 'H': sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
21873 case 'W': /* Fall thru */
21874 case 'j': {
21875 int nDay; /* Number of days since 1st day of year */
21876 DateTime y = x;
21877 y.validJD = 0;
21878 y.M = 1;
21879 y.D = 1;
21880 computeJD(&y);
21881 nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
21882 if( zFmt[i]=='W' ){
21883 int wd; /* 0=Monday, 1=Tuesday, ... 6=Sunday */
21884 wd = (int)(((x.iJD+43200000)/86400000)%7);
21885 sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
21886 j += 2;
21887 }else{
21888 sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
21889 j += 3;
21890 }
21891 break;
21892 }
21893 case 'J': {
21894 sqlite3_snprintf(20, &z[j],"%.16g",x.iJD/86400000.0);
21895 j+=sqlite3Strlen30(&z[j]);
21896 break;
21897 }
21898 case 'm': sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
21899 case 'M': sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
21900 case 's': {
21901 sqlite3_snprintf(30,&z[j],"%lld",
21902 (i64)(x.iJD/1000 - 21086676*(i64)10000));
21903 j += sqlite3Strlen30(&z[j]);
21904 break;
21905 }
21906 case 'S': sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
21907 case 'w': {
21908 z[j++] = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
21909 break;
21910 }
21911 case 'Y': {
21912 sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=sqlite3Strlen30(&z[j]);
21913 break;
21914 }
21915 default: z[j++] = '%'; break;
21916 }
21917 }
21918 }
21919 z[j] = 0;
21920 sqlite3_result_text(context, z, -1,
21921 z==zBuf ? SQLITE_TRANSIENT : SQLITE_DYNAMIC);
21922}
21923
21924/*
21925** current_time()
21926**
21927** This function returns the same value as time('now').
21928*/
21929static void ctimeFunc(
21930 sqlite3_context *context,
21931 int NotUsed,
21932 sqlite3_value **NotUsed2
21933){
21934 UNUSED_PARAMETER2(NotUsed, NotUsed2);
21935 timeFunc(context, 0, 0);
21936}
21937
21938/*
21939** current_date()
21940**
21941** This function returns the same value as date('now').
21942*/
21943static void cdateFunc(
21944 sqlite3_context *context,
21945 int NotUsed,
21946 sqlite3_value **NotUsed2
21947){
21948 UNUSED_PARAMETER2(NotUsed, NotUsed2);
21949 dateFunc(context, 0, 0);
21950}
21951
21952/*
21953** current_timestamp()
21954**
21955** This function returns the same value as datetime('now').
21956*/
21957static void ctimestampFunc(
21958 sqlite3_context *context,
21959 int NotUsed,
21960 sqlite3_value **NotUsed2
21961){
21962 UNUSED_PARAMETER2(NotUsed, NotUsed2);
21963 datetimeFunc(context, 0, 0);
21964}
21965#endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
21966
21967#ifdef SQLITE_OMIT_DATETIME_FUNCS
21968/*
21969** If the library is compiled to omit the full-scale date and time
21970** handling (to get a smaller binary), the following minimal version
21971** of the functions current_time(), current_date() and current_timestamp()
21972** are included instead. This is to support column declarations that
21973** include "DEFAULT CURRENT_TIME" etc.
21974**
21975** This function uses the C-library functions time(), gmtime()
21976** and strftime(). The format string to pass to strftime() is supplied
21977** as the user-data for the function.
21978*/
21979static void currentTimeFunc(
21980 sqlite3_context *context,
21981 int argc,
21982 sqlite3_value **argv
21983){
21984 time_t t;
21985 char *zFormat = (char *)sqlite3_user_data(context);
21986 sqlite3_int64 iT;
21987 struct tm *pTm;
21988 struct tm sNow;
21989 char zBuf[20];
21990
21991 UNUSED_PARAMETER(argc);
21992 UNUSED_PARAMETER(argv);
21993
21994 iT = sqlite3StmtCurrentTime(context);
21995 if( iT<=0 ) return;
21996 t = iT/1000 - 10000*(sqlite3_int64)21086676;
21997#if HAVE_GMTIME_R
21998 pTm = gmtime_r(&t, &sNow);
21999#else
22000 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
22001 pTm = gmtime(&t);
22002 if( pTm ) memcpy(&sNow, pTm, sizeof(sNow));
22003 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
22004#endif
22005 if( pTm ){
22006 strftime(zBuf, 20, zFormat, &sNow);
22007 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
22008 }
22009}
22010#endif
22011
22012/*
22013** This function registered all of the above C functions as SQL
22014** functions. This should be the only routine in this file with
22015** external linkage.
22016*/
22017SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
22018 static FuncDef aDateTimeFuncs[] = {
22019#ifndef SQLITE_OMIT_DATETIME_FUNCS
22020 PURE_DATE(julianday, -1, 0, 0, juliandayFunc ),
22021 PURE_DATE(date, -1, 0, 0, dateFunc ),
22022 PURE_DATE(time, -1, 0, 0, timeFunc ),
22023 PURE_DATE(datetime, -1, 0, 0, datetimeFunc ),
22024 PURE_DATE(strftime, -1, 0, 0, strftimeFunc ),
22025 DFUNCTION(current_time, 0, 0, 0, ctimeFunc ),
22026 DFUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
22027 DFUNCTION(current_date, 0, 0, 0, cdateFunc ),
22028#else
22029 STR_FUNCTION(current_time, 0, "%H:%M:%S", 0, currentTimeFunc),
22030 STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc),
22031 STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
22032#endif
22033 };
22034 sqlite3InsertBuiltinFuncs(aDateTimeFuncs, ArraySize(aDateTimeFuncs));
22035}
22036
22037/************** End of date.c ************************************************/
22038/************** Begin file os.c **********************************************/
22039/*
22040** 2005 November 29
22041**
22042** The author disclaims copyright to this source code. In place of
22043** a legal notice, here is a blessing:
22044**
22045** May you do good and not evil.
22046** May you find forgiveness for yourself and forgive others.
22047** May you share freely, never taking more than you give.
22048**
22049******************************************************************************
22050**
22051** This file contains OS interface code that is common to all
22052** architectures.
22053*/
22054/* #include "sqliteInt.h" */
22055
22056/*
22057** If we compile with the SQLITE_TEST macro set, then the following block
22058** of code will give us the ability to simulate a disk I/O error. This
22059** is used for testing the I/O recovery logic.
22060*/
22061#if defined(SQLITE_TEST)
22062SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */
22063SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
22064SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */
22065SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */
22066SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */
22067SQLITE_API int sqlite3_diskfull_pending = 0;
22068SQLITE_API int sqlite3_diskfull = 0;
22069#endif /* defined(SQLITE_TEST) */
22070
22071/*
22072** When testing, also keep a count of the number of open files.
22073*/
22074#if defined(SQLITE_TEST)
22075SQLITE_API int sqlite3_open_file_count = 0;
22076#endif /* defined(SQLITE_TEST) */
22077
22078/*
22079** The default SQLite sqlite3_vfs implementations do not allocate
22080** memory (actually, os_unix.c allocates a small amount of memory
22081** from within OsOpen()), but some third-party implementations may.
22082** So we test the effects of a malloc() failing and the sqlite3OsXXX()
22083** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
22084**
22085** The following functions are instrumented for malloc() failure
22086** testing:
22087**
22088** sqlite3OsRead()
22089** sqlite3OsWrite()
22090** sqlite3OsSync()
22091** sqlite3OsFileSize()
22092** sqlite3OsLock()
22093** sqlite3OsCheckReservedLock()
22094** sqlite3OsFileControl()
22095** sqlite3OsShmMap()
22096** sqlite3OsOpen()
22097** sqlite3OsDelete()
22098** sqlite3OsAccess()
22099** sqlite3OsFullPathname()
22100**
22101*/
22102#if defined(SQLITE_TEST)
22103SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
22104 #define DO_OS_MALLOC_TEST(x) \
22105 if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3JournalIsInMemory(x))) { \
22106 void *pTstAlloc = sqlite3Malloc(10); \
22107 if (!pTstAlloc) return SQLITE_IOERR_NOMEM_BKPT; \
22108 sqlite3_free(pTstAlloc); \
22109 }
22110#else
22111 #define DO_OS_MALLOC_TEST(x)
22112#endif
22113
22114/*
22115** The following routines are convenience wrappers around methods
22116** of the sqlite3_file object. This is mostly just syntactic sugar. All
22117** of this would be completely automatic if SQLite were coded using
22118** C++ instead of plain old C.
22119*/
22120SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file *pId){
22121 if( pId->pMethods ){
22122 pId->pMethods->xClose(pId);
22123 pId->pMethods = 0;
22124 }
22125}
22126SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
22127 DO_OS_MALLOC_TEST(id);
22128 return id->pMethods->xRead(id, pBuf, amt, offset);
22129}
22130SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
22131 DO_OS_MALLOC_TEST(id);
22132 return id->pMethods->xWrite(id, pBuf, amt, offset);
22133}
22134SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file *id, i64 size){
22135 return id->pMethods->xTruncate(id, size);
22136}
22137SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file *id, int flags){
22138 DO_OS_MALLOC_TEST(id);
22139 return flags ? id->pMethods->xSync(id, flags) : SQLITE_OK;
22140}
22141SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
22142 DO_OS_MALLOC_TEST(id);
22143 return id->pMethods->xFileSize(id, pSize);
22144}
22145SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
22146 DO_OS_MALLOC_TEST(id);
22147 return id->pMethods->xLock(id, lockType);
22148}
22149SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){
22150 return id->pMethods->xUnlock(id, lockType);
22151}
22152SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
22153 DO_OS_MALLOC_TEST(id);
22154 return id->pMethods->xCheckReservedLock(id, pResOut);
22155}
22156
22157/*
22158** Use sqlite3OsFileControl() when we are doing something that might fail
22159** and we need to know about the failures. Use sqlite3OsFileControlHint()
22160** when simply tossing information over the wall to the VFS and we do not
22161** really care if the VFS receives and understands the information since it
22162** is only a hint and can be safely ignored. The sqlite3OsFileControlHint()
22163** routine has no return value since the return value would be meaningless.
22164*/
22165SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
22166 if( id->pMethods==0 ) return SQLITE_NOTFOUND;
22167#ifdef SQLITE_TEST
22168 if( op!=SQLITE_FCNTL_COMMIT_PHASETWO
22169 && op!=SQLITE_FCNTL_LOCK_TIMEOUT
22170 ){
22171 /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
22172 ** is using a regular VFS, it is called after the corresponding
22173 ** transaction has been committed. Injecting a fault at this point
22174 ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
22175 ** but the transaction is committed anyway.
22176 **
22177 ** The core must call OsFileControl() though, not OsFileControlHint(),
22178 ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably
22179 ** means the commit really has failed and an error should be returned
22180 ** to the user. */
22181 DO_OS_MALLOC_TEST(id);
22182 }
22183#endif
22184 return id->pMethods->xFileControl(id, op, pArg);
22185}
22186SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
22187 if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg);
22188}
22189
22190SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
22191 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
22192 return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
22193}
22194SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
22195 return id->pMethods->xDeviceCharacteristics(id);
22196}
22197#ifndef SQLITE_OMIT_WAL
22198SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
22199 return id->pMethods->xShmLock(id, offset, n, flags);
22200}
22201SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id){
22202 id->pMethods->xShmBarrier(id);
22203}
22204SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
22205 return id->pMethods->xShmUnmap(id, deleteFlag);
22206}
22207SQLITE_PRIVATE int sqlite3OsShmMap(
22208 sqlite3_file *id, /* Database file handle */
22209 int iPage,
22210 int pgsz,
22211 int bExtend, /* True to extend file if necessary */
22212 void volatile **pp /* OUT: Pointer to mapping */
22213){
22214 DO_OS_MALLOC_TEST(id);
22215 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
22216}
22217#endif /* SQLITE_OMIT_WAL */
22218
22219#if SQLITE_MAX_MMAP_SIZE>0
22220/* The real implementation of xFetch and xUnfetch */
22221SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
22222 DO_OS_MALLOC_TEST(id);
22223 return id->pMethods->xFetch(id, iOff, iAmt, pp);
22224}
22225SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
22226 return id->pMethods->xUnfetch(id, iOff, p);
22227}
22228#else
22229/* No-op stubs to use when memory-mapped I/O is disabled */
22230SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
22231 *pp = 0;
22232 return SQLITE_OK;
22233}
22234SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
22235 return SQLITE_OK;
22236}
22237#endif
22238
22239/*
22240** The next group of routines are convenience wrappers around the
22241** VFS methods.
22242*/
22243SQLITE_PRIVATE int sqlite3OsOpen(
22244 sqlite3_vfs *pVfs,
22245 const char *zPath,
22246 sqlite3_file *pFile,
22247 int flags,
22248 int *pFlagsOut
22249){
22250 int rc;
22251 DO_OS_MALLOC_TEST(0);
22252 /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
22253 ** down into the VFS layer. Some SQLITE_OPEN_ flags (for example,
22254 ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
22255 ** reaching the VFS. */
22256 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f7f, pFlagsOut);
22257 assert( rc==SQLITE_OK || pFile->pMethods==0 );
22258 return rc;
22259}
22260SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
22261 DO_OS_MALLOC_TEST(0);
22262 assert( dirSync==0 || dirSync==1 );
22263 return pVfs->xDelete(pVfs, zPath, dirSync);
22264}
22265SQLITE_PRIVATE int sqlite3OsAccess(
22266 sqlite3_vfs *pVfs,
22267 const char *zPath,
22268 int flags,
22269 int *pResOut
22270){
22271 DO_OS_MALLOC_TEST(0);
22272 return pVfs->xAccess(pVfs, zPath, flags, pResOut);
22273}
22274SQLITE_PRIVATE int sqlite3OsFullPathname(
22275 sqlite3_vfs *pVfs,
22276 const char *zPath,
22277 int nPathOut,
22278 char *zPathOut
22279){
22280 DO_OS_MALLOC_TEST(0);
22281 zPathOut[0] = 0;
22282 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
22283}
22284#ifndef SQLITE_OMIT_LOAD_EXTENSION
22285SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
22286 return pVfs->xDlOpen(pVfs, zPath);
22287}
22288SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
22289 pVfs->xDlError(pVfs, nByte, zBufOut);
22290}
22291SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
22292 return pVfs->xDlSym(pVfs, pHdle, zSym);
22293}
22294SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
22295 pVfs->xDlClose(pVfs, pHandle);
22296}
22297#endif /* SQLITE_OMIT_LOAD_EXTENSION */
22298SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
22299 return pVfs->xRandomness(pVfs, nByte, zBufOut);
22300}
22301SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
22302 return pVfs->xSleep(pVfs, nMicro);
22303}
22304SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs *pVfs){
22305 return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0;
22306}
22307SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
22308 int rc;
22309 /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
22310 ** method to get the current date and time if that method is available
22311 ** (if iVersion is 2 or greater and the function pointer is not NULL) and
22312 ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
22313 ** unavailable.
22314 */
22315 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
22316 rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
22317 }else{
22318 double r;
22319 rc = pVfs->xCurrentTime(pVfs, &r);
22320 *pTimeOut = (sqlite3_int64)(r*86400000.0);
22321 }
22322 return rc;
22323}
22324
22325SQLITE_PRIVATE int sqlite3OsOpenMalloc(
22326 sqlite3_vfs *pVfs,
22327 const char *zFile,
22328 sqlite3_file **ppFile,
22329 int flags,
22330 int *pOutFlags
22331){
22332 int rc;
22333 sqlite3_file *pFile;
22334 pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
22335 if( pFile ){
22336 rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
22337 if( rc!=SQLITE_OK ){
22338 sqlite3_free(pFile);
22339 }else{
22340 *ppFile = pFile;
22341 }
22342 }else{
22343 rc = SQLITE_NOMEM_BKPT;
22344 }
22345 return rc;
22346}
22347SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *pFile){
22348 assert( pFile );
22349 sqlite3OsClose(pFile);
22350 sqlite3_free(pFile);
22351}
22352
22353/*
22354** This function is a wrapper around the OS specific implementation of
22355** sqlite3_os_init(). The purpose of the wrapper is to provide the
22356** ability to simulate a malloc failure, so that the handling of an
22357** error in sqlite3_os_init() by the upper layers can be tested.
22358*/
22359SQLITE_PRIVATE int sqlite3OsInit(void){
22360 void *p = sqlite3_malloc(10);
22361 if( p==0 ) return SQLITE_NOMEM_BKPT;
22362 sqlite3_free(p);
22363 return sqlite3_os_init();
22364}
22365
22366/*
22367** The list of all registered VFS implementations.
22368*/
22369static sqlite3_vfs * SQLITE_WSD vfsList = 0;
22370#define vfsList GLOBAL(sqlite3_vfs *, vfsList)
22371
22372/*
22373** Locate a VFS by name. If no name is given, simply return the
22374** first VFS on the list.
22375*/
22376SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
22377 sqlite3_vfs *pVfs = 0;
22378#if SQLITE_THREADSAFE
22379 sqlite3_mutex *mutex;
22380#endif
22381#ifndef SQLITE_OMIT_AUTOINIT
22382 int rc = sqlite3_initialize();
22383 if( rc ) return 0;
22384#endif
22385#if SQLITE_THREADSAFE
22386 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
22387#endif
22388 sqlite3_mutex_enter(mutex);
22389 for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
22390 if( zVfs==0 ) break;
22391 if( strcmp(zVfs, pVfs->zName)==0 ) break;
22392 }
22393 sqlite3_mutex_leave(mutex);
22394 return pVfs;
22395}
22396
22397/*
22398** Unlink a VFS from the linked list
22399*/
22400static void vfsUnlink(sqlite3_vfs *pVfs){
22401 assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) );
22402 if( pVfs==0 ){
22403 /* No-op */
22404 }else if( vfsList==pVfs ){
22405 vfsList = pVfs->pNext;
22406 }else if( vfsList ){
22407 sqlite3_vfs *p = vfsList;
22408 while( p->pNext && p->pNext!=pVfs ){
22409 p = p->pNext;
22410 }
22411 if( p->pNext==pVfs ){
22412 p->pNext = pVfs->pNext;
22413 }
22414 }
22415}
22416
22417/*
22418** Register a VFS with the system. It is harmless to register the same
22419** VFS multiple times. The new VFS becomes the default if makeDflt is
22420** true.
22421*/
22422SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
22423 MUTEX_LOGIC(sqlite3_mutex *mutex;)
22424#ifndef SQLITE_OMIT_AUTOINIT
22425 int rc = sqlite3_initialize();
22426 if( rc ) return rc;
22427#endif
22428#ifdef SQLITE_ENABLE_API_ARMOR
22429 if( pVfs==0 ) return SQLITE_MISUSE_BKPT;
22430#endif
22431
22432 MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
22433 sqlite3_mutex_enter(mutex);
22434 vfsUnlink(pVfs);
22435 if( makeDflt || vfsList==0 ){
22436 pVfs->pNext = vfsList;
22437 vfsList = pVfs;
22438 }else{
22439 pVfs->pNext = vfsList->pNext;
22440 vfsList->pNext = pVfs;
22441 }
22442 assert(vfsList);
22443 sqlite3_mutex_leave(mutex);
22444 return SQLITE_OK;
22445}
22446
22447/*
22448** Unregister a VFS so that it is no longer accessible.
22449*/
22450SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
22451 MUTEX_LOGIC(sqlite3_mutex *mutex;)
22452#ifndef SQLITE_OMIT_AUTOINIT
22453 int rc = sqlite3_initialize();
22454 if( rc ) return rc;
22455#endif
22456 MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
22457 sqlite3_mutex_enter(mutex);
22458 vfsUnlink(pVfs);
22459 sqlite3_mutex_leave(mutex);
22460 return SQLITE_OK;
22461}
22462
22463/************** End of os.c **************************************************/
22464/************** Begin file fault.c *******************************************/
22465/*
22466** 2008 Jan 22
22467**
22468** The author disclaims copyright to this source code. In place of
22469** a legal notice, here is a blessing:
22470**
22471** May you do good and not evil.
22472** May you find forgiveness for yourself and forgive others.
22473** May you share freely, never taking more than you give.
22474**
22475*************************************************************************
22476**
22477** This file contains code to support the concept of "benign"
22478** malloc failures (when the xMalloc() or xRealloc() method of the
22479** sqlite3_mem_methods structure fails to allocate a block of memory
22480** and returns 0).
22481**
22482** Most malloc failures are non-benign. After they occur, SQLite
22483** abandons the current operation and returns an error code (usually
22484** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
22485** fatal. For example, if a malloc fails while resizing a hash table, this
22486** is completely recoverable simply by not carrying out the resize. The
22487** hash table will continue to function normally. So a malloc failure
22488** during a hash table resize is a benign fault.
22489*/
22490
22491/* #include "sqliteInt.h" */
22492
22493#ifndef SQLITE_UNTESTABLE
22494
22495/*
22496** Global variables.
22497*/
22498typedef struct BenignMallocHooks BenignMallocHooks;
22499static SQLITE_WSD struct BenignMallocHooks {
22500 void (*xBenignBegin)(void);
22501 void (*xBenignEnd)(void);
22502} sqlite3Hooks = { 0, 0 };
22503
22504/* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks
22505** structure. If writable static data is unsupported on the target,
22506** we have to locate the state vector at run-time. In the more common
22507** case where writable static data is supported, wsdHooks can refer directly
22508** to the "sqlite3Hooks" state vector declared above.
22509*/
22510#ifdef SQLITE_OMIT_WSD
22511# define wsdHooksInit \
22512 BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks)
22513# define wsdHooks x[0]
22514#else
22515# define wsdHooksInit
22516# define wsdHooks sqlite3Hooks
22517#endif
22518
22519
22520/*
22521** Register hooks to call when sqlite3BeginBenignMalloc() and
22522** sqlite3EndBenignMalloc() are called, respectively.
22523*/
22524SQLITE_PRIVATE void sqlite3BenignMallocHooks(
22525 void (*xBenignBegin)(void),
22526 void (*xBenignEnd)(void)
22527){
22528 wsdHooksInit;
22529 wsdHooks.xBenignBegin = xBenignBegin;
22530 wsdHooks.xBenignEnd = xBenignEnd;
22531}
22532
22533/*
22534** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
22535** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
22536** indicates that subsequent malloc failures are non-benign.
22537*/
22538SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void){
22539 wsdHooksInit;
22540 if( wsdHooks.xBenignBegin ){
22541 wsdHooks.xBenignBegin();
22542 }
22543}
22544SQLITE_PRIVATE void sqlite3EndBenignMalloc(void){
22545 wsdHooksInit;
22546 if( wsdHooks.xBenignEnd ){
22547 wsdHooks.xBenignEnd();
22548 }
22549}
22550
22551#endif /* #ifndef SQLITE_UNTESTABLE */
22552
22553/************** End of fault.c ***********************************************/
22554/************** Begin file mem0.c ********************************************/
22555/*
22556** 2008 October 28
22557**
22558** The author disclaims copyright to this source code. In place of
22559** a legal notice, here is a blessing:
22560**
22561** May you do good and not evil.
22562** May you find forgiveness for yourself and forgive others.
22563** May you share freely, never taking more than you give.
22564**
22565*************************************************************************
22566**
22567** This file contains a no-op memory allocation drivers for use when
22568** SQLITE_ZERO_MALLOC is defined. The allocation drivers implemented
22569** here always fail. SQLite will not operate with these drivers. These
22570** are merely placeholders. Real drivers must be substituted using
22571** sqlite3_config() before SQLite will operate.
22572*/
22573/* #include "sqliteInt.h" */
22574
22575/*
22576** This version of the memory allocator is the default. It is
22577** used when no other memory allocator is specified using compile-time
22578** macros.
22579*/
22580#ifdef SQLITE_ZERO_MALLOC
22581
22582/*
22583** No-op versions of all memory allocation routines
22584*/
22585static void *sqlite3MemMalloc(int nByte){ return 0; }
22586static void sqlite3MemFree(void *pPrior){ return; }
22587static void *sqlite3MemRealloc(void *pPrior, int nByte){ return 0; }
22588static int sqlite3MemSize(void *pPrior){ return 0; }
22589static int sqlite3MemRoundup(int n){ return n; }
22590static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK; }
22591static void sqlite3MemShutdown(void *NotUsed){ return; }
22592
22593/*
22594** This routine is the only routine in this file with external linkage.
22595**
22596** Populate the low-level memory allocation function pointers in
22597** sqlite3GlobalConfig.m with pointers to the routines in this file.
22598*/
22599SQLITE_PRIVATE void sqlite3MemSetDefault(void){
22600 static const sqlite3_mem_methods defaultMethods = {
22601 sqlite3MemMalloc,
22602 sqlite3MemFree,
22603 sqlite3MemRealloc,
22604 sqlite3MemSize,
22605 sqlite3MemRoundup,
22606 sqlite3MemInit,
22607 sqlite3MemShutdown,
22608 0
22609 };
22610 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
22611}
22612
22613#endif /* SQLITE_ZERO_MALLOC */
22614
22615/************** End of mem0.c ************************************************/
22616/************** Begin file mem1.c ********************************************/
22617/*
22618** 2007 August 14
22619**
22620** The author disclaims copyright to this source code. In place of
22621** a legal notice, here is a blessing:
22622**
22623** May you do good and not evil.
22624** May you find forgiveness for yourself and forgive others.
22625** May you share freely, never taking more than you give.
22626**
22627*************************************************************************
22628**
22629** This file contains low-level memory allocation drivers for when
22630** SQLite will use the standard C-library malloc/realloc/free interface
22631** to obtain the memory it needs.
22632**
22633** This file contains implementations of the low-level memory allocation
22634** routines specified in the sqlite3_mem_methods object. The content of
22635** this file is only used if SQLITE_SYSTEM_MALLOC is defined. The
22636** SQLITE_SYSTEM_MALLOC macro is defined automatically if neither the
22637** SQLITE_MEMDEBUG nor the SQLITE_WIN32_MALLOC macros are defined. The
22638** default configuration is to use memory allocation routines in this
22639** file.
22640**
22641** C-preprocessor macro summary:
22642**
22643** HAVE_MALLOC_USABLE_SIZE The configure script sets this symbol if
22644** the malloc_usable_size() interface exists
22645** on the target platform. Or, this symbol
22646** can be set manually, if desired.
22647** If an equivalent interface exists by
22648** a different name, using a separate -D
22649** option to rename it.
22650**
22651** SQLITE_WITHOUT_ZONEMALLOC Some older macs lack support for the zone
22652** memory allocator. Set this symbol to enable
22653** building on older macs.
22654**
22655** SQLITE_WITHOUT_MSIZE Set this symbol to disable the use of
22656** _msize() on windows systems. This might
22657** be necessary when compiling for Delphi,
22658** for example.
22659*/
22660/* #include "sqliteInt.h" */
22661
22662/*
22663** This version of the memory allocator is the default. It is
22664** used when no other memory allocator is specified using compile-time
22665** macros.
22666*/
22667#ifdef SQLITE_SYSTEM_MALLOC
22668#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
22669
22670/*
22671** Use the zone allocator available on apple products unless the
22672** SQLITE_WITHOUT_ZONEMALLOC symbol is defined.
22673*/
22674#include <sys/sysctl.h>
22675#include <malloc/malloc.h>
22676#ifdef SQLITE_MIGHT_BE_SINGLE_CORE
22677#include <libkern/OSAtomic.h>
22678#endif /* SQLITE_MIGHT_BE_SINGLE_CORE */
22679static malloc_zone_t* _sqliteZone_;
22680#define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
22681#define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
22682#define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
22683#define SQLITE_MALLOCSIZE(x) \
22684 (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
22685
22686#else /* if not __APPLE__ */
22687
22688/*
22689** Use standard C library malloc and free on non-Apple systems.
22690** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined.
22691*/
22692#define SQLITE_MALLOC(x) malloc(x)
22693#define SQLITE_FREE(x) free(x)
22694#define SQLITE_REALLOC(x,y) realloc((x),(y))
22695
22696/*
22697** The malloc.h header file is needed for malloc_usable_size() function
22698** on some systems (e.g. Linux).
22699*/
22700#if HAVE_MALLOC_H && HAVE_MALLOC_USABLE_SIZE
22701# define SQLITE_USE_MALLOC_H 1
22702# define SQLITE_USE_MALLOC_USABLE_SIZE 1
22703/*
22704** The MSVCRT has malloc_usable_size(), but it is called _msize(). The
22705** use of _msize() is automatic, but can be disabled by compiling with
22706** -DSQLITE_WITHOUT_MSIZE. Using the _msize() function also requires
22707** the malloc.h header file.
22708*/
22709#elif defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)
22710# define SQLITE_USE_MALLOC_H
22711# define SQLITE_USE_MSIZE
22712#endif
22713
22714/*
22715** Include the malloc.h header file, if necessary. Also set define macro
22716** SQLITE_MALLOCSIZE to the appropriate function name, which is _msize()
22717** for MSVC and malloc_usable_size() for most other systems (e.g. Linux).
22718** The memory size function can always be overridden manually by defining
22719** the macro SQLITE_MALLOCSIZE to the desired function name.
22720*/
22721#if defined(SQLITE_USE_MALLOC_H)
22722# include <malloc.h>
22723# if defined(SQLITE_USE_MALLOC_USABLE_SIZE)
22724# if !defined(SQLITE_MALLOCSIZE)
22725# define SQLITE_MALLOCSIZE(x) malloc_usable_size(x)
22726# endif
22727# elif defined(SQLITE_USE_MSIZE)
22728# if !defined(SQLITE_MALLOCSIZE)
22729# define SQLITE_MALLOCSIZE _msize
22730# endif
22731# endif
22732#endif /* defined(SQLITE_USE_MALLOC_H) */
22733
22734#endif /* __APPLE__ or not __APPLE__ */
22735
22736/*
22737** Like malloc(), but remember the size of the allocation
22738** so that we can find it later using sqlite3MemSize().
22739**
22740** For this low-level routine, we are guaranteed that nByte>0 because
22741** cases of nByte<=0 will be intercepted and dealt with by higher level
22742** routines.
22743*/
22744static void *sqlite3MemMalloc(int nByte){
22745#ifdef SQLITE_MALLOCSIZE
22746 void *p;
22747 testcase( ROUND8(nByte)==nByte );
22748 p = SQLITE_MALLOC( nByte );
22749 if( p==0 ){
22750 testcase( sqlite3GlobalConfig.xLog!=0 );
22751 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
22752 }
22753 return p;
22754#else
22755 sqlite3_int64 *p;
22756 assert( nByte>0 );
22757 testcase( ROUND8(nByte)!=nByte );
22758 p = SQLITE_MALLOC( nByte+8 );
22759 if( p ){
22760 p[0] = nByte;
22761 p++;
22762 }else{
22763 testcase( sqlite3GlobalConfig.xLog!=0 );
22764 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
22765 }
22766 return (void *)p;
22767#endif
22768}
22769
22770/*
22771** Like free() but works for allocations obtained from sqlite3MemMalloc()
22772** or sqlite3MemRealloc().
22773**
22774** For this low-level routine, we already know that pPrior!=0 since
22775** cases where pPrior==0 will have been intecepted and dealt with
22776** by higher-level routines.
22777*/
22778static void sqlite3MemFree(void *pPrior){
22779#ifdef SQLITE_MALLOCSIZE
22780 SQLITE_FREE(pPrior);
22781#else
22782 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
22783 assert( pPrior!=0 );
22784 p--;
22785 SQLITE_FREE(p);
22786#endif
22787}
22788
22789/*
22790** Report the allocated size of a prior return from xMalloc()
22791** or xRealloc().
22792*/
22793static int sqlite3MemSize(void *pPrior){
22794#ifdef SQLITE_MALLOCSIZE
22795 assert( pPrior!=0 );
22796 return (int)SQLITE_MALLOCSIZE(pPrior);
22797#else
22798 sqlite3_int64 *p;
22799 assert( pPrior!=0 );
22800 p = (sqlite3_int64*)pPrior;
22801 p--;
22802 return (int)p[0];
22803#endif
22804}
22805
22806/*
22807** Like realloc(). Resize an allocation previously obtained from
22808** sqlite3MemMalloc().
22809**
22810** For this low-level interface, we know that pPrior!=0. Cases where
22811** pPrior==0 while have been intercepted by higher-level routine and
22812** redirected to xMalloc. Similarly, we know that nByte>0 because
22813** cases where nByte<=0 will have been intercepted by higher-level
22814** routines and redirected to xFree.
22815*/
22816static void *sqlite3MemRealloc(void *pPrior, int nByte){
22817#ifdef SQLITE_MALLOCSIZE
22818 void *p = SQLITE_REALLOC(pPrior, nByte);
22819 if( p==0 ){
22820 testcase( sqlite3GlobalConfig.xLog!=0 );
22821 sqlite3_log(SQLITE_NOMEM,
22822 "failed memory resize %u to %u bytes",
22823 SQLITE_MALLOCSIZE(pPrior), nByte);
22824 }
22825 return p;
22826#else
22827 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
22828 assert( pPrior!=0 && nByte>0 );
22829 assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
22830 p--;
22831 p = SQLITE_REALLOC(p, nByte+8 );
22832 if( p ){
22833 p[0] = nByte;
22834 p++;
22835 }else{
22836 testcase( sqlite3GlobalConfig.xLog!=0 );
22837 sqlite3_log(SQLITE_NOMEM,
22838 "failed memory resize %u to %u bytes",
22839 sqlite3MemSize(pPrior), nByte);
22840 }
22841 return (void*)p;
22842#endif
22843}
22844
22845/*
22846** Round up a request size to the next valid allocation size.
22847*/
22848static int sqlite3MemRoundup(int n){
22849 return ROUND8(n);
22850}
22851
22852/*
22853** Initialize this module.
22854*/
22855static int sqlite3MemInit(void *NotUsed){
22856#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
22857 int cpuCount;
22858 size_t len;
22859 if( _sqliteZone_ ){
22860 return SQLITE_OK;
22861 }
22862 len = sizeof(cpuCount);
22863 /* One usually wants to use hw.acctivecpu for MT decisions, but not here */
22864 sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
22865 if( cpuCount>1 ){
22866 /* defer MT decisions to system malloc */
22867 _sqliteZone_ = malloc_default_zone();
22868 }else{
22869 /* only 1 core, use our own zone to contention over global locks,
22870 ** e.g. we have our own dedicated locks */
22871 _sqliteZone_ = malloc_create_zone(4096, 0);
22872 malloc_set_zone_name(_sqliteZone_, "Sqlite_Heap");
22873 }
22874#endif /* defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC) */
22875 UNUSED_PARAMETER(NotUsed);
22876 return SQLITE_OK;
22877}
22878
22879/*
22880** Deinitialize this module.
22881*/
22882static void sqlite3MemShutdown(void *NotUsed){
22883 UNUSED_PARAMETER(NotUsed);
22884 return;
22885}
22886
22887/*
22888** This routine is the only routine in this file with external linkage.
22889**
22890** Populate the low-level memory allocation function pointers in
22891** sqlite3GlobalConfig.m with pointers to the routines in this file.
22892*/
22893SQLITE_PRIVATE void sqlite3MemSetDefault(void){
22894 static const sqlite3_mem_methods defaultMethods = {
22895 sqlite3MemMalloc,
22896 sqlite3MemFree,
22897 sqlite3MemRealloc,
22898 sqlite3MemSize,
22899 sqlite3MemRoundup,
22900 sqlite3MemInit,
22901 sqlite3MemShutdown,
22902 0
22903 };
22904 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
22905}
22906
22907#endif /* SQLITE_SYSTEM_MALLOC */
22908
22909/************** End of mem1.c ************************************************/
22910/************** Begin file mem2.c ********************************************/
22911/*
22912** 2007 August 15
22913**
22914** The author disclaims copyright to this source code. In place of
22915** a legal notice, here is a blessing:
22916**
22917** May you do good and not evil.
22918** May you find forgiveness for yourself and forgive others.
22919** May you share freely, never taking more than you give.
22920**
22921*************************************************************************
22922**
22923** This file contains low-level memory allocation drivers for when
22924** SQLite will use the standard C-library malloc/realloc/free interface
22925** to obtain the memory it needs while adding lots of additional debugging
22926** information to each allocation in order to help detect and fix memory
22927** leaks and memory usage errors.
22928**
22929** This file contains implementations of the low-level memory allocation
22930** routines specified in the sqlite3_mem_methods object.
22931*/
22932/* #include "sqliteInt.h" */
22933
22934/*
22935** This version of the memory allocator is used only if the
22936** SQLITE_MEMDEBUG macro is defined
22937*/
22938#ifdef SQLITE_MEMDEBUG
22939
22940/*
22941** The backtrace functionality is only available with GLIBC
22942*/
22943#ifdef __GLIBC__
22944 extern int backtrace(void**,int);
22945 extern void backtrace_symbols_fd(void*const*,int,int);
22946#else
22947# define backtrace(A,B) 1
22948# define backtrace_symbols_fd(A,B,C)
22949#endif
22950/* #include <stdio.h> */
22951
22952/*
22953** Each memory allocation looks like this:
22954**
22955** ------------------------------------------------------------------------
22956** | Title | backtrace pointers | MemBlockHdr | allocation | EndGuard |
22957** ------------------------------------------------------------------------
22958**
22959** The application code sees only a pointer to the allocation. We have
22960** to back up from the allocation pointer to find the MemBlockHdr. The
22961** MemBlockHdr tells us the size of the allocation and the number of
22962** backtrace pointers. There is also a guard word at the end of the
22963** MemBlockHdr.
22964*/
22965struct MemBlockHdr {
22966 i64 iSize; /* Size of this allocation */
22967 struct MemBlockHdr *pNext, *pPrev; /* Linked list of all unfreed memory */
22968 char nBacktrace; /* Number of backtraces on this alloc */
22969 char nBacktraceSlots; /* Available backtrace slots */
22970 u8 nTitle; /* Bytes of title; includes '\0' */
22971 u8 eType; /* Allocation type code */
22972 int iForeGuard; /* Guard word for sanity */
22973};
22974
22975/*
22976** Guard words
22977*/
22978#define FOREGUARD 0x80F5E153
22979#define REARGUARD 0xE4676B53
22980
22981/*
22982** Number of malloc size increments to track.
22983*/
22984#define NCSIZE 1000
22985
22986/*
22987** All of the static variables used by this module are collected
22988** into a single structure named "mem". This is to keep the
22989** static variables organized and to reduce namespace pollution
22990** when this module is combined with other in the amalgamation.
22991*/
22992static struct {
22993
22994 /*
22995 ** Mutex to control access to the memory allocation subsystem.
22996 */
22997 sqlite3_mutex *mutex;
22998
22999 /*
23000 ** Head and tail of a linked list of all outstanding allocations
23001 */
23002 struct MemBlockHdr *pFirst;
23003 struct MemBlockHdr *pLast;
23004
23005 /*
23006 ** The number of levels of backtrace to save in new allocations.
23007 */
23008 int nBacktrace;
23009 void (*xBacktrace)(int, int, void **);
23010
23011 /*
23012 ** Title text to insert in front of each block
23013 */
23014 int nTitle; /* Bytes of zTitle to save. Includes '\0' and padding */
23015 char zTitle[100]; /* The title text */
23016
23017 /*
23018 ** sqlite3MallocDisallow() increments the following counter.
23019 ** sqlite3MallocAllow() decrements it.
23020 */
23021 int disallow; /* Do not allow memory allocation */
23022
23023 /*
23024 ** Gather statistics on the sizes of memory allocations.
23025 ** nAlloc[i] is the number of allocation attempts of i*8
23026 ** bytes. i==NCSIZE is the number of allocation attempts for
23027 ** sizes more than NCSIZE*8 bytes.
23028 */
23029 int nAlloc[NCSIZE]; /* Total number of allocations */
23030 int nCurrent[NCSIZE]; /* Current number of allocations */
23031 int mxCurrent[NCSIZE]; /* Highwater mark for nCurrent */
23032
23033} mem;
23034
23035
23036/*
23037** Adjust memory usage statistics
23038*/
23039static void adjustStats(int iSize, int increment){
23040 int i = ROUND8(iSize)/8;
23041 if( i>NCSIZE-1 ){
23042 i = NCSIZE - 1;
23043 }
23044 if( increment>0 ){
23045 mem.nAlloc[i]++;
23046 mem.nCurrent[i]++;
23047 if( mem.nCurrent[i]>mem.mxCurrent[i] ){
23048 mem.mxCurrent[i] = mem.nCurrent[i];
23049 }
23050 }else{
23051 mem.nCurrent[i]--;
23052 assert( mem.nCurrent[i]>=0 );
23053 }
23054}
23055
23056/*
23057** Given an allocation, find the MemBlockHdr for that allocation.
23058**
23059** This routine checks the guards at either end of the allocation and
23060** if they are incorrect it asserts.
23061*/
23062static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
23063 struct MemBlockHdr *p;
23064 int *pInt;
23065 u8 *pU8;
23066 int nReserve;
23067
23068 p = (struct MemBlockHdr*)pAllocation;
23069 p--;
23070 assert( p->iForeGuard==(int)FOREGUARD );
23071 nReserve = ROUND8(p->iSize);
23072 pInt = (int*)pAllocation;
23073 pU8 = (u8*)pAllocation;
23074 assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD );
23075 /* This checks any of the "extra" bytes allocated due
23076 ** to rounding up to an 8 byte boundary to ensure
23077 ** they haven't been overwritten.
23078 */
23079 while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 );
23080 return p;
23081}
23082
23083/*
23084** Return the number of bytes currently allocated at address p.
23085*/
23086static int sqlite3MemSize(void *p){
23087 struct MemBlockHdr *pHdr;
23088 if( !p ){
23089 return 0;
23090 }
23091 pHdr = sqlite3MemsysGetHeader(p);
23092 return (int)pHdr->iSize;
23093}
23094
23095/*
23096** Initialize the memory allocation subsystem.
23097*/
23098static int sqlite3MemInit(void *NotUsed){
23099 UNUSED_PARAMETER(NotUsed);
23100 assert( (sizeof(struct MemBlockHdr)&7) == 0 );
23101 if( !sqlite3GlobalConfig.bMemstat ){
23102 /* If memory status is enabled, then the malloc.c wrapper will already
23103 ** hold the STATIC_MEM mutex when the routines here are invoked. */
23104 mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
23105 }
23106 return SQLITE_OK;
23107}
23108
23109/*
23110** Deinitialize the memory allocation subsystem.
23111*/
23112static void sqlite3MemShutdown(void *NotUsed){
23113 UNUSED_PARAMETER(NotUsed);
23114 mem.mutex = 0;
23115}
23116
23117/*
23118** Round up a request size to the next valid allocation size.
23119*/
23120static int sqlite3MemRoundup(int n){
23121 return ROUND8(n);
23122}
23123
23124/*
23125** Fill a buffer with pseudo-random bytes. This is used to preset
23126** the content of a new memory allocation to unpredictable values and
23127** to clear the content of a freed allocation to unpredictable values.
23128*/
23129static void randomFill(char *pBuf, int nByte){
23130 unsigned int x, y, r;
23131 x = SQLITE_PTR_TO_INT(pBuf);
23132 y = nByte | 1;
23133 while( nByte >= 4 ){
23134 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
23135 y = y*1103515245 + 12345;
23136 r = x ^ y;
23137 *(int*)pBuf = r;
23138 pBuf += 4;
23139 nByte -= 4;
23140 }
23141 while( nByte-- > 0 ){
23142 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
23143 y = y*1103515245 + 12345;
23144 r = x ^ y;
23145 *(pBuf++) = r & 0xff;
23146 }
23147}
23148
23149/*
23150** Allocate nByte bytes of memory.
23151*/
23152static void *sqlite3MemMalloc(int nByte){
23153 struct MemBlockHdr *pHdr;
23154 void **pBt;
23155 char *z;
23156 int *pInt;
23157 void *p = 0;
23158 int totalSize;
23159 int nReserve;
23160 sqlite3_mutex_enter(mem.mutex);
23161 assert( mem.disallow==0 );
23162 nReserve = ROUND8(nByte);
23163 totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
23164 mem.nBacktrace*sizeof(void*) + mem.nTitle;
23165 p = malloc(totalSize);
23166 if( p ){
23167 z = p;
23168 pBt = (void**)&z[mem.nTitle];
23169 pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
23170 pHdr->pNext = 0;
23171 pHdr->pPrev = mem.pLast;
23172 if( mem.pLast ){
23173 mem.pLast->pNext = pHdr;
23174 }else{
23175 mem.pFirst = pHdr;
23176 }
23177 mem.pLast = pHdr;
23178 pHdr->iForeGuard = FOREGUARD;
23179 pHdr->eType = MEMTYPE_HEAP;
23180 pHdr->nBacktraceSlots = mem.nBacktrace;
23181 pHdr->nTitle = mem.nTitle;
23182 if( mem.nBacktrace ){
23183 void *aAddr[40];
23184 pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
23185 memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
23186 assert(pBt[0]);
23187 if( mem.xBacktrace ){
23188 mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
23189 }
23190 }else{
23191 pHdr->nBacktrace = 0;
23192 }
23193 if( mem.nTitle ){
23194 memcpy(z, mem.zTitle, mem.nTitle);
23195 }
23196 pHdr->iSize = nByte;
23197 adjustStats(nByte, +1);
23198 pInt = (int*)&pHdr[1];
23199 pInt[nReserve/sizeof(int)] = REARGUARD;
23200 randomFill((char*)pInt, nByte);
23201 memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
23202 p = (void*)pInt;
23203 }
23204 sqlite3_mutex_leave(mem.mutex);
23205 return p;
23206}
23207
23208/*
23209** Free memory.
23210*/
23211static void sqlite3MemFree(void *pPrior){
23212 struct MemBlockHdr *pHdr;
23213 void **pBt;
23214 char *z;
23215 assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0
23216 || mem.mutex!=0 );
23217 pHdr = sqlite3MemsysGetHeader(pPrior);
23218 pBt = (void**)pHdr;
23219 pBt -= pHdr->nBacktraceSlots;
23220 sqlite3_mutex_enter(mem.mutex);
23221 if( pHdr->pPrev ){
23222 assert( pHdr->pPrev->pNext==pHdr );
23223 pHdr->pPrev->pNext = pHdr->pNext;
23224 }else{
23225 assert( mem.pFirst==pHdr );
23226 mem.pFirst = pHdr->pNext;
23227 }
23228 if( pHdr->pNext ){
23229 assert( pHdr->pNext->pPrev==pHdr );
23230 pHdr->pNext->pPrev = pHdr->pPrev;
23231 }else{
23232 assert( mem.pLast==pHdr );
23233 mem.pLast = pHdr->pPrev;
23234 }
23235 z = (char*)pBt;
23236 z -= pHdr->nTitle;
23237 adjustStats((int)pHdr->iSize, -1);
23238 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
23239 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle);
23240 free(z);
23241 sqlite3_mutex_leave(mem.mutex);
23242}
23243
23244/*
23245** Change the size of an existing memory allocation.
23246**
23247** For this debugging implementation, we *always* make a copy of the
23248** allocation into a new place in memory. In this way, if the
23249** higher level code is using pointer to the old allocation, it is
23250** much more likely to break and we are much more liking to find
23251** the error.
23252*/
23253static void *sqlite3MemRealloc(void *pPrior, int nByte){
23254 struct MemBlockHdr *pOldHdr;
23255 void *pNew;
23256 assert( mem.disallow==0 );
23257 assert( (nByte & 7)==0 ); /* EV: R-46199-30249 */
23258 pOldHdr = sqlite3MemsysGetHeader(pPrior);
23259 pNew = sqlite3MemMalloc(nByte);
23260 if( pNew ){
23261 memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize));
23262 if( nByte>pOldHdr->iSize ){
23263 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize);
23264 }
23265 sqlite3MemFree(pPrior);
23266 }
23267 return pNew;
23268}
23269
23270/*
23271** Populate the low-level memory allocation function pointers in
23272** sqlite3GlobalConfig.m with pointers to the routines in this file.
23273*/
23274SQLITE_PRIVATE void sqlite3MemSetDefault(void){
23275 static const sqlite3_mem_methods defaultMethods = {
23276 sqlite3MemMalloc,
23277 sqlite3MemFree,
23278 sqlite3MemRealloc,
23279 sqlite3MemSize,
23280 sqlite3MemRoundup,
23281 sqlite3MemInit,
23282 sqlite3MemShutdown,
23283 0
23284 };
23285 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
23286}
23287
23288/*
23289** Set the "type" of an allocation.
23290*/
23291SQLITE_PRIVATE void sqlite3MemdebugSetType(void *p, u8 eType){
23292 if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
23293 struct MemBlockHdr *pHdr;
23294 pHdr = sqlite3MemsysGetHeader(p);
23295 assert( pHdr->iForeGuard==FOREGUARD );
23296 pHdr->eType = eType;
23297 }
23298}
23299
23300/*
23301** Return TRUE if the mask of type in eType matches the type of the
23302** allocation p. Also return true if p==NULL.
23303**
23304** This routine is designed for use within an assert() statement, to
23305** verify the type of an allocation. For example:
23306**
23307** assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
23308*/
23309SQLITE_PRIVATE int sqlite3MemdebugHasType(void *p, u8 eType){
23310 int rc = 1;
23311 if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
23312 struct MemBlockHdr *pHdr;
23313 pHdr = sqlite3MemsysGetHeader(p);
23314 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */
23315 if( (pHdr->eType&eType)==0 ){
23316 rc = 0;
23317 }
23318 }
23319 return rc;
23320}
23321
23322/*
23323** Return TRUE if the mask of type in eType matches no bits of the type of the
23324** allocation p. Also return true if p==NULL.
23325**
23326** This routine is designed for use within an assert() statement, to
23327** verify the type of an allocation. For example:
23328**
23329** assert( sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
23330*/
23331SQLITE_PRIVATE int sqlite3MemdebugNoType(void *p, u8 eType){
23332 int rc = 1;
23333 if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
23334 struct MemBlockHdr *pHdr;
23335 pHdr = sqlite3MemsysGetHeader(p);
23336 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */
23337 if( (pHdr->eType&eType)!=0 ){
23338 rc = 0;
23339 }
23340 }
23341 return rc;
23342}
23343
23344/*
23345** Set the number of backtrace levels kept for each allocation.
23346** A value of zero turns off backtracing. The number is always rounded
23347** up to a multiple of 2.
23348*/
23349SQLITE_PRIVATE void sqlite3MemdebugBacktrace(int depth){
23350 if( depth<0 ){ depth = 0; }
23351 if( depth>20 ){ depth = 20; }
23352 depth = (depth+1)&0xfe;
23353 mem.nBacktrace = depth;
23354}
23355
23356SQLITE_PRIVATE void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
23357 mem.xBacktrace = xBacktrace;
23358}
23359
23360/*
23361** Set the title string for subsequent allocations.
23362*/
23363SQLITE_PRIVATE void sqlite3MemdebugSettitle(const char *zTitle){
23364 unsigned int n = sqlite3Strlen30(zTitle) + 1;
23365 sqlite3_mutex_enter(mem.mutex);
23366 if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
23367 memcpy(mem.zTitle, zTitle, n);
23368 mem.zTitle[n] = 0;
23369 mem.nTitle = ROUND8(n);
23370 sqlite3_mutex_leave(mem.mutex);
23371}
23372
23373SQLITE_PRIVATE void sqlite3MemdebugSync(){
23374 struct MemBlockHdr *pHdr;
23375 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
23376 void **pBt = (void**)pHdr;
23377 pBt -= pHdr->nBacktraceSlots;
23378 mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
23379 }
23380}
23381
23382/*
23383** Open the file indicated and write a log of all unfreed memory
23384** allocations into that log.
23385*/
23386SQLITE_PRIVATE void sqlite3MemdebugDump(const char *zFilename){
23387 FILE *out;
23388 struct MemBlockHdr *pHdr;
23389 void **pBt;
23390 int i;
23391 out = fopen(zFilename, "w");
23392 if( out==0 ){
23393 fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
23394 zFilename);
23395 return;
23396 }
23397 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
23398 char *z = (char*)pHdr;
23399 z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
23400 fprintf(out, "**** %lld bytes at %p from %s ****\n",
23401 pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
23402 if( pHdr->nBacktrace ){
23403 fflush(out);
23404 pBt = (void**)pHdr;
23405 pBt -= pHdr->nBacktraceSlots;
23406 backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
23407 fprintf(out, "\n");
23408 }
23409 }
23410 fprintf(out, "COUNTS:\n");
23411 for(i=0; i<NCSIZE-1; i++){
23412 if( mem.nAlloc[i] ){
23413 fprintf(out, " %5d: %10d %10d %10d\n",
23414 i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
23415 }
23416 }
23417 if( mem.nAlloc[NCSIZE-1] ){
23418 fprintf(out, " %5d: %10d %10d %10d\n",
23419 NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
23420 mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
23421 }
23422 fclose(out);
23423}
23424
23425/*
23426** Return the number of times sqlite3MemMalloc() has been called.
23427*/
23428SQLITE_PRIVATE int sqlite3MemdebugMallocCount(){
23429 int i;
23430 int nTotal = 0;
23431 for(i=0; i<NCSIZE; i++){
23432 nTotal += mem.nAlloc[i];
23433 }
23434 return nTotal;
23435}
23436
23437
23438#endif /* SQLITE_MEMDEBUG */
23439
23440/************** End of mem2.c ************************************************/
23441/************** Begin file mem3.c ********************************************/
23442/*
23443** 2007 October 14
23444**
23445** The author disclaims copyright to this source code. In place of
23446** a legal notice, here is a blessing:
23447**
23448** May you do good and not evil.
23449** May you find forgiveness for yourself and forgive others.
23450** May you share freely, never taking more than you give.
23451**
23452*************************************************************************
23453** This file contains the C functions that implement a memory
23454** allocation subsystem for use by SQLite.
23455**
23456** This version of the memory allocation subsystem omits all
23457** use of malloc(). The SQLite user supplies a block of memory
23458** before calling sqlite3_initialize() from which allocations
23459** are made and returned by the xMalloc() and xRealloc()
23460** implementations. Once sqlite3_initialize() has been called,
23461** the amount of memory available to SQLite is fixed and cannot
23462** be changed.
23463**
23464** This version of the memory allocation subsystem is included
23465** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
23466*/
23467/* #include "sqliteInt.h" */
23468
23469/*
23470** This version of the memory allocator is only built into the library
23471** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
23472** mean that the library will use a memory-pool by default, just that
23473** it is available. The mempool allocator is activated by calling
23474** sqlite3_config().
23475*/
23476#ifdef SQLITE_ENABLE_MEMSYS3
23477
23478/*
23479** Maximum size (in Mem3Blocks) of a "small" chunk.
23480*/
23481#define MX_SMALL 10
23482
23483
23484/*
23485** Number of freelist hash slots
23486*/
23487#define N_HASH 61
23488
23489/*
23490** A memory allocation (also called a "chunk") consists of two or
23491** more blocks where each block is 8 bytes. The first 8 bytes are
23492** a header that is not returned to the user.
23493**
23494** A chunk is two or more blocks that is either checked out or
23495** free. The first block has format u.hdr. u.hdr.size4x is 4 times the
23496** size of the allocation in blocks if the allocation is free.
23497** The u.hdr.size4x&1 bit is true if the chunk is checked out and
23498** false if the chunk is on the freelist. The u.hdr.size4x&2 bit
23499** is true if the previous chunk is checked out and false if the
23500** previous chunk is free. The u.hdr.prevSize field is the size of
23501** the previous chunk in blocks if the previous chunk is on the
23502** freelist. If the previous chunk is checked out, then
23503** u.hdr.prevSize can be part of the data for that chunk and should
23504** not be read or written.
23505**
23506** We often identify a chunk by its index in mem3.aPool[]. When
23507** this is done, the chunk index refers to the second block of
23508** the chunk. In this way, the first chunk has an index of 1.
23509** A chunk index of 0 means "no such chunk" and is the equivalent
23510** of a NULL pointer.
23511**
23512** The second block of free chunks is of the form u.list. The
23513** two fields form a double-linked list of chunks of related sizes.
23514** Pointers to the head of the list are stored in mem3.aiSmall[]
23515** for smaller chunks and mem3.aiHash[] for larger chunks.
23516**
23517** The second block of a chunk is user data if the chunk is checked
23518** out. If a chunk is checked out, the user data may extend into
23519** the u.hdr.prevSize value of the following chunk.
23520*/
23521typedef struct Mem3Block Mem3Block;
23522struct Mem3Block {
23523 union {
23524 struct {
23525 u32 prevSize; /* Size of previous chunk in Mem3Block elements */
23526 u32 size4x; /* 4x the size of current chunk in Mem3Block elements */
23527 } hdr;
23528 struct {
23529 u32 next; /* Index in mem3.aPool[] of next free chunk */
23530 u32 prev; /* Index in mem3.aPool[] of previous free chunk */
23531 } list;
23532 } u;
23533};
23534
23535/*
23536** All of the static variables used by this module are collected
23537** into a single structure named "mem3". This is to keep the
23538** static variables organized and to reduce namespace pollution
23539** when this module is combined with other in the amalgamation.
23540*/
23541static SQLITE_WSD struct Mem3Global {
23542 /*
23543 ** Memory available for allocation. nPool is the size of the array
23544 ** (in Mem3Blocks) pointed to by aPool less 2.
23545 */
23546 u32 nPool;
23547 Mem3Block *aPool;
23548
23549 /*
23550 ** True if we are evaluating an out-of-memory callback.
23551 */
23552 int alarmBusy;
23553
23554 /*
23555 ** Mutex to control access to the memory allocation subsystem.
23556 */
23557 sqlite3_mutex *mutex;
23558
23559 /*
23560 ** The minimum amount of free space that we have seen.
23561 */
23562 u32 mnMaster;
23563
23564 /*
23565 ** iMaster is the index of the master chunk. Most new allocations
23566 ** occur off of this chunk. szMaster is the size (in Mem3Blocks)
23567 ** of the current master. iMaster is 0 if there is not master chunk.
23568 ** The master chunk is not in either the aiHash[] or aiSmall[].
23569 */
23570 u32 iMaster;
23571 u32 szMaster;
23572
23573 /*
23574 ** Array of lists of free blocks according to the block size
23575 ** for smaller chunks, or a hash on the block size for larger
23576 ** chunks.
23577 */
23578 u32 aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */
23579 u32 aiHash[N_HASH]; /* For sizes MX_SMALL+1 and larger */
23580} mem3 = { 97535575 };
23581
23582#define mem3 GLOBAL(struct Mem3Global, mem3)
23583
23584/*
23585** Unlink the chunk at mem3.aPool[i] from list it is currently
23586** on. *pRoot is the list that i is a member of.
23587*/
23588static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
23589 u32 next = mem3.aPool[i].u.list.next;
23590 u32 prev = mem3.aPool[i].u.list.prev;
23591 assert( sqlite3_mutex_held(mem3.mutex) );
23592 if( prev==0 ){
23593 *pRoot = next;
23594 }else{
23595 mem3.aPool[prev].u.list.next = next;
23596 }
23597 if( next ){
23598 mem3.aPool[next].u.list.prev = prev;
23599 }
23600 mem3.aPool[i].u.list.next = 0;
23601 mem3.aPool[i].u.list.prev = 0;
23602}
23603
23604/*
23605** Unlink the chunk at index i from
23606** whatever list is currently a member of.
23607*/
23608static void memsys3Unlink(u32 i){
23609 u32 size, hash;
23610 assert( sqlite3_mutex_held(mem3.mutex) );
23611 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
23612 assert( i>=1 );
23613 size = mem3.aPool[i-1].u.hdr.size4x/4;
23614 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
23615 assert( size>=2 );
23616 if( size <= MX_SMALL ){
23617 memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
23618 }else{
23619 hash = size % N_HASH;
23620 memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
23621 }
23622}
23623
23624/*
23625** Link the chunk at mem3.aPool[i] so that is on the list rooted
23626** at *pRoot.
23627*/
23628static void memsys3LinkIntoList(u32 i, u32 *pRoot){
23629 assert( sqlite3_mutex_held(mem3.mutex) );
23630 mem3.aPool[i].u.list.next = *pRoot;
23631 mem3.aPool[i].u.list.prev = 0;
23632 if( *pRoot ){
23633 mem3.aPool[*pRoot].u.list.prev = i;
23634 }
23635 *pRoot = i;
23636}
23637
23638/*
23639** Link the chunk at index i into either the appropriate
23640** small chunk list, or into the large chunk hash table.
23641*/
23642static void memsys3Link(u32 i){
23643 u32 size, hash;
23644 assert( sqlite3_mutex_held(mem3.mutex) );
23645 assert( i>=1 );
23646 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
23647 size = mem3.aPool[i-1].u.hdr.size4x/4;
23648 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
23649 assert( size>=2 );
23650 if( size <= MX_SMALL ){
23651 memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
23652 }else{
23653 hash = size % N_HASH;
23654 memsys3LinkIntoList(i, &mem3.aiHash[hash]);
23655 }
23656}
23657
23658/*
23659** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
23660** will already be held (obtained by code in malloc.c) if
23661** sqlite3GlobalConfig.bMemStat is true.
23662*/
23663static void memsys3Enter(void){
23664 if( sqlite3GlobalConfig.bMemstat==0 && mem3.mutex==0 ){
23665 mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
23666 }
23667 sqlite3_mutex_enter(mem3.mutex);
23668}
23669static void memsys3Leave(void){
23670 sqlite3_mutex_leave(mem3.mutex);
23671}
23672
23673/*
23674** Called when we are unable to satisfy an allocation of nBytes.
23675*/
23676static void memsys3OutOfMemory(int nByte){
23677 if( !mem3.alarmBusy ){
23678 mem3.alarmBusy = 1;
23679 assert( sqlite3_mutex_held(mem3.mutex) );
23680 sqlite3_mutex_leave(mem3.mutex);
23681 sqlite3_release_memory(nByte);
23682 sqlite3_mutex_enter(mem3.mutex);
23683 mem3.alarmBusy = 0;
23684 }
23685}
23686
23687
23688/*
23689** Chunk i is a free chunk that has been unlinked. Adjust its
23690** size parameters for check-out and return a pointer to the
23691** user portion of the chunk.
23692*/
23693static void *memsys3Checkout(u32 i, u32 nBlock){
23694 u32 x;
23695 assert( sqlite3_mutex_held(mem3.mutex) );
23696 assert( i>=1 );
23697 assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
23698 assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
23699 x = mem3.aPool[i-1].u.hdr.size4x;
23700 mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
23701 mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
23702 mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
23703 return &mem3.aPool[i];
23704}
23705
23706/*
23707** Carve a piece off of the end of the mem3.iMaster free chunk.
23708** Return a pointer to the new allocation. Or, if the master chunk
23709** is not large enough, return 0.
23710*/
23711static void *memsys3FromMaster(u32 nBlock){
23712 assert( sqlite3_mutex_held(mem3.mutex) );
23713 assert( mem3.szMaster>=nBlock );
23714 if( nBlock>=mem3.szMaster-1 ){
23715 /* Use the entire master */
23716 void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster);
23717 mem3.iMaster = 0;
23718 mem3.szMaster = 0;
23719 mem3.mnMaster = 0;
23720 return p;
23721 }else{
23722 /* Split the master block. Return the tail. */
23723 u32 newi, x;
23724 newi = mem3.iMaster + mem3.szMaster - nBlock;
23725 assert( newi > mem3.iMaster+1 );
23726 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock;
23727 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2;
23728 mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
23729 mem3.szMaster -= nBlock;
23730 mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster;
23731 x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
23732 mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
23733 if( mem3.szMaster < mem3.mnMaster ){
23734 mem3.mnMaster = mem3.szMaster;
23735 }
23736 return (void*)&mem3.aPool[newi];
23737 }
23738}
23739
23740/*
23741** *pRoot is the head of a list of free chunks of the same size
23742** or same size hash. In other words, *pRoot is an entry in either
23743** mem3.aiSmall[] or mem3.aiHash[].
23744**
23745** This routine examines all entries on the given list and tries
23746** to coalesce each entries with adjacent free chunks.
23747**
23748** If it sees a chunk that is larger than mem3.iMaster, it replaces
23749** the current mem3.iMaster with the new larger chunk. In order for
23750** this mem3.iMaster replacement to work, the master chunk must be
23751** linked into the hash tables. That is not the normal state of
23752** affairs, of course. The calling routine must link the master
23753** chunk before invoking this routine, then must unlink the (possibly
23754** changed) master chunk once this routine has finished.
23755*/
23756static void memsys3Merge(u32 *pRoot){
23757 u32 iNext, prev, size, i, x;
23758
23759 assert( sqlite3_mutex_held(mem3.mutex) );
23760 for(i=*pRoot; i>0; i=iNext){
23761 iNext = mem3.aPool[i].u.list.next;
23762 size = mem3.aPool[i-1].u.hdr.size4x;
23763 assert( (size&1)==0 );
23764 if( (size&2)==0 ){
23765 memsys3UnlinkFromList(i, pRoot);
23766 assert( i > mem3.aPool[i-1].u.hdr.prevSize );
23767 prev = i - mem3.aPool[i-1].u.hdr.prevSize;
23768 if( prev==iNext ){
23769 iNext = mem3.aPool[prev].u.list.next;
23770 }
23771 memsys3Unlink(prev);
23772 size = i + size/4 - prev;
23773 x = mem3.aPool[prev-1].u.hdr.size4x & 2;
23774 mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
23775 mem3.aPool[prev+size-1].u.hdr.prevSize = size;
23776 memsys3Link(prev);
23777 i = prev;
23778 }else{
23779 size /= 4;
23780 }
23781 if( size>mem3.szMaster ){
23782 mem3.iMaster = i;
23783 mem3.szMaster = size;
23784 }
23785 }
23786}
23787
23788/*
23789** Return a block of memory of at least nBytes in size.
23790** Return NULL if unable.
23791**
23792** This function assumes that the necessary mutexes, if any, are
23793** already held by the caller. Hence "Unsafe".
23794*/
23795static void *memsys3MallocUnsafe(int nByte){
23796 u32 i;
23797 u32 nBlock;
23798 u32 toFree;
23799
23800 assert( sqlite3_mutex_held(mem3.mutex) );
23801 assert( sizeof(Mem3Block)==8 );
23802 if( nByte<=12 ){
23803 nBlock = 2;
23804 }else{
23805 nBlock = (nByte + 11)/8;
23806 }
23807 assert( nBlock>=2 );
23808
23809 /* STEP 1:
23810 ** Look for an entry of the correct size in either the small
23811 ** chunk table or in the large chunk hash table. This is
23812 ** successful most of the time (about 9 times out of 10).
23813 */
23814 if( nBlock <= MX_SMALL ){
23815 i = mem3.aiSmall[nBlock-2];
23816 if( i>0 ){
23817 memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
23818 return memsys3Checkout(i, nBlock);
23819 }
23820 }else{
23821 int hash = nBlock % N_HASH;
23822 for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
23823 if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
23824 memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
23825 return memsys3Checkout(i, nBlock);
23826 }
23827 }
23828 }
23829
23830 /* STEP 2:
23831 ** Try to satisfy the allocation by carving a piece off of the end
23832 ** of the master chunk. This step usually works if step 1 fails.
23833 */
23834 if( mem3.szMaster>=nBlock ){
23835 return memsys3FromMaster(nBlock);
23836 }
23837
23838
23839 /* STEP 3:
23840 ** Loop through the entire memory pool. Coalesce adjacent free
23841 ** chunks. Recompute the master chunk as the largest free chunk.
23842 ** Then try again to satisfy the allocation by carving a piece off
23843 ** of the end of the master chunk. This step happens very
23844 ** rarely (we hope!)
23845 */
23846 for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
23847 memsys3OutOfMemory(toFree);
23848 if( mem3.iMaster ){
23849 memsys3Link(mem3.iMaster);
23850 mem3.iMaster = 0;
23851 mem3.szMaster = 0;
23852 }
23853 for(i=0; i<N_HASH; i++){
23854 memsys3Merge(&mem3.aiHash[i]);
23855 }
23856 for(i=0; i<MX_SMALL-1; i++){
23857 memsys3Merge(&mem3.aiSmall[i]);
23858 }
23859 if( mem3.szMaster ){
23860 memsys3Unlink(mem3.iMaster);
23861 if( mem3.szMaster>=nBlock ){
23862 return memsys3FromMaster(nBlock);
23863 }
23864 }
23865 }
23866
23867 /* If none of the above worked, then we fail. */
23868 return 0;
23869}
23870
23871/*
23872** Free an outstanding memory allocation.
23873**
23874** This function assumes that the necessary mutexes, if any, are
23875** already held by the caller. Hence "Unsafe".
23876*/
23877static void memsys3FreeUnsafe(void *pOld){
23878 Mem3Block *p = (Mem3Block*)pOld;
23879 int i;
23880 u32 size, x;
23881 assert( sqlite3_mutex_held(mem3.mutex) );
23882 assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] );
23883 i = p - mem3.aPool;
23884 assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
23885 size = mem3.aPool[i-1].u.hdr.size4x/4;
23886 assert( i+size<=mem3.nPool+1 );
23887 mem3.aPool[i-1].u.hdr.size4x &= ~1;
23888 mem3.aPool[i+size-1].u.hdr.prevSize = size;
23889 mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
23890 memsys3Link(i);
23891
23892 /* Try to expand the master using the newly freed chunk */
23893 if( mem3.iMaster ){
23894 while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){
23895 size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize;
23896 mem3.iMaster -= size;
23897 mem3.szMaster += size;
23898 memsys3Unlink(mem3.iMaster);
23899 x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
23900 mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
23901 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
23902 }
23903 x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
23904 while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){
23905 memsys3Unlink(mem3.iMaster+mem3.szMaster);
23906 mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4;
23907 mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
23908 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
23909 }
23910 }
23911}
23912
23913/*
23914** Return the size of an outstanding allocation, in bytes. The
23915** size returned omits the 8-byte header overhead. This only
23916** works for chunks that are currently checked out.
23917*/
23918static int memsys3Size(void *p){
23919 Mem3Block *pBlock;
23920 assert( p!=0 );
23921 pBlock = (Mem3Block*)p;
23922 assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
23923 return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
23924}
23925
23926/*
23927** Round up a request size to the next valid allocation size.
23928*/
23929static int memsys3Roundup(int n){
23930 if( n<=12 ){
23931 return 12;
23932 }else{
23933 return ((n+11)&~7) - 4;
23934 }
23935}
23936
23937/*
23938** Allocate nBytes of memory.
23939*/
23940static void *memsys3Malloc(int nBytes){
23941 sqlite3_int64 *p;
23942 assert( nBytes>0 ); /* malloc.c filters out 0 byte requests */
23943 memsys3Enter();
23944 p = memsys3MallocUnsafe(nBytes);
23945 memsys3Leave();
23946 return (void*)p;
23947}
23948
23949/*
23950** Free memory.
23951*/
23952static void memsys3Free(void *pPrior){
23953 assert( pPrior );
23954 memsys3Enter();
23955 memsys3FreeUnsafe(pPrior);
23956 memsys3Leave();
23957}
23958
23959/*
23960** Change the size of an existing memory allocation
23961*/
23962static void *memsys3Realloc(void *pPrior, int nBytes){
23963 int nOld;
23964 void *p;
23965 if( pPrior==0 ){
23966 return sqlite3_malloc(nBytes);
23967 }
23968 if( nBytes<=0 ){
23969 sqlite3_free(pPrior);
23970 return 0;
23971 }
23972 nOld = memsys3Size(pPrior);
23973 if( nBytes<=nOld && nBytes>=nOld-128 ){
23974 return pPrior;
23975 }
23976 memsys3Enter();
23977 p = memsys3MallocUnsafe(nBytes);
23978 if( p ){
23979 if( nOld<nBytes ){
23980 memcpy(p, pPrior, nOld);
23981 }else{
23982 memcpy(p, pPrior, nBytes);
23983 }
23984 memsys3FreeUnsafe(pPrior);
23985 }
23986 memsys3Leave();
23987 return p;
23988}
23989
23990/*
23991** Initialize this module.
23992*/
23993static int memsys3Init(void *NotUsed){
23994 UNUSED_PARAMETER(NotUsed);
23995 if( !sqlite3GlobalConfig.pHeap ){
23996 return SQLITE_ERROR;
23997 }
23998
23999 /* Store a pointer to the memory block in global structure mem3. */
24000 assert( sizeof(Mem3Block)==8 );
24001 mem3.aPool = (Mem3Block *)sqlite3GlobalConfig.pHeap;
24002 mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
24003
24004 /* Initialize the master block. */
24005 mem3.szMaster = mem3.nPool;
24006 mem3.mnMaster = mem3.szMaster;
24007 mem3.iMaster = 1;
24008 mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2;
24009 mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
24010 mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
24011
24012 return SQLITE_OK;
24013}
24014
24015/*
24016** Deinitialize this module.
24017*/
24018static void memsys3Shutdown(void *NotUsed){
24019 UNUSED_PARAMETER(NotUsed);
24020 mem3.mutex = 0;
24021 return;
24022}
24023
24024
24025
24026/*
24027** Open the file indicated and write a log of all unfreed memory
24028** allocations into that log.
24029*/
24030SQLITE_PRIVATE void sqlite3Memsys3Dump(const char *zFilename){
24031#ifdef SQLITE_DEBUG
24032 FILE *out;
24033 u32 i, j;
24034 u32 size;
24035 if( zFilename==0 || zFilename[0]==0 ){
24036 out = stdout;
24037 }else{
24038 out = fopen(zFilename, "w");
24039 if( out==0 ){
24040 fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
24041 zFilename);
24042 return;
24043 }
24044 }
24045 memsys3Enter();
24046 fprintf(out, "CHUNKS:\n");
24047 for(i=1; i<=mem3.nPool; i+=size/4){
24048 size = mem3.aPool[i-1].u.hdr.size4x;
24049 if( size/4<=1 ){
24050 fprintf(out, "%p size error\n", &mem3.aPool[i]);
24051 assert( 0 );
24052 break;
24053 }
24054 if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
24055 fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
24056 assert( 0 );
24057 break;
24058 }
24059 if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
24060 fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
24061 assert( 0 );
24062 break;
24063 }
24064 if( size&1 ){
24065 fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
24066 }else{
24067 fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
24068 i==mem3.iMaster ? " **master**" : "");
24069 }
24070 }
24071 for(i=0; i<MX_SMALL-1; i++){
24072 if( mem3.aiSmall[i]==0 ) continue;
24073 fprintf(out, "small(%2d):", i);
24074 for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
24075 fprintf(out, " %p(%d)", &mem3.aPool[j],
24076 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
24077 }
24078 fprintf(out, "\n");
24079 }
24080 for(i=0; i<N_HASH; i++){
24081 if( mem3.aiHash[i]==0 ) continue;
24082 fprintf(out, "hash(%2d):", i);
24083 for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
24084 fprintf(out, " %p(%d)", &mem3.aPool[j],
24085 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
24086 }
24087 fprintf(out, "\n");
24088 }
24089 fprintf(out, "master=%d\n", mem3.iMaster);
24090 fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8);
24091 fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8);
24092 sqlite3_mutex_leave(mem3.mutex);
24093 if( out==stdout ){
24094 fflush(stdout);
24095 }else{
24096 fclose(out);
24097 }
24098#else
24099 UNUSED_PARAMETER(zFilename);
24100#endif
24101}
24102
24103/*
24104** This routine is the only routine in this file with external
24105** linkage.
24106**
24107** Populate the low-level memory allocation function pointers in
24108** sqlite3GlobalConfig.m with pointers to the routines in this file. The
24109** arguments specify the block of memory to manage.
24110**
24111** This routine is only called by sqlite3_config(), and therefore
24112** is not required to be threadsafe (it is not).
24113*/
24114SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
24115 static const sqlite3_mem_methods mempoolMethods = {
24116 memsys3Malloc,
24117 memsys3Free,
24118 memsys3Realloc,
24119 memsys3Size,
24120 memsys3Roundup,
24121 memsys3Init,
24122 memsys3Shutdown,
24123 0
24124 };
24125 return &mempoolMethods;
24126}
24127
24128#endif /* SQLITE_ENABLE_MEMSYS3 */
24129
24130/************** End of mem3.c ************************************************/
24131/************** Begin file mem5.c ********************************************/
24132/*
24133** 2007 October 14
24134**
24135** The author disclaims copyright to this source code. In place of
24136** a legal notice, here is a blessing:
24137**
24138** May you do good and not evil.
24139** May you find forgiveness for yourself and forgive others.
24140** May you share freely, never taking more than you give.
24141**
24142*************************************************************************
24143** This file contains the C functions that implement a memory
24144** allocation subsystem for use by SQLite.
24145**
24146** This version of the memory allocation subsystem omits all
24147** use of malloc(). The application gives SQLite a block of memory
24148** before calling sqlite3_initialize() from which allocations
24149** are made and returned by the xMalloc() and xRealloc()
24150** implementations. Once sqlite3_initialize() has been called,
24151** the amount of memory available to SQLite is fixed and cannot
24152** be changed.
24153**
24154** This version of the memory allocation subsystem is included
24155** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
24156**
24157** This memory allocator uses the following algorithm:
24158**
24159** 1. All memory allocation sizes are rounded up to a power of 2.
24160**
24161** 2. If two adjacent free blocks are the halves of a larger block,
24162** then the two blocks are coalesced into the single larger block.
24163**
24164** 3. New memory is allocated from the first available free block.
24165**
24166** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
24167** Concerning Dynamic Storage Allocation". Journal of the Association for
24168** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
24169**
24170** Let n be the size of the largest allocation divided by the minimum
24171** allocation size (after rounding all sizes up to a power of 2.) Let M
24172** be the maximum amount of memory ever outstanding at one time. Let
24173** N be the total amount of memory available for allocation. Robson
24174** proved that this memory allocator will never breakdown due to
24175** fragmentation as long as the following constraint holds:
24176**
24177** N >= M*(1 + log2(n)/2) - n + 1
24178**
24179** The sqlite3_status() logic tracks the maximum values of n and M so
24180** that an application can, at any time, verify this constraint.
24181*/
24182/* #include "sqliteInt.h" */
24183
24184/*
24185** This version of the memory allocator is used only when
24186** SQLITE_ENABLE_MEMSYS5 is defined.
24187*/
24188#ifdef SQLITE_ENABLE_MEMSYS5
24189
24190/*
24191** A minimum allocation is an instance of the following structure.
24192** Larger allocations are an array of these structures where the
24193** size of the array is a power of 2.
24194**
24195** The size of this object must be a power of two. That fact is
24196** verified in memsys5Init().
24197*/
24198typedef struct Mem5Link Mem5Link;
24199struct Mem5Link {
24200 int next; /* Index of next free chunk */
24201 int prev; /* Index of previous free chunk */
24202};
24203
24204/*
24205** Maximum size of any allocation is ((1<<LOGMAX)*mem5.szAtom). Since
24206** mem5.szAtom is always at least 8 and 32-bit integers are used,
24207** it is not actually possible to reach this limit.
24208*/
24209#define LOGMAX 30
24210
24211/*
24212** Masks used for mem5.aCtrl[] elements.
24213*/
24214#define CTRL_LOGSIZE 0x1f /* Log2 Size of this block */
24215#define CTRL_FREE 0x20 /* True if not checked out */
24216
24217/*
24218** All of the static variables used by this module are collected
24219** into a single structure named "mem5". This is to keep the
24220** static variables organized and to reduce namespace pollution
24221** when this module is combined with other in the amalgamation.
24222*/
24223static SQLITE_WSD struct Mem5Global {
24224 /*
24225 ** Memory available for allocation
24226 */
24227 int szAtom; /* Smallest possible allocation in bytes */
24228 int nBlock; /* Number of szAtom sized blocks in zPool */
24229 u8 *zPool; /* Memory available to be allocated */
24230
24231 /*
24232 ** Mutex to control access to the memory allocation subsystem.
24233 */
24234 sqlite3_mutex *mutex;
24235
24236#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
24237 /*
24238 ** Performance statistics
24239 */
24240 u64 nAlloc; /* Total number of calls to malloc */
24241 u64 totalAlloc; /* Total of all malloc calls - includes internal frag */
24242 u64 totalExcess; /* Total internal fragmentation */
24243 u32 currentOut; /* Current checkout, including internal fragmentation */
24244 u32 currentCount; /* Current number of distinct checkouts */
24245 u32 maxOut; /* Maximum instantaneous currentOut */
24246 u32 maxCount; /* Maximum instantaneous currentCount */
24247 u32 maxRequest; /* Largest allocation (exclusive of internal frag) */
24248#endif
24249
24250 /*
24251 ** Lists of free blocks. aiFreelist[0] is a list of free blocks of
24252 ** size mem5.szAtom. aiFreelist[1] holds blocks of size szAtom*2.
24253 ** aiFreelist[2] holds free blocks of size szAtom*4. And so forth.
24254 */
24255 int aiFreelist[LOGMAX+1];
24256
24257 /*
24258 ** Space for tracking which blocks are checked out and the size
24259 ** of each block. One byte per block.
24260 */
24261 u8 *aCtrl;
24262
24263} mem5;
24264
24265/*
24266** Access the static variable through a macro for SQLITE_OMIT_WSD.
24267*/
24268#define mem5 GLOBAL(struct Mem5Global, mem5)
24269
24270/*
24271** Assuming mem5.zPool is divided up into an array of Mem5Link
24272** structures, return a pointer to the idx-th such link.
24273*/
24274#define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
24275
24276/*
24277** Unlink the chunk at mem5.aPool[i] from list it is currently
24278** on. It should be found on mem5.aiFreelist[iLogsize].
24279*/
24280static void memsys5Unlink(int i, int iLogsize){
24281 int next, prev;
24282 assert( i>=0 && i<mem5.nBlock );
24283 assert( iLogsize>=0 && iLogsize<=LOGMAX );
24284 assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
24285
24286 next = MEM5LINK(i)->next;
24287 prev = MEM5LINK(i)->prev;
24288 if( prev<0 ){
24289 mem5.aiFreelist[iLogsize] = next;
24290 }else{
24291 MEM5LINK(prev)->next = next;
24292 }
24293 if( next>=0 ){
24294 MEM5LINK(next)->prev = prev;
24295 }
24296}
24297
24298/*
24299** Link the chunk at mem5.aPool[i] so that is on the iLogsize
24300** free list.
24301*/
24302static void memsys5Link(int i, int iLogsize){
24303 int x;
24304 assert( sqlite3_mutex_held(mem5.mutex) );
24305 assert( i>=0 && i<mem5.nBlock );
24306 assert( iLogsize>=0 && iLogsize<=LOGMAX );
24307 assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
24308
24309 x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
24310 MEM5LINK(i)->prev = -1;
24311 if( x>=0 ){
24312 assert( x<mem5.nBlock );
24313 MEM5LINK(x)->prev = i;
24314 }
24315 mem5.aiFreelist[iLogsize] = i;
24316}
24317
24318/*
24319** Obtain or release the mutex needed to access global data structures.
24320*/
24321static void memsys5Enter(void){
24322 sqlite3_mutex_enter(mem5.mutex);
24323}
24324static void memsys5Leave(void){
24325 sqlite3_mutex_leave(mem5.mutex);
24326}
24327
24328/*
24329** Return the size of an outstanding allocation, in bytes.
24330** This only works for chunks that are currently checked out.
24331*/
24332static int memsys5Size(void *p){
24333 int iSize, i;
24334 assert( p!=0 );
24335 i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom);
24336 assert( i>=0 && i<mem5.nBlock );
24337 iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
24338 return iSize;
24339}
24340
24341/*
24342** Return a block of memory of at least nBytes in size.
24343** Return NULL if unable. Return NULL if nBytes==0.
24344**
24345** The caller guarantees that nByte is positive.
24346**
24347** The caller has obtained a mutex prior to invoking this
24348** routine so there is never any chance that two or more
24349** threads can be in this routine at the same time.
24350*/
24351static void *memsys5MallocUnsafe(int nByte){
24352 int i; /* Index of a mem5.aPool[] slot */
24353 int iBin; /* Index into mem5.aiFreelist[] */
24354 int iFullSz; /* Size of allocation rounded up to power of 2 */
24355 int iLogsize; /* Log2 of iFullSz/POW2_MIN */
24356
24357 /* nByte must be a positive */
24358 assert( nByte>0 );
24359
24360 /* No more than 1GiB per allocation */
24361 if( nByte > 0x40000000 ) return 0;
24362
24363#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
24364 /* Keep track of the maximum allocation request. Even unfulfilled
24365 ** requests are counted */
24366 if( (u32)nByte>mem5.maxRequest ){
24367 mem5.maxRequest = nByte;
24368 }
24369#endif
24370
24371
24372 /* Round nByte up to the next valid power of two */
24373 for(iFullSz=mem5.szAtom,iLogsize=0; iFullSz<nByte; iFullSz*=2,iLogsize++){}
24374
24375 /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
24376 ** block. If not, then split a block of the next larger power of
24377 ** two in order to create a new free block of size iLogsize.
24378 */
24379 for(iBin=iLogsize; iBin<=LOGMAX && mem5.aiFreelist[iBin]<0; iBin++){}
24380 if( iBin>LOGMAX ){
24381 testcase( sqlite3GlobalConfig.xLog!=0 );
24382 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
24383 return 0;
24384 }
24385 i = mem5.aiFreelist[iBin];
24386 memsys5Unlink(i, iBin);
24387 while( iBin>iLogsize ){
24388 int newSize;
24389
24390 iBin--;
24391 newSize = 1 << iBin;
24392 mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
24393 memsys5Link(i+newSize, iBin);
24394 }
24395 mem5.aCtrl[i] = iLogsize;
24396
24397#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
24398 /* Update allocator performance statistics. */
24399 mem5.nAlloc++;
24400 mem5.totalAlloc += iFullSz;
24401 mem5.totalExcess += iFullSz - nByte;
24402 mem5.currentCount++;
24403 mem5.currentOut += iFullSz;
24404 if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
24405 if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
24406#endif
24407
24408#ifdef SQLITE_DEBUG
24409 /* Make sure the allocated memory does not assume that it is set to zero
24410 ** or retains a value from a previous allocation */
24411 memset(&mem5.zPool[i*mem5.szAtom], 0xAA, iFullSz);
24412#endif
24413
24414 /* Return a pointer to the allocated memory. */
24415 return (void*)&mem5.zPool[i*mem5.szAtom];
24416}
24417
24418/*
24419** Free an outstanding memory allocation.
24420*/
24421static void memsys5FreeUnsafe(void *pOld){
24422 u32 size, iLogsize;
24423 int iBlock;
24424
24425 /* Set iBlock to the index of the block pointed to by pOld in
24426 ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool.
24427 */
24428 iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom);
24429
24430 /* Check that the pointer pOld points to a valid, non-free block. */
24431 assert( iBlock>=0 && iBlock<mem5.nBlock );
24432 assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 );
24433 assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
24434
24435 iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
24436 size = 1<<iLogsize;
24437 assert( iBlock+size-1<(u32)mem5.nBlock );
24438
24439 mem5.aCtrl[iBlock] |= CTRL_FREE;
24440 mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
24441
24442#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
24443 assert( mem5.currentCount>0 );
24444 assert( mem5.currentOut>=(size*mem5.szAtom) );
24445 mem5.currentCount--;
24446 mem5.currentOut -= size*mem5.szAtom;
24447 assert( mem5.currentOut>0 || mem5.currentCount==0 );
24448 assert( mem5.currentCount>0 || mem5.currentOut==0 );
24449#endif
24450
24451 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
24452 while( ALWAYS(iLogsize<LOGMAX) ){
24453 int iBuddy;
24454 if( (iBlock>>iLogsize) & 1 ){
24455 iBuddy = iBlock - size;
24456 assert( iBuddy>=0 );
24457 }else{
24458 iBuddy = iBlock + size;
24459 if( iBuddy>=mem5.nBlock ) break;
24460 }
24461 if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
24462 memsys5Unlink(iBuddy, iLogsize);
24463 iLogsize++;
24464 if( iBuddy<iBlock ){
24465 mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
24466 mem5.aCtrl[iBlock] = 0;
24467 iBlock = iBuddy;
24468 }else{
24469 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
24470 mem5.aCtrl[iBuddy] = 0;
24471 }
24472 size *= 2;
24473 }
24474
24475#ifdef SQLITE_DEBUG
24476 /* Overwrite freed memory with the 0x55 bit pattern to verify that it is
24477 ** not used after being freed */
24478 memset(&mem5.zPool[iBlock*mem5.szAtom], 0x55, size);
24479#endif
24480
24481 memsys5Link(iBlock, iLogsize);
24482}
24483
24484/*
24485** Allocate nBytes of memory.
24486*/
24487static void *memsys5Malloc(int nBytes){
24488 sqlite3_int64 *p = 0;
24489 if( nBytes>0 ){
24490 memsys5Enter();
24491 p = memsys5MallocUnsafe(nBytes);
24492 memsys5Leave();
24493 }
24494 return (void*)p;
24495}
24496
24497/*
24498** Free memory.
24499**
24500** The outer layer memory allocator prevents this routine from
24501** being called with pPrior==0.
24502*/
24503static void memsys5Free(void *pPrior){
24504 assert( pPrior!=0 );
24505 memsys5Enter();
24506 memsys5FreeUnsafe(pPrior);
24507 memsys5Leave();
24508}
24509
24510/*
24511** Change the size of an existing memory allocation.
24512**
24513** The outer layer memory allocator prevents this routine from
24514** being called with pPrior==0.
24515**
24516** nBytes is always a value obtained from a prior call to
24517** memsys5Round(). Hence nBytes is always a non-negative power
24518** of two. If nBytes==0 that means that an oversize allocation
24519** (an allocation larger than 0x40000000) was requested and this
24520** routine should return 0 without freeing pPrior.
24521*/
24522static void *memsys5Realloc(void *pPrior, int nBytes){
24523 int nOld;
24524 void *p;
24525 assert( pPrior!=0 );
24526 assert( (nBytes&(nBytes-1))==0 ); /* EV: R-46199-30249 */
24527 assert( nBytes>=0 );
24528 if( nBytes==0 ){
24529 return 0;
24530 }
24531 nOld = memsys5Size(pPrior);
24532 if( nBytes<=nOld ){
24533 return pPrior;
24534 }
24535 p = memsys5Malloc(nBytes);
24536 if( p ){
24537 memcpy(p, pPrior, nOld);
24538 memsys5Free(pPrior);
24539 }
24540 return p;
24541}
24542
24543/*
24544** Round up a request size to the next valid allocation size. If
24545** the allocation is too large to be handled by this allocation system,
24546** return 0.
24547**
24548** All allocations must be a power of two and must be expressed by a
24549** 32-bit signed integer. Hence the largest allocation is 0x40000000
24550** or 1073741824 bytes.
24551*/
24552static int memsys5Roundup(int n){
24553 int iFullSz;
24554 if( n > 0x40000000 ) return 0;
24555 for(iFullSz=mem5.szAtom; iFullSz<n; iFullSz *= 2);
24556 return iFullSz;
24557}
24558
24559/*
24560** Return the ceiling of the logarithm base 2 of iValue.
24561**
24562** Examples: memsys5Log(1) -> 0
24563** memsys5Log(2) -> 1
24564** memsys5Log(4) -> 2
24565** memsys5Log(5) -> 3
24566** memsys5Log(8) -> 3
24567** memsys5Log(9) -> 4
24568*/
24569static int memsys5Log(int iValue){
24570 int iLog;
24571 for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
24572 return iLog;
24573}
24574
24575/*
24576** Initialize the memory allocator.
24577**
24578** This routine is not threadsafe. The caller must be holding a mutex
24579** to prevent multiple threads from entering at the same time.
24580*/
24581static int memsys5Init(void *NotUsed){
24582 int ii; /* Loop counter */
24583 int nByte; /* Number of bytes of memory available to this allocator */
24584 u8 *zByte; /* Memory usable by this allocator */
24585 int nMinLog; /* Log base 2 of minimum allocation size in bytes */
24586 int iOffset; /* An offset into mem5.aCtrl[] */
24587
24588 UNUSED_PARAMETER(NotUsed);
24589
24590 /* For the purposes of this routine, disable the mutex */
24591 mem5.mutex = 0;
24592
24593 /* The size of a Mem5Link object must be a power of two. Verify that
24594 ** this is case.
24595 */
24596 assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
24597
24598 nByte = sqlite3GlobalConfig.nHeap;
24599 zByte = (u8*)sqlite3GlobalConfig.pHeap;
24600 assert( zByte!=0 ); /* sqlite3_config() does not allow otherwise */
24601
24602 /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
24603 nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
24604 mem5.szAtom = (1<<nMinLog);
24605 while( (int)sizeof(Mem5Link)>mem5.szAtom ){
24606 mem5.szAtom = mem5.szAtom << 1;
24607 }
24608
24609 mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
24610 mem5.zPool = zByte;
24611 mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
24612
24613 for(ii=0; ii<=LOGMAX; ii++){
24614 mem5.aiFreelist[ii] = -1;
24615 }
24616
24617 iOffset = 0;
24618 for(ii=LOGMAX; ii>=0; ii--){
24619 int nAlloc = (1<<ii);
24620 if( (iOffset+nAlloc)<=mem5.nBlock ){
24621 mem5.aCtrl[iOffset] = ii | CTRL_FREE;
24622 memsys5Link(iOffset, ii);
24623 iOffset += nAlloc;
24624 }
24625 assert((iOffset+nAlloc)>mem5.nBlock);
24626 }
24627
24628 /* If a mutex is required for normal operation, allocate one */
24629 if( sqlite3GlobalConfig.bMemstat==0 ){
24630 mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
24631 }
24632
24633 return SQLITE_OK;
24634}
24635
24636/*
24637** Deinitialize this module.
24638*/
24639static void memsys5Shutdown(void *NotUsed){
24640 UNUSED_PARAMETER(NotUsed);
24641 mem5.mutex = 0;
24642 return;
24643}
24644
24645#ifdef SQLITE_TEST
24646/*
24647** Open the file indicated and write a log of all unfreed memory
24648** allocations into that log.
24649*/
24650SQLITE_PRIVATE void sqlite3Memsys5Dump(const char *zFilename){
24651 FILE *out;
24652 int i, j, n;
24653 int nMinLog;
24654
24655 if( zFilename==0 || zFilename[0]==0 ){
24656 out = stdout;
24657 }else{
24658 out = fopen(zFilename, "w");
24659 if( out==0 ){
24660 fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
24661 zFilename);
24662 return;
24663 }
24664 }
24665 memsys5Enter();
24666 nMinLog = memsys5Log(mem5.szAtom);
24667 for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
24668 for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
24669 fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
24670 }
24671 fprintf(out, "mem5.nAlloc = %llu\n", mem5.nAlloc);
24672 fprintf(out, "mem5.totalAlloc = %llu\n", mem5.totalAlloc);
24673 fprintf(out, "mem5.totalExcess = %llu\n", mem5.totalExcess);
24674 fprintf(out, "mem5.currentOut = %u\n", mem5.currentOut);
24675 fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
24676 fprintf(out, "mem5.maxOut = %u\n", mem5.maxOut);
24677 fprintf(out, "mem5.maxCount = %u\n", mem5.maxCount);
24678 fprintf(out, "mem5.maxRequest = %u\n", mem5.maxRequest);
24679 memsys5Leave();
24680 if( out==stdout ){
24681 fflush(stdout);
24682 }else{
24683 fclose(out);
24684 }
24685}
24686#endif
24687
24688/*
24689** This routine is the only routine in this file with external
24690** linkage. It returns a pointer to a static sqlite3_mem_methods
24691** struct populated with the memsys5 methods.
24692*/
24693SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
24694 static const sqlite3_mem_methods memsys5Methods = {
24695 memsys5Malloc,
24696 memsys5Free,
24697 memsys5Realloc,
24698 memsys5Size,
24699 memsys5Roundup,
24700 memsys5Init,
24701 memsys5Shutdown,
24702 0
24703 };
24704 return &memsys5Methods;
24705}
24706
24707#endif /* SQLITE_ENABLE_MEMSYS5 */
24708
24709/************** End of mem5.c ************************************************/
24710/************** Begin file mutex.c *******************************************/
24711/*
24712** 2007 August 14
24713**
24714** The author disclaims copyright to this source code. In place of
24715** a legal notice, here is a blessing:
24716**
24717** May you do good and not evil.
24718** May you find forgiveness for yourself and forgive others.
24719** May you share freely, never taking more than you give.
24720**
24721*************************************************************************
24722** This file contains the C functions that implement mutexes.
24723**
24724** This file contains code that is common across all mutex implementations.
24725*/
24726/* #include "sqliteInt.h" */
24727
24728#if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
24729/*
24730** For debugging purposes, record when the mutex subsystem is initialized
24731** and uninitialized so that we can assert() if there is an attempt to
24732** allocate a mutex while the system is uninitialized.
24733*/
24734static SQLITE_WSD int mutexIsInit = 0;
24735#endif /* SQLITE_DEBUG && !defined(SQLITE_MUTEX_OMIT) */
24736
24737
24738#ifndef SQLITE_MUTEX_OMIT
24739
24740#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
24741/*
24742** This block (enclosed by SQLITE_ENABLE_MULTITHREADED_CHECKS) contains
24743** the implementation of a wrapper around the system default mutex
24744** implementation (sqlite3DefaultMutex()).
24745**
24746** Most calls are passed directly through to the underlying default
24747** mutex implementation. Except, if a mutex is configured by calling
24748** sqlite3MutexWarnOnContention() on it, then if contention is ever
24749** encountered within xMutexEnter() a warning is emitted via sqlite3_log().
24750**
24751** This type of mutex is used as the database handle mutex when testing
24752** apps that usually use SQLITE_CONFIG_MULTITHREAD mode.
24753*/
24754
24755/*
24756** Type for all mutexes used when SQLITE_ENABLE_MULTITHREADED_CHECKS
24757** is defined. Variable CheckMutex.mutex is a pointer to the real mutex
24758** allocated by the system mutex implementation. Variable iType is usually set
24759** to the type of mutex requested - SQLITE_MUTEX_RECURSIVE, SQLITE_MUTEX_FAST
24760** or one of the static mutex identifiers. Or, if this is a recursive mutex
24761** that has been configured using sqlite3MutexWarnOnContention(), it is
24762** set to SQLITE_MUTEX_WARNONCONTENTION.
24763*/
24764typedef struct CheckMutex CheckMutex;
24765struct CheckMutex {
24766 int iType;
24767 sqlite3_mutex *mutex;
24768};
24769
24770#define SQLITE_MUTEX_WARNONCONTENTION (-1)
24771
24772/*
24773** Pointer to real mutex methods object used by the CheckMutex
24774** implementation. Set by checkMutexInit().
24775*/
24776static SQLITE_WSD const sqlite3_mutex_methods *pGlobalMutexMethods;
24777
24778#ifdef SQLITE_DEBUG
24779static int checkMutexHeld(sqlite3_mutex *p){
24780 return pGlobalMutexMethods->xMutexHeld(((CheckMutex*)p)->mutex);
24781}
24782static int checkMutexNotheld(sqlite3_mutex *p){
24783 return pGlobalMutexMethods->xMutexNotheld(((CheckMutex*)p)->mutex);
24784}
24785#endif
24786
24787/*
24788** Initialize and deinitialize the mutex subsystem.
24789*/
24790static int checkMutexInit(void){
24791 pGlobalMutexMethods = sqlite3DefaultMutex();
24792 return SQLITE_OK;
24793}
24794static int checkMutexEnd(void){
24795 pGlobalMutexMethods = 0;
24796 return SQLITE_OK;
24797}
24798
24799/*
24800** Allocate a mutex.
24801*/
24802static sqlite3_mutex *checkMutexAlloc(int iType){
24803 static CheckMutex staticMutexes[] = {
24804 {2, 0}, {3, 0}, {4, 0}, {5, 0},
24805 {6, 0}, {7, 0}, {8, 0}, {9, 0},
24806 {10, 0}, {11, 0}, {12, 0}, {13, 0}
24807 };
24808 CheckMutex *p = 0;
24809
24810 assert( SQLITE_MUTEX_RECURSIVE==1 && SQLITE_MUTEX_FAST==0 );
24811 if( iType<2 ){
24812 p = sqlite3MallocZero(sizeof(CheckMutex));
24813 if( p==0 ) return 0;
24814 p->iType = iType;
24815 }else{
24816#ifdef SQLITE_ENABLE_API_ARMOR
24817 if( iType-2>=ArraySize(staticMutexes) ){
24818 (void)SQLITE_MISUSE_BKPT;
24819 return 0;
24820 }
24821#endif
24822 p = &staticMutexes[iType-2];
24823 }
24824
24825 if( p->mutex==0 ){
24826 p->mutex = pGlobalMutexMethods->xMutexAlloc(iType);
24827 if( p->mutex==0 ){
24828 if( iType<2 ){
24829 sqlite3_free(p);
24830 }
24831 p = 0;
24832 }
24833 }
24834
24835 return (sqlite3_mutex*)p;
24836}
24837
24838/*
24839** Free a mutex.
24840*/
24841static void checkMutexFree(sqlite3_mutex *p){
24842 assert( SQLITE_MUTEX_RECURSIVE<2 );
24843 assert( SQLITE_MUTEX_FAST<2 );
24844 assert( SQLITE_MUTEX_WARNONCONTENTION<2 );
24845
24846#if SQLITE_ENABLE_API_ARMOR
24847 if( ((CheckMutex*)p)->iType<2 )
24848#endif
24849 {
24850 CheckMutex *pCheck = (CheckMutex*)p;
24851 pGlobalMutexMethods->xMutexFree(pCheck->mutex);
24852 sqlite3_free(pCheck);
24853 }
24854#ifdef SQLITE_ENABLE_API_ARMOR
24855 else{
24856 (void)SQLITE_MISUSE_BKPT;
24857 }
24858#endif
24859}
24860
24861/*
24862** Enter the mutex.
24863*/
24864static void checkMutexEnter(sqlite3_mutex *p){
24865 CheckMutex *pCheck = (CheckMutex*)p;
24866 if( pCheck->iType==SQLITE_MUTEX_WARNONCONTENTION ){
24867 if( SQLITE_OK==pGlobalMutexMethods->xMutexTry(pCheck->mutex) ){
24868 return;
24869 }
24870 sqlite3_log(SQLITE_MISUSE,
24871 "illegal multi-threaded access to database connection"
24872 );
24873 }
24874 pGlobalMutexMethods->xMutexEnter(pCheck->mutex);
24875}
24876
24877/*
24878** Enter the mutex (do not block).
24879*/
24880static int checkMutexTry(sqlite3_mutex *p){
24881 CheckMutex *pCheck = (CheckMutex*)p;
24882 return pGlobalMutexMethods->xMutexTry(pCheck->mutex);
24883}
24884
24885/*
24886** Leave the mutex.
24887*/
24888static void checkMutexLeave(sqlite3_mutex *p){
24889 CheckMutex *pCheck = (CheckMutex*)p;
24890 pGlobalMutexMethods->xMutexLeave(pCheck->mutex);
24891}
24892
24893sqlite3_mutex_methods const *multiThreadedCheckMutex(void){
24894 static const sqlite3_mutex_methods sMutex = {
24895 checkMutexInit,
24896 checkMutexEnd,
24897 checkMutexAlloc,
24898 checkMutexFree,
24899 checkMutexEnter,
24900 checkMutexTry,
24901 checkMutexLeave,
24902#ifdef SQLITE_DEBUG
24903 checkMutexHeld,
24904 checkMutexNotheld
24905#else
24906 0,
24907 0
24908#endif
24909 };
24910 return &sMutex;
24911}
24912
24913/*
24914** Mark the SQLITE_MUTEX_RECURSIVE mutex passed as the only argument as
24915** one on which there should be no contention.
24916*/
24917SQLITE_PRIVATE void sqlite3MutexWarnOnContention(sqlite3_mutex *p){
24918 if( sqlite3GlobalConfig.mutex.xMutexAlloc==checkMutexAlloc ){
24919 CheckMutex *pCheck = (CheckMutex*)p;
24920 assert( pCheck->iType==SQLITE_MUTEX_RECURSIVE );
24921 pCheck->iType = SQLITE_MUTEX_WARNONCONTENTION;
24922 }
24923}
24924#endif /* ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS */
24925
24926/*
24927** Initialize the mutex system.
24928*/
24929SQLITE_PRIVATE int sqlite3MutexInit(void){
24930 int rc = SQLITE_OK;
24931 if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
24932 /* If the xMutexAlloc method has not been set, then the user did not
24933 ** install a mutex implementation via sqlite3_config() prior to
24934 ** sqlite3_initialize() being called. This block copies pointers to
24935 ** the default implementation into the sqlite3GlobalConfig structure.
24936 */
24937 sqlite3_mutex_methods const *pFrom;
24938 sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
24939
24940 if( sqlite3GlobalConfig.bCoreMutex ){
24941#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
24942 pFrom = multiThreadedCheckMutex();
24943#else
24944 pFrom = sqlite3DefaultMutex();
24945#endif
24946 }else{
24947 pFrom = sqlite3NoopMutex();
24948 }
24949 pTo->xMutexInit = pFrom->xMutexInit;
24950 pTo->xMutexEnd = pFrom->xMutexEnd;
24951 pTo->xMutexFree = pFrom->xMutexFree;
24952 pTo->xMutexEnter = pFrom->xMutexEnter;
24953 pTo->xMutexTry = pFrom->xMutexTry;
24954 pTo->xMutexLeave = pFrom->xMutexLeave;
24955 pTo->xMutexHeld = pFrom->xMutexHeld;
24956 pTo->xMutexNotheld = pFrom->xMutexNotheld;
24957 sqlite3MemoryBarrier();
24958 pTo->xMutexAlloc = pFrom->xMutexAlloc;
24959 }
24960 assert( sqlite3GlobalConfig.mutex.xMutexInit );
24961 rc = sqlite3GlobalConfig.mutex.xMutexInit();
24962
24963#ifdef SQLITE_DEBUG
24964 GLOBAL(int, mutexIsInit) = 1;
24965#endif
24966
24967 return rc;
24968}
24969
24970/*
24971** Shutdown the mutex system. This call frees resources allocated by
24972** sqlite3MutexInit().
24973*/
24974SQLITE_PRIVATE int sqlite3MutexEnd(void){
24975 int rc = SQLITE_OK;
24976 if( sqlite3GlobalConfig.mutex.xMutexEnd ){
24977 rc = sqlite3GlobalConfig.mutex.xMutexEnd();
24978 }
24979
24980#ifdef SQLITE_DEBUG
24981 GLOBAL(int, mutexIsInit) = 0;
24982#endif
24983
24984 return rc;
24985}
24986
24987/*
24988** Retrieve a pointer to a static mutex or allocate a new dynamic one.
24989*/
24990SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
24991#ifndef SQLITE_OMIT_AUTOINIT
24992 if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0;
24993 if( id>SQLITE_MUTEX_RECURSIVE && sqlite3MutexInit() ) return 0;
24994#endif
24995 assert( sqlite3GlobalConfig.mutex.xMutexAlloc );
24996 return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
24997}
24998
24999SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
25000 if( !sqlite3GlobalConfig.bCoreMutex ){
25001 return 0;
25002 }
25003 assert( GLOBAL(int, mutexIsInit) );
25004 assert( sqlite3GlobalConfig.mutex.xMutexAlloc );
25005 return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
25006}
25007
25008/*
25009** Free a dynamic mutex.
25010*/
25011SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
25012 if( p ){
25013 assert( sqlite3GlobalConfig.mutex.xMutexFree );
25014 sqlite3GlobalConfig.mutex.xMutexFree(p);
25015 }
25016}
25017
25018/*
25019** Obtain the mutex p. If some other thread already has the mutex, block
25020** until it can be obtained.
25021*/
25022SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
25023 if( p ){
25024 assert( sqlite3GlobalConfig.mutex.xMutexEnter );
25025 sqlite3GlobalConfig.mutex.xMutexEnter(p);
25026 }
25027}
25028
25029/*
25030** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
25031** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
25032*/
25033SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
25034 int rc = SQLITE_OK;
25035 if( p ){
25036 assert( sqlite3GlobalConfig.mutex.xMutexTry );
25037 return sqlite3GlobalConfig.mutex.xMutexTry(p);
25038 }
25039 return rc;
25040}
25041
25042/*
25043** The sqlite3_mutex_leave() routine exits a mutex that was previously
25044** entered by the same thread. The behavior is undefined if the mutex
25045** is not currently entered. If a NULL pointer is passed as an argument
25046** this function is a no-op.
25047*/
25048SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
25049 if( p ){
25050 assert( sqlite3GlobalConfig.mutex.xMutexLeave );
25051 sqlite3GlobalConfig.mutex.xMutexLeave(p);
25052 }
25053}
25054
25055#ifndef NDEBUG
25056/*
25057** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
25058** intended for use inside assert() statements.
25059*/
25060SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
25061 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexHeld );
25062 return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
25063}
25064SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
25065 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld );
25066 return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
25067}
25068#endif
25069
25070#endif /* !defined(SQLITE_MUTEX_OMIT) */
25071
25072/************** End of mutex.c ***********************************************/
25073/************** Begin file mutex_noop.c **************************************/
25074/*
25075** 2008 October 07
25076**
25077** The author disclaims copyright to this source code. In place of
25078** a legal notice, here is a blessing:
25079**
25080** May you do good and not evil.
25081** May you find forgiveness for yourself and forgive others.
25082** May you share freely, never taking more than you give.
25083**
25084*************************************************************************
25085** This file contains the C functions that implement mutexes.
25086**
25087** This implementation in this file does not provide any mutual
25088** exclusion and is thus suitable for use only in applications
25089** that use SQLite in a single thread. The routines defined
25090** here are place-holders. Applications can substitute working
25091** mutex routines at start-time using the
25092**
25093** sqlite3_config(SQLITE_CONFIG_MUTEX,...)
25094**
25095** interface.
25096**
25097** If compiled with SQLITE_DEBUG, then additional logic is inserted
25098** that does error checking on mutexes to make sure they are being
25099** called correctly.
25100*/
25101/* #include "sqliteInt.h" */
25102
25103#ifndef SQLITE_MUTEX_OMIT
25104
25105#ifndef SQLITE_DEBUG
25106/*
25107** Stub routines for all mutex methods.
25108**
25109** This routines provide no mutual exclusion or error checking.
25110*/
25111static int noopMutexInit(void){ return SQLITE_OK; }
25112static int noopMutexEnd(void){ return SQLITE_OK; }
25113static sqlite3_mutex *noopMutexAlloc(int id){
25114 UNUSED_PARAMETER(id);
25115 return (sqlite3_mutex*)8;
25116}
25117static void noopMutexFree(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
25118static void noopMutexEnter(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
25119static int noopMutexTry(sqlite3_mutex *p){
25120 UNUSED_PARAMETER(p);
25121 return SQLITE_OK;
25122}
25123static void noopMutexLeave(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
25124
25125SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
25126 static const sqlite3_mutex_methods sMutex = {
25127 noopMutexInit,
25128 noopMutexEnd,
25129 noopMutexAlloc,
25130 noopMutexFree,
25131 noopMutexEnter,
25132 noopMutexTry,
25133 noopMutexLeave,
25134
25135 0,
25136 0,
25137 };
25138
25139 return &sMutex;
25140}
25141#endif /* !SQLITE_DEBUG */
25142
25143#ifdef SQLITE_DEBUG
25144/*
25145** In this implementation, error checking is provided for testing
25146** and debugging purposes. The mutexes still do not provide any
25147** mutual exclusion.
25148*/
25149
25150/*
25151** The mutex object
25152*/
25153typedef struct sqlite3_debug_mutex {
25154 int id; /* The mutex type */
25155 int cnt; /* Number of entries without a matching leave */
25156} sqlite3_debug_mutex;
25157
25158/*
25159** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
25160** intended for use inside assert() statements.
25161*/
25162static int debugMutexHeld(sqlite3_mutex *pX){
25163 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
25164 return p==0 || p->cnt>0;
25165}
25166static int debugMutexNotheld(sqlite3_mutex *pX){
25167 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
25168 return p==0 || p->cnt==0;
25169}
25170
25171/*
25172** Initialize and deinitialize the mutex subsystem.
25173*/
25174static int debugMutexInit(void){ return SQLITE_OK; }
25175static int debugMutexEnd(void){ return SQLITE_OK; }
25176
25177/*
25178** The sqlite3_mutex_alloc() routine allocates a new
25179** mutex and returns a pointer to it. If it returns NULL
25180** that means that a mutex could not be allocated.
25181*/
25182static sqlite3_mutex *debugMutexAlloc(int id){
25183 static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_VFS3 - 1];
25184 sqlite3_debug_mutex *pNew = 0;
25185 switch( id ){
25186 case SQLITE_MUTEX_FAST:
25187 case SQLITE_MUTEX_RECURSIVE: {
25188 pNew = sqlite3Malloc(sizeof(*pNew));
25189 if( pNew ){
25190 pNew->id = id;
25191 pNew->cnt = 0;
25192 }
25193 break;
25194 }
25195 default: {
25196#ifdef SQLITE_ENABLE_API_ARMOR
25197 if( id-2<0 || id-2>=ArraySize(aStatic) ){
25198 (void)SQLITE_MISUSE_BKPT;
25199 return 0;
25200 }
25201#endif
25202 pNew = &aStatic[id-2];
25203 pNew->id = id;
25204 break;
25205 }
25206 }
25207 return (sqlite3_mutex*)pNew;
25208}
25209
25210/*
25211** This routine deallocates a previously allocated mutex.
25212*/
25213static void debugMutexFree(sqlite3_mutex *pX){
25214 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
25215 assert( p->cnt==0 );
25216 if( p->id==SQLITE_MUTEX_RECURSIVE || p->id==SQLITE_MUTEX_FAST ){
25217 sqlite3_free(p);
25218 }else{
25219#ifdef SQLITE_ENABLE_API_ARMOR
25220 (void)SQLITE_MISUSE_BKPT;
25221#endif
25222 }
25223}
25224
25225/*
25226** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
25227** to enter a mutex. If another thread is already within the mutex,
25228** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
25229** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
25230** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
25231** be entered multiple times by the same thread. In such cases the,
25232** mutex must be exited an equal number of times before another thread
25233** can enter. If the same thread tries to enter any other kind of mutex
25234** more than once, the behavior is undefined.
25235*/
25236static void debugMutexEnter(sqlite3_mutex *pX){
25237 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
25238 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
25239 p->cnt++;
25240}
25241static int debugMutexTry(sqlite3_mutex *pX){
25242 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
25243 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
25244 p->cnt++;
25245 return SQLITE_OK;
25246}
25247
25248/*
25249** The sqlite3_mutex_leave() routine exits a mutex that was
25250** previously entered by the same thread. The behavior
25251** is undefined if the mutex is not currently entered or
25252** is not currently allocated. SQLite will never do either.
25253*/
25254static void debugMutexLeave(sqlite3_mutex *pX){
25255 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
25256 assert( debugMutexHeld(pX) );
25257 p->cnt--;
25258 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
25259}
25260
25261SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
25262 static const sqlite3_mutex_methods sMutex = {
25263 debugMutexInit,
25264 debugMutexEnd,
25265 debugMutexAlloc,
25266 debugMutexFree,
25267 debugMutexEnter,
25268 debugMutexTry,
25269 debugMutexLeave,
25270
25271 debugMutexHeld,
25272 debugMutexNotheld
25273 };
25274
25275 return &sMutex;
25276}
25277#endif /* SQLITE_DEBUG */
25278
25279/*
25280** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
25281** is used regardless of the run-time threadsafety setting.
25282*/
25283#ifdef SQLITE_MUTEX_NOOP
25284SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
25285 return sqlite3NoopMutex();
25286}
25287#endif /* defined(SQLITE_MUTEX_NOOP) */
25288#endif /* !defined(SQLITE_MUTEX_OMIT) */
25289
25290/************** End of mutex_noop.c ******************************************/
25291/************** Begin file mutex_unix.c **************************************/
25292/*
25293** 2007 August 28
25294**
25295** The author disclaims copyright to this source code. In place of
25296** a legal notice, here is a blessing:
25297**
25298** May you do good and not evil.
25299** May you find forgiveness for yourself and forgive others.
25300** May you share freely, never taking more than you give.
25301**
25302*************************************************************************
25303** This file contains the C functions that implement mutexes for pthreads
25304*/
25305/* #include "sqliteInt.h" */
25306
25307/*
25308** The code in this file is only used if we are compiling threadsafe
25309** under unix with pthreads.
25310**
25311** Note that this implementation requires a version of pthreads that
25312** supports recursive mutexes.
25313*/
25314#ifdef SQLITE_MUTEX_PTHREADS
25315
25316#include <pthread.h>
25317
25318/*
25319** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
25320** are necessary under two condidtions: (1) Debug builds and (2) using
25321** home-grown mutexes. Encapsulate these conditions into a single #define.
25322*/
25323#if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
25324# define SQLITE_MUTEX_NREF 1
25325#else
25326# define SQLITE_MUTEX_NREF 0
25327#endif
25328
25329/*
25330** Each recursive mutex is an instance of the following structure.
25331*/
25332struct sqlite3_mutex {
25333 pthread_mutex_t mutex; /* Mutex controlling the lock */
25334#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
25335 int id; /* Mutex type */
25336#endif
25337#if SQLITE_MUTEX_NREF
25338 volatile int nRef; /* Number of entrances */
25339 volatile pthread_t owner; /* Thread that is within this mutex */
25340 int trace; /* True to trace changes */
25341#endif
25342};
25343#if SQLITE_MUTEX_NREF
25344# define SQLITE3_MUTEX_INITIALIZER(id) \
25345 {PTHREAD_MUTEX_INITIALIZER,id,0,(pthread_t)0,0}
25346#elif defined(SQLITE_ENABLE_API_ARMOR)
25347# define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER, id }
25348#else
25349#define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER }
25350#endif
25351
25352/*
25353** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
25354** intended for use only inside assert() statements. On some platforms,
25355** there might be race conditions that can cause these routines to
25356** deliver incorrect results. In particular, if pthread_equal() is
25357** not an atomic operation, then these routines might delivery
25358** incorrect results. On most platforms, pthread_equal() is a
25359** comparison of two integers and is therefore atomic. But we are
25360** told that HPUX is not such a platform. If so, then these routines
25361** will not always work correctly on HPUX.
25362**
25363** On those platforms where pthread_equal() is not atomic, SQLite
25364** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
25365** make sure no assert() statements are evaluated and hence these
25366** routines are never called.
25367*/
25368#if !defined(NDEBUG) || defined(SQLITE_DEBUG)
25369static int pthreadMutexHeld(sqlite3_mutex *p){
25370 return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
25371}
25372static int pthreadMutexNotheld(sqlite3_mutex *p){
25373 return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
25374}
25375#endif
25376
25377/*
25378** Try to provide a memory barrier operation, needed for initialization
25379** and also for the implementation of xShmBarrier in the VFS in cases
25380** where SQLite is compiled without mutexes.
25381*/
25382SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
25383#if defined(SQLITE_MEMORY_BARRIER)
25384 SQLITE_MEMORY_BARRIER;
25385#elif defined(__GNUC__) && GCC_VERSION>=4001000
25386 __sync_synchronize();
25387#endif
25388}
25389
25390/*
25391** Initialize and deinitialize the mutex subsystem.
25392*/
25393static int pthreadMutexInit(void){ return SQLITE_OK; }
25394static int pthreadMutexEnd(void){ return SQLITE_OK; }
25395
25396/*
25397** The sqlite3_mutex_alloc() routine allocates a new
25398** mutex and returns a pointer to it. If it returns NULL
25399** that means that a mutex could not be allocated. SQLite
25400** will unwind its stack and return an error. The argument
25401** to sqlite3_mutex_alloc() is one of these integer constants:
25402**
25403** <ul>
25404** <li> SQLITE_MUTEX_FAST
25405** <li> SQLITE_MUTEX_RECURSIVE
25406** <li> SQLITE_MUTEX_STATIC_MASTER
25407** <li> SQLITE_MUTEX_STATIC_MEM
25408** <li> SQLITE_MUTEX_STATIC_OPEN
25409** <li> SQLITE_MUTEX_STATIC_PRNG
25410** <li> SQLITE_MUTEX_STATIC_LRU
25411** <li> SQLITE_MUTEX_STATIC_PMEM
25412** <li> SQLITE_MUTEX_STATIC_APP1
25413** <li> SQLITE_MUTEX_STATIC_APP2
25414** <li> SQLITE_MUTEX_STATIC_APP3
25415** <li> SQLITE_MUTEX_STATIC_VFS1
25416** <li> SQLITE_MUTEX_STATIC_VFS2
25417** <li> SQLITE_MUTEX_STATIC_VFS3
25418** </ul>
25419**
25420** The first two constants cause sqlite3_mutex_alloc() to create
25421** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
25422** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
25423** The mutex implementation does not need to make a distinction
25424** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
25425** not want to. But SQLite will only request a recursive mutex in
25426** cases where it really needs one. If a faster non-recursive mutex
25427** implementation is available on the host platform, the mutex subsystem
25428** might return such a mutex in response to SQLITE_MUTEX_FAST.
25429**
25430** The other allowed parameters to sqlite3_mutex_alloc() each return
25431** a pointer to a static preexisting mutex. Six static mutexes are
25432** used by the current version of SQLite. Future versions of SQLite
25433** may add additional static mutexes. Static mutexes are for internal
25434** use by SQLite only. Applications that use SQLite mutexes should
25435** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
25436** SQLITE_MUTEX_RECURSIVE.
25437**
25438** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
25439** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
25440** returns a different mutex on every call. But for the static
25441** mutex types, the same mutex is returned on every call that has
25442** the same type number.
25443*/
25444static sqlite3_mutex *pthreadMutexAlloc(int iType){
25445 static sqlite3_mutex staticMutexes[] = {
25446 SQLITE3_MUTEX_INITIALIZER(2),
25447 SQLITE3_MUTEX_INITIALIZER(3),
25448 SQLITE3_MUTEX_INITIALIZER(4),
25449 SQLITE3_MUTEX_INITIALIZER(5),
25450 SQLITE3_MUTEX_INITIALIZER(6),
25451 SQLITE3_MUTEX_INITIALIZER(7),
25452 SQLITE3_MUTEX_INITIALIZER(8),
25453 SQLITE3_MUTEX_INITIALIZER(9),
25454 SQLITE3_MUTEX_INITIALIZER(10),
25455 SQLITE3_MUTEX_INITIALIZER(11),
25456 SQLITE3_MUTEX_INITIALIZER(12),
25457 SQLITE3_MUTEX_INITIALIZER(13)
25458 };
25459 sqlite3_mutex *p;
25460 switch( iType ){
25461 case SQLITE_MUTEX_RECURSIVE: {
25462 p = sqlite3MallocZero( sizeof(*p) );
25463 if( p ){
25464#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
25465 /* If recursive mutexes are not available, we will have to
25466 ** build our own. See below. */
25467 pthread_mutex_init(&p->mutex, 0);
25468#else
25469 /* Use a recursive mutex if it is available */
25470 pthread_mutexattr_t recursiveAttr;
25471 pthread_mutexattr_init(&recursiveAttr);
25472 pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
25473 pthread_mutex_init(&p->mutex, &recursiveAttr);
25474 pthread_mutexattr_destroy(&recursiveAttr);
25475#endif
25476#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
25477 p->id = SQLITE_MUTEX_RECURSIVE;
25478#endif
25479 }
25480 break;
25481 }
25482 case SQLITE_MUTEX_FAST: {
25483 p = sqlite3MallocZero( sizeof(*p) );
25484 if( p ){
25485 pthread_mutex_init(&p->mutex, 0);
25486#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
25487 p->id = SQLITE_MUTEX_FAST;
25488#endif
25489 }
25490 break;
25491 }
25492 default: {
25493#ifdef SQLITE_ENABLE_API_ARMOR
25494 if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){
25495 (void)SQLITE_MISUSE_BKPT;
25496 return 0;
25497 }
25498#endif
25499 p = &staticMutexes[iType-2];
25500 break;
25501 }
25502 }
25503#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
25504 assert( p==0 || p->id==iType );
25505#endif
25506 return p;
25507}
25508
25509
25510/*
25511** This routine deallocates a previously
25512** allocated mutex. SQLite is careful to deallocate every
25513** mutex that it allocates.
25514*/
25515static void pthreadMutexFree(sqlite3_mutex *p){
25516 assert( p->nRef==0 );
25517#if SQLITE_ENABLE_API_ARMOR
25518 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE )
25519#endif
25520 {
25521 pthread_mutex_destroy(&p->mutex);
25522 sqlite3_free(p);
25523 }
25524#ifdef SQLITE_ENABLE_API_ARMOR
25525 else{
25526 (void)SQLITE_MISUSE_BKPT;
25527 }
25528#endif
25529}
25530
25531/*
25532** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
25533** to enter a mutex. If another thread is already within the mutex,
25534** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
25535** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
25536** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
25537** be entered multiple times by the same thread. In such cases the,
25538** mutex must be exited an equal number of times before another thread
25539** can enter. If the same thread tries to enter any other kind of mutex
25540** more than once, the behavior is undefined.
25541*/
25542static void pthreadMutexEnter(sqlite3_mutex *p){
25543 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
25544
25545#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
25546 /* If recursive mutexes are not available, then we have to grow
25547 ** our own. This implementation assumes that pthread_equal()
25548 ** is atomic - that it cannot be deceived into thinking self
25549 ** and p->owner are equal if p->owner changes between two values
25550 ** that are not equal to self while the comparison is taking place.
25551 ** This implementation also assumes a coherent cache - that
25552 ** separate processes cannot read different values from the same
25553 ** address at the same time. If either of these two conditions
25554 ** are not met, then the mutexes will fail and problems will result.
25555 */
25556 {
25557 pthread_t self = pthread_self();
25558 if( p->nRef>0 && pthread_equal(p->owner, self) ){
25559 p->nRef++;
25560 }else{
25561 pthread_mutex_lock(&p->mutex);
25562 assert( p->nRef==0 );
25563 p->owner = self;
25564 p->nRef = 1;
25565 }
25566 }
25567#else
25568 /* Use the built-in recursive mutexes if they are available.
25569 */
25570 pthread_mutex_lock(&p->mutex);
25571#if SQLITE_MUTEX_NREF
25572 assert( p->nRef>0 || p->owner==0 );
25573 p->owner = pthread_self();
25574 p->nRef++;
25575#endif
25576#endif
25577
25578#ifdef SQLITE_DEBUG
25579 if( p->trace ){
25580 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
25581 }
25582#endif
25583}
25584static int pthreadMutexTry(sqlite3_mutex *p){
25585 int rc;
25586 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
25587
25588#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
25589 /* If recursive mutexes are not available, then we have to grow
25590 ** our own. This implementation assumes that pthread_equal()
25591 ** is atomic - that it cannot be deceived into thinking self
25592 ** and p->owner are equal if p->owner changes between two values
25593 ** that are not equal to self while the comparison is taking place.
25594 ** This implementation also assumes a coherent cache - that
25595 ** separate processes cannot read different values from the same
25596 ** address at the same time. If either of these two conditions
25597 ** are not met, then the mutexes will fail and problems will result.
25598 */
25599 {
25600 pthread_t self = pthread_self();
25601 if( p->nRef>0 && pthread_equal(p->owner, self) ){
25602 p->nRef++;
25603 rc = SQLITE_OK;
25604 }else if( pthread_mutex_trylock(&p->mutex)==0 ){
25605 assert( p->nRef==0 );
25606 p->owner = self;
25607 p->nRef = 1;
25608 rc = SQLITE_OK;
25609 }else{
25610 rc = SQLITE_BUSY;
25611 }
25612 }
25613#else
25614 /* Use the built-in recursive mutexes if they are available.
25615 */
25616 if( pthread_mutex_trylock(&p->mutex)==0 ){
25617#if SQLITE_MUTEX_NREF
25618 p->owner = pthread_self();
25619 p->nRef++;
25620#endif
25621 rc = SQLITE_OK;
25622 }else{
25623 rc = SQLITE_BUSY;
25624 }
25625#endif
25626
25627#ifdef SQLITE_DEBUG
25628 if( rc==SQLITE_OK && p->trace ){
25629 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
25630 }
25631#endif
25632 return rc;
25633}
25634
25635/*
25636** The sqlite3_mutex_leave() routine exits a mutex that was
25637** previously entered by the same thread. The behavior
25638** is undefined if the mutex is not currently entered or
25639** is not currently allocated. SQLite will never do either.
25640*/
25641static void pthreadMutexLeave(sqlite3_mutex *p){
25642 assert( pthreadMutexHeld(p) );
25643#if SQLITE_MUTEX_NREF
25644 p->nRef--;
25645 if( p->nRef==0 ) p->owner = 0;
25646#endif
25647 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
25648
25649#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
25650 if( p->nRef==0 ){
25651 pthread_mutex_unlock(&p->mutex);
25652 }
25653#else
25654 pthread_mutex_unlock(&p->mutex);
25655#endif
25656
25657#ifdef SQLITE_DEBUG
25658 if( p->trace ){
25659 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
25660 }
25661#endif
25662}
25663
25664SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
25665 static const sqlite3_mutex_methods sMutex = {
25666 pthreadMutexInit,
25667 pthreadMutexEnd,
25668 pthreadMutexAlloc,
25669 pthreadMutexFree,
25670 pthreadMutexEnter,
25671 pthreadMutexTry,
25672 pthreadMutexLeave,
25673#ifdef SQLITE_DEBUG
25674 pthreadMutexHeld,
25675 pthreadMutexNotheld
25676#else
25677 0,
25678 0
25679#endif
25680 };
25681
25682 return &sMutex;
25683}
25684
25685#endif /* SQLITE_MUTEX_PTHREADS */
25686
25687/************** End of mutex_unix.c ******************************************/
25688/************** Begin file mutex_w32.c ***************************************/
25689/*
25690** 2007 August 14
25691**
25692** The author disclaims copyright to this source code. In place of
25693** a legal notice, here is a blessing:
25694**
25695** May you do good and not evil.
25696** May you find forgiveness for yourself and forgive others.
25697** May you share freely, never taking more than you give.
25698**
25699*************************************************************************
25700** This file contains the C functions that implement mutexes for Win32.
25701*/
25702/* #include "sqliteInt.h" */
25703
25704#if SQLITE_OS_WIN
25705/*
25706** Include code that is common to all os_*.c files
25707*/
25708/************** Include os_common.h in the middle of mutex_w32.c *************/
25709/************** Begin file os_common.h ***************************************/
25710/*
25711** 2004 May 22
25712**
25713** The author disclaims copyright to this source code. In place of
25714** a legal notice, here is a blessing:
25715**
25716** May you do good and not evil.
25717** May you find forgiveness for yourself and forgive others.
25718** May you share freely, never taking more than you give.
25719**
25720******************************************************************************
25721**
25722** This file contains macros and a little bit of code that is common to
25723** all of the platform-specific files (os_*.c) and is #included into those
25724** files.
25725**
25726** This file should be #included by the os_*.c files only. It is not a
25727** general purpose header file.
25728*/
25729#ifndef _OS_COMMON_H_
25730#define _OS_COMMON_H_
25731
25732/*
25733** At least two bugs have slipped in because we changed the MEMORY_DEBUG
25734** macro to SQLITE_DEBUG and some older makefiles have not yet made the
25735** switch. The following code should catch this problem at compile-time.
25736*/
25737#ifdef MEMORY_DEBUG
25738# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
25739#endif
25740
25741/*
25742** Macros for performance tracing. Normally turned off. Only works
25743** on i486 hardware.
25744*/
25745#ifdef SQLITE_PERFORMANCE_TRACE
25746
25747/*
25748** hwtime.h contains inline assembler code for implementing
25749** high-performance timing routines.
25750*/
25751/************** Include hwtime.h in the middle of os_common.h ****************/
25752/************** Begin file hwtime.h ******************************************/
25753/*
25754** 2008 May 27
25755**
25756** The author disclaims copyright to this source code. In place of
25757** a legal notice, here is a blessing:
25758**
25759** May you do good and not evil.
25760** May you find forgiveness for yourself and forgive others.
25761** May you share freely, never taking more than you give.
25762**
25763******************************************************************************
25764**
25765** This file contains inline asm code for retrieving "high-performance"
25766** counters for x86 class CPUs.
25767*/
25768#ifndef SQLITE_HWTIME_H
25769#define SQLITE_HWTIME_H
25770
25771/*
25772** The following routine only works on pentium-class (or newer) processors.
25773** It uses the RDTSC opcode to read the cycle count value out of the
25774** processor and returns that value. This can be used for high-res
25775** profiling.
25776*/
25777#if (defined(__GNUC__) || defined(_MSC_VER)) && \
25778 (defined(i386) || defined(__i386__) || defined(_M_IX86))
25779
25780 #if defined(__GNUC__)
25781
25782 __inline__ sqlite_uint64 sqlite3Hwtime(void){
25783 unsigned int lo, hi;
25784 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
25785 return (sqlite_uint64)hi << 32 | lo;
25786 }
25787
25788 #elif defined(_MSC_VER)
25789
25790 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
25791 __asm {
25792 rdtsc
25793 ret ; return value at EDX:EAX
25794 }
25795 }
25796
25797 #endif
25798
25799#elif (defined(__GNUC__) && defined(__x86_64__))
25800
25801 __inline__ sqlite_uint64 sqlite3Hwtime(void){
25802 unsigned long val;
25803 __asm__ __volatile__ ("rdtsc" : "=A" (val));
25804 return val;
25805 }
25806
25807#elif (defined(__GNUC__) && defined(__ppc__))
25808
25809 __inline__ sqlite_uint64 sqlite3Hwtime(void){
25810 unsigned long long retval;
25811 unsigned long junk;
25812 __asm__ __volatile__ ("\n\
25813 1: mftbu %1\n\
25814 mftb %L0\n\
25815 mftbu %0\n\
25816 cmpw %0,%1\n\
25817 bne 1b"
25818 : "=r" (retval), "=r" (junk));
25819 return retval;
25820 }
25821
25822#else
25823
25824 #error Need implementation of sqlite3Hwtime() for your platform.
25825
25826 /*
25827 ** To compile without implementing sqlite3Hwtime() for your platform,
25828 ** you can remove the above #error and use the following
25829 ** stub function. You will lose timing support for many
25830 ** of the debugging and testing utilities, but it should at
25831 ** least compile and run.
25832 */
25833SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
25834
25835#endif
25836
25837#endif /* !defined(SQLITE_HWTIME_H) */
25838
25839/************** End of hwtime.h **********************************************/
25840/************** Continuing where we left off in os_common.h ******************/
25841
25842static sqlite_uint64 g_start;
25843static sqlite_uint64 g_elapsed;
25844#define TIMER_START g_start=sqlite3Hwtime()
25845#define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
25846#define TIMER_ELAPSED g_elapsed
25847#else
25848#define TIMER_START
25849#define TIMER_END
25850#define TIMER_ELAPSED ((sqlite_uint64)0)
25851#endif
25852
25853/*
25854** If we compile with the SQLITE_TEST macro set, then the following block
25855** of code will give us the ability to simulate a disk I/O error. This
25856** is used for testing the I/O recovery logic.
25857*/
25858#if defined(SQLITE_TEST)
25859SQLITE_API extern int sqlite3_io_error_hit;
25860SQLITE_API extern int sqlite3_io_error_hardhit;
25861SQLITE_API extern int sqlite3_io_error_pending;
25862SQLITE_API extern int sqlite3_io_error_persist;
25863SQLITE_API extern int sqlite3_io_error_benign;
25864SQLITE_API extern int sqlite3_diskfull_pending;
25865SQLITE_API extern int sqlite3_diskfull;
25866#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
25867#define SimulateIOError(CODE) \
25868 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
25869 || sqlite3_io_error_pending-- == 1 ) \
25870 { local_ioerr(); CODE; }
25871static void local_ioerr(){
25872 IOTRACE(("IOERR\n"));
25873 sqlite3_io_error_hit++;
25874 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
25875}
25876#define SimulateDiskfullError(CODE) \
25877 if( sqlite3_diskfull_pending ){ \
25878 if( sqlite3_diskfull_pending == 1 ){ \
25879 local_ioerr(); \
25880 sqlite3_diskfull = 1; \
25881 sqlite3_io_error_hit = 1; \
25882 CODE; \
25883 }else{ \
25884 sqlite3_diskfull_pending--; \
25885 } \
25886 }
25887#else
25888#define SimulateIOErrorBenign(X)
25889#define SimulateIOError(A)
25890#define SimulateDiskfullError(A)
25891#endif /* defined(SQLITE_TEST) */
25892
25893/*
25894** When testing, keep a count of the number of open files.
25895*/
25896#if defined(SQLITE_TEST)
25897SQLITE_API extern int sqlite3_open_file_count;
25898#define OpenCounter(X) sqlite3_open_file_count+=(X)
25899#else
25900#define OpenCounter(X)
25901#endif /* defined(SQLITE_TEST) */
25902
25903#endif /* !defined(_OS_COMMON_H_) */
25904
25905/************** End of os_common.h *******************************************/
25906/************** Continuing where we left off in mutex_w32.c ******************/
25907
25908/*
25909** Include the header file for the Windows VFS.
25910*/
25911/************** Include os_win.h in the middle of mutex_w32.c ****************/
25912/************** Begin file os_win.h ******************************************/
25913/*
25914** 2013 November 25
25915**
25916** The author disclaims copyright to this source code. In place of
25917** a legal notice, here is a blessing:
25918**
25919** May you do good and not evil.
25920** May you find forgiveness for yourself and forgive others.
25921** May you share freely, never taking more than you give.
25922**
25923******************************************************************************
25924**
25925** This file contains code that is specific to Windows.
25926*/
25927#ifndef SQLITE_OS_WIN_H
25928#define SQLITE_OS_WIN_H
25929
25930/*
25931** Include the primary Windows SDK header file.
25932*/
25933#include "windows.h"
25934
25935#ifdef __CYGWIN__
25936# include <sys/cygwin.h>
25937# include <errno.h> /* amalgamator: dontcache */
25938#endif
25939
25940/*
25941** Determine if we are dealing with Windows NT.
25942**
25943** We ought to be able to determine if we are compiling for Windows 9x or
25944** Windows NT using the _WIN32_WINNT macro as follows:
25945**
25946** #if defined(_WIN32_WINNT)
25947** # define SQLITE_OS_WINNT 1
25948** #else
25949** # define SQLITE_OS_WINNT 0
25950** #endif
25951**
25952** However, Visual Studio 2005 does not set _WIN32_WINNT by default, as
25953** it ought to, so the above test does not work. We'll just assume that
25954** everything is Windows NT unless the programmer explicitly says otherwise
25955** by setting SQLITE_OS_WINNT to 0.
25956*/
25957#if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT)
25958# define SQLITE_OS_WINNT 1
25959#endif
25960
25961/*
25962** Determine if we are dealing with Windows CE - which has a much reduced
25963** API.
25964*/
25965#if defined(_WIN32_WCE)
25966# define SQLITE_OS_WINCE 1
25967#else
25968# define SQLITE_OS_WINCE 0
25969#endif
25970
25971/*
25972** Determine if we are dealing with WinRT, which provides only a subset of
25973** the full Win32 API.
25974*/
25975#if !defined(SQLITE_OS_WINRT)
25976# define SQLITE_OS_WINRT 0
25977#endif
25978
25979/*
25980** For WinCE, some API function parameters do not appear to be declared as
25981** volatile.
25982*/
25983#if SQLITE_OS_WINCE
25984# define SQLITE_WIN32_VOLATILE
25985#else
25986# define SQLITE_WIN32_VOLATILE volatile
25987#endif
25988
25989/*
25990** For some Windows sub-platforms, the _beginthreadex() / _endthreadex()
25991** functions are not available (e.g. those not using MSVC, Cygwin, etc).
25992*/
25993#if SQLITE_OS_WIN && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
25994 SQLITE_THREADSAFE>0 && !defined(__CYGWIN__)
25995# define SQLITE_OS_WIN_THREADS 1
25996#else
25997# define SQLITE_OS_WIN_THREADS 0
25998#endif
25999
26000#endif /* SQLITE_OS_WIN_H */
26001
26002/************** End of os_win.h **********************************************/
26003/************** Continuing where we left off in mutex_w32.c ******************/
26004#endif
26005
26006/*
26007** The code in this file is only used if we are compiling multithreaded
26008** on a Win32 system.
26009*/
26010#ifdef SQLITE_MUTEX_W32
26011
26012/*
26013** Each recursive mutex is an instance of the following structure.
26014*/
26015struct sqlite3_mutex {
26016 CRITICAL_SECTION mutex; /* Mutex controlling the lock */
26017 int id; /* Mutex type */
26018#ifdef SQLITE_DEBUG
26019 volatile int nRef; /* Number of enterances */
26020 volatile DWORD owner; /* Thread holding this mutex */
26021 volatile LONG trace; /* True to trace changes */
26022#endif
26023};
26024
26025/*
26026** These are the initializer values used when declaring a "static" mutex
26027** on Win32. It should be noted that all mutexes require initialization
26028** on the Win32 platform.
26029*/
26030#define SQLITE_W32_MUTEX_INITIALIZER { 0 }
26031
26032#ifdef SQLITE_DEBUG
26033#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id, \
26034 0L, (DWORD)0, 0 }
26035#else
26036#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id }
26037#endif
26038
26039#ifdef SQLITE_DEBUG
26040/*
26041** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
26042** intended for use only inside assert() statements.
26043*/
26044static int winMutexHeld(sqlite3_mutex *p){
26045 return p->nRef!=0 && p->owner==GetCurrentThreadId();
26046}
26047
26048static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
26049 return p->nRef==0 || p->owner!=tid;
26050}
26051
26052static int winMutexNotheld(sqlite3_mutex *p){
26053 DWORD tid = GetCurrentThreadId();
26054 return winMutexNotheld2(p, tid);
26055}
26056#endif
26057
26058/*
26059** Try to provide a memory barrier operation, needed for initialization
26060** and also for the xShmBarrier method of the VFS in cases when SQLite is
26061** compiled without mutexes (SQLITE_THREADSAFE=0).
26062*/
26063SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
26064#if defined(SQLITE_MEMORY_BARRIER)
26065 SQLITE_MEMORY_BARRIER;
26066#elif defined(__GNUC__)
26067 __sync_synchronize();
26068#elif MSVC_VERSION>=1300
26069 _ReadWriteBarrier();
26070#elif defined(MemoryBarrier)
26071 MemoryBarrier();
26072#endif
26073}
26074
26075/*
26076** Initialize and deinitialize the mutex subsystem.
26077*/
26078static sqlite3_mutex winMutex_staticMutexes[] = {
26079 SQLITE3_MUTEX_INITIALIZER(2),
26080 SQLITE3_MUTEX_INITIALIZER(3),
26081 SQLITE3_MUTEX_INITIALIZER(4),
26082 SQLITE3_MUTEX_INITIALIZER(5),
26083 SQLITE3_MUTEX_INITIALIZER(6),
26084 SQLITE3_MUTEX_INITIALIZER(7),
26085 SQLITE3_MUTEX_INITIALIZER(8),
26086 SQLITE3_MUTEX_INITIALIZER(9),
26087 SQLITE3_MUTEX_INITIALIZER(10),
26088 SQLITE3_MUTEX_INITIALIZER(11),
26089 SQLITE3_MUTEX_INITIALIZER(12),
26090 SQLITE3_MUTEX_INITIALIZER(13)
26091};
26092
26093static int winMutex_isInit = 0;
26094static int winMutex_isNt = -1; /* <0 means "need to query" */
26095
26096/* As the winMutexInit() and winMutexEnd() functions are called as part
26097** of the sqlite3_initialize() and sqlite3_shutdown() processing, the
26098** "interlocked" magic used here is probably not strictly necessary.
26099*/
26100static LONG SQLITE_WIN32_VOLATILE winMutex_lock = 0;
26101
26102SQLITE_API int sqlite3_win32_is_nt(void); /* os_win.c */
26103SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
26104
26105static int winMutexInit(void){
26106 /* The first to increment to 1 does actual initialization */
26107 if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
26108 int i;
26109 for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
26110#if SQLITE_OS_WINRT
26111 InitializeCriticalSectionEx(&winMutex_staticMutexes[i].mutex, 0, 0);
26112#else
26113 InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
26114#endif
26115 }
26116 winMutex_isInit = 1;
26117 }else{
26118 /* Another thread is (in the process of) initializing the static
26119 ** mutexes */
26120 while( !winMutex_isInit ){
26121 sqlite3_win32_sleep(1);
26122 }
26123 }
26124 return SQLITE_OK;
26125}
26126
26127static int winMutexEnd(void){
26128 /* The first to decrement to 0 does actual shutdown
26129 ** (which should be the last to shutdown.) */
26130 if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
26131 if( winMutex_isInit==1 ){
26132 int i;
26133 for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
26134 DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
26135 }
26136 winMutex_isInit = 0;
26137 }
26138 }
26139 return SQLITE_OK;
26140}
26141
26142/*
26143** The sqlite3_mutex_alloc() routine allocates a new
26144** mutex and returns a pointer to it. If it returns NULL
26145** that means that a mutex could not be allocated. SQLite
26146** will unwind its stack and return an error. The argument
26147** to sqlite3_mutex_alloc() is one of these integer constants:
26148**
26149** <ul>
26150** <li> SQLITE_MUTEX_FAST
26151** <li> SQLITE_MUTEX_RECURSIVE
26152** <li> SQLITE_MUTEX_STATIC_MASTER
26153** <li> SQLITE_MUTEX_STATIC_MEM
26154** <li> SQLITE_MUTEX_STATIC_OPEN
26155** <li> SQLITE_MUTEX_STATIC_PRNG
26156** <li> SQLITE_MUTEX_STATIC_LRU
26157** <li> SQLITE_MUTEX_STATIC_PMEM
26158** <li> SQLITE_MUTEX_STATIC_APP1
26159** <li> SQLITE_MUTEX_STATIC_APP2
26160** <li> SQLITE_MUTEX_STATIC_APP3
26161** <li> SQLITE_MUTEX_STATIC_VFS1
26162** <li> SQLITE_MUTEX_STATIC_VFS2
26163** <li> SQLITE_MUTEX_STATIC_VFS3
26164** </ul>
26165**
26166** The first two constants cause sqlite3_mutex_alloc() to create
26167** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
26168** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
26169** The mutex implementation does not need to make a distinction
26170** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
26171** not want to. But SQLite will only request a recursive mutex in
26172** cases where it really needs one. If a faster non-recursive mutex
26173** implementation is available on the host platform, the mutex subsystem
26174** might return such a mutex in response to SQLITE_MUTEX_FAST.
26175**
26176** The other allowed parameters to sqlite3_mutex_alloc() each return
26177** a pointer to a static preexisting mutex. Six static mutexes are
26178** used by the current version of SQLite. Future versions of SQLite
26179** may add additional static mutexes. Static mutexes are for internal
26180** use by SQLite only. Applications that use SQLite mutexes should
26181** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
26182** SQLITE_MUTEX_RECURSIVE.
26183**
26184** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
26185** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
26186** returns a different mutex on every call. But for the static
26187** mutex types, the same mutex is returned on every call that has
26188** the same type number.
26189*/
26190static sqlite3_mutex *winMutexAlloc(int iType){
26191 sqlite3_mutex *p;
26192
26193 switch( iType ){
26194 case SQLITE_MUTEX_FAST:
26195 case SQLITE_MUTEX_RECURSIVE: {
26196 p = sqlite3MallocZero( sizeof(*p) );
26197 if( p ){
26198 p->id = iType;
26199#ifdef SQLITE_DEBUG
26200#ifdef SQLITE_WIN32_MUTEX_TRACE_DYNAMIC
26201 p->trace = 1;
26202#endif
26203#endif
26204#if SQLITE_OS_WINRT
26205 InitializeCriticalSectionEx(&p->mutex, 0, 0);
26206#else
26207 InitializeCriticalSection(&p->mutex);
26208#endif
26209 }
26210 break;
26211 }
26212 default: {
26213#ifdef SQLITE_ENABLE_API_ARMOR
26214 if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){
26215 (void)SQLITE_MISUSE_BKPT;
26216 return 0;
26217 }
26218#endif
26219 p = &winMutex_staticMutexes[iType-2];
26220#ifdef SQLITE_DEBUG
26221#ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
26222 InterlockedCompareExchange(&p->trace, 1, 0);
26223#endif
26224#endif
26225 break;
26226 }
26227 }
26228 assert( p==0 || p->id==iType );
26229 return p;
26230}
26231
26232
26233/*
26234** This routine deallocates a previously
26235** allocated mutex. SQLite is careful to deallocate every
26236** mutex that it allocates.
26237*/
26238static void winMutexFree(sqlite3_mutex *p){
26239 assert( p );
26240 assert( p->nRef==0 && p->owner==0 );
26241 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ){
26242 DeleteCriticalSection(&p->mutex);
26243 sqlite3_free(p);
26244 }else{
26245#ifdef SQLITE_ENABLE_API_ARMOR
26246 (void)SQLITE_MISUSE_BKPT;
26247#endif
26248 }
26249}
26250
26251/*
26252** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
26253** to enter a mutex. If another thread is already within the mutex,
26254** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
26255** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
26256** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
26257** be entered multiple times by the same thread. In such cases the,
26258** mutex must be exited an equal number of times before another thread
26259** can enter. If the same thread tries to enter any other kind of mutex
26260** more than once, the behavior is undefined.
26261*/
26262static void winMutexEnter(sqlite3_mutex *p){
26263#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
26264 DWORD tid = GetCurrentThreadId();
26265#endif
26266#ifdef SQLITE_DEBUG
26267 assert( p );
26268 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
26269#else
26270 assert( p );
26271#endif
26272 assert( winMutex_isInit==1 );
26273 EnterCriticalSection(&p->mutex);
26274#ifdef SQLITE_DEBUG
26275 assert( p->nRef>0 || p->owner==0 );
26276 p->owner = tid;
26277 p->nRef++;
26278 if( p->trace ){
26279 OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
26280 tid, p->id, p, p->trace, p->nRef));
26281 }
26282#endif
26283}
26284
26285static int winMutexTry(sqlite3_mutex *p){
26286#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
26287 DWORD tid = GetCurrentThreadId();
26288#endif
26289 int rc = SQLITE_BUSY;
26290 assert( p );
26291 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
26292 /*
26293 ** The sqlite3_mutex_try() routine is very rarely used, and when it
26294 ** is used it is merely an optimization. So it is OK for it to always
26295 ** fail.
26296 **
26297 ** The TryEnterCriticalSection() interface is only available on WinNT.
26298 ** And some windows compilers complain if you try to use it without
26299 ** first doing some #defines that prevent SQLite from building on Win98.
26300 ** For that reason, we will omit this optimization for now. See
26301 ** ticket #2685.
26302 */
26303#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400
26304 assert( winMutex_isInit==1 );
26305 assert( winMutex_isNt>=-1 && winMutex_isNt<=1 );
26306 if( winMutex_isNt<0 ){
26307 winMutex_isNt = sqlite3_win32_is_nt();
26308 }
26309 assert( winMutex_isNt==0 || winMutex_isNt==1 );
26310 if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){
26311#ifdef SQLITE_DEBUG
26312 p->owner = tid;
26313 p->nRef++;
26314#endif
26315 rc = SQLITE_OK;
26316 }
26317#else
26318 UNUSED_PARAMETER(p);
26319#endif
26320#ifdef SQLITE_DEBUG
26321 if( p->trace ){
26322 OSTRACE(("TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
26323 tid, p->id, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
26324 }
26325#endif
26326 return rc;
26327}
26328
26329/*
26330** The sqlite3_mutex_leave() routine exits a mutex that was
26331** previously entered by the same thread. The behavior
26332** is undefined if the mutex is not currently entered or
26333** is not currently allocated. SQLite will never do either.
26334*/
26335static void winMutexLeave(sqlite3_mutex *p){
26336#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
26337 DWORD tid = GetCurrentThreadId();
26338#endif
26339 assert( p );
26340#ifdef SQLITE_DEBUG
26341 assert( p->nRef>0 );
26342 assert( p->owner==tid );
26343 p->nRef--;
26344 if( p->nRef==0 ) p->owner = 0;
26345 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
26346#endif
26347 assert( winMutex_isInit==1 );
26348 LeaveCriticalSection(&p->mutex);
26349#ifdef SQLITE_DEBUG
26350 if( p->trace ){
26351 OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
26352 tid, p->id, p, p->trace, p->nRef));
26353 }
26354#endif
26355}
26356
26357SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
26358 static const sqlite3_mutex_methods sMutex = {
26359 winMutexInit,
26360 winMutexEnd,
26361 winMutexAlloc,
26362 winMutexFree,
26363 winMutexEnter,
26364 winMutexTry,
26365 winMutexLeave,
26366#ifdef SQLITE_DEBUG
26367 winMutexHeld,
26368 winMutexNotheld
26369#else
26370 0,
26371 0
26372#endif
26373 };
26374 return &sMutex;
26375}
26376
26377#endif /* SQLITE_MUTEX_W32 */
26378
26379/************** End of mutex_w32.c *******************************************/
26380/************** Begin file malloc.c ******************************************/
26381/*
26382** 2001 September 15
26383**
26384** The author disclaims copyright to this source code. In place of
26385** a legal notice, here is a blessing:
26386**
26387** May you do good and not evil.
26388** May you find forgiveness for yourself and forgive others.
26389** May you share freely, never taking more than you give.
26390**
26391*************************************************************************
26392**
26393** Memory allocation functions used throughout sqlite.
26394*/
26395/* #include "sqliteInt.h" */
26396/* #include <stdarg.h> */
26397
26398/*
26399** Attempt to release up to n bytes of non-essential memory currently
26400** held by SQLite. An example of non-essential memory is memory used to
26401** cache database pages that are not currently in use.
26402*/
26403SQLITE_API int sqlite3_release_memory(int n){
26404#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
26405 return sqlite3PcacheReleaseMemory(n);
26406#else
26407 /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
26408 ** is a no-op returning zero if SQLite is not compiled with
26409 ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
26410 UNUSED_PARAMETER(n);
26411 return 0;
26412#endif
26413}
26414
26415/*
26416** State information local to the memory allocation subsystem.
26417*/
26418static SQLITE_WSD struct Mem0Global {
26419 sqlite3_mutex *mutex; /* Mutex to serialize access */
26420 sqlite3_int64 alarmThreshold; /* The soft heap limit */
26421
26422 /*
26423 ** True if heap is nearly "full" where "full" is defined by the
26424 ** sqlite3_soft_heap_limit() setting.
26425 */
26426 int nearlyFull;
26427} mem0 = { 0, 0, 0 };
26428
26429#define mem0 GLOBAL(struct Mem0Global, mem0)
26430
26431/*
26432** Return the memory allocator mutex. sqlite3_status() needs it.
26433*/
26434SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void){
26435 return mem0.mutex;
26436}
26437
26438#ifndef SQLITE_OMIT_DEPRECATED
26439/*
26440** Deprecated external interface. It used to set an alarm callback
26441** that was invoked when memory usage grew too large. Now it is a
26442** no-op.
26443*/
26444SQLITE_API int sqlite3_memory_alarm(
26445 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
26446 void *pArg,
26447 sqlite3_int64 iThreshold
26448){
26449 (void)xCallback;
26450 (void)pArg;
26451 (void)iThreshold;
26452 return SQLITE_OK;
26453}
26454#endif
26455
26456/*
26457** Set the soft heap-size limit for the library. Passing a zero or
26458** negative value indicates no limit.
26459*/
26460SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
26461 sqlite3_int64 priorLimit;
26462 sqlite3_int64 excess;
26463 sqlite3_int64 nUsed;
26464#ifndef SQLITE_OMIT_AUTOINIT
26465 int rc = sqlite3_initialize();
26466 if( rc ) return -1;
26467#endif
26468 sqlite3_mutex_enter(mem0.mutex);
26469 priorLimit = mem0.alarmThreshold;
26470 if( n<0 ){
26471 sqlite3_mutex_leave(mem0.mutex);
26472 return priorLimit;
26473 }
26474 mem0.alarmThreshold = n;
26475 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
26476 mem0.nearlyFull = (n>0 && n<=nUsed);
26477 sqlite3_mutex_leave(mem0.mutex);
26478 excess = sqlite3_memory_used() - n;
26479 if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
26480 return priorLimit;
26481}
26482SQLITE_API void sqlite3_soft_heap_limit(int n){
26483 if( n<0 ) n = 0;
26484 sqlite3_soft_heap_limit64(n);
26485}
26486
26487/*
26488** Initialize the memory allocation subsystem.
26489*/
26490SQLITE_PRIVATE int sqlite3MallocInit(void){
26491 int rc;
26492 if( sqlite3GlobalConfig.m.xMalloc==0 ){
26493 sqlite3MemSetDefault();
26494 }
26495 memset(&mem0, 0, sizeof(mem0));
26496 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
26497 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
26498 || sqlite3GlobalConfig.nPage<=0 ){
26499 sqlite3GlobalConfig.pPage = 0;
26500 sqlite3GlobalConfig.szPage = 0;
26501 }
26502 rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
26503 if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
26504 return rc;
26505}
26506
26507/*
26508** Return true if the heap is currently under memory pressure - in other
26509** words if the amount of heap used is close to the limit set by
26510** sqlite3_soft_heap_limit().
26511*/
26512SQLITE_PRIVATE int sqlite3HeapNearlyFull(void){
26513 return mem0.nearlyFull;
26514}
26515
26516/*
26517** Deinitialize the memory allocation subsystem.
26518*/
26519SQLITE_PRIVATE void sqlite3MallocEnd(void){
26520 if( sqlite3GlobalConfig.m.xShutdown ){
26521 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
26522 }
26523 memset(&mem0, 0, sizeof(mem0));
26524}
26525
26526/*
26527** Return the amount of memory currently checked out.
26528*/
26529SQLITE_API sqlite3_int64 sqlite3_memory_used(void){
26530 sqlite3_int64 res, mx;
26531 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
26532 return res;
26533}
26534
26535/*
26536** Return the maximum amount of memory that has ever been
26537** checked out since either the beginning of this process
26538** or since the most recent reset.
26539*/
26540SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
26541 sqlite3_int64 res, mx;
26542 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
26543 return mx;
26544}
26545
26546/*
26547** Trigger the alarm
26548*/
26549static void sqlite3MallocAlarm(int nByte){
26550 if( mem0.alarmThreshold<=0 ) return;
26551 sqlite3_mutex_leave(mem0.mutex);
26552 sqlite3_release_memory(nByte);
26553 sqlite3_mutex_enter(mem0.mutex);
26554}
26555
26556/*
26557** Do a memory allocation with statistics and alarms. Assume the
26558** lock is already held.
26559*/
26560static void mallocWithAlarm(int n, void **pp){
26561 void *p;
26562 int nFull;
26563 assert( sqlite3_mutex_held(mem0.mutex) );
26564 assert( n>0 );
26565
26566 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
26567 ** implementation of malloc_good_size(), which must be called in debug
26568 ** mode and specifically when the DMD "Dark Matter Detector" is enabled
26569 ** or else a crash results. Hence, do not attempt to optimize out the
26570 ** following xRoundup() call. */
26571 nFull = sqlite3GlobalConfig.m.xRoundup(n);
26572
26573#ifdef SQLITE_MAX_MEMORY
26574 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nFull>SQLITE_MAX_MEMORY ){
26575 *pp = 0;
26576 return;
26577 }
26578#endif
26579
26580 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
26581 if( mem0.alarmThreshold>0 ){
26582 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
26583 if( nUsed >= mem0.alarmThreshold - nFull ){
26584 mem0.nearlyFull = 1;
26585 sqlite3MallocAlarm(nFull);
26586 }else{
26587 mem0.nearlyFull = 0;
26588 }
26589 }
26590 p = sqlite3GlobalConfig.m.xMalloc(nFull);
26591#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
26592 if( p==0 && mem0.alarmThreshold>0 ){
26593 sqlite3MallocAlarm(nFull);
26594 p = sqlite3GlobalConfig.m.xMalloc(nFull);
26595 }
26596#endif
26597 if( p ){
26598 nFull = sqlite3MallocSize(p);
26599 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
26600 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
26601 }
26602 *pp = p;
26603}
26604
26605/*
26606** Allocate memory. This routine is like sqlite3_malloc() except that it
26607** assumes the memory subsystem has already been initialized.
26608*/
26609SQLITE_PRIVATE void *sqlite3Malloc(u64 n){
26610 void *p;
26611 if( n==0 || n>=0x7fffff00 ){
26612 /* A memory allocation of a number of bytes which is near the maximum
26613 ** signed integer value might cause an integer overflow inside of the
26614 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
26615 ** 255 bytes of overhead. SQLite itself will never use anything near
26616 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
26617 p = 0;
26618 }else if( sqlite3GlobalConfig.bMemstat ){
26619 sqlite3_mutex_enter(mem0.mutex);
26620 mallocWithAlarm((int)n, &p);
26621 sqlite3_mutex_leave(mem0.mutex);
26622 }else{
26623 p = sqlite3GlobalConfig.m.xMalloc((int)n);
26624 }
26625 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
26626 return p;
26627}
26628
26629/*
26630** This version of the memory allocation is for use by the application.
26631** First make sure the memory subsystem is initialized, then do the
26632** allocation.
26633*/
26634SQLITE_API void *sqlite3_malloc(int n){
26635#ifndef SQLITE_OMIT_AUTOINIT
26636 if( sqlite3_initialize() ) return 0;
26637#endif
26638 return n<=0 ? 0 : sqlite3Malloc(n);
26639}
26640SQLITE_API void *sqlite3_malloc64(sqlite3_uint64 n){
26641#ifndef SQLITE_OMIT_AUTOINIT
26642 if( sqlite3_initialize() ) return 0;
26643#endif
26644 return sqlite3Malloc(n);
26645}
26646
26647/*
26648** TRUE if p is a lookaside memory allocation from db
26649*/
26650#ifndef SQLITE_OMIT_LOOKASIDE
26651static int isLookaside(sqlite3 *db, void *p){
26652 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
26653}
26654#else
26655#define isLookaside(A,B) 0
26656#endif
26657
26658/*
26659** Return the size of a memory allocation previously obtained from
26660** sqlite3Malloc() or sqlite3_malloc().
26661*/
26662SQLITE_PRIVATE int sqlite3MallocSize(void *p){
26663 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
26664 return sqlite3GlobalConfig.m.xSize(p);
26665}
26666SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){
26667 assert( p!=0 );
26668 if( db==0 || !isLookaside(db,p) ){
26669#ifdef SQLITE_DEBUG
26670 if( db==0 ){
26671 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
26672 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
26673 }else{
26674 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
26675 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
26676 }
26677#endif
26678 return sqlite3GlobalConfig.m.xSize(p);
26679 }else{
26680 assert( sqlite3_mutex_held(db->mutex) );
26681 return db->lookaside.sz;
26682 }
26683}
26684SQLITE_API sqlite3_uint64 sqlite3_msize(void *p){
26685 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
26686 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
26687 return p ? sqlite3GlobalConfig.m.xSize(p) : 0;
26688}
26689
26690/*
26691** Free memory previously obtained from sqlite3Malloc().
26692*/
26693SQLITE_API void sqlite3_free(void *p){
26694 if( p==0 ) return; /* IMP: R-49053-54554 */
26695 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
26696 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
26697 if( sqlite3GlobalConfig.bMemstat ){
26698 sqlite3_mutex_enter(mem0.mutex);
26699 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
26700 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
26701 sqlite3GlobalConfig.m.xFree(p);
26702 sqlite3_mutex_leave(mem0.mutex);
26703 }else{
26704 sqlite3GlobalConfig.m.xFree(p);
26705 }
26706}
26707
26708/*
26709** Add the size of memory allocation "p" to the count in
26710** *db->pnBytesFreed.
26711*/
26712static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
26713 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
26714}
26715
26716/*
26717** Free memory that might be associated with a particular database
26718** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
26719** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
26720*/
26721SQLITE_PRIVATE void sqlite3DbFreeNN(sqlite3 *db, void *p){
26722 assert( db==0 || sqlite3_mutex_held(db->mutex) );
26723 assert( p!=0 );
26724 if( db ){
26725 if( db->pnBytesFreed ){
26726 measureAllocationSize(db, p);
26727 return;
26728 }
26729 if( isLookaside(db, p) ){
26730 LookasideSlot *pBuf = (LookasideSlot*)p;
26731#ifdef SQLITE_DEBUG
26732 /* Trash all content in the buffer being freed */
26733 memset(p, 0xaa, db->lookaside.sz);
26734#endif
26735 pBuf->pNext = db->lookaside.pFree;
26736 db->lookaside.pFree = pBuf;
26737 return;
26738 }
26739 }
26740 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
26741 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
26742 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
26743 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
26744 sqlite3_free(p);
26745}
26746SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
26747 assert( db==0 || sqlite3_mutex_held(db->mutex) );
26748 if( p ) sqlite3DbFreeNN(db, p);
26749}
26750
26751/*
26752** Change the size of an existing memory allocation
26753*/
26754SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, u64 nBytes){
26755 int nOld, nNew, nDiff;
26756 void *pNew;
26757 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
26758 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
26759 if( pOld==0 ){
26760 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
26761 }
26762 if( nBytes==0 ){
26763 sqlite3_free(pOld); /* IMP: R-26507-47431 */
26764 return 0;
26765 }
26766 if( nBytes>=0x7fffff00 ){
26767 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
26768 return 0;
26769 }
26770 nOld = sqlite3MallocSize(pOld);
26771 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
26772 ** argument to xRealloc is always a value returned by a prior call to
26773 ** xRoundup. */
26774 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
26775 if( nOld==nNew ){
26776 pNew = pOld;
26777 }else if( sqlite3GlobalConfig.bMemstat ){
26778 sqlite3_mutex_enter(mem0.mutex);
26779 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
26780 nDiff = nNew - nOld;
26781 if( nDiff>0 && sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
26782 mem0.alarmThreshold-nDiff ){
26783 sqlite3MallocAlarm(nDiff);
26784 }
26785 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
26786 if( pNew==0 && mem0.alarmThreshold>0 ){
26787 sqlite3MallocAlarm((int)nBytes);
26788 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
26789 }
26790 if( pNew ){
26791 nNew = sqlite3MallocSize(pNew);
26792 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
26793 }
26794 sqlite3_mutex_leave(mem0.mutex);
26795 }else{
26796 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
26797 }
26798 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
26799 return pNew;
26800}
26801
26802/*
26803** The public interface to sqlite3Realloc. Make sure that the memory
26804** subsystem is initialized prior to invoking sqliteRealloc.
26805*/
26806SQLITE_API void *sqlite3_realloc(void *pOld, int n){
26807#ifndef SQLITE_OMIT_AUTOINIT
26808 if( sqlite3_initialize() ) return 0;
26809#endif
26810 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
26811 return sqlite3Realloc(pOld, n);
26812}
26813SQLITE_API void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
26814#ifndef SQLITE_OMIT_AUTOINIT
26815 if( sqlite3_initialize() ) return 0;
26816#endif
26817 return sqlite3Realloc(pOld, n);
26818}
26819
26820
26821/*
26822** Allocate and zero memory.
26823*/
26824SQLITE_PRIVATE void *sqlite3MallocZero(u64 n){
26825 void *p = sqlite3Malloc(n);
26826 if( p ){
26827 memset(p, 0, (size_t)n);
26828 }
26829 return p;
26830}
26831
26832/*
26833** Allocate and zero memory. If the allocation fails, make
26834** the mallocFailed flag in the connection pointer.
26835*/
26836SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
26837 void *p;
26838 testcase( db==0 );
26839 p = sqlite3DbMallocRaw(db, n);
26840 if( p ) memset(p, 0, (size_t)n);
26841 return p;
26842}
26843
26844
26845/* Finish the work of sqlite3DbMallocRawNN for the unusual and
26846** slower case when the allocation cannot be fulfilled using lookaside.
26847*/
26848static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
26849 void *p;
26850 assert( db!=0 );
26851 p = sqlite3Malloc(n);
26852 if( !p ) sqlite3OomFault(db);
26853 sqlite3MemdebugSetType(p,
26854 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
26855 return p;
26856}
26857
26858/*
26859** Allocate memory, either lookaside (if possible) or heap.
26860** If the allocation fails, set the mallocFailed flag in
26861** the connection pointer.
26862**
26863** If db!=0 and db->mallocFailed is true (indicating a prior malloc
26864** failure on the same database connection) then always return 0.
26865** Hence for a particular database connection, once malloc starts
26866** failing, it fails consistently until mallocFailed is reset.
26867** This is an important assumption. There are many places in the
26868** code that do things like this:
26869**
26870** int *a = (int*)sqlite3DbMallocRaw(db, 100);
26871** int *b = (int*)sqlite3DbMallocRaw(db, 200);
26872** if( b ) a[10] = 9;
26873**
26874** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
26875** that all prior mallocs (ex: "a") worked too.
26876**
26877** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
26878** not a NULL pointer.
26879*/
26880SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
26881 void *p;
26882 if( db ) return sqlite3DbMallocRawNN(db, n);
26883 p = sqlite3Malloc(n);
26884 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
26885 return p;
26886}
26887SQLITE_PRIVATE void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
26888#ifndef SQLITE_OMIT_LOOKASIDE
26889 LookasideSlot *pBuf;
26890 assert( db!=0 );
26891 assert( sqlite3_mutex_held(db->mutex) );
26892 assert( db->pnBytesFreed==0 );
26893 if( db->lookaside.bDisable==0 ){
26894 assert( db->mallocFailed==0 );
26895 if( n>db->lookaside.sz ){
26896 db->lookaside.anStat[1]++;
26897 }else if( (pBuf = db->lookaside.pFree)!=0 ){
26898 db->lookaside.pFree = pBuf->pNext;
26899 db->lookaside.anStat[0]++;
26900 return (void*)pBuf;
26901 }else if( (pBuf = db->lookaside.pInit)!=0 ){
26902 db->lookaside.pInit = pBuf->pNext;
26903 db->lookaside.anStat[0]++;
26904 return (void*)pBuf;
26905 }else{
26906 db->lookaside.anStat[2]++;
26907 }
26908 }else if( db->mallocFailed ){
26909 return 0;
26910 }
26911#else
26912 assert( db!=0 );
26913 assert( sqlite3_mutex_held(db->mutex) );
26914 assert( db->pnBytesFreed==0 );
26915 if( db->mallocFailed ){
26916 return 0;
26917 }
26918#endif
26919 return dbMallocRawFinish(db, n);
26920}
26921
26922/* Forward declaration */
26923static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
26924
26925/*
26926** Resize the block of memory pointed to by p to n bytes. If the
26927** resize fails, set the mallocFailed flag in the connection object.
26928*/
26929SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
26930 assert( db!=0 );
26931 if( p==0 ) return sqlite3DbMallocRawNN(db, n);
26932 assert( sqlite3_mutex_held(db->mutex) );
26933 if( isLookaside(db,p) && n<=db->lookaside.sz ) return p;
26934 return dbReallocFinish(db, p, n);
26935}
26936static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
26937 void *pNew = 0;
26938 assert( db!=0 );
26939 assert( p!=0 );
26940 if( db->mallocFailed==0 ){
26941 if( isLookaside(db, p) ){
26942 pNew = sqlite3DbMallocRawNN(db, n);
26943 if( pNew ){
26944 memcpy(pNew, p, db->lookaside.sz);
26945 sqlite3DbFree(db, p);
26946 }
26947 }else{
26948 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
26949 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
26950 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
26951 pNew = sqlite3_realloc64(p, n);
26952 if( !pNew ){
26953 sqlite3OomFault(db);
26954 }
26955 sqlite3MemdebugSetType(pNew,
26956 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
26957 }
26958 }
26959 return pNew;
26960}
26961
26962/*
26963** Attempt to reallocate p. If the reallocation fails, then free p
26964** and set the mallocFailed flag in the database connection.
26965*/
26966SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
26967 void *pNew;
26968 pNew = sqlite3DbRealloc(db, p, n);
26969 if( !pNew ){
26970 sqlite3DbFree(db, p);
26971 }
26972 return pNew;
26973}
26974
26975/*
26976** Make a copy of a string in memory obtained from sqliteMalloc(). These
26977** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
26978** is because when memory debugging is turned on, these two functions are
26979** called via macros that record the current file and line number in the
26980** ThreadData structure.
26981*/
26982SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){
26983 char *zNew;
26984 size_t n;
26985 if( z==0 ){
26986 return 0;
26987 }
26988 n = strlen(z) + 1;
26989 zNew = sqlite3DbMallocRaw(db, n);
26990 if( zNew ){
26991 memcpy(zNew, z, n);
26992 }
26993 return zNew;
26994}
26995SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
26996 char *zNew;
26997 assert( db!=0 );
26998 if( z==0 ){
26999 return 0;
27000 }
27001 assert( (n&0x7fffffff)==n );
27002 zNew = sqlite3DbMallocRawNN(db, n+1);
27003 if( zNew ){
27004 memcpy(zNew, z, (size_t)n);
27005 zNew[n] = 0;
27006 }
27007 return zNew;
27008}
27009
27010/*
27011** The text between zStart and zEnd represents a phrase within a larger
27012** SQL statement. Make a copy of this phrase in space obtained form
27013** sqlite3DbMalloc(). Omit leading and trailing whitespace.
27014*/
27015SQLITE_PRIVATE char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
27016 int n;
27017 while( sqlite3Isspace(zStart[0]) ) zStart++;
27018 n = (int)(zEnd - zStart);
27019 while( ALWAYS(n>0) && sqlite3Isspace(zStart[n-1]) ) n--;
27020 return sqlite3DbStrNDup(db, zStart, n);
27021}
27022
27023/*
27024** Free any prior content in *pz and replace it with a copy of zNew.
27025*/
27026SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
27027 sqlite3DbFree(db, *pz);
27028 *pz = sqlite3DbStrDup(db, zNew);
27029}
27030
27031/*
27032** Call this routine to record the fact that an OOM (out-of-memory) error
27033** has happened. This routine will set db->mallocFailed, and also
27034** temporarily disable the lookaside memory allocator and interrupt
27035** any running VDBEs.
27036*/
27037SQLITE_PRIVATE void sqlite3OomFault(sqlite3 *db){
27038 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
27039 db->mallocFailed = 1;
27040 if( db->nVdbeExec>0 ){
27041 db->u1.isInterrupted = 1;
27042 }
27043 db->lookaside.bDisable++;
27044 }
27045}
27046
27047/*
27048** This routine reactivates the memory allocator and clears the
27049** db->mallocFailed flag as necessary.
27050**
27051** The memory allocator is not restarted if there are running
27052** VDBEs.
27053*/
27054SQLITE_PRIVATE void sqlite3OomClear(sqlite3 *db){
27055 if( db->mallocFailed && db->nVdbeExec==0 ){
27056 db->mallocFailed = 0;
27057 db->u1.isInterrupted = 0;
27058 assert( db->lookaside.bDisable>0 );
27059 db->lookaside.bDisable--;
27060 }
27061}
27062
27063/*
27064** Take actions at the end of an API call to indicate an OOM error
27065*/
27066static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
27067 sqlite3OomClear(db);
27068 sqlite3Error(db, SQLITE_NOMEM);
27069 return SQLITE_NOMEM_BKPT;
27070}
27071
27072/*
27073** This function must be called before exiting any API function (i.e.
27074** returning control to the user) that has called sqlite3_malloc or
27075** sqlite3_realloc.
27076**
27077** The returned value is normally a copy of the second argument to this
27078** function. However, if a malloc() failure has occurred since the previous
27079** invocation SQLITE_NOMEM is returned instead.
27080**
27081** If an OOM as occurred, then the connection error-code (the value
27082** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
27083*/
27084SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
27085 /* If the db handle must hold the connection handle mutex here.
27086 ** Otherwise the read (and possible write) of db->mallocFailed
27087 ** is unsafe, as is the call to sqlite3Error().
27088 */
27089 assert( db!=0 );
27090 assert( sqlite3_mutex_held(db->mutex) );
27091 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
27092 return apiOomError(db);
27093 }
27094 return rc & db->errMask;
27095}
27096
27097/************** End of malloc.c **********************************************/
27098/************** Begin file printf.c ******************************************/
27099/*
27100** The "printf" code that follows dates from the 1980's. It is in
27101** the public domain.
27102**
27103**************************************************************************
27104**
27105** This file contains code for a set of "printf"-like routines. These
27106** routines format strings much like the printf() from the standard C
27107** library, though the implementation here has enhancements to support
27108** SQLite.
27109*/
27110/* #include "sqliteInt.h" */
27111
27112/*
27113** Conversion types fall into various categories as defined by the
27114** following enumeration.
27115*/
27116#define etRADIX 0 /* non-decimal integer types. %x %o */
27117#define etFLOAT 1 /* Floating point. %f */
27118#define etEXP 2 /* Exponentional notation. %e and %E */
27119#define etGENERIC 3 /* Floating or exponential, depending on exponent. %g */
27120#define etSIZE 4 /* Return number of characters processed so far. %n */
27121#define etSTRING 5 /* Strings. %s */
27122#define etDYNSTRING 6 /* Dynamically allocated strings. %z */
27123#define etPERCENT 7 /* Percent symbol. %% */
27124#define etCHARX 8 /* Characters. %c */
27125/* The rest are extensions, not normally found in printf() */
27126#define etSQLESCAPE 9 /* Strings with '\'' doubled. %q */
27127#define etSQLESCAPE2 10 /* Strings with '\'' doubled and enclosed in '',
27128 NULL pointers replaced by SQL NULL. %Q */
27129#define etTOKEN 11 /* a pointer to a Token structure */
27130#define etSRCLIST 12 /* a pointer to a SrcList */
27131#define etPOINTER 13 /* The %p conversion */
27132#define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
27133#define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
27134#define etDECIMAL 16 /* %d or %u, but not %x, %o */
27135
27136#define etINVALID 17 /* Any unrecognized conversion type */
27137
27138
27139/*
27140** An "etByte" is an 8-bit unsigned value.
27141*/
27142typedef unsigned char etByte;
27143
27144/*
27145** Each builtin conversion character (ex: the 'd' in "%d") is described
27146** by an instance of the following structure
27147*/
27148typedef struct et_info { /* Information about each format field */
27149 char fmttype; /* The format field code letter */
27150 etByte base; /* The base for radix conversion */
27151 etByte flags; /* One or more of FLAG_ constants below */
27152 etByte type; /* Conversion paradigm */
27153 etByte charset; /* Offset into aDigits[] of the digits string */
27154 etByte prefix; /* Offset into aPrefix[] of the prefix string */
27155} et_info;
27156
27157/*
27158** Allowed values for et_info.flags
27159*/
27160#define FLAG_SIGNED 1 /* True if the value to convert is signed */
27161#define FLAG_STRING 4 /* Allow infinite precision */
27162
27163
27164/*
27165** The following table is searched linearly, so it is good to put the
27166** most frequently used conversion types first.
27167*/
27168static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
27169static const char aPrefix[] = "-x0\000X0";
27170static const et_info fmtinfo[] = {
27171 { 'd', 10, 1, etDECIMAL, 0, 0 },
27172 { 's', 0, 4, etSTRING, 0, 0 },
27173 { 'g', 0, 1, etGENERIC, 30, 0 },
27174 { 'z', 0, 4, etDYNSTRING, 0, 0 },
27175 { 'q', 0, 4, etSQLESCAPE, 0, 0 },
27176 { 'Q', 0, 4, etSQLESCAPE2, 0, 0 },
27177 { 'w', 0, 4, etSQLESCAPE3, 0, 0 },
27178 { 'c', 0, 0, etCHARX, 0, 0 },
27179 { 'o', 8, 0, etRADIX, 0, 2 },
27180 { 'u', 10, 0, etDECIMAL, 0, 0 },
27181 { 'x', 16, 0, etRADIX, 16, 1 },
27182 { 'X', 16, 0, etRADIX, 0, 4 },
27183#ifndef SQLITE_OMIT_FLOATING_POINT
27184 { 'f', 0, 1, etFLOAT, 0, 0 },
27185 { 'e', 0, 1, etEXP, 30, 0 },
27186 { 'E', 0, 1, etEXP, 14, 0 },
27187 { 'G', 0, 1, etGENERIC, 14, 0 },
27188#endif
27189 { 'i', 10, 1, etDECIMAL, 0, 0 },
27190 { 'n', 0, 0, etSIZE, 0, 0 },
27191 { '%', 0, 0, etPERCENT, 0, 0 },
27192 { 'p', 16, 0, etPOINTER, 0, 1 },
27193
27194 /* All the rest are undocumented and are for internal use only */
27195 { 'T', 0, 0, etTOKEN, 0, 0 },
27196 { 'S', 0, 0, etSRCLIST, 0, 0 },
27197 { 'r', 10, 1, etORDINAL, 0, 0 },
27198};
27199
27200/*
27201** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
27202** conversions will work.
27203*/
27204#ifndef SQLITE_OMIT_FLOATING_POINT
27205/*
27206** "*val" is a double such that 0.1 <= *val < 10.0
27207** Return the ascii code for the leading digit of *val, then
27208** multiply "*val" by 10.0 to renormalize.
27209**
27210** Example:
27211** input: *val = 3.14159
27212** output: *val = 1.4159 function return = '3'
27213**
27214** The counter *cnt is incremented each time. After counter exceeds
27215** 16 (the number of significant digits in a 64-bit float) '0' is
27216** always returned.
27217*/
27218static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
27219 int digit;
27220 LONGDOUBLE_TYPE d;
27221 if( (*cnt)<=0 ) return '0';
27222 (*cnt)--;
27223 digit = (int)*val;
27224 d = digit;
27225 digit += '0';
27226 *val = (*val - d)*10.0;
27227 return (char)digit;
27228}
27229#endif /* SQLITE_OMIT_FLOATING_POINT */
27230
27231/*
27232** Set the StrAccum object to an error mode.
27233*/
27234static void setStrAccumError(StrAccum *p, u8 eError){
27235 assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG );
27236 p->accError = eError;
27237 p->nAlloc = 0;
27238}
27239
27240/*
27241** Extra argument values from a PrintfArguments object
27242*/
27243static sqlite3_int64 getIntArg(PrintfArguments *p){
27244 if( p->nArg<=p->nUsed ) return 0;
27245 return sqlite3_value_int64(p->apArg[p->nUsed++]);
27246}
27247static double getDoubleArg(PrintfArguments *p){
27248 if( p->nArg<=p->nUsed ) return 0.0;
27249 return sqlite3_value_double(p->apArg[p->nUsed++]);
27250}
27251static char *getTextArg(PrintfArguments *p){
27252 if( p->nArg<=p->nUsed ) return 0;
27253 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
27254}
27255
27256
27257/*
27258** On machines with a small stack size, you can redefine the
27259** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
27260*/
27261#ifndef SQLITE_PRINT_BUF_SIZE
27262# define SQLITE_PRINT_BUF_SIZE 70
27263#endif
27264#define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */
27265
27266/*
27267** Render a string given by "fmt" into the StrAccum object.
27268*/
27269SQLITE_API void sqlite3_str_vappendf(
27270 sqlite3_str *pAccum, /* Accumulate results here */
27271 const char *fmt, /* Format string */
27272 va_list ap /* arguments */
27273){
27274 int c; /* Next character in the format string */
27275 char *bufpt; /* Pointer to the conversion buffer */
27276 int precision; /* Precision of the current field */
27277 int length; /* Length of the field */
27278 int idx; /* A general purpose loop counter */
27279 int width; /* Width of the current field */
27280 etByte flag_leftjustify; /* True if "-" flag is present */
27281 etByte flag_prefix; /* '+' or ' ' or 0 for prefix */
27282 etByte flag_alternateform; /* True if "#" flag is present */
27283 etByte flag_altform2; /* True if "!" flag is present */
27284 etByte flag_zeropad; /* True if field width constant starts with zero */
27285 etByte flag_long; /* 1 for the "l" flag, 2 for "ll", 0 by default */
27286 etByte done; /* Loop termination flag */
27287 etByte cThousand; /* Thousands separator for %d and %u */
27288 etByte xtype = etINVALID; /* Conversion paradigm */
27289 u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */
27290 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
27291 sqlite_uint64 longvalue; /* Value for integer types */
27292 LONGDOUBLE_TYPE realvalue; /* Value for real types */
27293 const et_info *infop; /* Pointer to the appropriate info structure */
27294 char *zOut; /* Rendering buffer */
27295 int nOut; /* Size of the rendering buffer */
27296 char *zExtra = 0; /* Malloced memory used by some conversion */
27297#ifndef SQLITE_OMIT_FLOATING_POINT
27298 int exp, e2; /* exponent of real numbers */
27299 int nsd; /* Number of significant digits returned */
27300 double rounder; /* Used for rounding floating point values */
27301 etByte flag_dp; /* True if decimal point should be shown */
27302 etByte flag_rtz; /* True if trailing zeros should be removed */
27303#endif
27304 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
27305 char buf[etBUFSIZE]; /* Conversion buffer */
27306
27307 /* pAccum never starts out with an empty buffer that was obtained from
27308 ** malloc(). This precondition is required by the mprintf("%z...")
27309 ** optimization. */
27310 assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
27311
27312 bufpt = 0;
27313 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
27314 pArgList = va_arg(ap, PrintfArguments*);
27315 bArgList = 1;
27316 }else{
27317 bArgList = 0;
27318 }
27319 for(; (c=(*fmt))!=0; ++fmt){
27320 if( c!='%' ){
27321 bufpt = (char *)fmt;
27322#if HAVE_STRCHRNUL
27323 fmt = strchrnul(fmt, '%');
27324#else
27325 do{ fmt++; }while( *fmt && *fmt != '%' );
27326#endif
27327 sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt));
27328 if( *fmt==0 ) break;
27329 }
27330 if( (c=(*++fmt))==0 ){
27331 sqlite3_str_append(pAccum, "%", 1);
27332 break;
27333 }
27334 /* Find out what flags are present */
27335 flag_leftjustify = flag_prefix = cThousand =
27336 flag_alternateform = flag_altform2 = flag_zeropad = 0;
27337 done = 0;
27338 do{
27339 switch( c ){
27340 case '-': flag_leftjustify = 1; break;
27341 case '+': flag_prefix = '+'; break;
27342 case ' ': flag_prefix = ' '; break;
27343 case '#': flag_alternateform = 1; break;
27344 case '!': flag_altform2 = 1; break;
27345 case '0': flag_zeropad = 1; break;
27346 case ',': cThousand = ','; break;
27347 default: done = 1; break;
27348 }
27349 }while( !done && (c=(*++fmt))!=0 );
27350 /* Get the field width */
27351 if( c=='*' ){
27352 if( bArgList ){
27353 width = (int)getIntArg(pArgList);
27354 }else{
27355 width = va_arg(ap,int);
27356 }
27357 if( width<0 ){
27358 flag_leftjustify = 1;
27359 width = width >= -2147483647 ? -width : 0;
27360 }
27361 c = *++fmt;
27362 }else{
27363 unsigned wx = 0;
27364 while( c>='0' && c<='9' ){
27365 wx = wx*10 + c - '0';
27366 c = *++fmt;
27367 }
27368 testcase( wx>0x7fffffff );
27369 width = wx & 0x7fffffff;
27370 }
27371 assert( width>=0 );
27372#ifdef SQLITE_PRINTF_PRECISION_LIMIT
27373 if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
27374 width = SQLITE_PRINTF_PRECISION_LIMIT;
27375 }
27376#endif
27377
27378 /* Get the precision */
27379 if( c=='.' ){
27380 c = *++fmt;
27381 if( c=='*' ){
27382 if( bArgList ){
27383 precision = (int)getIntArg(pArgList);
27384 }else{
27385 precision = va_arg(ap,int);
27386 }
27387 c = *++fmt;
27388 if( precision<0 ){
27389 precision = precision >= -2147483647 ? -precision : -1;
27390 }
27391 }else{
27392 unsigned px = 0;
27393 while( c>='0' && c<='9' ){
27394 px = px*10 + c - '0';
27395 c = *++fmt;
27396 }
27397 testcase( px>0x7fffffff );
27398 precision = px & 0x7fffffff;
27399 }
27400 }else{
27401 precision = -1;
27402 }
27403 assert( precision>=(-1) );
27404#ifdef SQLITE_PRINTF_PRECISION_LIMIT
27405 if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){
27406 precision = SQLITE_PRINTF_PRECISION_LIMIT;
27407 }
27408#endif
27409
27410
27411 /* Get the conversion type modifier */
27412 if( c=='l' ){
27413 flag_long = 1;
27414 c = *++fmt;
27415 if( c=='l' ){
27416 flag_long = 2;
27417 c = *++fmt;
27418 }
27419 }else{
27420 flag_long = 0;
27421 }
27422 /* Fetch the info entry for the field */
27423 infop = &fmtinfo[0];
27424 xtype = etINVALID;
27425 for(idx=0; idx<ArraySize(fmtinfo); idx++){
27426 if( c==fmtinfo[idx].fmttype ){
27427 infop = &fmtinfo[idx];
27428 xtype = infop->type;
27429 break;
27430 }
27431 }
27432
27433 /*
27434 ** At this point, variables are initialized as follows:
27435 **
27436 ** flag_alternateform TRUE if a '#' is present.
27437 ** flag_altform2 TRUE if a '!' is present.
27438 ** flag_prefix '+' or ' ' or zero
27439 ** flag_leftjustify TRUE if a '-' is present or if the
27440 ** field width was negative.
27441 ** flag_zeropad TRUE if the width began with 0.
27442 ** flag_long 1 for "l", 2 for "ll"
27443 ** width The specified field width. This is
27444 ** always non-negative. Zero is the default.
27445 ** precision The specified precision. The default
27446 ** is -1.
27447 ** xtype The class of the conversion.
27448 ** infop Pointer to the appropriate info struct.
27449 */
27450 switch( xtype ){
27451 case etPOINTER:
27452 flag_long = sizeof(char*)==sizeof(i64) ? 2 :
27453 sizeof(char*)==sizeof(long int) ? 1 : 0;
27454 /* Fall through into the next case */
27455 case etORDINAL:
27456 case etRADIX:
27457 cThousand = 0;
27458 /* Fall through into the next case */
27459 case etDECIMAL:
27460 if( infop->flags & FLAG_SIGNED ){
27461 i64 v;
27462 if( bArgList ){
27463 v = getIntArg(pArgList);
27464 }else if( flag_long ){
27465 if( flag_long==2 ){
27466 v = va_arg(ap,i64) ;
27467 }else{
27468 v = va_arg(ap,long int);
27469 }
27470 }else{
27471 v = va_arg(ap,int);
27472 }
27473 if( v<0 ){
27474 if( v==SMALLEST_INT64 ){
27475 longvalue = ((u64)1)<<63;
27476 }else{
27477 longvalue = -v;
27478 }
27479 prefix = '-';
27480 }else{
27481 longvalue = v;
27482 prefix = flag_prefix;
27483 }
27484 }else{
27485 if( bArgList ){
27486 longvalue = (u64)getIntArg(pArgList);
27487 }else if( flag_long ){
27488 if( flag_long==2 ){
27489 longvalue = va_arg(ap,u64);
27490 }else{
27491 longvalue = va_arg(ap,unsigned long int);
27492 }
27493 }else{
27494 longvalue = va_arg(ap,unsigned int);
27495 }
27496 prefix = 0;
27497 }
27498 if( longvalue==0 ) flag_alternateform = 0;
27499 if( flag_zeropad && precision<width-(prefix!=0) ){
27500 precision = width-(prefix!=0);
27501 }
27502 if( precision<etBUFSIZE-10-etBUFSIZE/3 ){
27503 nOut = etBUFSIZE;
27504 zOut = buf;
27505 }else{
27506 u64 n = (u64)precision + 10 + precision/3;
27507 zOut = zExtra = sqlite3Malloc( n );
27508 if( zOut==0 ){
27509 setStrAccumError(pAccum, SQLITE_NOMEM);
27510 return;
27511 }
27512 nOut = (int)n;
27513 }
27514 bufpt = &zOut[nOut-1];
27515 if( xtype==etORDINAL ){
27516 static const char zOrd[] = "thstndrd";
27517 int x = (int)(longvalue % 10);
27518 if( x>=4 || (longvalue/10)%10==1 ){
27519 x = 0;
27520 }
27521 *(--bufpt) = zOrd[x*2+1];
27522 *(--bufpt) = zOrd[x*2];
27523 }
27524 {
27525 const char *cset = &aDigits[infop->charset];
27526 u8 base = infop->base;
27527 do{ /* Convert to ascii */
27528 *(--bufpt) = cset[longvalue%base];
27529 longvalue = longvalue/base;
27530 }while( longvalue>0 );
27531 }
27532 length = (int)(&zOut[nOut-1]-bufpt);
27533 while( precision>length ){
27534 *(--bufpt) = '0'; /* Zero pad */
27535 length++;
27536 }
27537 if( cThousand ){
27538 int nn = (length - 1)/3; /* Number of "," to insert */
27539 int ix = (length - 1)%3 + 1;
27540 bufpt -= nn;
27541 for(idx=0; nn>0; idx++){
27542 bufpt[idx] = bufpt[idx+nn];
27543 ix--;
27544 if( ix==0 ){
27545 bufpt[++idx] = cThousand;
27546 nn--;
27547 ix = 3;
27548 }
27549 }
27550 }
27551 if( prefix ) *(--bufpt) = prefix; /* Add sign */
27552 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
27553 const char *pre;
27554 char x;
27555 pre = &aPrefix[infop->prefix];
27556 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
27557 }
27558 length = (int)(&zOut[nOut-1]-bufpt);
27559 break;
27560 case etFLOAT:
27561 case etEXP:
27562 case etGENERIC:
27563 if( bArgList ){
27564 realvalue = getDoubleArg(pArgList);
27565 }else{
27566 realvalue = va_arg(ap,double);
27567 }
27568#ifdef SQLITE_OMIT_FLOATING_POINT
27569 length = 0;
27570#else
27571 if( precision<0 ) precision = 6; /* Set default precision */
27572 if( realvalue<0.0 ){
27573 realvalue = -realvalue;
27574 prefix = '-';
27575 }else{
27576 prefix = flag_prefix;
27577 }
27578 if( xtype==etGENERIC && precision>0 ) precision--;
27579 testcase( precision>0xfff );
27580 for(idx=precision&0xfff, rounder=0.5; idx>0; idx--, rounder*=0.1){}
27581 if( xtype==etFLOAT ) realvalue += rounder;
27582 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
27583 exp = 0;
27584 if( sqlite3IsNaN((double)realvalue) ){
27585 bufpt = "NaN";
27586 length = 3;
27587 break;
27588 }
27589 if( realvalue>0.0 ){
27590 LONGDOUBLE_TYPE scale = 1.0;
27591 while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
27592 while( realvalue>=1e10*scale && exp<=350 ){ scale *= 1e10; exp+=10; }
27593 while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
27594 realvalue /= scale;
27595 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
27596 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
27597 if( exp>350 ){
27598 bufpt = buf;
27599 buf[0] = prefix;
27600 memcpy(buf+(prefix!=0),"Inf",4);
27601 length = 3+(prefix!=0);
27602 break;
27603 }
27604 }
27605 bufpt = buf;
27606 /*
27607 ** If the field type is etGENERIC, then convert to either etEXP
27608 ** or etFLOAT, as appropriate.
27609 */
27610 if( xtype!=etFLOAT ){
27611 realvalue += rounder;
27612 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
27613 }
27614 if( xtype==etGENERIC ){
27615 flag_rtz = !flag_alternateform;
27616 if( exp<-4 || exp>precision ){
27617 xtype = etEXP;
27618 }else{
27619 precision = precision - exp;
27620 xtype = etFLOAT;
27621 }
27622 }else{
27623 flag_rtz = flag_altform2;
27624 }
27625 if( xtype==etEXP ){
27626 e2 = 0;
27627 }else{
27628 e2 = exp;
27629 }
27630 if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){
27631 bufpt = zExtra
27632 = sqlite3Malloc( MAX(e2,0)+(i64)precision+(i64)width+15 );
27633 if( bufpt==0 ){
27634 setStrAccumError(pAccum, SQLITE_NOMEM);
27635 return;
27636 }
27637 }
27638 zOut = bufpt;
27639 nsd = 16 + flag_altform2*10;
27640 flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
27641 /* The sign in front of the number */
27642 if( prefix ){
27643 *(bufpt++) = prefix;
27644 }
27645 /* Digits prior to the decimal point */
27646 if( e2<0 ){
27647 *(bufpt++) = '0';
27648 }else{
27649 for(; e2>=0; e2--){
27650 *(bufpt++) = et_getdigit(&realvalue,&nsd);
27651 }
27652 }
27653 /* The decimal point */
27654 if( flag_dp ){
27655 *(bufpt++) = '.';
27656 }
27657 /* "0" digits after the decimal point but before the first
27658 ** significant digit of the number */
27659 for(e2++; e2<0; precision--, e2++){
27660 assert( precision>0 );
27661 *(bufpt++) = '0';
27662 }
27663 /* Significant digits after the decimal point */
27664 while( (precision--)>0 ){
27665 *(bufpt++) = et_getdigit(&realvalue,&nsd);
27666 }
27667 /* Remove trailing zeros and the "." if no digits follow the "." */
27668 if( flag_rtz && flag_dp ){
27669 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
27670 assert( bufpt>zOut );
27671 if( bufpt[-1]=='.' ){
27672 if( flag_altform2 ){
27673 *(bufpt++) = '0';
27674 }else{
27675 *(--bufpt) = 0;
27676 }
27677 }
27678 }
27679 /* Add the "eNNN" suffix */
27680 if( xtype==etEXP ){
27681 *(bufpt++) = aDigits[infop->charset];
27682 if( exp<0 ){
27683 *(bufpt++) = '-'; exp = -exp;
27684 }else{
27685 *(bufpt++) = '+';
27686 }
27687 if( exp>=100 ){
27688 *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */
27689 exp %= 100;
27690 }
27691 *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */
27692 *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */
27693 }
27694 *bufpt = 0;
27695
27696 /* The converted number is in buf[] and zero terminated. Output it.
27697 ** Note that the number is in the usual order, not reversed as with
27698 ** integer conversions. */
27699 length = (int)(bufpt-zOut);
27700 bufpt = zOut;
27701
27702 /* Special case: Add leading zeros if the flag_zeropad flag is
27703 ** set and we are not left justified */
27704 if( flag_zeropad && !flag_leftjustify && length < width){
27705 int i;
27706 int nPad = width - length;
27707 for(i=width; i>=nPad; i--){
27708 bufpt[i] = bufpt[i-nPad];
27709 }
27710 i = prefix!=0;
27711 while( nPad-- ) bufpt[i++] = '0';
27712 length = width;
27713 }
27714#endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
27715 break;
27716 case etSIZE:
27717 if( !bArgList ){
27718 *(va_arg(ap,int*)) = pAccum->nChar;
27719 }
27720 length = width = 0;
27721 break;
27722 case etPERCENT:
27723 buf[0] = '%';
27724 bufpt = buf;
27725 length = 1;
27726 break;
27727 case etCHARX:
27728 if( bArgList ){
27729 bufpt = getTextArg(pArgList);
27730 length = 1;
27731 if( bufpt ){
27732 buf[0] = c = *(bufpt++);
27733 if( (c&0xc0)==0xc0 ){
27734 while( length<4 && (bufpt[0]&0xc0)==0x80 ){
27735 buf[length++] = *(bufpt++);
27736 }
27737 }
27738 }else{
27739 buf[0] = 0;
27740 }
27741 }else{
27742 unsigned int ch = va_arg(ap,unsigned int);
27743 if( ch<0x00080 ){
27744 buf[0] = ch & 0xff;
27745 length = 1;
27746 }else if( ch<0x00800 ){
27747 buf[0] = 0xc0 + (u8)((ch>>6)&0x1f);
27748 buf[1] = 0x80 + (u8)(ch & 0x3f);
27749 length = 2;
27750 }else if( ch<0x10000 ){
27751 buf[0] = 0xe0 + (u8)((ch>>12)&0x0f);
27752 buf[1] = 0x80 + (u8)((ch>>6) & 0x3f);
27753 buf[2] = 0x80 + (u8)(ch & 0x3f);
27754 length = 3;
27755 }else{
27756 buf[0] = 0xf0 + (u8)((ch>>18) & 0x07);
27757 buf[1] = 0x80 + (u8)((ch>>12) & 0x3f);
27758 buf[2] = 0x80 + (u8)((ch>>6) & 0x3f);
27759 buf[3] = 0x80 + (u8)(ch & 0x3f);
27760 length = 4;
27761 }
27762 }
27763 if( precision>1 ){
27764 width -= precision-1;
27765 if( width>1 && !flag_leftjustify ){
27766 sqlite3_str_appendchar(pAccum, width-1, ' ');
27767 width = 0;
27768 }
27769 while( precision-- > 1 ){
27770 sqlite3_str_append(pAccum, buf, length);
27771 }
27772 }
27773 bufpt = buf;
27774 flag_altform2 = 1;
27775 goto adjust_width_for_utf8;
27776 case etSTRING:
27777 case etDYNSTRING:
27778 if( bArgList ){
27779 bufpt = getTextArg(pArgList);
27780 xtype = etSTRING;
27781 }else{
27782 bufpt = va_arg(ap,char*);
27783 }
27784 if( bufpt==0 ){
27785 bufpt = "";
27786 }else if( xtype==etDYNSTRING ){
27787 if( pAccum->nChar==0
27788 && pAccum->mxAlloc
27789 && width==0
27790 && precision<0
27791 && pAccum->accError==0
27792 ){
27793 /* Special optimization for sqlite3_mprintf("%z..."):
27794 ** Extend an existing memory allocation rather than creating
27795 ** a new one. */
27796 assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
27797 pAccum->zText = bufpt;
27798 pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt);
27799 pAccum->nChar = 0x7fffffff & (int)strlen(bufpt);
27800 pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED;
27801 length = 0;
27802 break;
27803 }
27804 zExtra = bufpt;
27805 }
27806 if( precision>=0 ){
27807 if( flag_altform2 ){
27808 /* Set length to the number of bytes needed in order to display
27809 ** precision characters */
27810 unsigned char *z = (unsigned char*)bufpt;
27811 while( precision-- > 0 && z[0] ){
27812 SQLITE_SKIP_UTF8(z);
27813 }
27814 length = (int)(z - (unsigned char*)bufpt);
27815 }else{
27816 for(length=0; length<precision && bufpt[length]; length++){}
27817 }
27818 }else{
27819 length = 0x7fffffff & (int)strlen(bufpt);
27820 }
27821 adjust_width_for_utf8:
27822 if( flag_altform2 && width>0 ){
27823 /* Adjust width to account for extra bytes in UTF-8 characters */
27824 int ii = length - 1;
27825 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
27826 }
27827 break;
27828 case etSQLESCAPE: /* %q: Escape ' characters */
27829 case etSQLESCAPE2: /* %Q: Escape ' and enclose in '...' */
27830 case etSQLESCAPE3: { /* %w: Escape " characters */
27831 int i, j, k, n, isnull;
27832 int needQuote;
27833 char ch;
27834 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */
27835 char *escarg;
27836
27837 if( bArgList ){
27838 escarg = getTextArg(pArgList);
27839 }else{
27840 escarg = va_arg(ap,char*);
27841 }
27842 isnull = escarg==0;
27843 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
27844 /* For %q, %Q, and %w, the precision is the number of byte (or
27845 ** characters if the ! flags is present) to use from the input.
27846 ** Because of the extra quoting characters inserted, the number
27847 ** of output characters may be larger than the precision.
27848 */
27849 k = precision;
27850 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
27851 if( ch==q ) n++;
27852 if( flag_altform2 && (ch&0xc0)==0xc0 ){
27853 while( (escarg[i+1]&0xc0)==0x80 ){ i++; }
27854 }
27855 }
27856 needQuote = !isnull && xtype==etSQLESCAPE2;
27857 n += i + 3;
27858 if( n>etBUFSIZE ){
27859 bufpt = zExtra = sqlite3Malloc( n );
27860 if( bufpt==0 ){
27861 setStrAccumError(pAccum, SQLITE_NOMEM);
27862 return;
27863 }
27864 }else{
27865 bufpt = buf;
27866 }
27867 j = 0;
27868 if( needQuote ) bufpt[j++] = q;
27869 k = i;
27870 for(i=0; i<k; i++){
27871 bufpt[j++] = ch = escarg[i];
27872 if( ch==q ) bufpt[j++] = ch;
27873 }
27874 if( needQuote ) bufpt[j++] = q;
27875 bufpt[j] = 0;
27876 length = j;
27877 goto adjust_width_for_utf8;
27878 }
27879 case etTOKEN: {
27880 Token *pToken;
27881 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
27882 pToken = va_arg(ap, Token*);
27883 assert( bArgList==0 );
27884 if( pToken && pToken->n ){
27885 sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n);
27886 }
27887 length = width = 0;
27888 break;
27889 }
27890 case etSRCLIST: {
27891 SrcList *pSrc;
27892 int k;
27893 struct SrcList_item *pItem;
27894 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
27895 pSrc = va_arg(ap, SrcList*);
27896 k = va_arg(ap, int);
27897 pItem = &pSrc->a[k];
27898 assert( bArgList==0 );
27899 assert( k>=0 && k<pSrc->nSrc );
27900 if( pItem->zDatabase ){
27901 sqlite3_str_appendall(pAccum, pItem->zDatabase);
27902 sqlite3_str_append(pAccum, ".", 1);
27903 }
27904 sqlite3_str_appendall(pAccum, pItem->zName);
27905 length = width = 0;
27906 break;
27907 }
27908 default: {
27909 assert( xtype==etINVALID );
27910 return;
27911 }
27912 }/* End switch over the format type */
27913 /*
27914 ** The text of the conversion is pointed to by "bufpt" and is
27915 ** "length" characters long. The field width is "width". Do
27916 ** the output. Both length and width are in bytes, not characters,
27917 ** at this point. If the "!" flag was present on string conversions
27918 ** indicating that width and precision should be expressed in characters,
27919 ** then the values have been translated prior to reaching this point.
27920 */
27921 width -= length;
27922 if( width>0 ){
27923 if( !flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
27924 sqlite3_str_append(pAccum, bufpt, length);
27925 if( flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
27926 }else{
27927 sqlite3_str_append(pAccum, bufpt, length);
27928 }
27929
27930 if( zExtra ){
27931 sqlite3DbFree(pAccum->db, zExtra);
27932 zExtra = 0;
27933 }
27934 }/* End for loop over the format string */
27935} /* End of function */
27936
27937/*
27938** Enlarge the memory allocation on a StrAccum object so that it is
27939** able to accept at least N more bytes of text.
27940**
27941** Return the number of bytes of text that StrAccum is able to accept
27942** after the attempted enlargement. The value returned might be zero.
27943*/
27944static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
27945 char *zNew;
27946 assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
27947 if( p->accError ){
27948 testcase(p->accError==SQLITE_TOOBIG);
27949 testcase(p->accError==SQLITE_NOMEM);
27950 return 0;
27951 }
27952 if( p->mxAlloc==0 ){
27953 N = p->nAlloc - p->nChar - 1;
27954 setStrAccumError(p, SQLITE_TOOBIG);
27955 return N;
27956 }else{
27957 char *zOld = isMalloced(p) ? p->zText : 0;
27958 i64 szNew = p->nChar;
27959 szNew += N + 1;
27960 if( szNew+p->nChar<=p->mxAlloc ){
27961 /* Force exponential buffer size growth as long as it does not overflow,
27962 ** to avoid having to call this routine too often */
27963 szNew += p->nChar;
27964 }
27965 if( szNew > p->mxAlloc ){
27966 sqlite3_str_reset(p);
27967 setStrAccumError(p, SQLITE_TOOBIG);
27968 return 0;
27969 }else{
27970 p->nAlloc = (int)szNew;
27971 }
27972 if( p->db ){
27973 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
27974 }else{
27975 zNew = sqlite3_realloc64(zOld, p->nAlloc);
27976 }
27977 if( zNew ){
27978 assert( p->zText!=0 || p->nChar==0 );
27979 if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
27980 p->zText = zNew;
27981 p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
27982 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
27983 }else{
27984 sqlite3_str_reset(p);
27985 setStrAccumError(p, SQLITE_NOMEM);
27986 return 0;
27987 }
27988 }
27989 return N;
27990}
27991
27992/*
27993** Append N copies of character c to the given string buffer.
27994*/
27995SQLITE_API void sqlite3_str_appendchar(sqlite3_str *p, int N, char c){
27996 testcase( p->nChar + (i64)N > 0x7fffffff );
27997 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
27998 return;
27999 }
28000 while( (N--)>0 ) p->zText[p->nChar++] = c;
28001}
28002
28003/*
28004** The StrAccum "p" is not large enough to accept N new bytes of z[].
28005** So enlarge if first, then do the append.
28006**
28007** This is a helper routine to sqlite3_str_append() that does special-case
28008** work (enlarging the buffer) using tail recursion, so that the
28009** sqlite3_str_append() routine can use fast calling semantics.
28010*/
28011static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){
28012 N = sqlite3StrAccumEnlarge(p, N);
28013 if( N>0 ){
28014 memcpy(&p->zText[p->nChar], z, N);
28015 p->nChar += N;
28016 }
28017}
28018
28019/*
28020** Append N bytes of text from z to the StrAccum object. Increase the
28021** size of the memory allocation for StrAccum if necessary.
28022*/
28023SQLITE_API void sqlite3_str_append(sqlite3_str *p, const char *z, int N){
28024 assert( z!=0 || N==0 );
28025 assert( p->zText!=0 || p->nChar==0 || p->accError );
28026 assert( N>=0 );
28027 assert( p->accError==0 || p->nAlloc==0 );
28028 if( p->nChar+N >= p->nAlloc ){
28029 enlargeAndAppend(p,z,N);
28030 }else if( N ){
28031 assert( p->zText );
28032 p->nChar += N;
28033 memcpy(&p->zText[p->nChar-N], z, N);
28034 }
28035}
28036
28037/*
28038** Append the complete text of zero-terminated string z[] to the p string.
28039*/
28040SQLITE_API void sqlite3_str_appendall(sqlite3_str *p, const char *z){
28041 sqlite3_str_append(p, z, sqlite3Strlen30(z));
28042}
28043
28044
28045/*
28046** Finish off a string by making sure it is zero-terminated.
28047** Return a pointer to the resulting string. Return a NULL
28048** pointer if any kind of error was encountered.
28049*/
28050static SQLITE_NOINLINE char *strAccumFinishRealloc(StrAccum *p){
28051 char *zText;
28052 assert( p->mxAlloc>0 && !isMalloced(p) );
28053 zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
28054 if( zText ){
28055 memcpy(zText, p->zText, p->nChar+1);
28056 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
28057 }else{
28058 setStrAccumError(p, SQLITE_NOMEM);
28059 }
28060 p->zText = zText;
28061 return zText;
28062}
28063SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
28064 if( p->zText ){
28065 p->zText[p->nChar] = 0;
28066 if( p->mxAlloc>0 && !isMalloced(p) ){
28067 return strAccumFinishRealloc(p);
28068 }
28069 }
28070 return p->zText;
28071}
28072
28073/*
28074** This singleton is an sqlite3_str object that is returned if
28075** sqlite3_malloc() fails to provide space for a real one. This
28076** sqlite3_str object accepts no new text and always returns
28077** an SQLITE_NOMEM error.
28078*/
28079static sqlite3_str sqlite3OomStr = {
28080 0, 0, 0, 0, 0, SQLITE_NOMEM, 0
28081};
28082
28083/* Finalize a string created using sqlite3_str_new().
28084*/
28085SQLITE_API char *sqlite3_str_finish(sqlite3_str *p){
28086 char *z;
28087 if( p!=0 && p!=&sqlite3OomStr ){
28088 z = sqlite3StrAccumFinish(p);
28089 sqlite3_free(p);
28090 }else{
28091 z = 0;
28092 }
28093 return z;
28094}
28095
28096/* Return any error code associated with p */
28097SQLITE_API int sqlite3_str_errcode(sqlite3_str *p){
28098 return p ? p->accError : SQLITE_NOMEM;
28099}
28100
28101/* Return the current length of p in bytes */
28102SQLITE_API int sqlite3_str_length(sqlite3_str *p){
28103 return p ? p->nChar : 0;
28104}
28105
28106/* Return the current value for p */
28107SQLITE_API char *sqlite3_str_value(sqlite3_str *p){
28108 if( p==0 || p->nChar==0 ) return 0;
28109 p->zText[p->nChar] = 0;
28110 return p->zText;
28111}
28112
28113/*
28114** Reset an StrAccum string. Reclaim all malloced memory.
28115*/
28116SQLITE_API void sqlite3_str_reset(StrAccum *p){
28117 if( isMalloced(p) ){
28118 sqlite3DbFree(p->db, p->zText);
28119 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED;
28120 }
28121 p->nAlloc = 0;
28122 p->nChar = 0;
28123 p->zText = 0;
28124}
28125
28126/*
28127** Initialize a string accumulator.
28128**
28129** p: The accumulator to be initialized.
28130** db: Pointer to a database connection. May be NULL. Lookaside
28131** memory is used if not NULL. db->mallocFailed is set appropriately
28132** when not NULL.
28133** zBase: An initial buffer. May be NULL in which case the initial buffer
28134** is malloced.
28135** n: Size of zBase in bytes. If total space requirements never exceed
28136** n then no memory allocations ever occur.
28137** mx: Maximum number of bytes to accumulate. If mx==0 then no memory
28138** allocations will ever occur.
28139*/
28140SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){
28141 p->zText = zBase;
28142 p->db = db;
28143 p->nAlloc = n;
28144 p->mxAlloc = mx;
28145 p->nChar = 0;
28146 p->accError = 0;
28147 p->printfFlags = 0;
28148}
28149
28150/* Allocate and initialize a new dynamic string object */
28151SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3 *db){
28152 sqlite3_str *p = sqlite3_malloc64(sizeof(*p));
28153 if( p ){
28154 sqlite3StrAccumInit(p, 0, 0, 0,
28155 db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH);
28156 }else{
28157 p = &sqlite3OomStr;
28158 }
28159 return p;
28160}
28161
28162/*
28163** Print into memory obtained from sqliteMalloc(). Use the internal
28164** %-conversion extensions.
28165*/
28166SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
28167 char *z;
28168 char zBase[SQLITE_PRINT_BUF_SIZE];
28169 StrAccum acc;
28170 assert( db!=0 );
28171 sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase),
28172 db->aLimit[SQLITE_LIMIT_LENGTH]);
28173 acc.printfFlags = SQLITE_PRINTF_INTERNAL;
28174 sqlite3_str_vappendf(&acc, zFormat, ap);
28175 z = sqlite3StrAccumFinish(&acc);
28176 if( acc.accError==SQLITE_NOMEM ){
28177 sqlite3OomFault(db);
28178 }
28179 return z;
28180}
28181
28182/*
28183** Print into memory obtained from sqliteMalloc(). Use the internal
28184** %-conversion extensions.
28185*/
28186SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
28187 va_list ap;
28188 char *z;
28189 va_start(ap, zFormat);
28190 z = sqlite3VMPrintf(db, zFormat, ap);
28191 va_end(ap);
28192 return z;
28193}
28194
28195/*
28196** Print into memory obtained from sqlite3_malloc(). Omit the internal
28197** %-conversion extensions.
28198*/
28199SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
28200 char *z;
28201 char zBase[SQLITE_PRINT_BUF_SIZE];
28202 StrAccum acc;
28203
28204#ifdef SQLITE_ENABLE_API_ARMOR
28205 if( zFormat==0 ){
28206 (void)SQLITE_MISUSE_BKPT;
28207 return 0;
28208 }
28209#endif
28210#ifndef SQLITE_OMIT_AUTOINIT
28211 if( sqlite3_initialize() ) return 0;
28212#endif
28213 sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
28214 sqlite3_str_vappendf(&acc, zFormat, ap);
28215 z = sqlite3StrAccumFinish(&acc);
28216 return z;
28217}
28218
28219/*
28220** Print into memory obtained from sqlite3_malloc()(). Omit the internal
28221** %-conversion extensions.
28222*/
28223SQLITE_API char *sqlite3_mprintf(const char *zFormat, ...){
28224 va_list ap;
28225 char *z;
28226#ifndef SQLITE_OMIT_AUTOINIT
28227 if( sqlite3_initialize() ) return 0;
28228#endif
28229 va_start(ap, zFormat);
28230 z = sqlite3_vmprintf(zFormat, ap);
28231 va_end(ap);
28232 return z;
28233}
28234
28235/*
28236** sqlite3_snprintf() works like snprintf() except that it ignores the
28237** current locale settings. This is important for SQLite because we
28238** are not able to use a "," as the decimal point in place of "." as
28239** specified by some locales.
28240**
28241** Oops: The first two arguments of sqlite3_snprintf() are backwards
28242** from the snprintf() standard. Unfortunately, it is too late to change
28243** this without breaking compatibility, so we just have to live with the
28244** mistake.
28245**
28246** sqlite3_vsnprintf() is the varargs version.
28247*/
28248SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
28249 StrAccum acc;
28250 if( n<=0 ) return zBuf;
28251#ifdef SQLITE_ENABLE_API_ARMOR
28252 if( zBuf==0 || zFormat==0 ) {
28253 (void)SQLITE_MISUSE_BKPT;
28254 if( zBuf ) zBuf[0] = 0;
28255 return zBuf;
28256 }
28257#endif
28258 sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
28259 sqlite3_str_vappendf(&acc, zFormat, ap);
28260 zBuf[acc.nChar] = 0;
28261 return zBuf;
28262}
28263SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
28264 char *z;
28265 va_list ap;
28266 va_start(ap,zFormat);
28267 z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
28268 va_end(ap);
28269 return z;
28270}
28271
28272/*
28273** This is the routine that actually formats the sqlite3_log() message.
28274** We house it in a separate routine from sqlite3_log() to avoid using
28275** stack space on small-stack systems when logging is disabled.
28276**
28277** sqlite3_log() must render into a static buffer. It cannot dynamically
28278** allocate memory because it might be called while the memory allocator
28279** mutex is held.
28280**
28281** sqlite3_str_vappendf() might ask for *temporary* memory allocations for
28282** certain format characters (%q) or for very large precisions or widths.
28283** Care must be taken that any sqlite3_log() calls that occur while the
28284** memory mutex is held do not use these mechanisms.
28285*/
28286static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
28287 StrAccum acc; /* String accumulator */
28288 char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */
28289
28290 sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0);
28291 sqlite3_str_vappendf(&acc, zFormat, ap);
28292 sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
28293 sqlite3StrAccumFinish(&acc));
28294}
28295
28296/*
28297** Format and write a message to the log if logging is enabled.
28298*/
28299SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...){
28300 va_list ap; /* Vararg list */
28301 if( sqlite3GlobalConfig.xLog ){
28302 va_start(ap, zFormat);
28303 renderLogMsg(iErrCode, zFormat, ap);
28304 va_end(ap);
28305 }
28306}
28307
28308#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
28309/*
28310** A version of printf() that understands %lld. Used for debugging.
28311** The printf() built into some versions of windows does not understand %lld
28312** and segfaults if you give it a long long int.
28313*/
28314SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){
28315 va_list ap;
28316 StrAccum acc;
28317 char zBuf[500];
28318 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
28319 va_start(ap,zFormat);
28320 sqlite3_str_vappendf(&acc, zFormat, ap);
28321 va_end(ap);
28322 sqlite3StrAccumFinish(&acc);
28323#ifdef SQLITE_OS_TRACE_PROC
28324 {
28325 extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf);
28326 SQLITE_OS_TRACE_PROC(zBuf, sizeof(zBuf));
28327 }
28328#else
28329 fprintf(stdout,"%s", zBuf);
28330 fflush(stdout);
28331#endif
28332}
28333#endif
28334
28335
28336/*
28337** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument
28338** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats.
28339*/
28340SQLITE_API void sqlite3_str_appendf(StrAccum *p, const char *zFormat, ...){
28341 va_list ap;
28342 va_start(ap,zFormat);
28343 sqlite3_str_vappendf(p, zFormat, ap);
28344 va_end(ap);
28345}
28346
28347/************** End of printf.c **********************************************/
28348/************** Begin file treeview.c ****************************************/
28349/*
28350** 2015-06-08
28351**
28352** The author disclaims copyright to this source code. In place of
28353** a legal notice, here is a blessing:
28354**
28355** May you do good and not evil.
28356** May you find forgiveness for yourself and forgive others.
28357** May you share freely, never taking more than you give.
28358**
28359*************************************************************************
28360**
28361** This file contains C code to implement the TreeView debugging routines.
28362** These routines print a parse tree to standard output for debugging and
28363** analysis.
28364**
28365** The interfaces in this file is only available when compiling
28366** with SQLITE_DEBUG.
28367*/
28368/* #include "sqliteInt.h" */
28369#ifdef SQLITE_DEBUG
28370
28371/*
28372** Add a new subitem to the tree. The moreToFollow flag indicates that this
28373** is not the last item in the tree.
28374*/
28375static TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
28376 if( p==0 ){
28377 p = sqlite3_malloc64( sizeof(*p) );
28378 if( p==0 ) return 0;
28379 memset(p, 0, sizeof(*p));
28380 }else{
28381 p->iLevel++;
28382 }
28383 assert( moreToFollow==0 || moreToFollow==1 );
28384 if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
28385 return p;
28386}
28387
28388/*
28389** Finished with one layer of the tree
28390*/
28391static void sqlite3TreeViewPop(TreeView *p){
28392 if( p==0 ) return;
28393 p->iLevel--;
28394 if( p->iLevel<0 ) sqlite3_free(p);
28395}
28396
28397/*
28398** Generate a single line of output for the tree, with a prefix that contains
28399** all the appropriate tree lines
28400*/
28401static void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
28402 va_list ap;
28403 int i;
28404 StrAccum acc;
28405 char zBuf[500];
28406 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
28407 if( p ){
28408 for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
28409 sqlite3_str_append(&acc, p->bLine[i] ? "| " : " ", 4);
28410 }
28411 sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
28412 }
28413 if( zFormat!=0 ){
28414 va_start(ap, zFormat);
28415 sqlite3_str_vappendf(&acc, zFormat, ap);
28416 va_end(ap);
28417 assert( acc.nChar>0 );
28418 sqlite3_str_append(&acc, "\n", 1);
28419 }
28420 sqlite3StrAccumFinish(&acc);
28421 fprintf(stdout,"%s", zBuf);
28422 fflush(stdout);
28423}
28424
28425/*
28426** Shorthand for starting a new tree item that consists of a single label
28427*/
28428static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){
28429 p = sqlite3TreeViewPush(p, moreFollows);
28430 sqlite3TreeViewLine(p, "%s", zLabel);
28431}
28432
28433/*
28434** Generate a human-readable description of a WITH clause.
28435*/
28436SQLITE_PRIVATE void sqlite3TreeViewWith(TreeView *pView, const With *pWith, u8 moreToFollow){
28437 int i;
28438 if( pWith==0 ) return;
28439 if( pWith->nCte==0 ) return;
28440 if( pWith->pOuter ){
28441 sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter);
28442 }else{
28443 sqlite3TreeViewLine(pView, "WITH (0x%p)", pWith);
28444 }
28445 if( pWith->nCte>0 ){
28446 pView = sqlite3TreeViewPush(pView, 1);
28447 for(i=0; i<pWith->nCte; i++){
28448 StrAccum x;
28449 char zLine[1000];
28450 const struct Cte *pCte = &pWith->a[i];
28451 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
28452 sqlite3_str_appendf(&x, "%s", pCte->zName);
28453 if( pCte->pCols && pCte->pCols->nExpr>0 ){
28454 char cSep = '(';
28455 int j;
28456 for(j=0; j<pCte->pCols->nExpr; j++){
28457 sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zName);
28458 cSep = ',';
28459 }
28460 sqlite3_str_appendf(&x, ")");
28461 }
28462 sqlite3_str_appendf(&x, " AS");
28463 sqlite3StrAccumFinish(&x);
28464 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
28465 sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
28466 sqlite3TreeViewPop(pView);
28467 }
28468 sqlite3TreeViewPop(pView);
28469 }
28470}
28471
28472/*
28473** Generate a human-readable description of a SrcList object.
28474*/
28475SQLITE_PRIVATE void sqlite3TreeViewSrcList(TreeView *pView, const SrcList *pSrc){
28476 int i;
28477 for(i=0; i<pSrc->nSrc; i++){
28478 const struct SrcList_item *pItem = &pSrc->a[i];
28479 StrAccum x;
28480 char zLine[100];
28481 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
28482 sqlite3_str_appendf(&x, "{%d,*}", pItem->iCursor);
28483 if( pItem->zDatabase ){
28484 sqlite3_str_appendf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
28485 }else if( pItem->zName ){
28486 sqlite3_str_appendf(&x, " %s", pItem->zName);
28487 }
28488 if( pItem->pTab ){
28489 sqlite3_str_appendf(&x, " tabname=%Q", pItem->pTab->zName);
28490 }
28491 if( pItem->zAlias ){
28492 sqlite3_str_appendf(&x, " (AS %s)", pItem->zAlias);
28493 }
28494 if( pItem->fg.jointype & JT_LEFT ){
28495 sqlite3_str_appendf(&x, " LEFT-JOIN");
28496 }
28497 sqlite3StrAccumFinish(&x);
28498 sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1);
28499 if( pItem->pSelect ){
28500 sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
28501 }
28502 if( pItem->fg.isTabFunc ){
28503 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
28504 }
28505 sqlite3TreeViewPop(pView);
28506 }
28507}
28508
28509/*
28510** Generate a human-readable description of a Select object.
28511*/
28512SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
28513 int n = 0;
28514 int cnt = 0;
28515 if( p==0 ){
28516 sqlite3TreeViewLine(pView, "nil-SELECT");
28517 return;
28518 }
28519 pView = sqlite3TreeViewPush(pView, moreToFollow);
28520 if( p->pWith ){
28521 sqlite3TreeViewWith(pView, p->pWith, 1);
28522 cnt = 1;
28523 sqlite3TreeViewPush(pView, 1);
28524 }
28525 do{
28526 sqlite3TreeViewLine(pView,
28527 "SELECT%s%s (%u/%p) selFlags=0x%x nSelectRow=%d",
28528 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
28529 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""),
28530 p->selId, p, p->selFlags,
28531 (int)p->nSelectRow
28532 );
28533 if( cnt++ ) sqlite3TreeViewPop(pView);
28534 if( p->pPrior ){
28535 n = 1000;
28536 }else{
28537 n = 0;
28538 if( p->pSrc && p->pSrc->nSrc ) n++;
28539 if( p->pWhere ) n++;
28540 if( p->pGroupBy ) n++;
28541 if( p->pHaving ) n++;
28542 if( p->pOrderBy ) n++;
28543 if( p->pLimit ) n++;
28544#ifndef SQLITE_OMIT_WINDOWFUNC
28545 if( p->pWin ) n++;
28546 if( p->pWinDefn ) n++;
28547#endif
28548 }
28549 sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set");
28550#ifndef SQLITE_OMIT_WINDOWFUNC
28551 if( p->pWin ){
28552 Window *pX;
28553 pView = sqlite3TreeViewPush(pView, (n--)>0);
28554 sqlite3TreeViewLine(pView, "window-functions");
28555 for(pX=p->pWin; pX; pX=pX->pNextWin){
28556 sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0);
28557 }
28558 sqlite3TreeViewPop(pView);
28559 }
28560#endif
28561 if( p->pSrc && p->pSrc->nSrc ){
28562 pView = sqlite3TreeViewPush(pView, (n--)>0);
28563 sqlite3TreeViewLine(pView, "FROM");
28564 sqlite3TreeViewSrcList(pView, p->pSrc);
28565 sqlite3TreeViewPop(pView);
28566 }
28567 if( p->pWhere ){
28568 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
28569 sqlite3TreeViewExpr(pView, p->pWhere, 0);
28570 sqlite3TreeViewPop(pView);
28571 }
28572 if( p->pGroupBy ){
28573 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
28574 }
28575 if( p->pHaving ){
28576 sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
28577 sqlite3TreeViewExpr(pView, p->pHaving, 0);
28578 sqlite3TreeViewPop(pView);
28579 }
28580#ifndef SQLITE_OMIT_WINDOWFUNC
28581 if( p->pWinDefn ){
28582 Window *pX;
28583 sqlite3TreeViewItem(pView, "WINDOW", (n--)>0);
28584 for(pX=p->pWinDefn; pX; pX=pX->pNextWin){
28585 sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0);
28586 }
28587 sqlite3TreeViewPop(pView);
28588 }
28589#endif
28590 if( p->pOrderBy ){
28591 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
28592 }
28593 if( p->pLimit ){
28594 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
28595 sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
28596 if( p->pLimit->pRight ){
28597 sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
28598 sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
28599 sqlite3TreeViewPop(pView);
28600 }
28601 sqlite3TreeViewPop(pView);
28602 }
28603 if( p->pPrior ){
28604 const char *zOp = "UNION";
28605 switch( p->op ){
28606 case TK_ALL: zOp = "UNION ALL"; break;
28607 case TK_INTERSECT: zOp = "INTERSECT"; break;
28608 case TK_EXCEPT: zOp = "EXCEPT"; break;
28609 }
28610 sqlite3TreeViewItem(pView, zOp, 1);
28611 }
28612 p = p->pPrior;
28613 }while( p!=0 );
28614 sqlite3TreeViewPop(pView);
28615}
28616
28617#ifndef SQLITE_OMIT_WINDOWFUNC
28618/*
28619** Generate a description of starting or stopping bounds
28620*/
28621SQLITE_PRIVATE void sqlite3TreeViewBound(
28622 TreeView *pView, /* View context */
28623 u8 eBound, /* UNBOUNDED, CURRENT, PRECEDING, FOLLOWING */
28624 Expr *pExpr, /* Value for PRECEDING or FOLLOWING */
28625 u8 moreToFollow /* True if more to follow */
28626){
28627 switch( eBound ){
28628 case TK_UNBOUNDED: {
28629 sqlite3TreeViewItem(pView, "UNBOUNDED", moreToFollow);
28630 sqlite3TreeViewPop(pView);
28631 break;
28632 }
28633 case TK_CURRENT: {
28634 sqlite3TreeViewItem(pView, "CURRENT", moreToFollow);
28635 sqlite3TreeViewPop(pView);
28636 break;
28637 }
28638 case TK_PRECEDING: {
28639 sqlite3TreeViewItem(pView, "PRECEDING", moreToFollow);
28640 sqlite3TreeViewExpr(pView, pExpr, 0);
28641 sqlite3TreeViewPop(pView);
28642 break;
28643 }
28644 case TK_FOLLOWING: {
28645 sqlite3TreeViewItem(pView, "FOLLOWING", moreToFollow);
28646 sqlite3TreeViewExpr(pView, pExpr, 0);
28647 sqlite3TreeViewPop(pView);
28648 break;
28649 }
28650 }
28651}
28652#endif /* SQLITE_OMIT_WINDOWFUNC */
28653
28654#ifndef SQLITE_OMIT_WINDOWFUNC
28655/*
28656** Generate a human-readable explanation for a Window object
28657*/
28658SQLITE_PRIVATE void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u8 more){
28659 pView = sqlite3TreeViewPush(pView, more);
28660 if( pWin->zName ){
28661 sqlite3TreeViewLine(pView, "OVER %s", pWin->zName);
28662 }else{
28663 sqlite3TreeViewLine(pView, "OVER");
28664 }
28665 if( pWin->pPartition ){
28666 sqlite3TreeViewExprList(pView, pWin->pPartition, 1, "PARTITION-BY");
28667 }
28668 if( pWin->pOrderBy ){
28669 sqlite3TreeViewExprList(pView, pWin->pOrderBy, 1, "ORDER-BY");
28670 }
28671 if( pWin->eType ){
28672 sqlite3TreeViewItem(pView, pWin->eType==TK_RANGE ? "RANGE" : "ROWS", 0);
28673 sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1);
28674 sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0);
28675 sqlite3TreeViewPop(pView);
28676 }
28677 sqlite3TreeViewPop(pView);
28678}
28679#endif /* SQLITE_OMIT_WINDOWFUNC */
28680
28681#ifndef SQLITE_OMIT_WINDOWFUNC
28682/*
28683** Generate a human-readable explanation for a Window Function object
28684*/
28685SQLITE_PRIVATE void sqlite3TreeViewWinFunc(TreeView *pView, const Window *pWin, u8 more){
28686 pView = sqlite3TreeViewPush(pView, more);
28687 sqlite3TreeViewLine(pView, "WINFUNC %s(%d)",
28688 pWin->pFunc->zName, pWin->pFunc->nArg);
28689 sqlite3TreeViewWindow(pView, pWin, 0);
28690 sqlite3TreeViewPop(pView);
28691}
28692#endif /* SQLITE_OMIT_WINDOWFUNC */
28693
28694/*
28695** Generate a human-readable explanation of an expression tree.
28696*/
28697SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
28698 const char *zBinOp = 0; /* Binary operator */
28699 const char *zUniOp = 0; /* Unary operator */
28700 char zFlgs[60];
28701 pView = sqlite3TreeViewPush(pView, moreToFollow);
28702 if( pExpr==0 ){
28703 sqlite3TreeViewLine(pView, "nil");
28704 sqlite3TreeViewPop(pView);
28705 return;
28706 }
28707 if( pExpr->flags ){
28708 if( ExprHasProperty(pExpr, EP_FromJoin) ){
28709 sqlite3_snprintf(sizeof(zFlgs),zFlgs," flags=0x%x iRJT=%d",
28710 pExpr->flags, pExpr->iRightJoinTable);
28711 }else{
28712 sqlite3_snprintf(sizeof(zFlgs),zFlgs," flags=0x%x",pExpr->flags);
28713 }
28714 }else{
28715 zFlgs[0] = 0;
28716 }
28717 switch( pExpr->op ){
28718 case TK_AGG_COLUMN: {
28719 sqlite3TreeViewLine(pView, "AGG{%d:%d}%s",
28720 pExpr->iTable, pExpr->iColumn, zFlgs);
28721 break;
28722 }
28723 case TK_COLUMN: {
28724 if( pExpr->iTable<0 ){
28725 /* This only happens when coding check constraints */
28726 sqlite3TreeViewLine(pView, "COLUMN(%d)%s", pExpr->iColumn, zFlgs);
28727 }else{
28728 sqlite3TreeViewLine(pView, "{%d:%d}%s",
28729 pExpr->iTable, pExpr->iColumn, zFlgs);
28730 }
28731 if( ExprHasProperty(pExpr, EP_FixedCol) ){
28732 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
28733 }
28734 break;
28735 }
28736 case TK_INTEGER: {
28737 if( pExpr->flags & EP_IntValue ){
28738 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
28739 }else{
28740 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
28741 }
28742 break;
28743 }
28744#ifndef SQLITE_OMIT_FLOATING_POINT
28745 case TK_FLOAT: {
28746 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
28747 break;
28748 }
28749#endif
28750 case TK_STRING: {
28751 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
28752 break;
28753 }
28754 case TK_NULL: {
28755 sqlite3TreeViewLine(pView,"NULL");
28756 break;
28757 }
28758 case TK_TRUEFALSE: {
28759 sqlite3TreeViewLine(pView,
28760 sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE");
28761 break;
28762 }
28763#ifndef SQLITE_OMIT_BLOB_LITERAL
28764 case TK_BLOB: {
28765 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
28766 break;
28767 }
28768#endif
28769 case TK_VARIABLE: {
28770 sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
28771 pExpr->u.zToken, pExpr->iColumn);
28772 break;
28773 }
28774 case TK_REGISTER: {
28775 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
28776 break;
28777 }
28778 case TK_ID: {
28779 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
28780 break;
28781 }
28782#ifndef SQLITE_OMIT_CAST
28783 case TK_CAST: {
28784 /* Expressions of the form: CAST(pLeft AS token) */
28785 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
28786 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
28787 break;
28788 }
28789#endif /* SQLITE_OMIT_CAST */
28790 case TK_LT: zBinOp = "LT"; break;
28791 case TK_LE: zBinOp = "LE"; break;
28792 case TK_GT: zBinOp = "GT"; break;
28793 case TK_GE: zBinOp = "GE"; break;
28794 case TK_NE: zBinOp = "NE"; break;
28795 case TK_EQ: zBinOp = "EQ"; break;
28796 case TK_IS: zBinOp = "IS"; break;
28797 case TK_ISNOT: zBinOp = "ISNOT"; break;
28798 case TK_AND: zBinOp = "AND"; break;
28799 case TK_OR: zBinOp = "OR"; break;
28800 case TK_PLUS: zBinOp = "ADD"; break;
28801 case TK_STAR: zBinOp = "MUL"; break;
28802 case TK_MINUS: zBinOp = "SUB"; break;
28803 case TK_REM: zBinOp = "REM"; break;
28804 case TK_BITAND: zBinOp = "BITAND"; break;
28805 case TK_BITOR: zBinOp = "BITOR"; break;
28806 case TK_SLASH: zBinOp = "DIV"; break;
28807 case TK_LSHIFT: zBinOp = "LSHIFT"; break;
28808 case TK_RSHIFT: zBinOp = "RSHIFT"; break;
28809 case TK_CONCAT: zBinOp = "CONCAT"; break;
28810 case TK_DOT: zBinOp = "DOT"; break;
28811
28812 case TK_UMINUS: zUniOp = "UMINUS"; break;
28813 case TK_UPLUS: zUniOp = "UPLUS"; break;
28814 case TK_BITNOT: zUniOp = "BITNOT"; break;
28815 case TK_NOT: zUniOp = "NOT"; break;
28816 case TK_ISNULL: zUniOp = "ISNULL"; break;
28817 case TK_NOTNULL: zUniOp = "NOTNULL"; break;
28818
28819 case TK_TRUTH: {
28820 int x;
28821 const char *azOp[] = {
28822 "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
28823 };
28824 assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
28825 assert( pExpr->pRight );
28826 assert( pExpr->pRight->op==TK_TRUEFALSE );
28827 x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
28828 zUniOp = azOp[x];
28829 break;
28830 }
28831
28832 case TK_SPAN: {
28833 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
28834 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
28835 break;
28836 }
28837
28838 case TK_COLLATE: {
28839 sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
28840 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
28841 break;
28842 }
28843
28844 case TK_AGG_FUNCTION:
28845 case TK_FUNCTION: {
28846 ExprList *pFarg; /* List of function arguments */
28847 Window *pWin;
28848 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
28849 pFarg = 0;
28850 pWin = 0;
28851 }else{
28852 pFarg = pExpr->x.pList;
28853#ifndef SQLITE_OMIT_WINDOWFUNC
28854 pWin = pExpr->y.pWin;
28855#else
28856 pWin = 0;
28857#endif
28858 }
28859 if( pExpr->op==TK_AGG_FUNCTION ){
28860 sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
28861 pExpr->op2, pExpr->u.zToken);
28862 }else{
28863 sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
28864 }
28865 if( pFarg ){
28866 sqlite3TreeViewExprList(pView, pFarg, pWin!=0, 0);
28867 }
28868#ifndef SQLITE_OMIT_WINDOWFUNC
28869 if( pWin ){
28870 sqlite3TreeViewWindow(pView, pWin, 0);
28871 }
28872#endif
28873 break;
28874 }
28875#ifndef SQLITE_OMIT_SUBQUERY
28876 case TK_EXISTS: {
28877 sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags);
28878 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
28879 break;
28880 }
28881 case TK_SELECT: {
28882 sqlite3TreeViewLine(pView, "SELECT-expr flags=0x%x", pExpr->flags);
28883 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
28884 break;
28885 }
28886 case TK_IN: {
28887 sqlite3TreeViewLine(pView, "IN flags=0x%x", pExpr->flags);
28888 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
28889 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
28890 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
28891 }else{
28892 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
28893 }
28894 break;
28895 }
28896#endif /* SQLITE_OMIT_SUBQUERY */
28897
28898 /*
28899 ** x BETWEEN y AND z
28900 **
28901 ** This is equivalent to
28902 **
28903 ** x>=y AND x<=z
28904 **
28905 ** X is stored in pExpr->pLeft.
28906 ** Y is stored in pExpr->pList->a[0].pExpr.
28907 ** Z is stored in pExpr->pList->a[1].pExpr.
28908 */
28909 case TK_BETWEEN: {
28910 Expr *pX = pExpr->pLeft;
28911 Expr *pY = pExpr->x.pList->a[0].pExpr;
28912 Expr *pZ = pExpr->x.pList->a[1].pExpr;
28913 sqlite3TreeViewLine(pView, "BETWEEN");
28914 sqlite3TreeViewExpr(pView, pX, 1);
28915 sqlite3TreeViewExpr(pView, pY, 1);
28916 sqlite3TreeViewExpr(pView, pZ, 0);
28917 break;
28918 }
28919 case TK_TRIGGER: {
28920 /* If the opcode is TK_TRIGGER, then the expression is a reference
28921 ** to a column in the new.* or old.* pseudo-tables available to
28922 ** trigger programs. In this case Expr.iTable is set to 1 for the
28923 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
28924 ** is set to the column of the pseudo-table to read, or to -1 to
28925 ** read the rowid field.
28926 */
28927 sqlite3TreeViewLine(pView, "%s(%d)",
28928 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
28929 break;
28930 }
28931 case TK_CASE: {
28932 sqlite3TreeViewLine(pView, "CASE");
28933 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
28934 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
28935 break;
28936 }
28937#ifndef SQLITE_OMIT_TRIGGER
28938 case TK_RAISE: {
28939 const char *zType = "unk";
28940 switch( pExpr->affinity ){
28941 case OE_Rollback: zType = "rollback"; break;
28942 case OE_Abort: zType = "abort"; break;
28943 case OE_Fail: zType = "fail"; break;
28944 case OE_Ignore: zType = "ignore"; break;
28945 }
28946 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
28947 break;
28948 }
28949#endif
28950 case TK_MATCH: {
28951 sqlite3TreeViewLine(pView, "MATCH {%d:%d}%s",
28952 pExpr->iTable, pExpr->iColumn, zFlgs);
28953 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
28954 break;
28955 }
28956 case TK_VECTOR: {
28957 sqlite3TreeViewBareExprList(pView, pExpr->x.pList, "VECTOR");
28958 break;
28959 }
28960 case TK_SELECT_COLUMN: {
28961 sqlite3TreeViewLine(pView, "SELECT-COLUMN %d", pExpr->iColumn);
28962 sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
28963 break;
28964 }
28965 case TK_IF_NULL_ROW: {
28966 sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable);
28967 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
28968 break;
28969 }
28970 default: {
28971 sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
28972 break;
28973 }
28974 }
28975 if( zBinOp ){
28976 sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
28977 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
28978 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
28979 }else if( zUniOp ){
28980 sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
28981 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
28982 }
28983 sqlite3TreeViewPop(pView);
28984}
28985
28986
28987/*
28988** Generate a human-readable explanation of an expression list.
28989*/
28990SQLITE_PRIVATE void sqlite3TreeViewBareExprList(
28991 TreeView *pView,
28992 const ExprList *pList,
28993 const char *zLabel
28994){
28995 if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
28996 if( pList==0 ){
28997 sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
28998 }else{
28999 int i;
29000 sqlite3TreeViewLine(pView, "%s", zLabel);
29001 for(i=0; i<pList->nExpr; i++){
29002 int j = pList->a[i].u.x.iOrderByCol;
29003 char *zName = pList->a[i].zName;
29004 int moreToFollow = i<pList->nExpr - 1;
29005 if( j || zName ){
29006 sqlite3TreeViewPush(pView, moreToFollow);
29007 moreToFollow = 0;
29008 sqlite3TreeViewLine(pView, 0);
29009 if( zName ){
29010 fprintf(stdout, "AS %s ", zName);
29011 }
29012 if( j ){
29013 fprintf(stdout, "iOrderByCol=%d", j);
29014 }
29015 fprintf(stdout, "\n");
29016 fflush(stdout);
29017 }
29018 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow);
29019 if( j || zName ){
29020 sqlite3TreeViewPop(pView);
29021 }
29022 }
29023 }
29024}
29025SQLITE_PRIVATE void sqlite3TreeViewExprList(
29026 TreeView *pView,
29027 const ExprList *pList,
29028 u8 moreToFollow,
29029 const char *zLabel
29030){
29031 pView = sqlite3TreeViewPush(pView, moreToFollow);
29032 sqlite3TreeViewBareExprList(pView, pList, zLabel);
29033 sqlite3TreeViewPop(pView);
29034}
29035
29036#endif /* SQLITE_DEBUG */
29037
29038/************** End of treeview.c ********************************************/
29039/************** Begin file random.c ******************************************/
29040/*
29041** 2001 September 15
29042**
29043** The author disclaims copyright to this source code. In place of
29044** a legal notice, here is a blessing:
29045**
29046** May you do good and not evil.
29047** May you find forgiveness for yourself and forgive others.
29048** May you share freely, never taking more than you give.
29049**
29050*************************************************************************
29051** This file contains code to implement a pseudo-random number
29052** generator (PRNG) for SQLite.
29053**
29054** Random numbers are used by some of the database backends in order
29055** to generate random integer keys for tables or random filenames.
29056*/
29057/* #include "sqliteInt.h" */
29058
29059
29060/* All threads share a single random number generator.
29061** This structure is the current state of the generator.
29062*/
29063static SQLITE_WSD struct sqlite3PrngType {
29064 unsigned char isInit; /* True if initialized */
29065 unsigned char i, j; /* State variables */
29066 unsigned char s[256]; /* State variables */
29067} sqlite3Prng;
29068
29069/*
29070** Return N random bytes.
29071*/
29072SQLITE_API void sqlite3_randomness(int N, void *pBuf){
29073 unsigned char t;
29074 unsigned char *zBuf = pBuf;
29075
29076 /* The "wsdPrng" macro will resolve to the pseudo-random number generator
29077 ** state vector. If writable static data is unsupported on the target,
29078 ** we have to locate the state vector at run-time. In the more common
29079 ** case where writable static data is supported, wsdPrng can refer directly
29080 ** to the "sqlite3Prng" state vector declared above.
29081 */
29082#ifdef SQLITE_OMIT_WSD
29083 struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng);
29084# define wsdPrng p[0]
29085#else
29086# define wsdPrng sqlite3Prng
29087#endif
29088
29089#if SQLITE_THREADSAFE
29090 sqlite3_mutex *mutex;
29091#endif
29092
29093#ifndef SQLITE_OMIT_AUTOINIT
29094 if( sqlite3_initialize() ) return;
29095#endif
29096
29097#if SQLITE_THREADSAFE
29098 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
29099#endif
29100
29101 sqlite3_mutex_enter(mutex);
29102 if( N<=0 || pBuf==0 ){
29103 wsdPrng.isInit = 0;
29104 sqlite3_mutex_leave(mutex);
29105 return;
29106 }
29107
29108 /* Initialize the state of the random number generator once,
29109 ** the first time this routine is called. The seed value does
29110 ** not need to contain a lot of randomness since we are not
29111 ** trying to do secure encryption or anything like that...
29112 **
29113 ** Nothing in this file or anywhere else in SQLite does any kind of
29114 ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random
29115 ** number generator) not as an encryption device.
29116 */
29117 if( !wsdPrng.isInit ){
29118 int i;
29119 char k[256];
29120 wsdPrng.j = 0;
29121 wsdPrng.i = 0;
29122 sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
29123 for(i=0; i<256; i++){
29124 wsdPrng.s[i] = (u8)i;
29125 }
29126 for(i=0; i<256; i++){
29127 wsdPrng.j += wsdPrng.s[i] + k[i];
29128 t = wsdPrng.s[wsdPrng.j];
29129 wsdPrng.s[wsdPrng.j] = wsdPrng.s[i];
29130 wsdPrng.s[i] = t;
29131 }
29132 wsdPrng.isInit = 1;
29133 }
29134
29135 assert( N>0 );
29136 do{
29137 wsdPrng.i++;
29138 t = wsdPrng.s[wsdPrng.i];
29139 wsdPrng.j += t;
29140 wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
29141 wsdPrng.s[wsdPrng.j] = t;
29142 t += wsdPrng.s[wsdPrng.i];
29143 *(zBuf++) = wsdPrng.s[t];
29144 }while( --N );
29145 sqlite3_mutex_leave(mutex);
29146}
29147
29148#ifndef SQLITE_UNTESTABLE
29149/*
29150** For testing purposes, we sometimes want to preserve the state of
29151** PRNG and restore the PRNG to its saved state at a later time, or
29152** to reset the PRNG to its initial state. These routines accomplish
29153** those tasks.
29154**
29155** The sqlite3_test_control() interface calls these routines to
29156** control the PRNG.
29157*/
29158static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
29159SQLITE_PRIVATE void sqlite3PrngSaveState(void){
29160 memcpy(
29161 &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
29162 &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
29163 sizeof(sqlite3Prng)
29164 );
29165}
29166SQLITE_PRIVATE void sqlite3PrngRestoreState(void){
29167 memcpy(
29168 &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
29169 &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
29170 sizeof(sqlite3Prng)
29171 );
29172}
29173#endif /* SQLITE_UNTESTABLE */
29174
29175/************** End of random.c **********************************************/
29176/************** Begin file threads.c *****************************************/
29177/*
29178** 2012 July 21
29179**
29180** The author disclaims copyright to this source code. In place of
29181** a legal notice, here is a blessing:
29182**
29183** May you do good and not evil.
29184** May you find forgiveness for yourself and forgive others.
29185** May you share freely, never taking more than you give.
29186**
29187******************************************************************************
29188**
29189** This file presents a simple cross-platform threading interface for
29190** use internally by SQLite.
29191**
29192** A "thread" can be created using sqlite3ThreadCreate(). This thread
29193** runs independently of its creator until it is joined using
29194** sqlite3ThreadJoin(), at which point it terminates.
29195**
29196** Threads do not have to be real. It could be that the work of the
29197** "thread" is done by the main thread at either the sqlite3ThreadCreate()
29198** or sqlite3ThreadJoin() call. This is, in fact, what happens in
29199** single threaded systems. Nothing in SQLite requires multiple threads.
29200** This interface exists so that applications that want to take advantage
29201** of multiple cores can do so, while also allowing applications to stay
29202** single-threaded if desired.
29203*/
29204/* #include "sqliteInt.h" */
29205#if SQLITE_OS_WIN
29206/* # include "os_win.h" */
29207#endif
29208
29209#if SQLITE_MAX_WORKER_THREADS>0
29210
29211/********************************* Unix Pthreads ****************************/
29212#if SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) && SQLITE_THREADSAFE>0
29213
29214#define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
29215/* #include <pthread.h> */
29216
29217/* A running thread */
29218struct SQLiteThread {
29219 pthread_t tid; /* Thread ID */
29220 int done; /* Set to true when thread finishes */
29221 void *pOut; /* Result returned by the thread */
29222 void *(*xTask)(void*); /* The thread routine */
29223 void *pIn; /* Argument to the thread */
29224};
29225
29226/* Create a new thread */
29227SQLITE_PRIVATE int sqlite3ThreadCreate(
29228 SQLiteThread **ppThread, /* OUT: Write the thread object here */
29229 void *(*xTask)(void*), /* Routine to run in a separate thread */
29230 void *pIn /* Argument passed into xTask() */
29231){
29232 SQLiteThread *p;
29233 int rc;
29234
29235 assert( ppThread!=0 );
29236 assert( xTask!=0 );
29237 /* This routine is never used in single-threaded mode */
29238 assert( sqlite3GlobalConfig.bCoreMutex!=0 );
29239
29240 *ppThread = 0;
29241 p = sqlite3Malloc(sizeof(*p));
29242 if( p==0 ) return SQLITE_NOMEM_BKPT;
29243 memset(p, 0, sizeof(*p));
29244 p->xTask = xTask;
29245 p->pIn = pIn;
29246 /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
29247 ** function that returns SQLITE_ERROR when passed the argument 200, that
29248 ** forces worker threads to run sequentially and deterministically
29249 ** for testing purposes. */
29250 if( sqlite3FaultSim(200) ){
29251 rc = 1;
29252 }else{
29253 rc = pthread_create(&p->tid, 0, xTask, pIn);
29254 }
29255 if( rc ){
29256 p->done = 1;
29257 p->pOut = xTask(pIn);
29258 }
29259 *ppThread = p;
29260 return SQLITE_OK;
29261}
29262
29263/* Get the results of the thread */
29264SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
29265 int rc;
29266
29267 assert( ppOut!=0 );
29268 if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
29269 if( p->done ){
29270 *ppOut = p->pOut;
29271 rc = SQLITE_OK;
29272 }else{
29273 rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR : SQLITE_OK;
29274 }
29275 sqlite3_free(p);
29276 return rc;
29277}
29278
29279#endif /* SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) */
29280/******************************** End Unix Pthreads *************************/
29281
29282
29283/********************************* Win32 Threads ****************************/
29284#if SQLITE_OS_WIN_THREADS
29285
29286#define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
29287#include <process.h>
29288
29289/* A running thread */
29290struct SQLiteThread {
29291 void *tid; /* The thread handle */
29292 unsigned id; /* The thread identifier */
29293 void *(*xTask)(void*); /* The routine to run as a thread */
29294 void *pIn; /* Argument to xTask */
29295 void *pResult; /* Result of xTask */
29296};
29297
29298/* Thread procedure Win32 compatibility shim */
29299static unsigned __stdcall sqlite3ThreadProc(
29300 void *pArg /* IN: Pointer to the SQLiteThread structure */
29301){
29302 SQLiteThread *p = (SQLiteThread *)pArg;
29303
29304 assert( p!=0 );
29305#if 0
29306 /*
29307 ** This assert appears to trigger spuriously on certain
29308 ** versions of Windows, possibly due to _beginthreadex()
29309 ** and/or CreateThread() not fully setting their thread
29310 ** ID parameter before starting the thread.
29311 */
29312 assert( p->id==GetCurrentThreadId() );
29313#endif
29314 assert( p->xTask!=0 );
29315 p->pResult = p->xTask(p->pIn);
29316
29317 _endthreadex(0);
29318 return 0; /* NOT REACHED */
29319}
29320
29321/* Create a new thread */
29322SQLITE_PRIVATE int sqlite3ThreadCreate(
29323 SQLiteThread **ppThread, /* OUT: Write the thread object here */
29324 void *(*xTask)(void*), /* Routine to run in a separate thread */
29325 void *pIn /* Argument passed into xTask() */
29326){
29327 SQLiteThread *p;
29328
29329 assert( ppThread!=0 );
29330 assert( xTask!=0 );
29331 *ppThread = 0;
29332 p = sqlite3Malloc(sizeof(*p));
29333 if( p==0 ) return SQLITE_NOMEM_BKPT;
29334 /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
29335 ** function that returns SQLITE_ERROR when passed the argument 200, that
29336 ** forces worker threads to run sequentially and deterministically
29337 ** (via the sqlite3FaultSim() term of the conditional) for testing
29338 ** purposes. */
29339 if( sqlite3GlobalConfig.bCoreMutex==0 || sqlite3FaultSim(200) ){
29340 memset(p, 0, sizeof(*p));
29341 }else{
29342 p->xTask = xTask;
29343 p->pIn = pIn;
29344 p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id);
29345 if( p->tid==0 ){
29346 memset(p, 0, sizeof(*p));
29347 }
29348 }
29349 if( p->xTask==0 ){
29350 p->id = GetCurrentThreadId();
29351 p->pResult = xTask(pIn);
29352 }
29353 *ppThread = p;
29354 return SQLITE_OK;
29355}
29356
29357SQLITE_PRIVATE DWORD sqlite3Win32Wait(HANDLE hObject); /* os_win.c */
29358
29359/* Get the results of the thread */
29360SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
29361 DWORD rc;
29362 BOOL bRc;
29363
29364 assert( ppOut!=0 );
29365 if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
29366 if( p->xTask==0 ){
29367 /* assert( p->id==GetCurrentThreadId() ); */
29368 rc = WAIT_OBJECT_0;
29369 assert( p->tid==0 );
29370 }else{
29371 assert( p->id!=0 && p->id!=GetCurrentThreadId() );
29372 rc = sqlite3Win32Wait((HANDLE)p->tid);
29373 assert( rc!=WAIT_IO_COMPLETION );
29374 bRc = CloseHandle((HANDLE)p->tid);
29375 assert( bRc );
29376 }
29377 if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult;
29378 sqlite3_free(p);
29379 return (rc==WAIT_OBJECT_0) ? SQLITE_OK : SQLITE_ERROR;
29380}
29381
29382#endif /* SQLITE_OS_WIN_THREADS */
29383/******************************** End Win32 Threads *************************/
29384
29385
29386/********************************* Single-Threaded **************************/
29387#ifndef SQLITE_THREADS_IMPLEMENTED
29388/*
29389** This implementation does not actually create a new thread. It does the
29390** work of the thread in the main thread, when either the thread is created
29391** or when it is joined
29392*/
29393
29394/* A running thread */
29395struct SQLiteThread {
29396 void *(*xTask)(void*); /* The routine to run as a thread */
29397 void *pIn; /* Argument to xTask */
29398 void *pResult; /* Result of xTask */
29399};
29400
29401/* Create a new thread */
29402SQLITE_PRIVATE int sqlite3ThreadCreate(
29403 SQLiteThread **ppThread, /* OUT: Write the thread object here */
29404 void *(*xTask)(void*), /* Routine to run in a separate thread */
29405 void *pIn /* Argument passed into xTask() */
29406){
29407 SQLiteThread *p;
29408
29409 assert( ppThread!=0 );
29410 assert( xTask!=0 );
29411 *ppThread = 0;
29412 p = sqlite3Malloc(sizeof(*p));
29413 if( p==0 ) return SQLITE_NOMEM_BKPT;
29414 if( (SQLITE_PTR_TO_INT(p)/17)&1 ){
29415 p->xTask = xTask;
29416 p->pIn = pIn;
29417 }else{
29418 p->xTask = 0;
29419 p->pResult = xTask(pIn);
29420 }
29421 *ppThread = p;
29422 return SQLITE_OK;
29423}
29424
29425/* Get the results of the thread */
29426SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
29427
29428 assert( ppOut!=0 );
29429 if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
29430 if( p->xTask ){
29431 *ppOut = p->xTask(p->pIn);
29432 }else{
29433 *ppOut = p->pResult;
29434 }
29435 sqlite3_free(p);
29436
29437#if defined(SQLITE_TEST)
29438 {
29439 void *pTstAlloc = sqlite3Malloc(10);
29440 if (!pTstAlloc) return SQLITE_NOMEM_BKPT;
29441 sqlite3_free(pTstAlloc);
29442 }
29443#endif
29444
29445 return SQLITE_OK;
29446}
29447
29448#endif /* !defined(SQLITE_THREADS_IMPLEMENTED) */
29449/****************************** End Single-Threaded *************************/
29450#endif /* SQLITE_MAX_WORKER_THREADS>0 */
29451
29452/************** End of threads.c *********************************************/
29453/************** Begin file utf.c *********************************************/
29454/*
29455** 2004 April 13
29456**
29457** The author disclaims copyright to this source code. In place of
29458** a legal notice, here is a blessing:
29459**
29460** May you do good and not evil.
29461** May you find forgiveness for yourself and forgive others.
29462** May you share freely, never taking more than you give.
29463**
29464*************************************************************************
29465** This file contains routines used to translate between UTF-8,
29466** UTF-16, UTF-16BE, and UTF-16LE.
29467**
29468** Notes on UTF-8:
29469**
29470** Byte-0 Byte-1 Byte-2 Byte-3 Value
29471** 0xxxxxxx 00000000 00000000 0xxxxxxx
29472** 110yyyyy 10xxxxxx 00000000 00000yyy yyxxxxxx
29473** 1110zzzz 10yyyyyy 10xxxxxx 00000000 zzzzyyyy yyxxxxxx
29474** 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx 000uuuuu zzzzyyyy yyxxxxxx
29475**
29476**
29477** Notes on UTF-16: (with wwww+1==uuuuu)
29478**
29479** Word-0 Word-1 Value
29480** 110110ww wwzzzzyy 110111yy yyxxxxxx 000uuuuu zzzzyyyy yyxxxxxx
29481** zzzzyyyy yyxxxxxx 00000000 zzzzyyyy yyxxxxxx
29482**
29483**
29484** BOM or Byte Order Mark:
29485** 0xff 0xfe little-endian utf-16 follows
29486** 0xfe 0xff big-endian utf-16 follows
29487**
29488*/
29489/* #include "sqliteInt.h" */
29490/* #include <assert.h> */
29491/* #include "vdbeInt.h" */
29492
29493#if !defined(SQLITE_AMALGAMATION) && SQLITE_BYTEORDER==0
29494/*
29495** The following constant value is used by the SQLITE_BIGENDIAN and
29496** SQLITE_LITTLEENDIAN macros.
29497*/
29498SQLITE_PRIVATE const int sqlite3one = 1;
29499#endif /* SQLITE_AMALGAMATION && SQLITE_BYTEORDER==0 */
29500
29501/*
29502** This lookup table is used to help decode the first byte of
29503** a multi-byte UTF8 character.
29504*/
29505static const unsigned char sqlite3Utf8Trans1[] = {
29506 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
29507 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
29508 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
29509 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
29510 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
29511 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
29512 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
29513 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
29514};
29515
29516
29517#define WRITE_UTF8(zOut, c) { \
29518 if( c<0x00080 ){ \
29519 *zOut++ = (u8)(c&0xFF); \
29520 } \
29521 else if( c<0x00800 ){ \
29522 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \
29523 *zOut++ = 0x80 + (u8)(c & 0x3F); \
29524 } \
29525 else if( c<0x10000 ){ \
29526 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \
29527 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
29528 *zOut++ = 0x80 + (u8)(c & 0x3F); \
29529 }else{ \
29530 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); \
29531 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); \
29532 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
29533 *zOut++ = 0x80 + (u8)(c & 0x3F); \
29534 } \
29535}
29536
29537#define WRITE_UTF16LE(zOut, c) { \
29538 if( c<=0xFFFF ){ \
29539 *zOut++ = (u8)(c&0x00FF); \
29540 *zOut++ = (u8)((c>>8)&0x00FF); \
29541 }else{ \
29542 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
29543 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
29544 *zOut++ = (u8)(c&0x00FF); \
29545 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \
29546 } \
29547}
29548
29549#define WRITE_UTF16BE(zOut, c) { \
29550 if( c<=0xFFFF ){ \
29551 *zOut++ = (u8)((c>>8)&0x00FF); \
29552 *zOut++ = (u8)(c&0x00FF); \
29553 }else{ \
29554 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
29555 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
29556 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \
29557 *zOut++ = (u8)(c&0x00FF); \
29558 } \
29559}
29560
29561#define READ_UTF16LE(zIn, TERM, c){ \
29562 c = (*zIn++); \
29563 c += ((*zIn++)<<8); \
29564 if( c>=0xD800 && c<0xE000 && TERM ){ \
29565 int c2 = (*zIn++); \
29566 c2 += ((*zIn++)<<8); \
29567 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); \
29568 } \
29569}
29570
29571#define READ_UTF16BE(zIn, TERM, c){ \
29572 c = ((*zIn++)<<8); \
29573 c += (*zIn++); \
29574 if( c>=0xD800 && c<0xE000 && TERM ){ \
29575 int c2 = ((*zIn++)<<8); \
29576 c2 += (*zIn++); \
29577 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); \
29578 } \
29579}
29580
29581/*
29582** Translate a single UTF-8 character. Return the unicode value.
29583**
29584** During translation, assume that the byte that zTerm points
29585** is a 0x00.
29586**
29587** Write a pointer to the next unread byte back into *pzNext.
29588**
29589** Notes On Invalid UTF-8:
29590**
29591** * This routine never allows a 7-bit character (0x00 through 0x7f) to
29592** be encoded as a multi-byte character. Any multi-byte character that
29593** attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
29594**
29595** * This routine never allows a UTF16 surrogate value to be encoded.
29596** If a multi-byte character attempts to encode a value between
29597** 0xd800 and 0xe000 then it is rendered as 0xfffd.
29598**
29599** * Bytes in the range of 0x80 through 0xbf which occur as the first
29600** byte of a character are interpreted as single-byte characters
29601** and rendered as themselves even though they are technically
29602** invalid characters.
29603**
29604** * This routine accepts over-length UTF8 encodings
29605** for unicode values 0x80 and greater. It does not change over-length
29606** encodings to 0xfffd as some systems recommend.
29607*/
29608#define READ_UTF8(zIn, zTerm, c) \
29609 c = *(zIn++); \
29610 if( c>=0xc0 ){ \
29611 c = sqlite3Utf8Trans1[c-0xc0]; \
29612 while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \
29613 c = (c<<6) + (0x3f & *(zIn++)); \
29614 } \
29615 if( c<0x80 \
29616 || (c&0xFFFFF800)==0xD800 \
29617 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
29618 }
29619SQLITE_PRIVATE u32 sqlite3Utf8Read(
29620 const unsigned char **pz /* Pointer to string from which to read char */
29621){
29622 unsigned int c;
29623
29624 /* Same as READ_UTF8() above but without the zTerm parameter.
29625 ** For this routine, we assume the UTF8 string is always zero-terminated.
29626 */
29627 c = *((*pz)++);
29628 if( c>=0xc0 ){
29629 c = sqlite3Utf8Trans1[c-0xc0];
29630 while( (*(*pz) & 0xc0)==0x80 ){
29631 c = (c<<6) + (0x3f & *((*pz)++));
29632 }
29633 if( c<0x80
29634 || (c&0xFFFFF800)==0xD800
29635 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; }
29636 }
29637 return c;
29638}
29639
29640
29641
29642
29643/*
29644** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
29645** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
29646*/
29647/* #define TRANSLATE_TRACE 1 */
29648
29649#ifndef SQLITE_OMIT_UTF16
29650/*
29651** This routine transforms the internal text encoding used by pMem to
29652** desiredEnc. It is an error if the string is already of the desired
29653** encoding, or if *pMem does not contain a string value.
29654*/
29655SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
29656 int len; /* Maximum length of output string in bytes */
29657 unsigned char *zOut; /* Output buffer */
29658 unsigned char *zIn; /* Input iterator */
29659 unsigned char *zTerm; /* End of input */
29660 unsigned char *z; /* Output iterator */
29661 unsigned int c;
29662
29663 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
29664 assert( pMem->flags&MEM_Str );
29665 assert( pMem->enc!=desiredEnc );
29666 assert( pMem->enc!=0 );
29667 assert( pMem->n>=0 );
29668
29669#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
29670 {
29671 char zBuf[100];
29672 sqlite3VdbeMemPrettyPrint(pMem, zBuf);
29673 fprintf(stderr, "INPUT: %s\n", zBuf);
29674 }
29675#endif
29676
29677 /* If the translation is between UTF-16 little and big endian, then
29678 ** all that is required is to swap the byte order. This case is handled
29679 ** differently from the others.
29680 */
29681 if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
29682 u8 temp;
29683 int rc;
29684 rc = sqlite3VdbeMemMakeWriteable(pMem);
29685 if( rc!=SQLITE_OK ){
29686 assert( rc==SQLITE_NOMEM );
29687 return SQLITE_NOMEM_BKPT;
29688 }
29689 zIn = (u8*)pMem->z;
29690 zTerm = &zIn[pMem->n&~1];
29691 while( zIn<zTerm ){
29692 temp = *zIn;
29693 *zIn = *(zIn+1);
29694 zIn++;
29695 *zIn++ = temp;
29696 }
29697 pMem->enc = desiredEnc;
29698 goto translate_out;
29699 }
29700
29701 /* Set len to the maximum number of bytes required in the output buffer. */
29702 if( desiredEnc==SQLITE_UTF8 ){
29703 /* When converting from UTF-16, the maximum growth results from
29704 ** translating a 2-byte character to a 4-byte UTF-8 character.
29705 ** A single byte is required for the output string
29706 ** nul-terminator.
29707 */
29708 pMem->n &= ~1;
29709 len = pMem->n * 2 + 1;
29710 }else{
29711 /* When converting from UTF-8 to UTF-16 the maximum growth is caused
29712 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
29713 ** character. Two bytes are required in the output buffer for the
29714 ** nul-terminator.
29715 */
29716 len = pMem->n * 2 + 2;
29717 }
29718
29719 /* Set zIn to point at the start of the input buffer and zTerm to point 1
29720 ** byte past the end.
29721 **
29722 ** Variable zOut is set to point at the output buffer, space obtained
29723 ** from sqlite3_malloc().
29724 */
29725 zIn = (u8*)pMem->z;
29726 zTerm = &zIn[pMem->n];
29727 zOut = sqlite3DbMallocRaw(pMem->db, len);
29728 if( !zOut ){
29729 return SQLITE_NOMEM_BKPT;
29730 }
29731 z = zOut;
29732
29733 if( pMem->enc==SQLITE_UTF8 ){
29734 if( desiredEnc==SQLITE_UTF16LE ){
29735 /* UTF-8 -> UTF-16 Little-endian */
29736 while( zIn<zTerm ){
29737 READ_UTF8(zIn, zTerm, c);
29738 WRITE_UTF16LE(z, c);
29739 }
29740 }else{
29741 assert( desiredEnc==SQLITE_UTF16BE );
29742 /* UTF-8 -> UTF-16 Big-endian */
29743 while( zIn<zTerm ){
29744 READ_UTF8(zIn, zTerm, c);
29745 WRITE_UTF16BE(z, c);
29746 }
29747 }
29748 pMem->n = (int)(z - zOut);
29749 *z++ = 0;
29750 }else{
29751 assert( desiredEnc==SQLITE_UTF8 );
29752 if( pMem->enc==SQLITE_UTF16LE ){
29753 /* UTF-16 Little-endian -> UTF-8 */
29754 while( zIn<zTerm ){
29755 READ_UTF16LE(zIn, zIn<zTerm, c);
29756 WRITE_UTF8(z, c);
29757 }
29758 }else{
29759 /* UTF-16 Big-endian -> UTF-8 */
29760 while( zIn<zTerm ){
29761 READ_UTF16BE(zIn, zIn<zTerm, c);
29762 WRITE_UTF8(z, c);
29763 }
29764 }
29765 pMem->n = (int)(z - zOut);
29766 }
29767 *z = 0;
29768 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
29769
29770 c = pMem->flags;
29771 sqlite3VdbeMemRelease(pMem);
29772 pMem->flags = MEM_Str|MEM_Term|(c&(MEM_AffMask|MEM_Subtype));
29773 pMem->enc = desiredEnc;
29774 pMem->z = (char*)zOut;
29775 pMem->zMalloc = pMem->z;
29776 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z);
29777
29778translate_out:
29779#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
29780 {
29781 char zBuf[100];
29782 sqlite3VdbeMemPrettyPrint(pMem, zBuf);
29783 fprintf(stderr, "OUTPUT: %s\n", zBuf);
29784 }
29785#endif
29786 return SQLITE_OK;
29787}
29788#endif /* SQLITE_OMIT_UTF16 */
29789
29790#ifndef SQLITE_OMIT_UTF16
29791/*
29792** This routine checks for a byte-order mark at the beginning of the
29793** UTF-16 string stored in *pMem. If one is present, it is removed and
29794** the encoding of the Mem adjusted. This routine does not do any
29795** byte-swapping, it just sets Mem.enc appropriately.
29796**
29797** The allocation (static, dynamic etc.) and encoding of the Mem may be
29798** changed by this function.
29799*/
29800SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem){
29801 int rc = SQLITE_OK;
29802 u8 bom = 0;
29803
29804 assert( pMem->n>=0 );
29805 if( pMem->n>1 ){
29806 u8 b1 = *(u8 *)pMem->z;
29807 u8 b2 = *(((u8 *)pMem->z) + 1);
29808 if( b1==0xFE && b2==0xFF ){
29809 bom = SQLITE_UTF16BE;
29810 }
29811 if( b1==0xFF && b2==0xFE ){
29812 bom = SQLITE_UTF16LE;
29813 }
29814 }
29815
29816 if( bom ){
29817 rc = sqlite3VdbeMemMakeWriteable(pMem);
29818 if( rc==SQLITE_OK ){
29819 pMem->n -= 2;
29820 memmove(pMem->z, &pMem->z[2], pMem->n);
29821 pMem->z[pMem->n] = '\0';
29822 pMem->z[pMem->n+1] = '\0';
29823 pMem->flags |= MEM_Term;
29824 pMem->enc = bom;
29825 }
29826 }
29827 return rc;
29828}
29829#endif /* SQLITE_OMIT_UTF16 */
29830
29831/*
29832** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
29833** return the number of unicode characters in pZ up to (but not including)
29834** the first 0x00 byte. If nByte is not less than zero, return the
29835** number of unicode characters in the first nByte of pZ (or up to
29836** the first 0x00, whichever comes first).
29837*/
29838SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *zIn, int nByte){
29839 int r = 0;
29840 const u8 *z = (const u8*)zIn;
29841 const u8 *zTerm;
29842 if( nByte>=0 ){
29843 zTerm = &z[nByte];
29844 }else{
29845 zTerm = (const u8*)(-1);
29846 }
29847 assert( z<=zTerm );
29848 while( *z!=0 && z<zTerm ){
29849 SQLITE_SKIP_UTF8(z);
29850 r++;
29851 }
29852 return r;
29853}
29854
29855/* This test function is not currently used by the automated test-suite.
29856** Hence it is only available in debug builds.
29857*/
29858#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
29859/*
29860** Translate UTF-8 to UTF-8.
29861**
29862** This has the effect of making sure that the string is well-formed
29863** UTF-8. Miscoded characters are removed.
29864**
29865** The translation is done in-place and aborted if the output
29866** overruns the input.
29867*/
29868SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){
29869 unsigned char *zOut = zIn;
29870 unsigned char *zStart = zIn;
29871 u32 c;
29872
29873 while( zIn[0] && zOut<=zIn ){
29874 c = sqlite3Utf8Read((const u8**)&zIn);
29875 if( c!=0xfffd ){
29876 WRITE_UTF8(zOut, c);
29877 }
29878 }
29879 *zOut = 0;
29880 return (int)(zOut - zStart);
29881}
29882#endif
29883
29884#ifndef SQLITE_OMIT_UTF16
29885/*
29886** Convert a UTF-16 string in the native encoding into a UTF-8 string.
29887** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
29888** be freed by the calling function.
29889**
29890** NULL is returned if there is an allocation error.
29891*/
29892SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){
29893 Mem m;
29894 memset(&m, 0, sizeof(m));
29895 m.db = db;
29896 sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC);
29897 sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
29898 if( db->mallocFailed ){
29899 sqlite3VdbeMemRelease(&m);
29900 m.z = 0;
29901 }
29902 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
29903 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
29904 assert( m.z || db->mallocFailed );
29905 return m.z;
29906}
29907
29908/*
29909** zIn is a UTF-16 encoded unicode string at least nChar characters long.
29910** Return the number of bytes in the first nChar unicode characters
29911** in pZ. nChar must be non-negative.
29912*/
29913SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *zIn, int nChar){
29914 int c;
29915 unsigned char const *z = zIn;
29916 int n = 0;
29917
29918 if( SQLITE_UTF16NATIVE==SQLITE_UTF16BE ){
29919 while( n<nChar ){
29920 READ_UTF16BE(z, 1, c);
29921 n++;
29922 }
29923 }else{
29924 while( n<nChar ){
29925 READ_UTF16LE(z, 1, c);
29926 n++;
29927 }
29928 }
29929 return (int)(z-(unsigned char const *)zIn);
29930}
29931
29932#if defined(SQLITE_TEST)
29933/*
29934** This routine is called from the TCL test function "translate_selftest".
29935** It checks that the primitives for serializing and deserializing
29936** characters in each encoding are inverses of each other.
29937*/
29938SQLITE_PRIVATE void sqlite3UtfSelfTest(void){
29939 unsigned int i, t;
29940 unsigned char zBuf[20];
29941 unsigned char *z;
29942 int n;
29943 unsigned int c;
29944
29945 for(i=0; i<0x00110000; i++){
29946 z = zBuf;
29947 WRITE_UTF8(z, i);
29948 n = (int)(z-zBuf);
29949 assert( n>0 && n<=4 );
29950 z[0] = 0;
29951 z = zBuf;
29952 c = sqlite3Utf8Read((const u8**)&z);
29953 t = i;
29954 if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
29955 if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
29956 assert( c==t );
29957 assert( (z-zBuf)==n );
29958 }
29959 for(i=0; i<0x00110000; i++){
29960 if( i>=0xD800 && i<0xE000 ) continue;
29961 z = zBuf;
29962 WRITE_UTF16LE(z, i);
29963 n = (int)(z-zBuf);
29964 assert( n>0 && n<=4 );
29965 z[0] = 0;
29966 z = zBuf;
29967 READ_UTF16LE(z, 1, c);
29968 assert( c==i );
29969 assert( (z-zBuf)==n );
29970 }
29971 for(i=0; i<0x00110000; i++){
29972 if( i>=0xD800 && i<0xE000 ) continue;
29973 z = zBuf;
29974 WRITE_UTF16BE(z, i);
29975 n = (int)(z-zBuf);
29976 assert( n>0 && n<=4 );
29977 z[0] = 0;
29978 z = zBuf;
29979 READ_UTF16BE(z, 1, c);
29980 assert( c==i );
29981 assert( (z-zBuf)==n );
29982 }
29983}
29984#endif /* SQLITE_TEST */
29985#endif /* SQLITE_OMIT_UTF16 */
29986
29987/************** End of utf.c *************************************************/
29988/************** Begin file util.c ********************************************/
29989/*
29990** 2001 September 15
29991**
29992** The author disclaims copyright to this source code. In place of
29993** a legal notice, here is a blessing:
29994**
29995** May you do good and not evil.
29996** May you find forgiveness for yourself and forgive others.
29997** May you share freely, never taking more than you give.
29998**
29999*************************************************************************
30000** Utility functions used throughout sqlite.
30001**
30002** This file contains functions for allocating memory, comparing
30003** strings, and stuff like that.
30004**
30005*/
30006/* #include "sqliteInt.h" */
30007/* #include <stdarg.h> */
30008#if HAVE_ISNAN || SQLITE_HAVE_ISNAN
30009# include <math.h>
30010#endif
30011
30012/*
30013** Routine needed to support the testcase() macro.
30014*/
30015#ifdef SQLITE_COVERAGE_TEST
30016SQLITE_PRIVATE void sqlite3Coverage(int x){
30017 static unsigned dummy = 0;
30018 dummy += (unsigned)x;
30019}
30020#endif
30021
30022/*
30023** Give a callback to the test harness that can be used to simulate faults
30024** in places where it is difficult or expensive to do so purely by means
30025** of inputs.
30026**
30027** The intent of the integer argument is to let the fault simulator know
30028** which of multiple sqlite3FaultSim() calls has been hit.
30029**
30030** Return whatever integer value the test callback returns, or return
30031** SQLITE_OK if no test callback is installed.
30032*/
30033#ifndef SQLITE_UNTESTABLE
30034SQLITE_PRIVATE int sqlite3FaultSim(int iTest){
30035 int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback;
30036 return xCallback ? xCallback(iTest) : SQLITE_OK;
30037}
30038#endif
30039
30040#ifndef SQLITE_OMIT_FLOATING_POINT
30041/*
30042** Return true if the floating point value is Not a Number (NaN).
30043**
30044** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
30045** Otherwise, we have our own implementation that works on most systems.
30046*/
30047SQLITE_PRIVATE int sqlite3IsNaN(double x){
30048 int rc; /* The value return */
30049#if !SQLITE_HAVE_ISNAN && !HAVE_ISNAN
30050 /*
30051 ** Systems that support the isnan() library function should probably
30052 ** make use of it by compiling with -DSQLITE_HAVE_ISNAN. But we have
30053 ** found that many systems do not have a working isnan() function so
30054 ** this implementation is provided as an alternative.
30055 **
30056 ** This NaN test sometimes fails if compiled on GCC with -ffast-math.
30057 ** On the other hand, the use of -ffast-math comes with the following
30058 ** warning:
30059 **
30060 ** This option [-ffast-math] should never be turned on by any
30061 ** -O option since it can result in incorrect output for programs
30062 ** which depend on an exact implementation of IEEE or ISO
30063 ** rules/specifications for math functions.
30064 **
30065 ** Under MSVC, this NaN test may fail if compiled with a floating-
30066 ** point precision mode other than /fp:precise. From the MSDN
30067 ** documentation:
30068 **
30069 ** The compiler [with /fp:precise] will properly handle comparisons
30070 ** involving NaN. For example, x != x evaluates to true if x is NaN
30071 ** ...
30072 */
30073#ifdef __FAST_MATH__
30074# error SQLite will not work correctly with the -ffast-math option of GCC.
30075#endif
30076 volatile double y = x;
30077 volatile double z = y;
30078 rc = (y!=z);
30079#else /* if HAVE_ISNAN */
30080 rc = isnan(x);
30081#endif /* HAVE_ISNAN */
30082 testcase( rc );
30083 return rc;
30084}
30085#endif /* SQLITE_OMIT_FLOATING_POINT */
30086
30087/*
30088** Compute a string length that is limited to what can be stored in
30089** lower 30 bits of a 32-bit signed integer.
30090**
30091** The value returned will never be negative. Nor will it ever be greater
30092** than the actual length of the string. For very long strings (greater
30093** than 1GiB) the value returned might be less than the true string length.
30094*/
30095SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
30096 if( z==0 ) return 0;
30097 return 0x3fffffff & (int)strlen(z);
30098}
30099
30100/*
30101** Return the declared type of a column. Or return zDflt if the column
30102** has no declared type.
30103**
30104** The column type is an extra string stored after the zero-terminator on
30105** the column name if and only if the COLFLAG_HASTYPE flag is set.
30106*/
30107SQLITE_PRIVATE char *sqlite3ColumnType(Column *pCol, char *zDflt){
30108 if( (pCol->colFlags & COLFLAG_HASTYPE)==0 ) return zDflt;
30109 return pCol->zName + strlen(pCol->zName) + 1;
30110}
30111
30112/*
30113** Helper function for sqlite3Error() - called rarely. Broken out into
30114** a separate routine to avoid unnecessary register saves on entry to
30115** sqlite3Error().
30116*/
30117static SQLITE_NOINLINE void sqlite3ErrorFinish(sqlite3 *db, int err_code){
30118 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
30119 sqlite3SystemError(db, err_code);
30120}
30121
30122/*
30123** Set the current error code to err_code and clear any prior error message.
30124** Also set iSysErrno (by calling sqlite3System) if the err_code indicates
30125** that would be appropriate.
30126*/
30127SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code){
30128 assert( db!=0 );
30129 db->errCode = err_code;
30130 if( err_code || db->pErr ) sqlite3ErrorFinish(db, err_code);
30131}
30132
30133/*
30134** Load the sqlite3.iSysErrno field if that is an appropriate thing
30135** to do based on the SQLite error code in rc.
30136*/
30137SQLITE_PRIVATE void sqlite3SystemError(sqlite3 *db, int rc){
30138 if( rc==SQLITE_IOERR_NOMEM ) return;
30139 rc &= 0xff;
30140 if( rc==SQLITE_CANTOPEN || rc==SQLITE_IOERR ){
30141 db->iSysErrno = sqlite3OsGetLastError(db->pVfs);
30142 }
30143}
30144
30145/*
30146** Set the most recent error code and error string for the sqlite
30147** handle "db". The error code is set to "err_code".
30148**
30149** If it is not NULL, string zFormat specifies the format of the
30150** error string in the style of the printf functions: The following
30151** format characters are allowed:
30152**
30153** %s Insert a string
30154** %z A string that should be freed after use
30155** %d Insert an integer
30156** %T Insert a token
30157** %S Insert the first element of a SrcList
30158**
30159** zFormat and any string tokens that follow it are assumed to be
30160** encoded in UTF-8.
30161**
30162** To clear the most recent error for sqlite handle "db", sqlite3Error
30163** should be called with err_code set to SQLITE_OK and zFormat set
30164** to NULL.
30165*/
30166SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){
30167 assert( db!=0 );
30168 db->errCode = err_code;
30169 sqlite3SystemError(db, err_code);
30170 if( zFormat==0 ){
30171 sqlite3Error(db, err_code);
30172 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){
30173 char *z;
30174 va_list ap;
30175 va_start(ap, zFormat);
30176 z = sqlite3VMPrintf(db, zFormat, ap);
30177 va_end(ap);
30178 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
30179 }
30180}
30181
30182/*
30183** Add an error message to pParse->zErrMsg and increment pParse->nErr.
30184** The following formatting characters are allowed:
30185**
30186** %s Insert a string
30187** %z A string that should be freed after use
30188** %d Insert an integer
30189** %T Insert a token
30190** %S Insert the first element of a SrcList
30191**
30192** This function should be used to report any error that occurs while
30193** compiling an SQL statement (i.e. within sqlite3_prepare()). The
30194** last thing the sqlite3_prepare() function does is copy the error
30195** stored by this function into the database handle using sqlite3Error().
30196** Functions sqlite3Error() or sqlite3ErrorWithMsg() should be used
30197** during statement execution (sqlite3_step() etc.).
30198*/
30199SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
30200 char *zMsg;
30201 va_list ap;
30202 sqlite3 *db = pParse->db;
30203 va_start(ap, zFormat);
30204 zMsg = sqlite3VMPrintf(db, zFormat, ap);
30205 va_end(ap);
30206 if( db->suppressErr ){
30207 sqlite3DbFree(db, zMsg);
30208 }else{
30209 pParse->nErr++;
30210 sqlite3DbFree(db, pParse->zErrMsg);
30211 pParse->zErrMsg = zMsg;
30212 pParse->rc = SQLITE_ERROR;
30213 }
30214}
30215
30216/*
30217** Convert an SQL-style quoted string into a normal string by removing
30218** the quote characters. The conversion is done in-place. If the
30219** input does not begin with a quote character, then this routine
30220** is a no-op.
30221**
30222** The input string must be zero-terminated. A new zero-terminator
30223** is added to the dequoted string.
30224**
30225** The return value is -1 if no dequoting occurs or the length of the
30226** dequoted string, exclusive of the zero terminator, if dequoting does
30227** occur.
30228**
30229** 2002-Feb-14: This routine is extended to remove MS-Access style
30230** brackets from around identifiers. For example: "[a-b-c]" becomes
30231** "a-b-c".
30232*/
30233SQLITE_PRIVATE void sqlite3Dequote(char *z){
30234 char quote;
30235 int i, j;
30236 if( z==0 ) return;
30237 quote = z[0];
30238 if( !sqlite3Isquote(quote) ) return;
30239 if( quote=='[' ) quote = ']';
30240 for(i=1, j=0;; i++){
30241 assert( z[i] );
30242 if( z[i]==quote ){
30243 if( z[i+1]==quote ){
30244 z[j++] = quote;
30245 i++;
30246 }else{
30247 break;
30248 }
30249 }else{
30250 z[j++] = z[i];
30251 }
30252 }
30253 z[j] = 0;
30254}
30255
30256/*
30257** Generate a Token object from a string
30258*/
30259SQLITE_PRIVATE void sqlite3TokenInit(Token *p, char *z){
30260 p->z = z;
30261 p->n = sqlite3Strlen30(z);
30262}
30263
30264/* Convenient short-hand */
30265#define UpperToLower sqlite3UpperToLower
30266
30267/*
30268** Some systems have stricmp(). Others have strcasecmp(). Because
30269** there is no consistency, we will define our own.
30270**
30271** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
30272** sqlite3_strnicmp() APIs allow applications and extensions to compare
30273** the contents of two buffers containing UTF-8 strings in a
30274** case-independent fashion, using the same definition of "case
30275** independence" that SQLite uses internally when comparing identifiers.
30276*/
30277SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){
30278 if( zLeft==0 ){
30279 return zRight ? -1 : 0;
30280 }else if( zRight==0 ){
30281 return 1;
30282 }
30283 return sqlite3StrICmp(zLeft, zRight);
30284}
30285SQLITE_PRIVATE int sqlite3StrICmp(const char *zLeft, const char *zRight){
30286 unsigned char *a, *b;
30287 int c;
30288 a = (unsigned char *)zLeft;
30289 b = (unsigned char *)zRight;
30290 for(;;){
30291 c = (int)UpperToLower[*a] - (int)UpperToLower[*b];
30292 if( c || *a==0 ) break;
30293 a++;
30294 b++;
30295 }
30296 return c;
30297}
30298SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
30299 register unsigned char *a, *b;
30300 if( zLeft==0 ){
30301 return zRight ? -1 : 0;
30302 }else if( zRight==0 ){
30303 return 1;
30304 }
30305 a = (unsigned char *)zLeft;
30306 b = (unsigned char *)zRight;
30307 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
30308 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
30309}
30310
30311/*
30312** Compute 10 to the E-th power. Examples: E==1 results in 10.
30313** E==2 results in 100. E==50 results in 1.0e50.
30314**
30315** This routine only works for values of E between 1 and 341.
30316*/
30317static LONGDOUBLE_TYPE sqlite3Pow10(int E){
30318#if defined(_MSC_VER)
30319 static const LONGDOUBLE_TYPE x[] = {
30320 1.0e+001,
30321 1.0e+002,
30322 1.0e+004,
30323 1.0e+008,
30324 1.0e+016,
30325 1.0e+032,
30326 1.0e+064,
30327 1.0e+128,
30328 1.0e+256
30329 };
30330 LONGDOUBLE_TYPE r = 1.0;
30331 int i;
30332 assert( E>=0 && E<=307 );
30333 for(i=0; E!=0; i++, E >>=1){
30334 if( E & 1 ) r *= x[i];
30335 }
30336 return r;
30337#else
30338 LONGDOUBLE_TYPE x = 10.0;
30339 LONGDOUBLE_TYPE r = 1.0;
30340 while(1){
30341 if( E & 1 ) r *= x;
30342 E >>= 1;
30343 if( E==0 ) break;
30344 x *= x;
30345 }
30346 return r;
30347#endif
30348}
30349
30350/*
30351** The string z[] is an text representation of a real number.
30352** Convert this string to a double and write it into *pResult.
30353**
30354** The string z[] is length bytes in length (bytes, not characters) and
30355** uses the encoding enc. The string is not necessarily zero-terminated.
30356**
30357** Return TRUE if the result is a valid real number (or integer) and FALSE
30358** if the string is empty or contains extraneous text. Valid numbers
30359** are in one of these formats:
30360**
30361** [+-]digits[E[+-]digits]
30362** [+-]digits.[digits][E[+-]digits]
30363** [+-].digits[E[+-]digits]
30364**
30365** Leading and trailing whitespace is ignored for the purpose of determining
30366** validity.
30367**
30368** If some prefix of the input string is a valid number, this routine
30369** returns FALSE but it still converts the prefix and writes the result
30370** into *pResult.
30371*/
30372SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
30373#ifndef SQLITE_OMIT_FLOATING_POINT
30374 int incr;
30375 const char *zEnd = z + length;
30376 /* sign * significand * (10 ^ (esign * exponent)) */
30377 int sign = 1; /* sign of significand */
30378 i64 s = 0; /* significand */
30379 int d = 0; /* adjust exponent for shifting decimal point */
30380 int esign = 1; /* sign of exponent */
30381 int e = 0; /* exponent */
30382 int eValid = 1; /* True exponent is either not used or is well-formed */
30383 double result;
30384 int nDigits = 0;
30385 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */
30386
30387 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
30388 *pResult = 0.0; /* Default return value, in case of an error */
30389
30390 if( enc==SQLITE_UTF8 ){
30391 incr = 1;
30392 }else{
30393 int i;
30394 incr = 2;
30395 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
30396 for(i=3-enc; i<length && z[i]==0; i+=2){}
30397 nonNum = i<length;
30398 zEnd = &z[i^1];
30399 z += (enc&1);
30400 }
30401
30402 /* skip leading spaces */
30403 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
30404 if( z>=zEnd ) return 0;
30405
30406 /* get sign of significand */
30407 if( *z=='-' ){
30408 sign = -1;
30409 z+=incr;
30410 }else if( *z=='+' ){
30411 z+=incr;
30412 }
30413
30414 /* copy max significant digits to significand */
30415 while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
30416 s = s*10 + (*z - '0');
30417 z+=incr; nDigits++;
30418 }
30419
30420 /* skip non-significant significand digits
30421 ** (increase exponent by d to shift decimal left) */
30422 while( z<zEnd && sqlite3Isdigit(*z) ){ z+=incr; nDigits++; d++; }
30423 if( z>=zEnd ) goto do_atof_calc;
30424
30425 /* if decimal point is present */
30426 if( *z=='.' ){
30427 z+=incr;
30428 /* copy digits from after decimal to significand
30429 ** (decrease exponent by d to shift decimal right) */
30430 while( z<zEnd && sqlite3Isdigit(*z) ){
30431 if( s<((LARGEST_INT64-9)/10) ){
30432 s = s*10 + (*z - '0');
30433 d--;
30434 }
30435 z+=incr; nDigits++;
30436 }
30437 }
30438 if( z>=zEnd ) goto do_atof_calc;
30439
30440 /* if exponent is present */
30441 if( *z=='e' || *z=='E' ){
30442 z+=incr;
30443 eValid = 0;
30444
30445 /* This branch is needed to avoid a (harmless) buffer overread. The
30446 ** special comment alerts the mutation tester that the correct answer
30447 ** is obtained even if the branch is omitted */
30448 if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/
30449
30450 /* get sign of exponent */
30451 if( *z=='-' ){
30452 esign = -1;
30453 z+=incr;
30454 }else if( *z=='+' ){
30455 z+=incr;
30456 }
30457 /* copy digits to exponent */
30458 while( z<zEnd && sqlite3Isdigit(*z) ){
30459 e = e<10000 ? (e*10 + (*z - '0')) : 10000;
30460 z+=incr;
30461 eValid = 1;
30462 }
30463 }
30464
30465 /* skip trailing spaces */
30466 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
30467
30468do_atof_calc:
30469 /* adjust exponent by d, and update sign */
30470 e = (e*esign) + d;
30471 if( e<0 ) {
30472 esign = -1;
30473 e *= -1;
30474 } else {
30475 esign = 1;
30476 }
30477
30478 if( s==0 ) {
30479 /* In the IEEE 754 standard, zero is signed. */
30480 result = sign<0 ? -(double)0 : (double)0;
30481 } else {
30482 /* Attempt to reduce exponent.
30483 **
30484 ** Branches that are not required for the correct answer but which only
30485 ** help to obtain the correct answer faster are marked with special
30486 ** comments, as a hint to the mutation tester.
30487 */
30488 while( e>0 ){ /*OPTIMIZATION-IF-TRUE*/
30489 if( esign>0 ){
30490 if( s>=(LARGEST_INT64/10) ) break; /*OPTIMIZATION-IF-FALSE*/
30491 s *= 10;
30492 }else{
30493 if( s%10!=0 ) break; /*OPTIMIZATION-IF-FALSE*/
30494 s /= 10;
30495 }
30496 e--;
30497 }
30498
30499 /* adjust the sign of significand */
30500 s = sign<0 ? -s : s;
30501
30502 if( e==0 ){ /*OPTIMIZATION-IF-TRUE*/
30503 result = (double)s;
30504 }else{
30505 /* attempt to handle extremely small/large numbers better */
30506 if( e>307 ){ /*OPTIMIZATION-IF-TRUE*/
30507 if( e<342 ){ /*OPTIMIZATION-IF-TRUE*/
30508 LONGDOUBLE_TYPE scale = sqlite3Pow10(e-308);
30509 if( esign<0 ){
30510 result = s / scale;
30511 result /= 1.0e+308;
30512 }else{
30513 result = s * scale;
30514 result *= 1.0e+308;
30515 }
30516 }else{ assert( e>=342 );
30517 if( esign<0 ){
30518 result = 0.0*s;
30519 }else{
30520#ifdef INFINITY
30521 result = INFINITY*s;
30522#else
30523 result = 1e308*1e308*s; /* Infinity */
30524#endif
30525 }
30526 }
30527 }else{
30528 LONGDOUBLE_TYPE scale = sqlite3Pow10(e);
30529 if( esign<0 ){
30530 result = s / scale;
30531 }else{
30532 result = s * scale;
30533 }
30534 }
30535 }
30536 }
30537
30538 /* store the result */
30539 *pResult = result;
30540
30541 /* return true if number and no extra non-whitespace chracters after */
30542 return z==zEnd && nDigits>0 && eValid && nonNum==0;
30543#else
30544 return !sqlite3Atoi64(z, pResult, length, enc);
30545#endif /* SQLITE_OMIT_FLOATING_POINT */
30546}
30547
30548/*
30549** Compare the 19-character string zNum against the text representation
30550** value 2^63: 9223372036854775808. Return negative, zero, or positive
30551** if zNum is less than, equal to, or greater than the string.
30552** Note that zNum must contain exactly 19 characters.
30553**
30554** Unlike memcmp() this routine is guaranteed to return the difference
30555** in the values of the last digit if the only difference is in the
30556** last digit. So, for example,
30557**
30558** compare2pow63("9223372036854775800", 1)
30559**
30560** will return -8.
30561*/
30562static int compare2pow63(const char *zNum, int incr){
30563 int c = 0;
30564 int i;
30565 /* 012345678901234567 */
30566 const char *pow63 = "922337203685477580";
30567 for(i=0; c==0 && i<18; i++){
30568 c = (zNum[i*incr]-pow63[i])*10;
30569 }
30570 if( c==0 ){
30571 c = zNum[18*incr] - '8';
30572 testcase( c==(-1) );
30573 testcase( c==0 );
30574 testcase( c==(+1) );
30575 }
30576 return c;
30577}
30578
30579/*
30580** Convert zNum to a 64-bit signed integer. zNum must be decimal. This
30581** routine does *not* accept hexadecimal notation.
30582**
30583** Returns:
30584**
30585** 0 Successful transformation. Fits in a 64-bit signed integer.
30586** 1 Excess non-space text after the integer value
30587** 2 Integer too large for a 64-bit signed integer or is malformed
30588** 3 Special case of 9223372036854775808
30589**
30590** length is the number of bytes in the string (bytes, not characters).
30591** The string is not necessarily zero-terminated. The encoding is
30592** given by enc.
30593*/
30594SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
30595 int incr;
30596 u64 u = 0;
30597 int neg = 0; /* assume positive */
30598 int i;
30599 int c = 0;
30600 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */
30601 int rc; /* Baseline return code */
30602 const char *zStart;
30603 const char *zEnd = zNum + length;
30604 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
30605 if( enc==SQLITE_UTF8 ){
30606 incr = 1;
30607 }else{
30608 incr = 2;
30609 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
30610 for(i=3-enc; i<length && zNum[i]==0; i+=2){}
30611 nonNum = i<length;
30612 zEnd = &zNum[i^1];
30613 zNum += (enc&1);
30614 }
30615 while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
30616 if( zNum<zEnd ){
30617 if( *zNum=='-' ){
30618 neg = 1;
30619 zNum+=incr;
30620 }else if( *zNum=='+' ){
30621 zNum+=incr;
30622 }
30623 }
30624 zStart = zNum;
30625 while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
30626 for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
30627 u = u*10 + c - '0';
30628 }
30629 testcase( i==18*incr );
30630 testcase( i==19*incr );
30631 testcase( i==20*incr );
30632 if( u>LARGEST_INT64 ){
30633 /* This test and assignment is needed only to suppress UB warnings
30634 ** from clang and -fsanitize=undefined. This test and assignment make
30635 ** the code a little larger and slower, and no harm comes from omitting
30636 ** them, but we must appaise the undefined-behavior pharisees. */
30637 *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
30638 }else if( neg ){
30639 *pNum = -(i64)u;
30640 }else{
30641 *pNum = (i64)u;
30642 }
30643 rc = 0;
30644 if( (i==0 && zStart==zNum) /* No digits */
30645 || nonNum /* UTF16 with high-order bytes non-zero */
30646 ){
30647 rc = 1;
30648 }else if( &zNum[i]<zEnd ){ /* Extra bytes at the end */
30649 int jj = i;
30650 do{
30651 if( !sqlite3Isspace(zNum[jj]) ){
30652 rc = 1; /* Extra non-space text after the integer */
30653 break;
30654 }
30655 jj += incr;
30656 }while( &zNum[jj]<zEnd );
30657 }
30658 if( i<19*incr ){
30659 /* Less than 19 digits, so we know that it fits in 64 bits */
30660 assert( u<=LARGEST_INT64 );
30661 return rc;
30662 }else{
30663 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */
30664 c = i>19*incr ? 1 : compare2pow63(zNum, incr);
30665 if( c<0 ){
30666 /* zNum is less than 9223372036854775808 so it fits */
30667 assert( u<=LARGEST_INT64 );
30668 return rc;
30669 }else{
30670 *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
30671 if( c>0 ){
30672 /* zNum is greater than 9223372036854775808 so it overflows */
30673 return 2;
30674 }else{
30675 /* zNum is exactly 9223372036854775808. Fits if negative. The
30676 ** special case 2 overflow if positive */
30677 assert( u-1==LARGEST_INT64 );
30678 return neg ? rc : 3;
30679 }
30680 }
30681 }
30682}
30683
30684/*
30685** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
30686** into a 64-bit signed integer. This routine accepts hexadecimal literals,
30687** whereas sqlite3Atoi64() does not.
30688**
30689** Returns:
30690**
30691** 0 Successful transformation. Fits in a 64-bit signed integer.
30692** 1 Excess text after the integer value
30693** 2 Integer too large for a 64-bit signed integer or is malformed
30694** 3 Special case of 9223372036854775808
30695*/
30696SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char *z, i64 *pOut){
30697#ifndef SQLITE_OMIT_HEX_INTEGER
30698 if( z[0]=='0'
30699 && (z[1]=='x' || z[1]=='X')
30700 ){
30701 u64 u = 0;
30702 int i, k;
30703 for(i=2; z[i]=='0'; i++){}
30704 for(k=i; sqlite3Isxdigit(z[k]); k++){
30705 u = u*16 + sqlite3HexToInt(z[k]);
30706 }
30707 memcpy(pOut, &u, 8);
30708 return (z[k]==0 && k-i<=16) ? 0 : 2;
30709 }else
30710#endif /* SQLITE_OMIT_HEX_INTEGER */
30711 {
30712 return sqlite3Atoi64(z, pOut, sqlite3Strlen30(z), SQLITE_UTF8);
30713 }
30714}
30715
30716/*
30717** If zNum represents an integer that will fit in 32-bits, then set
30718** *pValue to that integer and return true. Otherwise return false.
30719**
30720** This routine accepts both decimal and hexadecimal notation for integers.
30721**
30722** Any non-numeric characters that following zNum are ignored.
30723** This is different from sqlite3Atoi64() which requires the
30724** input number to be zero-terminated.
30725*/
30726SQLITE_PRIVATE int sqlite3GetInt32(const char *zNum, int *pValue){
30727 sqlite_int64 v = 0;
30728 int i, c;
30729 int neg = 0;
30730 if( zNum[0]=='-' ){
30731 neg = 1;
30732 zNum++;
30733 }else if( zNum[0]=='+' ){
30734 zNum++;
30735 }
30736#ifndef SQLITE_OMIT_HEX_INTEGER
30737 else if( zNum[0]=='0'
30738 && (zNum[1]=='x' || zNum[1]=='X')
30739 && sqlite3Isxdigit(zNum[2])
30740 ){
30741 u32 u = 0;
30742 zNum += 2;
30743 while( zNum[0]=='0' ) zNum++;
30744 for(i=0; sqlite3Isxdigit(zNum[i]) && i<8; i++){
30745 u = u*16 + sqlite3HexToInt(zNum[i]);
30746 }
30747 if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])==0 ){
30748 memcpy(pValue, &u, 4);
30749 return 1;
30750 }else{
30751 return 0;
30752 }
30753 }
30754#endif
30755 if( !sqlite3Isdigit(zNum[0]) ) return 0;
30756 while( zNum[0]=='0' ) zNum++;
30757 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
30758 v = v*10 + c;
30759 }
30760
30761 /* The longest decimal representation of a 32 bit integer is 10 digits:
30762 **
30763 ** 1234567890
30764 ** 2^31 -> 2147483648
30765 */
30766 testcase( i==10 );
30767 if( i>10 ){
30768 return 0;
30769 }
30770 testcase( v-neg==2147483647 );
30771 if( v-neg>2147483647 ){
30772 return 0;
30773 }
30774 if( neg ){
30775 v = -v;
30776 }
30777 *pValue = (int)v;
30778 return 1;
30779}
30780
30781/*
30782** Return a 32-bit integer value extracted from a string. If the
30783** string is not an integer, just return 0.
30784*/
30785SQLITE_PRIVATE int sqlite3Atoi(const char *z){
30786 int x = 0;
30787 if( z ) sqlite3GetInt32(z, &x);
30788 return x;
30789}
30790
30791/*
30792** The variable-length integer encoding is as follows:
30793**
30794** KEY:
30795** A = 0xxxxxxx 7 bits of data and one flag bit
30796** B = 1xxxxxxx 7 bits of data and one flag bit
30797** C = xxxxxxxx 8 bits of data
30798**
30799** 7 bits - A
30800** 14 bits - BA
30801** 21 bits - BBA
30802** 28 bits - BBBA
30803** 35 bits - BBBBA
30804** 42 bits - BBBBBA
30805** 49 bits - BBBBBBA
30806** 56 bits - BBBBBBBA
30807** 64 bits - BBBBBBBBC
30808*/
30809
30810/*
30811** Write a 64-bit variable-length integer to memory starting at p[0].
30812** The length of data write will be between 1 and 9 bytes. The number
30813** of bytes written is returned.
30814**
30815** A variable-length integer consists of the lower 7 bits of each byte
30816** for all bytes that have the 8th bit set and one byte with the 8th
30817** bit clear. Except, if we get to the 9th byte, it stores the full
30818** 8 bits and is the last byte.
30819*/
30820static int SQLITE_NOINLINE putVarint64(unsigned char *p, u64 v){
30821 int i, j, n;
30822 u8 buf[10];
30823 if( v & (((u64)0xff000000)<<32) ){
30824 p[8] = (u8)v;
30825 v >>= 8;
30826 for(i=7; i>=0; i--){
30827 p[i] = (u8)((v & 0x7f) | 0x80);
30828 v >>= 7;
30829 }
30830 return 9;
30831 }
30832 n = 0;
30833 do{
30834 buf[n++] = (u8)((v & 0x7f) | 0x80);
30835 v >>= 7;
30836 }while( v!=0 );
30837 buf[0] &= 0x7f;
30838 assert( n<=9 );
30839 for(i=0, j=n-1; j>=0; j--, i++){
30840 p[i] = buf[j];
30841 }
30842 return n;
30843}
30844SQLITE_PRIVATE int sqlite3PutVarint(unsigned char *p, u64 v){
30845 if( v<=0x7f ){
30846 p[0] = v&0x7f;
30847 return 1;
30848 }
30849 if( v<=0x3fff ){
30850 p[0] = ((v>>7)&0x7f)|0x80;
30851 p[1] = v&0x7f;
30852 return 2;
30853 }
30854 return putVarint64(p,v);
30855}
30856
30857/*
30858** Bitmasks used by sqlite3GetVarint(). These precomputed constants
30859** are defined here rather than simply putting the constant expressions
30860** inline in order to work around bugs in the RVT compiler.
30861**
30862** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
30863**
30864** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
30865*/
30866#define SLOT_2_0 0x001fc07f
30867#define SLOT_4_2_0 0xf01fc07f
30868
30869
30870/*
30871** Read a 64-bit variable-length integer from memory starting at p[0].
30872** Return the number of bytes read. The value is stored in *v.
30873*/
30874SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
30875 u32 a,b,s;
30876
30877 a = *p;
30878 /* a: p0 (unmasked) */
30879 if (!(a&0x80))
30880 {
30881 *v = a;
30882 return 1;
30883 }
30884
30885 p++;
30886 b = *p;
30887 /* b: p1 (unmasked) */
30888 if (!(b&0x80))
30889 {
30890 a &= 0x7f;
30891 a = a<<7;
30892 a |= b;
30893 *v = a;
30894 return 2;
30895 }
30896
30897 /* Verify that constants are precomputed correctly */
30898 assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
30899 assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
30900
30901 p++;
30902 a = a<<14;
30903 a |= *p;
30904 /* a: p0<<14 | p2 (unmasked) */
30905 if (!(a&0x80))
30906 {
30907 a &= SLOT_2_0;
30908 b &= 0x7f;
30909 b = b<<7;
30910 a |= b;
30911 *v = a;
30912 return 3;
30913 }
30914
30915 /* CSE1 from below */
30916 a &= SLOT_2_0;
30917 p++;
30918 b = b<<14;
30919 b |= *p;
30920 /* b: p1<<14 | p3 (unmasked) */
30921 if (!(b&0x80))
30922 {
30923 b &= SLOT_2_0;
30924 /* moved CSE1 up */
30925 /* a &= (0x7f<<14)|(0x7f); */
30926 a = a<<7;
30927 a |= b;
30928 *v = a;
30929 return 4;
30930 }
30931
30932 /* a: p0<<14 | p2 (masked) */
30933 /* b: p1<<14 | p3 (unmasked) */
30934 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
30935 /* moved CSE1 up */
30936 /* a &= (0x7f<<14)|(0x7f); */
30937 b &= SLOT_2_0;
30938 s = a;
30939 /* s: p0<<14 | p2 (masked) */
30940
30941 p++;
30942 a = a<<14;
30943 a |= *p;
30944 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
30945 if (!(a&0x80))
30946 {
30947 /* we can skip these cause they were (effectively) done above
30948 ** while calculating s */
30949 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
30950 /* b &= (0x7f<<14)|(0x7f); */
30951 b = b<<7;
30952 a |= b;
30953 s = s>>18;
30954 *v = ((u64)s)<<32 | a;
30955 return 5;
30956 }
30957
30958 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
30959 s = s<<7;
30960 s |= b;
30961 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
30962
30963 p++;
30964 b = b<<14;
30965 b |= *p;
30966 /* b: p1<<28 | p3<<14 | p5 (unmasked) */
30967 if (!(b&0x80))
30968 {
30969 /* we can skip this cause it was (effectively) done above in calc'ing s */
30970 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
30971 a &= SLOT_2_0;
30972 a = a<<7;
30973 a |= b;
30974 s = s>>18;
30975 *v = ((u64)s)<<32 | a;
30976 return 6;
30977 }
30978
30979 p++;
30980 a = a<<14;
30981 a |= *p;
30982 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
30983 if (!(a&0x80))
30984 {
30985 a &= SLOT_4_2_0;
30986 b &= SLOT_2_0;
30987 b = b<<7;
30988 a |= b;
30989 s = s>>11;
30990 *v = ((u64)s)<<32 | a;
30991 return 7;
30992 }
30993
30994 /* CSE2 from below */
30995 a &= SLOT_2_0;
30996 p++;
30997 b = b<<14;
30998 b |= *p;
30999 /* b: p3<<28 | p5<<14 | p7 (unmasked) */
31000 if (!(b&0x80))
31001 {
31002 b &= SLOT_4_2_0;
31003 /* moved CSE2 up */
31004 /* a &= (0x7f<<14)|(0x7f); */
31005 a = a<<7;
31006 a |= b;
31007 s = s>>4;
31008 *v = ((u64)s)<<32 | a;
31009 return 8;
31010 }
31011
31012 p++;
31013 a = a<<15;
31014 a |= *p;
31015 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
31016
31017 /* moved CSE2 up */
31018 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
31019 b &= SLOT_2_0;
31020 b = b<<8;
31021 a |= b;
31022
31023 s = s<<4;
31024 b = p[-4];
31025 b &= 0x7f;
31026 b = b>>3;
31027 s |= b;
31028
31029 *v = ((u64)s)<<32 | a;
31030
31031 return 9;
31032}
31033
31034/*
31035** Read a 32-bit variable-length integer from memory starting at p[0].
31036** Return the number of bytes read. The value is stored in *v.
31037**
31038** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
31039** integer, then set *v to 0xffffffff.
31040**
31041** A MACRO version, getVarint32, is provided which inlines the
31042** single-byte case. All code should use the MACRO version as
31043** this function assumes the single-byte case has already been handled.
31044*/
31045SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
31046 u32 a,b;
31047
31048 /* The 1-byte case. Overwhelmingly the most common. Handled inline
31049 ** by the getVarin32() macro */
31050 a = *p;
31051 /* a: p0 (unmasked) */
31052#ifndef getVarint32
31053 if (!(a&0x80))
31054 {
31055 /* Values between 0 and 127 */
31056 *v = a;
31057 return 1;
31058 }
31059#endif
31060
31061 /* The 2-byte case */
31062 p++;
31063 b = *p;
31064 /* b: p1 (unmasked) */
31065 if (!(b&0x80))
31066 {
31067 /* Values between 128 and 16383 */
31068 a &= 0x7f;
31069 a = a<<7;
31070 *v = a | b;
31071 return 2;
31072 }
31073
31074 /* The 3-byte case */
31075 p++;
31076 a = a<<14;
31077 a |= *p;
31078 /* a: p0<<14 | p2 (unmasked) */
31079 if (!(a&0x80))
31080 {
31081 /* Values between 16384 and 2097151 */
31082 a &= (0x7f<<14)|(0x7f);
31083 b &= 0x7f;
31084 b = b<<7;
31085 *v = a | b;
31086 return 3;
31087 }
31088
31089 /* A 32-bit varint is used to store size information in btrees.
31090 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
31091 ** A 3-byte varint is sufficient, for example, to record the size
31092 ** of a 1048569-byte BLOB or string.
31093 **
31094 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
31095 ** rare larger cases can be handled by the slower 64-bit varint
31096 ** routine.
31097 */
31098#if 1
31099 {
31100 u64 v64;
31101 u8 n;
31102
31103 p -= 2;
31104 n = sqlite3GetVarint(p, &v64);
31105 assert( n>3 && n<=9 );
31106 if( (v64 & SQLITE_MAX_U32)!=v64 ){
31107 *v = 0xffffffff;
31108 }else{
31109 *v = (u32)v64;
31110 }
31111 return n;
31112 }
31113
31114#else
31115 /* For following code (kept for historical record only) shows an
31116 ** unrolling for the 3- and 4-byte varint cases. This code is
31117 ** slightly faster, but it is also larger and much harder to test.
31118 */
31119 p++;
31120 b = b<<14;
31121 b |= *p;
31122 /* b: p1<<14 | p3 (unmasked) */
31123 if (!(b&0x80))
31124 {
31125 /* Values between 2097152 and 268435455 */
31126 b &= (0x7f<<14)|(0x7f);
31127 a &= (0x7f<<14)|(0x7f);
31128 a = a<<7;
31129 *v = a | b;
31130 return 4;
31131 }
31132
31133 p++;
31134 a = a<<14;
31135 a |= *p;
31136 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
31137 if (!(a&0x80))
31138 {
31139 /* Values between 268435456 and 34359738367 */
31140 a &= SLOT_4_2_0;
31141 b &= SLOT_4_2_0;
31142 b = b<<7;
31143 *v = a | b;
31144 return 5;
31145 }
31146
31147 /* We can only reach this point when reading a corrupt database
31148 ** file. In that case we are not in any hurry. Use the (relatively
31149 ** slow) general-purpose sqlite3GetVarint() routine to extract the
31150 ** value. */
31151 {
31152 u64 v64;
31153 u8 n;
31154
31155 p -= 4;
31156 n = sqlite3GetVarint(p, &v64);
31157 assert( n>5 && n<=9 );
31158 *v = (u32)v64;
31159 return n;
31160 }
31161#endif
31162}
31163
31164/*
31165** Return the number of bytes that will be needed to store the given
31166** 64-bit integer.
31167*/
31168SQLITE_PRIVATE int sqlite3VarintLen(u64 v){
31169 int i;
31170 for(i=1; (v >>= 7)!=0; i++){ assert( i<10 ); }
31171 return i;
31172}
31173
31174
31175/*
31176** Read or write a four-byte big-endian integer value.
31177*/
31178SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
31179#if SQLITE_BYTEORDER==4321
31180 u32 x;
31181 memcpy(&x,p,4);
31182 return x;
31183#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
31184 u32 x;
31185 memcpy(&x,p,4);
31186 return __builtin_bswap32(x);
31187#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
31188 u32 x;
31189 memcpy(&x,p,4);
31190 return _byteswap_ulong(x);
31191#else
31192 testcase( p[0]&0x80 );
31193 return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
31194#endif
31195}
31196SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
31197#if SQLITE_BYTEORDER==4321
31198 memcpy(p,&v,4);
31199#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
31200 u32 x = __builtin_bswap32(v);
31201 memcpy(p,&x,4);
31202#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
31203 u32 x = _byteswap_ulong(v);
31204 memcpy(p,&x,4);
31205#else
31206 p[0] = (u8)(v>>24);
31207 p[1] = (u8)(v>>16);
31208 p[2] = (u8)(v>>8);
31209 p[3] = (u8)v;
31210#endif
31211}
31212
31213
31214
31215/*
31216** Translate a single byte of Hex into an integer.
31217** This routine only works if h really is a valid hexadecimal
31218** character: 0..9a..fA..F
31219*/
31220SQLITE_PRIVATE u8 sqlite3HexToInt(int h){
31221 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') );
31222#ifdef SQLITE_ASCII
31223 h += 9*(1&(h>>6));
31224#endif
31225#ifdef SQLITE_EBCDIC
31226 h += 9*(1&~(h>>4));
31227#endif
31228 return (u8)(h & 0xf);
31229}
31230
31231#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
31232/*
31233** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
31234** value. Return a pointer to its binary value. Space to hold the
31235** binary value has been obtained from malloc and must be freed by
31236** the calling routine.
31237*/
31238SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
31239 char *zBlob;
31240 int i;
31241
31242 zBlob = (char *)sqlite3DbMallocRawNN(db, n/2 + 1);
31243 n--;
31244 if( zBlob ){
31245 for(i=0; i<n; i+=2){
31246 zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
31247 }
31248 zBlob[i/2] = 0;
31249 }
31250 return zBlob;
31251}
31252#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
31253
31254/*
31255** Log an error that is an API call on a connection pointer that should
31256** not have been used. The "type" of connection pointer is given as the
31257** argument. The zType is a word like "NULL" or "closed" or "invalid".
31258*/
31259static void logBadConnection(const char *zType){
31260 sqlite3_log(SQLITE_MISUSE,
31261 "API call with %s database connection pointer",
31262 zType
31263 );
31264}
31265
31266/*
31267** Check to make sure we have a valid db pointer. This test is not
31268** foolproof but it does provide some measure of protection against
31269** misuse of the interface such as passing in db pointers that are
31270** NULL or which have been previously closed. If this routine returns
31271** 1 it means that the db pointer is valid and 0 if it should not be
31272** dereferenced for any reason. The calling function should invoke
31273** SQLITE_MISUSE immediately.
31274**
31275** sqlite3SafetyCheckOk() requires that the db pointer be valid for
31276** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
31277** open properly and is not fit for general use but which can be
31278** used as an argument to sqlite3_errmsg() or sqlite3_close().
31279*/
31280SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){
31281 u32 magic;
31282 if( db==0 ){
31283 logBadConnection("NULL");
31284 return 0;
31285 }
31286 magic = db->magic;
31287 if( magic!=SQLITE_MAGIC_OPEN ){
31288 if( sqlite3SafetyCheckSickOrOk(db) ){
31289 testcase( sqlite3GlobalConfig.xLog!=0 );
31290 logBadConnection("unopened");
31291 }
31292 return 0;
31293 }else{
31294 return 1;
31295 }
31296}
31297SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
31298 u32 magic;
31299 magic = db->magic;
31300 if( magic!=SQLITE_MAGIC_SICK &&
31301 magic!=SQLITE_MAGIC_OPEN &&
31302 magic!=SQLITE_MAGIC_BUSY ){
31303 testcase( sqlite3GlobalConfig.xLog!=0 );
31304 logBadConnection("invalid");
31305 return 0;
31306 }else{
31307 return 1;
31308 }
31309}
31310
31311/*
31312** Attempt to add, substract, or multiply the 64-bit signed value iB against
31313** the other 64-bit signed integer at *pA and store the result in *pA.
31314** Return 0 on success. Or if the operation would have resulted in an
31315** overflow, leave *pA unchanged and return 1.
31316*/
31317SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
31318#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
31319 return __builtin_add_overflow(*pA, iB, pA);
31320#else
31321 i64 iA = *pA;
31322 testcase( iA==0 ); testcase( iA==1 );
31323 testcase( iB==-1 ); testcase( iB==0 );
31324 if( iB>=0 ){
31325 testcase( iA>0 && LARGEST_INT64 - iA == iB );
31326 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
31327 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
31328 }else{
31329 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
31330 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
31331 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
31332 }
31333 *pA += iB;
31334 return 0;
31335#endif
31336}
31337SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
31338#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
31339 return __builtin_sub_overflow(*pA, iB, pA);
31340#else
31341 testcase( iB==SMALLEST_INT64+1 );
31342 if( iB==SMALLEST_INT64 ){
31343 testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
31344 if( (*pA)>=0 ) return 1;
31345 *pA -= iB;
31346 return 0;
31347 }else{
31348 return sqlite3AddInt64(pA, -iB);
31349 }
31350#endif
31351}
31352SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
31353#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
31354 return __builtin_mul_overflow(*pA, iB, pA);
31355#else
31356 i64 iA = *pA;
31357 if( iB>0 ){
31358 if( iA>LARGEST_INT64/iB ) return 1;
31359 if( iA<SMALLEST_INT64/iB ) return 1;
31360 }else if( iB<0 ){
31361 if( iA>0 ){
31362 if( iB<SMALLEST_INT64/iA ) return 1;
31363 }else if( iA<0 ){
31364 if( iB==SMALLEST_INT64 ) return 1;
31365 if( iA==SMALLEST_INT64 ) return 1;
31366 if( -iA>LARGEST_INT64/-iB ) return 1;
31367 }
31368 }
31369 *pA = iA*iB;
31370 return 0;
31371#endif
31372}
31373
31374/*
31375** Compute the absolute value of a 32-bit signed integer, of possible. Or
31376** if the integer has a value of -2147483648, return +2147483647
31377*/
31378SQLITE_PRIVATE int sqlite3AbsInt32(int x){
31379 if( x>=0 ) return x;
31380 if( x==(int)0x80000000 ) return 0x7fffffff;
31381 return -x;
31382}
31383
31384#ifdef SQLITE_ENABLE_8_3_NAMES
31385/*
31386** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
31387** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
31388** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
31389** three characters, then shorten the suffix on z[] to be the last three
31390** characters of the original suffix.
31391**
31392** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
31393** do the suffix shortening regardless of URI parameter.
31394**
31395** Examples:
31396**
31397** test.db-journal => test.nal
31398** test.db-wal => test.wal
31399** test.db-shm => test.shm
31400** test.db-mj7f3319fa => test.9fa
31401*/
31402SQLITE_PRIVATE void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
31403#if SQLITE_ENABLE_8_3_NAMES<2
31404 if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
31405#endif
31406 {
31407 int i, sz;
31408 sz = sqlite3Strlen30(z);
31409 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
31410 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
31411 }
31412}
31413#endif
31414
31415/*
31416** Find (an approximate) sum of two LogEst values. This computation is
31417** not a simple "+" operator because LogEst is stored as a logarithmic
31418** value.
31419**
31420*/
31421SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst a, LogEst b){
31422 static const unsigned char x[] = {
31423 10, 10, /* 0,1 */
31424 9, 9, /* 2,3 */
31425 8, 8, /* 4,5 */
31426 7, 7, 7, /* 6,7,8 */
31427 6, 6, 6, /* 9,10,11 */
31428 5, 5, 5, /* 12-14 */
31429 4, 4, 4, 4, /* 15-18 */
31430 3, 3, 3, 3, 3, 3, /* 19-24 */
31431 2, 2, 2, 2, 2, 2, 2, /* 25-31 */
31432 };
31433 if( a>=b ){
31434 if( a>b+49 ) return a;
31435 if( a>b+31 ) return a+1;
31436 return a+x[a-b];
31437 }else{
31438 if( b>a+49 ) return b;
31439 if( b>a+31 ) return b+1;
31440 return b+x[b-a];
31441 }
31442}
31443
31444/*
31445** Convert an integer into a LogEst. In other words, compute an
31446** approximation for 10*log2(x).
31447*/
31448SQLITE_PRIVATE LogEst sqlite3LogEst(u64 x){
31449 static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
31450 LogEst y = 40;
31451 if( x<8 ){
31452 if( x<2 ) return 0;
31453 while( x<8 ){ y -= 10; x <<= 1; }
31454 }else{
31455#if GCC_VERSION>=5004000
31456 int i = 60 - __builtin_clzll(x);
31457 y += i*10;
31458 x >>= i;
31459#else
31460 while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/
31461 while( x>15 ){ y += 10; x >>= 1; }
31462#endif
31463 }
31464 return a[x&7] + y - 10;
31465}
31466
31467#ifndef SQLITE_OMIT_VIRTUALTABLE
31468/*
31469** Convert a double into a LogEst
31470** In other words, compute an approximation for 10*log2(x).
31471*/
31472SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double x){
31473 u64 a;
31474 LogEst e;
31475 assert( sizeof(x)==8 && sizeof(a)==8 );
31476 if( x<=1 ) return 0;
31477 if( x<=2000000000 ) return sqlite3LogEst((u64)x);
31478 memcpy(&a, &x, 8);
31479 e = (a>>52) - 1022;
31480 return e*10;
31481}
31482#endif /* SQLITE_OMIT_VIRTUALTABLE */
31483
31484#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
31485 defined(SQLITE_ENABLE_STAT3_OR_STAT4) || \
31486 defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
31487/*
31488** Convert a LogEst into an integer.
31489**
31490** Note that this routine is only used when one or more of various
31491** non-standard compile-time options is enabled.
31492*/
31493SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst x){
31494 u64 n;
31495 n = x%10;
31496 x /= 10;
31497 if( n>=5 ) n -= 2;
31498 else if( n>=1 ) n -= 1;
31499#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
31500 defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
31501 if( x>60 ) return (u64)LARGEST_INT64;
31502#else
31503 /* If only SQLITE_ENABLE_STAT3_OR_STAT4 is on, then the largest input
31504 ** possible to this routine is 310, resulting in a maximum x of 31 */
31505 assert( x<=60 );
31506#endif
31507 return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x);
31508}
31509#endif /* defined SCANSTAT or STAT4 or ESTIMATED_ROWS */
31510
31511/*
31512** Add a new name/number pair to a VList. This might require that the
31513** VList object be reallocated, so return the new VList. If an OOM
31514** error occurs, the original VList returned and the
31515** db->mallocFailed flag is set.
31516**
31517** A VList is really just an array of integers. To destroy a VList,
31518** simply pass it to sqlite3DbFree().
31519**
31520** The first integer is the number of integers allocated for the whole
31521** VList. The second integer is the number of integers actually used.
31522** Each name/number pair is encoded by subsequent groups of 3 or more
31523** integers.
31524**
31525** Each name/number pair starts with two integers which are the numeric
31526** value for the pair and the size of the name/number pair, respectively.
31527** The text name overlays one or more following integers. The text name
31528** is always zero-terminated.
31529**
31530** Conceptually:
31531**
31532** struct VList {
31533** int nAlloc; // Number of allocated slots
31534** int nUsed; // Number of used slots
31535** struct VListEntry {
31536** int iValue; // Value for this entry
31537** int nSlot; // Slots used by this entry
31538** // ... variable name goes here
31539** } a[0];
31540** }
31541**
31542** During code generation, pointers to the variable names within the
31543** VList are taken. When that happens, nAlloc is set to zero as an
31544** indication that the VList may never again be enlarged, since the
31545** accompanying realloc() would invalidate the pointers.
31546*/
31547SQLITE_PRIVATE VList *sqlite3VListAdd(
31548 sqlite3 *db, /* The database connection used for malloc() */
31549 VList *pIn, /* The input VList. Might be NULL */
31550 const char *zName, /* Name of symbol to add */
31551 int nName, /* Bytes of text in zName */
31552 int iVal /* Value to associate with zName */
31553){
31554 int nInt; /* number of sizeof(int) objects needed for zName */
31555 char *z; /* Pointer to where zName will be stored */
31556 int i; /* Index in pIn[] where zName is stored */
31557
31558 nInt = nName/4 + 3;
31559 assert( pIn==0 || pIn[0]>=3 ); /* Verify ok to add new elements */
31560 if( pIn==0 || pIn[1]+nInt > pIn[0] ){
31561 /* Enlarge the allocation */
31562 int nAlloc = (pIn ? pIn[0]*2 : 10) + nInt;
31563 VList *pOut = sqlite3DbRealloc(db, pIn, nAlloc*sizeof(int));
31564 if( pOut==0 ) return pIn;
31565 if( pIn==0 ) pOut[1] = 2;
31566 pIn = pOut;
31567 pIn[0] = nAlloc;
31568 }
31569 i = pIn[1];
31570 pIn[i] = iVal;
31571 pIn[i+1] = nInt;
31572 z = (char*)&pIn[i+2];
31573 pIn[1] = i+nInt;
31574 assert( pIn[1]<=pIn[0] );
31575 memcpy(z, zName, nName);
31576 z[nName] = 0;
31577 return pIn;
31578}
31579
31580/*
31581** Return a pointer to the name of a variable in the given VList that
31582** has the value iVal. Or return a NULL if there is no such variable in
31583** the list
31584*/
31585SQLITE_PRIVATE const char *sqlite3VListNumToName(VList *pIn, int iVal){
31586 int i, mx;
31587 if( pIn==0 ) return 0;
31588 mx = pIn[1];
31589 i = 2;
31590 do{
31591 if( pIn[i]==iVal ) return (char*)&pIn[i+2];
31592 i += pIn[i+1];
31593 }while( i<mx );
31594 return 0;
31595}
31596
31597/*
31598** Return the number of the variable named zName, if it is in VList.
31599** or return 0 if there is no such variable.
31600*/
31601SQLITE_PRIVATE int sqlite3VListNameToNum(VList *pIn, const char *zName, int nName){
31602 int i, mx;
31603 if( pIn==0 ) return 0;
31604 mx = pIn[1];
31605 i = 2;
31606 do{
31607 const char *z = (const char*)&pIn[i+2];
31608 if( strncmp(z,zName,nName)==0 && z[nName]==0 ) return pIn[i];
31609 i += pIn[i+1];
31610 }while( i<mx );
31611 return 0;
31612}
31613
31614/************** End of util.c ************************************************/
31615/************** Begin file hash.c ********************************************/
31616/*
31617** 2001 September 22
31618**
31619** The author disclaims copyright to this source code. In place of
31620** a legal notice, here is a blessing:
31621**
31622** May you do good and not evil.
31623** May you find forgiveness for yourself and forgive others.
31624** May you share freely, never taking more than you give.
31625**
31626*************************************************************************
31627** This is the implementation of generic hash-tables
31628** used in SQLite.
31629*/
31630/* #include "sqliteInt.h" */
31631/* #include <assert.h> */
31632
31633/* Turn bulk memory into a hash table object by initializing the
31634** fields of the Hash structure.
31635**
31636** "pNew" is a pointer to the hash table that is to be initialized.
31637*/
31638SQLITE_PRIVATE void sqlite3HashInit(Hash *pNew){
31639 assert( pNew!=0 );
31640 pNew->first = 0;
31641 pNew->count = 0;
31642 pNew->htsize = 0;
31643 pNew->ht = 0;
31644}
31645
31646/* Remove all entries from a hash table. Reclaim all memory.
31647** Call this routine to delete a hash table or to reset a hash table
31648** to the empty state.
31649*/
31650SQLITE_PRIVATE void sqlite3HashClear(Hash *pH){
31651 HashElem *elem; /* For looping over all elements of the table */
31652
31653 assert( pH!=0 );
31654 elem = pH->first;
31655 pH->first = 0;
31656 sqlite3_free(pH->ht);
31657 pH->ht = 0;
31658 pH->htsize = 0;
31659 while( elem ){
31660 HashElem *next_elem = elem->next;
31661 sqlite3_free(elem);
31662 elem = next_elem;
31663 }
31664 pH->count = 0;
31665}
31666
31667/*
31668** The hashing function.
31669*/
31670static unsigned int strHash(const char *z){
31671 unsigned int h = 0;
31672 unsigned char c;
31673 while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/
31674 /* Knuth multiplicative hashing. (Sorting & Searching, p. 510).
31675 ** 0x9e3779b1 is 2654435761 which is the closest prime number to
31676 ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
31677 h += sqlite3UpperToLower[c];
31678 h *= 0x9e3779b1;
31679 }
31680 return h;
31681}
31682#ifdef SQLITE_ENABLE_NORMALIZE
31683static unsigned int strHashN(const char *z, int n){
31684 unsigned int h = 0;
31685 int i;
31686 for(i=0; i<n; i++){
31687 /* Knuth multiplicative hashing. (Sorting & Searching, p. 510).
31688 ** 0x9e3779b1 is 2654435761 which is the closest prime number to
31689 ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
31690 h += sqlite3UpperToLower[z[i]];
31691 h *= 0x9e3779b1;
31692 }
31693 return h;
31694}
31695#endif /* SQLITE_ENABLE_NORMALIZE */
31696
31697
31698/* Link pNew element into the hash table pH. If pEntry!=0 then also
31699** insert pNew into the pEntry hash bucket.
31700*/
31701static void insertElement(
31702 Hash *pH, /* The complete hash table */
31703 struct _ht *pEntry, /* The entry into which pNew is inserted */
31704 HashElem *pNew /* The element to be inserted */
31705){
31706 HashElem *pHead; /* First element already in pEntry */
31707 if( pEntry ){
31708 pHead = pEntry->count ? pEntry->chain : 0;
31709 pEntry->count++;
31710 pEntry->chain = pNew;
31711 }else{
31712 pHead = 0;
31713 }
31714 if( pHead ){
31715 pNew->next = pHead;
31716 pNew->prev = pHead->prev;
31717 if( pHead->prev ){ pHead->prev->next = pNew; }
31718 else { pH->first = pNew; }
31719 pHead->prev = pNew;
31720 }else{
31721 pNew->next = pH->first;
31722 if( pH->first ){ pH->first->prev = pNew; }
31723 pNew->prev = 0;
31724 pH->first = pNew;
31725 }
31726}
31727
31728
31729/* Resize the hash table so that it cantains "new_size" buckets.
31730**
31731** The hash table might fail to resize if sqlite3_malloc() fails or
31732** if the new size is the same as the prior size.
31733** Return TRUE if the resize occurs and false if not.
31734*/
31735static int rehash(Hash *pH, unsigned int new_size){
31736 struct _ht *new_ht; /* The new hash table */
31737 HashElem *elem, *next_elem; /* For looping over existing elements */
31738
31739#if SQLITE_MALLOC_SOFT_LIMIT>0
31740 if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
31741 new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
31742 }
31743 if( new_size==pH->htsize ) return 0;
31744#endif
31745
31746 /* The inability to allocates space for a larger hash table is
31747 ** a performance hit but it is not a fatal error. So mark the
31748 ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of
31749 ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero()
31750 ** only zeroes the requested number of bytes whereas this module will
31751 ** use the actual amount of space allocated for the hash table (which
31752 ** may be larger than the requested amount).
31753 */
31754 sqlite3BeginBenignMalloc();
31755 new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
31756 sqlite3EndBenignMalloc();
31757
31758 if( new_ht==0 ) return 0;
31759 sqlite3_free(pH->ht);
31760 pH->ht = new_ht;
31761 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
31762 memset(new_ht, 0, new_size*sizeof(struct _ht));
31763 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
31764 unsigned int h = strHash(elem->pKey) % new_size;
31765 next_elem = elem->next;
31766 insertElement(pH, &new_ht[h], elem);
31767 }
31768 return 1;
31769}
31770
31771/* This function (for internal use only) locates an element in an
31772** hash table that matches the given key. If no element is found,
31773** a pointer to a static null element with HashElem.data==0 is returned.
31774** If pH is not NULL, then the hash for this key is written to *pH.
31775*/
31776static HashElem *findElementWithHash(
31777 const Hash *pH, /* The pH to be searched */
31778 const char *pKey, /* The key we are searching for */
31779 unsigned int *pHash /* Write the hash value here */
31780){
31781 HashElem *elem; /* Used to loop thru the element list */
31782 int count; /* Number of elements left to test */
31783 unsigned int h; /* The computed hash */
31784 static HashElem nullElement = { 0, 0, 0, 0 };
31785
31786 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/
31787 struct _ht *pEntry;
31788 h = strHash(pKey) % pH->htsize;
31789 pEntry = &pH->ht[h];
31790 elem = pEntry->chain;
31791 count = pEntry->count;
31792 }else{
31793 h = 0;
31794 elem = pH->first;
31795 count = pH->count;
31796 }
31797 if( pHash ) *pHash = h;
31798 while( count-- ){
31799 assert( elem!=0 );
31800 if( sqlite3StrICmp(elem->pKey,pKey)==0 ){
31801 return elem;
31802 }
31803 elem = elem->next;
31804 }
31805 return &nullElement;
31806}
31807#ifdef SQLITE_ENABLE_NORMALIZE
31808static HashElem *findElementWithHashN(
31809 const Hash *pH, /* The pH to be searched */
31810 const char *pKey, /* The key we are searching for */
31811 int nKey, /* Number of key bytes to use */
31812 unsigned int *pHash /* Write the hash value here */
31813){
31814 HashElem *elem; /* Used to loop thru the element list */
31815 int count; /* Number of elements left to test */
31816 unsigned int h; /* The computed hash */
31817 static HashElem nullElement = { 0, 0, 0, 0 };
31818
31819 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/
31820 struct _ht *pEntry;
31821 h = strHashN(pKey, nKey) % pH->htsize;
31822 pEntry = &pH->ht[h];
31823 elem = pEntry->chain;
31824 count = pEntry->count;
31825 }else{
31826 h = 0;
31827 elem = pH->first;
31828 count = pH->count;
31829 }
31830 if( pHash ) *pHash = h;
31831 while( count-- ){
31832 assert( elem!=0 );
31833 if( sqlite3StrNICmp(elem->pKey,pKey,nKey)==0 ){
31834 return elem;
31835 }
31836 elem = elem->next;
31837 }
31838 return &nullElement;
31839}
31840#endif /* SQLITE_ENABLE_NORMALIZE */
31841
31842/* Remove a single entry from the hash table given a pointer to that
31843** element and a hash on the element's key.
31844*/
31845static void removeElementGivenHash(
31846 Hash *pH, /* The pH containing "elem" */
31847 HashElem* elem, /* The element to be removed from the pH */
31848 unsigned int h /* Hash value for the element */
31849){
31850 struct _ht *pEntry;
31851 if( elem->prev ){
31852 elem->prev->next = elem->next;
31853 }else{
31854 pH->first = elem->next;
31855 }
31856 if( elem->next ){
31857 elem->next->prev = elem->prev;
31858 }
31859 if( pH->ht ){
31860 pEntry = &pH->ht[h];
31861 if( pEntry->chain==elem ){
31862 pEntry->chain = elem->next;
31863 }
31864 pEntry->count--;
31865 assert( pEntry->count>=0 );
31866 }
31867 sqlite3_free( elem );
31868 pH->count--;
31869 if( pH->count==0 ){
31870 assert( pH->first==0 );
31871 assert( pH->count==0 );
31872 sqlite3HashClear(pH);
31873 }
31874}
31875
31876/* Attempt to locate an element of the hash table pH with a key
31877** that matches pKey. Return the data for this element if it is
31878** found, or NULL if there is no match.
31879*/
31880SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey){
31881 assert( pH!=0 );
31882 assert( pKey!=0 );
31883 return findElementWithHash(pH, pKey, 0)->data;
31884}
31885#ifdef SQLITE_ENABLE_NORMALIZE
31886SQLITE_PRIVATE void *sqlite3HashFindN(const Hash *pH, const char *pKey, int nKey){
31887 assert( pH!=0 );
31888 assert( pKey!=0 );
31889 assert( nKey>=0 );
31890 return findElementWithHashN(pH, pKey, nKey, 0)->data;
31891}
31892#endif /* SQLITE_ENABLE_NORMALIZE */
31893
31894/* Insert an element into the hash table pH. The key is pKey
31895** and the data is "data".
31896**
31897** If no element exists with a matching key, then a new
31898** element is created and NULL is returned.
31899**
31900** If another element already exists with the same key, then the
31901** new data replaces the old data and the old data is returned.
31902** The key is not copied in this instance. If a malloc fails, then
31903** the new data is returned and the hash table is unchanged.
31904**
31905** If the "data" parameter to this function is NULL, then the
31906** element corresponding to "key" is removed from the hash table.
31907*/
31908SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){
31909 unsigned int h; /* the hash of the key modulo hash table size */
31910 HashElem *elem; /* Used to loop thru the element list */
31911 HashElem *new_elem; /* New element added to the pH */
31912
31913 assert( pH!=0 );
31914 assert( pKey!=0 );
31915 elem = findElementWithHash(pH,pKey,&h);
31916 if( elem->data ){
31917 void *old_data = elem->data;
31918 if( data==0 ){
31919 removeElementGivenHash(pH,elem,h);
31920 }else{
31921 elem->data = data;
31922 elem->pKey = pKey;
31923 }
31924 return old_data;
31925 }
31926 if( data==0 ) return 0;
31927 new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
31928 if( new_elem==0 ) return data;
31929 new_elem->pKey = pKey;
31930 new_elem->data = data;
31931 pH->count++;
31932 if( pH->count>=10 && pH->count > 2*pH->htsize ){
31933 if( rehash(pH, pH->count*2) ){
31934 assert( pH->htsize>0 );
31935 h = strHash(pKey) % pH->htsize;
31936 }
31937 }
31938 insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem);
31939 return 0;
31940}
31941
31942/************** End of hash.c ************************************************/
31943/************** Begin file opcodes.c *****************************************/
31944/* Automatically generated. Do not edit */
31945/* See the tool/mkopcodec.tcl script for details. */
31946#if !defined(SQLITE_OMIT_EXPLAIN) \
31947 || defined(VDBE_PROFILE) \
31948 || defined(SQLITE_DEBUG)
31949#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) || defined(SQLITE_DEBUG)
31950# define OpHelp(X) "\0" X
31951#else
31952# define OpHelp(X)
31953#endif
31954SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
31955 static const char *const azName[] = {
31956 /* 0 */ "Savepoint" OpHelp(""),
31957 /* 1 */ "AutoCommit" OpHelp(""),
31958 /* 2 */ "Transaction" OpHelp(""),
31959 /* 3 */ "SorterNext" OpHelp(""),
31960 /* 4 */ "Prev" OpHelp(""),
31961 /* 5 */ "Next" OpHelp(""),
31962 /* 6 */ "Checkpoint" OpHelp(""),
31963 /* 7 */ "JournalMode" OpHelp(""),
31964 /* 8 */ "Vacuum" OpHelp(""),
31965 /* 9 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"),
31966 /* 10 */ "VUpdate" OpHelp("data=r[P3@P2]"),
31967 /* 11 */ "Goto" OpHelp(""),
31968 /* 12 */ "Gosub" OpHelp(""),
31969 /* 13 */ "InitCoroutine" OpHelp(""),
31970 /* 14 */ "Yield" OpHelp(""),
31971 /* 15 */ "MustBeInt" OpHelp(""),
31972 /* 16 */ "Jump" OpHelp(""),
31973 /* 17 */ "Once" OpHelp(""),
31974 /* 18 */ "If" OpHelp(""),
31975 /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"),
31976 /* 20 */ "IfNot" OpHelp(""),
31977 /* 21 */ "IfNullRow" OpHelp("if P1.nullRow then r[P3]=NULL, goto P2"),
31978 /* 22 */ "SeekLT" OpHelp("key=r[P3@P4]"),
31979 /* 23 */ "SeekLE" OpHelp("key=r[P3@P4]"),
31980 /* 24 */ "SeekGE" OpHelp("key=r[P3@P4]"),
31981 /* 25 */ "SeekGT" OpHelp("key=r[P3@P4]"),
31982 /* 26 */ "IfNoHope" OpHelp("key=r[P3@P4]"),
31983 /* 27 */ "NoConflict" OpHelp("key=r[P3@P4]"),
31984 /* 28 */ "NotFound" OpHelp("key=r[P3@P4]"),
31985 /* 29 */ "Found" OpHelp("key=r[P3@P4]"),
31986 /* 30 */ "SeekRowid" OpHelp("intkey=r[P3]"),
31987 /* 31 */ "NotExists" OpHelp("intkey=r[P3]"),
31988 /* 32 */ "Last" OpHelp(""),
31989 /* 33 */ "IfSmaller" OpHelp(""),
31990 /* 34 */ "SorterSort" OpHelp(""),
31991 /* 35 */ "Sort" OpHelp(""),
31992 /* 36 */ "Rewind" OpHelp(""),
31993 /* 37 */ "IdxLE" OpHelp("key=r[P3@P4]"),
31994 /* 38 */ "IdxGT" OpHelp("key=r[P3@P4]"),
31995 /* 39 */ "IdxLT" OpHelp("key=r[P3@P4]"),
31996 /* 40 */ "IdxGE" OpHelp("key=r[P3@P4]"),
31997 /* 41 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
31998 /* 42 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
31999 /* 43 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"),
32000 /* 44 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"),
32001 /* 45 */ "Program" OpHelp(""),
32002 /* 46 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
32003 /* 47 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
32004 /* 48 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
32005 /* 49 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
32006 /* 50 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"),
32007 /* 51 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"),
32008 /* 52 */ "Ne" OpHelp("IF r[P3]!=r[P1]"),
32009 /* 53 */ "Eq" OpHelp("IF r[P3]==r[P1]"),
32010 /* 54 */ "Gt" OpHelp("IF r[P3]>r[P1]"),
32011 /* 55 */ "Le" OpHelp("IF r[P3]<=r[P1]"),
32012 /* 56 */ "Lt" OpHelp("IF r[P3]<r[P1]"),
32013 /* 57 */ "Ge" OpHelp("IF r[P3]>=r[P1]"),
32014 /* 58 */ "ElseNotEq" OpHelp(""),
32015 /* 59 */ "IncrVacuum" OpHelp(""),
32016 /* 60 */ "VNext" OpHelp(""),
32017 /* 61 */ "Init" OpHelp("Start at P2"),
32018 /* 62 */ "PureFunc0" OpHelp(""),
32019 /* 63 */ "Function0" OpHelp("r[P3]=func(r[P2@P5])"),
32020 /* 64 */ "PureFunc" OpHelp(""),
32021 /* 65 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"),
32022 /* 66 */ "Return" OpHelp(""),
32023 /* 67 */ "EndCoroutine" OpHelp(""),
32024 /* 68 */ "HaltIfNull" OpHelp("if r[P3]=null halt"),
32025 /* 69 */ "Halt" OpHelp(""),
32026 /* 70 */ "Integer" OpHelp("r[P2]=P1"),
32027 /* 71 */ "Int64" OpHelp("r[P2]=P4"),
32028 /* 72 */ "String" OpHelp("r[P2]='P4' (len=P1)"),
32029 /* 73 */ "Null" OpHelp("r[P2..P3]=NULL"),
32030 /* 74 */ "SoftNull" OpHelp("r[P1]=NULL"),
32031 /* 75 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"),
32032 /* 76 */ "Variable" OpHelp("r[P2]=parameter(P1,P4)"),
32033 /* 77 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"),
32034 /* 78 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
32035 /* 79 */ "SCopy" OpHelp("r[P2]=r[P1]"),
32036 /* 80 */ "IntCopy" OpHelp("r[P2]=r[P1]"),
32037 /* 81 */ "ResultRow" OpHelp("output=r[P1@P2]"),
32038 /* 82 */ "CollSeq" OpHelp(""),
32039 /* 83 */ "AddImm" OpHelp("r[P1]=r[P1]+P2"),
32040 /* 84 */ "RealAffinity" OpHelp(""),
32041 /* 85 */ "Cast" OpHelp("affinity(r[P1])"),
32042 /* 86 */ "Permutation" OpHelp(""),
32043 /* 87 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
32044 /* 88 */ "IsTrue" OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"),
32045 /* 89 */ "Offset" OpHelp("r[P3] = sqlite_offset(P1)"),
32046 /* 90 */ "Column" OpHelp("r[P3]=PX"),
32047 /* 91 */ "Affinity" OpHelp("affinity(r[P1@P2])"),
32048 /* 92 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"),
32049 /* 93 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"),
32050 /* 94 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"),
32051 /* 95 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"),
32052 /* 96 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"),
32053 /* 97 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
32054 /* 98 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"),
32055 /* 99 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
32056 /* 100 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
32057 /* 101 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
32058 /* 102 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"),
32059 /* 103 */ "BitNot" OpHelp("r[P2]= ~r[P1]"),
32060 /* 104 */ "Count" OpHelp("r[P2]=count()"),
32061 /* 105 */ "ReadCookie" OpHelp(""),
32062 /* 106 */ "String8" OpHelp("r[P2]='P4'"),
32063 /* 107 */ "SetCookie" OpHelp(""),
32064 /* 108 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"),
32065 /* 109 */ "OpenRead" OpHelp("root=P2 iDb=P3"),
32066 /* 110 */ "OpenWrite" OpHelp("root=P2 iDb=P3"),
32067 /* 111 */ "OpenDup" OpHelp(""),
32068 /* 112 */ "OpenAutoindex" OpHelp("nColumn=P2"),
32069 /* 113 */ "OpenEphemeral" OpHelp("nColumn=P2"),
32070 /* 114 */ "SorterOpen" OpHelp(""),
32071 /* 115 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
32072 /* 116 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"),
32073 /* 117 */ "Close" OpHelp(""),
32074 /* 118 */ "ColumnsUsed" OpHelp(""),
32075 /* 119 */ "SeekHit" OpHelp("seekHit=P2"),
32076 /* 120 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
32077 /* 121 */ "NewRowid" OpHelp("r[P2]=rowid"),
32078 /* 122 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
32079 /* 123 */ "InsertInt" OpHelp("intkey=P3 data=r[P2]"),
32080 /* 124 */ "Delete" OpHelp(""),
32081 /* 125 */ "ResetCount" OpHelp(""),
32082 /* 126 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
32083 /* 127 */ "SorterData" OpHelp("r[P2]=data"),
32084 /* 128 */ "RowData" OpHelp("r[P2]=data"),
32085 /* 129 */ "Rowid" OpHelp("r[P2]=rowid"),
32086 /* 130 */ "NullRow" OpHelp(""),
32087 /* 131 */ "SeekEnd" OpHelp(""),
32088 /* 132 */ "SorterInsert" OpHelp("key=r[P2]"),
32089 /* 133 */ "IdxInsert" OpHelp("key=r[P2]"),
32090 /* 134 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
32091 /* 135 */ "DeferredSeek" OpHelp("Move P3 to P1.rowid if needed"),
32092 /* 136 */ "IdxRowid" OpHelp("r[P2]=rowid"),
32093 /* 137 */ "Destroy" OpHelp(""),
32094 /* 138 */ "Clear" OpHelp(""),
32095 /* 139 */ "ResetSorter" OpHelp(""),
32096 /* 140 */ "CreateBtree" OpHelp("r[P2]=root iDb=P1 flags=P3"),
32097 /* 141 */ "Real" OpHelp("r[P2]=P4"),
32098 /* 142 */ "SqlExec" OpHelp(""),
32099 /* 143 */ "ParseSchema" OpHelp(""),
32100 /* 144 */ "LoadAnalysis" OpHelp(""),
32101 /* 145 */ "DropTable" OpHelp(""),
32102 /* 146 */ "DropIndex" OpHelp(""),
32103 /* 147 */ "DropTrigger" OpHelp(""),
32104 /* 148 */ "IntegrityCk" OpHelp(""),
32105 /* 149 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
32106 /* 150 */ "Param" OpHelp(""),
32107 /* 151 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
32108 /* 152 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
32109 /* 153 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
32110 /* 154 */ "AggInverse" OpHelp("accum=r[P3] inverse(r[P2@P5])"),
32111 /* 155 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"),
32112 /* 156 */ "AggStep1" OpHelp("accum=r[P3] step(r[P2@P5])"),
32113 /* 157 */ "AggValue" OpHelp("r[P3]=value N=P2"),
32114 /* 158 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
32115 /* 159 */ "Expire" OpHelp(""),
32116 /* 160 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
32117 /* 161 */ "VBegin" OpHelp(""),
32118 /* 162 */ "VCreate" OpHelp(""),
32119 /* 163 */ "VDestroy" OpHelp(""),
32120 /* 164 */ "VOpen" OpHelp(""),
32121 /* 165 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
32122 /* 166 */ "VRename" OpHelp(""),
32123 /* 167 */ "Pagecount" OpHelp(""),
32124 /* 168 */ "MaxPgcnt" OpHelp(""),
32125 /* 169 */ "Trace" OpHelp(""),
32126 /* 170 */ "CursorHint" OpHelp(""),
32127 /* 171 */ "Noop" OpHelp(""),
32128 /* 172 */ "Explain" OpHelp(""),
32129 /* 173 */ "Abortable" OpHelp(""),
32130 };
32131 return azName[i];
32132}
32133#endif
32134
32135/************** End of opcodes.c *********************************************/
32136/************** Begin file os_unix.c *****************************************/
32137/*
32138** 2004 May 22
32139**
32140** The author disclaims copyright to this source code. In place of
32141** a legal notice, here is a blessing:
32142**
32143** May you do good and not evil.
32144** May you find forgiveness for yourself and forgive others.
32145** May you share freely, never taking more than you give.
32146**
32147******************************************************************************
32148**
32149** This file contains the VFS implementation for unix-like operating systems
32150** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
32151**
32152** There are actually several different VFS implementations in this file.
32153** The differences are in the way that file locking is done. The default
32154** implementation uses Posix Advisory Locks. Alternative implementations
32155** use flock(), dot-files, various proprietary locking schemas, or simply
32156** skip locking all together.
32157**
32158** This source file is organized into divisions where the logic for various
32159** subfunctions is contained within the appropriate division. PLEASE
32160** KEEP THE STRUCTURE OF THIS FILE INTACT. New code should be placed
32161** in the correct division and should be clearly labeled.
32162**
32163** The layout of divisions is as follows:
32164**
32165** * General-purpose declarations and utility functions.
32166** * Unique file ID logic used by VxWorks.
32167** * Various locking primitive implementations (all except proxy locking):
32168** + for Posix Advisory Locks
32169** + for no-op locks
32170** + for dot-file locks
32171** + for flock() locking
32172** + for named semaphore locks (VxWorks only)
32173** + for AFP filesystem locks (MacOSX only)
32174** * sqlite3_file methods not associated with locking.
32175** * Definitions of sqlite3_io_methods objects for all locking
32176** methods plus "finder" functions for each locking method.
32177** * sqlite3_vfs method implementations.
32178** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
32179** * Definitions of sqlite3_vfs objects for all locking methods
32180** plus implementations of sqlite3_os_init() and sqlite3_os_end().
32181*/
32182/* #include "sqliteInt.h" */
32183#if SQLITE_OS_UNIX /* This file is used on unix only */
32184
32185/*
32186** There are various methods for file locking used for concurrency
32187** control:
32188**
32189** 1. POSIX locking (the default),
32190** 2. No locking,
32191** 3. Dot-file locking,
32192** 4. flock() locking,
32193** 5. AFP locking (OSX only),
32194** 6. Named POSIX semaphores (VXWorks only),
32195** 7. proxy locking. (OSX only)
32196**
32197** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
32198** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
32199** selection of the appropriate locking style based on the filesystem
32200** where the database is located.
32201*/
32202#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
32203# if defined(__APPLE__)
32204# define SQLITE_ENABLE_LOCKING_STYLE 1
32205# else
32206# define SQLITE_ENABLE_LOCKING_STYLE 0
32207# endif
32208#endif
32209
32210/* Use pread() and pwrite() if they are available */
32211#if defined(__APPLE__)
32212# define HAVE_PREAD 1
32213# define HAVE_PWRITE 1
32214#endif
32215#if defined(HAVE_PREAD64) && defined(HAVE_PWRITE64)
32216# undef USE_PREAD
32217# define USE_PREAD64 1
32218#elif defined(HAVE_PREAD) && defined(HAVE_PWRITE)
32219# undef USE_PREAD64
32220# define USE_PREAD 1
32221#endif
32222
32223/*
32224** standard include files.
32225*/
32226#include <sys/types.h>
32227#include <sys/stat.h>
32228#include <fcntl.h>
32229#include <sys/ioctl.h>
32230#include <unistd.h>
32231/* #include <time.h> */
32232#include <sys/time.h>
32233#include <errno.h>
32234#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
32235# include <sys/mman.h>
32236#endif
32237
32238#if SQLITE_ENABLE_LOCKING_STYLE
32239/* # include <sys/ioctl.h> */
32240# include <sys/file.h>
32241# include <sys/param.h>
32242#endif /* SQLITE_ENABLE_LOCKING_STYLE */
32243
32244#if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \
32245 (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000))
32246# if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \
32247 && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0))
32248# define HAVE_GETHOSTUUID 1
32249# else
32250# warning "gethostuuid() is disabled."
32251# endif
32252#endif
32253
32254
32255#if OS_VXWORKS
32256/* # include <sys/ioctl.h> */
32257# include <semaphore.h>
32258# include <limits.h>
32259#endif /* OS_VXWORKS */
32260
32261#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
32262# include <sys/mount.h>
32263#endif
32264
32265#ifdef HAVE_UTIME
32266# include <utime.h>
32267#endif
32268
32269/*
32270** Allowed values of unixFile.fsFlags
32271*/
32272#define SQLITE_FSFLAGS_IS_MSDOS 0x1
32273
32274/*
32275** If we are to be thread-safe, include the pthreads header.
32276*/
32277#if SQLITE_THREADSAFE
32278/* # include <pthread.h> */
32279#endif
32280
32281/*
32282** Default permissions when creating a new file
32283*/
32284#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
32285# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
32286#endif
32287
32288/*
32289** Default permissions when creating auto proxy dir
32290*/
32291#ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
32292# define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
32293#endif
32294
32295/*
32296** Maximum supported path-length.
32297*/
32298#define MAX_PATHNAME 512
32299
32300/*
32301** Maximum supported symbolic links
32302*/
32303#define SQLITE_MAX_SYMLINKS 100
32304
32305/* Always cast the getpid() return type for compatibility with
32306** kernel modules in VxWorks. */
32307#define osGetpid(X) (pid_t)getpid()
32308
32309/*
32310** Only set the lastErrno if the error code is a real error and not
32311** a normal expected return code of SQLITE_BUSY or SQLITE_OK
32312*/
32313#define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY))
32314
32315/* Forward references */
32316typedef struct unixShm unixShm; /* Connection shared memory */
32317typedef struct unixShmNode unixShmNode; /* Shared memory instance */
32318typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
32319typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */
32320
32321/*
32322** Sometimes, after a file handle is closed by SQLite, the file descriptor
32323** cannot be closed immediately. In these cases, instances of the following
32324** structure are used to store the file descriptor while waiting for an
32325** opportunity to either close or reuse it.
32326*/
32327struct UnixUnusedFd {
32328 int fd; /* File descriptor to close */
32329 int flags; /* Flags this file descriptor was opened with */
32330 UnixUnusedFd *pNext; /* Next unused file descriptor on same file */
32331};
32332
32333/*
32334** The unixFile structure is subclass of sqlite3_file specific to the unix
32335** VFS implementations.
32336*/
32337typedef struct unixFile unixFile;
32338struct unixFile {
32339 sqlite3_io_methods const *pMethod; /* Always the first entry */
32340 sqlite3_vfs *pVfs; /* The VFS that created this unixFile */
32341 unixInodeInfo *pInode; /* Info about locks on this inode */
32342 int h; /* The file descriptor */
32343 unsigned char eFileLock; /* The type of lock held on this fd */
32344 unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */
32345 int lastErrno; /* The unix errno from last I/O error */
32346 void *lockingContext; /* Locking style specific state */
32347 UnixUnusedFd *pPreallocatedUnused; /* Pre-allocated UnixUnusedFd */
32348 const char *zPath; /* Name of the file */
32349 unixShm *pShm; /* Shared memory segment information */
32350 int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
32351#if SQLITE_MAX_MMAP_SIZE>0
32352 int nFetchOut; /* Number of outstanding xFetch refs */
32353 sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */
32354 sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */
32355 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
32356 void *pMapRegion; /* Memory mapped region */
32357#endif
32358 int sectorSize; /* Device sector size */
32359 int deviceCharacteristics; /* Precomputed device characteristics */
32360#if SQLITE_ENABLE_LOCKING_STYLE
32361 int openFlags; /* The flags specified at open() */
32362#endif
32363#if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
32364 unsigned fsFlags; /* cached details from statfs() */
32365#endif
32366#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
32367 unsigned iBusyTimeout; /* Wait this many millisec on locks */
32368#endif
32369#if OS_VXWORKS
32370 struct vxworksFileId *pId; /* Unique file ID */
32371#endif
32372#ifdef SQLITE_DEBUG
32373 /* The next group of variables are used to track whether or not the
32374 ** transaction counter in bytes 24-27 of database files are updated
32375 ** whenever any part of the database changes. An assertion fault will
32376 ** occur if a file is updated without also updating the transaction
32377 ** counter. This test is made to avoid new problems similar to the
32378 ** one described by ticket #3584.
32379 */
32380 unsigned char transCntrChng; /* True if the transaction counter changed */
32381 unsigned char dbUpdate; /* True if any part of database file changed */
32382 unsigned char inNormalWrite; /* True if in a normal write operation */
32383
32384#endif
32385
32386#ifdef SQLITE_TEST
32387 /* In test mode, increase the size of this structure a bit so that
32388 ** it is larger than the struct CrashFile defined in test6.c.
32389 */
32390 char aPadding[32];
32391#endif
32392};
32393
32394/* This variable holds the process id (pid) from when the xRandomness()
32395** method was called. If xOpen() is called from a different process id,
32396** indicating that a fork() has occurred, the PRNG will be reset.
32397*/
32398static pid_t randomnessPid = 0;
32399
32400/*
32401** Allowed values for the unixFile.ctrlFlags bitmask:
32402*/
32403#define UNIXFILE_EXCL 0x01 /* Connections from one process only */
32404#define UNIXFILE_RDONLY 0x02 /* Connection is read only */
32405#define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
32406#ifndef SQLITE_DISABLE_DIRSYNC
32407# define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */
32408#else
32409# define UNIXFILE_DIRSYNC 0x00
32410#endif
32411#define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
32412#define UNIXFILE_DELETE 0x20 /* Delete on close */
32413#define UNIXFILE_URI 0x40 /* Filename might have query parameters */
32414#define UNIXFILE_NOLOCK 0x80 /* Do no file locking */
32415
32416/*
32417** Include code that is common to all os_*.c files
32418*/
32419/************** Include os_common.h in the middle of os_unix.c ***************/
32420/************** Begin file os_common.h ***************************************/
32421/*
32422** 2004 May 22
32423**
32424** The author disclaims copyright to this source code. In place of
32425** a legal notice, here is a blessing:
32426**
32427** May you do good and not evil.
32428** May you find forgiveness for yourself and forgive others.
32429** May you share freely, never taking more than you give.
32430**
32431******************************************************************************
32432**
32433** This file contains macros and a little bit of code that is common to
32434** all of the platform-specific files (os_*.c) and is #included into those
32435** files.
32436**
32437** This file should be #included by the os_*.c files only. It is not a
32438** general purpose header file.
32439*/
32440#ifndef _OS_COMMON_H_
32441#define _OS_COMMON_H_
32442
32443/*
32444** At least two bugs have slipped in because we changed the MEMORY_DEBUG
32445** macro to SQLITE_DEBUG and some older makefiles have not yet made the
32446** switch. The following code should catch this problem at compile-time.
32447*/
32448#ifdef MEMORY_DEBUG
32449# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
32450#endif
32451
32452/*
32453** Macros for performance tracing. Normally turned off. Only works
32454** on i486 hardware.
32455*/
32456#ifdef SQLITE_PERFORMANCE_TRACE
32457
32458/*
32459** hwtime.h contains inline assembler code for implementing
32460** high-performance timing routines.
32461*/
32462/************** Include hwtime.h in the middle of os_common.h ****************/
32463/************** Begin file hwtime.h ******************************************/
32464/*
32465** 2008 May 27
32466**
32467** The author disclaims copyright to this source code. In place of
32468** a legal notice, here is a blessing:
32469**
32470** May you do good and not evil.
32471** May you find forgiveness for yourself and forgive others.
32472** May you share freely, never taking more than you give.
32473**
32474******************************************************************************
32475**
32476** This file contains inline asm code for retrieving "high-performance"
32477** counters for x86 class CPUs.
32478*/
32479#ifndef SQLITE_HWTIME_H
32480#define SQLITE_HWTIME_H
32481
32482/*
32483** The following routine only works on pentium-class (or newer) processors.
32484** It uses the RDTSC opcode to read the cycle count value out of the
32485** processor and returns that value. This can be used for high-res
32486** profiling.
32487*/
32488#if (defined(__GNUC__) || defined(_MSC_VER)) && \
32489 (defined(i386) || defined(__i386__) || defined(_M_IX86))
32490
32491 #if defined(__GNUC__)
32492
32493 __inline__ sqlite_uint64 sqlite3Hwtime(void){
32494 unsigned int lo, hi;
32495 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
32496 return (sqlite_uint64)hi << 32 | lo;
32497 }
32498
32499 #elif defined(_MSC_VER)
32500
32501 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
32502 __asm {
32503 rdtsc
32504 ret ; return value at EDX:EAX
32505 }
32506 }
32507
32508 #endif
32509
32510#elif (defined(__GNUC__) && defined(__x86_64__))
32511
32512 __inline__ sqlite_uint64 sqlite3Hwtime(void){
32513 unsigned long val;
32514 __asm__ __volatile__ ("rdtsc" : "=A" (val));
32515 return val;
32516 }
32517
32518#elif (defined(__GNUC__) && defined(__ppc__))
32519
32520 __inline__ sqlite_uint64 sqlite3Hwtime(void){
32521 unsigned long long retval;
32522 unsigned long junk;
32523 __asm__ __volatile__ ("\n\
32524 1: mftbu %1\n\
32525 mftb %L0\n\
32526 mftbu %0\n\
32527 cmpw %0,%1\n\
32528 bne 1b"
32529 : "=r" (retval), "=r" (junk));
32530 return retval;
32531 }
32532
32533#else
32534
32535 #error Need implementation of sqlite3Hwtime() for your platform.
32536
32537 /*
32538 ** To compile without implementing sqlite3Hwtime() for your platform,
32539 ** you can remove the above #error and use the following
32540 ** stub function. You will lose timing support for many
32541 ** of the debugging and testing utilities, but it should at
32542 ** least compile and run.
32543 */
32544SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
32545
32546#endif
32547
32548#endif /* !defined(SQLITE_HWTIME_H) */
32549
32550/************** End of hwtime.h **********************************************/
32551/************** Continuing where we left off in os_common.h ******************/
32552
32553static sqlite_uint64 g_start;
32554static sqlite_uint64 g_elapsed;
32555#define TIMER_START g_start=sqlite3Hwtime()
32556#define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
32557#define TIMER_ELAPSED g_elapsed
32558#else
32559#define TIMER_START
32560#define TIMER_END
32561#define TIMER_ELAPSED ((sqlite_uint64)0)
32562#endif
32563
32564/*
32565** If we compile with the SQLITE_TEST macro set, then the following block
32566** of code will give us the ability to simulate a disk I/O error. This
32567** is used for testing the I/O recovery logic.
32568*/
32569#if defined(SQLITE_TEST)
32570SQLITE_API extern int sqlite3_io_error_hit;
32571SQLITE_API extern int sqlite3_io_error_hardhit;
32572SQLITE_API extern int sqlite3_io_error_pending;
32573SQLITE_API extern int sqlite3_io_error_persist;
32574SQLITE_API extern int sqlite3_io_error_benign;
32575SQLITE_API extern int sqlite3_diskfull_pending;
32576SQLITE_API extern int sqlite3_diskfull;
32577#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
32578#define SimulateIOError(CODE) \
32579 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
32580 || sqlite3_io_error_pending-- == 1 ) \
32581 { local_ioerr(); CODE; }
32582static void local_ioerr(){
32583 IOTRACE(("IOERR\n"));
32584 sqlite3_io_error_hit++;
32585 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
32586}
32587#define SimulateDiskfullError(CODE) \
32588 if( sqlite3_diskfull_pending ){ \
32589 if( sqlite3_diskfull_pending == 1 ){ \
32590 local_ioerr(); \
32591 sqlite3_diskfull = 1; \
32592 sqlite3_io_error_hit = 1; \
32593 CODE; \
32594 }else{ \
32595 sqlite3_diskfull_pending--; \
32596 } \
32597 }
32598#else
32599#define SimulateIOErrorBenign(X)
32600#define SimulateIOError(A)
32601#define SimulateDiskfullError(A)
32602#endif /* defined(SQLITE_TEST) */
32603
32604/*
32605** When testing, keep a count of the number of open files.
32606*/
32607#if defined(SQLITE_TEST)
32608SQLITE_API extern int sqlite3_open_file_count;
32609#define OpenCounter(X) sqlite3_open_file_count+=(X)
32610#else
32611#define OpenCounter(X)
32612#endif /* defined(SQLITE_TEST) */
32613
32614#endif /* !defined(_OS_COMMON_H_) */
32615
32616/************** End of os_common.h *******************************************/
32617/************** Continuing where we left off in os_unix.c ********************/
32618
32619/*
32620** Define various macros that are missing from some systems.
32621*/
32622#ifndef O_LARGEFILE
32623# define O_LARGEFILE 0
32624#endif
32625#ifdef SQLITE_DISABLE_LFS
32626# undef O_LARGEFILE
32627# define O_LARGEFILE 0
32628#endif
32629#ifndef O_NOFOLLOW
32630# define O_NOFOLLOW 0
32631#endif
32632#ifndef O_BINARY
32633# define O_BINARY 0
32634#endif
32635
32636/*
32637** The threadid macro resolves to the thread-id or to 0. Used for
32638** testing and debugging only.
32639*/
32640#if SQLITE_THREADSAFE
32641#define threadid pthread_self()
32642#else
32643#define threadid 0
32644#endif
32645
32646/*
32647** HAVE_MREMAP defaults to true on Linux and false everywhere else.
32648*/
32649#if !defined(HAVE_MREMAP)
32650# if defined(__linux__) && defined(_GNU_SOURCE)
32651# define HAVE_MREMAP 1
32652# else
32653# define HAVE_MREMAP 0
32654# endif
32655#endif
32656
32657/*
32658** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
32659** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
32660*/
32661#ifdef __ANDROID__
32662# define lseek lseek64
32663#endif
32664
32665#ifdef __linux__
32666/*
32667** Linux-specific IOCTL magic numbers used for controlling F2FS
32668*/
32669#define F2FS_IOCTL_MAGIC 0xf5
32670#define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1)
32671#define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2)
32672#define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3)
32673#define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5)
32674#define F2FS_IOC_GET_FEATURES _IOR(F2FS_IOCTL_MAGIC, 12, u32)
32675#define F2FS_FEATURE_ATOMIC_WRITE 0x0004
32676#endif /* __linux__ */
32677
32678
32679/*
32680** Different Unix systems declare open() in different ways. Same use
32681** open(const char*,int,mode_t). Others use open(const char*,int,...).
32682** The difference is important when using a pointer to the function.
32683**
32684** The safest way to deal with the problem is to always use this wrapper
32685** which always has the same well-defined interface.
32686*/
32687static int posixOpen(const char *zFile, int flags, int mode){
32688 return open(zFile, flags, mode);
32689}
32690
32691/* Forward reference */
32692static int openDirectory(const char*, int*);
32693static int unixGetpagesize(void);
32694
32695/*
32696** Many system calls are accessed through pointer-to-functions so that
32697** they may be overridden at runtime to facilitate fault injection during
32698** testing and sandboxing. The following array holds the names and pointers
32699** to all overrideable system calls.
32700*/
32701static struct unix_syscall {
32702 const char *zName; /* Name of the system call */
32703 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
32704 sqlite3_syscall_ptr pDefault; /* Default value */
32705} aSyscall[] = {
32706 { "open", (sqlite3_syscall_ptr)posixOpen, 0 },
32707#define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
32708
32709 { "close", (sqlite3_syscall_ptr)close, 0 },
32710#define osClose ((int(*)(int))aSyscall[1].pCurrent)
32711
32712 { "access", (sqlite3_syscall_ptr)access, 0 },
32713#define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent)
32714
32715 { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 },
32716#define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
32717
32718 { "stat", (sqlite3_syscall_ptr)stat, 0 },
32719#define osStat ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
32720
32721/*
32722** The DJGPP compiler environment looks mostly like Unix, but it
32723** lacks the fcntl() system call. So redefine fcntl() to be something
32724** that always succeeds. This means that locking does not occur under
32725** DJGPP. But it is DOS - what did you expect?
32726*/
32727#ifdef __DJGPP__
32728 { "fstat", 0, 0 },
32729#define osFstat(a,b,c) 0
32730#else
32731 { "fstat", (sqlite3_syscall_ptr)fstat, 0 },
32732#define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
32733#endif
32734
32735 { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 },
32736#define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent)
32737
32738 { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 },
32739#define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent)
32740
32741 { "read", (sqlite3_syscall_ptr)read, 0 },
32742#define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
32743
32744#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
32745 { "pread", (sqlite3_syscall_ptr)pread, 0 },
32746#else
32747 { "pread", (sqlite3_syscall_ptr)0, 0 },
32748#endif
32749#define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
32750
32751#if defined(USE_PREAD64)
32752 { "pread64", (sqlite3_syscall_ptr)pread64, 0 },
32753#else
32754 { "pread64", (sqlite3_syscall_ptr)0, 0 },
32755#endif
32756#define osPread64 ((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent)
32757
32758 { "write", (sqlite3_syscall_ptr)write, 0 },
32759#define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
32760
32761#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
32762 { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 },
32763#else
32764 { "pwrite", (sqlite3_syscall_ptr)0, 0 },
32765#endif
32766#define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\
32767 aSyscall[12].pCurrent)
32768
32769#if defined(USE_PREAD64)
32770 { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 },
32771#else
32772 { "pwrite64", (sqlite3_syscall_ptr)0, 0 },
32773#endif
32774#define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off64_t))\
32775 aSyscall[13].pCurrent)
32776
32777 { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 },
32778#define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent)
32779
32780#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
32781 { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 },
32782#else
32783 { "fallocate", (sqlite3_syscall_ptr)0, 0 },
32784#endif
32785#define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
32786
32787 { "unlink", (sqlite3_syscall_ptr)unlink, 0 },
32788#define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent)
32789
32790 { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 },
32791#define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
32792
32793 { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 },
32794#define osMkdir ((int(*)(const char*,mode_t))aSyscall[18].pCurrent)
32795
32796 { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 },
32797#define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent)
32798
32799#if defined(HAVE_FCHOWN)
32800 { "fchown", (sqlite3_syscall_ptr)fchown, 0 },
32801#else
32802 { "fchown", (sqlite3_syscall_ptr)0, 0 },
32803#endif
32804#define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
32805
32806#if defined(HAVE_FCHOWN)
32807 { "geteuid", (sqlite3_syscall_ptr)geteuid, 0 },
32808#else
32809 { "geteuid", (sqlite3_syscall_ptr)0, 0 },
32810#endif
32811#define osGeteuid ((uid_t(*)(void))aSyscall[21].pCurrent)
32812
32813#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
32814 { "mmap", (sqlite3_syscall_ptr)mmap, 0 },
32815#else
32816 { "mmap", (sqlite3_syscall_ptr)0, 0 },
32817#endif
32818#define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent)
32819
32820#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
32821 { "munmap", (sqlite3_syscall_ptr)munmap, 0 },
32822#else
32823 { "munmap", (sqlite3_syscall_ptr)0, 0 },
32824#endif
32825#define osMunmap ((int(*)(void*,size_t))aSyscall[23].pCurrent)
32826
32827#if HAVE_MREMAP && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
32828 { "mremap", (sqlite3_syscall_ptr)mremap, 0 },
32829#else
32830 { "mremap", (sqlite3_syscall_ptr)0, 0 },
32831#endif
32832#define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent)
32833
32834#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
32835 { "getpagesize", (sqlite3_syscall_ptr)unixGetpagesize, 0 },
32836#else
32837 { "getpagesize", (sqlite3_syscall_ptr)0, 0 },
32838#endif
32839#define osGetpagesize ((int(*)(void))aSyscall[25].pCurrent)
32840
32841#if defined(HAVE_READLINK)
32842 { "readlink", (sqlite3_syscall_ptr)readlink, 0 },
32843#else
32844 { "readlink", (sqlite3_syscall_ptr)0, 0 },
32845#endif
32846#define osReadlink ((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent)
32847
32848#if defined(HAVE_LSTAT)
32849 { "lstat", (sqlite3_syscall_ptr)lstat, 0 },
32850#else
32851 { "lstat", (sqlite3_syscall_ptr)0, 0 },
32852#endif
32853#define osLstat ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)
32854
32855#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
32856# ifdef __ANDROID__
32857 { "ioctl", (sqlite3_syscall_ptr)(int(*)(int, int, ...))ioctl, 0 },
32858# else
32859 { "ioctl", (sqlite3_syscall_ptr)ioctl, 0 },
32860# endif
32861#else
32862 { "ioctl", (sqlite3_syscall_ptr)0, 0 },
32863#endif
32864#define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent)
32865
32866}; /* End of the overrideable system calls */
32867
32868
32869/*
32870** On some systems, calls to fchown() will trigger a message in a security
32871** log if they come from non-root processes. So avoid calling fchown() if
32872** we are not running as root.
32873*/
32874static int robustFchown(int fd, uid_t uid, gid_t gid){
32875#if defined(HAVE_FCHOWN)
32876 return osGeteuid() ? 0 : osFchown(fd,uid,gid);
32877#else
32878 return 0;
32879#endif
32880}
32881
32882/*
32883** This is the xSetSystemCall() method of sqlite3_vfs for all of the
32884** "unix" VFSes. Return SQLITE_OK opon successfully updating the
32885** system call pointer, or SQLITE_NOTFOUND if there is no configurable
32886** system call named zName.
32887*/
32888static int unixSetSystemCall(
32889 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
32890 const char *zName, /* Name of system call to override */
32891 sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
32892){
32893 unsigned int i;
32894 int rc = SQLITE_NOTFOUND;
32895
32896 UNUSED_PARAMETER(pNotUsed);
32897 if( zName==0 ){
32898 /* If no zName is given, restore all system calls to their default
32899 ** settings and return NULL
32900 */
32901 rc = SQLITE_OK;
32902 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
32903 if( aSyscall[i].pDefault ){
32904 aSyscall[i].pCurrent = aSyscall[i].pDefault;
32905 }
32906 }
32907 }else{
32908 /* If zName is specified, operate on only the one system call
32909 ** specified.
32910 */
32911 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
32912 if( strcmp(zName, aSyscall[i].zName)==0 ){
32913 if( aSyscall[i].pDefault==0 ){
32914 aSyscall[i].pDefault = aSyscall[i].pCurrent;
32915 }
32916 rc = SQLITE_OK;
32917 if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
32918 aSyscall[i].pCurrent = pNewFunc;
32919 break;
32920 }
32921 }
32922 }
32923 return rc;
32924}
32925
32926/*
32927** Return the value of a system call. Return NULL if zName is not a
32928** recognized system call name. NULL is also returned if the system call
32929** is currently undefined.
32930*/
32931static sqlite3_syscall_ptr unixGetSystemCall(
32932 sqlite3_vfs *pNotUsed,
32933 const char *zName
32934){
32935 unsigned int i;
32936
32937 UNUSED_PARAMETER(pNotUsed);
32938 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
32939 if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
32940 }
32941 return 0;
32942}
32943
32944/*
32945** Return the name of the first system call after zName. If zName==NULL
32946** then return the name of the first system call. Return NULL if zName
32947** is the last system call or if zName is not the name of a valid
32948** system call.
32949*/
32950static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
32951 int i = -1;
32952
32953 UNUSED_PARAMETER(p);
32954 if( zName ){
32955 for(i=0; i<ArraySize(aSyscall)-1; i++){
32956 if( strcmp(zName, aSyscall[i].zName)==0 ) break;
32957 }
32958 }
32959 for(i++; i<ArraySize(aSyscall); i++){
32960 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
32961 }
32962 return 0;
32963}
32964
32965/*
32966** Do not accept any file descriptor less than this value, in order to avoid
32967** opening database file using file descriptors that are commonly used for
32968** standard input, output, and error.
32969*/
32970#ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR
32971# define SQLITE_MINIMUM_FILE_DESCRIPTOR 3
32972#endif
32973
32974/*
32975** Invoke open(). Do so multiple times, until it either succeeds or
32976** fails for some reason other than EINTR.
32977**
32978** If the file creation mode "m" is 0 then set it to the default for
32979** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally
32980** 0644) as modified by the system umask. If m is not 0, then
32981** make the file creation mode be exactly m ignoring the umask.
32982**
32983** The m parameter will be non-zero only when creating -wal, -journal,
32984** and -shm files. We want those files to have *exactly* the same
32985** permissions as their original database, unadulterated by the umask.
32986** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
32987** transaction crashes and leaves behind hot journals, then any
32988** process that is able to write to the database will also be able to
32989** recover the hot journals.
32990*/
32991static int robust_open(const char *z, int f, mode_t m){
32992 int fd;
32993 mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS;
32994 while(1){
32995#if defined(O_CLOEXEC)
32996 fd = osOpen(z,f|O_CLOEXEC,m2);
32997#else
32998 fd = osOpen(z,f,m2);
32999#endif
33000 if( fd<0 ){
33001 if( errno==EINTR ) continue;
33002 break;
33003 }
33004 if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break;
33005 osClose(fd);
33006 sqlite3_log(SQLITE_WARNING,
33007 "attempt to open \"%s\" as file descriptor %d", z, fd);
33008 fd = -1;
33009 if( osOpen("/dev/null", f, m)<0 ) break;
33010 }
33011 if( fd>=0 ){
33012 if( m!=0 ){
33013 struct stat statbuf;
33014 if( osFstat(fd, &statbuf)==0
33015 && statbuf.st_size==0
33016 && (statbuf.st_mode&0777)!=m
33017 ){
33018 osFchmod(fd, m);
33019 }
33020 }
33021#if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0)
33022 osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
33023#endif
33024 }
33025 return fd;
33026}
33027
33028/*
33029** Helper functions to obtain and relinquish the global mutex. The
33030** global mutex is used to protect the unixInodeInfo and
33031** vxworksFileId objects used by this file, all of which may be
33032** shared by multiple threads.
33033**
33034** Function unixMutexHeld() is used to assert() that the global mutex
33035** is held when required. This function is only used as part of assert()
33036** statements. e.g.
33037**
33038** unixEnterMutex()
33039** assert( unixMutexHeld() );
33040** unixEnterLeave()
33041**
33042** To prevent deadlock, the global unixBigLock must must be acquired
33043** before the unixInodeInfo.pLockMutex mutex, if both are held. It is
33044** OK to get the pLockMutex without holding unixBigLock first, but if
33045** that happens, the unixBigLock mutex must not be acquired until after
33046** pLockMutex is released.
33047**
33048** OK: enter(unixBigLock), enter(pLockInfo)
33049** OK: enter(unixBigLock)
33050** OK: enter(pLockInfo)
33051** ERROR: enter(pLockInfo), enter(unixBigLock)
33052*/
33053static sqlite3_mutex *unixBigLock = 0;
33054static void unixEnterMutex(void){
33055 assert( sqlite3_mutex_notheld(unixBigLock) ); /* Not a recursive mutex */
33056 sqlite3_mutex_enter(unixBigLock);
33057}
33058static void unixLeaveMutex(void){
33059 assert( sqlite3_mutex_held(unixBigLock) );
33060 sqlite3_mutex_leave(unixBigLock);
33061}
33062#ifdef SQLITE_DEBUG
33063static int unixMutexHeld(void) {
33064 return sqlite3_mutex_held(unixBigLock);
33065}
33066#endif
33067
33068
33069#ifdef SQLITE_HAVE_OS_TRACE
33070/*
33071** Helper function for printing out trace information from debugging
33072** binaries. This returns the string representation of the supplied
33073** integer lock-type.
33074*/
33075static const char *azFileLock(int eFileLock){
33076 switch( eFileLock ){
33077 case NO_LOCK: return "NONE";
33078 case SHARED_LOCK: return "SHARED";
33079 case RESERVED_LOCK: return "RESERVED";
33080 case PENDING_LOCK: return "PENDING";
33081 case EXCLUSIVE_LOCK: return "EXCLUSIVE";
33082 }
33083 return "ERROR";
33084}
33085#endif
33086
33087#ifdef SQLITE_LOCK_TRACE
33088/*
33089** Print out information about all locking operations.
33090**
33091** This routine is used for troubleshooting locks on multithreaded
33092** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
33093** command-line option on the compiler. This code is normally
33094** turned off.
33095*/
33096static int lockTrace(int fd, int op, struct flock *p){
33097 char *zOpName, *zType;
33098 int s;
33099 int savedErrno;
33100 if( op==F_GETLK ){
33101 zOpName = "GETLK";
33102 }else if( op==F_SETLK ){
33103 zOpName = "SETLK";
33104 }else{
33105 s = osFcntl(fd, op, p);
33106 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
33107 return s;
33108 }
33109 if( p->l_type==F_RDLCK ){
33110 zType = "RDLCK";
33111 }else if( p->l_type==F_WRLCK ){
33112 zType = "WRLCK";
33113 }else if( p->l_type==F_UNLCK ){
33114 zType = "UNLCK";
33115 }else{
33116 assert( 0 );
33117 }
33118 assert( p->l_whence==SEEK_SET );
33119 s = osFcntl(fd, op, p);
33120 savedErrno = errno;
33121 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
33122 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
33123 (int)p->l_pid, s);
33124 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
33125 struct flock l2;
33126 l2 = *p;
33127 osFcntl(fd, F_GETLK, &l2);
33128 if( l2.l_type==F_RDLCK ){
33129 zType = "RDLCK";
33130 }else if( l2.l_type==F_WRLCK ){
33131 zType = "WRLCK";
33132 }else if( l2.l_type==F_UNLCK ){
33133 zType = "UNLCK";
33134 }else{
33135 assert( 0 );
33136 }
33137 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
33138 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
33139 }
33140 errno = savedErrno;
33141 return s;
33142}
33143#undef osFcntl
33144#define osFcntl lockTrace
33145#endif /* SQLITE_LOCK_TRACE */
33146
33147/*
33148** Retry ftruncate() calls that fail due to EINTR
33149**
33150** All calls to ftruncate() within this file should be made through
33151** this wrapper. On the Android platform, bypassing the logic below
33152** could lead to a corrupt database.
33153*/
33154static int robust_ftruncate(int h, sqlite3_int64 sz){
33155 int rc;
33156#ifdef __ANDROID__
33157 /* On Android, ftruncate() always uses 32-bit offsets, even if
33158 ** _FILE_OFFSET_BITS=64 is defined. This means it is unsafe to attempt to
33159 ** truncate a file to any size larger than 2GiB. Silently ignore any
33160 ** such attempts. */
33161 if( sz>(sqlite3_int64)0x7FFFFFFF ){
33162 rc = SQLITE_OK;
33163 }else
33164#endif
33165 do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR );
33166 return rc;
33167}
33168
33169/*
33170** This routine translates a standard POSIX errno code into something
33171** useful to the clients of the sqlite3 functions. Specifically, it is
33172** intended to translate a variety of "try again" errors into SQLITE_BUSY
33173** and a variety of "please close the file descriptor NOW" errors into
33174** SQLITE_IOERR
33175**
33176** Errors during initialization of locks, or file system support for locks,
33177** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
33178*/
33179static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
33180 assert( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
33181 (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
33182 (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
33183 (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) );
33184 switch (posixError) {
33185 case EACCES:
33186 case EAGAIN:
33187 case ETIMEDOUT:
33188 case EBUSY:
33189 case EINTR:
33190 case ENOLCK:
33191 /* random NFS retry error, unless during file system support
33192 * introspection, in which it actually means what it says */
33193 return SQLITE_BUSY;
33194
33195 case EPERM:
33196 return SQLITE_PERM;
33197
33198 default:
33199 return sqliteIOErr;
33200 }
33201}
33202
33203
33204/******************************************************************************
33205****************** Begin Unique File ID Utility Used By VxWorks ***************
33206**
33207** On most versions of unix, we can get a unique ID for a file by concatenating
33208** the device number and the inode number. But this does not work on VxWorks.
33209** On VxWorks, a unique file id must be based on the canonical filename.
33210**
33211** A pointer to an instance of the following structure can be used as a
33212** unique file ID in VxWorks. Each instance of this structure contains
33213** a copy of the canonical filename. There is also a reference count.
33214** The structure is reclaimed when the number of pointers to it drops to
33215** zero.
33216**
33217** There are never very many files open at one time and lookups are not
33218** a performance-critical path, so it is sufficient to put these
33219** structures on a linked list.
33220*/
33221struct vxworksFileId {
33222 struct vxworksFileId *pNext; /* Next in a list of them all */
33223 int nRef; /* Number of references to this one */
33224 int nName; /* Length of the zCanonicalName[] string */
33225 char *zCanonicalName; /* Canonical filename */
33226};
33227
33228#if OS_VXWORKS
33229/*
33230** All unique filenames are held on a linked list headed by this
33231** variable:
33232*/
33233static struct vxworksFileId *vxworksFileList = 0;
33234
33235/*
33236** Simplify a filename into its canonical form
33237** by making the following changes:
33238**
33239** * removing any trailing and duplicate /
33240** * convert /./ into just /
33241** * convert /A/../ where A is any simple name into just /
33242**
33243** Changes are made in-place. Return the new name length.
33244**
33245** The original filename is in z[0..n-1]. Return the number of
33246** characters in the simplified name.
33247*/
33248static int vxworksSimplifyName(char *z, int n){
33249 int i, j;
33250 while( n>1 && z[n-1]=='/' ){ n--; }
33251 for(i=j=0; i<n; i++){
33252 if( z[i]=='/' ){
33253 if( z[i+1]=='/' ) continue;
33254 if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
33255 i += 1;
33256 continue;
33257 }
33258 if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
33259 while( j>0 && z[j-1]!='/' ){ j--; }
33260 if( j>0 ){ j--; }
33261 i += 2;
33262 continue;
33263 }
33264 }
33265 z[j++] = z[i];
33266 }
33267 z[j] = 0;
33268 return j;
33269}
33270
33271/*
33272** Find a unique file ID for the given absolute pathname. Return
33273** a pointer to the vxworksFileId object. This pointer is the unique
33274** file ID.
33275**
33276** The nRef field of the vxworksFileId object is incremented before
33277** the object is returned. A new vxworksFileId object is created
33278** and added to the global list if necessary.
33279**
33280** If a memory allocation error occurs, return NULL.
33281*/
33282static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
33283 struct vxworksFileId *pNew; /* search key and new file ID */
33284 struct vxworksFileId *pCandidate; /* For looping over existing file IDs */
33285 int n; /* Length of zAbsoluteName string */
33286
33287 assert( zAbsoluteName[0]=='/' );
33288 n = (int)strlen(zAbsoluteName);
33289 pNew = sqlite3_malloc64( sizeof(*pNew) + (n+1) );
33290 if( pNew==0 ) return 0;
33291 pNew->zCanonicalName = (char*)&pNew[1];
33292 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
33293 n = vxworksSimplifyName(pNew->zCanonicalName, n);
33294
33295 /* Search for an existing entry that matching the canonical name.
33296 ** If found, increment the reference count and return a pointer to
33297 ** the existing file ID.
33298 */
33299 unixEnterMutex();
33300 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
33301 if( pCandidate->nName==n
33302 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
33303 ){
33304 sqlite3_free(pNew);
33305 pCandidate->nRef++;
33306 unixLeaveMutex();
33307 return pCandidate;
33308 }
33309 }
33310
33311 /* No match was found. We will make a new file ID */
33312 pNew->nRef = 1;
33313 pNew->nName = n;
33314 pNew->pNext = vxworksFileList;
33315 vxworksFileList = pNew;
33316 unixLeaveMutex();
33317 return pNew;
33318}
33319
33320/*
33321** Decrement the reference count on a vxworksFileId object. Free
33322** the object when the reference count reaches zero.
33323*/
33324static void vxworksReleaseFileId(struct vxworksFileId *pId){
33325 unixEnterMutex();
33326 assert( pId->nRef>0 );
33327 pId->nRef--;
33328 if( pId->nRef==0 ){
33329 struct vxworksFileId **pp;
33330 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
33331 assert( *pp==pId );
33332 *pp = pId->pNext;
33333 sqlite3_free(pId);
33334 }
33335 unixLeaveMutex();
33336}
33337#endif /* OS_VXWORKS */
33338/*************** End of Unique File ID Utility Used By VxWorks ****************
33339******************************************************************************/
33340
33341
33342/******************************************************************************
33343*************************** Posix Advisory Locking ****************************
33344**
33345** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996)
33346** section 6.5.2.2 lines 483 through 490 specify that when a process
33347** sets or clears a lock, that operation overrides any prior locks set
33348** by the same process. It does not explicitly say so, but this implies
33349** that it overrides locks set by the same process using a different
33350** file descriptor. Consider this test case:
33351**
33352** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
33353** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
33354**
33355** Suppose ./file1 and ./file2 are really the same file (because
33356** one is a hard or symbolic link to the other) then if you set
33357** an exclusive lock on fd1, then try to get an exclusive lock
33358** on fd2, it works. I would have expected the second lock to
33359** fail since there was already a lock on the file due to fd1.
33360** But not so. Since both locks came from the same process, the
33361** second overrides the first, even though they were on different
33362** file descriptors opened on different file names.
33363**
33364** This means that we cannot use POSIX locks to synchronize file access
33365** among competing threads of the same process. POSIX locks will work fine
33366** to synchronize access for threads in separate processes, but not
33367** threads within the same process.
33368**
33369** To work around the problem, SQLite has to manage file locks internally
33370** on its own. Whenever a new database is opened, we have to find the
33371** specific inode of the database file (the inode is determined by the
33372** st_dev and st_ino fields of the stat structure that fstat() fills in)
33373** and check for locks already existing on that inode. When locks are
33374** created or removed, we have to look at our own internal record of the
33375** locks to see if another thread has previously set a lock on that same
33376** inode.
33377**
33378** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
33379** For VxWorks, we have to use the alternative unique ID system based on
33380** canonical filename and implemented in the previous division.)
33381**
33382** The sqlite3_file structure for POSIX is no longer just an integer file
33383** descriptor. It is now a structure that holds the integer file
33384** descriptor and a pointer to a structure that describes the internal
33385** locks on the corresponding inode. There is one locking structure
33386** per inode, so if the same inode is opened twice, both unixFile structures
33387** point to the same locking structure. The locking structure keeps
33388** a reference count (so we will know when to delete it) and a "cnt"
33389** field that tells us its internal lock status. cnt==0 means the
33390** file is unlocked. cnt==-1 means the file has an exclusive lock.
33391** cnt>0 means there are cnt shared locks on the file.
33392**
33393** Any attempt to lock or unlock a file first checks the locking
33394** structure. The fcntl() system call is only invoked to set a
33395** POSIX lock if the internal lock structure transitions between
33396** a locked and an unlocked state.
33397**
33398** But wait: there are yet more problems with POSIX advisory locks.
33399**
33400** If you close a file descriptor that points to a file that has locks,
33401** all locks on that file that are owned by the current process are
33402** released. To work around this problem, each unixInodeInfo object
33403** maintains a count of the number of pending locks on tha inode.
33404** When an attempt is made to close an unixFile, if there are
33405** other unixFile open on the same inode that are holding locks, the call
33406** to close() the file descriptor is deferred until all of the locks clear.
33407** The unixInodeInfo structure keeps a list of file descriptors that need to
33408** be closed and that list is walked (and cleared) when the last lock
33409** clears.
33410**
33411** Yet another problem: LinuxThreads do not play well with posix locks.
33412**
33413** Many older versions of linux use the LinuxThreads library which is
33414** not posix compliant. Under LinuxThreads, a lock created by thread
33415** A cannot be modified or overridden by a different thread B.
33416** Only thread A can modify the lock. Locking behavior is correct
33417** if the appliation uses the newer Native Posix Thread Library (NPTL)
33418** on linux - with NPTL a lock created by thread A can override locks
33419** in thread B. But there is no way to know at compile-time which
33420** threading library is being used. So there is no way to know at
33421** compile-time whether or not thread A can override locks on thread B.
33422** One has to do a run-time check to discover the behavior of the
33423** current process.
33424**
33425** SQLite used to support LinuxThreads. But support for LinuxThreads
33426** was dropped beginning with version 3.7.0. SQLite will still work with
33427** LinuxThreads provided that (1) there is no more than one connection
33428** per database file in the same process and (2) database connections
33429** do not move across threads.
33430*/
33431
33432/*
33433** An instance of the following structure serves as the key used
33434** to locate a particular unixInodeInfo object.
33435*/
33436struct unixFileId {
33437 dev_t dev; /* Device number */
33438#if OS_VXWORKS
33439 struct vxworksFileId *pId; /* Unique file ID for vxworks. */
33440#else
33441 /* We are told that some versions of Android contain a bug that
33442 ** sizes ino_t at only 32-bits instead of 64-bits. (See
33443 ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
33444 ** To work around this, always allocate 64-bits for the inode number.
33445 ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
33446 ** but that should not be a big deal. */
33447 /* WAS: ino_t ino; */
33448 u64 ino; /* Inode number */
33449#endif
33450};
33451
33452/*
33453** An instance of the following structure is allocated for each open
33454** inode.
33455**
33456** A single inode can have multiple file descriptors, so each unixFile
33457** structure contains a pointer to an instance of this object and this
33458** object keeps a count of the number of unixFile pointing to it.
33459**
33460** Mutex rules:
33461**
33462** (1) Only the pLockMutex mutex must be held in order to read or write
33463** any of the locking fields:
33464** nShared, nLock, eFileLock, bProcessLock, pUnused
33465**
33466** (2) When nRef>0, then the following fields are unchanging and can
33467** be read (but not written) without holding any mutex:
33468** fileId, pLockMutex
33469**
33470** (3) With the exceptions above, all the fields may only be read
33471** or written while holding the global unixBigLock mutex.
33472**
33473** Deadlock prevention: The global unixBigLock mutex may not
33474** be acquired while holding the pLockMutex mutex. If both unixBigLock
33475** and pLockMutex are needed, then unixBigLock must be acquired first.
33476*/
33477struct unixInodeInfo {
33478 struct unixFileId fileId; /* The lookup key */
33479 sqlite3_mutex *pLockMutex; /* Hold this mutex for... */
33480 int nShared; /* Number of SHARED locks held */
33481 int nLock; /* Number of outstanding file locks */
33482 unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
33483 unsigned char bProcessLock; /* An exclusive process lock is held */
33484 UnixUnusedFd *pUnused; /* Unused file descriptors to close */
33485 int nRef; /* Number of pointers to this structure */
33486 unixShmNode *pShmNode; /* Shared memory associated with this inode */
33487 unixInodeInfo *pNext; /* List of all unixInodeInfo objects */
33488 unixInodeInfo *pPrev; /* .... doubly linked */
33489#if SQLITE_ENABLE_LOCKING_STYLE
33490 unsigned long long sharedByte; /* for AFP simulated shared lock */
33491#endif
33492#if OS_VXWORKS
33493 sem_t *pSem; /* Named POSIX semaphore */
33494 char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */
33495#endif
33496};
33497
33498/*
33499** A lists of all unixInodeInfo objects.
33500**
33501** Must hold unixBigLock in order to read or write this variable.
33502*/
33503static unixInodeInfo *inodeList = 0; /* All unixInodeInfo objects */
33504
33505#ifdef SQLITE_DEBUG
33506/*
33507** True if the inode mutex (on the unixFile.pFileMutex field) is held, or not.
33508** This routine is used only within assert() to help verify correct mutex
33509** usage.
33510*/
33511int unixFileMutexHeld(unixFile *pFile){
33512 assert( pFile->pInode );
33513 return sqlite3_mutex_held(pFile->pInode->pLockMutex);
33514}
33515int unixFileMutexNotheld(unixFile *pFile){
33516 assert( pFile->pInode );
33517 return sqlite3_mutex_notheld(pFile->pInode->pLockMutex);
33518}
33519#endif
33520
33521/*
33522**
33523** This function - unixLogErrorAtLine(), is only ever called via the macro
33524** unixLogError().
33525**
33526** It is invoked after an error occurs in an OS function and errno has been
33527** set. It logs a message using sqlite3_log() containing the current value of
33528** errno and, if possible, the human-readable equivalent from strerror() or
33529** strerror_r().
33530**
33531** The first argument passed to the macro should be the error code that
33532** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
33533** The two subsequent arguments should be the name of the OS function that
33534** failed (e.g. "unlink", "open") and the associated file-system path,
33535** if any.
33536*/
33537#define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__)
33538static int unixLogErrorAtLine(
33539 int errcode, /* SQLite error code */
33540 const char *zFunc, /* Name of OS function that failed */
33541 const char *zPath, /* File path associated with error */
33542 int iLine /* Source line number where error occurred */
33543){
33544 char *zErr; /* Message from strerror() or equivalent */
33545 int iErrno = errno; /* Saved syscall error number */
33546
33547 /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
33548 ** the strerror() function to obtain the human-readable error message
33549 ** equivalent to errno. Otherwise, use strerror_r().
33550 */
33551#if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R)
33552 char aErr[80];
33553 memset(aErr, 0, sizeof(aErr));
33554 zErr = aErr;
33555
33556 /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
33557 ** assume that the system provides the GNU version of strerror_r() that
33558 ** returns a pointer to a buffer containing the error message. That pointer
33559 ** may point to aErr[], or it may point to some static storage somewhere.
33560 ** Otherwise, assume that the system provides the POSIX version of
33561 ** strerror_r(), which always writes an error message into aErr[].
33562 **
33563 ** If the code incorrectly assumes that it is the POSIX version that is
33564 ** available, the error message will often be an empty string. Not a
33565 ** huge problem. Incorrectly concluding that the GNU version is available
33566 ** could lead to a segfault though.
33567 */
33568#if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)
33569 zErr =
33570# endif
33571 strerror_r(iErrno, aErr, sizeof(aErr)-1);
33572
33573#elif SQLITE_THREADSAFE
33574 /* This is a threadsafe build, but strerror_r() is not available. */
33575 zErr = "";
33576#else
33577 /* Non-threadsafe build, use strerror(). */
33578 zErr = strerror(iErrno);
33579#endif
33580
33581 if( zPath==0 ) zPath = "";
33582 sqlite3_log(errcode,
33583 "os_unix.c:%d: (%d) %s(%s) - %s",
33584 iLine, iErrno, zFunc, zPath, zErr
33585 );
33586
33587 return errcode;
33588}
33589
33590/*
33591** Close a file descriptor.
33592**
33593** We assume that close() almost always works, since it is only in a
33594** very sick application or on a very sick platform that it might fail.
33595** If it does fail, simply leak the file descriptor, but do log the
33596** error.
33597**
33598** Note that it is not safe to retry close() after EINTR since the
33599** file descriptor might have already been reused by another thread.
33600** So we don't even try to recover from an EINTR. Just log the error
33601** and move on.
33602*/
33603static void robust_close(unixFile *pFile, int h, int lineno){
33604 if( osClose(h) ){
33605 unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close",
33606 pFile ? pFile->zPath : 0, lineno);
33607 }
33608}
33609
33610/*
33611** Set the pFile->lastErrno. Do this in a subroutine as that provides
33612** a convenient place to set a breakpoint.
33613*/
33614static void storeLastErrno(unixFile *pFile, int error){
33615 pFile->lastErrno = error;
33616}
33617
33618/*
33619** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
33620*/
33621static void closePendingFds(unixFile *pFile){
33622 unixInodeInfo *pInode = pFile->pInode;
33623 UnixUnusedFd *p;
33624 UnixUnusedFd *pNext;
33625 assert( unixFileMutexHeld(pFile) );
33626 for(p=pInode->pUnused; p; p=pNext){
33627 pNext = p->pNext;
33628 robust_close(pFile, p->fd, __LINE__);
33629 sqlite3_free(p);
33630 }
33631 pInode->pUnused = 0;
33632}
33633
33634/*
33635** Release a unixInodeInfo structure previously allocated by findInodeInfo().
33636**
33637** The global mutex must be held when this routine is called, but the mutex
33638** on the inode being deleted must NOT be held.
33639*/
33640static void releaseInodeInfo(unixFile *pFile){
33641 unixInodeInfo *pInode = pFile->pInode;
33642 assert( unixMutexHeld() );
33643 assert( unixFileMutexNotheld(pFile) );
33644 if( ALWAYS(pInode) ){
33645 pInode->nRef--;
33646 if( pInode->nRef==0 ){
33647 assert( pInode->pShmNode==0 );
33648 sqlite3_mutex_enter(pInode->pLockMutex);
33649 closePendingFds(pFile);
33650 sqlite3_mutex_leave(pInode->pLockMutex);
33651 if( pInode->pPrev ){
33652 assert( pInode->pPrev->pNext==pInode );
33653 pInode->pPrev->pNext = pInode->pNext;
33654 }else{
33655 assert( inodeList==pInode );
33656 inodeList = pInode->pNext;
33657 }
33658 if( pInode->pNext ){
33659 assert( pInode->pNext->pPrev==pInode );
33660 pInode->pNext->pPrev = pInode->pPrev;
33661 }
33662 sqlite3_mutex_free(pInode->pLockMutex);
33663 sqlite3_free(pInode);
33664 }
33665 }
33666}
33667
33668/*
33669** Given a file descriptor, locate the unixInodeInfo object that
33670** describes that file descriptor. Create a new one if necessary. The
33671** return value might be uninitialized if an error occurs.
33672**
33673** The global mutex must held when calling this routine.
33674**
33675** Return an appropriate error code.
33676*/
33677static int findInodeInfo(
33678 unixFile *pFile, /* Unix file with file desc used in the key */
33679 unixInodeInfo **ppInode /* Return the unixInodeInfo object here */
33680){
33681 int rc; /* System call return code */
33682 int fd; /* The file descriptor for pFile */
33683 struct unixFileId fileId; /* Lookup key for the unixInodeInfo */
33684 struct stat statbuf; /* Low-level file information */
33685 unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */
33686
33687 assert( unixMutexHeld() );
33688
33689 /* Get low-level information about the file that we can used to
33690 ** create a unique name for the file.
33691 */
33692 fd = pFile->h;
33693 rc = osFstat(fd, &statbuf);
33694 if( rc!=0 ){
33695 storeLastErrno(pFile, errno);
33696#if defined(EOVERFLOW) && defined(SQLITE_DISABLE_LFS)
33697 if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
33698#endif
33699 return SQLITE_IOERR;
33700 }
33701
33702#ifdef __APPLE__
33703 /* On OS X on an msdos filesystem, the inode number is reported
33704 ** incorrectly for zero-size files. See ticket #3260. To work
33705 ** around this problem (we consider it a bug in OS X, not SQLite)
33706 ** we always increase the file size to 1 by writing a single byte
33707 ** prior to accessing the inode number. The one byte written is
33708 ** an ASCII 'S' character which also happens to be the first byte
33709 ** in the header of every SQLite database. In this way, if there
33710 ** is a race condition such that another thread has already populated
33711 ** the first page of the database, no damage is done.
33712 */
33713 if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
33714 do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR );
33715 if( rc!=1 ){
33716 storeLastErrno(pFile, errno);
33717 return SQLITE_IOERR;
33718 }
33719 rc = osFstat(fd, &statbuf);
33720 if( rc!=0 ){
33721 storeLastErrno(pFile, errno);
33722 return SQLITE_IOERR;
33723 }
33724 }
33725#endif
33726
33727 memset(&fileId, 0, sizeof(fileId));
33728 fileId.dev = statbuf.st_dev;
33729#if OS_VXWORKS
33730 fileId.pId = pFile->pId;
33731#else
33732 fileId.ino = (u64)statbuf.st_ino;
33733#endif
33734 assert( unixMutexHeld() );
33735 pInode = inodeList;
33736 while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
33737 pInode = pInode->pNext;
33738 }
33739 if( pInode==0 ){
33740 pInode = sqlite3_malloc64( sizeof(*pInode) );
33741 if( pInode==0 ){
33742 return SQLITE_NOMEM_BKPT;
33743 }
33744 memset(pInode, 0, sizeof(*pInode));
33745 memcpy(&pInode->fileId, &fileId, sizeof(fileId));
33746 if( sqlite3GlobalConfig.bCoreMutex ){
33747 pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
33748 if( pInode->pLockMutex==0 ){
33749 sqlite3_free(pInode);
33750 return SQLITE_NOMEM_BKPT;
33751 }
33752 }
33753 pInode->nRef = 1;
33754 assert( unixMutexHeld() );
33755 pInode->pNext = inodeList;
33756 pInode->pPrev = 0;
33757 if( inodeList ) inodeList->pPrev = pInode;
33758 inodeList = pInode;
33759 }else{
33760 pInode->nRef++;
33761 }
33762 *ppInode = pInode;
33763 return SQLITE_OK;
33764}
33765
33766/*
33767** Return TRUE if pFile has been renamed or unlinked since it was first opened.
33768*/
33769static int fileHasMoved(unixFile *pFile){
33770#if OS_VXWORKS
33771 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
33772#else
33773 struct stat buf;
33774 return pFile->pInode!=0 &&
33775 (osStat(pFile->zPath, &buf)!=0
33776 || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
33777#endif
33778}
33779
33780
33781/*
33782** Check a unixFile that is a database. Verify the following:
33783**
33784** (1) There is exactly one hard link on the file
33785** (2) The file is not a symbolic link
33786** (3) The file has not been renamed or unlinked
33787**
33788** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right.
33789*/
33790static void verifyDbFile(unixFile *pFile){
33791 struct stat buf;
33792 int rc;
33793
33794 /* These verifications occurs for the main database only */
33795 if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return;
33796
33797 rc = osFstat(pFile->h, &buf);
33798 if( rc!=0 ){
33799 sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
33800 return;
33801 }
33802 if( buf.st_nlink==0 ){
33803 sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
33804 return;
33805 }
33806 if( buf.st_nlink>1 ){
33807 sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
33808 return;
33809 }
33810 if( fileHasMoved(pFile) ){
33811 sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
33812 return;
33813 }
33814}
33815
33816
33817/*
33818** This routine checks if there is a RESERVED lock held on the specified
33819** file by this or any other process. If such a lock is held, set *pResOut
33820** to a non-zero value otherwise *pResOut is set to zero. The return value
33821** is set to SQLITE_OK unless an I/O error occurs during lock checking.
33822*/
33823static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
33824 int rc = SQLITE_OK;
33825 int reserved = 0;
33826 unixFile *pFile = (unixFile*)id;
33827
33828 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
33829
33830 assert( pFile );
33831 assert( pFile->eFileLock<=SHARED_LOCK );
33832 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
33833
33834 /* Check if a thread in this process holds such a lock */
33835 if( pFile->pInode->eFileLock>SHARED_LOCK ){
33836 reserved = 1;
33837 }
33838
33839 /* Otherwise see if some other process holds it.
33840 */
33841#ifndef __DJGPP__
33842 if( !reserved && !pFile->pInode->bProcessLock ){
33843 struct flock lock;
33844 lock.l_whence = SEEK_SET;
33845 lock.l_start = RESERVED_BYTE;
33846 lock.l_len = 1;
33847 lock.l_type = F_WRLCK;
33848 if( osFcntl(pFile->h, F_GETLK, &lock) ){
33849 rc = SQLITE_IOERR_CHECKRESERVEDLOCK;
33850 storeLastErrno(pFile, errno);
33851 } else if( lock.l_type!=F_UNLCK ){
33852 reserved = 1;
33853 }
33854 }
33855#endif
33856
33857 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
33858 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
33859
33860 *pResOut = reserved;
33861 return rc;
33862}
33863
33864/*
33865** Set a posix-advisory-lock.
33866**
33867** There are two versions of this routine. If compiled with
33868** SQLITE_ENABLE_SETLK_TIMEOUT then the routine has an extra parameter
33869** which is a pointer to a unixFile. If the unixFile->iBusyTimeout
33870** value is set, then it is the number of milliseconds to wait before
33871** failing the lock. The iBusyTimeout value is always reset back to
33872** zero on each call.
33873**
33874** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
33875** attempt to set the lock.
33876*/
33877#ifndef SQLITE_ENABLE_SETLK_TIMEOUT
33878# define osSetPosixAdvisoryLock(h,x,t) osFcntl(h,F_SETLK,x)
33879#else
33880static int osSetPosixAdvisoryLock(
33881 int h, /* The file descriptor on which to take the lock */
33882 struct flock *pLock, /* The description of the lock */
33883 unixFile *pFile /* Structure holding timeout value */
33884){
33885 int rc = osFcntl(h,F_SETLK,pLock);
33886 while( rc<0 && pFile->iBusyTimeout>0 ){
33887 /* On systems that support some kind of blocking file lock with a timeout,
33888 ** make appropriate changes here to invoke that blocking file lock. On
33889 ** generic posix, however, there is no such API. So we simply try the
33890 ** lock once every millisecond until either the timeout expires, or until
33891 ** the lock is obtained. */
33892 usleep(1000);
33893 rc = osFcntl(h,F_SETLK,pLock);
33894 pFile->iBusyTimeout--;
33895 }
33896 return rc;
33897}
33898#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */
33899
33900
33901/*
33902** Attempt to set a system-lock on the file pFile. The lock is
33903** described by pLock.
33904**
33905** If the pFile was opened read/write from unix-excl, then the only lock
33906** ever obtained is an exclusive lock, and it is obtained exactly once
33907** the first time any lock is attempted. All subsequent system locking
33908** operations become no-ops. Locking operations still happen internally,
33909** in order to coordinate access between separate database connections
33910** within this process, but all of that is handled in memory and the
33911** operating system does not participate.
33912**
33913** This function is a pass-through to fcntl(F_SETLK) if pFile is using
33914** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
33915** and is read-only.
33916**
33917** Zero is returned if the call completes successfully, or -1 if a call
33918** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
33919*/
33920static int unixFileLock(unixFile *pFile, struct flock *pLock){
33921 int rc;
33922 unixInodeInfo *pInode = pFile->pInode;
33923 assert( pInode!=0 );
33924 assert( sqlite3_mutex_held(pInode->pLockMutex) );
33925 if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){
33926 if( pInode->bProcessLock==0 ){
33927 struct flock lock;
33928 assert( pInode->nLock==0 );
33929 lock.l_whence = SEEK_SET;
33930 lock.l_start = SHARED_FIRST;
33931 lock.l_len = SHARED_SIZE;
33932 lock.l_type = F_WRLCK;
33933 rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile);
33934 if( rc<0 ) return rc;
33935 pInode->bProcessLock = 1;
33936 pInode->nLock++;
33937 }else{
33938 rc = 0;
33939 }
33940 }else{
33941 rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile);
33942 }
33943 return rc;
33944}
33945
33946/*
33947** Lock the file with the lock specified by parameter eFileLock - one
33948** of the following:
33949**
33950** (1) SHARED_LOCK
33951** (2) RESERVED_LOCK
33952** (3) PENDING_LOCK
33953** (4) EXCLUSIVE_LOCK
33954**
33955** Sometimes when requesting one lock state, additional lock states
33956** are inserted in between. The locking might fail on one of the later
33957** transitions leaving the lock state different from what it started but
33958** still short of its goal. The following chart shows the allowed
33959** transitions and the inserted intermediate states:
33960**
33961** UNLOCKED -> SHARED
33962** SHARED -> RESERVED
33963** SHARED -> (PENDING) -> EXCLUSIVE
33964** RESERVED -> (PENDING) -> EXCLUSIVE
33965** PENDING -> EXCLUSIVE
33966**
33967** This routine will only increase a lock. Use the sqlite3OsUnlock()
33968** routine to lower a locking level.
33969*/
33970static int unixLock(sqlite3_file *id, int eFileLock){
33971 /* The following describes the implementation of the various locks and
33972 ** lock transitions in terms of the POSIX advisory shared and exclusive
33973 ** lock primitives (called read-locks and write-locks below, to avoid
33974 ** confusion with SQLite lock names). The algorithms are complicated
33975 ** slightly in order to be compatible with Windows95 systems simultaneously
33976 ** accessing the same database file, in case that is ever required.
33977 **
33978 ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
33979 ** byte', each single bytes at well known offsets, and the 'shared byte
33980 ** range', a range of 510 bytes at a well known offset.
33981 **
33982 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
33983 ** byte'. If this is successful, 'shared byte range' is read-locked
33984 ** and the lock on the 'pending byte' released. (Legacy note: When
33985 ** SQLite was first developed, Windows95 systems were still very common,
33986 ** and Widnows95 lacks a shared-lock capability. So on Windows95, a
33987 ** single randomly selected by from the 'shared byte range' is locked.
33988 ** Windows95 is now pretty much extinct, but this work-around for the
33989 ** lack of shared-locks on Windows95 lives on, for backwards
33990 ** compatibility.)
33991 **
33992 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
33993 ** A RESERVED lock is implemented by grabbing a write-lock on the
33994 ** 'reserved byte'.
33995 **
33996 ** A process may only obtain a PENDING lock after it has obtained a
33997 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
33998 ** on the 'pending byte'. This ensures that no new SHARED locks can be
33999 ** obtained, but existing SHARED locks are allowed to persist. A process
34000 ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
34001 ** This property is used by the algorithm for rolling back a journal file
34002 ** after a crash.
34003 **
34004 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
34005 ** implemented by obtaining a write-lock on the entire 'shared byte
34006 ** range'. Since all other locks require a read-lock on one of the bytes
34007 ** within this range, this ensures that no other locks are held on the
34008 ** database.
34009 */
34010 int rc = SQLITE_OK;
34011 unixFile *pFile = (unixFile*)id;
34012 unixInodeInfo *pInode;
34013 struct flock lock;
34014 int tErrno = 0;
34015
34016 assert( pFile );
34017 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
34018 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
34019 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
34020 osGetpid(0)));
34021
34022 /* If there is already a lock of this type or more restrictive on the
34023 ** unixFile, do nothing. Don't use the end_lock: exit path, as
34024 ** unixEnterMutex() hasn't been called yet.
34025 */
34026 if( pFile->eFileLock>=eFileLock ){
34027 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
34028 azFileLock(eFileLock)));
34029 return SQLITE_OK;
34030 }
34031
34032 /* Make sure the locking sequence is correct.
34033 ** (1) We never move from unlocked to anything higher than shared lock.
34034 ** (2) SQLite never explicitly requests a pendig lock.
34035 ** (3) A shared lock is always held when a reserve lock is requested.
34036 */
34037 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
34038 assert( eFileLock!=PENDING_LOCK );
34039 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
34040
34041 /* This mutex is needed because pFile->pInode is shared across threads
34042 */
34043 pInode = pFile->pInode;
34044 sqlite3_mutex_enter(pInode->pLockMutex);
34045
34046 /* If some thread using this PID has a lock via a different unixFile*
34047 ** handle that precludes the requested lock, return BUSY.
34048 */
34049 if( (pFile->eFileLock!=pInode->eFileLock &&
34050 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
34051 ){
34052 rc = SQLITE_BUSY;
34053 goto end_lock;
34054 }
34055
34056 /* If a SHARED lock is requested, and some thread using this PID already
34057 ** has a SHARED or RESERVED lock, then increment reference counts and
34058 ** return SQLITE_OK.
34059 */
34060 if( eFileLock==SHARED_LOCK &&
34061 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
34062 assert( eFileLock==SHARED_LOCK );
34063 assert( pFile->eFileLock==0 );
34064 assert( pInode->nShared>0 );
34065 pFile->eFileLock = SHARED_LOCK;
34066 pInode->nShared++;
34067 pInode->nLock++;
34068 goto end_lock;
34069 }
34070
34071
34072 /* A PENDING lock is needed before acquiring a SHARED lock and before
34073 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
34074 ** be released.
34075 */
34076 lock.l_len = 1L;
34077 lock.l_whence = SEEK_SET;
34078 if( eFileLock==SHARED_LOCK
34079 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
34080 ){
34081 lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
34082 lock.l_start = PENDING_BYTE;
34083 if( unixFileLock(pFile, &lock) ){
34084 tErrno = errno;
34085 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
34086 if( rc!=SQLITE_BUSY ){
34087 storeLastErrno(pFile, tErrno);
34088 }
34089 goto end_lock;
34090 }
34091 }
34092
34093
34094 /* If control gets to this point, then actually go ahead and make
34095 ** operating system calls for the specified lock.
34096 */
34097 if( eFileLock==SHARED_LOCK ){
34098 assert( pInode->nShared==0 );
34099 assert( pInode->eFileLock==0 );
34100 assert( rc==SQLITE_OK );
34101
34102 /* Now get the read-lock */
34103 lock.l_start = SHARED_FIRST;
34104 lock.l_len = SHARED_SIZE;
34105 if( unixFileLock(pFile, &lock) ){
34106 tErrno = errno;
34107 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
34108 }
34109
34110 /* Drop the temporary PENDING lock */
34111 lock.l_start = PENDING_BYTE;
34112 lock.l_len = 1L;
34113 lock.l_type = F_UNLCK;
34114 if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){
34115 /* This could happen with a network mount */
34116 tErrno = errno;
34117 rc = SQLITE_IOERR_UNLOCK;
34118 }
34119
34120 if( rc ){
34121 if( rc!=SQLITE_BUSY ){
34122 storeLastErrno(pFile, tErrno);
34123 }
34124 goto end_lock;
34125 }else{
34126 pFile->eFileLock = SHARED_LOCK;
34127 pInode->nLock++;
34128 pInode->nShared = 1;
34129 }
34130 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
34131 /* We are trying for an exclusive lock but another thread in this
34132 ** same process is still holding a shared lock. */
34133 rc = SQLITE_BUSY;
34134 }else{
34135 /* The request was for a RESERVED or EXCLUSIVE lock. It is
34136 ** assumed that there is a SHARED or greater lock on the file
34137 ** already.
34138 */
34139 assert( 0!=pFile->eFileLock );
34140 lock.l_type = F_WRLCK;
34141
34142 assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK );
34143 if( eFileLock==RESERVED_LOCK ){
34144 lock.l_start = RESERVED_BYTE;
34145 lock.l_len = 1L;
34146 }else{
34147 lock.l_start = SHARED_FIRST;
34148 lock.l_len = SHARED_SIZE;
34149 }
34150
34151 if( unixFileLock(pFile, &lock) ){
34152 tErrno = errno;
34153 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
34154 if( rc!=SQLITE_BUSY ){
34155 storeLastErrno(pFile, tErrno);
34156 }
34157 }
34158 }
34159
34160
34161#ifdef SQLITE_DEBUG
34162 /* Set up the transaction-counter change checking flags when
34163 ** transitioning from a SHARED to a RESERVED lock. The change
34164 ** from SHARED to RESERVED marks the beginning of a normal
34165 ** write operation (not a hot journal rollback).
34166 */
34167 if( rc==SQLITE_OK
34168 && pFile->eFileLock<=SHARED_LOCK
34169 && eFileLock==RESERVED_LOCK
34170 ){
34171 pFile->transCntrChng = 0;
34172 pFile->dbUpdate = 0;
34173 pFile->inNormalWrite = 1;
34174 }
34175#endif
34176
34177
34178 if( rc==SQLITE_OK ){
34179 pFile->eFileLock = eFileLock;
34180 pInode->eFileLock = eFileLock;
34181 }else if( eFileLock==EXCLUSIVE_LOCK ){
34182 pFile->eFileLock = PENDING_LOCK;
34183 pInode->eFileLock = PENDING_LOCK;
34184 }
34185
34186end_lock:
34187 sqlite3_mutex_leave(pInode->pLockMutex);
34188 OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
34189 rc==SQLITE_OK ? "ok" : "failed"));
34190 return rc;
34191}
34192
34193/*
34194** Add the file descriptor used by file handle pFile to the corresponding
34195** pUnused list.
34196*/
34197static void setPendingFd(unixFile *pFile){
34198 unixInodeInfo *pInode = pFile->pInode;
34199 UnixUnusedFd *p = pFile->pPreallocatedUnused;
34200 assert( unixFileMutexHeld(pFile) );
34201 p->pNext = pInode->pUnused;
34202 pInode->pUnused = p;
34203 pFile->h = -1;
34204 pFile->pPreallocatedUnused = 0;
34205}
34206
34207/*
34208** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
34209** must be either NO_LOCK or SHARED_LOCK.
34210**
34211** If the locking level of the file descriptor is already at or below
34212** the requested locking level, this routine is a no-op.
34213**
34214** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
34215** the byte range is divided into 2 parts and the first part is unlocked then
34216** set to a read lock, then the other part is simply unlocked. This works
34217** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
34218** remove the write lock on a region when a read lock is set.
34219*/
34220static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
34221 unixFile *pFile = (unixFile*)id;
34222 unixInodeInfo *pInode;
34223 struct flock lock;
34224 int rc = SQLITE_OK;
34225
34226 assert( pFile );
34227 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
34228 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
34229 osGetpid(0)));
34230
34231 assert( eFileLock<=SHARED_LOCK );
34232 if( pFile->eFileLock<=eFileLock ){
34233 return SQLITE_OK;
34234 }
34235 pInode = pFile->pInode;
34236 sqlite3_mutex_enter(pInode->pLockMutex);
34237 assert( pInode->nShared!=0 );
34238 if( pFile->eFileLock>SHARED_LOCK ){
34239 assert( pInode->eFileLock==pFile->eFileLock );
34240
34241#ifdef SQLITE_DEBUG
34242 /* When reducing a lock such that other processes can start
34243 ** reading the database file again, make sure that the
34244 ** transaction counter was updated if any part of the database
34245 ** file changed. If the transaction counter is not updated,
34246 ** other connections to the same file might not realize that
34247 ** the file has changed and hence might not know to flush their
34248 ** cache. The use of a stale cache can lead to database corruption.
34249 */
34250 pFile->inNormalWrite = 0;
34251#endif
34252
34253 /* downgrading to a shared lock on NFS involves clearing the write lock
34254 ** before establishing the readlock - to avoid a race condition we downgrade
34255 ** the lock in 2 blocks, so that part of the range will be covered by a
34256 ** write lock until the rest is covered by a read lock:
34257 ** 1: [WWWWW]
34258 ** 2: [....W]
34259 ** 3: [RRRRW]
34260 ** 4: [RRRR.]
34261 */
34262 if( eFileLock==SHARED_LOCK ){
34263#if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE
34264 (void)handleNFSUnlock;
34265 assert( handleNFSUnlock==0 );
34266#endif
34267#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
34268 if( handleNFSUnlock ){
34269 int tErrno; /* Error code from system call errors */
34270 off_t divSize = SHARED_SIZE - 1;
34271
34272 lock.l_type = F_UNLCK;
34273 lock.l_whence = SEEK_SET;
34274 lock.l_start = SHARED_FIRST;
34275 lock.l_len = divSize;
34276 if( unixFileLock(pFile, &lock)==(-1) ){
34277 tErrno = errno;
34278 rc = SQLITE_IOERR_UNLOCK;
34279 storeLastErrno(pFile, tErrno);
34280 goto end_unlock;
34281 }
34282 lock.l_type = F_RDLCK;
34283 lock.l_whence = SEEK_SET;
34284 lock.l_start = SHARED_FIRST;
34285 lock.l_len = divSize;
34286 if( unixFileLock(pFile, &lock)==(-1) ){
34287 tErrno = errno;
34288 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
34289 if( IS_LOCK_ERROR(rc) ){
34290 storeLastErrno(pFile, tErrno);
34291 }
34292 goto end_unlock;
34293 }
34294 lock.l_type = F_UNLCK;
34295 lock.l_whence = SEEK_SET;
34296 lock.l_start = SHARED_FIRST+divSize;
34297 lock.l_len = SHARED_SIZE-divSize;
34298 if( unixFileLock(pFile, &lock)==(-1) ){
34299 tErrno = errno;
34300 rc = SQLITE_IOERR_UNLOCK;
34301 storeLastErrno(pFile, tErrno);
34302 goto end_unlock;
34303 }
34304 }else
34305#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
34306 {
34307 lock.l_type = F_RDLCK;
34308 lock.l_whence = SEEK_SET;
34309 lock.l_start = SHARED_FIRST;
34310 lock.l_len = SHARED_SIZE;
34311 if( unixFileLock(pFile, &lock) ){
34312 /* In theory, the call to unixFileLock() cannot fail because another
34313 ** process is holding an incompatible lock. If it does, this
34314 ** indicates that the other process is not following the locking
34315 ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
34316 ** SQLITE_BUSY would confuse the upper layer (in practice it causes
34317 ** an assert to fail). */
34318 rc = SQLITE_IOERR_RDLOCK;
34319 storeLastErrno(pFile, errno);
34320 goto end_unlock;
34321 }
34322 }
34323 }
34324 lock.l_type = F_UNLCK;
34325 lock.l_whence = SEEK_SET;
34326 lock.l_start = PENDING_BYTE;
34327 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
34328 if( unixFileLock(pFile, &lock)==0 ){
34329 pInode->eFileLock = SHARED_LOCK;
34330 }else{
34331 rc = SQLITE_IOERR_UNLOCK;
34332 storeLastErrno(pFile, errno);
34333 goto end_unlock;
34334 }
34335 }
34336 if( eFileLock==NO_LOCK ){
34337 /* Decrement the shared lock counter. Release the lock using an
34338 ** OS call only when all threads in this same process have released
34339 ** the lock.
34340 */
34341 pInode->nShared--;
34342 if( pInode->nShared==0 ){
34343 lock.l_type = F_UNLCK;
34344 lock.l_whence = SEEK_SET;
34345 lock.l_start = lock.l_len = 0L;
34346 if( unixFileLock(pFile, &lock)==0 ){
34347 pInode->eFileLock = NO_LOCK;
34348 }else{
34349 rc = SQLITE_IOERR_UNLOCK;
34350 storeLastErrno(pFile, errno);
34351 pInode->eFileLock = NO_LOCK;
34352 pFile->eFileLock = NO_LOCK;
34353 }
34354 }
34355
34356 /* Decrement the count of locks against this same file. When the
34357 ** count reaches zero, close any other file descriptors whose close
34358 ** was deferred because of outstanding locks.
34359 */
34360 pInode->nLock--;
34361 assert( pInode->nLock>=0 );
34362 if( pInode->nLock==0 ) closePendingFds(pFile);
34363 }
34364
34365end_unlock:
34366 sqlite3_mutex_leave(pInode->pLockMutex);
34367 if( rc==SQLITE_OK ){
34368 pFile->eFileLock = eFileLock;
34369 }
34370 return rc;
34371}
34372
34373/*
34374** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
34375** must be either NO_LOCK or SHARED_LOCK.
34376**
34377** If the locking level of the file descriptor is already at or below
34378** the requested locking level, this routine is a no-op.
34379*/
34380static int unixUnlock(sqlite3_file *id, int eFileLock){
34381#if SQLITE_MAX_MMAP_SIZE>0
34382 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
34383#endif
34384 return posixUnlock(id, eFileLock, 0);
34385}
34386
34387#if SQLITE_MAX_MMAP_SIZE>0
34388static int unixMapfile(unixFile *pFd, i64 nByte);
34389static void unixUnmapfile(unixFile *pFd);
34390#endif
34391
34392/*
34393** This function performs the parts of the "close file" operation
34394** common to all locking schemes. It closes the directory and file
34395** handles, if they are valid, and sets all fields of the unixFile
34396** structure to 0.
34397**
34398** It is *not* necessary to hold the mutex when this routine is called,
34399** even on VxWorks. A mutex will be acquired on VxWorks by the
34400** vxworksReleaseFileId() routine.
34401*/
34402static int closeUnixFile(sqlite3_file *id){
34403 unixFile *pFile = (unixFile*)id;
34404#if SQLITE_MAX_MMAP_SIZE>0
34405 unixUnmapfile(pFile);
34406#endif
34407 if( pFile->h>=0 ){
34408 robust_close(pFile, pFile->h, __LINE__);
34409 pFile->h = -1;
34410 }
34411#if OS_VXWORKS
34412 if( pFile->pId ){
34413 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
34414 osUnlink(pFile->pId->zCanonicalName);
34415 }
34416 vxworksReleaseFileId(pFile->pId);
34417 pFile->pId = 0;
34418 }
34419#endif
34420#ifdef SQLITE_UNLINK_AFTER_CLOSE
34421 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
34422 osUnlink(pFile->zPath);
34423 sqlite3_free(*(char**)&pFile->zPath);
34424 pFile->zPath = 0;
34425 }
34426#endif
34427 OSTRACE(("CLOSE %-3d\n", pFile->h));
34428 OpenCounter(-1);
34429 sqlite3_free(pFile->pPreallocatedUnused);
34430 memset(pFile, 0, sizeof(unixFile));
34431 return SQLITE_OK;
34432}
34433
34434/*
34435** Close a file.
34436*/
34437static int unixClose(sqlite3_file *id){
34438 int rc = SQLITE_OK;
34439 unixFile *pFile = (unixFile *)id;
34440 unixInodeInfo *pInode = pFile->pInode;
34441
34442 assert( pInode!=0 );
34443 verifyDbFile(pFile);
34444 unixUnlock(id, NO_LOCK);
34445 assert( unixFileMutexNotheld(pFile) );
34446 unixEnterMutex();
34447
34448 /* unixFile.pInode is always valid here. Otherwise, a different close
34449 ** routine (e.g. nolockClose()) would be called instead.
34450 */
34451 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
34452 sqlite3_mutex_enter(pInode->pLockMutex);
34453 if( pInode->nLock ){
34454 /* If there are outstanding locks, do not actually close the file just
34455 ** yet because that would clear those locks. Instead, add the file
34456 ** descriptor to pInode->pUnused list. It will be automatically closed
34457 ** when the last lock is cleared.
34458 */
34459 setPendingFd(pFile);
34460 }
34461 sqlite3_mutex_leave(pInode->pLockMutex);
34462 releaseInodeInfo(pFile);
34463 rc = closeUnixFile(id);
34464 unixLeaveMutex();
34465 return rc;
34466}
34467
34468/************** End of the posix advisory lock implementation *****************
34469******************************************************************************/
34470
34471/******************************************************************************
34472****************************** No-op Locking **********************************
34473**
34474** Of the various locking implementations available, this is by far the
34475** simplest: locking is ignored. No attempt is made to lock the database
34476** file for reading or writing.
34477**
34478** This locking mode is appropriate for use on read-only databases
34479** (ex: databases that are burned into CD-ROM, for example.) It can
34480** also be used if the application employs some external mechanism to
34481** prevent simultaneous access of the same database by two or more
34482** database connections. But there is a serious risk of database
34483** corruption if this locking mode is used in situations where multiple
34484** database connections are accessing the same database file at the same
34485** time and one or more of those connections are writing.
34486*/
34487
34488static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
34489 UNUSED_PARAMETER(NotUsed);
34490 *pResOut = 0;
34491 return SQLITE_OK;
34492}
34493static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
34494 UNUSED_PARAMETER2(NotUsed, NotUsed2);
34495 return SQLITE_OK;
34496}
34497static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
34498 UNUSED_PARAMETER2(NotUsed, NotUsed2);
34499 return SQLITE_OK;
34500}
34501
34502/*
34503** Close the file.
34504*/
34505static int nolockClose(sqlite3_file *id) {
34506 return closeUnixFile(id);
34507}
34508
34509/******************* End of the no-op lock implementation *********************
34510******************************************************************************/
34511
34512/******************************************************************************
34513************************* Begin dot-file Locking ******************************
34514**
34515** The dotfile locking implementation uses the existence of separate lock
34516** files (really a directory) to control access to the database. This works
34517** on just about every filesystem imaginable. But there are serious downsides:
34518**
34519** (1) There is zero concurrency. A single reader blocks all other
34520** connections from reading or writing the database.
34521**
34522** (2) An application crash or power loss can leave stale lock files
34523** sitting around that need to be cleared manually.
34524**
34525** Nevertheless, a dotlock is an appropriate locking mode for use if no
34526** other locking strategy is available.
34527**
34528** Dotfile locking works by creating a subdirectory in the same directory as
34529** the database and with the same name but with a ".lock" extension added.
34530** The existence of a lock directory implies an EXCLUSIVE lock. All other
34531** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
34532*/
34533
34534/*
34535** The file suffix added to the data base filename in order to create the
34536** lock directory.
34537*/
34538#define DOTLOCK_SUFFIX ".lock"
34539
34540/*
34541** This routine checks if there is a RESERVED lock held on the specified
34542** file by this or any other process. If such a lock is held, set *pResOut
34543** to a non-zero value otherwise *pResOut is set to zero. The return value
34544** is set to SQLITE_OK unless an I/O error occurs during lock checking.
34545**
34546** In dotfile locking, either a lock exists or it does not. So in this
34547** variation of CheckReservedLock(), *pResOut is set to true if any lock
34548** is held on the file and false if the file is unlocked.
34549*/
34550static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
34551 int rc = SQLITE_OK;
34552 int reserved = 0;
34553 unixFile *pFile = (unixFile*)id;
34554
34555 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
34556
34557 assert( pFile );
34558 reserved = osAccess((const char*)pFile->lockingContext, 0)==0;
34559 OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
34560 *pResOut = reserved;
34561 return rc;
34562}
34563
34564/*
34565** Lock the file with the lock specified by parameter eFileLock - one
34566** of the following:
34567**
34568** (1) SHARED_LOCK
34569** (2) RESERVED_LOCK
34570** (3) PENDING_LOCK
34571** (4) EXCLUSIVE_LOCK
34572**
34573** Sometimes when requesting one lock state, additional lock states
34574** are inserted in between. The locking might fail on one of the later
34575** transitions leaving the lock state different from what it started but
34576** still short of its goal. The following chart shows the allowed
34577** transitions and the inserted intermediate states:
34578**
34579** UNLOCKED -> SHARED
34580** SHARED -> RESERVED
34581** SHARED -> (PENDING) -> EXCLUSIVE
34582** RESERVED -> (PENDING) -> EXCLUSIVE
34583** PENDING -> EXCLUSIVE
34584**
34585** This routine will only increase a lock. Use the sqlite3OsUnlock()
34586** routine to lower a locking level.
34587**
34588** With dotfile locking, we really only support state (4): EXCLUSIVE.
34589** But we track the other locking levels internally.
34590*/
34591static int dotlockLock(sqlite3_file *id, int eFileLock) {
34592 unixFile *pFile = (unixFile*)id;
34593 char *zLockFile = (char *)pFile->lockingContext;
34594 int rc = SQLITE_OK;
34595
34596
34597 /* If we have any lock, then the lock file already exists. All we have
34598 ** to do is adjust our internal record of the lock level.
34599 */
34600 if( pFile->eFileLock > NO_LOCK ){
34601 pFile->eFileLock = eFileLock;
34602 /* Always update the timestamp on the old file */
34603#ifdef HAVE_UTIME
34604 utime(zLockFile, NULL);
34605#else
34606 utimes(zLockFile, NULL);
34607#endif
34608 return SQLITE_OK;
34609 }
34610
34611 /* grab an exclusive lock */
34612 rc = osMkdir(zLockFile, 0777);
34613 if( rc<0 ){
34614 /* failed to open/create the lock directory */
34615 int tErrno = errno;
34616 if( EEXIST == tErrno ){
34617 rc = SQLITE_BUSY;
34618 } else {
34619 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
34620 if( rc!=SQLITE_BUSY ){
34621 storeLastErrno(pFile, tErrno);
34622 }
34623 }
34624 return rc;
34625 }
34626
34627 /* got it, set the type and return ok */
34628 pFile->eFileLock = eFileLock;
34629 return rc;
34630}
34631
34632/*
34633** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
34634** must be either NO_LOCK or SHARED_LOCK.
34635**
34636** If the locking level of the file descriptor is already at or below
34637** the requested locking level, this routine is a no-op.
34638**
34639** When the locking level reaches NO_LOCK, delete the lock file.
34640*/
34641static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
34642 unixFile *pFile = (unixFile*)id;
34643 char *zLockFile = (char *)pFile->lockingContext;
34644 int rc;
34645
34646 assert( pFile );
34647 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
34648 pFile->eFileLock, osGetpid(0)));
34649 assert( eFileLock<=SHARED_LOCK );
34650
34651 /* no-op if possible */
34652 if( pFile->eFileLock==eFileLock ){
34653 return SQLITE_OK;
34654 }
34655
34656 /* To downgrade to shared, simply update our internal notion of the
34657 ** lock state. No need to mess with the file on disk.
34658 */
34659 if( eFileLock==SHARED_LOCK ){
34660 pFile->eFileLock = SHARED_LOCK;
34661 return SQLITE_OK;
34662 }
34663
34664 /* To fully unlock the database, delete the lock file */
34665 assert( eFileLock==NO_LOCK );
34666 rc = osRmdir(zLockFile);
34667 if( rc<0 ){
34668 int tErrno = errno;
34669 if( tErrno==ENOENT ){
34670 rc = SQLITE_OK;
34671 }else{
34672 rc = SQLITE_IOERR_UNLOCK;
34673 storeLastErrno(pFile, tErrno);
34674 }
34675 return rc;
34676 }
34677 pFile->eFileLock = NO_LOCK;
34678 return SQLITE_OK;
34679}
34680
34681/*
34682** Close a file. Make sure the lock has been released before closing.
34683*/
34684static int dotlockClose(sqlite3_file *id) {
34685 unixFile *pFile = (unixFile*)id;
34686 assert( id!=0 );
34687 dotlockUnlock(id, NO_LOCK);
34688 sqlite3_free(pFile->lockingContext);
34689 return closeUnixFile(id);
34690}
34691/****************** End of the dot-file lock implementation *******************
34692******************************************************************************/
34693
34694/******************************************************************************
34695************************** Begin flock Locking ********************************
34696**
34697** Use the flock() system call to do file locking.
34698**
34699** flock() locking is like dot-file locking in that the various
34700** fine-grain locking levels supported by SQLite are collapsed into
34701** a single exclusive lock. In other words, SHARED, RESERVED, and
34702** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite
34703** still works when you do this, but concurrency is reduced since
34704** only a single process can be reading the database at a time.
34705**
34706** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off
34707*/
34708#if SQLITE_ENABLE_LOCKING_STYLE
34709
34710/*
34711** Retry flock() calls that fail with EINTR
34712*/
34713#ifdef EINTR
34714static int robust_flock(int fd, int op){
34715 int rc;
34716 do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR );
34717 return rc;
34718}
34719#else
34720# define robust_flock(a,b) flock(a,b)
34721#endif
34722
34723
34724/*
34725** This routine checks if there is a RESERVED lock held on the specified
34726** file by this or any other process. If such a lock is held, set *pResOut
34727** to a non-zero value otherwise *pResOut is set to zero. The return value
34728** is set to SQLITE_OK unless an I/O error occurs during lock checking.
34729*/
34730static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
34731 int rc = SQLITE_OK;
34732 int reserved = 0;
34733 unixFile *pFile = (unixFile*)id;
34734
34735 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
34736
34737 assert( pFile );
34738
34739 /* Check if a thread in this process holds such a lock */
34740 if( pFile->eFileLock>SHARED_LOCK ){
34741 reserved = 1;
34742 }
34743
34744 /* Otherwise see if some other process holds it. */
34745 if( !reserved ){
34746 /* attempt to get the lock */
34747 int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
34748 if( !lrc ){
34749 /* got the lock, unlock it */
34750 lrc = robust_flock(pFile->h, LOCK_UN);
34751 if ( lrc ) {
34752 int tErrno = errno;
34753 /* unlock failed with an error */
34754 lrc = SQLITE_IOERR_UNLOCK;
34755 storeLastErrno(pFile, tErrno);
34756 rc = lrc;
34757 }
34758 } else {
34759 int tErrno = errno;
34760 reserved = 1;
34761 /* someone else might have it reserved */
34762 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
34763 if( IS_LOCK_ERROR(lrc) ){
34764 storeLastErrno(pFile, tErrno);
34765 rc = lrc;
34766 }
34767 }
34768 }
34769 OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
34770
34771#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
34772 if( (rc & 0xff) == SQLITE_IOERR ){
34773 rc = SQLITE_OK;
34774 reserved=1;
34775 }
34776#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
34777 *pResOut = reserved;
34778 return rc;
34779}
34780
34781/*
34782** Lock the file with the lock specified by parameter eFileLock - one
34783** of the following:
34784**
34785** (1) SHARED_LOCK
34786** (2) RESERVED_LOCK
34787** (3) PENDING_LOCK
34788** (4) EXCLUSIVE_LOCK
34789**
34790** Sometimes when requesting one lock state, additional lock states
34791** are inserted in between. The locking might fail on one of the later
34792** transitions leaving the lock state different from what it started but
34793** still short of its goal. The following chart shows the allowed
34794** transitions and the inserted intermediate states:
34795**
34796** UNLOCKED -> SHARED
34797** SHARED -> RESERVED
34798** SHARED -> (PENDING) -> EXCLUSIVE
34799** RESERVED -> (PENDING) -> EXCLUSIVE
34800** PENDING -> EXCLUSIVE
34801**
34802** flock() only really support EXCLUSIVE locks. We track intermediate
34803** lock states in the sqlite3_file structure, but all locks SHARED or
34804** above are really EXCLUSIVE locks and exclude all other processes from
34805** access the file.
34806**
34807** This routine will only increase a lock. Use the sqlite3OsUnlock()
34808** routine to lower a locking level.
34809*/
34810static int flockLock(sqlite3_file *id, int eFileLock) {
34811 int rc = SQLITE_OK;
34812 unixFile *pFile = (unixFile*)id;
34813
34814 assert( pFile );
34815
34816 /* if we already have a lock, it is exclusive.
34817 ** Just adjust level and punt on outta here. */
34818 if (pFile->eFileLock > NO_LOCK) {
34819 pFile->eFileLock = eFileLock;
34820 return SQLITE_OK;
34821 }
34822
34823 /* grab an exclusive lock */
34824
34825 if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
34826 int tErrno = errno;
34827 /* didn't get, must be busy */
34828 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
34829 if( IS_LOCK_ERROR(rc) ){
34830 storeLastErrno(pFile, tErrno);
34831 }
34832 } else {
34833 /* got it, set the type and return ok */
34834 pFile->eFileLock = eFileLock;
34835 }
34836 OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
34837 rc==SQLITE_OK ? "ok" : "failed"));
34838#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
34839 if( (rc & 0xff) == SQLITE_IOERR ){
34840 rc = SQLITE_BUSY;
34841 }
34842#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
34843 return rc;
34844}
34845
34846
34847/*
34848** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
34849** must be either NO_LOCK or SHARED_LOCK.
34850**
34851** If the locking level of the file descriptor is already at or below
34852** the requested locking level, this routine is a no-op.
34853*/
34854static int flockUnlock(sqlite3_file *id, int eFileLock) {
34855 unixFile *pFile = (unixFile*)id;
34856
34857 assert( pFile );
34858 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
34859 pFile->eFileLock, osGetpid(0)));
34860 assert( eFileLock<=SHARED_LOCK );
34861
34862 /* no-op if possible */
34863 if( pFile->eFileLock==eFileLock ){
34864 return SQLITE_OK;
34865 }
34866
34867 /* shared can just be set because we always have an exclusive */
34868 if (eFileLock==SHARED_LOCK) {
34869 pFile->eFileLock = eFileLock;
34870 return SQLITE_OK;
34871 }
34872
34873 /* no, really, unlock. */
34874 if( robust_flock(pFile->h, LOCK_UN) ){
34875#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
34876 return SQLITE_OK;
34877#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
34878 return SQLITE_IOERR_UNLOCK;
34879 }else{
34880 pFile->eFileLock = NO_LOCK;
34881 return SQLITE_OK;
34882 }
34883}
34884
34885/*
34886** Close a file.
34887*/
34888static int flockClose(sqlite3_file *id) {
34889 assert( id!=0 );
34890 flockUnlock(id, NO_LOCK);
34891 return closeUnixFile(id);
34892}
34893
34894#endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
34895
34896/******************* End of the flock lock implementation *********************
34897******************************************************************************/
34898
34899/******************************************************************************
34900************************ Begin Named Semaphore Locking ************************
34901**
34902** Named semaphore locking is only supported on VxWorks.
34903**
34904** Semaphore locking is like dot-lock and flock in that it really only
34905** supports EXCLUSIVE locking. Only a single process can read or write
34906** the database file at a time. This reduces potential concurrency, but
34907** makes the lock implementation much easier.
34908*/
34909#if OS_VXWORKS
34910
34911/*
34912** This routine checks if there is a RESERVED lock held on the specified
34913** file by this or any other process. If such a lock is held, set *pResOut
34914** to a non-zero value otherwise *pResOut is set to zero. The return value
34915** is set to SQLITE_OK unless an I/O error occurs during lock checking.
34916*/
34917static int semXCheckReservedLock(sqlite3_file *id, int *pResOut) {
34918 int rc = SQLITE_OK;
34919 int reserved = 0;
34920 unixFile *pFile = (unixFile*)id;
34921
34922 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
34923
34924 assert( pFile );
34925
34926 /* Check if a thread in this process holds such a lock */
34927 if( pFile->eFileLock>SHARED_LOCK ){
34928 reserved = 1;
34929 }
34930
34931 /* Otherwise see if some other process holds it. */
34932 if( !reserved ){
34933 sem_t *pSem = pFile->pInode->pSem;
34934
34935 if( sem_trywait(pSem)==-1 ){
34936 int tErrno = errno;
34937 if( EAGAIN != tErrno ){
34938 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
34939 storeLastErrno(pFile, tErrno);
34940 } else {
34941 /* someone else has the lock when we are in NO_LOCK */
34942 reserved = (pFile->eFileLock < SHARED_LOCK);
34943 }
34944 }else{
34945 /* we could have it if we want it */
34946 sem_post(pSem);
34947 }
34948 }
34949 OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
34950
34951 *pResOut = reserved;
34952 return rc;
34953}
34954
34955/*
34956** Lock the file with the lock specified by parameter eFileLock - one
34957** of the following:
34958**
34959** (1) SHARED_LOCK
34960** (2) RESERVED_LOCK
34961** (3) PENDING_LOCK
34962** (4) EXCLUSIVE_LOCK
34963**
34964** Sometimes when requesting one lock state, additional lock states
34965** are inserted in between. The locking might fail on one of the later
34966** transitions leaving the lock state different from what it started but
34967** still short of its goal. The following chart shows the allowed
34968** transitions and the inserted intermediate states:
34969**
34970** UNLOCKED -> SHARED
34971** SHARED -> RESERVED
34972** SHARED -> (PENDING) -> EXCLUSIVE
34973** RESERVED -> (PENDING) -> EXCLUSIVE
34974** PENDING -> EXCLUSIVE
34975**
34976** Semaphore locks only really support EXCLUSIVE locks. We track intermediate
34977** lock states in the sqlite3_file structure, but all locks SHARED or
34978** above are really EXCLUSIVE locks and exclude all other processes from
34979** access the file.
34980**
34981** This routine will only increase a lock. Use the sqlite3OsUnlock()
34982** routine to lower a locking level.
34983*/
34984static int semXLock(sqlite3_file *id, int eFileLock) {
34985 unixFile *pFile = (unixFile*)id;
34986 sem_t *pSem = pFile->pInode->pSem;
34987 int rc = SQLITE_OK;
34988
34989 /* if we already have a lock, it is exclusive.
34990 ** Just adjust level and punt on outta here. */
34991 if (pFile->eFileLock > NO_LOCK) {
34992 pFile->eFileLock = eFileLock;
34993 rc = SQLITE_OK;
34994 goto sem_end_lock;
34995 }
34996
34997 /* lock semaphore now but bail out when already locked. */
34998 if( sem_trywait(pSem)==-1 ){
34999 rc = SQLITE_BUSY;
35000 goto sem_end_lock;
35001 }
35002
35003 /* got it, set the type and return ok */
35004 pFile->eFileLock = eFileLock;
35005
35006 sem_end_lock:
35007 return rc;
35008}
35009
35010/*
35011** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
35012** must be either NO_LOCK or SHARED_LOCK.
35013**
35014** If the locking level of the file descriptor is already at or below
35015** the requested locking level, this routine is a no-op.
35016*/
35017static int semXUnlock(sqlite3_file *id, int eFileLock) {
35018 unixFile *pFile = (unixFile*)id;
35019 sem_t *pSem = pFile->pInode->pSem;
35020
35021 assert( pFile );
35022 assert( pSem );
35023 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
35024 pFile->eFileLock, osGetpid(0)));
35025 assert( eFileLock<=SHARED_LOCK );
35026
35027 /* no-op if possible */
35028 if( pFile->eFileLock==eFileLock ){
35029 return SQLITE_OK;
35030 }
35031
35032 /* shared can just be set because we always have an exclusive */
35033 if (eFileLock==SHARED_LOCK) {
35034 pFile->eFileLock = eFileLock;
35035 return SQLITE_OK;
35036 }
35037
35038 /* no, really unlock. */
35039 if ( sem_post(pSem)==-1 ) {
35040 int rc, tErrno = errno;
35041 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
35042 if( IS_LOCK_ERROR(rc) ){
35043 storeLastErrno(pFile, tErrno);
35044 }
35045 return rc;
35046 }
35047 pFile->eFileLock = NO_LOCK;
35048 return SQLITE_OK;
35049}
35050
35051/*
35052 ** Close a file.
35053 */
35054static int semXClose(sqlite3_file *id) {
35055 if( id ){
35056 unixFile *pFile = (unixFile*)id;
35057 semXUnlock(id, NO_LOCK);
35058 assert( pFile );
35059 assert( unixFileMutexNotheld(pFile) );
35060 unixEnterMutex();
35061 releaseInodeInfo(pFile);
35062 unixLeaveMutex();
35063 closeUnixFile(id);
35064 }
35065 return SQLITE_OK;
35066}
35067
35068#endif /* OS_VXWORKS */
35069/*
35070** Named semaphore locking is only available on VxWorks.
35071**
35072*************** End of the named semaphore lock implementation ****************
35073******************************************************************************/
35074
35075
35076/******************************************************************************
35077*************************** Begin AFP Locking *********************************
35078**
35079** AFP is the Apple Filing Protocol. AFP is a network filesystem found
35080** on Apple Macintosh computers - both OS9 and OSX.
35081**
35082** Third-party implementations of AFP are available. But this code here
35083** only works on OSX.
35084*/
35085
35086#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
35087/*
35088** The afpLockingContext structure contains all afp lock specific state
35089*/
35090typedef struct afpLockingContext afpLockingContext;
35091struct afpLockingContext {
35092 int reserved;
35093 const char *dbPath; /* Name of the open file */
35094};
35095
35096struct ByteRangeLockPB2
35097{
35098 unsigned long long offset; /* offset to first byte to lock */
35099 unsigned long long length; /* nbr of bytes to lock */
35100 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
35101 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
35102 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
35103 int fd; /* file desc to assoc this lock with */
35104};
35105
35106#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)
35107
35108/*
35109** This is a utility for setting or clearing a bit-range lock on an
35110** AFP filesystem.
35111**
35112** Return SQLITE_OK on success, SQLITE_BUSY on failure.
35113*/
35114static int afpSetLock(
35115 const char *path, /* Name of the file to be locked or unlocked */
35116 unixFile *pFile, /* Open file descriptor on path */
35117 unsigned long long offset, /* First byte to be locked */
35118 unsigned long long length, /* Number of bytes to lock */
35119 int setLockFlag /* True to set lock. False to clear lock */
35120){
35121 struct ByteRangeLockPB2 pb;
35122 int err;
35123
35124 pb.unLockFlag = setLockFlag ? 0 : 1;
35125 pb.startEndFlag = 0;
35126 pb.offset = offset;
35127 pb.length = length;
35128 pb.fd = pFile->h;
35129
35130 OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
35131 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
35132 offset, length));
35133 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
35134 if ( err==-1 ) {
35135 int rc;
35136 int tErrno = errno;
35137 OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
35138 path, tErrno, strerror(tErrno)));
35139#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
35140 rc = SQLITE_BUSY;
35141#else
35142 rc = sqliteErrorFromPosixError(tErrno,
35143 setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
35144#endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
35145 if( IS_LOCK_ERROR(rc) ){
35146 storeLastErrno(pFile, tErrno);
35147 }
35148 return rc;
35149 } else {
35150 return SQLITE_OK;
35151 }
35152}
35153
35154/*
35155** This routine checks if there is a RESERVED lock held on the specified
35156** file by this or any other process. If such a lock is held, set *pResOut
35157** to a non-zero value otherwise *pResOut is set to zero. The return value
35158** is set to SQLITE_OK unless an I/O error occurs during lock checking.
35159*/
35160static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
35161 int rc = SQLITE_OK;
35162 int reserved = 0;
35163 unixFile *pFile = (unixFile*)id;
35164 afpLockingContext *context;
35165
35166 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
35167
35168 assert( pFile );
35169 context = (afpLockingContext *) pFile->lockingContext;
35170 if( context->reserved ){
35171 *pResOut = 1;
35172 return SQLITE_OK;
35173 }
35174 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
35175 /* Check if a thread in this process holds such a lock */
35176 if( pFile->pInode->eFileLock>SHARED_LOCK ){
35177 reserved = 1;
35178 }
35179
35180 /* Otherwise see if some other process holds it.
35181 */
35182 if( !reserved ){
35183 /* lock the RESERVED byte */
35184 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
35185 if( SQLITE_OK==lrc ){
35186 /* if we succeeded in taking the reserved lock, unlock it to restore
35187 ** the original state */
35188 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
35189 } else {
35190 /* if we failed to get the lock then someone else must have it */
35191 reserved = 1;
35192 }
35193 if( IS_LOCK_ERROR(lrc) ){
35194 rc=lrc;
35195 }
35196 }
35197
35198 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
35199 OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
35200
35201 *pResOut = reserved;
35202 return rc;
35203}
35204
35205/*
35206** Lock the file with the lock specified by parameter eFileLock - one
35207** of the following:
35208**
35209** (1) SHARED_LOCK
35210** (2) RESERVED_LOCK
35211** (3) PENDING_LOCK
35212** (4) EXCLUSIVE_LOCK
35213**
35214** Sometimes when requesting one lock state, additional lock states
35215** are inserted in between. The locking might fail on one of the later
35216** transitions leaving the lock state different from what it started but
35217** still short of its goal. The following chart shows the allowed
35218** transitions and the inserted intermediate states:
35219**
35220** UNLOCKED -> SHARED
35221** SHARED -> RESERVED
35222** SHARED -> (PENDING) -> EXCLUSIVE
35223** RESERVED -> (PENDING) -> EXCLUSIVE
35224** PENDING -> EXCLUSIVE
35225**
35226** This routine will only increase a lock. Use the sqlite3OsUnlock()
35227** routine to lower a locking level.
35228*/
35229static int afpLock(sqlite3_file *id, int eFileLock){
35230 int rc = SQLITE_OK;
35231 unixFile *pFile = (unixFile*)id;
35232 unixInodeInfo *pInode = pFile->pInode;
35233 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
35234
35235 assert( pFile );
35236 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
35237 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
35238 azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
35239
35240 /* If there is already a lock of this type or more restrictive on the
35241 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
35242 ** unixEnterMutex() hasn't been called yet.
35243 */
35244 if( pFile->eFileLock>=eFileLock ){
35245 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h,
35246 azFileLock(eFileLock)));
35247 return SQLITE_OK;
35248 }
35249
35250 /* Make sure the locking sequence is correct
35251 ** (1) We never move from unlocked to anything higher than shared lock.
35252 ** (2) SQLite never explicitly requests a pendig lock.
35253 ** (3) A shared lock is always held when a reserve lock is requested.
35254 */
35255 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
35256 assert( eFileLock!=PENDING_LOCK );
35257 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
35258
35259 /* This mutex is needed because pFile->pInode is shared across threads
35260 */
35261 pInode = pFile->pInode;
35262 sqlite3_mutex_enter(pInode->pLockMutex);
35263
35264 /* If some thread using this PID has a lock via a different unixFile*
35265 ** handle that precludes the requested lock, return BUSY.
35266 */
35267 if( (pFile->eFileLock!=pInode->eFileLock &&
35268 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
35269 ){
35270 rc = SQLITE_BUSY;
35271 goto afp_end_lock;
35272 }
35273
35274 /* If a SHARED lock is requested, and some thread using this PID already
35275 ** has a SHARED or RESERVED lock, then increment reference counts and
35276 ** return SQLITE_OK.
35277 */
35278 if( eFileLock==SHARED_LOCK &&
35279 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
35280 assert( eFileLock==SHARED_LOCK );
35281 assert( pFile->eFileLock==0 );
35282 assert( pInode->nShared>0 );
35283 pFile->eFileLock = SHARED_LOCK;
35284 pInode->nShared++;
35285 pInode->nLock++;
35286 goto afp_end_lock;
35287 }
35288
35289 /* A PENDING lock is needed before acquiring a SHARED lock and before
35290 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
35291 ** be released.
35292 */
35293 if( eFileLock==SHARED_LOCK
35294 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
35295 ){
35296 int failed;
35297 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
35298 if (failed) {
35299 rc = failed;
35300 goto afp_end_lock;
35301 }
35302 }
35303
35304 /* If control gets to this point, then actually go ahead and make
35305 ** operating system calls for the specified lock.
35306 */
35307 if( eFileLock==SHARED_LOCK ){
35308 int lrc1, lrc2, lrc1Errno = 0;
35309 long lk, mask;
35310
35311 assert( pInode->nShared==0 );
35312 assert( pInode->eFileLock==0 );
35313
35314 mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
35315 /* Now get the read-lock SHARED_LOCK */
35316 /* note that the quality of the randomness doesn't matter that much */
35317 lk = random();
35318 pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
35319 lrc1 = afpSetLock(context->dbPath, pFile,
35320 SHARED_FIRST+pInode->sharedByte, 1, 1);
35321 if( IS_LOCK_ERROR(lrc1) ){
35322 lrc1Errno = pFile->lastErrno;
35323 }
35324 /* Drop the temporary PENDING lock */
35325 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
35326
35327 if( IS_LOCK_ERROR(lrc1) ) {
35328 storeLastErrno(pFile, lrc1Errno);
35329 rc = lrc1;
35330 goto afp_end_lock;
35331 } else if( IS_LOCK_ERROR(lrc2) ){
35332 rc = lrc2;
35333 goto afp_end_lock;
35334 } else if( lrc1 != SQLITE_OK ) {
35335 rc = lrc1;
35336 } else {
35337 pFile->eFileLock = SHARED_LOCK;
35338 pInode->nLock++;
35339 pInode->nShared = 1;
35340 }
35341 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
35342 /* We are trying for an exclusive lock but another thread in this
35343 ** same process is still holding a shared lock. */
35344 rc = SQLITE_BUSY;
35345 }else{
35346 /* The request was for a RESERVED or EXCLUSIVE lock. It is
35347 ** assumed that there is a SHARED or greater lock on the file
35348 ** already.
35349 */
35350 int failed = 0;
35351 assert( 0!=pFile->eFileLock );
35352 if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
35353 /* Acquire a RESERVED lock */
35354 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
35355 if( !failed ){
35356 context->reserved = 1;
35357 }
35358 }
35359 if (!failed && eFileLock == EXCLUSIVE_LOCK) {
35360 /* Acquire an EXCLUSIVE lock */
35361
35362 /* Remove the shared lock before trying the range. we'll need to
35363 ** reestablish the shared lock if we can't get the afpUnlock
35364 */
35365 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
35366 pInode->sharedByte, 1, 0)) ){
35367 int failed2 = SQLITE_OK;
35368 /* now attemmpt to get the exclusive lock range */
35369 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
35370 SHARED_SIZE, 1);
35371 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
35372 SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
35373 /* Can't reestablish the shared lock. Sqlite can't deal, this is
35374 ** a critical I/O error
35375 */
35376 rc = ((failed & 0xff) == SQLITE_IOERR) ? failed2 :
35377 SQLITE_IOERR_LOCK;
35378 goto afp_end_lock;
35379 }
35380 }else{
35381 rc = failed;
35382 }
35383 }
35384 if( failed ){
35385 rc = failed;
35386 }
35387 }
35388
35389 if( rc==SQLITE_OK ){
35390 pFile->eFileLock = eFileLock;
35391 pInode->eFileLock = eFileLock;
35392 }else if( eFileLock==EXCLUSIVE_LOCK ){
35393 pFile->eFileLock = PENDING_LOCK;
35394 pInode->eFileLock = PENDING_LOCK;
35395 }
35396
35397afp_end_lock:
35398 sqlite3_mutex_leave(pInode->pLockMutex);
35399 OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
35400 rc==SQLITE_OK ? "ok" : "failed"));
35401 return rc;
35402}
35403
35404/*
35405** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
35406** must be either NO_LOCK or SHARED_LOCK.
35407**
35408** If the locking level of the file descriptor is already at or below
35409** the requested locking level, this routine is a no-op.
35410*/
35411static int afpUnlock(sqlite3_file *id, int eFileLock) {
35412 int rc = SQLITE_OK;
35413 unixFile *pFile = (unixFile*)id;
35414 unixInodeInfo *pInode;
35415 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
35416 int skipShared = 0;
35417#ifdef SQLITE_TEST
35418 int h = pFile->h;
35419#endif
35420
35421 assert( pFile );
35422 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
35423 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
35424 osGetpid(0)));
35425
35426 assert( eFileLock<=SHARED_LOCK );
35427 if( pFile->eFileLock<=eFileLock ){
35428 return SQLITE_OK;
35429 }
35430 pInode = pFile->pInode;
35431 sqlite3_mutex_enter(pInode->pLockMutex);
35432 assert( pInode->nShared!=0 );
35433 if( pFile->eFileLock>SHARED_LOCK ){
35434 assert( pInode->eFileLock==pFile->eFileLock );
35435 SimulateIOErrorBenign(1);
35436 SimulateIOError( h=(-1) )
35437 SimulateIOErrorBenign(0);
35438
35439#ifdef SQLITE_DEBUG
35440 /* When reducing a lock such that other processes can start
35441 ** reading the database file again, make sure that the
35442 ** transaction counter was updated if any part of the database
35443 ** file changed. If the transaction counter is not updated,
35444 ** other connections to the same file might not realize that
35445 ** the file has changed and hence might not know to flush their
35446 ** cache. The use of a stale cache can lead to database corruption.
35447 */
35448 assert( pFile->inNormalWrite==0
35449 || pFile->dbUpdate==0
35450 || pFile->transCntrChng==1 );
35451 pFile->inNormalWrite = 0;
35452#endif
35453
35454 if( pFile->eFileLock==EXCLUSIVE_LOCK ){
35455 rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
35456 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
35457 /* only re-establish the shared lock if necessary */
35458 int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
35459 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
35460 } else {
35461 skipShared = 1;
35462 }
35463 }
35464 if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
35465 rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
35466 }
35467 if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
35468 rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
35469 if( !rc ){
35470 context->reserved = 0;
35471 }
35472 }
35473 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
35474 pInode->eFileLock = SHARED_LOCK;
35475 }
35476 }
35477 if( rc==SQLITE_OK && eFileLock==NO_LOCK ){
35478
35479 /* Decrement the shared lock counter. Release the lock using an
35480 ** OS call only when all threads in this same process have released
35481 ** the lock.
35482 */
35483 unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
35484 pInode->nShared--;
35485 if( pInode->nShared==0 ){
35486 SimulateIOErrorBenign(1);
35487 SimulateIOError( h=(-1) )
35488 SimulateIOErrorBenign(0);
35489 if( !skipShared ){
35490 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
35491 }
35492 if( !rc ){
35493 pInode->eFileLock = NO_LOCK;
35494 pFile->eFileLock = NO_LOCK;
35495 }
35496 }
35497 if( rc==SQLITE_OK ){
35498 pInode->nLock--;
35499 assert( pInode->nLock>=0 );
35500 if( pInode->nLock==0 ) closePendingFds(pFile);
35501 }
35502 }
35503
35504 sqlite3_mutex_leave(pInode->pLockMutex);
35505 if( rc==SQLITE_OK ){
35506 pFile->eFileLock = eFileLock;
35507 }
35508 return rc;
35509}
35510
35511/*
35512** Close a file & cleanup AFP specific locking context
35513*/
35514static int afpClose(sqlite3_file *id) {
35515 int rc = SQLITE_OK;
35516 unixFile *pFile = (unixFile*)id;
35517 assert( id!=0 );
35518 afpUnlock(id, NO_LOCK);
35519 assert( unixFileMutexNotheld(pFile) );
35520 unixEnterMutex();
35521 if( pFile->pInode ){
35522 unixInodeInfo *pInode = pFile->pInode;
35523 sqlite3_mutex_enter(pInode->pLockMutex);
35524 if( pInode->nLock ){
35525 /* If there are outstanding locks, do not actually close the file just
35526 ** yet because that would clear those locks. Instead, add the file
35527 ** descriptor to pInode->aPending. It will be automatically closed when
35528 ** the last lock is cleared.
35529 */
35530 setPendingFd(pFile);
35531 }
35532 sqlite3_mutex_leave(pInode->pLockMutex);
35533 }
35534 releaseInodeInfo(pFile);
35535 sqlite3_free(pFile->lockingContext);
35536 rc = closeUnixFile(id);
35537 unixLeaveMutex();
35538 return rc;
35539}
35540
35541#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
35542/*
35543** The code above is the AFP lock implementation. The code is specific
35544** to MacOSX and does not work on other unix platforms. No alternative
35545** is available. If you don't compile for a mac, then the "unix-afp"
35546** VFS is not available.
35547**
35548********************* End of the AFP lock implementation **********************
35549******************************************************************************/
35550
35551/******************************************************************************
35552*************************** Begin NFS Locking ********************************/
35553
35554#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
35555/*
35556 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
35557 ** must be either NO_LOCK or SHARED_LOCK.
35558 **
35559 ** If the locking level of the file descriptor is already at or below
35560 ** the requested locking level, this routine is a no-op.
35561 */
35562static int nfsUnlock(sqlite3_file *id, int eFileLock){
35563 return posixUnlock(id, eFileLock, 1);
35564}
35565
35566#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
35567/*
35568** The code above is the NFS lock implementation. The code is specific
35569** to MacOSX and does not work on other unix platforms. No alternative
35570** is available.
35571**
35572********************* End of the NFS lock implementation **********************
35573******************************************************************************/
35574
35575/******************************************************************************
35576**************** Non-locking sqlite3_file methods *****************************
35577**
35578** The next division contains implementations for all methods of the
35579** sqlite3_file object other than the locking methods. The locking
35580** methods were defined in divisions above (one locking method per
35581** division). Those methods that are common to all locking modes
35582** are gather together into this division.
35583*/
35584
35585/*
35586** Seek to the offset passed as the second argument, then read cnt
35587** bytes into pBuf. Return the number of bytes actually read.
35588**
35589** NB: If you define USE_PREAD or USE_PREAD64, then it might also
35590** be necessary to define _XOPEN_SOURCE to be 500. This varies from
35591** one system to another. Since SQLite does not define USE_PREAD
35592** in any form by default, we will not attempt to define _XOPEN_SOURCE.
35593** See tickets #2741 and #2681.
35594**
35595** To avoid stomping the errno value on a failed read the lastErrno value
35596** is set before returning.
35597*/
35598static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
35599 int got;
35600 int prior = 0;
35601#if (!defined(USE_PREAD) && !defined(USE_PREAD64))
35602 i64 newOffset;
35603#endif
35604 TIMER_START;
35605 assert( cnt==(cnt&0x1ffff) );
35606 assert( id->h>2 );
35607 do{
35608#if defined(USE_PREAD)
35609 got = osPread(id->h, pBuf, cnt, offset);
35610 SimulateIOError( got = -1 );
35611#elif defined(USE_PREAD64)
35612 got = osPread64(id->h, pBuf, cnt, offset);
35613 SimulateIOError( got = -1 );
35614#else
35615 newOffset = lseek(id->h, offset, SEEK_SET);
35616 SimulateIOError( newOffset = -1 );
35617 if( newOffset<0 ){
35618 storeLastErrno((unixFile*)id, errno);
35619 return -1;
35620 }
35621 got = osRead(id->h, pBuf, cnt);
35622#endif
35623 if( got==cnt ) break;
35624 if( got<0 ){
35625 if( errno==EINTR ){ got = 1; continue; }
35626 prior = 0;
35627 storeLastErrno((unixFile*)id, errno);
35628 break;
35629 }else if( got>0 ){
35630 cnt -= got;
35631 offset += got;
35632 prior += got;
35633 pBuf = (void*)(got + (char*)pBuf);
35634 }
35635 }while( got>0 );
35636 TIMER_END;
35637 OSTRACE(("READ %-3d %5d %7lld %llu\n",
35638 id->h, got+prior, offset-prior, TIMER_ELAPSED));
35639 return got+prior;
35640}
35641
35642/*
35643** Read data from a file into a buffer. Return SQLITE_OK if all
35644** bytes were read successfully and SQLITE_IOERR if anything goes
35645** wrong.
35646*/
35647static int unixRead(
35648 sqlite3_file *id,
35649 void *pBuf,
35650 int amt,
35651 sqlite3_int64 offset
35652){
35653 unixFile *pFile = (unixFile *)id;
35654 int got;
35655 assert( id );
35656 assert( offset>=0 );
35657 assert( amt>0 );
35658
35659 /* If this is a database file (not a journal, master-journal or temp
35660 ** file), the bytes in the locking range should never be read or written. */
35661#if 0
35662 assert( pFile->pPreallocatedUnused==0
35663 || offset>=PENDING_BYTE+512
35664 || offset+amt<=PENDING_BYTE
35665 );
35666#endif
35667
35668#if SQLITE_MAX_MMAP_SIZE>0
35669 /* Deal with as much of this read request as possible by transfering
35670 ** data from the memory mapping using memcpy(). */
35671 if( offset<pFile->mmapSize ){
35672 if( offset+amt <= pFile->mmapSize ){
35673 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
35674 return SQLITE_OK;
35675 }else{
35676 int nCopy = pFile->mmapSize - offset;
35677 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
35678 pBuf = &((u8 *)pBuf)[nCopy];
35679 amt -= nCopy;
35680 offset += nCopy;
35681 }
35682 }
35683#endif
35684
35685 got = seekAndRead(pFile, offset, pBuf, amt);
35686 if( got==amt ){
35687 return SQLITE_OK;
35688 }else if( got<0 ){
35689 /* lastErrno set by seekAndRead */
35690 return SQLITE_IOERR_READ;
35691 }else{
35692 storeLastErrno(pFile, 0); /* not a system error */
35693 /* Unread parts of the buffer must be zero-filled */
35694 memset(&((char*)pBuf)[got], 0, amt-got);
35695 return SQLITE_IOERR_SHORT_READ;
35696 }
35697}
35698
35699/*
35700** Attempt to seek the file-descriptor passed as the first argument to
35701** absolute offset iOff, then attempt to write nBuf bytes of data from
35702** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
35703** return the actual number of bytes written (which may be less than
35704** nBuf).
35705*/
35706static int seekAndWriteFd(
35707 int fd, /* File descriptor to write to */
35708 i64 iOff, /* File offset to begin writing at */
35709 const void *pBuf, /* Copy data from this buffer to the file */
35710 int nBuf, /* Size of buffer pBuf in bytes */
35711 int *piErrno /* OUT: Error number if error occurs */
35712){
35713 int rc = 0; /* Value returned by system call */
35714
35715 assert( nBuf==(nBuf&0x1ffff) );
35716 assert( fd>2 );
35717 assert( piErrno!=0 );
35718 nBuf &= 0x1ffff;
35719 TIMER_START;
35720
35721#if defined(USE_PREAD)
35722 do{ rc = (int)osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR );
35723#elif defined(USE_PREAD64)
35724 do{ rc = (int)osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR);
35725#else
35726 do{
35727 i64 iSeek = lseek(fd, iOff, SEEK_SET);
35728 SimulateIOError( iSeek = -1 );
35729 if( iSeek<0 ){
35730 rc = -1;
35731 break;
35732 }
35733 rc = osWrite(fd, pBuf, nBuf);
35734 }while( rc<0 && errno==EINTR );
35735#endif
35736
35737 TIMER_END;
35738 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
35739
35740 if( rc<0 ) *piErrno = errno;
35741 return rc;
35742}
35743
35744
35745/*
35746** Seek to the offset in id->offset then read cnt bytes into pBuf.
35747** Return the number of bytes actually read. Update the offset.
35748**
35749** To avoid stomping the errno value on a failed write the lastErrno value
35750** is set before returning.
35751*/
35752static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
35753 return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
35754}
35755
35756
35757/*
35758** Write data from a buffer into a file. Return SQLITE_OK on success
35759** or some other error code on failure.
35760*/
35761static int unixWrite(
35762 sqlite3_file *id,
35763 const void *pBuf,
35764 int amt,
35765 sqlite3_int64 offset
35766){
35767 unixFile *pFile = (unixFile*)id;
35768 int wrote = 0;
35769 assert( id );
35770 assert( amt>0 );
35771
35772 /* If this is a database file (not a journal, master-journal or temp
35773 ** file), the bytes in the locking range should never be read or written. */
35774#if 0
35775 assert( pFile->pPreallocatedUnused==0
35776 || offset>=PENDING_BYTE+512
35777 || offset+amt<=PENDING_BYTE
35778 );
35779#endif
35780
35781#ifdef SQLITE_DEBUG
35782 /* If we are doing a normal write to a database file (as opposed to
35783 ** doing a hot-journal rollback or a write to some file other than a
35784 ** normal database file) then record the fact that the database
35785 ** has changed. If the transaction counter is modified, record that
35786 ** fact too.
35787 */
35788 if( pFile->inNormalWrite ){
35789 pFile->dbUpdate = 1; /* The database has been modified */
35790 if( offset<=24 && offset+amt>=27 ){
35791 int rc;
35792 char oldCntr[4];
35793 SimulateIOErrorBenign(1);
35794 rc = seekAndRead(pFile, 24, oldCntr, 4);
35795 SimulateIOErrorBenign(0);
35796 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
35797 pFile->transCntrChng = 1; /* The transaction counter has changed */
35798 }
35799 }
35800 }
35801#endif
35802
35803#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0
35804 /* Deal with as much of this write request as possible by transfering
35805 ** data from the memory mapping using memcpy(). */
35806 if( offset<pFile->mmapSize ){
35807 if( offset+amt <= pFile->mmapSize ){
35808 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
35809 return SQLITE_OK;
35810 }else{
35811 int nCopy = pFile->mmapSize - offset;
35812 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
35813 pBuf = &((u8 *)pBuf)[nCopy];
35814 amt -= nCopy;
35815 offset += nCopy;
35816 }
35817 }
35818#endif
35819
35820 while( (wrote = seekAndWrite(pFile, offset, pBuf, amt))<amt && wrote>0 ){
35821 amt -= wrote;
35822 offset += wrote;
35823 pBuf = &((char*)pBuf)[wrote];
35824 }
35825 SimulateIOError(( wrote=(-1), amt=1 ));
35826 SimulateDiskfullError(( wrote=0, amt=1 ));
35827
35828 if( amt>wrote ){
35829 if( wrote<0 && pFile->lastErrno!=ENOSPC ){
35830 /* lastErrno set by seekAndWrite */
35831 return SQLITE_IOERR_WRITE;
35832 }else{
35833 storeLastErrno(pFile, 0); /* not a system error */
35834 return SQLITE_FULL;
35835 }
35836 }
35837
35838 return SQLITE_OK;
35839}
35840
35841#ifdef SQLITE_TEST
35842/*
35843** Count the number of fullsyncs and normal syncs. This is used to test
35844** that syncs and fullsyncs are occurring at the right times.
35845*/
35846SQLITE_API int sqlite3_sync_count = 0;
35847SQLITE_API int sqlite3_fullsync_count = 0;
35848#endif
35849
35850/*
35851** We do not trust systems to provide a working fdatasync(). Some do.
35852** Others do no. To be safe, we will stick with the (slightly slower)
35853** fsync(). If you know that your system does support fdatasync() correctly,
35854** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
35855*/
35856#if !defined(fdatasync) && !HAVE_FDATASYNC
35857# define fdatasync fsync
35858#endif
35859
35860/*
35861** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
35862** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
35863** only available on Mac OS X. But that could change.
35864*/
35865#ifdef F_FULLFSYNC
35866# define HAVE_FULLFSYNC 1
35867#else
35868# define HAVE_FULLFSYNC 0
35869#endif
35870
35871
35872/*
35873** The fsync() system call does not work as advertised on many
35874** unix systems. The following procedure is an attempt to make
35875** it work better.
35876**
35877** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
35878** for testing when we want to run through the test suite quickly.
35879** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
35880** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
35881** or power failure will likely corrupt the database file.
35882**
35883** SQLite sets the dataOnly flag if the size of the file is unchanged.
35884** The idea behind dataOnly is that it should only write the file content
35885** to disk, not the inode. We only set dataOnly if the file size is
35886** unchanged since the file size is part of the inode. However,
35887** Ted Ts'o tells us that fdatasync() will also write the inode if the
35888** file size has changed. The only real difference between fdatasync()
35889** and fsync(), Ted tells us, is that fdatasync() will not flush the
35890** inode if the mtime or owner or other inode attributes have changed.
35891** We only care about the file size, not the other file attributes, so
35892** as far as SQLite is concerned, an fdatasync() is always adequate.
35893** So, we always use fdatasync() if it is available, regardless of
35894** the value of the dataOnly flag.
35895*/
35896static int full_fsync(int fd, int fullSync, int dataOnly){
35897 int rc;
35898
35899 /* The following "ifdef/elif/else/" block has the same structure as
35900 ** the one below. It is replicated here solely to avoid cluttering
35901 ** up the real code with the UNUSED_PARAMETER() macros.
35902 */
35903#ifdef SQLITE_NO_SYNC
35904 UNUSED_PARAMETER(fd);
35905 UNUSED_PARAMETER(fullSync);
35906 UNUSED_PARAMETER(dataOnly);
35907#elif HAVE_FULLFSYNC
35908 UNUSED_PARAMETER(dataOnly);
35909#else
35910 UNUSED_PARAMETER(fullSync);
35911 UNUSED_PARAMETER(dataOnly);
35912#endif
35913
35914 /* Record the number of times that we do a normal fsync() and
35915 ** FULLSYNC. This is used during testing to verify that this procedure
35916 ** gets called with the correct arguments.
35917 */
35918#ifdef SQLITE_TEST
35919 if( fullSync ) sqlite3_fullsync_count++;
35920 sqlite3_sync_count++;
35921#endif
35922
35923 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
35924 ** no-op. But go ahead and call fstat() to validate the file
35925 ** descriptor as we need a method to provoke a failure during
35926 ** coverate testing.
35927 */
35928#ifdef SQLITE_NO_SYNC
35929 {
35930 struct stat buf;
35931 rc = osFstat(fd, &buf);
35932 }
35933#elif HAVE_FULLFSYNC
35934 if( fullSync ){
35935 rc = osFcntl(fd, F_FULLFSYNC, 0);
35936 }else{
35937 rc = 1;
35938 }
35939 /* If the FULLFSYNC failed, fall back to attempting an fsync().
35940 ** It shouldn't be possible for fullfsync to fail on the local
35941 ** file system (on OSX), so failure indicates that FULLFSYNC
35942 ** isn't supported for this file system. So, attempt an fsync
35943 ** and (for now) ignore the overhead of a superfluous fcntl call.
35944 ** It'd be better to detect fullfsync support once and avoid
35945 ** the fcntl call every time sync is called.
35946 */
35947 if( rc ) rc = fsync(fd);
35948
35949#elif defined(__APPLE__)
35950 /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly
35951 ** so currently we default to the macro that redefines fdatasync to fsync
35952 */
35953 rc = fsync(fd);
35954#else
35955 rc = fdatasync(fd);
35956#if OS_VXWORKS
35957 if( rc==-1 && errno==ENOTSUP ){
35958 rc = fsync(fd);
35959 }
35960#endif /* OS_VXWORKS */
35961#endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
35962
35963 if( OS_VXWORKS && rc!= -1 ){
35964 rc = 0;
35965 }
35966 return rc;
35967}
35968
35969/*
35970** Open a file descriptor to the directory containing file zFilename.
35971** If successful, *pFd is set to the opened file descriptor and
35972** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
35973** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
35974** value.
35975**
35976** The directory file descriptor is used for only one thing - to
35977** fsync() a directory to make sure file creation and deletion events
35978** are flushed to disk. Such fsyncs are not needed on newer
35979** journaling filesystems, but are required on older filesystems.
35980**
35981** This routine can be overridden using the xSetSysCall interface.
35982** The ability to override this routine was added in support of the
35983** chromium sandbox. Opening a directory is a security risk (we are
35984** told) so making it overrideable allows the chromium sandbox to
35985** replace this routine with a harmless no-op. To make this routine
35986** a no-op, replace it with a stub that returns SQLITE_OK but leaves
35987** *pFd set to a negative number.
35988**
35989** If SQLITE_OK is returned, the caller is responsible for closing
35990** the file descriptor *pFd using close().
35991*/
35992static int openDirectory(const char *zFilename, int *pFd){
35993 int ii;
35994 int fd = -1;
35995 char zDirname[MAX_PATHNAME+1];
35996
35997 sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
35998 for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--);
35999 if( ii>0 ){
36000 zDirname[ii] = '\0';
36001 }else{
36002 if( zDirname[0]!='/' ) zDirname[0] = '.';
36003 zDirname[1] = 0;
36004 }
36005 fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
36006 if( fd>=0 ){
36007 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
36008 }
36009 *pFd = fd;
36010 if( fd>=0 ) return SQLITE_OK;
36011 return unixLogError(SQLITE_CANTOPEN_BKPT, "openDirectory", zDirname);
36012}
36013
36014/*
36015** Make sure all writes to a particular file are committed to disk.
36016**
36017** If dataOnly==0 then both the file itself and its metadata (file
36018** size, access time, etc) are synced. If dataOnly!=0 then only the
36019** file data is synced.
36020**
36021** Under Unix, also make sure that the directory entry for the file
36022** has been created by fsync-ing the directory that contains the file.
36023** If we do not do this and we encounter a power failure, the directory
36024** entry for the journal might not exist after we reboot. The next
36025** SQLite to access the file will not know that the journal exists (because
36026** the directory entry for the journal was never created) and the transaction
36027** will not roll back - possibly leading to database corruption.
36028*/
36029static int unixSync(sqlite3_file *id, int flags){
36030 int rc;
36031 unixFile *pFile = (unixFile*)id;
36032
36033 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
36034 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
36035
36036 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
36037 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
36038 || (flags&0x0F)==SQLITE_SYNC_FULL
36039 );
36040
36041 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
36042 ** line is to test that doing so does not cause any problems.
36043 */
36044 SimulateDiskfullError( return SQLITE_FULL );
36045
36046 assert( pFile );
36047 OSTRACE(("SYNC %-3d\n", pFile->h));
36048 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
36049 SimulateIOError( rc=1 );
36050 if( rc ){
36051 storeLastErrno(pFile, errno);
36052 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
36053 }
36054
36055 /* Also fsync the directory containing the file if the DIRSYNC flag
36056 ** is set. This is a one-time occurrence. Many systems (examples: AIX)
36057 ** are unable to fsync a directory, so ignore errors on the fsync.
36058 */
36059 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
36060 int dirfd;
36061 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
36062 HAVE_FULLFSYNC, isFullsync));
36063 rc = osOpenDirectory(pFile->zPath, &dirfd);
36064 if( rc==SQLITE_OK ){
36065 full_fsync(dirfd, 0, 0);
36066 robust_close(pFile, dirfd, __LINE__);
36067 }else{
36068 assert( rc==SQLITE_CANTOPEN );
36069 rc = SQLITE_OK;
36070 }
36071 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
36072 }
36073 return rc;
36074}
36075
36076/*
36077** Truncate an open file to a specified size
36078*/
36079static int unixTruncate(sqlite3_file *id, i64 nByte){
36080 unixFile *pFile = (unixFile *)id;
36081 int rc;
36082 assert( pFile );
36083 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
36084
36085 /* If the user has configured a chunk-size for this file, truncate the
36086 ** file so that it consists of an integer number of chunks (i.e. the
36087 ** actual file size after the operation may be larger than the requested
36088 ** size).
36089 */
36090 if( pFile->szChunk>0 ){
36091 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
36092 }
36093
36094 rc = robust_ftruncate(pFile->h, nByte);
36095 if( rc ){
36096 storeLastErrno(pFile, errno);
36097 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
36098 }else{
36099#ifdef SQLITE_DEBUG
36100 /* If we are doing a normal write to a database file (as opposed to
36101 ** doing a hot-journal rollback or a write to some file other than a
36102 ** normal database file) and we truncate the file to zero length,
36103 ** that effectively updates the change counter. This might happen
36104 ** when restoring a database using the backup API from a zero-length
36105 ** source.
36106 */
36107 if( pFile->inNormalWrite && nByte==0 ){
36108 pFile->transCntrChng = 1;
36109 }
36110#endif
36111
36112#if SQLITE_MAX_MMAP_SIZE>0
36113 /* If the file was just truncated to a size smaller than the currently
36114 ** mapped region, reduce the effective mapping size as well. SQLite will
36115 ** use read() and write() to access data beyond this point from now on.
36116 */
36117 if( nByte<pFile->mmapSize ){
36118 pFile->mmapSize = nByte;
36119 }
36120#endif
36121
36122 return SQLITE_OK;
36123 }
36124}
36125
36126/*
36127** Determine the current size of a file in bytes
36128*/
36129static int unixFileSize(sqlite3_file *id, i64 *pSize){
36130 int rc;
36131 struct stat buf;
36132 assert( id );
36133 rc = osFstat(((unixFile*)id)->h, &buf);
36134 SimulateIOError( rc=1 );
36135 if( rc!=0 ){
36136 storeLastErrno((unixFile*)id, errno);
36137 return SQLITE_IOERR_FSTAT;
36138 }
36139 *pSize = buf.st_size;
36140
36141 /* When opening a zero-size database, the findInodeInfo() procedure
36142 ** writes a single byte into that file in order to work around a bug
36143 ** in the OS-X msdos filesystem. In order to avoid problems with upper
36144 ** layers, we need to report this file size as zero even though it is
36145 ** really 1. Ticket #3260.
36146 */
36147 if( *pSize==1 ) *pSize = 0;
36148
36149
36150 return SQLITE_OK;
36151}
36152
36153#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
36154/*
36155** Handler for proxy-locking file-control verbs. Defined below in the
36156** proxying locking division.
36157*/
36158static int proxyFileControl(sqlite3_file*,int,void*);
36159#endif
36160
36161/*
36162** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
36163** file-control operation. Enlarge the database to nBytes in size
36164** (rounded up to the next chunk-size). If the database is already
36165** nBytes or larger, this routine is a no-op.
36166*/
36167static int fcntlSizeHint(unixFile *pFile, i64 nByte){
36168 if( pFile->szChunk>0 ){
36169 i64 nSize; /* Required file size */
36170 struct stat buf; /* Used to hold return values of fstat() */
36171
36172 if( osFstat(pFile->h, &buf) ){
36173 return SQLITE_IOERR_FSTAT;
36174 }
36175
36176 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
36177 if( nSize>(i64)buf.st_size ){
36178
36179#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
36180 /* The code below is handling the return value of osFallocate()
36181 ** correctly. posix_fallocate() is defined to "returns zero on success,
36182 ** or an error number on failure". See the manpage for details. */
36183 int err;
36184 do{
36185 err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
36186 }while( err==EINTR );
36187 if( err && err!=EINVAL ) return SQLITE_IOERR_WRITE;
36188#else
36189 /* If the OS does not have posix_fallocate(), fake it. Write a
36190 ** single byte to the last byte in each block that falls entirely
36191 ** within the extended region. Then, if required, a single byte
36192 ** at offset (nSize-1), to set the size of the file correctly.
36193 ** This is a similar technique to that used by glibc on systems
36194 ** that do not have a real fallocate() call.
36195 */
36196 int nBlk = buf.st_blksize; /* File-system block size */
36197 int nWrite = 0; /* Number of bytes written by seekAndWrite */
36198 i64 iWrite; /* Next offset to write to */
36199
36200 iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1;
36201 assert( iWrite>=buf.st_size );
36202 assert( ((iWrite+1)%nBlk)==0 );
36203 for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){
36204 if( iWrite>=nSize ) iWrite = nSize - 1;
36205 nWrite = seekAndWrite(pFile, iWrite, "", 1);
36206 if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
36207 }
36208#endif
36209 }
36210 }
36211
36212#if SQLITE_MAX_MMAP_SIZE>0
36213 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
36214 int rc;
36215 if( pFile->szChunk<=0 ){
36216 if( robust_ftruncate(pFile->h, nByte) ){
36217 storeLastErrno(pFile, errno);
36218 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
36219 }
36220 }
36221
36222 rc = unixMapfile(pFile, nByte);
36223 return rc;
36224 }
36225#endif
36226
36227 return SQLITE_OK;
36228}
36229
36230/*
36231** If *pArg is initially negative then this is a query. Set *pArg to
36232** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
36233**
36234** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
36235*/
36236static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
36237 if( *pArg<0 ){
36238 *pArg = (pFile->ctrlFlags & mask)!=0;
36239 }else if( (*pArg)==0 ){
36240 pFile->ctrlFlags &= ~mask;
36241 }else{
36242 pFile->ctrlFlags |= mask;
36243 }
36244}
36245
36246/* Forward declaration */
36247static int unixGetTempname(int nBuf, char *zBuf);
36248
36249/*
36250** Information and control of an open file handle.
36251*/
36252static int unixFileControl(sqlite3_file *id, int op, void *pArg){
36253 unixFile *pFile = (unixFile*)id;
36254 switch( op ){
36255#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
36256 case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE: {
36257 int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE);
36258 return rc ? SQLITE_IOERR_BEGIN_ATOMIC : SQLITE_OK;
36259 }
36260 case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE: {
36261 int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE);
36262 return rc ? SQLITE_IOERR_COMMIT_ATOMIC : SQLITE_OK;
36263 }
36264 case SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE: {
36265 int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE);
36266 return rc ? SQLITE_IOERR_ROLLBACK_ATOMIC : SQLITE_OK;
36267 }
36268#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
36269
36270 case SQLITE_FCNTL_LOCKSTATE: {
36271 *(int*)pArg = pFile->eFileLock;
36272 return SQLITE_OK;
36273 }
36274 case SQLITE_FCNTL_LAST_ERRNO: {
36275 *(int*)pArg = pFile->lastErrno;
36276 return SQLITE_OK;
36277 }
36278 case SQLITE_FCNTL_CHUNK_SIZE: {
36279 pFile->szChunk = *(int *)pArg;
36280 return SQLITE_OK;
36281 }
36282 case SQLITE_FCNTL_SIZE_HINT: {
36283 int rc;
36284 SimulateIOErrorBenign(1);
36285 rc = fcntlSizeHint(pFile, *(i64 *)pArg);
36286 SimulateIOErrorBenign(0);
36287 return rc;
36288 }
36289 case SQLITE_FCNTL_PERSIST_WAL: {
36290 unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg);
36291 return SQLITE_OK;
36292 }
36293 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
36294 unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg);
36295 return SQLITE_OK;
36296 }
36297 case SQLITE_FCNTL_VFSNAME: {
36298 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
36299 return SQLITE_OK;
36300 }
36301 case SQLITE_FCNTL_TEMPFILENAME: {
36302 char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname );
36303 if( zTFile ){
36304 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
36305 *(char**)pArg = zTFile;
36306 }
36307 return SQLITE_OK;
36308 }
36309 case SQLITE_FCNTL_HAS_MOVED: {
36310 *(int*)pArg = fileHasMoved(pFile);
36311 return SQLITE_OK;
36312 }
36313#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
36314 case SQLITE_FCNTL_LOCK_TIMEOUT: {
36315 pFile->iBusyTimeout = *(int*)pArg;
36316 return SQLITE_OK;
36317 }
36318#endif
36319#if SQLITE_MAX_MMAP_SIZE>0
36320 case SQLITE_FCNTL_MMAP_SIZE: {
36321 i64 newLimit = *(i64*)pArg;
36322 int rc = SQLITE_OK;
36323 if( newLimit>sqlite3GlobalConfig.mxMmap ){
36324 newLimit = sqlite3GlobalConfig.mxMmap;
36325 }
36326
36327 /* The value of newLimit may be eventually cast to (size_t) and passed
36328 ** to mmap(). Restrict its value to 2GB if (size_t) is not at least a
36329 ** 64-bit type. */
36330 if( newLimit>0 && sizeof(size_t)<8 ){
36331 newLimit = (newLimit & 0x7FFFFFFF);
36332 }
36333
36334 *(i64*)pArg = pFile->mmapSizeMax;
36335 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
36336 pFile->mmapSizeMax = newLimit;
36337 if( pFile->mmapSize>0 ){
36338 unixUnmapfile(pFile);
36339 rc = unixMapfile(pFile, -1);
36340 }
36341 }
36342 return rc;
36343 }
36344#endif
36345#ifdef SQLITE_DEBUG
36346 /* The pager calls this method to signal that it has done
36347 ** a rollback and that the database is therefore unchanged and
36348 ** it hence it is OK for the transaction change counter to be
36349 ** unchanged.
36350 */
36351 case SQLITE_FCNTL_DB_UNCHANGED: {
36352 ((unixFile*)id)->dbUpdate = 0;
36353 return SQLITE_OK;
36354 }
36355#endif
36356#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
36357 case SQLITE_FCNTL_SET_LOCKPROXYFILE:
36358 case SQLITE_FCNTL_GET_LOCKPROXYFILE: {
36359 return proxyFileControl(id,op,pArg);
36360 }
36361#endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
36362 }
36363 return SQLITE_NOTFOUND;
36364}
36365
36366/*
36367** If pFd->sectorSize is non-zero when this function is called, it is a
36368** no-op. Otherwise, the values of pFd->sectorSize and
36369** pFd->deviceCharacteristics are set according to the file-system
36370** characteristics.
36371**
36372** There are two versions of this function. One for QNX and one for all
36373** other systems.
36374*/
36375#ifndef __QNXNTO__
36376static void setDeviceCharacteristics(unixFile *pFd){
36377 assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 );
36378 if( pFd->sectorSize==0 ){
36379#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
36380 int res;
36381 u32 f = 0;
36382
36383 /* Check for support for F2FS atomic batch writes. */
36384 res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES, &f);
36385 if( res==0 && (f & F2FS_FEATURE_ATOMIC_WRITE) ){
36386 pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC;
36387 }
36388#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
36389
36390 /* Set the POWERSAFE_OVERWRITE flag if requested. */
36391 if( pFd->ctrlFlags & UNIXFILE_PSOW ){
36392 pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
36393 }
36394
36395 pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
36396 }
36397}
36398#else
36399#include <sys/dcmd_blk.h>
36400#include <sys/statvfs.h>
36401static void setDeviceCharacteristics(unixFile *pFile){
36402 if( pFile->sectorSize == 0 ){
36403 struct statvfs fsInfo;
36404
36405 /* Set defaults for non-supported filesystems */
36406 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
36407 pFile->deviceCharacteristics = 0;
36408 if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
36409 return;
36410 }
36411
36412 if( !strcmp(fsInfo.f_basetype, "tmp") ) {
36413 pFile->sectorSize = fsInfo.f_bsize;
36414 pFile->deviceCharacteristics =
36415 SQLITE_IOCAP_ATOMIC4K | /* All ram filesystem writes are atomic */
36416 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
36417 ** the write succeeds */
36418 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
36419 ** so it is ordered */
36420 0;
36421 }else if( strstr(fsInfo.f_basetype, "etfs") ){
36422 pFile->sectorSize = fsInfo.f_bsize;
36423 pFile->deviceCharacteristics =
36424 /* etfs cluster size writes are atomic */
36425 (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
36426 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
36427 ** the write succeeds */
36428 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
36429 ** so it is ordered */
36430 0;
36431 }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){
36432 pFile->sectorSize = fsInfo.f_bsize;
36433 pFile->deviceCharacteristics =
36434 SQLITE_IOCAP_ATOMIC | /* All filesystem writes are atomic */
36435 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
36436 ** the write succeeds */
36437 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
36438 ** so it is ordered */
36439 0;
36440 }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){
36441 pFile->sectorSize = fsInfo.f_bsize;
36442 pFile->deviceCharacteristics =
36443 /* full bitset of atomics from max sector size and smaller */
36444 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
36445 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
36446 ** so it is ordered */
36447 0;
36448 }else if( strstr(fsInfo.f_basetype, "dos") ){
36449 pFile->sectorSize = fsInfo.f_bsize;
36450 pFile->deviceCharacteristics =
36451 /* full bitset of atomics from max sector size and smaller */
36452 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
36453 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
36454 ** so it is ordered */
36455 0;
36456 }else{
36457 pFile->deviceCharacteristics =
36458 SQLITE_IOCAP_ATOMIC512 | /* blocks are atomic */
36459 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
36460 ** the write succeeds */
36461 0;
36462 }
36463 }
36464 /* Last chance verification. If the sector size isn't a multiple of 512
36465 ** then it isn't valid.*/
36466 if( pFile->sectorSize % 512 != 0 ){
36467 pFile->deviceCharacteristics = 0;
36468 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
36469 }
36470}
36471#endif
36472
36473/*
36474** Return the sector size in bytes of the underlying block device for
36475** the specified file. This is almost always 512 bytes, but may be
36476** larger for some devices.
36477**
36478** SQLite code assumes this function cannot fail. It also assumes that
36479** if two files are created in the same file-system directory (i.e.
36480** a database and its journal file) that the sector size will be the
36481** same for both.
36482*/
36483static int unixSectorSize(sqlite3_file *id){
36484 unixFile *pFd = (unixFile*)id;
36485 setDeviceCharacteristics(pFd);
36486 return pFd->sectorSize;
36487}
36488
36489/*
36490** Return the device characteristics for the file.
36491**
36492** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
36493** However, that choice is controversial since technically the underlying
36494** file system does not always provide powersafe overwrites. (In other
36495** words, after a power-loss event, parts of the file that were never
36496** written might end up being altered.) However, non-PSOW behavior is very,
36497** very rare. And asserting PSOW makes a large reduction in the amount
36498** of required I/O for journaling, since a lot of padding is eliminated.
36499** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
36500** available to turn it off and URI query parameter available to turn it off.
36501*/
36502static int unixDeviceCharacteristics(sqlite3_file *id){
36503 unixFile *pFd = (unixFile*)id;
36504 setDeviceCharacteristics(pFd);
36505 return pFd->deviceCharacteristics;
36506}
36507
36508#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
36509
36510/*
36511** Return the system page size.
36512**
36513** This function should not be called directly by other code in this file.
36514** Instead, it should be called via macro osGetpagesize().
36515*/
36516static int unixGetpagesize(void){
36517#if OS_VXWORKS
36518 return 1024;
36519#elif defined(_BSD_SOURCE)
36520 return getpagesize();
36521#else
36522 return (int)sysconf(_SC_PAGESIZE);
36523#endif
36524}
36525
36526#endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */
36527
36528#ifndef SQLITE_OMIT_WAL
36529
36530/*
36531** Object used to represent an shared memory buffer.
36532**
36533** When multiple threads all reference the same wal-index, each thread
36534** has its own unixShm object, but they all point to a single instance
36535** of this unixShmNode object. In other words, each wal-index is opened
36536** only once per process.
36537**
36538** Each unixShmNode object is connected to a single unixInodeInfo object.
36539** We could coalesce this object into unixInodeInfo, but that would mean
36540** every open file that does not use shared memory (in other words, most
36541** open files) would have to carry around this extra information. So
36542** the unixInodeInfo object contains a pointer to this unixShmNode object
36543** and the unixShmNode object is created only when needed.
36544**
36545** unixMutexHeld() must be true when creating or destroying
36546** this object or while reading or writing the following fields:
36547**
36548** nRef
36549**
36550** The following fields are read-only after the object is created:
36551**
36552** hShm
36553** zFilename
36554**
36555** Either unixShmNode.pShmMutex must be held or unixShmNode.nRef==0 and
36556** unixMutexHeld() is true when reading or writing any other field
36557** in this structure.
36558*/
36559struct unixShmNode {
36560 unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */
36561 sqlite3_mutex *pShmMutex; /* Mutex to access this object */
36562 char *zFilename; /* Name of the mmapped file */
36563 int hShm; /* Open file descriptor */
36564 int szRegion; /* Size of shared-memory regions */
36565 u16 nRegion; /* Size of array apRegion */
36566 u8 isReadonly; /* True if read-only */
36567 u8 isUnlocked; /* True if no DMS lock held */
36568 char **apRegion; /* Array of mapped shared-memory regions */
36569 int nRef; /* Number of unixShm objects pointing to this */
36570 unixShm *pFirst; /* All unixShm objects pointing to this */
36571#ifdef SQLITE_DEBUG
36572 u8 exclMask; /* Mask of exclusive locks held */
36573 u8 sharedMask; /* Mask of shared locks held */
36574 u8 nextShmId; /* Next available unixShm.id value */
36575#endif
36576};
36577
36578/*
36579** Structure used internally by this VFS to record the state of an
36580** open shared memory connection.
36581**
36582** The following fields are initialized when this object is created and
36583** are read-only thereafter:
36584**
36585** unixShm.pShmNode
36586** unixShm.id
36587**
36588** All other fields are read/write. The unixShm.pShmNode->pShmMutex must
36589** be held while accessing any read/write fields.
36590*/
36591struct unixShm {
36592 unixShmNode *pShmNode; /* The underlying unixShmNode object */
36593 unixShm *pNext; /* Next unixShm with the same unixShmNode */
36594 u8 hasMutex; /* True if holding the unixShmNode->pShmMutex */
36595 u8 id; /* Id of this connection within its unixShmNode */
36596 u16 sharedMask; /* Mask of shared locks held */
36597 u16 exclMask; /* Mask of exclusive locks held */
36598};
36599
36600/*
36601** Constants used for locking
36602*/
36603#define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
36604#define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
36605
36606/*
36607** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
36608**
36609** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
36610** otherwise.
36611*/
36612static int unixShmSystemLock(
36613 unixFile *pFile, /* Open connection to the WAL file */
36614 int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */
36615 int ofst, /* First byte of the locking range */
36616 int n /* Number of bytes to lock */
36617){
36618 unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
36619 struct flock f; /* The posix advisory locking structure */
36620 int rc = SQLITE_OK; /* Result code form fcntl() */
36621
36622 /* Access to the unixShmNode object is serialized by the caller */
36623 pShmNode = pFile->pInode->pShmNode;
36624 assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) );
36625 assert( pShmNode->nRef>0 || unixMutexHeld() );
36626
36627 /* Shared locks never span more than one byte */
36628 assert( n==1 || lockType!=F_RDLCK );
36629
36630 /* Locks are within range */
36631 assert( n>=1 && n<=SQLITE_SHM_NLOCK );
36632
36633 if( pShmNode->hShm>=0 ){
36634 /* Initialize the locking parameters */
36635 f.l_type = lockType;
36636 f.l_whence = SEEK_SET;
36637 f.l_start = ofst;
36638 f.l_len = n;
36639 rc = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile);
36640 rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
36641 }
36642
36643 /* Update the global lock state and do debug tracing */
36644#ifdef SQLITE_DEBUG
36645 { u16 mask;
36646 OSTRACE(("SHM-LOCK "));
36647 mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
36648 if( rc==SQLITE_OK ){
36649 if( lockType==F_UNLCK ){
36650 OSTRACE(("unlock %d ok", ofst));
36651 pShmNode->exclMask &= ~mask;
36652 pShmNode->sharedMask &= ~mask;
36653 }else if( lockType==F_RDLCK ){
36654 OSTRACE(("read-lock %d ok", ofst));
36655 pShmNode->exclMask &= ~mask;
36656 pShmNode->sharedMask |= mask;
36657 }else{
36658 assert( lockType==F_WRLCK );
36659 OSTRACE(("write-lock %d ok", ofst));
36660 pShmNode->exclMask |= mask;
36661 pShmNode->sharedMask &= ~mask;
36662 }
36663 }else{
36664 if( lockType==F_UNLCK ){
36665 OSTRACE(("unlock %d failed", ofst));
36666 }else if( lockType==F_RDLCK ){
36667 OSTRACE(("read-lock failed"));
36668 }else{
36669 assert( lockType==F_WRLCK );
36670 OSTRACE(("write-lock %d failed", ofst));
36671 }
36672 }
36673 OSTRACE((" - afterwards %03x,%03x\n",
36674 pShmNode->sharedMask, pShmNode->exclMask));
36675 }
36676#endif
36677
36678 return rc;
36679}
36680
36681/*
36682** Return the minimum number of 32KB shm regions that should be mapped at
36683** a time, assuming that each mapping must be an integer multiple of the
36684** current system page-size.
36685**
36686** Usually, this is 1. The exception seems to be systems that are configured
36687** to use 64KB pages - in this case each mapping must cover at least two
36688** shm regions.
36689*/
36690static int unixShmRegionPerMap(void){
36691 int shmsz = 32*1024; /* SHM region size */
36692 int pgsz = osGetpagesize(); /* System page size */
36693 assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */
36694 if( pgsz<shmsz ) return 1;
36695 return pgsz/shmsz;
36696}
36697
36698/*
36699** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
36700**
36701** This is not a VFS shared-memory method; it is a utility function called
36702** by VFS shared-memory methods.
36703*/
36704static void unixShmPurge(unixFile *pFd){
36705 unixShmNode *p = pFd->pInode->pShmNode;
36706 assert( unixMutexHeld() );
36707 if( p && ALWAYS(p->nRef==0) ){
36708 int nShmPerMap = unixShmRegionPerMap();
36709 int i;
36710 assert( p->pInode==pFd->pInode );
36711 sqlite3_mutex_free(p->pShmMutex);
36712 for(i=0; i<p->nRegion; i+=nShmPerMap){
36713 if( p->hShm>=0 ){
36714 osMunmap(p->apRegion[i], p->szRegion);
36715 }else{
36716 sqlite3_free(p->apRegion[i]);
36717 }
36718 }
36719 sqlite3_free(p->apRegion);
36720 if( p->hShm>=0 ){
36721 robust_close(pFd, p->hShm, __LINE__);
36722 p->hShm = -1;
36723 }
36724 p->pInode->pShmNode = 0;
36725 sqlite3_free(p);
36726 }
36727}
36728
36729/*
36730** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
36731** take it now. Return SQLITE_OK if successful, or an SQLite error
36732** code otherwise.
36733**
36734** If the DMS cannot be locked because this is a readonly_shm=1
36735** connection and no other process already holds a lock, return
36736** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
36737*/
36738static int unixLockSharedMemory(unixFile *pDbFd, unixShmNode *pShmNode){
36739 struct flock lock;
36740 int rc = SQLITE_OK;
36741
36742 /* Use F_GETLK to determine the locks other processes are holding
36743 ** on the DMS byte. If it indicates that another process is holding
36744 ** a SHARED lock, then this process may also take a SHARED lock
36745 ** and proceed with opening the *-shm file.
36746 **
36747 ** Or, if no other process is holding any lock, then this process
36748 ** is the first to open it. In this case take an EXCLUSIVE lock on the
36749 ** DMS byte and truncate the *-shm file to zero bytes in size. Then
36750 ** downgrade to a SHARED lock on the DMS byte.
36751 **
36752 ** If another process is holding an EXCLUSIVE lock on the DMS byte,
36753 ** return SQLITE_BUSY to the caller (it will try again). An earlier
36754 ** version of this code attempted the SHARED lock at this point. But
36755 ** this introduced a subtle race condition: if the process holding
36756 ** EXCLUSIVE failed just before truncating the *-shm file, then this
36757 ** process might open and use the *-shm file without truncating it.
36758 ** And if the *-shm file has been corrupted by a power failure or
36759 ** system crash, the database itself may also become corrupt. */
36760 lock.l_whence = SEEK_SET;
36761 lock.l_start = UNIX_SHM_DMS;
36762 lock.l_len = 1;
36763 lock.l_type = F_WRLCK;
36764 if( osFcntl(pShmNode->hShm, F_GETLK, &lock)!=0 ) {
36765 rc = SQLITE_IOERR_LOCK;
36766 }else if( lock.l_type==F_UNLCK ){
36767 if( pShmNode->isReadonly ){
36768 pShmNode->isUnlocked = 1;
36769 rc = SQLITE_READONLY_CANTINIT;
36770 }else{
36771 rc = unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1);
36772 /* The first connection to attach must truncate the -shm file. We
36773 ** truncate to 3 bytes (an arbitrary small number, less than the
36774 ** -shm header size) rather than 0 as a system debugging aid, to
36775 ** help detect if a -shm file truncation is legitimate or is the work
36776 ** or a rogue process. */
36777 if( rc==SQLITE_OK && robust_ftruncate(pShmNode->hShm, 3) ){
36778 rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename);
36779 }
36780 }
36781 }else if( lock.l_type==F_WRLCK ){
36782 rc = SQLITE_BUSY;
36783 }
36784
36785 if( rc==SQLITE_OK ){
36786 assert( lock.l_type==F_UNLCK || lock.l_type==F_RDLCK );
36787 rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1);
36788 }
36789 return rc;
36790}
36791
36792/*
36793** Open a shared-memory area associated with open database file pDbFd.
36794** This particular implementation uses mmapped files.
36795**
36796** The file used to implement shared-memory is in the same directory
36797** as the open database file and has the same name as the open database
36798** file with the "-shm" suffix added. For example, if the database file
36799** is "/home/user1/config.db" then the file that is created and mmapped
36800** for shared memory will be called "/home/user1/config.db-shm".
36801**
36802** Another approach to is to use files in /dev/shm or /dev/tmp or an
36803** some other tmpfs mount. But if a file in a different directory
36804** from the database file is used, then differing access permissions
36805** or a chroot() might cause two different processes on the same
36806** database to end up using different files for shared memory -
36807** meaning that their memory would not really be shared - resulting
36808** in database corruption. Nevertheless, this tmpfs file usage
36809** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
36810** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time
36811** option results in an incompatible build of SQLite; builds of SQLite
36812** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the
36813** same database file at the same time, database corruption will likely
36814** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
36815** "unsupported" and may go away in a future SQLite release.
36816**
36817** When opening a new shared-memory file, if no other instances of that
36818** file are currently open, in this process or in other processes, then
36819** the file must be truncated to zero length or have its header cleared.
36820**
36821** If the original database file (pDbFd) is using the "unix-excl" VFS
36822** that means that an exclusive lock is held on the database file and
36823** that no other processes are able to read or write the database. In
36824** that case, we do not really need shared memory. No shared memory
36825** file is created. The shared memory will be simulated with heap memory.
36826*/
36827static int unixOpenSharedMemory(unixFile *pDbFd){
36828 struct unixShm *p = 0; /* The connection to be opened */
36829 struct unixShmNode *pShmNode; /* The underlying mmapped file */
36830 int rc = SQLITE_OK; /* Result code */
36831 unixInodeInfo *pInode; /* The inode of fd */
36832 char *zShm; /* Name of the file used for SHM */
36833 int nShmFilename; /* Size of the SHM filename in bytes */
36834
36835 /* Allocate space for the new unixShm object. */
36836 p = sqlite3_malloc64( sizeof(*p) );
36837 if( p==0 ) return SQLITE_NOMEM_BKPT;
36838 memset(p, 0, sizeof(*p));
36839 assert( pDbFd->pShm==0 );
36840
36841 /* Check to see if a unixShmNode object already exists. Reuse an existing
36842 ** one if present. Create a new one if necessary.
36843 */
36844 assert( unixFileMutexNotheld(pDbFd) );
36845 unixEnterMutex();
36846 pInode = pDbFd->pInode;
36847 pShmNode = pInode->pShmNode;
36848 if( pShmNode==0 ){
36849 struct stat sStat; /* fstat() info for database file */
36850#ifndef SQLITE_SHM_DIRECTORY
36851 const char *zBasePath = pDbFd->zPath;
36852#endif
36853
36854 /* Call fstat() to figure out the permissions on the database file. If
36855 ** a new *-shm file is created, an attempt will be made to create it
36856 ** with the same permissions.
36857 */
36858 if( osFstat(pDbFd->h, &sStat) ){
36859 rc = SQLITE_IOERR_FSTAT;
36860 goto shm_open_err;
36861 }
36862
36863#ifdef SQLITE_SHM_DIRECTORY
36864 nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
36865#else
36866 nShmFilename = 6 + (int)strlen(zBasePath);
36867#endif
36868 pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename );
36869 if( pShmNode==0 ){
36870 rc = SQLITE_NOMEM_BKPT;
36871 goto shm_open_err;
36872 }
36873 memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
36874 zShm = pShmNode->zFilename = (char*)&pShmNode[1];
36875#ifdef SQLITE_SHM_DIRECTORY
36876 sqlite3_snprintf(nShmFilename, zShm,
36877 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
36878 (u32)sStat.st_ino, (u32)sStat.st_dev);
36879#else
36880 sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath);
36881 sqlite3FileSuffix3(pDbFd->zPath, zShm);
36882#endif
36883 pShmNode->hShm = -1;
36884 pDbFd->pInode->pShmNode = pShmNode;
36885 pShmNode->pInode = pDbFd->pInode;
36886 if( sqlite3GlobalConfig.bCoreMutex ){
36887 pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
36888 if( pShmNode->pShmMutex==0 ){
36889 rc = SQLITE_NOMEM_BKPT;
36890 goto shm_open_err;
36891 }
36892 }
36893
36894 if( pInode->bProcessLock==0 ){
36895 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
36896 pShmNode->hShm = robust_open(zShm, O_RDWR|O_CREAT,(sStat.st_mode&0777));
36897 }
36898 if( pShmNode->hShm<0 ){
36899 pShmNode->hShm = robust_open(zShm, O_RDONLY, (sStat.st_mode&0777));
36900 if( pShmNode->hShm<0 ){
36901 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShm);
36902 goto shm_open_err;
36903 }
36904 pShmNode->isReadonly = 1;
36905 }
36906
36907 /* If this process is running as root, make sure that the SHM file
36908 ** is owned by the same user that owns the original database. Otherwise,
36909 ** the original owner will not be able to connect.
36910 */
36911 robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid);
36912
36913 rc = unixLockSharedMemory(pDbFd, pShmNode);
36914 if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err;
36915 }
36916 }
36917
36918 /* Make the new connection a child of the unixShmNode */
36919 p->pShmNode = pShmNode;
36920#ifdef SQLITE_DEBUG
36921 p->id = pShmNode->nextShmId++;
36922#endif
36923 pShmNode->nRef++;
36924 pDbFd->pShm = p;
36925 unixLeaveMutex();
36926
36927 /* The reference count on pShmNode has already been incremented under
36928 ** the cover of the unixEnterMutex() mutex and the pointer from the
36929 ** new (struct unixShm) object to the pShmNode has been set. All that is
36930 ** left to do is to link the new object into the linked list starting
36931 ** at pShmNode->pFirst. This must be done while holding the
36932 ** pShmNode->pShmMutex.
36933 */
36934 sqlite3_mutex_enter(pShmNode->pShmMutex);
36935 p->pNext = pShmNode->pFirst;
36936 pShmNode->pFirst = p;
36937 sqlite3_mutex_leave(pShmNode->pShmMutex);
36938 return rc;
36939
36940 /* Jump here on any error */
36941shm_open_err:
36942 unixShmPurge(pDbFd); /* This call frees pShmNode if required */
36943 sqlite3_free(p);
36944 unixLeaveMutex();
36945 return rc;
36946}
36947
36948/*
36949** This function is called to obtain a pointer to region iRegion of the
36950** shared-memory associated with the database file fd. Shared-memory regions
36951** are numbered starting from zero. Each shared-memory region is szRegion
36952** bytes in size.
36953**
36954** If an error occurs, an error code is returned and *pp is set to NULL.
36955**
36956** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
36957** region has not been allocated (by any client, including one running in a
36958** separate process), then *pp is set to NULL and SQLITE_OK returned. If
36959** bExtend is non-zero and the requested shared-memory region has not yet
36960** been allocated, it is allocated by this function.
36961**
36962** If the shared-memory region has already been allocated or is allocated by
36963** this call as described above, then it is mapped into this processes
36964** address space (if it is not already), *pp is set to point to the mapped
36965** memory and SQLITE_OK returned.
36966*/
36967static int unixShmMap(
36968 sqlite3_file *fd, /* Handle open on database file */
36969 int iRegion, /* Region to retrieve */
36970 int szRegion, /* Size of regions */
36971 int bExtend, /* True to extend file if necessary */
36972 void volatile **pp /* OUT: Mapped memory */
36973){
36974 unixFile *pDbFd = (unixFile*)fd;
36975 unixShm *p;
36976 unixShmNode *pShmNode;
36977 int rc = SQLITE_OK;
36978 int nShmPerMap = unixShmRegionPerMap();
36979 int nReqRegion;
36980
36981 /* If the shared-memory file has not yet been opened, open it now. */
36982 if( pDbFd->pShm==0 ){
36983 rc = unixOpenSharedMemory(pDbFd);
36984 if( rc!=SQLITE_OK ) return rc;
36985 }
36986
36987 p = pDbFd->pShm;
36988 pShmNode = p->pShmNode;
36989 sqlite3_mutex_enter(pShmNode->pShmMutex);
36990 if( pShmNode->isUnlocked ){
36991 rc = unixLockSharedMemory(pDbFd, pShmNode);
36992 if( rc!=SQLITE_OK ) goto shmpage_out;
36993 pShmNode->isUnlocked = 0;
36994 }
36995 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
36996 assert( pShmNode->pInode==pDbFd->pInode );
36997 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
36998 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
36999
37000 /* Minimum number of regions required to be mapped. */
37001 nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap;
37002
37003 if( pShmNode->nRegion<nReqRegion ){
37004 char **apNew; /* New apRegion[] array */
37005 int nByte = nReqRegion*szRegion; /* Minimum required file size */
37006 struct stat sStat; /* Used by fstat() */
37007
37008 pShmNode->szRegion = szRegion;
37009
37010 if( pShmNode->hShm>=0 ){
37011 /* The requested region is not mapped into this processes address space.
37012 ** Check to see if it has been allocated (i.e. if the wal-index file is
37013 ** large enough to contain the requested region).
37014 */
37015 if( osFstat(pShmNode->hShm, &sStat) ){
37016 rc = SQLITE_IOERR_SHMSIZE;
37017 goto shmpage_out;
37018 }
37019
37020 if( sStat.st_size<nByte ){
37021 /* The requested memory region does not exist. If bExtend is set to
37022 ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
37023 */
37024 if( !bExtend ){
37025 goto shmpage_out;
37026 }
37027
37028 /* Alternatively, if bExtend is true, extend the file. Do this by
37029 ** writing a single byte to the end of each (OS) page being
37030 ** allocated or extended. Technically, we need only write to the
37031 ** last page in order to extend the file. But writing to all new
37032 ** pages forces the OS to allocate them immediately, which reduces
37033 ** the chances of SIGBUS while accessing the mapped region later on.
37034 */
37035 else{
37036 static const int pgsz = 4096;
37037 int iPg;
37038
37039 /* Write to the last byte of each newly allocated or extended page */
37040 assert( (nByte % pgsz)==0 );
37041 for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){
37042 int x = 0;
37043 if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){
37044 const char *zFile = pShmNode->zFilename;
37045 rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile);
37046 goto shmpage_out;
37047 }
37048 }
37049 }
37050 }
37051 }
37052
37053 /* Map the requested memory region into this processes address space. */
37054 apNew = (char **)sqlite3_realloc(
37055 pShmNode->apRegion, nReqRegion*sizeof(char *)
37056 );
37057 if( !apNew ){
37058 rc = SQLITE_IOERR_NOMEM_BKPT;
37059 goto shmpage_out;
37060 }
37061 pShmNode->apRegion = apNew;
37062 while( pShmNode->nRegion<nReqRegion ){
37063 int nMap = szRegion*nShmPerMap;
37064 int i;
37065 void *pMem;
37066 if( pShmNode->hShm>=0 ){
37067 pMem = osMmap(0, nMap,
37068 pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
37069 MAP_SHARED, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion
37070 );
37071 if( pMem==MAP_FAILED ){
37072 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
37073 goto shmpage_out;
37074 }
37075 }else{
37076 pMem = sqlite3_malloc64(nMap);
37077 if( pMem==0 ){
37078 rc = SQLITE_NOMEM_BKPT;
37079 goto shmpage_out;
37080 }
37081 memset(pMem, 0, nMap);
37082 }
37083
37084 for(i=0; i<nShmPerMap; i++){
37085 pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
37086 }
37087 pShmNode->nRegion += nShmPerMap;
37088 }
37089 }
37090
37091shmpage_out:
37092 if( pShmNode->nRegion>iRegion ){
37093 *pp = pShmNode->apRegion[iRegion];
37094 }else{
37095 *pp = 0;
37096 }
37097 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
37098 sqlite3_mutex_leave(pShmNode->pShmMutex);
37099 return rc;
37100}
37101
37102/*
37103** Change the lock state for a shared-memory segment.
37104**
37105** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
37106** different here than in posix. In xShmLock(), one can go from unlocked
37107** to shared and back or from unlocked to exclusive and back. But one may
37108** not go from shared to exclusive or from exclusive to shared.
37109*/
37110static int unixShmLock(
37111 sqlite3_file *fd, /* Database file holding the shared memory */
37112 int ofst, /* First lock to acquire or release */
37113 int n, /* Number of locks to acquire or release */
37114 int flags /* What to do with the lock */
37115){
37116 unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */
37117 unixShm *p = pDbFd->pShm; /* The shared memory being locked */
37118 unixShm *pX; /* For looping over all siblings */
37119 unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */
37120 int rc = SQLITE_OK; /* Result code */
37121 u16 mask; /* Mask of locks to take or release */
37122
37123 assert( pShmNode==pDbFd->pInode->pShmNode );
37124 assert( pShmNode->pInode==pDbFd->pInode );
37125 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
37126 assert( n>=1 );
37127 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
37128 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
37129 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
37130 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
37131 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
37132 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
37133 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
37134
37135 mask = (1<<(ofst+n)) - (1<<ofst);
37136 assert( n>1 || mask==(1<<ofst) );
37137 sqlite3_mutex_enter(pShmNode->pShmMutex);
37138 if( flags & SQLITE_SHM_UNLOCK ){
37139 u16 allMask = 0; /* Mask of locks held by siblings */
37140
37141 /* See if any siblings hold this same lock */
37142 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
37143 if( pX==p ) continue;
37144 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
37145 allMask |= pX->sharedMask;
37146 }
37147
37148 /* Unlock the system-level locks */
37149 if( (mask & allMask)==0 ){
37150 rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n);
37151 }else{
37152 rc = SQLITE_OK;
37153 }
37154
37155 /* Undo the local locks */
37156 if( rc==SQLITE_OK ){
37157 p->exclMask &= ~mask;
37158 p->sharedMask &= ~mask;
37159 }
37160 }else if( flags & SQLITE_SHM_SHARED ){
37161 u16 allShared = 0; /* Union of locks held by connections other than "p" */
37162
37163 /* Find out which shared locks are already held by sibling connections.
37164 ** If any sibling already holds an exclusive lock, go ahead and return
37165 ** SQLITE_BUSY.
37166 */
37167 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
37168 if( (pX->exclMask & mask)!=0 ){
37169 rc = SQLITE_BUSY;
37170 break;
37171 }
37172 allShared |= pX->sharedMask;
37173 }
37174
37175 /* Get shared locks at the system level, if necessary */
37176 if( rc==SQLITE_OK ){
37177 if( (allShared & mask)==0 ){
37178 rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n);
37179 }else{
37180 rc = SQLITE_OK;
37181 }
37182 }
37183
37184 /* Get the local shared locks */
37185 if( rc==SQLITE_OK ){
37186 p->sharedMask |= mask;
37187 }
37188 }else{
37189 /* Make sure no sibling connections hold locks that will block this
37190 ** lock. If any do, return SQLITE_BUSY right away.
37191 */
37192 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
37193 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
37194 rc = SQLITE_BUSY;
37195 break;
37196 }
37197 }
37198
37199 /* Get the exclusive locks at the system level. Then if successful
37200 ** also mark the local connection as being locked.
37201 */
37202 if( rc==SQLITE_OK ){
37203 rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n);
37204 if( rc==SQLITE_OK ){
37205 assert( (p->sharedMask & mask)==0 );
37206 p->exclMask |= mask;
37207 }
37208 }
37209 }
37210 sqlite3_mutex_leave(pShmNode->pShmMutex);
37211 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
37212 p->id, osGetpid(0), p->sharedMask, p->exclMask));
37213 return rc;
37214}
37215
37216/*
37217** Implement a memory barrier or memory fence on shared memory.
37218**
37219** All loads and stores begun before the barrier must complete before
37220** any load or store begun after the barrier.
37221*/
37222static void unixShmBarrier(
37223 sqlite3_file *fd /* Database file holding the shared memory */
37224){
37225 UNUSED_PARAMETER(fd);
37226 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
37227 assert( fd->pMethods->xLock==nolockLock
37228 || unixFileMutexNotheld((unixFile*)fd)
37229 );
37230 unixEnterMutex(); /* Also mutex, for redundancy */
37231 unixLeaveMutex();
37232}
37233
37234/*
37235** Close a connection to shared-memory. Delete the underlying
37236** storage if deleteFlag is true.
37237**
37238** If there is no shared memory associated with the connection then this
37239** routine is a harmless no-op.
37240*/
37241static int unixShmUnmap(
37242 sqlite3_file *fd, /* The underlying database file */
37243 int deleteFlag /* Delete shared-memory if true */
37244){
37245 unixShm *p; /* The connection to be closed */
37246 unixShmNode *pShmNode; /* The underlying shared-memory file */
37247 unixShm **pp; /* For looping over sibling connections */
37248 unixFile *pDbFd; /* The underlying database file */
37249
37250 pDbFd = (unixFile*)fd;
37251 p = pDbFd->pShm;
37252 if( p==0 ) return SQLITE_OK;
37253 pShmNode = p->pShmNode;
37254
37255 assert( pShmNode==pDbFd->pInode->pShmNode );
37256 assert( pShmNode->pInode==pDbFd->pInode );
37257
37258 /* Remove connection p from the set of connections associated
37259 ** with pShmNode */
37260 sqlite3_mutex_enter(pShmNode->pShmMutex);
37261 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
37262 *pp = p->pNext;
37263
37264 /* Free the connection p */
37265 sqlite3_free(p);
37266 pDbFd->pShm = 0;
37267 sqlite3_mutex_leave(pShmNode->pShmMutex);
37268
37269 /* If pShmNode->nRef has reached 0, then close the underlying
37270 ** shared-memory file, too */
37271 assert( unixFileMutexNotheld(pDbFd) );
37272 unixEnterMutex();
37273 assert( pShmNode->nRef>0 );
37274 pShmNode->nRef--;
37275 if( pShmNode->nRef==0 ){
37276 if( deleteFlag && pShmNode->hShm>=0 ){
37277 osUnlink(pShmNode->zFilename);
37278 }
37279 unixShmPurge(pDbFd);
37280 }
37281 unixLeaveMutex();
37282
37283 return SQLITE_OK;
37284}
37285
37286
37287#else
37288# define unixShmMap 0
37289# define unixShmLock 0
37290# define unixShmBarrier 0
37291# define unixShmUnmap 0
37292#endif /* #ifndef SQLITE_OMIT_WAL */
37293
37294#if SQLITE_MAX_MMAP_SIZE>0
37295/*
37296** If it is currently memory mapped, unmap file pFd.
37297*/
37298static void unixUnmapfile(unixFile *pFd){
37299 assert( pFd->nFetchOut==0 );
37300 if( pFd->pMapRegion ){
37301 osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
37302 pFd->pMapRegion = 0;
37303 pFd->mmapSize = 0;
37304 pFd->mmapSizeActual = 0;
37305 }
37306}
37307
37308/*
37309** Attempt to set the size of the memory mapping maintained by file
37310** descriptor pFd to nNew bytes. Any existing mapping is discarded.
37311**
37312** If successful, this function sets the following variables:
37313**
37314** unixFile.pMapRegion
37315** unixFile.mmapSize
37316** unixFile.mmapSizeActual
37317**
37318** If unsuccessful, an error message is logged via sqlite3_log() and
37319** the three variables above are zeroed. In this case SQLite should
37320** continue accessing the database using the xRead() and xWrite()
37321** methods.
37322*/
37323static void unixRemapfile(
37324 unixFile *pFd, /* File descriptor object */
37325 i64 nNew /* Required mapping size */
37326){
37327 const char *zErr = "mmap";
37328 int h = pFd->h; /* File descriptor open on db file */
37329 u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */
37330 i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */
37331 u8 *pNew = 0; /* Location of new mapping */
37332 int flags = PROT_READ; /* Flags to pass to mmap() */
37333
37334 assert( pFd->nFetchOut==0 );
37335 assert( nNew>pFd->mmapSize );
37336 assert( nNew<=pFd->mmapSizeMax );
37337 assert( nNew>0 );
37338 assert( pFd->mmapSizeActual>=pFd->mmapSize );
37339 assert( MAP_FAILED!=0 );
37340
37341#ifdef SQLITE_MMAP_READWRITE
37342 if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
37343#endif
37344
37345 if( pOrig ){
37346#if HAVE_MREMAP
37347 i64 nReuse = pFd->mmapSize;
37348#else
37349 const int szSyspage = osGetpagesize();
37350 i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
37351#endif
37352 u8 *pReq = &pOrig[nReuse];
37353
37354 /* Unmap any pages of the existing mapping that cannot be reused. */
37355 if( nReuse!=nOrig ){
37356 osMunmap(pReq, nOrig-nReuse);
37357 }
37358
37359#if HAVE_MREMAP
37360 pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE);
37361 zErr = "mremap";
37362#else
37363 pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
37364 if( pNew!=MAP_FAILED ){
37365 if( pNew!=pReq ){
37366 osMunmap(pNew, nNew - nReuse);
37367 pNew = 0;
37368 }else{
37369 pNew = pOrig;
37370 }
37371 }
37372#endif
37373
37374 /* The attempt to extend the existing mapping failed. Free it. */
37375 if( pNew==MAP_FAILED || pNew==0 ){
37376 osMunmap(pOrig, nReuse);
37377 }
37378 }
37379
37380 /* If pNew is still NULL, try to create an entirely new mapping. */
37381 if( pNew==0 ){
37382 pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0);
37383 }
37384
37385 if( pNew==MAP_FAILED ){
37386 pNew = 0;
37387 nNew = 0;
37388 unixLogError(SQLITE_OK, zErr, pFd->zPath);
37389
37390 /* If the mmap() above failed, assume that all subsequent mmap() calls
37391 ** will probably fail too. Fall back to using xRead/xWrite exclusively
37392 ** in this case. */
37393 pFd->mmapSizeMax = 0;
37394 }
37395 pFd->pMapRegion = (void *)pNew;
37396 pFd->mmapSize = pFd->mmapSizeActual = nNew;
37397}
37398
37399/*
37400** Memory map or remap the file opened by file-descriptor pFd (if the file
37401** is already mapped, the existing mapping is replaced by the new). Or, if
37402** there already exists a mapping for this file, and there are still
37403** outstanding xFetch() references to it, this function is a no-op.
37404**
37405** If parameter nByte is non-negative, then it is the requested size of
37406** the mapping to create. Otherwise, if nByte is less than zero, then the
37407** requested size is the size of the file on disk. The actual size of the
37408** created mapping is either the requested size or the value configured
37409** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller.
37410**
37411** SQLITE_OK is returned if no error occurs (even if the mapping is not
37412** recreated as a result of outstanding references) or an SQLite error
37413** code otherwise.
37414*/
37415static int unixMapfile(unixFile *pFd, i64 nMap){
37416 assert( nMap>=0 || pFd->nFetchOut==0 );
37417 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
37418 if( pFd->nFetchOut>0 ) return SQLITE_OK;
37419
37420 if( nMap<0 ){
37421 struct stat statbuf; /* Low-level file information */
37422 if( osFstat(pFd->h, &statbuf) ){
37423 return SQLITE_IOERR_FSTAT;
37424 }
37425 nMap = statbuf.st_size;
37426 }
37427 if( nMap>pFd->mmapSizeMax ){
37428 nMap = pFd->mmapSizeMax;
37429 }
37430
37431 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
37432 if( nMap!=pFd->mmapSize ){
37433 unixRemapfile(pFd, nMap);
37434 }
37435
37436 return SQLITE_OK;
37437}
37438#endif /* SQLITE_MAX_MMAP_SIZE>0 */
37439
37440/*
37441** If possible, return a pointer to a mapping of file fd starting at offset
37442** iOff. The mapping must be valid for at least nAmt bytes.
37443**
37444** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
37445** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
37446** Finally, if an error does occur, return an SQLite error code. The final
37447** value of *pp is undefined in this case.
37448**
37449** If this function does return a pointer, the caller must eventually
37450** release the reference by calling unixUnfetch().
37451*/
37452static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
37453#if SQLITE_MAX_MMAP_SIZE>0
37454 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
37455#endif
37456 *pp = 0;
37457
37458#if SQLITE_MAX_MMAP_SIZE>0
37459 if( pFd->mmapSizeMax>0 ){
37460 if( pFd->pMapRegion==0 ){
37461 int rc = unixMapfile(pFd, -1);
37462 if( rc!=SQLITE_OK ) return rc;
37463 }
37464 if( pFd->mmapSize >= iOff+nAmt ){
37465 *pp = &((u8 *)pFd->pMapRegion)[iOff];
37466 pFd->nFetchOut++;
37467 }
37468 }
37469#endif
37470 return SQLITE_OK;
37471}
37472
37473/*
37474** If the third argument is non-NULL, then this function releases a
37475** reference obtained by an earlier call to unixFetch(). The second
37476** argument passed to this function must be the same as the corresponding
37477** argument that was passed to the unixFetch() invocation.
37478**
37479** Or, if the third argument is NULL, then this function is being called
37480** to inform the VFS layer that, according to POSIX, any existing mapping
37481** may now be invalid and should be unmapped.
37482*/
37483static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
37484#if SQLITE_MAX_MMAP_SIZE>0
37485 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
37486 UNUSED_PARAMETER(iOff);
37487
37488 /* If p==0 (unmap the entire file) then there must be no outstanding
37489 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
37490 ** then there must be at least one outstanding. */
37491 assert( (p==0)==(pFd->nFetchOut==0) );
37492
37493 /* If p!=0, it must match the iOff value. */
37494 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
37495
37496 if( p ){
37497 pFd->nFetchOut--;
37498 }else{
37499 unixUnmapfile(pFd);
37500 }
37501
37502 assert( pFd->nFetchOut>=0 );
37503#else
37504 UNUSED_PARAMETER(fd);
37505 UNUSED_PARAMETER(p);
37506 UNUSED_PARAMETER(iOff);
37507#endif
37508 return SQLITE_OK;
37509}
37510
37511/*
37512** Here ends the implementation of all sqlite3_file methods.
37513**
37514********************** End sqlite3_file Methods *******************************
37515******************************************************************************/
37516
37517/*
37518** This division contains definitions of sqlite3_io_methods objects that
37519** implement various file locking strategies. It also contains definitions
37520** of "finder" functions. A finder-function is used to locate the appropriate
37521** sqlite3_io_methods object for a particular database file. The pAppData
37522** field of the sqlite3_vfs VFS objects are initialized to be pointers to
37523** the correct finder-function for that VFS.
37524**
37525** Most finder functions return a pointer to a fixed sqlite3_io_methods
37526** object. The only interesting finder-function is autolockIoFinder, which
37527** looks at the filesystem type and tries to guess the best locking
37528** strategy from that.
37529**
37530** For finder-function F, two objects are created:
37531**
37532** (1) The real finder-function named "FImpt()".
37533**
37534** (2) A constant pointer to this function named just "F".
37535**
37536**
37537** A pointer to the F pointer is used as the pAppData value for VFS
37538** objects. We have to do this instead of letting pAppData point
37539** directly at the finder-function since C90 rules prevent a void*
37540** from be cast into a function pointer.
37541**
37542**
37543** Each instance of this macro generates two objects:
37544**
37545** * A constant sqlite3_io_methods object call METHOD that has locking
37546** methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
37547**
37548** * An I/O method finder function called FINDER that returns a pointer
37549** to the METHOD object in the previous bullet.
37550*/
37551#define IOMETHODS(FINDER,METHOD,VERSION,CLOSE,LOCK,UNLOCK,CKLOCK,SHMMAP) \
37552static const sqlite3_io_methods METHOD = { \
37553 VERSION, /* iVersion */ \
37554 CLOSE, /* xClose */ \
37555 unixRead, /* xRead */ \
37556 unixWrite, /* xWrite */ \
37557 unixTruncate, /* xTruncate */ \
37558 unixSync, /* xSync */ \
37559 unixFileSize, /* xFileSize */ \
37560 LOCK, /* xLock */ \
37561 UNLOCK, /* xUnlock */ \
37562 CKLOCK, /* xCheckReservedLock */ \
37563 unixFileControl, /* xFileControl */ \
37564 unixSectorSize, /* xSectorSize */ \
37565 unixDeviceCharacteristics, /* xDeviceCapabilities */ \
37566 SHMMAP, /* xShmMap */ \
37567 unixShmLock, /* xShmLock */ \
37568 unixShmBarrier, /* xShmBarrier */ \
37569 unixShmUnmap, /* xShmUnmap */ \
37570 unixFetch, /* xFetch */ \
37571 unixUnfetch, /* xUnfetch */ \
37572}; \
37573static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \
37574 UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \
37575 return &METHOD; \
37576} \
37577static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \
37578 = FINDER##Impl;
37579
37580/*
37581** Here are all of the sqlite3_io_methods objects for each of the
37582** locking strategies. Functions that return pointers to these methods
37583** are also created.
37584*/
37585IOMETHODS(
37586 posixIoFinder, /* Finder function name */
37587 posixIoMethods, /* sqlite3_io_methods object name */
37588 3, /* shared memory and mmap are enabled */
37589 unixClose, /* xClose method */
37590 unixLock, /* xLock method */
37591 unixUnlock, /* xUnlock method */
37592 unixCheckReservedLock, /* xCheckReservedLock method */
37593 unixShmMap /* xShmMap method */
37594)
37595IOMETHODS(
37596 nolockIoFinder, /* Finder function name */
37597 nolockIoMethods, /* sqlite3_io_methods object name */
37598 3, /* shared memory and mmap are enabled */
37599 nolockClose, /* xClose method */
37600 nolockLock, /* xLock method */
37601 nolockUnlock, /* xUnlock method */
37602 nolockCheckReservedLock, /* xCheckReservedLock method */
37603 0 /* xShmMap method */
37604)
37605IOMETHODS(
37606 dotlockIoFinder, /* Finder function name */
37607 dotlockIoMethods, /* sqlite3_io_methods object name */
37608 1, /* shared memory is disabled */
37609 dotlockClose, /* xClose method */
37610 dotlockLock, /* xLock method */
37611 dotlockUnlock, /* xUnlock method */
37612 dotlockCheckReservedLock, /* xCheckReservedLock method */
37613 0 /* xShmMap method */
37614)
37615
37616#if SQLITE_ENABLE_LOCKING_STYLE
37617IOMETHODS(
37618 flockIoFinder, /* Finder function name */
37619 flockIoMethods, /* sqlite3_io_methods object name */
37620 1, /* shared memory is disabled */
37621 flockClose, /* xClose method */
37622 flockLock, /* xLock method */
37623 flockUnlock, /* xUnlock method */
37624 flockCheckReservedLock, /* xCheckReservedLock method */
37625 0 /* xShmMap method */
37626)
37627#endif
37628
37629#if OS_VXWORKS
37630IOMETHODS(
37631 semIoFinder, /* Finder function name */
37632 semIoMethods, /* sqlite3_io_methods object name */
37633 1, /* shared memory is disabled */
37634 semXClose, /* xClose method */
37635 semXLock, /* xLock method */
37636 semXUnlock, /* xUnlock method */
37637 semXCheckReservedLock, /* xCheckReservedLock method */
37638 0 /* xShmMap method */
37639)
37640#endif
37641
37642#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
37643IOMETHODS(
37644 afpIoFinder, /* Finder function name */
37645 afpIoMethods, /* sqlite3_io_methods object name */
37646 1, /* shared memory is disabled */
37647 afpClose, /* xClose method */
37648 afpLock, /* xLock method */
37649 afpUnlock, /* xUnlock method */
37650 afpCheckReservedLock, /* xCheckReservedLock method */
37651 0 /* xShmMap method */
37652)
37653#endif
37654
37655/*
37656** The proxy locking method is a "super-method" in the sense that it
37657** opens secondary file descriptors for the conch and lock files and
37658** it uses proxy, dot-file, AFP, and flock() locking methods on those
37659** secondary files. For this reason, the division that implements
37660** proxy locking is located much further down in the file. But we need
37661** to go ahead and define the sqlite3_io_methods and finder function
37662** for proxy locking here. So we forward declare the I/O methods.
37663*/
37664#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
37665static int proxyClose(sqlite3_file*);
37666static int proxyLock(sqlite3_file*, int);
37667static int proxyUnlock(sqlite3_file*, int);
37668static int proxyCheckReservedLock(sqlite3_file*, int*);
37669IOMETHODS(
37670 proxyIoFinder, /* Finder function name */
37671 proxyIoMethods, /* sqlite3_io_methods object name */
37672 1, /* shared memory is disabled */
37673 proxyClose, /* xClose method */
37674 proxyLock, /* xLock method */
37675 proxyUnlock, /* xUnlock method */
37676 proxyCheckReservedLock, /* xCheckReservedLock method */
37677 0 /* xShmMap method */
37678)
37679#endif
37680
37681/* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
37682#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
37683IOMETHODS(
37684 nfsIoFinder, /* Finder function name */
37685 nfsIoMethods, /* sqlite3_io_methods object name */
37686 1, /* shared memory is disabled */
37687 unixClose, /* xClose method */
37688 unixLock, /* xLock method */
37689 nfsUnlock, /* xUnlock method */
37690 unixCheckReservedLock, /* xCheckReservedLock method */
37691 0 /* xShmMap method */
37692)
37693#endif
37694
37695#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
37696/*
37697** This "finder" function attempts to determine the best locking strategy
37698** for the database file "filePath". It then returns the sqlite3_io_methods
37699** object that implements that strategy.
37700**
37701** This is for MacOSX only.
37702*/
37703static const sqlite3_io_methods *autolockIoFinderImpl(
37704 const char *filePath, /* name of the database file */
37705 unixFile *pNew /* open file object for the database file */
37706){
37707 static const struct Mapping {
37708 const char *zFilesystem; /* Filesystem type name */
37709 const sqlite3_io_methods *pMethods; /* Appropriate locking method */
37710 } aMap[] = {
37711 { "hfs", &posixIoMethods },
37712 { "ufs", &posixIoMethods },
37713 { "afpfs", &afpIoMethods },
37714 { "smbfs", &afpIoMethods },
37715 { "webdav", &nolockIoMethods },
37716 { 0, 0 }
37717 };
37718 int i;
37719 struct statfs fsInfo;
37720 struct flock lockInfo;
37721
37722 if( !filePath ){
37723 /* If filePath==NULL that means we are dealing with a transient file
37724 ** that does not need to be locked. */
37725 return &nolockIoMethods;
37726 }
37727 if( statfs(filePath, &fsInfo) != -1 ){
37728 if( fsInfo.f_flags & MNT_RDONLY ){
37729 return &nolockIoMethods;
37730 }
37731 for(i=0; aMap[i].zFilesystem; i++){
37732 if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
37733 return aMap[i].pMethods;
37734 }
37735 }
37736 }
37737
37738 /* Default case. Handles, amongst others, "nfs".
37739 ** Test byte-range lock using fcntl(). If the call succeeds,
37740 ** assume that the file-system supports POSIX style locks.
37741 */
37742 lockInfo.l_len = 1;
37743 lockInfo.l_start = 0;
37744 lockInfo.l_whence = SEEK_SET;
37745 lockInfo.l_type = F_RDLCK;
37746 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
37747 if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
37748 return &nfsIoMethods;
37749 } else {
37750 return &posixIoMethods;
37751 }
37752 }else{
37753 return &dotlockIoMethods;
37754 }
37755}
37756static const sqlite3_io_methods
37757 *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
37758
37759#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
37760
37761#if OS_VXWORKS
37762/*
37763** This "finder" function for VxWorks checks to see if posix advisory
37764** locking works. If it does, then that is what is used. If it does not
37765** work, then fallback to named semaphore locking.
37766*/
37767static const sqlite3_io_methods *vxworksIoFinderImpl(
37768 const char *filePath, /* name of the database file */
37769 unixFile *pNew /* the open file object */
37770){
37771 struct flock lockInfo;
37772
37773 if( !filePath ){
37774 /* If filePath==NULL that means we are dealing with a transient file
37775 ** that does not need to be locked. */
37776 return &nolockIoMethods;
37777 }
37778
37779 /* Test if fcntl() is supported and use POSIX style locks.
37780 ** Otherwise fall back to the named semaphore method.
37781 */
37782 lockInfo.l_len = 1;
37783 lockInfo.l_start = 0;
37784 lockInfo.l_whence = SEEK_SET;
37785 lockInfo.l_type = F_RDLCK;
37786 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
37787 return &posixIoMethods;
37788 }else{
37789 return &semIoMethods;
37790 }
37791}
37792static const sqlite3_io_methods
37793 *(*const vxworksIoFinder)(const char*,unixFile*) = vxworksIoFinderImpl;
37794
37795#endif /* OS_VXWORKS */
37796
37797/*
37798** An abstract type for a pointer to an IO method finder function:
37799*/
37800typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
37801
37802
37803/****************************************************************************
37804**************************** sqlite3_vfs methods ****************************
37805**
37806** This division contains the implementation of methods on the
37807** sqlite3_vfs object.
37808*/
37809
37810/*
37811** Initialize the contents of the unixFile structure pointed to by pId.
37812*/
37813static int fillInUnixFile(
37814 sqlite3_vfs *pVfs, /* Pointer to vfs object */
37815 int h, /* Open file descriptor of file being opened */
37816 sqlite3_file *pId, /* Write to the unixFile structure here */
37817 const char *zFilename, /* Name of the file being opened */
37818 int ctrlFlags /* Zero or more UNIXFILE_* values */
37819){
37820 const sqlite3_io_methods *pLockingStyle;
37821 unixFile *pNew = (unixFile *)pId;
37822 int rc = SQLITE_OK;
37823
37824 assert( pNew->pInode==NULL );
37825
37826 /* No locking occurs in temporary files */
37827 assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 );
37828
37829 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
37830 pNew->h = h;
37831 pNew->pVfs = pVfs;
37832 pNew->zPath = zFilename;
37833 pNew->ctrlFlags = (u8)ctrlFlags;
37834#if SQLITE_MAX_MMAP_SIZE>0
37835 pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
37836#endif
37837 if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
37838 "psow", SQLITE_POWERSAFE_OVERWRITE) ){
37839 pNew->ctrlFlags |= UNIXFILE_PSOW;
37840 }
37841 if( strcmp(pVfs->zName,"unix-excl")==0 ){
37842 pNew->ctrlFlags |= UNIXFILE_EXCL;
37843 }
37844
37845#if OS_VXWORKS
37846 pNew->pId = vxworksFindFileId(zFilename);
37847 if( pNew->pId==0 ){
37848 ctrlFlags |= UNIXFILE_NOLOCK;
37849 rc = SQLITE_NOMEM_BKPT;
37850 }
37851#endif
37852
37853 if( ctrlFlags & UNIXFILE_NOLOCK ){
37854 pLockingStyle = &nolockIoMethods;
37855 }else{
37856 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
37857#if SQLITE_ENABLE_LOCKING_STYLE
37858 /* Cache zFilename in the locking context (AFP and dotlock override) for
37859 ** proxyLock activation is possible (remote proxy is based on db name)
37860 ** zFilename remains valid until file is closed, to support */
37861 pNew->lockingContext = (void*)zFilename;
37862#endif
37863 }
37864
37865 if( pLockingStyle == &posixIoMethods
37866#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
37867 || pLockingStyle == &nfsIoMethods
37868#endif
37869 ){
37870 unixEnterMutex();
37871 rc = findInodeInfo(pNew, &pNew->pInode);
37872 if( rc!=SQLITE_OK ){
37873 /* If an error occurred in findInodeInfo(), close the file descriptor
37874 ** immediately, before releasing the mutex. findInodeInfo() may fail
37875 ** in two scenarios:
37876 **
37877 ** (a) A call to fstat() failed.
37878 ** (b) A malloc failed.
37879 **
37880 ** Scenario (b) may only occur if the process is holding no other
37881 ** file descriptors open on the same file. If there were other file
37882 ** descriptors on this file, then no malloc would be required by
37883 ** findInodeInfo(). If this is the case, it is quite safe to close
37884 ** handle h - as it is guaranteed that no posix locks will be released
37885 ** by doing so.
37886 **
37887 ** If scenario (a) caused the error then things are not so safe. The
37888 ** implicit assumption here is that if fstat() fails, things are in
37889 ** such bad shape that dropping a lock or two doesn't matter much.
37890 */
37891 robust_close(pNew, h, __LINE__);
37892 h = -1;
37893 }
37894 unixLeaveMutex();
37895 }
37896
37897#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
37898 else if( pLockingStyle == &afpIoMethods ){
37899 /* AFP locking uses the file path so it needs to be included in
37900 ** the afpLockingContext.
37901 */
37902 afpLockingContext *pCtx;
37903 pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
37904 if( pCtx==0 ){
37905 rc = SQLITE_NOMEM_BKPT;
37906 }else{
37907 /* NB: zFilename exists and remains valid until the file is closed
37908 ** according to requirement F11141. So we do not need to make a
37909 ** copy of the filename. */
37910 pCtx->dbPath = zFilename;
37911 pCtx->reserved = 0;
37912 srandomdev();
37913 unixEnterMutex();
37914 rc = findInodeInfo(pNew, &pNew->pInode);
37915 if( rc!=SQLITE_OK ){
37916 sqlite3_free(pNew->lockingContext);
37917 robust_close(pNew, h, __LINE__);
37918 h = -1;
37919 }
37920 unixLeaveMutex();
37921 }
37922 }
37923#endif
37924
37925 else if( pLockingStyle == &dotlockIoMethods ){
37926 /* Dotfile locking uses the file path so it needs to be included in
37927 ** the dotlockLockingContext
37928 */
37929 char *zLockFile;
37930 int nFilename;
37931 assert( zFilename!=0 );
37932 nFilename = (int)strlen(zFilename) + 6;
37933 zLockFile = (char *)sqlite3_malloc64(nFilename);
37934 if( zLockFile==0 ){
37935 rc = SQLITE_NOMEM_BKPT;
37936 }else{
37937 sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
37938 }
37939 pNew->lockingContext = zLockFile;
37940 }
37941
37942#if OS_VXWORKS
37943 else if( pLockingStyle == &semIoMethods ){
37944 /* Named semaphore locking uses the file path so it needs to be
37945 ** included in the semLockingContext
37946 */
37947 unixEnterMutex();
37948 rc = findInodeInfo(pNew, &pNew->pInode);
37949 if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
37950 char *zSemName = pNew->pInode->aSemName;
37951 int n;
37952 sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem",
37953 pNew->pId->zCanonicalName);
37954 for( n=1; zSemName[n]; n++ )
37955 if( zSemName[n]=='/' ) zSemName[n] = '_';
37956 pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
37957 if( pNew->pInode->pSem == SEM_FAILED ){
37958 rc = SQLITE_NOMEM_BKPT;
37959 pNew->pInode->aSemName[0] = '\0';
37960 }
37961 }
37962 unixLeaveMutex();
37963 }
37964#endif
37965
37966 storeLastErrno(pNew, 0);
37967#if OS_VXWORKS
37968 if( rc!=SQLITE_OK ){
37969 if( h>=0 ) robust_close(pNew, h, __LINE__);
37970 h = -1;
37971 osUnlink(zFilename);
37972 pNew->ctrlFlags |= UNIXFILE_DELETE;
37973 }
37974#endif
37975 if( rc!=SQLITE_OK ){
37976 if( h>=0 ) robust_close(pNew, h, __LINE__);
37977 }else{
37978 pNew->pMethod = pLockingStyle;
37979 OpenCounter(+1);
37980 verifyDbFile(pNew);
37981 }
37982 return rc;
37983}
37984
37985/*
37986** Return the name of a directory in which to put temporary files.
37987** If no suitable temporary file directory can be found, return NULL.
37988*/
37989static const char *unixTempFileDir(void){
37990 static const char *azDirs[] = {
37991 0,
37992 0,
37993 "/var/tmp",
37994 "/usr/tmp",
37995 "/tmp",
37996 "."
37997 };
37998 unsigned int i = 0;
37999 struct stat buf;
38000 const char *zDir = sqlite3_temp_directory;
38001
38002 if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
38003 if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
38004 while(1){
38005 if( zDir!=0
38006 && osStat(zDir, &buf)==0
38007 && S_ISDIR(buf.st_mode)
38008 && osAccess(zDir, 03)==0
38009 ){
38010 return zDir;
38011 }
38012 if( i>=sizeof(azDirs)/sizeof(azDirs[0]) ) break;
38013 zDir = azDirs[i++];
38014 }
38015 return 0;
38016}
38017
38018/*
38019** Create a temporary file name in zBuf. zBuf must be allocated
38020** by the calling process and must be big enough to hold at least
38021** pVfs->mxPathname bytes.
38022*/
38023static int unixGetTempname(int nBuf, char *zBuf){
38024 const char *zDir;
38025 int iLimit = 0;
38026
38027 /* It's odd to simulate an io-error here, but really this is just
38028 ** using the io-error infrastructure to test that SQLite handles this
38029 ** function failing.
38030 */
38031 zBuf[0] = 0;
38032 SimulateIOError( return SQLITE_IOERR );
38033
38034 zDir = unixTempFileDir();
38035 if( zDir==0 ) return SQLITE_IOERR_GETTEMPPATH;
38036 do{
38037 u64 r;
38038 sqlite3_randomness(sizeof(r), &r);
38039 assert( nBuf>2 );
38040 zBuf[nBuf-2] = 0;
38041 sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c",
38042 zDir, r, 0);
38043 if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR;
38044 }while( osAccess(zBuf,0)==0 );
38045 return SQLITE_OK;
38046}
38047
38048#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
38049/*
38050** Routine to transform a unixFile into a proxy-locking unixFile.
38051** Implementation in the proxy-lock division, but used by unixOpen()
38052** if SQLITE_PREFER_PROXY_LOCKING is defined.
38053*/
38054static int proxyTransformUnixFile(unixFile*, const char*);
38055#endif
38056
38057/*
38058** Search for an unused file descriptor that was opened on the database
38059** file (not a journal or master-journal file) identified by pathname
38060** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
38061** argument to this function.
38062**
38063** Such a file descriptor may exist if a database connection was closed
38064** but the associated file descriptor could not be closed because some
38065** other file descriptor open on the same file is holding a file-lock.
38066** Refer to comments in the unixClose() function and the lengthy comment
38067** describing "Posix Advisory Locking" at the start of this file for
38068** further details. Also, ticket #4018.
38069**
38070** If a suitable file descriptor is found, then it is returned. If no
38071** such file descriptor is located, -1 is returned.
38072*/
38073static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
38074 UnixUnusedFd *pUnused = 0;
38075
38076 /* Do not search for an unused file descriptor on vxworks. Not because
38077 ** vxworks would not benefit from the change (it might, we're not sure),
38078 ** but because no way to test it is currently available. It is better
38079 ** not to risk breaking vxworks support for the sake of such an obscure
38080 ** feature. */
38081#if !OS_VXWORKS
38082 struct stat sStat; /* Results of stat() call */
38083
38084 unixEnterMutex();
38085
38086 /* A stat() call may fail for various reasons. If this happens, it is
38087 ** almost certain that an open() call on the same path will also fail.
38088 ** For this reason, if an error occurs in the stat() call here, it is
38089 ** ignored and -1 is returned. The caller will try to open a new file
38090 ** descriptor on the same path, fail, and return an error to SQLite.
38091 **
38092 ** Even if a subsequent open() call does succeed, the consequences of
38093 ** not searching for a reusable file descriptor are not dire. */
38094 if( inodeList!=0 && 0==osStat(zPath, &sStat) ){
38095 unixInodeInfo *pInode;
38096
38097 pInode = inodeList;
38098 while( pInode && (pInode->fileId.dev!=sStat.st_dev
38099 || pInode->fileId.ino!=(u64)sStat.st_ino) ){
38100 pInode = pInode->pNext;
38101 }
38102 if( pInode ){
38103 UnixUnusedFd **pp;
38104 assert( sqlite3_mutex_notheld(pInode->pLockMutex) );
38105 sqlite3_mutex_enter(pInode->pLockMutex);
38106 for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
38107 pUnused = *pp;
38108 if( pUnused ){
38109 *pp = pUnused->pNext;
38110 }
38111 sqlite3_mutex_leave(pInode->pLockMutex);
38112 }
38113 }
38114 unixLeaveMutex();
38115#endif /* if !OS_VXWORKS */
38116 return pUnused;
38117}
38118
38119/*
38120** Find the mode, uid and gid of file zFile.
38121*/
38122static int getFileMode(
38123 const char *zFile, /* File name */
38124 mode_t *pMode, /* OUT: Permissions of zFile */
38125 uid_t *pUid, /* OUT: uid of zFile. */
38126 gid_t *pGid /* OUT: gid of zFile. */
38127){
38128 struct stat sStat; /* Output of stat() on database file */
38129 int rc = SQLITE_OK;
38130 if( 0==osStat(zFile, &sStat) ){
38131 *pMode = sStat.st_mode & 0777;
38132 *pUid = sStat.st_uid;
38133 *pGid = sStat.st_gid;
38134 }else{
38135 rc = SQLITE_IOERR_FSTAT;
38136 }
38137 return rc;
38138}
38139
38140/*
38141** This function is called by unixOpen() to determine the unix permissions
38142** to create new files with. If no error occurs, then SQLITE_OK is returned
38143** and a value suitable for passing as the third argument to open(2) is
38144** written to *pMode. If an IO error occurs, an SQLite error code is
38145** returned and the value of *pMode is not modified.
38146**
38147** In most cases, this routine sets *pMode to 0, which will become
38148** an indication to robust_open() to create the file using
38149** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask.
38150** But if the file being opened is a WAL or regular journal file, then
38151** this function queries the file-system for the permissions on the
38152** corresponding database file and sets *pMode to this value. Whenever
38153** possible, WAL and journal files are created using the same permissions
38154** as the associated database file.
38155**
38156** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the
38157** original filename is unavailable. But 8_3_NAMES is only used for
38158** FAT filesystems and permissions do not matter there, so just use
38159** the default permissions.
38160*/
38161static int findCreateFileMode(
38162 const char *zPath, /* Path of file (possibly) being created */
38163 int flags, /* Flags passed as 4th argument to xOpen() */
38164 mode_t *pMode, /* OUT: Permissions to open file with */
38165 uid_t *pUid, /* OUT: uid to set on the file */
38166 gid_t *pGid /* OUT: gid to set on the file */
38167){
38168 int rc = SQLITE_OK; /* Return Code */
38169 *pMode = 0;
38170 *pUid = 0;
38171 *pGid = 0;
38172 if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
38173 char zDb[MAX_PATHNAME+1]; /* Database file path */
38174 int nDb; /* Number of valid bytes in zDb */
38175
38176 /* zPath is a path to a WAL or journal file. The following block derives
38177 ** the path to the associated database file from zPath. This block handles
38178 ** the following naming conventions:
38179 **
38180 ** "<path to db>-journal"
38181 ** "<path to db>-wal"
38182 ** "<path to db>-journalNN"
38183 ** "<path to db>-walNN"
38184 **
38185 ** where NN is a decimal number. The NN naming schemes are
38186 ** used by the test_multiplex.c module.
38187 */
38188 nDb = sqlite3Strlen30(zPath) - 1;
38189 while( zPath[nDb]!='-' ){
38190 /* In normal operation, the journal file name will always contain
38191 ** a '-' character. However in 8+3 filename mode, or if a corrupt
38192 ** rollback journal specifies a master journal with a goofy name, then
38193 ** the '-' might be missing. */
38194 if( nDb==0 || zPath[nDb]=='.' ) return SQLITE_OK;
38195 nDb--;
38196 }
38197 memcpy(zDb, zPath, nDb);
38198 zDb[nDb] = '\0';
38199
38200 rc = getFileMode(zDb, pMode, pUid, pGid);
38201 }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
38202 *pMode = 0600;
38203 }else if( flags & SQLITE_OPEN_URI ){
38204 /* If this is a main database file and the file was opened using a URI
38205 ** filename, check for the "modeof" parameter. If present, interpret
38206 ** its value as a filename and try to copy the mode, uid and gid from
38207 ** that file. */
38208 const char *z = sqlite3_uri_parameter(zPath, "modeof");
38209 if( z ){
38210 rc = getFileMode(z, pMode, pUid, pGid);
38211 }
38212 }
38213 return rc;
38214}
38215
38216/*
38217** Open the file zPath.
38218**
38219** Previously, the SQLite OS layer used three functions in place of this
38220** one:
38221**
38222** sqlite3OsOpenReadWrite();
38223** sqlite3OsOpenReadOnly();
38224** sqlite3OsOpenExclusive();
38225**
38226** These calls correspond to the following combinations of flags:
38227**
38228** ReadWrite() -> (READWRITE | CREATE)
38229** ReadOnly() -> (READONLY)
38230** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
38231**
38232** The old OpenExclusive() accepted a boolean argument - "delFlag". If
38233** true, the file was configured to be automatically deleted when the
38234** file handle closed. To achieve the same effect using this new
38235** interface, add the DELETEONCLOSE flag to those specified above for
38236** OpenExclusive().
38237*/
38238static int unixOpen(
38239 sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */
38240 const char *zPath, /* Pathname of file to be opened */
38241 sqlite3_file *pFile, /* The file descriptor to be filled in */
38242 int flags, /* Input flags to control the opening */
38243 int *pOutFlags /* Output flags returned to SQLite core */
38244){
38245 unixFile *p = (unixFile *)pFile;
38246 int fd = -1; /* File descriptor returned by open() */
38247 int openFlags = 0; /* Flags to pass to open() */
38248 int eType = flags&0xFFFFFF00; /* Type of file to open */
38249 int noLock; /* True to omit locking primitives */
38250 int rc = SQLITE_OK; /* Function Return Code */
38251 int ctrlFlags = 0; /* UNIXFILE_* flags */
38252
38253 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
38254 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
38255 int isCreate = (flags & SQLITE_OPEN_CREATE);
38256 int isReadonly = (flags & SQLITE_OPEN_READONLY);
38257 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
38258#if SQLITE_ENABLE_LOCKING_STYLE
38259 int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY);
38260#endif
38261#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
38262 struct statfs fsInfo;
38263#endif
38264
38265 /* If creating a master or main-file journal, this function will open
38266 ** a file-descriptor on the directory too. The first time unixSync()
38267 ** is called the directory file descriptor will be fsync()ed and close()d.
38268 */
38269 int isNewJrnl = (isCreate && (
38270 eType==SQLITE_OPEN_MASTER_JOURNAL
38271 || eType==SQLITE_OPEN_MAIN_JOURNAL
38272 || eType==SQLITE_OPEN_WAL
38273 ));
38274
38275 /* If argument zPath is a NULL pointer, this function is required to open
38276 ** a temporary file. Use this buffer to store the file name in.
38277 */
38278 char zTmpname[MAX_PATHNAME+2];
38279 const char *zName = zPath;
38280
38281 /* Check the following statements are true:
38282 **
38283 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
38284 ** (b) if CREATE is set, then READWRITE must also be set, and
38285 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
38286 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
38287 */
38288 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
38289 assert(isCreate==0 || isReadWrite);
38290 assert(isExclusive==0 || isCreate);
38291 assert(isDelete==0 || isCreate);
38292
38293 /* The main DB, main journal, WAL file and master journal are never
38294 ** automatically deleted. Nor are they ever temporary files. */
38295 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
38296 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
38297 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
38298 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
38299
38300 /* Assert that the upper layer has set one of the "file-type" flags. */
38301 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
38302 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
38303 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
38304 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
38305 );
38306
38307 /* Detect a pid change and reset the PRNG. There is a race condition
38308 ** here such that two or more threads all trying to open databases at
38309 ** the same instant might all reset the PRNG. But multiple resets
38310 ** are harmless.
38311 */
38312 if( randomnessPid!=osGetpid(0) ){
38313 randomnessPid = osGetpid(0);
38314 sqlite3_randomness(0,0);
38315 }
38316 memset(p, 0, sizeof(unixFile));
38317
38318 if( eType==SQLITE_OPEN_MAIN_DB ){
38319 UnixUnusedFd *pUnused;
38320 pUnused = findReusableFd(zName, flags);
38321 if( pUnused ){
38322 fd = pUnused->fd;
38323 }else{
38324 pUnused = sqlite3_malloc64(sizeof(*pUnused));
38325 if( !pUnused ){
38326 return SQLITE_NOMEM_BKPT;
38327 }
38328 }
38329 p->pPreallocatedUnused = pUnused;
38330
38331 /* Database filenames are double-zero terminated if they are not
38332 ** URIs with parameters. Hence, they can always be passed into
38333 ** sqlite3_uri_parameter(). */
38334 assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 );
38335
38336 }else if( !zName ){
38337 /* If zName is NULL, the upper layer is requesting a temp file. */
38338 assert(isDelete && !isNewJrnl);
38339 rc = unixGetTempname(pVfs->mxPathname, zTmpname);
38340 if( rc!=SQLITE_OK ){
38341 return rc;
38342 }
38343 zName = zTmpname;
38344
38345 /* Generated temporary filenames are always double-zero terminated
38346 ** for use by sqlite3_uri_parameter(). */
38347 assert( zName[strlen(zName)+1]==0 );
38348 }
38349
38350 /* Determine the value of the flags parameter passed to POSIX function
38351 ** open(). These must be calculated even if open() is not called, as
38352 ** they may be stored as part of the file handle and used by the
38353 ** 'conch file' locking functions later on. */
38354 if( isReadonly ) openFlags |= O_RDONLY;
38355 if( isReadWrite ) openFlags |= O_RDWR;
38356 if( isCreate ) openFlags |= O_CREAT;
38357 if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
38358 openFlags |= (O_LARGEFILE|O_BINARY);
38359
38360 if( fd<0 ){
38361 mode_t openMode; /* Permissions to create file with */
38362 uid_t uid; /* Userid for the file */
38363 gid_t gid; /* Groupid for the file */
38364 rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid);
38365 if( rc!=SQLITE_OK ){
38366 assert( !p->pPreallocatedUnused );
38367 assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
38368 return rc;
38369 }
38370 fd = robust_open(zName, openFlags, openMode);
38371 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
38372 assert( !isExclusive || (openFlags & O_CREAT)!=0 );
38373 if( fd<0 ){
38374 if( isNewJrnl && errno==EACCES && osAccess(zName, F_OK) ){
38375 /* If unable to create a journal because the directory is not
38376 ** writable, change the error code to indicate that. */
38377 rc = SQLITE_READONLY_DIRECTORY;
38378 }else if( errno!=EISDIR && isReadWrite ){
38379 /* Failed to open the file for read/write access. Try read-only. */
38380 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
38381 openFlags &= ~(O_RDWR|O_CREAT);
38382 flags |= SQLITE_OPEN_READONLY;
38383 openFlags |= O_RDONLY;
38384 isReadonly = 1;
38385 fd = robust_open(zName, openFlags, openMode);
38386 }
38387 }
38388 if( fd<0 ){
38389 int rc2 = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
38390 if( rc==SQLITE_OK ) rc = rc2;
38391 goto open_finished;
38392 }
38393
38394 /* If this process is running as root and if creating a new rollback
38395 ** journal or WAL file, set the ownership of the journal or WAL to be
38396 ** the same as the original database.
38397 */
38398 if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
38399 robustFchown(fd, uid, gid);
38400 }
38401 }
38402 assert( fd>=0 );
38403 if( pOutFlags ){
38404 *pOutFlags = flags;
38405 }
38406
38407 if( p->pPreallocatedUnused ){
38408 p->pPreallocatedUnused->fd = fd;
38409 p->pPreallocatedUnused->flags = flags;
38410 }
38411
38412 if( isDelete ){
38413#if OS_VXWORKS
38414 zPath = zName;
38415#elif defined(SQLITE_UNLINK_AFTER_CLOSE)
38416 zPath = sqlite3_mprintf("%s", zName);
38417 if( zPath==0 ){
38418 robust_close(p, fd, __LINE__);
38419 return SQLITE_NOMEM_BKPT;
38420 }
38421#else
38422 osUnlink(zName);
38423#endif
38424 }
38425#if SQLITE_ENABLE_LOCKING_STYLE
38426 else{
38427 p->openFlags = openFlags;
38428 }
38429#endif
38430
38431#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
38432 if( fstatfs(fd, &fsInfo) == -1 ){
38433 storeLastErrno(p, errno);
38434 robust_close(p, fd, __LINE__);
38435 return SQLITE_IOERR_ACCESS;
38436 }
38437 if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
38438 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
38439 }
38440 if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) {
38441 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
38442 }
38443#endif
38444
38445 /* Set up appropriate ctrlFlags */
38446 if( isDelete ) ctrlFlags |= UNIXFILE_DELETE;
38447 if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY;
38448 noLock = eType!=SQLITE_OPEN_MAIN_DB;
38449 if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK;
38450 if( isNewJrnl ) ctrlFlags |= UNIXFILE_DIRSYNC;
38451 if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;
38452
38453#if SQLITE_ENABLE_LOCKING_STYLE
38454#if SQLITE_PREFER_PROXY_LOCKING
38455 isAutoProxy = 1;
38456#endif
38457 if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
38458 char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
38459 int useProxy = 0;
38460
38461 /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
38462 ** never use proxy, NULL means use proxy for non-local files only. */
38463 if( envforce!=NULL ){
38464 useProxy = atoi(envforce)>0;
38465 }else{
38466 useProxy = !(fsInfo.f_flags&MNT_LOCAL);
38467 }
38468 if( useProxy ){
38469 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
38470 if( rc==SQLITE_OK ){
38471 rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
38472 if( rc!=SQLITE_OK ){
38473 /* Use unixClose to clean up the resources added in fillInUnixFile
38474 ** and clear all the structure's references. Specifically,
38475 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
38476 */
38477 unixClose(pFile);
38478 return rc;
38479 }
38480 }
38481 goto open_finished;
38482 }
38483 }
38484#endif
38485
38486 assert( zPath==0 || zPath[0]=='/'
38487 || eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL
38488 );
38489 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
38490
38491open_finished:
38492 if( rc!=SQLITE_OK ){
38493 sqlite3_free(p->pPreallocatedUnused);
38494 }
38495 return rc;
38496}
38497
38498
38499/*
38500** Delete the file at zPath. If the dirSync argument is true, fsync()
38501** the directory after deleting the file.
38502*/
38503static int unixDelete(
38504 sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */
38505 const char *zPath, /* Name of file to be deleted */
38506 int dirSync /* If true, fsync() directory after deleting file */
38507){
38508 int rc = SQLITE_OK;
38509 UNUSED_PARAMETER(NotUsed);
38510 SimulateIOError(return SQLITE_IOERR_DELETE);
38511 if( osUnlink(zPath)==(-1) ){
38512 if( errno==ENOENT
38513#if OS_VXWORKS
38514 || osAccess(zPath,0)!=0
38515#endif
38516 ){
38517 rc = SQLITE_IOERR_DELETE_NOENT;
38518 }else{
38519 rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
38520 }
38521 return rc;
38522 }
38523#ifndef SQLITE_DISABLE_DIRSYNC
38524 if( (dirSync & 1)!=0 ){
38525 int fd;
38526 rc = osOpenDirectory(zPath, &fd);
38527 if( rc==SQLITE_OK ){
38528 if( full_fsync(fd,0,0) ){
38529 rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
38530 }
38531 robust_close(0, fd, __LINE__);
38532 }else{
38533 assert( rc==SQLITE_CANTOPEN );
38534 rc = SQLITE_OK;
38535 }
38536 }
38537#endif
38538 return rc;
38539}
38540
38541/*
38542** Test the existence of or access permissions of file zPath. The
38543** test performed depends on the value of flags:
38544**
38545** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
38546** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
38547** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
38548**
38549** Otherwise return 0.
38550*/
38551static int unixAccess(
38552 sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */
38553 const char *zPath, /* Path of the file to examine */
38554 int flags, /* What do we want to learn about the zPath file? */
38555 int *pResOut /* Write result boolean here */
38556){
38557 UNUSED_PARAMETER(NotUsed);
38558 SimulateIOError( return SQLITE_IOERR_ACCESS; );
38559 assert( pResOut!=0 );
38560
38561 /* The spec says there are three possible values for flags. But only
38562 ** two of them are actually used */
38563 assert( flags==SQLITE_ACCESS_EXISTS || flags==SQLITE_ACCESS_READWRITE );
38564
38565 if( flags==SQLITE_ACCESS_EXISTS ){
38566 struct stat buf;
38567 *pResOut = (0==osStat(zPath, &buf) && buf.st_size>0);
38568 }else{
38569 *pResOut = osAccess(zPath, W_OK|R_OK)==0;
38570 }
38571 return SQLITE_OK;
38572}
38573
38574/*
38575**
38576*/
38577static int mkFullPathname(
38578 const char *zPath, /* Input path */
38579 char *zOut, /* Output buffer */
38580 int nOut /* Allocated size of buffer zOut */
38581){
38582 int nPath = sqlite3Strlen30(zPath);
38583 int iOff = 0;
38584 if( zPath[0]!='/' ){
38585 if( osGetcwd(zOut, nOut-2)==0 ){
38586 return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
38587 }
38588 iOff = sqlite3Strlen30(zOut);
38589 zOut[iOff++] = '/';
38590 }
38591 if( (iOff+nPath+1)>nOut ){
38592 /* SQLite assumes that xFullPathname() nul-terminates the output buffer
38593 ** even if it returns an error. */
38594 zOut[iOff] = '\0';
38595 return SQLITE_CANTOPEN_BKPT;
38596 }
38597 sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath);
38598 return SQLITE_OK;
38599}
38600
38601/*
38602** Turn a relative pathname into a full pathname. The relative path
38603** is stored as a nul-terminated string in the buffer pointed to by
38604** zPath.
38605**
38606** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
38607** (in this case, MAX_PATHNAME bytes). The full-path is written to
38608** this buffer before returning.
38609*/
38610static int unixFullPathname(
38611 sqlite3_vfs *pVfs, /* Pointer to vfs object */
38612 const char *zPath, /* Possibly relative input path */
38613 int nOut, /* Size of output buffer in bytes */
38614 char *zOut /* Output buffer */
38615){
38616#if !defined(HAVE_READLINK) || !defined(HAVE_LSTAT)
38617 return mkFullPathname(zPath, zOut, nOut);
38618#else
38619 int rc = SQLITE_OK;
38620 int nByte;
38621 int nLink = 1; /* Number of symbolic links followed so far */
38622 const char *zIn = zPath; /* Input path for each iteration of loop */
38623 char *zDel = 0;
38624
38625 assert( pVfs->mxPathname==MAX_PATHNAME );
38626 UNUSED_PARAMETER(pVfs);
38627
38628 /* It's odd to simulate an io-error here, but really this is just
38629 ** using the io-error infrastructure to test that SQLite handles this
38630 ** function failing. This function could fail if, for example, the
38631 ** current working directory has been unlinked.
38632 */
38633 SimulateIOError( return SQLITE_ERROR );
38634
38635 do {
38636
38637 /* Call stat() on path zIn. Set bLink to true if the path is a symbolic
38638 ** link, or false otherwise. */
38639 int bLink = 0;
38640 struct stat buf;
38641 if( osLstat(zIn, &buf)!=0 ){
38642 if( errno!=ENOENT ){
38643 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn);
38644 }
38645 }else{
38646 bLink = S_ISLNK(buf.st_mode);
38647 }
38648
38649 if( bLink ){
38650 if( zDel==0 ){
38651 zDel = sqlite3_malloc(nOut);
38652 if( zDel==0 ) rc = SQLITE_NOMEM_BKPT;
38653 }else if( ++nLink>SQLITE_MAX_SYMLINKS ){
38654 rc = SQLITE_CANTOPEN_BKPT;
38655 }
38656
38657 if( rc==SQLITE_OK ){
38658 nByte = osReadlink(zIn, zDel, nOut-1);
38659 if( nByte<0 ){
38660 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn);
38661 }else{
38662 if( zDel[0]!='/' ){
38663 int n;
38664 for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--);
38665 if( nByte+n+1>nOut ){
38666 rc = SQLITE_CANTOPEN_BKPT;
38667 }else{
38668 memmove(&zDel[n], zDel, nByte+1);
38669 memcpy(zDel, zIn, n);
38670 nByte += n;
38671 }
38672 }
38673 zDel[nByte] = '\0';
38674 }
38675 }
38676
38677 zIn = zDel;
38678 }
38679
38680 assert( rc!=SQLITE_OK || zIn!=zOut || zIn[0]=='/' );
38681 if( rc==SQLITE_OK && zIn!=zOut ){
38682 rc = mkFullPathname(zIn, zOut, nOut);
38683 }
38684 if( bLink==0 ) break;
38685 zIn = zOut;
38686 }while( rc==SQLITE_OK );
38687
38688 sqlite3_free(zDel);
38689 return rc;
38690#endif /* HAVE_READLINK && HAVE_LSTAT */
38691}
38692
38693
38694#ifndef SQLITE_OMIT_LOAD_EXTENSION
38695/*
38696** Interfaces for opening a shared library, finding entry points
38697** within the shared library, and closing the shared library.
38698*/
38699#include <dlfcn.h>
38700static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
38701 UNUSED_PARAMETER(NotUsed);
38702 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
38703}
38704
38705/*
38706** SQLite calls this function immediately after a call to unixDlSym() or
38707** unixDlOpen() fails (returns a null pointer). If a more detailed error
38708** message is available, it is written to zBufOut. If no error message
38709** is available, zBufOut is left unmodified and SQLite uses a default
38710** error message.
38711*/
38712static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
38713 const char *zErr;
38714 UNUSED_PARAMETER(NotUsed);
38715 unixEnterMutex();
38716 zErr = dlerror();
38717 if( zErr ){
38718 sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
38719 }
38720 unixLeaveMutex();
38721}
38722static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
38723 /*
38724 ** GCC with -pedantic-errors says that C90 does not allow a void* to be
38725 ** cast into a pointer to a function. And yet the library dlsym() routine
38726 ** returns a void* which is really a pointer to a function. So how do we
38727 ** use dlsym() with -pedantic-errors?
38728 **
38729 ** Variable x below is defined to be a pointer to a function taking
38730 ** parameters void* and const char* and returning a pointer to a function.
38731 ** We initialize x by assigning it a pointer to the dlsym() function.
38732 ** (That assignment requires a cast.) Then we call the function that
38733 ** x points to.
38734 **
38735 ** This work-around is unlikely to work correctly on any system where
38736 ** you really cannot cast a function pointer into void*. But then, on the
38737 ** other hand, dlsym() will not work on such a system either, so we have
38738 ** not really lost anything.
38739 */
38740 void (*(*x)(void*,const char*))(void);
38741 UNUSED_PARAMETER(NotUsed);
38742 x = (void(*(*)(void*,const char*))(void))dlsym;
38743 return (*x)(p, zSym);
38744}
38745static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
38746 UNUSED_PARAMETER(NotUsed);
38747 dlclose(pHandle);
38748}
38749#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
38750 #define unixDlOpen 0
38751 #define unixDlError 0
38752 #define unixDlSym 0
38753 #define unixDlClose 0
38754#endif
38755
38756/*
38757** Write nBuf bytes of random data to the supplied buffer zBuf.
38758*/
38759static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
38760 UNUSED_PARAMETER(NotUsed);
38761 assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
38762
38763 /* We have to initialize zBuf to prevent valgrind from reporting
38764 ** errors. The reports issued by valgrind are incorrect - we would
38765 ** prefer that the randomness be increased by making use of the
38766 ** uninitialized space in zBuf - but valgrind errors tend to worry
38767 ** some users. Rather than argue, it seems easier just to initialize
38768 ** the whole array and silence valgrind, even if that means less randomness
38769 ** in the random seed.
38770 **
38771 ** When testing, initializing zBuf[] to zero is all we do. That means
38772 ** that we always use the same random number sequence. This makes the
38773 ** tests repeatable.
38774 */
38775 memset(zBuf, 0, nBuf);
38776 randomnessPid = osGetpid(0);
38777#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
38778 {
38779 int fd, got;
38780 fd = robust_open("/dev/urandom", O_RDONLY, 0);
38781 if( fd<0 ){
38782 time_t t;
38783 time(&t);
38784 memcpy(zBuf, &t, sizeof(t));
38785 memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid));
38786 assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf );
38787 nBuf = sizeof(t) + sizeof(randomnessPid);
38788 }else{
38789 do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR );
38790 robust_close(0, fd, __LINE__);
38791 }
38792 }
38793#endif
38794 return nBuf;
38795}
38796
38797
38798/*
38799** Sleep for a little while. Return the amount of time slept.
38800** The argument is the number of microseconds we want to sleep.
38801** The return value is the number of microseconds of sleep actually
38802** requested from the underlying operating system, a number which
38803** might be greater than or equal to the argument, but not less
38804** than the argument.
38805*/
38806static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
38807#if OS_VXWORKS
38808 struct timespec sp;
38809
38810 sp.tv_sec = microseconds / 1000000;
38811 sp.tv_nsec = (microseconds % 1000000) * 1000;
38812 nanosleep(&sp, NULL);
38813 UNUSED_PARAMETER(NotUsed);
38814 return microseconds;
38815#elif defined(HAVE_USLEEP) && HAVE_USLEEP
38816 usleep(microseconds);
38817 UNUSED_PARAMETER(NotUsed);
38818 return microseconds;
38819#else
38820 int seconds = (microseconds+999999)/1000000;
38821 sleep(seconds);
38822 UNUSED_PARAMETER(NotUsed);
38823 return seconds*1000000;
38824#endif
38825}
38826
38827/*
38828** The following variable, if set to a non-zero value, is interpreted as
38829** the number of seconds since 1970 and is used to set the result of
38830** sqlite3OsCurrentTime() during testing.
38831*/
38832#ifdef SQLITE_TEST
38833SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
38834#endif
38835
38836/*
38837** Find the current time (in Universal Coordinated Time). Write into *piNow
38838** the current time and date as a Julian Day number times 86_400_000. In
38839** other words, write into *piNow the number of milliseconds since the Julian
38840** epoch of noon in Greenwich on November 24, 4714 B.C according to the
38841** proleptic Gregorian calendar.
38842**
38843** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
38844** cannot be found.
38845*/
38846static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
38847 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
38848 int rc = SQLITE_OK;
38849#if defined(NO_GETTOD)
38850 time_t t;
38851 time(&t);
38852 *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
38853#elif OS_VXWORKS
38854 struct timespec sNow;
38855 clock_gettime(CLOCK_REALTIME, &sNow);
38856 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
38857#else
38858 struct timeval sNow;
38859 (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */
38860 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
38861#endif
38862
38863#ifdef SQLITE_TEST
38864 if( sqlite3_current_time ){
38865 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
38866 }
38867#endif
38868 UNUSED_PARAMETER(NotUsed);
38869 return rc;
38870}
38871
38872#ifndef SQLITE_OMIT_DEPRECATED
38873/*
38874** Find the current time (in Universal Coordinated Time). Write the
38875** current time and date as a Julian Day number into *prNow and
38876** return 0. Return 1 if the time and date cannot be found.
38877*/
38878static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
38879 sqlite3_int64 i = 0;
38880 int rc;
38881 UNUSED_PARAMETER(NotUsed);
38882 rc = unixCurrentTimeInt64(0, &i);
38883 *prNow = i/86400000.0;
38884 return rc;
38885}
38886#else
38887# define unixCurrentTime 0
38888#endif
38889
38890/*
38891** The xGetLastError() method is designed to return a better
38892** low-level error message when operating-system problems come up
38893** during SQLite operation. Only the integer return code is currently
38894** used.
38895*/
38896static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
38897 UNUSED_PARAMETER(NotUsed);
38898 UNUSED_PARAMETER(NotUsed2);
38899 UNUSED_PARAMETER(NotUsed3);
38900 return errno;
38901}
38902
38903
38904/*
38905************************ End of sqlite3_vfs methods ***************************
38906******************************************************************************/
38907
38908/******************************************************************************
38909************************** Begin Proxy Locking ********************************
38910**
38911** Proxy locking is a "uber-locking-method" in this sense: It uses the
38912** other locking methods on secondary lock files. Proxy locking is a
38913** meta-layer over top of the primitive locking implemented above. For
38914** this reason, the division that implements of proxy locking is deferred
38915** until late in the file (here) after all of the other I/O methods have
38916** been defined - so that the primitive locking methods are available
38917** as services to help with the implementation of proxy locking.
38918**
38919****
38920**
38921** The default locking schemes in SQLite use byte-range locks on the
38922** database file to coordinate safe, concurrent access by multiple readers
38923** and writers [http://sqlite.org/lockingv3.html]. The five file locking
38924** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
38925** as POSIX read & write locks over fixed set of locations (via fsctl),
38926** on AFP and SMB only exclusive byte-range locks are available via fsctl
38927** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
38928** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
38929** address in the shared range is taken for a SHARED lock, the entire
38930** shared range is taken for an EXCLUSIVE lock):
38931**
38932** PENDING_BYTE 0x40000000
38933** RESERVED_BYTE 0x40000001
38934** SHARED_RANGE 0x40000002 -> 0x40000200
38935**
38936** This works well on the local file system, but shows a nearly 100x
38937** slowdown in read performance on AFP because the AFP client disables
38938** the read cache when byte-range locks are present. Enabling the read
38939** cache exposes a cache coherency problem that is present on all OS X
38940** supported network file systems. NFS and AFP both observe the
38941** close-to-open semantics for ensuring cache coherency
38942** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
38943** address the requirements for concurrent database access by multiple
38944** readers and writers
38945** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
38946**
38947** To address the performance and cache coherency issues, proxy file locking
38948** changes the way database access is controlled by limiting access to a
38949** single host at a time and moving file locks off of the database file
38950** and onto a proxy file on the local file system.
38951**
38952**
38953** Using proxy locks
38954** -----------------
38955**
38956** C APIs
38957**
38958** sqlite3_file_control(db, dbname, SQLITE_FCNTL_SET_LOCKPROXYFILE,
38959** <proxy_path> | ":auto:");
38960** sqlite3_file_control(db, dbname, SQLITE_FCNTL_GET_LOCKPROXYFILE,
38961** &<proxy_path>);
38962**
38963**
38964** SQL pragmas
38965**
38966** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
38967** PRAGMA [database.]lock_proxy_file
38968**
38969** Specifying ":auto:" means that if there is a conch file with a matching
38970** host ID in it, the proxy path in the conch file will be used, otherwise
38971** a proxy path based on the user's temp dir
38972** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
38973** actual proxy file name is generated from the name and path of the
38974** database file. For example:
38975**
38976** For database path "/Users/me/foo.db"
38977** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
38978**
38979** Once a lock proxy is configured for a database connection, it can not
38980** be removed, however it may be switched to a different proxy path via
38981** the above APIs (assuming the conch file is not being held by another
38982** connection or process).
38983**
38984**
38985** How proxy locking works
38986** -----------------------
38987**
38988** Proxy file locking relies primarily on two new supporting files:
38989**
38990** * conch file to limit access to the database file to a single host
38991** at a time
38992**
38993** * proxy file to act as a proxy for the advisory locks normally
38994** taken on the database
38995**
38996** The conch file - to use a proxy file, sqlite must first "hold the conch"
38997** by taking an sqlite-style shared lock on the conch file, reading the
38998** contents and comparing the host's unique host ID (see below) and lock
38999** proxy path against the values stored in the conch. The conch file is
39000** stored in the same directory as the database file and the file name
39001** is patterned after the database file name as ".<databasename>-conch".
39002** If the conch file does not exist, or its contents do not match the
39003** host ID and/or proxy path, then the lock is escalated to an exclusive
39004** lock and the conch file contents is updated with the host ID and proxy
39005** path and the lock is downgraded to a shared lock again. If the conch
39006** is held by another process (with a shared lock), the exclusive lock
39007** will fail and SQLITE_BUSY is returned.
39008**
39009** The proxy file - a single-byte file used for all advisory file locks
39010** normally taken on the database file. This allows for safe sharing
39011** of the database file for multiple readers and writers on the same
39012** host (the conch ensures that they all use the same local lock file).
39013**
39014** Requesting the lock proxy does not immediately take the conch, it is
39015** only taken when the first request to lock database file is made.
39016** This matches the semantics of the traditional locking behavior, where
39017** opening a connection to a database file does not take a lock on it.
39018** The shared lock and an open file descriptor are maintained until
39019** the connection to the database is closed.
39020**
39021** The proxy file and the lock file are never deleted so they only need
39022** to be created the first time they are used.
39023**
39024** Configuration options
39025** ---------------------
39026**
39027** SQLITE_PREFER_PROXY_LOCKING
39028**
39029** Database files accessed on non-local file systems are
39030** automatically configured for proxy locking, lock files are
39031** named automatically using the same logic as
39032** PRAGMA lock_proxy_file=":auto:"
39033**
39034** SQLITE_PROXY_DEBUG
39035**
39036** Enables the logging of error messages during host id file
39037** retrieval and creation
39038**
39039** LOCKPROXYDIR
39040**
39041** Overrides the default directory used for lock proxy files that
39042** are named automatically via the ":auto:" setting
39043**
39044** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
39045**
39046** Permissions to use when creating a directory for storing the
39047** lock proxy files, only used when LOCKPROXYDIR is not set.
39048**
39049**
39050** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
39051** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
39052** force proxy locking to be used for every database file opened, and 0
39053** will force automatic proxy locking to be disabled for all database
39054** files (explicitly calling the SQLITE_FCNTL_SET_LOCKPROXYFILE pragma or
39055** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
39056*/
39057
39058/*
39059** Proxy locking is only available on MacOSX
39060*/
39061#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
39062
39063/*
39064** The proxyLockingContext has the path and file structures for the remote
39065** and local proxy files in it
39066*/
39067typedef struct proxyLockingContext proxyLockingContext;
39068struct proxyLockingContext {
39069 unixFile *conchFile; /* Open conch file */
39070 char *conchFilePath; /* Name of the conch file */
39071 unixFile *lockProxy; /* Open proxy lock file */
39072 char *lockProxyPath; /* Name of the proxy lock file */
39073 char *dbPath; /* Name of the open file */
39074 int conchHeld; /* 1 if the conch is held, -1 if lockless */
39075 int nFails; /* Number of conch taking failures */
39076 void *oldLockingContext; /* Original lockingcontext to restore on close */
39077 sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */
39078};
39079
39080/*
39081** The proxy lock file path for the database at dbPath is written into lPath,
39082** which must point to valid, writable memory large enough for a maxLen length
39083** file path.
39084*/
39085static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
39086 int len;
39087 int dbLen;
39088 int i;
39089
39090#ifdef LOCKPROXYDIR
39091 len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
39092#else
39093# ifdef _CS_DARWIN_USER_TEMP_DIR
39094 {
39095 if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
39096 OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n",
39097 lPath, errno, osGetpid(0)));
39098 return SQLITE_IOERR_LOCK;
39099 }
39100 len = strlcat(lPath, "sqliteplocks", maxLen);
39101 }
39102# else
39103 len = strlcpy(lPath, "/tmp/", maxLen);
39104# endif
39105#endif
39106
39107 if( lPath[len-1]!='/' ){
39108 len = strlcat(lPath, "/", maxLen);
39109 }
39110
39111 /* transform the db path to a unique cache name */
39112 dbLen = (int)strlen(dbPath);
39113 for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
39114 char c = dbPath[i];
39115 lPath[i+len] = (c=='/')?'_':c;
39116 }
39117 lPath[i+len]='\0';
39118 strlcat(lPath, ":auto:", maxLen);
39119 OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, osGetpid(0)));
39120 return SQLITE_OK;
39121}
39122
39123/*
39124 ** Creates the lock file and any missing directories in lockPath
39125 */
39126static int proxyCreateLockPath(const char *lockPath){
39127 int i, len;
39128 char buf[MAXPATHLEN];
39129 int start = 0;
39130
39131 assert(lockPath!=NULL);
39132 /* try to create all the intermediate directories */
39133 len = (int)strlen(lockPath);
39134 buf[0] = lockPath[0];
39135 for( i=1; i<len; i++ ){
39136 if( lockPath[i] == '/' && (i - start > 0) ){
39137 /* only mkdir if leaf dir != "." or "/" or ".." */
39138 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
39139 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
39140 buf[i]='\0';
39141 if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
39142 int err=errno;
39143 if( err!=EEXIST ) {
39144 OSTRACE(("CREATELOCKPATH FAILED creating %s, "
39145 "'%s' proxy lock path=%s pid=%d\n",
39146 buf, strerror(err), lockPath, osGetpid(0)));
39147 return err;
39148 }
39149 }
39150 }
39151 start=i+1;
39152 }
39153 buf[i] = lockPath[i];
39154 }
39155 OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n",lockPath,osGetpid(0)));
39156 return 0;
39157}
39158
39159/*
39160** Create a new VFS file descriptor (stored in memory obtained from
39161** sqlite3_malloc) and open the file named "path" in the file descriptor.
39162**
39163** The caller is responsible not only for closing the file descriptor
39164** but also for freeing the memory associated with the file descriptor.
39165*/
39166static int proxyCreateUnixFile(
39167 const char *path, /* path for the new unixFile */
39168 unixFile **ppFile, /* unixFile created and returned by ref */
39169 int islockfile /* if non zero missing dirs will be created */
39170) {
39171 int fd = -1;
39172 unixFile *pNew;
39173 int rc = SQLITE_OK;
39174 int openFlags = O_RDWR | O_CREAT;
39175 sqlite3_vfs dummyVfs;
39176 int terrno = 0;
39177 UnixUnusedFd *pUnused = NULL;
39178
39179 /* 1. first try to open/create the file
39180 ** 2. if that fails, and this is a lock file (not-conch), try creating
39181 ** the parent directories and then try again.
39182 ** 3. if that fails, try to open the file read-only
39183 ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
39184 */
39185 pUnused = findReusableFd(path, openFlags);
39186 if( pUnused ){
39187 fd = pUnused->fd;
39188 }else{
39189 pUnused = sqlite3_malloc64(sizeof(*pUnused));
39190 if( !pUnused ){
39191 return SQLITE_NOMEM_BKPT;
39192 }
39193 }
39194 if( fd<0 ){
39195 fd = robust_open(path, openFlags, 0);
39196 terrno = errno;
39197 if( fd<0 && errno==ENOENT && islockfile ){
39198 if( proxyCreateLockPath(path) == SQLITE_OK ){
39199 fd = robust_open(path, openFlags, 0);
39200 }
39201 }
39202 }
39203 if( fd<0 ){
39204 openFlags = O_RDONLY;
39205 fd = robust_open(path, openFlags, 0);
39206 terrno = errno;
39207 }
39208 if( fd<0 ){
39209 if( islockfile ){
39210 return SQLITE_BUSY;
39211 }
39212 switch (terrno) {
39213 case EACCES:
39214 return SQLITE_PERM;
39215 case EIO:
39216 return SQLITE_IOERR_LOCK; /* even though it is the conch */
39217 default:
39218 return SQLITE_CANTOPEN_BKPT;
39219 }
39220 }
39221
39222 pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew));
39223 if( pNew==NULL ){
39224 rc = SQLITE_NOMEM_BKPT;
39225 goto end_create_proxy;
39226 }
39227 memset(pNew, 0, sizeof(unixFile));
39228 pNew->openFlags = openFlags;
39229 memset(&dummyVfs, 0, sizeof(dummyVfs));
39230 dummyVfs.pAppData = (void*)&autolockIoFinder;
39231 dummyVfs.zName = "dummy";
39232 pUnused->fd = fd;
39233 pUnused->flags = openFlags;
39234 pNew->pPreallocatedUnused = pUnused;
39235
39236 rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
39237 if( rc==SQLITE_OK ){
39238 *ppFile = pNew;
39239 return SQLITE_OK;
39240 }
39241end_create_proxy:
39242 robust_close(pNew, fd, __LINE__);
39243 sqlite3_free(pNew);
39244 sqlite3_free(pUnused);
39245 return rc;
39246}
39247
39248#ifdef SQLITE_TEST
39249/* simulate multiple hosts by creating unique hostid file paths */
39250SQLITE_API int sqlite3_hostid_num = 0;
39251#endif
39252
39253#define PROXY_HOSTIDLEN 16 /* conch file host id length */
39254
39255#ifdef HAVE_GETHOSTUUID
39256/* Not always defined in the headers as it ought to be */
39257extern int gethostuuid(uuid_t id, const struct timespec *wait);
39258#endif
39259
39260/* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN
39261** bytes of writable memory.
39262*/
39263static int proxyGetHostID(unsigned char *pHostID, int *pError){
39264 assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
39265 memset(pHostID, 0, PROXY_HOSTIDLEN);
39266#ifdef HAVE_GETHOSTUUID
39267 {
39268 struct timespec timeout = {1, 0}; /* 1 sec timeout */
39269 if( gethostuuid(pHostID, &timeout) ){
39270 int err = errno;
39271 if( pError ){
39272 *pError = err;
39273 }
39274 return SQLITE_IOERR;
39275 }
39276 }
39277#else
39278 UNUSED_PARAMETER(pError);
39279#endif
39280#ifdef SQLITE_TEST
39281 /* simulate multiple hosts by creating unique hostid file paths */
39282 if( sqlite3_hostid_num != 0){
39283 pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
39284 }
39285#endif
39286
39287 return SQLITE_OK;
39288}
39289
39290/* The conch file contains the header, host id and lock file path
39291 */
39292#define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */
39293#define PROXY_HEADERLEN 1 /* conch file header length */
39294#define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN)
39295#define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN)
39296
39297/*
39298** Takes an open conch file, copies the contents to a new path and then moves
39299** it back. The newly created file's file descriptor is assigned to the
39300** conch file structure and finally the original conch file descriptor is
39301** closed. Returns zero if successful.
39302*/
39303static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
39304 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
39305 unixFile *conchFile = pCtx->conchFile;
39306 char tPath[MAXPATHLEN];
39307 char buf[PROXY_MAXCONCHLEN];
39308 char *cPath = pCtx->conchFilePath;
39309 size_t readLen = 0;
39310 size_t pathLen = 0;
39311 char errmsg[64] = "";
39312 int fd = -1;
39313 int rc = -1;
39314 UNUSED_PARAMETER(myHostID);
39315
39316 /* create a new path by replace the trailing '-conch' with '-break' */
39317 pathLen = strlcpy(tPath, cPath, MAXPATHLEN);
39318 if( pathLen>MAXPATHLEN || pathLen<6 ||
39319 (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
39320 sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
39321 goto end_breaklock;
39322 }
39323 /* read the conch content */
39324 readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
39325 if( readLen<PROXY_PATHINDEX ){
39326 sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
39327 goto end_breaklock;
39328 }
39329 /* write it out to the temporary break file */
39330 fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), 0);
39331 if( fd<0 ){
39332 sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno);
39333 goto end_breaklock;
39334 }
39335 if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){
39336 sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno);
39337 goto end_breaklock;
39338 }
39339 if( rename(tPath, cPath) ){
39340 sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno);
39341 goto end_breaklock;
39342 }
39343 rc = 0;
39344 fprintf(stderr, "broke stale lock on %s\n", cPath);
39345 robust_close(pFile, conchFile->h, __LINE__);
39346 conchFile->h = fd;
39347 conchFile->openFlags = O_RDWR | O_CREAT;
39348
39349end_breaklock:
39350 if( rc ){
39351 if( fd>=0 ){
39352 osUnlink(tPath);
39353 robust_close(pFile, fd, __LINE__);
39354 }
39355 fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
39356 }
39357 return rc;
39358}
39359
39360/* Take the requested lock on the conch file and break a stale lock if the
39361** host id matches.
39362*/
39363static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
39364 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
39365 unixFile *conchFile = pCtx->conchFile;
39366 int rc = SQLITE_OK;
39367 int nTries = 0;
39368 struct timespec conchModTime;
39369
39370 memset(&conchModTime, 0, sizeof(conchModTime));
39371 do {
39372 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
39373 nTries ++;
39374 if( rc==SQLITE_BUSY ){
39375 /* If the lock failed (busy):
39376 * 1st try: get the mod time of the conch, wait 0.5s and try again.
39377 * 2nd try: fail if the mod time changed or host id is different, wait
39378 * 10 sec and try again
39379 * 3rd try: break the lock unless the mod time has changed.
39380 */
39381 struct stat buf;
39382 if( osFstat(conchFile->h, &buf) ){
39383 storeLastErrno(pFile, errno);
39384 return SQLITE_IOERR_LOCK;
39385 }
39386
39387 if( nTries==1 ){
39388 conchModTime = buf.st_mtimespec;
39389 usleep(500000); /* wait 0.5 sec and try the lock again*/
39390 continue;
39391 }
39392
39393 assert( nTries>1 );
39394 if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec ||
39395 conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
39396 return SQLITE_BUSY;
39397 }
39398
39399 if( nTries==2 ){
39400 char tBuf[PROXY_MAXCONCHLEN];
39401 int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
39402 if( len<0 ){
39403 storeLastErrno(pFile, errno);
39404 return SQLITE_IOERR_LOCK;
39405 }
39406 if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
39407 /* don't break the lock if the host id doesn't match */
39408 if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){
39409 return SQLITE_BUSY;
39410 }
39411 }else{
39412 /* don't break the lock on short read or a version mismatch */
39413 return SQLITE_BUSY;
39414 }
39415 usleep(10000000); /* wait 10 sec and try the lock again */
39416 continue;
39417 }
39418
39419 assert( nTries==3 );
39420 if( 0==proxyBreakConchLock(pFile, myHostID) ){
39421 rc = SQLITE_OK;
39422 if( lockType==EXCLUSIVE_LOCK ){
39423 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
39424 }
39425 if( !rc ){
39426 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
39427 }
39428 }
39429 }
39430 } while( rc==SQLITE_BUSY && nTries<3 );
39431
39432 return rc;
39433}
39434
39435/* Takes the conch by taking a shared lock and read the contents conch, if
39436** lockPath is non-NULL, the host ID and lock file path must match. A NULL
39437** lockPath means that the lockPath in the conch file will be used if the
39438** host IDs match, or a new lock path will be generated automatically
39439** and written to the conch file.
39440*/
39441static int proxyTakeConch(unixFile *pFile){
39442 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
39443
39444 if( pCtx->conchHeld!=0 ){
39445 return SQLITE_OK;
39446 }else{
39447 unixFile *conchFile = pCtx->conchFile;
39448 uuid_t myHostID;
39449 int pError = 0;
39450 char readBuf[PROXY_MAXCONCHLEN];
39451 char lockPath[MAXPATHLEN];
39452 char *tempLockPath = NULL;
39453 int rc = SQLITE_OK;
39454 int createConch = 0;
39455 int hostIdMatch = 0;
39456 int readLen = 0;
39457 int tryOldLockPath = 0;
39458 int forceNewLockPath = 0;
39459
39460 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
39461 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
39462 osGetpid(0)));
39463
39464 rc = proxyGetHostID(myHostID, &pError);
39465 if( (rc&0xff)==SQLITE_IOERR ){
39466 storeLastErrno(pFile, pError);
39467 goto end_takeconch;
39468 }
39469 rc = proxyConchLock(pFile, myHostID, SHARED_LOCK);
39470 if( rc!=SQLITE_OK ){
39471 goto end_takeconch;
39472 }
39473 /* read the existing conch file */
39474 readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN);
39475 if( readLen<0 ){
39476 /* I/O error: lastErrno set by seekAndRead */
39477 storeLastErrno(pFile, conchFile->lastErrno);
39478 rc = SQLITE_IOERR_READ;
39479 goto end_takeconch;
39480 }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) ||
39481 readBuf[0]!=(char)PROXY_CONCHVERSION ){
39482 /* a short read or version format mismatch means we need to create a new
39483 ** conch file.
39484 */
39485 createConch = 1;
39486 }
39487 /* if the host id matches and the lock path already exists in the conch
39488 ** we'll try to use the path there, if we can't open that path, we'll
39489 ** retry with a new auto-generated path
39490 */
39491 do { /* in case we need to try again for an :auto: named lock file */
39492
39493 if( !createConch && !forceNewLockPath ){
39494 hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID,
39495 PROXY_HOSTIDLEN);
39496 /* if the conch has data compare the contents */
39497 if( !pCtx->lockProxyPath ){
39498 /* for auto-named local lock file, just check the host ID and we'll
39499 ** use the local lock file path that's already in there
39500 */
39501 if( hostIdMatch ){
39502 size_t pathLen = (readLen - PROXY_PATHINDEX);
39503
39504 if( pathLen>=MAXPATHLEN ){
39505 pathLen=MAXPATHLEN-1;
39506 }
39507 memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen);
39508 lockPath[pathLen] = 0;
39509 tempLockPath = lockPath;
39510 tryOldLockPath = 1;
39511 /* create a copy of the lock path if the conch is taken */
39512 goto end_takeconch;
39513 }
39514 }else if( hostIdMatch
39515 && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
39516 readLen-PROXY_PATHINDEX)
39517 ){
39518 /* conch host and lock path match */
39519 goto end_takeconch;
39520 }
39521 }
39522
39523 /* if the conch isn't writable and doesn't match, we can't take it */
39524 if( (conchFile->openFlags&O_RDWR) == 0 ){
39525 rc = SQLITE_BUSY;
39526 goto end_takeconch;
39527 }
39528
39529 /* either the conch didn't match or we need to create a new one */
39530 if( !pCtx->lockProxyPath ){
39531 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
39532 tempLockPath = lockPath;
39533 /* create a copy of the lock path _only_ if the conch is taken */
39534 }
39535
39536 /* update conch with host and path (this will fail if other process
39537 ** has a shared lock already), if the host id matches, use the big
39538 ** stick.
39539 */
39540 futimes(conchFile->h, NULL);
39541 if( hostIdMatch && !createConch ){
39542 if( conchFile->pInode && conchFile->pInode->nShared>1 ){
39543 /* We are trying for an exclusive lock but another thread in this
39544 ** same process is still holding a shared lock. */
39545 rc = SQLITE_BUSY;
39546 } else {
39547 rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
39548 }
39549 }else{
39550 rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
39551 }
39552 if( rc==SQLITE_OK ){
39553 char writeBuffer[PROXY_MAXCONCHLEN];
39554 int writeSize = 0;
39555
39556 writeBuffer[0] = (char)PROXY_CONCHVERSION;
39557 memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
39558 if( pCtx->lockProxyPath!=NULL ){
39559 strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
39560 MAXPATHLEN);
39561 }else{
39562 strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
39563 }
39564 writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
39565 robust_ftruncate(conchFile->h, writeSize);
39566 rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
39567 full_fsync(conchFile->h,0,0);
39568 /* If we created a new conch file (not just updated the contents of a
39569 ** valid conch file), try to match the permissions of the database
39570 */
39571 if( rc==SQLITE_OK && createConch ){
39572 struct stat buf;
39573 int err = osFstat(pFile->h, &buf);
39574 if( err==0 ){
39575 mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP |
39576 S_IROTH|S_IWOTH);
39577 /* try to match the database file R/W permissions, ignore failure */
39578#ifndef SQLITE_PROXY_DEBUG
39579 osFchmod(conchFile->h, cmode);
39580#else
39581 do{
39582 rc = osFchmod(conchFile->h, cmode);
39583 }while( rc==(-1) && errno==EINTR );
39584 if( rc!=0 ){
39585 int code = errno;
39586 fprintf(stderr, "fchmod %o FAILED with %d %s\n",
39587 cmode, code, strerror(code));
39588 } else {
39589 fprintf(stderr, "fchmod %o SUCCEDED\n",cmode);
39590 }
39591 }else{
39592 int code = errno;
39593 fprintf(stderr, "STAT FAILED[%d] with %d %s\n",
39594 err, code, strerror(code));
39595#endif
39596 }
39597 }
39598 }
39599 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
39600
39601 end_takeconch:
39602 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
39603 if( rc==SQLITE_OK && pFile->openFlags ){
39604 int fd;
39605 if( pFile->h>=0 ){
39606 robust_close(pFile, pFile->h, __LINE__);
39607 }
39608 pFile->h = -1;
39609 fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
39610 OSTRACE(("TRANSPROXY: OPEN %d\n", fd));
39611 if( fd>=0 ){
39612 pFile->h = fd;
39613 }else{
39614 rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
39615 during locking */
39616 }
39617 }
39618 if( rc==SQLITE_OK && !pCtx->lockProxy ){
39619 char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
39620 rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
39621 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){
39622 /* we couldn't create the proxy lock file with the old lock file path
39623 ** so try again via auto-naming
39624 */
39625 forceNewLockPath = 1;
39626 tryOldLockPath = 0;
39627 continue; /* go back to the do {} while start point, try again */
39628 }
39629 }
39630 if( rc==SQLITE_OK ){
39631 /* Need to make a copy of path if we extracted the value
39632 ** from the conch file or the path was allocated on the stack
39633 */
39634 if( tempLockPath ){
39635 pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
39636 if( !pCtx->lockProxyPath ){
39637 rc = SQLITE_NOMEM_BKPT;
39638 }
39639 }
39640 }
39641 if( rc==SQLITE_OK ){
39642 pCtx->conchHeld = 1;
39643
39644 if( pCtx->lockProxy->pMethod == &afpIoMethods ){
39645 afpLockingContext *afpCtx;
39646 afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
39647 afpCtx->dbPath = pCtx->lockProxyPath;
39648 }
39649 } else {
39650 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
39651 }
39652 OSTRACE(("TAKECONCH %d %s\n", conchFile->h,
39653 rc==SQLITE_OK?"ok":"failed"));
39654 return rc;
39655 } while (1); /* in case we need to retry the :auto: lock file -
39656 ** we should never get here except via the 'continue' call. */
39657 }
39658}
39659
39660/*
39661** If pFile holds a lock on a conch file, then release that lock.
39662*/
39663static int proxyReleaseConch(unixFile *pFile){
39664 int rc = SQLITE_OK; /* Subroutine return code */
39665 proxyLockingContext *pCtx; /* The locking context for the proxy lock */
39666 unixFile *conchFile; /* Name of the conch file */
39667
39668 pCtx = (proxyLockingContext *)pFile->lockingContext;
39669 conchFile = pCtx->conchFile;
39670 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
39671 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
39672 osGetpid(0)));
39673 if( pCtx->conchHeld>0 ){
39674 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
39675 }
39676 pCtx->conchHeld = 0;
39677 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
39678 (rc==SQLITE_OK ? "ok" : "failed")));
39679 return rc;
39680}
39681
39682/*
39683** Given the name of a database file, compute the name of its conch file.
39684** Store the conch filename in memory obtained from sqlite3_malloc64().
39685** Make *pConchPath point to the new name. Return SQLITE_OK on success
39686** or SQLITE_NOMEM if unable to obtain memory.
39687**
39688** The caller is responsible for ensuring that the allocated memory
39689** space is eventually freed.
39690**
39691** *pConchPath is set to NULL if a memory allocation error occurs.
39692*/
39693static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
39694 int i; /* Loop counter */
39695 int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
39696 char *conchPath; /* buffer in which to construct conch name */
39697
39698 /* Allocate space for the conch filename and initialize the name to
39699 ** the name of the original database file. */
39700 *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8);
39701 if( conchPath==0 ){
39702 return SQLITE_NOMEM_BKPT;
39703 }
39704 memcpy(conchPath, dbPath, len+1);
39705
39706 /* now insert a "." before the last / character */
39707 for( i=(len-1); i>=0; i-- ){
39708 if( conchPath[i]=='/' ){
39709 i++;
39710 break;
39711 }
39712 }
39713 conchPath[i]='.';
39714 while ( i<len ){
39715 conchPath[i+1]=dbPath[i];
39716 i++;
39717 }
39718
39719 /* append the "-conch" suffix to the file */
39720 memcpy(&conchPath[i+1], "-conch", 7);
39721 assert( (int)strlen(conchPath) == len+7 );
39722
39723 return SQLITE_OK;
39724}
39725
39726
39727/* Takes a fully configured proxy locking-style unix file and switches
39728** the local lock file path
39729*/
39730static int switchLockProxyPath(unixFile *pFile, const char *path) {
39731 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
39732 char *oldPath = pCtx->lockProxyPath;
39733 int rc = SQLITE_OK;
39734
39735 if( pFile->eFileLock!=NO_LOCK ){
39736 return SQLITE_BUSY;
39737 }
39738
39739 /* nothing to do if the path is NULL, :auto: or matches the existing path */
39740 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
39741 (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
39742 return SQLITE_OK;
39743 }else{
39744 unixFile *lockProxy = pCtx->lockProxy;
39745 pCtx->lockProxy=NULL;
39746 pCtx->conchHeld = 0;
39747 if( lockProxy!=NULL ){
39748 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
39749 if( rc ) return rc;
39750 sqlite3_free(lockProxy);
39751 }
39752 sqlite3_free(oldPath);
39753 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
39754 }
39755
39756 return rc;
39757}
39758
39759/*
39760** pFile is a file that has been opened by a prior xOpen call. dbPath
39761** is a string buffer at least MAXPATHLEN+1 characters in size.
39762**
39763** This routine find the filename associated with pFile and writes it
39764** int dbPath.
39765*/
39766static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
39767#if defined(__APPLE__)
39768 if( pFile->pMethod == &afpIoMethods ){
39769 /* afp style keeps a reference to the db path in the filePath field
39770 ** of the struct */
39771 assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
39772 strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
39773 MAXPATHLEN);
39774 } else
39775#endif
39776 if( pFile->pMethod == &dotlockIoMethods ){
39777 /* dot lock style uses the locking context to store the dot lock
39778 ** file path */
39779 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
39780 memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
39781 }else{
39782 /* all other styles use the locking context to store the db file path */
39783 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
39784 strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
39785 }
39786 return SQLITE_OK;
39787}
39788
39789/*
39790** Takes an already filled in unix file and alters it so all file locking
39791** will be performed on the local proxy lock file. The following fields
39792** are preserved in the locking context so that they can be restored and
39793** the unix structure properly cleaned up at close time:
39794** ->lockingContext
39795** ->pMethod
39796*/
39797static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
39798 proxyLockingContext *pCtx;
39799 char dbPath[MAXPATHLEN+1]; /* Name of the database file */
39800 char *lockPath=NULL;
39801 int rc = SQLITE_OK;
39802
39803 if( pFile->eFileLock!=NO_LOCK ){
39804 return SQLITE_BUSY;
39805 }
39806 proxyGetDbPathForUnixFile(pFile, dbPath);
39807 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
39808 lockPath=NULL;
39809 }else{
39810 lockPath=(char *)path;
39811 }
39812
39813 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
39814 (lockPath ? lockPath : ":auto:"), osGetpid(0)));
39815
39816 pCtx = sqlite3_malloc64( sizeof(*pCtx) );
39817 if( pCtx==0 ){
39818 return SQLITE_NOMEM_BKPT;
39819 }
39820 memset(pCtx, 0, sizeof(*pCtx));
39821
39822 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
39823 if( rc==SQLITE_OK ){
39824 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
39825 if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
39826 /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
39827 ** (c) the file system is read-only, then enable no-locking access.
39828 ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
39829 ** that openFlags will have only one of O_RDONLY or O_RDWR.
39830 */
39831 struct statfs fsInfo;
39832 struct stat conchInfo;
39833 int goLockless = 0;
39834
39835 if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
39836 int err = errno;
39837 if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
39838 goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY;
39839 }
39840 }
39841 if( goLockless ){
39842 pCtx->conchHeld = -1; /* read only FS/ lockless */
39843 rc = SQLITE_OK;
39844 }
39845 }
39846 }
39847 if( rc==SQLITE_OK && lockPath ){
39848 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
39849 }
39850
39851 if( rc==SQLITE_OK ){
39852 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
39853 if( pCtx->dbPath==NULL ){
39854 rc = SQLITE_NOMEM_BKPT;
39855 }
39856 }
39857 if( rc==SQLITE_OK ){
39858 /* all memory is allocated, proxys are created and assigned,
39859 ** switch the locking context and pMethod then return.
39860 */
39861 pCtx->oldLockingContext = pFile->lockingContext;
39862 pFile->lockingContext = pCtx;
39863 pCtx->pOldMethod = pFile->pMethod;
39864 pFile->pMethod = &proxyIoMethods;
39865 }else{
39866 if( pCtx->conchFile ){
39867 pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
39868 sqlite3_free(pCtx->conchFile);
39869 }
39870 sqlite3DbFree(0, pCtx->lockProxyPath);
39871 sqlite3_free(pCtx->conchFilePath);
39872 sqlite3_free(pCtx);
39873 }
39874 OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
39875 (rc==SQLITE_OK ? "ok" : "failed")));
39876 return rc;
39877}
39878
39879
39880/*
39881** This routine handles sqlite3_file_control() calls that are specific
39882** to proxy locking.
39883*/
39884static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
39885 switch( op ){
39886 case SQLITE_FCNTL_GET_LOCKPROXYFILE: {
39887 unixFile *pFile = (unixFile*)id;
39888 if( pFile->pMethod == &proxyIoMethods ){
39889 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
39890 proxyTakeConch(pFile);
39891 if( pCtx->lockProxyPath ){
39892 *(const char **)pArg = pCtx->lockProxyPath;
39893 }else{
39894 *(const char **)pArg = ":auto: (not held)";
39895 }
39896 } else {
39897 *(const char **)pArg = NULL;
39898 }
39899 return SQLITE_OK;
39900 }
39901 case SQLITE_FCNTL_SET_LOCKPROXYFILE: {
39902 unixFile *pFile = (unixFile*)id;
39903 int rc = SQLITE_OK;
39904 int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
39905 if( pArg==NULL || (const char *)pArg==0 ){
39906 if( isProxyStyle ){
39907 /* turn off proxy locking - not supported. If support is added for
39908 ** switching proxy locking mode off then it will need to fail if
39909 ** the journal mode is WAL mode.
39910 */
39911 rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
39912 }else{
39913 /* turn off proxy locking - already off - NOOP */
39914 rc = SQLITE_OK;
39915 }
39916 }else{
39917 const char *proxyPath = (const char *)pArg;
39918 if( isProxyStyle ){
39919 proxyLockingContext *pCtx =
39920 (proxyLockingContext*)pFile->lockingContext;
39921 if( !strcmp(pArg, ":auto:")
39922 || (pCtx->lockProxyPath &&
39923 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
39924 ){
39925 rc = SQLITE_OK;
39926 }else{
39927 rc = switchLockProxyPath(pFile, proxyPath);
39928 }
39929 }else{
39930 /* turn on proxy file locking */
39931 rc = proxyTransformUnixFile(pFile, proxyPath);
39932 }
39933 }
39934 return rc;
39935 }
39936 default: {
39937 assert( 0 ); /* The call assures that only valid opcodes are sent */
39938 }
39939 }
39940 /*NOTREACHED*/
39941 return SQLITE_ERROR;
39942}
39943
39944/*
39945** Within this division (the proxying locking implementation) the procedures
39946** above this point are all utilities. The lock-related methods of the
39947** proxy-locking sqlite3_io_method object follow.
39948*/
39949
39950
39951/*
39952** This routine checks if there is a RESERVED lock held on the specified
39953** file by this or any other process. If such a lock is held, set *pResOut
39954** to a non-zero value otherwise *pResOut is set to zero. The return value
39955** is set to SQLITE_OK unless an I/O error occurs during lock checking.
39956*/
39957static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
39958 unixFile *pFile = (unixFile*)id;
39959 int rc = proxyTakeConch(pFile);
39960 if( rc==SQLITE_OK ){
39961 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
39962 if( pCtx->conchHeld>0 ){
39963 unixFile *proxy = pCtx->lockProxy;
39964 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
39965 }else{ /* conchHeld < 0 is lockless */
39966 pResOut=0;
39967 }
39968 }
39969 return rc;
39970}
39971
39972/*
39973** Lock the file with the lock specified by parameter eFileLock - one
39974** of the following:
39975**
39976** (1) SHARED_LOCK
39977** (2) RESERVED_LOCK
39978** (3) PENDING_LOCK
39979** (4) EXCLUSIVE_LOCK
39980**
39981** Sometimes when requesting one lock state, additional lock states
39982** are inserted in between. The locking might fail on one of the later
39983** transitions leaving the lock state different from what it started but
39984** still short of its goal. The following chart shows the allowed
39985** transitions and the inserted intermediate states:
39986**
39987** UNLOCKED -> SHARED
39988** SHARED -> RESERVED
39989** SHARED -> (PENDING) -> EXCLUSIVE
39990** RESERVED -> (PENDING) -> EXCLUSIVE
39991** PENDING -> EXCLUSIVE
39992**
39993** This routine will only increase a lock. Use the sqlite3OsUnlock()
39994** routine to lower a locking level.
39995*/
39996static int proxyLock(sqlite3_file *id, int eFileLock) {
39997 unixFile *pFile = (unixFile*)id;
39998 int rc = proxyTakeConch(pFile);
39999 if( rc==SQLITE_OK ){
40000 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
40001 if( pCtx->conchHeld>0 ){
40002 unixFile *proxy = pCtx->lockProxy;
40003 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
40004 pFile->eFileLock = proxy->eFileLock;
40005 }else{
40006 /* conchHeld < 0 is lockless */
40007 }
40008 }
40009 return rc;
40010}
40011
40012
40013/*
40014** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
40015** must be either NO_LOCK or SHARED_LOCK.
40016**
40017** If the locking level of the file descriptor is already at or below
40018** the requested locking level, this routine is a no-op.
40019*/
40020static int proxyUnlock(sqlite3_file *id, int eFileLock) {
40021 unixFile *pFile = (unixFile*)id;
40022 int rc = proxyTakeConch(pFile);
40023 if( rc==SQLITE_OK ){
40024 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
40025 if( pCtx->conchHeld>0 ){
40026 unixFile *proxy = pCtx->lockProxy;
40027 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
40028 pFile->eFileLock = proxy->eFileLock;
40029 }else{
40030 /* conchHeld < 0 is lockless */
40031 }
40032 }
40033 return rc;
40034}
40035
40036/*
40037** Close a file that uses proxy locks.
40038*/
40039static int proxyClose(sqlite3_file *id) {
40040 if( ALWAYS(id) ){
40041 unixFile *pFile = (unixFile*)id;
40042 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
40043 unixFile *lockProxy = pCtx->lockProxy;
40044 unixFile *conchFile = pCtx->conchFile;
40045 int rc = SQLITE_OK;
40046
40047 if( lockProxy ){
40048 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
40049 if( rc ) return rc;
40050 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
40051 if( rc ) return rc;
40052 sqlite3_free(lockProxy);
40053 pCtx->lockProxy = 0;
40054 }
40055 if( conchFile ){
40056 if( pCtx->conchHeld ){
40057 rc = proxyReleaseConch(pFile);
40058 if( rc ) return rc;
40059 }
40060 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
40061 if( rc ) return rc;
40062 sqlite3_free(conchFile);
40063 }
40064 sqlite3DbFree(0, pCtx->lockProxyPath);
40065 sqlite3_free(pCtx->conchFilePath);
40066 sqlite3DbFree(0, pCtx->dbPath);
40067 /* restore the original locking context and pMethod then close it */
40068 pFile->lockingContext = pCtx->oldLockingContext;
40069 pFile->pMethod = pCtx->pOldMethod;
40070 sqlite3_free(pCtx);
40071 return pFile->pMethod->xClose(id);
40072 }
40073 return SQLITE_OK;
40074}
40075
40076
40077
40078#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
40079/*
40080** The proxy locking style is intended for use with AFP filesystems.
40081** And since AFP is only supported on MacOSX, the proxy locking is also
40082** restricted to MacOSX.
40083**
40084**
40085******************* End of the proxy lock implementation **********************
40086******************************************************************************/
40087
40088/*
40089** Initialize the operating system interface.
40090**
40091** This routine registers all VFS implementations for unix-like operating
40092** systems. This routine, and the sqlite3_os_end() routine that follows,
40093** should be the only routines in this file that are visible from other
40094** files.
40095**
40096** This routine is called once during SQLite initialization and by a
40097** single thread. The memory allocation and mutex subsystems have not
40098** necessarily been initialized when this routine is called, and so they
40099** should not be used.
40100*/
40101SQLITE_API int sqlite3_os_init(void){
40102 /*
40103 ** The following macro defines an initializer for an sqlite3_vfs object.
40104 ** The name of the VFS is NAME. The pAppData is a pointer to a pointer
40105 ** to the "finder" function. (pAppData is a pointer to a pointer because
40106 ** silly C90 rules prohibit a void* from being cast to a function pointer
40107 ** and so we have to go through the intermediate pointer to avoid problems
40108 ** when compiling with -pedantic-errors on GCC.)
40109 **
40110 ** The FINDER parameter to this macro is the name of the pointer to the
40111 ** finder-function. The finder-function returns a pointer to the
40112 ** sqlite_io_methods object that implements the desired locking
40113 ** behaviors. See the division above that contains the IOMETHODS
40114 ** macro for addition information on finder-functions.
40115 **
40116 ** Most finders simply return a pointer to a fixed sqlite3_io_methods
40117 ** object. But the "autolockIoFinder" available on MacOSX does a little
40118 ** more than that; it looks at the filesystem type that hosts the
40119 ** database file and tries to choose an locking method appropriate for
40120 ** that filesystem time.
40121 */
40122 #define UNIXVFS(VFSNAME, FINDER) { \
40123 3, /* iVersion */ \
40124 sizeof(unixFile), /* szOsFile */ \
40125 MAX_PATHNAME, /* mxPathname */ \
40126 0, /* pNext */ \
40127 VFSNAME, /* zName */ \
40128 (void*)&FINDER, /* pAppData */ \
40129 unixOpen, /* xOpen */ \
40130 unixDelete, /* xDelete */ \
40131 unixAccess, /* xAccess */ \
40132 unixFullPathname, /* xFullPathname */ \
40133 unixDlOpen, /* xDlOpen */ \
40134 unixDlError, /* xDlError */ \
40135 unixDlSym, /* xDlSym */ \
40136 unixDlClose, /* xDlClose */ \
40137 unixRandomness, /* xRandomness */ \
40138 unixSleep, /* xSleep */ \
40139 unixCurrentTime, /* xCurrentTime */ \
40140 unixGetLastError, /* xGetLastError */ \
40141 unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \
40142 unixSetSystemCall, /* xSetSystemCall */ \
40143 unixGetSystemCall, /* xGetSystemCall */ \
40144 unixNextSystemCall, /* xNextSystemCall */ \
40145 }
40146
40147 /*
40148 ** All default VFSes for unix are contained in the following array.
40149 **
40150 ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
40151 ** by the SQLite core when the VFS is registered. So the following
40152 ** array cannot be const.
40153 */
40154 static sqlite3_vfs aVfs[] = {
40155#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
40156 UNIXVFS("unix", autolockIoFinder ),
40157#elif OS_VXWORKS
40158 UNIXVFS("unix", vxworksIoFinder ),
40159#else
40160 UNIXVFS("unix", posixIoFinder ),
40161#endif
40162 UNIXVFS("unix-none", nolockIoFinder ),
40163 UNIXVFS("unix-dotfile", dotlockIoFinder ),
40164 UNIXVFS("unix-excl", posixIoFinder ),
40165#if OS_VXWORKS
40166 UNIXVFS("unix-namedsem", semIoFinder ),
40167#endif
40168#if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS
40169 UNIXVFS("unix-posix", posixIoFinder ),
40170#endif
40171#if SQLITE_ENABLE_LOCKING_STYLE
40172 UNIXVFS("unix-flock", flockIoFinder ),
40173#endif
40174#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
40175 UNIXVFS("unix-afp", afpIoFinder ),
40176 UNIXVFS("unix-nfs", nfsIoFinder ),
40177 UNIXVFS("unix-proxy", proxyIoFinder ),
40178#endif
40179 };
40180 unsigned int i; /* Loop counter */
40181
40182 /* Double-check that the aSyscall[] array has been constructed
40183 ** correctly. See ticket [bb3a86e890c8e96ab] */
40184 assert( ArraySize(aSyscall)==29 );
40185
40186 /* Register all VFSes defined in the aVfs[] array */
40187 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
40188 sqlite3_vfs_register(&aVfs[i], i==0);
40189 }
40190 unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
40191 return SQLITE_OK;
40192}
40193
40194/*
40195** Shutdown the operating system interface.
40196**
40197** Some operating systems might need to do some cleanup in this routine,
40198** to release dynamically allocated objects. But not on unix.
40199** This routine is a no-op for unix.
40200*/
40201SQLITE_API int sqlite3_os_end(void){
40202 unixBigLock = 0;
40203 return SQLITE_OK;
40204}
40205
40206#endif /* SQLITE_OS_UNIX */
40207
40208/************** End of os_unix.c *********************************************/
40209/************** Begin file os_win.c ******************************************/
40210/*
40211** 2004 May 22
40212**
40213** The author disclaims copyright to this source code. In place of
40214** a legal notice, here is a blessing:
40215**
40216** May you do good and not evil.
40217** May you find forgiveness for yourself and forgive others.
40218** May you share freely, never taking more than you give.
40219**
40220******************************************************************************
40221**
40222** This file contains code that is specific to Windows.
40223*/
40224/* #include "sqliteInt.h" */
40225#if SQLITE_OS_WIN /* This file is used for Windows only */
40226
40227/*
40228** Include code that is common to all os_*.c files
40229*/
40230/************** Include os_common.h in the middle of os_win.c ****************/
40231/************** Begin file os_common.h ***************************************/
40232/*
40233** 2004 May 22
40234**
40235** The author disclaims copyright to this source code. In place of
40236** a legal notice, here is a blessing:
40237**
40238** May you do good and not evil.
40239** May you find forgiveness for yourself and forgive others.
40240** May you share freely, never taking more than you give.
40241**
40242******************************************************************************
40243**
40244** This file contains macros and a little bit of code that is common to
40245** all of the platform-specific files (os_*.c) and is #included into those
40246** files.
40247**
40248** This file should be #included by the os_*.c files only. It is not a
40249** general purpose header file.
40250*/
40251#ifndef _OS_COMMON_H_
40252#define _OS_COMMON_H_
40253
40254/*
40255** At least two bugs have slipped in because we changed the MEMORY_DEBUG
40256** macro to SQLITE_DEBUG and some older makefiles have not yet made the
40257** switch. The following code should catch this problem at compile-time.
40258*/
40259#ifdef MEMORY_DEBUG
40260# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
40261#endif
40262
40263/*
40264** Macros for performance tracing. Normally turned off. Only works
40265** on i486 hardware.
40266*/
40267#ifdef SQLITE_PERFORMANCE_TRACE
40268
40269/*
40270** hwtime.h contains inline assembler code for implementing
40271** high-performance timing routines.
40272*/
40273/************** Include hwtime.h in the middle of os_common.h ****************/
40274/************** Begin file hwtime.h ******************************************/
40275/*
40276** 2008 May 27
40277**
40278** The author disclaims copyright to this source code. In place of
40279** a legal notice, here is a blessing:
40280**
40281** May you do good and not evil.
40282** May you find forgiveness for yourself and forgive others.
40283** May you share freely, never taking more than you give.
40284**
40285******************************************************************************
40286**
40287** This file contains inline asm code for retrieving "high-performance"
40288** counters for x86 class CPUs.
40289*/
40290#ifndef SQLITE_HWTIME_H
40291#define SQLITE_HWTIME_H
40292
40293/*
40294** The following routine only works on pentium-class (or newer) processors.
40295** It uses the RDTSC opcode to read the cycle count value out of the
40296** processor and returns that value. This can be used for high-res
40297** profiling.
40298*/
40299#if (defined(__GNUC__) || defined(_MSC_VER)) && \
40300 (defined(i386) || defined(__i386__) || defined(_M_IX86))
40301
40302 #if defined(__GNUC__)
40303
40304 __inline__ sqlite_uint64 sqlite3Hwtime(void){
40305 unsigned int lo, hi;
40306 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
40307 return (sqlite_uint64)hi << 32 | lo;
40308 }
40309
40310 #elif defined(_MSC_VER)
40311
40312 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
40313 __asm {
40314 rdtsc
40315 ret ; return value at EDX:EAX
40316 }
40317 }
40318
40319 #endif
40320
40321#elif (defined(__GNUC__) && defined(__x86_64__))
40322
40323 __inline__ sqlite_uint64 sqlite3Hwtime(void){
40324 unsigned long val;
40325 __asm__ __volatile__ ("rdtsc" : "=A" (val));
40326 return val;
40327 }
40328
40329#elif (defined(__GNUC__) && defined(__ppc__))
40330
40331 __inline__ sqlite_uint64 sqlite3Hwtime(void){
40332 unsigned long long retval;
40333 unsigned long junk;
40334 __asm__ __volatile__ ("\n\
40335 1: mftbu %1\n\
40336 mftb %L0\n\
40337 mftbu %0\n\
40338 cmpw %0,%1\n\
40339 bne 1b"
40340 : "=r" (retval), "=r" (junk));
40341 return retval;
40342 }
40343
40344#else
40345
40346 #error Need implementation of sqlite3Hwtime() for your platform.
40347
40348 /*
40349 ** To compile without implementing sqlite3Hwtime() for your platform,
40350 ** you can remove the above #error and use the following
40351 ** stub function. You will lose timing support for many
40352 ** of the debugging and testing utilities, but it should at
40353 ** least compile and run.
40354 */
40355SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
40356
40357#endif
40358
40359#endif /* !defined(SQLITE_HWTIME_H) */
40360
40361/************** End of hwtime.h **********************************************/
40362/************** Continuing where we left off in os_common.h ******************/
40363
40364static sqlite_uint64 g_start;
40365static sqlite_uint64 g_elapsed;
40366#define TIMER_START g_start=sqlite3Hwtime()
40367#define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
40368#define TIMER_ELAPSED g_elapsed
40369#else
40370#define TIMER_START
40371#define TIMER_END
40372#define TIMER_ELAPSED ((sqlite_uint64)0)
40373#endif
40374
40375/*
40376** If we compile with the SQLITE_TEST macro set, then the following block
40377** of code will give us the ability to simulate a disk I/O error. This
40378** is used for testing the I/O recovery logic.
40379*/
40380#if defined(SQLITE_TEST)
40381SQLITE_API extern int sqlite3_io_error_hit;
40382SQLITE_API extern int sqlite3_io_error_hardhit;
40383SQLITE_API extern int sqlite3_io_error_pending;
40384SQLITE_API extern int sqlite3_io_error_persist;
40385SQLITE_API extern int sqlite3_io_error_benign;
40386SQLITE_API extern int sqlite3_diskfull_pending;
40387SQLITE_API extern int sqlite3_diskfull;
40388#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
40389#define SimulateIOError(CODE) \
40390 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
40391 || sqlite3_io_error_pending-- == 1 ) \
40392 { local_ioerr(); CODE; }
40393static void local_ioerr(){
40394 IOTRACE(("IOERR\n"));
40395 sqlite3_io_error_hit++;
40396 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
40397}
40398#define SimulateDiskfullError(CODE) \
40399 if( sqlite3_diskfull_pending ){ \
40400 if( sqlite3_diskfull_pending == 1 ){ \
40401 local_ioerr(); \
40402 sqlite3_diskfull = 1; \
40403 sqlite3_io_error_hit = 1; \
40404 CODE; \
40405 }else{ \
40406 sqlite3_diskfull_pending--; \
40407 } \
40408 }
40409#else
40410#define SimulateIOErrorBenign(X)
40411#define SimulateIOError(A)
40412#define SimulateDiskfullError(A)
40413#endif /* defined(SQLITE_TEST) */
40414
40415/*
40416** When testing, keep a count of the number of open files.
40417*/
40418#if defined(SQLITE_TEST)
40419SQLITE_API extern int sqlite3_open_file_count;
40420#define OpenCounter(X) sqlite3_open_file_count+=(X)
40421#else
40422#define OpenCounter(X)
40423#endif /* defined(SQLITE_TEST) */
40424
40425#endif /* !defined(_OS_COMMON_H_) */
40426
40427/************** End of os_common.h *******************************************/
40428/************** Continuing where we left off in os_win.c *********************/
40429
40430/*
40431** Include the header file for the Windows VFS.
40432*/
40433/* #include "os_win.h" */
40434
40435/*
40436** Compiling and using WAL mode requires several APIs that are only
40437** available in Windows platforms based on the NT kernel.
40438*/
40439#if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
40440# error "WAL mode requires support from the Windows NT kernel, compile\
40441 with SQLITE_OMIT_WAL."
40442#endif
40443
40444#if !SQLITE_OS_WINNT && SQLITE_MAX_MMAP_SIZE>0
40445# error "Memory mapped files require support from the Windows NT kernel,\
40446 compile with SQLITE_MAX_MMAP_SIZE=0."
40447#endif
40448
40449/*
40450** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
40451** based on the sub-platform)?
40452*/
40453#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI)
40454# define SQLITE_WIN32_HAS_ANSI
40455#endif
40456
40457/*
40458** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions
40459** based on the sub-platform)?
40460*/
40461#if (SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT) && \
40462 !defined(SQLITE_WIN32_NO_WIDE)
40463# define SQLITE_WIN32_HAS_WIDE
40464#endif
40465
40466/*
40467** Make sure at least one set of Win32 APIs is available.
40468*/
40469#if !defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_WIN32_HAS_WIDE)
40470# error "At least one of SQLITE_WIN32_HAS_ANSI and SQLITE_WIN32_HAS_WIDE\
40471 must be defined."
40472#endif
40473
40474/*
40475** Define the required Windows SDK version constants if they are not
40476** already available.
40477*/
40478#ifndef NTDDI_WIN8
40479# define NTDDI_WIN8 0x06020000
40480#endif
40481
40482#ifndef NTDDI_WINBLUE
40483# define NTDDI_WINBLUE 0x06030000
40484#endif
40485
40486#ifndef NTDDI_WINTHRESHOLD
40487# define NTDDI_WINTHRESHOLD 0x06040000
40488#endif
40489
40490/*
40491** Check to see if the GetVersionEx[AW] functions are deprecated on the
40492** target system. GetVersionEx was first deprecated in Win8.1.
40493*/
40494#ifndef SQLITE_WIN32_GETVERSIONEX
40495# if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINBLUE
40496# define SQLITE_WIN32_GETVERSIONEX 0 /* GetVersionEx() is deprecated */
40497# else
40498# define SQLITE_WIN32_GETVERSIONEX 1 /* GetVersionEx() is current */
40499# endif
40500#endif
40501
40502/*
40503** Check to see if the CreateFileMappingA function is supported on the
40504** target system. It is unavailable when using "mincore.lib" on Win10.
40505** When compiling for Windows 10, always assume "mincore.lib" is in use.
40506*/
40507#ifndef SQLITE_WIN32_CREATEFILEMAPPINGA
40508# if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINTHRESHOLD
40509# define SQLITE_WIN32_CREATEFILEMAPPINGA 0
40510# else
40511# define SQLITE_WIN32_CREATEFILEMAPPINGA 1
40512# endif
40513#endif
40514
40515/*
40516** This constant should already be defined (in the "WinDef.h" SDK file).
40517*/
40518#ifndef MAX_PATH
40519# define MAX_PATH (260)
40520#endif
40521
40522/*
40523** Maximum pathname length (in chars) for Win32. This should normally be
40524** MAX_PATH.
40525*/
40526#ifndef SQLITE_WIN32_MAX_PATH_CHARS
40527# define SQLITE_WIN32_MAX_PATH_CHARS (MAX_PATH)
40528#endif
40529
40530/*
40531** This constant should already be defined (in the "WinNT.h" SDK file).
40532*/
40533#ifndef UNICODE_STRING_MAX_CHARS
40534# define UNICODE_STRING_MAX_CHARS (32767)
40535#endif
40536
40537/*
40538** Maximum pathname length (in chars) for WinNT. This should normally be
40539** UNICODE_STRING_MAX_CHARS.
40540*/
40541#ifndef SQLITE_WINNT_MAX_PATH_CHARS
40542# define SQLITE_WINNT_MAX_PATH_CHARS (UNICODE_STRING_MAX_CHARS)
40543#endif
40544
40545/*
40546** Maximum pathname length (in bytes) for Win32. The MAX_PATH macro is in
40547** characters, so we allocate 4 bytes per character assuming worst-case of
40548** 4-bytes-per-character for UTF8.
40549*/
40550#ifndef SQLITE_WIN32_MAX_PATH_BYTES
40551# define SQLITE_WIN32_MAX_PATH_BYTES (SQLITE_WIN32_MAX_PATH_CHARS*4)
40552#endif
40553
40554/*
40555** Maximum pathname length (in bytes) for WinNT. This should normally be
40556** UNICODE_STRING_MAX_CHARS * sizeof(WCHAR).
40557*/
40558#ifndef SQLITE_WINNT_MAX_PATH_BYTES
40559# define SQLITE_WINNT_MAX_PATH_BYTES \
40560 (sizeof(WCHAR) * SQLITE_WINNT_MAX_PATH_CHARS)
40561#endif
40562
40563/*
40564** Maximum error message length (in chars) for WinRT.
40565*/
40566#ifndef SQLITE_WIN32_MAX_ERRMSG_CHARS
40567# define SQLITE_WIN32_MAX_ERRMSG_CHARS (1024)
40568#endif
40569
40570/*
40571** Returns non-zero if the character should be treated as a directory
40572** separator.
40573*/
40574#ifndef winIsDirSep
40575# define winIsDirSep(a) (((a) == '/') || ((a) == '\\'))
40576#endif
40577
40578/*
40579** This macro is used when a local variable is set to a value that is
40580** [sometimes] not used by the code (e.g. via conditional compilation).
40581*/
40582#ifndef UNUSED_VARIABLE_VALUE
40583# define UNUSED_VARIABLE_VALUE(x) (void)(x)
40584#endif
40585
40586/*
40587** Returns the character that should be used as the directory separator.
40588*/
40589#ifndef winGetDirSep
40590# define winGetDirSep() '\\'
40591#endif
40592
40593/*
40594** Do we need to manually define the Win32 file mapping APIs for use with WAL
40595** mode or memory mapped files (e.g. these APIs are available in the Windows
40596** CE SDK; however, they are not present in the header file)?
40597*/
40598#if SQLITE_WIN32_FILEMAPPING_API && \
40599 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
40600/*
40601** Two of the file mapping APIs are different under WinRT. Figure out which
40602** set we need.
40603*/
40604#if SQLITE_OS_WINRT
40605WINBASEAPI HANDLE WINAPI CreateFileMappingFromApp(HANDLE, \
40606 LPSECURITY_ATTRIBUTES, ULONG, ULONG64, LPCWSTR);
40607
40608WINBASEAPI LPVOID WINAPI MapViewOfFileFromApp(HANDLE, ULONG, ULONG64, SIZE_T);
40609#else
40610#if defined(SQLITE_WIN32_HAS_ANSI)
40611WINBASEAPI HANDLE WINAPI CreateFileMappingA(HANDLE, LPSECURITY_ATTRIBUTES, \
40612 DWORD, DWORD, DWORD, LPCSTR);
40613#endif /* defined(SQLITE_WIN32_HAS_ANSI) */
40614
40615#if defined(SQLITE_WIN32_HAS_WIDE)
40616WINBASEAPI HANDLE WINAPI CreateFileMappingW(HANDLE, LPSECURITY_ATTRIBUTES, \
40617 DWORD, DWORD, DWORD, LPCWSTR);
40618#endif /* defined(SQLITE_WIN32_HAS_WIDE) */
40619
40620WINBASEAPI LPVOID WINAPI MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, SIZE_T);
40621#endif /* SQLITE_OS_WINRT */
40622
40623/*
40624** These file mapping APIs are common to both Win32 and WinRT.
40625*/
40626
40627WINBASEAPI BOOL WINAPI FlushViewOfFile(LPCVOID, SIZE_T);
40628WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
40629#endif /* SQLITE_WIN32_FILEMAPPING_API */
40630
40631/*
40632** Some Microsoft compilers lack this definition.
40633*/
40634#ifndef INVALID_FILE_ATTRIBUTES
40635# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
40636#endif
40637
40638#ifndef FILE_FLAG_MASK
40639# define FILE_FLAG_MASK (0xFF3C0000)
40640#endif
40641
40642#ifndef FILE_ATTRIBUTE_MASK
40643# define FILE_ATTRIBUTE_MASK (0x0003FFF7)
40644#endif
40645
40646#ifndef SQLITE_OMIT_WAL
40647/* Forward references to structures used for WAL */
40648typedef struct winShm winShm; /* A connection to shared-memory */
40649typedef struct winShmNode winShmNode; /* A region of shared-memory */
40650#endif
40651
40652/*
40653** WinCE lacks native support for file locking so we have to fake it
40654** with some code of our own.
40655*/
40656#if SQLITE_OS_WINCE
40657typedef struct winceLock {
40658 int nReaders; /* Number of reader locks obtained */
40659 BOOL bPending; /* Indicates a pending lock has been obtained */
40660 BOOL bReserved; /* Indicates a reserved lock has been obtained */
40661 BOOL bExclusive; /* Indicates an exclusive lock has been obtained */
40662} winceLock;
40663#endif
40664
40665/*
40666** The winFile structure is a subclass of sqlite3_file* specific to the win32
40667** portability layer.
40668*/
40669typedef struct winFile winFile;
40670struct winFile {
40671 const sqlite3_io_methods *pMethod; /*** Must be first ***/
40672 sqlite3_vfs *pVfs; /* The VFS used to open this file */
40673 HANDLE h; /* Handle for accessing the file */
40674 u8 locktype; /* Type of lock currently held on this file */
40675 short sharedLockByte; /* Randomly chosen byte used as a shared lock */
40676 u8 ctrlFlags; /* Flags. See WINFILE_* below */
40677 DWORD lastErrno; /* The Windows errno from the last I/O error */
40678#ifndef SQLITE_OMIT_WAL
40679 winShm *pShm; /* Instance of shared memory on this file */
40680#endif
40681 const char *zPath; /* Full pathname of this file */
40682 int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
40683#if SQLITE_OS_WINCE
40684 LPWSTR zDeleteOnClose; /* Name of file to delete when closing */
40685 HANDLE hMutex; /* Mutex used to control access to shared lock */
40686 HANDLE hShared; /* Shared memory segment used for locking */
40687 winceLock local; /* Locks obtained by this instance of winFile */
40688 winceLock *shared; /* Global shared lock memory for the file */
40689#endif
40690#if SQLITE_MAX_MMAP_SIZE>0
40691 int nFetchOut; /* Number of outstanding xFetch references */
40692 HANDLE hMap; /* Handle for accessing memory mapping */
40693 void *pMapRegion; /* Area memory mapped */
40694 sqlite3_int64 mmapSize; /* Size of mapped region */
40695 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
40696#endif
40697};
40698
40699/*
40700** The winVfsAppData structure is used for the pAppData member for all of the
40701** Win32 VFS variants.
40702*/
40703typedef struct winVfsAppData winVfsAppData;
40704struct winVfsAppData {
40705 const sqlite3_io_methods *pMethod; /* The file I/O methods to use. */
40706 void *pAppData; /* The extra pAppData, if any. */
40707 BOOL bNoLock; /* Non-zero if locking is disabled. */
40708};
40709
40710/*
40711** Allowed values for winFile.ctrlFlags
40712*/
40713#define WINFILE_RDONLY 0x02 /* Connection is read only */
40714#define WINFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
40715#define WINFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
40716
40717/*
40718 * The size of the buffer used by sqlite3_win32_write_debug().
40719 */
40720#ifndef SQLITE_WIN32_DBG_BUF_SIZE
40721# define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
40722#endif
40723
40724/*
40725 * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
40726 * various Win32 API heap functions instead of our own.
40727 */
40728#ifdef SQLITE_WIN32_MALLOC
40729
40730/*
40731 * If this is non-zero, an isolated heap will be created by the native Win32
40732 * allocator subsystem; otherwise, the default process heap will be used. This
40733 * setting has no effect when compiling for WinRT. By default, this is enabled
40734 * and an isolated heap will be created to store all allocated data.
40735 *
40736 ******************************************************************************
40737 * WARNING: It is important to note that when this setting is non-zero and the
40738 * winMemShutdown function is called (e.g. by the sqlite3_shutdown
40739 * function), all data that was allocated using the isolated heap will
40740 * be freed immediately and any attempt to access any of that freed
40741 * data will almost certainly result in an immediate access violation.
40742 ******************************************************************************
40743 */
40744#ifndef SQLITE_WIN32_HEAP_CREATE
40745# define SQLITE_WIN32_HEAP_CREATE (TRUE)
40746#endif
40747
40748/*
40749 * This is the maximum possible initial size of the Win32-specific heap, in
40750 * bytes.
40751 */
40752#ifndef SQLITE_WIN32_HEAP_MAX_INIT_SIZE
40753# define SQLITE_WIN32_HEAP_MAX_INIT_SIZE (4294967295U)
40754#endif
40755
40756/*
40757 * This is the extra space for the initial size of the Win32-specific heap,
40758 * in bytes. This value may be zero.
40759 */
40760#ifndef SQLITE_WIN32_HEAP_INIT_EXTRA
40761# define SQLITE_WIN32_HEAP_INIT_EXTRA (4194304)
40762#endif
40763
40764/*
40765 * Calculate the maximum legal cache size, in pages, based on the maximum
40766 * possible initial heap size and the default page size, setting aside the
40767 * needed extra space.
40768 */
40769#ifndef SQLITE_WIN32_MAX_CACHE_SIZE
40770# define SQLITE_WIN32_MAX_CACHE_SIZE (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \
40771 (SQLITE_WIN32_HEAP_INIT_EXTRA)) / \
40772 (SQLITE_DEFAULT_PAGE_SIZE))
40773#endif
40774
40775/*
40776 * This is cache size used in the calculation of the initial size of the
40777 * Win32-specific heap. It cannot be negative.
40778 */
40779#ifndef SQLITE_WIN32_CACHE_SIZE
40780# if SQLITE_DEFAULT_CACHE_SIZE>=0
40781# define SQLITE_WIN32_CACHE_SIZE (SQLITE_DEFAULT_CACHE_SIZE)
40782# else
40783# define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE))
40784# endif
40785#endif
40786
40787/*
40788 * Make sure that the calculated cache size, in pages, cannot cause the
40789 * initial size of the Win32-specific heap to exceed the maximum amount
40790 * of memory that can be specified in the call to HeapCreate.
40791 */
40792#if SQLITE_WIN32_CACHE_SIZE>SQLITE_WIN32_MAX_CACHE_SIZE
40793# undef SQLITE_WIN32_CACHE_SIZE
40794# define SQLITE_WIN32_CACHE_SIZE (2000)
40795#endif
40796
40797/*
40798 * The initial size of the Win32-specific heap. This value may be zero.
40799 */
40800#ifndef SQLITE_WIN32_HEAP_INIT_SIZE
40801# define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_WIN32_CACHE_SIZE) * \
40802 (SQLITE_DEFAULT_PAGE_SIZE) + \
40803 (SQLITE_WIN32_HEAP_INIT_EXTRA))
40804#endif
40805
40806/*
40807 * The maximum size of the Win32-specific heap. This value may be zero.
40808 */
40809#ifndef SQLITE_WIN32_HEAP_MAX_SIZE
40810# define SQLITE_WIN32_HEAP_MAX_SIZE (0)
40811#endif
40812
40813/*
40814 * The extra flags to use in calls to the Win32 heap APIs. This value may be
40815 * zero for the default behavior.
40816 */
40817#ifndef SQLITE_WIN32_HEAP_FLAGS
40818# define SQLITE_WIN32_HEAP_FLAGS (0)
40819#endif
40820
40821
40822/*
40823** The winMemData structure stores information required by the Win32-specific
40824** sqlite3_mem_methods implementation.
40825*/
40826typedef struct winMemData winMemData;
40827struct winMemData {
40828#ifndef NDEBUG
40829 u32 magic1; /* Magic number to detect structure corruption. */
40830#endif
40831 HANDLE hHeap; /* The handle to our heap. */
40832 BOOL bOwned; /* Do we own the heap (i.e. destroy it on shutdown)? */
40833#ifndef NDEBUG
40834 u32 magic2; /* Magic number to detect structure corruption. */
40835#endif
40836};
40837
40838#ifndef NDEBUG
40839#define WINMEM_MAGIC1 0x42b2830b
40840#define WINMEM_MAGIC2 0xbd4d7cf4
40841#endif
40842
40843static struct winMemData win_mem_data = {
40844#ifndef NDEBUG
40845 WINMEM_MAGIC1,
40846#endif
40847 NULL, FALSE
40848#ifndef NDEBUG
40849 ,WINMEM_MAGIC2
40850#endif
40851};
40852
40853#ifndef NDEBUG
40854#define winMemAssertMagic1() assert( win_mem_data.magic1==WINMEM_MAGIC1 )
40855#define winMemAssertMagic2() assert( win_mem_data.magic2==WINMEM_MAGIC2 )
40856#define winMemAssertMagic() winMemAssertMagic1(); winMemAssertMagic2();
40857#else
40858#define winMemAssertMagic()
40859#endif
40860
40861#define winMemGetDataPtr() &win_mem_data
40862#define winMemGetHeap() win_mem_data.hHeap
40863#define winMemGetOwned() win_mem_data.bOwned
40864
40865static void *winMemMalloc(int nBytes);
40866static void winMemFree(void *pPrior);
40867static void *winMemRealloc(void *pPrior, int nBytes);
40868static int winMemSize(void *p);
40869static int winMemRoundup(int n);
40870static int winMemInit(void *pAppData);
40871static void winMemShutdown(void *pAppData);
40872
40873SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void);
40874#endif /* SQLITE_WIN32_MALLOC */
40875
40876/*
40877** The following variable is (normally) set once and never changes
40878** thereafter. It records whether the operating system is Win9x
40879** or WinNT.
40880**
40881** 0: Operating system unknown.
40882** 1: Operating system is Win9x.
40883** 2: Operating system is WinNT.
40884**
40885** In order to facilitate testing on a WinNT system, the test fixture
40886** can manually set this value to 1 to emulate Win98 behavior.
40887*/
40888#ifdef SQLITE_TEST
40889SQLITE_API LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
40890#else
40891static LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
40892#endif
40893
40894#ifndef SYSCALL
40895# define SYSCALL sqlite3_syscall_ptr
40896#endif
40897
40898/*
40899** This function is not available on Windows CE or WinRT.
40900 */
40901
40902#if SQLITE_OS_WINCE || SQLITE_OS_WINRT
40903# define osAreFileApisANSI() 1
40904#endif
40905
40906/*
40907** Many system calls are accessed through pointer-to-functions so that
40908** they may be overridden at runtime to facilitate fault injection during
40909** testing and sandboxing. The following array holds the names and pointers
40910** to all overrideable system calls.
40911*/
40912static struct win_syscall {
40913 const char *zName; /* Name of the system call */
40914 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
40915 sqlite3_syscall_ptr pDefault; /* Default value */
40916} aSyscall[] = {
40917#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
40918 { "AreFileApisANSI", (SYSCALL)AreFileApisANSI, 0 },
40919#else
40920 { "AreFileApisANSI", (SYSCALL)0, 0 },
40921#endif
40922
40923#ifndef osAreFileApisANSI
40924#define osAreFileApisANSI ((BOOL(WINAPI*)(VOID))aSyscall[0].pCurrent)
40925#endif
40926
40927#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
40928 { "CharLowerW", (SYSCALL)CharLowerW, 0 },
40929#else
40930 { "CharLowerW", (SYSCALL)0, 0 },
40931#endif
40932
40933#define osCharLowerW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[1].pCurrent)
40934
40935#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
40936 { "CharUpperW", (SYSCALL)CharUpperW, 0 },
40937#else
40938 { "CharUpperW", (SYSCALL)0, 0 },
40939#endif
40940
40941#define osCharUpperW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[2].pCurrent)
40942
40943 { "CloseHandle", (SYSCALL)CloseHandle, 0 },
40944
40945#define osCloseHandle ((BOOL(WINAPI*)(HANDLE))aSyscall[3].pCurrent)
40946
40947#if defined(SQLITE_WIN32_HAS_ANSI)
40948 { "CreateFileA", (SYSCALL)CreateFileA, 0 },
40949#else
40950 { "CreateFileA", (SYSCALL)0, 0 },
40951#endif
40952
40953#define osCreateFileA ((HANDLE(WINAPI*)(LPCSTR,DWORD,DWORD, \
40954 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[4].pCurrent)
40955
40956#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
40957 { "CreateFileW", (SYSCALL)CreateFileW, 0 },
40958#else
40959 { "CreateFileW", (SYSCALL)0, 0 },
40960#endif
40961
40962#define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
40963 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
40964
40965#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
40966 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) && \
40967 SQLITE_WIN32_CREATEFILEMAPPINGA
40968 { "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 },
40969#else
40970 { "CreateFileMappingA", (SYSCALL)0, 0 },
40971#endif
40972
40973#define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
40974 DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent)
40975
40976#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
40977 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
40978 { "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 },
40979#else
40980 { "CreateFileMappingW", (SYSCALL)0, 0 },
40981#endif
40982
40983#define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
40984 DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent)
40985
40986#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
40987 { "CreateMutexW", (SYSCALL)CreateMutexW, 0 },
40988#else
40989 { "CreateMutexW", (SYSCALL)0, 0 },
40990#endif
40991
40992#define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \
40993 LPCWSTR))aSyscall[8].pCurrent)
40994
40995#if defined(SQLITE_WIN32_HAS_ANSI)
40996 { "DeleteFileA", (SYSCALL)DeleteFileA, 0 },
40997#else
40998 { "DeleteFileA", (SYSCALL)0, 0 },
40999#endif
41000
41001#define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent)
41002
41003#if defined(SQLITE_WIN32_HAS_WIDE)
41004 { "DeleteFileW", (SYSCALL)DeleteFileW, 0 },
41005#else
41006 { "DeleteFileW", (SYSCALL)0, 0 },
41007#endif
41008
41009#define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent)
41010
41011#if SQLITE_OS_WINCE
41012 { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 },
41013#else
41014 { "FileTimeToLocalFileTime", (SYSCALL)0, 0 },
41015#endif
41016
41017#define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \
41018 LPFILETIME))aSyscall[11].pCurrent)
41019
41020#if SQLITE_OS_WINCE
41021 { "FileTimeToSystemTime", (SYSCALL)FileTimeToSystemTime, 0 },
41022#else
41023 { "FileTimeToSystemTime", (SYSCALL)0, 0 },
41024#endif
41025
41026#define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \
41027 LPSYSTEMTIME))aSyscall[12].pCurrent)
41028
41029 { "FlushFileBuffers", (SYSCALL)FlushFileBuffers, 0 },
41030
41031#define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent)
41032
41033#if defined(SQLITE_WIN32_HAS_ANSI)
41034 { "FormatMessageA", (SYSCALL)FormatMessageA, 0 },
41035#else
41036 { "FormatMessageA", (SYSCALL)0, 0 },
41037#endif
41038
41039#define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \
41040 DWORD,va_list*))aSyscall[14].pCurrent)
41041
41042#if defined(SQLITE_WIN32_HAS_WIDE)
41043 { "FormatMessageW", (SYSCALL)FormatMessageW, 0 },
41044#else
41045 { "FormatMessageW", (SYSCALL)0, 0 },
41046#endif
41047
41048#define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \
41049 DWORD,va_list*))aSyscall[15].pCurrent)
41050
41051#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
41052 { "FreeLibrary", (SYSCALL)FreeLibrary, 0 },
41053#else
41054 { "FreeLibrary", (SYSCALL)0, 0 },
41055#endif
41056
41057#define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent)
41058
41059 { "GetCurrentProcessId", (SYSCALL)GetCurrentProcessId, 0 },
41060
41061#define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent)
41062
41063#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
41064 { "GetDiskFreeSpaceA", (SYSCALL)GetDiskFreeSpaceA, 0 },
41065#else
41066 { "GetDiskFreeSpaceA", (SYSCALL)0, 0 },
41067#endif
41068
41069#define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \
41070 LPDWORD))aSyscall[18].pCurrent)
41071
41072#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
41073 { "GetDiskFreeSpaceW", (SYSCALL)GetDiskFreeSpaceW, 0 },
41074#else
41075 { "GetDiskFreeSpaceW", (SYSCALL)0, 0 },
41076#endif
41077
41078#define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \
41079 LPDWORD))aSyscall[19].pCurrent)
41080
41081#if defined(SQLITE_WIN32_HAS_ANSI)
41082 { "GetFileAttributesA", (SYSCALL)GetFileAttributesA, 0 },
41083#else
41084 { "GetFileAttributesA", (SYSCALL)0, 0 },
41085#endif
41086
41087#define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent)
41088
41089#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
41090 { "GetFileAttributesW", (SYSCALL)GetFileAttributesW, 0 },
41091#else
41092 { "GetFileAttributesW", (SYSCALL)0, 0 },
41093#endif
41094
41095#define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent)
41096
41097#if defined(SQLITE_WIN32_HAS_WIDE)
41098 { "GetFileAttributesExW", (SYSCALL)GetFileAttributesExW, 0 },
41099#else
41100 { "GetFileAttributesExW", (SYSCALL)0, 0 },
41101#endif
41102
41103#define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \
41104 LPVOID))aSyscall[22].pCurrent)
41105
41106#if !SQLITE_OS_WINRT
41107 { "GetFileSize", (SYSCALL)GetFileSize, 0 },
41108#else
41109 { "GetFileSize", (SYSCALL)0, 0 },
41110#endif
41111
41112#define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent)
41113
41114#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
41115 { "GetFullPathNameA", (SYSCALL)GetFullPathNameA, 0 },
41116#else
41117 { "GetFullPathNameA", (SYSCALL)0, 0 },
41118#endif
41119
41120#define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \
41121 LPSTR*))aSyscall[24].pCurrent)
41122
41123#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
41124 { "GetFullPathNameW", (SYSCALL)GetFullPathNameW, 0 },
41125#else
41126 { "GetFullPathNameW", (SYSCALL)0, 0 },
41127#endif
41128
41129#define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
41130 LPWSTR*))aSyscall[25].pCurrent)
41131
41132 { "GetLastError", (SYSCALL)GetLastError, 0 },
41133
41134#define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
41135
41136#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
41137#if SQLITE_OS_WINCE
41138 /* The GetProcAddressA() routine is only available on Windows CE. */
41139 { "GetProcAddressA", (SYSCALL)GetProcAddressA, 0 },
41140#else
41141 /* All other Windows platforms expect GetProcAddress() to take
41142 ** an ANSI string regardless of the _UNICODE setting */
41143 { "GetProcAddressA", (SYSCALL)GetProcAddress, 0 },
41144#endif
41145#else
41146 { "GetProcAddressA", (SYSCALL)0, 0 },
41147#endif
41148
41149#define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \
41150 LPCSTR))aSyscall[27].pCurrent)
41151
41152#if !SQLITE_OS_WINRT
41153 { "GetSystemInfo", (SYSCALL)GetSystemInfo, 0 },
41154#else
41155 { "GetSystemInfo", (SYSCALL)0, 0 },
41156#endif
41157
41158#define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent)
41159
41160 { "GetSystemTime", (SYSCALL)GetSystemTime, 0 },
41161
41162#define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent)
41163
41164#if !SQLITE_OS_WINCE
41165 { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 },
41166#else
41167 { "GetSystemTimeAsFileTime", (SYSCALL)0, 0 },
41168#endif
41169
41170#define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \
41171 LPFILETIME))aSyscall[30].pCurrent)
41172
41173#if defined(SQLITE_WIN32_HAS_ANSI)
41174 { "GetTempPathA", (SYSCALL)GetTempPathA, 0 },
41175#else
41176 { "GetTempPathA", (SYSCALL)0, 0 },
41177#endif
41178
41179#define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent)
41180
41181#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
41182 { "GetTempPathW", (SYSCALL)GetTempPathW, 0 },
41183#else
41184 { "GetTempPathW", (SYSCALL)0, 0 },
41185#endif
41186
41187#define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent)
41188
41189#if !SQLITE_OS_WINRT
41190 { "GetTickCount", (SYSCALL)GetTickCount, 0 },
41191#else
41192 { "GetTickCount", (SYSCALL)0, 0 },
41193#endif
41194
41195#define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
41196
41197#if defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_GETVERSIONEX
41198 { "GetVersionExA", (SYSCALL)GetVersionExA, 0 },
41199#else
41200 { "GetVersionExA", (SYSCALL)0, 0 },
41201#endif
41202
41203#define osGetVersionExA ((BOOL(WINAPI*)( \
41204 LPOSVERSIONINFOA))aSyscall[34].pCurrent)
41205
41206#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
41207 SQLITE_WIN32_GETVERSIONEX
41208 { "GetVersionExW", (SYSCALL)GetVersionExW, 0 },
41209#else
41210 { "GetVersionExW", (SYSCALL)0, 0 },
41211#endif
41212
41213#define osGetVersionExW ((BOOL(WINAPI*)( \
41214 LPOSVERSIONINFOW))aSyscall[35].pCurrent)
41215
41216 { "HeapAlloc", (SYSCALL)HeapAlloc, 0 },
41217
41218#define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \
41219 SIZE_T))aSyscall[36].pCurrent)
41220
41221#if !SQLITE_OS_WINRT
41222 { "HeapCreate", (SYSCALL)HeapCreate, 0 },
41223#else
41224 { "HeapCreate", (SYSCALL)0, 0 },
41225#endif
41226
41227#define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \
41228 SIZE_T))aSyscall[37].pCurrent)
41229
41230#if !SQLITE_OS_WINRT
41231 { "HeapDestroy", (SYSCALL)HeapDestroy, 0 },
41232#else
41233 { "HeapDestroy", (SYSCALL)0, 0 },
41234#endif
41235
41236#define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[38].pCurrent)
41237
41238 { "HeapFree", (SYSCALL)HeapFree, 0 },
41239
41240#define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[39].pCurrent)
41241
41242 { "HeapReAlloc", (SYSCALL)HeapReAlloc, 0 },
41243
41244#define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \
41245 SIZE_T))aSyscall[40].pCurrent)
41246
41247 { "HeapSize", (SYSCALL)HeapSize, 0 },
41248
41249#define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \
41250 LPCVOID))aSyscall[41].pCurrent)
41251
41252#if !SQLITE_OS_WINRT
41253 { "HeapValidate", (SYSCALL)HeapValidate, 0 },
41254#else
41255 { "HeapValidate", (SYSCALL)0, 0 },
41256#endif
41257
41258#define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
41259 LPCVOID))aSyscall[42].pCurrent)
41260
41261#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
41262 { "HeapCompact", (SYSCALL)HeapCompact, 0 },
41263#else
41264 { "HeapCompact", (SYSCALL)0, 0 },
41265#endif
41266
41267#define osHeapCompact ((UINT(WINAPI*)(HANDLE,DWORD))aSyscall[43].pCurrent)
41268
41269#if defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
41270 { "LoadLibraryA", (SYSCALL)LoadLibraryA, 0 },
41271#else
41272 { "LoadLibraryA", (SYSCALL)0, 0 },
41273#endif
41274
41275#define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[44].pCurrent)
41276
41277#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
41278 !defined(SQLITE_OMIT_LOAD_EXTENSION)
41279 { "LoadLibraryW", (SYSCALL)LoadLibraryW, 0 },
41280#else
41281 { "LoadLibraryW", (SYSCALL)0, 0 },
41282#endif
41283
41284#define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[45].pCurrent)
41285
41286#if !SQLITE_OS_WINRT
41287 { "LocalFree", (SYSCALL)LocalFree, 0 },
41288#else
41289 { "LocalFree", (SYSCALL)0, 0 },
41290#endif
41291
41292#define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[46].pCurrent)
41293
41294#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
41295 { "LockFile", (SYSCALL)LockFile, 0 },
41296#else
41297 { "LockFile", (SYSCALL)0, 0 },
41298#endif
41299
41300#ifndef osLockFile
41301#define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
41302 DWORD))aSyscall[47].pCurrent)
41303#endif
41304
41305#if !SQLITE_OS_WINCE
41306 { "LockFileEx", (SYSCALL)LockFileEx, 0 },
41307#else
41308 { "LockFileEx", (SYSCALL)0, 0 },
41309#endif
41310
41311#ifndef osLockFileEx
41312#define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
41313 LPOVERLAPPED))aSyscall[48].pCurrent)
41314#endif
41315
41316#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && \
41317 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
41318 { "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 },
41319#else
41320 { "MapViewOfFile", (SYSCALL)0, 0 },
41321#endif
41322
41323#define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
41324 SIZE_T))aSyscall[49].pCurrent)
41325
41326 { "MultiByteToWideChar", (SYSCALL)MultiByteToWideChar, 0 },
41327
41328#define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
41329 int))aSyscall[50].pCurrent)
41330
41331 { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
41332
41333#define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
41334 LARGE_INTEGER*))aSyscall[51].pCurrent)
41335
41336 { "ReadFile", (SYSCALL)ReadFile, 0 },
41337
41338#define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
41339 LPOVERLAPPED))aSyscall[52].pCurrent)
41340
41341 { "SetEndOfFile", (SYSCALL)SetEndOfFile, 0 },
41342
41343#define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[53].pCurrent)
41344
41345#if !SQLITE_OS_WINRT
41346 { "SetFilePointer", (SYSCALL)SetFilePointer, 0 },
41347#else
41348 { "SetFilePointer", (SYSCALL)0, 0 },
41349#endif
41350
41351#define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
41352 DWORD))aSyscall[54].pCurrent)
41353
41354#if !SQLITE_OS_WINRT
41355 { "Sleep", (SYSCALL)Sleep, 0 },
41356#else
41357 { "Sleep", (SYSCALL)0, 0 },
41358#endif
41359
41360#define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[55].pCurrent)
41361
41362 { "SystemTimeToFileTime", (SYSCALL)SystemTimeToFileTime, 0 },
41363
41364#define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
41365 LPFILETIME))aSyscall[56].pCurrent)
41366
41367#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
41368 { "UnlockFile", (SYSCALL)UnlockFile, 0 },
41369#else
41370 { "UnlockFile", (SYSCALL)0, 0 },
41371#endif
41372
41373#ifndef osUnlockFile
41374#define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
41375 DWORD))aSyscall[57].pCurrent)
41376#endif
41377
41378#if !SQLITE_OS_WINCE
41379 { "UnlockFileEx", (SYSCALL)UnlockFileEx, 0 },
41380#else
41381 { "UnlockFileEx", (SYSCALL)0, 0 },
41382#endif
41383
41384#define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
41385 LPOVERLAPPED))aSyscall[58].pCurrent)
41386
41387#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
41388 { "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 },
41389#else
41390 { "UnmapViewOfFile", (SYSCALL)0, 0 },
41391#endif
41392
41393#define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[59].pCurrent)
41394
41395 { "WideCharToMultiByte", (SYSCALL)WideCharToMultiByte, 0 },
41396
41397#define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
41398 LPCSTR,LPBOOL))aSyscall[60].pCurrent)
41399
41400 { "WriteFile", (SYSCALL)WriteFile, 0 },
41401
41402#define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
41403 LPOVERLAPPED))aSyscall[61].pCurrent)
41404
41405#if SQLITE_OS_WINRT
41406 { "CreateEventExW", (SYSCALL)CreateEventExW, 0 },
41407#else
41408 { "CreateEventExW", (SYSCALL)0, 0 },
41409#endif
41410
41411#define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
41412 DWORD,DWORD))aSyscall[62].pCurrent)
41413
41414#if !SQLITE_OS_WINRT
41415 { "WaitForSingleObject", (SYSCALL)WaitForSingleObject, 0 },
41416#else
41417 { "WaitForSingleObject", (SYSCALL)0, 0 },
41418#endif
41419
41420#define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
41421 DWORD))aSyscall[63].pCurrent)
41422
41423#if !SQLITE_OS_WINCE
41424 { "WaitForSingleObjectEx", (SYSCALL)WaitForSingleObjectEx, 0 },
41425#else
41426 { "WaitForSingleObjectEx", (SYSCALL)0, 0 },
41427#endif
41428
41429#define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \
41430 BOOL))aSyscall[64].pCurrent)
41431
41432#if SQLITE_OS_WINRT
41433 { "SetFilePointerEx", (SYSCALL)SetFilePointerEx, 0 },
41434#else
41435 { "SetFilePointerEx", (SYSCALL)0, 0 },
41436#endif
41437
41438#define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \
41439 PLARGE_INTEGER,DWORD))aSyscall[65].pCurrent)
41440
41441#if SQLITE_OS_WINRT
41442 { "GetFileInformationByHandleEx", (SYSCALL)GetFileInformationByHandleEx, 0 },
41443#else
41444 { "GetFileInformationByHandleEx", (SYSCALL)0, 0 },
41445#endif
41446
41447#define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
41448 FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent)
41449
41450#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
41451 { "MapViewOfFileFromApp", (SYSCALL)MapViewOfFileFromApp, 0 },
41452#else
41453 { "MapViewOfFileFromApp", (SYSCALL)0, 0 },
41454#endif
41455
41456#define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \
41457 SIZE_T))aSyscall[67].pCurrent)
41458
41459#if SQLITE_OS_WINRT
41460 { "CreateFile2", (SYSCALL)CreateFile2, 0 },
41461#else
41462 { "CreateFile2", (SYSCALL)0, 0 },
41463#endif
41464
41465#define osCreateFile2 ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD,DWORD, \
41466 LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[68].pCurrent)
41467
41468#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_LOAD_EXTENSION)
41469 { "LoadPackagedLibrary", (SYSCALL)LoadPackagedLibrary, 0 },
41470#else
41471 { "LoadPackagedLibrary", (SYSCALL)0, 0 },
41472#endif
41473
41474#define osLoadPackagedLibrary ((HMODULE(WINAPI*)(LPCWSTR, \
41475 DWORD))aSyscall[69].pCurrent)
41476
41477#if SQLITE_OS_WINRT
41478 { "GetTickCount64", (SYSCALL)GetTickCount64, 0 },
41479#else
41480 { "GetTickCount64", (SYSCALL)0, 0 },
41481#endif
41482
41483#define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[70].pCurrent)
41484
41485#if SQLITE_OS_WINRT
41486 { "GetNativeSystemInfo", (SYSCALL)GetNativeSystemInfo, 0 },
41487#else
41488 { "GetNativeSystemInfo", (SYSCALL)0, 0 },
41489#endif
41490
41491#define osGetNativeSystemInfo ((VOID(WINAPI*)( \
41492 LPSYSTEM_INFO))aSyscall[71].pCurrent)
41493
41494#if defined(SQLITE_WIN32_HAS_ANSI)
41495 { "OutputDebugStringA", (SYSCALL)OutputDebugStringA, 0 },
41496#else
41497 { "OutputDebugStringA", (SYSCALL)0, 0 },
41498#endif
41499
41500#define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[72].pCurrent)
41501
41502#if defined(SQLITE_WIN32_HAS_WIDE)
41503 { "OutputDebugStringW", (SYSCALL)OutputDebugStringW, 0 },
41504#else
41505 { "OutputDebugStringW", (SYSCALL)0, 0 },
41506#endif
41507
41508#define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[73].pCurrent)
41509
41510 { "GetProcessHeap", (SYSCALL)GetProcessHeap, 0 },
41511
41512#define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent)
41513
41514#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
41515 { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
41516#else
41517 { "CreateFileMappingFromApp", (SYSCALL)0, 0 },
41518#endif
41519
41520#define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
41521 LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)
41522
41523/*
41524** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
41525** is really just a macro that uses a compiler intrinsic (e.g. x64).
41526** So do not try to make this is into a redefinable interface.
41527*/
41528#if defined(InterlockedCompareExchange)
41529 { "InterlockedCompareExchange", (SYSCALL)0, 0 },
41530
41531#define osInterlockedCompareExchange InterlockedCompareExchange
41532#else
41533 { "InterlockedCompareExchange", (SYSCALL)InterlockedCompareExchange, 0 },
41534
41535#define osInterlockedCompareExchange ((LONG(WINAPI*)(LONG \
41536 SQLITE_WIN32_VOLATILE*, LONG,LONG))aSyscall[76].pCurrent)
41537#endif /* defined(InterlockedCompareExchange) */
41538
41539#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
41540 { "UuidCreate", (SYSCALL)UuidCreate, 0 },
41541#else
41542 { "UuidCreate", (SYSCALL)0, 0 },
41543#endif
41544
41545#define osUuidCreate ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[77].pCurrent)
41546
41547#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
41548 { "UuidCreateSequential", (SYSCALL)UuidCreateSequential, 0 },
41549#else
41550 { "UuidCreateSequential", (SYSCALL)0, 0 },
41551#endif
41552
41553#define osUuidCreateSequential \
41554 ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[78].pCurrent)
41555
41556#if !defined(SQLITE_NO_SYNC) && SQLITE_MAX_MMAP_SIZE>0
41557 { "FlushViewOfFile", (SYSCALL)FlushViewOfFile, 0 },
41558#else
41559 { "FlushViewOfFile", (SYSCALL)0, 0 },
41560#endif
41561
41562#define osFlushViewOfFile \
41563 ((BOOL(WINAPI*)(LPCVOID,SIZE_T))aSyscall[79].pCurrent)
41564
41565}; /* End of the overrideable system calls */
41566
41567/*
41568** This is the xSetSystemCall() method of sqlite3_vfs for all of the
41569** "win32" VFSes. Return SQLITE_OK opon successfully updating the
41570** system call pointer, or SQLITE_NOTFOUND if there is no configurable
41571** system call named zName.
41572*/
41573static int winSetSystemCall(
41574 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
41575 const char *zName, /* Name of system call to override */
41576 sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
41577){
41578 unsigned int i;
41579 int rc = SQLITE_NOTFOUND;
41580
41581 UNUSED_PARAMETER(pNotUsed);
41582 if( zName==0 ){
41583 /* If no zName is given, restore all system calls to their default
41584 ** settings and return NULL
41585 */
41586 rc = SQLITE_OK;
41587 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
41588 if( aSyscall[i].pDefault ){
41589 aSyscall[i].pCurrent = aSyscall[i].pDefault;
41590 }
41591 }
41592 }else{
41593 /* If zName is specified, operate on only the one system call
41594 ** specified.
41595 */
41596 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
41597 if( strcmp(zName, aSyscall[i].zName)==0 ){
41598 if( aSyscall[i].pDefault==0 ){
41599 aSyscall[i].pDefault = aSyscall[i].pCurrent;
41600 }
41601 rc = SQLITE_OK;
41602 if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
41603 aSyscall[i].pCurrent = pNewFunc;
41604 break;
41605 }
41606 }
41607 }
41608 return rc;
41609}
41610
41611/*
41612** Return the value of a system call. Return NULL if zName is not a
41613** recognized system call name. NULL is also returned if the system call
41614** is currently undefined.
41615*/
41616static sqlite3_syscall_ptr winGetSystemCall(
41617 sqlite3_vfs *pNotUsed,
41618 const char *zName
41619){
41620 unsigned int i;
41621
41622 UNUSED_PARAMETER(pNotUsed);
41623 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
41624 if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
41625 }
41626 return 0;
41627}
41628
41629/*
41630** Return the name of the first system call after zName. If zName==NULL
41631** then return the name of the first system call. Return NULL if zName
41632** is the last system call or if zName is not the name of a valid
41633** system call.
41634*/
41635static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){
41636 int i = -1;
41637
41638 UNUSED_PARAMETER(p);
41639 if( zName ){
41640 for(i=0; i<ArraySize(aSyscall)-1; i++){
41641 if( strcmp(zName, aSyscall[i].zName)==0 ) break;
41642 }
41643 }
41644 for(i++; i<ArraySize(aSyscall); i++){
41645 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
41646 }
41647 return 0;
41648}
41649
41650#ifdef SQLITE_WIN32_MALLOC
41651/*
41652** If a Win32 native heap has been configured, this function will attempt to
41653** compact it. Upon success, SQLITE_OK will be returned. Upon failure, one
41654** of SQLITE_NOMEM, SQLITE_ERROR, or SQLITE_NOTFOUND will be returned. The
41655** "pnLargest" argument, if non-zero, will be used to return the size of the
41656** largest committed free block in the heap, in bytes.
41657*/
41658SQLITE_API int sqlite3_win32_compact_heap(LPUINT pnLargest){
41659 int rc = SQLITE_OK;
41660 UINT nLargest = 0;
41661 HANDLE hHeap;
41662
41663 winMemAssertMagic();
41664 hHeap = winMemGetHeap();
41665 assert( hHeap!=0 );
41666 assert( hHeap!=INVALID_HANDLE_VALUE );
41667#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
41668 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
41669#endif
41670#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
41671 if( (nLargest=osHeapCompact(hHeap, SQLITE_WIN32_HEAP_FLAGS))==0 ){
41672 DWORD lastErrno = osGetLastError();
41673 if( lastErrno==NO_ERROR ){
41674 sqlite3_log(SQLITE_NOMEM, "failed to HeapCompact (no space), heap=%p",
41675 (void*)hHeap);
41676 rc = SQLITE_NOMEM_BKPT;
41677 }else{
41678 sqlite3_log(SQLITE_ERROR, "failed to HeapCompact (%lu), heap=%p",
41679 osGetLastError(), (void*)hHeap);
41680 rc = SQLITE_ERROR;
41681 }
41682 }
41683#else
41684 sqlite3_log(SQLITE_NOTFOUND, "failed to HeapCompact, heap=%p",
41685 (void*)hHeap);
41686 rc = SQLITE_NOTFOUND;
41687#endif
41688 if( pnLargest ) *pnLargest = nLargest;
41689 return rc;
41690}
41691
41692/*
41693** If a Win32 native heap has been configured, this function will attempt to
41694** destroy and recreate it. If the Win32 native heap is not isolated and/or
41695** the sqlite3_memory_used() function does not return zero, SQLITE_BUSY will
41696** be returned and no changes will be made to the Win32 native heap.
41697*/
41698SQLITE_API int sqlite3_win32_reset_heap(){
41699 int rc;
41700 MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */
41701 MUTEX_LOGIC( sqlite3_mutex *pMem; ) /* The memsys static mutex */
41702 MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
41703 MUTEX_LOGIC( pMem = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); )
41704 sqlite3_mutex_enter(pMaster);
41705 sqlite3_mutex_enter(pMem);
41706 winMemAssertMagic();
41707 if( winMemGetHeap()!=NULL && winMemGetOwned() && sqlite3_memory_used()==0 ){
41708 /*
41709 ** At this point, there should be no outstanding memory allocations on
41710 ** the heap. Also, since both the master and memsys locks are currently
41711 ** being held by us, no other function (i.e. from another thread) should
41712 ** be able to even access the heap. Attempt to destroy and recreate our
41713 ** isolated Win32 native heap now.
41714 */
41715 assert( winMemGetHeap()!=NULL );
41716 assert( winMemGetOwned() );
41717 assert( sqlite3_memory_used()==0 );
41718 winMemShutdown(winMemGetDataPtr());
41719 assert( winMemGetHeap()==NULL );
41720 assert( !winMemGetOwned() );
41721 assert( sqlite3_memory_used()==0 );
41722 rc = winMemInit(winMemGetDataPtr());
41723 assert( rc!=SQLITE_OK || winMemGetHeap()!=NULL );
41724 assert( rc!=SQLITE_OK || winMemGetOwned() );
41725 assert( rc!=SQLITE_OK || sqlite3_memory_used()==0 );
41726 }else{
41727 /*
41728 ** The Win32 native heap cannot be modified because it may be in use.
41729 */
41730 rc = SQLITE_BUSY;
41731 }
41732 sqlite3_mutex_leave(pMem);
41733 sqlite3_mutex_leave(pMaster);
41734 return rc;
41735}
41736#endif /* SQLITE_WIN32_MALLOC */
41737
41738/*
41739** This function outputs the specified (ANSI) string to the Win32 debugger
41740** (if available).
41741*/
41742
41743SQLITE_API void sqlite3_win32_write_debug(const char *zBuf, int nBuf){
41744 char zDbgBuf[SQLITE_WIN32_DBG_BUF_SIZE];
41745 int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
41746 if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
41747 assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
41748#ifdef SQLITE_ENABLE_API_ARMOR
41749 if( !zBuf ){
41750 (void)SQLITE_MISUSE_BKPT;
41751 return;
41752 }
41753#endif
41754#if defined(SQLITE_WIN32_HAS_ANSI)
41755 if( nMin>0 ){
41756 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
41757 memcpy(zDbgBuf, zBuf, nMin);
41758 osOutputDebugStringA(zDbgBuf);
41759 }else{
41760 osOutputDebugStringA(zBuf);
41761 }
41762#elif defined(SQLITE_WIN32_HAS_WIDE)
41763 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
41764 if ( osMultiByteToWideChar(
41765 osAreFileApisANSI() ? CP_ACP : CP_OEMCP, 0, zBuf,
41766 nMin, (LPWSTR)zDbgBuf, SQLITE_WIN32_DBG_BUF_SIZE/sizeof(WCHAR))<=0 ){
41767 return;
41768 }
41769 osOutputDebugStringW((LPCWSTR)zDbgBuf);
41770#else
41771 if( nMin>0 ){
41772 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
41773 memcpy(zDbgBuf, zBuf, nMin);
41774 fprintf(stderr, "%s", zDbgBuf);
41775 }else{
41776 fprintf(stderr, "%s", zBuf);
41777 }
41778#endif
41779}
41780
41781/*
41782** The following routine suspends the current thread for at least ms
41783** milliseconds. This is equivalent to the Win32 Sleep() interface.
41784*/
41785#if SQLITE_OS_WINRT
41786static HANDLE sleepObj = NULL;
41787#endif
41788
41789SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds){
41790#if SQLITE_OS_WINRT
41791 if ( sleepObj==NULL ){
41792 sleepObj = osCreateEventExW(NULL, NULL, CREATE_EVENT_MANUAL_RESET,
41793 SYNCHRONIZE);
41794 }
41795 assert( sleepObj!=NULL );
41796 osWaitForSingleObjectEx(sleepObj, milliseconds, FALSE);
41797#else
41798 osSleep(milliseconds);
41799#endif
41800}
41801
41802#if SQLITE_MAX_WORKER_THREADS>0 && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
41803 SQLITE_THREADSAFE>0
41804SQLITE_PRIVATE DWORD sqlite3Win32Wait(HANDLE hObject){
41805 DWORD rc;
41806 while( (rc = osWaitForSingleObjectEx(hObject, INFINITE,
41807 TRUE))==WAIT_IO_COMPLETION ){}
41808 return rc;
41809}
41810#endif
41811
41812/*
41813** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
41814** or WinCE. Return false (zero) for Win95, Win98, or WinME.
41815**
41816** Here is an interesting observation: Win95, Win98, and WinME lack
41817** the LockFileEx() API. But we can still statically link against that
41818** API as long as we don't call it when running Win95/98/ME. A call to
41819** this routine is used to determine if the host is Win95/98/ME or
41820** WinNT/2K/XP so that we will know whether or not we can safely call
41821** the LockFileEx() API.
41822*/
41823
41824#if !SQLITE_WIN32_GETVERSIONEX
41825# define osIsNT() (1)
41826#elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
41827# define osIsNT() (1)
41828#elif !defined(SQLITE_WIN32_HAS_WIDE)
41829# define osIsNT() (0)
41830#else
41831# define osIsNT() ((sqlite3_os_type==2) || sqlite3_win32_is_nt())
41832#endif
41833
41834/*
41835** This function determines if the machine is running a version of Windows
41836** based on the NT kernel.
41837*/
41838SQLITE_API int sqlite3_win32_is_nt(void){
41839#if SQLITE_OS_WINRT
41840 /*
41841 ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
41842 ** kernel.
41843 */
41844 return 1;
41845#elif SQLITE_WIN32_GETVERSIONEX
41846 if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){
41847#if defined(SQLITE_WIN32_HAS_ANSI)
41848 OSVERSIONINFOA sInfo;
41849 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
41850 osGetVersionExA(&sInfo);
41851 osInterlockedCompareExchange(&sqlite3_os_type,
41852 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
41853#elif defined(SQLITE_WIN32_HAS_WIDE)
41854 OSVERSIONINFOW sInfo;
41855 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
41856 osGetVersionExW(&sInfo);
41857 osInterlockedCompareExchange(&sqlite3_os_type,
41858 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
41859#endif
41860 }
41861 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
41862#elif SQLITE_TEST
41863 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
41864#else
41865 /*
41866 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
41867 ** deprecated are always assumed to be based on the NT kernel.
41868 */
41869 return 1;
41870#endif
41871}
41872
41873#ifdef SQLITE_WIN32_MALLOC
41874/*
41875** Allocate nBytes of memory.
41876*/
41877static void *winMemMalloc(int nBytes){
41878 HANDLE hHeap;
41879 void *p;
41880
41881 winMemAssertMagic();
41882 hHeap = winMemGetHeap();
41883 assert( hHeap!=0 );
41884 assert( hHeap!=INVALID_HANDLE_VALUE );
41885#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
41886 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
41887#endif
41888 assert( nBytes>=0 );
41889 p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
41890 if( !p ){
41891 sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%lu), heap=%p",
41892 nBytes, osGetLastError(), (void*)hHeap);
41893 }
41894 return p;
41895}
41896
41897/*
41898** Free memory.
41899*/
41900static void winMemFree(void *pPrior){
41901 HANDLE hHeap;
41902
41903 winMemAssertMagic();
41904 hHeap = winMemGetHeap();
41905 assert( hHeap!=0 );
41906 assert( hHeap!=INVALID_HANDLE_VALUE );
41907#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
41908 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
41909#endif
41910 if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
41911 if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
41912 sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%lu), heap=%p",
41913 pPrior, osGetLastError(), (void*)hHeap);
41914 }
41915}
41916
41917/*
41918** Change the size of an existing memory allocation
41919*/
41920static void *winMemRealloc(void *pPrior, int nBytes){
41921 HANDLE hHeap;
41922 void *p;
41923
41924 winMemAssertMagic();
41925 hHeap = winMemGetHeap();
41926 assert( hHeap!=0 );
41927 assert( hHeap!=INVALID_HANDLE_VALUE );
41928#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
41929 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
41930#endif
41931 assert( nBytes>=0 );
41932 if( !pPrior ){
41933 p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
41934 }else{
41935 p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
41936 }
41937 if( !p ){
41938 sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%lu), heap=%p",
41939 pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, osGetLastError(),
41940 (void*)hHeap);
41941 }
41942 return p;
41943}
41944
41945/*
41946** Return the size of an outstanding allocation, in bytes.
41947*/
41948static int winMemSize(void *p){
41949 HANDLE hHeap;
41950 SIZE_T n;
41951
41952 winMemAssertMagic();
41953 hHeap = winMemGetHeap();
41954 assert( hHeap!=0 );
41955 assert( hHeap!=INVALID_HANDLE_VALUE );
41956#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
41957 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, p) );
41958#endif
41959 if( !p ) return 0;
41960 n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
41961 if( n==(SIZE_T)-1 ){
41962 sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%lu), heap=%p",
41963 p, osGetLastError(), (void*)hHeap);
41964 return 0;
41965 }
41966 return (int)n;
41967}
41968
41969/*
41970** Round up a request size to the next valid allocation size.
41971*/
41972static int winMemRoundup(int n){
41973 return n;
41974}
41975
41976/*
41977** Initialize this module.
41978*/
41979static int winMemInit(void *pAppData){
41980 winMemData *pWinMemData = (winMemData *)pAppData;
41981
41982 if( !pWinMemData ) return SQLITE_ERROR;
41983 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
41984 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
41985
41986#if !SQLITE_OS_WINRT && SQLITE_WIN32_HEAP_CREATE
41987 if( !pWinMemData->hHeap ){
41988 DWORD dwInitialSize = SQLITE_WIN32_HEAP_INIT_SIZE;
41989 DWORD dwMaximumSize = (DWORD)sqlite3GlobalConfig.nHeap;
41990 if( dwMaximumSize==0 ){
41991 dwMaximumSize = SQLITE_WIN32_HEAP_MAX_SIZE;
41992 }else if( dwInitialSize>dwMaximumSize ){
41993 dwInitialSize = dwMaximumSize;
41994 }
41995 pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
41996 dwInitialSize, dwMaximumSize);
41997 if( !pWinMemData->hHeap ){
41998 sqlite3_log(SQLITE_NOMEM,
41999 "failed to HeapCreate (%lu), flags=%u, initSize=%lu, maxSize=%lu",
42000 osGetLastError(), SQLITE_WIN32_HEAP_FLAGS, dwInitialSize,
42001 dwMaximumSize);
42002 return SQLITE_NOMEM_BKPT;
42003 }
42004 pWinMemData->bOwned = TRUE;
42005 assert( pWinMemData->bOwned );
42006 }
42007#else
42008 pWinMemData->hHeap = osGetProcessHeap();
42009 if( !pWinMemData->hHeap ){
42010 sqlite3_log(SQLITE_NOMEM,
42011 "failed to GetProcessHeap (%lu)", osGetLastError());
42012 return SQLITE_NOMEM_BKPT;
42013 }
42014 pWinMemData->bOwned = FALSE;
42015 assert( !pWinMemData->bOwned );
42016#endif
42017 assert( pWinMemData->hHeap!=0 );
42018 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
42019#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
42020 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
42021#endif
42022 return SQLITE_OK;
42023}
42024
42025/*
42026** Deinitialize this module.
42027*/
42028static void winMemShutdown(void *pAppData){
42029 winMemData *pWinMemData = (winMemData *)pAppData;
42030
42031 if( !pWinMemData ) return;
42032 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
42033 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
42034
42035 if( pWinMemData->hHeap ){
42036 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
42037#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
42038 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
42039#endif
42040 if( pWinMemData->bOwned ){
42041 if( !osHeapDestroy(pWinMemData->hHeap) ){
42042 sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%lu), heap=%p",
42043 osGetLastError(), (void*)pWinMemData->hHeap);
42044 }
42045 pWinMemData->bOwned = FALSE;
42046 }
42047 pWinMemData->hHeap = NULL;
42048 }
42049}
42050
42051/*
42052** Populate the low-level memory allocation function pointers in
42053** sqlite3GlobalConfig.m with pointers to the routines in this file. The
42054** arguments specify the block of memory to manage.
42055**
42056** This routine is only called by sqlite3_config(), and therefore
42057** is not required to be threadsafe (it is not).
42058*/
42059SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void){
42060 static const sqlite3_mem_methods winMemMethods = {
42061 winMemMalloc,
42062 winMemFree,
42063 winMemRealloc,
42064 winMemSize,
42065 winMemRoundup,
42066 winMemInit,
42067 winMemShutdown,
42068 &win_mem_data
42069 };
42070 return &winMemMethods;
42071}
42072
42073SQLITE_PRIVATE void sqlite3MemSetDefault(void){
42074 sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
42075}
42076#endif /* SQLITE_WIN32_MALLOC */
42077
42078/*
42079** Convert a UTF-8 string to Microsoft Unicode.
42080**
42081** Space to hold the returned string is obtained from sqlite3_malloc().
42082*/
42083static LPWSTR winUtf8ToUnicode(const char *zText){
42084 int nChar;
42085 LPWSTR zWideText;
42086
42087 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0);
42088 if( nChar==0 ){
42089 return 0;
42090 }
42091 zWideText = sqlite3MallocZero( nChar*sizeof(WCHAR) );
42092 if( zWideText==0 ){
42093 return 0;
42094 }
42095 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
42096 nChar);
42097 if( nChar==0 ){
42098 sqlite3_free(zWideText);
42099 zWideText = 0;
42100 }
42101 return zWideText;
42102}
42103
42104/*
42105** Convert a Microsoft Unicode string to UTF-8.
42106**
42107** Space to hold the returned string is obtained from sqlite3_malloc().
42108*/
42109static char *winUnicodeToUtf8(LPCWSTR zWideText){
42110 int nByte;
42111 char *zText;
42112
42113 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
42114 if( nByte == 0 ){
42115 return 0;
42116 }
42117 zText = sqlite3MallocZero( nByte );
42118 if( zText==0 ){
42119 return 0;
42120 }
42121 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
42122 0, 0);
42123 if( nByte == 0 ){
42124 sqlite3_free(zText);
42125 zText = 0;
42126 }
42127 return zText;
42128}
42129
42130/*
42131** Convert an ANSI string to Microsoft Unicode, using the ANSI or OEM
42132** code page.
42133**
42134** Space to hold the returned string is obtained from sqlite3_malloc().
42135*/
42136static LPWSTR winMbcsToUnicode(const char *zText, int useAnsi){
42137 int nByte;
42138 LPWSTR zMbcsText;
42139 int codepage = useAnsi ? CP_ACP : CP_OEMCP;
42140
42141 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
42142 0)*sizeof(WCHAR);
42143 if( nByte==0 ){
42144 return 0;
42145 }
42146 zMbcsText = sqlite3MallocZero( nByte*sizeof(WCHAR) );
42147 if( zMbcsText==0 ){
42148 return 0;
42149 }
42150 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
42151 nByte);
42152 if( nByte==0 ){
42153 sqlite3_free(zMbcsText);
42154 zMbcsText = 0;
42155 }
42156 return zMbcsText;
42157}
42158
42159/*
42160** Convert a Microsoft Unicode string to a multi-byte character string,
42161** using the ANSI or OEM code page.
42162**
42163** Space to hold the returned string is obtained from sqlite3_malloc().
42164*/
42165static char *winUnicodeToMbcs(LPCWSTR zWideText, int useAnsi){
42166 int nByte;
42167 char *zText;
42168 int codepage = useAnsi ? CP_ACP : CP_OEMCP;
42169
42170 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
42171 if( nByte == 0 ){
42172 return 0;
42173 }
42174 zText = sqlite3MallocZero( nByte );
42175 if( zText==0 ){
42176 return 0;
42177 }
42178 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
42179 nByte, 0, 0);
42180 if( nByte == 0 ){
42181 sqlite3_free(zText);
42182 zText = 0;
42183 }
42184 return zText;
42185}
42186
42187/*
42188** Convert a multi-byte character string to UTF-8.
42189**
42190** Space to hold the returned string is obtained from sqlite3_malloc().
42191*/
42192static char *winMbcsToUtf8(const char *zText, int useAnsi){
42193 char *zTextUtf8;
42194 LPWSTR zTmpWide;
42195
42196 zTmpWide = winMbcsToUnicode(zText, useAnsi);
42197 if( zTmpWide==0 ){
42198 return 0;
42199 }
42200 zTextUtf8 = winUnicodeToUtf8(zTmpWide);
42201 sqlite3_free(zTmpWide);
42202 return zTextUtf8;
42203}
42204
42205/*
42206** Convert a UTF-8 string to a multi-byte character string.
42207**
42208** Space to hold the returned string is obtained from sqlite3_malloc().
42209*/
42210static char *winUtf8ToMbcs(const char *zText, int useAnsi){
42211 char *zTextMbcs;
42212 LPWSTR zTmpWide;
42213
42214 zTmpWide = winUtf8ToUnicode(zText);
42215 if( zTmpWide==0 ){
42216 return 0;
42217 }
42218 zTextMbcs = winUnicodeToMbcs(zTmpWide, useAnsi);
42219 sqlite3_free(zTmpWide);
42220 return zTextMbcs;
42221}
42222
42223/*
42224** This is a public wrapper for the winUtf8ToUnicode() function.
42225*/
42226SQLITE_API LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText){
42227#ifdef SQLITE_ENABLE_API_ARMOR
42228 if( !zText ){
42229 (void)SQLITE_MISUSE_BKPT;
42230 return 0;
42231 }
42232#endif
42233#ifndef SQLITE_OMIT_AUTOINIT
42234 if( sqlite3_initialize() ) return 0;
42235#endif
42236 return winUtf8ToUnicode(zText);
42237}
42238
42239/*
42240** This is a public wrapper for the winUnicodeToUtf8() function.
42241*/
42242SQLITE_API char *sqlite3_win32_unicode_to_utf8(LPCWSTR zWideText){
42243#ifdef SQLITE_ENABLE_API_ARMOR
42244 if( !zWideText ){
42245 (void)SQLITE_MISUSE_BKPT;
42246 return 0;
42247 }
42248#endif
42249#ifndef SQLITE_OMIT_AUTOINIT
42250 if( sqlite3_initialize() ) return 0;
42251#endif
42252 return winUnicodeToUtf8(zWideText);
42253}
42254
42255/*
42256** This is a public wrapper for the winMbcsToUtf8() function.
42257*/
42258SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zText){
42259#ifdef SQLITE_ENABLE_API_ARMOR
42260 if( !zText ){
42261 (void)SQLITE_MISUSE_BKPT;
42262 return 0;
42263 }
42264#endif
42265#ifndef SQLITE_OMIT_AUTOINIT
42266 if( sqlite3_initialize() ) return 0;
42267#endif
42268 return winMbcsToUtf8(zText, osAreFileApisANSI());
42269}
42270
42271/*
42272** This is a public wrapper for the winMbcsToUtf8() function.
42273*/
42274SQLITE_API char *sqlite3_win32_mbcs_to_utf8_v2(const char *zText, int useAnsi){
42275#ifdef SQLITE_ENABLE_API_ARMOR
42276 if( !zText ){
42277 (void)SQLITE_MISUSE_BKPT;
42278 return 0;
42279 }
42280#endif
42281#ifndef SQLITE_OMIT_AUTOINIT
42282 if( sqlite3_initialize() ) return 0;
42283#endif
42284 return winMbcsToUtf8(zText, useAnsi);
42285}
42286
42287/*
42288** This is a public wrapper for the winUtf8ToMbcs() function.
42289*/
42290SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zText){
42291#ifdef SQLITE_ENABLE_API_ARMOR
42292 if( !zText ){
42293 (void)SQLITE_MISUSE_BKPT;
42294 return 0;
42295 }
42296#endif
42297#ifndef SQLITE_OMIT_AUTOINIT
42298 if( sqlite3_initialize() ) return 0;
42299#endif
42300 return winUtf8ToMbcs(zText, osAreFileApisANSI());
42301}
42302
42303/*
42304** This is a public wrapper for the winUtf8ToMbcs() function.
42305*/
42306SQLITE_API char *sqlite3_win32_utf8_to_mbcs_v2(const char *zText, int useAnsi){
42307#ifdef SQLITE_ENABLE_API_ARMOR
42308 if( !zText ){
42309 (void)SQLITE_MISUSE_BKPT;
42310 return 0;
42311 }
42312#endif
42313#ifndef SQLITE_OMIT_AUTOINIT
42314 if( sqlite3_initialize() ) return 0;
42315#endif
42316 return winUtf8ToMbcs(zText, useAnsi);
42317}
42318
42319/*
42320** This function is the same as sqlite3_win32_set_directory (below); however,
42321** it accepts a UTF-8 string.
42322*/
42323SQLITE_API int sqlite3_win32_set_directory8(
42324 unsigned long type, /* Identifier for directory being set or reset */
42325 const char *zValue /* New value for directory being set or reset */
42326){
42327 char **ppDirectory = 0;
42328#ifndef SQLITE_OMIT_AUTOINIT
42329 int rc = sqlite3_initialize();
42330 if( rc ) return rc;
42331#endif
42332 if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){
42333 ppDirectory = &sqlite3_data_directory;
42334 }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){
42335 ppDirectory = &sqlite3_temp_directory;
42336 }
42337 assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE
42338 || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE
42339 );
42340 assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
42341 if( ppDirectory ){
42342 char *zCopy = 0;
42343 if( zValue && zValue[0] ){
42344 zCopy = sqlite3_mprintf("%s", zValue);
42345 if ( zCopy==0 ){
42346 return SQLITE_NOMEM_BKPT;
42347 }
42348 }
42349 sqlite3_free(*ppDirectory);
42350 *ppDirectory = zCopy;
42351 return SQLITE_OK;
42352 }
42353 return SQLITE_ERROR;
42354}
42355
42356/*
42357** This function is the same as sqlite3_win32_set_directory (below); however,
42358** it accepts a UTF-16 string.
42359*/
42360SQLITE_API int sqlite3_win32_set_directory16(
42361 unsigned long type, /* Identifier for directory being set or reset */
42362 const void *zValue /* New value for directory being set or reset */
42363){
42364 int rc;
42365 char *zUtf8 = 0;
42366 if( zValue ){
42367 zUtf8 = sqlite3_win32_unicode_to_utf8(zValue);
42368 if( zUtf8==0 ) return SQLITE_NOMEM_BKPT;
42369 }
42370 rc = sqlite3_win32_set_directory8(type, zUtf8);
42371 if( zUtf8 ) sqlite3_free(zUtf8);
42372 return rc;
42373}
42374
42375/*
42376** This function sets the data directory or the temporary directory based on
42377** the provided arguments. The type argument must be 1 in order to set the
42378** data directory or 2 in order to set the temporary directory. The zValue
42379** argument is the name of the directory to use. The return value will be
42380** SQLITE_OK if successful.
42381*/
42382SQLITE_API int sqlite3_win32_set_directory(
42383 unsigned long type, /* Identifier for directory being set or reset */
42384 void *zValue /* New value for directory being set or reset */
42385){
42386 return sqlite3_win32_set_directory16(type, zValue);
42387}
42388
42389/*
42390** The return value of winGetLastErrorMsg
42391** is zero if the error message fits in the buffer, or non-zero
42392** otherwise (if the message was truncated).
42393*/
42394static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
42395 /* FormatMessage returns 0 on failure. Otherwise it
42396 ** returns the number of TCHARs written to the output
42397 ** buffer, excluding the terminating null char.
42398 */
42399 DWORD dwLen = 0;
42400 char *zOut = 0;
42401
42402 if( osIsNT() ){
42403#if SQLITE_OS_WINRT
42404 WCHAR zTempWide[SQLITE_WIN32_MAX_ERRMSG_CHARS+1];
42405 dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
42406 FORMAT_MESSAGE_IGNORE_INSERTS,
42407 NULL,
42408 lastErrno,
42409 0,
42410 zTempWide,
42411 SQLITE_WIN32_MAX_ERRMSG_CHARS,
42412 0);
42413#else
42414 LPWSTR zTempWide = NULL;
42415 dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
42416 FORMAT_MESSAGE_FROM_SYSTEM |
42417 FORMAT_MESSAGE_IGNORE_INSERTS,
42418 NULL,
42419 lastErrno,
42420 0,
42421 (LPWSTR) &zTempWide,
42422 0,
42423 0);
42424#endif
42425 if( dwLen > 0 ){
42426 /* allocate a buffer and convert to UTF8 */
42427 sqlite3BeginBenignMalloc();
42428 zOut = winUnicodeToUtf8(zTempWide);
42429 sqlite3EndBenignMalloc();
42430#if !SQLITE_OS_WINRT
42431 /* free the system buffer allocated by FormatMessage */
42432 osLocalFree(zTempWide);
42433#endif
42434 }
42435 }
42436#ifdef SQLITE_WIN32_HAS_ANSI
42437 else{
42438 char *zTemp = NULL;
42439 dwLen = osFormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
42440 FORMAT_MESSAGE_FROM_SYSTEM |
42441 FORMAT_MESSAGE_IGNORE_INSERTS,
42442 NULL,
42443 lastErrno,
42444 0,
42445 (LPSTR) &zTemp,
42446 0,
42447 0);
42448 if( dwLen > 0 ){
42449 /* allocate a buffer and convert to UTF8 */
42450 sqlite3BeginBenignMalloc();
42451 zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
42452 sqlite3EndBenignMalloc();
42453 /* free the system buffer allocated by FormatMessage */
42454 osLocalFree(zTemp);
42455 }
42456 }
42457#endif
42458 if( 0 == dwLen ){
42459 sqlite3_snprintf(nBuf, zBuf, "OsError 0x%lx (%lu)", lastErrno, lastErrno);
42460 }else{
42461 /* copy a maximum of nBuf chars to output buffer */
42462 sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
42463 /* free the UTF8 buffer */
42464 sqlite3_free(zOut);
42465 }
42466 return 0;
42467}
42468
42469/*
42470**
42471** This function - winLogErrorAtLine() - is only ever called via the macro
42472** winLogError().
42473**
42474** This routine is invoked after an error occurs in an OS function.
42475** It logs a message using sqlite3_log() containing the current value of
42476** error code and, if possible, the human-readable equivalent from
42477** FormatMessage.
42478**
42479** The first argument passed to the macro should be the error code that
42480** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
42481** The two subsequent arguments should be the name of the OS function that
42482** failed and the associated file-system path, if any.
42483*/
42484#define winLogError(a,b,c,d) winLogErrorAtLine(a,b,c,d,__LINE__)
42485static int winLogErrorAtLine(
42486 int errcode, /* SQLite error code */
42487 DWORD lastErrno, /* Win32 last error */
42488 const char *zFunc, /* Name of OS function that failed */
42489 const char *zPath, /* File path associated with error */
42490 int iLine /* Source line number where error occurred */
42491){
42492 char zMsg[500]; /* Human readable error text */
42493 int i; /* Loop counter */
42494
42495 zMsg[0] = 0;
42496 winGetLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
42497 assert( errcode!=SQLITE_OK );
42498 if( zPath==0 ) zPath = "";
42499 for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
42500 zMsg[i] = 0;
42501 sqlite3_log(errcode,
42502 "os_win.c:%d: (%lu) %s(%s) - %s",
42503 iLine, lastErrno, zFunc, zPath, zMsg
42504 );
42505
42506 return errcode;
42507}
42508
42509/*
42510** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
42511** will be retried following a locking error - probably caused by
42512** antivirus software. Also the initial delay before the first retry.
42513** The delay increases linearly with each retry.
42514*/
42515#ifndef SQLITE_WIN32_IOERR_RETRY
42516# define SQLITE_WIN32_IOERR_RETRY 10
42517#endif
42518#ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
42519# define SQLITE_WIN32_IOERR_RETRY_DELAY 25
42520#endif
42521static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
42522static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
42523
42524/*
42525** The "winIoerrCanRetry1" macro is used to determine if a particular I/O
42526** error code obtained via GetLastError() is eligible to be retried. It
42527** must accept the error code DWORD as its only argument and should return
42528** non-zero if the error code is transient in nature and the operation
42529** responsible for generating the original error might succeed upon being
42530** retried. The argument to this macro should be a variable.
42531**
42532** Additionally, a macro named "winIoerrCanRetry2" may be defined. If it
42533** is defined, it will be consulted only when the macro "winIoerrCanRetry1"
42534** returns zero. The "winIoerrCanRetry2" macro is completely optional and
42535** may be used to include additional error codes in the set that should
42536** result in the failing I/O operation being retried by the caller. If
42537** defined, the "winIoerrCanRetry2" macro must exhibit external semantics
42538** identical to those of the "winIoerrCanRetry1" macro.
42539*/
42540#if !defined(winIoerrCanRetry1)
42541#define winIoerrCanRetry1(a) (((a)==ERROR_ACCESS_DENIED) || \
42542 ((a)==ERROR_SHARING_VIOLATION) || \
42543 ((a)==ERROR_LOCK_VIOLATION) || \
42544 ((a)==ERROR_DEV_NOT_EXIST) || \
42545 ((a)==ERROR_NETNAME_DELETED) || \
42546 ((a)==ERROR_SEM_TIMEOUT) || \
42547 ((a)==ERROR_NETWORK_UNREACHABLE))
42548#endif
42549
42550/*
42551** If a ReadFile() or WriteFile() error occurs, invoke this routine
42552** to see if it should be retried. Return TRUE to retry. Return FALSE
42553** to give up with an error.
42554*/
42555static int winRetryIoerr(int *pnRetry, DWORD *pError){
42556 DWORD e = osGetLastError();
42557 if( *pnRetry>=winIoerrRetry ){
42558 if( pError ){
42559 *pError = e;
42560 }
42561 return 0;
42562 }
42563 if( winIoerrCanRetry1(e) ){
42564 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
42565 ++*pnRetry;
42566 return 1;
42567 }
42568#if defined(winIoerrCanRetry2)
42569 else if( winIoerrCanRetry2(e) ){
42570 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
42571 ++*pnRetry;
42572 return 1;
42573 }
42574#endif
42575 if( pError ){
42576 *pError = e;
42577 }
42578 return 0;
42579}
42580
42581/*
42582** Log a I/O error retry episode.
42583*/
42584static void winLogIoerr(int nRetry, int lineno){
42585 if( nRetry ){
42586 sqlite3_log(SQLITE_NOTICE,
42587 "delayed %dms for lock/sharing conflict at line %d",
42588 winIoerrRetryDelay*nRetry*(nRetry+1)/2, lineno
42589 );
42590 }
42591}
42592
42593/*
42594** This #if does not rely on the SQLITE_OS_WINCE define because the
42595** corresponding section in "date.c" cannot use it.
42596*/
42597#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
42598 (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
42599/*
42600** The MSVC CRT on Windows CE may not have a localtime() function.
42601** So define a substitute.
42602*/
42603/* # include <time.h> */
42604struct tm *__cdecl localtime(const time_t *t)
42605{
42606 static struct tm y;
42607 FILETIME uTm, lTm;
42608 SYSTEMTIME pTm;
42609 sqlite3_int64 t64;
42610 t64 = *t;
42611 t64 = (t64 + 11644473600)*10000000;
42612 uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
42613 uTm.dwHighDateTime= (DWORD)(t64 >> 32);
42614 osFileTimeToLocalFileTime(&uTm,&lTm);
42615 osFileTimeToSystemTime(&lTm,&pTm);
42616 y.tm_year = pTm.wYear - 1900;
42617 y.tm_mon = pTm.wMonth - 1;
42618 y.tm_wday = pTm.wDayOfWeek;
42619 y.tm_mday = pTm.wDay;
42620 y.tm_hour = pTm.wHour;
42621 y.tm_min = pTm.wMinute;
42622 y.tm_sec = pTm.wSecond;
42623 return &y;
42624}
42625#endif
42626
42627#if SQLITE_OS_WINCE
42628/*************************************************************************
42629** This section contains code for WinCE only.
42630*/
42631#define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
42632
42633/*
42634** Acquire a lock on the handle h
42635*/
42636static void winceMutexAcquire(HANDLE h){
42637 DWORD dwErr;
42638 do {
42639 dwErr = osWaitForSingleObject(h, INFINITE);
42640 } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
42641}
42642/*
42643** Release a lock acquired by winceMutexAcquire()
42644*/
42645#define winceMutexRelease(h) ReleaseMutex(h)
42646
42647/*
42648** Create the mutex and shared memory used for locking in the file
42649** descriptor pFile
42650*/
42651static int winceCreateLock(const char *zFilename, winFile *pFile){
42652 LPWSTR zTok;
42653 LPWSTR zName;
42654 DWORD lastErrno;
42655 BOOL bLogged = FALSE;
42656 BOOL bInit = TRUE;
42657
42658 zName = winUtf8ToUnicode(zFilename);
42659 if( zName==0 ){
42660 /* out of memory */
42661 return SQLITE_IOERR_NOMEM_BKPT;
42662 }
42663
42664 /* Initialize the local lockdata */
42665 memset(&pFile->local, 0, sizeof(pFile->local));
42666
42667 /* Replace the backslashes from the filename and lowercase it
42668 ** to derive a mutex name. */
42669 zTok = osCharLowerW(zName);
42670 for (;*zTok;zTok++){
42671 if (*zTok == '\\') *zTok = '_';
42672 }
42673
42674 /* Create/open the named mutex */
42675 pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
42676 if (!pFile->hMutex){
42677 pFile->lastErrno = osGetLastError();
42678 sqlite3_free(zName);
42679 return winLogError(SQLITE_IOERR, pFile->lastErrno,
42680 "winceCreateLock1", zFilename);
42681 }
42682
42683 /* Acquire the mutex before continuing */
42684 winceMutexAcquire(pFile->hMutex);
42685
42686 /* Since the names of named mutexes, semaphores, file mappings etc are
42687 ** case-sensitive, take advantage of that by uppercasing the mutex name
42688 ** and using that as the shared filemapping name.
42689 */
42690 osCharUpperW(zName);
42691 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
42692 PAGE_READWRITE, 0, sizeof(winceLock),
42693 zName);
42694
42695 /* Set a flag that indicates we're the first to create the memory so it
42696 ** must be zero-initialized */
42697 lastErrno = osGetLastError();
42698 if (lastErrno == ERROR_ALREADY_EXISTS){
42699 bInit = FALSE;
42700 }
42701
42702 sqlite3_free(zName);
42703
42704 /* If we succeeded in making the shared memory handle, map it. */
42705 if( pFile->hShared ){
42706 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
42707 FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
42708 /* If mapping failed, close the shared memory handle and erase it */
42709 if( !pFile->shared ){
42710 pFile->lastErrno = osGetLastError();
42711 winLogError(SQLITE_IOERR, pFile->lastErrno,
42712 "winceCreateLock2", zFilename);
42713 bLogged = TRUE;
42714 osCloseHandle(pFile->hShared);
42715 pFile->hShared = NULL;
42716 }
42717 }
42718
42719 /* If shared memory could not be created, then close the mutex and fail */
42720 if( pFile->hShared==NULL ){
42721 if( !bLogged ){
42722 pFile->lastErrno = lastErrno;
42723 winLogError(SQLITE_IOERR, pFile->lastErrno,
42724 "winceCreateLock3", zFilename);
42725 bLogged = TRUE;
42726 }
42727 winceMutexRelease(pFile->hMutex);
42728 osCloseHandle(pFile->hMutex);
42729 pFile->hMutex = NULL;
42730 return SQLITE_IOERR;
42731 }
42732
42733 /* Initialize the shared memory if we're supposed to */
42734 if( bInit ){
42735 memset(pFile->shared, 0, sizeof(winceLock));
42736 }
42737
42738 winceMutexRelease(pFile->hMutex);
42739 return SQLITE_OK;
42740}
42741
42742/*
42743** Destroy the part of winFile that deals with wince locks
42744*/
42745static void winceDestroyLock(winFile *pFile){
42746 if (pFile->hMutex){
42747 /* Acquire the mutex */
42748 winceMutexAcquire(pFile->hMutex);
42749
42750 /* The following blocks should probably assert in debug mode, but they
42751 are to cleanup in case any locks remained open */
42752 if (pFile->local.nReaders){
42753 pFile->shared->nReaders --;
42754 }
42755 if (pFile->local.bReserved){
42756 pFile->shared->bReserved = FALSE;
42757 }
42758 if (pFile->local.bPending){
42759 pFile->shared->bPending = FALSE;
42760 }
42761 if (pFile->local.bExclusive){
42762 pFile->shared->bExclusive = FALSE;
42763 }
42764
42765 /* De-reference and close our copy of the shared memory handle */
42766 osUnmapViewOfFile(pFile->shared);
42767 osCloseHandle(pFile->hShared);
42768
42769 /* Done with the mutex */
42770 winceMutexRelease(pFile->hMutex);
42771 osCloseHandle(pFile->hMutex);
42772 pFile->hMutex = NULL;
42773 }
42774}
42775
42776/*
42777** An implementation of the LockFile() API of Windows for CE
42778*/
42779static BOOL winceLockFile(
42780 LPHANDLE phFile,
42781 DWORD dwFileOffsetLow,
42782 DWORD dwFileOffsetHigh,
42783 DWORD nNumberOfBytesToLockLow,
42784 DWORD nNumberOfBytesToLockHigh
42785){
42786 winFile *pFile = HANDLE_TO_WINFILE(phFile);
42787 BOOL bReturn = FALSE;
42788
42789 UNUSED_PARAMETER(dwFileOffsetHigh);
42790 UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
42791
42792 if (!pFile->hMutex) return TRUE;
42793 winceMutexAcquire(pFile->hMutex);
42794
42795 /* Wanting an exclusive lock? */
42796 if (dwFileOffsetLow == (DWORD)SHARED_FIRST
42797 && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
42798 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
42799 pFile->shared->bExclusive = TRUE;
42800 pFile->local.bExclusive = TRUE;
42801 bReturn = TRUE;
42802 }
42803 }
42804
42805 /* Want a read-only lock? */
42806 else if (dwFileOffsetLow == (DWORD)SHARED_FIRST &&
42807 nNumberOfBytesToLockLow == 1){
42808 if (pFile->shared->bExclusive == 0){
42809 pFile->local.nReaders ++;
42810 if (pFile->local.nReaders == 1){
42811 pFile->shared->nReaders ++;
42812 }
42813 bReturn = TRUE;
42814 }
42815 }
42816
42817 /* Want a pending lock? */
42818 else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
42819 && nNumberOfBytesToLockLow == 1){
42820 /* If no pending lock has been acquired, then acquire it */
42821 if (pFile->shared->bPending == 0) {
42822 pFile->shared->bPending = TRUE;
42823 pFile->local.bPending = TRUE;
42824 bReturn = TRUE;
42825 }
42826 }
42827
42828 /* Want a reserved lock? */
42829 else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
42830 && nNumberOfBytesToLockLow == 1){
42831 if (pFile->shared->bReserved == 0) {
42832 pFile->shared->bReserved = TRUE;
42833 pFile->local.bReserved = TRUE;
42834 bReturn = TRUE;
42835 }
42836 }
42837
42838 winceMutexRelease(pFile->hMutex);
42839 return bReturn;
42840}
42841
42842/*
42843** An implementation of the UnlockFile API of Windows for CE
42844*/
42845static BOOL winceUnlockFile(
42846 LPHANDLE phFile,
42847 DWORD dwFileOffsetLow,
42848 DWORD dwFileOffsetHigh,
42849 DWORD nNumberOfBytesToUnlockLow,
42850 DWORD nNumberOfBytesToUnlockHigh
42851){
42852 winFile *pFile = HANDLE_TO_WINFILE(phFile);
42853 BOOL bReturn = FALSE;
42854
42855 UNUSED_PARAMETER(dwFileOffsetHigh);
42856 UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh);
42857
42858 if (!pFile->hMutex) return TRUE;
42859 winceMutexAcquire(pFile->hMutex);
42860
42861 /* Releasing a reader lock or an exclusive lock */
42862 if (dwFileOffsetLow == (DWORD)SHARED_FIRST){
42863 /* Did we have an exclusive lock? */
42864 if (pFile->local.bExclusive){
42865 assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE);
42866 pFile->local.bExclusive = FALSE;
42867 pFile->shared->bExclusive = FALSE;
42868 bReturn = TRUE;
42869 }
42870
42871 /* Did we just have a reader lock? */
42872 else if (pFile->local.nReaders){
42873 assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE
42874 || nNumberOfBytesToUnlockLow == 1);
42875 pFile->local.nReaders --;
42876 if (pFile->local.nReaders == 0)
42877 {
42878 pFile->shared->nReaders --;
42879 }
42880 bReturn = TRUE;
42881 }
42882 }
42883
42884 /* Releasing a pending lock */
42885 else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
42886 && nNumberOfBytesToUnlockLow == 1){
42887 if (pFile->local.bPending){
42888 pFile->local.bPending = FALSE;
42889 pFile->shared->bPending = FALSE;
42890 bReturn = TRUE;
42891 }
42892 }
42893 /* Releasing a reserved lock */
42894 else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
42895 && nNumberOfBytesToUnlockLow == 1){
42896 if (pFile->local.bReserved) {
42897 pFile->local.bReserved = FALSE;
42898 pFile->shared->bReserved = FALSE;
42899 bReturn = TRUE;
42900 }
42901 }
42902
42903 winceMutexRelease(pFile->hMutex);
42904 return bReturn;
42905}
42906/*
42907** End of the special code for wince
42908*****************************************************************************/
42909#endif /* SQLITE_OS_WINCE */
42910
42911/*
42912** Lock a file region.
42913*/
42914static BOOL winLockFile(
42915 LPHANDLE phFile,
42916 DWORD flags,
42917 DWORD offsetLow,
42918 DWORD offsetHigh,
42919 DWORD numBytesLow,
42920 DWORD numBytesHigh
42921){
42922#if SQLITE_OS_WINCE
42923 /*
42924 ** NOTE: Windows CE is handled differently here due its lack of the Win32
42925 ** API LockFile.
42926 */
42927 return winceLockFile(phFile, offsetLow, offsetHigh,
42928 numBytesLow, numBytesHigh);
42929#else
42930 if( osIsNT() ){
42931 OVERLAPPED ovlp;
42932 memset(&ovlp, 0, sizeof(OVERLAPPED));
42933 ovlp.Offset = offsetLow;
42934 ovlp.OffsetHigh = offsetHigh;
42935 return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
42936 }else{
42937 return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
42938 numBytesHigh);
42939 }
42940#endif
42941}
42942
42943/*
42944** Unlock a file region.
42945 */
42946static BOOL winUnlockFile(
42947 LPHANDLE phFile,
42948 DWORD offsetLow,
42949 DWORD offsetHigh,
42950 DWORD numBytesLow,
42951 DWORD numBytesHigh
42952){
42953#if SQLITE_OS_WINCE
42954 /*
42955 ** NOTE: Windows CE is handled differently here due its lack of the Win32
42956 ** API UnlockFile.
42957 */
42958 return winceUnlockFile(phFile, offsetLow, offsetHigh,
42959 numBytesLow, numBytesHigh);
42960#else
42961 if( osIsNT() ){
42962 OVERLAPPED ovlp;
42963 memset(&ovlp, 0, sizeof(OVERLAPPED));
42964 ovlp.Offset = offsetLow;
42965 ovlp.OffsetHigh = offsetHigh;
42966 return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
42967 }else{
42968 return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
42969 numBytesHigh);
42970 }
42971#endif
42972}
42973
42974/*****************************************************************************
42975** The next group of routines implement the I/O methods specified
42976** by the sqlite3_io_methods object.
42977******************************************************************************/
42978
42979/*
42980** Some Microsoft compilers lack this definition.
42981*/
42982#ifndef INVALID_SET_FILE_POINTER
42983# define INVALID_SET_FILE_POINTER ((DWORD)-1)
42984#endif
42985
42986/*
42987** Move the current position of the file handle passed as the first
42988** argument to offset iOffset within the file. If successful, return 0.
42989** Otherwise, set pFile->lastErrno and return non-zero.
42990*/
42991static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
42992#if !SQLITE_OS_WINRT
42993 LONG upperBits; /* Most sig. 32 bits of new offset */
42994 LONG lowerBits; /* Least sig. 32 bits of new offset */
42995 DWORD dwRet; /* Value returned by SetFilePointer() */
42996 DWORD lastErrno; /* Value returned by GetLastError() */
42997
42998 OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
42999
43000 upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
43001 lowerBits = (LONG)(iOffset & 0xffffffff);
43002
43003 /* API oddity: If successful, SetFilePointer() returns a dword
43004 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
43005 ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
43006 ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
43007 ** whether an error has actually occurred, it is also necessary to call
43008 ** GetLastError().
43009 */
43010 dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
43011
43012 if( (dwRet==INVALID_SET_FILE_POINTER
43013 && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
43014 pFile->lastErrno = lastErrno;
43015 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
43016 "winSeekFile", pFile->zPath);
43017 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
43018 return 1;
43019 }
43020
43021 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
43022 return 0;
43023#else
43024 /*
43025 ** Same as above, except that this implementation works for WinRT.
43026 */
43027
43028 LARGE_INTEGER x; /* The new offset */
43029 BOOL bRet; /* Value returned by SetFilePointerEx() */
43030
43031 x.QuadPart = iOffset;
43032 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
43033
43034 if(!bRet){
43035 pFile->lastErrno = osGetLastError();
43036 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
43037 "winSeekFile", pFile->zPath);
43038 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
43039 return 1;
43040 }
43041
43042 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
43043 return 0;
43044#endif
43045}
43046
43047#if SQLITE_MAX_MMAP_SIZE>0
43048/* Forward references to VFS helper methods used for memory mapped files */
43049static int winMapfile(winFile*, sqlite3_int64);
43050static int winUnmapfile(winFile*);
43051#endif
43052
43053/*
43054** Close a file.
43055**
43056** It is reported that an attempt to close a handle might sometimes
43057** fail. This is a very unreasonable result, but Windows is notorious
43058** for being unreasonable so I do not doubt that it might happen. If
43059** the close fails, we pause for 100 milliseconds and try again. As
43060** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
43061** giving up and returning an error.
43062*/
43063#define MX_CLOSE_ATTEMPT 3
43064static int winClose(sqlite3_file *id){
43065 int rc, cnt = 0;
43066 winFile *pFile = (winFile*)id;
43067
43068 assert( id!=0 );
43069#ifndef SQLITE_OMIT_WAL
43070 assert( pFile->pShm==0 );
43071#endif
43072 assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
43073 OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p\n",
43074 osGetCurrentProcessId(), pFile, pFile->h));
43075
43076#if SQLITE_MAX_MMAP_SIZE>0
43077 winUnmapfile(pFile);
43078#endif
43079
43080 do{
43081 rc = osCloseHandle(pFile->h);
43082 /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
43083 }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
43084#if SQLITE_OS_WINCE
43085#define WINCE_DELETION_ATTEMPTS 3
43086 {
43087 winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
43088 if( pAppData==NULL || !pAppData->bNoLock ){
43089 winceDestroyLock(pFile);
43090 }
43091 }
43092 if( pFile->zDeleteOnClose ){
43093 int cnt = 0;
43094 while(
43095 osDeleteFileW(pFile->zDeleteOnClose)==0
43096 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
43097 && cnt++ < WINCE_DELETION_ATTEMPTS
43098 ){
43099 sqlite3_win32_sleep(100); /* Wait a little before trying again */
43100 }
43101 sqlite3_free(pFile->zDeleteOnClose);
43102 }
43103#endif
43104 if( rc ){
43105 pFile->h = NULL;
43106 }
43107 OpenCounter(-1);
43108 OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p, rc=%s\n",
43109 osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
43110 return rc ? SQLITE_OK
43111 : winLogError(SQLITE_IOERR_CLOSE, osGetLastError(),
43112 "winClose", pFile->zPath);
43113}
43114
43115/*
43116** Read data from a file into a buffer. Return SQLITE_OK if all
43117** bytes were read successfully and SQLITE_IOERR if anything goes
43118** wrong.
43119*/
43120static int winRead(
43121 sqlite3_file *id, /* File to read from */
43122 void *pBuf, /* Write content into this buffer */
43123 int amt, /* Number of bytes to read */
43124 sqlite3_int64 offset /* Begin reading at this offset */
43125){
43126#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
43127 OVERLAPPED overlapped; /* The offset for ReadFile. */
43128#endif
43129 winFile *pFile = (winFile*)id; /* file handle */
43130 DWORD nRead; /* Number of bytes actually read from file */
43131 int nRetry = 0; /* Number of retrys */
43132
43133 assert( id!=0 );
43134 assert( amt>0 );
43135 assert( offset>=0 );
43136 SimulateIOError(return SQLITE_IOERR_READ);
43137 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
43138 "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
43139 pFile->h, pBuf, amt, offset, pFile->locktype));
43140
43141#if SQLITE_MAX_MMAP_SIZE>0
43142 /* Deal with as much of this read request as possible by transfering
43143 ** data from the memory mapping using memcpy(). */
43144 if( offset<pFile->mmapSize ){
43145 if( offset+amt <= pFile->mmapSize ){
43146 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
43147 OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
43148 osGetCurrentProcessId(), pFile, pFile->h));
43149 return SQLITE_OK;
43150 }else{
43151 int nCopy = (int)(pFile->mmapSize - offset);
43152 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
43153 pBuf = &((u8 *)pBuf)[nCopy];
43154 amt -= nCopy;
43155 offset += nCopy;
43156 }
43157 }
43158#endif
43159
43160#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
43161 if( winSeekFile(pFile, offset) ){
43162 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
43163 osGetCurrentProcessId(), pFile, pFile->h));
43164 return SQLITE_FULL;
43165 }
43166 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
43167#else
43168 memset(&overlapped, 0, sizeof(OVERLAPPED));
43169 overlapped.Offset = (LONG)(offset & 0xffffffff);
43170 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
43171 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
43172 osGetLastError()!=ERROR_HANDLE_EOF ){
43173#endif
43174 DWORD lastErrno;
43175 if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
43176 pFile->lastErrno = lastErrno;
43177 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_READ\n",
43178 osGetCurrentProcessId(), pFile, pFile->h));
43179 return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
43180 "winRead", pFile->zPath);
43181 }
43182 winLogIoerr(nRetry, __LINE__);
43183 if( nRead<(DWORD)amt ){
43184 /* Unread parts of the buffer must be zero-filled */
43185 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
43186 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_SHORT_READ\n",
43187 osGetCurrentProcessId(), pFile, pFile->h));
43188 return SQLITE_IOERR_SHORT_READ;
43189 }
43190
43191 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
43192 osGetCurrentProcessId(), pFile, pFile->h));
43193 return SQLITE_OK;
43194}
43195
43196/*
43197** Write data from a buffer into a file. Return SQLITE_OK on success
43198** or some other error code on failure.
43199*/
43200static int winWrite(
43201 sqlite3_file *id, /* File to write into */
43202 const void *pBuf, /* The bytes to be written */
43203 int amt, /* Number of bytes to write */
43204 sqlite3_int64 offset /* Offset into the file to begin writing at */
43205){
43206 int rc = 0; /* True if error has occurred, else false */
43207 winFile *pFile = (winFile*)id; /* File handle */
43208 int nRetry = 0; /* Number of retries */
43209
43210 assert( amt>0 );
43211 assert( pFile );
43212 SimulateIOError(return SQLITE_IOERR_WRITE);
43213 SimulateDiskfullError(return SQLITE_FULL);
43214
43215 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
43216 "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
43217 pFile->h, pBuf, amt, offset, pFile->locktype));
43218
43219#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0
43220 /* Deal with as much of this write request as possible by transfering
43221 ** data from the memory mapping using memcpy(). */
43222 if( offset<pFile->mmapSize ){
43223 if( offset+amt <= pFile->mmapSize ){
43224 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
43225 OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
43226 osGetCurrentProcessId(), pFile, pFile->h));
43227 return SQLITE_OK;
43228 }else{
43229 int nCopy = (int)(pFile->mmapSize - offset);
43230 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
43231 pBuf = &((u8 *)pBuf)[nCopy];
43232 amt -= nCopy;
43233 offset += nCopy;
43234 }
43235 }
43236#endif
43237
43238#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
43239 rc = winSeekFile(pFile, offset);
43240 if( rc==0 ){
43241#else
43242 {
43243#endif
43244#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
43245 OVERLAPPED overlapped; /* The offset for WriteFile. */
43246#endif
43247 u8 *aRem = (u8 *)pBuf; /* Data yet to be written */
43248 int nRem = amt; /* Number of bytes yet to be written */
43249 DWORD nWrite; /* Bytes written by each WriteFile() call */
43250 DWORD lastErrno = NO_ERROR; /* Value returned by GetLastError() */
43251
43252#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
43253 memset(&overlapped, 0, sizeof(OVERLAPPED));
43254 overlapped.Offset = (LONG)(offset & 0xffffffff);
43255 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
43256#endif
43257
43258 while( nRem>0 ){
43259#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
43260 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
43261#else
43262 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
43263#endif
43264 if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
43265 break;
43266 }
43267 assert( nWrite==0 || nWrite<=(DWORD)nRem );
43268 if( nWrite==0 || nWrite>(DWORD)nRem ){
43269 lastErrno = osGetLastError();
43270 break;
43271 }
43272#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
43273 offset += nWrite;
43274 overlapped.Offset = (LONG)(offset & 0xffffffff);
43275 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
43276#endif
43277 aRem += nWrite;
43278 nRem -= nWrite;
43279 }
43280 if( nRem>0 ){
43281 pFile->lastErrno = lastErrno;
43282 rc = 1;
43283 }
43284 }
43285
43286 if( rc ){
43287 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
43288 || ( pFile->lastErrno==ERROR_DISK_FULL )){
43289 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
43290 osGetCurrentProcessId(), pFile, pFile->h));
43291 return winLogError(SQLITE_FULL, pFile->lastErrno,
43292 "winWrite1", pFile->zPath);
43293 }
43294 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_WRITE\n",
43295 osGetCurrentProcessId(), pFile, pFile->h));
43296 return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
43297 "winWrite2", pFile->zPath);
43298 }else{
43299 winLogIoerr(nRetry, __LINE__);
43300 }
43301 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
43302 osGetCurrentProcessId(), pFile, pFile->h));
43303 return SQLITE_OK;
43304}
43305
43306/*
43307** Truncate an open file to a specified size
43308*/
43309static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
43310 winFile *pFile = (winFile*)id; /* File handle object */
43311 int rc = SQLITE_OK; /* Return code for this function */
43312 DWORD lastErrno;
43313#if SQLITE_MAX_MMAP_SIZE>0
43314 sqlite3_int64 oldMmapSize;
43315 if( pFile->nFetchOut>0 ){
43316 /* File truncation is a no-op if there are outstanding memory mapped
43317 ** pages. This is because truncating the file means temporarily unmapping
43318 ** the file, and that might delete memory out from under existing cursors.
43319 **
43320 ** This can result in incremental vacuum not truncating the file,
43321 ** if there is an active read cursor when the incremental vacuum occurs.
43322 ** No real harm comes of this - the database file is not corrupted,
43323 ** though some folks might complain that the file is bigger than it
43324 ** needs to be.
43325 **
43326 ** The only feasible work-around is to defer the truncation until after
43327 ** all references to memory-mapped content are closed. That is doable,
43328 ** but involves adding a few branches in the common write code path which
43329 ** could slow down normal operations slightly. Hence, we have decided for
43330 ** now to simply make trancations a no-op if there are pending reads. We
43331 ** can maybe revisit this decision in the future.
43332 */
43333 return SQLITE_OK;
43334 }
43335#endif
43336
43337 assert( pFile );
43338 SimulateIOError(return SQLITE_IOERR_TRUNCATE);
43339 OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, size=%lld, lock=%d\n",
43340 osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
43341
43342 /* If the user has configured a chunk-size for this file, truncate the
43343 ** file so that it consists of an integer number of chunks (i.e. the
43344 ** actual file size after the operation may be larger than the requested
43345 ** size).
43346 */
43347 if( pFile->szChunk>0 ){
43348 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
43349 }
43350
43351#if SQLITE_MAX_MMAP_SIZE>0
43352 if( pFile->pMapRegion ){
43353 oldMmapSize = pFile->mmapSize;
43354 }else{
43355 oldMmapSize = 0;
43356 }
43357 winUnmapfile(pFile);
43358#endif
43359
43360 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
43361 if( winSeekFile(pFile, nByte) ){
43362 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
43363 "winTruncate1", pFile->zPath);
43364 }else if( 0==osSetEndOfFile(pFile->h) &&
43365 ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
43366 pFile->lastErrno = lastErrno;
43367 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
43368 "winTruncate2", pFile->zPath);
43369 }
43370
43371#if SQLITE_MAX_MMAP_SIZE>0
43372 if( rc==SQLITE_OK && oldMmapSize>0 ){
43373 if( oldMmapSize>nByte ){
43374 winMapfile(pFile, -1);
43375 }else{
43376 winMapfile(pFile, oldMmapSize);
43377 }
43378 }
43379#endif
43380
43381 OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, rc=%s\n",
43382 osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
43383 return rc;
43384}
43385
43386#ifdef SQLITE_TEST
43387/*
43388** Count the number of fullsyncs and normal syncs. This is used to test
43389** that syncs and fullsyncs are occuring at the right times.
43390*/
43391SQLITE_API int sqlite3_sync_count = 0;
43392SQLITE_API int sqlite3_fullsync_count = 0;
43393#endif
43394
43395/*
43396** Make sure all writes to a particular file are committed to disk.
43397*/
43398static int winSync(sqlite3_file *id, int flags){
43399#ifndef SQLITE_NO_SYNC
43400 /*
43401 ** Used only when SQLITE_NO_SYNC is not defined.
43402 */
43403 BOOL rc;
43404#endif
43405#if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || \
43406 defined(SQLITE_HAVE_OS_TRACE)
43407 /*
43408 ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or
43409 ** OSTRACE() macros.
43410 */
43411 winFile *pFile = (winFile*)id;
43412#else
43413 UNUSED_PARAMETER(id);
43414#endif
43415
43416 assert( pFile );
43417 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
43418 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
43419 || (flags&0x0F)==SQLITE_SYNC_FULL
43420 );
43421
43422 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
43423 ** line is to test that doing so does not cause any problems.
43424 */
43425 SimulateDiskfullError( return SQLITE_FULL );
43426
43427 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, flags=%x, lock=%d\n",
43428 osGetCurrentProcessId(), pFile, pFile->h, flags,
43429 pFile->locktype));
43430
43431#ifndef SQLITE_TEST
43432 UNUSED_PARAMETER(flags);
43433#else
43434 if( (flags&0x0F)==SQLITE_SYNC_FULL ){
43435 sqlite3_fullsync_count++;
43436 }
43437 sqlite3_sync_count++;
43438#endif
43439
43440 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
43441 ** no-op
43442 */
43443#ifdef SQLITE_NO_SYNC
43444 OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
43445 osGetCurrentProcessId(), pFile, pFile->h));
43446 return SQLITE_OK;
43447#else
43448#if SQLITE_MAX_MMAP_SIZE>0
43449 if( pFile->pMapRegion ){
43450 if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
43451 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
43452 "rc=SQLITE_OK\n", osGetCurrentProcessId(),
43453 pFile, pFile->pMapRegion));
43454 }else{
43455 pFile->lastErrno = osGetLastError();
43456 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
43457 "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(),
43458 pFile, pFile->pMapRegion));
43459 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
43460 "winSync1", pFile->zPath);
43461 }
43462 }
43463#endif
43464 rc = osFlushFileBuffers(pFile->h);
43465 SimulateIOError( rc=FALSE );
43466 if( rc ){
43467 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
43468 osGetCurrentProcessId(), pFile, pFile->h));
43469 return SQLITE_OK;
43470 }else{
43471 pFile->lastErrno = osGetLastError();
43472 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_FSYNC\n",
43473 osGetCurrentProcessId(), pFile, pFile->h));
43474 return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
43475 "winSync2", pFile->zPath);
43476 }
43477#endif
43478}
43479
43480/*
43481** Determine the current size of a file in bytes
43482*/
43483static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
43484 winFile *pFile = (winFile*)id;
43485 int rc = SQLITE_OK;
43486
43487 assert( id!=0 );
43488 assert( pSize!=0 );
43489 SimulateIOError(return SQLITE_IOERR_FSTAT);
43490 OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
43491
43492#if SQLITE_OS_WINRT
43493 {
43494 FILE_STANDARD_INFO info;
43495 if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
43496 &info, sizeof(info)) ){
43497 *pSize = info.EndOfFile.QuadPart;
43498 }else{
43499 pFile->lastErrno = osGetLastError();
43500 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
43501 "winFileSize", pFile->zPath);
43502 }
43503 }
43504#else
43505 {
43506 DWORD upperBits;
43507 DWORD lowerBits;
43508 DWORD lastErrno;
43509
43510 lowerBits = osGetFileSize(pFile->h, &upperBits);
43511 *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
43512 if( (lowerBits == INVALID_FILE_SIZE)
43513 && ((lastErrno = osGetLastError())!=NO_ERROR) ){
43514 pFile->lastErrno = lastErrno;
43515 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
43516 "winFileSize", pFile->zPath);
43517 }
43518 }
43519#endif
43520 OSTRACE(("SIZE file=%p, pSize=%p, *pSize=%lld, rc=%s\n",
43521 pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
43522 return rc;
43523}
43524
43525/*
43526** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
43527*/
43528#ifndef LOCKFILE_FAIL_IMMEDIATELY
43529# define LOCKFILE_FAIL_IMMEDIATELY 1
43530#endif
43531
43532#ifndef LOCKFILE_EXCLUSIVE_LOCK
43533# define LOCKFILE_EXCLUSIVE_LOCK 2
43534#endif
43535
43536/*
43537** Historically, SQLite has used both the LockFile and LockFileEx functions.
43538** When the LockFile function was used, it was always expected to fail
43539** immediately if the lock could not be obtained. Also, it always expected to
43540** obtain an exclusive lock. These flags are used with the LockFileEx function
43541** and reflect those expectations; therefore, they should not be changed.
43542*/
43543#ifndef SQLITE_LOCKFILE_FLAGS
43544# define SQLITE_LOCKFILE_FLAGS (LOCKFILE_FAIL_IMMEDIATELY | \
43545 LOCKFILE_EXCLUSIVE_LOCK)
43546#endif
43547
43548/*
43549** Currently, SQLite never calls the LockFileEx function without wanting the
43550** call to fail immediately if the lock cannot be obtained.
43551*/
43552#ifndef SQLITE_LOCKFILEEX_FLAGS
43553# define SQLITE_LOCKFILEEX_FLAGS (LOCKFILE_FAIL_IMMEDIATELY)
43554#endif
43555
43556/*
43557** Acquire a reader lock.
43558** Different API routines are called depending on whether or not this
43559** is Win9x or WinNT.
43560*/
43561static int winGetReadLock(winFile *pFile){
43562 int res;
43563 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
43564 if( osIsNT() ){
43565#if SQLITE_OS_WINCE
43566 /*
43567 ** NOTE: Windows CE is handled differently here due its lack of the Win32
43568 ** API LockFileEx.
43569 */
43570 res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
43571#else
43572 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
43573 SHARED_SIZE, 0);
43574#endif
43575 }
43576#ifdef SQLITE_WIN32_HAS_ANSI
43577 else{
43578 int lk;
43579 sqlite3_randomness(sizeof(lk), &lk);
43580 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
43581 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
43582 SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
43583 }
43584#endif
43585 if( res == 0 ){
43586 pFile->lastErrno = osGetLastError();
43587 /* No need to log a failure to lock */
43588 }
43589 OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
43590 return res;
43591}
43592
43593/*
43594** Undo a readlock
43595*/
43596static int winUnlockReadLock(winFile *pFile){
43597 int res;
43598 DWORD lastErrno;
43599 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
43600 if( osIsNT() ){
43601 res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
43602 }
43603#ifdef SQLITE_WIN32_HAS_ANSI
43604 else{
43605 res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
43606 }
43607#endif
43608 if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
43609 pFile->lastErrno = lastErrno;
43610 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
43611 "winUnlockReadLock", pFile->zPath);
43612 }
43613 OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
43614 return res;
43615}
43616
43617/*
43618** Lock the file with the lock specified by parameter locktype - one
43619** of the following:
43620**
43621** (1) SHARED_LOCK
43622** (2) RESERVED_LOCK
43623** (3) PENDING_LOCK
43624** (4) EXCLUSIVE_LOCK
43625**
43626** Sometimes when requesting one lock state, additional lock states
43627** are inserted in between. The locking might fail on one of the later
43628** transitions leaving the lock state different from what it started but
43629** still short of its goal. The following chart shows the allowed
43630** transitions and the inserted intermediate states:
43631**
43632** UNLOCKED -> SHARED
43633** SHARED -> RESERVED
43634** SHARED -> (PENDING) -> EXCLUSIVE
43635** RESERVED -> (PENDING) -> EXCLUSIVE
43636** PENDING -> EXCLUSIVE
43637**
43638** This routine will only increase a lock. The winUnlock() routine
43639** erases all locks at once and returns us immediately to locking level 0.
43640** It is not possible to lower the locking level one step at a time. You
43641** must go straight to locking level 0.
43642*/
43643static int winLock(sqlite3_file *id, int locktype){
43644 int rc = SQLITE_OK; /* Return code from subroutines */
43645 int res = 1; /* Result of a Windows lock call */
43646 int newLocktype; /* Set pFile->locktype to this value before exiting */
43647 int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
43648 winFile *pFile = (winFile*)id;
43649 DWORD lastErrno = NO_ERROR;
43650
43651 assert( id!=0 );
43652 OSTRACE(("LOCK file=%p, oldLock=%d(%d), newLock=%d\n",
43653 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
43654
43655 /* If there is already a lock of this type or more restrictive on the
43656 ** OsFile, do nothing. Don't use the end_lock: exit path, as
43657 ** sqlite3OsEnterMutex() hasn't been called yet.
43658 */
43659 if( pFile->locktype>=locktype ){
43660 OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
43661 return SQLITE_OK;
43662 }
43663
43664 /* Do not allow any kind of write-lock on a read-only database
43665 */
43666 if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){
43667 return SQLITE_IOERR_LOCK;
43668 }
43669
43670 /* Make sure the locking sequence is correct
43671 */
43672 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
43673 assert( locktype!=PENDING_LOCK );
43674 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
43675
43676 /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
43677 ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of
43678 ** the PENDING_LOCK byte is temporary.
43679 */
43680 newLocktype = pFile->locktype;
43681 if( pFile->locktype==NO_LOCK
43682 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK)
43683 ){
43684 int cnt = 3;
43685 while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
43686 PENDING_BYTE, 0, 1, 0))==0 ){
43687 /* Try 3 times to get the pending lock. This is needed to work
43688 ** around problems caused by indexing and/or anti-virus software on
43689 ** Windows systems.
43690 ** If you are using this code as a model for alternative VFSes, do not
43691 ** copy this retry logic. It is a hack intended for Windows only.
43692 */
43693 lastErrno = osGetLastError();
43694 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
43695 pFile->h, cnt, res));
43696 if( lastErrno==ERROR_INVALID_HANDLE ){
43697 pFile->lastErrno = lastErrno;
43698 rc = SQLITE_IOERR_LOCK;
43699 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
43700 pFile->h, cnt, sqlite3ErrName(rc)));
43701 return rc;
43702 }
43703 if( cnt ) sqlite3_win32_sleep(1);
43704 }
43705 gotPendingLock = res;
43706 if( !res ){
43707 lastErrno = osGetLastError();
43708 }
43709 }
43710
43711 /* Acquire a shared lock
43712 */
43713 if( locktype==SHARED_LOCK && res ){
43714 assert( pFile->locktype==NO_LOCK );
43715 res = winGetReadLock(pFile);
43716 if( res ){
43717 newLocktype = SHARED_LOCK;
43718 }else{
43719 lastErrno = osGetLastError();
43720 }
43721 }
43722
43723 /* Acquire a RESERVED lock
43724 */
43725 if( locktype==RESERVED_LOCK && res ){
43726 assert( pFile->locktype==SHARED_LOCK );
43727 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
43728 if( res ){
43729 newLocktype = RESERVED_LOCK;
43730 }else{
43731 lastErrno = osGetLastError();
43732 }
43733 }
43734
43735 /* Acquire a PENDING lock
43736 */
43737 if( locktype==EXCLUSIVE_LOCK && res ){
43738 newLocktype = PENDING_LOCK;
43739 gotPendingLock = 0;
43740 }
43741
43742 /* Acquire an EXCLUSIVE lock
43743 */
43744 if( locktype==EXCLUSIVE_LOCK && res ){
43745 assert( pFile->locktype>=SHARED_LOCK );
43746 res = winUnlockReadLock(pFile);
43747 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
43748 SHARED_SIZE, 0);
43749 if( res ){
43750 newLocktype = EXCLUSIVE_LOCK;
43751 }else{
43752 lastErrno = osGetLastError();
43753 winGetReadLock(pFile);
43754 }
43755 }
43756
43757 /* If we are holding a PENDING lock that ought to be released, then
43758 ** release it now.
43759 */
43760 if( gotPendingLock && locktype==SHARED_LOCK ){
43761 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
43762 }
43763
43764 /* Update the state of the lock has held in the file descriptor then
43765 ** return the appropriate result code.
43766 */
43767 if( res ){
43768 rc = SQLITE_OK;
43769 }else{
43770 pFile->lastErrno = lastErrno;
43771 rc = SQLITE_BUSY;
43772 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
43773 pFile->h, locktype, newLocktype));
43774 }
43775 pFile->locktype = (u8)newLocktype;
43776 OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n",
43777 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
43778 return rc;
43779}
43780
43781/*
43782** This routine checks if there is a RESERVED lock held on the specified
43783** file by this or any other process. If such a lock is held, return
43784** non-zero, otherwise zero.
43785*/
43786static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
43787 int res;
43788 winFile *pFile = (winFile*)id;
43789
43790 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
43791 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
43792
43793 assert( id!=0 );
43794 if( pFile->locktype>=RESERVED_LOCK ){
43795 res = 1;
43796 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
43797 }else{
43798 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0);
43799 if( res ){
43800 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
43801 }
43802 res = !res;
43803 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
43804 }
43805 *pResOut = res;
43806 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
43807 pFile->h, pResOut, *pResOut));
43808 return SQLITE_OK;
43809}
43810
43811/*
43812** Lower the locking level on file descriptor id to locktype. locktype
43813** must be either NO_LOCK or SHARED_LOCK.
43814**
43815** If the locking level of the file descriptor is already at or below
43816** the requested locking level, this routine is a no-op.
43817**
43818** It is not possible for this routine to fail if the second argument
43819** is NO_LOCK. If the second argument is SHARED_LOCK then this routine
43820** might return SQLITE_IOERR;
43821*/
43822static int winUnlock(sqlite3_file *id, int locktype){
43823 int type;
43824 winFile *pFile = (winFile*)id;
43825 int rc = SQLITE_OK;
43826 assert( pFile!=0 );
43827 assert( locktype<=SHARED_LOCK );
43828 OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
43829 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
43830 type = pFile->locktype;
43831 if( type>=EXCLUSIVE_LOCK ){
43832 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
43833 if( locktype==SHARED_LOCK && !winGetReadLock(pFile) ){
43834 /* This should never happen. We should always be able to
43835 ** reacquire the read lock */
43836 rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
43837 "winUnlock", pFile->zPath);
43838 }
43839 }
43840 if( type>=RESERVED_LOCK ){
43841 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
43842 }
43843 if( locktype==NO_LOCK && type>=SHARED_LOCK ){
43844 winUnlockReadLock(pFile);
43845 }
43846 if( type>=PENDING_LOCK ){
43847 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
43848 }
43849 pFile->locktype = (u8)locktype;
43850 OSTRACE(("UNLOCK file=%p, lock=%d, rc=%s\n",
43851 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
43852 return rc;
43853}
43854
43855/******************************************************************************
43856****************************** No-op Locking **********************************
43857**
43858** Of the various locking implementations available, this is by far the
43859** simplest: locking is ignored. No attempt is made to lock the database
43860** file for reading or writing.
43861**
43862** This locking mode is appropriate for use on read-only databases
43863** (ex: databases that are burned into CD-ROM, for example.) It can
43864** also be used if the application employs some external mechanism to
43865** prevent simultaneous access of the same database by two or more
43866** database connections. But there is a serious risk of database
43867** corruption if this locking mode is used in situations where multiple
43868** database connections are accessing the same database file at the same
43869** time and one or more of those connections are writing.
43870*/
43871
43872static int winNolockLock(sqlite3_file *id, int locktype){
43873 UNUSED_PARAMETER(id);
43874 UNUSED_PARAMETER(locktype);
43875 return SQLITE_OK;
43876}
43877
43878static int winNolockCheckReservedLock(sqlite3_file *id, int *pResOut){
43879 UNUSED_PARAMETER(id);
43880 UNUSED_PARAMETER(pResOut);
43881 return SQLITE_OK;
43882}
43883
43884static int winNolockUnlock(sqlite3_file *id, int locktype){
43885 UNUSED_PARAMETER(id);
43886 UNUSED_PARAMETER(locktype);
43887 return SQLITE_OK;
43888}
43889
43890/******************* End of the no-op lock implementation *********************
43891******************************************************************************/
43892
43893/*
43894** If *pArg is initially negative then this is a query. Set *pArg to
43895** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
43896**
43897** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
43898*/
43899static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
43900 if( *pArg<0 ){
43901 *pArg = (pFile->ctrlFlags & mask)!=0;
43902 }else if( (*pArg)==0 ){
43903 pFile->ctrlFlags &= ~mask;
43904 }else{
43905 pFile->ctrlFlags |= mask;
43906 }
43907}
43908
43909/* Forward references to VFS helper methods used for temporary files */
43910static int winGetTempname(sqlite3_vfs *, char **);
43911static int winIsDir(const void *);
43912static BOOL winIsDriveLetterAndColon(const char *);
43913
43914/*
43915** Control and query of the open file handle.
43916*/
43917static int winFileControl(sqlite3_file *id, int op, void *pArg){
43918 winFile *pFile = (winFile*)id;
43919 OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
43920 switch( op ){
43921 case SQLITE_FCNTL_LOCKSTATE: {
43922 *(int*)pArg = pFile->locktype;
43923 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
43924 return SQLITE_OK;
43925 }
43926 case SQLITE_FCNTL_LAST_ERRNO: {
43927 *(int*)pArg = (int)pFile->lastErrno;
43928 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
43929 return SQLITE_OK;
43930 }
43931 case SQLITE_FCNTL_CHUNK_SIZE: {
43932 pFile->szChunk = *(int *)pArg;
43933 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
43934 return SQLITE_OK;
43935 }
43936 case SQLITE_FCNTL_SIZE_HINT: {
43937 if( pFile->szChunk>0 ){
43938 sqlite3_int64 oldSz;
43939 int rc = winFileSize(id, &oldSz);
43940 if( rc==SQLITE_OK ){
43941 sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
43942 if( newSz>oldSz ){
43943 SimulateIOErrorBenign(1);
43944 rc = winTruncate(id, newSz);
43945 SimulateIOErrorBenign(0);
43946 }
43947 }
43948 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
43949 return rc;
43950 }
43951 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
43952 return SQLITE_OK;
43953 }
43954 case SQLITE_FCNTL_PERSIST_WAL: {
43955 winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg);
43956 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
43957 return SQLITE_OK;
43958 }
43959 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
43960 winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
43961 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
43962 return SQLITE_OK;
43963 }
43964 case SQLITE_FCNTL_VFSNAME: {
43965 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
43966 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
43967 return SQLITE_OK;
43968 }
43969 case SQLITE_FCNTL_WIN32_AV_RETRY: {
43970 int *a = (int*)pArg;
43971 if( a[0]>0 ){
43972 winIoerrRetry = a[0];
43973 }else{
43974 a[0] = winIoerrRetry;
43975 }
43976 if( a[1]>0 ){
43977 winIoerrRetryDelay = a[1];
43978 }else{
43979 a[1] = winIoerrRetryDelay;
43980 }
43981 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
43982 return SQLITE_OK;
43983 }
43984 case SQLITE_FCNTL_WIN32_GET_HANDLE: {
43985 LPHANDLE phFile = (LPHANDLE)pArg;
43986 *phFile = pFile->h;
43987 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
43988 return SQLITE_OK;
43989 }
43990#ifdef SQLITE_TEST
43991 case SQLITE_FCNTL_WIN32_SET_HANDLE: {
43992 LPHANDLE phFile = (LPHANDLE)pArg;
43993 HANDLE hOldFile = pFile->h;
43994 pFile->h = *phFile;
43995 *phFile = hOldFile;
43996 OSTRACE(("FCNTL oldFile=%p, newFile=%p, rc=SQLITE_OK\n",
43997 hOldFile, pFile->h));
43998 return SQLITE_OK;
43999 }
44000#endif
44001 case SQLITE_FCNTL_TEMPFILENAME: {
44002 char *zTFile = 0;
44003 int rc = winGetTempname(pFile->pVfs, &zTFile);
44004 if( rc==SQLITE_OK ){
44005 *(char**)pArg = zTFile;
44006 }
44007 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
44008 return rc;
44009 }
44010#if SQLITE_MAX_MMAP_SIZE>0
44011 case SQLITE_FCNTL_MMAP_SIZE: {
44012 i64 newLimit = *(i64*)pArg;
44013 int rc = SQLITE_OK;
44014 if( newLimit>sqlite3GlobalConfig.mxMmap ){
44015 newLimit = sqlite3GlobalConfig.mxMmap;
44016 }
44017
44018 /* The value of newLimit may be eventually cast to (SIZE_T) and passed
44019 ** to MapViewOfFile(). Restrict its value to 2GB if (SIZE_T) is not at
44020 ** least a 64-bit type. */
44021 if( newLimit>0 && sizeof(SIZE_T)<8 ){
44022 newLimit = (newLimit & 0x7FFFFFFF);
44023 }
44024
44025 *(i64*)pArg = pFile->mmapSizeMax;
44026 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
44027 pFile->mmapSizeMax = newLimit;
44028 if( pFile->mmapSize>0 ){
44029 winUnmapfile(pFile);
44030 rc = winMapfile(pFile, -1);
44031 }
44032 }
44033 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
44034 return rc;
44035 }
44036#endif
44037 }
44038 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
44039 return SQLITE_NOTFOUND;
44040}
44041
44042/*
44043** Return the sector size in bytes of the underlying block device for
44044** the specified file. This is almost always 512 bytes, but may be
44045** larger for some devices.
44046**
44047** SQLite code assumes this function cannot fail. It also assumes that
44048** if two files are created in the same file-system directory (i.e.
44049** a database and its journal file) that the sector size will be the
44050** same for both.
44051*/
44052static int winSectorSize(sqlite3_file *id){
44053 (void)id;
44054 return SQLITE_DEFAULT_SECTOR_SIZE;
44055}
44056
44057/*
44058** Return a vector of device characteristics.
44059*/
44060static int winDeviceCharacteristics(sqlite3_file *id){
44061 winFile *p = (winFile*)id;
44062 return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
44063 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
44064}
44065
44066/*
44067** Windows will only let you create file view mappings
44068** on allocation size granularity boundaries.
44069** During sqlite3_os_init() we do a GetSystemInfo()
44070** to get the granularity size.
44071*/
44072static SYSTEM_INFO winSysInfo;
44073
44074#ifndef SQLITE_OMIT_WAL
44075
44076/*
44077** Helper functions to obtain and relinquish the global mutex. The
44078** global mutex is used to protect the winLockInfo objects used by
44079** this file, all of which may be shared by multiple threads.
44080**
44081** Function winShmMutexHeld() is used to assert() that the global mutex
44082** is held when required. This function is only used as part of assert()
44083** statements. e.g.
44084**
44085** winShmEnterMutex()
44086** assert( winShmMutexHeld() );
44087** winShmLeaveMutex()
44088*/
44089static sqlite3_mutex *winBigLock = 0;
44090static void winShmEnterMutex(void){
44091 sqlite3_mutex_enter(winBigLock);
44092}
44093static void winShmLeaveMutex(void){
44094 sqlite3_mutex_leave(winBigLock);
44095}
44096#ifndef NDEBUG
44097static int winShmMutexHeld(void) {
44098 return sqlite3_mutex_held(winBigLock);
44099}
44100#endif
44101
44102/*
44103** Object used to represent a single file opened and mmapped to provide
44104** shared memory. When multiple threads all reference the same
44105** log-summary, each thread has its own winFile object, but they all
44106** point to a single instance of this object. In other words, each
44107** log-summary is opened only once per process.
44108**
44109** winShmMutexHeld() must be true when creating or destroying
44110** this object or while reading or writing the following fields:
44111**
44112** nRef
44113** pNext
44114**
44115** The following fields are read-only after the object is created:
44116**
44117** fid
44118** zFilename
44119**
44120** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
44121** winShmMutexHeld() is true when reading or writing any other field
44122** in this structure.
44123**
44124*/
44125struct winShmNode {
44126 sqlite3_mutex *mutex; /* Mutex to access this object */
44127 char *zFilename; /* Name of the file */
44128 winFile hFile; /* File handle from winOpen */
44129
44130 int szRegion; /* Size of shared-memory regions */
44131 int nRegion; /* Size of array apRegion */
44132 u8 isReadonly; /* True if read-only */
44133 u8 isUnlocked; /* True if no DMS lock held */
44134
44135 struct ShmRegion {
44136 HANDLE hMap; /* File handle from CreateFileMapping */
44137 void *pMap;
44138 } *aRegion;
44139 DWORD lastErrno; /* The Windows errno from the last I/O error */
44140
44141 int nRef; /* Number of winShm objects pointing to this */
44142 winShm *pFirst; /* All winShm objects pointing to this */
44143 winShmNode *pNext; /* Next in list of all winShmNode objects */
44144#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
44145 u8 nextShmId; /* Next available winShm.id value */
44146#endif
44147};
44148
44149/*
44150** A global array of all winShmNode objects.
44151**
44152** The winShmMutexHeld() must be true while reading or writing this list.
44153*/
44154static winShmNode *winShmNodeList = 0;
44155
44156/*
44157** Structure used internally by this VFS to record the state of an
44158** open shared memory connection.
44159**
44160** The following fields are initialized when this object is created and
44161** are read-only thereafter:
44162**
44163** winShm.pShmNode
44164** winShm.id
44165**
44166** All other fields are read/write. The winShm.pShmNode->mutex must be held
44167** while accessing any read/write fields.
44168*/
44169struct winShm {
44170 winShmNode *pShmNode; /* The underlying winShmNode object */
44171 winShm *pNext; /* Next winShm with the same winShmNode */
44172 u8 hasMutex; /* True if holding the winShmNode mutex */
44173 u16 sharedMask; /* Mask of shared locks held */
44174 u16 exclMask; /* Mask of exclusive locks held */
44175#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
44176 u8 id; /* Id of this connection with its winShmNode */
44177#endif
44178};
44179
44180/*
44181** Constants used for locking
44182*/
44183#define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
44184#define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
44185
44186/*
44187** Apply advisory locks for all n bytes beginning at ofst.
44188*/
44189#define WINSHM_UNLCK 1
44190#define WINSHM_RDLCK 2
44191#define WINSHM_WRLCK 3
44192static int winShmSystemLock(
44193 winShmNode *pFile, /* Apply locks to this open shared-memory segment */
44194 int lockType, /* WINSHM_UNLCK, WINSHM_RDLCK, or WINSHM_WRLCK */
44195 int ofst, /* Offset to first byte to be locked/unlocked */
44196 int nByte /* Number of bytes to lock or unlock */
44197){
44198 int rc = 0; /* Result code form Lock/UnlockFileEx() */
44199
44200 /* Access to the winShmNode object is serialized by the caller */
44201 assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) );
44202
44203 OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
44204 pFile->hFile.h, lockType, ofst, nByte));
44205
44206 /* Release/Acquire the system-level lock */
44207 if( lockType==WINSHM_UNLCK ){
44208 rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
44209 }else{
44210 /* Initialize the locking parameters */
44211 DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
44212 if( lockType == WINSHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
44213 rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
44214 }
44215
44216 if( rc!= 0 ){
44217 rc = SQLITE_OK;
44218 }else{
44219 pFile->lastErrno = osGetLastError();
44220 rc = SQLITE_BUSY;
44221 }
44222
44223 OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
44224 pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
44225 "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
44226
44227 return rc;
44228}
44229
44230/* Forward references to VFS methods */
44231static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
44232static int winDelete(sqlite3_vfs *,const char*,int);
44233
44234/*
44235** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
44236**
44237** This is not a VFS shared-memory method; it is a utility function called
44238** by VFS shared-memory methods.
44239*/
44240static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
44241 winShmNode **pp;
44242 winShmNode *p;
44243 assert( winShmMutexHeld() );
44244 OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
44245 osGetCurrentProcessId(), deleteFlag));
44246 pp = &winShmNodeList;
44247 while( (p = *pp)!=0 ){
44248 if( p->nRef==0 ){
44249 int i;
44250 if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
44251 for(i=0; i<p->nRegion; i++){
44252 BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
44253 OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
44254 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
44255 UNUSED_VARIABLE_VALUE(bRc);
44256 bRc = osCloseHandle(p->aRegion[i].hMap);
44257 OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
44258 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
44259 UNUSED_VARIABLE_VALUE(bRc);
44260 }
44261 if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
44262 SimulateIOErrorBenign(1);
44263 winClose((sqlite3_file *)&p->hFile);
44264 SimulateIOErrorBenign(0);
44265 }
44266 if( deleteFlag ){
44267 SimulateIOErrorBenign(1);
44268 sqlite3BeginBenignMalloc();
44269 winDelete(pVfs, p->zFilename, 0);
44270 sqlite3EndBenignMalloc();
44271 SimulateIOErrorBenign(0);
44272 }
44273 *pp = p->pNext;
44274 sqlite3_free(p->aRegion);
44275 sqlite3_free(p);
44276 }else{
44277 pp = &p->pNext;
44278 }
44279 }
44280}
44281
44282/*
44283** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
44284** take it now. Return SQLITE_OK if successful, or an SQLite error
44285** code otherwise.
44286**
44287** If the DMS cannot be locked because this is a readonly_shm=1
44288** connection and no other process already holds a lock, return
44289** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
44290*/
44291static int winLockSharedMemory(winShmNode *pShmNode){
44292 int rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, WIN_SHM_DMS, 1);
44293
44294 if( rc==SQLITE_OK ){
44295 if( pShmNode->isReadonly ){
44296 pShmNode->isUnlocked = 1;
44297 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
44298 return SQLITE_READONLY_CANTINIT;
44299 }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){
44300 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
44301 return winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
44302 "winLockSharedMemory", pShmNode->zFilename);
44303 }
44304 }
44305
44306 if( rc==SQLITE_OK ){
44307 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
44308 }
44309
44310 return winShmSystemLock(pShmNode, WINSHM_RDLCK, WIN_SHM_DMS, 1);
44311}
44312
44313/*
44314** Open the shared-memory area associated with database file pDbFd.
44315**
44316** When opening a new shared-memory file, if no other instances of that
44317** file are currently open, in this process or in other processes, then
44318** the file must be truncated to zero length or have its header cleared.
44319*/
44320static int winOpenSharedMemory(winFile *pDbFd){
44321 struct winShm *p; /* The connection to be opened */
44322 winShmNode *pShmNode = 0; /* The underlying mmapped file */
44323 int rc = SQLITE_OK; /* Result code */
44324 winShmNode *pNew; /* Newly allocated winShmNode */
44325 int nName; /* Size of zName in bytes */
44326
44327 assert( pDbFd->pShm==0 ); /* Not previously opened */
44328
44329 /* Allocate space for the new sqlite3_shm object. Also speculatively
44330 ** allocate space for a new winShmNode and filename.
44331 */
44332 p = sqlite3MallocZero( sizeof(*p) );
44333 if( p==0 ) return SQLITE_IOERR_NOMEM_BKPT;
44334 nName = sqlite3Strlen30(pDbFd->zPath);
44335 pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
44336 if( pNew==0 ){
44337 sqlite3_free(p);
44338 return SQLITE_IOERR_NOMEM_BKPT;
44339 }
44340 pNew->zFilename = (char*)&pNew[1];
44341 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
44342 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
44343
44344 /* Look to see if there is an existing winShmNode that can be used.
44345 ** If no matching winShmNode currently exists, create a new one.
44346 */
44347 winShmEnterMutex();
44348 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
44349 /* TBD need to come up with better match here. Perhaps
44350 ** use FILE_ID_BOTH_DIR_INFO Structure.
44351 */
44352 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
44353 }
44354 if( pShmNode ){
44355 sqlite3_free(pNew);
44356 }else{
44357 int inFlags = SQLITE_OPEN_WAL;
44358 int outFlags = 0;
44359
44360 pShmNode = pNew;
44361 pNew = 0;
44362 ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
44363 pShmNode->pNext = winShmNodeList;
44364 winShmNodeList = pShmNode;
44365
44366 if( sqlite3GlobalConfig.bCoreMutex ){
44367 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
44368 if( pShmNode->mutex==0 ){
44369 rc = SQLITE_IOERR_NOMEM_BKPT;
44370 goto shm_open_err;
44371 }
44372 }
44373
44374 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
44375 inFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
44376 }else{
44377 inFlags |= SQLITE_OPEN_READONLY;
44378 }
44379 rc = winOpen(pDbFd->pVfs, pShmNode->zFilename,
44380 (sqlite3_file*)&pShmNode->hFile,
44381 inFlags, &outFlags);
44382 if( rc!=SQLITE_OK ){
44383 rc = winLogError(rc, osGetLastError(), "winOpenShm",
44384 pShmNode->zFilename);
44385 goto shm_open_err;
44386 }
44387 if( outFlags==SQLITE_OPEN_READONLY ) pShmNode->isReadonly = 1;
44388
44389 rc = winLockSharedMemory(pShmNode);
44390 if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err;
44391 }
44392
44393 /* Make the new connection a child of the winShmNode */
44394 p->pShmNode = pShmNode;
44395#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
44396 p->id = pShmNode->nextShmId++;
44397#endif
44398 pShmNode->nRef++;
44399 pDbFd->pShm = p;
44400 winShmLeaveMutex();
44401
44402 /* The reference count on pShmNode has already been incremented under
44403 ** the cover of the winShmEnterMutex() mutex and the pointer from the
44404 ** new (struct winShm) object to the pShmNode has been set. All that is
44405 ** left to do is to link the new object into the linked list starting
44406 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
44407 ** mutex.
44408 */
44409 sqlite3_mutex_enter(pShmNode->mutex);
44410 p->pNext = pShmNode->pFirst;
44411 pShmNode->pFirst = p;
44412 sqlite3_mutex_leave(pShmNode->mutex);
44413 return rc;
44414
44415 /* Jump here on any error */
44416shm_open_err:
44417 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
44418 winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
44419 sqlite3_free(p);
44420 sqlite3_free(pNew);
44421 winShmLeaveMutex();
44422 return rc;
44423}
44424
44425/*
44426** Close a connection to shared-memory. Delete the underlying
44427** storage if deleteFlag is true.
44428*/
44429static int winShmUnmap(
44430 sqlite3_file *fd, /* Database holding shared memory */
44431 int deleteFlag /* Delete after closing if true */
44432){
44433 winFile *pDbFd; /* Database holding shared-memory */
44434 winShm *p; /* The connection to be closed */
44435 winShmNode *pShmNode; /* The underlying shared-memory file */
44436 winShm **pp; /* For looping over sibling connections */
44437
44438 pDbFd = (winFile*)fd;
44439 p = pDbFd->pShm;
44440 if( p==0 ) return SQLITE_OK;
44441 pShmNode = p->pShmNode;
44442
44443 /* Remove connection p from the set of connections associated
44444 ** with pShmNode */
44445 sqlite3_mutex_enter(pShmNode->mutex);
44446 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
44447 *pp = p->pNext;
44448
44449 /* Free the connection p */
44450 sqlite3_free(p);
44451 pDbFd->pShm = 0;
44452 sqlite3_mutex_leave(pShmNode->mutex);
44453
44454 /* If pShmNode->nRef has reached 0, then close the underlying
44455 ** shared-memory file, too */
44456 winShmEnterMutex();
44457 assert( pShmNode->nRef>0 );
44458 pShmNode->nRef--;
44459 if( pShmNode->nRef==0 ){
44460 winShmPurge(pDbFd->pVfs, deleteFlag);
44461 }
44462 winShmLeaveMutex();
44463
44464 return SQLITE_OK;
44465}
44466
44467/*
44468** Change the lock state for a shared-memory segment.
44469*/
44470static int winShmLock(
44471 sqlite3_file *fd, /* Database file holding the shared memory */
44472 int ofst, /* First lock to acquire or release */
44473 int n, /* Number of locks to acquire or release */
44474 int flags /* What to do with the lock */
44475){
44476 winFile *pDbFd = (winFile*)fd; /* Connection holding shared memory */
44477 winShm *p = pDbFd->pShm; /* The shared memory being locked */
44478 winShm *pX; /* For looping over all siblings */
44479 winShmNode *pShmNode = p->pShmNode;
44480 int rc = SQLITE_OK; /* Result code */
44481 u16 mask; /* Mask of locks to take or release */
44482
44483 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
44484 assert( n>=1 );
44485 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
44486 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
44487 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
44488 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
44489 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
44490
44491 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
44492 assert( n>1 || mask==(1<<ofst) );
44493 sqlite3_mutex_enter(pShmNode->mutex);
44494 if( flags & SQLITE_SHM_UNLOCK ){
44495 u16 allMask = 0; /* Mask of locks held by siblings */
44496
44497 /* See if any siblings hold this same lock */
44498 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
44499 if( pX==p ) continue;
44500 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
44501 allMask |= pX->sharedMask;
44502 }
44503
44504 /* Unlock the system-level locks */
44505 if( (mask & allMask)==0 ){
44506 rc = winShmSystemLock(pShmNode, WINSHM_UNLCK, ofst+WIN_SHM_BASE, n);
44507 }else{
44508 rc = SQLITE_OK;
44509 }
44510
44511 /* Undo the local locks */
44512 if( rc==SQLITE_OK ){
44513 p->exclMask &= ~mask;
44514 p->sharedMask &= ~mask;
44515 }
44516 }else if( flags & SQLITE_SHM_SHARED ){
44517 u16 allShared = 0; /* Union of locks held by connections other than "p" */
44518
44519 /* Find out which shared locks are already held by sibling connections.
44520 ** If any sibling already holds an exclusive lock, go ahead and return
44521 ** SQLITE_BUSY.
44522 */
44523 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
44524 if( (pX->exclMask & mask)!=0 ){
44525 rc = SQLITE_BUSY;
44526 break;
44527 }
44528 allShared |= pX->sharedMask;
44529 }
44530
44531 /* Get shared locks at the system level, if necessary */
44532 if( rc==SQLITE_OK ){
44533 if( (allShared & mask)==0 ){
44534 rc = winShmSystemLock(pShmNode, WINSHM_RDLCK, ofst+WIN_SHM_BASE, n);
44535 }else{
44536 rc = SQLITE_OK;
44537 }
44538 }
44539
44540 /* Get the local shared locks */
44541 if( rc==SQLITE_OK ){
44542 p->sharedMask |= mask;
44543 }
44544 }else{
44545 /* Make sure no sibling connections hold locks that will block this
44546 ** lock. If any do, return SQLITE_BUSY right away.
44547 */
44548 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
44549 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
44550 rc = SQLITE_BUSY;
44551 break;
44552 }
44553 }
44554
44555 /* Get the exclusive locks at the system level. Then if successful
44556 ** also mark the local connection as being locked.
44557 */
44558 if( rc==SQLITE_OK ){
44559 rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, ofst+WIN_SHM_BASE, n);
44560 if( rc==SQLITE_OK ){
44561 assert( (p->sharedMask & mask)==0 );
44562 p->exclMask |= mask;
44563 }
44564 }
44565 }
44566 sqlite3_mutex_leave(pShmNode->mutex);
44567 OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
44568 osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
44569 sqlite3ErrName(rc)));
44570 return rc;
44571}
44572
44573/*
44574** Implement a memory barrier or memory fence on shared memory.
44575**
44576** All loads and stores begun before the barrier must complete before
44577** any load or store begun after the barrier.
44578*/
44579static void winShmBarrier(
44580 sqlite3_file *fd /* Database holding the shared memory */
44581){
44582 UNUSED_PARAMETER(fd);
44583 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
44584 winShmEnterMutex(); /* Also mutex, for redundancy */
44585 winShmLeaveMutex();
44586}
44587
44588/*
44589** This function is called to obtain a pointer to region iRegion of the
44590** shared-memory associated with the database file fd. Shared-memory regions
44591** are numbered starting from zero. Each shared-memory region is szRegion
44592** bytes in size.
44593**
44594** If an error occurs, an error code is returned and *pp is set to NULL.
44595**
44596** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
44597** region has not been allocated (by any client, including one running in a
44598** separate process), then *pp is set to NULL and SQLITE_OK returned. If
44599** isWrite is non-zero and the requested shared-memory region has not yet
44600** been allocated, it is allocated by this function.
44601**
44602** If the shared-memory region has already been allocated or is allocated by
44603** this call as described above, then it is mapped into this processes
44604** address space (if it is not already), *pp is set to point to the mapped
44605** memory and SQLITE_OK returned.
44606*/
44607static int winShmMap(
44608 sqlite3_file *fd, /* Handle open on database file */
44609 int iRegion, /* Region to retrieve */
44610 int szRegion, /* Size of regions */
44611 int isWrite, /* True to extend file if necessary */
44612 void volatile **pp /* OUT: Mapped memory */
44613){
44614 winFile *pDbFd = (winFile*)fd;
44615 winShm *pShm = pDbFd->pShm;
44616 winShmNode *pShmNode;
44617 DWORD protect = PAGE_READWRITE;
44618 DWORD flags = FILE_MAP_WRITE | FILE_MAP_READ;
44619 int rc = SQLITE_OK;
44620
44621 if( !pShm ){
44622 rc = winOpenSharedMemory(pDbFd);
44623 if( rc!=SQLITE_OK ) return rc;
44624 pShm = pDbFd->pShm;
44625 }
44626 pShmNode = pShm->pShmNode;
44627
44628 sqlite3_mutex_enter(pShmNode->mutex);
44629 if( pShmNode->isUnlocked ){
44630 rc = winLockSharedMemory(pShmNode);
44631 if( rc!=SQLITE_OK ) goto shmpage_out;
44632 pShmNode->isUnlocked = 0;
44633 }
44634 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
44635
44636 if( pShmNode->nRegion<=iRegion ){
44637 struct ShmRegion *apNew; /* New aRegion[] array */
44638 int nByte = (iRegion+1)*szRegion; /* Minimum required file size */
44639 sqlite3_int64 sz; /* Current size of wal-index file */
44640
44641 pShmNode->szRegion = szRegion;
44642
44643 /* The requested region is not mapped into this processes address space.
44644 ** Check to see if it has been allocated (i.e. if the wal-index file is
44645 ** large enough to contain the requested region).
44646 */
44647 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
44648 if( rc!=SQLITE_OK ){
44649 rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
44650 "winShmMap1", pDbFd->zPath);
44651 goto shmpage_out;
44652 }
44653
44654 if( sz<nByte ){
44655 /* The requested memory region does not exist. If isWrite is set to
44656 ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
44657 **
44658 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
44659 ** the requested memory region.
44660 */
44661 if( !isWrite ) goto shmpage_out;
44662 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
44663 if( rc!=SQLITE_OK ){
44664 rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
44665 "winShmMap2", pDbFd->zPath);
44666 goto shmpage_out;
44667 }
44668 }
44669
44670 /* Map the requested memory region into this processes address space. */
44671 apNew = (struct ShmRegion *)sqlite3_realloc64(
44672 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
44673 );
44674 if( !apNew ){
44675 rc = SQLITE_IOERR_NOMEM_BKPT;
44676 goto shmpage_out;
44677 }
44678 pShmNode->aRegion = apNew;
44679
44680 if( pShmNode->isReadonly ){
44681 protect = PAGE_READONLY;
44682 flags = FILE_MAP_READ;
44683 }
44684
44685 while( pShmNode->nRegion<=iRegion ){
44686 HANDLE hMap = NULL; /* file-mapping handle */
44687 void *pMap = 0; /* Mapped memory region */
44688
44689#if SQLITE_OS_WINRT
44690 hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
44691 NULL, protect, nByte, NULL
44692 );
44693#elif defined(SQLITE_WIN32_HAS_WIDE)
44694 hMap = osCreateFileMappingW(pShmNode->hFile.h,
44695 NULL, protect, 0, nByte, NULL
44696 );
44697#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
44698 hMap = osCreateFileMappingA(pShmNode->hFile.h,
44699 NULL, protect, 0, nByte, NULL
44700 );
44701#endif
44702 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
44703 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
44704 hMap ? "ok" : "failed"));
44705 if( hMap ){
44706 int iOffset = pShmNode->nRegion*szRegion;
44707 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
44708#if SQLITE_OS_WINRT
44709 pMap = osMapViewOfFileFromApp(hMap, flags,
44710 iOffset - iOffsetShift, szRegion + iOffsetShift
44711 );
44712#else
44713 pMap = osMapViewOfFile(hMap, flags,
44714 0, iOffset - iOffsetShift, szRegion + iOffsetShift
44715 );
44716#endif
44717 OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
44718 osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
44719 szRegion, pMap ? "ok" : "failed"));
44720 }
44721 if( !pMap ){
44722 pShmNode->lastErrno = osGetLastError();
44723 rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
44724 "winShmMap3", pDbFd->zPath);
44725 if( hMap ) osCloseHandle(hMap);
44726 goto shmpage_out;
44727 }
44728
44729 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
44730 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
44731 pShmNode->nRegion++;
44732 }
44733 }
44734
44735shmpage_out:
44736 if( pShmNode->nRegion>iRegion ){
44737 int iOffset = iRegion*szRegion;
44738 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
44739 char *p = (char *)pShmNode->aRegion[iRegion].pMap;
44740 *pp = (void *)&p[iOffsetShift];
44741 }else{
44742 *pp = 0;
44743 }
44744 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
44745 sqlite3_mutex_leave(pShmNode->mutex);
44746 return rc;
44747}
44748
44749#else
44750# define winShmMap 0
44751# define winShmLock 0
44752# define winShmBarrier 0
44753# define winShmUnmap 0
44754#endif /* #ifndef SQLITE_OMIT_WAL */
44755
44756/*
44757** Cleans up the mapped region of the specified file, if any.
44758*/
44759#if SQLITE_MAX_MMAP_SIZE>0
44760static int winUnmapfile(winFile *pFile){
44761 assert( pFile!=0 );
44762 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
44763 "mmapSize=%lld, mmapSizeMax=%lld\n",
44764 osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
44765 pFile->mmapSize, pFile->mmapSizeMax));
44766 if( pFile->pMapRegion ){
44767 if( !osUnmapViewOfFile(pFile->pMapRegion) ){
44768 pFile->lastErrno = osGetLastError();
44769 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
44770 "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile,
44771 pFile->pMapRegion));
44772 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
44773 "winUnmapfile1", pFile->zPath);
44774 }
44775 pFile->pMapRegion = 0;
44776 pFile->mmapSize = 0;
44777 }
44778 if( pFile->hMap!=NULL ){
44779 if( !osCloseHandle(pFile->hMap) ){
44780 pFile->lastErrno = osGetLastError();
44781 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
44782 osGetCurrentProcessId(), pFile, pFile->hMap));
44783 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
44784 "winUnmapfile2", pFile->zPath);
44785 }
44786 pFile->hMap = NULL;
44787 }
44788 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
44789 osGetCurrentProcessId(), pFile));
44790 return SQLITE_OK;
44791}
44792
44793/*
44794** Memory map or remap the file opened by file-descriptor pFd (if the file
44795** is already mapped, the existing mapping is replaced by the new). Or, if
44796** there already exists a mapping for this file, and there are still
44797** outstanding xFetch() references to it, this function is a no-op.
44798**
44799** If parameter nByte is non-negative, then it is the requested size of
44800** the mapping to create. Otherwise, if nByte is less than zero, then the
44801** requested size is the size of the file on disk. The actual size of the
44802** created mapping is either the requested size or the value configured
44803** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
44804**
44805** SQLITE_OK is returned if no error occurs (even if the mapping is not
44806** recreated as a result of outstanding references) or an SQLite error
44807** code otherwise.
44808*/
44809static int winMapfile(winFile *pFd, sqlite3_int64 nByte){
44810 sqlite3_int64 nMap = nByte;
44811 int rc;
44812
44813 assert( nMap>=0 || pFd->nFetchOut==0 );
44814 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
44815 osGetCurrentProcessId(), pFd, nByte));
44816
44817 if( pFd->nFetchOut>0 ) return SQLITE_OK;
44818
44819 if( nMap<0 ){
44820 rc = winFileSize((sqlite3_file*)pFd, &nMap);
44821 if( rc ){
44822 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
44823 osGetCurrentProcessId(), pFd));
44824 return SQLITE_IOERR_FSTAT;
44825 }
44826 }
44827 if( nMap>pFd->mmapSizeMax ){
44828 nMap = pFd->mmapSizeMax;
44829 }
44830 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
44831
44832 if( nMap==0 && pFd->mmapSize>0 ){
44833 winUnmapfile(pFd);
44834 }
44835 if( nMap!=pFd->mmapSize ){
44836 void *pNew = 0;
44837 DWORD protect = PAGE_READONLY;
44838 DWORD flags = FILE_MAP_READ;
44839
44840 winUnmapfile(pFd);
44841#ifdef SQLITE_MMAP_READWRITE
44842 if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
44843 protect = PAGE_READWRITE;
44844 flags |= FILE_MAP_WRITE;
44845 }
44846#endif
44847#if SQLITE_OS_WINRT
44848 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
44849#elif defined(SQLITE_WIN32_HAS_WIDE)
44850 pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
44851 (DWORD)((nMap>>32) & 0xffffffff),
44852 (DWORD)(nMap & 0xffffffff), NULL);
44853#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
44854 pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
44855 (DWORD)((nMap>>32) & 0xffffffff),
44856 (DWORD)(nMap & 0xffffffff), NULL);
44857#endif
44858 if( pFd->hMap==NULL ){
44859 pFd->lastErrno = osGetLastError();
44860 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
44861 "winMapfile1", pFd->zPath);
44862 /* Log the error, but continue normal operation using xRead/xWrite */
44863 OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
44864 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
44865 return SQLITE_OK;
44866 }
44867 assert( (nMap % winSysInfo.dwPageSize)==0 );
44868 assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff );
44869#if SQLITE_OS_WINRT
44870 pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
44871#else
44872 pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
44873#endif
44874 if( pNew==NULL ){
44875 osCloseHandle(pFd->hMap);
44876 pFd->hMap = NULL;
44877 pFd->lastErrno = osGetLastError();
44878 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
44879 "winMapfile2", pFd->zPath);
44880 /* Log the error, but continue normal operation using xRead/xWrite */
44881 OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
44882 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
44883 return SQLITE_OK;
44884 }
44885 pFd->pMapRegion = pNew;
44886 pFd->mmapSize = nMap;
44887 }
44888
44889 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
44890 osGetCurrentProcessId(), pFd));
44891 return SQLITE_OK;
44892}
44893#endif /* SQLITE_MAX_MMAP_SIZE>0 */
44894
44895/*
44896** If possible, return a pointer to a mapping of file fd starting at offset
44897** iOff. The mapping must be valid for at least nAmt bytes.
44898**
44899** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
44900** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
44901** Finally, if an error does occur, return an SQLite error code. The final
44902** value of *pp is undefined in this case.
44903**
44904** If this function does return a pointer, the caller must eventually
44905** release the reference by calling winUnfetch().
44906*/
44907static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
44908#if SQLITE_MAX_MMAP_SIZE>0
44909 winFile *pFd = (winFile*)fd; /* The underlying database file */
44910#endif
44911 *pp = 0;
44912
44913 OSTRACE(("FETCH pid=%lu, pFile=%p, offset=%lld, amount=%d, pp=%p\n",
44914 osGetCurrentProcessId(), fd, iOff, nAmt, pp));
44915
44916#if SQLITE_MAX_MMAP_SIZE>0
44917 if( pFd->mmapSizeMax>0 ){
44918 if( pFd->pMapRegion==0 ){
44919 int rc = winMapfile(pFd, -1);
44920 if( rc!=SQLITE_OK ){
44921 OSTRACE(("FETCH pid=%lu, pFile=%p, rc=%s\n",
44922 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
44923 return rc;
44924 }
44925 }
44926 if( pFd->mmapSize >= iOff+nAmt ){
44927 *pp = &((u8 *)pFd->pMapRegion)[iOff];
44928 pFd->nFetchOut++;
44929 }
44930 }
44931#endif
44932
44933 OSTRACE(("FETCH pid=%lu, pFile=%p, pp=%p, *pp=%p, rc=SQLITE_OK\n",
44934 osGetCurrentProcessId(), fd, pp, *pp));
44935 return SQLITE_OK;
44936}
44937
44938/*
44939** If the third argument is non-NULL, then this function releases a
44940** reference obtained by an earlier call to winFetch(). The second
44941** argument passed to this function must be the same as the corresponding
44942** argument that was passed to the winFetch() invocation.
44943**
44944** Or, if the third argument is NULL, then this function is being called
44945** to inform the VFS layer that, according to POSIX, any existing mapping
44946** may now be invalid and should be unmapped.
44947*/
44948static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
44949#if SQLITE_MAX_MMAP_SIZE>0
44950 winFile *pFd = (winFile*)fd; /* The underlying database file */
44951
44952 /* If p==0 (unmap the entire file) then there must be no outstanding
44953 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
44954 ** then there must be at least one outstanding. */
44955 assert( (p==0)==(pFd->nFetchOut==0) );
44956
44957 /* If p!=0, it must match the iOff value. */
44958 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
44959
44960 OSTRACE(("UNFETCH pid=%lu, pFile=%p, offset=%lld, p=%p\n",
44961 osGetCurrentProcessId(), pFd, iOff, p));
44962
44963 if( p ){
44964 pFd->nFetchOut--;
44965 }else{
44966 /* FIXME: If Windows truly always prevents truncating or deleting a
44967 ** file while a mapping is held, then the following winUnmapfile() call
44968 ** is unnecessary can be omitted - potentially improving
44969 ** performance. */
44970 winUnmapfile(pFd);
44971 }
44972
44973 assert( pFd->nFetchOut>=0 );
44974#endif
44975
44976 OSTRACE(("UNFETCH pid=%lu, pFile=%p, rc=SQLITE_OK\n",
44977 osGetCurrentProcessId(), fd));
44978 return SQLITE_OK;
44979}
44980
44981/*
44982** Here ends the implementation of all sqlite3_file methods.
44983**
44984********************** End sqlite3_file Methods *******************************
44985******************************************************************************/
44986
44987/*
44988** This vector defines all the methods that can operate on an
44989** sqlite3_file for win32.
44990*/
44991static const sqlite3_io_methods winIoMethod = {
44992 3, /* iVersion */
44993 winClose, /* xClose */
44994 winRead, /* xRead */
44995 winWrite, /* xWrite */
44996 winTruncate, /* xTruncate */
44997 winSync, /* xSync */
44998 winFileSize, /* xFileSize */
44999 winLock, /* xLock */
45000 winUnlock, /* xUnlock */
45001 winCheckReservedLock, /* xCheckReservedLock */
45002 winFileControl, /* xFileControl */
45003 winSectorSize, /* xSectorSize */
45004 winDeviceCharacteristics, /* xDeviceCharacteristics */
45005 winShmMap, /* xShmMap */
45006 winShmLock, /* xShmLock */
45007 winShmBarrier, /* xShmBarrier */
45008 winShmUnmap, /* xShmUnmap */
45009 winFetch, /* xFetch */
45010 winUnfetch /* xUnfetch */
45011};
45012
45013/*
45014** This vector defines all the methods that can operate on an
45015** sqlite3_file for win32 without performing any locking.
45016*/
45017static const sqlite3_io_methods winIoNolockMethod = {
45018 3, /* iVersion */
45019 winClose, /* xClose */
45020 winRead, /* xRead */
45021 winWrite, /* xWrite */
45022 winTruncate, /* xTruncate */
45023 winSync, /* xSync */
45024 winFileSize, /* xFileSize */
45025 winNolockLock, /* xLock */
45026 winNolockUnlock, /* xUnlock */
45027 winNolockCheckReservedLock, /* xCheckReservedLock */
45028 winFileControl, /* xFileControl */
45029 winSectorSize, /* xSectorSize */
45030 winDeviceCharacteristics, /* xDeviceCharacteristics */
45031 winShmMap, /* xShmMap */
45032 winShmLock, /* xShmLock */
45033 winShmBarrier, /* xShmBarrier */
45034 winShmUnmap, /* xShmUnmap */
45035 winFetch, /* xFetch */
45036 winUnfetch /* xUnfetch */
45037};
45038
45039static winVfsAppData winAppData = {
45040 &winIoMethod, /* pMethod */
45041 0, /* pAppData */
45042 0 /* bNoLock */
45043};
45044
45045static winVfsAppData winNolockAppData = {
45046 &winIoNolockMethod, /* pMethod */
45047 0, /* pAppData */
45048 1 /* bNoLock */
45049};
45050
45051/****************************************************************************
45052**************************** sqlite3_vfs methods ****************************
45053**
45054** This division contains the implementation of methods on the
45055** sqlite3_vfs object.
45056*/
45057
45058#if defined(__CYGWIN__)
45059/*
45060** Convert a filename from whatever the underlying operating system
45061** supports for filenames into UTF-8. Space to hold the result is
45062** obtained from malloc and must be freed by the calling function.
45063*/
45064static char *winConvertToUtf8Filename(const void *zFilename){
45065 char *zConverted = 0;
45066 if( osIsNT() ){
45067 zConverted = winUnicodeToUtf8(zFilename);
45068 }
45069#ifdef SQLITE_WIN32_HAS_ANSI
45070 else{
45071 zConverted = winMbcsToUtf8(zFilename, osAreFileApisANSI());
45072 }
45073#endif
45074 /* caller will handle out of memory */
45075 return zConverted;
45076}
45077#endif
45078
45079/*
45080** Convert a UTF-8 filename into whatever form the underlying
45081** operating system wants filenames in. Space to hold the result
45082** is obtained from malloc and must be freed by the calling
45083** function.
45084*/
45085static void *winConvertFromUtf8Filename(const char *zFilename){
45086 void *zConverted = 0;
45087 if( osIsNT() ){
45088 zConverted = winUtf8ToUnicode(zFilename);
45089 }
45090#ifdef SQLITE_WIN32_HAS_ANSI
45091 else{
45092 zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI());
45093 }
45094#endif
45095 /* caller will handle out of memory */
45096 return zConverted;
45097}
45098
45099/*
45100** This function returns non-zero if the specified UTF-8 string buffer
45101** ends with a directory separator character or one was successfully
45102** added to it.
45103*/
45104static int winMakeEndInDirSep(int nBuf, char *zBuf){
45105 if( zBuf ){
45106 int nLen = sqlite3Strlen30(zBuf);
45107 if( nLen>0 ){
45108 if( winIsDirSep(zBuf[nLen-1]) ){
45109 return 1;
45110 }else if( nLen+1<nBuf ){
45111 zBuf[nLen] = winGetDirSep();
45112 zBuf[nLen+1] = '\0';
45113 return 1;
45114 }
45115 }
45116 }
45117 return 0;
45118}
45119
45120/*
45121** Create a temporary file name and store the resulting pointer into pzBuf.
45122** The pointer returned in pzBuf must be freed via sqlite3_free().
45123*/
45124static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
45125 static char zChars[] =
45126 "abcdefghijklmnopqrstuvwxyz"
45127 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
45128 "0123456789";
45129 size_t i, j;
45130 int nPre = sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX);
45131 int nMax, nBuf, nDir, nLen;
45132 char *zBuf;
45133
45134 /* It's odd to simulate an io-error here, but really this is just
45135 ** using the io-error infrastructure to test that SQLite handles this
45136 ** function failing.
45137 */
45138 SimulateIOError( return SQLITE_IOERR );
45139
45140 /* Allocate a temporary buffer to store the fully qualified file
45141 ** name for the temporary file. If this fails, we cannot continue.
45142 */
45143 nMax = pVfs->mxPathname; nBuf = nMax + 2;
45144 zBuf = sqlite3MallocZero( nBuf );
45145 if( !zBuf ){
45146 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
45147 return SQLITE_IOERR_NOMEM_BKPT;
45148 }
45149
45150 /* Figure out the effective temporary directory. First, check if one
45151 ** has been explicitly set by the application; otherwise, use the one
45152 ** configured by the operating system.
45153 */
45154 nDir = nMax - (nPre + 15);
45155 assert( nDir>0 );
45156 if( sqlite3_temp_directory ){
45157 int nDirLen = sqlite3Strlen30(sqlite3_temp_directory);
45158 if( nDirLen>0 ){
45159 if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
45160 nDirLen++;
45161 }
45162 if( nDirLen>nDir ){
45163 sqlite3_free(zBuf);
45164 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
45165 return winLogError(SQLITE_ERROR, 0, "winGetTempname1", 0);
45166 }
45167 sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory);
45168 }
45169 }
45170#if defined(__CYGWIN__)
45171 else{
45172 static const char *azDirs[] = {
45173 0, /* getenv("SQLITE_TMPDIR") */
45174 0, /* getenv("TMPDIR") */
45175 0, /* getenv("TMP") */
45176 0, /* getenv("TEMP") */
45177 0, /* getenv("USERPROFILE") */
45178 "/var/tmp",
45179 "/usr/tmp",
45180 "/tmp",
45181 ".",
45182 0 /* List terminator */
45183 };
45184 unsigned int i;
45185 const char *zDir = 0;
45186
45187 if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
45188 if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
45189 if( !azDirs[2] ) azDirs[2] = getenv("TMP");
45190 if( !azDirs[3] ) azDirs[3] = getenv("TEMP");
45191 if( !azDirs[4] ) azDirs[4] = getenv("USERPROFILE");
45192 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
45193 void *zConverted;
45194 if( zDir==0 ) continue;
45195 /* If the path starts with a drive letter followed by the colon
45196 ** character, assume it is already a native Win32 path; otherwise,
45197 ** it must be converted to a native Win32 path via the Cygwin API
45198 ** prior to using it.
45199 */
45200 if( winIsDriveLetterAndColon(zDir) ){
45201 zConverted = winConvertFromUtf8Filename(zDir);
45202 if( !zConverted ){
45203 sqlite3_free(zBuf);
45204 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
45205 return SQLITE_IOERR_NOMEM_BKPT;
45206 }
45207 if( winIsDir(zConverted) ){
45208 sqlite3_snprintf(nMax, zBuf, "%s", zDir);
45209 sqlite3_free(zConverted);
45210 break;
45211 }
45212 sqlite3_free(zConverted);
45213 }else{
45214 zConverted = sqlite3MallocZero( nMax+1 );
45215 if( !zConverted ){
45216 sqlite3_free(zBuf);
45217 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
45218 return SQLITE_IOERR_NOMEM_BKPT;
45219 }
45220 if( cygwin_conv_path(
45221 osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A, zDir,
45222 zConverted, nMax+1)<0 ){
45223 sqlite3_free(zConverted);
45224 sqlite3_free(zBuf);
45225 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
45226 return winLogError(SQLITE_IOERR_CONVPATH, (DWORD)errno,
45227 "winGetTempname2", zDir);
45228 }
45229 if( winIsDir(zConverted) ){
45230 /* At this point, we know the candidate directory exists and should
45231 ** be used. However, we may need to convert the string containing
45232 ** its name into UTF-8 (i.e. if it is UTF-16 right now).
45233 */
45234 char *zUtf8 = winConvertToUtf8Filename(zConverted);
45235 if( !zUtf8 ){
45236 sqlite3_free(zConverted);
45237 sqlite3_free(zBuf);
45238 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
45239 return SQLITE_IOERR_NOMEM_BKPT;
45240 }
45241 sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
45242 sqlite3_free(zUtf8);
45243 sqlite3_free(zConverted);
45244 break;
45245 }
45246 sqlite3_free(zConverted);
45247 }
45248 }
45249 }
45250#elif !SQLITE_OS_WINRT && !defined(__CYGWIN__)
45251 else if( osIsNT() ){
45252 char *zMulti;
45253 LPWSTR zWidePath = sqlite3MallocZero( nMax*sizeof(WCHAR) );
45254 if( !zWidePath ){
45255 sqlite3_free(zBuf);
45256 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
45257 return SQLITE_IOERR_NOMEM_BKPT;
45258 }
45259 if( osGetTempPathW(nMax, zWidePath)==0 ){
45260 sqlite3_free(zWidePath);
45261 sqlite3_free(zBuf);
45262 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
45263 return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
45264 "winGetTempname2", 0);
45265 }
45266 zMulti = winUnicodeToUtf8(zWidePath);
45267 if( zMulti ){
45268 sqlite3_snprintf(nMax, zBuf, "%s", zMulti);
45269 sqlite3_free(zMulti);
45270 sqlite3_free(zWidePath);
45271 }else{
45272 sqlite3_free(zWidePath);
45273 sqlite3_free(zBuf);
45274 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
45275 return SQLITE_IOERR_NOMEM_BKPT;
45276 }
45277 }
45278#ifdef SQLITE_WIN32_HAS_ANSI
45279 else{
45280 char *zUtf8;
45281 char *zMbcsPath = sqlite3MallocZero( nMax );
45282 if( !zMbcsPath ){
45283 sqlite3_free(zBuf);
45284 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
45285 return SQLITE_IOERR_NOMEM_BKPT;
45286 }
45287 if( osGetTempPathA(nMax, zMbcsPath)==0 ){
45288 sqlite3_free(zBuf);
45289 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
45290 return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
45291 "winGetTempname3", 0);
45292 }
45293 zUtf8 = winMbcsToUtf8(zMbcsPath, osAreFileApisANSI());
45294 if( zUtf8 ){
45295 sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
45296 sqlite3_free(zUtf8);
45297 }else{
45298 sqlite3_free(zBuf);
45299 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
45300 return SQLITE_IOERR_NOMEM_BKPT;
45301 }
45302 }
45303#endif /* SQLITE_WIN32_HAS_ANSI */
45304#endif /* !SQLITE_OS_WINRT */
45305
45306 /*
45307 ** Check to make sure the temporary directory ends with an appropriate
45308 ** separator. If it does not and there is not enough space left to add
45309 ** one, fail.
45310 */
45311 if( !winMakeEndInDirSep(nDir+1, zBuf) ){
45312 sqlite3_free(zBuf);
45313 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
45314 return winLogError(SQLITE_ERROR, 0, "winGetTempname4", 0);
45315 }
45316
45317 /*
45318 ** Check that the output buffer is large enough for the temporary file
45319 ** name in the following format:
45320 **
45321 ** "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0"
45322 **
45323 ** If not, return SQLITE_ERROR. The number 17 is used here in order to
45324 ** account for the space used by the 15 character random suffix and the
45325 ** two trailing NUL characters. The final directory separator character
45326 ** has already added if it was not already present.
45327 */
45328 nLen = sqlite3Strlen30(zBuf);
45329 if( (nLen + nPre + 17) > nBuf ){
45330 sqlite3_free(zBuf);
45331 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
45332 return winLogError(SQLITE_ERROR, 0, "winGetTempname5", 0);
45333 }
45334
45335 sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
45336
45337 j = sqlite3Strlen30(zBuf);
45338 sqlite3_randomness(15, &zBuf[j]);
45339 for(i=0; i<15; i++, j++){
45340 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
45341 }
45342 zBuf[j] = 0;
45343 zBuf[j+1] = 0;
45344 *pzBuf = zBuf;
45345
45346 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
45347 return SQLITE_OK;
45348}
45349
45350/*
45351** Return TRUE if the named file is really a directory. Return false if
45352** it is something other than a directory, or if there is any kind of memory
45353** allocation failure.
45354*/
45355static int winIsDir(const void *zConverted){
45356 DWORD attr;
45357 int rc = 0;
45358 DWORD lastErrno;
45359
45360 if( osIsNT() ){
45361 int cnt = 0;
45362 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
45363 memset(&sAttrData, 0, sizeof(sAttrData));
45364 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
45365 GetFileExInfoStandard,
45366 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
45367 if( !rc ){
45368 return 0; /* Invalid name? */
45369 }
45370 attr = sAttrData.dwFileAttributes;
45371#if SQLITE_OS_WINCE==0
45372 }else{
45373 attr = osGetFileAttributesA((char*)zConverted);
45374#endif
45375 }
45376 return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY);
45377}
45378
45379/* forward reference */
45380static int winAccess(
45381 sqlite3_vfs *pVfs, /* Not used on win32 */
45382 const char *zFilename, /* Name of file to check */
45383 int flags, /* Type of test to make on this file */
45384 int *pResOut /* OUT: Result */
45385);
45386
45387/*
45388** Open a file.
45389*/
45390static int winOpen(
45391 sqlite3_vfs *pVfs, /* Used to get maximum path length and AppData */
45392 const char *zName, /* Name of the file (UTF-8) */
45393 sqlite3_file *id, /* Write the SQLite file handle here */
45394 int flags, /* Open mode flags */
45395 int *pOutFlags /* Status return flags */
45396){
45397 HANDLE h;
45398 DWORD lastErrno = 0;
45399 DWORD dwDesiredAccess;
45400 DWORD dwShareMode;
45401 DWORD dwCreationDisposition;
45402 DWORD dwFlagsAndAttributes = 0;
45403#if SQLITE_OS_WINCE
45404 int isTemp = 0;
45405#endif
45406 winVfsAppData *pAppData;
45407 winFile *pFile = (winFile*)id;
45408 void *zConverted; /* Filename in OS encoding */
45409 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
45410 int cnt = 0;
45411
45412 /* If argument zPath is a NULL pointer, this function is required to open
45413 ** a temporary file. Use this buffer to store the file name in.
45414 */
45415 char *zTmpname = 0; /* For temporary filename, if necessary. */
45416
45417 int rc = SQLITE_OK; /* Function Return Code */
45418#if !defined(NDEBUG) || SQLITE_OS_WINCE
45419 int eType = flags&0xFFFFFF00; /* Type of file to open */
45420#endif
45421
45422 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
45423 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
45424 int isCreate = (flags & SQLITE_OPEN_CREATE);
45425 int isReadonly = (flags & SQLITE_OPEN_READONLY);
45426 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
45427
45428#ifndef NDEBUG
45429 int isOpenJournal = (isCreate && (
45430 eType==SQLITE_OPEN_MASTER_JOURNAL
45431 || eType==SQLITE_OPEN_MAIN_JOURNAL
45432 || eType==SQLITE_OPEN_WAL
45433 ));
45434#endif
45435
45436 OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n",
45437 zUtf8Name, id, flags, pOutFlags));
45438
45439 /* Check the following statements are true:
45440 **
45441 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
45442 ** (b) if CREATE is set, then READWRITE must also be set, and
45443 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
45444 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
45445 */
45446 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
45447 assert(isCreate==0 || isReadWrite);
45448 assert(isExclusive==0 || isCreate);
45449 assert(isDelete==0 || isCreate);
45450
45451 /* The main DB, main journal, WAL file and master journal are never
45452 ** automatically deleted. Nor are they ever temporary files. */
45453 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
45454 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
45455 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
45456 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
45457
45458 /* Assert that the upper layer has set one of the "file-type" flags. */
45459 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
45460 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
45461 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
45462 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
45463 );
45464
45465 assert( pFile!=0 );
45466 memset(pFile, 0, sizeof(winFile));
45467 pFile->h = INVALID_HANDLE_VALUE;
45468
45469#if SQLITE_OS_WINRT
45470 if( !zUtf8Name && !sqlite3_temp_directory ){
45471 sqlite3_log(SQLITE_ERROR,
45472 "sqlite3_temp_directory variable should be set for WinRT");
45473 }
45474#endif
45475
45476 /* If the second argument to this function is NULL, generate a
45477 ** temporary file name to use
45478 */
45479 if( !zUtf8Name ){
45480 assert( isDelete && !isOpenJournal );
45481 rc = winGetTempname(pVfs, &zTmpname);
45482 if( rc!=SQLITE_OK ){
45483 OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
45484 return rc;
45485 }
45486 zUtf8Name = zTmpname;
45487 }
45488
45489 /* Database filenames are double-zero terminated if they are not
45490 ** URIs with parameters. Hence, they can always be passed into
45491 ** sqlite3_uri_parameter().
45492 */
45493 assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
45494 zUtf8Name[sqlite3Strlen30(zUtf8Name)+1]==0 );
45495
45496 /* Convert the filename to the system encoding. */
45497 zConverted = winConvertFromUtf8Filename(zUtf8Name);
45498 if( zConverted==0 ){
45499 sqlite3_free(zTmpname);
45500 OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
45501 return SQLITE_IOERR_NOMEM_BKPT;
45502 }
45503
45504 if( winIsDir(zConverted) ){
45505 sqlite3_free(zConverted);
45506 sqlite3_free(zTmpname);
45507 OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name));
45508 return SQLITE_CANTOPEN_ISDIR;
45509 }
45510
45511 if( isReadWrite ){
45512 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
45513 }else{
45514 dwDesiredAccess = GENERIC_READ;
45515 }
45516
45517 /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
45518 ** created. SQLite doesn't use it to indicate "exclusive access"
45519 ** as it is usually understood.
45520 */
45521 if( isExclusive ){
45522 /* Creates a new file, only if it does not already exist. */
45523 /* If the file exists, it fails. */
45524 dwCreationDisposition = CREATE_NEW;
45525 }else if( isCreate ){
45526 /* Open existing file, or create if it doesn't exist */
45527 dwCreationDisposition = OPEN_ALWAYS;
45528 }else{
45529 /* Opens a file, only if it exists. */
45530 dwCreationDisposition = OPEN_EXISTING;
45531 }
45532
45533 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
45534
45535 if( isDelete ){
45536#if SQLITE_OS_WINCE
45537 dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
45538 isTemp = 1;
45539#else
45540 dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
45541 | FILE_ATTRIBUTE_HIDDEN
45542 | FILE_FLAG_DELETE_ON_CLOSE;
45543#endif
45544 }else{
45545 dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
45546 }
45547 /* Reports from the internet are that performance is always
45548 ** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */
45549#if SQLITE_OS_WINCE
45550 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
45551#endif
45552
45553 if( osIsNT() ){
45554#if SQLITE_OS_WINRT
45555 CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
45556 extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
45557 extendedParameters.dwFileAttributes =
45558 dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
45559 extendedParameters.dwFileFlags = dwFlagsAndAttributes & FILE_FLAG_MASK;
45560 extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
45561 extendedParameters.lpSecurityAttributes = NULL;
45562 extendedParameters.hTemplateFile = NULL;
45563 do{
45564 h = osCreateFile2((LPCWSTR)zConverted,
45565 dwDesiredAccess,
45566 dwShareMode,
45567 dwCreationDisposition,
45568 &extendedParameters);
45569 if( h!=INVALID_HANDLE_VALUE ) break;
45570 if( isReadWrite ){
45571 int rc2, isRO = 0;
45572 sqlite3BeginBenignMalloc();
45573 rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
45574 sqlite3EndBenignMalloc();
45575 if( rc2==SQLITE_OK && isRO ) break;
45576 }
45577 }while( winRetryIoerr(&cnt, &lastErrno) );
45578#else
45579 do{
45580 h = osCreateFileW((LPCWSTR)zConverted,
45581 dwDesiredAccess,
45582 dwShareMode, NULL,
45583 dwCreationDisposition,
45584 dwFlagsAndAttributes,
45585 NULL);
45586 if( h!=INVALID_HANDLE_VALUE ) break;
45587 if( isReadWrite ){
45588 int rc2, isRO = 0;
45589 sqlite3BeginBenignMalloc();
45590 rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
45591 sqlite3EndBenignMalloc();
45592 if( rc2==SQLITE_OK && isRO ) break;
45593 }
45594 }while( winRetryIoerr(&cnt, &lastErrno) );
45595#endif
45596 }
45597#ifdef SQLITE_WIN32_HAS_ANSI
45598 else{
45599 do{
45600 h = osCreateFileA((LPCSTR)zConverted,
45601 dwDesiredAccess,
45602 dwShareMode, NULL,
45603 dwCreationDisposition,
45604 dwFlagsAndAttributes,
45605 NULL);
45606 if( h!=INVALID_HANDLE_VALUE ) break;
45607 if( isReadWrite ){
45608 int rc2, isRO = 0;
45609 sqlite3BeginBenignMalloc();
45610 rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
45611 sqlite3EndBenignMalloc();
45612 if( rc2==SQLITE_OK && isRO ) break;
45613 }
45614 }while( winRetryIoerr(&cnt, &lastErrno) );
45615 }
45616#endif
45617 winLogIoerr(cnt, __LINE__);
45618
45619 OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
45620 dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
45621
45622 if( h==INVALID_HANDLE_VALUE ){
45623 sqlite3_free(zConverted);
45624 sqlite3_free(zTmpname);
45625 if( isReadWrite && !isExclusive ){
45626 return winOpen(pVfs, zName, id,
45627 ((flags|SQLITE_OPEN_READONLY) &
45628 ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
45629 pOutFlags);
45630 }else{
45631 pFile->lastErrno = lastErrno;
45632 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
45633 return SQLITE_CANTOPEN_BKPT;
45634 }
45635 }
45636
45637 if( pOutFlags ){
45638 if( isReadWrite ){
45639 *pOutFlags = SQLITE_OPEN_READWRITE;
45640 }else{
45641 *pOutFlags = SQLITE_OPEN_READONLY;
45642 }
45643 }
45644
45645 OSTRACE(("OPEN file=%p, name=%s, access=%lx, pOutFlags=%p, *pOutFlags=%d, "
45646 "rc=%s\n", h, zUtf8Name, dwDesiredAccess, pOutFlags, pOutFlags ?
45647 *pOutFlags : 0, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
45648
45649 pAppData = (winVfsAppData*)pVfs->pAppData;
45650
45651#if SQLITE_OS_WINCE
45652 {
45653 if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
45654 && ((pAppData==NULL) || !pAppData->bNoLock)
45655 && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
45656 ){
45657 osCloseHandle(h);
45658 sqlite3_free(zConverted);
45659 sqlite3_free(zTmpname);
45660 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
45661 return rc;
45662 }
45663 }
45664 if( isTemp ){
45665 pFile->zDeleteOnClose = zConverted;
45666 }else
45667#endif
45668 {
45669 sqlite3_free(zConverted);
45670 }
45671
45672 sqlite3_free(zTmpname);
45673 pFile->pMethod = pAppData ? pAppData->pMethod : &winIoMethod;
45674 pFile->pVfs = pVfs;
45675 pFile->h = h;
45676 if( isReadonly ){
45677 pFile->ctrlFlags |= WINFILE_RDONLY;
45678 }
45679 if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
45680 pFile->ctrlFlags |= WINFILE_PSOW;
45681 }
45682 pFile->lastErrno = NO_ERROR;
45683 pFile->zPath = zName;
45684#if SQLITE_MAX_MMAP_SIZE>0
45685 pFile->hMap = NULL;
45686 pFile->pMapRegion = 0;
45687 pFile->mmapSize = 0;
45688 pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
45689#endif
45690
45691 OpenCounter(+1);
45692 return rc;
45693}
45694
45695/*
45696** Delete the named file.
45697**
45698** Note that Windows does not allow a file to be deleted if some other
45699** process has it open. Sometimes a virus scanner or indexing program
45700** will open a journal file shortly after it is created in order to do
45701** whatever it does. While this other process is holding the
45702** file open, we will be unable to delete it. To work around this
45703** problem, we delay 100 milliseconds and try to delete again. Up
45704** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
45705** up and returning an error.
45706*/
45707static int winDelete(
45708 sqlite3_vfs *pVfs, /* Not used on win32 */
45709 const char *zFilename, /* Name of file to delete */
45710 int syncDir /* Not used on win32 */
45711){
45712 int cnt = 0;
45713 int rc;
45714 DWORD attr;
45715 DWORD lastErrno = 0;
45716 void *zConverted;
45717 UNUSED_PARAMETER(pVfs);
45718 UNUSED_PARAMETER(syncDir);
45719
45720 SimulateIOError(return SQLITE_IOERR_DELETE);
45721 OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir));
45722
45723 zConverted = winConvertFromUtf8Filename(zFilename);
45724 if( zConverted==0 ){
45725 OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
45726 return SQLITE_IOERR_NOMEM_BKPT;
45727 }
45728 if( osIsNT() ){
45729 do {
45730#if SQLITE_OS_WINRT
45731 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
45732 memset(&sAttrData, 0, sizeof(sAttrData));
45733 if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard,
45734 &sAttrData) ){
45735 attr = sAttrData.dwFileAttributes;
45736 }else{
45737 lastErrno = osGetLastError();
45738 if( lastErrno==ERROR_FILE_NOT_FOUND
45739 || lastErrno==ERROR_PATH_NOT_FOUND ){
45740 rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
45741 }else{
45742 rc = SQLITE_ERROR;
45743 }
45744 break;
45745 }
45746#else
45747 attr = osGetFileAttributesW(zConverted);
45748#endif
45749 if ( attr==INVALID_FILE_ATTRIBUTES ){
45750 lastErrno = osGetLastError();
45751 if( lastErrno==ERROR_FILE_NOT_FOUND
45752 || lastErrno==ERROR_PATH_NOT_FOUND ){
45753 rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
45754 }else{
45755 rc = SQLITE_ERROR;
45756 }
45757 break;
45758 }
45759 if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
45760 rc = SQLITE_ERROR; /* Files only. */
45761 break;
45762 }
45763 if ( osDeleteFileW(zConverted) ){
45764 rc = SQLITE_OK; /* Deleted OK. */
45765 break;
45766 }
45767 if ( !winRetryIoerr(&cnt, &lastErrno) ){
45768 rc = SQLITE_ERROR; /* No more retries. */
45769 break;
45770 }
45771 } while(1);
45772 }
45773#ifdef SQLITE_WIN32_HAS_ANSI
45774 else{
45775 do {
45776 attr = osGetFileAttributesA(zConverted);
45777 if ( attr==INVALID_FILE_ATTRIBUTES ){
45778 lastErrno = osGetLastError();
45779 if( lastErrno==ERROR_FILE_NOT_FOUND
45780 || lastErrno==ERROR_PATH_NOT_FOUND ){
45781 rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
45782 }else{
45783 rc = SQLITE_ERROR;
45784 }
45785 break;
45786 }
45787 if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
45788 rc = SQLITE_ERROR; /* Files only. */
45789 break;
45790 }
45791 if ( osDeleteFileA(zConverted) ){
45792 rc = SQLITE_OK; /* Deleted OK. */
45793 break;
45794 }
45795 if ( !winRetryIoerr(&cnt, &lastErrno) ){
45796 rc = SQLITE_ERROR; /* No more retries. */
45797 break;
45798 }
45799 } while(1);
45800 }
45801#endif
45802 if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){
45803 rc = winLogError(SQLITE_IOERR_DELETE, lastErrno, "winDelete", zFilename);
45804 }else{
45805 winLogIoerr(cnt, __LINE__);
45806 }
45807 sqlite3_free(zConverted);
45808 OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
45809 return rc;
45810}
45811
45812/*
45813** Check the existence and status of a file.
45814*/
45815static int winAccess(
45816 sqlite3_vfs *pVfs, /* Not used on win32 */
45817 const char *zFilename, /* Name of file to check */
45818 int flags, /* Type of test to make on this file */
45819 int *pResOut /* OUT: Result */
45820){
45821 DWORD attr;
45822 int rc = 0;
45823 DWORD lastErrno = 0;
45824 void *zConverted;
45825 UNUSED_PARAMETER(pVfs);
45826
45827 SimulateIOError( return SQLITE_IOERR_ACCESS; );
45828 OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
45829 zFilename, flags, pResOut));
45830
45831 zConverted = winConvertFromUtf8Filename(zFilename);
45832 if( zConverted==0 ){
45833 OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
45834 return SQLITE_IOERR_NOMEM_BKPT;
45835 }
45836 if( osIsNT() ){
45837 int cnt = 0;
45838 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
45839 memset(&sAttrData, 0, sizeof(sAttrData));
45840 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
45841 GetFileExInfoStandard,
45842 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
45843 if( rc ){
45844 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
45845 ** as if it does not exist.
45846 */
45847 if( flags==SQLITE_ACCESS_EXISTS
45848 && sAttrData.nFileSizeHigh==0
45849 && sAttrData.nFileSizeLow==0 ){
45850 attr = INVALID_FILE_ATTRIBUTES;
45851 }else{
45852 attr = sAttrData.dwFileAttributes;
45853 }
45854 }else{
45855 winLogIoerr(cnt, __LINE__);
45856 if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
45857 sqlite3_free(zConverted);
45858 return winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess",
45859 zFilename);
45860 }else{
45861 attr = INVALID_FILE_ATTRIBUTES;
45862 }
45863 }
45864 }
45865#ifdef SQLITE_WIN32_HAS_ANSI
45866 else{
45867 attr = osGetFileAttributesA((char*)zConverted);
45868 }
45869#endif
45870 sqlite3_free(zConverted);
45871 switch( flags ){
45872 case SQLITE_ACCESS_READ:
45873 case SQLITE_ACCESS_EXISTS:
45874 rc = attr!=INVALID_FILE_ATTRIBUTES;
45875 break;
45876 case SQLITE_ACCESS_READWRITE:
45877 rc = attr!=INVALID_FILE_ATTRIBUTES &&
45878 (attr & FILE_ATTRIBUTE_READONLY)==0;
45879 break;
45880 default:
45881 assert(!"Invalid flags argument");
45882 }
45883 *pResOut = rc;
45884 OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
45885 zFilename, pResOut, *pResOut));
45886 return SQLITE_OK;
45887}
45888
45889/*
45890** Returns non-zero if the specified path name starts with a drive letter
45891** followed by a colon character.
45892*/
45893static BOOL winIsDriveLetterAndColon(
45894 const char *zPathname
45895){
45896 return ( sqlite3Isalpha(zPathname[0]) && zPathname[1]==':' );
45897}
45898
45899/*
45900** Returns non-zero if the specified path name should be used verbatim. If
45901** non-zero is returned from this function, the calling function must simply
45902** use the provided path name verbatim -OR- resolve it into a full path name
45903** using the GetFullPathName Win32 API function (if available).
45904*/
45905static BOOL winIsVerbatimPathname(
45906 const char *zPathname
45907){
45908 /*
45909 ** If the path name starts with a forward slash or a backslash, it is either
45910 ** a legal UNC name, a volume relative path, or an absolute path name in the
45911 ** "Unix" format on Windows. There is no easy way to differentiate between
45912 ** the final two cases; therefore, we return the safer return value of TRUE
45913 ** so that callers of this function will simply use it verbatim.
45914 */
45915 if ( winIsDirSep(zPathname[0]) ){
45916 return TRUE;
45917 }
45918
45919 /*
45920 ** If the path name starts with a letter and a colon it is either a volume
45921 ** relative path or an absolute path. Callers of this function must not
45922 ** attempt to treat it as a relative path name (i.e. they should simply use
45923 ** it verbatim).
45924 */
45925 if ( winIsDriveLetterAndColon(zPathname) ){
45926 return TRUE;
45927 }
45928
45929 /*
45930 ** If we get to this point, the path name should almost certainly be a purely
45931 ** relative one (i.e. not a UNC name, not absolute, and not volume relative).
45932 */
45933 return FALSE;
45934}
45935
45936/*
45937** Turn a relative pathname into a full pathname. Write the full
45938** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
45939** bytes in size.
45940*/
45941static int winFullPathname(
45942 sqlite3_vfs *pVfs, /* Pointer to vfs object */
45943 const char *zRelative, /* Possibly relative input path */
45944 int nFull, /* Size of output buffer in bytes */
45945 char *zFull /* Output buffer */
45946){
45947#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
45948 DWORD nByte;
45949 void *zConverted;
45950 char *zOut;
45951#endif
45952
45953 /* If this path name begins with "/X:", where "X" is any alphabetic
45954 ** character, discard the initial "/" from the pathname.
45955 */
45956 if( zRelative[0]=='/' && winIsDriveLetterAndColon(zRelative+1) ){
45957 zRelative++;
45958 }
45959
45960#if defined(__CYGWIN__)
45961 SimulateIOError( return SQLITE_ERROR );
45962 UNUSED_PARAMETER(nFull);
45963 assert( nFull>=pVfs->mxPathname );
45964 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
45965 /*
45966 ** NOTE: We are dealing with a relative path name and the data
45967 ** directory has been set. Therefore, use it as the basis
45968 ** for converting the relative path name to an absolute
45969 ** one by prepending the data directory and a slash.
45970 */
45971 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
45972 if( !zOut ){
45973 return SQLITE_IOERR_NOMEM_BKPT;
45974 }
45975 if( cygwin_conv_path(
45976 (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A) |
45977 CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
45978 sqlite3_free(zOut);
45979 return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
45980 "winFullPathname1", zRelative);
45981 }else{
45982 char *zUtf8 = winConvertToUtf8Filename(zOut);
45983 if( !zUtf8 ){
45984 sqlite3_free(zOut);
45985 return SQLITE_IOERR_NOMEM_BKPT;
45986 }
45987 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
45988 sqlite3_data_directory, winGetDirSep(), zUtf8);
45989 sqlite3_free(zUtf8);
45990 sqlite3_free(zOut);
45991 }
45992 }else{
45993 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
45994 if( !zOut ){
45995 return SQLITE_IOERR_NOMEM_BKPT;
45996 }
45997 if( cygwin_conv_path(
45998 (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A),
45999 zRelative, zOut, pVfs->mxPathname+1)<0 ){
46000 sqlite3_free(zOut);
46001 return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
46002 "winFullPathname2", zRelative);
46003 }else{
46004 char *zUtf8 = winConvertToUtf8Filename(zOut);
46005 if( !zUtf8 ){
46006 sqlite3_free(zOut);
46007 return SQLITE_IOERR_NOMEM_BKPT;
46008 }
46009 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
46010 sqlite3_free(zUtf8);
46011 sqlite3_free(zOut);
46012 }
46013 }
46014 return SQLITE_OK;
46015#endif
46016
46017#if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && !defined(__CYGWIN__)
46018 SimulateIOError( return SQLITE_ERROR );
46019 /* WinCE has no concept of a relative pathname, or so I am told. */
46020 /* WinRT has no way to convert a relative path to an absolute one. */
46021 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
46022 /*
46023 ** NOTE: We are dealing with a relative path name and the data
46024 ** directory has been set. Therefore, use it as the basis
46025 ** for converting the relative path name to an absolute
46026 ** one by prepending the data directory and a backslash.
46027 */
46028 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
46029 sqlite3_data_directory, winGetDirSep(), zRelative);
46030 }else{
46031 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
46032 }
46033 return SQLITE_OK;
46034#endif
46035
46036#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
46037 /* It's odd to simulate an io-error here, but really this is just
46038 ** using the io-error infrastructure to test that SQLite handles this
46039 ** function failing. This function could fail if, for example, the
46040 ** current working directory has been unlinked.
46041 */
46042 SimulateIOError( return SQLITE_ERROR );
46043 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
46044 /*
46045 ** NOTE: We are dealing with a relative path name and the data
46046 ** directory has been set. Therefore, use it as the basis
46047 ** for converting the relative path name to an absolute
46048 ** one by prepending the data directory and a backslash.
46049 */
46050 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
46051 sqlite3_data_directory, winGetDirSep(), zRelative);
46052 return SQLITE_OK;
46053 }
46054 zConverted = winConvertFromUtf8Filename(zRelative);
46055 if( zConverted==0 ){
46056 return SQLITE_IOERR_NOMEM_BKPT;
46057 }
46058 if( osIsNT() ){
46059 LPWSTR zTemp;
46060 nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
46061 if( nByte==0 ){
46062 sqlite3_free(zConverted);
46063 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
46064 "winFullPathname1", zRelative);
46065 }
46066 nByte += 3;
46067 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
46068 if( zTemp==0 ){
46069 sqlite3_free(zConverted);
46070 return SQLITE_IOERR_NOMEM_BKPT;
46071 }
46072 nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
46073 if( nByte==0 ){
46074 sqlite3_free(zConverted);
46075 sqlite3_free(zTemp);
46076 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
46077 "winFullPathname2", zRelative);
46078 }
46079 sqlite3_free(zConverted);
46080 zOut = winUnicodeToUtf8(zTemp);
46081 sqlite3_free(zTemp);
46082 }
46083#ifdef SQLITE_WIN32_HAS_ANSI
46084 else{
46085 char *zTemp;
46086 nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
46087 if( nByte==0 ){
46088 sqlite3_free(zConverted);
46089 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
46090 "winFullPathname3", zRelative);
46091 }
46092 nByte += 3;
46093 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
46094 if( zTemp==0 ){
46095 sqlite3_free(zConverted);
46096 return SQLITE_IOERR_NOMEM_BKPT;
46097 }
46098 nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
46099 if( nByte==0 ){
46100 sqlite3_free(zConverted);
46101 sqlite3_free(zTemp);
46102 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
46103 "winFullPathname4", zRelative);
46104 }
46105 sqlite3_free(zConverted);
46106 zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
46107 sqlite3_free(zTemp);
46108 }
46109#endif
46110 if( zOut ){
46111 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
46112 sqlite3_free(zOut);
46113 return SQLITE_OK;
46114 }else{
46115 return SQLITE_IOERR_NOMEM_BKPT;
46116 }
46117#endif
46118}
46119
46120#ifndef SQLITE_OMIT_LOAD_EXTENSION
46121/*
46122** Interfaces for opening a shared library, finding entry points
46123** within the shared library, and closing the shared library.
46124*/
46125static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
46126 HANDLE h;
46127#if defined(__CYGWIN__)
46128 int nFull = pVfs->mxPathname+1;
46129 char *zFull = sqlite3MallocZero( nFull );
46130 void *zConverted = 0;
46131 if( zFull==0 ){
46132 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
46133 return 0;
46134 }
46135 if( winFullPathname(pVfs, zFilename, nFull, zFull)!=SQLITE_OK ){
46136 sqlite3_free(zFull);
46137 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
46138 return 0;
46139 }
46140 zConverted = winConvertFromUtf8Filename(zFull);
46141 sqlite3_free(zFull);
46142#else
46143 void *zConverted = winConvertFromUtf8Filename(zFilename);
46144 UNUSED_PARAMETER(pVfs);
46145#endif
46146 if( zConverted==0 ){
46147 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
46148 return 0;
46149 }
46150 if( osIsNT() ){
46151#if SQLITE_OS_WINRT
46152 h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
46153#else
46154 h = osLoadLibraryW((LPCWSTR)zConverted);
46155#endif
46156 }
46157#ifdef SQLITE_WIN32_HAS_ANSI
46158 else{
46159 h = osLoadLibraryA((char*)zConverted);
46160 }
46161#endif
46162 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)h));
46163 sqlite3_free(zConverted);
46164 return (void*)h;
46165}
46166static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
46167 UNUSED_PARAMETER(pVfs);
46168 winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut);
46169}
46170static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
46171 FARPROC proc;
46172 UNUSED_PARAMETER(pVfs);
46173 proc = osGetProcAddressA((HANDLE)pH, zSym);
46174 OSTRACE(("DLSYM handle=%p, symbol=%s, address=%p\n",
46175 (void*)pH, zSym, (void*)proc));
46176 return (void(*)(void))proc;
46177}
46178static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
46179 UNUSED_PARAMETER(pVfs);
46180 osFreeLibrary((HANDLE)pHandle);
46181 OSTRACE(("DLCLOSE handle=%p\n", (void*)pHandle));
46182}
46183#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
46184 #define winDlOpen 0
46185 #define winDlError 0
46186 #define winDlSym 0
46187 #define winDlClose 0
46188#endif
46189
46190/* State information for the randomness gatherer. */
46191typedef struct EntropyGatherer EntropyGatherer;
46192struct EntropyGatherer {
46193 unsigned char *a; /* Gather entropy into this buffer */
46194 int na; /* Size of a[] in bytes */
46195 int i; /* XOR next input into a[i] */
46196 int nXor; /* Number of XOR operations done */
46197};
46198
46199#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
46200/* Mix sz bytes of entropy into p. */
46201static void xorMemory(EntropyGatherer *p, unsigned char *x, int sz){
46202 int j, k;
46203 for(j=0, k=p->i; j<sz; j++){
46204 p->a[k++] ^= x[j];
46205 if( k>=p->na ) k = 0;
46206 }
46207 p->i = k;
46208 p->nXor += sz;
46209}
46210#endif /* !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) */
46211
46212/*
46213** Write up to nBuf bytes of randomness into zBuf.
46214*/
46215static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
46216#if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS)
46217 UNUSED_PARAMETER(pVfs);
46218 memset(zBuf, 0, nBuf);
46219 return nBuf;
46220#else
46221 EntropyGatherer e;
46222 UNUSED_PARAMETER(pVfs);
46223 memset(zBuf, 0, nBuf);
46224 e.a = (unsigned char*)zBuf;
46225 e.na = nBuf;
46226 e.nXor = 0;
46227 e.i = 0;
46228 {
46229 SYSTEMTIME x;
46230 osGetSystemTime(&x);
46231 xorMemory(&e, (unsigned char*)&x, sizeof(SYSTEMTIME));
46232 }
46233 {
46234 DWORD pid = osGetCurrentProcessId();
46235 xorMemory(&e, (unsigned char*)&pid, sizeof(DWORD));
46236 }
46237#if SQLITE_OS_WINRT
46238 {
46239 ULONGLONG cnt = osGetTickCount64();
46240 xorMemory(&e, (unsigned char*)&cnt, sizeof(ULONGLONG));
46241 }
46242#else
46243 {
46244 DWORD cnt = osGetTickCount();
46245 xorMemory(&e, (unsigned char*)&cnt, sizeof(DWORD));
46246 }
46247#endif /* SQLITE_OS_WINRT */
46248 {
46249 LARGE_INTEGER i;
46250 osQueryPerformanceCounter(&i);
46251 xorMemory(&e, (unsigned char*)&i, sizeof(LARGE_INTEGER));
46252 }
46253#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
46254 {
46255 UUID id;
46256 memset(&id, 0, sizeof(UUID));
46257 osUuidCreate(&id);
46258 xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
46259 memset(&id, 0, sizeof(UUID));
46260 osUuidCreateSequential(&id);
46261 xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
46262 }
46263#endif /* !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID */
46264 return e.nXor>nBuf ? nBuf : e.nXor;
46265#endif /* defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) */
46266}
46267
46268
46269/*
46270** Sleep for a little while. Return the amount of time slept.
46271*/
46272static int winSleep(sqlite3_vfs *pVfs, int microsec){
46273 sqlite3_win32_sleep((microsec+999)/1000);
46274 UNUSED_PARAMETER(pVfs);
46275 return ((microsec+999)/1000)*1000;
46276}
46277
46278/*
46279** The following variable, if set to a non-zero value, is interpreted as
46280** the number of seconds since 1970 and is used to set the result of
46281** sqlite3OsCurrentTime() during testing.
46282*/
46283#ifdef SQLITE_TEST
46284SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
46285#endif
46286
46287/*
46288** Find the current time (in Universal Coordinated Time). Write into *piNow
46289** the current time and date as a Julian Day number times 86_400_000. In
46290** other words, write into *piNow the number of milliseconds since the Julian
46291** epoch of noon in Greenwich on November 24, 4714 B.C according to the
46292** proleptic Gregorian calendar.
46293**
46294** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
46295** cannot be found.
46296*/
46297static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
46298 /* FILETIME structure is a 64-bit value representing the number of
46299 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
46300 */
46301 FILETIME ft;
46302 static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
46303#ifdef SQLITE_TEST
46304 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
46305#endif
46306 /* 2^32 - to avoid use of LL and warnings in gcc */
46307 static const sqlite3_int64 max32BitValue =
46308 (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 +
46309 (sqlite3_int64)294967296;
46310
46311#if SQLITE_OS_WINCE
46312 SYSTEMTIME time;
46313 osGetSystemTime(&time);
46314 /* if SystemTimeToFileTime() fails, it returns zero. */
46315 if (!osSystemTimeToFileTime(&time,&ft)){
46316 return SQLITE_ERROR;
46317 }
46318#else
46319 osGetSystemTimeAsFileTime( &ft );
46320#endif
46321
46322 *piNow = winFiletimeEpoch +
46323 ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
46324 (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
46325
46326#ifdef SQLITE_TEST
46327 if( sqlite3_current_time ){
46328 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
46329 }
46330#endif
46331 UNUSED_PARAMETER(pVfs);
46332 return SQLITE_OK;
46333}
46334
46335/*
46336** Find the current time (in Universal Coordinated Time). Write the
46337** current time and date as a Julian Day number into *prNow and
46338** return 0. Return 1 if the time and date cannot be found.
46339*/
46340static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
46341 int rc;
46342 sqlite3_int64 i;
46343 rc = winCurrentTimeInt64(pVfs, &i);
46344 if( !rc ){
46345 *prNow = i/86400000.0;
46346 }
46347 return rc;
46348}
46349
46350/*
46351** The idea is that this function works like a combination of
46352** GetLastError() and FormatMessage() on Windows (or errno and
46353** strerror_r() on Unix). After an error is returned by an OS
46354** function, SQLite calls this function with zBuf pointing to
46355** a buffer of nBuf bytes. The OS layer should populate the
46356** buffer with a nul-terminated UTF-8 encoded error message
46357** describing the last IO error to have occurred within the calling
46358** thread.
46359**
46360** If the error message is too large for the supplied buffer,
46361** it should be truncated. The return value of xGetLastError
46362** is zero if the error message fits in the buffer, or non-zero
46363** otherwise (if the message was truncated). If non-zero is returned,
46364** then it is not necessary to include the nul-terminator character
46365** in the output buffer.
46366**
46367** Not supplying an error message will have no adverse effect
46368** on SQLite. It is fine to have an implementation that never
46369** returns an error message:
46370**
46371** int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
46372** assert(zBuf[0]=='\0');
46373** return 0;
46374** }
46375**
46376** However if an error message is supplied, it will be incorporated
46377** by sqlite into the error message available to the user using
46378** sqlite3_errmsg(), possibly making IO errors easier to debug.
46379*/
46380static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
46381 DWORD e = osGetLastError();
46382 UNUSED_PARAMETER(pVfs);
46383 if( nBuf>0 ) winGetLastErrorMsg(e, nBuf, zBuf);
46384 return e;
46385}
46386
46387/*
46388** Initialize and deinitialize the operating system interface.
46389*/
46390SQLITE_API int sqlite3_os_init(void){
46391 static sqlite3_vfs winVfs = {
46392 3, /* iVersion */
46393 sizeof(winFile), /* szOsFile */
46394 SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
46395 0, /* pNext */
46396 "win32", /* zName */
46397 &winAppData, /* pAppData */
46398 winOpen, /* xOpen */
46399 winDelete, /* xDelete */
46400 winAccess, /* xAccess */
46401 winFullPathname, /* xFullPathname */
46402 winDlOpen, /* xDlOpen */
46403 winDlError, /* xDlError */
46404 winDlSym, /* xDlSym */
46405 winDlClose, /* xDlClose */
46406 winRandomness, /* xRandomness */
46407 winSleep, /* xSleep */
46408 winCurrentTime, /* xCurrentTime */
46409 winGetLastError, /* xGetLastError */
46410 winCurrentTimeInt64, /* xCurrentTimeInt64 */
46411 winSetSystemCall, /* xSetSystemCall */
46412 winGetSystemCall, /* xGetSystemCall */
46413 winNextSystemCall, /* xNextSystemCall */
46414 };
46415#if defined(SQLITE_WIN32_HAS_WIDE)
46416 static sqlite3_vfs winLongPathVfs = {
46417 3, /* iVersion */
46418 sizeof(winFile), /* szOsFile */
46419 SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
46420 0, /* pNext */
46421 "win32-longpath", /* zName */
46422 &winAppData, /* pAppData */
46423 winOpen, /* xOpen */
46424 winDelete, /* xDelete */
46425 winAccess, /* xAccess */
46426 winFullPathname, /* xFullPathname */
46427 winDlOpen, /* xDlOpen */
46428 winDlError, /* xDlError */
46429 winDlSym, /* xDlSym */
46430 winDlClose, /* xDlClose */
46431 winRandomness, /* xRandomness */
46432 winSleep, /* xSleep */
46433 winCurrentTime, /* xCurrentTime */
46434 winGetLastError, /* xGetLastError */
46435 winCurrentTimeInt64, /* xCurrentTimeInt64 */
46436 winSetSystemCall, /* xSetSystemCall */
46437 winGetSystemCall, /* xGetSystemCall */
46438 winNextSystemCall, /* xNextSystemCall */
46439 };
46440#endif
46441 static sqlite3_vfs winNolockVfs = {
46442 3, /* iVersion */
46443 sizeof(winFile), /* szOsFile */
46444 SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
46445 0, /* pNext */
46446 "win32-none", /* zName */
46447 &winNolockAppData, /* pAppData */
46448 winOpen, /* xOpen */
46449 winDelete, /* xDelete */
46450 winAccess, /* xAccess */
46451 winFullPathname, /* xFullPathname */
46452 winDlOpen, /* xDlOpen */
46453 winDlError, /* xDlError */
46454 winDlSym, /* xDlSym */
46455 winDlClose, /* xDlClose */
46456 winRandomness, /* xRandomness */
46457 winSleep, /* xSleep */
46458 winCurrentTime, /* xCurrentTime */
46459 winGetLastError, /* xGetLastError */
46460 winCurrentTimeInt64, /* xCurrentTimeInt64 */
46461 winSetSystemCall, /* xSetSystemCall */
46462 winGetSystemCall, /* xGetSystemCall */
46463 winNextSystemCall, /* xNextSystemCall */
46464 };
46465#if defined(SQLITE_WIN32_HAS_WIDE)
46466 static sqlite3_vfs winLongPathNolockVfs = {
46467 3, /* iVersion */
46468 sizeof(winFile), /* szOsFile */
46469 SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
46470 0, /* pNext */
46471 "win32-longpath-none", /* zName */
46472 &winNolockAppData, /* pAppData */
46473 winOpen, /* xOpen */
46474 winDelete, /* xDelete */
46475 winAccess, /* xAccess */
46476 winFullPathname, /* xFullPathname */
46477 winDlOpen, /* xDlOpen */
46478 winDlError, /* xDlError */
46479 winDlSym, /* xDlSym */
46480 winDlClose, /* xDlClose */
46481 winRandomness, /* xRandomness */
46482 winSleep, /* xSleep */
46483 winCurrentTime, /* xCurrentTime */
46484 winGetLastError, /* xGetLastError */
46485 winCurrentTimeInt64, /* xCurrentTimeInt64 */
46486 winSetSystemCall, /* xSetSystemCall */
46487 winGetSystemCall, /* xGetSystemCall */
46488 winNextSystemCall, /* xNextSystemCall */
46489 };
46490#endif
46491
46492 /* Double-check that the aSyscall[] array has been constructed
46493 ** correctly. See ticket [bb3a86e890c8e96ab] */
46494 assert( ArraySize(aSyscall)==80 );
46495
46496 /* get memory map allocation granularity */
46497 memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
46498#if SQLITE_OS_WINRT
46499 osGetNativeSystemInfo(&winSysInfo);
46500#else
46501 osGetSystemInfo(&winSysInfo);
46502#endif
46503 assert( winSysInfo.dwAllocationGranularity>0 );
46504 assert( winSysInfo.dwPageSize>0 );
46505
46506 sqlite3_vfs_register(&winVfs, 1);
46507
46508#if defined(SQLITE_WIN32_HAS_WIDE)
46509 sqlite3_vfs_register(&winLongPathVfs, 0);
46510#endif
46511
46512 sqlite3_vfs_register(&winNolockVfs, 0);
46513
46514#if defined(SQLITE_WIN32_HAS_WIDE)
46515 sqlite3_vfs_register(&winLongPathNolockVfs, 0);
46516#endif
46517
46518#ifndef SQLITE_OMIT_WAL
46519 winBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
46520#endif
46521
46522 return SQLITE_OK;
46523}
46524
46525SQLITE_API int sqlite3_os_end(void){
46526#if SQLITE_OS_WINRT
46527 if( sleepObj!=NULL ){
46528 osCloseHandle(sleepObj);
46529 sleepObj = NULL;
46530 }
46531#endif
46532
46533#ifndef SQLITE_OMIT_WAL
46534 winBigLock = 0;
46535#endif
46536
46537 return SQLITE_OK;
46538}
46539
46540#endif /* SQLITE_OS_WIN */
46541
46542/************** End of os_win.c **********************************************/
46543/************** Begin file memdb.c *******************************************/
46544/*
46545** 2016-09-07
46546**
46547** The author disclaims copyright to this source code. In place of
46548** a legal notice, here is a blessing:
46549**
46550** May you do good and not evil.
46551** May you find forgiveness for yourself and forgive others.
46552** May you share freely, never taking more than you give.
46553**
46554******************************************************************************
46555**
46556** This file implements an in-memory VFS. A database is held as a contiguous
46557** block of memory.
46558**
46559** This file also implements interface sqlite3_serialize() and
46560** sqlite3_deserialize().
46561*/
46562/* #include "sqliteInt.h" */
46563#ifdef SQLITE_ENABLE_DESERIALIZE
46564
46565/*
46566** Forward declaration of objects used by this utility
46567*/
46568typedef struct sqlite3_vfs MemVfs;
46569typedef struct MemFile MemFile;
46570
46571/* Access to a lower-level VFS that (might) implement dynamic loading,
46572** access to randomness, etc.
46573*/
46574#define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
46575
46576/* An open file */
46577struct MemFile {
46578 sqlite3_file base; /* IO methods */
46579 sqlite3_int64 sz; /* Size of the file */
46580 sqlite3_int64 szMax; /* Space allocated to aData */
46581 unsigned char *aData; /* content of the file */
46582 int nMmap; /* Number of memory mapped pages */
46583 unsigned mFlags; /* Flags */
46584 int eLock; /* Most recent lock against this file */
46585};
46586
46587/*
46588** Methods for MemFile
46589*/
46590static int memdbClose(sqlite3_file*);
46591static int memdbRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
46592static int memdbWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
46593static int memdbTruncate(sqlite3_file*, sqlite3_int64 size);
46594static int memdbSync(sqlite3_file*, int flags);
46595static int memdbFileSize(sqlite3_file*, sqlite3_int64 *pSize);
46596static int memdbLock(sqlite3_file*, int);
46597/* static int memdbCheckReservedLock(sqlite3_file*, int *pResOut);// not used */
46598static int memdbFileControl(sqlite3_file*, int op, void *pArg);
46599/* static int memdbSectorSize(sqlite3_file*); // not used */
46600static int memdbDeviceCharacteristics(sqlite3_file*);
46601static int memdbFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
46602static int memdbUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
46603
46604/*
46605** Methods for MemVfs
46606*/
46607static int memdbOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
46608/* static int memdbDelete(sqlite3_vfs*, const char *zName, int syncDir); */
46609static int memdbAccess(sqlite3_vfs*, const char *zName, int flags, int *);
46610static int memdbFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
46611static void *memdbDlOpen(sqlite3_vfs*, const char *zFilename);
46612static void memdbDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
46613static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
46614static void memdbDlClose(sqlite3_vfs*, void*);
46615static int memdbRandomness(sqlite3_vfs*, int nByte, char *zOut);
46616static int memdbSleep(sqlite3_vfs*, int microseconds);
46617/* static int memdbCurrentTime(sqlite3_vfs*, double*); */
46618static int memdbGetLastError(sqlite3_vfs*, int, char *);
46619static int memdbCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
46620
46621static sqlite3_vfs memdb_vfs = {
46622 2, /* iVersion */
46623 0, /* szOsFile (set when registered) */
46624 1024, /* mxPathname */
46625 0, /* pNext */
46626 "memdb", /* zName */
46627 0, /* pAppData (set when registered) */
46628 memdbOpen, /* xOpen */
46629 0, /* memdbDelete, */ /* xDelete */
46630 memdbAccess, /* xAccess */
46631 memdbFullPathname, /* xFullPathname */
46632 memdbDlOpen, /* xDlOpen */
46633 memdbDlError, /* xDlError */
46634 memdbDlSym, /* xDlSym */
46635 memdbDlClose, /* xDlClose */
46636 memdbRandomness, /* xRandomness */
46637 memdbSleep, /* xSleep */
46638 0, /* memdbCurrentTime, */ /* xCurrentTime */
46639 memdbGetLastError, /* xGetLastError */
46640 memdbCurrentTimeInt64 /* xCurrentTimeInt64 */
46641};
46642
46643static const sqlite3_io_methods memdb_io_methods = {
46644 3, /* iVersion */
46645 memdbClose, /* xClose */
46646 memdbRead, /* xRead */
46647 memdbWrite, /* xWrite */
46648 memdbTruncate, /* xTruncate */
46649 memdbSync, /* xSync */
46650 memdbFileSize, /* xFileSize */
46651 memdbLock, /* xLock */
46652 memdbLock, /* xUnlock - same as xLock in this case */
46653 0, /* memdbCheckReservedLock, */ /* xCheckReservedLock */
46654 memdbFileControl, /* xFileControl */
46655 0, /* memdbSectorSize,*/ /* xSectorSize */
46656 memdbDeviceCharacteristics, /* xDeviceCharacteristics */
46657 0, /* xShmMap */
46658 0, /* xShmLock */
46659 0, /* xShmBarrier */
46660 0, /* xShmUnmap */
46661 memdbFetch, /* xFetch */
46662 memdbUnfetch /* xUnfetch */
46663};
46664
46665
46666
46667/*
46668** Close an memdb-file.
46669**
46670** The pData pointer is owned by the application, so there is nothing
46671** to free.
46672*/
46673static int memdbClose(sqlite3_file *pFile){
46674 MemFile *p = (MemFile *)pFile;
46675 if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ) sqlite3_free(p->aData);
46676 return SQLITE_OK;
46677}
46678
46679/*
46680** Read data from an memdb-file.
46681*/
46682static int memdbRead(
46683 sqlite3_file *pFile,
46684 void *zBuf,
46685 int iAmt,
46686 sqlite_int64 iOfst
46687){
46688 MemFile *p = (MemFile *)pFile;
46689 if( iOfst+iAmt>p->sz ){
46690 memset(zBuf, 0, iAmt);
46691 if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst);
46692 return SQLITE_IOERR_SHORT_READ;
46693 }
46694 memcpy(zBuf, p->aData+iOfst, iAmt);
46695 return SQLITE_OK;
46696}
46697
46698/*
46699** Try to enlarge the memory allocation to hold at least sz bytes
46700*/
46701static int memdbEnlarge(MemFile *p, sqlite3_int64 newSz){
46702 unsigned char *pNew;
46703 if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || p->nMmap>0 ){
46704 return SQLITE_FULL;
46705 }
46706 pNew = sqlite3_realloc64(p->aData, newSz);
46707 if( pNew==0 ) return SQLITE_NOMEM;
46708 p->aData = pNew;
46709 p->szMax = newSz;
46710 return SQLITE_OK;
46711}
46712
46713/*
46714** Write data to an memdb-file.
46715*/
46716static int memdbWrite(
46717 sqlite3_file *pFile,
46718 const void *z,
46719 int iAmt,
46720 sqlite_int64 iOfst
46721){
46722 MemFile *p = (MemFile *)pFile;
46723 if( iOfst+iAmt>p->sz ){
46724 int rc;
46725 if( iOfst+iAmt>p->szMax
46726 && (rc = memdbEnlarge(p, (iOfst+iAmt)*2))!=SQLITE_OK
46727 ){
46728 return rc;
46729 }
46730 if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz);
46731 p->sz = iOfst+iAmt;
46732 }
46733 memcpy(p->aData+iOfst, z, iAmt);
46734 return SQLITE_OK;
46735}
46736
46737/*
46738** Truncate an memdb-file.
46739**
46740** In rollback mode (which is always the case for memdb, as it does not
46741** support WAL mode) the truncate() method is only used to reduce
46742** the size of a file, never to increase the size.
46743*/
46744static int memdbTruncate(sqlite3_file *pFile, sqlite_int64 size){
46745 MemFile *p = (MemFile *)pFile;
46746 if( NEVER(size>p->sz) ) return SQLITE_FULL;
46747 p->sz = size;
46748 return SQLITE_OK;
46749}
46750
46751/*
46752** Sync an memdb-file.
46753*/
46754static int memdbSync(sqlite3_file *pFile, int flags){
46755 return SQLITE_OK;
46756}
46757
46758/*
46759** Return the current file-size of an memdb-file.
46760*/
46761static int memdbFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
46762 MemFile *p = (MemFile *)pFile;
46763 *pSize = p->sz;
46764 return SQLITE_OK;
46765}
46766
46767/*
46768** Lock an memdb-file.
46769*/
46770static int memdbLock(sqlite3_file *pFile, int eLock){
46771 MemFile *p = (MemFile *)pFile;
46772 p->eLock = eLock;
46773 return SQLITE_OK;
46774}
46775
46776#if 0 /* Never used because memdbAccess() always returns false */
46777/*
46778** Check if another file-handle holds a RESERVED lock on an memdb-file.
46779*/
46780static int memdbCheckReservedLock(sqlite3_file *pFile, int *pResOut){
46781 *pResOut = 0;
46782 return SQLITE_OK;
46783}
46784#endif
46785
46786/*
46787** File control method. For custom operations on an memdb-file.
46788*/
46789static int memdbFileControl(sqlite3_file *pFile, int op, void *pArg){
46790 MemFile *p = (MemFile *)pFile;
46791 int rc = SQLITE_NOTFOUND;
46792 if( op==SQLITE_FCNTL_VFSNAME ){
46793 *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz);
46794 rc = SQLITE_OK;
46795 }
46796 return rc;
46797}
46798
46799#if 0 /* Not used because of SQLITE_IOCAP_POWERSAFE_OVERWRITE */
46800/*
46801** Return the sector-size in bytes for an memdb-file.
46802*/
46803static int memdbSectorSize(sqlite3_file *pFile){
46804 return 1024;
46805}
46806#endif
46807
46808/*
46809** Return the device characteristic flags supported by an memdb-file.
46810*/
46811static int memdbDeviceCharacteristics(sqlite3_file *pFile){
46812 return SQLITE_IOCAP_ATOMIC |
46813 SQLITE_IOCAP_POWERSAFE_OVERWRITE |
46814 SQLITE_IOCAP_SAFE_APPEND |
46815 SQLITE_IOCAP_SEQUENTIAL;
46816}
46817
46818/* Fetch a page of a memory-mapped file */
46819static int memdbFetch(
46820 sqlite3_file *pFile,
46821 sqlite3_int64 iOfst,
46822 int iAmt,
46823 void **pp
46824){
46825 MemFile *p = (MemFile *)pFile;
46826 p->nMmap++;
46827 *pp = (void*)(p->aData + iOfst);
46828 return SQLITE_OK;
46829}
46830
46831/* Release a memory-mapped page */
46832static int memdbUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
46833 MemFile *p = (MemFile *)pFile;
46834 p->nMmap--;
46835 return SQLITE_OK;
46836}
46837
46838/*
46839** Open an mem file handle.
46840*/
46841static int memdbOpen(
46842 sqlite3_vfs *pVfs,
46843 const char *zName,
46844 sqlite3_file *pFile,
46845 int flags,
46846 int *pOutFlags
46847){
46848 MemFile *p = (MemFile*)pFile;
46849 if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
46850 return ORIGVFS(pVfs)->xOpen(ORIGVFS(pVfs), zName, pFile, flags, pOutFlags);
46851 }
46852 memset(p, 0, sizeof(*p));
46853 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE;
46854 assert( pOutFlags!=0 ); /* True because flags==SQLITE_OPEN_MAIN_DB */
46855 *pOutFlags = flags | SQLITE_OPEN_MEMORY;
46856 p->base.pMethods = &memdb_io_methods;
46857 return SQLITE_OK;
46858}
46859
46860#if 0 /* Only used to delete rollback journals, master journals, and WAL
46861 ** files, none of which exist in memdb. So this routine is never used */
46862/*
46863** Delete the file located at zPath. If the dirSync argument is true,
46864** ensure the file-system modifications are synced to disk before
46865** returning.
46866*/
46867static int memdbDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
46868 return SQLITE_IOERR_DELETE;
46869}
46870#endif
46871
46872/*
46873** Test for access permissions. Return true if the requested permission
46874** is available, or false otherwise.
46875**
46876** With memdb, no files ever exist on disk. So always return false.
46877*/
46878static int memdbAccess(
46879 sqlite3_vfs *pVfs,
46880 const char *zPath,
46881 int flags,
46882 int *pResOut
46883){
46884 *pResOut = 0;
46885 return SQLITE_OK;
46886}
46887
46888/*
46889** Populate buffer zOut with the full canonical pathname corresponding
46890** to the pathname in zPath. zOut is guaranteed to point to a buffer
46891** of at least (INST_MAX_PATHNAME+1) bytes.
46892*/
46893static int memdbFullPathname(
46894 sqlite3_vfs *pVfs,
46895 const char *zPath,
46896 int nOut,
46897 char *zOut
46898){
46899 sqlite3_snprintf(nOut, zOut, "%s", zPath);
46900 return SQLITE_OK;
46901}
46902
46903/*
46904** Open the dynamic library located at zPath and return a handle.
46905*/
46906static void *memdbDlOpen(sqlite3_vfs *pVfs, const char *zPath){
46907 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
46908}
46909
46910/*
46911** Populate the buffer zErrMsg (size nByte bytes) with a human readable
46912** utf-8 string describing the most recent error encountered associated
46913** with dynamic libraries.
46914*/
46915static void memdbDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
46916 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
46917}
46918
46919/*
46920** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
46921*/
46922static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
46923 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
46924}
46925
46926/*
46927** Close the dynamic library handle pHandle.
46928*/
46929static void memdbDlClose(sqlite3_vfs *pVfs, void *pHandle){
46930 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
46931}
46932
46933/*
46934** Populate the buffer pointed to by zBufOut with nByte bytes of
46935** random data.
46936*/
46937static int memdbRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
46938 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
46939}
46940
46941/*
46942** Sleep for nMicro microseconds. Return the number of microseconds
46943** actually slept.
46944*/
46945static int memdbSleep(sqlite3_vfs *pVfs, int nMicro){
46946 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
46947}
46948
46949#if 0 /* Never used. Modern cores only call xCurrentTimeInt64() */
46950/*
46951** Return the current time as a Julian Day number in *pTimeOut.
46952*/
46953static int memdbCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
46954 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
46955}
46956#endif
46957
46958static int memdbGetLastError(sqlite3_vfs *pVfs, int a, char *b){
46959 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
46960}
46961static int memdbCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
46962 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
46963}
46964
46965/*
46966** Translate a database connection pointer and schema name into a
46967** MemFile pointer.
46968*/
46969static MemFile *memdbFromDbSchema(sqlite3 *db, const char *zSchema){
46970 MemFile *p = 0;
46971 int rc = sqlite3_file_control(db, zSchema, SQLITE_FCNTL_FILE_POINTER, &p);
46972 if( rc ) return 0;
46973 if( p->base.pMethods!=&memdb_io_methods ) return 0;
46974 return p;
46975}
46976
46977/*
46978** Return the serialization of a database
46979*/
46980SQLITE_API unsigned char *sqlite3_serialize(
46981 sqlite3 *db, /* The database connection */
46982 const char *zSchema, /* Which database within the connection */
46983 sqlite3_int64 *piSize, /* Write size here, if not NULL */
46984 unsigned int mFlags /* Maybe SQLITE_SERIALIZE_NOCOPY */
46985){
46986 MemFile *p;
46987 int iDb;
46988 Btree *pBt;
46989 sqlite3_int64 sz;
46990 int szPage = 0;
46991 sqlite3_stmt *pStmt = 0;
46992 unsigned char *pOut;
46993 char *zSql;
46994 int rc;
46995
46996#ifdef SQLITE_ENABLE_API_ARMOR
46997 if( !sqlite3SafetyCheckOk(db) ){
46998 (void)SQLITE_MISUSE_BKPT;
46999 return 0;
47000 }
47001#endif
47002
47003 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
47004 p = memdbFromDbSchema(db, zSchema);
47005 iDb = sqlite3FindDbName(db, zSchema);
47006 if( piSize ) *piSize = -1;
47007 if( iDb<0 ) return 0;
47008 if( p ){
47009 if( piSize ) *piSize = p->sz;
47010 if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
47011 pOut = p->aData;
47012 }else{
47013 pOut = sqlite3_malloc64( p->sz );
47014 if( pOut ) memcpy(pOut, p->aData, p->sz);
47015 }
47016 return pOut;
47017 }
47018 pBt = db->aDb[iDb].pBt;
47019 if( pBt==0 ) return 0;
47020 szPage = sqlite3BtreeGetPageSize(pBt);
47021 zSql = sqlite3_mprintf("PRAGMA \"%w\".page_count", zSchema);
47022 rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM;
47023 sqlite3_free(zSql);
47024 if( rc ) return 0;
47025 rc = sqlite3_step(pStmt);
47026 if( rc!=SQLITE_ROW ){
47027 pOut = 0;
47028 }else{
47029 sz = sqlite3_column_int64(pStmt, 0)*szPage;
47030 if( piSize ) *piSize = sz;
47031 if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
47032 pOut = 0;
47033 }else{
47034 pOut = sqlite3_malloc64( sz );
47035 if( pOut ){
47036 int nPage = sqlite3_column_int(pStmt, 0);
47037 Pager *pPager = sqlite3BtreePager(pBt);
47038 int pgno;
47039 for(pgno=1; pgno<=nPage; pgno++){
47040 DbPage *pPage = 0;
47041 unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
47042 rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pPage, 0);
47043 if( rc==SQLITE_OK ){
47044 memcpy(pTo, sqlite3PagerGetData(pPage), szPage);
47045 }else{
47046 memset(pTo, 0, szPage);
47047 }
47048 sqlite3PagerUnref(pPage);
47049 }
47050 }
47051 }
47052 }
47053 sqlite3_finalize(pStmt);
47054 return pOut;
47055}
47056
47057/* Convert zSchema to a MemDB and initialize its content.
47058*/
47059SQLITE_API int sqlite3_deserialize(
47060 sqlite3 *db, /* The database connection */
47061 const char *zSchema, /* Which DB to reopen with the deserialization */
47062 unsigned char *pData, /* The serialized database content */
47063 sqlite3_int64 szDb, /* Number bytes in the deserialization */
47064 sqlite3_int64 szBuf, /* Total size of buffer pData[] */
47065 unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */
47066){
47067 MemFile *p;
47068 char *zSql;
47069 sqlite3_stmt *pStmt = 0;
47070 int rc;
47071 int iDb;
47072
47073#ifdef SQLITE_ENABLE_API_ARMOR
47074 if( !sqlite3SafetyCheckOk(db) ){
47075 return SQLITE_MISUSE_BKPT;
47076 }
47077 if( szDb<0 ) return SQLITE_MISUSE_BKPT;
47078 if( szBuf<0 ) return SQLITE_MISUSE_BKPT;
47079#endif
47080
47081 sqlite3_mutex_enter(db->mutex);
47082 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
47083 iDb = sqlite3FindDbName(db, zSchema);
47084 if( iDb<0 ){
47085 rc = SQLITE_ERROR;
47086 goto end_deserialize;
47087 }
47088 zSql = sqlite3_mprintf("ATTACH x AS %Q", zSchema);
47089 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
47090 sqlite3_free(zSql);
47091 if( rc ) goto end_deserialize;
47092 db->init.iDb = (u8)iDb;
47093 db->init.reopenMemdb = 1;
47094 rc = sqlite3_step(pStmt);
47095 db->init.reopenMemdb = 0;
47096 if( rc!=SQLITE_DONE ){
47097 rc = SQLITE_ERROR;
47098 goto end_deserialize;
47099 }
47100 p = memdbFromDbSchema(db, zSchema);
47101 if( p==0 ){
47102 rc = SQLITE_ERROR;
47103 }else{
47104 p->aData = pData;
47105 p->sz = szDb;
47106 p->szMax = szBuf;
47107 p->mFlags = mFlags;
47108 rc = SQLITE_OK;
47109 }
47110
47111end_deserialize:
47112 sqlite3_finalize(pStmt);
47113 sqlite3_mutex_leave(db->mutex);
47114 return rc;
47115}
47116
47117/*
47118** This routine is called when the extension is loaded.
47119** Register the new VFS.
47120*/
47121SQLITE_PRIVATE int sqlite3MemdbInit(void){
47122 sqlite3_vfs *pLower = sqlite3_vfs_find(0);
47123 int sz = pLower->szOsFile;
47124 memdb_vfs.pAppData = pLower;
47125 /* In all known configurations of SQLite, the size of a default
47126 ** sqlite3_file is greater than the size of a memdb sqlite3_file.
47127 ** Should that ever change, remove the following NEVER() */
47128 if( NEVER(sz<sizeof(MemFile)) ) sz = sizeof(MemFile);
47129 memdb_vfs.szOsFile = sz;
47130 return sqlite3_vfs_register(&memdb_vfs, 0);
47131}
47132#endif /* SQLITE_ENABLE_DESERIALIZE */
47133
47134/************** End of memdb.c ***********************************************/
47135/************** Begin file bitvec.c ******************************************/
47136/*
47137** 2008 February 16
47138**
47139** The author disclaims copyright to this source code. In place of
47140** a legal notice, here is a blessing:
47141**
47142** May you do good and not evil.
47143** May you find forgiveness for yourself and forgive others.
47144** May you share freely, never taking more than you give.
47145**
47146*************************************************************************
47147** This file implements an object that represents a fixed-length
47148** bitmap. Bits are numbered starting with 1.
47149**
47150** A bitmap is used to record which pages of a database file have been
47151** journalled during a transaction, or which pages have the "dont-write"
47152** property. Usually only a few pages are meet either condition.
47153** So the bitmap is usually sparse and has low cardinality.
47154** But sometimes (for example when during a DROP of a large table) most
47155** or all of the pages in a database can get journalled. In those cases,
47156** the bitmap becomes dense with high cardinality. The algorithm needs
47157** to handle both cases well.
47158**
47159** The size of the bitmap is fixed when the object is created.
47160**
47161** All bits are clear when the bitmap is created. Individual bits
47162** may be set or cleared one at a time.
47163**
47164** Test operations are about 100 times more common that set operations.
47165** Clear operations are exceedingly rare. There are usually between
47166** 5 and 500 set operations per Bitvec object, though the number of sets can
47167** sometimes grow into tens of thousands or larger. The size of the
47168** Bitvec object is the number of pages in the database file at the
47169** start of a transaction, and is thus usually less than a few thousand,
47170** but can be as large as 2 billion for a really big database.
47171*/
47172/* #include "sqliteInt.h" */
47173
47174/* Size of the Bitvec structure in bytes. */
47175#define BITVEC_SZ 512
47176
47177/* Round the union size down to the nearest pointer boundary, since that's how
47178** it will be aligned within the Bitvec struct. */
47179#define BITVEC_USIZE \
47180 (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
47181
47182/* Type of the array "element" for the bitmap representation.
47183** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE.
47184** Setting this to the "natural word" size of your CPU may improve
47185** performance. */
47186#define BITVEC_TELEM u8
47187/* Size, in bits, of the bitmap element. */
47188#define BITVEC_SZELEM 8
47189/* Number of elements in a bitmap array. */
47190#define BITVEC_NELEM (BITVEC_USIZE/sizeof(BITVEC_TELEM))
47191/* Number of bits in the bitmap array. */
47192#define BITVEC_NBIT (BITVEC_NELEM*BITVEC_SZELEM)
47193
47194/* Number of u32 values in hash table. */
47195#define BITVEC_NINT (BITVEC_USIZE/sizeof(u32))
47196/* Maximum number of entries in hash table before
47197** sub-dividing and re-hashing. */
47198#define BITVEC_MXHASH (BITVEC_NINT/2)
47199/* Hashing function for the aHash representation.
47200** Empirical testing showed that the *37 multiplier
47201** (an arbitrary prime)in the hash function provided
47202** no fewer collisions than the no-op *1. */
47203#define BITVEC_HASH(X) (((X)*1)%BITVEC_NINT)
47204
47205#define BITVEC_NPTR (BITVEC_USIZE/sizeof(Bitvec *))
47206
47207
47208/*
47209** A bitmap is an instance of the following structure.
47210**
47211** This bitmap records the existence of zero or more bits
47212** with values between 1 and iSize, inclusive.
47213**
47214** There are three possible representations of the bitmap.
47215** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
47216** bitmap. The least significant bit is bit 1.
47217**
47218** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
47219** a hash table that will hold up to BITVEC_MXHASH distinct values.
47220**
47221** Otherwise, the value i is redirected into one of BITVEC_NPTR
47222** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
47223** handles up to iDivisor separate values of i. apSub[0] holds
47224** values between 1 and iDivisor. apSub[1] holds values between
47225** iDivisor+1 and 2*iDivisor. apSub[N] holds values between
47226** N*iDivisor+1 and (N+1)*iDivisor. Each subbitmap is normalized
47227** to hold deal with values between 1 and iDivisor.
47228*/
47229struct Bitvec {
47230 u32 iSize; /* Maximum bit index. Max iSize is 4,294,967,296. */
47231 u32 nSet; /* Number of bits that are set - only valid for aHash
47232 ** element. Max is BITVEC_NINT. For BITVEC_SZ of 512,
47233 ** this would be 125. */
47234 u32 iDivisor; /* Number of bits handled by each apSub[] entry. */
47235 /* Should >=0 for apSub element. */
47236 /* Max iDivisor is max(u32) / BITVEC_NPTR + 1. */
47237 /* For a BITVEC_SZ of 512, this would be 34,359,739. */
47238 union {
47239 BITVEC_TELEM aBitmap[BITVEC_NELEM]; /* Bitmap representation */
47240 u32 aHash[BITVEC_NINT]; /* Hash table representation */
47241 Bitvec *apSub[BITVEC_NPTR]; /* Recursive representation */
47242 } u;
47243};
47244
47245/*
47246** Create a new bitmap object able to handle bits between 0 and iSize,
47247** inclusive. Return a pointer to the new object. Return NULL if
47248** malloc fails.
47249*/
47250SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32 iSize){
47251 Bitvec *p;
47252 assert( sizeof(*p)==BITVEC_SZ );
47253 p = sqlite3MallocZero( sizeof(*p) );
47254 if( p ){
47255 p->iSize = iSize;
47256 }
47257 return p;
47258}
47259
47260/*
47261** Check to see if the i-th bit is set. Return true or false.
47262** If p is NULL (if the bitmap has not been created) or if
47263** i is out of range, then return false.
47264*/
47265SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec *p, u32 i){
47266 assert( p!=0 );
47267 i--;
47268 if( i>=p->iSize ) return 0;
47269 while( p->iDivisor ){
47270 u32 bin = i/p->iDivisor;
47271 i = i%p->iDivisor;
47272 p = p->u.apSub[bin];
47273 if (!p) {
47274 return 0;
47275 }
47276 }
47277 if( p->iSize<=BITVEC_NBIT ){
47278 return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
47279 } else{
47280 u32 h = BITVEC_HASH(i++);
47281 while( p->u.aHash[h] ){
47282 if( p->u.aHash[h]==i ) return 1;
47283 h = (h+1) % BITVEC_NINT;
47284 }
47285 return 0;
47286 }
47287}
47288SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
47289 return p!=0 && sqlite3BitvecTestNotNull(p,i);
47290}
47291
47292/*
47293** Set the i-th bit. Return 0 on success and an error code if
47294** anything goes wrong.
47295**
47296** This routine might cause sub-bitmaps to be allocated. Failing
47297** to get the memory needed to hold the sub-bitmap is the only
47298** that can go wrong with an insert, assuming p and i are valid.
47299**
47300** The calling function must ensure that p is a valid Bitvec object
47301** and that the value for "i" is within range of the Bitvec object.
47302** Otherwise the behavior is undefined.
47303*/
47304SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
47305 u32 h;
47306 if( p==0 ) return SQLITE_OK;
47307 assert( i>0 );
47308 assert( i<=p->iSize );
47309 i--;
47310 while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
47311 u32 bin = i/p->iDivisor;
47312 i = i%p->iDivisor;
47313 if( p->u.apSub[bin]==0 ){
47314 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
47315 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT;
47316 }
47317 p = p->u.apSub[bin];
47318 }
47319 if( p->iSize<=BITVEC_NBIT ){
47320 p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
47321 return SQLITE_OK;
47322 }
47323 h = BITVEC_HASH(i++);
47324 /* if there wasn't a hash collision, and this doesn't */
47325 /* completely fill the hash, then just add it without */
47326 /* worring about sub-dividing and re-hashing. */
47327 if( !p->u.aHash[h] ){
47328 if (p->nSet<(BITVEC_NINT-1)) {
47329 goto bitvec_set_end;
47330 } else {
47331 goto bitvec_set_rehash;
47332 }
47333 }
47334 /* there was a collision, check to see if it's already */
47335 /* in hash, if not, try to find a spot for it */
47336 do {
47337 if( p->u.aHash[h]==i ) return SQLITE_OK;
47338 h++;
47339 if( h>=BITVEC_NINT ) h = 0;
47340 } while( p->u.aHash[h] );
47341 /* we didn't find it in the hash. h points to the first */
47342 /* available free spot. check to see if this is going to */
47343 /* make our hash too "full". */
47344bitvec_set_rehash:
47345 if( p->nSet>=BITVEC_MXHASH ){
47346 unsigned int j;
47347 int rc;
47348 u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
47349 if( aiValues==0 ){
47350 return SQLITE_NOMEM_BKPT;
47351 }else{
47352 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
47353 memset(p->u.apSub, 0, sizeof(p->u.apSub));
47354 p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
47355 rc = sqlite3BitvecSet(p, i);
47356 for(j=0; j<BITVEC_NINT; j++){
47357 if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
47358 }
47359 sqlite3StackFree(0, aiValues);
47360 return rc;
47361 }
47362 }
47363bitvec_set_end:
47364 p->nSet++;
47365 p->u.aHash[h] = i;
47366 return SQLITE_OK;
47367}
47368
47369/*
47370** Clear the i-th bit.
47371**
47372** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
47373** that BitvecClear can use to rebuilt its hash table.
47374*/
47375SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
47376 if( p==0 ) return;
47377 assert( i>0 );
47378 i--;
47379 while( p->iDivisor ){
47380 u32 bin = i/p->iDivisor;
47381 i = i%p->iDivisor;
47382 p = p->u.apSub[bin];
47383 if (!p) {
47384 return;
47385 }
47386 }
47387 if( p->iSize<=BITVEC_NBIT ){
47388 p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
47389 }else{
47390 unsigned int j;
47391 u32 *aiValues = pBuf;
47392 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
47393 memset(p->u.aHash, 0, sizeof(p->u.aHash));
47394 p->nSet = 0;
47395 for(j=0; j<BITVEC_NINT; j++){
47396 if( aiValues[j] && aiValues[j]!=(i+1) ){
47397 u32 h = BITVEC_HASH(aiValues[j]-1);
47398 p->nSet++;
47399 while( p->u.aHash[h] ){
47400 h++;
47401 if( h>=BITVEC_NINT ) h = 0;
47402 }
47403 p->u.aHash[h] = aiValues[j];
47404 }
47405 }
47406 }
47407}
47408
47409/*
47410** Destroy a bitmap object. Reclaim all memory used.
47411*/
47412SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){
47413 if( p==0 ) return;
47414 if( p->iDivisor ){
47415 unsigned int i;
47416 for(i=0; i<BITVEC_NPTR; i++){
47417 sqlite3BitvecDestroy(p->u.apSub[i]);
47418 }
47419 }
47420 sqlite3_free(p);
47421}
47422
47423/*
47424** Return the value of the iSize parameter specified when Bitvec *p
47425** was created.
47426*/
47427SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec *p){
47428 return p->iSize;
47429}
47430
47431#ifndef SQLITE_UNTESTABLE
47432/*
47433** Let V[] be an array of unsigned characters sufficient to hold
47434** up to N bits. Let I be an integer between 0 and N. 0<=I<N.
47435** Then the following macros can be used to set, clear, or test
47436** individual bits within V.
47437*/
47438#define SETBIT(V,I) V[I>>3] |= (1<<(I&7))
47439#define CLEARBIT(V,I) V[I>>3] &= ~(1<<(I&7))
47440#define TESTBIT(V,I) (V[I>>3]&(1<<(I&7)))!=0
47441
47442/*
47443** This routine runs an extensive test of the Bitvec code.
47444**
47445** The input is an array of integers that acts as a program
47446** to test the Bitvec. The integers are opcodes followed
47447** by 0, 1, or 3 operands, depending on the opcode. Another
47448** opcode follows immediately after the last operand.
47449**
47450** There are 6 opcodes numbered from 0 through 5. 0 is the
47451** "halt" opcode and causes the test to end.
47452**
47453** 0 Halt and return the number of errors
47454** 1 N S X Set N bits beginning with S and incrementing by X
47455** 2 N S X Clear N bits beginning with S and incrementing by X
47456** 3 N Set N randomly chosen bits
47457** 4 N Clear N randomly chosen bits
47458** 5 N S X Set N bits from S increment X in array only, not in bitvec
47459**
47460** The opcodes 1 through 4 perform set and clear operations are performed
47461** on both a Bitvec object and on a linear array of bits obtained from malloc.
47462** Opcode 5 works on the linear array only, not on the Bitvec.
47463** Opcode 5 is used to deliberately induce a fault in order to
47464** confirm that error detection works.
47465**
47466** At the conclusion of the test the linear array is compared
47467** against the Bitvec object. If there are any differences,
47468** an error is returned. If they are the same, zero is returned.
47469**
47470** If a memory allocation error occurs, return -1.
47471*/
47472SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int sz, int *aOp){
47473 Bitvec *pBitvec = 0;
47474 unsigned char *pV = 0;
47475 int rc = -1;
47476 int i, nx, pc, op;
47477 void *pTmpSpace;
47478
47479 /* Allocate the Bitvec to be tested and a linear array of
47480 ** bits to act as the reference */
47481 pBitvec = sqlite3BitvecCreate( sz );
47482 pV = sqlite3MallocZero( (sz+7)/8 + 1 );
47483 pTmpSpace = sqlite3_malloc64(BITVEC_SZ);
47484 if( pBitvec==0 || pV==0 || pTmpSpace==0 ) goto bitvec_end;
47485
47486 /* NULL pBitvec tests */
47487 sqlite3BitvecSet(0, 1);
47488 sqlite3BitvecClear(0, 1, pTmpSpace);
47489
47490 /* Run the program */
47491 pc = 0;
47492 while( (op = aOp[pc])!=0 ){
47493 switch( op ){
47494 case 1:
47495 case 2:
47496 case 5: {
47497 nx = 4;
47498 i = aOp[pc+2] - 1;
47499 aOp[pc+2] += aOp[pc+3];
47500 break;
47501 }
47502 case 3:
47503 case 4:
47504 default: {
47505 nx = 2;
47506 sqlite3_randomness(sizeof(i), &i);
47507 break;
47508 }
47509 }
47510 if( (--aOp[pc+1]) > 0 ) nx = 0;
47511 pc += nx;
47512 i = (i & 0x7fffffff)%sz;
47513 if( (op & 1)!=0 ){
47514 SETBIT(pV, (i+1));
47515 if( op!=5 ){
47516 if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
47517 }
47518 }else{
47519 CLEARBIT(pV, (i+1));
47520 sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
47521 }
47522 }
47523
47524 /* Test to make sure the linear array exactly matches the
47525 ** Bitvec object. Start with the assumption that they do
47526 ** match (rc==0). Change rc to non-zero if a discrepancy
47527 ** is found.
47528 */
47529 rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
47530 + sqlite3BitvecTest(pBitvec, 0)
47531 + (sqlite3BitvecSize(pBitvec) - sz);
47532 for(i=1; i<=sz; i++){
47533 if( (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){
47534 rc = i;
47535 break;
47536 }
47537 }
47538
47539 /* Free allocated structure */
47540bitvec_end:
47541 sqlite3_free(pTmpSpace);
47542 sqlite3_free(pV);
47543 sqlite3BitvecDestroy(pBitvec);
47544 return rc;
47545}
47546#endif /* SQLITE_UNTESTABLE */
47547
47548/************** End of bitvec.c **********************************************/
47549/************** Begin file pcache.c ******************************************/
47550/*
47551** 2008 August 05
47552**
47553** The author disclaims copyright to this source code. In place of
47554** a legal notice, here is a blessing:
47555**
47556** May you do good and not evil.
47557** May you find forgiveness for yourself and forgive others.
47558** May you share freely, never taking more than you give.
47559**
47560*************************************************************************
47561** This file implements that page cache.
47562*/
47563/* #include "sqliteInt.h" */
47564
47565/*
47566** A complete page cache is an instance of this structure. Every
47567** entry in the cache holds a single page of the database file. The
47568** btree layer only operates on the cached copy of the database pages.
47569**
47570** A page cache entry is "clean" if it exactly matches what is currently
47571** on disk. A page is "dirty" if it has been modified and needs to be
47572** persisted to disk.
47573**
47574** pDirty, pDirtyTail, pSynced:
47575** All dirty pages are linked into the doubly linked list using
47576** PgHdr.pDirtyNext and pDirtyPrev. The list is maintained in LRU order
47577** such that p was added to the list more recently than p->pDirtyNext.
47578** PCache.pDirty points to the first (newest) element in the list and
47579** pDirtyTail to the last (oldest).
47580**
47581** The PCache.pSynced variable is used to optimize searching for a dirty
47582** page to eject from the cache mid-transaction. It is better to eject
47583** a page that does not require a journal sync than one that does.
47584** Therefore, pSynced is maintained so that it *almost* always points
47585** to either the oldest page in the pDirty/pDirtyTail list that has a
47586** clear PGHDR_NEED_SYNC flag or to a page that is older than this one
47587** (so that the right page to eject can be found by following pDirtyPrev
47588** pointers).
47589*/
47590struct PCache {
47591 PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
47592 PgHdr *pSynced; /* Last synced page in dirty page list */
47593 int nRefSum; /* Sum of ref counts over all pages */
47594 int szCache; /* Configured cache size */
47595 int szSpill; /* Size before spilling occurs */
47596 int szPage; /* Size of every page in this cache */
47597 int szExtra; /* Size of extra space for each page */
47598 u8 bPurgeable; /* True if pages are on backing store */
47599 u8 eCreate; /* eCreate value for for xFetch() */
47600 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
47601 void *pStress; /* Argument to xStress */
47602 sqlite3_pcache *pCache; /* Pluggable cache module */
47603};
47604
47605/********************************** Test and Debug Logic **********************/
47606/*
47607** Debug tracing macros. Enable by by changing the "0" to "1" and
47608** recompiling.
47609**
47610** When sqlite3PcacheTrace is 1, single line trace messages are issued.
47611** When sqlite3PcacheTrace is 2, a dump of the pcache showing all cache entries
47612** is displayed for many operations, resulting in a lot of output.
47613*/
47614#if defined(SQLITE_DEBUG) && 0
47615 int sqlite3PcacheTrace = 2; /* 0: off 1: simple 2: cache dumps */
47616 int sqlite3PcacheMxDump = 9999; /* Max cache entries for pcacheDump() */
47617# define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;}
47618 void pcacheDump(PCache *pCache){
47619 int N;
47620 int i, j;
47621 sqlite3_pcache_page *pLower;
47622 PgHdr *pPg;
47623 unsigned char *a;
47624
47625 if( sqlite3PcacheTrace<2 ) return;
47626 if( pCache->pCache==0 ) return;
47627 N = sqlite3PcachePagecount(pCache);
47628 if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump;
47629 for(i=1; i<=N; i++){
47630 pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
47631 if( pLower==0 ) continue;
47632 pPg = (PgHdr*)pLower->pExtra;
47633 printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
47634 a = (unsigned char *)pLower->pBuf;
47635 for(j=0; j<12; j++) printf("%02x", a[j]);
47636 printf("\n");
47637 if( pPg->pPage==0 ){
47638 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
47639 }
47640 }
47641 }
47642 #else
47643# define pcacheTrace(X)
47644# define pcacheDump(X)
47645#endif
47646
47647/*
47648** Check invariants on a PgHdr entry. Return true if everything is OK.
47649** Return false if any invariant is violated.
47650**
47651** This routine is for use inside of assert() statements only. For
47652** example:
47653**
47654** assert( sqlite3PcachePageSanity(pPg) );
47655*/
47656#ifdef SQLITE_DEBUG
47657SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr *pPg){
47658 PCache *pCache;
47659 assert( pPg!=0 );
47660 assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */
47661 pCache = pPg->pCache;
47662 assert( pCache!=0 ); /* Every page has an associated PCache */
47663 if( pPg->flags & PGHDR_CLEAN ){
47664 assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
47665 assert( pCache->pDirty!=pPg ); /* CLEAN pages not on dirty list */
47666 assert( pCache->pDirtyTail!=pPg );
47667 }
47668 /* WRITEABLE pages must also be DIRTY */
47669 if( pPg->flags & PGHDR_WRITEABLE ){
47670 assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */
47671 }
47672 /* NEED_SYNC can be set independently of WRITEABLE. This can happen,
47673 ** for example, when using the sqlite3PagerDontWrite() optimization:
47674 ** (1) Page X is journalled, and gets WRITEABLE and NEED_SEEK.
47675 ** (2) Page X moved to freelist, WRITEABLE is cleared
47676 ** (3) Page X reused, WRITEABLE is set again
47677 ** If NEED_SYNC had been cleared in step 2, then it would not be reset
47678 ** in step 3, and page might be written into the database without first
47679 ** syncing the rollback journal, which might cause corruption on a power
47680 ** loss.
47681 **
47682 ** Another example is when the database page size is smaller than the
47683 ** disk sector size. When any page of a sector is journalled, all pages
47684 ** in that sector are marked NEED_SYNC even if they are still CLEAN, just
47685 ** in case they are later modified, since all pages in the same sector
47686 ** must be journalled and synced before any of those pages can be safely
47687 ** written.
47688 */
47689 return 1;
47690}
47691#endif /* SQLITE_DEBUG */
47692
47693
47694/********************************** Linked List Management ********************/
47695
47696/* Allowed values for second argument to pcacheManageDirtyList() */
47697#define PCACHE_DIRTYLIST_REMOVE 1 /* Remove pPage from dirty list */
47698#define PCACHE_DIRTYLIST_ADD 2 /* Add pPage to the dirty list */
47699#define PCACHE_DIRTYLIST_FRONT 3 /* Move pPage to the front of the list */
47700
47701/*
47702** Manage pPage's participation on the dirty list. Bits of the addRemove
47703** argument determines what operation to do. The 0x01 bit means first
47704** remove pPage from the dirty list. The 0x02 means add pPage back to
47705** the dirty list. Doing both moves pPage to the front of the dirty list.
47706*/
47707static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
47708 PCache *p = pPage->pCache;
47709
47710 pcacheTrace(("%p.DIRTYLIST.%s %d\n", p,
47711 addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT",
47712 pPage->pgno));
47713 if( addRemove & PCACHE_DIRTYLIST_REMOVE ){
47714 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
47715 assert( pPage->pDirtyPrev || pPage==p->pDirty );
47716
47717 /* Update the PCache1.pSynced variable if necessary. */
47718 if( p->pSynced==pPage ){
47719 p->pSynced = pPage->pDirtyPrev;
47720 }
47721
47722 if( pPage->pDirtyNext ){
47723 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
47724 }else{
47725 assert( pPage==p->pDirtyTail );
47726 p->pDirtyTail = pPage->pDirtyPrev;
47727 }
47728 if( pPage->pDirtyPrev ){
47729 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
47730 }else{
47731 /* If there are now no dirty pages in the cache, set eCreate to 2.
47732 ** This is an optimization that allows sqlite3PcacheFetch() to skip
47733 ** searching for a dirty page to eject from the cache when it might
47734 ** otherwise have to. */
47735 assert( pPage==p->pDirty );
47736 p->pDirty = pPage->pDirtyNext;
47737 assert( p->bPurgeable || p->eCreate==2 );
47738 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
47739 assert( p->bPurgeable==0 || p->eCreate==1 );
47740 p->eCreate = 2;
47741 }
47742 }
47743 }
47744 if( addRemove & PCACHE_DIRTYLIST_ADD ){
47745 pPage->pDirtyPrev = 0;
47746 pPage->pDirtyNext = p->pDirty;
47747 if( pPage->pDirtyNext ){
47748 assert( pPage->pDirtyNext->pDirtyPrev==0 );
47749 pPage->pDirtyNext->pDirtyPrev = pPage;
47750 }else{
47751 p->pDirtyTail = pPage;
47752 if( p->bPurgeable ){
47753 assert( p->eCreate==2 );
47754 p->eCreate = 1;
47755 }
47756 }
47757 p->pDirty = pPage;
47758
47759 /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set
47760 ** pSynced to point to it. Checking the NEED_SYNC flag is an
47761 ** optimization, as if pSynced points to a page with the NEED_SYNC
47762 ** flag set sqlite3PcacheFetchStress() searches through all newer
47763 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
47764 if( !p->pSynced
47765 && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/
47766 ){
47767 p->pSynced = pPage;
47768 }
47769 }
47770 pcacheDump(p);
47771}
47772
47773/*
47774** Wrapper around the pluggable caches xUnpin method. If the cache is
47775** being used for an in-memory database, this function is a no-op.
47776*/
47777static void pcacheUnpin(PgHdr *p){
47778 if( p->pCache->bPurgeable ){
47779 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
47780 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
47781 pcacheDump(p->pCache);
47782 }
47783}
47784
47785/*
47786** Compute the number of pages of cache requested. p->szCache is the
47787** cache size requested by the "PRAGMA cache_size" statement.
47788*/
47789static int numberOfCachePages(PCache *p){
47790 if( p->szCache>=0 ){
47791 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
47792 ** suggested cache size is set to N. */
47793 return p->szCache;
47794 }else{
47795 /* IMPLEMENTATION-OF: R-61436-13639 If the argument N is negative, then
47796 ** the number of cache pages is adjusted to use approximately abs(N*1024)
47797 ** bytes of memory. */
47798 return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
47799 }
47800}
47801
47802/*************************************************** General Interfaces ******
47803**
47804** Initialize and shutdown the page cache subsystem. Neither of these
47805** functions are threadsafe.
47806*/
47807SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
47808 if( sqlite3GlobalConfig.pcache2.xInit==0 ){
47809 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
47810 ** built-in default page cache is used instead of the application defined
47811 ** page cache. */
47812 sqlite3PCacheSetDefault();
47813 }
47814 return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
47815}
47816SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
47817 if( sqlite3GlobalConfig.pcache2.xShutdown ){
47818 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
47819 sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
47820 }
47821}
47822
47823/*
47824** Return the size in bytes of a PCache object.
47825*/
47826SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
47827
47828/*
47829** Create a new PCache object. Storage space to hold the object
47830** has already been allocated and is passed in as the p pointer.
47831** The caller discovers how much space needs to be allocated by
47832** calling sqlite3PcacheSize().
47833**
47834** szExtra is some extra space allocated for each page. The first
47835** 8 bytes of the extra space will be zeroed as the page is allocated,
47836** but remaining content will be uninitialized. Though it is opaque
47837** to this module, the extra space really ends up being the MemPage
47838** structure in the pager.
47839*/
47840SQLITE_PRIVATE int sqlite3PcacheOpen(
47841 int szPage, /* Size of every page */
47842 int szExtra, /* Extra space associated with each page */
47843 int bPurgeable, /* True if pages are on backing store */
47844 int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
47845 void *pStress, /* Argument to xStress */
47846 PCache *p /* Preallocated space for the PCache */
47847){
47848 memset(p, 0, sizeof(PCache));
47849 p->szPage = 1;
47850 p->szExtra = szExtra;
47851 assert( szExtra>=8 ); /* First 8 bytes will be zeroed */
47852 p->bPurgeable = bPurgeable;
47853 p->eCreate = 2;
47854 p->xStress = xStress;
47855 p->pStress = pStress;
47856 p->szCache = 100;
47857 p->szSpill = 1;
47858 pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable));
47859 return sqlite3PcacheSetPageSize(p, szPage);
47860}
47861
47862/*
47863** Change the page size for PCache object. The caller must ensure that there
47864** are no outstanding page references when this function is called.
47865*/
47866SQLITE_PRIVATE int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
47867 assert( pCache->nRefSum==0 && pCache->pDirty==0 );
47868 if( pCache->szPage ){
47869 sqlite3_pcache *pNew;
47870 pNew = sqlite3GlobalConfig.pcache2.xCreate(
47871 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
47872 pCache->bPurgeable
47873 );
47874 if( pNew==0 ) return SQLITE_NOMEM_BKPT;
47875 sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
47876 if( pCache->pCache ){
47877 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
47878 }
47879 pCache->pCache = pNew;
47880 pCache->szPage = szPage;
47881 pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage));
47882 }
47883 return SQLITE_OK;
47884}
47885
47886/*
47887** Try to obtain a page from the cache.
47888**
47889** This routine returns a pointer to an sqlite3_pcache_page object if
47890** such an object is already in cache, or if a new one is created.
47891** This routine returns a NULL pointer if the object was not in cache
47892** and could not be created.
47893**
47894** The createFlags should be 0 to check for existing pages and should
47895** be 3 (not 1, but 3) to try to create a new page.
47896**
47897** If the createFlag is 0, then NULL is always returned if the page
47898** is not already in the cache. If createFlag is 1, then a new page
47899** is created only if that can be done without spilling dirty pages
47900** and without exceeding the cache size limit.
47901**
47902** The caller needs to invoke sqlite3PcacheFetchFinish() to properly
47903** initialize the sqlite3_pcache_page object and convert it into a
47904** PgHdr object. The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish()
47905** routines are split this way for performance reasons. When separated
47906** they can both (usually) operate without having to push values to
47907** the stack on entry and pop them back off on exit, which saves a
47908** lot of pushing and popping.
47909*/
47910SQLITE_PRIVATE sqlite3_pcache_page *sqlite3PcacheFetch(
47911 PCache *pCache, /* Obtain the page from this cache */
47912 Pgno pgno, /* Page number to obtain */
47913 int createFlag /* If true, create page if it does not exist already */
47914){
47915 int eCreate;
47916 sqlite3_pcache_page *pRes;
47917
47918 assert( pCache!=0 );
47919 assert( pCache->pCache!=0 );
47920 assert( createFlag==3 || createFlag==0 );
47921 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
47922
47923 /* eCreate defines what to do if the page does not exist.
47924 ** 0 Do not allocate a new page. (createFlag==0)
47925 ** 1 Allocate a new page if doing so is inexpensive.
47926 ** (createFlag==1 AND bPurgeable AND pDirty)
47927 ** 2 Allocate a new page even it doing so is difficult.
47928 ** (createFlag==1 AND !(bPurgeable AND pDirty)
47929 */
47930 eCreate = createFlag & pCache->eCreate;
47931 assert( eCreate==0 || eCreate==1 || eCreate==2 );
47932 assert( createFlag==0 || pCache->eCreate==eCreate );
47933 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
47934 pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
47935 pcacheTrace(("%p.FETCH %d%s (result: %p)\n",pCache,pgno,
47936 createFlag?" create":"",pRes));
47937 return pRes;
47938}
47939
47940/*
47941** If the sqlite3PcacheFetch() routine is unable to allocate a new
47942** page because no clean pages are available for reuse and the cache
47943** size limit has been reached, then this routine can be invoked to
47944** try harder to allocate a page. This routine might invoke the stress
47945** callback to spill dirty pages to the journal. It will then try to
47946** allocate the new page and will only fail to allocate a new page on
47947** an OOM error.
47948**
47949** This routine should be invoked only after sqlite3PcacheFetch() fails.
47950*/
47951SQLITE_PRIVATE int sqlite3PcacheFetchStress(
47952 PCache *pCache, /* Obtain the page from this cache */
47953 Pgno pgno, /* Page number to obtain */
47954 sqlite3_pcache_page **ppPage /* Write result here */
47955){
47956 PgHdr *pPg;
47957 if( pCache->eCreate==2 ) return 0;
47958
47959 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
47960 /* Find a dirty page to write-out and recycle. First try to find a
47961 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
47962 ** cleared), but if that is not possible settle for any other
47963 ** unreferenced dirty page.
47964 **
47965 ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC
47966 ** flag is currently referenced, then the following may leave pSynced
47967 ** set incorrectly (pointing to other than the LRU page with NEED_SYNC
47968 ** cleared). This is Ok, as pSynced is just an optimization. */
47969 for(pPg=pCache->pSynced;
47970 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
47971 pPg=pPg->pDirtyPrev
47972 );
47973 pCache->pSynced = pPg;
47974 if( !pPg ){
47975 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
47976 }
47977 if( pPg ){
47978 int rc;
47979#ifdef SQLITE_LOG_CACHE_SPILL
47980 sqlite3_log(SQLITE_FULL,
47981 "spill page %d making room for %d - cache used: %d/%d",
47982 pPg->pgno, pgno,
47983 sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
47984 numberOfCachePages(pCache));
47985#endif
47986 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
47987 rc = pCache->xStress(pCache->pStress, pPg);
47988 pcacheDump(pCache);
47989 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
47990 return rc;
47991 }
47992 }
47993 }
47994 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
47995 return *ppPage==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
47996}
47997
47998/*
47999** This is a helper routine for sqlite3PcacheFetchFinish()
48000**
48001** In the uncommon case where the page being fetched has not been
48002** initialized, this routine is invoked to do the initialization.
48003** This routine is broken out into a separate function since it
48004** requires extra stack manipulation that can be avoided in the common
48005** case.
48006*/
48007static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit(
48008 PCache *pCache, /* Obtain the page from this cache */
48009 Pgno pgno, /* Page number obtained */
48010 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
48011){
48012 PgHdr *pPgHdr;
48013 assert( pPage!=0 );
48014 pPgHdr = (PgHdr*)pPage->pExtra;
48015 assert( pPgHdr->pPage==0 );
48016 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
48017 pPgHdr->pPage = pPage;
48018 pPgHdr->pData = pPage->pBuf;
48019 pPgHdr->pExtra = (void *)&pPgHdr[1];
48020 memset(pPgHdr->pExtra, 0, 8);
48021 pPgHdr->pCache = pCache;
48022 pPgHdr->pgno = pgno;
48023 pPgHdr->flags = PGHDR_CLEAN;
48024 return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
48025}
48026
48027/*
48028** This routine converts the sqlite3_pcache_page object returned by
48029** sqlite3PcacheFetch() into an initialized PgHdr object. This routine
48030** must be called after sqlite3PcacheFetch() in order to get a usable
48031** result.
48032*/
48033SQLITE_PRIVATE PgHdr *sqlite3PcacheFetchFinish(
48034 PCache *pCache, /* Obtain the page from this cache */
48035 Pgno pgno, /* Page number obtained */
48036 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
48037){
48038 PgHdr *pPgHdr;
48039
48040 assert( pPage!=0 );
48041 pPgHdr = (PgHdr *)pPage->pExtra;
48042
48043 if( !pPgHdr->pPage ){
48044 return pcacheFetchFinishWithInit(pCache, pgno, pPage);
48045 }
48046 pCache->nRefSum++;
48047 pPgHdr->nRef++;
48048 assert( sqlite3PcachePageSanity(pPgHdr) );
48049 return pPgHdr;
48050}
48051
48052/*
48053** Decrement the reference count on a page. If the page is clean and the
48054** reference count drops to 0, then it is made eligible for recycling.
48055*/
48056SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
48057 assert( p->nRef>0 );
48058 p->pCache->nRefSum--;
48059 if( (--p->nRef)==0 ){
48060 if( p->flags&PGHDR_CLEAN ){
48061 pcacheUnpin(p);
48062 }else{
48063 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
48064 }
48065 }
48066}
48067
48068/*
48069** Increase the reference count of a supplied page by 1.
48070*/
48071SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
48072 assert(p->nRef>0);
48073 assert( sqlite3PcachePageSanity(p) );
48074 p->nRef++;
48075 p->pCache->nRefSum++;
48076}
48077
48078/*
48079** Drop a page from the cache. There must be exactly one reference to the
48080** page. This function deletes that reference, so after it returns the
48081** page pointed to by p is invalid.
48082*/
48083SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
48084 assert( p->nRef==1 );
48085 assert( sqlite3PcachePageSanity(p) );
48086 if( p->flags&PGHDR_DIRTY ){
48087 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
48088 }
48089 p->pCache->nRefSum--;
48090 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
48091}
48092
48093/*
48094** Make sure the page is marked as dirty. If it isn't dirty already,
48095** make it so.
48096*/
48097SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
48098 assert( p->nRef>0 );
48099 assert( sqlite3PcachePageSanity(p) );
48100 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/
48101 p->flags &= ~PGHDR_DONT_WRITE;
48102 if( p->flags & PGHDR_CLEAN ){
48103 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
48104 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
48105 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
48106 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
48107 }
48108 assert( sqlite3PcachePageSanity(p) );
48109 }
48110}
48111
48112/*
48113** Make sure the page is marked as clean. If it isn't clean already,
48114** make it so.
48115*/
48116SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
48117 assert( sqlite3PcachePageSanity(p) );
48118 assert( (p->flags & PGHDR_DIRTY)!=0 );
48119 assert( (p->flags & PGHDR_CLEAN)==0 );
48120 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
48121 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
48122 p->flags |= PGHDR_CLEAN;
48123 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
48124 assert( sqlite3PcachePageSanity(p) );
48125 if( p->nRef==0 ){
48126 pcacheUnpin(p);
48127 }
48128}
48129
48130/*
48131** Make every page in the cache clean.
48132*/
48133SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
48134 PgHdr *p;
48135 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
48136 while( (p = pCache->pDirty)!=0 ){
48137 sqlite3PcacheMakeClean(p);
48138 }
48139}
48140
48141/*
48142** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages.
48143*/
48144SQLITE_PRIVATE void sqlite3PcacheClearWritable(PCache *pCache){
48145 PgHdr *p;
48146 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
48147 for(p=pCache->pDirty; p; p=p->pDirtyNext){
48148 p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
48149 }
48150 pCache->pSynced = pCache->pDirtyTail;
48151}
48152
48153/*
48154** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
48155*/
48156SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
48157 PgHdr *p;
48158 for(p=pCache->pDirty; p; p=p->pDirtyNext){
48159 p->flags &= ~PGHDR_NEED_SYNC;
48160 }
48161 pCache->pSynced = pCache->pDirtyTail;
48162}
48163
48164/*
48165** Change the page number of page p to newPgno.
48166*/
48167SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
48168 PCache *pCache = p->pCache;
48169 assert( p->nRef>0 );
48170 assert( newPgno>0 );
48171 assert( sqlite3PcachePageSanity(p) );
48172 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
48173 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
48174 p->pgno = newPgno;
48175 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
48176 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
48177 }
48178}
48179
48180/*
48181** Drop every cache entry whose page number is greater than "pgno". The
48182** caller must ensure that there are no outstanding references to any pages
48183** other than page 1 with a page number greater than pgno.
48184**
48185** If there is a reference to page 1 and the pgno parameter passed to this
48186** function is 0, then the data area associated with page 1 is zeroed, but
48187** the page object is not dropped.
48188*/
48189SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
48190 if( pCache->pCache ){
48191 PgHdr *p;
48192 PgHdr *pNext;
48193 pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno));
48194 for(p=pCache->pDirty; p; p=pNext){
48195 pNext = p->pDirtyNext;
48196 /* This routine never gets call with a positive pgno except right
48197 ** after sqlite3PcacheCleanAll(). So if there are dirty pages,
48198 ** it must be that pgno==0.
48199 */
48200 assert( p->pgno>0 );
48201 if( p->pgno>pgno ){
48202 assert( p->flags&PGHDR_DIRTY );
48203 sqlite3PcacheMakeClean(p);
48204 }
48205 }
48206 if( pgno==0 && pCache->nRefSum ){
48207 sqlite3_pcache_page *pPage1;
48208 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
48209 if( ALWAYS(pPage1) ){ /* Page 1 is always available in cache, because
48210 ** pCache->nRefSum>0 */
48211 memset(pPage1->pBuf, 0, pCache->szPage);
48212 pgno = 1;
48213 }
48214 }
48215 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
48216 }
48217}
48218
48219/*
48220** Close a cache.
48221*/
48222SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
48223 assert( pCache->pCache!=0 );
48224 pcacheTrace(("%p.CLOSE\n",pCache));
48225 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
48226}
48227
48228/*
48229** Discard the contents of the cache.
48230*/
48231SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){
48232 sqlite3PcacheTruncate(pCache, 0);
48233}
48234
48235/*
48236** Merge two lists of pages connected by pDirty and in pgno order.
48237** Do not bother fixing the pDirtyPrev pointers.
48238*/
48239static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
48240 PgHdr result, *pTail;
48241 pTail = &result;
48242 assert( pA!=0 && pB!=0 );
48243 for(;;){
48244 if( pA->pgno<pB->pgno ){
48245 pTail->pDirty = pA;
48246 pTail = pA;
48247 pA = pA->pDirty;
48248 if( pA==0 ){
48249 pTail->pDirty = pB;
48250 break;
48251 }
48252 }else{
48253 pTail->pDirty = pB;
48254 pTail = pB;
48255 pB = pB->pDirty;
48256 if( pB==0 ){
48257 pTail->pDirty = pA;
48258 break;
48259 }
48260 }
48261 }
48262 return result.pDirty;
48263}
48264
48265/*
48266** Sort the list of pages in accending order by pgno. Pages are
48267** connected by pDirty pointers. The pDirtyPrev pointers are
48268** corrupted by this sort.
48269**
48270** Since there cannot be more than 2^31 distinct pages in a database,
48271** there cannot be more than 31 buckets required by the merge sorter.
48272** One extra bucket is added to catch overflow in case something
48273** ever changes to make the previous sentence incorrect.
48274*/
48275#define N_SORT_BUCKET 32
48276static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
48277 PgHdr *a[N_SORT_BUCKET], *p;
48278 int i;
48279 memset(a, 0, sizeof(a));
48280 while( pIn ){
48281 p = pIn;
48282 pIn = p->pDirty;
48283 p->pDirty = 0;
48284 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
48285 if( a[i]==0 ){
48286 a[i] = p;
48287 break;
48288 }else{
48289 p = pcacheMergeDirtyList(a[i], p);
48290 a[i] = 0;
48291 }
48292 }
48293 if( NEVER(i==N_SORT_BUCKET-1) ){
48294 /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
48295 ** the input list. But that is impossible.
48296 */
48297 a[i] = pcacheMergeDirtyList(a[i], p);
48298 }
48299 }
48300 p = a[0];
48301 for(i=1; i<N_SORT_BUCKET; i++){
48302 if( a[i]==0 ) continue;
48303 p = p ? pcacheMergeDirtyList(p, a[i]) : a[i];
48304 }
48305 return p;
48306}
48307
48308/*
48309** Return a list of all dirty pages in the cache, sorted by page number.
48310*/
48311SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
48312 PgHdr *p;
48313 for(p=pCache->pDirty; p; p=p->pDirtyNext){
48314 p->pDirty = p->pDirtyNext;
48315 }
48316 return pcacheSortDirtyList(pCache->pDirty);
48317}
48318
48319/*
48320** Return the total number of references to all pages held by the cache.
48321**
48322** This is not the total number of pages referenced, but the sum of the
48323** reference count for all pages.
48324*/
48325SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){
48326 return pCache->nRefSum;
48327}
48328
48329/*
48330** Return the number of references to the page supplied as an argument.
48331*/
48332SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){
48333 return p->nRef;
48334}
48335
48336/*
48337** Return the total number of pages in the cache.
48338*/
48339SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
48340 assert( pCache->pCache!=0 );
48341 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
48342}
48343
48344#ifdef SQLITE_TEST
48345/*
48346** Get the suggested cache-size value.
48347*/
48348SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
48349 return numberOfCachePages(pCache);
48350}
48351#endif
48352
48353/*
48354** Set the suggested cache-size value.
48355*/
48356SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
48357 assert( pCache->pCache!=0 );
48358 pCache->szCache = mxPage;
48359 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
48360 numberOfCachePages(pCache));
48361}
48362
48363/*
48364** Set the suggested cache-spill value. Make no changes if if the
48365** argument is zero. Return the effective cache-spill size, which will
48366** be the larger of the szSpill and szCache.
48367*/
48368SQLITE_PRIVATE int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){
48369 int res;
48370 assert( p->pCache!=0 );
48371 if( mxPage ){
48372 if( mxPage<0 ){
48373 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
48374 }
48375 p->szSpill = mxPage;
48376 }
48377 res = numberOfCachePages(p);
48378 if( res<p->szSpill ) res = p->szSpill;
48379 return res;
48380}
48381
48382/*
48383** Free up as much memory as possible from the page cache.
48384*/
48385SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){
48386 assert( pCache->pCache!=0 );
48387 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
48388}
48389
48390/*
48391** Return the size of the header added by this middleware layer
48392** in the page-cache hierarchy.
48393*/
48394SQLITE_PRIVATE int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); }
48395
48396/*
48397** Return the number of dirty pages currently in the cache, as a percentage
48398** of the configured cache size.
48399*/
48400SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache *pCache){
48401 PgHdr *pDirty;
48402 int nDirty = 0;
48403 int nCache = numberOfCachePages(pCache);
48404 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
48405 return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;
48406}
48407
48408#ifdef SQLITE_DIRECT_OVERFLOW_READ
48409/*
48410** Return true if there are one or more dirty pages in the cache. Else false.
48411*/
48412SQLITE_PRIVATE int sqlite3PCacheIsDirty(PCache *pCache){
48413 return (pCache->pDirty!=0);
48414}
48415#endif
48416
48417#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
48418/*
48419** For all dirty pages currently in the cache, invoke the specified
48420** callback. This is only used if the SQLITE_CHECK_PAGES macro is
48421** defined.
48422*/
48423SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
48424 PgHdr *pDirty;
48425 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
48426 xIter(pDirty);
48427 }
48428}
48429#endif
48430
48431/************** End of pcache.c **********************************************/
48432/************** Begin file pcache1.c *****************************************/
48433/*
48434** 2008 November 05
48435**
48436** The author disclaims copyright to this source code. In place of
48437** a legal notice, here is a blessing:
48438**
48439** May you do good and not evil.
48440** May you find forgiveness for yourself and forgive others.
48441** May you share freely, never taking more than you give.
48442**
48443*************************************************************************
48444**
48445** This file implements the default page cache implementation (the
48446** sqlite3_pcache interface). It also contains part of the implementation
48447** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
48448** If the default page cache implementation is overridden, then neither of
48449** these two features are available.
48450**
48451** A Page cache line looks like this:
48452**
48453** -------------------------------------------------------------
48454** | database page content | PgHdr1 | MemPage | PgHdr |
48455** -------------------------------------------------------------
48456**
48457** The database page content is up front (so that buffer overreads tend to
48458** flow harmlessly into the PgHdr1, MemPage, and PgHdr extensions). MemPage
48459** is the extension added by the btree.c module containing information such
48460** as the database page number and how that database page is used. PgHdr
48461** is added by the pcache.c layer and contains information used to keep track
48462** of which pages are "dirty". PgHdr1 is an extension added by this
48463** module (pcache1.c). The PgHdr1 header is a subclass of sqlite3_pcache_page.
48464** PgHdr1 contains information needed to look up a page by its page number.
48465** The superclass sqlite3_pcache_page.pBuf points to the start of the
48466** database page content and sqlite3_pcache_page.pExtra points to PgHdr.
48467**
48468** The size of the extension (MemPage+PgHdr+PgHdr1) can be determined at
48469** runtime using sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &size). The
48470** sizes of the extensions sum to 272 bytes on x64 for 3.8.10, but this
48471** size can vary according to architecture, compile-time options, and
48472** SQLite library version number.
48473**
48474** If SQLITE_PCACHE_SEPARATE_HEADER is defined, then the extension is obtained
48475** using a separate memory allocation from the database page content. This
48476** seeks to overcome the "clownshoe" problem (also called "internal
48477** fragmentation" in academic literature) of allocating a few bytes more
48478** than a power of two with the memory allocator rounding up to the next
48479** power of two, and leaving the rounded-up space unused.
48480**
48481** This module tracks pointers to PgHdr1 objects. Only pcache.c communicates
48482** with this module. Information is passed back and forth as PgHdr1 pointers.
48483**
48484** The pcache.c and pager.c modules deal pointers to PgHdr objects.
48485** The btree.c module deals with pointers to MemPage objects.
48486**
48487** SOURCE OF PAGE CACHE MEMORY:
48488**
48489** Memory for a page might come from any of three sources:
48490**
48491** (1) The general-purpose memory allocator - sqlite3Malloc()
48492** (2) Global page-cache memory provided using sqlite3_config() with
48493** SQLITE_CONFIG_PAGECACHE.
48494** (3) PCache-local bulk allocation.
48495**
48496** The third case is a chunk of heap memory (defaulting to 100 pages worth)
48497** that is allocated when the page cache is created. The size of the local
48498** bulk allocation can be adjusted using
48499**
48500** sqlite3_config(SQLITE_CONFIG_PAGECACHE, (void*)0, 0, N).
48501**
48502** If N is positive, then N pages worth of memory are allocated using a single
48503** sqlite3Malloc() call and that memory is used for the first N pages allocated.
48504** Or if N is negative, then -1024*N bytes of memory are allocated and used
48505** for as many pages as can be accomodated.
48506**
48507** Only one of (2) or (3) can be used. Once the memory available to (2) or
48508** (3) is exhausted, subsequent allocations fail over to the general-purpose
48509** memory allocator (1).
48510**
48511** Earlier versions of SQLite used only methods (1) and (2). But experiments
48512** show that method (3) with N==100 provides about a 5% performance boost for
48513** common workloads.
48514*/
48515/* #include "sqliteInt.h" */
48516
48517typedef struct PCache1 PCache1;
48518typedef struct PgHdr1 PgHdr1;
48519typedef struct PgFreeslot PgFreeslot;
48520typedef struct PGroup PGroup;
48521
48522/*
48523** Each cache entry is represented by an instance of the following
48524** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
48525** PgHdr1.pCache->szPage bytes is allocated directly before this structure
48526** in memory.
48527*/
48528struct PgHdr1 {
48529 sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */
48530 unsigned int iKey; /* Key value (page number) */
48531 u8 isBulkLocal; /* This page from bulk local storage */
48532 u8 isAnchor; /* This is the PGroup.lru element */
48533 PgHdr1 *pNext; /* Next in hash table chain */
48534 PCache1 *pCache; /* Cache that currently owns this page */
48535 PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */
48536 PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
48537};
48538
48539/*
48540** A page is pinned if it is not on the LRU list. To be "pinned" means
48541** that the page is in active use and must not be deallocated.
48542*/
48543#define PAGE_IS_PINNED(p) ((p)->pLruNext==0)
48544#define PAGE_IS_UNPINNED(p) ((p)->pLruNext!=0)
48545
48546/* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
48547** of one or more PCaches that are able to recycle each other's unpinned
48548** pages when they are under memory pressure. A PGroup is an instance of
48549** the following object.
48550**
48551** This page cache implementation works in one of two modes:
48552**
48553** (1) Every PCache is the sole member of its own PGroup. There is
48554** one PGroup per PCache.
48555**
48556** (2) There is a single global PGroup that all PCaches are a member
48557** of.
48558**
48559** Mode 1 uses more memory (since PCache instances are not able to rob
48560** unused pages from other PCaches) but it also operates without a mutex,
48561** and is therefore often faster. Mode 2 requires a mutex in order to be
48562** threadsafe, but recycles pages more efficiently.
48563**
48564** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single
48565** PGroup which is the pcache1.grp global variable and its mutex is
48566** SQLITE_MUTEX_STATIC_LRU.
48567*/
48568struct PGroup {
48569 sqlite3_mutex *mutex; /* MUTEX_STATIC_LRU or NULL */
48570 unsigned int nMaxPage; /* Sum of nMax for purgeable caches */
48571 unsigned int nMinPage; /* Sum of nMin for purgeable caches */
48572 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
48573 unsigned int nPurgeable; /* Number of purgeable pages allocated */
48574 PgHdr1 lru; /* The beginning and end of the LRU list */
48575};
48576
48577/* Each page cache is an instance of the following object. Every
48578** open database file (including each in-memory database and each
48579** temporary or transient database) has a single page cache which
48580** is an instance of this object.
48581**
48582** Pointers to structures of this type are cast and returned as
48583** opaque sqlite3_pcache* handles.
48584*/
48585struct PCache1 {
48586 /* Cache configuration parameters. Page size (szPage) and the purgeable
48587 ** flag (bPurgeable) and the pnPurgeable pointer are all set when the
48588 ** cache is created and are never changed thereafter. nMax may be
48589 ** modified at any time by a call to the pcache1Cachesize() method.
48590 ** The PGroup mutex must be held when accessing nMax.
48591 */
48592 PGroup *pGroup; /* PGroup this cache belongs to */
48593 unsigned int *pnPurgeable; /* Pointer to pGroup->nPurgeable */
48594 int szPage; /* Size of database content section */
48595 int szExtra; /* sizeof(MemPage)+sizeof(PgHdr) */
48596 int szAlloc; /* Total size of one pcache line */
48597 int bPurgeable; /* True if cache is purgeable */
48598 unsigned int nMin; /* Minimum number of pages reserved */
48599 unsigned int nMax; /* Configured "cache_size" value */
48600 unsigned int n90pct; /* nMax*9/10 */
48601 unsigned int iMaxKey; /* Largest key seen since xTruncate() */
48602
48603 /* Hash table of all pages. The following variables may only be accessed
48604 ** when the accessor is holding the PGroup mutex.
48605 */
48606 unsigned int nRecyclable; /* Number of pages in the LRU list */
48607 unsigned int nPage; /* Total number of pages in apHash */
48608 unsigned int nHash; /* Number of slots in apHash[] */
48609 PgHdr1 **apHash; /* Hash table for fast lookup by key */
48610 PgHdr1 *pFree; /* List of unused pcache-local pages */
48611 void *pBulk; /* Bulk memory used by pcache-local */
48612};
48613
48614/*
48615** Free slots in the allocator used to divide up the global page cache
48616** buffer provided using the SQLITE_CONFIG_PAGECACHE mechanism.
48617*/
48618struct PgFreeslot {
48619 PgFreeslot *pNext; /* Next free slot */
48620};
48621
48622/*
48623** Global data used by this cache.
48624*/
48625static SQLITE_WSD struct PCacheGlobal {
48626 PGroup grp; /* The global PGroup for mode (2) */
48627
48628 /* Variables related to SQLITE_CONFIG_PAGECACHE settings. The
48629 ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
48630 ** fixed at sqlite3_initialize() time and do not require mutex protection.
48631 ** The nFreeSlot and pFree values do require mutex protection.
48632 */
48633 int isInit; /* True if initialized */
48634 int separateCache; /* Use a new PGroup for each PCache */
48635 int nInitPage; /* Initial bulk allocation size */
48636 int szSlot; /* Size of each free slot */
48637 int nSlot; /* The number of pcache slots */
48638 int nReserve; /* Try to keep nFreeSlot above this */
48639 void *pStart, *pEnd; /* Bounds of global page cache memory */
48640 /* Above requires no mutex. Use mutex below for variable that follow. */
48641 sqlite3_mutex *mutex; /* Mutex for accessing the following: */
48642 PgFreeslot *pFree; /* Free page blocks */
48643 int nFreeSlot; /* Number of unused pcache slots */
48644 /* The following value requires a mutex to change. We skip the mutex on
48645 ** reading because (1) most platforms read a 32-bit integer atomically and
48646 ** (2) even if an incorrect value is read, no great harm is done since this
48647 ** is really just an optimization. */
48648 int bUnderPressure; /* True if low on PAGECACHE memory */
48649} pcache1_g;
48650
48651/*
48652** All code in this file should access the global structure above via the
48653** alias "pcache1". This ensures that the WSD emulation is used when
48654** compiling for systems that do not support real WSD.
48655*/
48656#define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
48657
48658/*
48659** Macros to enter and leave the PCache LRU mutex.
48660*/
48661#if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
48662# define pcache1EnterMutex(X) assert((X)->mutex==0)
48663# define pcache1LeaveMutex(X) assert((X)->mutex==0)
48664# define PCACHE1_MIGHT_USE_GROUP_MUTEX 0
48665#else
48666# define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
48667# define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
48668# define PCACHE1_MIGHT_USE_GROUP_MUTEX 1
48669#endif
48670
48671/******************************************************************************/
48672/******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
48673
48674
48675/*
48676** This function is called during initialization if a static buffer is
48677** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
48678** verb to sqlite3_config(). Parameter pBuf points to an allocation large
48679** enough to contain 'n' buffers of 'sz' bytes each.
48680**
48681** This routine is called from sqlite3_initialize() and so it is guaranteed
48682** to be serialized already. There is no need for further mutexing.
48683*/
48684SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
48685 if( pcache1.isInit ){
48686 PgFreeslot *p;
48687 if( pBuf==0 ) sz = n = 0;
48688 if( n==0 ) sz = 0;
48689 sz = ROUNDDOWN8(sz);
48690 pcache1.szSlot = sz;
48691 pcache1.nSlot = pcache1.nFreeSlot = n;
48692 pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
48693 pcache1.pStart = pBuf;
48694 pcache1.pFree = 0;
48695 pcache1.bUnderPressure = 0;
48696 while( n-- ){
48697 p = (PgFreeslot*)pBuf;
48698 p->pNext = pcache1.pFree;
48699 pcache1.pFree = p;
48700 pBuf = (void*)&((char*)pBuf)[sz];
48701 }
48702 pcache1.pEnd = pBuf;
48703 }
48704}
48705
48706/*
48707** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
48708** true if pCache->pFree ends up containing one or more free pages.
48709*/
48710static int pcache1InitBulk(PCache1 *pCache){
48711 i64 szBulk;
48712 char *zBulk;
48713 if( pcache1.nInitPage==0 ) return 0;
48714 /* Do not bother with a bulk allocation if the cache size very small */
48715 if( pCache->nMax<3 ) return 0;
48716 sqlite3BeginBenignMalloc();
48717 if( pcache1.nInitPage>0 ){
48718 szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
48719 }else{
48720 szBulk = -1024 * (i64)pcache1.nInitPage;
48721 }
48722 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
48723 szBulk = pCache->szAlloc*(i64)pCache->nMax;
48724 }
48725 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
48726 sqlite3EndBenignMalloc();
48727 if( zBulk ){
48728 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
48729 do{
48730 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
48731 pX->page.pBuf = zBulk;
48732 pX->page.pExtra = &pX[1];
48733 pX->isBulkLocal = 1;
48734 pX->isAnchor = 0;
48735 pX->pNext = pCache->pFree;
48736 pCache->pFree = pX;
48737 zBulk += pCache->szAlloc;
48738 }while( --nBulk );
48739 }
48740 return pCache->pFree!=0;
48741}
48742
48743/*
48744** Malloc function used within this file to allocate space from the buffer
48745** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
48746** such buffer exists or there is no space left in it, this function falls
48747** back to sqlite3Malloc().
48748**
48749** Multiple threads can run this routine at the same time. Global variables
48750** in pcache1 need to be protected via mutex.
48751*/
48752static void *pcache1Alloc(int nByte){
48753 void *p = 0;
48754 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
48755 if( nByte<=pcache1.szSlot ){
48756 sqlite3_mutex_enter(pcache1.mutex);
48757 p = (PgHdr1 *)pcache1.pFree;
48758 if( p ){
48759 pcache1.pFree = pcache1.pFree->pNext;
48760 pcache1.nFreeSlot--;
48761 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
48762 assert( pcache1.nFreeSlot>=0 );
48763 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
48764 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, 1);
48765 }
48766 sqlite3_mutex_leave(pcache1.mutex);
48767 }
48768 if( p==0 ){
48769 /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get
48770 ** it from sqlite3Malloc instead.
48771 */
48772 p = sqlite3Malloc(nByte);
48773#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
48774 if( p ){
48775 int sz = sqlite3MallocSize(p);
48776 sqlite3_mutex_enter(pcache1.mutex);
48777 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
48778 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
48779 sqlite3_mutex_leave(pcache1.mutex);
48780 }
48781#endif
48782 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
48783 }
48784 return p;
48785}
48786
48787/*
48788** Free an allocated buffer obtained from pcache1Alloc().
48789*/
48790static void pcache1Free(void *p){
48791 if( p==0 ) return;
48792 if( SQLITE_WITHIN(p, pcache1.pStart, pcache1.pEnd) ){
48793 PgFreeslot *pSlot;
48794 sqlite3_mutex_enter(pcache1.mutex);
48795 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED, 1);
48796 pSlot = (PgFreeslot*)p;
48797 pSlot->pNext = pcache1.pFree;
48798 pcache1.pFree = pSlot;
48799 pcache1.nFreeSlot++;
48800 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
48801 assert( pcache1.nFreeSlot<=pcache1.nSlot );
48802 sqlite3_mutex_leave(pcache1.mutex);
48803 }else{
48804 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
48805 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
48806#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
48807 {
48808 int nFreed = 0;
48809 nFreed = sqlite3MallocSize(p);
48810 sqlite3_mutex_enter(pcache1.mutex);
48811 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, nFreed);
48812 sqlite3_mutex_leave(pcache1.mutex);
48813 }
48814#endif
48815 sqlite3_free(p);
48816 }
48817}
48818
48819#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
48820/*
48821** Return the size of a pcache allocation
48822*/
48823static int pcache1MemSize(void *p){
48824 if( p>=pcache1.pStart && p<pcache1.pEnd ){
48825 return pcache1.szSlot;
48826 }else{
48827 int iSize;
48828 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
48829 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
48830 iSize = sqlite3MallocSize(p);
48831 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
48832 return iSize;
48833 }
48834}
48835#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
48836
48837/*
48838** Allocate a new page object initially associated with cache pCache.
48839*/
48840static PgHdr1 *pcache1AllocPage(PCache1 *pCache, int benignMalloc){
48841 PgHdr1 *p = 0;
48842 void *pPg;
48843
48844 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
48845 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
48846 p = pCache->pFree;
48847 pCache->pFree = p->pNext;
48848 p->pNext = 0;
48849 }else{
48850#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
48851 /* The group mutex must be released before pcache1Alloc() is called. This
48852 ** is because it might call sqlite3_release_memory(), which assumes that
48853 ** this mutex is not held. */
48854 assert( pcache1.separateCache==0 );
48855 assert( pCache->pGroup==&pcache1.grp );
48856 pcache1LeaveMutex(pCache->pGroup);
48857#endif
48858 if( benignMalloc ){ sqlite3BeginBenignMalloc(); }
48859#ifdef SQLITE_PCACHE_SEPARATE_HEADER
48860 pPg = pcache1Alloc(pCache->szPage);
48861 p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
48862 if( !pPg || !p ){
48863 pcache1Free(pPg);
48864 sqlite3_free(p);
48865 pPg = 0;
48866 }
48867#else
48868 pPg = pcache1Alloc(pCache->szAlloc);
48869 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
48870#endif
48871 if( benignMalloc ){ sqlite3EndBenignMalloc(); }
48872#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
48873 pcache1EnterMutex(pCache->pGroup);
48874#endif
48875 if( pPg==0 ) return 0;
48876 p->page.pBuf = pPg;
48877 p->page.pExtra = &p[1];
48878 p->isBulkLocal = 0;
48879 p->isAnchor = 0;
48880 }
48881 (*pCache->pnPurgeable)++;
48882 return p;
48883}
48884
48885/*
48886** Free a page object allocated by pcache1AllocPage().
48887*/
48888static void pcache1FreePage(PgHdr1 *p){
48889 PCache1 *pCache;
48890 assert( p!=0 );
48891 pCache = p->pCache;
48892 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
48893 if( p->isBulkLocal ){
48894 p->pNext = pCache->pFree;
48895 pCache->pFree = p;
48896 }else{
48897 pcache1Free(p->page.pBuf);
48898#ifdef SQLITE_PCACHE_SEPARATE_HEADER
48899 sqlite3_free(p);
48900#endif
48901 }
48902 (*pCache->pnPurgeable)--;
48903}
48904
48905/*
48906** Malloc function used by SQLite to obtain space from the buffer configured
48907** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
48908** exists, this function falls back to sqlite3Malloc().
48909*/
48910SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
48911 return pcache1Alloc(sz);
48912}
48913
48914/*
48915** Free an allocated buffer obtained from sqlite3PageMalloc().
48916*/
48917SQLITE_PRIVATE void sqlite3PageFree(void *p){
48918 pcache1Free(p);
48919}
48920
48921
48922/*
48923** Return true if it desirable to avoid allocating a new page cache
48924** entry.
48925**
48926** If memory was allocated specifically to the page cache using
48927** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
48928** it is desirable to avoid allocating a new page cache entry because
48929** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
48930** for all page cache needs and we should not need to spill the
48931** allocation onto the heap.
48932**
48933** Or, the heap is used for all page cache memory but the heap is
48934** under memory pressure, then again it is desirable to avoid
48935** allocating a new page cache entry in order to avoid stressing
48936** the heap even further.
48937*/
48938static int pcache1UnderMemoryPressure(PCache1 *pCache){
48939 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
48940 return pcache1.bUnderPressure;
48941 }else{
48942 return sqlite3HeapNearlyFull();
48943 }
48944}
48945
48946/******************************************************************************/
48947/******** General Implementation Functions ************************************/
48948
48949/*
48950** This function is used to resize the hash table used by the cache passed
48951** as the first argument.
48952**
48953** The PCache mutex must be held when this function is called.
48954*/
48955static void pcache1ResizeHash(PCache1 *p){
48956 PgHdr1 **apNew;
48957 unsigned int nNew;
48958 unsigned int i;
48959
48960 assert( sqlite3_mutex_held(p->pGroup->mutex) );
48961
48962 nNew = p->nHash*2;
48963 if( nNew<256 ){
48964 nNew = 256;
48965 }
48966
48967 pcache1LeaveMutex(p->pGroup);
48968 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
48969 apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew);
48970 if( p->nHash ){ sqlite3EndBenignMalloc(); }
48971 pcache1EnterMutex(p->pGroup);
48972 if( apNew ){
48973 for(i=0; i<p->nHash; i++){
48974 PgHdr1 *pPage;
48975 PgHdr1 *pNext = p->apHash[i];
48976 while( (pPage = pNext)!=0 ){
48977 unsigned int h = pPage->iKey % nNew;
48978 pNext = pPage->pNext;
48979 pPage->pNext = apNew[h];
48980 apNew[h] = pPage;
48981 }
48982 }
48983 sqlite3_free(p->apHash);
48984 p->apHash = apNew;
48985 p->nHash = nNew;
48986 }
48987}
48988
48989/*
48990** This function is used internally to remove the page pPage from the
48991** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
48992** LRU list, then this function is a no-op.
48993**
48994** The PGroup mutex must be held when this function is called.
48995*/
48996static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){
48997 assert( pPage!=0 );
48998 assert( PAGE_IS_UNPINNED(pPage) );
48999 assert( pPage->pLruNext );
49000 assert( pPage->pLruPrev );
49001 assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
49002 pPage->pLruPrev->pLruNext = pPage->pLruNext;
49003 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
49004 pPage->pLruNext = 0;
49005 pPage->pLruPrev = 0;
49006 assert( pPage->isAnchor==0 );
49007 assert( pPage->pCache->pGroup->lru.isAnchor==1 );
49008 pPage->pCache->nRecyclable--;
49009 return pPage;
49010}
49011
49012
49013/*
49014** Remove the page supplied as an argument from the hash table
49015** (PCache1.apHash structure) that it is currently stored in.
49016** Also free the page if freePage is true.
49017**
49018** The PGroup mutex must be held when this function is called.
49019*/
49020static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){
49021 unsigned int h;
49022 PCache1 *pCache = pPage->pCache;
49023 PgHdr1 **pp;
49024
49025 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
49026 h = pPage->iKey % pCache->nHash;
49027 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
49028 *pp = (*pp)->pNext;
49029
49030 pCache->nPage--;
49031 if( freeFlag ) pcache1FreePage(pPage);
49032}
49033
49034/*
49035** If there are currently more than nMaxPage pages allocated, try
49036** to recycle pages to reduce the number allocated to nMaxPage.
49037*/
49038static void pcache1EnforceMaxPage(PCache1 *pCache){
49039 PGroup *pGroup = pCache->pGroup;
49040 PgHdr1 *p;
49041 assert( sqlite3_mutex_held(pGroup->mutex) );
49042 while( pGroup->nPurgeable>pGroup->nMaxPage
49043 && (p=pGroup->lru.pLruPrev)->isAnchor==0
49044 ){
49045 assert( p->pCache->pGroup==pGroup );
49046 assert( PAGE_IS_UNPINNED(p) );
49047 pcache1PinPage(p);
49048 pcache1RemoveFromHash(p, 1);
49049 }
49050 if( pCache->nPage==0 && pCache->pBulk ){
49051 sqlite3_free(pCache->pBulk);
49052 pCache->pBulk = pCache->pFree = 0;
49053 }
49054}
49055
49056/*
49057** Discard all pages from cache pCache with a page number (key value)
49058** greater than or equal to iLimit. Any pinned pages that meet this
49059** criteria are unpinned before they are discarded.
49060**
49061** The PCache mutex must be held when this function is called.
49062*/
49063static void pcache1TruncateUnsafe(
49064 PCache1 *pCache, /* The cache to truncate */
49065 unsigned int iLimit /* Drop pages with this pgno or larger */
49066){
49067 TESTONLY( int nPage = 0; ) /* To assert pCache->nPage is correct */
49068 unsigned int h, iStop;
49069 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
49070 assert( pCache->iMaxKey >= iLimit );
49071 assert( pCache->nHash > 0 );
49072 if( pCache->iMaxKey - iLimit < pCache->nHash ){
49073 /* If we are just shaving the last few pages off the end of the
49074 ** cache, then there is no point in scanning the entire hash table.
49075 ** Only scan those hash slots that might contain pages that need to
49076 ** be removed. */
49077 h = iLimit % pCache->nHash;
49078 iStop = pCache->iMaxKey % pCache->nHash;
49079 TESTONLY( nPage = -10; ) /* Disable the pCache->nPage validity check */
49080 }else{
49081 /* This is the general case where many pages are being removed.
49082 ** It is necessary to scan the entire hash table */
49083 h = pCache->nHash/2;
49084 iStop = h - 1;
49085 }
49086 for(;;){
49087 PgHdr1 **pp;
49088 PgHdr1 *pPage;
49089 assert( h<pCache->nHash );
49090 pp = &pCache->apHash[h];
49091 while( (pPage = *pp)!=0 ){
49092 if( pPage->iKey>=iLimit ){
49093 pCache->nPage--;
49094 *pp = pPage->pNext;
49095 if( PAGE_IS_UNPINNED(pPage) ) pcache1PinPage(pPage);
49096 pcache1FreePage(pPage);
49097 }else{
49098 pp = &pPage->pNext;
49099 TESTONLY( if( nPage>=0 ) nPage++; )
49100 }
49101 }
49102 if( h==iStop ) break;
49103 h = (h+1) % pCache->nHash;
49104 }
49105 assert( nPage<0 || pCache->nPage==(unsigned)nPage );
49106}
49107
49108/******************************************************************************/
49109/******** sqlite3_pcache Methods **********************************************/
49110
49111/*
49112** Implementation of the sqlite3_pcache.xInit method.
49113*/
49114static int pcache1Init(void *NotUsed){
49115 UNUSED_PARAMETER(NotUsed);
49116 assert( pcache1.isInit==0 );
49117 memset(&pcache1, 0, sizeof(pcache1));
49118
49119
49120 /*
49121 ** The pcache1.separateCache variable is true if each PCache has its own
49122 ** private PGroup (mode-1). pcache1.separateCache is false if the single
49123 ** PGroup in pcache1.grp is used for all page caches (mode-2).
49124 **
49125 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
49126 **
49127 ** * Use a unified cache in single-threaded applications that have
49128 ** configured a start-time buffer for use as page-cache memory using
49129 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
49130 ** pBuf argument.
49131 **
49132 ** * Otherwise use separate caches (mode-1)
49133 */
49134#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT)
49135 pcache1.separateCache = 0;
49136#elif SQLITE_THREADSAFE
49137 pcache1.separateCache = sqlite3GlobalConfig.pPage==0
49138 || sqlite3GlobalConfig.bCoreMutex>0;
49139#else
49140 pcache1.separateCache = sqlite3GlobalConfig.pPage==0;
49141#endif
49142
49143#if SQLITE_THREADSAFE
49144 if( sqlite3GlobalConfig.bCoreMutex ){
49145 pcache1.grp.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU);
49146 pcache1.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PMEM);
49147 }
49148#endif
49149 if( pcache1.separateCache
49150 && sqlite3GlobalConfig.nPage!=0
49151 && sqlite3GlobalConfig.pPage==0
49152 ){
49153 pcache1.nInitPage = sqlite3GlobalConfig.nPage;
49154 }else{
49155 pcache1.nInitPage = 0;
49156 }
49157 pcache1.grp.mxPinned = 10;
49158 pcache1.isInit = 1;
49159 return SQLITE_OK;
49160}
49161
49162/*
49163** Implementation of the sqlite3_pcache.xShutdown method.
49164** Note that the static mutex allocated in xInit does
49165** not need to be freed.
49166*/
49167static void pcache1Shutdown(void *NotUsed){
49168 UNUSED_PARAMETER(NotUsed);
49169 assert( pcache1.isInit!=0 );
49170 memset(&pcache1, 0, sizeof(pcache1));
49171}
49172
49173/* forward declaration */
49174static void pcache1Destroy(sqlite3_pcache *p);
49175
49176/*
49177** Implementation of the sqlite3_pcache.xCreate method.
49178**
49179** Allocate a new cache.
49180*/
49181static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
49182 PCache1 *pCache; /* The newly created page cache */
49183 PGroup *pGroup; /* The group the new page cache will belong to */
49184 int sz; /* Bytes of memory required to allocate the new cache */
49185
49186 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
49187 assert( szExtra < 300 );
49188
49189 sz = sizeof(PCache1) + sizeof(PGroup)*pcache1.separateCache;
49190 pCache = (PCache1 *)sqlite3MallocZero(sz);
49191 if( pCache ){
49192 if( pcache1.separateCache ){
49193 pGroup = (PGroup*)&pCache[1];
49194 pGroup->mxPinned = 10;
49195 }else{
49196 pGroup = &pcache1.grp;
49197 }
49198 if( pGroup->lru.isAnchor==0 ){
49199 pGroup->lru.isAnchor = 1;
49200 pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
49201 }
49202 pCache->pGroup = pGroup;
49203 pCache->szPage = szPage;
49204 pCache->szExtra = szExtra;
49205 pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1));
49206 pCache->bPurgeable = (bPurgeable ? 1 : 0);
49207 pcache1EnterMutex(pGroup);
49208 pcache1ResizeHash(pCache);
49209 if( bPurgeable ){
49210 pCache->nMin = 10;
49211 pGroup->nMinPage += pCache->nMin;
49212 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
49213 pCache->pnPurgeable = &pGroup->nPurgeable;
49214 }else{
49215 static unsigned int dummyCurrentPage;
49216 pCache->pnPurgeable = &dummyCurrentPage;
49217 }
49218 pcache1LeaveMutex(pGroup);
49219 if( pCache->nHash==0 ){
49220 pcache1Destroy((sqlite3_pcache*)pCache);
49221 pCache = 0;
49222 }
49223 }
49224 return (sqlite3_pcache *)pCache;
49225}
49226
49227/*
49228** Implementation of the sqlite3_pcache.xCachesize method.
49229**
49230** Configure the cache_size limit for a cache.
49231*/
49232static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
49233 PCache1 *pCache = (PCache1 *)p;
49234 if( pCache->bPurgeable ){
49235 PGroup *pGroup = pCache->pGroup;
49236 pcache1EnterMutex(pGroup);
49237 pGroup->nMaxPage += (nMax - pCache->nMax);
49238 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
49239 pCache->nMax = nMax;
49240 pCache->n90pct = pCache->nMax*9/10;
49241 pcache1EnforceMaxPage(pCache);
49242 pcache1LeaveMutex(pGroup);
49243 }
49244}
49245
49246/*
49247** Implementation of the sqlite3_pcache.xShrink method.
49248**
49249** Free up as much memory as possible.
49250*/
49251static void pcache1Shrink(sqlite3_pcache *p){
49252 PCache1 *pCache = (PCache1*)p;
49253 if( pCache->bPurgeable ){
49254 PGroup *pGroup = pCache->pGroup;
49255 int savedMaxPage;
49256 pcache1EnterMutex(pGroup);
49257 savedMaxPage = pGroup->nMaxPage;
49258 pGroup->nMaxPage = 0;
49259 pcache1EnforceMaxPage(pCache);
49260 pGroup->nMaxPage = savedMaxPage;
49261 pcache1LeaveMutex(pGroup);
49262 }
49263}
49264
49265/*
49266** Implementation of the sqlite3_pcache.xPagecount method.
49267*/
49268static int pcache1Pagecount(sqlite3_pcache *p){
49269 int n;
49270 PCache1 *pCache = (PCache1*)p;
49271 pcache1EnterMutex(pCache->pGroup);
49272 n = pCache->nPage;
49273 pcache1LeaveMutex(pCache->pGroup);
49274 return n;
49275}
49276
49277
49278/*
49279** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described
49280** in the header of the pcache1Fetch() procedure.
49281**
49282** This steps are broken out into a separate procedure because they are
49283** usually not needed, and by avoiding the stack initialization required
49284** for these steps, the main pcache1Fetch() procedure can run faster.
49285*/
49286static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2(
49287 PCache1 *pCache,
49288 unsigned int iKey,
49289 int createFlag
49290){
49291 unsigned int nPinned;
49292 PGroup *pGroup = pCache->pGroup;
49293 PgHdr1 *pPage = 0;
49294
49295 /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
49296 assert( pCache->nPage >= pCache->nRecyclable );
49297 nPinned = pCache->nPage - pCache->nRecyclable;
49298 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
49299 assert( pCache->n90pct == pCache->nMax*9/10 );
49300 if( createFlag==1 && (
49301 nPinned>=pGroup->mxPinned
49302 || nPinned>=pCache->n90pct
49303 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
49304 )){
49305 return 0;
49306 }
49307
49308 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
49309 assert( pCache->nHash>0 && pCache->apHash );
49310
49311 /* Step 4. Try to recycle a page. */
49312 if( pCache->bPurgeable
49313 && !pGroup->lru.pLruPrev->isAnchor
49314 && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
49315 ){
49316 PCache1 *pOther;
49317 pPage = pGroup->lru.pLruPrev;
49318 assert( PAGE_IS_UNPINNED(pPage) );
49319 pcache1RemoveFromHash(pPage, 0);
49320 pcache1PinPage(pPage);
49321 pOther = pPage->pCache;
49322 if( pOther->szAlloc != pCache->szAlloc ){
49323 pcache1FreePage(pPage);
49324 pPage = 0;
49325 }else{
49326 pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
49327 }
49328 }
49329
49330 /* Step 5. If a usable page buffer has still not been found,
49331 ** attempt to allocate a new one.
49332 */
49333 if( !pPage ){
49334 pPage = pcache1AllocPage(pCache, createFlag==1);
49335 }
49336
49337 if( pPage ){
49338 unsigned int h = iKey % pCache->nHash;
49339 pCache->nPage++;
49340 pPage->iKey = iKey;
49341 pPage->pNext = pCache->apHash[h];
49342 pPage->pCache = pCache;
49343 pPage->pLruPrev = 0;
49344 pPage->pLruNext = 0;
49345 *(void **)pPage->page.pExtra = 0;
49346 pCache->apHash[h] = pPage;
49347 if( iKey>pCache->iMaxKey ){
49348 pCache->iMaxKey = iKey;
49349 }
49350 }
49351 return pPage;
49352}
49353
49354/*
49355** Implementation of the sqlite3_pcache.xFetch method.
49356**
49357** Fetch a page by key value.
49358**
49359** Whether or not a new page may be allocated by this function depends on
49360** the value of the createFlag argument. 0 means do not allocate a new
49361** page. 1 means allocate a new page if space is easily available. 2
49362** means to try really hard to allocate a new page.
49363**
49364** For a non-purgeable cache (a cache used as the storage for an in-memory
49365** database) there is really no difference between createFlag 1 and 2. So
49366** the calling function (pcache.c) will never have a createFlag of 1 on
49367** a non-purgeable cache.
49368**
49369** There are three different approaches to obtaining space for a page,
49370** depending on the value of parameter createFlag (which may be 0, 1 or 2).
49371**
49372** 1. Regardless of the value of createFlag, the cache is searched for a
49373** copy of the requested page. If one is found, it is returned.
49374**
49375** 2. If createFlag==0 and the page is not already in the cache, NULL is
49376** returned.
49377**
49378** 3. If createFlag is 1, and the page is not already in the cache, then
49379** return NULL (do not allocate a new page) if any of the following
49380** conditions are true:
49381**
49382** (a) the number of pages pinned by the cache is greater than
49383** PCache1.nMax, or
49384**
49385** (b) the number of pages pinned by the cache is greater than
49386** the sum of nMax for all purgeable caches, less the sum of
49387** nMin for all other purgeable caches, or
49388**
49389** 4. If none of the first three conditions apply and the cache is marked
49390** as purgeable, and if one of the following is true:
49391**
49392** (a) The number of pages allocated for the cache is already
49393** PCache1.nMax, or
49394**
49395** (b) The number of pages allocated for all purgeable caches is
49396** already equal to or greater than the sum of nMax for all
49397** purgeable caches,
49398**
49399** (c) The system is under memory pressure and wants to avoid
49400** unnecessary pages cache entry allocations
49401**
49402** then attempt to recycle a page from the LRU list. If it is the right
49403** size, return the recycled buffer. Otherwise, free the buffer and
49404** proceed to step 5.
49405**
49406** 5. Otherwise, allocate and return a new page buffer.
49407**
49408** There are two versions of this routine. pcache1FetchWithMutex() is
49409** the general case. pcache1FetchNoMutex() is a faster implementation for
49410** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
49411** invokes the appropriate routine.
49412*/
49413static PgHdr1 *pcache1FetchNoMutex(
49414 sqlite3_pcache *p,
49415 unsigned int iKey,
49416 int createFlag
49417){
49418 PCache1 *pCache = (PCache1 *)p;
49419 PgHdr1 *pPage = 0;
49420
49421 /* Step 1: Search the hash table for an existing entry. */
49422 pPage = pCache->apHash[iKey % pCache->nHash];
49423 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
49424
49425 /* Step 2: If the page was found in the hash table, then return it.
49426 ** If the page was not in the hash table and createFlag is 0, abort.
49427 ** Otherwise (page not in hash and createFlag!=0) continue with
49428 ** subsequent steps to try to create the page. */
49429 if( pPage ){
49430 if( PAGE_IS_UNPINNED(pPage) ){
49431 return pcache1PinPage(pPage);
49432 }else{
49433 return pPage;
49434 }
49435 }else if( createFlag ){
49436 /* Steps 3, 4, and 5 implemented by this subroutine */
49437 return pcache1FetchStage2(pCache, iKey, createFlag);
49438 }else{
49439 return 0;
49440 }
49441}
49442#if PCACHE1_MIGHT_USE_GROUP_MUTEX
49443static PgHdr1 *pcache1FetchWithMutex(
49444 sqlite3_pcache *p,
49445 unsigned int iKey,
49446 int createFlag
49447){
49448 PCache1 *pCache = (PCache1 *)p;
49449 PgHdr1 *pPage;
49450
49451 pcache1EnterMutex(pCache->pGroup);
49452 pPage = pcache1FetchNoMutex(p, iKey, createFlag);
49453 assert( pPage==0 || pCache->iMaxKey>=iKey );
49454 pcache1LeaveMutex(pCache->pGroup);
49455 return pPage;
49456}
49457#endif
49458static sqlite3_pcache_page *pcache1Fetch(
49459 sqlite3_pcache *p,
49460 unsigned int iKey,
49461 int createFlag
49462){
49463#if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG)
49464 PCache1 *pCache = (PCache1 *)p;
49465#endif
49466
49467 assert( offsetof(PgHdr1,page)==0 );
49468 assert( pCache->bPurgeable || createFlag!=1 );
49469 assert( pCache->bPurgeable || pCache->nMin==0 );
49470 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
49471 assert( pCache->nMin==0 || pCache->bPurgeable );
49472 assert( pCache->nHash>0 );
49473#if PCACHE1_MIGHT_USE_GROUP_MUTEX
49474 if( pCache->pGroup->mutex ){
49475 return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag);
49476 }else
49477#endif
49478 {
49479 return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag);
49480 }
49481}
49482
49483
49484/*
49485** Implementation of the sqlite3_pcache.xUnpin method.
49486**
49487** Mark a page as unpinned (eligible for asynchronous recycling).
49488*/
49489static void pcache1Unpin(
49490 sqlite3_pcache *p,
49491 sqlite3_pcache_page *pPg,
49492 int reuseUnlikely
49493){
49494 PCache1 *pCache = (PCache1 *)p;
49495 PgHdr1 *pPage = (PgHdr1 *)pPg;
49496 PGroup *pGroup = pCache->pGroup;
49497
49498 assert( pPage->pCache==pCache );
49499 pcache1EnterMutex(pGroup);
49500
49501 /* It is an error to call this function if the page is already
49502 ** part of the PGroup LRU list.
49503 */
49504 assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
49505 assert( PAGE_IS_PINNED(pPage) );
49506
49507 if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
49508 pcache1RemoveFromHash(pPage, 1);
49509 }else{
49510 /* Add the page to the PGroup LRU list. */
49511 PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
49512 pPage->pLruPrev = &pGroup->lru;
49513 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
49514 *ppFirst = pPage;
49515 pCache->nRecyclable++;
49516 }
49517
49518 pcache1LeaveMutex(pCache->pGroup);
49519}
49520
49521/*
49522** Implementation of the sqlite3_pcache.xRekey method.
49523*/
49524static void pcache1Rekey(
49525 sqlite3_pcache *p,
49526 sqlite3_pcache_page *pPg,
49527 unsigned int iOld,
49528 unsigned int iNew
49529){
49530 PCache1 *pCache = (PCache1 *)p;
49531 PgHdr1 *pPage = (PgHdr1 *)pPg;
49532 PgHdr1 **pp;
49533 unsigned int h;
49534 assert( pPage->iKey==iOld );
49535 assert( pPage->pCache==pCache );
49536
49537 pcache1EnterMutex(pCache->pGroup);
49538
49539 h = iOld%pCache->nHash;
49540 pp = &pCache->apHash[h];
49541 while( (*pp)!=pPage ){
49542 pp = &(*pp)->pNext;
49543 }
49544 *pp = pPage->pNext;
49545
49546 h = iNew%pCache->nHash;
49547 pPage->iKey = iNew;
49548 pPage->pNext = pCache->apHash[h];
49549 pCache->apHash[h] = pPage;
49550 if( iNew>pCache->iMaxKey ){
49551 pCache->iMaxKey = iNew;
49552 }
49553
49554 pcache1LeaveMutex(pCache->pGroup);
49555}
49556
49557/*
49558** Implementation of the sqlite3_pcache.xTruncate method.
49559**
49560** Discard all unpinned pages in the cache with a page number equal to
49561** or greater than parameter iLimit. Any pinned pages with a page number
49562** equal to or greater than iLimit are implicitly unpinned.
49563*/
49564static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
49565 PCache1 *pCache = (PCache1 *)p;
49566 pcache1EnterMutex(pCache->pGroup);
49567 if( iLimit<=pCache->iMaxKey ){
49568 pcache1TruncateUnsafe(pCache, iLimit);
49569 pCache->iMaxKey = iLimit-1;
49570 }
49571 pcache1LeaveMutex(pCache->pGroup);
49572}
49573
49574/*
49575** Implementation of the sqlite3_pcache.xDestroy method.
49576**
49577** Destroy a cache allocated using pcache1Create().
49578*/
49579static void pcache1Destroy(sqlite3_pcache *p){
49580 PCache1 *pCache = (PCache1 *)p;
49581 PGroup *pGroup = pCache->pGroup;
49582 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
49583 pcache1EnterMutex(pGroup);
49584 if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
49585 assert( pGroup->nMaxPage >= pCache->nMax );
49586 pGroup->nMaxPage -= pCache->nMax;
49587 assert( pGroup->nMinPage >= pCache->nMin );
49588 pGroup->nMinPage -= pCache->nMin;
49589 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
49590 pcache1EnforceMaxPage(pCache);
49591 pcache1LeaveMutex(pGroup);
49592 sqlite3_free(pCache->pBulk);
49593 sqlite3_free(pCache->apHash);
49594 sqlite3_free(pCache);
49595}
49596
49597/*
49598** This function is called during initialization (sqlite3_initialize()) to
49599** install the default pluggable cache module, assuming the user has not
49600** already provided an alternative.
49601*/
49602SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){
49603 static const sqlite3_pcache_methods2 defaultMethods = {
49604 1, /* iVersion */
49605 0, /* pArg */
49606 pcache1Init, /* xInit */
49607 pcache1Shutdown, /* xShutdown */
49608 pcache1Create, /* xCreate */
49609 pcache1Cachesize, /* xCachesize */
49610 pcache1Pagecount, /* xPagecount */
49611 pcache1Fetch, /* xFetch */
49612 pcache1Unpin, /* xUnpin */
49613 pcache1Rekey, /* xRekey */
49614 pcache1Truncate, /* xTruncate */
49615 pcache1Destroy, /* xDestroy */
49616 pcache1Shrink /* xShrink */
49617 };
49618 sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
49619}
49620
49621/*
49622** Return the size of the header on each page of this PCACHE implementation.
49623*/
49624SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1)); }
49625
49626/*
49627** Return the global mutex used by this PCACHE implementation. The
49628** sqlite3_status() routine needs access to this mutex.
49629*/
49630SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void){
49631 return pcache1.mutex;
49632}
49633
49634#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
49635/*
49636** This function is called to free superfluous dynamically allocated memory
49637** held by the pager system. Memory in use by any SQLite pager allocated
49638** by the current thread may be sqlite3_free()ed.
49639**
49640** nReq is the number of bytes of memory required. Once this much has
49641** been released, the function returns. The return value is the total number
49642** of bytes of memory released.
49643*/
49644SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
49645 int nFree = 0;
49646 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
49647 assert( sqlite3_mutex_notheld(pcache1.mutex) );
49648 if( sqlite3GlobalConfig.pPage==0 ){
49649 PgHdr1 *p;
49650 pcache1EnterMutex(&pcache1.grp);
49651 while( (nReq<0 || nFree<nReq)
49652 && (p=pcache1.grp.lru.pLruPrev)!=0
49653 && p->isAnchor==0
49654 ){
49655 nFree += pcache1MemSize(p->page.pBuf);
49656#ifdef SQLITE_PCACHE_SEPARATE_HEADER
49657 nFree += sqlite3MemSize(p);
49658#endif
49659 assert( PAGE_IS_UNPINNED(p) );
49660 pcache1PinPage(p);
49661 pcache1RemoveFromHash(p, 1);
49662 }
49663 pcache1LeaveMutex(&pcache1.grp);
49664 }
49665 return nFree;
49666}
49667#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
49668
49669#ifdef SQLITE_TEST
49670/*
49671** This function is used by test procedures to inspect the internal state
49672** of the global cache.
49673*/
49674SQLITE_PRIVATE void sqlite3PcacheStats(
49675 int *pnCurrent, /* OUT: Total number of pages cached */
49676 int *pnMax, /* OUT: Global maximum cache size */
49677 int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */
49678 int *pnRecyclable /* OUT: Total number of pages available for recycling */
49679){
49680 PgHdr1 *p;
49681 int nRecyclable = 0;
49682 for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
49683 assert( PAGE_IS_UNPINNED(p) );
49684 nRecyclable++;
49685 }
49686 *pnCurrent = pcache1.grp.nPurgeable;
49687 *pnMax = (int)pcache1.grp.nMaxPage;
49688 *pnMin = (int)pcache1.grp.nMinPage;
49689 *pnRecyclable = nRecyclable;
49690}
49691#endif
49692
49693/************** End of pcache1.c *********************************************/
49694/************** Begin file rowset.c ******************************************/
49695/*
49696** 2008 December 3
49697**
49698** The author disclaims copyright to this source code. In place of
49699** a legal notice, here is a blessing:
49700**
49701** May you do good and not evil.
49702** May you find forgiveness for yourself and forgive others.
49703** May you share freely, never taking more than you give.
49704**
49705*************************************************************************
49706**
49707** This module implements an object we call a "RowSet".
49708**
49709** The RowSet object is a collection of rowids. Rowids
49710** are inserted into the RowSet in an arbitrary order. Inserts
49711** can be intermixed with tests to see if a given rowid has been
49712** previously inserted into the RowSet.
49713**
49714** After all inserts are finished, it is possible to extract the
49715** elements of the RowSet in sorted order. Once this extraction
49716** process has started, no new elements may be inserted.
49717**
49718** Hence, the primitive operations for a RowSet are:
49719**
49720** CREATE
49721** INSERT
49722** TEST
49723** SMALLEST
49724** DESTROY
49725**
49726** The CREATE and DESTROY primitives are the constructor and destructor,
49727** obviously. The INSERT primitive adds a new element to the RowSet.
49728** TEST checks to see if an element is already in the RowSet. SMALLEST
49729** extracts the least value from the RowSet.
49730**
49731** The INSERT primitive might allocate additional memory. Memory is
49732** allocated in chunks so most INSERTs do no allocation. There is an
49733** upper bound on the size of allocated memory. No memory is freed
49734** until DESTROY.
49735**
49736** The TEST primitive includes a "batch" number. The TEST primitive
49737** will only see elements that were inserted before the last change
49738** in the batch number. In other words, if an INSERT occurs between
49739** two TESTs where the TESTs have the same batch nubmer, then the
49740** value added by the INSERT will not be visible to the second TEST.
49741** The initial batch number is zero, so if the very first TEST contains
49742** a non-zero batch number, it will see all prior INSERTs.
49743**
49744** No INSERTs may occurs after a SMALLEST. An assertion will fail if
49745** that is attempted.
49746**
49747** The cost of an INSERT is roughly constant. (Sometimes new memory
49748** has to be allocated on an INSERT.) The cost of a TEST with a new
49749** batch number is O(NlogN) where N is the number of elements in the RowSet.
49750** The cost of a TEST using the same batch number is O(logN). The cost
49751** of the first SMALLEST is O(NlogN). Second and subsequent SMALLEST
49752** primitives are constant time. The cost of DESTROY is O(N).
49753**
49754** TEST and SMALLEST may not be used by the same RowSet. This used to
49755** be possible, but the feature was not used, so it was removed in order
49756** to simplify the code.
49757*/
49758/* #include "sqliteInt.h" */
49759
49760
49761/*
49762** Target size for allocation chunks.
49763*/
49764#define ROWSET_ALLOCATION_SIZE 1024
49765
49766/*
49767** The number of rowset entries per allocation chunk.
49768*/
49769#define ROWSET_ENTRY_PER_CHUNK \
49770 ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
49771
49772/*
49773** Each entry in a RowSet is an instance of the following object.
49774**
49775** This same object is reused to store a linked list of trees of RowSetEntry
49776** objects. In that alternative use, pRight points to the next entry
49777** in the list, pLeft points to the tree, and v is unused. The
49778** RowSet.pForest value points to the head of this forest list.
49779*/
49780struct RowSetEntry {
49781 i64 v; /* ROWID value for this entry */
49782 struct RowSetEntry *pRight; /* Right subtree (larger entries) or list */
49783 struct RowSetEntry *pLeft; /* Left subtree (smaller entries) */
49784};
49785
49786/*
49787** RowSetEntry objects are allocated in large chunks (instances of the
49788** following structure) to reduce memory allocation overhead. The
49789** chunks are kept on a linked list so that they can be deallocated
49790** when the RowSet is destroyed.
49791*/
49792struct RowSetChunk {
49793 struct RowSetChunk *pNextChunk; /* Next chunk on list of them all */
49794 struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK]; /* Allocated entries */
49795};
49796
49797/*
49798** A RowSet in an instance of the following structure.
49799**
49800** A typedef of this structure if found in sqliteInt.h.
49801*/
49802struct RowSet {
49803 struct RowSetChunk *pChunk; /* List of all chunk allocations */
49804 sqlite3 *db; /* The database connection */
49805 struct RowSetEntry *pEntry; /* List of entries using pRight */
49806 struct RowSetEntry *pLast; /* Last entry on the pEntry list */
49807 struct RowSetEntry *pFresh; /* Source of new entry objects */
49808 struct RowSetEntry *pForest; /* List of binary trees of entries */
49809 u16 nFresh; /* Number of objects on pFresh */
49810 u16 rsFlags; /* Various flags */
49811 int iBatch; /* Current insert batch */
49812};
49813
49814/*
49815** Allowed values for RowSet.rsFlags
49816*/
49817#define ROWSET_SORTED 0x01 /* True if RowSet.pEntry is sorted */
49818#define ROWSET_NEXT 0x02 /* True if sqlite3RowSetNext() has been called */
49819
49820/*
49821** Allocate a RowSet object. Return NULL if a memory allocation
49822** error occurs.
49823*/
49824SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3 *db){
49825 RowSet *p = sqlite3DbMallocRawNN(db, sizeof(*p));
49826 if( p ){
49827 int N = sqlite3DbMallocSize(db, p);
49828 p->pChunk = 0;
49829 p->db = db;
49830 p->pEntry = 0;
49831 p->pLast = 0;
49832 p->pForest = 0;
49833 p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
49834 p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
49835 p->rsFlags = ROWSET_SORTED;
49836 p->iBatch = 0;
49837 }
49838 return p;
49839}
49840
49841/*
49842** Deallocate all chunks from a RowSet. This frees all memory that
49843** the RowSet has allocated over its lifetime. This routine is
49844** the destructor for the RowSet.
49845*/
49846SQLITE_PRIVATE void sqlite3RowSetClear(void *pArg){
49847 RowSet *p = (RowSet*)pArg;
49848 struct RowSetChunk *pChunk, *pNextChunk;
49849 for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
49850 pNextChunk = pChunk->pNextChunk;
49851 sqlite3DbFree(p->db, pChunk);
49852 }
49853 p->pChunk = 0;
49854 p->nFresh = 0;
49855 p->pEntry = 0;
49856 p->pLast = 0;
49857 p->pForest = 0;
49858 p->rsFlags = ROWSET_SORTED;
49859}
49860
49861/*
49862** Deallocate all chunks from a RowSet. This frees all memory that
49863** the RowSet has allocated over its lifetime. This routine is
49864** the destructor for the RowSet.
49865*/
49866SQLITE_PRIVATE void sqlite3RowSetDelete(void *pArg){
49867 sqlite3RowSetClear(pArg);
49868 sqlite3DbFree(((RowSet*)pArg)->db, pArg);
49869}
49870
49871/*
49872** Allocate a new RowSetEntry object that is associated with the
49873** given RowSet. Return a pointer to the new and completely uninitialized
49874** objected.
49875**
49876** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
49877** routine returns NULL.
49878*/
49879static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
49880 assert( p!=0 );
49881 if( p->nFresh==0 ){ /*OPTIMIZATION-IF-FALSE*/
49882 /* We could allocate a fresh RowSetEntry each time one is needed, but it
49883 ** is more efficient to pull a preallocated entry from the pool */
49884 struct RowSetChunk *pNew;
49885 pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
49886 if( pNew==0 ){
49887 return 0;
49888 }
49889 pNew->pNextChunk = p->pChunk;
49890 p->pChunk = pNew;
49891 p->pFresh = pNew->aEntry;
49892 p->nFresh = ROWSET_ENTRY_PER_CHUNK;
49893 }
49894 p->nFresh--;
49895 return p->pFresh++;
49896}
49897
49898/*
49899** Insert a new value into a RowSet.
49900**
49901** The mallocFailed flag of the database connection is set if a
49902** memory allocation fails.
49903*/
49904SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet *p, i64 rowid){
49905 struct RowSetEntry *pEntry; /* The new entry */
49906 struct RowSetEntry *pLast; /* The last prior entry */
49907
49908 /* This routine is never called after sqlite3RowSetNext() */
49909 assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
49910
49911 pEntry = rowSetEntryAlloc(p);
49912 if( pEntry==0 ) return;
49913 pEntry->v = rowid;
49914 pEntry->pRight = 0;
49915 pLast = p->pLast;
49916 if( pLast ){
49917 if( rowid<=pLast->v ){ /*OPTIMIZATION-IF-FALSE*/
49918 /* Avoid unnecessary sorts by preserving the ROWSET_SORTED flags
49919 ** where possible */
49920 p->rsFlags &= ~ROWSET_SORTED;
49921 }
49922 pLast->pRight = pEntry;
49923 }else{
49924 p->pEntry = pEntry;
49925 }
49926 p->pLast = pEntry;
49927}
49928
49929/*
49930** Merge two lists of RowSetEntry objects. Remove duplicates.
49931**
49932** The input lists are connected via pRight pointers and are
49933** assumed to each already be in sorted order.
49934*/
49935static struct RowSetEntry *rowSetEntryMerge(
49936 struct RowSetEntry *pA, /* First sorted list to be merged */
49937 struct RowSetEntry *pB /* Second sorted list to be merged */
49938){
49939 struct RowSetEntry head;
49940 struct RowSetEntry *pTail;
49941
49942 pTail = &head;
49943 assert( pA!=0 && pB!=0 );
49944 for(;;){
49945 assert( pA->pRight==0 || pA->v<=pA->pRight->v );
49946 assert( pB->pRight==0 || pB->v<=pB->pRight->v );
49947 if( pA->v<=pB->v ){
49948 if( pA->v<pB->v ) pTail = pTail->pRight = pA;
49949 pA = pA->pRight;
49950 if( pA==0 ){
49951 pTail->pRight = pB;
49952 break;
49953 }
49954 }else{
49955 pTail = pTail->pRight = pB;
49956 pB = pB->pRight;
49957 if( pB==0 ){
49958 pTail->pRight = pA;
49959 break;
49960 }
49961 }
49962 }
49963 return head.pRight;
49964}
49965
49966/*
49967** Sort all elements on the list of RowSetEntry objects into order of
49968** increasing v.
49969*/
49970static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){
49971 unsigned int i;
49972 struct RowSetEntry *pNext, *aBucket[40];
49973
49974 memset(aBucket, 0, sizeof(aBucket));
49975 while( pIn ){
49976 pNext = pIn->pRight;
49977 pIn->pRight = 0;
49978 for(i=0; aBucket[i]; i++){
49979 pIn = rowSetEntryMerge(aBucket[i], pIn);
49980 aBucket[i] = 0;
49981 }
49982 aBucket[i] = pIn;
49983 pIn = pNext;
49984 }
49985 pIn = aBucket[0];
49986 for(i=1; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
49987 if( aBucket[i]==0 ) continue;
49988 pIn = pIn ? rowSetEntryMerge(pIn, aBucket[i]) : aBucket[i];
49989 }
49990 return pIn;
49991}
49992
49993
49994/*
49995** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
49996** Convert this tree into a linked list connected by the pRight pointers
49997** and return pointers to the first and last elements of the new list.
49998*/
49999static void rowSetTreeToList(
50000 struct RowSetEntry *pIn, /* Root of the input tree */
50001 struct RowSetEntry **ppFirst, /* Write head of the output list here */
50002 struct RowSetEntry **ppLast /* Write tail of the output list here */
50003){
50004 assert( pIn!=0 );
50005 if( pIn->pLeft ){
50006 struct RowSetEntry *p;
50007 rowSetTreeToList(pIn->pLeft, ppFirst, &p);
50008 p->pRight = pIn;
50009 }else{
50010 *ppFirst = pIn;
50011 }
50012 if( pIn->pRight ){
50013 rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
50014 }else{
50015 *ppLast = pIn;
50016 }
50017 assert( (*ppLast)->pRight==0 );
50018}
50019
50020
50021/*
50022** Convert a sorted list of elements (connected by pRight) into a binary
50023** tree with depth of iDepth. A depth of 1 means the tree contains a single
50024** node taken from the head of *ppList. A depth of 2 means a tree with
50025** three nodes. And so forth.
50026**
50027** Use as many entries from the input list as required and update the
50028** *ppList to point to the unused elements of the list. If the input
50029** list contains too few elements, then construct an incomplete tree
50030** and leave *ppList set to NULL.
50031**
50032** Return a pointer to the root of the constructed binary tree.
50033*/
50034static struct RowSetEntry *rowSetNDeepTree(
50035 struct RowSetEntry **ppList,
50036 int iDepth
50037){
50038 struct RowSetEntry *p; /* Root of the new tree */
50039 struct RowSetEntry *pLeft; /* Left subtree */
50040 if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
50041 /* Prevent unnecessary deep recursion when we run out of entries */
50042 return 0;
50043 }
50044 if( iDepth>1 ){ /*OPTIMIZATION-IF-TRUE*/
50045 /* This branch causes a *balanced* tree to be generated. A valid tree
50046 ** is still generated without this branch, but the tree is wildly
50047 ** unbalanced and inefficient. */
50048 pLeft = rowSetNDeepTree(ppList, iDepth-1);
50049 p = *ppList;
50050 if( p==0 ){ /*OPTIMIZATION-IF-FALSE*/
50051 /* It is safe to always return here, but the resulting tree
50052 ** would be unbalanced */
50053 return pLeft;
50054 }
50055 p->pLeft = pLeft;
50056 *ppList = p->pRight;
50057 p->pRight = rowSetNDeepTree(ppList, iDepth-1);
50058 }else{
50059 p = *ppList;
50060 *ppList = p->pRight;
50061 p->pLeft = p->pRight = 0;
50062 }
50063 return p;
50064}
50065
50066/*
50067** Convert a sorted list of elements into a binary tree. Make the tree
50068** as deep as it needs to be in order to contain the entire list.
50069*/
50070static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){
50071 int iDepth; /* Depth of the tree so far */
50072 struct RowSetEntry *p; /* Current tree root */
50073 struct RowSetEntry *pLeft; /* Left subtree */
50074
50075 assert( pList!=0 );
50076 p = pList;
50077 pList = p->pRight;
50078 p->pLeft = p->pRight = 0;
50079 for(iDepth=1; pList; iDepth++){
50080 pLeft = p;
50081 p = pList;
50082 pList = p->pRight;
50083 p->pLeft = pLeft;
50084 p->pRight = rowSetNDeepTree(&pList, iDepth);
50085 }
50086 return p;
50087}
50088
50089/*
50090** Extract the smallest element from the RowSet.
50091** Write the element into *pRowid. Return 1 on success. Return
50092** 0 if the RowSet is already empty.
50093**
50094** After this routine has been called, the sqlite3RowSetInsert()
50095** routine may not be called again.
50096**
50097** This routine may not be called after sqlite3RowSetTest() has
50098** been used. Older versions of RowSet allowed that, but as the
50099** capability was not used by the code generator, it was removed
50100** for code economy.
50101*/
50102SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
50103 assert( p!=0 );
50104 assert( p->pForest==0 ); /* Cannot be used with sqlite3RowSetText() */
50105
50106 /* Merge the forest into a single sorted list on first call */
50107 if( (p->rsFlags & ROWSET_NEXT)==0 ){ /*OPTIMIZATION-IF-FALSE*/
50108 if( (p->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
50109 p->pEntry = rowSetEntrySort(p->pEntry);
50110 }
50111 p->rsFlags |= ROWSET_SORTED|ROWSET_NEXT;
50112 }
50113
50114 /* Return the next entry on the list */
50115 if( p->pEntry ){
50116 *pRowid = p->pEntry->v;
50117 p->pEntry = p->pEntry->pRight;
50118 if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
50119 /* Free memory immediately, rather than waiting on sqlite3_finalize() */
50120 sqlite3RowSetClear(p);
50121 }
50122 return 1;
50123 }else{
50124 return 0;
50125 }
50126}
50127
50128/*
50129** Check to see if element iRowid was inserted into the rowset as
50130** part of any insert batch prior to iBatch. Return 1 or 0.
50131**
50132** If this is the first test of a new batch and if there exist entries
50133** on pRowSet->pEntry, then sort those entries into the forest at
50134** pRowSet->pForest so that they can be tested.
50135*/
50136SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64 iRowid){
50137 struct RowSetEntry *p, *pTree;
50138
50139 /* This routine is never called after sqlite3RowSetNext() */
50140 assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
50141
50142 /* Sort entries into the forest on the first test of a new batch.
50143 ** To save unnecessary work, only do this when the batch number changes.
50144 */
50145 if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/
50146 p = pRowSet->pEntry;
50147 if( p ){
50148 struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
50149 if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
50150 /* Only sort the current set of entiries if they need it */
50151 p = rowSetEntrySort(p);
50152 }
50153 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
50154 ppPrevTree = &pTree->pRight;
50155 if( pTree->pLeft==0 ){
50156 pTree->pLeft = rowSetListToTree(p);
50157 break;
50158 }else{
50159 struct RowSetEntry *pAux, *pTail;
50160 rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
50161 pTree->pLeft = 0;
50162 p = rowSetEntryMerge(pAux, p);
50163 }
50164 }
50165 if( pTree==0 ){
50166 *ppPrevTree = pTree = rowSetEntryAlloc(pRowSet);
50167 if( pTree ){
50168 pTree->v = 0;
50169 pTree->pRight = 0;
50170 pTree->pLeft = rowSetListToTree(p);
50171 }
50172 }
50173 pRowSet->pEntry = 0;
50174 pRowSet->pLast = 0;
50175 pRowSet->rsFlags |= ROWSET_SORTED;
50176 }
50177 pRowSet->iBatch = iBatch;
50178 }
50179
50180 /* Test to see if the iRowid value appears anywhere in the forest.
50181 ** Return 1 if it does and 0 if not.
50182 */
50183 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
50184 p = pTree->pLeft;
50185 while( p ){
50186 if( p->v<iRowid ){
50187 p = p->pRight;
50188 }else if( p->v>iRowid ){
50189 p = p->pLeft;
50190 }else{
50191 return 1;
50192 }
50193 }
50194 }
50195 return 0;
50196}
50197
50198/************** End of rowset.c **********************************************/
50199/************** Begin file pager.c *******************************************/
50200/*
50201** 2001 September 15
50202**
50203** The author disclaims copyright to this source code. In place of
50204** a legal notice, here is a blessing:
50205**
50206** May you do good and not evil.
50207** May you find forgiveness for yourself and forgive others.
50208** May you share freely, never taking more than you give.
50209**
50210*************************************************************************
50211** This is the implementation of the page cache subsystem or "pager".
50212**
50213** The pager is used to access a database disk file. It implements
50214** atomic commit and rollback through the use of a journal file that
50215** is separate from the database file. The pager also implements file
50216** locking to prevent two processes from writing the same database
50217** file simultaneously, or one process from reading the database while
50218** another is writing.
50219*/
50220#ifndef SQLITE_OMIT_DISKIO
50221/* #include "sqliteInt.h" */
50222/************** Include wal.h in the middle of pager.c ***********************/
50223/************** Begin file wal.h *********************************************/
50224/*
50225** 2010 February 1
50226**
50227** The author disclaims copyright to this source code. In place of
50228** a legal notice, here is a blessing:
50229**
50230** May you do good and not evil.
50231** May you find forgiveness for yourself and forgive others.
50232** May you share freely, never taking more than you give.
50233**
50234*************************************************************************
50235** This header file defines the interface to the write-ahead logging
50236** system. Refer to the comments below and the header comment attached to
50237** the implementation of each function in log.c for further details.
50238*/
50239
50240#ifndef SQLITE_WAL_H
50241#define SQLITE_WAL_H
50242
50243/* #include "sqliteInt.h" */
50244
50245/* Macros for extracting appropriate sync flags for either transaction
50246** commits (WAL_SYNC_FLAGS(X)) or for checkpoint ops (CKPT_SYNC_FLAGS(X)):
50247*/
50248#define WAL_SYNC_FLAGS(X) ((X)&0x03)
50249#define CKPT_SYNC_FLAGS(X) (((X)>>2)&0x03)
50250
50251#ifdef SQLITE_OMIT_WAL
50252# define sqlite3WalOpen(x,y,z) 0
50253# define sqlite3WalLimit(x,y)
50254# define sqlite3WalClose(v,w,x,y,z) 0
50255# define sqlite3WalBeginReadTransaction(y,z) 0
50256# define sqlite3WalEndReadTransaction(z)
50257# define sqlite3WalDbsize(y) 0
50258# define sqlite3WalBeginWriteTransaction(y) 0
50259# define sqlite3WalEndWriteTransaction(x) 0
50260# define sqlite3WalUndo(x,y,z) 0
50261# define sqlite3WalSavepoint(y,z)
50262# define sqlite3WalSavepointUndo(y,z) 0
50263# define sqlite3WalFrames(u,v,w,x,y,z) 0
50264# define sqlite3WalCheckpoint(q,r,s,t,u,v,w,x,y,z) 0
50265# define sqlite3WalCallback(z) 0
50266# define sqlite3WalExclusiveMode(y,z) 0
50267# define sqlite3WalHeapMemory(z) 0
50268# define sqlite3WalFramesize(z) 0
50269# define sqlite3WalFindFrame(x,y,z) 0
50270# define sqlite3WalFile(x) 0
50271#else
50272
50273#define WAL_SAVEPOINT_NDATA 4
50274
50275/* Connection to a write-ahead log (WAL) file.
50276** There is one object of this type for each pager.
50277*/
50278typedef struct Wal Wal;
50279
50280/* Open and close a connection to a write-ahead log. */
50281SQLITE_PRIVATE int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**);
50282SQLITE_PRIVATE int sqlite3WalClose(Wal *pWal, sqlite3*, int sync_flags, int, u8 *);
50283
50284/* Set the limiting size of a WAL file. */
50285SQLITE_PRIVATE void sqlite3WalLimit(Wal*, i64);
50286
50287/* Used by readers to open (lock) and close (unlock) a snapshot. A
50288** snapshot is like a read-transaction. It is the state of the database
50289** at an instant in time. sqlite3WalOpenSnapshot gets a read lock and
50290** preserves the current state even if the other threads or processes
50291** write to or checkpoint the WAL. sqlite3WalCloseSnapshot() closes the
50292** transaction and releases the lock.
50293*/
50294SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
50295SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal);
50296
50297/* Read a page from the write-ahead log, if it is present. */
50298SQLITE_PRIVATE int sqlite3WalFindFrame(Wal *, Pgno, u32 *);
50299SQLITE_PRIVATE int sqlite3WalReadFrame(Wal *, u32, int, u8 *);
50300
50301/* If the WAL is not empty, return the size of the database. */
50302SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal);
50303
50304/* Obtain or release the WRITER lock. */
50305SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal);
50306SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal);
50307
50308/* Undo any frames written (but not committed) to the log */
50309SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx);
50310
50311/* Return an integer that records the current (uncommitted) write
50312** position in the WAL */
50313SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData);
50314
50315/* Move the write position of the WAL back to iFrame. Called in
50316** response to a ROLLBACK TO command. */
50317SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData);
50318
50319/* Write a frame or frames to the log. */
50320SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
50321
50322/* Copy pages from the log to the database file */
50323SQLITE_PRIVATE int sqlite3WalCheckpoint(
50324 Wal *pWal, /* Write-ahead log connection */
50325 sqlite3 *db, /* Check this handle's interrupt flag */
50326 int eMode, /* One of PASSIVE, FULL and RESTART */
50327 int (*xBusy)(void*), /* Function to call when busy */
50328 void *pBusyArg, /* Context argument for xBusyHandler */
50329 int sync_flags, /* Flags to sync db file with (or 0) */
50330 int nBuf, /* Size of buffer nBuf */
50331 u8 *zBuf, /* Temporary buffer to use */
50332 int *pnLog, /* OUT: Number of frames in WAL */
50333 int *pnCkpt /* OUT: Number of backfilled frames in WAL */
50334);
50335
50336/* Return the value to pass to a sqlite3_wal_hook callback, the
50337** number of frames in the WAL at the point of the last commit since
50338** sqlite3WalCallback() was called. If no commits have occurred since
50339** the last call, then return 0.
50340*/
50341SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal);
50342
50343/* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released)
50344** by the pager layer on the database file.
50345*/
50346SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op);
50347
50348/* Return true if the argument is non-NULL and the WAL module is using
50349** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
50350** WAL module is using shared-memory, return false.
50351*/
50352SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal);
50353
50354#ifdef SQLITE_ENABLE_SNAPSHOT
50355SQLITE_PRIVATE int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot);
50356SQLITE_PRIVATE void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot);
50357SQLITE_PRIVATE int sqlite3WalSnapshotRecover(Wal *pWal);
50358SQLITE_PRIVATE int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot);
50359SQLITE_PRIVATE void sqlite3WalSnapshotUnlock(Wal *pWal);
50360#endif
50361
50362#ifdef SQLITE_ENABLE_ZIPVFS
50363/* If the WAL file is not empty, return the number of bytes of content
50364** stored in each frame (i.e. the db page-size when the WAL was created).
50365*/
50366SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal);
50367#endif
50368
50369/* Return the sqlite3_file object for the WAL file */
50370SQLITE_PRIVATE sqlite3_file *sqlite3WalFile(Wal *pWal);
50371
50372#endif /* ifndef SQLITE_OMIT_WAL */
50373#endif /* SQLITE_WAL_H */
50374
50375/************** End of wal.h *************************************************/
50376/************** Continuing where we left off in pager.c **********************/
50377
50378
50379/******************* NOTES ON THE DESIGN OF THE PAGER ************************
50380**
50381** This comment block describes invariants that hold when using a rollback
50382** journal. These invariants do not apply for journal_mode=WAL,
50383** journal_mode=MEMORY, or journal_mode=OFF.
50384**
50385** Within this comment block, a page is deemed to have been synced
50386** automatically as soon as it is written when PRAGMA synchronous=OFF.
50387** Otherwise, the page is not synced until the xSync method of the VFS
50388** is called successfully on the file containing the page.
50389**
50390** Definition: A page of the database file is said to be "overwriteable" if
50391** one or more of the following are true about the page:
50392**
50393** (a) The original content of the page as it was at the beginning of
50394** the transaction has been written into the rollback journal and
50395** synced.
50396**
50397** (b) The page was a freelist leaf page at the start of the transaction.
50398**
50399** (c) The page number is greater than the largest page that existed in
50400** the database file at the start of the transaction.
50401**
50402** (1) A page of the database file is never overwritten unless one of the
50403** following are true:
50404**
50405** (a) The page and all other pages on the same sector are overwriteable.
50406**
50407** (b) The atomic page write optimization is enabled, and the entire
50408** transaction other than the update of the transaction sequence
50409** number consists of a single page change.
50410**
50411** (2) The content of a page written into the rollback journal exactly matches
50412** both the content in the database when the rollback journal was written
50413** and the content in the database at the beginning of the current
50414** transaction.
50415**
50416** (3) Writes to the database file are an integer multiple of the page size
50417** in length and are aligned on a page boundary.
50418**
50419** (4) Reads from the database file are either aligned on a page boundary and
50420** an integer multiple of the page size in length or are taken from the
50421** first 100 bytes of the database file.
50422**
50423** (5) All writes to the database file are synced prior to the rollback journal
50424** being deleted, truncated, or zeroed.
50425**
50426** (6) If a master journal file is used, then all writes to the database file
50427** are synced prior to the master journal being deleted.
50428**
50429** Definition: Two databases (or the same database at two points it time)
50430** are said to be "logically equivalent" if they give the same answer to
50431** all queries. Note in particular the content of freelist leaf
50432** pages can be changed arbitrarily without affecting the logical equivalence
50433** of the database.
50434**
50435** (7) At any time, if any subset, including the empty set and the total set,
50436** of the unsynced changes to a rollback journal are removed and the
50437** journal is rolled back, the resulting database file will be logically
50438** equivalent to the database file at the beginning of the transaction.
50439**
50440** (8) When a transaction is rolled back, the xTruncate method of the VFS
50441** is called to restore the database file to the same size it was at
50442** the beginning of the transaction. (In some VFSes, the xTruncate
50443** method is a no-op, but that does not change the fact the SQLite will
50444** invoke it.)
50445**
50446** (9) Whenever the database file is modified, at least one bit in the range
50447** of bytes from 24 through 39 inclusive will be changed prior to releasing
50448** the EXCLUSIVE lock, thus signaling other connections on the same
50449** database to flush their caches.
50450**
50451** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
50452** than one billion transactions.
50453**
50454** (11) A database file is well-formed at the beginning and at the conclusion
50455** of every transaction.
50456**
50457** (12) An EXCLUSIVE lock is held on the database file when writing to
50458** the database file.
50459**
50460** (13) A SHARED lock is held on the database file while reading any
50461** content out of the database file.
50462**
50463******************************************************************************/
50464
50465/*
50466** Macros for troubleshooting. Normally turned off
50467*/
50468#if 0
50469int sqlite3PagerTrace=1; /* True to enable tracing */
50470#define sqlite3DebugPrintf printf
50471#define PAGERTRACE(X) if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; }
50472#else
50473#define PAGERTRACE(X)
50474#endif
50475
50476/*
50477** The following two macros are used within the PAGERTRACE() macros above
50478** to print out file-descriptors.
50479**
50480** PAGERID() takes a pointer to a Pager struct as its argument. The
50481** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
50482** struct as its argument.
50483*/
50484#define PAGERID(p) (SQLITE_PTR_TO_INT(p->fd))
50485#define FILEHANDLEID(fd) (SQLITE_PTR_TO_INT(fd))
50486
50487/*
50488** The Pager.eState variable stores the current 'state' of a pager. A
50489** pager may be in any one of the seven states shown in the following
50490** state diagram.
50491**
50492** OPEN <------+------+
50493** | | |
50494** V | |
50495** +---------> READER-------+ |
50496** | | |
50497** | V |
50498** |<-------WRITER_LOCKED------> ERROR
50499** | | ^
50500** | V |
50501** |<------WRITER_CACHEMOD-------->|
50502** | | |
50503** | V |
50504** |<-------WRITER_DBMOD---------->|
50505** | | |
50506** | V |
50507** +<------WRITER_FINISHED-------->+
50508**
50509**
50510** List of state transitions and the C [function] that performs each:
50511**
50512** OPEN -> READER [sqlite3PagerSharedLock]
50513** READER -> OPEN [pager_unlock]
50514**
50515** READER -> WRITER_LOCKED [sqlite3PagerBegin]
50516** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal]
50517** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal]
50518** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne]
50519** WRITER_*** -> READER [pager_end_transaction]
50520**
50521** WRITER_*** -> ERROR [pager_error]
50522** ERROR -> OPEN [pager_unlock]
50523**
50524**
50525** OPEN:
50526**
50527** The pager starts up in this state. Nothing is guaranteed in this
50528** state - the file may or may not be locked and the database size is
50529** unknown. The database may not be read or written.
50530**
50531** * No read or write transaction is active.
50532** * Any lock, or no lock at all, may be held on the database file.
50533** * The dbSize, dbOrigSize and dbFileSize variables may not be trusted.
50534**
50535** READER:
50536**
50537** In this state all the requirements for reading the database in
50538** rollback (non-WAL) mode are met. Unless the pager is (or recently
50539** was) in exclusive-locking mode, a user-level read transaction is
50540** open. The database size is known in this state.
50541**
50542** A connection running with locking_mode=normal enters this state when
50543** it opens a read-transaction on the database and returns to state
50544** OPEN after the read-transaction is completed. However a connection
50545** running in locking_mode=exclusive (including temp databases) remains in
50546** this state even after the read-transaction is closed. The only way
50547** a locking_mode=exclusive connection can transition from READER to OPEN
50548** is via the ERROR state (see below).
50549**
50550** * A read transaction may be active (but a write-transaction cannot).
50551** * A SHARED or greater lock is held on the database file.
50552** * The dbSize variable may be trusted (even if a user-level read
50553** transaction is not active). The dbOrigSize and dbFileSize variables
50554** may not be trusted at this point.
50555** * If the database is a WAL database, then the WAL connection is open.
50556** * Even if a read-transaction is not open, it is guaranteed that
50557** there is no hot-journal in the file-system.
50558**
50559** WRITER_LOCKED:
50560**
50561** The pager moves to this state from READER when a write-transaction
50562** is first opened on the database. In WRITER_LOCKED state, all locks
50563** required to start a write-transaction are held, but no actual
50564** modifications to the cache or database have taken place.
50565**
50566** In rollback mode, a RESERVED or (if the transaction was opened with
50567** BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when
50568** moving to this state, but the journal file is not written to or opened
50569** to in this state. If the transaction is committed or rolled back while
50570** in WRITER_LOCKED state, all that is required is to unlock the database
50571** file.
50572**
50573** IN WAL mode, WalBeginWriteTransaction() is called to lock the log file.
50574** If the connection is running with locking_mode=exclusive, an attempt
50575** is made to obtain an EXCLUSIVE lock on the database file.
50576**
50577** * A write transaction is active.
50578** * If the connection is open in rollback-mode, a RESERVED or greater
50579** lock is held on the database file.
50580** * If the connection is open in WAL-mode, a WAL write transaction
50581** is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully
50582** called).
50583** * The dbSize, dbOrigSize and dbFileSize variables are all valid.
50584** * The contents of the pager cache have not been modified.
50585** * The journal file may or may not be open.
50586** * Nothing (not even the first header) has been written to the journal.
50587**
50588** WRITER_CACHEMOD:
50589**
50590** A pager moves from WRITER_LOCKED state to this state when a page is
50591** first modified by the upper layer. In rollback mode the journal file
50592** is opened (if it is not already open) and a header written to the
50593** start of it. The database file on disk has not been modified.
50594**
50595** * A write transaction is active.
50596** * A RESERVED or greater lock is held on the database file.
50597** * The journal file is open and the first header has been written
50598** to it, but the header has not been synced to disk.
50599** * The contents of the page cache have been modified.
50600**
50601** WRITER_DBMOD:
50602**
50603** The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state
50604** when it modifies the contents of the database file. WAL connections
50605** never enter this state (since they do not modify the database file,
50606** just the log file).
50607**
50608** * A write transaction is active.
50609** * An EXCLUSIVE or greater lock is held on the database file.
50610** * The journal file is open and the first header has been written
50611** and synced to disk.
50612** * The contents of the page cache have been modified (and possibly
50613** written to disk).
50614**
50615** WRITER_FINISHED:
50616**
50617** It is not possible for a WAL connection to enter this state.
50618**
50619** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
50620** state after the entire transaction has been successfully written into the
50621** database file. In this state the transaction may be committed simply
50622** by finalizing the journal file. Once in WRITER_FINISHED state, it is
50623** not possible to modify the database further. At this point, the upper
50624** layer must either commit or rollback the transaction.
50625**
50626** * A write transaction is active.
50627** * An EXCLUSIVE or greater lock is held on the database file.
50628** * All writing and syncing of journal and database data has finished.
50629** If no error occurred, all that remains is to finalize the journal to
50630** commit the transaction. If an error did occur, the caller will need
50631** to rollback the transaction.
50632**
50633** ERROR:
50634**
50635** The ERROR state is entered when an IO or disk-full error (including
50636** SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it
50637** difficult to be sure that the in-memory pager state (cache contents,
50638** db size etc.) are consistent with the contents of the file-system.
50639**
50640** Temporary pager files may enter the ERROR state, but in-memory pagers
50641** cannot.
50642**
50643** For example, if an IO error occurs while performing a rollback,
50644** the contents of the page-cache may be left in an inconsistent state.
50645** At this point it would be dangerous to change back to READER state
50646** (as usually happens after a rollback). Any subsequent readers might
50647** report database corruption (due to the inconsistent cache), and if
50648** they upgrade to writers, they may inadvertently corrupt the database
50649** file. To avoid this hazard, the pager switches into the ERROR state
50650** instead of READER following such an error.
50651**
50652** Once it has entered the ERROR state, any attempt to use the pager
50653** to read or write data returns an error. Eventually, once all
50654** outstanding transactions have been abandoned, the pager is able to
50655** transition back to OPEN state, discarding the contents of the
50656** page-cache and any other in-memory state at the same time. Everything
50657** is reloaded from disk (and, if necessary, hot-journal rollback peformed)
50658** when a read-transaction is next opened on the pager (transitioning
50659** the pager into READER state). At that point the system has recovered
50660** from the error.
50661**
50662** Specifically, the pager jumps into the ERROR state if:
50663**
50664** 1. An error occurs while attempting a rollback. This happens in
50665** function sqlite3PagerRollback().
50666**
50667** 2. An error occurs while attempting to finalize a journal file
50668** following a commit in function sqlite3PagerCommitPhaseTwo().
50669**
50670** 3. An error occurs while attempting to write to the journal or
50671** database file in function pagerStress() in order to free up
50672** memory.
50673**
50674** In other cases, the error is returned to the b-tree layer. The b-tree
50675** layer then attempts a rollback operation. If the error condition
50676** persists, the pager enters the ERROR state via condition (1) above.
50677**
50678** Condition (3) is necessary because it can be triggered by a read-only
50679** statement executed within a transaction. In this case, if the error
50680** code were simply returned to the user, the b-tree layer would not
50681** automatically attempt a rollback, as it assumes that an error in a
50682** read-only statement cannot leave the pager in an internally inconsistent
50683** state.
50684**
50685** * The Pager.errCode variable is set to something other than SQLITE_OK.
50686** * There are one or more outstanding references to pages (after the
50687** last reference is dropped the pager should move back to OPEN state).
50688** * The pager is not an in-memory pager.
50689**
50690**
50691** Notes:
50692**
50693** * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the
50694** connection is open in WAL mode. A WAL connection is always in one
50695** of the first four states.
50696**
50697** * Normally, a connection open in exclusive mode is never in PAGER_OPEN
50698** state. There are two exceptions: immediately after exclusive-mode has
50699** been turned on (and before any read or write transactions are
50700** executed), and when the pager is leaving the "error state".
50701**
50702** * See also: assert_pager_state().
50703*/
50704#define PAGER_OPEN 0
50705#define PAGER_READER 1
50706#define PAGER_WRITER_LOCKED 2
50707#define PAGER_WRITER_CACHEMOD 3
50708#define PAGER_WRITER_DBMOD 4
50709#define PAGER_WRITER_FINISHED 5
50710#define PAGER_ERROR 6
50711
50712/*
50713** The Pager.eLock variable is almost always set to one of the
50714** following locking-states, according to the lock currently held on
50715** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
50716** This variable is kept up to date as locks are taken and released by
50717** the pagerLockDb() and pagerUnlockDb() wrappers.
50718**
50719** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY
50720** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not
50721** the operation was successful. In these circumstances pagerLockDb() and
50722** pagerUnlockDb() take a conservative approach - eLock is always updated
50723** when unlocking the file, and only updated when locking the file if the
50724** VFS call is successful. This way, the Pager.eLock variable may be set
50725** to a less exclusive (lower) value than the lock that is actually held
50726** at the system level, but it is never set to a more exclusive value.
50727**
50728** This is usually safe. If an xUnlock fails or appears to fail, there may
50729** be a few redundant xLock() calls or a lock may be held for longer than
50730** required, but nothing really goes wrong.
50731**
50732** The exception is when the database file is unlocked as the pager moves
50733** from ERROR to OPEN state. At this point there may be a hot-journal file
50734** in the file-system that needs to be rolled back (as part of an OPEN->SHARED
50735** transition, by the same pager or any other). If the call to xUnlock()
50736** fails at this point and the pager is left holding an EXCLUSIVE lock, this
50737** can confuse the call to xCheckReservedLock() call made later as part
50738** of hot-journal detection.
50739**
50740** xCheckReservedLock() is defined as returning true "if there is a RESERVED
50741** lock held by this process or any others". So xCheckReservedLock may
50742** return true because the caller itself is holding an EXCLUSIVE lock (but
50743** doesn't know it because of a previous error in xUnlock). If this happens
50744** a hot-journal may be mistaken for a journal being created by an active
50745** transaction in another process, causing SQLite to read from the database
50746** without rolling it back.
50747**
50748** To work around this, if a call to xUnlock() fails when unlocking the
50749** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It
50750** is only changed back to a real locking state after a successful call
50751** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
50752** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
50753** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
50754** lock on the database file before attempting to roll it back. See function
50755** PagerSharedLock() for more detail.
50756**
50757** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in
50758** PAGER_OPEN state.
50759*/
50760#define UNKNOWN_LOCK (EXCLUSIVE_LOCK+1)
50761
50762/*
50763** A macro used for invoking the codec if there is one
50764*/
50765#ifdef SQLITE_HAS_CODEC
50766# define CODEC1(P,D,N,X,E) \
50767 if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; }
50768# define CODEC2(P,D,N,X,E,O) \
50769 if( P->xCodec==0 ){ O=(char*)D; }else \
50770 if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; }
50771#else
50772# define CODEC1(P,D,N,X,E) /* NO-OP */
50773# define CODEC2(P,D,N,X,E,O) O=(char*)D
50774#endif
50775
50776/*
50777** The maximum allowed sector size. 64KiB. If the xSectorsize() method
50778** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
50779** This could conceivably cause corruption following a power failure on
50780** such a system. This is currently an undocumented limit.
50781*/
50782#define MAX_SECTOR_SIZE 0x10000
50783
50784
50785/*
50786** An instance of the following structure is allocated for each active
50787** savepoint and statement transaction in the system. All such structures
50788** are stored in the Pager.aSavepoint[] array, which is allocated and
50789** resized using sqlite3Realloc().
50790**
50791** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
50792** set to 0. If a journal-header is written into the main journal while
50793** the savepoint is active, then iHdrOffset is set to the byte offset
50794** immediately following the last journal record written into the main
50795** journal before the journal-header. This is required during savepoint
50796** rollback (see pagerPlaybackSavepoint()).
50797*/
50798typedef struct PagerSavepoint PagerSavepoint;
50799struct PagerSavepoint {
50800 i64 iOffset; /* Starting offset in main journal */
50801 i64 iHdrOffset; /* See above */
50802 Bitvec *pInSavepoint; /* Set of pages in this savepoint */
50803 Pgno nOrig; /* Original number of pages in file */
50804 Pgno iSubRec; /* Index of first record in sub-journal */
50805#ifndef SQLITE_OMIT_WAL
50806 u32 aWalData[WAL_SAVEPOINT_NDATA]; /* WAL savepoint context */
50807#endif
50808};
50809
50810/*
50811** Bits of the Pager.doNotSpill flag. See further description below.
50812*/
50813#define SPILLFLAG_OFF 0x01 /* Never spill cache. Set via pragma */
50814#define SPILLFLAG_ROLLBACK 0x02 /* Current rolling back, so do not spill */
50815#define SPILLFLAG_NOSYNC 0x04 /* Spill is ok, but do not sync */
50816
50817/*
50818** An open page cache is an instance of struct Pager. A description of
50819** some of the more important member variables follows:
50820**
50821** eState
50822**
50823** The current 'state' of the pager object. See the comment and state
50824** diagram above for a description of the pager state.
50825**
50826** eLock
50827**
50828** For a real on-disk database, the current lock held on the database file -
50829** NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
50830**
50831** For a temporary or in-memory database (neither of which require any
50832** locks), this variable is always set to EXCLUSIVE_LOCK. Since such
50833** databases always have Pager.exclusiveMode==1, this tricks the pager
50834** logic into thinking that it already has all the locks it will ever
50835** need (and no reason to release them).
50836**
50837** In some (obscure) circumstances, this variable may also be set to
50838** UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for
50839** details.
50840**
50841** changeCountDone
50842**
50843** This boolean variable is used to make sure that the change-counter
50844** (the 4-byte header field at byte offset 24 of the database file) is
50845** not updated more often than necessary.
50846**
50847** It is set to true when the change-counter field is updated, which
50848** can only happen if an exclusive lock is held on the database file.
50849** It is cleared (set to false) whenever an exclusive lock is
50850** relinquished on the database file. Each time a transaction is committed,
50851** The changeCountDone flag is inspected. If it is true, the work of
50852** updating the change-counter is omitted for the current transaction.
50853**
50854** This mechanism means that when running in exclusive mode, a connection
50855** need only update the change-counter once, for the first transaction
50856** committed.
50857**
50858** setMaster
50859**
50860** When PagerCommitPhaseOne() is called to commit a transaction, it may
50861** (or may not) specify a master-journal name to be written into the
50862** journal file before it is synced to disk.
50863**
50864** Whether or not a journal file contains a master-journal pointer affects
50865** the way in which the journal file is finalized after the transaction is
50866** committed or rolled back when running in "journal_mode=PERSIST" mode.
50867** If a journal file does not contain a master-journal pointer, it is
50868** finalized by overwriting the first journal header with zeroes. If
50869** it does contain a master-journal pointer the journal file is finalized
50870** by truncating it to zero bytes, just as if the connection were
50871** running in "journal_mode=truncate" mode.
50872**
50873** Journal files that contain master journal pointers cannot be finalized
50874** simply by overwriting the first journal-header with zeroes, as the
50875** master journal pointer could interfere with hot-journal rollback of any
50876** subsequently interrupted transaction that reuses the journal file.
50877**
50878** The flag is cleared as soon as the journal file is finalized (either
50879** by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
50880** journal file from being successfully finalized, the setMaster flag
50881** is cleared anyway (and the pager will move to ERROR state).
50882**
50883** doNotSpill
50884**
50885** This variables control the behavior of cache-spills (calls made by
50886** the pcache module to the pagerStress() routine to write cached data
50887** to the file-system in order to free up memory).
50888**
50889** When bits SPILLFLAG_OFF or SPILLFLAG_ROLLBACK of doNotSpill are set,
50890** writing to the database from pagerStress() is disabled altogether.
50891** The SPILLFLAG_ROLLBACK case is done in a very obscure case that
50892** comes up during savepoint rollback that requires the pcache module
50893** to allocate a new page to prevent the journal file from being written
50894** while it is being traversed by code in pager_playback(). The SPILLFLAG_OFF
50895** case is a user preference.
50896**
50897** If the SPILLFLAG_NOSYNC bit is set, writing to the database from
50898** pagerStress() is permitted, but syncing the journal file is not.
50899** This flag is set by sqlite3PagerWrite() when the file-system sector-size
50900** is larger than the database page-size in order to prevent a journal sync
50901** from happening in between the journalling of two pages on the same sector.
50902**
50903** subjInMemory
50904**
50905** This is a boolean variable. If true, then any required sub-journal
50906** is opened as an in-memory journal file. If false, then in-memory
50907** sub-journals are only used for in-memory pager files.
50908**
50909** This variable is updated by the upper layer each time a new
50910** write-transaction is opened.
50911**
50912** dbSize, dbOrigSize, dbFileSize
50913**
50914** Variable dbSize is set to the number of pages in the database file.
50915** It is valid in PAGER_READER and higher states (all states except for
50916** OPEN and ERROR).
50917**
50918** dbSize is set based on the size of the database file, which may be
50919** larger than the size of the database (the value stored at offset
50920** 28 of the database header by the btree). If the size of the file
50921** is not an integer multiple of the page-size, the value stored in
50922** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
50923** Except, any file that is greater than 0 bytes in size is considered
50924** to have at least one page. (i.e. a 1KB file with 2K page-size leads
50925** to dbSize==1).
50926**
50927** During a write-transaction, if pages with page-numbers greater than
50928** dbSize are modified in the cache, dbSize is updated accordingly.
50929** Similarly, if the database is truncated using PagerTruncateImage(),
50930** dbSize is updated.
50931**
50932** Variables dbOrigSize and dbFileSize are valid in states
50933** PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize
50934** variable at the start of the transaction. It is used during rollback,
50935** and to determine whether or not pages need to be journalled before
50936** being modified.
50937**
50938** Throughout a write-transaction, dbFileSize contains the size of
50939** the file on disk in pages. It is set to a copy of dbSize when the
50940** write-transaction is first opened, and updated when VFS calls are made
50941** to write or truncate the database file on disk.
50942**
50943** The only reason the dbFileSize variable is required is to suppress
50944** unnecessary calls to xTruncate() after committing a transaction. If,
50945** when a transaction is committed, the dbFileSize variable indicates
50946** that the database file is larger than the database image (Pager.dbSize),
50947** pager_truncate() is called. The pager_truncate() call uses xFilesize()
50948** to measure the database file on disk, and then truncates it if required.
50949** dbFileSize is not used when rolling back a transaction. In this case
50950** pager_truncate() is called unconditionally (which means there may be
50951** a call to xFilesize() that is not strictly required). In either case,
50952** pager_truncate() may cause the file to become smaller or larger.
50953**
50954** dbHintSize
50955**
50956** The dbHintSize variable is used to limit the number of calls made to
50957** the VFS xFileControl(FCNTL_SIZE_HINT) method.
50958**
50959** dbHintSize is set to a copy of the dbSize variable when a
50960** write-transaction is opened (at the same time as dbFileSize and
50961** dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called,
50962** dbHintSize is increased to the number of pages that correspond to the
50963** size-hint passed to the method call. See pager_write_pagelist() for
50964** details.
50965**
50966** errCode
50967**
50968** The Pager.errCode variable is only ever used in PAGER_ERROR state. It
50969** is set to zero in all other states. In PAGER_ERROR state, Pager.errCode
50970** is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX
50971** sub-codes.
50972**
50973** syncFlags, walSyncFlags
50974**
50975** syncFlags is either SQLITE_SYNC_NORMAL (0x02) or SQLITE_SYNC_FULL (0x03).
50976** syncFlags is used for rollback mode. walSyncFlags is used for WAL mode
50977** and contains the flags used to sync the checkpoint operations in the
50978** lower two bits, and sync flags used for transaction commits in the WAL
50979** file in bits 0x04 and 0x08. In other words, to get the correct sync flags
50980** for checkpoint operations, use (walSyncFlags&0x03) and to get the correct
50981** sync flags for transaction commit, use ((walSyncFlags>>2)&0x03). Note
50982** that with synchronous=NORMAL in WAL mode, transaction commit is not synced
50983** meaning that the 0x04 and 0x08 bits are both zero.
50984*/
50985struct Pager {
50986 sqlite3_vfs *pVfs; /* OS functions to use for IO */
50987 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
50988 u8 journalMode; /* One of the PAGER_JOURNALMODE_* values */
50989 u8 useJournal; /* Use a rollback journal on this file */
50990 u8 noSync; /* Do not sync the journal if true */
50991 u8 fullSync; /* Do extra syncs of the journal for robustness */
50992 u8 extraSync; /* sync directory after journal delete */
50993 u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */
50994 u8 walSyncFlags; /* See description above */
50995 u8 tempFile; /* zFilename is a temporary or immutable file */
50996 u8 noLock; /* Do not lock (except in WAL mode) */
50997 u8 readOnly; /* True for a read-only database */
50998 u8 memDb; /* True to inhibit all file I/O */
50999
51000 /**************************************************************************
51001 ** The following block contains those class members that change during
51002 ** routine operation. Class members not in this block are either fixed
51003 ** when the pager is first created or else only change when there is a
51004 ** significant mode change (such as changing the page_size, locking_mode,
51005 ** or the journal_mode). From another view, these class members describe
51006 ** the "state" of the pager, while other class members describe the
51007 ** "configuration" of the pager.
51008 */
51009 u8 eState; /* Pager state (OPEN, READER, WRITER_LOCKED..) */
51010 u8 eLock; /* Current lock held on database file */
51011 u8 changeCountDone; /* Set after incrementing the change-counter */
51012 u8 setMaster; /* True if a m-j name has been written to jrnl */
51013 u8 doNotSpill; /* Do not spill the cache when non-zero */
51014 u8 subjInMemory; /* True to use in-memory sub-journals */
51015 u8 bUseFetch; /* True to use xFetch() */
51016 u8 hasHeldSharedLock; /* True if a shared lock has ever been held */
51017 Pgno dbSize; /* Number of pages in the database */
51018 Pgno dbOrigSize; /* dbSize before the current transaction */
51019 Pgno dbFileSize; /* Number of pages in the database file */
51020 Pgno dbHintSize; /* Value passed to FCNTL_SIZE_HINT call */
51021 int errCode; /* One of several kinds of errors */
51022 int nRec; /* Pages journalled since last j-header written */
51023 u32 cksumInit; /* Quasi-random value added to every checksum */
51024 u32 nSubRec; /* Number of records written to sub-journal */
51025 Bitvec *pInJournal; /* One bit for each page in the database file */
51026 sqlite3_file *fd; /* File descriptor for database */
51027 sqlite3_file *jfd; /* File descriptor for main journal */
51028 sqlite3_file *sjfd; /* File descriptor for sub-journal */
51029 i64 journalOff; /* Current write offset in the journal file */
51030 i64 journalHdr; /* Byte offset to previous journal header */
51031 sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
51032 PagerSavepoint *aSavepoint; /* Array of active savepoints */
51033 int nSavepoint; /* Number of elements in aSavepoint[] */
51034 u32 iDataVersion; /* Changes whenever database content changes */
51035 char dbFileVers[16]; /* Changes whenever database file changes */
51036
51037 int nMmapOut; /* Number of mmap pages currently outstanding */
51038 sqlite3_int64 szMmap; /* Desired maximum mmap size */
51039 PgHdr *pMmapFreelist; /* List of free mmap page headers (pDirty) */
51040 /*
51041 ** End of the routinely-changing class members
51042 ***************************************************************************/
51043
51044 u16 nExtra; /* Add this many bytes to each in-memory page */
51045 i16 nReserve; /* Number of unused bytes at end of each page */
51046 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
51047 u32 sectorSize; /* Assumed sector size during rollback */
51048 int pageSize; /* Number of bytes in a page */
51049 Pgno mxPgno; /* Maximum allowed size of the database */
51050 i64 journalSizeLimit; /* Size limit for persistent journal files */
51051 char *zFilename; /* Name of the database file */
51052 char *zJournal; /* Name of the journal file */
51053 int (*xBusyHandler)(void*); /* Function to call when busy */
51054 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
51055 int aStat[4]; /* Total cache hits, misses, writes, spills */
51056#ifdef SQLITE_TEST
51057 int nRead; /* Database pages read */
51058#endif
51059 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
51060 int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */
51061#ifdef SQLITE_HAS_CODEC
51062 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
51063 void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
51064 void (*xCodecFree)(void*); /* Destructor for the codec */
51065 void *pCodec; /* First argument to xCodec... methods */
51066#endif
51067 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
51068 PCache *pPCache; /* Pointer to page cache object */
51069#ifndef SQLITE_OMIT_WAL
51070 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
51071 char *zWal; /* File name for write-ahead log */
51072#endif
51073};
51074
51075/*
51076** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains
51077** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS
51078** or CACHE_WRITE to sqlite3_db_status().
51079*/
51080#define PAGER_STAT_HIT 0
51081#define PAGER_STAT_MISS 1
51082#define PAGER_STAT_WRITE 2
51083#define PAGER_STAT_SPILL 3
51084
51085/*
51086** The following global variables hold counters used for
51087** testing purposes only. These variables do not exist in
51088** a non-testing build. These variables are not thread-safe.
51089*/
51090#ifdef SQLITE_TEST
51091SQLITE_API int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
51092SQLITE_API int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
51093SQLITE_API int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
51094# define PAGER_INCR(v) v++
51095#else
51096# define PAGER_INCR(v)
51097#endif
51098
51099
51100
51101/*
51102** Journal files begin with the following magic string. The data
51103** was obtained from /dev/random. It is used only as a sanity check.
51104**
51105** Since version 2.8.0, the journal format contains additional sanity
51106** checking information. If the power fails while the journal is being
51107** written, semi-random garbage data might appear in the journal
51108** file after power is restored. If an attempt is then made
51109** to roll the journal back, the database could be corrupted. The additional
51110** sanity checking data is an attempt to discover the garbage in the
51111** journal and ignore it.
51112**
51113** The sanity checking information for the new journal format consists
51114** of a 32-bit checksum on each page of data. The checksum covers both
51115** the page number and the pPager->pageSize bytes of data for the page.
51116** This cksum is initialized to a 32-bit random value that appears in the
51117** journal file right after the header. The random initializer is important,
51118** because garbage data that appears at the end of a journal is likely
51119** data that was once in other files that have now been deleted. If the
51120** garbage data came from an obsolete journal file, the checksums might
51121** be correct. But by initializing the checksum to random value which
51122** is different for every journal, we minimize that risk.
51123*/
51124static const unsigned char aJournalMagic[] = {
51125 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
51126};
51127
51128/*
51129** The size of the of each page record in the journal is given by
51130** the following macro.
51131*/
51132#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
51133
51134/*
51135** The journal header size for this pager. This is usually the same
51136** size as a single disk sector. See also setSectorSize().
51137*/
51138#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
51139
51140/*
51141** The macro MEMDB is true if we are dealing with an in-memory database.
51142** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
51143** the value of MEMDB will be a constant and the compiler will optimize
51144** out code that would never execute.
51145*/
51146#ifdef SQLITE_OMIT_MEMORYDB
51147# define MEMDB 0
51148#else
51149# define MEMDB pPager->memDb
51150#endif
51151
51152/*
51153** The macro USEFETCH is true if we are allowed to use the xFetch and xUnfetch
51154** interfaces to access the database using memory-mapped I/O.
51155*/
51156#if SQLITE_MAX_MMAP_SIZE>0
51157# define USEFETCH(x) ((x)->bUseFetch)
51158#else
51159# define USEFETCH(x) 0
51160#endif
51161
51162/*
51163** The maximum legal page number is (2^31 - 1).
51164*/
51165#define PAGER_MAX_PGNO 2147483647
51166
51167/*
51168** The argument to this macro is a file descriptor (type sqlite3_file*).
51169** Return 0 if it is not open, or non-zero (but not 1) if it is.
51170**
51171** This is so that expressions can be written as:
51172**
51173** if( isOpen(pPager->jfd) ){ ...
51174**
51175** instead of
51176**
51177** if( pPager->jfd->pMethods ){ ...
51178*/
51179#define isOpen(pFd) ((pFd)->pMethods!=0)
51180
51181#ifdef SQLITE_DIRECT_OVERFLOW_READ
51182/*
51183** Return true if page pgno can be read directly from the database file
51184** by the b-tree layer. This is the case if:
51185**
51186** * the database file is open,
51187** * there are no dirty pages in the cache, and
51188** * the desired page is not currently in the wal file.
51189*/
51190SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){
51191 if( pPager->fd->pMethods==0 ) return 0;
51192 if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
51193#ifndef SQLITE_OMIT_WAL
51194 if( pPager->pWal ){
51195 u32 iRead = 0;
51196 int rc;
51197 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
51198 return (rc==SQLITE_OK && iRead==0);
51199 }
51200#endif
51201 return 1;
51202}
51203#endif
51204
51205#ifndef SQLITE_OMIT_WAL
51206# define pagerUseWal(x) ((x)->pWal!=0)
51207#else
51208# define pagerUseWal(x) 0
51209# define pagerRollbackWal(x) 0
51210# define pagerWalFrames(v,w,x,y) 0
51211# define pagerOpenWalIfPresent(z) SQLITE_OK
51212# define pagerBeginReadTransaction(z) SQLITE_OK
51213#endif
51214
51215#ifndef NDEBUG
51216/*
51217** Usage:
51218**
51219** assert( assert_pager_state(pPager) );
51220**
51221** This function runs many asserts to try to find inconsistencies in
51222** the internal state of the Pager object.
51223*/
51224static int assert_pager_state(Pager *p){
51225 Pager *pPager = p;
51226
51227 /* State must be valid. */
51228 assert( p->eState==PAGER_OPEN
51229 || p->eState==PAGER_READER
51230 || p->eState==PAGER_WRITER_LOCKED
51231 || p->eState==PAGER_WRITER_CACHEMOD
51232 || p->eState==PAGER_WRITER_DBMOD
51233 || p->eState==PAGER_WRITER_FINISHED
51234 || p->eState==PAGER_ERROR
51235 );
51236
51237 /* Regardless of the current state, a temp-file connection always behaves
51238 ** as if it has an exclusive lock on the database file. It never updates
51239 ** the change-counter field, so the changeCountDone flag is always set.
51240 */
51241 assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
51242 assert( p->tempFile==0 || pPager->changeCountDone );
51243
51244 /* If the useJournal flag is clear, the journal-mode must be "OFF".
51245 ** And if the journal-mode is "OFF", the journal file must not be open.
51246 */
51247 assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
51248 assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
51249
51250 /* Check that MEMDB implies noSync. And an in-memory journal. Since
51251 ** this means an in-memory pager performs no IO at all, it cannot encounter
51252 ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing
51253 ** a journal file. (although the in-memory journal implementation may
51254 ** return SQLITE_IOERR_NOMEM while the journal file is being written). It
51255 ** is therefore not possible for an in-memory pager to enter the ERROR
51256 ** state.
51257 */
51258 if( MEMDB ){
51259 assert( !isOpen(p->fd) );
51260 assert( p->noSync );
51261 assert( p->journalMode==PAGER_JOURNALMODE_OFF
51262 || p->journalMode==PAGER_JOURNALMODE_MEMORY
51263 );
51264 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
51265 assert( pagerUseWal(p)==0 );
51266 }
51267
51268 /* If changeCountDone is set, a RESERVED lock or greater must be held
51269 ** on the file.
51270 */
51271 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
51272 assert( p->eLock!=PENDING_LOCK );
51273
51274 switch( p->eState ){
51275 case PAGER_OPEN:
51276 assert( !MEMDB );
51277 assert( pPager->errCode==SQLITE_OK );
51278 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
51279 break;
51280
51281 case PAGER_READER:
51282 assert( pPager->errCode==SQLITE_OK );
51283 assert( p->eLock!=UNKNOWN_LOCK );
51284 assert( p->eLock>=SHARED_LOCK );
51285 break;
51286
51287 case PAGER_WRITER_LOCKED:
51288 assert( p->eLock!=UNKNOWN_LOCK );
51289 assert( pPager->errCode==SQLITE_OK );
51290 if( !pagerUseWal(pPager) ){
51291 assert( p->eLock>=RESERVED_LOCK );
51292 }
51293 assert( pPager->dbSize==pPager->dbOrigSize );
51294 assert( pPager->dbOrigSize==pPager->dbFileSize );
51295 assert( pPager->dbOrigSize==pPager->dbHintSize );
51296 assert( pPager->setMaster==0 );
51297 break;
51298
51299 case PAGER_WRITER_CACHEMOD:
51300 assert( p->eLock!=UNKNOWN_LOCK );
51301 assert( pPager->errCode==SQLITE_OK );
51302 if( !pagerUseWal(pPager) ){
51303 /* It is possible that if journal_mode=wal here that neither the
51304 ** journal file nor the WAL file are open. This happens during
51305 ** a rollback transaction that switches from journal_mode=off
51306 ** to journal_mode=wal.
51307 */
51308 assert( p->eLock>=RESERVED_LOCK );
51309 assert( isOpen(p->jfd)
51310 || p->journalMode==PAGER_JOURNALMODE_OFF
51311 || p->journalMode==PAGER_JOURNALMODE_WAL
51312 );
51313 }
51314 assert( pPager->dbOrigSize==pPager->dbFileSize );
51315 assert( pPager->dbOrigSize==pPager->dbHintSize );
51316 break;
51317
51318 case PAGER_WRITER_DBMOD:
51319 assert( p->eLock==EXCLUSIVE_LOCK );
51320 assert( pPager->errCode==SQLITE_OK );
51321 assert( !pagerUseWal(pPager) );
51322 assert( p->eLock>=EXCLUSIVE_LOCK );
51323 assert( isOpen(p->jfd)
51324 || p->journalMode==PAGER_JOURNALMODE_OFF
51325 || p->journalMode==PAGER_JOURNALMODE_WAL
51326 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
51327 );
51328 assert( pPager->dbOrigSize<=pPager->dbHintSize );
51329 break;
51330
51331 case PAGER_WRITER_FINISHED:
51332 assert( p->eLock==EXCLUSIVE_LOCK );
51333 assert( pPager->errCode==SQLITE_OK );
51334 assert( !pagerUseWal(pPager) );
51335 assert( isOpen(p->jfd)
51336 || p->journalMode==PAGER_JOURNALMODE_OFF
51337 || p->journalMode==PAGER_JOURNALMODE_WAL
51338 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
51339 );
51340 break;
51341
51342 case PAGER_ERROR:
51343 /* There must be at least one outstanding reference to the pager if
51344 ** in ERROR state. Otherwise the pager should have already dropped
51345 ** back to OPEN state.
51346 */
51347 assert( pPager->errCode!=SQLITE_OK );
51348 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile );
51349 break;
51350 }
51351
51352 return 1;
51353}
51354#endif /* ifndef NDEBUG */
51355
51356#ifdef SQLITE_DEBUG
51357/*
51358** Return a pointer to a human readable string in a static buffer
51359** containing the state of the Pager object passed as an argument. This
51360** is intended to be used within debuggers. For example, as an alternative
51361** to "print *pPager" in gdb:
51362**
51363** (gdb) printf "%s", print_pager_state(pPager)
51364**
51365** This routine has external linkage in order to suppress compiler warnings
51366** about an unused function. It is enclosed within SQLITE_DEBUG and so does
51367** not appear in normal builds.
51368*/
51369char *print_pager_state(Pager *p){
51370 static char zRet[1024];
51371
51372 sqlite3_snprintf(1024, zRet,
51373 "Filename: %s\n"
51374 "State: %s errCode=%d\n"
51375 "Lock: %s\n"
51376 "Locking mode: locking_mode=%s\n"
51377 "Journal mode: journal_mode=%s\n"
51378 "Backing store: tempFile=%d memDb=%d useJournal=%d\n"
51379 "Journal: journalOff=%lld journalHdr=%lld\n"
51380 "Size: dbsize=%d dbOrigSize=%d dbFileSize=%d\n"
51381 , p->zFilename
51382 , p->eState==PAGER_OPEN ? "OPEN" :
51383 p->eState==PAGER_READER ? "READER" :
51384 p->eState==PAGER_WRITER_LOCKED ? "WRITER_LOCKED" :
51385 p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
51386 p->eState==PAGER_WRITER_DBMOD ? "WRITER_DBMOD" :
51387 p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
51388 p->eState==PAGER_ERROR ? "ERROR" : "?error?"
51389 , (int)p->errCode
51390 , p->eLock==NO_LOCK ? "NO_LOCK" :
51391 p->eLock==RESERVED_LOCK ? "RESERVED" :
51392 p->eLock==EXCLUSIVE_LOCK ? "EXCLUSIVE" :
51393 p->eLock==SHARED_LOCK ? "SHARED" :
51394 p->eLock==UNKNOWN_LOCK ? "UNKNOWN" : "?error?"
51395 , p->exclusiveMode ? "exclusive" : "normal"
51396 , p->journalMode==PAGER_JOURNALMODE_MEMORY ? "memory" :
51397 p->journalMode==PAGER_JOURNALMODE_OFF ? "off" :
51398 p->journalMode==PAGER_JOURNALMODE_DELETE ? "delete" :
51399 p->journalMode==PAGER_JOURNALMODE_PERSIST ? "persist" :
51400 p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
51401 p->journalMode==PAGER_JOURNALMODE_WAL ? "wal" : "?error?"
51402 , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
51403 , p->journalOff, p->journalHdr
51404 , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
51405 );
51406
51407 return zRet;
51408}
51409#endif
51410
51411/* Forward references to the various page getters */
51412static int getPageNormal(Pager*,Pgno,DbPage**,int);
51413static int getPageError(Pager*,Pgno,DbPage**,int);
51414#if SQLITE_MAX_MMAP_SIZE>0
51415static int getPageMMap(Pager*,Pgno,DbPage**,int);
51416#endif
51417
51418/*
51419** Set the Pager.xGet method for the appropriate routine used to fetch
51420** content from the pager.
51421*/
51422static void setGetterMethod(Pager *pPager){
51423 if( pPager->errCode ){
51424 pPager->xGet = getPageError;
51425#if SQLITE_MAX_MMAP_SIZE>0
51426 }else if( USEFETCH(pPager)
51427#ifdef SQLITE_HAS_CODEC
51428 && pPager->xCodec==0
51429#endif
51430 ){
51431 pPager->xGet = getPageMMap;
51432#endif /* SQLITE_MAX_MMAP_SIZE>0 */
51433 }else{
51434 pPager->xGet = getPageNormal;
51435 }
51436}
51437
51438/*
51439** Return true if it is necessary to write page *pPg into the sub-journal.
51440** A page needs to be written into the sub-journal if there exists one
51441** or more open savepoints for which:
51442**
51443** * The page-number is less than or equal to PagerSavepoint.nOrig, and
51444** * The bit corresponding to the page-number is not set in
51445** PagerSavepoint.pInSavepoint.
51446*/
51447static int subjRequiresPage(PgHdr *pPg){
51448 Pager *pPager = pPg->pPager;
51449 PagerSavepoint *p;
51450 Pgno pgno = pPg->pgno;
51451 int i;
51452 for(i=0; i<pPager->nSavepoint; i++){
51453 p = &pPager->aSavepoint[i];
51454 if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){
51455 return 1;
51456 }
51457 }
51458 return 0;
51459}
51460
51461#ifdef SQLITE_DEBUG
51462/*
51463** Return true if the page is already in the journal file.
51464*/
51465static int pageInJournal(Pager *pPager, PgHdr *pPg){
51466 return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
51467}
51468#endif
51469
51470/*
51471** Read a 32-bit integer from the given file descriptor. Store the integer
51472** that is read in *pRes. Return SQLITE_OK if everything worked, or an
51473** error code is something goes wrong.
51474**
51475** All values are stored on disk as big-endian.
51476*/
51477static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
51478 unsigned char ac[4];
51479 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
51480 if( rc==SQLITE_OK ){
51481 *pRes = sqlite3Get4byte(ac);
51482 }
51483 return rc;
51484}
51485
51486/*
51487** Write a 32-bit integer into a string buffer in big-endian byte order.
51488*/
51489#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
51490
51491
51492/*
51493** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
51494** on success or an error code is something goes wrong.
51495*/
51496static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
51497 char ac[4];
51498 put32bits(ac, val);
51499 return sqlite3OsWrite(fd, ac, 4, offset);
51500}
51501
51502/*
51503** Unlock the database file to level eLock, which must be either NO_LOCK
51504** or SHARED_LOCK. Regardless of whether or not the call to xUnlock()
51505** succeeds, set the Pager.eLock variable to match the (attempted) new lock.
51506**
51507** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
51508** called, do not modify it. See the comment above the #define of
51509** UNKNOWN_LOCK for an explanation of this.
51510*/
51511static int pagerUnlockDb(Pager *pPager, int eLock){
51512 int rc = SQLITE_OK;
51513
51514 assert( !pPager->exclusiveMode || pPager->eLock==eLock );
51515 assert( eLock==NO_LOCK || eLock==SHARED_LOCK );
51516 assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
51517 if( isOpen(pPager->fd) ){
51518 assert( pPager->eLock>=eLock );
51519 rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(pPager->fd, eLock);
51520 if( pPager->eLock!=UNKNOWN_LOCK ){
51521 pPager->eLock = (u8)eLock;
51522 }
51523 IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
51524 }
51525 return rc;
51526}
51527
51528/*
51529** Lock the database file to level eLock, which must be either SHARED_LOCK,
51530** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the
51531** Pager.eLock variable to the new locking state.
51532**
51533** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
51534** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK.
51535** See the comment above the #define of UNKNOWN_LOCK for an explanation
51536** of this.
51537*/
51538static int pagerLockDb(Pager *pPager, int eLock){
51539 int rc = SQLITE_OK;
51540
51541 assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
51542 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
51543 rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(pPager->fd, eLock);
51544 if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
51545 pPager->eLock = (u8)eLock;
51546 IOTRACE(("LOCK %p %d\n", pPager, eLock))
51547 }
51548 }
51549 return rc;
51550}
51551
51552/*
51553** This function determines whether or not the atomic-write or
51554** atomic-batch-write optimizations can be used with this pager. The
51555** atomic-write optimization can be used if:
51556**
51557** (a) the value returned by OsDeviceCharacteristics() indicates that
51558** a database page may be written atomically, and
51559** (b) the value returned by OsSectorSize() is less than or equal
51560** to the page size.
51561**
51562** If it can be used, then the value returned is the size of the journal
51563** file when it contains rollback data for exactly one page.
51564**
51565** The atomic-batch-write optimization can be used if OsDeviceCharacteristics()
51566** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is
51567** returned in this case.
51568**
51569** If neither optimization can be used, 0 is returned.
51570*/
51571static int jrnlBufferSize(Pager *pPager){
51572 assert( !MEMDB );
51573
51574#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
51575 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
51576 int dc; /* Device characteristics */
51577
51578 assert( isOpen(pPager->fd) );
51579 dc = sqlite3OsDeviceCharacteristics(pPager->fd);
51580#else
51581 UNUSED_PARAMETER(pPager);
51582#endif
51583
51584#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
51585 if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){
51586 return -1;
51587 }
51588#endif
51589
51590#ifdef SQLITE_ENABLE_ATOMIC_WRITE
51591 {
51592 int nSector = pPager->sectorSize;
51593 int szPage = pPager->pageSize;
51594
51595 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
51596 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
51597 if( 0==(dc&(SQLITE_IOCAP_ATOMIC|(szPage>>8)) || nSector>szPage) ){
51598 return 0;
51599 }
51600 }
51601
51602 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
51603#endif
51604
51605 return 0;
51606}
51607
51608/*
51609** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
51610** on the cache using a hash function. This is used for testing
51611** and debugging only.
51612*/
51613#ifdef SQLITE_CHECK_PAGES
51614/*
51615** Return a 32-bit hash of the page data for pPage.
51616*/
51617static u32 pager_datahash(int nByte, unsigned char *pData){
51618 u32 hash = 0;
51619 int i;
51620 for(i=0; i<nByte; i++){
51621 hash = (hash*1039) + pData[i];
51622 }
51623 return hash;
51624}
51625static u32 pager_pagehash(PgHdr *pPage){
51626 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
51627}
51628static void pager_set_pagehash(PgHdr *pPage){
51629 pPage->pageHash = pager_pagehash(pPage);
51630}
51631
51632/*
51633** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
51634** is defined, and NDEBUG is not defined, an assert() statement checks
51635** that the page is either dirty or still matches the calculated page-hash.
51636*/
51637#define CHECK_PAGE(x) checkPage(x)
51638static void checkPage(PgHdr *pPg){
51639 Pager *pPager = pPg->pPager;
51640 assert( pPager->eState!=PAGER_ERROR );
51641 assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
51642}
51643
51644#else
51645#define pager_datahash(X,Y) 0
51646#define pager_pagehash(X) 0
51647#define pager_set_pagehash(X)
51648#define CHECK_PAGE(x)
51649#endif /* SQLITE_CHECK_PAGES */
51650
51651/*
51652** When this is called the journal file for pager pPager must be open.
51653** This function attempts to read a master journal file name from the
51654** end of the file and, if successful, copies it into memory supplied
51655** by the caller. See comments above writeMasterJournal() for the format
51656** used to store a master journal file name at the end of a journal file.
51657**
51658** zMaster must point to a buffer of at least nMaster bytes allocated by
51659** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
51660** enough space to write the master journal name). If the master journal
51661** name in the journal is longer than nMaster bytes (including a
51662** nul-terminator), then this is handled as if no master journal name
51663** were present in the journal.
51664**
51665** If a master journal file name is present at the end of the journal
51666** file, then it is copied into the buffer pointed to by zMaster. A
51667** nul-terminator byte is appended to the buffer following the master
51668** journal file name.
51669**
51670** If it is determined that no master journal file name is present
51671** zMaster[0] is set to 0 and SQLITE_OK returned.
51672**
51673** If an error occurs while reading from the journal file, an SQLite
51674** error code is returned.
51675*/
51676static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){
51677 int rc; /* Return code */
51678 u32 len; /* Length in bytes of master journal name */
51679 i64 szJ; /* Total size in bytes of journal file pJrnl */
51680 u32 cksum; /* MJ checksum value read from journal */
51681 u32 u; /* Unsigned loop counter */
51682 unsigned char aMagic[8]; /* A buffer to hold the magic header */
51683 zMaster[0] = '\0';
51684
51685 if( SQLITE_OK!=(rc = sqlite3OsFileSize(pJrnl, &szJ))
51686 || szJ<16
51687 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
51688 || len>=nMaster
51689 || len>szJ-16
51690 || len==0
51691 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
51692 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
51693 || memcmp(aMagic, aJournalMagic, 8)
51694 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len))
51695 ){
51696 return rc;
51697 }
51698
51699 /* See if the checksum matches the master journal name */
51700 for(u=0; u<len; u++){
51701 cksum -= zMaster[u];
51702 }
51703 if( cksum ){
51704 /* If the checksum doesn't add up, then one or more of the disk sectors
51705 ** containing the master journal filename is corrupted. This means
51706 ** definitely roll back, so just return SQLITE_OK and report a (nul)
51707 ** master-journal filename.
51708 */
51709 len = 0;
51710 }
51711 zMaster[len] = '\0';
51712
51713 return SQLITE_OK;
51714}
51715
51716/*
51717** Return the offset of the sector boundary at or immediately
51718** following the value in pPager->journalOff, assuming a sector
51719** size of pPager->sectorSize bytes.
51720**
51721** i.e for a sector size of 512:
51722**
51723** Pager.journalOff Return value
51724** ---------------------------------------
51725** 0 0
51726** 512 512
51727** 100 512
51728** 2000 2048
51729**
51730*/
51731static i64 journalHdrOffset(Pager *pPager){
51732 i64 offset = 0;
51733 i64 c = pPager->journalOff;
51734 if( c ){
51735 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
51736 }
51737 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
51738 assert( offset>=c );
51739 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
51740 return offset;
51741}
51742
51743/*
51744** The journal file must be open when this function is called.
51745**
51746** This function is a no-op if the journal file has not been written to
51747** within the current transaction (i.e. if Pager.journalOff==0).
51748**
51749** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
51750** set to 0, then truncate the journal file to zero bytes in size. Otherwise,
51751** zero the 28-byte header at the start of the journal file. In either case,
51752** if the pager is not in no-sync mode, sync the journal file immediately
51753** after writing or truncating it.
51754**
51755** If Pager.journalSizeLimit is set to a positive, non-zero value, and
51756** following the truncation or zeroing described above the size of the
51757** journal file in bytes is larger than this value, then truncate the
51758** journal file to Pager.journalSizeLimit bytes. The journal file does
51759** not need to be synced following this operation.
51760**
51761** If an IO error occurs, abandon processing and return the IO error code.
51762** Otherwise, return SQLITE_OK.
51763*/
51764static int zeroJournalHdr(Pager *pPager, int doTruncate){
51765 int rc = SQLITE_OK; /* Return code */
51766 assert( isOpen(pPager->jfd) );
51767 assert( !sqlite3JournalIsInMemory(pPager->jfd) );
51768 if( pPager->journalOff ){
51769 const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */
51770
51771 IOTRACE(("JZEROHDR %p\n", pPager))
51772 if( doTruncate || iLimit==0 ){
51773 rc = sqlite3OsTruncate(pPager->jfd, 0);
51774 }else{
51775 static const char zeroHdr[28] = {0};
51776 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
51777 }
51778 if( rc==SQLITE_OK && !pPager->noSync ){
51779 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
51780 }
51781
51782 /* At this point the transaction is committed but the write lock
51783 ** is still held on the file. If there is a size limit configured for
51784 ** the persistent journal and the journal file currently consumes more
51785 ** space than that limit allows for, truncate it now. There is no need
51786 ** to sync the file following this operation.
51787 */
51788 if( rc==SQLITE_OK && iLimit>0 ){
51789 i64 sz;
51790 rc = sqlite3OsFileSize(pPager->jfd, &sz);
51791 if( rc==SQLITE_OK && sz>iLimit ){
51792 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
51793 }
51794 }
51795 }
51796 return rc;
51797}
51798
51799/*
51800** The journal file must be open when this routine is called. A journal
51801** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
51802** current location.
51803**
51804** The format for the journal header is as follows:
51805** - 8 bytes: Magic identifying journal format.
51806** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
51807** - 4 bytes: Random number used for page hash.
51808** - 4 bytes: Initial database page count.
51809** - 4 bytes: Sector size used by the process that wrote this journal.
51810** - 4 bytes: Database page size.
51811**
51812** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
51813*/
51814static int writeJournalHdr(Pager *pPager){
51815 int rc = SQLITE_OK; /* Return code */
51816 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
51817 u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
51818 u32 nWrite; /* Bytes of header sector written */
51819 int ii; /* Loop counter */
51820
51821 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
51822
51823 if( nHeader>JOURNAL_HDR_SZ(pPager) ){
51824 nHeader = JOURNAL_HDR_SZ(pPager);
51825 }
51826
51827 /* If there are active savepoints and any of them were created
51828 ** since the most recent journal header was written, update the
51829 ** PagerSavepoint.iHdrOffset fields now.
51830 */
51831 for(ii=0; ii<pPager->nSavepoint; ii++){
51832 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
51833 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
51834 }
51835 }
51836
51837 pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
51838
51839 /*
51840 ** Write the nRec Field - the number of page records that follow this
51841 ** journal header. Normally, zero is written to this value at this time.
51842 ** After the records are added to the journal (and the journal synced,
51843 ** if in full-sync mode), the zero is overwritten with the true number
51844 ** of records (see syncJournal()).
51845 **
51846 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
51847 ** reading the journal this value tells SQLite to assume that the
51848 ** rest of the journal file contains valid page records. This assumption
51849 ** is dangerous, as if a failure occurred whilst writing to the journal
51850 ** file it may contain some garbage data. There are two scenarios
51851 ** where this risk can be ignored:
51852 **
51853 ** * When the pager is in no-sync mode. Corruption can follow a
51854 ** power failure in this case anyway.
51855 **
51856 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
51857 ** that garbage data is never appended to the journal file.
51858 */
51859 assert( isOpen(pPager->fd) || pPager->noSync );
51860 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
51861 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
51862 ){
51863 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
51864 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
51865 }else{
51866 memset(zHeader, 0, sizeof(aJournalMagic)+4);
51867 }
51868
51869 /* The random check-hash initializer */
51870 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
51871 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
51872 /* The initial database size */
51873 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
51874 /* The assumed sector size for this process */
51875 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
51876
51877 /* The page size */
51878 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
51879
51880 /* Initializing the tail of the buffer is not necessary. Everything
51881 ** works find if the following memset() is omitted. But initializing
51882 ** the memory prevents valgrind from complaining, so we are willing to
51883 ** take the performance hit.
51884 */
51885 memset(&zHeader[sizeof(aJournalMagic)+20], 0,
51886 nHeader-(sizeof(aJournalMagic)+20));
51887
51888 /* In theory, it is only necessary to write the 28 bytes that the
51889 ** journal header consumes to the journal file here. Then increment the
51890 ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next
51891 ** record is written to the following sector (leaving a gap in the file
51892 ** that will be implicitly filled in by the OS).
51893 **
51894 ** However it has been discovered that on some systems this pattern can
51895 ** be significantly slower than contiguously writing data to the file,
51896 ** even if that means explicitly writing data to the block of
51897 ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
51898 ** is done.
51899 **
51900 ** The loop is required here in case the sector-size is larger than the
51901 ** database page size. Since the zHeader buffer is only Pager.pageSize
51902 ** bytes in size, more than one call to sqlite3OsWrite() may be required
51903 ** to populate the entire journal header sector.
51904 */
51905 for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
51906 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
51907 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
51908 assert( pPager->journalHdr <= pPager->journalOff );
51909 pPager->journalOff += nHeader;
51910 }
51911
51912 return rc;
51913}
51914
51915/*
51916** The journal file must be open when this is called. A journal header file
51917** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
51918** file. The current location in the journal file is given by
51919** pPager->journalOff. See comments above function writeJournalHdr() for
51920** a description of the journal header format.
51921**
51922** If the header is read successfully, *pNRec is set to the number of
51923** page records following this header and *pDbSize is set to the size of the
51924** database before the transaction began, in pages. Also, pPager->cksumInit
51925** is set to the value read from the journal header. SQLITE_OK is returned
51926** in this case.
51927**
51928** If the journal header file appears to be corrupted, SQLITE_DONE is
51929** returned and *pNRec and *PDbSize are undefined. If JOURNAL_HDR_SZ bytes
51930** cannot be read from the journal file an error code is returned.
51931*/
51932static int readJournalHdr(
51933 Pager *pPager, /* Pager object */
51934 int isHot,
51935 i64 journalSize, /* Size of the open journal file in bytes */
51936 u32 *pNRec, /* OUT: Value read from the nRec field */
51937 u32 *pDbSize /* OUT: Value of original database size field */
51938){
51939 int rc; /* Return code */
51940 unsigned char aMagic[8]; /* A buffer to hold the magic header */
51941 i64 iHdrOff; /* Offset of journal header being read */
51942
51943 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
51944
51945 /* Advance Pager.journalOff to the start of the next sector. If the
51946 ** journal file is too small for there to be a header stored at this
51947 ** point, return SQLITE_DONE.
51948 */
51949 pPager->journalOff = journalHdrOffset(pPager);
51950 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
51951 return SQLITE_DONE;
51952 }
51953 iHdrOff = pPager->journalOff;
51954
51955 /* Read in the first 8 bytes of the journal header. If they do not match
51956 ** the magic string found at the start of each journal header, return
51957 ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
51958 ** proceed.
51959 */
51960 if( isHot || iHdrOff!=pPager->journalHdr ){
51961 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
51962 if( rc ){
51963 return rc;
51964 }
51965 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
51966 return SQLITE_DONE;
51967 }
51968 }
51969
51970 /* Read the first three 32-bit fields of the journal header: The nRec
51971 ** field, the checksum-initializer and the database size at the start
51972 ** of the transaction. Return an error code if anything goes wrong.
51973 */
51974 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
51975 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
51976 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
51977 ){
51978 return rc;
51979 }
51980
51981 if( pPager->journalOff==0 ){
51982 u32 iPageSize; /* Page-size field of journal header */
51983 u32 iSectorSize; /* Sector-size field of journal header */
51984
51985 /* Read the page-size and sector-size journal header fields. */
51986 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
51987 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
51988 ){
51989 return rc;
51990 }
51991
51992 /* Versions of SQLite prior to 3.5.8 set the page-size field of the
51993 ** journal header to zero. In this case, assume that the Pager.pageSize
51994 ** variable is already set to the correct page size.
51995 */
51996 if( iPageSize==0 ){
51997 iPageSize = pPager->pageSize;
51998 }
51999
52000 /* Check that the values read from the page-size and sector-size fields
52001 ** are within range. To be 'in range', both values need to be a power
52002 ** of two greater than or equal to 512 or 32, and not greater than their
52003 ** respective compile time maximum limits.
52004 */
52005 if( iPageSize<512 || iSectorSize<32
52006 || iPageSize>SQLITE_MAX_PAGE_SIZE || iSectorSize>MAX_SECTOR_SIZE
52007 || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0
52008 ){
52009 /* If the either the page-size or sector-size in the journal-header is
52010 ** invalid, then the process that wrote the journal-header must have
52011 ** crashed before the header was synced. In this case stop reading
52012 ** the journal file here.
52013 */
52014 return SQLITE_DONE;
52015 }
52016
52017 /* Update the page-size to match the value read from the journal.
52018 ** Use a testcase() macro to make sure that malloc failure within
52019 ** PagerSetPagesize() is tested.
52020 */
52021 rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
52022 testcase( rc!=SQLITE_OK );
52023
52024 /* Update the assumed sector-size to match the value used by
52025 ** the process that created this journal. If this journal was
52026 ** created by a process other than this one, then this routine
52027 ** is being called from within pager_playback(). The local value
52028 ** of Pager.sectorSize is restored at the end of that routine.
52029 */
52030 pPager->sectorSize = iSectorSize;
52031 }
52032
52033 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
52034 return rc;
52035}
52036
52037
52038/*
52039** Write the supplied master journal name into the journal file for pager
52040** pPager at the current location. The master journal name must be the last
52041** thing written to a journal file. If the pager is in full-sync mode, the
52042** journal file descriptor is advanced to the next sector boundary before
52043** anything is written. The format is:
52044**
52045** + 4 bytes: PAGER_MJ_PGNO.
52046** + N bytes: Master journal filename in utf-8.
52047** + 4 bytes: N (length of master journal name in bytes, no nul-terminator).
52048** + 4 bytes: Master journal name checksum.
52049** + 8 bytes: aJournalMagic[].
52050**
52051** The master journal page checksum is the sum of the bytes in the master
52052** journal name, where each byte is interpreted as a signed 8-bit integer.
52053**
52054** If zMaster is a NULL pointer (occurs for a single database transaction),
52055** this call is a no-op.
52056*/
52057static int writeMasterJournal(Pager *pPager, const char *zMaster){
52058 int rc; /* Return code */
52059 int nMaster; /* Length of string zMaster */
52060 i64 iHdrOff; /* Offset of header in journal file */
52061 i64 jrnlSize; /* Size of journal file on disk */
52062 u32 cksum = 0; /* Checksum of string zMaster */
52063
52064 assert( pPager->setMaster==0 );
52065 assert( !pagerUseWal(pPager) );
52066
52067 if( !zMaster
52068 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
52069 || !isOpen(pPager->jfd)
52070 ){
52071 return SQLITE_OK;
52072 }
52073 pPager->setMaster = 1;
52074 assert( pPager->journalHdr <= pPager->journalOff );
52075
52076 /* Calculate the length in bytes and the checksum of zMaster */
52077 for(nMaster=0; zMaster[nMaster]; nMaster++){
52078 cksum += zMaster[nMaster];
52079 }
52080
52081 /* If in full-sync mode, advance to the next disk sector before writing
52082 ** the master journal name. This is in case the previous page written to
52083 ** the journal has already been synced.
52084 */
52085 if( pPager->fullSync ){
52086 pPager->journalOff = journalHdrOffset(pPager);
52087 }
52088 iHdrOff = pPager->journalOff;
52089
52090 /* Write the master journal data to the end of the journal file. If
52091 ** an error occurs, return the error code to the caller.
52092 */
52093 if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
52094 || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
52095 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
52096 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
52097 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8,
52098 iHdrOff+4+nMaster+8)))
52099 ){
52100 return rc;
52101 }
52102 pPager->journalOff += (nMaster+20);
52103
52104 /* If the pager is in peristent-journal mode, then the physical
52105 ** journal-file may extend past the end of the master-journal name
52106 ** and 8 bytes of magic data just written to the file. This is
52107 ** dangerous because the code to rollback a hot-journal file
52108 ** will not be able to find the master-journal name to determine
52109 ** whether or not the journal is hot.
52110 **
52111 ** Easiest thing to do in this scenario is to truncate the journal
52112 ** file to the required size.
52113 */
52114 if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
52115 && jrnlSize>pPager->journalOff
52116 ){
52117 rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
52118 }
52119 return rc;
52120}
52121
52122/*
52123** Discard the entire contents of the in-memory page-cache.
52124*/
52125static void pager_reset(Pager *pPager){
52126 pPager->iDataVersion++;
52127 sqlite3BackupRestart(pPager->pBackup);
52128 sqlite3PcacheClear(pPager->pPCache);
52129}
52130
52131/*
52132** Return the pPager->iDataVersion value
52133*/
52134SQLITE_PRIVATE u32 sqlite3PagerDataVersion(Pager *pPager){
52135 return pPager->iDataVersion;
52136}
52137
52138/*
52139** Free all structures in the Pager.aSavepoint[] array and set both
52140** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
52141** if it is open and the pager is not in exclusive mode.
52142*/
52143static void releaseAllSavepoints(Pager *pPager){
52144 int ii; /* Iterator for looping through Pager.aSavepoint */
52145 for(ii=0; ii<pPager->nSavepoint; ii++){
52146 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
52147 }
52148 if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){
52149 sqlite3OsClose(pPager->sjfd);
52150 }
52151 sqlite3_free(pPager->aSavepoint);
52152 pPager->aSavepoint = 0;
52153 pPager->nSavepoint = 0;
52154 pPager->nSubRec = 0;
52155}
52156
52157/*
52158** Set the bit number pgno in the PagerSavepoint.pInSavepoint
52159** bitvecs of all open savepoints. Return SQLITE_OK if successful
52160** or SQLITE_NOMEM if a malloc failure occurs.
52161*/
52162static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
52163 int ii; /* Loop counter */
52164 int rc = SQLITE_OK; /* Result code */
52165
52166 for(ii=0; ii<pPager->nSavepoint; ii++){
52167 PagerSavepoint *p = &pPager->aSavepoint[ii];
52168 if( pgno<=p->nOrig ){
52169 rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
52170 testcase( rc==SQLITE_NOMEM );
52171 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
52172 }
52173 }
52174 return rc;
52175}
52176
52177/*
52178** This function is a no-op if the pager is in exclusive mode and not
52179** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN
52180** state.
52181**
52182** If the pager is not in exclusive-access mode, the database file is
52183** completely unlocked. If the file is unlocked and the file-system does
52184** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is
52185** closed (if it is open).
52186**
52187** If the pager is in ERROR state when this function is called, the
52188** contents of the pager cache are discarded before switching back to
52189** the OPEN state. Regardless of whether the pager is in exclusive-mode
52190** or not, any journal file left in the file-system will be treated
52191** as a hot-journal and rolled back the next time a read-transaction
52192** is opened (by this or by any other connection).
52193*/
52194static void pager_unlock(Pager *pPager){
52195
52196 assert( pPager->eState==PAGER_READER
52197 || pPager->eState==PAGER_OPEN
52198 || pPager->eState==PAGER_ERROR
52199 );
52200
52201 sqlite3BitvecDestroy(pPager->pInJournal);
52202 pPager->pInJournal = 0;
52203 releaseAllSavepoints(pPager);
52204
52205 if( pagerUseWal(pPager) ){
52206 assert( !isOpen(pPager->jfd) );
52207 sqlite3WalEndReadTransaction(pPager->pWal);
52208 pPager->eState = PAGER_OPEN;
52209 }else if( !pPager->exclusiveMode ){
52210 int rc; /* Error code returned by pagerUnlockDb() */
52211 int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
52212
52213 /* If the operating system support deletion of open files, then
52214 ** close the journal file when dropping the database lock. Otherwise
52215 ** another connection with journal_mode=delete might delete the file
52216 ** out from under us.
52217 */
52218 assert( (PAGER_JOURNALMODE_MEMORY & 5)!=1 );
52219 assert( (PAGER_JOURNALMODE_OFF & 5)!=1 );
52220 assert( (PAGER_JOURNALMODE_WAL & 5)!=1 );
52221 assert( (PAGER_JOURNALMODE_DELETE & 5)!=1 );
52222 assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
52223 assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
52224 if( 0==(iDc & SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN)
52225 || 1!=(pPager->journalMode & 5)
52226 ){
52227 sqlite3OsClose(pPager->jfd);
52228 }
52229
52230 /* If the pager is in the ERROR state and the call to unlock the database
52231 ** file fails, set the current lock to UNKNOWN_LOCK. See the comment
52232 ** above the #define for UNKNOWN_LOCK for an explanation of why this
52233 ** is necessary.
52234 */
52235 rc = pagerUnlockDb(pPager, NO_LOCK);
52236 if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
52237 pPager->eLock = UNKNOWN_LOCK;
52238 }
52239
52240 /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here
52241 ** without clearing the error code. This is intentional - the error
52242 ** code is cleared and the cache reset in the block below.
52243 */
52244 assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
52245 pPager->changeCountDone = 0;
52246 pPager->eState = PAGER_OPEN;
52247 }
52248
52249 /* If Pager.errCode is set, the contents of the pager cache cannot be
52250 ** trusted. Now that there are no outstanding references to the pager,
52251 ** it can safely move back to PAGER_OPEN state. This happens in both
52252 ** normal and exclusive-locking mode.
52253 */
52254 assert( pPager->errCode==SQLITE_OK || !MEMDB );
52255 if( pPager->errCode ){
52256 if( pPager->tempFile==0 ){
52257 pager_reset(pPager);
52258 pPager->changeCountDone = 0;
52259 pPager->eState = PAGER_OPEN;
52260 }else{
52261 pPager->eState = (isOpen(pPager->jfd) ? PAGER_OPEN : PAGER_READER);
52262 }
52263 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
52264 pPager->errCode = SQLITE_OK;
52265 setGetterMethod(pPager);
52266 }
52267
52268 pPager->journalOff = 0;
52269 pPager->journalHdr = 0;
52270 pPager->setMaster = 0;
52271}
52272
52273/*
52274** This function is called whenever an IOERR or FULL error that requires
52275** the pager to transition into the ERROR state may ahve occurred.
52276** The first argument is a pointer to the pager structure, the second
52277** the error-code about to be returned by a pager API function. The
52278** value returned is a copy of the second argument to this function.
52279**
52280** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the
52281** IOERR sub-codes, the pager enters the ERROR state and the error code
52282** is stored in Pager.errCode. While the pager remains in the ERROR state,
52283** all major API calls on the Pager will immediately return Pager.errCode.
52284**
52285** The ERROR state indicates that the contents of the pager-cache
52286** cannot be trusted. This state can be cleared by completely discarding
52287** the contents of the pager-cache. If a transaction was active when
52288** the persistent error occurred, then the rollback journal may need
52289** to be replayed to restore the contents of the database file (as if
52290** it were a hot-journal).
52291*/
52292static int pager_error(Pager *pPager, int rc){
52293 int rc2 = rc & 0xff;
52294 assert( rc==SQLITE_OK || !MEMDB );
52295 assert(
52296 pPager->errCode==SQLITE_FULL ||
52297 pPager->errCode==SQLITE_OK ||
52298 (pPager->errCode & 0xff)==SQLITE_IOERR
52299 );
52300 if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){
52301 pPager->errCode = rc;
52302 pPager->eState = PAGER_ERROR;
52303 setGetterMethod(pPager);
52304 }
52305 return rc;
52306}
52307
52308static int pager_truncate(Pager *pPager, Pgno nPage);
52309
52310/*
52311** The write transaction open on pPager is being committed (bCommit==1)
52312** or rolled back (bCommit==0).
52313**
52314** Return TRUE if and only if all dirty pages should be flushed to disk.
52315**
52316** Rules:
52317**
52318** * For non-TEMP databases, always sync to disk. This is necessary
52319** for transactions to be durable.
52320**
52321** * Sync TEMP database only on a COMMIT (not a ROLLBACK) when the backing
52322** file has been created already (via a spill on pagerStress()) and
52323** when the number of dirty pages in memory exceeds 25% of the total
52324** cache size.
52325*/
52326static int pagerFlushOnCommit(Pager *pPager, int bCommit){
52327 if( pPager->tempFile==0 ) return 1;
52328 if( !bCommit ) return 0;
52329 if( !isOpen(pPager->fd) ) return 0;
52330 return (sqlite3PCachePercentDirty(pPager->pPCache)>=25);
52331}
52332
52333/*
52334** This routine ends a transaction. A transaction is usually ended by
52335** either a COMMIT or a ROLLBACK operation. This routine may be called
52336** after rollback of a hot-journal, or if an error occurs while opening
52337** the journal file or writing the very first journal-header of a
52338** database transaction.
52339**
52340** This routine is never called in PAGER_ERROR state. If it is called
52341** in PAGER_NONE or PAGER_SHARED state and the lock held is less
52342** exclusive than a RESERVED lock, it is a no-op.
52343**
52344** Otherwise, any active savepoints are released.
52345**
52346** If the journal file is open, then it is "finalized". Once a journal
52347** file has been finalized it is not possible to use it to roll back a
52348** transaction. Nor will it be considered to be a hot-journal by this
52349** or any other database connection. Exactly how a journal is finalized
52350** depends on whether or not the pager is running in exclusive mode and
52351** the current journal-mode (Pager.journalMode value), as follows:
52352**
52353** journalMode==MEMORY
52354** Journal file descriptor is simply closed. This destroys an
52355** in-memory journal.
52356**
52357** journalMode==TRUNCATE
52358** Journal file is truncated to zero bytes in size.
52359**
52360** journalMode==PERSIST
52361** The first 28 bytes of the journal file are zeroed. This invalidates
52362** the first journal header in the file, and hence the entire journal
52363** file. An invalid journal file cannot be rolled back.
52364**
52365** journalMode==DELETE
52366** The journal file is closed and deleted using sqlite3OsDelete().
52367**
52368** If the pager is running in exclusive mode, this method of finalizing
52369** the journal file is never used. Instead, if the journalMode is
52370** DELETE and the pager is in exclusive mode, the method described under
52371** journalMode==PERSIST is used instead.
52372**
52373** After the journal is finalized, the pager moves to PAGER_READER state.
52374** If running in non-exclusive rollback mode, the lock on the file is
52375** downgraded to a SHARED_LOCK.
52376**
52377** SQLITE_OK is returned if no error occurs. If an error occurs during
52378** any of the IO operations to finalize the journal file or unlock the
52379** database then the IO error code is returned to the user. If the
52380** operation to finalize the journal file fails, then the code still
52381** tries to unlock the database file if not in exclusive mode. If the
52382** unlock operation fails as well, then the first error code related
52383** to the first error encountered (the journal finalization one) is
52384** returned.
52385*/
52386static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){
52387 int rc = SQLITE_OK; /* Error code from journal finalization operation */
52388 int rc2 = SQLITE_OK; /* Error code from db file unlock operation */
52389
52390 /* Do nothing if the pager does not have an open write transaction
52391 ** or at least a RESERVED lock. This function may be called when there
52392 ** is no write-transaction active but a RESERVED or greater lock is
52393 ** held under two circumstances:
52394 **
52395 ** 1. After a successful hot-journal rollback, it is called with
52396 ** eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK.
52397 **
52398 ** 2. If a connection with locking_mode=exclusive holding an EXCLUSIVE
52399 ** lock switches back to locking_mode=normal and then executes a
52400 ** read-transaction, this function is called with eState==PAGER_READER
52401 ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
52402 */
52403 assert( assert_pager_state(pPager) );
52404 assert( pPager->eState!=PAGER_ERROR );
52405 if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
52406 return SQLITE_OK;
52407 }
52408
52409 releaseAllSavepoints(pPager);
52410 assert( isOpen(pPager->jfd) || pPager->pInJournal==0
52411 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
52412 );
52413 if( isOpen(pPager->jfd) ){
52414 assert( !pagerUseWal(pPager) );
52415
52416 /* Finalize the journal file. */
52417 if( sqlite3JournalIsInMemory(pPager->jfd) ){
52418 /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */
52419 sqlite3OsClose(pPager->jfd);
52420 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
52421 if( pPager->journalOff==0 ){
52422 rc = SQLITE_OK;
52423 }else{
52424 rc = sqlite3OsTruncate(pPager->jfd, 0);
52425 if( rc==SQLITE_OK && pPager->fullSync ){
52426 /* Make sure the new file size is written into the inode right away.
52427 ** Otherwise the journal might resurrect following a power loss and
52428 ** cause the last transaction to roll back. See
52429 ** https://bugzilla.mozilla.org/show_bug.cgi?id=1072773
52430 */
52431 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
52432 }
52433 }
52434 pPager->journalOff = 0;
52435 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
52436 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
52437 ){
52438 rc = zeroJournalHdr(pPager, hasMaster||pPager->tempFile);
52439 pPager->journalOff = 0;
52440 }else{
52441 /* This branch may be executed with Pager.journalMode==MEMORY if
52442 ** a hot-journal was just rolled back. In this case the journal
52443 ** file should be closed and deleted. If this connection writes to
52444 ** the database file, it will do so using an in-memory journal.
52445 */
52446 int bDelete = !pPager->tempFile;
52447 assert( sqlite3JournalIsInMemory(pPager->jfd)==0 );
52448 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
52449 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
52450 || pPager->journalMode==PAGER_JOURNALMODE_WAL
52451 );
52452 sqlite3OsClose(pPager->jfd);
52453 if( bDelete ){
52454 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync);
52455 }
52456 }
52457 }
52458
52459#ifdef SQLITE_CHECK_PAGES
52460 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
52461 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
52462 PgHdr *p = sqlite3PagerLookup(pPager, 1);
52463 if( p ){
52464 p->pageHash = 0;
52465 sqlite3PagerUnrefNotNull(p);
52466 }
52467 }
52468#endif
52469
52470 sqlite3BitvecDestroy(pPager->pInJournal);
52471 pPager->pInJournal = 0;
52472 pPager->nRec = 0;
52473 if( rc==SQLITE_OK ){
52474 if( MEMDB || pagerFlushOnCommit(pPager, bCommit) ){
52475 sqlite3PcacheCleanAll(pPager->pPCache);
52476 }else{
52477 sqlite3PcacheClearWritable(pPager->pPCache);
52478 }
52479 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
52480 }
52481
52482 if( pagerUseWal(pPager) ){
52483 /* Drop the WAL write-lock, if any. Also, if the connection was in
52484 ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE
52485 ** lock held on the database file.
52486 */
52487 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
52488 assert( rc2==SQLITE_OK );
52489 }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){
52490 /* This branch is taken when committing a transaction in rollback-journal
52491 ** mode if the database file on disk is larger than the database image.
52492 ** At this point the journal has been finalized and the transaction
52493 ** successfully committed, but the EXCLUSIVE lock is still held on the
52494 ** file. So it is safe to truncate the database file to its minimum
52495 ** required size. */
52496 assert( pPager->eLock==EXCLUSIVE_LOCK );
52497 rc = pager_truncate(pPager, pPager->dbSize);
52498 }
52499
52500 if( rc==SQLITE_OK && bCommit ){
52501 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
52502 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
52503 }
52504
52505 if( !pPager->exclusiveMode
52506 && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
52507 ){
52508 rc2 = pagerUnlockDb(pPager, SHARED_LOCK);
52509 pPager->changeCountDone = 0;
52510 }
52511 pPager->eState = PAGER_READER;
52512 pPager->setMaster = 0;
52513
52514 return (rc==SQLITE_OK?rc2:rc);
52515}
52516
52517/*
52518** Execute a rollback if a transaction is active and unlock the
52519** database file.
52520**
52521** If the pager has already entered the ERROR state, do not attempt
52522** the rollback at this time. Instead, pager_unlock() is called. The
52523** call to pager_unlock() will discard all in-memory pages, unlock
52524** the database file and move the pager back to OPEN state. If this
52525** means that there is a hot-journal left in the file-system, the next
52526** connection to obtain a shared lock on the pager (which may be this one)
52527** will roll it back.
52528**
52529** If the pager has not already entered the ERROR state, but an IO or
52530** malloc error occurs during a rollback, then this will itself cause
52531** the pager to enter the ERROR state. Which will be cleared by the
52532** call to pager_unlock(), as described above.
52533*/
52534static void pagerUnlockAndRollback(Pager *pPager){
52535 if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
52536 assert( assert_pager_state(pPager) );
52537 if( pPager->eState>=PAGER_WRITER_LOCKED ){
52538 sqlite3BeginBenignMalloc();
52539 sqlite3PagerRollback(pPager);
52540 sqlite3EndBenignMalloc();
52541 }else if( !pPager->exclusiveMode ){
52542 assert( pPager->eState==PAGER_READER );
52543 pager_end_transaction(pPager, 0, 0);
52544 }
52545 }
52546 pager_unlock(pPager);
52547}
52548
52549/*
52550** Parameter aData must point to a buffer of pPager->pageSize bytes
52551** of data. Compute and return a checksum based ont the contents of the
52552** page of data and the current value of pPager->cksumInit.
52553**
52554** This is not a real checksum. It is really just the sum of the
52555** random initial value (pPager->cksumInit) and every 200th byte
52556** of the page data, starting with byte offset (pPager->pageSize%200).
52557** Each byte is interpreted as an 8-bit unsigned integer.
52558**
52559** Changing the formula used to compute this checksum results in an
52560** incompatible journal file format.
52561**
52562** If journal corruption occurs due to a power failure, the most likely
52563** scenario is that one end or the other of the record will be changed.
52564** It is much less likely that the two ends of the journal record will be
52565** correct and the middle be corrupt. Thus, this "checksum" scheme,
52566** though fast and simple, catches the mostly likely kind of corruption.
52567*/
52568static u32 pager_cksum(Pager *pPager, const u8 *aData){
52569 u32 cksum = pPager->cksumInit; /* Checksum value to return */
52570 int i = pPager->pageSize-200; /* Loop counter */
52571 while( i>0 ){
52572 cksum += aData[i];
52573 i -= 200;
52574 }
52575 return cksum;
52576}
52577
52578/*
52579** Report the current page size and number of reserved bytes back
52580** to the codec.
52581*/
52582#ifdef SQLITE_HAS_CODEC
52583static void pagerReportSize(Pager *pPager){
52584 if( pPager->xCodecSizeChng ){
52585 pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize,
52586 (int)pPager->nReserve);
52587 }
52588}
52589#else
52590# define pagerReportSize(X) /* No-op if we do not support a codec */
52591#endif
52592
52593#ifdef SQLITE_HAS_CODEC
52594/*
52595** Make sure the number of reserved bits is the same in the destination
52596** pager as it is in the source. This comes up when a VACUUM changes the
52597** number of reserved bits to the "optimal" amount.
52598*/
52599SQLITE_PRIVATE void sqlite3PagerAlignReserve(Pager *pDest, Pager *pSrc){
52600 if( pDest->nReserve!=pSrc->nReserve ){
52601 pDest->nReserve = pSrc->nReserve;
52602 pagerReportSize(pDest);
52603 }
52604}
52605#endif
52606
52607/*
52608** Read a single page from either the journal file (if isMainJrnl==1) or
52609** from the sub-journal (if isMainJrnl==0) and playback that page.
52610** The page begins at offset *pOffset into the file. The *pOffset
52611** value is increased to the start of the next page in the journal.
52612**
52613** The main rollback journal uses checksums - the statement journal does
52614** not.
52615**
52616** If the page number of the page record read from the (sub-)journal file
52617** is greater than the current value of Pager.dbSize, then playback is
52618** skipped and SQLITE_OK is returned.
52619**
52620** If pDone is not NULL, then it is a record of pages that have already
52621** been played back. If the page at *pOffset has already been played back
52622** (if the corresponding pDone bit is set) then skip the playback.
52623** Make sure the pDone bit corresponding to the *pOffset page is set
52624** prior to returning.
52625**
52626** If the page record is successfully read from the (sub-)journal file
52627** and played back, then SQLITE_OK is returned. If an IO error occurs
52628** while reading the record from the (sub-)journal file or while writing
52629** to the database file, then the IO error code is returned. If data
52630** is successfully read from the (sub-)journal file but appears to be
52631** corrupted, SQLITE_DONE is returned. Data is considered corrupted in
52632** two circumstances:
52633**
52634** * If the record page-number is illegal (0 or PAGER_MJ_PGNO), or
52635** * If the record is being rolled back from the main journal file
52636** and the checksum field does not match the record content.
52637**
52638** Neither of these two scenarios are possible during a savepoint rollback.
52639**
52640** If this is a savepoint rollback, then memory may have to be dynamically
52641** allocated by this function. If this is the case and an allocation fails,
52642** SQLITE_NOMEM is returned.
52643*/
52644static int pager_playback_one_page(
52645 Pager *pPager, /* The pager being played back */
52646 i64 *pOffset, /* Offset of record to playback */
52647 Bitvec *pDone, /* Bitvec of pages already played back */
52648 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
52649 int isSavepnt /* True for a savepoint rollback */
52650){
52651 int rc;
52652 PgHdr *pPg; /* An existing page in the cache */
52653 Pgno pgno; /* The page number of a page in journal */
52654 u32 cksum; /* Checksum used for sanity checking */
52655 char *aData; /* Temporary storage for the page */
52656 sqlite3_file *jfd; /* The file descriptor for the journal file */
52657 int isSynced; /* True if journal page is synced */
52658#ifdef SQLITE_HAS_CODEC
52659 /* The jrnlEnc flag is true if Journal pages should be passed through
52660 ** the codec. It is false for pure in-memory journals. */
52661 const int jrnlEnc = (isMainJrnl || pPager->subjInMemory==0);
52662#endif
52663
52664 assert( (isMainJrnl&~1)==0 ); /* isMainJrnl is 0 or 1 */
52665 assert( (isSavepnt&~1)==0 ); /* isSavepnt is 0 or 1 */
52666 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
52667 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
52668
52669 aData = pPager->pTmpSpace;
52670 assert( aData ); /* Temp storage must have already been allocated */
52671 assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );
52672
52673 /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction
52674 ** or savepoint rollback done at the request of the caller) or this is
52675 ** a hot-journal rollback. If it is a hot-journal rollback, the pager
52676 ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
52677 ** only reads from the main journal, not the sub-journal.
52678 */
52679 assert( pPager->eState>=PAGER_WRITER_CACHEMOD
52680 || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
52681 );
52682 assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
52683
52684 /* Read the page number and page data from the journal or sub-journal
52685 ** file. Return an error code to the caller if an IO error occurs.
52686 */
52687 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
52688 rc = read32bits(jfd, *pOffset, &pgno);
52689 if( rc!=SQLITE_OK ) return rc;
52690 rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
52691 if( rc!=SQLITE_OK ) return rc;
52692 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
52693
52694 /* Sanity checking on the page. This is more important that I originally
52695 ** thought. If a power failure occurs while the journal is being written,
52696 ** it could cause invalid data to be written into the journal. We need to
52697 ** detect this invalid data (with high probability) and ignore it.
52698 */
52699 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
52700 assert( !isSavepnt );
52701 return SQLITE_DONE;
52702 }
52703 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
52704 return SQLITE_OK;
52705 }
52706 if( isMainJrnl ){
52707 rc = read32bits(jfd, (*pOffset)-4, &cksum);
52708 if( rc ) return rc;
52709 if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){
52710 return SQLITE_DONE;
52711 }
52712 }
52713
52714 /* If this page has already been played back before during the current
52715 ** rollback, then don't bother to play it back again.
52716 */
52717 if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){
52718 return rc;
52719 }
52720
52721 /* When playing back page 1, restore the nReserve setting
52722 */
52723 if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
52724 pPager->nReserve = ((u8*)aData)[20];
52725 pagerReportSize(pPager);
52726 }
52727
52728 /* If the pager is in CACHEMOD state, then there must be a copy of this
52729 ** page in the pager cache. In this case just update the pager cache,
52730 ** not the database file. The page is left marked dirty in this case.
52731 **
52732 ** An exception to the above rule: If the database is in no-sync mode
52733 ** and a page is moved during an incremental vacuum then the page may
52734 ** not be in the pager cache. Later: if a malloc() or IO error occurs
52735 ** during a Movepage() call, then the page may not be in the cache
52736 ** either. So the condition described in the above paragraph is not
52737 ** assert()able.
52738 **
52739 ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the
52740 ** pager cache if it exists and the main file. The page is then marked
52741 ** not dirty. Since this code is only executed in PAGER_OPEN state for
52742 ** a hot-journal rollback, it is guaranteed that the page-cache is empty
52743 ** if the pager is in OPEN state.
52744 **
52745 ** Ticket #1171: The statement journal might contain page content that is
52746 ** different from the page content at the start of the transaction.
52747 ** This occurs when a page is changed prior to the start of a statement
52748 ** then changed again within the statement. When rolling back such a
52749 ** statement we must not write to the original database unless we know
52750 ** for certain that original page contents are synced into the main rollback
52751 ** journal. Otherwise, a power loss might leave modified data in the
52752 ** database file without an entry in the rollback journal that can
52753 ** restore the database to its original form. Two conditions must be
52754 ** met before writing to the database files. (1) the database must be
52755 ** locked. (2) we know that the original page content is fully synced
52756 ** in the main journal either because the page is not in cache or else
52757 ** the page is marked as needSync==0.
52758 **
52759 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
52760 ** is possible to fail a statement on a database that does not yet exist.
52761 ** Do not attempt to write if database file has never been opened.
52762 */
52763 if( pagerUseWal(pPager) ){
52764 pPg = 0;
52765 }else{
52766 pPg = sqlite3PagerLookup(pPager, pgno);
52767 }
52768 assert( pPg || !MEMDB );
52769 assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile );
52770 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
52771 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
52772 (isMainJrnl?"main-journal":"sub-journal")
52773 ));
52774 if( isMainJrnl ){
52775 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
52776 }else{
52777 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
52778 }
52779 if( isOpen(pPager->fd)
52780 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
52781 && isSynced
52782 ){
52783 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
52784 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
52785 assert( !pagerUseWal(pPager) );
52786
52787 /* Write the data read from the journal back into the database file.
52788 ** This is usually safe even for an encrypted database - as the data
52789 ** was encrypted before it was written to the journal file. The exception
52790 ** is if the data was just read from an in-memory sub-journal. In that
52791 ** case it must be encrypted here before it is copied into the database
52792 ** file. */
52793#ifdef SQLITE_HAS_CODEC
52794 if( !jrnlEnc ){
52795 CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM_BKPT, aData);
52796 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
52797 CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM_BKPT);
52798 }else
52799#endif
52800 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
52801
52802 if( pgno>pPager->dbFileSize ){
52803 pPager->dbFileSize = pgno;
52804 }
52805 if( pPager->pBackup ){
52806#ifdef SQLITE_HAS_CODEC
52807 if( jrnlEnc ){
52808 CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM_BKPT);
52809 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
52810 CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM_BKPT,aData);
52811 }else
52812#endif
52813 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
52814 }
52815 }else if( !isMainJrnl && pPg==0 ){
52816 /* If this is a rollback of a savepoint and data was not written to
52817 ** the database and the page is not in-memory, there is a potential
52818 ** problem. When the page is next fetched by the b-tree layer, it
52819 ** will be read from the database file, which may or may not be
52820 ** current.
52821 **
52822 ** There are a couple of different ways this can happen. All are quite
52823 ** obscure. When running in synchronous mode, this can only happen
52824 ** if the page is on the free-list at the start of the transaction, then
52825 ** populated, then moved using sqlite3PagerMovepage().
52826 **
52827 ** The solution is to add an in-memory page to the cache containing
52828 ** the data just read from the sub-journal. Mark the page as dirty
52829 ** and if the pager requires a journal-sync, then mark the page as
52830 ** requiring a journal-sync before it is written.
52831 */
52832 assert( isSavepnt );
52833 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
52834 pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
52835 rc = sqlite3PagerGet(pPager, pgno, &pPg, 1);
52836 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
52837 pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
52838 if( rc!=SQLITE_OK ) return rc;
52839 sqlite3PcacheMakeDirty(pPg);
52840 }
52841 if( pPg ){
52842 /* No page should ever be explicitly rolled back that is in use, except
52843 ** for page 1 which is held in use in order to keep the lock on the
52844 ** database active. However such a page may be rolled back as a result
52845 ** of an internal error resulting in an automatic call to
52846 ** sqlite3PagerRollback().
52847 */
52848 void *pData;
52849 pData = pPg->pData;
52850 memcpy(pData, (u8*)aData, pPager->pageSize);
52851 pPager->xReiniter(pPg);
52852 /* It used to be that sqlite3PcacheMakeClean(pPg) was called here. But
52853 ** that call was dangerous and had no detectable benefit since the cache
52854 ** is normally cleaned by sqlite3PcacheCleanAll() after rollback and so
52855 ** has been removed. */
52856 pager_set_pagehash(pPg);
52857
52858 /* If this was page 1, then restore the value of Pager.dbFileVers.
52859 ** Do this before any decoding. */
52860 if( pgno==1 ){
52861 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
52862 }
52863
52864 /* Decode the page just read from disk */
52865#if SQLITE_HAS_CODEC
52866 if( jrnlEnc ){ CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM_BKPT); }
52867#endif
52868 sqlite3PcacheRelease(pPg);
52869 }
52870 return rc;
52871}
52872
52873/*
52874** Parameter zMaster is the name of a master journal file. A single journal
52875** file that referred to the master journal file has just been rolled back.
52876** This routine checks if it is possible to delete the master journal file,
52877** and does so if it is.
52878**
52879** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
52880** available for use within this function.
52881**
52882** When a master journal file is created, it is populated with the names
52883** of all of its child journals, one after another, formatted as utf-8
52884** encoded text. The end of each child journal file is marked with a
52885** nul-terminator byte (0x00). i.e. the entire contents of a master journal
52886** file for a transaction involving two databases might be:
52887**
52888** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
52889**
52890** A master journal file may only be deleted once all of its child
52891** journals have been rolled back.
52892**
52893** This function reads the contents of the master-journal file into
52894** memory and loops through each of the child journal names. For
52895** each child journal, it checks if:
52896**
52897** * if the child journal exists, and if so
52898** * if the child journal contains a reference to master journal
52899** file zMaster
52900**
52901** If a child journal can be found that matches both of the criteria
52902** above, this function returns without doing anything. Otherwise, if
52903** no such child journal can be found, file zMaster is deleted from
52904** the file-system using sqlite3OsDelete().
52905**
52906** If an IO error within this function, an error code is returned. This
52907** function allocates memory by calling sqlite3Malloc(). If an allocation
52908** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors
52909** occur, SQLITE_OK is returned.
52910**
52911** TODO: This function allocates a single block of memory to load
52912** the entire contents of the master journal file. This could be
52913** a couple of kilobytes or so - potentially larger than the page
52914** size.
52915*/
52916static int pager_delmaster(Pager *pPager, const char *zMaster){
52917 sqlite3_vfs *pVfs = pPager->pVfs;
52918 int rc; /* Return code */
52919 sqlite3_file *pMaster; /* Malloc'd master-journal file descriptor */
52920 sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
52921 char *zMasterJournal = 0; /* Contents of master journal file */
52922 i64 nMasterJournal; /* Size of master journal file */
52923 char *zJournal; /* Pointer to one journal within MJ file */
52924 char *zMasterPtr; /* Space to hold MJ filename from a journal file */
52925 int nMasterPtr; /* Amount of space allocated to zMasterPtr[] */
52926
52927 /* Allocate space for both the pJournal and pMaster file descriptors.
52928 ** If successful, open the master journal file for reading.
52929 */
52930 pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
52931 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
52932 if( !pMaster ){
52933 rc = SQLITE_NOMEM_BKPT;
52934 }else{
52935 const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
52936 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
52937 }
52938 if( rc!=SQLITE_OK ) goto delmaster_out;
52939
52940 /* Load the entire master journal file into space obtained from
52941 ** sqlite3_malloc() and pointed to by zMasterJournal. Also obtain
52942 ** sufficient space (in zMasterPtr) to hold the names of master
52943 ** journal files extracted from regular rollback-journals.
52944 */
52945 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
52946 if( rc!=SQLITE_OK ) goto delmaster_out;
52947 nMasterPtr = pVfs->mxPathname+1;
52948 zMasterJournal = sqlite3Malloc(nMasterJournal + nMasterPtr + 1);
52949 if( !zMasterJournal ){
52950 rc = SQLITE_NOMEM_BKPT;
52951 goto delmaster_out;
52952 }
52953 zMasterPtr = &zMasterJournal[nMasterJournal+1];
52954 rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
52955 if( rc!=SQLITE_OK ) goto delmaster_out;
52956 zMasterJournal[nMasterJournal] = 0;
52957
52958 zJournal = zMasterJournal;
52959 while( (zJournal-zMasterJournal)<nMasterJournal ){
52960 int exists;
52961 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
52962 if( rc!=SQLITE_OK ){
52963 goto delmaster_out;
52964 }
52965 if( exists ){
52966 /* One of the journals pointed to by the master journal exists.
52967 ** Open it and check if it points at the master journal. If
52968 ** so, return without deleting the master journal file.
52969 */
52970 int c;
52971 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
52972 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
52973 if( rc!=SQLITE_OK ){
52974 goto delmaster_out;
52975 }
52976
52977 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
52978 sqlite3OsClose(pJournal);
52979 if( rc!=SQLITE_OK ){
52980 goto delmaster_out;
52981 }
52982
52983 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
52984 if( c ){
52985 /* We have a match. Do not delete the master journal file. */
52986 goto delmaster_out;
52987 }
52988 }
52989 zJournal += (sqlite3Strlen30(zJournal)+1);
52990 }
52991
52992 sqlite3OsClose(pMaster);
52993 rc = sqlite3OsDelete(pVfs, zMaster, 0);
52994
52995delmaster_out:
52996 sqlite3_free(zMasterJournal);
52997 if( pMaster ){
52998 sqlite3OsClose(pMaster);
52999 assert( !isOpen(pJournal) );
53000 sqlite3_free(pMaster);
53001 }
53002 return rc;
53003}
53004
53005
53006/*
53007** This function is used to change the actual size of the database
53008** file in the file-system. This only happens when committing a transaction,
53009** or rolling back a transaction (including rolling back a hot-journal).
53010**
53011** If the main database file is not open, or the pager is not in either
53012** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
53013** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
53014** If the file on disk is currently larger than nPage pages, then use the VFS
53015** xTruncate() method to truncate it.
53016**
53017** Or, it might be the case that the file on disk is smaller than
53018** nPage pages. Some operating system implementations can get confused if
53019** you try to truncate a file to some size that is larger than it
53020** currently is, so detect this case and write a single zero byte to
53021** the end of the new file instead.
53022**
53023** If successful, return SQLITE_OK. If an IO error occurs while modifying
53024** the database file, return the error code to the caller.
53025*/
53026static int pager_truncate(Pager *pPager, Pgno nPage){
53027 int rc = SQLITE_OK;
53028 assert( pPager->eState!=PAGER_ERROR );
53029 assert( pPager->eState!=PAGER_READER );
53030
53031 if( isOpen(pPager->fd)
53032 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
53033 ){
53034 i64 currentSize, newSize;
53035 int szPage = pPager->pageSize;
53036 assert( pPager->eLock==EXCLUSIVE_LOCK );
53037 /* TODO: Is it safe to use Pager.dbFileSize here? */
53038 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
53039 newSize = szPage*(i64)nPage;
53040 if( rc==SQLITE_OK && currentSize!=newSize ){
53041 if( currentSize>newSize ){
53042 rc = sqlite3OsTruncate(pPager->fd, newSize);
53043 }else if( (currentSize+szPage)<=newSize ){
53044 char *pTmp = pPager->pTmpSpace;
53045 memset(pTmp, 0, szPage);
53046 testcase( (newSize-szPage) == currentSize );
53047 testcase( (newSize-szPage) > currentSize );
53048 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
53049 }
53050 if( rc==SQLITE_OK ){
53051 pPager->dbFileSize = nPage;
53052 }
53053 }
53054 }
53055 return rc;
53056}
53057
53058/*
53059** Return a sanitized version of the sector-size of OS file pFile. The
53060** return value is guaranteed to lie between 32 and MAX_SECTOR_SIZE.
53061*/
53062SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *pFile){
53063 int iRet = sqlite3OsSectorSize(pFile);
53064 if( iRet<32 ){
53065 iRet = 512;
53066 }else if( iRet>MAX_SECTOR_SIZE ){
53067 assert( MAX_SECTOR_SIZE>=512 );
53068 iRet = MAX_SECTOR_SIZE;
53069 }
53070 return iRet;
53071}
53072
53073/*
53074** Set the value of the Pager.sectorSize variable for the given
53075** pager based on the value returned by the xSectorSize method
53076** of the open database file. The sector size will be used
53077** to determine the size and alignment of journal header and
53078** master journal pointers within created journal files.
53079**
53080** For temporary files the effective sector size is always 512 bytes.
53081**
53082** Otherwise, for non-temporary files, the effective sector size is
53083** the value returned by the xSectorSize() method rounded up to 32 if
53084** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it
53085** is greater than MAX_SECTOR_SIZE.
53086**
53087** If the file has the SQLITE_IOCAP_POWERSAFE_OVERWRITE property, then set
53088** the effective sector size to its minimum value (512). The purpose of
53089** pPager->sectorSize is to define the "blast radius" of bytes that
53090** might change if a crash occurs while writing to a single byte in
53091** that range. But with POWERSAFE_OVERWRITE, the blast radius is zero
53092** (that is what POWERSAFE_OVERWRITE means), so we minimize the sector
53093** size. For backwards compatibility of the rollback journal file format,
53094** we cannot reduce the effective sector size below 512.
53095*/
53096static void setSectorSize(Pager *pPager){
53097 assert( isOpen(pPager->fd) || pPager->tempFile );
53098
53099 if( pPager->tempFile
53100 || (sqlite3OsDeviceCharacteristics(pPager->fd) &
53101 SQLITE_IOCAP_POWERSAFE_OVERWRITE)!=0
53102 ){
53103 /* Sector size doesn't matter for temporary files. Also, the file
53104 ** may not have been opened yet, in which case the OsSectorSize()
53105 ** call will segfault. */
53106 pPager->sectorSize = 512;
53107 }else{
53108 pPager->sectorSize = sqlite3SectorSize(pPager->fd);
53109 }
53110}
53111
53112/*
53113** Playback the journal and thus restore the database file to
53114** the state it was in before we started making changes.
53115**
53116** The journal file format is as follows:
53117**
53118** (1) 8 byte prefix. A copy of aJournalMagic[].
53119** (2) 4 byte big-endian integer which is the number of valid page records
53120** in the journal. If this value is 0xffffffff, then compute the
53121** number of page records from the journal size.
53122** (3) 4 byte big-endian integer which is the initial value for the
53123** sanity checksum.
53124** (4) 4 byte integer which is the number of pages to truncate the
53125** database to during a rollback.
53126** (5) 4 byte big-endian integer which is the sector size. The header
53127** is this many bytes in size.
53128** (6) 4 byte big-endian integer which is the page size.
53129** (7) zero padding out to the next sector size.
53130** (8) Zero or more pages instances, each as follows:
53131** + 4 byte page number.
53132** + pPager->pageSize bytes of data.
53133** + 4 byte checksum
53134**
53135** When we speak of the journal header, we mean the first 7 items above.
53136** Each entry in the journal is an instance of the 8th item.
53137**
53138** Call the value from the second bullet "nRec". nRec is the number of
53139** valid page entries in the journal. In most cases, you can compute the
53140** value of nRec from the size of the journal file. But if a power
53141** failure occurred while the journal was being written, it could be the
53142** case that the size of the journal file had already been increased but
53143** the extra entries had not yet made it safely to disk. In such a case,
53144** the value of nRec computed from the file size would be too large. For
53145** that reason, we always use the nRec value in the header.
53146**
53147** If the nRec value is 0xffffffff it means that nRec should be computed
53148** from the file size. This value is used when the user selects the
53149** no-sync option for the journal. A power failure could lead to corruption
53150** in this case. But for things like temporary table (which will be
53151** deleted when the power is restored) we don't care.
53152**
53153** If the file opened as the journal file is not a well-formed
53154** journal file then all pages up to the first corrupted page are rolled
53155** back (or no pages if the journal header is corrupted). The journal file
53156** is then deleted and SQLITE_OK returned, just as if no corruption had
53157** been encountered.
53158**
53159** If an I/O or malloc() error occurs, the journal-file is not deleted
53160** and an error code is returned.
53161**
53162** The isHot parameter indicates that we are trying to rollback a journal
53163** that might be a hot journal. Or, it could be that the journal is
53164** preserved because of JOURNALMODE_PERSIST or JOURNALMODE_TRUNCATE.
53165** If the journal really is hot, reset the pager cache prior rolling
53166** back any content. If the journal is merely persistent, no reset is
53167** needed.
53168*/
53169static int pager_playback(Pager *pPager, int isHot){
53170 sqlite3_vfs *pVfs = pPager->pVfs;
53171 i64 szJ; /* Size of the journal file in bytes */
53172 u32 nRec; /* Number of Records in the journal */
53173 u32 u; /* Unsigned loop counter */
53174 Pgno mxPg = 0; /* Size of the original file in pages */
53175 int rc; /* Result code of a subroutine */
53176 int res = 1; /* Value returned by sqlite3OsAccess() */
53177 char *zMaster = 0; /* Name of master journal file if any */
53178 int needPagerReset; /* True to reset page prior to first page rollback */
53179 int nPlayback = 0; /* Total number of pages restored from journal */
53180 u32 savedPageSize = pPager->pageSize;
53181
53182 /* Figure out how many records are in the journal. Abort early if
53183 ** the journal is empty.
53184 */
53185 assert( isOpen(pPager->jfd) );
53186 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
53187 if( rc!=SQLITE_OK ){
53188 goto end_playback;
53189 }
53190
53191 /* Read the master journal name from the journal, if it is present.
53192 ** If a master journal file name is specified, but the file is not
53193 ** present on disk, then the journal is not hot and does not need to be
53194 ** played back.
53195 **
53196 ** TODO: Technically the following is an error because it assumes that
53197 ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
53198 ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
53199 ** mxPathname is 512, which is the same as the minimum allowable value
53200 ** for pageSize.
53201 */
53202 zMaster = pPager->pTmpSpace;
53203 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
53204 if( rc==SQLITE_OK && zMaster[0] ){
53205 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
53206 }
53207 zMaster = 0;
53208 if( rc!=SQLITE_OK || !res ){
53209 goto end_playback;
53210 }
53211 pPager->journalOff = 0;
53212 needPagerReset = isHot;
53213
53214 /* This loop terminates either when a readJournalHdr() or
53215 ** pager_playback_one_page() call returns SQLITE_DONE or an IO error
53216 ** occurs.
53217 */
53218 while( 1 ){
53219 /* Read the next journal header from the journal file. If there are
53220 ** not enough bytes left in the journal file for a complete header, or
53221 ** it is corrupted, then a process must have failed while writing it.
53222 ** This indicates nothing more needs to be rolled back.
53223 */
53224 rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
53225 if( rc!=SQLITE_OK ){
53226 if( rc==SQLITE_DONE ){
53227 rc = SQLITE_OK;
53228 }
53229 goto end_playback;
53230 }
53231
53232 /* If nRec is 0xffffffff, then this journal was created by a process
53233 ** working in no-sync mode. This means that the rest of the journal
53234 ** file consists of pages, there are no more journal headers. Compute
53235 ** the value of nRec based on this assumption.
53236 */
53237 if( nRec==0xffffffff ){
53238 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
53239 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
53240 }
53241
53242 /* If nRec is 0 and this rollback is of a transaction created by this
53243 ** process and if this is the final header in the journal, then it means
53244 ** that this part of the journal was being filled but has not yet been
53245 ** synced to disk. Compute the number of pages based on the remaining
53246 ** size of the file.
53247 **
53248 ** The third term of the test was added to fix ticket #2565.
53249 ** When rolling back a hot journal, nRec==0 always means that the next
53250 ** chunk of the journal contains zero pages to be rolled back. But
53251 ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
53252 ** the journal, it means that the journal might contain additional
53253 ** pages that need to be rolled back and that the number of pages
53254 ** should be computed based on the journal file size.
53255 */
53256 if( nRec==0 && !isHot &&
53257 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
53258 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
53259 }
53260
53261 /* If this is the first header read from the journal, truncate the
53262 ** database file back to its original size.
53263 */
53264 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
53265 rc = pager_truncate(pPager, mxPg);
53266 if( rc!=SQLITE_OK ){
53267 goto end_playback;
53268 }
53269 pPager->dbSize = mxPg;
53270 }
53271
53272 /* Copy original pages out of the journal and back into the
53273 ** database file and/or page cache.
53274 */
53275 for(u=0; u<nRec; u++){
53276 if( needPagerReset ){
53277 pager_reset(pPager);
53278 needPagerReset = 0;
53279 }
53280 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
53281 if( rc==SQLITE_OK ){
53282 nPlayback++;
53283 }else{
53284 if( rc==SQLITE_DONE ){
53285 pPager->journalOff = szJ;
53286 break;
53287 }else if( rc==SQLITE_IOERR_SHORT_READ ){
53288 /* If the journal has been truncated, simply stop reading and
53289 ** processing the journal. This might happen if the journal was
53290 ** not completely written and synced prior to a crash. In that
53291 ** case, the database should have never been written in the
53292 ** first place so it is OK to simply abandon the rollback. */
53293 rc = SQLITE_OK;
53294 goto end_playback;
53295 }else{
53296 /* If we are unable to rollback, quit and return the error
53297 ** code. This will cause the pager to enter the error state
53298 ** so that no further harm will be done. Perhaps the next
53299 ** process to come along will be able to rollback the database.
53300 */
53301 goto end_playback;
53302 }
53303 }
53304 }
53305 }
53306 /*NOTREACHED*/
53307 assert( 0 );
53308
53309end_playback:
53310 if( rc==SQLITE_OK ){
53311 rc = sqlite3PagerSetPagesize(pPager, &savedPageSize, -1);
53312 }
53313 /* Following a rollback, the database file should be back in its original
53314 ** state prior to the start of the transaction, so invoke the
53315 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
53316 ** assertion that the transaction counter was modified.
53317 */
53318#ifdef SQLITE_DEBUG
53319 sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
53320#endif
53321
53322 /* If this playback is happening automatically as a result of an IO or
53323 ** malloc error that occurred after the change-counter was updated but
53324 ** before the transaction was committed, then the change-counter
53325 ** modification may just have been reverted. If this happens in exclusive
53326 ** mode, then subsequent transactions performed by the connection will not
53327 ** update the change-counter at all. This may lead to cache inconsistency
53328 ** problems for other processes at some point in the future. So, just
53329 ** in case this has happened, clear the changeCountDone flag now.
53330 */
53331 pPager->changeCountDone = pPager->tempFile;
53332
53333 if( rc==SQLITE_OK ){
53334 zMaster = pPager->pTmpSpace;
53335 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
53336 testcase( rc!=SQLITE_OK );
53337 }
53338 if( rc==SQLITE_OK
53339 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
53340 ){
53341 rc = sqlite3PagerSync(pPager, 0);
53342 }
53343 if( rc==SQLITE_OK ){
53344 rc = pager_end_transaction(pPager, zMaster[0]!='\0', 0);
53345 testcase( rc!=SQLITE_OK );
53346 }
53347 if( rc==SQLITE_OK && zMaster[0] && res ){
53348 /* If there was a master journal and this routine will return success,
53349 ** see if it is possible to delete the master journal.
53350 */
53351 rc = pager_delmaster(pPager, zMaster);
53352 testcase( rc!=SQLITE_OK );
53353 }
53354 if( isHot && nPlayback ){
53355 sqlite3_log(SQLITE_NOTICE_RECOVER_ROLLBACK, "recovered %d pages from %s",
53356 nPlayback, pPager->zJournal);
53357 }
53358
53359 /* The Pager.sectorSize variable may have been updated while rolling
53360 ** back a journal created by a process with a different sector size
53361 ** value. Reset it to the correct value for this process.
53362 */
53363 setSectorSize(pPager);
53364 return rc;
53365}
53366
53367
53368/*
53369** Read the content for page pPg out of the database file (or out of
53370** the WAL if that is where the most recent copy if found) into
53371** pPg->pData. A shared lock or greater must be held on the database
53372** file before this function is called.
53373**
53374** If page 1 is read, then the value of Pager.dbFileVers[] is set to
53375** the value read from the database file.
53376**
53377** If an IO error occurs, then the IO error is returned to the caller.
53378** Otherwise, SQLITE_OK is returned.
53379*/
53380static int readDbPage(PgHdr *pPg){
53381 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
53382 int rc = SQLITE_OK; /* Return code */
53383
53384#ifndef SQLITE_OMIT_WAL
53385 u32 iFrame = 0; /* Frame of WAL containing pgno */
53386
53387 assert( pPager->eState>=PAGER_READER && !MEMDB );
53388 assert( isOpen(pPager->fd) );
53389
53390 if( pagerUseWal(pPager) ){
53391 rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
53392 if( rc ) return rc;
53393 }
53394 if( iFrame ){
53395 rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData);
53396 }else
53397#endif
53398 {
53399 i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize;
53400 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
53401 if( rc==SQLITE_IOERR_SHORT_READ ){
53402 rc = SQLITE_OK;
53403 }
53404 }
53405
53406 if( pPg->pgno==1 ){
53407 if( rc ){
53408 /* If the read is unsuccessful, set the dbFileVers[] to something
53409 ** that will never be a valid file version. dbFileVers[] is a copy
53410 ** of bytes 24..39 of the database. Bytes 28..31 should always be
53411 ** zero or the size of the database in page. Bytes 32..35 and 35..39
53412 ** should be page numbers which are never 0xffffffff. So filling
53413 ** pPager->dbFileVers[] with all 0xff bytes should suffice.
53414 **
53415 ** For an encrypted database, the situation is more complex: bytes
53416 ** 24..39 of the database are white noise. But the probability of
53417 ** white noise equaling 16 bytes of 0xff is vanishingly small so
53418 ** we should still be ok.
53419 */
53420 memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
53421 }else{
53422 u8 *dbFileVers = &((u8*)pPg->pData)[24];
53423 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
53424 }
53425 }
53426 CODEC1(pPager, pPg->pData, pPg->pgno, 3, rc = SQLITE_NOMEM_BKPT);
53427
53428 PAGER_INCR(sqlite3_pager_readdb_count);
53429 PAGER_INCR(pPager->nRead);
53430 IOTRACE(("PGIN %p %d\n", pPager, pPg->pgno));
53431 PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
53432 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
53433
53434 return rc;
53435}
53436
53437/*
53438** Update the value of the change-counter at offsets 24 and 92 in
53439** the header and the sqlite version number at offset 96.
53440**
53441** This is an unconditional update. See also the pager_incr_changecounter()
53442** routine which only updates the change-counter if the update is actually
53443** needed, as determined by the pPager->changeCountDone state variable.
53444*/
53445static void pager_write_changecounter(PgHdr *pPg){
53446 u32 change_counter;
53447
53448 /* Increment the value just read and write it back to byte 24. */
53449 change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
53450 put32bits(((char*)pPg->pData)+24, change_counter);
53451
53452 /* Also store the SQLite version number in bytes 96..99 and in
53453 ** bytes 92..95 store the change counter for which the version number
53454 ** is valid. */
53455 put32bits(((char*)pPg->pData)+92, change_counter);
53456 put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
53457}
53458
53459#ifndef SQLITE_OMIT_WAL
53460/*
53461** This function is invoked once for each page that has already been
53462** written into the log file when a WAL transaction is rolled back.
53463** Parameter iPg is the page number of said page. The pCtx argument
53464** is actually a pointer to the Pager structure.
53465**
53466** If page iPg is present in the cache, and has no outstanding references,
53467** it is discarded. Otherwise, if there are one or more outstanding
53468** references, the page content is reloaded from the database. If the
53469** attempt to reload content from the database is required and fails,
53470** return an SQLite error code. Otherwise, SQLITE_OK.
53471*/
53472static int pagerUndoCallback(void *pCtx, Pgno iPg){
53473 int rc = SQLITE_OK;
53474 Pager *pPager = (Pager *)pCtx;
53475 PgHdr *pPg;
53476
53477 assert( pagerUseWal(pPager) );
53478 pPg = sqlite3PagerLookup(pPager, iPg);
53479 if( pPg ){
53480 if( sqlite3PcachePageRefcount(pPg)==1 ){
53481 sqlite3PcacheDrop(pPg);
53482 }else{
53483 rc = readDbPage(pPg);
53484 if( rc==SQLITE_OK ){
53485 pPager->xReiniter(pPg);
53486 }
53487 sqlite3PagerUnrefNotNull(pPg);
53488 }
53489 }
53490
53491 /* Normally, if a transaction is rolled back, any backup processes are
53492 ** updated as data is copied out of the rollback journal and into the
53493 ** database. This is not generally possible with a WAL database, as
53494 ** rollback involves simply truncating the log file. Therefore, if one
53495 ** or more frames have already been written to the log (and therefore
53496 ** also copied into the backup databases) as part of this transaction,
53497 ** the backups must be restarted.
53498 */
53499 sqlite3BackupRestart(pPager->pBackup);
53500
53501 return rc;
53502}
53503
53504/*
53505** This function is called to rollback a transaction on a WAL database.
53506*/
53507static int pagerRollbackWal(Pager *pPager){
53508 int rc; /* Return Code */
53509 PgHdr *pList; /* List of dirty pages to revert */
53510
53511 /* For all pages in the cache that are currently dirty or have already
53512 ** been written (but not committed) to the log file, do one of the
53513 ** following:
53514 **
53515 ** + Discard the cached page (if refcount==0), or
53516 ** + Reload page content from the database (if refcount>0).
53517 */
53518 pPager->dbSize = pPager->dbOrigSize;
53519 rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
53520 pList = sqlite3PcacheDirtyList(pPager->pPCache);
53521 while( pList && rc==SQLITE_OK ){
53522 PgHdr *pNext = pList->pDirty;
53523 rc = pagerUndoCallback((void *)pPager, pList->pgno);
53524 pList = pNext;
53525 }
53526
53527 return rc;
53528}
53529
53530/*
53531** This function is a wrapper around sqlite3WalFrames(). As well as logging
53532** the contents of the list of pages headed by pList (connected by pDirty),
53533** this function notifies any active backup processes that the pages have
53534** changed.
53535**
53536** The list of pages passed into this routine is always sorted by page number.
53537** Hence, if page 1 appears anywhere on the list, it will be the first page.
53538*/
53539static int pagerWalFrames(
53540 Pager *pPager, /* Pager object */
53541 PgHdr *pList, /* List of frames to log */
53542 Pgno nTruncate, /* Database size after this commit */
53543 int isCommit /* True if this is a commit */
53544){
53545 int rc; /* Return code */
53546 int nList; /* Number of pages in pList */
53547 PgHdr *p; /* For looping over pages */
53548
53549 assert( pPager->pWal );
53550 assert( pList );
53551#ifdef SQLITE_DEBUG
53552 /* Verify that the page list is in accending order */
53553 for(p=pList; p && p->pDirty; p=p->pDirty){
53554 assert( p->pgno < p->pDirty->pgno );
53555 }
53556#endif
53557
53558 assert( pList->pDirty==0 || isCommit );
53559 if( isCommit ){
53560 /* If a WAL transaction is being committed, there is no point in writing
53561 ** any pages with page numbers greater than nTruncate into the WAL file.
53562 ** They will never be read by any client. So remove them from the pDirty
53563 ** list here. */
53564 PgHdr **ppNext = &pList;
53565 nList = 0;
53566 for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
53567 if( p->pgno<=nTruncate ){
53568 ppNext = &p->pDirty;
53569 nList++;
53570 }
53571 }
53572 assert( pList );
53573 }else{
53574 nList = 1;
53575 }
53576 pPager->aStat[PAGER_STAT_WRITE] += nList;
53577
53578 if( pList->pgno==1 ) pager_write_changecounter(pList);
53579 rc = sqlite3WalFrames(pPager->pWal,
53580 pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
53581 );
53582 if( rc==SQLITE_OK && pPager->pBackup ){
53583 for(p=pList; p; p=p->pDirty){
53584 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
53585 }
53586 }
53587
53588#ifdef SQLITE_CHECK_PAGES
53589 pList = sqlite3PcacheDirtyList(pPager->pPCache);
53590 for(p=pList; p; p=p->pDirty){
53591 pager_set_pagehash(p);
53592 }
53593#endif
53594
53595 return rc;
53596}
53597
53598/*
53599** Begin a read transaction on the WAL.
53600**
53601** This routine used to be called "pagerOpenSnapshot()" because it essentially
53602** makes a snapshot of the database at the current point in time and preserves
53603** that snapshot for use by the reader in spite of concurrently changes by
53604** other writers or checkpointers.
53605*/
53606static int pagerBeginReadTransaction(Pager *pPager){
53607 int rc; /* Return code */
53608 int changed = 0; /* True if cache must be reset */
53609
53610 assert( pagerUseWal(pPager) );
53611 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
53612
53613 /* sqlite3WalEndReadTransaction() was not called for the previous
53614 ** transaction in locking_mode=EXCLUSIVE. So call it now. If we
53615 ** are in locking_mode=NORMAL and EndRead() was previously called,
53616 ** the duplicate call is harmless.
53617 */
53618 sqlite3WalEndReadTransaction(pPager->pWal);
53619
53620 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
53621 if( rc!=SQLITE_OK || changed ){
53622 pager_reset(pPager);
53623 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
53624 }
53625
53626 return rc;
53627}
53628#endif
53629
53630/*
53631** This function is called as part of the transition from PAGER_OPEN
53632** to PAGER_READER state to determine the size of the database file
53633** in pages (assuming the page size currently stored in Pager.pageSize).
53634**
53635** If no error occurs, SQLITE_OK is returned and the size of the database
53636** in pages is stored in *pnPage. Otherwise, an error code (perhaps
53637** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified.
53638*/
53639static int pagerPagecount(Pager *pPager, Pgno *pnPage){
53640 Pgno nPage; /* Value to return via *pnPage */
53641
53642 /* Query the WAL sub-system for the database size. The WalDbsize()
53643 ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or
53644 ** if the database size is not available. The database size is not
53645 ** available from the WAL sub-system if the log file is empty or
53646 ** contains no valid committed transactions.
53647 */
53648 assert( pPager->eState==PAGER_OPEN );
53649 assert( pPager->eLock>=SHARED_LOCK );
53650 assert( isOpen(pPager->fd) );
53651 assert( pPager->tempFile==0 );
53652 nPage = sqlite3WalDbsize(pPager->pWal);
53653
53654 /* If the number of pages in the database is not available from the
53655 ** WAL sub-system, determine the page count based on the size of
53656 ** the database file. If the size of the database file is not an
53657 ** integer multiple of the page-size, round up the result.
53658 */
53659 if( nPage==0 && ALWAYS(isOpen(pPager->fd)) ){
53660 i64 n = 0; /* Size of db file in bytes */
53661 int rc = sqlite3OsFileSize(pPager->fd, &n);
53662 if( rc!=SQLITE_OK ){
53663 return rc;
53664 }
53665 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
53666 }
53667
53668 /* If the current number of pages in the file is greater than the
53669 ** configured maximum pager number, increase the allowed limit so
53670 ** that the file can be read.
53671 */
53672 if( nPage>pPager->mxPgno ){
53673 pPager->mxPgno = (Pgno)nPage;
53674 }
53675
53676 *pnPage = nPage;
53677 return SQLITE_OK;
53678}
53679
53680#ifndef SQLITE_OMIT_WAL
53681/*
53682** Check if the *-wal file that corresponds to the database opened by pPager
53683** exists if the database is not empy, or verify that the *-wal file does
53684** not exist (by deleting it) if the database file is empty.
53685**
53686** If the database is not empty and the *-wal file exists, open the pager
53687** in WAL mode. If the database is empty or if no *-wal file exists and
53688** if no error occurs, make sure Pager.journalMode is not set to
53689** PAGER_JOURNALMODE_WAL.
53690**
53691** Return SQLITE_OK or an error code.
53692**
53693** The caller must hold a SHARED lock on the database file to call this
53694** function. Because an EXCLUSIVE lock on the db file is required to delete
53695** a WAL on a none-empty database, this ensures there is no race condition
53696** between the xAccess() below and an xDelete() being executed by some
53697** other connection.
53698*/
53699static int pagerOpenWalIfPresent(Pager *pPager){
53700 int rc = SQLITE_OK;
53701 assert( pPager->eState==PAGER_OPEN );
53702 assert( pPager->eLock>=SHARED_LOCK );
53703
53704 if( !pPager->tempFile ){
53705 int isWal; /* True if WAL file exists */
53706 rc = sqlite3OsAccess(
53707 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
53708 );
53709 if( rc==SQLITE_OK ){
53710 if( isWal ){
53711 Pgno nPage; /* Size of the database file */
53712
53713 rc = pagerPagecount(pPager, &nPage);
53714 if( rc ) return rc;
53715 if( nPage==0 ){
53716 rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
53717 }else{
53718 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
53719 rc = sqlite3PagerOpenWal(pPager, 0);
53720 }
53721 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
53722 pPager->journalMode = PAGER_JOURNALMODE_DELETE;
53723 }
53724 }
53725 }
53726 return rc;
53727}
53728#endif
53729
53730/*
53731** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
53732** the entire master journal file. The case pSavepoint==NULL occurs when
53733** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction
53734** savepoint.
53735**
53736** When pSavepoint is not NULL (meaning a non-transaction savepoint is
53737** being rolled back), then the rollback consists of up to three stages,
53738** performed in the order specified:
53739**
53740** * Pages are played back from the main journal starting at byte
53741** offset PagerSavepoint.iOffset and continuing to
53742** PagerSavepoint.iHdrOffset, or to the end of the main journal
53743** file if PagerSavepoint.iHdrOffset is zero.
53744**
53745** * If PagerSavepoint.iHdrOffset is not zero, then pages are played
53746** back starting from the journal header immediately following
53747** PagerSavepoint.iHdrOffset to the end of the main journal file.
53748**
53749** * Pages are then played back from the sub-journal file, starting
53750** with the PagerSavepoint.iSubRec and continuing to the end of
53751** the journal file.
53752**
53753** Throughout the rollback process, each time a page is rolled back, the
53754** corresponding bit is set in a bitvec structure (variable pDone in the
53755** implementation below). This is used to ensure that a page is only
53756** rolled back the first time it is encountered in either journal.
53757**
53758** If pSavepoint is NULL, then pages are only played back from the main
53759** journal file. There is no need for a bitvec in this case.
53760**
53761** In either case, before playback commences the Pager.dbSize variable
53762** is reset to the value that it held at the start of the savepoint
53763** (or transaction). No page with a page-number greater than this value
53764** is played back. If one is encountered it is simply skipped.
53765*/
53766static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
53767 i64 szJ; /* Effective size of the main journal */
53768 i64 iHdrOff; /* End of first segment of main-journal records */
53769 int rc = SQLITE_OK; /* Return code */
53770 Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */
53771
53772 assert( pPager->eState!=PAGER_ERROR );
53773 assert( pPager->eState>=PAGER_WRITER_LOCKED );
53774
53775 /* Allocate a bitvec to use to store the set of pages rolled back */
53776 if( pSavepoint ){
53777 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
53778 if( !pDone ){
53779 return SQLITE_NOMEM_BKPT;
53780 }
53781 }
53782
53783 /* Set the database size back to the value it was before the savepoint
53784 ** being reverted was opened.
53785 */
53786 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
53787 pPager->changeCountDone = pPager->tempFile;
53788
53789 if( !pSavepoint && pagerUseWal(pPager) ){
53790 return pagerRollbackWal(pPager);
53791 }
53792
53793 /* Use pPager->journalOff as the effective size of the main rollback
53794 ** journal. The actual file might be larger than this in
53795 ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST. But anything
53796 ** past pPager->journalOff is off-limits to us.
53797 */
53798 szJ = pPager->journalOff;
53799 assert( pagerUseWal(pPager)==0 || szJ==0 );
53800
53801 /* Begin by rolling back records from the main journal starting at
53802 ** PagerSavepoint.iOffset and continuing to the next journal header.
53803 ** There might be records in the main journal that have a page number
53804 ** greater than the current database size (pPager->dbSize) but those
53805 ** will be skipped automatically. Pages are added to pDone as they
53806 ** are played back.
53807 */
53808 if( pSavepoint && !pagerUseWal(pPager) ){
53809 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
53810 pPager->journalOff = pSavepoint->iOffset;
53811 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
53812 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
53813 }
53814 assert( rc!=SQLITE_DONE );
53815 }else{
53816 pPager->journalOff = 0;
53817 }
53818
53819 /* Continue rolling back records out of the main journal starting at
53820 ** the first journal header seen and continuing until the effective end
53821 ** of the main journal file. Continue to skip out-of-range pages and
53822 ** continue adding pages rolled back to pDone.
53823 */
53824 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
53825 u32 ii; /* Loop counter */
53826 u32 nJRec = 0; /* Number of Journal Records */
53827 u32 dummy;
53828 rc = readJournalHdr(pPager, 0, szJ, &nJRec, &dummy);
53829 assert( rc!=SQLITE_DONE );
53830
53831 /*
53832 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
53833 ** test is related to ticket #2565. See the discussion in the
53834 ** pager_playback() function for additional information.
53835 */
53836 if( nJRec==0
53837 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
53838 ){
53839 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
53840 }
53841 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
53842 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
53843 }
53844 assert( rc!=SQLITE_DONE );
53845 }
53846 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
53847
53848 /* Finally, rollback pages from the sub-journal. Page that were
53849 ** previously rolled back out of the main journal (and are hence in pDone)
53850 ** will be skipped. Out-of-range pages are also skipped.
53851 */
53852 if( pSavepoint ){
53853 u32 ii; /* Loop counter */
53854 i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
53855
53856 if( pagerUseWal(pPager) ){
53857 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
53858 }
53859 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
53860 assert( offset==(i64)ii*(4+pPager->pageSize) );
53861 rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
53862 }
53863 assert( rc!=SQLITE_DONE );
53864 }
53865
53866 sqlite3BitvecDestroy(pDone);
53867 if( rc==SQLITE_OK ){
53868 pPager->journalOff = szJ;
53869 }
53870
53871 return rc;
53872}
53873
53874/*
53875** Change the maximum number of in-memory pages that are allowed
53876** before attempting to recycle clean and unused pages.
53877*/
53878SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
53879 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
53880}
53881
53882/*
53883** Change the maximum number of in-memory pages that are allowed
53884** before attempting to spill pages to journal.
53885*/
53886SQLITE_PRIVATE int sqlite3PagerSetSpillsize(Pager *pPager, int mxPage){
53887 return sqlite3PcacheSetSpillsize(pPager->pPCache, mxPage);
53888}
53889
53890/*
53891** Invoke SQLITE_FCNTL_MMAP_SIZE based on the current value of szMmap.
53892*/
53893static void pagerFixMaplimit(Pager *pPager){
53894#if SQLITE_MAX_MMAP_SIZE>0
53895 sqlite3_file *fd = pPager->fd;
53896 if( isOpen(fd) && fd->pMethods->iVersion>=3 ){
53897 sqlite3_int64 sz;
53898 sz = pPager->szMmap;
53899 pPager->bUseFetch = (sz>0);
53900 setGetterMethod(pPager);
53901 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz);
53902 }
53903#endif
53904}
53905
53906/*
53907** Change the maximum size of any memory mapping made of the database file.
53908*/
53909SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *pPager, sqlite3_int64 szMmap){
53910 pPager->szMmap = szMmap;
53911 pagerFixMaplimit(pPager);
53912}
53913
53914/*
53915** Free as much memory as possible from the pager.
53916*/
53917SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
53918 sqlite3PcacheShrink(pPager->pPCache);
53919}
53920
53921/*
53922** Adjust settings of the pager to those specified in the pgFlags parameter.
53923**
53924** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness
53925** of the database to damage due to OS crashes or power failures by
53926** changing the number of syncs()s when writing the journals.
53927** There are four levels:
53928**
53929** OFF sqlite3OsSync() is never called. This is the default
53930** for temporary and transient files.
53931**
53932** NORMAL The journal is synced once before writes begin on the
53933** database. This is normally adequate protection, but
53934** it is theoretically possible, though very unlikely,
53935** that an inopertune power failure could leave the journal
53936** in a state which would cause damage to the database
53937** when it is rolled back.
53938**
53939** FULL The journal is synced twice before writes begin on the
53940** database (with some additional information - the nRec field
53941** of the journal header - being written in between the two
53942** syncs). If we assume that writing a
53943** single disk sector is atomic, then this mode provides
53944** assurance that the journal will not be corrupted to the
53945** point of causing damage to the database during rollback.
53946**
53947** EXTRA This is like FULL except that is also syncs the directory
53948** that contains the rollback journal after the rollback
53949** journal is unlinked.
53950**
53951** The above is for a rollback-journal mode. For WAL mode, OFF continues
53952** to mean that no syncs ever occur. NORMAL means that the WAL is synced
53953** prior to the start of checkpoint and that the database file is synced
53954** at the conclusion of the checkpoint if the entire content of the WAL
53955** was written back into the database. But no sync operations occur for
53956** an ordinary commit in NORMAL mode with WAL. FULL means that the WAL
53957** file is synced following each commit operation, in addition to the
53958** syncs associated with NORMAL. There is no difference between FULL
53959** and EXTRA for WAL mode.
53960**
53961** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL. The
53962** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
53963** using fcntl(F_FULLFSYNC). SQLITE_SYNC_NORMAL means to do an
53964** ordinary fsync() call. There is no difference between SQLITE_SYNC_FULL
53965** and SQLITE_SYNC_NORMAL on platforms other than MacOSX. But the
53966** synchronous=FULL versus synchronous=NORMAL setting determines when
53967** the xSync primitive is called and is relevant to all platforms.
53968**
53969** Numeric values associated with these states are OFF==1, NORMAL=2,
53970** and FULL=3.
53971*/
53972#ifndef SQLITE_OMIT_PAGER_PRAGMAS
53973SQLITE_PRIVATE void sqlite3PagerSetFlags(
53974 Pager *pPager, /* The pager to set safety level for */
53975 unsigned pgFlags /* Various flags */
53976){
53977 unsigned level = pgFlags & PAGER_SYNCHRONOUS_MASK;
53978 if( pPager->tempFile ){
53979 pPager->noSync = 1;
53980 pPager->fullSync = 0;
53981 pPager->extraSync = 0;
53982 }else{
53983 pPager->noSync = level==PAGER_SYNCHRONOUS_OFF ?1:0;
53984 pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL ?1:0;
53985 pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA ?1:0;
53986 }
53987 if( pPager->noSync ){
53988 pPager->syncFlags = 0;
53989 }else if( pgFlags & PAGER_FULLFSYNC ){
53990 pPager->syncFlags = SQLITE_SYNC_FULL;
53991 }else{
53992 pPager->syncFlags = SQLITE_SYNC_NORMAL;
53993 }
53994 pPager->walSyncFlags = (pPager->syncFlags<<2);
53995 if( pPager->fullSync ){
53996 pPager->walSyncFlags |= pPager->syncFlags;
53997 }
53998 if( (pgFlags & PAGER_CKPT_FULLFSYNC) && !pPager->noSync ){
53999 pPager->walSyncFlags |= (SQLITE_SYNC_FULL<<2);
54000 }
54001 if( pgFlags & PAGER_CACHESPILL ){
54002 pPager->doNotSpill &= ~SPILLFLAG_OFF;
54003 }else{
54004 pPager->doNotSpill |= SPILLFLAG_OFF;
54005 }
54006}
54007#endif
54008
54009/*
54010** The following global variable is incremented whenever the library
54011** attempts to open a temporary file. This information is used for
54012** testing and analysis only.
54013*/
54014#ifdef SQLITE_TEST
54015SQLITE_API int sqlite3_opentemp_count = 0;
54016#endif
54017
54018/*
54019** Open a temporary file.
54020**
54021** Write the file descriptor into *pFile. Return SQLITE_OK on success
54022** or some other error code if we fail. The OS will automatically
54023** delete the temporary file when it is closed.
54024**
54025** The flags passed to the VFS layer xOpen() call are those specified
54026** by parameter vfsFlags ORed with the following:
54027**
54028** SQLITE_OPEN_READWRITE
54029** SQLITE_OPEN_CREATE
54030** SQLITE_OPEN_EXCLUSIVE
54031** SQLITE_OPEN_DELETEONCLOSE
54032*/
54033static int pagerOpentemp(
54034 Pager *pPager, /* The pager object */
54035 sqlite3_file *pFile, /* Write the file descriptor here */
54036 int vfsFlags /* Flags passed through to the VFS */
54037){
54038 int rc; /* Return code */
54039
54040#ifdef SQLITE_TEST
54041 sqlite3_opentemp_count++; /* Used for testing and analysis only */
54042#endif
54043
54044 vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
54045 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
54046 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
54047 assert( rc!=SQLITE_OK || isOpen(pFile) );
54048 return rc;
54049}
54050
54051/*
54052** Set the busy handler function.
54053**
54054** The pager invokes the busy-handler if sqlite3OsLock() returns
54055** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
54056** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE
54057** lock. It does *not* invoke the busy handler when upgrading from
54058** SHARED to RESERVED, or when upgrading from SHARED to EXCLUSIVE
54059** (which occurs during hot-journal rollback). Summary:
54060**
54061** Transition | Invokes xBusyHandler
54062** --------------------------------------------------------
54063** NO_LOCK -> SHARED_LOCK | Yes
54064** SHARED_LOCK -> RESERVED_LOCK | No
54065** SHARED_LOCK -> EXCLUSIVE_LOCK | No
54066** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes
54067**
54068** If the busy-handler callback returns non-zero, the lock is
54069** retried. If it returns zero, then the SQLITE_BUSY error is
54070** returned to the caller of the pager API function.
54071*/
54072SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(
54073 Pager *pPager, /* Pager object */
54074 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
54075 void *pBusyHandlerArg /* Argument to pass to xBusyHandler */
54076){
54077 void **ap;
54078 pPager->xBusyHandler = xBusyHandler;
54079 pPager->pBusyHandlerArg = pBusyHandlerArg;
54080 ap = (void **)&pPager->xBusyHandler;
54081 assert( ((int(*)(void *))(ap[0]))==xBusyHandler );
54082 assert( ap[1]==pBusyHandlerArg );
54083 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
54084}
54085
54086/*
54087** Change the page size used by the Pager object. The new page size
54088** is passed in *pPageSize.
54089**
54090** If the pager is in the error state when this function is called, it
54091** is a no-op. The value returned is the error state error code (i.e.
54092** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
54093**
54094** Otherwise, if all of the following are true:
54095**
54096** * the new page size (value of *pPageSize) is valid (a power
54097** of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
54098**
54099** * there are no outstanding page references, and
54100**
54101** * the database is either not an in-memory database or it is
54102** an in-memory database that currently consists of zero pages.
54103**
54104** then the pager object page size is set to *pPageSize.
54105**
54106** If the page size is changed, then this function uses sqlite3PagerMalloc()
54107** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt
54108** fails, SQLITE_NOMEM is returned and the page size remains unchanged.
54109** In all other cases, SQLITE_OK is returned.
54110**
54111** If the page size is not changed, either because one of the enumerated
54112** conditions above is not true, the pager was in error state when this
54113** function was called, or because the memory allocation attempt failed,
54114** then *pPageSize is set to the old, retained page size before returning.
54115*/
54116SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
54117 int rc = SQLITE_OK;
54118
54119 /* It is not possible to do a full assert_pager_state() here, as this
54120 ** function may be called from within PagerOpen(), before the state
54121 ** of the Pager object is internally consistent.
54122 **
54123 ** At one point this function returned an error if the pager was in
54124 ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that
54125 ** there is at least one outstanding page reference, this function
54126 ** is a no-op for that case anyhow.
54127 */
54128
54129 u32 pageSize = *pPageSize;
54130 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
54131 if( (pPager->memDb==0 || pPager->dbSize==0)
54132 && sqlite3PcacheRefCount(pPager->pPCache)==0
54133 && pageSize && pageSize!=(u32)pPager->pageSize
54134 ){
54135 char *pNew = NULL; /* New temp space */
54136 i64 nByte = 0;
54137
54138 if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
54139 rc = sqlite3OsFileSize(pPager->fd, &nByte);
54140 }
54141 if( rc==SQLITE_OK ){
54142 pNew = (char *)sqlite3PageMalloc(pageSize);
54143 if( !pNew ) rc = SQLITE_NOMEM_BKPT;
54144 }
54145
54146 if( rc==SQLITE_OK ){
54147 pager_reset(pPager);
54148 rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
54149 }
54150 if( rc==SQLITE_OK ){
54151 sqlite3PageFree(pPager->pTmpSpace);
54152 pPager->pTmpSpace = pNew;
54153 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
54154 pPager->pageSize = pageSize;
54155 }else{
54156 sqlite3PageFree(pNew);
54157 }
54158 }
54159
54160 *pPageSize = pPager->pageSize;
54161 if( rc==SQLITE_OK ){
54162 if( nReserve<0 ) nReserve = pPager->nReserve;
54163 assert( nReserve>=0 && nReserve<1000 );
54164 pPager->nReserve = (i16)nReserve;
54165 pagerReportSize(pPager);
54166 pagerFixMaplimit(pPager);
54167 }
54168 return rc;
54169}
54170
54171/*
54172** Return a pointer to the "temporary page" buffer held internally
54173** by the pager. This is a buffer that is big enough to hold the
54174** entire content of a database page. This buffer is used internally
54175** during rollback and will be overwritten whenever a rollback
54176** occurs. But other modules are free to use it too, as long as
54177** no rollbacks are happening.
54178*/
54179SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager *pPager){
54180 return pPager->pTmpSpace;
54181}
54182
54183/*
54184** Attempt to set the maximum database page count if mxPage is positive.
54185** Make no changes if mxPage is zero or negative. And never reduce the
54186** maximum page count below the current size of the database.
54187**
54188** Regardless of mxPage, return the current maximum page count.
54189*/
54190SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
54191 if( mxPage>0 ){
54192 pPager->mxPgno = mxPage;
54193 }
54194 assert( pPager->eState!=PAGER_OPEN ); /* Called only by OP_MaxPgcnt */
54195 assert( pPager->mxPgno>=pPager->dbSize ); /* OP_MaxPgcnt enforces this */
54196 return pPager->mxPgno;
54197}
54198
54199/*
54200** The following set of routines are used to disable the simulated
54201** I/O error mechanism. These routines are used to avoid simulated
54202** errors in places where we do not care about errors.
54203**
54204** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
54205** and generate no code.
54206*/
54207#ifdef SQLITE_TEST
54208SQLITE_API extern int sqlite3_io_error_pending;
54209SQLITE_API extern int sqlite3_io_error_hit;
54210static int saved_cnt;
54211void disable_simulated_io_errors(void){
54212 saved_cnt = sqlite3_io_error_pending;
54213 sqlite3_io_error_pending = -1;
54214}
54215void enable_simulated_io_errors(void){
54216 sqlite3_io_error_pending = saved_cnt;
54217}
54218#else
54219# define disable_simulated_io_errors()
54220# define enable_simulated_io_errors()
54221#endif
54222
54223/*
54224** Read the first N bytes from the beginning of the file into memory
54225** that pDest points to.
54226**
54227** If the pager was opened on a transient file (zFilename==""), or
54228** opened on a file less than N bytes in size, the output buffer is
54229** zeroed and SQLITE_OK returned. The rationale for this is that this
54230** function is used to read database headers, and a new transient or
54231** zero sized database has a header than consists entirely of zeroes.
54232**
54233** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered,
54234** the error code is returned to the caller and the contents of the
54235** output buffer undefined.
54236*/
54237SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
54238 int rc = SQLITE_OK;
54239 memset(pDest, 0, N);
54240 assert( isOpen(pPager->fd) || pPager->tempFile );
54241
54242 /* This routine is only called by btree immediately after creating
54243 ** the Pager object. There has not been an opportunity to transition
54244 ** to WAL mode yet.
54245 */
54246 assert( !pagerUseWal(pPager) );
54247
54248 if( isOpen(pPager->fd) ){
54249 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
54250 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
54251 if( rc==SQLITE_IOERR_SHORT_READ ){
54252 rc = SQLITE_OK;
54253 }
54254 }
54255 return rc;
54256}
54257
54258/*
54259** This function may only be called when a read-transaction is open on
54260** the pager. It returns the total number of pages in the database.
54261**
54262** However, if the file is between 1 and <page-size> bytes in size, then
54263** this is considered a 1 page file.
54264*/
54265SQLITE_PRIVATE void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
54266 assert( pPager->eState>=PAGER_READER );
54267 assert( pPager->eState!=PAGER_WRITER_FINISHED );
54268 *pnPage = (int)pPager->dbSize;
54269}
54270
54271
54272/*
54273** Try to obtain a lock of type locktype on the database file. If
54274** a similar or greater lock is already held, this function is a no-op
54275** (returning SQLITE_OK immediately).
54276**
54277** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke
54278** the busy callback if the lock is currently not available. Repeat
54279** until the busy callback returns false or until the attempt to
54280** obtain the lock succeeds.
54281**
54282** Return SQLITE_OK on success and an error code if we cannot obtain
54283** the lock. If the lock is obtained successfully, set the Pager.state
54284** variable to locktype before returning.
54285*/
54286static int pager_wait_on_lock(Pager *pPager, int locktype){
54287 int rc; /* Return code */
54288
54289 /* Check that this is either a no-op (because the requested lock is
54290 ** already held), or one of the transitions that the busy-handler
54291 ** may be invoked during, according to the comment above
54292 ** sqlite3PagerSetBusyhandler().
54293 */
54294 assert( (pPager->eLock>=locktype)
54295 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
54296 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
54297 );
54298
54299 do {
54300 rc = pagerLockDb(pPager, locktype);
54301 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
54302 return rc;
54303}
54304
54305/*
54306** Function assertTruncateConstraint(pPager) checks that one of the
54307** following is true for all dirty pages currently in the page-cache:
54308**
54309** a) The page number is less than or equal to the size of the
54310** current database image, in pages, OR
54311**
54312** b) if the page content were written at this time, it would not
54313** be necessary to write the current content out to the sub-journal
54314** (as determined by function subjRequiresPage()).
54315**
54316** If the condition asserted by this function were not true, and the
54317** dirty page were to be discarded from the cache via the pagerStress()
54318** routine, pagerStress() would not write the current page content to
54319** the database file. If a savepoint transaction were rolled back after
54320** this happened, the correct behavior would be to restore the current
54321** content of the page. However, since this content is not present in either
54322** the database file or the portion of the rollback journal and
54323** sub-journal rolled back the content could not be restored and the
54324** database image would become corrupt. It is therefore fortunate that
54325** this circumstance cannot arise.
54326*/
54327#if defined(SQLITE_DEBUG)
54328static void assertTruncateConstraintCb(PgHdr *pPg){
54329 assert( pPg->flags&PGHDR_DIRTY );
54330 assert( !subjRequiresPage(pPg) || pPg->pgno<=pPg->pPager->dbSize );
54331}
54332static void assertTruncateConstraint(Pager *pPager){
54333 sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
54334}
54335#else
54336# define assertTruncateConstraint(pPager)
54337#endif
54338
54339/*
54340** Truncate the in-memory database file image to nPage pages. This
54341** function does not actually modify the database file on disk. It
54342** just sets the internal state of the pager object so that the
54343** truncation will be done when the current transaction is committed.
54344**
54345** This function is only called right before committing a transaction.
54346** Once this function has been called, the transaction must either be
54347** rolled back or committed. It is not safe to call this function and
54348** then continue writing to the database.
54349*/
54350SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
54351 assert( pPager->dbSize>=nPage );
54352 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
54353 pPager->dbSize = nPage;
54354
54355 /* At one point the code here called assertTruncateConstraint() to
54356 ** ensure that all pages being truncated away by this operation are,
54357 ** if one or more savepoints are open, present in the savepoint
54358 ** journal so that they can be restored if the savepoint is rolled
54359 ** back. This is no longer necessary as this function is now only
54360 ** called right before committing a transaction. So although the
54361 ** Pager object may still have open savepoints (Pager.nSavepoint!=0),
54362 ** they cannot be rolled back. So the assertTruncateConstraint() call
54363 ** is no longer correct. */
54364}
54365
54366
54367/*
54368** This function is called before attempting a hot-journal rollback. It
54369** syncs the journal file to disk, then sets pPager->journalHdr to the
54370** size of the journal file so that the pager_playback() routine knows
54371** that the entire journal file has been synced.
54372**
54373** Syncing a hot-journal to disk before attempting to roll it back ensures
54374** that if a power-failure occurs during the rollback, the process that
54375** attempts rollback following system recovery sees the same journal
54376** content as this process.
54377**
54378** If everything goes as planned, SQLITE_OK is returned. Otherwise,
54379** an SQLite error code.
54380*/
54381static int pagerSyncHotJournal(Pager *pPager){
54382 int rc = SQLITE_OK;
54383 if( !pPager->noSync ){
54384 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
54385 }
54386 if( rc==SQLITE_OK ){
54387 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
54388 }
54389 return rc;
54390}
54391
54392#if SQLITE_MAX_MMAP_SIZE>0
54393/*
54394** Obtain a reference to a memory mapped page object for page number pgno.
54395** The new object will use the pointer pData, obtained from xFetch().
54396** If successful, set *ppPage to point to the new page reference
54397** and return SQLITE_OK. Otherwise, return an SQLite error code and set
54398** *ppPage to zero.
54399**
54400** Page references obtained by calling this function should be released
54401** by calling pagerReleaseMapPage().
54402*/
54403static int pagerAcquireMapPage(
54404 Pager *pPager, /* Pager object */
54405 Pgno pgno, /* Page number */
54406 void *pData, /* xFetch()'d data for this page */
54407 PgHdr **ppPage /* OUT: Acquired page object */
54408){
54409 PgHdr *p; /* Memory mapped page to return */
54410
54411 if( pPager->pMmapFreelist ){
54412 *ppPage = p = pPager->pMmapFreelist;
54413 pPager->pMmapFreelist = p->pDirty;
54414 p->pDirty = 0;
54415 assert( pPager->nExtra>=8 );
54416 memset(p->pExtra, 0, 8);
54417 }else{
54418 *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
54419 if( p==0 ){
54420 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
54421 return SQLITE_NOMEM_BKPT;
54422 }
54423 p->pExtra = (void *)&p[1];
54424 p->flags = PGHDR_MMAP;
54425 p->nRef = 1;
54426 p->pPager = pPager;
54427 }
54428
54429 assert( p->pExtra==(void *)&p[1] );
54430 assert( p->pPage==0 );
54431 assert( p->flags==PGHDR_MMAP );
54432 assert( p->pPager==pPager );
54433 assert( p->nRef==1 );
54434
54435 p->pgno = pgno;
54436 p->pData = pData;
54437 pPager->nMmapOut++;
54438
54439 return SQLITE_OK;
54440}
54441#endif
54442
54443/*
54444** Release a reference to page pPg. pPg must have been returned by an
54445** earlier call to pagerAcquireMapPage().
54446*/
54447static void pagerReleaseMapPage(PgHdr *pPg){
54448 Pager *pPager = pPg->pPager;
54449 pPager->nMmapOut--;
54450 pPg->pDirty = pPager->pMmapFreelist;
54451 pPager->pMmapFreelist = pPg;
54452
54453 assert( pPager->fd->pMethods->iVersion>=3 );
54454 sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
54455}
54456
54457/*
54458** Free all PgHdr objects stored in the Pager.pMmapFreelist list.
54459*/
54460static void pagerFreeMapHdrs(Pager *pPager){
54461 PgHdr *p;
54462 PgHdr *pNext;
54463 for(p=pPager->pMmapFreelist; p; p=pNext){
54464 pNext = p->pDirty;
54465 sqlite3_free(p);
54466 }
54467}
54468
54469/* Verify that the database file has not be deleted or renamed out from
54470** under the pager. Return SQLITE_OK if the database is still where it ought
54471** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error
54472** code from sqlite3OsAccess()) if the database has gone missing.
54473*/
54474static int databaseIsUnmoved(Pager *pPager){
54475 int bHasMoved = 0;
54476 int rc;
54477
54478 if( pPager->tempFile ) return SQLITE_OK;
54479 if( pPager->dbSize==0 ) return SQLITE_OK;
54480 assert( pPager->zFilename && pPager->zFilename[0] );
54481 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
54482 if( rc==SQLITE_NOTFOUND ){
54483 /* If the HAS_MOVED file-control is unimplemented, assume that the file
54484 ** has not been moved. That is the historical behavior of SQLite: prior to
54485 ** version 3.8.3, it never checked */
54486 rc = SQLITE_OK;
54487 }else if( rc==SQLITE_OK && bHasMoved ){
54488 rc = SQLITE_READONLY_DBMOVED;
54489 }
54490 return rc;
54491}
54492
54493
54494/*
54495** Shutdown the page cache. Free all memory and close all files.
54496**
54497** If a transaction was in progress when this routine is called, that
54498** transaction is rolled back. All outstanding pages are invalidated
54499** and their memory is freed. Any attempt to use a page associated
54500** with this page cache after this function returns will likely
54501** result in a coredump.
54502**
54503** This function always succeeds. If a transaction is active an attempt
54504** is made to roll it back. If an error occurs during the rollback
54505** a hot journal may be left in the filesystem but no error is returned
54506** to the caller.
54507*/
54508SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3 *db){
54509 u8 *pTmp = (u8*)pPager->pTmpSpace;
54510 assert( db || pagerUseWal(pPager)==0 );
54511 assert( assert_pager_state(pPager) );
54512 disable_simulated_io_errors();
54513 sqlite3BeginBenignMalloc();
54514 pagerFreeMapHdrs(pPager);
54515 /* pPager->errCode = 0; */
54516 pPager->exclusiveMode = 0;
54517#ifndef SQLITE_OMIT_WAL
54518 {
54519 u8 *a = 0;
54520 assert( db || pPager->pWal==0 );
54521 if( db && 0==(db->flags & SQLITE_NoCkptOnClose)
54522 && SQLITE_OK==databaseIsUnmoved(pPager)
54523 ){
54524 a = pTmp;
54525 }
54526 sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a);
54527 pPager->pWal = 0;
54528 }
54529#endif
54530 pager_reset(pPager);
54531 if( MEMDB ){
54532 pager_unlock(pPager);
54533 }else{
54534 /* If it is open, sync the journal file before calling UnlockAndRollback.
54535 ** If this is not done, then an unsynced portion of the open journal
54536 ** file may be played back into the database. If a power failure occurs
54537 ** while this is happening, the database could become corrupt.
54538 **
54539 ** If an error occurs while trying to sync the journal, shift the pager
54540 ** into the ERROR state. This causes UnlockAndRollback to unlock the
54541 ** database and close the journal file without attempting to roll it
54542 ** back or finalize it. The next database user will have to do hot-journal
54543 ** rollback before accessing the database file.
54544 */
54545 if( isOpen(pPager->jfd) ){
54546 pager_error(pPager, pagerSyncHotJournal(pPager));
54547 }
54548 pagerUnlockAndRollback(pPager);
54549 }
54550 sqlite3EndBenignMalloc();
54551 enable_simulated_io_errors();
54552 PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
54553 IOTRACE(("CLOSE %p\n", pPager))
54554 sqlite3OsClose(pPager->jfd);
54555 sqlite3OsClose(pPager->fd);
54556 sqlite3PageFree(pTmp);
54557 sqlite3PcacheClose(pPager->pPCache);
54558
54559#ifdef SQLITE_HAS_CODEC
54560 if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
54561#endif
54562
54563 assert( !pPager->aSavepoint && !pPager->pInJournal );
54564 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
54565
54566 sqlite3_free(pPager);
54567 return SQLITE_OK;
54568}
54569
54570#if !defined(NDEBUG) || defined(SQLITE_TEST)
54571/*
54572** Return the page number for page pPg.
54573*/
54574SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage *pPg){
54575 return pPg->pgno;
54576}
54577#endif
54578
54579/*
54580** Increment the reference count for page pPg.
54581*/
54582SQLITE_PRIVATE void sqlite3PagerRef(DbPage *pPg){
54583 sqlite3PcacheRef(pPg);
54584}
54585
54586/*
54587** Sync the journal. In other words, make sure all the pages that have
54588** been written to the journal have actually reached the surface of the
54589** disk and can be restored in the event of a hot-journal rollback.
54590**
54591** If the Pager.noSync flag is set, then this function is a no-op.
54592** Otherwise, the actions required depend on the journal-mode and the
54593** device characteristics of the file-system, as follows:
54594**
54595** * If the journal file is an in-memory journal file, no action need
54596** be taken.
54597**
54598** * Otherwise, if the device does not support the SAFE_APPEND property,
54599** then the nRec field of the most recently written journal header
54600** is updated to contain the number of journal records that have
54601** been written following it. If the pager is operating in full-sync
54602** mode, then the journal file is synced before this field is updated.
54603**
54604** * If the device does not support the SEQUENTIAL property, then
54605** journal file is synced.
54606**
54607** Or, in pseudo-code:
54608**
54609** if( NOT <in-memory journal> ){
54610** if( NOT SAFE_APPEND ){
54611** if( <full-sync mode> ) xSync(<journal file>);
54612** <update nRec field>
54613** }
54614** if( NOT SEQUENTIAL ) xSync(<journal file>);
54615** }
54616**
54617** If successful, this routine clears the PGHDR_NEED_SYNC flag of every
54618** page currently held in memory before returning SQLITE_OK. If an IO
54619** error is encountered, then the IO error code is returned to the caller.
54620*/
54621static int syncJournal(Pager *pPager, int newHdr){
54622 int rc; /* Return code */
54623
54624 assert( pPager->eState==PAGER_WRITER_CACHEMOD
54625 || pPager->eState==PAGER_WRITER_DBMOD
54626 );
54627 assert( assert_pager_state(pPager) );
54628 assert( !pagerUseWal(pPager) );
54629
54630 rc = sqlite3PagerExclusiveLock(pPager);
54631 if( rc!=SQLITE_OK ) return rc;
54632
54633 if( !pPager->noSync ){
54634 assert( !pPager->tempFile );
54635 if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
54636 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
54637 assert( isOpen(pPager->jfd) );
54638
54639 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
54640 /* This block deals with an obscure problem. If the last connection
54641 ** that wrote to this database was operating in persistent-journal
54642 ** mode, then the journal file may at this point actually be larger
54643 ** than Pager.journalOff bytes. If the next thing in the journal
54644 ** file happens to be a journal-header (written as part of the
54645 ** previous connection's transaction), and a crash or power-failure
54646 ** occurs after nRec is updated but before this connection writes
54647 ** anything else to the journal file (or commits/rolls back its
54648 ** transaction), then SQLite may become confused when doing the
54649 ** hot-journal rollback following recovery. It may roll back all
54650 ** of this connections data, then proceed to rolling back the old,
54651 ** out-of-date data that follows it. Database corruption.
54652 **
54653 ** To work around this, if the journal file does appear to contain
54654 ** a valid header following Pager.journalOff, then write a 0x00
54655 ** byte to the start of it to prevent it from being recognized.
54656 **
54657 ** Variable iNextHdrOffset is set to the offset at which this
54658 ** problematic header will occur, if it exists. aMagic is used
54659 ** as a temporary buffer to inspect the first couple of bytes of
54660 ** the potential journal header.
54661 */
54662 i64 iNextHdrOffset;
54663 u8 aMagic[8];
54664 u8 zHeader[sizeof(aJournalMagic)+4];
54665
54666 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
54667 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
54668
54669 iNextHdrOffset = journalHdrOffset(pPager);
54670 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
54671 if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){
54672 static const u8 zerobyte = 0;
54673 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
54674 }
54675 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
54676 return rc;
54677 }
54678
54679 /* Write the nRec value into the journal file header. If in
54680 ** full-synchronous mode, sync the journal first. This ensures that
54681 ** all data has really hit the disk before nRec is updated to mark
54682 ** it as a candidate for rollback.
54683 **
54684 ** This is not required if the persistent media supports the
54685 ** SAFE_APPEND property. Because in this case it is not possible
54686 ** for garbage data to be appended to the file, the nRec field
54687 ** is populated with 0xFFFFFFFF when the journal header is written
54688 ** and never needs to be updated.
54689 */
54690 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
54691 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
54692 IOTRACE(("JSYNC %p\n", pPager))
54693 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
54694 if( rc!=SQLITE_OK ) return rc;
54695 }
54696 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
54697 rc = sqlite3OsWrite(
54698 pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
54699 );
54700 if( rc!=SQLITE_OK ) return rc;
54701 }
54702 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
54703 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
54704 IOTRACE(("JSYNC %p\n", pPager))
54705 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
54706 (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
54707 );
54708 if( rc!=SQLITE_OK ) return rc;
54709 }
54710
54711 pPager->journalHdr = pPager->journalOff;
54712 if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
54713 pPager->nRec = 0;
54714 rc = writeJournalHdr(pPager);
54715 if( rc!=SQLITE_OK ) return rc;
54716 }
54717 }else{
54718 pPager->journalHdr = pPager->journalOff;
54719 }
54720 }
54721
54722 /* Unless the pager is in noSync mode, the journal file was just
54723 ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on
54724 ** all pages.
54725 */
54726 sqlite3PcacheClearSyncFlags(pPager->pPCache);
54727 pPager->eState = PAGER_WRITER_DBMOD;
54728 assert( assert_pager_state(pPager) );
54729 return SQLITE_OK;
54730}
54731
54732/*
54733** The argument is the first in a linked list of dirty pages connected
54734** by the PgHdr.pDirty pointer. This function writes each one of the
54735** in-memory pages in the list to the database file. The argument may
54736** be NULL, representing an empty list. In this case this function is
54737** a no-op.
54738**
54739** The pager must hold at least a RESERVED lock when this function
54740** is called. Before writing anything to the database file, this lock
54741** is upgraded to an EXCLUSIVE lock. If the lock cannot be obtained,
54742** SQLITE_BUSY is returned and no data is written to the database file.
54743**
54744** If the pager is a temp-file pager and the actual file-system file
54745** is not yet open, it is created and opened before any data is
54746** written out.
54747**
54748** Once the lock has been upgraded and, if necessary, the file opened,
54749** the pages are written out to the database file in list order. Writing
54750** a page is skipped if it meets either of the following criteria:
54751**
54752** * The page number is greater than Pager.dbSize, or
54753** * The PGHDR_DONT_WRITE flag is set on the page.
54754**
54755** If writing out a page causes the database file to grow, Pager.dbFileSize
54756** is updated accordingly. If page 1 is written out, then the value cached
54757** in Pager.dbFileVers[] is updated to match the new value stored in
54758** the database file.
54759**
54760** If everything is successful, SQLITE_OK is returned. If an IO error
54761** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
54762** be obtained, SQLITE_BUSY is returned.
54763*/
54764static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
54765 int rc = SQLITE_OK; /* Return code */
54766
54767 /* This function is only called for rollback pagers in WRITER_DBMOD state. */
54768 assert( !pagerUseWal(pPager) );
54769 assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD );
54770 assert( pPager->eLock==EXCLUSIVE_LOCK );
54771 assert( isOpen(pPager->fd) || pList->pDirty==0 );
54772
54773 /* If the file is a temp-file has not yet been opened, open it now. It
54774 ** is not possible for rc to be other than SQLITE_OK if this branch
54775 ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
54776 */
54777 if( !isOpen(pPager->fd) ){
54778 assert( pPager->tempFile && rc==SQLITE_OK );
54779 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
54780 }
54781
54782 /* Before the first write, give the VFS a hint of what the final
54783 ** file size will be.
54784 */
54785 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
54786 if( rc==SQLITE_OK
54787 && pPager->dbHintSize<pPager->dbSize
54788 && (pList->pDirty || pList->pgno>pPager->dbHintSize)
54789 ){
54790 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
54791 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
54792 pPager->dbHintSize = pPager->dbSize;
54793 }
54794
54795 while( rc==SQLITE_OK && pList ){
54796 Pgno pgno = pList->pgno;
54797
54798 /* If there are dirty pages in the page cache with page numbers greater
54799 ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
54800 ** make the file smaller (presumably by auto-vacuum code). Do not write
54801 ** any such pages to the file.
54802 **
54803 ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
54804 ** set (set by sqlite3PagerDontWrite()).
54805 */
54806 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
54807 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
54808 char *pData; /* Data to write */
54809
54810 assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
54811 if( pList->pgno==1 ) pager_write_changecounter(pList);
54812
54813 /* Encode the database */
54814 CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM_BKPT, pData);
54815
54816 /* Write out the page data. */
54817 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
54818
54819 /* If page 1 was just written, update Pager.dbFileVers to match
54820 ** the value now stored in the database file. If writing this
54821 ** page caused the database file to grow, update dbFileSize.
54822 */
54823 if( pgno==1 ){
54824 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
54825 }
54826 if( pgno>pPager->dbFileSize ){
54827 pPager->dbFileSize = pgno;
54828 }
54829 pPager->aStat[PAGER_STAT_WRITE]++;
54830
54831 /* Update any backup objects copying the contents of this pager. */
54832 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
54833
54834 PAGERTRACE(("STORE %d page %d hash(%08x)\n",
54835 PAGERID(pPager), pgno, pager_pagehash(pList)));
54836 IOTRACE(("PGOUT %p %d\n", pPager, pgno));
54837 PAGER_INCR(sqlite3_pager_writedb_count);
54838 }else{
54839 PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
54840 }
54841 pager_set_pagehash(pList);
54842 pList = pList->pDirty;
54843 }
54844
54845 return rc;
54846}
54847
54848/*
54849** Ensure that the sub-journal file is open. If it is already open, this
54850** function is a no-op.
54851**
54852** SQLITE_OK is returned if everything goes according to plan. An
54853** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen()
54854** fails.
54855*/
54856static int openSubJournal(Pager *pPager){
54857 int rc = SQLITE_OK;
54858 if( !isOpen(pPager->sjfd) ){
54859 const int flags = SQLITE_OPEN_SUBJOURNAL | SQLITE_OPEN_READWRITE
54860 | SQLITE_OPEN_CREATE | SQLITE_OPEN_EXCLUSIVE
54861 | SQLITE_OPEN_DELETEONCLOSE;
54862 int nStmtSpill = sqlite3Config.nStmtSpill;
54863 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
54864 nStmtSpill = -1;
54865 }
54866 rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill);
54867 }
54868 return rc;
54869}
54870
54871/*
54872** Append a record of the current state of page pPg to the sub-journal.
54873**
54874** If successful, set the bit corresponding to pPg->pgno in the bitvecs
54875** for all open savepoints before returning.
54876**
54877** This function returns SQLITE_OK if everything is successful, an IO
54878** error code if the attempt to write to the sub-journal fails, or
54879** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint
54880** bitvec.
54881*/
54882static int subjournalPage(PgHdr *pPg){
54883 int rc = SQLITE_OK;
54884 Pager *pPager = pPg->pPager;
54885 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
54886
54887 /* Open the sub-journal, if it has not already been opened */
54888 assert( pPager->useJournal );
54889 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
54890 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
54891 assert( pagerUseWal(pPager)
54892 || pageInJournal(pPager, pPg)
54893 || pPg->pgno>pPager->dbOrigSize
54894 );
54895 rc = openSubJournal(pPager);
54896
54897 /* If the sub-journal was opened successfully (or was already open),
54898 ** write the journal record into the file. */
54899 if( rc==SQLITE_OK ){
54900 void *pData = pPg->pData;
54901 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
54902 char *pData2;
54903
54904#if SQLITE_HAS_CODEC
54905 if( !pPager->subjInMemory ){
54906 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2);
54907 }else
54908#endif
54909 pData2 = pData;
54910 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
54911 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
54912 if( rc==SQLITE_OK ){
54913 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
54914 }
54915 }
54916 }
54917 if( rc==SQLITE_OK ){
54918 pPager->nSubRec++;
54919 assert( pPager->nSavepoint>0 );
54920 rc = addToSavepointBitvecs(pPager, pPg->pgno);
54921 }
54922 return rc;
54923}
54924static int subjournalPageIfRequired(PgHdr *pPg){
54925 if( subjRequiresPage(pPg) ){
54926 return subjournalPage(pPg);
54927 }else{
54928 return SQLITE_OK;
54929 }
54930}
54931
54932/*
54933** This function is called by the pcache layer when it has reached some
54934** soft memory limit. The first argument is a pointer to a Pager object
54935** (cast as a void*). The pager is always 'purgeable' (not an in-memory
54936** database). The second argument is a reference to a page that is
54937** currently dirty but has no outstanding references. The page
54938** is always associated with the Pager object passed as the first
54939** argument.
54940**
54941** The job of this function is to make pPg clean by writing its contents
54942** out to the database file, if possible. This may involve syncing the
54943** journal file.
54944**
54945** If successful, sqlite3PcacheMakeClean() is called on the page and
54946** SQLITE_OK returned. If an IO error occurs while trying to make the
54947** page clean, the IO error code is returned. If the page cannot be
54948** made clean for some other reason, but no error occurs, then SQLITE_OK
54949** is returned by sqlite3PcacheMakeClean() is not called.
54950*/
54951static int pagerStress(void *p, PgHdr *pPg){
54952 Pager *pPager = (Pager *)p;
54953 int rc = SQLITE_OK;
54954
54955 assert( pPg->pPager==pPager );
54956 assert( pPg->flags&PGHDR_DIRTY );
54957
54958 /* The doNotSpill NOSYNC bit is set during times when doing a sync of
54959 ** journal (and adding a new header) is not allowed. This occurs
54960 ** during calls to sqlite3PagerWrite() while trying to journal multiple
54961 ** pages belonging to the same sector.
54962 **
54963 ** The doNotSpill ROLLBACK and OFF bits inhibits all cache spilling
54964 ** regardless of whether or not a sync is required. This is set during
54965 ** a rollback or by user request, respectively.
54966 **
54967 ** Spilling is also prohibited when in an error state since that could
54968 ** lead to database corruption. In the current implementation it
54969 ** is impossible for sqlite3PcacheFetch() to be called with createFlag==3
54970 ** while in the error state, hence it is impossible for this routine to
54971 ** be called in the error state. Nevertheless, we include a NEVER()
54972 ** test for the error state as a safeguard against future changes.
54973 */
54974 if( NEVER(pPager->errCode) ) return SQLITE_OK;
54975 testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
54976 testcase( pPager->doNotSpill & SPILLFLAG_OFF );
54977 testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
54978 if( pPager->doNotSpill
54979 && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
54980 || (pPg->flags & PGHDR_NEED_SYNC)!=0)
54981 ){
54982 return SQLITE_OK;
54983 }
54984
54985 pPager->aStat[PAGER_STAT_SPILL]++;
54986 pPg->pDirty = 0;
54987 if( pagerUseWal(pPager) ){
54988 /* Write a single frame for this page to the log. */
54989 rc = subjournalPageIfRequired(pPg);
54990 if( rc==SQLITE_OK ){
54991 rc = pagerWalFrames(pPager, pPg, 0, 0);
54992 }
54993 }else{
54994
54995#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
54996 if( pPager->tempFile==0 ){
54997 rc = sqlite3JournalCreate(pPager->jfd);
54998 if( rc!=SQLITE_OK ) return pager_error(pPager, rc);
54999 }
55000#endif
55001
55002 /* Sync the journal file if required. */
55003 if( pPg->flags&PGHDR_NEED_SYNC
55004 || pPager->eState==PAGER_WRITER_CACHEMOD
55005 ){
55006 rc = syncJournal(pPager, 1);
55007 }
55008
55009 /* Write the contents of the page out to the database file. */
55010 if( rc==SQLITE_OK ){
55011 assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
55012 rc = pager_write_pagelist(pPager, pPg);
55013 }
55014 }
55015
55016 /* Mark the page as clean. */
55017 if( rc==SQLITE_OK ){
55018 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
55019 sqlite3PcacheMakeClean(pPg);
55020 }
55021
55022 return pager_error(pPager, rc);
55023}
55024
55025/*
55026** Flush all unreferenced dirty pages to disk.
55027*/
55028SQLITE_PRIVATE int sqlite3PagerFlush(Pager *pPager){
55029 int rc = pPager->errCode;
55030 if( !MEMDB ){
55031 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
55032 assert( assert_pager_state(pPager) );
55033 while( rc==SQLITE_OK && pList ){
55034 PgHdr *pNext = pList->pDirty;
55035 if( pList->nRef==0 ){
55036 rc = pagerStress((void*)pPager, pList);
55037 }
55038 pList = pNext;
55039 }
55040 }
55041
55042 return rc;
55043}
55044
55045/*
55046** Allocate and initialize a new Pager object and put a pointer to it
55047** in *ppPager. The pager should eventually be freed by passing it
55048** to sqlite3PagerClose().
55049**
55050** The zFilename argument is the path to the database file to open.
55051** If zFilename is NULL then a randomly-named temporary file is created
55052** and used as the file to be cached. Temporary files are be deleted
55053** automatically when they are closed. If zFilename is ":memory:" then
55054** all information is held in cache. It is never written to disk.
55055** This can be used to implement an in-memory database.
55056**
55057** The nExtra parameter specifies the number of bytes of space allocated
55058** along with each page reference. This space is available to the user
55059** via the sqlite3PagerGetExtra() API. When a new page is allocated, the
55060** first 8 bytes of this space are zeroed but the remainder is uninitialized.
55061** (The extra space is used by btree as the MemPage object.)
55062**
55063** The flags argument is used to specify properties that affect the
55064** operation of the pager. It should be passed some bitwise combination
55065** of the PAGER_* flags.
55066**
55067** The vfsFlags parameter is a bitmask to pass to the flags parameter
55068** of the xOpen() method of the supplied VFS when opening files.
55069**
55070** If the pager object is allocated and the specified file opened
55071** successfully, SQLITE_OK is returned and *ppPager set to point to
55072** the new pager object. If an error occurs, *ppPager is set to NULL
55073** and error code returned. This function may return SQLITE_NOMEM
55074** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or
55075** various SQLITE_IO_XXX errors.
55076*/
55077SQLITE_PRIVATE int sqlite3PagerOpen(
55078 sqlite3_vfs *pVfs, /* The virtual file system to use */
55079 Pager **ppPager, /* OUT: Return the Pager structure here */
55080 const char *zFilename, /* Name of the database file to open */
55081 int nExtra, /* Extra bytes append to each in-memory page */
55082 int flags, /* flags controlling this file */
55083 int vfsFlags, /* flags passed through to sqlite3_vfs.xOpen() */
55084 void (*xReinit)(DbPage*) /* Function to reinitialize pages */
55085){
55086 u8 *pPtr;
55087 Pager *pPager = 0; /* Pager object to allocate and return */
55088 int rc = SQLITE_OK; /* Return code */
55089 int tempFile = 0; /* True for temp files (incl. in-memory files) */
55090 int memDb = 0; /* True if this is an in-memory file */
55091#ifdef SQLITE_ENABLE_DESERIALIZE
55092 int memJM = 0; /* Memory journal mode */
55093#else
55094# define memJM 0
55095#endif
55096 int readOnly = 0; /* True if this is a read-only file */
55097 int journalFileSize; /* Bytes to allocate for each journal fd */
55098 char *zPathname = 0; /* Full path to database file */
55099 int nPathname = 0; /* Number of bytes in zPathname */
55100 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
55101 int pcacheSize = sqlite3PcacheSize(); /* Bytes to allocate for PCache */
55102 u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE; /* Default page size */
55103 const char *zUri = 0; /* URI args to copy */
55104 int nUri = 0; /* Number of bytes of URI args at *zUri */
55105
55106 /* Figure out how much space is required for each journal file-handle
55107 ** (there are two of them, the main journal and the sub-journal). */
55108 journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
55109
55110 /* Set the output variable to NULL in case an error occurs. */
55111 *ppPager = 0;
55112
55113#ifndef SQLITE_OMIT_MEMORYDB
55114 if( flags & PAGER_MEMORY ){
55115 memDb = 1;
55116 if( zFilename && zFilename[0] ){
55117 zPathname = sqlite3DbStrDup(0, zFilename);
55118 if( zPathname==0 ) return SQLITE_NOMEM_BKPT;
55119 nPathname = sqlite3Strlen30(zPathname);
55120 zFilename = 0;
55121 }
55122 }
55123#endif
55124
55125 /* Compute and store the full pathname in an allocated buffer pointed
55126 ** to by zPathname, length nPathname. Or, if this is a temporary file,
55127 ** leave both nPathname and zPathname set to 0.
55128 */
55129 if( zFilename && zFilename[0] ){
55130 const char *z;
55131 nPathname = pVfs->mxPathname+1;
55132 zPathname = sqlite3DbMallocRaw(0, nPathname*2);
55133 if( zPathname==0 ){
55134 return SQLITE_NOMEM_BKPT;
55135 }
55136 zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
55137 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
55138 nPathname = sqlite3Strlen30(zPathname);
55139 z = zUri = &zFilename[sqlite3Strlen30(zFilename)+1];
55140 while( *z ){
55141 z += sqlite3Strlen30(z)+1;
55142 z += sqlite3Strlen30(z)+1;
55143 }
55144 nUri = (int)(&z[1] - zUri);
55145 assert( nUri>=0 );
55146 if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
55147 /* This branch is taken when the journal path required by
55148 ** the database being opened will be more than pVfs->mxPathname
55149 ** bytes in length. This means the database cannot be opened,
55150 ** as it will not be possible to open the journal file or even
55151 ** check for a hot-journal before reading.
55152 */
55153 rc = SQLITE_CANTOPEN_BKPT;
55154 }
55155 if( rc!=SQLITE_OK ){
55156 sqlite3DbFree(0, zPathname);
55157 return rc;
55158 }
55159 }
55160
55161 /* Allocate memory for the Pager structure, PCache object, the
55162 ** three file descriptors, the database file name and the journal
55163 ** file name. The layout in memory is as follows:
55164 **
55165 ** Pager object (sizeof(Pager) bytes)
55166 ** PCache object (sqlite3PcacheSize() bytes)
55167 ** Database file handle (pVfs->szOsFile bytes)
55168 ** Sub-journal file handle (journalFileSize bytes)
55169 ** Main journal file handle (journalFileSize bytes)
55170 ** Database file name (nPathname+1 bytes)
55171 ** Journal file name (nPathname+8+1 bytes)
55172 */
55173 pPtr = (u8 *)sqlite3MallocZero(
55174 ROUND8(sizeof(*pPager)) + /* Pager structure */
55175 ROUND8(pcacheSize) + /* PCache object */
55176 ROUND8(pVfs->szOsFile) + /* The main db file */
55177 journalFileSize * 2 + /* The two journal files */
55178 nPathname + 1 + nUri + /* zFilename */
55179 nPathname + 8 + 2 /* zJournal */
55180#ifndef SQLITE_OMIT_WAL
55181 + nPathname + 4 + 2 /* zWal */
55182#endif
55183 );
55184 assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) );
55185 if( !pPtr ){
55186 sqlite3DbFree(0, zPathname);
55187 return SQLITE_NOMEM_BKPT;
55188 }
55189 pPager = (Pager*)(pPtr);
55190 pPager->pPCache = (PCache*)(pPtr += ROUND8(sizeof(*pPager)));
55191 pPager->fd = (sqlite3_file*)(pPtr += ROUND8(pcacheSize));
55192 pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile));
55193 pPager->jfd = (sqlite3_file*)(pPtr += journalFileSize);
55194 pPager->zFilename = (char*)(pPtr += journalFileSize);
55195 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
55196
55197 /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */
55198 if( zPathname ){
55199 assert( nPathname>0 );
55200 pPager->zJournal = (char*)(pPtr += nPathname + 1 + nUri);
55201 memcpy(pPager->zFilename, zPathname, nPathname);
55202 if( nUri ) memcpy(&pPager->zFilename[nPathname+1], zUri, nUri);
55203 memcpy(pPager->zJournal, zPathname, nPathname);
55204 memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+2);
55205 sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal);
55206#ifndef SQLITE_OMIT_WAL
55207 pPager->zWal = &pPager->zJournal[nPathname+8+1];
55208 memcpy(pPager->zWal, zPathname, nPathname);
55209 memcpy(&pPager->zWal[nPathname], "-wal\000", 4+1);
55210 sqlite3FileSuffix3(pPager->zFilename, pPager->zWal);
55211#endif
55212 sqlite3DbFree(0, zPathname);
55213 }
55214 pPager->pVfs = pVfs;
55215 pPager->vfsFlags = vfsFlags;
55216
55217 /* Open the pager file.
55218 */
55219 if( zFilename && zFilename[0] ){
55220 int fout = 0; /* VFS flags returned by xOpen() */
55221 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
55222 assert( !memDb );
55223#ifdef SQLITE_ENABLE_DESERIALIZE
55224 memJM = (fout&SQLITE_OPEN_MEMORY)!=0;
55225#endif
55226 readOnly = (fout&SQLITE_OPEN_READONLY)!=0;
55227
55228 /* If the file was successfully opened for read/write access,
55229 ** choose a default page size in case we have to create the
55230 ** database file. The default page size is the maximum of:
55231 **
55232 ** + SQLITE_DEFAULT_PAGE_SIZE,
55233 ** + The value returned by sqlite3OsSectorSize()
55234 ** + The largest page size that can be written atomically.
55235 */
55236 if( rc==SQLITE_OK ){
55237 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
55238 if( !readOnly ){
55239 setSectorSize(pPager);
55240 assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
55241 if( szPageDflt<pPager->sectorSize ){
55242 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
55243 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
55244 }else{
55245 szPageDflt = (u32)pPager->sectorSize;
55246 }
55247 }
55248#ifdef SQLITE_ENABLE_ATOMIC_WRITE
55249 {
55250 int ii;
55251 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
55252 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
55253 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
55254 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
55255 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
55256 szPageDflt = ii;
55257 }
55258 }
55259 }
55260#endif
55261 }
55262 pPager->noLock = sqlite3_uri_boolean(zFilename, "nolock", 0);
55263 if( (iDc & SQLITE_IOCAP_IMMUTABLE)!=0
55264 || sqlite3_uri_boolean(zFilename, "immutable", 0) ){
55265 vfsFlags |= SQLITE_OPEN_READONLY;
55266 goto act_like_temp_file;
55267 }
55268 }
55269 }else{
55270 /* If a temporary file is requested, it is not opened immediately.
55271 ** In this case we accept the default page size and delay actually
55272 ** opening the file until the first call to OsWrite().
55273 **
55274 ** This branch is also run for an in-memory database. An in-memory
55275 ** database is the same as a temp-file that is never written out to
55276 ** disk and uses an in-memory rollback journal.
55277 **
55278 ** This branch also runs for files marked as immutable.
55279 */
55280act_like_temp_file:
55281 tempFile = 1;
55282 pPager->eState = PAGER_READER; /* Pretend we already have a lock */
55283 pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE mode */
55284 pPager->noLock = 1; /* Do no locking */
55285 readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
55286 }
55287
55288 /* The following call to PagerSetPagesize() serves to set the value of
55289 ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
55290 */
55291 if( rc==SQLITE_OK ){
55292 assert( pPager->memDb==0 );
55293 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
55294 testcase( rc!=SQLITE_OK );
55295 }
55296
55297 /* Initialize the PCache object. */
55298 if( rc==SQLITE_OK ){
55299 nExtra = ROUND8(nExtra);
55300 assert( nExtra>=8 && nExtra<1000 );
55301 rc = sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
55302 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
55303 }
55304
55305 /* If an error occurred above, free the Pager structure and close the file.
55306 */
55307 if( rc!=SQLITE_OK ){
55308 sqlite3OsClose(pPager->fd);
55309 sqlite3PageFree(pPager->pTmpSpace);
55310 sqlite3_free(pPager);
55311 return rc;
55312 }
55313
55314 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
55315 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
55316
55317 pPager->useJournal = (u8)useJournal;
55318 /* pPager->stmtOpen = 0; */
55319 /* pPager->stmtInUse = 0; */
55320 /* pPager->nRef = 0; */
55321 /* pPager->stmtSize = 0; */
55322 /* pPager->stmtJSize = 0; */
55323 /* pPager->nPage = 0; */
55324 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
55325 /* pPager->state = PAGER_UNLOCK; */
55326 /* pPager->errMask = 0; */
55327 pPager->tempFile = (u8)tempFile;
55328 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
55329 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
55330 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
55331 pPager->exclusiveMode = (u8)tempFile;
55332 pPager->changeCountDone = pPager->tempFile;
55333 pPager->memDb = (u8)memDb;
55334 pPager->readOnly = (u8)readOnly;
55335 assert( useJournal || pPager->tempFile );
55336 pPager->noSync = pPager->tempFile;
55337 if( pPager->noSync ){
55338 assert( pPager->fullSync==0 );
55339 assert( pPager->extraSync==0 );
55340 assert( pPager->syncFlags==0 );
55341 assert( pPager->walSyncFlags==0 );
55342 }else{
55343 pPager->fullSync = 1;
55344 pPager->extraSync = 0;
55345 pPager->syncFlags = SQLITE_SYNC_NORMAL;
55346 pPager->walSyncFlags = SQLITE_SYNC_NORMAL | (SQLITE_SYNC_NORMAL<<2);
55347 }
55348 /* pPager->pFirst = 0; */
55349 /* pPager->pFirstSynced = 0; */
55350 /* pPager->pLast = 0; */
55351 pPager->nExtra = (u16)nExtra;
55352 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
55353 assert( isOpen(pPager->fd) || tempFile );
55354 setSectorSize(pPager);
55355 if( !useJournal ){
55356 pPager->journalMode = PAGER_JOURNALMODE_OFF;
55357 }else if( memDb || memJM ){
55358 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
55359 }
55360 /* pPager->xBusyHandler = 0; */
55361 /* pPager->pBusyHandlerArg = 0; */
55362 pPager->xReiniter = xReinit;
55363 setGetterMethod(pPager);
55364 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
55365 /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
55366
55367 *ppPager = pPager;
55368 return SQLITE_OK;
55369}
55370
55371
55372
55373/*
55374** This function is called after transitioning from PAGER_UNLOCK to
55375** PAGER_SHARED state. It tests if there is a hot journal present in
55376** the file-system for the given pager. A hot journal is one that
55377** needs to be played back. According to this function, a hot-journal
55378** file exists if the following criteria are met:
55379**
55380** * The journal file exists in the file system, and
55381** * No process holds a RESERVED or greater lock on the database file, and
55382** * The database file itself is greater than 0 bytes in size, and
55383** * The first byte of the journal file exists and is not 0x00.
55384**
55385** If the current size of the database file is 0 but a journal file
55386** exists, that is probably an old journal left over from a prior
55387** database with the same name. In this case the journal file is
55388** just deleted using OsDelete, *pExists is set to 0 and SQLITE_OK
55389** is returned.
55390**
55391** This routine does not check if there is a master journal filename
55392** at the end of the file. If there is, and that master journal file
55393** does not exist, then the journal file is not really hot. In this
55394** case this routine will return a false-positive. The pager_playback()
55395** routine will discover that the journal file is not really hot and
55396** will not roll it back.
55397**
55398** If a hot-journal file is found to exist, *pExists is set to 1 and
55399** SQLITE_OK returned. If no hot-journal file is present, *pExists is
55400** set to 0 and SQLITE_OK returned. If an IO error occurs while trying
55401** to determine whether or not a hot-journal file exists, the IO error
55402** code is returned and the value of *pExists is undefined.
55403*/
55404static int hasHotJournal(Pager *pPager, int *pExists){
55405 sqlite3_vfs * const pVfs = pPager->pVfs;
55406 int rc = SQLITE_OK; /* Return code */
55407 int exists = 1; /* True if a journal file is present */
55408 int jrnlOpen = !!isOpen(pPager->jfd);
55409
55410 assert( pPager->useJournal );
55411 assert( isOpen(pPager->fd) );
55412 assert( pPager->eState==PAGER_OPEN );
55413
55414 assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
55415 SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
55416 ));
55417
55418 *pExists = 0;
55419 if( !jrnlOpen ){
55420 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
55421 }
55422 if( rc==SQLITE_OK && exists ){
55423 int locked = 0; /* True if some process holds a RESERVED lock */
55424
55425 /* Race condition here: Another process might have been holding the
55426 ** the RESERVED lock and have a journal open at the sqlite3OsAccess()
55427 ** call above, but then delete the journal and drop the lock before
55428 ** we get to the following sqlite3OsCheckReservedLock() call. If that
55429 ** is the case, this routine might think there is a hot journal when
55430 ** in fact there is none. This results in a false-positive which will
55431 ** be dealt with by the playback routine. Ticket #3883.
55432 */
55433 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
55434 if( rc==SQLITE_OK && !locked ){
55435 Pgno nPage; /* Number of pages in database file */
55436
55437 assert( pPager->tempFile==0 );
55438 rc = pagerPagecount(pPager, &nPage);
55439 if( rc==SQLITE_OK ){
55440 /* If the database is zero pages in size, that means that either (1) the
55441 ** journal is a remnant from a prior database with the same name where
55442 ** the database file but not the journal was deleted, or (2) the initial
55443 ** transaction that populates a new database is being rolled back.
55444 ** In either case, the journal file can be deleted. However, take care
55445 ** not to delete the journal file if it is already open due to
55446 ** journal_mode=PERSIST.
55447 */
55448 if( nPage==0 && !jrnlOpen ){
55449 sqlite3BeginBenignMalloc();
55450 if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){
55451 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
55452 if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
55453 }
55454 sqlite3EndBenignMalloc();
55455 }else{
55456 /* The journal file exists and no other connection has a reserved
55457 ** or greater lock on the database file. Now check that there is
55458 ** at least one non-zero bytes at the start of the journal file.
55459 ** If there is, then we consider this journal to be hot. If not,
55460 ** it can be ignored.
55461 */
55462 if( !jrnlOpen ){
55463 int f = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL;
55464 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
55465 }
55466 if( rc==SQLITE_OK ){
55467 u8 first = 0;
55468 rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
55469 if( rc==SQLITE_IOERR_SHORT_READ ){
55470 rc = SQLITE_OK;
55471 }
55472 if( !jrnlOpen ){
55473 sqlite3OsClose(pPager->jfd);
55474 }
55475 *pExists = (first!=0);
55476 }else if( rc==SQLITE_CANTOPEN ){
55477 /* If we cannot open the rollback journal file in order to see if
55478 ** it has a zero header, that might be due to an I/O error, or
55479 ** it might be due to the race condition described above and in
55480 ** ticket #3883. Either way, assume that the journal is hot.
55481 ** This might be a false positive. But if it is, then the
55482 ** automatic journal playback and recovery mechanism will deal
55483 ** with it under an EXCLUSIVE lock where we do not need to
55484 ** worry so much with race conditions.
55485 */
55486 *pExists = 1;
55487 rc = SQLITE_OK;
55488 }
55489 }
55490 }
55491 }
55492 }
55493
55494 return rc;
55495}
55496
55497/*
55498** This function is called to obtain a shared lock on the database file.
55499** It is illegal to call sqlite3PagerGet() until after this function
55500** has been successfully called. If a shared-lock is already held when
55501** this function is called, it is a no-op.
55502**
55503** The following operations are also performed by this function.
55504**
55505** 1) If the pager is currently in PAGER_OPEN state (no lock held
55506** on the database file), then an attempt is made to obtain a
55507** SHARED lock on the database file. Immediately after obtaining
55508** the SHARED lock, the file-system is checked for a hot-journal,
55509** which is played back if present. Following any hot-journal
55510** rollback, the contents of the cache are validated by checking
55511** the 'change-counter' field of the database file header and
55512** discarded if they are found to be invalid.
55513**
55514** 2) If the pager is running in exclusive-mode, and there are currently
55515** no outstanding references to any pages, and is in the error state,
55516** then an attempt is made to clear the error state by discarding
55517** the contents of the page cache and rolling back any open journal
55518** file.
55519**
55520** If everything is successful, SQLITE_OK is returned. If an IO error
55521** occurs while locking the database, checking for a hot-journal file or
55522** rolling back a journal file, the IO error code is returned.
55523*/
55524SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
55525 int rc = SQLITE_OK; /* Return code */
55526
55527 /* This routine is only called from b-tree and only when there are no
55528 ** outstanding pages. This implies that the pager state should either
55529 ** be OPEN or READER. READER is only possible if the pager is or was in
55530 ** exclusive access mode. */
55531 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
55532 assert( assert_pager_state(pPager) );
55533 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
55534 assert( pPager->errCode==SQLITE_OK );
55535
55536 if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
55537 int bHotJournal = 1; /* True if there exists a hot journal-file */
55538
55539 assert( !MEMDB );
55540 assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK );
55541
55542 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
55543 if( rc!=SQLITE_OK ){
55544 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
55545 goto failed;
55546 }
55547
55548 /* If a journal file exists, and there is no RESERVED lock on the
55549 ** database file, then it either needs to be played back or deleted.
55550 */
55551 if( pPager->eLock<=SHARED_LOCK ){
55552 rc = hasHotJournal(pPager, &bHotJournal);
55553 }
55554 if( rc!=SQLITE_OK ){
55555 goto failed;
55556 }
55557 if( bHotJournal ){
55558 if( pPager->readOnly ){
55559 rc = SQLITE_READONLY_ROLLBACK;
55560 goto failed;
55561 }
55562
55563 /* Get an EXCLUSIVE lock on the database file. At this point it is
55564 ** important that a RESERVED lock is not obtained on the way to the
55565 ** EXCLUSIVE lock. If it were, another process might open the
55566 ** database file, detect the RESERVED lock, and conclude that the
55567 ** database is safe to read while this process is still rolling the
55568 ** hot-journal back.
55569 **
55570 ** Because the intermediate RESERVED lock is not requested, any
55571 ** other process attempting to access the database file will get to
55572 ** this point in the code and fail to obtain its own EXCLUSIVE lock
55573 ** on the database file.
55574 **
55575 ** Unless the pager is in locking_mode=exclusive mode, the lock is
55576 ** downgraded to SHARED_LOCK before this function returns.
55577 */
55578 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
55579 if( rc!=SQLITE_OK ){
55580 goto failed;
55581 }
55582
55583 /* If it is not already open and the file exists on disk, open the
55584 ** journal for read/write access. Write access is required because
55585 ** in exclusive-access mode the file descriptor will be kept open
55586 ** and possibly used for a transaction later on. Also, write-access
55587 ** is usually required to finalize the journal in journal_mode=persist
55588 ** mode (and also for journal_mode=truncate on some systems).
55589 **
55590 ** If the journal does not exist, it usually means that some
55591 ** other connection managed to get in and roll it back before
55592 ** this connection obtained the exclusive lock above. Or, it
55593 ** may mean that the pager was in the error-state when this
55594 ** function was called and the journal file does not exist.
55595 */
55596 if( !isOpen(pPager->jfd) ){
55597 sqlite3_vfs * const pVfs = pPager->pVfs;
55598 int bExists; /* True if journal file exists */
55599 rc = sqlite3OsAccess(
55600 pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
55601 if( rc==SQLITE_OK && bExists ){
55602 int fout = 0;
55603 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
55604 assert( !pPager->tempFile );
55605 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
55606 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
55607 if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
55608 rc = SQLITE_CANTOPEN_BKPT;
55609 sqlite3OsClose(pPager->jfd);
55610 }
55611 }
55612 }
55613
55614 /* Playback and delete the journal. Drop the database write
55615 ** lock and reacquire the read lock. Purge the cache before
55616 ** playing back the hot-journal so that we don't end up with
55617 ** an inconsistent cache. Sync the hot journal before playing
55618 ** it back since the process that crashed and left the hot journal
55619 ** probably did not sync it and we are required to always sync
55620 ** the journal before playing it back.
55621 */
55622 if( isOpen(pPager->jfd) ){
55623 assert( rc==SQLITE_OK );
55624 rc = pagerSyncHotJournal(pPager);
55625 if( rc==SQLITE_OK ){
55626 rc = pager_playback(pPager, !pPager->tempFile);
55627 pPager->eState = PAGER_OPEN;
55628 }
55629 }else if( !pPager->exclusiveMode ){
55630 pagerUnlockDb(pPager, SHARED_LOCK);
55631 }
55632
55633 if( rc!=SQLITE_OK ){
55634 /* This branch is taken if an error occurs while trying to open
55635 ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
55636 ** pager_unlock() routine will be called before returning to unlock
55637 ** the file. If the unlock attempt fails, then Pager.eLock must be
55638 ** set to UNKNOWN_LOCK (see the comment above the #define for
55639 ** UNKNOWN_LOCK above for an explanation).
55640 **
55641 ** In order to get pager_unlock() to do this, set Pager.eState to
55642 ** PAGER_ERROR now. This is not actually counted as a transition
55643 ** to ERROR state in the state diagram at the top of this file,
55644 ** since we know that the same call to pager_unlock() will very
55645 ** shortly transition the pager object to the OPEN state. Calling
55646 ** assert_pager_state() would fail now, as it should not be possible
55647 ** to be in ERROR state when there are zero outstanding page
55648 ** references.
55649 */
55650 pager_error(pPager, rc);
55651 goto failed;
55652 }
55653
55654 assert( pPager->eState==PAGER_OPEN );
55655 assert( (pPager->eLock==SHARED_LOCK)
55656 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
55657 );
55658 }
55659
55660 if( !pPager->tempFile && pPager->hasHeldSharedLock ){
55661 /* The shared-lock has just been acquired then check to
55662 ** see if the database has been modified. If the database has changed,
55663 ** flush the cache. The hasHeldSharedLock flag prevents this from
55664 ** occurring on the very first access to a file, in order to save a
55665 ** single unnecessary sqlite3OsRead() call at the start-up.
55666 **
55667 ** Database changes are detected by looking at 15 bytes beginning
55668 ** at offset 24 into the file. The first 4 of these 16 bytes are
55669 ** a 32-bit counter that is incremented with each change. The
55670 ** other bytes change randomly with each file change when
55671 ** a codec is in use.
55672 **
55673 ** There is a vanishingly small chance that a change will not be
55674 ** detected. The chance of an undetected change is so small that
55675 ** it can be neglected.
55676 */
55677 char dbFileVers[sizeof(pPager->dbFileVers)];
55678
55679 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
55680 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
55681 if( rc!=SQLITE_OK ){
55682 if( rc!=SQLITE_IOERR_SHORT_READ ){
55683 goto failed;
55684 }
55685 memset(dbFileVers, 0, sizeof(dbFileVers));
55686 }
55687
55688 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
55689 pager_reset(pPager);
55690
55691 /* Unmap the database file. It is possible that external processes
55692 ** may have truncated the database file and then extended it back
55693 ** to its original size while this process was not holding a lock.
55694 ** In this case there may exist a Pager.pMap mapping that appears
55695 ** to be the right size but is not actually valid. Avoid this
55696 ** possibility by unmapping the db here. */
55697 if( USEFETCH(pPager) ){
55698 sqlite3OsUnfetch(pPager->fd, 0, 0);
55699 }
55700 }
55701 }
55702
55703 /* If there is a WAL file in the file-system, open this database in WAL
55704 ** mode. Otherwise, the following function call is a no-op.
55705 */
55706 rc = pagerOpenWalIfPresent(pPager);
55707#ifndef SQLITE_OMIT_WAL
55708 assert( pPager->pWal==0 || rc==SQLITE_OK );
55709#endif
55710 }
55711
55712 if( pagerUseWal(pPager) ){
55713 assert( rc==SQLITE_OK );
55714 rc = pagerBeginReadTransaction(pPager);
55715 }
55716
55717 if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
55718 rc = pagerPagecount(pPager, &pPager->dbSize);
55719 }
55720
55721 failed:
55722 if( rc!=SQLITE_OK ){
55723 assert( !MEMDB );
55724 pager_unlock(pPager);
55725 assert( pPager->eState==PAGER_OPEN );
55726 }else{
55727 pPager->eState = PAGER_READER;
55728 pPager->hasHeldSharedLock = 1;
55729 }
55730 return rc;
55731}
55732
55733/*
55734** If the reference count has reached zero, rollback any active
55735** transaction and unlock the pager.
55736**
55737** Except, in locking_mode=EXCLUSIVE when there is nothing to in
55738** the rollback journal, the unlock is not performed and there is
55739** nothing to rollback, so this routine is a no-op.
55740*/
55741static void pagerUnlockIfUnused(Pager *pPager){
55742 if( sqlite3PcacheRefCount(pPager->pPCache)==0 ){
55743 assert( pPager->nMmapOut==0 ); /* because page1 is never memory mapped */
55744 pagerUnlockAndRollback(pPager);
55745 }
55746}
55747
55748/*
55749** The page getter methods each try to acquire a reference to a
55750** page with page number pgno. If the requested reference is
55751** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
55752**
55753** There are different implementations of the getter method depending
55754** on the current state of the pager.
55755**
55756** getPageNormal() -- The normal getter
55757** getPageError() -- Used if the pager is in an error state
55758** getPageMmap() -- Used if memory-mapped I/O is enabled
55759**
55760** If the requested page is already in the cache, it is returned.
55761** Otherwise, a new page object is allocated and populated with data
55762** read from the database file. In some cases, the pcache module may
55763** choose not to allocate a new page object and may reuse an existing
55764** object with no outstanding references.
55765**
55766** The extra data appended to a page is always initialized to zeros the
55767** first time a page is loaded into memory. If the page requested is
55768** already in the cache when this function is called, then the extra
55769** data is left as it was when the page object was last used.
55770**
55771** If the database image is smaller than the requested page or if
55772** the flags parameter contains the PAGER_GET_NOCONTENT bit and the
55773** requested page is not already stored in the cache, then no
55774** actual disk read occurs. In this case the memory image of the
55775** page is initialized to all zeros.
55776**
55777** If PAGER_GET_NOCONTENT is true, it means that we do not care about
55778** the contents of the page. This occurs in two scenarios:
55779**
55780** a) When reading a free-list leaf page from the database, and
55781**
55782** b) When a savepoint is being rolled back and we need to load
55783** a new page into the cache to be filled with the data read
55784** from the savepoint journal.
55785**
55786** If PAGER_GET_NOCONTENT is true, then the data returned is zeroed instead
55787** of being read from the database. Additionally, the bits corresponding
55788** to pgno in Pager.pInJournal (bitvec of pages already written to the
55789** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
55790** savepoints are set. This means if the page is made writable at any
55791** point in the future, using a call to sqlite3PagerWrite(), its contents
55792** will not be journaled. This saves IO.
55793**
55794** The acquisition might fail for several reasons. In all cases,
55795** an appropriate error code is returned and *ppPage is set to NULL.
55796**
55797** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
55798** to find a page in the in-memory cache first. If the page is not already
55799** in memory, this routine goes to disk to read it in whereas Lookup()
55800** just returns 0. This routine acquires a read-lock the first time it
55801** has to go to disk, and could also playback an old journal if necessary.
55802** Since Lookup() never goes to disk, it never has to deal with locks
55803** or journal files.
55804*/
55805static int getPageNormal(
55806 Pager *pPager, /* The pager open on the database file */
55807 Pgno pgno, /* Page number to fetch */
55808 DbPage **ppPage, /* Write a pointer to the page here */
55809 int flags /* PAGER_GET_XXX flags */
55810){
55811 int rc = SQLITE_OK;
55812 PgHdr *pPg;
55813 u8 noContent; /* True if PAGER_GET_NOCONTENT is set */
55814 sqlite3_pcache_page *pBase;
55815
55816 assert( pPager->errCode==SQLITE_OK );
55817 assert( pPager->eState>=PAGER_READER );
55818 assert( assert_pager_state(pPager) );
55819 assert( pPager->hasHeldSharedLock==1 );
55820
55821 if( pgno==0 ) return SQLITE_CORRUPT_BKPT;
55822 pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
55823 if( pBase==0 ){
55824 pPg = 0;
55825 rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
55826 if( rc!=SQLITE_OK ) goto pager_acquire_err;
55827 if( pBase==0 ){
55828 rc = SQLITE_NOMEM_BKPT;
55829 goto pager_acquire_err;
55830 }
55831 }
55832 pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
55833 assert( pPg==(*ppPage) );
55834 assert( pPg->pgno==pgno );
55835 assert( pPg->pPager==pPager || pPg->pPager==0 );
55836
55837 noContent = (flags & PAGER_GET_NOCONTENT)!=0;
55838 if( pPg->pPager && !noContent ){
55839 /* In this case the pcache already contains an initialized copy of
55840 ** the page. Return without further ado. */
55841 assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
55842 pPager->aStat[PAGER_STAT_HIT]++;
55843 return SQLITE_OK;
55844
55845 }else{
55846 /* The pager cache has created a new page. Its content needs to
55847 ** be initialized. But first some error checks:
55848 **
55849 ** (1) The maximum page number is 2^31
55850 ** (2) Never try to fetch the locking page
55851 */
55852 if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
55853 rc = SQLITE_CORRUPT_BKPT;
55854 goto pager_acquire_err;
55855 }
55856
55857 pPg->pPager = pPager;
55858
55859 assert( !isOpen(pPager->fd) || !MEMDB );
55860 if( !isOpen(pPager->fd) || pPager->dbSize<pgno || noContent ){
55861 if( pgno>pPager->mxPgno ){
55862 rc = SQLITE_FULL;
55863 goto pager_acquire_err;
55864 }
55865 if( noContent ){
55866 /* Failure to set the bits in the InJournal bit-vectors is benign.
55867 ** It merely means that we might do some extra work to journal a
55868 ** page that does not need to be journaled. Nevertheless, be sure
55869 ** to test the case where a malloc error occurs while trying to set
55870 ** a bit in a bit vector.
55871 */
55872 sqlite3BeginBenignMalloc();
55873 if( pgno<=pPager->dbOrigSize ){
55874 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
55875 testcase( rc==SQLITE_NOMEM );
55876 }
55877 TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
55878 testcase( rc==SQLITE_NOMEM );
55879 sqlite3EndBenignMalloc();
55880 }
55881 memset(pPg->pData, 0, pPager->pageSize);
55882 IOTRACE(("ZERO %p %d\n", pPager, pgno));
55883 }else{
55884 assert( pPg->pPager==pPager );
55885 pPager->aStat[PAGER_STAT_MISS]++;
55886 rc = readDbPage(pPg);
55887 if( rc!=SQLITE_OK ){
55888 goto pager_acquire_err;
55889 }
55890 }
55891 pager_set_pagehash(pPg);
55892 }
55893 return SQLITE_OK;
55894
55895pager_acquire_err:
55896 assert( rc!=SQLITE_OK );
55897 if( pPg ){
55898 sqlite3PcacheDrop(pPg);
55899 }
55900 pagerUnlockIfUnused(pPager);
55901 *ppPage = 0;
55902 return rc;
55903}
55904
55905#if SQLITE_MAX_MMAP_SIZE>0
55906/* The page getter for when memory-mapped I/O is enabled */
55907static int getPageMMap(
55908 Pager *pPager, /* The pager open on the database file */
55909 Pgno pgno, /* Page number to fetch */
55910 DbPage **ppPage, /* Write a pointer to the page here */
55911 int flags /* PAGER_GET_XXX flags */
55912){
55913 int rc = SQLITE_OK;
55914 PgHdr *pPg = 0;
55915 u32 iFrame = 0; /* Frame to read from WAL file */
55916
55917 /* It is acceptable to use a read-only (mmap) page for any page except
55918 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
55919 ** flag was specified by the caller. And so long as the db is not a
55920 ** temporary or in-memory database. */
55921 const int bMmapOk = (pgno>1
55922 && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
55923 );
55924
55925 assert( USEFETCH(pPager) );
55926#ifdef SQLITE_HAS_CODEC
55927 assert( pPager->xCodec==0 );
55928#endif
55929
55930 /* Optimization note: Adding the "pgno<=1" term before "pgno==0" here
55931 ** allows the compiler optimizer to reuse the results of the "pgno>1"
55932 ** test in the previous statement, and avoid testing pgno==0 in the
55933 ** common case where pgno is large. */
55934 if( pgno<=1 && pgno==0 ){
55935 return SQLITE_CORRUPT_BKPT;
55936 }
55937 assert( pPager->eState>=PAGER_READER );
55938 assert( assert_pager_state(pPager) );
55939 assert( pPager->hasHeldSharedLock==1 );
55940 assert( pPager->errCode==SQLITE_OK );
55941
55942 if( bMmapOk && pagerUseWal(pPager) ){
55943 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
55944 if( rc!=SQLITE_OK ){
55945 *ppPage = 0;
55946 return rc;
55947 }
55948 }
55949 if( bMmapOk && iFrame==0 ){
55950 void *pData = 0;
55951 rc = sqlite3OsFetch(pPager->fd,
55952 (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
55953 );
55954 if( rc==SQLITE_OK && pData ){
55955 if( pPager->eState>PAGER_READER || pPager->tempFile ){
55956 pPg = sqlite3PagerLookup(pPager, pgno);
55957 }
55958 if( pPg==0 ){
55959 rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg);
55960 }else{
55961 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
55962 }
55963 if( pPg ){
55964 assert( rc==SQLITE_OK );
55965 *ppPage = pPg;
55966 return SQLITE_OK;
55967 }
55968 }
55969 if( rc!=SQLITE_OK ){
55970 *ppPage = 0;
55971 return rc;
55972 }
55973 }
55974 return getPageNormal(pPager, pgno, ppPage, flags);
55975}
55976#endif /* SQLITE_MAX_MMAP_SIZE>0 */
55977
55978/* The page getter method for when the pager is an error state */
55979static int getPageError(
55980 Pager *pPager, /* The pager open on the database file */
55981 Pgno pgno, /* Page number to fetch */
55982 DbPage **ppPage, /* Write a pointer to the page here */
55983 int flags /* PAGER_GET_XXX flags */
55984){
55985 UNUSED_PARAMETER(pgno);
55986 UNUSED_PARAMETER(flags);
55987 assert( pPager->errCode!=SQLITE_OK );
55988 *ppPage = 0;
55989 return pPager->errCode;
55990}
55991
55992
55993/* Dispatch all page fetch requests to the appropriate getter method.
55994*/
55995SQLITE_PRIVATE int sqlite3PagerGet(
55996 Pager *pPager, /* The pager open on the database file */
55997 Pgno pgno, /* Page number to fetch */
55998 DbPage **ppPage, /* Write a pointer to the page here */
55999 int flags /* PAGER_GET_XXX flags */
56000){
56001 return pPager->xGet(pPager, pgno, ppPage, flags);
56002}
56003
56004/*
56005** Acquire a page if it is already in the in-memory cache. Do
56006** not read the page from disk. Return a pointer to the page,
56007** or 0 if the page is not in cache.
56008**
56009** See also sqlite3PagerGet(). The difference between this routine
56010** and sqlite3PagerGet() is that _get() will go to the disk and read
56011** in the page if the page is not already in cache. This routine
56012** returns NULL if the page is not in cache or if a disk I/O error
56013** has ever happened.
56014*/
56015SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
56016 sqlite3_pcache_page *pPage;
56017 assert( pPager!=0 );
56018 assert( pgno!=0 );
56019 assert( pPager->pPCache!=0 );
56020 pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
56021 assert( pPage==0 || pPager->hasHeldSharedLock );
56022 if( pPage==0 ) return 0;
56023 return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
56024}
56025
56026/*
56027** Release a page reference.
56028**
56029** The sqlite3PagerUnref() and sqlite3PagerUnrefNotNull() may only be
56030** used if we know that the page being released is not the last page.
56031** The btree layer always holds page1 open until the end, so these first
56032** to routines can be used to release any page other than BtShared.pPage1.
56033**
56034** Use sqlite3PagerUnrefPageOne() to release page1. This latter routine
56035** checks the total number of outstanding pages and if the number of
56036** pages reaches zero it drops the database lock.
56037*/
56038SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage *pPg){
56039 TESTONLY( Pager *pPager = pPg->pPager; )
56040 assert( pPg!=0 );
56041 if( pPg->flags & PGHDR_MMAP ){
56042 assert( pPg->pgno!=1 ); /* Page1 is never memory mapped */
56043 pagerReleaseMapPage(pPg);
56044 }else{
56045 sqlite3PcacheRelease(pPg);
56046 }
56047 /* Do not use this routine to release the last reference to page1 */
56048 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
56049}
56050SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){
56051 if( pPg ) sqlite3PagerUnrefNotNull(pPg);
56052}
56053SQLITE_PRIVATE void sqlite3PagerUnrefPageOne(DbPage *pPg){
56054 Pager *pPager;
56055 assert( pPg!=0 );
56056 assert( pPg->pgno==1 );
56057 assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
56058 pPager = pPg->pPager;
56059 sqlite3PagerResetLockTimeout(pPager);
56060 sqlite3PcacheRelease(pPg);
56061 pagerUnlockIfUnused(pPager);
56062}
56063
56064/*
56065** This function is called at the start of every write transaction.
56066** There must already be a RESERVED or EXCLUSIVE lock on the database
56067** file when this routine is called.
56068**
56069** Open the journal file for pager pPager and write a journal header
56070** to the start of it. If there are active savepoints, open the sub-journal
56071** as well. This function is only used when the journal file is being
56072** opened to write a rollback log for a transaction. It is not used
56073** when opening a hot journal file to roll it back.
56074**
56075** If the journal file is already open (as it may be in exclusive mode),
56076** then this function just writes a journal header to the start of the
56077** already open file.
56078**
56079** Whether or not the journal file is opened by this function, the
56080** Pager.pInJournal bitvec structure is allocated.
56081**
56082** Return SQLITE_OK if everything is successful. Otherwise, return
56083** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or
56084** an IO error code if opening or writing the journal file fails.
56085*/
56086static int pager_open_journal(Pager *pPager){
56087 int rc = SQLITE_OK; /* Return code */
56088 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
56089
56090 assert( pPager->eState==PAGER_WRITER_LOCKED );
56091 assert( assert_pager_state(pPager) );
56092 assert( pPager->pInJournal==0 );
56093
56094 /* If already in the error state, this function is a no-op. But on
56095 ** the other hand, this routine is never called if we are already in
56096 ** an error state. */
56097 if( NEVER(pPager->errCode) ) return pPager->errCode;
56098
56099 if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
56100 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
56101 if( pPager->pInJournal==0 ){
56102 return SQLITE_NOMEM_BKPT;
56103 }
56104
56105 /* Open the journal file if it is not already open. */
56106 if( !isOpen(pPager->jfd) ){
56107 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
56108 sqlite3MemJournalOpen(pPager->jfd);
56109 }else{
56110 int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
56111 int nSpill;
56112
56113 if( pPager->tempFile ){
56114 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
56115 nSpill = sqlite3Config.nStmtSpill;
56116 }else{
56117 flags |= SQLITE_OPEN_MAIN_JOURNAL;
56118 nSpill = jrnlBufferSize(pPager);
56119 }
56120
56121 /* Verify that the database still has the same name as it did when
56122 ** it was originally opened. */
56123 rc = databaseIsUnmoved(pPager);
56124 if( rc==SQLITE_OK ){
56125 rc = sqlite3JournalOpen (
56126 pVfs, pPager->zJournal, pPager->jfd, flags, nSpill
56127 );
56128 }
56129 }
56130 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
56131 }
56132
56133
56134 /* Write the first journal header to the journal file and open
56135 ** the sub-journal if necessary.
56136 */
56137 if( rc==SQLITE_OK ){
56138 /* TODO: Check if all of these are really required. */
56139 pPager->nRec = 0;
56140 pPager->journalOff = 0;
56141 pPager->setMaster = 0;
56142 pPager->journalHdr = 0;
56143 rc = writeJournalHdr(pPager);
56144 }
56145 }
56146
56147 if( rc!=SQLITE_OK ){
56148 sqlite3BitvecDestroy(pPager->pInJournal);
56149 pPager->pInJournal = 0;
56150 }else{
56151 assert( pPager->eState==PAGER_WRITER_LOCKED );
56152 pPager->eState = PAGER_WRITER_CACHEMOD;
56153 }
56154
56155 return rc;
56156}
56157
56158/*
56159** Begin a write-transaction on the specified pager object. If a
56160** write-transaction has already been opened, this function is a no-op.
56161**
56162** If the exFlag argument is false, then acquire at least a RESERVED
56163** lock on the database file. If exFlag is true, then acquire at least
56164** an EXCLUSIVE lock. If such a lock is already held, no locking
56165** functions need be called.
56166**
56167** If the subjInMemory argument is non-zero, then any sub-journal opened
56168** within this transaction will be opened as an in-memory file. This
56169** has no effect if the sub-journal is already opened (as it may be when
56170** running in exclusive mode) or if the transaction does not require a
56171** sub-journal. If the subjInMemory argument is zero, then any required
56172** sub-journal is implemented in-memory if pPager is an in-memory database,
56173** or using a temporary file otherwise.
56174*/
56175SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
56176 int rc = SQLITE_OK;
56177
56178 if( pPager->errCode ) return pPager->errCode;
56179 assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
56180 pPager->subjInMemory = (u8)subjInMemory;
56181
56182 if( ALWAYS(pPager->eState==PAGER_READER) ){
56183 assert( pPager->pInJournal==0 );
56184
56185 if( pagerUseWal(pPager) ){
56186 /* If the pager is configured to use locking_mode=exclusive, and an
56187 ** exclusive lock on the database is not already held, obtain it now.
56188 */
56189 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
56190 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
56191 if( rc!=SQLITE_OK ){
56192 return rc;
56193 }
56194 (void)sqlite3WalExclusiveMode(pPager->pWal, 1);
56195 }
56196
56197 /* Grab the write lock on the log file. If successful, upgrade to
56198 ** PAGER_RESERVED state. Otherwise, return an error code to the caller.
56199 ** The busy-handler is not invoked if another connection already
56200 ** holds the write-lock. If possible, the upper layer will call it.
56201 */
56202 rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
56203 }else{
56204 /* Obtain a RESERVED lock on the database file. If the exFlag parameter
56205 ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
56206 ** busy-handler callback can be used when upgrading to the EXCLUSIVE
56207 ** lock, but not when obtaining the RESERVED lock.
56208 */
56209 rc = pagerLockDb(pPager, RESERVED_LOCK);
56210 if( rc==SQLITE_OK && exFlag ){
56211 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
56212 }
56213 }
56214
56215 if( rc==SQLITE_OK ){
56216 /* Change to WRITER_LOCKED state.
56217 **
56218 ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD
56219 ** when it has an open transaction, but never to DBMOD or FINISHED.
56220 ** This is because in those states the code to roll back savepoint
56221 ** transactions may copy data from the sub-journal into the database
56222 ** file as well as into the page cache. Which would be incorrect in
56223 ** WAL mode.
56224 */
56225 pPager->eState = PAGER_WRITER_LOCKED;
56226 pPager->dbHintSize = pPager->dbSize;
56227 pPager->dbFileSize = pPager->dbSize;
56228 pPager->dbOrigSize = pPager->dbSize;
56229 pPager->journalOff = 0;
56230 }
56231
56232 assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
56233 assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
56234 assert( assert_pager_state(pPager) );
56235 }
56236
56237 PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
56238 return rc;
56239}
56240
56241/*
56242** Write page pPg onto the end of the rollback journal.
56243*/
56244static SQLITE_NOINLINE int pagerAddPageToRollbackJournal(PgHdr *pPg){
56245 Pager *pPager = pPg->pPager;
56246 int rc;
56247 u32 cksum;
56248 char *pData2;
56249 i64 iOff = pPager->journalOff;
56250
56251 /* We should never write to the journal file the page that
56252 ** contains the database locks. The following assert verifies
56253 ** that we do not. */
56254 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
56255
56256 assert( pPager->journalHdr<=pPager->journalOff );
56257 CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2);
56258 cksum = pager_cksum(pPager, (u8*)pData2);
56259
56260 /* Even if an IO or diskfull error occurs while journalling the
56261 ** page in the block above, set the need-sync flag for the page.
56262 ** Otherwise, when the transaction is rolled back, the logic in
56263 ** playback_one_page() will think that the page needs to be restored
56264 ** in the database file. And if an IO error occurs while doing so,
56265 ** then corruption may follow.
56266 */
56267 pPg->flags |= PGHDR_NEED_SYNC;
56268
56269 rc = write32bits(pPager->jfd, iOff, pPg->pgno);
56270 if( rc!=SQLITE_OK ) return rc;
56271 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
56272 if( rc!=SQLITE_OK ) return rc;
56273 rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
56274 if( rc!=SQLITE_OK ) return rc;
56275
56276 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
56277 pPager->journalOff, pPager->pageSize));
56278 PAGER_INCR(sqlite3_pager_writej_count);
56279 PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
56280 PAGERID(pPager), pPg->pgno,
56281 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
56282
56283 pPager->journalOff += 8 + pPager->pageSize;
56284 pPager->nRec++;
56285 assert( pPager->pInJournal!=0 );
56286 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
56287 testcase( rc==SQLITE_NOMEM );
56288 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
56289 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
56290 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
56291 return rc;
56292}
56293
56294/*
56295** Mark a single data page as writeable. The page is written into the
56296** main journal or sub-journal as required. If the page is written into
56297** one of the journals, the corresponding bit is set in the
56298** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs
56299** of any open savepoints as appropriate.
56300*/
56301static int pager_write(PgHdr *pPg){
56302 Pager *pPager = pPg->pPager;
56303 int rc = SQLITE_OK;
56304
56305 /* This routine is not called unless a write-transaction has already
56306 ** been started. The journal file may or may not be open at this point.
56307 ** It is never called in the ERROR state.
56308 */
56309 assert( pPager->eState==PAGER_WRITER_LOCKED
56310 || pPager->eState==PAGER_WRITER_CACHEMOD
56311 || pPager->eState==PAGER_WRITER_DBMOD
56312 );
56313 assert( assert_pager_state(pPager) );
56314 assert( pPager->errCode==0 );
56315 assert( pPager->readOnly==0 );
56316 CHECK_PAGE(pPg);
56317
56318 /* The journal file needs to be opened. Higher level routines have already
56319 ** obtained the necessary locks to begin the write-transaction, but the
56320 ** rollback journal might not yet be open. Open it now if this is the case.
56321 **
56322 ** This is done before calling sqlite3PcacheMakeDirty() on the page.
56323 ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then
56324 ** an error might occur and the pager would end up in WRITER_LOCKED state
56325 ** with pages marked as dirty in the cache.
56326 */
56327 if( pPager->eState==PAGER_WRITER_LOCKED ){
56328 rc = pager_open_journal(pPager);
56329 if( rc!=SQLITE_OK ) return rc;
56330 }
56331 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
56332 assert( assert_pager_state(pPager) );
56333
56334 /* Mark the page that is about to be modified as dirty. */
56335 sqlite3PcacheMakeDirty(pPg);
56336
56337 /* If a rollback journal is in use, them make sure the page that is about
56338 ** to change is in the rollback journal, or if the page is a new page off
56339 ** then end of the file, make sure it is marked as PGHDR_NEED_SYNC.
56340 */
56341 assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) );
56342 if( pPager->pInJournal!=0
56343 && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0
56344 ){
56345 assert( pagerUseWal(pPager)==0 );
56346 if( pPg->pgno<=pPager->dbOrigSize ){
56347 rc = pagerAddPageToRollbackJournal(pPg);
56348 if( rc!=SQLITE_OK ){
56349 return rc;
56350 }
56351 }else{
56352 if( pPager->eState!=PAGER_WRITER_DBMOD ){
56353 pPg->flags |= PGHDR_NEED_SYNC;
56354 }
56355 PAGERTRACE(("APPEND %d page %d needSync=%d\n",
56356 PAGERID(pPager), pPg->pgno,
56357 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
56358 }
56359 }
56360
56361 /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
56362 ** and before writing the page into the rollback journal. Wait until now,
56363 ** after the page has been successfully journalled, before setting the
56364 ** PGHDR_WRITEABLE bit that indicates that the page can be safely modified.
56365 */
56366 pPg->flags |= PGHDR_WRITEABLE;
56367
56368 /* If the statement journal is open and the page is not in it,
56369 ** then write the page into the statement journal.
56370 */
56371 if( pPager->nSavepoint>0 ){
56372 rc = subjournalPageIfRequired(pPg);
56373 }
56374
56375 /* Update the database size and return. */
56376 if( pPager->dbSize<pPg->pgno ){
56377 pPager->dbSize = pPg->pgno;
56378 }
56379 return rc;
56380}
56381
56382/*
56383** This is a variant of sqlite3PagerWrite() that runs when the sector size
56384** is larger than the page size. SQLite makes the (reasonable) assumption that
56385** all bytes of a sector are written together by hardware. Hence, all bytes of
56386** a sector need to be journalled in case of a power loss in the middle of
56387** a write.
56388**
56389** Usually, the sector size is less than or equal to the page size, in which
56390** case pages can be individually written. This routine only runs in the
56391** exceptional case where the page size is smaller than the sector size.
56392*/
56393static SQLITE_NOINLINE int pagerWriteLargeSector(PgHdr *pPg){
56394 int rc = SQLITE_OK; /* Return code */
56395 Pgno nPageCount; /* Total number of pages in database file */
56396 Pgno pg1; /* First page of the sector pPg is located on. */
56397 int nPage = 0; /* Number of pages starting at pg1 to journal */
56398 int ii; /* Loop counter */
56399 int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */
56400 Pager *pPager = pPg->pPager; /* The pager that owns pPg */
56401 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
56402
56403 /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
56404 ** a journal header to be written between the pages journaled by
56405 ** this function.
56406 */
56407 assert( !MEMDB );
56408 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
56409 pPager->doNotSpill |= SPILLFLAG_NOSYNC;
56410
56411 /* This trick assumes that both the page-size and sector-size are
56412 ** an integer power of 2. It sets variable pg1 to the identifier
56413 ** of the first page of the sector pPg is located on.
56414 */
56415 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
56416
56417 nPageCount = pPager->dbSize;
56418 if( pPg->pgno>nPageCount ){
56419 nPage = (pPg->pgno - pg1)+1;
56420 }else if( (pg1+nPagePerSector-1)>nPageCount ){
56421 nPage = nPageCount+1-pg1;
56422 }else{
56423 nPage = nPagePerSector;
56424 }
56425 assert(nPage>0);
56426 assert(pg1<=pPg->pgno);
56427 assert((pg1+nPage)>pPg->pgno);
56428
56429 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
56430 Pgno pg = pg1+ii;
56431 PgHdr *pPage;
56432 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
56433 if( pg!=PAGER_MJ_PGNO(pPager) ){
56434 rc = sqlite3PagerGet(pPager, pg, &pPage, 0);
56435 if( rc==SQLITE_OK ){
56436 rc = pager_write(pPage);
56437 if( pPage->flags&PGHDR_NEED_SYNC ){
56438 needSync = 1;
56439 }
56440 sqlite3PagerUnrefNotNull(pPage);
56441 }
56442 }
56443 }else if( (pPage = sqlite3PagerLookup(pPager, pg))!=0 ){
56444 if( pPage->flags&PGHDR_NEED_SYNC ){
56445 needSync = 1;
56446 }
56447 sqlite3PagerUnrefNotNull(pPage);
56448 }
56449 }
56450
56451 /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages
56452 ** starting at pg1, then it needs to be set for all of them. Because
56453 ** writing to any of these nPage pages may damage the others, the
56454 ** journal file must contain sync()ed copies of all of them
56455 ** before any of them can be written out to the database file.
56456 */
56457 if( rc==SQLITE_OK && needSync ){
56458 assert( !MEMDB );
56459 for(ii=0; ii<nPage; ii++){
56460 PgHdr *pPage = sqlite3PagerLookup(pPager, pg1+ii);
56461 if( pPage ){
56462 pPage->flags |= PGHDR_NEED_SYNC;
56463 sqlite3PagerUnrefNotNull(pPage);
56464 }
56465 }
56466 }
56467
56468 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
56469 pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
56470 return rc;
56471}
56472
56473/*
56474** Mark a data page as writeable. This routine must be called before
56475** making changes to a page. The caller must check the return value
56476** of this function and be careful not to change any page data unless
56477** this routine returns SQLITE_OK.
56478**
56479** The difference between this function and pager_write() is that this
56480** function also deals with the special case where 2 or more pages
56481** fit on a single disk sector. In this case all co-resident pages
56482** must have been written to the journal file before returning.
56483**
56484** If an error occurs, SQLITE_NOMEM or an IO error code is returned
56485** as appropriate. Otherwise, SQLITE_OK.
56486*/
56487SQLITE_PRIVATE int sqlite3PagerWrite(PgHdr *pPg){
56488 Pager *pPager = pPg->pPager;
56489 assert( (pPg->flags & PGHDR_MMAP)==0 );
56490 assert( pPager->eState>=PAGER_WRITER_LOCKED );
56491 assert( assert_pager_state(pPager) );
56492 if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){
56493 if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
56494 return SQLITE_OK;
56495 }else if( pPager->errCode ){
56496 return pPager->errCode;
56497 }else if( pPager->sectorSize > (u32)pPager->pageSize ){
56498 assert( pPager->tempFile==0 );
56499 return pagerWriteLargeSector(pPg);
56500 }else{
56501 return pager_write(pPg);
56502 }
56503}
56504
56505/*
56506** Return TRUE if the page given in the argument was previously passed
56507** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
56508** to change the content of the page.
56509*/
56510#ifndef NDEBUG
56511SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){
56512 return pPg->flags & PGHDR_WRITEABLE;
56513}
56514#endif
56515
56516/*
56517** A call to this routine tells the pager that it is not necessary to
56518** write the information on page pPg back to the disk, even though
56519** that page might be marked as dirty. This happens, for example, when
56520** the page has been added as a leaf of the freelist and so its
56521** content no longer matters.
56522**
56523** The overlying software layer calls this routine when all of the data
56524** on the given page is unused. The pager marks the page as clean so
56525** that it does not get written to disk.
56526**
56527** Tests show that this optimization can quadruple the speed of large
56528** DELETE operations.
56529**
56530** This optimization cannot be used with a temp-file, as the page may
56531** have been dirty at the start of the transaction. In that case, if
56532** memory pressure forces page pPg out of the cache, the data does need
56533** to be written out to disk so that it may be read back in if the
56534** current transaction is rolled back.
56535*/
56536SQLITE_PRIVATE void sqlite3PagerDontWrite(PgHdr *pPg){
56537 Pager *pPager = pPg->pPager;
56538 if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
56539 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
56540 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
56541 pPg->flags |= PGHDR_DONT_WRITE;
56542 pPg->flags &= ~PGHDR_WRITEABLE;
56543 testcase( pPg->flags & PGHDR_NEED_SYNC );
56544 pager_set_pagehash(pPg);
56545 }
56546}
56547
56548/*
56549** This routine is called to increment the value of the database file
56550** change-counter, stored as a 4-byte big-endian integer starting at
56551** byte offset 24 of the pager file. The secondary change counter at
56552** 92 is also updated, as is the SQLite version number at offset 96.
56553**
56554** But this only happens if the pPager->changeCountDone flag is false.
56555** To avoid excess churning of page 1, the update only happens once.
56556** See also the pager_write_changecounter() routine that does an
56557** unconditional update of the change counters.
56558**
56559** If the isDirectMode flag is zero, then this is done by calling
56560** sqlite3PagerWrite() on page 1, then modifying the contents of the
56561** page data. In this case the file will be updated when the current
56562** transaction is committed.
56563**
56564** The isDirectMode flag may only be non-zero if the library was compiled
56565** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case,
56566** if isDirect is non-zero, then the database file is updated directly
56567** by writing an updated version of page 1 using a call to the
56568** sqlite3OsWrite() function.
56569*/
56570static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
56571 int rc = SQLITE_OK;
56572
56573 assert( pPager->eState==PAGER_WRITER_CACHEMOD
56574 || pPager->eState==PAGER_WRITER_DBMOD
56575 );
56576 assert( assert_pager_state(pPager) );
56577
56578 /* Declare and initialize constant integer 'isDirect'. If the
56579 ** atomic-write optimization is enabled in this build, then isDirect
56580 ** is initialized to the value passed as the isDirectMode parameter
56581 ** to this function. Otherwise, it is always set to zero.
56582 **
56583 ** The idea is that if the atomic-write optimization is not
56584 ** enabled at compile time, the compiler can omit the tests of
56585 ** 'isDirect' below, as well as the block enclosed in the
56586 ** "if( isDirect )" condition.
56587 */
56588#ifndef SQLITE_ENABLE_ATOMIC_WRITE
56589# define DIRECT_MODE 0
56590 assert( isDirectMode==0 );
56591 UNUSED_PARAMETER(isDirectMode);
56592#else
56593# define DIRECT_MODE isDirectMode
56594#endif
56595
56596 if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0) ){
56597 PgHdr *pPgHdr; /* Reference to page 1 */
56598
56599 assert( !pPager->tempFile && isOpen(pPager->fd) );
56600
56601 /* Open page 1 of the file for writing. */
56602 rc = sqlite3PagerGet(pPager, 1, &pPgHdr, 0);
56603 assert( pPgHdr==0 || rc==SQLITE_OK );
56604
56605 /* If page one was fetched successfully, and this function is not
56606 ** operating in direct-mode, make page 1 writable. When not in
56607 ** direct mode, page 1 is always held in cache and hence the PagerGet()
56608 ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
56609 */
56610 if( !DIRECT_MODE && ALWAYS(rc==SQLITE_OK) ){
56611 rc = sqlite3PagerWrite(pPgHdr);
56612 }
56613
56614 if( rc==SQLITE_OK ){
56615 /* Actually do the update of the change counter */
56616 pager_write_changecounter(pPgHdr);
56617
56618 /* If running in direct mode, write the contents of page 1 to the file. */
56619 if( DIRECT_MODE ){
56620 const void *zBuf;
56621 assert( pPager->dbFileSize>0 );
56622 CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM_BKPT, zBuf);
56623 if( rc==SQLITE_OK ){
56624 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
56625 pPager->aStat[PAGER_STAT_WRITE]++;
56626 }
56627 if( rc==SQLITE_OK ){
56628 /* Update the pager's copy of the change-counter. Otherwise, the
56629 ** next time a read transaction is opened the cache will be
56630 ** flushed (as the change-counter values will not match). */
56631 const void *pCopy = (const void *)&((const char *)zBuf)[24];
56632 memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers));
56633 pPager->changeCountDone = 1;
56634 }
56635 }else{
56636 pPager->changeCountDone = 1;
56637 }
56638 }
56639
56640 /* Release the page reference. */
56641 sqlite3PagerUnref(pPgHdr);
56642 }
56643 return rc;
56644}
56645
56646/*
56647** Sync the database file to disk. This is a no-op for in-memory databases
56648** or pages with the Pager.noSync flag set.
56649**
56650** If successful, or if called on a pager for which it is a no-op, this
56651** function returns SQLITE_OK. Otherwise, an IO error code is returned.
56652*/
56653SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster){
56654 int rc = SQLITE_OK;
56655 void *pArg = (void*)zMaster;
56656 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
56657 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
56658 if( rc==SQLITE_OK && !pPager->noSync ){
56659 assert( !MEMDB );
56660 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
56661 }
56662 return rc;
56663}
56664
56665/*
56666** This function may only be called while a write-transaction is active in
56667** rollback. If the connection is in WAL mode, this call is a no-op.
56668** Otherwise, if the connection does not already have an EXCLUSIVE lock on
56669** the database file, an attempt is made to obtain one.
56670**
56671** If the EXCLUSIVE lock is already held or the attempt to obtain it is
56672** successful, or the connection is in WAL mode, SQLITE_OK is returned.
56673** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is
56674** returned.
56675*/
56676SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){
56677 int rc = pPager->errCode;
56678 assert( assert_pager_state(pPager) );
56679 if( rc==SQLITE_OK ){
56680 assert( pPager->eState==PAGER_WRITER_CACHEMOD
56681 || pPager->eState==PAGER_WRITER_DBMOD
56682 || pPager->eState==PAGER_WRITER_LOCKED
56683 );
56684 assert( assert_pager_state(pPager) );
56685 if( 0==pagerUseWal(pPager) ){
56686 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
56687 }
56688 }
56689 return rc;
56690}
56691
56692/*
56693** Sync the database file for the pager pPager. zMaster points to the name
56694** of a master journal file that should be written into the individual
56695** journal file. zMaster may be NULL, which is interpreted as no master
56696** journal (a single database transaction).
56697**
56698** This routine ensures that:
56699**
56700** * The database file change-counter is updated,
56701** * the journal is synced (unless the atomic-write optimization is used),
56702** * all dirty pages are written to the database file,
56703** * the database file is truncated (if required), and
56704** * the database file synced.
56705**
56706** The only thing that remains to commit the transaction is to finalize
56707** (delete, truncate or zero the first part of) the journal file (or
56708** delete the master journal file if specified).
56709**
56710** Note that if zMaster==NULL, this does not overwrite a previous value
56711** passed to an sqlite3PagerCommitPhaseOne() call.
56712**
56713** If the final parameter - noSync - is true, then the database file itself
56714** is not synced. The caller must call sqlite3PagerSync() directly to
56715** sync the database file before calling CommitPhaseTwo() to delete the
56716** journal file in this case.
56717*/
56718SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(
56719 Pager *pPager, /* Pager object */
56720 const char *zMaster, /* If not NULL, the master journal name */
56721 int noSync /* True to omit the xSync on the db file */
56722){
56723 int rc = SQLITE_OK; /* Return code */
56724
56725 assert( pPager->eState==PAGER_WRITER_LOCKED
56726 || pPager->eState==PAGER_WRITER_CACHEMOD
56727 || pPager->eState==PAGER_WRITER_DBMOD
56728 || pPager->eState==PAGER_ERROR
56729 );
56730 assert( assert_pager_state(pPager) );
56731
56732 /* If a prior error occurred, report that error again. */
56733 if( NEVER(pPager->errCode) ) return pPager->errCode;
56734
56735 /* Provide the ability to easily simulate an I/O error during testing */
56736 if( sqlite3FaultSim(400) ) return SQLITE_IOERR;
56737
56738 PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n",
56739 pPager->zFilename, zMaster, pPager->dbSize));
56740
56741 /* If no database changes have been made, return early. */
56742 if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
56743
56744 assert( MEMDB==0 || pPager->tempFile );
56745 assert( isOpen(pPager->fd) || pPager->tempFile );
56746 if( 0==pagerFlushOnCommit(pPager, 1) ){
56747 /* If this is an in-memory db, or no pages have been written to, or this
56748 ** function has already been called, it is mostly a no-op. However, any
56749 ** backup in progress needs to be restarted. */
56750 sqlite3BackupRestart(pPager->pBackup);
56751 }else{
56752 PgHdr *pList;
56753 if( pagerUseWal(pPager) ){
56754 PgHdr *pPageOne = 0;
56755 pList = sqlite3PcacheDirtyList(pPager->pPCache);
56756 if( pList==0 ){
56757 /* Must have at least one page for the WAL commit flag.
56758 ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
56759 rc = sqlite3PagerGet(pPager, 1, &pPageOne, 0);
56760 pList = pPageOne;
56761 pList->pDirty = 0;
56762 }
56763 assert( rc==SQLITE_OK );
56764 if( ALWAYS(pList) ){
56765 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
56766 }
56767 sqlite3PagerUnref(pPageOne);
56768 if( rc==SQLITE_OK ){
56769 sqlite3PcacheCleanAll(pPager->pPCache);
56770 }
56771 }else{
56772 /* The bBatch boolean is true if the batch-atomic-write commit method
56773 ** should be used. No rollback journal is created if batch-atomic-write
56774 ** is enabled.
56775 */
56776#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
56777 sqlite3_file *fd = pPager->fd;
56778 int bBatch = zMaster==0 /* An SQLITE_IOCAP_BATCH_ATOMIC commit */
56779 && (sqlite3OsDeviceCharacteristics(fd) & SQLITE_IOCAP_BATCH_ATOMIC)
56780 && !pPager->noSync
56781 && sqlite3JournalIsInMemory(pPager->jfd);
56782#else
56783# define bBatch 0
56784#endif
56785
56786#ifdef SQLITE_ENABLE_ATOMIC_WRITE
56787 /* The following block updates the change-counter. Exactly how it
56788 ** does this depends on whether or not the atomic-update optimization
56789 ** was enabled at compile time, and if this transaction meets the
56790 ** runtime criteria to use the operation:
56791 **
56792 ** * The file-system supports the atomic-write property for
56793 ** blocks of size page-size, and
56794 ** * This commit is not part of a multi-file transaction, and
56795 ** * Exactly one page has been modified and store in the journal file.
56796 **
56797 ** If the optimization was not enabled at compile time, then the
56798 ** pager_incr_changecounter() function is called to update the change
56799 ** counter in 'indirect-mode'. If the optimization is compiled in but
56800 ** is not applicable to this transaction, call sqlite3JournalCreate()
56801 ** to make sure the journal file has actually been created, then call
56802 ** pager_incr_changecounter() to update the change-counter in indirect
56803 ** mode.
56804 **
56805 ** Otherwise, if the optimization is both enabled and applicable,
56806 ** then call pager_incr_changecounter() to update the change-counter
56807 ** in 'direct' mode. In this case the journal file will never be
56808 ** created for this transaction.
56809 */
56810 if( bBatch==0 ){
56811 PgHdr *pPg;
56812 assert( isOpen(pPager->jfd)
56813 || pPager->journalMode==PAGER_JOURNALMODE_OFF
56814 || pPager->journalMode==PAGER_JOURNALMODE_WAL
56815 );
56816 if( !zMaster && isOpen(pPager->jfd)
56817 && pPager->journalOff==jrnlBufferSize(pPager)
56818 && pPager->dbSize>=pPager->dbOrigSize
56819 && (!(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
56820 ){
56821 /* Update the db file change counter via the direct-write method. The
56822 ** following call will modify the in-memory representation of page 1
56823 ** to include the updated change counter and then write page 1
56824 ** directly to the database file. Because of the atomic-write
56825 ** property of the host file-system, this is safe.
56826 */
56827 rc = pager_incr_changecounter(pPager, 1);
56828 }else{
56829 rc = sqlite3JournalCreate(pPager->jfd);
56830 if( rc==SQLITE_OK ){
56831 rc = pager_incr_changecounter(pPager, 0);
56832 }
56833 }
56834 }
56835#else /* SQLITE_ENABLE_ATOMIC_WRITE */
56836#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
56837 if( zMaster ){
56838 rc = sqlite3JournalCreate(pPager->jfd);
56839 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
56840 assert( bBatch==0 );
56841 }
56842#endif
56843 rc = pager_incr_changecounter(pPager, 0);
56844#endif /* !SQLITE_ENABLE_ATOMIC_WRITE */
56845 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
56846
56847 /* Write the master journal name into the journal file. If a master
56848 ** journal file name has already been written to the journal file,
56849 ** or if zMaster is NULL (no master journal), then this call is a no-op.
56850 */
56851 rc = writeMasterJournal(pPager, zMaster);
56852 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
56853
56854 /* Sync the journal file and write all dirty pages to the database.
56855 ** If the atomic-update optimization is being used, this sync will not
56856 ** create the journal file or perform any real IO.
56857 **
56858 ** Because the change-counter page was just modified, unless the
56859 ** atomic-update optimization is used it is almost certain that the
56860 ** journal requires a sync here. However, in locking_mode=exclusive
56861 ** on a system under memory pressure it is just possible that this is
56862 ** not the case. In this case it is likely enough that the redundant
56863 ** xSync() call will be changed to a no-op by the OS anyhow.
56864 */
56865 rc = syncJournal(pPager, 0);
56866 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
56867
56868 pList = sqlite3PcacheDirtyList(pPager->pPCache);
56869#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
56870 if( bBatch ){
56871 rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_BEGIN_ATOMIC_WRITE, 0);
56872 if( rc==SQLITE_OK ){
56873 rc = pager_write_pagelist(pPager, pList);
56874 if( rc==SQLITE_OK ){
56875 rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_COMMIT_ATOMIC_WRITE, 0);
56876 }
56877 if( rc!=SQLITE_OK ){
56878 sqlite3OsFileControlHint(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE, 0);
56879 }
56880 }
56881
56882 if( (rc&0xFF)==SQLITE_IOERR && rc!=SQLITE_IOERR_NOMEM ){
56883 rc = sqlite3JournalCreate(pPager->jfd);
56884 if( rc!=SQLITE_OK ){
56885 sqlite3OsClose(pPager->jfd);
56886 goto commit_phase_one_exit;
56887 }
56888 bBatch = 0;
56889 }else{
56890 sqlite3OsClose(pPager->jfd);
56891 }
56892 }
56893#endif /* SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
56894
56895 if( bBatch==0 ){
56896 rc = pager_write_pagelist(pPager, pList);
56897 }
56898 if( rc!=SQLITE_OK ){
56899 assert( rc!=SQLITE_IOERR_BLOCKED );
56900 goto commit_phase_one_exit;
56901 }
56902 sqlite3PcacheCleanAll(pPager->pPCache);
56903
56904 /* If the file on disk is smaller than the database image, use
56905 ** pager_truncate to grow the file here. This can happen if the database
56906 ** image was extended as part of the current transaction and then the
56907 ** last page in the db image moved to the free-list. In this case the
56908 ** last page is never written out to disk, leaving the database file
56909 ** undersized. Fix this now if it is the case. */
56910 if( pPager->dbSize>pPager->dbFileSize ){
56911 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
56912 assert( pPager->eState==PAGER_WRITER_DBMOD );
56913 rc = pager_truncate(pPager, nNew);
56914 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
56915 }
56916
56917 /* Finally, sync the database file. */
56918 if( !noSync ){
56919 rc = sqlite3PagerSync(pPager, zMaster);
56920 }
56921 IOTRACE(("DBSYNC %p\n", pPager))
56922 }
56923 }
56924
56925commit_phase_one_exit:
56926 if( rc==SQLITE_OK && !pagerUseWal(pPager) ){
56927 pPager->eState = PAGER_WRITER_FINISHED;
56928 }
56929 return rc;
56930}
56931
56932
56933/*
56934** When this function is called, the database file has been completely
56935** updated to reflect the changes made by the current transaction and
56936** synced to disk. The journal file still exists in the file-system
56937** though, and if a failure occurs at this point it will eventually
56938** be used as a hot-journal and the current transaction rolled back.
56939**
56940** This function finalizes the journal file, either by deleting,
56941** truncating or partially zeroing it, so that it cannot be used
56942** for hot-journal rollback. Once this is done the transaction is
56943** irrevocably committed.
56944**
56945** If an error occurs, an IO error code is returned and the pager
56946** moves into the error state. Otherwise, SQLITE_OK is returned.
56947*/
56948SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager *pPager){
56949 int rc = SQLITE_OK; /* Return code */
56950
56951 /* This routine should not be called if a prior error has occurred.
56952 ** But if (due to a coding error elsewhere in the system) it does get
56953 ** called, just return the same error code without doing anything. */
56954 if( NEVER(pPager->errCode) ) return pPager->errCode;
56955
56956 assert( pPager->eState==PAGER_WRITER_LOCKED
56957 || pPager->eState==PAGER_WRITER_FINISHED
56958 || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
56959 );
56960 assert( assert_pager_state(pPager) );
56961
56962 /* An optimization. If the database was not actually modified during
56963 ** this transaction, the pager is running in exclusive-mode and is
56964 ** using persistent journals, then this function is a no-op.
56965 **
56966 ** The start of the journal file currently contains a single journal
56967 ** header with the nRec field set to 0. If such a journal is used as
56968 ** a hot-journal during hot-journal rollback, 0 changes will be made
56969 ** to the database file. So there is no need to zero the journal
56970 ** header. Since the pager is in exclusive mode, there is no need
56971 ** to drop any locks either.
56972 */
56973 if( pPager->eState==PAGER_WRITER_LOCKED
56974 && pPager->exclusiveMode
56975 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
56976 ){
56977 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
56978 pPager->eState = PAGER_READER;
56979 return SQLITE_OK;
56980 }
56981
56982 PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
56983 pPager->iDataVersion++;
56984 rc = pager_end_transaction(pPager, pPager->setMaster, 1);
56985 return pager_error(pPager, rc);
56986}
56987
56988/*
56989** If a write transaction is open, then all changes made within the
56990** transaction are reverted and the current write-transaction is closed.
56991** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR
56992** state if an error occurs.
56993**
56994** If the pager is already in PAGER_ERROR state when this function is called,
56995** it returns Pager.errCode immediately. No work is performed in this case.
56996**
56997** Otherwise, in rollback mode, this function performs two functions:
56998**
56999** 1) It rolls back the journal file, restoring all database file and
57000** in-memory cache pages to the state they were in when the transaction
57001** was opened, and
57002**
57003** 2) It finalizes the journal file, so that it is not used for hot
57004** rollback at any point in the future.
57005**
57006** Finalization of the journal file (task 2) is only performed if the
57007** rollback is successful.
57008**
57009** In WAL mode, all cache-entries containing data modified within the
57010** current transaction are either expelled from the cache or reverted to
57011** their pre-transaction state by re-reading data from the database or
57012** WAL files. The WAL transaction is then closed.
57013*/
57014SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
57015 int rc = SQLITE_OK; /* Return code */
57016 PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
57017
57018 /* PagerRollback() is a no-op if called in READER or OPEN state. If
57019 ** the pager is already in the ERROR state, the rollback is not
57020 ** attempted here. Instead, the error code is returned to the caller.
57021 */
57022 assert( assert_pager_state(pPager) );
57023 if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
57024 if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
57025
57026 if( pagerUseWal(pPager) ){
57027 int rc2;
57028 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
57029 rc2 = pager_end_transaction(pPager, pPager->setMaster, 0);
57030 if( rc==SQLITE_OK ) rc = rc2;
57031 }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
57032 int eState = pPager->eState;
57033 rc = pager_end_transaction(pPager, 0, 0);
57034 if( !MEMDB && eState>PAGER_WRITER_LOCKED ){
57035 /* This can happen using journal_mode=off. Move the pager to the error
57036 ** state to indicate that the contents of the cache may not be trusted.
57037 ** Any active readers will get SQLITE_ABORT.
57038 */
57039 pPager->errCode = SQLITE_ABORT;
57040 pPager->eState = PAGER_ERROR;
57041 setGetterMethod(pPager);
57042 return rc;
57043 }
57044 }else{
57045 rc = pager_playback(pPager, 0);
57046 }
57047
57048 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
57049 assert( rc==SQLITE_OK || rc==SQLITE_FULL || rc==SQLITE_CORRUPT
57050 || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR
57051 || rc==SQLITE_CANTOPEN
57052 );
57053
57054 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
57055 ** cache. So call pager_error() on the way out to make any error persistent.
57056 */
57057 return pager_error(pPager, rc);
57058}
57059
57060/*
57061** Return TRUE if the database file is opened read-only. Return FALSE
57062** if the database is (in theory) writable.
57063*/
57064SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){
57065 return pPager->readOnly;
57066}
57067
57068#ifdef SQLITE_DEBUG
57069/*
57070** Return the sum of the reference counts for all pages held by pPager.
57071*/
57072SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){
57073 return sqlite3PcacheRefCount(pPager->pPCache);
57074}
57075#endif
57076
57077/*
57078** Return the approximate number of bytes of memory currently
57079** used by the pager and its associated cache.
57080*/
57081SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager *pPager){
57082 int perPageSize = pPager->pageSize + pPager->nExtra + sizeof(PgHdr)
57083 + 5*sizeof(void*);
57084 return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
57085 + sqlite3MallocSize(pPager)
57086 + pPager->pageSize;
57087}
57088
57089/*
57090** Return the number of references to the specified page.
57091*/
57092SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage *pPage){
57093 return sqlite3PcachePageRefcount(pPage);
57094}
57095
57096#ifdef SQLITE_TEST
57097/*
57098** This routine is used for testing and analysis only.
57099*/
57100SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
57101 static int a[11];
57102 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
57103 a[1] = sqlite3PcachePagecount(pPager->pPCache);
57104 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
57105 a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
57106 a[4] = pPager->eState;
57107 a[5] = pPager->errCode;
57108 a[6] = pPager->aStat[PAGER_STAT_HIT];
57109 a[7] = pPager->aStat[PAGER_STAT_MISS];
57110 a[8] = 0; /* Used to be pPager->nOvfl */
57111 a[9] = pPager->nRead;
57112 a[10] = pPager->aStat[PAGER_STAT_WRITE];
57113 return a;
57114}
57115#endif
57116
57117/*
57118** Parameter eStat must be one of SQLITE_DBSTATUS_CACHE_HIT, _MISS, _WRITE,
57119** or _WRITE+1. The SQLITE_DBSTATUS_CACHE_WRITE+1 case is a translation
57120** of SQLITE_DBSTATUS_CACHE_SPILL. The _SPILL case is not contiguous because
57121** it was added later.
57122**
57123** Before returning, *pnVal is incremented by the
57124** current cache hit or miss count, according to the value of eStat. If the
57125** reset parameter is non-zero, the cache hit or miss count is zeroed before
57126** returning.
57127*/
57128SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
57129
57130 assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
57131 || eStat==SQLITE_DBSTATUS_CACHE_MISS
57132 || eStat==SQLITE_DBSTATUS_CACHE_WRITE
57133 || eStat==SQLITE_DBSTATUS_CACHE_WRITE+1
57134 );
57135
57136 assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS );
57137 assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE );
57138 assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1
57139 && PAGER_STAT_WRITE==2 && PAGER_STAT_SPILL==3 );
57140
57141 eStat -= SQLITE_DBSTATUS_CACHE_HIT;
57142 *pnVal += pPager->aStat[eStat];
57143 if( reset ){
57144 pPager->aStat[eStat] = 0;
57145 }
57146}
57147
57148/*
57149** Return true if this is an in-memory or temp-file backed pager.
57150*/
57151SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
57152 return pPager->tempFile;
57153}
57154
57155/*
57156** Check that there are at least nSavepoint savepoints open. If there are
57157** currently less than nSavepoints open, then open one or more savepoints
57158** to make up the difference. If the number of savepoints is already
57159** equal to nSavepoint, then this function is a no-op.
57160**
57161** If a memory allocation fails, SQLITE_NOMEM is returned. If an error
57162** occurs while opening the sub-journal file, then an IO error code is
57163** returned. Otherwise, SQLITE_OK.
57164*/
57165static SQLITE_NOINLINE int pagerOpenSavepoint(Pager *pPager, int nSavepoint){
57166 int rc = SQLITE_OK; /* Return code */
57167 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
57168 int ii; /* Iterator variable */
57169 PagerSavepoint *aNew; /* New Pager.aSavepoint array */
57170
57171 assert( pPager->eState>=PAGER_WRITER_LOCKED );
57172 assert( assert_pager_state(pPager) );
57173 assert( nSavepoint>nCurrent && pPager->useJournal );
57174
57175 /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
57176 ** if the allocation fails. Otherwise, zero the new portion in case a
57177 ** malloc failure occurs while populating it in the for(...) loop below.
57178 */
57179 aNew = (PagerSavepoint *)sqlite3Realloc(
57180 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
57181 );
57182 if( !aNew ){
57183 return SQLITE_NOMEM_BKPT;
57184 }
57185 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
57186 pPager->aSavepoint = aNew;
57187
57188 /* Populate the PagerSavepoint structures just allocated. */
57189 for(ii=nCurrent; ii<nSavepoint; ii++){
57190 aNew[ii].nOrig = pPager->dbSize;
57191 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
57192 aNew[ii].iOffset = pPager->journalOff;
57193 }else{
57194 aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
57195 }
57196 aNew[ii].iSubRec = pPager->nSubRec;
57197 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
57198 if( !aNew[ii].pInSavepoint ){
57199 return SQLITE_NOMEM_BKPT;
57200 }
57201 if( pagerUseWal(pPager) ){
57202 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
57203 }
57204 pPager->nSavepoint = ii+1;
57205 }
57206 assert( pPager->nSavepoint==nSavepoint );
57207 assertTruncateConstraint(pPager);
57208 return rc;
57209}
57210SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
57211 assert( pPager->eState>=PAGER_WRITER_LOCKED );
57212 assert( assert_pager_state(pPager) );
57213
57214 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
57215 return pagerOpenSavepoint(pPager, nSavepoint);
57216 }else{
57217 return SQLITE_OK;
57218 }
57219}
57220
57221
57222/*
57223** This function is called to rollback or release (commit) a savepoint.
57224** The savepoint to release or rollback need not be the most recently
57225** created savepoint.
57226**
57227** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
57228** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
57229** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
57230** that have occurred since the specified savepoint was created.
57231**
57232** The savepoint to rollback or release is identified by parameter
57233** iSavepoint. A value of 0 means to operate on the outermost savepoint
57234** (the first created). A value of (Pager.nSavepoint-1) means operate
57235** on the most recently created savepoint. If iSavepoint is greater than
57236** (Pager.nSavepoint-1), then this function is a no-op.
57237**
57238** If a negative value is passed to this function, then the current
57239** transaction is rolled back. This is different to calling
57240** sqlite3PagerRollback() because this function does not terminate
57241** the transaction or unlock the database, it just restores the
57242** contents of the database to its original state.
57243**
57244** In any case, all savepoints with an index greater than iSavepoint
57245** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE),
57246** then savepoint iSavepoint is also destroyed.
57247**
57248** This function may return SQLITE_NOMEM if a memory allocation fails,
57249** or an IO error code if an IO error occurs while rolling back a
57250** savepoint. If no errors occur, SQLITE_OK is returned.
57251*/
57252SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
57253 int rc = pPager->errCode;
57254
57255#ifdef SQLITE_ENABLE_ZIPVFS
57256 if( op==SAVEPOINT_RELEASE ) rc = SQLITE_OK;
57257#endif
57258
57259 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
57260 assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK );
57261
57262 if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
57263 int ii; /* Iterator variable */
57264 int nNew; /* Number of remaining savepoints after this op. */
57265
57266 /* Figure out how many savepoints will still be active after this
57267 ** operation. Store this value in nNew. Then free resources associated
57268 ** with any savepoints that are destroyed by this operation.
57269 */
57270 nNew = iSavepoint + (( op==SAVEPOINT_RELEASE ) ? 0 : 1);
57271 for(ii=nNew; ii<pPager->nSavepoint; ii++){
57272 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
57273 }
57274 pPager->nSavepoint = nNew;
57275
57276 /* If this is a release of the outermost savepoint, truncate
57277 ** the sub-journal to zero bytes in size. */
57278 if( op==SAVEPOINT_RELEASE ){
57279 if( nNew==0 && isOpen(pPager->sjfd) ){
57280 /* Only truncate if it is an in-memory sub-journal. */
57281 if( sqlite3JournalIsInMemory(pPager->sjfd) ){
57282 rc = sqlite3OsTruncate(pPager->sjfd, 0);
57283 assert( rc==SQLITE_OK );
57284 }
57285 pPager->nSubRec = 0;
57286 }
57287 }
57288 /* Else this is a rollback operation, playback the specified savepoint.
57289 ** If this is a temp-file, it is possible that the journal file has
57290 ** not yet been opened. In this case there have been no changes to
57291 ** the database file, so the playback operation can be skipped.
57292 */
57293 else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
57294 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
57295 rc = pagerPlaybackSavepoint(pPager, pSavepoint);
57296 assert(rc!=SQLITE_DONE);
57297 }
57298
57299#ifdef SQLITE_ENABLE_ZIPVFS
57300 /* If the cache has been modified but the savepoint cannot be rolled
57301 ** back journal_mode=off, put the pager in the error state. This way,
57302 ** if the VFS used by this pager includes ZipVFS, the entire transaction
57303 ** can be rolled back at the ZipVFS level. */
57304 else if(
57305 pPager->journalMode==PAGER_JOURNALMODE_OFF
57306 && pPager->eState>=PAGER_WRITER_CACHEMOD
57307 ){
57308 pPager->errCode = SQLITE_ABORT;
57309 pPager->eState = PAGER_ERROR;
57310 setGetterMethod(pPager);
57311 }
57312#endif
57313 }
57314
57315 return rc;
57316}
57317
57318/*
57319** Return the full pathname of the database file.
57320**
57321** Except, if the pager is in-memory only, then return an empty string if
57322** nullIfMemDb is true. This routine is called with nullIfMemDb==1 when
57323** used to report the filename to the user, for compatibility with legacy
57324** behavior. But when the Btree needs to know the filename for matching to
57325** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
57326** participate in shared-cache.
57327*/
57328SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager *pPager, int nullIfMemDb){
57329 return (nullIfMemDb && pPager->memDb) ? "" : pPager->zFilename;
57330}
57331
57332/*
57333** Return the VFS structure for the pager.
57334*/
57335SQLITE_PRIVATE sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
57336 return pPager->pVfs;
57337}
57338
57339/*
57340** Return the file handle for the database file associated
57341** with the pager. This might return NULL if the file has
57342** not yet been opened.
57343*/
57344SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
57345 return pPager->fd;
57346}
57347
57348#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
57349/*
57350** Reset the lock timeout for pager.
57351*/
57352SQLITE_PRIVATE void sqlite3PagerResetLockTimeout(Pager *pPager){
57353 int x = 0;
57354 sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_LOCK_TIMEOUT, &x);
57355}
57356#endif
57357
57358/*
57359** Return the file handle for the journal file (if it exists).
57360** This will be either the rollback journal or the WAL file.
57361*/
57362SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager *pPager){
57363#if SQLITE_OMIT_WAL
57364 return pPager->jfd;
57365#else
57366 return pPager->pWal ? sqlite3WalFile(pPager->pWal) : pPager->jfd;
57367#endif
57368}
57369
57370/*
57371** Return the full pathname of the journal file.
57372*/
57373SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager *pPager){
57374 return pPager->zJournal;
57375}
57376
57377#ifdef SQLITE_HAS_CODEC
57378/*
57379** Set or retrieve the codec for this pager
57380*/
57381SQLITE_PRIVATE void sqlite3PagerSetCodec(
57382 Pager *pPager,
57383 void *(*xCodec)(void*,void*,Pgno,int),
57384 void (*xCodecSizeChng)(void*,int,int),
57385 void (*xCodecFree)(void*),
57386 void *pCodec
57387){
57388 if( pPager->xCodecFree ){
57389 pPager->xCodecFree(pPager->pCodec);
57390 }else{
57391 pager_reset(pPager);
57392 }
57393 pPager->xCodec = pPager->memDb ? 0 : xCodec;
57394 pPager->xCodecSizeChng = xCodecSizeChng;
57395 pPager->xCodecFree = xCodecFree;
57396 pPager->pCodec = pCodec;
57397 setGetterMethod(pPager);
57398 pagerReportSize(pPager);
57399}
57400SQLITE_PRIVATE void *sqlite3PagerGetCodec(Pager *pPager){
57401 return pPager->pCodec;
57402}
57403
57404/*
57405** This function is called by the wal module when writing page content
57406** into the log file.
57407**
57408** This function returns a pointer to a buffer containing the encrypted
57409** page content. If a malloc fails, this function may return NULL.
57410*/
57411SQLITE_PRIVATE void *sqlite3PagerCodec(PgHdr *pPg){
57412 void *aData = 0;
57413 CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
57414 return aData;
57415}
57416
57417/*
57418** Return the current pager state
57419*/
57420SQLITE_PRIVATE int sqlite3PagerState(Pager *pPager){
57421 return pPager->eState;
57422}
57423#endif /* SQLITE_HAS_CODEC */
57424
57425#ifndef SQLITE_OMIT_AUTOVACUUM
57426/*
57427** Move the page pPg to location pgno in the file.
57428**
57429** There must be no references to the page previously located at
57430** pgno (which we call pPgOld) though that page is allowed to be
57431** in cache. If the page previously located at pgno is not already
57432** in the rollback journal, it is not put there by by this routine.
57433**
57434** References to the page pPg remain valid. Updating any
57435** meta-data associated with pPg (i.e. data stored in the nExtra bytes
57436** allocated along with the page) is the responsibility of the caller.
57437**
57438** A transaction must be active when this routine is called. It used to be
57439** required that a statement transaction was not active, but this restriction
57440** has been removed (CREATE INDEX needs to move a page when a statement
57441** transaction is active).
57442**
57443** If the fourth argument, isCommit, is non-zero, then this page is being
57444** moved as part of a database reorganization just before the transaction
57445** is being committed. In this case, it is guaranteed that the database page
57446** pPg refers to will not be written to again within this transaction.
57447**
57448** This function may return SQLITE_NOMEM or an IO error code if an error
57449** occurs. Otherwise, it returns SQLITE_OK.
57450*/
57451SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
57452 PgHdr *pPgOld; /* The page being overwritten. */
57453 Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */
57454 int rc; /* Return code */
57455 Pgno origPgno; /* The original page number */
57456
57457 assert( pPg->nRef>0 );
57458 assert( pPager->eState==PAGER_WRITER_CACHEMOD
57459 || pPager->eState==PAGER_WRITER_DBMOD
57460 );
57461 assert( assert_pager_state(pPager) );
57462
57463 /* In order to be able to rollback, an in-memory database must journal
57464 ** the page we are moving from.
57465 */
57466 assert( pPager->tempFile || !MEMDB );
57467 if( pPager->tempFile ){
57468 rc = sqlite3PagerWrite(pPg);
57469 if( rc ) return rc;
57470 }
57471
57472 /* If the page being moved is dirty and has not been saved by the latest
57473 ** savepoint, then save the current contents of the page into the
57474 ** sub-journal now. This is required to handle the following scenario:
57475 **
57476 ** BEGIN;
57477 ** <journal page X, then modify it in memory>
57478 ** SAVEPOINT one;
57479 ** <Move page X to location Y>
57480 ** ROLLBACK TO one;
57481 **
57482 ** If page X were not written to the sub-journal here, it would not
57483 ** be possible to restore its contents when the "ROLLBACK TO one"
57484 ** statement were is processed.
57485 **
57486 ** subjournalPage() may need to allocate space to store pPg->pgno into
57487 ** one or more savepoint bitvecs. This is the reason this function
57488 ** may return SQLITE_NOMEM.
57489 */
57490 if( (pPg->flags & PGHDR_DIRTY)!=0
57491 && SQLITE_OK!=(rc = subjournalPageIfRequired(pPg))
57492 ){
57493 return rc;
57494 }
57495
57496 PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
57497 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
57498 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
57499
57500 /* If the journal needs to be sync()ed before page pPg->pgno can
57501 ** be written to, store pPg->pgno in local variable needSyncPgno.
57502 **
57503 ** If the isCommit flag is set, there is no need to remember that
57504 ** the journal needs to be sync()ed before database page pPg->pgno
57505 ** can be written to. The caller has already promised not to write to it.
57506 */
57507 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
57508 needSyncPgno = pPg->pgno;
57509 assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
57510 pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize );
57511 assert( pPg->flags&PGHDR_DIRTY );
57512 }
57513
57514 /* If the cache contains a page with page-number pgno, remove it
57515 ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for
57516 ** page pgno before the 'move' operation, it needs to be retained
57517 ** for the page moved there.
57518 */
57519 pPg->flags &= ~PGHDR_NEED_SYNC;
57520 pPgOld = sqlite3PagerLookup(pPager, pgno);
57521 assert( !pPgOld || pPgOld->nRef==1 );
57522 if( pPgOld ){
57523 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
57524 if( pPager->tempFile ){
57525 /* Do not discard pages from an in-memory database since we might
57526 ** need to rollback later. Just move the page out of the way. */
57527 sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
57528 }else{
57529 sqlite3PcacheDrop(pPgOld);
57530 }
57531 }
57532
57533 origPgno = pPg->pgno;
57534 sqlite3PcacheMove(pPg, pgno);
57535 sqlite3PcacheMakeDirty(pPg);
57536
57537 /* For an in-memory database, make sure the original page continues
57538 ** to exist, in case the transaction needs to roll back. Use pPgOld
57539 ** as the original page since it has already been allocated.
57540 */
57541 if( pPager->tempFile && pPgOld ){
57542 sqlite3PcacheMove(pPgOld, origPgno);
57543 sqlite3PagerUnrefNotNull(pPgOld);
57544 }
57545
57546 if( needSyncPgno ){
57547 /* If needSyncPgno is non-zero, then the journal file needs to be
57548 ** sync()ed before any data is written to database file page needSyncPgno.
57549 ** Currently, no such page exists in the page-cache and the
57550 ** "is journaled" bitvec flag has been set. This needs to be remedied by
57551 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
57552 ** flag.
57553 **
57554 ** If the attempt to load the page into the page-cache fails, (due
57555 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
57556 ** array. Otherwise, if the page is loaded and written again in
57557 ** this transaction, it may be written to the database file before
57558 ** it is synced into the journal file. This way, it may end up in
57559 ** the journal file twice, but that is not a problem.
57560 */
57561 PgHdr *pPgHdr;
57562 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr, 0);
57563 if( rc!=SQLITE_OK ){
57564 if( needSyncPgno<=pPager->dbOrigSize ){
57565 assert( pPager->pTmpSpace!=0 );
57566 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
57567 }
57568 return rc;
57569 }
57570 pPgHdr->flags |= PGHDR_NEED_SYNC;
57571 sqlite3PcacheMakeDirty(pPgHdr);
57572 sqlite3PagerUnrefNotNull(pPgHdr);
57573 }
57574
57575 return SQLITE_OK;
57576}
57577#endif
57578
57579/*
57580** The page handle passed as the first argument refers to a dirty page
57581** with a page number other than iNew. This function changes the page's
57582** page number to iNew and sets the value of the PgHdr.flags field to
57583** the value passed as the third parameter.
57584*/
57585SQLITE_PRIVATE void sqlite3PagerRekey(DbPage *pPg, Pgno iNew, u16 flags){
57586 assert( pPg->pgno!=iNew );
57587 pPg->flags = flags;
57588 sqlite3PcacheMove(pPg, iNew);
57589}
57590
57591/*
57592** Return a pointer to the data for the specified page.
57593*/
57594SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){
57595 assert( pPg->nRef>0 || pPg->pPager->memDb );
57596 return pPg->pData;
57597}
57598
57599/*
57600** Return a pointer to the Pager.nExtra bytes of "extra" space
57601** allocated along with the specified page.
57602*/
57603SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *pPg){
57604 return pPg->pExtra;
57605}
57606
57607/*
57608** Get/set the locking-mode for this pager. Parameter eMode must be one
57609** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
57610** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
57611** the locking-mode is set to the value specified.
57612**
57613** The returned value is either PAGER_LOCKINGMODE_NORMAL or
57614** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
57615** locking-mode.
57616*/
57617SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *pPager, int eMode){
57618 assert( eMode==PAGER_LOCKINGMODE_QUERY
57619 || eMode==PAGER_LOCKINGMODE_NORMAL
57620 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
57621 assert( PAGER_LOCKINGMODE_QUERY<0 );
57622 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
57623 assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
57624 if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
57625 pPager->exclusiveMode = (u8)eMode;
57626 }
57627 return (int)pPager->exclusiveMode;
57628}
57629
57630/*
57631** Set the journal-mode for this pager. Parameter eMode must be one of:
57632**
57633** PAGER_JOURNALMODE_DELETE
57634** PAGER_JOURNALMODE_TRUNCATE
57635** PAGER_JOURNALMODE_PERSIST
57636** PAGER_JOURNALMODE_OFF
57637** PAGER_JOURNALMODE_MEMORY
57638** PAGER_JOURNALMODE_WAL
57639**
57640** The journalmode is set to the value specified if the change is allowed.
57641** The change may be disallowed for the following reasons:
57642**
57643** * An in-memory database can only have its journal_mode set to _OFF
57644** or _MEMORY.
57645**
57646** * Temporary databases cannot have _WAL journalmode.
57647**
57648** The returned indicate the current (possibly updated) journal-mode.
57649*/
57650SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
57651 u8 eOld = pPager->journalMode; /* Prior journalmode */
57652
57653 /* The eMode parameter is always valid */
57654 assert( eMode==PAGER_JOURNALMODE_DELETE
57655 || eMode==PAGER_JOURNALMODE_TRUNCATE
57656 || eMode==PAGER_JOURNALMODE_PERSIST
57657 || eMode==PAGER_JOURNALMODE_OFF
57658 || eMode==PAGER_JOURNALMODE_WAL
57659 || eMode==PAGER_JOURNALMODE_MEMORY );
57660
57661 /* This routine is only called from the OP_JournalMode opcode, and
57662 ** the logic there will never allow a temporary file to be changed
57663 ** to WAL mode.
57664 */
57665 assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
57666
57667 /* Do allow the journalmode of an in-memory database to be set to
57668 ** anything other than MEMORY or OFF
57669 */
57670 if( MEMDB ){
57671 assert( eOld==PAGER_JOURNALMODE_MEMORY || eOld==PAGER_JOURNALMODE_OFF );
57672 if( eMode!=PAGER_JOURNALMODE_MEMORY && eMode!=PAGER_JOURNALMODE_OFF ){
57673 eMode = eOld;
57674 }
57675 }
57676
57677 if( eMode!=eOld ){
57678
57679 /* Change the journal mode. */
57680 assert( pPager->eState!=PAGER_ERROR );
57681 pPager->journalMode = (u8)eMode;
57682
57683 /* When transistioning from TRUNCATE or PERSIST to any other journal
57684 ** mode except WAL, unless the pager is in locking_mode=exclusive mode,
57685 ** delete the journal file.
57686 */
57687 assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
57688 assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
57689 assert( (PAGER_JOURNALMODE_DELETE & 5)==0 );
57690 assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 );
57691 assert( (PAGER_JOURNALMODE_OFF & 5)==0 );
57692 assert( (PAGER_JOURNALMODE_WAL & 5)==5 );
57693
57694 assert( isOpen(pPager->fd) || pPager->exclusiveMode );
57695 if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
57696
57697 /* In this case we would like to delete the journal file. If it is
57698 ** not possible, then that is not a problem. Deleting the journal file
57699 ** here is an optimization only.
57700 **
57701 ** Before deleting the journal file, obtain a RESERVED lock on the
57702 ** database file. This ensures that the journal file is not deleted
57703 ** while it is in use by some other client.
57704 */
57705 sqlite3OsClose(pPager->jfd);
57706 if( pPager->eLock>=RESERVED_LOCK ){
57707 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
57708 }else{
57709 int rc = SQLITE_OK;
57710 int state = pPager->eState;
57711 assert( state==PAGER_OPEN || state==PAGER_READER );
57712 if( state==PAGER_OPEN ){
57713 rc = sqlite3PagerSharedLock(pPager);
57714 }
57715 if( pPager->eState==PAGER_READER ){
57716 assert( rc==SQLITE_OK );
57717 rc = pagerLockDb(pPager, RESERVED_LOCK);
57718 }
57719 if( rc==SQLITE_OK ){
57720 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
57721 }
57722 if( rc==SQLITE_OK && state==PAGER_READER ){
57723 pagerUnlockDb(pPager, SHARED_LOCK);
57724 }else if( state==PAGER_OPEN ){
57725 pager_unlock(pPager);
57726 }
57727 assert( state==pPager->eState );
57728 }
57729 }else if( eMode==PAGER_JOURNALMODE_OFF ){
57730 sqlite3OsClose(pPager->jfd);
57731 }
57732 }
57733
57734 /* Return the new journal mode */
57735 return (int)pPager->journalMode;
57736}
57737
57738/*
57739** Return the current journal mode.
57740*/
57741SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager *pPager){
57742 return (int)pPager->journalMode;
57743}
57744
57745/*
57746** Return TRUE if the pager is in a state where it is OK to change the
57747** journalmode. Journalmode changes can only happen when the database
57748** is unmodified.
57749*/
57750SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
57751 assert( assert_pager_state(pPager) );
57752 if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
57753 if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
57754 return 1;
57755}
57756
57757/*
57758** Get/set the size-limit used for persistent journal files.
57759**
57760** Setting the size limit to -1 means no limit is enforced.
57761** An attempt to set a limit smaller than -1 is a no-op.
57762*/
57763SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
57764 if( iLimit>=-1 ){
57765 pPager->journalSizeLimit = iLimit;
57766 sqlite3WalLimit(pPager->pWal, iLimit);
57767 }
57768 return pPager->journalSizeLimit;
57769}
57770
57771/*
57772** Return a pointer to the pPager->pBackup variable. The backup module
57773** in backup.c maintains the content of this variable. This module
57774** uses it opaquely as an argument to sqlite3BackupRestart() and
57775** sqlite3BackupUpdate() only.
57776*/
57777SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
57778 return &pPager->pBackup;
57779}
57780
57781#ifndef SQLITE_OMIT_VACUUM
57782/*
57783** Unless this is an in-memory or temporary database, clear the pager cache.
57784*/
57785SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){
57786 assert( MEMDB==0 || pPager->tempFile );
57787 if( pPager->tempFile==0 ) pager_reset(pPager);
57788}
57789#endif
57790
57791
57792#ifndef SQLITE_OMIT_WAL
57793/*
57794** This function is called when the user invokes "PRAGMA wal_checkpoint",
57795** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
57796** or wal_blocking_checkpoint() API functions.
57797**
57798** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
57799*/
57800SQLITE_PRIVATE int sqlite3PagerCheckpoint(
57801 Pager *pPager, /* Checkpoint on this pager */
57802 sqlite3 *db, /* Db handle used to check for interrupts */
57803 int eMode, /* Type of checkpoint */
57804 int *pnLog, /* OUT: Final number of frames in log */
57805 int *pnCkpt /* OUT: Final number of checkpointed frames */
57806){
57807 int rc = SQLITE_OK;
57808 if( pPager->pWal ){
57809 rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
57810 (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
57811 pPager->pBusyHandlerArg,
57812 pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
57813 pnLog, pnCkpt
57814 );
57815 sqlite3PagerResetLockTimeout(pPager);
57816 }
57817 return rc;
57818}
57819
57820SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
57821 return sqlite3WalCallback(pPager->pWal);
57822}
57823
57824/*
57825** Return true if the underlying VFS for the given pager supports the
57826** primitives necessary for write-ahead logging.
57827*/
57828SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){
57829 const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
57830 if( pPager->noLock ) return 0;
57831 return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
57832}
57833
57834/*
57835** Attempt to take an exclusive lock on the database file. If a PENDING lock
57836** is obtained instead, immediately release it.
57837*/
57838static int pagerExclusiveLock(Pager *pPager){
57839 int rc; /* Return code */
57840
57841 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
57842 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
57843 if( rc!=SQLITE_OK ){
57844 /* If the attempt to grab the exclusive lock failed, release the
57845 ** pending lock that may have been obtained instead. */
57846 pagerUnlockDb(pPager, SHARED_LOCK);
57847 }
57848
57849 return rc;
57850}
57851
57852/*
57853** Call sqlite3WalOpen() to open the WAL handle. If the pager is in
57854** exclusive-locking mode when this function is called, take an EXCLUSIVE
57855** lock on the database file and use heap-memory to store the wal-index
57856** in. Otherwise, use the normal shared-memory.
57857*/
57858static int pagerOpenWal(Pager *pPager){
57859 int rc = SQLITE_OK;
57860
57861 assert( pPager->pWal==0 && pPager->tempFile==0 );
57862 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
57863
57864 /* If the pager is already in exclusive-mode, the WAL module will use
57865 ** heap-memory for the wal-index instead of the VFS shared-memory
57866 ** implementation. Take the exclusive lock now, before opening the WAL
57867 ** file, to make sure this is safe.
57868 */
57869 if( pPager->exclusiveMode ){
57870 rc = pagerExclusiveLock(pPager);
57871 }
57872
57873 /* Open the connection to the log file. If this operation fails,
57874 ** (e.g. due to malloc() failure), return an error code.
57875 */
57876 if( rc==SQLITE_OK ){
57877 rc = sqlite3WalOpen(pPager->pVfs,
57878 pPager->fd, pPager->zWal, pPager->exclusiveMode,
57879 pPager->journalSizeLimit, &pPager->pWal
57880 );
57881 }
57882 pagerFixMaplimit(pPager);
57883
57884 return rc;
57885}
57886
57887
57888/*
57889** The caller must be holding a SHARED lock on the database file to call
57890** this function.
57891**
57892** If the pager passed as the first argument is open on a real database
57893** file (not a temp file or an in-memory database), and the WAL file
57894** is not already open, make an attempt to open it now. If successful,
57895** return SQLITE_OK. If an error occurs or the VFS used by the pager does
57896** not support the xShmXXX() methods, return an error code. *pbOpen is
57897** not modified in either case.
57898**
57899** If the pager is open on a temp-file (or in-memory database), or if
57900** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK
57901** without doing anything.
57902*/
57903SQLITE_PRIVATE int sqlite3PagerOpenWal(
57904 Pager *pPager, /* Pager object */
57905 int *pbOpen /* OUT: Set to true if call is a no-op */
57906){
57907 int rc = SQLITE_OK; /* Return code */
57908
57909 assert( assert_pager_state(pPager) );
57910 assert( pPager->eState==PAGER_OPEN || pbOpen );
57911 assert( pPager->eState==PAGER_READER || !pbOpen );
57912 assert( pbOpen==0 || *pbOpen==0 );
57913 assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
57914
57915 if( !pPager->tempFile && !pPager->pWal ){
57916 if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN;
57917
57918 /* Close any rollback journal previously open */
57919 sqlite3OsClose(pPager->jfd);
57920
57921 rc = pagerOpenWal(pPager);
57922 if( rc==SQLITE_OK ){
57923 pPager->journalMode = PAGER_JOURNALMODE_WAL;
57924 pPager->eState = PAGER_OPEN;
57925 }
57926 }else{
57927 *pbOpen = 1;
57928 }
57929
57930 return rc;
57931}
57932
57933/*
57934** This function is called to close the connection to the log file prior
57935** to switching from WAL to rollback mode.
57936**
57937** Before closing the log file, this function attempts to take an
57938** EXCLUSIVE lock on the database file. If this cannot be obtained, an
57939** error (SQLITE_BUSY) is returned and the log connection is not closed.
57940** If successful, the EXCLUSIVE lock is not released before returning.
57941*/
57942SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3 *db){
57943 int rc = SQLITE_OK;
57944
57945 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
57946
57947 /* If the log file is not already open, but does exist in the file-system,
57948 ** it may need to be checkpointed before the connection can switch to
57949 ** rollback mode. Open it now so this can happen.
57950 */
57951 if( !pPager->pWal ){
57952 int logexists = 0;
57953 rc = pagerLockDb(pPager, SHARED_LOCK);
57954 if( rc==SQLITE_OK ){
57955 rc = sqlite3OsAccess(
57956 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
57957 );
57958 }
57959 if( rc==SQLITE_OK && logexists ){
57960 rc = pagerOpenWal(pPager);
57961 }
57962 }
57963
57964 /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
57965 ** the database file, the log and log-summary files will be deleted.
57966 */
57967 if( rc==SQLITE_OK && pPager->pWal ){
57968 rc = pagerExclusiveLock(pPager);
57969 if( rc==SQLITE_OK ){
57970 rc = sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags,
57971 pPager->pageSize, (u8*)pPager->pTmpSpace);
57972 pPager->pWal = 0;
57973 pagerFixMaplimit(pPager);
57974 if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
57975 }
57976 }
57977 return rc;
57978}
57979
57980#ifdef SQLITE_ENABLE_SNAPSHOT
57981/*
57982** If this is a WAL database, obtain a snapshot handle for the snapshot
57983** currently open. Otherwise, return an error.
57984*/
57985SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot){
57986 int rc = SQLITE_ERROR;
57987 if( pPager->pWal ){
57988 rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot);
57989 }
57990 return rc;
57991}
57992
57993/*
57994** If this is a WAL database, store a pointer to pSnapshot. Next time a
57995** read transaction is opened, attempt to read from the snapshot it
57996** identifies. If this is not a WAL database, return an error.
57997*/
57998SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot){
57999 int rc = SQLITE_OK;
58000 if( pPager->pWal ){
58001 sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot);
58002 }else{
58003 rc = SQLITE_ERROR;
58004 }
58005 return rc;
58006}
58007
58008/*
58009** If this is a WAL database, call sqlite3WalSnapshotRecover(). If this
58010** is not a WAL database, return an error.
58011*/
58012SQLITE_PRIVATE int sqlite3PagerSnapshotRecover(Pager *pPager){
58013 int rc;
58014 if( pPager->pWal ){
58015 rc = sqlite3WalSnapshotRecover(pPager->pWal);
58016 }else{
58017 rc = SQLITE_ERROR;
58018 }
58019 return rc;
58020}
58021
58022/*
58023** The caller currently has a read transaction open on the database.
58024** If this is not a WAL database, SQLITE_ERROR is returned. Otherwise,
58025** this function takes a SHARED lock on the CHECKPOINTER slot and then
58026** checks if the snapshot passed as the second argument is still
58027** available. If so, SQLITE_OK is returned.
58028**
58029** If the snapshot is not available, SQLITE_ERROR is returned. Or, if
58030** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error
58031** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER
58032** lock is released before returning.
58033*/
58034SQLITE_PRIVATE int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot){
58035 int rc;
58036 if( pPager->pWal ){
58037 rc = sqlite3WalSnapshotCheck(pPager->pWal, pSnapshot);
58038 }else{
58039 rc = SQLITE_ERROR;
58040 }
58041 return rc;
58042}
58043
58044/*
58045** Release a lock obtained by an earlier successful call to
58046** sqlite3PagerSnapshotCheck().
58047*/
58048SQLITE_PRIVATE void sqlite3PagerSnapshotUnlock(Pager *pPager){
58049 assert( pPager->pWal );
58050 return sqlite3WalSnapshotUnlock(pPager->pWal);
58051}
58052
58053#endif /* SQLITE_ENABLE_SNAPSHOT */
58054#endif /* !SQLITE_OMIT_WAL */
58055
58056#ifdef SQLITE_ENABLE_ZIPVFS
58057/*
58058** A read-lock must be held on the pager when this function is called. If
58059** the pager is in WAL mode and the WAL file currently contains one or more
58060** frames, return the size in bytes of the page images stored within the
58061** WAL frames. Otherwise, if this is not a WAL database or the WAL file
58062** is empty, return 0.
58063*/
58064SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){
58065 assert( pPager->eState>=PAGER_READER );
58066 return sqlite3WalFramesize(pPager->pWal);
58067}
58068#endif
58069
58070#endif /* SQLITE_OMIT_DISKIO */
58071
58072/************** End of pager.c ***********************************************/
58073/************** Begin file wal.c *********************************************/
58074/*
58075** 2010 February 1
58076**
58077** The author disclaims copyright to this source code. In place of
58078** a legal notice, here is a blessing:
58079**
58080** May you do good and not evil.
58081** May you find forgiveness for yourself and forgive others.
58082** May you share freely, never taking more than you give.
58083**
58084*************************************************************************
58085**
58086** This file contains the implementation of a write-ahead log (WAL) used in
58087** "journal_mode=WAL" mode.
58088**
58089** WRITE-AHEAD LOG (WAL) FILE FORMAT
58090**
58091** A WAL file consists of a header followed by zero or more "frames".
58092** Each frame records the revised content of a single page from the
58093** database file. All changes to the database are recorded by writing
58094** frames into the WAL. Transactions commit when a frame is written that
58095** contains a commit marker. A single WAL can and usually does record
58096** multiple transactions. Periodically, the content of the WAL is
58097** transferred back into the database file in an operation called a
58098** "checkpoint".
58099**
58100** A single WAL file can be used multiple times. In other words, the
58101** WAL can fill up with frames and then be checkpointed and then new
58102** frames can overwrite the old ones. A WAL always grows from beginning
58103** toward the end. Checksums and counters attached to each frame are
58104** used to determine which frames within the WAL are valid and which
58105** are leftovers from prior checkpoints.
58106**
58107** The WAL header is 32 bytes in size and consists of the following eight
58108** big-endian 32-bit unsigned integer values:
58109**
58110** 0: Magic number. 0x377f0682 or 0x377f0683
58111** 4: File format version. Currently 3007000
58112** 8: Database page size. Example: 1024
58113** 12: Checkpoint sequence number
58114** 16: Salt-1, random integer incremented with each checkpoint
58115** 20: Salt-2, a different random integer changing with each ckpt
58116** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
58117** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
58118**
58119** Immediately following the wal-header are zero or more frames. Each
58120** frame consists of a 24-byte frame-header followed by a <page-size> bytes
58121** of page data. The frame-header is six big-endian 32-bit unsigned
58122** integer values, as follows:
58123**
58124** 0: Page number.
58125** 4: For commit records, the size of the database image in pages
58126** after the commit. For all other records, zero.
58127** 8: Salt-1 (copied from the header)
58128** 12: Salt-2 (copied from the header)
58129** 16: Checksum-1.
58130** 20: Checksum-2.
58131**
58132** A frame is considered valid if and only if the following conditions are
58133** true:
58134**
58135** (1) The salt-1 and salt-2 values in the frame-header match
58136** salt values in the wal-header
58137**
58138** (2) The checksum values in the final 8 bytes of the frame-header
58139** exactly match the checksum computed consecutively on the
58140** WAL header and the first 8 bytes and the content of all frames
58141** up to and including the current frame.
58142**
58143** The checksum is computed using 32-bit big-endian integers if the
58144** magic number in the first 4 bytes of the WAL is 0x377f0683 and it
58145** is computed using little-endian if the magic number is 0x377f0682.
58146** The checksum values are always stored in the frame header in a
58147** big-endian format regardless of which byte order is used to compute
58148** the checksum. The checksum is computed by interpreting the input as
58149** an even number of unsigned 32-bit integers: x[0] through x[N]. The
58150** algorithm used for the checksum is as follows:
58151**
58152** for i from 0 to n-1 step 2:
58153** s0 += x[i] + s1;
58154** s1 += x[i+1] + s0;
58155** endfor
58156**
58157** Note that s0 and s1 are both weighted checksums using fibonacci weights
58158** in reverse order (the largest fibonacci weight occurs on the first element
58159** of the sequence being summed.) The s1 value spans all 32-bit
58160** terms of the sequence whereas s0 omits the final term.
58161**
58162** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
58163** WAL is transferred into the database, then the database is VFS.xSync-ed.
58164** The VFS.xSync operations serve as write barriers - all writes launched
58165** before the xSync must complete before any write that launches after the
58166** xSync begins.
58167**
58168** After each checkpoint, the salt-1 value is incremented and the salt-2
58169** value is randomized. This prevents old and new frames in the WAL from
58170** being considered valid at the same time and being checkpointing together
58171** following a crash.
58172**
58173** READER ALGORITHM
58174**
58175** To read a page from the database (call it page number P), a reader
58176** first checks the WAL to see if it contains page P. If so, then the
58177** last valid instance of page P that is a followed by a commit frame
58178** or is a commit frame itself becomes the value read. If the WAL
58179** contains no copies of page P that are valid and which are a commit
58180** frame or are followed by a commit frame, then page P is read from
58181** the database file.
58182**
58183** To start a read transaction, the reader records the index of the last
58184** valid frame in the WAL. The reader uses this recorded "mxFrame" value
58185** for all subsequent read operations. New transactions can be appended
58186** to the WAL, but as long as the reader uses its original mxFrame value
58187** and ignores the newly appended content, it will see a consistent snapshot
58188** of the database from a single point in time. This technique allows
58189** multiple concurrent readers to view different versions of the database
58190** content simultaneously.
58191**
58192** The reader algorithm in the previous paragraphs works correctly, but
58193** because frames for page P can appear anywhere within the WAL, the
58194** reader has to scan the entire WAL looking for page P frames. If the
58195** WAL is large (multiple megabytes is typical) that scan can be slow,
58196** and read performance suffers. To overcome this problem, a separate
58197** data structure called the wal-index is maintained to expedite the
58198** search for frames of a particular page.
58199**
58200** WAL-INDEX FORMAT
58201**
58202** Conceptually, the wal-index is shared memory, though VFS implementations
58203** might choose to implement the wal-index using a mmapped file. Because
58204** the wal-index is shared memory, SQLite does not support journal_mode=WAL
58205** on a network filesystem. All users of the database must be able to
58206** share memory.
58207**
58208** In the default unix and windows implementation, the wal-index is a mmapped
58209** file whose name is the database name with a "-shm" suffix added. For that
58210** reason, the wal-index is sometimes called the "shm" file.
58211**
58212** The wal-index is transient. After a crash, the wal-index can (and should
58213** be) reconstructed from the original WAL file. In fact, the VFS is required
58214** to either truncate or zero the header of the wal-index when the last
58215** connection to it closes. Because the wal-index is transient, it can
58216** use an architecture-specific format; it does not have to be cross-platform.
58217** Hence, unlike the database and WAL file formats which store all values
58218** as big endian, the wal-index can store multi-byte values in the native
58219** byte order of the host computer.
58220**
58221** The purpose of the wal-index is to answer this question quickly: Given
58222** a page number P and a maximum frame index M, return the index of the
58223** last frame in the wal before frame M for page P in the WAL, or return
58224** NULL if there are no frames for page P in the WAL prior to M.
58225**
58226** The wal-index consists of a header region, followed by an one or
58227** more index blocks.
58228**
58229** The wal-index header contains the total number of frames within the WAL
58230** in the mxFrame field.
58231**
58232** Each index block except for the first contains information on
58233** HASHTABLE_NPAGE frames. The first index block contains information on
58234** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and
58235** HASHTABLE_NPAGE are selected so that together the wal-index header and
58236** first index block are the same size as all other index blocks in the
58237** wal-index.
58238**
58239** Each index block contains two sections, a page-mapping that contains the
58240** database page number associated with each wal frame, and a hash-table
58241** that allows readers to query an index block for a specific page number.
58242** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
58243** for the first index block) 32-bit page numbers. The first entry in the
58244** first index-block contains the database page number corresponding to the
58245** first frame in the WAL file. The first entry in the second index block
58246** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in
58247** the log, and so on.
58248**
58249** The last index block in a wal-index usually contains less than the full
58250** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
58251** depending on the contents of the WAL file. This does not change the
58252** allocated size of the page-mapping array - the page-mapping array merely
58253** contains unused entries.
58254**
58255** Even without using the hash table, the last frame for page P
58256** can be found by scanning the page-mapping sections of each index block
58257** starting with the last index block and moving toward the first, and
58258** within each index block, starting at the end and moving toward the
58259** beginning. The first entry that equals P corresponds to the frame
58260** holding the content for that page.
58261**
58262** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
58263** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the
58264** hash table for each page number in the mapping section, so the hash
58265** table is never more than half full. The expected number of collisions
58266** prior to finding a match is 1. Each entry of the hash table is an
58267** 1-based index of an entry in the mapping section of the same
58268** index block. Let K be the 1-based index of the largest entry in
58269** the mapping section. (For index blocks other than the last, K will
58270** always be exactly HASHTABLE_NPAGE (4096) and for the last index block
58271** K will be (mxFrame%HASHTABLE_NPAGE).) Unused slots of the hash table
58272** contain a value of 0.
58273**
58274** To look for page P in the hash table, first compute a hash iKey on
58275** P as follows:
58276**
58277** iKey = (P * 383) % HASHTABLE_NSLOT
58278**
58279** Then start scanning entries of the hash table, starting with iKey
58280** (wrapping around to the beginning when the end of the hash table is
58281** reached) until an unused hash slot is found. Let the first unused slot
58282** be at index iUnused. (iUnused might be less than iKey if there was
58283** wrap-around.) Because the hash table is never more than half full,
58284** the search is guaranteed to eventually hit an unused entry. Let
58285** iMax be the value between iKey and iUnused, closest to iUnused,
58286** where aHash[iMax]==P. If there is no iMax entry (if there exists
58287** no hash slot such that aHash[i]==p) then page P is not in the
58288** current index block. Otherwise the iMax-th mapping entry of the
58289** current index block corresponds to the last entry that references
58290** page P.
58291**
58292** A hash search begins with the last index block and moves toward the
58293** first index block, looking for entries corresponding to page P. On
58294** average, only two or three slots in each index block need to be
58295** examined in order to either find the last entry for page P, or to
58296** establish that no such entry exists in the block. Each index block
58297** holds over 4000 entries. So two or three index blocks are sufficient
58298** to cover a typical 10 megabyte WAL file, assuming 1K pages. 8 or 10
58299** comparisons (on average) suffice to either locate a frame in the
58300** WAL or to establish that the frame does not exist in the WAL. This
58301** is much faster than scanning the entire 10MB WAL.
58302**
58303** Note that entries are added in order of increasing K. Hence, one
58304** reader might be using some value K0 and a second reader that started
58305** at a later time (after additional transactions were added to the WAL
58306** and to the wal-index) might be using a different value K1, where K1>K0.
58307** Both readers can use the same hash table and mapping section to get
58308** the correct result. There may be entries in the hash table with
58309** K>K0 but to the first reader, those entries will appear to be unused
58310** slots in the hash table and so the first reader will get an answer as
58311** if no values greater than K0 had ever been inserted into the hash table
58312** in the first place - which is what reader one wants. Meanwhile, the
58313** second reader using K1 will see additional values that were inserted
58314** later, which is exactly what reader two wants.
58315**
58316** When a rollback occurs, the value of K is decreased. Hash table entries
58317** that correspond to frames greater than the new K value are removed
58318** from the hash table at this point.
58319*/
58320#ifndef SQLITE_OMIT_WAL
58321
58322/* #include "wal.h" */
58323
58324/*
58325** Trace output macros
58326*/
58327#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
58328SQLITE_PRIVATE int sqlite3WalTrace = 0;
58329# define WALTRACE(X) if(sqlite3WalTrace) sqlite3DebugPrintf X
58330#else
58331# define WALTRACE(X)
58332#endif
58333
58334/*
58335** WAL mode depends on atomic aligned 32-bit loads and stores in a few
58336** places. The following macros try to make this explicit.
58337*/
58338#if GCC_VESRION>=5004000
58339# define AtomicLoad(PTR) __atomic_load_n((PTR),__ATOMIC_RELAXED)
58340# define AtomicStore(PTR,VAL) __atomic_store_n((PTR),(VAL),__ATOMIC_RELAXED)
58341#else
58342# define AtomicLoad(PTR) (*(PTR))
58343# define AtomicStore(PTR,VAL) (*(PTR) = (VAL))
58344#endif
58345
58346/*
58347** The maximum (and only) versions of the wal and wal-index formats
58348** that may be interpreted by this version of SQLite.
58349**
58350** If a client begins recovering a WAL file and finds that (a) the checksum
58351** values in the wal-header are correct and (b) the version field is not
58352** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
58353**
58354** Similarly, if a client successfully reads a wal-index header (i.e. the
58355** checksum test is successful) and finds that the version field is not
58356** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
58357** returns SQLITE_CANTOPEN.
58358*/
58359#define WAL_MAX_VERSION 3007000
58360#define WALINDEX_MAX_VERSION 3007000
58361
58362/*
58363** Index numbers for various locking bytes. WAL_NREADER is the number
58364** of available reader locks and should be at least 3. The default
58365** is SQLITE_SHM_NLOCK==8 and WAL_NREADER==5.
58366**
58367** Technically, the various VFSes are free to implement these locks however
58368** they see fit. However, compatibility is encouraged so that VFSes can
58369** interoperate. The standard implemention used on both unix and windows
58370** is for the index number to indicate a byte offset into the
58371** WalCkptInfo.aLock[] array in the wal-index header. In other words, all
58372** locks are on the shm file. The WALINDEX_LOCK_OFFSET constant (which
58373** should be 120) is the location in the shm file for the first locking
58374** byte.
58375*/
58376#define WAL_WRITE_LOCK 0
58377#define WAL_ALL_BUT_WRITE 1
58378#define WAL_CKPT_LOCK 1
58379#define WAL_RECOVER_LOCK 2
58380#define WAL_READ_LOCK(I) (3+(I))
58381#define WAL_NREADER (SQLITE_SHM_NLOCK-3)
58382
58383
58384/* Object declarations */
58385typedef struct WalIndexHdr WalIndexHdr;
58386typedef struct WalIterator WalIterator;
58387typedef struct WalCkptInfo WalCkptInfo;
58388
58389
58390/*
58391** The following object holds a copy of the wal-index header content.
58392**
58393** The actual header in the wal-index consists of two copies of this
58394** object followed by one instance of the WalCkptInfo object.
58395** For all versions of SQLite through 3.10.0 and probably beyond,
58396** the locking bytes (WalCkptInfo.aLock) start at offset 120 and
58397** the total header size is 136 bytes.
58398**
58399** The szPage value can be any power of 2 between 512 and 32768, inclusive.
58400** Or it can be 1 to represent a 65536-byte page. The latter case was
58401** added in 3.7.1 when support for 64K pages was added.
58402*/
58403struct WalIndexHdr {
58404 u32 iVersion; /* Wal-index version */
58405 u32 unused; /* Unused (padding) field */
58406 u32 iChange; /* Counter incremented each transaction */
58407 u8 isInit; /* 1 when initialized */
58408 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
58409 u16 szPage; /* Database page size in bytes. 1==64K */
58410 u32 mxFrame; /* Index of last valid frame in the WAL */
58411 u32 nPage; /* Size of database in pages */
58412 u32 aFrameCksum[2]; /* Checksum of last frame in log */
58413 u32 aSalt[2]; /* Two salt values copied from WAL header */
58414 u32 aCksum[2]; /* Checksum over all prior fields */
58415};
58416
58417/*
58418** A copy of the following object occurs in the wal-index immediately
58419** following the second copy of the WalIndexHdr. This object stores
58420** information used by checkpoint.
58421**
58422** nBackfill is the number of frames in the WAL that have been written
58423** back into the database. (We call the act of moving content from WAL to
58424** database "backfilling".) The nBackfill number is never greater than
58425** WalIndexHdr.mxFrame. nBackfill can only be increased by threads
58426** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
58427** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
58428** mxFrame back to zero when the WAL is reset.
58429**
58430** nBackfillAttempted is the largest value of nBackfill that a checkpoint
58431** has attempted to achieve. Normally nBackfill==nBackfillAtempted, however
58432** the nBackfillAttempted is set before any backfilling is done and the
58433** nBackfill is only set after all backfilling completes. So if a checkpoint
58434** crashes, nBackfillAttempted might be larger than nBackfill. The
58435** WalIndexHdr.mxFrame must never be less than nBackfillAttempted.
58436**
58437** The aLock[] field is a set of bytes used for locking. These bytes should
58438** never be read or written.
58439**
58440** There is one entry in aReadMark[] for each reader lock. If a reader
58441** holds read-lock K, then the value in aReadMark[K] is no greater than
58442** the mxFrame for that reader. The value READMARK_NOT_USED (0xffffffff)
58443** for any aReadMark[] means that entry is unused. aReadMark[0] is
58444** a special case; its value is never used and it exists as a place-holder
58445** to avoid having to offset aReadMark[] indexs by one. Readers holding
58446** WAL_READ_LOCK(0) always ignore the entire WAL and read all content
58447** directly from the database.
58448**
58449** The value of aReadMark[K] may only be changed by a thread that
58450** is holding an exclusive lock on WAL_READ_LOCK(K). Thus, the value of
58451** aReadMark[K] cannot changed while there is a reader is using that mark
58452** since the reader will be holding a shared lock on WAL_READ_LOCK(K).
58453**
58454** The checkpointer may only transfer frames from WAL to database where
58455** the frame numbers are less than or equal to every aReadMark[] that is
58456** in use (that is, every aReadMark[j] for which there is a corresponding
58457** WAL_READ_LOCK(j)). New readers (usually) pick the aReadMark[] with the
58458** largest value and will increase an unused aReadMark[] to mxFrame if there
58459** is not already an aReadMark[] equal to mxFrame. The exception to the
58460** previous sentence is when nBackfill equals mxFrame (meaning that everything
58461** in the WAL has been backfilled into the database) then new readers
58462** will choose aReadMark[0] which has value 0 and hence such reader will
58463** get all their all content directly from the database file and ignore
58464** the WAL.
58465**
58466** Writers normally append new frames to the end of the WAL. However,
58467** if nBackfill equals mxFrame (meaning that all WAL content has been
58468** written back into the database) and if no readers are using the WAL
58469** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then
58470** the writer will first "reset" the WAL back to the beginning and start
58471** writing new content beginning at frame 1.
58472**
58473** We assume that 32-bit loads are atomic and so no locks are needed in
58474** order to read from any aReadMark[] entries.
58475*/
58476struct WalCkptInfo {
58477 u32 nBackfill; /* Number of WAL frames backfilled into DB */
58478 u32 aReadMark[WAL_NREADER]; /* Reader marks */
58479 u8 aLock[SQLITE_SHM_NLOCK]; /* Reserved space for locks */
58480 u32 nBackfillAttempted; /* WAL frames perhaps written, or maybe not */
58481 u32 notUsed0; /* Available for future enhancements */
58482};
58483#define READMARK_NOT_USED 0xffffffff
58484
58485
58486/* A block of WALINDEX_LOCK_RESERVED bytes beginning at
58487** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems
58488** only support mandatory file-locks, we do not read or write data
58489** from the region of the file on which locks are applied.
58490*/
58491#define WALINDEX_LOCK_OFFSET (sizeof(WalIndexHdr)*2+offsetof(WalCkptInfo,aLock))
58492#define WALINDEX_HDR_SIZE (sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))
58493
58494/* Size of header before each frame in wal */
58495#define WAL_FRAME_HDRSIZE 24
58496
58497/* Size of write ahead log header, including checksum. */
58498#define WAL_HDRSIZE 32
58499
58500/* WAL magic value. Either this value, or the same value with the least
58501** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
58502** big-endian format in the first 4 bytes of a WAL file.
58503**
58504** If the LSB is set, then the checksums for each frame within the WAL
58505** file are calculated by treating all data as an array of 32-bit
58506** big-endian words. Otherwise, they are calculated by interpreting
58507** all data as 32-bit little-endian words.
58508*/
58509#define WAL_MAGIC 0x377f0682
58510
58511/*
58512** Return the offset of frame iFrame in the write-ahead log file,
58513** assuming a database page size of szPage bytes. The offset returned
58514** is to the start of the write-ahead log frame-header.
58515*/
58516#define walFrameOffset(iFrame, szPage) ( \
58517 WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE) \
58518)
58519
58520/*
58521** An open write-ahead log file is represented by an instance of the
58522** following object.
58523*/
58524struct Wal {
58525 sqlite3_vfs *pVfs; /* The VFS used to create pDbFd */
58526 sqlite3_file *pDbFd; /* File handle for the database file */
58527 sqlite3_file *pWalFd; /* File handle for WAL file */
58528 u32 iCallback; /* Value to pass to log callback (or 0) */
58529 i64 mxWalSize; /* Truncate WAL to this size upon reset */
58530 int nWiData; /* Size of array apWiData */
58531 int szFirstBlock; /* Size of first block written to WAL file */
58532 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
58533 u32 szPage; /* Database page size */
58534 i16 readLock; /* Which read lock is being held. -1 for none */
58535 u8 syncFlags; /* Flags to use to sync header writes */
58536 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
58537 u8 writeLock; /* True if in a write transaction */
58538 u8 ckptLock; /* True if holding a checkpoint lock */
58539 u8 readOnly; /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */
58540 u8 truncateOnCommit; /* True to truncate WAL file on commit */
58541 u8 syncHeader; /* Fsync the WAL header if true */
58542 u8 padToSectorBoundary; /* Pad transactions out to the next sector */
58543 u8 bShmUnreliable; /* SHM content is read-only and unreliable */
58544 WalIndexHdr hdr; /* Wal-index header for current transaction */
58545 u32 minFrame; /* Ignore wal frames before this one */
58546 u32 iReCksum; /* On commit, recalculate checksums from here */
58547 const char *zWalName; /* Name of WAL file */
58548 u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
58549#ifdef SQLITE_DEBUG
58550 u8 lockError; /* True if a locking error has occurred */
58551#endif
58552#ifdef SQLITE_ENABLE_SNAPSHOT
58553 WalIndexHdr *pSnapshot; /* Start transaction here if not NULL */
58554#endif
58555};
58556
58557/*
58558** Candidate values for Wal.exclusiveMode.
58559*/
58560#define WAL_NORMAL_MODE 0
58561#define WAL_EXCLUSIVE_MODE 1
58562#define WAL_HEAPMEMORY_MODE 2
58563
58564/*
58565** Possible values for WAL.readOnly
58566*/
58567#define WAL_RDWR 0 /* Normal read/write connection */
58568#define WAL_RDONLY 1 /* The WAL file is readonly */
58569#define WAL_SHM_RDONLY 2 /* The SHM file is readonly */
58570
58571/*
58572** Each page of the wal-index mapping contains a hash-table made up of
58573** an array of HASHTABLE_NSLOT elements of the following type.
58574*/
58575typedef u16 ht_slot;
58576
58577/*
58578** This structure is used to implement an iterator that loops through
58579** all frames in the WAL in database page order. Where two or more frames
58580** correspond to the same database page, the iterator visits only the
58581** frame most recently written to the WAL (in other words, the frame with
58582** the largest index).
58583**
58584** The internals of this structure are only accessed by:
58585**
58586** walIteratorInit() - Create a new iterator,
58587** walIteratorNext() - Step an iterator,
58588** walIteratorFree() - Free an iterator.
58589**
58590** This functionality is used by the checkpoint code (see walCheckpoint()).
58591*/
58592struct WalIterator {
58593 int iPrior; /* Last result returned from the iterator */
58594 int nSegment; /* Number of entries in aSegment[] */
58595 struct WalSegment {
58596 int iNext; /* Next slot in aIndex[] not yet returned */
58597 ht_slot *aIndex; /* i0, i1, i2... such that aPgno[iN] ascend */
58598 u32 *aPgno; /* Array of page numbers. */
58599 int nEntry; /* Nr. of entries in aPgno[] and aIndex[] */
58600 int iZero; /* Frame number associated with aPgno[0] */
58601 } aSegment[1]; /* One for every 32KB page in the wal-index */
58602};
58603
58604/*
58605** Define the parameters of the hash tables in the wal-index file. There
58606** is a hash-table following every HASHTABLE_NPAGE page numbers in the
58607** wal-index.
58608**
58609** Changing any of these constants will alter the wal-index format and
58610** create incompatibilities.
58611*/
58612#define HASHTABLE_NPAGE 4096 /* Must be power of 2 */
58613#define HASHTABLE_HASH_1 383 /* Should be prime */
58614#define HASHTABLE_NSLOT (HASHTABLE_NPAGE*2) /* Must be a power of 2 */
58615
58616/*
58617** The block of page numbers associated with the first hash-table in a
58618** wal-index is smaller than usual. This is so that there is a complete
58619** hash-table on each aligned 32KB page of the wal-index.
58620*/
58621#define HASHTABLE_NPAGE_ONE (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
58622
58623/* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
58624#define WALINDEX_PGSZ ( \
58625 sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \
58626)
58627
58628/*
58629** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
58630** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
58631** numbered from zero.
58632**
58633** If the wal-index is currently smaller the iPage pages then the size
58634** of the wal-index might be increased, but only if it is safe to do
58635** so. It is safe to enlarge the wal-index if pWal->writeLock is true
58636** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
58637**
58638** If this call is successful, *ppPage is set to point to the wal-index
58639** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
58640** then an SQLite error code is returned and *ppPage is set to 0.
58641*/
58642static SQLITE_NOINLINE int walIndexPageRealloc(
58643 Wal *pWal, /* The WAL context */
58644 int iPage, /* The page we seek */
58645 volatile u32 **ppPage /* Write the page pointer here */
58646){
58647 int rc = SQLITE_OK;
58648
58649 /* Enlarge the pWal->apWiData[] array if required */
58650 if( pWal->nWiData<=iPage ){
58651 int nByte = sizeof(u32*)*(iPage+1);
58652 volatile u32 **apNew;
58653 apNew = (volatile u32 **)sqlite3_realloc64((void *)pWal->apWiData, nByte);
58654 if( !apNew ){
58655 *ppPage = 0;
58656 return SQLITE_NOMEM_BKPT;
58657 }
58658 memset((void*)&apNew[pWal->nWiData], 0,
58659 sizeof(u32*)*(iPage+1-pWal->nWiData));
58660 pWal->apWiData = apNew;
58661 pWal->nWiData = iPage+1;
58662 }
58663
58664 /* Request a pointer to the required page from the VFS */
58665 assert( pWal->apWiData[iPage]==0 );
58666 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
58667 pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
58668 if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
58669 }else{
58670 rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
58671 pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
58672 );
58673 assert( pWal->apWiData[iPage]!=0 || rc!=SQLITE_OK || pWal->writeLock==0 );
58674 testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
58675 if( (rc&0xff)==SQLITE_READONLY ){
58676 pWal->readOnly |= WAL_SHM_RDONLY;
58677 if( rc==SQLITE_READONLY ){
58678 rc = SQLITE_OK;
58679 }
58680 }
58681 }
58682
58683 *ppPage = pWal->apWiData[iPage];
58684 assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
58685 return rc;
58686}
58687static int walIndexPage(
58688 Wal *pWal, /* The WAL context */
58689 int iPage, /* The page we seek */
58690 volatile u32 **ppPage /* Write the page pointer here */
58691){
58692 if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
58693 return walIndexPageRealloc(pWal, iPage, ppPage);
58694 }
58695 return SQLITE_OK;
58696}
58697
58698/*
58699** Return a pointer to the WalCkptInfo structure in the wal-index.
58700*/
58701static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
58702 assert( pWal->nWiData>0 && pWal->apWiData[0] );
58703 return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
58704}
58705
58706/*
58707** Return a pointer to the WalIndexHdr structure in the wal-index.
58708*/
58709static volatile WalIndexHdr *walIndexHdr(Wal *pWal){
58710 assert( pWal->nWiData>0 && pWal->apWiData[0] );
58711 return (volatile WalIndexHdr*)pWal->apWiData[0];
58712}
58713
58714/*
58715** The argument to this macro must be of type u32. On a little-endian
58716** architecture, it returns the u32 value that results from interpreting
58717** the 4 bytes as a big-endian value. On a big-endian architecture, it
58718** returns the value that would be produced by interpreting the 4 bytes
58719** of the input value as a little-endian integer.
58720*/
58721#define BYTESWAP32(x) ( \
58722 (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8) \
58723 + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>24) \
58724)
58725
58726/*
58727** Generate or extend an 8 byte checksum based on the data in
58728** array aByte[] and the initial values of aIn[0] and aIn[1] (or
58729** initial values of 0 and 0 if aIn==NULL).
58730**
58731** The checksum is written back into aOut[] before returning.
58732**
58733** nByte must be a positive multiple of 8.
58734*/
58735static void walChecksumBytes(
58736 int nativeCksum, /* True for native byte-order, false for non-native */
58737 u8 *a, /* Content to be checksummed */
58738 int nByte, /* Bytes of content in a[]. Must be a multiple of 8. */
58739 const u32 *aIn, /* Initial checksum value input */
58740 u32 *aOut /* OUT: Final checksum value output */
58741){
58742 u32 s1, s2;
58743 u32 *aData = (u32 *)a;
58744 u32 *aEnd = (u32 *)&a[nByte];
58745
58746 if( aIn ){
58747 s1 = aIn[0];
58748 s2 = aIn[1];
58749 }else{
58750 s1 = s2 = 0;
58751 }
58752
58753 assert( nByte>=8 );
58754 assert( (nByte&0x00000007)==0 );
58755
58756 if( nativeCksum ){
58757 do {
58758 s1 += *aData++ + s2;
58759 s2 += *aData++ + s1;
58760 }while( aData<aEnd );
58761 }else{
58762 do {
58763 s1 += BYTESWAP32(aData[0]) + s2;
58764 s2 += BYTESWAP32(aData[1]) + s1;
58765 aData += 2;
58766 }while( aData<aEnd );
58767 }
58768
58769 aOut[0] = s1;
58770 aOut[1] = s2;
58771}
58772
58773static void walShmBarrier(Wal *pWal){
58774 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
58775 sqlite3OsShmBarrier(pWal->pDbFd);
58776 }
58777}
58778
58779/*
58780** Write the header information in pWal->hdr into the wal-index.
58781**
58782** The checksum on pWal->hdr is updated before it is written.
58783*/
58784static void walIndexWriteHdr(Wal *pWal){
58785 volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
58786 const int nCksum = offsetof(WalIndexHdr, aCksum);
58787
58788 assert( pWal->writeLock );
58789 pWal->hdr.isInit = 1;
58790 pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
58791 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
58792 memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
58793 walShmBarrier(pWal);
58794 memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
58795}
58796
58797/*
58798** This function encodes a single frame header and writes it to a buffer
58799** supplied by the caller. A frame-header is made up of a series of
58800** 4-byte big-endian integers, as follows:
58801**
58802** 0: Page number.
58803** 4: For commit records, the size of the database image in pages
58804** after the commit. For all other records, zero.
58805** 8: Salt-1 (copied from the wal-header)
58806** 12: Salt-2 (copied from the wal-header)
58807** 16: Checksum-1.
58808** 20: Checksum-2.
58809*/
58810static void walEncodeFrame(
58811 Wal *pWal, /* The write-ahead log */
58812 u32 iPage, /* Database page number for frame */
58813 u32 nTruncate, /* New db size (or 0 for non-commit frames) */
58814 u8 *aData, /* Pointer to page data */
58815 u8 *aFrame /* OUT: Write encoded frame here */
58816){
58817 int nativeCksum; /* True for native byte-order checksums */
58818 u32 *aCksum = pWal->hdr.aFrameCksum;
58819 assert( WAL_FRAME_HDRSIZE==24 );
58820 sqlite3Put4byte(&aFrame[0], iPage);
58821 sqlite3Put4byte(&aFrame[4], nTruncate);
58822 if( pWal->iReCksum==0 ){
58823 memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
58824
58825 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
58826 walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
58827 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
58828
58829 sqlite3Put4byte(&aFrame[16], aCksum[0]);
58830 sqlite3Put4byte(&aFrame[20], aCksum[1]);
58831 }else{
58832 memset(&aFrame[8], 0, 16);
58833 }
58834}
58835
58836/*
58837** Check to see if the frame with header in aFrame[] and content
58838** in aData[] is valid. If it is a valid frame, fill *piPage and
58839** *pnTruncate and return true. Return if the frame is not valid.
58840*/
58841static int walDecodeFrame(
58842 Wal *pWal, /* The write-ahead log */
58843 u32 *piPage, /* OUT: Database page number for frame */
58844 u32 *pnTruncate, /* OUT: New db size (or 0 if not commit) */
58845 u8 *aData, /* Pointer to page data (for checksum) */
58846 u8 *aFrame /* Frame data */
58847){
58848 int nativeCksum; /* True for native byte-order checksums */
58849 u32 *aCksum = pWal->hdr.aFrameCksum;
58850 u32 pgno; /* Page number of the frame */
58851 assert( WAL_FRAME_HDRSIZE==24 );
58852
58853 /* A frame is only valid if the salt values in the frame-header
58854 ** match the salt values in the wal-header.
58855 */
58856 if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
58857 return 0;
58858 }
58859
58860 /* A frame is only valid if the page number is creater than zero.
58861 */
58862 pgno = sqlite3Get4byte(&aFrame[0]);
58863 if( pgno==0 ){
58864 return 0;
58865 }
58866
58867 /* A frame is only valid if a checksum of the WAL header,
58868 ** all prior frams, the first 16 bytes of this frame-header,
58869 ** and the frame-data matches the checksum in the last 8
58870 ** bytes of this frame-header.
58871 */
58872 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
58873 walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
58874 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
58875 if( aCksum[0]!=sqlite3Get4byte(&aFrame[16])
58876 || aCksum[1]!=sqlite3Get4byte(&aFrame[20])
58877 ){
58878 /* Checksum failed. */
58879 return 0;
58880 }
58881
58882 /* If we reach this point, the frame is valid. Return the page number
58883 ** and the new database size.
58884 */
58885 *piPage = pgno;
58886 *pnTruncate = sqlite3Get4byte(&aFrame[4]);
58887 return 1;
58888}
58889
58890
58891#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
58892/*
58893** Names of locks. This routine is used to provide debugging output and is not
58894** a part of an ordinary build.
58895*/
58896static const char *walLockName(int lockIdx){
58897 if( lockIdx==WAL_WRITE_LOCK ){
58898 return "WRITE-LOCK";
58899 }else if( lockIdx==WAL_CKPT_LOCK ){
58900 return "CKPT-LOCK";
58901 }else if( lockIdx==WAL_RECOVER_LOCK ){
58902 return "RECOVER-LOCK";
58903 }else{
58904 static char zName[15];
58905 sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
58906 lockIdx-WAL_READ_LOCK(0));
58907 return zName;
58908 }
58909}
58910#endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
58911
58912
58913/*
58914** Set or release locks on the WAL. Locks are either shared or exclusive.
58915** A lock cannot be moved directly between shared and exclusive - it must go
58916** through the unlocked state first.
58917**
58918** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
58919*/
58920static int walLockShared(Wal *pWal, int lockIdx){
58921 int rc;
58922 if( pWal->exclusiveMode ) return SQLITE_OK;
58923 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
58924 SQLITE_SHM_LOCK | SQLITE_SHM_SHARED);
58925 WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
58926 walLockName(lockIdx), rc ? "failed" : "ok"));
58927 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
58928 return rc;
58929}
58930static void walUnlockShared(Wal *pWal, int lockIdx){
58931 if( pWal->exclusiveMode ) return;
58932 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
58933 SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED);
58934 WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
58935}
58936static int walLockExclusive(Wal *pWal, int lockIdx, int n){
58937 int rc;
58938 if( pWal->exclusiveMode ) return SQLITE_OK;
58939 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
58940 SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE);
58941 WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
58942 walLockName(lockIdx), n, rc ? "failed" : "ok"));
58943 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
58944 return rc;
58945}
58946static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){
58947 if( pWal->exclusiveMode ) return;
58948 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
58949 SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE);
58950 WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
58951 walLockName(lockIdx), n));
58952}
58953
58954/*
58955** Compute a hash on a page number. The resulting hash value must land
58956** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
58957** the hash to the next value in the event of a collision.
58958*/
58959static int walHash(u32 iPage){
58960 assert( iPage>0 );
58961 assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
58962 return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
58963}
58964static int walNextHash(int iPriorHash){
58965 return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
58966}
58967
58968/*
58969** An instance of the WalHashLoc object is used to describe the location
58970** of a page hash table in the wal-index. This becomes the return value
58971** from walHashGet().
58972*/
58973typedef struct WalHashLoc WalHashLoc;
58974struct WalHashLoc {
58975 volatile ht_slot *aHash; /* Start of the wal-index hash table */
58976 volatile u32 *aPgno; /* aPgno[1] is the page of first frame indexed */
58977 u32 iZero; /* One less than the frame number of first indexed*/
58978};
58979
58980/*
58981** Return pointers to the hash table and page number array stored on
58982** page iHash of the wal-index. The wal-index is broken into 32KB pages
58983** numbered starting from 0.
58984**
58985** Set output variable pLoc->aHash to point to the start of the hash table
58986** in the wal-index file. Set pLoc->iZero to one less than the frame
58987** number of the first frame indexed by this hash table. If a
58988** slot in the hash table is set to N, it refers to frame number
58989** (pLoc->iZero+N) in the log.
58990**
58991** Finally, set pLoc->aPgno so that pLoc->aPgno[1] is the page number of the
58992** first frame indexed by the hash table, frame (pLoc->iZero+1).
58993*/
58994static int walHashGet(
58995 Wal *pWal, /* WAL handle */
58996 int iHash, /* Find the iHash'th table */
58997 WalHashLoc *pLoc /* OUT: Hash table location */
58998){
58999 int rc; /* Return code */
59000
59001 rc = walIndexPage(pWal, iHash, &pLoc->aPgno);
59002 assert( rc==SQLITE_OK || iHash>0 );
59003
59004 if( rc==SQLITE_OK ){
59005 pLoc->aHash = (volatile ht_slot *)&pLoc->aPgno[HASHTABLE_NPAGE];
59006 if( iHash==0 ){
59007 pLoc->aPgno = &pLoc->aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
59008 pLoc->iZero = 0;
59009 }else{
59010 pLoc->iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
59011 }
59012 pLoc->aPgno = &pLoc->aPgno[-1];
59013 }
59014 return rc;
59015}
59016
59017/*
59018** Return the number of the wal-index page that contains the hash-table
59019** and page-number array that contain entries corresponding to WAL frame
59020** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
59021** are numbered starting from 0.
59022*/
59023static int walFramePage(u32 iFrame){
59024 int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
59025 assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE)
59026 && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE)
59027 && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE))
59028 && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)
59029 && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE))
59030 );
59031 return iHash;
59032}
59033
59034/*
59035** Return the page number associated with frame iFrame in this WAL.
59036*/
59037static u32 walFramePgno(Wal *pWal, u32 iFrame){
59038 int iHash = walFramePage(iFrame);
59039 if( iHash==0 ){
59040 return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
59041 }
59042 return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
59043}
59044
59045/*
59046** Remove entries from the hash table that point to WAL slots greater
59047** than pWal->hdr.mxFrame.
59048**
59049** This function is called whenever pWal->hdr.mxFrame is decreased due
59050** to a rollback or savepoint.
59051**
59052** At most only the hash table containing pWal->hdr.mxFrame needs to be
59053** updated. Any later hash tables will be automatically cleared when
59054** pWal->hdr.mxFrame advances to the point where those hash tables are
59055** actually needed.
59056*/
59057static void walCleanupHash(Wal *pWal){
59058 WalHashLoc sLoc; /* Hash table location */
59059 int iLimit = 0; /* Zero values greater than this */
59060 int nByte; /* Number of bytes to zero in aPgno[] */
59061 int i; /* Used to iterate through aHash[] */
59062
59063 assert( pWal->writeLock );
59064 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
59065 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
59066 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
59067
59068 if( pWal->hdr.mxFrame==0 ) return;
59069
59070 /* Obtain pointers to the hash-table and page-number array containing
59071 ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
59072 ** that the page said hash-table and array reside on is already mapped.
59073 */
59074 assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
59075 assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
59076 walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
59077
59078 /* Zero all hash-table entries that correspond to frame numbers greater
59079 ** than pWal->hdr.mxFrame.
59080 */
59081 iLimit = pWal->hdr.mxFrame - sLoc.iZero;
59082 assert( iLimit>0 );
59083 for(i=0; i<HASHTABLE_NSLOT; i++){
59084 if( sLoc.aHash[i]>iLimit ){
59085 sLoc.aHash[i] = 0;
59086 }
59087 }
59088
59089 /* Zero the entries in the aPgno array that correspond to frames with
59090 ** frame numbers greater than pWal->hdr.mxFrame.
59091 */
59092 nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit+1]);
59093 memset((void *)&sLoc.aPgno[iLimit+1], 0, nByte);
59094
59095#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
59096 /* Verify that the every entry in the mapping region is still reachable
59097 ** via the hash table even after the cleanup.
59098 */
59099 if( iLimit ){
59100 int j; /* Loop counter */
59101 int iKey; /* Hash key */
59102 for(j=1; j<=iLimit; j++){
59103 for(iKey=walHash(sLoc.aPgno[j]);sLoc.aHash[iKey];iKey=walNextHash(iKey)){
59104 if( sLoc.aHash[iKey]==j ) break;
59105 }
59106 assert( sLoc.aHash[iKey]==j );
59107 }
59108 }
59109#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
59110}
59111
59112
59113/*
59114** Set an entry in the wal-index that will map database page number
59115** pPage into WAL frame iFrame.
59116*/
59117static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
59118 int rc; /* Return code */
59119 WalHashLoc sLoc; /* Wal-index hash table location */
59120
59121 rc = walHashGet(pWal, walFramePage(iFrame), &sLoc);
59122
59123 /* Assuming the wal-index file was successfully mapped, populate the
59124 ** page number array and hash table entry.
59125 */
59126 if( rc==SQLITE_OK ){
59127 int iKey; /* Hash table key */
59128 int idx; /* Value to write to hash-table slot */
59129 int nCollide; /* Number of hash collisions */
59130
59131 idx = iFrame - sLoc.iZero;
59132 assert( idx <= HASHTABLE_NSLOT/2 + 1 );
59133
59134 /* If this is the first entry to be added to this hash-table, zero the
59135 ** entire hash table and aPgno[] array before proceeding.
59136 */
59137 if( idx==1 ){
59138 int nByte = (int)((u8 *)&sLoc.aHash[HASHTABLE_NSLOT]
59139 - (u8 *)&sLoc.aPgno[1]);
59140 memset((void*)&sLoc.aPgno[1], 0, nByte);
59141 }
59142
59143 /* If the entry in aPgno[] is already set, then the previous writer
59144 ** must have exited unexpectedly in the middle of a transaction (after
59145 ** writing one or more dirty pages to the WAL to free up memory).
59146 ** Remove the remnants of that writers uncommitted transaction from
59147 ** the hash-table before writing any new entries.
59148 */
59149 if( sLoc.aPgno[idx] ){
59150 walCleanupHash(pWal);
59151 assert( !sLoc.aPgno[idx] );
59152 }
59153
59154 /* Write the aPgno[] array entry and the hash-table slot. */
59155 nCollide = idx;
59156 for(iKey=walHash(iPage); sLoc.aHash[iKey]; iKey=walNextHash(iKey)){
59157 if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
59158 }
59159 sLoc.aPgno[idx] = iPage;
59160 sLoc.aHash[iKey] = (ht_slot)idx;
59161
59162#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
59163 /* Verify that the number of entries in the hash table exactly equals
59164 ** the number of entries in the mapping region.
59165 */
59166 {
59167 int i; /* Loop counter */
59168 int nEntry = 0; /* Number of entries in the hash table */
59169 for(i=0; i<HASHTABLE_NSLOT; i++){ if( sLoc.aHash[i] ) nEntry++; }
59170 assert( nEntry==idx );
59171 }
59172
59173 /* Verify that the every entry in the mapping region is reachable
59174 ** via the hash table. This turns out to be a really, really expensive
59175 ** thing to check, so only do this occasionally - not on every
59176 ** iteration.
59177 */
59178 if( (idx&0x3ff)==0 ){
59179 int i; /* Loop counter */
59180 for(i=1; i<=idx; i++){
59181 for(iKey=walHash(sLoc.aPgno[i]);
59182 sLoc.aHash[iKey];
59183 iKey=walNextHash(iKey)){
59184 if( sLoc.aHash[iKey]==i ) break;
59185 }
59186 assert( sLoc.aHash[iKey]==i );
59187 }
59188 }
59189#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
59190 }
59191
59192
59193 return rc;
59194}
59195
59196
59197/*
59198** Recover the wal-index by reading the write-ahead log file.
59199**
59200** This routine first tries to establish an exclusive lock on the
59201** wal-index to prevent other threads/processes from doing anything
59202** with the WAL or wal-index while recovery is running. The
59203** WAL_RECOVER_LOCK is also held so that other threads will know
59204** that this thread is running recovery. If unable to establish
59205** the necessary locks, this routine returns SQLITE_BUSY.
59206*/
59207static int walIndexRecover(Wal *pWal){
59208 int rc; /* Return Code */
59209 i64 nSize; /* Size of log file */
59210 u32 aFrameCksum[2] = {0, 0};
59211 int iLock; /* Lock offset to lock for checkpoint */
59212
59213 /* Obtain an exclusive lock on all byte in the locking range not already
59214 ** locked by the caller. The caller is guaranteed to have locked the
59215 ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
59216 ** If successful, the same bytes that are locked here are unlocked before
59217 ** this function returns.
59218 */
59219 assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
59220 assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 );
59221 assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE );
59222 assert( pWal->writeLock );
59223 iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
59224 rc = walLockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
59225 if( rc==SQLITE_OK ){
59226 rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
59227 if( rc!=SQLITE_OK ){
59228 walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
59229 }
59230 }
59231 if( rc ){
59232 return rc;
59233 }
59234
59235 WALTRACE(("WAL%p: recovery begin...\n", pWal));
59236
59237 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
59238
59239 rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
59240 if( rc!=SQLITE_OK ){
59241 goto recovery_error;
59242 }
59243
59244 if( nSize>WAL_HDRSIZE ){
59245 u8 aBuf[WAL_HDRSIZE]; /* Buffer to load WAL header into */
59246 u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
59247 int szFrame; /* Number of bytes in buffer aFrame[] */
59248 u8 *aData; /* Pointer to data part of aFrame buffer */
59249 int iFrame; /* Index of last frame read */
59250 i64 iOffset; /* Next offset to read from log file */
59251 int szPage; /* Page size according to the log */
59252 u32 magic; /* Magic value read from WAL header */
59253 u32 version; /* Magic value read from WAL header */
59254 int isValid; /* True if this frame is valid */
59255
59256 /* Read in the WAL header. */
59257 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
59258 if( rc!=SQLITE_OK ){
59259 goto recovery_error;
59260 }
59261
59262 /* If the database page size is not a power of two, or is greater than
59263 ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid
59264 ** data. Similarly, if the 'magic' value is invalid, ignore the whole
59265 ** WAL file.
59266 */
59267 magic = sqlite3Get4byte(&aBuf[0]);
59268 szPage = sqlite3Get4byte(&aBuf[8]);
59269 if( (magic&0xFFFFFFFE)!=WAL_MAGIC
59270 || szPage&(szPage-1)
59271 || szPage>SQLITE_MAX_PAGE_SIZE
59272 || szPage<512
59273 ){
59274 goto finished;
59275 }
59276 pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
59277 pWal->szPage = szPage;
59278 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
59279 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
59280
59281 /* Verify that the WAL header checksum is correct */
59282 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
59283 aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
59284 );
59285 if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
59286 || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
59287 ){
59288 goto finished;
59289 }
59290
59291 /* Verify that the version number on the WAL format is one that
59292 ** are able to understand */
59293 version = sqlite3Get4byte(&aBuf[4]);
59294 if( version!=WAL_MAX_VERSION ){
59295 rc = SQLITE_CANTOPEN_BKPT;
59296 goto finished;
59297 }
59298
59299 /* Malloc a buffer to read frames into. */
59300 szFrame = szPage + WAL_FRAME_HDRSIZE;
59301 aFrame = (u8 *)sqlite3_malloc64(szFrame);
59302 if( !aFrame ){
59303 rc = SQLITE_NOMEM_BKPT;
59304 goto recovery_error;
59305 }
59306 aData = &aFrame[WAL_FRAME_HDRSIZE];
59307
59308 /* Read all frames from the log file. */
59309 iFrame = 0;
59310 for(iOffset=WAL_HDRSIZE; (iOffset+szFrame)<=nSize; iOffset+=szFrame){
59311 u32 pgno; /* Database page number for frame */
59312 u32 nTruncate; /* dbsize field from frame header */
59313
59314 /* Read and decode the next log frame. */
59315 iFrame++;
59316 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
59317 if( rc!=SQLITE_OK ) break;
59318 isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
59319 if( !isValid ) break;
59320 rc = walIndexAppend(pWal, iFrame, pgno);
59321 if( rc!=SQLITE_OK ) break;
59322
59323 /* If nTruncate is non-zero, this is a commit record. */
59324 if( nTruncate ){
59325 pWal->hdr.mxFrame = iFrame;
59326 pWal->hdr.nPage = nTruncate;
59327 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
59328 testcase( szPage<=32768 );
59329 testcase( szPage>=65536 );
59330 aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
59331 aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
59332 }
59333 }
59334
59335 sqlite3_free(aFrame);
59336 }
59337
59338finished:
59339 if( rc==SQLITE_OK ){
59340 volatile WalCkptInfo *pInfo;
59341 int i;
59342 pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
59343 pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
59344 walIndexWriteHdr(pWal);
59345
59346 /* Reset the checkpoint-header. This is safe because this thread is
59347 ** currently holding locks that exclude all other readers, writers and
59348 ** checkpointers.
59349 */
59350 pInfo = walCkptInfo(pWal);
59351 pInfo->nBackfill = 0;
59352 pInfo->nBackfillAttempted = pWal->hdr.mxFrame;
59353 pInfo->aReadMark[0] = 0;
59354 for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
59355 if( pWal->hdr.mxFrame ) pInfo->aReadMark[1] = pWal->hdr.mxFrame;
59356
59357 /* If more than one frame was recovered from the log file, report an
59358 ** event via sqlite3_log(). This is to help with identifying performance
59359 ** problems caused by applications routinely shutting down without
59360 ** checkpointing the log file.
59361 */
59362 if( pWal->hdr.nPage ){
59363 sqlite3_log(SQLITE_NOTICE_RECOVER_WAL,
59364 "recovered %d frames from WAL file %s",
59365 pWal->hdr.mxFrame, pWal->zWalName
59366 );
59367 }
59368 }
59369
59370recovery_error:
59371 WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
59372 walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
59373 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
59374 return rc;
59375}
59376
59377/*
59378** Close an open wal-index.
59379*/
59380static void walIndexClose(Wal *pWal, int isDelete){
59381 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE || pWal->bShmUnreliable ){
59382 int i;
59383 for(i=0; i<pWal->nWiData; i++){
59384 sqlite3_free((void *)pWal->apWiData[i]);
59385 pWal->apWiData[i] = 0;
59386 }
59387 }
59388 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
59389 sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
59390 }
59391}
59392
59393/*
59394** Open a connection to the WAL file zWalName. The database file must
59395** already be opened on connection pDbFd. The buffer that zWalName points
59396** to must remain valid for the lifetime of the returned Wal* handle.
59397**
59398** A SHARED lock should be held on the database file when this function
59399** is called. The purpose of this SHARED lock is to prevent any other
59400** client from unlinking the WAL or wal-index file. If another process
59401** were to do this just after this client opened one of these files, the
59402** system would be badly broken.
59403**
59404** If the log file is successfully opened, SQLITE_OK is returned and
59405** *ppWal is set to point to a new WAL handle. If an error occurs,
59406** an SQLite error code is returned and *ppWal is left unmodified.
59407*/
59408SQLITE_PRIVATE int sqlite3WalOpen(
59409 sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
59410 sqlite3_file *pDbFd, /* The open database file */
59411 const char *zWalName, /* Name of the WAL file */
59412 int bNoShm, /* True to run in heap-memory mode */
59413 i64 mxWalSize, /* Truncate WAL to this size on reset */
59414 Wal **ppWal /* OUT: Allocated Wal handle */
59415){
59416 int rc; /* Return Code */
59417 Wal *pRet; /* Object to allocate and return */
59418 int flags; /* Flags passed to OsOpen() */
59419
59420 assert( zWalName && zWalName[0] );
59421 assert( pDbFd );
59422
59423 /* In the amalgamation, the os_unix.c and os_win.c source files come before
59424 ** this source file. Verify that the #defines of the locking byte offsets
59425 ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
59426 ** For that matter, if the lock offset ever changes from its initial design
59427 ** value of 120, we need to know that so there is an assert() to check it.
59428 */
59429 assert( 120==WALINDEX_LOCK_OFFSET );
59430 assert( 136==WALINDEX_HDR_SIZE );
59431#ifdef WIN_SHM_BASE
59432 assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET );
59433#endif
59434#ifdef UNIX_SHM_BASE
59435 assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET );
59436#endif
59437
59438
59439 /* Allocate an instance of struct Wal to return. */
59440 *ppWal = 0;
59441 pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
59442 if( !pRet ){
59443 return SQLITE_NOMEM_BKPT;
59444 }
59445
59446 pRet->pVfs = pVfs;
59447 pRet->pWalFd = (sqlite3_file *)&pRet[1];
59448 pRet->pDbFd = pDbFd;
59449 pRet->readLock = -1;
59450 pRet->mxWalSize = mxWalSize;
59451 pRet->zWalName = zWalName;
59452 pRet->syncHeader = 1;
59453 pRet->padToSectorBoundary = 1;
59454 pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
59455
59456 /* Open file handle on the write-ahead log file. */
59457 flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL);
59458 rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
59459 if( rc==SQLITE_OK && flags&SQLITE_OPEN_READONLY ){
59460 pRet->readOnly = WAL_RDONLY;
59461 }
59462
59463 if( rc!=SQLITE_OK ){
59464 walIndexClose(pRet, 0);
59465 sqlite3OsClose(pRet->pWalFd);
59466 sqlite3_free(pRet);
59467 }else{
59468 int iDC = sqlite3OsDeviceCharacteristics(pDbFd);
59469 if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
59470 if( iDC & SQLITE_IOCAP_POWERSAFE_OVERWRITE ){
59471 pRet->padToSectorBoundary = 0;
59472 }
59473 *ppWal = pRet;
59474 WALTRACE(("WAL%d: opened\n", pRet));
59475 }
59476 return rc;
59477}
59478
59479/*
59480** Change the size to which the WAL file is trucated on each reset.
59481*/
59482SQLITE_PRIVATE void sqlite3WalLimit(Wal *pWal, i64 iLimit){
59483 if( pWal ) pWal->mxWalSize = iLimit;
59484}
59485
59486/*
59487** Find the smallest page number out of all pages held in the WAL that
59488** has not been returned by any prior invocation of this method on the
59489** same WalIterator object. Write into *piFrame the frame index where
59490** that page was last written into the WAL. Write into *piPage the page
59491** number.
59492**
59493** Return 0 on success. If there are no pages in the WAL with a page
59494** number larger than *piPage, then return 1.
59495*/
59496static int walIteratorNext(
59497 WalIterator *p, /* Iterator */
59498 u32 *piPage, /* OUT: The page number of the next page */
59499 u32 *piFrame /* OUT: Wal frame index of next page */
59500){
59501 u32 iMin; /* Result pgno must be greater than iMin */
59502 u32 iRet = 0xFFFFFFFF; /* 0xffffffff is never a valid page number */
59503 int i; /* For looping through segments */
59504
59505 iMin = p->iPrior;
59506 assert( iMin<0xffffffff );
59507 for(i=p->nSegment-1; i>=0; i--){
59508 struct WalSegment *pSegment = &p->aSegment[i];
59509 while( pSegment->iNext<pSegment->nEntry ){
59510 u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
59511 if( iPg>iMin ){
59512 if( iPg<iRet ){
59513 iRet = iPg;
59514 *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
59515 }
59516 break;
59517 }
59518 pSegment->iNext++;
59519 }
59520 }
59521
59522 *piPage = p->iPrior = iRet;
59523 return (iRet==0xFFFFFFFF);
59524}
59525
59526/*
59527** This function merges two sorted lists into a single sorted list.
59528**
59529** aLeft[] and aRight[] are arrays of indices. The sort key is
59530** aContent[aLeft[]] and aContent[aRight[]]. Upon entry, the following
59531** is guaranteed for all J<K:
59532**
59533** aContent[aLeft[J]] < aContent[aLeft[K]]
59534** aContent[aRight[J]] < aContent[aRight[K]]
59535**
59536** This routine overwrites aRight[] with a new (probably longer) sequence
59537** of indices such that the aRight[] contains every index that appears in
59538** either aLeft[] or the old aRight[] and such that the second condition
59539** above is still met.
59540**
59541** The aContent[aLeft[X]] values will be unique for all X. And the
59542** aContent[aRight[X]] values will be unique too. But there might be
59543** one or more combinations of X and Y such that
59544**
59545** aLeft[X]!=aRight[Y] && aContent[aLeft[X]] == aContent[aRight[Y]]
59546**
59547** When that happens, omit the aLeft[X] and use the aRight[Y] index.
59548*/
59549static void walMerge(
59550 const u32 *aContent, /* Pages in wal - keys for the sort */
59551 ht_slot *aLeft, /* IN: Left hand input list */
59552 int nLeft, /* IN: Elements in array *paLeft */
59553 ht_slot **paRight, /* IN/OUT: Right hand input list */
59554 int *pnRight, /* IN/OUT: Elements in *paRight */
59555 ht_slot *aTmp /* Temporary buffer */
59556){
59557 int iLeft = 0; /* Current index in aLeft */
59558 int iRight = 0; /* Current index in aRight */
59559 int iOut = 0; /* Current index in output buffer */
59560 int nRight = *pnRight;
59561 ht_slot *aRight = *paRight;
59562
59563 assert( nLeft>0 && nRight>0 );
59564 while( iRight<nRight || iLeft<nLeft ){
59565 ht_slot logpage;
59566 Pgno dbpage;
59567
59568 if( (iLeft<nLeft)
59569 && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
59570 ){
59571 logpage = aLeft[iLeft++];
59572 }else{
59573 logpage = aRight[iRight++];
59574 }
59575 dbpage = aContent[logpage];
59576
59577 aTmp[iOut++] = logpage;
59578 if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
59579
59580 assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage );
59581 assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage );
59582 }
59583
59584 *paRight = aLeft;
59585 *pnRight = iOut;
59586 memcpy(aLeft, aTmp, sizeof(aTmp[0])*iOut);
59587}
59588
59589/*
59590** Sort the elements in list aList using aContent[] as the sort key.
59591** Remove elements with duplicate keys, preferring to keep the
59592** larger aList[] values.
59593**
59594** The aList[] entries are indices into aContent[]. The values in
59595** aList[] are to be sorted so that for all J<K:
59596**
59597** aContent[aList[J]] < aContent[aList[K]]
59598**
59599** For any X and Y such that
59600**
59601** aContent[aList[X]] == aContent[aList[Y]]
59602**
59603** Keep the larger of the two values aList[X] and aList[Y] and discard
59604** the smaller.
59605*/
59606static void walMergesort(
59607 const u32 *aContent, /* Pages in wal */
59608 ht_slot *aBuffer, /* Buffer of at least *pnList items to use */
59609 ht_slot *aList, /* IN/OUT: List to sort */
59610 int *pnList /* IN/OUT: Number of elements in aList[] */
59611){
59612 struct Sublist {
59613 int nList; /* Number of elements in aList */
59614 ht_slot *aList; /* Pointer to sub-list content */
59615 };
59616
59617 const int nList = *pnList; /* Size of input list */
59618 int nMerge = 0; /* Number of elements in list aMerge */
59619 ht_slot *aMerge = 0; /* List to be merged */
59620 int iList; /* Index into input list */
59621 u32 iSub = 0; /* Index into aSub array */
59622 struct Sublist aSub[13]; /* Array of sub-lists */
59623
59624 memset(aSub, 0, sizeof(aSub));
59625 assert( nList<=HASHTABLE_NPAGE && nList>0 );
59626 assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
59627
59628 for(iList=0; iList<nList; iList++){
59629 nMerge = 1;
59630 aMerge = &aList[iList];
59631 for(iSub=0; iList & (1<<iSub); iSub++){
59632 struct Sublist *p;
59633 assert( iSub<ArraySize(aSub) );
59634 p = &aSub[iSub];
59635 assert( p->aList && p->nList<=(1<<iSub) );
59636 assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
59637 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
59638 }
59639 aSub[iSub].aList = aMerge;
59640 aSub[iSub].nList = nMerge;
59641 }
59642
59643 for(iSub++; iSub<ArraySize(aSub); iSub++){
59644 if( nList & (1<<iSub) ){
59645 struct Sublist *p;
59646 assert( iSub<ArraySize(aSub) );
59647 p = &aSub[iSub];
59648 assert( p->nList<=(1<<iSub) );
59649 assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
59650 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
59651 }
59652 }
59653 assert( aMerge==aList );
59654 *pnList = nMerge;
59655
59656#ifdef SQLITE_DEBUG
59657 {
59658 int i;
59659 for(i=1; i<*pnList; i++){
59660 assert( aContent[aList[i]] > aContent[aList[i-1]] );
59661 }
59662 }
59663#endif
59664}
59665
59666/*
59667** Free an iterator allocated by walIteratorInit().
59668*/
59669static void walIteratorFree(WalIterator *p){
59670 sqlite3_free(p);
59671}
59672
59673/*
59674** Construct a WalInterator object that can be used to loop over all
59675** pages in the WAL following frame nBackfill in ascending order. Frames
59676** nBackfill or earlier may be included - excluding them is an optimization
59677** only. The caller must hold the checkpoint lock.
59678**
59679** On success, make *pp point to the newly allocated WalInterator object
59680** return SQLITE_OK. Otherwise, return an error code. If this routine
59681** returns an error, the value of *pp is undefined.
59682**
59683** The calling routine should invoke walIteratorFree() to destroy the
59684** WalIterator object when it has finished with it.
59685*/
59686static int walIteratorInit(Wal *pWal, u32 nBackfill, WalIterator **pp){
59687 WalIterator *p; /* Return value */
59688 int nSegment; /* Number of segments to merge */
59689 u32 iLast; /* Last frame in log */
59690 int nByte; /* Number of bytes to allocate */
59691 int i; /* Iterator variable */
59692 ht_slot *aTmp; /* Temp space used by merge-sort */
59693 int rc = SQLITE_OK; /* Return Code */
59694
59695 /* This routine only runs while holding the checkpoint lock. And
59696 ** it only runs if there is actually content in the log (mxFrame>0).
59697 */
59698 assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
59699 iLast = pWal->hdr.mxFrame;
59700
59701 /* Allocate space for the WalIterator object. */
59702 nSegment = walFramePage(iLast) + 1;
59703 nByte = sizeof(WalIterator)
59704 + (nSegment-1)*sizeof(struct WalSegment)
59705 + iLast*sizeof(ht_slot);
59706 p = (WalIterator *)sqlite3_malloc64(nByte);
59707 if( !p ){
59708 return SQLITE_NOMEM_BKPT;
59709 }
59710 memset(p, 0, nByte);
59711 p->nSegment = nSegment;
59712
59713 /* Allocate temporary space used by the merge-sort routine. This block
59714 ** of memory will be freed before this function returns.
59715 */
59716 aTmp = (ht_slot *)sqlite3_malloc64(
59717 sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
59718 );
59719 if( !aTmp ){
59720 rc = SQLITE_NOMEM_BKPT;
59721 }
59722
59723 for(i=walFramePage(nBackfill+1); rc==SQLITE_OK && i<nSegment; i++){
59724 WalHashLoc sLoc;
59725
59726 rc = walHashGet(pWal, i, &sLoc);
59727 if( rc==SQLITE_OK ){
59728 int j; /* Counter variable */
59729 int nEntry; /* Number of entries in this segment */
59730 ht_slot *aIndex; /* Sorted index for this segment */
59731
59732 sLoc.aPgno++;
59733 if( (i+1)==nSegment ){
59734 nEntry = (int)(iLast - sLoc.iZero);
59735 }else{
59736 nEntry = (int)((u32*)sLoc.aHash - (u32*)sLoc.aPgno);
59737 }
59738 aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[sLoc.iZero];
59739 sLoc.iZero++;
59740
59741 for(j=0; j<nEntry; j++){
59742 aIndex[j] = (ht_slot)j;
59743 }
59744 walMergesort((u32 *)sLoc.aPgno, aTmp, aIndex, &nEntry);
59745 p->aSegment[i].iZero = sLoc.iZero;
59746 p->aSegment[i].nEntry = nEntry;
59747 p->aSegment[i].aIndex = aIndex;
59748 p->aSegment[i].aPgno = (u32 *)sLoc.aPgno;
59749 }
59750 }
59751 sqlite3_free(aTmp);
59752
59753 if( rc!=SQLITE_OK ){
59754 walIteratorFree(p);
59755 p = 0;
59756 }
59757 *pp = p;
59758 return rc;
59759}
59760
59761/*
59762** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
59763** n. If the attempt fails and parameter xBusy is not NULL, then it is a
59764** busy-handler function. Invoke it and retry the lock until either the
59765** lock is successfully obtained or the busy-handler returns 0.
59766*/
59767static int walBusyLock(
59768 Wal *pWal, /* WAL connection */
59769 int (*xBusy)(void*), /* Function to call when busy */
59770 void *pBusyArg, /* Context argument for xBusyHandler */
59771 int lockIdx, /* Offset of first byte to lock */
59772 int n /* Number of bytes to lock */
59773){
59774 int rc;
59775 do {
59776 rc = walLockExclusive(pWal, lockIdx, n);
59777 }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) );
59778 return rc;
59779}
59780
59781/*
59782** The cache of the wal-index header must be valid to call this function.
59783** Return the page-size in bytes used by the database.
59784*/
59785static int walPagesize(Wal *pWal){
59786 return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
59787}
59788
59789/*
59790** The following is guaranteed when this function is called:
59791**
59792** a) the WRITER lock is held,
59793** b) the entire log file has been checkpointed, and
59794** c) any existing readers are reading exclusively from the database
59795** file - there are no readers that may attempt to read a frame from
59796** the log file.
59797**
59798** This function updates the shared-memory structures so that the next
59799** client to write to the database (which may be this one) does so by
59800** writing frames into the start of the log file.
59801**
59802** The value of parameter salt1 is used as the aSalt[1] value in the
59803** new wal-index header. It should be passed a pseudo-random value (i.e.
59804** one obtained from sqlite3_randomness()).
59805*/
59806static void walRestartHdr(Wal *pWal, u32 salt1){
59807 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
59808 int i; /* Loop counter */
59809 u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
59810 pWal->nCkpt++;
59811 pWal->hdr.mxFrame = 0;
59812 sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
59813 memcpy(&pWal->hdr.aSalt[1], &salt1, 4);
59814 walIndexWriteHdr(pWal);
59815 pInfo->nBackfill = 0;
59816 pInfo->nBackfillAttempted = 0;
59817 pInfo->aReadMark[1] = 0;
59818 for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
59819 assert( pInfo->aReadMark[0]==0 );
59820}
59821
59822/*
59823** Copy as much content as we can from the WAL back into the database file
59824** in response to an sqlite3_wal_checkpoint() request or the equivalent.
59825**
59826** The amount of information copies from WAL to database might be limited
59827** by active readers. This routine will never overwrite a database page
59828** that a concurrent reader might be using.
59829**
59830** All I/O barrier operations (a.k.a fsyncs) occur in this routine when
59831** SQLite is in WAL-mode in synchronous=NORMAL. That means that if
59832** checkpoints are always run by a background thread or background
59833** process, foreground threads will never block on a lengthy fsync call.
59834**
59835** Fsync is called on the WAL before writing content out of the WAL and
59836** into the database. This ensures that if the new content is persistent
59837** in the WAL and can be recovered following a power-loss or hard reset.
59838**
59839** Fsync is also called on the database file if (and only if) the entire
59840** WAL content is copied into the database file. This second fsync makes
59841** it safe to delete the WAL since the new content will persist in the
59842** database file.
59843**
59844** This routine uses and updates the nBackfill field of the wal-index header.
59845** This is the only routine that will increase the value of nBackfill.
59846** (A WAL reset or recovery will revert nBackfill to zero, but not increase
59847** its value.)
59848**
59849** The caller must be holding sufficient locks to ensure that no other
59850** checkpoint is running (in any other thread or process) at the same
59851** time.
59852*/
59853static int walCheckpoint(
59854 Wal *pWal, /* Wal connection */
59855 sqlite3 *db, /* Check for interrupts on this handle */
59856 int eMode, /* One of PASSIVE, FULL or RESTART */
59857 int (*xBusy)(void*), /* Function to call when busy */
59858 void *pBusyArg, /* Context argument for xBusyHandler */
59859 int sync_flags, /* Flags for OsSync() (or 0) */
59860 u8 *zBuf /* Temporary buffer to use */
59861){
59862 int rc = SQLITE_OK; /* Return code */
59863 int szPage; /* Database page-size */
59864 WalIterator *pIter = 0; /* Wal iterator context */
59865 u32 iDbpage = 0; /* Next database page to write */
59866 u32 iFrame = 0; /* Wal frame containing data for iDbpage */
59867 u32 mxSafeFrame; /* Max frame that can be backfilled */
59868 u32 mxPage; /* Max database page to write */
59869 int i; /* Loop counter */
59870 volatile WalCkptInfo *pInfo; /* The checkpoint status information */
59871
59872 szPage = walPagesize(pWal);
59873 testcase( szPage<=32768 );
59874 testcase( szPage>=65536 );
59875 pInfo = walCkptInfo(pWal);
59876 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
59877
59878 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
59879 ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
59880 assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
59881
59882 /* Compute in mxSafeFrame the index of the last frame of the WAL that is
59883 ** safe to write into the database. Frames beyond mxSafeFrame might
59884 ** overwrite database pages that are in use by active readers and thus
59885 ** cannot be backfilled from the WAL.
59886 */
59887 mxSafeFrame = pWal->hdr.mxFrame;
59888 mxPage = pWal->hdr.nPage;
59889 for(i=1; i<WAL_NREADER; i++){
59890 /* Thread-sanitizer reports that the following is an unsafe read,
59891 ** as some other thread may be in the process of updating the value
59892 ** of the aReadMark[] slot. The assumption here is that if that is
59893 ** happening, the other client may only be increasing the value,
59894 ** not decreasing it. So assuming either that either the "old" or
59895 ** "new" version of the value is read, and not some arbitrary value
59896 ** that would never be written by a real client, things are still
59897 ** safe. */
59898 u32 y = pInfo->aReadMark[i];
59899 if( mxSafeFrame>y ){
59900 assert( y<=pWal->hdr.mxFrame );
59901 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1);
59902 if( rc==SQLITE_OK ){
59903 pInfo->aReadMark[i] = (i==1 ? mxSafeFrame : READMARK_NOT_USED);
59904 walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
59905 }else if( rc==SQLITE_BUSY ){
59906 mxSafeFrame = y;
59907 xBusy = 0;
59908 }else{
59909 goto walcheckpoint_out;
59910 }
59911 }
59912 }
59913
59914 /* Allocate the iterator */
59915 if( pInfo->nBackfill<mxSafeFrame ){
59916 rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter);
59917 assert( rc==SQLITE_OK || pIter==0 );
59918 }
59919
59920 if( pIter
59921 && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0),1))==SQLITE_OK
59922 ){
59923 u32 nBackfill = pInfo->nBackfill;
59924
59925 pInfo->nBackfillAttempted = mxSafeFrame;
59926
59927 /* Sync the WAL to disk */
59928 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
59929
59930 /* If the database may grow as a result of this checkpoint, hint
59931 ** about the eventual size of the db file to the VFS layer.
59932 */
59933 if( rc==SQLITE_OK ){
59934 i64 nReq = ((i64)mxPage * szPage);
59935 i64 nSize; /* Current size of database file */
59936 rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
59937 if( rc==SQLITE_OK && nSize<nReq ){
59938 sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
59939 }
59940 }
59941
59942
59943 /* Iterate through the contents of the WAL, copying data to the db file */
59944 while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
59945 i64 iOffset;
59946 assert( walFramePgno(pWal, iFrame)==iDbpage );
59947 if( db->u1.isInterrupted ){
59948 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
59949 break;
59950 }
59951 if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ){
59952 continue;
59953 }
59954 iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE;
59955 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
59956 rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
59957 if( rc!=SQLITE_OK ) break;
59958 iOffset = (iDbpage-1)*(i64)szPage;
59959 testcase( IS_BIG_INT(iOffset) );
59960 rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
59961 if( rc!=SQLITE_OK ) break;
59962 }
59963
59964 /* If work was actually accomplished... */
59965 if( rc==SQLITE_OK ){
59966 if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
59967 i64 szDb = pWal->hdr.nPage*(i64)szPage;
59968 testcase( IS_BIG_INT(szDb) );
59969 rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
59970 if( rc==SQLITE_OK ){
59971 rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags));
59972 }
59973 }
59974 if( rc==SQLITE_OK ){
59975 pInfo->nBackfill = mxSafeFrame;
59976 }
59977 }
59978
59979 /* Release the reader lock held while backfilling */
59980 walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1);
59981 }
59982
59983 if( rc==SQLITE_BUSY ){
59984 /* Reset the return code so as not to report a checkpoint failure
59985 ** just because there are active readers. */
59986 rc = SQLITE_OK;
59987 }
59988 }
59989
59990 /* If this is an SQLITE_CHECKPOINT_RESTART or TRUNCATE operation, and the
59991 ** entire wal file has been copied into the database file, then block
59992 ** until all readers have finished using the wal file. This ensures that
59993 ** the next process to write to the database restarts the wal file.
59994 */
59995 if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){
59996 assert( pWal->writeLock );
59997 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
59998 rc = SQLITE_BUSY;
59999 }else if( eMode>=SQLITE_CHECKPOINT_RESTART ){
60000 u32 salt1;
60001 sqlite3_randomness(4, &salt1);
60002 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
60003 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
60004 if( rc==SQLITE_OK ){
60005 if( eMode==SQLITE_CHECKPOINT_TRUNCATE ){
60006 /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
60007 ** SQLITE_CHECKPOINT_RESTART with the addition that it also
60008 ** truncates the log file to zero bytes just prior to a
60009 ** successful return.
60010 **
60011 ** In theory, it might be safe to do this without updating the
60012 ** wal-index header in shared memory, as all subsequent reader or
60013 ** writer clients should see that the entire log file has been
60014 ** checkpointed and behave accordingly. This seems unsafe though,
60015 ** as it would leave the system in a state where the contents of
60016 ** the wal-index header do not match the contents of the
60017 ** file-system. To avoid this, update the wal-index header to
60018 ** indicate that the log file contains zero valid frames. */
60019 walRestartHdr(pWal, salt1);
60020 rc = sqlite3OsTruncate(pWal->pWalFd, 0);
60021 }
60022 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
60023 }
60024 }
60025 }
60026
60027 walcheckpoint_out:
60028 walIteratorFree(pIter);
60029 return rc;
60030}
60031
60032/*
60033** If the WAL file is currently larger than nMax bytes in size, truncate
60034** it to exactly nMax bytes. If an error occurs while doing so, ignore it.
60035*/
60036static void walLimitSize(Wal *pWal, i64 nMax){
60037 i64 sz;
60038 int rx;
60039 sqlite3BeginBenignMalloc();
60040 rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
60041 if( rx==SQLITE_OK && (sz > nMax ) ){
60042 rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
60043 }
60044 sqlite3EndBenignMalloc();
60045 if( rx ){
60046 sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
60047 }
60048}
60049
60050/*
60051** Close a connection to a log file.
60052*/
60053SQLITE_PRIVATE int sqlite3WalClose(
60054 Wal *pWal, /* Wal to close */
60055 sqlite3 *db, /* For interrupt flag */
60056 int sync_flags, /* Flags to pass to OsSync() (or 0) */
60057 int nBuf,
60058 u8 *zBuf /* Buffer of at least nBuf bytes */
60059){
60060 int rc = SQLITE_OK;
60061 if( pWal ){
60062 int isDelete = 0; /* True to unlink wal and wal-index files */
60063
60064 /* If an EXCLUSIVE lock can be obtained on the database file (using the
60065 ** ordinary, rollback-mode locking methods, this guarantees that the
60066 ** connection associated with this log file is the only connection to
60067 ** the database. In this case checkpoint the database and unlink both
60068 ** the wal and wal-index files.
60069 **
60070 ** The EXCLUSIVE lock is not released before returning.
60071 */
60072 if( zBuf!=0
60073 && SQLITE_OK==(rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE))
60074 ){
60075 if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
60076 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
60077 }
60078 rc = sqlite3WalCheckpoint(pWal, db,
60079 SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
60080 );
60081 if( rc==SQLITE_OK ){
60082 int bPersist = -1;
60083 sqlite3OsFileControlHint(
60084 pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
60085 );
60086 if( bPersist!=1 ){
60087 /* Try to delete the WAL file if the checkpoint completed and
60088 ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal
60089 ** mode (!bPersist) */
60090 isDelete = 1;
60091 }else if( pWal->mxWalSize>=0 ){
60092 /* Try to truncate the WAL file to zero bytes if the checkpoint
60093 ** completed and fsynced (rc==SQLITE_OK) and we are in persistent
60094 ** WAL mode (bPersist) and if the PRAGMA journal_size_limit is a
60095 ** non-negative value (pWal->mxWalSize>=0). Note that we truncate
60096 ** to zero bytes as truncating to the journal_size_limit might
60097 ** leave a corrupt WAL file on disk. */
60098 walLimitSize(pWal, 0);
60099 }
60100 }
60101 }
60102
60103 walIndexClose(pWal, isDelete);
60104 sqlite3OsClose(pWal->pWalFd);
60105 if( isDelete ){
60106 sqlite3BeginBenignMalloc();
60107 sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
60108 sqlite3EndBenignMalloc();
60109 }
60110 WALTRACE(("WAL%p: closed\n", pWal));
60111 sqlite3_free((void *)pWal->apWiData);
60112 sqlite3_free(pWal);
60113 }
60114 return rc;
60115}
60116
60117/*
60118** Try to read the wal-index header. Return 0 on success and 1 if
60119** there is a problem.
60120**
60121** The wal-index is in shared memory. Another thread or process might
60122** be writing the header at the same time this procedure is trying to
60123** read it, which might result in inconsistency. A dirty read is detected
60124** by verifying that both copies of the header are the same and also by
60125** a checksum on the header.
60126**
60127** If and only if the read is consistent and the header is different from
60128** pWal->hdr, then pWal->hdr is updated to the content of the new header
60129** and *pChanged is set to 1.
60130**
60131** If the checksum cannot be verified return non-zero. If the header
60132** is read successfully and the checksum verified, return zero.
60133*/
60134static int walIndexTryHdr(Wal *pWal, int *pChanged){
60135 u32 aCksum[2]; /* Checksum on the header content */
60136 WalIndexHdr h1, h2; /* Two copies of the header content */
60137 WalIndexHdr volatile *aHdr; /* Header in shared memory */
60138
60139 /* The first page of the wal-index must be mapped at this point. */
60140 assert( pWal->nWiData>0 && pWal->apWiData[0] );
60141
60142 /* Read the header. This might happen concurrently with a write to the
60143 ** same area of shared memory on a different CPU in a SMP,
60144 ** meaning it is possible that an inconsistent snapshot is read
60145 ** from the file. If this happens, return non-zero.
60146 **
60147 ** There are two copies of the header at the beginning of the wal-index.
60148 ** When reading, read [0] first then [1]. Writes are in the reverse order.
60149 ** Memory barriers are used to prevent the compiler or the hardware from
60150 ** reordering the reads and writes.
60151 */
60152 aHdr = walIndexHdr(pWal);
60153 memcpy(&h1, (void *)&aHdr[0], sizeof(h1));
60154 walShmBarrier(pWal);
60155 memcpy(&h2, (void *)&aHdr[1], sizeof(h2));
60156
60157 if( memcmp(&h1, &h2, sizeof(h1))!=0 ){
60158 return 1; /* Dirty read */
60159 }
60160 if( h1.isInit==0 ){
60161 return 1; /* Malformed header - probably all zeros */
60162 }
60163 walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
60164 if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){
60165 return 1; /* Checksum does not match */
60166 }
60167
60168 if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
60169 *pChanged = 1;
60170 memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
60171 pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
60172 testcase( pWal->szPage<=32768 );
60173 testcase( pWal->szPage>=65536 );
60174 }
60175
60176 /* The header was successfully read. Return zero. */
60177 return 0;
60178}
60179
60180/*
60181** This is the value that walTryBeginRead returns when it needs to
60182** be retried.
60183*/
60184#define WAL_RETRY (-1)
60185
60186/*
60187** Read the wal-index header from the wal-index and into pWal->hdr.
60188** If the wal-header appears to be corrupt, try to reconstruct the
60189** wal-index from the WAL before returning.
60190**
60191** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
60192** changed by this operation. If pWal->hdr is unchanged, set *pChanged
60193** to 0.
60194**
60195** If the wal-index header is successfully read, return SQLITE_OK.
60196** Otherwise an SQLite error code.
60197*/
60198static int walIndexReadHdr(Wal *pWal, int *pChanged){
60199 int rc; /* Return code */
60200 int badHdr; /* True if a header read failed */
60201 volatile u32 *page0; /* Chunk of wal-index containing header */
60202
60203 /* Ensure that page 0 of the wal-index (the page that contains the
60204 ** wal-index header) is mapped. Return early if an error occurs here.
60205 */
60206 assert( pChanged );
60207 rc = walIndexPage(pWal, 0, &page0);
60208 if( rc!=SQLITE_OK ){
60209 assert( rc!=SQLITE_READONLY ); /* READONLY changed to OK in walIndexPage */
60210 if( rc==SQLITE_READONLY_CANTINIT ){
60211 /* The SQLITE_READONLY_CANTINIT return means that the shared-memory
60212 ** was openable but is not writable, and this thread is unable to
60213 ** confirm that another write-capable connection has the shared-memory
60214 ** open, and hence the content of the shared-memory is unreliable,
60215 ** since the shared-memory might be inconsistent with the WAL file
60216 ** and there is no writer on hand to fix it. */
60217 assert( page0==0 );
60218 assert( pWal->writeLock==0 );
60219 assert( pWal->readOnly & WAL_SHM_RDONLY );
60220 pWal->bShmUnreliable = 1;
60221 pWal->exclusiveMode = WAL_HEAPMEMORY_MODE;
60222 *pChanged = 1;
60223 }else{
60224 return rc; /* Any other non-OK return is just an error */
60225 }
60226 }else{
60227 /* page0 can be NULL if the SHM is zero bytes in size and pWal->writeLock
60228 ** is zero, which prevents the SHM from growing */
60229 testcase( page0!=0 );
60230 }
60231 assert( page0!=0 || pWal->writeLock==0 );
60232
60233 /* If the first page of the wal-index has been mapped, try to read the
60234 ** wal-index header immediately, without holding any lock. This usually
60235 ** works, but may fail if the wal-index header is corrupt or currently
60236 ** being modified by another thread or process.
60237 */
60238 badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
60239
60240 /* If the first attempt failed, it might have been due to a race
60241 ** with a writer. So get a WRITE lock and try again.
60242 */
60243 assert( badHdr==0 || pWal->writeLock==0 );
60244 if( badHdr ){
60245 if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){
60246 if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
60247 walUnlockShared(pWal, WAL_WRITE_LOCK);
60248 rc = SQLITE_READONLY_RECOVERY;
60249 }
60250 }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){
60251 pWal->writeLock = 1;
60252 if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
60253 badHdr = walIndexTryHdr(pWal, pChanged);
60254 if( badHdr ){
60255 /* If the wal-index header is still malformed even while holding
60256 ** a WRITE lock, it can only mean that the header is corrupted and
60257 ** needs to be reconstructed. So run recovery to do exactly that.
60258 */
60259 rc = walIndexRecover(pWal);
60260 *pChanged = 1;
60261 }
60262 }
60263 pWal->writeLock = 0;
60264 walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
60265 }
60266 }
60267
60268 /* If the header is read successfully, check the version number to make
60269 ** sure the wal-index was not constructed with some future format that
60270 ** this version of SQLite cannot understand.
60271 */
60272 if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
60273 rc = SQLITE_CANTOPEN_BKPT;
60274 }
60275 if( pWal->bShmUnreliable ){
60276 if( rc!=SQLITE_OK ){
60277 walIndexClose(pWal, 0);
60278 pWal->bShmUnreliable = 0;
60279 assert( pWal->nWiData>0 && pWal->apWiData[0]==0 );
60280 /* walIndexRecover() might have returned SHORT_READ if a concurrent
60281 ** writer truncated the WAL out from under it. If that happens, it
60282 ** indicates that a writer has fixed the SHM file for us, so retry */
60283 if( rc==SQLITE_IOERR_SHORT_READ ) rc = WAL_RETRY;
60284 }
60285 pWal->exclusiveMode = WAL_NORMAL_MODE;
60286 }
60287
60288 return rc;
60289}
60290
60291/*
60292** Open a transaction in a connection where the shared-memory is read-only
60293** and where we cannot verify that there is a separate write-capable connection
60294** on hand to keep the shared-memory up-to-date with the WAL file.
60295**
60296** This can happen, for example, when the shared-memory is implemented by
60297** memory-mapping a *-shm file, where a prior writer has shut down and
60298** left the *-shm file on disk, and now the present connection is trying
60299** to use that database but lacks write permission on the *-shm file.
60300** Other scenarios are also possible, depending on the VFS implementation.
60301**
60302** Precondition:
60303**
60304** The *-wal file has been read and an appropriate wal-index has been
60305** constructed in pWal->apWiData[] using heap memory instead of shared
60306** memory.
60307**
60308** If this function returns SQLITE_OK, then the read transaction has
60309** been successfully opened. In this case output variable (*pChanged)
60310** is set to true before returning if the caller should discard the
60311** contents of the page cache before proceeding. Or, if it returns
60312** WAL_RETRY, then the heap memory wal-index has been discarded and
60313** the caller should retry opening the read transaction from the
60314** beginning (including attempting to map the *-shm file).
60315**
60316** If an error occurs, an SQLite error code is returned.
60317*/
60318static int walBeginShmUnreliable(Wal *pWal, int *pChanged){
60319 i64 szWal; /* Size of wal file on disk in bytes */
60320 i64 iOffset; /* Current offset when reading wal file */
60321 u8 aBuf[WAL_HDRSIZE]; /* Buffer to load WAL header into */
60322 u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
60323 int szFrame; /* Number of bytes in buffer aFrame[] */
60324 u8 *aData; /* Pointer to data part of aFrame buffer */
60325 volatile void *pDummy; /* Dummy argument for xShmMap */
60326 int rc; /* Return code */
60327 u32 aSaveCksum[2]; /* Saved copy of pWal->hdr.aFrameCksum */
60328
60329 assert( pWal->bShmUnreliable );
60330 assert( pWal->readOnly & WAL_SHM_RDONLY );
60331 assert( pWal->nWiData>0 && pWal->apWiData[0] );
60332
60333 /* Take WAL_READ_LOCK(0). This has the effect of preventing any
60334 ** writers from running a checkpoint, but does not stop them
60335 ** from running recovery. */
60336 rc = walLockShared(pWal, WAL_READ_LOCK(0));
60337 if( rc!=SQLITE_OK ){
60338 if( rc==SQLITE_BUSY ) rc = WAL_RETRY;
60339 goto begin_unreliable_shm_out;
60340 }
60341 pWal->readLock = 0;
60342
60343 /* Check to see if a separate writer has attached to the shared-memory area,
60344 ** thus making the shared-memory "reliable" again. Do this by invoking
60345 ** the xShmMap() routine of the VFS and looking to see if the return
60346 ** is SQLITE_READONLY instead of SQLITE_READONLY_CANTINIT.
60347 **
60348 ** If the shared-memory is now "reliable" return WAL_RETRY, which will
60349 ** cause the heap-memory WAL-index to be discarded and the actual
60350 ** shared memory to be used in its place.
60351 **
60352 ** This step is important because, even though this connection is holding
60353 ** the WAL_READ_LOCK(0) which prevents a checkpoint, a writer might
60354 ** have already checkpointed the WAL file and, while the current
60355 ** is active, wrap the WAL and start overwriting frames that this
60356 ** process wants to use.
60357 **
60358 ** Once sqlite3OsShmMap() has been called for an sqlite3_file and has
60359 ** returned any SQLITE_READONLY value, it must return only SQLITE_READONLY
60360 ** or SQLITE_READONLY_CANTINIT or some error for all subsequent invocations,
60361 ** even if some external agent does a "chmod" to make the shared-memory
60362 ** writable by us, until sqlite3OsShmUnmap() has been called.
60363 ** This is a requirement on the VFS implementation.
60364 */
60365 rc = sqlite3OsShmMap(pWal->pDbFd, 0, WALINDEX_PGSZ, 0, &pDummy);
60366 assert( rc!=SQLITE_OK ); /* SQLITE_OK not possible for read-only connection */
60367 if( rc!=SQLITE_READONLY_CANTINIT ){
60368 rc = (rc==SQLITE_READONLY ? WAL_RETRY : rc);
60369 goto begin_unreliable_shm_out;
60370 }
60371
60372 /* We reach this point only if the real shared-memory is still unreliable.
60373 ** Assume the in-memory WAL-index substitute is correct and load it
60374 ** into pWal->hdr.
60375 */
60376 memcpy(&pWal->hdr, (void*)walIndexHdr(pWal), sizeof(WalIndexHdr));
60377
60378 /* Make sure some writer hasn't come in and changed the WAL file out
60379 ** from under us, then disconnected, while we were not looking.
60380 */
60381 rc = sqlite3OsFileSize(pWal->pWalFd, &szWal);
60382 if( rc!=SQLITE_OK ){
60383 goto begin_unreliable_shm_out;
60384 }
60385 if( szWal<WAL_HDRSIZE ){
60386 /* If the wal file is too small to contain a wal-header and the
60387 ** wal-index header has mxFrame==0, then it must be safe to proceed
60388 ** reading the database file only. However, the page cache cannot
60389 ** be trusted, as a read/write connection may have connected, written
60390 ** the db, run a checkpoint, truncated the wal file and disconnected
60391 ** since this client's last read transaction. */
60392 *pChanged = 1;
60393 rc = (pWal->hdr.mxFrame==0 ? SQLITE_OK : WAL_RETRY);
60394 goto begin_unreliable_shm_out;
60395 }
60396
60397 /* Check the salt keys at the start of the wal file still match. */
60398 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
60399 if( rc!=SQLITE_OK ){
60400 goto begin_unreliable_shm_out;
60401 }
60402 if( memcmp(&pWal->hdr.aSalt, &aBuf[16], 8) ){
60403 /* Some writer has wrapped the WAL file while we were not looking.
60404 ** Return WAL_RETRY which will cause the in-memory WAL-index to be
60405 ** rebuilt. */
60406 rc = WAL_RETRY;
60407 goto begin_unreliable_shm_out;
60408 }
60409
60410 /* Allocate a buffer to read frames into */
60411 szFrame = pWal->hdr.szPage + WAL_FRAME_HDRSIZE;
60412 aFrame = (u8 *)sqlite3_malloc64(szFrame);
60413 if( aFrame==0 ){
60414 rc = SQLITE_NOMEM_BKPT;
60415 goto begin_unreliable_shm_out;
60416 }
60417 aData = &aFrame[WAL_FRAME_HDRSIZE];
60418
60419 /* Check to see if a complete transaction has been appended to the
60420 ** wal file since the heap-memory wal-index was created. If so, the
60421 ** heap-memory wal-index is discarded and WAL_RETRY returned to
60422 ** the caller. */
60423 aSaveCksum[0] = pWal->hdr.aFrameCksum[0];
60424 aSaveCksum[1] = pWal->hdr.aFrameCksum[1];
60425 for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->hdr.szPage);
60426 iOffset+szFrame<=szWal;
60427 iOffset+=szFrame
60428 ){
60429 u32 pgno; /* Database page number for frame */
60430 u32 nTruncate; /* dbsize field from frame header */
60431
60432 /* Read and decode the next log frame. */
60433 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
60434 if( rc!=SQLITE_OK ) break;
60435 if( !walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame) ) break;
60436
60437 /* If nTruncate is non-zero, then a complete transaction has been
60438 ** appended to this wal file. Set rc to WAL_RETRY and break out of
60439 ** the loop. */
60440 if( nTruncate ){
60441 rc = WAL_RETRY;
60442 break;
60443 }
60444 }
60445 pWal->hdr.aFrameCksum[0] = aSaveCksum[0];
60446 pWal->hdr.aFrameCksum[1] = aSaveCksum[1];
60447
60448 begin_unreliable_shm_out:
60449 sqlite3_free(aFrame);
60450 if( rc!=SQLITE_OK ){
60451 int i;
60452 for(i=0; i<pWal->nWiData; i++){
60453 sqlite3_free((void*)pWal->apWiData[i]);
60454 pWal->apWiData[i] = 0;
60455 }
60456 pWal->bShmUnreliable = 0;
60457 sqlite3WalEndReadTransaction(pWal);
60458 *pChanged = 1;
60459 }
60460 return rc;
60461}
60462
60463/*
60464** Attempt to start a read transaction. This might fail due to a race or
60465** other transient condition. When that happens, it returns WAL_RETRY to
60466** indicate to the caller that it is safe to retry immediately.
60467**
60468** On success return SQLITE_OK. On a permanent failure (such an
60469** I/O error or an SQLITE_BUSY because another process is running
60470** recovery) return a positive error code.
60471**
60472** The useWal parameter is true to force the use of the WAL and disable
60473** the case where the WAL is bypassed because it has been completely
60474** checkpointed. If useWal==0 then this routine calls walIndexReadHdr()
60475** to make a copy of the wal-index header into pWal->hdr. If the
60476** wal-index header has changed, *pChanged is set to 1 (as an indication
60477** to the caller that the local page cache is obsolete and needs to be
60478** flushed.) When useWal==1, the wal-index header is assumed to already
60479** be loaded and the pChanged parameter is unused.
60480**
60481** The caller must set the cnt parameter to the number of prior calls to
60482** this routine during the current read attempt that returned WAL_RETRY.
60483** This routine will start taking more aggressive measures to clear the
60484** race conditions after multiple WAL_RETRY returns, and after an excessive
60485** number of errors will ultimately return SQLITE_PROTOCOL. The
60486** SQLITE_PROTOCOL return indicates that some other process has gone rogue
60487** and is not honoring the locking protocol. There is a vanishingly small
60488** chance that SQLITE_PROTOCOL could be returned because of a run of really
60489** bad luck when there is lots of contention for the wal-index, but that
60490** possibility is so small that it can be safely neglected, we believe.
60491**
60492** On success, this routine obtains a read lock on
60493** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
60494** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
60495** that means the Wal does not hold any read lock. The reader must not
60496** access any database page that is modified by a WAL frame up to and
60497** including frame number aReadMark[pWal->readLock]. The reader will
60498** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
60499** Or if pWal->readLock==0, then the reader will ignore the WAL
60500** completely and get all content directly from the database file.
60501** If the useWal parameter is 1 then the WAL will never be ignored and
60502** this routine will always set pWal->readLock>0 on success.
60503** When the read transaction is completed, the caller must release the
60504** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
60505**
60506** This routine uses the nBackfill and aReadMark[] fields of the header
60507** to select a particular WAL_READ_LOCK() that strives to let the
60508** checkpoint process do as much work as possible. This routine might
60509** update values of the aReadMark[] array in the header, but if it does
60510** so it takes care to hold an exclusive lock on the corresponding
60511** WAL_READ_LOCK() while changing values.
60512*/
60513static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
60514 volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
60515 u32 mxReadMark; /* Largest aReadMark[] value */
60516 int mxI; /* Index of largest aReadMark[] value */
60517 int i; /* Loop counter */
60518 int rc = SQLITE_OK; /* Return code */
60519 u32 mxFrame; /* Wal frame to lock to */
60520
60521 assert( pWal->readLock<0 ); /* Not currently locked */
60522
60523 /* useWal may only be set for read/write connections */
60524 assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 );
60525
60526 /* Take steps to avoid spinning forever if there is a protocol error.
60527 **
60528 ** Circumstances that cause a RETRY should only last for the briefest
60529 ** instances of time. No I/O or other system calls are done while the
60530 ** locks are held, so the locks should not be held for very long. But
60531 ** if we are unlucky, another process that is holding a lock might get
60532 ** paged out or take a page-fault that is time-consuming to resolve,
60533 ** during the few nanoseconds that it is holding the lock. In that case,
60534 ** it might take longer than normal for the lock to free.
60535 **
60536 ** After 5 RETRYs, we begin calling sqlite3OsSleep(). The first few
60537 ** calls to sqlite3OsSleep() have a delay of 1 microsecond. Really this
60538 ** is more of a scheduler yield than an actual delay. But on the 10th
60539 ** an subsequent retries, the delays start becoming longer and longer,
60540 ** so that on the 100th (and last) RETRY we delay for 323 milliseconds.
60541 ** The total delay time before giving up is less than 10 seconds.
60542 */
60543 if( cnt>5 ){
60544 int nDelay = 1; /* Pause time in microseconds */
60545 if( cnt>100 ){
60546 VVA_ONLY( pWal->lockError = 1; )
60547 return SQLITE_PROTOCOL;
60548 }
60549 if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
60550 sqlite3OsSleep(pWal->pVfs, nDelay);
60551 }
60552
60553 if( !useWal ){
60554 assert( rc==SQLITE_OK );
60555 if( pWal->bShmUnreliable==0 ){
60556 rc = walIndexReadHdr(pWal, pChanged);
60557 }
60558 if( rc==SQLITE_BUSY ){
60559 /* If there is not a recovery running in another thread or process
60560 ** then convert BUSY errors to WAL_RETRY. If recovery is known to
60561 ** be running, convert BUSY to BUSY_RECOVERY. There is a race here
60562 ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
60563 ** would be technically correct. But the race is benign since with
60564 ** WAL_RETRY this routine will be called again and will probably be
60565 ** right on the second iteration.
60566 */
60567 if( pWal->apWiData[0]==0 ){
60568 /* This branch is taken when the xShmMap() method returns SQLITE_BUSY.
60569 ** We assume this is a transient condition, so return WAL_RETRY. The
60570 ** xShmMap() implementation used by the default unix and win32 VFS
60571 ** modules may return SQLITE_BUSY due to a race condition in the
60572 ** code that determines whether or not the shared-memory region
60573 ** must be zeroed before the requested page is returned.
60574 */
60575 rc = WAL_RETRY;
60576 }else if( SQLITE_OK==(rc = walLockShared(pWal, WAL_RECOVER_LOCK)) ){
60577 walUnlockShared(pWal, WAL_RECOVER_LOCK);
60578 rc = WAL_RETRY;
60579 }else if( rc==SQLITE_BUSY ){
60580 rc = SQLITE_BUSY_RECOVERY;
60581 }
60582 }
60583 if( rc!=SQLITE_OK ){
60584 return rc;
60585 }
60586 else if( pWal->bShmUnreliable ){
60587 return walBeginShmUnreliable(pWal, pChanged);
60588 }
60589 }
60590
60591 assert( pWal->nWiData>0 );
60592 assert( pWal->apWiData[0]!=0 );
60593 pInfo = walCkptInfo(pWal);
60594 if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame
60595#ifdef SQLITE_ENABLE_SNAPSHOT
60596 && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0)
60597#endif
60598 ){
60599 /* The WAL has been completely backfilled (or it is empty).
60600 ** and can be safely ignored.
60601 */
60602 rc = walLockShared(pWal, WAL_READ_LOCK(0));
60603 walShmBarrier(pWal);
60604 if( rc==SQLITE_OK ){
60605 if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
60606 /* It is not safe to allow the reader to continue here if frames
60607 ** may have been appended to the log before READ_LOCK(0) was obtained.
60608 ** When holding READ_LOCK(0), the reader ignores the entire log file,
60609 ** which implies that the database file contains a trustworthy
60610 ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from
60611 ** happening, this is usually correct.
60612 **
60613 ** However, if frames have been appended to the log (or if the log
60614 ** is wrapped and written for that matter) before the READ_LOCK(0)
60615 ** is obtained, that is not necessarily true. A checkpointer may
60616 ** have started to backfill the appended frames but crashed before
60617 ** it finished. Leaving a corrupt image in the database file.
60618 */
60619 walUnlockShared(pWal, WAL_READ_LOCK(0));
60620 return WAL_RETRY;
60621 }
60622 pWal->readLock = 0;
60623 return SQLITE_OK;
60624 }else if( rc!=SQLITE_BUSY ){
60625 return rc;
60626 }
60627 }
60628
60629 /* If we get this far, it means that the reader will want to use
60630 ** the WAL to get at content from recent commits. The job now is
60631 ** to select one of the aReadMark[] entries that is closest to
60632 ** but not exceeding pWal->hdr.mxFrame and lock that entry.
60633 */
60634 mxReadMark = 0;
60635 mxI = 0;
60636 mxFrame = pWal->hdr.mxFrame;
60637#ifdef SQLITE_ENABLE_SNAPSHOT
60638 if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
60639 mxFrame = pWal->pSnapshot->mxFrame;
60640 }
60641#endif
60642 for(i=1; i<WAL_NREADER; i++){
60643 u32 thisMark = AtomicLoad(pInfo->aReadMark+i);
60644 if( mxReadMark<=thisMark && thisMark<=mxFrame ){
60645 assert( thisMark!=READMARK_NOT_USED );
60646 mxReadMark = thisMark;
60647 mxI = i;
60648 }
60649 }
60650 if( (pWal->readOnly & WAL_SHM_RDONLY)==0
60651 && (mxReadMark<mxFrame || mxI==0)
60652 ){
60653 for(i=1; i<WAL_NREADER; i++){
60654 rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
60655 if( rc==SQLITE_OK ){
60656 mxReadMark = AtomicStore(pInfo->aReadMark+i,mxFrame);
60657 mxI = i;
60658 walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
60659 break;
60660 }else if( rc!=SQLITE_BUSY ){
60661 return rc;
60662 }
60663 }
60664 }
60665 if( mxI==0 ){
60666 assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
60667 return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTINIT;
60668 }
60669
60670 rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
60671 if( rc ){
60672 return rc==SQLITE_BUSY ? WAL_RETRY : rc;
60673 }
60674 /* Now that the read-lock has been obtained, check that neither the
60675 ** value in the aReadMark[] array or the contents of the wal-index
60676 ** header have changed.
60677 **
60678 ** It is necessary to check that the wal-index header did not change
60679 ** between the time it was read and when the shared-lock was obtained
60680 ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
60681 ** that the log file may have been wrapped by a writer, or that frames
60682 ** that occur later in the log than pWal->hdr.mxFrame may have been
60683 ** copied into the database by a checkpointer. If either of these things
60684 ** happened, then reading the database with the current value of
60685 ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
60686 ** instead.
60687 **
60688 ** Before checking that the live wal-index header has not changed
60689 ** since it was read, set Wal.minFrame to the first frame in the wal
60690 ** file that has not yet been checkpointed. This client will not need
60691 ** to read any frames earlier than minFrame from the wal file - they
60692 ** can be safely read directly from the database file.
60693 **
60694 ** Because a ShmBarrier() call is made between taking the copy of
60695 ** nBackfill and checking that the wal-header in shared-memory still
60696 ** matches the one cached in pWal->hdr, it is guaranteed that the
60697 ** checkpointer that set nBackfill was not working with a wal-index
60698 ** header newer than that cached in pWal->hdr. If it were, that could
60699 ** cause a problem. The checkpointer could omit to checkpoint
60700 ** a version of page X that lies before pWal->minFrame (call that version
60701 ** A) on the basis that there is a newer version (version B) of the same
60702 ** page later in the wal file. But if version B happens to like past
60703 ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
60704 ** that it can read version A from the database file. However, since
60705 ** we can guarantee that the checkpointer that set nBackfill could not
60706 ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
60707 */
60708 pWal->minFrame = AtomicLoad(&pInfo->nBackfill)+1;
60709 walShmBarrier(pWal);
60710 if( AtomicLoad(pInfo->aReadMark+mxI)!=mxReadMark
60711 || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
60712 ){
60713 walUnlockShared(pWal, WAL_READ_LOCK(mxI));
60714 return WAL_RETRY;
60715 }else{
60716 assert( mxReadMark<=pWal->hdr.mxFrame );
60717 pWal->readLock = (i16)mxI;
60718 }
60719 return rc;
60720}
60721
60722#ifdef SQLITE_ENABLE_SNAPSHOT
60723/*
60724** Attempt to reduce the value of the WalCkptInfo.nBackfillAttempted
60725** variable so that older snapshots can be accessed. To do this, loop
60726** through all wal frames from nBackfillAttempted to (nBackfill+1),
60727** comparing their content to the corresponding page with the database
60728** file, if any. Set nBackfillAttempted to the frame number of the
60729** first frame for which the wal file content matches the db file.
60730**
60731** This is only really safe if the file-system is such that any page
60732** writes made by earlier checkpointers were atomic operations, which
60733** is not always true. It is also possible that nBackfillAttempted
60734** may be left set to a value larger than expected, if a wal frame
60735** contains content that duplicate of an earlier version of the same
60736** page.
60737**
60738** SQLITE_OK is returned if successful, or an SQLite error code if an
60739** error occurs. It is not an error if nBackfillAttempted cannot be
60740** decreased at all.
60741*/
60742SQLITE_PRIVATE int sqlite3WalSnapshotRecover(Wal *pWal){
60743 int rc;
60744
60745 assert( pWal->readLock>=0 );
60746 rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
60747 if( rc==SQLITE_OK ){
60748 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
60749 int szPage = (int)pWal->szPage;
60750 i64 szDb; /* Size of db file in bytes */
60751
60752 rc = sqlite3OsFileSize(pWal->pDbFd, &szDb);
60753 if( rc==SQLITE_OK ){
60754 void *pBuf1 = sqlite3_malloc(szPage);
60755 void *pBuf2 = sqlite3_malloc(szPage);
60756 if( pBuf1==0 || pBuf2==0 ){
60757 rc = SQLITE_NOMEM;
60758 }else{
60759 u32 i = pInfo->nBackfillAttempted;
60760 for(i=pInfo->nBackfillAttempted; i>pInfo->nBackfill; i--){
60761 WalHashLoc sLoc; /* Hash table location */
60762 u32 pgno; /* Page number in db file */
60763 i64 iDbOff; /* Offset of db file entry */
60764 i64 iWalOff; /* Offset of wal file entry */
60765
60766 rc = walHashGet(pWal, walFramePage(i), &sLoc);
60767 if( rc!=SQLITE_OK ) break;
60768 pgno = sLoc.aPgno[i-sLoc.iZero];
60769 iDbOff = (i64)(pgno-1) * szPage;
60770
60771 if( iDbOff+szPage<=szDb ){
60772 iWalOff = walFrameOffset(i, szPage) + WAL_FRAME_HDRSIZE;
60773 rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff);
60774
60775 if( rc==SQLITE_OK ){
60776 rc = sqlite3OsRead(pWal->pDbFd, pBuf2, szPage, iDbOff);
60777 }
60778
60779 if( rc!=SQLITE_OK || 0==memcmp(pBuf1, pBuf2, szPage) ){
60780 break;
60781 }
60782 }
60783
60784 pInfo->nBackfillAttempted = i-1;
60785 }
60786 }
60787
60788 sqlite3_free(pBuf1);
60789 sqlite3_free(pBuf2);
60790 }
60791 walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
60792 }
60793
60794 return rc;
60795}
60796#endif /* SQLITE_ENABLE_SNAPSHOT */
60797
60798/*
60799** Begin a read transaction on the database.
60800**
60801** This routine used to be called sqlite3OpenSnapshot() and with good reason:
60802** it takes a snapshot of the state of the WAL and wal-index for the current
60803** instant in time. The current thread will continue to use this snapshot.
60804** Other threads might append new content to the WAL and wal-index but
60805** that extra content is ignored by the current thread.
60806**
60807** If the database contents have changes since the previous read
60808** transaction, then *pChanged is set to 1 before returning. The
60809** Pager layer will use this to know that its cache is stale and
60810** needs to be flushed.
60811*/
60812SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
60813 int rc; /* Return code */
60814 int cnt = 0; /* Number of TryBeginRead attempts */
60815
60816#ifdef SQLITE_ENABLE_SNAPSHOT
60817 int bChanged = 0;
60818 WalIndexHdr *pSnapshot = pWal->pSnapshot;
60819 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
60820 bChanged = 1;
60821 }
60822#endif
60823
60824 do{
60825 rc = walTryBeginRead(pWal, pChanged, 0, ++cnt);
60826 }while( rc==WAL_RETRY );
60827 testcase( (rc&0xff)==SQLITE_BUSY );
60828 testcase( (rc&0xff)==SQLITE_IOERR );
60829 testcase( rc==SQLITE_PROTOCOL );
60830 testcase( rc==SQLITE_OK );
60831
60832#ifdef SQLITE_ENABLE_SNAPSHOT
60833 if( rc==SQLITE_OK ){
60834 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
60835 /* At this point the client has a lock on an aReadMark[] slot holding
60836 ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr
60837 ** is populated with the wal-index header corresponding to the head
60838 ** of the wal file. Verify that pSnapshot is still valid before
60839 ** continuing. Reasons why pSnapshot might no longer be valid:
60840 **
60841 ** (1) The WAL file has been reset since the snapshot was taken.
60842 ** In this case, the salt will have changed.
60843 **
60844 ** (2) A checkpoint as been attempted that wrote frames past
60845 ** pSnapshot->mxFrame into the database file. Note that the
60846 ** checkpoint need not have completed for this to cause problems.
60847 */
60848 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
60849
60850 assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 );
60851 assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame );
60852
60853 /* It is possible that there is a checkpointer thread running
60854 ** concurrent with this code. If this is the case, it may be that the
60855 ** checkpointer has already determined that it will checkpoint
60856 ** snapshot X, where X is later in the wal file than pSnapshot, but
60857 ** has not yet set the pInfo->nBackfillAttempted variable to indicate
60858 ** its intent. To avoid the race condition this leads to, ensure that
60859 ** there is no checkpointer process by taking a shared CKPT lock
60860 ** before checking pInfo->nBackfillAttempted.
60861 **
60862 ** TODO: Does the aReadMark[] lock prevent a checkpointer from doing
60863 ** this already?
60864 */
60865 rc = walLockShared(pWal, WAL_CKPT_LOCK);
60866
60867 if( rc==SQLITE_OK ){
60868 /* Check that the wal file has not been wrapped. Assuming that it has
60869 ** not, also check that no checkpointer has attempted to checkpoint any
60870 ** frames beyond pSnapshot->mxFrame. If either of these conditions are
60871 ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr
60872 ** with *pSnapshot and set *pChanged as appropriate for opening the
60873 ** snapshot. */
60874 if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
60875 && pSnapshot->mxFrame>=pInfo->nBackfillAttempted
60876 ){
60877 assert( pWal->readLock>0 );
60878 memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr));
60879 *pChanged = bChanged;
60880 }else{
60881 rc = SQLITE_ERROR_SNAPSHOT;
60882 }
60883
60884 /* Release the shared CKPT lock obtained above. */
60885 walUnlockShared(pWal, WAL_CKPT_LOCK);
60886 pWal->minFrame = 1;
60887 }
60888
60889
60890 if( rc!=SQLITE_OK ){
60891 sqlite3WalEndReadTransaction(pWal);
60892 }
60893 }
60894 }
60895#endif
60896 return rc;
60897}
60898
60899/*
60900** Finish with a read transaction. All this does is release the
60901** read-lock.
60902*/
60903SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){
60904 sqlite3WalEndWriteTransaction(pWal);
60905 if( pWal->readLock>=0 ){
60906 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
60907 pWal->readLock = -1;
60908 }
60909}
60910
60911/*
60912** Search the wal file for page pgno. If found, set *piRead to the frame that
60913** contains the page. Otherwise, if pgno is not in the wal file, set *piRead
60914** to zero.
60915**
60916** Return SQLITE_OK if successful, or an error code if an error occurs. If an
60917** error does occur, the final value of *piRead is undefined.
60918*/
60919SQLITE_PRIVATE int sqlite3WalFindFrame(
60920 Wal *pWal, /* WAL handle */
60921 Pgno pgno, /* Database page number to read data for */
60922 u32 *piRead /* OUT: Frame number (or zero) */
60923){
60924 u32 iRead = 0; /* If !=0, WAL frame to return data from */
60925 u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
60926 int iHash; /* Used to loop through N hash tables */
60927 int iMinHash;
60928
60929 /* This routine is only be called from within a read transaction. */
60930 assert( pWal->readLock>=0 || pWal->lockError );
60931
60932 /* If the "last page" field of the wal-index header snapshot is 0, then
60933 ** no data will be read from the wal under any circumstances. Return early
60934 ** in this case as an optimization. Likewise, if pWal->readLock==0,
60935 ** then the WAL is ignored by the reader so return early, as if the
60936 ** WAL were empty.
60937 */
60938 if( iLast==0 || (pWal->readLock==0 && pWal->bShmUnreliable==0) ){
60939 *piRead = 0;
60940 return SQLITE_OK;
60941 }
60942
60943 /* Search the hash table or tables for an entry matching page number
60944 ** pgno. Each iteration of the following for() loop searches one
60945 ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
60946 **
60947 ** This code might run concurrently to the code in walIndexAppend()
60948 ** that adds entries to the wal-index (and possibly to this hash
60949 ** table). This means the value just read from the hash
60950 ** slot (aHash[iKey]) may have been added before or after the
60951 ** current read transaction was opened. Values added after the
60952 ** read transaction was opened may have been written incorrectly -
60953 ** i.e. these slots may contain garbage data. However, we assume
60954 ** that any slots written before the current read transaction was
60955 ** opened remain unmodified.
60956 **
60957 ** For the reasons above, the if(...) condition featured in the inner
60958 ** loop of the following block is more stringent that would be required
60959 ** if we had exclusive access to the hash-table:
60960 **
60961 ** (aPgno[iFrame]==pgno):
60962 ** This condition filters out normal hash-table collisions.
60963 **
60964 ** (iFrame<=iLast):
60965 ** This condition filters out entries that were added to the hash
60966 ** table after the current read-transaction had started.
60967 */
60968 iMinHash = walFramePage(pWal->minFrame);
60969 for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){
60970 WalHashLoc sLoc; /* Hash table location */
60971 int iKey; /* Hash slot index */
60972 int nCollide; /* Number of hash collisions remaining */
60973 int rc; /* Error code */
60974
60975 rc = walHashGet(pWal, iHash, &sLoc);
60976 if( rc!=SQLITE_OK ){
60977 return rc;
60978 }
60979 nCollide = HASHTABLE_NSLOT;
60980 for(iKey=walHash(pgno); sLoc.aHash[iKey]; iKey=walNextHash(iKey)){
60981 u32 iFrame = sLoc.aHash[iKey] + sLoc.iZero;
60982 if( iFrame<=iLast && iFrame>=pWal->minFrame
60983 && sLoc.aPgno[sLoc.aHash[iKey]]==pgno ){
60984 assert( iFrame>iRead || CORRUPT_DB );
60985 iRead = iFrame;
60986 }
60987 if( (nCollide--)==0 ){
60988 return SQLITE_CORRUPT_BKPT;
60989 }
60990 }
60991 if( iRead ) break;
60992 }
60993
60994#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
60995 /* If expensive assert() statements are available, do a linear search
60996 ** of the wal-index file content. Make sure the results agree with the
60997 ** result obtained using the hash indexes above. */
60998 {
60999 u32 iRead2 = 0;
61000 u32 iTest;
61001 assert( pWal->bShmUnreliable || pWal->minFrame>0 );
61002 for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){
61003 if( walFramePgno(pWal, iTest)==pgno ){
61004 iRead2 = iTest;
61005 break;
61006 }
61007 }
61008 assert( iRead==iRead2 );
61009 }
61010#endif
61011
61012 *piRead = iRead;
61013 return SQLITE_OK;
61014}
61015
61016/*
61017** Read the contents of frame iRead from the wal file into buffer pOut
61018** (which is nOut bytes in size). Return SQLITE_OK if successful, or an
61019** error code otherwise.
61020*/
61021SQLITE_PRIVATE int sqlite3WalReadFrame(
61022 Wal *pWal, /* WAL handle */
61023 u32 iRead, /* Frame to read */
61024 int nOut, /* Size of buffer pOut in bytes */
61025 u8 *pOut /* Buffer to write page data to */
61026){
61027 int sz;
61028 i64 iOffset;
61029 sz = pWal->hdr.szPage;
61030 sz = (sz&0xfe00) + ((sz&0x0001)<<16);
61031 testcase( sz<=32768 );
61032 testcase( sz>=65536 );
61033 iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
61034 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
61035 return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
61036}
61037
61038/*
61039** Return the size of the database in pages (or zero, if unknown).
61040*/
61041SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal){
61042 if( pWal && ALWAYS(pWal->readLock>=0) ){
61043 return pWal->hdr.nPage;
61044 }
61045 return 0;
61046}
61047
61048
61049/*
61050** This function starts a write transaction on the WAL.
61051**
61052** A read transaction must have already been started by a prior call
61053** to sqlite3WalBeginReadTransaction().
61054**
61055** If another thread or process has written into the database since
61056** the read transaction was started, then it is not possible for this
61057** thread to write as doing so would cause a fork. So this routine
61058** returns SQLITE_BUSY in that case and no write transaction is started.
61059**
61060** There can only be a single writer active at a time.
61061*/
61062SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){
61063 int rc;
61064
61065 /* Cannot start a write transaction without first holding a read
61066 ** transaction. */
61067 assert( pWal->readLock>=0 );
61068 assert( pWal->writeLock==0 && pWal->iReCksum==0 );
61069
61070 if( pWal->readOnly ){
61071 return SQLITE_READONLY;
61072 }
61073
61074 /* Only one writer allowed at a time. Get the write lock. Return
61075 ** SQLITE_BUSY if unable.
61076 */
61077 rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
61078 if( rc ){
61079 return rc;
61080 }
61081 pWal->writeLock = 1;
61082
61083 /* If another connection has written to the database file since the
61084 ** time the read transaction on this connection was started, then
61085 ** the write is disallowed.
61086 */
61087 if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
61088 walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
61089 pWal->writeLock = 0;
61090 rc = SQLITE_BUSY_SNAPSHOT;
61091 }
61092
61093 return rc;
61094}
61095
61096/*
61097** End a write transaction. The commit has already been done. This
61098** routine merely releases the lock.
61099*/
61100SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal){
61101 if( pWal->writeLock ){
61102 walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
61103 pWal->writeLock = 0;
61104 pWal->iReCksum = 0;
61105 pWal->truncateOnCommit = 0;
61106 }
61107 return SQLITE_OK;
61108}
61109
61110/*
61111** If any data has been written (but not committed) to the log file, this
61112** function moves the write-pointer back to the start of the transaction.
61113**
61114** Additionally, the callback function is invoked for each frame written
61115** to the WAL since the start of the transaction. If the callback returns
61116** other than SQLITE_OK, it is not invoked again and the error code is
61117** returned to the caller.
61118**
61119** Otherwise, if the callback function does not return an error, this
61120** function returns SQLITE_OK.
61121*/
61122SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
61123 int rc = SQLITE_OK;
61124 if( ALWAYS(pWal->writeLock) ){
61125 Pgno iMax = pWal->hdr.mxFrame;
61126 Pgno iFrame;
61127
61128 /* Restore the clients cache of the wal-index header to the state it
61129 ** was in before the client began writing to the database.
61130 */
61131 memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
61132
61133 for(iFrame=pWal->hdr.mxFrame+1;
61134 ALWAYS(rc==SQLITE_OK) && iFrame<=iMax;
61135 iFrame++
61136 ){
61137 /* This call cannot fail. Unless the page for which the page number
61138 ** is passed as the second argument is (a) in the cache and
61139 ** (b) has an outstanding reference, then xUndo is either a no-op
61140 ** (if (a) is false) or simply expels the page from the cache (if (b)
61141 ** is false).
61142 **
61143 ** If the upper layer is doing a rollback, it is guaranteed that there
61144 ** are no outstanding references to any page other than page 1. And
61145 ** page 1 is never written to the log until the transaction is
61146 ** committed. As a result, the call to xUndo may not fail.
61147 */
61148 assert( walFramePgno(pWal, iFrame)!=1 );
61149 rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
61150 }
61151 if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
61152 }
61153 return rc;
61154}
61155
61156/*
61157** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32
61158** values. This function populates the array with values required to
61159** "rollback" the write position of the WAL handle back to the current
61160** point in the event of a savepoint rollback (via WalSavepointUndo()).
61161*/
61162SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){
61163 assert( pWal->writeLock );
61164 aWalData[0] = pWal->hdr.mxFrame;
61165 aWalData[1] = pWal->hdr.aFrameCksum[0];
61166 aWalData[2] = pWal->hdr.aFrameCksum[1];
61167 aWalData[3] = pWal->nCkpt;
61168}
61169
61170/*
61171** Move the write position of the WAL back to the point identified by
61172** the values in the aWalData[] array. aWalData must point to an array
61173** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated
61174** by a call to WalSavepoint().
61175*/
61176SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){
61177 int rc = SQLITE_OK;
61178
61179 assert( pWal->writeLock );
61180 assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
61181
61182 if( aWalData[3]!=pWal->nCkpt ){
61183 /* This savepoint was opened immediately after the write-transaction
61184 ** was started. Right after that, the writer decided to wrap around
61185 ** to the start of the log. Update the savepoint values to match.
61186 */
61187 aWalData[0] = 0;
61188 aWalData[3] = pWal->nCkpt;
61189 }
61190
61191 if( aWalData[0]<pWal->hdr.mxFrame ){
61192 pWal->hdr.mxFrame = aWalData[0];
61193 pWal->hdr.aFrameCksum[0] = aWalData[1];
61194 pWal->hdr.aFrameCksum[1] = aWalData[2];
61195 walCleanupHash(pWal);
61196 }
61197
61198 return rc;
61199}
61200
61201/*
61202** This function is called just before writing a set of frames to the log
61203** file (see sqlite3WalFrames()). It checks to see if, instead of appending
61204** to the current log file, it is possible to overwrite the start of the
61205** existing log file with the new frames (i.e. "reset" the log). If so,
61206** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
61207** unchanged.
61208**
61209** SQLITE_OK is returned if no error is encountered (regardless of whether
61210** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
61211** if an error occurs.
61212*/
61213static int walRestartLog(Wal *pWal){
61214 int rc = SQLITE_OK;
61215 int cnt;
61216
61217 if( pWal->readLock==0 ){
61218 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
61219 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
61220 if( pInfo->nBackfill>0 ){
61221 u32 salt1;
61222 sqlite3_randomness(4, &salt1);
61223 rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
61224 if( rc==SQLITE_OK ){
61225 /* If all readers are using WAL_READ_LOCK(0) (in other words if no
61226 ** readers are currently using the WAL), then the transactions
61227 ** frames will overwrite the start of the existing log. Update the
61228 ** wal-index header to reflect this.
61229 **
61230 ** In theory it would be Ok to update the cache of the header only
61231 ** at this point. But updating the actual wal-index header is also
61232 ** safe and means there is no special case for sqlite3WalUndo()
61233 ** to handle if this transaction is rolled back. */
61234 walRestartHdr(pWal, salt1);
61235 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
61236 }else if( rc!=SQLITE_BUSY ){
61237 return rc;
61238 }
61239 }
61240 walUnlockShared(pWal, WAL_READ_LOCK(0));
61241 pWal->readLock = -1;
61242 cnt = 0;
61243 do{
61244 int notUsed;
61245 rc = walTryBeginRead(pWal, &notUsed, 1, ++cnt);
61246 }while( rc==WAL_RETRY );
61247 assert( (rc&0xff)!=SQLITE_BUSY ); /* BUSY not possible when useWal==1 */
61248 testcase( (rc&0xff)==SQLITE_IOERR );
61249 testcase( rc==SQLITE_PROTOCOL );
61250 testcase( rc==SQLITE_OK );
61251 }
61252 return rc;
61253}
61254
61255/*
61256** Information about the current state of the WAL file and where
61257** the next fsync should occur - passed from sqlite3WalFrames() into
61258** walWriteToLog().
61259*/
61260typedef struct WalWriter {
61261 Wal *pWal; /* The complete WAL information */
61262 sqlite3_file *pFd; /* The WAL file to which we write */
61263 sqlite3_int64 iSyncPoint; /* Fsync at this offset */
61264 int syncFlags; /* Flags for the fsync */
61265 int szPage; /* Size of one page */
61266} WalWriter;
61267
61268/*
61269** Write iAmt bytes of content into the WAL file beginning at iOffset.
61270** Do a sync when crossing the p->iSyncPoint boundary.
61271**
61272** In other words, if iSyncPoint is in between iOffset and iOffset+iAmt,
61273** first write the part before iSyncPoint, then sync, then write the
61274** rest.
61275*/
61276static int walWriteToLog(
61277 WalWriter *p, /* WAL to write to */
61278 void *pContent, /* Content to be written */
61279 int iAmt, /* Number of bytes to write */
61280 sqlite3_int64 iOffset /* Start writing at this offset */
61281){
61282 int rc;
61283 if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
61284 int iFirstAmt = (int)(p->iSyncPoint - iOffset);
61285 rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
61286 if( rc ) return rc;
61287 iOffset += iFirstAmt;
61288 iAmt -= iFirstAmt;
61289 pContent = (void*)(iFirstAmt + (char*)pContent);
61290 assert( WAL_SYNC_FLAGS(p->syncFlags)!=0 );
61291 rc = sqlite3OsSync(p->pFd, WAL_SYNC_FLAGS(p->syncFlags));
61292 if( iAmt==0 || rc ) return rc;
61293 }
61294 rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
61295 return rc;
61296}
61297
61298/*
61299** Write out a single frame of the WAL
61300*/
61301static int walWriteOneFrame(
61302 WalWriter *p, /* Where to write the frame */
61303 PgHdr *pPage, /* The page of the frame to be written */
61304 int nTruncate, /* The commit flag. Usually 0. >0 for commit */
61305 sqlite3_int64 iOffset /* Byte offset at which to write */
61306){
61307 int rc; /* Result code from subfunctions */
61308 void *pData; /* Data actually written */
61309 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
61310#if defined(SQLITE_HAS_CODEC)
61311 if( (pData = sqlite3PagerCodec(pPage))==0 ) return SQLITE_NOMEM_BKPT;
61312#else
61313 pData = pPage->pData;
61314#endif
61315 walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
61316 rc = walWriteToLog(p, aFrame, sizeof(aFrame), iOffset);
61317 if( rc ) return rc;
61318 /* Write the page data */
61319 rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
61320 return rc;
61321}
61322
61323/*
61324** This function is called as part of committing a transaction within which
61325** one or more frames have been overwritten. It updates the checksums for
61326** all frames written to the wal file by the current transaction starting
61327** with the earliest to have been overwritten.
61328**
61329** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
61330*/
61331static int walRewriteChecksums(Wal *pWal, u32 iLast){
61332 const int szPage = pWal->szPage;/* Database page size */
61333 int rc = SQLITE_OK; /* Return code */
61334 u8 *aBuf; /* Buffer to load data from wal file into */
61335 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-headers in */
61336 u32 iRead; /* Next frame to read from wal file */
61337 i64 iCksumOff;
61338
61339 aBuf = sqlite3_malloc(szPage + WAL_FRAME_HDRSIZE);
61340 if( aBuf==0 ) return SQLITE_NOMEM_BKPT;
61341
61342 /* Find the checksum values to use as input for the recalculating the
61343 ** first checksum. If the first frame is frame 1 (implying that the current
61344 ** transaction restarted the wal file), these values must be read from the
61345 ** wal-file header. Otherwise, read them from the frame header of the
61346 ** previous frame. */
61347 assert( pWal->iReCksum>0 );
61348 if( pWal->iReCksum==1 ){
61349 iCksumOff = 24;
61350 }else{
61351 iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage) + 16;
61352 }
61353 rc = sqlite3OsRead(pWal->pWalFd, aBuf, sizeof(u32)*2, iCksumOff);
61354 pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(aBuf);
61355 pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(&aBuf[sizeof(u32)]);
61356
61357 iRead = pWal->iReCksum;
61358 pWal->iReCksum = 0;
61359 for(; rc==SQLITE_OK && iRead<=iLast; iRead++){
61360 i64 iOff = walFrameOffset(iRead, szPage);
61361 rc = sqlite3OsRead(pWal->pWalFd, aBuf, szPage+WAL_FRAME_HDRSIZE, iOff);
61362 if( rc==SQLITE_OK ){
61363 u32 iPgno, nDbSize;
61364 iPgno = sqlite3Get4byte(aBuf);
61365 nDbSize = sqlite3Get4byte(&aBuf[4]);
61366
61367 walEncodeFrame(pWal, iPgno, nDbSize, &aBuf[WAL_FRAME_HDRSIZE], aFrame);
61368 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOff);
61369 }
61370 }
61371
61372 sqlite3_free(aBuf);
61373 return rc;
61374}
61375
61376/*
61377** Write a set of frames to the log. The caller must hold the write-lock
61378** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
61379*/
61380SQLITE_PRIVATE int sqlite3WalFrames(
61381 Wal *pWal, /* Wal handle to write to */
61382 int szPage, /* Database page-size in bytes */
61383 PgHdr *pList, /* List of dirty pages to write */
61384 Pgno nTruncate, /* Database size after this commit */
61385 int isCommit, /* True if this is a commit */
61386 int sync_flags /* Flags to pass to OsSync() (or 0) */
61387){
61388 int rc; /* Used to catch return codes */
61389 u32 iFrame; /* Next frame address */
61390 PgHdr *p; /* Iterator to run through pList with. */
61391 PgHdr *pLast = 0; /* Last frame in list */
61392 int nExtra = 0; /* Number of extra copies of last page */
61393 int szFrame; /* The size of a single frame */
61394 i64 iOffset; /* Next byte to write in WAL file */
61395 WalWriter w; /* The writer */
61396 u32 iFirst = 0; /* First frame that may be overwritten */
61397 WalIndexHdr *pLive; /* Pointer to shared header */
61398
61399 assert( pList );
61400 assert( pWal->writeLock );
61401
61402 /* If this frame set completes a transaction, then nTruncate>0. If
61403 ** nTruncate==0 then this frame set does not complete the transaction. */
61404 assert( (isCommit!=0)==(nTruncate!=0) );
61405
61406#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
61407 { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
61408 WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
61409 pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
61410 }
61411#endif
61412
61413 pLive = (WalIndexHdr*)walIndexHdr(pWal);
61414 if( memcmp(&pWal->hdr, (void *)pLive, sizeof(WalIndexHdr))!=0 ){
61415 iFirst = pLive->mxFrame+1;
61416 }
61417
61418 /* See if it is possible to write these frames into the start of the
61419 ** log file, instead of appending to it at pWal->hdr.mxFrame.
61420 */
61421 if( SQLITE_OK!=(rc = walRestartLog(pWal)) ){
61422 return rc;
61423 }
61424
61425 /* If this is the first frame written into the log, write the WAL
61426 ** header to the start of the WAL file. See comments at the top of
61427 ** this source file for a description of the WAL header format.
61428 */
61429 iFrame = pWal->hdr.mxFrame;
61430 if( iFrame==0 ){
61431 u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assemble wal-header in */
61432 u32 aCksum[2]; /* Checksum for wal-header */
61433
61434 sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
61435 sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION);
61436 sqlite3Put4byte(&aWalHdr[8], szPage);
61437 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
61438 if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
61439 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
61440 walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
61441 sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
61442 sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
61443
61444 pWal->szPage = szPage;
61445 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
61446 pWal->hdr.aFrameCksum[0] = aCksum[0];
61447 pWal->hdr.aFrameCksum[1] = aCksum[1];
61448 pWal->truncateOnCommit = 1;
61449
61450 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
61451 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
61452 if( rc!=SQLITE_OK ){
61453 return rc;
61454 }
61455
61456 /* Sync the header (unless SQLITE_IOCAP_SEQUENTIAL is true or unless
61457 ** all syncing is turned off by PRAGMA synchronous=OFF). Otherwise
61458 ** an out-of-order write following a WAL restart could result in
61459 ** database corruption. See the ticket:
61460 **
61461 ** https://sqlite.org/src/info/ff5be73dee
61462 */
61463 if( pWal->syncHeader ){
61464 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
61465 if( rc ) return rc;
61466 }
61467 }
61468 assert( (int)pWal->szPage==szPage );
61469
61470 /* Setup information needed to write frames into the WAL */
61471 w.pWal = pWal;
61472 w.pFd = pWal->pWalFd;
61473 w.iSyncPoint = 0;
61474 w.syncFlags = sync_flags;
61475 w.szPage = szPage;
61476 iOffset = walFrameOffset(iFrame+1, szPage);
61477 szFrame = szPage + WAL_FRAME_HDRSIZE;
61478
61479 /* Write all frames into the log file exactly once */
61480 for(p=pList; p; p=p->pDirty){
61481 int nDbSize; /* 0 normally. Positive == commit flag */
61482
61483 /* Check if this page has already been written into the wal file by
61484 ** the current transaction. If so, overwrite the existing frame and
61485 ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that
61486 ** checksums must be recomputed when the transaction is committed. */
61487 if( iFirst && (p->pDirty || isCommit==0) ){
61488 u32 iWrite = 0;
61489 VVA_ONLY(rc =) sqlite3WalFindFrame(pWal, p->pgno, &iWrite);
61490 assert( rc==SQLITE_OK || iWrite==0 );
61491 if( iWrite>=iFirst ){
61492 i64 iOff = walFrameOffset(iWrite, szPage) + WAL_FRAME_HDRSIZE;
61493 void *pData;
61494 if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
61495 pWal->iReCksum = iWrite;
61496 }
61497#if defined(SQLITE_HAS_CODEC)
61498 if( (pData = sqlite3PagerCodec(p))==0 ) return SQLITE_NOMEM;
61499#else
61500 pData = p->pData;
61501#endif
61502 rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
61503 if( rc ) return rc;
61504 p->flags &= ~PGHDR_WAL_APPEND;
61505 continue;
61506 }
61507 }
61508
61509 iFrame++;
61510 assert( iOffset==walFrameOffset(iFrame, szPage) );
61511 nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
61512 rc = walWriteOneFrame(&w, p, nDbSize, iOffset);
61513 if( rc ) return rc;
61514 pLast = p;
61515 iOffset += szFrame;
61516 p->flags |= PGHDR_WAL_APPEND;
61517 }
61518
61519 /* Recalculate checksums within the wal file if required. */
61520 if( isCommit && pWal->iReCksum ){
61521 rc = walRewriteChecksums(pWal, iFrame);
61522 if( rc ) return rc;
61523 }
61524
61525 /* If this is the end of a transaction, then we might need to pad
61526 ** the transaction and/or sync the WAL file.
61527 **
61528 ** Padding and syncing only occur if this set of frames complete a
61529 ** transaction and if PRAGMA synchronous=FULL. If synchronous==NORMAL
61530 ** or synchronous==OFF, then no padding or syncing are needed.
61531 **
61532 ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not
61533 ** needed and only the sync is done. If padding is needed, then the
61534 ** final frame is repeated (with its commit mark) until the next sector
61535 ** boundary is crossed. Only the part of the WAL prior to the last
61536 ** sector boundary is synced; the part of the last frame that extends
61537 ** past the sector boundary is written after the sync.
61538 */
61539 if( isCommit && WAL_SYNC_FLAGS(sync_flags)!=0 ){
61540 int bSync = 1;
61541 if( pWal->padToSectorBoundary ){
61542 int sectorSize = sqlite3SectorSize(pWal->pWalFd);
61543 w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
61544 bSync = (w.iSyncPoint==iOffset);
61545 testcase( bSync );
61546 while( iOffset<w.iSyncPoint ){
61547 rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
61548 if( rc ) return rc;
61549 iOffset += szFrame;
61550 nExtra++;
61551 }
61552 }
61553 if( bSync ){
61554 assert( rc==SQLITE_OK );
61555 rc = sqlite3OsSync(w.pFd, WAL_SYNC_FLAGS(sync_flags));
61556 }
61557 }
61558
61559 /* If this frame set completes the first transaction in the WAL and
61560 ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
61561 ** journal size limit, if possible.
61562 */
61563 if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
61564 i64 sz = pWal->mxWalSize;
61565 if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
61566 sz = walFrameOffset(iFrame+nExtra+1, szPage);
61567 }
61568 walLimitSize(pWal, sz);
61569 pWal->truncateOnCommit = 0;
61570 }
61571
61572 /* Append data to the wal-index. It is not necessary to lock the
61573 ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
61574 ** guarantees that there are no other writers, and no data that may
61575 ** be in use by existing readers is being overwritten.
61576 */
61577 iFrame = pWal->hdr.mxFrame;
61578 for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
61579 if( (p->flags & PGHDR_WAL_APPEND)==0 ) continue;
61580 iFrame++;
61581 rc = walIndexAppend(pWal, iFrame, p->pgno);
61582 }
61583 while( rc==SQLITE_OK && nExtra>0 ){
61584 iFrame++;
61585 nExtra--;
61586 rc = walIndexAppend(pWal, iFrame, pLast->pgno);
61587 }
61588
61589 if( rc==SQLITE_OK ){
61590 /* Update the private copy of the header. */
61591 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
61592 testcase( szPage<=32768 );
61593 testcase( szPage>=65536 );
61594 pWal->hdr.mxFrame = iFrame;
61595 if( isCommit ){
61596 pWal->hdr.iChange++;
61597 pWal->hdr.nPage = nTruncate;
61598 }
61599 /* If this is a commit, update the wal-index header too. */
61600 if( isCommit ){
61601 walIndexWriteHdr(pWal);
61602 pWal->iCallback = iFrame;
61603 }
61604 }
61605
61606 WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
61607 return rc;
61608}
61609
61610/*
61611** This routine is called to implement sqlite3_wal_checkpoint() and
61612** related interfaces.
61613**
61614** Obtain a CHECKPOINT lock and then backfill as much information as
61615** we can from WAL into the database.
61616**
61617** If parameter xBusy is not NULL, it is a pointer to a busy-handler
61618** callback. In this case this function runs a blocking checkpoint.
61619*/
61620SQLITE_PRIVATE int sqlite3WalCheckpoint(
61621 Wal *pWal, /* Wal connection */
61622 sqlite3 *db, /* Check this handle's interrupt flag */
61623 int eMode, /* PASSIVE, FULL, RESTART, or TRUNCATE */
61624 int (*xBusy)(void*), /* Function to call when busy */
61625 void *pBusyArg, /* Context argument for xBusyHandler */
61626 int sync_flags, /* Flags to sync db file with (or 0) */
61627 int nBuf, /* Size of temporary buffer */
61628 u8 *zBuf, /* Temporary buffer to use */
61629 int *pnLog, /* OUT: Number of frames in WAL */
61630 int *pnCkpt /* OUT: Number of backfilled frames in WAL */
61631){
61632 int rc; /* Return code */
61633 int isChanged = 0; /* True if a new wal-index header is loaded */
61634 int eMode2 = eMode; /* Mode to pass to walCheckpoint() */
61635 int (*xBusy2)(void*) = xBusy; /* Busy handler for eMode2 */
61636
61637 assert( pWal->ckptLock==0 );
61638 assert( pWal->writeLock==0 );
61639
61640 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
61641 ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
61642 assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
61643
61644 if( pWal->readOnly ) return SQLITE_READONLY;
61645 WALTRACE(("WAL%p: checkpoint begins\n", pWal));
61646
61647 /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
61648 ** "checkpoint" lock on the database file. */
61649 rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
61650 if( rc ){
61651 /* EVIDENCE-OF: R-10421-19736 If any other process is running a
61652 ** checkpoint operation at the same time, the lock cannot be obtained and
61653 ** SQLITE_BUSY is returned.
61654 ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
61655 ** it will not be invoked in this case.
61656 */
61657 testcase( rc==SQLITE_BUSY );
61658 testcase( xBusy!=0 );
61659 return rc;
61660 }
61661 pWal->ckptLock = 1;
61662
61663 /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
61664 ** TRUNCATE modes also obtain the exclusive "writer" lock on the database
61665 ** file.
61666 **
61667 ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
61668 ** immediately, and a busy-handler is configured, it is invoked and the
61669 ** writer lock retried until either the busy-handler returns 0 or the
61670 ** lock is successfully obtained.
61671 */
61672 if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){
61673 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_WRITE_LOCK, 1);
61674 if( rc==SQLITE_OK ){
61675 pWal->writeLock = 1;
61676 }else if( rc==SQLITE_BUSY ){
61677 eMode2 = SQLITE_CHECKPOINT_PASSIVE;
61678 xBusy2 = 0;
61679 rc = SQLITE_OK;
61680 }
61681 }
61682
61683 /* Read the wal-index header. */
61684 if( rc==SQLITE_OK ){
61685 rc = walIndexReadHdr(pWal, &isChanged);
61686 if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
61687 sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
61688 }
61689 }
61690
61691 /* Copy data from the log to the database file. */
61692 if( rc==SQLITE_OK ){
61693
61694 if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
61695 rc = SQLITE_CORRUPT_BKPT;
61696 }else{
61697 rc = walCheckpoint(pWal, db, eMode2, xBusy2, pBusyArg, sync_flags, zBuf);
61698 }
61699
61700 /* If no error occurred, set the output variables. */
61701 if( rc==SQLITE_OK || rc==SQLITE_BUSY ){
61702 if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
61703 if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
61704 }
61705 }
61706
61707 if( isChanged ){
61708 /* If a new wal-index header was loaded before the checkpoint was
61709 ** performed, then the pager-cache associated with pWal is now
61710 ** out of date. So zero the cached wal-index header to ensure that
61711 ** next time the pager opens a snapshot on this database it knows that
61712 ** the cache needs to be reset.
61713 */
61714 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
61715 }
61716
61717 /* Release the locks. */
61718 sqlite3WalEndWriteTransaction(pWal);
61719 walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
61720 pWal->ckptLock = 0;
61721 WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
61722 return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc);
61723}
61724
61725/* Return the value to pass to a sqlite3_wal_hook callback, the
61726** number of frames in the WAL at the point of the last commit since
61727** sqlite3WalCallback() was called. If no commits have occurred since
61728** the last call, then return 0.
61729*/
61730SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal){
61731 u32 ret = 0;
61732 if( pWal ){
61733 ret = pWal->iCallback;
61734 pWal->iCallback = 0;
61735 }
61736 return (int)ret;
61737}
61738
61739/*
61740** This function is called to change the WAL subsystem into or out
61741** of locking_mode=EXCLUSIVE.
61742**
61743** If op is zero, then attempt to change from locking_mode=EXCLUSIVE
61744** into locking_mode=NORMAL. This means that we must acquire a lock
61745** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL
61746** or if the acquisition of the lock fails, then return 0. If the
61747** transition out of exclusive-mode is successful, return 1. This
61748** operation must occur while the pager is still holding the exclusive
61749** lock on the main database file.
61750**
61751** If op is one, then change from locking_mode=NORMAL into
61752** locking_mode=EXCLUSIVE. This means that the pWal->readLock must
61753** be released. Return 1 if the transition is made and 0 if the
61754** WAL is already in exclusive-locking mode - meaning that this
61755** routine is a no-op. The pager must already hold the exclusive lock
61756** on the main database file before invoking this operation.
61757**
61758** If op is negative, then do a dry-run of the op==1 case but do
61759** not actually change anything. The pager uses this to see if it
61760** should acquire the database exclusive lock prior to invoking
61761** the op==1 case.
61762*/
61763SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op){
61764 int rc;
61765 assert( pWal->writeLock==0 );
61766 assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
61767
61768 /* pWal->readLock is usually set, but might be -1 if there was a
61769 ** prior error while attempting to acquire are read-lock. This cannot
61770 ** happen if the connection is actually in exclusive mode (as no xShmLock
61771 ** locks are taken in this case). Nor should the pager attempt to
61772 ** upgrade to exclusive-mode following such an error.
61773 */
61774 assert( pWal->readLock>=0 || pWal->lockError );
61775 assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
61776
61777 if( op==0 ){
61778 if( pWal->exclusiveMode!=WAL_NORMAL_MODE ){
61779 pWal->exclusiveMode = WAL_NORMAL_MODE;
61780 if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
61781 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
61782 }
61783 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
61784 }else{
61785 /* Already in locking_mode=NORMAL */
61786 rc = 0;
61787 }
61788 }else if( op>0 ){
61789 assert( pWal->exclusiveMode==WAL_NORMAL_MODE );
61790 assert( pWal->readLock>=0 );
61791 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
61792 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
61793 rc = 1;
61794 }else{
61795 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
61796 }
61797 return rc;
61798}
61799
61800/*
61801** Return true if the argument is non-NULL and the WAL module is using
61802** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
61803** WAL module is using shared-memory, return false.
61804*/
61805SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal){
61806 return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
61807}
61808
61809#ifdef SQLITE_ENABLE_SNAPSHOT
61810/* Create a snapshot object. The content of a snapshot is opaque to
61811** every other subsystem, so the WAL module can put whatever it needs
61812** in the object.
61813*/
61814SQLITE_PRIVATE int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot){
61815 int rc = SQLITE_OK;
61816 WalIndexHdr *pRet;
61817 static const u32 aZero[4] = { 0, 0, 0, 0 };
61818
61819 assert( pWal->readLock>=0 && pWal->writeLock==0 );
61820
61821 if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){
61822 *ppSnapshot = 0;
61823 return SQLITE_ERROR;
61824 }
61825 pRet = (WalIndexHdr*)sqlite3_malloc(sizeof(WalIndexHdr));
61826 if( pRet==0 ){
61827 rc = SQLITE_NOMEM_BKPT;
61828 }else{
61829 memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
61830 *ppSnapshot = (sqlite3_snapshot*)pRet;
61831 }
61832
61833 return rc;
61834}
61835
61836/* Try to open on pSnapshot when the next read-transaction starts
61837*/
61838SQLITE_PRIVATE void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot){
61839 pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
61840}
61841
61842/*
61843** Return a +ve value if snapshot p1 is newer than p2. A -ve value if
61844** p1 is older than p2 and zero if p1 and p2 are the same snapshot.
61845*/
61846SQLITE_API int sqlite3_snapshot_cmp(sqlite3_snapshot *p1, sqlite3_snapshot *p2){
61847 WalIndexHdr *pHdr1 = (WalIndexHdr*)p1;
61848 WalIndexHdr *pHdr2 = (WalIndexHdr*)p2;
61849
61850 /* aSalt[0] is a copy of the value stored in the wal file header. It
61851 ** is incremented each time the wal file is restarted. */
61852 if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1;
61853 if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1;
61854 if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1;
61855 if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1;
61856 return 0;
61857}
61858
61859/*
61860** The caller currently has a read transaction open on the database.
61861** This function takes a SHARED lock on the CHECKPOINTER slot and then
61862** checks if the snapshot passed as the second argument is still
61863** available. If so, SQLITE_OK is returned.
61864**
61865** If the snapshot is not available, SQLITE_ERROR is returned. Or, if
61866** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error
61867** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER
61868** lock is released before returning.
61869*/
61870SQLITE_PRIVATE int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot){
61871 int rc;
61872 rc = walLockShared(pWal, WAL_CKPT_LOCK);
61873 if( rc==SQLITE_OK ){
61874 WalIndexHdr *pNew = (WalIndexHdr*)pSnapshot;
61875 if( memcmp(pNew->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
61876 || pNew->mxFrame<walCkptInfo(pWal)->nBackfillAttempted
61877 ){
61878 rc = SQLITE_ERROR_SNAPSHOT;
61879 walUnlockShared(pWal, WAL_CKPT_LOCK);
61880 }
61881 }
61882 return rc;
61883}
61884
61885/*
61886** Release a lock obtained by an earlier successful call to
61887** sqlite3WalSnapshotCheck().
61888*/
61889SQLITE_PRIVATE void sqlite3WalSnapshotUnlock(Wal *pWal){
61890 assert( pWal );
61891 walUnlockShared(pWal, WAL_CKPT_LOCK);
61892}
61893
61894
61895#endif /* SQLITE_ENABLE_SNAPSHOT */
61896
61897#ifdef SQLITE_ENABLE_ZIPVFS
61898/*
61899** If the argument is not NULL, it points to a Wal object that holds a
61900** read-lock. This function returns the database page-size if it is known,
61901** or zero if it is not (or if pWal is NULL).
61902*/
61903SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal){
61904 assert( pWal==0 || pWal->readLock>=0 );
61905 return (pWal ? pWal->szPage : 0);
61906}
61907#endif
61908
61909/* Return the sqlite3_file object for the WAL file
61910*/
61911SQLITE_PRIVATE sqlite3_file *sqlite3WalFile(Wal *pWal){
61912 return pWal->pWalFd;
61913}
61914
61915#endif /* #ifndef SQLITE_OMIT_WAL */
61916
61917/************** End of wal.c *************************************************/
61918/************** Begin file btmutex.c *****************************************/
61919/*
61920** 2007 August 27
61921**
61922** The author disclaims copyright to this source code. In place of
61923** a legal notice, here is a blessing:
61924**
61925** May you do good and not evil.
61926** May you find forgiveness for yourself and forgive others.
61927** May you share freely, never taking more than you give.
61928**
61929*************************************************************************
61930**
61931** This file contains code used to implement mutexes on Btree objects.
61932** This code really belongs in btree.c. But btree.c is getting too
61933** big and we want to break it down some. This packaged seemed like
61934** a good breakout.
61935*/
61936/************** Include btreeInt.h in the middle of btmutex.c ****************/
61937/************** Begin file btreeInt.h ****************************************/
61938/*
61939** 2004 April 6
61940**
61941** The author disclaims copyright to this source code. In place of
61942** a legal notice, here is a blessing:
61943**
61944** May you do good and not evil.
61945** May you find forgiveness for yourself and forgive others.
61946** May you share freely, never taking more than you give.
61947**
61948*************************************************************************
61949** This file implements an external (disk-based) database using BTrees.
61950** For a detailed discussion of BTrees, refer to
61951**
61952** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
61953** "Sorting And Searching", pages 473-480. Addison-Wesley
61954** Publishing Company, Reading, Massachusetts.
61955**
61956** The basic idea is that each page of the file contains N database
61957** entries and N+1 pointers to subpages.
61958**
61959** ----------------------------------------------------------------
61960** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
61961** ----------------------------------------------------------------
61962**
61963** All of the keys on the page that Ptr(0) points to have values less
61964** than Key(0). All of the keys on page Ptr(1) and its subpages have
61965** values greater than Key(0) and less than Key(1). All of the keys
61966** on Ptr(N) and its subpages have values greater than Key(N-1). And
61967** so forth.
61968**
61969** Finding a particular key requires reading O(log(M)) pages from the
61970** disk where M is the number of entries in the tree.
61971**
61972** In this implementation, a single file can hold one or more separate
61973** BTrees. Each BTree is identified by the index of its root page. The
61974** key and data for any entry are combined to form the "payload". A
61975** fixed amount of payload can be carried directly on the database
61976** page. If the payload is larger than the preset amount then surplus
61977** bytes are stored on overflow pages. The payload for an entry
61978** and the preceding pointer are combined to form a "Cell". Each
61979** page has a small header which contains the Ptr(N) pointer and other
61980** information such as the size of key and data.
61981**
61982** FORMAT DETAILS
61983**
61984** The file is divided into pages. The first page is called page 1,
61985** the second is page 2, and so forth. A page number of zero indicates
61986** "no such page". The page size can be any power of 2 between 512 and 65536.
61987** Each page can be either a btree page, a freelist page, an overflow
61988** page, or a pointer-map page.
61989**
61990** The first page is always a btree page. The first 100 bytes of the first
61991** page contain a special header (the "file header") that describes the file.
61992** The format of the file header is as follows:
61993**
61994** OFFSET SIZE DESCRIPTION
61995** 0 16 Header string: "SQLite format 3\000"
61996** 16 2 Page size in bytes. (1 means 65536)
61997** 18 1 File format write version
61998** 19 1 File format read version
61999** 20 1 Bytes of unused space at the end of each page
62000** 21 1 Max embedded payload fraction (must be 64)
62001** 22 1 Min embedded payload fraction (must be 32)
62002** 23 1 Min leaf payload fraction (must be 32)
62003** 24 4 File change counter
62004** 28 4 Reserved for future use
62005** 32 4 First freelist page
62006** 36 4 Number of freelist pages in the file
62007** 40 60 15 4-byte meta values passed to higher layers
62008**
62009** 40 4 Schema cookie
62010** 44 4 File format of schema layer
62011** 48 4 Size of page cache
62012** 52 4 Largest root-page (auto/incr_vacuum)
62013** 56 4 1=UTF-8 2=UTF16le 3=UTF16be
62014** 60 4 User version
62015** 64 4 Incremental vacuum mode
62016** 68 4 Application-ID
62017** 72 20 unused
62018** 92 4 The version-valid-for number
62019** 96 4 SQLITE_VERSION_NUMBER
62020**
62021** All of the integer values are big-endian (most significant byte first).
62022**
62023** The file change counter is incremented when the database is changed
62024** This counter allows other processes to know when the file has changed
62025** and thus when they need to flush their cache.
62026**
62027** The max embedded payload fraction is the amount of the total usable
62028** space in a page that can be consumed by a single cell for standard
62029** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default
62030** is to limit the maximum cell size so that at least 4 cells will fit
62031** on one page. Thus the default max embedded payload fraction is 64.
62032**
62033** If the payload for a cell is larger than the max payload, then extra
62034** payload is spilled to overflow pages. Once an overflow page is allocated,
62035** as many bytes as possible are moved into the overflow pages without letting
62036** the cell size drop below the min embedded payload fraction.
62037**
62038** The min leaf payload fraction is like the min embedded payload fraction
62039** except that it applies to leaf nodes in a LEAFDATA tree. The maximum
62040** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
62041** not specified in the header.
62042**
62043** Each btree pages is divided into three sections: The header, the
62044** cell pointer array, and the cell content area. Page 1 also has a 100-byte
62045** file header that occurs before the page header.
62046**
62047** |----------------|
62048** | file header | 100 bytes. Page 1 only.
62049** |----------------|
62050** | page header | 8 bytes for leaves. 12 bytes for interior nodes
62051** |----------------|
62052** | cell pointer | | 2 bytes per cell. Sorted order.
62053** | array | | Grows downward
62054** | | v
62055** |----------------|
62056** | unallocated |
62057** | space |
62058** |----------------| ^ Grows upwards
62059** | cell content | | Arbitrary order interspersed with freeblocks.
62060** | area | | and free space fragments.
62061** |----------------|
62062**
62063** The page headers looks like this:
62064**
62065** OFFSET SIZE DESCRIPTION
62066** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
62067** 1 2 byte offset to the first freeblock
62068** 3 2 number of cells on this page
62069** 5 2 first byte of the cell content area
62070** 7 1 number of fragmented free bytes
62071** 8 4 Right child (the Ptr(N) value). Omitted on leaves.
62072**
62073** The flags define the format of this btree page. The leaf flag means that
62074** this page has no children. The zerodata flag means that this page carries
62075** only keys and no data. The intkey flag means that the key is an integer
62076** which is stored in the key size entry of the cell header rather than in
62077** the payload area.
62078**
62079** The cell pointer array begins on the first byte after the page header.
62080** The cell pointer array contains zero or more 2-byte numbers which are
62081** offsets from the beginning of the page to the cell content in the cell
62082** content area. The cell pointers occur in sorted order. The system strives
62083** to keep free space after the last cell pointer so that new cells can
62084** be easily added without having to defragment the page.
62085**
62086** Cell content is stored at the very end of the page and grows toward the
62087** beginning of the page.
62088**
62089** Unused space within the cell content area is collected into a linked list of
62090** freeblocks. Each freeblock is at least 4 bytes in size. The byte offset
62091** to the first freeblock is given in the header. Freeblocks occur in
62092** increasing order. Because a freeblock must be at least 4 bytes in size,
62093** any group of 3 or fewer unused bytes in the cell content area cannot
62094** exist on the freeblock chain. A group of 3 or fewer free bytes is called
62095** a fragment. The total number of bytes in all fragments is recorded.
62096** in the page header at offset 7.
62097**
62098** SIZE DESCRIPTION
62099** 2 Byte offset of the next freeblock
62100** 2 Bytes in this freeblock
62101**
62102** Cells are of variable length. Cells are stored in the cell content area at
62103** the end of the page. Pointers to the cells are in the cell pointer array
62104** that immediately follows the page header. Cells is not necessarily
62105** contiguous or in order, but cell pointers are contiguous and in order.
62106**
62107** Cell content makes use of variable length integers. A variable
62108** length integer is 1 to 9 bytes where the lower 7 bits of each
62109** byte are used. The integer consists of all bytes that have bit 8 set and
62110** the first byte with bit 8 clear. The most significant byte of the integer
62111** appears first. A variable-length integer may not be more than 9 bytes long.
62112** As a special case, all 8 bytes of the 9th byte are used as data. This
62113** allows a 64-bit integer to be encoded in 9 bytes.
62114**
62115** 0x00 becomes 0x00000000
62116** 0x7f becomes 0x0000007f
62117** 0x81 0x00 becomes 0x00000080
62118** 0x82 0x00 becomes 0x00000100
62119** 0x80 0x7f becomes 0x0000007f
62120** 0x8a 0x91 0xd1 0xac 0x78 becomes 0x12345678
62121** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081
62122**
62123** Variable length integers are used for rowids and to hold the number of
62124** bytes of key and data in a btree cell.
62125**
62126** The content of a cell looks like this:
62127**
62128** SIZE DESCRIPTION
62129** 4 Page number of the left child. Omitted if leaf flag is set.
62130** var Number of bytes of data. Omitted if the zerodata flag is set.
62131** var Number of bytes of key. Or the key itself if intkey flag is set.
62132** * Payload
62133** 4 First page of the overflow chain. Omitted if no overflow
62134**
62135** Overflow pages form a linked list. Each page except the last is completely
62136** filled with data (pagesize - 4 bytes). The last page can have as little
62137** as 1 byte of data.
62138**
62139** SIZE DESCRIPTION
62140** 4 Page number of next overflow page
62141** * Data
62142**
62143** Freelist pages come in two subtypes: trunk pages and leaf pages. The
62144** file header points to the first in a linked list of trunk page. Each trunk
62145** page points to multiple leaf pages. The content of a leaf page is
62146** unspecified. A trunk page looks like this:
62147**
62148** SIZE DESCRIPTION
62149** 4 Page number of next trunk page
62150** 4 Number of leaf pointers on this page
62151** * zero or more pages numbers of leaves
62152*/
62153/* #include "sqliteInt.h" */
62154
62155
62156/* The following value is the maximum cell size assuming a maximum page
62157** size give above.
62158*/
62159#define MX_CELL_SIZE(pBt) ((int)(pBt->pageSize-8))
62160
62161/* The maximum number of cells on a single page of the database. This
62162** assumes a minimum cell size of 6 bytes (4 bytes for the cell itself
62163** plus 2 bytes for the index to the cell in the page header). Such
62164** small cells will be rare, but they are possible.
62165*/
62166#define MX_CELL(pBt) ((pBt->pageSize-8)/6)
62167
62168/* Forward declarations */
62169typedef struct MemPage MemPage;
62170typedef struct BtLock BtLock;
62171typedef struct CellInfo CellInfo;
62172
62173/*
62174** This is a magic string that appears at the beginning of every
62175** SQLite database in order to identify the file as a real database.
62176**
62177** You can change this value at compile-time by specifying a
62178** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The
62179** header must be exactly 16 bytes including the zero-terminator so
62180** the string itself should be 15 characters long. If you change
62181** the header, then your custom library will not be able to read
62182** databases generated by the standard tools and the standard tools
62183** will not be able to read databases created by your custom library.
62184*/
62185#ifndef SQLITE_FILE_HEADER /* 123456789 123456 */
62186# define SQLITE_FILE_HEADER "SQLite format 3"
62187#endif
62188
62189/*
62190** Page type flags. An ORed combination of these flags appear as the
62191** first byte of on-disk image of every BTree page.
62192*/
62193#define PTF_INTKEY 0x01
62194#define PTF_ZERODATA 0x02
62195#define PTF_LEAFDATA 0x04
62196#define PTF_LEAF 0x08
62197
62198/*
62199** An instance of this object stores information about each a single database
62200** page that has been loaded into memory. The information in this object
62201** is derived from the raw on-disk page content.
62202**
62203** As each database page is loaded into memory, the pager allocats an
62204** instance of this object and zeros the first 8 bytes. (This is the
62205** "extra" information associated with each page of the pager.)
62206**
62207** Access to all fields of this structure is controlled by the mutex
62208** stored in MemPage.pBt->mutex.
62209*/
62210struct MemPage {
62211 u8 isInit; /* True if previously initialized. MUST BE FIRST! */
62212 u8 bBusy; /* Prevent endless loops on corrupt database files */
62213 u8 intKey; /* True if table b-trees. False for index b-trees */
62214 u8 intKeyLeaf; /* True if the leaf of an intKey table */
62215 Pgno pgno; /* Page number for this page */
62216 /* Only the first 8 bytes (above) are zeroed by pager.c when a new page
62217 ** is allocated. All fields that follow must be initialized before use */
62218 u8 leaf; /* True if a leaf page */
62219 u8 hdrOffset; /* 100 for page 1. 0 otherwise */
62220 u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */
62221 u8 max1bytePayload; /* min(maxLocal,127) */
62222 u8 nOverflow; /* Number of overflow cell bodies in aCell[] */
62223 u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
62224 u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */
62225 u16 cellOffset; /* Index in aData of first cell pointer */
62226 u16 nFree; /* Number of free bytes on the page */
62227 u16 nCell; /* Number of cells on this page, local and ovfl */
62228 u16 maskPage; /* Mask for page offset */
62229 u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th
62230 ** non-overflow cell */
62231 u8 *apOvfl[4]; /* Pointers to the body of overflow cells */
62232 BtShared *pBt; /* Pointer to BtShared that this page is part of */
62233 u8 *aData; /* Pointer to disk image of the page data */
62234 u8 *aDataEnd; /* One byte past the end of usable data */
62235 u8 *aCellIdx; /* The cell index area */
62236 u8 *aDataOfst; /* Same as aData for leaves. aData+4 for interior */
62237 DbPage *pDbPage; /* Pager page handle */
62238 u16 (*xCellSize)(MemPage*,u8*); /* cellSizePtr method */
62239 void (*xParseCell)(MemPage*,u8*,CellInfo*); /* btreeParseCell method */
62240};
62241
62242/*
62243** A linked list of the following structures is stored at BtShared.pLock.
62244** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor
62245** is opened on the table with root page BtShared.iTable. Locks are removed
62246** from this list when a transaction is committed or rolled back, or when
62247** a btree handle is closed.
62248*/
62249struct BtLock {
62250 Btree *pBtree; /* Btree handle holding this lock */
62251 Pgno iTable; /* Root page of table */
62252 u8 eLock; /* READ_LOCK or WRITE_LOCK */
62253 BtLock *pNext; /* Next in BtShared.pLock list */
62254};
62255
62256/* Candidate values for BtLock.eLock */
62257#define READ_LOCK 1
62258#define WRITE_LOCK 2
62259
62260/* A Btree handle
62261**
62262** A database connection contains a pointer to an instance of
62263** this object for every database file that it has open. This structure
62264** is opaque to the database connection. The database connection cannot
62265** see the internals of this structure and only deals with pointers to
62266** this structure.
62267**
62268** For some database files, the same underlying database cache might be
62269** shared between multiple connections. In that case, each connection
62270** has it own instance of this object. But each instance of this object
62271** points to the same BtShared object. The database cache and the
62272** schema associated with the database file are all contained within
62273** the BtShared object.
62274**
62275** All fields in this structure are accessed under sqlite3.mutex.
62276** The pBt pointer itself may not be changed while there exists cursors
62277** in the referenced BtShared that point back to this Btree since those
62278** cursors have to go through this Btree to find their BtShared and
62279** they often do so without holding sqlite3.mutex.
62280*/
62281struct Btree {
62282 sqlite3 *db; /* The database connection holding this btree */
62283 BtShared *pBt; /* Sharable content of this btree */
62284 u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
62285 u8 sharable; /* True if we can share pBt with another db */
62286 u8 locked; /* True if db currently has pBt locked */
62287 u8 hasIncrblobCur; /* True if there are one or more Incrblob cursors */
62288 int wantToLock; /* Number of nested calls to sqlite3BtreeEnter() */
62289 int nBackup; /* Number of backup operations reading this btree */
62290 u32 iDataVersion; /* Combines with pBt->pPager->iDataVersion */
62291 Btree *pNext; /* List of other sharable Btrees from the same db */
62292 Btree *pPrev; /* Back pointer of the same list */
62293#ifndef SQLITE_OMIT_SHARED_CACHE
62294 BtLock lock; /* Object used to lock page 1 */
62295#endif
62296};
62297
62298/*
62299** Btree.inTrans may take one of the following values.
62300**
62301** If the shared-data extension is enabled, there may be multiple users
62302** of the Btree structure. At most one of these may open a write transaction,
62303** but any number may have active read transactions.
62304*/
62305#define TRANS_NONE 0
62306#define TRANS_READ 1
62307#define TRANS_WRITE 2
62308
62309/*
62310** An instance of this object represents a single database file.
62311**
62312** A single database file can be in use at the same time by two
62313** or more database connections. When two or more connections are
62314** sharing the same database file, each connection has it own
62315** private Btree object for the file and each of those Btrees points
62316** to this one BtShared object. BtShared.nRef is the number of
62317** connections currently sharing this database file.
62318**
62319** Fields in this structure are accessed under the BtShared.mutex
62320** mutex, except for nRef and pNext which are accessed under the
62321** global SQLITE_MUTEX_STATIC_MASTER mutex. The pPager field
62322** may not be modified once it is initially set as long as nRef>0.
62323** The pSchema field may be set once under BtShared.mutex and
62324** thereafter is unchanged as long as nRef>0.
62325**
62326** isPending:
62327**
62328** If a BtShared client fails to obtain a write-lock on a database
62329** table (because there exists one or more read-locks on the table),
62330** the shared-cache enters 'pending-lock' state and isPending is
62331** set to true.
62332**
62333** The shared-cache leaves the 'pending lock' state when either of
62334** the following occur:
62335**
62336** 1) The current writer (BtShared.pWriter) concludes its transaction, OR
62337** 2) The number of locks held by other connections drops to zero.
62338**
62339** while in the 'pending-lock' state, no connection may start a new
62340** transaction.
62341**
62342** This feature is included to help prevent writer-starvation.
62343*/
62344struct BtShared {
62345 Pager *pPager; /* The page cache */
62346 sqlite3 *db; /* Database connection currently using this Btree */
62347 BtCursor *pCursor; /* A list of all open cursors */
62348 MemPage *pPage1; /* First page of the database */
62349 u8 openFlags; /* Flags to sqlite3BtreeOpen() */
62350#ifndef SQLITE_OMIT_AUTOVACUUM
62351 u8 autoVacuum; /* True if auto-vacuum is enabled */
62352 u8 incrVacuum; /* True if incr-vacuum is enabled */
62353 u8 bDoTruncate; /* True to truncate db on commit */
62354#endif
62355 u8 inTransaction; /* Transaction state */
62356 u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
62357#ifdef SQLITE_HAS_CODEC
62358 u8 optimalReserve; /* Desired amount of reserved space per page */
62359#endif
62360 u16 btsFlags; /* Boolean parameters. See BTS_* macros below */
62361 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
62362 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
62363 u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */
62364 u16 minLeaf; /* Minimum local payload in a LEAFDATA table */
62365 u32 pageSize; /* Total number of bytes on a page */
62366 u32 usableSize; /* Number of usable bytes on each page */
62367 int nTransaction; /* Number of open transactions (read + write) */
62368 u32 nPage; /* Number of pages in the database */
62369 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
62370 void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
62371 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
62372 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */
62373#ifndef SQLITE_OMIT_SHARED_CACHE
62374 int nRef; /* Number of references to this structure */
62375 BtShared *pNext; /* Next on a list of sharable BtShared structs */
62376 BtLock *pLock; /* List of locks held on this shared-btree struct */
62377 Btree *pWriter; /* Btree with currently open write transaction */
62378#endif
62379 u8 *pTmpSpace; /* Temp space sufficient to hold a single cell */
62380};
62381
62382/*
62383** Allowed values for BtShared.btsFlags
62384*/
62385#define BTS_READ_ONLY 0x0001 /* Underlying file is readonly */
62386#define BTS_PAGESIZE_FIXED 0x0002 /* Page size can no longer be changed */
62387#define BTS_SECURE_DELETE 0x0004 /* PRAGMA secure_delete is enabled */
62388#define BTS_OVERWRITE 0x0008 /* Overwrite deleted content with zeros */
62389#define BTS_FAST_SECURE 0x000c /* Combination of the previous two */
62390#define BTS_INITIALLY_EMPTY 0x0010 /* Database was empty at trans start */
62391#define BTS_NO_WAL 0x0020 /* Do not open write-ahead-log files */
62392#define BTS_EXCLUSIVE 0x0040 /* pWriter has an exclusive lock */
62393#define BTS_PENDING 0x0080 /* Waiting for read-locks to clear */
62394
62395/*
62396** An instance of the following structure is used to hold information
62397** about a cell. The parseCellPtr() function fills in this structure
62398** based on information extract from the raw disk page.
62399*/
62400struct CellInfo {
62401 i64 nKey; /* The key for INTKEY tables, or nPayload otherwise */
62402 u8 *pPayload; /* Pointer to the start of payload */
62403 u32 nPayload; /* Bytes of payload */
62404 u16 nLocal; /* Amount of payload held locally, not on overflow */
62405 u16 nSize; /* Size of the cell content on the main b-tree page */
62406};
62407
62408/*
62409** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
62410** this will be declared corrupt. This value is calculated based on a
62411** maximum database size of 2^31 pages a minimum fanout of 2 for a
62412** root-node and 3 for all other internal nodes.
62413**
62414** If a tree that appears to be taller than this is encountered, it is
62415** assumed that the database is corrupt.
62416*/
62417#define BTCURSOR_MAX_DEPTH 20
62418
62419/*
62420** A cursor is a pointer to a particular entry within a particular
62421** b-tree within a database file.
62422**
62423** The entry is identified by its MemPage and the index in
62424** MemPage.aCell[] of the entry.
62425**
62426** A single database file can be shared by two more database connections,
62427** but cursors cannot be shared. Each cursor is associated with a
62428** particular database connection identified BtCursor.pBtree.db.
62429**
62430** Fields in this structure are accessed under the BtShared.mutex
62431** found at self->pBt->mutex.
62432**
62433** skipNext meaning:
62434** eState==SKIPNEXT && skipNext>0: Next sqlite3BtreeNext() is no-op.
62435** eState==SKIPNEXT && skipNext<0: Next sqlite3BtreePrevious() is no-op.
62436** eState==FAULT: Cursor fault with skipNext as error code.
62437*/
62438struct BtCursor {
62439 u8 eState; /* One of the CURSOR_XXX constants (see below) */
62440 u8 curFlags; /* zero or more BTCF_* flags defined below */
62441 u8 curPagerFlags; /* Flags to send to sqlite3PagerGet() */
62442 u8 hints; /* As configured by CursorSetHints() */
62443 int skipNext; /* Prev() is noop if negative. Next() is noop if positive.
62444 ** Error code if eState==CURSOR_FAULT */
62445 Btree *pBtree; /* The Btree to which this cursor belongs */
62446 Pgno *aOverflow; /* Cache of overflow page locations */
62447 void *pKey; /* Saved key that was cursor last known position */
62448 /* All fields above are zeroed when the cursor is allocated. See
62449 ** sqlite3BtreeCursorZero(). Fields that follow must be manually
62450 ** initialized. */
62451#define BTCURSOR_FIRST_UNINIT pBt /* Name of first uninitialized field */
62452 BtShared *pBt; /* The BtShared this cursor points to */
62453 BtCursor *pNext; /* Forms a linked list of all cursors */
62454 CellInfo info; /* A parse of the cell we are pointing at */
62455 i64 nKey; /* Size of pKey, or last integer key */
62456 Pgno pgnoRoot; /* The root page of this tree */
62457 i8 iPage; /* Index of current page in apPage */
62458 u8 curIntKey; /* Value of apPage[0]->intKey */
62459 u16 ix; /* Current index for apPage[iPage] */
62460 u16 aiIdx[BTCURSOR_MAX_DEPTH-1]; /* Current index in apPage[i] */
62461 struct KeyInfo *pKeyInfo; /* Arg passed to comparison function */
62462 MemPage *pPage; /* Current page */
62463 MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */
62464};
62465
62466/*
62467** Legal values for BtCursor.curFlags
62468*/
62469#define BTCF_WriteFlag 0x01 /* True if a write cursor */
62470#define BTCF_ValidNKey 0x02 /* True if info.nKey is valid */
62471#define BTCF_ValidOvfl 0x04 /* True if aOverflow is valid */
62472#define BTCF_AtLast 0x08 /* Cursor is pointing ot the last entry */
62473#define BTCF_Incrblob 0x10 /* True if an incremental I/O handle */
62474#define BTCF_Multiple 0x20 /* Maybe another cursor on the same btree */
62475
62476/*
62477** Potential values for BtCursor.eState.
62478**
62479** CURSOR_INVALID:
62480** Cursor does not point to a valid entry. This can happen (for example)
62481** because the table is empty or because BtreeCursorFirst() has not been
62482** called.
62483**
62484** CURSOR_VALID:
62485** Cursor points to a valid entry. getPayload() etc. may be called.
62486**
62487** CURSOR_SKIPNEXT:
62488** Cursor is valid except that the Cursor.skipNext field is non-zero
62489** indicating that the next sqlite3BtreeNext() or sqlite3BtreePrevious()
62490** operation should be a no-op.
62491**
62492** CURSOR_REQUIRESEEK:
62493** The table that this cursor was opened on still exists, but has been
62494** modified since the cursor was last used. The cursor position is saved
62495** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in
62496** this state, restoreCursorPosition() can be called to attempt to
62497** seek the cursor to the saved position.
62498**
62499** CURSOR_FAULT:
62500** An unrecoverable error (an I/O error or a malloc failure) has occurred
62501** on a different connection that shares the BtShared cache with this
62502** cursor. The error has left the cache in an inconsistent state.
62503** Do nothing else with this cursor. Any attempt to use the cursor
62504** should return the error code stored in BtCursor.skipNext
62505*/
62506#define CURSOR_VALID 0
62507#define CURSOR_INVALID 1
62508#define CURSOR_SKIPNEXT 2
62509#define CURSOR_REQUIRESEEK 3
62510#define CURSOR_FAULT 4
62511
62512/*
62513** The database page the PENDING_BYTE occupies. This page is never used.
62514*/
62515# define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt)
62516
62517/*
62518** These macros define the location of the pointer-map entry for a
62519** database page. The first argument to each is the number of usable
62520** bytes on each page of the database (often 1024). The second is the
62521** page number to look up in the pointer map.
62522**
62523** PTRMAP_PAGENO returns the database page number of the pointer-map
62524** page that stores the required pointer. PTRMAP_PTROFFSET returns
62525** the offset of the requested map entry.
62526**
62527** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
62528** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
62529** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
62530** this test.
62531*/
62532#define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno)
62533#define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
62534#define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno))
62535
62536/*
62537** The pointer map is a lookup table that identifies the parent page for
62538** each child page in the database file. The parent page is the page that
62539** contains a pointer to the child. Every page in the database contains
62540** 0 or 1 parent pages. (In this context 'database page' refers
62541** to any page that is not part of the pointer map itself.) Each pointer map
62542** entry consists of a single byte 'type' and a 4 byte parent page number.
62543** The PTRMAP_XXX identifiers below are the valid types.
62544**
62545** The purpose of the pointer map is to facility moving pages from one
62546** position in the file to another as part of autovacuum. When a page
62547** is moved, the pointer in its parent must be updated to point to the
62548** new location. The pointer map is used to locate the parent page quickly.
62549**
62550** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
62551** used in this case.
62552**
62553** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
62554** is not used in this case.
62555**
62556** PTRMAP_OVERFLOW1: The database page is the first page in a list of
62557** overflow pages. The page number identifies the page that
62558** contains the cell with a pointer to this overflow page.
62559**
62560** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
62561** overflow pages. The page-number identifies the previous
62562** page in the overflow page list.
62563**
62564** PTRMAP_BTREE: The database page is a non-root btree page. The page number
62565** identifies the parent page in the btree.
62566*/
62567#define PTRMAP_ROOTPAGE 1
62568#define PTRMAP_FREEPAGE 2
62569#define PTRMAP_OVERFLOW1 3
62570#define PTRMAP_OVERFLOW2 4
62571#define PTRMAP_BTREE 5
62572
62573/* A bunch of assert() statements to check the transaction state variables
62574** of handle p (type Btree*) are internally consistent.
62575*/
62576#define btreeIntegrity(p) \
62577 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
62578 assert( p->pBt->inTransaction>=p->inTrans );
62579
62580
62581/*
62582** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
62583** if the database supports auto-vacuum or not. Because it is used
62584** within an expression that is an argument to another macro
62585** (sqliteMallocRaw), it is not possible to use conditional compilation.
62586** So, this macro is defined instead.
62587*/
62588#ifndef SQLITE_OMIT_AUTOVACUUM
62589#define ISAUTOVACUUM (pBt->autoVacuum)
62590#else
62591#define ISAUTOVACUUM 0
62592#endif
62593
62594
62595/*
62596** This structure is passed around through all the sanity checking routines
62597** in order to keep track of some global state information.
62598**
62599** The aRef[] array is allocated so that there is 1 bit for each page in
62600** the database. As the integrity-check proceeds, for each page used in
62601** the database the corresponding bit is set. This allows integrity-check to
62602** detect pages that are used twice and orphaned pages (both of which
62603** indicate corruption).
62604*/
62605typedef struct IntegrityCk IntegrityCk;
62606struct IntegrityCk {
62607 BtShared *pBt; /* The tree being checked out */
62608 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
62609 u8 *aPgRef; /* 1 bit per page in the db (see above) */
62610 Pgno nPage; /* Number of pages in the database */
62611 int mxErr; /* Stop accumulating errors when this reaches zero */
62612 int nErr; /* Number of messages written to zErrMsg so far */
62613 int mallocFailed; /* A memory allocation error has occurred */
62614 const char *zPfx; /* Error message prefix */
62615 int v1, v2; /* Values for up to two %d fields in zPfx */
62616 StrAccum errMsg; /* Accumulate the error message text here */
62617 u32 *heap; /* Min-heap used for analyzing cell coverage */
62618};
62619
62620/*
62621** Routines to read or write a two- and four-byte big-endian integer values.
62622*/
62623#define get2byte(x) ((x)[0]<<8 | (x)[1])
62624#define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
62625#define get4byte sqlite3Get4byte
62626#define put4byte sqlite3Put4byte
62627
62628/*
62629** get2byteAligned(), unlike get2byte(), requires that its argument point to a
62630** two-byte aligned address. get2bytea() is only used for accessing the
62631** cell addresses in a btree header.
62632*/
62633#if SQLITE_BYTEORDER==4321
62634# define get2byteAligned(x) (*(u16*)(x))
62635#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4008000
62636# define get2byteAligned(x) __builtin_bswap16(*(u16*)(x))
62637#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
62638# define get2byteAligned(x) _byteswap_ushort(*(u16*)(x))
62639#else
62640# define get2byteAligned(x) ((x)[0]<<8 | (x)[1])
62641#endif
62642
62643/************** End of btreeInt.h ********************************************/
62644/************** Continuing where we left off in btmutex.c ********************/
62645#ifndef SQLITE_OMIT_SHARED_CACHE
62646#if SQLITE_THREADSAFE
62647
62648/*
62649** Obtain the BtShared mutex associated with B-Tree handle p. Also,
62650** set BtShared.db to the database handle associated with p and the
62651** p->locked boolean to true.
62652*/
62653static void lockBtreeMutex(Btree *p){
62654 assert( p->locked==0 );
62655 assert( sqlite3_mutex_notheld(p->pBt->mutex) );
62656 assert( sqlite3_mutex_held(p->db->mutex) );
62657
62658 sqlite3_mutex_enter(p->pBt->mutex);
62659 p->pBt->db = p->db;
62660 p->locked = 1;
62661}
62662
62663/*
62664** Release the BtShared mutex associated with B-Tree handle p and
62665** clear the p->locked boolean.
62666*/
62667static void SQLITE_NOINLINE unlockBtreeMutex(Btree *p){
62668 BtShared *pBt = p->pBt;
62669 assert( p->locked==1 );
62670 assert( sqlite3_mutex_held(pBt->mutex) );
62671 assert( sqlite3_mutex_held(p->db->mutex) );
62672 assert( p->db==pBt->db );
62673
62674 sqlite3_mutex_leave(pBt->mutex);
62675 p->locked = 0;
62676}
62677
62678/* Forward reference */
62679static void SQLITE_NOINLINE btreeLockCarefully(Btree *p);
62680
62681/*
62682** Enter a mutex on the given BTree object.
62683**
62684** If the object is not sharable, then no mutex is ever required
62685** and this routine is a no-op. The underlying mutex is non-recursive.
62686** But we keep a reference count in Btree.wantToLock so the behavior
62687** of this interface is recursive.
62688**
62689** To avoid deadlocks, multiple Btrees are locked in the same order
62690** by all database connections. The p->pNext is a list of other
62691** Btrees belonging to the same database connection as the p Btree
62692** which need to be locked after p. If we cannot get a lock on
62693** p, then first unlock all of the others on p->pNext, then wait
62694** for the lock to become available on p, then relock all of the
62695** subsequent Btrees that desire a lock.
62696*/
62697SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
62698 /* Some basic sanity checking on the Btree. The list of Btrees
62699 ** connected by pNext and pPrev should be in sorted order by
62700 ** Btree.pBt value. All elements of the list should belong to
62701 ** the same connection. Only shared Btrees are on the list. */
62702 assert( p->pNext==0 || p->pNext->pBt>p->pBt );
62703 assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
62704 assert( p->pNext==0 || p->pNext->db==p->db );
62705 assert( p->pPrev==0 || p->pPrev->db==p->db );
62706 assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
62707
62708 /* Check for locking consistency */
62709 assert( !p->locked || p->wantToLock>0 );
62710 assert( p->sharable || p->wantToLock==0 );
62711
62712 /* We should already hold a lock on the database connection */
62713 assert( sqlite3_mutex_held(p->db->mutex) );
62714
62715 /* Unless the database is sharable and unlocked, then BtShared.db
62716 ** should already be set correctly. */
62717 assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
62718
62719 if( !p->sharable ) return;
62720 p->wantToLock++;
62721 if( p->locked ) return;
62722 btreeLockCarefully(p);
62723}
62724
62725/* This is a helper function for sqlite3BtreeLock(). By moving
62726** complex, but seldom used logic, out of sqlite3BtreeLock() and
62727** into this routine, we avoid unnecessary stack pointer changes
62728** and thus help the sqlite3BtreeLock() routine to run much faster
62729** in the common case.
62730*/
62731static void SQLITE_NOINLINE btreeLockCarefully(Btree *p){
62732 Btree *pLater;
62733
62734 /* In most cases, we should be able to acquire the lock we
62735 ** want without having to go through the ascending lock
62736 ** procedure that follows. Just be sure not to block.
62737 */
62738 if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
62739 p->pBt->db = p->db;
62740 p->locked = 1;
62741 return;
62742 }
62743
62744 /* To avoid deadlock, first release all locks with a larger
62745 ** BtShared address. Then acquire our lock. Then reacquire
62746 ** the other BtShared locks that we used to hold in ascending
62747 ** order.
62748 */
62749 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
62750 assert( pLater->sharable );
62751 assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
62752 assert( !pLater->locked || pLater->wantToLock>0 );
62753 if( pLater->locked ){
62754 unlockBtreeMutex(pLater);
62755 }
62756 }
62757 lockBtreeMutex(p);
62758 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
62759 if( pLater->wantToLock ){
62760 lockBtreeMutex(pLater);
62761 }
62762 }
62763}
62764
62765
62766/*
62767** Exit the recursive mutex on a Btree.
62768*/
62769SQLITE_PRIVATE void sqlite3BtreeLeave(Btree *p){
62770 assert( sqlite3_mutex_held(p->db->mutex) );
62771 if( p->sharable ){
62772 assert( p->wantToLock>0 );
62773 p->wantToLock--;
62774 if( p->wantToLock==0 ){
62775 unlockBtreeMutex(p);
62776 }
62777 }
62778}
62779
62780#ifndef NDEBUG
62781/*
62782** Return true if the BtShared mutex is held on the btree, or if the
62783** B-Tree is not marked as sharable.
62784**
62785** This routine is used only from within assert() statements.
62786*/
62787SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree *p){
62788 assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
62789 assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
62790 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
62791 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
62792
62793 return (p->sharable==0 || p->locked);
62794}
62795#endif
62796
62797
62798/*
62799** Enter the mutex on every Btree associated with a database
62800** connection. This is needed (for example) prior to parsing
62801** a statement since we will be comparing table and column names
62802** against all schemas and we do not want those schemas being
62803** reset out from under us.
62804**
62805** There is a corresponding leave-all procedures.
62806**
62807** Enter the mutexes in accending order by BtShared pointer address
62808** to avoid the possibility of deadlock when two threads with
62809** two or more btrees in common both try to lock all their btrees
62810** at the same instant.
62811*/
62812static void SQLITE_NOINLINE btreeEnterAll(sqlite3 *db){
62813 int i;
62814 int skipOk = 1;
62815 Btree *p;
62816 assert( sqlite3_mutex_held(db->mutex) );
62817 for(i=0; i<db->nDb; i++){
62818 p = db->aDb[i].pBt;
62819 if( p && p->sharable ){
62820 sqlite3BtreeEnter(p);
62821 skipOk = 0;
62822 }
62823 }
62824 db->noSharedCache = skipOk;
62825}
62826SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
62827 if( db->noSharedCache==0 ) btreeEnterAll(db);
62828}
62829static void SQLITE_NOINLINE btreeLeaveAll(sqlite3 *db){
62830 int i;
62831 Btree *p;
62832 assert( sqlite3_mutex_held(db->mutex) );
62833 for(i=0; i<db->nDb; i++){
62834 p = db->aDb[i].pBt;
62835 if( p ) sqlite3BtreeLeave(p);
62836 }
62837}
62838SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
62839 if( db->noSharedCache==0 ) btreeLeaveAll(db);
62840}
62841
62842#ifndef NDEBUG
62843/*
62844** Return true if the current thread holds the database connection
62845** mutex and all required BtShared mutexes.
62846**
62847** This routine is used inside assert() statements only.
62848*/
62849SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
62850 int i;
62851 if( !sqlite3_mutex_held(db->mutex) ){
62852 return 0;
62853 }
62854 for(i=0; i<db->nDb; i++){
62855 Btree *p;
62856 p = db->aDb[i].pBt;
62857 if( p && p->sharable &&
62858 (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
62859 return 0;
62860 }
62861 }
62862 return 1;
62863}
62864#endif /* NDEBUG */
62865
62866#ifndef NDEBUG
62867/*
62868** Return true if the correct mutexes are held for accessing the
62869** db->aDb[iDb].pSchema structure. The mutexes required for schema
62870** access are:
62871**
62872** (1) The mutex on db
62873** (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
62874**
62875** If pSchema is not NULL, then iDb is computed from pSchema and
62876** db using sqlite3SchemaToIndex().
62877*/
62878SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
62879 Btree *p;
62880 assert( db!=0 );
62881 if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
62882 assert( iDb>=0 && iDb<db->nDb );
62883 if( !sqlite3_mutex_held(db->mutex) ) return 0;
62884 if( iDb==1 ) return 1;
62885 p = db->aDb[iDb].pBt;
62886 assert( p!=0 );
62887 return p->sharable==0 || p->locked==1;
62888}
62889#endif /* NDEBUG */
62890
62891#else /* SQLITE_THREADSAFE>0 above. SQLITE_THREADSAFE==0 below */
62892/*
62893** The following are special cases for mutex enter routines for use
62894** in single threaded applications that use shared cache. Except for
62895** these two routines, all mutex operations are no-ops in that case and
62896** are null #defines in btree.h.
62897**
62898** If shared cache is disabled, then all btree mutex routines, including
62899** the ones below, are no-ops and are null #defines in btree.h.
62900*/
62901
62902SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
62903 p->pBt->db = p->db;
62904}
62905SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
62906 int i;
62907 for(i=0; i<db->nDb; i++){
62908 Btree *p = db->aDb[i].pBt;
62909 if( p ){
62910 p->pBt->db = p->db;
62911 }
62912 }
62913}
62914#endif /* if SQLITE_THREADSAFE */
62915
62916#ifndef SQLITE_OMIT_INCRBLOB
62917/*
62918** Enter a mutex on a Btree given a cursor owned by that Btree.
62919**
62920** These entry points are used by incremental I/O only. Enter() is required
62921** any time OMIT_SHARED_CACHE is not defined, regardless of whether or not
62922** the build is threadsafe. Leave() is only required by threadsafe builds.
62923*/
62924SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){
62925 sqlite3BtreeEnter(pCur->pBtree);
62926}
62927# if SQLITE_THREADSAFE
62928SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){
62929 sqlite3BtreeLeave(pCur->pBtree);
62930}
62931# endif
62932#endif /* ifndef SQLITE_OMIT_INCRBLOB */
62933
62934#endif /* ifndef SQLITE_OMIT_SHARED_CACHE */
62935
62936/************** End of btmutex.c *********************************************/
62937/************** Begin file btree.c *******************************************/
62938/*
62939** 2004 April 6
62940**
62941** The author disclaims copyright to this source code. In place of
62942** a legal notice, here is a blessing:
62943**
62944** May you do good and not evil.
62945** May you find forgiveness for yourself and forgive others.
62946** May you share freely, never taking more than you give.
62947**
62948*************************************************************************
62949** This file implements an external (disk-based) database using BTrees.
62950** See the header comment on "btreeInt.h" for additional information.
62951** Including a description of file format and an overview of operation.
62952*/
62953/* #include "btreeInt.h" */
62954
62955/*
62956** The header string that appears at the beginning of every
62957** SQLite database.
62958*/
62959static const char zMagicHeader[] = SQLITE_FILE_HEADER;
62960
62961/*
62962** Set this global variable to 1 to enable tracing using the TRACE
62963** macro.
62964*/
62965#if 0
62966int sqlite3BtreeTrace=1; /* True to enable tracing */
62967# define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);}
62968#else
62969# define TRACE(X)
62970#endif
62971
62972/*
62973** Extract a 2-byte big-endian integer from an array of unsigned bytes.
62974** But if the value is zero, make it 65536.
62975**
62976** This routine is used to extract the "offset to cell content area" value
62977** from the header of a btree page. If the page size is 65536 and the page
62978** is empty, the offset should be 65536, but the 2-byte value stores zero.
62979** This routine makes the necessary adjustment to 65536.
62980*/
62981#define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1)
62982
62983/*
62984** Values passed as the 5th argument to allocateBtreePage()
62985*/
62986#define BTALLOC_ANY 0 /* Allocate any page */
62987#define BTALLOC_EXACT 1 /* Allocate exact page if possible */
62988#define BTALLOC_LE 2 /* Allocate any page <= the parameter */
62989
62990/*
62991** Macro IfNotOmitAV(x) returns (x) if SQLITE_OMIT_AUTOVACUUM is not
62992** defined, or 0 if it is. For example:
62993**
62994** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
62995*/
62996#ifndef SQLITE_OMIT_AUTOVACUUM
62997#define IfNotOmitAV(expr) (expr)
62998#else
62999#define IfNotOmitAV(expr) 0
63000#endif
63001
63002#ifndef SQLITE_OMIT_SHARED_CACHE
63003/*
63004** A list of BtShared objects that are eligible for participation
63005** in shared cache. This variable has file scope during normal builds,
63006** but the test harness needs to access it so we make it global for
63007** test builds.
63008**
63009** Access to this variable is protected by SQLITE_MUTEX_STATIC_MASTER.
63010*/
63011#ifdef SQLITE_TEST
63012SQLITE_PRIVATE BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
63013#else
63014static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
63015#endif
63016#endif /* SQLITE_OMIT_SHARED_CACHE */
63017
63018#ifndef SQLITE_OMIT_SHARED_CACHE
63019/*
63020** Enable or disable the shared pager and schema features.
63021**
63022** This routine has no effect on existing database connections.
63023** The shared cache setting effects only future calls to
63024** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
63025*/
63026SQLITE_API int sqlite3_enable_shared_cache(int enable){
63027 sqlite3GlobalConfig.sharedCacheEnabled = enable;
63028 return SQLITE_OK;
63029}
63030#endif
63031
63032
63033
63034#ifdef SQLITE_OMIT_SHARED_CACHE
63035 /*
63036 ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
63037 ** and clearAllSharedCacheTableLocks()
63038 ** manipulate entries in the BtShared.pLock linked list used to store
63039 ** shared-cache table level locks. If the library is compiled with the
63040 ** shared-cache feature disabled, then there is only ever one user
63041 ** of each BtShared structure and so this locking is not necessary.
63042 ** So define the lock related functions as no-ops.
63043 */
63044 #define querySharedCacheTableLock(a,b,c) SQLITE_OK
63045 #define setSharedCacheTableLock(a,b,c) SQLITE_OK
63046 #define clearAllSharedCacheTableLocks(a)
63047 #define downgradeAllSharedCacheTableLocks(a)
63048 #define hasSharedCacheTableLock(a,b,c,d) 1
63049 #define hasReadConflicts(a, b) 0
63050#endif
63051
63052/*
63053** Implementation of the SQLITE_CORRUPT_PAGE() macro. Takes a single
63054** (MemPage*) as an argument. The (MemPage*) must not be NULL.
63055**
63056** If SQLITE_DEBUG is not defined, then this macro is equivalent to
63057** SQLITE_CORRUPT_BKPT. Or, if SQLITE_DEBUG is set, then the log message
63058** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
63059** with the page number and filename associated with the (MemPage*).
63060*/
63061#ifdef SQLITE_DEBUG
63062int corruptPageError(int lineno, MemPage *p){
63063 char *zMsg;
63064 sqlite3BeginBenignMalloc();
63065 zMsg = sqlite3_mprintf("database corruption page %d of %s",
63066 (int)p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
63067 );
63068 sqlite3EndBenignMalloc();
63069 if( zMsg ){
63070 sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg);
63071 }
63072 sqlite3_free(zMsg);
63073 return SQLITE_CORRUPT_BKPT;
63074}
63075# define SQLITE_CORRUPT_PAGE(pMemPage) corruptPageError(__LINE__, pMemPage)
63076#else
63077# define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno)
63078#endif
63079
63080#ifndef SQLITE_OMIT_SHARED_CACHE
63081
63082#ifdef SQLITE_DEBUG
63083/*
63084**** This function is only used as part of an assert() statement. ***
63085**
63086** Check to see if pBtree holds the required locks to read or write to the
63087** table with root page iRoot. Return 1 if it does and 0 if not.
63088**
63089** For example, when writing to a table with root-page iRoot via
63090** Btree connection pBtree:
63091**
63092** assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
63093**
63094** When writing to an index that resides in a sharable database, the
63095** caller should have first obtained a lock specifying the root page of
63096** the corresponding table. This makes things a bit more complicated,
63097** as this module treats each table as a separate structure. To determine
63098** the table corresponding to the index being written, this
63099** function has to search through the database schema.
63100**
63101** Instead of a lock on the table/index rooted at page iRoot, the caller may
63102** hold a write-lock on the schema table (root page 1). This is also
63103** acceptable.
63104*/
63105static int hasSharedCacheTableLock(
63106 Btree *pBtree, /* Handle that must hold lock */
63107 Pgno iRoot, /* Root page of b-tree */
63108 int isIndex, /* True if iRoot is the root of an index b-tree */
63109 int eLockType /* Required lock type (READ_LOCK or WRITE_LOCK) */
63110){
63111 Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
63112 Pgno iTab = 0;
63113 BtLock *pLock;
63114
63115 /* If this database is not shareable, or if the client is reading
63116 ** and has the read-uncommitted flag set, then no lock is required.
63117 ** Return true immediately.
63118 */
63119 if( (pBtree->sharable==0)
63120 || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommit))
63121 ){
63122 return 1;
63123 }
63124
63125 /* If the client is reading or writing an index and the schema is
63126 ** not loaded, then it is too difficult to actually check to see if
63127 ** the correct locks are held. So do not bother - just return true.
63128 ** This case does not come up very often anyhow.
63129 */
63130 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
63131 return 1;
63132 }
63133
63134 /* Figure out the root-page that the lock should be held on. For table
63135 ** b-trees, this is just the root page of the b-tree being read or
63136 ** written. For index b-trees, it is the root page of the associated
63137 ** table. */
63138 if( isIndex ){
63139 HashElem *p;
63140 for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
63141 Index *pIdx = (Index *)sqliteHashData(p);
63142 if( pIdx->tnum==(int)iRoot ){
63143 if( iTab ){
63144 /* Two or more indexes share the same root page. There must
63145 ** be imposter tables. So just return true. The assert is not
63146 ** useful in that case. */
63147 return 1;
63148 }
63149 iTab = pIdx->pTable->tnum;
63150 }
63151 }
63152 }else{
63153 iTab = iRoot;
63154 }
63155
63156 /* Search for the required lock. Either a write-lock on root-page iTab, a
63157 ** write-lock on the schema table, or (if the client is reading) a
63158 ** read-lock on iTab will suffice. Return 1 if any of these are found. */
63159 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
63160 if( pLock->pBtree==pBtree
63161 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
63162 && pLock->eLock>=eLockType
63163 ){
63164 return 1;
63165 }
63166 }
63167
63168 /* Failed to find the required lock. */
63169 return 0;
63170}
63171#endif /* SQLITE_DEBUG */
63172
63173#ifdef SQLITE_DEBUG
63174/*
63175**** This function may be used as part of assert() statements only. ****
63176**
63177** Return true if it would be illegal for pBtree to write into the
63178** table or index rooted at iRoot because other shared connections are
63179** simultaneously reading that same table or index.
63180**
63181** It is illegal for pBtree to write if some other Btree object that
63182** shares the same BtShared object is currently reading or writing
63183** the iRoot table. Except, if the other Btree object has the
63184** read-uncommitted flag set, then it is OK for the other object to
63185** have a read cursor.
63186**
63187** For example, before writing to any part of the table or index
63188** rooted at page iRoot, one should call:
63189**
63190** assert( !hasReadConflicts(pBtree, iRoot) );
63191*/
63192static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
63193 BtCursor *p;
63194 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
63195 if( p->pgnoRoot==iRoot
63196 && p->pBtree!=pBtree
63197 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit)
63198 ){
63199 return 1;
63200 }
63201 }
63202 return 0;
63203}
63204#endif /* #ifdef SQLITE_DEBUG */
63205
63206/*
63207** Query to see if Btree handle p may obtain a lock of type eLock
63208** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
63209** SQLITE_OK if the lock may be obtained (by calling
63210** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
63211*/
63212static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
63213 BtShared *pBt = p->pBt;
63214 BtLock *pIter;
63215
63216 assert( sqlite3BtreeHoldsMutex(p) );
63217 assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
63218 assert( p->db!=0 );
63219 assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 );
63220
63221 /* If requesting a write-lock, then the Btree must have an open write
63222 ** transaction on this file. And, obviously, for this to be so there
63223 ** must be an open write transaction on the file itself.
63224 */
63225 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
63226 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
63227
63228 /* This routine is a no-op if the shared-cache is not enabled */
63229 if( !p->sharable ){
63230 return SQLITE_OK;
63231 }
63232
63233 /* If some other connection is holding an exclusive lock, the
63234 ** requested lock may not be obtained.
63235 */
63236 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
63237 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
63238 return SQLITE_LOCKED_SHAREDCACHE;
63239 }
63240
63241 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
63242 /* The condition (pIter->eLock!=eLock) in the following if(...)
63243 ** statement is a simplification of:
63244 **
63245 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
63246 **
63247 ** since we know that if eLock==WRITE_LOCK, then no other connection
63248 ** may hold a WRITE_LOCK on any table in this file (since there can
63249 ** only be a single writer).
63250 */
63251 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
63252 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
63253 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
63254 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
63255 if( eLock==WRITE_LOCK ){
63256 assert( p==pBt->pWriter );
63257 pBt->btsFlags |= BTS_PENDING;
63258 }
63259 return SQLITE_LOCKED_SHAREDCACHE;
63260 }
63261 }
63262 return SQLITE_OK;
63263}
63264#endif /* !SQLITE_OMIT_SHARED_CACHE */
63265
63266#ifndef SQLITE_OMIT_SHARED_CACHE
63267/*
63268** Add a lock on the table with root-page iTable to the shared-btree used
63269** by Btree handle p. Parameter eLock must be either READ_LOCK or
63270** WRITE_LOCK.
63271**
63272** This function assumes the following:
63273**
63274** (a) The specified Btree object p is connected to a sharable
63275** database (one with the BtShared.sharable flag set), and
63276**
63277** (b) No other Btree objects hold a lock that conflicts
63278** with the requested lock (i.e. querySharedCacheTableLock() has
63279** already been called and returned SQLITE_OK).
63280**
63281** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM
63282** is returned if a malloc attempt fails.
63283*/
63284static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
63285 BtShared *pBt = p->pBt;
63286 BtLock *pLock = 0;
63287 BtLock *pIter;
63288
63289 assert( sqlite3BtreeHoldsMutex(p) );
63290 assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
63291 assert( p->db!=0 );
63292
63293 /* A connection with the read-uncommitted flag set will never try to
63294 ** obtain a read-lock using this function. The only read-lock obtained
63295 ** by a connection in read-uncommitted mode is on the sqlite_master
63296 ** table, and that lock is obtained in BtreeBeginTrans(). */
63297 assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK );
63298
63299 /* This function should only be called on a sharable b-tree after it
63300 ** has been determined that no other b-tree holds a conflicting lock. */
63301 assert( p->sharable );
63302 assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) );
63303
63304 /* First search the list for an existing lock on this table. */
63305 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
63306 if( pIter->iTable==iTable && pIter->pBtree==p ){
63307 pLock = pIter;
63308 break;
63309 }
63310 }
63311
63312 /* If the above search did not find a BtLock struct associating Btree p
63313 ** with table iTable, allocate one and link it into the list.
63314 */
63315 if( !pLock ){
63316 pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
63317 if( !pLock ){
63318 return SQLITE_NOMEM_BKPT;
63319 }
63320 pLock->iTable = iTable;
63321 pLock->pBtree = p;
63322 pLock->pNext = pBt->pLock;
63323 pBt->pLock = pLock;
63324 }
63325
63326 /* Set the BtLock.eLock variable to the maximum of the current lock
63327 ** and the requested lock. This means if a write-lock was already held
63328 ** and a read-lock requested, we don't incorrectly downgrade the lock.
63329 */
63330 assert( WRITE_LOCK>READ_LOCK );
63331 if( eLock>pLock->eLock ){
63332 pLock->eLock = eLock;
63333 }
63334
63335 return SQLITE_OK;
63336}
63337#endif /* !SQLITE_OMIT_SHARED_CACHE */
63338
63339#ifndef SQLITE_OMIT_SHARED_CACHE
63340/*
63341** Release all the table locks (locks obtained via calls to
63342** the setSharedCacheTableLock() procedure) held by Btree object p.
63343**
63344** This function assumes that Btree p has an open read or write
63345** transaction. If it does not, then the BTS_PENDING flag
63346** may be incorrectly cleared.
63347*/
63348static void clearAllSharedCacheTableLocks(Btree *p){
63349 BtShared *pBt = p->pBt;
63350 BtLock **ppIter = &pBt->pLock;
63351
63352 assert( sqlite3BtreeHoldsMutex(p) );
63353 assert( p->sharable || 0==*ppIter );
63354 assert( p->inTrans>0 );
63355
63356 while( *ppIter ){
63357 BtLock *pLock = *ppIter;
63358 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
63359 assert( pLock->pBtree->inTrans>=pLock->eLock );
63360 if( pLock->pBtree==p ){
63361 *ppIter = pLock->pNext;
63362 assert( pLock->iTable!=1 || pLock==&p->lock );
63363 if( pLock->iTable!=1 ){
63364 sqlite3_free(pLock);
63365 }
63366 }else{
63367 ppIter = &pLock->pNext;
63368 }
63369 }
63370
63371 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
63372 if( pBt->pWriter==p ){
63373 pBt->pWriter = 0;
63374 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
63375 }else if( pBt->nTransaction==2 ){
63376 /* This function is called when Btree p is concluding its
63377 ** transaction. If there currently exists a writer, and p is not
63378 ** that writer, then the number of locks held by connections other
63379 ** than the writer must be about to drop to zero. In this case
63380 ** set the BTS_PENDING flag to 0.
63381 **
63382 ** If there is not currently a writer, then BTS_PENDING must
63383 ** be zero already. So this next line is harmless in that case.
63384 */
63385 pBt->btsFlags &= ~BTS_PENDING;
63386 }
63387}
63388
63389/*
63390** This function changes all write-locks held by Btree p into read-locks.
63391*/
63392static void downgradeAllSharedCacheTableLocks(Btree *p){
63393 BtShared *pBt = p->pBt;
63394 if( pBt->pWriter==p ){
63395 BtLock *pLock;
63396 pBt->pWriter = 0;
63397 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
63398 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
63399 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
63400 pLock->eLock = READ_LOCK;
63401 }
63402 }
63403}
63404
63405#endif /* SQLITE_OMIT_SHARED_CACHE */
63406
63407static void releasePage(MemPage *pPage); /* Forward reference */
63408static void releasePageOne(MemPage *pPage); /* Forward reference */
63409static void releasePageNotNull(MemPage *pPage); /* Forward reference */
63410
63411/*
63412***** This routine is used inside of assert() only ****
63413**
63414** Verify that the cursor holds the mutex on its BtShared
63415*/
63416#ifdef SQLITE_DEBUG
63417static int cursorHoldsMutex(BtCursor *p){
63418 return sqlite3_mutex_held(p->pBt->mutex);
63419}
63420
63421/* Verify that the cursor and the BtShared agree about what is the current
63422** database connetion. This is important in shared-cache mode. If the database
63423** connection pointers get out-of-sync, it is possible for routines like
63424** btreeInitPage() to reference an stale connection pointer that references a
63425** a connection that has already closed. This routine is used inside assert()
63426** statements only and for the purpose of double-checking that the btree code
63427** does keep the database connection pointers up-to-date.
63428*/
63429static int cursorOwnsBtShared(BtCursor *p){
63430 assert( cursorHoldsMutex(p) );
63431 return (p->pBtree->db==p->pBt->db);
63432}
63433#endif
63434
63435/*
63436** Invalidate the overflow cache of the cursor passed as the first argument.
63437** on the shared btree structure pBt.
63438*/
63439#define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
63440
63441/*
63442** Invalidate the overflow page-list cache for all cursors opened
63443** on the shared btree structure pBt.
63444*/
63445static void invalidateAllOverflowCache(BtShared *pBt){
63446 BtCursor *p;
63447 assert( sqlite3_mutex_held(pBt->mutex) );
63448 for(p=pBt->pCursor; p; p=p->pNext){
63449 invalidateOverflowCache(p);
63450 }
63451}
63452
63453#ifndef SQLITE_OMIT_INCRBLOB
63454/*
63455** This function is called before modifying the contents of a table
63456** to invalidate any incrblob cursors that are open on the
63457** row or one of the rows being modified.
63458**
63459** If argument isClearTable is true, then the entire contents of the
63460** table is about to be deleted. In this case invalidate all incrblob
63461** cursors open on any row within the table with root-page pgnoRoot.
63462**
63463** Otherwise, if argument isClearTable is false, then the row with
63464** rowid iRow is being replaced or deleted. In this case invalidate
63465** only those incrblob cursors open on that specific row.
63466*/
63467static void invalidateIncrblobCursors(
63468 Btree *pBtree, /* The database file to check */
63469 Pgno pgnoRoot, /* The table that might be changing */
63470 i64 iRow, /* The rowid that might be changing */
63471 int isClearTable /* True if all rows are being deleted */
63472){
63473 BtCursor *p;
63474 if( pBtree->hasIncrblobCur==0 ) return;
63475 assert( sqlite3BtreeHoldsMutex(pBtree) );
63476 pBtree->hasIncrblobCur = 0;
63477 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
63478 if( (p->curFlags & BTCF_Incrblob)!=0 ){
63479 pBtree->hasIncrblobCur = 1;
63480 if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
63481 p->eState = CURSOR_INVALID;
63482 }
63483 }
63484 }
63485}
63486
63487#else
63488 /* Stub function when INCRBLOB is omitted */
63489 #define invalidateIncrblobCursors(w,x,y,z)
63490#endif /* SQLITE_OMIT_INCRBLOB */
63491
63492/*
63493** Set bit pgno of the BtShared.pHasContent bitvec. This is called
63494** when a page that previously contained data becomes a free-list leaf
63495** page.
63496**
63497** The BtShared.pHasContent bitvec exists to work around an obscure
63498** bug caused by the interaction of two useful IO optimizations surrounding
63499** free-list leaf pages:
63500**
63501** 1) When all data is deleted from a page and the page becomes
63502** a free-list leaf page, the page is not written to the database
63503** (as free-list leaf pages contain no meaningful data). Sometimes
63504** such a page is not even journalled (as it will not be modified,
63505** why bother journalling it?).
63506**
63507** 2) When a free-list leaf page is reused, its content is not read
63508** from the database or written to the journal file (why should it
63509** be, if it is not at all meaningful?).
63510**
63511** By themselves, these optimizations work fine and provide a handy
63512** performance boost to bulk delete or insert operations. However, if
63513** a page is moved to the free-list and then reused within the same
63514** transaction, a problem comes up. If the page is not journalled when
63515** it is moved to the free-list and it is also not journalled when it
63516** is extracted from the free-list and reused, then the original data
63517** may be lost. In the event of a rollback, it may not be possible
63518** to restore the database to its original configuration.
63519**
63520** The solution is the BtShared.pHasContent bitvec. Whenever a page is
63521** moved to become a free-list leaf page, the corresponding bit is
63522** set in the bitvec. Whenever a leaf page is extracted from the free-list,
63523** optimization 2 above is omitted if the corresponding bit is already
63524** set in BtShared.pHasContent. The contents of the bitvec are cleared
63525** at the end of every transaction.
63526*/
63527static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
63528 int rc = SQLITE_OK;
63529 if( !pBt->pHasContent ){
63530 assert( pgno<=pBt->nPage );
63531 pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
63532 if( !pBt->pHasContent ){
63533 rc = SQLITE_NOMEM_BKPT;
63534 }
63535 }
63536 if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
63537 rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
63538 }
63539 return rc;
63540}
63541
63542/*
63543** Query the BtShared.pHasContent vector.
63544**
63545** This function is called when a free-list leaf page is removed from the
63546** free-list for reuse. It returns false if it is safe to retrieve the
63547** page from the pager layer with the 'no-content' flag set. True otherwise.
63548*/
63549static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
63550 Bitvec *p = pBt->pHasContent;
63551 return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno)));
63552}
63553
63554/*
63555** Clear (destroy) the BtShared.pHasContent bitvec. This should be
63556** invoked at the conclusion of each write-transaction.
63557*/
63558static void btreeClearHasContent(BtShared *pBt){
63559 sqlite3BitvecDestroy(pBt->pHasContent);
63560 pBt->pHasContent = 0;
63561}
63562
63563/*
63564** Release all of the apPage[] pages for a cursor.
63565*/
63566static void btreeReleaseAllCursorPages(BtCursor *pCur){
63567 int i;
63568 if( pCur->iPage>=0 ){
63569 for(i=0; i<pCur->iPage; i++){
63570 releasePageNotNull(pCur->apPage[i]);
63571 }
63572 releasePageNotNull(pCur->pPage);
63573 pCur->iPage = -1;
63574 }
63575}
63576
63577/*
63578** The cursor passed as the only argument must point to a valid entry
63579** when this function is called (i.e. have eState==CURSOR_VALID). This
63580** function saves the current cursor key in variables pCur->nKey and
63581** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
63582** code otherwise.
63583**
63584** If the cursor is open on an intkey table, then the integer key
63585** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
63586** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
63587** set to point to a malloced buffer pCur->nKey bytes in size containing
63588** the key.
63589*/
63590static int saveCursorKey(BtCursor *pCur){
63591 int rc = SQLITE_OK;
63592 assert( CURSOR_VALID==pCur->eState );
63593 assert( 0==pCur->pKey );
63594 assert( cursorHoldsMutex(pCur) );
63595
63596 if( pCur->curIntKey ){
63597 /* Only the rowid is required for a table btree */
63598 pCur->nKey = sqlite3BtreeIntegerKey(pCur);
63599 }else{
63600 /* For an index btree, save the complete key content */
63601 void *pKey;
63602 pCur->nKey = sqlite3BtreePayloadSize(pCur);
63603 pKey = sqlite3Malloc( pCur->nKey );
63604 if( pKey ){
63605 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
63606 if( rc==SQLITE_OK ){
63607 pCur->pKey = pKey;
63608 }else{
63609 sqlite3_free(pKey);
63610 }
63611 }else{
63612 rc = SQLITE_NOMEM_BKPT;
63613 }
63614 }
63615 assert( !pCur->curIntKey || !pCur->pKey );
63616 return rc;
63617}
63618
63619/*
63620** Save the current cursor position in the variables BtCursor.nKey
63621** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
63622**
63623** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
63624** prior to calling this routine.
63625*/
63626static int saveCursorPosition(BtCursor *pCur){
63627 int rc;
63628
63629 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
63630 assert( 0==pCur->pKey );
63631 assert( cursorHoldsMutex(pCur) );
63632
63633 if( pCur->eState==CURSOR_SKIPNEXT ){
63634 pCur->eState = CURSOR_VALID;
63635 }else{
63636 pCur->skipNext = 0;
63637 }
63638
63639 rc = saveCursorKey(pCur);
63640 if( rc==SQLITE_OK ){
63641 btreeReleaseAllCursorPages(pCur);
63642 pCur->eState = CURSOR_REQUIRESEEK;
63643 }
63644
63645 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast);
63646 return rc;
63647}
63648
63649/* Forward reference */
63650static int SQLITE_NOINLINE saveCursorsOnList(BtCursor*,Pgno,BtCursor*);
63651
63652/*
63653** Save the positions of all cursors (except pExcept) that are open on
63654** the table with root-page iRoot. "Saving the cursor position" means that
63655** the location in the btree is remembered in such a way that it can be
63656** moved back to the same spot after the btree has been modified. This
63657** routine is called just before cursor pExcept is used to modify the
63658** table, for example in BtreeDelete() or BtreeInsert().
63659**
63660** If there are two or more cursors on the same btree, then all such
63661** cursors should have their BTCF_Multiple flag set. The btreeCursor()
63662** routine enforces that rule. This routine only needs to be called in
63663** the uncommon case when pExpect has the BTCF_Multiple flag set.
63664**
63665** If pExpect!=NULL and if no other cursors are found on the same root-page,
63666** then the BTCF_Multiple flag on pExpect is cleared, to avoid another
63667** pointless call to this routine.
63668**
63669** Implementation note: This routine merely checks to see if any cursors
63670** need to be saved. It calls out to saveCursorsOnList() in the (unusual)
63671** event that cursors are in need to being saved.
63672*/
63673static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
63674 BtCursor *p;
63675 assert( sqlite3_mutex_held(pBt->mutex) );
63676 assert( pExcept==0 || pExcept->pBt==pBt );
63677 for(p=pBt->pCursor; p; p=p->pNext){
63678 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
63679 }
63680 if( p ) return saveCursorsOnList(p, iRoot, pExcept);
63681 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple;
63682 return SQLITE_OK;
63683}
63684
63685/* This helper routine to saveAllCursors does the actual work of saving
63686** the cursors if and when a cursor is found that actually requires saving.
63687** The common case is that no cursors need to be saved, so this routine is
63688** broken out from its caller to avoid unnecessary stack pointer movement.
63689*/
63690static int SQLITE_NOINLINE saveCursorsOnList(
63691 BtCursor *p, /* The first cursor that needs saving */
63692 Pgno iRoot, /* Only save cursor with this iRoot. Save all if zero */
63693 BtCursor *pExcept /* Do not save this cursor */
63694){
63695 do{
63696 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
63697 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
63698 int rc = saveCursorPosition(p);
63699 if( SQLITE_OK!=rc ){
63700 return rc;
63701 }
63702 }else{
63703 testcase( p->iPage>=0 );
63704 btreeReleaseAllCursorPages(p);
63705 }
63706 }
63707 p = p->pNext;
63708 }while( p );
63709 return SQLITE_OK;
63710}
63711
63712/*
63713** Clear the current cursor position.
63714*/
63715SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *pCur){
63716 assert( cursorHoldsMutex(pCur) );
63717 sqlite3_free(pCur->pKey);
63718 pCur->pKey = 0;
63719 pCur->eState = CURSOR_INVALID;
63720}
63721
63722/*
63723** In this version of BtreeMoveto, pKey is a packed index record
63724** such as is generated by the OP_MakeRecord opcode. Unpack the
63725** record and then call BtreeMovetoUnpacked() to do the work.
63726*/
63727static int btreeMoveto(
63728 BtCursor *pCur, /* Cursor open on the btree to be searched */
63729 const void *pKey, /* Packed key if the btree is an index */
63730 i64 nKey, /* Integer key for tables. Size of pKey for indices */
63731 int bias, /* Bias search to the high end */
63732 int *pRes /* Write search results here */
63733){
63734 int rc; /* Status code */
63735 UnpackedRecord *pIdxKey; /* Unpacked index key */
63736
63737 if( pKey ){
63738 assert( nKey==(i64)(int)nKey );
63739 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pCur->pKeyInfo);
63740 if( pIdxKey==0 ) return SQLITE_NOMEM_BKPT;
63741 sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey);
63742 if( pIdxKey->nField==0 ){
63743 rc = SQLITE_CORRUPT_BKPT;
63744 goto moveto_done;
63745 }
63746 }else{
63747 pIdxKey = 0;
63748 }
63749 rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
63750moveto_done:
63751 if( pIdxKey ){
63752 sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
63753 }
63754 return rc;
63755}
63756
63757/*
63758** Restore the cursor to the position it was in (or as close to as possible)
63759** when saveCursorPosition() was called. Note that this call deletes the
63760** saved position info stored by saveCursorPosition(), so there can be
63761** at most one effective restoreCursorPosition() call after each
63762** saveCursorPosition().
63763*/
63764static int btreeRestoreCursorPosition(BtCursor *pCur){
63765 int rc;
63766 int skipNext;
63767 assert( cursorOwnsBtShared(pCur) );
63768 assert( pCur->eState>=CURSOR_REQUIRESEEK );
63769 if( pCur->eState==CURSOR_FAULT ){
63770 return pCur->skipNext;
63771 }
63772 pCur->eState = CURSOR_INVALID;
63773 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
63774 if( rc==SQLITE_OK ){
63775 sqlite3_free(pCur->pKey);
63776 pCur->pKey = 0;
63777 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
63778 pCur->skipNext |= skipNext;
63779 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
63780 pCur->eState = CURSOR_SKIPNEXT;
63781 }
63782 }
63783 return rc;
63784}
63785
63786#define restoreCursorPosition(p) \
63787 (p->eState>=CURSOR_REQUIRESEEK ? \
63788 btreeRestoreCursorPosition(p) : \
63789 SQLITE_OK)
63790
63791/*
63792** Determine whether or not a cursor has moved from the position where
63793** it was last placed, or has been invalidated for any other reason.
63794** Cursors can move when the row they are pointing at is deleted out
63795** from under them, for example. Cursor might also move if a btree
63796** is rebalanced.
63797**
63798** Calling this routine with a NULL cursor pointer returns false.
63799**
63800** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor
63801** back to where it ought to be if this routine returns true.
63802*/
63803SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur){
63804 assert( EIGHT_BYTE_ALIGNMENT(pCur)
63805 || pCur==sqlite3BtreeFakeValidCursor() );
63806 assert( offsetof(BtCursor, eState)==0 );
63807 assert( sizeof(pCur->eState)==1 );
63808 return CURSOR_VALID != *(u8*)pCur;
63809}
63810
63811/*
63812** Return a pointer to a fake BtCursor object that will always answer
63813** false to the sqlite3BtreeCursorHasMoved() routine above. The fake
63814** cursor returned must not be used with any other Btree interface.
63815*/
63816SQLITE_PRIVATE BtCursor *sqlite3BtreeFakeValidCursor(void){
63817 static u8 fakeCursor = CURSOR_VALID;
63818 assert( offsetof(BtCursor, eState)==0 );
63819 return (BtCursor*)&fakeCursor;
63820}
63821
63822/*
63823** This routine restores a cursor back to its original position after it
63824** has been moved by some outside activity (such as a btree rebalance or
63825** a row having been deleted out from under the cursor).
63826**
63827** On success, the *pDifferentRow parameter is false if the cursor is left
63828** pointing at exactly the same row. *pDifferntRow is the row the cursor
63829** was pointing to has been deleted, forcing the cursor to point to some
63830** nearby row.
63831**
63832** This routine should only be called for a cursor that just returned
63833** TRUE from sqlite3BtreeCursorHasMoved().
63834*/
63835SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){
63836 int rc;
63837
63838 assert( pCur!=0 );
63839 assert( pCur->eState!=CURSOR_VALID );
63840 rc = restoreCursorPosition(pCur);
63841 if( rc ){
63842 *pDifferentRow = 1;
63843 return rc;
63844 }
63845 if( pCur->eState!=CURSOR_VALID ){
63846 *pDifferentRow = 1;
63847 }else{
63848 assert( pCur->skipNext==0 );
63849 *pDifferentRow = 0;
63850 }
63851 return SQLITE_OK;
63852}
63853
63854#ifdef SQLITE_ENABLE_CURSOR_HINTS
63855/*
63856** Provide hints to the cursor. The particular hint given (and the type
63857** and number of the varargs parameters) is determined by the eHintType
63858** parameter. See the definitions of the BTREE_HINT_* macros for details.
63859*/
63860SQLITE_PRIVATE void sqlite3BtreeCursorHint(BtCursor *pCur, int eHintType, ...){
63861 /* Used only by system that substitute their own storage engine */
63862}
63863#endif
63864
63865/*
63866** Provide flag hints to the cursor.
63867*/
63868SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){
63869 assert( x==BTREE_SEEK_EQ || x==BTREE_BULKLOAD || x==0 );
63870 pCur->hints = x;
63871}
63872
63873
63874#ifndef SQLITE_OMIT_AUTOVACUUM
63875/*
63876** Given a page number of a regular database page, return the page
63877** number for the pointer-map page that contains the entry for the
63878** input page number.
63879**
63880** Return 0 (not a valid page) for pgno==1 since there is
63881** no pointer map associated with page 1. The integrity_check logic
63882** requires that ptrmapPageno(*,1)!=1.
63883*/
63884static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
63885 int nPagesPerMapPage;
63886 Pgno iPtrMap, ret;
63887 assert( sqlite3_mutex_held(pBt->mutex) );
63888 if( pgno<2 ) return 0;
63889 nPagesPerMapPage = (pBt->usableSize/5)+1;
63890 iPtrMap = (pgno-2)/nPagesPerMapPage;
63891 ret = (iPtrMap*nPagesPerMapPage) + 2;
63892 if( ret==PENDING_BYTE_PAGE(pBt) ){
63893 ret++;
63894 }
63895 return ret;
63896}
63897
63898/*
63899** Write an entry into the pointer map.
63900**
63901** This routine updates the pointer map entry for page number 'key'
63902** so that it maps to type 'eType' and parent page number 'pgno'.
63903**
63904** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
63905** a no-op. If an error occurs, the appropriate error code is written
63906** into *pRC.
63907*/
63908static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
63909 DbPage *pDbPage; /* The pointer map page */
63910 u8 *pPtrmap; /* The pointer map data */
63911 Pgno iPtrmap; /* The pointer map page number */
63912 int offset; /* Offset in pointer map page */
63913 int rc; /* Return code from subfunctions */
63914
63915 if( *pRC ) return;
63916
63917 assert( sqlite3_mutex_held(pBt->mutex) );
63918 /* The master-journal page number must never be used as a pointer map page */
63919 assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
63920
63921 assert( pBt->autoVacuum );
63922 if( key==0 ){
63923 *pRC = SQLITE_CORRUPT_BKPT;
63924 return;
63925 }
63926 iPtrmap = PTRMAP_PAGENO(pBt, key);
63927 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
63928 if( rc!=SQLITE_OK ){
63929 *pRC = rc;
63930 return;
63931 }
63932 offset = PTRMAP_PTROFFSET(iPtrmap, key);
63933 if( offset<0 ){
63934 *pRC = SQLITE_CORRUPT_BKPT;
63935 goto ptrmap_exit;
63936 }
63937 assert( offset <= (int)pBt->usableSize-5 );
63938 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
63939
63940 if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
63941 TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
63942 *pRC= rc = sqlite3PagerWrite(pDbPage);
63943 if( rc==SQLITE_OK ){
63944 pPtrmap[offset] = eType;
63945 put4byte(&pPtrmap[offset+1], parent);
63946 }
63947 }
63948
63949ptrmap_exit:
63950 sqlite3PagerUnref(pDbPage);
63951}
63952
63953/*
63954** Read an entry from the pointer map.
63955**
63956** This routine retrieves the pointer map entry for page 'key', writing
63957** the type and parent page number to *pEType and *pPgno respectively.
63958** An error code is returned if something goes wrong, otherwise SQLITE_OK.
63959*/
63960static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
63961 DbPage *pDbPage; /* The pointer map page */
63962 int iPtrmap; /* Pointer map page index */
63963 u8 *pPtrmap; /* Pointer map page data */
63964 int offset; /* Offset of entry in pointer map */
63965 int rc;
63966
63967 assert( sqlite3_mutex_held(pBt->mutex) );
63968
63969 iPtrmap = PTRMAP_PAGENO(pBt, key);
63970 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
63971 if( rc!=0 ){
63972 return rc;
63973 }
63974 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
63975
63976 offset = PTRMAP_PTROFFSET(iPtrmap, key);
63977 if( offset<0 ){
63978 sqlite3PagerUnref(pDbPage);
63979 return SQLITE_CORRUPT_BKPT;
63980 }
63981 assert( offset <= (int)pBt->usableSize-5 );
63982 assert( pEType!=0 );
63983 *pEType = pPtrmap[offset];
63984 if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
63985
63986 sqlite3PagerUnref(pDbPage);
63987 if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_PGNO(iPtrmap);
63988 return SQLITE_OK;
63989}
63990
63991#else /* if defined SQLITE_OMIT_AUTOVACUUM */
63992 #define ptrmapPut(w,x,y,z,rc)
63993 #define ptrmapGet(w,x,y,z) SQLITE_OK
63994 #define ptrmapPutOvflPtr(x, y, rc)
63995#endif
63996
63997/*
63998** Given a btree page and a cell index (0 means the first cell on
63999** the page, 1 means the second cell, and so forth) return a pointer
64000** to the cell content.
64001**
64002** findCellPastPtr() does the same except it skips past the initial
64003** 4-byte child pointer found on interior pages, if there is one.
64004**
64005** This routine works only for pages that do not contain overflow cells.
64006*/
64007#define findCell(P,I) \
64008 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
64009#define findCellPastPtr(P,I) \
64010 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
64011
64012
64013/*
64014** This is common tail processing for btreeParseCellPtr() and
64015** btreeParseCellPtrIndex() for the case when the cell does not fit entirely
64016** on a single B-tree page. Make necessary adjustments to the CellInfo
64017** structure.
64018*/
64019static SQLITE_NOINLINE void btreeParseCellAdjustSizeForOverflow(
64020 MemPage *pPage, /* Page containing the cell */
64021 u8 *pCell, /* Pointer to the cell text. */
64022 CellInfo *pInfo /* Fill in this structure */
64023){
64024 /* If the payload will not fit completely on the local page, we have
64025 ** to decide how much to store locally and how much to spill onto
64026 ** overflow pages. The strategy is to minimize the amount of unused
64027 ** space on overflow pages while keeping the amount of local storage
64028 ** in between minLocal and maxLocal.
64029 **
64030 ** Warning: changing the way overflow payload is distributed in any
64031 ** way will result in an incompatible file format.
64032 */
64033 int minLocal; /* Minimum amount of payload held locally */
64034 int maxLocal; /* Maximum amount of payload held locally */
64035 int surplus; /* Overflow payload available for local storage */
64036
64037 minLocal = pPage->minLocal;
64038 maxLocal = pPage->maxLocal;
64039 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
64040 testcase( surplus==maxLocal );
64041 testcase( surplus==maxLocal+1 );
64042 if( surplus <= maxLocal ){
64043 pInfo->nLocal = (u16)surplus;
64044 }else{
64045 pInfo->nLocal = (u16)minLocal;
64046 }
64047 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
64048}
64049
64050/*
64051** The following routines are implementations of the MemPage.xParseCell()
64052** method.
64053**
64054** Parse a cell content block and fill in the CellInfo structure.
64055**
64056** btreeParseCellPtr() => table btree leaf nodes
64057** btreeParseCellNoPayload() => table btree internal nodes
64058** btreeParseCellPtrIndex() => index btree nodes
64059**
64060** There is also a wrapper function btreeParseCell() that works for
64061** all MemPage types and that references the cell by index rather than
64062** by pointer.
64063*/
64064static void btreeParseCellPtrNoPayload(
64065 MemPage *pPage, /* Page containing the cell */
64066 u8 *pCell, /* Pointer to the cell text. */
64067 CellInfo *pInfo /* Fill in this structure */
64068){
64069 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
64070 assert( pPage->leaf==0 );
64071 assert( pPage->childPtrSize==4 );
64072#ifndef SQLITE_DEBUG
64073 UNUSED_PARAMETER(pPage);
64074#endif
64075 pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
64076 pInfo->nPayload = 0;
64077 pInfo->nLocal = 0;
64078 pInfo->pPayload = 0;
64079 return;
64080}
64081static void btreeParseCellPtr(
64082 MemPage *pPage, /* Page containing the cell */
64083 u8 *pCell, /* Pointer to the cell text. */
64084 CellInfo *pInfo /* Fill in this structure */
64085){
64086 u8 *pIter; /* For scanning through pCell */
64087 u32 nPayload; /* Number of bytes of cell payload */
64088 u64 iKey; /* Extracted Key value */
64089
64090 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
64091 assert( pPage->leaf==0 || pPage->leaf==1 );
64092 assert( pPage->intKeyLeaf );
64093 assert( pPage->childPtrSize==0 );
64094 pIter = pCell;
64095
64096 /* The next block of code is equivalent to:
64097 **
64098 ** pIter += getVarint32(pIter, nPayload);
64099 **
64100 ** The code is inlined to avoid a function call.
64101 */
64102 nPayload = *pIter;
64103 if( nPayload>=0x80 ){
64104 u8 *pEnd = &pIter[8];
64105 nPayload &= 0x7f;
64106 do{
64107 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
64108 }while( (*pIter)>=0x80 && pIter<pEnd );
64109 }
64110 pIter++;
64111
64112 /* The next block of code is equivalent to:
64113 **
64114 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
64115 **
64116 ** The code is inlined to avoid a function call.
64117 */
64118 iKey = *pIter;
64119 if( iKey>=0x80 ){
64120 u8 *pEnd = &pIter[7];
64121 iKey &= 0x7f;
64122 while(1){
64123 iKey = (iKey<<7) | (*++pIter & 0x7f);
64124 if( (*pIter)<0x80 ) break;
64125 if( pIter>=pEnd ){
64126 iKey = (iKey<<8) | *++pIter;
64127 break;
64128 }
64129 }
64130 }
64131 pIter++;
64132
64133 pInfo->nKey = *(i64*)&iKey;
64134 pInfo->nPayload = nPayload;
64135 pInfo->pPayload = pIter;
64136 testcase( nPayload==pPage->maxLocal );
64137 testcase( nPayload==pPage->maxLocal+1 );
64138 if( nPayload<=pPage->maxLocal ){
64139 /* This is the (easy) common case where the entire payload fits
64140 ** on the local page. No overflow is required.
64141 */
64142 pInfo->nSize = nPayload + (u16)(pIter - pCell);
64143 if( pInfo->nSize<4 ) pInfo->nSize = 4;
64144 pInfo->nLocal = (u16)nPayload;
64145 }else{
64146 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
64147 }
64148}
64149static void btreeParseCellPtrIndex(
64150 MemPage *pPage, /* Page containing the cell */
64151 u8 *pCell, /* Pointer to the cell text. */
64152 CellInfo *pInfo /* Fill in this structure */
64153){
64154 u8 *pIter; /* For scanning through pCell */
64155 u32 nPayload; /* Number of bytes of cell payload */
64156
64157 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
64158 assert( pPage->leaf==0 || pPage->leaf==1 );
64159 assert( pPage->intKeyLeaf==0 );
64160 pIter = pCell + pPage->childPtrSize;
64161 nPayload = *pIter;
64162 if( nPayload>=0x80 ){
64163 u8 *pEnd = &pIter[8];
64164 nPayload &= 0x7f;
64165 do{
64166 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
64167 }while( *(pIter)>=0x80 && pIter<pEnd );
64168 }
64169 pIter++;
64170 pInfo->nKey = nPayload;
64171 pInfo->nPayload = nPayload;
64172 pInfo->pPayload = pIter;
64173 testcase( nPayload==pPage->maxLocal );
64174 testcase( nPayload==pPage->maxLocal+1 );
64175 if( nPayload<=pPage->maxLocal ){
64176 /* This is the (easy) common case where the entire payload fits
64177 ** on the local page. No overflow is required.
64178 */
64179 pInfo->nSize = nPayload + (u16)(pIter - pCell);
64180 if( pInfo->nSize<4 ) pInfo->nSize = 4;
64181 pInfo->nLocal = (u16)nPayload;
64182 }else{
64183 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
64184 }
64185}
64186static void btreeParseCell(
64187 MemPage *pPage, /* Page containing the cell */
64188 int iCell, /* The cell index. First cell is 0 */
64189 CellInfo *pInfo /* Fill in this structure */
64190){
64191 pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
64192}
64193
64194/*
64195** The following routines are implementations of the MemPage.xCellSize
64196** method.
64197**
64198** Compute the total number of bytes that a Cell needs in the cell
64199** data area of the btree-page. The return number includes the cell
64200** data header and the local payload, but not any overflow page or
64201** the space used by the cell pointer.
64202**
64203** cellSizePtrNoPayload() => table internal nodes
64204** cellSizePtr() => all index nodes & table leaf nodes
64205*/
64206static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
64207 u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */
64208 u8 *pEnd; /* End mark for a varint */
64209 u32 nSize; /* Size value to return */
64210
64211#ifdef SQLITE_DEBUG
64212 /* The value returned by this function should always be the same as
64213 ** the (CellInfo.nSize) value found by doing a full parse of the
64214 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
64215 ** this function verifies that this invariant is not violated. */
64216 CellInfo debuginfo;
64217 pPage->xParseCell(pPage, pCell, &debuginfo);
64218#endif
64219
64220 nSize = *pIter;
64221 if( nSize>=0x80 ){
64222 pEnd = &pIter[8];
64223 nSize &= 0x7f;
64224 do{
64225 nSize = (nSize<<7) | (*++pIter & 0x7f);
64226 }while( *(pIter)>=0x80 && pIter<pEnd );
64227 }
64228 pIter++;
64229 if( pPage->intKey ){
64230 /* pIter now points at the 64-bit integer key value, a variable length
64231 ** integer. The following block moves pIter to point at the first byte
64232 ** past the end of the key value. */
64233 pEnd = &pIter[9];
64234 while( (*pIter++)&0x80 && pIter<pEnd );
64235 }
64236 testcase( nSize==pPage->maxLocal );
64237 testcase( nSize==pPage->maxLocal+1 );
64238 if( nSize<=pPage->maxLocal ){
64239 nSize += (u32)(pIter - pCell);
64240 if( nSize<4 ) nSize = 4;
64241 }else{
64242 int minLocal = pPage->minLocal;
64243 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
64244 testcase( nSize==pPage->maxLocal );
64245 testcase( nSize==pPage->maxLocal+1 );
64246 if( nSize>pPage->maxLocal ){
64247 nSize = minLocal;
64248 }
64249 nSize += 4 + (u16)(pIter - pCell);
64250 }
64251 assert( nSize==debuginfo.nSize || CORRUPT_DB );
64252 return (u16)nSize;
64253}
64254static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){
64255 u8 *pIter = pCell + 4; /* For looping over bytes of pCell */
64256 u8 *pEnd; /* End mark for a varint */
64257
64258#ifdef SQLITE_DEBUG
64259 /* The value returned by this function should always be the same as
64260 ** the (CellInfo.nSize) value found by doing a full parse of the
64261 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
64262 ** this function verifies that this invariant is not violated. */
64263 CellInfo debuginfo;
64264 pPage->xParseCell(pPage, pCell, &debuginfo);
64265#else
64266 UNUSED_PARAMETER(pPage);
64267#endif
64268
64269 assert( pPage->childPtrSize==4 );
64270 pEnd = pIter + 9;
64271 while( (*pIter++)&0x80 && pIter<pEnd );
64272 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
64273 return (u16)(pIter - pCell);
64274}
64275
64276
64277#ifdef SQLITE_DEBUG
64278/* This variation on cellSizePtr() is used inside of assert() statements
64279** only. */
64280static u16 cellSize(MemPage *pPage, int iCell){
64281 return pPage->xCellSize(pPage, findCell(pPage, iCell));
64282}
64283#endif
64284
64285#ifndef SQLITE_OMIT_AUTOVACUUM
64286/*
64287** If the cell pCell, part of page pPage contains a pointer
64288** to an overflow page, insert an entry into the pointer-map
64289** for the overflow page.
64290*/
64291static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
64292 CellInfo info;
64293 if( *pRC ) return;
64294 assert( pCell!=0 );
64295 pPage->xParseCell(pPage, pCell, &info);
64296 if( info.nLocal<info.nPayload ){
64297 Pgno ovfl = get4byte(&pCell[info.nSize-4]);
64298 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
64299 }
64300}
64301#endif
64302
64303
64304/*
64305** Defragment the page given. This routine reorganizes cells within the
64306** page so that there are no free-blocks on the free-block list.
64307**
64308** Parameter nMaxFrag is the maximum amount of fragmented space that may be
64309** present in the page after this routine returns.
64310**
64311** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
64312** b-tree page so that there are no freeblocks or fragment bytes, all
64313** unused bytes are contained in the unallocated space region, and all
64314** cells are packed tightly at the end of the page.
64315*/
64316static int defragmentPage(MemPage *pPage, int nMaxFrag){
64317 int i; /* Loop counter */
64318 int pc; /* Address of the i-th cell */
64319 int hdr; /* Offset to the page header */
64320 int size; /* Size of a cell */
64321 int usableSize; /* Number of usable bytes on a page */
64322 int cellOffset; /* Offset to the cell pointer array */
64323 int cbrk; /* Offset to the cell content area */
64324 int nCell; /* Number of cells on the page */
64325 unsigned char *data; /* The page data */
64326 unsigned char *temp; /* Temp area for cell content */
64327 unsigned char *src; /* Source of content */
64328 int iCellFirst; /* First allowable cell index */
64329 int iCellLast; /* Last possible cell index */
64330
64331 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
64332 assert( pPage->pBt!=0 );
64333 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
64334 assert( pPage->nOverflow==0 );
64335 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
64336 temp = 0;
64337 src = data = pPage->aData;
64338 hdr = pPage->hdrOffset;
64339 cellOffset = pPage->cellOffset;
64340 nCell = pPage->nCell;
64341 assert( nCell==get2byte(&data[hdr+3]) );
64342 iCellFirst = cellOffset + 2*nCell;
64343 usableSize = pPage->pBt->usableSize;
64344
64345 /* This block handles pages with two or fewer free blocks and nMaxFrag
64346 ** or fewer fragmented bytes. In this case it is faster to move the
64347 ** two (or one) blocks of cells using memmove() and add the required
64348 ** offsets to each pointer in the cell-pointer array than it is to
64349 ** reconstruct the entire page. */
64350 if( (int)data[hdr+7]<=nMaxFrag ){
64351 int iFree = get2byte(&data[hdr+1]);
64352 if( iFree ){
64353 int iFree2 = get2byte(&data[iFree]);
64354
64355 /* pageFindSlot() has already verified that free blocks are sorted
64356 ** in order of offset within the page, and that no block extends
64357 ** past the end of the page. Provided the two free slots do not
64358 ** overlap, this guarantees that the memmove() calls below will not
64359 ** overwrite the usableSize byte buffer, even if the database page
64360 ** is corrupt. */
64361 assert( iFree2==0 || iFree2>iFree );
64362 assert( iFree+get2byte(&data[iFree+2]) <= usableSize );
64363 assert( iFree2==0 || iFree2+get2byte(&data[iFree2+2]) <= usableSize );
64364
64365 if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
64366 u8 *pEnd = &data[cellOffset + nCell*2];
64367 u8 *pAddr;
64368 int sz2 = 0;
64369 int sz = get2byte(&data[iFree+2]);
64370 int top = get2byte(&data[hdr+5]);
64371 if( top>=iFree ){
64372 return SQLITE_CORRUPT_PAGE(pPage);
64373 }
64374 if( iFree2 ){
64375 assert( iFree+sz<=iFree2 ); /* Verified by pageFindSlot() */
64376 sz2 = get2byte(&data[iFree2+2]);
64377 assert( iFree+sz+sz2+iFree2-(iFree+sz) <= usableSize );
64378 memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
64379 sz += sz2;
64380 }
64381 cbrk = top+sz;
64382 assert( cbrk+(iFree-top) <= usableSize );
64383 memmove(&data[cbrk], &data[top], iFree-top);
64384 for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){
64385 pc = get2byte(pAddr);
64386 if( pc<iFree ){ put2byte(pAddr, pc+sz); }
64387 else if( pc<iFree2 ){ put2byte(pAddr, pc+sz2); }
64388 }
64389 goto defragment_out;
64390 }
64391 }
64392 }
64393
64394 cbrk = usableSize;
64395 iCellLast = usableSize - 4;
64396 for(i=0; i<nCell; i++){
64397 u8 *pAddr; /* The i-th cell pointer */
64398 pAddr = &data[cellOffset + i*2];
64399 pc = get2byte(pAddr);
64400 testcase( pc==iCellFirst );
64401 testcase( pc==iCellLast );
64402 /* These conditions have already been verified in btreeInitPage()
64403 ** if PRAGMA cell_size_check=ON.
64404 */
64405 if( pc<iCellFirst || pc>iCellLast ){
64406 return SQLITE_CORRUPT_PAGE(pPage);
64407 }
64408 assert( pc>=iCellFirst && pc<=iCellLast );
64409 size = pPage->xCellSize(pPage, &src[pc]);
64410 cbrk -= size;
64411 if( cbrk<iCellFirst || pc+size>usableSize ){
64412 return SQLITE_CORRUPT_PAGE(pPage);
64413 }
64414 assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
64415 testcase( cbrk+size==usableSize );
64416 testcase( pc+size==usableSize );
64417 put2byte(pAddr, cbrk);
64418 if( temp==0 ){
64419 int x;
64420 if( cbrk==pc ) continue;
64421 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
64422 x = get2byte(&data[hdr+5]);
64423 memcpy(&temp[x], &data[x], (cbrk+size) - x);
64424 src = temp;
64425 }
64426 memcpy(&data[cbrk], &src[pc], size);
64427 }
64428 data[hdr+7] = 0;
64429
64430 defragment_out:
64431 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
64432 return SQLITE_CORRUPT_PAGE(pPage);
64433 }
64434 assert( cbrk>=iCellFirst );
64435 put2byte(&data[hdr+5], cbrk);
64436 data[hdr+1] = 0;
64437 data[hdr+2] = 0;
64438 memset(&data[iCellFirst], 0, cbrk-iCellFirst);
64439 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
64440 return SQLITE_OK;
64441}
64442
64443/*
64444** Search the free-list on page pPg for space to store a cell nByte bytes in
64445** size. If one can be found, return a pointer to the space and remove it
64446** from the free-list.
64447**
64448** If no suitable space can be found on the free-list, return NULL.
64449**
64450** This function may detect corruption within pPg. If corruption is
64451** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
64452**
64453** Slots on the free list that are between 1 and 3 bytes larger than nByte
64454** will be ignored if adding the extra space to the fragmentation count
64455** causes the fragmentation count to exceed 60.
64456*/
64457static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
64458 const int hdr = pPg->hdrOffset;
64459 u8 * const aData = pPg->aData;
64460 int iAddr = hdr + 1;
64461 int pc = get2byte(&aData[iAddr]);
64462 int x;
64463 int usableSize = pPg->pBt->usableSize;
64464 int size; /* Size of the free slot */
64465
64466 assert( pc>0 );
64467 while( pc<=usableSize-4 ){
64468 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
64469 ** freeblock form a big-endian integer which is the size of the freeblock
64470 ** in bytes, including the 4-byte header. */
64471 size = get2byte(&aData[pc+2]);
64472 if( (x = size - nByte)>=0 ){
64473 testcase( x==4 );
64474 testcase( x==3 );
64475 if( size+pc > usableSize ){
64476 *pRc = SQLITE_CORRUPT_PAGE(pPg);
64477 return 0;
64478 }else if( x<4 ){
64479 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
64480 ** number of bytes in fragments may not exceed 60. */
64481 if( aData[hdr+7]>57 ) return 0;
64482
64483 /* Remove the slot from the free-list. Update the number of
64484 ** fragmented bytes within the page. */
64485 memcpy(&aData[iAddr], &aData[pc], 2);
64486 aData[hdr+7] += (u8)x;
64487 }else{
64488 /* The slot remains on the free-list. Reduce its size to account
64489 ** for the portion used by the new allocation. */
64490 put2byte(&aData[pc+2], x);
64491 }
64492 return &aData[pc + x];
64493 }
64494 iAddr = pc;
64495 pc = get2byte(&aData[pc]);
64496 if( pc<iAddr+size ) break;
64497 }
64498 if( pc ){
64499 *pRc = SQLITE_CORRUPT_PAGE(pPg);
64500 }
64501
64502 return 0;
64503}
64504
64505/*
64506** Allocate nByte bytes of space from within the B-Tree page passed
64507** as the first argument. Write into *pIdx the index into pPage->aData[]
64508** of the first byte of allocated space. Return either SQLITE_OK or
64509** an error code (usually SQLITE_CORRUPT).
64510**
64511** The caller guarantees that there is sufficient space to make the
64512** allocation. This routine might need to defragment in order to bring
64513** all the space together, however. This routine will avoid using
64514** the first two bytes past the cell pointer area since presumably this
64515** allocation is being made in order to insert a new cell, so we will
64516** also end up needing a new cell pointer.
64517*/
64518static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
64519 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
64520 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
64521 int top; /* First byte of cell content area */
64522 int rc = SQLITE_OK; /* Integer return code */
64523 int gap; /* First byte of gap between cell pointers and cell content */
64524
64525 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
64526 assert( pPage->pBt );
64527 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
64528 assert( nByte>=0 ); /* Minimum cell size is 4 */
64529 assert( pPage->nFree>=nByte );
64530 assert( pPage->nOverflow==0 );
64531 assert( nByte < (int)(pPage->pBt->usableSize-8) );
64532
64533 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
64534 gap = pPage->cellOffset + 2*pPage->nCell;
64535 assert( gap<=65536 );
64536 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
64537 ** and the reserved space is zero (the usual value for reserved space)
64538 ** then the cell content offset of an empty page wants to be 65536.
64539 ** However, that integer is too large to be stored in a 2-byte unsigned
64540 ** integer, so a value of 0 is used in its place. */
64541 top = get2byte(&data[hdr+5]);
64542 assert( top<=(int)pPage->pBt->usableSize ); /* Prevent by getAndInitPage() */
64543 if( gap>top ){
64544 if( top==0 && pPage->pBt->usableSize==65536 ){
64545 top = 65536;
64546 }else{
64547 return SQLITE_CORRUPT_PAGE(pPage);
64548 }
64549 }
64550
64551 /* If there is enough space between gap and top for one more cell pointer
64552 ** array entry offset, and if the freelist is not empty, then search the
64553 ** freelist looking for a free slot big enough to satisfy the request.
64554 */
64555 testcase( gap+2==top );
64556 testcase( gap+1==top );
64557 testcase( gap==top );
64558 if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
64559 u8 *pSpace = pageFindSlot(pPage, nByte, &rc);
64560 if( pSpace ){
64561 assert( pSpace>=data && (pSpace - data)<65536 );
64562 *pIdx = (int)(pSpace - data);
64563 return SQLITE_OK;
64564 }else if( rc ){
64565 return rc;
64566 }
64567 }
64568
64569 /* The request could not be fulfilled using a freelist slot. Check
64570 ** to see if defragmentation is necessary.
64571 */
64572 testcase( gap+2+nByte==top );
64573 if( gap+2+nByte>top ){
64574 assert( pPage->nCell>0 || CORRUPT_DB );
64575 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
64576 if( rc ) return rc;
64577 top = get2byteNotZero(&data[hdr+5]);
64578 assert( gap+2+nByte<=top );
64579 }
64580
64581
64582 /* Allocate memory from the gap in between the cell pointer array
64583 ** and the cell content area. The btreeInitPage() call has already
64584 ** validated the freelist. Given that the freelist is valid, there
64585 ** is no way that the allocation can extend off the end of the page.
64586 ** The assert() below verifies the previous sentence.
64587 */
64588 top -= nByte;
64589 put2byte(&data[hdr+5], top);
64590 assert( top+nByte <= (int)pPage->pBt->usableSize );
64591 *pIdx = top;
64592 return SQLITE_OK;
64593}
64594
64595/*
64596** Return a section of the pPage->aData to the freelist.
64597** The first byte of the new free block is pPage->aData[iStart]
64598** and the size of the block is iSize bytes.
64599**
64600** Adjacent freeblocks are coalesced.
64601**
64602** Note that even though the freeblock list was checked by btreeInitPage(),
64603** that routine will not detect overlap between cells or freeblocks. Nor
64604** does it detect cells or freeblocks that encrouch into the reserved bytes
64605** at the end of the page. So do additional corruption checks inside this
64606** routine and return SQLITE_CORRUPT if any problems are found.
64607*/
64608static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
64609 u16 iPtr; /* Address of ptr to next freeblock */
64610 u16 iFreeBlk; /* Address of the next freeblock */
64611 u8 hdr; /* Page header size. 0 or 100 */
64612 u8 nFrag = 0; /* Reduction in fragmentation */
64613 u16 iOrigSize = iSize; /* Original value of iSize */
64614 u16 x; /* Offset to cell content area */
64615 u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */
64616 unsigned char *data = pPage->aData; /* Page content */
64617
64618 assert( pPage->pBt!=0 );
64619 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
64620 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
64621 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
64622 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
64623 assert( iSize>=4 ); /* Minimum cell size is 4 */
64624 assert( iStart<=pPage->pBt->usableSize-4 );
64625
64626 /* The list of freeblocks must be in ascending order. Find the
64627 ** spot on the list where iStart should be inserted.
64628 */
64629 hdr = pPage->hdrOffset;
64630 iPtr = hdr + 1;
64631 if( data[iPtr+1]==0 && data[iPtr]==0 ){
64632 iFreeBlk = 0; /* Shortcut for the case when the freelist is empty */
64633 }else{
64634 while( (iFreeBlk = get2byte(&data[iPtr]))<iStart ){
64635 if( iFreeBlk<iPtr+4 ){
64636 if( iFreeBlk==0 ) break;
64637 return SQLITE_CORRUPT_PAGE(pPage);
64638 }
64639 iPtr = iFreeBlk;
64640 }
64641 if( iFreeBlk>pPage->pBt->usableSize-4 ){
64642 return SQLITE_CORRUPT_PAGE(pPage);
64643 }
64644 assert( iFreeBlk>iPtr || iFreeBlk==0 );
64645
64646 /* At this point:
64647 ** iFreeBlk: First freeblock after iStart, or zero if none
64648 ** iPtr: The address of a pointer to iFreeBlk
64649 **
64650 ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
64651 */
64652 if( iFreeBlk && iEnd+3>=iFreeBlk ){
64653 nFrag = iFreeBlk - iEnd;
64654 if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage);
64655 iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
64656 if( iEnd > pPage->pBt->usableSize ){
64657 return SQLITE_CORRUPT_PAGE(pPage);
64658 }
64659 iSize = iEnd - iStart;
64660 iFreeBlk = get2byte(&data[iFreeBlk]);
64661 }
64662
64663 /* If iPtr is another freeblock (that is, if iPtr is not the freelist
64664 ** pointer in the page header) then check to see if iStart should be
64665 ** coalesced onto the end of iPtr.
64666 */
64667 if( iPtr>hdr+1 ){
64668 int iPtrEnd = iPtr + get2byte(&data[iPtr+2]);
64669 if( iPtrEnd+3>=iStart ){
64670 if( iPtrEnd>iStart ) return SQLITE_CORRUPT_PAGE(pPage);
64671 nFrag += iStart - iPtrEnd;
64672 iSize = iEnd - iPtr;
64673 iStart = iPtr;
64674 }
64675 }
64676 if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage);
64677 data[hdr+7] -= nFrag;
64678 }
64679 x = get2byte(&data[hdr+5]);
64680 if( iStart<=x ){
64681 /* The new freeblock is at the beginning of the cell content area,
64682 ** so just extend the cell content area rather than create another
64683 ** freelist entry */
64684 if( iStart<x || iPtr!=hdr+1 ) return SQLITE_CORRUPT_PAGE(pPage);
64685 put2byte(&data[hdr+1], iFreeBlk);
64686 put2byte(&data[hdr+5], iEnd);
64687 }else{
64688 /* Insert the new freeblock into the freelist */
64689 put2byte(&data[iPtr], iStart);
64690 }
64691 if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
64692 /* Overwrite deleted information with zeros when the secure_delete
64693 ** option is enabled */
64694 memset(&data[iStart], 0, iSize);
64695 }
64696 put2byte(&data[iStart], iFreeBlk);
64697 put2byte(&data[iStart+2], iSize);
64698 pPage->nFree += iOrigSize;
64699 return SQLITE_OK;
64700}
64701
64702/*
64703** Decode the flags byte (the first byte of the header) for a page
64704** and initialize fields of the MemPage structure accordingly.
64705**
64706** Only the following combinations are supported. Anything different
64707** indicates a corrupt database files:
64708**
64709** PTF_ZERODATA
64710** PTF_ZERODATA | PTF_LEAF
64711** PTF_LEAFDATA | PTF_INTKEY
64712** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
64713*/
64714static int decodeFlags(MemPage *pPage, int flagByte){
64715 BtShared *pBt; /* A copy of pPage->pBt */
64716
64717 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
64718 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
64719 pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 );
64720 flagByte &= ~PTF_LEAF;
64721 pPage->childPtrSize = 4-4*pPage->leaf;
64722 pPage->xCellSize = cellSizePtr;
64723 pBt = pPage->pBt;
64724 if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
64725 /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an
64726 ** interior table b-tree page. */
64727 assert( (PTF_LEAFDATA|PTF_INTKEY)==5 );
64728 /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a
64729 ** leaf table b-tree page. */
64730 assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 );
64731 pPage->intKey = 1;
64732 if( pPage->leaf ){
64733 pPage->intKeyLeaf = 1;
64734 pPage->xParseCell = btreeParseCellPtr;
64735 }else{
64736 pPage->intKeyLeaf = 0;
64737 pPage->xCellSize = cellSizePtrNoPayload;
64738 pPage->xParseCell = btreeParseCellPtrNoPayload;
64739 }
64740 pPage->maxLocal = pBt->maxLeaf;
64741 pPage->minLocal = pBt->minLeaf;
64742 }else if( flagByte==PTF_ZERODATA ){
64743 /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an
64744 ** interior index b-tree page. */
64745 assert( (PTF_ZERODATA)==2 );
64746 /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a
64747 ** leaf index b-tree page. */
64748 assert( (PTF_ZERODATA|PTF_LEAF)==10 );
64749 pPage->intKey = 0;
64750 pPage->intKeyLeaf = 0;
64751 pPage->xParseCell = btreeParseCellPtrIndex;
64752 pPage->maxLocal = pBt->maxLocal;
64753 pPage->minLocal = pBt->minLocal;
64754 }else{
64755 /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
64756 ** an error. */
64757 return SQLITE_CORRUPT_PAGE(pPage);
64758 }
64759 pPage->max1bytePayload = pBt->max1bytePayload;
64760 return SQLITE_OK;
64761}
64762
64763/*
64764** Initialize the auxiliary information for a disk block.
64765**
64766** Return SQLITE_OK on success. If we see that the page does
64767** not contain a well-formed database page, then return
64768** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
64769** guarantee that the page is well-formed. It only shows that
64770** we failed to detect any corruption.
64771*/
64772static int btreeInitPage(MemPage *pPage){
64773 int pc; /* Address of a freeblock within pPage->aData[] */
64774 u8 hdr; /* Offset to beginning of page header */
64775 u8 *data; /* Equal to pPage->aData */
64776 BtShared *pBt; /* The main btree structure */
64777 int usableSize; /* Amount of usable space on each page */
64778 u16 cellOffset; /* Offset from start of page to first cell pointer */
64779 int nFree; /* Number of unused bytes on the page */
64780 int top; /* First byte of the cell content area */
64781 int iCellFirst; /* First allowable cell or freeblock offset */
64782 int iCellLast; /* Last possible cell or freeblock offset */
64783
64784 assert( pPage->pBt!=0 );
64785 assert( pPage->pBt->db!=0 );
64786 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
64787 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
64788 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
64789 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
64790 assert( pPage->isInit==0 );
64791
64792 pBt = pPage->pBt;
64793 hdr = pPage->hdrOffset;
64794 data = pPage->aData;
64795 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
64796 ** the b-tree page type. */
64797 if( decodeFlags(pPage, data[hdr]) ){
64798 return SQLITE_CORRUPT_PAGE(pPage);
64799 }
64800 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
64801 pPage->maskPage = (u16)(pBt->pageSize - 1);
64802 pPage->nOverflow = 0;
64803 usableSize = pBt->usableSize;
64804 pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize;
64805 pPage->aDataEnd = &data[usableSize];
64806 pPage->aCellIdx = &data[cellOffset];
64807 pPage->aDataOfst = &data[pPage->childPtrSize];
64808 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
64809 ** the start of the cell content area. A zero value for this integer is
64810 ** interpreted as 65536. */
64811 top = get2byteNotZero(&data[hdr+5]);
64812 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
64813 ** number of cells on the page. */
64814 pPage->nCell = get2byte(&data[hdr+3]);
64815 if( pPage->nCell>MX_CELL(pBt) ){
64816 /* To many cells for a single page. The page must be corrupt */
64817 return SQLITE_CORRUPT_PAGE(pPage);
64818 }
64819 testcase( pPage->nCell==MX_CELL(pBt) );
64820 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
64821 ** possible for a root page of a table that contains no rows) then the
64822 ** offset to the cell content area will equal the page size minus the
64823 ** bytes of reserved space. */
64824 assert( pPage->nCell>0 || top==usableSize || CORRUPT_DB );
64825
64826 /* A malformed database page might cause us to read past the end
64827 ** of page when parsing a cell.
64828 **
64829 ** The following block of code checks early to see if a cell extends
64830 ** past the end of a page boundary and causes SQLITE_CORRUPT to be
64831 ** returned if it does.
64832 */
64833 iCellFirst = cellOffset + 2*pPage->nCell;
64834 iCellLast = usableSize - 4;
64835 if( pBt->db->flags & SQLITE_CellSizeCk ){
64836 int i; /* Index into the cell pointer array */
64837 int sz; /* Size of a cell */
64838
64839 if( !pPage->leaf ) iCellLast--;
64840 for(i=0; i<pPage->nCell; i++){
64841 pc = get2byteAligned(&data[cellOffset+i*2]);
64842 testcase( pc==iCellFirst );
64843 testcase( pc==iCellLast );
64844 if( pc<iCellFirst || pc>iCellLast ){
64845 return SQLITE_CORRUPT_PAGE(pPage);
64846 }
64847 sz = pPage->xCellSize(pPage, &data[pc]);
64848 testcase( pc+sz==usableSize );
64849 if( pc+sz>usableSize ){
64850 return SQLITE_CORRUPT_PAGE(pPage);
64851 }
64852 }
64853 if( !pPage->leaf ) iCellLast++;
64854 }
64855
64856 /* Compute the total free space on the page
64857 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
64858 ** start of the first freeblock on the page, or is zero if there are no
64859 ** freeblocks. */
64860 pc = get2byte(&data[hdr+1]);
64861 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */
64862 if( pc>0 ){
64863 u32 next, size;
64864 if( pc<iCellFirst ){
64865 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
64866 ** always be at least one cell before the first freeblock.
64867 */
64868 return SQLITE_CORRUPT_PAGE(pPage);
64869 }
64870 while( 1 ){
64871 if( pc>iCellLast ){
64872 /* Freeblock off the end of the page */
64873 return SQLITE_CORRUPT_PAGE(pPage);
64874 }
64875 next = get2byte(&data[pc]);
64876 size = get2byte(&data[pc+2]);
64877 nFree = nFree + size;
64878 if( next<=pc+size+3 ) break;
64879 pc = next;
64880 }
64881 if( next>0 ){
64882 /* Freeblock not in ascending order */
64883 return SQLITE_CORRUPT_PAGE(pPage);
64884 }
64885 if( pc+size>(unsigned int)usableSize ){
64886 /* Last freeblock extends past page end */
64887 return SQLITE_CORRUPT_PAGE(pPage);
64888 }
64889 }
64890
64891 /* At this point, nFree contains the sum of the offset to the start
64892 ** of the cell-content area plus the number of free bytes within
64893 ** the cell-content area. If this is greater than the usable-size
64894 ** of the page, then the page must be corrupted. This check also
64895 ** serves to verify that the offset to the start of the cell-content
64896 ** area, according to the page header, lies within the page.
64897 */
64898 if( nFree>usableSize ){
64899 return SQLITE_CORRUPT_PAGE(pPage);
64900 }
64901 pPage->nFree = (u16)(nFree - iCellFirst);
64902 pPage->isInit = 1;
64903 return SQLITE_OK;
64904}
64905
64906/*
64907** Set up a raw page so that it looks like a database page holding
64908** no entries.
64909*/
64910static void zeroPage(MemPage *pPage, int flags){
64911 unsigned char *data = pPage->aData;
64912 BtShared *pBt = pPage->pBt;
64913 u8 hdr = pPage->hdrOffset;
64914 u16 first;
64915
64916 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
64917 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
64918 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
64919 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
64920 assert( sqlite3_mutex_held(pBt->mutex) );
64921 if( pBt->btsFlags & BTS_FAST_SECURE ){
64922 memset(&data[hdr], 0, pBt->usableSize - hdr);
64923 }
64924 data[hdr] = (char)flags;
64925 first = hdr + ((flags&PTF_LEAF)==0 ? 12 : 8);
64926 memset(&data[hdr+1], 0, 4);
64927 data[hdr+7] = 0;
64928 put2byte(&data[hdr+5], pBt->usableSize);
64929 pPage->nFree = (u16)(pBt->usableSize - first);
64930 decodeFlags(pPage, flags);
64931 pPage->cellOffset = first;
64932 pPage->aDataEnd = &data[pBt->usableSize];
64933 pPage->aCellIdx = &data[first];
64934 pPage->aDataOfst = &data[pPage->childPtrSize];
64935 pPage->nOverflow = 0;
64936 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
64937 pPage->maskPage = (u16)(pBt->pageSize - 1);
64938 pPage->nCell = 0;
64939 pPage->isInit = 1;
64940}
64941
64942
64943/*
64944** Convert a DbPage obtained from the pager into a MemPage used by
64945** the btree layer.
64946*/
64947static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
64948 MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
64949 if( pgno!=pPage->pgno ){
64950 pPage->aData = sqlite3PagerGetData(pDbPage);
64951 pPage->pDbPage = pDbPage;
64952 pPage->pBt = pBt;
64953 pPage->pgno = pgno;
64954 pPage->hdrOffset = pgno==1 ? 100 : 0;
64955 }
64956 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
64957 return pPage;
64958}
64959
64960/*
64961** Get a page from the pager. Initialize the MemPage.pBt and
64962** MemPage.aData elements if needed. See also: btreeGetUnusedPage().
64963**
64964** If the PAGER_GET_NOCONTENT flag is set, it means that we do not care
64965** about the content of the page at this time. So do not go to the disk
64966** to fetch the content. Just fill in the content with zeros for now.
64967** If in the future we call sqlite3PagerWrite() on this page, that
64968** means we have started to be concerned about content and the disk
64969** read should occur at that point.
64970*/
64971static int btreeGetPage(
64972 BtShared *pBt, /* The btree */
64973 Pgno pgno, /* Number of the page to fetch */
64974 MemPage **ppPage, /* Return the page in this parameter */
64975 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
64976){
64977 int rc;
64978 DbPage *pDbPage;
64979
64980 assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY );
64981 assert( sqlite3_mutex_held(pBt->mutex) );
64982 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
64983 if( rc ) return rc;
64984 *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
64985 return SQLITE_OK;
64986}
64987
64988/*
64989** Retrieve a page from the pager cache. If the requested page is not
64990** already in the pager cache return NULL. Initialize the MemPage.pBt and
64991** MemPage.aData elements if needed.
64992*/
64993static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
64994 DbPage *pDbPage;
64995 assert( sqlite3_mutex_held(pBt->mutex) );
64996 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
64997 if( pDbPage ){
64998 return btreePageFromDbPage(pDbPage, pgno, pBt);
64999 }
65000 return 0;
65001}
65002
65003/*
65004** Return the size of the database file in pages. If there is any kind of
65005** error, return ((unsigned int)-1).
65006*/
65007static Pgno btreePagecount(BtShared *pBt){
65008 return pBt->nPage;
65009}
65010SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){
65011 assert( sqlite3BtreeHoldsMutex(p) );
65012 assert( ((p->pBt->nPage)&0x80000000)==0 );
65013 return btreePagecount(p->pBt);
65014}
65015
65016/*
65017** Get a page from the pager and initialize it.
65018**
65019** If pCur!=0 then the page is being fetched as part of a moveToChild()
65020** call. Do additional sanity checking on the page in this case.
65021** And if the fetch fails, this routine must decrement pCur->iPage.
65022**
65023** The page is fetched as read-write unless pCur is not NULL and is
65024** a read-only cursor.
65025**
65026** If an error occurs, then *ppPage is undefined. It
65027** may remain unchanged, or it may be set to an invalid value.
65028*/
65029static int getAndInitPage(
65030 BtShared *pBt, /* The database file */
65031 Pgno pgno, /* Number of the page to get */
65032 MemPage **ppPage, /* Write the page pointer here */
65033 BtCursor *pCur, /* Cursor to receive the page, or NULL */
65034 int bReadOnly /* True for a read-only page */
65035){
65036 int rc;
65037 DbPage *pDbPage;
65038 assert( sqlite3_mutex_held(pBt->mutex) );
65039 assert( pCur==0 || ppPage==&pCur->pPage );
65040 assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
65041 assert( pCur==0 || pCur->iPage>0 );
65042
65043 if( pgno>btreePagecount(pBt) ){
65044 rc = SQLITE_CORRUPT_BKPT;
65045 goto getAndInitPage_error;
65046 }
65047 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
65048 if( rc ){
65049 goto getAndInitPage_error;
65050 }
65051 *ppPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
65052 if( (*ppPage)->isInit==0 ){
65053 btreePageFromDbPage(pDbPage, pgno, pBt);
65054 rc = btreeInitPage(*ppPage);
65055 if( rc!=SQLITE_OK ){
65056 releasePage(*ppPage);
65057 goto getAndInitPage_error;
65058 }
65059 }
65060 assert( (*ppPage)->pgno==pgno );
65061 assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) );
65062
65063 /* If obtaining a child page for a cursor, we must verify that the page is
65064 ** compatible with the root page. */
65065 if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){
65066 rc = SQLITE_CORRUPT_PGNO(pgno);
65067 releasePage(*ppPage);
65068 goto getAndInitPage_error;
65069 }
65070 return SQLITE_OK;
65071
65072getAndInitPage_error:
65073 if( pCur ){
65074 pCur->iPage--;
65075 pCur->pPage = pCur->apPage[pCur->iPage];
65076 }
65077 testcase( pgno==0 );
65078 assert( pgno!=0 || rc==SQLITE_CORRUPT );
65079 return rc;
65080}
65081
65082/*
65083** Release a MemPage. This should be called once for each prior
65084** call to btreeGetPage.
65085**
65086** Page1 is a special case and must be released using releasePageOne().
65087*/
65088static void releasePageNotNull(MemPage *pPage){
65089 assert( pPage->aData );
65090 assert( pPage->pBt );
65091 assert( pPage->pDbPage!=0 );
65092 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
65093 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
65094 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
65095 sqlite3PagerUnrefNotNull(pPage->pDbPage);
65096}
65097static void releasePage(MemPage *pPage){
65098 if( pPage ) releasePageNotNull(pPage);
65099}
65100static void releasePageOne(MemPage *pPage){
65101 assert( pPage!=0 );
65102 assert( pPage->aData );
65103 assert( pPage->pBt );
65104 assert( pPage->pDbPage!=0 );
65105 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
65106 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
65107 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
65108 sqlite3PagerUnrefPageOne(pPage->pDbPage);
65109}
65110
65111/*
65112** Get an unused page.
65113**
65114** This works just like btreeGetPage() with the addition:
65115**
65116** * If the page is already in use for some other purpose, immediately
65117** release it and return an SQLITE_CURRUPT error.
65118** * Make sure the isInit flag is clear
65119*/
65120static int btreeGetUnusedPage(
65121 BtShared *pBt, /* The btree */
65122 Pgno pgno, /* Number of the page to fetch */
65123 MemPage **ppPage, /* Return the page in this parameter */
65124 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
65125){
65126 int rc = btreeGetPage(pBt, pgno, ppPage, flags);
65127 if( rc==SQLITE_OK ){
65128 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
65129 releasePage(*ppPage);
65130 *ppPage = 0;
65131 return SQLITE_CORRUPT_BKPT;
65132 }
65133 (*ppPage)->isInit = 0;
65134 }else{
65135 *ppPage = 0;
65136 }
65137 return rc;
65138}
65139
65140
65141/*
65142** During a rollback, when the pager reloads information into the cache
65143** so that the cache is restored to its original state at the start of
65144** the transaction, for each page restored this routine is called.
65145**
65146** This routine needs to reset the extra data section at the end of the
65147** page to agree with the restored data.
65148*/
65149static void pageReinit(DbPage *pData){
65150 MemPage *pPage;
65151 pPage = (MemPage *)sqlite3PagerGetExtra(pData);
65152 assert( sqlite3PagerPageRefcount(pData)>0 );
65153 if( pPage->isInit ){
65154 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
65155 pPage->isInit = 0;
65156 if( sqlite3PagerPageRefcount(pData)>1 ){
65157 /* pPage might not be a btree page; it might be an overflow page
65158 ** or ptrmap page or a free page. In those cases, the following
65159 ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
65160 ** But no harm is done by this. And it is very important that
65161 ** btreeInitPage() be called on every btree page so we make
65162 ** the call for every page that comes in for re-initing. */
65163 btreeInitPage(pPage);
65164 }
65165 }
65166}
65167
65168/*
65169** Invoke the busy handler for a btree.
65170*/
65171static int btreeInvokeBusyHandler(void *pArg){
65172 BtShared *pBt = (BtShared*)pArg;
65173 assert( pBt->db );
65174 assert( sqlite3_mutex_held(pBt->db->mutex) );
65175 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler,
65176 sqlite3PagerFile(pBt->pPager));
65177}
65178
65179/*
65180** Open a database file.
65181**
65182** zFilename is the name of the database file. If zFilename is NULL
65183** then an ephemeral database is created. The ephemeral database might
65184** be exclusively in memory, or it might use a disk-based memory cache.
65185** Either way, the ephemeral database will be automatically deleted
65186** when sqlite3BtreeClose() is called.
65187**
65188** If zFilename is ":memory:" then an in-memory database is created
65189** that is automatically destroyed when it is closed.
65190**
65191** The "flags" parameter is a bitmask that might contain bits like
65192** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY.
65193**
65194** If the database is already opened in the same database connection
65195** and we are in shared cache mode, then the open will fail with an
65196** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared
65197** objects in the same database connection since doing so will lead
65198** to problems with locking.
65199*/
65200SQLITE_PRIVATE int sqlite3BtreeOpen(
65201 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
65202 const char *zFilename, /* Name of the file containing the BTree database */
65203 sqlite3 *db, /* Associated database handle */
65204 Btree **ppBtree, /* Pointer to new Btree object written here */
65205 int flags, /* Options */
65206 int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */
65207){
65208 BtShared *pBt = 0; /* Shared part of btree structure */
65209 Btree *p; /* Handle to return */
65210 sqlite3_mutex *mutexOpen = 0; /* Prevents a race condition. Ticket #3537 */
65211 int rc = SQLITE_OK; /* Result code from this function */
65212 u8 nReserve; /* Byte of unused space on each page */
65213 unsigned char zDbHeader[100]; /* Database header content */
65214
65215 /* True if opening an ephemeral, temporary database */
65216 const int isTempDb = zFilename==0 || zFilename[0]==0;
65217
65218 /* Set the variable isMemdb to true for an in-memory database, or
65219 ** false for a file-based database.
65220 */
65221#ifdef SQLITE_OMIT_MEMORYDB
65222 const int isMemdb = 0;
65223#else
65224 const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
65225 || (isTempDb && sqlite3TempInMemory(db))
65226 || (vfsFlags & SQLITE_OPEN_MEMORY)!=0;
65227#endif
65228
65229 assert( db!=0 );
65230 assert( pVfs!=0 );
65231 assert( sqlite3_mutex_held(db->mutex) );
65232 assert( (flags&0xff)==flags ); /* flags fit in 8 bits */
65233
65234 /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
65235 assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
65236
65237 /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
65238 assert( (flags & BTREE_SINGLE)==0 || isTempDb );
65239
65240 if( isMemdb ){
65241 flags |= BTREE_MEMORY;
65242 }
65243 if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
65244 vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
65245 }
65246 p = sqlite3MallocZero(sizeof(Btree));
65247 if( !p ){
65248 return SQLITE_NOMEM_BKPT;
65249 }
65250 p->inTrans = TRANS_NONE;
65251 p->db = db;
65252#ifndef SQLITE_OMIT_SHARED_CACHE
65253 p->lock.pBtree = p;
65254 p->lock.iTable = 1;
65255#endif
65256
65257#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
65258 /*
65259 ** If this Btree is a candidate for shared cache, try to find an
65260 ** existing BtShared object that we can share with
65261 */
65262 if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI)!=0) ){
65263 if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
65264 int nFilename = sqlite3Strlen30(zFilename)+1;
65265 int nFullPathname = pVfs->mxPathname+1;
65266 char *zFullPathname = sqlite3Malloc(MAX(nFullPathname,nFilename));
65267 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
65268
65269 p->sharable = 1;
65270 if( !zFullPathname ){
65271 sqlite3_free(p);
65272 return SQLITE_NOMEM_BKPT;
65273 }
65274 if( isMemdb ){
65275 memcpy(zFullPathname, zFilename, nFilename);
65276 }else{
65277 rc = sqlite3OsFullPathname(pVfs, zFilename,
65278 nFullPathname, zFullPathname);
65279 if( rc ){
65280 sqlite3_free(zFullPathname);
65281 sqlite3_free(p);
65282 return rc;
65283 }
65284 }
65285#if SQLITE_THREADSAFE
65286 mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
65287 sqlite3_mutex_enter(mutexOpen);
65288 mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
65289 sqlite3_mutex_enter(mutexShared);
65290#endif
65291 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
65292 assert( pBt->nRef>0 );
65293 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
65294 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
65295 int iDb;
65296 for(iDb=db->nDb-1; iDb>=0; iDb--){
65297 Btree *pExisting = db->aDb[iDb].pBt;
65298 if( pExisting && pExisting->pBt==pBt ){
65299 sqlite3_mutex_leave(mutexShared);
65300 sqlite3_mutex_leave(mutexOpen);
65301 sqlite3_free(zFullPathname);
65302 sqlite3_free(p);
65303 return SQLITE_CONSTRAINT;
65304 }
65305 }
65306 p->pBt = pBt;
65307 pBt->nRef++;
65308 break;
65309 }
65310 }
65311 sqlite3_mutex_leave(mutexShared);
65312 sqlite3_free(zFullPathname);
65313 }
65314#ifdef SQLITE_DEBUG
65315 else{
65316 /* In debug mode, we mark all persistent databases as sharable
65317 ** even when they are not. This exercises the locking code and
65318 ** gives more opportunity for asserts(sqlite3_mutex_held())
65319 ** statements to find locking problems.
65320 */
65321 p->sharable = 1;
65322 }
65323#endif
65324 }
65325#endif
65326 if( pBt==0 ){
65327 /*
65328 ** The following asserts make sure that structures used by the btree are
65329 ** the right size. This is to guard against size changes that result
65330 ** when compiling on a different architecture.
65331 */
65332 assert( sizeof(i64)==8 );
65333 assert( sizeof(u64)==8 );
65334 assert( sizeof(u32)==4 );
65335 assert( sizeof(u16)==2 );
65336 assert( sizeof(Pgno)==4 );
65337
65338 pBt = sqlite3MallocZero( sizeof(*pBt) );
65339 if( pBt==0 ){
65340 rc = SQLITE_NOMEM_BKPT;
65341 goto btree_open_out;
65342 }
65343 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
65344 sizeof(MemPage), flags, vfsFlags, pageReinit);
65345 if( rc==SQLITE_OK ){
65346 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
65347 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
65348 }
65349 if( rc!=SQLITE_OK ){
65350 goto btree_open_out;
65351 }
65352 pBt->openFlags = (u8)flags;
65353 pBt->db = db;
65354 sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
65355 p->pBt = pBt;
65356
65357 pBt->pCursor = 0;
65358 pBt->pPage1 = 0;
65359 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
65360#if defined(SQLITE_SECURE_DELETE)
65361 pBt->btsFlags |= BTS_SECURE_DELETE;
65362#elif defined(SQLITE_FAST_SECURE_DELETE)
65363 pBt->btsFlags |= BTS_OVERWRITE;
65364#endif
65365 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
65366 ** determined by the 2-byte integer located at an offset of 16 bytes from
65367 ** the beginning of the database file. */
65368 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
65369 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
65370 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
65371 pBt->pageSize = 0;
65372#ifndef SQLITE_OMIT_AUTOVACUUM
65373 /* If the magic name ":memory:" will create an in-memory database, then
65374 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
65375 ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
65376 ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
65377 ** regular file-name. In this case the auto-vacuum applies as per normal.
65378 */
65379 if( zFilename && !isMemdb ){
65380 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
65381 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
65382 }
65383#endif
65384 nReserve = 0;
65385 }else{
65386 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
65387 ** determined by the one-byte unsigned integer found at an offset of 20
65388 ** into the database file header. */
65389 nReserve = zDbHeader[20];
65390 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
65391#ifndef SQLITE_OMIT_AUTOVACUUM
65392 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
65393 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
65394#endif
65395 }
65396 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
65397 if( rc ) goto btree_open_out;
65398 pBt->usableSize = pBt->pageSize - nReserve;
65399 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
65400
65401#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
65402 /* Add the new BtShared object to the linked list sharable BtShareds.
65403 */
65404 pBt->nRef = 1;
65405 if( p->sharable ){
65406 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
65407 MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);)
65408 if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
65409 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
65410 if( pBt->mutex==0 ){
65411 rc = SQLITE_NOMEM_BKPT;
65412 goto btree_open_out;
65413 }
65414 }
65415 sqlite3_mutex_enter(mutexShared);
65416 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
65417 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
65418 sqlite3_mutex_leave(mutexShared);
65419 }
65420#endif
65421 }
65422
65423#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
65424 /* If the new Btree uses a sharable pBtShared, then link the new
65425 ** Btree into the list of all sharable Btrees for the same connection.
65426 ** The list is kept in ascending order by pBt address.
65427 */
65428 if( p->sharable ){
65429 int i;
65430 Btree *pSib;
65431 for(i=0; i<db->nDb; i++){
65432 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
65433 while( pSib->pPrev ){ pSib = pSib->pPrev; }
65434 if( (uptr)p->pBt<(uptr)pSib->pBt ){
65435 p->pNext = pSib;
65436 p->pPrev = 0;
65437 pSib->pPrev = p;
65438 }else{
65439 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
65440 pSib = pSib->pNext;
65441 }
65442 p->pNext = pSib->pNext;
65443 p->pPrev = pSib;
65444 if( p->pNext ){
65445 p->pNext->pPrev = p;
65446 }
65447 pSib->pNext = p;
65448 }
65449 break;
65450 }
65451 }
65452 }
65453#endif
65454 *ppBtree = p;
65455
65456btree_open_out:
65457 if( rc!=SQLITE_OK ){
65458 if( pBt && pBt->pPager ){
65459 sqlite3PagerClose(pBt->pPager, 0);
65460 }
65461 sqlite3_free(pBt);
65462 sqlite3_free(p);
65463 *ppBtree = 0;
65464 }else{
65465 sqlite3_file *pFile;
65466
65467 /* If the B-Tree was successfully opened, set the pager-cache size to the
65468 ** default value. Except, when opening on an existing shared pager-cache,
65469 ** do not change the pager-cache size.
65470 */
65471 if( sqlite3BtreeSchema(p, 0, 0)==0 ){
65472 sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE);
65473 }
65474
65475 pFile = sqlite3PagerFile(pBt->pPager);
65476 if( pFile->pMethods ){
65477 sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB, (void*)&pBt->db);
65478 }
65479 }
65480 if( mutexOpen ){
65481 assert( sqlite3_mutex_held(mutexOpen) );
65482 sqlite3_mutex_leave(mutexOpen);
65483 }
65484 assert( rc!=SQLITE_OK || sqlite3BtreeConnectionCount(*ppBtree)>0 );
65485 return rc;
65486}
65487
65488/*
65489** Decrement the BtShared.nRef counter. When it reaches zero,
65490** remove the BtShared structure from the sharing list. Return
65491** true if the BtShared.nRef counter reaches zero and return
65492** false if it is still positive.
65493*/
65494static int removeFromSharingList(BtShared *pBt){
65495#ifndef SQLITE_OMIT_SHARED_CACHE
65496 MUTEX_LOGIC( sqlite3_mutex *pMaster; )
65497 BtShared *pList;
65498 int removed = 0;
65499
65500 assert( sqlite3_mutex_notheld(pBt->mutex) );
65501 MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
65502 sqlite3_mutex_enter(pMaster);
65503 pBt->nRef--;
65504 if( pBt->nRef<=0 ){
65505 if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
65506 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
65507 }else{
65508 pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
65509 while( ALWAYS(pList) && pList->pNext!=pBt ){
65510 pList=pList->pNext;
65511 }
65512 if( ALWAYS(pList) ){
65513 pList->pNext = pBt->pNext;
65514 }
65515 }
65516 if( SQLITE_THREADSAFE ){
65517 sqlite3_mutex_free(pBt->mutex);
65518 }
65519 removed = 1;
65520 }
65521 sqlite3_mutex_leave(pMaster);
65522 return removed;
65523#else
65524 return 1;
65525#endif
65526}
65527
65528/*
65529** Make sure pBt->pTmpSpace points to an allocation of
65530** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
65531** pointer.
65532*/
65533static void allocateTempSpace(BtShared *pBt){
65534 if( !pBt->pTmpSpace ){
65535 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
65536
65537 /* One of the uses of pBt->pTmpSpace is to format cells before
65538 ** inserting them into a leaf page (function fillInCell()). If
65539 ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes
65540 ** by the various routines that manipulate binary cells. Which
65541 ** can mean that fillInCell() only initializes the first 2 or 3
65542 ** bytes of pTmpSpace, but that the first 4 bytes are copied from
65543 ** it into a database page. This is not actually a problem, but it
65544 ** does cause a valgrind error when the 1 or 2 bytes of unitialized
65545 ** data is passed to system call write(). So to avoid this error,
65546 ** zero the first 4 bytes of temp space here.
65547 **
65548 ** Also: Provide four bytes of initialized space before the
65549 ** beginning of pTmpSpace as an area available to prepend the
65550 ** left-child pointer to the beginning of a cell.
65551 */
65552 if( pBt->pTmpSpace ){
65553 memset(pBt->pTmpSpace, 0, 8);
65554 pBt->pTmpSpace += 4;
65555 }
65556 }
65557}
65558
65559/*
65560** Free the pBt->pTmpSpace allocation
65561*/
65562static void freeTempSpace(BtShared *pBt){
65563 if( pBt->pTmpSpace ){
65564 pBt->pTmpSpace -= 4;
65565 sqlite3PageFree(pBt->pTmpSpace);
65566 pBt->pTmpSpace = 0;
65567 }
65568}
65569
65570/*
65571** Close an open database and invalidate all cursors.
65572*/
65573SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){
65574 BtShared *pBt = p->pBt;
65575 BtCursor *pCur;
65576
65577 /* Close all cursors opened via this handle. */
65578 assert( sqlite3_mutex_held(p->db->mutex) );
65579 sqlite3BtreeEnter(p);
65580 pCur = pBt->pCursor;
65581 while( pCur ){
65582 BtCursor *pTmp = pCur;
65583 pCur = pCur->pNext;
65584 if( pTmp->pBtree==p ){
65585 sqlite3BtreeCloseCursor(pTmp);
65586 }
65587 }
65588
65589 /* Rollback any active transaction and free the handle structure.
65590 ** The call to sqlite3BtreeRollback() drops any table-locks held by
65591 ** this handle.
65592 */
65593 sqlite3BtreeRollback(p, SQLITE_OK, 0);
65594 sqlite3BtreeLeave(p);
65595
65596 /* If there are still other outstanding references to the shared-btree
65597 ** structure, return now. The remainder of this procedure cleans
65598 ** up the shared-btree.
65599 */
65600 assert( p->wantToLock==0 && p->locked==0 );
65601 if( !p->sharable || removeFromSharingList(pBt) ){
65602 /* The pBt is no longer on the sharing list, so we can access
65603 ** it without having to hold the mutex.
65604 **
65605 ** Clean out and delete the BtShared object.
65606 */
65607 assert( !pBt->pCursor );
65608 sqlite3PagerClose(pBt->pPager, p->db);
65609 if( pBt->xFreeSchema && pBt->pSchema ){
65610 pBt->xFreeSchema(pBt->pSchema);
65611 }
65612 sqlite3DbFree(0, pBt->pSchema);
65613 freeTempSpace(pBt);
65614 sqlite3_free(pBt);
65615 }
65616
65617#ifndef SQLITE_OMIT_SHARED_CACHE
65618 assert( p->wantToLock==0 );
65619 assert( p->locked==0 );
65620 if( p->pPrev ) p->pPrev->pNext = p->pNext;
65621 if( p->pNext ) p->pNext->pPrev = p->pPrev;
65622#endif
65623
65624 sqlite3_free(p);
65625 return SQLITE_OK;
65626}
65627
65628/*
65629** Change the "soft" limit on the number of pages in the cache.
65630** Unused and unmodified pages will be recycled when the number of
65631** pages in the cache exceeds this soft limit. But the size of the
65632** cache is allowed to grow larger than this limit if it contains
65633** dirty pages or pages still in active use.
65634*/
65635SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
65636 BtShared *pBt = p->pBt;
65637 assert( sqlite3_mutex_held(p->db->mutex) );
65638 sqlite3BtreeEnter(p);
65639 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
65640 sqlite3BtreeLeave(p);
65641 return SQLITE_OK;
65642}
65643
65644/*
65645** Change the "spill" limit on the number of pages in the cache.
65646** If the number of pages exceeds this limit during a write transaction,
65647** the pager might attempt to "spill" pages to the journal early in
65648** order to free up memory.
65649**
65650** The value returned is the current spill size. If zero is passed
65651** as an argument, no changes are made to the spill size setting, so
65652** using mxPage of 0 is a way to query the current spill size.
65653*/
65654SQLITE_PRIVATE int sqlite3BtreeSetSpillSize(Btree *p, int mxPage){
65655 BtShared *pBt = p->pBt;
65656 int res;
65657 assert( sqlite3_mutex_held(p->db->mutex) );
65658 sqlite3BtreeEnter(p);
65659 res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
65660 sqlite3BtreeLeave(p);
65661 return res;
65662}
65663
65664#if SQLITE_MAX_MMAP_SIZE>0
65665/*
65666** Change the limit on the amount of the database file that may be
65667** memory mapped.
65668*/
65669SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){
65670 BtShared *pBt = p->pBt;
65671 assert( sqlite3_mutex_held(p->db->mutex) );
65672 sqlite3BtreeEnter(p);
65673 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
65674 sqlite3BtreeLeave(p);
65675 return SQLITE_OK;
65676}
65677#endif /* SQLITE_MAX_MMAP_SIZE>0 */
65678
65679/*
65680** Change the way data is synced to disk in order to increase or decrease
65681** how well the database resists damage due to OS crashes and power
65682** failures. Level 1 is the same as asynchronous (no syncs() occur and
65683** there is a high probability of damage) Level 2 is the default. There
65684** is a very low but non-zero probability of damage. Level 3 reduces the
65685** probability of damage to near zero but with a write performance reduction.
65686*/
65687#ifndef SQLITE_OMIT_PAGER_PRAGMAS
65688SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(
65689 Btree *p, /* The btree to set the safety level on */
65690 unsigned pgFlags /* Various PAGER_* flags */
65691){
65692 BtShared *pBt = p->pBt;
65693 assert( sqlite3_mutex_held(p->db->mutex) );
65694 sqlite3BtreeEnter(p);
65695 sqlite3PagerSetFlags(pBt->pPager, pgFlags);
65696 sqlite3BtreeLeave(p);
65697 return SQLITE_OK;
65698}
65699#endif
65700
65701/*
65702** Change the default pages size and the number of reserved bytes per page.
65703** Or, if the page size has already been fixed, return SQLITE_READONLY
65704** without changing anything.
65705**
65706** The page size must be a power of 2 between 512 and 65536. If the page
65707** size supplied does not meet this constraint then the page size is not
65708** changed.
65709**
65710** Page sizes are constrained to be a power of two so that the region
65711** of the database file used for locking (beginning at PENDING_BYTE,
65712** the first byte past the 1GB boundary, 0x40000000) needs to occur
65713** at the beginning of a page.
65714**
65715** If parameter nReserve is less than zero, then the number of reserved
65716** bytes per page is left unchanged.
65717**
65718** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
65719** and autovacuum mode can no longer be changed.
65720*/
65721SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
65722 int rc = SQLITE_OK;
65723 BtShared *pBt = p->pBt;
65724 assert( nReserve>=-1 && nReserve<=255 );
65725 sqlite3BtreeEnter(p);
65726#if SQLITE_HAS_CODEC
65727 if( nReserve>pBt->optimalReserve ) pBt->optimalReserve = (u8)nReserve;
65728#endif
65729 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
65730 sqlite3BtreeLeave(p);
65731 return SQLITE_READONLY;
65732 }
65733 if( nReserve<0 ){
65734 nReserve = pBt->pageSize - pBt->usableSize;
65735 }
65736 assert( nReserve>=0 && nReserve<=255 );
65737 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
65738 ((pageSize-1)&pageSize)==0 ){
65739 assert( (pageSize & 7)==0 );
65740 assert( !pBt->pCursor );
65741 pBt->pageSize = (u32)pageSize;
65742 freeTempSpace(pBt);
65743 }
65744 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
65745 pBt->usableSize = pBt->pageSize - (u16)nReserve;
65746 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
65747 sqlite3BtreeLeave(p);
65748 return rc;
65749}
65750
65751/*
65752** Return the currently defined page size
65753*/
65754SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){
65755 return p->pBt->pageSize;
65756}
65757
65758/*
65759** This function is similar to sqlite3BtreeGetReserve(), except that it
65760** may only be called if it is guaranteed that the b-tree mutex is already
65761** held.
65762**
65763** This is useful in one special case in the backup API code where it is
65764** known that the shared b-tree mutex is held, but the mutex on the
65765** database handle that owns *p is not. In this case if sqlite3BtreeEnter()
65766** were to be called, it might collide with some other operation on the
65767** database handle that owns *p, causing undefined behavior.
65768*/
65769SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p){
65770 int n;
65771 assert( sqlite3_mutex_held(p->pBt->mutex) );
65772 n = p->pBt->pageSize - p->pBt->usableSize;
65773 return n;
65774}
65775
65776/*
65777** Return the number of bytes of space at the end of every page that
65778** are intentually left unused. This is the "reserved" space that is
65779** sometimes used by extensions.
65780**
65781** If SQLITE_HAS_MUTEX is defined then the number returned is the
65782** greater of the current reserved space and the maximum requested
65783** reserve space.
65784*/
65785SQLITE_PRIVATE int sqlite3BtreeGetOptimalReserve(Btree *p){
65786 int n;
65787 sqlite3BtreeEnter(p);
65788 n = sqlite3BtreeGetReserveNoMutex(p);
65789#ifdef SQLITE_HAS_CODEC
65790 if( n<p->pBt->optimalReserve ) n = p->pBt->optimalReserve;
65791#endif
65792 sqlite3BtreeLeave(p);
65793 return n;
65794}
65795
65796
65797/*
65798** Set the maximum page count for a database if mxPage is positive.
65799** No changes are made if mxPage is 0 or negative.
65800** Regardless of the value of mxPage, return the maximum page count.
65801*/
65802SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
65803 int n;
65804 sqlite3BtreeEnter(p);
65805 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
65806 sqlite3BtreeLeave(p);
65807 return n;
65808}
65809
65810/*
65811** Change the values for the BTS_SECURE_DELETE and BTS_OVERWRITE flags:
65812**
65813** newFlag==0 Both BTS_SECURE_DELETE and BTS_OVERWRITE are cleared
65814** newFlag==1 BTS_SECURE_DELETE set and BTS_OVERWRITE is cleared
65815** newFlag==2 BTS_SECURE_DELETE cleared and BTS_OVERWRITE is set
65816** newFlag==(-1) No changes
65817**
65818** This routine acts as a query if newFlag is less than zero
65819**
65820** With BTS_OVERWRITE set, deleted content is overwritten by zeros, but
65821** freelist leaf pages are not written back to the database. Thus in-page
65822** deleted content is cleared, but freelist deleted content is not.
65823**
65824** With BTS_SECURE_DELETE, operation is like BTS_OVERWRITE with the addition
65825** that freelist leaf pages are written back into the database, increasing
65826** the amount of disk I/O.
65827*/
65828SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
65829 int b;
65830 if( p==0 ) return 0;
65831 sqlite3BtreeEnter(p);
65832 assert( BTS_OVERWRITE==BTS_SECURE_DELETE*2 );
65833 assert( BTS_FAST_SECURE==(BTS_OVERWRITE|BTS_SECURE_DELETE) );
65834 if( newFlag>=0 ){
65835 p->pBt->btsFlags &= ~BTS_FAST_SECURE;
65836 p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag;
65837 }
65838 b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
65839 sqlite3BtreeLeave(p);
65840 return b;
65841}
65842
65843/*
65844** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
65845** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
65846** is disabled. The default value for the auto-vacuum property is
65847** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
65848*/
65849SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
65850#ifdef SQLITE_OMIT_AUTOVACUUM
65851 return SQLITE_READONLY;
65852#else
65853 BtShared *pBt = p->pBt;
65854 int rc = SQLITE_OK;
65855 u8 av = (u8)autoVacuum;
65856
65857 sqlite3BtreeEnter(p);
65858 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
65859 rc = SQLITE_READONLY;
65860 }else{
65861 pBt->autoVacuum = av ?1:0;
65862 pBt->incrVacuum = av==2 ?1:0;
65863 }
65864 sqlite3BtreeLeave(p);
65865 return rc;
65866#endif
65867}
65868
65869/*
65870** Return the value of the 'auto-vacuum' property. If auto-vacuum is
65871** enabled 1 is returned. Otherwise 0.
65872*/
65873SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){
65874#ifdef SQLITE_OMIT_AUTOVACUUM
65875 return BTREE_AUTOVACUUM_NONE;
65876#else
65877 int rc;
65878 sqlite3BtreeEnter(p);
65879 rc = (
65880 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
65881 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
65882 BTREE_AUTOVACUUM_INCR
65883 );
65884 sqlite3BtreeLeave(p);
65885 return rc;
65886#endif
65887}
65888
65889/*
65890** If the user has not set the safety-level for this database connection
65891** using "PRAGMA synchronous", and if the safety-level is not already
65892** set to the value passed to this function as the second parameter,
65893** set it so.
65894*/
65895#if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS \
65896 && !defined(SQLITE_OMIT_WAL)
65897static void setDefaultSyncFlag(BtShared *pBt, u8 safety_level){
65898 sqlite3 *db;
65899 Db *pDb;
65900 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
65901 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
65902 if( pDb->bSyncSet==0
65903 && pDb->safety_level!=safety_level
65904 && pDb!=&db->aDb[1]
65905 ){
65906 pDb->safety_level = safety_level;
65907 sqlite3PagerSetFlags(pBt->pPager,
65908 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
65909 }
65910 }
65911}
65912#else
65913# define setDefaultSyncFlag(pBt,safety_level)
65914#endif
65915
65916/* Forward declaration */
65917static int newDatabase(BtShared*);
65918
65919
65920/*
65921** Get a reference to pPage1 of the database file. This will
65922** also acquire a readlock on that file.
65923**
65924** SQLITE_OK is returned on success. If the file is not a
65925** well-formed database file, then SQLITE_CORRUPT is returned.
65926** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
65927** is returned if we run out of memory.
65928*/
65929static int lockBtree(BtShared *pBt){
65930 int rc; /* Result code from subfunctions */
65931 MemPage *pPage1; /* Page 1 of the database file */
65932 int nPage; /* Number of pages in the database */
65933 int nPageFile = 0; /* Number of pages in the database file */
65934 int nPageHeader; /* Number of pages in the database according to hdr */
65935
65936 assert( sqlite3_mutex_held(pBt->mutex) );
65937 assert( pBt->pPage1==0 );
65938 rc = sqlite3PagerSharedLock(pBt->pPager);
65939 if( rc!=SQLITE_OK ) return rc;
65940 rc = btreeGetPage(pBt, 1, &pPage1, 0);
65941 if( rc!=SQLITE_OK ) return rc;
65942
65943 /* Do some checking to help insure the file we opened really is
65944 ** a valid database file.
65945 */
65946 nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
65947 sqlite3PagerPagecount(pBt->pPager, &nPageFile);
65948 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
65949 nPage = nPageFile;
65950 }
65951 if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){
65952 nPage = 0;
65953 }
65954 if( nPage>0 ){
65955 u32 pageSize;
65956 u32 usableSize;
65957 u8 *page1 = pPage1->aData;
65958 rc = SQLITE_NOTADB;
65959 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
65960 ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d
65961 ** 61 74 20 33 00. */
65962 if( memcmp(page1, zMagicHeader, 16)!=0 ){
65963 goto page1_init_failed;
65964 }
65965
65966#ifdef SQLITE_OMIT_WAL
65967 if( page1[18]>1 ){
65968 pBt->btsFlags |= BTS_READ_ONLY;
65969 }
65970 if( page1[19]>1 ){
65971 goto page1_init_failed;
65972 }
65973#else
65974 if( page1[18]>2 ){
65975 pBt->btsFlags |= BTS_READ_ONLY;
65976 }
65977 if( page1[19]>2 ){
65978 goto page1_init_failed;
65979 }
65980
65981 /* If the write version is set to 2, this database should be accessed
65982 ** in WAL mode. If the log is not already open, open it now. Then
65983 ** return SQLITE_OK and return without populating BtShared.pPage1.
65984 ** The caller detects this and calls this function again. This is
65985 ** required as the version of page 1 currently in the page1 buffer
65986 ** may not be the latest version - there may be a newer one in the log
65987 ** file.
65988 */
65989 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
65990 int isOpen = 0;
65991 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
65992 if( rc!=SQLITE_OK ){
65993 goto page1_init_failed;
65994 }else{
65995 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_WAL_SYNCHRONOUS+1);
65996 if( isOpen==0 ){
65997 releasePageOne(pPage1);
65998 return SQLITE_OK;
65999 }
66000 }
66001 rc = SQLITE_NOTADB;
66002 }else{
66003 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_SYNCHRONOUS+1);
66004 }
66005#endif
66006
66007 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
66008 ** fractions and the leaf payload fraction values must be 64, 32, and 32.
66009 **
66010 ** The original design allowed these amounts to vary, but as of
66011 ** version 3.6.0, we require them to be fixed.
66012 */
66013 if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
66014 goto page1_init_failed;
66015 }
66016 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
66017 ** determined by the 2-byte integer located at an offset of 16 bytes from
66018 ** the beginning of the database file. */
66019 pageSize = (page1[16]<<8) | (page1[17]<<16);
66020 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
66021 ** between 512 and 65536 inclusive. */
66022 if( ((pageSize-1)&pageSize)!=0
66023 || pageSize>SQLITE_MAX_PAGE_SIZE
66024 || pageSize<=256
66025 ){
66026 goto page1_init_failed;
66027 }
66028 assert( (pageSize & 7)==0 );
66029 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
66030 ** integer at offset 20 is the number of bytes of space at the end of
66031 ** each page to reserve for extensions.
66032 **
66033 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
66034 ** determined by the one-byte unsigned integer found at an offset of 20
66035 ** into the database file header. */
66036 usableSize = pageSize - page1[20];
66037 if( (u32)pageSize!=pBt->pageSize ){
66038 /* After reading the first page of the database assuming a page size
66039 ** of BtShared.pageSize, we have discovered that the page-size is
66040 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
66041 ** zero and return SQLITE_OK. The caller will call this function
66042 ** again with the correct page-size.
66043 */
66044 releasePageOne(pPage1);
66045 pBt->usableSize = usableSize;
66046 pBt->pageSize = pageSize;
66047 freeTempSpace(pBt);
66048 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
66049 pageSize-usableSize);
66050 return rc;
66051 }
66052 if( sqlite3WritableSchema(pBt->db)==0 && nPage>nPageFile ){
66053 rc = SQLITE_CORRUPT_BKPT;
66054 goto page1_init_failed;
66055 }
66056 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
66057 ** be less than 480. In other words, if the page size is 512, then the
66058 ** reserved space size cannot exceed 32. */
66059 if( usableSize<480 ){
66060 goto page1_init_failed;
66061 }
66062 pBt->pageSize = pageSize;
66063 pBt->usableSize = usableSize;
66064#ifndef SQLITE_OMIT_AUTOVACUUM
66065 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
66066 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
66067#endif
66068 }
66069
66070 /* maxLocal is the maximum amount of payload to store locally for
66071 ** a cell. Make sure it is small enough so that at least minFanout
66072 ** cells can will fit on one page. We assume a 10-byte page header.
66073 ** Besides the payload, the cell must store:
66074 ** 2-byte pointer to the cell
66075 ** 4-byte child pointer
66076 ** 9-byte nKey value
66077 ** 4-byte nData value
66078 ** 4-byte overflow page pointer
66079 ** So a cell consists of a 2-byte pointer, a header which is as much as
66080 ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
66081 ** page pointer.
66082 */
66083 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
66084 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
66085 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
66086 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
66087 if( pBt->maxLocal>127 ){
66088 pBt->max1bytePayload = 127;
66089 }else{
66090 pBt->max1bytePayload = (u8)pBt->maxLocal;
66091 }
66092 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
66093 pBt->pPage1 = pPage1;
66094 pBt->nPage = nPage;
66095 return SQLITE_OK;
66096
66097page1_init_failed:
66098 releasePageOne(pPage1);
66099 pBt->pPage1 = 0;
66100 return rc;
66101}
66102
66103#ifndef NDEBUG
66104/*
66105** Return the number of cursors open on pBt. This is for use
66106** in assert() expressions, so it is only compiled if NDEBUG is not
66107** defined.
66108**
66109** Only write cursors are counted if wrOnly is true. If wrOnly is
66110** false then all cursors are counted.
66111**
66112** For the purposes of this routine, a cursor is any cursor that
66113** is capable of reading or writing to the database. Cursors that
66114** have been tripped into the CURSOR_FAULT state are not counted.
66115*/
66116static int countValidCursors(BtShared *pBt, int wrOnly){
66117 BtCursor *pCur;
66118 int r = 0;
66119 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
66120 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
66121 && pCur->eState!=CURSOR_FAULT ) r++;
66122 }
66123 return r;
66124}
66125#endif
66126
66127/*
66128** If there are no outstanding cursors and we are not in the middle
66129** of a transaction but there is a read lock on the database, then
66130** this routine unrefs the first page of the database file which
66131** has the effect of releasing the read lock.
66132**
66133** If there is a transaction in progress, this routine is a no-op.
66134*/
66135static void unlockBtreeIfUnused(BtShared *pBt){
66136 assert( sqlite3_mutex_held(pBt->mutex) );
66137 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
66138 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
66139 MemPage *pPage1 = pBt->pPage1;
66140 assert( pPage1->aData );
66141 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
66142 pBt->pPage1 = 0;
66143 releasePageOne(pPage1);
66144 }
66145}
66146
66147/*
66148** If pBt points to an empty file then convert that empty file
66149** into a new empty database by initializing the first page of
66150** the database.
66151*/
66152static int newDatabase(BtShared *pBt){
66153 MemPage *pP1;
66154 unsigned char *data;
66155 int rc;
66156
66157 assert( sqlite3_mutex_held(pBt->mutex) );
66158 if( pBt->nPage>0 ){
66159 return SQLITE_OK;
66160 }
66161 pP1 = pBt->pPage1;
66162 assert( pP1!=0 );
66163 data = pP1->aData;
66164 rc = sqlite3PagerWrite(pP1->pDbPage);
66165 if( rc ) return rc;
66166 memcpy(data, zMagicHeader, sizeof(zMagicHeader));
66167 assert( sizeof(zMagicHeader)==16 );
66168 data[16] = (u8)((pBt->pageSize>>8)&0xff);
66169 data[17] = (u8)((pBt->pageSize>>16)&0xff);
66170 data[18] = 1;
66171 data[19] = 1;
66172 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
66173 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
66174 data[21] = 64;
66175 data[22] = 32;
66176 data[23] = 32;
66177 memset(&data[24], 0, 100-24);
66178 zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
66179 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
66180#ifndef SQLITE_OMIT_AUTOVACUUM
66181 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
66182 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
66183 put4byte(&data[36 + 4*4], pBt->autoVacuum);
66184 put4byte(&data[36 + 7*4], pBt->incrVacuum);
66185#endif
66186 pBt->nPage = 1;
66187 data[31] = 1;
66188 return SQLITE_OK;
66189}
66190
66191/*
66192** Initialize the first page of the database file (creating a database
66193** consisting of a single page and no schema objects). Return SQLITE_OK
66194** if successful, or an SQLite error code otherwise.
66195*/
66196SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p){
66197 int rc;
66198 sqlite3BtreeEnter(p);
66199 p->pBt->nPage = 0;
66200 rc = newDatabase(p->pBt);
66201 sqlite3BtreeLeave(p);
66202 return rc;
66203}
66204
66205/*
66206** Attempt to start a new transaction. A write-transaction
66207** is started if the second argument is nonzero, otherwise a read-
66208** transaction. If the second argument is 2 or more and exclusive
66209** transaction is started, meaning that no other process is allowed
66210** to access the database. A preexisting transaction may not be
66211** upgraded to exclusive by calling this routine a second time - the
66212** exclusivity flag only works for a new transaction.
66213**
66214** A write-transaction must be started before attempting any
66215** changes to the database. None of the following routines
66216** will work unless a transaction is started first:
66217**
66218** sqlite3BtreeCreateTable()
66219** sqlite3BtreeCreateIndex()
66220** sqlite3BtreeClearTable()
66221** sqlite3BtreeDropTable()
66222** sqlite3BtreeInsert()
66223** sqlite3BtreeDelete()
66224** sqlite3BtreeUpdateMeta()
66225**
66226** If an initial attempt to acquire the lock fails because of lock contention
66227** and the database was previously unlocked, then invoke the busy handler
66228** if there is one. But if there was previously a read-lock, do not
66229** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
66230** returned when there is already a read-lock in order to avoid a deadlock.
66231**
66232** Suppose there are two processes A and B. A has a read lock and B has
66233** a reserved lock. B tries to promote to exclusive but is blocked because
66234** of A's read lock. A tries to promote to reserved but is blocked by B.
66235** One or the other of the two processes must give way or there can be
66236** no progress. By returning SQLITE_BUSY and not invoking the busy callback
66237** when A already has a read lock, we encourage A to give up and let B
66238** proceed.
66239*/
66240SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){
66241 BtShared *pBt = p->pBt;
66242 int rc = SQLITE_OK;
66243
66244 sqlite3BtreeEnter(p);
66245 btreeIntegrity(p);
66246
66247 /* If the btree is already in a write-transaction, or it
66248 ** is already in a read-transaction and a read-transaction
66249 ** is requested, this is a no-op.
66250 */
66251 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
66252 goto trans_begun;
66253 }
66254 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
66255
66256 if( (p->db->flags & SQLITE_ResetDatabase)
66257 && sqlite3PagerIsreadonly(pBt->pPager)==0
66258 ){
66259 pBt->btsFlags &= ~BTS_READ_ONLY;
66260 }
66261
66262 /* Write transactions are not possible on a read-only database */
66263 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
66264 rc = SQLITE_READONLY;
66265 goto trans_begun;
66266 }
66267
66268#ifndef SQLITE_OMIT_SHARED_CACHE
66269 {
66270 sqlite3 *pBlock = 0;
66271 /* If another database handle has already opened a write transaction
66272 ** on this shared-btree structure and a second write transaction is
66273 ** requested, return SQLITE_LOCKED.
66274 */
66275 if( (wrflag && pBt->inTransaction==TRANS_WRITE)
66276 || (pBt->btsFlags & BTS_PENDING)!=0
66277 ){
66278 pBlock = pBt->pWriter->db;
66279 }else if( wrflag>1 ){
66280 BtLock *pIter;
66281 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
66282 if( pIter->pBtree!=p ){
66283 pBlock = pIter->pBtree->db;
66284 break;
66285 }
66286 }
66287 }
66288 if( pBlock ){
66289 sqlite3ConnectionBlocked(p->db, pBlock);
66290 rc = SQLITE_LOCKED_SHAREDCACHE;
66291 goto trans_begun;
66292 }
66293 }
66294#endif
66295
66296 /* Any read-only or read-write transaction implies a read-lock on
66297 ** page 1. So if some other shared-cache client already has a write-lock
66298 ** on page 1, the transaction cannot be opened. */
66299 rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
66300 if( SQLITE_OK!=rc ) goto trans_begun;
66301
66302 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
66303 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
66304 do {
66305 /* Call lockBtree() until either pBt->pPage1 is populated or
66306 ** lockBtree() returns something other than SQLITE_OK. lockBtree()
66307 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
66308 ** reading page 1 it discovers that the page-size of the database
66309 ** file is not pBt->pageSize. In this case lockBtree() will update
66310 ** pBt->pageSize to the page-size of the file on disk.
66311 */
66312 while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
66313
66314 if( rc==SQLITE_OK && wrflag ){
66315 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
66316 rc = SQLITE_READONLY;
66317 }else{
66318 rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
66319 if( rc==SQLITE_OK ){
66320 rc = newDatabase(pBt);
66321 }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){
66322 /* if there was no transaction opened when this function was
66323 ** called and SQLITE_BUSY_SNAPSHOT is returned, change the error
66324 ** code to SQLITE_BUSY. */
66325 rc = SQLITE_BUSY;
66326 }
66327 }
66328 }
66329
66330 if( rc!=SQLITE_OK ){
66331 unlockBtreeIfUnused(pBt);
66332 }
66333 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
66334 btreeInvokeBusyHandler(pBt) );
66335 sqlite3PagerResetLockTimeout(pBt->pPager);
66336
66337 if( rc==SQLITE_OK ){
66338 if( p->inTrans==TRANS_NONE ){
66339 pBt->nTransaction++;
66340#ifndef SQLITE_OMIT_SHARED_CACHE
66341 if( p->sharable ){
66342 assert( p->lock.pBtree==p && p->lock.iTable==1 );
66343 p->lock.eLock = READ_LOCK;
66344 p->lock.pNext = pBt->pLock;
66345 pBt->pLock = &p->lock;
66346 }
66347#endif
66348 }
66349 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
66350 if( p->inTrans>pBt->inTransaction ){
66351 pBt->inTransaction = p->inTrans;
66352 }
66353 if( wrflag ){
66354 MemPage *pPage1 = pBt->pPage1;
66355#ifndef SQLITE_OMIT_SHARED_CACHE
66356 assert( !pBt->pWriter );
66357 pBt->pWriter = p;
66358 pBt->btsFlags &= ~BTS_EXCLUSIVE;
66359 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
66360#endif
66361
66362 /* If the db-size header field is incorrect (as it may be if an old
66363 ** client has been writing the database file), update it now. Doing
66364 ** this sooner rather than later means the database size can safely
66365 ** re-read the database size from page 1 if a savepoint or transaction
66366 ** rollback occurs within the transaction.
66367 */
66368 if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
66369 rc = sqlite3PagerWrite(pPage1->pDbPage);
66370 if( rc==SQLITE_OK ){
66371 put4byte(&pPage1->aData[28], pBt->nPage);
66372 }
66373 }
66374 }
66375 }
66376
66377trans_begun:
66378 if( rc==SQLITE_OK ){
66379 if( pSchemaVersion ){
66380 *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]);
66381 }
66382 if( wrflag ){
66383 /* This call makes sure that the pager has the correct number of
66384 ** open savepoints. If the second parameter is greater than 0 and
66385 ** the sub-journal is not already open, then it will be opened here.
66386 */
66387 rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
66388 }
66389 }
66390
66391 btreeIntegrity(p);
66392 sqlite3BtreeLeave(p);
66393 return rc;
66394}
66395
66396#ifndef SQLITE_OMIT_AUTOVACUUM
66397
66398/*
66399** Set the pointer-map entries for all children of page pPage. Also, if
66400** pPage contains cells that point to overflow pages, set the pointer
66401** map entries for the overflow pages as well.
66402*/
66403static int setChildPtrmaps(MemPage *pPage){
66404 int i; /* Counter variable */
66405 int nCell; /* Number of cells in page pPage */
66406 int rc; /* Return code */
66407 BtShared *pBt = pPage->pBt;
66408 Pgno pgno = pPage->pgno;
66409
66410 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66411 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
66412 if( rc!=SQLITE_OK ) return rc;
66413 nCell = pPage->nCell;
66414
66415 for(i=0; i<nCell; i++){
66416 u8 *pCell = findCell(pPage, i);
66417
66418 ptrmapPutOvflPtr(pPage, pCell, &rc);
66419
66420 if( !pPage->leaf ){
66421 Pgno childPgno = get4byte(pCell);
66422 ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
66423 }
66424 }
66425
66426 if( !pPage->leaf ){
66427 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
66428 ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
66429 }
66430
66431 return rc;
66432}
66433
66434/*
66435** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so
66436** that it points to iTo. Parameter eType describes the type of pointer to
66437** be modified, as follows:
66438**
66439** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
66440** page of pPage.
66441**
66442** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
66443** page pointed to by one of the cells on pPage.
66444**
66445** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
66446** overflow page in the list.
66447*/
66448static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
66449 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66450 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
66451 if( eType==PTRMAP_OVERFLOW2 ){
66452 /* The pointer is always the first 4 bytes of the page in this case. */
66453 if( get4byte(pPage->aData)!=iFrom ){
66454 return SQLITE_CORRUPT_PAGE(pPage);
66455 }
66456 put4byte(pPage->aData, iTo);
66457 }else{
66458 int i;
66459 int nCell;
66460 int rc;
66461
66462 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
66463 if( rc ) return rc;
66464 nCell = pPage->nCell;
66465
66466 for(i=0; i<nCell; i++){
66467 u8 *pCell = findCell(pPage, i);
66468 if( eType==PTRMAP_OVERFLOW1 ){
66469 CellInfo info;
66470 pPage->xParseCell(pPage, pCell, &info);
66471 if( info.nLocal<info.nPayload ){
66472 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
66473 return SQLITE_CORRUPT_PAGE(pPage);
66474 }
66475 if( iFrom==get4byte(pCell+info.nSize-4) ){
66476 put4byte(pCell+info.nSize-4, iTo);
66477 break;
66478 }
66479 }
66480 }else{
66481 if( get4byte(pCell)==iFrom ){
66482 put4byte(pCell, iTo);
66483 break;
66484 }
66485 }
66486 }
66487
66488 if( i==nCell ){
66489 if( eType!=PTRMAP_BTREE ||
66490 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
66491 return SQLITE_CORRUPT_PAGE(pPage);
66492 }
66493 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
66494 }
66495 }
66496 return SQLITE_OK;
66497}
66498
66499
66500/*
66501** Move the open database page pDbPage to location iFreePage in the
66502** database. The pDbPage reference remains valid.
66503**
66504** The isCommit flag indicates that there is no need to remember that
66505** the journal needs to be sync()ed before database page pDbPage->pgno
66506** can be written to. The caller has already promised not to write to that
66507** page.
66508*/
66509static int relocatePage(
66510 BtShared *pBt, /* Btree */
66511 MemPage *pDbPage, /* Open page to move */
66512 u8 eType, /* Pointer map 'type' entry for pDbPage */
66513 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
66514 Pgno iFreePage, /* The location to move pDbPage to */
66515 int isCommit /* isCommit flag passed to sqlite3PagerMovepage */
66516){
66517 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
66518 Pgno iDbPage = pDbPage->pgno;
66519 Pager *pPager = pBt->pPager;
66520 int rc;
66521
66522 assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
66523 eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
66524 assert( sqlite3_mutex_held(pBt->mutex) );
66525 assert( pDbPage->pBt==pBt );
66526 if( iDbPage<3 ) return SQLITE_CORRUPT_BKPT;
66527
66528 /* Move page iDbPage from its current location to page number iFreePage */
66529 TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
66530 iDbPage, iFreePage, iPtrPage, eType));
66531 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
66532 if( rc!=SQLITE_OK ){
66533 return rc;
66534 }
66535 pDbPage->pgno = iFreePage;
66536
66537 /* If pDbPage was a btree-page, then it may have child pages and/or cells
66538 ** that point to overflow pages. The pointer map entries for all these
66539 ** pages need to be changed.
66540 **
66541 ** If pDbPage is an overflow page, then the first 4 bytes may store a
66542 ** pointer to a subsequent overflow page. If this is the case, then
66543 ** the pointer map needs to be updated for the subsequent overflow page.
66544 */
66545 if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
66546 rc = setChildPtrmaps(pDbPage);
66547 if( rc!=SQLITE_OK ){
66548 return rc;
66549 }
66550 }else{
66551 Pgno nextOvfl = get4byte(pDbPage->aData);
66552 if( nextOvfl!=0 ){
66553 ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc);
66554 if( rc!=SQLITE_OK ){
66555 return rc;
66556 }
66557 }
66558 }
66559
66560 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
66561 ** that it points at iFreePage. Also fix the pointer map entry for
66562 ** iPtrPage.
66563 */
66564 if( eType!=PTRMAP_ROOTPAGE ){
66565 rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
66566 if( rc!=SQLITE_OK ){
66567 return rc;
66568 }
66569 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
66570 if( rc!=SQLITE_OK ){
66571 releasePage(pPtrPage);
66572 return rc;
66573 }
66574 rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
66575 releasePage(pPtrPage);
66576 if( rc==SQLITE_OK ){
66577 ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
66578 }
66579 }
66580 return rc;
66581}
66582
66583/* Forward declaration required by incrVacuumStep(). */
66584static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
66585
66586/*
66587** Perform a single step of an incremental-vacuum. If successful, return
66588** SQLITE_OK. If there is no work to do (and therefore no point in
66589** calling this function again), return SQLITE_DONE. Or, if an error
66590** occurs, return some other error code.
66591**
66592** More specifically, this function attempts to re-organize the database so
66593** that the last page of the file currently in use is no longer in use.
66594**
66595** Parameter nFin is the number of pages that this database would contain
66596** were this function called until it returns SQLITE_DONE.
66597**
66598** If the bCommit parameter is non-zero, this function assumes that the
66599** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE
66600** or an error. bCommit is passed true for an auto-vacuum-on-commit
66601** operation, or false for an incremental vacuum.
66602*/
66603static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){
66604 Pgno nFreeList; /* Number of pages still on the free-list */
66605 int rc;
66606
66607 assert( sqlite3_mutex_held(pBt->mutex) );
66608 assert( iLastPg>nFin );
66609
66610 if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
66611 u8 eType;
66612 Pgno iPtrPage;
66613
66614 nFreeList = get4byte(&pBt->pPage1->aData[36]);
66615 if( nFreeList==0 ){
66616 return SQLITE_DONE;
66617 }
66618
66619 rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
66620 if( rc!=SQLITE_OK ){
66621 return rc;
66622 }
66623 if( eType==PTRMAP_ROOTPAGE ){
66624 return SQLITE_CORRUPT_BKPT;
66625 }
66626
66627 if( eType==PTRMAP_FREEPAGE ){
66628 if( bCommit==0 ){
66629 /* Remove the page from the files free-list. This is not required
66630 ** if bCommit is non-zero. In that case, the free-list will be
66631 ** truncated to zero after this function returns, so it doesn't
66632 ** matter if it still contains some garbage entries.
66633 */
66634 Pgno iFreePg;
66635 MemPage *pFreePg;
66636 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, BTALLOC_EXACT);
66637 if( rc!=SQLITE_OK ){
66638 return rc;
66639 }
66640 assert( iFreePg==iLastPg );
66641 releasePage(pFreePg);
66642 }
66643 } else {
66644 Pgno iFreePg; /* Index of free page to move pLastPg to */
66645 MemPage *pLastPg;
66646 u8 eMode = BTALLOC_ANY; /* Mode parameter for allocateBtreePage() */
66647 Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */
66648
66649 rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
66650 if( rc!=SQLITE_OK ){
66651 return rc;
66652 }
66653
66654 /* If bCommit is zero, this loop runs exactly once and page pLastPg
66655 ** is swapped with the first free page pulled off the free list.
66656 **
66657 ** On the other hand, if bCommit is greater than zero, then keep
66658 ** looping until a free-page located within the first nFin pages
66659 ** of the file is found.
66660 */
66661 if( bCommit==0 ){
66662 eMode = BTALLOC_LE;
66663 iNear = nFin;
66664 }
66665 do {
66666 MemPage *pFreePg;
66667 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode);
66668 if( rc!=SQLITE_OK ){
66669 releasePage(pLastPg);
66670 return rc;
66671 }
66672 releasePage(pFreePg);
66673 }while( bCommit && iFreePg>nFin );
66674 assert( iFreePg<iLastPg );
66675
66676 rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, bCommit);
66677 releasePage(pLastPg);
66678 if( rc!=SQLITE_OK ){
66679 return rc;
66680 }
66681 }
66682 }
66683
66684 if( bCommit==0 ){
66685 do {
66686 iLastPg--;
66687 }while( iLastPg==PENDING_BYTE_PAGE(pBt) || PTRMAP_ISPAGE(pBt, iLastPg) );
66688 pBt->bDoTruncate = 1;
66689 pBt->nPage = iLastPg;
66690 }
66691 return SQLITE_OK;
66692}
66693
66694/*
66695** The database opened by the first argument is an auto-vacuum database
66696** nOrig pages in size containing nFree free pages. Return the expected
66697** size of the database in pages following an auto-vacuum operation.
66698*/
66699static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){
66700 int nEntry; /* Number of entries on one ptrmap page */
66701 Pgno nPtrmap; /* Number of PtrMap pages to be freed */
66702 Pgno nFin; /* Return value */
66703
66704 nEntry = pBt->usableSize/5;
66705 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
66706 nFin = nOrig - nFree - nPtrmap;
66707 if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){
66708 nFin--;
66709 }
66710 while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
66711 nFin--;
66712 }
66713
66714 return nFin;
66715}
66716
66717/*
66718** A write-transaction must be opened before calling this function.
66719** It performs a single unit of work towards an incremental vacuum.
66720**
66721** If the incremental vacuum is finished after this function has run,
66722** SQLITE_DONE is returned. If it is not finished, but no error occurred,
66723** SQLITE_OK is returned. Otherwise an SQLite error code.
66724*/
66725SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *p){
66726 int rc;
66727 BtShared *pBt = p->pBt;
66728
66729 sqlite3BtreeEnter(p);
66730 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
66731 if( !pBt->autoVacuum ){
66732 rc = SQLITE_DONE;
66733 }else{
66734 Pgno nOrig = btreePagecount(pBt);
66735 Pgno nFree = get4byte(&pBt->pPage1->aData[36]);
66736 Pgno nFin = finalDbSize(pBt, nOrig, nFree);
66737
66738 if( nOrig<nFin ){
66739 rc = SQLITE_CORRUPT_BKPT;
66740 }else if( nFree>0 ){
66741 rc = saveAllCursors(pBt, 0, 0);
66742 if( rc==SQLITE_OK ){
66743 invalidateAllOverflowCache(pBt);
66744 rc = incrVacuumStep(pBt, nFin, nOrig, 0);
66745 }
66746 if( rc==SQLITE_OK ){
66747 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
66748 put4byte(&pBt->pPage1->aData[28], pBt->nPage);
66749 }
66750 }else{
66751 rc = SQLITE_DONE;
66752 }
66753 }
66754 sqlite3BtreeLeave(p);
66755 return rc;
66756}
66757
66758/*
66759** This routine is called prior to sqlite3PagerCommit when a transaction
66760** is committed for an auto-vacuum database.
66761**
66762** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
66763** the database file should be truncated to during the commit process.
66764** i.e. the database has been reorganized so that only the first *pnTrunc
66765** pages are in use.
66766*/
66767static int autoVacuumCommit(BtShared *pBt){
66768 int rc = SQLITE_OK;
66769 Pager *pPager = pBt->pPager;
66770 VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager); )
66771
66772 assert( sqlite3_mutex_held(pBt->mutex) );
66773 invalidateAllOverflowCache(pBt);
66774 assert(pBt->autoVacuum);
66775 if( !pBt->incrVacuum ){
66776 Pgno nFin; /* Number of pages in database after autovacuuming */
66777 Pgno nFree; /* Number of pages on the freelist initially */
66778 Pgno iFree; /* The next page to be freed */
66779 Pgno nOrig; /* Database size before freeing */
66780
66781 nOrig = btreePagecount(pBt);
66782 if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){
66783 /* It is not possible to create a database for which the final page
66784 ** is either a pointer-map page or the pending-byte page. If one
66785 ** is encountered, this indicates corruption.
66786 */
66787 return SQLITE_CORRUPT_BKPT;
66788 }
66789
66790 nFree = get4byte(&pBt->pPage1->aData[36]);
66791 nFin = finalDbSize(pBt, nOrig, nFree);
66792 if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
66793 if( nFin<nOrig ){
66794 rc = saveAllCursors(pBt, 0, 0);
66795 }
66796 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
66797 rc = incrVacuumStep(pBt, nFin, iFree, 1);
66798 }
66799 if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
66800 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
66801 put4byte(&pBt->pPage1->aData[32], 0);
66802 put4byte(&pBt->pPage1->aData[36], 0);
66803 put4byte(&pBt->pPage1->aData[28], nFin);
66804 pBt->bDoTruncate = 1;
66805 pBt->nPage = nFin;
66806 }
66807 if( rc!=SQLITE_OK ){
66808 sqlite3PagerRollback(pPager);
66809 }
66810 }
66811
66812 assert( nRef>=sqlite3PagerRefcount(pPager) );
66813 return rc;
66814}
66815
66816#else /* ifndef SQLITE_OMIT_AUTOVACUUM */
66817# define setChildPtrmaps(x) SQLITE_OK
66818#endif
66819
66820/*
66821** This routine does the first phase of a two-phase commit. This routine
66822** causes a rollback journal to be created (if it does not already exist)
66823** and populated with enough information so that if a power loss occurs
66824** the database can be restored to its original state by playing back
66825** the journal. Then the contents of the journal are flushed out to
66826** the disk. After the journal is safely on oxide, the changes to the
66827** database are written into the database file and flushed to oxide.
66828** At the end of this call, the rollback journal still exists on the
66829** disk and we are still holding all locks, so the transaction has not
66830** committed. See sqlite3BtreeCommitPhaseTwo() for the second phase of the
66831** commit process.
66832**
66833** This call is a no-op if no write-transaction is currently active on pBt.
66834**
66835** Otherwise, sync the database file for the btree pBt. zMaster points to
66836** the name of a master journal file that should be written into the
66837** individual journal file, or is NULL, indicating no master journal file
66838** (single database transaction).
66839**
66840** When this is called, the master journal should already have been
66841** created, populated with this journal pointer and synced to disk.
66842**
66843** Once this is routine has returned, the only thing required to commit
66844** the write-transaction for this database file is to delete the journal.
66845*/
66846SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
66847 int rc = SQLITE_OK;
66848 if( p->inTrans==TRANS_WRITE ){
66849 BtShared *pBt = p->pBt;
66850 sqlite3BtreeEnter(p);
66851#ifndef SQLITE_OMIT_AUTOVACUUM
66852 if( pBt->autoVacuum ){
66853 rc = autoVacuumCommit(pBt);
66854 if( rc!=SQLITE_OK ){
66855 sqlite3BtreeLeave(p);
66856 return rc;
66857 }
66858 }
66859 if( pBt->bDoTruncate ){
66860 sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
66861 }
66862#endif
66863 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
66864 sqlite3BtreeLeave(p);
66865 }
66866 return rc;
66867}
66868
66869/*
66870** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
66871** at the conclusion of a transaction.
66872*/
66873static void btreeEndTransaction(Btree *p){
66874 BtShared *pBt = p->pBt;
66875 sqlite3 *db = p->db;
66876 assert( sqlite3BtreeHoldsMutex(p) );
66877
66878#ifndef SQLITE_OMIT_AUTOVACUUM
66879 pBt->bDoTruncate = 0;
66880#endif
66881 if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
66882 /* If there are other active statements that belong to this database
66883 ** handle, downgrade to a read-only transaction. The other statements
66884 ** may still be reading from the database. */
66885 downgradeAllSharedCacheTableLocks(p);
66886 p->inTrans = TRANS_READ;
66887 }else{
66888 /* If the handle had any kind of transaction open, decrement the
66889 ** transaction count of the shared btree. If the transaction count
66890 ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
66891 ** call below will unlock the pager. */
66892 if( p->inTrans!=TRANS_NONE ){
66893 clearAllSharedCacheTableLocks(p);
66894 pBt->nTransaction--;
66895 if( 0==pBt->nTransaction ){
66896 pBt->inTransaction = TRANS_NONE;
66897 }
66898 }
66899
66900 /* Set the current transaction state to TRANS_NONE and unlock the
66901 ** pager if this call closed the only read or write transaction. */
66902 p->inTrans = TRANS_NONE;
66903 unlockBtreeIfUnused(pBt);
66904 }
66905
66906 btreeIntegrity(p);
66907}
66908
66909/*
66910** Commit the transaction currently in progress.
66911**
66912** This routine implements the second phase of a 2-phase commit. The
66913** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
66914** be invoked prior to calling this routine. The sqlite3BtreeCommitPhaseOne()
66915** routine did all the work of writing information out to disk and flushing the
66916** contents so that they are written onto the disk platter. All this
66917** routine has to do is delete or truncate or zero the header in the
66918** the rollback journal (which causes the transaction to commit) and
66919** drop locks.
66920**
66921** Normally, if an error occurs while the pager layer is attempting to
66922** finalize the underlying journal file, this function returns an error and
66923** the upper layer will attempt a rollback. However, if the second argument
66924** is non-zero then this b-tree transaction is part of a multi-file
66925** transaction. In this case, the transaction has already been committed
66926** (by deleting a master journal file) and the caller will ignore this
66927** functions return code. So, even if an error occurs in the pager layer,
66928** reset the b-tree objects internal state to indicate that the write
66929** transaction has been closed. This is quite safe, as the pager will have
66930** transitioned to the error state.
66931**
66932** This will release the write lock on the database file. If there
66933** are no active cursors, it also releases the read lock.
66934*/
66935SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
66936
66937 if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
66938 sqlite3BtreeEnter(p);
66939 btreeIntegrity(p);
66940
66941 /* If the handle has a write-transaction open, commit the shared-btrees
66942 ** transaction and set the shared state to TRANS_READ.
66943 */
66944 if( p->inTrans==TRANS_WRITE ){
66945 int rc;
66946 BtShared *pBt = p->pBt;
66947 assert( pBt->inTransaction==TRANS_WRITE );
66948 assert( pBt->nTransaction>0 );
66949 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
66950 if( rc!=SQLITE_OK && bCleanup==0 ){
66951 sqlite3BtreeLeave(p);
66952 return rc;
66953 }
66954 p->iDataVersion--; /* Compensate for pPager->iDataVersion++; */
66955 pBt->inTransaction = TRANS_READ;
66956 btreeClearHasContent(pBt);
66957 }
66958
66959 btreeEndTransaction(p);
66960 sqlite3BtreeLeave(p);
66961 return SQLITE_OK;
66962}
66963
66964/*
66965** Do both phases of a commit.
66966*/
66967SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){
66968 int rc;
66969 sqlite3BtreeEnter(p);
66970 rc = sqlite3BtreeCommitPhaseOne(p, 0);
66971 if( rc==SQLITE_OK ){
66972 rc = sqlite3BtreeCommitPhaseTwo(p, 0);
66973 }
66974 sqlite3BtreeLeave(p);
66975 return rc;
66976}
66977
66978/*
66979** This routine sets the state to CURSOR_FAULT and the error
66980** code to errCode for every cursor on any BtShared that pBtree
66981** references. Or if the writeOnly flag is set to 1, then only
66982** trip write cursors and leave read cursors unchanged.
66983**
66984** Every cursor is a candidate to be tripped, including cursors
66985** that belong to other database connections that happen to be
66986** sharing the cache with pBtree.
66987**
66988** This routine gets called when a rollback occurs. If the writeOnly
66989** flag is true, then only write-cursors need be tripped - read-only
66990** cursors save their current positions so that they may continue
66991** following the rollback. Or, if writeOnly is false, all cursors are
66992** tripped. In general, writeOnly is false if the transaction being
66993** rolled back modified the database schema. In this case b-tree root
66994** pages may be moved or deleted from the database altogether, making
66995** it unsafe for read cursors to continue.
66996**
66997** If the writeOnly flag is true and an error is encountered while
66998** saving the current position of a read-only cursor, all cursors,
66999** including all read-cursors are tripped.
67000**
67001** SQLITE_OK is returned if successful, or if an error occurs while
67002** saving a cursor position, an SQLite error code.
67003*/
67004SQLITE_PRIVATE int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){
67005 BtCursor *p;
67006 int rc = SQLITE_OK;
67007
67008 assert( (writeOnly==0 || writeOnly==1) && BTCF_WriteFlag==1 );
67009 if( pBtree ){
67010 sqlite3BtreeEnter(pBtree);
67011 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
67012 if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
67013 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
67014 rc = saveCursorPosition(p);
67015 if( rc!=SQLITE_OK ){
67016 (void)sqlite3BtreeTripAllCursors(pBtree, rc, 0);
67017 break;
67018 }
67019 }
67020 }else{
67021 sqlite3BtreeClearCursor(p);
67022 p->eState = CURSOR_FAULT;
67023 p->skipNext = errCode;
67024 }
67025 btreeReleaseAllCursorPages(p);
67026 }
67027 sqlite3BtreeLeave(pBtree);
67028 }
67029 return rc;
67030}
67031
67032/*
67033** Rollback the transaction in progress.
67034**
67035** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped).
67036** Only write cursors are tripped if writeOnly is true but all cursors are
67037** tripped if writeOnly is false. Any attempt to use
67038** a tripped cursor will result in an error.
67039**
67040** This will release the write lock on the database file. If there
67041** are no active cursors, it also releases the read lock.
67042*/
67043SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){
67044 int rc;
67045 BtShared *pBt = p->pBt;
67046 MemPage *pPage1;
67047
67048 assert( writeOnly==1 || writeOnly==0 );
67049 assert( tripCode==SQLITE_ABORT_ROLLBACK || tripCode==SQLITE_OK );
67050 sqlite3BtreeEnter(p);
67051 if( tripCode==SQLITE_OK ){
67052 rc = tripCode = saveAllCursors(pBt, 0, 0);
67053 if( rc ) writeOnly = 0;
67054 }else{
67055 rc = SQLITE_OK;
67056 }
67057 if( tripCode ){
67058 int rc2 = sqlite3BtreeTripAllCursors(p, tripCode, writeOnly);
67059 assert( rc==SQLITE_OK || (writeOnly==0 && rc2==SQLITE_OK) );
67060 if( rc2!=SQLITE_OK ) rc = rc2;
67061 }
67062 btreeIntegrity(p);
67063
67064 if( p->inTrans==TRANS_WRITE ){
67065 int rc2;
67066
67067 assert( TRANS_WRITE==pBt->inTransaction );
67068 rc2 = sqlite3PagerRollback(pBt->pPager);
67069 if( rc2!=SQLITE_OK ){
67070 rc = rc2;
67071 }
67072
67073 /* The rollback may have destroyed the pPage1->aData value. So
67074 ** call btreeGetPage() on page 1 again to make
67075 ** sure pPage1->aData is set correctly. */
67076 if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
67077 int nPage = get4byte(28+(u8*)pPage1->aData);
67078 testcase( nPage==0 );
67079 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
67080 testcase( pBt->nPage!=nPage );
67081 pBt->nPage = nPage;
67082 releasePageOne(pPage1);
67083 }
67084 assert( countValidCursors(pBt, 1)==0 );
67085 pBt->inTransaction = TRANS_READ;
67086 btreeClearHasContent(pBt);
67087 }
67088
67089 btreeEndTransaction(p);
67090 sqlite3BtreeLeave(p);
67091 return rc;
67092}
67093
67094/*
67095** Start a statement subtransaction. The subtransaction can be rolled
67096** back independently of the main transaction. You must start a transaction
67097** before starting a subtransaction. The subtransaction is ended automatically
67098** if the main transaction commits or rolls back.
67099**
67100** Statement subtransactions are used around individual SQL statements
67101** that are contained within a BEGIN...COMMIT block. If a constraint
67102** error occurs within the statement, the effect of that one statement
67103** can be rolled back without having to rollback the entire transaction.
67104**
67105** A statement sub-transaction is implemented as an anonymous savepoint. The
67106** value passed as the second parameter is the total number of savepoints,
67107** including the new anonymous savepoint, open on the B-Tree. i.e. if there
67108** are no active savepoints and no other statement-transactions open,
67109** iStatement is 1. This anonymous savepoint can be released or rolled back
67110** using the sqlite3BtreeSavepoint() function.
67111*/
67112SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
67113 int rc;
67114 BtShared *pBt = p->pBt;
67115 sqlite3BtreeEnter(p);
67116 assert( p->inTrans==TRANS_WRITE );
67117 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
67118 assert( iStatement>0 );
67119 assert( iStatement>p->db->nSavepoint );
67120 assert( pBt->inTransaction==TRANS_WRITE );
67121 /* At the pager level, a statement transaction is a savepoint with
67122 ** an index greater than all savepoints created explicitly using
67123 ** SQL statements. It is illegal to open, release or rollback any
67124 ** such savepoints while the statement transaction savepoint is active.
67125 */
67126 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
67127 sqlite3BtreeLeave(p);
67128 return rc;
67129}
67130
67131/*
67132** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
67133** or SAVEPOINT_RELEASE. This function either releases or rolls back the
67134** savepoint identified by parameter iSavepoint, depending on the value
67135** of op.
67136**
67137** Normally, iSavepoint is greater than or equal to zero. However, if op is
67138** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
67139** contents of the entire transaction are rolled back. This is different
67140** from a normal transaction rollback, as no locks are released and the
67141** transaction remains open.
67142*/
67143SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
67144 int rc = SQLITE_OK;
67145 if( p && p->inTrans==TRANS_WRITE ){
67146 BtShared *pBt = p->pBt;
67147 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
67148 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
67149 sqlite3BtreeEnter(p);
67150 if( op==SAVEPOINT_ROLLBACK ){
67151 rc = saveAllCursors(pBt, 0, 0);
67152 }
67153 if( rc==SQLITE_OK ){
67154 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
67155 }
67156 if( rc==SQLITE_OK ){
67157 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
67158 pBt->nPage = 0;
67159 }
67160 rc = newDatabase(pBt);
67161 pBt->nPage = get4byte(28 + pBt->pPage1->aData);
67162
67163 /* The database size was written into the offset 28 of the header
67164 ** when the transaction started, so we know that the value at offset
67165 ** 28 is nonzero. */
67166 assert( pBt->nPage>0 );
67167 }
67168 sqlite3BtreeLeave(p);
67169 }
67170 return rc;
67171}
67172
67173/*
67174** Create a new cursor for the BTree whose root is on the page
67175** iTable. If a read-only cursor is requested, it is assumed that
67176** the caller already has at least a read-only transaction open
67177** on the database already. If a write-cursor is requested, then
67178** the caller is assumed to have an open write transaction.
67179**
67180** If the BTREE_WRCSR bit of wrFlag is clear, then the cursor can only
67181** be used for reading. If the BTREE_WRCSR bit is set, then the cursor
67182** can be used for reading or for writing if other conditions for writing
67183** are also met. These are the conditions that must be met in order
67184** for writing to be allowed:
67185**
67186** 1: The cursor must have been opened with wrFlag containing BTREE_WRCSR
67187**
67188** 2: Other database connections that share the same pager cache
67189** but which are not in the READ_UNCOMMITTED state may not have
67190** cursors open with wrFlag==0 on the same table. Otherwise
67191** the changes made by this write cursor would be visible to
67192** the read cursors in the other database connection.
67193**
67194** 3: The database must be writable (not on read-only media)
67195**
67196** 4: There must be an active transaction.
67197**
67198** The BTREE_FORDELETE bit of wrFlag may optionally be set if BTREE_WRCSR
67199** is set. If FORDELETE is set, that is a hint to the implementation that
67200** this cursor will only be used to seek to and delete entries of an index
67201** as part of a larger DELETE statement. The FORDELETE hint is not used by
67202** this implementation. But in a hypothetical alternative storage engine
67203** in which index entries are automatically deleted when corresponding table
67204** rows are deleted, the FORDELETE flag is a hint that all SEEK and DELETE
67205** operations on this cursor can be no-ops and all READ operations can
67206** return a null row (2-bytes: 0x01 0x00).
67207**
67208** No checking is done to make sure that page iTable really is the
67209** root page of a b-tree. If it is not, then the cursor acquired
67210** will not work correctly.
67211**
67212** It is assumed that the sqlite3BtreeCursorZero() has been called
67213** on pCur to initialize the memory space prior to invoking this routine.
67214*/
67215static int btreeCursor(
67216 Btree *p, /* The btree */
67217 int iTable, /* Root page of table to open */
67218 int wrFlag, /* 1 to write. 0 read-only */
67219 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
67220 BtCursor *pCur /* Space for new cursor */
67221){
67222 BtShared *pBt = p->pBt; /* Shared b-tree handle */
67223 BtCursor *pX; /* Looping over other all cursors */
67224
67225 assert( sqlite3BtreeHoldsMutex(p) );
67226 assert( wrFlag==0
67227 || wrFlag==BTREE_WRCSR
67228 || wrFlag==(BTREE_WRCSR|BTREE_FORDELETE)
67229 );
67230
67231 /* The following assert statements verify that if this is a sharable
67232 ** b-tree database, the connection is holding the required table locks,
67233 ** and that no other connection has any open cursor that conflicts with
67234 ** this lock. */
67235 assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, (wrFlag?2:1)) );
67236 assert( wrFlag==0 || !hasReadConflicts(p, iTable) );
67237
67238 /* Assert that the caller has opened the required transaction. */
67239 assert( p->inTrans>TRANS_NONE );
67240 assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
67241 assert( pBt->pPage1 && pBt->pPage1->aData );
67242 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
67243
67244 if( wrFlag ){
67245 allocateTempSpace(pBt);
67246 if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM_BKPT;
67247 }
67248 if( iTable==1 && btreePagecount(pBt)==0 ){
67249 assert( wrFlag==0 );
67250 iTable = 0;
67251 }
67252
67253 /* Now that no other errors can occur, finish filling in the BtCursor
67254 ** variables and link the cursor into the BtShared list. */
67255 pCur->pgnoRoot = (Pgno)iTable;
67256 pCur->iPage = -1;
67257 pCur->pKeyInfo = pKeyInfo;
67258 pCur->pBtree = p;
67259 pCur->pBt = pBt;
67260 pCur->curFlags = wrFlag ? BTCF_WriteFlag : 0;
67261 pCur->curPagerFlags = wrFlag ? 0 : PAGER_GET_READONLY;
67262 /* If there are two or more cursors on the same btree, then all such
67263 ** cursors *must* have the BTCF_Multiple flag set. */
67264 for(pX=pBt->pCursor; pX; pX=pX->pNext){
67265 if( pX->pgnoRoot==(Pgno)iTable ){
67266 pX->curFlags |= BTCF_Multiple;
67267 pCur->curFlags |= BTCF_Multiple;
67268 }
67269 }
67270 pCur->pNext = pBt->pCursor;
67271 pBt->pCursor = pCur;
67272 pCur->eState = CURSOR_INVALID;
67273 return SQLITE_OK;
67274}
67275SQLITE_PRIVATE int sqlite3BtreeCursor(
67276 Btree *p, /* The btree */
67277 int iTable, /* Root page of table to open */
67278 int wrFlag, /* 1 to write. 0 read-only */
67279 struct KeyInfo *pKeyInfo, /* First arg to xCompare() */
67280 BtCursor *pCur /* Write new cursor here */
67281){
67282 int rc;
67283 if( iTable<1 ){
67284 rc = SQLITE_CORRUPT_BKPT;
67285 }else{
67286 sqlite3BtreeEnter(p);
67287 rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
67288 sqlite3BtreeLeave(p);
67289 }
67290 return rc;
67291}
67292
67293/*
67294** Return the size of a BtCursor object in bytes.
67295**
67296** This interfaces is needed so that users of cursors can preallocate
67297** sufficient storage to hold a cursor. The BtCursor object is opaque
67298** to users so they cannot do the sizeof() themselves - they must call
67299** this routine.
67300*/
67301SQLITE_PRIVATE int sqlite3BtreeCursorSize(void){
67302 return ROUND8(sizeof(BtCursor));
67303}
67304
67305/*
67306** Initialize memory that will be converted into a BtCursor object.
67307**
67308** The simple approach here would be to memset() the entire object
67309** to zero. But it turns out that the apPage[] and aiIdx[] arrays
67310** do not need to be zeroed and they are large, so we can save a lot
67311** of run-time by skipping the initialization of those elements.
67312*/
67313SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
67314 memset(p, 0, offsetof(BtCursor, BTCURSOR_FIRST_UNINIT));
67315}
67316
67317/*
67318** Close a cursor. The read lock on the database file is released
67319** when the last cursor is closed.
67320*/
67321SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
67322 Btree *pBtree = pCur->pBtree;
67323 if( pBtree ){
67324 BtShared *pBt = pCur->pBt;
67325 sqlite3BtreeEnter(pBtree);
67326 assert( pBt->pCursor!=0 );
67327 if( pBt->pCursor==pCur ){
67328 pBt->pCursor = pCur->pNext;
67329 }else{
67330 BtCursor *pPrev = pBt->pCursor;
67331 do{
67332 if( pPrev->pNext==pCur ){
67333 pPrev->pNext = pCur->pNext;
67334 break;
67335 }
67336 pPrev = pPrev->pNext;
67337 }while( ALWAYS(pPrev) );
67338 }
67339 btreeReleaseAllCursorPages(pCur);
67340 unlockBtreeIfUnused(pBt);
67341 sqlite3_free(pCur->aOverflow);
67342 sqlite3_free(pCur->pKey);
67343 sqlite3BtreeLeave(pBtree);
67344 }
67345 return SQLITE_OK;
67346}
67347
67348/*
67349** Make sure the BtCursor* given in the argument has a valid
67350** BtCursor.info structure. If it is not already valid, call
67351** btreeParseCell() to fill it in.
67352**
67353** BtCursor.info is a cache of the information in the current cell.
67354** Using this cache reduces the number of calls to btreeParseCell().
67355*/
67356#ifndef NDEBUG
67357 static int cellInfoEqual(CellInfo *a, CellInfo *b){
67358 if( a->nKey!=b->nKey ) return 0;
67359 if( a->pPayload!=b->pPayload ) return 0;
67360 if( a->nPayload!=b->nPayload ) return 0;
67361 if( a->nLocal!=b->nLocal ) return 0;
67362 if( a->nSize!=b->nSize ) return 0;
67363 return 1;
67364 }
67365 static void assertCellInfo(BtCursor *pCur){
67366 CellInfo info;
67367 memset(&info, 0, sizeof(info));
67368 btreeParseCell(pCur->pPage, pCur->ix, &info);
67369 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
67370 }
67371#else
67372 #define assertCellInfo(x)
67373#endif
67374static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
67375 if( pCur->info.nSize==0 ){
67376 pCur->curFlags |= BTCF_ValidNKey;
67377 btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
67378 }else{
67379 assertCellInfo(pCur);
67380 }
67381}
67382
67383#ifndef NDEBUG /* The next routine used only within assert() statements */
67384/*
67385** Return true if the given BtCursor is valid. A valid cursor is one
67386** that is currently pointing to a row in a (non-empty) table.
67387** This is a verification routine is used only within assert() statements.
67388*/
67389SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){
67390 return pCur && pCur->eState==CURSOR_VALID;
67391}
67392#endif /* NDEBUG */
67393SQLITE_PRIVATE int sqlite3BtreeCursorIsValidNN(BtCursor *pCur){
67394 assert( pCur!=0 );
67395 return pCur->eState==CURSOR_VALID;
67396}
67397
67398/*
67399** Return the value of the integer key or "rowid" for a table btree.
67400** This routine is only valid for a cursor that is pointing into a
67401** ordinary table btree. If the cursor points to an index btree or
67402** is invalid, the result of this routine is undefined.
67403*/
67404SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor *pCur){
67405 assert( cursorHoldsMutex(pCur) );
67406 assert( pCur->eState==CURSOR_VALID );
67407 assert( pCur->curIntKey );
67408 getCellInfo(pCur);
67409 return pCur->info.nKey;
67410}
67411
67412#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
67413/*
67414** Return the offset into the database file for the start of the
67415** payload to which the cursor is pointing.
67416*/
67417SQLITE_PRIVATE i64 sqlite3BtreeOffset(BtCursor *pCur){
67418 assert( cursorHoldsMutex(pCur) );
67419 assert( pCur->eState==CURSOR_VALID );
67420 getCellInfo(pCur);
67421 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
67422 (i64)(pCur->info.pPayload - pCur->pPage->aData);
67423}
67424#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
67425
67426/*
67427** Return the number of bytes of payload for the entry that pCur is
67428** currently pointing to. For table btrees, this will be the amount
67429** of data. For index btrees, this will be the size of the key.
67430**
67431** The caller must guarantee that the cursor is pointing to a non-NULL
67432** valid entry. In other words, the calling procedure must guarantee
67433** that the cursor has Cursor.eState==CURSOR_VALID.
67434*/
67435SQLITE_PRIVATE u32 sqlite3BtreePayloadSize(BtCursor *pCur){
67436 assert( cursorHoldsMutex(pCur) );
67437 assert( pCur->eState==CURSOR_VALID );
67438 getCellInfo(pCur);
67439 return pCur->info.nPayload;
67440}
67441
67442/*
67443** Given the page number of an overflow page in the database (parameter
67444** ovfl), this function finds the page number of the next page in the
67445** linked list of overflow pages. If possible, it uses the auto-vacuum
67446** pointer-map data instead of reading the content of page ovfl to do so.
67447**
67448** If an error occurs an SQLite error code is returned. Otherwise:
67449**
67450** The page number of the next overflow page in the linked list is
67451** written to *pPgnoNext. If page ovfl is the last page in its linked
67452** list, *pPgnoNext is set to zero.
67453**
67454** If ppPage is not NULL, and a reference to the MemPage object corresponding
67455** to page number pOvfl was obtained, then *ppPage is set to point to that
67456** reference. It is the responsibility of the caller to call releasePage()
67457** on *ppPage to free the reference. In no reference was obtained (because
67458** the pointer-map was used to obtain the value for *pPgnoNext), then
67459** *ppPage is set to zero.
67460*/
67461static int getOverflowPage(
67462 BtShared *pBt, /* The database file */
67463 Pgno ovfl, /* Current overflow page number */
67464 MemPage **ppPage, /* OUT: MemPage handle (may be NULL) */
67465 Pgno *pPgnoNext /* OUT: Next overflow page number */
67466){
67467 Pgno next = 0;
67468 MemPage *pPage = 0;
67469 int rc = SQLITE_OK;
67470
67471 assert( sqlite3_mutex_held(pBt->mutex) );
67472 assert(pPgnoNext);
67473
67474#ifndef SQLITE_OMIT_AUTOVACUUM
67475 /* Try to find the next page in the overflow list using the
67476 ** autovacuum pointer-map pages. Guess that the next page in
67477 ** the overflow list is page number (ovfl+1). If that guess turns
67478 ** out to be wrong, fall back to loading the data of page
67479 ** number ovfl to determine the next page number.
67480 */
67481 if( pBt->autoVacuum ){
67482 Pgno pgno;
67483 Pgno iGuess = ovfl+1;
67484 u8 eType;
67485
67486 while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
67487 iGuess++;
67488 }
67489
67490 if( iGuess<=btreePagecount(pBt) ){
67491 rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
67492 if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
67493 next = iGuess;
67494 rc = SQLITE_DONE;
67495 }
67496 }
67497 }
67498#endif
67499
67500 assert( next==0 || rc==SQLITE_DONE );
67501 if( rc==SQLITE_OK ){
67502 rc = btreeGetPage(pBt, ovfl, &pPage, (ppPage==0) ? PAGER_GET_READONLY : 0);
67503 assert( rc==SQLITE_OK || pPage==0 );
67504 if( rc==SQLITE_OK ){
67505 next = get4byte(pPage->aData);
67506 }
67507 }
67508
67509 *pPgnoNext = next;
67510 if( ppPage ){
67511 *ppPage = pPage;
67512 }else{
67513 releasePage(pPage);
67514 }
67515 return (rc==SQLITE_DONE ? SQLITE_OK : rc);
67516}
67517
67518/*
67519** Copy data from a buffer to a page, or from a page to a buffer.
67520**
67521** pPayload is a pointer to data stored on database page pDbPage.
67522** If argument eOp is false, then nByte bytes of data are copied
67523** from pPayload to the buffer pointed at by pBuf. If eOp is true,
67524** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
67525** of data are copied from the buffer pBuf to pPayload.
67526**
67527** SQLITE_OK is returned on success, otherwise an error code.
67528*/
67529static int copyPayload(
67530 void *pPayload, /* Pointer to page data */
67531 void *pBuf, /* Pointer to buffer */
67532 int nByte, /* Number of bytes to copy */
67533 int eOp, /* 0 -> copy from page, 1 -> copy to page */
67534 DbPage *pDbPage /* Page containing pPayload */
67535){
67536 if( eOp ){
67537 /* Copy data from buffer to page (a write operation) */
67538 int rc = sqlite3PagerWrite(pDbPage);
67539 if( rc!=SQLITE_OK ){
67540 return rc;
67541 }
67542 memcpy(pPayload, pBuf, nByte);
67543 }else{
67544 /* Copy data from page to buffer (a read operation) */
67545 memcpy(pBuf, pPayload, nByte);
67546 }
67547 return SQLITE_OK;
67548}
67549
67550/*
67551** This function is used to read or overwrite payload information
67552** for the entry that the pCur cursor is pointing to. The eOp
67553** argument is interpreted as follows:
67554**
67555** 0: The operation is a read. Populate the overflow cache.
67556** 1: The operation is a write. Populate the overflow cache.
67557**
67558** A total of "amt" bytes are read or written beginning at "offset".
67559** Data is read to or from the buffer pBuf.
67560**
67561** The content being read or written might appear on the main page
67562** or be scattered out on multiple overflow pages.
67563**
67564** If the current cursor entry uses one or more overflow pages
67565** this function may allocate space for and lazily populate
67566** the overflow page-list cache array (BtCursor.aOverflow).
67567** Subsequent calls use this cache to make seeking to the supplied offset
67568** more efficient.
67569**
67570** Once an overflow page-list cache has been allocated, it must be
67571** invalidated if some other cursor writes to the same table, or if
67572** the cursor is moved to a different row. Additionally, in auto-vacuum
67573** mode, the following events may invalidate an overflow page-list cache.
67574**
67575** * An incremental vacuum,
67576** * A commit in auto_vacuum="full" mode,
67577** * Creating a table (may require moving an overflow page).
67578*/
67579static int accessPayload(
67580 BtCursor *pCur, /* Cursor pointing to entry to read from */
67581 u32 offset, /* Begin reading this far into payload */
67582 u32 amt, /* Read this many bytes */
67583 unsigned char *pBuf, /* Write the bytes into this buffer */
67584 int eOp /* zero to read. non-zero to write. */
67585){
67586 unsigned char *aPayload;
67587 int rc = SQLITE_OK;
67588 int iIdx = 0;
67589 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
67590 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
67591#ifdef SQLITE_DIRECT_OVERFLOW_READ
67592 unsigned char * const pBufStart = pBuf; /* Start of original out buffer */
67593#endif
67594
67595 assert( pPage );
67596 assert( eOp==0 || eOp==1 );
67597 assert( pCur->eState==CURSOR_VALID );
67598 assert( pCur->ix<pPage->nCell );
67599 assert( cursorHoldsMutex(pCur) );
67600
67601 getCellInfo(pCur);
67602 aPayload = pCur->info.pPayload;
67603 assert( offset+amt <= pCur->info.nPayload );
67604
67605 assert( aPayload > pPage->aData );
67606 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
67607 /* Trying to read or write past the end of the data is an error. The
67608 ** conditional above is really:
67609 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
67610 ** but is recast into its current form to avoid integer overflow problems
67611 */
67612 return SQLITE_CORRUPT_PAGE(pPage);
67613 }
67614
67615 /* Check if data must be read/written to/from the btree page itself. */
67616 if( offset<pCur->info.nLocal ){
67617 int a = amt;
67618 if( a+offset>pCur->info.nLocal ){
67619 a = pCur->info.nLocal - offset;
67620 }
67621 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
67622 offset = 0;
67623 pBuf += a;
67624 amt -= a;
67625 }else{
67626 offset -= pCur->info.nLocal;
67627 }
67628
67629
67630 if( rc==SQLITE_OK && amt>0 ){
67631 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
67632 Pgno nextPage;
67633
67634 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
67635
67636 /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
67637 **
67638 ** The aOverflow[] array is sized at one entry for each overflow page
67639 ** in the overflow chain. The page number of the first overflow page is
67640 ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
67641 ** means "not yet known" (the cache is lazily populated).
67642 */
67643 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
67644 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
67645 if( pCur->aOverflow==0
67646 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
67647 ){
67648 Pgno *aNew = (Pgno*)sqlite3Realloc(
67649 pCur->aOverflow, nOvfl*2*sizeof(Pgno)
67650 );
67651 if( aNew==0 ){
67652 return SQLITE_NOMEM_BKPT;
67653 }else{
67654 pCur->aOverflow = aNew;
67655 }
67656 }
67657 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
67658 pCur->curFlags |= BTCF_ValidOvfl;
67659 }else{
67660 /* If the overflow page-list cache has been allocated and the
67661 ** entry for the first required overflow page is valid, skip
67662 ** directly to it.
67663 */
67664 if( pCur->aOverflow[offset/ovflSize] ){
67665 iIdx = (offset/ovflSize);
67666 nextPage = pCur->aOverflow[iIdx];
67667 offset = (offset%ovflSize);
67668 }
67669 }
67670
67671 assert( rc==SQLITE_OK && amt>0 );
67672 while( nextPage ){
67673 /* If required, populate the overflow page-list cache. */
67674 assert( pCur->aOverflow[iIdx]==0
67675 || pCur->aOverflow[iIdx]==nextPage
67676 || CORRUPT_DB );
67677 pCur->aOverflow[iIdx] = nextPage;
67678
67679 if( offset>=ovflSize ){
67680 /* The only reason to read this page is to obtain the page
67681 ** number for the next page in the overflow chain. The page
67682 ** data is not required. So first try to lookup the overflow
67683 ** page-list cache, if any, then fall back to the getOverflowPage()
67684 ** function.
67685 */
67686 assert( pCur->curFlags & BTCF_ValidOvfl );
67687 assert( pCur->pBtree->db==pBt->db );
67688 if( pCur->aOverflow[iIdx+1] ){
67689 nextPage = pCur->aOverflow[iIdx+1];
67690 }else{
67691 rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
67692 }
67693 offset -= ovflSize;
67694 }else{
67695 /* Need to read this page properly. It contains some of the
67696 ** range of data that is being read (eOp==0) or written (eOp!=0).
67697 */
67698 int a = amt;
67699 if( a + offset > ovflSize ){
67700 a = ovflSize - offset;
67701 }
67702
67703#ifdef SQLITE_DIRECT_OVERFLOW_READ
67704 /* If all the following are true:
67705 **
67706 ** 1) this is a read operation, and
67707 ** 2) data is required from the start of this overflow page, and
67708 ** 3) there are no dirty pages in the page-cache
67709 ** 4) the database is file-backed, and
67710 ** 5) the page is not in the WAL file
67711 ** 6) at least 4 bytes have already been read into the output buffer
67712 **
67713 ** then data can be read directly from the database file into the
67714 ** output buffer, bypassing the page-cache altogether. This speeds
67715 ** up loading large records that span many overflow pages.
67716 */
67717 if( eOp==0 /* (1) */
67718 && offset==0 /* (2) */
67719 && sqlite3PagerDirectReadOk(pBt->pPager, nextPage) /* (3,4,5) */
67720 && &pBuf[-4]>=pBufStart /* (6) */
67721 ){
67722 sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
67723 u8 aSave[4];
67724 u8 *aWrite = &pBuf[-4];
67725 assert( aWrite>=pBufStart ); /* due to (6) */
67726 memcpy(aSave, aWrite, 4);
67727 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
67728 nextPage = get4byte(aWrite);
67729 memcpy(aWrite, aSave, 4);
67730 }else
67731#endif
67732
67733 {
67734 DbPage *pDbPage;
67735 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
67736 (eOp==0 ? PAGER_GET_READONLY : 0)
67737 );
67738 if( rc==SQLITE_OK ){
67739 aPayload = sqlite3PagerGetData(pDbPage);
67740 nextPage = get4byte(aPayload);
67741 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
67742 sqlite3PagerUnref(pDbPage);
67743 offset = 0;
67744 }
67745 }
67746 amt -= a;
67747 if( amt==0 ) return rc;
67748 pBuf += a;
67749 }
67750 if( rc ) break;
67751 iIdx++;
67752 }
67753 }
67754
67755 if( rc==SQLITE_OK && amt>0 ){
67756 /* Overflow chain ends prematurely */
67757 return SQLITE_CORRUPT_PAGE(pPage);
67758 }
67759 return rc;
67760}
67761
67762/*
67763** Read part of the payload for the row at which that cursor pCur is currently
67764** pointing. "amt" bytes will be transferred into pBuf[]. The transfer
67765** begins at "offset".
67766**
67767** pCur can be pointing to either a table or an index b-tree.
67768** If pointing to a table btree, then the content section is read. If
67769** pCur is pointing to an index b-tree then the key section is read.
67770**
67771** For sqlite3BtreePayload(), the caller must ensure that pCur is pointing
67772** to a valid row in the table. For sqlite3BtreePayloadChecked(), the
67773** cursor might be invalid or might need to be restored before being read.
67774**
67775** Return SQLITE_OK on success or an error code if anything goes
67776** wrong. An error is returned if "offset+amt" is larger than
67777** the available payload.
67778*/
67779SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
67780 assert( cursorHoldsMutex(pCur) );
67781 assert( pCur->eState==CURSOR_VALID );
67782 assert( pCur->iPage>=0 && pCur->pPage );
67783 assert( pCur->ix<pCur->pPage->nCell );
67784 return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
67785}
67786
67787/*
67788** This variant of sqlite3BtreePayload() works even if the cursor has not
67789** in the CURSOR_VALID state. It is only used by the sqlite3_blob_read()
67790** interface.
67791*/
67792#ifndef SQLITE_OMIT_INCRBLOB
67793static SQLITE_NOINLINE int accessPayloadChecked(
67794 BtCursor *pCur,
67795 u32 offset,
67796 u32 amt,
67797 void *pBuf
67798){
67799 int rc;
67800 if ( pCur->eState==CURSOR_INVALID ){
67801 return SQLITE_ABORT;
67802 }
67803 assert( cursorOwnsBtShared(pCur) );
67804 rc = btreeRestoreCursorPosition(pCur);
67805 return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0);
67806}
67807SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
67808 if( pCur->eState==CURSOR_VALID ){
67809 assert( cursorOwnsBtShared(pCur) );
67810 return accessPayload(pCur, offset, amt, pBuf, 0);
67811 }else{
67812 return accessPayloadChecked(pCur, offset, amt, pBuf);
67813 }
67814}
67815#endif /* SQLITE_OMIT_INCRBLOB */
67816
67817/*
67818** Return a pointer to payload information from the entry that the
67819** pCur cursor is pointing to. The pointer is to the beginning of
67820** the key if index btrees (pPage->intKey==0) and is the data for
67821** table btrees (pPage->intKey==1). The number of bytes of available
67822** key/data is written into *pAmt. If *pAmt==0, then the value
67823** returned will not be a valid pointer.
67824**
67825** This routine is an optimization. It is common for the entire key
67826** and data to fit on the local page and for there to be no overflow
67827** pages. When that is so, this routine can be used to access the
67828** key and data without making a copy. If the key and/or data spills
67829** onto overflow pages, then accessPayload() must be used to reassemble
67830** the key/data and copy it into a preallocated buffer.
67831**
67832** The pointer returned by this routine looks directly into the cached
67833** page of the database. The data might change or move the next time
67834** any btree routine is called.
67835*/
67836static const void *fetchPayload(
67837 BtCursor *pCur, /* Cursor pointing to entry to read from */
67838 u32 *pAmt /* Write the number of available bytes here */
67839){
67840 int amt;
67841 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
67842 assert( pCur->eState==CURSOR_VALID );
67843 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
67844 assert( cursorOwnsBtShared(pCur) );
67845 assert( pCur->ix<pCur->pPage->nCell );
67846 assert( pCur->info.nSize>0 );
67847 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB );
67848 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB);
67849 amt = pCur->info.nLocal;
67850 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
67851 /* There is too little space on the page for the expected amount
67852 ** of local content. Database must be corrupt. */
67853 assert( CORRUPT_DB );
67854 amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload));
67855 }
67856 *pAmt = (u32)amt;
67857 return (void*)pCur->info.pPayload;
67858}
67859
67860
67861/*
67862** For the entry that cursor pCur is point to, return as
67863** many bytes of the key or data as are available on the local
67864** b-tree page. Write the number of available bytes into *pAmt.
67865**
67866** The pointer returned is ephemeral. The key/data may move
67867** or be destroyed on the next call to any Btree routine,
67868** including calls from other threads against the same cache.
67869** Hence, a mutex on the BtShared should be held prior to calling
67870** this routine.
67871**
67872** These routines is used to get quick access to key and data
67873** in the common case where no overflow pages are used.
67874*/
67875SQLITE_PRIVATE const void *sqlite3BtreePayloadFetch(BtCursor *pCur, u32 *pAmt){
67876 return fetchPayload(pCur, pAmt);
67877}
67878
67879
67880/*
67881** Move the cursor down to a new child page. The newPgno argument is the
67882** page number of the child page to move to.
67883**
67884** This function returns SQLITE_CORRUPT if the page-header flags field of
67885** the new child page does not match the flags field of the parent (i.e.
67886** if an intkey page appears to be the parent of a non-intkey page, or
67887** vice-versa).
67888*/
67889static int moveToChild(BtCursor *pCur, u32 newPgno){
67890 BtShared *pBt = pCur->pBt;
67891
67892 assert( cursorOwnsBtShared(pCur) );
67893 assert( pCur->eState==CURSOR_VALID );
67894 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
67895 assert( pCur->iPage>=0 );
67896 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
67897 return SQLITE_CORRUPT_BKPT;
67898 }
67899 pCur->info.nSize = 0;
67900 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
67901 pCur->aiIdx[pCur->iPage] = pCur->ix;
67902 pCur->apPage[pCur->iPage] = pCur->pPage;
67903 pCur->ix = 0;
67904 pCur->iPage++;
67905 return getAndInitPage(pBt, newPgno, &pCur->pPage, pCur, pCur->curPagerFlags);
67906}
67907
67908#ifdef SQLITE_DEBUG
67909/*
67910** Page pParent is an internal (non-leaf) tree page. This function
67911** asserts that page number iChild is the left-child if the iIdx'th
67912** cell in page pParent. Or, if iIdx is equal to the total number of
67913** cells in pParent, that page number iChild is the right-child of
67914** the page.
67915*/
67916static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
67917 if( CORRUPT_DB ) return; /* The conditions tested below might not be true
67918 ** in a corrupt database */
67919 assert( iIdx<=pParent->nCell );
67920 if( iIdx==pParent->nCell ){
67921 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
67922 }else{
67923 assert( get4byte(findCell(pParent, iIdx))==iChild );
67924 }
67925}
67926#else
67927# define assertParentIndex(x,y,z)
67928#endif
67929
67930/*
67931** Move the cursor up to the parent page.
67932**
67933** pCur->idx is set to the cell index that contains the pointer
67934** to the page we are coming from. If we are coming from the
67935** right-most child page then pCur->idx is set to one more than
67936** the largest cell index.
67937*/
67938static void moveToParent(BtCursor *pCur){
67939 MemPage *pLeaf;
67940 assert( cursorOwnsBtShared(pCur) );
67941 assert( pCur->eState==CURSOR_VALID );
67942 assert( pCur->iPage>0 );
67943 assert( pCur->pPage );
67944 assertParentIndex(
67945 pCur->apPage[pCur->iPage-1],
67946 pCur->aiIdx[pCur->iPage-1],
67947 pCur->pPage->pgno
67948 );
67949 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
67950 pCur->info.nSize = 0;
67951 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
67952 pCur->ix = pCur->aiIdx[pCur->iPage-1];
67953 pLeaf = pCur->pPage;
67954 pCur->pPage = pCur->apPage[--pCur->iPage];
67955 releasePageNotNull(pLeaf);
67956}
67957
67958/*
67959** Move the cursor to point to the root page of its b-tree structure.
67960**
67961** If the table has a virtual root page, then the cursor is moved to point
67962** to the virtual root page instead of the actual root page. A table has a
67963** virtual root page when the actual root page contains no cells and a
67964** single child page. This can only happen with the table rooted at page 1.
67965**
67966** If the b-tree structure is empty, the cursor state is set to
67967** CURSOR_INVALID and this routine returns SQLITE_EMPTY. Otherwise,
67968** the cursor is set to point to the first cell located on the root
67969** (or virtual root) page and the cursor state is set to CURSOR_VALID.
67970**
67971** If this function returns successfully, it may be assumed that the
67972** page-header flags indicate that the [virtual] root-page is the expected
67973** kind of b-tree page (i.e. if when opening the cursor the caller did not
67974** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
67975** indicating a table b-tree, or if the caller did specify a KeyInfo
67976** structure the flags byte is set to 0x02 or 0x0A, indicating an index
67977** b-tree).
67978*/
67979static int moveToRoot(BtCursor *pCur){
67980 MemPage *pRoot;
67981 int rc = SQLITE_OK;
67982
67983 assert( cursorOwnsBtShared(pCur) );
67984 assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
67985 assert( CURSOR_VALID < CURSOR_REQUIRESEEK );
67986 assert( CURSOR_FAULT > CURSOR_REQUIRESEEK );
67987 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 );
67988 assert( pCur->pgnoRoot>0 || pCur->iPage<0 );
67989
67990 if( pCur->iPage>=0 ){
67991 if( pCur->iPage ){
67992 releasePageNotNull(pCur->pPage);
67993 while( --pCur->iPage ){
67994 releasePageNotNull(pCur->apPage[pCur->iPage]);
67995 }
67996 pCur->pPage = pCur->apPage[0];
67997 goto skip_init;
67998 }
67999 }else if( pCur->pgnoRoot==0 ){
68000 pCur->eState = CURSOR_INVALID;
68001 return SQLITE_EMPTY;
68002 }else{
68003 assert( pCur->iPage==(-1) );
68004 if( pCur->eState>=CURSOR_REQUIRESEEK ){
68005 if( pCur->eState==CURSOR_FAULT ){
68006 assert( pCur->skipNext!=SQLITE_OK );
68007 return pCur->skipNext;
68008 }
68009 sqlite3BtreeClearCursor(pCur);
68010 }
68011 rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->pPage,
68012 0, pCur->curPagerFlags);
68013 if( rc!=SQLITE_OK ){
68014 pCur->eState = CURSOR_INVALID;
68015 return rc;
68016 }
68017 pCur->iPage = 0;
68018 pCur->curIntKey = pCur->pPage->intKey;
68019 }
68020 pRoot = pCur->pPage;
68021 assert( pRoot->pgno==pCur->pgnoRoot );
68022
68023 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
68024 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
68025 ** NULL, the caller expects a table b-tree. If this is not the case,
68026 ** return an SQLITE_CORRUPT error.
68027 **
68028 ** Earlier versions of SQLite assumed that this test could not fail
68029 ** if the root page was already loaded when this function was called (i.e.
68030 ** if pCur->iPage>=0). But this is not so if the database is corrupted
68031 ** in such a way that page pRoot is linked into a second b-tree table
68032 ** (or the freelist). */
68033 assert( pRoot->intKey==1 || pRoot->intKey==0 );
68034 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
68035 return SQLITE_CORRUPT_PAGE(pCur->pPage);
68036 }
68037
68038skip_init:
68039 pCur->ix = 0;
68040 pCur->info.nSize = 0;
68041 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
68042
68043 pRoot = pCur->pPage;
68044 if( pRoot->nCell>0 ){
68045 pCur->eState = CURSOR_VALID;
68046 }else if( !pRoot->leaf ){
68047 Pgno subpage;
68048 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
68049 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
68050 pCur->eState = CURSOR_VALID;
68051 rc = moveToChild(pCur, subpage);
68052 }else{
68053 pCur->eState = CURSOR_INVALID;
68054 rc = SQLITE_EMPTY;
68055 }
68056 return rc;
68057}
68058
68059/*
68060** Move the cursor down to the left-most leaf entry beneath the
68061** entry to which it is currently pointing.
68062**
68063** The left-most leaf is the one with the smallest key - the first
68064** in ascending order.
68065*/
68066static int moveToLeftmost(BtCursor *pCur){
68067 Pgno pgno;
68068 int rc = SQLITE_OK;
68069 MemPage *pPage;
68070
68071 assert( cursorOwnsBtShared(pCur) );
68072 assert( pCur->eState==CURSOR_VALID );
68073 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
68074 assert( pCur->ix<pPage->nCell );
68075 pgno = get4byte(findCell(pPage, pCur->ix));
68076 rc = moveToChild(pCur, pgno);
68077 }
68078 return rc;
68079}
68080
68081/*
68082** Move the cursor down to the right-most leaf entry beneath the
68083** page to which it is currently pointing. Notice the difference
68084** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
68085** finds the left-most entry beneath the *entry* whereas moveToRightmost()
68086** finds the right-most entry beneath the *page*.
68087**
68088** The right-most entry is the one with the largest key - the last
68089** key in ascending order.
68090*/
68091static int moveToRightmost(BtCursor *pCur){
68092 Pgno pgno;
68093 int rc = SQLITE_OK;
68094 MemPage *pPage = 0;
68095
68096 assert( cursorOwnsBtShared(pCur) );
68097 assert( pCur->eState==CURSOR_VALID );
68098 while( !(pPage = pCur->pPage)->leaf ){
68099 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
68100 pCur->ix = pPage->nCell;
68101 rc = moveToChild(pCur, pgno);
68102 if( rc ) return rc;
68103 }
68104 pCur->ix = pPage->nCell-1;
68105 assert( pCur->info.nSize==0 );
68106 assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
68107 return SQLITE_OK;
68108}
68109
68110/* Move the cursor to the first entry in the table. Return SQLITE_OK
68111** on success. Set *pRes to 0 if the cursor actually points to something
68112** or set *pRes to 1 if the table is empty.
68113*/
68114SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
68115 int rc;
68116
68117 assert( cursorOwnsBtShared(pCur) );
68118 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
68119 rc = moveToRoot(pCur);
68120 if( rc==SQLITE_OK ){
68121 assert( pCur->pPage->nCell>0 );
68122 *pRes = 0;
68123 rc = moveToLeftmost(pCur);
68124 }else if( rc==SQLITE_EMPTY ){
68125 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
68126 *pRes = 1;
68127 rc = SQLITE_OK;
68128 }
68129 return rc;
68130}
68131
68132/*
68133** This function is a no-op if cursor pCur does not point to a valid row.
68134** Otherwise, if pCur is valid, configure it so that the next call to
68135** sqlite3BtreeNext() is a no-op.
68136*/
68137#ifndef SQLITE_OMIT_WINDOWFUNC
68138SQLITE_PRIVATE void sqlite3BtreeSkipNext(BtCursor *pCur){
68139 /* We believe that the cursor must always be in the valid state when
68140 ** this routine is called, but the proof is difficult, so we add an
68141 ** ALWaYS() test just in case we are wrong. */
68142 if( ALWAYS(pCur->eState==CURSOR_VALID) ){
68143 pCur->eState = CURSOR_SKIPNEXT;
68144 pCur->skipNext = 1;
68145 }
68146}
68147#endif /* SQLITE_OMIT_WINDOWFUNC */
68148
68149/* Move the cursor to the last entry in the table. Return SQLITE_OK
68150** on success. Set *pRes to 0 if the cursor actually points to something
68151** or set *pRes to 1 if the table is empty.
68152*/
68153SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
68154 int rc;
68155
68156 assert( cursorOwnsBtShared(pCur) );
68157 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
68158
68159 /* If the cursor already points to the last entry, this is a no-op. */
68160 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
68161#ifdef SQLITE_DEBUG
68162 /* This block serves to assert() that the cursor really does point
68163 ** to the last entry in the b-tree. */
68164 int ii;
68165 for(ii=0; ii<pCur->iPage; ii++){
68166 assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
68167 }
68168 assert( pCur->ix==pCur->pPage->nCell-1 );
68169 assert( pCur->pPage->leaf );
68170#endif
68171 return SQLITE_OK;
68172 }
68173
68174 rc = moveToRoot(pCur);
68175 if( rc==SQLITE_OK ){
68176 assert( pCur->eState==CURSOR_VALID );
68177 *pRes = 0;
68178 rc = moveToRightmost(pCur);
68179 if( rc==SQLITE_OK ){
68180 pCur->curFlags |= BTCF_AtLast;
68181 }else{
68182 pCur->curFlags &= ~BTCF_AtLast;
68183 }
68184 }else if( rc==SQLITE_EMPTY ){
68185 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
68186 *pRes = 1;
68187 rc = SQLITE_OK;
68188 }
68189 return rc;
68190}
68191
68192/* Move the cursor so that it points to an entry near the key
68193** specified by pIdxKey or intKey. Return a success code.
68194**
68195** For INTKEY tables, the intKey parameter is used. pIdxKey
68196** must be NULL. For index tables, pIdxKey is used and intKey
68197** is ignored.
68198**
68199** If an exact match is not found, then the cursor is always
68200** left pointing at a leaf page which would hold the entry if it
68201** were present. The cursor might point to an entry that comes
68202** before or after the key.
68203**
68204** An integer is written into *pRes which is the result of
68205** comparing the key with the entry to which the cursor is
68206** pointing. The meaning of the integer written into
68207** *pRes is as follows:
68208**
68209** *pRes<0 The cursor is left pointing at an entry that
68210** is smaller than intKey/pIdxKey or if the table is empty
68211** and the cursor is therefore left point to nothing.
68212**
68213** *pRes==0 The cursor is left pointing at an entry that
68214** exactly matches intKey/pIdxKey.
68215**
68216** *pRes>0 The cursor is left pointing at an entry that
68217** is larger than intKey/pIdxKey.
68218**
68219** For index tables, the pIdxKey->eqSeen field is set to 1 if there
68220** exists an entry in the table that exactly matches pIdxKey.
68221*/
68222SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
68223 BtCursor *pCur, /* The cursor to be moved */
68224 UnpackedRecord *pIdxKey, /* Unpacked index key */
68225 i64 intKey, /* The table key */
68226 int biasRight, /* If true, bias the search to the high end */
68227 int *pRes /* Write search results here */
68228){
68229 int rc;
68230 RecordCompare xRecordCompare;
68231
68232 assert( cursorOwnsBtShared(pCur) );
68233 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
68234 assert( pRes );
68235 assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
68236 assert( pCur->eState!=CURSOR_VALID || (pIdxKey==0)==(pCur->curIntKey!=0) );
68237
68238 /* If the cursor is already positioned at the point we are trying
68239 ** to move to, then just return without doing any work */
68240 if( pIdxKey==0
68241 && pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
68242 ){
68243 if( pCur->info.nKey==intKey ){
68244 *pRes = 0;
68245 return SQLITE_OK;
68246 }
68247 if( pCur->info.nKey<intKey ){
68248 if( (pCur->curFlags & BTCF_AtLast)!=0 ){
68249 *pRes = -1;
68250 return SQLITE_OK;
68251 }
68252 /* If the requested key is one more than the previous key, then
68253 ** try to get there using sqlite3BtreeNext() rather than a full
68254 ** binary search. This is an optimization only. The correct answer
68255 ** is still obtained without this case, only a little more slowely */
68256 if( pCur->info.nKey+1==intKey && !pCur->skipNext ){
68257 *pRes = 0;
68258 rc = sqlite3BtreeNext(pCur, 0);
68259 if( rc==SQLITE_OK ){
68260 getCellInfo(pCur);
68261 if( pCur->info.nKey==intKey ){
68262 return SQLITE_OK;
68263 }
68264 }else if( rc==SQLITE_DONE ){
68265 rc = SQLITE_OK;
68266 }else{
68267 return rc;
68268 }
68269 }
68270 }
68271 }
68272
68273 if( pIdxKey ){
68274 xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
68275 pIdxKey->errCode = 0;
68276 assert( pIdxKey->default_rc==1
68277 || pIdxKey->default_rc==0
68278 || pIdxKey->default_rc==-1
68279 );
68280 }else{
68281 xRecordCompare = 0; /* All keys are integers */
68282 }
68283
68284 rc = moveToRoot(pCur);
68285 if( rc ){
68286 if( rc==SQLITE_EMPTY ){
68287 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
68288 *pRes = -1;
68289 return SQLITE_OK;
68290 }
68291 return rc;
68292 }
68293 assert( pCur->pPage );
68294 assert( pCur->pPage->isInit );
68295 assert( pCur->eState==CURSOR_VALID );
68296 assert( pCur->pPage->nCell > 0 );
68297 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
68298 assert( pCur->curIntKey || pIdxKey );
68299 for(;;){
68300 int lwr, upr, idx, c;
68301 Pgno chldPg;
68302 MemPage *pPage = pCur->pPage;
68303 u8 *pCell; /* Pointer to current cell in pPage */
68304
68305 /* pPage->nCell must be greater than zero. If this is the root-page
68306 ** the cursor would have been INVALID above and this for(;;) loop
68307 ** not run. If this is not the root-page, then the moveToChild() routine
68308 ** would have already detected db corruption. Similarly, pPage must
68309 ** be the right kind (index or table) of b-tree page. Otherwise
68310 ** a moveToChild() or moveToRoot() call would have detected corruption. */
68311 assert( pPage->nCell>0 );
68312 assert( pPage->intKey==(pIdxKey==0) );
68313 lwr = 0;
68314 upr = pPage->nCell-1;
68315 assert( biasRight==0 || biasRight==1 );
68316 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
68317 pCur->ix = (u16)idx;
68318 if( xRecordCompare==0 ){
68319 for(;;){
68320 i64 nCellKey;
68321 pCell = findCellPastPtr(pPage, idx);
68322 if( pPage->intKeyLeaf ){
68323 while( 0x80 <= *(pCell++) ){
68324 if( pCell>=pPage->aDataEnd ){
68325 return SQLITE_CORRUPT_PAGE(pPage);
68326 }
68327 }
68328 }
68329 getVarint(pCell, (u64*)&nCellKey);
68330 if( nCellKey<intKey ){
68331 lwr = idx+1;
68332 if( lwr>upr ){ c = -1; break; }
68333 }else if( nCellKey>intKey ){
68334 upr = idx-1;
68335 if( lwr>upr ){ c = +1; break; }
68336 }else{
68337 assert( nCellKey==intKey );
68338 pCur->ix = (u16)idx;
68339 if( !pPage->leaf ){
68340 lwr = idx;
68341 goto moveto_next_layer;
68342 }else{
68343 pCur->curFlags |= BTCF_ValidNKey;
68344 pCur->info.nKey = nCellKey;
68345 pCur->info.nSize = 0;
68346 *pRes = 0;
68347 return SQLITE_OK;
68348 }
68349 }
68350 assert( lwr+upr>=0 );
68351 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */
68352 }
68353 }else{
68354 for(;;){
68355 int nCell; /* Size of the pCell cell in bytes */
68356 pCell = findCellPastPtr(pPage, idx);
68357
68358 /* The maximum supported page-size is 65536 bytes. This means that
68359 ** the maximum number of record bytes stored on an index B-Tree
68360 ** page is less than 16384 bytes and may be stored as a 2-byte
68361 ** varint. This information is used to attempt to avoid parsing
68362 ** the entire cell by checking for the cases where the record is
68363 ** stored entirely within the b-tree page by inspecting the first
68364 ** 2 bytes of the cell.
68365 */
68366 nCell = pCell[0];
68367 if( nCell<=pPage->max1bytePayload ){
68368 /* This branch runs if the record-size field of the cell is a
68369 ** single byte varint and the record fits entirely on the main
68370 ** b-tree page. */
68371 testcase( pCell+nCell+1==pPage->aDataEnd );
68372 c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
68373 }else if( !(pCell[1] & 0x80)
68374 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
68375 ){
68376 /* The record-size field is a 2 byte varint and the record
68377 ** fits entirely on the main b-tree page. */
68378 testcase( pCell+nCell+2==pPage->aDataEnd );
68379 c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
68380 }else{
68381 /* The record flows over onto one or more overflow pages. In
68382 ** this case the whole cell needs to be parsed, a buffer allocated
68383 ** and accessPayload() used to retrieve the record into the
68384 ** buffer before VdbeRecordCompare() can be called.
68385 **
68386 ** If the record is corrupt, the xRecordCompare routine may read
68387 ** up to two varints past the end of the buffer. An extra 18
68388 ** bytes of padding is allocated at the end of the buffer in
68389 ** case this happens. */
68390 void *pCellKey;
68391 u8 * const pCellBody = pCell - pPage->childPtrSize;
68392 pPage->xParseCell(pPage, pCellBody, &pCur->info);
68393 nCell = (int)pCur->info.nKey;
68394 testcase( nCell<0 ); /* True if key size is 2^32 or more */
68395 testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */
68396 testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */
68397 testcase( nCell==2 ); /* Minimum legal index key size */
68398 if( nCell<2 ){
68399 rc = SQLITE_CORRUPT_PAGE(pPage);
68400 goto moveto_finish;
68401 }
68402 pCellKey = sqlite3Malloc( nCell+18 );
68403 if( pCellKey==0 ){
68404 rc = SQLITE_NOMEM_BKPT;
68405 goto moveto_finish;
68406 }
68407 pCur->ix = (u16)idx;
68408 rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
68409 pCur->curFlags &= ~BTCF_ValidOvfl;
68410 if( rc ){
68411 sqlite3_free(pCellKey);
68412 goto moveto_finish;
68413 }
68414 c = xRecordCompare(nCell, pCellKey, pIdxKey);
68415 sqlite3_free(pCellKey);
68416 }
68417 assert(
68418 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
68419 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
68420 );
68421 if( c<0 ){
68422 lwr = idx+1;
68423 }else if( c>0 ){
68424 upr = idx-1;
68425 }else{
68426 assert( c==0 );
68427 *pRes = 0;
68428 rc = SQLITE_OK;
68429 pCur->ix = (u16)idx;
68430 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT;
68431 goto moveto_finish;
68432 }
68433 if( lwr>upr ) break;
68434 assert( lwr+upr>=0 );
68435 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2 */
68436 }
68437 }
68438 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
68439 assert( pPage->isInit );
68440 if( pPage->leaf ){
68441 assert( pCur->ix<pCur->pPage->nCell );
68442 pCur->ix = (u16)idx;
68443 *pRes = c;
68444 rc = SQLITE_OK;
68445 goto moveto_finish;
68446 }
68447moveto_next_layer:
68448 if( lwr>=pPage->nCell ){
68449 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
68450 }else{
68451 chldPg = get4byte(findCell(pPage, lwr));
68452 }
68453 pCur->ix = (u16)lwr;
68454 rc = moveToChild(pCur, chldPg);
68455 if( rc ) break;
68456 }
68457moveto_finish:
68458 pCur->info.nSize = 0;
68459 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
68460 return rc;
68461}
68462
68463
68464/*
68465** Return TRUE if the cursor is not pointing at an entry of the table.
68466**
68467** TRUE will be returned after a call to sqlite3BtreeNext() moves
68468** past the last entry in the table or sqlite3BtreePrev() moves past
68469** the first entry. TRUE is also returned if the table is empty.
68470*/
68471SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){
68472 /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
68473 ** have been deleted? This API will need to change to return an error code
68474 ** as well as the boolean result value.
68475 */
68476 return (CURSOR_VALID!=pCur->eState);
68477}
68478
68479/*
68480** Return an estimate for the number of rows in the table that pCur is
68481** pointing to. Return a negative number if no estimate is currently
68482** available.
68483*/
68484SQLITE_PRIVATE i64 sqlite3BtreeRowCountEst(BtCursor *pCur){
68485 i64 n;
68486 u8 i;
68487
68488 assert( cursorOwnsBtShared(pCur) );
68489 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
68490
68491 /* Currently this interface is only called by the OP_IfSmaller
68492 ** opcode, and it that case the cursor will always be valid and
68493 ** will always point to a leaf node. */
68494 if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1;
68495 if( NEVER(pCur->pPage->leaf==0) ) return -1;
68496
68497 n = pCur->pPage->nCell;
68498 for(i=0; i<pCur->iPage; i++){
68499 n *= pCur->apPage[i]->nCell;
68500 }
68501 return n;
68502}
68503
68504/*
68505** Advance the cursor to the next entry in the database.
68506** Return value:
68507**
68508** SQLITE_OK success
68509** SQLITE_DONE cursor is already pointing at the last element
68510** otherwise some kind of error occurred
68511**
68512** The main entry point is sqlite3BtreeNext(). That routine is optimized
68513** for the common case of merely incrementing the cell counter BtCursor.aiIdx
68514** to the next cell on the current page. The (slower) btreeNext() helper
68515** routine is called when it is necessary to move to a different page or
68516** to restore the cursor.
68517**
68518** If bit 0x01 of the F argument in sqlite3BtreeNext(C,F) is 1, then the
68519** cursor corresponds to an SQL index and this routine could have been
68520** skipped if the SQL index had been a unique index. The F argument
68521** is a hint to the implement. SQLite btree implementation does not use
68522** this hint, but COMDB2 does.
68523*/
68524static SQLITE_NOINLINE int btreeNext(BtCursor *pCur){
68525 int rc;
68526 int idx;
68527 MemPage *pPage;
68528
68529 assert( cursorOwnsBtShared(pCur) );
68530 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
68531 if( pCur->eState!=CURSOR_VALID ){
68532 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
68533 rc = restoreCursorPosition(pCur);
68534 if( rc!=SQLITE_OK ){
68535 return rc;
68536 }
68537 if( CURSOR_INVALID==pCur->eState ){
68538 return SQLITE_DONE;
68539 }
68540 if( pCur->skipNext ){
68541 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
68542 pCur->eState = CURSOR_VALID;
68543 if( pCur->skipNext>0 ){
68544 pCur->skipNext = 0;
68545 return SQLITE_OK;
68546 }
68547 pCur->skipNext = 0;
68548 }
68549 }
68550
68551 pPage = pCur->pPage;
68552 idx = ++pCur->ix;
68553 if( !pPage->isInit ){
68554 /* The only known way for this to happen is for there to be a
68555 ** recursive SQL function that does a DELETE operation as part of a
68556 ** SELECT which deletes content out from under an active cursor
68557 ** in a corrupt database file where the table being DELETE-ed from
68558 ** has pages in common with the table being queried. See TH3
68559 ** module cov1/btree78.test testcase 220 (2018-06-08) for an
68560 ** example. */
68561 return SQLITE_CORRUPT_BKPT;
68562 }
68563
68564 /* If the database file is corrupt, it is possible for the value of idx
68565 ** to be invalid here. This can only occur if a second cursor modifies
68566 ** the page while cursor pCur is holding a reference to it. Which can
68567 ** only happen if the database is corrupt in such a way as to link the
68568 ** page into more than one b-tree structure. */
68569 testcase( idx>pPage->nCell );
68570
68571 if( idx>=pPage->nCell ){
68572 if( !pPage->leaf ){
68573 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
68574 if( rc ) return rc;
68575 return moveToLeftmost(pCur);
68576 }
68577 do{
68578 if( pCur->iPage==0 ){
68579 pCur->eState = CURSOR_INVALID;
68580 return SQLITE_DONE;
68581 }
68582 moveToParent(pCur);
68583 pPage = pCur->pPage;
68584 }while( pCur->ix>=pPage->nCell );
68585 if( pPage->intKey ){
68586 return sqlite3BtreeNext(pCur, 0);
68587 }else{
68588 return SQLITE_OK;
68589 }
68590 }
68591 if( pPage->leaf ){
68592 return SQLITE_OK;
68593 }else{
68594 return moveToLeftmost(pCur);
68595 }
68596}
68597SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int flags){
68598 MemPage *pPage;
68599 UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */
68600 assert( cursorOwnsBtShared(pCur) );
68601 assert( flags==0 || flags==1 );
68602 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
68603 pCur->info.nSize = 0;
68604 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
68605 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur);
68606 pPage = pCur->pPage;
68607 if( (++pCur->ix)>=pPage->nCell ){
68608 pCur->ix--;
68609 return btreeNext(pCur);
68610 }
68611 if( pPage->leaf ){
68612 return SQLITE_OK;
68613 }else{
68614 return moveToLeftmost(pCur);
68615 }
68616}
68617
68618/*
68619** Step the cursor to the back to the previous entry in the database.
68620** Return values:
68621**
68622** SQLITE_OK success
68623** SQLITE_DONE the cursor is already on the first element of the table
68624** otherwise some kind of error occurred
68625**
68626** The main entry point is sqlite3BtreePrevious(). That routine is optimized
68627** for the common case of merely decrementing the cell counter BtCursor.aiIdx
68628** to the previous cell on the current page. The (slower) btreePrevious()
68629** helper routine is called when it is necessary to move to a different page
68630** or to restore the cursor.
68631**
68632** If bit 0x01 of the F argument to sqlite3BtreePrevious(C,F) is 1, then
68633** the cursor corresponds to an SQL index and this routine could have been
68634** skipped if the SQL index had been a unique index. The F argument is a
68635** hint to the implement. The native SQLite btree implementation does not
68636** use this hint, but COMDB2 does.
68637*/
68638static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur){
68639 int rc;
68640 MemPage *pPage;
68641
68642 assert( cursorOwnsBtShared(pCur) );
68643 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
68644 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
68645 assert( pCur->info.nSize==0 );
68646 if( pCur->eState!=CURSOR_VALID ){
68647 rc = restoreCursorPosition(pCur);
68648 if( rc!=SQLITE_OK ){
68649 return rc;
68650 }
68651 if( CURSOR_INVALID==pCur->eState ){
68652 return SQLITE_DONE;
68653 }
68654 if( pCur->skipNext ){
68655 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
68656 pCur->eState = CURSOR_VALID;
68657 if( pCur->skipNext<0 ){
68658 pCur->skipNext = 0;
68659 return SQLITE_OK;
68660 }
68661 pCur->skipNext = 0;
68662 }
68663 }
68664
68665 pPage = pCur->pPage;
68666 assert( pPage->isInit );
68667 if( !pPage->leaf ){
68668 int idx = pCur->ix;
68669 rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
68670 if( rc ) return rc;
68671 rc = moveToRightmost(pCur);
68672 }else{
68673 while( pCur->ix==0 ){
68674 if( pCur->iPage==0 ){
68675 pCur->eState = CURSOR_INVALID;
68676 return SQLITE_DONE;
68677 }
68678 moveToParent(pCur);
68679 }
68680 assert( pCur->info.nSize==0 );
68681 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
68682
68683 pCur->ix--;
68684 pPage = pCur->pPage;
68685 if( pPage->intKey && !pPage->leaf ){
68686 rc = sqlite3BtreePrevious(pCur, 0);
68687 }else{
68688 rc = SQLITE_OK;
68689 }
68690 }
68691 return rc;
68692}
68693SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int flags){
68694 assert( cursorOwnsBtShared(pCur) );
68695 assert( flags==0 || flags==1 );
68696 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
68697 UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */
68698 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
68699 pCur->info.nSize = 0;
68700 if( pCur->eState!=CURSOR_VALID
68701 || pCur->ix==0
68702 || pCur->pPage->leaf==0
68703 ){
68704 return btreePrevious(pCur);
68705 }
68706 pCur->ix--;
68707 return SQLITE_OK;
68708}
68709
68710/*
68711** Allocate a new page from the database file.
68712**
68713** The new page is marked as dirty. (In other words, sqlite3PagerWrite()
68714** has already been called on the new page.) The new page has also
68715** been referenced and the calling routine is responsible for calling
68716** sqlite3PagerUnref() on the new page when it is done.
68717**
68718** SQLITE_OK is returned on success. Any other return value indicates
68719** an error. *ppPage is set to NULL in the event of an error.
68720**
68721** If the "nearby" parameter is not 0, then an effort is made to
68722** locate a page close to the page number "nearby". This can be used in an
68723** attempt to keep related pages close to each other in the database file,
68724** which in turn can make database access faster.
68725**
68726** If the eMode parameter is BTALLOC_EXACT and the nearby page exists
68727** anywhere on the free-list, then it is guaranteed to be returned. If
68728** eMode is BTALLOC_LT then the page returned will be less than or equal
68729** to nearby if any such page exists. If eMode is BTALLOC_ANY then there
68730** are no restrictions on which page is returned.
68731*/
68732static int allocateBtreePage(
68733 BtShared *pBt, /* The btree */
68734 MemPage **ppPage, /* Store pointer to the allocated page here */
68735 Pgno *pPgno, /* Store the page number here */
68736 Pgno nearby, /* Search for a page near this one */
68737 u8 eMode /* BTALLOC_EXACT, BTALLOC_LT, or BTALLOC_ANY */
68738){
68739 MemPage *pPage1;
68740 int rc;
68741 u32 n; /* Number of pages on the freelist */
68742 u32 k; /* Number of leaves on the trunk of the freelist */
68743 MemPage *pTrunk = 0;
68744 MemPage *pPrevTrunk = 0;
68745 Pgno mxPage; /* Total size of the database file */
68746
68747 assert( sqlite3_mutex_held(pBt->mutex) );
68748 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
68749 pPage1 = pBt->pPage1;
68750 mxPage = btreePagecount(pBt);
68751 /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36
68752 ** stores stores the total number of pages on the freelist. */
68753 n = get4byte(&pPage1->aData[36]);
68754 testcase( n==mxPage-1 );
68755 if( n>=mxPage ){
68756 return SQLITE_CORRUPT_BKPT;
68757 }
68758 if( n>0 ){
68759 /* There are pages on the freelist. Reuse one of those pages. */
68760 Pgno iTrunk;
68761 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
68762 u32 nSearch = 0; /* Count of the number of search attempts */
68763
68764 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
68765 ** shows that the page 'nearby' is somewhere on the free-list, then
68766 ** the entire-list will be searched for that page.
68767 */
68768#ifndef SQLITE_OMIT_AUTOVACUUM
68769 if( eMode==BTALLOC_EXACT ){
68770 if( nearby<=mxPage ){
68771 u8 eType;
68772 assert( nearby>0 );
68773 assert( pBt->autoVacuum );
68774 rc = ptrmapGet(pBt, nearby, &eType, 0);
68775 if( rc ) return rc;
68776 if( eType==PTRMAP_FREEPAGE ){
68777 searchList = 1;
68778 }
68779 }
68780 }else if( eMode==BTALLOC_LE ){
68781 searchList = 1;
68782 }
68783#endif
68784
68785 /* Decrement the free-list count by 1. Set iTrunk to the index of the
68786 ** first free-list trunk page. iPrevTrunk is initially 1.
68787 */
68788 rc = sqlite3PagerWrite(pPage1->pDbPage);
68789 if( rc ) return rc;
68790 put4byte(&pPage1->aData[36], n-1);
68791
68792 /* The code within this loop is run only once if the 'searchList' variable
68793 ** is not true. Otherwise, it runs once for each trunk-page on the
68794 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
68795 ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT)
68796 */
68797 do {
68798 pPrevTrunk = pTrunk;
68799 if( pPrevTrunk ){
68800 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
68801 ** is the page number of the next freelist trunk page in the list or
68802 ** zero if this is the last freelist trunk page. */
68803 iTrunk = get4byte(&pPrevTrunk->aData[0]);
68804 }else{
68805 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
68806 ** stores the page number of the first page of the freelist, or zero if
68807 ** the freelist is empty. */
68808 iTrunk = get4byte(&pPage1->aData[32]);
68809 }
68810 testcase( iTrunk==mxPage );
68811 if( iTrunk>mxPage || nSearch++ > n ){
68812 rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1);
68813 }else{
68814 rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0);
68815 }
68816 if( rc ){
68817 pTrunk = 0;
68818 goto end_allocate_page;
68819 }
68820 assert( pTrunk!=0 );
68821 assert( pTrunk->aData!=0 );
68822 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
68823 ** is the number of leaf page pointers to follow. */
68824 k = get4byte(&pTrunk->aData[4]);
68825 if( k==0 && !searchList ){
68826 /* The trunk has no leaves and the list is not being searched.
68827 ** So extract the trunk page itself and use it as the newly
68828 ** allocated page */
68829 assert( pPrevTrunk==0 );
68830 rc = sqlite3PagerWrite(pTrunk->pDbPage);
68831 if( rc ){
68832 goto end_allocate_page;
68833 }
68834 *pPgno = iTrunk;
68835 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
68836 *ppPage = pTrunk;
68837 pTrunk = 0;
68838 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
68839 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
68840 /* Value of k is out of range. Database corruption */
68841 rc = SQLITE_CORRUPT_PGNO(iTrunk);
68842 goto end_allocate_page;
68843#ifndef SQLITE_OMIT_AUTOVACUUM
68844 }else if( searchList
68845 && (nearby==iTrunk || (iTrunk<nearby && eMode==BTALLOC_LE))
68846 ){
68847 /* The list is being searched and this trunk page is the page
68848 ** to allocate, regardless of whether it has leaves.
68849 */
68850 *pPgno = iTrunk;
68851 *ppPage = pTrunk;
68852 searchList = 0;
68853 rc = sqlite3PagerWrite(pTrunk->pDbPage);
68854 if( rc ){
68855 goto end_allocate_page;
68856 }
68857 if( k==0 ){
68858 if( !pPrevTrunk ){
68859 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
68860 }else{
68861 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
68862 if( rc!=SQLITE_OK ){
68863 goto end_allocate_page;
68864 }
68865 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
68866 }
68867 }else{
68868 /* The trunk page is required by the caller but it contains
68869 ** pointers to free-list leaves. The first leaf becomes a trunk
68870 ** page in this case.
68871 */
68872 MemPage *pNewTrunk;
68873 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
68874 if( iNewTrunk>mxPage ){
68875 rc = SQLITE_CORRUPT_PGNO(iTrunk);
68876 goto end_allocate_page;
68877 }
68878 testcase( iNewTrunk==mxPage );
68879 rc = btreeGetUnusedPage(pBt, iNewTrunk, &pNewTrunk, 0);
68880 if( rc!=SQLITE_OK ){
68881 goto end_allocate_page;
68882 }
68883 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
68884 if( rc!=SQLITE_OK ){
68885 releasePage(pNewTrunk);
68886 goto end_allocate_page;
68887 }
68888 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
68889 put4byte(&pNewTrunk->aData[4], k-1);
68890 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
68891 releasePage(pNewTrunk);
68892 if( !pPrevTrunk ){
68893 assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
68894 put4byte(&pPage1->aData[32], iNewTrunk);
68895 }else{
68896 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
68897 if( rc ){
68898 goto end_allocate_page;
68899 }
68900 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
68901 }
68902 }
68903 pTrunk = 0;
68904 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
68905#endif
68906 }else if( k>0 ){
68907 /* Extract a leaf from the trunk */
68908 u32 closest;
68909 Pgno iPage;
68910 unsigned char *aData = pTrunk->aData;
68911 if( nearby>0 ){
68912 u32 i;
68913 closest = 0;
68914 if( eMode==BTALLOC_LE ){
68915 for(i=0; i<k; i++){
68916 iPage = get4byte(&aData[8+i*4]);
68917 if( iPage<=nearby ){
68918 closest = i;
68919 break;
68920 }
68921 }
68922 }else{
68923 int dist;
68924 dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
68925 for(i=1; i<k; i++){
68926 int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
68927 if( d2<dist ){
68928 closest = i;
68929 dist = d2;
68930 }
68931 }
68932 }
68933 }else{
68934 closest = 0;
68935 }
68936
68937 iPage = get4byte(&aData[8+closest*4]);
68938 testcase( iPage==mxPage );
68939 if( iPage>mxPage ){
68940 rc = SQLITE_CORRUPT_PGNO(iTrunk);
68941 goto end_allocate_page;
68942 }
68943 testcase( iPage==mxPage );
68944 if( !searchList
68945 || (iPage==nearby || (iPage<nearby && eMode==BTALLOC_LE))
68946 ){
68947 int noContent;
68948 *pPgno = iPage;
68949 TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
68950 ": %d more free pages\n",
68951 *pPgno, closest+1, k, pTrunk->pgno, n-1));
68952 rc = sqlite3PagerWrite(pTrunk->pDbPage);
68953 if( rc ) goto end_allocate_page;
68954 if( closest<k-1 ){
68955 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
68956 }
68957 put4byte(&aData[4], k-1);
68958 noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0;
68959 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent);
68960 if( rc==SQLITE_OK ){
68961 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
68962 if( rc!=SQLITE_OK ){
68963 releasePage(*ppPage);
68964 *ppPage = 0;
68965 }
68966 }
68967 searchList = 0;
68968 }
68969 }
68970 releasePage(pPrevTrunk);
68971 pPrevTrunk = 0;
68972 }while( searchList );
68973 }else{
68974 /* There are no pages on the freelist, so append a new page to the
68975 ** database image.
68976 **
68977 ** Normally, new pages allocated by this block can be requested from the
68978 ** pager layer with the 'no-content' flag set. This prevents the pager
68979 ** from trying to read the pages content from disk. However, if the
68980 ** current transaction has already run one or more incremental-vacuum
68981 ** steps, then the page we are about to allocate may contain content
68982 ** that is required in the event of a rollback. In this case, do
68983 ** not set the no-content flag. This causes the pager to load and journal
68984 ** the current page content before overwriting it.
68985 **
68986 ** Note that the pager will not actually attempt to load or journal
68987 ** content for any page that really does lie past the end of the database
68988 ** file on disk. So the effects of disabling the no-content optimization
68989 ** here are confined to those pages that lie between the end of the
68990 ** database image and the end of the database file.
68991 */
68992 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
68993
68994 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
68995 if( rc ) return rc;
68996 pBt->nPage++;
68997 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
68998
68999#ifndef SQLITE_OMIT_AUTOVACUUM
69000 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
69001 /* If *pPgno refers to a pointer-map page, allocate two new pages
69002 ** at the end of the file instead of one. The first allocated page
69003 ** becomes a new pointer-map page, the second is used by the caller.
69004 */
69005 MemPage *pPg = 0;
69006 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
69007 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
69008 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
69009 if( rc==SQLITE_OK ){
69010 rc = sqlite3PagerWrite(pPg->pDbPage);
69011 releasePage(pPg);
69012 }
69013 if( rc ) return rc;
69014 pBt->nPage++;
69015 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
69016 }
69017#endif
69018 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
69019 *pPgno = pBt->nPage;
69020
69021 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
69022 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, bNoContent);
69023 if( rc ) return rc;
69024 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
69025 if( rc!=SQLITE_OK ){
69026 releasePage(*ppPage);
69027 *ppPage = 0;
69028 }
69029 TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
69030 }
69031
69032 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
69033
69034end_allocate_page:
69035 releasePage(pTrunk);
69036 releasePage(pPrevTrunk);
69037 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
69038 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
69039 return rc;
69040}
69041
69042/*
69043** This function is used to add page iPage to the database file free-list.
69044** It is assumed that the page is not already a part of the free-list.
69045**
69046** The value passed as the second argument to this function is optional.
69047** If the caller happens to have a pointer to the MemPage object
69048** corresponding to page iPage handy, it may pass it as the second value.
69049** Otherwise, it may pass NULL.
69050**
69051** If a pointer to a MemPage object is passed as the second argument,
69052** its reference count is not altered by this function.
69053*/
69054static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
69055 MemPage *pTrunk = 0; /* Free-list trunk page */
69056 Pgno iTrunk = 0; /* Page number of free-list trunk page */
69057 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
69058 MemPage *pPage; /* Page being freed. May be NULL. */
69059 int rc; /* Return Code */
69060 int nFree; /* Initial number of pages on free-list */
69061
69062 assert( sqlite3_mutex_held(pBt->mutex) );
69063 assert( CORRUPT_DB || iPage>1 );
69064 assert( !pMemPage || pMemPage->pgno==iPage );
69065
69066 if( iPage<2 ) return SQLITE_CORRUPT_BKPT;
69067 if( pMemPage ){
69068 pPage = pMemPage;
69069 sqlite3PagerRef(pPage->pDbPage);
69070 }else{
69071 pPage = btreePageLookup(pBt, iPage);
69072 }
69073
69074 /* Increment the free page count on pPage1 */
69075 rc = sqlite3PagerWrite(pPage1->pDbPage);
69076 if( rc ) goto freepage_out;
69077 nFree = get4byte(&pPage1->aData[36]);
69078 put4byte(&pPage1->aData[36], nFree+1);
69079
69080 if( pBt->btsFlags & BTS_SECURE_DELETE ){
69081 /* If the secure_delete option is enabled, then
69082 ** always fully overwrite deleted information with zeros.
69083 */
69084 if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
69085 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
69086 ){
69087 goto freepage_out;
69088 }
69089 memset(pPage->aData, 0, pPage->pBt->pageSize);
69090 }
69091
69092 /* If the database supports auto-vacuum, write an entry in the pointer-map
69093 ** to indicate that the page is free.
69094 */
69095 if( ISAUTOVACUUM ){
69096 ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc);
69097 if( rc ) goto freepage_out;
69098 }
69099
69100 /* Now manipulate the actual database free-list structure. There are two
69101 ** possibilities. If the free-list is currently empty, or if the first
69102 ** trunk page in the free-list is full, then this page will become a
69103 ** new free-list trunk page. Otherwise, it will become a leaf of the
69104 ** first trunk page in the current free-list. This block tests if it
69105 ** is possible to add the page as a new free-list leaf.
69106 */
69107 if( nFree!=0 ){
69108 u32 nLeaf; /* Initial number of leaf cells on trunk page */
69109
69110 iTrunk = get4byte(&pPage1->aData[32]);
69111 rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
69112 if( rc!=SQLITE_OK ){
69113 goto freepage_out;
69114 }
69115
69116 nLeaf = get4byte(&pTrunk->aData[4]);
69117 assert( pBt->usableSize>32 );
69118 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
69119 rc = SQLITE_CORRUPT_BKPT;
69120 goto freepage_out;
69121 }
69122 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
69123 /* In this case there is room on the trunk page to insert the page
69124 ** being freed as a new leaf.
69125 **
69126 ** Note that the trunk page is not really full until it contains
69127 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
69128 ** coded. But due to a coding error in versions of SQLite prior to
69129 ** 3.6.0, databases with freelist trunk pages holding more than
69130 ** usableSize/4 - 8 entries will be reported as corrupt. In order
69131 ** to maintain backwards compatibility with older versions of SQLite,
69132 ** we will continue to restrict the number of entries to usableSize/4 - 8
69133 ** for now. At some point in the future (once everyone has upgraded
69134 ** to 3.6.0 or later) we should consider fixing the conditional above
69135 ** to read "usableSize/4-2" instead of "usableSize/4-8".
69136 **
69137 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
69138 ** avoid using the last six entries in the freelist trunk page array in
69139 ** order that database files created by newer versions of SQLite can be
69140 ** read by older versions of SQLite.
69141 */
69142 rc = sqlite3PagerWrite(pTrunk->pDbPage);
69143 if( rc==SQLITE_OK ){
69144 put4byte(&pTrunk->aData[4], nLeaf+1);
69145 put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
69146 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
69147 sqlite3PagerDontWrite(pPage->pDbPage);
69148 }
69149 rc = btreeSetHasContent(pBt, iPage);
69150 }
69151 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
69152 goto freepage_out;
69153 }
69154 }
69155
69156 /* If control flows to this point, then it was not possible to add the
69157 ** the page being freed as a leaf page of the first trunk in the free-list.
69158 ** Possibly because the free-list is empty, or possibly because the
69159 ** first trunk in the free-list is full. Either way, the page being freed
69160 ** will become the new first trunk page in the free-list.
69161 */
69162 if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
69163 goto freepage_out;
69164 }
69165 rc = sqlite3PagerWrite(pPage->pDbPage);
69166 if( rc!=SQLITE_OK ){
69167 goto freepage_out;
69168 }
69169 put4byte(pPage->aData, iTrunk);
69170 put4byte(&pPage->aData[4], 0);
69171 put4byte(&pPage1->aData[32], iPage);
69172 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
69173
69174freepage_out:
69175 if( pPage ){
69176 pPage->isInit = 0;
69177 }
69178 releasePage(pPage);
69179 releasePage(pTrunk);
69180 return rc;
69181}
69182static void freePage(MemPage *pPage, int *pRC){
69183 if( (*pRC)==SQLITE_OK ){
69184 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
69185 }
69186}
69187
69188/*
69189** Free any overflow pages associated with the given Cell. Store
69190** size information about the cell in pInfo.
69191*/
69192static int clearCell(
69193 MemPage *pPage, /* The page that contains the Cell */
69194 unsigned char *pCell, /* First byte of the Cell */
69195 CellInfo *pInfo /* Size information about the cell */
69196){
69197 BtShared *pBt;
69198 Pgno ovflPgno;
69199 int rc;
69200 int nOvfl;
69201 u32 ovflPageSize;
69202
69203 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
69204 pPage->xParseCell(pPage, pCell, pInfo);
69205 if( pInfo->nLocal==pInfo->nPayload ){
69206 return SQLITE_OK; /* No overflow pages. Return without doing anything */
69207 }
69208 testcase( pCell + pInfo->nSize == pPage->aDataEnd );
69209 testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
69210 if( pCell + pInfo->nSize > pPage->aDataEnd ){
69211 /* Cell extends past end of page */
69212 return SQLITE_CORRUPT_PAGE(pPage);
69213 }
69214 ovflPgno = get4byte(pCell + pInfo->nSize - 4);
69215 pBt = pPage->pBt;
69216 assert( pBt->usableSize > 4 );
69217 ovflPageSize = pBt->usableSize - 4;
69218 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
69219 assert( nOvfl>0 ||
69220 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
69221 );
69222 while( nOvfl-- ){
69223 Pgno iNext = 0;
69224 MemPage *pOvfl = 0;
69225 if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
69226 /* 0 is not a legal page number and page 1 cannot be an
69227 ** overflow page. Therefore if ovflPgno<2 or past the end of the
69228 ** file the database must be corrupt. */
69229 return SQLITE_CORRUPT_BKPT;
69230 }
69231 if( nOvfl ){
69232 rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
69233 if( rc ) return rc;
69234 }
69235
69236 if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) )
69237 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
69238 ){
69239 /* There is no reason any cursor should have an outstanding reference
69240 ** to an overflow page belonging to a cell that is being deleted/updated.
69241 ** So if there exists more than one reference to this page, then it
69242 ** must not really be an overflow page and the database must be corrupt.
69243 ** It is helpful to detect this before calling freePage2(), as
69244 ** freePage2() may zero the page contents if secure-delete mode is
69245 ** enabled. If this 'overflow' page happens to be a page that the
69246 ** caller is iterating through or using in some other way, this
69247 ** can be problematic.
69248 */
69249 rc = SQLITE_CORRUPT_BKPT;
69250 }else{
69251 rc = freePage2(pBt, pOvfl, ovflPgno);
69252 }
69253
69254 if( pOvfl ){
69255 sqlite3PagerUnref(pOvfl->pDbPage);
69256 }
69257 if( rc ) return rc;
69258 ovflPgno = iNext;
69259 }
69260 return SQLITE_OK;
69261}
69262
69263/*
69264** Create the byte sequence used to represent a cell on page pPage
69265** and write that byte sequence into pCell[]. Overflow pages are
69266** allocated and filled in as necessary. The calling procedure
69267** is responsible for making sure sufficient space has been allocated
69268** for pCell[].
69269**
69270** Note that pCell does not necessary need to point to the pPage->aData
69271** area. pCell might point to some temporary storage. The cell will
69272** be constructed in this temporary area then copied into pPage->aData
69273** later.
69274*/
69275static int fillInCell(
69276 MemPage *pPage, /* The page that contains the cell */
69277 unsigned char *pCell, /* Complete text of the cell */
69278 const BtreePayload *pX, /* Payload with which to construct the cell */
69279 int *pnSize /* Write cell size here */
69280){
69281 int nPayload;
69282 const u8 *pSrc;
69283 int nSrc, n, rc, mn;
69284 int spaceLeft;
69285 MemPage *pToRelease;
69286 unsigned char *pPrior;
69287 unsigned char *pPayload;
69288 BtShared *pBt;
69289 Pgno pgnoOvfl;
69290 int nHeader;
69291
69292 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
69293
69294 /* pPage is not necessarily writeable since pCell might be auxiliary
69295 ** buffer space that is separate from the pPage buffer area */
69296 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]
69297 || sqlite3PagerIswriteable(pPage->pDbPage) );
69298
69299 /* Fill in the header. */
69300 nHeader = pPage->childPtrSize;
69301 if( pPage->intKey ){
69302 nPayload = pX->nData + pX->nZero;
69303 pSrc = pX->pData;
69304 nSrc = pX->nData;
69305 assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
69306 nHeader += putVarint32(&pCell[nHeader], nPayload);
69307 nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
69308 }else{
69309 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
69310 nSrc = nPayload = (int)pX->nKey;
69311 pSrc = pX->pKey;
69312 nHeader += putVarint32(&pCell[nHeader], nPayload);
69313 }
69314
69315 /* Fill in the payload */
69316 pPayload = &pCell[nHeader];
69317 if( nPayload<=pPage->maxLocal ){
69318 /* This is the common case where everything fits on the btree page
69319 ** and no overflow pages are required. */
69320 n = nHeader + nPayload;
69321 testcase( n==3 );
69322 testcase( n==4 );
69323 if( n<4 ) n = 4;
69324 *pnSize = n;
69325 assert( nSrc<=nPayload );
69326 testcase( nSrc<nPayload );
69327 memcpy(pPayload, pSrc, nSrc);
69328 memset(pPayload+nSrc, 0, nPayload-nSrc);
69329 return SQLITE_OK;
69330 }
69331
69332 /* If we reach this point, it means that some of the content will need
69333 ** to spill onto overflow pages.
69334 */
69335 mn = pPage->minLocal;
69336 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
69337 testcase( n==pPage->maxLocal );
69338 testcase( n==pPage->maxLocal+1 );
69339 if( n > pPage->maxLocal ) n = mn;
69340 spaceLeft = n;
69341 *pnSize = n + nHeader + 4;
69342 pPrior = &pCell[nHeader+n];
69343 pToRelease = 0;
69344 pgnoOvfl = 0;
69345 pBt = pPage->pBt;
69346
69347 /* At this point variables should be set as follows:
69348 **
69349 ** nPayload Total payload size in bytes
69350 ** pPayload Begin writing payload here
69351 ** spaceLeft Space available at pPayload. If nPayload>spaceLeft,
69352 ** that means content must spill into overflow pages.
69353 ** *pnSize Size of the local cell (not counting overflow pages)
69354 ** pPrior Where to write the pgno of the first overflow page
69355 **
69356 ** Use a call to btreeParseCellPtr() to verify that the values above
69357 ** were computed correctly.
69358 */
69359#ifdef SQLITE_DEBUG
69360 {
69361 CellInfo info;
69362 pPage->xParseCell(pPage, pCell, &info);
69363 assert( nHeader==(int)(info.pPayload - pCell) );
69364 assert( info.nKey==pX->nKey );
69365 assert( *pnSize == info.nSize );
69366 assert( spaceLeft == info.nLocal );
69367 }
69368#endif
69369
69370 /* Write the payload into the local Cell and any extra into overflow pages */
69371 while( 1 ){
69372 n = nPayload;
69373 if( n>spaceLeft ) n = spaceLeft;
69374
69375 /* If pToRelease is not zero than pPayload points into the data area
69376 ** of pToRelease. Make sure pToRelease is still writeable. */
69377 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
69378
69379 /* If pPayload is part of the data area of pPage, then make sure pPage
69380 ** is still writeable */
69381 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
69382 || sqlite3PagerIswriteable(pPage->pDbPage) );
69383
69384 if( nSrc>=n ){
69385 memcpy(pPayload, pSrc, n);
69386 }else if( nSrc>0 ){
69387 n = nSrc;
69388 memcpy(pPayload, pSrc, n);
69389 }else{
69390 memset(pPayload, 0, n);
69391 }
69392 nPayload -= n;
69393 if( nPayload<=0 ) break;
69394 pPayload += n;
69395 pSrc += n;
69396 nSrc -= n;
69397 spaceLeft -= n;
69398 if( spaceLeft==0 ){
69399 MemPage *pOvfl = 0;
69400#ifndef SQLITE_OMIT_AUTOVACUUM
69401 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
69402 if( pBt->autoVacuum ){
69403 do{
69404 pgnoOvfl++;
69405 } while(
69406 PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)
69407 );
69408 }
69409#endif
69410 rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
69411#ifndef SQLITE_OMIT_AUTOVACUUM
69412 /* If the database supports auto-vacuum, and the second or subsequent
69413 ** overflow page is being allocated, add an entry to the pointer-map
69414 ** for that page now.
69415 **
69416 ** If this is the first overflow page, then write a partial entry
69417 ** to the pointer-map. If we write nothing to this pointer-map slot,
69418 ** then the optimistic overflow chain processing in clearCell()
69419 ** may misinterpret the uninitialized values and delete the
69420 ** wrong pages from the database.
69421 */
69422 if( pBt->autoVacuum && rc==SQLITE_OK ){
69423 u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
69424 ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
69425 if( rc ){
69426 releasePage(pOvfl);
69427 }
69428 }
69429#endif
69430 if( rc ){
69431 releasePage(pToRelease);
69432 return rc;
69433 }
69434
69435 /* If pToRelease is not zero than pPrior points into the data area
69436 ** of pToRelease. Make sure pToRelease is still writeable. */
69437 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
69438
69439 /* If pPrior is part of the data area of pPage, then make sure pPage
69440 ** is still writeable */
69441 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
69442 || sqlite3PagerIswriteable(pPage->pDbPage) );
69443
69444 put4byte(pPrior, pgnoOvfl);
69445 releasePage(pToRelease);
69446 pToRelease = pOvfl;
69447 pPrior = pOvfl->aData;
69448 put4byte(pPrior, 0);
69449 pPayload = &pOvfl->aData[4];
69450 spaceLeft = pBt->usableSize - 4;
69451 }
69452 }
69453 releasePage(pToRelease);
69454 return SQLITE_OK;
69455}
69456
69457/*
69458** Remove the i-th cell from pPage. This routine effects pPage only.
69459** The cell content is not freed or deallocated. It is assumed that
69460** the cell content has been copied someplace else. This routine just
69461** removes the reference to the cell from pPage.
69462**
69463** "sz" must be the number of bytes in the cell.
69464*/
69465static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
69466 u32 pc; /* Offset to cell content of cell being deleted */
69467 u8 *data; /* pPage->aData */
69468 u8 *ptr; /* Used to move bytes around within data[] */
69469 int rc; /* The return code */
69470 int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */
69471
69472 if( *pRC ) return;
69473 assert( idx>=0 && idx<pPage->nCell );
69474 assert( CORRUPT_DB || sz==cellSize(pPage, idx) );
69475 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
69476 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
69477 data = pPage->aData;
69478 ptr = &pPage->aCellIdx[2*idx];
69479 pc = get2byte(ptr);
69480 hdr = pPage->hdrOffset;
69481 testcase( pc==get2byte(&data[hdr+5]) );
69482 testcase( pc+sz==pPage->pBt->usableSize );
69483 if( pc+sz > pPage->pBt->usableSize ){
69484 *pRC = SQLITE_CORRUPT_BKPT;
69485 return;
69486 }
69487 rc = freeSpace(pPage, pc, sz);
69488 if( rc ){
69489 *pRC = rc;
69490 return;
69491 }
69492 pPage->nCell--;
69493 if( pPage->nCell==0 ){
69494 memset(&data[hdr+1], 0, 4);
69495 data[hdr+7] = 0;
69496 put2byte(&data[hdr+5], pPage->pBt->usableSize);
69497 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
69498 - pPage->childPtrSize - 8;
69499 }else{
69500 memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
69501 put2byte(&data[hdr+3], pPage->nCell);
69502 pPage->nFree += 2;
69503 }
69504}
69505
69506/*
69507** Insert a new cell on pPage at cell index "i". pCell points to the
69508** content of the cell.
69509**
69510** If the cell content will fit on the page, then put it there. If it
69511** will not fit, then make a copy of the cell content into pTemp if
69512** pTemp is not null. Regardless of pTemp, allocate a new entry
69513** in pPage->apOvfl[] and make it point to the cell content (either
69514** in pTemp or the original pCell) and also record its index.
69515** Allocating a new entry in pPage->aCell[] implies that
69516** pPage->nOverflow is incremented.
69517**
69518** *pRC must be SQLITE_OK when this routine is called.
69519*/
69520static void insertCell(
69521 MemPage *pPage, /* Page into which we are copying */
69522 int i, /* New cell becomes the i-th cell of the page */
69523 u8 *pCell, /* Content of the new cell */
69524 int sz, /* Bytes of content in pCell */
69525 u8 *pTemp, /* Temp storage space for pCell, if needed */
69526 Pgno iChild, /* If non-zero, replace first 4 bytes with this value */
69527 int *pRC /* Read and write return code from here */
69528){
69529 int idx = 0; /* Where to write new cell content in data[] */
69530 int j; /* Loop counter */
69531 u8 *data; /* The content of the whole page */
69532 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
69533
69534 assert( *pRC==SQLITE_OK );
69535 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
69536 assert( MX_CELL(pPage->pBt)<=10921 );
69537 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
69538 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
69539 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
69540 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
69541 /* The cell should normally be sized correctly. However, when moving a
69542 ** malformed cell from a leaf page to an interior page, if the cell size
69543 ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
69544 ** might be less than 8 (leaf-size + pointer) on the interior node. Hence
69545 ** the term after the || in the following assert(). */
69546 assert( sz==pPage->xCellSize(pPage, pCell) || (sz==8 && iChild>0) );
69547 if( pPage->nOverflow || sz+2>pPage->nFree ){
69548 if( pTemp ){
69549 memcpy(pTemp, pCell, sz);
69550 pCell = pTemp;
69551 }
69552 if( iChild ){
69553 put4byte(pCell, iChild);
69554 }
69555 j = pPage->nOverflow++;
69556 /* Comparison against ArraySize-1 since we hold back one extra slot
69557 ** as a contingency. In other words, never need more than 3 overflow
69558 ** slots but 4 are allocated, just to be safe. */
69559 assert( j < ArraySize(pPage->apOvfl)-1 );
69560 pPage->apOvfl[j] = pCell;
69561 pPage->aiOvfl[j] = (u16)i;
69562
69563 /* When multiple overflows occur, they are always sequential and in
69564 ** sorted order. This invariants arise because multiple overflows can
69565 ** only occur when inserting divider cells into the parent page during
69566 ** balancing, and the dividers are adjacent and sorted.
69567 */
69568 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
69569 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
69570 }else{
69571 int rc = sqlite3PagerWrite(pPage->pDbPage);
69572 if( rc!=SQLITE_OK ){
69573 *pRC = rc;
69574 return;
69575 }
69576 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
69577 data = pPage->aData;
69578 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
69579 rc = allocateSpace(pPage, sz, &idx);
69580 if( rc ){ *pRC = rc; return; }
69581 /* The allocateSpace() routine guarantees the following properties
69582 ** if it returns successfully */
69583 assert( idx >= 0 );
69584 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
69585 assert( idx+sz <= (int)pPage->pBt->usableSize );
69586 pPage->nFree -= (u16)(2 + sz);
69587 memcpy(&data[idx], pCell, sz);
69588 if( iChild ){
69589 put4byte(&data[idx], iChild);
69590 }
69591 pIns = pPage->aCellIdx + i*2;
69592 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
69593 put2byte(pIns, idx);
69594 pPage->nCell++;
69595 /* increment the cell count */
69596 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
69597 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell );
69598#ifndef SQLITE_OMIT_AUTOVACUUM
69599 if( pPage->pBt->autoVacuum ){
69600 /* The cell may contain a pointer to an overflow page. If so, write
69601 ** the entry for the overflow page into the pointer map.
69602 */
69603 ptrmapPutOvflPtr(pPage, pCell, pRC);
69604 }
69605#endif
69606 }
69607}
69608
69609/*
69610** A CellArray object contains a cache of pointers and sizes for a
69611** consecutive sequence of cells that might be held on multiple pages.
69612*/
69613typedef struct CellArray CellArray;
69614struct CellArray {
69615 int nCell; /* Number of cells in apCell[] */
69616 MemPage *pRef; /* Reference page */
69617 u8 **apCell; /* All cells begin balanced */
69618 u16 *szCell; /* Local size of all cells in apCell[] */
69619};
69620
69621/*
69622** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
69623** computed.
69624*/
69625static void populateCellCache(CellArray *p, int idx, int N){
69626 assert( idx>=0 && idx+N<=p->nCell );
69627 while( N>0 ){
69628 assert( p->apCell[idx]!=0 );
69629 if( p->szCell[idx]==0 ){
69630 p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]);
69631 }else{
69632 assert( CORRUPT_DB ||
69633 p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) );
69634 }
69635 idx++;
69636 N--;
69637 }
69638}
69639
69640/*
69641** Return the size of the Nth element of the cell array
69642*/
69643static SQLITE_NOINLINE u16 computeCellSize(CellArray *p, int N){
69644 assert( N>=0 && N<p->nCell );
69645 assert( p->szCell[N]==0 );
69646 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
69647 return p->szCell[N];
69648}
69649static u16 cachedCellSize(CellArray *p, int N){
69650 assert( N>=0 && N<p->nCell );
69651 if( p->szCell[N] ) return p->szCell[N];
69652 return computeCellSize(p, N);
69653}
69654
69655/*
69656** Array apCell[] contains pointers to nCell b-tree page cells. The
69657** szCell[] array contains the size in bytes of each cell. This function
69658** replaces the current contents of page pPg with the contents of the cell
69659** array.
69660**
69661** Some of the cells in apCell[] may currently be stored in pPg. This
69662** function works around problems caused by this by making a copy of any
69663** such cells before overwriting the page data.
69664**
69665** The MemPage.nFree field is invalidated by this function. It is the
69666** responsibility of the caller to set it correctly.
69667*/
69668static int rebuildPage(
69669 MemPage *pPg, /* Edit this page */
69670 int nCell, /* Final number of cells on page */
69671 u8 **apCell, /* Array of cells */
69672 u16 *szCell /* Array of cell sizes */
69673){
69674 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
69675 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
69676 const int usableSize = pPg->pBt->usableSize;
69677 u8 * const pEnd = &aData[usableSize];
69678 int i;
69679 u8 *pCellptr = pPg->aCellIdx;
69680 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
69681 u8 *pData;
69682
69683 i = get2byte(&aData[hdr+5]);
69684 memcpy(&pTmp[i], &aData[i], usableSize - i);
69685
69686 pData = pEnd;
69687 for(i=0; i<nCell; i++){
69688 u8 *pCell = apCell[i];
69689 if( SQLITE_WITHIN(pCell,aData,pEnd) ){
69690 pCell = &pTmp[pCell - aData];
69691 }
69692 pData -= szCell[i];
69693 put2byte(pCellptr, (pData - aData));
69694 pCellptr += 2;
69695 if( pData < pCellptr ) return SQLITE_CORRUPT_BKPT;
69696 memcpy(pData, pCell, szCell[i]);
69697 assert( szCell[i]==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
69698 testcase( szCell[i]!=pPg->xCellSize(pPg,pCell) );
69699 }
69700
69701 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
69702 pPg->nCell = nCell;
69703 pPg->nOverflow = 0;
69704
69705 put2byte(&aData[hdr+1], 0);
69706 put2byte(&aData[hdr+3], pPg->nCell);
69707 put2byte(&aData[hdr+5], pData - aData);
69708 aData[hdr+7] = 0x00;
69709 return SQLITE_OK;
69710}
69711
69712/*
69713** Array apCell[] contains nCell pointers to b-tree cells. Array szCell
69714** contains the size in bytes of each such cell. This function attempts to
69715** add the cells stored in the array to page pPg. If it cannot (because
69716** the page needs to be defragmented before the cells will fit), non-zero
69717** is returned. Otherwise, if the cells are added successfully, zero is
69718** returned.
69719**
69720** Argument pCellptr points to the first entry in the cell-pointer array
69721** (part of page pPg) to populate. After cell apCell[0] is written to the
69722** page body, a 16-bit offset is written to pCellptr. And so on, for each
69723** cell in the array. It is the responsibility of the caller to ensure
69724** that it is safe to overwrite this part of the cell-pointer array.
69725**
69726** When this function is called, *ppData points to the start of the
69727** content area on page pPg. If the size of the content area is extended,
69728** *ppData is updated to point to the new start of the content area
69729** before returning.
69730**
69731** Finally, argument pBegin points to the byte immediately following the
69732** end of the space required by this page for the cell-pointer area (for
69733** all cells - not just those inserted by the current call). If the content
69734** area must be extended to before this point in order to accomodate all
69735** cells in apCell[], then the cells do not fit and non-zero is returned.
69736*/
69737static int pageInsertArray(
69738 MemPage *pPg, /* Page to add cells to */
69739 u8 *pBegin, /* End of cell-pointer array */
69740 u8 **ppData, /* IN/OUT: Page content -area pointer */
69741 u8 *pCellptr, /* Pointer to cell-pointer area */
69742 int iFirst, /* Index of first cell to add */
69743 int nCell, /* Number of cells to add to pPg */
69744 CellArray *pCArray /* Array of cells */
69745){
69746 int i;
69747 u8 *aData = pPg->aData;
69748 u8 *pData = *ppData;
69749 int iEnd = iFirst + nCell;
69750 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
69751 for(i=iFirst; i<iEnd; i++){
69752 int sz, rc;
69753 u8 *pSlot;
69754 sz = cachedCellSize(pCArray, i);
69755 if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){
69756 if( (pData - pBegin)<sz ) return 1;
69757 pData -= sz;
69758 pSlot = pData;
69759 }
69760 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
69761 ** database. But they might for a corrupt database. Hence use memmove()
69762 ** since memcpy() sends SIGABORT with overlapping buffers on OpenBSD */
69763 assert( (pSlot+sz)<=pCArray->apCell[i]
69764 || pSlot>=(pCArray->apCell[i]+sz)
69765 || CORRUPT_DB );
69766 memmove(pSlot, pCArray->apCell[i], sz);
69767 put2byte(pCellptr, (pSlot - aData));
69768 pCellptr += 2;
69769 }
69770 *ppData = pData;
69771 return 0;
69772}
69773
69774/*
69775** Array apCell[] contains nCell pointers to b-tree cells. Array szCell
69776** contains the size in bytes of each such cell. This function adds the
69777** space associated with each cell in the array that is currently stored
69778** within the body of pPg to the pPg free-list. The cell-pointers and other
69779** fields of the page are not updated.
69780**
69781** This function returns the total number of cells added to the free-list.
69782*/
69783static int pageFreeArray(
69784 MemPage *pPg, /* Page to edit */
69785 int iFirst, /* First cell to delete */
69786 int nCell, /* Cells to delete */
69787 CellArray *pCArray /* Array of cells */
69788){
69789 u8 * const aData = pPg->aData;
69790 u8 * const pEnd = &aData[pPg->pBt->usableSize];
69791 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
69792 int nRet = 0;
69793 int i;
69794 int iEnd = iFirst + nCell;
69795 u8 *pFree = 0;
69796 int szFree = 0;
69797
69798 for(i=iFirst; i<iEnd; i++){
69799 u8 *pCell = pCArray->apCell[i];
69800 if( SQLITE_WITHIN(pCell, pStart, pEnd) ){
69801 int sz;
69802 /* No need to use cachedCellSize() here. The sizes of all cells that
69803 ** are to be freed have already been computing while deciding which
69804 ** cells need freeing */
69805 sz = pCArray->szCell[i]; assert( sz>0 );
69806 if( pFree!=(pCell + sz) ){
69807 if( pFree ){
69808 assert( pFree>aData && (pFree - aData)<65536 );
69809 freeSpace(pPg, (u16)(pFree - aData), szFree);
69810 }
69811 pFree = pCell;
69812 szFree = sz;
69813 if( pFree+sz>pEnd ) return 0;
69814 }else{
69815 pFree = pCell;
69816 szFree += sz;
69817 }
69818 nRet++;
69819 }
69820 }
69821 if( pFree ){
69822 assert( pFree>aData && (pFree - aData)<65536 );
69823 freeSpace(pPg, (u16)(pFree - aData), szFree);
69824 }
69825 return nRet;
69826}
69827
69828/*
69829** apCell[] and szCell[] contains pointers to and sizes of all cells in the
69830** pages being balanced. The current page, pPg, has pPg->nCell cells starting
69831** with apCell[iOld]. After balancing, this page should hold nNew cells
69832** starting at apCell[iNew].
69833**
69834** This routine makes the necessary adjustments to pPg so that it contains
69835** the correct cells after being balanced.
69836**
69837** The pPg->nFree field is invalid when this function returns. It is the
69838** responsibility of the caller to set it correctly.
69839*/
69840static int editPage(
69841 MemPage *pPg, /* Edit this page */
69842 int iOld, /* Index of first cell currently on page */
69843 int iNew, /* Index of new first cell on page */
69844 int nNew, /* Final number of cells on page */
69845 CellArray *pCArray /* Array of cells and sizes */
69846){
69847 u8 * const aData = pPg->aData;
69848 const int hdr = pPg->hdrOffset;
69849 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
69850 int nCell = pPg->nCell; /* Cells stored on pPg */
69851 u8 *pData;
69852 u8 *pCellptr;
69853 int i;
69854 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
69855 int iNewEnd = iNew + nNew;
69856
69857#ifdef SQLITE_DEBUG
69858 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
69859 memcpy(pTmp, aData, pPg->pBt->usableSize);
69860#endif
69861
69862 /* Remove cells from the start and end of the page */
69863 if( iOld<iNew ){
69864 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
69865 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
69866 nCell -= nShift;
69867 }
69868 if( iNewEnd < iOldEnd ){
69869 nCell -= pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
69870 }
69871
69872 pData = &aData[get2byteNotZero(&aData[hdr+5])];
69873 if( pData<pBegin ) goto editpage_fail;
69874
69875 /* Add cells to the start of the page */
69876 if( iNew<iOld ){
69877 int nAdd = MIN(nNew,iOld-iNew);
69878 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
69879 pCellptr = pPg->aCellIdx;
69880 memmove(&pCellptr[nAdd*2], pCellptr, nCell*2);
69881 if( pageInsertArray(
69882 pPg, pBegin, &pData, pCellptr,
69883 iNew, nAdd, pCArray
69884 ) ) goto editpage_fail;
69885 nCell += nAdd;
69886 }
69887
69888 /* Add any overflow cells */
69889 for(i=0; i<pPg->nOverflow; i++){
69890 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
69891 if( iCell>=0 && iCell<nNew ){
69892 pCellptr = &pPg->aCellIdx[iCell * 2];
69893 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
69894 nCell++;
69895 if( pageInsertArray(
69896 pPg, pBegin, &pData, pCellptr,
69897 iCell+iNew, 1, pCArray
69898 ) ) goto editpage_fail;
69899 }
69900 }
69901
69902 /* Append cells to the end of the page */
69903 pCellptr = &pPg->aCellIdx[nCell*2];
69904 if( pageInsertArray(
69905 pPg, pBegin, &pData, pCellptr,
69906 iNew+nCell, nNew-nCell, pCArray
69907 ) ) goto editpage_fail;
69908
69909 pPg->nCell = nNew;
69910 pPg->nOverflow = 0;
69911
69912 put2byte(&aData[hdr+3], pPg->nCell);
69913 put2byte(&aData[hdr+5], pData - aData);
69914
69915#ifdef SQLITE_DEBUG
69916 for(i=0; i<nNew && !CORRUPT_DB; i++){
69917 u8 *pCell = pCArray->apCell[i+iNew];
69918 int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
69919 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
69920 pCell = &pTmp[pCell - aData];
69921 }
69922 assert( 0==memcmp(pCell, &aData[iOff],
69923 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
69924 }
69925#endif
69926
69927 return SQLITE_OK;
69928 editpage_fail:
69929 /* Unable to edit this page. Rebuild it from scratch instead. */
69930 populateCellCache(pCArray, iNew, nNew);
69931 return rebuildPage(pPg, nNew, &pCArray->apCell[iNew], &pCArray->szCell[iNew]);
69932}
69933
69934/*
69935** The following parameters determine how many adjacent pages get involved
69936** in a balancing operation. NN is the number of neighbors on either side
69937** of the page that participate in the balancing operation. NB is the
69938** total number of pages that participate, including the target page and
69939** NN neighbors on either side.
69940**
69941** The minimum value of NN is 1 (of course). Increasing NN above 1
69942** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
69943** in exchange for a larger degradation in INSERT and UPDATE performance.
69944** The value of NN appears to give the best results overall.
69945*/
69946#define NN 1 /* Number of neighbors on either side of pPage */
69947#define NB (NN*2+1) /* Total pages involved in the balance */
69948
69949
69950#ifndef SQLITE_OMIT_QUICKBALANCE
69951/*
69952** This version of balance() handles the common special case where
69953** a new entry is being inserted on the extreme right-end of the
69954** tree, in other words, when the new entry will become the largest
69955** entry in the tree.
69956**
69957** Instead of trying to balance the 3 right-most leaf pages, just add
69958** a new page to the right-hand side and put the one new entry in
69959** that page. This leaves the right side of the tree somewhat
69960** unbalanced. But odds are that we will be inserting new entries
69961** at the end soon afterwards so the nearly empty page will quickly
69962** fill up. On average.
69963**
69964** pPage is the leaf page which is the right-most page in the tree.
69965** pParent is its parent. pPage must have a single overflow entry
69966** which is also the right-most entry on the page.
69967**
69968** The pSpace buffer is used to store a temporary copy of the divider
69969** cell that will be inserted into pParent. Such a cell consists of a 4
69970** byte page number followed by a variable length integer. In other
69971** words, at most 13 bytes. Hence the pSpace buffer must be at
69972** least 13 bytes in size.
69973*/
69974static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
69975 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
69976 MemPage *pNew; /* Newly allocated page */
69977 int rc; /* Return Code */
69978 Pgno pgnoNew; /* Page number of pNew */
69979
69980 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
69981 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
69982 assert( pPage->nOverflow==1 );
69983
69984 /* This error condition is now caught prior to reaching this function */
69985 if( NEVER(pPage->nCell==0) ) return SQLITE_CORRUPT_BKPT;
69986
69987 /* Allocate a new page. This page will become the right-sibling of
69988 ** pPage. Make the parent page writable, so that the new divider cell
69989 ** may be inserted. If both these operations are successful, proceed.
69990 */
69991 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
69992
69993 if( rc==SQLITE_OK ){
69994
69995 u8 *pOut = &pSpace[4];
69996 u8 *pCell = pPage->apOvfl[0];
69997 u16 szCell = pPage->xCellSize(pPage, pCell);
69998 u8 *pStop;
69999
70000 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
70001 assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
70002 zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
70003 rc = rebuildPage(pNew, 1, &pCell, &szCell);
70004 if( NEVER(rc) ) return rc;
70005 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
70006
70007 /* If this is an auto-vacuum database, update the pointer map
70008 ** with entries for the new page, and any pointer from the
70009 ** cell on the page to an overflow page. If either of these
70010 ** operations fails, the return code is set, but the contents
70011 ** of the parent page are still manipulated by thh code below.
70012 ** That is Ok, at this point the parent page is guaranteed to
70013 ** be marked as dirty. Returning an error code will cause a
70014 ** rollback, undoing any changes made to the parent page.
70015 */
70016 if( ISAUTOVACUUM ){
70017 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
70018 if( szCell>pNew->minLocal ){
70019 ptrmapPutOvflPtr(pNew, pCell, &rc);
70020 }
70021 }
70022
70023 /* Create a divider cell to insert into pParent. The divider cell
70024 ** consists of a 4-byte page number (the page number of pPage) and
70025 ** a variable length key value (which must be the same value as the
70026 ** largest key on pPage).
70027 **
70028 ** To find the largest key value on pPage, first find the right-most
70029 ** cell on pPage. The first two fields of this cell are the
70030 ** record-length (a variable length integer at most 32-bits in size)
70031 ** and the key value (a variable length integer, may have any value).
70032 ** The first of the while(...) loops below skips over the record-length
70033 ** field. The second while(...) loop copies the key value from the
70034 ** cell on pPage into the pSpace buffer.
70035 */
70036 pCell = findCell(pPage, pPage->nCell-1);
70037 pStop = &pCell[9];
70038 while( (*(pCell++)&0x80) && pCell<pStop );
70039 pStop = &pCell[9];
70040 while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
70041
70042 /* Insert the new divider cell into pParent. */
70043 if( rc==SQLITE_OK ){
70044 insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
70045 0, pPage->pgno, &rc);
70046 }
70047
70048 /* Set the right-child pointer of pParent to point to the new page. */
70049 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
70050
70051 /* Release the reference to the new page. */
70052 releasePage(pNew);
70053 }
70054
70055 return rc;
70056}
70057#endif /* SQLITE_OMIT_QUICKBALANCE */
70058
70059#if 0
70060/*
70061** This function does not contribute anything to the operation of SQLite.
70062** it is sometimes activated temporarily while debugging code responsible
70063** for setting pointer-map entries.
70064*/
70065static int ptrmapCheckPages(MemPage **apPage, int nPage){
70066 int i, j;
70067 for(i=0; i<nPage; i++){
70068 Pgno n;
70069 u8 e;
70070 MemPage *pPage = apPage[i];
70071 BtShared *pBt = pPage->pBt;
70072 assert( pPage->isInit );
70073
70074 for(j=0; j<pPage->nCell; j++){
70075 CellInfo info;
70076 u8 *z;
70077
70078 z = findCell(pPage, j);
70079 pPage->xParseCell(pPage, z, &info);
70080 if( info.nLocal<info.nPayload ){
70081 Pgno ovfl = get4byte(&z[info.nSize-4]);
70082 ptrmapGet(pBt, ovfl, &e, &n);
70083 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
70084 }
70085 if( !pPage->leaf ){
70086 Pgno child = get4byte(z);
70087 ptrmapGet(pBt, child, &e, &n);
70088 assert( n==pPage->pgno && e==PTRMAP_BTREE );
70089 }
70090 }
70091 if( !pPage->leaf ){
70092 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
70093 ptrmapGet(pBt, child, &e, &n);
70094 assert( n==pPage->pgno && e==PTRMAP_BTREE );
70095 }
70096 }
70097 return 1;
70098}
70099#endif
70100
70101/*
70102** This function is used to copy the contents of the b-tree node stored
70103** on page pFrom to page pTo. If page pFrom was not a leaf page, then
70104** the pointer-map entries for each child page are updated so that the
70105** parent page stored in the pointer map is page pTo. If pFrom contained
70106** any cells with overflow page pointers, then the corresponding pointer
70107** map entries are also updated so that the parent page is page pTo.
70108**
70109** If pFrom is currently carrying any overflow cells (entries in the
70110** MemPage.apOvfl[] array), they are not copied to pTo.
70111**
70112** Before returning, page pTo is reinitialized using btreeInitPage().
70113**
70114** The performance of this function is not critical. It is only used by
70115** the balance_shallower() and balance_deeper() procedures, neither of
70116** which are called often under normal circumstances.
70117*/
70118static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
70119 if( (*pRC)==SQLITE_OK ){
70120 BtShared * const pBt = pFrom->pBt;
70121 u8 * const aFrom = pFrom->aData;
70122 u8 * const aTo = pTo->aData;
70123 int const iFromHdr = pFrom->hdrOffset;
70124 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
70125 int rc;
70126 int iData;
70127
70128
70129 assert( pFrom->isInit );
70130 assert( pFrom->nFree>=iToHdr );
70131 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
70132
70133 /* Copy the b-tree node content from page pFrom to page pTo. */
70134 iData = get2byte(&aFrom[iFromHdr+5]);
70135 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
70136 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
70137
70138 /* Reinitialize page pTo so that the contents of the MemPage structure
70139 ** match the new data. The initialization of pTo can actually fail under
70140 ** fairly obscure circumstances, even though it is a copy of initialized
70141 ** page pFrom.
70142 */
70143 pTo->isInit = 0;
70144 rc = btreeInitPage(pTo);
70145 if( rc!=SQLITE_OK ){
70146 *pRC = rc;
70147 return;
70148 }
70149
70150 /* If this is an auto-vacuum database, update the pointer-map entries
70151 ** for any b-tree or overflow pages that pTo now contains the pointers to.
70152 */
70153 if( ISAUTOVACUUM ){
70154 *pRC = setChildPtrmaps(pTo);
70155 }
70156 }
70157}
70158
70159/*
70160** This routine redistributes cells on the iParentIdx'th child of pParent
70161** (hereafter "the page") and up to 2 siblings so that all pages have about the
70162** same amount of free space. Usually a single sibling on either side of the
70163** page are used in the balancing, though both siblings might come from one
70164** side if the page is the first or last child of its parent. If the page
70165** has fewer than 2 siblings (something which can only happen if the page
70166** is a root page or a child of a root page) then all available siblings
70167** participate in the balancing.
70168**
70169** The number of siblings of the page might be increased or decreased by
70170** one or two in an effort to keep pages nearly full but not over full.
70171**
70172** Note that when this routine is called, some of the cells on the page
70173** might not actually be stored in MemPage.aData[]. This can happen
70174** if the page is overfull. This routine ensures that all cells allocated
70175** to the page and its siblings fit into MemPage.aData[] before returning.
70176**
70177** In the course of balancing the page and its siblings, cells may be
70178** inserted into or removed from the parent page (pParent). Doing so
70179** may cause the parent page to become overfull or underfull. If this
70180** happens, it is the responsibility of the caller to invoke the correct
70181** balancing routine to fix this problem (see the balance() routine).
70182**
70183** If this routine fails for any reason, it might leave the database
70184** in a corrupted state. So if this routine fails, the database should
70185** be rolled back.
70186**
70187** The third argument to this function, aOvflSpace, is a pointer to a
70188** buffer big enough to hold one page. If while inserting cells into the parent
70189** page (pParent) the parent page becomes overfull, this buffer is
70190** used to store the parent's overflow cells. Because this function inserts
70191** a maximum of four divider cells into the parent page, and the maximum
70192** size of a cell stored within an internal node is always less than 1/4
70193** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
70194** enough for all overflow cells.
70195**
70196** If aOvflSpace is set to a null pointer, this function returns
70197** SQLITE_NOMEM.
70198*/
70199static int balance_nonroot(
70200 MemPage *pParent, /* Parent page of siblings being balanced */
70201 int iParentIdx, /* Index of "the page" in pParent */
70202 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
70203 int isRoot, /* True if pParent is a root-page */
70204 int bBulk /* True if this call is part of a bulk load */
70205){
70206 BtShared *pBt; /* The whole database */
70207 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
70208 int nNew = 0; /* Number of pages in apNew[] */
70209 int nOld; /* Number of pages in apOld[] */
70210 int i, j, k; /* Loop counters */
70211 int nxDiv; /* Next divider slot in pParent->aCell[] */
70212 int rc = SQLITE_OK; /* The return code */
70213 u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */
70214 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
70215 int usableSpace; /* Bytes in pPage beyond the header */
70216 int pageFlags; /* Value of pPage->aData[0] */
70217 int iSpace1 = 0; /* First unused byte of aSpace1[] */
70218 int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
70219 int szScratch; /* Size of scratch memory requested */
70220 MemPage *apOld[NB]; /* pPage and up to two siblings */
70221 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
70222 u8 *pRight; /* Location in parent of right-sibling pointer */
70223 u8 *apDiv[NB-1]; /* Divider cells in pParent */
70224 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
70225 int cntOld[NB+2]; /* Old index in b.apCell[] */
70226 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
70227 u8 *aSpace1; /* Space for copies of dividers cells */
70228 Pgno pgno; /* Temp var to store a page number in */
70229 u8 abDone[NB+2]; /* True after i'th new page is populated */
70230 Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */
70231 Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */
70232 u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */
70233 CellArray b; /* Parsed information on cells being balanced */
70234
70235 memset(abDone, 0, sizeof(abDone));
70236 b.nCell = 0;
70237 b.apCell = 0;
70238 pBt = pParent->pBt;
70239 assert( sqlite3_mutex_held(pBt->mutex) );
70240 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
70241
70242#if 0
70243 TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
70244#endif
70245
70246 /* At this point pParent may have at most one overflow cell. And if
70247 ** this overflow cell is present, it must be the cell with
70248 ** index iParentIdx. This scenario comes about when this function
70249 ** is called (indirectly) from sqlite3BtreeDelete().
70250 */
70251 assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
70252 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
70253
70254 if( !aOvflSpace ){
70255 return SQLITE_NOMEM_BKPT;
70256 }
70257
70258 /* Find the sibling pages to balance. Also locate the cells in pParent
70259 ** that divide the siblings. An attempt is made to find NN siblings on
70260 ** either side of pPage. More siblings are taken from one side, however,
70261 ** if there are fewer than NN siblings on the other side. If pParent
70262 ** has NB or fewer children then all children of pParent are taken.
70263 **
70264 ** This loop also drops the divider cells from the parent page. This
70265 ** way, the remainder of the function does not have to deal with any
70266 ** overflow cells in the parent page, since if any existed they will
70267 ** have already been removed.
70268 */
70269 i = pParent->nOverflow + pParent->nCell;
70270 if( i<2 ){
70271 nxDiv = 0;
70272 }else{
70273 assert( bBulk==0 || bBulk==1 );
70274 if( iParentIdx==0 ){
70275 nxDiv = 0;
70276 }else if( iParentIdx==i ){
70277 nxDiv = i-2+bBulk;
70278 }else{
70279 nxDiv = iParentIdx-1;
70280 }
70281 i = 2-bBulk;
70282 }
70283 nOld = i+1;
70284 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
70285 pRight = &pParent->aData[pParent->hdrOffset+8];
70286 }else{
70287 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
70288 }
70289 pgno = get4byte(pRight);
70290 while( 1 ){
70291 rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0);
70292 if( rc ){
70293 memset(apOld, 0, (i+1)*sizeof(MemPage*));
70294 goto balance_cleanup;
70295 }
70296 nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
70297 if( (i--)==0 ) break;
70298
70299 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
70300 apDiv[i] = pParent->apOvfl[0];
70301 pgno = get4byte(apDiv[i]);
70302 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
70303 pParent->nOverflow = 0;
70304 }else{
70305 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
70306 pgno = get4byte(apDiv[i]);
70307 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
70308
70309 /* Drop the cell from the parent page. apDiv[i] still points to
70310 ** the cell within the parent, even though it has been dropped.
70311 ** This is safe because dropping a cell only overwrites the first
70312 ** four bytes of it, and this function does not need the first
70313 ** four bytes of the divider cell. So the pointer is safe to use
70314 ** later on.
70315 **
70316 ** But not if we are in secure-delete mode. In secure-delete mode,
70317 ** the dropCell() routine will overwrite the entire cell with zeroes.
70318 ** In this case, temporarily copy the cell into the aOvflSpace[]
70319 ** buffer. It will be copied out again as soon as the aSpace[] buffer
70320 ** is allocated. */
70321 if( pBt->btsFlags & BTS_FAST_SECURE ){
70322 int iOff;
70323
70324 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
70325 if( (iOff+szNew[i])>(int)pBt->usableSize ){
70326 rc = SQLITE_CORRUPT_BKPT;
70327 memset(apOld, 0, (i+1)*sizeof(MemPage*));
70328 goto balance_cleanup;
70329 }else{
70330 memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
70331 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
70332 }
70333 }
70334 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
70335 }
70336 }
70337
70338 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
70339 ** alignment */
70340 nMaxCells = (nMaxCells + 3)&~3;
70341
70342 /*
70343 ** Allocate space for memory structures
70344 */
70345 szScratch =
70346 nMaxCells*sizeof(u8*) /* b.apCell */
70347 + nMaxCells*sizeof(u16) /* b.szCell */
70348 + pBt->pageSize; /* aSpace1 */
70349
70350 assert( szScratch<=6*(int)pBt->pageSize );
70351 b.apCell = sqlite3StackAllocRaw(0, szScratch );
70352 if( b.apCell==0 ){
70353 rc = SQLITE_NOMEM_BKPT;
70354 goto balance_cleanup;
70355 }
70356 b.szCell = (u16*)&b.apCell[nMaxCells];
70357 aSpace1 = (u8*)&b.szCell[nMaxCells];
70358 assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
70359
70360 /*
70361 ** Load pointers to all cells on sibling pages and the divider cells
70362 ** into the local b.apCell[] array. Make copies of the divider cells
70363 ** into space obtained from aSpace1[]. The divider cells have already
70364 ** been removed from pParent.
70365 **
70366 ** If the siblings are on leaf pages, then the child pointers of the
70367 ** divider cells are stripped from the cells before they are copied
70368 ** into aSpace1[]. In this way, all cells in b.apCell[] are without
70369 ** child pointers. If siblings are not leaves, then all cell in
70370 ** b.apCell[] include child pointers. Either way, all cells in b.apCell[]
70371 ** are alike.
70372 **
70373 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
70374 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
70375 */
70376 b.pRef = apOld[0];
70377 leafCorrection = b.pRef->leaf*4;
70378 leafData = b.pRef->intKeyLeaf;
70379 for(i=0; i<nOld; i++){
70380 MemPage *pOld = apOld[i];
70381 int limit = pOld->nCell;
70382 u8 *aData = pOld->aData;
70383 u16 maskPage = pOld->maskPage;
70384 u8 *piCell = aData + pOld->cellOffset;
70385 u8 *piEnd;
70386
70387 /* Verify that all sibling pages are of the same "type" (table-leaf,
70388 ** table-interior, index-leaf, or index-interior).
70389 */
70390 if( pOld->aData[0]!=apOld[0]->aData[0] ){
70391 rc = SQLITE_CORRUPT_BKPT;
70392 goto balance_cleanup;
70393 }
70394
70395 /* Load b.apCell[] with pointers to all cells in pOld. If pOld
70396 ** contains overflow cells, include them in the b.apCell[] array
70397 ** in the correct spot.
70398 **
70399 ** Note that when there are multiple overflow cells, it is always the
70400 ** case that they are sequential and adjacent. This invariant arises
70401 ** because multiple overflows can only occurs when inserting divider
70402 ** cells into a parent on a prior balance, and divider cells are always
70403 ** adjacent and are inserted in order. There is an assert() tagged
70404 ** with "NOTE 1" in the overflow cell insertion loop to prove this
70405 ** invariant.
70406 **
70407 ** This must be done in advance. Once the balance starts, the cell
70408 ** offset section of the btree page will be overwritten and we will no
70409 ** long be able to find the cells if a pointer to each cell is not saved
70410 ** first.
70411 */
70412 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
70413 if( pOld->nOverflow>0 ){
70414 limit = pOld->aiOvfl[0];
70415 for(j=0; j<limit; j++){
70416 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
70417 piCell += 2;
70418 b.nCell++;
70419 }
70420 for(k=0; k<pOld->nOverflow; k++){
70421 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
70422 b.apCell[b.nCell] = pOld->apOvfl[k];
70423 b.nCell++;
70424 }
70425 }
70426 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
70427 while( piCell<piEnd ){
70428 assert( b.nCell<nMaxCells );
70429 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
70430 piCell += 2;
70431 b.nCell++;
70432 }
70433
70434 cntOld[i] = b.nCell;
70435 if( i<nOld-1 && !leafData){
70436 u16 sz = (u16)szNew[i];
70437 u8 *pTemp;
70438 assert( b.nCell<nMaxCells );
70439 b.szCell[b.nCell] = sz;
70440 pTemp = &aSpace1[iSpace1];
70441 iSpace1 += sz;
70442 assert( sz<=pBt->maxLocal+23 );
70443 assert( iSpace1 <= (int)pBt->pageSize );
70444 memcpy(pTemp, apDiv[i], sz);
70445 b.apCell[b.nCell] = pTemp+leafCorrection;
70446 assert( leafCorrection==0 || leafCorrection==4 );
70447 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
70448 if( !pOld->leaf ){
70449 assert( leafCorrection==0 );
70450 assert( pOld->hdrOffset==0 );
70451 /* The right pointer of the child page pOld becomes the left
70452 ** pointer of the divider cell */
70453 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
70454 }else{
70455 assert( leafCorrection==4 );
70456 while( b.szCell[b.nCell]<4 ){
70457 /* Do not allow any cells smaller than 4 bytes. If a smaller cell
70458 ** does exist, pad it with 0x00 bytes. */
70459 assert( b.szCell[b.nCell]==3 || CORRUPT_DB );
70460 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
70461 aSpace1[iSpace1++] = 0x00;
70462 b.szCell[b.nCell]++;
70463 }
70464 }
70465 b.nCell++;
70466 }
70467 }
70468
70469 /*
70470 ** Figure out the number of pages needed to hold all b.nCell cells.
70471 ** Store this number in "k". Also compute szNew[] which is the total
70472 ** size of all cells on the i-th page and cntNew[] which is the index
70473 ** in b.apCell[] of the cell that divides page i from page i+1.
70474 ** cntNew[k] should equal b.nCell.
70475 **
70476 ** Values computed by this block:
70477 **
70478 ** k: The total number of sibling pages
70479 ** szNew[i]: Spaced used on the i-th sibling page.
70480 ** cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to
70481 ** the right of the i-th sibling page.
70482 ** usableSpace: Number of bytes of space available on each sibling.
70483 **
70484 */
70485 usableSpace = pBt->usableSize - 12 + leafCorrection;
70486 for(i=0; i<nOld; i++){
70487 MemPage *p = apOld[i];
70488 szNew[i] = usableSpace - p->nFree;
70489 for(j=0; j<p->nOverflow; j++){
70490 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
70491 }
70492 cntNew[i] = cntOld[i];
70493 }
70494 k = nOld;
70495 for(i=0; i<k; i++){
70496 int sz;
70497 while( szNew[i]>usableSpace ){
70498 if( i+1>=k ){
70499 k = i+2;
70500 if( k>NB+2 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
70501 szNew[k-1] = 0;
70502 cntNew[k-1] = b.nCell;
70503 }
70504 sz = 2 + cachedCellSize(&b, cntNew[i]-1);
70505 szNew[i] -= sz;
70506 if( !leafData ){
70507 if( cntNew[i]<b.nCell ){
70508 sz = 2 + cachedCellSize(&b, cntNew[i]);
70509 }else{
70510 sz = 0;
70511 }
70512 }
70513 szNew[i+1] += sz;
70514 cntNew[i]--;
70515 }
70516 while( cntNew[i]<b.nCell ){
70517 sz = 2 + cachedCellSize(&b, cntNew[i]);
70518 if( szNew[i]+sz>usableSpace ) break;
70519 szNew[i] += sz;
70520 cntNew[i]++;
70521 if( !leafData ){
70522 if( cntNew[i]<b.nCell ){
70523 sz = 2 + cachedCellSize(&b, cntNew[i]);
70524 }else{
70525 sz = 0;
70526 }
70527 }
70528 szNew[i+1] -= sz;
70529 }
70530 if( cntNew[i]>=b.nCell ){
70531 k = i+1;
70532 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
70533 rc = SQLITE_CORRUPT_BKPT;
70534 goto balance_cleanup;
70535 }
70536 }
70537
70538 /*
70539 ** The packing computed by the previous block is biased toward the siblings
70540 ** on the left side (siblings with smaller keys). The left siblings are
70541 ** always nearly full, while the right-most sibling might be nearly empty.
70542 ** The next block of code attempts to adjust the packing of siblings to
70543 ** get a better balance.
70544 **
70545 ** This adjustment is more than an optimization. The packing above might
70546 ** be so out of balance as to be illegal. For example, the right-most
70547 ** sibling might be completely empty. This adjustment is not optional.
70548 */
70549 for(i=k-1; i>0; i--){
70550 int szRight = szNew[i]; /* Size of sibling on the right */
70551 int szLeft = szNew[i-1]; /* Size of sibling on the left */
70552 int r; /* Index of right-most cell in left sibling */
70553 int d; /* Index of first cell to the left of right sibling */
70554
70555 r = cntNew[i-1] - 1;
70556 d = r + 1 - leafData;
70557 (void)cachedCellSize(&b, d);
70558 do{
70559 assert( d<nMaxCells );
70560 assert( r<nMaxCells );
70561 (void)cachedCellSize(&b, r);
70562 if( szRight!=0
70563 && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+(i==k-1?0:2)))){
70564 break;
70565 }
70566 szRight += b.szCell[d] + 2;
70567 szLeft -= b.szCell[r] + 2;
70568 cntNew[i-1] = r;
70569 r--;
70570 d--;
70571 }while( r>=0 );
70572 szNew[i] = szRight;
70573 szNew[i-1] = szLeft;
70574 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
70575 rc = SQLITE_CORRUPT_BKPT;
70576 goto balance_cleanup;
70577 }
70578 }
70579
70580 /* Sanity check: For a non-corrupt database file one of the follwing
70581 ** must be true:
70582 ** (1) We found one or more cells (cntNew[0])>0), or
70583 ** (2) pPage is a virtual root page. A virtual root page is when
70584 ** the real root page is page 1 and we are the only child of
70585 ** that page.
70586 */
70587 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB);
70588 TRACE(("BALANCE: old: %d(nc=%d) %d(nc=%d) %d(nc=%d)\n",
70589 apOld[0]->pgno, apOld[0]->nCell,
70590 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
70591 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
70592 ));
70593
70594 /*
70595 ** Allocate k new pages. Reuse old pages where possible.
70596 */
70597 pageFlags = apOld[0]->aData[0];
70598 for(i=0; i<k; i++){
70599 MemPage *pNew;
70600 if( i<nOld ){
70601 pNew = apNew[i] = apOld[i];
70602 apOld[i] = 0;
70603 rc = sqlite3PagerWrite(pNew->pDbPage);
70604 nNew++;
70605 if( rc ) goto balance_cleanup;
70606 }else{
70607 assert( i>0 );
70608 rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
70609 if( rc ) goto balance_cleanup;
70610 zeroPage(pNew, pageFlags);
70611 apNew[i] = pNew;
70612 nNew++;
70613 cntOld[i] = b.nCell;
70614
70615 /* Set the pointer-map entry for the new sibling page. */
70616 if( ISAUTOVACUUM ){
70617 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
70618 if( rc!=SQLITE_OK ){
70619 goto balance_cleanup;
70620 }
70621 }
70622 }
70623 }
70624
70625 /*
70626 ** Reassign page numbers so that the new pages are in ascending order.
70627 ** This helps to keep entries in the disk file in order so that a scan
70628 ** of the table is closer to a linear scan through the file. That in turn
70629 ** helps the operating system to deliver pages from the disk more rapidly.
70630 **
70631 ** An O(n^2) insertion sort algorithm is used, but since n is never more
70632 ** than (NB+2) (a small constant), that should not be a problem.
70633 **
70634 ** When NB==3, this one optimization makes the database about 25% faster
70635 ** for large insertions and deletions.
70636 */
70637 for(i=0; i<nNew; i++){
70638 aPgOrder[i] = aPgno[i] = apNew[i]->pgno;
70639 aPgFlags[i] = apNew[i]->pDbPage->flags;
70640 for(j=0; j<i; j++){
70641 if( aPgno[j]==aPgno[i] ){
70642 /* This branch is taken if the set of sibling pages somehow contains
70643 ** duplicate entries. This can happen if the database is corrupt.
70644 ** It would be simpler to detect this as part of the loop below, but
70645 ** we do the detection here in order to avoid populating the pager
70646 ** cache with two separate objects associated with the same
70647 ** page number. */
70648 assert( CORRUPT_DB );
70649 rc = SQLITE_CORRUPT_BKPT;
70650 goto balance_cleanup;
70651 }
70652 }
70653 }
70654 for(i=0; i<nNew; i++){
70655 int iBest = 0; /* aPgno[] index of page number to use */
70656 for(j=1; j<nNew; j++){
70657 if( aPgOrder[j]<aPgOrder[iBest] ) iBest = j;
70658 }
70659 pgno = aPgOrder[iBest];
70660 aPgOrder[iBest] = 0xffffffff;
70661 if( iBest!=i ){
70662 if( iBest>i ){
70663 sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0);
70664 }
70665 sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]);
70666 apNew[i]->pgno = pgno;
70667 }
70668 }
70669
70670 TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) "
70671 "%d(%d nc=%d) %d(%d nc=%d)\n",
70672 apNew[0]->pgno, szNew[0], cntNew[0],
70673 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
70674 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
70675 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
70676 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
70677 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
70678 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
70679 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
70680 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
70681 ));
70682
70683 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
70684 put4byte(pRight, apNew[nNew-1]->pgno);
70685
70686 /* If the sibling pages are not leaves, ensure that the right-child pointer
70687 ** of the right-most new sibling page is set to the value that was
70688 ** originally in the same field of the right-most old sibling page. */
70689 if( (pageFlags & PTF_LEAF)==0 && nOld!=nNew ){
70690 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
70691 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
70692 }
70693
70694 /* Make any required updates to pointer map entries associated with
70695 ** cells stored on sibling pages following the balance operation. Pointer
70696 ** map entries associated with divider cells are set by the insertCell()
70697 ** routine. The associated pointer map entries are:
70698 **
70699 ** a) if the cell contains a reference to an overflow chain, the
70700 ** entry associated with the first page in the overflow chain, and
70701 **
70702 ** b) if the sibling pages are not leaves, the child page associated
70703 ** with the cell.
70704 **
70705 ** If the sibling pages are not leaves, then the pointer map entry
70706 ** associated with the right-child of each sibling may also need to be
70707 ** updated. This happens below, after the sibling pages have been
70708 ** populated, not here.
70709 */
70710 if( ISAUTOVACUUM ){
70711 MemPage *pNew = apNew[0];
70712 u8 *aOld = pNew->aData;
70713 int cntOldNext = pNew->nCell + pNew->nOverflow;
70714 int usableSize = pBt->usableSize;
70715 int iNew = 0;
70716 int iOld = 0;
70717
70718 for(i=0; i<b.nCell; i++){
70719 u8 *pCell = b.apCell[i];
70720 if( i==cntOldNext ){
70721 MemPage *pOld = (++iOld)<nNew ? apNew[iOld] : apOld[iOld];
70722 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
70723 aOld = pOld->aData;
70724 }
70725 if( i==cntNew[iNew] ){
70726 pNew = apNew[++iNew];
70727 if( !leafData ) continue;
70728 }
70729
70730 /* Cell pCell is destined for new sibling page pNew. Originally, it
70731 ** was either part of sibling page iOld (possibly an overflow cell),
70732 ** or else the divider cell to the left of sibling page iOld. So,
70733 ** if sibling page iOld had the same page number as pNew, and if
70734 ** pCell really was a part of sibling page iOld (not a divider or
70735 ** overflow cell), we can skip updating the pointer map entries. */
70736 if( iOld>=nNew
70737 || pNew->pgno!=aPgno[iOld]
70738 || !SQLITE_WITHIN(pCell,aOld,&aOld[usableSize])
70739 ){
70740 if( !leafCorrection ){
70741 ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
70742 }
70743 if( cachedCellSize(&b,i)>pNew->minLocal ){
70744 ptrmapPutOvflPtr(pNew, pCell, &rc);
70745 }
70746 if( rc ) goto balance_cleanup;
70747 }
70748 }
70749 }
70750
70751 /* Insert new divider cells into pParent. */
70752 for(i=0; i<nNew-1; i++){
70753 u8 *pCell;
70754 u8 *pTemp;
70755 int sz;
70756 MemPage *pNew = apNew[i];
70757 j = cntNew[i];
70758
70759 assert( j<nMaxCells );
70760 assert( b.apCell[j]!=0 );
70761 pCell = b.apCell[j];
70762 sz = b.szCell[j] + leafCorrection;
70763 pTemp = &aOvflSpace[iOvflSpace];
70764 if( !pNew->leaf ){
70765 memcpy(&pNew->aData[8], pCell, 4);
70766 }else if( leafData ){
70767 /* If the tree is a leaf-data tree, and the siblings are leaves,
70768 ** then there is no divider cell in b.apCell[]. Instead, the divider
70769 ** cell consists of the integer key for the right-most cell of
70770 ** the sibling-page assembled above only.
70771 */
70772 CellInfo info;
70773 j--;
70774 pNew->xParseCell(pNew, b.apCell[j], &info);
70775 pCell = pTemp;
70776 sz = 4 + putVarint(&pCell[4], info.nKey);
70777 pTemp = 0;
70778 }else{
70779 pCell -= 4;
70780 /* Obscure case for non-leaf-data trees: If the cell at pCell was
70781 ** previously stored on a leaf node, and its reported size was 4
70782 ** bytes, then it may actually be smaller than this
70783 ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
70784 ** any cell). But it is important to pass the correct size to
70785 ** insertCell(), so reparse the cell now.
70786 **
70787 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
70788 ** and WITHOUT ROWID tables with exactly one column which is the
70789 ** primary key.
70790 */
70791 if( b.szCell[j]==4 ){
70792 assert(leafCorrection==4);
70793 sz = pParent->xCellSize(pParent, pCell);
70794 }
70795 }
70796 iOvflSpace += sz;
70797 assert( sz<=pBt->maxLocal+23 );
70798 assert( iOvflSpace <= (int)pBt->pageSize );
70799 insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc);
70800 if( rc!=SQLITE_OK ) goto balance_cleanup;
70801 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
70802 }
70803
70804 /* Now update the actual sibling pages. The order in which they are updated
70805 ** is important, as this code needs to avoid disrupting any page from which
70806 ** cells may still to be read. In practice, this means:
70807 **
70808 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
70809 ** then it is not safe to update page apNew[iPg] until after
70810 ** the left-hand sibling apNew[iPg-1] has been updated.
70811 **
70812 ** (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1])
70813 ** then it is not safe to update page apNew[iPg] until after
70814 ** the right-hand sibling apNew[iPg+1] has been updated.
70815 **
70816 ** If neither of the above apply, the page is safe to update.
70817 **
70818 ** The iPg value in the following loop starts at nNew-1 goes down
70819 ** to 0, then back up to nNew-1 again, thus making two passes over
70820 ** the pages. On the initial downward pass, only condition (1) above
70821 ** needs to be tested because (2) will always be true from the previous
70822 ** step. On the upward pass, both conditions are always true, so the
70823 ** upwards pass simply processes pages that were missed on the downward
70824 ** pass.
70825 */
70826 for(i=1-nNew; i<nNew; i++){
70827 int iPg = i<0 ? -i : i;
70828 assert( iPg>=0 && iPg<nNew );
70829 if( abDone[iPg] ) continue; /* Skip pages already processed */
70830 if( i>=0 /* On the upwards pass, or... */
70831 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
70832 ){
70833 int iNew;
70834 int iOld;
70835 int nNewCell;
70836
70837 /* Verify condition (1): If cells are moving left, update iPg
70838 ** only after iPg-1 has already been updated. */
70839 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
70840
70841 /* Verify condition (2): If cells are moving right, update iPg
70842 ** only after iPg+1 has already been updated. */
70843 assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] );
70844
70845 if( iPg==0 ){
70846 iNew = iOld = 0;
70847 nNewCell = cntNew[0];
70848 }else{
70849 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
70850 iNew = cntNew[iPg-1] + !leafData;
70851 nNewCell = cntNew[iPg] - iNew;
70852 }
70853
70854 rc = editPage(apNew[iPg], iOld, iNew, nNewCell, &b);
70855 if( rc ) goto balance_cleanup;
70856 abDone[iPg]++;
70857 apNew[iPg]->nFree = usableSpace-szNew[iPg];
70858 assert( apNew[iPg]->nOverflow==0 );
70859 assert( apNew[iPg]->nCell==nNewCell );
70860 }
70861 }
70862
70863 /* All pages have been processed exactly once */
70864 assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 );
70865
70866 assert( nOld>0 );
70867 assert( nNew>0 );
70868
70869 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
70870 /* The root page of the b-tree now contains no cells. The only sibling
70871 ** page is the right-child of the parent. Copy the contents of the
70872 ** child page into the parent, decreasing the overall height of the
70873 ** b-tree structure by one. This is described as the "balance-shallower"
70874 ** sub-algorithm in some documentation.
70875 **
70876 ** If this is an auto-vacuum database, the call to copyNodeContent()
70877 ** sets all pointer-map entries corresponding to database image pages
70878 ** for which the pointer is stored within the content being copied.
70879 **
70880 ** It is critical that the child page be defragmented before being
70881 ** copied into the parent, because if the parent is page 1 then it will
70882 ** by smaller than the child due to the database header, and so all the
70883 ** free space needs to be up front.
70884 */
70885 assert( nNew==1 || CORRUPT_DB );
70886 rc = defragmentPage(apNew[0], -1);
70887 testcase( rc!=SQLITE_OK );
70888 assert( apNew[0]->nFree ==
70889 (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
70890 || rc!=SQLITE_OK
70891 );
70892 copyNodeContent(apNew[0], pParent, &rc);
70893 freePage(apNew[0], &rc);
70894 }else if( ISAUTOVACUUM && !leafCorrection ){
70895 /* Fix the pointer map entries associated with the right-child of each
70896 ** sibling page. All other pointer map entries have already been taken
70897 ** care of. */
70898 for(i=0; i<nNew; i++){
70899 u32 key = get4byte(&apNew[i]->aData[8]);
70900 ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
70901 }
70902 }
70903
70904 assert( pParent->isInit );
70905 TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
70906 nOld, nNew, b.nCell));
70907
70908 /* Free any old pages that were not reused as new pages.
70909 */
70910 for(i=nNew; i<nOld; i++){
70911 freePage(apOld[i], &rc);
70912 }
70913
70914#if 0
70915 if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){
70916 /* The ptrmapCheckPages() contains assert() statements that verify that
70917 ** all pointer map pages are set correctly. This is helpful while
70918 ** debugging. This is usually disabled because a corrupt database may
70919 ** cause an assert() statement to fail. */
70920 ptrmapCheckPages(apNew, nNew);
70921 ptrmapCheckPages(&pParent, 1);
70922 }
70923#endif
70924
70925 /*
70926 ** Cleanup before returning.
70927 */
70928balance_cleanup:
70929 sqlite3StackFree(0, b.apCell);
70930 for(i=0; i<nOld; i++){
70931 releasePage(apOld[i]);
70932 }
70933 for(i=0; i<nNew; i++){
70934 releasePage(apNew[i]);
70935 }
70936
70937 return rc;
70938}
70939
70940
70941/*
70942** This function is called when the root page of a b-tree structure is
70943** overfull (has one or more overflow pages).
70944**
70945** A new child page is allocated and the contents of the current root
70946** page, including overflow cells, are copied into the child. The root
70947** page is then overwritten to make it an empty page with the right-child
70948** pointer pointing to the new page.
70949**
70950** Before returning, all pointer-map entries corresponding to pages
70951** that the new child-page now contains pointers to are updated. The
70952** entry corresponding to the new right-child pointer of the root
70953** page is also updated.
70954**
70955** If successful, *ppChild is set to contain a reference to the child
70956** page and SQLITE_OK is returned. In this case the caller is required
70957** to call releasePage() on *ppChild exactly once. If an error occurs,
70958** an error code is returned and *ppChild is set to 0.
70959*/
70960static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
70961 int rc; /* Return value from subprocedures */
70962 MemPage *pChild = 0; /* Pointer to a new child page */
70963 Pgno pgnoChild = 0; /* Page number of the new child page */
70964 BtShared *pBt = pRoot->pBt; /* The BTree */
70965
70966 assert( pRoot->nOverflow>0 );
70967 assert( sqlite3_mutex_held(pBt->mutex) );
70968
70969 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
70970 ** page that will become the new right-child of pPage. Copy the contents
70971 ** of the node stored on pRoot into the new child page.
70972 */
70973 rc = sqlite3PagerWrite(pRoot->pDbPage);
70974 if( rc==SQLITE_OK ){
70975 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
70976 copyNodeContent(pRoot, pChild, &rc);
70977 if( ISAUTOVACUUM ){
70978 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
70979 }
70980 }
70981 if( rc ){
70982 *ppChild = 0;
70983 releasePage(pChild);
70984 return rc;
70985 }
70986 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
70987 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
70988 assert( pChild->nCell==pRoot->nCell );
70989
70990 TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
70991
70992 /* Copy the overflow cells from pRoot to pChild */
70993 memcpy(pChild->aiOvfl, pRoot->aiOvfl,
70994 pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
70995 memcpy(pChild->apOvfl, pRoot->apOvfl,
70996 pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
70997 pChild->nOverflow = pRoot->nOverflow;
70998
70999 /* Zero the contents of pRoot. Then install pChild as the right-child. */
71000 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
71001 put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
71002
71003 *ppChild = pChild;
71004 return SQLITE_OK;
71005}
71006
71007/*
71008** The page that pCur currently points to has just been modified in
71009** some way. This function figures out if this modification means the
71010** tree needs to be balanced, and if so calls the appropriate balancing
71011** routine. Balancing routines are:
71012**
71013** balance_quick()
71014** balance_deeper()
71015** balance_nonroot()
71016*/
71017static int balance(BtCursor *pCur){
71018 int rc = SQLITE_OK;
71019 const int nMin = pCur->pBt->usableSize * 2 / 3;
71020 u8 aBalanceQuickSpace[13];
71021 u8 *pFree = 0;
71022
71023 VVA_ONLY( int balance_quick_called = 0 );
71024 VVA_ONLY( int balance_deeper_called = 0 );
71025
71026 do {
71027 int iPage = pCur->iPage;
71028 MemPage *pPage = pCur->pPage;
71029
71030 if( iPage==0 ){
71031 if( pPage->nOverflow ){
71032 /* The root page of the b-tree is overfull. In this case call the
71033 ** balance_deeper() function to create a new child for the root-page
71034 ** and copy the current contents of the root-page to it. The
71035 ** next iteration of the do-loop will balance the child page.
71036 */
71037 assert( balance_deeper_called==0 );
71038 VVA_ONLY( balance_deeper_called++ );
71039 rc = balance_deeper(pPage, &pCur->apPage[1]);
71040 if( rc==SQLITE_OK ){
71041 pCur->iPage = 1;
71042 pCur->ix = 0;
71043 pCur->aiIdx[0] = 0;
71044 pCur->apPage[0] = pPage;
71045 pCur->pPage = pCur->apPage[1];
71046 assert( pCur->pPage->nOverflow );
71047 }
71048 }else{
71049 break;
71050 }
71051 }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
71052 break;
71053 }else{
71054 MemPage * const pParent = pCur->apPage[iPage-1];
71055 int const iIdx = pCur->aiIdx[iPage-1];
71056
71057 rc = sqlite3PagerWrite(pParent->pDbPage);
71058 if( rc==SQLITE_OK ){
71059#ifndef SQLITE_OMIT_QUICKBALANCE
71060 if( pPage->intKeyLeaf
71061 && pPage->nOverflow==1
71062 && pPage->aiOvfl[0]==pPage->nCell
71063 && pParent->pgno!=1
71064 && pParent->nCell==iIdx
71065 ){
71066 /* Call balance_quick() to create a new sibling of pPage on which
71067 ** to store the overflow cell. balance_quick() inserts a new cell
71068 ** into pParent, which may cause pParent overflow. If this
71069 ** happens, the next iteration of the do-loop will balance pParent
71070 ** use either balance_nonroot() or balance_deeper(). Until this
71071 ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
71072 ** buffer.
71073 **
71074 ** The purpose of the following assert() is to check that only a
71075 ** single call to balance_quick() is made for each call to this
71076 ** function. If this were not verified, a subtle bug involving reuse
71077 ** of the aBalanceQuickSpace[] might sneak in.
71078 */
71079 assert( balance_quick_called==0 );
71080 VVA_ONLY( balance_quick_called++ );
71081 rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
71082 }else
71083#endif
71084 {
71085 /* In this case, call balance_nonroot() to redistribute cells
71086 ** between pPage and up to 2 of its sibling pages. This involves
71087 ** modifying the contents of pParent, which may cause pParent to
71088 ** become overfull or underfull. The next iteration of the do-loop
71089 ** will balance the parent page to correct this.
71090 **
71091 ** If the parent page becomes overfull, the overflow cell or cells
71092 ** are stored in the pSpace buffer allocated immediately below.
71093 ** A subsequent iteration of the do-loop will deal with this by
71094 ** calling balance_nonroot() (balance_deeper() may be called first,
71095 ** but it doesn't deal with overflow cells - just moves them to a
71096 ** different page). Once this subsequent call to balance_nonroot()
71097 ** has completed, it is safe to release the pSpace buffer used by
71098 ** the previous call, as the overflow cell data will have been
71099 ** copied either into the body of a database page or into the new
71100 ** pSpace buffer passed to the latter call to balance_nonroot().
71101 */
71102 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
71103 rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1,
71104 pCur->hints&BTREE_BULKLOAD);
71105 if( pFree ){
71106 /* If pFree is not NULL, it points to the pSpace buffer used
71107 ** by a previous call to balance_nonroot(). Its contents are
71108 ** now stored either on real database pages or within the
71109 ** new pSpace buffer, so it may be safely freed here. */
71110 sqlite3PageFree(pFree);
71111 }
71112
71113 /* The pSpace buffer will be freed after the next call to
71114 ** balance_nonroot(), or just before this function returns, whichever
71115 ** comes first. */
71116 pFree = pSpace;
71117 }
71118 }
71119
71120 pPage->nOverflow = 0;
71121
71122 /* The next iteration of the do-loop balances the parent page. */
71123 releasePage(pPage);
71124 pCur->iPage--;
71125 assert( pCur->iPage>=0 );
71126 pCur->pPage = pCur->apPage[pCur->iPage];
71127 }
71128 }while( rc==SQLITE_OK );
71129
71130 if( pFree ){
71131 sqlite3PageFree(pFree);
71132 }
71133 return rc;
71134}
71135
71136/* Overwrite content from pX into pDest. Only do the write if the
71137** content is different from what is already there.
71138*/
71139static int btreeOverwriteContent(
71140 MemPage *pPage, /* MemPage on which writing will occur */
71141 u8 *pDest, /* Pointer to the place to start writing */
71142 const BtreePayload *pX, /* Source of data to write */
71143 int iOffset, /* Offset of first byte to write */
71144 int iAmt /* Number of bytes to be written */
71145){
71146 int nData = pX->nData - iOffset;
71147 if( nData<=0 ){
71148 /* Overwritting with zeros */
71149 int i;
71150 for(i=0; i<iAmt && pDest[i]==0; i++){}
71151 if( i<iAmt ){
71152 int rc = sqlite3PagerWrite(pPage->pDbPage);
71153 if( rc ) return rc;
71154 memset(pDest + i, 0, iAmt - i);
71155 }
71156 }else{
71157 if( nData<iAmt ){
71158 /* Mixed read data and zeros at the end. Make a recursive call
71159 ** to write the zeros then fall through to write the real data */
71160 int rc = btreeOverwriteContent(pPage, pDest+nData, pX, iOffset+nData,
71161 iAmt-nData);
71162 if( rc ) return rc;
71163 iAmt = nData;
71164 }
71165 if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){
71166 int rc = sqlite3PagerWrite(pPage->pDbPage);
71167 if( rc ) return rc;
71168 memcpy(pDest, ((u8*)pX->pData) + iOffset, iAmt);
71169 }
71170 }
71171 return SQLITE_OK;
71172}
71173
71174/*
71175** Overwrite the cell that cursor pCur is pointing to with fresh content
71176** contained in pX.
71177*/
71178static int btreeOverwriteCell(BtCursor *pCur, const BtreePayload *pX){
71179 int iOffset; /* Next byte of pX->pData to write */
71180 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
71181 int rc; /* Return code */
71182 MemPage *pPage = pCur->pPage; /* Page being written */
71183 BtShared *pBt; /* Btree */
71184 Pgno ovflPgno; /* Next overflow page to write */
71185 u32 ovflPageSize; /* Size to write on overflow page */
71186
71187 if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd ){
71188 return SQLITE_CORRUPT_BKPT;
71189 }
71190 /* Overwrite the local portion first */
71191 rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
71192 0, pCur->info.nLocal);
71193 if( rc ) return rc;
71194 if( pCur->info.nLocal==nTotal ) return SQLITE_OK;
71195
71196 /* Now overwrite the overflow pages */
71197 iOffset = pCur->info.nLocal;
71198 assert( nTotal>=0 );
71199 assert( iOffset>=0 );
71200 ovflPgno = get4byte(pCur->info.pPayload + iOffset);
71201 pBt = pPage->pBt;
71202 ovflPageSize = pBt->usableSize - 4;
71203 do{
71204 rc = btreeGetPage(pBt, ovflPgno, &pPage, 0);
71205 if( rc ) return rc;
71206 if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 ){
71207 rc = SQLITE_CORRUPT_BKPT;
71208 }else{
71209 if( iOffset+ovflPageSize<(u32)nTotal ){
71210 ovflPgno = get4byte(pPage->aData);
71211 }else{
71212 ovflPageSize = nTotal - iOffset;
71213 }
71214 rc = btreeOverwriteContent(pPage, pPage->aData+4, pX,
71215 iOffset, ovflPageSize);
71216 }
71217 sqlite3PagerUnref(pPage->pDbPage);
71218 if( rc ) return rc;
71219 iOffset += ovflPageSize;
71220 }while( iOffset<nTotal );
71221 return SQLITE_OK;
71222}
71223
71224
71225/*
71226** Insert a new record into the BTree. The content of the new record
71227** is described by the pX object. The pCur cursor is used only to
71228** define what table the record should be inserted into, and is left
71229** pointing at a random location.
71230**
71231** For a table btree (used for rowid tables), only the pX.nKey value of
71232** the key is used. The pX.pKey value must be NULL. The pX.nKey is the
71233** rowid or INTEGER PRIMARY KEY of the row. The pX.nData,pData,nZero fields
71234** hold the content of the row.
71235**
71236** For an index btree (used for indexes and WITHOUT ROWID tables), the
71237** key is an arbitrary byte sequence stored in pX.pKey,nKey. The
71238** pX.pData,nData,nZero fields must be zero.
71239**
71240** If the seekResult parameter is non-zero, then a successful call to
71241** MovetoUnpacked() to seek cursor pCur to (pKey,nKey) has already
71242** been performed. In other words, if seekResult!=0 then the cursor
71243** is currently pointing to a cell that will be adjacent to the cell
71244** to be inserted. If seekResult<0 then pCur points to a cell that is
71245** smaller then (pKey,nKey). If seekResult>0 then pCur points to a cell
71246** that is larger than (pKey,nKey).
71247**
71248** If seekResult==0, that means pCur is pointing at some unknown location.
71249** In that case, this routine must seek the cursor to the correct insertion
71250** point for (pKey,nKey) before doing the insertion. For index btrees,
71251** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
71252** key values and pX->aMem can be used instead of pX->pKey to avoid having
71253** to decode the key.
71254*/
71255SQLITE_PRIVATE int sqlite3BtreeInsert(
71256 BtCursor *pCur, /* Insert data into the table of this cursor */
71257 const BtreePayload *pX, /* Content of the row to be inserted */
71258 int flags, /* True if this is likely an append */
71259 int seekResult /* Result of prior MovetoUnpacked() call */
71260){
71261 int rc;
71262 int loc = seekResult; /* -1: before desired location +1: after */
71263 int szNew = 0;
71264 int idx;
71265 MemPage *pPage;
71266 Btree *p = pCur->pBtree;
71267 BtShared *pBt = p->pBt;
71268 unsigned char *oldCell;
71269 unsigned char *newCell = 0;
71270
71271 assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND))==flags );
71272
71273 if( pCur->eState==CURSOR_FAULT ){
71274 assert( pCur->skipNext!=SQLITE_OK );
71275 return pCur->skipNext;
71276 }
71277
71278 assert( cursorOwnsBtShared(pCur) );
71279 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
71280 && pBt->inTransaction==TRANS_WRITE
71281 && (pBt->btsFlags & BTS_READ_ONLY)==0 );
71282 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
71283
71284 /* Assert that the caller has been consistent. If this cursor was opened
71285 ** expecting an index b-tree, then the caller should be inserting blob
71286 ** keys with no associated data. If the cursor was opened expecting an
71287 ** intkey table, the caller should be inserting integer keys with a
71288 ** blob of associated data. */
71289 assert( (pX->pKey==0)==(pCur->pKeyInfo==0) );
71290
71291 /* Save the positions of any other cursors open on this table.
71292 **
71293 ** In some cases, the call to btreeMoveto() below is a no-op. For
71294 ** example, when inserting data into a table with auto-generated integer
71295 ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the
71296 ** integer key to use. It then calls this function to actually insert the
71297 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
71298 ** that the cursor is already where it needs to be and returns without
71299 ** doing any work. To avoid thwarting these optimizations, it is important
71300 ** not to clear the cursor here.
71301 */
71302 if( pCur->curFlags & BTCF_Multiple ){
71303 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
71304 if( rc ) return rc;
71305 }
71306
71307 if( pCur->pKeyInfo==0 ){
71308 assert( pX->pKey==0 );
71309 /* If this is an insert into a table b-tree, invalidate any incrblob
71310 ** cursors open on the row being replaced */
71311 invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
71312
71313 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
71314 ** to a row with the same key as the new entry being inserted.
71315 */
71316#ifdef SQLITE_DEBUG
71317 if( flags & BTREE_SAVEPOSITION ){
71318 assert( pCur->curFlags & BTCF_ValidNKey );
71319 assert( pX->nKey==pCur->info.nKey );
71320 assert( pCur->info.nSize!=0 );
71321 assert( loc==0 );
71322 }
71323#endif
71324
71325 /* On the other hand, BTREE_SAVEPOSITION==0 does not imply
71326 ** that the cursor is not pointing to a row to be overwritten.
71327 ** So do a complete check.
71328 */
71329 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
71330 /* The cursor is pointing to the entry that is to be
71331 ** overwritten */
71332 assert( pX->nData>=0 && pX->nZero>=0 );
71333 if( pCur->info.nSize!=0
71334 && pCur->info.nPayload==(u32)pX->nData+pX->nZero
71335 ){
71336 /* New entry is the same size as the old. Do an overwrite */
71337 return btreeOverwriteCell(pCur, pX);
71338 }
71339 assert( loc==0 );
71340 }else if( loc==0 ){
71341 /* The cursor is *not* pointing to the cell to be overwritten, nor
71342 ** to an adjacent cell. Move the cursor so that it is pointing either
71343 ** to the cell to be overwritten or an adjacent cell.
71344 */
71345 rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, flags!=0, &loc);
71346 if( rc ) return rc;
71347 }
71348 }else{
71349 /* This is an index or a WITHOUT ROWID table */
71350
71351 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
71352 ** to a row with the same key as the new entry being inserted.
71353 */
71354 assert( (flags & BTREE_SAVEPOSITION)==0 || loc==0 );
71355
71356 /* If the cursor is not already pointing either to the cell to be
71357 ** overwritten, or if a new cell is being inserted, if the cursor is
71358 ** not pointing to an immediately adjacent cell, then move the cursor
71359 ** so that it does.
71360 */
71361 if( loc==0 && (flags & BTREE_SAVEPOSITION)==0 ){
71362 if( pX->nMem ){
71363 UnpackedRecord r;
71364 r.pKeyInfo = pCur->pKeyInfo;
71365 r.aMem = pX->aMem;
71366 r.nField = pX->nMem;
71367 r.default_rc = 0;
71368 r.errCode = 0;
71369 r.r1 = 0;
71370 r.r2 = 0;
71371 r.eqSeen = 0;
71372 rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, flags!=0, &loc);
71373 }else{
71374 rc = btreeMoveto(pCur, pX->pKey, pX->nKey, flags!=0, &loc);
71375 }
71376 if( rc ) return rc;
71377 }
71378
71379 /* If the cursor is currently pointing to an entry to be overwritten
71380 ** and the new content is the same as as the old, then use the
71381 ** overwrite optimization.
71382 */
71383 if( loc==0 ){
71384 getCellInfo(pCur);
71385 if( pCur->info.nKey==pX->nKey ){
71386 BtreePayload x2;
71387 x2.pData = pX->pKey;
71388 x2.nData = pX->nKey;
71389 x2.nZero = 0;
71390 return btreeOverwriteCell(pCur, &x2);
71391 }
71392 }
71393
71394 }
71395 assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
71396
71397 pPage = pCur->pPage;
71398 assert( pPage->intKey || pX->nKey>=0 );
71399 assert( pPage->leaf || !pPage->intKey );
71400
71401 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
71402 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
71403 loc==0 ? "overwrite" : "new entry"));
71404 assert( pPage->isInit );
71405 newCell = pBt->pTmpSpace;
71406 assert( newCell!=0 );
71407 rc = fillInCell(pPage, newCell, pX, &szNew);
71408 if( rc ) goto end_insert;
71409 assert( szNew==pPage->xCellSize(pPage, newCell) );
71410 assert( szNew <= MX_CELL_SIZE(pBt) );
71411 idx = pCur->ix;
71412 if( loc==0 ){
71413 CellInfo info;
71414 assert( idx<pPage->nCell );
71415 rc = sqlite3PagerWrite(pPage->pDbPage);
71416 if( rc ){
71417 goto end_insert;
71418 }
71419 oldCell = findCell(pPage, idx);
71420 if( !pPage->leaf ){
71421 memcpy(newCell, oldCell, 4);
71422 }
71423 rc = clearCell(pPage, oldCell, &info);
71424 if( info.nSize==szNew && info.nLocal==info.nPayload
71425 && (!ISAUTOVACUUM || szNew<pPage->minLocal)
71426 ){
71427 /* Overwrite the old cell with the new if they are the same size.
71428 ** We could also try to do this if the old cell is smaller, then add
71429 ** the leftover space to the free list. But experiments show that
71430 ** doing that is no faster then skipping this optimization and just
71431 ** calling dropCell() and insertCell().
71432 **
71433 ** This optimization cannot be used on an autovacuum database if the
71434 ** new entry uses overflow pages, as the insertCell() call below is
71435 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */
71436 assert( rc==SQLITE_OK ); /* clearCell never fails when nLocal==nPayload */
71437 if( oldCell+szNew > pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
71438 memcpy(oldCell, newCell, szNew);
71439 return SQLITE_OK;
71440 }
71441 dropCell(pPage, idx, info.nSize, &rc);
71442 if( rc ) goto end_insert;
71443 }else if( loc<0 && pPage->nCell>0 ){
71444 assert( pPage->leaf );
71445 idx = ++pCur->ix;
71446 pCur->curFlags &= ~BTCF_ValidNKey;
71447 }else{
71448 assert( pPage->leaf );
71449 }
71450 insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
71451 assert( pPage->nOverflow==0 || rc==SQLITE_OK );
71452 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
71453
71454 /* If no error has occurred and pPage has an overflow cell, call balance()
71455 ** to redistribute the cells within the tree. Since balance() may move
71456 ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey
71457 ** variables.
71458 **
71459 ** Previous versions of SQLite called moveToRoot() to move the cursor
71460 ** back to the root page as balance() used to invalidate the contents
71461 ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
71462 ** set the cursor state to "invalid". This makes common insert operations
71463 ** slightly faster.
71464 **
71465 ** There is a subtle but important optimization here too. When inserting
71466 ** multiple records into an intkey b-tree using a single cursor (as can
71467 ** happen while processing an "INSERT INTO ... SELECT" statement), it
71468 ** is advantageous to leave the cursor pointing to the last entry in
71469 ** the b-tree if possible. If the cursor is left pointing to the last
71470 ** entry in the table, and the next row inserted has an integer key
71471 ** larger than the largest existing key, it is possible to insert the
71472 ** row without seeking the cursor. This can be a big performance boost.
71473 */
71474 pCur->info.nSize = 0;
71475 if( pPage->nOverflow ){
71476 assert( rc==SQLITE_OK );
71477 pCur->curFlags &= ~(BTCF_ValidNKey);
71478 rc = balance(pCur);
71479
71480 /* Must make sure nOverflow is reset to zero even if the balance()
71481 ** fails. Internal data structure corruption will result otherwise.
71482 ** Also, set the cursor state to invalid. This stops saveCursorPosition()
71483 ** from trying to save the current position of the cursor. */
71484 pCur->pPage->nOverflow = 0;
71485 pCur->eState = CURSOR_INVALID;
71486 if( (flags & BTREE_SAVEPOSITION) && rc==SQLITE_OK ){
71487 btreeReleaseAllCursorPages(pCur);
71488 if( pCur->pKeyInfo ){
71489 assert( pCur->pKey==0 );
71490 pCur->pKey = sqlite3Malloc( pX->nKey );
71491 if( pCur->pKey==0 ){
71492 rc = SQLITE_NOMEM;
71493 }else{
71494 memcpy(pCur->pKey, pX->pKey, pX->nKey);
71495 }
71496 }
71497 pCur->eState = CURSOR_REQUIRESEEK;
71498 pCur->nKey = pX->nKey;
71499 }
71500 }
71501 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 );
71502
71503end_insert:
71504 return rc;
71505}
71506
71507/*
71508** Delete the entry that the cursor is pointing to.
71509**
71510** If the BTREE_SAVEPOSITION bit of the flags parameter is zero, then
71511** the cursor is left pointing at an arbitrary location after the delete.
71512** But if that bit is set, then the cursor is left in a state such that
71513** the next call to BtreeNext() or BtreePrev() moves it to the same row
71514** as it would have been on if the call to BtreeDelete() had been omitted.
71515**
71516** The BTREE_AUXDELETE bit of flags indicates that is one of several deletes
71517** associated with a single table entry and its indexes. Only one of those
71518** deletes is considered the "primary" delete. The primary delete occurs
71519** on a cursor that is not a BTREE_FORDELETE cursor. All but one delete
71520** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
71521** The BTREE_AUXDELETE bit is a hint that is not used by this implementation,
71522** but which might be used by alternative storage engines.
71523*/
71524SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
71525 Btree *p = pCur->pBtree;
71526 BtShared *pBt = p->pBt;
71527 int rc; /* Return code */
71528 MemPage *pPage; /* Page to delete cell from */
71529 unsigned char *pCell; /* Pointer to cell to delete */
71530 int iCellIdx; /* Index of cell to delete */
71531 int iCellDepth; /* Depth of node containing pCell */
71532 CellInfo info; /* Size of the cell being deleted */
71533 int bSkipnext = 0; /* Leaf cursor in SKIPNEXT state */
71534 u8 bPreserve = flags & BTREE_SAVEPOSITION; /* Keep cursor valid */
71535
71536 assert( cursorOwnsBtShared(pCur) );
71537 assert( pBt->inTransaction==TRANS_WRITE );
71538 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
71539 assert( pCur->curFlags & BTCF_WriteFlag );
71540 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
71541 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
71542 assert( pCur->ix<pCur->pPage->nCell );
71543 assert( pCur->eState==CURSOR_VALID );
71544 assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 );
71545
71546 iCellDepth = pCur->iPage;
71547 iCellIdx = pCur->ix;
71548 pPage = pCur->pPage;
71549 pCell = findCell(pPage, iCellIdx);
71550
71551 /* If the bPreserve flag is set to true, then the cursor position must
71552 ** be preserved following this delete operation. If the current delete
71553 ** will cause a b-tree rebalance, then this is done by saving the cursor
71554 ** key and leaving the cursor in CURSOR_REQUIRESEEK state before
71555 ** returning.
71556 **
71557 ** Or, if the current delete will not cause a rebalance, then the cursor
71558 ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately
71559 ** before or after the deleted entry. In this case set bSkipnext to true. */
71560 if( bPreserve ){
71561 if( !pPage->leaf
71562 || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3)
71563 ){
71564 /* A b-tree rebalance will be required after deleting this entry.
71565 ** Save the cursor key. */
71566 rc = saveCursorKey(pCur);
71567 if( rc ) return rc;
71568 }else{
71569 bSkipnext = 1;
71570 }
71571 }
71572
71573 /* If the page containing the entry to delete is not a leaf page, move
71574 ** the cursor to the largest entry in the tree that is smaller than
71575 ** the entry being deleted. This cell will replace the cell being deleted
71576 ** from the internal node. The 'previous' entry is used for this instead
71577 ** of the 'next' entry, as the previous entry is always a part of the
71578 ** sub-tree headed by the child page of the cell being deleted. This makes
71579 ** balancing the tree following the delete operation easier. */
71580 if( !pPage->leaf ){
71581 rc = sqlite3BtreePrevious(pCur, 0);
71582 assert( rc!=SQLITE_DONE );
71583 if( rc ) return rc;
71584 }
71585
71586 /* Save the positions of any other cursors open on this table before
71587 ** making any modifications. */
71588 if( pCur->curFlags & BTCF_Multiple ){
71589 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
71590 if( rc ) return rc;
71591 }
71592
71593 /* If this is a delete operation to remove a row from a table b-tree,
71594 ** invalidate any incrblob cursors open on the row being deleted. */
71595 if( pCur->pKeyInfo==0 ){
71596 invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
71597 }
71598
71599 /* Make the page containing the entry to be deleted writable. Then free any
71600 ** overflow pages associated with the entry and finally remove the cell
71601 ** itself from within the page. */
71602 rc = sqlite3PagerWrite(pPage->pDbPage);
71603 if( rc ) return rc;
71604 rc = clearCell(pPage, pCell, &info);
71605 dropCell(pPage, iCellIdx, info.nSize, &rc);
71606 if( rc ) return rc;
71607
71608 /* If the cell deleted was not located on a leaf page, then the cursor
71609 ** is currently pointing to the largest entry in the sub-tree headed
71610 ** by the child-page of the cell that was just deleted from an internal
71611 ** node. The cell from the leaf node needs to be moved to the internal
71612 ** node to replace the deleted cell. */
71613 if( !pPage->leaf ){
71614 MemPage *pLeaf = pCur->pPage;
71615 int nCell;
71616 Pgno n;
71617 unsigned char *pTmp;
71618
71619 if( iCellDepth<pCur->iPage-1 ){
71620 n = pCur->apPage[iCellDepth+1]->pgno;
71621 }else{
71622 n = pCur->pPage->pgno;
71623 }
71624 pCell = findCell(pLeaf, pLeaf->nCell-1);
71625 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
71626 nCell = pLeaf->xCellSize(pLeaf, pCell);
71627 assert( MX_CELL_SIZE(pBt) >= nCell );
71628 pTmp = pBt->pTmpSpace;
71629 assert( pTmp!=0 );
71630 rc = sqlite3PagerWrite(pLeaf->pDbPage);
71631 if( rc==SQLITE_OK ){
71632 insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
71633 }
71634 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
71635 if( rc ) return rc;
71636 }
71637
71638 /* Balance the tree. If the entry deleted was located on a leaf page,
71639 ** then the cursor still points to that page. In this case the first
71640 ** call to balance() repairs the tree, and the if(...) condition is
71641 ** never true.
71642 **
71643 ** Otherwise, if the entry deleted was on an internal node page, then
71644 ** pCur is pointing to the leaf page from which a cell was removed to
71645 ** replace the cell deleted from the internal node. This is slightly
71646 ** tricky as the leaf node may be underfull, and the internal node may
71647 ** be either under or overfull. In this case run the balancing algorithm
71648 ** on the leaf node first. If the balance proceeds far enough up the
71649 ** tree that we can be sure that any problem in the internal node has
71650 ** been corrected, so be it. Otherwise, after balancing the leaf node,
71651 ** walk the cursor up the tree to the internal node and balance it as
71652 ** well. */
71653 rc = balance(pCur);
71654 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
71655 releasePageNotNull(pCur->pPage);
71656 pCur->iPage--;
71657 while( pCur->iPage>iCellDepth ){
71658 releasePage(pCur->apPage[pCur->iPage--]);
71659 }
71660 pCur->pPage = pCur->apPage[pCur->iPage];
71661 rc = balance(pCur);
71662 }
71663
71664 if( rc==SQLITE_OK ){
71665 if( bSkipnext ){
71666 assert( bPreserve && (pCur->iPage==iCellDepth || CORRUPT_DB) );
71667 assert( pPage==pCur->pPage || CORRUPT_DB );
71668 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
71669 pCur->eState = CURSOR_SKIPNEXT;
71670 if( iCellIdx>=pPage->nCell ){
71671 pCur->skipNext = -1;
71672 pCur->ix = pPage->nCell-1;
71673 }else{
71674 pCur->skipNext = 1;
71675 }
71676 }else{
71677 rc = moveToRoot(pCur);
71678 if( bPreserve ){
71679 btreeReleaseAllCursorPages(pCur);
71680 pCur->eState = CURSOR_REQUIRESEEK;
71681 }
71682 if( rc==SQLITE_EMPTY ) rc = SQLITE_OK;
71683 }
71684 }
71685 return rc;
71686}
71687
71688/*
71689** Create a new BTree table. Write into *piTable the page
71690** number for the root page of the new table.
71691**
71692** The type of type is determined by the flags parameter. Only the
71693** following values of flags are currently in use. Other values for
71694** flags might not work:
71695**
71696** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys
71697** BTREE_ZERODATA Used for SQL indices
71698*/
71699static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){
71700 BtShared *pBt = p->pBt;
71701 MemPage *pRoot;
71702 Pgno pgnoRoot;
71703 int rc;
71704 int ptfFlags; /* Page-type flage for the root page of new table */
71705
71706 assert( sqlite3BtreeHoldsMutex(p) );
71707 assert( pBt->inTransaction==TRANS_WRITE );
71708 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
71709
71710#ifdef SQLITE_OMIT_AUTOVACUUM
71711 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
71712 if( rc ){
71713 return rc;
71714 }
71715#else
71716 if( pBt->autoVacuum ){
71717 Pgno pgnoMove; /* Move a page here to make room for the root-page */
71718 MemPage *pPageMove; /* The page to move to. */
71719
71720 /* Creating a new table may probably require moving an existing database
71721 ** to make room for the new tables root page. In case this page turns
71722 ** out to be an overflow page, delete all overflow page-map caches
71723 ** held by open cursors.
71724 */
71725 invalidateAllOverflowCache(pBt);
71726
71727 /* Read the value of meta[3] from the database to determine where the
71728 ** root page of the new table should go. meta[3] is the largest root-page
71729 ** created so far, so the new root-page is (meta[3]+1).
71730 */
71731 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot);
71732 pgnoRoot++;
71733
71734 /* The new root-page may not be allocated on a pointer-map page, or the
71735 ** PENDING_BYTE page.
71736 */
71737 while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
71738 pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
71739 pgnoRoot++;
71740 }
71741 assert( pgnoRoot>=3 || CORRUPT_DB );
71742 testcase( pgnoRoot<3 );
71743
71744 /* Allocate a page. The page that currently resides at pgnoRoot will
71745 ** be moved to the allocated page (unless the allocated page happens
71746 ** to reside at pgnoRoot).
71747 */
71748 rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, BTALLOC_EXACT);
71749 if( rc!=SQLITE_OK ){
71750 return rc;
71751 }
71752
71753 if( pgnoMove!=pgnoRoot ){
71754 /* pgnoRoot is the page that will be used for the root-page of
71755 ** the new table (assuming an error did not occur). But we were
71756 ** allocated pgnoMove. If required (i.e. if it was not allocated
71757 ** by extending the file), the current page at position pgnoMove
71758 ** is already journaled.
71759 */
71760 u8 eType = 0;
71761 Pgno iPtrPage = 0;
71762
71763 /* Save the positions of any open cursors. This is required in
71764 ** case they are holding a reference to an xFetch reference
71765 ** corresponding to page pgnoRoot. */
71766 rc = saveAllCursors(pBt, 0, 0);
71767 releasePage(pPageMove);
71768 if( rc!=SQLITE_OK ){
71769 return rc;
71770 }
71771
71772 /* Move the page currently at pgnoRoot to pgnoMove. */
71773 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
71774 if( rc!=SQLITE_OK ){
71775 return rc;
71776 }
71777 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
71778 if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
71779 rc = SQLITE_CORRUPT_BKPT;
71780 }
71781 if( rc!=SQLITE_OK ){
71782 releasePage(pRoot);
71783 return rc;
71784 }
71785 assert( eType!=PTRMAP_ROOTPAGE );
71786 assert( eType!=PTRMAP_FREEPAGE );
71787 rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
71788 releasePage(pRoot);
71789
71790 /* Obtain the page at pgnoRoot */
71791 if( rc!=SQLITE_OK ){
71792 return rc;
71793 }
71794 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
71795 if( rc!=SQLITE_OK ){
71796 return rc;
71797 }
71798 rc = sqlite3PagerWrite(pRoot->pDbPage);
71799 if( rc!=SQLITE_OK ){
71800 releasePage(pRoot);
71801 return rc;
71802 }
71803 }else{
71804 pRoot = pPageMove;
71805 }
71806
71807 /* Update the pointer-map and meta-data with the new root-page number. */
71808 ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc);
71809 if( rc ){
71810 releasePage(pRoot);
71811 return rc;
71812 }
71813
71814 /* When the new root page was allocated, page 1 was made writable in
71815 ** order either to increase the database filesize, or to decrement the
71816 ** freelist count. Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
71817 */
71818 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
71819 rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
71820 if( NEVER(rc) ){
71821 releasePage(pRoot);
71822 return rc;
71823 }
71824
71825 }else{
71826 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
71827 if( rc ) return rc;
71828 }
71829#endif
71830 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
71831 if( createTabFlags & BTREE_INTKEY ){
71832 ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF;
71833 }else{
71834 ptfFlags = PTF_ZERODATA | PTF_LEAF;
71835 }
71836 zeroPage(pRoot, ptfFlags);
71837 sqlite3PagerUnref(pRoot->pDbPage);
71838 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
71839 *piTable = (int)pgnoRoot;
71840 return SQLITE_OK;
71841}
71842SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
71843 int rc;
71844 sqlite3BtreeEnter(p);
71845 rc = btreeCreateTable(p, piTable, flags);
71846 sqlite3BtreeLeave(p);
71847 return rc;
71848}
71849
71850/*
71851** Erase the given database page and all its children. Return
71852** the page to the freelist.
71853*/
71854static int clearDatabasePage(
71855 BtShared *pBt, /* The BTree that contains the table */
71856 Pgno pgno, /* Page number to clear */
71857 int freePageFlag, /* Deallocate page if true */
71858 int *pnChange /* Add number of Cells freed to this counter */
71859){
71860 MemPage *pPage;
71861 int rc;
71862 unsigned char *pCell;
71863 int i;
71864 int hdr;
71865 CellInfo info;
71866
71867 assert( sqlite3_mutex_held(pBt->mutex) );
71868 if( pgno>btreePagecount(pBt) ){
71869 return SQLITE_CORRUPT_BKPT;
71870 }
71871 rc = getAndInitPage(pBt, pgno, &pPage, 0, 0);
71872 if( rc ) return rc;
71873 if( pPage->bBusy ){
71874 rc = SQLITE_CORRUPT_BKPT;
71875 goto cleardatabasepage_out;
71876 }
71877 pPage->bBusy = 1;
71878 hdr = pPage->hdrOffset;
71879 for(i=0; i<pPage->nCell; i++){
71880 pCell = findCell(pPage, i);
71881 if( !pPage->leaf ){
71882 rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
71883 if( rc ) goto cleardatabasepage_out;
71884 }
71885 rc = clearCell(pPage, pCell, &info);
71886 if( rc ) goto cleardatabasepage_out;
71887 }
71888 if( !pPage->leaf ){
71889 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
71890 if( rc ) goto cleardatabasepage_out;
71891 }else if( pnChange ){
71892 assert( pPage->intKey || CORRUPT_DB );
71893 testcase( !pPage->intKey );
71894 *pnChange += pPage->nCell;
71895 }
71896 if( freePageFlag ){
71897 freePage(pPage, &rc);
71898 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
71899 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
71900 }
71901
71902cleardatabasepage_out:
71903 pPage->bBusy = 0;
71904 releasePage(pPage);
71905 return rc;
71906}
71907
71908/*
71909** Delete all information from a single table in the database. iTable is
71910** the page number of the root of the table. After this routine returns,
71911** the root page is empty, but still exists.
71912**
71913** This routine will fail with SQLITE_LOCKED if there are any open
71914** read cursors on the table. Open write cursors are moved to the
71915** root of the table.
71916**
71917** If pnChange is not NULL, then table iTable must be an intkey table. The
71918** integer value pointed to by pnChange is incremented by the number of
71919** entries in the table.
71920*/
71921SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
71922 int rc;
71923 BtShared *pBt = p->pBt;
71924 sqlite3BtreeEnter(p);
71925 assert( p->inTrans==TRANS_WRITE );
71926
71927 rc = saveAllCursors(pBt, (Pgno)iTable, 0);
71928
71929 if( SQLITE_OK==rc ){
71930 /* Invalidate all incrblob cursors open on table iTable (assuming iTable
71931 ** is the root of a table b-tree - if it is not, the following call is
71932 ** a no-op). */
71933 invalidateIncrblobCursors(p, (Pgno)iTable, 0, 1);
71934 rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
71935 }
71936 sqlite3BtreeLeave(p);
71937 return rc;
71938}
71939
71940/*
71941** Delete all information from the single table that pCur is open on.
71942**
71943** This routine only work for pCur on an ephemeral table.
71944*/
71945SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){
71946 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
71947}
71948
71949/*
71950** Erase all information in a table and add the root of the table to
71951** the freelist. Except, the root of the principle table (the one on
71952** page 1) is never added to the freelist.
71953**
71954** This routine will fail with SQLITE_LOCKED if there are any open
71955** cursors on the table.
71956**
71957** If AUTOVACUUM is enabled and the page at iTable is not the last
71958** root page in the database file, then the last root page
71959** in the database file is moved into the slot formerly occupied by
71960** iTable and that last slot formerly occupied by the last root page
71961** is added to the freelist instead of iTable. In this say, all
71962** root pages are kept at the beginning of the database file, which
71963** is necessary for AUTOVACUUM to work right. *piMoved is set to the
71964** page number that used to be the last root page in the file before
71965** the move. If no page gets moved, *piMoved is set to 0.
71966** The last root page is recorded in meta[3] and the value of
71967** meta[3] is updated by this procedure.
71968*/
71969static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
71970 int rc;
71971 MemPage *pPage = 0;
71972 BtShared *pBt = p->pBt;
71973
71974 assert( sqlite3BtreeHoldsMutex(p) );
71975 assert( p->inTrans==TRANS_WRITE );
71976 assert( iTable>=2 );
71977
71978 rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
71979 if( rc ) return rc;
71980 rc = sqlite3BtreeClearTable(p, iTable, 0);
71981 if( rc ){
71982 releasePage(pPage);
71983 return rc;
71984 }
71985
71986 *piMoved = 0;
71987
71988#ifdef SQLITE_OMIT_AUTOVACUUM
71989 freePage(pPage, &rc);
71990 releasePage(pPage);
71991#else
71992 if( pBt->autoVacuum ){
71993 Pgno maxRootPgno;
71994 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno);
71995
71996 if( iTable==maxRootPgno ){
71997 /* If the table being dropped is the table with the largest root-page
71998 ** number in the database, put the root page on the free list.
71999 */
72000 freePage(pPage, &rc);
72001 releasePage(pPage);
72002 if( rc!=SQLITE_OK ){
72003 return rc;
72004 }
72005 }else{
72006 /* The table being dropped does not have the largest root-page
72007 ** number in the database. So move the page that does into the
72008 ** gap left by the deleted root-page.
72009 */
72010 MemPage *pMove;
72011 releasePage(pPage);
72012 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
72013 if( rc!=SQLITE_OK ){
72014 return rc;
72015 }
72016 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
72017 releasePage(pMove);
72018 if( rc!=SQLITE_OK ){
72019 return rc;
72020 }
72021 pMove = 0;
72022 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
72023 freePage(pMove, &rc);
72024 releasePage(pMove);
72025 if( rc!=SQLITE_OK ){
72026 return rc;
72027 }
72028 *piMoved = maxRootPgno;
72029 }
72030
72031 /* Set the new 'max-root-page' value in the database header. This
72032 ** is the old value less one, less one more if that happens to
72033 ** be a root-page number, less one again if that is the
72034 ** PENDING_BYTE_PAGE.
72035 */
72036 maxRootPgno--;
72037 while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
72038 || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
72039 maxRootPgno--;
72040 }
72041 assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
72042
72043 rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
72044 }else{
72045 freePage(pPage, &rc);
72046 releasePage(pPage);
72047 }
72048#endif
72049 return rc;
72050}
72051SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
72052 int rc;
72053 sqlite3BtreeEnter(p);
72054 rc = btreeDropTable(p, iTable, piMoved);
72055 sqlite3BtreeLeave(p);
72056 return rc;
72057}
72058
72059
72060/*
72061** This function may only be called if the b-tree connection already
72062** has a read or write transaction open on the database.
72063**
72064** Read the meta-information out of a database file. Meta[0]
72065** is the number of free pages currently in the database. Meta[1]
72066** through meta[15] are available for use by higher layers. Meta[0]
72067** is read-only, the others are read/write.
72068**
72069** The schema layer numbers meta values differently. At the schema
72070** layer (and the SetCookie and ReadCookie opcodes) the number of
72071** free pages is not visible. So Cookie[0] is the same as Meta[1].
72072**
72073** This routine treats Meta[BTREE_DATA_VERSION] as a special case. Instead
72074** of reading the value out of the header, it instead loads the "DataVersion"
72075** from the pager. The BTREE_DATA_VERSION value is not actually stored in the
72076** database file. It is a number computed by the pager. But its access
72077** pattern is the same as header meta values, and so it is convenient to
72078** read it from this routine.
72079*/
72080SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
72081 BtShared *pBt = p->pBt;
72082
72083 sqlite3BtreeEnter(p);
72084 assert( p->inTrans>TRANS_NONE );
72085 assert( SQLITE_OK==querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK) );
72086 assert( pBt->pPage1 );
72087 assert( idx>=0 && idx<=15 );
72088
72089 if( idx==BTREE_DATA_VERSION ){
72090 *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iDataVersion;
72091 }else{
72092 *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
72093 }
72094
72095 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
72096 ** database, mark the database as read-only. */
72097#ifdef SQLITE_OMIT_AUTOVACUUM
72098 if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){
72099 pBt->btsFlags |= BTS_READ_ONLY;
72100 }
72101#endif
72102
72103 sqlite3BtreeLeave(p);
72104}
72105
72106/*
72107** Write meta-information back into the database. Meta[0] is
72108** read-only and may not be written.
72109*/
72110SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
72111 BtShared *pBt = p->pBt;
72112 unsigned char *pP1;
72113 int rc;
72114 assert( idx>=1 && idx<=15 );
72115 sqlite3BtreeEnter(p);
72116 assert( p->inTrans==TRANS_WRITE );
72117 assert( pBt->pPage1!=0 );
72118 pP1 = pBt->pPage1->aData;
72119 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
72120 if( rc==SQLITE_OK ){
72121 put4byte(&pP1[36 + idx*4], iMeta);
72122#ifndef SQLITE_OMIT_AUTOVACUUM
72123 if( idx==BTREE_INCR_VACUUM ){
72124 assert( pBt->autoVacuum || iMeta==0 );
72125 assert( iMeta==0 || iMeta==1 );
72126 pBt->incrVacuum = (u8)iMeta;
72127 }
72128#endif
72129 }
72130 sqlite3BtreeLeave(p);
72131 return rc;
72132}
72133
72134#ifndef SQLITE_OMIT_BTREECOUNT
72135/*
72136** The first argument, pCur, is a cursor opened on some b-tree. Count the
72137** number of entries in the b-tree and write the result to *pnEntry.
72138**
72139** SQLITE_OK is returned if the operation is successfully executed.
72140** Otherwise, if an error is encountered (i.e. an IO error or database
72141** corruption) an SQLite error code is returned.
72142*/
72143SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
72144 i64 nEntry = 0; /* Value to return in *pnEntry */
72145 int rc; /* Return code */
72146
72147 rc = moveToRoot(pCur);
72148 if( rc==SQLITE_EMPTY ){
72149 *pnEntry = 0;
72150 return SQLITE_OK;
72151 }
72152
72153 /* Unless an error occurs, the following loop runs one iteration for each
72154 ** page in the B-Tree structure (not including overflow pages).
72155 */
72156 while( rc==SQLITE_OK ){
72157 int iIdx; /* Index of child node in parent */
72158 MemPage *pPage; /* Current page of the b-tree */
72159
72160 /* If this is a leaf page or the tree is not an int-key tree, then
72161 ** this page contains countable entries. Increment the entry counter
72162 ** accordingly.
72163 */
72164 pPage = pCur->pPage;
72165 if( pPage->leaf || !pPage->intKey ){
72166 nEntry += pPage->nCell;
72167 }
72168
72169 /* pPage is a leaf node. This loop navigates the cursor so that it
72170 ** points to the first interior cell that it points to the parent of
72171 ** the next page in the tree that has not yet been visited. The
72172 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
72173 ** of the page, or to the number of cells in the page if the next page
72174 ** to visit is the right-child of its parent.
72175 **
72176 ** If all pages in the tree have been visited, return SQLITE_OK to the
72177 ** caller.
72178 */
72179 if( pPage->leaf ){
72180 do {
72181 if( pCur->iPage==0 ){
72182 /* All pages of the b-tree have been visited. Return successfully. */
72183 *pnEntry = nEntry;
72184 return moveToRoot(pCur);
72185 }
72186 moveToParent(pCur);
72187 }while ( pCur->ix>=pCur->pPage->nCell );
72188
72189 pCur->ix++;
72190 pPage = pCur->pPage;
72191 }
72192
72193 /* Descend to the child node of the cell that the cursor currently
72194 ** points at. This is the right-child if (iIdx==pPage->nCell).
72195 */
72196 iIdx = pCur->ix;
72197 if( iIdx==pPage->nCell ){
72198 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
72199 }else{
72200 rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
72201 }
72202 }
72203
72204 /* An error has occurred. Return an error code. */
72205 return rc;
72206}
72207#endif
72208
72209/*
72210** Return the pager associated with a BTree. This routine is used for
72211** testing and debugging only.
72212*/
72213SQLITE_PRIVATE Pager *sqlite3BtreePager(Btree *p){
72214 return p->pBt->pPager;
72215}
72216
72217#ifndef SQLITE_OMIT_INTEGRITY_CHECK
72218/*
72219** Append a message to the error message string.
72220*/
72221static void checkAppendMsg(
72222 IntegrityCk *pCheck,
72223 const char *zFormat,
72224 ...
72225){
72226 va_list ap;
72227 if( !pCheck->mxErr ) return;
72228 pCheck->mxErr--;
72229 pCheck->nErr++;
72230 va_start(ap, zFormat);
72231 if( pCheck->errMsg.nChar ){
72232 sqlite3_str_append(&pCheck->errMsg, "\n", 1);
72233 }
72234 if( pCheck->zPfx ){
72235 sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2);
72236 }
72237 sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap);
72238 va_end(ap);
72239 if( pCheck->errMsg.accError==SQLITE_NOMEM ){
72240 pCheck->mallocFailed = 1;
72241 }
72242}
72243#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
72244
72245#ifndef SQLITE_OMIT_INTEGRITY_CHECK
72246
72247/*
72248** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
72249** corresponds to page iPg is already set.
72250*/
72251static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){
72252 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
72253 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
72254}
72255
72256/*
72257** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg.
72258*/
72259static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){
72260 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
72261 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
72262}
72263
72264
72265/*
72266** Add 1 to the reference count for page iPage. If this is the second
72267** reference to the page, add an error message to pCheck->zErrMsg.
72268** Return 1 if there are 2 or more references to the page and 0 if
72269** if this is the first reference to the page.
72270**
72271** Also check that the page number is in bounds.
72272*/
72273static int checkRef(IntegrityCk *pCheck, Pgno iPage){
72274 if( iPage>pCheck->nPage || iPage==0 ){
72275 checkAppendMsg(pCheck, "invalid page number %d", iPage);
72276 return 1;
72277 }
72278 if( getPageReferenced(pCheck, iPage) ){
72279 checkAppendMsg(pCheck, "2nd reference to page %d", iPage);
72280 return 1;
72281 }
72282 setPageReferenced(pCheck, iPage);
72283 return 0;
72284}
72285
72286#ifndef SQLITE_OMIT_AUTOVACUUM
72287/*
72288** Check that the entry in the pointer-map for page iChild maps to
72289** page iParent, pointer type ptrType. If not, append an error message
72290** to pCheck.
72291*/
72292static void checkPtrmap(
72293 IntegrityCk *pCheck, /* Integrity check context */
72294 Pgno iChild, /* Child page number */
72295 u8 eType, /* Expected pointer map type */
72296 Pgno iParent /* Expected pointer map parent page number */
72297){
72298 int rc;
72299 u8 ePtrmapType;
72300 Pgno iPtrmapParent;
72301
72302 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
72303 if( rc!=SQLITE_OK ){
72304 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1;
72305 checkAppendMsg(pCheck, "Failed to read ptrmap key=%d", iChild);
72306 return;
72307 }
72308
72309 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
72310 checkAppendMsg(pCheck,
72311 "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
72312 iChild, eType, iParent, ePtrmapType, iPtrmapParent);
72313 }
72314}
72315#endif
72316
72317/*
72318** Check the integrity of the freelist or of an overflow page list.
72319** Verify that the number of pages on the list is N.
72320*/
72321static void checkList(
72322 IntegrityCk *pCheck, /* Integrity checking context */
72323 int isFreeList, /* True for a freelist. False for overflow page list */
72324 int iPage, /* Page number for first page in the list */
72325 int N /* Expected number of pages in the list */
72326){
72327 int i;
72328 int expected = N;
72329 int nErrAtStart = pCheck->nErr;
72330 while( iPage!=0 && pCheck->mxErr ){
72331 DbPage *pOvflPage;
72332 unsigned char *pOvflData;
72333 if( checkRef(pCheck, iPage) ) break;
72334 N--;
72335 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
72336 checkAppendMsg(pCheck, "failed to get page %d", iPage);
72337 break;
72338 }
72339 pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
72340 if( isFreeList ){
72341 int n = get4byte(&pOvflData[4]);
72342#ifndef SQLITE_OMIT_AUTOVACUUM
72343 if( pCheck->pBt->autoVacuum ){
72344 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0);
72345 }
72346#endif
72347 if( n>(int)pCheck->pBt->usableSize/4-2 ){
72348 checkAppendMsg(pCheck,
72349 "freelist leaf count too big on page %d", iPage);
72350 N--;
72351 }else{
72352 for(i=0; i<n; i++){
72353 Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
72354#ifndef SQLITE_OMIT_AUTOVACUUM
72355 if( pCheck->pBt->autoVacuum ){
72356 checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0);
72357 }
72358#endif
72359 checkRef(pCheck, iFreePage);
72360 }
72361 N -= n;
72362 }
72363 }
72364#ifndef SQLITE_OMIT_AUTOVACUUM
72365 else{
72366 /* If this database supports auto-vacuum and iPage is not the last
72367 ** page in this overflow list, check that the pointer-map entry for
72368 ** the following page matches iPage.
72369 */
72370 if( pCheck->pBt->autoVacuum && N>0 ){
72371 i = get4byte(pOvflData);
72372 checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage);
72373 }
72374 }
72375#endif
72376 iPage = get4byte(pOvflData);
72377 sqlite3PagerUnref(pOvflPage);
72378 }
72379 if( N && nErrAtStart==pCheck->nErr ){
72380 checkAppendMsg(pCheck,
72381 "%s is %d but should be %d",
72382 isFreeList ? "size" : "overflow list length",
72383 expected-N, expected);
72384 }
72385}
72386#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
72387
72388/*
72389** An implementation of a min-heap.
72390**
72391** aHeap[0] is the number of elements on the heap. aHeap[1] is the
72392** root element. The daughter nodes of aHeap[N] are aHeap[N*2]
72393** and aHeap[N*2+1].
72394**
72395** The heap property is this: Every node is less than or equal to both
72396** of its daughter nodes. A consequence of the heap property is that the
72397** root node aHeap[1] is always the minimum value currently in the heap.
72398**
72399** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
72400** the heap, preserving the heap property. The btreeHeapPull() routine
72401** removes the root element from the heap (the minimum value in the heap)
72402** and then moves other nodes around as necessary to preserve the heap
72403** property.
72404**
72405** This heap is used for cell overlap and coverage testing. Each u32
72406** entry represents the span of a cell or freeblock on a btree page.
72407** The upper 16 bits are the index of the first byte of a range and the
72408** lower 16 bits are the index of the last byte of that range.
72409*/
72410static void btreeHeapInsert(u32 *aHeap, u32 x){
72411 u32 j, i = ++aHeap[0];
72412 aHeap[i] = x;
72413 while( (j = i/2)>0 && aHeap[j]>aHeap[i] ){
72414 x = aHeap[j];
72415 aHeap[j] = aHeap[i];
72416 aHeap[i] = x;
72417 i = j;
72418 }
72419}
72420static int btreeHeapPull(u32 *aHeap, u32 *pOut){
72421 u32 j, i, x;
72422 if( (x = aHeap[0])==0 ) return 0;
72423 *pOut = aHeap[1];
72424 aHeap[1] = aHeap[x];
72425 aHeap[x] = 0xffffffff;
72426 aHeap[0]--;
72427 i = 1;
72428 while( (j = i*2)<=aHeap[0] ){
72429 if( aHeap[j]>aHeap[j+1] ) j++;
72430 if( aHeap[i]<aHeap[j] ) break;
72431 x = aHeap[i];
72432 aHeap[i] = aHeap[j];
72433 aHeap[j] = x;
72434 i = j;
72435 }
72436 return 1;
72437}
72438
72439#ifndef SQLITE_OMIT_INTEGRITY_CHECK
72440/*
72441** Do various sanity checks on a single page of a tree. Return
72442** the tree depth. Root pages return 0. Parents of root pages
72443** return 1, and so forth.
72444**
72445** These checks are done:
72446**
72447** 1. Make sure that cells and freeblocks do not overlap
72448** but combine to completely cover the page.
72449** 2. Make sure integer cell keys are in order.
72450** 3. Check the integrity of overflow pages.
72451** 4. Recursively call checkTreePage on all children.
72452** 5. Verify that the depth of all children is the same.
72453*/
72454static int checkTreePage(
72455 IntegrityCk *pCheck, /* Context for the sanity check */
72456 int iPage, /* Page number of the page to check */
72457 i64 *piMinKey, /* Write minimum integer primary key here */
72458 i64 maxKey /* Error if integer primary key greater than this */
72459){
72460 MemPage *pPage = 0; /* The page being analyzed */
72461 int i; /* Loop counter */
72462 int rc; /* Result code from subroutine call */
72463 int depth = -1, d2; /* Depth of a subtree */
72464 int pgno; /* Page number */
72465 int nFrag; /* Number of fragmented bytes on the page */
72466 int hdr; /* Offset to the page header */
72467 int cellStart; /* Offset to the start of the cell pointer array */
72468 int nCell; /* Number of cells */
72469 int doCoverageCheck = 1; /* True if cell coverage checking should be done */
72470 int keyCanBeEqual = 1; /* True if IPK can be equal to maxKey
72471 ** False if IPK must be strictly less than maxKey */
72472 u8 *data; /* Page content */
72473 u8 *pCell; /* Cell content */
72474 u8 *pCellIdx; /* Next element of the cell pointer array */
72475 BtShared *pBt; /* The BtShared object that owns pPage */
72476 u32 pc; /* Address of a cell */
72477 u32 usableSize; /* Usable size of the page */
72478 u32 contentOffset; /* Offset to the start of the cell content area */
72479 u32 *heap = 0; /* Min-heap used for checking cell coverage */
72480 u32 x, prev = 0; /* Next and previous entry on the min-heap */
72481 const char *saved_zPfx = pCheck->zPfx;
72482 int saved_v1 = pCheck->v1;
72483 int saved_v2 = pCheck->v2;
72484 u8 savedIsInit = 0;
72485
72486 /* Check that the page exists
72487 */
72488 pBt = pCheck->pBt;
72489 usableSize = pBt->usableSize;
72490 if( iPage==0 ) return 0;
72491 if( checkRef(pCheck, iPage) ) return 0;
72492 pCheck->zPfx = "Page %d: ";
72493 pCheck->v1 = iPage;
72494 if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
72495 checkAppendMsg(pCheck,
72496 "unable to get the page. error code=%d", rc);
72497 goto end_of_check;
72498 }
72499
72500 /* Clear MemPage.isInit to make sure the corruption detection code in
72501 ** btreeInitPage() is executed. */
72502 savedIsInit = pPage->isInit;
72503 pPage->isInit = 0;
72504 if( (rc = btreeInitPage(pPage))!=0 ){
72505 assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */
72506 checkAppendMsg(pCheck,
72507 "btreeInitPage() returns error code %d", rc);
72508 goto end_of_check;
72509 }
72510 data = pPage->aData;
72511 hdr = pPage->hdrOffset;
72512
72513 /* Set up for cell analysis */
72514 pCheck->zPfx = "On tree page %d cell %d: ";
72515 contentOffset = get2byteNotZero(&data[hdr+5]);
72516 assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */
72517
72518 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
72519 ** number of cells on the page. */
72520 nCell = get2byte(&data[hdr+3]);
72521 assert( pPage->nCell==nCell );
72522
72523 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
72524 ** immediately follows the b-tree page header. */
72525 cellStart = hdr + 12 - 4*pPage->leaf;
72526 assert( pPage->aCellIdx==&data[cellStart] );
72527 pCellIdx = &data[cellStart + 2*(nCell-1)];
72528
72529 if( !pPage->leaf ){
72530 /* Analyze the right-child page of internal pages */
72531 pgno = get4byte(&data[hdr+8]);
72532#ifndef SQLITE_OMIT_AUTOVACUUM
72533 if( pBt->autoVacuum ){
72534 pCheck->zPfx = "On page %d at right child: ";
72535 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
72536 }
72537#endif
72538 depth = checkTreePage(pCheck, pgno, &maxKey, maxKey);
72539 keyCanBeEqual = 0;
72540 }else{
72541 /* For leaf pages, the coverage check will occur in the same loop
72542 ** as the other cell checks, so initialize the heap. */
72543 heap = pCheck->heap;
72544 heap[0] = 0;
72545 }
72546
72547 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
72548 ** integer offsets to the cell contents. */
72549 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
72550 CellInfo info;
72551
72552 /* Check cell size */
72553 pCheck->v2 = i;
72554 assert( pCellIdx==&data[cellStart + i*2] );
72555 pc = get2byteAligned(pCellIdx);
72556 pCellIdx -= 2;
72557 if( pc<contentOffset || pc>usableSize-4 ){
72558 checkAppendMsg(pCheck, "Offset %d out of range %d..%d",
72559 pc, contentOffset, usableSize-4);
72560 doCoverageCheck = 0;
72561 continue;
72562 }
72563 pCell = &data[pc];
72564 pPage->xParseCell(pPage, pCell, &info);
72565 if( pc+info.nSize>usableSize ){
72566 checkAppendMsg(pCheck, "Extends off end of page");
72567 doCoverageCheck = 0;
72568 continue;
72569 }
72570
72571 /* Check for integer primary key out of range */
72572 if( pPage->intKey ){
72573 if( keyCanBeEqual ? (info.nKey > maxKey) : (info.nKey >= maxKey) ){
72574 checkAppendMsg(pCheck, "Rowid %lld out of order", info.nKey);
72575 }
72576 maxKey = info.nKey;
72577 keyCanBeEqual = 0; /* Only the first key on the page may ==maxKey */
72578 }
72579
72580 /* Check the content overflow list */
72581 if( info.nPayload>info.nLocal ){
72582 int nPage; /* Number of pages on the overflow chain */
72583 Pgno pgnoOvfl; /* First page of the overflow chain */
72584 assert( pc + info.nSize - 4 <= usableSize );
72585 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
72586 pgnoOvfl = get4byte(&pCell[info.nSize - 4]);
72587#ifndef SQLITE_OMIT_AUTOVACUUM
72588 if( pBt->autoVacuum ){
72589 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage);
72590 }
72591#endif
72592 checkList(pCheck, 0, pgnoOvfl, nPage);
72593 }
72594
72595 if( !pPage->leaf ){
72596 /* Check sanity of left child page for internal pages */
72597 pgno = get4byte(pCell);
72598#ifndef SQLITE_OMIT_AUTOVACUUM
72599 if( pBt->autoVacuum ){
72600 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
72601 }
72602#endif
72603 d2 = checkTreePage(pCheck, pgno, &maxKey, maxKey);
72604 keyCanBeEqual = 0;
72605 if( d2!=depth ){
72606 checkAppendMsg(pCheck, "Child page depth differs");
72607 depth = d2;
72608 }
72609 }else{
72610 /* Populate the coverage-checking heap for leaf pages */
72611 btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
72612 }
72613 }
72614 *piMinKey = maxKey;
72615
72616 /* Check for complete coverage of the page
72617 */
72618 pCheck->zPfx = 0;
72619 if( doCoverageCheck && pCheck->mxErr>0 ){
72620 /* For leaf pages, the min-heap has already been initialized and the
72621 ** cells have already been inserted. But for internal pages, that has
72622 ** not yet been done, so do it now */
72623 if( !pPage->leaf ){
72624 heap = pCheck->heap;
72625 heap[0] = 0;
72626 for(i=nCell-1; i>=0; i--){
72627 u32 size;
72628 pc = get2byteAligned(&data[cellStart+i*2]);
72629 size = pPage->xCellSize(pPage, &data[pc]);
72630 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
72631 }
72632 }
72633 /* Add the freeblocks to the min-heap
72634 **
72635 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
72636 ** is the offset of the first freeblock, or zero if there are no
72637 ** freeblocks on the page.
72638 */
72639 i = get2byte(&data[hdr+1]);
72640 while( i>0 ){
72641 int size, j;
72642 assert( (u32)i<=usableSize-4 ); /* Enforced by btreeInitPage() */
72643 size = get2byte(&data[i+2]);
72644 assert( (u32)(i+size)<=usableSize ); /* Enforced by btreeInitPage() */
72645 btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
72646 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
72647 ** big-endian integer which is the offset in the b-tree page of the next
72648 ** freeblock in the chain, or zero if the freeblock is the last on the
72649 ** chain. */
72650 j = get2byte(&data[i]);
72651 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
72652 ** increasing offset. */
72653 assert( j==0 || j>i+size ); /* Enforced by btreeInitPage() */
72654 assert( (u32)j<=usableSize-4 ); /* Enforced by btreeInitPage() */
72655 i = j;
72656 }
72657 /* Analyze the min-heap looking for overlap between cells and/or
72658 ** freeblocks, and counting the number of untracked bytes in nFrag.
72659 **
72660 ** Each min-heap entry is of the form: (start_address<<16)|end_address.
72661 ** There is an implied first entry the covers the page header, the cell
72662 ** pointer index, and the gap between the cell pointer index and the start
72663 ** of cell content.
72664 **
72665 ** The loop below pulls entries from the min-heap in order and compares
72666 ** the start_address against the previous end_address. If there is an
72667 ** overlap, that means bytes are used multiple times. If there is a gap,
72668 ** that gap is added to the fragmentation count.
72669 */
72670 nFrag = 0;
72671 prev = contentOffset - 1; /* Implied first min-heap entry */
72672 while( btreeHeapPull(heap,&x) ){
72673 if( (prev&0xffff)>=(x>>16) ){
72674 checkAppendMsg(pCheck,
72675 "Multiple uses for byte %u of page %d", x>>16, iPage);
72676 break;
72677 }else{
72678 nFrag += (x>>16) - (prev&0xffff) - 1;
72679 prev = x;
72680 }
72681 }
72682 nFrag += usableSize - (prev&0xffff) - 1;
72683 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
72684 ** is stored in the fifth field of the b-tree page header.
72685 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
72686 ** number of fragmented free bytes within the cell content area.
72687 */
72688 if( heap[0]==0 && nFrag!=data[hdr+7] ){
72689 checkAppendMsg(pCheck,
72690 "Fragmentation of %d bytes reported as %d on page %d",
72691 nFrag, data[hdr+7], iPage);
72692 }
72693 }
72694
72695end_of_check:
72696 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
72697 releasePage(pPage);
72698 pCheck->zPfx = saved_zPfx;
72699 pCheck->v1 = saved_v1;
72700 pCheck->v2 = saved_v2;
72701 return depth+1;
72702}
72703#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
72704
72705#ifndef SQLITE_OMIT_INTEGRITY_CHECK
72706/*
72707** This routine does a complete check of the given BTree file. aRoot[] is
72708** an array of pages numbers were each page number is the root page of
72709** a table. nRoot is the number of entries in aRoot.
72710**
72711** A read-only or read-write transaction must be opened before calling
72712** this function.
72713**
72714** Write the number of error seen in *pnErr. Except for some memory
72715** allocation errors, an error message held in memory obtained from
72716** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
72717** returned. If a memory allocation error occurs, NULL is returned.
72718*/
72719SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(
72720 Btree *p, /* The btree to be checked */
72721 int *aRoot, /* An array of root pages numbers for individual trees */
72722 int nRoot, /* Number of entries in aRoot[] */
72723 int mxErr, /* Stop reporting errors after this many */
72724 int *pnErr /* Write number of errors seen to this variable */
72725){
72726 Pgno i;
72727 IntegrityCk sCheck;
72728 BtShared *pBt = p->pBt;
72729 int savedDbFlags = pBt->db->flags;
72730 char zErr[100];
72731 VVA_ONLY( int nRef );
72732
72733 sqlite3BtreeEnter(p);
72734 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
72735 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
72736 assert( nRef>=0 );
72737 sCheck.pBt = pBt;
72738 sCheck.pPager = pBt->pPager;
72739 sCheck.nPage = btreePagecount(sCheck.pBt);
72740 sCheck.mxErr = mxErr;
72741 sCheck.nErr = 0;
72742 sCheck.mallocFailed = 0;
72743 sCheck.zPfx = 0;
72744 sCheck.v1 = 0;
72745 sCheck.v2 = 0;
72746 sCheck.aPgRef = 0;
72747 sCheck.heap = 0;
72748 sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
72749 sCheck.errMsg.printfFlags = SQLITE_PRINTF_INTERNAL;
72750 if( sCheck.nPage==0 ){
72751 goto integrity_ck_cleanup;
72752 }
72753
72754 sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
72755 if( !sCheck.aPgRef ){
72756 sCheck.mallocFailed = 1;
72757 goto integrity_ck_cleanup;
72758 }
72759 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
72760 if( sCheck.heap==0 ){
72761 sCheck.mallocFailed = 1;
72762 goto integrity_ck_cleanup;
72763 }
72764
72765 i = PENDING_BYTE_PAGE(pBt);
72766 if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
72767
72768 /* Check the integrity of the freelist
72769 */
72770 sCheck.zPfx = "Main freelist: ";
72771 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
72772 get4byte(&pBt->pPage1->aData[36]));
72773 sCheck.zPfx = 0;
72774
72775 /* Check all the tables.
72776 */
72777#ifndef SQLITE_OMIT_AUTOVACUUM
72778 if( pBt->autoVacuum ){
72779 int mx = 0;
72780 int mxInHdr;
72781 for(i=0; (int)i<nRoot; i++) if( mx<aRoot[i] ) mx = aRoot[i];
72782 mxInHdr = get4byte(&pBt->pPage1->aData[52]);
72783 if( mx!=mxInHdr ){
72784 checkAppendMsg(&sCheck,
72785 "max rootpage (%d) disagrees with header (%d)",
72786 mx, mxInHdr
72787 );
72788 }
72789 }else if( get4byte(&pBt->pPage1->aData[64])!=0 ){
72790 checkAppendMsg(&sCheck,
72791 "incremental_vacuum enabled with a max rootpage of zero"
72792 );
72793 }
72794#endif
72795 testcase( pBt->db->flags & SQLITE_CellSizeCk );
72796 pBt->db->flags &= ~SQLITE_CellSizeCk;
72797 for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
72798 i64 notUsed;
72799 if( aRoot[i]==0 ) continue;
72800#ifndef SQLITE_OMIT_AUTOVACUUM
72801 if( pBt->autoVacuum && aRoot[i]>1 ){
72802 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0);
72803 }
72804#endif
72805 checkTreePage(&sCheck, aRoot[i], &notUsed, LARGEST_INT64);
72806 }
72807 pBt->db->flags = savedDbFlags;
72808
72809 /* Make sure every page in the file is referenced
72810 */
72811 for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
72812#ifdef SQLITE_OMIT_AUTOVACUUM
72813 if( getPageReferenced(&sCheck, i)==0 ){
72814 checkAppendMsg(&sCheck, "Page %d is never used", i);
72815 }
72816#else
72817 /* If the database supports auto-vacuum, make sure no tables contain
72818 ** references to pointer-map pages.
72819 */
72820 if( getPageReferenced(&sCheck, i)==0 &&
72821 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
72822 checkAppendMsg(&sCheck, "Page %d is never used", i);
72823 }
72824 if( getPageReferenced(&sCheck, i)!=0 &&
72825 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
72826 checkAppendMsg(&sCheck, "Pointer map page %d is referenced", i);
72827 }
72828#endif
72829 }
72830
72831 /* Clean up and report errors.
72832 */
72833integrity_ck_cleanup:
72834 sqlite3PageFree(sCheck.heap);
72835 sqlite3_free(sCheck.aPgRef);
72836 if( sCheck.mallocFailed ){
72837 sqlite3_str_reset(&sCheck.errMsg);
72838 sCheck.nErr++;
72839 }
72840 *pnErr = sCheck.nErr;
72841 if( sCheck.nErr==0 ) sqlite3_str_reset(&sCheck.errMsg);
72842 /* Make sure this analysis did not leave any unref() pages. */
72843 assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
72844 sqlite3BtreeLeave(p);
72845 return sqlite3StrAccumFinish(&sCheck.errMsg);
72846}
72847#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
72848
72849/*
72850** Return the full pathname of the underlying database file. Return
72851** an empty string if the database is in-memory or a TEMP database.
72852**
72853** The pager filename is invariant as long as the pager is
72854** open so it is safe to access without the BtShared mutex.
72855*/
72856SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *p){
72857 assert( p->pBt->pPager!=0 );
72858 return sqlite3PagerFilename(p->pBt->pPager, 1);
72859}
72860
72861/*
72862** Return the pathname of the journal file for this database. The return
72863** value of this routine is the same regardless of whether the journal file
72864** has been created or not.
72865**
72866** The pager journal filename is invariant as long as the pager is
72867** open so it is safe to access without the BtShared mutex.
72868*/
72869SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *p){
72870 assert( p->pBt->pPager!=0 );
72871 return sqlite3PagerJournalname(p->pBt->pPager);
72872}
72873
72874/*
72875** Return non-zero if a transaction is active.
72876*/
72877SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree *p){
72878 assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
72879 return (p && (p->inTrans==TRANS_WRITE));
72880}
72881
72882#ifndef SQLITE_OMIT_WAL
72883/*
72884** Run a checkpoint on the Btree passed as the first argument.
72885**
72886** Return SQLITE_LOCKED if this or any other connection has an open
72887** transaction on the shared-cache the argument Btree is connected to.
72888**
72889** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
72890*/
72891SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
72892 int rc = SQLITE_OK;
72893 if( p ){
72894 BtShared *pBt = p->pBt;
72895 sqlite3BtreeEnter(p);
72896 if( pBt->inTransaction!=TRANS_NONE ){
72897 rc = SQLITE_LOCKED;
72898 }else{
72899 rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
72900 }
72901 sqlite3BtreeLeave(p);
72902 }
72903 return rc;
72904}
72905#endif
72906
72907/*
72908** Return non-zero if a read (or write) transaction is active.
72909*/
72910SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree *p){
72911 assert( p );
72912 assert( sqlite3_mutex_held(p->db->mutex) );
72913 return p->inTrans!=TRANS_NONE;
72914}
72915
72916SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree *p){
72917 assert( p );
72918 assert( sqlite3_mutex_held(p->db->mutex) );
72919 return p->nBackup!=0;
72920}
72921
72922/*
72923** This function returns a pointer to a blob of memory associated with
72924** a single shared-btree. The memory is used by client code for its own
72925** purposes (for example, to store a high-level schema associated with
72926** the shared-btree). The btree layer manages reference counting issues.
72927**
72928** The first time this is called on a shared-btree, nBytes bytes of memory
72929** are allocated, zeroed, and returned to the caller. For each subsequent
72930** call the nBytes parameter is ignored and a pointer to the same blob
72931** of memory returned.
72932**
72933** If the nBytes parameter is 0 and the blob of memory has not yet been
72934** allocated, a null pointer is returned. If the blob has already been
72935** allocated, it is returned as normal.
72936**
72937** Just before the shared-btree is closed, the function passed as the
72938** xFree argument when the memory allocation was made is invoked on the
72939** blob of allocated memory. The xFree function should not call sqlite3_free()
72940** on the memory, the btree layer does that.
72941*/
72942SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
72943 BtShared *pBt = p->pBt;
72944 sqlite3BtreeEnter(p);
72945 if( !pBt->pSchema && nBytes ){
72946 pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
72947 pBt->xFreeSchema = xFree;
72948 }
72949 sqlite3BtreeLeave(p);
72950 return pBt->pSchema;
72951}
72952
72953/*
72954** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared
72955** btree as the argument handle holds an exclusive lock on the
72956** sqlite_master table. Otherwise SQLITE_OK.
72957*/
72958SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *p){
72959 int rc;
72960 assert( sqlite3_mutex_held(p->db->mutex) );
72961 sqlite3BtreeEnter(p);
72962 rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
72963 assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE );
72964 sqlite3BtreeLeave(p);
72965 return rc;
72966}
72967
72968
72969#ifndef SQLITE_OMIT_SHARED_CACHE
72970/*
72971** Obtain a lock on the table whose root page is iTab. The
72972** lock is a write lock if isWritelock is true or a read lock
72973** if it is false.
72974*/
72975SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
72976 int rc = SQLITE_OK;
72977 assert( p->inTrans!=TRANS_NONE );
72978 if( p->sharable ){
72979 u8 lockType = READ_LOCK + isWriteLock;
72980 assert( READ_LOCK+1==WRITE_LOCK );
72981 assert( isWriteLock==0 || isWriteLock==1 );
72982
72983 sqlite3BtreeEnter(p);
72984 rc = querySharedCacheTableLock(p, iTab, lockType);
72985 if( rc==SQLITE_OK ){
72986 rc = setSharedCacheTableLock(p, iTab, lockType);
72987 }
72988 sqlite3BtreeLeave(p);
72989 }
72990 return rc;
72991}
72992#endif
72993
72994#ifndef SQLITE_OMIT_INCRBLOB
72995/*
72996** Argument pCsr must be a cursor opened for writing on an
72997** INTKEY table currently pointing at a valid table entry.
72998** This function modifies the data stored as part of that entry.
72999**
73000** Only the data content may only be modified, it is not possible to
73001** change the length of the data stored. If this function is called with
73002** parameters that attempt to write past the end of the existing data,
73003** no modifications are made and SQLITE_CORRUPT is returned.
73004*/
73005SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
73006 int rc;
73007 assert( cursorOwnsBtShared(pCsr) );
73008 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
73009 assert( pCsr->curFlags & BTCF_Incrblob );
73010
73011 rc = restoreCursorPosition(pCsr);
73012 if( rc!=SQLITE_OK ){
73013 return rc;
73014 }
73015 assert( pCsr->eState!=CURSOR_REQUIRESEEK );
73016 if( pCsr->eState!=CURSOR_VALID ){
73017 return SQLITE_ABORT;
73018 }
73019
73020 /* Save the positions of all other cursors open on this table. This is
73021 ** required in case any of them are holding references to an xFetch
73022 ** version of the b-tree page modified by the accessPayload call below.
73023 **
73024 ** Note that pCsr must be open on a INTKEY table and saveCursorPosition()
73025 ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
73026 ** saveAllCursors can only return SQLITE_OK.
73027 */
73028 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
73029 assert( rc==SQLITE_OK );
73030
73031 /* Check some assumptions:
73032 ** (a) the cursor is open for writing,
73033 ** (b) there is a read/write transaction open,
73034 ** (c) the connection holds a write-lock on the table (if required),
73035 ** (d) there are no conflicting read-locks, and
73036 ** (e) the cursor points at a valid row of an intKey table.
73037 */
73038 if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
73039 return SQLITE_READONLY;
73040 }
73041 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
73042 && pCsr->pBt->inTransaction==TRANS_WRITE );
73043 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
73044 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
73045 assert( pCsr->pPage->intKey );
73046
73047 return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
73048}
73049
73050/*
73051** Mark this cursor as an incremental blob cursor.
73052*/
73053SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
73054 pCur->curFlags |= BTCF_Incrblob;
73055 pCur->pBtree->hasIncrblobCur = 1;
73056}
73057#endif
73058
73059/*
73060** Set both the "read version" (single byte at byte offset 18) and
73061** "write version" (single byte at byte offset 19) fields in the database
73062** header to iVersion.
73063*/
73064SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
73065 BtShared *pBt = pBtree->pBt;
73066 int rc; /* Return code */
73067
73068 assert( iVersion==1 || iVersion==2 );
73069
73070 /* If setting the version fields to 1, do not automatically open the
73071 ** WAL connection, even if the version fields are currently set to 2.
73072 */
73073 pBt->btsFlags &= ~BTS_NO_WAL;
73074 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
73075
73076 rc = sqlite3BtreeBeginTrans(pBtree, 0, 0);
73077 if( rc==SQLITE_OK ){
73078 u8 *aData = pBt->pPage1->aData;
73079 if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
73080 rc = sqlite3BtreeBeginTrans(pBtree, 2, 0);
73081 if( rc==SQLITE_OK ){
73082 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
73083 if( rc==SQLITE_OK ){
73084 aData[18] = (u8)iVersion;
73085 aData[19] = (u8)iVersion;
73086 }
73087 }
73088 }
73089 }
73090
73091 pBt->btsFlags &= ~BTS_NO_WAL;
73092 return rc;
73093}
73094
73095/*
73096** Return true if the cursor has a hint specified. This routine is
73097** only used from within assert() statements
73098*/
73099SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){
73100 return (pCsr->hints & mask)!=0;
73101}
73102
73103/*
73104** Return true if the given Btree is read-only.
73105*/
73106SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){
73107 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
73108}
73109
73110/*
73111** Return the size of the header added to each page by this module.
73112*/
73113SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage)); }
73114
73115#if !defined(SQLITE_OMIT_SHARED_CACHE)
73116/*
73117** Return true if the Btree passed as the only argument is sharable.
73118*/
73119SQLITE_PRIVATE int sqlite3BtreeSharable(Btree *p){
73120 return p->sharable;
73121}
73122
73123/*
73124** Return the number of connections to the BtShared object accessed by
73125** the Btree handle passed as the only argument. For private caches
73126** this is always 1. For shared caches it may be 1 or greater.
73127*/
73128SQLITE_PRIVATE int sqlite3BtreeConnectionCount(Btree *p){
73129 testcase( p->sharable );
73130 return p->pBt->nRef;
73131}
73132#endif
73133
73134/************** End of btree.c ***********************************************/
73135/************** Begin file backup.c ******************************************/
73136/*
73137** 2009 January 28
73138**
73139** The author disclaims copyright to this source code. In place of
73140** a legal notice, here is a blessing:
73141**
73142** May you do good and not evil.
73143** May you find forgiveness for yourself and forgive others.
73144** May you share freely, never taking more than you give.
73145**
73146*************************************************************************
73147** This file contains the implementation of the sqlite3_backup_XXX()
73148** API functions and the related features.
73149*/
73150/* #include "sqliteInt.h" */
73151/* #include "btreeInt.h" */
73152
73153/*
73154** Structure allocated for each backup operation.
73155*/
73156struct sqlite3_backup {
73157 sqlite3* pDestDb; /* Destination database handle */
73158 Btree *pDest; /* Destination b-tree file */
73159 u32 iDestSchema; /* Original schema cookie in destination */
73160 int bDestLocked; /* True once a write-transaction is open on pDest */
73161
73162 Pgno iNext; /* Page number of the next source page to copy */
73163 sqlite3* pSrcDb; /* Source database handle */
73164 Btree *pSrc; /* Source b-tree file */
73165
73166 int rc; /* Backup process error code */
73167
73168 /* These two variables are set by every call to backup_step(). They are
73169 ** read by calls to backup_remaining() and backup_pagecount().
73170 */
73171 Pgno nRemaining; /* Number of pages left to copy */
73172 Pgno nPagecount; /* Total number of pages to copy */
73173
73174 int isAttached; /* True once backup has been registered with pager */
73175 sqlite3_backup *pNext; /* Next backup associated with source pager */
73176};
73177
73178/*
73179** THREAD SAFETY NOTES:
73180**
73181** Once it has been created using backup_init(), a single sqlite3_backup
73182** structure may be accessed via two groups of thread-safe entry points:
73183**
73184** * Via the sqlite3_backup_XXX() API function backup_step() and
73185** backup_finish(). Both these functions obtain the source database
73186** handle mutex and the mutex associated with the source BtShared
73187** structure, in that order.
73188**
73189** * Via the BackupUpdate() and BackupRestart() functions, which are
73190** invoked by the pager layer to report various state changes in
73191** the page cache associated with the source database. The mutex
73192** associated with the source database BtShared structure will always
73193** be held when either of these functions are invoked.
73194**
73195** The other sqlite3_backup_XXX() API functions, backup_remaining() and
73196** backup_pagecount() are not thread-safe functions. If they are called
73197** while some other thread is calling backup_step() or backup_finish(),
73198** the values returned may be invalid. There is no way for a call to
73199** BackupUpdate() or BackupRestart() to interfere with backup_remaining()
73200** or backup_pagecount().
73201**
73202** Depending on the SQLite configuration, the database handles and/or
73203** the Btree objects may have their own mutexes that require locking.
73204** Non-sharable Btrees (in-memory databases for example), do not have
73205** associated mutexes.
73206*/
73207
73208/*
73209** Return a pointer corresponding to database zDb (i.e. "main", "temp")
73210** in connection handle pDb. If such a database cannot be found, return
73211** a NULL pointer and write an error message to pErrorDb.
73212**
73213** If the "temp" database is requested, it may need to be opened by this
73214** function. If an error occurs while doing so, return 0 and write an
73215** error message to pErrorDb.
73216*/
73217static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
73218 int i = sqlite3FindDbName(pDb, zDb);
73219
73220 if( i==1 ){
73221 Parse sParse;
73222 int rc = 0;
73223 memset(&sParse, 0, sizeof(sParse));
73224 sParse.db = pDb;
73225 if( sqlite3OpenTempDatabase(&sParse) ){
73226 sqlite3ErrorWithMsg(pErrorDb, sParse.rc, "%s", sParse.zErrMsg);
73227 rc = SQLITE_ERROR;
73228 }
73229 sqlite3DbFree(pErrorDb, sParse.zErrMsg);
73230 sqlite3ParserReset(&sParse);
73231 if( rc ){
73232 return 0;
73233 }
73234 }
73235
73236 if( i<0 ){
73237 sqlite3ErrorWithMsg(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
73238 return 0;
73239 }
73240
73241 return pDb->aDb[i].pBt;
73242}
73243
73244/*
73245** Attempt to set the page size of the destination to match the page size
73246** of the source.
73247*/
73248static int setDestPgsz(sqlite3_backup *p){
73249 int rc;
73250 rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0);
73251 return rc;
73252}
73253
73254/*
73255** Check that there is no open read-transaction on the b-tree passed as the
73256** second argument. If there is not, return SQLITE_OK. Otherwise, if there
73257** is an open read-transaction, return SQLITE_ERROR and leave an error
73258** message in database handle db.
73259*/
73260static int checkReadTransaction(sqlite3 *db, Btree *p){
73261 if( sqlite3BtreeIsInReadTrans(p) ){
73262 sqlite3ErrorWithMsg(db, SQLITE_ERROR, "destination database is in use");
73263 return SQLITE_ERROR;
73264 }
73265 return SQLITE_OK;
73266}
73267
73268/*
73269** Create an sqlite3_backup process to copy the contents of zSrcDb from
73270** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
73271** a pointer to the new sqlite3_backup object.
73272**
73273** If an error occurs, NULL is returned and an error code and error message
73274** stored in database handle pDestDb.
73275*/
73276SQLITE_API sqlite3_backup *sqlite3_backup_init(
73277 sqlite3* pDestDb, /* Database to write to */
73278 const char *zDestDb, /* Name of database within pDestDb */
73279 sqlite3* pSrcDb, /* Database connection to read from */
73280 const char *zSrcDb /* Name of database within pSrcDb */
73281){
73282 sqlite3_backup *p; /* Value to return */
73283
73284#ifdef SQLITE_ENABLE_API_ARMOR
73285 if( !sqlite3SafetyCheckOk(pSrcDb)||!sqlite3SafetyCheckOk(pDestDb) ){
73286 (void)SQLITE_MISUSE_BKPT;
73287 return 0;
73288 }
73289#endif
73290
73291 /* Lock the source database handle. The destination database
73292 ** handle is not locked in this routine, but it is locked in
73293 ** sqlite3_backup_step(). The user is required to ensure that no
73294 ** other thread accesses the destination handle for the duration
73295 ** of the backup operation. Any attempt to use the destination
73296 ** database connection while a backup is in progress may cause
73297 ** a malfunction or a deadlock.
73298 */
73299 sqlite3_mutex_enter(pSrcDb->mutex);
73300 sqlite3_mutex_enter(pDestDb->mutex);
73301
73302 if( pSrcDb==pDestDb ){
73303 sqlite3ErrorWithMsg(
73304 pDestDb, SQLITE_ERROR, "source and destination must be distinct"
73305 );
73306 p = 0;
73307 }else {
73308 /* Allocate space for a new sqlite3_backup object...
73309 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
73310 ** call to sqlite3_backup_init() and is destroyed by a call to
73311 ** sqlite3_backup_finish(). */
73312 p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
73313 if( !p ){
73314 sqlite3Error(pDestDb, SQLITE_NOMEM_BKPT);
73315 }
73316 }
73317
73318 /* If the allocation succeeded, populate the new object. */
73319 if( p ){
73320 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
73321 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
73322 p->pDestDb = pDestDb;
73323 p->pSrcDb = pSrcDb;
73324 p->iNext = 1;
73325 p->isAttached = 0;
73326
73327 if( 0==p->pSrc || 0==p->pDest
73328 || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK
73329 ){
73330 /* One (or both) of the named databases did not exist or an OOM
73331 ** error was hit. Or there is a transaction open on the destination
73332 ** database. The error has already been written into the pDestDb
73333 ** handle. All that is left to do here is free the sqlite3_backup
73334 ** structure. */
73335 sqlite3_free(p);
73336 p = 0;
73337 }
73338 }
73339 if( p ){
73340 p->pSrc->nBackup++;
73341 }
73342
73343 sqlite3_mutex_leave(pDestDb->mutex);
73344 sqlite3_mutex_leave(pSrcDb->mutex);
73345 return p;
73346}
73347
73348/*
73349** Argument rc is an SQLite error code. Return true if this error is
73350** considered fatal if encountered during a backup operation. All errors
73351** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
73352*/
73353static int isFatalError(int rc){
73354 return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
73355}
73356
73357/*
73358** Parameter zSrcData points to a buffer containing the data for
73359** page iSrcPg from the source database. Copy this data into the
73360** destination database.
73361*/
73362static int backupOnePage(
73363 sqlite3_backup *p, /* Backup handle */
73364 Pgno iSrcPg, /* Source database page to backup */
73365 const u8 *zSrcData, /* Source database page data */
73366 int bUpdate /* True for an update, false otherwise */
73367){
73368 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
73369 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
73370 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
73371 const int nCopy = MIN(nSrcPgsz, nDestPgsz);
73372 const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
73373#ifdef SQLITE_HAS_CODEC
73374 /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is
73375 ** guaranteed that the shared-mutex is held by this thread, handle
73376 ** p->pSrc may not actually be the owner. */
73377 int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc);
73378 int nDestReserve = sqlite3BtreeGetOptimalReserve(p->pDest);
73379#endif
73380 int rc = SQLITE_OK;
73381 i64 iOff;
73382
73383 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
73384 assert( p->bDestLocked );
73385 assert( !isFatalError(p->rc) );
73386 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
73387 assert( zSrcData );
73388
73389 /* Catch the case where the destination is an in-memory database and the
73390 ** page sizes of the source and destination differ.
73391 */
73392 if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
73393 rc = SQLITE_READONLY;
73394 }
73395
73396#ifdef SQLITE_HAS_CODEC
73397 /* Backup is not possible if the page size of the destination is changing
73398 ** and a codec is in use.
73399 */
73400 if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){
73401 rc = SQLITE_READONLY;
73402 }
73403
73404 /* Backup is not possible if the number of bytes of reserve space differ
73405 ** between source and destination. If there is a difference, try to
73406 ** fix the destination to agree with the source. If that is not possible,
73407 ** then the backup cannot proceed.
73408 */
73409 if( nSrcReserve!=nDestReserve ){
73410 u32 newPgsz = nSrcPgsz;
73411 rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve);
73412 if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY;
73413 }
73414#endif
73415
73416 /* This loop runs once for each destination page spanned by the source
73417 ** page. For each iteration, variable iOff is set to the byte offset
73418 ** of the destination page.
73419 */
73420 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
73421 DbPage *pDestPg = 0;
73422 Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
73423 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
73424 if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg, 0))
73425 && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
73426 ){
73427 const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
73428 u8 *zDestData = sqlite3PagerGetData(pDestPg);
73429 u8 *zOut = &zDestData[iOff%nDestPgsz];
73430
73431 /* Copy the data from the source page into the destination page.
73432 ** Then clear the Btree layer MemPage.isInit flag. Both this module
73433 ** and the pager code use this trick (clearing the first byte
73434 ** of the page 'extra' space to invalidate the Btree layers
73435 ** cached parse of the page). MemPage.isInit is marked
73436 ** "MUST BE FIRST" for this purpose.
73437 */
73438 memcpy(zOut, zIn, nCopy);
73439 ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
73440 if( iOff==0 && bUpdate==0 ){
73441 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
73442 }
73443 }
73444 sqlite3PagerUnref(pDestPg);
73445 }
73446
73447 return rc;
73448}
73449
73450/*
73451** If pFile is currently larger than iSize bytes, then truncate it to
73452** exactly iSize bytes. If pFile is not larger than iSize bytes, then
73453** this function is a no-op.
73454**
73455** Return SQLITE_OK if everything is successful, or an SQLite error
73456** code if an error occurs.
73457*/
73458static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
73459 i64 iCurrent;
73460 int rc = sqlite3OsFileSize(pFile, &iCurrent);
73461 if( rc==SQLITE_OK && iCurrent>iSize ){
73462 rc = sqlite3OsTruncate(pFile, iSize);
73463 }
73464 return rc;
73465}
73466
73467/*
73468** Register this backup object with the associated source pager for
73469** callbacks when pages are changed or the cache invalidated.
73470*/
73471static void attachBackupObject(sqlite3_backup *p){
73472 sqlite3_backup **pp;
73473 assert( sqlite3BtreeHoldsMutex(p->pSrc) );
73474 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
73475 p->pNext = *pp;
73476 *pp = p;
73477 p->isAttached = 1;
73478}
73479
73480/*
73481** Copy nPage pages from the source b-tree to the destination.
73482*/
73483SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
73484 int rc;
73485 int destMode; /* Destination journal mode */
73486 int pgszSrc = 0; /* Source page size */
73487 int pgszDest = 0; /* Destination page size */
73488
73489#ifdef SQLITE_ENABLE_API_ARMOR
73490 if( p==0 ) return SQLITE_MISUSE_BKPT;
73491#endif
73492 sqlite3_mutex_enter(p->pSrcDb->mutex);
73493 sqlite3BtreeEnter(p->pSrc);
73494 if( p->pDestDb ){
73495 sqlite3_mutex_enter(p->pDestDb->mutex);
73496 }
73497
73498 rc = p->rc;
73499 if( !isFatalError(rc) ){
73500 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
73501 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
73502 int ii; /* Iterator variable */
73503 int nSrcPage = -1; /* Size of source db in pages */
73504 int bCloseTrans = 0; /* True if src db requires unlocking */
73505
73506 /* If the source pager is currently in a write-transaction, return
73507 ** SQLITE_BUSY immediately.
73508 */
73509 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
73510 rc = SQLITE_BUSY;
73511 }else{
73512 rc = SQLITE_OK;
73513 }
73514
73515 /* If there is no open read-transaction on the source database, open
73516 ** one now. If a transaction is opened here, then it will be closed
73517 ** before this function exits.
73518 */
73519 if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
73520 rc = sqlite3BtreeBeginTrans(p->pSrc, 0, 0);
73521 bCloseTrans = 1;
73522 }
73523
73524 /* If the destination database has not yet been locked (i.e. if this
73525 ** is the first call to backup_step() for the current backup operation),
73526 ** try to set its page size to the same as the source database. This
73527 ** is especially important on ZipVFS systems, as in that case it is
73528 ** not possible to create a database file that uses one page size by
73529 ** writing to it with another. */
73530 if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
73531 rc = SQLITE_NOMEM;
73532 }
73533
73534 /* Lock the destination database, if it is not locked already. */
73535 if( SQLITE_OK==rc && p->bDestLocked==0
73536 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2,
73537 (int*)&p->iDestSchema))
73538 ){
73539 p->bDestLocked = 1;
73540 }
73541
73542 /* Do not allow backup if the destination database is in WAL mode
73543 ** and the page sizes are different between source and destination */
73544 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
73545 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
73546 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
73547 if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
73548 rc = SQLITE_READONLY;
73549 }
73550
73551 /* Now that there is a read-lock on the source database, query the
73552 ** source pager for the number of pages in the database.
73553 */
73554 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
73555 assert( nSrcPage>=0 );
73556 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
73557 const Pgno iSrcPg = p->iNext; /* Source page number */
73558 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
73559 DbPage *pSrcPg; /* Source page object */
73560 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg,PAGER_GET_READONLY);
73561 if( rc==SQLITE_OK ){
73562 rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
73563 sqlite3PagerUnref(pSrcPg);
73564 }
73565 }
73566 p->iNext++;
73567 }
73568 if( rc==SQLITE_OK ){
73569 p->nPagecount = nSrcPage;
73570 p->nRemaining = nSrcPage+1-p->iNext;
73571 if( p->iNext>(Pgno)nSrcPage ){
73572 rc = SQLITE_DONE;
73573 }else if( !p->isAttached ){
73574 attachBackupObject(p);
73575 }
73576 }
73577
73578 /* Update the schema version field in the destination database. This
73579 ** is to make sure that the schema-version really does change in
73580 ** the case where the source and destination databases have the
73581 ** same schema version.
73582 */
73583 if( rc==SQLITE_DONE ){
73584 if( nSrcPage==0 ){
73585 rc = sqlite3BtreeNewDb(p->pDest);
73586 nSrcPage = 1;
73587 }
73588 if( rc==SQLITE_OK || rc==SQLITE_DONE ){
73589 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
73590 }
73591 if( rc==SQLITE_OK ){
73592 if( p->pDestDb ){
73593 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
73594 }
73595 if( destMode==PAGER_JOURNALMODE_WAL ){
73596 rc = sqlite3BtreeSetVersion(p->pDest, 2);
73597 }
73598 }
73599 if( rc==SQLITE_OK ){
73600 int nDestTruncate;
73601 /* Set nDestTruncate to the final number of pages in the destination
73602 ** database. The complication here is that the destination page
73603 ** size may be different to the source page size.
73604 **
73605 ** If the source page size is smaller than the destination page size,
73606 ** round up. In this case the call to sqlite3OsTruncate() below will
73607 ** fix the size of the file. However it is important to call
73608 ** sqlite3PagerTruncateImage() here so that any pages in the
73609 ** destination file that lie beyond the nDestTruncate page mark are
73610 ** journalled by PagerCommitPhaseOne() before they are destroyed
73611 ** by the file truncation.
73612 */
73613 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
73614 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
73615 if( pgszSrc<pgszDest ){
73616 int ratio = pgszDest/pgszSrc;
73617 nDestTruncate = (nSrcPage+ratio-1)/ratio;
73618 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
73619 nDestTruncate--;
73620 }
73621 }else{
73622 nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
73623 }
73624 assert( nDestTruncate>0 );
73625
73626 if( pgszSrc<pgszDest ){
73627 /* If the source page-size is smaller than the destination page-size,
73628 ** two extra things may need to happen:
73629 **
73630 ** * The destination may need to be truncated, and
73631 **
73632 ** * Data stored on the pages immediately following the
73633 ** pending-byte page in the source database may need to be
73634 ** copied into the destination database.
73635 */
73636 const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
73637 sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
73638 Pgno iPg;
73639 int nDstPage;
73640 i64 iOff;
73641 i64 iEnd;
73642
73643 assert( pFile );
73644 assert( nDestTruncate==0
73645 || (i64)nDestTruncate*(i64)pgszDest >= iSize || (
73646 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
73647 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
73648 ));
73649
73650 /* This block ensures that all data required to recreate the original
73651 ** database has been stored in the journal for pDestPager and the
73652 ** journal synced to disk. So at this point we may safely modify
73653 ** the database file in any way, knowing that if a power failure
73654 ** occurs, the original database will be reconstructed from the
73655 ** journal file. */
73656 sqlite3PagerPagecount(pDestPager, &nDstPage);
73657 for(iPg=nDestTruncate; rc==SQLITE_OK && iPg<=(Pgno)nDstPage; iPg++){
73658 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
73659 DbPage *pPg;
73660 rc = sqlite3PagerGet(pDestPager, iPg, &pPg, 0);
73661 if( rc==SQLITE_OK ){
73662 rc = sqlite3PagerWrite(pPg);
73663 sqlite3PagerUnref(pPg);
73664 }
73665 }
73666 }
73667 if( rc==SQLITE_OK ){
73668 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
73669 }
73670
73671 /* Write the extra pages and truncate the database file as required */
73672 iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
73673 for(
73674 iOff=PENDING_BYTE+pgszSrc;
73675 rc==SQLITE_OK && iOff<iEnd;
73676 iOff+=pgszSrc
73677 ){
73678 PgHdr *pSrcPg = 0;
73679 const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
73680 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg, 0);
73681 if( rc==SQLITE_OK ){
73682 u8 *zData = sqlite3PagerGetData(pSrcPg);
73683 rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
73684 }
73685 sqlite3PagerUnref(pSrcPg);
73686 }
73687 if( rc==SQLITE_OK ){
73688 rc = backupTruncateFile(pFile, iSize);
73689 }
73690
73691 /* Sync the database file to disk. */
73692 if( rc==SQLITE_OK ){
73693 rc = sqlite3PagerSync(pDestPager, 0);
73694 }
73695 }else{
73696 sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
73697 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
73698 }
73699
73700 /* Finish committing the transaction to the destination database. */
73701 if( SQLITE_OK==rc
73702 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
73703 ){
73704 rc = SQLITE_DONE;
73705 }
73706 }
73707 }
73708
73709 /* If bCloseTrans is true, then this function opened a read transaction
73710 ** on the source database. Close the read transaction here. There is
73711 ** no need to check the return values of the btree methods here, as
73712 ** "committing" a read-only transaction cannot fail.
73713 */
73714 if( bCloseTrans ){
73715 TESTONLY( int rc2 );
73716 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
73717 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
73718 assert( rc2==SQLITE_OK );
73719 }
73720
73721 if( rc==SQLITE_IOERR_NOMEM ){
73722 rc = SQLITE_NOMEM_BKPT;
73723 }
73724 p->rc = rc;
73725 }
73726 if( p->pDestDb ){
73727 sqlite3_mutex_leave(p->pDestDb->mutex);
73728 }
73729 sqlite3BtreeLeave(p->pSrc);
73730 sqlite3_mutex_leave(p->pSrcDb->mutex);
73731 return rc;
73732}
73733
73734/*
73735** Release all resources associated with an sqlite3_backup* handle.
73736*/
73737SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
73738 sqlite3_backup **pp; /* Ptr to head of pagers backup list */
73739 sqlite3 *pSrcDb; /* Source database connection */
73740 int rc; /* Value to return */
73741
73742 /* Enter the mutexes */
73743 if( p==0 ) return SQLITE_OK;
73744 pSrcDb = p->pSrcDb;
73745 sqlite3_mutex_enter(pSrcDb->mutex);
73746 sqlite3BtreeEnter(p->pSrc);
73747 if( p->pDestDb ){
73748 sqlite3_mutex_enter(p->pDestDb->mutex);
73749 }
73750
73751 /* Detach this backup from the source pager. */
73752 if( p->pDestDb ){
73753 p->pSrc->nBackup--;
73754 }
73755 if( p->isAttached ){
73756 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
73757 while( *pp!=p ){
73758 pp = &(*pp)->pNext;
73759 }
73760 *pp = p->pNext;
73761 }
73762
73763 /* If a transaction is still open on the Btree, roll it back. */
73764 sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0);
73765
73766 /* Set the error code of the destination database handle. */
73767 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
73768 if( p->pDestDb ){
73769 sqlite3Error(p->pDestDb, rc);
73770
73771 /* Exit the mutexes and free the backup context structure. */
73772 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
73773 }
73774 sqlite3BtreeLeave(p->pSrc);
73775 if( p->pDestDb ){
73776 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
73777 ** call to sqlite3_backup_init() and is destroyed by a call to
73778 ** sqlite3_backup_finish(). */
73779 sqlite3_free(p);
73780 }
73781 sqlite3LeaveMutexAndCloseZombie(pSrcDb);
73782 return rc;
73783}
73784
73785/*
73786** Return the number of pages still to be backed up as of the most recent
73787** call to sqlite3_backup_step().
73788*/
73789SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
73790#ifdef SQLITE_ENABLE_API_ARMOR
73791 if( p==0 ){
73792 (void)SQLITE_MISUSE_BKPT;
73793 return 0;
73794 }
73795#endif
73796 return p->nRemaining;
73797}
73798
73799/*
73800** Return the total number of pages in the source database as of the most
73801** recent call to sqlite3_backup_step().
73802*/
73803SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
73804#ifdef SQLITE_ENABLE_API_ARMOR
73805 if( p==0 ){
73806 (void)SQLITE_MISUSE_BKPT;
73807 return 0;
73808 }
73809#endif
73810 return p->nPagecount;
73811}
73812
73813/*
73814** This function is called after the contents of page iPage of the
73815** source database have been modified. If page iPage has already been
73816** copied into the destination database, then the data written to the
73817** destination is now invalidated. The destination copy of iPage needs
73818** to be updated with the new data before the backup operation is
73819** complete.
73820**
73821** It is assumed that the mutex associated with the BtShared object
73822** corresponding to the source database is held when this function is
73823** called.
73824*/
73825static SQLITE_NOINLINE void backupUpdate(
73826 sqlite3_backup *p,
73827 Pgno iPage,
73828 const u8 *aData
73829){
73830 assert( p!=0 );
73831 do{
73832 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
73833 if( !isFatalError(p->rc) && iPage<p->iNext ){
73834 /* The backup process p has already copied page iPage. But now it
73835 ** has been modified by a transaction on the source pager. Copy
73836 ** the new data into the backup.
73837 */
73838 int rc;
73839 assert( p->pDestDb );
73840 sqlite3_mutex_enter(p->pDestDb->mutex);
73841 rc = backupOnePage(p, iPage, aData, 1);
73842 sqlite3_mutex_leave(p->pDestDb->mutex);
73843 assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
73844 if( rc!=SQLITE_OK ){
73845 p->rc = rc;
73846 }
73847 }
73848 }while( (p = p->pNext)!=0 );
73849}
73850SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
73851 if( pBackup ) backupUpdate(pBackup, iPage, aData);
73852}
73853
73854/*
73855** Restart the backup process. This is called when the pager layer
73856** detects that the database has been modified by an external database
73857** connection. In this case there is no way of knowing which of the
73858** pages that have been copied into the destination database are still
73859** valid and which are not, so the entire process needs to be restarted.
73860**
73861** It is assumed that the mutex associated with the BtShared object
73862** corresponding to the source database is held when this function is
73863** called.
73864*/
73865SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *pBackup){
73866 sqlite3_backup *p; /* Iterator variable */
73867 for(p=pBackup; p; p=p->pNext){
73868 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
73869 p->iNext = 1;
73870 }
73871}
73872
73873#ifndef SQLITE_OMIT_VACUUM
73874/*
73875** Copy the complete content of pBtFrom into pBtTo. A transaction
73876** must be active for both files.
73877**
73878** The size of file pTo may be reduced by this operation. If anything
73879** goes wrong, the transaction on pTo is rolled back. If successful, the
73880** transaction is committed before returning.
73881*/
73882SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
73883 int rc;
73884 sqlite3_file *pFd; /* File descriptor for database pTo */
73885 sqlite3_backup b;
73886 sqlite3BtreeEnter(pTo);
73887 sqlite3BtreeEnter(pFrom);
73888
73889 assert( sqlite3BtreeIsInTrans(pTo) );
73890 pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
73891 if( pFd->pMethods ){
73892 i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
73893 rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
73894 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
73895 if( rc ) goto copy_finished;
73896 }
73897
73898 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
73899 ** to 0. This is used by the implementations of sqlite3_backup_step()
73900 ** and sqlite3_backup_finish() to detect that they are being called
73901 ** from this function, not directly by the user.
73902 */
73903 memset(&b, 0, sizeof(b));
73904 b.pSrcDb = pFrom->db;
73905 b.pSrc = pFrom;
73906 b.pDest = pTo;
73907 b.iNext = 1;
73908
73909#ifdef SQLITE_HAS_CODEC
73910 sqlite3PagerAlignReserve(sqlite3BtreePager(pTo), sqlite3BtreePager(pFrom));
73911#endif
73912
73913 /* 0x7FFFFFFF is the hard limit for the number of pages in a database
73914 ** file. By passing this as the number of pages to copy to
73915 ** sqlite3_backup_step(), we can guarantee that the copy finishes
73916 ** within a single call (unless an error occurs). The assert() statement
73917 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
73918 ** or an error code. */
73919 sqlite3_backup_step(&b, 0x7FFFFFFF);
73920 assert( b.rc!=SQLITE_OK );
73921
73922 rc = sqlite3_backup_finish(&b);
73923 if( rc==SQLITE_OK ){
73924 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
73925 }else{
73926 sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
73927 }
73928
73929 assert( sqlite3BtreeIsInTrans(pTo)==0 );
73930copy_finished:
73931 sqlite3BtreeLeave(pFrom);
73932 sqlite3BtreeLeave(pTo);
73933 return rc;
73934}
73935#endif /* SQLITE_OMIT_VACUUM */
73936
73937/************** End of backup.c **********************************************/
73938/************** Begin file vdbemem.c *****************************************/
73939/*
73940** 2004 May 26
73941**
73942** The author disclaims copyright to this source code. In place of
73943** a legal notice, here is a blessing:
73944**
73945** May you do good and not evil.
73946** May you find forgiveness for yourself and forgive others.
73947** May you share freely, never taking more than you give.
73948**
73949*************************************************************************
73950**
73951** This file contains code use to manipulate "Mem" structure. A "Mem"
73952** stores a single value in the VDBE. Mem is an opaque structure visible
73953** only within the VDBE. Interface routines refer to a Mem using the
73954** name sqlite_value
73955*/
73956/* #include "sqliteInt.h" */
73957/* #include "vdbeInt.h" */
73958
73959#ifdef SQLITE_DEBUG
73960/*
73961** Check invariants on a Mem object.
73962**
73963** This routine is intended for use inside of assert() statements, like
73964** this: assert( sqlite3VdbeCheckMemInvariants(pMem) );
73965*/
73966SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem *p){
73967 /* If MEM_Dyn is set then Mem.xDel!=0.
73968 ** Mem.xDel might not be initialized if MEM_Dyn is clear.
73969 */
73970 assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
73971
73972 /* MEM_Dyn may only be set if Mem.szMalloc==0. In this way we
73973 ** ensure that if Mem.szMalloc>0 then it is safe to do
73974 ** Mem.z = Mem.zMalloc without having to check Mem.flags&MEM_Dyn.
73975 ** That saves a few cycles in inner loops. */
73976 assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
73977
73978 /* Cannot be both MEM_Int and MEM_Real at the same time */
73979 assert( (p->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) );
73980
73981 if( p->flags & MEM_Null ){
73982 /* Cannot be both MEM_Null and some other type */
73983 assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob|MEM_Agg))==0 );
73984
73985 /* If MEM_Null is set, then either the value is a pure NULL (the usual
73986 ** case) or it is a pointer set using sqlite3_bind_pointer() or
73987 ** sqlite3_result_pointer(). If a pointer, then MEM_Term must also be
73988 ** set.
73989 */
73990 if( (p->flags & (MEM_Term|MEM_Subtype))==(MEM_Term|MEM_Subtype) ){
73991 /* This is a pointer type. There may be a flag to indicate what to
73992 ** do with the pointer. */
73993 assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
73994 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
73995 ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 );
73996
73997 /* No other bits set */
73998 assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype
73999 |MEM_Dyn|MEM_Ephem|MEM_Static))==0 );
74000 }else{
74001 /* A pure NULL might have other flags, such as MEM_Static, MEM_Dyn,
74002 ** MEM_Ephem, MEM_Cleared, or MEM_Subtype */
74003 }
74004 }else{
74005 /* The MEM_Cleared bit is only allowed on NULLs */
74006 assert( (p->flags & MEM_Cleared)==0 );
74007 }
74008
74009 /* The szMalloc field holds the correct memory allocation size */
74010 assert( p->szMalloc==0
74011 || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc) );
74012
74013 /* If p holds a string or blob, the Mem.z must point to exactly
74014 ** one of the following:
74015 **
74016 ** (1) Memory in Mem.zMalloc and managed by the Mem object
74017 ** (2) Memory to be freed using Mem.xDel
74018 ** (3) An ephemeral string or blob
74019 ** (4) A static string or blob
74020 */
74021 if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){
74022 assert(
74023 ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +
74024 ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
74025 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
74026 ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
74027 );
74028 }
74029 return 1;
74030}
74031#endif
74032
74033#ifdef SQLITE_DEBUG
74034/*
74035** Check that string value of pMem agrees with its integer or real value.
74036**
74037** A single int or real value always converts to the same strings. But
74038** many different strings can be converted into the same int or real.
74039** If a table contains a numeric value and an index is based on the
74040** corresponding string value, then it is important that the string be
74041** derived from the numeric value, not the other way around, to ensure
74042** that the index and table are consistent. See ticket
74043** https://www.sqlite.org/src/info/343634942dd54ab (2018-01-31) for
74044** an example.
74045**
74046** This routine looks at pMem to verify that if it has both a numeric
74047** representation and a string representation then the string rep has
74048** been derived from the numeric and not the other way around. It returns
74049** true if everything is ok and false if there is a problem.
74050**
74051** This routine is for use inside of assert() statements only.
74052*/
74053SQLITE_PRIVATE int sqlite3VdbeMemConsistentDualRep(Mem *p){
74054 char zBuf[100];
74055 char *z;
74056 int i, j, incr;
74057 if( (p->flags & MEM_Str)==0 ) return 1;
74058 if( (p->flags & (MEM_Int|MEM_Real))==0 ) return 1;
74059 if( p->flags & MEM_Int ){
74060 sqlite3_snprintf(sizeof(zBuf),zBuf,"%lld",p->u.i);
74061 }else{
74062 sqlite3_snprintf(sizeof(zBuf),zBuf,"%!.15g",p->u.r);
74063 }
74064 z = p->z;
74065 i = j = 0;
74066 incr = 1;
74067 if( p->enc!=SQLITE_UTF8 ){
74068 incr = 2;
74069 if( p->enc==SQLITE_UTF16BE ) z++;
74070 }
74071 while( zBuf[j] ){
74072 if( zBuf[j++]!=z[i] ) return 0;
74073 i += incr;
74074 }
74075 return 1;
74076}
74077#endif /* SQLITE_DEBUG */
74078
74079/*
74080** If pMem is an object with a valid string representation, this routine
74081** ensures the internal encoding for the string representation is
74082** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
74083**
74084** If pMem is not a string object, or the encoding of the string
74085** representation is already stored using the requested encoding, then this
74086** routine is a no-op.
74087**
74088** SQLITE_OK is returned if the conversion is successful (or not required).
74089** SQLITE_NOMEM may be returned if a malloc() fails during conversion
74090** between formats.
74091*/
74092SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
74093#ifndef SQLITE_OMIT_UTF16
74094 int rc;
74095#endif
74096 assert( !sqlite3VdbeMemIsRowSet(pMem) );
74097 assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
74098 || desiredEnc==SQLITE_UTF16BE );
74099 if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
74100 return SQLITE_OK;
74101 }
74102 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
74103#ifdef SQLITE_OMIT_UTF16
74104 return SQLITE_ERROR;
74105#else
74106
74107 /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
74108 ** then the encoding of the value may not have changed.
74109 */
74110 rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
74111 assert(rc==SQLITE_OK || rc==SQLITE_NOMEM);
74112 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
74113 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
74114 return rc;
74115#endif
74116}
74117
74118/*
74119** Make sure pMem->z points to a writable allocation of at least
74120** min(n,32) bytes.
74121**
74122** If the bPreserve argument is true, then copy of the content of
74123** pMem->z into the new allocation. pMem must be either a string or
74124** blob if bPreserve is true. If bPreserve is false, any prior content
74125** in pMem->z is discarded.
74126*/
74127SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
74128 assert( sqlite3VdbeCheckMemInvariants(pMem) );
74129 assert( !sqlite3VdbeMemIsRowSet(pMem) );
74130 testcase( pMem->db==0 );
74131
74132 /* If the bPreserve flag is set to true, then the memory cell must already
74133 ** contain a valid string or blob value. */
74134 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
74135 testcase( bPreserve && pMem->z==0 );
74136
74137 assert( pMem->szMalloc==0
74138 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) );
74139 if( n<32 ) n = 32;
74140 if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
74141 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
74142 bPreserve = 0;
74143 }else{
74144 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
74145 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
74146 }
74147 if( pMem->zMalloc==0 ){
74148 sqlite3VdbeMemSetNull(pMem);
74149 pMem->z = 0;
74150 pMem->szMalloc = 0;
74151 return SQLITE_NOMEM_BKPT;
74152 }else{
74153 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
74154 }
74155
74156 if( bPreserve && pMem->z ){
74157 assert( pMem->z!=pMem->zMalloc );
74158 memcpy(pMem->zMalloc, pMem->z, pMem->n);
74159 }
74160 if( (pMem->flags&MEM_Dyn)!=0 ){
74161 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
74162 pMem->xDel((void *)(pMem->z));
74163 }
74164
74165 pMem->z = pMem->zMalloc;
74166 pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
74167 return SQLITE_OK;
74168}
74169
74170/*
74171** Change the pMem->zMalloc allocation to be at least szNew bytes.
74172** If pMem->zMalloc already meets or exceeds the requested size, this
74173** routine is a no-op.
74174**
74175** Any prior string or blob content in the pMem object may be discarded.
74176** The pMem->xDel destructor is called, if it exists. Though MEM_Str
74177** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, and MEM_Null
74178** values are preserved.
74179**
74180** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM)
74181** if unable to complete the resizing.
74182*/
74183SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){
74184 assert( szNew>0 );
74185 assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );
74186 if( pMem->szMalloc<szNew ){
74187 return sqlite3VdbeMemGrow(pMem, szNew, 0);
74188 }
74189 assert( (pMem->flags & MEM_Dyn)==0 );
74190 pMem->z = pMem->zMalloc;
74191 pMem->flags &= (MEM_Null|MEM_Int|MEM_Real);
74192 return SQLITE_OK;
74193}
74194
74195/*
74196** It is already known that pMem contains an unterminated string.
74197** Add the zero terminator.
74198*/
74199static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){
74200 if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
74201 return SQLITE_NOMEM_BKPT;
74202 }
74203 pMem->z[pMem->n] = 0;
74204 pMem->z[pMem->n+1] = 0;
74205 pMem->flags |= MEM_Term;
74206 return SQLITE_OK;
74207}
74208
74209/*
74210** Change pMem so that its MEM_Str or MEM_Blob value is stored in
74211** MEM.zMalloc, where it can be safely written.
74212**
74213** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
74214*/
74215SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
74216 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
74217 assert( !sqlite3VdbeMemIsRowSet(pMem) );
74218 if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){
74219 if( ExpandBlob(pMem) ) return SQLITE_NOMEM;
74220 if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
74221 int rc = vdbeMemAddTerminator(pMem);
74222 if( rc ) return rc;
74223 }
74224 }
74225 pMem->flags &= ~MEM_Ephem;
74226#ifdef SQLITE_DEBUG
74227 pMem->pScopyFrom = 0;
74228#endif
74229
74230 return SQLITE_OK;
74231}
74232
74233/*
74234** If the given Mem* has a zero-filled tail, turn it into an ordinary
74235** blob stored in dynamically allocated space.
74236*/
74237#ifndef SQLITE_OMIT_INCRBLOB
74238SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
74239 int nByte;
74240 assert( pMem->flags & MEM_Zero );
74241 assert( pMem->flags&MEM_Blob );
74242 assert( !sqlite3VdbeMemIsRowSet(pMem) );
74243 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
74244
74245 /* Set nByte to the number of bytes required to store the expanded blob. */
74246 nByte = pMem->n + pMem->u.nZero;
74247 if( nByte<=0 ){
74248 nByte = 1;
74249 }
74250 if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
74251 return SQLITE_NOMEM_BKPT;
74252 }
74253
74254 memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
74255 pMem->n += pMem->u.nZero;
74256 pMem->flags &= ~(MEM_Zero|MEM_Term);
74257 return SQLITE_OK;
74258}
74259#endif
74260
74261/*
74262** Make sure the given Mem is \u0000 terminated.
74263*/
74264SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem *pMem){
74265 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
74266 testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
74267 testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
74268 if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){
74269 return SQLITE_OK; /* Nothing to do */
74270 }else{
74271 return vdbeMemAddTerminator(pMem);
74272 }
74273}
74274
74275/*
74276** Add MEM_Str to the set of representations for the given Mem. Numbers
74277** are converted using sqlite3_snprintf(). Converting a BLOB to a string
74278** is a no-op.
74279**
74280** Existing representations MEM_Int and MEM_Real are invalidated if
74281** bForce is true but are retained if bForce is false.
74282**
74283** A MEM_Null value will never be passed to this function. This function is
74284** used for converting values to text for returning to the user (i.e. via
74285** sqlite3_value_text()), or for ensuring that values to be used as btree
74286** keys are strings. In the former case a NULL pointer is returned the
74287** user and the latter is an internal programming error.
74288*/
74289SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){
74290 int fg = pMem->flags;
74291 const int nByte = 32;
74292
74293 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
74294 assert( !(fg&MEM_Zero) );
74295 assert( !(fg&(MEM_Str|MEM_Blob)) );
74296 assert( fg&(MEM_Int|MEM_Real) );
74297 assert( !sqlite3VdbeMemIsRowSet(pMem) );
74298 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
74299
74300
74301 if( sqlite3VdbeMemClearAndResize(pMem, nByte) ){
74302 pMem->enc = 0;
74303 return SQLITE_NOMEM_BKPT;
74304 }
74305
74306 /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8
74307 ** string representation of the value. Then, if the required encoding
74308 ** is UTF-16le or UTF-16be do a translation.
74309 **
74310 ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
74311 */
74312 if( fg & MEM_Int ){
74313 sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
74314 }else{
74315 assert( fg & MEM_Real );
74316 sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->u.r);
74317 }
74318 assert( pMem->z!=0 );
74319 pMem->n = sqlite3Strlen30NN(pMem->z);
74320 pMem->enc = SQLITE_UTF8;
74321 pMem->flags |= MEM_Str|MEM_Term;
74322 if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real);
74323 sqlite3VdbeChangeEncoding(pMem, enc);
74324 return SQLITE_OK;
74325}
74326
74327/*
74328** Memory cell pMem contains the context of an aggregate function.
74329** This routine calls the finalize method for that function. The
74330** result of the aggregate is stored back into pMem.
74331**
74332** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK
74333** otherwise.
74334*/
74335SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
74336 sqlite3_context ctx;
74337 Mem t;
74338 assert( pFunc!=0 );
74339 assert( pFunc->xFinalize!=0 );
74340 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
74341 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
74342 memset(&ctx, 0, sizeof(ctx));
74343 memset(&t, 0, sizeof(t));
74344 t.flags = MEM_Null;
74345 t.db = pMem->db;
74346 ctx.pOut = &t;
74347 ctx.pMem = pMem;
74348 ctx.pFunc = pFunc;
74349 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
74350 assert( (pMem->flags & MEM_Dyn)==0 );
74351 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
74352 memcpy(pMem, &t, sizeof(t));
74353 return ctx.isError;
74354}
74355
74356/*
74357** Memory cell pAccum contains the context of an aggregate function.
74358** This routine calls the xValue method for that function and stores
74359** the results in memory cell pMem.
74360**
74361** SQLITE_ERROR is returned if xValue() reports an error. SQLITE_OK
74362** otherwise.
74363*/
74364#ifndef SQLITE_OMIT_WINDOWFUNC
74365SQLITE_PRIVATE int sqlite3VdbeMemAggValue(Mem *pAccum, Mem *pOut, FuncDef *pFunc){
74366 sqlite3_context ctx;
74367 Mem t;
74368 assert( pFunc!=0 );
74369 assert( pFunc->xValue!=0 );
74370 assert( (pAccum->flags & MEM_Null)!=0 || pFunc==pAccum->u.pDef );
74371 assert( pAccum->db==0 || sqlite3_mutex_held(pAccum->db->mutex) );
74372 memset(&ctx, 0, sizeof(ctx));
74373 memset(&t, 0, sizeof(t));
74374 t.flags = MEM_Null;
74375 t.db = pAccum->db;
74376 sqlite3VdbeMemSetNull(pOut);
74377 ctx.pOut = pOut;
74378 ctx.pMem = pAccum;
74379 ctx.pFunc = pFunc;
74380 pFunc->xValue(&ctx);
74381 return ctx.isError;
74382}
74383#endif /* SQLITE_OMIT_WINDOWFUNC */
74384
74385/*
74386** If the memory cell contains a value that must be freed by
74387** invoking the external callback in Mem.xDel, then this routine
74388** will free that value. It also sets Mem.flags to MEM_Null.
74389**
74390** This is a helper routine for sqlite3VdbeMemSetNull() and
74391** for sqlite3VdbeMemRelease(). Use those other routines as the
74392** entry point for releasing Mem resources.
74393*/
74394static SQLITE_NOINLINE void vdbeMemClearExternAndSetNull(Mem *p){
74395 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
74396 assert( VdbeMemDynamic(p) );
74397 if( p->flags&MEM_Agg ){
74398 sqlite3VdbeMemFinalize(p, p->u.pDef);
74399 assert( (p->flags & MEM_Agg)==0 );
74400 testcase( p->flags & MEM_Dyn );
74401 }
74402 if( p->flags&MEM_Dyn ){
74403 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
74404 p->xDel((void *)p->z);
74405 }
74406 p->flags = MEM_Null;
74407}
74408
74409/*
74410** Release memory held by the Mem p, both external memory cleared
74411** by p->xDel and memory in p->zMalloc.
74412**
74413** This is a helper routine invoked by sqlite3VdbeMemRelease() in
74414** the unusual case where there really is memory in p that needs
74415** to be freed.
74416*/
74417static SQLITE_NOINLINE void vdbeMemClear(Mem *p){
74418 if( VdbeMemDynamic(p) ){
74419 vdbeMemClearExternAndSetNull(p);
74420 }
74421 if( p->szMalloc ){
74422 sqlite3DbFreeNN(p->db, p->zMalloc);
74423 p->szMalloc = 0;
74424 }
74425 p->z = 0;
74426}
74427
74428/*
74429** Release any memory resources held by the Mem. Both the memory that is
74430** free by Mem.xDel and the Mem.zMalloc allocation are freed.
74431**
74432** Use this routine prior to clean up prior to abandoning a Mem, or to
74433** reset a Mem back to its minimum memory utilization.
74434**
74435** Use sqlite3VdbeMemSetNull() to release just the Mem.xDel space
74436** prior to inserting new content into the Mem.
74437*/
74438SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
74439 assert( sqlite3VdbeCheckMemInvariants(p) );
74440 if( VdbeMemDynamic(p) || p->szMalloc ){
74441 vdbeMemClear(p);
74442 }
74443}
74444
74445/*
74446** Convert a 64-bit IEEE double into a 64-bit signed integer.
74447** If the double is out of range of a 64-bit signed integer then
74448** return the closest available 64-bit signed integer.
74449*/
74450static SQLITE_NOINLINE i64 doubleToInt64(double r){
74451#ifdef SQLITE_OMIT_FLOATING_POINT
74452 /* When floating-point is omitted, double and int64 are the same thing */
74453 return r;
74454#else
74455 /*
74456 ** Many compilers we encounter do not define constants for the
74457 ** minimum and maximum 64-bit integers, or they define them
74458 ** inconsistently. And many do not understand the "LL" notation.
74459 ** So we define our own static constants here using nothing
74460 ** larger than a 32-bit integer constant.
74461 */
74462 static const i64 maxInt = LARGEST_INT64;
74463 static const i64 minInt = SMALLEST_INT64;
74464
74465 if( r<=(double)minInt ){
74466 return minInt;
74467 }else if( r>=(double)maxInt ){
74468 return maxInt;
74469 }else{
74470 return (i64)r;
74471 }
74472#endif
74473}
74474
74475/*
74476** Return some kind of integer value which is the best we can do
74477** at representing the value that *pMem describes as an integer.
74478** If pMem is an integer, then the value is exact. If pMem is
74479** a floating-point then the value returned is the integer part.
74480** If pMem is a string or blob, then we make an attempt to convert
74481** it into an integer and return that. If pMem represents an
74482** an SQL-NULL value, return 0.
74483**
74484** If pMem represents a string value, its encoding might be changed.
74485*/
74486static SQLITE_NOINLINE i64 memIntValue(Mem *pMem){
74487 i64 value = 0;
74488 sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
74489 return value;
74490}
74491SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem *pMem){
74492 int flags;
74493 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
74494 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
74495 flags = pMem->flags;
74496 if( flags & MEM_Int ){
74497 return pMem->u.i;
74498 }else if( flags & MEM_Real ){
74499 return doubleToInt64(pMem->u.r);
74500 }else if( flags & (MEM_Str|MEM_Blob) ){
74501 assert( pMem->z || pMem->n==0 );
74502 return memIntValue(pMem);
74503 }else{
74504 return 0;
74505 }
74506}
74507
74508/*
74509** Return the best representation of pMem that we can get into a
74510** double. If pMem is already a double or an integer, return its
74511** value. If it is a string or blob, try to convert it to a double.
74512** If it is a NULL, return 0.0.
74513*/
74514static SQLITE_NOINLINE double memRealValue(Mem *pMem){
74515 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
74516 double val = (double)0;
74517 sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
74518 return val;
74519}
74520SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){
74521 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
74522 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
74523 if( pMem->flags & MEM_Real ){
74524 return pMem->u.r;
74525 }else if( pMem->flags & MEM_Int ){
74526 return (double)pMem->u.i;
74527 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
74528 return memRealValue(pMem);
74529 }else{
74530 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
74531 return (double)0;
74532 }
74533}
74534
74535/*
74536** Return 1 if pMem represents true, and return 0 if pMem represents false.
74537** Return the value ifNull if pMem is NULL.
74538*/
74539SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem *pMem, int ifNull){
74540 if( pMem->flags & MEM_Int ) return pMem->u.i!=0;
74541 if( pMem->flags & MEM_Null ) return ifNull;
74542 return sqlite3VdbeRealValue(pMem)!=0.0;
74543}
74544
74545/*
74546** The MEM structure is already a MEM_Real. Try to also make it a
74547** MEM_Int if we can.
74548*/
74549SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
74550 i64 ix;
74551 assert( pMem->flags & MEM_Real );
74552 assert( !sqlite3VdbeMemIsRowSet(pMem) );
74553 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
74554 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
74555
74556 ix = doubleToInt64(pMem->u.r);
74557
74558 /* Only mark the value as an integer if
74559 **
74560 ** (1) the round-trip conversion real->int->real is a no-op, and
74561 ** (2) The integer is neither the largest nor the smallest
74562 ** possible integer (ticket #3922)
74563 **
74564 ** The second and third terms in the following conditional enforces
74565 ** the second condition under the assumption that addition overflow causes
74566 ** values to wrap around.
74567 */
74568 if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
74569 pMem->u.i = ix;
74570 MemSetTypeFlag(pMem, MEM_Int);
74571 }
74572}
74573
74574/*
74575** Convert pMem to type integer. Invalidate any prior representations.
74576*/
74577SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem *pMem){
74578 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
74579 assert( !sqlite3VdbeMemIsRowSet(pMem) );
74580 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
74581
74582 pMem->u.i = sqlite3VdbeIntValue(pMem);
74583 MemSetTypeFlag(pMem, MEM_Int);
74584 return SQLITE_OK;
74585}
74586
74587/*
74588** Convert pMem so that it is of type MEM_Real.
74589** Invalidate any prior representations.
74590*/
74591SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem *pMem){
74592 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
74593 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
74594
74595 pMem->u.r = sqlite3VdbeRealValue(pMem);
74596 MemSetTypeFlag(pMem, MEM_Real);
74597 return SQLITE_OK;
74598}
74599
74600/* Compare a floating point value to an integer. Return true if the two
74601** values are the same within the precision of the floating point value.
74602**
74603** For some versions of GCC on 32-bit machines, if you do the more obvious
74604** comparison of "r1==(double)i" you sometimes get an answer of false even
74605** though the r1 and (double)i values are bit-for-bit the same.
74606*/
74607static int sqlite3RealSameAsInt(double r1, sqlite3_int64 i){
74608 double r2 = (double)i;
74609 return memcmp(&r1, &r2, sizeof(r1))==0;
74610}
74611
74612/*
74613** Convert pMem so that it has types MEM_Real or MEM_Int or both.
74614** Invalidate any prior representations.
74615**
74616** Every effort is made to force the conversion, even if the input
74617** is a string that does not look completely like a number. Convert
74618** as much of the string as we can and ignore the rest.
74619*/
74620SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){
74621 if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
74622 int rc;
74623 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
74624 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
74625 rc = sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc);
74626 if( rc==0 ){
74627 MemSetTypeFlag(pMem, MEM_Int);
74628 }else{
74629 i64 i = pMem->u.i;
74630 sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
74631 if( rc==1 && sqlite3RealSameAsInt(pMem->u.r, i) ){
74632 pMem->u.i = i;
74633 MemSetTypeFlag(pMem, MEM_Int);
74634 }else{
74635 MemSetTypeFlag(pMem, MEM_Real);
74636 }
74637 }
74638 }
74639 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
74640 pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);
74641 return SQLITE_OK;
74642}
74643
74644/*
74645** Cast the datatype of the value in pMem according to the affinity
74646** "aff". Casting is different from applying affinity in that a cast
74647** is forced. In other words, the value is converted into the desired
74648** affinity even if that results in loss of data. This routine is
74649** used (for example) to implement the SQL "cast()" operator.
74650*/
74651SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){
74652 if( pMem->flags & MEM_Null ) return;
74653 switch( aff ){
74654 case SQLITE_AFF_BLOB: { /* Really a cast to BLOB */
74655 if( (pMem->flags & MEM_Blob)==0 ){
74656 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
74657 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
74658 if( pMem->flags & MEM_Str ) MemSetTypeFlag(pMem, MEM_Blob);
74659 }else{
74660 pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
74661 }
74662 break;
74663 }
74664 case SQLITE_AFF_NUMERIC: {
74665 sqlite3VdbeMemNumerify(pMem);
74666 break;
74667 }
74668 case SQLITE_AFF_INTEGER: {
74669 sqlite3VdbeMemIntegerify(pMem);
74670 break;
74671 }
74672 case SQLITE_AFF_REAL: {
74673 sqlite3VdbeMemRealify(pMem);
74674 break;
74675 }
74676 default: {
74677 assert( aff==SQLITE_AFF_TEXT );
74678 assert( MEM_Str==(MEM_Blob>>3) );
74679 pMem->flags |= (pMem->flags&MEM_Blob)>>3;
74680 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
74681 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
74682 pMem->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
74683 break;
74684 }
74685 }
74686}
74687
74688/*
74689** Initialize bulk memory to be a consistent Mem object.
74690**
74691** The minimum amount of initialization feasible is performed.
74692*/
74693SQLITE_PRIVATE void sqlite3VdbeMemInit(Mem *pMem, sqlite3 *db, u16 flags){
74694 assert( (flags & ~MEM_TypeMask)==0 );
74695 pMem->flags = flags;
74696 pMem->db = db;
74697 pMem->szMalloc = 0;
74698}
74699
74700
74701/*
74702** Delete any previous value and set the value stored in *pMem to NULL.
74703**
74704** This routine calls the Mem.xDel destructor to dispose of values that
74705** require the destructor. But it preserves the Mem.zMalloc memory allocation.
74706** To free all resources, use sqlite3VdbeMemRelease(), which both calls this
74707** routine to invoke the destructor and deallocates Mem.zMalloc.
74708**
74709** Use this routine to reset the Mem prior to insert a new value.
74710**
74711** Use sqlite3VdbeMemRelease() to complete erase the Mem prior to abandoning it.
74712*/
74713SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem *pMem){
74714 if( VdbeMemDynamic(pMem) ){
74715 vdbeMemClearExternAndSetNull(pMem);
74716 }else{
74717 pMem->flags = MEM_Null;
74718 }
74719}
74720SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value *p){
74721 sqlite3VdbeMemSetNull((Mem*)p);
74722}
74723
74724/*
74725** Delete any previous value and set the value to be a BLOB of length
74726** n containing all zeros.
74727*/
74728SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
74729 sqlite3VdbeMemRelease(pMem);
74730 pMem->flags = MEM_Blob|MEM_Zero;
74731 pMem->n = 0;
74732 if( n<0 ) n = 0;
74733 pMem->u.nZero = n;
74734 pMem->enc = SQLITE_UTF8;
74735 pMem->z = 0;
74736}
74737
74738/*
74739** The pMem is known to contain content that needs to be destroyed prior
74740** to a value change. So invoke the destructor, then set the value to
74741** a 64-bit integer.
74742*/
74743static SQLITE_NOINLINE void vdbeReleaseAndSetInt64(Mem *pMem, i64 val){
74744 sqlite3VdbeMemSetNull(pMem);
74745 pMem->u.i = val;
74746 pMem->flags = MEM_Int;
74747}
74748
74749/*
74750** Delete any previous value and set the value stored in *pMem to val,
74751** manifest type INTEGER.
74752*/
74753SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
74754 if( VdbeMemDynamic(pMem) ){
74755 vdbeReleaseAndSetInt64(pMem, val);
74756 }else{
74757 pMem->u.i = val;
74758 pMem->flags = MEM_Int;
74759 }
74760}
74761
74762/* A no-op destructor */
74763SQLITE_PRIVATE void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p); }
74764
74765/*
74766** Set the value stored in *pMem should already be a NULL.
74767** Also store a pointer to go with it.
74768*/
74769SQLITE_PRIVATE void sqlite3VdbeMemSetPointer(
74770 Mem *pMem,
74771 void *pPtr,
74772 const char *zPType,
74773 void (*xDestructor)(void*)
74774){
74775 assert( pMem->flags==MEM_Null );
74776 pMem->u.zPType = zPType ? zPType : "";
74777 pMem->z = pPtr;
74778 pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term;
74779 pMem->eSubtype = 'p';
74780 pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor;
74781}
74782
74783#ifndef SQLITE_OMIT_FLOATING_POINT
74784/*
74785** Delete any previous value and set the value stored in *pMem to val,
74786** manifest type REAL.
74787*/
74788SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
74789 sqlite3VdbeMemSetNull(pMem);
74790 if( !sqlite3IsNaN(val) ){
74791 pMem->u.r = val;
74792 pMem->flags = MEM_Real;
74793 }
74794}
74795#endif
74796
74797#ifdef SQLITE_DEBUG
74798/*
74799** Return true if the Mem holds a RowSet object. This routine is intended
74800** for use inside of assert() statements.
74801*/
74802SQLITE_PRIVATE int sqlite3VdbeMemIsRowSet(const Mem *pMem){
74803 return (pMem->flags&(MEM_Blob|MEM_Dyn))==(MEM_Blob|MEM_Dyn)
74804 && pMem->xDel==sqlite3RowSetDelete;
74805}
74806#endif
74807
74808/*
74809** Delete any previous value and set the value of pMem to be an
74810** empty boolean index.
74811**
74812** Return SQLITE_OK on success and SQLITE_NOMEM if a memory allocation
74813** error occurs.
74814*/
74815SQLITE_PRIVATE int sqlite3VdbeMemSetRowSet(Mem *pMem){
74816 sqlite3 *db = pMem->db;
74817 RowSet *p;
74818 assert( db!=0 );
74819 assert( !sqlite3VdbeMemIsRowSet(pMem) );
74820 sqlite3VdbeMemRelease(pMem);
74821 p = sqlite3RowSetInit(db);
74822 if( p==0 ) return SQLITE_NOMEM;
74823 pMem->z = (char*)p;
74824 pMem->flags = MEM_Blob|MEM_Dyn;
74825 pMem->xDel = sqlite3RowSetDelete;
74826 return SQLITE_OK;
74827}
74828
74829/*
74830** Return true if the Mem object contains a TEXT or BLOB that is
74831** too large - whose size exceeds SQLITE_MAX_LENGTH.
74832*/
74833SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem *p){
74834 assert( p->db!=0 );
74835 if( p->flags & (MEM_Str|MEM_Blob) ){
74836 int n = p->n;
74837 if( p->flags & MEM_Zero ){
74838 n += p->u.nZero;
74839 }
74840 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
74841 }
74842 return 0;
74843}
74844
74845#ifdef SQLITE_DEBUG
74846/*
74847** This routine prepares a memory cell for modification by breaking
74848** its link to a shallow copy and by marking any current shallow
74849** copies of this cell as invalid.
74850**
74851** This is used for testing and debugging only - to make sure shallow
74852** copies are not misused.
74853*/
74854SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
74855 int i;
74856 Mem *pX;
74857 for(i=0, pX=pVdbe->aMem; i<pVdbe->nMem; i++, pX++){
74858 if( pX->pScopyFrom==pMem ){
74859 /* If pX is marked as a shallow copy of pMem, then verify that
74860 ** no significant changes have been made to pX since the OP_SCopy.
74861 ** A significant change would indicated a missed call to this
74862 ** function for pX. Minor changes, such as adding or removing a
74863 ** dual type, are allowed, as long as the underlying value is the
74864 ** same. */
74865 u16 mFlags = pMem->flags & pX->flags & pX->mScopyFlags;
74866 assert( (mFlags&MEM_Int)==0 || pMem->u.i==pX->u.i );
74867 assert( (mFlags&MEM_Real)==0 || pMem->u.r==pX->u.r );
74868 assert( (mFlags&MEM_Str)==0 || (pMem->n==pX->n && pMem->z==pX->z) );
74869 assert( (mFlags&MEM_Blob)==0 || sqlite3BlobCompare(pMem,pX)==0 );
74870
74871 /* pMem is the register that is changing. But also mark pX as
74872 ** undefined so that we can quickly detect the shallow-copy error */
74873 pX->flags = MEM_Undefined;
74874 pX->pScopyFrom = 0;
74875 }
74876 }
74877 pMem->pScopyFrom = 0;
74878}
74879#endif /* SQLITE_DEBUG */
74880
74881
74882/*
74883** Make an shallow copy of pFrom into pTo. Prior contents of
74884** pTo are freed. The pFrom->z field is not duplicated. If
74885** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
74886** and flags gets srcType (either MEM_Ephem or MEM_Static).
74887*/
74888static SQLITE_NOINLINE void vdbeClrCopy(Mem *pTo, const Mem *pFrom, int eType){
74889 vdbeMemClearExternAndSetNull(pTo);
74890 assert( !VdbeMemDynamic(pTo) );
74891 sqlite3VdbeMemShallowCopy(pTo, pFrom, eType);
74892}
74893SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
74894 assert( !sqlite3VdbeMemIsRowSet(pFrom) );
74895 assert( pTo->db==pFrom->db );
74896 if( VdbeMemDynamic(pTo) ){ vdbeClrCopy(pTo,pFrom,srcType); return; }
74897 memcpy(pTo, pFrom, MEMCELLSIZE);
74898 if( (pFrom->flags&MEM_Static)==0 ){
74899 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
74900 assert( srcType==MEM_Ephem || srcType==MEM_Static );
74901 pTo->flags |= srcType;
74902 }
74903}
74904
74905/*
74906** Make a full copy of pFrom into pTo. Prior contents of pTo are
74907** freed before the copy is made.
74908*/
74909SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
74910 int rc = SQLITE_OK;
74911
74912 assert( !sqlite3VdbeMemIsRowSet(pFrom) );
74913 if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo);
74914 memcpy(pTo, pFrom, MEMCELLSIZE);
74915 pTo->flags &= ~MEM_Dyn;
74916 if( pTo->flags&(MEM_Str|MEM_Blob) ){
74917 if( 0==(pFrom->flags&MEM_Static) ){
74918 pTo->flags |= MEM_Ephem;
74919 rc = sqlite3VdbeMemMakeWriteable(pTo);
74920 }
74921 }
74922
74923 return rc;
74924}
74925
74926/*
74927** Transfer the contents of pFrom to pTo. Any existing value in pTo is
74928** freed. If pFrom contains ephemeral data, a copy is made.
74929**
74930** pFrom contains an SQL NULL when this routine returns.
74931*/
74932SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
74933 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
74934 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
74935 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
74936
74937 sqlite3VdbeMemRelease(pTo);
74938 memcpy(pTo, pFrom, sizeof(Mem));
74939 pFrom->flags = MEM_Null;
74940 pFrom->szMalloc = 0;
74941}
74942
74943/*
74944** Change the value of a Mem to be a string or a BLOB.
74945**
74946** The memory management strategy depends on the value of the xDel
74947** parameter. If the value passed is SQLITE_TRANSIENT, then the
74948** string is copied into a (possibly existing) buffer managed by the
74949** Mem structure. Otherwise, any existing buffer is freed and the
74950** pointer copied.
74951**
74952** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
74953** size limit) then no memory allocation occurs. If the string can be
74954** stored without allocating memory, then it is. If a memory allocation
74955** is required to store the string, then value of pMem is unchanged. In
74956** either case, SQLITE_TOOBIG is returned.
74957*/
74958SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
74959 Mem *pMem, /* Memory cell to set to string value */
74960 const char *z, /* String pointer */
74961 int n, /* Bytes in string, or negative */
74962 u8 enc, /* Encoding of z. 0 for BLOBs */
74963 void (*xDel)(void*) /* Destructor function */
74964){
74965 int nByte = n; /* New value for pMem->n */
74966 int iLimit; /* Maximum allowed string or blob size */
74967 u16 flags = 0; /* New value for pMem->flags */
74968
74969 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
74970 assert( !sqlite3VdbeMemIsRowSet(pMem) );
74971
74972 /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
74973 if( !z ){
74974 sqlite3VdbeMemSetNull(pMem);
74975 return SQLITE_OK;
74976 }
74977
74978 if( pMem->db ){
74979 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
74980 }else{
74981 iLimit = SQLITE_MAX_LENGTH;
74982 }
74983 flags = (enc==0?MEM_Blob:MEM_Str);
74984 if( nByte<0 ){
74985 assert( enc!=0 );
74986 if( enc==SQLITE_UTF8 ){
74987 nByte = 0x7fffffff & (int)strlen(z);
74988 if( nByte>iLimit ) nByte = iLimit+1;
74989 }else{
74990 for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
74991 }
74992 flags |= MEM_Term;
74993 }
74994
74995 /* The following block sets the new values of Mem.z and Mem.xDel. It
74996 ** also sets a flag in local variable "flags" to indicate the memory
74997 ** management (one of MEM_Dyn or MEM_Static).
74998 */
74999 if( xDel==SQLITE_TRANSIENT ){
75000 int nAlloc = nByte;
75001 if( flags&MEM_Term ){
75002 nAlloc += (enc==SQLITE_UTF8?1:2);
75003 }
75004 if( nByte>iLimit ){
75005 return SQLITE_TOOBIG;
75006 }
75007 testcase( nAlloc==0 );
75008 testcase( nAlloc==31 );
75009 testcase( nAlloc==32 );
75010 if( sqlite3VdbeMemClearAndResize(pMem, MAX(nAlloc,32)) ){
75011 return SQLITE_NOMEM_BKPT;
75012 }
75013 memcpy(pMem->z, z, nAlloc);
75014 }else if( xDel==SQLITE_DYNAMIC ){
75015 sqlite3VdbeMemRelease(pMem);
75016 pMem->zMalloc = pMem->z = (char *)z;
75017 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
75018 }else{
75019 sqlite3VdbeMemRelease(pMem);
75020 pMem->z = (char *)z;
75021 pMem->xDel = xDel;
75022 flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
75023 }
75024
75025 pMem->n = nByte;
75026 pMem->flags = flags;
75027 pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
75028
75029#ifndef SQLITE_OMIT_UTF16
75030 if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
75031 return SQLITE_NOMEM_BKPT;
75032 }
75033#endif
75034
75035 if( nByte>iLimit ){
75036 return SQLITE_TOOBIG;
75037 }
75038
75039 return SQLITE_OK;
75040}
75041
75042/*
75043** Move data out of a btree key or data field and into a Mem structure.
75044** The data is payload from the entry that pCur is currently pointing
75045** to. offset and amt determine what portion of the data or key to retrieve.
75046** The result is written into the pMem element.
75047**
75048** The pMem object must have been initialized. This routine will use
75049** pMem->zMalloc to hold the content from the btree, if possible. New
75050** pMem->zMalloc space will be allocated if necessary. The calling routine
75051** is responsible for making sure that the pMem object is eventually
75052** destroyed.
75053**
75054** If this routine fails for any reason (malloc returns NULL or unable
75055** to read from the disk) then the pMem is left in an inconsistent state.
75056*/
75057static SQLITE_NOINLINE int vdbeMemFromBtreeResize(
75058 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
75059 u32 offset, /* Offset from the start of data to return bytes from. */
75060 u32 amt, /* Number of bytes to return. */
75061 Mem *pMem /* OUT: Return data in this Mem structure. */
75062){
75063 int rc;
75064 pMem->flags = MEM_Null;
75065 if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+1)) ){
75066 rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
75067 if( rc==SQLITE_OK ){
75068 pMem->z[amt] = 0; /* Overrun area used when reading malformed records */
75069 pMem->flags = MEM_Blob;
75070 pMem->n = (int)amt;
75071 }else{
75072 sqlite3VdbeMemRelease(pMem);
75073 }
75074 }
75075 return rc;
75076}
75077SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
75078 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
75079 u32 offset, /* Offset from the start of data to return bytes from. */
75080 u32 amt, /* Number of bytes to return. */
75081 Mem *pMem /* OUT: Return data in this Mem structure. */
75082){
75083 char *zData; /* Data from the btree layer */
75084 u32 available = 0; /* Number of bytes available on the local btree page */
75085 int rc = SQLITE_OK; /* Return code */
75086
75087 assert( sqlite3BtreeCursorIsValid(pCur) );
75088 assert( !VdbeMemDynamic(pMem) );
75089
75090 /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert()
75091 ** that both the BtShared and database handle mutexes are held. */
75092 assert( !sqlite3VdbeMemIsRowSet(pMem) );
75093 zData = (char *)sqlite3BtreePayloadFetch(pCur, &available);
75094 assert( zData!=0 );
75095
75096 if( offset+amt<=available ){
75097 pMem->z = &zData[offset];
75098 pMem->flags = MEM_Blob|MEM_Ephem;
75099 pMem->n = (int)amt;
75100 }else{
75101 rc = vdbeMemFromBtreeResize(pCur, offset, amt, pMem);
75102 }
75103
75104 return rc;
75105}
75106
75107/*
75108** The pVal argument is known to be a value other than NULL.
75109** Convert it into a string with encoding enc and return a pointer
75110** to a zero-terminated version of that string.
75111*/
75112static SQLITE_NOINLINE const void *valueToText(sqlite3_value* pVal, u8 enc){
75113 assert( pVal!=0 );
75114 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
75115 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
75116 assert( !sqlite3VdbeMemIsRowSet(pVal) );
75117 assert( (pVal->flags & (MEM_Null))==0 );
75118 if( pVal->flags & (MEM_Blob|MEM_Str) ){
75119 if( ExpandBlob(pVal) ) return 0;
75120 pVal->flags |= MEM_Str;
75121 if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
75122 sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
75123 }
75124 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
75125 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
75126 if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
75127 return 0;
75128 }
75129 }
75130 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
75131 }else{
75132 sqlite3VdbeMemStringify(pVal, enc, 0);
75133 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
75134 }
75135 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
75136 || pVal->db->mallocFailed );
75137 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
75138 assert( sqlite3VdbeMemConsistentDualRep(pVal) );
75139 return pVal->z;
75140 }else{
75141 return 0;
75142 }
75143}
75144
75145/* This function is only available internally, it is not part of the
75146** external API. It works in a similar way to sqlite3_value_text(),
75147** except the data returned is in the encoding specified by the second
75148** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
75149** SQLITE_UTF8.
75150**
75151** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
75152** If that is the case, then the result must be aligned on an even byte
75153** boundary.
75154*/
75155SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
75156 if( !pVal ) return 0;
75157 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
75158 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
75159 assert( !sqlite3VdbeMemIsRowSet(pVal) );
75160 if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
75161 assert( sqlite3VdbeMemConsistentDualRep(pVal) );
75162 return pVal->z;
75163 }
75164 if( pVal->flags&MEM_Null ){
75165 return 0;
75166 }
75167 return valueToText(pVal, enc);
75168}
75169
75170/*
75171** Create a new sqlite3_value object.
75172*/
75173SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *db){
75174 Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
75175 if( p ){
75176 p->flags = MEM_Null;
75177 p->db = db;
75178 }
75179 return p;
75180}
75181
75182/*
75183** Context object passed by sqlite3Stat4ProbeSetValue() through to
75184** valueNew(). See comments above valueNew() for details.
75185*/
75186struct ValueNewStat4Ctx {
75187 Parse *pParse;
75188 Index *pIdx;
75189 UnpackedRecord **ppRec;
75190 int iVal;
75191};
75192
75193/*
75194** Allocate and return a pointer to a new sqlite3_value object. If
75195** the second argument to this function is NULL, the object is allocated
75196** by calling sqlite3ValueNew().
75197**
75198** Otherwise, if the second argument is non-zero, then this function is
75199** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
75200** already been allocated, allocate the UnpackedRecord structure that
75201** that function will return to its caller here. Then return a pointer to
75202** an sqlite3_value within the UnpackedRecord.a[] array.
75203*/
75204static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
75205#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
75206 if( p ){
75207 UnpackedRecord *pRec = p->ppRec[0];
75208
75209 if( pRec==0 ){
75210 Index *pIdx = p->pIdx; /* Index being probed */
75211 int nByte; /* Bytes of space to allocate */
75212 int i; /* Counter variable */
75213 int nCol = pIdx->nColumn; /* Number of index columns including rowid */
75214
75215 nByte = sizeof(Mem) * nCol + ROUND8(sizeof(UnpackedRecord));
75216 pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte);
75217 if( pRec ){
75218 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
75219 if( pRec->pKeyInfo ){
75220 assert( pRec->pKeyInfo->nAllField==nCol );
75221 assert( pRec->pKeyInfo->enc==ENC(db) );
75222 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
75223 for(i=0; i<nCol; i++){
75224 pRec->aMem[i].flags = MEM_Null;
75225 pRec->aMem[i].db = db;
75226 }
75227 }else{
75228 sqlite3DbFreeNN(db, pRec);
75229 pRec = 0;
75230 }
75231 }
75232 if( pRec==0 ) return 0;
75233 p->ppRec[0] = pRec;
75234 }
75235
75236 pRec->nField = p->iVal+1;
75237 return &pRec->aMem[p->iVal];
75238 }
75239#else
75240 UNUSED_PARAMETER(p);
75241#endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
75242 return sqlite3ValueNew(db);
75243}
75244
75245/*
75246** The expression object indicated by the second argument is guaranteed
75247** to be a scalar SQL function. If
75248**
75249** * all function arguments are SQL literals,
75250** * one of the SQLITE_FUNC_CONSTANT or _SLOCHNG function flags is set, and
75251** * the SQLITE_FUNC_NEEDCOLL function flag is not set,
75252**
75253** then this routine attempts to invoke the SQL function. Assuming no
75254** error occurs, output parameter (*ppVal) is set to point to a value
75255** object containing the result before returning SQLITE_OK.
75256**
75257** Affinity aff is applied to the result of the function before returning.
75258** If the result is a text value, the sqlite3_value object uses encoding
75259** enc.
75260**
75261** If the conditions above are not met, this function returns SQLITE_OK
75262** and sets (*ppVal) to NULL. Or, if an error occurs, (*ppVal) is set to
75263** NULL and an SQLite error code returned.
75264*/
75265#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
75266static int valueFromFunction(
75267 sqlite3 *db, /* The database connection */
75268 Expr *p, /* The expression to evaluate */
75269 u8 enc, /* Encoding to use */
75270 u8 aff, /* Affinity to use */
75271 sqlite3_value **ppVal, /* Write the new value here */
75272 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
75273){
75274 sqlite3_context ctx; /* Context object for function invocation */
75275 sqlite3_value **apVal = 0; /* Function arguments */
75276 int nVal = 0; /* Size of apVal[] array */
75277 FuncDef *pFunc = 0; /* Function definition */
75278 sqlite3_value *pVal = 0; /* New value */
75279 int rc = SQLITE_OK; /* Return code */
75280 ExprList *pList = 0; /* Function arguments */
75281 int i; /* Iterator variable */
75282
75283 assert( pCtx!=0 );
75284 assert( (p->flags & EP_TokenOnly)==0 );
75285 pList = p->x.pList;
75286 if( pList ) nVal = pList->nExpr;
75287 pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0);
75288 assert( pFunc );
75289 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
75290 || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
75291 ){
75292 return SQLITE_OK;
75293 }
75294
75295 if( pList ){
75296 apVal = (sqlite3_value**)sqlite3DbMallocZero(db, sizeof(apVal[0]) * nVal);
75297 if( apVal==0 ){
75298 rc = SQLITE_NOMEM_BKPT;
75299 goto value_from_function_out;
75300 }
75301 for(i=0; i<nVal; i++){
75302 rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]);
75303 if( apVal[i]==0 || rc!=SQLITE_OK ) goto value_from_function_out;
75304 }
75305 }
75306
75307 pVal = valueNew(db, pCtx);
75308 if( pVal==0 ){
75309 rc = SQLITE_NOMEM_BKPT;
75310 goto value_from_function_out;
75311 }
75312
75313 assert( pCtx->pParse->rc==SQLITE_OK );
75314 memset(&ctx, 0, sizeof(ctx));
75315 ctx.pOut = pVal;
75316 ctx.pFunc = pFunc;
75317 pFunc->xSFunc(&ctx, nVal, apVal);
75318 if( ctx.isError ){
75319 rc = ctx.isError;
75320 sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
75321 }else{
75322 sqlite3ValueApplyAffinity(pVal, aff, SQLITE_UTF8);
75323 assert( rc==SQLITE_OK );
75324 rc = sqlite3VdbeChangeEncoding(pVal, enc);
75325 if( rc==SQLITE_OK && sqlite3VdbeMemTooBig(pVal) ){
75326 rc = SQLITE_TOOBIG;
75327 pCtx->pParse->nErr++;
75328 }
75329 }
75330 pCtx->pParse->rc = rc;
75331
75332 value_from_function_out:
75333 if( rc!=SQLITE_OK ){
75334 pVal = 0;
75335 }
75336 if( apVal ){
75337 for(i=0; i<nVal; i++){
75338 sqlite3ValueFree(apVal[i]);
75339 }
75340 sqlite3DbFreeNN(db, apVal);
75341 }
75342
75343 *ppVal = pVal;
75344 return rc;
75345}
75346#else
75347# define valueFromFunction(a,b,c,d,e,f) SQLITE_OK
75348#endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
75349
75350/*
75351** Extract a value from the supplied expression in the manner described
75352** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
75353** using valueNew().
75354**
75355** If pCtx is NULL and an error occurs after the sqlite3_value object
75356** has been allocated, it is freed before returning. Or, if pCtx is not
75357** NULL, it is assumed that the caller will free any allocated object
75358** in all cases.
75359*/
75360static int valueFromExpr(
75361 sqlite3 *db, /* The database connection */
75362 Expr *pExpr, /* The expression to evaluate */
75363 u8 enc, /* Encoding to use */
75364 u8 affinity, /* Affinity to use */
75365 sqlite3_value **ppVal, /* Write the new value here */
75366 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
75367){
75368 int op;
75369 char *zVal = 0;
75370 sqlite3_value *pVal = 0;
75371 int negInt = 1;
75372 const char *zNeg = "";
75373 int rc = SQLITE_OK;
75374
75375 assert( pExpr!=0 );
75376 while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
75377#if defined(SQLITE_ENABLE_STAT3_OR_STAT4)
75378 if( op==TK_REGISTER ) op = pExpr->op2;
75379#else
75380 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
75381#endif
75382
75383 /* Compressed expressions only appear when parsing the DEFAULT clause
75384 ** on a table column definition, and hence only when pCtx==0. This
75385 ** check ensures that an EP_TokenOnly expression is never passed down
75386 ** into valueFromFunction(). */
75387 assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
75388
75389 if( op==TK_CAST ){
75390 u8 aff = sqlite3AffinityType(pExpr->u.zToken,0);
75391 rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
75392 testcase( rc!=SQLITE_OK );
75393 if( *ppVal ){
75394 sqlite3VdbeMemCast(*ppVal, aff, SQLITE_UTF8);
75395 sqlite3ValueApplyAffinity(*ppVal, affinity, SQLITE_UTF8);
75396 }
75397 return rc;
75398 }
75399
75400 /* Handle negative integers in a single step. This is needed in the
75401 ** case when the value is -9223372036854775808.
75402 */
75403 if( op==TK_UMINUS
75404 && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
75405 pExpr = pExpr->pLeft;
75406 op = pExpr->op;
75407 negInt = -1;
75408 zNeg = "-";
75409 }
75410
75411 if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
75412 pVal = valueNew(db, pCtx);
75413 if( pVal==0 ) goto no_mem;
75414 if( ExprHasProperty(pExpr, EP_IntValue) ){
75415 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
75416 }else{
75417 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
75418 if( zVal==0 ) goto no_mem;
75419 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
75420 }
75421 if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_BLOB ){
75422 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
75423 }else{
75424 sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
75425 }
75426 if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
75427 if( enc!=SQLITE_UTF8 ){
75428 rc = sqlite3VdbeChangeEncoding(pVal, enc);
75429 }
75430 }else if( op==TK_UMINUS ) {
75431 /* This branch happens for multiple negative signs. Ex: -(-5) */
75432 if( SQLITE_OK==valueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal,pCtx)
75433 && pVal!=0
75434 ){
75435 sqlite3VdbeMemNumerify(pVal);
75436 if( pVal->flags & MEM_Real ){
75437 pVal->u.r = -pVal->u.r;
75438 }else if( pVal->u.i==SMALLEST_INT64 ){
75439 pVal->u.r = -(double)SMALLEST_INT64;
75440 MemSetTypeFlag(pVal, MEM_Real);
75441 }else{
75442 pVal->u.i = -pVal->u.i;
75443 }
75444 sqlite3ValueApplyAffinity(pVal, affinity, enc);
75445 }
75446 }else if( op==TK_NULL ){
75447 pVal = valueNew(db, pCtx);
75448 if( pVal==0 ) goto no_mem;
75449 sqlite3VdbeMemNumerify(pVal);
75450 }
75451#ifndef SQLITE_OMIT_BLOB_LITERAL
75452 else if( op==TK_BLOB ){
75453 int nVal;
75454 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
75455 assert( pExpr->u.zToken[1]=='\'' );
75456 pVal = valueNew(db, pCtx);
75457 if( !pVal ) goto no_mem;
75458 zVal = &pExpr->u.zToken[2];
75459 nVal = sqlite3Strlen30(zVal)-1;
75460 assert( zVal[nVal]=='\'' );
75461 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
75462 0, SQLITE_DYNAMIC);
75463 }
75464#endif
75465#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
75466 else if( op==TK_FUNCTION && pCtx!=0 ){
75467 rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx);
75468 }
75469#endif
75470 else if( op==TK_TRUEFALSE ){
75471 pVal = valueNew(db, pCtx);
75472 pVal->flags = MEM_Int;
75473 pVal->u.i = pExpr->u.zToken[4]==0;
75474 }
75475
75476 *ppVal = pVal;
75477 return rc;
75478
75479no_mem:
75480#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
75481 if( pCtx==0 || pCtx->pParse->nErr==0 )
75482#endif
75483 sqlite3OomFault(db);
75484 sqlite3DbFree(db, zVal);
75485 assert( *ppVal==0 );
75486#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
75487 if( pCtx==0 ) sqlite3ValueFree(pVal);
75488#else
75489 assert( pCtx==0 ); sqlite3ValueFree(pVal);
75490#endif
75491 return SQLITE_NOMEM_BKPT;
75492}
75493
75494/*
75495** Create a new sqlite3_value object, containing the value of pExpr.
75496**
75497** This only works for very simple expressions that consist of one constant
75498** token (i.e. "5", "5.1", "'a string'"). If the expression can
75499** be converted directly into a value, then the value is allocated and
75500** a pointer written to *ppVal. The caller is responsible for deallocating
75501** the value by passing it to sqlite3ValueFree() later on. If the expression
75502** cannot be converted to a value, then *ppVal is set to NULL.
75503*/
75504SQLITE_PRIVATE int sqlite3ValueFromExpr(
75505 sqlite3 *db, /* The database connection */
75506 Expr *pExpr, /* The expression to evaluate */
75507 u8 enc, /* Encoding to use */
75508 u8 affinity, /* Affinity to use */
75509 sqlite3_value **ppVal /* Write the new value here */
75510){
75511 return pExpr ? valueFromExpr(db, pExpr, enc, affinity, ppVal, 0) : 0;
75512}
75513
75514#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
75515/*
75516** The implementation of the sqlite_record() function. This function accepts
75517** a single argument of any type. The return value is a formatted database
75518** record (a blob) containing the argument value.
75519**
75520** This is used to convert the value stored in the 'sample' column of the
75521** sqlite_stat3 table to the record format SQLite uses internally.
75522*/
75523static void recordFunc(
75524 sqlite3_context *context,
75525 int argc,
75526 sqlite3_value **argv
75527){
75528 const int file_format = 1;
75529 u32 iSerial; /* Serial type */
75530 int nSerial; /* Bytes of space for iSerial as varint */
75531 u32 nVal; /* Bytes of space required for argv[0] */
75532 int nRet;
75533 sqlite3 *db;
75534 u8 *aRet;
75535
75536 UNUSED_PARAMETER( argc );
75537 iSerial = sqlite3VdbeSerialType(argv[0], file_format, &nVal);
75538 nSerial = sqlite3VarintLen(iSerial);
75539 db = sqlite3_context_db_handle(context);
75540
75541 nRet = 1 + nSerial + nVal;
75542 aRet = sqlite3DbMallocRawNN(db, nRet);
75543 if( aRet==0 ){
75544 sqlite3_result_error_nomem(context);
75545 }else{
75546 aRet[0] = nSerial+1;
75547 putVarint32(&aRet[1], iSerial);
75548 sqlite3VdbeSerialPut(&aRet[1+nSerial], argv[0], iSerial);
75549 sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT);
75550 sqlite3DbFreeNN(db, aRet);
75551 }
75552}
75553
75554/*
75555** Register built-in functions used to help read ANALYZE data.
75556*/
75557SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void){
75558 static FuncDef aAnalyzeTableFuncs[] = {
75559 FUNCTION(sqlite_record, 1, 0, 0, recordFunc),
75560 };
75561 sqlite3InsertBuiltinFuncs(aAnalyzeTableFuncs, ArraySize(aAnalyzeTableFuncs));
75562}
75563
75564/*
75565** Attempt to extract a value from pExpr and use it to construct *ppVal.
75566**
75567** If pAlloc is not NULL, then an UnpackedRecord object is created for
75568** pAlloc if one does not exist and the new value is added to the
75569** UnpackedRecord object.
75570**
75571** A value is extracted in the following cases:
75572**
75573** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
75574**
75575** * The expression is a bound variable, and this is a reprepare, or
75576**
75577** * The expression is a literal value.
75578**
75579** On success, *ppVal is made to point to the extracted value. The caller
75580** is responsible for ensuring that the value is eventually freed.
75581*/
75582static int stat4ValueFromExpr(
75583 Parse *pParse, /* Parse context */
75584 Expr *pExpr, /* The expression to extract a value from */
75585 u8 affinity, /* Affinity to use */
75586 struct ValueNewStat4Ctx *pAlloc,/* How to allocate space. Or NULL */
75587 sqlite3_value **ppVal /* OUT: New value object (or NULL) */
75588){
75589 int rc = SQLITE_OK;
75590 sqlite3_value *pVal = 0;
75591 sqlite3 *db = pParse->db;
75592
75593 /* Skip over any TK_COLLATE nodes */
75594 pExpr = sqlite3ExprSkipCollate(pExpr);
75595
75596 assert( pExpr==0 || pExpr->op!=TK_REGISTER || pExpr->op2!=TK_VARIABLE );
75597 if( !pExpr ){
75598 pVal = valueNew(db, pAlloc);
75599 if( pVal ){
75600 sqlite3VdbeMemSetNull((Mem*)pVal);
75601 }
75602 }else if( pExpr->op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
75603 Vdbe *v;
75604 int iBindVar = pExpr->iColumn;
75605 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
75606 if( (v = pParse->pReprepare)!=0 ){
75607 pVal = valueNew(db, pAlloc);
75608 if( pVal ){
75609 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
75610 sqlite3ValueApplyAffinity(pVal, affinity, ENC(db));
75611 pVal->db = pParse->db;
75612 }
75613 }
75614 }else{
75615 rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, pAlloc);
75616 }
75617
75618 assert( pVal==0 || pVal->db==db );
75619 *ppVal = pVal;
75620 return rc;
75621}
75622
75623/*
75624** This function is used to allocate and populate UnpackedRecord
75625** structures intended to be compared against sample index keys stored
75626** in the sqlite_stat4 table.
75627**
75628** A single call to this function populates zero or more fields of the
75629** record starting with field iVal (fields are numbered from left to
75630** right starting with 0). A single field is populated if:
75631**
75632** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
75633**
75634** * The expression is a bound variable, and this is a reprepare, or
75635**
75636** * The sqlite3ValueFromExpr() function is able to extract a value
75637** from the expression (i.e. the expression is a literal value).
75638**
75639** Or, if pExpr is a TK_VECTOR, one field is populated for each of the
75640** vector components that match either of the two latter criteria listed
75641** above.
75642**
75643** Before any value is appended to the record, the affinity of the
75644** corresponding column within index pIdx is applied to it. Before
75645** this function returns, output parameter *pnExtract is set to the
75646** number of values appended to the record.
75647**
75648** When this function is called, *ppRec must either point to an object
75649** allocated by an earlier call to this function, or must be NULL. If it
75650** is NULL and a value can be successfully extracted, a new UnpackedRecord
75651** is allocated (and *ppRec set to point to it) before returning.
75652**
75653** Unless an error is encountered, SQLITE_OK is returned. It is not an
75654** error if a value cannot be extracted from pExpr. If an error does
75655** occur, an SQLite error code is returned.
75656*/
75657SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
75658 Parse *pParse, /* Parse context */
75659 Index *pIdx, /* Index being probed */
75660 UnpackedRecord **ppRec, /* IN/OUT: Probe record */
75661 Expr *pExpr, /* The expression to extract a value from */
75662 int nElem, /* Maximum number of values to append */
75663 int iVal, /* Array element to populate */
75664 int *pnExtract /* OUT: Values appended to the record */
75665){
75666 int rc = SQLITE_OK;
75667 int nExtract = 0;
75668
75669 if( pExpr==0 || pExpr->op!=TK_SELECT ){
75670 int i;
75671 struct ValueNewStat4Ctx alloc;
75672
75673 alloc.pParse = pParse;
75674 alloc.pIdx = pIdx;
75675 alloc.ppRec = ppRec;
75676
75677 for(i=0; i<nElem; i++){
75678 sqlite3_value *pVal = 0;
75679 Expr *pElem = (pExpr ? sqlite3VectorFieldSubexpr(pExpr, i) : 0);
75680 u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
75681 alloc.iVal = iVal+i;
75682 rc = stat4ValueFromExpr(pParse, pElem, aff, &alloc, &pVal);
75683 if( !pVal ) break;
75684 nExtract++;
75685 }
75686 }
75687
75688 *pnExtract = nExtract;
75689 return rc;
75690}
75691
75692/*
75693** Attempt to extract a value from expression pExpr using the methods
75694** as described for sqlite3Stat4ProbeSetValue() above.
75695**
75696** If successful, set *ppVal to point to a new value object and return
75697** SQLITE_OK. If no value can be extracted, but no other error occurs
75698** (e.g. OOM), return SQLITE_OK and set *ppVal to NULL. Or, if an error
75699** does occur, return an SQLite error code. The final value of *ppVal
75700** is undefined in this case.
75701*/
75702SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(
75703 Parse *pParse, /* Parse context */
75704 Expr *pExpr, /* The expression to extract a value from */
75705 u8 affinity, /* Affinity to use */
75706 sqlite3_value **ppVal /* OUT: New value object (or NULL) */
75707){
75708 return stat4ValueFromExpr(pParse, pExpr, affinity, 0, ppVal);
75709}
75710
75711/*
75712** Extract the iCol-th column from the nRec-byte record in pRec. Write
75713** the column value into *ppVal. If *ppVal is initially NULL then a new
75714** sqlite3_value object is allocated.
75715**
75716** If *ppVal is initially NULL then the caller is responsible for
75717** ensuring that the value written into *ppVal is eventually freed.
75718*/
75719SQLITE_PRIVATE int sqlite3Stat4Column(
75720 sqlite3 *db, /* Database handle */
75721 const void *pRec, /* Pointer to buffer containing record */
75722 int nRec, /* Size of buffer pRec in bytes */
75723 int iCol, /* Column to extract */
75724 sqlite3_value **ppVal /* OUT: Extracted value */
75725){
75726 u32 t = 0; /* a column type code */
75727 int nHdr; /* Size of the header in the record */
75728 int iHdr; /* Next unread header byte */
75729 int iField; /* Next unread data byte */
75730 int szField = 0; /* Size of the current data field */
75731 int i; /* Column index */
75732 u8 *a = (u8*)pRec; /* Typecast byte array */
75733 Mem *pMem = *ppVal; /* Write result into this Mem object */
75734
75735 assert( iCol>0 );
75736 iHdr = getVarint32(a, nHdr);
75737 if( nHdr>nRec || iHdr>=nHdr ) return SQLITE_CORRUPT_BKPT;
75738 iField = nHdr;
75739 for(i=0; i<=iCol; i++){
75740 iHdr += getVarint32(&a[iHdr], t);
75741 testcase( iHdr==nHdr );
75742 testcase( iHdr==nHdr+1 );
75743 if( iHdr>nHdr ) return SQLITE_CORRUPT_BKPT;
75744 szField = sqlite3VdbeSerialTypeLen(t);
75745 iField += szField;
75746 }
75747 testcase( iField==nRec );
75748 testcase( iField==nRec+1 );
75749 if( iField>nRec ) return SQLITE_CORRUPT_BKPT;
75750 if( pMem==0 ){
75751 pMem = *ppVal = sqlite3ValueNew(db);
75752 if( pMem==0 ) return SQLITE_NOMEM_BKPT;
75753 }
75754 sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
75755 pMem->enc = ENC(db);
75756 return SQLITE_OK;
75757}
75758
75759/*
75760** Unless it is NULL, the argument must be an UnpackedRecord object returned
75761** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
75762** the object.
75763*/
75764SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
75765 if( pRec ){
75766 int i;
75767 int nCol = pRec->pKeyInfo->nAllField;
75768 Mem *aMem = pRec->aMem;
75769 sqlite3 *db = aMem[0].db;
75770 for(i=0; i<nCol; i++){
75771 sqlite3VdbeMemRelease(&aMem[i]);
75772 }
75773 sqlite3KeyInfoUnref(pRec->pKeyInfo);
75774 sqlite3DbFreeNN(db, pRec);
75775 }
75776}
75777#endif /* ifdef SQLITE_ENABLE_STAT4 */
75778
75779/*
75780** Change the string value of an sqlite3_value object
75781*/
75782SQLITE_PRIVATE void sqlite3ValueSetStr(
75783 sqlite3_value *v, /* Value to be set */
75784 int n, /* Length of string z */
75785 const void *z, /* Text of the new string */
75786 u8 enc, /* Encoding to use */
75787 void (*xDel)(void*) /* Destructor for the string */
75788){
75789 if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
75790}
75791
75792/*
75793** Free an sqlite3_value object
75794*/
75795SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value *v){
75796 if( !v ) return;
75797 sqlite3VdbeMemRelease((Mem *)v);
75798 sqlite3DbFreeNN(((Mem*)v)->db, v);
75799}
75800
75801/*
75802** The sqlite3ValueBytes() routine returns the number of bytes in the
75803** sqlite3_value object assuming that it uses the encoding "enc".
75804** The valueBytes() routine is a helper function.
75805*/
75806static SQLITE_NOINLINE int valueBytes(sqlite3_value *pVal, u8 enc){
75807 return valueToText(pVal, enc)!=0 ? pVal->n : 0;
75808}
75809SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
75810 Mem *p = (Mem*)pVal;
75811 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
75812 if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
75813 return p->n;
75814 }
75815 if( (p->flags & MEM_Blob)!=0 ){
75816 if( p->flags & MEM_Zero ){
75817 return p->n + p->u.nZero;
75818 }else{
75819 return p->n;
75820 }
75821 }
75822 if( p->flags & MEM_Null ) return 0;
75823 return valueBytes(pVal, enc);
75824}
75825
75826/************** End of vdbemem.c *********************************************/
75827/************** Begin file vdbeaux.c *****************************************/
75828/*
75829** 2003 September 6
75830**
75831** The author disclaims copyright to this source code. In place of
75832** a legal notice, here is a blessing:
75833**
75834** May you do good and not evil.
75835** May you find forgiveness for yourself and forgive others.
75836** May you share freely, never taking more than you give.
75837**
75838*************************************************************************
75839** This file contains code used for creating, destroying, and populating
75840** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)
75841*/
75842/* #include "sqliteInt.h" */
75843/* #include "vdbeInt.h" */
75844
75845/*
75846** Create a new virtual database engine.
75847*/
75848SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse *pParse){
75849 sqlite3 *db = pParse->db;
75850 Vdbe *p;
75851 p = sqlite3DbMallocRawNN(db, sizeof(Vdbe) );
75852 if( p==0 ) return 0;
75853 memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp));
75854 p->db = db;
75855 if( db->pVdbe ){
75856 db->pVdbe->pPrev = p;
75857 }
75858 p->pNext = db->pVdbe;
75859 p->pPrev = 0;
75860 db->pVdbe = p;
75861 p->magic = VDBE_MAGIC_INIT;
75862 p->pParse = pParse;
75863 pParse->pVdbe = p;
75864 assert( pParse->aLabel==0 );
75865 assert( pParse->nLabel==0 );
75866 assert( pParse->nOpAlloc==0 );
75867 assert( pParse->szOpAlloc==0 );
75868 sqlite3VdbeAddOp2(p, OP_Init, 0, 1);
75869 return p;
75870}
75871
75872/*
75873** Change the error string stored in Vdbe.zErrMsg
75874*/
75875SQLITE_PRIVATE void sqlite3VdbeError(Vdbe *p, const char *zFormat, ...){
75876 va_list ap;
75877 sqlite3DbFree(p->db, p->zErrMsg);
75878 va_start(ap, zFormat);
75879 p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
75880 va_end(ap);
75881}
75882
75883/*
75884** Remember the SQL string for a prepared statement.
75885*/
75886SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, u8 prepFlags){
75887 if( p==0 ) return;
75888 p->prepFlags = prepFlags;
75889 if( (prepFlags & SQLITE_PREPARE_SAVESQL)==0 ){
75890 p->expmask = 0;
75891 }
75892 assert( p->zSql==0 );
75893 p->zSql = sqlite3DbStrNDup(p->db, z, n);
75894#ifdef SQLITE_ENABLE_NORMALIZE
75895 assert( p->zNormSql==0 );
75896 if( p->zSql && (prepFlags & SQLITE_PREPARE_NORMALIZE)!=0 ){
75897 sqlite3Normalize(p, p->zSql, n, prepFlags);
75898 assert( p->zNormSql!=0 || p->db->mallocFailed );
75899 }
75900#endif
75901}
75902
75903/*
75904** Swap all content between two VDBE structures.
75905*/
75906SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
75907 Vdbe tmp, *pTmp;
75908 char *zTmp;
75909 assert( pA->db==pB->db );
75910 tmp = *pA;
75911 *pA = *pB;
75912 *pB = tmp;
75913 pTmp = pA->pNext;
75914 pA->pNext = pB->pNext;
75915 pB->pNext = pTmp;
75916 pTmp = pA->pPrev;
75917 pA->pPrev = pB->pPrev;
75918 pB->pPrev = pTmp;
75919 zTmp = pA->zSql;
75920 pA->zSql = pB->zSql;
75921 pB->zSql = zTmp;
75922#ifdef SQLITE_ENABLE_NORMALIZE
75923 zTmp = pA->zNormSql;
75924 pA->zNormSql = pB->zNormSql;
75925 pB->zNormSql = zTmp;
75926#endif
75927 pB->expmask = pA->expmask;
75928 pB->prepFlags = pA->prepFlags;
75929 memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter));
75930 pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++;
75931}
75932
75933/*
75934** Resize the Vdbe.aOp array so that it is at least nOp elements larger
75935** than its current size. nOp is guaranteed to be less than or equal
75936** to 1024/sizeof(Op).
75937**
75938** If an out-of-memory error occurs while resizing the array, return
75939** SQLITE_NOMEM. In this case Vdbe.aOp and Parse.nOpAlloc remain
75940** unchanged (this is so that any opcodes already allocated can be
75941** correctly deallocated along with the rest of the Vdbe).
75942*/
75943static int growOpArray(Vdbe *v, int nOp){
75944 VdbeOp *pNew;
75945 Parse *p = v->pParse;
75946
75947 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
75948 ** more frequent reallocs and hence provide more opportunities for
75949 ** simulated OOM faults. SQLITE_TEST_REALLOC_STRESS is generally used
75950 ** during testing only. With SQLITE_TEST_REALLOC_STRESS grow the op array
75951 ** by the minimum* amount required until the size reaches 512. Normal
75952 ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current
75953 ** size of the op array or add 1KB of space, whichever is smaller. */
75954#ifdef SQLITE_TEST_REALLOC_STRESS
75955 int nNew = (p->nOpAlloc>=512 ? p->nOpAlloc*2 : p->nOpAlloc+nOp);
75956#else
75957 int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
75958 UNUSED_PARAMETER(nOp);
75959#endif
75960
75961 /* Ensure that the size of a VDBE does not grow too large */
75962 if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){
75963 sqlite3OomFault(p->db);
75964 return SQLITE_NOMEM;
75965 }
75966
75967 assert( nOp<=(1024/sizeof(Op)) );
75968 assert( nNew>=(p->nOpAlloc+nOp) );
75969 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
75970 if( pNew ){
75971 p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
75972 p->nOpAlloc = p->szOpAlloc/sizeof(Op);
75973 v->aOp = pNew;
75974 }
75975 return (pNew ? SQLITE_OK : SQLITE_NOMEM_BKPT);
75976}
75977
75978#ifdef SQLITE_DEBUG
75979/* This routine is just a convenient place to set a breakpoint that will
75980** fire after each opcode is inserted and displayed using
75981** "PRAGMA vdbe_addoptrace=on".
75982*/
75983static void test_addop_breakpoint(void){
75984 static int n = 0;
75985 n++;
75986}
75987#endif
75988
75989/*
75990** Add a new instruction to the list of instructions current in the
75991** VDBE. Return the address of the new instruction.
75992**
75993** Parameters:
75994**
75995** p Pointer to the VDBE
75996**
75997** op The opcode for this instruction
75998**
75999** p1, p2, p3 Operands
76000**
76001** Use the sqlite3VdbeResolveLabel() function to fix an address and
76002** the sqlite3VdbeChangeP4() function to change the value of the P4
76003** operand.
76004*/
76005static SQLITE_NOINLINE int growOp3(Vdbe *p, int op, int p1, int p2, int p3){
76006 assert( p->pParse->nOpAlloc<=p->nOp );
76007 if( growOpArray(p, 1) ) return 1;
76008 assert( p->pParse->nOpAlloc>p->nOp );
76009 return sqlite3VdbeAddOp3(p, op, p1, p2, p3);
76010}
76011SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
76012 int i;
76013 VdbeOp *pOp;
76014
76015 i = p->nOp;
76016 assert( p->magic==VDBE_MAGIC_INIT );
76017 assert( op>=0 && op<0xff );
76018 if( p->pParse->nOpAlloc<=i ){
76019 return growOp3(p, op, p1, p2, p3);
76020 }
76021 p->nOp++;
76022 pOp = &p->aOp[i];
76023 pOp->opcode = (u8)op;
76024 pOp->p5 = 0;
76025 pOp->p1 = p1;
76026 pOp->p2 = p2;
76027 pOp->p3 = p3;
76028 pOp->p4.p = 0;
76029 pOp->p4type = P4_NOTUSED;
76030#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
76031 pOp->zComment = 0;
76032#endif
76033#ifdef SQLITE_DEBUG
76034 if( p->db->flags & SQLITE_VdbeAddopTrace ){
76035 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
76036 test_addop_breakpoint();
76037 }
76038#endif
76039#ifdef VDBE_PROFILE
76040 pOp->cycles = 0;
76041 pOp->cnt = 0;
76042#endif
76043#ifdef SQLITE_VDBE_COVERAGE
76044 pOp->iSrcLine = 0;
76045#endif
76046 return i;
76047}
76048SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe *p, int op){
76049 return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
76050}
76051SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
76052 return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
76053}
76054SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
76055 return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
76056}
76057
76058/* Generate code for an unconditional jump to instruction iDest
76059*/
76060SQLITE_PRIVATE int sqlite3VdbeGoto(Vdbe *p, int iDest){
76061 return sqlite3VdbeAddOp3(p, OP_Goto, 0, iDest, 0);
76062}
76063
76064/* Generate code to cause the string zStr to be loaded into
76065** register iDest
76066*/
76067SQLITE_PRIVATE int sqlite3VdbeLoadString(Vdbe *p, int iDest, const char *zStr){
76068 return sqlite3VdbeAddOp4(p, OP_String8, 0, iDest, 0, zStr, 0);
76069}
76070
76071/*
76072** Generate code that initializes multiple registers to string or integer
76073** constants. The registers begin with iDest and increase consecutively.
76074** One register is initialized for each characgter in zTypes[]. For each
76075** "s" character in zTypes[], the register is a string if the argument is
76076** not NULL, or OP_Null if the value is a null pointer. For each "i" character
76077** in zTypes[], the register is initialized to an integer.
76078**
76079** If the input string does not end with "X" then an OP_ResultRow instruction
76080** is generated for the values inserted.
76081*/
76082SQLITE_PRIVATE void sqlite3VdbeMultiLoad(Vdbe *p, int iDest, const char *zTypes, ...){
76083 va_list ap;
76084 int i;
76085 char c;
76086 va_start(ap, zTypes);
76087 for(i=0; (c = zTypes[i])!=0; i++){
76088 if( c=='s' ){
76089 const char *z = va_arg(ap, const char*);
76090 sqlite3VdbeAddOp4(p, z==0 ? OP_Null : OP_String8, 0, iDest+i, 0, z, 0);
76091 }else if( c=='i' ){
76092 sqlite3VdbeAddOp2(p, OP_Integer, va_arg(ap, int), iDest+i);
76093 }else{
76094 goto skip_op_resultrow;
76095 }
76096 }
76097 sqlite3VdbeAddOp2(p, OP_ResultRow, iDest, i);
76098skip_op_resultrow:
76099 va_end(ap);
76100}
76101
76102/*
76103** Add an opcode that includes the p4 value as a pointer.
76104*/
76105SQLITE_PRIVATE int sqlite3VdbeAddOp4(
76106 Vdbe *p, /* Add the opcode to this VM */
76107 int op, /* The new opcode */
76108 int p1, /* The P1 operand */
76109 int p2, /* The P2 operand */
76110 int p3, /* The P3 operand */
76111 const char *zP4, /* The P4 operand */
76112 int p4type /* P4 operand type */
76113){
76114 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
76115 sqlite3VdbeChangeP4(p, addr, zP4, p4type);
76116 return addr;
76117}
76118
76119/*
76120** Add an opcode that includes the p4 value with a P4_INT64 or
76121** P4_REAL type.
76122*/
76123SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(
76124 Vdbe *p, /* Add the opcode to this VM */
76125 int op, /* The new opcode */
76126 int p1, /* The P1 operand */
76127 int p2, /* The P2 operand */
76128 int p3, /* The P3 operand */
76129 const u8 *zP4, /* The P4 operand */
76130 int p4type /* P4 operand type */
76131){
76132 char *p4copy = sqlite3DbMallocRawNN(sqlite3VdbeDb(p), 8);
76133 if( p4copy ) memcpy(p4copy, zP4, 8);
76134 return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
76135}
76136
76137#ifndef SQLITE_OMIT_EXPLAIN
76138/*
76139** Return the address of the current EXPLAIN QUERY PLAN baseline.
76140** 0 means "none".
76141*/
76142SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse *pParse){
76143 VdbeOp *pOp;
76144 if( pParse->addrExplain==0 ) return 0;
76145 pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain);
76146 return pOp->p2;
76147}
76148
76149/*
76150** Add a new OP_Explain opcode.
76151**
76152** If the bPush flag is true, then make this opcode the parent for
76153** subsequent Explains until sqlite3VdbeExplainPop() is called.
76154*/
76155SQLITE_PRIVATE void sqlite3VdbeExplain(Parse *pParse, u8 bPush, const char *zFmt, ...){
76156 if( pParse->explain==2 ){
76157 char *zMsg;
76158 Vdbe *v;
76159 va_list ap;
76160 int iThis;
76161 va_start(ap, zFmt);
76162 zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap);
76163 va_end(ap);
76164 v = pParse->pVdbe;
76165 iThis = v->nOp;
76166 sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0,
76167 zMsg, P4_DYNAMIC);
76168 if( bPush) pParse->addrExplain = iThis;
76169 }
76170}
76171
76172/*
76173** Pop the EXPLAIN QUERY PLAN stack one level.
76174*/
76175SQLITE_PRIVATE void sqlite3VdbeExplainPop(Parse *pParse){
76176 pParse->addrExplain = sqlite3VdbeExplainParent(pParse);
76177}
76178#endif /* SQLITE_OMIT_EXPLAIN */
76179
76180/*
76181** Add an OP_ParseSchema opcode. This routine is broken out from
76182** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
76183** as having been used.
76184**
76185** The zWhere string must have been obtained from sqlite3_malloc().
76186** This routine will take ownership of the allocated memory.
76187*/
76188SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
76189 int j;
76190 sqlite3VdbeAddOp4(p, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC);
76191 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
76192}
76193
76194/*
76195** Add an opcode that includes the p4 value as an integer.
76196*/
76197SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(
76198 Vdbe *p, /* Add the opcode to this VM */
76199 int op, /* The new opcode */
76200 int p1, /* The P1 operand */
76201 int p2, /* The P2 operand */
76202 int p3, /* The P3 operand */
76203 int p4 /* The P4 operand as an integer */
76204){
76205 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
76206 if( p->db->mallocFailed==0 ){
76207 VdbeOp *pOp = &p->aOp[addr];
76208 pOp->p4type = P4_INT32;
76209 pOp->p4.i = p4;
76210 }
76211 return addr;
76212}
76213
76214/* Insert the end of a co-routine
76215*/
76216SQLITE_PRIVATE void sqlite3VdbeEndCoroutine(Vdbe *v, int regYield){
76217 sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
76218
76219 /* Clear the temporary register cache, thereby ensuring that each
76220 ** co-routine has its own independent set of registers, because co-routines
76221 ** might expect their registers to be preserved across an OP_Yield, and
76222 ** that could cause problems if two or more co-routines are using the same
76223 ** temporary register.
76224 */
76225 v->pParse->nTempReg = 0;
76226 v->pParse->nRangeReg = 0;
76227}
76228
76229/*
76230** Create a new symbolic label for an instruction that has yet to be
76231** coded. The symbolic label is really just a negative number. The
76232** label can be used as the P2 value of an operation. Later, when
76233** the label is resolved to a specific address, the VDBE will scan
76234** through its operation list and change all values of P2 which match
76235** the label into the resolved address.
76236**
76237** The VDBE knows that a P2 value is a label because labels are
76238** always negative and P2 values are suppose to be non-negative.
76239** Hence, a negative P2 value is a label that has yet to be resolved.
76240**
76241** Zero is returned if a malloc() fails.
76242*/
76243SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe *v){
76244 Parse *p = v->pParse;
76245 int i = p->nLabel++;
76246 assert( v->magic==VDBE_MAGIC_INIT );
76247 if( (i & (i-1))==0 ){
76248 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
76249 (i*2+1)*sizeof(p->aLabel[0]));
76250 }
76251 if( p->aLabel ){
76252 p->aLabel[i] = -1;
76253 }
76254 return ADDR(i);
76255}
76256
76257/*
76258** Resolve label "x" to be the address of the next instruction to
76259** be inserted. The parameter "x" must have been obtained from
76260** a prior call to sqlite3VdbeMakeLabel().
76261*/
76262SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *v, int x){
76263 Parse *p = v->pParse;
76264 int j = ADDR(x);
76265 assert( v->magic==VDBE_MAGIC_INIT );
76266 assert( j<p->nLabel );
76267 assert( j>=0 );
76268 if( p->aLabel ){
76269#ifdef SQLITE_DEBUG
76270 if( p->db->flags & SQLITE_VdbeAddopTrace ){
76271 printf("RESOLVE LABEL %d to %d\n", x, v->nOp);
76272 }
76273#endif
76274 assert( p->aLabel[j]==(-1) ); /* Labels may only be resolved once */
76275 p->aLabel[j] = v->nOp;
76276 }
76277}
76278
76279/*
76280** Mark the VDBE as one that can only be run one time.
76281*/
76282SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe *p){
76283 p->runOnlyOnce = 1;
76284}
76285
76286/*
76287** Mark the VDBE as one that can only be run multiple times.
76288*/
76289SQLITE_PRIVATE void sqlite3VdbeReusable(Vdbe *p){
76290 p->runOnlyOnce = 0;
76291}
76292
76293#ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
76294
76295/*
76296** The following type and function are used to iterate through all opcodes
76297** in a Vdbe main program and each of the sub-programs (triggers) it may
76298** invoke directly or indirectly. It should be used as follows:
76299**
76300** Op *pOp;
76301** VdbeOpIter sIter;
76302**
76303** memset(&sIter, 0, sizeof(sIter));
76304** sIter.v = v; // v is of type Vdbe*
76305** while( (pOp = opIterNext(&sIter)) ){
76306** // Do something with pOp
76307** }
76308** sqlite3DbFree(v->db, sIter.apSub);
76309**
76310*/
76311typedef struct VdbeOpIter VdbeOpIter;
76312struct VdbeOpIter {
76313 Vdbe *v; /* Vdbe to iterate through the opcodes of */
76314 SubProgram **apSub; /* Array of subprograms */
76315 int nSub; /* Number of entries in apSub */
76316 int iAddr; /* Address of next instruction to return */
76317 int iSub; /* 0 = main program, 1 = first sub-program etc. */
76318};
76319static Op *opIterNext(VdbeOpIter *p){
76320 Vdbe *v = p->v;
76321 Op *pRet = 0;
76322 Op *aOp;
76323 int nOp;
76324
76325 if( p->iSub<=p->nSub ){
76326
76327 if( p->iSub==0 ){
76328 aOp = v->aOp;
76329 nOp = v->nOp;
76330 }else{
76331 aOp = p->apSub[p->iSub-1]->aOp;
76332 nOp = p->apSub[p->iSub-1]->nOp;
76333 }
76334 assert( p->iAddr<nOp );
76335
76336 pRet = &aOp[p->iAddr];
76337 p->iAddr++;
76338 if( p->iAddr==nOp ){
76339 p->iSub++;
76340 p->iAddr = 0;
76341 }
76342
76343 if( pRet->p4type==P4_SUBPROGRAM ){
76344 int nByte = (p->nSub+1)*sizeof(SubProgram*);
76345 int j;
76346 for(j=0; j<p->nSub; j++){
76347 if( p->apSub[j]==pRet->p4.pProgram ) break;
76348 }
76349 if( j==p->nSub ){
76350 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
76351 if( !p->apSub ){
76352 pRet = 0;
76353 }else{
76354 p->apSub[p->nSub++] = pRet->p4.pProgram;
76355 }
76356 }
76357 }
76358 }
76359
76360 return pRet;
76361}
76362
76363/*
76364** Check if the program stored in the VM associated with pParse may
76365** throw an ABORT exception (causing the statement, but not entire transaction
76366** to be rolled back). This condition is true if the main program or any
76367** sub-programs contains any of the following:
76368**
76369** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
76370** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
76371** * OP_Destroy
76372** * OP_VUpdate
76373** * OP_VRename
76374** * OP_FkCounter with P2==0 (immediate foreign key constraint)
76375** * OP_CreateBtree/BTREE_INTKEY and OP_InitCoroutine
76376** (for CREATE TABLE AS SELECT ...)
76377**
76378** Then check that the value of Parse.mayAbort is true if an
76379** ABORT may be thrown, or false otherwise. Return true if it does
76380** match, or false otherwise. This function is intended to be used as
76381** part of an assert statement in the compiler. Similar to:
76382**
76383** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
76384*/
76385SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
76386 int hasAbort = 0;
76387 int hasFkCounter = 0;
76388 int hasCreateTable = 0;
76389 int hasInitCoroutine = 0;
76390 Op *pOp;
76391 VdbeOpIter sIter;
76392 memset(&sIter, 0, sizeof(sIter));
76393 sIter.v = v;
76394
76395 while( (pOp = opIterNext(&sIter))!=0 ){
76396 int opcode = pOp->opcode;
76397 if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
76398 || ((opcode==OP_Halt || opcode==OP_HaltIfNull)
76399 && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
76400 ){
76401 hasAbort = 1;
76402 break;
76403 }
76404 if( opcode==OP_CreateBtree && pOp->p3==BTREE_INTKEY ) hasCreateTable = 1;
76405 if( opcode==OP_InitCoroutine ) hasInitCoroutine = 1;
76406#ifndef SQLITE_OMIT_FOREIGN_KEY
76407 if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
76408 hasFkCounter = 1;
76409 }
76410#endif
76411 }
76412 sqlite3DbFree(v->db, sIter.apSub);
76413
76414 /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
76415 ** If malloc failed, then the while() loop above may not have iterated
76416 ** through all opcodes and hasAbort may be set incorrectly. Return
76417 ** true for this case to prevent the assert() in the callers frame
76418 ** from failing. */
76419 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
76420 || (hasCreateTable && hasInitCoroutine) );
76421}
76422#endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
76423
76424#ifdef SQLITE_DEBUG
76425/*
76426** Increment the nWrite counter in the VDBE if the cursor is not an
76427** ephemeral cursor, or if the cursor argument is NULL.
76428*/
76429SQLITE_PRIVATE void sqlite3VdbeIncrWriteCounter(Vdbe *p, VdbeCursor *pC){
76430 if( pC==0
76431 || (pC->eCurType!=CURTYPE_SORTER
76432 && pC->eCurType!=CURTYPE_PSEUDO
76433 && !pC->isEphemeral)
76434 ){
76435 p->nWrite++;
76436 }
76437}
76438#endif
76439
76440#ifdef SQLITE_DEBUG
76441/*
76442** Assert if an Abort at this point in time might result in a corrupt
76443** database.
76444*/
76445SQLITE_PRIVATE void sqlite3VdbeAssertAbortable(Vdbe *p){
76446 assert( p->nWrite==0 || p->usesStmtJournal );
76447}
76448#endif
76449
76450/*
76451** This routine is called after all opcodes have been inserted. It loops
76452** through all the opcodes and fixes up some details.
76453**
76454** (1) For each jump instruction with a negative P2 value (a label)
76455** resolve the P2 value to an actual address.
76456**
76457** (2) Compute the maximum number of arguments used by any SQL function
76458** and store that value in *pMaxFuncArgs.
76459**
76460** (3) Update the Vdbe.readOnly and Vdbe.bIsReader flags to accurately
76461** indicate what the prepared statement actually does.
76462**
76463** (4) Initialize the p4.xAdvance pointer on opcodes that use it.
76464**
76465** (5) Reclaim the memory allocated for storing labels.
76466**
76467** This routine will only function correctly if the mkopcodeh.tcl generator
76468** script numbers the opcodes correctly. Changes to this routine must be
76469** coordinated with changes to mkopcodeh.tcl.
76470*/
76471static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
76472 int nMaxArgs = *pMaxFuncArgs;
76473 Op *pOp;
76474 Parse *pParse = p->pParse;
76475 int *aLabel = pParse->aLabel;
76476 p->readOnly = 1;
76477 p->bIsReader = 0;
76478 pOp = &p->aOp[p->nOp-1];
76479 while(1){
76480
76481 /* Only JUMP opcodes and the short list of special opcodes in the switch
76482 ** below need to be considered. The mkopcodeh.tcl generator script groups
76483 ** all these opcodes together near the front of the opcode list. Skip
76484 ** any opcode that does not need processing by virtual of the fact that
76485 ** it is larger than SQLITE_MX_JUMP_OPCODE, as a performance optimization.
76486 */
76487 if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE ){
76488 /* NOTE: Be sure to update mkopcodeh.tcl when adding or removing
76489 ** cases from this switch! */
76490 switch( pOp->opcode ){
76491 case OP_Transaction: {
76492 if( pOp->p2!=0 ) p->readOnly = 0;
76493 /* fall thru */
76494 }
76495 case OP_AutoCommit:
76496 case OP_Savepoint: {
76497 p->bIsReader = 1;
76498 break;
76499 }
76500#ifndef SQLITE_OMIT_WAL
76501 case OP_Checkpoint:
76502#endif
76503 case OP_Vacuum:
76504 case OP_JournalMode: {
76505 p->readOnly = 0;
76506 p->bIsReader = 1;
76507 break;
76508 }
76509 case OP_Next:
76510 case OP_SorterNext: {
76511 pOp->p4.xAdvance = sqlite3BtreeNext;
76512 pOp->p4type = P4_ADVANCE;
76513 /* The code generator never codes any of these opcodes as a jump
76514 ** to a label. They are always coded as a jump backwards to a
76515 ** known address */
76516 assert( pOp->p2>=0 );
76517 break;
76518 }
76519 case OP_Prev: {
76520 pOp->p4.xAdvance = sqlite3BtreePrevious;
76521 pOp->p4type = P4_ADVANCE;
76522 /* The code generator never codes any of these opcodes as a jump
76523 ** to a label. They are always coded as a jump backwards to a
76524 ** known address */
76525 assert( pOp->p2>=0 );
76526 break;
76527 }
76528#ifndef SQLITE_OMIT_VIRTUALTABLE
76529 case OP_VUpdate: {
76530 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
76531 break;
76532 }
76533 case OP_VFilter: {
76534 int n;
76535 assert( (pOp - p->aOp) >= 3 );
76536 assert( pOp[-1].opcode==OP_Integer );
76537 n = pOp[-1].p1;
76538 if( n>nMaxArgs ) nMaxArgs = n;
76539 /* Fall through into the default case */
76540 }
76541#endif
76542 default: {
76543 if( pOp->p2<0 ){
76544 /* The mkopcodeh.tcl script has so arranged things that the only
76545 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
76546 ** have non-negative values for P2. */
76547 assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 );
76548 assert( ADDR(pOp->p2)<pParse->nLabel );
76549 pOp->p2 = aLabel[ADDR(pOp->p2)];
76550 }
76551 break;
76552 }
76553 }
76554 /* The mkopcodeh.tcl script has so arranged things that the only
76555 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
76556 ** have non-negative values for P2. */
76557 assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0);
76558 }
76559 if( pOp==p->aOp ) break;
76560 pOp--;
76561 }
76562 sqlite3DbFree(p->db, pParse->aLabel);
76563 pParse->aLabel = 0;
76564 pParse->nLabel = 0;
76565 *pMaxFuncArgs = nMaxArgs;
76566 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
76567}
76568
76569/*
76570** Return the address of the next instruction to be inserted.
76571*/
76572SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe *p){
76573 assert( p->magic==VDBE_MAGIC_INIT );
76574 return p->nOp;
76575}
76576
76577/*
76578** Verify that at least N opcode slots are available in p without
76579** having to malloc for more space (except when compiled using
76580** SQLITE_TEST_REALLOC_STRESS). This interface is used during testing
76581** to verify that certain calls to sqlite3VdbeAddOpList() can never
76582** fail due to a OOM fault and hence that the return value from
76583** sqlite3VdbeAddOpList() will always be non-NULL.
76584*/
76585#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
76586SQLITE_PRIVATE void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N){
76587 assert( p->nOp + N <= p->pParse->nOpAlloc );
76588}
76589#endif
76590
76591/*
76592** Verify that the VM passed as the only argument does not contain
76593** an OP_ResultRow opcode. Fail an assert() if it does. This is used
76594** by code in pragma.c to ensure that the implementation of certain
76595** pragmas comports with the flags specified in the mkpragmatab.tcl
76596** script.
76597*/
76598#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
76599SQLITE_PRIVATE void sqlite3VdbeVerifyNoResultRow(Vdbe *p){
76600 int i;
76601 for(i=0; i<p->nOp; i++){
76602 assert( p->aOp[i].opcode!=OP_ResultRow );
76603 }
76604}
76605#endif
76606
76607/*
76608** Generate code (a single OP_Abortable opcode) that will
76609** verify that the VDBE program can safely call Abort in the current
76610** context.
76611*/
76612#if defined(SQLITE_DEBUG)
76613SQLITE_PRIVATE void sqlite3VdbeVerifyAbortable(Vdbe *p, int onError){
76614 if( onError==OE_Abort ) sqlite3VdbeAddOp0(p, OP_Abortable);
76615}
76616#endif
76617
76618/*
76619** This function returns a pointer to the array of opcodes associated with
76620** the Vdbe passed as the first argument. It is the callers responsibility
76621** to arrange for the returned array to be eventually freed using the
76622** vdbeFreeOpArray() function.
76623**
76624** Before returning, *pnOp is set to the number of entries in the returned
76625** array. Also, *pnMaxArg is set to the larger of its current value and
76626** the number of entries in the Vdbe.apArg[] array required to execute the
76627** returned program.
76628*/
76629SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
76630 VdbeOp *aOp = p->aOp;
76631 assert( aOp && !p->db->mallocFailed );
76632
76633 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
76634 assert( DbMaskAllZero(p->btreeMask) );
76635
76636 resolveP2Values(p, pnMaxArg);
76637 *pnOp = p->nOp;
76638 p->aOp = 0;
76639 return aOp;
76640}
76641
76642/*
76643** Add a whole list of operations to the operation stack. Return a
76644** pointer to the first operation inserted.
76645**
76646** Non-zero P2 arguments to jump instructions are automatically adjusted
76647** so that the jump target is relative to the first operation inserted.
76648*/
76649SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(
76650 Vdbe *p, /* Add opcodes to the prepared statement */
76651 int nOp, /* Number of opcodes to add */
76652 VdbeOpList const *aOp, /* The opcodes to be added */
76653 int iLineno /* Source-file line number of first opcode */
76654){
76655 int i;
76656 VdbeOp *pOut, *pFirst;
76657 assert( nOp>0 );
76658 assert( p->magic==VDBE_MAGIC_INIT );
76659 if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p, nOp) ){
76660 return 0;
76661 }
76662 pFirst = pOut = &p->aOp[p->nOp];
76663 for(i=0; i<nOp; i++, aOp++, pOut++){
76664 pOut->opcode = aOp->opcode;
76665 pOut->p1 = aOp->p1;
76666 pOut->p2 = aOp->p2;
76667 assert( aOp->p2>=0 );
76668 if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){
76669 pOut->p2 += p->nOp;
76670 }
76671 pOut->p3 = aOp->p3;
76672 pOut->p4type = P4_NOTUSED;
76673 pOut->p4.p = 0;
76674 pOut->p5 = 0;
76675#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
76676 pOut->zComment = 0;
76677#endif
76678#ifdef SQLITE_VDBE_COVERAGE
76679 pOut->iSrcLine = iLineno+i;
76680#else
76681 (void)iLineno;
76682#endif
76683#ifdef SQLITE_DEBUG
76684 if( p->db->flags & SQLITE_VdbeAddopTrace ){
76685 sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
76686 }
76687#endif
76688 }
76689 p->nOp += nOp;
76690 return pFirst;
76691}
76692
76693#if defined(SQLITE_ENABLE_STMT_SCANSTATUS)
76694/*
76695** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus().
76696*/
76697SQLITE_PRIVATE void sqlite3VdbeScanStatus(
76698 Vdbe *p, /* VM to add scanstatus() to */
76699 int addrExplain, /* Address of OP_Explain (or 0) */
76700 int addrLoop, /* Address of loop counter */
76701 int addrVisit, /* Address of rows visited counter */
76702 LogEst nEst, /* Estimated number of output rows */
76703 const char *zName /* Name of table or index being scanned */
76704){
76705 int nByte = (p->nScan+1) * sizeof(ScanStatus);
76706 ScanStatus *aNew;
76707 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
76708 if( aNew ){
76709 ScanStatus *pNew = &aNew[p->nScan++];
76710 pNew->addrExplain = addrExplain;
76711 pNew->addrLoop = addrLoop;
76712 pNew->addrVisit = addrVisit;
76713 pNew->nEst = nEst;
76714 pNew->zName = sqlite3DbStrDup(p->db, zName);
76715 p->aScan = aNew;
76716 }
76717}
76718#endif
76719
76720
76721/*
76722** Change the value of the opcode, or P1, P2, P3, or P5 operands
76723** for a specific instruction.
76724*/
76725SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe *p, u32 addr, u8 iNewOpcode){
76726 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
76727}
76728SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
76729 sqlite3VdbeGetOp(p,addr)->p1 = val;
76730}
76731SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
76732 sqlite3VdbeGetOp(p,addr)->p2 = val;
76733}
76734SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
76735 sqlite3VdbeGetOp(p,addr)->p3 = val;
76736}
76737SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
76738 assert( p->nOp>0 || p->db->mallocFailed );
76739 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
76740}
76741
76742/*
76743** Change the P2 operand of instruction addr so that it points to
76744** the address of the next instruction to be coded.
76745*/
76746SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
76747 sqlite3VdbeChangeP2(p, addr, p->nOp);
76748}
76749
76750
76751/*
76752** If the input FuncDef structure is ephemeral, then free it. If
76753** the FuncDef is not ephermal, then do nothing.
76754*/
76755static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
76756 if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
76757 sqlite3DbFreeNN(db, pDef);
76758 }
76759}
76760
76761static void vdbeFreeOpArray(sqlite3 *, Op *, int);
76762
76763/*
76764** Delete a P4 value if necessary.
76765*/
76766static SQLITE_NOINLINE void freeP4Mem(sqlite3 *db, Mem *p){
76767 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
76768 sqlite3DbFreeNN(db, p);
76769}
76770static SQLITE_NOINLINE void freeP4FuncCtx(sqlite3 *db, sqlite3_context *p){
76771 freeEphemeralFunction(db, p->pFunc);
76772 sqlite3DbFreeNN(db, p);
76773}
76774static void freeP4(sqlite3 *db, int p4type, void *p4){
76775 assert( db );
76776 switch( p4type ){
76777 case P4_FUNCCTX: {
76778 freeP4FuncCtx(db, (sqlite3_context*)p4);
76779 break;
76780 }
76781 case P4_REAL:
76782 case P4_INT64:
76783 case P4_DYNAMIC:
76784 case P4_DYNBLOB:
76785 case P4_INTARRAY: {
76786 sqlite3DbFree(db, p4);
76787 break;
76788 }
76789 case P4_KEYINFO: {
76790 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
76791 break;
76792 }
76793#ifdef SQLITE_ENABLE_CURSOR_HINTS
76794 case P4_EXPR: {
76795 sqlite3ExprDelete(db, (Expr*)p4);
76796 break;
76797 }
76798#endif
76799 case P4_FUNCDEF: {
76800 freeEphemeralFunction(db, (FuncDef*)p4);
76801 break;
76802 }
76803 case P4_MEM: {
76804 if( db->pnBytesFreed==0 ){
76805 sqlite3ValueFree((sqlite3_value*)p4);
76806 }else{
76807 freeP4Mem(db, (Mem*)p4);
76808 }
76809 break;
76810 }
76811 case P4_VTAB : {
76812 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
76813 break;
76814 }
76815 }
76816}
76817
76818/*
76819** Free the space allocated for aOp and any p4 values allocated for the
76820** opcodes contained within. If aOp is not NULL it is assumed to contain
76821** nOp entries.
76822*/
76823static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
76824 if( aOp ){
76825 Op *pOp;
76826 for(pOp=&aOp[nOp-1]; pOp>=aOp; pOp--){
76827 if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, pOp->p4type, pOp->p4.p);
76828#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
76829 sqlite3DbFree(db, pOp->zComment);
76830#endif
76831 }
76832 sqlite3DbFreeNN(db, aOp);
76833 }
76834}
76835
76836/*
76837** Link the SubProgram object passed as the second argument into the linked
76838** list at Vdbe.pSubProgram. This list is used to delete all sub-program
76839** objects when the VM is no longer required.
76840*/
76841SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
76842 p->pNext = pVdbe->pProgram;
76843 pVdbe->pProgram = p;
76844}
76845
76846/*
76847** Change the opcode at addr into OP_Noop
76848*/
76849SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
76850 VdbeOp *pOp;
76851 if( p->db->mallocFailed ) return 0;
76852 assert( addr>=0 && addr<p->nOp );
76853 pOp = &p->aOp[addr];
76854 freeP4(p->db, pOp->p4type, pOp->p4.p);
76855 pOp->p4type = P4_NOTUSED;
76856 pOp->p4.z = 0;
76857 pOp->opcode = OP_Noop;
76858 return 1;
76859}
76860
76861/*
76862** If the last opcode is "op" and it is not a jump destination,
76863** then remove it. Return true if and only if an opcode was removed.
76864*/
76865SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
76866 if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
76867 return sqlite3VdbeChangeToNoop(p, p->nOp-1);
76868 }else{
76869 return 0;
76870 }
76871}
76872
76873/*
76874** Change the value of the P4 operand for a specific instruction.
76875** This routine is useful when a large program is loaded from a
76876** static array using sqlite3VdbeAddOpList but we want to make a
76877** few minor changes to the program.
76878**
76879** If n>=0 then the P4 operand is dynamic, meaning that a copy of
76880** the string is made into memory obtained from sqlite3_malloc().
76881** A value of n==0 means copy bytes of zP4 up to and including the
76882** first null byte. If n>0 then copy n+1 bytes of zP4.
76883**
76884** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
76885** to a string or structure that is guaranteed to exist for the lifetime of
76886** the Vdbe. In these cases we can just copy the pointer.
76887**
76888** If addr<0 then change P4 on the most recently inserted instruction.
76889*/
76890static void SQLITE_NOINLINE vdbeChangeP4Full(
76891 Vdbe *p,
76892 Op *pOp,
76893 const char *zP4,
76894 int n
76895){
76896 if( pOp->p4type ){
76897 freeP4(p->db, pOp->p4type, pOp->p4.p);
76898 pOp->p4type = 0;
76899 pOp->p4.p = 0;
76900 }
76901 if( n<0 ){
76902 sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n);
76903 }else{
76904 if( n==0 ) n = sqlite3Strlen30(zP4);
76905 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
76906 pOp->p4type = P4_DYNAMIC;
76907 }
76908}
76909SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
76910 Op *pOp;
76911 sqlite3 *db;
76912 assert( p!=0 );
76913 db = p->db;
76914 assert( p->magic==VDBE_MAGIC_INIT );
76915 assert( p->aOp!=0 || db->mallocFailed );
76916 if( db->mallocFailed ){
76917 if( n!=P4_VTAB ) freeP4(db, n, (void*)*(char**)&zP4);
76918 return;
76919 }
76920 assert( p->nOp>0 );
76921 assert( addr<p->nOp );
76922 if( addr<0 ){
76923 addr = p->nOp - 1;
76924 }
76925 pOp = &p->aOp[addr];
76926 if( n>=0 || pOp->p4type ){
76927 vdbeChangeP4Full(p, pOp, zP4, n);
76928 return;
76929 }
76930 if( n==P4_INT32 ){
76931 /* Note: this cast is safe, because the origin data point was an int
76932 ** that was cast to a (const char *). */
76933 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
76934 pOp->p4type = P4_INT32;
76935 }else if( zP4!=0 ){
76936 assert( n<0 );
76937 pOp->p4.p = (void*)zP4;
76938 pOp->p4type = (signed char)n;
76939 if( n==P4_VTAB ) sqlite3VtabLock((VTable*)zP4);
76940 }
76941}
76942
76943/*
76944** Change the P4 operand of the most recently coded instruction
76945** to the value defined by the arguments. This is a high-speed
76946** version of sqlite3VdbeChangeP4().
76947**
76948** The P4 operand must not have been previously defined. And the new
76949** P4 must not be P4_INT32. Use sqlite3VdbeChangeP4() in either of
76950** those cases.
76951*/
76952SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe *p, void *pP4, int n){
76953 VdbeOp *pOp;
76954 assert( n!=P4_INT32 && n!=P4_VTAB );
76955 assert( n<=0 );
76956 if( p->db->mallocFailed ){
76957 freeP4(p->db, n, pP4);
76958 }else{
76959 assert( pP4!=0 );
76960 assert( p->nOp>0 );
76961 pOp = &p->aOp[p->nOp-1];
76962 assert( pOp->p4type==P4_NOTUSED );
76963 pOp->p4type = n;
76964 pOp->p4.p = pP4;
76965 }
76966}
76967
76968/*
76969** Set the P4 on the most recently added opcode to the KeyInfo for the
76970** index given.
76971*/
76972SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
76973 Vdbe *v = pParse->pVdbe;
76974 KeyInfo *pKeyInfo;
76975 assert( v!=0 );
76976 assert( pIdx!=0 );
76977 pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pIdx);
76978 if( pKeyInfo ) sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO);
76979}
76980
76981#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
76982/*
76983** Change the comment on the most recently coded instruction. Or
76984** insert a No-op and add the comment to that new instruction. This
76985** makes the code easier to read during debugging. None of this happens
76986** in a production build.
76987*/
76988static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
76989 assert( p->nOp>0 || p->aOp==0 );
76990 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
76991 if( p->nOp ){
76992 assert( p->aOp );
76993 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
76994 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
76995 }
76996}
76997SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
76998 va_list ap;
76999 if( p ){
77000 va_start(ap, zFormat);
77001 vdbeVComment(p, zFormat, ap);
77002 va_end(ap);
77003 }
77004}
77005SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
77006 va_list ap;
77007 if( p ){
77008 sqlite3VdbeAddOp0(p, OP_Noop);
77009 va_start(ap, zFormat);
77010 vdbeVComment(p, zFormat, ap);
77011 va_end(ap);
77012 }
77013}
77014#endif /* NDEBUG */
77015
77016#ifdef SQLITE_VDBE_COVERAGE
77017/*
77018** Set the value if the iSrcLine field for the previously coded instruction.
77019*/
77020SQLITE_PRIVATE void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
77021 sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
77022}
77023#endif /* SQLITE_VDBE_COVERAGE */
77024
77025/*
77026** Return the opcode for a given address. If the address is -1, then
77027** return the most recently inserted opcode.
77028**
77029** If a memory allocation error has occurred prior to the calling of this
77030** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
77031** is readable but not writable, though it is cast to a writable value.
77032** The return of a dummy opcode allows the call to continue functioning
77033** after an OOM fault without having to check to see if the return from
77034** this routine is a valid pointer. But because the dummy.opcode is 0,
77035** dummy will never be written to. This is verified by code inspection and
77036** by running with Valgrind.
77037*/
77038SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
77039 /* C89 specifies that the constant "dummy" will be initialized to all
77040 ** zeros, which is correct. MSVC generates a warning, nevertheless. */
77041 static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */
77042 assert( p->magic==VDBE_MAGIC_INIT );
77043 if( addr<0 ){
77044 addr = p->nOp - 1;
77045 }
77046 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
77047 if( p->db->mallocFailed ){
77048 return (VdbeOp*)&dummy;
77049 }else{
77050 return &p->aOp[addr];
77051 }
77052}
77053
77054#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
77055/*
77056** Return an integer value for one of the parameters to the opcode pOp
77057** determined by character c.
77058*/
77059static int translateP(char c, const Op *pOp){
77060 if( c=='1' ) return pOp->p1;
77061 if( c=='2' ) return pOp->p2;
77062 if( c=='3' ) return pOp->p3;
77063 if( c=='4' ) return pOp->p4.i;
77064 return pOp->p5;
77065}
77066
77067/*
77068** Compute a string for the "comment" field of a VDBE opcode listing.
77069**
77070** The Synopsis: field in comments in the vdbe.c source file gets converted
77071** to an extra string that is appended to the sqlite3OpcodeName(). In the
77072** absence of other comments, this synopsis becomes the comment on the opcode.
77073** Some translation occurs:
77074**
77075** "PX" -> "r[X]"
77076** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
77077** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
77078** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
77079*/
77080static int displayComment(
77081 const Op *pOp, /* The opcode to be commented */
77082 const char *zP4, /* Previously obtained value for P4 */
77083 char *zTemp, /* Write result here */
77084 int nTemp /* Space available in zTemp[] */
77085){
77086 const char *zOpName;
77087 const char *zSynopsis;
77088 int nOpName;
77089 int ii, jj;
77090 char zAlt[50];
77091 zOpName = sqlite3OpcodeName(pOp->opcode);
77092 nOpName = sqlite3Strlen30(zOpName);
77093 if( zOpName[nOpName+1] ){
77094 int seenCom = 0;
77095 char c;
77096 zSynopsis = zOpName += nOpName + 1;
77097 if( strncmp(zSynopsis,"IF ",3)==0 ){
77098 if( pOp->p5 & SQLITE_STOREP2 ){
77099 sqlite3_snprintf(sizeof(zAlt), zAlt, "r[P2] = (%s)", zSynopsis+3);
77100 }else{
77101 sqlite3_snprintf(sizeof(zAlt), zAlt, "if %s goto P2", zSynopsis+3);
77102 }
77103 zSynopsis = zAlt;
77104 }
77105 for(ii=jj=0; jj<nTemp-1 && (c = zSynopsis[ii])!=0; ii++){
77106 if( c=='P' ){
77107 c = zSynopsis[++ii];
77108 if( c=='4' ){
77109 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", zP4);
77110 }else if( c=='X' ){
77111 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", pOp->zComment);
77112 seenCom = 1;
77113 }else{
77114 int v1 = translateP(c, pOp);
77115 int v2;
77116 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%d", v1);
77117 if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){
77118 ii += 3;
77119 jj += sqlite3Strlen30(zTemp+jj);
77120 v2 = translateP(zSynopsis[ii], pOp);
77121 if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){
77122 ii += 2;
77123 v2++;
77124 }
77125 if( v2>1 ){
77126 sqlite3_snprintf(nTemp-jj, zTemp+jj, "..%d", v1+v2-1);
77127 }
77128 }else if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
77129 ii += 4;
77130 }
77131 }
77132 jj += sqlite3Strlen30(zTemp+jj);
77133 }else{
77134 zTemp[jj++] = c;
77135 }
77136 }
77137 if( !seenCom && jj<nTemp-5 && pOp->zComment ){
77138 sqlite3_snprintf(nTemp-jj, zTemp+jj, "; %s", pOp->zComment);
77139 jj += sqlite3Strlen30(zTemp+jj);
77140 }
77141 if( jj<nTemp ) zTemp[jj] = 0;
77142 }else if( pOp->zComment ){
77143 sqlite3_snprintf(nTemp, zTemp, "%s", pOp->zComment);
77144 jj = sqlite3Strlen30(zTemp);
77145 }else{
77146 zTemp[0] = 0;
77147 jj = 0;
77148 }
77149 return jj;
77150}
77151#endif /* SQLITE_DEBUG */
77152
77153#if VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS)
77154/*
77155** Translate the P4.pExpr value for an OP_CursorHint opcode into text
77156** that can be displayed in the P4 column of EXPLAIN output.
77157*/
77158static void displayP4Expr(StrAccum *p, Expr *pExpr){
77159 const char *zOp = 0;
77160 switch( pExpr->op ){
77161 case TK_STRING:
77162 sqlite3_str_appendf(p, "%Q", pExpr->u.zToken);
77163 break;
77164 case TK_INTEGER:
77165 sqlite3_str_appendf(p, "%d", pExpr->u.iValue);
77166 break;
77167 case TK_NULL:
77168 sqlite3_str_appendf(p, "NULL");
77169 break;
77170 case TK_REGISTER: {
77171 sqlite3_str_appendf(p, "r[%d]", pExpr->iTable);
77172 break;
77173 }
77174 case TK_COLUMN: {
77175 if( pExpr->iColumn<0 ){
77176 sqlite3_str_appendf(p, "rowid");
77177 }else{
77178 sqlite3_str_appendf(p, "c%d", (int)pExpr->iColumn);
77179 }
77180 break;
77181 }
77182 case TK_LT: zOp = "LT"; break;
77183 case TK_LE: zOp = "LE"; break;
77184 case TK_GT: zOp = "GT"; break;
77185 case TK_GE: zOp = "GE"; break;
77186 case TK_NE: zOp = "NE"; break;
77187 case TK_EQ: zOp = "EQ"; break;
77188 case TK_IS: zOp = "IS"; break;
77189 case TK_ISNOT: zOp = "ISNOT"; break;
77190 case TK_AND: zOp = "AND"; break;
77191 case TK_OR: zOp = "OR"; break;
77192 case TK_PLUS: zOp = "ADD"; break;
77193 case TK_STAR: zOp = "MUL"; break;
77194 case TK_MINUS: zOp = "SUB"; break;
77195 case TK_REM: zOp = "REM"; break;
77196 case TK_BITAND: zOp = "BITAND"; break;
77197 case TK_BITOR: zOp = "BITOR"; break;
77198 case TK_SLASH: zOp = "DIV"; break;
77199 case TK_LSHIFT: zOp = "LSHIFT"; break;
77200 case TK_RSHIFT: zOp = "RSHIFT"; break;
77201 case TK_CONCAT: zOp = "CONCAT"; break;
77202 case TK_UMINUS: zOp = "MINUS"; break;
77203 case TK_UPLUS: zOp = "PLUS"; break;
77204 case TK_BITNOT: zOp = "BITNOT"; break;
77205 case TK_NOT: zOp = "NOT"; break;
77206 case TK_ISNULL: zOp = "ISNULL"; break;
77207 case TK_NOTNULL: zOp = "NOTNULL"; break;
77208
77209 default:
77210 sqlite3_str_appendf(p, "%s", "expr");
77211 break;
77212 }
77213
77214 if( zOp ){
77215 sqlite3_str_appendf(p, "%s(", zOp);
77216 displayP4Expr(p, pExpr->pLeft);
77217 if( pExpr->pRight ){
77218 sqlite3_str_append(p, ",", 1);
77219 displayP4Expr(p, pExpr->pRight);
77220 }
77221 sqlite3_str_append(p, ")", 1);
77222 }
77223}
77224#endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */
77225
77226
77227#if VDBE_DISPLAY_P4
77228/*
77229** Compute a string that describes the P4 parameter for an opcode.
77230** Use zTemp for any required temporary buffer space.
77231*/
77232static char *displayP4(Op *pOp, char *zTemp, int nTemp){
77233 char *zP4 = zTemp;
77234 StrAccum x;
77235 assert( nTemp>=20 );
77236 sqlite3StrAccumInit(&x, 0, zTemp, nTemp, 0);
77237 switch( pOp->p4type ){
77238 case P4_KEYINFO: {
77239 int j;
77240 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
77241 assert( pKeyInfo->aSortOrder!=0 );
77242 sqlite3_str_appendf(&x, "k(%d", pKeyInfo->nKeyField);
77243 for(j=0; j<pKeyInfo->nKeyField; j++){
77244 CollSeq *pColl = pKeyInfo->aColl[j];
77245 const char *zColl = pColl ? pColl->zName : "";
77246 if( strcmp(zColl, "BINARY")==0 ) zColl = "B";
77247 sqlite3_str_appendf(&x, ",%s%s",
77248 pKeyInfo->aSortOrder[j] ? "-" : "", zColl);
77249 }
77250 sqlite3_str_append(&x, ")", 1);
77251 break;
77252 }
77253#ifdef SQLITE_ENABLE_CURSOR_HINTS
77254 case P4_EXPR: {
77255 displayP4Expr(&x, pOp->p4.pExpr);
77256 break;
77257 }
77258#endif
77259 case P4_COLLSEQ: {
77260 CollSeq *pColl = pOp->p4.pColl;
77261 sqlite3_str_appendf(&x, "(%.20s)", pColl->zName);
77262 break;
77263 }
77264 case P4_FUNCDEF: {
77265 FuncDef *pDef = pOp->p4.pFunc;
77266 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
77267 break;
77268 }
77269#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
77270 case P4_FUNCCTX: {
77271 FuncDef *pDef = pOp->p4.pCtx->pFunc;
77272 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
77273 break;
77274 }
77275#endif
77276 case P4_INT64: {
77277 sqlite3_str_appendf(&x, "%lld", *pOp->p4.pI64);
77278 break;
77279 }
77280 case P4_INT32: {
77281 sqlite3_str_appendf(&x, "%d", pOp->p4.i);
77282 break;
77283 }
77284 case P4_REAL: {
77285 sqlite3_str_appendf(&x, "%.16g", *pOp->p4.pReal);
77286 break;
77287 }
77288 case P4_MEM: {
77289 Mem *pMem = pOp->p4.pMem;
77290 if( pMem->flags & MEM_Str ){
77291 zP4 = pMem->z;
77292 }else if( pMem->flags & MEM_Int ){
77293 sqlite3_str_appendf(&x, "%lld", pMem->u.i);
77294 }else if( pMem->flags & MEM_Real ){
77295 sqlite3_str_appendf(&x, "%.16g", pMem->u.r);
77296 }else if( pMem->flags & MEM_Null ){
77297 zP4 = "NULL";
77298 }else{
77299 assert( pMem->flags & MEM_Blob );
77300 zP4 = "(blob)";
77301 }
77302 break;
77303 }
77304#ifndef SQLITE_OMIT_VIRTUALTABLE
77305 case P4_VTAB: {
77306 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
77307 sqlite3_str_appendf(&x, "vtab:%p", pVtab);
77308 break;
77309 }
77310#endif
77311 case P4_INTARRAY: {
77312 int i;
77313 int *ai = pOp->p4.ai;
77314 int n = ai[0]; /* The first element of an INTARRAY is always the
77315 ** count of the number of elements to follow */
77316 for(i=1; i<=n; i++){
77317 sqlite3_str_appendf(&x, ",%d", ai[i]);
77318 }
77319 zTemp[0] = '[';
77320 sqlite3_str_append(&x, "]", 1);
77321 break;
77322 }
77323 case P4_SUBPROGRAM: {
77324 sqlite3_str_appendf(&x, "program");
77325 break;
77326 }
77327 case P4_DYNBLOB:
77328 case P4_ADVANCE: {
77329 zTemp[0] = 0;
77330 break;
77331 }
77332 case P4_TABLE: {
77333 sqlite3_str_appendf(&x, "%s", pOp->p4.pTab->zName);
77334 break;
77335 }
77336 default: {
77337 zP4 = pOp->p4.z;
77338 if( zP4==0 ){
77339 zP4 = zTemp;
77340 zTemp[0] = 0;
77341 }
77342 }
77343 }
77344 sqlite3StrAccumFinish(&x);
77345 assert( zP4!=0 );
77346 return zP4;
77347}
77348#endif /* VDBE_DISPLAY_P4 */
77349
77350/*
77351** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
77352**
77353** The prepared statements need to know in advance the complete set of
77354** attached databases that will be use. A mask of these databases
77355** is maintained in p->btreeMask. The p->lockMask value is the subset of
77356** p->btreeMask of databases that will require a lock.
77357*/
77358SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
77359 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
77360 assert( i<(int)sizeof(p->btreeMask)*8 );
77361 DbMaskSet(p->btreeMask, i);
77362 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
77363 DbMaskSet(p->lockMask, i);
77364 }
77365}
77366
77367#if !defined(SQLITE_OMIT_SHARED_CACHE)
77368/*
77369** If SQLite is compiled to support shared-cache mode and to be threadsafe,
77370** this routine obtains the mutex associated with each BtShared structure
77371** that may be accessed by the VM passed as an argument. In doing so it also
77372** sets the BtShared.db member of each of the BtShared structures, ensuring
77373** that the correct busy-handler callback is invoked if required.
77374**
77375** If SQLite is not threadsafe but does support shared-cache mode, then
77376** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
77377** of all of BtShared structures accessible via the database handle
77378** associated with the VM.
77379**
77380** If SQLite is not threadsafe and does not support shared-cache mode, this
77381** function is a no-op.
77382**
77383** The p->btreeMask field is a bitmask of all btrees that the prepared
77384** statement p will ever use. Let N be the number of bits in p->btreeMask
77385** corresponding to btrees that use shared cache. Then the runtime of
77386** this routine is N*N. But as N is rarely more than 1, this should not
77387** be a problem.
77388*/
77389SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){
77390 int i;
77391 sqlite3 *db;
77392 Db *aDb;
77393 int nDb;
77394 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
77395 db = p->db;
77396 aDb = db->aDb;
77397 nDb = db->nDb;
77398 for(i=0; i<nDb; i++){
77399 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
77400 sqlite3BtreeEnter(aDb[i].pBt);
77401 }
77402 }
77403}
77404#endif
77405
77406#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
77407/*
77408** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
77409*/
77410static SQLITE_NOINLINE void vdbeLeave(Vdbe *p){
77411 int i;
77412 sqlite3 *db;
77413 Db *aDb;
77414 int nDb;
77415 db = p->db;
77416 aDb = db->aDb;
77417 nDb = db->nDb;
77418 for(i=0; i<nDb; i++){
77419 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
77420 sqlite3BtreeLeave(aDb[i].pBt);
77421 }
77422 }
77423}
77424SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
77425 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
77426 vdbeLeave(p);
77427}
77428#endif
77429
77430#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
77431/*
77432** Print a single opcode. This routine is used for debugging only.
77433*/
77434SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, VdbeOp *pOp){
77435 char *zP4;
77436 char zPtr[50];
77437 char zCom[100];
77438 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
77439 if( pOut==0 ) pOut = stdout;
77440 zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
77441#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
77442 displayComment(pOp, zP4, zCom, sizeof(zCom));
77443#else
77444 zCom[0] = 0;
77445#endif
77446 /* NB: The sqlite3OpcodeName() function is implemented by code created
77447 ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the
77448 ** information from the vdbe.c source text */
77449 fprintf(pOut, zFormat1, pc,
77450 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
77451 zCom
77452 );
77453 fflush(pOut);
77454}
77455#endif
77456
77457/*
77458** Initialize an array of N Mem element.
77459*/
77460static void initMemArray(Mem *p, int N, sqlite3 *db, u16 flags){
77461 while( (N--)>0 ){
77462 p->db = db;
77463 p->flags = flags;
77464 p->szMalloc = 0;
77465#ifdef SQLITE_DEBUG
77466 p->pScopyFrom = 0;
77467#endif
77468 p++;
77469 }
77470}
77471
77472/*
77473** Release an array of N Mem elements
77474*/
77475static void releaseMemArray(Mem *p, int N){
77476 if( p && N ){
77477 Mem *pEnd = &p[N];
77478 sqlite3 *db = p->db;
77479 if( db->pnBytesFreed ){
77480 do{
77481 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
77482 }while( (++p)<pEnd );
77483 return;
77484 }
77485 do{
77486 assert( (&p[1])==pEnd || p[0].db==p[1].db );
77487 assert( sqlite3VdbeCheckMemInvariants(p) );
77488
77489 /* This block is really an inlined version of sqlite3VdbeMemRelease()
77490 ** that takes advantage of the fact that the memory cell value is
77491 ** being set to NULL after releasing any dynamic resources.
77492 **
77493 ** The justification for duplicating code is that according to
77494 ** callgrind, this causes a certain test case to hit the CPU 4.7
77495 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
77496 ** sqlite3MemRelease() were called from here. With -O2, this jumps
77497 ** to 6.6 percent. The test case is inserting 1000 rows into a table
77498 ** with no indexes using a single prepared INSERT statement, bind()
77499 ** and reset(). Inserts are grouped into a transaction.
77500 */
77501 testcase( p->flags & MEM_Agg );
77502 testcase( p->flags & MEM_Dyn );
77503 testcase( p->xDel==sqlite3VdbeFrameMemDel );
77504 if( p->flags&(MEM_Agg|MEM_Dyn) ){
77505 sqlite3VdbeMemRelease(p);
77506 }else if( p->szMalloc ){
77507 sqlite3DbFreeNN(db, p->zMalloc);
77508 p->szMalloc = 0;
77509 }
77510
77511 p->flags = MEM_Undefined;
77512 }while( (++p)<pEnd );
77513 }
77514}
77515
77516#ifdef SQLITE_DEBUG
77517/*
77518** Verify that pFrame is a valid VdbeFrame pointer. Return true if it is
77519** and false if something is wrong.
77520**
77521** This routine is intended for use inside of assert() statements only.
77522*/
77523SQLITE_PRIVATE int sqlite3VdbeFrameIsValid(VdbeFrame *pFrame){
77524 if( pFrame->iFrameMagic!=SQLITE_FRAME_MAGIC ) return 0;
77525 return 1;
77526}
77527#endif
77528
77529
77530/*
77531** This is a destructor on a Mem object (which is really an sqlite3_value)
77532** that deletes the Frame object that is attached to it as a blob.
77533**
77534** This routine does not delete the Frame right away. It merely adds the
77535** frame to a list of frames to be deleted when the Vdbe halts.
77536*/
77537SQLITE_PRIVATE void sqlite3VdbeFrameMemDel(void *pArg){
77538 VdbeFrame *pFrame = (VdbeFrame*)pArg;
77539 assert( sqlite3VdbeFrameIsValid(pFrame) );
77540 pFrame->pParent = pFrame->v->pDelFrame;
77541 pFrame->v->pDelFrame = pFrame;
77542}
77543
77544
77545/*
77546** Delete a VdbeFrame object and its contents. VdbeFrame objects are
77547** allocated by the OP_Program opcode in sqlite3VdbeExec().
77548*/
77549SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame *p){
77550 int i;
77551 Mem *aMem = VdbeFrameMem(p);
77552 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
77553 assert( sqlite3VdbeFrameIsValid(p) );
77554 for(i=0; i<p->nChildCsr; i++){
77555 sqlite3VdbeFreeCursor(p->v, apCsr[i]);
77556 }
77557 releaseMemArray(aMem, p->nChildMem);
77558 sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0);
77559 sqlite3DbFree(p->v->db, p);
77560}
77561
77562#ifndef SQLITE_OMIT_EXPLAIN
77563/*
77564** Give a listing of the program in the virtual machine.
77565**
77566** The interface is the same as sqlite3VdbeExec(). But instead of
77567** running the code, it invokes the callback once for each instruction.
77568** This feature is used to implement "EXPLAIN".
77569**
77570** When p->explain==1, each instruction is listed. When
77571** p->explain==2, only OP_Explain instructions are listed and these
77572** are shown in a different format. p->explain==2 is used to implement
77573** EXPLAIN QUERY PLAN.
77574** 2018-04-24: In p->explain==2 mode, the OP_Init opcodes of triggers
77575** are also shown, so that the boundaries between the main program and
77576** each trigger are clear.
77577**
77578** When p->explain==1, first the main program is listed, then each of
77579** the trigger subprograms are listed one by one.
77580*/
77581SQLITE_PRIVATE int sqlite3VdbeList(
77582 Vdbe *p /* The VDBE */
77583){
77584 int nRow; /* Stop when row count reaches this */
77585 int nSub = 0; /* Number of sub-vdbes seen so far */
77586 SubProgram **apSub = 0; /* Array of sub-vdbes */
77587 Mem *pSub = 0; /* Memory cell hold array of subprogs */
77588 sqlite3 *db = p->db; /* The database connection */
77589 int i; /* Loop counter */
77590 int rc = SQLITE_OK; /* Return code */
77591 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
77592 int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP)!=0);
77593 Op *pOp = 0;
77594
77595 assert( p->explain );
77596 assert( p->magic==VDBE_MAGIC_RUN );
77597 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
77598
77599 /* Even though this opcode does not use dynamic strings for
77600 ** the result, result columns may become dynamic if the user calls
77601 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
77602 */
77603 releaseMemArray(pMem, 8);
77604 p->pResultSet = 0;
77605
77606 if( p->rc==SQLITE_NOMEM ){
77607 /* This happens if a malloc() inside a call to sqlite3_column_text() or
77608 ** sqlite3_column_text16() failed. */
77609 sqlite3OomFault(db);
77610 return SQLITE_ERROR;
77611 }
77612
77613 /* When the number of output rows reaches nRow, that means the
77614 ** listing has finished and sqlite3_step() should return SQLITE_DONE.
77615 ** nRow is the sum of the number of rows in the main program, plus
77616 ** the sum of the number of rows in all trigger subprograms encountered
77617 ** so far. The nRow value will increase as new trigger subprograms are
77618 ** encountered, but p->pc will eventually catch up to nRow.
77619 */
77620 nRow = p->nOp;
77621 if( bListSubprogs ){
77622 /* The first 8 memory cells are used for the result set. So we will
77623 ** commandeer the 9th cell to use as storage for an array of pointers
77624 ** to trigger subprograms. The VDBE is guaranteed to have at least 9
77625 ** cells. */
77626 assert( p->nMem>9 );
77627 pSub = &p->aMem[9];
77628 if( pSub->flags&MEM_Blob ){
77629 /* On the first call to sqlite3_step(), pSub will hold a NULL. It is
77630 ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
77631 nSub = pSub->n/sizeof(Vdbe*);
77632 apSub = (SubProgram **)pSub->z;
77633 }
77634 for(i=0; i<nSub; i++){
77635 nRow += apSub[i]->nOp;
77636 }
77637 }
77638
77639 while(1){ /* Loop exits via break */
77640 i = p->pc++;
77641 if( i>=nRow ){
77642 p->rc = SQLITE_OK;
77643 rc = SQLITE_DONE;
77644 break;
77645 }
77646 if( i<p->nOp ){
77647 /* The output line number is small enough that we are still in the
77648 ** main program. */
77649 pOp = &p->aOp[i];
77650 }else{
77651 /* We are currently listing subprograms. Figure out which one and
77652 ** pick up the appropriate opcode. */
77653 int j;
77654 i -= p->nOp;
77655 for(j=0; i>=apSub[j]->nOp; j++){
77656 i -= apSub[j]->nOp;
77657 }
77658 pOp = &apSub[j]->aOp[i];
77659 }
77660
77661 /* When an OP_Program opcode is encounter (the only opcode that has
77662 ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
77663 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
77664 ** has not already been seen.
77665 */
77666 if( bListSubprogs && pOp->p4type==P4_SUBPROGRAM ){
77667 int nByte = (nSub+1)*sizeof(SubProgram*);
77668 int j;
77669 for(j=0; j<nSub; j++){
77670 if( apSub[j]==pOp->p4.pProgram ) break;
77671 }
77672 if( j==nSub ){
77673 p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0);
77674 if( p->rc!=SQLITE_OK ){
77675 rc = SQLITE_ERROR;
77676 break;
77677 }
77678 apSub = (SubProgram **)pSub->z;
77679 apSub[nSub++] = pOp->p4.pProgram;
77680 pSub->flags |= MEM_Blob;
77681 pSub->n = nSub*sizeof(SubProgram*);
77682 nRow += pOp->p4.pProgram->nOp;
77683 }
77684 }
77685 if( p->explain<2 ) break;
77686 if( pOp->opcode==OP_Explain ) break;
77687 if( pOp->opcode==OP_Init && p->pc>1 ) break;
77688 }
77689
77690 if( rc==SQLITE_OK ){
77691 if( db->u1.isInterrupted ){
77692 p->rc = SQLITE_INTERRUPT;
77693 rc = SQLITE_ERROR;
77694 sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
77695 }else{
77696 char *zP4;
77697 if( p->explain==1 ){
77698 pMem->flags = MEM_Int;
77699 pMem->u.i = i; /* Program counter */
77700 pMem++;
77701
77702 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
77703 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
77704 assert( pMem->z!=0 );
77705 pMem->n = sqlite3Strlen30(pMem->z);
77706 pMem->enc = SQLITE_UTF8;
77707 pMem++;
77708 }
77709
77710 pMem->flags = MEM_Int;
77711 pMem->u.i = pOp->p1; /* P1 */
77712 pMem++;
77713
77714 pMem->flags = MEM_Int;
77715 pMem->u.i = pOp->p2; /* P2 */
77716 pMem++;
77717
77718 pMem->flags = MEM_Int;
77719 pMem->u.i = pOp->p3; /* P3 */
77720 pMem++;
77721
77722 if( sqlite3VdbeMemClearAndResize(pMem, 100) ){ /* P4 */
77723 assert( p->db->mallocFailed );
77724 return SQLITE_ERROR;
77725 }
77726 pMem->flags = MEM_Str|MEM_Term;
77727 zP4 = displayP4(pOp, pMem->z, pMem->szMalloc);
77728 if( zP4!=pMem->z ){
77729 pMem->n = 0;
77730 sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
77731 }else{
77732 assert( pMem->z!=0 );
77733 pMem->n = sqlite3Strlen30(pMem->z);
77734 pMem->enc = SQLITE_UTF8;
77735 }
77736 pMem++;
77737
77738 if( p->explain==1 ){
77739 if( sqlite3VdbeMemClearAndResize(pMem, 4) ){
77740 assert( p->db->mallocFailed );
77741 return SQLITE_ERROR;
77742 }
77743 pMem->flags = MEM_Str|MEM_Term;
77744 pMem->n = 2;
77745 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */
77746 pMem->enc = SQLITE_UTF8;
77747 pMem++;
77748
77749#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
77750 if( sqlite3VdbeMemClearAndResize(pMem, 500) ){
77751 assert( p->db->mallocFailed );
77752 return SQLITE_ERROR;
77753 }
77754 pMem->flags = MEM_Str|MEM_Term;
77755 pMem->n = displayComment(pOp, zP4, pMem->z, 500);
77756 pMem->enc = SQLITE_UTF8;
77757#else
77758 pMem->flags = MEM_Null; /* Comment */
77759#endif
77760 }
77761
77762 p->nResColumn = 8 - 4*(p->explain-1);
77763 p->pResultSet = &p->aMem[1];
77764 p->rc = SQLITE_OK;
77765 rc = SQLITE_ROW;
77766 }
77767 }
77768 return rc;
77769}
77770#endif /* SQLITE_OMIT_EXPLAIN */
77771
77772#ifdef SQLITE_DEBUG
77773/*
77774** Print the SQL that was used to generate a VDBE program.
77775*/
77776SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe *p){
77777 const char *z = 0;
77778 if( p->zSql ){
77779 z = p->zSql;
77780 }else if( p->nOp>=1 ){
77781 const VdbeOp *pOp = &p->aOp[0];
77782 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
77783 z = pOp->p4.z;
77784 while( sqlite3Isspace(*z) ) z++;
77785 }
77786 }
77787 if( z ) printf("SQL: [%s]\n", z);
77788}
77789#endif
77790
77791#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
77792/*
77793** Print an IOTRACE message showing SQL content.
77794*/
77795SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe *p){
77796 int nOp = p->nOp;
77797 VdbeOp *pOp;
77798 if( sqlite3IoTrace==0 ) return;
77799 if( nOp<1 ) return;
77800 pOp = &p->aOp[0];
77801 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
77802 int i, j;
77803 char z[1000];
77804 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
77805 for(i=0; sqlite3Isspace(z[i]); i++){}
77806 for(j=0; z[i]; i++){
77807 if( sqlite3Isspace(z[i]) ){
77808 if( z[i-1]!=' ' ){
77809 z[j++] = ' ';
77810 }
77811 }else{
77812 z[j++] = z[i];
77813 }
77814 }
77815 z[j] = 0;
77816 sqlite3IoTrace("SQL %s\n", z);
77817 }
77818}
77819#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
77820
77821/* An instance of this object describes bulk memory available for use
77822** by subcomponents of a prepared statement. Space is allocated out
77823** of a ReusableSpace object by the allocSpace() routine below.
77824*/
77825struct ReusableSpace {
77826 u8 *pSpace; /* Available memory */
77827 int nFree; /* Bytes of available memory */
77828 int nNeeded; /* Total bytes that could not be allocated */
77829};
77830
77831/* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
77832** from the ReusableSpace object. Return a pointer to the allocated
77833** memory on success. If insufficient memory is available in the
77834** ReusableSpace object, increase the ReusableSpace.nNeeded
77835** value by the amount needed and return NULL.
77836**
77837** If pBuf is not initially NULL, that means that the memory has already
77838** been allocated by a prior call to this routine, so just return a copy
77839** of pBuf and leave ReusableSpace unchanged.
77840**
77841** This allocator is employed to repurpose unused slots at the end of the
77842** opcode array of prepared state for other memory needs of the prepared
77843** statement.
77844*/
77845static void *allocSpace(
77846 struct ReusableSpace *p, /* Bulk memory available for allocation */
77847 void *pBuf, /* Pointer to a prior allocation */
77848 int nByte /* Bytes of memory needed */
77849){
77850 assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
77851 if( pBuf==0 ){
77852 nByte = ROUND8(nByte);
77853 if( nByte <= p->nFree ){
77854 p->nFree -= nByte;
77855 pBuf = &p->pSpace[p->nFree];
77856 }else{
77857 p->nNeeded += nByte;
77858 }
77859 }
77860 assert( EIGHT_BYTE_ALIGNMENT(pBuf) );
77861 return pBuf;
77862}
77863
77864/*
77865** Rewind the VDBE back to the beginning in preparation for
77866** running it.
77867*/
77868SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe *p){
77869#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
77870 int i;
77871#endif
77872 assert( p!=0 );
77873 assert( p->magic==VDBE_MAGIC_INIT || p->magic==VDBE_MAGIC_RESET );
77874
77875 /* There should be at least one opcode.
77876 */
77877 assert( p->nOp>0 );
77878
77879 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
77880 p->magic = VDBE_MAGIC_RUN;
77881
77882#ifdef SQLITE_DEBUG
77883 for(i=0; i<p->nMem; i++){
77884 assert( p->aMem[i].db==p->db );
77885 }
77886#endif
77887 p->pc = -1;
77888 p->rc = SQLITE_OK;
77889 p->errorAction = OE_Abort;
77890 p->nChange = 0;
77891 p->cacheCtr = 1;
77892 p->minWriteFileFormat = 255;
77893 p->iStatement = 0;
77894 p->nFkConstraint = 0;
77895#ifdef VDBE_PROFILE
77896 for(i=0; i<p->nOp; i++){
77897 p->aOp[i].cnt = 0;
77898 p->aOp[i].cycles = 0;
77899 }
77900#endif
77901}
77902
77903/*
77904** Prepare a virtual machine for execution for the first time after
77905** creating the virtual machine. This involves things such
77906** as allocating registers and initializing the program counter.
77907** After the VDBE has be prepped, it can be executed by one or more
77908** calls to sqlite3VdbeExec().
77909**
77910** This function may be called exactly once on each virtual machine.
77911** After this routine is called the VM has been "packaged" and is ready
77912** to run. After this routine is called, further calls to
77913** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects
77914** the Vdbe from the Parse object that helped generate it so that the
77915** the Vdbe becomes an independent entity and the Parse object can be
77916** destroyed.
77917**
77918** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
77919** to its initial state after it has been run.
77920*/
77921SQLITE_PRIVATE void sqlite3VdbeMakeReady(
77922 Vdbe *p, /* The VDBE */
77923 Parse *pParse /* Parsing context */
77924){
77925 sqlite3 *db; /* The database connection */
77926 int nVar; /* Number of parameters */
77927 int nMem; /* Number of VM memory registers */
77928 int nCursor; /* Number of cursors required */
77929 int nArg; /* Number of arguments in subprograms */
77930 int n; /* Loop counter */
77931 struct ReusableSpace x; /* Reusable bulk memory */
77932
77933 assert( p!=0 );
77934 assert( p->nOp>0 );
77935 assert( pParse!=0 );
77936 assert( p->magic==VDBE_MAGIC_INIT );
77937 assert( pParse==p->pParse );
77938 db = p->db;
77939 assert( db->mallocFailed==0 );
77940 nVar = pParse->nVar;
77941 nMem = pParse->nMem;
77942 nCursor = pParse->nTab;
77943 nArg = pParse->nMaxArg;
77944
77945 /* Each cursor uses a memory cell. The first cursor (cursor 0) can
77946 ** use aMem[0] which is not otherwise used by the VDBE program. Allocate
77947 ** space at the end of aMem[] for cursors 1 and greater.
77948 ** See also: allocateCursor().
77949 */
77950 nMem += nCursor;
77951 if( nCursor==0 && nMem>0 ) nMem++; /* Space for aMem[0] even if not used */
77952
77953 /* Figure out how much reusable memory is available at the end of the
77954 ** opcode array. This extra memory will be reallocated for other elements
77955 ** of the prepared statement.
77956 */
77957 n = ROUND8(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */
77958 x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */
77959 assert( EIGHT_BYTE_ALIGNMENT(x.pSpace) );
77960 x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused memory */
77961 assert( x.nFree>=0 );
77962 assert( EIGHT_BYTE_ALIGNMENT(&x.pSpace[x.nFree]) );
77963
77964 resolveP2Values(p, &nArg);
77965 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
77966 if( pParse->explain && nMem<10 ){
77967 nMem = 10;
77968 }
77969 p->expired = 0;
77970
77971 /* Memory for registers, parameters, cursor, etc, is allocated in one or two
77972 ** passes. On the first pass, we try to reuse unused memory at the
77973 ** end of the opcode array. If we are unable to satisfy all memory
77974 ** requirements by reusing the opcode array tail, then the second
77975 ** pass will fill in the remainder using a fresh memory allocation.
77976 **
77977 ** This two-pass approach that reuses as much memory as possible from
77978 ** the leftover memory at the end of the opcode array. This can significantly
77979 ** reduce the amount of memory held by a prepared statement.
77980 */
77981 do {
77982 x.nNeeded = 0;
77983 p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
77984 p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
77985 p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
77986 p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
77987#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
77988 p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64));
77989#endif
77990 if( x.nNeeded==0 ) break;
77991 x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded);
77992 x.nFree = x.nNeeded;
77993 }while( !db->mallocFailed );
77994
77995 p->pVList = pParse->pVList;
77996 pParse->pVList = 0;
77997 p->explain = pParse->explain;
77998 if( db->mallocFailed ){
77999 p->nVar = 0;
78000 p->nCursor = 0;
78001 p->nMem = 0;
78002 }else{
78003 p->nCursor = nCursor;
78004 p->nVar = (ynVar)nVar;
78005 initMemArray(p->aVar, nVar, db, MEM_Null);
78006 p->nMem = nMem;
78007 initMemArray(p->aMem, nMem, db, MEM_Undefined);
78008 memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));
78009#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
78010 memset(p->anExec, 0, p->nOp*sizeof(i64));
78011#endif
78012 }
78013 sqlite3VdbeRewind(p);
78014}
78015
78016/*
78017** Close a VDBE cursor and release all the resources that cursor
78018** happens to hold.
78019*/
78020SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
78021 if( pCx==0 ){
78022 return;
78023 }
78024 assert( pCx->pBtx==0 || pCx->eCurType==CURTYPE_BTREE );
78025 switch( pCx->eCurType ){
78026 case CURTYPE_SORTER: {
78027 sqlite3VdbeSorterClose(p->db, pCx);
78028 break;
78029 }
78030 case CURTYPE_BTREE: {
78031 if( pCx->isEphemeral ){
78032 if( pCx->pBtx ) sqlite3BtreeClose(pCx->pBtx);
78033 /* The pCx->pCursor will be close automatically, if it exists, by
78034 ** the call above. */
78035 }else{
78036 assert( pCx->uc.pCursor!=0 );
78037 sqlite3BtreeCloseCursor(pCx->uc.pCursor);
78038 }
78039 break;
78040 }
78041#ifndef SQLITE_OMIT_VIRTUALTABLE
78042 case CURTYPE_VTAB: {
78043 sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
78044 const sqlite3_module *pModule = pVCur->pVtab->pModule;
78045 assert( pVCur->pVtab->nRef>0 );
78046 pVCur->pVtab->nRef--;
78047 pModule->xClose(pVCur);
78048 break;
78049 }
78050#endif
78051 }
78052}
78053
78054/*
78055** Close all cursors in the current frame.
78056*/
78057static void closeCursorsInFrame(Vdbe *p){
78058 if( p->apCsr ){
78059 int i;
78060 for(i=0; i<p->nCursor; i++){
78061 VdbeCursor *pC = p->apCsr[i];
78062 if( pC ){
78063 sqlite3VdbeFreeCursor(p, pC);
78064 p->apCsr[i] = 0;
78065 }
78066 }
78067 }
78068}
78069
78070/*
78071** Copy the values stored in the VdbeFrame structure to its Vdbe. This
78072** is used, for example, when a trigger sub-program is halted to restore
78073** control to the main program.
78074*/
78075SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
78076 Vdbe *v = pFrame->v;
78077 closeCursorsInFrame(v);
78078#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
78079 v->anExec = pFrame->anExec;
78080#endif
78081 v->aOp = pFrame->aOp;
78082 v->nOp = pFrame->nOp;
78083 v->aMem = pFrame->aMem;
78084 v->nMem = pFrame->nMem;
78085 v->apCsr = pFrame->apCsr;
78086 v->nCursor = pFrame->nCursor;
78087 v->db->lastRowid = pFrame->lastRowid;
78088 v->nChange = pFrame->nChange;
78089 v->db->nChange = pFrame->nDbChange;
78090 sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0);
78091 v->pAuxData = pFrame->pAuxData;
78092 pFrame->pAuxData = 0;
78093 return pFrame->pc;
78094}
78095
78096/*
78097** Close all cursors.
78098**
78099** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
78100** cell array. This is necessary as the memory cell array may contain
78101** pointers to VdbeFrame objects, which may in turn contain pointers to
78102** open cursors.
78103*/
78104static void closeAllCursors(Vdbe *p){
78105 if( p->pFrame ){
78106 VdbeFrame *pFrame;
78107 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
78108 sqlite3VdbeFrameRestore(pFrame);
78109 p->pFrame = 0;
78110 p->nFrame = 0;
78111 }
78112 assert( p->nFrame==0 );
78113 closeCursorsInFrame(p);
78114 if( p->aMem ){
78115 releaseMemArray(p->aMem, p->nMem);
78116 }
78117 while( p->pDelFrame ){
78118 VdbeFrame *pDel = p->pDelFrame;
78119 p->pDelFrame = pDel->pParent;
78120 sqlite3VdbeFrameDelete(pDel);
78121 }
78122
78123 /* Delete any auxdata allocations made by the VM */
78124 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
78125 assert( p->pAuxData==0 );
78126}
78127
78128/*
78129** Set the number of result columns that will be returned by this SQL
78130** statement. This is now set at compile time, rather than during
78131** execution of the vdbe program so that sqlite3_column_count() can
78132** be called on an SQL statement before sqlite3_step().
78133*/
78134SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
78135 int n;
78136 sqlite3 *db = p->db;
78137
78138 if( p->nResColumn ){
78139 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
78140 sqlite3DbFree(db, p->aColName);
78141 }
78142 n = nResColumn*COLNAME_N;
78143 p->nResColumn = (u16)nResColumn;
78144 p->aColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n );
78145 if( p->aColName==0 ) return;
78146 initMemArray(p->aColName, n, db, MEM_Null);
78147}
78148
78149/*
78150** Set the name of the idx'th column to be returned by the SQL statement.
78151** zName must be a pointer to a nul terminated string.
78152**
78153** This call must be made after a call to sqlite3VdbeSetNumCols().
78154**
78155** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
78156** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
78157** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
78158*/
78159SQLITE_PRIVATE int sqlite3VdbeSetColName(
78160 Vdbe *p, /* Vdbe being configured */
78161 int idx, /* Index of column zName applies to */
78162 int var, /* One of the COLNAME_* constants */
78163 const char *zName, /* Pointer to buffer containing name */
78164 void (*xDel)(void*) /* Memory management strategy for zName */
78165){
78166 int rc;
78167 Mem *pColName;
78168 assert( idx<p->nResColumn );
78169 assert( var<COLNAME_N );
78170 if( p->db->mallocFailed ){
78171 assert( !zName || xDel!=SQLITE_DYNAMIC );
78172 return SQLITE_NOMEM_BKPT;
78173 }
78174 assert( p->aColName!=0 );
78175 pColName = &(p->aColName[idx+var*p->nResColumn]);
78176 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
78177 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
78178 return rc;
78179}
78180
78181/*
78182** A read or write transaction may or may not be active on database handle
78183** db. If a transaction is active, commit it. If there is a
78184** write-transaction spanning more than one database file, this routine
78185** takes care of the master journal trickery.
78186*/
78187static int vdbeCommit(sqlite3 *db, Vdbe *p){
78188 int i;
78189 int nTrans = 0; /* Number of databases with an active write-transaction
78190 ** that are candidates for a two-phase commit using a
78191 ** master-journal */
78192 int rc = SQLITE_OK;
78193 int needXcommit = 0;
78194
78195#ifdef SQLITE_OMIT_VIRTUALTABLE
78196 /* With this option, sqlite3VtabSync() is defined to be simply
78197 ** SQLITE_OK so p is not used.
78198 */
78199 UNUSED_PARAMETER(p);
78200#endif
78201
78202 /* Before doing anything else, call the xSync() callback for any
78203 ** virtual module tables written in this transaction. This has to
78204 ** be done before determining whether a master journal file is
78205 ** required, as an xSync() callback may add an attached database
78206 ** to the transaction.
78207 */
78208 rc = sqlite3VtabSync(db, p);
78209
78210 /* This loop determines (a) if the commit hook should be invoked and
78211 ** (b) how many database files have open write transactions, not
78212 ** including the temp database. (b) is important because if more than
78213 ** one database file has an open write transaction, a master journal
78214 ** file is required for an atomic commit.
78215 */
78216 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
78217 Btree *pBt = db->aDb[i].pBt;
78218 if( sqlite3BtreeIsInTrans(pBt) ){
78219 /* Whether or not a database might need a master journal depends upon
78220 ** its journal mode (among other things). This matrix determines which
78221 ** journal modes use a master journal and which do not */
78222 static const u8 aMJNeeded[] = {
78223 /* DELETE */ 1,
78224 /* PERSIST */ 1,
78225 /* OFF */ 0,
78226 /* TRUNCATE */ 1,
78227 /* MEMORY */ 0,
78228 /* WAL */ 0
78229 };
78230 Pager *pPager; /* Pager associated with pBt */
78231 needXcommit = 1;
78232 sqlite3BtreeEnter(pBt);
78233 pPager = sqlite3BtreePager(pBt);
78234 if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF
78235 && aMJNeeded[sqlite3PagerGetJournalMode(pPager)]
78236 && sqlite3PagerIsMemdb(pPager)==0
78237 ){
78238 assert( i!=1 );
78239 nTrans++;
78240 }
78241 rc = sqlite3PagerExclusiveLock(pPager);
78242 sqlite3BtreeLeave(pBt);
78243 }
78244 }
78245 if( rc!=SQLITE_OK ){
78246 return rc;
78247 }
78248
78249 /* If there are any write-transactions at all, invoke the commit hook */
78250 if( needXcommit && db->xCommitCallback ){
78251 rc = db->xCommitCallback(db->pCommitArg);
78252 if( rc ){
78253 return SQLITE_CONSTRAINT_COMMITHOOK;
78254 }
78255 }
78256
78257 /* The simple case - no more than one database file (not counting the
78258 ** TEMP database) has a transaction active. There is no need for the
78259 ** master-journal.
78260 **
78261 ** If the return value of sqlite3BtreeGetFilename() is a zero length
78262 ** string, it means the main database is :memory: or a temp file. In
78263 ** that case we do not support atomic multi-file commits, so use the
78264 ** simple case then too.
78265 */
78266 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
78267 || nTrans<=1
78268 ){
78269 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
78270 Btree *pBt = db->aDb[i].pBt;
78271 if( pBt ){
78272 rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
78273 }
78274 }
78275
78276 /* Do the commit only if all databases successfully complete phase 1.
78277 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
78278 ** IO error while deleting or truncating a journal file. It is unlikely,
78279 ** but could happen. In this case abandon processing and return the error.
78280 */
78281 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
78282 Btree *pBt = db->aDb[i].pBt;
78283 if( pBt ){
78284 rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
78285 }
78286 }
78287 if( rc==SQLITE_OK ){
78288 sqlite3VtabCommit(db);
78289 }
78290 }
78291
78292 /* The complex case - There is a multi-file write-transaction active.
78293 ** This requires a master journal file to ensure the transaction is
78294 ** committed atomically.
78295 */
78296#ifndef SQLITE_OMIT_DISKIO
78297 else{
78298 sqlite3_vfs *pVfs = db->pVfs;
78299 char *zMaster = 0; /* File-name for the master journal */
78300 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
78301 sqlite3_file *pMaster = 0;
78302 i64 offset = 0;
78303 int res;
78304 int retryCount = 0;
78305 int nMainFile;
78306
78307 /* Select a master journal file name */
78308 nMainFile = sqlite3Strlen30(zMainFile);
78309 zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
78310 if( zMaster==0 ) return SQLITE_NOMEM_BKPT;
78311 do {
78312 u32 iRandom;
78313 if( retryCount ){
78314 if( retryCount>100 ){
78315 sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster);
78316 sqlite3OsDelete(pVfs, zMaster, 0);
78317 break;
78318 }else if( retryCount==1 ){
78319 sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster);
78320 }
78321 }
78322 retryCount++;
78323 sqlite3_randomness(sizeof(iRandom), &iRandom);
78324 sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
78325 (iRandom>>8)&0xffffff, iRandom&0xff);
78326 /* The antipenultimate character of the master journal name must
78327 ** be "9" to avoid name collisions when using 8+3 filenames. */
78328 assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
78329 sqlite3FileSuffix3(zMainFile, zMaster);
78330 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
78331 }while( rc==SQLITE_OK && res );
78332 if( rc==SQLITE_OK ){
78333 /* Open the master journal. */
78334 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
78335 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
78336 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
78337 );
78338 }
78339 if( rc!=SQLITE_OK ){
78340 sqlite3DbFree(db, zMaster);
78341 return rc;
78342 }
78343
78344 /* Write the name of each database file in the transaction into the new
78345 ** master journal file. If an error occurs at this point close
78346 ** and delete the master journal file. All the individual journal files
78347 ** still have 'null' as the master journal pointer, so they will roll
78348 ** back independently if a failure occurs.
78349 */
78350 for(i=0; i<db->nDb; i++){
78351 Btree *pBt = db->aDb[i].pBt;
78352 if( sqlite3BtreeIsInTrans(pBt) ){
78353 char const *zFile = sqlite3BtreeGetJournalname(pBt);
78354 if( zFile==0 ){
78355 continue; /* Ignore TEMP and :memory: databases */
78356 }
78357 assert( zFile[0]!=0 );
78358 rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
78359 offset += sqlite3Strlen30(zFile)+1;
78360 if( rc!=SQLITE_OK ){
78361 sqlite3OsCloseFree(pMaster);
78362 sqlite3OsDelete(pVfs, zMaster, 0);
78363 sqlite3DbFree(db, zMaster);
78364 return rc;
78365 }
78366 }
78367 }
78368
78369 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
78370 ** flag is set this is not required.
78371 */
78372 if( 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
78373 && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
78374 ){
78375 sqlite3OsCloseFree(pMaster);
78376 sqlite3OsDelete(pVfs, zMaster, 0);
78377 sqlite3DbFree(db, zMaster);
78378 return rc;
78379 }
78380
78381 /* Sync all the db files involved in the transaction. The same call
78382 ** sets the master journal pointer in each individual journal. If
78383 ** an error occurs here, do not delete the master journal file.
78384 **
78385 ** If the error occurs during the first call to
78386 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
78387 ** master journal file will be orphaned. But we cannot delete it,
78388 ** in case the master journal file name was written into the journal
78389 ** file before the failure occurred.
78390 */
78391 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
78392 Btree *pBt = db->aDb[i].pBt;
78393 if( pBt ){
78394 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
78395 }
78396 }
78397 sqlite3OsCloseFree(pMaster);
78398 assert( rc!=SQLITE_BUSY );
78399 if( rc!=SQLITE_OK ){
78400 sqlite3DbFree(db, zMaster);
78401 return rc;
78402 }
78403
78404 /* Delete the master journal file. This commits the transaction. After
78405 ** doing this the directory is synced again before any individual
78406 ** transaction files are deleted.
78407 */
78408 rc = sqlite3OsDelete(pVfs, zMaster, 1);
78409 sqlite3DbFree(db, zMaster);
78410 zMaster = 0;
78411 if( rc ){
78412 return rc;
78413 }
78414
78415 /* All files and directories have already been synced, so the following
78416 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
78417 ** deleting or truncating journals. If something goes wrong while
78418 ** this is happening we don't really care. The integrity of the
78419 ** transaction is already guaranteed, but some stray 'cold' journals
78420 ** may be lying around. Returning an error code won't help matters.
78421 */
78422 disable_simulated_io_errors();
78423 sqlite3BeginBenignMalloc();
78424 for(i=0; i<db->nDb; i++){
78425 Btree *pBt = db->aDb[i].pBt;
78426 if( pBt ){
78427 sqlite3BtreeCommitPhaseTwo(pBt, 1);
78428 }
78429 }
78430 sqlite3EndBenignMalloc();
78431 enable_simulated_io_errors();
78432
78433 sqlite3VtabCommit(db);
78434 }
78435#endif
78436
78437 return rc;
78438}
78439
78440/*
78441** This routine checks that the sqlite3.nVdbeActive count variable
78442** matches the number of vdbe's in the list sqlite3.pVdbe that are
78443** currently active. An assertion fails if the two counts do not match.
78444** This is an internal self-check only - it is not an essential processing
78445** step.
78446**
78447** This is a no-op if NDEBUG is defined.
78448*/
78449#ifndef NDEBUG
78450static void checkActiveVdbeCnt(sqlite3 *db){
78451 Vdbe *p;
78452 int cnt = 0;
78453 int nWrite = 0;
78454 int nRead = 0;
78455 p = db->pVdbe;
78456 while( p ){
78457 if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){
78458 cnt++;
78459 if( p->readOnly==0 ) nWrite++;
78460 if( p->bIsReader ) nRead++;
78461 }
78462 p = p->pNext;
78463 }
78464 assert( cnt==db->nVdbeActive );
78465 assert( nWrite==db->nVdbeWrite );
78466 assert( nRead==db->nVdbeRead );
78467}
78468#else
78469#define checkActiveVdbeCnt(x)
78470#endif
78471
78472/*
78473** If the Vdbe passed as the first argument opened a statement-transaction,
78474** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
78475** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
78476** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
78477** statement transaction is committed.
78478**
78479** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
78480** Otherwise SQLITE_OK.
78481*/
78482static SQLITE_NOINLINE int vdbeCloseStatement(Vdbe *p, int eOp){
78483 sqlite3 *const db = p->db;
78484 int rc = SQLITE_OK;
78485 int i;
78486 const int iSavepoint = p->iStatement-1;
78487
78488 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
78489 assert( db->nStatement>0 );
78490 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
78491
78492 for(i=0; i<db->nDb; i++){
78493 int rc2 = SQLITE_OK;
78494 Btree *pBt = db->aDb[i].pBt;
78495 if( pBt ){
78496 if( eOp==SAVEPOINT_ROLLBACK ){
78497 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
78498 }
78499 if( rc2==SQLITE_OK ){
78500 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
78501 }
78502 if( rc==SQLITE_OK ){
78503 rc = rc2;
78504 }
78505 }
78506 }
78507 db->nStatement--;
78508 p->iStatement = 0;
78509
78510 if( rc==SQLITE_OK ){
78511 if( eOp==SAVEPOINT_ROLLBACK ){
78512 rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
78513 }
78514 if( rc==SQLITE_OK ){
78515 rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
78516 }
78517 }
78518
78519 /* If the statement transaction is being rolled back, also restore the
78520 ** database handles deferred constraint counter to the value it had when
78521 ** the statement transaction was opened. */
78522 if( eOp==SAVEPOINT_ROLLBACK ){
78523 db->nDeferredCons = p->nStmtDefCons;
78524 db->nDeferredImmCons = p->nStmtDefImmCons;
78525 }
78526 return rc;
78527}
78528SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
78529 if( p->db->nStatement && p->iStatement ){
78530 return vdbeCloseStatement(p, eOp);
78531 }
78532 return SQLITE_OK;
78533}
78534
78535
78536/*
78537** This function is called when a transaction opened by the database
78538** handle associated with the VM passed as an argument is about to be
78539** committed. If there are outstanding deferred foreign key constraint
78540** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
78541**
78542** If there are outstanding FK violations and this function returns
78543** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
78544** and write an error message to it. Then return SQLITE_ERROR.
78545*/
78546#ifndef SQLITE_OMIT_FOREIGN_KEY
78547SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
78548 sqlite3 *db = p->db;
78549 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
78550 || (!deferred && p->nFkConstraint>0)
78551 ){
78552 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
78553 p->errorAction = OE_Abort;
78554 sqlite3VdbeError(p, "FOREIGN KEY constraint failed");
78555 return SQLITE_ERROR;
78556 }
78557 return SQLITE_OK;
78558}
78559#endif
78560
78561/*
78562** This routine is called the when a VDBE tries to halt. If the VDBE
78563** has made changes and is in autocommit mode, then commit those
78564** changes. If a rollback is needed, then do the rollback.
78565**
78566** This routine is the only way to move the state of a VM from
78567** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
78568** call this on a VM that is in the SQLITE_MAGIC_HALT state.
78569**
78570** Return an error code. If the commit could not complete because of
78571** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
78572** means the close did not happen and needs to be repeated.
78573*/
78574SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe *p){
78575 int rc; /* Used to store transient return codes */
78576 sqlite3 *db = p->db;
78577
78578 /* This function contains the logic that determines if a statement or
78579 ** transaction will be committed or rolled back as a result of the
78580 ** execution of this virtual machine.
78581 **
78582 ** If any of the following errors occur:
78583 **
78584 ** SQLITE_NOMEM
78585 ** SQLITE_IOERR
78586 ** SQLITE_FULL
78587 ** SQLITE_INTERRUPT
78588 **
78589 ** Then the internal cache might have been left in an inconsistent
78590 ** state. We need to rollback the statement transaction, if there is
78591 ** one, or the complete transaction if there is no statement transaction.
78592 */
78593
78594 if( p->magic!=VDBE_MAGIC_RUN ){
78595 return SQLITE_OK;
78596 }
78597 if( db->mallocFailed ){
78598 p->rc = SQLITE_NOMEM_BKPT;
78599 }
78600 closeAllCursors(p);
78601 checkActiveVdbeCnt(db);
78602
78603 /* No commit or rollback needed if the program never started or if the
78604 ** SQL statement does not read or write a database file. */
78605 if( p->pc>=0 && p->bIsReader ){
78606 int mrc; /* Primary error code from p->rc */
78607 int eStatementOp = 0;
78608 int isSpecialError; /* Set to true if a 'special' error */
78609
78610 /* Lock all btrees used by the statement */
78611 sqlite3VdbeEnter(p);
78612
78613 /* Check for one of the special errors */
78614 mrc = p->rc & 0xff;
78615 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
78616 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
78617 if( isSpecialError ){
78618 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
78619 ** no rollback is necessary. Otherwise, at least a savepoint
78620 ** transaction must be rolled back to restore the database to a
78621 ** consistent state.
78622 **
78623 ** Even if the statement is read-only, it is important to perform
78624 ** a statement or transaction rollback operation. If the error
78625 ** occurred while writing to the journal, sub-journal or database
78626 ** file as part of an effort to free up cache space (see function
78627 ** pagerStress() in pager.c), the rollback is required to restore
78628 ** the pager to a consistent state.
78629 */
78630 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
78631 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
78632 eStatementOp = SAVEPOINT_ROLLBACK;
78633 }else{
78634 /* We are forced to roll back the active transaction. Before doing
78635 ** so, abort any other statements this handle currently has active.
78636 */
78637 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
78638 sqlite3CloseSavepoints(db);
78639 db->autoCommit = 1;
78640 p->nChange = 0;
78641 }
78642 }
78643 }
78644
78645 /* Check for immediate foreign key violations. */
78646 if( p->rc==SQLITE_OK ){
78647 sqlite3VdbeCheckFk(p, 0);
78648 }
78649
78650 /* If the auto-commit flag is set and this is the only active writer
78651 ** VM, then we do either a commit or rollback of the current transaction.
78652 **
78653 ** Note: This block also runs if one of the special errors handled
78654 ** above has occurred.
78655 */
78656 if( !sqlite3VtabInSync(db)
78657 && db->autoCommit
78658 && db->nVdbeWrite==(p->readOnly==0)
78659 ){
78660 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
78661 rc = sqlite3VdbeCheckFk(p, 1);
78662 if( rc!=SQLITE_OK ){
78663 if( NEVER(p->readOnly) ){
78664 sqlite3VdbeLeave(p);
78665 return SQLITE_ERROR;
78666 }
78667 rc = SQLITE_CONSTRAINT_FOREIGNKEY;
78668 }else{
78669 /* The auto-commit flag is true, the vdbe program was successful
78670 ** or hit an 'OR FAIL' constraint and there are no deferred foreign
78671 ** key constraints to hold up the transaction. This means a commit
78672 ** is required. */
78673 rc = vdbeCommit(db, p);
78674 }
78675 if( rc==SQLITE_BUSY && p->readOnly ){
78676 sqlite3VdbeLeave(p);
78677 return SQLITE_BUSY;
78678 }else if( rc!=SQLITE_OK ){
78679 p->rc = rc;
78680 sqlite3RollbackAll(db, SQLITE_OK);
78681 p->nChange = 0;
78682 }else{
78683 db->nDeferredCons = 0;
78684 db->nDeferredImmCons = 0;
78685 db->flags &= ~SQLITE_DeferFKs;
78686 sqlite3CommitInternalChanges(db);
78687 }
78688 }else{
78689 sqlite3RollbackAll(db, SQLITE_OK);
78690 p->nChange = 0;
78691 }
78692 db->nStatement = 0;
78693 }else if( eStatementOp==0 ){
78694 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
78695 eStatementOp = SAVEPOINT_RELEASE;
78696 }else if( p->errorAction==OE_Abort ){
78697 eStatementOp = SAVEPOINT_ROLLBACK;
78698 }else{
78699 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
78700 sqlite3CloseSavepoints(db);
78701 db->autoCommit = 1;
78702 p->nChange = 0;
78703 }
78704 }
78705
78706 /* If eStatementOp is non-zero, then a statement transaction needs to
78707 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
78708 ** do so. If this operation returns an error, and the current statement
78709 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
78710 ** current statement error code.
78711 */
78712 if( eStatementOp ){
78713 rc = sqlite3VdbeCloseStatement(p, eStatementOp);
78714 if( rc ){
78715 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
78716 p->rc = rc;
78717 sqlite3DbFree(db, p->zErrMsg);
78718 p->zErrMsg = 0;
78719 }
78720 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
78721 sqlite3CloseSavepoints(db);
78722 db->autoCommit = 1;
78723 p->nChange = 0;
78724 }
78725 }
78726
78727 /* If this was an INSERT, UPDATE or DELETE and no statement transaction
78728 ** has been rolled back, update the database connection change-counter.
78729 */
78730 if( p->changeCntOn ){
78731 if( eStatementOp!=SAVEPOINT_ROLLBACK ){
78732 sqlite3VdbeSetChanges(db, p->nChange);
78733 }else{
78734 sqlite3VdbeSetChanges(db, 0);
78735 }
78736 p->nChange = 0;
78737 }
78738
78739 /* Release the locks */
78740 sqlite3VdbeLeave(p);
78741 }
78742
78743 /* We have successfully halted and closed the VM. Record this fact. */
78744 if( p->pc>=0 ){
78745 db->nVdbeActive--;
78746 if( !p->readOnly ) db->nVdbeWrite--;
78747 if( p->bIsReader ) db->nVdbeRead--;
78748 assert( db->nVdbeActive>=db->nVdbeRead );
78749 assert( db->nVdbeRead>=db->nVdbeWrite );
78750 assert( db->nVdbeWrite>=0 );
78751 }
78752 p->magic = VDBE_MAGIC_HALT;
78753 checkActiveVdbeCnt(db);
78754 if( db->mallocFailed ){
78755 p->rc = SQLITE_NOMEM_BKPT;
78756 }
78757
78758 /* If the auto-commit flag is set to true, then any locks that were held
78759 ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
78760 ** to invoke any required unlock-notify callbacks.
78761 */
78762 if( db->autoCommit ){
78763 sqlite3ConnectionUnlocked(db);
78764 }
78765
78766 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
78767 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
78768}
78769
78770
78771/*
78772** Each VDBE holds the result of the most recent sqlite3_step() call
78773** in p->rc. This routine sets that result back to SQLITE_OK.
78774*/
78775SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe *p){
78776 p->rc = SQLITE_OK;
78777}
78778
78779/*
78780** Copy the error code and error message belonging to the VDBE passed
78781** as the first argument to its database handle (so that they will be
78782** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
78783**
78784** This function does not clear the VDBE error code or message, just
78785** copies them to the database handle.
78786*/
78787SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p){
78788 sqlite3 *db = p->db;
78789 int rc = p->rc;
78790 if( p->zErrMsg ){
78791 db->bBenignMalloc++;
78792 sqlite3BeginBenignMalloc();
78793 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
78794 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
78795 sqlite3EndBenignMalloc();
78796 db->bBenignMalloc--;
78797 }else if( db->pErr ){
78798 sqlite3ValueSetNull(db->pErr);
78799 }
78800 db->errCode = rc;
78801 return rc;
78802}
78803
78804#ifdef SQLITE_ENABLE_SQLLOG
78805/*
78806** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run,
78807** invoke it.
78808*/
78809static void vdbeInvokeSqllog(Vdbe *v){
78810 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
78811 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
78812 assert( v->db->init.busy==0 );
78813 if( zExpanded ){
78814 sqlite3GlobalConfig.xSqllog(
78815 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
78816 );
78817 sqlite3DbFree(v->db, zExpanded);
78818 }
78819 }
78820}
78821#else
78822# define vdbeInvokeSqllog(x)
78823#endif
78824
78825/*
78826** Clean up a VDBE after execution but do not delete the VDBE just yet.
78827** Write any error messages into *pzErrMsg. Return the result code.
78828**
78829** After this routine is run, the VDBE should be ready to be executed
78830** again.
78831**
78832** To look at it another way, this routine resets the state of the
78833** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
78834** VDBE_MAGIC_INIT.
78835*/
78836SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
78837#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
78838 int i;
78839#endif
78840
78841 sqlite3 *db;
78842 db = p->db;
78843
78844 /* If the VM did not run to completion or if it encountered an
78845 ** error, then it might not have been halted properly. So halt
78846 ** it now.
78847 */
78848 sqlite3VdbeHalt(p);
78849
78850 /* If the VDBE has been run even partially, then transfer the error code
78851 ** and error message from the VDBE into the main database structure. But
78852 ** if the VDBE has just been set to run but has not actually executed any
78853 ** instructions yet, leave the main database error information unchanged.
78854 */
78855 if( p->pc>=0 ){
78856 vdbeInvokeSqllog(p);
78857 sqlite3VdbeTransferError(p);
78858 if( p->runOnlyOnce ) p->expired = 1;
78859 }else if( p->rc && p->expired ){
78860 /* The expired flag was set on the VDBE before the first call
78861 ** to sqlite3_step(). For consistency (since sqlite3_step() was
78862 ** called), set the database error in this case as well.
78863 */
78864 sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
78865 }
78866
78867 /* Reset register contents and reclaim error message memory.
78868 */
78869#ifdef SQLITE_DEBUG
78870 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
78871 ** Vdbe.aMem[] arrays have already been cleaned up. */
78872 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
78873 if( p->aMem ){
78874 for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
78875 }
78876#endif
78877 sqlite3DbFree(db, p->zErrMsg);
78878 p->zErrMsg = 0;
78879 p->pResultSet = 0;
78880#ifdef SQLITE_DEBUG
78881 p->nWrite = 0;
78882#endif
78883
78884 /* Save profiling information from this VDBE run.
78885 */
78886#ifdef VDBE_PROFILE
78887 {
78888 FILE *out = fopen("vdbe_profile.out", "a");
78889 if( out ){
78890 fprintf(out, "---- ");
78891 for(i=0; i<p->nOp; i++){
78892 fprintf(out, "%02x", p->aOp[i].opcode);
78893 }
78894 fprintf(out, "\n");
78895 if( p->zSql ){
78896 char c, pc = 0;
78897 fprintf(out, "-- ");
78898 for(i=0; (c = p->zSql[i])!=0; i++){
78899 if( pc=='\n' ) fprintf(out, "-- ");
78900 putc(c, out);
78901 pc = c;
78902 }
78903 if( pc!='\n' ) fprintf(out, "\n");
78904 }
78905 for(i=0; i<p->nOp; i++){
78906 char zHdr[100];
78907 sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ",
78908 p->aOp[i].cnt,
78909 p->aOp[i].cycles,
78910 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
78911 );
78912 fprintf(out, "%s", zHdr);
78913 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
78914 }
78915 fclose(out);
78916 }
78917 }
78918#endif
78919 p->magic = VDBE_MAGIC_RESET;
78920 return p->rc & db->errMask;
78921}
78922
78923/*
78924** Clean up and delete a VDBE after execution. Return an integer which is
78925** the result code. Write any error message text into *pzErrMsg.
78926*/
78927SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe *p){
78928 int rc = SQLITE_OK;
78929 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
78930 rc = sqlite3VdbeReset(p);
78931 assert( (rc & p->db->errMask)==rc );
78932 }
78933 sqlite3VdbeDelete(p);
78934 return rc;
78935}
78936
78937/*
78938** If parameter iOp is less than zero, then invoke the destructor for
78939** all auxiliary data pointers currently cached by the VM passed as
78940** the first argument.
78941**
78942** Or, if iOp is greater than or equal to zero, then the destructor is
78943** only invoked for those auxiliary data pointers created by the user
78944** function invoked by the OP_Function opcode at instruction iOp of
78945** VM pVdbe, and only then if:
78946**
78947** * the associated function parameter is the 32nd or later (counting
78948** from left to right), or
78949**
78950** * the corresponding bit in argument mask is clear (where the first
78951** function parameter corresponds to bit 0 etc.).
78952*/
78953SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3 *db, AuxData **pp, int iOp, int mask){
78954 while( *pp ){
78955 AuxData *pAux = *pp;
78956 if( (iOp<0)
78957 || (pAux->iAuxOp==iOp
78958 && pAux->iAuxArg>=0
78959 && (pAux->iAuxArg>31 || !(mask & MASKBIT32(pAux->iAuxArg))))
78960 ){
78961 testcase( pAux->iAuxArg==31 );
78962 if( pAux->xDeleteAux ){
78963 pAux->xDeleteAux(pAux->pAux);
78964 }
78965 *pp = pAux->pNextAux;
78966 sqlite3DbFree(db, pAux);
78967 }else{
78968 pp= &pAux->pNextAux;
78969 }
78970 }
78971}
78972
78973/*
78974** Free all memory associated with the Vdbe passed as the second argument,
78975** except for object itself, which is preserved.
78976**
78977** The difference between this function and sqlite3VdbeDelete() is that
78978** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
78979** the database connection and frees the object itself.
78980*/
78981SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
78982 SubProgram *pSub, *pNext;
78983 assert( p->db==0 || p->db==db );
78984 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
78985 for(pSub=p->pProgram; pSub; pSub=pNext){
78986 pNext = pSub->pNext;
78987 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
78988 sqlite3DbFree(db, pSub);
78989 }
78990 if( p->magic!=VDBE_MAGIC_INIT ){
78991 releaseMemArray(p->aVar, p->nVar);
78992 sqlite3DbFree(db, p->pVList);
78993 sqlite3DbFree(db, p->pFree);
78994 }
78995 vdbeFreeOpArray(db, p->aOp, p->nOp);
78996 sqlite3DbFree(db, p->aColName);
78997 sqlite3DbFree(db, p->zSql);
78998#ifdef SQLITE_ENABLE_NORMALIZE
78999 sqlite3DbFree(db, p->zNormSql);
79000#endif
79001#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
79002 {
79003 int i;
79004 for(i=0; i<p->nScan; i++){
79005 sqlite3DbFree(db, p->aScan[i].zName);
79006 }
79007 sqlite3DbFree(db, p->aScan);
79008 }
79009#endif
79010}
79011
79012/*
79013** Delete an entire VDBE.
79014*/
79015SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
79016 sqlite3 *db;
79017
79018 assert( p!=0 );
79019 db = p->db;
79020 assert( sqlite3_mutex_held(db->mutex) );
79021 sqlite3VdbeClearObject(db, p);
79022 if( p->pPrev ){
79023 p->pPrev->pNext = p->pNext;
79024 }else{
79025 assert( db->pVdbe==p );
79026 db->pVdbe = p->pNext;
79027 }
79028 if( p->pNext ){
79029 p->pNext->pPrev = p->pPrev;
79030 }
79031 p->magic = VDBE_MAGIC_DEAD;
79032 p->db = 0;
79033 sqlite3DbFreeNN(db, p);
79034}
79035
79036/*
79037** The cursor "p" has a pending seek operation that has not yet been
79038** carried out. Seek the cursor now. If an error occurs, return
79039** the appropriate error code.
79040*/
79041static int SQLITE_NOINLINE handleDeferredMoveto(VdbeCursor *p){
79042 int res, rc;
79043#ifdef SQLITE_TEST
79044 extern int sqlite3_search_count;
79045#endif
79046 assert( p->deferredMoveto );
79047 assert( p->isTable );
79048 assert( p->eCurType==CURTYPE_BTREE );
79049 rc = sqlite3BtreeMovetoUnpacked(p->uc.pCursor, 0, p->movetoTarget, 0, &res);
79050 if( rc ) return rc;
79051 if( res!=0 ) return SQLITE_CORRUPT_BKPT;
79052#ifdef SQLITE_TEST
79053 sqlite3_search_count++;
79054#endif
79055 p->deferredMoveto = 0;
79056 p->cacheStatus = CACHE_STALE;
79057 return SQLITE_OK;
79058}
79059
79060/*
79061** Something has moved cursor "p" out of place. Maybe the row it was
79062** pointed to was deleted out from under it. Or maybe the btree was
79063** rebalanced. Whatever the cause, try to restore "p" to the place it
79064** is supposed to be pointing. If the row was deleted out from under the
79065** cursor, set the cursor to point to a NULL row.
79066*/
79067static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){
79068 int isDifferentRow, rc;
79069 assert( p->eCurType==CURTYPE_BTREE );
79070 assert( p->uc.pCursor!=0 );
79071 assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) );
79072 rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
79073 p->cacheStatus = CACHE_STALE;
79074 if( isDifferentRow ) p->nullRow = 1;
79075 return rc;
79076}
79077
79078/*
79079** Check to ensure that the cursor is valid. Restore the cursor
79080** if need be. Return any I/O error from the restore operation.
79081*/
79082SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor *p){
79083 assert( p->eCurType==CURTYPE_BTREE );
79084 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
79085 return handleMovedCursor(p);
79086 }
79087 return SQLITE_OK;
79088}
79089
79090/*
79091** Make sure the cursor p is ready to read or write the row to which it
79092** was last positioned. Return an error code if an OOM fault or I/O error
79093** prevents us from positioning the cursor to its correct position.
79094**
79095** If a MoveTo operation is pending on the given cursor, then do that
79096** MoveTo now. If no move is pending, check to see if the row has been
79097** deleted out from under the cursor and if it has, mark the row as
79098** a NULL row.
79099**
79100** If the cursor is already pointing to the correct row and that row has
79101** not been deleted out from under the cursor, then this routine is a no-op.
79102*/
79103SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor **pp, int *piCol){
79104 VdbeCursor *p = *pp;
79105 assert( p->eCurType==CURTYPE_BTREE || p->eCurType==CURTYPE_PSEUDO );
79106 if( p->deferredMoveto ){
79107 int iMap;
79108 if( p->aAltMap && (iMap = p->aAltMap[1+*piCol])>0 ){
79109 *pp = p->pAltCursor;
79110 *piCol = iMap - 1;
79111 return SQLITE_OK;
79112 }
79113 return handleDeferredMoveto(p);
79114 }
79115 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
79116 return handleMovedCursor(p);
79117 }
79118 return SQLITE_OK;
79119}
79120
79121/*
79122** The following functions:
79123**
79124** sqlite3VdbeSerialType()
79125** sqlite3VdbeSerialTypeLen()
79126** sqlite3VdbeSerialLen()
79127** sqlite3VdbeSerialPut()
79128** sqlite3VdbeSerialGet()
79129**
79130** encapsulate the code that serializes values for storage in SQLite
79131** data and index records. Each serialized value consists of a
79132** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
79133** integer, stored as a varint.
79134**
79135** In an SQLite index record, the serial type is stored directly before
79136** the blob of data that it corresponds to. In a table record, all serial
79137** types are stored at the start of the record, and the blobs of data at
79138** the end. Hence these functions allow the caller to handle the
79139** serial-type and data blob separately.
79140**
79141** The following table describes the various storage classes for data:
79142**
79143** serial type bytes of data type
79144** -------------- --------------- ---------------
79145** 0 0 NULL
79146** 1 1 signed integer
79147** 2 2 signed integer
79148** 3 3 signed integer
79149** 4 4 signed integer
79150** 5 6 signed integer
79151** 6 8 signed integer
79152** 7 8 IEEE float
79153** 8 0 Integer constant 0
79154** 9 0 Integer constant 1
79155** 10,11 reserved for expansion
79156** N>=12 and even (N-12)/2 BLOB
79157** N>=13 and odd (N-13)/2 text
79158**
79159** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
79160** of SQLite will not understand those serial types.
79161*/
79162
79163/*
79164** Return the serial-type for the value stored in pMem.
79165*/
79166SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){
79167 int flags = pMem->flags;
79168 u32 n;
79169
79170 assert( pLen!=0 );
79171 if( flags&MEM_Null ){
79172 *pLen = 0;
79173 return 0;
79174 }
79175 if( flags&MEM_Int ){
79176 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
79177# define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
79178 i64 i = pMem->u.i;
79179 u64 u;
79180 if( i<0 ){
79181 u = ~i;
79182 }else{
79183 u = i;
79184 }
79185 if( u<=127 ){
79186 if( (i&1)==i && file_format>=4 ){
79187 *pLen = 0;
79188 return 8+(u32)u;
79189 }else{
79190 *pLen = 1;
79191 return 1;
79192 }
79193 }
79194 if( u<=32767 ){ *pLen = 2; return 2; }
79195 if( u<=8388607 ){ *pLen = 3; return 3; }
79196 if( u<=2147483647 ){ *pLen = 4; return 4; }
79197 if( u<=MAX_6BYTE ){ *pLen = 6; return 5; }
79198 *pLen = 8;
79199 return 6;
79200 }
79201 if( flags&MEM_Real ){
79202 *pLen = 8;
79203 return 7;
79204 }
79205 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
79206 assert( pMem->n>=0 );
79207 n = (u32)pMem->n;
79208 if( flags & MEM_Zero ){
79209 n += pMem->u.nZero;
79210 }
79211 *pLen = n;
79212 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
79213}
79214
79215/*
79216** The sizes for serial types less than 128
79217*/
79218static const u8 sqlite3SmallTypeSizes[] = {
79219 /* 0 1 2 3 4 5 6 7 8 9 */
79220/* 0 */ 0, 1, 2, 3, 4, 6, 8, 8, 0, 0,
79221/* 10 */ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
79222/* 20 */ 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
79223/* 30 */ 9, 9, 10, 10, 11, 11, 12, 12, 13, 13,
79224/* 40 */ 14, 14, 15, 15, 16, 16, 17, 17, 18, 18,
79225/* 50 */ 19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
79226/* 60 */ 24, 24, 25, 25, 26, 26, 27, 27, 28, 28,
79227/* 70 */ 29, 29, 30, 30, 31, 31, 32, 32, 33, 33,
79228/* 80 */ 34, 34, 35, 35, 36, 36, 37, 37, 38, 38,
79229/* 90 */ 39, 39, 40, 40, 41, 41, 42, 42, 43, 43,
79230/* 100 */ 44, 44, 45, 45, 46, 46, 47, 47, 48, 48,
79231/* 110 */ 49, 49, 50, 50, 51, 51, 52, 52, 53, 53,
79232/* 120 */ 54, 54, 55, 55, 56, 56, 57, 57
79233};
79234
79235/*
79236** Return the length of the data corresponding to the supplied serial-type.
79237*/
79238SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
79239 if( serial_type>=128 ){
79240 return (serial_type-12)/2;
79241 }else{
79242 assert( serial_type<12
79243 || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 );
79244 return sqlite3SmallTypeSizes[serial_type];
79245 }
79246}
79247SQLITE_PRIVATE u8 sqlite3VdbeOneByteSerialTypeLen(u8 serial_type){
79248 assert( serial_type<128 );
79249 return sqlite3SmallTypeSizes[serial_type];
79250}
79251
79252/*
79253** If we are on an architecture with mixed-endian floating
79254** points (ex: ARM7) then swap the lower 4 bytes with the
79255** upper 4 bytes. Return the result.
79256**
79257** For most architectures, this is a no-op.
79258**
79259** (later): It is reported to me that the mixed-endian problem
79260** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
79261** that early versions of GCC stored the two words of a 64-bit
79262** float in the wrong order. And that error has been propagated
79263** ever since. The blame is not necessarily with GCC, though.
79264** GCC might have just copying the problem from a prior compiler.
79265** I am also told that newer versions of GCC that follow a different
79266** ABI get the byte order right.
79267**
79268** Developers using SQLite on an ARM7 should compile and run their
79269** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
79270** enabled, some asserts below will ensure that the byte order of
79271** floating point values is correct.
79272**
79273** (2007-08-30) Frank van Vugt has studied this problem closely
79274** and has send his findings to the SQLite developers. Frank
79275** writes that some Linux kernels offer floating point hardware
79276** emulation that uses only 32-bit mantissas instead of a full
79277** 48-bits as required by the IEEE standard. (This is the
79278** CONFIG_FPE_FASTFPE option.) On such systems, floating point
79279** byte swapping becomes very complicated. To avoid problems,
79280** the necessary byte swapping is carried out using a 64-bit integer
79281** rather than a 64-bit float. Frank assures us that the code here
79282** works for him. We, the developers, have no way to independently
79283** verify this, but Frank seems to know what he is talking about
79284** so we trust him.
79285*/
79286#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
79287static u64 floatSwap(u64 in){
79288 union {
79289 u64 r;
79290 u32 i[2];
79291 } u;
79292 u32 t;
79293
79294 u.r = in;
79295 t = u.i[0];
79296 u.i[0] = u.i[1];
79297 u.i[1] = t;
79298 return u.r;
79299}
79300# define swapMixedEndianFloat(X) X = floatSwap(X)
79301#else
79302# define swapMixedEndianFloat(X)
79303#endif
79304
79305/*
79306** Write the serialized data blob for the value stored in pMem into
79307** buf. It is assumed that the caller has allocated sufficient space.
79308** Return the number of bytes written.
79309**
79310** nBuf is the amount of space left in buf[]. The caller is responsible
79311** for allocating enough space to buf[] to hold the entire field, exclusive
79312** of the pMem->u.nZero bytes for a MEM_Zero value.
79313**
79314** Return the number of bytes actually written into buf[]. The number
79315** of bytes in the zero-filled tail is included in the return value only
79316** if those bytes were zeroed in buf[].
79317*/
79318SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){
79319 u32 len;
79320
79321 /* Integer and Real */
79322 if( serial_type<=7 && serial_type>0 ){
79323 u64 v;
79324 u32 i;
79325 if( serial_type==7 ){
79326 assert( sizeof(v)==sizeof(pMem->u.r) );
79327 memcpy(&v, &pMem->u.r, sizeof(v));
79328 swapMixedEndianFloat(v);
79329 }else{
79330 v = pMem->u.i;
79331 }
79332 len = i = sqlite3SmallTypeSizes[serial_type];
79333 assert( i>0 );
79334 do{
79335 buf[--i] = (u8)(v&0xFF);
79336 v >>= 8;
79337 }while( i );
79338 return len;
79339 }
79340
79341 /* String or blob */
79342 if( serial_type>=12 ){
79343 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
79344 == (int)sqlite3VdbeSerialTypeLen(serial_type) );
79345 len = pMem->n;
79346 if( len>0 ) memcpy(buf, pMem->z, len);
79347 return len;
79348 }
79349
79350 /* NULL or constants 0 or 1 */
79351 return 0;
79352}
79353
79354/* Input "x" is a sequence of unsigned characters that represent a
79355** big-endian integer. Return the equivalent native integer
79356*/
79357#define ONE_BYTE_INT(x) ((i8)(x)[0])
79358#define TWO_BYTE_INT(x) (256*(i8)((x)[0])|(x)[1])
79359#define THREE_BYTE_INT(x) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
79360#define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
79361#define FOUR_BYTE_INT(x) (16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
79362
79363/*
79364** Deserialize the data blob pointed to by buf as serial type serial_type
79365** and store the result in pMem. Return the number of bytes read.
79366**
79367** This function is implemented as two separate routines for performance.
79368** The few cases that require local variables are broken out into a separate
79369** routine so that in most cases the overhead of moving the stack pointer
79370** is avoided.
79371*/
79372static u32 SQLITE_NOINLINE serialGet(
79373 const unsigned char *buf, /* Buffer to deserialize from */
79374 u32 serial_type, /* Serial type to deserialize */
79375 Mem *pMem /* Memory cell to write value into */
79376){
79377 u64 x = FOUR_BYTE_UINT(buf);
79378 u32 y = FOUR_BYTE_UINT(buf+4);
79379 x = (x<<32) + y;
79380 if( serial_type==6 ){
79381 /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
79382 ** twos-complement integer. */
79383 pMem->u.i = *(i64*)&x;
79384 pMem->flags = MEM_Int;
79385 testcase( pMem->u.i<0 );
79386 }else{
79387 /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
79388 ** floating point number. */
79389#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
79390 /* Verify that integers and floating point values use the same
79391 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
79392 ** defined that 64-bit floating point values really are mixed
79393 ** endian.
79394 */
79395 static const u64 t1 = ((u64)0x3ff00000)<<32;
79396 static const double r1 = 1.0;
79397 u64 t2 = t1;
79398 swapMixedEndianFloat(t2);
79399 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
79400#endif
79401 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
79402 swapMixedEndianFloat(x);
79403 memcpy(&pMem->u.r, &x, sizeof(x));
79404 pMem->flags = sqlite3IsNaN(pMem->u.r) ? MEM_Null : MEM_Real;
79405 }
79406 return 8;
79407}
79408SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
79409 const unsigned char *buf, /* Buffer to deserialize from */
79410 u32 serial_type, /* Serial type to deserialize */
79411 Mem *pMem /* Memory cell to write value into */
79412){
79413 switch( serial_type ){
79414 case 10: { /* Internal use only: NULL with virtual table
79415 ** UPDATE no-change flag set */
79416 pMem->flags = MEM_Null|MEM_Zero;
79417 pMem->n = 0;
79418 pMem->u.nZero = 0;
79419 break;
79420 }
79421 case 11: /* Reserved for future use */
79422 case 0: { /* Null */
79423 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
79424 pMem->flags = MEM_Null;
79425 break;
79426 }
79427 case 1: {
79428 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
79429 ** integer. */
79430 pMem->u.i = ONE_BYTE_INT(buf);
79431 pMem->flags = MEM_Int;
79432 testcase( pMem->u.i<0 );
79433 return 1;
79434 }
79435 case 2: { /* 2-byte signed integer */
79436 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
79437 ** twos-complement integer. */
79438 pMem->u.i = TWO_BYTE_INT(buf);
79439 pMem->flags = MEM_Int;
79440 testcase( pMem->u.i<0 );
79441 return 2;
79442 }
79443 case 3: { /* 3-byte signed integer */
79444 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
79445 ** twos-complement integer. */
79446 pMem->u.i = THREE_BYTE_INT(buf);
79447 pMem->flags = MEM_Int;
79448 testcase( pMem->u.i<0 );
79449 return 3;
79450 }
79451 case 4: { /* 4-byte signed integer */
79452 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
79453 ** twos-complement integer. */
79454 pMem->u.i = FOUR_BYTE_INT(buf);
79455#ifdef __HP_cc
79456 /* Work around a sign-extension bug in the HP compiler for HP/UX */
79457 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
79458#endif
79459 pMem->flags = MEM_Int;
79460 testcase( pMem->u.i<0 );
79461 return 4;
79462 }
79463 case 5: { /* 6-byte signed integer */
79464 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
79465 ** twos-complement integer. */
79466 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
79467 pMem->flags = MEM_Int;
79468 testcase( pMem->u.i<0 );
79469 return 6;
79470 }
79471 case 6: /* 8-byte signed integer */
79472 case 7: { /* IEEE floating point */
79473 /* These use local variables, so do them in a separate routine
79474 ** to avoid having to move the frame pointer in the common case */
79475 return serialGet(buf,serial_type,pMem);
79476 }
79477 case 8: /* Integer 0 */
79478 case 9: { /* Integer 1 */
79479 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
79480 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
79481 pMem->u.i = serial_type-8;
79482 pMem->flags = MEM_Int;
79483 return 0;
79484 }
79485 default: {
79486 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
79487 ** length.
79488 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
79489 ** (N-13)/2 bytes in length. */
79490 static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
79491 pMem->z = (char *)buf;
79492 pMem->n = (serial_type-12)/2;
79493 pMem->flags = aFlag[serial_type&1];
79494 return pMem->n;
79495 }
79496 }
79497 return 0;
79498}
79499/*
79500** This routine is used to allocate sufficient space for an UnpackedRecord
79501** structure large enough to be used with sqlite3VdbeRecordUnpack() if
79502** the first argument is a pointer to KeyInfo structure pKeyInfo.
79503**
79504** The space is either allocated using sqlite3DbMallocRaw() or from within
79505** the unaligned buffer passed via the second and third arguments (presumably
79506** stack space). If the former, then *ppFree is set to a pointer that should
79507** be eventually freed by the caller using sqlite3DbFree(). Or, if the
79508** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
79509** before returning.
79510**
79511** If an OOM error occurs, NULL is returned.
79512*/
79513SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
79514 KeyInfo *pKeyInfo /* Description of the record */
79515){
79516 UnpackedRecord *p; /* Unpacked record to return */
79517 int nByte; /* Number of bytes required for *p */
79518 nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1);
79519 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
79520 if( !p ) return 0;
79521 p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
79522 assert( pKeyInfo->aSortOrder!=0 );
79523 p->pKeyInfo = pKeyInfo;
79524 p->nField = pKeyInfo->nKeyField + 1;
79525 return p;
79526}
79527
79528/*
79529** Given the nKey-byte encoding of a record in pKey[], populate the
79530** UnpackedRecord structure indicated by the fourth argument with the
79531** contents of the decoded record.
79532*/
79533SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(
79534 KeyInfo *pKeyInfo, /* Information about the record format */
79535 int nKey, /* Size of the binary record */
79536 const void *pKey, /* The binary record */
79537 UnpackedRecord *p /* Populate this structure before returning. */
79538){
79539 const unsigned char *aKey = (const unsigned char *)pKey;
79540 int d;
79541 u32 idx; /* Offset in aKey[] to read from */
79542 u16 u; /* Unsigned loop counter */
79543 u32 szHdr;
79544 Mem *pMem = p->aMem;
79545
79546 p->default_rc = 0;
79547 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
79548 idx = getVarint32(aKey, szHdr);
79549 d = szHdr;
79550 u = 0;
79551 while( idx<szHdr && d<=nKey ){
79552 u32 serial_type;
79553
79554 idx += getVarint32(&aKey[idx], serial_type);
79555 pMem->enc = pKeyInfo->enc;
79556 pMem->db = pKeyInfo->db;
79557 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
79558 pMem->szMalloc = 0;
79559 pMem->z = 0;
79560 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
79561 pMem++;
79562 if( (++u)>=p->nField ) break;
79563 }
79564 assert( u<=pKeyInfo->nKeyField + 1 );
79565 p->nField = u;
79566}
79567
79568#ifdef SQLITE_DEBUG
79569/*
79570** This function compares two index or table record keys in the same way
79571** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
79572** this function deserializes and compares values using the
79573** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
79574** in assert() statements to ensure that the optimized code in
79575** sqlite3VdbeRecordCompare() returns results with these two primitives.
79576**
79577** Return true if the result of comparison is equivalent to desiredResult.
79578** Return false if there is a disagreement.
79579*/
79580static int vdbeRecordCompareDebug(
79581 int nKey1, const void *pKey1, /* Left key */
79582 const UnpackedRecord *pPKey2, /* Right key */
79583 int desiredResult /* Correct answer */
79584){
79585 u32 d1; /* Offset into aKey[] of next data element */
79586 u32 idx1; /* Offset into aKey[] of next header element */
79587 u32 szHdr1; /* Number of bytes in header */
79588 int i = 0;
79589 int rc = 0;
79590 const unsigned char *aKey1 = (const unsigned char *)pKey1;
79591 KeyInfo *pKeyInfo;
79592 Mem mem1;
79593
79594 pKeyInfo = pPKey2->pKeyInfo;
79595 if( pKeyInfo->db==0 ) return 1;
79596 mem1.enc = pKeyInfo->enc;
79597 mem1.db = pKeyInfo->db;
79598 /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */
79599 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
79600
79601 /* Compilers may complain that mem1.u.i is potentially uninitialized.
79602 ** We could initialize it, as shown here, to silence those complaints.
79603 ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
79604 ** the unnecessary initialization has a measurable negative performance
79605 ** impact, since this routine is a very high runner. And so, we choose
79606 ** to ignore the compiler warnings and leave this variable uninitialized.
79607 */
79608 /* mem1.u.i = 0; // not needed, here to silence compiler warning */
79609
79610 idx1 = getVarint32(aKey1, szHdr1);
79611 if( szHdr1>98307 ) return SQLITE_CORRUPT;
79612 d1 = szHdr1;
79613 assert( pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB );
79614 assert( pKeyInfo->aSortOrder!=0 );
79615 assert( pKeyInfo->nKeyField>0 );
79616 assert( idx1<=szHdr1 || CORRUPT_DB );
79617 do{
79618 u32 serial_type1;
79619
79620 /* Read the serial types for the next element in each key. */
79621 idx1 += getVarint32( aKey1+idx1, serial_type1 );
79622
79623 /* Verify that there is enough key space remaining to avoid
79624 ** a buffer overread. The "d1+serial_type1+2" subexpression will
79625 ** always be greater than or equal to the amount of required key space.
79626 ** Use that approximation to avoid the more expensive call to
79627 ** sqlite3VdbeSerialTypeLen() in the common case.
79628 */
79629 if( d1+serial_type1+2>(u32)nKey1
79630 && d1+sqlite3VdbeSerialTypeLen(serial_type1)>(u32)nKey1
79631 ){
79632 break;
79633 }
79634
79635 /* Extract the values to be compared.
79636 */
79637 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
79638
79639 /* Do the comparison
79640 */
79641 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]);
79642 if( rc!=0 ){
79643 assert( mem1.szMalloc==0 ); /* See comment below */
79644 if( pKeyInfo->aSortOrder[i] ){
79645 rc = -rc; /* Invert the result for DESC sort order. */
79646 }
79647 goto debugCompareEnd;
79648 }
79649 i++;
79650 }while( idx1<szHdr1 && i<pPKey2->nField );
79651
79652 /* No memory allocation is ever used on mem1. Prove this using
79653 ** the following assert(). If the assert() fails, it indicates a
79654 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
79655 */
79656 assert( mem1.szMalloc==0 );
79657
79658 /* rc==0 here means that one of the keys ran out of fields and
79659 ** all the fields up to that point were equal. Return the default_rc
79660 ** value. */
79661 rc = pPKey2->default_rc;
79662
79663debugCompareEnd:
79664 if( desiredResult==0 && rc==0 ) return 1;
79665 if( desiredResult<0 && rc<0 ) return 1;
79666 if( desiredResult>0 && rc>0 ) return 1;
79667 if( CORRUPT_DB ) return 1;
79668 if( pKeyInfo->db->mallocFailed ) return 1;
79669 return 0;
79670}
79671#endif
79672
79673#ifdef SQLITE_DEBUG
79674/*
79675** Count the number of fields (a.k.a. columns) in the record given by
79676** pKey,nKey. The verify that this count is less than or equal to the
79677** limit given by pKeyInfo->nAllField.
79678**
79679** If this constraint is not satisfied, it means that the high-speed
79680** vdbeRecordCompareInt() and vdbeRecordCompareString() routines will
79681** not work correctly. If this assert() ever fires, it probably means
79682** that the KeyInfo.nKeyField or KeyInfo.nAllField values were computed
79683** incorrectly.
79684*/
79685static void vdbeAssertFieldCountWithinLimits(
79686 int nKey, const void *pKey, /* The record to verify */
79687 const KeyInfo *pKeyInfo /* Compare size with this KeyInfo */
79688){
79689 int nField = 0;
79690 u32 szHdr;
79691 u32 idx;
79692 u32 notUsed;
79693 const unsigned char *aKey = (const unsigned char*)pKey;
79694
79695 if( CORRUPT_DB ) return;
79696 idx = getVarint32(aKey, szHdr);
79697 assert( nKey>=0 );
79698 assert( szHdr<=(u32)nKey );
79699 while( idx<szHdr ){
79700 idx += getVarint32(aKey+idx, notUsed);
79701 nField++;
79702 }
79703 assert( nField <= pKeyInfo->nAllField );
79704}
79705#else
79706# define vdbeAssertFieldCountWithinLimits(A,B,C)
79707#endif
79708
79709/*
79710** Both *pMem1 and *pMem2 contain string values. Compare the two values
79711** using the collation sequence pColl. As usual, return a negative , zero
79712** or positive value if *pMem1 is less than, equal to or greater than
79713** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
79714*/
79715static int vdbeCompareMemString(
79716 const Mem *pMem1,
79717 const Mem *pMem2,
79718 const CollSeq *pColl,
79719 u8 *prcErr /* If an OOM occurs, set to SQLITE_NOMEM */
79720){
79721 if( pMem1->enc==pColl->enc ){
79722 /* The strings are already in the correct encoding. Call the
79723 ** comparison function directly */
79724 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
79725 }else{
79726 int rc;
79727 const void *v1, *v2;
79728 Mem c1;
79729 Mem c2;
79730 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null);
79731 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null);
79732 sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
79733 sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
79734 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
79735 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
79736 if( (v1==0 || v2==0) ){
79737 if( prcErr ) *prcErr = SQLITE_NOMEM_BKPT;
79738 rc = 0;
79739 }else{
79740 rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2);
79741 }
79742 sqlite3VdbeMemRelease(&c1);
79743 sqlite3VdbeMemRelease(&c2);
79744 return rc;
79745 }
79746}
79747
79748/*
79749** The input pBlob is guaranteed to be a Blob that is not marked
79750** with MEM_Zero. Return true if it could be a zero-blob.
79751*/
79752static int isAllZero(const char *z, int n){
79753 int i;
79754 for(i=0; i<n; i++){
79755 if( z[i] ) return 0;
79756 }
79757 return 1;
79758}
79759
79760/*
79761** Compare two blobs. Return negative, zero, or positive if the first
79762** is less than, equal to, or greater than the second, respectively.
79763** If one blob is a prefix of the other, then the shorter is the lessor.
79764*/
79765SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){
79766 int c;
79767 int n1 = pB1->n;
79768 int n2 = pB2->n;
79769
79770 /* It is possible to have a Blob value that has some non-zero content
79771 ** followed by zero content. But that only comes up for Blobs formed
79772 ** by the OP_MakeRecord opcode, and such Blobs never get passed into
79773 ** sqlite3MemCompare(). */
79774 assert( (pB1->flags & MEM_Zero)==0 || n1==0 );
79775 assert( (pB2->flags & MEM_Zero)==0 || n2==0 );
79776
79777 if( (pB1->flags|pB2->flags) & MEM_Zero ){
79778 if( pB1->flags & pB2->flags & MEM_Zero ){
79779 return pB1->u.nZero - pB2->u.nZero;
79780 }else if( pB1->flags & MEM_Zero ){
79781 if( !isAllZero(pB2->z, pB2->n) ) return -1;
79782 return pB1->u.nZero - n2;
79783 }else{
79784 if( !isAllZero(pB1->z, pB1->n) ) return +1;
79785 return n1 - pB2->u.nZero;
79786 }
79787 }
79788 c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1);
79789 if( c ) return c;
79790 return n1 - n2;
79791}
79792
79793/*
79794** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
79795** number. Return negative, zero, or positive if the first (i64) is less than,
79796** equal to, or greater than the second (double).
79797*/
79798static int sqlite3IntFloatCompare(i64 i, double r){
79799 if( sizeof(LONGDOUBLE_TYPE)>8 ){
79800 LONGDOUBLE_TYPE x = (LONGDOUBLE_TYPE)i;
79801 if( x<r ) return -1;
79802 if( x>r ) return +1;
79803 return 0;
79804 }else{
79805 i64 y;
79806 double s;
79807 if( r<-9223372036854775808.0 ) return +1;
79808 if( r>=9223372036854775808.0 ) return -1;
79809 y = (i64)r;
79810 if( i<y ) return -1;
79811 if( i>y ) return +1;
79812 s = (double)i;
79813 if( s<r ) return -1;
79814 if( s>r ) return +1;
79815 return 0;
79816 }
79817}
79818
79819/*
79820** Compare the values contained by the two memory cells, returning
79821** negative, zero or positive if pMem1 is less than, equal to, or greater
79822** than pMem2. Sorting order is NULL's first, followed by numbers (integers
79823** and reals) sorted numerically, followed by text ordered by the collating
79824** sequence pColl and finally blob's ordered by memcmp().
79825**
79826** Two NULL values are considered equal by this function.
79827*/
79828SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
79829 int f1, f2;
79830 int combined_flags;
79831
79832 f1 = pMem1->flags;
79833 f2 = pMem2->flags;
79834 combined_flags = f1|f2;
79835 assert( !sqlite3VdbeMemIsRowSet(pMem1) && !sqlite3VdbeMemIsRowSet(pMem2) );
79836
79837 /* If one value is NULL, it is less than the other. If both values
79838 ** are NULL, return 0.
79839 */
79840 if( combined_flags&MEM_Null ){
79841 return (f2&MEM_Null) - (f1&MEM_Null);
79842 }
79843
79844 /* At least one of the two values is a number
79845 */
79846 if( combined_flags&(MEM_Int|MEM_Real) ){
79847 if( (f1 & f2 & MEM_Int)!=0 ){
79848 if( pMem1->u.i < pMem2->u.i ) return -1;
79849 if( pMem1->u.i > pMem2->u.i ) return +1;
79850 return 0;
79851 }
79852 if( (f1 & f2 & MEM_Real)!=0 ){
79853 if( pMem1->u.r < pMem2->u.r ) return -1;
79854 if( pMem1->u.r > pMem2->u.r ) return +1;
79855 return 0;
79856 }
79857 if( (f1&MEM_Int)!=0 ){
79858 if( (f2&MEM_Real)!=0 ){
79859 return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
79860 }else{
79861 return -1;
79862 }
79863 }
79864 if( (f1&MEM_Real)!=0 ){
79865 if( (f2&MEM_Int)!=0 ){
79866 return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
79867 }else{
79868 return -1;
79869 }
79870 }
79871 return +1;
79872 }
79873
79874 /* If one value is a string and the other is a blob, the string is less.
79875 ** If both are strings, compare using the collating functions.
79876 */
79877 if( combined_flags&MEM_Str ){
79878 if( (f1 & MEM_Str)==0 ){
79879 return 1;
79880 }
79881 if( (f2 & MEM_Str)==0 ){
79882 return -1;
79883 }
79884
79885 assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed );
79886 assert( pMem1->enc==SQLITE_UTF8 ||
79887 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
79888
79889 /* The collation sequence must be defined at this point, even if
79890 ** the user deletes the collation sequence after the vdbe program is
79891 ** compiled (this was not always the case).
79892 */
79893 assert( !pColl || pColl->xCmp );
79894
79895 if( pColl ){
79896 return vdbeCompareMemString(pMem1, pMem2, pColl, 0);
79897 }
79898 /* If a NULL pointer was passed as the collate function, fall through
79899 ** to the blob case and use memcmp(). */
79900 }
79901
79902 /* Both values must be blobs. Compare using memcmp(). */
79903 return sqlite3BlobCompare(pMem1, pMem2);
79904}
79905
79906
79907/*
79908** The first argument passed to this function is a serial-type that
79909** corresponds to an integer - all values between 1 and 9 inclusive
79910** except 7. The second points to a buffer containing an integer value
79911** serialized according to serial_type. This function deserializes
79912** and returns the value.
79913*/
79914static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
79915 u32 y;
79916 assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) );
79917 switch( serial_type ){
79918 case 0:
79919 case 1:
79920 testcase( aKey[0]&0x80 );
79921 return ONE_BYTE_INT(aKey);
79922 case 2:
79923 testcase( aKey[0]&0x80 );
79924 return TWO_BYTE_INT(aKey);
79925 case 3:
79926 testcase( aKey[0]&0x80 );
79927 return THREE_BYTE_INT(aKey);
79928 case 4: {
79929 testcase( aKey[0]&0x80 );
79930 y = FOUR_BYTE_UINT(aKey);
79931 return (i64)*(int*)&y;
79932 }
79933 case 5: {
79934 testcase( aKey[0]&0x80 );
79935 return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
79936 }
79937 case 6: {
79938 u64 x = FOUR_BYTE_UINT(aKey);
79939 testcase( aKey[0]&0x80 );
79940 x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
79941 return (i64)*(i64*)&x;
79942 }
79943 }
79944
79945 return (serial_type - 8);
79946}
79947
79948/*
79949** This function compares the two table rows or index records
79950** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
79951** or positive integer if key1 is less than, equal to or
79952** greater than key2. The {nKey1, pKey1} key must be a blob
79953** created by the OP_MakeRecord opcode of the VDBE. The pPKey2
79954** key must be a parsed key such as obtained from
79955** sqlite3VdbeParseRecord.
79956**
79957** If argument bSkip is non-zero, it is assumed that the caller has already
79958** determined that the first fields of the keys are equal.
79959**
79960** Key1 and Key2 do not have to contain the same number of fields. If all
79961** fields that appear in both keys are equal, then pPKey2->default_rc is
79962** returned.
79963**
79964** If database corruption is discovered, set pPKey2->errCode to
79965** SQLITE_CORRUPT and return 0. If an OOM error is encountered,
79966** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
79967** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
79968*/
79969SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(
79970 int nKey1, const void *pKey1, /* Left key */
79971 UnpackedRecord *pPKey2, /* Right key */
79972 int bSkip /* If true, skip the first field */
79973){
79974 u32 d1; /* Offset into aKey[] of next data element */
79975 int i; /* Index of next field to compare */
79976 u32 szHdr1; /* Size of record header in bytes */
79977 u32 idx1; /* Offset of first type in header */
79978 int rc = 0; /* Return value */
79979 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
79980 KeyInfo *pKeyInfo;
79981 const unsigned char *aKey1 = (const unsigned char *)pKey1;
79982 Mem mem1;
79983
79984 /* If bSkip is true, then the caller has already determined that the first
79985 ** two elements in the keys are equal. Fix the various stack variables so
79986 ** that this routine begins comparing at the second field. */
79987 if( bSkip ){
79988 u32 s1;
79989 idx1 = 1 + getVarint32(&aKey1[1], s1);
79990 szHdr1 = aKey1[0];
79991 d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
79992 i = 1;
79993 pRhs++;
79994 }else{
79995 idx1 = getVarint32(aKey1, szHdr1);
79996 d1 = szHdr1;
79997 if( d1>(unsigned)nKey1 ){
79998 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
79999 return 0; /* Corruption */
80000 }
80001 i = 0;
80002 }
80003
80004 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
80005 assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField
80006 || CORRUPT_DB );
80007 assert( pPKey2->pKeyInfo->aSortOrder!=0 );
80008 assert( pPKey2->pKeyInfo->nKeyField>0 );
80009 assert( idx1<=szHdr1 || CORRUPT_DB );
80010 do{
80011 u32 serial_type;
80012
80013 /* RHS is an integer */
80014 if( pRhs->flags & MEM_Int ){
80015 serial_type = aKey1[idx1];
80016 testcase( serial_type==12 );
80017 if( serial_type>=10 ){
80018 rc = +1;
80019 }else if( serial_type==0 ){
80020 rc = -1;
80021 }else if( serial_type==7 ){
80022 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
80023 rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
80024 }else{
80025 i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
80026 i64 rhs = pRhs->u.i;
80027 if( lhs<rhs ){
80028 rc = -1;
80029 }else if( lhs>rhs ){
80030 rc = +1;
80031 }
80032 }
80033 }
80034
80035 /* RHS is real */
80036 else if( pRhs->flags & MEM_Real ){
80037 serial_type = aKey1[idx1];
80038 if( serial_type>=10 ){
80039 /* Serial types 12 or greater are strings and blobs (greater than
80040 ** numbers). Types 10 and 11 are currently "reserved for future
80041 ** use", so it doesn't really matter what the results of comparing
80042 ** them to numberic values are. */
80043 rc = +1;
80044 }else if( serial_type==0 ){
80045 rc = -1;
80046 }else{
80047 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
80048 if( serial_type==7 ){
80049 if( mem1.u.r<pRhs->u.r ){
80050 rc = -1;
80051 }else if( mem1.u.r>pRhs->u.r ){
80052 rc = +1;
80053 }
80054 }else{
80055 rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r);
80056 }
80057 }
80058 }
80059
80060 /* RHS is a string */
80061 else if( pRhs->flags & MEM_Str ){
80062 getVarint32(&aKey1[idx1], serial_type);
80063 testcase( serial_type==12 );
80064 if( serial_type<12 ){
80065 rc = -1;
80066 }else if( !(serial_type & 0x01) ){
80067 rc = +1;
80068 }else{
80069 mem1.n = (serial_type - 12) / 2;
80070 testcase( (d1+mem1.n)==(unsigned)nKey1 );
80071 testcase( (d1+mem1.n+1)==(unsigned)nKey1 );
80072 if( (d1+mem1.n) > (unsigned)nKey1 ){
80073 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
80074 return 0; /* Corruption */
80075 }else if( (pKeyInfo = pPKey2->pKeyInfo)->aColl[i] ){
80076 mem1.enc = pKeyInfo->enc;
80077 mem1.db = pKeyInfo->db;
80078 mem1.flags = MEM_Str;
80079 mem1.z = (char*)&aKey1[d1];
80080 rc = vdbeCompareMemString(
80081 &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
80082 );
80083 }else{
80084 int nCmp = MIN(mem1.n, pRhs->n);
80085 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
80086 if( rc==0 ) rc = mem1.n - pRhs->n;
80087 }
80088 }
80089 }
80090
80091 /* RHS is a blob */
80092 else if( pRhs->flags & MEM_Blob ){
80093 assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 );
80094 getVarint32(&aKey1[idx1], serial_type);
80095 testcase( serial_type==12 );
80096 if( serial_type<12 || (serial_type & 0x01) ){
80097 rc = -1;
80098 }else{
80099 int nStr = (serial_type - 12) / 2;
80100 testcase( (d1+nStr)==(unsigned)nKey1 );
80101 testcase( (d1+nStr+1)==(unsigned)nKey1 );
80102 if( (d1+nStr) > (unsigned)nKey1 ){
80103 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
80104 return 0; /* Corruption */
80105 }else if( pRhs->flags & MEM_Zero ){
80106 if( !isAllZero((const char*)&aKey1[d1],nStr) ){
80107 rc = 1;
80108 }else{
80109 rc = nStr - pRhs->u.nZero;
80110 }
80111 }else{
80112 int nCmp = MIN(nStr, pRhs->n);
80113 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
80114 if( rc==0 ) rc = nStr - pRhs->n;
80115 }
80116 }
80117 }
80118
80119 /* RHS is null */
80120 else{
80121 serial_type = aKey1[idx1];
80122 rc = (serial_type!=0);
80123 }
80124
80125 if( rc!=0 ){
80126 if( pPKey2->pKeyInfo->aSortOrder[i] ){
80127 rc = -rc;
80128 }
80129 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) );
80130 assert( mem1.szMalloc==0 ); /* See comment below */
80131 return rc;
80132 }
80133
80134 i++;
80135 if( i==pPKey2->nField ) break;
80136 pRhs++;
80137 d1 += sqlite3VdbeSerialTypeLen(serial_type);
80138 idx1 += sqlite3VarintLen(serial_type);
80139 }while( idx1<(unsigned)szHdr1 && d1<=(unsigned)nKey1 );
80140
80141 /* No memory allocation is ever used on mem1. Prove this using
80142 ** the following assert(). If the assert() fails, it indicates a
80143 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */
80144 assert( mem1.szMalloc==0 );
80145
80146 /* rc==0 here means that one or both of the keys ran out of fields and
80147 ** all the fields up to that point were equal. Return the default_rc
80148 ** value. */
80149 assert( CORRUPT_DB
80150 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)
80151 || pPKey2->pKeyInfo->db->mallocFailed
80152 );
80153 pPKey2->eqSeen = 1;
80154 return pPKey2->default_rc;
80155}
80156SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
80157 int nKey1, const void *pKey1, /* Left key */
80158 UnpackedRecord *pPKey2 /* Right key */
80159){
80160 return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0);
80161}
80162
80163
80164/*
80165** This function is an optimized version of sqlite3VdbeRecordCompare()
80166** that (a) the first field of pPKey2 is an integer, and (b) the
80167** size-of-header varint at the start of (pKey1/nKey1) fits in a single
80168** byte (i.e. is less than 128).
80169**
80170** To avoid concerns about buffer overreads, this routine is only used
80171** on schemas where the maximum valid header size is 63 bytes or less.
80172*/
80173static int vdbeRecordCompareInt(
80174 int nKey1, const void *pKey1, /* Left key */
80175 UnpackedRecord *pPKey2 /* Right key */
80176){
80177 const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
80178 int serial_type = ((const u8*)pKey1)[1];
80179 int res;
80180 u32 y;
80181 u64 x;
80182 i64 v;
80183 i64 lhs;
80184
80185 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
80186 assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB );
80187 switch( serial_type ){
80188 case 1: { /* 1-byte signed integer */
80189 lhs = ONE_BYTE_INT(aKey);
80190 testcase( lhs<0 );
80191 break;
80192 }
80193 case 2: { /* 2-byte signed integer */
80194 lhs = TWO_BYTE_INT(aKey);
80195 testcase( lhs<0 );
80196 break;
80197 }
80198 case 3: { /* 3-byte signed integer */
80199 lhs = THREE_BYTE_INT(aKey);
80200 testcase( lhs<0 );
80201 break;
80202 }
80203 case 4: { /* 4-byte signed integer */
80204 y = FOUR_BYTE_UINT(aKey);
80205 lhs = (i64)*(int*)&y;
80206 testcase( lhs<0 );
80207 break;
80208 }
80209 case 5: { /* 6-byte signed integer */
80210 lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
80211 testcase( lhs<0 );
80212 break;
80213 }
80214 case 6: { /* 8-byte signed integer */
80215 x = FOUR_BYTE_UINT(aKey);
80216 x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
80217 lhs = *(i64*)&x;
80218 testcase( lhs<0 );
80219 break;
80220 }
80221 case 8:
80222 lhs = 0;
80223 break;
80224 case 9:
80225 lhs = 1;
80226 break;
80227
80228 /* This case could be removed without changing the results of running
80229 ** this code. Including it causes gcc to generate a faster switch
80230 ** statement (since the range of switch targets now starts at zero and
80231 ** is contiguous) but does not cause any duplicate code to be generated
80232 ** (as gcc is clever enough to combine the two like cases). Other
80233 ** compilers might be similar. */
80234 case 0: case 7:
80235 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
80236
80237 default:
80238 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
80239 }
80240
80241 v = pPKey2->aMem[0].u.i;
80242 if( v>lhs ){
80243 res = pPKey2->r1;
80244 }else if( v<lhs ){
80245 res = pPKey2->r2;
80246 }else if( pPKey2->nField>1 ){
80247 /* The first fields of the two keys are equal. Compare the trailing
80248 ** fields. */
80249 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
80250 }else{
80251 /* The first fields of the two keys are equal and there are no trailing
80252 ** fields. Return pPKey2->default_rc in this case. */
80253 res = pPKey2->default_rc;
80254 pPKey2->eqSeen = 1;
80255 }
80256
80257 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) );
80258 return res;
80259}
80260
80261/*
80262** This function is an optimized version of sqlite3VdbeRecordCompare()
80263** that (a) the first field of pPKey2 is a string, that (b) the first field
80264** uses the collation sequence BINARY and (c) that the size-of-header varint
80265** at the start of (pKey1/nKey1) fits in a single byte.
80266*/
80267static int vdbeRecordCompareString(
80268 int nKey1, const void *pKey1, /* Left key */
80269 UnpackedRecord *pPKey2 /* Right key */
80270){
80271 const u8 *aKey1 = (const u8*)pKey1;
80272 int serial_type;
80273 int res;
80274
80275 assert( pPKey2->aMem[0].flags & MEM_Str );
80276 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
80277 getVarint32(&aKey1[1], serial_type);
80278 if( serial_type<12 ){
80279 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
80280 }else if( !(serial_type & 0x01) ){
80281 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
80282 }else{
80283 int nCmp;
80284 int nStr;
80285 int szHdr = aKey1[0];
80286
80287 nStr = (serial_type-12) / 2;
80288 if( (szHdr + nStr) > nKey1 ){
80289 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
80290 return 0; /* Corruption */
80291 }
80292 nCmp = MIN( pPKey2->aMem[0].n, nStr );
80293 res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
80294
80295 if( res==0 ){
80296 res = nStr - pPKey2->aMem[0].n;
80297 if( res==0 ){
80298 if( pPKey2->nField>1 ){
80299 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
80300 }else{
80301 res = pPKey2->default_rc;
80302 pPKey2->eqSeen = 1;
80303 }
80304 }else if( res>0 ){
80305 res = pPKey2->r2;
80306 }else{
80307 res = pPKey2->r1;
80308 }
80309 }else if( res>0 ){
80310 res = pPKey2->r2;
80311 }else{
80312 res = pPKey2->r1;
80313 }
80314 }
80315
80316 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res)
80317 || CORRUPT_DB
80318 || pPKey2->pKeyInfo->db->mallocFailed
80319 );
80320 return res;
80321}
80322
80323/*
80324** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
80325** suitable for comparing serialized records to the unpacked record passed
80326** as the only argument.
80327*/
80328SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
80329 /* varintRecordCompareInt() and varintRecordCompareString() both assume
80330 ** that the size-of-header varint that occurs at the start of each record
80331 ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
80332 ** also assumes that it is safe to overread a buffer by at least the
80333 ** maximum possible legal header size plus 8 bytes. Because there is
80334 ** guaranteed to be at least 74 (but not 136) bytes of padding following each
80335 ** buffer passed to varintRecordCompareInt() this makes it convenient to
80336 ** limit the size of the header to 64 bytes in cases where the first field
80337 ** is an integer.
80338 **
80339 ** The easiest way to enforce this limit is to consider only records with
80340 ** 13 fields or less. If the first field is an integer, the maximum legal
80341 ** header size is (12*5 + 1 + 1) bytes. */
80342 if( p->pKeyInfo->nAllField<=13 ){
80343 int flags = p->aMem[0].flags;
80344 if( p->pKeyInfo->aSortOrder[0] ){
80345 p->r1 = 1;
80346 p->r2 = -1;
80347 }else{
80348 p->r1 = -1;
80349 p->r2 = 1;
80350 }
80351 if( (flags & MEM_Int) ){
80352 return vdbeRecordCompareInt;
80353 }
80354 testcase( flags & MEM_Real );
80355 testcase( flags & MEM_Null );
80356 testcase( flags & MEM_Blob );
80357 if( (flags & (MEM_Real|MEM_Null|MEM_Blob))==0 && p->pKeyInfo->aColl[0]==0 ){
80358 assert( flags & MEM_Str );
80359 return vdbeRecordCompareString;
80360 }
80361 }
80362
80363 return sqlite3VdbeRecordCompare;
80364}
80365
80366/*
80367** pCur points at an index entry created using the OP_MakeRecord opcode.
80368** Read the rowid (the last field in the record) and store it in *rowid.
80369** Return SQLITE_OK if everything works, or an error code otherwise.
80370**
80371** pCur might be pointing to text obtained from a corrupt database file.
80372** So the content cannot be trusted. Do appropriate checks on the content.
80373*/
80374SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
80375 i64 nCellKey = 0;
80376 int rc;
80377 u32 szHdr; /* Size of the header */
80378 u32 typeRowid; /* Serial type of the rowid */
80379 u32 lenRowid; /* Size of the rowid */
80380 Mem m, v;
80381
80382 /* Get the size of the index entry. Only indices entries of less
80383 ** than 2GiB are support - anything large must be database corruption.
80384 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
80385 ** this code can safely assume that nCellKey is 32-bits
80386 */
80387 assert( sqlite3BtreeCursorIsValid(pCur) );
80388 nCellKey = sqlite3BtreePayloadSize(pCur);
80389 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
80390
80391 /* Read in the complete content of the index entry */
80392 sqlite3VdbeMemInit(&m, db, 0);
80393 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, &m);
80394 if( rc ){
80395 return rc;
80396 }
80397
80398 /* The index entry must begin with a header size */
80399 (void)getVarint32((u8*)m.z, szHdr);
80400 testcase( szHdr==3 );
80401 testcase( szHdr==m.n );
80402 testcase( szHdr>0x7fffffff );
80403 assert( m.n>=0 );
80404 if( unlikely(szHdr<3 || szHdr>(unsigned)m.n) ){
80405 goto idx_rowid_corruption;
80406 }
80407
80408 /* The last field of the index should be an integer - the ROWID.
80409 ** Verify that the last entry really is an integer. */
80410 (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
80411 testcase( typeRowid==1 );
80412 testcase( typeRowid==2 );
80413 testcase( typeRowid==3 );
80414 testcase( typeRowid==4 );
80415 testcase( typeRowid==5 );
80416 testcase( typeRowid==6 );
80417 testcase( typeRowid==8 );
80418 testcase( typeRowid==9 );
80419 if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
80420 goto idx_rowid_corruption;
80421 }
80422 lenRowid = sqlite3SmallTypeSizes[typeRowid];
80423 testcase( (u32)m.n==szHdr+lenRowid );
80424 if( unlikely((u32)m.n<szHdr+lenRowid) ){
80425 goto idx_rowid_corruption;
80426 }
80427
80428 /* Fetch the integer off the end of the index record */
80429 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
80430 *rowid = v.u.i;
80431 sqlite3VdbeMemRelease(&m);
80432 return SQLITE_OK;
80433
80434 /* Jump here if database corruption is detected after m has been
80435 ** allocated. Free the m object and return SQLITE_CORRUPT. */
80436idx_rowid_corruption:
80437 testcase( m.szMalloc!=0 );
80438 sqlite3VdbeMemRelease(&m);
80439 return SQLITE_CORRUPT_BKPT;
80440}
80441
80442/*
80443** Compare the key of the index entry that cursor pC is pointing to against
80444** the key string in pUnpacked. Write into *pRes a number
80445** that is negative, zero, or positive if pC is less than, equal to,
80446** or greater than pUnpacked. Return SQLITE_OK on success.
80447**
80448** pUnpacked is either created without a rowid or is truncated so that it
80449** omits the rowid at the end. The rowid at the end of the index entry
80450** is ignored as well. Hence, this routine only compares the prefixes
80451** of the keys prior to the final rowid, not the entire key.
80452*/
80453SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
80454 sqlite3 *db, /* Database connection */
80455 VdbeCursor *pC, /* The cursor to compare against */
80456 UnpackedRecord *pUnpacked, /* Unpacked version of key */
80457 int *res /* Write the comparison result here */
80458){
80459 i64 nCellKey = 0;
80460 int rc;
80461 BtCursor *pCur;
80462 Mem m;
80463
80464 assert( pC->eCurType==CURTYPE_BTREE );
80465 pCur = pC->uc.pCursor;
80466 assert( sqlite3BtreeCursorIsValid(pCur) );
80467 nCellKey = sqlite3BtreePayloadSize(pCur);
80468 /* nCellKey will always be between 0 and 0xffffffff because of the way
80469 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
80470 if( nCellKey<=0 || nCellKey>0x7fffffff ){
80471 *res = 0;
80472 return SQLITE_CORRUPT_BKPT;
80473 }
80474 sqlite3VdbeMemInit(&m, db, 0);
80475 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, &m);
80476 if( rc ){
80477 return rc;
80478 }
80479 *res = sqlite3VdbeRecordCompareWithSkip(m.n, m.z, pUnpacked, 0);
80480 sqlite3VdbeMemRelease(&m);
80481 return SQLITE_OK;
80482}
80483
80484/*
80485** This routine sets the value to be returned by subsequent calls to
80486** sqlite3_changes() on the database handle 'db'.
80487*/
80488SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
80489 assert( sqlite3_mutex_held(db->mutex) );
80490 db->nChange = nChange;
80491 db->nTotalChange += nChange;
80492}
80493
80494/*
80495** Set a flag in the vdbe to update the change counter when it is finalised
80496** or reset.
80497*/
80498SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe *v){
80499 v->changeCntOn = 1;
80500}
80501
80502/*
80503** Mark every prepared statement associated with a database connection
80504** as expired.
80505**
80506** An expired statement means that recompilation of the statement is
80507** recommend. Statements expire when things happen that make their
80508** programs obsolete. Removing user-defined functions or collating
80509** sequences, or changing an authorization function are the types of
80510** things that make prepared statements obsolete.
80511**
80512** If iCode is 1, then expiration is advisory. The statement should
80513** be reprepared before being restarted, but if it is already running
80514** it is allowed to run to completion.
80515**
80516** Internally, this function just sets the Vdbe.expired flag on all
80517** prepared statements. The flag is set to 1 for an immediate expiration
80518** and set to 2 for an advisory expiration.
80519*/
80520SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db, int iCode){
80521 Vdbe *p;
80522 for(p = db->pVdbe; p; p=p->pNext){
80523 p->expired = iCode+1;
80524 }
80525}
80526
80527/*
80528** Return the database associated with the Vdbe.
80529*/
80530SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe *v){
80531 return v->db;
80532}
80533
80534/*
80535** Return the SQLITE_PREPARE flags for a Vdbe.
80536*/
80537SQLITE_PRIVATE u8 sqlite3VdbePrepareFlags(Vdbe *v){
80538 return v->prepFlags;
80539}
80540
80541/*
80542** Return a pointer to an sqlite3_value structure containing the value bound
80543** parameter iVar of VM v. Except, if the value is an SQL NULL, return
80544** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
80545** constants) to the value before returning it.
80546**
80547** The returned value must be freed by the caller using sqlite3ValueFree().
80548*/
80549SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){
80550 assert( iVar>0 );
80551 if( v ){
80552 Mem *pMem = &v->aVar[iVar-1];
80553 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
80554 if( 0==(pMem->flags & MEM_Null) ){
80555 sqlite3_value *pRet = sqlite3ValueNew(v->db);
80556 if( pRet ){
80557 sqlite3VdbeMemCopy((Mem *)pRet, pMem);
80558 sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
80559 }
80560 return pRet;
80561 }
80562 }
80563 return 0;
80564}
80565
80566/*
80567** Configure SQL variable iVar so that binding a new value to it signals
80568** to sqlite3_reoptimize() that re-preparing the statement may result
80569** in a better query plan.
80570*/
80571SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
80572 assert( iVar>0 );
80573 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
80574 if( iVar>=32 ){
80575 v->expmask |= 0x80000000;
80576 }else{
80577 v->expmask |= ((u32)1 << (iVar-1));
80578 }
80579}
80580
80581/*
80582** Cause a function to throw an error if it was call from OP_PureFunc
80583** rather than OP_Function.
80584**
80585** OP_PureFunc means that the function must be deterministic, and should
80586** throw an error if it is given inputs that would make it non-deterministic.
80587** This routine is invoked by date/time functions that use non-deterministic
80588** features such as 'now'.
80589*/
80590SQLITE_PRIVATE int sqlite3NotPureFunc(sqlite3_context *pCtx){
80591#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
80592 if( pCtx->pVdbe==0 ) return 1;
80593#endif
80594 if( pCtx->pVdbe->aOp[pCtx->iOp].opcode==OP_PureFunc ){
80595 sqlite3_result_error(pCtx,
80596 "non-deterministic function in index expression or CHECK constraint",
80597 -1);
80598 return 0;
80599 }
80600 return 1;
80601}
80602
80603#ifndef SQLITE_OMIT_VIRTUALTABLE
80604/*
80605** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
80606** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
80607** in memory obtained from sqlite3DbMalloc).
80608*/
80609SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
80610 if( pVtab->zErrMsg ){
80611 sqlite3 *db = p->db;
80612 sqlite3DbFree(db, p->zErrMsg);
80613 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
80614 sqlite3_free(pVtab->zErrMsg);
80615 pVtab->zErrMsg = 0;
80616 }
80617}
80618#endif /* SQLITE_OMIT_VIRTUALTABLE */
80619
80620#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
80621
80622/*
80623** If the second argument is not NULL, release any allocations associated
80624** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
80625** structure itself, using sqlite3DbFree().
80626**
80627** This function is used to free UnpackedRecord structures allocated by
80628** the vdbeUnpackRecord() function found in vdbeapi.c.
80629*/
80630static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){
80631 if( p ){
80632 int i;
80633 for(i=0; i<nField; i++){
80634 Mem *pMem = &p->aMem[i];
80635 if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem);
80636 }
80637 sqlite3DbFreeNN(db, p);
80638 }
80639}
80640#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
80641
80642#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
80643/*
80644** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
80645** then cursor passed as the second argument should point to the row about
80646** to be update or deleted. If the application calls sqlite3_preupdate_old(),
80647** the required value will be read from the row the cursor points to.
80648*/
80649SQLITE_PRIVATE void sqlite3VdbePreUpdateHook(
80650 Vdbe *v, /* Vdbe pre-update hook is invoked by */
80651 VdbeCursor *pCsr, /* Cursor to grab old.* values from */
80652 int op, /* SQLITE_INSERT, UPDATE or DELETE */
80653 const char *zDb, /* Database name */
80654 Table *pTab, /* Modified table */
80655 i64 iKey1, /* Initial key value */
80656 int iReg /* Register for new.* record */
80657){
80658 sqlite3 *db = v->db;
80659 i64 iKey2;
80660 PreUpdate preupdate;
80661 const char *zTbl = pTab->zName;
80662 static const u8 fakeSortOrder = 0;
80663
80664 assert( db->pPreUpdate==0 );
80665 memset(&preupdate, 0, sizeof(PreUpdate));
80666 if( HasRowid(pTab)==0 ){
80667 iKey1 = iKey2 = 0;
80668 preupdate.pPk = sqlite3PrimaryKeyIndex(pTab);
80669 }else{
80670 if( op==SQLITE_UPDATE ){
80671 iKey2 = v->aMem[iReg].u.i;
80672 }else{
80673 iKey2 = iKey1;
80674 }
80675 }
80676
80677 assert( pCsr->nField==pTab->nCol
80678 || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1)
80679 );
80680
80681 preupdate.v = v;
80682 preupdate.pCsr = pCsr;
80683 preupdate.op = op;
80684 preupdate.iNewReg = iReg;
80685 preupdate.keyinfo.db = db;
80686 preupdate.keyinfo.enc = ENC(db);
80687 preupdate.keyinfo.nKeyField = pTab->nCol;
80688 preupdate.keyinfo.aSortOrder = (u8*)&fakeSortOrder;
80689 preupdate.iKey1 = iKey1;
80690 preupdate.iKey2 = iKey2;
80691 preupdate.pTab = pTab;
80692
80693 db->pPreUpdate = &preupdate;
80694 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
80695 db->pPreUpdate = 0;
80696 sqlite3DbFree(db, preupdate.aRecord);
80697 vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pUnpacked);
80698 vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pNewUnpacked);
80699 if( preupdate.aNew ){
80700 int i;
80701 for(i=0; i<pCsr->nField; i++){
80702 sqlite3VdbeMemRelease(&preupdate.aNew[i]);
80703 }
80704 sqlite3DbFreeNN(db, preupdate.aNew);
80705 }
80706}
80707#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
80708
80709/************** End of vdbeaux.c *********************************************/
80710/************** Begin file vdbeapi.c *****************************************/
80711/*
80712** 2004 May 26
80713**
80714** The author disclaims copyright to this source code. In place of
80715** a legal notice, here is a blessing:
80716**
80717** May you do good and not evil.
80718** May you find forgiveness for yourself and forgive others.
80719** May you share freely, never taking more than you give.
80720**
80721*************************************************************************
80722**
80723** This file contains code use to implement APIs that are part of the
80724** VDBE.
80725*/
80726/* #include "sqliteInt.h" */
80727/* #include "vdbeInt.h" */
80728
80729#ifndef SQLITE_OMIT_DEPRECATED
80730/*
80731** Return TRUE (non-zero) of the statement supplied as an argument needs
80732** to be recompiled. A statement needs to be recompiled whenever the
80733** execution environment changes in a way that would alter the program
80734** that sqlite3_prepare() generates. For example, if new functions or
80735** collating sequences are registered or if an authorizer function is
80736** added or changed.
80737*/
80738SQLITE_API int sqlite3_expired(sqlite3_stmt *pStmt){
80739 Vdbe *p = (Vdbe*)pStmt;
80740 return p==0 || p->expired;
80741}
80742#endif
80743
80744/*
80745** Check on a Vdbe to make sure it has not been finalized. Log
80746** an error and return true if it has been finalized (or is otherwise
80747** invalid). Return false if it is ok.
80748*/
80749static int vdbeSafety(Vdbe *p){
80750 if( p->db==0 ){
80751 sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement");
80752 return 1;
80753 }else{
80754 return 0;
80755 }
80756}
80757static int vdbeSafetyNotNull(Vdbe *p){
80758 if( p==0 ){
80759 sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");
80760 return 1;
80761 }else{
80762 return vdbeSafety(p);
80763 }
80764}
80765
80766#ifndef SQLITE_OMIT_TRACE
80767/*
80768** Invoke the profile callback. This routine is only called if we already
80769** know that the profile callback is defined and needs to be invoked.
80770*/
80771static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){
80772 sqlite3_int64 iNow;
80773 sqlite3_int64 iElapse;
80774 assert( p->startTime>0 );
80775 assert( db->xProfile!=0 || (db->mTrace & SQLITE_TRACE_PROFILE)!=0 );
80776 assert( db->init.busy==0 );
80777 assert( p->zSql!=0 );
80778 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
80779 iElapse = (iNow - p->startTime)*1000000;
80780 if( db->xProfile ){
80781 db->xProfile(db->pProfileArg, p->zSql, iElapse);
80782 }
80783 if( db->mTrace & SQLITE_TRACE_PROFILE ){
80784 db->xTrace(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
80785 }
80786 p->startTime = 0;
80787}
80788/*
80789** The checkProfileCallback(DB,P) macro checks to see if a profile callback
80790** is needed, and it invokes the callback if it is needed.
80791*/
80792# define checkProfileCallback(DB,P) \
80793 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
80794#else
80795# define checkProfileCallback(DB,P) /*no-op*/
80796#endif
80797
80798/*
80799** The following routine destroys a virtual machine that is created by
80800** the sqlite3_compile() routine. The integer returned is an SQLITE_
80801** success/failure code that describes the result of executing the virtual
80802** machine.
80803**
80804** This routine sets the error code and string returned by
80805** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
80806*/
80807SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){
80808 int rc;
80809 if( pStmt==0 ){
80810 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
80811 ** pointer is a harmless no-op. */
80812 rc = SQLITE_OK;
80813 }else{
80814 Vdbe *v = (Vdbe*)pStmt;
80815 sqlite3 *db = v->db;
80816 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
80817 sqlite3_mutex_enter(db->mutex);
80818 checkProfileCallback(db, v);
80819 rc = sqlite3VdbeFinalize(v);
80820 rc = sqlite3ApiExit(db, rc);
80821 sqlite3LeaveMutexAndCloseZombie(db);
80822 }
80823 return rc;
80824}
80825
80826/*
80827** Terminate the current execution of an SQL statement and reset it
80828** back to its starting state so that it can be reused. A success code from
80829** the prior execution is returned.
80830**
80831** This routine sets the error code and string returned by
80832** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
80833*/
80834SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){
80835 int rc;
80836 if( pStmt==0 ){
80837 rc = SQLITE_OK;
80838 }else{
80839 Vdbe *v = (Vdbe*)pStmt;
80840 sqlite3 *db = v->db;
80841 sqlite3_mutex_enter(db->mutex);
80842 checkProfileCallback(db, v);
80843 rc = sqlite3VdbeReset(v);
80844 sqlite3VdbeRewind(v);
80845 assert( (rc & (db->errMask))==rc );
80846 rc = sqlite3ApiExit(db, rc);
80847 sqlite3_mutex_leave(db->mutex);
80848 }
80849 return rc;
80850}
80851
80852/*
80853** Set all the parameters in the compiled SQL statement to NULL.
80854*/
80855SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
80856 int i;
80857 int rc = SQLITE_OK;
80858 Vdbe *p = (Vdbe*)pStmt;
80859#if SQLITE_THREADSAFE
80860 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
80861#endif
80862 sqlite3_mutex_enter(mutex);
80863 for(i=0; i<p->nVar; i++){
80864 sqlite3VdbeMemRelease(&p->aVar[i]);
80865 p->aVar[i].flags = MEM_Null;
80866 }
80867 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
80868 if( p->expmask ){
80869 p->expired = 1;
80870 }
80871 sqlite3_mutex_leave(mutex);
80872 return rc;
80873}
80874
80875
80876/**************************** sqlite3_value_ *******************************
80877** The following routines extract information from a Mem or sqlite3_value
80878** structure.
80879*/
80880SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){
80881 Mem *p = (Mem*)pVal;
80882 if( p->flags & (MEM_Blob|MEM_Str) ){
80883 if( ExpandBlob(p)!=SQLITE_OK ){
80884 assert( p->flags==MEM_Null && p->z==0 );
80885 return 0;
80886 }
80887 p->flags |= MEM_Blob;
80888 return p->n ? p->z : 0;
80889 }else{
80890 return sqlite3_value_text(pVal);
80891 }
80892}
80893SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){
80894 return sqlite3ValueBytes(pVal, SQLITE_UTF8);
80895}
80896SQLITE_API int sqlite3_value_bytes16(sqlite3_value *pVal){
80897 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
80898}
80899SQLITE_API double sqlite3_value_double(sqlite3_value *pVal){
80900 return sqlite3VdbeRealValue((Mem*)pVal);
80901}
80902SQLITE_API int sqlite3_value_int(sqlite3_value *pVal){
80903 return (int)sqlite3VdbeIntValue((Mem*)pVal);
80904}
80905SQLITE_API sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
80906 return sqlite3VdbeIntValue((Mem*)pVal);
80907}
80908SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value *pVal){
80909 Mem *pMem = (Mem*)pVal;
80910 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
80911}
80912SQLITE_API void *sqlite3_value_pointer(sqlite3_value *pVal, const char *zPType){
80913 Mem *p = (Mem*)pVal;
80914 if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
80915 (MEM_Null|MEM_Term|MEM_Subtype)
80916 && zPType!=0
80917 && p->eSubtype=='p'
80918 && strcmp(p->u.zPType, zPType)==0
80919 ){
80920 return (void*)p->z;
80921 }else{
80922 return 0;
80923 }
80924}
80925SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
80926 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
80927}
80928#ifndef SQLITE_OMIT_UTF16
80929SQLITE_API const void *sqlite3_value_text16(sqlite3_value* pVal){
80930 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
80931}
80932SQLITE_API const void *sqlite3_value_text16be(sqlite3_value *pVal){
80933 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
80934}
80935SQLITE_API const void *sqlite3_value_text16le(sqlite3_value *pVal){
80936 return sqlite3ValueText(pVal, SQLITE_UTF16LE);
80937}
80938#endif /* SQLITE_OMIT_UTF16 */
80939/* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
80940** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
80941** point number string BLOB NULL
80942*/
80943SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){
80944 static const u8 aType[] = {
80945 SQLITE_BLOB, /* 0x00 */
80946 SQLITE_NULL, /* 0x01 */
80947 SQLITE_TEXT, /* 0x02 */
80948 SQLITE_NULL, /* 0x03 */
80949 SQLITE_INTEGER, /* 0x04 */
80950 SQLITE_NULL, /* 0x05 */
80951 SQLITE_INTEGER, /* 0x06 */
80952 SQLITE_NULL, /* 0x07 */
80953 SQLITE_FLOAT, /* 0x08 */
80954 SQLITE_NULL, /* 0x09 */
80955 SQLITE_FLOAT, /* 0x0a */
80956 SQLITE_NULL, /* 0x0b */
80957 SQLITE_INTEGER, /* 0x0c */
80958 SQLITE_NULL, /* 0x0d */
80959 SQLITE_INTEGER, /* 0x0e */
80960 SQLITE_NULL, /* 0x0f */
80961 SQLITE_BLOB, /* 0x10 */
80962 SQLITE_NULL, /* 0x11 */
80963 SQLITE_TEXT, /* 0x12 */
80964 SQLITE_NULL, /* 0x13 */
80965 SQLITE_INTEGER, /* 0x14 */
80966 SQLITE_NULL, /* 0x15 */
80967 SQLITE_INTEGER, /* 0x16 */
80968 SQLITE_NULL, /* 0x17 */
80969 SQLITE_FLOAT, /* 0x18 */
80970 SQLITE_NULL, /* 0x19 */
80971 SQLITE_FLOAT, /* 0x1a */
80972 SQLITE_NULL, /* 0x1b */
80973 SQLITE_INTEGER, /* 0x1c */
80974 SQLITE_NULL, /* 0x1d */
80975 SQLITE_INTEGER, /* 0x1e */
80976 SQLITE_NULL, /* 0x1f */
80977 };
80978 return aType[pVal->flags&MEM_AffMask];
80979}
80980
80981/* Return true if a parameter to xUpdate represents an unchanged column */
80982SQLITE_API int sqlite3_value_nochange(sqlite3_value *pVal){
80983 return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
80984}
80985
80986/* Make a copy of an sqlite3_value object
80987*/
80988SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){
80989 sqlite3_value *pNew;
80990 if( pOrig==0 ) return 0;
80991 pNew = sqlite3_malloc( sizeof(*pNew) );
80992 if( pNew==0 ) return 0;
80993 memset(pNew, 0, sizeof(*pNew));
80994 memcpy(pNew, pOrig, MEMCELLSIZE);
80995 pNew->flags &= ~MEM_Dyn;
80996 pNew->db = 0;
80997 if( pNew->flags&(MEM_Str|MEM_Blob) ){
80998 pNew->flags &= ~(MEM_Static|MEM_Dyn);
80999 pNew->flags |= MEM_Ephem;
81000 if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){
81001 sqlite3ValueFree(pNew);
81002 pNew = 0;
81003 }
81004 }
81005 return pNew;
81006}
81007
81008/* Destroy an sqlite3_value object previously obtained from
81009** sqlite3_value_dup().
81010*/
81011SQLITE_API void sqlite3_value_free(sqlite3_value *pOld){
81012 sqlite3ValueFree(pOld);
81013}
81014
81015
81016/**************************** sqlite3_result_ *******************************
81017** The following routines are used by user-defined functions to specify
81018** the function result.
81019**
81020** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
81021** result as a string or blob but if the string or blob is too large, it
81022** then sets the error code to SQLITE_TOOBIG
81023**
81024** The invokeValueDestructor(P,X) routine invokes destructor function X()
81025** on value P is not going to be used and need to be destroyed.
81026*/
81027static void setResultStrOrError(
81028 sqlite3_context *pCtx, /* Function context */
81029 const char *z, /* String pointer */
81030 int n, /* Bytes in string, or negative */
81031 u8 enc, /* Encoding of z. 0 for BLOBs */
81032 void (*xDel)(void*) /* Destructor function */
81033){
81034 if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){
81035 sqlite3_result_error_toobig(pCtx);
81036 }
81037}
81038static int invokeValueDestructor(
81039 const void *p, /* Value to destroy */
81040 void (*xDel)(void*), /* The destructor */
81041 sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if no NULL */
81042){
81043 assert( xDel!=SQLITE_DYNAMIC );
81044 if( xDel==0 ){
81045 /* noop */
81046 }else if( xDel==SQLITE_TRANSIENT ){
81047 /* noop */
81048 }else{
81049 xDel((void*)p);
81050 }
81051 if( pCtx ) sqlite3_result_error_toobig(pCtx);
81052 return SQLITE_TOOBIG;
81053}
81054SQLITE_API void sqlite3_result_blob(
81055 sqlite3_context *pCtx,
81056 const void *z,
81057 int n,
81058 void (*xDel)(void *)
81059){
81060 assert( n>=0 );
81061 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
81062 setResultStrOrError(pCtx, z, n, 0, xDel);
81063}
81064SQLITE_API void sqlite3_result_blob64(
81065 sqlite3_context *pCtx,
81066 const void *z,
81067 sqlite3_uint64 n,
81068 void (*xDel)(void *)
81069){
81070 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
81071 assert( xDel!=SQLITE_DYNAMIC );
81072 if( n>0x7fffffff ){
81073 (void)invokeValueDestructor(z, xDel, pCtx);
81074 }else{
81075 setResultStrOrError(pCtx, z, (int)n, 0, xDel);
81076 }
81077}
81078SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
81079 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
81080 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
81081}
81082SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
81083 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
81084 pCtx->isError = SQLITE_ERROR;
81085 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
81086}
81087#ifndef SQLITE_OMIT_UTF16
81088SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
81089 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
81090 pCtx->isError = SQLITE_ERROR;
81091 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
81092}
81093#endif
81094SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
81095 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
81096 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
81097}
81098SQLITE_API void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
81099 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
81100 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
81101}
81102SQLITE_API void sqlite3_result_null(sqlite3_context *pCtx){
81103 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
81104 sqlite3VdbeMemSetNull(pCtx->pOut);
81105}
81106SQLITE_API void sqlite3_result_pointer(
81107 sqlite3_context *pCtx,
81108 void *pPtr,
81109 const char *zPType,
81110 void (*xDestructor)(void*)
81111){
81112 Mem *pOut = pCtx->pOut;
81113 assert( sqlite3_mutex_held(pOut->db->mutex) );
81114 sqlite3VdbeMemRelease(pOut);
81115 pOut->flags = MEM_Null;
81116 sqlite3VdbeMemSetPointer(pOut, pPtr, zPType, xDestructor);
81117}
81118SQLITE_API void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
81119 Mem *pOut = pCtx->pOut;
81120 assert( sqlite3_mutex_held(pOut->db->mutex) );
81121 pOut->eSubtype = eSubtype & 0xff;
81122 pOut->flags |= MEM_Subtype;
81123}
81124SQLITE_API void sqlite3_result_text(
81125 sqlite3_context *pCtx,
81126 const char *z,
81127 int n,
81128 void (*xDel)(void *)
81129){
81130 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
81131 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
81132}
81133SQLITE_API void sqlite3_result_text64(
81134 sqlite3_context *pCtx,
81135 const char *z,
81136 sqlite3_uint64 n,
81137 void (*xDel)(void *),
81138 unsigned char enc
81139){
81140 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
81141 assert( xDel!=SQLITE_DYNAMIC );
81142 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
81143 if( n>0x7fffffff ){
81144 (void)invokeValueDestructor(z, xDel, pCtx);
81145 }else{
81146 setResultStrOrError(pCtx, z, (int)n, enc, xDel);
81147 }
81148}
81149#ifndef SQLITE_OMIT_UTF16
81150SQLITE_API void sqlite3_result_text16(
81151 sqlite3_context *pCtx,
81152 const void *z,
81153 int n,
81154 void (*xDel)(void *)
81155){
81156 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
81157 setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
81158}
81159SQLITE_API void sqlite3_result_text16be(
81160 sqlite3_context *pCtx,
81161 const void *z,
81162 int n,
81163 void (*xDel)(void *)
81164){
81165 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
81166 setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
81167}
81168SQLITE_API void sqlite3_result_text16le(
81169 sqlite3_context *pCtx,
81170 const void *z,
81171 int n,
81172 void (*xDel)(void *)
81173){
81174 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
81175 setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
81176}
81177#endif /* SQLITE_OMIT_UTF16 */
81178SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
81179 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
81180 sqlite3VdbeMemCopy(pCtx->pOut, pValue);
81181}
81182SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
81183 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
81184 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n);
81185}
81186SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){
81187 Mem *pOut = pCtx->pOut;
81188 assert( sqlite3_mutex_held(pOut->db->mutex) );
81189 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
81190 return SQLITE_TOOBIG;
81191 }
81192 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
81193 return SQLITE_OK;
81194}
81195SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
81196 pCtx->isError = errCode ? errCode : -1;
81197#ifdef SQLITE_DEBUG
81198 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
81199#endif
81200 if( pCtx->pOut->flags & MEM_Null ){
81201 sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1,
81202 SQLITE_UTF8, SQLITE_STATIC);
81203 }
81204}
81205
81206/* Force an SQLITE_TOOBIG error. */
81207SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
81208 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
81209 pCtx->isError = SQLITE_TOOBIG;
81210 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
81211 SQLITE_UTF8, SQLITE_STATIC);
81212}
81213
81214/* An SQLITE_NOMEM error. */
81215SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
81216 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
81217 sqlite3VdbeMemSetNull(pCtx->pOut);
81218 pCtx->isError = SQLITE_NOMEM_BKPT;
81219 sqlite3OomFault(pCtx->pOut->db);
81220}
81221
81222/*
81223** This function is called after a transaction has been committed. It
81224** invokes callbacks registered with sqlite3_wal_hook() as required.
81225*/
81226static int doWalCallbacks(sqlite3 *db){
81227 int rc = SQLITE_OK;
81228#ifndef SQLITE_OMIT_WAL
81229 int i;
81230 for(i=0; i<db->nDb; i++){
81231 Btree *pBt = db->aDb[i].pBt;
81232 if( pBt ){
81233 int nEntry;
81234 sqlite3BtreeEnter(pBt);
81235 nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
81236 sqlite3BtreeLeave(pBt);
81237 if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){
81238 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
81239 }
81240 }
81241 }
81242#endif
81243 return rc;
81244}
81245
81246
81247/*
81248** Execute the statement pStmt, either until a row of data is ready, the
81249** statement is completely executed or an error occurs.
81250**
81251** This routine implements the bulk of the logic behind the sqlite_step()
81252** API. The only thing omitted is the automatic recompile if a
81253** schema change has occurred. That detail is handled by the
81254** outer sqlite3_step() wrapper procedure.
81255*/
81256static int sqlite3Step(Vdbe *p){
81257 sqlite3 *db;
81258 int rc;
81259
81260 assert(p);
81261 if( p->magic!=VDBE_MAGIC_RUN ){
81262 /* We used to require that sqlite3_reset() be called before retrying
81263 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning
81264 ** with version 3.7.0, we changed this so that sqlite3_reset() would
81265 ** be called automatically instead of throwing the SQLITE_MISUSE error.
81266 ** This "automatic-reset" change is not technically an incompatibility,
81267 ** since any application that receives an SQLITE_MISUSE is broken by
81268 ** definition.
81269 **
81270 ** Nevertheless, some published applications that were originally written
81271 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
81272 ** returns, and those were broken by the automatic-reset change. As a
81273 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
81274 ** legacy behavior of returning SQLITE_MISUSE for cases where the
81275 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
81276 ** or SQLITE_BUSY error.
81277 */
81278#ifdef SQLITE_OMIT_AUTORESET
81279 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
81280 sqlite3_reset((sqlite3_stmt*)p);
81281 }else{
81282 return SQLITE_MISUSE_BKPT;
81283 }
81284#else
81285 sqlite3_reset((sqlite3_stmt*)p);
81286#endif
81287 }
81288
81289 /* Check that malloc() has not failed. If it has, return early. */
81290 db = p->db;
81291 if( db->mallocFailed ){
81292 p->rc = SQLITE_NOMEM;
81293 return SQLITE_NOMEM_BKPT;
81294 }
81295
81296 if( p->pc<=0 && p->expired ){
81297 p->rc = SQLITE_SCHEMA;
81298 rc = SQLITE_ERROR;
81299 goto end_of_step;
81300 }
81301 if( p->pc<0 ){
81302 /* If there are no other statements currently running, then
81303 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
81304 ** from interrupting a statement that has not yet started.
81305 */
81306 if( db->nVdbeActive==0 ){
81307 db->u1.isInterrupted = 0;
81308 }
81309
81310 assert( db->nVdbeWrite>0 || db->autoCommit==0
81311 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
81312 );
81313
81314#ifndef SQLITE_OMIT_TRACE
81315 if( (db->xProfile || (db->mTrace & SQLITE_TRACE_PROFILE)!=0)
81316 && !db->init.busy && p->zSql ){
81317 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
81318 }else{
81319 assert( p->startTime==0 );
81320 }
81321#endif
81322
81323 db->nVdbeActive++;
81324 if( p->readOnly==0 ) db->nVdbeWrite++;
81325 if( p->bIsReader ) db->nVdbeRead++;
81326 p->pc = 0;
81327 }
81328#ifdef SQLITE_DEBUG
81329 p->rcApp = SQLITE_OK;
81330#endif
81331#ifndef SQLITE_OMIT_EXPLAIN
81332 if( p->explain ){
81333 rc = sqlite3VdbeList(p);
81334 }else
81335#endif /* SQLITE_OMIT_EXPLAIN */
81336 {
81337 db->nVdbeExec++;
81338 rc = sqlite3VdbeExec(p);
81339 db->nVdbeExec--;
81340 }
81341
81342#ifndef SQLITE_OMIT_TRACE
81343 /* If the statement completed successfully, invoke the profile callback */
81344 if( rc!=SQLITE_ROW ) checkProfileCallback(db, p);
81345#endif
81346
81347 if( rc==SQLITE_DONE && db->autoCommit ){
81348 assert( p->rc==SQLITE_OK );
81349 p->rc = doWalCallbacks(db);
81350 if( p->rc!=SQLITE_OK ){
81351 rc = SQLITE_ERROR;
81352 }
81353 }
81354
81355 db->errCode = rc;
81356 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
81357 p->rc = SQLITE_NOMEM_BKPT;
81358 }
81359end_of_step:
81360 /* At this point local variable rc holds the value that should be
81361 ** returned if this statement was compiled using the legacy
81362 ** sqlite3_prepare() interface. According to the docs, this can only
81363 ** be one of the values in the first assert() below. Variable p->rc
81364 ** contains the value that would be returned if sqlite3_finalize()
81365 ** were called on statement p.
81366 */
81367 assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR
81368 || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE
81369 );
81370 assert( (p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE) || p->rc==p->rcApp );
81371 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0
81372 && rc!=SQLITE_ROW
81373 && rc!=SQLITE_DONE
81374 ){
81375 /* If this statement was prepared using saved SQL and an
81376 ** error has occurred, then return the error code in p->rc to the
81377 ** caller. Set the error code in the database handle to the same value.
81378 */
81379 rc = sqlite3VdbeTransferError(p);
81380 }
81381 return (rc&db->errMask);
81382}
81383
81384/*
81385** This is the top-level implementation of sqlite3_step(). Call
81386** sqlite3Step() to do most of the work. If a schema error occurs,
81387** call sqlite3Reprepare() and try again.
81388*/
81389SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
81390 int rc = SQLITE_OK; /* Result from sqlite3Step() */
81391 Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */
81392 int cnt = 0; /* Counter to prevent infinite loop of reprepares */
81393 sqlite3 *db; /* The database connection */
81394
81395 if( vdbeSafetyNotNull(v) ){
81396 return SQLITE_MISUSE_BKPT;
81397 }
81398 db = v->db;
81399 sqlite3_mutex_enter(db->mutex);
81400 v->doingRerun = 0;
81401 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
81402 && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){
81403 int savedPc = v->pc;
81404 rc = sqlite3Reprepare(v);
81405 if( rc!=SQLITE_OK ){
81406 /* This case occurs after failing to recompile an sql statement.
81407 ** The error message from the SQL compiler has already been loaded
81408 ** into the database handle. This block copies the error message
81409 ** from the database handle into the statement and sets the statement
81410 ** program counter to 0 to ensure that when the statement is
81411 ** finalized or reset the parser error message is available via
81412 ** sqlite3_errmsg() and sqlite3_errcode().
81413 */
81414 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
81415 sqlite3DbFree(db, v->zErrMsg);
81416 if( !db->mallocFailed ){
81417 v->zErrMsg = sqlite3DbStrDup(db, zErr);
81418 v->rc = rc = sqlite3ApiExit(db, rc);
81419 } else {
81420 v->zErrMsg = 0;
81421 v->rc = rc = SQLITE_NOMEM_BKPT;
81422 }
81423 break;
81424 }
81425 sqlite3_reset(pStmt);
81426 if( savedPc>=0 ) v->doingRerun = 1;
81427 assert( v->expired==0 );
81428 }
81429 sqlite3_mutex_leave(db->mutex);
81430 return rc;
81431}
81432
81433
81434/*
81435** Extract the user data from a sqlite3_context structure and return a
81436** pointer to it.
81437*/
81438SQLITE_API void *sqlite3_user_data(sqlite3_context *p){
81439 assert( p && p->pFunc );
81440 return p->pFunc->pUserData;
81441}
81442
81443/*
81444** Extract the user data from a sqlite3_context structure and return a
81445** pointer to it.
81446**
81447** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
81448** returns a copy of the pointer to the database connection (the 1st
81449** parameter) of the sqlite3_create_function() and
81450** sqlite3_create_function16() routines that originally registered the
81451** application defined function.
81452*/
81453SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
81454 assert( p && p->pOut );
81455 return p->pOut->db;
81456}
81457
81458/*
81459** If this routine is invoked from within an xColumn method of a virtual
81460** table, then it returns true if and only if the the call is during an
81461** UPDATE operation and the value of the column will not be modified
81462** by the UPDATE.
81463**
81464** If this routine is called from any context other than within the
81465** xColumn method of a virtual table, then the return value is meaningless
81466** and arbitrary.
81467**
81468** Virtual table implements might use this routine to optimize their
81469** performance by substituting a NULL result, or some other light-weight
81470** value, as a signal to the xUpdate routine that the column is unchanged.
81471*/
81472SQLITE_API int sqlite3_vtab_nochange(sqlite3_context *p){
81473 assert( p );
81474 return sqlite3_value_nochange(p->pOut);
81475}
81476
81477/*
81478** Return the current time for a statement. If the current time
81479** is requested more than once within the same run of a single prepared
81480** statement, the exact same time is returned for each invocation regardless
81481** of the amount of time that elapses between invocations. In other words,
81482** the time returned is always the time of the first call.
81483*/
81484SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
81485 int rc;
81486#ifndef SQLITE_ENABLE_STAT3_OR_STAT4
81487 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
81488 assert( p->pVdbe!=0 );
81489#else
81490 sqlite3_int64 iTime = 0;
81491 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
81492#endif
81493 if( *piTime==0 ){
81494 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
81495 if( rc ) *piTime = 0;
81496 }
81497 return *piTime;
81498}
81499
81500/*
81501** Create a new aggregate context for p and return a pointer to
81502** its pMem->z element.
81503*/
81504static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
81505 Mem *pMem = p->pMem;
81506 assert( (pMem->flags & MEM_Agg)==0 );
81507 if( nByte<=0 ){
81508 sqlite3VdbeMemSetNull(pMem);
81509 pMem->z = 0;
81510 }else{
81511 sqlite3VdbeMemClearAndResize(pMem, nByte);
81512 pMem->flags = MEM_Agg;
81513 pMem->u.pDef = p->pFunc;
81514 if( pMem->z ){
81515 memset(pMem->z, 0, nByte);
81516 }
81517 }
81518 return (void*)pMem->z;
81519}
81520
81521/*
81522** Allocate or return the aggregate context for a user function. A new
81523** context is allocated on the first call. Subsequent calls return the
81524** same context that was returned on prior calls.
81525*/
81526SQLITE_API void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
81527 assert( p && p->pFunc && p->pFunc->xFinalize );
81528 assert( sqlite3_mutex_held(p->pOut->db->mutex) );
81529 testcase( nByte<0 );
81530 if( (p->pMem->flags & MEM_Agg)==0 ){
81531 return createAggContext(p, nByte);
81532 }else{
81533 return (void*)p->pMem->z;
81534 }
81535}
81536
81537/*
81538** Return the auxiliary data pointer, if any, for the iArg'th argument to
81539** the user-function defined by pCtx.
81540**
81541** The left-most argument is 0.
81542**
81543** Undocumented behavior: If iArg is negative then access a cache of
81544** auxiliary data pointers that is available to all functions within a
81545** single prepared statement. The iArg values must match.
81546*/
81547SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
81548 AuxData *pAuxData;
81549
81550 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
81551#if SQLITE_ENABLE_STAT3_OR_STAT4
81552 if( pCtx->pVdbe==0 ) return 0;
81553#else
81554 assert( pCtx->pVdbe!=0 );
81555#endif
81556 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
81557 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
81558 return pAuxData->pAux;
81559 }
81560 }
81561 return 0;
81562}
81563
81564/*
81565** Set the auxiliary data pointer and delete function, for the iArg'th
81566** argument to the user-function defined by pCtx. Any previous value is
81567** deleted by calling the delete function specified when it was set.
81568**
81569** The left-most argument is 0.
81570**
81571** Undocumented behavior: If iArg is negative then make the data available
81572** to all functions within the current prepared statement using iArg as an
81573** access code.
81574*/
81575SQLITE_API void sqlite3_set_auxdata(
81576 sqlite3_context *pCtx,
81577 int iArg,
81578 void *pAux,
81579 void (*xDelete)(void*)
81580){
81581 AuxData *pAuxData;
81582 Vdbe *pVdbe = pCtx->pVdbe;
81583
81584 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
81585#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81586 if( pVdbe==0 ) goto failed;
81587#else
81588 assert( pVdbe!=0 );
81589#endif
81590
81591 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
81592 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
81593 break;
81594 }
81595 }
81596 if( pAuxData==0 ){
81597 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
81598 if( !pAuxData ) goto failed;
81599 pAuxData->iAuxOp = pCtx->iOp;
81600 pAuxData->iAuxArg = iArg;
81601 pAuxData->pNextAux = pVdbe->pAuxData;
81602 pVdbe->pAuxData = pAuxData;
81603 if( pCtx->isError==0 ) pCtx->isError = -1;
81604 }else if( pAuxData->xDeleteAux ){
81605 pAuxData->xDeleteAux(pAuxData->pAux);
81606 }
81607
81608 pAuxData->pAux = pAux;
81609 pAuxData->xDeleteAux = xDelete;
81610 return;
81611
81612failed:
81613 if( xDelete ){
81614 xDelete(pAux);
81615 }
81616}
81617
81618#ifndef SQLITE_OMIT_DEPRECATED
81619/*
81620** Return the number of times the Step function of an aggregate has been
81621** called.
81622**
81623** This function is deprecated. Do not use it for new code. It is
81624** provide only to avoid breaking legacy code. New aggregate function
81625** implementations should keep their own counts within their aggregate
81626** context.
81627*/
81628SQLITE_API int sqlite3_aggregate_count(sqlite3_context *p){
81629 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
81630 return p->pMem->n;
81631}
81632#endif
81633
81634/*
81635** Return the number of columns in the result set for the statement pStmt.
81636*/
81637SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){
81638 Vdbe *pVm = (Vdbe *)pStmt;
81639 return pVm ? pVm->nResColumn : 0;
81640}
81641
81642/*
81643** Return the number of values available from the current row of the
81644** currently executing statement pStmt.
81645*/
81646SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){
81647 Vdbe *pVm = (Vdbe *)pStmt;
81648 if( pVm==0 || pVm->pResultSet==0 ) return 0;
81649 return pVm->nResColumn;
81650}
81651
81652/*
81653** Return a pointer to static memory containing an SQL NULL value.
81654*/
81655static const Mem *columnNullValue(void){
81656 /* Even though the Mem structure contains an element
81657 ** of type i64, on certain architectures (x86) with certain compiler
81658 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
81659 ** instead of an 8-byte one. This all works fine, except that when
81660 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
81661 ** that a Mem structure is located on an 8-byte boundary. To prevent
81662 ** these assert()s from failing, when building with SQLITE_DEBUG defined
81663 ** using gcc, we force nullMem to be 8-byte aligned using the magical
81664 ** __attribute__((aligned(8))) macro. */
81665 static const Mem nullMem
81666#if defined(SQLITE_DEBUG) && defined(__GNUC__)
81667 __attribute__((aligned(8)))
81668#endif
81669 = {
81670 /* .u = */ {0},
81671 /* .flags = */ (u16)MEM_Null,
81672 /* .enc = */ (u8)0,
81673 /* .eSubtype = */ (u8)0,
81674 /* .n = */ (int)0,
81675 /* .z = */ (char*)0,
81676 /* .zMalloc = */ (char*)0,
81677 /* .szMalloc = */ (int)0,
81678 /* .uTemp = */ (u32)0,
81679 /* .db = */ (sqlite3*)0,
81680 /* .xDel = */ (void(*)(void*))0,
81681#ifdef SQLITE_DEBUG
81682 /* .pScopyFrom = */ (Mem*)0,
81683 /* .mScopyFlags= */ 0,
81684#endif
81685 };
81686 return &nullMem;
81687}
81688
81689/*
81690** Check to see if column iCol of the given statement is valid. If
81691** it is, return a pointer to the Mem for the value of that column.
81692** If iCol is not valid, return a pointer to a Mem which has a value
81693** of NULL.
81694*/
81695static Mem *columnMem(sqlite3_stmt *pStmt, int i){
81696 Vdbe *pVm;
81697 Mem *pOut;
81698
81699 pVm = (Vdbe *)pStmt;
81700 if( pVm==0 ) return (Mem*)columnNullValue();
81701 assert( pVm->db );
81702 sqlite3_mutex_enter(pVm->db->mutex);
81703 if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
81704 pOut = &pVm->pResultSet[i];
81705 }else{
81706 sqlite3Error(pVm->db, SQLITE_RANGE);
81707 pOut = (Mem*)columnNullValue();
81708 }
81709 return pOut;
81710}
81711
81712/*
81713** This function is called after invoking an sqlite3_value_XXX function on a
81714** column value (i.e. a value returned by evaluating an SQL expression in the
81715** select list of a SELECT statement) that may cause a malloc() failure. If
81716** malloc() has failed, the threads mallocFailed flag is cleared and the result
81717** code of statement pStmt set to SQLITE_NOMEM.
81718**
81719** Specifically, this is called from within:
81720**
81721** sqlite3_column_int()
81722** sqlite3_column_int64()
81723** sqlite3_column_text()
81724** sqlite3_column_text16()
81725** sqlite3_column_real()
81726** sqlite3_column_bytes()
81727** sqlite3_column_bytes16()
81728** sqiite3_column_blob()
81729*/
81730static void columnMallocFailure(sqlite3_stmt *pStmt)
81731{
81732 /* If malloc() failed during an encoding conversion within an
81733 ** sqlite3_column_XXX API, then set the return code of the statement to
81734 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
81735 ** and _finalize() will return NOMEM.
81736 */
81737 Vdbe *p = (Vdbe *)pStmt;
81738 if( p ){
81739 assert( p->db!=0 );
81740 assert( sqlite3_mutex_held(p->db->mutex) );
81741 p->rc = sqlite3ApiExit(p->db, p->rc);
81742 sqlite3_mutex_leave(p->db->mutex);
81743 }
81744}
81745
81746/**************************** sqlite3_column_ *******************************
81747** The following routines are used to access elements of the current row
81748** in the result set.
81749*/
81750SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
81751 const void *val;
81752 val = sqlite3_value_blob( columnMem(pStmt,i) );
81753 /* Even though there is no encoding conversion, value_blob() might
81754 ** need to call malloc() to expand the result of a zeroblob()
81755 ** expression.
81756 */
81757 columnMallocFailure(pStmt);
81758 return val;
81759}
81760SQLITE_API int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
81761 int val = sqlite3_value_bytes( columnMem(pStmt,i) );
81762 columnMallocFailure(pStmt);
81763 return val;
81764}
81765SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
81766 int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
81767 columnMallocFailure(pStmt);
81768 return val;
81769}
81770SQLITE_API double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
81771 double val = sqlite3_value_double( columnMem(pStmt,i) );
81772 columnMallocFailure(pStmt);
81773 return val;
81774}
81775SQLITE_API int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
81776 int val = sqlite3_value_int( columnMem(pStmt,i) );
81777 columnMallocFailure(pStmt);
81778 return val;
81779}
81780SQLITE_API sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
81781 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
81782 columnMallocFailure(pStmt);
81783 return val;
81784}
81785SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
81786 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
81787 columnMallocFailure(pStmt);
81788 return val;
81789}
81790SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
81791 Mem *pOut = columnMem(pStmt, i);
81792 if( pOut->flags&MEM_Static ){
81793 pOut->flags &= ~MEM_Static;
81794 pOut->flags |= MEM_Ephem;
81795 }
81796 columnMallocFailure(pStmt);
81797 return (sqlite3_value *)pOut;
81798}
81799#ifndef SQLITE_OMIT_UTF16
81800SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
81801 const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
81802 columnMallocFailure(pStmt);
81803 return val;
81804}
81805#endif /* SQLITE_OMIT_UTF16 */
81806SQLITE_API int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
81807 int iType = sqlite3_value_type( columnMem(pStmt,i) );
81808 columnMallocFailure(pStmt);
81809 return iType;
81810}
81811
81812/*
81813** Convert the N-th element of pStmt->pColName[] into a string using
81814** xFunc() then return that string. If N is out of range, return 0.
81815**
81816** There are up to 5 names for each column. useType determines which
81817** name is returned. Here are the names:
81818**
81819** 0 The column name as it should be displayed for output
81820** 1 The datatype name for the column
81821** 2 The name of the database that the column derives from
81822** 3 The name of the table that the column derives from
81823** 4 The name of the table column that the result column derives from
81824**
81825** If the result is not a simple column reference (if it is an expression
81826** or a constant) then useTypes 2, 3, and 4 return NULL.
81827*/
81828static const void *columnName(
81829 sqlite3_stmt *pStmt,
81830 int N,
81831 const void *(*xFunc)(Mem*),
81832 int useType
81833){
81834 const void *ret;
81835 Vdbe *p;
81836 int n;
81837 sqlite3 *db;
81838#ifdef SQLITE_ENABLE_API_ARMOR
81839 if( pStmt==0 ){
81840 (void)SQLITE_MISUSE_BKPT;
81841 return 0;
81842 }
81843#endif
81844 ret = 0;
81845 p = (Vdbe *)pStmt;
81846 db = p->db;
81847 assert( db!=0 );
81848 n = sqlite3_column_count(pStmt);
81849 if( N<n && N>=0 ){
81850 N += useType*n;
81851 sqlite3_mutex_enter(db->mutex);
81852 assert( db->mallocFailed==0 );
81853 ret = xFunc(&p->aColName[N]);
81854 /* A malloc may have failed inside of the xFunc() call. If this
81855 ** is the case, clear the mallocFailed flag and return NULL.
81856 */
81857 if( db->mallocFailed ){
81858 sqlite3OomClear(db);
81859 ret = 0;
81860 }
81861 sqlite3_mutex_leave(db->mutex);
81862 }
81863 return ret;
81864}
81865
81866/*
81867** Return the name of the Nth column of the result set returned by SQL
81868** statement pStmt.
81869*/
81870SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
81871 return columnName(
81872 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
81873}
81874#ifndef SQLITE_OMIT_UTF16
81875SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
81876 return columnName(
81877 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
81878}
81879#endif
81880
81881/*
81882** Constraint: If you have ENABLE_COLUMN_METADATA then you must
81883** not define OMIT_DECLTYPE.
81884*/
81885#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
81886# error "Must not define both SQLITE_OMIT_DECLTYPE \
81887 and SQLITE_ENABLE_COLUMN_METADATA"
81888#endif
81889
81890#ifndef SQLITE_OMIT_DECLTYPE
81891/*
81892** Return the column declaration type (if applicable) of the 'i'th column
81893** of the result set of SQL statement pStmt.
81894*/
81895SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
81896 return columnName(
81897 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
81898}
81899#ifndef SQLITE_OMIT_UTF16
81900SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
81901 return columnName(
81902 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
81903}
81904#endif /* SQLITE_OMIT_UTF16 */
81905#endif /* SQLITE_OMIT_DECLTYPE */
81906
81907#ifdef SQLITE_ENABLE_COLUMN_METADATA
81908/*
81909** Return the name of the database from which a result column derives.
81910** NULL is returned if the result column is an expression or constant or
81911** anything else which is not an unambiguous reference to a database column.
81912*/
81913SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
81914 return columnName(
81915 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
81916}
81917#ifndef SQLITE_OMIT_UTF16
81918SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
81919 return columnName(
81920 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
81921}
81922#endif /* SQLITE_OMIT_UTF16 */
81923
81924/*
81925** Return the name of the table from which a result column derives.
81926** NULL is returned if the result column is an expression or constant or
81927** anything else which is not an unambiguous reference to a database column.
81928*/
81929SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
81930 return columnName(
81931 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
81932}
81933#ifndef SQLITE_OMIT_UTF16
81934SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
81935 return columnName(
81936 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
81937}
81938#endif /* SQLITE_OMIT_UTF16 */
81939
81940/*
81941** Return the name of the table column from which a result column derives.
81942** NULL is returned if the result column is an expression or constant or
81943** anything else which is not an unambiguous reference to a database column.
81944*/
81945SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
81946 return columnName(
81947 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
81948}
81949#ifndef SQLITE_OMIT_UTF16
81950SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
81951 return columnName(
81952 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
81953}
81954#endif /* SQLITE_OMIT_UTF16 */
81955#endif /* SQLITE_ENABLE_COLUMN_METADATA */
81956
81957
81958/******************************* sqlite3_bind_ ***************************
81959**
81960** Routines used to attach values to wildcards in a compiled SQL statement.
81961*/
81962/*
81963** Unbind the value bound to variable i in virtual machine p. This is the
81964** the same as binding a NULL value to the column. If the "i" parameter is
81965** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
81966**
81967** A successful evaluation of this routine acquires the mutex on p.
81968** the mutex is released if any kind of error occurs.
81969**
81970** The error code stored in database p->db is overwritten with the return
81971** value in any case.
81972*/
81973static int vdbeUnbind(Vdbe *p, int i){
81974 Mem *pVar;
81975 if( vdbeSafetyNotNull(p) ){
81976 return SQLITE_MISUSE_BKPT;
81977 }
81978 sqlite3_mutex_enter(p->db->mutex);
81979 if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
81980 sqlite3Error(p->db, SQLITE_MISUSE);
81981 sqlite3_mutex_leave(p->db->mutex);
81982 sqlite3_log(SQLITE_MISUSE,
81983 "bind on a busy prepared statement: [%s]", p->zSql);
81984 return SQLITE_MISUSE_BKPT;
81985 }
81986 if( i<1 || i>p->nVar ){
81987 sqlite3Error(p->db, SQLITE_RANGE);
81988 sqlite3_mutex_leave(p->db->mutex);
81989 return SQLITE_RANGE;
81990 }
81991 i--;
81992 pVar = &p->aVar[i];
81993 sqlite3VdbeMemRelease(pVar);
81994 pVar->flags = MEM_Null;
81995 sqlite3Error(p->db, SQLITE_OK);
81996
81997 /* If the bit corresponding to this variable in Vdbe.expmask is set, then
81998 ** binding a new value to this variable invalidates the current query plan.
81999 **
82000 ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
82001 ** parameter in the WHERE clause might influence the choice of query plan
82002 ** for a statement, then the statement will be automatically recompiled,
82003 ** as if there had been a schema change, on the first sqlite3_step() call
82004 ** following any change to the bindings of that parameter.
82005 */
82006 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
82007 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
82008 p->expired = 1;
82009 }
82010 return SQLITE_OK;
82011}
82012
82013/*
82014** Bind a text or BLOB value.
82015*/
82016static int bindText(
82017 sqlite3_stmt *pStmt, /* The statement to bind against */
82018 int i, /* Index of the parameter to bind */
82019 const void *zData, /* Pointer to the data to be bound */
82020 int nData, /* Number of bytes of data to be bound */
82021 void (*xDel)(void*), /* Destructor for the data */
82022 u8 encoding /* Encoding for the data */
82023){
82024 Vdbe *p = (Vdbe *)pStmt;
82025 Mem *pVar;
82026 int rc;
82027
82028 rc = vdbeUnbind(p, i);
82029 if( rc==SQLITE_OK ){
82030 if( zData!=0 ){
82031 pVar = &p->aVar[i-1];
82032 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
82033 if( rc==SQLITE_OK && encoding!=0 ){
82034 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
82035 }
82036 if( rc ){
82037 sqlite3Error(p->db, rc);
82038 rc = sqlite3ApiExit(p->db, rc);
82039 }
82040 }
82041 sqlite3_mutex_leave(p->db->mutex);
82042 }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
82043 xDel((void*)zData);
82044 }
82045 return rc;
82046}
82047
82048
82049/*
82050** Bind a blob value to an SQL statement variable.
82051*/
82052SQLITE_API int sqlite3_bind_blob(
82053 sqlite3_stmt *pStmt,
82054 int i,
82055 const void *zData,
82056 int nData,
82057 void (*xDel)(void*)
82058){
82059#ifdef SQLITE_ENABLE_API_ARMOR
82060 if( nData<0 ) return SQLITE_MISUSE_BKPT;
82061#endif
82062 return bindText(pStmt, i, zData, nData, xDel, 0);
82063}
82064SQLITE_API int sqlite3_bind_blob64(
82065 sqlite3_stmt *pStmt,
82066 int i,
82067 const void *zData,
82068 sqlite3_uint64 nData,
82069 void (*xDel)(void*)
82070){
82071 assert( xDel!=SQLITE_DYNAMIC );
82072 if( nData>0x7fffffff ){
82073 return invokeValueDestructor(zData, xDel, 0);
82074 }else{
82075 return bindText(pStmt, i, zData, (int)nData, xDel, 0);
82076 }
82077}
82078SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
82079 int rc;
82080 Vdbe *p = (Vdbe *)pStmt;
82081 rc = vdbeUnbind(p, i);
82082 if( rc==SQLITE_OK ){
82083 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
82084 sqlite3_mutex_leave(p->db->mutex);
82085 }
82086 return rc;
82087}
82088SQLITE_API int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
82089 return sqlite3_bind_int64(p, i, (i64)iValue);
82090}
82091SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
82092 int rc;
82093 Vdbe *p = (Vdbe *)pStmt;
82094 rc = vdbeUnbind(p, i);
82095 if( rc==SQLITE_OK ){
82096 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
82097 sqlite3_mutex_leave(p->db->mutex);
82098 }
82099 return rc;
82100}
82101SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
82102 int rc;
82103 Vdbe *p = (Vdbe*)pStmt;
82104 rc = vdbeUnbind(p, i);
82105 if( rc==SQLITE_OK ){
82106 sqlite3_mutex_leave(p->db->mutex);
82107 }
82108 return rc;
82109}
82110SQLITE_API int sqlite3_bind_pointer(
82111 sqlite3_stmt *pStmt,
82112 int i,
82113 void *pPtr,
82114 const char *zPTtype,
82115 void (*xDestructor)(void*)
82116){
82117 int rc;
82118 Vdbe *p = (Vdbe*)pStmt;
82119 rc = vdbeUnbind(p, i);
82120 if( rc==SQLITE_OK ){
82121 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
82122 sqlite3_mutex_leave(p->db->mutex);
82123 }else if( xDestructor ){
82124 xDestructor(pPtr);
82125 }
82126 return rc;
82127}
82128SQLITE_API int sqlite3_bind_text(
82129 sqlite3_stmt *pStmt,
82130 int i,
82131 const char *zData,
82132 int nData,
82133 void (*xDel)(void*)
82134){
82135 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
82136}
82137SQLITE_API int sqlite3_bind_text64(
82138 sqlite3_stmt *pStmt,
82139 int i,
82140 const char *zData,
82141 sqlite3_uint64 nData,
82142 void (*xDel)(void*),
82143 unsigned char enc
82144){
82145 assert( xDel!=SQLITE_DYNAMIC );
82146 if( nData>0x7fffffff ){
82147 return invokeValueDestructor(zData, xDel, 0);
82148 }else{
82149 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
82150 return bindText(pStmt, i, zData, (int)nData, xDel, enc);
82151 }
82152}
82153#ifndef SQLITE_OMIT_UTF16
82154SQLITE_API int sqlite3_bind_text16(
82155 sqlite3_stmt *pStmt,
82156 int i,
82157 const void *zData,
82158 int nData,
82159 void (*xDel)(void*)
82160){
82161 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
82162}
82163#endif /* SQLITE_OMIT_UTF16 */
82164SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
82165 int rc;
82166 switch( sqlite3_value_type((sqlite3_value*)pValue) ){
82167 case SQLITE_INTEGER: {
82168 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
82169 break;
82170 }
82171 case SQLITE_FLOAT: {
82172 rc = sqlite3_bind_double(pStmt, i, pValue->u.r);
82173 break;
82174 }
82175 case SQLITE_BLOB: {
82176 if( pValue->flags & MEM_Zero ){
82177 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
82178 }else{
82179 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
82180 }
82181 break;
82182 }
82183 case SQLITE_TEXT: {
82184 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
82185 pValue->enc);
82186 break;
82187 }
82188 default: {
82189 rc = sqlite3_bind_null(pStmt, i);
82190 break;
82191 }
82192 }
82193 return rc;
82194}
82195SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
82196 int rc;
82197 Vdbe *p = (Vdbe *)pStmt;
82198 rc = vdbeUnbind(p, i);
82199 if( rc==SQLITE_OK ){
82200 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
82201 sqlite3_mutex_leave(p->db->mutex);
82202 }
82203 return rc;
82204}
82205SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){
82206 int rc;
82207 Vdbe *p = (Vdbe *)pStmt;
82208 sqlite3_mutex_enter(p->db->mutex);
82209 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
82210 rc = SQLITE_TOOBIG;
82211 }else{
82212 assert( (n & 0x7FFFFFFF)==n );
82213 rc = sqlite3_bind_zeroblob(pStmt, i, n);
82214 }
82215 rc = sqlite3ApiExit(p->db, rc);
82216 sqlite3_mutex_leave(p->db->mutex);
82217 return rc;
82218}
82219
82220/*
82221** Return the number of wildcards that can be potentially bound to.
82222** This routine is added to support DBD::SQLite.
82223*/
82224SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
82225 Vdbe *p = (Vdbe*)pStmt;
82226 return p ? p->nVar : 0;
82227}
82228
82229/*
82230** Return the name of a wildcard parameter. Return NULL if the index
82231** is out of range or if the wildcard is unnamed.
82232**
82233** The result is always UTF-8.
82234*/
82235SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
82236 Vdbe *p = (Vdbe*)pStmt;
82237 if( p==0 ) return 0;
82238 return sqlite3VListNumToName(p->pVList, i);
82239}
82240
82241/*
82242** Given a wildcard parameter name, return the index of the variable
82243** with that name. If there is no variable with the given name,
82244** return 0.
82245*/
82246SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
82247 if( p==0 || zName==0 ) return 0;
82248 return sqlite3VListNameToNum(p->pVList, zName, nName);
82249}
82250SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
82251 return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
82252}
82253
82254/*
82255** Transfer all bindings from the first statement over to the second.
82256*/
82257SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
82258 Vdbe *pFrom = (Vdbe*)pFromStmt;
82259 Vdbe *pTo = (Vdbe*)pToStmt;
82260 int i;
82261 assert( pTo->db==pFrom->db );
82262 assert( pTo->nVar==pFrom->nVar );
82263 sqlite3_mutex_enter(pTo->db->mutex);
82264 for(i=0; i<pFrom->nVar; i++){
82265 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
82266 }
82267 sqlite3_mutex_leave(pTo->db->mutex);
82268 return SQLITE_OK;
82269}
82270
82271#ifndef SQLITE_OMIT_DEPRECATED
82272/*
82273** Deprecated external interface. Internal/core SQLite code
82274** should call sqlite3TransferBindings.
82275**
82276** It is misuse to call this routine with statements from different
82277** database connections. But as this is a deprecated interface, we
82278** will not bother to check for that condition.
82279**
82280** If the two statements contain a different number of bindings, then
82281** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise
82282** SQLITE_OK is returned.
82283*/
82284SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
82285 Vdbe *pFrom = (Vdbe*)pFromStmt;
82286 Vdbe *pTo = (Vdbe*)pToStmt;
82287 if( pFrom->nVar!=pTo->nVar ){
82288 return SQLITE_ERROR;
82289 }
82290 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 );
82291 if( pTo->expmask ){
82292 pTo->expired = 1;
82293 }
82294 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 );
82295 if( pFrom->expmask ){
82296 pFrom->expired = 1;
82297 }
82298 return sqlite3TransferBindings(pFromStmt, pToStmt);
82299}
82300#endif
82301
82302/*
82303** Return the sqlite3* database handle to which the prepared statement given
82304** in the argument belongs. This is the same database handle that was
82305** the first argument to the sqlite3_prepare() that was used to create
82306** the statement in the first place.
82307*/
82308SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
82309 return pStmt ? ((Vdbe*)pStmt)->db : 0;
82310}
82311
82312/*
82313** Return true if the prepared statement is guaranteed to not modify the
82314** database.
82315*/
82316SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
82317 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
82318}
82319
82320/*
82321** Return true if the prepared statement is in need of being reset.
82322*/
82323SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
82324 Vdbe *v = (Vdbe*)pStmt;
82325 return v!=0 && v->magic==VDBE_MAGIC_RUN && v->pc>=0;
82326}
82327
82328/*
82329** Return a pointer to the next prepared statement after pStmt associated
82330** with database connection pDb. If pStmt is NULL, return the first
82331** prepared statement for the database connection. Return NULL if there
82332** are no more.
82333*/
82334SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
82335 sqlite3_stmt *pNext;
82336#ifdef SQLITE_ENABLE_API_ARMOR
82337 if( !sqlite3SafetyCheckOk(pDb) ){
82338 (void)SQLITE_MISUSE_BKPT;
82339 return 0;
82340 }
82341#endif
82342 sqlite3_mutex_enter(pDb->mutex);
82343 if( pStmt==0 ){
82344 pNext = (sqlite3_stmt*)pDb->pVdbe;
82345 }else{
82346 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
82347 }
82348 sqlite3_mutex_leave(pDb->mutex);
82349 return pNext;
82350}
82351
82352/*
82353** Return the value of a status counter for a prepared statement
82354*/
82355SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
82356 Vdbe *pVdbe = (Vdbe*)pStmt;
82357 u32 v;
82358#ifdef SQLITE_ENABLE_API_ARMOR
82359 if( !pStmt
82360 || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter)))
82361 ){
82362 (void)SQLITE_MISUSE_BKPT;
82363 return 0;
82364 }
82365#endif
82366 if( op==SQLITE_STMTSTATUS_MEMUSED ){
82367 sqlite3 *db = pVdbe->db;
82368 sqlite3_mutex_enter(db->mutex);
82369 v = 0;
82370 db->pnBytesFreed = (int*)&v;
82371 sqlite3VdbeClearObject(db, pVdbe);
82372 sqlite3DbFree(db, pVdbe);
82373 db->pnBytesFreed = 0;
82374 sqlite3_mutex_leave(db->mutex);
82375 }else{
82376 v = pVdbe->aCounter[op];
82377 if( resetFlag ) pVdbe->aCounter[op] = 0;
82378 }
82379 return (int)v;
82380}
82381
82382/*
82383** Return the SQL associated with a prepared statement
82384*/
82385SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt){
82386 Vdbe *p = (Vdbe *)pStmt;
82387 return p ? p->zSql : 0;
82388}
82389
82390/*
82391** Return the SQL associated with a prepared statement with
82392** bound parameters expanded. Space to hold the returned string is
82393** obtained from sqlite3_malloc(). The caller is responsible for
82394** freeing the returned string by passing it to sqlite3_free().
82395**
82396** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of
82397** expanded bound parameters.
82398*/
82399SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){
82400#ifdef SQLITE_OMIT_TRACE
82401 return 0;
82402#else
82403 char *z = 0;
82404 const char *zSql = sqlite3_sql(pStmt);
82405 if( zSql ){
82406 Vdbe *p = (Vdbe *)pStmt;
82407 sqlite3_mutex_enter(p->db->mutex);
82408 z = sqlite3VdbeExpandSql(p, zSql);
82409 sqlite3_mutex_leave(p->db->mutex);
82410 }
82411 return z;
82412#endif
82413}
82414
82415#ifdef SQLITE_ENABLE_NORMALIZE
82416/*
82417** Return the normalized SQL associated with a prepared statement.
82418*/
82419SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt){
82420 Vdbe *p = (Vdbe *)pStmt;
82421 return p ? p->zNormSql : 0;
82422}
82423#endif /* SQLITE_ENABLE_NORMALIZE */
82424
82425#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
82426/*
82427** Allocate and populate an UnpackedRecord structure based on the serialized
82428** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
82429** if successful, or a NULL pointer if an OOM error is encountered.
82430*/
82431static UnpackedRecord *vdbeUnpackRecord(
82432 KeyInfo *pKeyInfo,
82433 int nKey,
82434 const void *pKey
82435){
82436 UnpackedRecord *pRet; /* Return value */
82437
82438 pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
82439 if( pRet ){
82440 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1));
82441 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet);
82442 }
82443 return pRet;
82444}
82445
82446/*
82447** This function is called from within a pre-update callback to retrieve
82448** a field of the row currently being updated or deleted.
82449*/
82450SQLITE_API int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
82451 PreUpdate *p = db->pPreUpdate;
82452 Mem *pMem;
82453 int rc = SQLITE_OK;
82454
82455 /* Test that this call is being made from within an SQLITE_DELETE or
82456 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
82457 if( !p || p->op==SQLITE_INSERT ){
82458 rc = SQLITE_MISUSE_BKPT;
82459 goto preupdate_old_out;
82460 }
82461 if( p->pPk ){
82462 iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
82463 }
82464 if( iIdx>=p->pCsr->nField || iIdx<0 ){
82465 rc = SQLITE_RANGE;
82466 goto preupdate_old_out;
82467 }
82468
82469 /* If the old.* record has not yet been loaded into memory, do so now. */
82470 if( p->pUnpacked==0 ){
82471 u32 nRec;
82472 u8 *aRec;
82473
82474 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
82475 aRec = sqlite3DbMallocRaw(db, nRec);
82476 if( !aRec ) goto preupdate_old_out;
82477 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
82478 if( rc==SQLITE_OK ){
82479 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
82480 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
82481 }
82482 if( rc!=SQLITE_OK ){
82483 sqlite3DbFree(db, aRec);
82484 goto preupdate_old_out;
82485 }
82486 p->aRecord = aRec;
82487 }
82488
82489 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
82490 if( iIdx==p->pTab->iPKey ){
82491 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
82492 }else if( iIdx>=p->pUnpacked->nField ){
82493 *ppValue = (sqlite3_value *)columnNullValue();
82494 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
82495 if( pMem->flags & MEM_Int ){
82496 sqlite3VdbeMemRealify(pMem);
82497 }
82498 }
82499
82500 preupdate_old_out:
82501 sqlite3Error(db, rc);
82502 return sqlite3ApiExit(db, rc);
82503}
82504#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
82505
82506#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
82507/*
82508** This function is called from within a pre-update callback to retrieve
82509** the number of columns in the row being updated, deleted or inserted.
82510*/
82511SQLITE_API int sqlite3_preupdate_count(sqlite3 *db){
82512 PreUpdate *p = db->pPreUpdate;
82513 return (p ? p->keyinfo.nKeyField : 0);
82514}
82515#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
82516
82517#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
82518/*
82519** This function is designed to be called from within a pre-update callback
82520** only. It returns zero if the change that caused the callback was made
82521** immediately by a user SQL statement. Or, if the change was made by a
82522** trigger program, it returns the number of trigger programs currently
82523** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
82524** top-level trigger etc.).
82525**
82526** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL
82527** or SET DEFAULT action is considered a trigger.
82528*/
82529SQLITE_API int sqlite3_preupdate_depth(sqlite3 *db){
82530 PreUpdate *p = db->pPreUpdate;
82531 return (p ? p->v->nFrame : 0);
82532}
82533#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
82534
82535#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
82536/*
82537** This function is called from within a pre-update callback to retrieve
82538** a field of the row currently being updated or inserted.
82539*/
82540SQLITE_API int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
82541 PreUpdate *p = db->pPreUpdate;
82542 int rc = SQLITE_OK;
82543 Mem *pMem;
82544
82545 if( !p || p->op==SQLITE_DELETE ){
82546 rc = SQLITE_MISUSE_BKPT;
82547 goto preupdate_new_out;
82548 }
82549 if( p->pPk && p->op!=SQLITE_UPDATE ){
82550 iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
82551 }
82552 if( iIdx>=p->pCsr->nField || iIdx<0 ){
82553 rc = SQLITE_RANGE;
82554 goto preupdate_new_out;
82555 }
82556
82557 if( p->op==SQLITE_INSERT ){
82558 /* For an INSERT, memory cell p->iNewReg contains the serialized record
82559 ** that is being inserted. Deserialize it. */
82560 UnpackedRecord *pUnpack = p->pNewUnpacked;
82561 if( !pUnpack ){
82562 Mem *pData = &p->v->aMem[p->iNewReg];
82563 rc = ExpandBlob(pData);
82564 if( rc!=SQLITE_OK ) goto preupdate_new_out;
82565 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
82566 if( !pUnpack ){
82567 rc = SQLITE_NOMEM;
82568 goto preupdate_new_out;
82569 }
82570 p->pNewUnpacked = pUnpack;
82571 }
82572 pMem = &pUnpack->aMem[iIdx];
82573 if( iIdx==p->pTab->iPKey ){
82574 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
82575 }else if( iIdx>=pUnpack->nField ){
82576 pMem = (sqlite3_value *)columnNullValue();
82577 }
82578 }else{
82579 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
82580 ** value. Make a copy of the cell contents and return a pointer to it.
82581 ** It is not safe to return a pointer to the memory cell itself as the
82582 ** caller may modify the value text encoding.
82583 */
82584 assert( p->op==SQLITE_UPDATE );
82585 if( !p->aNew ){
82586 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
82587 if( !p->aNew ){
82588 rc = SQLITE_NOMEM;
82589 goto preupdate_new_out;
82590 }
82591 }
82592 assert( iIdx>=0 && iIdx<p->pCsr->nField );
82593 pMem = &p->aNew[iIdx];
82594 if( pMem->flags==0 ){
82595 if( iIdx==p->pTab->iPKey ){
82596 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
82597 }else{
82598 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
82599 if( rc!=SQLITE_OK ) goto preupdate_new_out;
82600 }
82601 }
82602 }
82603 *ppValue = pMem;
82604
82605 preupdate_new_out:
82606 sqlite3Error(db, rc);
82607 return sqlite3ApiExit(db, rc);
82608}
82609#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
82610
82611#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
82612/*
82613** Return status data for a single loop within query pStmt.
82614*/
82615SQLITE_API int sqlite3_stmt_scanstatus(
82616 sqlite3_stmt *pStmt, /* Prepared statement being queried */
82617 int idx, /* Index of loop to report on */
82618 int iScanStatusOp, /* Which metric to return */
82619 void *pOut /* OUT: Write the answer here */
82620){
82621 Vdbe *p = (Vdbe*)pStmt;
82622 ScanStatus *pScan;
82623 if( idx<0 || idx>=p->nScan ) return 1;
82624 pScan = &p->aScan[idx];
82625 switch( iScanStatusOp ){
82626 case SQLITE_SCANSTAT_NLOOP: {
82627 *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop];
82628 break;
82629 }
82630 case SQLITE_SCANSTAT_NVISIT: {
82631 *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit];
82632 break;
82633 }
82634 case SQLITE_SCANSTAT_EST: {
82635 double r = 1.0;
82636 LogEst x = pScan->nEst;
82637 while( x<100 ){
82638 x += 10;
82639 r *= 0.5;
82640 }
82641 *(double*)pOut = r*sqlite3LogEstToInt(x);
82642 break;
82643 }
82644 case SQLITE_SCANSTAT_NAME: {
82645 *(const char**)pOut = pScan->zName;
82646 break;
82647 }
82648 case SQLITE_SCANSTAT_EXPLAIN: {
82649 if( pScan->addrExplain ){
82650 *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z;
82651 }else{
82652 *(const char**)pOut = 0;
82653 }
82654 break;
82655 }
82656 case SQLITE_SCANSTAT_SELECTID: {
82657 if( pScan->addrExplain ){
82658 *(int*)pOut = p->aOp[ pScan->addrExplain ].p1;
82659 }else{
82660 *(int*)pOut = -1;
82661 }
82662 break;
82663 }
82664 default: {
82665 return 1;
82666 }
82667 }
82668 return 0;
82669}
82670
82671/*
82672** Zero all counters associated with the sqlite3_stmt_scanstatus() data.
82673*/
82674SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){
82675 Vdbe *p = (Vdbe*)pStmt;
82676 memset(p->anExec, 0, p->nOp * sizeof(i64));
82677}
82678#endif /* SQLITE_ENABLE_STMT_SCANSTATUS */
82679
82680/************** End of vdbeapi.c *********************************************/
82681/************** Begin file vdbetrace.c ***************************************/
82682/*
82683** 2009 November 25
82684**
82685** The author disclaims copyright to this source code. In place of
82686** a legal notice, here is a blessing:
82687**
82688** May you do good and not evil.
82689** May you find forgiveness for yourself and forgive others.
82690** May you share freely, never taking more than you give.
82691**
82692*************************************************************************
82693**
82694** This file contains code used to insert the values of host parameters
82695** (aka "wildcards") into the SQL text output by sqlite3_trace().
82696**
82697** The Vdbe parse-tree explainer is also found here.
82698*/
82699/* #include "sqliteInt.h" */
82700/* #include "vdbeInt.h" */
82701
82702#ifndef SQLITE_OMIT_TRACE
82703
82704/*
82705** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
82706** bytes in this text up to but excluding the first character in
82707** a host parameter. If the text contains no host parameters, return
82708** the total number of bytes in the text.
82709*/
82710static int findNextHostParameter(const char *zSql, int *pnToken){
82711 int tokenType;
82712 int nTotal = 0;
82713 int n;
82714
82715 *pnToken = 0;
82716 while( zSql[0] ){
82717 n = sqlite3GetToken((u8*)zSql, &tokenType);
82718 assert( n>0 && tokenType!=TK_ILLEGAL );
82719 if( tokenType==TK_VARIABLE ){
82720 *pnToken = n;
82721 break;
82722 }
82723 nTotal += n;
82724 zSql += n;
82725 }
82726 return nTotal;
82727}
82728
82729/*
82730** This function returns a pointer to a nul-terminated string in memory
82731** obtained from sqlite3DbMalloc(). If sqlite3.nVdbeExec is 1, then the
82732** string contains a copy of zRawSql but with host parameters expanded to
82733** their current bindings. Or, if sqlite3.nVdbeExec is greater than 1,
82734** then the returned string holds a copy of zRawSql with "-- " prepended
82735** to each line of text.
82736**
82737** If the SQLITE_TRACE_SIZE_LIMIT macro is defined to an integer, then
82738** then long strings and blobs are truncated to that many bytes. This
82739** can be used to prevent unreasonably large trace strings when dealing
82740** with large (multi-megabyte) strings and blobs.
82741**
82742** The calling function is responsible for making sure the memory returned
82743** is eventually freed.
82744**
82745** ALGORITHM: Scan the input string looking for host parameters in any of
82746** these forms: ?, ?N, $A, @A, :A. Take care to avoid text within
82747** string literals, quoted identifier names, and comments. For text forms,
82748** the host parameter index is found by scanning the prepared
82749** statement for the corresponding OP_Variable opcode. Once the host
82750** parameter index is known, locate the value in p->aVar[]. Then render
82751** the value as a literal in place of the host parameter name.
82752*/
82753SQLITE_PRIVATE char *sqlite3VdbeExpandSql(
82754 Vdbe *p, /* The prepared statement being evaluated */
82755 const char *zRawSql /* Raw text of the SQL statement */
82756){
82757 sqlite3 *db; /* The database connection */
82758 int idx = 0; /* Index of a host parameter */
82759 int nextIndex = 1; /* Index of next ? host parameter */
82760 int n; /* Length of a token prefix */
82761 int nToken; /* Length of the parameter token */
82762 int i; /* Loop counter */
82763 Mem *pVar; /* Value of a host parameter */
82764 StrAccum out; /* Accumulate the output here */
82765#ifndef SQLITE_OMIT_UTF16
82766 Mem utf8; /* Used to convert UTF16 into UTF8 for display */
82767#endif
82768 char zBase[100]; /* Initial working space */
82769
82770 db = p->db;
82771 sqlite3StrAccumInit(&out, 0, zBase, sizeof(zBase),
82772 db->aLimit[SQLITE_LIMIT_LENGTH]);
82773 if( db->nVdbeExec>1 ){
82774 while( *zRawSql ){
82775 const char *zStart = zRawSql;
82776 while( *(zRawSql++)!='\n' && *zRawSql );
82777 sqlite3_str_append(&out, "-- ", 3);
82778 assert( (zRawSql - zStart) > 0 );
82779 sqlite3_str_append(&out, zStart, (int)(zRawSql-zStart));
82780 }
82781 }else if( p->nVar==0 ){
82782 sqlite3_str_append(&out, zRawSql, sqlite3Strlen30(zRawSql));
82783 }else{
82784 while( zRawSql[0] ){
82785 n = findNextHostParameter(zRawSql, &nToken);
82786 assert( n>0 );
82787 sqlite3_str_append(&out, zRawSql, n);
82788 zRawSql += n;
82789 assert( zRawSql[0] || nToken==0 );
82790 if( nToken==0 ) break;
82791 if( zRawSql[0]=='?' ){
82792 if( nToken>1 ){
82793 assert( sqlite3Isdigit(zRawSql[1]) );
82794 sqlite3GetInt32(&zRawSql[1], &idx);
82795 }else{
82796 idx = nextIndex;
82797 }
82798 }else{
82799 assert( zRawSql[0]==':' || zRawSql[0]=='$' ||
82800 zRawSql[0]=='@' || zRawSql[0]=='#' );
82801 testcase( zRawSql[0]==':' );
82802 testcase( zRawSql[0]=='$' );
82803 testcase( zRawSql[0]=='@' );
82804 testcase( zRawSql[0]=='#' );
82805 idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
82806 assert( idx>0 );
82807 }
82808 zRawSql += nToken;
82809 nextIndex = idx + 1;
82810 assert( idx>0 && idx<=p->nVar );
82811 pVar = &p->aVar[idx-1];
82812 if( pVar->flags & MEM_Null ){
82813 sqlite3_str_append(&out, "NULL", 4);
82814 }else if( pVar->flags & MEM_Int ){
82815 sqlite3_str_appendf(&out, "%lld", pVar->u.i);
82816 }else if( pVar->flags & MEM_Real ){
82817 sqlite3_str_appendf(&out, "%!.15g", pVar->u.r);
82818 }else if( pVar->flags & MEM_Str ){
82819 int nOut; /* Number of bytes of the string text to include in output */
82820#ifndef SQLITE_OMIT_UTF16
82821 u8 enc = ENC(db);
82822 if( enc!=SQLITE_UTF8 ){
82823 memset(&utf8, 0, sizeof(utf8));
82824 utf8.db = db;
82825 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
82826 if( SQLITE_NOMEM==sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8) ){
82827 out.accError = SQLITE_NOMEM;
82828 out.nAlloc = 0;
82829 }
82830 pVar = &utf8;
82831 }
82832#endif
82833 nOut = pVar->n;
82834#ifdef SQLITE_TRACE_SIZE_LIMIT
82835 if( nOut>SQLITE_TRACE_SIZE_LIMIT ){
82836 nOut = SQLITE_TRACE_SIZE_LIMIT;
82837 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
82838 }
82839#endif
82840 sqlite3_str_appendf(&out, "'%.*q'", nOut, pVar->z);
82841#ifdef SQLITE_TRACE_SIZE_LIMIT
82842 if( nOut<pVar->n ){
82843 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
82844 }
82845#endif
82846#ifndef SQLITE_OMIT_UTF16
82847 if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8);
82848#endif
82849 }else if( pVar->flags & MEM_Zero ){
82850 sqlite3_str_appendf(&out, "zeroblob(%d)", pVar->u.nZero);
82851 }else{
82852 int nOut; /* Number of bytes of the blob to include in output */
82853 assert( pVar->flags & MEM_Blob );
82854 sqlite3_str_append(&out, "x'", 2);
82855 nOut = pVar->n;
82856#ifdef SQLITE_TRACE_SIZE_LIMIT
82857 if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
82858#endif
82859 for(i=0; i<nOut; i++){
82860 sqlite3_str_appendf(&out, "%02x", pVar->z[i]&0xff);
82861 }
82862 sqlite3_str_append(&out, "'", 1);
82863#ifdef SQLITE_TRACE_SIZE_LIMIT
82864 if( nOut<pVar->n ){
82865 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
82866 }
82867#endif
82868 }
82869 }
82870 }
82871 if( out.accError ) sqlite3_str_reset(&out);
82872 return sqlite3StrAccumFinish(&out);
82873}
82874
82875#endif /* #ifndef SQLITE_OMIT_TRACE */
82876
82877/************** End of vdbetrace.c *******************************************/
82878/************** Begin file vdbe.c ********************************************/
82879/*
82880** 2001 September 15
82881**
82882** The author disclaims copyright to this source code. In place of
82883** a legal notice, here is a blessing:
82884**
82885** May you do good and not evil.
82886** May you find forgiveness for yourself and forgive others.
82887** May you share freely, never taking more than you give.
82888**
82889*************************************************************************
82890** The code in this file implements the function that runs the
82891** bytecode of a prepared statement.
82892**
82893** Various scripts scan this source file in order to generate HTML
82894** documentation, headers files, or other derived files. The formatting
82895** of the code in this file is, therefore, important. See other comments
82896** in this file for details. If in doubt, do not deviate from existing
82897** commenting and indentation practices when changing or adding code.
82898*/
82899/* #include "sqliteInt.h" */
82900/* #include "vdbeInt.h" */
82901
82902/*
82903** Invoke this macro on memory cells just prior to changing the
82904** value of the cell. This macro verifies that shallow copies are
82905** not misused. A shallow copy of a string or blob just copies a
82906** pointer to the string or blob, not the content. If the original
82907** is changed while the copy is still in use, the string or blob might
82908** be changed out from under the copy. This macro verifies that nothing
82909** like that ever happens.
82910*/
82911#ifdef SQLITE_DEBUG
82912# define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
82913#else
82914# define memAboutToChange(P,M)
82915#endif
82916
82917/*
82918** The following global variable is incremented every time a cursor
82919** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
82920** procedures use this information to make sure that indices are
82921** working correctly. This variable has no function other than to
82922** help verify the correct operation of the library.
82923*/
82924#ifdef SQLITE_TEST
82925SQLITE_API int sqlite3_search_count = 0;
82926#endif
82927
82928/*
82929** When this global variable is positive, it gets decremented once before
82930** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted
82931** field of the sqlite3 structure is set in order to simulate an interrupt.
82932**
82933** This facility is used for testing purposes only. It does not function
82934** in an ordinary build.
82935*/
82936#ifdef SQLITE_TEST
82937SQLITE_API int sqlite3_interrupt_count = 0;
82938#endif
82939
82940/*
82941** The next global variable is incremented each type the OP_Sort opcode
82942** is executed. The test procedures use this information to make sure that
82943** sorting is occurring or not occurring at appropriate times. This variable
82944** has no function other than to help verify the correct operation of the
82945** library.
82946*/
82947#ifdef SQLITE_TEST
82948SQLITE_API int sqlite3_sort_count = 0;
82949#endif
82950
82951/*
82952** The next global variable records the size of the largest MEM_Blob
82953** or MEM_Str that has been used by a VDBE opcode. The test procedures
82954** use this information to make sure that the zero-blob functionality
82955** is working correctly. This variable has no function other than to
82956** help verify the correct operation of the library.
82957*/
82958#ifdef SQLITE_TEST
82959SQLITE_API int sqlite3_max_blobsize = 0;
82960static void updateMaxBlobsize(Mem *p){
82961 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
82962 sqlite3_max_blobsize = p->n;
82963 }
82964}
82965#endif
82966
82967/*
82968** This macro evaluates to true if either the update hook or the preupdate
82969** hook are enabled for database connect DB.
82970*/
82971#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
82972# define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
82973#else
82974# define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
82975#endif
82976
82977/*
82978** The next global variable is incremented each time the OP_Found opcode
82979** is executed. This is used to test whether or not the foreign key
82980** operation implemented using OP_FkIsZero is working. This variable
82981** has no function other than to help verify the correct operation of the
82982** library.
82983*/
82984#ifdef SQLITE_TEST
82985SQLITE_API int sqlite3_found_count = 0;
82986#endif
82987
82988/*
82989** Test a register to see if it exceeds the current maximum blob size.
82990** If it does, record the new maximum blob size.
82991*/
82992#if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE)
82993# define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
82994#else
82995# define UPDATE_MAX_BLOBSIZE(P)
82996#endif
82997
82998/*
82999** Invoke the VDBE coverage callback, if that callback is defined. This
83000** feature is used for test suite validation only and does not appear an
83001** production builds.
83002**
83003** M is an integer between 2 and 4. 2 indicates a ordinary two-way
83004** branch (I=0 means fall through and I=1 means taken). 3 indicates
83005** a 3-way branch where the third way is when one of the operands is
83006** NULL. 4 indicates the OP_Jump instruction which has three destinations
83007** depending on whether the first operand is less than, equal to, or greater
83008** than the second.
83009**
83010** iSrcLine is the source code line (from the __LINE__ macro) that
83011** generated the VDBE instruction combined with flag bits. The source
83012** code line number is in the lower 24 bits of iSrcLine and the upper
83013** 8 bytes are flags. The lower three bits of the flags indicate
83014** values for I that should never occur. For example, if the branch is
83015** always taken, the flags should be 0x05 since the fall-through and
83016** alternate branch are never taken. If a branch is never taken then
83017** flags should be 0x06 since only the fall-through approach is allowed.
83018**
83019** Bit 0x04 of the flags indicates an OP_Jump opcode that is only
83020** interested in equal or not-equal. In other words, I==0 and I==2
83021** should be treated the same.
83022**
83023** Since only a line number is retained, not the filename, this macro
83024** only works for amalgamation builds. But that is ok, since these macros
83025** should be no-ops except for special builds used to measure test coverage.
83026*/
83027#if !defined(SQLITE_VDBE_COVERAGE)
83028# define VdbeBranchTaken(I,M)
83029#else
83030# define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
83031 static void vdbeTakeBranch(u32 iSrcLine, u8 I, u8 M){
83032 u8 mNever;
83033 assert( I<=2 ); /* 0: fall through, 1: taken, 2: alternate taken */
83034 assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
83035 assert( I<M ); /* I can only be 2 if M is 3 or 4 */
83036 /* Transform I from a integer [0,1,2] into a bitmask of [1,2,4] */
83037 I = 1<<I;
83038 /* The upper 8 bits of iSrcLine are flags. The lower three bits of
83039 ** the flags indicate directions that the branch can never go. If
83040 ** a branch really does go in one of those directions, assert right
83041 ** away. */
83042 mNever = iSrcLine >> 24;
83043 assert( (I & mNever)==0 );
83044 if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/
83045 I |= mNever;
83046 if( M==2 ) I |= 0x04;
83047 if( M==4 ){
83048 I |= 0x08;
83049 if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/
83050 }
83051 sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
83052 iSrcLine&0xffffff, I, M);
83053 }
83054#endif
83055
83056/*
83057** Convert the given register into a string if it isn't one
83058** already. Return non-zero if a malloc() fails.
83059*/
83060#define Stringify(P, enc) \
83061 if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc,0)) \
83062 { goto no_mem; }
83063
83064/*
83065** An ephemeral string value (signified by the MEM_Ephem flag) contains
83066** a pointer to a dynamically allocated string where some other entity
83067** is responsible for deallocating that string. Because the register
83068** does not control the string, it might be deleted without the register
83069** knowing it.
83070**
83071** This routine converts an ephemeral string into a dynamically allocated
83072** string that the register itself controls. In other words, it
83073** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
83074*/
83075#define Deephemeralize(P) \
83076 if( ((P)->flags&MEM_Ephem)!=0 \
83077 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
83078
83079/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
83080#define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
83081
83082/*
83083** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
83084** if we run out of memory.
83085*/
83086static VdbeCursor *allocateCursor(
83087 Vdbe *p, /* The virtual machine */
83088 int iCur, /* Index of the new VdbeCursor */
83089 int nField, /* Number of fields in the table or index */
83090 int iDb, /* Database the cursor belongs to, or -1 */
83091 u8 eCurType /* Type of the new cursor */
83092){
83093 /* Find the memory cell that will be used to store the blob of memory
83094 ** required for this VdbeCursor structure. It is convenient to use a
83095 ** vdbe memory cell to manage the memory allocation required for a
83096 ** VdbeCursor structure for the following reasons:
83097 **
83098 ** * Sometimes cursor numbers are used for a couple of different
83099 ** purposes in a vdbe program. The different uses might require
83100 ** different sized allocations. Memory cells provide growable
83101 ** allocations.
83102 **
83103 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
83104 ** be freed lazily via the sqlite3_release_memory() API. This
83105 ** minimizes the number of malloc calls made by the system.
83106 **
83107 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from
83108 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
83109 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
83110 */
83111 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
83112
83113 int nByte;
83114 VdbeCursor *pCx = 0;
83115 nByte =
83116 ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
83117 (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0);
83118
83119 assert( iCur>=0 && iCur<p->nCursor );
83120 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
83121 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
83122 p->apCsr[iCur] = 0;
83123 }
83124 if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){
83125 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
83126 memset(pCx, 0, offsetof(VdbeCursor,pAltCursor));
83127 pCx->eCurType = eCurType;
83128 pCx->iDb = iDb;
83129 pCx->nField = nField;
83130 pCx->aOffset = &pCx->aType[nField];
83131 if( eCurType==CURTYPE_BTREE ){
83132 pCx->uc.pCursor = (BtCursor*)
83133 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
83134 sqlite3BtreeCursorZero(pCx->uc.pCursor);
83135 }
83136 }
83137 return pCx;
83138}
83139
83140/*
83141** Try to convert a value into a numeric representation if we can
83142** do so without loss of information. In other words, if the string
83143** looks like a number, convert it into a number. If it does not
83144** look like a number, leave it alone.
83145**
83146** If the bTryForInt flag is true, then extra effort is made to give
83147** an integer representation. Strings that look like floating point
83148** values but which have no fractional component (example: '48.00')
83149** will have a MEM_Int representation when bTryForInt is true.
83150**
83151** If bTryForInt is false, then if the input string contains a decimal
83152** point or exponential notation, the result is only MEM_Real, even
83153** if there is an exact integer representation of the quantity.
83154*/
83155static void applyNumericAffinity(Mem *pRec, int bTryForInt){
83156 double rValue;
83157 i64 iValue;
83158 u8 enc = pRec->enc;
83159 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str );
83160 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
83161 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
83162 pRec->u.i = iValue;
83163 pRec->flags |= MEM_Int;
83164 }else{
83165 pRec->u.r = rValue;
83166 pRec->flags |= MEM_Real;
83167 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
83168 }
83169 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
83170 ** string representation after computing a numeric equivalent, because the
83171 ** string representation might not be the canonical representation for the
83172 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
83173 pRec->flags &= ~MEM_Str;
83174}
83175
83176/*
83177** Processing is determine by the affinity parameter:
83178**
83179** SQLITE_AFF_INTEGER:
83180** SQLITE_AFF_REAL:
83181** SQLITE_AFF_NUMERIC:
83182** Try to convert pRec to an integer representation or a
83183** floating-point representation if an integer representation
83184** is not possible. Note that the integer representation is
83185** always preferred, even if the affinity is REAL, because
83186** an integer representation is more space efficient on disk.
83187**
83188** SQLITE_AFF_TEXT:
83189** Convert pRec to a text representation.
83190**
83191** SQLITE_AFF_BLOB:
83192** No-op. pRec is unchanged.
83193*/
83194static void applyAffinity(
83195 Mem *pRec, /* The value to apply affinity to */
83196 char affinity, /* The affinity to be applied */
83197 u8 enc /* Use this text encoding */
83198){
83199 if( affinity>=SQLITE_AFF_NUMERIC ){
83200 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
83201 || affinity==SQLITE_AFF_NUMERIC );
83202 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
83203 if( (pRec->flags & MEM_Real)==0 ){
83204 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
83205 }else{
83206 sqlite3VdbeIntegerAffinity(pRec);
83207 }
83208 }
83209 }else if( affinity==SQLITE_AFF_TEXT ){
83210 /* Only attempt the conversion to TEXT if there is an integer or real
83211 ** representation (blob and NULL do not get converted) but no string
83212 ** representation. It would be harmless to repeat the conversion if
83213 ** there is already a string rep, but it is pointless to waste those
83214 ** CPU cycles. */
83215 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
83216 if( (pRec->flags&(MEM_Real|MEM_Int)) ){
83217 sqlite3VdbeMemStringify(pRec, enc, 1);
83218 }
83219 }
83220 pRec->flags &= ~(MEM_Real|MEM_Int);
83221 }
83222}
83223
83224/*
83225** Try to convert the type of a function argument or a result column
83226** into a numeric representation. Use either INTEGER or REAL whichever
83227** is appropriate. But only do the conversion if it is possible without
83228** loss of information and return the revised type of the argument.
83229*/
83230SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){
83231 int eType = sqlite3_value_type(pVal);
83232 if( eType==SQLITE_TEXT ){
83233 Mem *pMem = (Mem*)pVal;
83234 applyNumericAffinity(pMem, 0);
83235 eType = sqlite3_value_type(pVal);
83236 }
83237 return eType;
83238}
83239
83240/*
83241** Exported version of applyAffinity(). This one works on sqlite3_value*,
83242** not the internal Mem* type.
83243*/
83244SQLITE_PRIVATE void sqlite3ValueApplyAffinity(
83245 sqlite3_value *pVal,
83246 u8 affinity,
83247 u8 enc
83248){
83249 applyAffinity((Mem *)pVal, affinity, enc);
83250}
83251
83252/*
83253** pMem currently only holds a string type (or maybe a BLOB that we can
83254** interpret as a string if we want to). Compute its corresponding
83255** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
83256** accordingly.
83257*/
83258static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
83259 assert( (pMem->flags & (MEM_Int|MEM_Real))==0 );
83260 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
83261 if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){
83262 return 0;
83263 }
83264 if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==0 ){
83265 return MEM_Int;
83266 }
83267 return MEM_Real;
83268}
83269
83270/*
83271** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
83272** none.
83273**
83274** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
83275** But it does set pMem->u.r and pMem->u.i appropriately.
83276*/
83277static u16 numericType(Mem *pMem){
83278 if( pMem->flags & (MEM_Int|MEM_Real) ){
83279 return pMem->flags & (MEM_Int|MEM_Real);
83280 }
83281 if( pMem->flags & (MEM_Str|MEM_Blob) ){
83282 return computeNumericType(pMem);
83283 }
83284 return 0;
83285}
83286
83287#ifdef SQLITE_DEBUG
83288/*
83289** Write a nice string representation of the contents of cell pMem
83290** into buffer zBuf, length nBuf.
83291*/
83292SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
83293 char *zCsr = zBuf;
83294 int f = pMem->flags;
83295
83296 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
83297
83298 if( f&MEM_Blob ){
83299 int i;
83300 char c;
83301 if( f & MEM_Dyn ){
83302 c = 'z';
83303 assert( (f & (MEM_Static|MEM_Ephem))==0 );
83304 }else if( f & MEM_Static ){
83305 c = 't';
83306 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
83307 }else if( f & MEM_Ephem ){
83308 c = 'e';
83309 assert( (f & (MEM_Static|MEM_Dyn))==0 );
83310 }else{
83311 c = 's';
83312 }
83313 *(zCsr++) = c;
83314 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
83315 zCsr += sqlite3Strlen30(zCsr);
83316 for(i=0; i<16 && i<pMem->n; i++){
83317 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
83318 zCsr += sqlite3Strlen30(zCsr);
83319 }
83320 for(i=0; i<16 && i<pMem->n; i++){
83321 char z = pMem->z[i];
83322 if( z<32 || z>126 ) *zCsr++ = '.';
83323 else *zCsr++ = z;
83324 }
83325 *(zCsr++) = ']';
83326 if( f & MEM_Zero ){
83327 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
83328 zCsr += sqlite3Strlen30(zCsr);
83329 }
83330 *zCsr = '\0';
83331 }else if( f & MEM_Str ){
83332 int j, k;
83333 zBuf[0] = ' ';
83334 if( f & MEM_Dyn ){
83335 zBuf[1] = 'z';
83336 assert( (f & (MEM_Static|MEM_Ephem))==0 );
83337 }else if( f & MEM_Static ){
83338 zBuf[1] = 't';
83339 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
83340 }else if( f & MEM_Ephem ){
83341 zBuf[1] = 'e';
83342 assert( (f & (MEM_Static|MEM_Dyn))==0 );
83343 }else{
83344 zBuf[1] = 's';
83345 }
83346 k = 2;
83347 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
83348 k += sqlite3Strlen30(&zBuf[k]);
83349 zBuf[k++] = '[';
83350 for(j=0; j<15 && j<pMem->n; j++){
83351 u8 c = pMem->z[j];
83352 if( c>=0x20 && c<0x7f ){
83353 zBuf[k++] = c;
83354 }else{
83355 zBuf[k++] = '.';
83356 }
83357 }
83358 zBuf[k++] = ']';
83359 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
83360 k += sqlite3Strlen30(&zBuf[k]);
83361 zBuf[k++] = 0;
83362 }
83363}
83364#endif
83365
83366#ifdef SQLITE_DEBUG
83367/*
83368** Print the value of a register for tracing purposes:
83369*/
83370static void memTracePrint(Mem *p){
83371 if( p->flags & MEM_Undefined ){
83372 printf(" undefined");
83373 }else if( p->flags & MEM_Null ){
83374 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
83375 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
83376 printf(" si:%lld", p->u.i);
83377 }else if( p->flags & MEM_Int ){
83378 printf(" i:%lld", p->u.i);
83379#ifndef SQLITE_OMIT_FLOATING_POINT
83380 }else if( p->flags & MEM_Real ){
83381 printf(" r:%g", p->u.r);
83382#endif
83383 }else if( sqlite3VdbeMemIsRowSet(p) ){
83384 printf(" (rowset)");
83385 }else{
83386 char zBuf[200];
83387 sqlite3VdbeMemPrettyPrint(p, zBuf);
83388 printf(" %s", zBuf);
83389 }
83390 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
83391}
83392static void registerTrace(int iReg, Mem *p){
83393 printf("REG[%d] = ", iReg);
83394 memTracePrint(p);
83395 printf("\n");
83396 sqlite3VdbeCheckMemInvariants(p);
83397}
83398#endif
83399
83400#ifdef SQLITE_DEBUG
83401# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
83402#else
83403# define REGISTER_TRACE(R,M)
83404#endif
83405
83406
83407#ifdef VDBE_PROFILE
83408
83409/*
83410** hwtime.h contains inline assembler code for implementing
83411** high-performance timing routines.
83412*/
83413/************** Include hwtime.h in the middle of vdbe.c *********************/
83414/************** Begin file hwtime.h ******************************************/
83415/*
83416** 2008 May 27
83417**
83418** The author disclaims copyright to this source code. In place of
83419** a legal notice, here is a blessing:
83420**
83421** May you do good and not evil.
83422** May you find forgiveness for yourself and forgive others.
83423** May you share freely, never taking more than you give.
83424**
83425******************************************************************************
83426**
83427** This file contains inline asm code for retrieving "high-performance"
83428** counters for x86 class CPUs.
83429*/
83430#ifndef SQLITE_HWTIME_H
83431#define SQLITE_HWTIME_H
83432
83433/*
83434** The following routine only works on pentium-class (or newer) processors.
83435** It uses the RDTSC opcode to read the cycle count value out of the
83436** processor and returns that value. This can be used for high-res
83437** profiling.
83438*/
83439#if (defined(__GNUC__) || defined(_MSC_VER)) && \
83440 (defined(i386) || defined(__i386__) || defined(_M_IX86))
83441
83442 #if defined(__GNUC__)
83443
83444 __inline__ sqlite_uint64 sqlite3Hwtime(void){
83445 unsigned int lo, hi;
83446 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
83447 return (sqlite_uint64)hi << 32 | lo;
83448 }
83449
83450 #elif defined(_MSC_VER)
83451
83452 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
83453 __asm {
83454 rdtsc
83455 ret ; return value at EDX:EAX
83456 }
83457 }
83458
83459 #endif
83460
83461#elif (defined(__GNUC__) && defined(__x86_64__))
83462
83463 __inline__ sqlite_uint64 sqlite3Hwtime(void){
83464 unsigned long val;
83465 __asm__ __volatile__ ("rdtsc" : "=A" (val));
83466 return val;
83467 }
83468
83469#elif (defined(__GNUC__) && defined(__ppc__))
83470
83471 __inline__ sqlite_uint64 sqlite3Hwtime(void){
83472 unsigned long long retval;
83473 unsigned long junk;
83474 __asm__ __volatile__ ("\n\
83475 1: mftbu %1\n\
83476 mftb %L0\n\
83477 mftbu %0\n\
83478 cmpw %0,%1\n\
83479 bne 1b"
83480 : "=r" (retval), "=r" (junk));
83481 return retval;
83482 }
83483
83484#else
83485
83486 #error Need implementation of sqlite3Hwtime() for your platform.
83487
83488 /*
83489 ** To compile without implementing sqlite3Hwtime() for your platform,
83490 ** you can remove the above #error and use the following
83491 ** stub function. You will lose timing support for many
83492 ** of the debugging and testing utilities, but it should at
83493 ** least compile and run.
83494 */
83495SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
83496
83497#endif
83498
83499#endif /* !defined(SQLITE_HWTIME_H) */
83500
83501/************** End of hwtime.h **********************************************/
83502/************** Continuing where we left off in vdbe.c ***********************/
83503
83504#endif
83505
83506#ifndef NDEBUG
83507/*
83508** This function is only called from within an assert() expression. It
83509** checks that the sqlite3.nTransaction variable is correctly set to
83510** the number of non-transaction savepoints currently in the
83511** linked list starting at sqlite3.pSavepoint.
83512**
83513** Usage:
83514**
83515** assert( checkSavepointCount(db) );
83516*/
83517static int checkSavepointCount(sqlite3 *db){
83518 int n = 0;
83519 Savepoint *p;
83520 for(p=db->pSavepoint; p; p=p->pNext) n++;
83521 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
83522 return 1;
83523}
83524#endif
83525
83526/*
83527** Return the register of pOp->p2 after first preparing it to be
83528** overwritten with an integer value.
83529*/
83530static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){
83531 sqlite3VdbeMemSetNull(pOut);
83532 pOut->flags = MEM_Int;
83533 return pOut;
83534}
83535static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
83536 Mem *pOut;
83537 assert( pOp->p2>0 );
83538 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
83539 pOut = &p->aMem[pOp->p2];
83540 memAboutToChange(p, pOut);
83541 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
83542 return out2PrereleaseWithClear(pOut);
83543 }else{
83544 pOut->flags = MEM_Int;
83545 return pOut;
83546 }
83547}
83548
83549
83550/*
83551** Execute as much of a VDBE program as we can.
83552** This is the core of sqlite3_step().
83553*/
83554SQLITE_PRIVATE int sqlite3VdbeExec(
83555 Vdbe *p /* The VDBE */
83556){
83557 Op *aOp = p->aOp; /* Copy of p->aOp */
83558 Op *pOp = aOp; /* Current operation */
83559#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
83560 Op *pOrigOp; /* Value of pOp at the top of the loop */
83561#endif
83562#ifdef SQLITE_DEBUG
83563 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
83564#endif
83565 int rc = SQLITE_OK; /* Value to return */
83566 sqlite3 *db = p->db; /* The database */
83567 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
83568 u8 encoding = ENC(db); /* The database encoding */
83569 int iCompare = 0; /* Result of last comparison */
83570 unsigned nVmStep = 0; /* Number of virtual machine steps */
83571#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
83572 unsigned nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */
83573#endif
83574 Mem *aMem = p->aMem; /* Copy of p->aMem */
83575 Mem *pIn1 = 0; /* 1st input operand */
83576 Mem *pIn2 = 0; /* 2nd input operand */
83577 Mem *pIn3 = 0; /* 3rd input operand */
83578 Mem *pOut = 0; /* Output operand */
83579#ifdef VDBE_PROFILE
83580 u64 start; /* CPU clock count at start of opcode */
83581#endif
83582 /*** INSERT STACK UNION HERE ***/
83583
83584 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
83585 sqlite3VdbeEnter(p);
83586 if( p->rc==SQLITE_NOMEM ){
83587 /* This happens if a malloc() inside a call to sqlite3_column_text() or
83588 ** sqlite3_column_text16() failed. */
83589 goto no_mem;
83590 }
83591 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
83592 assert( p->bIsReader || p->readOnly!=0 );
83593 p->iCurrentTime = 0;
83594 assert( p->explain==0 );
83595 p->pResultSet = 0;
83596 db->busyHandler.nBusy = 0;
83597 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
83598 sqlite3VdbeIOTraceSql(p);
83599#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
83600 if( db->xProgress ){
83601 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
83602 assert( 0 < db->nProgressOps );
83603 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
83604 }else{
83605 nProgressLimit = 0xffffffff;
83606 }
83607#endif
83608#ifdef SQLITE_DEBUG
83609 sqlite3BeginBenignMalloc();
83610 if( p->pc==0
83611 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
83612 ){
83613 int i;
83614 int once = 1;
83615 sqlite3VdbePrintSql(p);
83616 if( p->db->flags & SQLITE_VdbeListing ){
83617 printf("VDBE Program Listing:\n");
83618 for(i=0; i<p->nOp; i++){
83619 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
83620 }
83621 }
83622 if( p->db->flags & SQLITE_VdbeEQP ){
83623 for(i=0; i<p->nOp; i++){
83624 if( aOp[i].opcode==OP_Explain ){
83625 if( once ) printf("VDBE Query Plan:\n");
83626 printf("%s\n", aOp[i].p4.z);
83627 once = 0;
83628 }
83629 }
83630 }
83631 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
83632 }
83633 sqlite3EndBenignMalloc();
83634#endif
83635 for(pOp=&aOp[p->pc]; 1; pOp++){
83636 /* Errors are detected by individual opcodes, with an immediate
83637 ** jumps to abort_due_to_error. */
83638 assert( rc==SQLITE_OK );
83639
83640 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
83641#ifdef VDBE_PROFILE
83642 start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
83643#endif
83644 nVmStep++;
83645#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
83646 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
83647#endif
83648
83649 /* Only allow tracing if SQLITE_DEBUG is defined.
83650 */
83651#ifdef SQLITE_DEBUG
83652 if( db->flags & SQLITE_VdbeTrace ){
83653 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
83654 }
83655#endif
83656
83657
83658 /* Check to see if we need to simulate an interrupt. This only happens
83659 ** if we have a special test build.
83660 */
83661#ifdef SQLITE_TEST
83662 if( sqlite3_interrupt_count>0 ){
83663 sqlite3_interrupt_count--;
83664 if( sqlite3_interrupt_count==0 ){
83665 sqlite3_interrupt(db);
83666 }
83667 }
83668#endif
83669
83670 /* Sanity checking on other operands */
83671#ifdef SQLITE_DEBUG
83672 {
83673 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
83674 if( (opProperty & OPFLG_IN1)!=0 ){
83675 assert( pOp->p1>0 );
83676 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
83677 assert( memIsValid(&aMem[pOp->p1]) );
83678 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
83679 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
83680 }
83681 if( (opProperty & OPFLG_IN2)!=0 ){
83682 assert( pOp->p2>0 );
83683 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
83684 assert( memIsValid(&aMem[pOp->p2]) );
83685 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
83686 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
83687 }
83688 if( (opProperty & OPFLG_IN3)!=0 ){
83689 assert( pOp->p3>0 );
83690 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
83691 assert( memIsValid(&aMem[pOp->p3]) );
83692 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
83693 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
83694 }
83695 if( (opProperty & OPFLG_OUT2)!=0 ){
83696 assert( pOp->p2>0 );
83697 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
83698 memAboutToChange(p, &aMem[pOp->p2]);
83699 }
83700 if( (opProperty & OPFLG_OUT3)!=0 ){
83701 assert( pOp->p3>0 );
83702 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
83703 memAboutToChange(p, &aMem[pOp->p3]);
83704 }
83705 }
83706#endif
83707#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
83708 pOrigOp = pOp;
83709#endif
83710
83711 switch( pOp->opcode ){
83712
83713/*****************************************************************************
83714** What follows is a massive switch statement where each case implements a
83715** separate instruction in the virtual machine. If we follow the usual
83716** indentation conventions, each case should be indented by 6 spaces. But
83717** that is a lot of wasted space on the left margin. So the code within
83718** the switch statement will break with convention and be flush-left. Another
83719** big comment (similar to this one) will mark the point in the code where
83720** we transition back to normal indentation.
83721**
83722** The formatting of each case is important. The makefile for SQLite
83723** generates two C files "opcodes.h" and "opcodes.c" by scanning this
83724** file looking for lines that begin with "case OP_". The opcodes.h files
83725** will be filled with #defines that give unique integer values to each
83726** opcode and the opcodes.c file is filled with an array of strings where
83727** each string is the symbolic name for the corresponding opcode. If the
83728** case statement is followed by a comment of the form "/# same as ... #/"
83729** that comment is used to determine the particular value of the opcode.
83730**
83731** Other keywords in the comment that follows each case are used to
83732** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
83733** Keywords include: in1, in2, in3, out2, out3. See
83734** the mkopcodeh.awk script for additional information.
83735**
83736** Documentation about VDBE opcodes is generated by scanning this file
83737** for lines of that contain "Opcode:". That line and all subsequent
83738** comment lines are used in the generation of the opcode.html documentation
83739** file.
83740**
83741** SUMMARY:
83742**
83743** Formatting is important to scripts that scan this file.
83744** Do not deviate from the formatting style currently in use.
83745**
83746*****************************************************************************/
83747
83748/* Opcode: Goto * P2 * * *
83749**
83750** An unconditional jump to address P2.
83751** The next instruction executed will be
83752** the one at index P2 from the beginning of
83753** the program.
83754**
83755** The P1 parameter is not actually used by this opcode. However, it
83756** is sometimes set to 1 instead of 0 as a hint to the command-line shell
83757** that this Goto is the bottom of a loop and that the lines from P2 down
83758** to the current line should be indented for EXPLAIN output.
83759*/
83760case OP_Goto: { /* jump */
83761jump_to_p2_and_check_for_interrupt:
83762 pOp = &aOp[pOp->p2 - 1];
83763
83764 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
83765 ** OP_VNext, or OP_SorterNext) all jump here upon
83766 ** completion. Check to see if sqlite3_interrupt() has been called
83767 ** or if the progress callback needs to be invoked.
83768 **
83769 ** This code uses unstructured "goto" statements and does not look clean.
83770 ** But that is not due to sloppy coding habits. The code is written this
83771 ** way for performance, to avoid having to run the interrupt and progress
83772 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
83773 ** faster according to "valgrind --tool=cachegrind" */
83774check_for_interrupt:
83775 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
83776#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
83777 /* Call the progress callback if it is configured and the required number
83778 ** of VDBE ops have been executed (either since this invocation of
83779 ** sqlite3VdbeExec() or since last time the progress callback was called).
83780 ** If the progress callback returns non-zero, exit the virtual machine with
83781 ** a return code SQLITE_ABORT.
83782 */
83783 if( nVmStep>=nProgressLimit && db->xProgress!=0 ){
83784 assert( db->nProgressOps!=0 );
83785 nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
83786 if( db->xProgress(db->pProgressArg) ){
83787 rc = SQLITE_INTERRUPT;
83788 goto abort_due_to_error;
83789 }
83790 }
83791#endif
83792
83793 break;
83794}
83795
83796/* Opcode: Gosub P1 P2 * * *
83797**
83798** Write the current address onto register P1
83799** and then jump to address P2.
83800*/
83801case OP_Gosub: { /* jump */
83802 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
83803 pIn1 = &aMem[pOp->p1];
83804 assert( VdbeMemDynamic(pIn1)==0 );
83805 memAboutToChange(p, pIn1);
83806 pIn1->flags = MEM_Int;
83807 pIn1->u.i = (int)(pOp-aOp);
83808 REGISTER_TRACE(pOp->p1, pIn1);
83809
83810 /* Most jump operations do a goto to this spot in order to update
83811 ** the pOp pointer. */
83812jump_to_p2:
83813 pOp = &aOp[pOp->p2 - 1];
83814 break;
83815}
83816
83817/* Opcode: Return P1 * * * *
83818**
83819** Jump to the next instruction after the address in register P1. After
83820** the jump, register P1 becomes undefined.
83821*/
83822case OP_Return: { /* in1 */
83823 pIn1 = &aMem[pOp->p1];
83824 assert( pIn1->flags==MEM_Int );
83825 pOp = &aOp[pIn1->u.i];
83826 pIn1->flags = MEM_Undefined;
83827 break;
83828}
83829
83830/* Opcode: InitCoroutine P1 P2 P3 * *
83831**
83832** Set up register P1 so that it will Yield to the coroutine
83833** located at address P3.
83834**
83835** If P2!=0 then the coroutine implementation immediately follows
83836** this opcode. So jump over the coroutine implementation to
83837** address P2.
83838**
83839** See also: EndCoroutine
83840*/
83841case OP_InitCoroutine: { /* jump */
83842 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
83843 assert( pOp->p2>=0 && pOp->p2<p->nOp );
83844 assert( pOp->p3>=0 && pOp->p3<p->nOp );
83845 pOut = &aMem[pOp->p1];
83846 assert( !VdbeMemDynamic(pOut) );
83847 pOut->u.i = pOp->p3 - 1;
83848 pOut->flags = MEM_Int;
83849 if( pOp->p2 ) goto jump_to_p2;
83850 break;
83851}
83852
83853/* Opcode: EndCoroutine P1 * * * *
83854**
83855** The instruction at the address in register P1 is a Yield.
83856** Jump to the P2 parameter of that Yield.
83857** After the jump, register P1 becomes undefined.
83858**
83859** See also: InitCoroutine
83860*/
83861case OP_EndCoroutine: { /* in1 */
83862 VdbeOp *pCaller;
83863 pIn1 = &aMem[pOp->p1];
83864 assert( pIn1->flags==MEM_Int );
83865 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
83866 pCaller = &aOp[pIn1->u.i];
83867 assert( pCaller->opcode==OP_Yield );
83868 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
83869 pOp = &aOp[pCaller->p2 - 1];
83870 pIn1->flags = MEM_Undefined;
83871 break;
83872}
83873
83874/* Opcode: Yield P1 P2 * * *
83875**
83876** Swap the program counter with the value in register P1. This
83877** has the effect of yielding to a coroutine.
83878**
83879** If the coroutine that is launched by this instruction ends with
83880** Yield or Return then continue to the next instruction. But if
83881** the coroutine launched by this instruction ends with
83882** EndCoroutine, then jump to P2 rather than continuing with the
83883** next instruction.
83884**
83885** See also: InitCoroutine
83886*/
83887case OP_Yield: { /* in1, jump */
83888 int pcDest;
83889 pIn1 = &aMem[pOp->p1];
83890 assert( VdbeMemDynamic(pIn1)==0 );
83891 pIn1->flags = MEM_Int;
83892 pcDest = (int)pIn1->u.i;
83893 pIn1->u.i = (int)(pOp - aOp);
83894 REGISTER_TRACE(pOp->p1, pIn1);
83895 pOp = &aOp[pcDest];
83896 break;
83897}
83898
83899/* Opcode: HaltIfNull P1 P2 P3 P4 P5
83900** Synopsis: if r[P3]=null halt
83901**
83902** Check the value in register P3. If it is NULL then Halt using
83903** parameter P1, P2, and P4 as if this were a Halt instruction. If the
83904** value in register P3 is not NULL, then this routine is a no-op.
83905** The P5 parameter should be 1.
83906*/
83907case OP_HaltIfNull: { /* in3 */
83908 pIn3 = &aMem[pOp->p3];
83909#ifdef SQLITE_DEBUG
83910 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
83911#endif
83912 if( (pIn3->flags & MEM_Null)==0 ) break;
83913 /* Fall through into OP_Halt */
83914}
83915
83916/* Opcode: Halt P1 P2 * P4 P5
83917**
83918** Exit immediately. All open cursors, etc are closed
83919** automatically.
83920**
83921** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
83922** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
83923** For errors, it can be some other value. If P1!=0 then P2 will determine
83924** whether or not to rollback the current transaction. Do not rollback
83925** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
83926** then back out all changes that have occurred during this execution of the
83927** VDBE, but do not rollback the transaction.
83928**
83929** If P4 is not null then it is an error message string.
83930**
83931** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
83932**
83933** 0: (no change)
83934** 1: NOT NULL contraint failed: P4
83935** 2: UNIQUE constraint failed: P4
83936** 3: CHECK constraint failed: P4
83937** 4: FOREIGN KEY constraint failed: P4
83938**
83939** If P5 is not zero and P4 is NULL, then everything after the ":" is
83940** omitted.
83941**
83942** There is an implied "Halt 0 0 0" instruction inserted at the very end of
83943** every program. So a jump past the last instruction of the program
83944** is the same as executing Halt.
83945*/
83946case OP_Halt: {
83947 VdbeFrame *pFrame;
83948 int pcx;
83949
83950 pcx = (int)(pOp - aOp);
83951#ifdef SQLITE_DEBUG
83952 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
83953#endif
83954 if( pOp->p1==SQLITE_OK && p->pFrame ){
83955 /* Halt the sub-program. Return control to the parent frame. */
83956 pFrame = p->pFrame;
83957 p->pFrame = pFrame->pParent;
83958 p->nFrame--;
83959 sqlite3VdbeSetChanges(db, p->nChange);
83960 pcx = sqlite3VdbeFrameRestore(pFrame);
83961 if( pOp->p2==OE_Ignore ){
83962 /* Instruction pcx is the OP_Program that invoked the sub-program
83963 ** currently being halted. If the p2 instruction of this OP_Halt
83964 ** instruction is set to OE_Ignore, then the sub-program is throwing
83965 ** an IGNORE exception. In this case jump to the address specified
83966 ** as the p2 of the calling OP_Program. */
83967 pcx = p->aOp[pcx].p2-1;
83968 }
83969 aOp = p->aOp;
83970 aMem = p->aMem;
83971 pOp = &aOp[pcx];
83972 break;
83973 }
83974 p->rc = pOp->p1;
83975 p->errorAction = (u8)pOp->p2;
83976 p->pc = pcx;
83977 assert( pOp->p5<=4 );
83978 if( p->rc ){
83979 if( pOp->p5 ){
83980 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
83981 "FOREIGN KEY" };
83982 testcase( pOp->p5==1 );
83983 testcase( pOp->p5==2 );
83984 testcase( pOp->p5==3 );
83985 testcase( pOp->p5==4 );
83986 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
83987 if( pOp->p4.z ){
83988 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
83989 }
83990 }else{
83991 sqlite3VdbeError(p, "%s", pOp->p4.z);
83992 }
83993 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
83994 }
83995 rc = sqlite3VdbeHalt(p);
83996 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
83997 if( rc==SQLITE_BUSY ){
83998 p->rc = SQLITE_BUSY;
83999 }else{
84000 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
84001 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
84002 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
84003 }
84004 goto vdbe_return;
84005}
84006
84007/* Opcode: Integer P1 P2 * * *
84008** Synopsis: r[P2]=P1
84009**
84010** The 32-bit integer value P1 is written into register P2.
84011*/
84012case OP_Integer: { /* out2 */
84013 pOut = out2Prerelease(p, pOp);
84014 pOut->u.i = pOp->p1;
84015 break;
84016}
84017
84018/* Opcode: Int64 * P2 * P4 *
84019** Synopsis: r[P2]=P4
84020**
84021** P4 is a pointer to a 64-bit integer value.
84022** Write that value into register P2.
84023*/
84024case OP_Int64: { /* out2 */
84025 pOut = out2Prerelease(p, pOp);
84026 assert( pOp->p4.pI64!=0 );
84027 pOut->u.i = *pOp->p4.pI64;
84028 break;
84029}
84030
84031#ifndef SQLITE_OMIT_FLOATING_POINT
84032/* Opcode: Real * P2 * P4 *
84033** Synopsis: r[P2]=P4
84034**
84035** P4 is a pointer to a 64-bit floating point value.
84036** Write that value into register P2.
84037*/
84038case OP_Real: { /* same as TK_FLOAT, out2 */
84039 pOut = out2Prerelease(p, pOp);
84040 pOut->flags = MEM_Real;
84041 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
84042 pOut->u.r = *pOp->p4.pReal;
84043 break;
84044}
84045#endif
84046
84047/* Opcode: String8 * P2 * P4 *
84048** Synopsis: r[P2]='P4'
84049**
84050** P4 points to a nul terminated UTF-8 string. This opcode is transformed
84051** into a String opcode before it is executed for the first time. During
84052** this transformation, the length of string P4 is computed and stored
84053** as the P1 parameter.
84054*/
84055case OP_String8: { /* same as TK_STRING, out2 */
84056 assert( pOp->p4.z!=0 );
84057 pOut = out2Prerelease(p, pOp);
84058 pOp->opcode = OP_String;
84059 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
84060
84061#ifndef SQLITE_OMIT_UTF16
84062 if( encoding!=SQLITE_UTF8 ){
84063 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
84064 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
84065 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
84066 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
84067 assert( VdbeMemDynamic(pOut)==0 );
84068 pOut->szMalloc = 0;
84069 pOut->flags |= MEM_Static;
84070 if( pOp->p4type==P4_DYNAMIC ){
84071 sqlite3DbFree(db, pOp->p4.z);
84072 }
84073 pOp->p4type = P4_DYNAMIC;
84074 pOp->p4.z = pOut->z;
84075 pOp->p1 = pOut->n;
84076 }
84077 testcase( rc==SQLITE_TOOBIG );
84078#endif
84079 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
84080 goto too_big;
84081 }
84082 assert( rc==SQLITE_OK );
84083 /* Fall through to the next case, OP_String */
84084}
84085
84086/* Opcode: String P1 P2 P3 P4 P5
84087** Synopsis: r[P2]='P4' (len=P1)
84088**
84089** The string value P4 of length P1 (bytes) is stored in register P2.
84090**
84091** If P3 is not zero and the content of register P3 is equal to P5, then
84092** the datatype of the register P2 is converted to BLOB. The content is
84093** the same sequence of bytes, it is merely interpreted as a BLOB instead
84094** of a string, as if it had been CAST. In other words:
84095**
84096** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
84097*/
84098case OP_String: { /* out2 */
84099 assert( pOp->p4.z!=0 );
84100 pOut = out2Prerelease(p, pOp);
84101 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
84102 pOut->z = pOp->p4.z;
84103 pOut->n = pOp->p1;
84104 pOut->enc = encoding;
84105 UPDATE_MAX_BLOBSIZE(pOut);
84106#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
84107 if( pOp->p3>0 ){
84108 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
84109 pIn3 = &aMem[pOp->p3];
84110 assert( pIn3->flags & MEM_Int );
84111 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
84112 }
84113#endif
84114 break;
84115}
84116
84117/* Opcode: Null P1 P2 P3 * *
84118** Synopsis: r[P2..P3]=NULL
84119**
84120** Write a NULL into registers P2. If P3 greater than P2, then also write
84121** NULL into register P3 and every register in between P2 and P3. If P3
84122** is less than P2 (typically P3 is zero) then only register P2 is
84123** set to NULL.
84124**
84125** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
84126** NULL values will not compare equal even if SQLITE_NULLEQ is set on
84127** OP_Ne or OP_Eq.
84128*/
84129case OP_Null: { /* out2 */
84130 int cnt;
84131 u16 nullFlag;
84132 pOut = out2Prerelease(p, pOp);
84133 cnt = pOp->p3-pOp->p2;
84134 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
84135 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
84136 pOut->n = 0;
84137#ifdef SQLITE_DEBUG
84138 pOut->uTemp = 0;
84139#endif
84140 while( cnt>0 ){
84141 pOut++;
84142 memAboutToChange(p, pOut);
84143 sqlite3VdbeMemSetNull(pOut);
84144 pOut->flags = nullFlag;
84145 pOut->n = 0;
84146 cnt--;
84147 }
84148 break;
84149}
84150
84151/* Opcode: SoftNull P1 * * * *
84152** Synopsis: r[P1]=NULL
84153**
84154** Set register P1 to have the value NULL as seen by the OP_MakeRecord
84155** instruction, but do not free any string or blob memory associated with
84156** the register, so that if the value was a string or blob that was
84157** previously copied using OP_SCopy, the copies will continue to be valid.
84158*/
84159case OP_SoftNull: {
84160 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
84161 pOut = &aMem[pOp->p1];
84162 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
84163 break;
84164}
84165
84166/* Opcode: Blob P1 P2 * P4 *
84167** Synopsis: r[P2]=P4 (len=P1)
84168**
84169** P4 points to a blob of data P1 bytes long. Store this
84170** blob in register P2.
84171*/
84172case OP_Blob: { /* out2 */
84173 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
84174 pOut = out2Prerelease(p, pOp);
84175 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
84176 pOut->enc = encoding;
84177 UPDATE_MAX_BLOBSIZE(pOut);
84178 break;
84179}
84180
84181/* Opcode: Variable P1 P2 * P4 *
84182** Synopsis: r[P2]=parameter(P1,P4)
84183**
84184** Transfer the values of bound parameter P1 into register P2
84185**
84186** If the parameter is named, then its name appears in P4.
84187** The P4 value is used by sqlite3_bind_parameter_name().
84188*/
84189case OP_Variable: { /* out2 */
84190 Mem *pVar; /* Value being transferred */
84191
84192 assert( pOp->p1>0 && pOp->p1<=p->nVar );
84193 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
84194 pVar = &p->aVar[pOp->p1 - 1];
84195 if( sqlite3VdbeMemTooBig(pVar) ){
84196 goto too_big;
84197 }
84198 pOut = &aMem[pOp->p2];
84199 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
84200 UPDATE_MAX_BLOBSIZE(pOut);
84201 break;
84202}
84203
84204/* Opcode: Move P1 P2 P3 * *
84205** Synopsis: r[P2@P3]=r[P1@P3]
84206**
84207** Move the P3 values in register P1..P1+P3-1 over into
84208** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
84209** left holding a NULL. It is an error for register ranges
84210** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
84211** for P3 to be less than 1.
84212*/
84213case OP_Move: {
84214 int n; /* Number of registers left to copy */
84215 int p1; /* Register to copy from */
84216 int p2; /* Register to copy to */
84217
84218 n = pOp->p3;
84219 p1 = pOp->p1;
84220 p2 = pOp->p2;
84221 assert( n>0 && p1>0 && p2>0 );
84222 assert( p1+n<=p2 || p2+n<=p1 );
84223
84224 pIn1 = &aMem[p1];
84225 pOut = &aMem[p2];
84226 do{
84227 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
84228 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
84229 assert( memIsValid(pIn1) );
84230 memAboutToChange(p, pOut);
84231 sqlite3VdbeMemMove(pOut, pIn1);
84232#ifdef SQLITE_DEBUG
84233 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){
84234 pOut->pScopyFrom += pOp->p2 - p1;
84235 }
84236#endif
84237 Deephemeralize(pOut);
84238 REGISTER_TRACE(p2++, pOut);
84239 pIn1++;
84240 pOut++;
84241 }while( --n );
84242 break;
84243}
84244
84245/* Opcode: Copy P1 P2 P3 * *
84246** Synopsis: r[P2@P3+1]=r[P1@P3+1]
84247**
84248** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
84249**
84250** This instruction makes a deep copy of the value. A duplicate
84251** is made of any string or blob constant. See also OP_SCopy.
84252*/
84253case OP_Copy: {
84254 int n;
84255
84256 n = pOp->p3;
84257 pIn1 = &aMem[pOp->p1];
84258 pOut = &aMem[pOp->p2];
84259 assert( pOut!=pIn1 );
84260 while( 1 ){
84261 memAboutToChange(p, pOut);
84262 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
84263 Deephemeralize(pOut);
84264#ifdef SQLITE_DEBUG
84265 pOut->pScopyFrom = 0;
84266#endif
84267 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
84268 if( (n--)==0 ) break;
84269 pOut++;
84270 pIn1++;
84271 }
84272 break;
84273}
84274
84275/* Opcode: SCopy P1 P2 * * *
84276** Synopsis: r[P2]=r[P1]
84277**
84278** Make a shallow copy of register P1 into register P2.
84279**
84280** This instruction makes a shallow copy of the value. If the value
84281** is a string or blob, then the copy is only a pointer to the
84282** original and hence if the original changes so will the copy.
84283** Worse, if the original is deallocated, the copy becomes invalid.
84284** Thus the program must guarantee that the original will not change
84285** during the lifetime of the copy. Use OP_Copy to make a complete
84286** copy.
84287*/
84288case OP_SCopy: { /* out2 */
84289 pIn1 = &aMem[pOp->p1];
84290 pOut = &aMem[pOp->p2];
84291 assert( pOut!=pIn1 );
84292 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
84293#ifdef SQLITE_DEBUG
84294 pOut->pScopyFrom = pIn1;
84295 pOut->mScopyFlags = pIn1->flags;
84296#endif
84297 break;
84298}
84299
84300/* Opcode: IntCopy P1 P2 * * *
84301** Synopsis: r[P2]=r[P1]
84302**
84303** Transfer the integer value held in register P1 into register P2.
84304**
84305** This is an optimized version of SCopy that works only for integer
84306** values.
84307*/
84308case OP_IntCopy: { /* out2 */
84309 pIn1 = &aMem[pOp->p1];
84310 assert( (pIn1->flags & MEM_Int)!=0 );
84311 pOut = &aMem[pOp->p2];
84312 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
84313 break;
84314}
84315
84316/* Opcode: ResultRow P1 P2 * * *
84317** Synopsis: output=r[P1@P2]
84318**
84319** The registers P1 through P1+P2-1 contain a single row of
84320** results. This opcode causes the sqlite3_step() call to terminate
84321** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
84322** structure to provide access to the r(P1)..r(P1+P2-1) values as
84323** the result row.
84324*/
84325case OP_ResultRow: {
84326 Mem *pMem;
84327 int i;
84328 assert( p->nResColumn==pOp->p2 );
84329 assert( pOp->p1>0 );
84330 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
84331
84332#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
84333 /* Run the progress counter just before returning.
84334 */
84335 if( db->xProgress!=0
84336 && nVmStep>=nProgressLimit
84337 && db->xProgress(db->pProgressArg)!=0
84338 ){
84339 rc = SQLITE_INTERRUPT;
84340 goto abort_due_to_error;
84341 }
84342#endif
84343
84344 /* If this statement has violated immediate foreign key constraints, do
84345 ** not return the number of rows modified. And do not RELEASE the statement
84346 ** transaction. It needs to be rolled back. */
84347 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
84348 assert( db->flags&SQLITE_CountRows );
84349 assert( p->usesStmtJournal );
84350 goto abort_due_to_error;
84351 }
84352
84353 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
84354 ** DML statements invoke this opcode to return the number of rows
84355 ** modified to the user. This is the only way that a VM that
84356 ** opens a statement transaction may invoke this opcode.
84357 **
84358 ** In case this is such a statement, close any statement transaction
84359 ** opened by this VM before returning control to the user. This is to
84360 ** ensure that statement-transactions are always nested, not overlapping.
84361 ** If the open statement-transaction is not closed here, then the user
84362 ** may step another VM that opens its own statement transaction. This
84363 ** may lead to overlapping statement transactions.
84364 **
84365 ** The statement transaction is never a top-level transaction. Hence
84366 ** the RELEASE call below can never fail.
84367 */
84368 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
84369 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
84370 assert( rc==SQLITE_OK );
84371
84372 /* Invalidate all ephemeral cursor row caches */
84373 p->cacheCtr = (p->cacheCtr + 2)|1;
84374
84375 /* Make sure the results of the current row are \000 terminated
84376 ** and have an assigned type. The results are de-ephemeralized as
84377 ** a side effect.
84378 */
84379 pMem = p->pResultSet = &aMem[pOp->p1];
84380 for(i=0; i<pOp->p2; i++){
84381 assert( memIsValid(&pMem[i]) );
84382 Deephemeralize(&pMem[i]);
84383 assert( (pMem[i].flags & MEM_Ephem)==0
84384 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
84385 sqlite3VdbeMemNulTerminate(&pMem[i]);
84386 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
84387 }
84388 if( db->mallocFailed ) goto no_mem;
84389
84390 if( db->mTrace & SQLITE_TRACE_ROW ){
84391 db->xTrace(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
84392 }
84393
84394 /* Return SQLITE_ROW
84395 */
84396 p->pc = (int)(pOp - aOp) + 1;
84397 rc = SQLITE_ROW;
84398 goto vdbe_return;
84399}
84400
84401/* Opcode: Concat P1 P2 P3 * *
84402** Synopsis: r[P3]=r[P2]+r[P1]
84403**
84404** Add the text in register P1 onto the end of the text in
84405** register P2 and store the result in register P3.
84406** If either the P1 or P2 text are NULL then store NULL in P3.
84407**
84408** P3 = P2 || P1
84409**
84410** It is illegal for P1 and P3 to be the same register. Sometimes,
84411** if P3 is the same register as P2, the implementation is able
84412** to avoid a memcpy().
84413*/
84414case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
84415 i64 nByte;
84416
84417 pIn1 = &aMem[pOp->p1];
84418 pIn2 = &aMem[pOp->p2];
84419 pOut = &aMem[pOp->p3];
84420 assert( pIn1!=pOut );
84421 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
84422 sqlite3VdbeMemSetNull(pOut);
84423 break;
84424 }
84425 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
84426 Stringify(pIn1, encoding);
84427 Stringify(pIn2, encoding);
84428 nByte = pIn1->n + pIn2->n;
84429 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
84430 goto too_big;
84431 }
84432 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
84433 goto no_mem;
84434 }
84435 MemSetTypeFlag(pOut, MEM_Str);
84436 if( pOut!=pIn2 ){
84437 memcpy(pOut->z, pIn2->z, pIn2->n);
84438 }
84439 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
84440 pOut->z[nByte]=0;
84441 pOut->z[nByte+1] = 0;
84442 pOut->flags |= MEM_Term;
84443 pOut->n = (int)nByte;
84444 pOut->enc = encoding;
84445 UPDATE_MAX_BLOBSIZE(pOut);
84446 break;
84447}
84448
84449/* Opcode: Add P1 P2 P3 * *
84450** Synopsis: r[P3]=r[P1]+r[P2]
84451**
84452** Add the value in register P1 to the value in register P2
84453** and store the result in register P3.
84454** If either input is NULL, the result is NULL.
84455*/
84456/* Opcode: Multiply P1 P2 P3 * *
84457** Synopsis: r[P3]=r[P1]*r[P2]
84458**
84459**
84460** Multiply the value in register P1 by the value in register P2
84461** and store the result in register P3.
84462** If either input is NULL, the result is NULL.
84463*/
84464/* Opcode: Subtract P1 P2 P3 * *
84465** Synopsis: r[P3]=r[P2]-r[P1]
84466**
84467** Subtract the value in register P1 from the value in register P2
84468** and store the result in register P3.
84469** If either input is NULL, the result is NULL.
84470*/
84471/* Opcode: Divide P1 P2 P3 * *
84472** Synopsis: r[P3]=r[P2]/r[P1]
84473**
84474** Divide the value in register P1 by the value in register P2
84475** and store the result in register P3 (P3=P2/P1). If the value in
84476** register P1 is zero, then the result is NULL. If either input is
84477** NULL, the result is NULL.
84478*/
84479/* Opcode: Remainder P1 P2 P3 * *
84480** Synopsis: r[P3]=r[P2]%r[P1]
84481**
84482** Compute the remainder after integer register P2 is divided by
84483** register P1 and store the result in register P3.
84484** If the value in register P1 is zero the result is NULL.
84485** If either operand is NULL, the result is NULL.
84486*/
84487case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
84488case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
84489case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
84490case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
84491case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
84492 char bIntint; /* Started out as two integer operands */
84493 u16 flags; /* Combined MEM_* flags from both inputs */
84494 u16 type1; /* Numeric type of left operand */
84495 u16 type2; /* Numeric type of right operand */
84496 i64 iA; /* Integer value of left operand */
84497 i64 iB; /* Integer value of right operand */
84498 double rA; /* Real value of left operand */
84499 double rB; /* Real value of right operand */
84500
84501 pIn1 = &aMem[pOp->p1];
84502 type1 = numericType(pIn1);
84503 pIn2 = &aMem[pOp->p2];
84504 type2 = numericType(pIn2);
84505 pOut = &aMem[pOp->p3];
84506 flags = pIn1->flags | pIn2->flags;
84507 if( (type1 & type2 & MEM_Int)!=0 ){
84508 iA = pIn1->u.i;
84509 iB = pIn2->u.i;
84510 bIntint = 1;
84511 switch( pOp->opcode ){
84512 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
84513 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
84514 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
84515 case OP_Divide: {
84516 if( iA==0 ) goto arithmetic_result_is_null;
84517 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
84518 iB /= iA;
84519 break;
84520 }
84521 default: {
84522 if( iA==0 ) goto arithmetic_result_is_null;
84523 if( iA==-1 ) iA = 1;
84524 iB %= iA;
84525 break;
84526 }
84527 }
84528 pOut->u.i = iB;
84529 MemSetTypeFlag(pOut, MEM_Int);
84530 }else if( (flags & MEM_Null)!=0 ){
84531 goto arithmetic_result_is_null;
84532 }else{
84533 bIntint = 0;
84534fp_math:
84535 rA = sqlite3VdbeRealValue(pIn1);
84536 rB = sqlite3VdbeRealValue(pIn2);
84537 switch( pOp->opcode ){
84538 case OP_Add: rB += rA; break;
84539 case OP_Subtract: rB -= rA; break;
84540 case OP_Multiply: rB *= rA; break;
84541 case OP_Divide: {
84542 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
84543 if( rA==(double)0 ) goto arithmetic_result_is_null;
84544 rB /= rA;
84545 break;
84546 }
84547 default: {
84548 iA = (i64)rA;
84549 iB = (i64)rB;
84550 if( iA==0 ) goto arithmetic_result_is_null;
84551 if( iA==-1 ) iA = 1;
84552 rB = (double)(iB % iA);
84553 break;
84554 }
84555 }
84556#ifdef SQLITE_OMIT_FLOATING_POINT
84557 pOut->u.i = rB;
84558 MemSetTypeFlag(pOut, MEM_Int);
84559#else
84560 if( sqlite3IsNaN(rB) ){
84561 goto arithmetic_result_is_null;
84562 }
84563 pOut->u.r = rB;
84564 MemSetTypeFlag(pOut, MEM_Real);
84565 if( ((type1|type2)&MEM_Real)==0 && !bIntint ){
84566 sqlite3VdbeIntegerAffinity(pOut);
84567 }
84568#endif
84569 }
84570 break;
84571
84572arithmetic_result_is_null:
84573 sqlite3VdbeMemSetNull(pOut);
84574 break;
84575}
84576
84577/* Opcode: CollSeq P1 * * P4
84578**
84579** P4 is a pointer to a CollSeq object. If the next call to a user function
84580** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
84581** be returned. This is used by the built-in min(), max() and nullif()
84582** functions.
84583**
84584** If P1 is not zero, then it is a register that a subsequent min() or
84585** max() aggregate will set to 1 if the current row is not the minimum or
84586** maximum. The P1 register is initialized to 0 by this instruction.
84587**
84588** The interface used by the implementation of the aforementioned functions
84589** to retrieve the collation sequence set by this opcode is not available
84590** publicly. Only built-in functions have access to this feature.
84591*/
84592case OP_CollSeq: {
84593 assert( pOp->p4type==P4_COLLSEQ );
84594 if( pOp->p1 ){
84595 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
84596 }
84597 break;
84598}
84599
84600/* Opcode: BitAnd P1 P2 P3 * *
84601** Synopsis: r[P3]=r[P1]&r[P2]
84602**
84603** Take the bit-wise AND of the values in register P1 and P2 and
84604** store the result in register P3.
84605** If either input is NULL, the result is NULL.
84606*/
84607/* Opcode: BitOr P1 P2 P3 * *
84608** Synopsis: r[P3]=r[P1]|r[P2]
84609**
84610** Take the bit-wise OR of the values in register P1 and P2 and
84611** store the result in register P3.
84612** If either input is NULL, the result is NULL.
84613*/
84614/* Opcode: ShiftLeft P1 P2 P3 * *
84615** Synopsis: r[P3]=r[P2]<<r[P1]
84616**
84617** Shift the integer value in register P2 to the left by the
84618** number of bits specified by the integer in register P1.
84619** Store the result in register P3.
84620** If either input is NULL, the result is NULL.
84621*/
84622/* Opcode: ShiftRight P1 P2 P3 * *
84623** Synopsis: r[P3]=r[P2]>>r[P1]
84624**
84625** Shift the integer value in register P2 to the right by the
84626** number of bits specified by the integer in register P1.
84627** Store the result in register P3.
84628** If either input is NULL, the result is NULL.
84629*/
84630case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
84631case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
84632case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
84633case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
84634 i64 iA;
84635 u64 uA;
84636 i64 iB;
84637 u8 op;
84638
84639 pIn1 = &aMem[pOp->p1];
84640 pIn2 = &aMem[pOp->p2];
84641 pOut = &aMem[pOp->p3];
84642 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
84643 sqlite3VdbeMemSetNull(pOut);
84644 break;
84645 }
84646 iA = sqlite3VdbeIntValue(pIn2);
84647 iB = sqlite3VdbeIntValue(pIn1);
84648 op = pOp->opcode;
84649 if( op==OP_BitAnd ){
84650 iA &= iB;
84651 }else if( op==OP_BitOr ){
84652 iA |= iB;
84653 }else if( iB!=0 ){
84654 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
84655
84656 /* If shifting by a negative amount, shift in the other direction */
84657 if( iB<0 ){
84658 assert( OP_ShiftRight==OP_ShiftLeft+1 );
84659 op = 2*OP_ShiftLeft + 1 - op;
84660 iB = iB>(-64) ? -iB : 64;
84661 }
84662
84663 if( iB>=64 ){
84664 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
84665 }else{
84666 memcpy(&uA, &iA, sizeof(uA));
84667 if( op==OP_ShiftLeft ){
84668 uA <<= iB;
84669 }else{
84670 uA >>= iB;
84671 /* Sign-extend on a right shift of a negative number */
84672 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
84673 }
84674 memcpy(&iA, &uA, sizeof(iA));
84675 }
84676 }
84677 pOut->u.i = iA;
84678 MemSetTypeFlag(pOut, MEM_Int);
84679 break;
84680}
84681
84682/* Opcode: AddImm P1 P2 * * *
84683** Synopsis: r[P1]=r[P1]+P2
84684**
84685** Add the constant P2 to the value in register P1.
84686** The result is always an integer.
84687**
84688** To force any register to be an integer, just add 0.
84689*/
84690case OP_AddImm: { /* in1 */
84691 pIn1 = &aMem[pOp->p1];
84692 memAboutToChange(p, pIn1);
84693 sqlite3VdbeMemIntegerify(pIn1);
84694 pIn1->u.i += pOp->p2;
84695 break;
84696}
84697
84698/* Opcode: MustBeInt P1 P2 * * *
84699**
84700** Force the value in register P1 to be an integer. If the value
84701** in P1 is not an integer and cannot be converted into an integer
84702** without data loss, then jump immediately to P2, or if P2==0
84703** raise an SQLITE_MISMATCH exception.
84704*/
84705case OP_MustBeInt: { /* jump, in1 */
84706 pIn1 = &aMem[pOp->p1];
84707 if( (pIn1->flags & MEM_Int)==0 ){
84708 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
84709 VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
84710 if( (pIn1->flags & MEM_Int)==0 ){
84711 if( pOp->p2==0 ){
84712 rc = SQLITE_MISMATCH;
84713 goto abort_due_to_error;
84714 }else{
84715 goto jump_to_p2;
84716 }
84717 }
84718 }
84719 MemSetTypeFlag(pIn1, MEM_Int);
84720 break;
84721}
84722
84723#ifndef SQLITE_OMIT_FLOATING_POINT
84724/* Opcode: RealAffinity P1 * * * *
84725**
84726** If register P1 holds an integer convert it to a real value.
84727**
84728** This opcode is used when extracting information from a column that
84729** has REAL affinity. Such column values may still be stored as
84730** integers, for space efficiency, but after extraction we want them
84731** to have only a real value.
84732*/
84733case OP_RealAffinity: { /* in1 */
84734 pIn1 = &aMem[pOp->p1];
84735 if( pIn1->flags & MEM_Int ){
84736 sqlite3VdbeMemRealify(pIn1);
84737 }
84738 break;
84739}
84740#endif
84741
84742#ifndef SQLITE_OMIT_CAST
84743/* Opcode: Cast P1 P2 * * *
84744** Synopsis: affinity(r[P1])
84745**
84746** Force the value in register P1 to be the type defined by P2.
84747**
84748** <ul>
84749** <li> P2=='A' &rarr; BLOB
84750** <li> P2=='B' &rarr; TEXT
84751** <li> P2=='C' &rarr; NUMERIC
84752** <li> P2=='D' &rarr; INTEGER
84753** <li> P2=='E' &rarr; REAL
84754** </ul>
84755**
84756** A NULL value is not changed by this routine. It remains NULL.
84757*/
84758case OP_Cast: { /* in1 */
84759 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
84760 testcase( pOp->p2==SQLITE_AFF_TEXT );
84761 testcase( pOp->p2==SQLITE_AFF_BLOB );
84762 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
84763 testcase( pOp->p2==SQLITE_AFF_INTEGER );
84764 testcase( pOp->p2==SQLITE_AFF_REAL );
84765 pIn1 = &aMem[pOp->p1];
84766 memAboutToChange(p, pIn1);
84767 rc = ExpandBlob(pIn1);
84768 sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
84769 UPDATE_MAX_BLOBSIZE(pIn1);
84770 if( rc ) goto abort_due_to_error;
84771 break;
84772}
84773#endif /* SQLITE_OMIT_CAST */
84774
84775/* Opcode: Eq P1 P2 P3 P4 P5
84776** Synopsis: IF r[P3]==r[P1]
84777**
84778** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
84779** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5, then
84780** store the result of comparison in register P2.
84781**
84782** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
84783** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
84784** to coerce both inputs according to this affinity before the
84785** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
84786** affinity is used. Note that the affinity conversions are stored
84787** back into the input registers P1 and P3. So this opcode can cause
84788** persistent changes to registers P1 and P3.
84789**
84790** Once any conversions have taken place, and neither value is NULL,
84791** the values are compared. If both values are blobs then memcmp() is
84792** used to determine the results of the comparison. If both values
84793** are text, then the appropriate collating function specified in
84794** P4 is used to do the comparison. If P4 is not specified then
84795** memcmp() is used to compare text string. If both values are
84796** numeric, then a numeric comparison is used. If the two values
84797** are of different types, then numbers are considered less than
84798** strings and strings are considered less than blobs.
84799**
84800** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
84801** true or false and is never NULL. If both operands are NULL then the result
84802** of comparison is true. If either operand is NULL then the result is false.
84803** If neither operand is NULL the result is the same as it would be if
84804** the SQLITE_NULLEQ flag were omitted from P5.
84805**
84806** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
84807** content of r[P2] is only changed if the new value is NULL or 0 (false).
84808** In other words, a prior r[P2] value will not be overwritten by 1 (true).
84809*/
84810/* Opcode: Ne P1 P2 P3 P4 P5
84811** Synopsis: IF r[P3]!=r[P1]
84812**
84813** This works just like the Eq opcode except that the jump is taken if
84814** the operands in registers P1 and P3 are not equal. See the Eq opcode for
84815** additional information.
84816**
84817** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
84818** content of r[P2] is only changed if the new value is NULL or 1 (true).
84819** In other words, a prior r[P2] value will not be overwritten by 0 (false).
84820*/
84821/* Opcode: Lt P1 P2 P3 P4 P5
84822** Synopsis: IF r[P3]<r[P1]
84823**
84824** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
84825** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5 store
84826** the result of comparison (0 or 1 or NULL) into register P2.
84827**
84828** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
84829** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
84830** bit is clear then fall through if either operand is NULL.
84831**
84832** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
84833** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
84834** to coerce both inputs according to this affinity before the
84835** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
84836** affinity is used. Note that the affinity conversions are stored
84837** back into the input registers P1 and P3. So this opcode can cause
84838** persistent changes to registers P1 and P3.
84839**
84840** Once any conversions have taken place, and neither value is NULL,
84841** the values are compared. If both values are blobs then memcmp() is
84842** used to determine the results of the comparison. If both values
84843** are text, then the appropriate collating function specified in
84844** P4 is used to do the comparison. If P4 is not specified then
84845** memcmp() is used to compare text string. If both values are
84846** numeric, then a numeric comparison is used. If the two values
84847** are of different types, then numbers are considered less than
84848** strings and strings are considered less than blobs.
84849*/
84850/* Opcode: Le P1 P2 P3 P4 P5
84851** Synopsis: IF r[P3]<=r[P1]
84852**
84853** This works just like the Lt opcode except that the jump is taken if
84854** the content of register P3 is less than or equal to the content of
84855** register P1. See the Lt opcode for additional information.
84856*/
84857/* Opcode: Gt P1 P2 P3 P4 P5
84858** Synopsis: IF r[P3]>r[P1]
84859**
84860** This works just like the Lt opcode except that the jump is taken if
84861** the content of register P3 is greater than the content of
84862** register P1. See the Lt opcode for additional information.
84863*/
84864/* Opcode: Ge P1 P2 P3 P4 P5
84865** Synopsis: IF r[P3]>=r[P1]
84866**
84867** This works just like the Lt opcode except that the jump is taken if
84868** the content of register P3 is greater than or equal to the content of
84869** register P1. See the Lt opcode for additional information.
84870*/
84871case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
84872case OP_Ne: /* same as TK_NE, jump, in1, in3 */
84873case OP_Lt: /* same as TK_LT, jump, in1, in3 */
84874case OP_Le: /* same as TK_LE, jump, in1, in3 */
84875case OP_Gt: /* same as TK_GT, jump, in1, in3 */
84876case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
84877 int res, res2; /* Result of the comparison of pIn1 against pIn3 */
84878 char affinity; /* Affinity to use for comparison */
84879 u16 flags1; /* Copy of initial value of pIn1->flags */
84880 u16 flags3; /* Copy of initial value of pIn3->flags */
84881
84882 pIn1 = &aMem[pOp->p1];
84883 pIn3 = &aMem[pOp->p3];
84884 flags1 = pIn1->flags;
84885 flags3 = pIn3->flags;
84886 if( (flags1 | flags3)&MEM_Null ){
84887 /* One or both operands are NULL */
84888 if( pOp->p5 & SQLITE_NULLEQ ){
84889 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
84890 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
84891 ** or not both operands are null.
84892 */
84893 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
84894 assert( (flags1 & MEM_Cleared)==0 );
84895 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 );
84896 if( (flags1&flags3&MEM_Null)!=0
84897 && (flags3&MEM_Cleared)==0
84898 ){
84899 res = 0; /* Operands are equal */
84900 }else{
84901 res = 1; /* Operands are not equal */
84902 }
84903 }else{
84904 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
84905 ** then the result is always NULL.
84906 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
84907 */
84908 if( pOp->p5 & SQLITE_STOREP2 ){
84909 pOut = &aMem[pOp->p2];
84910 iCompare = 1; /* Operands are not equal */
84911 memAboutToChange(p, pOut);
84912 MemSetTypeFlag(pOut, MEM_Null);
84913 REGISTER_TRACE(pOp->p2, pOut);
84914 }else{
84915 VdbeBranchTaken(2,3);
84916 if( pOp->p5 & SQLITE_JUMPIFNULL ){
84917 goto jump_to_p2;
84918 }
84919 }
84920 break;
84921 }
84922 }else{
84923 /* Neither operand is NULL. Do a comparison. */
84924 affinity = pOp->p5 & SQLITE_AFF_MASK;
84925 if( affinity>=SQLITE_AFF_NUMERIC ){
84926 if( (flags1 | flags3)&MEM_Str ){
84927 if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
84928 applyNumericAffinity(pIn1,0);
84929 assert( flags3==pIn3->flags );
84930 /* testcase( flags3!=pIn3->flags );
84931 ** this used to be possible with pIn1==pIn3, but not since
84932 ** the column cache was removed. The following assignment
84933 ** is essentially a no-op. But, it provides defense-in-depth
84934 ** in case our analysis is incorrect, so it is left in. */
84935 flags3 = pIn3->flags;
84936 }
84937 if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
84938 applyNumericAffinity(pIn3,0);
84939 }
84940 }
84941 /* Handle the common case of integer comparison here, as an
84942 ** optimization, to avoid a call to sqlite3MemCompare() */
84943 if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){
84944 if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; }
84945 if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; }
84946 res = 0;
84947 goto compare_op;
84948 }
84949 }else if( affinity==SQLITE_AFF_TEXT ){
84950 if( (flags1 & MEM_Str)==0 && (flags1 & (MEM_Int|MEM_Real))!=0 ){
84951 testcase( pIn1->flags & MEM_Int );
84952 testcase( pIn1->flags & MEM_Real );
84953 sqlite3VdbeMemStringify(pIn1, encoding, 1);
84954 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
84955 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
84956 assert( pIn1!=pIn3 );
84957 }
84958 if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){
84959 testcase( pIn3->flags & MEM_Int );
84960 testcase( pIn3->flags & MEM_Real );
84961 sqlite3VdbeMemStringify(pIn3, encoding, 1);
84962 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
84963 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
84964 }
84965 }
84966 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
84967 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
84968 }
84969compare_op:
84970 /* At this point, res is negative, zero, or positive if reg[P1] is
84971 ** less than, equal to, or greater than reg[P3], respectively. Compute
84972 ** the answer to this operator in res2, depending on what the comparison
84973 ** operator actually is. The next block of code depends on the fact
84974 ** that the 6 comparison operators are consecutive integers in this
84975 ** order: NE, EQ, GT, LE, LT, GE */
84976 assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 );
84977 assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 );
84978 if( res<0 ){ /* ne, eq, gt, le, lt, ge */
84979 static const unsigned char aLTb[] = { 1, 0, 0, 1, 1, 0 };
84980 res2 = aLTb[pOp->opcode - OP_Ne];
84981 }else if( res==0 ){
84982 static const unsigned char aEQb[] = { 0, 1, 0, 1, 0, 1 };
84983 res2 = aEQb[pOp->opcode - OP_Ne];
84984 }else{
84985 static const unsigned char aGTb[] = { 1, 0, 1, 0, 0, 1 };
84986 res2 = aGTb[pOp->opcode - OP_Ne];
84987 }
84988
84989 /* Undo any changes made by applyAffinity() to the input registers. */
84990 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
84991 pIn1->flags = flags1;
84992 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
84993 pIn3->flags = flags3;
84994
84995 if( pOp->p5 & SQLITE_STOREP2 ){
84996 pOut = &aMem[pOp->p2];
84997 iCompare = res;
84998 if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){
84999 /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1
85000 ** and prevents OP_Ne from overwriting NULL with 0. This flag
85001 ** is only used in contexts where either:
85002 ** (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0)
85003 ** (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1)
85004 ** Therefore it is not necessary to check the content of r[P2] for
85005 ** NULL. */
85006 assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq );
85007 assert( res2==0 || res2==1 );
85008 testcase( res2==0 && pOp->opcode==OP_Eq );
85009 testcase( res2==1 && pOp->opcode==OP_Eq );
85010 testcase( res2==0 && pOp->opcode==OP_Ne );
85011 testcase( res2==1 && pOp->opcode==OP_Ne );
85012 if( (pOp->opcode==OP_Eq)==res2 ) break;
85013 }
85014 memAboutToChange(p, pOut);
85015 MemSetTypeFlag(pOut, MEM_Int);
85016 pOut->u.i = res2;
85017 REGISTER_TRACE(pOp->p2, pOut);
85018 }else{
85019 VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
85020 if( res2 ){
85021 goto jump_to_p2;
85022 }
85023 }
85024 break;
85025}
85026
85027/* Opcode: ElseNotEq * P2 * * *
85028**
85029** This opcode must immediately follow an OP_Lt or OP_Gt comparison operator.
85030** If result of an OP_Eq comparison on the same two operands
85031** would have be NULL or false (0), then then jump to P2.
85032** If the result of an OP_Eq comparison on the two previous operands
85033** would have been true (1), then fall through.
85034*/
85035case OP_ElseNotEq: { /* same as TK_ESCAPE, jump */
85036 assert( pOp>aOp );
85037 assert( pOp[-1].opcode==OP_Lt || pOp[-1].opcode==OP_Gt );
85038 assert( pOp[-1].p5 & SQLITE_STOREP2 );
85039 VdbeBranchTaken(iCompare!=0, 2);
85040 if( iCompare!=0 ) goto jump_to_p2;
85041 break;
85042}
85043
85044
85045/* Opcode: Permutation * * * P4 *
85046**
85047** Set the permutation used by the OP_Compare operator in the next
85048** instruction. The permutation is stored in the P4 operand.
85049**
85050** The permutation is only valid until the next OP_Compare that has
85051** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
85052** occur immediately prior to the OP_Compare.
85053**
85054** The first integer in the P4 integer array is the length of the array
85055** and does not become part of the permutation.
85056*/
85057case OP_Permutation: {
85058 assert( pOp->p4type==P4_INTARRAY );
85059 assert( pOp->p4.ai );
85060 assert( pOp[1].opcode==OP_Compare );
85061 assert( pOp[1].p5 & OPFLAG_PERMUTE );
85062 break;
85063}
85064
85065/* Opcode: Compare P1 P2 P3 P4 P5
85066** Synopsis: r[P1@P3] <-> r[P2@P3]
85067**
85068** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
85069** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
85070** the comparison for use by the next OP_Jump instruct.
85071**
85072** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
85073** determined by the most recent OP_Permutation operator. If the
85074** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
85075** order.
85076**
85077** P4 is a KeyInfo structure that defines collating sequences and sort
85078** orders for the comparison. The permutation applies to registers
85079** only. The KeyInfo elements are used sequentially.
85080**
85081** The comparison is a sort comparison, so NULLs compare equal,
85082** NULLs are less than numbers, numbers are less than strings,
85083** and strings are less than blobs.
85084*/
85085case OP_Compare: {
85086 int n;
85087 int i;
85088 int p1;
85089 int p2;
85090 const KeyInfo *pKeyInfo;
85091 int idx;
85092 CollSeq *pColl; /* Collating sequence to use on this term */
85093 int bRev; /* True for DESCENDING sort order */
85094 int *aPermute; /* The permutation */
85095
85096 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
85097 aPermute = 0;
85098 }else{
85099 assert( pOp>aOp );
85100 assert( pOp[-1].opcode==OP_Permutation );
85101 assert( pOp[-1].p4type==P4_INTARRAY );
85102 aPermute = pOp[-1].p4.ai + 1;
85103 assert( aPermute!=0 );
85104 }
85105 n = pOp->p3;
85106 pKeyInfo = pOp->p4.pKeyInfo;
85107 assert( n>0 );
85108 assert( pKeyInfo!=0 );
85109 p1 = pOp->p1;
85110 p2 = pOp->p2;
85111#ifdef SQLITE_DEBUG
85112 if( aPermute ){
85113 int k, mx = 0;
85114 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
85115 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
85116 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
85117 }else{
85118 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
85119 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
85120 }
85121#endif /* SQLITE_DEBUG */
85122 for(i=0; i<n; i++){
85123 idx = aPermute ? aPermute[i] : i;
85124 assert( memIsValid(&aMem[p1+idx]) );
85125 assert( memIsValid(&aMem[p2+idx]) );
85126 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
85127 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
85128 assert( i<pKeyInfo->nKeyField );
85129 pColl = pKeyInfo->aColl[i];
85130 bRev = pKeyInfo->aSortOrder[i];
85131 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
85132 if( iCompare ){
85133 if( bRev ) iCompare = -iCompare;
85134 break;
85135 }
85136 }
85137 break;
85138}
85139
85140/* Opcode: Jump P1 P2 P3 * *
85141**
85142** Jump to the instruction at address P1, P2, or P3 depending on whether
85143** in the most recent OP_Compare instruction the P1 vector was less than
85144** equal to, or greater than the P2 vector, respectively.
85145*/
85146case OP_Jump: { /* jump */
85147 if( iCompare<0 ){
85148 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
85149 }else if( iCompare==0 ){
85150 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
85151 }else{
85152 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
85153 }
85154 break;
85155}
85156
85157/* Opcode: And P1 P2 P3 * *
85158** Synopsis: r[P3]=(r[P1] && r[P2])
85159**
85160** Take the logical AND of the values in registers P1 and P2 and
85161** write the result into register P3.
85162**
85163** If either P1 or P2 is 0 (false) then the result is 0 even if
85164** the other input is NULL. A NULL and true or two NULLs give
85165** a NULL output.
85166*/
85167/* Opcode: Or P1 P2 P3 * *
85168** Synopsis: r[P3]=(r[P1] || r[P2])
85169**
85170** Take the logical OR of the values in register P1 and P2 and
85171** store the answer in register P3.
85172**
85173** If either P1 or P2 is nonzero (true) then the result is 1 (true)
85174** even if the other input is NULL. A NULL and false or two NULLs
85175** give a NULL output.
85176*/
85177case OP_And: /* same as TK_AND, in1, in2, out3 */
85178case OP_Or: { /* same as TK_OR, in1, in2, out3 */
85179 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
85180 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
85181
85182 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
85183 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
85184 if( pOp->opcode==OP_And ){
85185 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
85186 v1 = and_logic[v1*3+v2];
85187 }else{
85188 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
85189 v1 = or_logic[v1*3+v2];
85190 }
85191 pOut = &aMem[pOp->p3];
85192 if( v1==2 ){
85193 MemSetTypeFlag(pOut, MEM_Null);
85194 }else{
85195 pOut->u.i = v1;
85196 MemSetTypeFlag(pOut, MEM_Int);
85197 }
85198 break;
85199}
85200
85201/* Opcode: IsTrue P1 P2 P3 P4 *
85202** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
85203**
85204** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
85205** IS NOT FALSE operators.
85206**
85207** Interpret the value in register P1 as a boolean value. Store that
85208** boolean (a 0 or 1) in register P2. Or if the value in register P1 is
85209** NULL, then the P3 is stored in register P2. Invert the answer if P4
85210** is 1.
85211**
85212** The logic is summarized like this:
85213**
85214** <ul>
85215** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE
85216** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE
85217** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE
85218** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE
85219** </ul>
85220*/
85221case OP_IsTrue: { /* in1, out2 */
85222 assert( pOp->p4type==P4_INT32 );
85223 assert( pOp->p4.i==0 || pOp->p4.i==1 );
85224 assert( pOp->p3==0 || pOp->p3==1 );
85225 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
85226 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
85227 break;
85228}
85229
85230/* Opcode: Not P1 P2 * * *
85231** Synopsis: r[P2]= !r[P1]
85232**
85233** Interpret the value in register P1 as a boolean value. Store the
85234** boolean complement in register P2. If the value in register P1 is
85235** NULL, then a NULL is stored in P2.
85236*/
85237case OP_Not: { /* same as TK_NOT, in1, out2 */
85238 pIn1 = &aMem[pOp->p1];
85239 pOut = &aMem[pOp->p2];
85240 if( (pIn1->flags & MEM_Null)==0 ){
85241 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0));
85242 }else{
85243 sqlite3VdbeMemSetNull(pOut);
85244 }
85245 break;
85246}
85247
85248/* Opcode: BitNot P1 P2 * * *
85249** Synopsis: r[P2]= ~r[P1]
85250**
85251** Interpret the content of register P1 as an integer. Store the
85252** ones-complement of the P1 value into register P2. If P1 holds
85253** a NULL then store a NULL in P2.
85254*/
85255case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
85256 pIn1 = &aMem[pOp->p1];
85257 pOut = &aMem[pOp->p2];
85258 sqlite3VdbeMemSetNull(pOut);
85259 if( (pIn1->flags & MEM_Null)==0 ){
85260 pOut->flags = MEM_Int;
85261 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
85262 }
85263 break;
85264}
85265
85266/* Opcode: Once P1 P2 * * *
85267**
85268** Fall through to the next instruction the first time this opcode is
85269** encountered on each invocation of the byte-code program. Jump to P2
85270** on the second and all subsequent encounters during the same invocation.
85271**
85272** Top-level programs determine first invocation by comparing the P1
85273** operand against the P1 operand on the OP_Init opcode at the beginning
85274** of the program. If the P1 values differ, then fall through and make
85275** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are
85276** the same then take the jump.
85277**
85278** For subprograms, there is a bitmask in the VdbeFrame that determines
85279** whether or not the jump should be taken. The bitmask is necessary
85280** because the self-altering code trick does not work for recursive
85281** triggers.
85282*/
85283case OP_Once: { /* jump */
85284 u32 iAddr; /* Address of this instruction */
85285 assert( p->aOp[0].opcode==OP_Init );
85286 if( p->pFrame ){
85287 iAddr = (int)(pOp - p->aOp);
85288 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
85289 VdbeBranchTaken(1, 2);
85290 goto jump_to_p2;
85291 }
85292 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
85293 }else{
85294 if( p->aOp[0].p1==pOp->p1 ){
85295 VdbeBranchTaken(1, 2);
85296 goto jump_to_p2;
85297 }
85298 }
85299 VdbeBranchTaken(0, 2);
85300 pOp->p1 = p->aOp[0].p1;
85301 break;
85302}
85303
85304/* Opcode: If P1 P2 P3 * *
85305**
85306** Jump to P2 if the value in register P1 is true. The value
85307** is considered true if it is numeric and non-zero. If the value
85308** in P1 is NULL then take the jump if and only if P3 is non-zero.
85309*/
85310case OP_If: { /* jump, in1 */
85311 int c;
85312 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
85313 VdbeBranchTaken(c!=0, 2);
85314 if( c ) goto jump_to_p2;
85315 break;
85316}
85317
85318/* Opcode: IfNot P1 P2 P3 * *
85319**
85320** Jump to P2 if the value in register P1 is False. The value
85321** is considered false if it has a numeric value of zero. If the value
85322** in P1 is NULL then take the jump if and only if P3 is non-zero.
85323*/
85324case OP_IfNot: { /* jump, in1 */
85325 int c;
85326 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
85327 VdbeBranchTaken(c!=0, 2);
85328 if( c ) goto jump_to_p2;
85329 break;
85330}
85331
85332/* Opcode: IsNull P1 P2 * * *
85333** Synopsis: if r[P1]==NULL goto P2
85334**
85335** Jump to P2 if the value in register P1 is NULL.
85336*/
85337case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
85338 pIn1 = &aMem[pOp->p1];
85339 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
85340 if( (pIn1->flags & MEM_Null)!=0 ){
85341 goto jump_to_p2;
85342 }
85343 break;
85344}
85345
85346/* Opcode: NotNull P1 P2 * * *
85347** Synopsis: if r[P1]!=NULL goto P2
85348**
85349** Jump to P2 if the value in register P1 is not NULL.
85350*/
85351case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
85352 pIn1 = &aMem[pOp->p1];
85353 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
85354 if( (pIn1->flags & MEM_Null)==0 ){
85355 goto jump_to_p2;
85356 }
85357 break;
85358}
85359
85360/* Opcode: IfNullRow P1 P2 P3 * *
85361** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
85362**
85363** Check the cursor P1 to see if it is currently pointing at a NULL row.
85364** If it is, then set register P3 to NULL and jump immediately to P2.
85365** If P1 is not on a NULL row, then fall through without making any
85366** changes.
85367*/
85368case OP_IfNullRow: { /* jump */
85369 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
85370 assert( p->apCsr[pOp->p1]!=0 );
85371 if( p->apCsr[pOp->p1]->nullRow ){
85372 sqlite3VdbeMemSetNull(aMem + pOp->p3);
85373 goto jump_to_p2;
85374 }
85375 break;
85376}
85377
85378#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
85379/* Opcode: Offset P1 P2 P3 * *
85380** Synopsis: r[P3] = sqlite_offset(P1)
85381**
85382** Store in register r[P3] the byte offset into the database file that is the
85383** start of the payload for the record at which that cursor P1 is currently
85384** pointing.
85385**
85386** P2 is the column number for the argument to the sqlite_offset() function.
85387** This opcode does not use P2 itself, but the P2 value is used by the
85388** code generator. The P1, P2, and P3 operands to this opcode are the
85389** same as for OP_Column.
85390**
85391** This opcode is only available if SQLite is compiled with the
85392** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
85393*/
85394case OP_Offset: { /* out3 */
85395 VdbeCursor *pC; /* The VDBE cursor */
85396 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
85397 pC = p->apCsr[pOp->p1];
85398 pOut = &p->aMem[pOp->p3];
85399 if( NEVER(pC==0) || pC->eCurType!=CURTYPE_BTREE ){
85400 sqlite3VdbeMemSetNull(pOut);
85401 }else{
85402 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
85403 }
85404 break;
85405}
85406#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
85407
85408/* Opcode: Column P1 P2 P3 P4 P5
85409** Synopsis: r[P3]=PX
85410**
85411** Interpret the data that cursor P1 points to as a structure built using
85412** the MakeRecord instruction. (See the MakeRecord opcode for additional
85413** information about the format of the data.) Extract the P2-th column
85414** from this record. If there are less that (P2+1)
85415** values in the record, extract a NULL.
85416**
85417** The value extracted is stored in register P3.
85418**
85419** If the record contains fewer than P2 fields, then extract a NULL. Or,
85420** if the P4 argument is a P4_MEM use the value of the P4 argument as
85421** the result.
85422**
85423** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
85424** then the cache of the cursor is reset prior to extracting the column.
85425** The first OP_Column against a pseudo-table after the value of the content
85426** register has changed should have this bit set.
85427**
85428** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then
85429** the result is guaranteed to only be used as the argument of a length()
85430** or typeof() function, respectively. The loading of large blobs can be
85431** skipped for length() and all content loading can be skipped for typeof().
85432*/
85433case OP_Column: {
85434 int p2; /* column number to retrieve */
85435 VdbeCursor *pC; /* The VDBE cursor */
85436 BtCursor *pCrsr; /* The BTree cursor */
85437 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
85438 int len; /* The length of the serialized data for the column */
85439 int i; /* Loop counter */
85440 Mem *pDest; /* Where to write the extracted value */
85441 Mem sMem; /* For storing the record being decoded */
85442 const u8 *zData; /* Part of the record being decoded */
85443 const u8 *zHdr; /* Next unparsed byte of the header */
85444 const u8 *zEndHdr; /* Pointer to first byte after the header */
85445 u64 offset64; /* 64-bit offset */
85446 u32 t; /* A type code from the record header */
85447 Mem *pReg; /* PseudoTable input register */
85448
85449 pC = p->apCsr[pOp->p1];
85450 p2 = pOp->p2;
85451
85452 /* If the cursor cache is stale (meaning it is not currently point at
85453 ** the correct row) then bring it up-to-date by doing the necessary
85454 ** B-Tree seek. */
85455 rc = sqlite3VdbeCursorMoveto(&pC, &p2);
85456 if( rc ) goto abort_due_to_error;
85457
85458 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
85459 pDest = &aMem[pOp->p3];
85460 memAboutToChange(p, pDest);
85461 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
85462 assert( pC!=0 );
85463 assert( p2<pC->nField );
85464 aOffset = pC->aOffset;
85465 assert( pC->eCurType!=CURTYPE_VTAB );
85466 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
85467 assert( pC->eCurType!=CURTYPE_SORTER );
85468
85469 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
85470 if( pC->nullRow ){
85471 if( pC->eCurType==CURTYPE_PSEUDO ){
85472 /* For the special case of as pseudo-cursor, the seekResult field
85473 ** identifies the register that holds the record */
85474 assert( pC->seekResult>0 );
85475 pReg = &aMem[pC->seekResult];
85476 assert( pReg->flags & MEM_Blob );
85477 assert( memIsValid(pReg) );
85478 pC->payloadSize = pC->szRow = pReg->n;
85479 pC->aRow = (u8*)pReg->z;
85480 }else{
85481 sqlite3VdbeMemSetNull(pDest);
85482 goto op_column_out;
85483 }
85484 }else{
85485 pCrsr = pC->uc.pCursor;
85486 assert( pC->eCurType==CURTYPE_BTREE );
85487 assert( pCrsr );
85488 assert( sqlite3BtreeCursorIsValid(pCrsr) );
85489 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
85490 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
85491 assert( pC->szRow<=pC->payloadSize );
85492 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
85493 if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
85494 goto too_big;
85495 }
85496 }
85497 pC->cacheStatus = p->cacheCtr;
85498 pC->iHdrOffset = getVarint32(pC->aRow, aOffset[0]);
85499 pC->nHdrParsed = 0;
85500
85501
85502 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
85503 /* pC->aRow does not have to hold the entire row, but it does at least
85504 ** need to cover the header of the record. If pC->aRow does not contain
85505 ** the complete header, then set it to zero, forcing the header to be
85506 ** dynamically allocated. */
85507 pC->aRow = 0;
85508 pC->szRow = 0;
85509
85510 /* Make sure a corrupt database has not given us an oversize header.
85511 ** Do this now to avoid an oversize memory allocation.
85512 **
85513 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
85514 ** types use so much data space that there can only be 4096 and 32 of
85515 ** them, respectively. So the maximum header length results from a
85516 ** 3-byte type for each of the maximum of 32768 columns plus three
85517 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
85518 */
85519 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
85520 goto op_column_corrupt;
85521 }
85522 }else{
85523 /* This is an optimization. By skipping over the first few tests
85524 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
85525 ** measurable performance gain.
85526 **
85527 ** This branch is taken even if aOffset[0]==0. Such a record is never
85528 ** generated by SQLite, and could be considered corruption, but we
85529 ** accept it for historical reasons. When aOffset[0]==0, the code this
85530 ** branch jumps to reads past the end of the record, but never more
85531 ** than a few bytes. Even if the record occurs at the end of the page
85532 ** content area, the "page header" comes after the page content and so
85533 ** this overread is harmless. Similar overreads can occur for a corrupt
85534 ** database file.
85535 */
85536 zData = pC->aRow;
85537 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
85538 testcase( aOffset[0]==0 );
85539 goto op_column_read_header;
85540 }
85541 }
85542
85543 /* Make sure at least the first p2+1 entries of the header have been
85544 ** parsed and valid information is in aOffset[] and pC->aType[].
85545 */
85546 if( pC->nHdrParsed<=p2 ){
85547 /* If there is more header available for parsing in the record, try
85548 ** to extract additional fields up through the p2+1-th field
85549 */
85550 if( pC->iHdrOffset<aOffset[0] ){
85551 /* Make sure zData points to enough of the record to cover the header. */
85552 if( pC->aRow==0 ){
85553 memset(&sMem, 0, sizeof(sMem));
85554 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, 0, aOffset[0], &sMem);
85555 if( rc!=SQLITE_OK ) goto abort_due_to_error;
85556 zData = (u8*)sMem.z;
85557 }else{
85558 zData = pC->aRow;
85559 }
85560
85561 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
85562 op_column_read_header:
85563 i = pC->nHdrParsed;
85564 offset64 = aOffset[i];
85565 zHdr = zData + pC->iHdrOffset;
85566 zEndHdr = zData + aOffset[0];
85567 testcase( zHdr>=zEndHdr );
85568 do{
85569 if( (t = zHdr[0])<0x80 ){
85570 zHdr++;
85571 offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
85572 }else{
85573 zHdr += sqlite3GetVarint32(zHdr, &t);
85574 offset64 += sqlite3VdbeSerialTypeLen(t);
85575 }
85576 pC->aType[i++] = t;
85577 aOffset[i] = (u32)(offset64 & 0xffffffff);
85578 }while( i<=p2 && zHdr<zEndHdr );
85579
85580 /* The record is corrupt if any of the following are true:
85581 ** (1) the bytes of the header extend past the declared header size
85582 ** (2) the entire header was used but not all data was used
85583 ** (3) the end of the data extends beyond the end of the record.
85584 */
85585 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
85586 || (offset64 > pC->payloadSize)
85587 ){
85588 if( aOffset[0]==0 ){
85589 i = 0;
85590 zHdr = zEndHdr;
85591 }else{
85592 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
85593 goto op_column_corrupt;
85594 }
85595 }
85596
85597 pC->nHdrParsed = i;
85598 pC->iHdrOffset = (u32)(zHdr - zData);
85599 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
85600 }else{
85601 t = 0;
85602 }
85603
85604 /* If after trying to extract new entries from the header, nHdrParsed is
85605 ** still not up to p2, that means that the record has fewer than p2
85606 ** columns. So the result will be either the default value or a NULL.
85607 */
85608 if( pC->nHdrParsed<=p2 ){
85609 if( pOp->p4type==P4_MEM ){
85610 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
85611 }else{
85612 sqlite3VdbeMemSetNull(pDest);
85613 }
85614 goto op_column_out;
85615 }
85616 }else{
85617 t = pC->aType[p2];
85618 }
85619
85620 /* Extract the content for the p2+1-th column. Control can only
85621 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
85622 ** all valid.
85623 */
85624 assert( p2<pC->nHdrParsed );
85625 assert( rc==SQLITE_OK );
85626 assert( sqlite3VdbeCheckMemInvariants(pDest) );
85627 if( VdbeMemDynamic(pDest) ){
85628 sqlite3VdbeMemSetNull(pDest);
85629 }
85630 assert( t==pC->aType[p2] );
85631 if( pC->szRow>=aOffset[p2+1] ){
85632 /* This is the common case where the desired content fits on the original
85633 ** page - where the content is not on an overflow page */
85634 zData = pC->aRow + aOffset[p2];
85635 if( t<12 ){
85636 sqlite3VdbeSerialGet(zData, t, pDest);
85637 }else{
85638 /* If the column value is a string, we need a persistent value, not
85639 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
85640 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
85641 */
85642 static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term };
85643 pDest->n = len = (t-12)/2;
85644 pDest->enc = encoding;
85645 if( pDest->szMalloc < len+2 ){
85646 pDest->flags = MEM_Null;
85647 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem;
85648 }else{
85649 pDest->z = pDest->zMalloc;
85650 }
85651 memcpy(pDest->z, zData, len);
85652 pDest->z[len] = 0;
85653 pDest->z[len+1] = 0;
85654 pDest->flags = aFlag[t&1];
85655 }
85656 }else{
85657 pDest->enc = encoding;
85658 /* This branch happens only when content is on overflow pages */
85659 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
85660 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
85661 || (len = sqlite3VdbeSerialTypeLen(t))==0
85662 ){
85663 /* Content is irrelevant for
85664 ** 1. the typeof() function,
85665 ** 2. the length(X) function if X is a blob, and
85666 ** 3. if the content length is zero.
85667 ** So we might as well use bogus content rather than reading
85668 ** content from disk.
85669 **
85670 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
85671 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
85672 ** read up to 16. So 16 bytes of bogus content is supplied.
85673 */
85674 static u8 aZero[16]; /* This is the bogus content */
85675 sqlite3VdbeSerialGet(aZero, t, pDest);
85676 }else{
85677 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
85678 if( rc!=SQLITE_OK ) goto abort_due_to_error;
85679 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
85680 pDest->flags &= ~MEM_Ephem;
85681 }
85682 }
85683
85684op_column_out:
85685 UPDATE_MAX_BLOBSIZE(pDest);
85686 REGISTER_TRACE(pOp->p3, pDest);
85687 break;
85688
85689op_column_corrupt:
85690 if( aOp[0].p3>0 ){
85691 pOp = &aOp[aOp[0].p3-1];
85692 break;
85693 }else{
85694 rc = SQLITE_CORRUPT_BKPT;
85695 goto abort_due_to_error;
85696 }
85697}
85698
85699/* Opcode: Affinity P1 P2 * P4 *
85700** Synopsis: affinity(r[P1@P2])
85701**
85702** Apply affinities to a range of P2 registers starting with P1.
85703**
85704** P4 is a string that is P2 characters long. The N-th character of the
85705** string indicates the column affinity that should be used for the N-th
85706** memory cell in the range.
85707*/
85708case OP_Affinity: {
85709 const char *zAffinity; /* The affinity to be applied */
85710
85711 zAffinity = pOp->p4.z;
85712 assert( zAffinity!=0 );
85713 assert( pOp->p2>0 );
85714 assert( zAffinity[pOp->p2]==0 );
85715 pIn1 = &aMem[pOp->p1];
85716 do{
85717 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
85718 assert( memIsValid(pIn1) );
85719 applyAffinity(pIn1, *(zAffinity++), encoding);
85720 pIn1++;
85721 }while( zAffinity[0] );
85722 break;
85723}
85724
85725/* Opcode: MakeRecord P1 P2 P3 P4 *
85726** Synopsis: r[P3]=mkrec(r[P1@P2])
85727**
85728** Convert P2 registers beginning with P1 into the [record format]
85729** use as a data record in a database table or as a key
85730** in an index. The OP_Column opcode can decode the record later.
85731**
85732** P4 may be a string that is P2 characters long. The N-th character of the
85733** string indicates the column affinity that should be used for the N-th
85734** field of the index key.
85735**
85736** The mapping from character to affinity is given by the SQLITE_AFF_
85737** macros defined in sqliteInt.h.
85738**
85739** If P4 is NULL then all index fields have the affinity BLOB.
85740*/
85741case OP_MakeRecord: {
85742 u8 *zNewRecord; /* A buffer to hold the data for the new record */
85743 Mem *pRec; /* The new record */
85744 u64 nData; /* Number of bytes of data space */
85745 int nHdr; /* Number of bytes of header space */
85746 i64 nByte; /* Data space required for this record */
85747 i64 nZero; /* Number of zero bytes at the end of the record */
85748 int nVarint; /* Number of bytes in a varint */
85749 u32 serial_type; /* Type field */
85750 Mem *pData0; /* First field to be combined into the record */
85751 Mem *pLast; /* Last field of the record */
85752 int nField; /* Number of fields in the record */
85753 char *zAffinity; /* The affinity string for the record */
85754 int file_format; /* File format to use for encoding */
85755 int i; /* Space used in zNewRecord[] header */
85756 int j; /* Space used in zNewRecord[] content */
85757 u32 len; /* Length of a field */
85758
85759 /* Assuming the record contains N fields, the record format looks
85760 ** like this:
85761 **
85762 ** ------------------------------------------------------------------------
85763 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
85764 ** ------------------------------------------------------------------------
85765 **
85766 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
85767 ** and so forth.
85768 **
85769 ** Each type field is a varint representing the serial type of the
85770 ** corresponding data element (see sqlite3VdbeSerialType()). The
85771 ** hdr-size field is also a varint which is the offset from the beginning
85772 ** of the record to data0.
85773 */
85774 nData = 0; /* Number of bytes of data space */
85775 nHdr = 0; /* Number of bytes of header space */
85776 nZero = 0; /* Number of zero bytes at the end of the record */
85777 nField = pOp->p1;
85778 zAffinity = pOp->p4.z;
85779 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
85780 pData0 = &aMem[nField];
85781 nField = pOp->p2;
85782 pLast = &pData0[nField-1];
85783 file_format = p->minWriteFileFormat;
85784
85785 /* Identify the output register */
85786 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
85787 pOut = &aMem[pOp->p3];
85788 memAboutToChange(p, pOut);
85789
85790 /* Apply the requested affinity to all inputs
85791 */
85792 assert( pData0<=pLast );
85793 if( zAffinity ){
85794 pRec = pData0;
85795 do{
85796 applyAffinity(pRec++, *(zAffinity++), encoding);
85797 assert( zAffinity[0]==0 || pRec<=pLast );
85798 }while( zAffinity[0] );
85799 }
85800
85801#ifdef SQLITE_ENABLE_NULL_TRIM
85802 /* NULLs can be safely trimmed from the end of the record, as long as
85803 ** as the schema format is 2 or more and none of the omitted columns
85804 ** have a non-NULL default value. Also, the record must be left with
85805 ** at least one field. If P5>0 then it will be one more than the
85806 ** index of the right-most column with a non-NULL default value */
85807 if( pOp->p5 ){
85808 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
85809 pLast--;
85810 nField--;
85811 }
85812 }
85813#endif
85814
85815 /* Loop through the elements that will make up the record to figure
85816 ** out how much space is required for the new record.
85817 */
85818 pRec = pLast;
85819 do{
85820 assert( memIsValid(pRec) );
85821 serial_type = sqlite3VdbeSerialType(pRec, file_format, &len);
85822 if( pRec->flags & MEM_Zero ){
85823 if( serial_type==0 ){
85824 /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
85825 ** table methods that never invoke sqlite3_result_xxxxx() while
85826 ** computing an unchanging column value in an UPDATE statement.
85827 ** Give such values a special internal-use-only serial-type of 10
85828 ** so that they can be passed through to xUpdate and have
85829 ** a true sqlite3_value_nochange(). */
85830 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
85831 serial_type = 10;
85832 }else if( nData ){
85833 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
85834 }else{
85835 nZero += pRec->u.nZero;
85836 len -= pRec->u.nZero;
85837 }
85838 }
85839 nData += len;
85840 testcase( serial_type==127 );
85841 testcase( serial_type==128 );
85842 nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
85843 pRec->uTemp = serial_type;
85844 if( pRec==pData0 ) break;
85845 pRec--;
85846 }while(1);
85847
85848 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
85849 ** which determines the total number of bytes in the header. The varint
85850 ** value is the size of the header in bytes including the size varint
85851 ** itself. */
85852 testcase( nHdr==126 );
85853 testcase( nHdr==127 );
85854 if( nHdr<=126 ){
85855 /* The common case */
85856 nHdr += 1;
85857 }else{
85858 /* Rare case of a really large header */
85859 nVarint = sqlite3VarintLen(nHdr);
85860 nHdr += nVarint;
85861 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
85862 }
85863 nByte = nHdr+nData;
85864
85865 /* Make sure the output register has a buffer large enough to store
85866 ** the new record. The output register (pOp->p3) is not allowed to
85867 ** be one of the input registers (because the following call to
85868 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
85869 */
85870 if( nByte+nZero<=pOut->szMalloc ){
85871 /* The output register is already large enough to hold the record.
85872 ** No error checks or buffer enlargement is required */
85873 pOut->z = pOut->zMalloc;
85874 }else{
85875 /* Need to make sure that the output is not too big and then enlarge
85876 ** the output register to hold the full result */
85877 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
85878 goto too_big;
85879 }
85880 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
85881 goto no_mem;
85882 }
85883 }
85884 zNewRecord = (u8 *)pOut->z;
85885
85886 /* Write the record */
85887 i = putVarint32(zNewRecord, nHdr);
85888 j = nHdr;
85889 assert( pData0<=pLast );
85890 pRec = pData0;
85891 do{
85892 serial_type = pRec->uTemp;
85893 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
85894 ** additional varints, one per column. */
85895 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
85896 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
85897 ** immediately follow the header. */
85898 j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */
85899 }while( (++pRec)<=pLast );
85900 assert( i==nHdr );
85901 assert( j==nByte );
85902
85903 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
85904 pOut->n = (int)nByte;
85905 pOut->flags = MEM_Blob;
85906 if( nZero ){
85907 pOut->u.nZero = nZero;
85908 pOut->flags |= MEM_Zero;
85909 }
85910 REGISTER_TRACE(pOp->p3, pOut);
85911 UPDATE_MAX_BLOBSIZE(pOut);
85912 break;
85913}
85914
85915/* Opcode: Count P1 P2 * * *
85916** Synopsis: r[P2]=count()
85917**
85918** Store the number of entries (an integer value) in the table or index
85919** opened by cursor P1 in register P2
85920*/
85921#ifndef SQLITE_OMIT_BTREECOUNT
85922case OP_Count: { /* out2 */
85923 i64 nEntry;
85924 BtCursor *pCrsr;
85925
85926 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
85927 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
85928 assert( pCrsr );
85929 nEntry = 0; /* Not needed. Only used to silence a warning. */
85930 rc = sqlite3BtreeCount(pCrsr, &nEntry);
85931 if( rc ) goto abort_due_to_error;
85932 pOut = out2Prerelease(p, pOp);
85933 pOut->u.i = nEntry;
85934 break;
85935}
85936#endif
85937
85938/* Opcode: Savepoint P1 * * P4 *
85939**
85940** Open, release or rollback the savepoint named by parameter P4, depending
85941** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
85942** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
85943*/
85944case OP_Savepoint: {
85945 int p1; /* Value of P1 operand */
85946 char *zName; /* Name of savepoint */
85947 int nName;
85948 Savepoint *pNew;
85949 Savepoint *pSavepoint;
85950 Savepoint *pTmp;
85951 int iSavepoint;
85952 int ii;
85953
85954 p1 = pOp->p1;
85955 zName = pOp->p4.z;
85956
85957 /* Assert that the p1 parameter is valid. Also that if there is no open
85958 ** transaction, then there cannot be any savepoints.
85959 */
85960 assert( db->pSavepoint==0 || db->autoCommit==0 );
85961 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
85962 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
85963 assert( checkSavepointCount(db) );
85964 assert( p->bIsReader );
85965
85966 if( p1==SAVEPOINT_BEGIN ){
85967 if( db->nVdbeWrite>0 ){
85968 /* A new savepoint cannot be created if there are active write
85969 ** statements (i.e. open read/write incremental blob handles).
85970 */
85971 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
85972 rc = SQLITE_BUSY;
85973 }else{
85974 nName = sqlite3Strlen30(zName);
85975
85976#ifndef SQLITE_OMIT_VIRTUALTABLE
85977 /* This call is Ok even if this savepoint is actually a transaction
85978 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
85979 ** If this is a transaction savepoint being opened, it is guaranteed
85980 ** that the db->aVTrans[] array is empty. */
85981 assert( db->autoCommit==0 || db->nVTrans==0 );
85982 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
85983 db->nStatement+db->nSavepoint);
85984 if( rc!=SQLITE_OK ) goto abort_due_to_error;
85985#endif
85986
85987 /* Create a new savepoint structure. */
85988 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
85989 if( pNew ){
85990 pNew->zName = (char *)&pNew[1];
85991 memcpy(pNew->zName, zName, nName+1);
85992
85993 /* If there is no open transaction, then mark this as a special
85994 ** "transaction savepoint". */
85995 if( db->autoCommit ){
85996 db->autoCommit = 0;
85997 db->isTransactionSavepoint = 1;
85998 }else{
85999 db->nSavepoint++;
86000 }
86001
86002 /* Link the new savepoint into the database handle's list. */
86003 pNew->pNext = db->pSavepoint;
86004 db->pSavepoint = pNew;
86005 pNew->nDeferredCons = db->nDeferredCons;
86006 pNew->nDeferredImmCons = db->nDeferredImmCons;
86007 }
86008 }
86009 }else{
86010 iSavepoint = 0;
86011
86012 /* Find the named savepoint. If there is no such savepoint, then an
86013 ** an error is returned to the user. */
86014 for(
86015 pSavepoint = db->pSavepoint;
86016 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
86017 pSavepoint = pSavepoint->pNext
86018 ){
86019 iSavepoint++;
86020 }
86021 if( !pSavepoint ){
86022 sqlite3VdbeError(p, "no such savepoint: %s", zName);
86023 rc = SQLITE_ERROR;
86024 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
86025 /* It is not possible to release (commit) a savepoint if there are
86026 ** active write statements.
86027 */
86028 sqlite3VdbeError(p, "cannot release savepoint - "
86029 "SQL statements in progress");
86030 rc = SQLITE_BUSY;
86031 }else{
86032
86033 /* Determine whether or not this is a transaction savepoint. If so,
86034 ** and this is a RELEASE command, then the current transaction
86035 ** is committed.
86036 */
86037 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
86038 if( isTransaction && p1==SAVEPOINT_RELEASE ){
86039 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
86040 goto vdbe_return;
86041 }
86042 db->autoCommit = 1;
86043 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
86044 p->pc = (int)(pOp - aOp);
86045 db->autoCommit = 0;
86046 p->rc = rc = SQLITE_BUSY;
86047 goto vdbe_return;
86048 }
86049 db->isTransactionSavepoint = 0;
86050 rc = p->rc;
86051 }else{
86052 int isSchemaChange;
86053 iSavepoint = db->nSavepoint - iSavepoint - 1;
86054 if( p1==SAVEPOINT_ROLLBACK ){
86055 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
86056 for(ii=0; ii<db->nDb; ii++){
86057 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
86058 SQLITE_ABORT_ROLLBACK,
86059 isSchemaChange==0);
86060 if( rc!=SQLITE_OK ) goto abort_due_to_error;
86061 }
86062 }else{
86063 isSchemaChange = 0;
86064 }
86065 for(ii=0; ii<db->nDb; ii++){
86066 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
86067 if( rc!=SQLITE_OK ){
86068 goto abort_due_to_error;
86069 }
86070 }
86071 if( isSchemaChange ){
86072 sqlite3ExpirePreparedStatements(db, 0);
86073 sqlite3ResetAllSchemasOfConnection(db);
86074 db->mDbFlags |= DBFLAG_SchemaChange;
86075 }
86076 }
86077
86078 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
86079 ** savepoints nested inside of the savepoint being operated on. */
86080 while( db->pSavepoint!=pSavepoint ){
86081 pTmp = db->pSavepoint;
86082 db->pSavepoint = pTmp->pNext;
86083 sqlite3DbFree(db, pTmp);
86084 db->nSavepoint--;
86085 }
86086
86087 /* If it is a RELEASE, then destroy the savepoint being operated on
86088 ** too. If it is a ROLLBACK TO, then set the number of deferred
86089 ** constraint violations present in the database to the value stored
86090 ** when the savepoint was created. */
86091 if( p1==SAVEPOINT_RELEASE ){
86092 assert( pSavepoint==db->pSavepoint );
86093 db->pSavepoint = pSavepoint->pNext;
86094 sqlite3DbFree(db, pSavepoint);
86095 if( !isTransaction ){
86096 db->nSavepoint--;
86097 }
86098 }else{
86099 db->nDeferredCons = pSavepoint->nDeferredCons;
86100 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
86101 }
86102
86103 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
86104 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
86105 if( rc!=SQLITE_OK ) goto abort_due_to_error;
86106 }
86107 }
86108 }
86109 if( rc ) goto abort_due_to_error;
86110
86111 break;
86112}
86113
86114/* Opcode: AutoCommit P1 P2 * * *
86115**
86116** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
86117** back any currently active btree transactions. If there are any active
86118** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
86119** there are active writing VMs or active VMs that use shared cache.
86120**
86121** This instruction causes the VM to halt.
86122*/
86123case OP_AutoCommit: {
86124 int desiredAutoCommit;
86125 int iRollback;
86126
86127 desiredAutoCommit = pOp->p1;
86128 iRollback = pOp->p2;
86129 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
86130 assert( desiredAutoCommit==1 || iRollback==0 );
86131 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
86132 assert( p->bIsReader );
86133
86134 if( desiredAutoCommit!=db->autoCommit ){
86135 if( iRollback ){
86136 assert( desiredAutoCommit==1 );
86137 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
86138 db->autoCommit = 1;
86139 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
86140 /* If this instruction implements a COMMIT and other VMs are writing
86141 ** return an error indicating that the other VMs must complete first.
86142 */
86143 sqlite3VdbeError(p, "cannot commit transaction - "
86144 "SQL statements in progress");
86145 rc = SQLITE_BUSY;
86146 goto abort_due_to_error;
86147 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
86148 goto vdbe_return;
86149 }else{
86150 db->autoCommit = (u8)desiredAutoCommit;
86151 }
86152 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
86153 p->pc = (int)(pOp - aOp);
86154 db->autoCommit = (u8)(1-desiredAutoCommit);
86155 p->rc = rc = SQLITE_BUSY;
86156 goto vdbe_return;
86157 }
86158 assert( db->nStatement==0 );
86159 sqlite3CloseSavepoints(db);
86160 if( p->rc==SQLITE_OK ){
86161 rc = SQLITE_DONE;
86162 }else{
86163 rc = SQLITE_ERROR;
86164 }
86165 goto vdbe_return;
86166 }else{
86167 sqlite3VdbeError(p,
86168 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
86169 (iRollback)?"cannot rollback - no transaction is active":
86170 "cannot commit - no transaction is active"));
86171
86172 rc = SQLITE_ERROR;
86173 goto abort_due_to_error;
86174 }
86175 break;
86176}
86177
86178/* Opcode: Transaction P1 P2 P3 P4 P5
86179**
86180** Begin a transaction on database P1 if a transaction is not already
86181** active.
86182** If P2 is non-zero, then a write-transaction is started, or if a
86183** read-transaction is already active, it is upgraded to a write-transaction.
86184** If P2 is zero, then a read-transaction is started.
86185**
86186** P1 is the index of the database file on which the transaction is
86187** started. Index 0 is the main database file and index 1 is the
86188** file used for temporary tables. Indices of 2 or more are used for
86189** attached databases.
86190**
86191** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
86192** true (this flag is set if the Vdbe may modify more than one row and may
86193** throw an ABORT exception), a statement transaction may also be opened.
86194** More specifically, a statement transaction is opened iff the database
86195** connection is currently not in autocommit mode, or if there are other
86196** active statements. A statement transaction allows the changes made by this
86197** VDBE to be rolled back after an error without having to roll back the
86198** entire transaction. If no error is encountered, the statement transaction
86199** will automatically commit when the VDBE halts.
86200**
86201** If P5!=0 then this opcode also checks the schema cookie against P3
86202** and the schema generation counter against P4.
86203** The cookie changes its value whenever the database schema changes.
86204** This operation is used to detect when that the cookie has changed
86205** and that the current process needs to reread the schema. If the schema
86206** cookie in P3 differs from the schema cookie in the database header or
86207** if the schema generation counter in P4 differs from the current
86208** generation counter, then an SQLITE_SCHEMA error is raised and execution
86209** halts. The sqlite3_step() wrapper function might then reprepare the
86210** statement and rerun it from the beginning.
86211*/
86212case OP_Transaction: {
86213 Btree *pBt;
86214 int iMeta = 0;
86215
86216 assert( p->bIsReader );
86217 assert( p->readOnly==0 || pOp->p2==0 );
86218 assert( pOp->p1>=0 && pOp->p1<db->nDb );
86219 assert( DbMaskTest(p->btreeMask, pOp->p1) );
86220 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
86221 rc = SQLITE_READONLY;
86222 goto abort_due_to_error;
86223 }
86224 pBt = db->aDb[pOp->p1].pBt;
86225
86226 if( pBt ){
86227 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
86228 testcase( rc==SQLITE_BUSY_SNAPSHOT );
86229 testcase( rc==SQLITE_BUSY_RECOVERY );
86230 if( rc!=SQLITE_OK ){
86231 if( (rc&0xff)==SQLITE_BUSY ){
86232 p->pc = (int)(pOp - aOp);
86233 p->rc = rc;
86234 goto vdbe_return;
86235 }
86236 goto abort_due_to_error;
86237 }
86238
86239 if( pOp->p2 && p->usesStmtJournal
86240 && (db->autoCommit==0 || db->nVdbeRead>1)
86241 ){
86242 assert( sqlite3BtreeIsInTrans(pBt) );
86243 if( p->iStatement==0 ){
86244 assert( db->nStatement>=0 && db->nSavepoint>=0 );
86245 db->nStatement++;
86246 p->iStatement = db->nSavepoint + db->nStatement;
86247 }
86248
86249 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
86250 if( rc==SQLITE_OK ){
86251 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
86252 }
86253
86254 /* Store the current value of the database handles deferred constraint
86255 ** counter. If the statement transaction needs to be rolled back,
86256 ** the value of this counter needs to be restored too. */
86257 p->nStmtDefCons = db->nDeferredCons;
86258 p->nStmtDefImmCons = db->nDeferredImmCons;
86259 }
86260 }
86261 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
86262 if( pOp->p5
86263 && (iMeta!=pOp->p3
86264 || db->aDb[pOp->p1].pSchema->iGeneration!=pOp->p4.i)
86265 ){
86266 /*
86267 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
86268 ** version is checked to ensure that the schema has not changed since the
86269 ** SQL statement was prepared.
86270 */
86271 sqlite3DbFree(db, p->zErrMsg);
86272 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
86273 /* If the schema-cookie from the database file matches the cookie
86274 ** stored with the in-memory representation of the schema, do
86275 ** not reload the schema from the database file.
86276 **
86277 ** If virtual-tables are in use, this is not just an optimization.
86278 ** Often, v-tables store their data in other SQLite tables, which
86279 ** are queried from within xNext() and other v-table methods using
86280 ** prepared queries. If such a query is out-of-date, we do not want to
86281 ** discard the database schema, as the user code implementing the
86282 ** v-table would have to be ready for the sqlite3_vtab structure itself
86283 ** to be invalidated whenever sqlite3_step() is called from within
86284 ** a v-table method.
86285 */
86286 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
86287 sqlite3ResetOneSchema(db, pOp->p1);
86288 }
86289 p->expired = 1;
86290 rc = SQLITE_SCHEMA;
86291 }
86292 if( rc ) goto abort_due_to_error;
86293 break;
86294}
86295
86296/* Opcode: ReadCookie P1 P2 P3 * *
86297**
86298** Read cookie number P3 from database P1 and write it into register P2.
86299** P3==1 is the schema version. P3==2 is the database format.
86300** P3==3 is the recommended pager cache size, and so forth. P1==0 is
86301** the main database file and P1==1 is the database file used to store
86302** temporary tables.
86303**
86304** There must be a read-lock on the database (either a transaction
86305** must be started or there must be an open cursor) before
86306** executing this instruction.
86307*/
86308case OP_ReadCookie: { /* out2 */
86309 int iMeta;
86310 int iDb;
86311 int iCookie;
86312
86313 assert( p->bIsReader );
86314 iDb = pOp->p1;
86315 iCookie = pOp->p3;
86316 assert( pOp->p3<SQLITE_N_BTREE_META );
86317 assert( iDb>=0 && iDb<db->nDb );
86318 assert( db->aDb[iDb].pBt!=0 );
86319 assert( DbMaskTest(p->btreeMask, iDb) );
86320
86321 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
86322 pOut = out2Prerelease(p, pOp);
86323 pOut->u.i = iMeta;
86324 break;
86325}
86326
86327/* Opcode: SetCookie P1 P2 P3 * *
86328**
86329** Write the integer value P3 into cookie number P2 of database P1.
86330** P2==1 is the schema version. P2==2 is the database format.
86331** P2==3 is the recommended pager cache
86332** size, and so forth. P1==0 is the main database file and P1==1 is the
86333** database file used to store temporary tables.
86334**
86335** A transaction must be started before executing this opcode.
86336*/
86337case OP_SetCookie: {
86338 Db *pDb;
86339
86340 sqlite3VdbeIncrWriteCounter(p, 0);
86341 assert( pOp->p2<SQLITE_N_BTREE_META );
86342 assert( pOp->p1>=0 && pOp->p1<db->nDb );
86343 assert( DbMaskTest(p->btreeMask, pOp->p1) );
86344 assert( p->readOnly==0 );
86345 pDb = &db->aDb[pOp->p1];
86346 assert( pDb->pBt!=0 );
86347 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
86348 /* See note about index shifting on OP_ReadCookie */
86349 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
86350 if( pOp->p2==BTREE_SCHEMA_VERSION ){
86351 /* When the schema cookie changes, record the new cookie internally */
86352 pDb->pSchema->schema_cookie = pOp->p3;
86353 db->mDbFlags |= DBFLAG_SchemaChange;
86354 }else if( pOp->p2==BTREE_FILE_FORMAT ){
86355 /* Record changes in the file format */
86356 pDb->pSchema->file_format = pOp->p3;
86357 }
86358 if( pOp->p1==1 ){
86359 /* Invalidate all prepared statements whenever the TEMP database
86360 ** schema is changed. Ticket #1644 */
86361 sqlite3ExpirePreparedStatements(db, 0);
86362 p->expired = 0;
86363 }
86364 if( rc ) goto abort_due_to_error;
86365 break;
86366}
86367
86368/* Opcode: OpenRead P1 P2 P3 P4 P5
86369** Synopsis: root=P2 iDb=P3
86370**
86371** Open a read-only cursor for the database table whose root page is
86372** P2 in a database file. The database file is determined by P3.
86373** P3==0 means the main database, P3==1 means the database used for
86374** temporary tables, and P3>1 means used the corresponding attached
86375** database. Give the new cursor an identifier of P1. The P1
86376** values need not be contiguous but all P1 values should be small integers.
86377** It is an error for P1 to be negative.
86378**
86379** Allowed P5 bits:
86380** <ul>
86381** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
86382** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
86383** of OP_SeekLE/OP_IdxGT)
86384** </ul>
86385**
86386** The P4 value may be either an integer (P4_INT32) or a pointer to
86387** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
86388** object, then table being opened must be an [index b-tree] where the
86389** KeyInfo object defines the content and collating
86390** sequence of that index b-tree. Otherwise, if P4 is an integer
86391** value, then the table being opened must be a [table b-tree] with a
86392** number of columns no less than the value of P4.
86393**
86394** See also: OpenWrite, ReopenIdx
86395*/
86396/* Opcode: ReopenIdx P1 P2 P3 P4 P5
86397** Synopsis: root=P2 iDb=P3
86398**
86399** The ReopenIdx opcode works like OP_OpenRead except that it first
86400** checks to see if the cursor on P1 is already open on the same
86401** b-tree and if it is this opcode becomes a no-op. In other words,
86402** if the cursor is already open, do not reopen it.
86403**
86404** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ
86405** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must
86406** be the same as every other ReopenIdx or OpenRead for the same cursor
86407** number.
86408**
86409** Allowed P5 bits:
86410** <ul>
86411** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
86412** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
86413** of OP_SeekLE/OP_IdxGT)
86414** </ul>
86415**
86416** See also: OP_OpenRead, OP_OpenWrite
86417*/
86418/* Opcode: OpenWrite P1 P2 P3 P4 P5
86419** Synopsis: root=P2 iDb=P3
86420**
86421** Open a read/write cursor named P1 on the table or index whose root
86422** page is P2 (or whose root page is held in register P2 if the
86423** OPFLAG_P2ISREG bit is set in P5 - see below).
86424**
86425** The P4 value may be either an integer (P4_INT32) or a pointer to
86426** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
86427** object, then table being opened must be an [index b-tree] where the
86428** KeyInfo object defines the content and collating
86429** sequence of that index b-tree. Otherwise, if P4 is an integer
86430** value, then the table being opened must be a [table b-tree] with a
86431** number of columns no less than the value of P4.
86432**
86433** Allowed P5 bits:
86434** <ul>
86435** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
86436** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
86437** of OP_SeekLE/OP_IdxGT)
86438** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
86439** and subsequently delete entries in an index btree. This is a
86440** hint to the storage engine that the storage engine is allowed to
86441** ignore. The hint is not used by the official SQLite b*tree storage
86442** engine, but is used by COMDB2.
86443** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2
86444** as the root page, not the value of P2 itself.
86445** </ul>
86446**
86447** This instruction works like OpenRead except that it opens the cursor
86448** in read/write mode.
86449**
86450** See also: OP_OpenRead, OP_ReopenIdx
86451*/
86452case OP_ReopenIdx: {
86453 int nField;
86454 KeyInfo *pKeyInfo;
86455 int p2;
86456 int iDb;
86457 int wrFlag;
86458 Btree *pX;
86459 VdbeCursor *pCur;
86460 Db *pDb;
86461
86462 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
86463 assert( pOp->p4type==P4_KEYINFO );
86464 pCur = p->apCsr[pOp->p1];
86465 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
86466 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
86467 goto open_cursor_set_hints;
86468 }
86469 /* If the cursor is not currently open or is open on a different
86470 ** index, then fall through into OP_OpenRead to force a reopen */
86471case OP_OpenRead:
86472case OP_OpenWrite:
86473
86474 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
86475 assert( p->bIsReader );
86476 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
86477 || p->readOnly==0 );
86478
86479 if( p->expired==1 ){
86480 rc = SQLITE_ABORT_ROLLBACK;
86481 goto abort_due_to_error;
86482 }
86483
86484 nField = 0;
86485 pKeyInfo = 0;
86486 p2 = pOp->p2;
86487 iDb = pOp->p3;
86488 assert( iDb>=0 && iDb<db->nDb );
86489 assert( DbMaskTest(p->btreeMask, iDb) );
86490 pDb = &db->aDb[iDb];
86491 pX = pDb->pBt;
86492 assert( pX!=0 );
86493 if( pOp->opcode==OP_OpenWrite ){
86494 assert( OPFLAG_FORDELETE==BTREE_FORDELETE );
86495 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
86496 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
86497 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
86498 p->minWriteFileFormat = pDb->pSchema->file_format;
86499 }
86500 }else{
86501 wrFlag = 0;
86502 }
86503 if( pOp->p5 & OPFLAG_P2ISREG ){
86504 assert( p2>0 );
86505 assert( p2<=(p->nMem+1 - p->nCursor) );
86506 assert( pOp->opcode==OP_OpenWrite );
86507 pIn2 = &aMem[p2];
86508 assert( memIsValid(pIn2) );
86509 assert( (pIn2->flags & MEM_Int)!=0 );
86510 sqlite3VdbeMemIntegerify(pIn2);
86511 p2 = (int)pIn2->u.i;
86512 /* The p2 value always comes from a prior OP_CreateBtree opcode and
86513 ** that opcode will always set the p2 value to 2 or more or else fail.
86514 ** If there were a failure, the prepared statement would have halted
86515 ** before reaching this instruction. */
86516 assert( p2>=2 );
86517 }
86518 if( pOp->p4type==P4_KEYINFO ){
86519 pKeyInfo = pOp->p4.pKeyInfo;
86520 assert( pKeyInfo->enc==ENC(db) );
86521 assert( pKeyInfo->db==db );
86522 nField = pKeyInfo->nAllField;
86523 }else if( pOp->p4type==P4_INT32 ){
86524 nField = pOp->p4.i;
86525 }
86526 assert( pOp->p1>=0 );
86527 assert( nField>=0 );
86528 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
86529 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
86530 if( pCur==0 ) goto no_mem;
86531 pCur->nullRow = 1;
86532 pCur->isOrdered = 1;
86533 pCur->pgnoRoot = p2;
86534#ifdef SQLITE_DEBUG
86535 pCur->wrFlag = wrFlag;
86536#endif
86537 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
86538 pCur->pKeyInfo = pKeyInfo;
86539 /* Set the VdbeCursor.isTable variable. Previous versions of
86540 ** SQLite used to check if the root-page flags were sane at this point
86541 ** and report database corruption if they were not, but this check has
86542 ** since moved into the btree layer. */
86543 pCur->isTable = pOp->p4type!=P4_KEYINFO;
86544
86545open_cursor_set_hints:
86546 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
86547 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
86548 testcase( pOp->p5 & OPFLAG_BULKCSR );
86549#ifdef SQLITE_ENABLE_CURSOR_HINTS
86550 testcase( pOp->p2 & OPFLAG_SEEKEQ );
86551#endif
86552 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
86553 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
86554 if( rc ) goto abort_due_to_error;
86555 break;
86556}
86557
86558/* Opcode: OpenDup P1 P2 * * *
86559**
86560** Open a new cursor P1 that points to the same ephemeral table as
86561** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral
86562** opcode. Only ephemeral cursors may be duplicated.
86563**
86564** Duplicate ephemeral cursors are used for self-joins of materialized views.
86565*/
86566case OP_OpenDup: {
86567 VdbeCursor *pOrig; /* The original cursor to be duplicated */
86568 VdbeCursor *pCx; /* The new cursor */
86569
86570 pOrig = p->apCsr[pOp->p2];
86571 assert( pOrig->pBtx!=0 ); /* Only ephemeral cursors can be duplicated */
86572
86573 pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE);
86574 if( pCx==0 ) goto no_mem;
86575 pCx->nullRow = 1;
86576 pCx->isEphemeral = 1;
86577 pCx->pKeyInfo = pOrig->pKeyInfo;
86578 pCx->isTable = pOrig->isTable;
86579 rc = sqlite3BtreeCursor(pOrig->pBtx, MASTER_ROOT, BTREE_WRCSR,
86580 pCx->pKeyInfo, pCx->uc.pCursor);
86581 /* The sqlite3BtreeCursor() routine can only fail for the first cursor
86582 ** opened for a database. Since there is already an open cursor when this
86583 ** opcode is run, the sqlite3BtreeCursor() cannot fail */
86584 assert( rc==SQLITE_OK );
86585 break;
86586}
86587
86588
86589/* Opcode: OpenEphemeral P1 P2 * P4 P5
86590** Synopsis: nColumn=P2
86591**
86592** Open a new cursor P1 to a transient table.
86593** The cursor is always opened read/write even if
86594** the main database is read-only. The ephemeral
86595** table is deleted automatically when the cursor is closed.
86596**
86597** P2 is the number of columns in the ephemeral table.
86598** The cursor points to a BTree table if P4==0 and to a BTree index
86599** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
86600** that defines the format of keys in the index.
86601**
86602** The P5 parameter can be a mask of the BTREE_* flags defined
86603** in btree.h. These flags control aspects of the operation of
86604** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
86605** added automatically.
86606*/
86607/* Opcode: OpenAutoindex P1 P2 * P4 *
86608** Synopsis: nColumn=P2
86609**
86610** This opcode works the same as OP_OpenEphemeral. It has a
86611** different name to distinguish its use. Tables created using
86612** by this opcode will be used for automatically created transient
86613** indices in joins.
86614*/
86615case OP_OpenAutoindex:
86616case OP_OpenEphemeral: {
86617 VdbeCursor *pCx;
86618 KeyInfo *pKeyInfo;
86619
86620 static const int vfsFlags =
86621 SQLITE_OPEN_READWRITE |
86622 SQLITE_OPEN_CREATE |
86623 SQLITE_OPEN_EXCLUSIVE |
86624 SQLITE_OPEN_DELETEONCLOSE |
86625 SQLITE_OPEN_TRANSIENT_DB;
86626 assert( pOp->p1>=0 );
86627 assert( pOp->p2>=0 );
86628 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
86629 if( pCx==0 ) goto no_mem;
86630 pCx->nullRow = 1;
86631 pCx->isEphemeral = 1;
86632 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx,
86633 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
86634 if( rc==SQLITE_OK ){
86635 rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1, 0);
86636 }
86637 if( rc==SQLITE_OK ){
86638 /* If a transient index is required, create it by calling
86639 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
86640 ** opening it. If a transient table is required, just use the
86641 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
86642 */
86643 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
86644 int pgno;
86645 assert( pOp->p4type==P4_KEYINFO );
86646 rc = sqlite3BtreeCreateTable(pCx->pBtx, &pgno, BTREE_BLOBKEY | pOp->p5);
86647 if( rc==SQLITE_OK ){
86648 assert( pgno==MASTER_ROOT+1 );
86649 assert( pKeyInfo->db==db );
86650 assert( pKeyInfo->enc==ENC(db) );
86651 rc = sqlite3BtreeCursor(pCx->pBtx, pgno, BTREE_WRCSR,
86652 pKeyInfo, pCx->uc.pCursor);
86653 }
86654 pCx->isTable = 0;
86655 }else{
86656 rc = sqlite3BtreeCursor(pCx->pBtx, MASTER_ROOT, BTREE_WRCSR,
86657 0, pCx->uc.pCursor);
86658 pCx->isTable = 1;
86659 }
86660 }
86661 if( rc ) goto abort_due_to_error;
86662 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
86663 break;
86664}
86665
86666/* Opcode: SorterOpen P1 P2 P3 P4 *
86667**
86668** This opcode works like OP_OpenEphemeral except that it opens
86669** a transient index that is specifically designed to sort large
86670** tables using an external merge-sort algorithm.
86671**
86672** If argument P3 is non-zero, then it indicates that the sorter may
86673** assume that a stable sort considering the first P3 fields of each
86674** key is sufficient to produce the required results.
86675*/
86676case OP_SorterOpen: {
86677 VdbeCursor *pCx;
86678
86679 assert( pOp->p1>=0 );
86680 assert( pOp->p2>=0 );
86681 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER);
86682 if( pCx==0 ) goto no_mem;
86683 pCx->pKeyInfo = pOp->p4.pKeyInfo;
86684 assert( pCx->pKeyInfo->db==db );
86685 assert( pCx->pKeyInfo->enc==ENC(db) );
86686 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
86687 if( rc ) goto abort_due_to_error;
86688 break;
86689}
86690
86691/* Opcode: SequenceTest P1 P2 * * *
86692** Synopsis: if( cursor[P1].ctr++ ) pc = P2
86693**
86694** P1 is a sorter cursor. If the sequence counter is currently zero, jump
86695** to P2. Regardless of whether or not the jump is taken, increment the
86696** the sequence value.
86697*/
86698case OP_SequenceTest: {
86699 VdbeCursor *pC;
86700 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
86701 pC = p->apCsr[pOp->p1];
86702 assert( isSorter(pC) );
86703 if( (pC->seqCount++)==0 ){
86704 goto jump_to_p2;
86705 }
86706 break;
86707}
86708
86709/* Opcode: OpenPseudo P1 P2 P3 * *
86710** Synopsis: P3 columns in r[P2]
86711**
86712** Open a new cursor that points to a fake table that contains a single
86713** row of data. The content of that one row is the content of memory
86714** register P2. In other words, cursor P1 becomes an alias for the
86715** MEM_Blob content contained in register P2.
86716**
86717** A pseudo-table created by this opcode is used to hold a single
86718** row output from the sorter so that the row can be decomposed into
86719** individual columns using the OP_Column opcode. The OP_Column opcode
86720** is the only cursor opcode that works with a pseudo-table.
86721**
86722** P3 is the number of fields in the records that will be stored by
86723** the pseudo-table.
86724*/
86725case OP_OpenPseudo: {
86726 VdbeCursor *pCx;
86727
86728 assert( pOp->p1>=0 );
86729 assert( pOp->p3>=0 );
86730 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO);
86731 if( pCx==0 ) goto no_mem;
86732 pCx->nullRow = 1;
86733 pCx->seekResult = pOp->p2;
86734 pCx->isTable = 1;
86735 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
86736 ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test
86737 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
86738 ** which is a performance optimization */
86739 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
86740 assert( pOp->p5==0 );
86741 break;
86742}
86743
86744/* Opcode: Close P1 * * * *
86745**
86746** Close a cursor previously opened as P1. If P1 is not
86747** currently open, this instruction is a no-op.
86748*/
86749case OP_Close: {
86750 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
86751 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
86752 p->apCsr[pOp->p1] = 0;
86753 break;
86754}
86755
86756#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
86757/* Opcode: ColumnsUsed P1 * * P4 *
86758**
86759** This opcode (which only exists if SQLite was compiled with
86760** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
86761** table or index for cursor P1 are used. P4 is a 64-bit integer
86762** (P4_INT64) in which the first 63 bits are one for each of the
86763** first 63 columns of the table or index that are actually used
86764** by the cursor. The high-order bit is set if any column after
86765** the 64th is used.
86766*/
86767case OP_ColumnsUsed: {
86768 VdbeCursor *pC;
86769 pC = p->apCsr[pOp->p1];
86770 assert( pC->eCurType==CURTYPE_BTREE );
86771 pC->maskUsed = *(u64*)pOp->p4.pI64;
86772 break;
86773}
86774#endif
86775
86776/* Opcode: SeekGE P1 P2 P3 P4 *
86777** Synopsis: key=r[P3@P4]
86778**
86779** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
86780** use the value in register P3 as the key. If cursor P1 refers
86781** to an SQL index, then P3 is the first in an array of P4 registers
86782** that are used as an unpacked index key.
86783**
86784** Reposition cursor P1 so that it points to the smallest entry that
86785** is greater than or equal to the key value. If there are no records
86786** greater than or equal to the key and P2 is not zero, then jump to P2.
86787**
86788** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
86789** opcode will always land on a record that equally equals the key, or
86790** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
86791** opcode must be followed by an IdxLE opcode with the same arguments.
86792** The IdxLE opcode will be skipped if this opcode succeeds, but the
86793** IdxLE opcode will be used on subsequent loop iterations.
86794**
86795** This opcode leaves the cursor configured to move in forward order,
86796** from the beginning toward the end. In other words, the cursor is
86797** configured to use Next, not Prev.
86798**
86799** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
86800*/
86801/* Opcode: SeekGT P1 P2 P3 P4 *
86802** Synopsis: key=r[P3@P4]
86803**
86804** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
86805** use the value in register P3 as a key. If cursor P1 refers
86806** to an SQL index, then P3 is the first in an array of P4 registers
86807** that are used as an unpacked index key.
86808**
86809** Reposition cursor P1 so that it points to the smallest entry that
86810** is greater than the key value. If there are no records greater than
86811** the key and P2 is not zero, then jump to P2.
86812**
86813** This opcode leaves the cursor configured to move in forward order,
86814** from the beginning toward the end. In other words, the cursor is
86815** configured to use Next, not Prev.
86816**
86817** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
86818*/
86819/* Opcode: SeekLT P1 P2 P3 P4 *
86820** Synopsis: key=r[P3@P4]
86821**
86822** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
86823** use the value in register P3 as a key. If cursor P1 refers
86824** to an SQL index, then P3 is the first in an array of P4 registers
86825** that are used as an unpacked index key.
86826**
86827** Reposition cursor P1 so that it points to the largest entry that
86828** is less than the key value. If there are no records less than
86829** the key and P2 is not zero, then jump to P2.
86830**
86831** This opcode leaves the cursor configured to move in reverse order,
86832** from the end toward the beginning. In other words, the cursor is
86833** configured to use Prev, not Next.
86834**
86835** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
86836*/
86837/* Opcode: SeekLE P1 P2 P3 P4 *
86838** Synopsis: key=r[P3@P4]
86839**
86840** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
86841** use the value in register P3 as a key. If cursor P1 refers
86842** to an SQL index, then P3 is the first in an array of P4 registers
86843** that are used as an unpacked index key.
86844**
86845** Reposition cursor P1 so that it points to the largest entry that
86846** is less than or equal to the key value. If there are no records
86847** less than or equal to the key and P2 is not zero, then jump to P2.
86848**
86849** This opcode leaves the cursor configured to move in reverse order,
86850** from the end toward the beginning. In other words, the cursor is
86851** configured to use Prev, not Next.
86852**
86853** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
86854** opcode will always land on a record that equally equals the key, or
86855** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
86856** opcode must be followed by an IdxGE opcode with the same arguments.
86857** The IdxGE opcode will be skipped if this opcode succeeds, but the
86858** IdxGE opcode will be used on subsequent loop iterations.
86859**
86860** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
86861*/
86862case OP_SeekLT: /* jump, in3, group */
86863case OP_SeekLE: /* jump, in3, group */
86864case OP_SeekGE: /* jump, in3, group */
86865case OP_SeekGT: { /* jump, in3, group */
86866 int res; /* Comparison result */
86867 int oc; /* Opcode */
86868 VdbeCursor *pC; /* The cursor to seek */
86869 UnpackedRecord r; /* The key to seek for */
86870 int nField; /* Number of columns or fields in the key */
86871 i64 iKey; /* The rowid we are to seek to */
86872 int eqOnly; /* Only interested in == results */
86873
86874 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
86875 assert( pOp->p2!=0 );
86876 pC = p->apCsr[pOp->p1];
86877 assert( pC!=0 );
86878 assert( pC->eCurType==CURTYPE_BTREE );
86879 assert( OP_SeekLE == OP_SeekLT+1 );
86880 assert( OP_SeekGE == OP_SeekLT+2 );
86881 assert( OP_SeekGT == OP_SeekLT+3 );
86882 assert( pC->isOrdered );
86883 assert( pC->uc.pCursor!=0 );
86884 oc = pOp->opcode;
86885 eqOnly = 0;
86886 pC->nullRow = 0;
86887#ifdef SQLITE_DEBUG
86888 pC->seekOp = pOp->opcode;
86889#endif
86890
86891 if( pC->isTable ){
86892 /* The BTREE_SEEK_EQ flag is only set on index cursors */
86893 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
86894 || CORRUPT_DB );
86895
86896 /* The input value in P3 might be of any type: integer, real, string,
86897 ** blob, or NULL. But it needs to be an integer before we can do
86898 ** the seek, so convert it. */
86899 pIn3 = &aMem[pOp->p3];
86900 if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
86901 applyNumericAffinity(pIn3, 0);
86902 }
86903 iKey = sqlite3VdbeIntValue(pIn3);
86904
86905 /* If the P3 value could not be converted into an integer without
86906 ** loss of information, then special processing is required... */
86907 if( (pIn3->flags & MEM_Int)==0 ){
86908 if( (pIn3->flags & MEM_Real)==0 ){
86909 /* If the P3 value cannot be converted into any kind of a number,
86910 ** then the seek is not possible, so jump to P2 */
86911 VdbeBranchTaken(1,2); goto jump_to_p2;
86912 break;
86913 }
86914
86915 /* If the approximation iKey is larger than the actual real search
86916 ** term, substitute >= for > and < for <=. e.g. if the search term
86917 ** is 4.9 and the integer approximation 5:
86918 **
86919 ** (x > 4.9) -> (x >= 5)
86920 ** (x <= 4.9) -> (x < 5)
86921 */
86922 if( pIn3->u.r<(double)iKey ){
86923 assert( OP_SeekGE==(OP_SeekGT-1) );
86924 assert( OP_SeekLT==(OP_SeekLE-1) );
86925 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
86926 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
86927 }
86928
86929 /* If the approximation iKey is smaller than the actual real search
86930 ** term, substitute <= for < and > for >=. */
86931 else if( pIn3->u.r>(double)iKey ){
86932 assert( OP_SeekLE==(OP_SeekLT+1) );
86933 assert( OP_SeekGT==(OP_SeekGE+1) );
86934 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
86935 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
86936 }
86937 }
86938 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
86939 pC->movetoTarget = iKey; /* Used by OP_Delete */
86940 if( rc!=SQLITE_OK ){
86941 goto abort_due_to_error;
86942 }
86943 }else{
86944 /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and
86945 ** OP_SeekLE opcodes are allowed, and these must be immediately followed
86946 ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key.
86947 */
86948 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
86949 eqOnly = 1;
86950 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
86951 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
86952 assert( pOp[1].p1==pOp[0].p1 );
86953 assert( pOp[1].p2==pOp[0].p2 );
86954 assert( pOp[1].p3==pOp[0].p3 );
86955 assert( pOp[1].p4.i==pOp[0].p4.i );
86956 }
86957
86958 nField = pOp->p4.i;
86959 assert( pOp->p4type==P4_INT32 );
86960 assert( nField>0 );
86961 r.pKeyInfo = pC->pKeyInfo;
86962 r.nField = (u16)nField;
86963
86964 /* The next line of code computes as follows, only faster:
86965 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
86966 ** r.default_rc = -1;
86967 ** }else{
86968 ** r.default_rc = +1;
86969 ** }
86970 */
86971 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
86972 assert( oc!=OP_SeekGT || r.default_rc==-1 );
86973 assert( oc!=OP_SeekLE || r.default_rc==-1 );
86974 assert( oc!=OP_SeekGE || r.default_rc==+1 );
86975 assert( oc!=OP_SeekLT || r.default_rc==+1 );
86976
86977 r.aMem = &aMem[pOp->p3];
86978#ifdef SQLITE_DEBUG
86979 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
86980#endif
86981 r.eqSeen = 0;
86982 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
86983 if( rc!=SQLITE_OK ){
86984 goto abort_due_to_error;
86985 }
86986 if( eqOnly && r.eqSeen==0 ){
86987 assert( res!=0 );
86988 goto seek_not_found;
86989 }
86990 }
86991 pC->deferredMoveto = 0;
86992 pC->cacheStatus = CACHE_STALE;
86993#ifdef SQLITE_TEST
86994 sqlite3_search_count++;
86995#endif
86996 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT );
86997 if( res<0 || (res==0 && oc==OP_SeekGT) ){
86998 res = 0;
86999 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
87000 if( rc!=SQLITE_OK ){
87001 if( rc==SQLITE_DONE ){
87002 rc = SQLITE_OK;
87003 res = 1;
87004 }else{
87005 goto abort_due_to_error;
87006 }
87007 }
87008 }else{
87009 res = 0;
87010 }
87011 }else{
87012 assert( oc==OP_SeekLT || oc==OP_SeekLE );
87013 if( res>0 || (res==0 && oc==OP_SeekLT) ){
87014 res = 0;
87015 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
87016 if( rc!=SQLITE_OK ){
87017 if( rc==SQLITE_DONE ){
87018 rc = SQLITE_OK;
87019 res = 1;
87020 }else{
87021 goto abort_due_to_error;
87022 }
87023 }
87024 }else{
87025 /* res might be negative because the table is empty. Check to
87026 ** see if this is the case.
87027 */
87028 res = sqlite3BtreeEof(pC->uc.pCursor);
87029 }
87030 }
87031seek_not_found:
87032 assert( pOp->p2>0 );
87033 VdbeBranchTaken(res!=0,2);
87034 if( res ){
87035 goto jump_to_p2;
87036 }else if( eqOnly ){
87037 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
87038 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
87039 }
87040 break;
87041}
87042
87043/* Opcode: SeekHit P1 P2 * * *
87044** Synopsis: seekHit=P2
87045**
87046** Set the seekHit flag on cursor P1 to the value in P2.
87047** The seekHit flag is used by the IfNoHope opcode.
87048**
87049** P1 must be a valid b-tree cursor. P2 must be a boolean value,
87050** either 0 or 1.
87051*/
87052case OP_SeekHit: {
87053 VdbeCursor *pC;
87054 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87055 pC = p->apCsr[pOp->p1];
87056 assert( pC!=0 );
87057 assert( pOp->p2==0 || pOp->p2==1 );
87058 pC->seekHit = pOp->p2 & 1;
87059 break;
87060}
87061
87062/* Opcode: Found P1 P2 P3 P4 *
87063** Synopsis: key=r[P3@P4]
87064**
87065** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
87066** P4>0 then register P3 is the first of P4 registers that form an unpacked
87067** record.
87068**
87069** Cursor P1 is on an index btree. If the record identified by P3 and P4
87070** is a prefix of any entry in P1 then a jump is made to P2 and
87071** P1 is left pointing at the matching entry.
87072**
87073** This operation leaves the cursor in a state where it can be
87074** advanced in the forward direction. The Next instruction will work,
87075** but not the Prev instruction.
87076**
87077** See also: NotFound, NoConflict, NotExists. SeekGe
87078*/
87079/* Opcode: NotFound P1 P2 P3 P4 *
87080** Synopsis: key=r[P3@P4]
87081**
87082** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
87083** P4>0 then register P3 is the first of P4 registers that form an unpacked
87084** record.
87085**
87086** Cursor P1 is on an index btree. If the record identified by P3 and P4
87087** is not the prefix of any entry in P1 then a jump is made to P2. If P1
87088** does contain an entry whose prefix matches the P3/P4 record then control
87089** falls through to the next instruction and P1 is left pointing at the
87090** matching entry.
87091**
87092** This operation leaves the cursor in a state where it cannot be
87093** advanced in either direction. In other words, the Next and Prev
87094** opcodes do not work after this operation.
87095**
87096** See also: Found, NotExists, NoConflict, IfNoHope
87097*/
87098/* Opcode: IfNoHope P1 P2 P3 P4 *
87099** Synopsis: key=r[P3@P4]
87100**
87101** Register P3 is the first of P4 registers that form an unpacked
87102** record.
87103**
87104** Cursor P1 is on an index btree. If the seekHit flag is set on P1, then
87105** this opcode is a no-op. But if the seekHit flag of P1 is clear, then
87106** check to see if there is any entry in P1 that matches the
87107** prefix identified by P3 and P4. If no entry matches the prefix,
87108** jump to P2. Otherwise fall through.
87109**
87110** This opcode behaves like OP_NotFound if the seekHit
87111** flag is clear and it behaves like OP_Noop if the seekHit flag is set.
87112**
87113** This opcode is used in IN clause processing for a multi-column key.
87114** If an IN clause is attached to an element of the key other than the
87115** left-most element, and if there are no matches on the most recent
87116** seek over the whole key, then it might be that one of the key element
87117** to the left is prohibiting a match, and hence there is "no hope" of
87118** any match regardless of how many IN clause elements are checked.
87119** In such a case, we abandon the IN clause search early, using this
87120** opcode. The opcode name comes from the fact that the
87121** jump is taken if there is "no hope" of achieving a match.
87122**
87123** See also: NotFound, SeekHit
87124*/
87125/* Opcode: NoConflict P1 P2 P3 P4 *
87126** Synopsis: key=r[P3@P4]
87127**
87128** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
87129** P4>0 then register P3 is the first of P4 registers that form an unpacked
87130** record.
87131**
87132** Cursor P1 is on an index btree. If the record identified by P3 and P4
87133** contains any NULL value, jump immediately to P2. If all terms of the
87134** record are not-NULL then a check is done to determine if any row in the
87135** P1 index btree has a matching key prefix. If there are no matches, jump
87136** immediately to P2. If there is a match, fall through and leave the P1
87137** cursor pointing to the matching row.
87138**
87139** This opcode is similar to OP_NotFound with the exceptions that the
87140** branch is always taken if any part of the search key input is NULL.
87141**
87142** This operation leaves the cursor in a state where it cannot be
87143** advanced in either direction. In other words, the Next and Prev
87144** opcodes do not work after this operation.
87145**
87146** See also: NotFound, Found, NotExists
87147*/
87148case OP_IfNoHope: { /* jump, in3 */
87149 VdbeCursor *pC;
87150 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87151 pC = p->apCsr[pOp->p1];
87152 assert( pC!=0 );
87153 if( pC->seekHit ) break;
87154 /* Fall through into OP_NotFound */
87155}
87156case OP_NoConflict: /* jump, in3 */
87157case OP_NotFound: /* jump, in3 */
87158case OP_Found: { /* jump, in3 */
87159 int alreadyExists;
87160 int takeJump;
87161 int ii;
87162 VdbeCursor *pC;
87163 int res;
87164 UnpackedRecord *pFree;
87165 UnpackedRecord *pIdxKey;
87166 UnpackedRecord r;
87167
87168#ifdef SQLITE_TEST
87169 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
87170#endif
87171
87172 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87173 assert( pOp->p4type==P4_INT32 );
87174 pC = p->apCsr[pOp->p1];
87175 assert( pC!=0 );
87176#ifdef SQLITE_DEBUG
87177 pC->seekOp = pOp->opcode;
87178#endif
87179 pIn3 = &aMem[pOp->p3];
87180 assert( pC->eCurType==CURTYPE_BTREE );
87181 assert( pC->uc.pCursor!=0 );
87182 assert( pC->isTable==0 );
87183 if( pOp->p4.i>0 ){
87184 r.pKeyInfo = pC->pKeyInfo;
87185 r.nField = (u16)pOp->p4.i;
87186 r.aMem = pIn3;
87187#ifdef SQLITE_DEBUG
87188 for(ii=0; ii<r.nField; ii++){
87189 assert( memIsValid(&r.aMem[ii]) );
87190 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 );
87191 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
87192 }
87193#endif
87194 pIdxKey = &r;
87195 pFree = 0;
87196 }else{
87197 assert( pIn3->flags & MEM_Blob );
87198 rc = ExpandBlob(pIn3);
87199 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
87200 if( rc ) goto no_mem;
87201 pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
87202 if( pIdxKey==0 ) goto no_mem;
87203 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
87204 }
87205 pIdxKey->default_rc = 0;
87206 takeJump = 0;
87207 if( pOp->opcode==OP_NoConflict ){
87208 /* For the OP_NoConflict opcode, take the jump if any of the
87209 ** input fields are NULL, since any key with a NULL will not
87210 ** conflict */
87211 for(ii=0; ii<pIdxKey->nField; ii++){
87212 if( pIdxKey->aMem[ii].flags & MEM_Null ){
87213 takeJump = 1;
87214 break;
87215 }
87216 }
87217 }
87218 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
87219 if( pFree ) sqlite3DbFreeNN(db, pFree);
87220 if( rc!=SQLITE_OK ){
87221 goto abort_due_to_error;
87222 }
87223 pC->seekResult = res;
87224 alreadyExists = (res==0);
87225 pC->nullRow = 1-alreadyExists;
87226 pC->deferredMoveto = 0;
87227 pC->cacheStatus = CACHE_STALE;
87228 if( pOp->opcode==OP_Found ){
87229 VdbeBranchTaken(alreadyExists!=0,2);
87230 if( alreadyExists ) goto jump_to_p2;
87231 }else{
87232 VdbeBranchTaken(takeJump||alreadyExists==0,2);
87233 if( takeJump || !alreadyExists ) goto jump_to_p2;
87234 }
87235 break;
87236}
87237
87238/* Opcode: SeekRowid P1 P2 P3 * *
87239** Synopsis: intkey=r[P3]
87240**
87241** P1 is the index of a cursor open on an SQL table btree (with integer
87242** keys). If register P3 does not contain an integer or if P1 does not
87243** contain a record with rowid P3 then jump immediately to P2.
87244** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
87245** a record with rowid P3 then
87246** leave the cursor pointing at that record and fall through to the next
87247** instruction.
87248**
87249** The OP_NotExists opcode performs the same operation, but with OP_NotExists
87250** the P3 register must be guaranteed to contain an integer value. With this
87251** opcode, register P3 might not contain an integer.
87252**
87253** The OP_NotFound opcode performs the same operation on index btrees
87254** (with arbitrary multi-value keys).
87255**
87256** This opcode leaves the cursor in a state where it cannot be advanced
87257** in either direction. In other words, the Next and Prev opcodes will
87258** not work following this opcode.
87259**
87260** See also: Found, NotFound, NoConflict, SeekRowid
87261*/
87262/* Opcode: NotExists P1 P2 P3 * *
87263** Synopsis: intkey=r[P3]
87264**
87265** P1 is the index of a cursor open on an SQL table btree (with integer
87266** keys). P3 is an integer rowid. If P1 does not contain a record with
87267** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
87268** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
87269** leave the cursor pointing at that record and fall through to the next
87270** instruction.
87271**
87272** The OP_SeekRowid opcode performs the same operation but also allows the
87273** P3 register to contain a non-integer value, in which case the jump is
87274** always taken. This opcode requires that P3 always contain an integer.
87275**
87276** The OP_NotFound opcode performs the same operation on index btrees
87277** (with arbitrary multi-value keys).
87278**
87279** This opcode leaves the cursor in a state where it cannot be advanced
87280** in either direction. In other words, the Next and Prev opcodes will
87281** not work following this opcode.
87282**
87283** See also: Found, NotFound, NoConflict, SeekRowid
87284*/
87285case OP_SeekRowid: { /* jump, in3 */
87286 VdbeCursor *pC;
87287 BtCursor *pCrsr;
87288 int res;
87289 u64 iKey;
87290
87291 pIn3 = &aMem[pOp->p3];
87292 if( (pIn3->flags & MEM_Int)==0 ){
87293 /* Make sure pIn3->u.i contains a valid integer representation of
87294 ** the key value, but do not change the datatype of the register, as
87295 ** other parts of the perpared statement might be depending on the
87296 ** current datatype. */
87297 u16 origFlags = pIn3->flags;
87298 int isNotInt;
87299 applyAffinity(pIn3, SQLITE_AFF_NUMERIC, encoding);
87300 isNotInt = (pIn3->flags & MEM_Int)==0;
87301 pIn3->flags = origFlags;
87302 if( isNotInt ) goto jump_to_p2;
87303 }
87304 /* Fall through into OP_NotExists */
87305case OP_NotExists: /* jump, in3 */
87306 pIn3 = &aMem[pOp->p3];
87307 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid );
87308 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87309 pC = p->apCsr[pOp->p1];
87310 assert( pC!=0 );
87311#ifdef SQLITE_DEBUG
87312 pC->seekOp = OP_SeekRowid;
87313#endif
87314 assert( pC->isTable );
87315 assert( pC->eCurType==CURTYPE_BTREE );
87316 pCrsr = pC->uc.pCursor;
87317 assert( pCrsr!=0 );
87318 res = 0;
87319 iKey = pIn3->u.i;
87320 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
87321 assert( rc==SQLITE_OK || res==0 );
87322 pC->movetoTarget = iKey; /* Used by OP_Delete */
87323 pC->nullRow = 0;
87324 pC->cacheStatus = CACHE_STALE;
87325 pC->deferredMoveto = 0;
87326 VdbeBranchTaken(res!=0,2);
87327 pC->seekResult = res;
87328 if( res!=0 ){
87329 assert( rc==SQLITE_OK );
87330 if( pOp->p2==0 ){
87331 rc = SQLITE_CORRUPT_BKPT;
87332 }else{
87333 goto jump_to_p2;
87334 }
87335 }
87336 if( rc ) goto abort_due_to_error;
87337 break;
87338}
87339
87340/* Opcode: Sequence P1 P2 * * *
87341** Synopsis: r[P2]=cursor[P1].ctr++
87342**
87343** Find the next available sequence number for cursor P1.
87344** Write the sequence number into register P2.
87345** The sequence number on the cursor is incremented after this
87346** instruction.
87347*/
87348case OP_Sequence: { /* out2 */
87349 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87350 assert( p->apCsr[pOp->p1]!=0 );
87351 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
87352 pOut = out2Prerelease(p, pOp);
87353 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
87354 break;
87355}
87356
87357
87358/* Opcode: NewRowid P1 P2 P3 * *
87359** Synopsis: r[P2]=rowid
87360**
87361** Get a new integer record number (a.k.a "rowid") used as the key to a table.
87362** The record number is not previously used as a key in the database
87363** table that cursor P1 points to. The new record number is written
87364** written to register P2.
87365**
87366** If P3>0 then P3 is a register in the root frame of this VDBE that holds
87367** the largest previously generated record number. No new record numbers are
87368** allowed to be less than this value. When this value reaches its maximum,
87369** an SQLITE_FULL error is generated. The P3 register is updated with the '
87370** generated record number. This P3 mechanism is used to help implement the
87371** AUTOINCREMENT feature.
87372*/
87373case OP_NewRowid: { /* out2 */
87374 i64 v; /* The new rowid */
87375 VdbeCursor *pC; /* Cursor of table to get the new rowid */
87376 int res; /* Result of an sqlite3BtreeLast() */
87377 int cnt; /* Counter to limit the number of searches */
87378 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
87379 VdbeFrame *pFrame; /* Root frame of VDBE */
87380
87381 v = 0;
87382 res = 0;
87383 pOut = out2Prerelease(p, pOp);
87384 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87385 pC = p->apCsr[pOp->p1];
87386 assert( pC!=0 );
87387 assert( pC->isTable );
87388 assert( pC->eCurType==CURTYPE_BTREE );
87389 assert( pC->uc.pCursor!=0 );
87390 {
87391 /* The next rowid or record number (different terms for the same
87392 ** thing) is obtained in a two-step algorithm.
87393 **
87394 ** First we attempt to find the largest existing rowid and add one
87395 ** to that. But if the largest existing rowid is already the maximum
87396 ** positive integer, we have to fall through to the second
87397 ** probabilistic algorithm
87398 **
87399 ** The second algorithm is to select a rowid at random and see if
87400 ** it already exists in the table. If it does not exist, we have
87401 ** succeeded. If the random rowid does exist, we select a new one
87402 ** and try again, up to 100 times.
87403 */
87404 assert( pC->isTable );
87405
87406#ifdef SQLITE_32BIT_ROWID
87407# define MAX_ROWID 0x7fffffff
87408#else
87409 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
87410 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
87411 ** to provide the constant while making all compilers happy.
87412 */
87413# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
87414#endif
87415
87416 if( !pC->useRandomRowid ){
87417 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
87418 if( rc!=SQLITE_OK ){
87419 goto abort_due_to_error;
87420 }
87421 if( res ){
87422 v = 1; /* IMP: R-61914-48074 */
87423 }else{
87424 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
87425 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
87426 if( v>=MAX_ROWID ){
87427 pC->useRandomRowid = 1;
87428 }else{
87429 v++; /* IMP: R-29538-34987 */
87430 }
87431 }
87432 }
87433
87434#ifndef SQLITE_OMIT_AUTOINCREMENT
87435 if( pOp->p3 ){
87436 /* Assert that P3 is a valid memory cell. */
87437 assert( pOp->p3>0 );
87438 if( p->pFrame ){
87439 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
87440 /* Assert that P3 is a valid memory cell. */
87441 assert( pOp->p3<=pFrame->nMem );
87442 pMem = &pFrame->aMem[pOp->p3];
87443 }else{
87444 /* Assert that P3 is a valid memory cell. */
87445 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
87446 pMem = &aMem[pOp->p3];
87447 memAboutToChange(p, pMem);
87448 }
87449 assert( memIsValid(pMem) );
87450
87451 REGISTER_TRACE(pOp->p3, pMem);
87452 sqlite3VdbeMemIntegerify(pMem);
87453 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
87454 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
87455 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
87456 goto abort_due_to_error;
87457 }
87458 if( v<pMem->u.i+1 ){
87459 v = pMem->u.i + 1;
87460 }
87461 pMem->u.i = v;
87462 }
87463#endif
87464 if( pC->useRandomRowid ){
87465 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
87466 ** largest possible integer (9223372036854775807) then the database
87467 ** engine starts picking positive candidate ROWIDs at random until
87468 ** it finds one that is not previously used. */
87469 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
87470 ** an AUTOINCREMENT table. */
87471 cnt = 0;
87472 do{
87473 sqlite3_randomness(sizeof(v), &v);
87474 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */
87475 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v,
87476 0, &res))==SQLITE_OK)
87477 && (res==0)
87478 && (++cnt<100));
87479 if( rc ) goto abort_due_to_error;
87480 if( res==0 ){
87481 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
87482 goto abort_due_to_error;
87483 }
87484 assert( v>0 ); /* EV: R-40812-03570 */
87485 }
87486 pC->deferredMoveto = 0;
87487 pC->cacheStatus = CACHE_STALE;
87488 }
87489 pOut->u.i = v;
87490 break;
87491}
87492
87493/* Opcode: Insert P1 P2 P3 P4 P5
87494** Synopsis: intkey=r[P3] data=r[P2]
87495**
87496** Write an entry into the table of cursor P1. A new entry is
87497** created if it doesn't already exist or the data for an existing
87498** entry is overwritten. The data is the value MEM_Blob stored in register
87499** number P2. The key is stored in register P3. The key must
87500** be a MEM_Int.
87501**
87502** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
87503** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
87504** then rowid is stored for subsequent return by the
87505** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
87506**
87507** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
87508** run faster by avoiding an unnecessary seek on cursor P1. However,
87509** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
87510** seeks on the cursor or if the most recent seek used a key equal to P3.
87511**
87512** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
87513** UPDATE operation. Otherwise (if the flag is clear) then this opcode
87514** is part of an INSERT operation. The difference is only important to
87515** the update hook.
87516**
87517** Parameter P4 may point to a Table structure, or may be NULL. If it is
87518** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
87519** following a successful insert.
87520**
87521** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
87522** allocated, then ownership of P2 is transferred to the pseudo-cursor
87523** and register P2 becomes ephemeral. If the cursor is changed, the
87524** value of register P2 will then change. Make sure this does not
87525** cause any problems.)
87526**
87527** This instruction only works on tables. The equivalent instruction
87528** for indices is OP_IdxInsert.
87529*/
87530/* Opcode: InsertInt P1 P2 P3 P4 P5
87531** Synopsis: intkey=P3 data=r[P2]
87532**
87533** This works exactly like OP_Insert except that the key is the
87534** integer value P3, not the value of the integer stored in register P3.
87535*/
87536case OP_Insert:
87537case OP_InsertInt: {
87538 Mem *pData; /* MEM cell holding data for the record to be inserted */
87539 Mem *pKey; /* MEM cell holding key for the record */
87540 VdbeCursor *pC; /* Cursor to table into which insert is written */
87541 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
87542 const char *zDb; /* database name - used by the update hook */
87543 Table *pTab; /* Table structure - used by update and pre-update hooks */
87544 BtreePayload x; /* Payload to be inserted */
87545
87546 pData = &aMem[pOp->p2];
87547 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87548 assert( memIsValid(pData) );
87549 pC = p->apCsr[pOp->p1];
87550 assert( pC!=0 );
87551 assert( pC->eCurType==CURTYPE_BTREE );
87552 assert( pC->uc.pCursor!=0 );
87553 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
87554 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
87555 REGISTER_TRACE(pOp->p2, pData);
87556 sqlite3VdbeIncrWriteCounter(p, pC);
87557
87558 if( pOp->opcode==OP_Insert ){
87559 pKey = &aMem[pOp->p3];
87560 assert( pKey->flags & MEM_Int );
87561 assert( memIsValid(pKey) );
87562 REGISTER_TRACE(pOp->p3, pKey);
87563 x.nKey = pKey->u.i;
87564 }else{
87565 assert( pOp->opcode==OP_InsertInt );
87566 x.nKey = pOp->p3;
87567 }
87568
87569 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
87570 assert( pC->iDb>=0 );
87571 zDb = db->aDb[pC->iDb].zDbSName;
87572 pTab = pOp->p4.pTab;
87573 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
87574 }else{
87575 pTab = 0;
87576 zDb = 0; /* Not needed. Silence a compiler warning. */
87577 }
87578
87579#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
87580 /* Invoke the pre-update hook, if any */
87581 if( pTab ){
87582 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
87583 sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey,pOp->p2);
87584 }
87585 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
87586 /* Prevent post-update hook from running in cases when it should not */
87587 pTab = 0;
87588 }
87589 }
87590 if( pOp->p5 & OPFLAG_ISNOOP ) break;
87591#endif
87592
87593 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
87594 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
87595 assert( pData->flags & (MEM_Blob|MEM_Str) );
87596 x.pData = pData->z;
87597 x.nData = pData->n;
87598 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
87599 if( pData->flags & MEM_Zero ){
87600 x.nZero = pData->u.nZero;
87601 }else{
87602 x.nZero = 0;
87603 }
87604 x.pKey = 0;
87605 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
87606 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult
87607 );
87608 pC->deferredMoveto = 0;
87609 pC->cacheStatus = CACHE_STALE;
87610
87611 /* Invoke the update-hook if required. */
87612 if( rc ) goto abort_due_to_error;
87613 if( pTab ){
87614 assert( db->xUpdateCallback!=0 );
87615 assert( pTab->aCol!=0 );
87616 db->xUpdateCallback(db->pUpdateArg,
87617 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
87618 zDb, pTab->zName, x.nKey);
87619 }
87620 break;
87621}
87622
87623/* Opcode: Delete P1 P2 P3 P4 P5
87624**
87625** Delete the record at which the P1 cursor is currently pointing.
87626**
87627** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
87628** the cursor will be left pointing at either the next or the previous
87629** record in the table. If it is left pointing at the next record, then
87630** the next Next instruction will be a no-op. As a result, in this case
87631** it is ok to delete a record from within a Next loop. If
87632** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
87633** left in an undefined state.
87634**
87635** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
87636** delete one of several associated with deleting a table row and all its
87637** associated index entries. Exactly one of those deletes is the "primary"
87638** delete. The others are all on OPFLAG_FORDELETE cursors or else are
87639** marked with the AUXDELETE flag.
87640**
87641** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
87642** change count is incremented (otherwise not).
87643**
87644** P1 must not be pseudo-table. It has to be a real table with
87645** multiple rows.
87646**
87647** If P4 is not NULL then it points to a Table object. In this case either
87648** the update or pre-update hook, or both, may be invoked. The P1 cursor must
87649** have been positioned using OP_NotFound prior to invoking this opcode in
87650** this case. Specifically, if one is configured, the pre-update hook is
87651** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
87652** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
87653**
87654** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
87655** of the memory cell that contains the value that the rowid of the row will
87656** be set to by the update.
87657*/
87658case OP_Delete: {
87659 VdbeCursor *pC;
87660 const char *zDb;
87661 Table *pTab;
87662 int opflags;
87663
87664 opflags = pOp->p2;
87665 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87666 pC = p->apCsr[pOp->p1];
87667 assert( pC!=0 );
87668 assert( pC->eCurType==CURTYPE_BTREE );
87669 assert( pC->uc.pCursor!=0 );
87670 assert( pC->deferredMoveto==0 );
87671 sqlite3VdbeIncrWriteCounter(p, pC);
87672
87673#ifdef SQLITE_DEBUG
87674 if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){
87675 /* If p5 is zero, the seek operation that positioned the cursor prior to
87676 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
87677 ** the row that is being deleted */
87678 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
87679 assert( pC->movetoTarget==iKey );
87680 }
87681#endif
87682
87683 /* If the update-hook or pre-update-hook will be invoked, set zDb to
87684 ** the name of the db to pass as to it. Also set local pTab to a copy
87685 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
87686 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
87687 ** VdbeCursor.movetoTarget to the current rowid. */
87688 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
87689 assert( pC->iDb>=0 );
87690 assert( pOp->p4.pTab!=0 );
87691 zDb = db->aDb[pC->iDb].zDbSName;
87692 pTab = pOp->p4.pTab;
87693 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
87694 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
87695 }
87696 }else{
87697 zDb = 0; /* Not needed. Silence a compiler warning. */
87698 pTab = 0; /* Not needed. Silence a compiler warning. */
87699 }
87700
87701#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
87702 /* Invoke the pre-update-hook if required. */
87703 if( db->xPreUpdateCallback && pOp->p4.pTab ){
87704 assert( !(opflags & OPFLAG_ISUPDATE)
87705 || HasRowid(pTab)==0
87706 || (aMem[pOp->p3].flags & MEM_Int)
87707 );
87708 sqlite3VdbePreUpdateHook(p, pC,
87709 (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE,
87710 zDb, pTab, pC->movetoTarget,
87711 pOp->p3
87712 );
87713 }
87714 if( opflags & OPFLAG_ISNOOP ) break;
87715#endif
87716
87717 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
87718 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
87719 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION );
87720 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE );
87721
87722#ifdef SQLITE_DEBUG
87723 if( p->pFrame==0 ){
87724 if( pC->isEphemeral==0
87725 && (pOp->p5 & OPFLAG_AUXDELETE)==0
87726 && (pC->wrFlag & OPFLAG_FORDELETE)==0
87727 ){
87728 nExtraDelete++;
87729 }
87730 if( pOp->p2 & OPFLAG_NCHANGE ){
87731 nExtraDelete--;
87732 }
87733 }
87734#endif
87735
87736 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
87737 pC->cacheStatus = CACHE_STALE;
87738 pC->seekResult = 0;
87739 if( rc ) goto abort_due_to_error;
87740
87741 /* Invoke the update-hook if required. */
87742 if( opflags & OPFLAG_NCHANGE ){
87743 p->nChange++;
87744 if( db->xUpdateCallback && HasRowid(pTab) ){
87745 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
87746 pC->movetoTarget);
87747 assert( pC->iDb>=0 );
87748 }
87749 }
87750
87751 break;
87752}
87753/* Opcode: ResetCount * * * * *
87754**
87755** The value of the change counter is copied to the database handle
87756** change counter (returned by subsequent calls to sqlite3_changes()).
87757** Then the VMs internal change counter resets to 0.
87758** This is used by trigger programs.
87759*/
87760case OP_ResetCount: {
87761 sqlite3VdbeSetChanges(db, p->nChange);
87762 p->nChange = 0;
87763 break;
87764}
87765
87766/* Opcode: SorterCompare P1 P2 P3 P4
87767** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
87768**
87769** P1 is a sorter cursor. This instruction compares a prefix of the
87770** record blob in register P3 against a prefix of the entry that
87771** the sorter cursor currently points to. Only the first P4 fields
87772** of r[P3] and the sorter record are compared.
87773**
87774** If either P3 or the sorter contains a NULL in one of their significant
87775** fields (not counting the P4 fields at the end which are ignored) then
87776** the comparison is assumed to be equal.
87777**
87778** Fall through to next instruction if the two records compare equal to
87779** each other. Jump to P2 if they are different.
87780*/
87781case OP_SorterCompare: {
87782 VdbeCursor *pC;
87783 int res;
87784 int nKeyCol;
87785
87786 pC = p->apCsr[pOp->p1];
87787 assert( isSorter(pC) );
87788 assert( pOp->p4type==P4_INT32 );
87789 pIn3 = &aMem[pOp->p3];
87790 nKeyCol = pOp->p4.i;
87791 res = 0;
87792 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
87793 VdbeBranchTaken(res!=0,2);
87794 if( rc ) goto abort_due_to_error;
87795 if( res ) goto jump_to_p2;
87796 break;
87797};
87798
87799/* Opcode: SorterData P1 P2 P3 * *
87800** Synopsis: r[P2]=data
87801**
87802** Write into register P2 the current sorter data for sorter cursor P1.
87803** Then clear the column header cache on cursor P3.
87804**
87805** This opcode is normally use to move a record out of the sorter and into
87806** a register that is the source for a pseudo-table cursor created using
87807** OpenPseudo. That pseudo-table cursor is the one that is identified by
87808** parameter P3. Clearing the P3 column cache as part of this opcode saves
87809** us from having to issue a separate NullRow instruction to clear that cache.
87810*/
87811case OP_SorterData: {
87812 VdbeCursor *pC;
87813
87814 pOut = &aMem[pOp->p2];
87815 pC = p->apCsr[pOp->p1];
87816 assert( isSorter(pC) );
87817 rc = sqlite3VdbeSorterRowkey(pC, pOut);
87818 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
87819 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87820 if( rc ) goto abort_due_to_error;
87821 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
87822 break;
87823}
87824
87825/* Opcode: RowData P1 P2 P3 * *
87826** Synopsis: r[P2]=data
87827**
87828** Write into register P2 the complete row content for the row at
87829** which cursor P1 is currently pointing.
87830** There is no interpretation of the data.
87831** It is just copied onto the P2 register exactly as
87832** it is found in the database file.
87833**
87834** If cursor P1 is an index, then the content is the key of the row.
87835** If cursor P2 is a table, then the content extracted is the data.
87836**
87837** If the P1 cursor must be pointing to a valid row (not a NULL row)
87838** of a real table, not a pseudo-table.
87839**
87840** If P3!=0 then this opcode is allowed to make an ephemeral pointer
87841** into the database page. That means that the content of the output
87842** register will be invalidated as soon as the cursor moves - including
87843** moves caused by other cursors that "save" the current cursors
87844** position in order that they can write to the same table. If P3==0
87845** then a copy of the data is made into memory. P3!=0 is faster, but
87846** P3==0 is safer.
87847**
87848** If P3!=0 then the content of the P2 register is unsuitable for use
87849** in OP_Result and any OP_Result will invalidate the P2 register content.
87850** The P2 register content is invalidated by opcodes like OP_Function or
87851** by any use of another cursor pointing to the same table.
87852*/
87853case OP_RowData: {
87854 VdbeCursor *pC;
87855 BtCursor *pCrsr;
87856 u32 n;
87857
87858 pOut = out2Prerelease(p, pOp);
87859
87860 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87861 pC = p->apCsr[pOp->p1];
87862 assert( pC!=0 );
87863 assert( pC->eCurType==CURTYPE_BTREE );
87864 assert( isSorter(pC)==0 );
87865 assert( pC->nullRow==0 );
87866 assert( pC->uc.pCursor!=0 );
87867 pCrsr = pC->uc.pCursor;
87868
87869 /* The OP_RowData opcodes always follow OP_NotExists or
87870 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
87871 ** that might invalidate the cursor.
87872 ** If this where not the case, on of the following assert()s
87873 ** would fail. Should this ever change (because of changes in the code
87874 ** generator) then the fix would be to insert a call to
87875 ** sqlite3VdbeCursorMoveto().
87876 */
87877 assert( pC->deferredMoveto==0 );
87878 assert( sqlite3BtreeCursorIsValid(pCrsr) );
87879#if 0 /* Not required due to the previous to assert() statements */
87880 rc = sqlite3VdbeCursorMoveto(pC);
87881 if( rc!=SQLITE_OK ) goto abort_due_to_error;
87882#endif
87883
87884 n = sqlite3BtreePayloadSize(pCrsr);
87885 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
87886 goto too_big;
87887 }
87888 testcase( n==0 );
87889 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, n, pOut);
87890 if( rc ) goto abort_due_to_error;
87891 if( !pOp->p3 ) Deephemeralize(pOut);
87892 UPDATE_MAX_BLOBSIZE(pOut);
87893 REGISTER_TRACE(pOp->p2, pOut);
87894 break;
87895}
87896
87897/* Opcode: Rowid P1 P2 * * *
87898** Synopsis: r[P2]=rowid
87899**
87900** Store in register P2 an integer which is the key of the table entry that
87901** P1 is currently point to.
87902**
87903** P1 can be either an ordinary table or a virtual table. There used to
87904** be a separate OP_VRowid opcode for use with virtual tables, but this
87905** one opcode now works for both table types.
87906*/
87907case OP_Rowid: { /* out2 */
87908 VdbeCursor *pC;
87909 i64 v;
87910 sqlite3_vtab *pVtab;
87911 const sqlite3_module *pModule;
87912
87913 pOut = out2Prerelease(p, pOp);
87914 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87915 pC = p->apCsr[pOp->p1];
87916 assert( pC!=0 );
87917 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
87918 if( pC->nullRow ){
87919 pOut->flags = MEM_Null;
87920 break;
87921 }else if( pC->deferredMoveto ){
87922 v = pC->movetoTarget;
87923#ifndef SQLITE_OMIT_VIRTUALTABLE
87924 }else if( pC->eCurType==CURTYPE_VTAB ){
87925 assert( pC->uc.pVCur!=0 );
87926 pVtab = pC->uc.pVCur->pVtab;
87927 pModule = pVtab->pModule;
87928 assert( pModule->xRowid );
87929 rc = pModule->xRowid(pC->uc.pVCur, &v);
87930 sqlite3VtabImportErrmsg(p, pVtab);
87931 if( rc ) goto abort_due_to_error;
87932#endif /* SQLITE_OMIT_VIRTUALTABLE */
87933 }else{
87934 assert( pC->eCurType==CURTYPE_BTREE );
87935 assert( pC->uc.pCursor!=0 );
87936 rc = sqlite3VdbeCursorRestore(pC);
87937 if( rc ) goto abort_due_to_error;
87938 if( pC->nullRow ){
87939 pOut->flags = MEM_Null;
87940 break;
87941 }
87942 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
87943 }
87944 pOut->u.i = v;
87945 break;
87946}
87947
87948/* Opcode: NullRow P1 * * * *
87949**
87950** Move the cursor P1 to a null row. Any OP_Column operations
87951** that occur while the cursor is on the null row will always
87952** write a NULL.
87953*/
87954case OP_NullRow: {
87955 VdbeCursor *pC;
87956
87957 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87958 pC = p->apCsr[pOp->p1];
87959 assert( pC!=0 );
87960 pC->nullRow = 1;
87961 pC->cacheStatus = CACHE_STALE;
87962 if( pC->eCurType==CURTYPE_BTREE ){
87963 assert( pC->uc.pCursor!=0 );
87964 sqlite3BtreeClearCursor(pC->uc.pCursor);
87965 }
87966#ifdef SQLITE_DEBUG
87967 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
87968#endif
87969 break;
87970}
87971
87972/* Opcode: SeekEnd P1 * * * *
87973**
87974** Position cursor P1 at the end of the btree for the purpose of
87975** appending a new entry onto the btree.
87976**
87977** It is assumed that the cursor is used only for appending and so
87978** if the cursor is valid, then the cursor must already be pointing
87979** at the end of the btree and so no changes are made to
87980** the cursor.
87981*/
87982/* Opcode: Last P1 P2 * * *
87983**
87984** The next use of the Rowid or Column or Prev instruction for P1
87985** will refer to the last entry in the database table or index.
87986** If the table or index is empty and P2>0, then jump immediately to P2.
87987** If P2 is 0 or if the table or index is not empty, fall through
87988** to the following instruction.
87989**
87990** This opcode leaves the cursor configured to move in reverse order,
87991** from the end toward the beginning. In other words, the cursor is
87992** configured to use Prev, not Next.
87993*/
87994case OP_SeekEnd:
87995case OP_Last: { /* jump */
87996 VdbeCursor *pC;
87997 BtCursor *pCrsr;
87998 int res;
87999
88000 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
88001 pC = p->apCsr[pOp->p1];
88002 assert( pC!=0 );
88003 assert( pC->eCurType==CURTYPE_BTREE );
88004 pCrsr = pC->uc.pCursor;
88005 res = 0;
88006 assert( pCrsr!=0 );
88007#ifdef SQLITE_DEBUG
88008 pC->seekOp = pOp->opcode;
88009#endif
88010 if( pOp->opcode==OP_SeekEnd ){
88011 assert( pOp->p2==0 );
88012 pC->seekResult = -1;
88013 if( sqlite3BtreeCursorIsValidNN(pCrsr) ){
88014 break;
88015 }
88016 }
88017 rc = sqlite3BtreeLast(pCrsr, &res);
88018 pC->nullRow = (u8)res;
88019 pC->deferredMoveto = 0;
88020 pC->cacheStatus = CACHE_STALE;
88021 if( rc ) goto abort_due_to_error;
88022 if( pOp->p2>0 ){
88023 VdbeBranchTaken(res!=0,2);
88024 if( res ) goto jump_to_p2;
88025 }
88026 break;
88027}
88028
88029/* Opcode: IfSmaller P1 P2 P3 * *
88030**
88031** Estimate the number of rows in the table P1. Jump to P2 if that
88032** estimate is less than approximately 2**(0.1*P3).
88033*/
88034case OP_IfSmaller: { /* jump */
88035 VdbeCursor *pC;
88036 BtCursor *pCrsr;
88037 int res;
88038 i64 sz;
88039
88040 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
88041 pC = p->apCsr[pOp->p1];
88042 assert( pC!=0 );
88043 pCrsr = pC->uc.pCursor;
88044 assert( pCrsr );
88045 rc = sqlite3BtreeFirst(pCrsr, &res);
88046 if( rc ) goto abort_due_to_error;
88047 if( res==0 ){
88048 sz = sqlite3BtreeRowCountEst(pCrsr);
88049 if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
88050 }
88051 VdbeBranchTaken(res!=0,2);
88052 if( res ) goto jump_to_p2;
88053 break;
88054}
88055
88056
88057/* Opcode: SorterSort P1 P2 * * *
88058**
88059** After all records have been inserted into the Sorter object
88060** identified by P1, invoke this opcode to actually do the sorting.
88061** Jump to P2 if there are no records to be sorted.
88062**
88063** This opcode is an alias for OP_Sort and OP_Rewind that is used
88064** for Sorter objects.
88065*/
88066/* Opcode: Sort P1 P2 * * *
88067**
88068** This opcode does exactly the same thing as OP_Rewind except that
88069** it increments an undocumented global variable used for testing.
88070**
88071** Sorting is accomplished by writing records into a sorting index,
88072** then rewinding that index and playing it back from beginning to
88073** end. We use the OP_Sort opcode instead of OP_Rewind to do the
88074** rewinding so that the global variable will be incremented and
88075** regression tests can determine whether or not the optimizer is
88076** correctly optimizing out sorts.
88077*/
88078case OP_SorterSort: /* jump */
88079case OP_Sort: { /* jump */
88080#ifdef SQLITE_TEST
88081 sqlite3_sort_count++;
88082 sqlite3_search_count--;
88083#endif
88084 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
88085 /* Fall through into OP_Rewind */
88086}
88087/* Opcode: Rewind P1 P2 * * P5
88088**
88089** The next use of the Rowid or Column or Next instruction for P1
88090** will refer to the first entry in the database table or index.
88091** If the table or index is empty, jump immediately to P2.
88092** If the table or index is not empty, fall through to the following
88093** instruction.
88094**
88095** If P5 is non-zero and the table is not empty, then the "skip-next"
88096** flag is set on the cursor so that the next OP_Next instruction
88097** executed on it is a no-op.
88098**
88099** This opcode leaves the cursor configured to move in forward order,
88100** from the beginning toward the end. In other words, the cursor is
88101** configured to use Next, not Prev.
88102*/
88103case OP_Rewind: { /* jump */
88104 VdbeCursor *pC;
88105 BtCursor *pCrsr;
88106 int res;
88107
88108 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
88109 pC = p->apCsr[pOp->p1];
88110 assert( pC!=0 );
88111 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
88112 res = 1;
88113#ifdef SQLITE_DEBUG
88114 pC->seekOp = OP_Rewind;
88115#endif
88116 if( isSorter(pC) ){
88117 rc = sqlite3VdbeSorterRewind(pC, &res);
88118 }else{
88119 assert( pC->eCurType==CURTYPE_BTREE );
88120 pCrsr = pC->uc.pCursor;
88121 assert( pCrsr );
88122 rc = sqlite3BtreeFirst(pCrsr, &res);
88123#ifndef SQLITE_OMIT_WINDOWFUNC
88124 if( pOp->p5 ) sqlite3BtreeSkipNext(pCrsr);
88125#endif
88126 pC->deferredMoveto = 0;
88127 pC->cacheStatus = CACHE_STALE;
88128 }
88129 if( rc ) goto abort_due_to_error;
88130 pC->nullRow = (u8)res;
88131 assert( pOp->p2>0 && pOp->p2<p->nOp );
88132 VdbeBranchTaken(res!=0,2);
88133 if( res ) goto jump_to_p2;
88134 break;
88135}
88136
88137/* Opcode: Next P1 P2 P3 P4 P5
88138**
88139** Advance cursor P1 so that it points to the next key/data pair in its
88140** table or index. If there are no more key/value pairs then fall through
88141** to the following instruction. But if the cursor advance was successful,
88142** jump immediately to P2.
88143**
88144** The Next opcode is only valid following an SeekGT, SeekGE, or
88145** OP_Rewind opcode used to position the cursor. Next is not allowed
88146** to follow SeekLT, SeekLE, or OP_Last.
88147**
88148** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
88149** been opened prior to this opcode or the program will segfault.
88150**
88151** The P3 value is a hint to the btree implementation. If P3==1, that
88152** means P1 is an SQL index and that this instruction could have been
88153** omitted if that index had been unique. P3 is usually 0. P3 is
88154** always either 0 or 1.
88155**
88156** P4 is always of type P4_ADVANCE. The function pointer points to
88157** sqlite3BtreeNext().
88158**
88159** If P5 is positive and the jump is taken, then event counter
88160** number P5-1 in the prepared statement is incremented.
88161**
88162** See also: Prev
88163*/
88164/* Opcode: Prev P1 P2 P3 P4 P5
88165**
88166** Back up cursor P1 so that it points to the previous key/data pair in its
88167** table or index. If there is no previous key/value pairs then fall through
88168** to the following instruction. But if the cursor backup was successful,
88169** jump immediately to P2.
88170**
88171**
88172** The Prev opcode is only valid following an SeekLT, SeekLE, or
88173** OP_Last opcode used to position the cursor. Prev is not allowed
88174** to follow SeekGT, SeekGE, or OP_Rewind.
88175**
88176** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
88177** not open then the behavior is undefined.
88178**
88179** The P3 value is a hint to the btree implementation. If P3==1, that
88180** means P1 is an SQL index and that this instruction could have been
88181** omitted if that index had been unique. P3 is usually 0. P3 is
88182** always either 0 or 1.
88183**
88184** P4 is always of type P4_ADVANCE. The function pointer points to
88185** sqlite3BtreePrevious().
88186**
88187** If P5 is positive and the jump is taken, then event counter
88188** number P5-1 in the prepared statement is incremented.
88189*/
88190/* Opcode: SorterNext P1 P2 * * P5
88191**
88192** This opcode works just like OP_Next except that P1 must be a
88193** sorter object for which the OP_SorterSort opcode has been
88194** invoked. This opcode advances the cursor to the next sorted
88195** record, or jumps to P2 if there are no more sorted records.
88196*/
88197case OP_SorterNext: { /* jump */
88198 VdbeCursor *pC;
88199
88200 pC = p->apCsr[pOp->p1];
88201 assert( isSorter(pC) );
88202 rc = sqlite3VdbeSorterNext(db, pC);
88203 goto next_tail;
88204case OP_Prev: /* jump */
88205case OP_Next: /* jump */
88206 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
88207 assert( pOp->p5<ArraySize(p->aCounter) );
88208 pC = p->apCsr[pOp->p1];
88209 assert( pC!=0 );
88210 assert( pC->deferredMoveto==0 );
88211 assert( pC->eCurType==CURTYPE_BTREE );
88212 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
88213 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
88214
88215 /* The Next opcode is only used after SeekGT, SeekGE, Rewind, and Found.
88216 ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
88217 assert( pOp->opcode!=OP_Next
88218 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
88219 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found
88220 || pC->seekOp==OP_NullRow);
88221 assert( pOp->opcode!=OP_Prev
88222 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
88223 || pC->seekOp==OP_Last
88224 || pC->seekOp==OP_NullRow);
88225
88226 rc = pOp->p4.xAdvance(pC->uc.pCursor, pOp->p3);
88227next_tail:
88228 pC->cacheStatus = CACHE_STALE;
88229 VdbeBranchTaken(rc==SQLITE_OK,2);
88230 if( rc==SQLITE_OK ){
88231 pC->nullRow = 0;
88232 p->aCounter[pOp->p5]++;
88233#ifdef SQLITE_TEST
88234 sqlite3_search_count++;
88235#endif
88236 goto jump_to_p2_and_check_for_interrupt;
88237 }
88238 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
88239 rc = SQLITE_OK;
88240 pC->nullRow = 1;
88241 goto check_for_interrupt;
88242}
88243
88244/* Opcode: IdxInsert P1 P2 P3 P4 P5
88245** Synopsis: key=r[P2]
88246**
88247** Register P2 holds an SQL index key made using the
88248** MakeRecord instructions. This opcode writes that key
88249** into the index P1. Data for the entry is nil.
88250**
88251** If P4 is not zero, then it is the number of values in the unpacked
88252** key of reg(P2). In that case, P3 is the index of the first register
88253** for the unpacked key. The availability of the unpacked key can sometimes
88254** be an optimization.
88255**
88256** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
88257** that this insert is likely to be an append.
88258**
88259** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
88260** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
88261** then the change counter is unchanged.
88262**
88263** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
88264** run faster by avoiding an unnecessary seek on cursor P1. However,
88265** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
88266** seeks on the cursor or if the most recent seek used a key equivalent
88267** to P2.
88268**
88269** This instruction only works for indices. The equivalent instruction
88270** for tables is OP_Insert.
88271*/
88272/* Opcode: SorterInsert P1 P2 * * *
88273** Synopsis: key=r[P2]
88274**
88275** Register P2 holds an SQL index key made using the
88276** MakeRecord instructions. This opcode writes that key
88277** into the sorter P1. Data for the entry is nil.
88278*/
88279case OP_SorterInsert: /* in2 */
88280case OP_IdxInsert: { /* in2 */
88281 VdbeCursor *pC;
88282 BtreePayload x;
88283
88284 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
88285 pC = p->apCsr[pOp->p1];
88286 sqlite3VdbeIncrWriteCounter(p, pC);
88287 assert( pC!=0 );
88288 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
88289 pIn2 = &aMem[pOp->p2];
88290 assert( pIn2->flags & MEM_Blob );
88291 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
88292 assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert );
88293 assert( pC->isTable==0 );
88294 rc = ExpandBlob(pIn2);
88295 if( rc ) goto abort_due_to_error;
88296 if( pOp->opcode==OP_SorterInsert ){
88297 rc = sqlite3VdbeSorterWrite(pC, pIn2);
88298 }else{
88299 x.nKey = pIn2->n;
88300 x.pKey = pIn2->z;
88301 x.aMem = aMem + pOp->p3;
88302 x.nMem = (u16)pOp->p4.i;
88303 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
88304 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)),
88305 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
88306 );
88307 assert( pC->deferredMoveto==0 );
88308 pC->cacheStatus = CACHE_STALE;
88309 }
88310 if( rc) goto abort_due_to_error;
88311 break;
88312}
88313
88314/* Opcode: IdxDelete P1 P2 P3 * *
88315** Synopsis: key=r[P2@P3]
88316**
88317** The content of P3 registers starting at register P2 form
88318** an unpacked index key. This opcode removes that entry from the
88319** index opened by cursor P1.
88320*/
88321case OP_IdxDelete: {
88322 VdbeCursor *pC;
88323 BtCursor *pCrsr;
88324 int res;
88325 UnpackedRecord r;
88326
88327 assert( pOp->p3>0 );
88328 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
88329 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
88330 pC = p->apCsr[pOp->p1];
88331 assert( pC!=0 );
88332 assert( pC->eCurType==CURTYPE_BTREE );
88333 sqlite3VdbeIncrWriteCounter(p, pC);
88334 pCrsr = pC->uc.pCursor;
88335 assert( pCrsr!=0 );
88336 assert( pOp->p5==0 );
88337 r.pKeyInfo = pC->pKeyInfo;
88338 r.nField = (u16)pOp->p3;
88339 r.default_rc = 0;
88340 r.aMem = &aMem[pOp->p2];
88341 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
88342 if( rc ) goto abort_due_to_error;
88343 if( res==0 ){
88344 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE);
88345 if( rc ) goto abort_due_to_error;
88346 }
88347 assert( pC->deferredMoveto==0 );
88348 pC->cacheStatus = CACHE_STALE;
88349 pC->seekResult = 0;
88350 break;
88351}
88352
88353/* Opcode: DeferredSeek P1 * P3 P4 *
88354** Synopsis: Move P3 to P1.rowid if needed
88355**
88356** P1 is an open index cursor and P3 is a cursor on the corresponding
88357** table. This opcode does a deferred seek of the P3 table cursor
88358** to the row that corresponds to the current row of P1.
88359**
88360** This is a deferred seek. Nothing actually happens until
88361** the cursor is used to read a record. That way, if no reads
88362** occur, no unnecessary I/O happens.
88363**
88364** P4 may be an array of integers (type P4_INTARRAY) containing
88365** one entry for each column in the P3 table. If array entry a(i)
88366** is non-zero, then reading column a(i)-1 from cursor P3 is
88367** equivalent to performing the deferred seek and then reading column i
88368** from P1. This information is stored in P3 and used to redirect
88369** reads against P3 over to P1, thus possibly avoiding the need to
88370** seek and read cursor P3.
88371*/
88372/* Opcode: IdxRowid P1 P2 * * *
88373** Synopsis: r[P2]=rowid
88374**
88375** Write into register P2 an integer which is the last entry in the record at
88376** the end of the index key pointed to by cursor P1. This integer should be
88377** the rowid of the table entry to which this index entry points.
88378**
88379** See also: Rowid, MakeRecord.
88380*/
88381case OP_DeferredSeek:
88382case OP_IdxRowid: { /* out2 */
88383 VdbeCursor *pC; /* The P1 index cursor */
88384 VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */
88385 i64 rowid; /* Rowid that P1 current points to */
88386
88387 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
88388 pC = p->apCsr[pOp->p1];
88389 assert( pC!=0 );
88390 assert( pC->eCurType==CURTYPE_BTREE );
88391 assert( pC->uc.pCursor!=0 );
88392 assert( pC->isTable==0 );
88393 assert( pC->deferredMoveto==0 );
88394 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
88395
88396 /* The IdxRowid and Seek opcodes are combined because of the commonality
88397 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
88398 rc = sqlite3VdbeCursorRestore(pC);
88399
88400 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
88401 ** out from under the cursor. That will never happens for an IdxRowid
88402 ** or Seek opcode */
88403 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
88404
88405 if( !pC->nullRow ){
88406 rowid = 0; /* Not needed. Only used to silence a warning. */
88407 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
88408 if( rc!=SQLITE_OK ){
88409 goto abort_due_to_error;
88410 }
88411 if( pOp->opcode==OP_DeferredSeek ){
88412 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
88413 pTabCur = p->apCsr[pOp->p3];
88414 assert( pTabCur!=0 );
88415 assert( pTabCur->eCurType==CURTYPE_BTREE );
88416 assert( pTabCur->uc.pCursor!=0 );
88417 assert( pTabCur->isTable );
88418 pTabCur->nullRow = 0;
88419 pTabCur->movetoTarget = rowid;
88420 pTabCur->deferredMoveto = 1;
88421 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
88422 pTabCur->aAltMap = pOp->p4.ai;
88423 pTabCur->pAltCursor = pC;
88424 }else{
88425 pOut = out2Prerelease(p, pOp);
88426 pOut->u.i = rowid;
88427 }
88428 }else{
88429 assert( pOp->opcode==OP_IdxRowid );
88430 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
88431 }
88432 break;
88433}
88434
88435/* Opcode: IdxGE P1 P2 P3 P4 P5
88436** Synopsis: key=r[P3@P4]
88437**
88438** The P4 register values beginning with P3 form an unpacked index
88439** key that omits the PRIMARY KEY. Compare this key value against the index
88440** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
88441** fields at the end.
88442**
88443** If the P1 index entry is greater than or equal to the key value
88444** then jump to P2. Otherwise fall through to the next instruction.
88445*/
88446/* Opcode: IdxGT P1 P2 P3 P4 P5
88447** Synopsis: key=r[P3@P4]
88448**
88449** The P4 register values beginning with P3 form an unpacked index
88450** key that omits the PRIMARY KEY. Compare this key value against the index
88451** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
88452** fields at the end.
88453**
88454** If the P1 index entry is greater than the key value
88455** then jump to P2. Otherwise fall through to the next instruction.
88456*/
88457/* Opcode: IdxLT P1 P2 P3 P4 P5
88458** Synopsis: key=r[P3@P4]
88459**
88460** The P4 register values beginning with P3 form an unpacked index
88461** key that omits the PRIMARY KEY or ROWID. Compare this key value against
88462** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
88463** ROWID on the P1 index.
88464**
88465** If the P1 index entry is less than the key value then jump to P2.
88466** Otherwise fall through to the next instruction.
88467*/
88468/* Opcode: IdxLE P1 P2 P3 P4 P5
88469** Synopsis: key=r[P3@P4]
88470**
88471** The P4 register values beginning with P3 form an unpacked index
88472** key that omits the PRIMARY KEY or ROWID. Compare this key value against
88473** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
88474** ROWID on the P1 index.
88475**
88476** If the P1 index entry is less than or equal to the key value then jump
88477** to P2. Otherwise fall through to the next instruction.
88478*/
88479case OP_IdxLE: /* jump */
88480case OP_IdxGT: /* jump */
88481case OP_IdxLT: /* jump */
88482case OP_IdxGE: { /* jump */
88483 VdbeCursor *pC;
88484 int res;
88485 UnpackedRecord r;
88486
88487 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
88488 pC = p->apCsr[pOp->p1];
88489 assert( pC!=0 );
88490 assert( pC->isOrdered );
88491 assert( pC->eCurType==CURTYPE_BTREE );
88492 assert( pC->uc.pCursor!=0);
88493 assert( pC->deferredMoveto==0 );
88494 assert( pOp->p5==0 || pOp->p5==1 );
88495 assert( pOp->p4type==P4_INT32 );
88496 r.pKeyInfo = pC->pKeyInfo;
88497 r.nField = (u16)pOp->p4.i;
88498 if( pOp->opcode<OP_IdxLT ){
88499 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
88500 r.default_rc = -1;
88501 }else{
88502 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
88503 r.default_rc = 0;
88504 }
88505 r.aMem = &aMem[pOp->p3];
88506#ifdef SQLITE_DEBUG
88507 {
88508 int i;
88509 for(i=0; i<r.nField; i++){
88510 assert( memIsValid(&r.aMem[i]) );
88511 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
88512 }
88513 }
88514#endif
88515 res = 0; /* Not needed. Only used to silence a warning. */
88516 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
88517 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
88518 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
88519 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
88520 res = -res;
88521 }else{
88522 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
88523 res++;
88524 }
88525 VdbeBranchTaken(res>0,2);
88526 if( rc ) goto abort_due_to_error;
88527 if( res>0 ) goto jump_to_p2;
88528 break;
88529}
88530
88531/* Opcode: Destroy P1 P2 P3 * *
88532**
88533** Delete an entire database table or index whose root page in the database
88534** file is given by P1.
88535**
88536** The table being destroyed is in the main database file if P3==0. If
88537** P3==1 then the table to be clear is in the auxiliary database file
88538** that is used to store tables create using CREATE TEMPORARY TABLE.
88539**
88540** If AUTOVACUUM is enabled then it is possible that another root page
88541** might be moved into the newly deleted root page in order to keep all
88542** root pages contiguous at the beginning of the database. The former
88543** value of the root page that moved - its value before the move occurred -
88544** is stored in register P2. If no page movement was required (because the
88545** table being dropped was already the last one in the database) then a
88546** zero is stored in register P2. If AUTOVACUUM is disabled then a zero
88547** is stored in register P2.
88548**
88549** This opcode throws an error if there are any active reader VMs when
88550** it is invoked. This is done to avoid the difficulty associated with
88551** updating existing cursors when a root page is moved in an AUTOVACUUM
88552** database. This error is thrown even if the database is not an AUTOVACUUM
88553** db in order to avoid introducing an incompatibility between autovacuum
88554** and non-autovacuum modes.
88555**
88556** See also: Clear
88557*/
88558case OP_Destroy: { /* out2 */
88559 int iMoved;
88560 int iDb;
88561
88562 sqlite3VdbeIncrWriteCounter(p, 0);
88563 assert( p->readOnly==0 );
88564 assert( pOp->p1>1 );
88565 pOut = out2Prerelease(p, pOp);
88566 pOut->flags = MEM_Null;
88567 if( db->nVdbeRead > db->nVDestroy+1 ){
88568 rc = SQLITE_LOCKED;
88569 p->errorAction = OE_Abort;
88570 goto abort_due_to_error;
88571 }else{
88572 iDb = pOp->p3;
88573 assert( DbMaskTest(p->btreeMask, iDb) );
88574 iMoved = 0; /* Not needed. Only to silence a warning. */
88575 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
88576 pOut->flags = MEM_Int;
88577 pOut->u.i = iMoved;
88578 if( rc ) goto abort_due_to_error;
88579#ifndef SQLITE_OMIT_AUTOVACUUM
88580 if( iMoved!=0 ){
88581 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
88582 /* All OP_Destroy operations occur on the same btree */
88583 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
88584 resetSchemaOnFault = iDb+1;
88585 }
88586#endif
88587 }
88588 break;
88589}
88590
88591/* Opcode: Clear P1 P2 P3
88592**
88593** Delete all contents of the database table or index whose root page
88594** in the database file is given by P1. But, unlike Destroy, do not
88595** remove the table or index from the database file.
88596**
88597** The table being clear is in the main database file if P2==0. If
88598** P2==1 then the table to be clear is in the auxiliary database file
88599** that is used to store tables create using CREATE TEMPORARY TABLE.
88600**
88601** If the P3 value is non-zero, then the table referred to must be an
88602** intkey table (an SQL table, not an index). In this case the row change
88603** count is incremented by the number of rows in the table being cleared.
88604** If P3 is greater than zero, then the value stored in register P3 is
88605** also incremented by the number of rows in the table being cleared.
88606**
88607** See also: Destroy
88608*/
88609case OP_Clear: {
88610 int nChange;
88611
88612 sqlite3VdbeIncrWriteCounter(p, 0);
88613 nChange = 0;
88614 assert( p->readOnly==0 );
88615 assert( DbMaskTest(p->btreeMask, pOp->p2) );
88616 rc = sqlite3BtreeClearTable(
88617 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
88618 );
88619 if( pOp->p3 ){
88620 p->nChange += nChange;
88621 if( pOp->p3>0 ){
88622 assert( memIsValid(&aMem[pOp->p3]) );
88623 memAboutToChange(p, &aMem[pOp->p3]);
88624 aMem[pOp->p3].u.i += nChange;
88625 }
88626 }
88627 if( rc ) goto abort_due_to_error;
88628 break;
88629}
88630
88631/* Opcode: ResetSorter P1 * * * *
88632**
88633** Delete all contents from the ephemeral table or sorter
88634** that is open on cursor P1.
88635**
88636** This opcode only works for cursors used for sorting and
88637** opened with OP_OpenEphemeral or OP_SorterOpen.
88638*/
88639case OP_ResetSorter: {
88640 VdbeCursor *pC;
88641
88642 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
88643 pC = p->apCsr[pOp->p1];
88644 assert( pC!=0 );
88645 if( isSorter(pC) ){
88646 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
88647 }else{
88648 assert( pC->eCurType==CURTYPE_BTREE );
88649 assert( pC->isEphemeral );
88650 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
88651 if( rc ) goto abort_due_to_error;
88652 }
88653 break;
88654}
88655
88656/* Opcode: CreateBtree P1 P2 P3 * *
88657** Synopsis: r[P2]=root iDb=P1 flags=P3
88658**
88659** Allocate a new b-tree in the main database file if P1==0 or in the
88660** TEMP database file if P1==1 or in an attached database if
88661** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
88662** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
88663** The root page number of the new b-tree is stored in register P2.
88664*/
88665case OP_CreateBtree: { /* out2 */
88666 int pgno;
88667 Db *pDb;
88668
88669 sqlite3VdbeIncrWriteCounter(p, 0);
88670 pOut = out2Prerelease(p, pOp);
88671 pgno = 0;
88672 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
88673 assert( pOp->p1>=0 && pOp->p1<db->nDb );
88674 assert( DbMaskTest(p->btreeMask, pOp->p1) );
88675 assert( p->readOnly==0 );
88676 pDb = &db->aDb[pOp->p1];
88677 assert( pDb->pBt!=0 );
88678 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
88679 if( rc ) goto abort_due_to_error;
88680 pOut->u.i = pgno;
88681 break;
88682}
88683
88684/* Opcode: SqlExec * * * P4 *
88685**
88686** Run the SQL statement or statements specified in the P4 string.
88687*/
88688case OP_SqlExec: {
88689 sqlite3VdbeIncrWriteCounter(p, 0);
88690 db->nSqlExec++;
88691 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
88692 db->nSqlExec--;
88693 if( rc ) goto abort_due_to_error;
88694 break;
88695}
88696
88697/* Opcode: ParseSchema P1 * * P4 *
88698**
88699** Read and parse all entries from the SQLITE_MASTER table of database P1
88700** that match the WHERE clause P4. If P4 is a NULL pointer, then the
88701** entire schema for P1 is reparsed.
88702**
88703** This opcode invokes the parser to create a new virtual machine,
88704** then runs the new virtual machine. It is thus a re-entrant opcode.
88705*/
88706case OP_ParseSchema: {
88707 int iDb;
88708 const char *zMaster;
88709 char *zSql;
88710 InitData initData;
88711
88712 /* Any prepared statement that invokes this opcode will hold mutexes
88713 ** on every btree. This is a prerequisite for invoking
88714 ** sqlite3InitCallback().
88715 */
88716#ifdef SQLITE_DEBUG
88717 for(iDb=0; iDb<db->nDb; iDb++){
88718 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
88719 }
88720#endif
88721
88722 iDb = pOp->p1;
88723 assert( iDb>=0 && iDb<db->nDb );
88724 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
88725
88726#ifndef SQLITE_OMIT_ALTERTABLE
88727 if( pOp->p4.z==0 ){
88728 sqlite3SchemaClear(db->aDb[iDb].pSchema);
88729 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
88730 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, INITFLAG_AlterTable);
88731 db->mDbFlags |= DBFLAG_SchemaChange;
88732 p->expired = 0;
88733 }else
88734#endif
88735 {
88736 zMaster = MASTER_NAME;
88737 initData.db = db;
88738 initData.iDb = iDb;
88739 initData.pzErrMsg = &p->zErrMsg;
88740 initData.mInitFlags = 0;
88741 zSql = sqlite3MPrintf(db,
88742 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
88743 db->aDb[iDb].zDbSName, zMaster, pOp->p4.z);
88744 if( zSql==0 ){
88745 rc = SQLITE_NOMEM_BKPT;
88746 }else{
88747 assert( db->init.busy==0 );
88748 db->init.busy = 1;
88749 initData.rc = SQLITE_OK;
88750 assert( !db->mallocFailed );
88751 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
88752 if( rc==SQLITE_OK ) rc = initData.rc;
88753 sqlite3DbFreeNN(db, zSql);
88754 db->init.busy = 0;
88755 }
88756 }
88757 if( rc ){
88758 sqlite3ResetAllSchemasOfConnection(db);
88759 if( rc==SQLITE_NOMEM ){
88760 goto no_mem;
88761 }
88762 goto abort_due_to_error;
88763 }
88764 break;
88765}
88766
88767#if !defined(SQLITE_OMIT_ANALYZE)
88768/* Opcode: LoadAnalysis P1 * * * *
88769**
88770** Read the sqlite_stat1 table for database P1 and load the content
88771** of that table into the internal index hash table. This will cause
88772** the analysis to be used when preparing all subsequent queries.
88773*/
88774case OP_LoadAnalysis: {
88775 assert( pOp->p1>=0 && pOp->p1<db->nDb );
88776 rc = sqlite3AnalysisLoad(db, pOp->p1);
88777 if( rc ) goto abort_due_to_error;
88778 break;
88779}
88780#endif /* !defined(SQLITE_OMIT_ANALYZE) */
88781
88782/* Opcode: DropTable P1 * * P4 *
88783**
88784** Remove the internal (in-memory) data structures that describe
88785** the table named P4 in database P1. This is called after a table
88786** is dropped from disk (using the Destroy opcode) in order to keep
88787** the internal representation of the
88788** schema consistent with what is on disk.
88789*/
88790case OP_DropTable: {
88791 sqlite3VdbeIncrWriteCounter(p, 0);
88792 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
88793 break;
88794}
88795
88796/* Opcode: DropIndex P1 * * P4 *
88797**
88798** Remove the internal (in-memory) data structures that describe
88799** the index named P4 in database P1. This is called after an index
88800** is dropped from disk (using the Destroy opcode)
88801** in order to keep the internal representation of the
88802** schema consistent with what is on disk.
88803*/
88804case OP_DropIndex: {
88805 sqlite3VdbeIncrWriteCounter(p, 0);
88806 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
88807 break;
88808}
88809
88810/* Opcode: DropTrigger P1 * * P4 *
88811**
88812** Remove the internal (in-memory) data structures that describe
88813** the trigger named P4 in database P1. This is called after a trigger
88814** is dropped from disk (using the Destroy opcode) in order to keep
88815** the internal representation of the
88816** schema consistent with what is on disk.
88817*/
88818case OP_DropTrigger: {
88819 sqlite3VdbeIncrWriteCounter(p, 0);
88820 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
88821 break;
88822}
88823
88824
88825#ifndef SQLITE_OMIT_INTEGRITY_CHECK
88826/* Opcode: IntegrityCk P1 P2 P3 P4 P5
88827**
88828** Do an analysis of the currently open database. Store in
88829** register P1 the text of an error message describing any problems.
88830** If no problems are found, store a NULL in register P1.
88831**
88832** The register P3 contains one less than the maximum number of allowed errors.
88833** At most reg(P3) errors will be reported.
88834** In other words, the analysis stops as soon as reg(P1) errors are
88835** seen. Reg(P1) is updated with the number of errors remaining.
88836**
88837** The root page numbers of all tables in the database are integers
88838** stored in P4_INTARRAY argument.
88839**
88840** If P5 is not zero, the check is done on the auxiliary database
88841** file, not the main database file.
88842**
88843** This opcode is used to implement the integrity_check pragma.
88844*/
88845case OP_IntegrityCk: {
88846 int nRoot; /* Number of tables to check. (Number of root pages.) */
88847 int *aRoot; /* Array of rootpage numbers for tables to be checked */
88848 int nErr; /* Number of errors reported */
88849 char *z; /* Text of the error report */
88850 Mem *pnErr; /* Register keeping track of errors remaining */
88851
88852 assert( p->bIsReader );
88853 nRoot = pOp->p2;
88854 aRoot = pOp->p4.ai;
88855 assert( nRoot>0 );
88856 assert( aRoot[0]==nRoot );
88857 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
88858 pnErr = &aMem[pOp->p3];
88859 assert( (pnErr->flags & MEM_Int)!=0 );
88860 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
88861 pIn1 = &aMem[pOp->p1];
88862 assert( pOp->p5<db->nDb );
88863 assert( DbMaskTest(p->btreeMask, pOp->p5) );
88864 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
88865 (int)pnErr->u.i+1, &nErr);
88866 sqlite3VdbeMemSetNull(pIn1);
88867 if( nErr==0 ){
88868 assert( z==0 );
88869 }else if( z==0 ){
88870 goto no_mem;
88871 }else{
88872 pnErr->u.i -= nErr-1;
88873 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
88874 }
88875 UPDATE_MAX_BLOBSIZE(pIn1);
88876 sqlite3VdbeChangeEncoding(pIn1, encoding);
88877 break;
88878}
88879#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
88880
88881/* Opcode: RowSetAdd P1 P2 * * *
88882** Synopsis: rowset(P1)=r[P2]
88883**
88884** Insert the integer value held by register P2 into a RowSet object
88885** held in register P1.
88886**
88887** An assertion fails if P2 is not an integer.
88888*/
88889case OP_RowSetAdd: { /* in1, in2 */
88890 pIn1 = &aMem[pOp->p1];
88891 pIn2 = &aMem[pOp->p2];
88892 assert( (pIn2->flags & MEM_Int)!=0 );
88893 if( (pIn1->flags & MEM_Blob)==0 ){
88894 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
88895 }
88896 assert( sqlite3VdbeMemIsRowSet(pIn1) );
88897 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
88898 break;
88899}
88900
88901/* Opcode: RowSetRead P1 P2 P3 * *
88902** Synopsis: r[P3]=rowset(P1)
88903**
88904** Extract the smallest value from the RowSet object in P1
88905** and put that value into register P3.
88906** Or, if RowSet object P1 is initially empty, leave P3
88907** unchanged and jump to instruction P2.
88908*/
88909case OP_RowSetRead: { /* jump, in1, out3 */
88910 i64 val;
88911
88912 pIn1 = &aMem[pOp->p1];
88913 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) );
88914 if( (pIn1->flags & MEM_Blob)==0
88915 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
88916 ){
88917 /* The boolean index is empty */
88918 sqlite3VdbeMemSetNull(pIn1);
88919 VdbeBranchTaken(1,2);
88920 goto jump_to_p2_and_check_for_interrupt;
88921 }else{
88922 /* A value was pulled from the index */
88923 VdbeBranchTaken(0,2);
88924 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
88925 }
88926 goto check_for_interrupt;
88927}
88928
88929/* Opcode: RowSetTest P1 P2 P3 P4
88930** Synopsis: if r[P3] in rowset(P1) goto P2
88931**
88932** Register P3 is assumed to hold a 64-bit integer value. If register P1
88933** contains a RowSet object and that RowSet object contains
88934** the value held in P3, jump to register P2. Otherwise, insert the
88935** integer in P3 into the RowSet and continue on to the
88936** next opcode.
88937**
88938** The RowSet object is optimized for the case where sets of integers
88939** are inserted in distinct phases, which each set contains no duplicates.
88940** Each set is identified by a unique P4 value. The first set
88941** must have P4==0, the final set must have P4==-1, and for all other sets
88942** must have P4>0.
88943**
88944** This allows optimizations: (a) when P4==0 there is no need to test
88945** the RowSet object for P3, as it is guaranteed not to contain it,
88946** (b) when P4==-1 there is no need to insert the value, as it will
88947** never be tested for, and (c) when a value that is part of set X is
88948** inserted, there is no need to search to see if the same value was
88949** previously inserted as part of set X (only if it was previously
88950** inserted as part of some other set).
88951*/
88952case OP_RowSetTest: { /* jump, in1, in3 */
88953 int iSet;
88954 int exists;
88955
88956 pIn1 = &aMem[pOp->p1];
88957 pIn3 = &aMem[pOp->p3];
88958 iSet = pOp->p4.i;
88959 assert( pIn3->flags&MEM_Int );
88960
88961 /* If there is anything other than a rowset object in memory cell P1,
88962 ** delete it now and initialize P1 with an empty rowset
88963 */
88964 if( (pIn1->flags & MEM_Blob)==0 ){
88965 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
88966 }
88967 assert( sqlite3VdbeMemIsRowSet(pIn1) );
88968 assert( pOp->p4type==P4_INT32 );
88969 assert( iSet==-1 || iSet>=0 );
88970 if( iSet ){
88971 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
88972 VdbeBranchTaken(exists!=0,2);
88973 if( exists ) goto jump_to_p2;
88974 }
88975 if( iSet>=0 ){
88976 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
88977 }
88978 break;
88979}
88980
88981
88982#ifndef SQLITE_OMIT_TRIGGER
88983
88984/* Opcode: Program P1 P2 P3 P4 P5
88985**
88986** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
88987**
88988** P1 contains the address of the memory cell that contains the first memory
88989** cell in an array of values used as arguments to the sub-program. P2
88990** contains the address to jump to if the sub-program throws an IGNORE
88991** exception using the RAISE() function. Register P3 contains the address
88992** of a memory cell in this (the parent) VM that is used to allocate the
88993** memory required by the sub-vdbe at runtime.
88994**
88995** P4 is a pointer to the VM containing the trigger program.
88996**
88997** If P5 is non-zero, then recursive program invocation is enabled.
88998*/
88999case OP_Program: { /* jump */
89000 int nMem; /* Number of memory registers for sub-program */
89001 int nByte; /* Bytes of runtime space required for sub-program */
89002 Mem *pRt; /* Register to allocate runtime space */
89003 Mem *pMem; /* Used to iterate through memory cells */
89004 Mem *pEnd; /* Last memory cell in new array */
89005 VdbeFrame *pFrame; /* New vdbe frame to execute in */
89006 SubProgram *pProgram; /* Sub-program to execute */
89007 void *t; /* Token identifying trigger */
89008
89009 pProgram = pOp->p4.pProgram;
89010 pRt = &aMem[pOp->p3];
89011 assert( pProgram->nOp>0 );
89012
89013 /* If the p5 flag is clear, then recursive invocation of triggers is
89014 ** disabled for backwards compatibility (p5 is set if this sub-program
89015 ** is really a trigger, not a foreign key action, and the flag set
89016 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
89017 **
89018 ** It is recursive invocation of triggers, at the SQL level, that is
89019 ** disabled. In some cases a single trigger may generate more than one
89020 ** SubProgram (if the trigger may be executed with more than one different
89021 ** ON CONFLICT algorithm). SubProgram structures associated with a
89022 ** single trigger all have the same value for the SubProgram.token
89023 ** variable. */
89024 if( pOp->p5 ){
89025 t = pProgram->token;
89026 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
89027 if( pFrame ) break;
89028 }
89029
89030 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
89031 rc = SQLITE_ERROR;
89032 sqlite3VdbeError(p, "too many levels of trigger recursion");
89033 goto abort_due_to_error;
89034 }
89035
89036 /* Register pRt is used to store the memory required to save the state
89037 ** of the current program, and the memory required at runtime to execute
89038 ** the trigger program. If this trigger has been fired before, then pRt
89039 ** is already allocated. Otherwise, it must be initialized. */
89040 if( (pRt->flags&MEM_Blob)==0 ){
89041 /* SubProgram.nMem is set to the number of memory cells used by the
89042 ** program stored in SubProgram.aOp. As well as these, one memory
89043 ** cell is required for each cursor used by the program. Set local
89044 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
89045 */
89046 nMem = pProgram->nMem + pProgram->nCsr;
89047 assert( nMem>0 );
89048 if( pProgram->nCsr==0 ) nMem++;
89049 nByte = ROUND8(sizeof(VdbeFrame))
89050 + nMem * sizeof(Mem)
89051 + pProgram->nCsr * sizeof(VdbeCursor*)
89052 + (pProgram->nOp + 7)/8;
89053 pFrame = sqlite3DbMallocZero(db, nByte);
89054 if( !pFrame ){
89055 goto no_mem;
89056 }
89057 sqlite3VdbeMemRelease(pRt);
89058 pRt->flags = MEM_Blob|MEM_Dyn;
89059 pRt->z = (char*)pFrame;
89060 pRt->n = nByte;
89061 pRt->xDel = sqlite3VdbeFrameMemDel;
89062
89063 pFrame->v = p;
89064 pFrame->nChildMem = nMem;
89065 pFrame->nChildCsr = pProgram->nCsr;
89066 pFrame->pc = (int)(pOp - aOp);
89067 pFrame->aMem = p->aMem;
89068 pFrame->nMem = p->nMem;
89069 pFrame->apCsr = p->apCsr;
89070 pFrame->nCursor = p->nCursor;
89071 pFrame->aOp = p->aOp;
89072 pFrame->nOp = p->nOp;
89073 pFrame->token = pProgram->token;
89074#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
89075 pFrame->anExec = p->anExec;
89076#endif
89077#ifdef SQLITE_DEBUG
89078 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC;
89079#endif
89080
89081 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
89082 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
89083 pMem->flags = MEM_Undefined;
89084 pMem->db = db;
89085 }
89086 }else{
89087 pFrame = (VdbeFrame*)pRt->z;
89088 assert( pRt->xDel==sqlite3VdbeFrameMemDel );
89089 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
89090 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
89091 assert( pProgram->nCsr==pFrame->nChildCsr );
89092 assert( (int)(pOp - aOp)==pFrame->pc );
89093 }
89094
89095 p->nFrame++;
89096 pFrame->pParent = p->pFrame;
89097 pFrame->lastRowid = db->lastRowid;
89098 pFrame->nChange = p->nChange;
89099 pFrame->nDbChange = p->db->nChange;
89100 assert( pFrame->pAuxData==0 );
89101 pFrame->pAuxData = p->pAuxData;
89102 p->pAuxData = 0;
89103 p->nChange = 0;
89104 p->pFrame = pFrame;
89105 p->aMem = aMem = VdbeFrameMem(pFrame);
89106 p->nMem = pFrame->nChildMem;
89107 p->nCursor = (u16)pFrame->nChildCsr;
89108 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
89109 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
89110 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
89111 p->aOp = aOp = pProgram->aOp;
89112 p->nOp = pProgram->nOp;
89113#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
89114 p->anExec = 0;
89115#endif
89116 pOp = &aOp[-1];
89117
89118 break;
89119}
89120
89121/* Opcode: Param P1 P2 * * *
89122**
89123** This opcode is only ever present in sub-programs called via the
89124** OP_Program instruction. Copy a value currently stored in a memory
89125** cell of the calling (parent) frame to cell P2 in the current frames
89126** address space. This is used by trigger programs to access the new.*
89127** and old.* values.
89128**
89129** The address of the cell in the parent frame is determined by adding
89130** the value of the P1 argument to the value of the P1 argument to the
89131** calling OP_Program instruction.
89132*/
89133case OP_Param: { /* out2 */
89134 VdbeFrame *pFrame;
89135 Mem *pIn;
89136 pOut = out2Prerelease(p, pOp);
89137 pFrame = p->pFrame;
89138 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
89139 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
89140 break;
89141}
89142
89143#endif /* #ifndef SQLITE_OMIT_TRIGGER */
89144
89145#ifndef SQLITE_OMIT_FOREIGN_KEY
89146/* Opcode: FkCounter P1 P2 * * *
89147** Synopsis: fkctr[P1]+=P2
89148**
89149** Increment a "constraint counter" by P2 (P2 may be negative or positive).
89150** If P1 is non-zero, the database constraint counter is incremented
89151** (deferred foreign key constraints). Otherwise, if P1 is zero, the
89152** statement counter is incremented (immediate foreign key constraints).
89153*/
89154case OP_FkCounter: {
89155 if( db->flags & SQLITE_DeferFKs ){
89156 db->nDeferredImmCons += pOp->p2;
89157 }else if( pOp->p1 ){
89158 db->nDeferredCons += pOp->p2;
89159 }else{
89160 p->nFkConstraint += pOp->p2;
89161 }
89162 break;
89163}
89164
89165/* Opcode: FkIfZero P1 P2 * * *
89166** Synopsis: if fkctr[P1]==0 goto P2
89167**
89168** This opcode tests if a foreign key constraint-counter is currently zero.
89169** If so, jump to instruction P2. Otherwise, fall through to the next
89170** instruction.
89171**
89172** If P1 is non-zero, then the jump is taken if the database constraint-counter
89173** is zero (the one that counts deferred constraint violations). If P1 is
89174** zero, the jump is taken if the statement constraint-counter is zero
89175** (immediate foreign key constraint violations).
89176*/
89177case OP_FkIfZero: { /* jump */
89178 if( pOp->p1 ){
89179 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
89180 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
89181 }else{
89182 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
89183 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
89184 }
89185 break;
89186}
89187#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
89188
89189#ifndef SQLITE_OMIT_AUTOINCREMENT
89190/* Opcode: MemMax P1 P2 * * *
89191** Synopsis: r[P1]=max(r[P1],r[P2])
89192**
89193** P1 is a register in the root frame of this VM (the root frame is
89194** different from the current frame if this instruction is being executed
89195** within a sub-program). Set the value of register P1 to the maximum of
89196** its current value and the value in register P2.
89197**
89198** This instruction throws an error if the memory cell is not initially
89199** an integer.
89200*/
89201case OP_MemMax: { /* in2 */
89202 VdbeFrame *pFrame;
89203 if( p->pFrame ){
89204 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
89205 pIn1 = &pFrame->aMem[pOp->p1];
89206 }else{
89207 pIn1 = &aMem[pOp->p1];
89208 }
89209 assert( memIsValid(pIn1) );
89210 sqlite3VdbeMemIntegerify(pIn1);
89211 pIn2 = &aMem[pOp->p2];
89212 sqlite3VdbeMemIntegerify(pIn2);
89213 if( pIn1->u.i<pIn2->u.i){
89214 pIn1->u.i = pIn2->u.i;
89215 }
89216 break;
89217}
89218#endif /* SQLITE_OMIT_AUTOINCREMENT */
89219
89220/* Opcode: IfPos P1 P2 P3 * *
89221** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
89222**
89223** Register P1 must contain an integer.
89224** If the value of register P1 is 1 or greater, subtract P3 from the
89225** value in P1 and jump to P2.
89226**
89227** If the initial value of register P1 is less than 1, then the
89228** value is unchanged and control passes through to the next instruction.
89229*/
89230case OP_IfPos: { /* jump, in1 */
89231 pIn1 = &aMem[pOp->p1];
89232 assert( pIn1->flags&MEM_Int );
89233 VdbeBranchTaken( pIn1->u.i>0, 2);
89234 if( pIn1->u.i>0 ){
89235 pIn1->u.i -= pOp->p3;
89236 goto jump_to_p2;
89237 }
89238 break;
89239}
89240
89241/* Opcode: OffsetLimit P1 P2 P3 * *
89242** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
89243**
89244** This opcode performs a commonly used computation associated with
89245** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3]
89246** holds the offset counter. The opcode computes the combined value
89247** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
89248** value computed is the total number of rows that will need to be
89249** visited in order to complete the query.
89250**
89251** If r[P3] is zero or negative, that means there is no OFFSET
89252** and r[P2] is set to be the value of the LIMIT, r[P1].
89253**
89254** if r[P1] is zero or negative, that means there is no LIMIT
89255** and r[P2] is set to -1.
89256**
89257** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
89258*/
89259case OP_OffsetLimit: { /* in1, out2, in3 */
89260 i64 x;
89261 pIn1 = &aMem[pOp->p1];
89262 pIn3 = &aMem[pOp->p3];
89263 pOut = out2Prerelease(p, pOp);
89264 assert( pIn1->flags & MEM_Int );
89265 assert( pIn3->flags & MEM_Int );
89266 x = pIn1->u.i;
89267 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
89268 /* If the LIMIT is less than or equal to zero, loop forever. This
89269 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
89270 ** also loop forever. This is undocumented. In fact, one could argue
89271 ** that the loop should terminate. But assuming 1 billion iterations
89272 ** per second (far exceeding the capabilities of any current hardware)
89273 ** it would take nearly 300 years to actually reach the limit. So
89274 ** looping forever is a reasonable approximation. */
89275 pOut->u.i = -1;
89276 }else{
89277 pOut->u.i = x;
89278 }
89279 break;
89280}
89281
89282/* Opcode: IfNotZero P1 P2 * * *
89283** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
89284**
89285** Register P1 must contain an integer. If the content of register P1 is
89286** initially greater than zero, then decrement the value in register P1.
89287** If it is non-zero (negative or positive) and then also jump to P2.
89288** If register P1 is initially zero, leave it unchanged and fall through.
89289*/
89290case OP_IfNotZero: { /* jump, in1 */
89291 pIn1 = &aMem[pOp->p1];
89292 assert( pIn1->flags&MEM_Int );
89293 VdbeBranchTaken(pIn1->u.i<0, 2);
89294 if( pIn1->u.i ){
89295 if( pIn1->u.i>0 ) pIn1->u.i--;
89296 goto jump_to_p2;
89297 }
89298 break;
89299}
89300
89301/* Opcode: DecrJumpZero P1 P2 * * *
89302** Synopsis: if (--r[P1])==0 goto P2
89303**
89304** Register P1 must hold an integer. Decrement the value in P1
89305** and jump to P2 if the new value is exactly zero.
89306*/
89307case OP_DecrJumpZero: { /* jump, in1 */
89308 pIn1 = &aMem[pOp->p1];
89309 assert( pIn1->flags&MEM_Int );
89310 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
89311 VdbeBranchTaken(pIn1->u.i==0, 2);
89312 if( pIn1->u.i==0 ) goto jump_to_p2;
89313 break;
89314}
89315
89316
89317/* Opcode: AggStep * P2 P3 P4 P5
89318** Synopsis: accum=r[P3] step(r[P2@P5])
89319**
89320** Execute the xStep function for an aggregate.
89321** The function has P5 arguments. P4 is a pointer to the
89322** FuncDef structure that specifies the function. Register P3 is the
89323** accumulator.
89324**
89325** The P5 arguments are taken from register P2 and its
89326** successors.
89327*/
89328/* Opcode: AggInverse * P2 P3 P4 P5
89329** Synopsis: accum=r[P3] inverse(r[P2@P5])
89330**
89331** Execute the xInverse function for an aggregate.
89332** The function has P5 arguments. P4 is a pointer to the
89333** FuncDef structure that specifies the function. Register P3 is the
89334** accumulator.
89335**
89336** The P5 arguments are taken from register P2 and its
89337** successors.
89338*/
89339/* Opcode: AggStep1 P1 P2 P3 P4 P5
89340** Synopsis: accum=r[P3] step(r[P2@P5])
89341**
89342** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
89343** aggregate. The function has P5 arguments. P4 is a pointer to the
89344** FuncDef structure that specifies the function. Register P3 is the
89345** accumulator.
89346**
89347** The P5 arguments are taken from register P2 and its
89348** successors.
89349**
89350** This opcode is initially coded as OP_AggStep0. On first evaluation,
89351** the FuncDef stored in P4 is converted into an sqlite3_context and
89352** the opcode is changed. In this way, the initialization of the
89353** sqlite3_context only happens once, instead of on each call to the
89354** step function.
89355*/
89356case OP_AggInverse:
89357case OP_AggStep: {
89358 int n;
89359 sqlite3_context *pCtx;
89360
89361 assert( pOp->p4type==P4_FUNCDEF );
89362 n = pOp->p5;
89363 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
89364 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
89365 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
89366 pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) +
89367 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
89368 if( pCtx==0 ) goto no_mem;
89369 pCtx->pMem = 0;
89370 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
89371 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
89372 pCtx->pFunc = pOp->p4.pFunc;
89373 pCtx->iOp = (int)(pOp - aOp);
89374 pCtx->pVdbe = p;
89375 pCtx->skipFlag = 0;
89376 pCtx->isError = 0;
89377 pCtx->argc = n;
89378 pOp->p4type = P4_FUNCCTX;
89379 pOp->p4.pCtx = pCtx;
89380
89381 /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */
89382 assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
89383
89384 pOp->opcode = OP_AggStep1;
89385 /* Fall through into OP_AggStep */
89386}
89387case OP_AggStep1: {
89388 int i;
89389 sqlite3_context *pCtx;
89390 Mem *pMem;
89391
89392 assert( pOp->p4type==P4_FUNCCTX );
89393 pCtx = pOp->p4.pCtx;
89394 pMem = &aMem[pOp->p3];
89395
89396#ifdef SQLITE_DEBUG
89397 if( pOp->p1 ){
89398 /* This is an OP_AggInverse call. Verify that xStep has always
89399 ** been called at least once prior to any xInverse call. */
89400 assert( pMem->uTemp==0x1122e0e3 );
89401 }else{
89402 /* This is an OP_AggStep call. Mark it as such. */
89403 pMem->uTemp = 0x1122e0e3;
89404 }
89405#endif
89406
89407 /* If this function is inside of a trigger, the register array in aMem[]
89408 ** might change from one evaluation to the next. The next block of code
89409 ** checks to see if the register array has changed, and if so it
89410 ** reinitializes the relavant parts of the sqlite3_context object */
89411 if( pCtx->pMem != pMem ){
89412 pCtx->pMem = pMem;
89413 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
89414 }
89415
89416#ifdef SQLITE_DEBUG
89417 for(i=0; i<pCtx->argc; i++){
89418 assert( memIsValid(pCtx->argv[i]) );
89419 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
89420 }
89421#endif
89422
89423 pMem->n++;
89424 assert( pCtx->pOut->flags==MEM_Null );
89425 assert( pCtx->isError==0 );
89426 assert( pCtx->skipFlag==0 );
89427#ifndef SQLITE_OMIT_WINDOWFUNC
89428 if( pOp->p1 ){
89429 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
89430 }else
89431#endif
89432 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
89433
89434 if( pCtx->isError ){
89435 if( pCtx->isError>0 ){
89436 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
89437 rc = pCtx->isError;
89438 }
89439 if( pCtx->skipFlag ){
89440 assert( pOp[-1].opcode==OP_CollSeq );
89441 i = pOp[-1].p1;
89442 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
89443 pCtx->skipFlag = 0;
89444 }
89445 sqlite3VdbeMemRelease(pCtx->pOut);
89446 pCtx->pOut->flags = MEM_Null;
89447 pCtx->isError = 0;
89448 if( rc ) goto abort_due_to_error;
89449 }
89450 assert( pCtx->pOut->flags==MEM_Null );
89451 assert( pCtx->skipFlag==0 );
89452 break;
89453}
89454
89455/* Opcode: AggFinal P1 P2 * P4 *
89456** Synopsis: accum=r[P1] N=P2
89457**
89458** P1 is the memory location that is the accumulator for an aggregate
89459** or window function. Execute the finalizer function
89460** for an aggregate and store the result in P1.
89461**
89462** P2 is the number of arguments that the step function takes and
89463** P4 is a pointer to the FuncDef for this function. The P2
89464** argument is not used by this opcode. It is only there to disambiguate
89465** functions that can take varying numbers of arguments. The
89466** P4 argument is only needed for the case where
89467** the step function was not previously called.
89468*/
89469/* Opcode: AggValue * P2 P3 P4 *
89470** Synopsis: r[P3]=value N=P2
89471**
89472** Invoke the xValue() function and store the result in register P3.
89473**
89474** P2 is the number of arguments that the step function takes and
89475** P4 is a pointer to the FuncDef for this function. The P2
89476** argument is not used by this opcode. It is only there to disambiguate
89477** functions that can take varying numbers of arguments. The
89478** P4 argument is only needed for the case where
89479** the step function was not previously called.
89480*/
89481case OP_AggValue:
89482case OP_AggFinal: {
89483 Mem *pMem;
89484 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
89485 assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
89486 pMem = &aMem[pOp->p1];
89487 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
89488#ifndef SQLITE_OMIT_WINDOWFUNC
89489 if( pOp->p3 ){
89490 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
89491 pMem = &aMem[pOp->p3];
89492 }else
89493#endif
89494 {
89495 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
89496 }
89497
89498 if( rc ){
89499 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
89500 goto abort_due_to_error;
89501 }
89502 sqlite3VdbeChangeEncoding(pMem, encoding);
89503 UPDATE_MAX_BLOBSIZE(pMem);
89504 if( sqlite3VdbeMemTooBig(pMem) ){
89505 goto too_big;
89506 }
89507 break;
89508}
89509
89510#ifndef SQLITE_OMIT_WAL
89511/* Opcode: Checkpoint P1 P2 P3 * *
89512**
89513** Checkpoint database P1. This is a no-op if P1 is not currently in
89514** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
89515** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
89516** SQLITE_BUSY or not, respectively. Write the number of pages in the
89517** WAL after the checkpoint into mem[P3+1] and the number of pages
89518** in the WAL that have been checkpointed after the checkpoint
89519** completes into mem[P3+2]. However on an error, mem[P3+1] and
89520** mem[P3+2] are initialized to -1.
89521*/
89522case OP_Checkpoint: {
89523 int i; /* Loop counter */
89524 int aRes[3]; /* Results */
89525 Mem *pMem; /* Write results here */
89526
89527 assert( p->readOnly==0 );
89528 aRes[0] = 0;
89529 aRes[1] = aRes[2] = -1;
89530 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
89531 || pOp->p2==SQLITE_CHECKPOINT_FULL
89532 || pOp->p2==SQLITE_CHECKPOINT_RESTART
89533 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
89534 );
89535 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
89536 if( rc ){
89537 if( rc!=SQLITE_BUSY ) goto abort_due_to_error;
89538 rc = SQLITE_OK;
89539 aRes[0] = 1;
89540 }
89541 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
89542 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
89543 }
89544 break;
89545};
89546#endif
89547
89548#ifndef SQLITE_OMIT_PRAGMA
89549/* Opcode: JournalMode P1 P2 P3 * *
89550**
89551** Change the journal mode of database P1 to P3. P3 must be one of the
89552** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
89553** modes (delete, truncate, persist, off and memory), this is a simple
89554** operation. No IO is required.
89555**
89556** If changing into or out of WAL mode the procedure is more complicated.
89557**
89558** Write a string containing the final journal-mode to register P2.
89559*/
89560case OP_JournalMode: { /* out2 */
89561 Btree *pBt; /* Btree to change journal mode of */
89562 Pager *pPager; /* Pager associated with pBt */
89563 int eNew; /* New journal mode */
89564 int eOld; /* The old journal mode */
89565#ifndef SQLITE_OMIT_WAL
89566 const char *zFilename; /* Name of database file for pPager */
89567#endif
89568
89569 pOut = out2Prerelease(p, pOp);
89570 eNew = pOp->p3;
89571 assert( eNew==PAGER_JOURNALMODE_DELETE
89572 || eNew==PAGER_JOURNALMODE_TRUNCATE
89573 || eNew==PAGER_JOURNALMODE_PERSIST
89574 || eNew==PAGER_JOURNALMODE_OFF
89575 || eNew==PAGER_JOURNALMODE_MEMORY
89576 || eNew==PAGER_JOURNALMODE_WAL
89577 || eNew==PAGER_JOURNALMODE_QUERY
89578 );
89579 assert( pOp->p1>=0 && pOp->p1<db->nDb );
89580 assert( p->readOnly==0 );
89581
89582 pBt = db->aDb[pOp->p1].pBt;
89583 pPager = sqlite3BtreePager(pBt);
89584 eOld = sqlite3PagerGetJournalMode(pPager);
89585 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
89586 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
89587
89588#ifndef SQLITE_OMIT_WAL
89589 zFilename = sqlite3PagerFilename(pPager, 1);
89590
89591 /* Do not allow a transition to journal_mode=WAL for a database
89592 ** in temporary storage or if the VFS does not support shared memory
89593 */
89594 if( eNew==PAGER_JOURNALMODE_WAL
89595 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
89596 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
89597 ){
89598 eNew = eOld;
89599 }
89600
89601 if( (eNew!=eOld)
89602 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
89603 ){
89604 if( !db->autoCommit || db->nVdbeRead>1 ){
89605 rc = SQLITE_ERROR;
89606 sqlite3VdbeError(p,
89607 "cannot change %s wal mode from within a transaction",
89608 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
89609 );
89610 goto abort_due_to_error;
89611 }else{
89612
89613 if( eOld==PAGER_JOURNALMODE_WAL ){
89614 /* If leaving WAL mode, close the log file. If successful, the call
89615 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
89616 ** file. An EXCLUSIVE lock may still be held on the database file
89617 ** after a successful return.
89618 */
89619 rc = sqlite3PagerCloseWal(pPager, db);
89620 if( rc==SQLITE_OK ){
89621 sqlite3PagerSetJournalMode(pPager, eNew);
89622 }
89623 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
89624 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
89625 ** as an intermediate */
89626 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
89627 }
89628
89629 /* Open a transaction on the database file. Regardless of the journal
89630 ** mode, this transaction always uses a rollback journal.
89631 */
89632 assert( sqlite3BtreeIsInTrans(pBt)==0 );
89633 if( rc==SQLITE_OK ){
89634 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
89635 }
89636 }
89637 }
89638#endif /* ifndef SQLITE_OMIT_WAL */
89639
89640 if( rc ) eNew = eOld;
89641 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
89642
89643 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
89644 pOut->z = (char *)sqlite3JournalModename(eNew);
89645 pOut->n = sqlite3Strlen30(pOut->z);
89646 pOut->enc = SQLITE_UTF8;
89647 sqlite3VdbeChangeEncoding(pOut, encoding);
89648 if( rc ) goto abort_due_to_error;
89649 break;
89650};
89651#endif /* SQLITE_OMIT_PRAGMA */
89652
89653#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
89654/* Opcode: Vacuum P1 * * * *
89655**
89656** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
89657** for an attached database. The "temp" database may not be vacuumed.
89658*/
89659case OP_Vacuum: {
89660 assert( p->readOnly==0 );
89661 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1);
89662 if( rc ) goto abort_due_to_error;
89663 break;
89664}
89665#endif
89666
89667#if !defined(SQLITE_OMIT_AUTOVACUUM)
89668/* Opcode: IncrVacuum P1 P2 * * *
89669**
89670** Perform a single step of the incremental vacuum procedure on
89671** the P1 database. If the vacuum has finished, jump to instruction
89672** P2. Otherwise, fall through to the next instruction.
89673*/
89674case OP_IncrVacuum: { /* jump */
89675 Btree *pBt;
89676
89677 assert( pOp->p1>=0 && pOp->p1<db->nDb );
89678 assert( DbMaskTest(p->btreeMask, pOp->p1) );
89679 assert( p->readOnly==0 );
89680 pBt = db->aDb[pOp->p1].pBt;
89681 rc = sqlite3BtreeIncrVacuum(pBt);
89682 VdbeBranchTaken(rc==SQLITE_DONE,2);
89683 if( rc ){
89684 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
89685 rc = SQLITE_OK;
89686 goto jump_to_p2;
89687 }
89688 break;
89689}
89690#endif
89691
89692/* Opcode: Expire P1 P2 * * *
89693**
89694** Cause precompiled statements to expire. When an expired statement
89695** is executed using sqlite3_step() it will either automatically
89696** reprepare itself (if it was originally created using sqlite3_prepare_v2())
89697** or it will fail with SQLITE_SCHEMA.
89698**
89699** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
89700** then only the currently executing statement is expired.
89701**
89702** If P2 is 0, then SQL statements are expired immediately. If P2 is 1,
89703** then running SQL statements are allowed to continue to run to completion.
89704** The P2==1 case occurs when a CREATE INDEX or similar schema change happens
89705** that might help the statement run faster but which does not affect the
89706** correctness of operation.
89707*/
89708case OP_Expire: {
89709 assert( pOp->p2==0 || pOp->p2==1 );
89710 if( !pOp->p1 ){
89711 sqlite3ExpirePreparedStatements(db, pOp->p2);
89712 }else{
89713 p->expired = pOp->p2+1;
89714 }
89715 break;
89716}
89717
89718#ifndef SQLITE_OMIT_SHARED_CACHE
89719/* Opcode: TableLock P1 P2 P3 P4 *
89720** Synopsis: iDb=P1 root=P2 write=P3
89721**
89722** Obtain a lock on a particular table. This instruction is only used when
89723** the shared-cache feature is enabled.
89724**
89725** P1 is the index of the database in sqlite3.aDb[] of the database
89726** on which the lock is acquired. A readlock is obtained if P3==0 or
89727** a write lock if P3==1.
89728**
89729** P2 contains the root-page of the table to lock.
89730**
89731** P4 contains a pointer to the name of the table being locked. This is only
89732** used to generate an error message if the lock cannot be obtained.
89733*/
89734case OP_TableLock: {
89735 u8 isWriteLock = (u8)pOp->p3;
89736 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
89737 int p1 = pOp->p1;
89738 assert( p1>=0 && p1<db->nDb );
89739 assert( DbMaskTest(p->btreeMask, p1) );
89740 assert( isWriteLock==0 || isWriteLock==1 );
89741 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
89742 if( rc ){
89743 if( (rc&0xFF)==SQLITE_LOCKED ){
89744 const char *z = pOp->p4.z;
89745 sqlite3VdbeError(p, "database table is locked: %s", z);
89746 }
89747 goto abort_due_to_error;
89748 }
89749 }
89750 break;
89751}
89752#endif /* SQLITE_OMIT_SHARED_CACHE */
89753
89754#ifndef SQLITE_OMIT_VIRTUALTABLE
89755/* Opcode: VBegin * * * P4 *
89756**
89757** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
89758** xBegin method for that table.
89759**
89760** Also, whether or not P4 is set, check that this is not being called from
89761** within a callback to a virtual table xSync() method. If it is, the error
89762** code will be set to SQLITE_LOCKED.
89763*/
89764case OP_VBegin: {
89765 VTable *pVTab;
89766 pVTab = pOp->p4.pVtab;
89767 rc = sqlite3VtabBegin(db, pVTab);
89768 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
89769 if( rc ) goto abort_due_to_error;
89770 break;
89771}
89772#endif /* SQLITE_OMIT_VIRTUALTABLE */
89773
89774#ifndef SQLITE_OMIT_VIRTUALTABLE
89775/* Opcode: VCreate P1 P2 * * *
89776**
89777** P2 is a register that holds the name of a virtual table in database
89778** P1. Call the xCreate method for that table.
89779*/
89780case OP_VCreate: {
89781 Mem sMem; /* For storing the record being decoded */
89782 const char *zTab; /* Name of the virtual table */
89783
89784 memset(&sMem, 0, sizeof(sMem));
89785 sMem.db = db;
89786 /* Because P2 is always a static string, it is impossible for the
89787 ** sqlite3VdbeMemCopy() to fail */
89788 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
89789 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
89790 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
89791 assert( rc==SQLITE_OK );
89792 zTab = (const char*)sqlite3_value_text(&sMem);
89793 assert( zTab || db->mallocFailed );
89794 if( zTab ){
89795 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
89796 }
89797 sqlite3VdbeMemRelease(&sMem);
89798 if( rc ) goto abort_due_to_error;
89799 break;
89800}
89801#endif /* SQLITE_OMIT_VIRTUALTABLE */
89802
89803#ifndef SQLITE_OMIT_VIRTUALTABLE
89804/* Opcode: VDestroy P1 * * P4 *
89805**
89806** P4 is the name of a virtual table in database P1. Call the xDestroy method
89807** of that table.
89808*/
89809case OP_VDestroy: {
89810 db->nVDestroy++;
89811 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
89812 db->nVDestroy--;
89813 if( rc ) goto abort_due_to_error;
89814 break;
89815}
89816#endif /* SQLITE_OMIT_VIRTUALTABLE */
89817
89818#ifndef SQLITE_OMIT_VIRTUALTABLE
89819/* Opcode: VOpen P1 * * P4 *
89820**
89821** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
89822** P1 is a cursor number. This opcode opens a cursor to the virtual
89823** table and stores that cursor in P1.
89824*/
89825case OP_VOpen: {
89826 VdbeCursor *pCur;
89827 sqlite3_vtab_cursor *pVCur;
89828 sqlite3_vtab *pVtab;
89829 const sqlite3_module *pModule;
89830
89831 assert( p->bIsReader );
89832 pCur = 0;
89833 pVCur = 0;
89834 pVtab = pOp->p4.pVtab->pVtab;
89835 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
89836 rc = SQLITE_LOCKED;
89837 goto abort_due_to_error;
89838 }
89839 pModule = pVtab->pModule;
89840 rc = pModule->xOpen(pVtab, &pVCur);
89841 sqlite3VtabImportErrmsg(p, pVtab);
89842 if( rc ) goto abort_due_to_error;
89843
89844 /* Initialize sqlite3_vtab_cursor base class */
89845 pVCur->pVtab = pVtab;
89846
89847 /* Initialize vdbe cursor object */
89848 pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
89849 if( pCur ){
89850 pCur->uc.pVCur = pVCur;
89851 pVtab->nRef++;
89852 }else{
89853 assert( db->mallocFailed );
89854 pModule->xClose(pVCur);
89855 goto no_mem;
89856 }
89857 break;
89858}
89859#endif /* SQLITE_OMIT_VIRTUALTABLE */
89860
89861#ifndef SQLITE_OMIT_VIRTUALTABLE
89862/* Opcode: VFilter P1 P2 P3 P4 *
89863** Synopsis: iplan=r[P3] zplan='P4'
89864**
89865** P1 is a cursor opened using VOpen. P2 is an address to jump to if
89866** the filtered result set is empty.
89867**
89868** P4 is either NULL or a string that was generated by the xBestIndex
89869** method of the module. The interpretation of the P4 string is left
89870** to the module implementation.
89871**
89872** This opcode invokes the xFilter method on the virtual table specified
89873** by P1. The integer query plan parameter to xFilter is stored in register
89874** P3. Register P3+1 stores the argc parameter to be passed to the
89875** xFilter method. Registers P3+2..P3+1+argc are the argc
89876** additional parameters which are passed to
89877** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
89878**
89879** A jump is made to P2 if the result set after filtering would be empty.
89880*/
89881case OP_VFilter: { /* jump */
89882 int nArg;
89883 int iQuery;
89884 const sqlite3_module *pModule;
89885 Mem *pQuery;
89886 Mem *pArgc;
89887 sqlite3_vtab_cursor *pVCur;
89888 sqlite3_vtab *pVtab;
89889 VdbeCursor *pCur;
89890 int res;
89891 int i;
89892 Mem **apArg;
89893
89894 pQuery = &aMem[pOp->p3];
89895 pArgc = &pQuery[1];
89896 pCur = p->apCsr[pOp->p1];
89897 assert( memIsValid(pQuery) );
89898 REGISTER_TRACE(pOp->p3, pQuery);
89899 assert( pCur->eCurType==CURTYPE_VTAB );
89900 pVCur = pCur->uc.pVCur;
89901 pVtab = pVCur->pVtab;
89902 pModule = pVtab->pModule;
89903
89904 /* Grab the index number and argc parameters */
89905 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
89906 nArg = (int)pArgc->u.i;
89907 iQuery = (int)pQuery->u.i;
89908
89909 /* Invoke the xFilter method */
89910 res = 0;
89911 apArg = p->apArg;
89912 for(i = 0; i<nArg; i++){
89913 apArg[i] = &pArgc[i+1];
89914 }
89915 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
89916 sqlite3VtabImportErrmsg(p, pVtab);
89917 if( rc ) goto abort_due_to_error;
89918 res = pModule->xEof(pVCur);
89919 pCur->nullRow = 0;
89920 VdbeBranchTaken(res!=0,2);
89921 if( res ) goto jump_to_p2;
89922 break;
89923}
89924#endif /* SQLITE_OMIT_VIRTUALTABLE */
89925
89926#ifndef SQLITE_OMIT_VIRTUALTABLE
89927/* Opcode: VColumn P1 P2 P3 * P5
89928** Synopsis: r[P3]=vcolumn(P2)
89929**
89930** Store in register P3 the value of the P2-th column of
89931** the current row of the virtual-table of cursor P1.
89932**
89933** If the VColumn opcode is being used to fetch the value of
89934** an unchanging column during an UPDATE operation, then the P5
89935** value is OPFLAG_NOCHNG. This will cause the sqlite3_vtab_nochange()
89936** function to return true inside the xColumn method of the virtual
89937** table implementation. The P5 column might also contain other
89938** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are
89939** unused by OP_VColumn.
89940*/
89941case OP_VColumn: {
89942 sqlite3_vtab *pVtab;
89943 const sqlite3_module *pModule;
89944 Mem *pDest;
89945 sqlite3_context sContext;
89946
89947 VdbeCursor *pCur = p->apCsr[pOp->p1];
89948 assert( pCur->eCurType==CURTYPE_VTAB );
89949 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
89950 pDest = &aMem[pOp->p3];
89951 memAboutToChange(p, pDest);
89952 if( pCur->nullRow ){
89953 sqlite3VdbeMemSetNull(pDest);
89954 break;
89955 }
89956 pVtab = pCur->uc.pVCur->pVtab;
89957 pModule = pVtab->pModule;
89958 assert( pModule->xColumn );
89959 memset(&sContext, 0, sizeof(sContext));
89960 sContext.pOut = pDest;
89961 testcase( (pOp->p5 & OPFLAG_NOCHNG)==0 && pOp->p5!=0 );
89962 if( pOp->p5 & OPFLAG_NOCHNG ){
89963 sqlite3VdbeMemSetNull(pDest);
89964 pDest->flags = MEM_Null|MEM_Zero;
89965 pDest->u.nZero = 0;
89966 }else{
89967 MemSetTypeFlag(pDest, MEM_Null);
89968 }
89969 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
89970 sqlite3VtabImportErrmsg(p, pVtab);
89971 if( sContext.isError>0 ){
89972 sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest));
89973 rc = sContext.isError;
89974 }
89975 sqlite3VdbeChangeEncoding(pDest, encoding);
89976 REGISTER_TRACE(pOp->p3, pDest);
89977 UPDATE_MAX_BLOBSIZE(pDest);
89978
89979 if( sqlite3VdbeMemTooBig(pDest) ){
89980 goto too_big;
89981 }
89982 if( rc ) goto abort_due_to_error;
89983 break;
89984}
89985#endif /* SQLITE_OMIT_VIRTUALTABLE */
89986
89987#ifndef SQLITE_OMIT_VIRTUALTABLE
89988/* Opcode: VNext P1 P2 * * *
89989**
89990** Advance virtual table P1 to the next row in its result set and
89991** jump to instruction P2. Or, if the virtual table has reached
89992** the end of its result set, then fall through to the next instruction.
89993*/
89994case OP_VNext: { /* jump */
89995 sqlite3_vtab *pVtab;
89996 const sqlite3_module *pModule;
89997 int res;
89998 VdbeCursor *pCur;
89999
90000 res = 0;
90001 pCur = p->apCsr[pOp->p1];
90002 assert( pCur->eCurType==CURTYPE_VTAB );
90003 if( pCur->nullRow ){
90004 break;
90005 }
90006 pVtab = pCur->uc.pVCur->pVtab;
90007 pModule = pVtab->pModule;
90008 assert( pModule->xNext );
90009
90010 /* Invoke the xNext() method of the module. There is no way for the
90011 ** underlying implementation to return an error if one occurs during
90012 ** xNext(). Instead, if an error occurs, true is returned (indicating that
90013 ** data is available) and the error code returned when xColumn or
90014 ** some other method is next invoked on the save virtual table cursor.
90015 */
90016 rc = pModule->xNext(pCur->uc.pVCur);
90017 sqlite3VtabImportErrmsg(p, pVtab);
90018 if( rc ) goto abort_due_to_error;
90019 res = pModule->xEof(pCur->uc.pVCur);
90020 VdbeBranchTaken(!res,2);
90021 if( !res ){
90022 /* If there is data, jump to P2 */
90023 goto jump_to_p2_and_check_for_interrupt;
90024 }
90025 goto check_for_interrupt;
90026}
90027#endif /* SQLITE_OMIT_VIRTUALTABLE */
90028
90029#ifndef SQLITE_OMIT_VIRTUALTABLE
90030/* Opcode: VRename P1 * * P4 *
90031**
90032** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
90033** This opcode invokes the corresponding xRename method. The value
90034** in register P1 is passed as the zName argument to the xRename method.
90035*/
90036case OP_VRename: {
90037 sqlite3_vtab *pVtab;
90038 Mem *pName;
90039 int isLegacy;
90040
90041 isLegacy = (db->flags & SQLITE_LegacyAlter);
90042 db->flags |= SQLITE_LegacyAlter;
90043 pVtab = pOp->p4.pVtab->pVtab;
90044 pName = &aMem[pOp->p1];
90045 assert( pVtab->pModule->xRename );
90046 assert( memIsValid(pName) );
90047 assert( p->readOnly==0 );
90048 REGISTER_TRACE(pOp->p1, pName);
90049 assert( pName->flags & MEM_Str );
90050 testcase( pName->enc==SQLITE_UTF8 );
90051 testcase( pName->enc==SQLITE_UTF16BE );
90052 testcase( pName->enc==SQLITE_UTF16LE );
90053 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
90054 if( rc ) goto abort_due_to_error;
90055 rc = pVtab->pModule->xRename(pVtab, pName->z);
90056 if( isLegacy==0 ) db->flags &= ~SQLITE_LegacyAlter;
90057 sqlite3VtabImportErrmsg(p, pVtab);
90058 p->expired = 0;
90059 if( rc ) goto abort_due_to_error;
90060 break;
90061}
90062#endif
90063
90064#ifndef SQLITE_OMIT_VIRTUALTABLE
90065/* Opcode: VUpdate P1 P2 P3 P4 P5
90066** Synopsis: data=r[P3@P2]
90067**
90068** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
90069** This opcode invokes the corresponding xUpdate method. P2 values
90070** are contiguous memory cells starting at P3 to pass to the xUpdate
90071** invocation. The value in register (P3+P2-1) corresponds to the
90072** p2th element of the argv array passed to xUpdate.
90073**
90074** The xUpdate method will do a DELETE or an INSERT or both.
90075** The argv[0] element (which corresponds to memory cell P3)
90076** is the rowid of a row to delete. If argv[0] is NULL then no
90077** deletion occurs. The argv[1] element is the rowid of the new
90078** row. This can be NULL to have the virtual table select the new
90079** rowid for itself. The subsequent elements in the array are
90080** the values of columns in the new row.
90081**
90082** If P2==1 then no insert is performed. argv[0] is the rowid of
90083** a row to delete.
90084**
90085** P1 is a boolean flag. If it is set to true and the xUpdate call
90086** is successful, then the value returned by sqlite3_last_insert_rowid()
90087** is set to the value of the rowid for the row just inserted.
90088**
90089** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
90090** apply in the case of a constraint failure on an insert or update.
90091*/
90092case OP_VUpdate: {
90093 sqlite3_vtab *pVtab;
90094 const sqlite3_module *pModule;
90095 int nArg;
90096 int i;
90097 sqlite_int64 rowid;
90098 Mem **apArg;
90099 Mem *pX;
90100
90101 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
90102 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
90103 );
90104 assert( p->readOnly==0 );
90105 if( db->mallocFailed ) goto no_mem;
90106 sqlite3VdbeIncrWriteCounter(p, 0);
90107 pVtab = pOp->p4.pVtab->pVtab;
90108 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
90109 rc = SQLITE_LOCKED;
90110 goto abort_due_to_error;
90111 }
90112 pModule = pVtab->pModule;
90113 nArg = pOp->p2;
90114 assert( pOp->p4type==P4_VTAB );
90115 if( ALWAYS(pModule->xUpdate) ){
90116 u8 vtabOnConflict = db->vtabOnConflict;
90117 apArg = p->apArg;
90118 pX = &aMem[pOp->p3];
90119 for(i=0; i<nArg; i++){
90120 assert( memIsValid(pX) );
90121 memAboutToChange(p, pX);
90122 apArg[i] = pX;
90123 pX++;
90124 }
90125 db->vtabOnConflict = pOp->p5;
90126 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
90127 db->vtabOnConflict = vtabOnConflict;
90128 sqlite3VtabImportErrmsg(p, pVtab);
90129 if( rc==SQLITE_OK && pOp->p1 ){
90130 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
90131 db->lastRowid = rowid;
90132 }
90133 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
90134 if( pOp->p5==OE_Ignore ){
90135 rc = SQLITE_OK;
90136 }else{
90137 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
90138 }
90139 }else{
90140 p->nChange++;
90141 }
90142 if( rc ) goto abort_due_to_error;
90143 }
90144 break;
90145}
90146#endif /* SQLITE_OMIT_VIRTUALTABLE */
90147
90148#ifndef SQLITE_OMIT_PAGER_PRAGMAS
90149/* Opcode: Pagecount P1 P2 * * *
90150**
90151** Write the current number of pages in database P1 to memory cell P2.
90152*/
90153case OP_Pagecount: { /* out2 */
90154 pOut = out2Prerelease(p, pOp);
90155 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
90156 break;
90157}
90158#endif
90159
90160
90161#ifndef SQLITE_OMIT_PAGER_PRAGMAS
90162/* Opcode: MaxPgcnt P1 P2 P3 * *
90163**
90164** Try to set the maximum page count for database P1 to the value in P3.
90165** Do not let the maximum page count fall below the current page count and
90166** do not change the maximum page count value if P3==0.
90167**
90168** Store the maximum page count after the change in register P2.
90169*/
90170case OP_MaxPgcnt: { /* out2 */
90171 unsigned int newMax;
90172 Btree *pBt;
90173
90174 pOut = out2Prerelease(p, pOp);
90175 pBt = db->aDb[pOp->p1].pBt;
90176 newMax = 0;
90177 if( pOp->p3 ){
90178 newMax = sqlite3BtreeLastPage(pBt);
90179 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
90180 }
90181 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
90182 break;
90183}
90184#endif
90185
90186/* Opcode: Function0 P1 P2 P3 P4 P5
90187** Synopsis: r[P3]=func(r[P2@P5])
90188**
90189** Invoke a user function (P4 is a pointer to a FuncDef object that
90190** defines the function) with P5 arguments taken from register P2 and
90191** successors. The result of the function is stored in register P3.
90192** Register P3 must not be one of the function inputs.
90193**
90194** P1 is a 32-bit bitmask indicating whether or not each argument to the
90195** function was determined to be constant at compile time. If the first
90196** argument was constant then bit 0 of P1 is set. This is used to determine
90197** whether meta data associated with a user function argument using the
90198** sqlite3_set_auxdata() API may be safely retained until the next
90199** invocation of this opcode.
90200**
90201** See also: Function, AggStep, AggFinal
90202*/
90203/* Opcode: Function P1 P2 P3 P4 P5
90204** Synopsis: r[P3]=func(r[P2@P5])
90205**
90206** Invoke a user function (P4 is a pointer to an sqlite3_context object that
90207** contains a pointer to the function to be run) with P5 arguments taken
90208** from register P2 and successors. The result of the function is stored
90209** in register P3. Register P3 must not be one of the function inputs.
90210**
90211** P1 is a 32-bit bitmask indicating whether or not each argument to the
90212** function was determined to be constant at compile time. If the first
90213** argument was constant then bit 0 of P1 is set. This is used to determine
90214** whether meta data associated with a user function argument using the
90215** sqlite3_set_auxdata() API may be safely retained until the next
90216** invocation of this opcode.
90217**
90218** SQL functions are initially coded as OP_Function0 with P4 pointing
90219** to a FuncDef object. But on first evaluation, the P4 operand is
90220** automatically converted into an sqlite3_context object and the operation
90221** changed to this OP_Function opcode. In this way, the initialization of
90222** the sqlite3_context object occurs only once, rather than once for each
90223** evaluation of the function.
90224**
90225** See also: Function0, AggStep, AggFinal
90226*/
90227case OP_PureFunc0: /* group */
90228case OP_Function0: { /* group */
90229 int n;
90230 sqlite3_context *pCtx;
90231
90232 assert( pOp->p4type==P4_FUNCDEF );
90233 n = pOp->p5;
90234 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
90235 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
90236 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
90237 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
90238 if( pCtx==0 ) goto no_mem;
90239 pCtx->pOut = 0;
90240 pCtx->pFunc = pOp->p4.pFunc;
90241 pCtx->iOp = (int)(pOp - aOp);
90242 pCtx->pVdbe = p;
90243 pCtx->isError = 0;
90244 pCtx->argc = n;
90245 pOp->p4type = P4_FUNCCTX;
90246 pOp->p4.pCtx = pCtx;
90247 assert( OP_PureFunc == OP_PureFunc0+2 );
90248 assert( OP_Function == OP_Function0+2 );
90249 pOp->opcode += 2;
90250 /* Fall through into OP_Function */
90251}
90252case OP_PureFunc: /* group */
90253case OP_Function: { /* group */
90254 int i;
90255 sqlite3_context *pCtx;
90256
90257 assert( pOp->p4type==P4_FUNCCTX );
90258 pCtx = pOp->p4.pCtx;
90259
90260 /* If this function is inside of a trigger, the register array in aMem[]
90261 ** might change from one evaluation to the next. The next block of code
90262 ** checks to see if the register array has changed, and if so it
90263 ** reinitializes the relavant parts of the sqlite3_context object */
90264 pOut = &aMem[pOp->p3];
90265 if( pCtx->pOut != pOut ){
90266 pCtx->pOut = pOut;
90267 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
90268 }
90269
90270 memAboutToChange(p, pOut);
90271#ifdef SQLITE_DEBUG
90272 for(i=0; i<pCtx->argc; i++){
90273 assert( memIsValid(pCtx->argv[i]) );
90274 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
90275 }
90276#endif
90277 MemSetTypeFlag(pOut, MEM_Null);
90278 assert( pCtx->isError==0 );
90279 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
90280
90281 /* If the function returned an error, throw an exception */
90282 if( pCtx->isError ){
90283 if( pCtx->isError>0 ){
90284 sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
90285 rc = pCtx->isError;
90286 }
90287 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
90288 pCtx->isError = 0;
90289 if( rc ) goto abort_due_to_error;
90290 }
90291
90292 /* Copy the result of the function into register P3 */
90293 if( pOut->flags & (MEM_Str|MEM_Blob) ){
90294 sqlite3VdbeChangeEncoding(pOut, encoding);
90295 if( sqlite3VdbeMemTooBig(pOut) ) goto too_big;
90296 }
90297
90298 REGISTER_TRACE(pOp->p3, pOut);
90299 UPDATE_MAX_BLOBSIZE(pOut);
90300 break;
90301}
90302
90303/* Opcode: Trace P1 P2 * P4 *
90304**
90305** Write P4 on the statement trace output if statement tracing is
90306** enabled.
90307**
90308** Operand P1 must be 0x7fffffff and P2 must positive.
90309*/
90310/* Opcode: Init P1 P2 P3 P4 *
90311** Synopsis: Start at P2
90312**
90313** Programs contain a single instance of this opcode as the very first
90314** opcode.
90315**
90316** If tracing is enabled (by the sqlite3_trace()) interface, then
90317** the UTF-8 string contained in P4 is emitted on the trace callback.
90318** Or if P4 is blank, use the string returned by sqlite3_sql().
90319**
90320** If P2 is not zero, jump to instruction P2.
90321**
90322** Increment the value of P1 so that OP_Once opcodes will jump the
90323** first time they are evaluated for this run.
90324**
90325** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
90326** error is encountered.
90327*/
90328case OP_Trace:
90329case OP_Init: { /* jump */
90330 int i;
90331#ifndef SQLITE_OMIT_TRACE
90332 char *zTrace;
90333#endif
90334
90335 /* If the P4 argument is not NULL, then it must be an SQL comment string.
90336 ** The "--" string is broken up to prevent false-positives with srcck1.c.
90337 **
90338 ** This assert() provides evidence for:
90339 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
90340 ** would have been returned by the legacy sqlite3_trace() interface by
90341 ** using the X argument when X begins with "--" and invoking
90342 ** sqlite3_expanded_sql(P) otherwise.
90343 */
90344 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
90345
90346 /* OP_Init is always instruction 0 */
90347 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
90348
90349#ifndef SQLITE_OMIT_TRACE
90350 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
90351 && !p->doingRerun
90352 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
90353 ){
90354#ifndef SQLITE_OMIT_DEPRECATED
90355 if( db->mTrace & SQLITE_TRACE_LEGACY ){
90356 void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace;
90357 char *z = sqlite3VdbeExpandSql(p, zTrace);
90358 x(db->pTraceArg, z);
90359 sqlite3_free(z);
90360 }else
90361#endif
90362 if( db->nVdbeExec>1 ){
90363 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
90364 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
90365 sqlite3DbFree(db, z);
90366 }else{
90367 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
90368 }
90369 }
90370#ifdef SQLITE_USE_FCNTL_TRACE
90371 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
90372 if( zTrace ){
90373 int j;
90374 for(j=0; j<db->nDb; j++){
90375 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
90376 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
90377 }
90378 }
90379#endif /* SQLITE_USE_FCNTL_TRACE */
90380#ifdef SQLITE_DEBUG
90381 if( (db->flags & SQLITE_SqlTrace)!=0
90382 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
90383 ){
90384 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
90385 }
90386#endif /* SQLITE_DEBUG */
90387#endif /* SQLITE_OMIT_TRACE */
90388 assert( pOp->p2>0 );
90389 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
90390 if( pOp->opcode==OP_Trace ) break;
90391 for(i=1; i<p->nOp; i++){
90392 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
90393 }
90394 pOp->p1 = 0;
90395 }
90396 pOp->p1++;
90397 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
90398 goto jump_to_p2;
90399}
90400
90401#ifdef SQLITE_ENABLE_CURSOR_HINTS
90402/* Opcode: CursorHint P1 * * P4 *
90403**
90404** Provide a hint to cursor P1 that it only needs to return rows that
90405** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
90406** to values currently held in registers. TK_COLUMN terms in the P4
90407** expression refer to columns in the b-tree to which cursor P1 is pointing.
90408*/
90409case OP_CursorHint: {
90410 VdbeCursor *pC;
90411
90412 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90413 assert( pOp->p4type==P4_EXPR );
90414 pC = p->apCsr[pOp->p1];
90415 if( pC ){
90416 assert( pC->eCurType==CURTYPE_BTREE );
90417 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
90418 pOp->p4.pExpr, aMem);
90419 }
90420 break;
90421}
90422#endif /* SQLITE_ENABLE_CURSOR_HINTS */
90423
90424#ifdef SQLITE_DEBUG
90425/* Opcode: Abortable * * * * *
90426**
90427** Verify that an Abort can happen. Assert if an Abort at this point
90428** might cause database corruption. This opcode only appears in debugging
90429** builds.
90430**
90431** An Abort is safe if either there have been no writes, or if there is
90432** an active statement journal.
90433*/
90434case OP_Abortable: {
90435 sqlite3VdbeAssertAbortable(p);
90436 break;
90437}
90438#endif
90439
90440/* Opcode: Noop * * * * *
90441**
90442** Do nothing. This instruction is often useful as a jump
90443** destination.
90444*/
90445/*
90446** The magic Explain opcode are only inserted when explain==2 (which
90447** is to say when the EXPLAIN QUERY PLAN syntax is used.)
90448** This opcode records information from the optimizer. It is the
90449** the same as a no-op. This opcodesnever appears in a real VM program.
90450*/
90451default: { /* This is really OP_Noop, OP_Explain */
90452 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
90453
90454 break;
90455}
90456
90457/*****************************************************************************
90458** The cases of the switch statement above this line should all be indented
90459** by 6 spaces. But the left-most 6 spaces have been removed to improve the
90460** readability. From this point on down, the normal indentation rules are
90461** restored.
90462*****************************************************************************/
90463 }
90464
90465#ifdef VDBE_PROFILE
90466 {
90467 u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
90468 if( endTime>start ) pOrigOp->cycles += endTime - start;
90469 pOrigOp->cnt++;
90470 }
90471#endif
90472
90473 /* The following code adds nothing to the actual functionality
90474 ** of the program. It is only here for testing and debugging.
90475 ** On the other hand, it does burn CPU cycles every time through
90476 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
90477 */
90478#ifndef NDEBUG
90479 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
90480
90481#ifdef SQLITE_DEBUG
90482 if( db->flags & SQLITE_VdbeTrace ){
90483 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
90484 if( rc!=0 ) printf("rc=%d\n",rc);
90485 if( opProperty & (OPFLG_OUT2) ){
90486 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
90487 }
90488 if( opProperty & OPFLG_OUT3 ){
90489 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
90490 }
90491 }
90492#endif /* SQLITE_DEBUG */
90493#endif /* NDEBUG */
90494 } /* The end of the for(;;) loop the loops through opcodes */
90495
90496 /* If we reach this point, it means that execution is finished with
90497 ** an error of some kind.
90498 */
90499abort_due_to_error:
90500 if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT;
90501 assert( rc );
90502 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
90503 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
90504 }
90505 p->rc = rc;
90506 sqlite3SystemError(db, rc);
90507 testcase( sqlite3GlobalConfig.xLog!=0 );
90508 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
90509 (int)(pOp - aOp), p->zSql, p->zErrMsg);
90510 sqlite3VdbeHalt(p);
90511 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
90512 rc = SQLITE_ERROR;
90513 if( resetSchemaOnFault>0 ){
90514 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
90515 }
90516
90517 /* This is the only way out of this procedure. We have to
90518 ** release the mutexes on btrees that were acquired at the
90519 ** top. */
90520vdbe_return:
90521 testcase( nVmStep>0 );
90522 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
90523 sqlite3VdbeLeave(p);
90524 assert( rc!=SQLITE_OK || nExtraDelete==0
90525 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
90526 );
90527 return rc;
90528
90529 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
90530 ** is encountered.
90531 */
90532too_big:
90533 sqlite3VdbeError(p, "string or blob too big");
90534 rc = SQLITE_TOOBIG;
90535 goto abort_due_to_error;
90536
90537 /* Jump to here if a malloc() fails.
90538 */
90539no_mem:
90540 sqlite3OomFault(db);
90541 sqlite3VdbeError(p, "out of memory");
90542 rc = SQLITE_NOMEM_BKPT;
90543 goto abort_due_to_error;
90544
90545 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
90546 ** flag.
90547 */
90548abort_due_to_interrupt:
90549 assert( db->u1.isInterrupted );
90550 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
90551 p->rc = rc;
90552 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
90553 goto abort_due_to_error;
90554}
90555
90556
90557/************** End of vdbe.c ************************************************/
90558/************** Begin file vdbeblob.c ****************************************/
90559/*
90560** 2007 May 1
90561**
90562** The author disclaims copyright to this source code. In place of
90563** a legal notice, here is a blessing:
90564**
90565** May you do good and not evil.
90566** May you find forgiveness for yourself and forgive others.
90567** May you share freely, never taking more than you give.
90568**
90569*************************************************************************
90570**
90571** This file contains code used to implement incremental BLOB I/O.
90572*/
90573
90574/* #include "sqliteInt.h" */
90575/* #include "vdbeInt.h" */
90576
90577#ifndef SQLITE_OMIT_INCRBLOB
90578
90579/*
90580** Valid sqlite3_blob* handles point to Incrblob structures.
90581*/
90582typedef struct Incrblob Incrblob;
90583struct Incrblob {
90584 int nByte; /* Size of open blob, in bytes */
90585 int iOffset; /* Byte offset of blob in cursor data */
90586 u16 iCol; /* Table column this handle is open on */
90587 BtCursor *pCsr; /* Cursor pointing at blob row */
90588 sqlite3_stmt *pStmt; /* Statement holding cursor open */
90589 sqlite3 *db; /* The associated database */
90590 char *zDb; /* Database name */
90591 Table *pTab; /* Table object */
90592};
90593
90594
90595/*
90596** This function is used by both blob_open() and blob_reopen(). It seeks
90597** the b-tree cursor associated with blob handle p to point to row iRow.
90598** If successful, SQLITE_OK is returned and subsequent calls to
90599** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
90600**
90601** If an error occurs, or if the specified row does not exist or does not
90602** contain a value of type TEXT or BLOB in the column nominated when the
90603** blob handle was opened, then an error code is returned and *pzErr may
90604** be set to point to a buffer containing an error message. It is the
90605** responsibility of the caller to free the error message buffer using
90606** sqlite3DbFree().
90607**
90608** If an error does occur, then the b-tree cursor is closed. All subsequent
90609** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will
90610** immediately return SQLITE_ABORT.
90611*/
90612static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
90613 int rc; /* Error code */
90614 char *zErr = 0; /* Error message */
90615 Vdbe *v = (Vdbe *)p->pStmt;
90616
90617 /* Set the value of register r[1] in the SQL statement to integer iRow.
90618 ** This is done directly as a performance optimization
90619 */
90620 v->aMem[1].flags = MEM_Int;
90621 v->aMem[1].u.i = iRow;
90622
90623 /* If the statement has been run before (and is paused at the OP_ResultRow)
90624 ** then back it up to the point where it does the OP_NotExists. This could
90625 ** have been down with an extra OP_Goto, but simply setting the program
90626 ** counter is faster. */
90627 if( v->pc>4 ){
90628 v->pc = 4;
90629 assert( v->aOp[v->pc].opcode==OP_NotExists );
90630 rc = sqlite3VdbeExec(v);
90631 }else{
90632 rc = sqlite3_step(p->pStmt);
90633 }
90634 if( rc==SQLITE_ROW ){
90635 VdbeCursor *pC = v->apCsr[0];
90636 u32 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
90637 testcase( pC->nHdrParsed==p->iCol );
90638 testcase( pC->nHdrParsed==p->iCol+1 );
90639 if( type<12 ){
90640 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
90641 type==0?"null": type==7?"real": "integer"
90642 );
90643 rc = SQLITE_ERROR;
90644 sqlite3_finalize(p->pStmt);
90645 p->pStmt = 0;
90646 }else{
90647 p->iOffset = pC->aType[p->iCol + pC->nField];
90648 p->nByte = sqlite3VdbeSerialTypeLen(type);
90649 p->pCsr = pC->uc.pCursor;
90650 sqlite3BtreeIncrblobCursor(p->pCsr);
90651 }
90652 }
90653
90654 if( rc==SQLITE_ROW ){
90655 rc = SQLITE_OK;
90656 }else if( p->pStmt ){
90657 rc = sqlite3_finalize(p->pStmt);
90658 p->pStmt = 0;
90659 if( rc==SQLITE_OK ){
90660 zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
90661 rc = SQLITE_ERROR;
90662 }else{
90663 zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
90664 }
90665 }
90666
90667 assert( rc!=SQLITE_OK || zErr==0 );
90668 assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE );
90669
90670 *pzErr = zErr;
90671 return rc;
90672}
90673
90674/*
90675** Open a blob handle.
90676*/
90677SQLITE_API int sqlite3_blob_open(
90678 sqlite3* db, /* The database connection */
90679 const char *zDb, /* The attached database containing the blob */
90680 const char *zTable, /* The table containing the blob */
90681 const char *zColumn, /* The column containing the blob */
90682 sqlite_int64 iRow, /* The row containing the glob */
90683 int wrFlag, /* True -> read/write access, false -> read-only */
90684 sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */
90685){
90686 int nAttempt = 0;
90687 int iCol; /* Index of zColumn in row-record */
90688 int rc = SQLITE_OK;
90689 char *zErr = 0;
90690 Table *pTab;
90691 Incrblob *pBlob = 0;
90692 Parse sParse;
90693
90694#ifdef SQLITE_ENABLE_API_ARMOR
90695 if( ppBlob==0 ){
90696 return SQLITE_MISUSE_BKPT;
90697 }
90698#endif
90699 *ppBlob = 0;
90700#ifdef SQLITE_ENABLE_API_ARMOR
90701 if( !sqlite3SafetyCheckOk(db) || zTable==0 ){
90702 return SQLITE_MISUSE_BKPT;
90703 }
90704#endif
90705 wrFlag = !!wrFlag; /* wrFlag = (wrFlag ? 1 : 0); */
90706
90707 sqlite3_mutex_enter(db->mutex);
90708
90709 pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
90710 do {
90711 memset(&sParse, 0, sizeof(Parse));
90712 if( !pBlob ) goto blob_open_out;
90713 sParse.db = db;
90714 sqlite3DbFree(db, zErr);
90715 zErr = 0;
90716
90717 sqlite3BtreeEnterAll(db);
90718 pTab = sqlite3LocateTable(&sParse, 0, zTable, zDb);
90719 if( pTab && IsVirtual(pTab) ){
90720 pTab = 0;
90721 sqlite3ErrorMsg(&sParse, "cannot open virtual table: %s", zTable);
90722 }
90723 if( pTab && !HasRowid(pTab) ){
90724 pTab = 0;
90725 sqlite3ErrorMsg(&sParse, "cannot open table without rowid: %s", zTable);
90726 }
90727#ifndef SQLITE_OMIT_VIEW
90728 if( pTab && pTab->pSelect ){
90729 pTab = 0;
90730 sqlite3ErrorMsg(&sParse, "cannot open view: %s", zTable);
90731 }
90732#endif
90733 if( !pTab ){
90734 if( sParse.zErrMsg ){
90735 sqlite3DbFree(db, zErr);
90736 zErr = sParse.zErrMsg;
90737 sParse.zErrMsg = 0;
90738 }
90739 rc = SQLITE_ERROR;
90740 sqlite3BtreeLeaveAll(db);
90741 goto blob_open_out;
90742 }
90743 pBlob->pTab = pTab;
90744 pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
90745
90746 /* Now search pTab for the exact column. */
90747 for(iCol=0; iCol<pTab->nCol; iCol++) {
90748 if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
90749 break;
90750 }
90751 }
90752 if( iCol==pTab->nCol ){
90753 sqlite3DbFree(db, zErr);
90754 zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
90755 rc = SQLITE_ERROR;
90756 sqlite3BtreeLeaveAll(db);
90757 goto blob_open_out;
90758 }
90759
90760 /* If the value is being opened for writing, check that the
90761 ** column is not indexed, and that it is not part of a foreign key.
90762 */
90763 if( wrFlag ){
90764 const char *zFault = 0;
90765 Index *pIdx;
90766#ifndef SQLITE_OMIT_FOREIGN_KEY
90767 if( db->flags&SQLITE_ForeignKeys ){
90768 /* Check that the column is not part of an FK child key definition. It
90769 ** is not necessary to check if it is part of a parent key, as parent
90770 ** key columns must be indexed. The check below will pick up this
90771 ** case. */
90772 FKey *pFKey;
90773 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
90774 int j;
90775 for(j=0; j<pFKey->nCol; j++){
90776 if( pFKey->aCol[j].iFrom==iCol ){
90777 zFault = "foreign key";
90778 }
90779 }
90780 }
90781 }
90782#endif
90783 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
90784 int j;
90785 for(j=0; j<pIdx->nKeyCol; j++){
90786 /* FIXME: Be smarter about indexes that use expressions */
90787 if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){
90788 zFault = "indexed";
90789 }
90790 }
90791 }
90792 if( zFault ){
90793 sqlite3DbFree(db, zErr);
90794 zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
90795 rc = SQLITE_ERROR;
90796 sqlite3BtreeLeaveAll(db);
90797 goto blob_open_out;
90798 }
90799 }
90800
90801 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(&sParse);
90802 assert( pBlob->pStmt || db->mallocFailed );
90803 if( pBlob->pStmt ){
90804
90805 /* This VDBE program seeks a btree cursor to the identified
90806 ** db/table/row entry. The reason for using a vdbe program instead
90807 ** of writing code to use the b-tree layer directly is that the
90808 ** vdbe program will take advantage of the various transaction,
90809 ** locking and error handling infrastructure built into the vdbe.
90810 **
90811 ** After seeking the cursor, the vdbe executes an OP_ResultRow.
90812 ** Code external to the Vdbe then "borrows" the b-tree cursor and
90813 ** uses it to implement the blob_read(), blob_write() and
90814 ** blob_bytes() functions.
90815 **
90816 ** The sqlite3_blob_close() function finalizes the vdbe program,
90817 ** which closes the b-tree cursor and (possibly) commits the
90818 ** transaction.
90819 */
90820 static const int iLn = VDBE_OFFSET_LINENO(2);
90821 static const VdbeOpList openBlob[] = {
90822 {OP_TableLock, 0, 0, 0}, /* 0: Acquire a read or write lock */
90823 {OP_OpenRead, 0, 0, 0}, /* 1: Open a cursor */
90824 /* blobSeekToRow() will initialize r[1] to the desired rowid */
90825 {OP_NotExists, 0, 5, 1}, /* 2: Seek the cursor to rowid=r[1] */
90826 {OP_Column, 0, 0, 1}, /* 3 */
90827 {OP_ResultRow, 1, 0, 0}, /* 4 */
90828 {OP_Halt, 0, 0, 0}, /* 5 */
90829 };
90830 Vdbe *v = (Vdbe *)pBlob->pStmt;
90831 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
90832 VdbeOp *aOp;
90833
90834 sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, wrFlag,
90835 pTab->pSchema->schema_cookie,
90836 pTab->pSchema->iGeneration);
90837 sqlite3VdbeChangeP5(v, 1);
90838 assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed );
90839 aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
90840
90841 /* Make sure a mutex is held on the table to be accessed */
90842 sqlite3VdbeUsesBtree(v, iDb);
90843
90844 if( db->mallocFailed==0 ){
90845 assert( aOp!=0 );
90846 /* Configure the OP_TableLock instruction */
90847#ifdef SQLITE_OMIT_SHARED_CACHE
90848 aOp[0].opcode = OP_Noop;
90849#else
90850 aOp[0].p1 = iDb;
90851 aOp[0].p2 = pTab->tnum;
90852 aOp[0].p3 = wrFlag;
90853 sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
90854 }
90855 if( db->mallocFailed==0 ){
90856#endif
90857
90858 /* Remove either the OP_OpenWrite or OpenRead. Set the P2
90859 ** parameter of the other to pTab->tnum. */
90860 if( wrFlag ) aOp[1].opcode = OP_OpenWrite;
90861 aOp[1].p2 = pTab->tnum;
90862 aOp[1].p3 = iDb;
90863
90864 /* Configure the number of columns. Configure the cursor to
90865 ** think that the table has one more column than it really
90866 ** does. An OP_Column to retrieve this imaginary column will
90867 ** always return an SQL NULL. This is useful because it means
90868 ** we can invoke OP_Column to fill in the vdbe cursors type
90869 ** and offset cache without causing any IO.
90870 */
90871 aOp[1].p4type = P4_INT32;
90872 aOp[1].p4.i = pTab->nCol+1;
90873 aOp[3].p2 = pTab->nCol;
90874
90875 sParse.nVar = 0;
90876 sParse.nMem = 1;
90877 sParse.nTab = 1;
90878 sqlite3VdbeMakeReady(v, &sParse);
90879 }
90880 }
90881
90882 pBlob->iCol = iCol;
90883 pBlob->db = db;
90884 sqlite3BtreeLeaveAll(db);
90885 if( db->mallocFailed ){
90886 goto blob_open_out;
90887 }
90888 rc = blobSeekToRow(pBlob, iRow, &zErr);
90889 } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA );
90890
90891blob_open_out:
90892 if( rc==SQLITE_OK && db->mallocFailed==0 ){
90893 *ppBlob = (sqlite3_blob *)pBlob;
90894 }else{
90895 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
90896 sqlite3DbFree(db, pBlob);
90897 }
90898 sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
90899 sqlite3DbFree(db, zErr);
90900 sqlite3ParserReset(&sParse);
90901 rc = sqlite3ApiExit(db, rc);
90902 sqlite3_mutex_leave(db->mutex);
90903 return rc;
90904}
90905
90906/*
90907** Close a blob handle that was previously created using
90908** sqlite3_blob_open().
90909*/
90910SQLITE_API int sqlite3_blob_close(sqlite3_blob *pBlob){
90911 Incrblob *p = (Incrblob *)pBlob;
90912 int rc;
90913 sqlite3 *db;
90914
90915 if( p ){
90916 db = p->db;
90917 sqlite3_mutex_enter(db->mutex);
90918 rc = sqlite3_finalize(p->pStmt);
90919 sqlite3DbFree(db, p);
90920 sqlite3_mutex_leave(db->mutex);
90921 }else{
90922 rc = SQLITE_OK;
90923 }
90924 return rc;
90925}
90926
90927/*
90928** Perform a read or write operation on a blob
90929*/
90930static int blobReadWrite(
90931 sqlite3_blob *pBlob,
90932 void *z,
90933 int n,
90934 int iOffset,
90935 int (*xCall)(BtCursor*, u32, u32, void*)
90936){
90937 int rc;
90938 Incrblob *p = (Incrblob *)pBlob;
90939 Vdbe *v;
90940 sqlite3 *db;
90941
90942 if( p==0 ) return SQLITE_MISUSE_BKPT;
90943 db = p->db;
90944 sqlite3_mutex_enter(db->mutex);
90945 v = (Vdbe*)p->pStmt;
90946
90947 if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
90948 /* Request is out of range. Return a transient error. */
90949 rc = SQLITE_ERROR;
90950 }else if( v==0 ){
90951 /* If there is no statement handle, then the blob-handle has
90952 ** already been invalidated. Return SQLITE_ABORT in this case.
90953 */
90954 rc = SQLITE_ABORT;
90955 }else{
90956 /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
90957 ** returned, clean-up the statement handle.
90958 */
90959 assert( db == v->db );
90960 sqlite3BtreeEnterCursor(p->pCsr);
90961
90962#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
90963 if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
90964 /* If a pre-update hook is registered and this is a write cursor,
90965 ** invoke it here.
90966 **
90967 ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
90968 ** operation should really be an SQLITE_UPDATE. This is probably
90969 ** incorrect, but is convenient because at this point the new.* values
90970 ** are not easily obtainable. And for the sessions module, an
90971 ** SQLITE_UPDATE where the PK columns do not change is handled in the
90972 ** same way as an SQLITE_DELETE (the SQLITE_DELETE code is actually
90973 ** slightly more efficient). Since you cannot write to a PK column
90974 ** using the incremental-blob API, this works. For the sessions module
90975 ** anyhow.
90976 */
90977 sqlite3_int64 iKey;
90978 iKey = sqlite3BtreeIntegerKey(p->pCsr);
90979 sqlite3VdbePreUpdateHook(
90980 v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1
90981 );
90982 }
90983#endif
90984
90985 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
90986 sqlite3BtreeLeaveCursor(p->pCsr);
90987 if( rc==SQLITE_ABORT ){
90988 sqlite3VdbeFinalize(v);
90989 p->pStmt = 0;
90990 }else{
90991 v->rc = rc;
90992 }
90993 }
90994 sqlite3Error(db, rc);
90995 rc = sqlite3ApiExit(db, rc);
90996 sqlite3_mutex_leave(db->mutex);
90997 return rc;
90998}
90999
91000/*
91001** Read data from a blob handle.
91002*/
91003SQLITE_API int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
91004 return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreePayloadChecked);
91005}
91006
91007/*
91008** Write data to a blob handle.
91009*/
91010SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
91011 return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
91012}
91013
91014/*
91015** Query a blob handle for the size of the data.
91016**
91017** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
91018** so no mutex is required for access.
91019*/
91020SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *pBlob){
91021 Incrblob *p = (Incrblob *)pBlob;
91022 return (p && p->pStmt) ? p->nByte : 0;
91023}
91024
91025/*
91026** Move an existing blob handle to point to a different row of the same
91027** database table.
91028**
91029** If an error occurs, or if the specified row does not exist or does not
91030** contain a blob or text value, then an error code is returned and the
91031** database handle error code and message set. If this happens, then all
91032** subsequent calls to sqlite3_blob_xxx() functions (except blob_close())
91033** immediately return SQLITE_ABORT.
91034*/
91035SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
91036 int rc;
91037 Incrblob *p = (Incrblob *)pBlob;
91038 sqlite3 *db;
91039
91040 if( p==0 ) return SQLITE_MISUSE_BKPT;
91041 db = p->db;
91042 sqlite3_mutex_enter(db->mutex);
91043
91044 if( p->pStmt==0 ){
91045 /* If there is no statement handle, then the blob-handle has
91046 ** already been invalidated. Return SQLITE_ABORT in this case.
91047 */
91048 rc = SQLITE_ABORT;
91049 }else{
91050 char *zErr;
91051 rc = blobSeekToRow(p, iRow, &zErr);
91052 if( rc!=SQLITE_OK ){
91053 sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
91054 sqlite3DbFree(db, zErr);
91055 }
91056 assert( rc!=SQLITE_SCHEMA );
91057 }
91058
91059 rc = sqlite3ApiExit(db, rc);
91060 assert( rc==SQLITE_OK || p->pStmt==0 );
91061 sqlite3_mutex_leave(db->mutex);
91062 return rc;
91063}
91064
91065#endif /* #ifndef SQLITE_OMIT_INCRBLOB */
91066
91067/************** End of vdbeblob.c ********************************************/
91068/************** Begin file vdbesort.c ****************************************/
91069/*
91070** 2011-07-09
91071**
91072** The author disclaims copyright to this source code. In place of
91073** a legal notice, here is a blessing:
91074**
91075** May you do good and not evil.
91076** May you find forgiveness for yourself and forgive others.
91077** May you share freely, never taking more than you give.
91078**
91079*************************************************************************
91080** This file contains code for the VdbeSorter object, used in concert with
91081** a VdbeCursor to sort large numbers of keys for CREATE INDEX statements
91082** or by SELECT statements with ORDER BY clauses that cannot be satisfied
91083** using indexes and without LIMIT clauses.
91084**
91085** The VdbeSorter object implements a multi-threaded external merge sort
91086** algorithm that is efficient even if the number of elements being sorted
91087** exceeds the available memory.
91088**
91089** Here is the (internal, non-API) interface between this module and the
91090** rest of the SQLite system:
91091**
91092** sqlite3VdbeSorterInit() Create a new VdbeSorter object.
91093**
91094** sqlite3VdbeSorterWrite() Add a single new row to the VdbeSorter
91095** object. The row is a binary blob in the
91096** OP_MakeRecord format that contains both
91097** the ORDER BY key columns and result columns
91098** in the case of a SELECT w/ ORDER BY, or
91099** the complete record for an index entry
91100** in the case of a CREATE INDEX.
91101**
91102** sqlite3VdbeSorterRewind() Sort all content previously added.
91103** Position the read cursor on the
91104** first sorted element.
91105**
91106** sqlite3VdbeSorterNext() Advance the read cursor to the next sorted
91107** element.
91108**
91109** sqlite3VdbeSorterRowkey() Return the complete binary blob for the
91110** row currently under the read cursor.
91111**
91112** sqlite3VdbeSorterCompare() Compare the binary blob for the row
91113** currently under the read cursor against
91114** another binary blob X and report if
91115** X is strictly less than the read cursor.
91116** Used to enforce uniqueness in a
91117** CREATE UNIQUE INDEX statement.
91118**
91119** sqlite3VdbeSorterClose() Close the VdbeSorter object and reclaim
91120** all resources.
91121**
91122** sqlite3VdbeSorterReset() Refurbish the VdbeSorter for reuse. This
91123** is like Close() followed by Init() only
91124** much faster.
91125**
91126** The interfaces above must be called in a particular order. Write() can
91127** only occur in between Init()/Reset() and Rewind(). Next(), Rowkey(), and
91128** Compare() can only occur in between Rewind() and Close()/Reset(). i.e.
91129**
91130** Init()
91131** for each record: Write()
91132** Rewind()
91133** Rowkey()/Compare()
91134** Next()
91135** Close()
91136**
91137** Algorithm:
91138**
91139** Records passed to the sorter via calls to Write() are initially held
91140** unsorted in main memory. Assuming the amount of memory used never exceeds
91141** a threshold, when Rewind() is called the set of records is sorted using
91142** an in-memory merge sort. In this case, no temporary files are required
91143** and subsequent calls to Rowkey(), Next() and Compare() read records
91144** directly from main memory.
91145**
91146** If the amount of space used to store records in main memory exceeds the
91147** threshold, then the set of records currently in memory are sorted and
91148** written to a temporary file in "Packed Memory Array" (PMA) format.
91149** A PMA created at this point is known as a "level-0 PMA". Higher levels
91150** of PMAs may be created by merging existing PMAs together - for example
91151** merging two or more level-0 PMAs together creates a level-1 PMA.
91152**
91153** The threshold for the amount of main memory to use before flushing
91154** records to a PMA is roughly the same as the limit configured for the
91155** page-cache of the main database. Specifically, the threshold is set to
91156** the value returned by "PRAGMA main.page_size" multipled by
91157** that returned by "PRAGMA main.cache_size", in bytes.
91158**
91159** If the sorter is running in single-threaded mode, then all PMAs generated
91160** are appended to a single temporary file. Or, if the sorter is running in
91161** multi-threaded mode then up to (N+1) temporary files may be opened, where
91162** N is the configured number of worker threads. In this case, instead of
91163** sorting the records and writing the PMA to a temporary file itself, the
91164** calling thread usually launches a worker thread to do so. Except, if
91165** there are already N worker threads running, the main thread does the work
91166** itself.
91167**
91168** The sorter is running in multi-threaded mode if (a) the library was built
91169** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
91170** than zero, and (b) worker threads have been enabled at runtime by calling
91171** "PRAGMA threads=N" with some value of N greater than 0.
91172**
91173** When Rewind() is called, any data remaining in memory is flushed to a
91174** final PMA. So at this point the data is stored in some number of sorted
91175** PMAs within temporary files on disk.
91176**
91177** If there are fewer than SORTER_MAX_MERGE_COUNT PMAs in total and the
91178** sorter is running in single-threaded mode, then these PMAs are merged
91179** incrementally as keys are retreived from the sorter by the VDBE. The
91180** MergeEngine object, described in further detail below, performs this
91181** merge.
91182**
91183** Or, if running in multi-threaded mode, then a background thread is
91184** launched to merge the existing PMAs. Once the background thread has
91185** merged T bytes of data into a single sorted PMA, the main thread
91186** begins reading keys from that PMA while the background thread proceeds
91187** with merging the next T bytes of data. And so on.
91188**
91189** Parameter T is set to half the value of the memory threshold used
91190** by Write() above to determine when to create a new PMA.
91191**
91192** If there are more than SORTER_MAX_MERGE_COUNT PMAs in total when
91193** Rewind() is called, then a hierarchy of incremental-merges is used.
91194** First, T bytes of data from the first SORTER_MAX_MERGE_COUNT PMAs on
91195** disk are merged together. Then T bytes of data from the second set, and
91196** so on, such that no operation ever merges more than SORTER_MAX_MERGE_COUNT
91197** PMAs at a time. This done is to improve locality.
91198**
91199** If running in multi-threaded mode and there are more than
91200** SORTER_MAX_MERGE_COUNT PMAs on disk when Rewind() is called, then more
91201** than one background thread may be created. Specifically, there may be
91202** one background thread for each temporary file on disk, and one background
91203** thread to merge the output of each of the others to a single PMA for
91204** the main thread to read from.
91205*/
91206/* #include "sqliteInt.h" */
91207/* #include "vdbeInt.h" */
91208
91209/*
91210** If SQLITE_DEBUG_SORTER_THREADS is defined, this module outputs various
91211** messages to stderr that may be helpful in understanding the performance
91212** characteristics of the sorter in multi-threaded mode.
91213*/
91214#if 0
91215# define SQLITE_DEBUG_SORTER_THREADS 1
91216#endif
91217
91218/*
91219** Hard-coded maximum amount of data to accumulate in memory before flushing
91220** to a level 0 PMA. The purpose of this limit is to prevent various integer
91221** overflows. 512MiB.
91222*/
91223#define SQLITE_MAX_PMASZ (1<<29)
91224
91225/*
91226** Private objects used by the sorter
91227*/
91228typedef struct MergeEngine MergeEngine; /* Merge PMAs together */
91229typedef struct PmaReader PmaReader; /* Incrementally read one PMA */
91230typedef struct PmaWriter PmaWriter; /* Incrementally write one PMA */
91231typedef struct SorterRecord SorterRecord; /* A record being sorted */
91232typedef struct SortSubtask SortSubtask; /* A sub-task in the sort process */
91233typedef struct SorterFile SorterFile; /* Temporary file object wrapper */
91234typedef struct SorterList SorterList; /* In-memory list of records */
91235typedef struct IncrMerger IncrMerger; /* Read & merge multiple PMAs */
91236
91237/*
91238** A container for a temp file handle and the current amount of data
91239** stored in the file.
91240*/
91241struct SorterFile {
91242 sqlite3_file *pFd; /* File handle */
91243 i64 iEof; /* Bytes of data stored in pFd */
91244};
91245
91246/*
91247** An in-memory list of objects to be sorted.
91248**
91249** If aMemory==0 then each object is allocated separately and the objects
91250** are connected using SorterRecord.u.pNext. If aMemory!=0 then all objects
91251** are stored in the aMemory[] bulk memory, one right after the other, and
91252** are connected using SorterRecord.u.iNext.
91253*/
91254struct SorterList {
91255 SorterRecord *pList; /* Linked list of records */
91256 u8 *aMemory; /* If non-NULL, bulk memory to hold pList */
91257 int szPMA; /* Size of pList as PMA in bytes */
91258};
91259
91260/*
91261** The MergeEngine object is used to combine two or more smaller PMAs into
91262** one big PMA using a merge operation. Separate PMAs all need to be
91263** combined into one big PMA in order to be able to step through the sorted
91264** records in order.
91265**
91266** The aReadr[] array contains a PmaReader object for each of the PMAs being
91267** merged. An aReadr[] object either points to a valid key or else is at EOF.
91268** ("EOF" means "End Of File". When aReadr[] is at EOF there is no more data.)
91269** For the purposes of the paragraphs below, we assume that the array is
91270** actually N elements in size, where N is the smallest power of 2 greater
91271** to or equal to the number of PMAs being merged. The extra aReadr[] elements
91272** are treated as if they are empty (always at EOF).
91273**
91274** The aTree[] array is also N elements in size. The value of N is stored in
91275** the MergeEngine.nTree variable.
91276**
91277** The final (N/2) elements of aTree[] contain the results of comparing
91278** pairs of PMA keys together. Element i contains the result of
91279** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the
91280** aTree element is set to the index of it.
91281**
91282** For the purposes of this comparison, EOF is considered greater than any
91283** other key value. If the keys are equal (only possible with two EOF
91284** values), it doesn't matter which index is stored.
91285**
91286** The (N/4) elements of aTree[] that precede the final (N/2) described
91287** above contains the index of the smallest of each block of 4 PmaReaders
91288** And so on. So that aTree[1] contains the index of the PmaReader that
91289** currently points to the smallest key value. aTree[0] is unused.
91290**
91291** Example:
91292**
91293** aReadr[0] -> Banana
91294** aReadr[1] -> Feijoa
91295** aReadr[2] -> Elderberry
91296** aReadr[3] -> Currant
91297** aReadr[4] -> Grapefruit
91298** aReadr[5] -> Apple
91299** aReadr[6] -> Durian
91300** aReadr[7] -> EOF
91301**
91302** aTree[] = { X, 5 0, 5 0, 3, 5, 6 }
91303**
91304** The current element is "Apple" (the value of the key indicated by
91305** PmaReader 5). When the Next() operation is invoked, PmaReader 5 will
91306** be advanced to the next key in its segment. Say the next key is
91307** "Eggplant":
91308**
91309** aReadr[5] -> Eggplant
91310**
91311** The contents of aTree[] are updated first by comparing the new PmaReader
91312** 5 key to the current key of PmaReader 4 (still "Grapefruit"). The PmaReader
91313** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
91314** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader
91315** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
91316** so the value written into element 1 of the array is 0. As follows:
91317**
91318** aTree[] = { X, 0 0, 6 0, 3, 5, 6 }
91319**
91320** In other words, each time we advance to the next sorter element, log2(N)
91321** key comparison operations are required, where N is the number of segments
91322** being merged (rounded up to the next power of 2).
91323*/
91324struct MergeEngine {
91325 int nTree; /* Used size of aTree/aReadr (power of 2) */
91326 SortSubtask *pTask; /* Used by this thread only */
91327 int *aTree; /* Current state of incremental merge */
91328 PmaReader *aReadr; /* Array of PmaReaders to merge data from */
91329};
91330
91331/*
91332** This object represents a single thread of control in a sort operation.
91333** Exactly VdbeSorter.nTask instances of this object are allocated
91334** as part of each VdbeSorter object. Instances are never allocated any
91335** other way. VdbeSorter.nTask is set to the number of worker threads allowed
91336** (see SQLITE_CONFIG_WORKER_THREADS) plus one (the main thread). Thus for
91337** single-threaded operation, there is exactly one instance of this object
91338** and for multi-threaded operation there are two or more instances.
91339**
91340** Essentially, this structure contains all those fields of the VdbeSorter
91341** structure for which each thread requires a separate instance. For example,
91342** each thread requries its own UnpackedRecord object to unpack records in
91343** as part of comparison operations.
91344**
91345** Before a background thread is launched, variable bDone is set to 0. Then,
91346** right before it exits, the thread itself sets bDone to 1. This is used for
91347** two purposes:
91348**
91349** 1. When flushing the contents of memory to a level-0 PMA on disk, to
91350** attempt to select a SortSubtask for which there is not already an
91351** active background thread (since doing so causes the main thread
91352** to block until it finishes).
91353**
91354** 2. If SQLITE_DEBUG_SORTER_THREADS is defined, to determine if a call
91355** to sqlite3ThreadJoin() is likely to block. Cases that are likely to
91356** block provoke debugging output.
91357**
91358** In both cases, the effects of the main thread seeing (bDone==0) even
91359** after the thread has finished are not dire. So we don't worry about
91360** memory barriers and such here.
91361*/
91362typedef int (*SorterCompare)(SortSubtask*,int*,const void*,int,const void*,int);
91363struct SortSubtask {
91364 SQLiteThread *pThread; /* Background thread, if any */
91365 int bDone; /* Set if thread is finished but not joined */
91366 VdbeSorter *pSorter; /* Sorter that owns this sub-task */
91367 UnpackedRecord *pUnpacked; /* Space to unpack a record */
91368 SorterList list; /* List for thread to write to a PMA */
91369 int nPMA; /* Number of PMAs currently in file */
91370 SorterCompare xCompare; /* Compare function to use */
91371 SorterFile file; /* Temp file for level-0 PMAs */
91372 SorterFile file2; /* Space for other PMAs */
91373};
91374
91375
91376/*
91377** Main sorter structure. A single instance of this is allocated for each
91378** sorter cursor created by the VDBE.
91379**
91380** mxKeysize:
91381** As records are added to the sorter by calls to sqlite3VdbeSorterWrite(),
91382** this variable is updated so as to be set to the size on disk of the
91383** largest record in the sorter.
91384*/
91385struct VdbeSorter {
91386 int mnPmaSize; /* Minimum PMA size, in bytes */
91387 int mxPmaSize; /* Maximum PMA size, in bytes. 0==no limit */
91388 int mxKeysize; /* Largest serialized key seen so far */
91389 int pgsz; /* Main database page size */
91390 PmaReader *pReader; /* Readr data from here after Rewind() */
91391 MergeEngine *pMerger; /* Or here, if bUseThreads==0 */
91392 sqlite3 *db; /* Database connection */
91393 KeyInfo *pKeyInfo; /* How to compare records */
91394 UnpackedRecord *pUnpacked; /* Used by VdbeSorterCompare() */
91395 SorterList list; /* List of in-memory records */
91396 int iMemory; /* Offset of free space in list.aMemory */
91397 int nMemory; /* Size of list.aMemory allocation in bytes */
91398 u8 bUsePMA; /* True if one or more PMAs created */
91399 u8 bUseThreads; /* True to use background threads */
91400 u8 iPrev; /* Previous thread used to flush PMA */
91401 u8 nTask; /* Size of aTask[] array */
91402 u8 typeMask;
91403 SortSubtask aTask[1]; /* One or more subtasks */
91404};
91405
91406#define SORTER_TYPE_INTEGER 0x01
91407#define SORTER_TYPE_TEXT 0x02
91408
91409/*
91410** An instance of the following object is used to read records out of a
91411** PMA, in sorted order. The next key to be read is cached in nKey/aKey.
91412** aKey might point into aMap or into aBuffer. If neither of those locations
91413** contain a contiguous representation of the key, then aAlloc is allocated
91414** and the key is copied into aAlloc and aKey is made to poitn to aAlloc.
91415**
91416** pFd==0 at EOF.
91417*/
91418struct PmaReader {
91419 i64 iReadOff; /* Current read offset */
91420 i64 iEof; /* 1 byte past EOF for this PmaReader */
91421 int nAlloc; /* Bytes of space at aAlloc */
91422 int nKey; /* Number of bytes in key */
91423 sqlite3_file *pFd; /* File handle we are reading from */
91424 u8 *aAlloc; /* Space for aKey if aBuffer and pMap wont work */
91425 u8 *aKey; /* Pointer to current key */
91426 u8 *aBuffer; /* Current read buffer */
91427 int nBuffer; /* Size of read buffer in bytes */
91428 u8 *aMap; /* Pointer to mapping of entire file */
91429 IncrMerger *pIncr; /* Incremental merger */
91430};
91431
91432/*
91433** Normally, a PmaReader object iterates through an existing PMA stored
91434** within a temp file. However, if the PmaReader.pIncr variable points to
91435** an object of the following type, it may be used to iterate/merge through
91436** multiple PMAs simultaneously.
91437**
91438** There are two types of IncrMerger object - single (bUseThread==0) and
91439** multi-threaded (bUseThread==1).
91440**
91441** A multi-threaded IncrMerger object uses two temporary files - aFile[0]
91442** and aFile[1]. Neither file is allowed to grow to more than mxSz bytes in
91443** size. When the IncrMerger is initialized, it reads enough data from
91444** pMerger to populate aFile[0]. It then sets variables within the
91445** corresponding PmaReader object to read from that file and kicks off
91446** a background thread to populate aFile[1] with the next mxSz bytes of
91447** sorted record data from pMerger.
91448**
91449** When the PmaReader reaches the end of aFile[0], it blocks until the
91450** background thread has finished populating aFile[1]. It then exchanges
91451** the contents of the aFile[0] and aFile[1] variables within this structure,
91452** sets the PmaReader fields to read from the new aFile[0] and kicks off
91453** another background thread to populate the new aFile[1]. And so on, until
91454** the contents of pMerger are exhausted.
91455**
91456** A single-threaded IncrMerger does not open any temporary files of its
91457** own. Instead, it has exclusive access to mxSz bytes of space beginning
91458** at offset iStartOff of file pTask->file2. And instead of using a
91459** background thread to prepare data for the PmaReader, with a single
91460** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with
91461** keys from pMerger by the calling thread whenever the PmaReader runs out
91462** of data.
91463*/
91464struct IncrMerger {
91465 SortSubtask *pTask; /* Task that owns this merger */
91466 MergeEngine *pMerger; /* Merge engine thread reads data from */
91467 i64 iStartOff; /* Offset to start writing file at */
91468 int mxSz; /* Maximum bytes of data to store */
91469 int bEof; /* Set to true when merge is finished */
91470 int bUseThread; /* True to use a bg thread for this object */
91471 SorterFile aFile[2]; /* aFile[0] for reading, [1] for writing */
91472};
91473
91474/*
91475** An instance of this object is used for writing a PMA.
91476**
91477** The PMA is written one record at a time. Each record is of an arbitrary
91478** size. But I/O is more efficient if it occurs in page-sized blocks where
91479** each block is aligned on a page boundary. This object caches writes to
91480** the PMA so that aligned, page-size blocks are written.
91481*/
91482struct PmaWriter {
91483 int eFWErr; /* Non-zero if in an error state */
91484 u8 *aBuffer; /* Pointer to write buffer */
91485 int nBuffer; /* Size of write buffer in bytes */
91486 int iBufStart; /* First byte of buffer to write */
91487 int iBufEnd; /* Last byte of buffer to write */
91488 i64 iWriteOff; /* Offset of start of buffer in file */
91489 sqlite3_file *pFd; /* File handle to write to */
91490};
91491
91492/*
91493** This object is the header on a single record while that record is being
91494** held in memory and prior to being written out as part of a PMA.
91495**
91496** How the linked list is connected depends on how memory is being managed
91497** by this module. If using a separate allocation for each in-memory record
91498** (VdbeSorter.list.aMemory==0), then the list is always connected using the
91499** SorterRecord.u.pNext pointers.
91500**
91501** Or, if using the single large allocation method (VdbeSorter.list.aMemory!=0),
91502** then while records are being accumulated the list is linked using the
91503** SorterRecord.u.iNext offset. This is because the aMemory[] array may
91504** be sqlite3Realloc()ed while records are being accumulated. Once the VM
91505** has finished passing records to the sorter, or when the in-memory buffer
91506** is full, the list is sorted. As part of the sorting process, it is
91507** converted to use the SorterRecord.u.pNext pointers. See function
91508** vdbeSorterSort() for details.
91509*/
91510struct SorterRecord {
91511 int nVal; /* Size of the record in bytes */
91512 union {
91513 SorterRecord *pNext; /* Pointer to next record in list */
91514 int iNext; /* Offset within aMemory of next record */
91515 } u;
91516 /* The data for the record immediately follows this header */
91517};
91518
91519/* Return a pointer to the buffer containing the record data for SorterRecord
91520** object p. Should be used as if:
91521**
91522** void *SRVAL(SorterRecord *p) { return (void*)&p[1]; }
91523*/
91524#define SRVAL(p) ((void*)((SorterRecord*)(p) + 1))
91525
91526
91527/* Maximum number of PMAs that a single MergeEngine can merge */
91528#define SORTER_MAX_MERGE_COUNT 16
91529
91530static int vdbeIncrSwap(IncrMerger*);
91531static void vdbeIncrFree(IncrMerger *);
91532
91533/*
91534** Free all memory belonging to the PmaReader object passed as the
91535** argument. All structure fields are set to zero before returning.
91536*/
91537static void vdbePmaReaderClear(PmaReader *pReadr){
91538 sqlite3_free(pReadr->aAlloc);
91539 sqlite3_free(pReadr->aBuffer);
91540 if( pReadr->aMap ) sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
91541 vdbeIncrFree(pReadr->pIncr);
91542 memset(pReadr, 0, sizeof(PmaReader));
91543}
91544
91545/*
91546** Read the next nByte bytes of data from the PMA p.
91547** If successful, set *ppOut to point to a buffer containing the data
91548** and return SQLITE_OK. Otherwise, if an error occurs, return an SQLite
91549** error code.
91550**
91551** The buffer returned in *ppOut is only valid until the
91552** next call to this function.
91553*/
91554static int vdbePmaReadBlob(
91555 PmaReader *p, /* PmaReader from which to take the blob */
91556 int nByte, /* Bytes of data to read */
91557 u8 **ppOut /* OUT: Pointer to buffer containing data */
91558){
91559 int iBuf; /* Offset within buffer to read from */
91560 int nAvail; /* Bytes of data available in buffer */
91561
91562 if( p->aMap ){
91563 *ppOut = &p->aMap[p->iReadOff];
91564 p->iReadOff += nByte;
91565 return SQLITE_OK;
91566 }
91567
91568 assert( p->aBuffer );
91569
91570 /* If there is no more data to be read from the buffer, read the next
91571 ** p->nBuffer bytes of data from the file into it. Or, if there are less
91572 ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */
91573 iBuf = p->iReadOff % p->nBuffer;
91574 if( iBuf==0 ){
91575 int nRead; /* Bytes to read from disk */
91576 int rc; /* sqlite3OsRead() return code */
91577
91578 /* Determine how many bytes of data to read. */
91579 if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
91580 nRead = p->nBuffer;
91581 }else{
91582 nRead = (int)(p->iEof - p->iReadOff);
91583 }
91584 assert( nRead>0 );
91585
91586 /* Readr data from the file. Return early if an error occurs. */
91587 rc = sqlite3OsRead(p->pFd, p->aBuffer, nRead, p->iReadOff);
91588 assert( rc!=SQLITE_IOERR_SHORT_READ );
91589 if( rc!=SQLITE_OK ) return rc;
91590 }
91591 nAvail = p->nBuffer - iBuf;
91592
91593 if( nByte<=nAvail ){
91594 /* The requested data is available in the in-memory buffer. In this
91595 ** case there is no need to make a copy of the data, just return a
91596 ** pointer into the buffer to the caller. */
91597 *ppOut = &p->aBuffer[iBuf];
91598 p->iReadOff += nByte;
91599 }else{
91600 /* The requested data is not all available in the in-memory buffer.
91601 ** In this case, allocate space at p->aAlloc[] to copy the requested
91602 ** range into. Then return a copy of pointer p->aAlloc to the caller. */
91603 int nRem; /* Bytes remaining to copy */
91604
91605 /* Extend the p->aAlloc[] allocation if required. */
91606 if( p->nAlloc<nByte ){
91607 u8 *aNew;
91608 int nNew = MAX(128, p->nAlloc*2);
91609 while( nByte>nNew ) nNew = nNew*2;
91610 aNew = sqlite3Realloc(p->aAlloc, nNew);
91611 if( !aNew ) return SQLITE_NOMEM_BKPT;
91612 p->nAlloc = nNew;
91613 p->aAlloc = aNew;
91614 }
91615
91616 /* Copy as much data as is available in the buffer into the start of
91617 ** p->aAlloc[]. */
91618 memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
91619 p->iReadOff += nAvail;
91620 nRem = nByte - nAvail;
91621
91622 /* The following loop copies up to p->nBuffer bytes per iteration into
91623 ** the p->aAlloc[] buffer. */
91624 while( nRem>0 ){
91625 int rc; /* vdbePmaReadBlob() return code */
91626 int nCopy; /* Number of bytes to copy */
91627 u8 *aNext; /* Pointer to buffer to copy data from */
91628
91629 nCopy = nRem;
91630 if( nRem>p->nBuffer ) nCopy = p->nBuffer;
91631 rc = vdbePmaReadBlob(p, nCopy, &aNext);
91632 if( rc!=SQLITE_OK ) return rc;
91633 assert( aNext!=p->aAlloc );
91634 memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
91635 nRem -= nCopy;
91636 }
91637
91638 *ppOut = p->aAlloc;
91639 }
91640
91641 return SQLITE_OK;
91642}
91643
91644/*
91645** Read a varint from the stream of data accessed by p. Set *pnOut to
91646** the value read.
91647*/
91648static int vdbePmaReadVarint(PmaReader *p, u64 *pnOut){
91649 int iBuf;
91650
91651 if( p->aMap ){
91652 p->iReadOff += sqlite3GetVarint(&p->aMap[p->iReadOff], pnOut);
91653 }else{
91654 iBuf = p->iReadOff % p->nBuffer;
91655 if( iBuf && (p->nBuffer-iBuf)>=9 ){
91656 p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
91657 }else{
91658 u8 aVarint[16], *a;
91659 int i = 0, rc;
91660 do{
91661 rc = vdbePmaReadBlob(p, 1, &a);
91662 if( rc ) return rc;
91663 aVarint[(i++)&0xf] = a[0];
91664 }while( (a[0]&0x80)!=0 );
91665 sqlite3GetVarint(aVarint, pnOut);
91666 }
91667 }
91668
91669 return SQLITE_OK;
91670}
91671
91672/*
91673** Attempt to memory map file pFile. If successful, set *pp to point to the
91674** new mapping and return SQLITE_OK. If the mapping is not attempted
91675** (because the file is too large or the VFS layer is configured not to use
91676** mmap), return SQLITE_OK and set *pp to NULL.
91677**
91678** Or, if an error occurs, return an SQLite error code. The final value of
91679** *pp is undefined in this case.
91680*/
91681static int vdbeSorterMapFile(SortSubtask *pTask, SorterFile *pFile, u8 **pp){
91682 int rc = SQLITE_OK;
91683 if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){
91684 sqlite3_file *pFd = pFile->pFd;
91685 if( pFd->pMethods->iVersion>=3 ){
91686 rc = sqlite3OsFetch(pFd, 0, (int)pFile->iEof, (void**)pp);
91687 testcase( rc!=SQLITE_OK );
91688 }
91689 }
91690 return rc;
91691}
91692
91693/*
91694** Attach PmaReader pReadr to file pFile (if it is not already attached to
91695** that file) and seek it to offset iOff within the file. Return SQLITE_OK
91696** if successful, or an SQLite error code if an error occurs.
91697*/
91698static int vdbePmaReaderSeek(
91699 SortSubtask *pTask, /* Task context */
91700 PmaReader *pReadr, /* Reader whose cursor is to be moved */
91701 SorterFile *pFile, /* Sorter file to read from */
91702 i64 iOff /* Offset in pFile */
91703){
91704 int rc = SQLITE_OK;
91705
91706 assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 );
91707
91708 if( sqlite3FaultSim(201) ) return SQLITE_IOERR_READ;
91709 if( pReadr->aMap ){
91710 sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
91711 pReadr->aMap = 0;
91712 }
91713 pReadr->iReadOff = iOff;
91714 pReadr->iEof = pFile->iEof;
91715 pReadr->pFd = pFile->pFd;
91716
91717 rc = vdbeSorterMapFile(pTask, pFile, &pReadr->aMap);
91718 if( rc==SQLITE_OK && pReadr->aMap==0 ){
91719 int pgsz = pTask->pSorter->pgsz;
91720 int iBuf = pReadr->iReadOff % pgsz;
91721 if( pReadr->aBuffer==0 ){
91722 pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz);
91723 if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT;
91724 pReadr->nBuffer = pgsz;
91725 }
91726 if( rc==SQLITE_OK && iBuf ){
91727 int nRead = pgsz - iBuf;
91728 if( (pReadr->iReadOff + nRead) > pReadr->iEof ){
91729 nRead = (int)(pReadr->iEof - pReadr->iReadOff);
91730 }
91731 rc = sqlite3OsRead(
91732 pReadr->pFd, &pReadr->aBuffer[iBuf], nRead, pReadr->iReadOff
91733 );
91734 testcase( rc!=SQLITE_OK );
91735 }
91736 }
91737
91738 return rc;
91739}
91740
91741/*
91742** Advance PmaReader pReadr to the next key in its PMA. Return SQLITE_OK if
91743** no error occurs, or an SQLite error code if one does.
91744*/
91745static int vdbePmaReaderNext(PmaReader *pReadr){
91746 int rc = SQLITE_OK; /* Return Code */
91747 u64 nRec = 0; /* Size of record in bytes */
91748
91749
91750 if( pReadr->iReadOff>=pReadr->iEof ){
91751 IncrMerger *pIncr = pReadr->pIncr;
91752 int bEof = 1;
91753 if( pIncr ){
91754 rc = vdbeIncrSwap(pIncr);
91755 if( rc==SQLITE_OK && pIncr->bEof==0 ){
91756 rc = vdbePmaReaderSeek(
91757 pIncr->pTask, pReadr, &pIncr->aFile[0], pIncr->iStartOff
91758 );
91759 bEof = 0;
91760 }
91761 }
91762
91763 if( bEof ){
91764 /* This is an EOF condition */
91765 vdbePmaReaderClear(pReadr);
91766 testcase( rc!=SQLITE_OK );
91767 return rc;
91768 }
91769 }
91770
91771 if( rc==SQLITE_OK ){
91772 rc = vdbePmaReadVarint(pReadr, &nRec);
91773 }
91774 if( rc==SQLITE_OK ){
91775 pReadr->nKey = (int)nRec;
91776 rc = vdbePmaReadBlob(pReadr, (int)nRec, &pReadr->aKey);
91777 testcase( rc!=SQLITE_OK );
91778 }
91779
91780 return rc;
91781}
91782
91783/*
91784** Initialize PmaReader pReadr to scan through the PMA stored in file pFile
91785** starting at offset iStart and ending at offset iEof-1. This function
91786** leaves the PmaReader pointing to the first key in the PMA (or EOF if the
91787** PMA is empty).
91788**
91789** If the pnByte parameter is NULL, then it is assumed that the file
91790** contains a single PMA, and that that PMA omits the initial length varint.
91791*/
91792static int vdbePmaReaderInit(
91793 SortSubtask *pTask, /* Task context */
91794 SorterFile *pFile, /* Sorter file to read from */
91795 i64 iStart, /* Start offset in pFile */
91796 PmaReader *pReadr, /* PmaReader to populate */
91797 i64 *pnByte /* IN/OUT: Increment this value by PMA size */
91798){
91799 int rc;
91800
91801 assert( pFile->iEof>iStart );
91802 assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 );
91803 assert( pReadr->aBuffer==0 );
91804 assert( pReadr->aMap==0 );
91805
91806 rc = vdbePmaReaderSeek(pTask, pReadr, pFile, iStart);
91807 if( rc==SQLITE_OK ){
91808 u64 nByte = 0; /* Size of PMA in bytes */
91809 rc = vdbePmaReadVarint(pReadr, &nByte);
91810 pReadr->iEof = pReadr->iReadOff + nByte;
91811 *pnByte += nByte;
91812 }
91813
91814 if( rc==SQLITE_OK ){
91815 rc = vdbePmaReaderNext(pReadr);
91816 }
91817 return rc;
91818}
91819
91820/*
91821** A version of vdbeSorterCompare() that assumes that it has already been
91822** determined that the first field of key1 is equal to the first field of
91823** key2.
91824*/
91825static int vdbeSorterCompareTail(
91826 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
91827 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
91828 const void *pKey1, int nKey1, /* Left side of comparison */
91829 const void *pKey2, int nKey2 /* Right side of comparison */
91830){
91831 UnpackedRecord *r2 = pTask->pUnpacked;
91832 if( *pbKey2Cached==0 ){
91833 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
91834 *pbKey2Cached = 1;
91835 }
91836 return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, r2, 1);
91837}
91838
91839/*
91840** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2,
91841** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
91842** used by the comparison. Return the result of the comparison.
91843**
91844** If IN/OUT parameter *pbKey2Cached is true when this function is called,
91845** it is assumed that (pTask->pUnpacked) contains the unpacked version
91846** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
91847** version of key2 and *pbKey2Cached set to true before returning.
91848**
91849** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
91850** to SQLITE_NOMEM.
91851*/
91852static int vdbeSorterCompare(
91853 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
91854 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
91855 const void *pKey1, int nKey1, /* Left side of comparison */
91856 const void *pKey2, int nKey2 /* Right side of comparison */
91857){
91858 UnpackedRecord *r2 = pTask->pUnpacked;
91859 if( !*pbKey2Cached ){
91860 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
91861 *pbKey2Cached = 1;
91862 }
91863 return sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
91864}
91865
91866/*
91867** A specially optimized version of vdbeSorterCompare() that assumes that
91868** the first field of each key is a TEXT value and that the collation
91869** sequence to compare them with is BINARY.
91870*/
91871static int vdbeSorterCompareText(
91872 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
91873 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
91874 const void *pKey1, int nKey1, /* Left side of comparison */
91875 const void *pKey2, int nKey2 /* Right side of comparison */
91876){
91877 const u8 * const p1 = (const u8 * const)pKey1;
91878 const u8 * const p2 = (const u8 * const)pKey2;
91879 const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */
91880 const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */
91881
91882 int n1;
91883 int n2;
91884 int res;
91885
91886 getVarint32(&p1[1], n1);
91887 getVarint32(&p2[1], n2);
91888 res = memcmp(v1, v2, (MIN(n1, n2) - 13)/2);
91889 if( res==0 ){
91890 res = n1 - n2;
91891 }
91892
91893 if( res==0 ){
91894 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
91895 res = vdbeSorterCompareTail(
91896 pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
91897 );
91898 }
91899 }else{
91900 if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){
91901 res = res * -1;
91902 }
91903 }
91904
91905 return res;
91906}
91907
91908/*
91909** A specially optimized version of vdbeSorterCompare() that assumes that
91910** the first field of each key is an INTEGER value.
91911*/
91912static int vdbeSorterCompareInt(
91913 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
91914 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
91915 const void *pKey1, int nKey1, /* Left side of comparison */
91916 const void *pKey2, int nKey2 /* Right side of comparison */
91917){
91918 const u8 * const p1 = (const u8 * const)pKey1;
91919 const u8 * const p2 = (const u8 * const)pKey2;
91920 const int s1 = p1[1]; /* Left hand serial type */
91921 const int s2 = p2[1]; /* Right hand serial type */
91922 const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */
91923 const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */
91924 int res; /* Return value */
91925
91926 assert( (s1>0 && s1<7) || s1==8 || s1==9 );
91927 assert( (s2>0 && s2<7) || s2==8 || s2==9 );
91928
91929 if( s1==s2 ){
91930 /* The two values have the same sign. Compare using memcmp(). */
91931 static const u8 aLen[] = {0, 1, 2, 3, 4, 6, 8, 0, 0, 0 };
91932 const u8 n = aLen[s1];
91933 int i;
91934 res = 0;
91935 for(i=0; i<n; i++){
91936 if( (res = v1[i] - v2[i])!=0 ){
91937 if( ((v1[0] ^ v2[0]) & 0x80)!=0 ){
91938 res = v1[0] & 0x80 ? -1 : +1;
91939 }
91940 break;
91941 }
91942 }
91943 }else if( s1>7 && s2>7 ){
91944 res = s1 - s2;
91945 }else{
91946 if( s2>7 ){
91947 res = +1;
91948 }else if( s1>7 ){
91949 res = -1;
91950 }else{
91951 res = s1 - s2;
91952 }
91953 assert( res!=0 );
91954
91955 if( res>0 ){
91956 if( *v1 & 0x80 ) res = -1;
91957 }else{
91958 if( *v2 & 0x80 ) res = +1;
91959 }
91960 }
91961
91962 if( res==0 ){
91963 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
91964 res = vdbeSorterCompareTail(
91965 pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
91966 );
91967 }
91968 }else if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){
91969 res = res * -1;
91970 }
91971
91972 return res;
91973}
91974
91975/*
91976** Initialize the temporary index cursor just opened as a sorter cursor.
91977**
91978** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nKeyField)
91979** to determine the number of fields that should be compared from the
91980** records being sorted. However, if the value passed as argument nField
91981** is non-zero and the sorter is able to guarantee a stable sort, nField
91982** is used instead. This is used when sorting records for a CREATE INDEX
91983** statement. In this case, keys are always delivered to the sorter in
91984** order of the primary key, which happens to be make up the final part
91985** of the records being sorted. So if the sort is stable, there is never
91986** any reason to compare PK fields and they can be ignored for a small
91987** performance boost.
91988**
91989** The sorter can guarantee a stable sort when running in single-threaded
91990** mode, but not in multi-threaded mode.
91991**
91992** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
91993*/
91994SQLITE_PRIVATE int sqlite3VdbeSorterInit(
91995 sqlite3 *db, /* Database connection (for malloc()) */
91996 int nField, /* Number of key fields in each record */
91997 VdbeCursor *pCsr /* Cursor that holds the new sorter */
91998){
91999 int pgsz; /* Page size of main database */
92000 int i; /* Used to iterate through aTask[] */
92001 VdbeSorter *pSorter; /* The new sorter */
92002 KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */
92003 int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */
92004 int sz; /* Size of pSorter in bytes */
92005 int rc = SQLITE_OK;
92006#if SQLITE_MAX_WORKER_THREADS==0
92007# define nWorker 0
92008#else
92009 int nWorker;
92010#endif
92011
92012 /* Initialize the upper limit on the number of worker threads */
92013#if SQLITE_MAX_WORKER_THREADS>0
92014 if( sqlite3TempInMemory(db) || sqlite3GlobalConfig.bCoreMutex==0 ){
92015 nWorker = 0;
92016 }else{
92017 nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS];
92018 }
92019#endif
92020
92021 /* Do not allow the total number of threads (main thread + all workers)
92022 ** to exceed the maximum merge count */
92023#if SQLITE_MAX_WORKER_THREADS>=SORTER_MAX_MERGE_COUNT
92024 if( nWorker>=SORTER_MAX_MERGE_COUNT ){
92025 nWorker = SORTER_MAX_MERGE_COUNT-1;
92026 }
92027#endif
92028
92029 assert( pCsr->pKeyInfo && pCsr->pBtx==0 );
92030 assert( pCsr->eCurType==CURTYPE_SORTER );
92031 szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nKeyField-1)*sizeof(CollSeq*);
92032 sz = sizeof(VdbeSorter) + nWorker * sizeof(SortSubtask);
92033
92034 pSorter = (VdbeSorter*)sqlite3DbMallocZero(db, sz + szKeyInfo);
92035 pCsr->uc.pSorter = pSorter;
92036 if( pSorter==0 ){
92037 rc = SQLITE_NOMEM_BKPT;
92038 }else{
92039 pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
92040 memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
92041 pKeyInfo->db = 0;
92042 if( nField && nWorker==0 ){
92043 pKeyInfo->nKeyField = nField;
92044 }
92045 pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
92046 pSorter->nTask = nWorker + 1;
92047 pSorter->iPrev = (u8)(nWorker - 1);
92048 pSorter->bUseThreads = (pSorter->nTask>1);
92049 pSorter->db = db;
92050 for(i=0; i<pSorter->nTask; i++){
92051 SortSubtask *pTask = &pSorter->aTask[i];
92052 pTask->pSorter = pSorter;
92053 }
92054
92055 if( !sqlite3TempInMemory(db) ){
92056 i64 mxCache; /* Cache size in bytes*/
92057 u32 szPma = sqlite3GlobalConfig.szPma;
92058 pSorter->mnPmaSize = szPma * pgsz;
92059
92060 mxCache = db->aDb[0].pSchema->cache_size;
92061 if( mxCache<0 ){
92062 /* A negative cache-size value C indicates that the cache is abs(C)
92063 ** KiB in size. */
92064 mxCache = mxCache * -1024;
92065 }else{
92066 mxCache = mxCache * pgsz;
92067 }
92068 mxCache = MIN(mxCache, SQLITE_MAX_PMASZ);
92069 pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache);
92070
92071 /* Avoid large memory allocations if the application has requested
92072 ** SQLITE_CONFIG_SMALL_MALLOC. */
92073 if( sqlite3GlobalConfig.bSmallMalloc==0 ){
92074 assert( pSorter->iMemory==0 );
92075 pSorter->nMemory = pgsz;
92076 pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
92077 if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT;
92078 }
92079 }
92080
92081 if( pKeyInfo->nAllField<13
92082 && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
92083 ){
92084 pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT;
92085 }
92086 }
92087
92088 return rc;
92089}
92090#undef nWorker /* Defined at the top of this function */
92091
92092/*
92093** Free the list of sorted records starting at pRecord.
92094*/
92095static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){
92096 SorterRecord *p;
92097 SorterRecord *pNext;
92098 for(p=pRecord; p; p=pNext){
92099 pNext = p->u.pNext;
92100 sqlite3DbFree(db, p);
92101 }
92102}
92103
92104/*
92105** Free all resources owned by the object indicated by argument pTask. All
92106** fields of *pTask are zeroed before returning.
92107*/
92108static void vdbeSortSubtaskCleanup(sqlite3 *db, SortSubtask *pTask){
92109 sqlite3DbFree(db, pTask->pUnpacked);
92110#if SQLITE_MAX_WORKER_THREADS>0
92111 /* pTask->list.aMemory can only be non-zero if it was handed memory
92112 ** from the main thread. That only occurs SQLITE_MAX_WORKER_THREADS>0 */
92113 if( pTask->list.aMemory ){
92114 sqlite3_free(pTask->list.aMemory);
92115 }else
92116#endif
92117 {
92118 assert( pTask->list.aMemory==0 );
92119 vdbeSorterRecordFree(0, pTask->list.pList);
92120 }
92121 if( pTask->file.pFd ){
92122 sqlite3OsCloseFree(pTask->file.pFd);
92123 }
92124 if( pTask->file2.pFd ){
92125 sqlite3OsCloseFree(pTask->file2.pFd);
92126 }
92127 memset(pTask, 0, sizeof(SortSubtask));
92128}
92129
92130#ifdef SQLITE_DEBUG_SORTER_THREADS
92131static void vdbeSorterWorkDebug(SortSubtask *pTask, const char *zEvent){
92132 i64 t;
92133 int iTask = (pTask - pTask->pSorter->aTask);
92134 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
92135 fprintf(stderr, "%lld:%d %s\n", t, iTask, zEvent);
92136}
92137static void vdbeSorterRewindDebug(const char *zEvent){
92138 i64 t;
92139 sqlite3OsCurrentTimeInt64(sqlite3_vfs_find(0), &t);
92140 fprintf(stderr, "%lld:X %s\n", t, zEvent);
92141}
92142static void vdbeSorterPopulateDebug(
92143 SortSubtask *pTask,
92144 const char *zEvent
92145){
92146 i64 t;
92147 int iTask = (pTask - pTask->pSorter->aTask);
92148 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
92149 fprintf(stderr, "%lld:bg%d %s\n", t, iTask, zEvent);
92150}
92151static void vdbeSorterBlockDebug(
92152 SortSubtask *pTask,
92153 int bBlocked,
92154 const char *zEvent
92155){
92156 if( bBlocked ){
92157 i64 t;
92158 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
92159 fprintf(stderr, "%lld:main %s\n", t, zEvent);
92160 }
92161}
92162#else
92163# define vdbeSorterWorkDebug(x,y)
92164# define vdbeSorterRewindDebug(y)
92165# define vdbeSorterPopulateDebug(x,y)
92166# define vdbeSorterBlockDebug(x,y,z)
92167#endif
92168
92169#if SQLITE_MAX_WORKER_THREADS>0
92170/*
92171** Join thread pTask->thread.
92172*/
92173static int vdbeSorterJoinThread(SortSubtask *pTask){
92174 int rc = SQLITE_OK;
92175 if( pTask->pThread ){
92176#ifdef SQLITE_DEBUG_SORTER_THREADS
92177 int bDone = pTask->bDone;
92178#endif
92179 void *pRet = SQLITE_INT_TO_PTR(SQLITE_ERROR);
92180 vdbeSorterBlockDebug(pTask, !bDone, "enter");
92181 (void)sqlite3ThreadJoin(pTask->pThread, &pRet);
92182 vdbeSorterBlockDebug(pTask, !bDone, "exit");
92183 rc = SQLITE_PTR_TO_INT(pRet);
92184 assert( pTask->bDone==1 );
92185 pTask->bDone = 0;
92186 pTask->pThread = 0;
92187 }
92188 return rc;
92189}
92190
92191/*
92192** Launch a background thread to run xTask(pIn).
92193*/
92194static int vdbeSorterCreateThread(
92195 SortSubtask *pTask, /* Thread will use this task object */
92196 void *(*xTask)(void*), /* Routine to run in a separate thread */
92197 void *pIn /* Argument passed into xTask() */
92198){
92199 assert( pTask->pThread==0 && pTask->bDone==0 );
92200 return sqlite3ThreadCreate(&pTask->pThread, xTask, pIn);
92201}
92202
92203/*
92204** Join all outstanding threads launched by SorterWrite() to create
92205** level-0 PMAs.
92206*/
92207static int vdbeSorterJoinAll(VdbeSorter *pSorter, int rcin){
92208 int rc = rcin;
92209 int i;
92210
92211 /* This function is always called by the main user thread.
92212 **
92213 ** If this function is being called after SorterRewind() has been called,
92214 ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread
92215 ** is currently attempt to join one of the other threads. To avoid a race
92216 ** condition where this thread also attempts to join the same object, join
92217 ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
92218 for(i=pSorter->nTask-1; i>=0; i--){
92219 SortSubtask *pTask = &pSorter->aTask[i];
92220 int rc2 = vdbeSorterJoinThread(pTask);
92221 if( rc==SQLITE_OK ) rc = rc2;
92222 }
92223 return rc;
92224}
92225#else
92226# define vdbeSorterJoinAll(x,rcin) (rcin)
92227# define vdbeSorterJoinThread(pTask) SQLITE_OK
92228#endif
92229
92230/*
92231** Allocate a new MergeEngine object capable of handling up to
92232** nReader PmaReader inputs.
92233**
92234** nReader is automatically rounded up to the next power of two.
92235** nReader may not exceed SORTER_MAX_MERGE_COUNT even after rounding up.
92236*/
92237static MergeEngine *vdbeMergeEngineNew(int nReader){
92238 int N = 2; /* Smallest power of two >= nReader */
92239 int nByte; /* Total bytes of space to allocate */
92240 MergeEngine *pNew; /* Pointer to allocated object to return */
92241
92242 assert( nReader<=SORTER_MAX_MERGE_COUNT );
92243
92244 while( N<nReader ) N += N;
92245 nByte = sizeof(MergeEngine) + N * (sizeof(int) + sizeof(PmaReader));
92246
92247 pNew = sqlite3FaultSim(100) ? 0 : (MergeEngine*)sqlite3MallocZero(nByte);
92248 if( pNew ){
92249 pNew->nTree = N;
92250 pNew->pTask = 0;
92251 pNew->aReadr = (PmaReader*)&pNew[1];
92252 pNew->aTree = (int*)&pNew->aReadr[N];
92253 }
92254 return pNew;
92255}
92256
92257/*
92258** Free the MergeEngine object passed as the only argument.
92259*/
92260static void vdbeMergeEngineFree(MergeEngine *pMerger){
92261 int i;
92262 if( pMerger ){
92263 for(i=0; i<pMerger->nTree; i++){
92264 vdbePmaReaderClear(&pMerger->aReadr[i]);
92265 }
92266 }
92267 sqlite3_free(pMerger);
92268}
92269
92270/*
92271** Free all resources associated with the IncrMerger object indicated by
92272** the first argument.
92273*/
92274static void vdbeIncrFree(IncrMerger *pIncr){
92275 if( pIncr ){
92276#if SQLITE_MAX_WORKER_THREADS>0
92277 if( pIncr->bUseThread ){
92278 vdbeSorterJoinThread(pIncr->pTask);
92279 if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pIncr->aFile[0].pFd);
92280 if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pIncr->aFile[1].pFd);
92281 }
92282#endif
92283 vdbeMergeEngineFree(pIncr->pMerger);
92284 sqlite3_free(pIncr);
92285 }
92286}
92287
92288/*
92289** Reset a sorting cursor back to its original empty state.
92290*/
92291SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *db, VdbeSorter *pSorter){
92292 int i;
92293 (void)vdbeSorterJoinAll(pSorter, SQLITE_OK);
92294 assert( pSorter->bUseThreads || pSorter->pReader==0 );
92295#if SQLITE_MAX_WORKER_THREADS>0
92296 if( pSorter->pReader ){
92297 vdbePmaReaderClear(pSorter->pReader);
92298 sqlite3DbFree(db, pSorter->pReader);
92299 pSorter->pReader = 0;
92300 }
92301#endif
92302 vdbeMergeEngineFree(pSorter->pMerger);
92303 pSorter->pMerger = 0;
92304 for(i=0; i<pSorter->nTask; i++){
92305 SortSubtask *pTask = &pSorter->aTask[i];
92306 vdbeSortSubtaskCleanup(db, pTask);
92307 pTask->pSorter = pSorter;
92308 }
92309 if( pSorter->list.aMemory==0 ){
92310 vdbeSorterRecordFree(0, pSorter->list.pList);
92311 }
92312 pSorter->list.pList = 0;
92313 pSorter->list.szPMA = 0;
92314 pSorter->bUsePMA = 0;
92315 pSorter->iMemory = 0;
92316 pSorter->mxKeysize = 0;
92317 sqlite3DbFree(db, pSorter->pUnpacked);
92318 pSorter->pUnpacked = 0;
92319}
92320
92321/*
92322** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
92323*/
92324SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
92325 VdbeSorter *pSorter;
92326 assert( pCsr->eCurType==CURTYPE_SORTER );
92327 pSorter = pCsr->uc.pSorter;
92328 if( pSorter ){
92329 sqlite3VdbeSorterReset(db, pSorter);
92330 sqlite3_free(pSorter->list.aMemory);
92331 sqlite3DbFree(db, pSorter);
92332 pCsr->uc.pSorter = 0;
92333 }
92334}
92335
92336#if SQLITE_MAX_MMAP_SIZE>0
92337/*
92338** The first argument is a file-handle open on a temporary file. The file
92339** is guaranteed to be nByte bytes or smaller in size. This function
92340** attempts to extend the file to nByte bytes in size and to ensure that
92341** the VFS has memory mapped it.
92342**
92343** Whether or not the file does end up memory mapped of course depends on
92344** the specific VFS implementation.
92345*/
92346static void vdbeSorterExtendFile(sqlite3 *db, sqlite3_file *pFd, i64 nByte){
92347 if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){
92348 void *p = 0;
92349 int chunksize = 4*1024;
92350 sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_CHUNK_SIZE, &chunksize);
92351 sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_SIZE_HINT, &nByte);
92352 sqlite3OsFetch(pFd, 0, (int)nByte, &p);
92353 sqlite3OsUnfetch(pFd, 0, p);
92354 }
92355}
92356#else
92357# define vdbeSorterExtendFile(x,y,z)
92358#endif
92359
92360/*
92361** Allocate space for a file-handle and open a temporary file. If successful,
92362** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK.
92363** Otherwise, set *ppFd to 0 and return an SQLite error code.
92364*/
92365static int vdbeSorterOpenTempFile(
92366 sqlite3 *db, /* Database handle doing sort */
92367 i64 nExtend, /* Attempt to extend file to this size */
92368 sqlite3_file **ppFd
92369){
92370 int rc;
92371 if( sqlite3FaultSim(202) ) return SQLITE_IOERR_ACCESS;
92372 rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd,
92373 SQLITE_OPEN_TEMP_JOURNAL |
92374 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
92375 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE, &rc
92376 );
92377 if( rc==SQLITE_OK ){
92378 i64 max = SQLITE_MAX_MMAP_SIZE;
92379 sqlite3OsFileControlHint(*ppFd, SQLITE_FCNTL_MMAP_SIZE, (void*)&max);
92380 if( nExtend>0 ){
92381 vdbeSorterExtendFile(db, *ppFd, nExtend);
92382 }
92383 }
92384 return rc;
92385}
92386
92387/*
92388** If it has not already been allocated, allocate the UnpackedRecord
92389** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or
92390** if no allocation was required), or SQLITE_NOMEM otherwise.
92391*/
92392static int vdbeSortAllocUnpacked(SortSubtask *pTask){
92393 if( pTask->pUnpacked==0 ){
92394 pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pTask->pSorter->pKeyInfo);
92395 if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT;
92396 pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nKeyField;
92397 pTask->pUnpacked->errCode = 0;
92398 }
92399 return SQLITE_OK;
92400}
92401
92402
92403/*
92404** Merge the two sorted lists p1 and p2 into a single list.
92405*/
92406static SorterRecord *vdbeSorterMerge(
92407 SortSubtask *pTask, /* Calling thread context */
92408 SorterRecord *p1, /* First list to merge */
92409 SorterRecord *p2 /* Second list to merge */
92410){
92411 SorterRecord *pFinal = 0;
92412 SorterRecord **pp = &pFinal;
92413 int bCached = 0;
92414
92415 assert( p1!=0 && p2!=0 );
92416 for(;;){
92417 int res;
92418 res = pTask->xCompare(
92419 pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal
92420 );
92421
92422 if( res<=0 ){
92423 *pp = p1;
92424 pp = &p1->u.pNext;
92425 p1 = p1->u.pNext;
92426 if( p1==0 ){
92427 *pp = p2;
92428 break;
92429 }
92430 }else{
92431 *pp = p2;
92432 pp = &p2->u.pNext;
92433 p2 = p2->u.pNext;
92434 bCached = 0;
92435 if( p2==0 ){
92436 *pp = p1;
92437 break;
92438 }
92439 }
92440 }
92441 return pFinal;
92442}
92443
92444/*
92445** Return the SorterCompare function to compare values collected by the
92446** sorter object passed as the only argument.
92447*/
92448static SorterCompare vdbeSorterGetCompare(VdbeSorter *p){
92449 if( p->typeMask==SORTER_TYPE_INTEGER ){
92450 return vdbeSorterCompareInt;
92451 }else if( p->typeMask==SORTER_TYPE_TEXT ){
92452 return vdbeSorterCompareText;
92453 }
92454 return vdbeSorterCompare;
92455}
92456
92457/*
92458** Sort the linked list of records headed at pTask->pList. Return
92459** SQLITE_OK if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if
92460** an error occurs.
92461*/
92462static int vdbeSorterSort(SortSubtask *pTask, SorterList *pList){
92463 int i;
92464 SorterRecord **aSlot;
92465 SorterRecord *p;
92466 int rc;
92467
92468 rc = vdbeSortAllocUnpacked(pTask);
92469 if( rc!=SQLITE_OK ) return rc;
92470
92471 p = pList->pList;
92472 pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter);
92473
92474 aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *));
92475 if( !aSlot ){
92476 return SQLITE_NOMEM_BKPT;
92477 }
92478
92479 while( p ){
92480 SorterRecord *pNext;
92481 if( pList->aMemory ){
92482 if( (u8*)p==pList->aMemory ){
92483 pNext = 0;
92484 }else{
92485 assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) );
92486 pNext = (SorterRecord*)&pList->aMemory[p->u.iNext];
92487 }
92488 }else{
92489 pNext = p->u.pNext;
92490 }
92491
92492 p->u.pNext = 0;
92493 for(i=0; aSlot[i]; i++){
92494 p = vdbeSorterMerge(pTask, p, aSlot[i]);
92495 aSlot[i] = 0;
92496 }
92497 aSlot[i] = p;
92498 p = pNext;
92499 }
92500
92501 p = 0;
92502 for(i=0; i<64; i++){
92503 if( aSlot[i]==0 ) continue;
92504 p = p ? vdbeSorterMerge(pTask, p, aSlot[i]) : aSlot[i];
92505 }
92506 pList->pList = p;
92507
92508 sqlite3_free(aSlot);
92509 assert( pTask->pUnpacked->errCode==SQLITE_OK
92510 || pTask->pUnpacked->errCode==SQLITE_NOMEM
92511 );
92512 return pTask->pUnpacked->errCode;
92513}
92514
92515/*
92516** Initialize a PMA-writer object.
92517*/
92518static void vdbePmaWriterInit(
92519 sqlite3_file *pFd, /* File handle to write to */
92520 PmaWriter *p, /* Object to populate */
92521 int nBuf, /* Buffer size */
92522 i64 iStart /* Offset of pFd to begin writing at */
92523){
92524 memset(p, 0, sizeof(PmaWriter));
92525 p->aBuffer = (u8*)sqlite3Malloc(nBuf);
92526 if( !p->aBuffer ){
92527 p->eFWErr = SQLITE_NOMEM_BKPT;
92528 }else{
92529 p->iBufEnd = p->iBufStart = (iStart % nBuf);
92530 p->iWriteOff = iStart - p->iBufStart;
92531 p->nBuffer = nBuf;
92532 p->pFd = pFd;
92533 }
92534}
92535
92536/*
92537** Write nData bytes of data to the PMA. Return SQLITE_OK
92538** if successful, or an SQLite error code if an error occurs.
92539*/
92540static void vdbePmaWriteBlob(PmaWriter *p, u8 *pData, int nData){
92541 int nRem = nData;
92542 while( nRem>0 && p->eFWErr==0 ){
92543 int nCopy = nRem;
92544 if( nCopy>(p->nBuffer - p->iBufEnd) ){
92545 nCopy = p->nBuffer - p->iBufEnd;
92546 }
92547
92548 memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
92549 p->iBufEnd += nCopy;
92550 if( p->iBufEnd==p->nBuffer ){
92551 p->eFWErr = sqlite3OsWrite(p->pFd,
92552 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
92553 p->iWriteOff + p->iBufStart
92554 );
92555 p->iBufStart = p->iBufEnd = 0;
92556 p->iWriteOff += p->nBuffer;
92557 }
92558 assert( p->iBufEnd<p->nBuffer );
92559
92560 nRem -= nCopy;
92561 }
92562}
92563
92564/*
92565** Flush any buffered data to disk and clean up the PMA-writer object.
92566** The results of using the PMA-writer after this call are undefined.
92567** Return SQLITE_OK if flushing the buffered data succeeds or is not
92568** required. Otherwise, return an SQLite error code.
92569**
92570** Before returning, set *piEof to the offset immediately following the
92571** last byte written to the file.
92572*/
92573static int vdbePmaWriterFinish(PmaWriter *p, i64 *piEof){
92574 int rc;
92575 if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
92576 p->eFWErr = sqlite3OsWrite(p->pFd,
92577 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
92578 p->iWriteOff + p->iBufStart
92579 );
92580 }
92581 *piEof = (p->iWriteOff + p->iBufEnd);
92582 sqlite3_free(p->aBuffer);
92583 rc = p->eFWErr;
92584 memset(p, 0, sizeof(PmaWriter));
92585 return rc;
92586}
92587
92588/*
92589** Write value iVal encoded as a varint to the PMA. Return
92590** SQLITE_OK if successful, or an SQLite error code if an error occurs.
92591*/
92592static void vdbePmaWriteVarint(PmaWriter *p, u64 iVal){
92593 int nByte;
92594 u8 aByte[10];
92595 nByte = sqlite3PutVarint(aByte, iVal);
92596 vdbePmaWriteBlob(p, aByte, nByte);
92597}
92598
92599/*
92600** Write the current contents of in-memory linked-list pList to a level-0
92601** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if
92602** successful, or an SQLite error code otherwise.
92603**
92604** The format of a PMA is:
92605**
92606** * A varint. This varint contains the total number of bytes of content
92607** in the PMA (not including the varint itself).
92608**
92609** * One or more records packed end-to-end in order of ascending keys.
92610** Each record consists of a varint followed by a blob of data (the
92611** key). The varint is the number of bytes in the blob of data.
92612*/
92613static int vdbeSorterListToPMA(SortSubtask *pTask, SorterList *pList){
92614 sqlite3 *db = pTask->pSorter->db;
92615 int rc = SQLITE_OK; /* Return code */
92616 PmaWriter writer; /* Object used to write to the file */
92617
92618#ifdef SQLITE_DEBUG
92619 /* Set iSz to the expected size of file pTask->file after writing the PMA.
92620 ** This is used by an assert() statement at the end of this function. */
92621 i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof;
92622#endif
92623
92624 vdbeSorterWorkDebug(pTask, "enter");
92625 memset(&writer, 0, sizeof(PmaWriter));
92626 assert( pList->szPMA>0 );
92627
92628 /* If the first temporary PMA file has not been opened, open it now. */
92629 if( pTask->file.pFd==0 ){
92630 rc = vdbeSorterOpenTempFile(db, 0, &pTask->file.pFd);
92631 assert( rc!=SQLITE_OK || pTask->file.pFd );
92632 assert( pTask->file.iEof==0 );
92633 assert( pTask->nPMA==0 );
92634 }
92635
92636 /* Try to get the file to memory map */
92637 if( rc==SQLITE_OK ){
92638 vdbeSorterExtendFile(db, pTask->file.pFd, pTask->file.iEof+pList->szPMA+9);
92639 }
92640
92641 /* Sort the list */
92642 if( rc==SQLITE_OK ){
92643 rc = vdbeSorterSort(pTask, pList);
92644 }
92645
92646 if( rc==SQLITE_OK ){
92647 SorterRecord *p;
92648 SorterRecord *pNext = 0;
92649
92650 vdbePmaWriterInit(pTask->file.pFd, &writer, pTask->pSorter->pgsz,
92651 pTask->file.iEof);
92652 pTask->nPMA++;
92653 vdbePmaWriteVarint(&writer, pList->szPMA);
92654 for(p=pList->pList; p; p=pNext){
92655 pNext = p->u.pNext;
92656 vdbePmaWriteVarint(&writer, p->nVal);
92657 vdbePmaWriteBlob(&writer, SRVAL(p), p->nVal);
92658 if( pList->aMemory==0 ) sqlite3_free(p);
92659 }
92660 pList->pList = p;
92661 rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof);
92662 }
92663
92664 vdbeSorterWorkDebug(pTask, "exit");
92665 assert( rc!=SQLITE_OK || pList->pList==0 );
92666 assert( rc!=SQLITE_OK || pTask->file.iEof==iSz );
92667 return rc;
92668}
92669
92670/*
92671** Advance the MergeEngine to its next entry.
92672** Set *pbEof to true there is no next entry because
92673** the MergeEngine has reached the end of all its inputs.
92674**
92675** Return SQLITE_OK if successful or an error code if an error occurs.
92676*/
92677static int vdbeMergeEngineStep(
92678 MergeEngine *pMerger, /* The merge engine to advance to the next row */
92679 int *pbEof /* Set TRUE at EOF. Set false for more content */
92680){
92681 int rc;
92682 int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
92683 SortSubtask *pTask = pMerger->pTask;
92684
92685 /* Advance the current PmaReader */
92686 rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);
92687
92688 /* Update contents of aTree[] */
92689 if( rc==SQLITE_OK ){
92690 int i; /* Index of aTree[] to recalculate */
92691 PmaReader *pReadr1; /* First PmaReader to compare */
92692 PmaReader *pReadr2; /* Second PmaReader to compare */
92693 int bCached = 0;
92694
92695 /* Find the first two PmaReaders to compare. The one that was just
92696 ** advanced (iPrev) and the one next to it in the array. */
92697 pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
92698 pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
92699
92700 for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
92701 /* Compare pReadr1 and pReadr2. Store the result in variable iRes. */
92702 int iRes;
92703 if( pReadr1->pFd==0 ){
92704 iRes = +1;
92705 }else if( pReadr2->pFd==0 ){
92706 iRes = -1;
92707 }else{
92708 iRes = pTask->xCompare(pTask, &bCached,
92709 pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
92710 );
92711 }
92712
92713 /* If pReadr1 contained the smaller value, set aTree[i] to its index.
92714 ** Then set pReadr2 to the next PmaReader to compare to pReadr1. In this
92715 ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
92716 ** pKey2 to point to the record belonging to pReadr2.
92717 **
92718 ** Alternatively, if pReadr2 contains the smaller of the two values,
92719 ** set aTree[i] to its index and update pReadr1. If vdbeSorterCompare()
92720 ** was actually called above, then pTask->pUnpacked now contains
92721 ** a value equivalent to pReadr2. So set pKey2 to NULL to prevent
92722 ** vdbeSorterCompare() from decoding pReadr2 again.
92723 **
92724 ** If the two values were equal, then the value from the oldest
92725 ** PMA should be considered smaller. The VdbeSorter.aReadr[] array
92726 ** is sorted from oldest to newest, so pReadr1 contains older values
92727 ** than pReadr2 iff (pReadr1<pReadr2). */
92728 if( iRes<0 || (iRes==0 && pReadr1<pReadr2) ){
92729 pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
92730 pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
92731 bCached = 0;
92732 }else{
92733 if( pReadr1->pFd ) bCached = 0;
92734 pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
92735 pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
92736 }
92737 }
92738 *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
92739 }
92740
92741 return (rc==SQLITE_OK ? pTask->pUnpacked->errCode : rc);
92742}
92743
92744#if SQLITE_MAX_WORKER_THREADS>0
92745/*
92746** The main routine for background threads that write level-0 PMAs.
92747*/
92748static void *vdbeSorterFlushThread(void *pCtx){
92749 SortSubtask *pTask = (SortSubtask*)pCtx;
92750 int rc; /* Return code */
92751 assert( pTask->bDone==0 );
92752 rc = vdbeSorterListToPMA(pTask, &pTask->list);
92753 pTask->bDone = 1;
92754 return SQLITE_INT_TO_PTR(rc);
92755}
92756#endif /* SQLITE_MAX_WORKER_THREADS>0 */
92757
92758/*
92759** Flush the current contents of VdbeSorter.list to a new PMA, possibly
92760** using a background thread.
92761*/
92762static int vdbeSorterFlushPMA(VdbeSorter *pSorter){
92763#if SQLITE_MAX_WORKER_THREADS==0
92764 pSorter->bUsePMA = 1;
92765 return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list);
92766#else
92767 int rc = SQLITE_OK;
92768 int i;
92769 SortSubtask *pTask = 0; /* Thread context used to create new PMA */
92770 int nWorker = (pSorter->nTask-1);
92771
92772 /* Set the flag to indicate that at least one PMA has been written.
92773 ** Or will be, anyhow. */
92774 pSorter->bUsePMA = 1;
92775
92776 /* Select a sub-task to sort and flush the current list of in-memory
92777 ** records to disk. If the sorter is running in multi-threaded mode,
92778 ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
92779 ** the background thread from a sub-tasks previous turn is still running,
92780 ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
92781 ** fall back to using the final sub-task. The first (pSorter->nTask-1)
92782 ** sub-tasks are prefered as they use background threads - the final
92783 ** sub-task uses the main thread. */
92784 for(i=0; i<nWorker; i++){
92785 int iTest = (pSorter->iPrev + i + 1) % nWorker;
92786 pTask = &pSorter->aTask[iTest];
92787 if( pTask->bDone ){
92788 rc = vdbeSorterJoinThread(pTask);
92789 }
92790 if( rc!=SQLITE_OK || pTask->pThread==0 ) break;
92791 }
92792
92793 if( rc==SQLITE_OK ){
92794 if( i==nWorker ){
92795 /* Use the foreground thread for this operation */
92796 rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list);
92797 }else{
92798 /* Launch a background thread for this operation */
92799 u8 *aMem = pTask->list.aMemory;
92800 void *pCtx = (void*)pTask;
92801
92802 assert( pTask->pThread==0 && pTask->bDone==0 );
92803 assert( pTask->list.pList==0 );
92804 assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 );
92805
92806 pSorter->iPrev = (u8)(pTask - pSorter->aTask);
92807 pTask->list = pSorter->list;
92808 pSorter->list.pList = 0;
92809 pSorter->list.szPMA = 0;
92810 if( aMem ){
92811 pSorter->list.aMemory = aMem;
92812 pSorter->nMemory = sqlite3MallocSize(aMem);
92813 }else if( pSorter->list.aMemory ){
92814 pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory);
92815 if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT;
92816 }
92817
92818 rc = vdbeSorterCreateThread(pTask, vdbeSorterFlushThread, pCtx);
92819 }
92820 }
92821
92822 return rc;
92823#endif /* SQLITE_MAX_WORKER_THREADS!=0 */
92824}
92825
92826/*
92827** Add a record to the sorter.
92828*/
92829SQLITE_PRIVATE int sqlite3VdbeSorterWrite(
92830 const VdbeCursor *pCsr, /* Sorter cursor */
92831 Mem *pVal /* Memory cell containing record */
92832){
92833 VdbeSorter *pSorter;
92834 int rc = SQLITE_OK; /* Return Code */
92835 SorterRecord *pNew; /* New list element */
92836 int bFlush; /* True to flush contents of memory to PMA */
92837 int nReq; /* Bytes of memory required */
92838 int nPMA; /* Bytes of PMA space required */
92839 int t; /* serial type of first record field */
92840
92841 assert( pCsr->eCurType==CURTYPE_SORTER );
92842 pSorter = pCsr->uc.pSorter;
92843 getVarint32((const u8*)&pVal->z[1], t);
92844 if( t>0 && t<10 && t!=7 ){
92845 pSorter->typeMask &= SORTER_TYPE_INTEGER;
92846 }else if( t>10 && (t & 0x01) ){
92847 pSorter->typeMask &= SORTER_TYPE_TEXT;
92848 }else{
92849 pSorter->typeMask = 0;
92850 }
92851
92852 assert( pSorter );
92853
92854 /* Figure out whether or not the current contents of memory should be
92855 ** flushed to a PMA before continuing. If so, do so.
92856 **
92857 ** If using the single large allocation mode (pSorter->aMemory!=0), then
92858 ** flush the contents of memory to a new PMA if (a) at least one value is
92859 ** already in memory and (b) the new value will not fit in memory.
92860 **
92861 ** Or, if using separate allocations for each record, flush the contents
92862 ** of memory to a PMA if either of the following are true:
92863 **
92864 ** * The total memory allocated for the in-memory list is greater
92865 ** than (page-size * cache-size), or
92866 **
92867 ** * The total memory allocated for the in-memory list is greater
92868 ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
92869 */
92870 nReq = pVal->n + sizeof(SorterRecord);
92871 nPMA = pVal->n + sqlite3VarintLen(pVal->n);
92872 if( pSorter->mxPmaSize ){
92873 if( pSorter->list.aMemory ){
92874 bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize;
92875 }else{
92876 bFlush = (
92877 (pSorter->list.szPMA > pSorter->mxPmaSize)
92878 || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull())
92879 );
92880 }
92881 if( bFlush ){
92882 rc = vdbeSorterFlushPMA(pSorter);
92883 pSorter->list.szPMA = 0;
92884 pSorter->iMemory = 0;
92885 assert( rc!=SQLITE_OK || pSorter->list.pList==0 );
92886 }
92887 }
92888
92889 pSorter->list.szPMA += nPMA;
92890 if( nPMA>pSorter->mxKeysize ){
92891 pSorter->mxKeysize = nPMA;
92892 }
92893
92894 if( pSorter->list.aMemory ){
92895 int nMin = pSorter->iMemory + nReq;
92896
92897 if( nMin>pSorter->nMemory ){
92898 u8 *aNew;
92899 int iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
92900 int nNew = pSorter->nMemory * 2;
92901 while( nNew < nMin ) nNew = nNew*2;
92902 if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
92903 if( nNew < nMin ) nNew = nMin;
92904
92905 aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
92906 if( !aNew ) return SQLITE_NOMEM_BKPT;
92907 pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
92908 pSorter->list.aMemory = aNew;
92909 pSorter->nMemory = nNew;
92910 }
92911
92912 pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
92913 pSorter->iMemory += ROUND8(nReq);
92914 if( pSorter->list.pList ){
92915 pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
92916 }
92917 }else{
92918 pNew = (SorterRecord *)sqlite3Malloc(nReq);
92919 if( pNew==0 ){
92920 return SQLITE_NOMEM_BKPT;
92921 }
92922 pNew->u.pNext = pSorter->list.pList;
92923 }
92924
92925 memcpy(SRVAL(pNew), pVal->z, pVal->n);
92926 pNew->nVal = pVal->n;
92927 pSorter->list.pList = pNew;
92928
92929 return rc;
92930}
92931
92932/*
92933** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format
92934** of the data stored in aFile[1] is the same as that used by regular PMAs,
92935** except that the number-of-bytes varint is omitted from the start.
92936*/
92937static int vdbeIncrPopulate(IncrMerger *pIncr){
92938 int rc = SQLITE_OK;
92939 int rc2;
92940 i64 iStart = pIncr->iStartOff;
92941 SorterFile *pOut = &pIncr->aFile[1];
92942 SortSubtask *pTask = pIncr->pTask;
92943 MergeEngine *pMerger = pIncr->pMerger;
92944 PmaWriter writer;
92945 assert( pIncr->bEof==0 );
92946
92947 vdbeSorterPopulateDebug(pTask, "enter");
92948
92949 vdbePmaWriterInit(pOut->pFd, &writer, pTask->pSorter->pgsz, iStart);
92950 while( rc==SQLITE_OK ){
92951 int dummy;
92952 PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ];
92953 int nKey = pReader->nKey;
92954 i64 iEof = writer.iWriteOff + writer.iBufEnd;
92955
92956 /* Check if the output file is full or if the input has been exhausted.
92957 ** In either case exit the loop. */
92958 if( pReader->pFd==0 ) break;
92959 if( (iEof + nKey + sqlite3VarintLen(nKey))>(iStart + pIncr->mxSz) ) break;
92960
92961 /* Write the next key to the output. */
92962 vdbePmaWriteVarint(&writer, nKey);
92963 vdbePmaWriteBlob(&writer, pReader->aKey, nKey);
92964 assert( pIncr->pMerger->pTask==pTask );
92965 rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy);
92966 }
92967
92968 rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof);
92969 if( rc==SQLITE_OK ) rc = rc2;
92970 vdbeSorterPopulateDebug(pTask, "exit");
92971 return rc;
92972}
92973
92974#if SQLITE_MAX_WORKER_THREADS>0
92975/*
92976** The main routine for background threads that populate aFile[1] of
92977** multi-threaded IncrMerger objects.
92978*/
92979static void *vdbeIncrPopulateThread(void *pCtx){
92980 IncrMerger *pIncr = (IncrMerger*)pCtx;
92981 void *pRet = SQLITE_INT_TO_PTR( vdbeIncrPopulate(pIncr) );
92982 pIncr->pTask->bDone = 1;
92983 return pRet;
92984}
92985
92986/*
92987** Launch a background thread to populate aFile[1] of pIncr.
92988*/
92989static int vdbeIncrBgPopulate(IncrMerger *pIncr){
92990 void *p = (void*)pIncr;
92991 assert( pIncr->bUseThread );
92992 return vdbeSorterCreateThread(pIncr->pTask, vdbeIncrPopulateThread, p);
92993}
92994#endif
92995
92996/*
92997** This function is called when the PmaReader corresponding to pIncr has
92998** finished reading the contents of aFile[0]. Its purpose is to "refill"
92999** aFile[0] such that the PmaReader should start rereading it from the
93000** beginning.
93001**
93002** For single-threaded objects, this is accomplished by literally reading
93003** keys from pIncr->pMerger and repopulating aFile[0].
93004**
93005** For multi-threaded objects, all that is required is to wait until the
93006** background thread is finished (if it is not already) and then swap
93007** aFile[0] and aFile[1] in place. If the contents of pMerger have not
93008** been exhausted, this function also launches a new background thread
93009** to populate the new aFile[1].
93010**
93011** SQLITE_OK is returned on success, or an SQLite error code otherwise.
93012*/
93013static int vdbeIncrSwap(IncrMerger *pIncr){
93014 int rc = SQLITE_OK;
93015
93016#if SQLITE_MAX_WORKER_THREADS>0
93017 if( pIncr->bUseThread ){
93018 rc = vdbeSorterJoinThread(pIncr->pTask);
93019
93020 if( rc==SQLITE_OK ){
93021 SorterFile f0 = pIncr->aFile[0];
93022 pIncr->aFile[0] = pIncr->aFile[1];
93023 pIncr->aFile[1] = f0;
93024 }
93025
93026 if( rc==SQLITE_OK ){
93027 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
93028 pIncr->bEof = 1;
93029 }else{
93030 rc = vdbeIncrBgPopulate(pIncr);
93031 }
93032 }
93033 }else
93034#endif
93035 {
93036 rc = vdbeIncrPopulate(pIncr);
93037 pIncr->aFile[0] = pIncr->aFile[1];
93038 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
93039 pIncr->bEof = 1;
93040 }
93041 }
93042
93043 return rc;
93044}
93045
93046/*
93047** Allocate and return a new IncrMerger object to read data from pMerger.
93048**
93049** If an OOM condition is encountered, return NULL. In this case free the
93050** pMerger argument before returning.
93051*/
93052static int vdbeIncrMergerNew(
93053 SortSubtask *pTask, /* The thread that will be using the new IncrMerger */
93054 MergeEngine *pMerger, /* The MergeEngine that the IncrMerger will control */
93055 IncrMerger **ppOut /* Write the new IncrMerger here */
93056){
93057 int rc = SQLITE_OK;
93058 IncrMerger *pIncr = *ppOut = (IncrMerger*)
93059 (sqlite3FaultSim(100) ? 0 : sqlite3MallocZero(sizeof(*pIncr)));
93060 if( pIncr ){
93061 pIncr->pMerger = pMerger;
93062 pIncr->pTask = pTask;
93063 pIncr->mxSz = MAX(pTask->pSorter->mxKeysize+9,pTask->pSorter->mxPmaSize/2);
93064 pTask->file2.iEof += pIncr->mxSz;
93065 }else{
93066 vdbeMergeEngineFree(pMerger);
93067 rc = SQLITE_NOMEM_BKPT;
93068 }
93069 return rc;
93070}
93071
93072#if SQLITE_MAX_WORKER_THREADS>0
93073/*
93074** Set the "use-threads" flag on object pIncr.
93075*/
93076static void vdbeIncrMergerSetThreads(IncrMerger *pIncr){
93077 pIncr->bUseThread = 1;
93078 pIncr->pTask->file2.iEof -= pIncr->mxSz;
93079}
93080#endif /* SQLITE_MAX_WORKER_THREADS>0 */
93081
93082
93083
93084/*
93085** Recompute pMerger->aTree[iOut] by comparing the next keys on the
93086** two PmaReaders that feed that entry. Neither of the PmaReaders
93087** are advanced. This routine merely does the comparison.
93088*/
93089static void vdbeMergeEngineCompare(
93090 MergeEngine *pMerger, /* Merge engine containing PmaReaders to compare */
93091 int iOut /* Store the result in pMerger->aTree[iOut] */
93092){
93093 int i1;
93094 int i2;
93095 int iRes;
93096 PmaReader *p1;
93097 PmaReader *p2;
93098
93099 assert( iOut<pMerger->nTree && iOut>0 );
93100
93101 if( iOut>=(pMerger->nTree/2) ){
93102 i1 = (iOut - pMerger->nTree/2) * 2;
93103 i2 = i1 + 1;
93104 }else{
93105 i1 = pMerger->aTree[iOut*2];
93106 i2 = pMerger->aTree[iOut*2+1];
93107 }
93108
93109 p1 = &pMerger->aReadr[i1];
93110 p2 = &pMerger->aReadr[i2];
93111
93112 if( p1->pFd==0 ){
93113 iRes = i2;
93114 }else if( p2->pFd==0 ){
93115 iRes = i1;
93116 }else{
93117 SortSubtask *pTask = pMerger->pTask;
93118 int bCached = 0;
93119 int res;
93120 assert( pTask->pUnpacked!=0 ); /* from vdbeSortSubtaskMain() */
93121 res = pTask->xCompare(
93122 pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
93123 );
93124 if( res<=0 ){
93125 iRes = i1;
93126 }else{
93127 iRes = i2;
93128 }
93129 }
93130
93131 pMerger->aTree[iOut] = iRes;
93132}
93133
93134/*
93135** Allowed values for the eMode parameter to vdbeMergeEngineInit()
93136** and vdbePmaReaderIncrMergeInit().
93137**
93138** Only INCRINIT_NORMAL is valid in single-threaded builds (when
93139** SQLITE_MAX_WORKER_THREADS==0). The other values are only used
93140** when there exists one or more separate worker threads.
93141*/
93142#define INCRINIT_NORMAL 0
93143#define INCRINIT_TASK 1
93144#define INCRINIT_ROOT 2
93145
93146/*
93147** Forward reference required as the vdbeIncrMergeInit() and
93148** vdbePmaReaderIncrInit() routines are called mutually recursively when
93149** building a merge tree.
93150*/
93151static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode);
93152
93153/*
93154** Initialize the MergeEngine object passed as the second argument. Once this
93155** function returns, the first key of merged data may be read from the
93156** MergeEngine object in the usual fashion.
93157**
93158** If argument eMode is INCRINIT_ROOT, then it is assumed that any IncrMerge
93159** objects attached to the PmaReader objects that the merger reads from have
93160** already been populated, but that they have not yet populated aFile[0] and
93161** set the PmaReader objects up to read from it. In this case all that is
93162** required is to call vdbePmaReaderNext() on each PmaReader to point it at
93163** its first key.
93164**
93165** Otherwise, if eMode is any value other than INCRINIT_ROOT, then use
93166** vdbePmaReaderIncrMergeInit() to initialize each PmaReader that feeds data
93167** to pMerger.
93168**
93169** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
93170*/
93171static int vdbeMergeEngineInit(
93172 SortSubtask *pTask, /* Thread that will run pMerger */
93173 MergeEngine *pMerger, /* MergeEngine to initialize */
93174 int eMode /* One of the INCRINIT_XXX constants */
93175){
93176 int rc = SQLITE_OK; /* Return code */
93177 int i; /* For looping over PmaReader objects */
93178 int nTree; /* Number of subtrees to merge */
93179
93180 /* Failure to allocate the merge would have been detected prior to
93181 ** invoking this routine */
93182 assert( pMerger!=0 );
93183
93184 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
93185 assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL );
93186
93187 /* Verify that the MergeEngine is assigned to a single thread */
93188 assert( pMerger->pTask==0 );
93189 pMerger->pTask = pTask;
93190
93191 nTree = pMerger->nTree;
93192 for(i=0; i<nTree; i++){
93193 if( SQLITE_MAX_WORKER_THREADS>0 && eMode==INCRINIT_ROOT ){
93194 /* PmaReaders should be normally initialized in order, as if they are
93195 ** reading from the same temp file this makes for more linear file IO.
93196 ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
93197 ** in use it will block the vdbePmaReaderNext() call while it uses
93198 ** the main thread to fill its buffer. So calling PmaReaderNext()
93199 ** on this PmaReader before any of the multi-threaded PmaReaders takes
93200 ** better advantage of multi-processor hardware. */
93201 rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]);
93202 }else{
93203 rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL);
93204 }
93205 if( rc!=SQLITE_OK ) return rc;
93206 }
93207
93208 for(i=pMerger->nTree-1; i>0; i--){
93209 vdbeMergeEngineCompare(pMerger, i);
93210 }
93211 return pTask->pUnpacked->errCode;
93212}
93213
93214/*
93215** The PmaReader passed as the first argument is guaranteed to be an
93216** incremental-reader (pReadr->pIncr!=0). This function serves to open
93217** and/or initialize the temp file related fields of the IncrMerge
93218** object at (pReadr->pIncr).
93219**
93220** If argument eMode is set to INCRINIT_NORMAL, then all PmaReaders
93221** in the sub-tree headed by pReadr are also initialized. Data is then
93222** loaded into the buffers belonging to pReadr and it is set to point to
93223** the first key in its range.
93224**
93225** If argument eMode is set to INCRINIT_TASK, then pReadr is guaranteed
93226** to be a multi-threaded PmaReader and this function is being called in a
93227** background thread. In this case all PmaReaders in the sub-tree are
93228** initialized as for INCRINIT_NORMAL and the aFile[1] buffer belonging to
93229** pReadr is populated. However, pReadr itself is not set up to point
93230** to its first key. A call to vdbePmaReaderNext() is still required to do
93231** that.
93232**
93233** The reason this function does not call vdbePmaReaderNext() immediately
93234** in the INCRINIT_TASK case is that vdbePmaReaderNext() assumes that it has
93235** to block on thread (pTask->thread) before accessing aFile[1]. But, since
93236** this entire function is being run by thread (pTask->thread), that will
93237** lead to the current background thread attempting to join itself.
93238**
93239** Finally, if argument eMode is set to INCRINIT_ROOT, it may be assumed
93240** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all
93241** child-trees have already been initialized using IncrInit(INCRINIT_TASK).
93242** In this case vdbePmaReaderNext() is called on all child PmaReaders and
93243** the current PmaReader set to point to the first key in its range.
93244**
93245** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
93246*/
93247static int vdbePmaReaderIncrMergeInit(PmaReader *pReadr, int eMode){
93248 int rc = SQLITE_OK;
93249 IncrMerger *pIncr = pReadr->pIncr;
93250 SortSubtask *pTask = pIncr->pTask;
93251 sqlite3 *db = pTask->pSorter->db;
93252
93253 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
93254 assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL );
93255
93256 rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
93257
93258 /* Set up the required files for pIncr. A multi-theaded IncrMerge object
93259 ** requires two temp files to itself, whereas a single-threaded object
93260 ** only requires a region of pTask->file2. */
93261 if( rc==SQLITE_OK ){
93262 int mxSz = pIncr->mxSz;
93263#if SQLITE_MAX_WORKER_THREADS>0
93264 if( pIncr->bUseThread ){
93265 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
93266 if( rc==SQLITE_OK ){
93267 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
93268 }
93269 }else
93270#endif
93271 /*if( !pIncr->bUseThread )*/{
93272 if( pTask->file2.pFd==0 ){
93273 assert( pTask->file2.iEof>0 );
93274 rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
93275 pTask->file2.iEof = 0;
93276 }
93277 if( rc==SQLITE_OK ){
93278 pIncr->aFile[1].pFd = pTask->file2.pFd;
93279 pIncr->iStartOff = pTask->file2.iEof;
93280 pTask->file2.iEof += mxSz;
93281 }
93282 }
93283 }
93284
93285#if SQLITE_MAX_WORKER_THREADS>0
93286 if( rc==SQLITE_OK && pIncr->bUseThread ){
93287 /* Use the current thread to populate aFile[1], even though this
93288 ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
93289 ** then this function is already running in background thread
93290 ** pIncr->pTask->thread.
93291 **
93292 ** If this is the INCRINIT_ROOT object, then it is running in the
93293 ** main VDBE thread. But that is Ok, as that thread cannot return
93294 ** control to the VDBE or proceed with anything useful until the
93295 ** first results are ready from this merger object anyway.
93296 */
93297 assert( eMode==INCRINIT_ROOT || eMode==INCRINIT_TASK );
93298 rc = vdbeIncrPopulate(pIncr);
93299 }
93300#endif
93301
93302 if( rc==SQLITE_OK && (SQLITE_MAX_WORKER_THREADS==0 || eMode!=INCRINIT_TASK) ){
93303 rc = vdbePmaReaderNext(pReadr);
93304 }
93305
93306 return rc;
93307}
93308
93309#if SQLITE_MAX_WORKER_THREADS>0
93310/*
93311** The main routine for vdbePmaReaderIncrMergeInit() operations run in
93312** background threads.
93313*/
93314static void *vdbePmaReaderBgIncrInit(void *pCtx){
93315 PmaReader *pReader = (PmaReader*)pCtx;
93316 void *pRet = SQLITE_INT_TO_PTR(
93317 vdbePmaReaderIncrMergeInit(pReader,INCRINIT_TASK)
93318 );
93319 pReader->pIncr->pTask->bDone = 1;
93320 return pRet;
93321}
93322#endif
93323
93324/*
93325** If the PmaReader passed as the first argument is not an incremental-reader
93326** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
93327** the vdbePmaReaderIncrMergeInit() function with the parameters passed to
93328** this routine to initialize the incremental merge.
93329**
93330** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1),
93331** then a background thread is launched to call vdbePmaReaderIncrMergeInit().
93332** Or, if the IncrMerger is single threaded, the same function is called
93333** using the current thread.
93334*/
93335static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode){
93336 IncrMerger *pIncr = pReadr->pIncr; /* Incremental merger */
93337 int rc = SQLITE_OK; /* Return code */
93338 if( pIncr ){
93339#if SQLITE_MAX_WORKER_THREADS>0
93340 assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK );
93341 if( pIncr->bUseThread ){
93342 void *pCtx = (void*)pReadr;
93343 rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx);
93344 }else
93345#endif
93346 {
93347 rc = vdbePmaReaderIncrMergeInit(pReadr, eMode);
93348 }
93349 }
93350 return rc;
93351}
93352
93353/*
93354** Allocate a new MergeEngine object to merge the contents of nPMA level-0
93355** PMAs from pTask->file. If no error occurs, set *ppOut to point to
93356** the new object and return SQLITE_OK. Or, if an error does occur, set *ppOut
93357** to NULL and return an SQLite error code.
93358**
93359** When this function is called, *piOffset is set to the offset of the
93360** first PMA to read from pTask->file. Assuming no error occurs, it is
93361** set to the offset immediately following the last byte of the last
93362** PMA before returning. If an error does occur, then the final value of
93363** *piOffset is undefined.
93364*/
93365static int vdbeMergeEngineLevel0(
93366 SortSubtask *pTask, /* Sorter task to read from */
93367 int nPMA, /* Number of PMAs to read */
93368 i64 *piOffset, /* IN/OUT: Readr offset in pTask->file */
93369 MergeEngine **ppOut /* OUT: New merge-engine */
93370){
93371 MergeEngine *pNew; /* Merge engine to return */
93372 i64 iOff = *piOffset;
93373 int i;
93374 int rc = SQLITE_OK;
93375
93376 *ppOut = pNew = vdbeMergeEngineNew(nPMA);
93377 if( pNew==0 ) rc = SQLITE_NOMEM_BKPT;
93378
93379 for(i=0; i<nPMA && rc==SQLITE_OK; i++){
93380 i64 nDummy = 0;
93381 PmaReader *pReadr = &pNew->aReadr[i];
93382 rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy);
93383 iOff = pReadr->iEof;
93384 }
93385
93386 if( rc!=SQLITE_OK ){
93387 vdbeMergeEngineFree(pNew);
93388 *ppOut = 0;
93389 }
93390 *piOffset = iOff;
93391 return rc;
93392}
93393
93394/*
93395** Return the depth of a tree comprising nPMA PMAs, assuming a fanout of
93396** SORTER_MAX_MERGE_COUNT. The returned value does not include leaf nodes.
93397**
93398** i.e.
93399**
93400** nPMA<=16 -> TreeDepth() == 0
93401** nPMA<=256 -> TreeDepth() == 1
93402** nPMA<=65536 -> TreeDepth() == 2
93403*/
93404static int vdbeSorterTreeDepth(int nPMA){
93405 int nDepth = 0;
93406 i64 nDiv = SORTER_MAX_MERGE_COUNT;
93407 while( nDiv < (i64)nPMA ){
93408 nDiv = nDiv * SORTER_MAX_MERGE_COUNT;
93409 nDepth++;
93410 }
93411 return nDepth;
93412}
93413
93414/*
93415** pRoot is the root of an incremental merge-tree with depth nDepth (according
93416** to vdbeSorterTreeDepth()). pLeaf is the iSeq'th leaf to be added to the
93417** tree, counting from zero. This function adds pLeaf to the tree.
93418**
93419** If successful, SQLITE_OK is returned. If an error occurs, an SQLite error
93420** code is returned and pLeaf is freed.
93421*/
93422static int vdbeSorterAddToTree(
93423 SortSubtask *pTask, /* Task context */
93424 int nDepth, /* Depth of tree according to TreeDepth() */
93425 int iSeq, /* Sequence number of leaf within tree */
93426 MergeEngine *pRoot, /* Root of tree */
93427 MergeEngine *pLeaf /* Leaf to add to tree */
93428){
93429 int rc = SQLITE_OK;
93430 int nDiv = 1;
93431 int i;
93432 MergeEngine *p = pRoot;
93433 IncrMerger *pIncr;
93434
93435 rc = vdbeIncrMergerNew(pTask, pLeaf, &pIncr);
93436
93437 for(i=1; i<nDepth; i++){
93438 nDiv = nDiv * SORTER_MAX_MERGE_COUNT;
93439 }
93440
93441 for(i=1; i<nDepth && rc==SQLITE_OK; i++){
93442 int iIter = (iSeq / nDiv) % SORTER_MAX_MERGE_COUNT;
93443 PmaReader *pReadr = &p->aReadr[iIter];
93444
93445 if( pReadr->pIncr==0 ){
93446 MergeEngine *pNew = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT);
93447 if( pNew==0 ){
93448 rc = SQLITE_NOMEM_BKPT;
93449 }else{
93450 rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr);
93451 }
93452 }
93453 if( rc==SQLITE_OK ){
93454 p = pReadr->pIncr->pMerger;
93455 nDiv = nDiv / SORTER_MAX_MERGE_COUNT;
93456 }
93457 }
93458
93459 if( rc==SQLITE_OK ){
93460 p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT].pIncr = pIncr;
93461 }else{
93462 vdbeIncrFree(pIncr);
93463 }
93464 return rc;
93465}
93466
93467/*
93468** This function is called as part of a SorterRewind() operation on a sorter
93469** that has already written two or more level-0 PMAs to one or more temp
93470** files. It builds a tree of MergeEngine/IncrMerger/PmaReader objects that
93471** can be used to incrementally merge all PMAs on disk.
93472**
93473** If successful, SQLITE_OK is returned and *ppOut set to point to the
93474** MergeEngine object at the root of the tree before returning. Or, if an
93475** error occurs, an SQLite error code is returned and the final value
93476** of *ppOut is undefined.
93477*/
93478static int vdbeSorterMergeTreeBuild(
93479 VdbeSorter *pSorter, /* The VDBE cursor that implements the sort */
93480 MergeEngine **ppOut /* Write the MergeEngine here */
93481){
93482 MergeEngine *pMain = 0;
93483 int rc = SQLITE_OK;
93484 int iTask;
93485
93486#if SQLITE_MAX_WORKER_THREADS>0
93487 /* If the sorter uses more than one task, then create the top-level
93488 ** MergeEngine here. This MergeEngine will read data from exactly
93489 ** one PmaReader per sub-task. */
93490 assert( pSorter->bUseThreads || pSorter->nTask==1 );
93491 if( pSorter->nTask>1 ){
93492 pMain = vdbeMergeEngineNew(pSorter->nTask);
93493 if( pMain==0 ) rc = SQLITE_NOMEM_BKPT;
93494 }
93495#endif
93496
93497 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
93498 SortSubtask *pTask = &pSorter->aTask[iTask];
93499 assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 );
93500 if( SQLITE_MAX_WORKER_THREADS==0 || pTask->nPMA ){
93501 MergeEngine *pRoot = 0; /* Root node of tree for this task */
93502 int nDepth = vdbeSorterTreeDepth(pTask->nPMA);
93503 i64 iReadOff = 0;
93504
93505 if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT ){
93506 rc = vdbeMergeEngineLevel0(pTask, pTask->nPMA, &iReadOff, &pRoot);
93507 }else{
93508 int i;
93509 int iSeq = 0;
93510 pRoot = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT);
93511 if( pRoot==0 ) rc = SQLITE_NOMEM_BKPT;
93512 for(i=0; i<pTask->nPMA && rc==SQLITE_OK; i += SORTER_MAX_MERGE_COUNT){
93513 MergeEngine *pMerger = 0; /* New level-0 PMA merger */
93514 int nReader; /* Number of level-0 PMAs to merge */
93515
93516 nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT);
93517 rc = vdbeMergeEngineLevel0(pTask, nReader, &iReadOff, &pMerger);
93518 if( rc==SQLITE_OK ){
93519 rc = vdbeSorterAddToTree(pTask, nDepth, iSeq++, pRoot, pMerger);
93520 }
93521 }
93522 }
93523
93524 if( rc==SQLITE_OK ){
93525#if SQLITE_MAX_WORKER_THREADS>0
93526 if( pMain!=0 ){
93527 rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr);
93528 }else
93529#endif
93530 {
93531 assert( pMain==0 );
93532 pMain = pRoot;
93533 }
93534 }else{
93535 vdbeMergeEngineFree(pRoot);
93536 }
93537 }
93538 }
93539
93540 if( rc!=SQLITE_OK ){
93541 vdbeMergeEngineFree(pMain);
93542 pMain = 0;
93543 }
93544 *ppOut = pMain;
93545 return rc;
93546}
93547
93548/*
93549** This function is called as part of an sqlite3VdbeSorterRewind() operation
93550** on a sorter that has written two or more PMAs to temporary files. It sets
93551** up either VdbeSorter.pMerger (for single threaded sorters) or pReader
93552** (for multi-threaded sorters) so that it can be used to iterate through
93553** all records stored in the sorter.
93554**
93555** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
93556*/
93557static int vdbeSorterSetupMerge(VdbeSorter *pSorter){
93558 int rc; /* Return code */
93559 SortSubtask *pTask0 = &pSorter->aTask[0];
93560 MergeEngine *pMain = 0;
93561#if SQLITE_MAX_WORKER_THREADS
93562 sqlite3 *db = pTask0->pSorter->db;
93563 int i;
93564 SorterCompare xCompare = vdbeSorterGetCompare(pSorter);
93565 for(i=0; i<pSorter->nTask; i++){
93566 pSorter->aTask[i].xCompare = xCompare;
93567 }
93568#endif
93569
93570 rc = vdbeSorterMergeTreeBuild(pSorter, &pMain);
93571 if( rc==SQLITE_OK ){
93572#if SQLITE_MAX_WORKER_THREADS
93573 assert( pSorter->bUseThreads==0 || pSorter->nTask>1 );
93574 if( pSorter->bUseThreads ){
93575 int iTask;
93576 PmaReader *pReadr = 0;
93577 SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1];
93578 rc = vdbeSortAllocUnpacked(pLast);
93579 if( rc==SQLITE_OK ){
93580 pReadr = (PmaReader*)sqlite3DbMallocZero(db, sizeof(PmaReader));
93581 pSorter->pReader = pReadr;
93582 if( pReadr==0 ) rc = SQLITE_NOMEM_BKPT;
93583 }
93584 if( rc==SQLITE_OK ){
93585 rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr);
93586 if( rc==SQLITE_OK ){
93587 vdbeIncrMergerSetThreads(pReadr->pIncr);
93588 for(iTask=0; iTask<(pSorter->nTask-1); iTask++){
93589 IncrMerger *pIncr;
93590 if( (pIncr = pMain->aReadr[iTask].pIncr) ){
93591 vdbeIncrMergerSetThreads(pIncr);
93592 assert( pIncr->pTask!=pLast );
93593 }
93594 }
93595 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
93596 /* Check that:
93597 **
93598 ** a) The incremental merge object is configured to use the
93599 ** right task, and
93600 ** b) If it is using task (nTask-1), it is configured to run
93601 ** in single-threaded mode. This is important, as the
93602 ** root merge (INCRINIT_ROOT) will be using the same task
93603 ** object.
93604 */
93605 PmaReader *p = &pMain->aReadr[iTask];
93606 assert( p->pIncr==0 || (
93607 (p->pIncr->pTask==&pSorter->aTask[iTask]) /* a */
93608 && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0) /* b */
93609 ));
93610 rc = vdbePmaReaderIncrInit(p, INCRINIT_TASK);
93611 }
93612 }
93613 pMain = 0;
93614 }
93615 if( rc==SQLITE_OK ){
93616 rc = vdbePmaReaderIncrMergeInit(pReadr, INCRINIT_ROOT);
93617 }
93618 }else
93619#endif
93620 {
93621 rc = vdbeMergeEngineInit(pTask0, pMain, INCRINIT_NORMAL);
93622 pSorter->pMerger = pMain;
93623 pMain = 0;
93624 }
93625 }
93626
93627 if( rc!=SQLITE_OK ){
93628 vdbeMergeEngineFree(pMain);
93629 }
93630 return rc;
93631}
93632
93633
93634/*
93635** Once the sorter has been populated by calls to sqlite3VdbeSorterWrite,
93636** this function is called to prepare for iterating through the records
93637** in sorted order.
93638*/
93639SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *pCsr, int *pbEof){
93640 VdbeSorter *pSorter;
93641 int rc = SQLITE_OK; /* Return code */
93642
93643 assert( pCsr->eCurType==CURTYPE_SORTER );
93644 pSorter = pCsr->uc.pSorter;
93645 assert( pSorter );
93646
93647 /* If no data has been written to disk, then do not do so now. Instead,
93648 ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
93649 ** from the in-memory list. */
93650 if( pSorter->bUsePMA==0 ){
93651 if( pSorter->list.pList ){
93652 *pbEof = 0;
93653 rc = vdbeSorterSort(&pSorter->aTask[0], &pSorter->list);
93654 }else{
93655 *pbEof = 1;
93656 }
93657 return rc;
93658 }
93659
93660 /* Write the current in-memory list to a PMA. When the VdbeSorterWrite()
93661 ** function flushes the contents of memory to disk, it immediately always
93662 ** creates a new list consisting of a single key immediately afterwards.
93663 ** So the list is never empty at this point. */
93664 assert( pSorter->list.pList );
93665 rc = vdbeSorterFlushPMA(pSorter);
93666
93667 /* Join all threads */
93668 rc = vdbeSorterJoinAll(pSorter, rc);
93669
93670 vdbeSorterRewindDebug("rewind");
93671
93672 /* Assuming no errors have occurred, set up a merger structure to
93673 ** incrementally read and merge all remaining PMAs. */
93674 assert( pSorter->pReader==0 );
93675 if( rc==SQLITE_OK ){
93676 rc = vdbeSorterSetupMerge(pSorter);
93677 *pbEof = 0;
93678 }
93679
93680 vdbeSorterRewindDebug("rewinddone");
93681 return rc;
93682}
93683
93684/*
93685** Advance to the next element in the sorter. Return value:
93686**
93687** SQLITE_OK success
93688** SQLITE_DONE end of data
93689** otherwise some kind of error.
93690*/
93691SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr){
93692 VdbeSorter *pSorter;
93693 int rc; /* Return code */
93694
93695 assert( pCsr->eCurType==CURTYPE_SORTER );
93696 pSorter = pCsr->uc.pSorter;
93697 assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) );
93698 if( pSorter->bUsePMA ){
93699 assert( pSorter->pReader==0 || pSorter->pMerger==0 );
93700 assert( pSorter->bUseThreads==0 || pSorter->pReader );
93701 assert( pSorter->bUseThreads==1 || pSorter->pMerger );
93702#if SQLITE_MAX_WORKER_THREADS>0
93703 if( pSorter->bUseThreads ){
93704 rc = vdbePmaReaderNext(pSorter->pReader);
93705 if( rc==SQLITE_OK && pSorter->pReader->pFd==0 ) rc = SQLITE_DONE;
93706 }else
93707#endif
93708 /*if( !pSorter->bUseThreads )*/ {
93709 int res = 0;
93710 assert( pSorter->pMerger!=0 );
93711 assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) );
93712 rc = vdbeMergeEngineStep(pSorter->pMerger, &res);
93713 if( rc==SQLITE_OK && res ) rc = SQLITE_DONE;
93714 }
93715 }else{
93716 SorterRecord *pFree = pSorter->list.pList;
93717 pSorter->list.pList = pFree->u.pNext;
93718 pFree->u.pNext = 0;
93719 if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree);
93720 rc = pSorter->list.pList ? SQLITE_OK : SQLITE_DONE;
93721 }
93722 return rc;
93723}
93724
93725/*
93726** Return a pointer to a buffer owned by the sorter that contains the
93727** current key.
93728*/
93729static void *vdbeSorterRowkey(
93730 const VdbeSorter *pSorter, /* Sorter object */
93731 int *pnKey /* OUT: Size of current key in bytes */
93732){
93733 void *pKey;
93734 if( pSorter->bUsePMA ){
93735 PmaReader *pReader;
93736#if SQLITE_MAX_WORKER_THREADS>0
93737 if( pSorter->bUseThreads ){
93738 pReader = pSorter->pReader;
93739 }else
93740#endif
93741 /*if( !pSorter->bUseThreads )*/{
93742 pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]];
93743 }
93744 *pnKey = pReader->nKey;
93745 pKey = pReader->aKey;
93746 }else{
93747 *pnKey = pSorter->list.pList->nVal;
93748 pKey = SRVAL(pSorter->list.pList);
93749 }
93750 return pKey;
93751}
93752
93753/*
93754** Copy the current sorter key into the memory cell pOut.
93755*/
93756SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){
93757 VdbeSorter *pSorter;
93758 void *pKey; int nKey; /* Sorter key to copy into pOut */
93759
93760 assert( pCsr->eCurType==CURTYPE_SORTER );
93761 pSorter = pCsr->uc.pSorter;
93762 pKey = vdbeSorterRowkey(pSorter, &nKey);
93763 if( sqlite3VdbeMemClearAndResize(pOut, nKey) ){
93764 return SQLITE_NOMEM_BKPT;
93765 }
93766 pOut->n = nKey;
93767 MemSetTypeFlag(pOut, MEM_Blob);
93768 memcpy(pOut->z, pKey, nKey);
93769
93770 return SQLITE_OK;
93771}
93772
93773/*
93774** Compare the key in memory cell pVal with the key that the sorter cursor
93775** passed as the first argument currently points to. For the purposes of
93776** the comparison, ignore the rowid field at the end of each record.
93777**
93778** If the sorter cursor key contains any NULL values, consider it to be
93779** less than pVal. Even if pVal also contains NULL values.
93780**
93781** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
93782** Otherwise, set *pRes to a negative, zero or positive value if the
93783** key in pVal is smaller than, equal to or larger than the current sorter
93784** key.
93785**
93786** This routine forms the core of the OP_SorterCompare opcode, which in
93787** turn is used to verify uniqueness when constructing a UNIQUE INDEX.
93788*/
93789SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
93790 const VdbeCursor *pCsr, /* Sorter cursor */
93791 Mem *pVal, /* Value to compare to current sorter key */
93792 int nKeyCol, /* Compare this many columns */
93793 int *pRes /* OUT: Result of comparison */
93794){
93795 VdbeSorter *pSorter;
93796 UnpackedRecord *r2;
93797 KeyInfo *pKeyInfo;
93798 int i;
93799 void *pKey; int nKey; /* Sorter key to compare pVal with */
93800
93801 assert( pCsr->eCurType==CURTYPE_SORTER );
93802 pSorter = pCsr->uc.pSorter;
93803 r2 = pSorter->pUnpacked;
93804 pKeyInfo = pCsr->pKeyInfo;
93805 if( r2==0 ){
93806 r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
93807 if( r2==0 ) return SQLITE_NOMEM_BKPT;
93808 r2->nField = nKeyCol;
93809 }
93810 assert( r2->nField==nKeyCol );
93811
93812 pKey = vdbeSorterRowkey(pSorter, &nKey);
93813 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, r2);
93814 for(i=0; i<nKeyCol; i++){
93815 if( r2->aMem[i].flags & MEM_Null ){
93816 *pRes = -1;
93817 return SQLITE_OK;
93818 }
93819 }
93820
93821 *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2);
93822 return SQLITE_OK;
93823}
93824
93825/************** End of vdbesort.c ********************************************/
93826/************** Begin file memjournal.c **************************************/
93827/*
93828** 2008 October 7
93829**
93830** The author disclaims copyright to this source code. In place of
93831** a legal notice, here is a blessing:
93832**
93833** May you do good and not evil.
93834** May you find forgiveness for yourself and forgive others.
93835** May you share freely, never taking more than you give.
93836**
93837*************************************************************************
93838**
93839** This file contains code use to implement an in-memory rollback journal.
93840** The in-memory rollback journal is used to journal transactions for
93841** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
93842**
93843** Update: The in-memory journal is also used to temporarily cache
93844** smaller journals that are not critical for power-loss recovery.
93845** For example, statement journals that are not too big will be held
93846** entirely in memory, thus reducing the number of file I/O calls, and
93847** more importantly, reducing temporary file creation events. If these
93848** journals become too large for memory, they are spilled to disk. But
93849** in the common case, they are usually small and no file I/O needs to
93850** occur.
93851*/
93852/* #include "sqliteInt.h" */
93853
93854/* Forward references to internal structures */
93855typedef struct MemJournal MemJournal;
93856typedef struct FilePoint FilePoint;
93857typedef struct FileChunk FileChunk;
93858
93859/*
93860** The rollback journal is composed of a linked list of these structures.
93861**
93862** The zChunk array is always at least 8 bytes in size - usually much more.
93863** Its actual size is stored in the MemJournal.nChunkSize variable.
93864*/
93865struct FileChunk {
93866 FileChunk *pNext; /* Next chunk in the journal */
93867 u8 zChunk[8]; /* Content of this chunk */
93868};
93869
93870/*
93871** By default, allocate this many bytes of memory for each FileChunk object.
93872*/
93873#define MEMJOURNAL_DFLT_FILECHUNKSIZE 1024
93874
93875/*
93876** For chunk size nChunkSize, return the number of bytes that should
93877** be allocated for each FileChunk structure.
93878*/
93879#define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8))
93880
93881/*
93882** An instance of this object serves as a cursor into the rollback journal.
93883** The cursor can be either for reading or writing.
93884*/
93885struct FilePoint {
93886 sqlite3_int64 iOffset; /* Offset from the beginning of the file */
93887 FileChunk *pChunk; /* Specific chunk into which cursor points */
93888};
93889
93890/*
93891** This structure is a subclass of sqlite3_file. Each open memory-journal
93892** is an instance of this class.
93893*/
93894struct MemJournal {
93895 const sqlite3_io_methods *pMethod; /* Parent class. MUST BE FIRST */
93896 int nChunkSize; /* In-memory chunk-size */
93897
93898 int nSpill; /* Bytes of data before flushing */
93899 int nSize; /* Bytes of data currently in memory */
93900 FileChunk *pFirst; /* Head of in-memory chunk-list */
93901 FilePoint endpoint; /* Pointer to the end of the file */
93902 FilePoint readpoint; /* Pointer to the end of the last xRead() */
93903
93904 int flags; /* xOpen flags */
93905 sqlite3_vfs *pVfs; /* The "real" underlying VFS */
93906 const char *zJournal; /* Name of the journal file */
93907};
93908
93909/*
93910** Read data from the in-memory journal file. This is the implementation
93911** of the sqlite3_vfs.xRead method.
93912*/
93913static int memjrnlRead(
93914 sqlite3_file *pJfd, /* The journal file from which to read */
93915 void *zBuf, /* Put the results here */
93916 int iAmt, /* Number of bytes to read */
93917 sqlite_int64 iOfst /* Begin reading at this offset */
93918){
93919 MemJournal *p = (MemJournal *)pJfd;
93920 u8 *zOut = zBuf;
93921 int nRead = iAmt;
93922 int iChunkOffset;
93923 FileChunk *pChunk;
93924
93925#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
93926 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
93927 if( (iAmt+iOfst)>p->endpoint.iOffset ){
93928 return SQLITE_IOERR_SHORT_READ;
93929 }
93930#endif
93931
93932 assert( (iAmt+iOfst)<=p->endpoint.iOffset );
93933 assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 );
93934 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
93935 sqlite3_int64 iOff = 0;
93936 for(pChunk=p->pFirst;
93937 ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst;
93938 pChunk=pChunk->pNext
93939 ){
93940 iOff += p->nChunkSize;
93941 }
93942 }else{
93943 pChunk = p->readpoint.pChunk;
93944 assert( pChunk!=0 );
93945 }
93946
93947 iChunkOffset = (int)(iOfst%p->nChunkSize);
93948 do {
93949 int iSpace = p->nChunkSize - iChunkOffset;
93950 int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset));
93951 memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
93952 zOut += nCopy;
93953 nRead -= iSpace;
93954 iChunkOffset = 0;
93955 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
93956 p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
93957 p->readpoint.pChunk = pChunk;
93958
93959 return SQLITE_OK;
93960}
93961
93962/*
93963** Free the list of FileChunk structures headed at MemJournal.pFirst.
93964*/
93965static void memjrnlFreeChunks(MemJournal *p){
93966 FileChunk *pIter;
93967 FileChunk *pNext;
93968 for(pIter=p->pFirst; pIter; pIter=pNext){
93969 pNext = pIter->pNext;
93970 sqlite3_free(pIter);
93971 }
93972 p->pFirst = 0;
93973}
93974
93975/*
93976** Flush the contents of memory to a real file on disk.
93977*/
93978static int memjrnlCreateFile(MemJournal *p){
93979 int rc;
93980 sqlite3_file *pReal = (sqlite3_file*)p;
93981 MemJournal copy = *p;
93982
93983 memset(p, 0, sizeof(MemJournal));
93984 rc = sqlite3OsOpen(copy.pVfs, copy.zJournal, pReal, copy.flags, 0);
93985 if( rc==SQLITE_OK ){
93986 int nChunk = copy.nChunkSize;
93987 i64 iOff = 0;
93988 FileChunk *pIter;
93989 for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
93990 if( iOff + nChunk > copy.endpoint.iOffset ){
93991 nChunk = copy.endpoint.iOffset - iOff;
93992 }
93993 rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff);
93994 if( rc ) break;
93995 iOff += nChunk;
93996 }
93997 if( rc==SQLITE_OK ){
93998 /* No error has occurred. Free the in-memory buffers. */
93999 memjrnlFreeChunks(&copy);
94000 }
94001 }
94002 if( rc!=SQLITE_OK ){
94003 /* If an error occurred while creating or writing to the file, restore
94004 ** the original before returning. This way, SQLite uses the in-memory
94005 ** journal data to roll back changes made to the internal page-cache
94006 ** before this function was called. */
94007 sqlite3OsClose(pReal);
94008 *p = copy;
94009 }
94010 return rc;
94011}
94012
94013
94014/*
94015** Write data to the file.
94016*/
94017static int memjrnlWrite(
94018 sqlite3_file *pJfd, /* The journal file into which to write */
94019 const void *zBuf, /* Take data to be written from here */
94020 int iAmt, /* Number of bytes to write */
94021 sqlite_int64 iOfst /* Begin writing at this offset into the file */
94022){
94023 MemJournal *p = (MemJournal *)pJfd;
94024 int nWrite = iAmt;
94025 u8 *zWrite = (u8 *)zBuf;
94026
94027 /* If the file should be created now, create it and write the new data
94028 ** into the file on disk. */
94029 if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
94030 int rc = memjrnlCreateFile(p);
94031 if( rc==SQLITE_OK ){
94032 rc = sqlite3OsWrite(pJfd, zBuf, iAmt, iOfst);
94033 }
94034 return rc;
94035 }
94036
94037 /* If the contents of this write should be stored in memory */
94038 else{
94039 /* An in-memory journal file should only ever be appended to. Random
94040 ** access writes are not required. The only exception to this is when
94041 ** the in-memory journal is being used by a connection using the
94042 ** atomic-write optimization. In this case the first 28 bytes of the
94043 ** journal file may be written as part of committing the transaction. */
94044 assert( iOfst==p->endpoint.iOffset || iOfst==0 );
94045#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
94046 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
94047 if( iOfst==0 && p->pFirst ){
94048 assert( p->nChunkSize>iAmt );
94049 memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
94050 }else
94051#else
94052 assert( iOfst>0 || p->pFirst==0 );
94053#endif
94054 {
94055 while( nWrite>0 ){
94056 FileChunk *pChunk = p->endpoint.pChunk;
94057 int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
94058 int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset);
94059
94060 if( iChunkOffset==0 ){
94061 /* New chunk is required to extend the file. */
94062 FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize));
94063 if( !pNew ){
94064 return SQLITE_IOERR_NOMEM_BKPT;
94065 }
94066 pNew->pNext = 0;
94067 if( pChunk ){
94068 assert( p->pFirst );
94069 pChunk->pNext = pNew;
94070 }else{
94071 assert( !p->pFirst );
94072 p->pFirst = pNew;
94073 }
94074 p->endpoint.pChunk = pNew;
94075 }
94076
94077 memcpy((u8*)p->endpoint.pChunk->zChunk + iChunkOffset, zWrite, iSpace);
94078 zWrite += iSpace;
94079 nWrite -= iSpace;
94080 p->endpoint.iOffset += iSpace;
94081 }
94082 p->nSize = iAmt + iOfst;
94083 }
94084 }
94085
94086 return SQLITE_OK;
94087}
94088
94089/*
94090** Truncate the file.
94091**
94092** If the journal file is already on disk, truncate it there. Or, if it
94093** is still in main memory but is being truncated to zero bytes in size,
94094** ignore
94095*/
94096static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
94097 MemJournal *p = (MemJournal *)pJfd;
94098 if( ALWAYS(size==0) ){
94099 memjrnlFreeChunks(p);
94100 p->nSize = 0;
94101 p->endpoint.pChunk = 0;
94102 p->endpoint.iOffset = 0;
94103 p->readpoint.pChunk = 0;
94104 p->readpoint.iOffset = 0;
94105 }
94106 return SQLITE_OK;
94107}
94108
94109/*
94110** Close the file.
94111*/
94112static int memjrnlClose(sqlite3_file *pJfd){
94113 MemJournal *p = (MemJournal *)pJfd;
94114 memjrnlFreeChunks(p);
94115 return SQLITE_OK;
94116}
94117
94118/*
94119** Sync the file.
94120**
94121** If the real file has been created, call its xSync method. Otherwise,
94122** syncing an in-memory journal is a no-op.
94123*/
94124static int memjrnlSync(sqlite3_file *pJfd, int flags){
94125 UNUSED_PARAMETER2(pJfd, flags);
94126 return SQLITE_OK;
94127}
94128
94129/*
94130** Query the size of the file in bytes.
94131*/
94132static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
94133 MemJournal *p = (MemJournal *)pJfd;
94134 *pSize = (sqlite_int64) p->endpoint.iOffset;
94135 return SQLITE_OK;
94136}
94137
94138/*
94139** Table of methods for MemJournal sqlite3_file object.
94140*/
94141static const struct sqlite3_io_methods MemJournalMethods = {
94142 1, /* iVersion */
94143 memjrnlClose, /* xClose */
94144 memjrnlRead, /* xRead */
94145 memjrnlWrite, /* xWrite */
94146 memjrnlTruncate, /* xTruncate */
94147 memjrnlSync, /* xSync */
94148 memjrnlFileSize, /* xFileSize */
94149 0, /* xLock */
94150 0, /* xUnlock */
94151 0, /* xCheckReservedLock */
94152 0, /* xFileControl */
94153 0, /* xSectorSize */
94154 0, /* xDeviceCharacteristics */
94155 0, /* xShmMap */
94156 0, /* xShmLock */
94157 0, /* xShmBarrier */
94158 0, /* xShmUnmap */
94159 0, /* xFetch */
94160 0 /* xUnfetch */
94161};
94162
94163/*
94164** Open a journal file.
94165**
94166** The behaviour of the journal file depends on the value of parameter
94167** nSpill. If nSpill is 0, then the journal file is always create and
94168** accessed using the underlying VFS. If nSpill is less than zero, then
94169** all content is always stored in main-memory. Finally, if nSpill is a
94170** positive value, then the journal file is initially created in-memory
94171** but may be flushed to disk later on. In this case the journal file is
94172** flushed to disk either when it grows larger than nSpill bytes in size,
94173** or when sqlite3JournalCreate() is called.
94174*/
94175SQLITE_PRIVATE int sqlite3JournalOpen(
94176 sqlite3_vfs *pVfs, /* The VFS to use for actual file I/O */
94177 const char *zName, /* Name of the journal file */
94178 sqlite3_file *pJfd, /* Preallocated, blank file handle */
94179 int flags, /* Opening flags */
94180 int nSpill /* Bytes buffered before opening the file */
94181){
94182 MemJournal *p = (MemJournal*)pJfd;
94183
94184 /* Zero the file-handle object. If nSpill was passed zero, initialize
94185 ** it using the sqlite3OsOpen() function of the underlying VFS. In this
94186 ** case none of the code in this module is executed as a result of calls
94187 ** made on the journal file-handle. */
94188 memset(p, 0, sizeof(MemJournal));
94189 if( nSpill==0 ){
94190 return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
94191 }
94192
94193 if( nSpill>0 ){
94194 p->nChunkSize = nSpill;
94195 }else{
94196 p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk);
94197 assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) );
94198 }
94199
94200 p->pMethod = (const sqlite3_io_methods*)&MemJournalMethods;
94201 p->nSpill = nSpill;
94202 p->flags = flags;
94203 p->zJournal = zName;
94204 p->pVfs = pVfs;
94205 return SQLITE_OK;
94206}
94207
94208/*
94209** Open an in-memory journal file.
94210*/
94211SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){
94212 sqlite3JournalOpen(0, 0, pJfd, 0, -1);
94213}
94214
94215#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
94216 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
94217/*
94218** If the argument p points to a MemJournal structure that is not an
94219** in-memory-only journal file (i.e. is one that was opened with a +ve
94220** nSpill parameter or as SQLITE_OPEN_MAIN_JOURNAL), and the underlying
94221** file has not yet been created, create it now.
94222*/
94223SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *pJfd){
94224 int rc = SQLITE_OK;
94225 MemJournal *p = (MemJournal*)pJfd;
94226 if( p->pMethod==&MemJournalMethods && (
94227#ifdef SQLITE_ENABLE_ATOMIC_WRITE
94228 p->nSpill>0
94229#else
94230 /* While this appears to not be possible without ATOMIC_WRITE, the
94231 ** paths are complex, so it seems prudent to leave the test in as
94232 ** a NEVER(), in case our analysis is subtly flawed. */
94233 NEVER(p->nSpill>0)
94234#endif
94235#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
94236 || (p->flags & SQLITE_OPEN_MAIN_JOURNAL)
94237#endif
94238 )){
94239 rc = memjrnlCreateFile(p);
94240 }
94241 return rc;
94242}
94243#endif
94244
94245/*
94246** The file-handle passed as the only argument is open on a journal file.
94247** Return true if this "journal file" is currently stored in heap memory,
94248** or false otherwise.
94249*/
94250SQLITE_PRIVATE int sqlite3JournalIsInMemory(sqlite3_file *p){
94251 return p->pMethods==&MemJournalMethods;
94252}
94253
94254/*
94255** Return the number of bytes required to store a JournalFile that uses vfs
94256** pVfs to create the underlying on-disk files.
94257*/
94258SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
94259 return MAX(pVfs->szOsFile, (int)sizeof(MemJournal));
94260}
94261
94262/************** End of memjournal.c ******************************************/
94263/************** Begin file walker.c ******************************************/
94264/*
94265** 2008 August 16
94266**
94267** The author disclaims copyright to this source code. In place of
94268** a legal notice, here is a blessing:
94269**
94270** May you do good and not evil.
94271** May you find forgiveness for yourself and forgive others.
94272** May you share freely, never taking more than you give.
94273**
94274*************************************************************************
94275** This file contains routines used for walking the parser tree for
94276** an SQL statement.
94277*/
94278/* #include "sqliteInt.h" */
94279/* #include <stdlib.h> */
94280/* #include <string.h> */
94281
94282
94283/*
94284** Walk an expression tree. Invoke the callback once for each node
94285** of the expression, while descending. (In other words, the callback
94286** is invoked before visiting children.)
94287**
94288** The return value from the callback should be one of the WRC_*
94289** constants to specify how to proceed with the walk.
94290**
94291** WRC_Continue Continue descending down the tree.
94292**
94293** WRC_Prune Do not descend into child nodes, but allow
94294** the walk to continue with sibling nodes.
94295**
94296** WRC_Abort Do no more callbacks. Unwind the stack and
94297** return from the top-level walk call.
94298**
94299** The return value from this routine is WRC_Abort to abandon the tree walk
94300** and WRC_Continue to continue.
94301*/
94302static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){
94303 int rc;
94304 testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
94305 testcase( ExprHasProperty(pExpr, EP_Reduced) );
94306 while(1){
94307 rc = pWalker->xExprCallback(pWalker, pExpr);
94308 if( rc ) return rc & WRC_Abort;
94309 if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){
94310 if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
94311 assert( pExpr->x.pList==0 || pExpr->pRight==0 );
94312 if( pExpr->pRight ){
94313 pExpr = pExpr->pRight;
94314 continue;
94315 }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
94316 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
94317 }else if( pExpr->x.pList ){
94318 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
94319 }
94320#ifndef SQLITE_OMIT_WINDOWFUNC
94321 if( ExprHasProperty(pExpr, EP_WinFunc) ){
94322 Window *pWin = pExpr->y.pWin;
94323 if( sqlite3WalkExprList(pWalker, pWin->pPartition) ) return WRC_Abort;
94324 if( sqlite3WalkExprList(pWalker, pWin->pOrderBy) ) return WRC_Abort;
94325 if( sqlite3WalkExpr(pWalker, pWin->pFilter) ) return WRC_Abort;
94326 }
94327#endif
94328 }
94329 break;
94330 }
94331 return WRC_Continue;
94332}
94333SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
94334 return pExpr ? walkExpr(pWalker,pExpr) : WRC_Continue;
94335}
94336
94337/*
94338** Call sqlite3WalkExpr() for every expression in list p or until
94339** an abort request is seen.
94340*/
94341SQLITE_PRIVATE int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
94342 int i;
94343 struct ExprList_item *pItem;
94344 if( p ){
94345 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
94346 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
94347 }
94348 }
94349 return WRC_Continue;
94350}
94351
94352/*
94353** Walk all expressions associated with SELECT statement p. Do
94354** not invoke the SELECT callback on p, but do (of course) invoke
94355** any expr callbacks and SELECT callbacks that come from subqueries.
94356** Return WRC_Abort or WRC_Continue.
94357*/
94358SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
94359 if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
94360 if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
94361 if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
94362 if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
94363 if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
94364 if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
94365 return WRC_Continue;
94366}
94367
94368/*
94369** Walk the parse trees associated with all subqueries in the
94370** FROM clause of SELECT statement p. Do not invoke the select
94371** callback on p, but do invoke it on each FROM clause subquery
94372** and on any subqueries further down in the tree. Return
94373** WRC_Abort or WRC_Continue;
94374*/
94375SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
94376 SrcList *pSrc;
94377 int i;
94378 struct SrcList_item *pItem;
94379
94380 pSrc = p->pSrc;
94381 assert( pSrc!=0 );
94382 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
94383 if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
94384 return WRC_Abort;
94385 }
94386 if( pItem->fg.isTabFunc
94387 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
94388 ){
94389 return WRC_Abort;
94390 }
94391 }
94392 return WRC_Continue;
94393}
94394
94395/*
94396** Call sqlite3WalkExpr() for every expression in Select statement p.
94397** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
94398** on the compound select chain, p->pPrior.
94399**
94400** If it is not NULL, the xSelectCallback() callback is invoked before
94401** the walk of the expressions and FROM clause. The xSelectCallback2()
94402** method is invoked following the walk of the expressions and FROM clause,
94403** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
94404** and if the expressions and FROM clause both return WRC_Continue;
94405**
94406** Return WRC_Continue under normal conditions. Return WRC_Abort if
94407** there is an abort request.
94408**
94409** If the Walker does not have an xSelectCallback() then this routine
94410** is a no-op returning WRC_Continue.
94411*/
94412SQLITE_PRIVATE int sqlite3WalkSelect(Walker *pWalker, Select *p){
94413 int rc;
94414 if( p==0 ) return WRC_Continue;
94415 if( pWalker->xSelectCallback==0 ) return WRC_Continue;
94416 do{
94417 rc = pWalker->xSelectCallback(pWalker, p);
94418 if( rc ) return rc & WRC_Abort;
94419 if( sqlite3WalkSelectExpr(pWalker, p)
94420 || sqlite3WalkSelectFrom(pWalker, p)
94421 ){
94422 return WRC_Abort;
94423 }
94424 if( pWalker->xSelectCallback2 ){
94425 pWalker->xSelectCallback2(pWalker, p);
94426 }
94427 p = p->pPrior;
94428 }while( p!=0 );
94429 return WRC_Continue;
94430}
94431
94432/************** End of walker.c **********************************************/
94433/************** Begin file resolve.c *****************************************/
94434/*
94435** 2008 August 18
94436**
94437** The author disclaims copyright to this source code. In place of
94438** a legal notice, here is a blessing:
94439**
94440** May you do good and not evil.
94441** May you find forgiveness for yourself and forgive others.
94442** May you share freely, never taking more than you give.
94443**
94444*************************************************************************
94445**
94446** This file contains routines used for walking the parser tree and
94447** resolve all identifiers by associating them with a particular
94448** table and column.
94449*/
94450/* #include "sqliteInt.h" */
94451
94452/*
94453** Walk the expression tree pExpr and increase the aggregate function
94454** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
94455** This needs to occur when copying a TK_AGG_FUNCTION node from an
94456** outer query into an inner subquery.
94457**
94458** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..)
94459** is a helper function - a callback for the tree walker.
94460*/
94461static int incrAggDepth(Walker *pWalker, Expr *pExpr){
94462 if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
94463 return WRC_Continue;
94464}
94465static void incrAggFunctionDepth(Expr *pExpr, int N){
94466 if( N>0 ){
94467 Walker w;
94468 memset(&w, 0, sizeof(w));
94469 w.xExprCallback = incrAggDepth;
94470 w.u.n = N;
94471 sqlite3WalkExpr(&w, pExpr);
94472 }
94473}
94474
94475/*
94476** Turn the pExpr expression into an alias for the iCol-th column of the
94477** result set in pEList.
94478**
94479** If the reference is followed by a COLLATE operator, then make sure
94480** the COLLATE operator is preserved. For example:
94481**
94482** SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
94483**
94484** Should be transformed into:
94485**
94486** SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
94487**
94488** The nSubquery parameter specifies how many levels of subquery the
94489** alias is removed from the original expression. The usual value is
94490** zero but it might be more if the alias is contained within a subquery
94491** of the original expression. The Expr.op2 field of TK_AGG_FUNCTION
94492** structures must be increased by the nSubquery amount.
94493*/
94494static void resolveAlias(
94495 Parse *pParse, /* Parsing context */
94496 ExprList *pEList, /* A result set */
94497 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
94498 Expr *pExpr, /* Transform this into an alias to the result set */
94499 const char *zType, /* "GROUP" or "ORDER" or "" */
94500 int nSubquery /* Number of subqueries that the label is moving */
94501){
94502 Expr *pOrig; /* The iCol-th column of the result set */
94503 Expr *pDup; /* Copy of pOrig */
94504 sqlite3 *db; /* The database connection */
94505
94506 assert( iCol>=0 && iCol<pEList->nExpr );
94507 pOrig = pEList->a[iCol].pExpr;
94508 assert( pOrig!=0 );
94509 db = pParse->db;
94510 pDup = sqlite3ExprDup(db, pOrig, 0);
94511 if( pDup!=0 ){
94512 if( zType[0]!='G' ) incrAggFunctionDepth(pDup, nSubquery);
94513 if( pExpr->op==TK_COLLATE ){
94514 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
94515 }
94516 ExprSetProperty(pDup, EP_Alias);
94517
94518 /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This
94519 ** prevents ExprDelete() from deleting the Expr structure itself,
94520 ** allowing it to be repopulated by the memcpy() on the following line.
94521 ** The pExpr->u.zToken might point into memory that will be freed by the
94522 ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
94523 ** make a copy of the token before doing the sqlite3DbFree().
94524 */
94525 ExprSetProperty(pExpr, EP_Static);
94526 sqlite3ExprDelete(db, pExpr);
94527 memcpy(pExpr, pDup, sizeof(*pExpr));
94528 if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
94529 assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
94530 pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
94531 pExpr->flags |= EP_MemToken;
94532 }
94533 sqlite3DbFree(db, pDup);
94534 }
94535 ExprSetProperty(pExpr, EP_Alias);
94536}
94537
94538
94539/*
94540** Return TRUE if the name zCol occurs anywhere in the USING clause.
94541**
94542** Return FALSE if the USING clause is NULL or if it does not contain
94543** zCol.
94544*/
94545static int nameInUsingClause(IdList *pUsing, const char *zCol){
94546 if( pUsing ){
94547 int k;
94548 for(k=0; k<pUsing->nId; k++){
94549 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1;
94550 }
94551 }
94552 return 0;
94553}
94554
94555/*
94556** Subqueries stores the original database, table and column names for their
94557** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN".
94558** Check to see if the zSpan given to this routine matches the zDb, zTab,
94559** and zCol. If any of zDb, zTab, and zCol are NULL then those fields will
94560** match anything.
94561*/
94562SQLITE_PRIVATE int sqlite3MatchSpanName(
94563 const char *zSpan,
94564 const char *zCol,
94565 const char *zTab,
94566 const char *zDb
94567){
94568 int n;
94569 for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
94570 if( zDb && (sqlite3StrNICmp(zSpan, zDb, n)!=0 || zDb[n]!=0) ){
94571 return 0;
94572 }
94573 zSpan += n+1;
94574 for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
94575 if( zTab && (sqlite3StrNICmp(zSpan, zTab, n)!=0 || zTab[n]!=0) ){
94576 return 0;
94577 }
94578 zSpan += n+1;
94579 if( zCol && sqlite3StrICmp(zSpan, zCol)!=0 ){
94580 return 0;
94581 }
94582 return 1;
94583}
94584
94585/*
94586** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
94587** that name in the set of source tables in pSrcList and make the pExpr
94588** expression node refer back to that source column. The following changes
94589** are made to pExpr:
94590**
94591** pExpr->iDb Set the index in db->aDb[] of the database X
94592** (even if X is implied).
94593** pExpr->iTable Set to the cursor number for the table obtained
94594** from pSrcList.
94595** pExpr->y.pTab Points to the Table structure of X.Y (even if
94596** X and/or Y are implied.)
94597** pExpr->iColumn Set to the column number within the table.
94598** pExpr->op Set to TK_COLUMN.
94599** pExpr->pLeft Any expression this points to is deleted
94600** pExpr->pRight Any expression this points to is deleted.
94601**
94602** The zDb variable is the name of the database (the "X"). This value may be
94603** NULL meaning that name is of the form Y.Z or Z. Any available database
94604** can be used. The zTable variable is the name of the table (the "Y"). This
94605** value can be NULL if zDb is also NULL. If zTable is NULL it
94606** means that the form of the name is Z and that columns from any table
94607** can be used.
94608**
94609** If the name cannot be resolved unambiguously, leave an error message
94610** in pParse and return WRC_Abort. Return WRC_Prune on success.
94611*/
94612static int lookupName(
94613 Parse *pParse, /* The parsing context */
94614 const char *zDb, /* Name of the database containing table, or NULL */
94615 const char *zTab, /* Name of table containing column, or NULL */
94616 const char *zCol, /* Name of the column. */
94617 NameContext *pNC, /* The name context used to resolve the name */
94618 Expr *pExpr /* Make this EXPR node point to the selected column */
94619){
94620 int i, j; /* Loop counters */
94621 int cnt = 0; /* Number of matching column names */
94622 int cntTab = 0; /* Number of matching table names */
94623 int nSubquery = 0; /* How many levels of subquery */
94624 sqlite3 *db = pParse->db; /* The database connection */
94625 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
94626 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
94627 NameContext *pTopNC = pNC; /* First namecontext in the list */
94628 Schema *pSchema = 0; /* Schema of the expression */
94629 int eNewExprOp = TK_COLUMN; /* New value for pExpr->op on success */
94630 Table *pTab = 0; /* Table hold the row */
94631 Column *pCol; /* A column of pTab */
94632
94633 assert( pNC ); /* the name context cannot be NULL. */
94634 assert( zCol ); /* The Z in X.Y.Z cannot be NULL */
94635 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
94636
94637 /* Initialize the node to no-match */
94638 pExpr->iTable = -1;
94639 ExprSetVVAProperty(pExpr, EP_NoReduce);
94640
94641 /* Translate the schema name in zDb into a pointer to the corresponding
94642 ** schema. If not found, pSchema will remain NULL and nothing will match
94643 ** resulting in an appropriate error message toward the end of this routine
94644 */
94645 if( zDb ){
94646 testcase( pNC->ncFlags & NC_PartIdx );
94647 testcase( pNC->ncFlags & NC_IsCheck );
94648 if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
94649 /* Silently ignore database qualifiers inside CHECK constraints and
94650 ** partial indices. Do not raise errors because that might break
94651 ** legacy and because it does not hurt anything to just ignore the
94652 ** database name. */
94653 zDb = 0;
94654 }else{
94655 for(i=0; i<db->nDb; i++){
94656 assert( db->aDb[i].zDbSName );
94657 if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
94658 pSchema = db->aDb[i].pSchema;
94659 break;
94660 }
94661 }
94662 }
94663 }
94664
94665 /* Start at the inner-most context and move outward until a match is found */
94666 assert( pNC && cnt==0 );
94667 do{
94668 ExprList *pEList;
94669 SrcList *pSrcList = pNC->pSrcList;
94670
94671 if( pSrcList ){
94672 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
94673 pTab = pItem->pTab;
94674 assert( pTab!=0 && pTab->zName!=0 );
94675 assert( pTab->nCol>0 );
94676 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){
94677 int hit = 0;
94678 pEList = pItem->pSelect->pEList;
94679 for(j=0; j<pEList->nExpr; j++){
94680 if( sqlite3MatchSpanName(pEList->a[j].zSpan, zCol, zTab, zDb) ){
94681 cnt++;
94682 cntTab = 2;
94683 pMatch = pItem;
94684 pExpr->iColumn = j;
94685 hit = 1;
94686 }
94687 }
94688 if( hit || zTab==0 ) continue;
94689 }
94690 if( zDb && pTab->pSchema!=pSchema ){
94691 continue;
94692 }
94693 if( zTab ){
94694 const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
94695 assert( zTabName!=0 );
94696 if( sqlite3StrICmp(zTabName, zTab)!=0 ){
94697 continue;
94698 }
94699 if( IN_RENAME_OBJECT && pItem->zAlias ){
94700 sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab);
94701 }
94702 }
94703 if( 0==(cntTab++) ){
94704 pMatch = pItem;
94705 }
94706 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
94707 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
94708 /* If there has been exactly one prior match and this match
94709 ** is for the right-hand table of a NATURAL JOIN or is in a
94710 ** USING clause, then skip this match.
94711 */
94712 if( cnt==1 ){
94713 if( pItem->fg.jointype & JT_NATURAL ) continue;
94714 if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
94715 }
94716 cnt++;
94717 pMatch = pItem;
94718 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
94719 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
94720 break;
94721 }
94722 }
94723 }
94724 if( pMatch ){
94725 pExpr->iTable = pMatch->iCursor;
94726 pExpr->y.pTab = pMatch->pTab;
94727 /* RIGHT JOIN not (yet) supported */
94728 assert( (pMatch->fg.jointype & JT_RIGHT)==0 );
94729 if( (pMatch->fg.jointype & JT_LEFT)!=0 ){
94730 ExprSetProperty(pExpr, EP_CanBeNull);
94731 }
94732 pSchema = pExpr->y.pTab->pSchema;
94733 }
94734 } /* if( pSrcList ) */
94735
94736#if !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT)
94737 /* If we have not already resolved the name, then maybe
94738 ** it is a new.* or old.* trigger argument reference. Or
94739 ** maybe it is an excluded.* from an upsert.
94740 */
94741 if( zDb==0 && zTab!=0 && cntTab==0 ){
94742 pTab = 0;
94743#ifndef SQLITE_OMIT_TRIGGER
94744 if( pParse->pTriggerTab!=0 ){
94745 int op = pParse->eTriggerOp;
94746 assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
94747 if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
94748 pExpr->iTable = 1;
94749 pTab = pParse->pTriggerTab;
94750 }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
94751 pExpr->iTable = 0;
94752 pTab = pParse->pTriggerTab;
94753 }
94754 }
94755#endif /* SQLITE_OMIT_TRIGGER */
94756#ifndef SQLITE_OMIT_UPSERT
94757 if( (pNC->ncFlags & NC_UUpsert)!=0 ){
94758 Upsert *pUpsert = pNC->uNC.pUpsert;
94759 if( pUpsert && sqlite3StrICmp("excluded",zTab)==0 ){
94760 pTab = pUpsert->pUpsertSrc->a[0].pTab;
94761 pExpr->iTable = 2;
94762 }
94763 }
94764#endif /* SQLITE_OMIT_UPSERT */
94765
94766 if( pTab ){
94767 int iCol;
94768 pSchema = pTab->pSchema;
94769 cntTab++;
94770 for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
94771 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
94772 if( iCol==pTab->iPKey ){
94773 iCol = -1;
94774 }
94775 break;
94776 }
94777 }
94778 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
94779 /* IMP: R-51414-32910 */
94780 iCol = -1;
94781 }
94782 if( iCol<pTab->nCol ){
94783 cnt++;
94784#ifndef SQLITE_OMIT_UPSERT
94785 if( pExpr->iTable==2 ){
94786 testcase( iCol==(-1) );
94787 if( IN_RENAME_OBJECT ){
94788 pExpr->iColumn = iCol;
94789 pExpr->y.pTab = pTab;
94790 eNewExprOp = TK_COLUMN;
94791 }else{
94792 pExpr->iTable = pNC->uNC.pUpsert->regData + iCol;
94793 eNewExprOp = TK_REGISTER;
94794 ExprSetProperty(pExpr, EP_Alias);
94795 }
94796 }else
94797#endif /* SQLITE_OMIT_UPSERT */
94798 {
94799#ifndef SQLITE_OMIT_TRIGGER
94800 if( iCol<0 ){
94801 pExpr->affinity = SQLITE_AFF_INTEGER;
94802 }else if( pExpr->iTable==0 ){
94803 testcase( iCol==31 );
94804 testcase( iCol==32 );
94805 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
94806 }else{
94807 testcase( iCol==31 );
94808 testcase( iCol==32 );
94809 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
94810 }
94811 pExpr->y.pTab = pTab;
94812 pExpr->iColumn = (i16)iCol;
94813 eNewExprOp = TK_TRIGGER;
94814#endif /* SQLITE_OMIT_TRIGGER */
94815 }
94816 }
94817 }
94818 }
94819#endif /* !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT) */
94820
94821 /*
94822 ** Perhaps the name is a reference to the ROWID
94823 */
94824 if( cnt==0
94825 && cntTab==1
94826 && pMatch
94827 && (pNC->ncFlags & NC_IdxExpr)==0
94828 && sqlite3IsRowid(zCol)
94829 && VisibleRowid(pMatch->pTab)
94830 ){
94831 cnt = 1;
94832 pExpr->iColumn = -1;
94833 pExpr->affinity = SQLITE_AFF_INTEGER;
94834 }
94835
94836 /*
94837 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
94838 ** might refer to an result-set alias. This happens, for example, when
94839 ** we are resolving names in the WHERE clause of the following command:
94840 **
94841 ** SELECT a+b AS x FROM table WHERE x<10;
94842 **
94843 ** In cases like this, replace pExpr with a copy of the expression that
94844 ** forms the result set entry ("a+b" in the example) and return immediately.
94845 ** Note that the expression in the result set should have already been
94846 ** resolved by the time the WHERE clause is resolved.
94847 **
94848 ** The ability to use an output result-set column in the WHERE, GROUP BY,
94849 ** or HAVING clauses, or as part of a larger expression in the ORDER BY
94850 ** clause is not standard SQL. This is a (goofy) SQLite extension, that
94851 ** is supported for backwards compatibility only. Hence, we issue a warning
94852 ** on sqlite3_log() whenever the capability is used.
94853 */
94854 if( (pNC->ncFlags & NC_UEList)!=0
94855 && cnt==0
94856 && zTab==0
94857 ){
94858 pEList = pNC->uNC.pEList;
94859 assert( pEList!=0 );
94860 for(j=0; j<pEList->nExpr; j++){
94861 char *zAs = pEList->a[j].zName;
94862 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
94863 Expr *pOrig;
94864 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
94865 assert( pExpr->x.pList==0 );
94866 assert( pExpr->x.pSelect==0 );
94867 pOrig = pEList->a[j].pExpr;
94868 if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
94869 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
94870 return WRC_Abort;
94871 }
94872 if( sqlite3ExprVectorSize(pOrig)!=1 ){
94873 sqlite3ErrorMsg(pParse, "row value misused");
94874 return WRC_Abort;
94875 }
94876 resolveAlias(pParse, pEList, j, pExpr, "", nSubquery);
94877 cnt = 1;
94878 pMatch = 0;
94879 assert( zTab==0 && zDb==0 );
94880 if( IN_RENAME_OBJECT ){
94881 sqlite3RenameTokenRemap(pParse, 0, (void*)pExpr);
94882 }
94883 goto lookupname_end;
94884 }
94885 }
94886 }
94887
94888 /* Advance to the next name context. The loop will exit when either
94889 ** we have a match (cnt>0) or when we run out of name contexts.
94890 */
94891 if( cnt ) break;
94892 pNC = pNC->pNext;
94893 nSubquery++;
94894 }while( pNC );
94895
94896
94897 /*
94898 ** If X and Y are NULL (in other words if only the column name Z is
94899 ** supplied) and the value of Z is enclosed in double-quotes, then
94900 ** Z is a string literal if it doesn't match any column names. In that
94901 ** case, we need to return right away and not make any changes to
94902 ** pExpr.
94903 **
94904 ** Because no reference was made to outer contexts, the pNC->nRef
94905 ** fields are not changed in any context.
94906 */
94907 if( cnt==0 && zTab==0 ){
94908 assert( pExpr->op==TK_ID );
94909 if( ExprHasProperty(pExpr,EP_DblQuoted) ){
94910 pExpr->op = TK_STRING;
94911 pExpr->y.pTab = 0;
94912 return WRC_Prune;
94913 }
94914 if( sqlite3ExprIdToTrueFalse(pExpr) ){
94915 return WRC_Prune;
94916 }
94917 }
94918
94919 /*
94920 ** cnt==0 means there was not match. cnt>1 means there were two or
94921 ** more matches. Either way, we have an error.
94922 */
94923 if( cnt!=1 ){
94924 const char *zErr;
94925 zErr = cnt==0 ? "no such column" : "ambiguous column name";
94926 if( zDb ){
94927 sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
94928 }else if( zTab ){
94929 sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
94930 }else{
94931 sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
94932 }
94933 pParse->checkSchema = 1;
94934 pTopNC->nErr++;
94935 }
94936
94937 /* If a column from a table in pSrcList is referenced, then record
94938 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
94939 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
94940 ** column number is greater than the number of bits in the bitmask
94941 ** then set the high-order bit of the bitmask.
94942 */
94943 if( pExpr->iColumn>=0 && pMatch!=0 ){
94944 int n = pExpr->iColumn;
94945 testcase( n==BMS-1 );
94946 if( n>=BMS ){
94947 n = BMS-1;
94948 }
94949 assert( pMatch->iCursor==pExpr->iTable );
94950 pMatch->colUsed |= ((Bitmask)1)<<n;
94951 }
94952
94953 /* Clean up and return
94954 */
94955 sqlite3ExprDelete(db, pExpr->pLeft);
94956 pExpr->pLeft = 0;
94957 sqlite3ExprDelete(db, pExpr->pRight);
94958 pExpr->pRight = 0;
94959 pExpr->op = eNewExprOp;
94960 ExprSetProperty(pExpr, EP_Leaf);
94961lookupname_end:
94962 if( cnt==1 ){
94963 assert( pNC!=0 );
94964 if( !ExprHasProperty(pExpr, EP_Alias) ){
94965 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
94966 }
94967 /* Increment the nRef value on all name contexts from TopNC up to
94968 ** the point where the name matched. */
94969 for(;;){
94970 assert( pTopNC!=0 );
94971 pTopNC->nRef++;
94972 if( pTopNC==pNC ) break;
94973 pTopNC = pTopNC->pNext;
94974 }
94975 return WRC_Prune;
94976 } else {
94977 return WRC_Abort;
94978 }
94979}
94980
94981/*
94982** Allocate and return a pointer to an expression to load the column iCol
94983** from datasource iSrc in SrcList pSrc.
94984*/
94985SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
94986 Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
94987 if( p ){
94988 struct SrcList_item *pItem = &pSrc->a[iSrc];
94989 p->y.pTab = pItem->pTab;
94990 p->iTable = pItem->iCursor;
94991 if( p->y.pTab->iPKey==iCol ){
94992 p->iColumn = -1;
94993 }else{
94994 p->iColumn = (ynVar)iCol;
94995 testcase( iCol==BMS );
94996 testcase( iCol==BMS-1 );
94997 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
94998 }
94999 }
95000 return p;
95001}
95002
95003/*
95004** Report an error that an expression is not valid for some set of
95005** pNC->ncFlags values determined by validMask.
95006*/
95007static void notValid(
95008 Parse *pParse, /* Leave error message here */
95009 NameContext *pNC, /* The name context */
95010 const char *zMsg, /* Type of error */
95011 int validMask /* Set of contexts for which prohibited */
95012){
95013 assert( (validMask&~(NC_IsCheck|NC_PartIdx|NC_IdxExpr))==0 );
95014 if( (pNC->ncFlags & validMask)!=0 ){
95015 const char *zIn = "partial index WHERE clauses";
95016 if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions";
95017#ifndef SQLITE_OMIT_CHECK
95018 else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints";
95019#endif
95020 sqlite3ErrorMsg(pParse, "%s prohibited in %s", zMsg, zIn);
95021 }
95022}
95023
95024/*
95025** Expression p should encode a floating point value between 1.0 and 0.0.
95026** Return 1024 times this value. Or return -1 if p is not a floating point
95027** value between 1.0 and 0.0.
95028*/
95029static int exprProbability(Expr *p){
95030 double r = -1.0;
95031 if( p->op!=TK_FLOAT ) return -1;
95032 sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
95033 assert( r>=0.0 );
95034 if( r>1.0 ) return -1;
95035 return (int)(r*134217728.0);
95036}
95037
95038/*
95039** This routine is callback for sqlite3WalkExpr().
95040**
95041** Resolve symbolic names into TK_COLUMN operators for the current
95042** node in the expression tree. Return 0 to continue the search down
95043** the tree or 2 to abort the tree walk.
95044**
95045** This routine also does error checking and name resolution for
95046** function names. The operator for aggregate functions is changed
95047** to TK_AGG_FUNCTION.
95048*/
95049static int resolveExprStep(Walker *pWalker, Expr *pExpr){
95050 NameContext *pNC;
95051 Parse *pParse;
95052
95053 pNC = pWalker->u.pNC;
95054 assert( pNC!=0 );
95055 pParse = pNC->pParse;
95056 assert( pParse==pWalker->pParse );
95057
95058#ifndef NDEBUG
95059 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
95060 SrcList *pSrcList = pNC->pSrcList;
95061 int i;
95062 for(i=0; i<pNC->pSrcList->nSrc; i++){
95063 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
95064 }
95065 }
95066#endif
95067 switch( pExpr->op ){
95068
95069#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
95070 /* The special operator TK_ROW means use the rowid for the first
95071 ** column in the FROM clause. This is used by the LIMIT and ORDER BY
95072 ** clause processing on UPDATE and DELETE statements.
95073 */
95074 case TK_ROW: {
95075 SrcList *pSrcList = pNC->pSrcList;
95076 struct SrcList_item *pItem;
95077 assert( pSrcList && pSrcList->nSrc==1 );
95078 pItem = pSrcList->a;
95079 assert( HasRowid(pItem->pTab) && pItem->pTab->pSelect==0 );
95080 pExpr->op = TK_COLUMN;
95081 pExpr->y.pTab = pItem->pTab;
95082 pExpr->iTable = pItem->iCursor;
95083 pExpr->iColumn = -1;
95084 pExpr->affinity = SQLITE_AFF_INTEGER;
95085 break;
95086 }
95087#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
95088 && !defined(SQLITE_OMIT_SUBQUERY) */
95089
95090 /* A column name: ID
95091 ** Or table name and column name: ID.ID
95092 ** Or a database, table and column: ID.ID.ID
95093 **
95094 ** The TK_ID and TK_OUT cases are combined so that there will only
95095 ** be one call to lookupName(). Then the compiler will in-line
95096 ** lookupName() for a size reduction and performance increase.
95097 */
95098 case TK_ID:
95099 case TK_DOT: {
95100 const char *zColumn;
95101 const char *zTable;
95102 const char *zDb;
95103 Expr *pRight;
95104
95105 if( pExpr->op==TK_ID ){
95106 zDb = 0;
95107 zTable = 0;
95108 zColumn = pExpr->u.zToken;
95109 }else{
95110 Expr *pLeft = pExpr->pLeft;
95111 notValid(pParse, pNC, "the \".\" operator", NC_IdxExpr);
95112 pRight = pExpr->pRight;
95113 if( pRight->op==TK_ID ){
95114 zDb = 0;
95115 }else{
95116 assert( pRight->op==TK_DOT );
95117 zDb = pLeft->u.zToken;
95118 pLeft = pRight->pLeft;
95119 pRight = pRight->pRight;
95120 }
95121 zTable = pLeft->u.zToken;
95122 zColumn = pRight->u.zToken;
95123 if( IN_RENAME_OBJECT ){
95124 sqlite3RenameTokenRemap(pParse, (void*)pExpr, (void*)pRight);
95125 sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft);
95126 }
95127 }
95128 return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
95129 }
95130
95131 /* Resolve function names
95132 */
95133 case TK_FUNCTION: {
95134 ExprList *pList = pExpr->x.pList; /* The argument list */
95135 int n = pList ? pList->nExpr : 0; /* Number of arguments */
95136 int no_such_func = 0; /* True if no such function exists */
95137 int wrong_num_args = 0; /* True if wrong number of arguments */
95138 int is_agg = 0; /* True if is an aggregate function */
95139 int nId; /* Number of characters in function name */
95140 const char *zId; /* The function name. */
95141 FuncDef *pDef; /* Information about the function */
95142 u8 enc = ENC(pParse->db); /* The database encoding */
95143
95144 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
95145 zId = pExpr->u.zToken;
95146 nId = sqlite3Strlen30(zId);
95147 pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
95148 if( pDef==0 ){
95149 pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
95150 if( pDef==0 ){
95151 no_such_func = 1;
95152 }else{
95153 wrong_num_args = 1;
95154 }
95155 }else{
95156 is_agg = pDef->xFinalize!=0;
95157 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
95158 ExprSetProperty(pExpr, EP_Unlikely|EP_Skip);
95159 if( n==2 ){
95160 pExpr->iTable = exprProbability(pList->a[1].pExpr);
95161 if( pExpr->iTable<0 ){
95162 sqlite3ErrorMsg(pParse,
95163 "second argument to likelihood() must be a "
95164 "constant between 0.0 and 1.0");
95165 pNC->nErr++;
95166 }
95167 }else{
95168 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
95169 ** equivalent to likelihood(X, 0.0625).
95170 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
95171 ** short-hand for likelihood(X,0.0625).
95172 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
95173 ** for likelihood(X,0.9375).
95174 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
95175 ** to likelihood(X,0.9375). */
95176 /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */
95177 pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
95178 }
95179 }
95180#ifndef SQLITE_OMIT_AUTHORIZATION
95181 {
95182 int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0);
95183 if( auth!=SQLITE_OK ){
95184 if( auth==SQLITE_DENY ){
95185 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
95186 pDef->zName);
95187 pNC->nErr++;
95188 }
95189 pExpr->op = TK_NULL;
95190 return WRC_Prune;
95191 }
95192 }
95193#endif
95194 if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){
95195 /* For the purposes of the EP_ConstFunc flag, date and time
95196 ** functions and other functions that change slowly are considered
95197 ** constant because they are constant for the duration of one query */
95198 ExprSetProperty(pExpr,EP_ConstFunc);
95199 }
95200 if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
95201 /* Date/time functions that use 'now', and other functions like
95202 ** sqlite_version() that might change over time cannot be used
95203 ** in an index. */
95204 notValid(pParse, pNC, "non-deterministic functions",
95205 NC_IdxExpr|NC_PartIdx);
95206 }
95207 if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL)!=0
95208 && pParse->nested==0
95209 && sqlite3Config.bInternalFunctions==0
95210 ){
95211 /* Internal-use-only functions are disallowed unless the
95212 ** SQL is being compiled using sqlite3NestedParse() */
95213 no_such_func = 1;
95214 pDef = 0;
95215 }
95216 }
95217
95218 if( 0==IN_RENAME_OBJECT ){
95219#ifndef SQLITE_OMIT_WINDOWFUNC
95220 assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)
95221 || (pDef->xValue==0 && pDef->xInverse==0)
95222 || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)
95223 );
95224 if( pDef && pDef->xValue==0 && ExprHasProperty(pExpr, EP_WinFunc) ){
95225 sqlite3ErrorMsg(pParse,
95226 "%.*s() may not be used as a window function", nId, zId
95227 );
95228 pNC->nErr++;
95229 }else if(
95230 (is_agg && (pNC->ncFlags & NC_AllowAgg)==0)
95231 || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pExpr->y.pWin)
95232 || (is_agg && pExpr->y.pWin && (pNC->ncFlags & NC_AllowWin)==0)
95233 ){
95234 const char *zType;
95235 if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pExpr->y.pWin ){
95236 zType = "window";
95237 }else{
95238 zType = "aggregate";
95239 }
95240 sqlite3ErrorMsg(pParse, "misuse of %s function %.*s()",zType,nId,zId);
95241 pNC->nErr++;
95242 is_agg = 0;
95243 }
95244#else
95245 if( (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) ){
95246 sqlite3ErrorMsg(pParse,"misuse of aggregate function %.*s()",nId,zId);
95247 pNC->nErr++;
95248 is_agg = 0;
95249 }
95250#endif
95251 else if( no_such_func && pParse->db->init.busy==0
95252#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
95253 && pParse->explain==0
95254#endif
95255 ){
95256 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
95257 pNC->nErr++;
95258 }else if( wrong_num_args ){
95259 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
95260 nId, zId);
95261 pNC->nErr++;
95262 }
95263 if( is_agg ){
95264#ifndef SQLITE_OMIT_WINDOWFUNC
95265 pNC->ncFlags &= ~(pExpr->y.pWin ? NC_AllowWin : NC_AllowAgg);
95266#else
95267 pNC->ncFlags &= ~NC_AllowAgg;
95268#endif
95269 }
95270 }
95271 sqlite3WalkExprList(pWalker, pList);
95272 if( is_agg ){
95273#ifndef SQLITE_OMIT_WINDOWFUNC
95274 if( pExpr->y.pWin ){
95275 Select *pSel = pNC->pWinSelect;
95276 sqlite3WalkExprList(pWalker, pExpr->y.pWin->pPartition);
95277 sqlite3WalkExprList(pWalker, pExpr->y.pWin->pOrderBy);
95278 sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
95279 sqlite3WindowUpdate(pParse, pSel->pWinDefn, pExpr->y.pWin, pDef);
95280 if( 0==pSel->pWin
95281 || 0==sqlite3WindowCompare(pParse, pSel->pWin, pExpr->y.pWin)
95282 ){
95283 pExpr->y.pWin->pNextWin = pSel->pWin;
95284 pSel->pWin = pExpr->y.pWin;
95285 }
95286 pNC->ncFlags |= NC_AllowWin;
95287 }else
95288#endif /* SQLITE_OMIT_WINDOWFUNC */
95289 {
95290 NameContext *pNC2 = pNC;
95291 pExpr->op = TK_AGG_FUNCTION;
95292 pExpr->op2 = 0;
95293 while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
95294 pExpr->op2++;
95295 pNC2 = pNC2->pNext;
95296 }
95297 assert( pDef!=0 );
95298 if( pNC2 ){
95299 assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg );
95300 testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
95301 pNC2->ncFlags |= NC_HasAgg | (pDef->funcFlags & SQLITE_FUNC_MINMAX);
95302
95303 }
95304 pNC->ncFlags |= NC_AllowAgg;
95305 }
95306 }
95307 /* FIX ME: Compute pExpr->affinity based on the expected return
95308 ** type of the function
95309 */
95310 return WRC_Prune;
95311 }
95312#ifndef SQLITE_OMIT_SUBQUERY
95313 case TK_SELECT:
95314 case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
95315#endif
95316 case TK_IN: {
95317 testcase( pExpr->op==TK_IN );
95318 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
95319 int nRef = pNC->nRef;
95320 notValid(pParse, pNC, "subqueries", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
95321 sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
95322 assert( pNC->nRef>=nRef );
95323 if( nRef!=pNC->nRef ){
95324 ExprSetProperty(pExpr, EP_VarSelect);
95325 pNC->ncFlags |= NC_VarSelect;
95326 }
95327 }
95328 break;
95329 }
95330 case TK_VARIABLE: {
95331 notValid(pParse, pNC, "parameters", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
95332 break;
95333 }
95334 case TK_IS:
95335 case TK_ISNOT: {
95336 Expr *pRight;
95337 assert( !ExprHasProperty(pExpr, EP_Reduced) );
95338 /* Handle special cases of "x IS TRUE", "x IS FALSE", "x IS NOT TRUE",
95339 ** and "x IS NOT FALSE". */
95340 if( (pRight = pExpr->pRight)->op==TK_ID ){
95341 int rc = resolveExprStep(pWalker, pRight);
95342 if( rc==WRC_Abort ) return WRC_Abort;
95343 if( pRight->op==TK_TRUEFALSE ){
95344 pExpr->op2 = pExpr->op;
95345 pExpr->op = TK_TRUTH;
95346 return WRC_Continue;
95347 }
95348 }
95349 /* Fall thru */
95350 }
95351 case TK_BETWEEN:
95352 case TK_EQ:
95353 case TK_NE:
95354 case TK_LT:
95355 case TK_LE:
95356 case TK_GT:
95357 case TK_GE: {
95358 int nLeft, nRight;
95359 if( pParse->db->mallocFailed ) break;
95360 assert( pExpr->pLeft!=0 );
95361 nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
95362 if( pExpr->op==TK_BETWEEN ){
95363 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
95364 if( nRight==nLeft ){
95365 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr);
95366 }
95367 }else{
95368 assert( pExpr->pRight!=0 );
95369 nRight = sqlite3ExprVectorSize(pExpr->pRight);
95370 }
95371 if( nLeft!=nRight ){
95372 testcase( pExpr->op==TK_EQ );
95373 testcase( pExpr->op==TK_NE );
95374 testcase( pExpr->op==TK_LT );
95375 testcase( pExpr->op==TK_LE );
95376 testcase( pExpr->op==TK_GT );
95377 testcase( pExpr->op==TK_GE );
95378 testcase( pExpr->op==TK_IS );
95379 testcase( pExpr->op==TK_ISNOT );
95380 testcase( pExpr->op==TK_BETWEEN );
95381 sqlite3ErrorMsg(pParse, "row value misused");
95382 }
95383 break;
95384 }
95385 }
95386 return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
95387}
95388
95389/*
95390** pEList is a list of expressions which are really the result set of the
95391** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause.
95392** This routine checks to see if pE is a simple identifier which corresponds
95393** to the AS-name of one of the terms of the expression list. If it is,
95394** this routine return an integer between 1 and N where N is the number of
95395** elements in pEList, corresponding to the matching entry. If there is
95396** no match, or if pE is not a simple identifier, then this routine
95397** return 0.
95398**
95399** pEList has been resolved. pE has not.
95400*/
95401static int resolveAsName(
95402 Parse *pParse, /* Parsing context for error messages */
95403 ExprList *pEList, /* List of expressions to scan */
95404 Expr *pE /* Expression we are trying to match */
95405){
95406 int i; /* Loop counter */
95407
95408 UNUSED_PARAMETER(pParse);
95409
95410 if( pE->op==TK_ID ){
95411 char *zCol = pE->u.zToken;
95412 for(i=0; i<pEList->nExpr; i++){
95413 char *zAs = pEList->a[i].zName;
95414 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
95415 return i+1;
95416 }
95417 }
95418 }
95419 return 0;
95420}
95421
95422/*
95423** pE is a pointer to an expression which is a single term in the
95424** ORDER BY of a compound SELECT. The expression has not been
95425** name resolved.
95426**
95427** At the point this routine is called, we already know that the
95428** ORDER BY term is not an integer index into the result set. That
95429** case is handled by the calling routine.
95430**
95431** Attempt to match pE against result set columns in the left-most
95432** SELECT statement. Return the index i of the matching column,
95433** as an indication to the caller that it should sort by the i-th column.
95434** The left-most column is 1. In other words, the value returned is the
95435** same integer value that would be used in the SQL statement to indicate
95436** the column.
95437**
95438** If there is no match, return 0. Return -1 if an error occurs.
95439*/
95440static int resolveOrderByTermToExprList(
95441 Parse *pParse, /* Parsing context for error messages */
95442 Select *pSelect, /* The SELECT statement with the ORDER BY clause */
95443 Expr *pE /* The specific ORDER BY term */
95444){
95445 int i; /* Loop counter */
95446 ExprList *pEList; /* The columns of the result set */
95447 NameContext nc; /* Name context for resolving pE */
95448 sqlite3 *db; /* Database connection */
95449 int rc; /* Return code from subprocedures */
95450 u8 savedSuppErr; /* Saved value of db->suppressErr */
95451
95452 assert( sqlite3ExprIsInteger(pE, &i)==0 );
95453 pEList = pSelect->pEList;
95454
95455 /* Resolve all names in the ORDER BY term expression
95456 */
95457 memset(&nc, 0, sizeof(nc));
95458 nc.pParse = pParse;
95459 nc.pSrcList = pSelect->pSrc;
95460 nc.uNC.pEList = pEList;
95461 nc.ncFlags = NC_AllowAgg|NC_UEList;
95462 nc.nErr = 0;
95463 db = pParse->db;
95464 savedSuppErr = db->suppressErr;
95465 db->suppressErr = 1;
95466 rc = sqlite3ResolveExprNames(&nc, pE);
95467 db->suppressErr = savedSuppErr;
95468 if( rc ) return 0;
95469
95470 /* Try to match the ORDER BY expression against an expression
95471 ** in the result set. Return an 1-based index of the matching
95472 ** result-set entry.
95473 */
95474 for(i=0; i<pEList->nExpr; i++){
95475 if( sqlite3ExprCompare(0, pEList->a[i].pExpr, pE, -1)<2 ){
95476 return i+1;
95477 }
95478 }
95479
95480 /* If no match, return 0. */
95481 return 0;
95482}
95483
95484/*
95485** Generate an ORDER BY or GROUP BY term out-of-range error.
95486*/
95487static void resolveOutOfRangeError(
95488 Parse *pParse, /* The error context into which to write the error */
95489 const char *zType, /* "ORDER" or "GROUP" */
95490 int i, /* The index (1-based) of the term out of range */
95491 int mx /* Largest permissible value of i */
95492){
95493 sqlite3ErrorMsg(pParse,
95494 "%r %s BY term out of range - should be "
95495 "between 1 and %d", i, zType, mx);
95496}
95497
95498/*
95499** Analyze the ORDER BY clause in a compound SELECT statement. Modify
95500** each term of the ORDER BY clause is a constant integer between 1
95501** and N where N is the number of columns in the compound SELECT.
95502**
95503** ORDER BY terms that are already an integer between 1 and N are
95504** unmodified. ORDER BY terms that are integers outside the range of
95505** 1 through N generate an error. ORDER BY terms that are expressions
95506** are matched against result set expressions of compound SELECT
95507** beginning with the left-most SELECT and working toward the right.
95508** At the first match, the ORDER BY expression is transformed into
95509** the integer column number.
95510**
95511** Return the number of errors seen.
95512*/
95513static int resolveCompoundOrderBy(
95514 Parse *pParse, /* Parsing context. Leave error messages here */
95515 Select *pSelect /* The SELECT statement containing the ORDER BY */
95516){
95517 int i;
95518 ExprList *pOrderBy;
95519 ExprList *pEList;
95520 sqlite3 *db;
95521 int moreToDo = 1;
95522
95523 pOrderBy = pSelect->pOrderBy;
95524 if( pOrderBy==0 ) return 0;
95525 db = pParse->db;
95526 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
95527 sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
95528 return 1;
95529 }
95530 for(i=0; i<pOrderBy->nExpr; i++){
95531 pOrderBy->a[i].done = 0;
95532 }
95533 pSelect->pNext = 0;
95534 while( pSelect->pPrior ){
95535 pSelect->pPrior->pNext = pSelect;
95536 pSelect = pSelect->pPrior;
95537 }
95538 while( pSelect && moreToDo ){
95539 struct ExprList_item *pItem;
95540 moreToDo = 0;
95541 pEList = pSelect->pEList;
95542 assert( pEList!=0 );
95543 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
95544 int iCol = -1;
95545 Expr *pE, *pDup;
95546 if( pItem->done ) continue;
95547 pE = sqlite3ExprSkipCollate(pItem->pExpr);
95548 if( sqlite3ExprIsInteger(pE, &iCol) ){
95549 if( iCol<=0 || iCol>pEList->nExpr ){
95550 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
95551 return 1;
95552 }
95553 }else{
95554 iCol = resolveAsName(pParse, pEList, pE);
95555 if( iCol==0 ){
95556 pDup = sqlite3ExprDup(db, pE, 0);
95557 if( !db->mallocFailed ){
95558 assert(pDup);
95559 iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
95560 }
95561 sqlite3ExprDelete(db, pDup);
95562 }
95563 }
95564 if( iCol>0 ){
95565 /* Convert the ORDER BY term into an integer column number iCol,
95566 ** taking care to preserve the COLLATE clause if it exists */
95567 Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
95568 if( pNew==0 ) return 1;
95569 pNew->flags |= EP_IntValue;
95570 pNew->u.iValue = iCol;
95571 if( pItem->pExpr==pE ){
95572 pItem->pExpr = pNew;
95573 }else{
95574 Expr *pParent = pItem->pExpr;
95575 assert( pParent->op==TK_COLLATE );
95576 while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
95577 assert( pParent->pLeft==pE );
95578 pParent->pLeft = pNew;
95579 }
95580 sqlite3ExprDelete(db, pE);
95581 pItem->u.x.iOrderByCol = (u16)iCol;
95582 pItem->done = 1;
95583 }else{
95584 moreToDo = 1;
95585 }
95586 }
95587 pSelect = pSelect->pNext;
95588 }
95589 for(i=0; i<pOrderBy->nExpr; i++){
95590 if( pOrderBy->a[i].done==0 ){
95591 sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
95592 "column in the result set", i+1);
95593 return 1;
95594 }
95595 }
95596 return 0;
95597}
95598
95599/*
95600** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
95601** the SELECT statement pSelect. If any term is reference to a
95602** result set expression (as determined by the ExprList.a.u.x.iOrderByCol
95603** field) then convert that term into a copy of the corresponding result set
95604** column.
95605**
95606** If any errors are detected, add an error message to pParse and
95607** return non-zero. Return zero if no errors are seen.
95608*/
95609SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(
95610 Parse *pParse, /* Parsing context. Leave error messages here */
95611 Select *pSelect, /* The SELECT statement containing the clause */
95612 ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */
95613 const char *zType /* "ORDER" or "GROUP" */
95614){
95615 int i;
95616 sqlite3 *db = pParse->db;
95617 ExprList *pEList;
95618 struct ExprList_item *pItem;
95619
95620 if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
95621 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
95622 sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
95623 return 1;
95624 }
95625 pEList = pSelect->pEList;
95626 assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */
95627 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
95628 if( pItem->u.x.iOrderByCol ){
95629 if( pItem->u.x.iOrderByCol>pEList->nExpr ){
95630 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
95631 return 1;
95632 }
95633 resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,
95634 zType,0);
95635 }
95636 }
95637 return 0;
95638}
95639
95640/*
95641** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
95642** The Name context of the SELECT statement is pNC. zType is either
95643** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
95644**
95645** This routine resolves each term of the clause into an expression.
95646** If the order-by term is an integer I between 1 and N (where N is the
95647** number of columns in the result set of the SELECT) then the expression
95648** in the resolution is a copy of the I-th result-set expression. If
95649** the order-by term is an identifier that corresponds to the AS-name of
95650** a result-set expression, then the term resolves to a copy of the
95651** result-set expression. Otherwise, the expression is resolved in
95652** the usual way - using sqlite3ResolveExprNames().
95653**
95654** This routine returns the number of errors. If errors occur, then
95655** an appropriate error message might be left in pParse. (OOM errors
95656** excepted.)
95657*/
95658static int resolveOrderGroupBy(
95659 NameContext *pNC, /* The name context of the SELECT statement */
95660 Select *pSelect, /* The SELECT statement holding pOrderBy */
95661 ExprList *pOrderBy, /* An ORDER BY or GROUP BY clause to resolve */
95662 const char *zType /* Either "ORDER" or "GROUP", as appropriate */
95663){
95664 int i, j; /* Loop counters */
95665 int iCol; /* Column number */
95666 struct ExprList_item *pItem; /* A term of the ORDER BY clause */
95667 Parse *pParse; /* Parsing context */
95668 int nResult; /* Number of terms in the result set */
95669
95670 if( pOrderBy==0 ) return 0;
95671 nResult = pSelect->pEList->nExpr;
95672 pParse = pNC->pParse;
95673 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
95674 Expr *pE = pItem->pExpr;
95675 Expr *pE2 = sqlite3ExprSkipCollate(pE);
95676 if( zType[0]!='G' ){
95677 iCol = resolveAsName(pParse, pSelect->pEList, pE2);
95678 if( iCol>0 ){
95679 /* If an AS-name match is found, mark this ORDER BY column as being
95680 ** a copy of the iCol-th result-set column. The subsequent call to
95681 ** sqlite3ResolveOrderGroupBy() will convert the expression to a
95682 ** copy of the iCol-th result-set expression. */
95683 pItem->u.x.iOrderByCol = (u16)iCol;
95684 continue;
95685 }
95686 }
95687 if( sqlite3ExprIsInteger(pE2, &iCol) ){
95688 /* The ORDER BY term is an integer constant. Again, set the column
95689 ** number so that sqlite3ResolveOrderGroupBy() will convert the
95690 ** order-by term to a copy of the result-set expression */
95691 if( iCol<1 || iCol>0xffff ){
95692 resolveOutOfRangeError(pParse, zType, i+1, nResult);
95693 return 1;
95694 }
95695 pItem->u.x.iOrderByCol = (u16)iCol;
95696 continue;
95697 }
95698
95699 /* Otherwise, treat the ORDER BY term as an ordinary expression */
95700 pItem->u.x.iOrderByCol = 0;
95701 if( sqlite3ResolveExprNames(pNC, pE) ){
95702 return 1;
95703 }
95704 for(j=0; j<pSelect->pEList->nExpr; j++){
95705 if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
95706#ifndef SQLITE_OMIT_WINDOWFUNC
95707 if( ExprHasProperty(pE, EP_WinFunc) ){
95708 /* Since this window function is being changed into a reference
95709 ** to the same window function the result set, remove the instance
95710 ** of this window function from the Select.pWin list. */
95711 Window **pp;
95712 for(pp=&pSelect->pWin; *pp; pp=&(*pp)->pNextWin){
95713 if( *pp==pE->y.pWin ){
95714 *pp = (*pp)->pNextWin;
95715 }
95716 }
95717 }
95718#endif
95719 pItem->u.x.iOrderByCol = j+1;
95720 }
95721 }
95722 }
95723 return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
95724}
95725
95726/*
95727** Resolve names in the SELECT statement p and all of its descendants.
95728*/
95729static int resolveSelectStep(Walker *pWalker, Select *p){
95730 NameContext *pOuterNC; /* Context that contains this SELECT */
95731 NameContext sNC; /* Name context of this SELECT */
95732 int isCompound; /* True if p is a compound select */
95733 int nCompound; /* Number of compound terms processed so far */
95734 Parse *pParse; /* Parsing context */
95735 int i; /* Loop counter */
95736 ExprList *pGroupBy; /* The GROUP BY clause */
95737 Select *pLeftmost; /* Left-most of SELECT of a compound */
95738 sqlite3 *db; /* Database connection */
95739
95740
95741 assert( p!=0 );
95742 if( p->selFlags & SF_Resolved ){
95743 return WRC_Prune;
95744 }
95745 pOuterNC = pWalker->u.pNC;
95746 pParse = pWalker->pParse;
95747 db = pParse->db;
95748
95749 /* Normally sqlite3SelectExpand() will be called first and will have
95750 ** already expanded this SELECT. However, if this is a subquery within
95751 ** an expression, sqlite3ResolveExprNames() will be called without a
95752 ** prior call to sqlite3SelectExpand(). When that happens, let
95753 ** sqlite3SelectPrep() do all of the processing for this SELECT.
95754 ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
95755 ** this routine in the correct order.
95756 */
95757 if( (p->selFlags & SF_Expanded)==0 ){
95758 sqlite3SelectPrep(pParse, p, pOuterNC);
95759 return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
95760 }
95761
95762 isCompound = p->pPrior!=0;
95763 nCompound = 0;
95764 pLeftmost = p;
95765 while( p ){
95766 assert( (p->selFlags & SF_Expanded)!=0 );
95767 assert( (p->selFlags & SF_Resolved)==0 );
95768 p->selFlags |= SF_Resolved;
95769
95770 /* Resolve the expressions in the LIMIT and OFFSET clauses. These
95771 ** are not allowed to refer to any names, so pass an empty NameContext.
95772 */
95773 memset(&sNC, 0, sizeof(sNC));
95774 sNC.pParse = pParse;
95775 sNC.pWinSelect = p;
95776 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){
95777 return WRC_Abort;
95778 }
95779
95780 /* If the SF_Converted flags is set, then this Select object was
95781 ** was created by the convertCompoundSelectToSubquery() function.
95782 ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
95783 ** as if it were part of the sub-query, not the parent. This block
95784 ** moves the pOrderBy down to the sub-query. It will be moved back
95785 ** after the names have been resolved. */
95786 if( p->selFlags & SF_Converted ){
95787 Select *pSub = p->pSrc->a[0].pSelect;
95788 assert( p->pSrc->nSrc==1 && p->pOrderBy );
95789 assert( pSub->pPrior && pSub->pOrderBy==0 );
95790 pSub->pOrderBy = p->pOrderBy;
95791 p->pOrderBy = 0;
95792 }
95793
95794 /* Recursively resolve names in all subqueries
95795 */
95796 for(i=0; i<p->pSrc->nSrc; i++){
95797 struct SrcList_item *pItem = &p->pSrc->a[i];
95798 if( pItem->pSelect ){
95799 NameContext *pNC; /* Used to iterate name contexts */
95800 int nRef = 0; /* Refcount for pOuterNC and outer contexts */
95801 const char *zSavedContext = pParse->zAuthContext;
95802
95803 /* Count the total number of references to pOuterNC and all of its
95804 ** parent contexts. After resolving references to expressions in
95805 ** pItem->pSelect, check if this value has changed. If so, then
95806 ** SELECT statement pItem->pSelect must be correlated. Set the
95807 ** pItem->fg.isCorrelated flag if this is the case. */
95808 for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef;
95809
95810 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
95811 sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
95812 pParse->zAuthContext = zSavedContext;
95813 if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
95814
95815 for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
95816 assert( pItem->fg.isCorrelated==0 && nRef<=0 );
95817 pItem->fg.isCorrelated = (nRef!=0);
95818 }
95819 }
95820
95821 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
95822 ** resolve the result-set expression list.
95823 */
95824 sNC.ncFlags = NC_AllowAgg|NC_AllowWin;
95825 sNC.pSrcList = p->pSrc;
95826 sNC.pNext = pOuterNC;
95827
95828 /* Resolve names in the result set. */
95829 if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort;
95830 sNC.ncFlags &= ~NC_AllowWin;
95831
95832 /* If there are no aggregate functions in the result-set, and no GROUP BY
95833 ** expression, do not allow aggregates in any of the other expressions.
95834 */
95835 assert( (p->selFlags & SF_Aggregate)==0 );
95836 pGroupBy = p->pGroupBy;
95837 if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){
95838 assert( NC_MinMaxAgg==SF_MinMaxAgg );
95839 p->selFlags |= SF_Aggregate | (sNC.ncFlags&NC_MinMaxAgg);
95840 }else{
95841 sNC.ncFlags &= ~NC_AllowAgg;
95842 }
95843
95844 /* If a HAVING clause is present, then there must be a GROUP BY clause.
95845 */
95846 if( p->pHaving && !pGroupBy ){
95847 sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
95848 return WRC_Abort;
95849 }
95850
95851 /* Add the output column list to the name-context before parsing the
95852 ** other expressions in the SELECT statement. This is so that
95853 ** expressions in the WHERE clause (etc.) can refer to expressions by
95854 ** aliases in the result set.
95855 **
95856 ** Minor point: If this is the case, then the expression will be
95857 ** re-evaluated for each reference to it.
95858 */
95859 assert( (sNC.ncFlags & (NC_UAggInfo|NC_UUpsert))==0 );
95860 sNC.uNC.pEList = p->pEList;
95861 sNC.ncFlags |= NC_UEList;
95862 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
95863 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
95864
95865 /* Resolve names in table-valued-function arguments */
95866 for(i=0; i<p->pSrc->nSrc; i++){
95867 struct SrcList_item *pItem = &p->pSrc->a[i];
95868 if( pItem->fg.isTabFunc
95869 && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
95870 ){
95871 return WRC_Abort;
95872 }
95873 }
95874
95875 /* The ORDER BY and GROUP BY clauses may not refer to terms in
95876 ** outer queries
95877 */
95878 sNC.pNext = 0;
95879 sNC.ncFlags |= NC_AllowAgg|NC_AllowWin;
95880
95881 /* If this is a converted compound query, move the ORDER BY clause from
95882 ** the sub-query back to the parent query. At this point each term
95883 ** within the ORDER BY clause has been transformed to an integer value.
95884 ** These integers will be replaced by copies of the corresponding result
95885 ** set expressions by the call to resolveOrderGroupBy() below. */
95886 if( p->selFlags & SF_Converted ){
95887 Select *pSub = p->pSrc->a[0].pSelect;
95888 p->pOrderBy = pSub->pOrderBy;
95889 pSub->pOrderBy = 0;
95890 }
95891
95892 /* Process the ORDER BY clause for singleton SELECT statements.
95893 ** The ORDER BY clause for compounds SELECT statements is handled
95894 ** below, after all of the result-sets for all of the elements of
95895 ** the compound have been resolved.
95896 **
95897 ** If there is an ORDER BY clause on a term of a compound-select other
95898 ** than the right-most term, then that is a syntax error. But the error
95899 ** is not detected until much later, and so we need to go ahead and
95900 ** resolve those symbols on the incorrect ORDER BY for consistency.
95901 */
95902 if( isCompound<=nCompound /* Defer right-most ORDER BY of a compound */
95903 && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
95904 ){
95905 return WRC_Abort;
95906 }
95907 if( db->mallocFailed ){
95908 return WRC_Abort;
95909 }
95910 sNC.ncFlags &= ~NC_AllowWin;
95911
95912 /* Resolve the GROUP BY clause. At the same time, make sure
95913 ** the GROUP BY clause does not contain aggregate functions.
95914 */
95915 if( pGroupBy ){
95916 struct ExprList_item *pItem;
95917
95918 if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
95919 return WRC_Abort;
95920 }
95921 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
95922 if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
95923 sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
95924 "the GROUP BY clause");
95925 return WRC_Abort;
95926 }
95927 }
95928 }
95929
95930 /* If this is part of a compound SELECT, check that it has the right
95931 ** number of expressions in the select list. */
95932 if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
95933 sqlite3SelectWrongNumTermsError(pParse, p->pNext);
95934 return WRC_Abort;
95935 }
95936
95937 /* Advance to the next term of the compound
95938 */
95939 p = p->pPrior;
95940 nCompound++;
95941 }
95942
95943 /* Resolve the ORDER BY on a compound SELECT after all terms of
95944 ** the compound have been resolved.
95945 */
95946 if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
95947 return WRC_Abort;
95948 }
95949
95950 return WRC_Prune;
95951}
95952
95953/*
95954** This routine walks an expression tree and resolves references to
95955** table columns and result-set columns. At the same time, do error
95956** checking on function usage and set a flag if any aggregate functions
95957** are seen.
95958**
95959** To resolve table columns references we look for nodes (or subtrees) of the
95960** form X.Y.Z or Y.Z or just Z where
95961**
95962** X: The name of a database. Ex: "main" or "temp" or
95963** the symbolic name assigned to an ATTACH-ed database.
95964**
95965** Y: The name of a table in a FROM clause. Or in a trigger
95966** one of the special names "old" or "new".
95967**
95968** Z: The name of a column in table Y.
95969**
95970** The node at the root of the subtree is modified as follows:
95971**
95972** Expr.op Changed to TK_COLUMN
95973** Expr.pTab Points to the Table object for X.Y
95974** Expr.iColumn The column index in X.Y. -1 for the rowid.
95975** Expr.iTable The VDBE cursor number for X.Y
95976**
95977**
95978** To resolve result-set references, look for expression nodes of the
95979** form Z (with no X and Y prefix) where the Z matches the right-hand
95980** size of an AS clause in the result-set of a SELECT. The Z expression
95981** is replaced by a copy of the left-hand side of the result-set expression.
95982** Table-name and function resolution occurs on the substituted expression
95983** tree. For example, in:
95984**
95985** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
95986**
95987** The "x" term of the order by is replaced by "a+b" to render:
95988**
95989** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
95990**
95991** Function calls are checked to make sure that the function is
95992** defined and that the correct number of arguments are specified.
95993** If the function is an aggregate function, then the NC_HasAgg flag is
95994** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
95995** If an expression contains aggregate functions then the EP_Agg
95996** property on the expression is set.
95997**
95998** An error message is left in pParse if anything is amiss. The number
95999** if errors is returned.
96000*/
96001SQLITE_PRIVATE int sqlite3ResolveExprNames(
96002 NameContext *pNC, /* Namespace to resolve expressions in. */
96003 Expr *pExpr /* The expression to be analyzed. */
96004){
96005 u16 savedHasAgg;
96006 Walker w;
96007
96008 if( pExpr==0 ) return SQLITE_OK;
96009 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg);
96010 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg);
96011 w.pParse = pNC->pParse;
96012 w.xExprCallback = resolveExprStep;
96013 w.xSelectCallback = resolveSelectStep;
96014 w.xSelectCallback2 = 0;
96015 w.u.pNC = pNC;
96016#if SQLITE_MAX_EXPR_DEPTH>0
96017 w.pParse->nHeight += pExpr->nHeight;
96018 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
96019 return SQLITE_ERROR;
96020 }
96021#endif
96022 sqlite3WalkExpr(&w, pExpr);
96023#if SQLITE_MAX_EXPR_DEPTH>0
96024 w.pParse->nHeight -= pExpr->nHeight;
96025#endif
96026 if( pNC->ncFlags & NC_HasAgg ){
96027 ExprSetProperty(pExpr, EP_Agg);
96028 }
96029 pNC->ncFlags |= savedHasAgg;
96030 return pNC->nErr>0 || w.pParse->nErr>0;
96031}
96032
96033/*
96034** Resolve all names for all expression in an expression list. This is
96035** just like sqlite3ResolveExprNames() except that it works for an expression
96036** list rather than a single expression.
96037*/
96038SQLITE_PRIVATE int sqlite3ResolveExprListNames(
96039 NameContext *pNC, /* Namespace to resolve expressions in. */
96040 ExprList *pList /* The expression list to be analyzed. */
96041){
96042 int i;
96043 if( pList ){
96044 for(i=0; i<pList->nExpr; i++){
96045 if( sqlite3ResolveExprNames(pNC, pList->a[i].pExpr) ) return WRC_Abort;
96046 }
96047 }
96048 return WRC_Continue;
96049}
96050
96051/*
96052** Resolve all names in all expressions of a SELECT and in all
96053** decendents of the SELECT, including compounds off of p->pPrior,
96054** subqueries in expressions, and subqueries used as FROM clause
96055** terms.
96056**
96057** See sqlite3ResolveExprNames() for a description of the kinds of
96058** transformations that occur.
96059**
96060** All SELECT statements should have been expanded using
96061** sqlite3SelectExpand() prior to invoking this routine.
96062*/
96063SQLITE_PRIVATE void sqlite3ResolveSelectNames(
96064 Parse *pParse, /* The parser context */
96065 Select *p, /* The SELECT statement being coded. */
96066 NameContext *pOuterNC /* Name context for parent SELECT statement */
96067){
96068 Walker w;
96069
96070 assert( p!=0 );
96071 w.xExprCallback = resolveExprStep;
96072 w.xSelectCallback = resolveSelectStep;
96073 w.xSelectCallback2 = 0;
96074 w.pParse = pParse;
96075 w.u.pNC = pOuterNC;
96076 sqlite3WalkSelect(&w, p);
96077}
96078
96079/*
96080** Resolve names in expressions that can only reference a single table:
96081**
96082** * CHECK constraints
96083** * WHERE clauses on partial indices
96084**
96085** The Expr.iTable value for Expr.op==TK_COLUMN nodes of the expression
96086** is set to -1 and the Expr.iColumn value is set to the column number.
96087**
96088** Any errors cause an error message to be set in pParse.
96089*/
96090SQLITE_PRIVATE void sqlite3ResolveSelfReference(
96091 Parse *pParse, /* Parsing context */
96092 Table *pTab, /* The table being referenced */
96093 int type, /* NC_IsCheck or NC_PartIdx or NC_IdxExpr */
96094 Expr *pExpr, /* Expression to resolve. May be NULL. */
96095 ExprList *pList /* Expression list to resolve. May be NULL. */
96096){
96097 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
96098 NameContext sNC; /* Name context for pParse->pNewTable */
96099
96100 assert( type==NC_IsCheck || type==NC_PartIdx || type==NC_IdxExpr );
96101 memset(&sNC, 0, sizeof(sNC));
96102 memset(&sSrc, 0, sizeof(sSrc));
96103 sSrc.nSrc = 1;
96104 sSrc.a[0].zName = pTab->zName;
96105 sSrc.a[0].pTab = pTab;
96106 sSrc.a[0].iCursor = -1;
96107 sNC.pParse = pParse;
96108 sNC.pSrcList = &sSrc;
96109 sNC.ncFlags = type;
96110 if( sqlite3ResolveExprNames(&sNC, pExpr) ) return;
96111 if( pList ) sqlite3ResolveExprListNames(&sNC, pList);
96112}
96113
96114/************** End of resolve.c *********************************************/
96115/************** Begin file expr.c ********************************************/
96116/*
96117** 2001 September 15
96118**
96119** The author disclaims copyright to this source code. In place of
96120** a legal notice, here is a blessing:
96121**
96122** May you do good and not evil.
96123** May you find forgiveness for yourself and forgive others.
96124** May you share freely, never taking more than you give.
96125**
96126*************************************************************************
96127** This file contains routines used for analyzing expressions and
96128** for generating VDBE code that evaluates expressions in SQLite.
96129*/
96130/* #include "sqliteInt.h" */
96131
96132/* Forward declarations */
96133static void exprCodeBetween(Parse*,Expr*,int,void(*)(Parse*,Expr*,int,int),int);
96134static int exprCodeVector(Parse *pParse, Expr *p, int *piToFree);
96135
96136/*
96137** Return the affinity character for a single column of a table.
96138*/
96139SQLITE_PRIVATE char sqlite3TableColumnAffinity(Table *pTab, int iCol){
96140 assert( iCol<pTab->nCol );
96141 return iCol>=0 ? pTab->aCol[iCol].affinity : SQLITE_AFF_INTEGER;
96142}
96143
96144/*
96145** Return the 'affinity' of the expression pExpr if any.
96146**
96147** If pExpr is a column, a reference to a column via an 'AS' alias,
96148** or a sub-select with a column as the return value, then the
96149** affinity of that column is returned. Otherwise, 0x00 is returned,
96150** indicating no affinity for the expression.
96151**
96152** i.e. the WHERE clause expressions in the following statements all
96153** have an affinity:
96154**
96155** CREATE TABLE t1(a);
96156** SELECT * FROM t1 WHERE a;
96157** SELECT a AS b FROM t1 WHERE b;
96158** SELECT * FROM t1 WHERE (select a from t1);
96159*/
96160SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){
96161 int op;
96162 pExpr = sqlite3ExprSkipCollate(pExpr);
96163 if( pExpr->flags & EP_Generic ) return 0;
96164 op = pExpr->op;
96165 if( op==TK_SELECT ){
96166 assert( pExpr->flags&EP_xIsSelect );
96167 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
96168 }
96169 if( op==TK_REGISTER ) op = pExpr->op2;
96170#ifndef SQLITE_OMIT_CAST
96171 if( op==TK_CAST ){
96172 assert( !ExprHasProperty(pExpr, EP_IntValue) );
96173 return sqlite3AffinityType(pExpr->u.zToken, 0);
96174 }
96175#endif
96176 if( (op==TK_AGG_COLUMN || op==TK_COLUMN) && pExpr->y.pTab ){
96177 return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
96178 }
96179 if( op==TK_SELECT_COLUMN ){
96180 assert( pExpr->pLeft->flags&EP_xIsSelect );
96181 return sqlite3ExprAffinity(
96182 pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
96183 );
96184 }
96185 return pExpr->affinity;
96186}
96187
96188/*
96189** Set the collating sequence for expression pExpr to be the collating
96190** sequence named by pToken. Return a pointer to a new Expr node that
96191** implements the COLLATE operator.
96192**
96193** If a memory allocation error occurs, that fact is recorded in pParse->db
96194** and the pExpr parameter is returned unchanged.
96195*/
96196SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(
96197 Parse *pParse, /* Parsing context */
96198 Expr *pExpr, /* Add the "COLLATE" clause to this expression */
96199 const Token *pCollName, /* Name of collating sequence */
96200 int dequote /* True to dequote pCollName */
96201){
96202 if( pCollName->n>0 ){
96203 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
96204 if( pNew ){
96205 pNew->pLeft = pExpr;
96206 pNew->flags |= EP_Collate|EP_Skip;
96207 pExpr = pNew;
96208 }
96209 }
96210 return pExpr;
96211}
96212SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
96213 Token s;
96214 assert( zC!=0 );
96215 sqlite3TokenInit(&s, (char*)zC);
96216 return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0);
96217}
96218
96219/*
96220** Skip over any TK_COLLATE operators and any unlikely()
96221** or likelihood() function at the root of an expression.
96222*/
96223SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr *pExpr){
96224 while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){
96225 if( ExprHasProperty(pExpr, EP_Unlikely) ){
96226 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
96227 assert( pExpr->x.pList->nExpr>0 );
96228 assert( pExpr->op==TK_FUNCTION );
96229 pExpr = pExpr->x.pList->a[0].pExpr;
96230 }else{
96231 assert( pExpr->op==TK_COLLATE );
96232 pExpr = pExpr->pLeft;
96233 }
96234 }
96235 return pExpr;
96236}
96237
96238/*
96239** Return the collation sequence for the expression pExpr. If
96240** there is no defined collating sequence, return NULL.
96241**
96242** See also: sqlite3ExprNNCollSeq()
96243**
96244** The sqlite3ExprNNCollSeq() works the same exact that it returns the
96245** default collation if pExpr has no defined collation.
96246**
96247** The collating sequence might be determined by a COLLATE operator
96248** or by the presence of a column with a defined collating sequence.
96249** COLLATE operators take first precedence. Left operands take
96250** precedence over right operands.
96251*/
96252SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
96253 sqlite3 *db = pParse->db;
96254 CollSeq *pColl = 0;
96255 Expr *p = pExpr;
96256 while( p ){
96257 int op = p->op;
96258 if( p->flags & EP_Generic ) break;
96259 if( (op==TK_AGG_COLUMN || op==TK_COLUMN
96260 || op==TK_REGISTER || op==TK_TRIGGER)
96261 && p->y.pTab!=0
96262 ){
96263 /* op==TK_REGISTER && p->y.pTab!=0 happens when pExpr was originally
96264 ** a TK_COLUMN but was previously evaluated and cached in a register */
96265 int j = p->iColumn;
96266 if( j>=0 ){
96267 const char *zColl = p->y.pTab->aCol[j].zColl;
96268 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
96269 }
96270 break;
96271 }
96272 if( op==TK_CAST || op==TK_UPLUS ){
96273 p = p->pLeft;
96274 continue;
96275 }
96276 if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){
96277 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
96278 break;
96279 }
96280 if( p->flags & EP_Collate ){
96281 if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
96282 p = p->pLeft;
96283 }else{
96284 Expr *pNext = p->pRight;
96285 /* The Expr.x union is never used at the same time as Expr.pRight */
96286 assert( p->x.pList==0 || p->pRight==0 );
96287 /* p->flags holds EP_Collate and p->pLeft->flags does not. And
96288 ** p->x.pSelect cannot. So if p->x.pLeft exists, it must hold at
96289 ** least one EP_Collate. Thus the following two ALWAYS. */
96290 if( p->x.pList!=0 && ALWAYS(!ExprHasProperty(p, EP_xIsSelect)) ){
96291 int i;
96292 for(i=0; ALWAYS(i<p->x.pList->nExpr); i++){
96293 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){
96294 pNext = p->x.pList->a[i].pExpr;
96295 break;
96296 }
96297 }
96298 }
96299 p = pNext;
96300 }
96301 }else{
96302 break;
96303 }
96304 }
96305 if( sqlite3CheckCollSeq(pParse, pColl) ){
96306 pColl = 0;
96307 }
96308 return pColl;
96309}
96310
96311/*
96312** Return the collation sequence for the expression pExpr. If
96313** there is no defined collating sequence, return a pointer to the
96314** defautl collation sequence.
96315**
96316** See also: sqlite3ExprCollSeq()
96317**
96318** The sqlite3ExprCollSeq() routine works the same except that it
96319** returns NULL if there is no defined collation.
96320*/
96321SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr){
96322 CollSeq *p = sqlite3ExprCollSeq(pParse, pExpr);
96323 if( p==0 ) p = pParse->db->pDfltColl;
96324 assert( p!=0 );
96325 return p;
96326}
96327
96328/*
96329** Return TRUE if the two expressions have equivalent collating sequences.
96330*/
96331SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse *pParse, Expr *pE1, Expr *pE2){
96332 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pE1);
96333 CollSeq *pColl2 = sqlite3ExprNNCollSeq(pParse, pE2);
96334 return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0;
96335}
96336
96337/*
96338** pExpr is an operand of a comparison operator. aff2 is the
96339** type affinity of the other operand. This routine returns the
96340** type affinity that should be used for the comparison operator.
96341*/
96342SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2){
96343 char aff1 = sqlite3ExprAffinity(pExpr);
96344 if( aff1 && aff2 ){
96345 /* Both sides of the comparison are columns. If one has numeric
96346 ** affinity, use that. Otherwise use no affinity.
96347 */
96348 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
96349 return SQLITE_AFF_NUMERIC;
96350 }else{
96351 return SQLITE_AFF_BLOB;
96352 }
96353 }else if( !aff1 && !aff2 ){
96354 /* Neither side of the comparison is a column. Compare the
96355 ** results directly.
96356 */
96357 return SQLITE_AFF_BLOB;
96358 }else{
96359 /* One side is a column, the other is not. Use the columns affinity. */
96360 assert( aff1==0 || aff2==0 );
96361 return (aff1 + aff2);
96362 }
96363}
96364
96365/*
96366** pExpr is a comparison operator. Return the type affinity that should
96367** be applied to both operands prior to doing the comparison.
96368*/
96369static char comparisonAffinity(Expr *pExpr){
96370 char aff;
96371 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
96372 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
96373 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
96374 assert( pExpr->pLeft );
96375 aff = sqlite3ExprAffinity(pExpr->pLeft);
96376 if( pExpr->pRight ){
96377 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
96378 }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
96379 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
96380 }else if( aff==0 ){
96381 aff = SQLITE_AFF_BLOB;
96382 }
96383 return aff;
96384}
96385
96386/*
96387** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
96388** idx_affinity is the affinity of an indexed column. Return true
96389** if the index with affinity idx_affinity may be used to implement
96390** the comparison in pExpr.
96391*/
96392SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
96393 char aff = comparisonAffinity(pExpr);
96394 switch( aff ){
96395 case SQLITE_AFF_BLOB:
96396 return 1;
96397 case SQLITE_AFF_TEXT:
96398 return idx_affinity==SQLITE_AFF_TEXT;
96399 default:
96400 return sqlite3IsNumericAffinity(idx_affinity);
96401 }
96402}
96403
96404/*
96405** Return the P5 value that should be used for a binary comparison
96406** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
96407*/
96408static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
96409 u8 aff = (char)sqlite3ExprAffinity(pExpr2);
96410 aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
96411 return aff;
96412}
96413
96414/*
96415** Return a pointer to the collation sequence that should be used by
96416** a binary comparison operator comparing pLeft and pRight.
96417**
96418** If the left hand expression has a collating sequence type, then it is
96419** used. Otherwise the collation sequence for the right hand expression
96420** is used, or the default (BINARY) if neither expression has a collating
96421** type.
96422**
96423** Argument pRight (but not pLeft) may be a null pointer. In this case,
96424** it is not considered.
96425*/
96426SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(
96427 Parse *pParse,
96428 Expr *pLeft,
96429 Expr *pRight
96430){
96431 CollSeq *pColl;
96432 assert( pLeft );
96433 if( pLeft->flags & EP_Collate ){
96434 pColl = sqlite3ExprCollSeq(pParse, pLeft);
96435 }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
96436 pColl = sqlite3ExprCollSeq(pParse, pRight);
96437 }else{
96438 pColl = sqlite3ExprCollSeq(pParse, pLeft);
96439 if( !pColl ){
96440 pColl = sqlite3ExprCollSeq(pParse, pRight);
96441 }
96442 }
96443 return pColl;
96444}
96445
96446/*
96447** Generate code for a comparison operator.
96448*/
96449static int codeCompare(
96450 Parse *pParse, /* The parsing (and code generating) context */
96451 Expr *pLeft, /* The left operand */
96452 Expr *pRight, /* The right operand */
96453 int opcode, /* The comparison opcode */
96454 int in1, int in2, /* Register holding operands */
96455 int dest, /* Jump here if true. */
96456 int jumpIfNull /* If true, jump if either operand is NULL */
96457){
96458 int p5;
96459 int addr;
96460 CollSeq *p4;
96461
96462 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
96463 p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
96464 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
96465 (void*)p4, P4_COLLSEQ);
96466 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
96467 return addr;
96468}
96469
96470/*
96471** Return true if expression pExpr is a vector, or false otherwise.
96472**
96473** A vector is defined as any expression that results in two or more
96474** columns of result. Every TK_VECTOR node is an vector because the
96475** parser will not generate a TK_VECTOR with fewer than two entries.
96476** But a TK_SELECT might be either a vector or a scalar. It is only
96477** considered a vector if it has two or more result columns.
96478*/
96479SQLITE_PRIVATE int sqlite3ExprIsVector(Expr *pExpr){
96480 return sqlite3ExprVectorSize(pExpr)>1;
96481}
96482
96483/*
96484** If the expression passed as the only argument is of type TK_VECTOR
96485** return the number of expressions in the vector. Or, if the expression
96486** is a sub-select, return the number of columns in the sub-select. For
96487** any other type of expression, return 1.
96488*/
96489SQLITE_PRIVATE int sqlite3ExprVectorSize(Expr *pExpr){
96490 u8 op = pExpr->op;
96491 if( op==TK_REGISTER ) op = pExpr->op2;
96492 if( op==TK_VECTOR ){
96493 return pExpr->x.pList->nExpr;
96494 }else if( op==TK_SELECT ){
96495 return pExpr->x.pSelect->pEList->nExpr;
96496 }else{
96497 return 1;
96498 }
96499}
96500
96501/*
96502** Return a pointer to a subexpression of pVector that is the i-th
96503** column of the vector (numbered starting with 0). The caller must
96504** ensure that i is within range.
96505**
96506** If pVector is really a scalar (and "scalar" here includes subqueries
96507** that return a single column!) then return pVector unmodified.
96508**
96509** pVector retains ownership of the returned subexpression.
96510**
96511** If the vector is a (SELECT ...) then the expression returned is
96512** just the expression for the i-th term of the result set, and may
96513** not be ready for evaluation because the table cursor has not yet
96514** been positioned.
96515*/
96516SQLITE_PRIVATE Expr *sqlite3VectorFieldSubexpr(Expr *pVector, int i){
96517 assert( i<sqlite3ExprVectorSize(pVector) );
96518 if( sqlite3ExprIsVector(pVector) ){
96519 assert( pVector->op2==0 || pVector->op==TK_REGISTER );
96520 if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){
96521 return pVector->x.pSelect->pEList->a[i].pExpr;
96522 }else{
96523 return pVector->x.pList->a[i].pExpr;
96524 }
96525 }
96526 return pVector;
96527}
96528
96529/*
96530** Compute and return a new Expr object which when passed to
96531** sqlite3ExprCode() will generate all necessary code to compute
96532** the iField-th column of the vector expression pVector.
96533**
96534** It is ok for pVector to be a scalar (as long as iField==0).
96535** In that case, this routine works like sqlite3ExprDup().
96536**
96537** The caller owns the returned Expr object and is responsible for
96538** ensuring that the returned value eventually gets freed.
96539**
96540** The caller retains ownership of pVector. If pVector is a TK_SELECT,
96541** then the returned object will reference pVector and so pVector must remain
96542** valid for the life of the returned object. If pVector is a TK_VECTOR
96543** or a scalar expression, then it can be deleted as soon as this routine
96544** returns.
96545**
96546** A trick to cause a TK_SELECT pVector to be deleted together with
96547** the returned Expr object is to attach the pVector to the pRight field
96548** of the returned TK_SELECT_COLUMN Expr object.
96549*/
96550SQLITE_PRIVATE Expr *sqlite3ExprForVectorField(
96551 Parse *pParse, /* Parsing context */
96552 Expr *pVector, /* The vector. List of expressions or a sub-SELECT */
96553 int iField /* Which column of the vector to return */
96554){
96555 Expr *pRet;
96556 if( pVector->op==TK_SELECT ){
96557 assert( pVector->flags & EP_xIsSelect );
96558 /* The TK_SELECT_COLUMN Expr node:
96559 **
96560 ** pLeft: pVector containing TK_SELECT. Not deleted.
96561 ** pRight: not used. But recursively deleted.
96562 ** iColumn: Index of a column in pVector
96563 ** iTable: 0 or the number of columns on the LHS of an assignment
96564 ** pLeft->iTable: First in an array of register holding result, or 0
96565 ** if the result is not yet computed.
96566 **
96567 ** sqlite3ExprDelete() specifically skips the recursive delete of
96568 ** pLeft on TK_SELECT_COLUMN nodes. But pRight is followed, so pVector
96569 ** can be attached to pRight to cause this node to take ownership of
96570 ** pVector. Typically there will be multiple TK_SELECT_COLUMN nodes
96571 ** with the same pLeft pointer to the pVector, but only one of them
96572 ** will own the pVector.
96573 */
96574 pRet = sqlite3PExpr(pParse, TK_SELECT_COLUMN, 0, 0);
96575 if( pRet ){
96576 pRet->iColumn = iField;
96577 pRet->pLeft = pVector;
96578 }
96579 assert( pRet==0 || pRet->iTable==0 );
96580 }else{
96581 if( pVector->op==TK_VECTOR ) pVector = pVector->x.pList->a[iField].pExpr;
96582 pRet = sqlite3ExprDup(pParse->db, pVector, 0);
96583 }
96584 return pRet;
96585}
96586
96587/*
96588** If expression pExpr is of type TK_SELECT, generate code to evaluate
96589** it. Return the register in which the result is stored (or, if the
96590** sub-select returns more than one column, the first in an array
96591** of registers in which the result is stored).
96592**
96593** If pExpr is not a TK_SELECT expression, return 0.
96594*/
96595static int exprCodeSubselect(Parse *pParse, Expr *pExpr){
96596 int reg = 0;
96597#ifndef SQLITE_OMIT_SUBQUERY
96598 if( pExpr->op==TK_SELECT ){
96599 reg = sqlite3CodeSubselect(pParse, pExpr, 0, 0);
96600 }
96601#endif
96602 return reg;
96603}
96604
96605/*
96606** Argument pVector points to a vector expression - either a TK_VECTOR
96607** or TK_SELECT that returns more than one column. This function returns
96608** the register number of a register that contains the value of
96609** element iField of the vector.
96610**
96611** If pVector is a TK_SELECT expression, then code for it must have
96612** already been generated using the exprCodeSubselect() routine. In this
96613** case parameter regSelect should be the first in an array of registers
96614** containing the results of the sub-select.
96615**
96616** If pVector is of type TK_VECTOR, then code for the requested field
96617** is generated. In this case (*pRegFree) may be set to the number of
96618** a temporary register to be freed by the caller before returning.
96619**
96620** Before returning, output parameter (*ppExpr) is set to point to the
96621** Expr object corresponding to element iElem of the vector.
96622*/
96623static int exprVectorRegister(
96624 Parse *pParse, /* Parse context */
96625 Expr *pVector, /* Vector to extract element from */
96626 int iField, /* Field to extract from pVector */
96627 int regSelect, /* First in array of registers */
96628 Expr **ppExpr, /* OUT: Expression element */
96629 int *pRegFree /* OUT: Temp register to free */
96630){
96631 u8 op = pVector->op;
96632 assert( op==TK_VECTOR || op==TK_REGISTER || op==TK_SELECT );
96633 if( op==TK_REGISTER ){
96634 *ppExpr = sqlite3VectorFieldSubexpr(pVector, iField);
96635 return pVector->iTable+iField;
96636 }
96637 if( op==TK_SELECT ){
96638 *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
96639 return regSelect+iField;
96640 }
96641 *ppExpr = pVector->x.pList->a[iField].pExpr;
96642 return sqlite3ExprCodeTemp(pParse, *ppExpr, pRegFree);
96643}
96644
96645/*
96646** Expression pExpr is a comparison between two vector values. Compute
96647** the result of the comparison (1, 0, or NULL) and write that
96648** result into register dest.
96649**
96650** The caller must satisfy the following preconditions:
96651**
96652** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ
96653** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ
96654** otherwise: op==pExpr->op and p5==0
96655*/
96656static void codeVectorCompare(
96657 Parse *pParse, /* Code generator context */
96658 Expr *pExpr, /* The comparison operation */
96659 int dest, /* Write results into this register */
96660 u8 op, /* Comparison operator */
96661 u8 p5 /* SQLITE_NULLEQ or zero */
96662){
96663 Vdbe *v = pParse->pVdbe;
96664 Expr *pLeft = pExpr->pLeft;
96665 Expr *pRight = pExpr->pRight;
96666 int nLeft = sqlite3ExprVectorSize(pLeft);
96667 int i;
96668 int regLeft = 0;
96669 int regRight = 0;
96670 u8 opx = op;
96671 int addrDone = sqlite3VdbeMakeLabel(v);
96672
96673 if( nLeft!=sqlite3ExprVectorSize(pRight) ){
96674 sqlite3ErrorMsg(pParse, "row value misused");
96675 return;
96676 }
96677 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE
96678 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT
96679 || pExpr->op==TK_LT || pExpr->op==TK_GT
96680 || pExpr->op==TK_LE || pExpr->op==TK_GE
96681 );
96682 assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)
96683 || (pExpr->op==TK_ISNOT && op==TK_NE) );
96684 assert( p5==0 || pExpr->op!=op );
96685 assert( p5==SQLITE_NULLEQ || pExpr->op==op );
96686
96687 p5 |= SQLITE_STOREP2;
96688 if( opx==TK_LE ) opx = TK_LT;
96689 if( opx==TK_GE ) opx = TK_GT;
96690
96691 regLeft = exprCodeSubselect(pParse, pLeft);
96692 regRight = exprCodeSubselect(pParse, pRight);
96693
96694 for(i=0; 1 /*Loop exits by "break"*/; i++){
96695 int regFree1 = 0, regFree2 = 0;
96696 Expr *pL, *pR;
96697 int r1, r2;
96698 assert( i>=0 && i<nLeft );
96699 r1 = exprVectorRegister(pParse, pLeft, i, regLeft, &pL, &regFree1);
96700 r2 = exprVectorRegister(pParse, pRight, i, regRight, &pR, &regFree2);
96701 codeCompare(pParse, pL, pR, opx, r1, r2, dest, p5);
96702 testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
96703 testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
96704 testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
96705 testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
96706 testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
96707 testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
96708 sqlite3ReleaseTempReg(pParse, regFree1);
96709 sqlite3ReleaseTempReg(pParse, regFree2);
96710 if( i==nLeft-1 ){
96711 break;
96712 }
96713 if( opx==TK_EQ ){
96714 sqlite3VdbeAddOp2(v, OP_IfNot, dest, addrDone); VdbeCoverage(v);
96715 p5 |= SQLITE_KEEPNULL;
96716 }else if( opx==TK_NE ){
96717 sqlite3VdbeAddOp2(v, OP_If, dest, addrDone); VdbeCoverage(v);
96718 p5 |= SQLITE_KEEPNULL;
96719 }else{
96720 assert( op==TK_LT || op==TK_GT || op==TK_LE || op==TK_GE );
96721 sqlite3VdbeAddOp2(v, OP_ElseNotEq, 0, addrDone);
96722 VdbeCoverageIf(v, op==TK_LT);
96723 VdbeCoverageIf(v, op==TK_GT);
96724 VdbeCoverageIf(v, op==TK_LE);
96725 VdbeCoverageIf(v, op==TK_GE);
96726 if( i==nLeft-2 ) opx = op;
96727 }
96728 }
96729 sqlite3VdbeResolveLabel(v, addrDone);
96730}
96731
96732#if SQLITE_MAX_EXPR_DEPTH>0
96733/*
96734** Check that argument nHeight is less than or equal to the maximum
96735** expression depth allowed. If it is not, leave an error message in
96736** pParse.
96737*/
96738SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
96739 int rc = SQLITE_OK;
96740 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
96741 if( nHeight>mxHeight ){
96742 sqlite3ErrorMsg(pParse,
96743 "Expression tree is too large (maximum depth %d)", mxHeight
96744 );
96745 rc = SQLITE_ERROR;
96746 }
96747 return rc;
96748}
96749
96750/* The following three functions, heightOfExpr(), heightOfExprList()
96751** and heightOfSelect(), are used to determine the maximum height
96752** of any expression tree referenced by the structure passed as the
96753** first argument.
96754**
96755** If this maximum height is greater than the current value pointed
96756** to by pnHeight, the second parameter, then set *pnHeight to that
96757** value.
96758*/
96759static void heightOfExpr(Expr *p, int *pnHeight){
96760 if( p ){
96761 if( p->nHeight>*pnHeight ){
96762 *pnHeight = p->nHeight;
96763 }
96764 }
96765}
96766static void heightOfExprList(ExprList *p, int *pnHeight){
96767 if( p ){
96768 int i;
96769 for(i=0; i<p->nExpr; i++){
96770 heightOfExpr(p->a[i].pExpr, pnHeight);
96771 }
96772 }
96773}
96774static void heightOfSelect(Select *pSelect, int *pnHeight){
96775 Select *p;
96776 for(p=pSelect; p; p=p->pPrior){
96777 heightOfExpr(p->pWhere, pnHeight);
96778 heightOfExpr(p->pHaving, pnHeight);
96779 heightOfExpr(p->pLimit, pnHeight);
96780 heightOfExprList(p->pEList, pnHeight);
96781 heightOfExprList(p->pGroupBy, pnHeight);
96782 heightOfExprList(p->pOrderBy, pnHeight);
96783 }
96784}
96785
96786/*
96787** Set the Expr.nHeight variable in the structure passed as an
96788** argument. An expression with no children, Expr.pList or
96789** Expr.pSelect member has a height of 1. Any other expression
96790** has a height equal to the maximum height of any other
96791** referenced Expr plus one.
96792**
96793** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags,
96794** if appropriate.
96795*/
96796static void exprSetHeight(Expr *p){
96797 int nHeight = 0;
96798 heightOfExpr(p->pLeft, &nHeight);
96799 heightOfExpr(p->pRight, &nHeight);
96800 if( ExprHasProperty(p, EP_xIsSelect) ){
96801 heightOfSelect(p->x.pSelect, &nHeight);
96802 }else if( p->x.pList ){
96803 heightOfExprList(p->x.pList, &nHeight);
96804 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
96805 }
96806 p->nHeight = nHeight + 1;
96807}
96808
96809/*
96810** Set the Expr.nHeight variable using the exprSetHeight() function. If
96811** the height is greater than the maximum allowed expression depth,
96812** leave an error in pParse.
96813**
96814** Also propagate all EP_Propagate flags from the Expr.x.pList into
96815** Expr.flags.
96816*/
96817SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
96818 if( pParse->nErr ) return;
96819 exprSetHeight(p);
96820 sqlite3ExprCheckHeight(pParse, p->nHeight);
96821}
96822
96823/*
96824** Return the maximum height of any expression tree referenced
96825** by the select statement passed as an argument.
96826*/
96827SQLITE_PRIVATE int sqlite3SelectExprHeight(Select *p){
96828 int nHeight = 0;
96829 heightOfSelect(p, &nHeight);
96830 return nHeight;
96831}
96832#else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */
96833/*
96834** Propagate all EP_Propagate flags from the Expr.x.pList into
96835** Expr.flags.
96836*/
96837SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
96838 if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect) ){
96839 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
96840 }
96841}
96842#define exprSetHeight(y)
96843#endif /* SQLITE_MAX_EXPR_DEPTH>0 */
96844
96845/*
96846** This routine is the core allocator for Expr nodes.
96847**
96848** Construct a new expression node and return a pointer to it. Memory
96849** for this node and for the pToken argument is a single allocation
96850** obtained from sqlite3DbMalloc(). The calling function
96851** is responsible for making sure the node eventually gets freed.
96852**
96853** If dequote is true, then the token (if it exists) is dequoted.
96854** If dequote is false, no dequoting is performed. The deQuote
96855** parameter is ignored if pToken is NULL or if the token does not
96856** appear to be quoted. If the quotes were of the form "..." (double-quotes)
96857** then the EP_DblQuoted flag is set on the expression node.
96858**
96859** Special case: If op==TK_INTEGER and pToken points to a string that
96860** can be translated into a 32-bit integer, then the token is not
96861** stored in u.zToken. Instead, the integer values is written
96862** into u.iValue and the EP_IntValue flag is set. No extra storage
96863** is allocated to hold the integer text and the dequote flag is ignored.
96864*/
96865SQLITE_PRIVATE Expr *sqlite3ExprAlloc(
96866 sqlite3 *db, /* Handle for sqlite3DbMallocRawNN() */
96867 int op, /* Expression opcode */
96868 const Token *pToken, /* Token argument. Might be NULL */
96869 int dequote /* True to dequote */
96870){
96871 Expr *pNew;
96872 int nExtra = 0;
96873 int iValue = 0;
96874
96875 assert( db!=0 );
96876 if( pToken ){
96877 if( op!=TK_INTEGER || pToken->z==0
96878 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
96879 nExtra = pToken->n+1;
96880 assert( iValue>=0 );
96881 }
96882 }
96883 pNew = sqlite3DbMallocRawNN(db, sizeof(Expr)+nExtra);
96884 if( pNew ){
96885 memset(pNew, 0, sizeof(Expr));
96886 pNew->op = (u8)op;
96887 pNew->iAgg = -1;
96888 if( pToken ){
96889 if( nExtra==0 ){
96890 pNew->flags |= EP_IntValue|EP_Leaf;
96891 pNew->u.iValue = iValue;
96892 }else{
96893 pNew->u.zToken = (char*)&pNew[1];
96894 assert( pToken->z!=0 || pToken->n==0 );
96895 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
96896 pNew->u.zToken[pToken->n] = 0;
96897 if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){
96898 if( pNew->u.zToken[0]=='"' ) pNew->flags |= EP_DblQuoted;
96899 sqlite3Dequote(pNew->u.zToken);
96900 }
96901 }
96902 }
96903#if SQLITE_MAX_EXPR_DEPTH>0
96904 pNew->nHeight = 1;
96905#endif
96906 }
96907 return pNew;
96908}
96909
96910/*
96911** Allocate a new expression node from a zero-terminated token that has
96912** already been dequoted.
96913*/
96914SQLITE_PRIVATE Expr *sqlite3Expr(
96915 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
96916 int op, /* Expression opcode */
96917 const char *zToken /* Token argument. Might be NULL */
96918){
96919 Token x;
96920 x.z = zToken;
96921 x.n = sqlite3Strlen30(zToken);
96922 return sqlite3ExprAlloc(db, op, &x, 0);
96923}
96924
96925/*
96926** Attach subtrees pLeft and pRight to the Expr node pRoot.
96927**
96928** If pRoot==NULL that means that a memory allocation error has occurred.
96929** In that case, delete the subtrees pLeft and pRight.
96930*/
96931SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(
96932 sqlite3 *db,
96933 Expr *pRoot,
96934 Expr *pLeft,
96935 Expr *pRight
96936){
96937 if( pRoot==0 ){
96938 assert( db->mallocFailed );
96939 sqlite3ExprDelete(db, pLeft);
96940 sqlite3ExprDelete(db, pRight);
96941 }else{
96942 if( pRight ){
96943 pRoot->pRight = pRight;
96944 pRoot->flags |= EP_Propagate & pRight->flags;
96945 }
96946 if( pLeft ){
96947 pRoot->pLeft = pLeft;
96948 pRoot->flags |= EP_Propagate & pLeft->flags;
96949 }
96950 exprSetHeight(pRoot);
96951 }
96952}
96953
96954/*
96955** Allocate an Expr node which joins as many as two subtrees.
96956**
96957** One or both of the subtrees can be NULL. Return a pointer to the new
96958** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
96959** free the subtrees and return NULL.
96960*/
96961SQLITE_PRIVATE Expr *sqlite3PExpr(
96962 Parse *pParse, /* Parsing context */
96963 int op, /* Expression opcode */
96964 Expr *pLeft, /* Left operand */
96965 Expr *pRight /* Right operand */
96966){
96967 Expr *p;
96968 if( op==TK_AND && pParse->nErr==0 ){
96969 /* Take advantage of short-circuit false optimization for AND */
96970 p = sqlite3ExprAnd(pParse->db, pLeft, pRight);
96971 }else{
96972 p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
96973 if( p ){
96974 memset(p, 0, sizeof(Expr));
96975 p->op = op & TKFLG_MASK;
96976 p->iAgg = -1;
96977 }
96978 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
96979 }
96980 if( p ) {
96981 sqlite3ExprCheckHeight(pParse, p->nHeight);
96982 }
96983 return p;
96984}
96985
96986/*
96987** Add pSelect to the Expr.x.pSelect field. Or, if pExpr is NULL (due
96988** do a memory allocation failure) then delete the pSelect object.
96989*/
96990SQLITE_PRIVATE void sqlite3PExprAddSelect(Parse *pParse, Expr *pExpr, Select *pSelect){
96991 if( pExpr ){
96992 pExpr->x.pSelect = pSelect;
96993 ExprSetProperty(pExpr, EP_xIsSelect|EP_Subquery);
96994 sqlite3ExprSetHeightAndFlags(pParse, pExpr);
96995 }else{
96996 assert( pParse->db->mallocFailed );
96997 sqlite3SelectDelete(pParse->db, pSelect);
96998 }
96999}
97000
97001
97002/*
97003** If the expression is always either TRUE or FALSE (respectively),
97004** then return 1. If one cannot determine the truth value of the
97005** expression at compile-time return 0.
97006**
97007** This is an optimization. If is OK to return 0 here even if
97008** the expression really is always false or false (a false negative).
97009** But it is a bug to return 1 if the expression might have different
97010** boolean values in different circumstances (a false positive.)
97011**
97012** Note that if the expression is part of conditional for a
97013** LEFT JOIN, then we cannot determine at compile-time whether or not
97014** is it true or false, so always return 0.
97015*/
97016static int exprAlwaysTrue(Expr *p){
97017 int v = 0;
97018 if( ExprHasProperty(p, EP_FromJoin) ) return 0;
97019 if( !sqlite3ExprIsInteger(p, &v) ) return 0;
97020 return v!=0;
97021}
97022static int exprAlwaysFalse(Expr *p){
97023 int v = 0;
97024 if( ExprHasProperty(p, EP_FromJoin) ) return 0;
97025 if( !sqlite3ExprIsInteger(p, &v) ) return 0;
97026 return v==0;
97027}
97028
97029/*
97030** Join two expressions using an AND operator. If either expression is
97031** NULL, then just return the other expression.
97032**
97033** If one side or the other of the AND is known to be false, then instead
97034** of returning an AND expression, just return a constant expression with
97035** a value of false.
97036*/
97037SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
97038 if( pLeft==0 ){
97039 return pRight;
97040 }else if( pRight==0 ){
97041 return pLeft;
97042 }else if( exprAlwaysFalse(pLeft) || exprAlwaysFalse(pRight) ){
97043 sqlite3ExprDelete(db, pLeft);
97044 sqlite3ExprDelete(db, pRight);
97045 return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0);
97046 }else{
97047 Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0);
97048 sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight);
97049 return pNew;
97050 }
97051}
97052
97053/*
97054** Construct a new expression node for a function with multiple
97055** arguments.
97056*/
97057SQLITE_PRIVATE Expr *sqlite3ExprFunction(
97058 Parse *pParse, /* Parsing context */
97059 ExprList *pList, /* Argument list */
97060 Token *pToken, /* Name of the function */
97061 int eDistinct /* SF_Distinct or SF_ALL or 0 */
97062){
97063 Expr *pNew;
97064 sqlite3 *db = pParse->db;
97065 assert( pToken );
97066 pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
97067 if( pNew==0 ){
97068 sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
97069 return 0;
97070 }
97071 if( pList && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
97072 sqlite3ErrorMsg(pParse, "too many arguments on function %T", pToken);
97073 }
97074 pNew->x.pList = pList;
97075 ExprSetProperty(pNew, EP_HasFunc);
97076 assert( !ExprHasProperty(pNew, EP_xIsSelect) );
97077 sqlite3ExprSetHeightAndFlags(pParse, pNew);
97078 if( eDistinct==SF_Distinct ) ExprSetProperty(pNew, EP_Distinct);
97079 return pNew;
97080}
97081
97082/*
97083** Assign a variable number to an expression that encodes a wildcard
97084** in the original SQL statement.
97085**
97086** Wildcards consisting of a single "?" are assigned the next sequential
97087** variable number.
97088**
97089** Wildcards of the form "?nnn" are assigned the number "nnn". We make
97090** sure "nnn" is not too big to avoid a denial of service attack when
97091** the SQL statement comes from an external source.
97092**
97093** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
97094** as the previous instance of the same wildcard. Or if this is the first
97095** instance of the wildcard, the next sequential variable number is
97096** assigned.
97097*/
97098SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr, u32 n){
97099 sqlite3 *db = pParse->db;
97100 const char *z;
97101 ynVar x;
97102
97103 if( pExpr==0 ) return;
97104 assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
97105 z = pExpr->u.zToken;
97106 assert( z!=0 );
97107 assert( z[0]!=0 );
97108 assert( n==(u32)sqlite3Strlen30(z) );
97109 if( z[1]==0 ){
97110 /* Wildcard of the form "?". Assign the next variable number */
97111 assert( z[0]=='?' );
97112 x = (ynVar)(++pParse->nVar);
97113 }else{
97114 int doAdd = 0;
97115 if( z[0]=='?' ){
97116 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
97117 ** use it as the variable number */
97118 i64 i;
97119 int bOk;
97120 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
97121 i = z[1]-'0'; /* The common case of ?N for a single digit N */
97122 bOk = 1;
97123 }else{
97124 bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
97125 }
97126 testcase( i==0 );
97127 testcase( i==1 );
97128 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
97129 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
97130 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
97131 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
97132 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
97133 return;
97134 }
97135 x = (ynVar)i;
97136 if( x>pParse->nVar ){
97137 pParse->nVar = (int)x;
97138 doAdd = 1;
97139 }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
97140 doAdd = 1;
97141 }
97142 }else{
97143 /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable
97144 ** number as the prior appearance of the same name, or if the name
97145 ** has never appeared before, reuse the same variable number
97146 */
97147 x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n);
97148 if( x==0 ){
97149 x = (ynVar)(++pParse->nVar);
97150 doAdd = 1;
97151 }
97152 }
97153 if( doAdd ){
97154 pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x);
97155 }
97156 }
97157 pExpr->iColumn = x;
97158 if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
97159 sqlite3ErrorMsg(pParse, "too many SQL variables");
97160 }
97161}
97162
97163/*
97164** Recursively delete an expression tree.
97165*/
97166static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
97167 assert( p!=0 );
97168 /* Sanity check: Assert that the IntValue is non-negative if it exists */
97169 assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
97170
97171 assert( !ExprHasProperty(p, EP_WinFunc) || p->y.pWin!=0 || db->mallocFailed );
97172 assert( p->op!=TK_FUNCTION || ExprHasProperty(p, EP_TokenOnly|EP_Reduced)
97173 || p->y.pWin==0 || ExprHasProperty(p, EP_WinFunc) );
97174#ifdef SQLITE_DEBUG
97175 if( ExprHasProperty(p, EP_Leaf) && !ExprHasProperty(p, EP_TokenOnly) ){
97176 assert( p->pLeft==0 );
97177 assert( p->pRight==0 );
97178 assert( p->x.pSelect==0 );
97179 }
97180#endif
97181 if( !ExprHasProperty(p, (EP_TokenOnly|EP_Leaf)) ){
97182 /* The Expr.x union is never used at the same time as Expr.pRight */
97183 assert( p->x.pList==0 || p->pRight==0 );
97184 if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft);
97185 if( p->pRight ){
97186 sqlite3ExprDeleteNN(db, p->pRight);
97187 }else if( ExprHasProperty(p, EP_xIsSelect) ){
97188 sqlite3SelectDelete(db, p->x.pSelect);
97189 }else{
97190 sqlite3ExprListDelete(db, p->x.pList);
97191 }
97192 if( ExprHasProperty(p, EP_WinFunc) ){
97193 assert( p->op==TK_FUNCTION );
97194 sqlite3WindowDelete(db, p->y.pWin);
97195 }
97196 }
97197 if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
97198 if( !ExprHasProperty(p, EP_Static) ){
97199 sqlite3DbFreeNN(db, p);
97200 }
97201}
97202SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
97203 if( p ) sqlite3ExprDeleteNN(db, p);
97204}
97205
97206/*
97207** Return the number of bytes allocated for the expression structure
97208** passed as the first argument. This is always one of EXPR_FULLSIZE,
97209** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
97210*/
97211static int exprStructSize(Expr *p){
97212 if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
97213 if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
97214 return EXPR_FULLSIZE;
97215}
97216
97217/*
97218** The dupedExpr*Size() routines each return the number of bytes required
97219** to store a copy of an expression or expression tree. They differ in
97220** how much of the tree is measured.
97221**
97222** dupedExprStructSize() Size of only the Expr structure
97223** dupedExprNodeSize() Size of Expr + space for token
97224** dupedExprSize() Expr + token + subtree components
97225**
97226***************************************************************************
97227**
97228** The dupedExprStructSize() function returns two values OR-ed together:
97229** (1) the space required for a copy of the Expr structure only and
97230** (2) the EP_xxx flags that indicate what the structure size should be.
97231** The return values is always one of:
97232**
97233** EXPR_FULLSIZE
97234** EXPR_REDUCEDSIZE | EP_Reduced
97235** EXPR_TOKENONLYSIZE | EP_TokenOnly
97236**
97237** The size of the structure can be found by masking the return value
97238** of this routine with 0xfff. The flags can be found by masking the
97239** return value with EP_Reduced|EP_TokenOnly.
97240**
97241** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
97242** (unreduced) Expr objects as they or originally constructed by the parser.
97243** During expression analysis, extra information is computed and moved into
97244** later parts of the Expr object and that extra information might get chopped
97245** off if the expression is reduced. Note also that it does not work to
97246** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal
97247** to reduce a pristine expression tree from the parser. The implementation
97248** of dupedExprStructSize() contain multiple assert() statements that attempt
97249** to enforce this constraint.
97250*/
97251static int dupedExprStructSize(Expr *p, int flags){
97252 int nSize;
97253 assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
97254 assert( EXPR_FULLSIZE<=0xfff );
97255 assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 );
97256 if( 0==flags || p->op==TK_SELECT_COLUMN
97257#ifndef SQLITE_OMIT_WINDOWFUNC
97258 || ExprHasProperty(p, EP_WinFunc)
97259#endif
97260 ){
97261 nSize = EXPR_FULLSIZE;
97262 }else{
97263 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
97264 assert( !ExprHasProperty(p, EP_FromJoin) );
97265 assert( !ExprHasProperty(p, EP_MemToken) );
97266 assert( !ExprHasProperty(p, EP_NoReduce) );
97267 if( p->pLeft || p->x.pList ){
97268 nSize = EXPR_REDUCEDSIZE | EP_Reduced;
97269 }else{
97270 assert( p->pRight==0 );
97271 nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
97272 }
97273 }
97274 return nSize;
97275}
97276
97277/*
97278** This function returns the space in bytes required to store the copy
97279** of the Expr structure and a copy of the Expr.u.zToken string (if that
97280** string is defined.)
97281*/
97282static int dupedExprNodeSize(Expr *p, int flags){
97283 int nByte = dupedExprStructSize(p, flags) & 0xfff;
97284 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
97285 nByte += sqlite3Strlen30NN(p->u.zToken)+1;
97286 }
97287 return ROUND8(nByte);
97288}
97289
97290/*
97291** Return the number of bytes required to create a duplicate of the
97292** expression passed as the first argument. The second argument is a
97293** mask containing EXPRDUP_XXX flags.
97294**
97295** The value returned includes space to create a copy of the Expr struct
97296** itself and the buffer referred to by Expr.u.zToken, if any.
97297**
97298** If the EXPRDUP_REDUCE flag is set, then the return value includes
97299** space to duplicate all Expr nodes in the tree formed by Expr.pLeft
97300** and Expr.pRight variables (but not for any structures pointed to or
97301** descended from the Expr.x.pList or Expr.x.pSelect variables).
97302*/
97303static int dupedExprSize(Expr *p, int flags){
97304 int nByte = 0;
97305 if( p ){
97306 nByte = dupedExprNodeSize(p, flags);
97307 if( flags&EXPRDUP_REDUCE ){
97308 nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
97309 }
97310 }
97311 return nByte;
97312}
97313
97314/*
97315** This function is similar to sqlite3ExprDup(), except that if pzBuffer
97316** is not NULL then *pzBuffer is assumed to point to a buffer large enough
97317** to store the copy of expression p, the copies of p->u.zToken
97318** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
97319** if any. Before returning, *pzBuffer is set to the first byte past the
97320** portion of the buffer copied into by this function.
97321*/
97322static Expr *exprDup(sqlite3 *db, Expr *p, int dupFlags, u8 **pzBuffer){
97323 Expr *pNew; /* Value to return */
97324 u8 *zAlloc; /* Memory space from which to build Expr object */
97325 u32 staticFlag; /* EP_Static if space not obtained from malloc */
97326
97327 assert( db!=0 );
97328 assert( p );
97329 assert( dupFlags==0 || dupFlags==EXPRDUP_REDUCE );
97330 assert( pzBuffer==0 || dupFlags==EXPRDUP_REDUCE );
97331
97332 /* Figure out where to write the new Expr structure. */
97333 if( pzBuffer ){
97334 zAlloc = *pzBuffer;
97335 staticFlag = EP_Static;
97336 }else{
97337 zAlloc = sqlite3DbMallocRawNN(db, dupedExprSize(p, dupFlags));
97338 staticFlag = 0;
97339 }
97340 pNew = (Expr *)zAlloc;
97341
97342 if( pNew ){
97343 /* Set nNewSize to the size allocated for the structure pointed to
97344 ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
97345 ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
97346 ** by the copy of the p->u.zToken string (if any).
97347 */
97348 const unsigned nStructSize = dupedExprStructSize(p, dupFlags);
97349 const int nNewSize = nStructSize & 0xfff;
97350 int nToken;
97351 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
97352 nToken = sqlite3Strlen30(p->u.zToken) + 1;
97353 }else{
97354 nToken = 0;
97355 }
97356 if( dupFlags ){
97357 assert( ExprHasProperty(p, EP_Reduced)==0 );
97358 memcpy(zAlloc, p, nNewSize);
97359 }else{
97360 u32 nSize = (u32)exprStructSize(p);
97361 memcpy(zAlloc, p, nSize);
97362 if( nSize<EXPR_FULLSIZE ){
97363 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
97364 }
97365 }
97366
97367 /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
97368 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
97369 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
97370 pNew->flags |= staticFlag;
97371
97372 /* Copy the p->u.zToken string, if any. */
97373 if( nToken ){
97374 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
97375 memcpy(zToken, p->u.zToken, nToken);
97376 }
97377
97378 if( 0==((p->flags|pNew->flags) & (EP_TokenOnly|EP_Leaf)) ){
97379 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
97380 if( ExprHasProperty(p, EP_xIsSelect) ){
97381 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
97382 }else{
97383 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
97384 }
97385 }
97386
97387 /* Fill in pNew->pLeft and pNew->pRight. */
97388 if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly|EP_WinFunc) ){
97389 zAlloc += dupedExprNodeSize(p, dupFlags);
97390 if( !ExprHasProperty(pNew, EP_TokenOnly|EP_Leaf) ){
97391 pNew->pLeft = p->pLeft ?
97392 exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0;
97393 pNew->pRight = p->pRight ?
97394 exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0;
97395 }
97396#ifndef SQLITE_OMIT_WINDOWFUNC
97397 if( ExprHasProperty(p, EP_WinFunc) ){
97398 pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin);
97399 assert( ExprHasProperty(pNew, EP_WinFunc) );
97400 }
97401#endif /* SQLITE_OMIT_WINDOWFUNC */
97402 if( pzBuffer ){
97403 *pzBuffer = zAlloc;
97404 }
97405 }else{
97406 if( !ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
97407 if( pNew->op==TK_SELECT_COLUMN ){
97408 pNew->pLeft = p->pLeft;
97409 assert( p->iColumn==0 || p->pRight==0 );
97410 assert( p->pRight==0 || p->pRight==p->pLeft );
97411 }else{
97412 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
97413 }
97414 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
97415 }
97416 }
97417 }
97418 return pNew;
97419}
97420
97421/*
97422** Create and return a deep copy of the object passed as the second
97423** argument. If an OOM condition is encountered, NULL is returned
97424** and the db->mallocFailed flag set.
97425*/
97426#ifndef SQLITE_OMIT_CTE
97427static With *withDup(sqlite3 *db, With *p){
97428 With *pRet = 0;
97429 if( p ){
97430 int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
97431 pRet = sqlite3DbMallocZero(db, nByte);
97432 if( pRet ){
97433 int i;
97434 pRet->nCte = p->nCte;
97435 for(i=0; i<p->nCte; i++){
97436 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
97437 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
97438 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
97439 }
97440 }
97441 }
97442 return pRet;
97443}
97444#else
97445# define withDup(x,y) 0
97446#endif
97447
97448/*
97449** The following group of routines make deep copies of expressions,
97450** expression lists, ID lists, and select statements. The copies can
97451** be deleted (by being passed to their respective ...Delete() routines)
97452** without effecting the originals.
97453**
97454** The expression list, ID, and source lists return by sqlite3ExprListDup(),
97455** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
97456** by subsequent calls to sqlite*ListAppend() routines.
97457**
97458** Any tables that the SrcList might point to are not duplicated.
97459**
97460** The flags parameter contains a combination of the EXPRDUP_XXX flags.
97461** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
97462** truncated version of the usual Expr structure that will be stored as
97463** part of the in-memory representation of the database schema.
97464*/
97465SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
97466 assert( flags==0 || flags==EXPRDUP_REDUCE );
97467 return p ? exprDup(db, p, flags, 0) : 0;
97468}
97469SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
97470 ExprList *pNew;
97471 struct ExprList_item *pItem, *pOldItem;
97472 int i;
97473 Expr *pPriorSelectCol = 0;
97474 assert( db!=0 );
97475 if( p==0 ) return 0;
97476 pNew = sqlite3DbMallocRawNN(db, sqlite3DbMallocSize(db, p));
97477 if( pNew==0 ) return 0;
97478 pNew->nExpr = p->nExpr;
97479 pItem = pNew->a;
97480 pOldItem = p->a;
97481 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
97482 Expr *pOldExpr = pOldItem->pExpr;
97483 Expr *pNewExpr;
97484 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
97485 if( pOldExpr
97486 && pOldExpr->op==TK_SELECT_COLUMN
97487 && (pNewExpr = pItem->pExpr)!=0
97488 ){
97489 assert( pNewExpr->iColumn==0 || i>0 );
97490 if( pNewExpr->iColumn==0 ){
97491 assert( pOldExpr->pLeft==pOldExpr->pRight );
97492 pPriorSelectCol = pNewExpr->pLeft = pNewExpr->pRight;
97493 }else{
97494 assert( i>0 );
97495 assert( pItem[-1].pExpr!=0 );
97496 assert( pNewExpr->iColumn==pItem[-1].pExpr->iColumn+1 );
97497 assert( pPriorSelectCol==pItem[-1].pExpr->pLeft );
97498 pNewExpr->pLeft = pPriorSelectCol;
97499 }
97500 }
97501 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
97502 pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
97503 pItem->sortOrder = pOldItem->sortOrder;
97504 pItem->done = 0;
97505 pItem->bSpanIsTab = pOldItem->bSpanIsTab;
97506 pItem->bSorterRef = pOldItem->bSorterRef;
97507 pItem->u = pOldItem->u;
97508 }
97509 return pNew;
97510}
97511
97512/*
97513** If cursors, triggers, views and subqueries are all omitted from
97514** the build, then none of the following routines, except for
97515** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
97516** called with a NULL argument.
97517*/
97518#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
97519 || !defined(SQLITE_OMIT_SUBQUERY)
97520SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
97521 SrcList *pNew;
97522 int i;
97523 int nByte;
97524 assert( db!=0 );
97525 if( p==0 ) return 0;
97526 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
97527 pNew = sqlite3DbMallocRawNN(db, nByte );
97528 if( pNew==0 ) return 0;
97529 pNew->nSrc = pNew->nAlloc = p->nSrc;
97530 for(i=0; i<p->nSrc; i++){
97531 struct SrcList_item *pNewItem = &pNew->a[i];
97532 struct SrcList_item *pOldItem = &p->a[i];
97533 Table *pTab;
97534 pNewItem->pSchema = pOldItem->pSchema;
97535 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
97536 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
97537 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
97538 pNewItem->fg = pOldItem->fg;
97539 pNewItem->iCursor = pOldItem->iCursor;
97540 pNewItem->addrFillSub = pOldItem->addrFillSub;
97541 pNewItem->regReturn = pOldItem->regReturn;
97542 if( pNewItem->fg.isIndexedBy ){
97543 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
97544 }
97545 pNewItem->pIBIndex = pOldItem->pIBIndex;
97546 if( pNewItem->fg.isTabFunc ){
97547 pNewItem->u1.pFuncArg =
97548 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
97549 }
97550 pTab = pNewItem->pTab = pOldItem->pTab;
97551 if( pTab ){
97552 pTab->nTabRef++;
97553 }
97554 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
97555 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
97556 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
97557 pNewItem->colUsed = pOldItem->colUsed;
97558 }
97559 return pNew;
97560}
97561SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
97562 IdList *pNew;
97563 int i;
97564 assert( db!=0 );
97565 if( p==0 ) return 0;
97566 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) );
97567 if( pNew==0 ) return 0;
97568 pNew->nId = p->nId;
97569 pNew->a = sqlite3DbMallocRawNN(db, p->nId*sizeof(p->a[0]) );
97570 if( pNew->a==0 ){
97571 sqlite3DbFreeNN(db, pNew);
97572 return 0;
97573 }
97574 /* Note that because the size of the allocation for p->a[] is not
97575 ** necessarily a power of two, sqlite3IdListAppend() may not be called
97576 ** on the duplicate created by this function. */
97577 for(i=0; i<p->nId; i++){
97578 struct IdList_item *pNewItem = &pNew->a[i];
97579 struct IdList_item *pOldItem = &p->a[i];
97580 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
97581 pNewItem->idx = pOldItem->idx;
97582 }
97583 return pNew;
97584}
97585SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *pDup, int flags){
97586 Select *pRet = 0;
97587 Select *pNext = 0;
97588 Select **pp = &pRet;
97589 Select *p;
97590
97591 assert( db!=0 );
97592 for(p=pDup; p; p=p->pPrior){
97593 Select *pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
97594 if( pNew==0 ) break;
97595 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
97596 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
97597 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
97598 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
97599 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
97600 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
97601 pNew->op = p->op;
97602 pNew->pNext = pNext;
97603 pNew->pPrior = 0;
97604 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
97605 pNew->iLimit = 0;
97606 pNew->iOffset = 0;
97607 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
97608 pNew->addrOpenEphm[0] = -1;
97609 pNew->addrOpenEphm[1] = -1;
97610 pNew->nSelectRow = p->nSelectRow;
97611 pNew->pWith = withDup(db, p->pWith);
97612#ifndef SQLITE_OMIT_WINDOWFUNC
97613 pNew->pWin = 0;
97614 pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn);
97615#endif
97616 pNew->selId = p->selId;
97617 *pp = pNew;
97618 pp = &pNew->pPrior;
97619 pNext = pNew;
97620 }
97621
97622 return pRet;
97623}
97624#else
97625SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
97626 assert( p==0 );
97627 return 0;
97628}
97629#endif
97630
97631
97632/*
97633** Add a new element to the end of an expression list. If pList is
97634** initially NULL, then create a new expression list.
97635**
97636** The pList argument must be either NULL or a pointer to an ExprList
97637** obtained from a prior call to sqlite3ExprListAppend(). This routine
97638** may not be used with an ExprList obtained from sqlite3ExprListDup().
97639** Reason: This routine assumes that the number of slots in pList->a[]
97640** is a power of two. That is true for sqlite3ExprListAppend() returns
97641** but is not necessarily true from the return value of sqlite3ExprListDup().
97642**
97643** If a memory allocation error occurs, the entire list is freed and
97644** NULL is returned. If non-NULL is returned, then it is guaranteed
97645** that the new entry was successfully appended.
97646*/
97647SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(
97648 Parse *pParse, /* Parsing context */
97649 ExprList *pList, /* List to which to append. Might be NULL */
97650 Expr *pExpr /* Expression to be appended. Might be NULL */
97651){
97652 struct ExprList_item *pItem;
97653 sqlite3 *db = pParse->db;
97654 assert( db!=0 );
97655 if( pList==0 ){
97656 pList = sqlite3DbMallocRawNN(db, sizeof(ExprList) );
97657 if( pList==0 ){
97658 goto no_mem;
97659 }
97660 pList->nExpr = 0;
97661 }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
97662 ExprList *pNew;
97663 pNew = sqlite3DbRealloc(db, pList,
97664 sizeof(*pList)+(2*pList->nExpr - 1)*sizeof(pList->a[0]));
97665 if( pNew==0 ){
97666 goto no_mem;
97667 }
97668 pList = pNew;
97669 }
97670 pItem = &pList->a[pList->nExpr++];
97671 assert( offsetof(struct ExprList_item,zName)==sizeof(pItem->pExpr) );
97672 assert( offsetof(struct ExprList_item,pExpr)==0 );
97673 memset(&pItem->zName,0,sizeof(*pItem)-offsetof(struct ExprList_item,zName));
97674 pItem->pExpr = pExpr;
97675 return pList;
97676
97677no_mem:
97678 /* Avoid leaking memory if malloc has failed. */
97679 sqlite3ExprDelete(db, pExpr);
97680 sqlite3ExprListDelete(db, pList);
97681 return 0;
97682}
97683
97684/*
97685** pColumns and pExpr form a vector assignment which is part of the SET
97686** clause of an UPDATE statement. Like this:
97687**
97688** (a,b,c) = (expr1,expr2,expr3)
97689** Or: (a,b,c) = (SELECT x,y,z FROM ....)
97690**
97691** For each term of the vector assignment, append new entries to the
97692** expression list pList. In the case of a subquery on the RHS, append
97693** TK_SELECT_COLUMN expressions.
97694*/
97695SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(
97696 Parse *pParse, /* Parsing context */
97697 ExprList *pList, /* List to which to append. Might be NULL */
97698 IdList *pColumns, /* List of names of LHS of the assignment */
97699 Expr *pExpr /* Vector expression to be appended. Might be NULL */
97700){
97701 sqlite3 *db = pParse->db;
97702 int n;
97703 int i;
97704 int iFirst = pList ? pList->nExpr : 0;
97705 /* pColumns can only be NULL due to an OOM but an OOM will cause an
97706 ** exit prior to this routine being invoked */
97707 if( NEVER(pColumns==0) ) goto vector_append_error;
97708 if( pExpr==0 ) goto vector_append_error;
97709
97710 /* If the RHS is a vector, then we can immediately check to see that
97711 ** the size of the RHS and LHS match. But if the RHS is a SELECT,
97712 ** wildcards ("*") in the result set of the SELECT must be expanded before
97713 ** we can do the size check, so defer the size check until code generation.
97714 */
97715 if( pExpr->op!=TK_SELECT && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){
97716 sqlite3ErrorMsg(pParse, "%d columns assigned %d values",
97717 pColumns->nId, n);
97718 goto vector_append_error;
97719 }
97720
97721 for(i=0; i<pColumns->nId; i++){
97722 Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i);
97723 pList = sqlite3ExprListAppend(pParse, pList, pSubExpr);
97724 if( pList ){
97725 assert( pList->nExpr==iFirst+i+1 );
97726 pList->a[pList->nExpr-1].zName = pColumns->a[i].zName;
97727 pColumns->a[i].zName = 0;
97728 }
97729 }
97730
97731 if( !db->mallocFailed && pExpr->op==TK_SELECT && ALWAYS(pList!=0) ){
97732 Expr *pFirst = pList->a[iFirst].pExpr;
97733 assert( pFirst!=0 );
97734 assert( pFirst->op==TK_SELECT_COLUMN );
97735
97736 /* Store the SELECT statement in pRight so it will be deleted when
97737 ** sqlite3ExprListDelete() is called */
97738 pFirst->pRight = pExpr;
97739 pExpr = 0;
97740
97741 /* Remember the size of the LHS in iTable so that we can check that
97742 ** the RHS and LHS sizes match during code generation. */
97743 pFirst->iTable = pColumns->nId;
97744 }
97745
97746vector_append_error:
97747 sqlite3ExprDelete(db, pExpr);
97748 sqlite3IdListDelete(db, pColumns);
97749 return pList;
97750}
97751
97752/*
97753** Set the sort order for the last element on the given ExprList.
97754*/
97755SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder){
97756 if( p==0 ) return;
97757 assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC>=0 && SQLITE_SO_DESC>0 );
97758 assert( p->nExpr>0 );
97759 if( iSortOrder<0 ){
97760 assert( p->a[p->nExpr-1].sortOrder==SQLITE_SO_ASC );
97761 return;
97762 }
97763 p->a[p->nExpr-1].sortOrder = (u8)iSortOrder;
97764}
97765
97766/*
97767** Set the ExprList.a[].zName element of the most recently added item
97768** on the expression list.
97769**
97770** pList might be NULL following an OOM error. But pName should never be
97771** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
97772** is set.
97773*/
97774SQLITE_PRIVATE void sqlite3ExprListSetName(
97775 Parse *pParse, /* Parsing context */
97776 ExprList *pList, /* List to which to add the span. */
97777 Token *pName, /* Name to be added */
97778 int dequote /* True to cause the name to be dequoted */
97779){
97780 assert( pList!=0 || pParse->db->mallocFailed!=0 );
97781 if( pList ){
97782 struct ExprList_item *pItem;
97783 assert( pList->nExpr>0 );
97784 pItem = &pList->a[pList->nExpr-1];
97785 assert( pItem->zName==0 );
97786 pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
97787 if( dequote ) sqlite3Dequote(pItem->zName);
97788 if( IN_RENAME_OBJECT ){
97789 sqlite3RenameTokenMap(pParse, (void*)pItem->zName, pName);
97790 }
97791 }
97792}
97793
97794/*
97795** Set the ExprList.a[].zSpan element of the most recently added item
97796** on the expression list.
97797**
97798** pList might be NULL following an OOM error. But pSpan should never be
97799** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
97800** is set.
97801*/
97802SQLITE_PRIVATE void sqlite3ExprListSetSpan(
97803 Parse *pParse, /* Parsing context */
97804 ExprList *pList, /* List to which to add the span. */
97805 const char *zStart, /* Start of the span */
97806 const char *zEnd /* End of the span */
97807){
97808 sqlite3 *db = pParse->db;
97809 assert( pList!=0 || db->mallocFailed!=0 );
97810 if( pList ){
97811 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
97812 assert( pList->nExpr>0 );
97813 sqlite3DbFree(db, pItem->zSpan);
97814 pItem->zSpan = sqlite3DbSpanDup(db, zStart, zEnd);
97815 }
97816}
97817
97818/*
97819** If the expression list pEList contains more than iLimit elements,
97820** leave an error message in pParse.
97821*/
97822SQLITE_PRIVATE void sqlite3ExprListCheckLength(
97823 Parse *pParse,
97824 ExprList *pEList,
97825 const char *zObject
97826){
97827 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
97828 testcase( pEList && pEList->nExpr==mx );
97829 testcase( pEList && pEList->nExpr==mx+1 );
97830 if( pEList && pEList->nExpr>mx ){
97831 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
97832 }
97833}
97834
97835/*
97836** Delete an entire expression list.
97837*/
97838static SQLITE_NOINLINE void exprListDeleteNN(sqlite3 *db, ExprList *pList){
97839 int i = pList->nExpr;
97840 struct ExprList_item *pItem = pList->a;
97841 assert( pList->nExpr>0 );
97842 do{
97843 sqlite3ExprDelete(db, pItem->pExpr);
97844 sqlite3DbFree(db, pItem->zName);
97845 sqlite3DbFree(db, pItem->zSpan);
97846 pItem++;
97847 }while( --i>0 );
97848 sqlite3DbFreeNN(db, pList);
97849}
97850SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
97851 if( pList ) exprListDeleteNN(db, pList);
97852}
97853
97854/*
97855** Return the bitwise-OR of all Expr.flags fields in the given
97856** ExprList.
97857*/
97858SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList *pList){
97859 int i;
97860 u32 m = 0;
97861 assert( pList!=0 );
97862 for(i=0; i<pList->nExpr; i++){
97863 Expr *pExpr = pList->a[i].pExpr;
97864 assert( pExpr!=0 );
97865 m |= pExpr->flags;
97866 }
97867 return m;
97868}
97869
97870/*
97871** This is a SELECT-node callback for the expression walker that
97872** always "fails". By "fail" in this case, we mean set
97873** pWalker->eCode to zero and abort.
97874**
97875** This callback is used by multiple expression walkers.
97876*/
97877SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){
97878 UNUSED_PARAMETER(NotUsed);
97879 pWalker->eCode = 0;
97880 return WRC_Abort;
97881}
97882
97883/*
97884** If the input expression is an ID with the name "true" or "false"
97885** then convert it into an TK_TRUEFALSE term. Return non-zero if
97886** the conversion happened, and zero if the expression is unaltered.
97887*/
97888SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr *pExpr){
97889 assert( pExpr->op==TK_ID || pExpr->op==TK_STRING );
97890 if( sqlite3StrICmp(pExpr->u.zToken, "true")==0
97891 || sqlite3StrICmp(pExpr->u.zToken, "false")==0
97892 ){
97893 pExpr->op = TK_TRUEFALSE;
97894 return 1;
97895 }
97896 return 0;
97897}
97898
97899/*
97900** The argument must be a TK_TRUEFALSE Expr node. Return 1 if it is TRUE
97901** and 0 if it is FALSE.
97902*/
97903SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr *pExpr){
97904 assert( pExpr->op==TK_TRUEFALSE );
97905 assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0
97906 || sqlite3StrICmp(pExpr->u.zToken,"false")==0 );
97907 return pExpr->u.zToken[4]==0;
97908}
97909
97910
97911/*
97912** These routines are Walker callbacks used to check expressions to
97913** see if they are "constant" for some definition of constant. The
97914** Walker.eCode value determines the type of "constant" we are looking
97915** for.
97916**
97917** These callback routines are used to implement the following:
97918**
97919** sqlite3ExprIsConstant() pWalker->eCode==1
97920** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
97921** sqlite3ExprIsTableConstant() pWalker->eCode==3
97922** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
97923**
97924** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
97925** is found to not be a constant.
97926**
97927** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions
97928** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing
97929** an existing schema and 4 when processing a new statement. A bound
97930** parameter raises an error for new statements, but is silently converted
97931** to NULL for existing schemas. This allows sqlite_master tables that
97932** contain a bound parameter because they were generated by older versions
97933** of SQLite to be parsed by newer versions of SQLite without raising a
97934** malformed schema error.
97935*/
97936static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
97937
97938 /* If pWalker->eCode is 2 then any term of the expression that comes from
97939 ** the ON or USING clauses of a left join disqualifies the expression
97940 ** from being considered constant. */
97941 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){
97942 pWalker->eCode = 0;
97943 return WRC_Abort;
97944 }
97945
97946 switch( pExpr->op ){
97947 /* Consider functions to be constant if all their arguments are constant
97948 ** and either pWalker->eCode==4 or 5 or the function has the
97949 ** SQLITE_FUNC_CONST flag. */
97950 case TK_FUNCTION:
97951 if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc) ){
97952 return WRC_Continue;
97953 }else{
97954 pWalker->eCode = 0;
97955 return WRC_Abort;
97956 }
97957 case TK_ID:
97958 /* Convert "true" or "false" in a DEFAULT clause into the
97959 ** appropriate TK_TRUEFALSE operator */
97960 if( sqlite3ExprIdToTrueFalse(pExpr) ){
97961 return WRC_Prune;
97962 }
97963 /* Fall thru */
97964 case TK_COLUMN:
97965 case TK_AGG_FUNCTION:
97966 case TK_AGG_COLUMN:
97967 testcase( pExpr->op==TK_ID );
97968 testcase( pExpr->op==TK_COLUMN );
97969 testcase( pExpr->op==TK_AGG_FUNCTION );
97970 testcase( pExpr->op==TK_AGG_COLUMN );
97971 if( ExprHasProperty(pExpr, EP_FixedCol) && pWalker->eCode!=2 ){
97972 return WRC_Continue;
97973 }
97974 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
97975 return WRC_Continue;
97976 }
97977 /* Fall through */
97978 case TK_IF_NULL_ROW:
97979 case TK_REGISTER:
97980 testcase( pExpr->op==TK_REGISTER );
97981 testcase( pExpr->op==TK_IF_NULL_ROW );
97982 pWalker->eCode = 0;
97983 return WRC_Abort;
97984 case TK_VARIABLE:
97985 if( pWalker->eCode==5 ){
97986 /* Silently convert bound parameters that appear inside of CREATE
97987 ** statements into a NULL when parsing the CREATE statement text out
97988 ** of the sqlite_master table */
97989 pExpr->op = TK_NULL;
97990 }else if( pWalker->eCode==4 ){
97991 /* A bound parameter in a CREATE statement that originates from
97992 ** sqlite3_prepare() causes an error */
97993 pWalker->eCode = 0;
97994 return WRC_Abort;
97995 }
97996 /* Fall through */
97997 default:
97998 testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */
97999 testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */
98000 return WRC_Continue;
98001 }
98002}
98003static int exprIsConst(Expr *p, int initFlag, int iCur){
98004 Walker w;
98005 w.eCode = initFlag;
98006 w.xExprCallback = exprNodeIsConstant;
98007 w.xSelectCallback = sqlite3SelectWalkFail;
98008#ifdef SQLITE_DEBUG
98009 w.xSelectCallback2 = sqlite3SelectWalkAssert2;
98010#endif
98011 w.u.iCur = iCur;
98012 sqlite3WalkExpr(&w, p);
98013 return w.eCode;
98014}
98015
98016/*
98017** Walk an expression tree. Return non-zero if the expression is constant
98018** and 0 if it involves variables or function calls.
98019**
98020** For the purposes of this function, a double-quoted string (ex: "abc")
98021** is considered a variable but a single-quoted string (ex: 'abc') is
98022** a constant.
98023*/
98024SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){
98025 return exprIsConst(p, 1, 0);
98026}
98027
98028/*
98029** Walk an expression tree. Return non-zero if
98030**
98031** (1) the expression is constant, and
98032** (2) the expression does originate in the ON or USING clause
98033** of a LEFT JOIN, and
98034** (3) the expression does not contain any EP_FixedCol TK_COLUMN
98035** operands created by the constant propagation optimization.
98036**
98037** When this routine returns true, it indicates that the expression
98038** can be added to the pParse->pConstExpr list and evaluated once when
98039** the prepared statement starts up. See sqlite3ExprCodeAtInit().
98040*/
98041SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
98042 return exprIsConst(p, 2, 0);
98043}
98044
98045/*
98046** Walk an expression tree. Return non-zero if the expression is constant
98047** for any single row of the table with cursor iCur. In other words, the
98048** expression must not refer to any non-deterministic function nor any
98049** table other than iCur.
98050*/
98051SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){
98052 return exprIsConst(p, 3, iCur);
98053}
98054
98055
98056/*
98057** sqlite3WalkExpr() callback used by sqlite3ExprIsConstantOrGroupBy().
98058*/
98059static int exprNodeIsConstantOrGroupBy(Walker *pWalker, Expr *pExpr){
98060 ExprList *pGroupBy = pWalker->u.pGroupBy;
98061 int i;
98062
98063 /* Check if pExpr is identical to any GROUP BY term. If so, consider
98064 ** it constant. */
98065 for(i=0; i<pGroupBy->nExpr; i++){
98066 Expr *p = pGroupBy->a[i].pExpr;
98067 if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
98068 CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p);
98069 if( sqlite3IsBinary(pColl) ){
98070 return WRC_Prune;
98071 }
98072 }
98073 }
98074
98075 /* Check if pExpr is a sub-select. If so, consider it variable. */
98076 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
98077 pWalker->eCode = 0;
98078 return WRC_Abort;
98079 }
98080
98081 return exprNodeIsConstant(pWalker, pExpr);
98082}
98083
98084/*
98085** Walk the expression tree passed as the first argument. Return non-zero
98086** if the expression consists entirely of constants or copies of terms
98087** in pGroupBy that sort with the BINARY collation sequence.
98088**
98089** This routine is used to determine if a term of the HAVING clause can
98090** be promoted into the WHERE clause. In order for such a promotion to work,
98091** the value of the HAVING clause term must be the same for all members of
98092** a "group". The requirement that the GROUP BY term must be BINARY
98093** assumes that no other collating sequence will have a finer-grained
98094** grouping than binary. In other words (A=B COLLATE binary) implies
98095** A=B in every other collating sequence. The requirement that the
98096** GROUP BY be BINARY is stricter than necessary. It would also work
98097** to promote HAVING clauses that use the same alternative collating
98098** sequence as the GROUP BY term, but that is much harder to check,
98099** alternative collating sequences are uncommon, and this is only an
98100** optimization, so we take the easy way out and simply require the
98101** GROUP BY to use the BINARY collating sequence.
98102*/
98103SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse *pParse, Expr *p, ExprList *pGroupBy){
98104 Walker w;
98105 w.eCode = 1;
98106 w.xExprCallback = exprNodeIsConstantOrGroupBy;
98107 w.xSelectCallback = 0;
98108 w.u.pGroupBy = pGroupBy;
98109 w.pParse = pParse;
98110 sqlite3WalkExpr(&w, p);
98111 return w.eCode;
98112}
98113
98114/*
98115** Walk an expression tree. Return non-zero if the expression is constant
98116** or a function call with constant arguments. Return and 0 if there
98117** are any variables.
98118**
98119** For the purposes of this function, a double-quoted string (ex: "abc")
98120** is considered a variable but a single-quoted string (ex: 'abc') is
98121** a constant.
98122*/
98123SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
98124 assert( isInit==0 || isInit==1 );
98125 return exprIsConst(p, 4+isInit, 0);
98126}
98127
98128#ifdef SQLITE_ENABLE_CURSOR_HINTS
98129/*
98130** Walk an expression tree. Return 1 if the expression contains a
98131** subquery of some kind. Return 0 if there are no subqueries.
98132*/
98133SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr *p){
98134 Walker w;
98135 w.eCode = 1;
98136 w.xExprCallback = sqlite3ExprWalkNoop;
98137 w.xSelectCallback = sqlite3SelectWalkFail;
98138#ifdef SQLITE_DEBUG
98139 w.xSelectCallback2 = sqlite3SelectWalkAssert2;
98140#endif
98141 sqlite3WalkExpr(&w, p);
98142 return w.eCode==0;
98143}
98144#endif
98145
98146/*
98147** If the expression p codes a constant integer that is small enough
98148** to fit in a 32-bit integer, return 1 and put the value of the integer
98149** in *pValue. If the expression is not an integer or if it is too big
98150** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
98151*/
98152SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr *p, int *pValue){
98153 int rc = 0;
98154 if( p==0 ) return 0; /* Can only happen following on OOM */
98155
98156 /* If an expression is an integer literal that fits in a signed 32-bit
98157 ** integer, then the EP_IntValue flag will have already been set */
98158 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
98159 || sqlite3GetInt32(p->u.zToken, &rc)==0 );
98160
98161 if( p->flags & EP_IntValue ){
98162 *pValue = p->u.iValue;
98163 return 1;
98164 }
98165 switch( p->op ){
98166 case TK_UPLUS: {
98167 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
98168 break;
98169 }
98170 case TK_UMINUS: {
98171 int v;
98172 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
98173 assert( v!=(-2147483647-1) );
98174 *pValue = -v;
98175 rc = 1;
98176 }
98177 break;
98178 }
98179 default: break;
98180 }
98181 return rc;
98182}
98183
98184/*
98185** Return FALSE if there is no chance that the expression can be NULL.
98186**
98187** If the expression might be NULL or if the expression is too complex
98188** to tell return TRUE.
98189**
98190** This routine is used as an optimization, to skip OP_IsNull opcodes
98191** when we know that a value cannot be NULL. Hence, a false positive
98192** (returning TRUE when in fact the expression can never be NULL) might
98193** be a small performance hit but is otherwise harmless. On the other
98194** hand, a false negative (returning FALSE when the result could be NULL)
98195** will likely result in an incorrect answer. So when in doubt, return
98196** TRUE.
98197*/
98198SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr *p){
98199 u8 op;
98200 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
98201 op = p->op;
98202 if( op==TK_REGISTER ) op = p->op2;
98203 switch( op ){
98204 case TK_INTEGER:
98205 case TK_STRING:
98206 case TK_FLOAT:
98207 case TK_BLOB:
98208 return 0;
98209 case TK_COLUMN:
98210 return ExprHasProperty(p, EP_CanBeNull) ||
98211 p->y.pTab==0 || /* Reference to column of index on expression */
98212 (p->iColumn>=0 && p->y.pTab->aCol[p->iColumn].notNull==0);
98213 default:
98214 return 1;
98215 }
98216}
98217
98218/*
98219** Return TRUE if the given expression is a constant which would be
98220** unchanged by OP_Affinity with the affinity given in the second
98221** argument.
98222**
98223** This routine is used to determine if the OP_Affinity operation
98224** can be omitted. When in doubt return FALSE. A false negative
98225** is harmless. A false positive, however, can result in the wrong
98226** answer.
98227*/
98228SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
98229 u8 op;
98230 if( aff==SQLITE_AFF_BLOB ) return 1;
98231 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
98232 op = p->op;
98233 if( op==TK_REGISTER ) op = p->op2;
98234 switch( op ){
98235 case TK_INTEGER: {
98236 return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC;
98237 }
98238 case TK_FLOAT: {
98239 return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC;
98240 }
98241 case TK_STRING: {
98242 return aff==SQLITE_AFF_TEXT;
98243 }
98244 case TK_BLOB: {
98245 return 1;
98246 }
98247 case TK_COLUMN: {
98248 assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */
98249 return p->iColumn<0
98250 && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC);
98251 }
98252 default: {
98253 return 0;
98254 }
98255 }
98256}
98257
98258/*
98259** Return TRUE if the given string is a row-id column name.
98260*/
98261SQLITE_PRIVATE int sqlite3IsRowid(const char *z){
98262 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
98263 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
98264 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
98265 return 0;
98266}
98267#ifdef SQLITE_ENABLE_NORMALIZE
98268SQLITE_PRIVATE int sqlite3IsRowidN(const char *z, int n){
98269 if( sqlite3StrNICmp(z, "_ROWID_", n)==0 ) return 1;
98270 if( sqlite3StrNICmp(z, "ROWID", n)==0 ) return 1;
98271 if( sqlite3StrNICmp(z, "OID", n)==0 ) return 1;
98272 return 0;
98273}
98274#endif
98275
98276/*
98277** pX is the RHS of an IN operator. If pX is a SELECT statement
98278** that can be simplified to a direct table access, then return
98279** a pointer to the SELECT statement. If pX is not a SELECT statement,
98280** or if the SELECT statement needs to be manifested into a transient
98281** table, then return NULL.
98282*/
98283#ifndef SQLITE_OMIT_SUBQUERY
98284static Select *isCandidateForInOpt(Expr *pX){
98285 Select *p;
98286 SrcList *pSrc;
98287 ExprList *pEList;
98288 Table *pTab;
98289 int i;
98290 if( !ExprHasProperty(pX, EP_xIsSelect) ) return 0; /* Not a subquery */
98291 if( ExprHasProperty(pX, EP_VarSelect) ) return 0; /* Correlated subq */
98292 p = pX->x.pSelect;
98293 if( p->pPrior ) return 0; /* Not a compound SELECT */
98294 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
98295 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
98296 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
98297 return 0; /* No DISTINCT keyword and no aggregate functions */
98298 }
98299 assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */
98300 if( p->pLimit ) return 0; /* Has no LIMIT clause */
98301 if( p->pWhere ) return 0; /* Has no WHERE clause */
98302 pSrc = p->pSrc;
98303 assert( pSrc!=0 );
98304 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
98305 if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */
98306 pTab = pSrc->a[0].pTab;
98307 assert( pTab!=0 );
98308 assert( pTab->pSelect==0 ); /* FROM clause is not a view */
98309 if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */
98310 pEList = p->pEList;
98311 assert( pEList!=0 );
98312 /* All SELECT results must be columns. */
98313 for(i=0; i<pEList->nExpr; i++){
98314 Expr *pRes = pEList->a[i].pExpr;
98315 if( pRes->op!=TK_COLUMN ) return 0;
98316 assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */
98317 }
98318 return p;
98319}
98320#endif /* SQLITE_OMIT_SUBQUERY */
98321
98322#ifndef SQLITE_OMIT_SUBQUERY
98323/*
98324** Generate code that checks the left-most column of index table iCur to see if
98325** it contains any NULL entries. Cause the register at regHasNull to be set
98326** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
98327** to be set to NULL if iCur contains one or more NULL values.
98328*/
98329static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
98330 int addr1;
98331 sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull);
98332 addr1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v);
98333 sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull);
98334 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
98335 VdbeComment((v, "first_entry_in(%d)", iCur));
98336 sqlite3VdbeJumpHere(v, addr1);
98337}
98338#endif
98339
98340
98341#ifndef SQLITE_OMIT_SUBQUERY
98342/*
98343** The argument is an IN operator with a list (not a subquery) on the
98344** right-hand side. Return TRUE if that list is constant.
98345*/
98346static int sqlite3InRhsIsConstant(Expr *pIn){
98347 Expr *pLHS;
98348 int res;
98349 assert( !ExprHasProperty(pIn, EP_xIsSelect) );
98350 pLHS = pIn->pLeft;
98351 pIn->pLeft = 0;
98352 res = sqlite3ExprIsConstant(pIn);
98353 pIn->pLeft = pLHS;
98354 return res;
98355}
98356#endif
98357
98358/*
98359** This function is used by the implementation of the IN (...) operator.
98360** The pX parameter is the expression on the RHS of the IN operator, which
98361** might be either a list of expressions or a subquery.
98362**
98363** The job of this routine is to find or create a b-tree object that can
98364** be used either to test for membership in the RHS set or to iterate through
98365** all members of the RHS set, skipping duplicates.
98366**
98367** A cursor is opened on the b-tree object that is the RHS of the IN operator
98368** and pX->iTable is set to the index of that cursor.
98369**
98370** The returned value of this function indicates the b-tree type, as follows:
98371**
98372** IN_INDEX_ROWID - The cursor was opened on a database table.
98373** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
98374** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
98375** IN_INDEX_EPH - The cursor was opened on a specially created and
98376** populated epheremal table.
98377** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
98378** implemented as a sequence of comparisons.
98379**
98380** An existing b-tree might be used if the RHS expression pX is a simple
98381** subquery such as:
98382**
98383** SELECT <column1>, <column2>... FROM <table>
98384**
98385** If the RHS of the IN operator is a list or a more complex subquery, then
98386** an ephemeral table might need to be generated from the RHS and then
98387** pX->iTable made to point to the ephemeral table instead of an
98388** existing table.
98389**
98390** The inFlags parameter must contain, at a minimum, one of the bits
98391** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP but not both. If inFlags contains
98392** IN_INDEX_MEMBERSHIP, then the generated table will be used for a fast
98393** membership test. When the IN_INDEX_LOOP bit is set, the IN index will
98394** be used to loop over all values of the RHS of the IN operator.
98395**
98396** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
98397** through the set members) then the b-tree must not contain duplicates.
98398** An epheremal table will be created unless the selected columns are guaranteed
98399** to be unique - either because it is an INTEGER PRIMARY KEY or due to
98400** a UNIQUE constraint or index.
98401**
98402** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
98403** for fast set membership tests) then an epheremal table must
98404** be used unless <columns> is a single INTEGER PRIMARY KEY column or an
98405** index can be found with the specified <columns> as its left-most.
98406**
98407** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and
98408** if the RHS of the IN operator is a list (not a subquery) then this
98409** routine might decide that creating an ephemeral b-tree for membership
98410** testing is too expensive and return IN_INDEX_NOOP. In that case, the
98411** calling routine should implement the IN operator using a sequence
98412** of Eq or Ne comparison operations.
98413**
98414** When the b-tree is being used for membership tests, the calling function
98415** might need to know whether or not the RHS side of the IN operator
98416** contains a NULL. If prRhsHasNull is not a NULL pointer and
98417** if there is any chance that the (...) might contain a NULL value at
98418** runtime, then a register is allocated and the register number written
98419** to *prRhsHasNull. If there is no chance that the (...) contains a
98420** NULL value, then *prRhsHasNull is left unchanged.
98421**
98422** If a register is allocated and its location stored in *prRhsHasNull, then
98423** the value in that register will be NULL if the b-tree contains one or more
98424** NULL values, and it will be some non-NULL value if the b-tree contains no
98425** NULL values.
98426**
98427** If the aiMap parameter is not NULL, it must point to an array containing
98428** one element for each column returned by the SELECT statement on the RHS
98429** of the IN(...) operator. The i'th entry of the array is populated with the
98430** offset of the index column that matches the i'th column returned by the
98431** SELECT. For example, if the expression and selected index are:
98432**
98433** (?,?,?) IN (SELECT a, b, c FROM t1)
98434** CREATE INDEX i1 ON t1(b, c, a);
98435**
98436** then aiMap[] is populated with {2, 0, 1}.
98437*/
98438#ifndef SQLITE_OMIT_SUBQUERY
98439SQLITE_PRIVATE int sqlite3FindInIndex(
98440 Parse *pParse, /* Parsing context */
98441 Expr *pX, /* The right-hand side (RHS) of the IN operator */
98442 u32 inFlags, /* IN_INDEX_LOOP, _MEMBERSHIP, and/or _NOOP_OK */
98443 int *prRhsHasNull, /* Register holding NULL status. See notes */
98444 int *aiMap /* Mapping from Index fields to RHS fields */
98445){
98446 Select *p; /* SELECT to the right of IN operator */
98447 int eType = 0; /* Type of RHS table. IN_INDEX_* */
98448 int iTab = pParse->nTab++; /* Cursor of the RHS table */
98449 int mustBeUnique; /* True if RHS must be unique */
98450 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
98451
98452 assert( pX->op==TK_IN );
98453 mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0;
98454
98455 /* If the RHS of this IN(...) operator is a SELECT, and if it matters
98456 ** whether or not the SELECT result contains NULL values, check whether
98457 ** or not NULL is actually possible (it may not be, for example, due
98458 ** to NOT NULL constraints in the schema). If no NULL values are possible,
98459 ** set prRhsHasNull to 0 before continuing. */
98460 if( prRhsHasNull && (pX->flags & EP_xIsSelect) ){
98461 int i;
98462 ExprList *pEList = pX->x.pSelect->pEList;
98463 for(i=0; i<pEList->nExpr; i++){
98464 if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break;
98465 }
98466 if( i==pEList->nExpr ){
98467 prRhsHasNull = 0;
98468 }
98469 }
98470
98471 /* Check to see if an existing table or index can be used to
98472 ** satisfy the query. This is preferable to generating a new
98473 ** ephemeral table. */
98474 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
98475 sqlite3 *db = pParse->db; /* Database connection */
98476 Table *pTab; /* Table <table>. */
98477 i16 iDb; /* Database idx for pTab */
98478 ExprList *pEList = p->pEList;
98479 int nExpr = pEList->nExpr;
98480
98481 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
98482 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
98483 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
98484 pTab = p->pSrc->a[0].pTab;
98485
98486 /* Code an OP_Transaction and OP_TableLock for <table>. */
98487 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
98488 sqlite3CodeVerifySchema(pParse, iDb);
98489 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
98490
98491 assert(v); /* sqlite3GetVdbe() has always been previously called */
98492 if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
98493 /* The "x IN (SELECT rowid FROM table)" case */
98494 int iAddr = sqlite3VdbeAddOp0(v, OP_Once);
98495 VdbeCoverage(v);
98496
98497 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
98498 eType = IN_INDEX_ROWID;
98499 ExplainQueryPlan((pParse, 0,
98500 "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR",pTab->zName));
98501 sqlite3VdbeJumpHere(v, iAddr);
98502 }else{
98503 Index *pIdx; /* Iterator variable */
98504 int affinity_ok = 1;
98505 int i;
98506
98507 /* Check that the affinity that will be used to perform each
98508 ** comparison is the same as the affinity of each column in table
98509 ** on the RHS of the IN operator. If it not, it is not possible to
98510 ** use any index of the RHS table. */
98511 for(i=0; i<nExpr && affinity_ok; i++){
98512 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
98513 int iCol = pEList->a[i].pExpr->iColumn;
98514 char idxaff = sqlite3TableColumnAffinity(pTab,iCol); /* RHS table */
98515 char cmpaff = sqlite3CompareAffinity(pLhs, idxaff);
98516 testcase( cmpaff==SQLITE_AFF_BLOB );
98517 testcase( cmpaff==SQLITE_AFF_TEXT );
98518 switch( cmpaff ){
98519 case SQLITE_AFF_BLOB:
98520 break;
98521 case SQLITE_AFF_TEXT:
98522 /* sqlite3CompareAffinity() only returns TEXT if one side or the
98523 ** other has no affinity and the other side is TEXT. Hence,
98524 ** the only way for cmpaff to be TEXT is for idxaff to be TEXT
98525 ** and for the term on the LHS of the IN to have no affinity. */
98526 assert( idxaff==SQLITE_AFF_TEXT );
98527 break;
98528 default:
98529 affinity_ok = sqlite3IsNumericAffinity(idxaff);
98530 }
98531 }
98532
98533 if( affinity_ok ){
98534 /* Search for an existing index that will work for this IN operator */
98535 for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
98536 Bitmask colUsed; /* Columns of the index used */
98537 Bitmask mCol; /* Mask for the current column */
98538 if( pIdx->nColumn<nExpr ) continue;
98539 /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
98540 ** BITMASK(nExpr) without overflowing */
98541 testcase( pIdx->nColumn==BMS-2 );
98542 testcase( pIdx->nColumn==BMS-1 );
98543 if( pIdx->nColumn>=BMS-1 ) continue;
98544 if( mustBeUnique ){
98545 if( pIdx->nKeyCol>nExpr
98546 ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx))
98547 ){
98548 continue; /* This index is not unique over the IN RHS columns */
98549 }
98550 }
98551
98552 colUsed = 0; /* Columns of index used so far */
98553 for(i=0; i<nExpr; i++){
98554 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
98555 Expr *pRhs = pEList->a[i].pExpr;
98556 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs);
98557 int j;
98558
98559 assert( pReq!=0 || pRhs->iColumn==XN_ROWID || pParse->nErr );
98560 for(j=0; j<nExpr; j++){
98561 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
98562 assert( pIdx->azColl[j] );
98563 if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){
98564 continue;
98565 }
98566 break;
98567 }
98568 if( j==nExpr ) break;
98569 mCol = MASKBIT(j);
98570 if( mCol & colUsed ) break; /* Each column used only once */
98571 colUsed |= mCol;
98572 if( aiMap ) aiMap[i] = j;
98573 }
98574
98575 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
98576 if( colUsed==(MASKBIT(nExpr)-1) ){
98577 /* If we reach this point, that means the index pIdx is usable */
98578 int iAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
98579 ExplainQueryPlan((pParse, 0,
98580 "USING INDEX %s FOR IN-OPERATOR",pIdx->zName));
98581 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
98582 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
98583 VdbeComment((v, "%s", pIdx->zName));
98584 assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
98585 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
98586
98587 if( prRhsHasNull ){
98588#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
98589 i64 mask = (1<<nExpr)-1;
98590 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed,
98591 iTab, 0, 0, (u8*)&mask, P4_INT64);
98592#endif
98593 *prRhsHasNull = ++pParse->nMem;
98594 if( nExpr==1 ){
98595 sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull);
98596 }
98597 }
98598 sqlite3VdbeJumpHere(v, iAddr);
98599 }
98600 } /* End loop over indexes */
98601 } /* End if( affinity_ok ) */
98602 } /* End if not an rowid index */
98603 } /* End attempt to optimize using an index */
98604
98605 /* If no preexisting index is available for the IN clause
98606 ** and IN_INDEX_NOOP is an allowed reply
98607 ** and the RHS of the IN operator is a list, not a subquery
98608 ** and the RHS is not constant or has two or fewer terms,
98609 ** then it is not worth creating an ephemeral table to evaluate
98610 ** the IN operator so return IN_INDEX_NOOP.
98611 */
98612 if( eType==0
98613 && (inFlags & IN_INDEX_NOOP_OK)
98614 && !ExprHasProperty(pX, EP_xIsSelect)
98615 && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
98616 ){
98617 eType = IN_INDEX_NOOP;
98618 }
98619
98620 if( eType==0 ){
98621 /* Could not find an existing table or index to use as the RHS b-tree.
98622 ** We will have to generate an ephemeral table to do the job.
98623 */
98624 u32 savedNQueryLoop = pParse->nQueryLoop;
98625 int rMayHaveNull = 0;
98626 eType = IN_INDEX_EPH;
98627 if( inFlags & IN_INDEX_LOOP ){
98628 pParse->nQueryLoop = 0;
98629 if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){
98630 eType = IN_INDEX_ROWID;
98631 }
98632 }else if( prRhsHasNull ){
98633 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
98634 }
98635 sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
98636 pParse->nQueryLoop = savedNQueryLoop;
98637 }else{
98638 pX->iTable = iTab;
98639 }
98640
98641 if( aiMap && eType!=IN_INDEX_INDEX_ASC && eType!=IN_INDEX_INDEX_DESC ){
98642 int i, n;
98643 n = sqlite3ExprVectorSize(pX->pLeft);
98644 for(i=0; i<n; i++) aiMap[i] = i;
98645 }
98646 return eType;
98647}
98648#endif
98649
98650#ifndef SQLITE_OMIT_SUBQUERY
98651/*
98652** Argument pExpr is an (?, ?...) IN(...) expression. This
98653** function allocates and returns a nul-terminated string containing
98654** the affinities to be used for each column of the comparison.
98655**
98656** It is the responsibility of the caller to ensure that the returned
98657** string is eventually freed using sqlite3DbFree().
98658*/
98659static char *exprINAffinity(Parse *pParse, Expr *pExpr){
98660 Expr *pLeft = pExpr->pLeft;
98661 int nVal = sqlite3ExprVectorSize(pLeft);
98662 Select *pSelect = (pExpr->flags & EP_xIsSelect) ? pExpr->x.pSelect : 0;
98663 char *zRet;
98664
98665 assert( pExpr->op==TK_IN );
98666 zRet = sqlite3DbMallocRaw(pParse->db, nVal+1);
98667 if( zRet ){
98668 int i;
98669 for(i=0; i<nVal; i++){
98670 Expr *pA = sqlite3VectorFieldSubexpr(pLeft, i);
98671 char a = sqlite3ExprAffinity(pA);
98672 if( pSelect ){
98673 zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
98674 }else{
98675 zRet[i] = a;
98676 }
98677 }
98678 zRet[nVal] = '\0';
98679 }
98680 return zRet;
98681}
98682#endif
98683
98684#ifndef SQLITE_OMIT_SUBQUERY
98685/*
98686** Load the Parse object passed as the first argument with an error
98687** message of the form:
98688**
98689** "sub-select returns N columns - expected M"
98690*/
98691SQLITE_PRIVATE void sqlite3SubselectError(Parse *pParse, int nActual, int nExpect){
98692 const char *zFmt = "sub-select returns %d columns - expected %d";
98693 sqlite3ErrorMsg(pParse, zFmt, nActual, nExpect);
98694}
98695#endif
98696
98697/*
98698** Expression pExpr is a vector that has been used in a context where
98699** it is not permitted. If pExpr is a sub-select vector, this routine
98700** loads the Parse object with a message of the form:
98701**
98702** "sub-select returns N columns - expected 1"
98703**
98704** Or, if it is a regular scalar vector:
98705**
98706** "row value misused"
98707*/
98708SQLITE_PRIVATE void sqlite3VectorErrorMsg(Parse *pParse, Expr *pExpr){
98709#ifndef SQLITE_OMIT_SUBQUERY
98710 if( pExpr->flags & EP_xIsSelect ){
98711 sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1);
98712 }else
98713#endif
98714 {
98715 sqlite3ErrorMsg(pParse, "row value misused");
98716 }
98717}
98718
98719/*
98720** Generate code for scalar subqueries used as a subquery expression, EXISTS,
98721** or IN operators. Examples:
98722**
98723** (SELECT a FROM b) -- subquery
98724** EXISTS (SELECT a FROM b) -- EXISTS subquery
98725** x IN (4,5,11) -- IN operator with list on right-hand side
98726** x IN (SELECT a FROM b) -- IN operator with subquery on the right
98727**
98728** The pExpr parameter describes the expression that contains the IN
98729** operator or subquery.
98730**
98731** If parameter isRowid is non-zero, then expression pExpr is guaranteed
98732** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference
98733** to some integer key column of a table B-Tree. In this case, use an
98734** intkey B-Tree to store the set of IN(...) values instead of the usual
98735** (slower) variable length keys B-Tree.
98736**
98737** If rMayHaveNull is non-zero, that means that the operation is an IN
98738** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
98739** All this routine does is initialize the register given by rMayHaveNull
98740** to NULL. Calling routines will take care of changing this register
98741** value to non-NULL if the RHS is NULL-free.
98742**
98743** For a SELECT or EXISTS operator, return the register that holds the
98744** result. For a multi-column SELECT, the result is stored in a contiguous
98745** array of registers and the return value is the register of the left-most
98746** result column. Return 0 for IN operators or if an error occurs.
98747*/
98748#ifndef SQLITE_OMIT_SUBQUERY
98749SQLITE_PRIVATE int sqlite3CodeSubselect(
98750 Parse *pParse, /* Parsing context */
98751 Expr *pExpr, /* The IN, SELECT, or EXISTS operator */
98752 int rHasNullFlag, /* Register that records whether NULLs exist in RHS */
98753 int isRowid /* If true, LHS of IN operator is a rowid */
98754){
98755 int jmpIfDynamic = -1; /* One-time test address */
98756 int rReg = 0; /* Register storing resulting */
98757 Vdbe *v = sqlite3GetVdbe(pParse);
98758 if( NEVER(v==0) ) return 0;
98759
98760 /* The evaluation of the IN/EXISTS/SELECT must be repeated every time it
98761 ** is encountered if any of the following is true:
98762 **
98763 ** * The right-hand side is a correlated subquery
98764 ** * The right-hand side is an expression list containing variables
98765 ** * We are inside a trigger
98766 **
98767 ** If all of the above are false, then we can run this code just once
98768 ** save the results, and reuse the same result on subsequent invocations.
98769 */
98770 if( !ExprHasProperty(pExpr, EP_VarSelect) ){
98771 jmpIfDynamic = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
98772 }
98773
98774 switch( pExpr->op ){
98775 case TK_IN: {
98776 int addr; /* Address of OP_OpenEphemeral instruction */
98777 Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
98778 KeyInfo *pKeyInfo = 0; /* Key information */
98779 int nVal; /* Size of vector pLeft */
98780
98781 nVal = sqlite3ExprVectorSize(pLeft);
98782 assert( !isRowid || nVal==1 );
98783
98784 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
98785 ** expression it is handled the same way. An ephemeral table is
98786 ** filled with index keys representing the results from the
98787 ** SELECT or the <exprlist>.
98788 **
98789 ** If the 'x' expression is a column value, or the SELECT...
98790 ** statement returns a column value, then the affinity of that
98791 ** column is used to build the index keys. If both 'x' and the
98792 ** SELECT... statement are columns, then numeric affinity is used
98793 ** if either column has NUMERIC or INTEGER affinity. If neither
98794 ** 'x' nor the SELECT... statement are columns, then numeric affinity
98795 ** is used.
98796 */
98797 pExpr->iTable = pParse->nTab++;
98798 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral,
98799 pExpr->iTable, (isRowid?0:nVal));
98800 pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
98801
98802 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
98803 /* Case 1: expr IN (SELECT ...)
98804 **
98805 ** Generate code to write the results of the select into the temporary
98806 ** table allocated and opened above.
98807 */
98808 Select *pSelect = pExpr->x.pSelect;
98809 ExprList *pEList = pSelect->pEList;
98810
98811 ExplainQueryPlan((pParse, 1, "%sLIST SUBQUERY",
98812 jmpIfDynamic>=0?"":"CORRELATED "
98813 ));
98814 assert( !isRowid );
98815 /* If the LHS and RHS of the IN operator do not match, that
98816 ** error will have been caught long before we reach this point. */
98817 if( ALWAYS(pEList->nExpr==nVal) ){
98818 SelectDest dest;
98819 int i;
98820 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
98821 dest.zAffSdst = exprINAffinity(pParse, pExpr);
98822 pSelect->iLimit = 0;
98823 testcase( pSelect->selFlags & SF_Distinct );
98824 testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
98825 if( sqlite3Select(pParse, pSelect, &dest) ){
98826 sqlite3DbFree(pParse->db, dest.zAffSdst);
98827 sqlite3KeyInfoUnref(pKeyInfo);
98828 return 0;
98829 }
98830 sqlite3DbFree(pParse->db, dest.zAffSdst);
98831 assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
98832 assert( pEList!=0 );
98833 assert( pEList->nExpr>0 );
98834 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
98835 for(i=0; i<nVal; i++){
98836 Expr *p = sqlite3VectorFieldSubexpr(pLeft, i);
98837 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
98838 pParse, p, pEList->a[i].pExpr
98839 );
98840 }
98841 }
98842 }else if( ALWAYS(pExpr->x.pList!=0) ){
98843 /* Case 2: expr IN (exprlist)
98844 **
98845 ** For each expression, build an index key from the evaluation and
98846 ** store it in the temporary table. If <expr> is a column, then use
98847 ** that columns affinity when building index keys. If <expr> is not
98848 ** a column, use numeric affinity.
98849 */
98850 char affinity; /* Affinity of the LHS of the IN */
98851 int i;
98852 ExprList *pList = pExpr->x.pList;
98853 struct ExprList_item *pItem;
98854 int r1, r2, r3;
98855 affinity = sqlite3ExprAffinity(pLeft);
98856 if( !affinity ){
98857 affinity = SQLITE_AFF_BLOB;
98858 }
98859 if( pKeyInfo ){
98860 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
98861 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
98862 }
98863
98864 /* Loop through each expression in <exprlist>. */
98865 r1 = sqlite3GetTempReg(pParse);
98866 r2 = sqlite3GetTempReg(pParse);
98867 if( isRowid ) sqlite3VdbeAddOp4(v, OP_Blob, 0, r2, 0, "", P4_STATIC);
98868 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
98869 Expr *pE2 = pItem->pExpr;
98870 int iValToIns;
98871
98872 /* If the expression is not constant then we will need to
98873 ** disable the test that was generated above that makes sure
98874 ** this code only executes once. Because for a non-constant
98875 ** expression we need to rerun this code each time.
98876 */
98877 if( jmpIfDynamic>=0 && !sqlite3ExprIsConstant(pE2) ){
98878 sqlite3VdbeChangeToNoop(v, jmpIfDynamic);
98879 jmpIfDynamic = -1;
98880 }
98881
98882 /* Evaluate the expression and insert it into the temp table */
98883 if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
98884 sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
98885 }else{
98886 r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
98887 if( isRowid ){
98888 sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
98889 sqlite3VdbeCurrentAddr(v)+2);
98890 VdbeCoverage(v);
98891 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
98892 }else{
98893 sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
98894 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pExpr->iTable, r2, r3, 1);
98895 }
98896 }
98897 }
98898 sqlite3ReleaseTempReg(pParse, r1);
98899 sqlite3ReleaseTempReg(pParse, r2);
98900 }
98901 if( pKeyInfo ){
98902 sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO);
98903 }
98904 break;
98905 }
98906
98907 case TK_EXISTS:
98908 case TK_SELECT:
98909 default: {
98910 /* Case 3: (SELECT ... FROM ...)
98911 ** or: EXISTS(SELECT ... FROM ...)
98912 **
98913 ** For a SELECT, generate code to put the values for all columns of
98914 ** the first row into an array of registers and return the index of
98915 ** the first register.
98916 **
98917 ** If this is an EXISTS, write an integer 0 (not exists) or 1 (exists)
98918 ** into a register and return that register number.
98919 **
98920 ** In both cases, the query is augmented with "LIMIT 1". Any
98921 ** preexisting limit is discarded in place of the new LIMIT 1.
98922 */
98923 Select *pSel; /* SELECT statement to encode */
98924 SelectDest dest; /* How to deal with SELECT result */
98925 int nReg; /* Registers to allocate */
98926 Expr *pLimit; /* New limit expression */
98927
98928 testcase( pExpr->op==TK_EXISTS );
98929 testcase( pExpr->op==TK_SELECT );
98930 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
98931 assert( ExprHasProperty(pExpr, EP_xIsSelect) );
98932
98933 pSel = pExpr->x.pSelect;
98934 ExplainQueryPlan((pParse, 1, "%sSCALAR SUBQUERY",
98935 jmpIfDynamic>=0?"":"CORRELATED "));
98936 nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
98937 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
98938 pParse->nMem += nReg;
98939 if( pExpr->op==TK_SELECT ){
98940 dest.eDest = SRT_Mem;
98941 dest.iSdst = dest.iSDParm;
98942 dest.nSdst = nReg;
98943 sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1);
98944 VdbeComment((v, "Init subquery result"));
98945 }else{
98946 dest.eDest = SRT_Exists;
98947 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
98948 VdbeComment((v, "Init EXISTS result"));
98949 }
98950 pLimit = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[1], 0);
98951 if( pSel->pLimit ){
98952 sqlite3ExprDelete(pParse->db, pSel->pLimit->pLeft);
98953 pSel->pLimit->pLeft = pLimit;
98954 }else{
98955 pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0);
98956 }
98957 pSel->iLimit = 0;
98958 if( sqlite3Select(pParse, pSel, &dest) ){
98959 return 0;
98960 }
98961 rReg = dest.iSDParm;
98962 ExprSetVVAProperty(pExpr, EP_NoReduce);
98963 break;
98964 }
98965 }
98966
98967 if( rHasNullFlag ){
98968 sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag);
98969 }
98970
98971 if( jmpIfDynamic>=0 ){
98972 sqlite3VdbeJumpHere(v, jmpIfDynamic);
98973 }
98974
98975 return rReg;
98976}
98977#endif /* SQLITE_OMIT_SUBQUERY */
98978
98979#ifndef SQLITE_OMIT_SUBQUERY
98980/*
98981** Expr pIn is an IN(...) expression. This function checks that the
98982** sub-select on the RHS of the IN() operator has the same number of
98983** columns as the vector on the LHS. Or, if the RHS of the IN() is not
98984** a sub-query, that the LHS is a vector of size 1.
98985*/
98986SQLITE_PRIVATE int sqlite3ExprCheckIN(Parse *pParse, Expr *pIn){
98987 int nVector = sqlite3ExprVectorSize(pIn->pLeft);
98988 if( (pIn->flags & EP_xIsSelect) ){
98989 if( nVector!=pIn->x.pSelect->pEList->nExpr ){
98990 sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
98991 return 1;
98992 }
98993 }else if( nVector!=1 ){
98994 sqlite3VectorErrorMsg(pParse, pIn->pLeft);
98995 return 1;
98996 }
98997 return 0;
98998}
98999#endif
99000
99001#ifndef SQLITE_OMIT_SUBQUERY
99002/*
99003** Generate code for an IN expression.
99004**
99005** x IN (SELECT ...)
99006** x IN (value, value, ...)
99007**
99008** The left-hand side (LHS) is a scalar or vector expression. The
99009** right-hand side (RHS) is an array of zero or more scalar values, or a
99010** subquery. If the RHS is a subquery, the number of result columns must
99011** match the number of columns in the vector on the LHS. If the RHS is
99012** a list of values, the LHS must be a scalar.
99013**
99014** The IN operator is true if the LHS value is contained within the RHS.
99015** The result is false if the LHS is definitely not in the RHS. The
99016** result is NULL if the presence of the LHS in the RHS cannot be
99017** determined due to NULLs.
99018**
99019** This routine generates code that jumps to destIfFalse if the LHS is not
99020** contained within the RHS. If due to NULLs we cannot determine if the LHS
99021** is contained in the RHS then jump to destIfNull. If the LHS is contained
99022** within the RHS then fall through.
99023**
99024** See the separate in-operator.md documentation file in the canonical
99025** SQLite source tree for additional information.
99026*/
99027static void sqlite3ExprCodeIN(
99028 Parse *pParse, /* Parsing and code generating context */
99029 Expr *pExpr, /* The IN expression */
99030 int destIfFalse, /* Jump here if LHS is not contained in the RHS */
99031 int destIfNull /* Jump here if the results are unknown due to NULLs */
99032){
99033 int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */
99034 int eType; /* Type of the RHS */
99035 int rLhs; /* Register(s) holding the LHS values */
99036 int rLhsOrig; /* LHS values prior to reordering by aiMap[] */
99037 Vdbe *v; /* Statement under construction */
99038 int *aiMap = 0; /* Map from vector field to index column */
99039 char *zAff = 0; /* Affinity string for comparisons */
99040 int nVector; /* Size of vectors for this IN operator */
99041 int iDummy; /* Dummy parameter to exprCodeVector() */
99042 Expr *pLeft; /* The LHS of the IN operator */
99043 int i; /* loop counter */
99044 int destStep2; /* Where to jump when NULLs seen in step 2 */
99045 int destStep6 = 0; /* Start of code for Step 6 */
99046 int addrTruthOp; /* Address of opcode that determines the IN is true */
99047 int destNotNull; /* Jump here if a comparison is not true in step 6 */
99048 int addrTop; /* Top of the step-6 loop */
99049
99050 pLeft = pExpr->pLeft;
99051 if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
99052 zAff = exprINAffinity(pParse, pExpr);
99053 nVector = sqlite3ExprVectorSize(pExpr->pLeft);
99054 aiMap = (int*)sqlite3DbMallocZero(
99055 pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1
99056 );
99057 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
99058
99059 /* Attempt to compute the RHS. After this step, if anything other than
99060 ** IN_INDEX_NOOP is returned, the table opened ith cursor pExpr->iTable
99061 ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned,
99062 ** the RHS has not yet been coded. */
99063 v = pParse->pVdbe;
99064 assert( v!=0 ); /* OOM detected prior to this routine */
99065 VdbeNoopComment((v, "begin IN expr"));
99066 eType = sqlite3FindInIndex(pParse, pExpr,
99067 IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK,
99068 destIfFalse==destIfNull ? 0 : &rRhsHasNull, aiMap);
99069
99070 assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH
99071 || eType==IN_INDEX_INDEX_ASC || eType==IN_INDEX_INDEX_DESC
99072 );
99073#ifdef SQLITE_DEBUG
99074 /* Confirm that aiMap[] contains nVector integer values between 0 and
99075 ** nVector-1. */
99076 for(i=0; i<nVector; i++){
99077 int j, cnt;
99078 for(cnt=j=0; j<nVector; j++) if( aiMap[j]==i ) cnt++;
99079 assert( cnt==1 );
99080 }
99081#endif
99082
99083 /* Code the LHS, the <expr> from "<expr> IN (...)". If the LHS is a
99084 ** vector, then it is stored in an array of nVector registers starting
99085 ** at r1.
99086 **
99087 ** sqlite3FindInIndex() might have reordered the fields of the LHS vector
99088 ** so that the fields are in the same order as an existing index. The
99089 ** aiMap[] array contains a mapping from the original LHS field order to
99090 ** the field order that matches the RHS index.
99091 */
99092 rLhsOrig = exprCodeVector(pParse, pLeft, &iDummy);
99093 for(i=0; i<nVector && aiMap[i]==i; i++){} /* Are LHS fields reordered? */
99094 if( i==nVector ){
99095 /* LHS fields are not reordered */
99096 rLhs = rLhsOrig;
99097 }else{
99098 /* Need to reorder the LHS fields according to aiMap */
99099 rLhs = sqlite3GetTempRange(pParse, nVector);
99100 for(i=0; i<nVector; i++){
99101 sqlite3VdbeAddOp3(v, OP_Copy, rLhsOrig+i, rLhs+aiMap[i], 0);
99102 }
99103 }
99104
99105 /* If sqlite3FindInIndex() did not find or create an index that is
99106 ** suitable for evaluating the IN operator, then evaluate using a
99107 ** sequence of comparisons.
99108 **
99109 ** This is step (1) in the in-operator.md optimized algorithm.
99110 */
99111 if( eType==IN_INDEX_NOOP ){
99112 ExprList *pList = pExpr->x.pList;
99113 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
99114 int labelOk = sqlite3VdbeMakeLabel(v);
99115 int r2, regToFree;
99116 int regCkNull = 0;
99117 int ii;
99118 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
99119 if( destIfNull!=destIfFalse ){
99120 regCkNull = sqlite3GetTempReg(pParse);
99121 sqlite3VdbeAddOp3(v, OP_BitAnd, rLhs, rLhs, regCkNull);
99122 }
99123 for(ii=0; ii<pList->nExpr; ii++){
99124 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
99125 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
99126 sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull);
99127 }
99128 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
99129 sqlite3VdbeAddOp4(v, OP_Eq, rLhs, labelOk, r2,
99130 (void*)pColl, P4_COLLSEQ);
99131 VdbeCoverageIf(v, ii<pList->nExpr-1);
99132 VdbeCoverageIf(v, ii==pList->nExpr-1);
99133 sqlite3VdbeChangeP5(v, zAff[0]);
99134 }else{
99135 assert( destIfNull==destIfFalse );
99136 sqlite3VdbeAddOp4(v, OP_Ne, rLhs, destIfFalse, r2,
99137 (void*)pColl, P4_COLLSEQ); VdbeCoverage(v);
99138 sqlite3VdbeChangeP5(v, zAff[0] | SQLITE_JUMPIFNULL);
99139 }
99140 sqlite3ReleaseTempReg(pParse, regToFree);
99141 }
99142 if( regCkNull ){
99143 sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v);
99144 sqlite3VdbeGoto(v, destIfFalse);
99145 }
99146 sqlite3VdbeResolveLabel(v, labelOk);
99147 sqlite3ReleaseTempReg(pParse, regCkNull);
99148 goto sqlite3ExprCodeIN_finished;
99149 }
99150
99151 /* Step 2: Check to see if the LHS contains any NULL columns. If the
99152 ** LHS does contain NULLs then the result must be either FALSE or NULL.
99153 ** We will then skip the binary search of the RHS.
99154 */
99155 if( destIfNull==destIfFalse ){
99156 destStep2 = destIfFalse;
99157 }else{
99158 destStep2 = destStep6 = sqlite3VdbeMakeLabel(v);
99159 }
99160 for(i=0; i<nVector; i++){
99161 Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i);
99162 if( sqlite3ExprCanBeNull(p) ){
99163 sqlite3VdbeAddOp2(v, OP_IsNull, rLhs+i, destStep2);
99164 VdbeCoverage(v);
99165 }
99166 }
99167
99168 /* Step 3. The LHS is now known to be non-NULL. Do the binary search
99169 ** of the RHS using the LHS as a probe. If found, the result is
99170 ** true.
99171 */
99172 if( eType==IN_INDEX_ROWID ){
99173 /* In this case, the RHS is the ROWID of table b-tree and so we also
99174 ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4
99175 ** into a single opcode. */
99176 sqlite3VdbeAddOp3(v, OP_SeekRowid, pExpr->iTable, destIfFalse, rLhs);
99177 VdbeCoverage(v);
99178 addrTruthOp = sqlite3VdbeAddOp0(v, OP_Goto); /* Return True */
99179 }else{
99180 sqlite3VdbeAddOp4(v, OP_Affinity, rLhs, nVector, 0, zAff, nVector);
99181 if( destIfFalse==destIfNull ){
99182 /* Combine Step 3 and Step 5 into a single opcode */
99183 sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse,
99184 rLhs, nVector); VdbeCoverage(v);
99185 goto sqlite3ExprCodeIN_finished;
99186 }
99187 /* Ordinary Step 3, for the case where FALSE and NULL are distinct */
99188 addrTruthOp = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0,
99189 rLhs, nVector); VdbeCoverage(v);
99190 }
99191
99192 /* Step 4. If the RHS is known to be non-NULL and we did not find
99193 ** an match on the search above, then the result must be FALSE.
99194 */
99195 if( rRhsHasNull && nVector==1 ){
99196 sqlite3VdbeAddOp2(v, OP_NotNull, rRhsHasNull, destIfFalse);
99197 VdbeCoverage(v);
99198 }
99199
99200 /* Step 5. If we do not care about the difference between NULL and
99201 ** FALSE, then just return false.
99202 */
99203 if( destIfFalse==destIfNull ) sqlite3VdbeGoto(v, destIfFalse);
99204
99205 /* Step 6: Loop through rows of the RHS. Compare each row to the LHS.
99206 ** If any comparison is NULL, then the result is NULL. If all
99207 ** comparisons are FALSE then the final result is FALSE.
99208 **
99209 ** For a scalar LHS, it is sufficient to check just the first row
99210 ** of the RHS.
99211 */
99212 if( destStep6 ) sqlite3VdbeResolveLabel(v, destStep6);
99213 addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
99214 VdbeCoverage(v);
99215 if( nVector>1 ){
99216 destNotNull = sqlite3VdbeMakeLabel(v);
99217 }else{
99218 /* For nVector==1, combine steps 6 and 7 by immediately returning
99219 ** FALSE if the first comparison is not NULL */
99220 destNotNull = destIfFalse;
99221 }
99222 for(i=0; i<nVector; i++){
99223 Expr *p;
99224 CollSeq *pColl;
99225 int r3 = sqlite3GetTempReg(pParse);
99226 p = sqlite3VectorFieldSubexpr(pLeft, i);
99227 pColl = sqlite3ExprCollSeq(pParse, p);
99228 sqlite3VdbeAddOp3(v, OP_Column, pExpr->iTable, i, r3);
99229 sqlite3VdbeAddOp4(v, OP_Ne, rLhs+i, destNotNull, r3,
99230 (void*)pColl, P4_COLLSEQ);
99231 VdbeCoverage(v);
99232 sqlite3ReleaseTempReg(pParse, r3);
99233 }
99234 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
99235 if( nVector>1 ){
99236 sqlite3VdbeResolveLabel(v, destNotNull);
99237 sqlite3VdbeAddOp2(v, OP_Next, pExpr->iTable, addrTop+1);
99238 VdbeCoverage(v);
99239
99240 /* Step 7: If we reach this point, we know that the result must
99241 ** be false. */
99242 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
99243 }
99244
99245 /* Jumps here in order to return true. */
99246 sqlite3VdbeJumpHere(v, addrTruthOp);
99247
99248sqlite3ExprCodeIN_finished:
99249 if( rLhs!=rLhsOrig ) sqlite3ReleaseTempReg(pParse, rLhs);
99250 VdbeComment((v, "end IN expr"));
99251sqlite3ExprCodeIN_oom_error:
99252 sqlite3DbFree(pParse->db, aiMap);
99253 sqlite3DbFree(pParse->db, zAff);
99254}
99255#endif /* SQLITE_OMIT_SUBQUERY */
99256
99257#ifndef SQLITE_OMIT_FLOATING_POINT
99258/*
99259** Generate an instruction that will put the floating point
99260** value described by z[0..n-1] into register iMem.
99261**
99262** The z[] string will probably not be zero-terminated. But the
99263** z[n] character is guaranteed to be something that does not look
99264** like the continuation of the number.
99265*/
99266static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
99267 if( ALWAYS(z!=0) ){
99268 double value;
99269 sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
99270 assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
99271 if( negateFlag ) value = -value;
99272 sqlite3VdbeAddOp4Dup8(v, OP_Real, 0, iMem, 0, (u8*)&value, P4_REAL);
99273 }
99274}
99275#endif
99276
99277
99278/*
99279** Generate an instruction that will put the integer describe by
99280** text z[0..n-1] into register iMem.
99281**
99282** Expr.u.zToken is always UTF8 and zero-terminated.
99283*/
99284static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
99285 Vdbe *v = pParse->pVdbe;
99286 if( pExpr->flags & EP_IntValue ){
99287 int i = pExpr->u.iValue;
99288 assert( i>=0 );
99289 if( negFlag ) i = -i;
99290 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
99291 }else{
99292 int c;
99293 i64 value;
99294 const char *z = pExpr->u.zToken;
99295 assert( z!=0 );
99296 c = sqlite3DecOrHexToI64(z, &value);
99297 if( (c==3 && !negFlag) || (c==2) || (negFlag && value==SMALLEST_INT64)){
99298#ifdef SQLITE_OMIT_FLOATING_POINT
99299 sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
99300#else
99301#ifndef SQLITE_OMIT_HEX_INTEGER
99302 if( sqlite3_strnicmp(z,"0x",2)==0 ){
99303 sqlite3ErrorMsg(pParse, "hex literal too big: %s%s", negFlag?"-":"",z);
99304 }else
99305#endif
99306 {
99307 codeReal(v, z, negFlag, iMem);
99308 }
99309#endif
99310 }else{
99311 if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; }
99312 sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64);
99313 }
99314 }
99315}
99316
99317
99318/* Generate code that will load into register regOut a value that is
99319** appropriate for the iIdxCol-th column of index pIdx.
99320*/
99321SQLITE_PRIVATE void sqlite3ExprCodeLoadIndexColumn(
99322 Parse *pParse, /* The parsing context */
99323 Index *pIdx, /* The index whose column is to be loaded */
99324 int iTabCur, /* Cursor pointing to a table row */
99325 int iIdxCol, /* The column of the index to be loaded */
99326 int regOut /* Store the index column value in this register */
99327){
99328 i16 iTabCol = pIdx->aiColumn[iIdxCol];
99329 if( iTabCol==XN_EXPR ){
99330 assert( pIdx->aColExpr );
99331 assert( pIdx->aColExpr->nExpr>iIdxCol );
99332 pParse->iSelfTab = iTabCur + 1;
99333 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
99334 pParse->iSelfTab = 0;
99335 }else{
99336 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
99337 iTabCol, regOut);
99338 }
99339}
99340
99341/*
99342** Generate code to extract the value of the iCol-th column of a table.
99343*/
99344SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(
99345 Vdbe *v, /* The VDBE under construction */
99346 Table *pTab, /* The table containing the value */
99347 int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */
99348 int iCol, /* Index of the column to extract */
99349 int regOut /* Extract the value into this register */
99350){
99351 if( pTab==0 ){
99352 sqlite3VdbeAddOp3(v, OP_Column, iTabCur, iCol, regOut);
99353 return;
99354 }
99355 if( iCol<0 || iCol==pTab->iPKey ){
99356 sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
99357 }else{
99358 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
99359 int x = iCol;
99360 if( !HasRowid(pTab) && !IsVirtual(pTab) ){
99361 x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
99362 }
99363 sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut);
99364 }
99365 if( iCol>=0 ){
99366 sqlite3ColumnDefault(v, pTab, iCol, regOut);
99367 }
99368}
99369
99370/*
99371** Generate code that will extract the iColumn-th column from
99372** table pTab and store the column value in register iReg.
99373**
99374** There must be an open cursor to pTab in iTable when this routine
99375** is called. If iColumn<0 then code is generated that extracts the rowid.
99376*/
99377SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(
99378 Parse *pParse, /* Parsing and code generating context */
99379 Table *pTab, /* Description of the table we are reading from */
99380 int iColumn, /* Index of the table column */
99381 int iTable, /* The cursor pointing to the table */
99382 int iReg, /* Store results here */
99383 u8 p5 /* P5 value for OP_Column + FLAGS */
99384){
99385 Vdbe *v = pParse->pVdbe;
99386 assert( v!=0 );
99387 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
99388 if( p5 ){
99389 sqlite3VdbeChangeP5(v, p5);
99390 }
99391 return iReg;
99392}
99393
99394/*
99395** Generate code to move content from registers iFrom...iFrom+nReg-1
99396** over to iTo..iTo+nReg-1.
99397*/
99398SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
99399 assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo );
99400 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
99401}
99402
99403/*
99404** Convert a scalar expression node to a TK_REGISTER referencing
99405** register iReg. The caller must ensure that iReg already contains
99406** the correct value for the expression.
99407*/
99408static void exprToRegister(Expr *p, int iReg){
99409 p->op2 = p->op;
99410 p->op = TK_REGISTER;
99411 p->iTable = iReg;
99412 ExprClearProperty(p, EP_Skip);
99413}
99414
99415/*
99416** Evaluate an expression (either a vector or a scalar expression) and store
99417** the result in continguous temporary registers. Return the index of
99418** the first register used to store the result.
99419**
99420** If the returned result register is a temporary scalar, then also write
99421** that register number into *piFreeable. If the returned result register
99422** is not a temporary or if the expression is a vector set *piFreeable
99423** to 0.
99424*/
99425static int exprCodeVector(Parse *pParse, Expr *p, int *piFreeable){
99426 int iResult;
99427 int nResult = sqlite3ExprVectorSize(p);
99428 if( nResult==1 ){
99429 iResult = sqlite3ExprCodeTemp(pParse, p, piFreeable);
99430 }else{
99431 *piFreeable = 0;
99432 if( p->op==TK_SELECT ){
99433#if SQLITE_OMIT_SUBQUERY
99434 iResult = 0;
99435#else
99436 iResult = sqlite3CodeSubselect(pParse, p, 0, 0);
99437#endif
99438 }else{
99439 int i;
99440 iResult = pParse->nMem+1;
99441 pParse->nMem += nResult;
99442 for(i=0; i<nResult; i++){
99443 sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult);
99444 }
99445 }
99446 }
99447 return iResult;
99448}
99449
99450
99451/*
99452** Generate code into the current Vdbe to evaluate the given
99453** expression. Attempt to store the results in register "target".
99454** Return the register where results are stored.
99455**
99456** With this routine, there is no guarantee that results will
99457** be stored in target. The result might be stored in some other
99458** register if it is convenient to do so. The calling function
99459** must check the return code and move the results to the desired
99460** register.
99461*/
99462SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
99463 Vdbe *v = pParse->pVdbe; /* The VM under construction */
99464 int op; /* The opcode being coded */
99465 int inReg = target; /* Results stored in register inReg */
99466 int regFree1 = 0; /* If non-zero free this temporary register */
99467 int regFree2 = 0; /* If non-zero free this temporary register */
99468 int r1, r2; /* Various register numbers */
99469 Expr tempX; /* Temporary expression node */
99470 int p5 = 0;
99471
99472 assert( target>0 && target<=pParse->nMem );
99473 if( v==0 ){
99474 assert( pParse->db->mallocFailed );
99475 return 0;
99476 }
99477
99478expr_code_doover:
99479 if( pExpr==0 ){
99480 op = TK_NULL;
99481 }else{
99482 op = pExpr->op;
99483 }
99484 switch( op ){
99485 case TK_AGG_COLUMN: {
99486 AggInfo *pAggInfo = pExpr->pAggInfo;
99487 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
99488 if( !pAggInfo->directMode ){
99489 assert( pCol->iMem>0 );
99490 return pCol->iMem;
99491 }else if( pAggInfo->useSortingIdx ){
99492 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
99493 pCol->iSorterColumn, target);
99494 return target;
99495 }
99496 /* Otherwise, fall thru into the TK_COLUMN case */
99497 }
99498 case TK_COLUMN: {
99499 int iTab = pExpr->iTable;
99500 if( ExprHasProperty(pExpr, EP_FixedCol) ){
99501 /* This COLUMN expression is really a constant due to WHERE clause
99502 ** constraints, and that constant is coded by the pExpr->pLeft
99503 ** expresssion. However, make sure the constant has the correct
99504 ** datatype by applying the Affinity of the table column to the
99505 ** constant.
99506 */
99507 int iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
99508 int aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
99509 if( aff!=SQLITE_AFF_BLOB ){
99510 static const char zAff[] = "B\000C\000D\000E";
99511 assert( SQLITE_AFF_BLOB=='A' );
99512 assert( SQLITE_AFF_TEXT=='B' );
99513 if( iReg!=target ){
99514 sqlite3VdbeAddOp2(v, OP_SCopy, iReg, target);
99515 iReg = target;
99516 }
99517 sqlite3VdbeAddOp4(v, OP_Affinity, iReg, 1, 0,
99518 &zAff[(aff-'B')*2], P4_STATIC);
99519 }
99520 return iReg;
99521 }
99522 if( iTab<0 ){
99523 if( pParse->iSelfTab<0 ){
99524 /* Generating CHECK constraints or inserting into partial index */
99525 return pExpr->iColumn - pParse->iSelfTab;
99526 }else{
99527 /* Coding an expression that is part of an index where column names
99528 ** in the index refer to the table to which the index belongs */
99529 iTab = pParse->iSelfTab - 1;
99530 }
99531 }
99532 return sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
99533 pExpr->iColumn, iTab, target,
99534 pExpr->op2);
99535 }
99536 case TK_INTEGER: {
99537 codeInteger(pParse, pExpr, 0, target);
99538 return target;
99539 }
99540 case TK_TRUEFALSE: {
99541 sqlite3VdbeAddOp2(v, OP_Integer, sqlite3ExprTruthValue(pExpr), target);
99542 return target;
99543 }
99544#ifndef SQLITE_OMIT_FLOATING_POINT
99545 case TK_FLOAT: {
99546 assert( !ExprHasProperty(pExpr, EP_IntValue) );
99547 codeReal(v, pExpr->u.zToken, 0, target);
99548 return target;
99549 }
99550#endif
99551 case TK_STRING: {
99552 assert( !ExprHasProperty(pExpr, EP_IntValue) );
99553 sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
99554 return target;
99555 }
99556 case TK_NULL: {
99557 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
99558 return target;
99559 }
99560#ifndef SQLITE_OMIT_BLOB_LITERAL
99561 case TK_BLOB: {
99562 int n;
99563 const char *z;
99564 char *zBlob;
99565 assert( !ExprHasProperty(pExpr, EP_IntValue) );
99566 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
99567 assert( pExpr->u.zToken[1]=='\'' );
99568 z = &pExpr->u.zToken[2];
99569 n = sqlite3Strlen30(z) - 1;
99570 assert( z[n]=='\'' );
99571 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
99572 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
99573 return target;
99574 }
99575#endif
99576 case TK_VARIABLE: {
99577 assert( !ExprHasProperty(pExpr, EP_IntValue) );
99578 assert( pExpr->u.zToken!=0 );
99579 assert( pExpr->u.zToken[0]!=0 );
99580 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
99581 if( pExpr->u.zToken[1]!=0 ){
99582 const char *z = sqlite3VListNumToName(pParse->pVList, pExpr->iColumn);
99583 assert( pExpr->u.zToken[0]=='?' || strcmp(pExpr->u.zToken, z)==0 );
99584 pParse->pVList[0] = 0; /* Indicate VList may no longer be enlarged */
99585 sqlite3VdbeAppendP4(v, (char*)z, P4_STATIC);
99586 }
99587 return target;
99588 }
99589 case TK_REGISTER: {
99590 return pExpr->iTable;
99591 }
99592#ifndef SQLITE_OMIT_CAST
99593 case TK_CAST: {
99594 /* Expressions of the form: CAST(pLeft AS token) */
99595 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
99596 if( inReg!=target ){
99597 sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
99598 inReg = target;
99599 }
99600 sqlite3VdbeAddOp2(v, OP_Cast, target,
99601 sqlite3AffinityType(pExpr->u.zToken, 0));
99602 return inReg;
99603 }
99604#endif /* SQLITE_OMIT_CAST */
99605 case TK_IS:
99606 case TK_ISNOT:
99607 op = (op==TK_IS) ? TK_EQ : TK_NE;
99608 p5 = SQLITE_NULLEQ;
99609 /* fall-through */
99610 case TK_LT:
99611 case TK_LE:
99612 case TK_GT:
99613 case TK_GE:
99614 case TK_NE:
99615 case TK_EQ: {
99616 Expr *pLeft = pExpr->pLeft;
99617 if( sqlite3ExprIsVector(pLeft) ){
99618 codeVectorCompare(pParse, pExpr, target, op, p5);
99619 }else{
99620 r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
99621 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
99622 codeCompare(pParse, pLeft, pExpr->pRight, op,
99623 r1, r2, inReg, SQLITE_STOREP2 | p5);
99624 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
99625 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
99626 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
99627 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
99628 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
99629 assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
99630 testcase( regFree1==0 );
99631 testcase( regFree2==0 );
99632 }
99633 break;
99634 }
99635 case TK_AND:
99636 case TK_OR:
99637 case TK_PLUS:
99638 case TK_STAR:
99639 case TK_MINUS:
99640 case TK_REM:
99641 case TK_BITAND:
99642 case TK_BITOR:
99643 case TK_SLASH:
99644 case TK_LSHIFT:
99645 case TK_RSHIFT:
99646 case TK_CONCAT: {
99647 assert( TK_AND==OP_And ); testcase( op==TK_AND );
99648 assert( TK_OR==OP_Or ); testcase( op==TK_OR );
99649 assert( TK_PLUS==OP_Add ); testcase( op==TK_PLUS );
99650 assert( TK_MINUS==OP_Subtract ); testcase( op==TK_MINUS );
99651 assert( TK_REM==OP_Remainder ); testcase( op==TK_REM );
99652 assert( TK_BITAND==OP_BitAnd ); testcase( op==TK_BITAND );
99653 assert( TK_BITOR==OP_BitOr ); testcase( op==TK_BITOR );
99654 assert( TK_SLASH==OP_Divide ); testcase( op==TK_SLASH );
99655 assert( TK_LSHIFT==OP_ShiftLeft ); testcase( op==TK_LSHIFT );
99656 assert( TK_RSHIFT==OP_ShiftRight ); testcase( op==TK_RSHIFT );
99657 assert( TK_CONCAT==OP_Concat ); testcase( op==TK_CONCAT );
99658 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
99659 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
99660 sqlite3VdbeAddOp3(v, op, r2, r1, target);
99661 testcase( regFree1==0 );
99662 testcase( regFree2==0 );
99663 break;
99664 }
99665 case TK_UMINUS: {
99666 Expr *pLeft = pExpr->pLeft;
99667 assert( pLeft );
99668 if( pLeft->op==TK_INTEGER ){
99669 codeInteger(pParse, pLeft, 1, target);
99670 return target;
99671#ifndef SQLITE_OMIT_FLOATING_POINT
99672 }else if( pLeft->op==TK_FLOAT ){
99673 assert( !ExprHasProperty(pExpr, EP_IntValue) );
99674 codeReal(v, pLeft->u.zToken, 1, target);
99675 return target;
99676#endif
99677 }else{
99678 tempX.op = TK_INTEGER;
99679 tempX.flags = EP_IntValue|EP_TokenOnly;
99680 tempX.u.iValue = 0;
99681 r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
99682 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
99683 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
99684 testcase( regFree2==0 );
99685 }
99686 break;
99687 }
99688 case TK_BITNOT:
99689 case TK_NOT: {
99690 assert( TK_BITNOT==OP_BitNot ); testcase( op==TK_BITNOT );
99691 assert( TK_NOT==OP_Not ); testcase( op==TK_NOT );
99692 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
99693 testcase( regFree1==0 );
99694 sqlite3VdbeAddOp2(v, op, r1, inReg);
99695 break;
99696 }
99697 case TK_TRUTH: {
99698 int isTrue; /* IS TRUE or IS NOT TRUE */
99699 int bNormal; /* IS TRUE or IS FALSE */
99700 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
99701 testcase( regFree1==0 );
99702 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
99703 bNormal = pExpr->op2==TK_IS;
99704 testcase( isTrue && bNormal);
99705 testcase( !isTrue && bNormal);
99706 sqlite3VdbeAddOp4Int(v, OP_IsTrue, r1, inReg, !isTrue, isTrue ^ bNormal);
99707 break;
99708 }
99709 case TK_ISNULL:
99710 case TK_NOTNULL: {
99711 int addr;
99712 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
99713 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
99714 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
99715 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
99716 testcase( regFree1==0 );
99717 addr = sqlite3VdbeAddOp1(v, op, r1);
99718 VdbeCoverageIf(v, op==TK_ISNULL);
99719 VdbeCoverageIf(v, op==TK_NOTNULL);
99720 sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
99721 sqlite3VdbeJumpHere(v, addr);
99722 break;
99723 }
99724 case TK_AGG_FUNCTION: {
99725 AggInfo *pInfo = pExpr->pAggInfo;
99726 if( pInfo==0 ){
99727 assert( !ExprHasProperty(pExpr, EP_IntValue) );
99728 sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
99729 }else{
99730 return pInfo->aFunc[pExpr->iAgg].iMem;
99731 }
99732 break;
99733 }
99734 case TK_FUNCTION: {
99735 ExprList *pFarg; /* List of function arguments */
99736 int nFarg; /* Number of function arguments */
99737 FuncDef *pDef; /* The function definition object */
99738 const char *zId; /* The function name */
99739 u32 constMask = 0; /* Mask of function arguments that are constant */
99740 int i; /* Loop counter */
99741 sqlite3 *db = pParse->db; /* The database connection */
99742 u8 enc = ENC(db); /* The text encoding used by this database */
99743 CollSeq *pColl = 0; /* A collating sequence */
99744
99745#ifndef SQLITE_OMIT_WINDOWFUNC
99746 if( ExprHasProperty(pExpr, EP_WinFunc) ){
99747 return pExpr->y.pWin->regResult;
99748 }
99749#endif
99750
99751 if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){
99752 /* SQL functions can be expensive. So try to move constant functions
99753 ** out of the inner loop, even if that means an extra OP_Copy. */
99754 return sqlite3ExprCodeAtInit(pParse, pExpr, -1);
99755 }
99756 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
99757 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
99758 pFarg = 0;
99759 }else{
99760 pFarg = pExpr->x.pList;
99761 }
99762 nFarg = pFarg ? pFarg->nExpr : 0;
99763 assert( !ExprHasProperty(pExpr, EP_IntValue) );
99764 zId = pExpr->u.zToken;
99765 pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0);
99766#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
99767 if( pDef==0 && pParse->explain ){
99768 pDef = sqlite3FindFunction(db, "unknown", nFarg, enc, 0);
99769 }
99770#endif
99771 if( pDef==0 || pDef->xFinalize!=0 ){
99772 sqlite3ErrorMsg(pParse, "unknown function: %s()", zId);
99773 break;
99774 }
99775
99776 /* Attempt a direct implementation of the built-in COALESCE() and
99777 ** IFNULL() functions. This avoids unnecessary evaluation of
99778 ** arguments past the first non-NULL argument.
99779 */
99780 if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){
99781 int endCoalesce = sqlite3VdbeMakeLabel(v);
99782 assert( nFarg>=2 );
99783 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
99784 for(i=1; i<nFarg; i++){
99785 sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
99786 VdbeCoverage(v);
99787 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
99788 }
99789 sqlite3VdbeResolveLabel(v, endCoalesce);
99790 break;
99791 }
99792
99793 /* The UNLIKELY() function is a no-op. The result is the value
99794 ** of the first argument.
99795 */
99796 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
99797 assert( nFarg>=1 );
99798 return sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
99799 }
99800
99801#ifdef SQLITE_DEBUG
99802 /* The AFFINITY() function evaluates to a string that describes
99803 ** the type affinity of the argument. This is used for testing of
99804 ** the SQLite type logic.
99805 */
99806 if( pDef->funcFlags & SQLITE_FUNC_AFFINITY ){
99807 const char *azAff[] = { "blob", "text", "numeric", "integer", "real" };
99808 char aff;
99809 assert( nFarg==1 );
99810 aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
99811 sqlite3VdbeLoadString(v, target,
99812 aff ? azAff[aff-SQLITE_AFF_BLOB] : "none");
99813 return target;
99814 }
99815#endif
99816
99817 for(i=0; i<nFarg; i++){
99818 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
99819 testcase( i==31 );
99820 constMask |= MASKBIT32(i);
99821 }
99822 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
99823 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
99824 }
99825 }
99826 if( pFarg ){
99827 if( constMask ){
99828 r1 = pParse->nMem+1;
99829 pParse->nMem += nFarg;
99830 }else{
99831 r1 = sqlite3GetTempRange(pParse, nFarg);
99832 }
99833
99834 /* For length() and typeof() functions with a column argument,
99835 ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG
99836 ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data
99837 ** loading.
99838 */
99839 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
99840 u8 exprOp;
99841 assert( nFarg==1 );
99842 assert( pFarg->a[0].pExpr!=0 );
99843 exprOp = pFarg->a[0].pExpr->op;
99844 if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){
99845 assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG );
99846 assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG );
99847 testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
99848 pFarg->a[0].pExpr->op2 =
99849 pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
99850 }
99851 }
99852
99853 sqlite3ExprCodeExprList(pParse, pFarg, r1, 0,
99854 SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR);
99855 }else{
99856 r1 = 0;
99857 }
99858#ifndef SQLITE_OMIT_VIRTUALTABLE
99859 /* Possibly overload the function if the first argument is
99860 ** a virtual table column.
99861 **
99862 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
99863 ** second argument, not the first, as the argument to test to
99864 ** see if it is a column in a virtual table. This is done because
99865 ** the left operand of infix functions (the operand we want to
99866 ** control overloading) ends up as the second argument to the
99867 ** function. The expression "A glob B" is equivalent to
99868 ** "glob(B,A). We want to use the A in "A glob B" to test
99869 ** for function overloading. But we use the B term in "glob(B,A)".
99870 */
99871 if( nFarg>=2 && ExprHasProperty(pExpr, EP_InfixFunc) ){
99872 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
99873 }else if( nFarg>0 ){
99874 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
99875 }
99876#endif
99877 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
99878 if( !pColl ) pColl = db->pDfltColl;
99879 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
99880 }
99881#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
99882 if( pDef->funcFlags & SQLITE_FUNC_OFFSET ){
99883 Expr *pArg = pFarg->a[0].pExpr;
99884 if( pArg->op==TK_COLUMN ){
99885 sqlite3VdbeAddOp3(v, OP_Offset, pArg->iTable, pArg->iColumn, target);
99886 }else{
99887 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
99888 }
99889 }else
99890#endif
99891 {
99892 sqlite3VdbeAddOp4(v, pParse->iSelfTab ? OP_PureFunc0 : OP_Function0,
99893 constMask, r1, target, (char*)pDef, P4_FUNCDEF);
99894 sqlite3VdbeChangeP5(v, (u8)nFarg);
99895 }
99896 if( nFarg && constMask==0 ){
99897 sqlite3ReleaseTempRange(pParse, r1, nFarg);
99898 }
99899 return target;
99900 }
99901#ifndef SQLITE_OMIT_SUBQUERY
99902 case TK_EXISTS:
99903 case TK_SELECT: {
99904 int nCol;
99905 testcase( op==TK_EXISTS );
99906 testcase( op==TK_SELECT );
99907 if( op==TK_SELECT && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1 ){
99908 sqlite3SubselectError(pParse, nCol, 1);
99909 }else{
99910 return sqlite3CodeSubselect(pParse, pExpr, 0, 0);
99911 }
99912 break;
99913 }
99914 case TK_SELECT_COLUMN: {
99915 int n;
99916 if( pExpr->pLeft->iTable==0 ){
99917 pExpr->pLeft->iTable = sqlite3CodeSubselect(pParse, pExpr->pLeft, 0, 0);
99918 }
99919 assert( pExpr->iTable==0 || pExpr->pLeft->op==TK_SELECT );
99920 if( pExpr->iTable
99921 && pExpr->iTable!=(n = sqlite3ExprVectorSize(pExpr->pLeft))
99922 ){
99923 sqlite3ErrorMsg(pParse, "%d columns assigned %d values",
99924 pExpr->iTable, n);
99925 }
99926 return pExpr->pLeft->iTable + pExpr->iColumn;
99927 }
99928 case TK_IN: {
99929 int destIfFalse = sqlite3VdbeMakeLabel(v);
99930 int destIfNull = sqlite3VdbeMakeLabel(v);
99931 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
99932 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
99933 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
99934 sqlite3VdbeResolveLabel(v, destIfFalse);
99935 sqlite3VdbeAddOp2(v, OP_AddImm, target, 0);
99936 sqlite3VdbeResolveLabel(v, destIfNull);
99937 return target;
99938 }
99939#endif /* SQLITE_OMIT_SUBQUERY */
99940
99941
99942 /*
99943 ** x BETWEEN y AND z
99944 **
99945 ** This is equivalent to
99946 **
99947 ** x>=y AND x<=z
99948 **
99949 ** X is stored in pExpr->pLeft.
99950 ** Y is stored in pExpr->pList->a[0].pExpr.
99951 ** Z is stored in pExpr->pList->a[1].pExpr.
99952 */
99953 case TK_BETWEEN: {
99954 exprCodeBetween(pParse, pExpr, target, 0, 0);
99955 return target;
99956 }
99957 case TK_SPAN:
99958 case TK_COLLATE:
99959 case TK_UPLUS: {
99960 pExpr = pExpr->pLeft;
99961 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
99962 }
99963
99964 case TK_TRIGGER: {
99965 /* If the opcode is TK_TRIGGER, then the expression is a reference
99966 ** to a column in the new.* or old.* pseudo-tables available to
99967 ** trigger programs. In this case Expr.iTable is set to 1 for the
99968 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
99969 ** is set to the column of the pseudo-table to read, or to -1 to
99970 ** read the rowid field.
99971 **
99972 ** The expression is implemented using an OP_Param opcode. The p1
99973 ** parameter is set to 0 for an old.rowid reference, or to (i+1)
99974 ** to reference another column of the old.* pseudo-table, where
99975 ** i is the index of the column. For a new.rowid reference, p1 is
99976 ** set to (n+1), where n is the number of columns in each pseudo-table.
99977 ** For a reference to any other column in the new.* pseudo-table, p1
99978 ** is set to (n+2+i), where n and i are as defined previously. For
99979 ** example, if the table on which triggers are being fired is
99980 ** declared as:
99981 **
99982 ** CREATE TABLE t1(a, b);
99983 **
99984 ** Then p1 is interpreted as follows:
99985 **
99986 ** p1==0 -> old.rowid p1==3 -> new.rowid
99987 ** p1==1 -> old.a p1==4 -> new.a
99988 ** p1==2 -> old.b p1==5 -> new.b
99989 */
99990 Table *pTab = pExpr->y.pTab;
99991 int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
99992
99993 assert( pExpr->iTable==0 || pExpr->iTable==1 );
99994 assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
99995 assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
99996 assert( p1>=0 && p1<(pTab->nCol*2+2) );
99997
99998 sqlite3VdbeAddOp2(v, OP_Param, p1, target);
99999 VdbeComment((v, "r[%d]=%s.%s", target,
100000 (pExpr->iTable ? "new" : "old"),
100001 (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[pExpr->iColumn].zName)
100002 ));
100003
100004#ifndef SQLITE_OMIT_FLOATING_POINT
100005 /* If the column has REAL affinity, it may currently be stored as an
100006 ** integer. Use OP_RealAffinity to make sure it is really real.
100007 **
100008 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
100009 ** floating point when extracting it from the record. */
100010 if( pExpr->iColumn>=0
100011 && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL
100012 ){
100013 sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
100014 }
100015#endif
100016 break;
100017 }
100018
100019 case TK_VECTOR: {
100020 sqlite3ErrorMsg(pParse, "row value misused");
100021 break;
100022 }
100023
100024 case TK_IF_NULL_ROW: {
100025 int addrINR;
100026 addrINR = sqlite3VdbeAddOp1(v, OP_IfNullRow, pExpr->iTable);
100027 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
100028 sqlite3VdbeJumpHere(v, addrINR);
100029 sqlite3VdbeChangeP3(v, addrINR, inReg);
100030 break;
100031 }
100032
100033 /*
100034 ** Form A:
100035 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
100036 **
100037 ** Form B:
100038 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
100039 **
100040 ** Form A is can be transformed into the equivalent form B as follows:
100041 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
100042 ** WHEN x=eN THEN rN ELSE y END
100043 **
100044 ** X (if it exists) is in pExpr->pLeft.
100045 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
100046 ** odd. The Y is also optional. If the number of elements in x.pList
100047 ** is even, then Y is omitted and the "otherwise" result is NULL.
100048 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
100049 **
100050 ** The result of the expression is the Ri for the first matching Ei,
100051 ** or if there is no matching Ei, the ELSE term Y, or if there is
100052 ** no ELSE term, NULL.
100053 */
100054 default: assert( op==TK_CASE ); {
100055 int endLabel; /* GOTO label for end of CASE stmt */
100056 int nextCase; /* GOTO label for next WHEN clause */
100057 int nExpr; /* 2x number of WHEN terms */
100058 int i; /* Loop counter */
100059 ExprList *pEList; /* List of WHEN terms */
100060 struct ExprList_item *aListelem; /* Array of WHEN terms */
100061 Expr opCompare; /* The X==Ei expression */
100062 Expr *pX; /* The X expression */
100063 Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */
100064
100065 assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
100066 assert(pExpr->x.pList->nExpr > 0);
100067 pEList = pExpr->x.pList;
100068 aListelem = pEList->a;
100069 nExpr = pEList->nExpr;
100070 endLabel = sqlite3VdbeMakeLabel(v);
100071 if( (pX = pExpr->pLeft)!=0 ){
100072 tempX = *pX;
100073 testcase( pX->op==TK_COLUMN );
100074 exprToRegister(&tempX, exprCodeVector(pParse, &tempX, &regFree1));
100075 testcase( regFree1==0 );
100076 memset(&opCompare, 0, sizeof(opCompare));
100077 opCompare.op = TK_EQ;
100078 opCompare.pLeft = &tempX;
100079 pTest = &opCompare;
100080 /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
100081 ** The value in regFree1 might get SCopy-ed into the file result.
100082 ** So make sure that the regFree1 register is not reused for other
100083 ** purposes and possibly overwritten. */
100084 regFree1 = 0;
100085 }
100086 for(i=0; i<nExpr-1; i=i+2){
100087 if( pX ){
100088 assert( pTest!=0 );
100089 opCompare.pRight = aListelem[i].pExpr;
100090 }else{
100091 pTest = aListelem[i].pExpr;
100092 }
100093 nextCase = sqlite3VdbeMakeLabel(v);
100094 testcase( pTest->op==TK_COLUMN );
100095 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
100096 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
100097 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
100098 sqlite3VdbeGoto(v, endLabel);
100099 sqlite3VdbeResolveLabel(v, nextCase);
100100 }
100101 if( (nExpr&1)!=0 ){
100102 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
100103 }else{
100104 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
100105 }
100106 sqlite3VdbeResolveLabel(v, endLabel);
100107 break;
100108 }
100109#ifndef SQLITE_OMIT_TRIGGER
100110 case TK_RAISE: {
100111 assert( pExpr->affinity==OE_Rollback
100112 || pExpr->affinity==OE_Abort
100113 || pExpr->affinity==OE_Fail
100114 || pExpr->affinity==OE_Ignore
100115 );
100116 if( !pParse->pTriggerTab ){
100117 sqlite3ErrorMsg(pParse,
100118 "RAISE() may only be used within a trigger-program");
100119 return 0;
100120 }
100121 if( pExpr->affinity==OE_Abort ){
100122 sqlite3MayAbort(pParse);
100123 }
100124 assert( !ExprHasProperty(pExpr, EP_IntValue) );
100125 if( pExpr->affinity==OE_Ignore ){
100126 sqlite3VdbeAddOp4(
100127 v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
100128 VdbeCoverage(v);
100129 }else{
100130 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER,
100131 pExpr->affinity, pExpr->u.zToken, 0, 0);
100132 }
100133
100134 break;
100135 }
100136#endif
100137 }
100138 sqlite3ReleaseTempReg(pParse, regFree1);
100139 sqlite3ReleaseTempReg(pParse, regFree2);
100140 return inReg;
100141}
100142
100143/*
100144** Factor out the code of the given expression to initialization time.
100145**
100146** If regDest>=0 then the result is always stored in that register and the
100147** result is not reusable. If regDest<0 then this routine is free to
100148** store the value whereever it wants. The register where the expression
100149** is stored is returned. When regDest<0, two identical expressions will
100150** code to the same register.
100151*/
100152SQLITE_PRIVATE int sqlite3ExprCodeAtInit(
100153 Parse *pParse, /* Parsing context */
100154 Expr *pExpr, /* The expression to code when the VDBE initializes */
100155 int regDest /* Store the value in this register */
100156){
100157 ExprList *p;
100158 assert( ConstFactorOk(pParse) );
100159 p = pParse->pConstExpr;
100160 if( regDest<0 && p ){
100161 struct ExprList_item *pItem;
100162 int i;
100163 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
100164 if( pItem->reusable && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0 ){
100165 return pItem->u.iConstExprReg;
100166 }
100167 }
100168 }
100169 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
100170 p = sqlite3ExprListAppend(pParse, p, pExpr);
100171 if( p ){
100172 struct ExprList_item *pItem = &p->a[p->nExpr-1];
100173 pItem->reusable = regDest<0;
100174 if( regDest<0 ) regDest = ++pParse->nMem;
100175 pItem->u.iConstExprReg = regDest;
100176 }
100177 pParse->pConstExpr = p;
100178 return regDest;
100179}
100180
100181/*
100182** Generate code to evaluate an expression and store the results
100183** into a register. Return the register number where the results
100184** are stored.
100185**
100186** If the register is a temporary register that can be deallocated,
100187** then write its number into *pReg. If the result register is not
100188** a temporary, then set *pReg to zero.
100189**
100190** If pExpr is a constant, then this routine might generate this
100191** code to fill the register in the initialization section of the
100192** VDBE program, in order to factor it out of the evaluation loop.
100193*/
100194SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
100195 int r2;
100196 pExpr = sqlite3ExprSkipCollate(pExpr);
100197 if( ConstFactorOk(pParse)
100198 && pExpr->op!=TK_REGISTER
100199 && sqlite3ExprIsConstantNotJoin(pExpr)
100200 ){
100201 *pReg = 0;
100202 r2 = sqlite3ExprCodeAtInit(pParse, pExpr, -1);
100203 }else{
100204 int r1 = sqlite3GetTempReg(pParse);
100205 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
100206 if( r2==r1 ){
100207 *pReg = r1;
100208 }else{
100209 sqlite3ReleaseTempReg(pParse, r1);
100210 *pReg = 0;
100211 }
100212 }
100213 return r2;
100214}
100215
100216/*
100217** Generate code that will evaluate expression pExpr and store the
100218** results in register target. The results are guaranteed to appear
100219** in register target.
100220*/
100221SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
100222 int inReg;
100223
100224 assert( target>0 && target<=pParse->nMem );
100225 if( pExpr && pExpr->op==TK_REGISTER ){
100226 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target);
100227 }else{
100228 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
100229 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
100230 if( inReg!=target && pParse->pVdbe ){
100231 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
100232 }
100233 }
100234}
100235
100236/*
100237** Make a transient copy of expression pExpr and then code it using
100238** sqlite3ExprCode(). This routine works just like sqlite3ExprCode()
100239** except that the input expression is guaranteed to be unchanged.
100240*/
100241SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){
100242 sqlite3 *db = pParse->db;
100243 pExpr = sqlite3ExprDup(db, pExpr, 0);
100244 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
100245 sqlite3ExprDelete(db, pExpr);
100246}
100247
100248/*
100249** Generate code that will evaluate expression pExpr and store the
100250** results in register target. The results are guaranteed to appear
100251** in register target. If the expression is constant, then this routine
100252** might choose to code the expression at initialization time.
100253*/
100254SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
100255 if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pExpr) ){
100256 sqlite3ExprCodeAtInit(pParse, pExpr, target);
100257 }else{
100258 sqlite3ExprCode(pParse, pExpr, target);
100259 }
100260}
100261
100262/*
100263** Generate code that evaluates the given expression and puts the result
100264** in register target.
100265**
100266** Also make a copy of the expression results into another "cache" register
100267** and modify the expression so that the next time it is evaluated,
100268** the result is a copy of the cache register.
100269**
100270** This routine is used for expressions that are used multiple
100271** times. They are evaluated once and the results of the expression
100272** are reused.
100273*/
100274SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
100275 Vdbe *v = pParse->pVdbe;
100276 int iMem;
100277
100278 assert( target>0 );
100279 assert( pExpr->op!=TK_REGISTER );
100280 sqlite3ExprCode(pParse, pExpr, target);
100281 iMem = ++pParse->nMem;
100282 sqlite3VdbeAddOp2(v, OP_Copy, target, iMem);
100283 exprToRegister(pExpr, iMem);
100284}
100285
100286/*
100287** Generate code that pushes the value of every element of the given
100288** expression list into a sequence of registers beginning at target.
100289**
100290** Return the number of elements evaluated. The number returned will
100291** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF
100292** is defined.
100293**
100294** The SQLITE_ECEL_DUP flag prevents the arguments from being
100295** filled using OP_SCopy. OP_Copy must be used instead.
100296**
100297** The SQLITE_ECEL_FACTOR argument allows constant arguments to be
100298** factored out into initialization code.
100299**
100300** The SQLITE_ECEL_REF flag means that expressions in the list with
100301** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored
100302** in registers at srcReg, and so the value can be copied from there.
100303** If SQLITE_ECEL_OMITREF is also set, then the values with u.x.iOrderByCol>0
100304** are simply omitted rather than being copied from srcReg.
100305*/
100306SQLITE_PRIVATE int sqlite3ExprCodeExprList(
100307 Parse *pParse, /* Parsing context */
100308 ExprList *pList, /* The expression list to be coded */
100309 int target, /* Where to write results */
100310 int srcReg, /* Source registers if SQLITE_ECEL_REF */
100311 u8 flags /* SQLITE_ECEL_* flags */
100312){
100313 struct ExprList_item *pItem;
100314 int i, j, n;
100315 u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy;
100316 Vdbe *v = pParse->pVdbe;
100317 assert( pList!=0 );
100318 assert( target>0 );
100319 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */
100320 n = pList->nExpr;
100321 if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR;
100322 for(pItem=pList->a, i=0; i<n; i++, pItem++){
100323 Expr *pExpr = pItem->pExpr;
100324#ifdef SQLITE_ENABLE_SORTER_REFERENCES
100325 if( pItem->bSorterRef ){
100326 i--;
100327 n--;
100328 }else
100329#endif
100330 if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
100331 if( flags & SQLITE_ECEL_OMITREF ){
100332 i--;
100333 n--;
100334 }else{
100335 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
100336 }
100337 }else if( (flags & SQLITE_ECEL_FACTOR)!=0
100338 && sqlite3ExprIsConstantNotJoin(pExpr)
100339 ){
100340 sqlite3ExprCodeAtInit(pParse, pExpr, target+i);
100341 }else{
100342 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
100343 if( inReg!=target+i ){
100344 VdbeOp *pOp;
100345 if( copyOp==OP_Copy
100346 && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy
100347 && pOp->p1+pOp->p3+1==inReg
100348 && pOp->p2+pOp->p3+1==target+i
100349 ){
100350 pOp->p3++;
100351 }else{
100352 sqlite3VdbeAddOp2(v, copyOp, inReg, target+i);
100353 }
100354 }
100355 }
100356 }
100357 return n;
100358}
100359
100360/*
100361** Generate code for a BETWEEN operator.
100362**
100363** x BETWEEN y AND z
100364**
100365** The above is equivalent to
100366**
100367** x>=y AND x<=z
100368**
100369** Code it as such, taking care to do the common subexpression
100370** elimination of x.
100371**
100372** The xJumpIf parameter determines details:
100373**
100374** NULL: Store the boolean result in reg[dest]
100375** sqlite3ExprIfTrue: Jump to dest if true
100376** sqlite3ExprIfFalse: Jump to dest if false
100377**
100378** The jumpIfNull parameter is ignored if xJumpIf is NULL.
100379*/
100380static void exprCodeBetween(
100381 Parse *pParse, /* Parsing and code generating context */
100382 Expr *pExpr, /* The BETWEEN expression */
100383 int dest, /* Jump destination or storage location */
100384 void (*xJump)(Parse*,Expr*,int,int), /* Action to take */
100385 int jumpIfNull /* Take the jump if the BETWEEN is NULL */
100386){
100387 Expr exprAnd; /* The AND operator in x>=y AND x<=z */
100388 Expr compLeft; /* The x>=y term */
100389 Expr compRight; /* The x<=z term */
100390 Expr exprX; /* The x subexpression */
100391 int regFree1 = 0; /* Temporary use register */
100392
100393
100394 memset(&compLeft, 0, sizeof(Expr));
100395 memset(&compRight, 0, sizeof(Expr));
100396 memset(&exprAnd, 0, sizeof(Expr));
100397
100398 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
100399 exprX = *pExpr->pLeft;
100400 exprAnd.op = TK_AND;
100401 exprAnd.pLeft = &compLeft;
100402 exprAnd.pRight = &compRight;
100403 compLeft.op = TK_GE;
100404 compLeft.pLeft = &exprX;
100405 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
100406 compRight.op = TK_LE;
100407 compRight.pLeft = &exprX;
100408 compRight.pRight = pExpr->x.pList->a[1].pExpr;
100409 exprToRegister(&exprX, exprCodeVector(pParse, &exprX, &regFree1));
100410 if( xJump ){
100411 xJump(pParse, &exprAnd, dest, jumpIfNull);
100412 }else{
100413 /* Mark the expression is being from the ON or USING clause of a join
100414 ** so that the sqlite3ExprCodeTarget() routine will not attempt to move
100415 ** it into the Parse.pConstExpr list. We should use a new bit for this,
100416 ** for clarity, but we are out of bits in the Expr.flags field so we
100417 ** have to reuse the EP_FromJoin bit. Bummer. */
100418 exprX.flags |= EP_FromJoin;
100419 sqlite3ExprCodeTarget(pParse, &exprAnd, dest);
100420 }
100421 sqlite3ReleaseTempReg(pParse, regFree1);
100422
100423 /* Ensure adequate test coverage */
100424 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1==0 );
100425 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1!=0 );
100426 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1==0 );
100427 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1!=0 );
100428 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1==0 );
100429 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1!=0 );
100430 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1==0 );
100431 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1!=0 );
100432 testcase( xJump==0 );
100433}
100434
100435/*
100436** Generate code for a boolean expression such that a jump is made
100437** to the label "dest" if the expression is true but execution
100438** continues straight thru if the expression is false.
100439**
100440** If the expression evaluates to NULL (neither true nor false), then
100441** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
100442**
100443** This code depends on the fact that certain token values (ex: TK_EQ)
100444** are the same as opcode values (ex: OP_Eq) that implement the corresponding
100445** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
100446** the make process cause these values to align. Assert()s in the code
100447** below verify that the numbers are aligned correctly.
100448*/
100449SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
100450 Vdbe *v = pParse->pVdbe;
100451 int op = 0;
100452 int regFree1 = 0;
100453 int regFree2 = 0;
100454 int r1, r2;
100455
100456 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
100457 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
100458 if( NEVER(pExpr==0) ) return; /* No way this can happen */
100459 op = pExpr->op;
100460 switch( op ){
100461 case TK_AND: {
100462 int d2 = sqlite3VdbeMakeLabel(v);
100463 testcase( jumpIfNull==0 );
100464 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
100465 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
100466 sqlite3VdbeResolveLabel(v, d2);
100467 break;
100468 }
100469 case TK_OR: {
100470 testcase( jumpIfNull==0 );
100471 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
100472 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
100473 break;
100474 }
100475 case TK_NOT: {
100476 testcase( jumpIfNull==0 );
100477 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
100478 break;
100479 }
100480 case TK_TRUTH: {
100481 int isNot; /* IS NOT TRUE or IS NOT FALSE */
100482 int isTrue; /* IS TRUE or IS NOT TRUE */
100483 testcase( jumpIfNull==0 );
100484 isNot = pExpr->op2==TK_ISNOT;
100485 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
100486 testcase( isTrue && isNot );
100487 testcase( !isTrue && isNot );
100488 if( isTrue ^ isNot ){
100489 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
100490 isNot ? SQLITE_JUMPIFNULL : 0);
100491 }else{
100492 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
100493 isNot ? SQLITE_JUMPIFNULL : 0);
100494 }
100495 break;
100496 }
100497 case TK_IS:
100498 case TK_ISNOT:
100499 testcase( op==TK_IS );
100500 testcase( op==TK_ISNOT );
100501 op = (op==TK_IS) ? TK_EQ : TK_NE;
100502 jumpIfNull = SQLITE_NULLEQ;
100503 /* Fall thru */
100504 case TK_LT:
100505 case TK_LE:
100506 case TK_GT:
100507 case TK_GE:
100508 case TK_NE:
100509 case TK_EQ: {
100510 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
100511 testcase( jumpIfNull==0 );
100512 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
100513 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
100514 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
100515 r1, r2, dest, jumpIfNull);
100516 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
100517 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
100518 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
100519 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
100520 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq);
100521 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
100522 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
100523 assert(TK_NE==OP_Ne); testcase(op==OP_Ne);
100524 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
100525 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
100526 testcase( regFree1==0 );
100527 testcase( regFree2==0 );
100528 break;
100529 }
100530 case TK_ISNULL:
100531 case TK_NOTNULL: {
100532 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
100533 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
100534 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
100535 sqlite3VdbeAddOp2(v, op, r1, dest);
100536 VdbeCoverageIf(v, op==TK_ISNULL);
100537 VdbeCoverageIf(v, op==TK_NOTNULL);
100538 testcase( regFree1==0 );
100539 break;
100540 }
100541 case TK_BETWEEN: {
100542 testcase( jumpIfNull==0 );
100543 exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfTrue, jumpIfNull);
100544 break;
100545 }
100546#ifndef SQLITE_OMIT_SUBQUERY
100547 case TK_IN: {
100548 int destIfFalse = sqlite3VdbeMakeLabel(v);
100549 int destIfNull = jumpIfNull ? dest : destIfFalse;
100550 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
100551 sqlite3VdbeGoto(v, dest);
100552 sqlite3VdbeResolveLabel(v, destIfFalse);
100553 break;
100554 }
100555#endif
100556 default: {
100557 default_expr:
100558 if( exprAlwaysTrue(pExpr) ){
100559 sqlite3VdbeGoto(v, dest);
100560 }else if( exprAlwaysFalse(pExpr) ){
100561 /* No-op */
100562 }else{
100563 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
100564 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
100565 VdbeCoverage(v);
100566 testcase( regFree1==0 );
100567 testcase( jumpIfNull==0 );
100568 }
100569 break;
100570 }
100571 }
100572 sqlite3ReleaseTempReg(pParse, regFree1);
100573 sqlite3ReleaseTempReg(pParse, regFree2);
100574}
100575
100576/*
100577** Generate code for a boolean expression such that a jump is made
100578** to the label "dest" if the expression is false but execution
100579** continues straight thru if the expression is true.
100580**
100581** If the expression evaluates to NULL (neither true nor false) then
100582** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
100583** is 0.
100584*/
100585SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
100586 Vdbe *v = pParse->pVdbe;
100587 int op = 0;
100588 int regFree1 = 0;
100589 int regFree2 = 0;
100590 int r1, r2;
100591
100592 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
100593 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
100594 if( pExpr==0 ) return;
100595
100596 /* The value of pExpr->op and op are related as follows:
100597 **
100598 ** pExpr->op op
100599 ** --------- ----------
100600 ** TK_ISNULL OP_NotNull
100601 ** TK_NOTNULL OP_IsNull
100602 ** TK_NE OP_Eq
100603 ** TK_EQ OP_Ne
100604 ** TK_GT OP_Le
100605 ** TK_LE OP_Gt
100606 ** TK_GE OP_Lt
100607 ** TK_LT OP_Ge
100608 **
100609 ** For other values of pExpr->op, op is undefined and unused.
100610 ** The value of TK_ and OP_ constants are arranged such that we
100611 ** can compute the mapping above using the following expression.
100612 ** Assert()s verify that the computation is correct.
100613 */
100614 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
100615
100616 /* Verify correct alignment of TK_ and OP_ constants
100617 */
100618 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
100619 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
100620 assert( pExpr->op!=TK_NE || op==OP_Eq );
100621 assert( pExpr->op!=TK_EQ || op==OP_Ne );
100622 assert( pExpr->op!=TK_LT || op==OP_Ge );
100623 assert( pExpr->op!=TK_LE || op==OP_Gt );
100624 assert( pExpr->op!=TK_GT || op==OP_Le );
100625 assert( pExpr->op!=TK_GE || op==OP_Lt );
100626
100627 switch( pExpr->op ){
100628 case TK_AND: {
100629 testcase( jumpIfNull==0 );
100630 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
100631 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
100632 break;
100633 }
100634 case TK_OR: {
100635 int d2 = sqlite3VdbeMakeLabel(v);
100636 testcase( jumpIfNull==0 );
100637 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
100638 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
100639 sqlite3VdbeResolveLabel(v, d2);
100640 break;
100641 }
100642 case TK_NOT: {
100643 testcase( jumpIfNull==0 );
100644 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
100645 break;
100646 }
100647 case TK_TRUTH: {
100648 int isNot; /* IS NOT TRUE or IS NOT FALSE */
100649 int isTrue; /* IS TRUE or IS NOT TRUE */
100650 testcase( jumpIfNull==0 );
100651 isNot = pExpr->op2==TK_ISNOT;
100652 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
100653 testcase( isTrue && isNot );
100654 testcase( !isTrue && isNot );
100655 if( isTrue ^ isNot ){
100656 /* IS TRUE and IS NOT FALSE */
100657 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
100658 isNot ? 0 : SQLITE_JUMPIFNULL);
100659
100660 }else{
100661 /* IS FALSE and IS NOT TRUE */
100662 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
100663 isNot ? 0 : SQLITE_JUMPIFNULL);
100664 }
100665 break;
100666 }
100667 case TK_IS:
100668 case TK_ISNOT:
100669 testcase( pExpr->op==TK_IS );
100670 testcase( pExpr->op==TK_ISNOT );
100671 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
100672 jumpIfNull = SQLITE_NULLEQ;
100673 /* Fall thru */
100674 case TK_LT:
100675 case TK_LE:
100676 case TK_GT:
100677 case TK_GE:
100678 case TK_NE:
100679 case TK_EQ: {
100680 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
100681 testcase( jumpIfNull==0 );
100682 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
100683 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
100684 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
100685 r1, r2, dest, jumpIfNull);
100686 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
100687 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
100688 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
100689 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
100690 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq);
100691 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
100692 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
100693 assert(TK_NE==OP_Ne); testcase(op==OP_Ne);
100694 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
100695 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
100696 testcase( regFree1==0 );
100697 testcase( regFree2==0 );
100698 break;
100699 }
100700 case TK_ISNULL:
100701 case TK_NOTNULL: {
100702 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
100703 sqlite3VdbeAddOp2(v, op, r1, dest);
100704 testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL);
100705 testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL);
100706 testcase( regFree1==0 );
100707 break;
100708 }
100709 case TK_BETWEEN: {
100710 testcase( jumpIfNull==0 );
100711 exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfFalse, jumpIfNull);
100712 break;
100713 }
100714#ifndef SQLITE_OMIT_SUBQUERY
100715 case TK_IN: {
100716 if( jumpIfNull ){
100717 sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
100718 }else{
100719 int destIfNull = sqlite3VdbeMakeLabel(v);
100720 sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
100721 sqlite3VdbeResolveLabel(v, destIfNull);
100722 }
100723 break;
100724 }
100725#endif
100726 default: {
100727 default_expr:
100728 if( exprAlwaysFalse(pExpr) ){
100729 sqlite3VdbeGoto(v, dest);
100730 }else if( exprAlwaysTrue(pExpr) ){
100731 /* no-op */
100732 }else{
100733 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
100734 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
100735 VdbeCoverage(v);
100736 testcase( regFree1==0 );
100737 testcase( jumpIfNull==0 );
100738 }
100739 break;
100740 }
100741 }
100742 sqlite3ReleaseTempReg(pParse, regFree1);
100743 sqlite3ReleaseTempReg(pParse, regFree2);
100744}
100745
100746/*
100747** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before
100748** code generation, and that copy is deleted after code generation. This
100749** ensures that the original pExpr is unchanged.
100750*/
100751SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){
100752 sqlite3 *db = pParse->db;
100753 Expr *pCopy = sqlite3ExprDup(db, pExpr, 0);
100754 if( db->mallocFailed==0 ){
100755 sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull);
100756 }
100757 sqlite3ExprDelete(db, pCopy);
100758}
100759
100760/*
100761** Expression pVar is guaranteed to be an SQL variable. pExpr may be any
100762** type of expression.
100763**
100764** If pExpr is a simple SQL value - an integer, real, string, blob
100765** or NULL value - then the VDBE currently being prepared is configured
100766** to re-prepare each time a new value is bound to variable pVar.
100767**
100768** Additionally, if pExpr is a simple SQL value and the value is the
100769** same as that currently bound to variable pVar, non-zero is returned.
100770** Otherwise, if the values are not the same or if pExpr is not a simple
100771** SQL value, zero is returned.
100772*/
100773static int exprCompareVariable(Parse *pParse, Expr *pVar, Expr *pExpr){
100774 int res = 0;
100775 int iVar;
100776 sqlite3_value *pL, *pR = 0;
100777
100778 sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, &pR);
100779 if( pR ){
100780 iVar = pVar->iColumn;
100781 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
100782 pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB);
100783 if( pL ){
100784 if( sqlite3_value_type(pL)==SQLITE_TEXT ){
100785 sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */
100786 }
100787 res = 0==sqlite3MemCompare(pL, pR, 0);
100788 }
100789 sqlite3ValueFree(pR);
100790 sqlite3ValueFree(pL);
100791 }
100792
100793 return res;
100794}
100795
100796/*
100797** Do a deep comparison of two expression trees. Return 0 if the two
100798** expressions are completely identical. Return 1 if they differ only
100799** by a COLLATE operator at the top level. Return 2 if there are differences
100800** other than the top-level COLLATE operator.
100801**
100802** If any subelement of pB has Expr.iTable==(-1) then it is allowed
100803** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
100804**
100805** The pA side might be using TK_REGISTER. If that is the case and pB is
100806** not using TK_REGISTER but is otherwise equivalent, then still return 0.
100807**
100808** Sometimes this routine will return 2 even if the two expressions
100809** really are equivalent. If we cannot prove that the expressions are
100810** identical, we return 2 just to be safe. So if this routine
100811** returns 2, then you do not really know for certain if the two
100812** expressions are the same. But if you get a 0 or 1 return, then you
100813** can be sure the expressions are the same. In the places where
100814** this routine is used, it does not hurt to get an extra 2 - that
100815** just might result in some slightly slower code. But returning
100816** an incorrect 0 or 1 could lead to a malfunction.
100817**
100818** If pParse is not NULL then TK_VARIABLE terms in pA with bindings in
100819** pParse->pReprepare can be matched against literals in pB. The
100820** pParse->pVdbe->expmask bitmask is updated for each variable referenced.
100821** If pParse is NULL (the normal case) then any TK_VARIABLE term in
100822** Argument pParse should normally be NULL. If it is not NULL and pA or
100823** pB causes a return value of 2.
100824*/
100825SQLITE_PRIVATE int sqlite3ExprCompare(Parse *pParse, Expr *pA, Expr *pB, int iTab){
100826 u32 combinedFlags;
100827 if( pA==0 || pB==0 ){
100828 return pB==pA ? 0 : 2;
100829 }
100830 if( pParse && pA->op==TK_VARIABLE && exprCompareVariable(pParse, pA, pB) ){
100831 return 0;
100832 }
100833 combinedFlags = pA->flags | pB->flags;
100834 if( combinedFlags & EP_IntValue ){
100835 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
100836 return 0;
100837 }
100838 return 2;
100839 }
100840 if( pA->op!=pB->op ){
100841 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){
100842 return 1;
100843 }
100844 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
100845 return 1;
100846 }
100847 return 2;
100848 }
100849 if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){
100850 if( pA->op==TK_FUNCTION ){
100851 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
100852#ifndef SQLITE_OMIT_WINDOWFUNC
100853 /* Justification for the assert():
100854 ** window functions have p->op==TK_FUNCTION but aggregate functions
100855 ** have p->op==TK_AGG_FUNCTION. So any comparison between an aggregate
100856 ** function and a window function should have failed before reaching
100857 ** this point. And, it is not possible to have a window function and
100858 ** a scalar function with the same name and number of arguments. So
100859 ** if we reach this point, either A and B both window functions or
100860 ** neither are a window functions. */
100861 assert( ExprHasProperty(pA,EP_WinFunc)==ExprHasProperty(pB,EP_WinFunc) );
100862 if( ExprHasProperty(pA,EP_WinFunc) ){
100863 if( sqlite3WindowCompare(pParse,pA->y.pWin,pB->y.pWin)!=0 ) return 2;
100864 }
100865#endif
100866 }else if( pA->op==TK_COLLATE ){
100867 if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
100868 }else if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
100869 return 2;
100870 }
100871 }
100872 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
100873 if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
100874 if( combinedFlags & EP_xIsSelect ) return 2;
100875 if( (combinedFlags & EP_FixedCol)==0
100876 && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
100877 if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
100878 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
100879 assert( (combinedFlags & EP_Reduced)==0 );
100880 if( pA->op!=TK_STRING && pA->op!=TK_TRUEFALSE ){
100881 if( pA->iColumn!=pB->iColumn ) return 2;
100882 if( pA->iTable!=pB->iTable
100883 && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
100884 }
100885 }
100886 return 0;
100887}
100888
100889/*
100890** Compare two ExprList objects. Return 0 if they are identical and
100891** non-zero if they differ in any way.
100892**
100893** If any subelement of pB has Expr.iTable==(-1) then it is allowed
100894** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
100895**
100896** This routine might return non-zero for equivalent ExprLists. The
100897** only consequence will be disabled optimizations. But this routine
100898** must never return 0 if the two ExprList objects are different, or
100899** a malfunction will result.
100900**
100901** Two NULL pointers are considered to be the same. But a NULL pointer
100902** always differs from a non-NULL pointer.
100903*/
100904SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){
100905 int i;
100906 if( pA==0 && pB==0 ) return 0;
100907 if( pA==0 || pB==0 ) return 1;
100908 if( pA->nExpr!=pB->nExpr ) return 1;
100909 for(i=0; i<pA->nExpr; i++){
100910 Expr *pExprA = pA->a[i].pExpr;
100911 Expr *pExprB = pB->a[i].pExpr;
100912 if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
100913 if( sqlite3ExprCompare(0, pExprA, pExprB, iTab) ) return 1;
100914 }
100915 return 0;
100916}
100917
100918/*
100919** Like sqlite3ExprCompare() except COLLATE operators at the top-level
100920** are ignored.
100921*/
100922SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr *pA, Expr *pB, int iTab){
100923 return sqlite3ExprCompare(0,
100924 sqlite3ExprSkipCollate(pA),
100925 sqlite3ExprSkipCollate(pB),
100926 iTab);
100927}
100928
100929/*
100930** Return true if we can prove the pE2 will always be true if pE1 is
100931** true. Return false if we cannot complete the proof or if pE2 might
100932** be false. Examples:
100933**
100934** pE1: x==5 pE2: x==5 Result: true
100935** pE1: x>0 pE2: x==5 Result: false
100936** pE1: x=21 pE2: x=21 OR y=43 Result: true
100937** pE1: x!=123 pE2: x IS NOT NULL Result: true
100938** pE1: x!=?1 pE2: x IS NOT NULL Result: true
100939** pE1: x IS NULL pE2: x IS NOT NULL Result: false
100940** pE1: x IS ?2 pE2: x IS NOT NULL Reuslt: false
100941**
100942** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has
100943** Expr.iTable<0 then assume a table number given by iTab.
100944**
100945** If pParse is not NULL, then the values of bound variables in pE1 are
100946** compared against literal values in pE2 and pParse->pVdbe->expmask is
100947** modified to record which bound variables are referenced. If pParse
100948** is NULL, then false will be returned if pE1 contains any bound variables.
100949**
100950** When in doubt, return false. Returning true might give a performance
100951** improvement. Returning false might cause a performance reduction, but
100952** it will always give the correct answer and is hence always safe.
100953*/
100954SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Parse *pParse, Expr *pE1, Expr *pE2, int iTab){
100955 if( sqlite3ExprCompare(pParse, pE1, pE2, iTab)==0 ){
100956 return 1;
100957 }
100958 if( pE2->op==TK_OR
100959 && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab)
100960 || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) )
100961 ){
100962 return 1;
100963 }
100964 if( pE2->op==TK_NOTNULL && pE1->op!=TK_ISNULL && pE1->op!=TK_IS ){
100965 Expr *pX = sqlite3ExprSkipCollate(pE1->pLeft);
100966 testcase( pX!=pE1->pLeft );
100967 if( sqlite3ExprCompare(pParse, pX, pE2->pLeft, iTab)==0 ) return 1;
100968 }
100969 return 0;
100970}
100971
100972/*
100973** This is the Expr node callback for sqlite3ExprImpliesNotNullRow().
100974** If the expression node requires that the table at pWalker->iCur
100975** have one or more non-NULL column, then set pWalker->eCode to 1 and abort.
100976**
100977** This routine controls an optimization. False positives (setting
100978** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives
100979** (never setting pWalker->eCode) is a harmless missed optimization.
100980*/
100981static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){
100982 testcase( pExpr->op==TK_AGG_COLUMN );
100983 testcase( pExpr->op==TK_AGG_FUNCTION );
100984 if( ExprHasProperty(pExpr, EP_FromJoin) ) return WRC_Prune;
100985 switch( pExpr->op ){
100986 case TK_ISNOT:
100987 case TK_NOT:
100988 case TK_ISNULL:
100989 case TK_IS:
100990 case TK_OR:
100991 case TK_CASE:
100992 case TK_IN:
100993 case TK_FUNCTION:
100994 testcase( pExpr->op==TK_ISNOT );
100995 testcase( pExpr->op==TK_NOT );
100996 testcase( pExpr->op==TK_ISNULL );
100997 testcase( pExpr->op==TK_IS );
100998 testcase( pExpr->op==TK_OR );
100999 testcase( pExpr->op==TK_CASE );
101000 testcase( pExpr->op==TK_IN );
101001 testcase( pExpr->op==TK_FUNCTION );
101002 return WRC_Prune;
101003 case TK_COLUMN:
101004 if( pWalker->u.iCur==pExpr->iTable ){
101005 pWalker->eCode = 1;
101006 return WRC_Abort;
101007 }
101008 return WRC_Prune;
101009
101010 /* Virtual tables are allowed to use constraints like x=NULL. So
101011 ** a term of the form x=y does not prove that y is not null if x
101012 ** is the column of a virtual table */
101013 case TK_EQ:
101014 case TK_NE:
101015 case TK_LT:
101016 case TK_LE:
101017 case TK_GT:
101018 case TK_GE:
101019 testcase( pExpr->op==TK_EQ );
101020 testcase( pExpr->op==TK_NE );
101021 testcase( pExpr->op==TK_LT );
101022 testcase( pExpr->op==TK_LE );
101023 testcase( pExpr->op==TK_GT );
101024 testcase( pExpr->op==TK_GE );
101025 if( (pExpr->pLeft->op==TK_COLUMN && IsVirtual(pExpr->pLeft->y.pTab))
101026 || (pExpr->pRight->op==TK_COLUMN && IsVirtual(pExpr->pRight->y.pTab))
101027 ){
101028 return WRC_Prune;
101029 }
101030 default:
101031 return WRC_Continue;
101032 }
101033}
101034
101035/*
101036** Return true (non-zero) if expression p can only be true if at least
101037** one column of table iTab is non-null. In other words, return true
101038** if expression p will always be NULL or false if every column of iTab
101039** is NULL.
101040**
101041** False negatives are acceptable. In other words, it is ok to return
101042** zero even if expression p will never be true of every column of iTab
101043** is NULL. A false negative is merely a missed optimization opportunity.
101044**
101045** False positives are not allowed, however. A false positive may result
101046** in an incorrect answer.
101047**
101048** Terms of p that are marked with EP_FromJoin (and hence that come from
101049** the ON or USING clauses of LEFT JOINS) are excluded from the analysis.
101050**
101051** This routine is used to check if a LEFT JOIN can be converted into
101052** an ordinary JOIN. The p argument is the WHERE clause. If the WHERE
101053** clause requires that some column of the right table of the LEFT JOIN
101054** be non-NULL, then the LEFT JOIN can be safely converted into an
101055** ordinary join.
101056*/
101057SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr *p, int iTab){
101058 Walker w;
101059 w.xExprCallback = impliesNotNullRow;
101060 w.xSelectCallback = 0;
101061 w.xSelectCallback2 = 0;
101062 w.eCode = 0;
101063 w.u.iCur = iTab;
101064 sqlite3WalkExpr(&w, p);
101065 return w.eCode;
101066}
101067
101068/*
101069** An instance of the following structure is used by the tree walker
101070** to determine if an expression can be evaluated by reference to the
101071** index only, without having to do a search for the corresponding
101072** table entry. The IdxCover.pIdx field is the index. IdxCover.iCur
101073** is the cursor for the table.
101074*/
101075struct IdxCover {
101076 Index *pIdx; /* The index to be tested for coverage */
101077 int iCur; /* Cursor number for the table corresponding to the index */
101078};
101079
101080/*
101081** Check to see if there are references to columns in table
101082** pWalker->u.pIdxCover->iCur can be satisfied using the index
101083** pWalker->u.pIdxCover->pIdx.
101084*/
101085static int exprIdxCover(Walker *pWalker, Expr *pExpr){
101086 if( pExpr->op==TK_COLUMN
101087 && pExpr->iTable==pWalker->u.pIdxCover->iCur
101088 && sqlite3ColumnOfIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
101089 ){
101090 pWalker->eCode = 1;
101091 return WRC_Abort;
101092 }
101093 return WRC_Continue;
101094}
101095
101096/*
101097** Determine if an index pIdx on table with cursor iCur contains will
101098** the expression pExpr. Return true if the index does cover the
101099** expression and false if the pExpr expression references table columns
101100** that are not found in the index pIdx.
101101**
101102** An index covering an expression means that the expression can be
101103** evaluated using only the index and without having to lookup the
101104** corresponding table entry.
101105*/
101106SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(
101107 Expr *pExpr, /* The index to be tested */
101108 int iCur, /* The cursor number for the corresponding table */
101109 Index *pIdx /* The index that might be used for coverage */
101110){
101111 Walker w;
101112 struct IdxCover xcov;
101113 memset(&w, 0, sizeof(w));
101114 xcov.iCur = iCur;
101115 xcov.pIdx = pIdx;
101116 w.xExprCallback = exprIdxCover;
101117 w.u.pIdxCover = &xcov;
101118 sqlite3WalkExpr(&w, pExpr);
101119 return !w.eCode;
101120}
101121
101122
101123/*
101124** An instance of the following structure is used by the tree walker
101125** to count references to table columns in the arguments of an
101126** aggregate function, in order to implement the
101127** sqlite3FunctionThisSrc() routine.
101128*/
101129struct SrcCount {
101130 SrcList *pSrc; /* One particular FROM clause in a nested query */
101131 int nThis; /* Number of references to columns in pSrcList */
101132 int nOther; /* Number of references to columns in other FROM clauses */
101133};
101134
101135/*
101136** Count the number of references to columns.
101137*/
101138static int exprSrcCount(Walker *pWalker, Expr *pExpr){
101139 /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc()
101140 ** is always called before sqlite3ExprAnalyzeAggregates() and so the
101141 ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN. If
101142 ** sqlite3FunctionUsesThisSrc() is used differently in the future, the
101143 ** NEVER() will need to be removed. */
101144 if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){
101145 int i;
101146 struct SrcCount *p = pWalker->u.pSrcCount;
101147 SrcList *pSrc = p->pSrc;
101148 int nSrc = pSrc ? pSrc->nSrc : 0;
101149 for(i=0; i<nSrc; i++){
101150 if( pExpr->iTable==pSrc->a[i].iCursor ) break;
101151 }
101152 if( i<nSrc ){
101153 p->nThis++;
101154 }else{
101155 p->nOther++;
101156 }
101157 }
101158 return WRC_Continue;
101159}
101160
101161/*
101162** Determine if any of the arguments to the pExpr Function reference
101163** pSrcList. Return true if they do. Also return true if the function
101164** has no arguments or has only constant arguments. Return false if pExpr
101165** references columns but not columns of tables found in pSrcList.
101166*/
101167SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){
101168 Walker w;
101169 struct SrcCount cnt;
101170 assert( pExpr->op==TK_AGG_FUNCTION );
101171 w.xExprCallback = exprSrcCount;
101172 w.xSelectCallback = 0;
101173 w.u.pSrcCount = &cnt;
101174 cnt.pSrc = pSrcList;
101175 cnt.nThis = 0;
101176 cnt.nOther = 0;
101177 sqlite3WalkExprList(&w, pExpr->x.pList);
101178 return cnt.nThis>0 || cnt.nOther==0;
101179}
101180
101181/*
101182** Add a new element to the pAggInfo->aCol[] array. Return the index of
101183** the new element. Return a negative number if malloc fails.
101184*/
101185static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
101186 int i;
101187 pInfo->aCol = sqlite3ArrayAllocate(
101188 db,
101189 pInfo->aCol,
101190 sizeof(pInfo->aCol[0]),
101191 &pInfo->nColumn,
101192 &i
101193 );
101194 return i;
101195}
101196
101197/*
101198** Add a new element to the pAggInfo->aFunc[] array. Return the index of
101199** the new element. Return a negative number if malloc fails.
101200*/
101201static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
101202 int i;
101203 pInfo->aFunc = sqlite3ArrayAllocate(
101204 db,
101205 pInfo->aFunc,
101206 sizeof(pInfo->aFunc[0]),
101207 &pInfo->nFunc,
101208 &i
101209 );
101210 return i;
101211}
101212
101213/*
101214** This is the xExprCallback for a tree walker. It is used to
101215** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
101216** for additional information.
101217*/
101218static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
101219 int i;
101220 NameContext *pNC = pWalker->u.pNC;
101221 Parse *pParse = pNC->pParse;
101222 SrcList *pSrcList = pNC->pSrcList;
101223 AggInfo *pAggInfo = pNC->uNC.pAggInfo;
101224
101225 assert( pNC->ncFlags & NC_UAggInfo );
101226 switch( pExpr->op ){
101227 case TK_AGG_COLUMN:
101228 case TK_COLUMN: {
101229 testcase( pExpr->op==TK_AGG_COLUMN );
101230 testcase( pExpr->op==TK_COLUMN );
101231 /* Check to see if the column is in one of the tables in the FROM
101232 ** clause of the aggregate query */
101233 if( ALWAYS(pSrcList!=0) ){
101234 struct SrcList_item *pItem = pSrcList->a;
101235 for(i=0; i<pSrcList->nSrc; i++, pItem++){
101236 struct AggInfo_col *pCol;
101237 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
101238 if( pExpr->iTable==pItem->iCursor ){
101239 /* If we reach this point, it means that pExpr refers to a table
101240 ** that is in the FROM clause of the aggregate query.
101241 **
101242 ** Make an entry for the column in pAggInfo->aCol[] if there
101243 ** is not an entry there already.
101244 */
101245 int k;
101246 pCol = pAggInfo->aCol;
101247 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
101248 if( pCol->iTable==pExpr->iTable &&
101249 pCol->iColumn==pExpr->iColumn ){
101250 break;
101251 }
101252 }
101253 if( (k>=pAggInfo->nColumn)
101254 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
101255 ){
101256 pCol = &pAggInfo->aCol[k];
101257 pCol->pTab = pExpr->y.pTab;
101258 pCol->iTable = pExpr->iTable;
101259 pCol->iColumn = pExpr->iColumn;
101260 pCol->iMem = ++pParse->nMem;
101261 pCol->iSorterColumn = -1;
101262 pCol->pExpr = pExpr;
101263 if( pAggInfo->pGroupBy ){
101264 int j, n;
101265 ExprList *pGB = pAggInfo->pGroupBy;
101266 struct ExprList_item *pTerm = pGB->a;
101267 n = pGB->nExpr;
101268 for(j=0; j<n; j++, pTerm++){
101269 Expr *pE = pTerm->pExpr;
101270 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
101271 pE->iColumn==pExpr->iColumn ){
101272 pCol->iSorterColumn = j;
101273 break;
101274 }
101275 }
101276 }
101277 if( pCol->iSorterColumn<0 ){
101278 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
101279 }
101280 }
101281 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
101282 ** because it was there before or because we just created it).
101283 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
101284 ** pAggInfo->aCol[] entry.
101285 */
101286 ExprSetVVAProperty(pExpr, EP_NoReduce);
101287 pExpr->pAggInfo = pAggInfo;
101288 pExpr->op = TK_AGG_COLUMN;
101289 pExpr->iAgg = (i16)k;
101290 break;
101291 } /* endif pExpr->iTable==pItem->iCursor */
101292 } /* end loop over pSrcList */
101293 }
101294 return WRC_Prune;
101295 }
101296 case TK_AGG_FUNCTION: {
101297 if( (pNC->ncFlags & NC_InAggFunc)==0
101298 && pWalker->walkerDepth==pExpr->op2
101299 ){
101300 /* Check to see if pExpr is a duplicate of another aggregate
101301 ** function that is already in the pAggInfo structure
101302 */
101303 struct AggInfo_func *pItem = pAggInfo->aFunc;
101304 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
101305 if( sqlite3ExprCompare(0, pItem->pExpr, pExpr, -1)==0 ){
101306 break;
101307 }
101308 }
101309 if( i>=pAggInfo->nFunc ){
101310 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
101311 */
101312 u8 enc = ENC(pParse->db);
101313 i = addAggInfoFunc(pParse->db, pAggInfo);
101314 if( i>=0 ){
101315 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
101316 pItem = &pAggInfo->aFunc[i];
101317 pItem->pExpr = pExpr;
101318 pItem->iMem = ++pParse->nMem;
101319 assert( !ExprHasProperty(pExpr, EP_IntValue) );
101320 pItem->pFunc = sqlite3FindFunction(pParse->db,
101321 pExpr->u.zToken,
101322 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
101323 if( pExpr->flags & EP_Distinct ){
101324 pItem->iDistinct = pParse->nTab++;
101325 }else{
101326 pItem->iDistinct = -1;
101327 }
101328 }
101329 }
101330 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
101331 */
101332 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
101333 ExprSetVVAProperty(pExpr, EP_NoReduce);
101334 pExpr->iAgg = (i16)i;
101335 pExpr->pAggInfo = pAggInfo;
101336 return WRC_Prune;
101337 }else{
101338 return WRC_Continue;
101339 }
101340 }
101341 }
101342 return WRC_Continue;
101343}
101344static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
101345 UNUSED_PARAMETER(pSelect);
101346 pWalker->walkerDepth++;
101347 return WRC_Continue;
101348}
101349static void analyzeAggregatesInSelectEnd(Walker *pWalker, Select *pSelect){
101350 UNUSED_PARAMETER(pSelect);
101351 pWalker->walkerDepth--;
101352}
101353
101354/*
101355** Analyze the pExpr expression looking for aggregate functions and
101356** for variables that need to be added to AggInfo object that pNC->pAggInfo
101357** points to. Additional entries are made on the AggInfo object as
101358** necessary.
101359**
101360** This routine should only be called after the expression has been
101361** analyzed by sqlite3ResolveExprNames().
101362*/
101363SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
101364 Walker w;
101365 w.xExprCallback = analyzeAggregate;
101366 w.xSelectCallback = analyzeAggregatesInSelect;
101367 w.xSelectCallback2 = analyzeAggregatesInSelectEnd;
101368 w.walkerDepth = 0;
101369 w.u.pNC = pNC;
101370 assert( pNC->pSrcList!=0 );
101371 sqlite3WalkExpr(&w, pExpr);
101372}
101373
101374/*
101375** Call sqlite3ExprAnalyzeAggregates() for every expression in an
101376** expression list. Return the number of errors.
101377**
101378** If an error is found, the analysis is cut short.
101379*/
101380SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
101381 struct ExprList_item *pItem;
101382 int i;
101383 if( pList ){
101384 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
101385 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
101386 }
101387 }
101388}
101389
101390/*
101391** Allocate a single new register for use to hold some intermediate result.
101392*/
101393SQLITE_PRIVATE int sqlite3GetTempReg(Parse *pParse){
101394 if( pParse->nTempReg==0 ){
101395 return ++pParse->nMem;
101396 }
101397 return pParse->aTempReg[--pParse->nTempReg];
101398}
101399
101400/*
101401** Deallocate a register, making available for reuse for some other
101402** purpose.
101403*/
101404SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
101405 if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
101406 pParse->aTempReg[pParse->nTempReg++] = iReg;
101407 }
101408}
101409
101410/*
101411** Allocate or deallocate a block of nReg consecutive registers.
101412*/
101413SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){
101414 int i, n;
101415 if( nReg==1 ) return sqlite3GetTempReg(pParse);
101416 i = pParse->iRangeReg;
101417 n = pParse->nRangeReg;
101418 if( nReg<=n ){
101419 pParse->iRangeReg += nReg;
101420 pParse->nRangeReg -= nReg;
101421 }else{
101422 i = pParse->nMem+1;
101423 pParse->nMem += nReg;
101424 }
101425 return i;
101426}
101427SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
101428 if( nReg==1 ){
101429 sqlite3ReleaseTempReg(pParse, iReg);
101430 return;
101431 }
101432 if( nReg>pParse->nRangeReg ){
101433 pParse->nRangeReg = nReg;
101434 pParse->iRangeReg = iReg;
101435 }
101436}
101437
101438/*
101439** Mark all temporary registers as being unavailable for reuse.
101440*/
101441SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){
101442 pParse->nTempReg = 0;
101443 pParse->nRangeReg = 0;
101444}
101445
101446/*
101447** Validate that no temporary register falls within the range of
101448** iFirst..iLast, inclusive. This routine is only call from within assert()
101449** statements.
101450*/
101451#ifdef SQLITE_DEBUG
101452SQLITE_PRIVATE int sqlite3NoTempsInRange(Parse *pParse, int iFirst, int iLast){
101453 int i;
101454 if( pParse->nRangeReg>0
101455 && pParse->iRangeReg+pParse->nRangeReg > iFirst
101456 && pParse->iRangeReg <= iLast
101457 ){
101458 return 0;
101459 }
101460 for(i=0; i<pParse->nTempReg; i++){
101461 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
101462 return 0;
101463 }
101464 }
101465 return 1;
101466}
101467#endif /* SQLITE_DEBUG */
101468
101469/************** End of expr.c ************************************************/
101470/************** Begin file alter.c *******************************************/
101471/*
101472** 2005 February 15
101473**
101474** The author disclaims copyright to this source code. In place of
101475** a legal notice, here is a blessing:
101476**
101477** May you do good and not evil.
101478** May you find forgiveness for yourself and forgive others.
101479** May you share freely, never taking more than you give.
101480**
101481*************************************************************************
101482** This file contains C code routines that used to generate VDBE code
101483** that implements the ALTER TABLE command.
101484*/
101485/* #include "sqliteInt.h" */
101486
101487/*
101488** The code in this file only exists if we are not omitting the
101489** ALTER TABLE logic from the build.
101490*/
101491#ifndef SQLITE_OMIT_ALTERTABLE
101492
101493/*
101494** Parameter zName is the name of a table that is about to be altered
101495** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
101496** If the table is a system table, this function leaves an error message
101497** in pParse->zErr (system tables may not be altered) and returns non-zero.
101498**
101499** Or, if zName is not a system table, zero is returned.
101500*/
101501static int isSystemTable(Parse *pParse, const char *zName){
101502 if( 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
101503 sqlite3ErrorMsg(pParse, "table %s may not be altered", zName);
101504 return 1;
101505 }
101506 return 0;
101507}
101508
101509/*
101510** Generate code to verify that the schemas of database zDb and, if
101511** bTemp is not true, database "temp", can still be parsed. This is
101512** called at the end of the generation of an ALTER TABLE ... RENAME ...
101513** statement to ensure that the operation has not rendered any schema
101514** objects unusable.
101515*/
101516static void renameTestSchema(Parse *pParse, const char *zDb, int bTemp){
101517 sqlite3NestedParse(pParse,
101518 "SELECT 1 "
101519 "FROM \"%w\".%s "
101520 "WHERE name NOT LIKE 'sqlite_%%'"
101521 " AND sql NOT LIKE 'create virtual%%'"
101522 " AND sqlite_rename_test(%Q, sql, type, name, %d)=NULL ",
101523 zDb, MASTER_NAME,
101524 zDb, bTemp
101525 );
101526
101527 if( bTemp==0 ){
101528 sqlite3NestedParse(pParse,
101529 "SELECT 1 "
101530 "FROM temp.%s "
101531 "WHERE name NOT LIKE 'sqlite_%%'"
101532 " AND sql NOT LIKE 'create virtual%%'"
101533 " AND sqlite_rename_test(%Q, sql, type, name, 1)=NULL ",
101534 MASTER_NAME, zDb
101535 );
101536 }
101537}
101538
101539/*
101540** Generate code to reload the schema for database iDb. And, if iDb!=1, for
101541** the temp database as well.
101542*/
101543static void renameReloadSchema(Parse *pParse, int iDb){
101544 Vdbe *v = pParse->pVdbe;
101545 if( v ){
101546 sqlite3ChangeCookie(pParse, iDb);
101547 sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iDb, 0);
101548 if( iDb!=1 ) sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, 1, 0);
101549 }
101550}
101551
101552/*
101553** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
101554** command.
101555*/
101556SQLITE_PRIVATE void sqlite3AlterRenameTable(
101557 Parse *pParse, /* Parser context. */
101558 SrcList *pSrc, /* The table to rename. */
101559 Token *pName /* The new table name. */
101560){
101561 int iDb; /* Database that contains the table */
101562 char *zDb; /* Name of database iDb */
101563 Table *pTab; /* Table being renamed */
101564 char *zName = 0; /* NULL-terminated version of pName */
101565 sqlite3 *db = pParse->db; /* Database connection */
101566 int nTabName; /* Number of UTF-8 characters in zTabName */
101567 const char *zTabName; /* Original name of the table */
101568 Vdbe *v;
101569 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
101570 u32 savedDbFlags; /* Saved value of db->mDbFlags */
101571
101572 savedDbFlags = db->mDbFlags;
101573 if( NEVER(db->mallocFailed) ) goto exit_rename_table;
101574 assert( pSrc->nSrc==1 );
101575 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
101576
101577 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
101578 if( !pTab ) goto exit_rename_table;
101579 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
101580 zDb = db->aDb[iDb].zDbSName;
101581 db->mDbFlags |= DBFLAG_PreferBuiltin;
101582
101583 /* Get a NULL terminated version of the new table name. */
101584 zName = sqlite3NameFromToken(db, pName);
101585 if( !zName ) goto exit_rename_table;
101586
101587 /* Check that a table or index named 'zName' does not already exist
101588 ** in database iDb. If so, this is an error.
101589 */
101590 if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
101591 sqlite3ErrorMsg(pParse,
101592 "there is already another table or index with this name: %s", zName);
101593 goto exit_rename_table;
101594 }
101595
101596 /* Make sure it is not a system table being altered, or a reserved name
101597 ** that the table is being renamed to.
101598 */
101599 if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
101600 goto exit_rename_table;
101601 }
101602 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto
101603 exit_rename_table;
101604 }
101605
101606#ifndef SQLITE_OMIT_VIEW
101607 if( pTab->pSelect ){
101608 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
101609 goto exit_rename_table;
101610 }
101611#endif
101612
101613#ifndef SQLITE_OMIT_AUTHORIZATION
101614 /* Invoke the authorization callback. */
101615 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
101616 goto exit_rename_table;
101617 }
101618#endif
101619
101620#ifndef SQLITE_OMIT_VIRTUALTABLE
101621 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
101622 goto exit_rename_table;
101623 }
101624 if( IsVirtual(pTab) ){
101625 pVTab = sqlite3GetVTable(db, pTab);
101626 if( pVTab->pVtab->pModule->xRename==0 ){
101627 pVTab = 0;
101628 }
101629 }
101630#endif
101631
101632 /* Begin a transaction for database iDb.
101633 ** Then modify the schema cookie (since the ALTER TABLE modifies the
101634 ** schema). Open a statement transaction if the table is a virtual
101635 ** table.
101636 */
101637 v = sqlite3GetVdbe(pParse);
101638 if( v==0 ){
101639 goto exit_rename_table;
101640 }
101641
101642 /* figure out how many UTF-8 characters are in zName */
101643 zTabName = pTab->zName;
101644 nTabName = sqlite3Utf8CharLen(zTabName, -1);
101645
101646 /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in
101647 ** the schema to use the new table name. */
101648 sqlite3NestedParse(pParse,
101649 "UPDATE \"%w\".%s SET "
101650 "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, %d) "
101651 "WHERE (type!='index' OR tbl_name=%Q COLLATE nocase)"
101652 "AND name NOT LIKE 'sqlite_%%'"
101653 , zDb, MASTER_NAME, zDb, zTabName, zName, (iDb==1), zTabName
101654 );
101655
101656 /* Update the tbl_name and name columns of the sqlite_master table
101657 ** as required. */
101658 sqlite3NestedParse(pParse,
101659 "UPDATE %Q.%s SET "
101660 "tbl_name = %Q, "
101661 "name = CASE "
101662 "WHEN type='table' THEN %Q "
101663 "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
101664 "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
101665 "ELSE name END "
101666 "WHERE tbl_name=%Q COLLATE nocase AND "
101667 "(type='table' OR type='index' OR type='trigger');",
101668 zDb, MASTER_NAME,
101669 zName, zName, zName,
101670 nTabName, zTabName
101671 );
101672
101673#ifndef SQLITE_OMIT_AUTOINCREMENT
101674 /* If the sqlite_sequence table exists in this database, then update
101675 ** it with the new table name.
101676 */
101677 if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
101678 sqlite3NestedParse(pParse,
101679 "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
101680 zDb, zName, pTab->zName);
101681 }
101682#endif
101683
101684 /* If the table being renamed is not itself part of the temp database,
101685 ** edit view and trigger definitions within the temp database
101686 ** as required. */
101687 if( iDb!=1 ){
101688 sqlite3NestedParse(pParse,
101689 "UPDATE sqlite_temp_master SET "
101690 "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, 1), "
101691 "tbl_name = "
101692 "CASE WHEN tbl_name=%Q COLLATE nocase AND "
101693 " sqlite_rename_test(%Q, sql, type, name, 1) "
101694 "THEN %Q ELSE tbl_name END "
101695 "WHERE type IN ('view', 'trigger')"
101696 , zDb, zTabName, zName, zTabName, zDb, zName);
101697 }
101698
101699 /* If this is a virtual table, invoke the xRename() function if
101700 ** one is defined. The xRename() callback will modify the names
101701 ** of any resources used by the v-table implementation (including other
101702 ** SQLite tables) that are identified by the name of the virtual table.
101703 */
101704#ifndef SQLITE_OMIT_VIRTUALTABLE
101705 if( pVTab ){
101706 int i = ++pParse->nMem;
101707 sqlite3VdbeLoadString(v, i, zName);
101708 sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
101709 sqlite3MayAbort(pParse);
101710 }
101711#endif
101712
101713 renameReloadSchema(pParse, iDb);
101714 renameTestSchema(pParse, zDb, iDb==1);
101715
101716exit_rename_table:
101717 sqlite3SrcListDelete(db, pSrc);
101718 sqlite3DbFree(db, zName);
101719 db->mDbFlags = savedDbFlags;
101720}
101721
101722/*
101723** This function is called after an "ALTER TABLE ... ADD" statement
101724** has been parsed. Argument pColDef contains the text of the new
101725** column definition.
101726**
101727** The Table structure pParse->pNewTable was extended to include
101728** the new column during parsing.
101729*/
101730SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
101731 Table *pNew; /* Copy of pParse->pNewTable */
101732 Table *pTab; /* Table being altered */
101733 int iDb; /* Database number */
101734 const char *zDb; /* Database name */
101735 const char *zTab; /* Table name */
101736 char *zCol; /* Null-terminated column definition */
101737 Column *pCol; /* The new column */
101738 Expr *pDflt; /* Default value for the new column */
101739 sqlite3 *db; /* The database connection; */
101740 Vdbe *v; /* The prepared statement under construction */
101741 int r1; /* Temporary registers */
101742
101743 db = pParse->db;
101744 if( pParse->nErr || db->mallocFailed ) return;
101745 pNew = pParse->pNewTable;
101746 assert( pNew );
101747
101748 assert( sqlite3BtreeHoldsAllMutexes(db) );
101749 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
101750 zDb = db->aDb[iDb].zDbSName;
101751 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
101752 pCol = &pNew->aCol[pNew->nCol-1];
101753 pDflt = pCol->pDflt;
101754 pTab = sqlite3FindTable(db, zTab, zDb);
101755 assert( pTab );
101756
101757#ifndef SQLITE_OMIT_AUTHORIZATION
101758 /* Invoke the authorization callback. */
101759 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
101760 return;
101761 }
101762#endif
101763
101764 /* If the default value for the new column was specified with a
101765 ** literal NULL, then set pDflt to 0. This simplifies checking
101766 ** for an SQL NULL default below.
101767 */
101768 assert( pDflt==0 || pDflt->op==TK_SPAN );
101769 if( pDflt && pDflt->pLeft->op==TK_NULL ){
101770 pDflt = 0;
101771 }
101772
101773 /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
101774 ** If there is a NOT NULL constraint, then the default value for the
101775 ** column must not be NULL.
101776 */
101777 if( pCol->colFlags & COLFLAG_PRIMKEY ){
101778 sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
101779 return;
101780 }
101781 if( pNew->pIndex ){
101782 sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
101783 return;
101784 }
101785 if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
101786 sqlite3ErrorMsg(pParse,
101787 "Cannot add a REFERENCES column with non-NULL default value");
101788 return;
101789 }
101790 if( pCol->notNull && !pDflt ){
101791 sqlite3ErrorMsg(pParse,
101792 "Cannot add a NOT NULL column with default value NULL");
101793 return;
101794 }
101795
101796 /* Ensure the default expression is something that sqlite3ValueFromExpr()
101797 ** can handle (i.e. not CURRENT_TIME etc.)
101798 */
101799 if( pDflt ){
101800 sqlite3_value *pVal = 0;
101801 int rc;
101802 rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal);
101803 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
101804 if( rc!=SQLITE_OK ){
101805 assert( db->mallocFailed == 1 );
101806 return;
101807 }
101808 if( !pVal ){
101809 sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
101810 return;
101811 }
101812 sqlite3ValueFree(pVal);
101813 }
101814
101815 /* Modify the CREATE TABLE statement. */
101816 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
101817 if( zCol ){
101818 char *zEnd = &zCol[pColDef->n-1];
101819 u32 savedDbFlags = db->mDbFlags;
101820 while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
101821 *zEnd-- = '\0';
101822 }
101823 db->mDbFlags |= DBFLAG_PreferBuiltin;
101824 sqlite3NestedParse(pParse,
101825 "UPDATE \"%w\".%s SET "
101826 "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
101827 "WHERE type = 'table' AND name = %Q",
101828 zDb, MASTER_NAME, pNew->addColOffset, zCol, pNew->addColOffset+1,
101829 zTab
101830 );
101831 sqlite3DbFree(db, zCol);
101832 db->mDbFlags = savedDbFlags;
101833 }
101834
101835 /* Make sure the schema version is at least 3. But do not upgrade
101836 ** from less than 3 to 4, as that will corrupt any preexisting DESC
101837 ** index.
101838 */
101839 v = sqlite3GetVdbe(pParse);
101840 if( v ){
101841 r1 = sqlite3GetTempReg(pParse);
101842 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
101843 sqlite3VdbeUsesBtree(v, iDb);
101844 sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
101845 sqlite3VdbeAddOp2(v, OP_IfPos, r1, sqlite3VdbeCurrentAddr(v)+2);
101846 VdbeCoverage(v);
101847 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, 3);
101848 sqlite3ReleaseTempReg(pParse, r1);
101849 }
101850
101851 /* Reload the table definition */
101852 renameReloadSchema(pParse, iDb);
101853}
101854
101855/*
101856** This function is called by the parser after the table-name in
101857** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
101858** pSrc is the full-name of the table being altered.
101859**
101860** This routine makes a (partial) copy of the Table structure
101861** for the table being altered and sets Parse.pNewTable to point
101862** to it. Routines called by the parser as the column definition
101863** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
101864** the copy. The copy of the Table structure is deleted by tokenize.c
101865** after parsing is finished.
101866**
101867** Routine sqlite3AlterFinishAddColumn() will be called to complete
101868** coding the "ALTER TABLE ... ADD" statement.
101869*/
101870SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
101871 Table *pNew;
101872 Table *pTab;
101873 int iDb;
101874 int i;
101875 int nAlloc;
101876 sqlite3 *db = pParse->db;
101877
101878 /* Look up the table being altered. */
101879 assert( pParse->pNewTable==0 );
101880 assert( sqlite3BtreeHoldsAllMutexes(db) );
101881 if( db->mallocFailed ) goto exit_begin_add_column;
101882 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
101883 if( !pTab ) goto exit_begin_add_column;
101884
101885#ifndef SQLITE_OMIT_VIRTUALTABLE
101886 if( IsVirtual(pTab) ){
101887 sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
101888 goto exit_begin_add_column;
101889 }
101890#endif
101891
101892 /* Make sure this is not an attempt to ALTER a view. */
101893 if( pTab->pSelect ){
101894 sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
101895 goto exit_begin_add_column;
101896 }
101897 if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
101898 goto exit_begin_add_column;
101899 }
101900
101901 assert( pTab->addColOffset>0 );
101902 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
101903
101904 /* Put a copy of the Table struct in Parse.pNewTable for the
101905 ** sqlite3AddColumn() function and friends to modify. But modify
101906 ** the name by adding an "sqlite_altertab_" prefix. By adding this
101907 ** prefix, we insure that the name will not collide with an existing
101908 ** table because user table are not allowed to have the "sqlite_"
101909 ** prefix on their name.
101910 */
101911 pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
101912 if( !pNew ) goto exit_begin_add_column;
101913 pParse->pNewTable = pNew;
101914 pNew->nTabRef = 1;
101915 pNew->nCol = pTab->nCol;
101916 assert( pNew->nCol>0 );
101917 nAlloc = (((pNew->nCol-1)/8)*8)+8;
101918 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
101919 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
101920 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
101921 if( !pNew->aCol || !pNew->zName ){
101922 assert( db->mallocFailed );
101923 goto exit_begin_add_column;
101924 }
101925 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
101926 for(i=0; i<pNew->nCol; i++){
101927 Column *pCol = &pNew->aCol[i];
101928 pCol->zName = sqlite3DbStrDup(db, pCol->zName);
101929 pCol->zColl = 0;
101930 pCol->pDflt = 0;
101931 }
101932 pNew->pSchema = db->aDb[iDb].pSchema;
101933 pNew->addColOffset = pTab->addColOffset;
101934 pNew->nTabRef = 1;
101935
101936exit_begin_add_column:
101937 sqlite3SrcListDelete(db, pSrc);
101938 return;
101939}
101940
101941/*
101942** Parameter pTab is the subject of an ALTER TABLE ... RENAME COLUMN
101943** command. This function checks if the table is a view or virtual
101944** table (columns of views or virtual tables may not be renamed). If so,
101945** it loads an error message into pParse and returns non-zero.
101946**
101947** Or, if pTab is not a view or virtual table, zero is returned.
101948*/
101949#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
101950static int isRealTable(Parse *pParse, Table *pTab){
101951 const char *zType = 0;
101952#ifndef SQLITE_OMIT_VIEW
101953 if( pTab->pSelect ){
101954 zType = "view";
101955 }
101956#endif
101957#ifndef SQLITE_OMIT_VIRTUALTABLE
101958 if( IsVirtual(pTab) ){
101959 zType = "virtual table";
101960 }
101961#endif
101962 if( zType ){
101963 sqlite3ErrorMsg(
101964 pParse, "cannot rename columns of %s \"%s\"", zType, pTab->zName
101965 );
101966 return 1;
101967 }
101968 return 0;
101969}
101970#else /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
101971# define isRealTable(x,y) (0)
101972#endif
101973
101974/*
101975** Handles the following parser reduction:
101976**
101977** cmd ::= ALTER TABLE pSrc RENAME COLUMN pOld TO pNew
101978*/
101979SQLITE_PRIVATE void sqlite3AlterRenameColumn(
101980 Parse *pParse, /* Parsing context */
101981 SrcList *pSrc, /* Table being altered. pSrc->nSrc==1 */
101982 Token *pOld, /* Name of column being changed */
101983 Token *pNew /* New column name */
101984){
101985 sqlite3 *db = pParse->db; /* Database connection */
101986 Table *pTab; /* Table being updated */
101987 int iCol; /* Index of column being renamed */
101988 char *zOld = 0; /* Old column name */
101989 char *zNew = 0; /* New column name */
101990 const char *zDb; /* Name of schema containing the table */
101991 int iSchema; /* Index of the schema */
101992 int bQuote; /* True to quote the new name */
101993
101994 /* Locate the table to be altered */
101995 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
101996 if( !pTab ) goto exit_rename_column;
101997
101998 /* Cannot alter a system table */
101999 if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ) goto exit_rename_column;
102000 if( SQLITE_OK!=isRealTable(pParse, pTab) ) goto exit_rename_column;
102001
102002 /* Which schema holds the table to be altered */
102003 iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
102004 assert( iSchema>=0 );
102005 zDb = db->aDb[iSchema].zDbSName;
102006
102007#ifndef SQLITE_OMIT_AUTHORIZATION
102008 /* Invoke the authorization callback. */
102009 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
102010 goto exit_rename_column;
102011 }
102012#endif
102013
102014 /* Make sure the old name really is a column name in the table to be
102015 ** altered. Set iCol to be the index of the column being renamed */
102016 zOld = sqlite3NameFromToken(db, pOld);
102017 if( !zOld ) goto exit_rename_column;
102018 for(iCol=0; iCol<pTab->nCol; iCol++){
102019 if( 0==sqlite3StrICmp(pTab->aCol[iCol].zName, zOld) ) break;
102020 }
102021 if( iCol==pTab->nCol ){
102022 sqlite3ErrorMsg(pParse, "no such column: \"%s\"", zOld);
102023 goto exit_rename_column;
102024 }
102025
102026 /* Do the rename operation using a recursive UPDATE statement that
102027 ** uses the sqlite_rename_column() SQL function to compute the new
102028 ** CREATE statement text for the sqlite_master table.
102029 */
102030 zNew = sqlite3NameFromToken(db, pNew);
102031 if( !zNew ) goto exit_rename_column;
102032 assert( pNew->n>0 );
102033 bQuote = sqlite3Isquote(pNew->z[0]);
102034 sqlite3NestedParse(pParse,
102035 "UPDATE \"%w\".%s SET "
102036 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, %d) "
102037 "WHERE name NOT LIKE 'sqlite_%%' AND (type != 'index' OR tbl_name = %Q)"
102038 " AND sql NOT LIKE 'create virtual%%'",
102039 zDb, MASTER_NAME,
102040 zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
102041 pTab->zName
102042 );
102043
102044 sqlite3NestedParse(pParse,
102045 "UPDATE temp.%s SET "
102046 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, 1) "
102047 "WHERE type IN ('trigger', 'view')",
102048 MASTER_NAME,
102049 zDb, pTab->zName, iCol, zNew, bQuote
102050 );
102051
102052 /* Drop and reload the database schema. */
102053 renameReloadSchema(pParse, iSchema);
102054 renameTestSchema(pParse, zDb, iSchema==1);
102055
102056 exit_rename_column:
102057 sqlite3SrcListDelete(db, pSrc);
102058 sqlite3DbFree(db, zOld);
102059 sqlite3DbFree(db, zNew);
102060 return;
102061}
102062
102063/*
102064** Each RenameToken object maps an element of the parse tree into
102065** the token that generated that element. The parse tree element
102066** might be one of:
102067**
102068** * A pointer to an Expr that represents an ID
102069** * The name of a table column in Column.zName
102070**
102071** A list of RenameToken objects can be constructed during parsing.
102072** Each new object is created by sqlite3RenameTokenMap().
102073** As the parse tree is transformed, the sqlite3RenameTokenRemap()
102074** routine is used to keep the mapping current.
102075**
102076** After the parse finishes, renameTokenFind() routine can be used
102077** to look up the actual token value that created some element in
102078** the parse tree.
102079*/
102080struct RenameToken {
102081 void *p; /* Parse tree element created by token t */
102082 Token t; /* The token that created parse tree element p */
102083 RenameToken *pNext; /* Next is a list of all RenameToken objects */
102084};
102085
102086/*
102087** The context of an ALTER TABLE RENAME COLUMN operation that gets passed
102088** down into the Walker.
102089*/
102090typedef struct RenameCtx RenameCtx;
102091struct RenameCtx {
102092 RenameToken *pList; /* List of tokens to overwrite */
102093 int nList; /* Number of tokens in pList */
102094 int iCol; /* Index of column being renamed */
102095 Table *pTab; /* Table being ALTERed */
102096 const char *zOld; /* Old column name */
102097};
102098
102099#ifdef SQLITE_DEBUG
102100/*
102101** This function is only for debugging. It performs two tasks:
102102**
102103** 1. Checks that pointer pPtr does not already appear in the
102104** rename-token list.
102105**
102106** 2. Dereferences each pointer in the rename-token list.
102107**
102108** The second is most effective when debugging under valgrind or
102109** address-sanitizer or similar. If any of these pointers no longer
102110** point to valid objects, an exception is raised by the memory-checking
102111** tool.
102112**
102113** The point of this is to prevent comparisons of invalid pointer values.
102114** Even though this always seems to work, it is undefined according to the
102115** C standard. Example of undefined comparison:
102116**
102117** sqlite3_free(x);
102118** if( x==y ) ...
102119**
102120** Technically, as x no longer points into a valid object or to the byte
102121** following a valid object, it may not be used in comparison operations.
102122*/
102123static void renameTokenCheckAll(Parse *pParse, void *pPtr){
102124 if( pParse->nErr==0 && pParse->db->mallocFailed==0 ){
102125 RenameToken *p;
102126 u8 i = 0;
102127 for(p=pParse->pRename; p; p=p->pNext){
102128 if( p->p ){
102129 assert( p->p!=pPtr );
102130 i += *(u8*)(p->p);
102131 }
102132 }
102133 }
102134}
102135#else
102136# define renameTokenCheckAll(x,y)
102137#endif
102138
102139/*
102140** Remember that the parser tree element pPtr was created using
102141** the token pToken.
102142**
102143** In other words, construct a new RenameToken object and add it
102144** to the list of RenameToken objects currently being built up
102145** in pParse->pRename.
102146**
102147** The pPtr argument is returned so that this routine can be used
102148** with tail recursion in tokenExpr() routine, for a small performance
102149** improvement.
102150*/
102151SQLITE_PRIVATE void *sqlite3RenameTokenMap(Parse *pParse, void *pPtr, Token *pToken){
102152 RenameToken *pNew;
102153 assert( pPtr || pParse->db->mallocFailed );
102154 renameTokenCheckAll(pParse, pPtr);
102155 pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
102156 if( pNew ){
102157 pNew->p = pPtr;
102158 pNew->t = *pToken;
102159 pNew->pNext = pParse->pRename;
102160 pParse->pRename = pNew;
102161 }
102162
102163 return pPtr;
102164}
102165
102166/*
102167** It is assumed that there is already a RenameToken object associated
102168** with parse tree element pFrom. This function remaps the associated token
102169** to parse tree element pTo.
102170*/
102171SQLITE_PRIVATE void sqlite3RenameTokenRemap(Parse *pParse, void *pTo, void *pFrom){
102172 RenameToken *p;
102173 renameTokenCheckAll(pParse, pTo);
102174 for(p=pParse->pRename; p; p=p->pNext){
102175 if( p->p==pFrom ){
102176 p->p = pTo;
102177 break;
102178 }
102179 }
102180}
102181
102182/*
102183** Walker callback used by sqlite3RenameExprUnmap().
102184*/
102185static int renameUnmapExprCb(Walker *pWalker, Expr *pExpr){
102186 Parse *pParse = pWalker->pParse;
102187 sqlite3RenameTokenRemap(pParse, 0, (void*)pExpr);
102188 return WRC_Continue;
102189}
102190
102191/*
102192** Remove all nodes that are part of expression pExpr from the rename list.
102193*/
102194SQLITE_PRIVATE void sqlite3RenameExprUnmap(Parse *pParse, Expr *pExpr){
102195 Walker sWalker;
102196 memset(&sWalker, 0, sizeof(Walker));
102197 sWalker.pParse = pParse;
102198 sWalker.xExprCallback = renameUnmapExprCb;
102199 sqlite3WalkExpr(&sWalker, pExpr);
102200}
102201
102202/*
102203** Remove all nodes that are part of expression-list pEList from the
102204** rename list.
102205*/
102206SQLITE_PRIVATE void sqlite3RenameExprlistUnmap(Parse *pParse, ExprList *pEList){
102207 if( pEList ){
102208 int i;
102209 Walker sWalker;
102210 memset(&sWalker, 0, sizeof(Walker));
102211 sWalker.pParse = pParse;
102212 sWalker.xExprCallback = renameUnmapExprCb;
102213 sqlite3WalkExprList(&sWalker, pEList);
102214 for(i=0; i<pEList->nExpr; i++){
102215 sqlite3RenameTokenRemap(pParse, 0, (void*)pEList->a[i].zName);
102216 }
102217 }
102218}
102219
102220/*
102221** Free the list of RenameToken objects given in the second argument
102222*/
102223static void renameTokenFree(sqlite3 *db, RenameToken *pToken){
102224 RenameToken *pNext;
102225 RenameToken *p;
102226 for(p=pToken; p; p=pNext){
102227 pNext = p->pNext;
102228 sqlite3DbFree(db, p);
102229 }
102230}
102231
102232/*
102233** Search the Parse object passed as the first argument for a RenameToken
102234** object associated with parse tree element pPtr. If found, remove it
102235** from the Parse object and add it to the list maintained by the
102236** RenameCtx object passed as the second argument.
102237*/
102238static void renameTokenFind(Parse *pParse, struct RenameCtx *pCtx, void *pPtr){
102239 RenameToken **pp;
102240 assert( pPtr!=0 );
102241 for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
102242 if( (*pp)->p==pPtr ){
102243 RenameToken *pToken = *pp;
102244 *pp = pToken->pNext;
102245 pToken->pNext = pCtx->pList;
102246 pCtx->pList = pToken;
102247 pCtx->nList++;
102248 break;
102249 }
102250 }
102251}
102252
102253/*
102254** This is a Walker select callback. It does nothing. It is only required
102255** because without a dummy callback, sqlite3WalkExpr() and similar do not
102256** descend into sub-select statements.
102257*/
102258static int renameColumnSelectCb(Walker *pWalker, Select *p){
102259 UNUSED_PARAMETER(pWalker);
102260 UNUSED_PARAMETER(p);
102261 return WRC_Continue;
102262}
102263
102264/*
102265** This is a Walker expression callback.
102266**
102267** For every TK_COLUMN node in the expression tree, search to see
102268** if the column being references is the column being renamed by an
102269** ALTER TABLE statement. If it is, then attach its associated
102270** RenameToken object to the list of RenameToken objects being
102271** constructed in RenameCtx object at pWalker->u.pRename.
102272*/
102273static int renameColumnExprCb(Walker *pWalker, Expr *pExpr){
102274 RenameCtx *p = pWalker->u.pRename;
102275 if( pExpr->op==TK_TRIGGER
102276 && pExpr->iColumn==p->iCol
102277 && pWalker->pParse->pTriggerTab==p->pTab
102278 ){
102279 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
102280 }else if( pExpr->op==TK_COLUMN
102281 && pExpr->iColumn==p->iCol
102282 && p->pTab==pExpr->y.pTab
102283 ){
102284 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
102285 }
102286 return WRC_Continue;
102287}
102288
102289/*
102290** The RenameCtx contains a list of tokens that reference a column that
102291** is being renamed by an ALTER TABLE statement. Return the "last"
102292** RenameToken in the RenameCtx and remove that RenameToken from the
102293** RenameContext. "Last" means the last RenameToken encountered when
102294** the input SQL is parsed from left to right. Repeated calls to this routine
102295** return all column name tokens in the order that they are encountered
102296** in the SQL statement.
102297*/
102298static RenameToken *renameColumnTokenNext(RenameCtx *pCtx){
102299 RenameToken *pBest = pCtx->pList;
102300 RenameToken *pToken;
102301 RenameToken **pp;
102302
102303 for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){
102304 if( pToken->t.z>pBest->t.z ) pBest = pToken;
102305 }
102306 for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext);
102307 *pp = pBest->pNext;
102308
102309 return pBest;
102310}
102311
102312/*
102313** An error occured while parsing or otherwise processing a database
102314** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an
102315** ALTER TABLE RENAME COLUMN program. The error message emitted by the
102316** sub-routine is currently stored in pParse->zErrMsg. This function
102317** adds context to the error message and then stores it in pCtx.
102318*/
102319static void renameColumnParseError(
102320 sqlite3_context *pCtx,
102321 int bPost,
102322 sqlite3_value *pType,
102323 sqlite3_value *pObject,
102324 Parse *pParse
102325){
102326 const char *zT = (const char*)sqlite3_value_text(pType);
102327 const char *zN = (const char*)sqlite3_value_text(pObject);
102328 char *zErr;
102329
102330 zErr = sqlite3_mprintf("error in %s %s%s: %s",
102331 zT, zN, (bPost ? " after rename" : ""),
102332 pParse->zErrMsg
102333 );
102334 sqlite3_result_error(pCtx, zErr, -1);
102335 sqlite3_free(zErr);
102336}
102337
102338/*
102339** For each name in the the expression-list pEList (i.e. each
102340** pEList->a[i].zName) that matches the string in zOld, extract the
102341** corresponding rename-token from Parse object pParse and add it
102342** to the RenameCtx pCtx.
102343*/
102344static void renameColumnElistNames(
102345 Parse *pParse,
102346 RenameCtx *pCtx,
102347 ExprList *pEList,
102348 const char *zOld
102349){
102350 if( pEList ){
102351 int i;
102352 for(i=0; i<pEList->nExpr; i++){
102353 char *zName = pEList->a[i].zName;
102354 if( 0==sqlite3_stricmp(zName, zOld) ){
102355 renameTokenFind(pParse, pCtx, (void*)zName);
102356 }
102357 }
102358 }
102359}
102360
102361/*
102362** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName)
102363** that matches the string in zOld, extract the corresponding rename-token
102364** from Parse object pParse and add it to the RenameCtx pCtx.
102365*/
102366static void renameColumnIdlistNames(
102367 Parse *pParse,
102368 RenameCtx *pCtx,
102369 IdList *pIdList,
102370 const char *zOld
102371){
102372 if( pIdList ){
102373 int i;
102374 for(i=0; i<pIdList->nId; i++){
102375 char *zName = pIdList->a[i].zName;
102376 if( 0==sqlite3_stricmp(zName, zOld) ){
102377 renameTokenFind(pParse, pCtx, (void*)zName);
102378 }
102379 }
102380 }
102381}
102382
102383/*
102384** Parse the SQL statement zSql using Parse object (*p). The Parse object
102385** is initialized by this function before it is used.
102386*/
102387static int renameParseSql(
102388 Parse *p, /* Memory to use for Parse object */
102389 const char *zDb, /* Name of schema SQL belongs to */
102390 int bTable, /* 1 -> RENAME TABLE, 0 -> RENAME COLUMN */
102391 sqlite3 *db, /* Database handle */
102392 const char *zSql, /* SQL to parse */
102393 int bTemp /* True if SQL is from temp schema */
102394){
102395 int rc;
102396 char *zErr = 0;
102397
102398 db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
102399
102400 /* Parse the SQL statement passed as the first argument. If no error
102401 ** occurs and the parse does not result in a new table, index or
102402 ** trigger object, the database must be corrupt. */
102403 memset(p, 0, sizeof(Parse));
102404 p->eParseMode = (bTable ? PARSE_MODE_RENAME_TABLE : PARSE_MODE_RENAME_COLUMN);
102405 p->db = db;
102406 p->nQueryLoop = 1;
102407 rc = sqlite3RunParser(p, zSql, &zErr);
102408 assert( p->zErrMsg==0 );
102409 assert( rc!=SQLITE_OK || zErr==0 );
102410 assert( (0!=p->pNewTable) + (0!=p->pNewIndex) + (0!=p->pNewTrigger)<2 );
102411 p->zErrMsg = zErr;
102412 if( db->mallocFailed ) rc = SQLITE_NOMEM;
102413 if( rc==SQLITE_OK
102414 && p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0
102415 ){
102416 rc = SQLITE_CORRUPT_BKPT;
102417 }
102418
102419#ifdef SQLITE_DEBUG
102420 /* Ensure that all mappings in the Parse.pRename list really do map to
102421 ** a part of the input string. */
102422 if( rc==SQLITE_OK ){
102423 int nSql = sqlite3Strlen30(zSql);
102424 RenameToken *pToken;
102425 for(pToken=p->pRename; pToken; pToken=pToken->pNext){
102426 assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] );
102427 }
102428 }
102429#endif
102430
102431 db->init.iDb = 0;
102432 return rc;
102433}
102434
102435/*
102436** This function edits SQL statement zSql, replacing each token identified
102437** by the linked list pRename with the text of zNew. If argument bQuote is
102438** true, then zNew is always quoted first. If no error occurs, the result
102439** is loaded into context object pCtx as the result.
102440**
102441** Or, if an error occurs (i.e. an OOM condition), an error is left in
102442** pCtx and an SQLite error code returned.
102443*/
102444static int renameEditSql(
102445 sqlite3_context *pCtx, /* Return result here */
102446 RenameCtx *pRename, /* Rename context */
102447 const char *zSql, /* SQL statement to edit */
102448 const char *zNew, /* New token text */
102449 int bQuote /* True to always quote token */
102450){
102451 int nNew = sqlite3Strlen30(zNew);
102452 int nSql = sqlite3Strlen30(zSql);
102453 sqlite3 *db = sqlite3_context_db_handle(pCtx);
102454 int rc = SQLITE_OK;
102455 char *zQuot;
102456 char *zOut;
102457 int nQuot;
102458
102459 /* Set zQuot to point to a buffer containing a quoted copy of the
102460 ** identifier zNew. If the corresponding identifier in the original
102461 ** ALTER TABLE statement was quoted (bQuote==1), then set zNew to
102462 ** point to zQuot so that all substitutions are made using the
102463 ** quoted version of the new column name. */
102464 zQuot = sqlite3MPrintf(db, "\"%w\"", zNew);
102465 if( zQuot==0 ){
102466 return SQLITE_NOMEM;
102467 }else{
102468 nQuot = sqlite3Strlen30(zQuot);
102469 }
102470 if( bQuote ){
102471 zNew = zQuot;
102472 nNew = nQuot;
102473 }
102474
102475 /* At this point pRename->pList contains a list of RenameToken objects
102476 ** corresponding to all tokens in the input SQL that must be replaced
102477 ** with the new column name. All that remains is to construct and
102478 ** return the edited SQL string. */
102479 assert( nQuot>=nNew );
102480 zOut = sqlite3DbMallocZero(db, nSql + pRename->nList*nQuot + 1);
102481 if( zOut ){
102482 int nOut = nSql;
102483 memcpy(zOut, zSql, nSql);
102484 while( pRename->pList ){
102485 int iOff; /* Offset of token to replace in zOut */
102486 RenameToken *pBest = renameColumnTokenNext(pRename);
102487
102488 u32 nReplace;
102489 const char *zReplace;
102490 if( sqlite3IsIdChar(*pBest->t.z) ){
102491 nReplace = nNew;
102492 zReplace = zNew;
102493 }else{
102494 nReplace = nQuot;
102495 zReplace = zQuot;
102496 }
102497
102498 iOff = pBest->t.z - zSql;
102499 if( pBest->t.n!=nReplace ){
102500 memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n],
102501 nOut - (iOff + pBest->t.n)
102502 );
102503 nOut += nReplace - pBest->t.n;
102504 zOut[nOut] = '\0';
102505 }
102506 memcpy(&zOut[iOff], zReplace, nReplace);
102507 sqlite3DbFree(db, pBest);
102508 }
102509
102510 sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT);
102511 sqlite3DbFree(db, zOut);
102512 }else{
102513 rc = SQLITE_NOMEM;
102514 }
102515
102516 sqlite3_free(zQuot);
102517 return rc;
102518}
102519
102520/*
102521** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
102522** it was read from the schema of database zDb. Return SQLITE_OK if
102523** successful. Otherwise, return an SQLite error code and leave an error
102524** message in the Parse object.
102525*/
102526static int renameResolveTrigger(Parse *pParse, const char *zDb){
102527 sqlite3 *db = pParse->db;
102528 Trigger *pNew = pParse->pNewTrigger;
102529 TriggerStep *pStep;
102530 NameContext sNC;
102531 int rc = SQLITE_OK;
102532
102533 memset(&sNC, 0, sizeof(sNC));
102534 sNC.pParse = pParse;
102535 assert( pNew->pTabSchema );
102536 pParse->pTriggerTab = sqlite3FindTable(db, pNew->table,
102537 db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
102538 );
102539 pParse->eTriggerOp = pNew->op;
102540 /* ALWAYS() because if the table of the trigger does not exist, the
102541 ** error would have been hit before this point */
102542 if( ALWAYS(pParse->pTriggerTab) ){
102543 rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab);
102544 }
102545
102546 /* Resolve symbols in WHEN clause */
102547 if( rc==SQLITE_OK && pNew->pWhen ){
102548 rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
102549 }
102550
102551 for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){
102552 if( pStep->pSelect ){
102553 sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
102554 if( pParse->nErr ) rc = pParse->rc;
102555 }
102556 if( rc==SQLITE_OK && pStep->zTarget ){
102557 Table *pTarget = sqlite3LocateTable(pParse, 0, pStep->zTarget, zDb);
102558 if( pTarget==0 ){
102559 rc = SQLITE_ERROR;
102560 }else if( SQLITE_OK==(rc = sqlite3ViewGetColumnNames(pParse, pTarget)) ){
102561 SrcList sSrc;
102562 memset(&sSrc, 0, sizeof(sSrc));
102563 sSrc.nSrc = 1;
102564 sSrc.a[0].zName = pStep->zTarget;
102565 sSrc.a[0].pTab = pTarget;
102566 sNC.pSrcList = &sSrc;
102567 if( pStep->pWhere ){
102568 rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere);
102569 }
102570 if( rc==SQLITE_OK ){
102571 rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList);
102572 }
102573 assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) );
102574 if( pStep->pUpsert ){
102575 Upsert *pUpsert = pStep->pUpsert;
102576 assert( rc==SQLITE_OK );
102577 pUpsert->pUpsertSrc = &sSrc;
102578 sNC.uNC.pUpsert = pUpsert;
102579 sNC.ncFlags = NC_UUpsert;
102580 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
102581 if( rc==SQLITE_OK ){
102582 ExprList *pUpsertSet = pUpsert->pUpsertSet;
102583 rc = sqlite3ResolveExprListNames(&sNC, pUpsertSet);
102584 }
102585 if( rc==SQLITE_OK ){
102586 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere);
102587 }
102588 if( rc==SQLITE_OK ){
102589 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
102590 }
102591 sNC.ncFlags = 0;
102592 }
102593 }
102594 }
102595 }
102596 return rc;
102597}
102598
102599/*
102600** Invoke sqlite3WalkExpr() or sqlite3WalkSelect() on all Select or Expr
102601** objects that are part of the trigger passed as the second argument.
102602*/
102603static void renameWalkTrigger(Walker *pWalker, Trigger *pTrigger){
102604 TriggerStep *pStep;
102605
102606 /* Find tokens to edit in WHEN clause */
102607 sqlite3WalkExpr(pWalker, pTrigger->pWhen);
102608
102609 /* Find tokens to edit in trigger steps */
102610 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
102611 sqlite3WalkSelect(pWalker, pStep->pSelect);
102612 sqlite3WalkExpr(pWalker, pStep->pWhere);
102613 sqlite3WalkExprList(pWalker, pStep->pExprList);
102614 if( pStep->pUpsert ){
102615 Upsert *pUpsert = pStep->pUpsert;
102616 sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget);
102617 sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
102618 sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
102619 sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
102620 }
102621 }
102622}
102623
102624/*
102625** Free the contents of Parse object (*pParse). Do not free the memory
102626** occupied by the Parse object itself.
102627*/
102628static void renameParseCleanup(Parse *pParse){
102629 sqlite3 *db = pParse->db;
102630 if( pParse->pVdbe ){
102631 sqlite3VdbeFinalize(pParse->pVdbe);
102632 }
102633 sqlite3DeleteTable(db, pParse->pNewTable);
102634 if( pParse->pNewIndex ) sqlite3FreeIndex(db, pParse->pNewIndex);
102635 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
102636 sqlite3DbFree(db, pParse->zErrMsg);
102637 renameTokenFree(db, pParse->pRename);
102638 sqlite3ParserReset(pParse);
102639}
102640
102641/*
102642** SQL function:
102643**
102644** sqlite_rename_column(zSql, iCol, bQuote, zNew, zTable, zOld)
102645**
102646** 0. zSql: SQL statement to rewrite
102647** 1. type: Type of object ("table", "view" etc.)
102648** 2. object: Name of object
102649** 3. Database: Database name (e.g. "main")
102650** 4. Table: Table name
102651** 5. iCol: Index of column to rename
102652** 6. zNew: New column name
102653** 7. bQuote: Non-zero if the new column name should be quoted.
102654** 8. bTemp: True if zSql comes from temp schema
102655**
102656** Do a column rename operation on the CREATE statement given in zSql.
102657** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
102658** into zNew. The name should be quoted if bQuote is true.
102659**
102660** This function is used internally by the ALTER TABLE RENAME COLUMN command.
102661** It is only accessible to SQL created using sqlite3NestedParse(). It is
102662** not reachable from ordinary SQL passed into sqlite3_prepare().
102663*/
102664static void renameColumnFunc(
102665 sqlite3_context *context,
102666 int NotUsed,
102667 sqlite3_value **argv
102668){
102669 sqlite3 *db = sqlite3_context_db_handle(context);
102670 RenameCtx sCtx;
102671 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
102672 const char *zDb = (const char*)sqlite3_value_text(argv[3]);
102673 const char *zTable = (const char*)sqlite3_value_text(argv[4]);
102674 int iCol = sqlite3_value_int(argv[5]);
102675 const char *zNew = (const char*)sqlite3_value_text(argv[6]);
102676 int bQuote = sqlite3_value_int(argv[7]);
102677 int bTemp = sqlite3_value_int(argv[8]);
102678 const char *zOld;
102679 int rc;
102680 Parse sParse;
102681 Walker sWalker;
102682 Index *pIdx;
102683 int i;
102684 Table *pTab;
102685#ifndef SQLITE_OMIT_AUTHORIZATION
102686 sqlite3_xauth xAuth = db->xAuth;
102687#endif
102688
102689 UNUSED_PARAMETER(NotUsed);
102690 if( zSql==0 ) return;
102691 if( zTable==0 ) return;
102692 if( zNew==0 ) return;
102693 if( iCol<0 ) return;
102694 sqlite3BtreeEnterAll(db);
102695 pTab = sqlite3FindTable(db, zTable, zDb);
102696 if( pTab==0 || iCol>=pTab->nCol ){
102697 sqlite3BtreeLeaveAll(db);
102698 return;
102699 }
102700 zOld = pTab->aCol[iCol].zName;
102701 memset(&sCtx, 0, sizeof(sCtx));
102702 sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
102703
102704#ifndef SQLITE_OMIT_AUTHORIZATION
102705 db->xAuth = 0;
102706#endif
102707 rc = renameParseSql(&sParse, zDb, 0, db, zSql, bTemp);
102708
102709 /* Find tokens that need to be replaced. */
102710 memset(&sWalker, 0, sizeof(Walker));
102711 sWalker.pParse = &sParse;
102712 sWalker.xExprCallback = renameColumnExprCb;
102713 sWalker.xSelectCallback = renameColumnSelectCb;
102714 sWalker.u.pRename = &sCtx;
102715
102716 sCtx.pTab = pTab;
102717 if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
102718 if( sParse.pNewTable ){
102719 Select *pSelect = sParse.pNewTable->pSelect;
102720 if( pSelect ){
102721 sParse.rc = SQLITE_OK;
102722 sqlite3SelectPrep(&sParse, sParse.pNewTable->pSelect, 0);
102723 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
102724 if( rc==SQLITE_OK ){
102725 sqlite3WalkSelect(&sWalker, pSelect);
102726 }
102727 if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
102728 }else{
102729 /* A regular table */
102730 int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
102731 FKey *pFKey;
102732 assert( sParse.pNewTable->pSelect==0 );
102733 sCtx.pTab = sParse.pNewTable;
102734 if( bFKOnly==0 ){
102735 renameTokenFind(
102736 &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zName
102737 );
102738 if( sCtx.iCol<0 ){
102739 renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
102740 }
102741 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
102742 for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
102743 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
102744 }
102745 }
102746
102747 for(pFKey=sParse.pNewTable->pFKey; pFKey; pFKey=pFKey->pNextFrom){
102748 for(i=0; i<pFKey->nCol; i++){
102749 if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){
102750 renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
102751 }
102752 if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
102753 && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
102754 ){
102755 renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
102756 }
102757 }
102758 }
102759 }
102760 }else if( sParse.pNewIndex ){
102761 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
102762 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
102763 }else{
102764 /* A trigger */
102765 TriggerStep *pStep;
102766 rc = renameResolveTrigger(&sParse, (bTemp ? 0 : zDb));
102767 if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
102768
102769 for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){
102770 if( pStep->zTarget ){
102771 Table *pTarget = sqlite3LocateTable(&sParse, 0, pStep->zTarget, zDb);
102772 if( pTarget==pTab ){
102773 if( pStep->pUpsert ){
102774 ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet;
102775 renameColumnElistNames(&sParse, &sCtx, pUpsertSet, zOld);
102776 }
102777 renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld);
102778 renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld);
102779 }
102780 }
102781 }
102782
102783
102784 /* Find tokens to edit in UPDATE OF clause */
102785 if( sParse.pTriggerTab==pTab ){
102786 renameColumnIdlistNames(&sParse, &sCtx,sParse.pNewTrigger->pColumns,zOld);
102787 }
102788
102789 /* Find tokens to edit in various expressions and selects */
102790 renameWalkTrigger(&sWalker, sParse.pNewTrigger);
102791 }
102792
102793 assert( rc==SQLITE_OK );
102794 rc = renameEditSql(context, &sCtx, zSql, zNew, bQuote);
102795
102796renameColumnFunc_done:
102797 if( rc!=SQLITE_OK ){
102798 if( sParse.zErrMsg ){
102799 renameColumnParseError(context, 0, argv[1], argv[2], &sParse);
102800 }else{
102801 sqlite3_result_error_code(context, rc);
102802 }
102803 }
102804
102805 renameParseCleanup(&sParse);
102806 renameTokenFree(db, sCtx.pList);
102807#ifndef SQLITE_OMIT_AUTHORIZATION
102808 db->xAuth = xAuth;
102809#endif
102810 sqlite3BtreeLeaveAll(db);
102811}
102812
102813/*
102814** Walker expression callback used by "RENAME TABLE".
102815*/
102816static int renameTableExprCb(Walker *pWalker, Expr *pExpr){
102817 RenameCtx *p = pWalker->u.pRename;
102818 if( pExpr->op==TK_COLUMN && p->pTab==pExpr->y.pTab ){
102819 renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab);
102820 }
102821 return WRC_Continue;
102822}
102823
102824/*
102825** Walker select callback used by "RENAME TABLE".
102826*/
102827static int renameTableSelectCb(Walker *pWalker, Select *pSelect){
102828 int i;
102829 RenameCtx *p = pWalker->u.pRename;
102830 SrcList *pSrc = pSelect->pSrc;
102831 for(i=0; i<pSrc->nSrc; i++){
102832 struct SrcList_item *pItem = &pSrc->a[i];
102833 if( pItem->pTab==p->pTab ){
102834 renameTokenFind(pWalker->pParse, p, pItem->zName);
102835 }
102836 }
102837
102838 return WRC_Continue;
102839}
102840
102841
102842/*
102843** This C function implements an SQL user function that is used by SQL code
102844** generated by the ALTER TABLE ... RENAME command to modify the definition
102845** of any foreign key constraints that use the table being renamed as the
102846** parent table. It is passed three arguments:
102847**
102848** 0: The database containing the table being renamed.
102849** 1. type: Type of object ("table", "view" etc.)
102850** 2. object: Name of object
102851** 3: The complete text of the schema statement being modified,
102852** 4: The old name of the table being renamed, and
102853** 5: The new name of the table being renamed.
102854** 6: True if the schema statement comes from the temp db.
102855**
102856** It returns the new schema statement. For example:
102857**
102858** sqlite_rename_table('main', 'CREATE TABLE t1(a REFERENCES t2)','t2','t3',0)
102859** -> 'CREATE TABLE t1(a REFERENCES t3)'
102860*/
102861static void renameTableFunc(
102862 sqlite3_context *context,
102863 int NotUsed,
102864 sqlite3_value **argv
102865){
102866 sqlite3 *db = sqlite3_context_db_handle(context);
102867 const char *zDb = (const char*)sqlite3_value_text(argv[0]);
102868 const char *zInput = (const char*)sqlite3_value_text(argv[3]);
102869 const char *zOld = (const char*)sqlite3_value_text(argv[4]);
102870 const char *zNew = (const char*)sqlite3_value_text(argv[5]);
102871 int bTemp = sqlite3_value_int(argv[6]);
102872 UNUSED_PARAMETER(NotUsed);
102873
102874 if( zInput && zOld && zNew ){
102875 Parse sParse;
102876 int rc;
102877 int bQuote = 1;
102878 RenameCtx sCtx;
102879 Walker sWalker;
102880
102881#ifndef SQLITE_OMIT_AUTHORIZATION
102882 sqlite3_xauth xAuth = db->xAuth;
102883 db->xAuth = 0;
102884#endif
102885
102886 sqlite3BtreeEnterAll(db);
102887
102888 memset(&sCtx, 0, sizeof(RenameCtx));
102889 sCtx.pTab = sqlite3FindTable(db, zOld, zDb);
102890 memset(&sWalker, 0, sizeof(Walker));
102891 sWalker.pParse = &sParse;
102892 sWalker.xExprCallback = renameTableExprCb;
102893 sWalker.xSelectCallback = renameTableSelectCb;
102894 sWalker.u.pRename = &sCtx;
102895
102896 rc = renameParseSql(&sParse, zDb, 1, db, zInput, bTemp);
102897
102898 if( rc==SQLITE_OK ){
102899 int isLegacy = (db->flags & SQLITE_LegacyAlter);
102900 if( sParse.pNewTable ){
102901 Table *pTab = sParse.pNewTable;
102902
102903 if( pTab->pSelect ){
102904 if( isLegacy==0 ){
102905 NameContext sNC;
102906 memset(&sNC, 0, sizeof(sNC));
102907 sNC.pParse = &sParse;
102908
102909 sqlite3SelectPrep(&sParse, pTab->pSelect, &sNC);
102910 if( sParse.nErr ) rc = sParse.rc;
102911 sqlite3WalkSelect(&sWalker, pTab->pSelect);
102912 }
102913 }else{
102914 /* Modify any FK definitions to point to the new table. */
102915#ifndef SQLITE_OMIT_FOREIGN_KEY
102916 if( isLegacy==0 || (db->flags & SQLITE_ForeignKeys) ){
102917 FKey *pFKey;
102918 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
102919 if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
102920 renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
102921 }
102922 }
102923 }
102924#endif
102925
102926 /* If this is the table being altered, fix any table refs in CHECK
102927 ** expressions. Also update the name that appears right after the
102928 ** "CREATE [VIRTUAL] TABLE" bit. */
102929 if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
102930 sCtx.pTab = pTab;
102931 if( isLegacy==0 ){
102932 sqlite3WalkExprList(&sWalker, pTab->pCheck);
102933 }
102934 renameTokenFind(&sParse, &sCtx, pTab->zName);
102935 }
102936 }
102937 }
102938
102939 else if( sParse.pNewIndex ){
102940 renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
102941 if( isLegacy==0 ){
102942 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
102943 }
102944 }
102945
102946#ifndef SQLITE_OMIT_TRIGGER
102947 else{
102948 Trigger *pTrigger = sParse.pNewTrigger;
102949 TriggerStep *pStep;
102950 if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld)
102951 && sCtx.pTab->pSchema==pTrigger->pTabSchema
102952 ){
102953 renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
102954 }
102955
102956 if( isLegacy==0 ){
102957 rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
102958 if( rc==SQLITE_OK ){
102959 renameWalkTrigger(&sWalker, pTrigger);
102960 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
102961 if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
102962 renameTokenFind(&sParse, &sCtx, pStep->zTarget);
102963 }
102964 }
102965 }
102966 }
102967 }
102968#endif
102969 }
102970
102971 if( rc==SQLITE_OK ){
102972 rc = renameEditSql(context, &sCtx, zInput, zNew, bQuote);
102973 }
102974 if( rc!=SQLITE_OK ){
102975 if( sParse.zErrMsg ){
102976 renameColumnParseError(context, 0, argv[1], argv[2], &sParse);
102977 }else{
102978 sqlite3_result_error_code(context, rc);
102979 }
102980 }
102981
102982 renameParseCleanup(&sParse);
102983 renameTokenFree(db, sCtx.pList);
102984 sqlite3BtreeLeaveAll(db);
102985#ifndef SQLITE_OMIT_AUTHORIZATION
102986 db->xAuth = xAuth;
102987#endif
102988 }
102989
102990 return;
102991}
102992
102993/*
102994** An SQL user function that checks that there are no parse or symbol
102995** resolution problems in a CREATE TRIGGER|TABLE|VIEW|INDEX statement.
102996** After an ALTER TABLE .. RENAME operation is performed and the schema
102997** reloaded, this function is called on each SQL statement in the schema
102998** to ensure that it is still usable.
102999**
103000** 0: Database name ("main", "temp" etc.).
103001** 1: SQL statement.
103002** 2: Object type ("view", "table", "trigger" or "index").
103003** 3: Object name.
103004** 4: True if object is from temp schema.
103005**
103006** Unless it finds an error, this function normally returns NULL. However, it
103007** returns integer value 1 if:
103008**
103009** * the SQL argument creates a trigger, and
103010** * the table that the trigger is attached to is in database zDb.
103011*/
103012static void renameTableTest(
103013 sqlite3_context *context,
103014 int NotUsed,
103015 sqlite3_value **argv
103016){
103017 sqlite3 *db = sqlite3_context_db_handle(context);
103018 char const *zDb = (const char*)sqlite3_value_text(argv[0]);
103019 char const *zInput = (const char*)sqlite3_value_text(argv[1]);
103020 int bTemp = sqlite3_value_int(argv[4]);
103021 int isLegacy = (db->flags & SQLITE_LegacyAlter);
103022
103023#ifndef SQLITE_OMIT_AUTHORIZATION
103024 sqlite3_xauth xAuth = db->xAuth;
103025 db->xAuth = 0;
103026#endif
103027
103028 UNUSED_PARAMETER(NotUsed);
103029 if( zDb && zInput ){
103030 int rc;
103031 Parse sParse;
103032 rc = renameParseSql(&sParse, zDb, 1, db, zInput, bTemp);
103033 if( rc==SQLITE_OK ){
103034 if( isLegacy==0 && sParse.pNewTable && sParse.pNewTable->pSelect ){
103035 NameContext sNC;
103036 memset(&sNC, 0, sizeof(sNC));
103037 sNC.pParse = &sParse;
103038 sqlite3SelectPrep(&sParse, sParse.pNewTable->pSelect, &sNC);
103039 if( sParse.nErr ) rc = sParse.rc;
103040 }
103041
103042 else if( sParse.pNewTrigger ){
103043 if( isLegacy==0 ){
103044 rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
103045 }
103046 if( rc==SQLITE_OK ){
103047 int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
103048 int i2 = sqlite3FindDbName(db, zDb);
103049 if( i1==i2 ) sqlite3_result_int(context, 1);
103050 }
103051 }
103052 }
103053
103054 if( rc!=SQLITE_OK ){
103055 renameColumnParseError(context, 1, argv[2], argv[3], &sParse);
103056 }
103057 renameParseCleanup(&sParse);
103058 }
103059
103060#ifndef SQLITE_OMIT_AUTHORIZATION
103061 db->xAuth = xAuth;
103062#endif
103063}
103064
103065/*
103066** Register built-in functions used to help implement ALTER TABLE
103067*/
103068SQLITE_PRIVATE void sqlite3AlterFunctions(void){
103069 static FuncDef aAlterTableFuncs[] = {
103070 INTERNAL_FUNCTION(sqlite_rename_column, 9, renameColumnFunc),
103071 INTERNAL_FUNCTION(sqlite_rename_table, 7, renameTableFunc),
103072 INTERNAL_FUNCTION(sqlite_rename_test, 5, renameTableTest),
103073 };
103074 sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs));
103075}
103076#endif /* SQLITE_ALTER_TABLE */
103077
103078/************** End of alter.c ***********************************************/
103079/************** Begin file analyze.c *****************************************/
103080/*
103081** 2005-07-08
103082**
103083** The author disclaims copyright to this source code. In place of
103084** a legal notice, here is a blessing:
103085**
103086** May you do good and not evil.
103087** May you find forgiveness for yourself and forgive others.
103088** May you share freely, never taking more than you give.
103089**
103090*************************************************************************
103091** This file contains code associated with the ANALYZE command.
103092**
103093** The ANALYZE command gather statistics about the content of tables
103094** and indices. These statistics are made available to the query planner
103095** to help it make better decisions about how to perform queries.
103096**
103097** The following system tables are or have been supported:
103098**
103099** CREATE TABLE sqlite_stat1(tbl, idx, stat);
103100** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
103101** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
103102** CREATE TABLE sqlite_stat4(tbl, idx, nEq, nLt, nDLt, sample);
103103**
103104** Additional tables might be added in future releases of SQLite.
103105** The sqlite_stat2 table is not created or used unless the SQLite version
103106** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
103107** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated.
103108** The sqlite_stat2 table is superseded by sqlite_stat3, which is only
103109** created and used by SQLite versions 3.7.9 and later and with
103110** SQLITE_ENABLE_STAT3 defined. The functionality of sqlite_stat3
103111** is a superset of sqlite_stat2. The sqlite_stat4 is an enhanced
103112** version of sqlite_stat3 and is only available when compiled with
103113** SQLITE_ENABLE_STAT4 and in SQLite versions 3.8.1 and later. It is
103114** not possible to enable both STAT3 and STAT4 at the same time. If they
103115** are both enabled, then STAT4 takes precedence.
103116**
103117** For most applications, sqlite_stat1 provides all the statistics required
103118** for the query planner to make good choices.
103119**
103120** Format of sqlite_stat1:
103121**
103122** There is normally one row per index, with the index identified by the
103123** name in the idx column. The tbl column is the name of the table to
103124** which the index belongs. In each such row, the stat column will be
103125** a string consisting of a list of integers. The first integer in this
103126** list is the number of rows in the index. (This is the same as the
103127** number of rows in the table, except for partial indices.) The second
103128** integer is the average number of rows in the index that have the same
103129** value in the first column of the index. The third integer is the average
103130** number of rows in the index that have the same value for the first two
103131** columns. The N-th integer (for N>1) is the average number of rows in
103132** the index which have the same value for the first N-1 columns. For
103133** a K-column index, there will be K+1 integers in the stat column. If
103134** the index is unique, then the last integer will be 1.
103135**
103136** The list of integers in the stat column can optionally be followed
103137** by the keyword "unordered". The "unordered" keyword, if it is present,
103138** must be separated from the last integer by a single space. If the
103139** "unordered" keyword is present, then the query planner assumes that
103140** the index is unordered and will not use the index for a range query.
103141**
103142** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
103143** column contains a single integer which is the (estimated) number of
103144** rows in the table identified by sqlite_stat1.tbl.
103145**
103146** Format of sqlite_stat2:
103147**
103148** The sqlite_stat2 is only created and is only used if SQLite is compiled
103149** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
103150** 3.6.18 and 3.7.8. The "stat2" table contains additional information
103151** about the distribution of keys within an index. The index is identified by
103152** the "idx" column and the "tbl" column is the name of the table to which
103153** the index belongs. There are usually 10 rows in the sqlite_stat2
103154** table for each index.
103155**
103156** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
103157** inclusive are samples of the left-most key value in the index taken at
103158** evenly spaced points along the index. Let the number of samples be S
103159** (10 in the standard build) and let C be the number of rows in the index.
103160** Then the sampled rows are given by:
103161**
103162** rownumber = (i*C*2 + C)/(S*2)
103163**
103164** For i between 0 and S-1. Conceptually, the index space is divided into
103165** S uniform buckets and the samples are the middle row from each bucket.
103166**
103167** The format for sqlite_stat2 is recorded here for legacy reference. This
103168** version of SQLite does not support sqlite_stat2. It neither reads nor
103169** writes the sqlite_stat2 table. This version of SQLite only supports
103170** sqlite_stat3.
103171**
103172** Format for sqlite_stat3:
103173**
103174** The sqlite_stat3 format is a subset of sqlite_stat4. Hence, the
103175** sqlite_stat4 format will be described first. Further information
103176** about sqlite_stat3 follows the sqlite_stat4 description.
103177**
103178** Format for sqlite_stat4:
103179**
103180** As with sqlite_stat2, the sqlite_stat4 table contains histogram data
103181** to aid the query planner in choosing good indices based on the values
103182** that indexed columns are compared against in the WHERE clauses of
103183** queries.
103184**
103185** The sqlite_stat4 table contains multiple entries for each index.
103186** The idx column names the index and the tbl column is the table of the
103187** index. If the idx and tbl columns are the same, then the sample is
103188** of the INTEGER PRIMARY KEY. The sample column is a blob which is the
103189** binary encoding of a key from the index. The nEq column is a
103190** list of integers. The first integer is the approximate number
103191** of entries in the index whose left-most column exactly matches
103192** the left-most column of the sample. The second integer in nEq
103193** is the approximate number of entries in the index where the
103194** first two columns match the first two columns of the sample.
103195** And so forth. nLt is another list of integers that show the approximate
103196** number of entries that are strictly less than the sample. The first
103197** integer in nLt contains the number of entries in the index where the
103198** left-most column is less than the left-most column of the sample.
103199** The K-th integer in the nLt entry is the number of index entries
103200** where the first K columns are less than the first K columns of the
103201** sample. The nDLt column is like nLt except that it contains the
103202** number of distinct entries in the index that are less than the
103203** sample.
103204**
103205** There can be an arbitrary number of sqlite_stat4 entries per index.
103206** The ANALYZE command will typically generate sqlite_stat4 tables
103207** that contain between 10 and 40 samples which are distributed across
103208** the key space, though not uniformly, and which include samples with
103209** large nEq values.
103210**
103211** Format for sqlite_stat3 redux:
103212**
103213** The sqlite_stat3 table is like sqlite_stat4 except that it only
103214** looks at the left-most column of the index. The sqlite_stat3.sample
103215** column contains the actual value of the left-most column instead
103216** of a blob encoding of the complete index key as is found in
103217** sqlite_stat4.sample. The nEq, nLt, and nDLt entries of sqlite_stat3
103218** all contain just a single integer which is the same as the first
103219** integer in the equivalent columns in sqlite_stat4.
103220*/
103221#ifndef SQLITE_OMIT_ANALYZE
103222/* #include "sqliteInt.h" */
103223
103224#if defined(SQLITE_ENABLE_STAT4)
103225# define IsStat4 1
103226# define IsStat3 0
103227#elif defined(SQLITE_ENABLE_STAT3)
103228# define IsStat4 0
103229# define IsStat3 1
103230#else
103231# define IsStat4 0
103232# define IsStat3 0
103233# undef SQLITE_STAT4_SAMPLES
103234# define SQLITE_STAT4_SAMPLES 1
103235#endif
103236#define IsStat34 (IsStat3+IsStat4) /* 1 for STAT3 or STAT4. 0 otherwise */
103237
103238/*
103239** This routine generates code that opens the sqlite_statN tables.
103240** The sqlite_stat1 table is always relevant. sqlite_stat2 is now
103241** obsolete. sqlite_stat3 and sqlite_stat4 are only opened when
103242** appropriate compile-time options are provided.
103243**
103244** If the sqlite_statN tables do not previously exist, it is created.
103245**
103246** Argument zWhere may be a pointer to a buffer containing a table name,
103247** or it may be a NULL pointer. If it is not NULL, then all entries in
103248** the sqlite_statN tables associated with the named table are deleted.
103249** If zWhere==0, then code is generated to delete all stat table entries.
103250*/
103251static void openStatTable(
103252 Parse *pParse, /* Parsing context */
103253 int iDb, /* The database we are looking in */
103254 int iStatCur, /* Open the sqlite_stat1 table on this cursor */
103255 const char *zWhere, /* Delete entries for this table or index */
103256 const char *zWhereType /* Either "tbl" or "idx" */
103257){
103258 static const struct {
103259 const char *zName;
103260 const char *zCols;
103261 } aTable[] = {
103262 { "sqlite_stat1", "tbl,idx,stat" },
103263#if defined(SQLITE_ENABLE_STAT4)
103264 { "sqlite_stat4", "tbl,idx,neq,nlt,ndlt,sample" },
103265 { "sqlite_stat3", 0 },
103266#elif defined(SQLITE_ENABLE_STAT3)
103267 { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
103268 { "sqlite_stat4", 0 },
103269#else
103270 { "sqlite_stat3", 0 },
103271 { "sqlite_stat4", 0 },
103272#endif
103273 };
103274 int i;
103275 sqlite3 *db = pParse->db;
103276 Db *pDb;
103277 Vdbe *v = sqlite3GetVdbe(pParse);
103278 int aRoot[ArraySize(aTable)];
103279 u8 aCreateTbl[ArraySize(aTable)];
103280
103281 if( v==0 ) return;
103282 assert( sqlite3BtreeHoldsAllMutexes(db) );
103283 assert( sqlite3VdbeDb(v)==db );
103284 pDb = &db->aDb[iDb];
103285
103286 /* Create new statistic tables if they do not exist, or clear them
103287 ** if they do already exist.
103288 */
103289 for(i=0; i<ArraySize(aTable); i++){
103290 const char *zTab = aTable[i].zName;
103291 Table *pStat;
103292 if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
103293 if( aTable[i].zCols ){
103294 /* The sqlite_statN table does not exist. Create it. Note that a
103295 ** side-effect of the CREATE TABLE statement is to leave the rootpage
103296 ** of the new table in register pParse->regRoot. This is important
103297 ** because the OpenWrite opcode below will be needing it. */
103298 sqlite3NestedParse(pParse,
103299 "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols
103300 );
103301 aRoot[i] = pParse->regRoot;
103302 aCreateTbl[i] = OPFLAG_P2ISREG;
103303 }
103304 }else{
103305 /* The table already exists. If zWhere is not NULL, delete all entries
103306 ** associated with the table zWhere. If zWhere is NULL, delete the
103307 ** entire contents of the table. */
103308 aRoot[i] = pStat->tnum;
103309 aCreateTbl[i] = 0;
103310 sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
103311 if( zWhere ){
103312 sqlite3NestedParse(pParse,
103313 "DELETE FROM %Q.%s WHERE %s=%Q",
103314 pDb->zDbSName, zTab, zWhereType, zWhere
103315 );
103316#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
103317 }else if( db->xPreUpdateCallback ){
103318 sqlite3NestedParse(pParse, "DELETE FROM %Q.%s", pDb->zDbSName, zTab);
103319#endif
103320 }else{
103321 /* The sqlite_stat[134] table already exists. Delete all rows. */
103322 sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
103323 }
103324 }
103325 }
103326
103327 /* Open the sqlite_stat[134] tables for writing. */
103328 for(i=0; aTable[i].zCols; i++){
103329 assert( i<ArraySize(aTable) );
103330 sqlite3VdbeAddOp4Int(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb, 3);
103331 sqlite3VdbeChangeP5(v, aCreateTbl[i]);
103332 VdbeComment((v, aTable[i].zName));
103333 }
103334}
103335
103336/*
103337** Recommended number of samples for sqlite_stat4
103338*/
103339#ifndef SQLITE_STAT4_SAMPLES
103340# define SQLITE_STAT4_SAMPLES 24
103341#endif
103342
103343/*
103344** Three SQL functions - stat_init(), stat_push(), and stat_get() -
103345** share an instance of the following structure to hold their state
103346** information.
103347*/
103348typedef struct Stat4Accum Stat4Accum;
103349typedef struct Stat4Sample Stat4Sample;
103350struct Stat4Sample {
103351 tRowcnt *anEq; /* sqlite_stat4.nEq */
103352 tRowcnt *anDLt; /* sqlite_stat4.nDLt */
103353#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103354 tRowcnt *anLt; /* sqlite_stat4.nLt */
103355 union {
103356 i64 iRowid; /* Rowid in main table of the key */
103357 u8 *aRowid; /* Key for WITHOUT ROWID tables */
103358 } u;
103359 u32 nRowid; /* Sizeof aRowid[] */
103360 u8 isPSample; /* True if a periodic sample */
103361 int iCol; /* If !isPSample, the reason for inclusion */
103362 u32 iHash; /* Tiebreaker hash */
103363#endif
103364};
103365struct Stat4Accum {
103366 tRowcnt nRow; /* Number of rows in the entire table */
103367 tRowcnt nPSample; /* How often to do a periodic sample */
103368 int nCol; /* Number of columns in index + pk/rowid */
103369 int nKeyCol; /* Number of index columns w/o the pk/rowid */
103370 int mxSample; /* Maximum number of samples to accumulate */
103371 Stat4Sample current; /* Current row as a Stat4Sample */
103372 u32 iPrn; /* Pseudo-random number used for sampling */
103373 Stat4Sample *aBest; /* Array of nCol best samples */
103374 int iMin; /* Index in a[] of entry with minimum score */
103375 int nSample; /* Current number of samples */
103376 int nMaxEqZero; /* Max leading 0 in anEq[] for any a[] entry */
103377 int iGet; /* Index of current sample accessed by stat_get() */
103378 Stat4Sample *a; /* Array of mxSample Stat4Sample objects */
103379 sqlite3 *db; /* Database connection, for malloc() */
103380};
103381
103382/* Reclaim memory used by a Stat4Sample
103383*/
103384#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103385static void sampleClear(sqlite3 *db, Stat4Sample *p){
103386 assert( db!=0 );
103387 if( p->nRowid ){
103388 sqlite3DbFree(db, p->u.aRowid);
103389 p->nRowid = 0;
103390 }
103391}
103392#endif
103393
103394/* Initialize the BLOB value of a ROWID
103395*/
103396#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103397static void sampleSetRowid(sqlite3 *db, Stat4Sample *p, int n, const u8 *pData){
103398 assert( db!=0 );
103399 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
103400 p->u.aRowid = sqlite3DbMallocRawNN(db, n);
103401 if( p->u.aRowid ){
103402 p->nRowid = n;
103403 memcpy(p->u.aRowid, pData, n);
103404 }else{
103405 p->nRowid = 0;
103406 }
103407}
103408#endif
103409
103410/* Initialize the INTEGER value of a ROWID.
103411*/
103412#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103413static void sampleSetRowidInt64(sqlite3 *db, Stat4Sample *p, i64 iRowid){
103414 assert( db!=0 );
103415 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
103416 p->nRowid = 0;
103417 p->u.iRowid = iRowid;
103418}
103419#endif
103420
103421
103422/*
103423** Copy the contents of object (*pFrom) into (*pTo).
103424*/
103425#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103426static void sampleCopy(Stat4Accum *p, Stat4Sample *pTo, Stat4Sample *pFrom){
103427 pTo->isPSample = pFrom->isPSample;
103428 pTo->iCol = pFrom->iCol;
103429 pTo->iHash = pFrom->iHash;
103430 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
103431 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
103432 memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
103433 if( pFrom->nRowid ){
103434 sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
103435 }else{
103436 sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
103437 }
103438}
103439#endif
103440
103441/*
103442** Reclaim all memory of a Stat4Accum structure.
103443*/
103444static void stat4Destructor(void *pOld){
103445 Stat4Accum *p = (Stat4Accum*)pOld;
103446#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103447 int i;
103448 for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
103449 for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
103450 sampleClear(p->db, &p->current);
103451#endif
103452 sqlite3DbFree(p->db, p);
103453}
103454
103455/*
103456** Implementation of the stat_init(N,K,C) SQL function. The three parameters
103457** are:
103458** N: The number of columns in the index including the rowid/pk (note 1)
103459** K: The number of columns in the index excluding the rowid/pk.
103460** C: The number of rows in the index (note 2)
103461**
103462** Note 1: In the special case of the covering index that implements a
103463** WITHOUT ROWID table, N is the number of PRIMARY KEY columns, not the
103464** total number of columns in the table.
103465**
103466** Note 2: C is only used for STAT3 and STAT4.
103467**
103468** For indexes on ordinary rowid tables, N==K+1. But for indexes on
103469** WITHOUT ROWID tables, N=K+P where P is the number of columns in the
103470** PRIMARY KEY of the table. The covering index that implements the
103471** original WITHOUT ROWID table as N==K as a special case.
103472**
103473** This routine allocates the Stat4Accum object in heap memory. The return
103474** value is a pointer to the Stat4Accum object. The datatype of the
103475** return value is BLOB, but it is really just a pointer to the Stat4Accum
103476** object.
103477*/
103478static void statInit(
103479 sqlite3_context *context,
103480 int argc,
103481 sqlite3_value **argv
103482){
103483 Stat4Accum *p;
103484 int nCol; /* Number of columns in index being sampled */
103485 int nKeyCol; /* Number of key columns */
103486 int nColUp; /* nCol rounded up for alignment */
103487 int n; /* Bytes of space to allocate */
103488 sqlite3 *db; /* Database connection */
103489#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103490 int mxSample = SQLITE_STAT4_SAMPLES;
103491#endif
103492
103493 /* Decode the three function arguments */
103494 UNUSED_PARAMETER(argc);
103495 nCol = sqlite3_value_int(argv[0]);
103496 assert( nCol>0 );
103497 nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
103498 nKeyCol = sqlite3_value_int(argv[1]);
103499 assert( nKeyCol<=nCol );
103500 assert( nKeyCol>0 );
103501
103502 /* Allocate the space required for the Stat4Accum object */
103503 n = sizeof(*p)
103504 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anEq */
103505 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anDLt */
103506#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103507 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anLt */
103508 + sizeof(Stat4Sample)*(nCol+mxSample) /* Stat4Accum.aBest[], a[] */
103509 + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample)
103510#endif
103511 ;
103512 db = sqlite3_context_db_handle(context);
103513 p = sqlite3DbMallocZero(db, n);
103514 if( p==0 ){
103515 sqlite3_result_error_nomem(context);
103516 return;
103517 }
103518
103519 p->db = db;
103520 p->nRow = 0;
103521 p->nCol = nCol;
103522 p->nKeyCol = nKeyCol;
103523 p->current.anDLt = (tRowcnt*)&p[1];
103524 p->current.anEq = &p->current.anDLt[nColUp];
103525
103526#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103527 {
103528 u8 *pSpace; /* Allocated space not yet assigned */
103529 int i; /* Used to iterate through p->aSample[] */
103530
103531 p->iGet = -1;
103532 p->mxSample = mxSample;
103533 p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[2])/(mxSample/3+1) + 1);
103534 p->current.anLt = &p->current.anEq[nColUp];
103535 p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
103536
103537 /* Set up the Stat4Accum.a[] and aBest[] arrays */
103538 p->a = (struct Stat4Sample*)&p->current.anLt[nColUp];
103539 p->aBest = &p->a[mxSample];
103540 pSpace = (u8*)(&p->a[mxSample+nCol]);
103541 for(i=0; i<(mxSample+nCol); i++){
103542 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
103543 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
103544 p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
103545 }
103546 assert( (pSpace - (u8*)p)==n );
103547
103548 for(i=0; i<nCol; i++){
103549 p->aBest[i].iCol = i;
103550 }
103551 }
103552#endif
103553
103554 /* Return a pointer to the allocated object to the caller. Note that
103555 ** only the pointer (the 2nd parameter) matters. The size of the object
103556 ** (given by the 3rd parameter) is never used and can be any positive
103557 ** value. */
103558 sqlite3_result_blob(context, p, sizeof(*p), stat4Destructor);
103559}
103560static const FuncDef statInitFuncdef = {
103561 2+IsStat34, /* nArg */
103562 SQLITE_UTF8, /* funcFlags */
103563 0, /* pUserData */
103564 0, /* pNext */
103565 statInit, /* xSFunc */
103566 0, /* xFinalize */
103567 0, 0, /* xValue, xInverse */
103568 "stat_init", /* zName */
103569 {0}
103570};
103571
103572#ifdef SQLITE_ENABLE_STAT4
103573/*
103574** pNew and pOld are both candidate non-periodic samples selected for
103575** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
103576** considering only any trailing columns and the sample hash value, this
103577** function returns true if sample pNew is to be preferred over pOld.
103578** In other words, if we assume that the cardinalities of the selected
103579** column for pNew and pOld are equal, is pNew to be preferred over pOld.
103580**
103581** This function assumes that for each argument sample, the contents of
103582** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
103583*/
103584static int sampleIsBetterPost(
103585 Stat4Accum *pAccum,
103586 Stat4Sample *pNew,
103587 Stat4Sample *pOld
103588){
103589 int nCol = pAccum->nCol;
103590 int i;
103591 assert( pNew->iCol==pOld->iCol );
103592 for(i=pNew->iCol+1; i<nCol; i++){
103593 if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
103594 if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
103595 }
103596 if( pNew->iHash>pOld->iHash ) return 1;
103597 return 0;
103598}
103599#endif
103600
103601#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103602/*
103603** Return true if pNew is to be preferred over pOld.
103604**
103605** This function assumes that for each argument sample, the contents of
103606** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
103607*/
103608static int sampleIsBetter(
103609 Stat4Accum *pAccum,
103610 Stat4Sample *pNew,
103611 Stat4Sample *pOld
103612){
103613 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
103614 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
103615
103616 assert( pOld->isPSample==0 && pNew->isPSample==0 );
103617 assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
103618
103619 if( (nEqNew>nEqOld) ) return 1;
103620#ifdef SQLITE_ENABLE_STAT4
103621 if( nEqNew==nEqOld ){
103622 if( pNew->iCol<pOld->iCol ) return 1;
103623 return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
103624 }
103625 return 0;
103626#else
103627 return (nEqNew==nEqOld && pNew->iHash>pOld->iHash);
103628#endif
103629}
103630
103631/*
103632** Copy the contents of sample *pNew into the p->a[] array. If necessary,
103633** remove the least desirable sample from p->a[] to make room.
103634*/
103635static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){
103636 Stat4Sample *pSample = 0;
103637 int i;
103638
103639 assert( IsStat4 || nEqZero==0 );
103640
103641#ifdef SQLITE_ENABLE_STAT4
103642 /* Stat4Accum.nMaxEqZero is set to the maximum number of leading 0
103643 ** values in the anEq[] array of any sample in Stat4Accum.a[]. In
103644 ** other words, if nMaxEqZero is n, then it is guaranteed that there
103645 ** are no samples with Stat4Sample.anEq[m]==0 for (m>=n). */
103646 if( nEqZero>p->nMaxEqZero ){
103647 p->nMaxEqZero = nEqZero;
103648 }
103649 if( pNew->isPSample==0 ){
103650 Stat4Sample *pUpgrade = 0;
103651 assert( pNew->anEq[pNew->iCol]>0 );
103652
103653 /* This sample is being added because the prefix that ends in column
103654 ** iCol occurs many times in the table. However, if we have already
103655 ** added a sample that shares this prefix, there is no need to add
103656 ** this one. Instead, upgrade the priority of the highest priority
103657 ** existing sample that shares this prefix. */
103658 for(i=p->nSample-1; i>=0; i--){
103659 Stat4Sample *pOld = &p->a[i];
103660 if( pOld->anEq[pNew->iCol]==0 ){
103661 if( pOld->isPSample ) return;
103662 assert( pOld->iCol>pNew->iCol );
103663 assert( sampleIsBetter(p, pNew, pOld) );
103664 if( pUpgrade==0 || sampleIsBetter(p, pOld, pUpgrade) ){
103665 pUpgrade = pOld;
103666 }
103667 }
103668 }
103669 if( pUpgrade ){
103670 pUpgrade->iCol = pNew->iCol;
103671 pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
103672 goto find_new_min;
103673 }
103674 }
103675#endif
103676
103677 /* If necessary, remove sample iMin to make room for the new sample. */
103678 if( p->nSample>=p->mxSample ){
103679 Stat4Sample *pMin = &p->a[p->iMin];
103680 tRowcnt *anEq = pMin->anEq;
103681 tRowcnt *anLt = pMin->anLt;
103682 tRowcnt *anDLt = pMin->anDLt;
103683 sampleClear(p->db, pMin);
103684 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
103685 pSample = &p->a[p->nSample-1];
103686 pSample->nRowid = 0;
103687 pSample->anEq = anEq;
103688 pSample->anDLt = anDLt;
103689 pSample->anLt = anLt;
103690 p->nSample = p->mxSample-1;
103691 }
103692
103693 /* The "rows less-than" for the rowid column must be greater than that
103694 ** for the last sample in the p->a[] array. Otherwise, the samples would
103695 ** be out of order. */
103696#ifdef SQLITE_ENABLE_STAT4
103697 assert( p->nSample==0
103698 || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
103699#endif
103700
103701 /* Insert the new sample */
103702 pSample = &p->a[p->nSample];
103703 sampleCopy(p, pSample, pNew);
103704 p->nSample++;
103705
103706 /* Zero the first nEqZero entries in the anEq[] array. */
103707 memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
103708
103709#ifdef SQLITE_ENABLE_STAT4
103710 find_new_min:
103711#endif
103712 if( p->nSample>=p->mxSample ){
103713 int iMin = -1;
103714 for(i=0; i<p->mxSample; i++){
103715 if( p->a[i].isPSample ) continue;
103716 if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
103717 iMin = i;
103718 }
103719 }
103720 assert( iMin>=0 );
103721 p->iMin = iMin;
103722 }
103723}
103724#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
103725
103726/*
103727** Field iChng of the index being scanned has changed. So at this point
103728** p->current contains a sample that reflects the previous row of the
103729** index. The value of anEq[iChng] and subsequent anEq[] elements are
103730** correct at this point.
103731*/
103732static void samplePushPrevious(Stat4Accum *p, int iChng){
103733#ifdef SQLITE_ENABLE_STAT4
103734 int i;
103735
103736 /* Check if any samples from the aBest[] array should be pushed
103737 ** into IndexSample.a[] at this point. */
103738 for(i=(p->nCol-2); i>=iChng; i--){
103739 Stat4Sample *pBest = &p->aBest[i];
103740 pBest->anEq[i] = p->current.anEq[i];
103741 if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
103742 sampleInsert(p, pBest, i);
103743 }
103744 }
103745
103746 /* Check that no sample contains an anEq[] entry with an index of
103747 ** p->nMaxEqZero or greater set to zero. */
103748 for(i=p->nSample-1; i>=0; i--){
103749 int j;
103750 for(j=p->nMaxEqZero; j<p->nCol; j++) assert( p->a[i].anEq[j]>0 );
103751 }
103752
103753 /* Update the anEq[] fields of any samples already collected. */
103754 if( iChng<p->nMaxEqZero ){
103755 for(i=p->nSample-1; i>=0; i--){
103756 int j;
103757 for(j=iChng; j<p->nCol; j++){
103758 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
103759 }
103760 }
103761 p->nMaxEqZero = iChng;
103762 }
103763#endif
103764
103765#if defined(SQLITE_ENABLE_STAT3) && !defined(SQLITE_ENABLE_STAT4)
103766 if( iChng==0 ){
103767 tRowcnt nLt = p->current.anLt[0];
103768 tRowcnt nEq = p->current.anEq[0];
103769
103770 /* Check if this is to be a periodic sample. If so, add it. */
103771 if( (nLt/p->nPSample)!=(nLt+nEq)/p->nPSample ){
103772 p->current.isPSample = 1;
103773 sampleInsert(p, &p->current, 0);
103774 p->current.isPSample = 0;
103775 }else
103776
103777 /* Or if it is a non-periodic sample. Add it in this case too. */
103778 if( p->nSample<p->mxSample
103779 || sampleIsBetter(p, &p->current, &p->a[p->iMin])
103780 ){
103781 sampleInsert(p, &p->current, 0);
103782 }
103783 }
103784#endif
103785
103786#ifndef SQLITE_ENABLE_STAT3_OR_STAT4
103787 UNUSED_PARAMETER( p );
103788 UNUSED_PARAMETER( iChng );
103789#endif
103790}
103791
103792/*
103793** Implementation of the stat_push SQL function: stat_push(P,C,R)
103794** Arguments:
103795**
103796** P Pointer to the Stat4Accum object created by stat_init()
103797** C Index of left-most column to differ from previous row
103798** R Rowid for the current row. Might be a key record for
103799** WITHOUT ROWID tables.
103800**
103801** This SQL function always returns NULL. It's purpose it to accumulate
103802** statistical data and/or samples in the Stat4Accum object about the
103803** index being analyzed. The stat_get() SQL function will later be used to
103804** extract relevant information for constructing the sqlite_statN tables.
103805**
103806** The R parameter is only used for STAT3 and STAT4
103807*/
103808static void statPush(
103809 sqlite3_context *context,
103810 int argc,
103811 sqlite3_value **argv
103812){
103813 int i;
103814
103815 /* The three function arguments */
103816 Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
103817 int iChng = sqlite3_value_int(argv[1]);
103818
103819 UNUSED_PARAMETER( argc );
103820 UNUSED_PARAMETER( context );
103821 assert( p->nCol>0 );
103822 assert( iChng<p->nCol );
103823
103824 if( p->nRow==0 ){
103825 /* This is the first call to this function. Do initialization. */
103826 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
103827 }else{
103828 /* Second and subsequent calls get processed here */
103829 samplePushPrevious(p, iChng);
103830
103831 /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
103832 ** to the current row of the index. */
103833 for(i=0; i<iChng; i++){
103834 p->current.anEq[i]++;
103835 }
103836 for(i=iChng; i<p->nCol; i++){
103837 p->current.anDLt[i]++;
103838#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103839 p->current.anLt[i] += p->current.anEq[i];
103840#endif
103841 p->current.anEq[i] = 1;
103842 }
103843 }
103844 p->nRow++;
103845#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103846 if( sqlite3_value_type(argv[2])==SQLITE_INTEGER ){
103847 sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
103848 }else{
103849 sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
103850 sqlite3_value_blob(argv[2]));
103851 }
103852 p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
103853#endif
103854
103855#ifdef SQLITE_ENABLE_STAT4
103856 {
103857 tRowcnt nLt = p->current.anLt[p->nCol-1];
103858
103859 /* Check if this is to be a periodic sample. If so, add it. */
103860 if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
103861 p->current.isPSample = 1;
103862 p->current.iCol = 0;
103863 sampleInsert(p, &p->current, p->nCol-1);
103864 p->current.isPSample = 0;
103865 }
103866
103867 /* Update the aBest[] array. */
103868 for(i=0; i<(p->nCol-1); i++){
103869 p->current.iCol = i;
103870 if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
103871 sampleCopy(p, &p->aBest[i], &p->current);
103872 }
103873 }
103874 }
103875#endif
103876}
103877static const FuncDef statPushFuncdef = {
103878 2+IsStat34, /* nArg */
103879 SQLITE_UTF8, /* funcFlags */
103880 0, /* pUserData */
103881 0, /* pNext */
103882 statPush, /* xSFunc */
103883 0, /* xFinalize */
103884 0, 0, /* xValue, xInverse */
103885 "stat_push", /* zName */
103886 {0}
103887};
103888
103889#define STAT_GET_STAT1 0 /* "stat" column of stat1 table */
103890#define STAT_GET_ROWID 1 /* "rowid" column of stat[34] entry */
103891#define STAT_GET_NEQ 2 /* "neq" column of stat[34] entry */
103892#define STAT_GET_NLT 3 /* "nlt" column of stat[34] entry */
103893#define STAT_GET_NDLT 4 /* "ndlt" column of stat[34] entry */
103894
103895/*
103896** Implementation of the stat_get(P,J) SQL function. This routine is
103897** used to query statistical information that has been gathered into
103898** the Stat4Accum object by prior calls to stat_push(). The P parameter
103899** has type BLOB but it is really just a pointer to the Stat4Accum object.
103900** The content to returned is determined by the parameter J
103901** which is one of the STAT_GET_xxxx values defined above.
103902**
103903** The stat_get(P,J) function is not available to generic SQL. It is
103904** inserted as part of a manually constructed bytecode program. (See
103905** the callStatGet() routine below.) It is guaranteed that the P
103906** parameter will always be a poiner to a Stat4Accum object, never a
103907** NULL.
103908**
103909** If neither STAT3 nor STAT4 are enabled, then J is always
103910** STAT_GET_STAT1 and is hence omitted and this routine becomes
103911** a one-parameter function, stat_get(P), that always returns the
103912** stat1 table entry information.
103913*/
103914static void statGet(
103915 sqlite3_context *context,
103916 int argc,
103917 sqlite3_value **argv
103918){
103919 Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
103920#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103921 /* STAT3 and STAT4 have a parameter on this routine. */
103922 int eCall = sqlite3_value_int(argv[1]);
103923 assert( argc==2 );
103924 assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ
103925 || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT
103926 || eCall==STAT_GET_NDLT
103927 );
103928 if( eCall==STAT_GET_STAT1 )
103929#else
103930 assert( argc==1 );
103931#endif
103932 {
103933 /* Return the value to store in the "stat" column of the sqlite_stat1
103934 ** table for this index.
103935 **
103936 ** The value is a string composed of a list of integers describing
103937 ** the index. The first integer in the list is the total number of
103938 ** entries in the index. There is one additional integer in the list
103939 ** for each indexed column. This additional integer is an estimate of
103940 ** the number of rows matched by a stabbing query on the index using
103941 ** a key with the corresponding number of fields. In other words,
103942 ** if the index is on columns (a,b) and the sqlite_stat1 value is
103943 ** "100 10 2", then SQLite estimates that:
103944 **
103945 ** * the index contains 100 rows,
103946 ** * "WHERE a=?" matches 10 rows, and
103947 ** * "WHERE a=? AND b=?" matches 2 rows.
103948 **
103949 ** If D is the count of distinct values and K is the total number of
103950 ** rows, then each estimate is computed as:
103951 **
103952 ** I = (K+D-1)/D
103953 */
103954 char *z;
103955 int i;
103956
103957 char *zRet = sqlite3MallocZero( (p->nKeyCol+1)*25 );
103958 if( zRet==0 ){
103959 sqlite3_result_error_nomem(context);
103960 return;
103961 }
103962
103963 sqlite3_snprintf(24, zRet, "%llu", (u64)p->nRow);
103964 z = zRet + sqlite3Strlen30(zRet);
103965 for(i=0; i<p->nKeyCol; i++){
103966 u64 nDistinct = p->current.anDLt[i] + 1;
103967 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
103968 sqlite3_snprintf(24, z, " %llu", iVal);
103969 z += sqlite3Strlen30(z);
103970 assert( p->current.anEq[i] );
103971 }
103972 assert( z[0]=='\0' && z>zRet );
103973
103974 sqlite3_result_text(context, zRet, -1, sqlite3_free);
103975 }
103976#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103977 else if( eCall==STAT_GET_ROWID ){
103978 if( p->iGet<0 ){
103979 samplePushPrevious(p, 0);
103980 p->iGet = 0;
103981 }
103982 if( p->iGet<p->nSample ){
103983 Stat4Sample *pS = p->a + p->iGet;
103984 if( pS->nRowid==0 ){
103985 sqlite3_result_int64(context, pS->u.iRowid);
103986 }else{
103987 sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
103988 SQLITE_TRANSIENT);
103989 }
103990 }
103991 }else{
103992 tRowcnt *aCnt = 0;
103993
103994 assert( p->iGet<p->nSample );
103995 switch( eCall ){
103996 case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break;
103997 case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break;
103998 default: {
103999 aCnt = p->a[p->iGet].anDLt;
104000 p->iGet++;
104001 break;
104002 }
104003 }
104004
104005 if( IsStat3 ){
104006 sqlite3_result_int64(context, (i64)aCnt[0]);
104007 }else{
104008 char *zRet = sqlite3MallocZero(p->nCol * 25);
104009 if( zRet==0 ){
104010 sqlite3_result_error_nomem(context);
104011 }else{
104012 int i;
104013 char *z = zRet;
104014 for(i=0; i<p->nCol; i++){
104015 sqlite3_snprintf(24, z, "%llu ", (u64)aCnt[i]);
104016 z += sqlite3Strlen30(z);
104017 }
104018 assert( z[0]=='\0' && z>zRet );
104019 z[-1] = '\0';
104020 sqlite3_result_text(context, zRet, -1, sqlite3_free);
104021 }
104022 }
104023 }
104024#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
104025#ifndef SQLITE_DEBUG
104026 UNUSED_PARAMETER( argc );
104027#endif
104028}
104029static const FuncDef statGetFuncdef = {
104030 1+IsStat34, /* nArg */
104031 SQLITE_UTF8, /* funcFlags */
104032 0, /* pUserData */
104033 0, /* pNext */
104034 statGet, /* xSFunc */
104035 0, /* xFinalize */
104036 0, 0, /* xValue, xInverse */
104037 "stat_get", /* zName */
104038 {0}
104039};
104040
104041static void callStatGet(Vdbe *v, int regStat4, int iParam, int regOut){
104042 assert( regOut!=regStat4 && regOut!=regStat4+1 );
104043#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104044 sqlite3VdbeAddOp2(v, OP_Integer, iParam, regStat4+1);
104045#elif SQLITE_DEBUG
104046 assert( iParam==STAT_GET_STAT1 );
104047#else
104048 UNUSED_PARAMETER( iParam );
104049#endif
104050 sqlite3VdbeAddOp4(v, OP_Function0, 0, regStat4, regOut,
104051 (char*)&statGetFuncdef, P4_FUNCDEF);
104052 sqlite3VdbeChangeP5(v, 1 + IsStat34);
104053}
104054
104055/*
104056** Generate code to do an analysis of all indices associated with
104057** a single table.
104058*/
104059static void analyzeOneTable(
104060 Parse *pParse, /* Parser context */
104061 Table *pTab, /* Table whose indices are to be analyzed */
104062 Index *pOnlyIdx, /* If not NULL, only analyze this one index */
104063 int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */
104064 int iMem, /* Available memory locations begin here */
104065 int iTab /* Next available cursor */
104066){
104067 sqlite3 *db = pParse->db; /* Database handle */
104068 Index *pIdx; /* An index to being analyzed */
104069 int iIdxCur; /* Cursor open on index being analyzed */
104070 int iTabCur; /* Table cursor */
104071 Vdbe *v; /* The virtual machine being built up */
104072 int i; /* Loop counter */
104073 int jZeroRows = -1; /* Jump from here if number of rows is zero */
104074 int iDb; /* Index of database containing pTab */
104075 u8 needTableCnt = 1; /* True to count the table */
104076 int regNewRowid = iMem++; /* Rowid for the inserted record */
104077 int regStat4 = iMem++; /* Register to hold Stat4Accum object */
104078 int regChng = iMem++; /* Index of changed index field */
104079#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104080 int regRowid = iMem++; /* Rowid argument passed to stat_push() */
104081#endif
104082 int regTemp = iMem++; /* Temporary use register */
104083 int regTabname = iMem++; /* Register containing table name */
104084 int regIdxname = iMem++; /* Register containing index name */
104085 int regStat1 = iMem++; /* Value for the stat column of sqlite_stat1 */
104086 int regPrev = iMem; /* MUST BE LAST (see below) */
104087#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
104088 Table *pStat1 = 0;
104089#endif
104090
104091 pParse->nMem = MAX(pParse->nMem, iMem);
104092 v = sqlite3GetVdbe(pParse);
104093 if( v==0 || NEVER(pTab==0) ){
104094 return;
104095 }
104096 if( pTab->tnum==0 ){
104097 /* Do not gather statistics on views or virtual tables */
104098 return;
104099 }
104100 if( sqlite3_strlike("sqlite\\_%", pTab->zName, '\\')==0 ){
104101 /* Do not gather statistics on system tables */
104102 return;
104103 }
104104 assert( sqlite3BtreeHoldsAllMutexes(db) );
104105 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
104106 assert( iDb>=0 );
104107 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
104108#ifndef SQLITE_OMIT_AUTHORIZATION
104109 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
104110 db->aDb[iDb].zDbSName ) ){
104111 return;
104112 }
104113#endif
104114
104115#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
104116 if( db->xPreUpdateCallback ){
104117 pStat1 = (Table*)sqlite3DbMallocZero(db, sizeof(Table) + 13);
104118 if( pStat1==0 ) return;
104119 pStat1->zName = (char*)&pStat1[1];
104120 memcpy(pStat1->zName, "sqlite_stat1", 13);
104121 pStat1->nCol = 3;
104122 pStat1->iPKey = -1;
104123 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Noop, 0, 0, 0,(char*)pStat1,P4_DYNBLOB);
104124 }
104125#endif
104126
104127 /* Establish a read-lock on the table at the shared-cache level.
104128 ** Open a read-only cursor on the table. Also allocate a cursor number
104129 ** to use for scanning indexes (iIdxCur). No index cursor is opened at
104130 ** this time though. */
104131 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
104132 iTabCur = iTab++;
104133 iIdxCur = iTab++;
104134 pParse->nTab = MAX(pParse->nTab, iTab);
104135 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
104136 sqlite3VdbeLoadString(v, regTabname, pTab->zName);
104137
104138 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
104139 int nCol; /* Number of columns in pIdx. "N" */
104140 int addrRewind; /* Address of "OP_Rewind iIdxCur" */
104141 int addrNextRow; /* Address of "next_row:" */
104142 const char *zIdxName; /* Name of the index */
104143 int nColTest; /* Number of columns to test for changes */
104144
104145 if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
104146 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
104147 if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIdx) ){
104148 nCol = pIdx->nKeyCol;
104149 zIdxName = pTab->zName;
104150 nColTest = nCol - 1;
104151 }else{
104152 nCol = pIdx->nColumn;
104153 zIdxName = pIdx->zName;
104154 nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
104155 }
104156
104157 /* Populate the register containing the index name. */
104158 sqlite3VdbeLoadString(v, regIdxname, zIdxName);
104159 VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
104160
104161 /*
104162 ** Pseudo-code for loop that calls stat_push():
104163 **
104164 ** Rewind csr
104165 ** if eof(csr) goto end_of_scan;
104166 ** regChng = 0
104167 ** goto chng_addr_0;
104168 **
104169 ** next_row:
104170 ** regChng = 0
104171 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
104172 ** regChng = 1
104173 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
104174 ** ...
104175 ** regChng = N
104176 ** goto chng_addr_N
104177 **
104178 ** chng_addr_0:
104179 ** regPrev(0) = idx(0)
104180 ** chng_addr_1:
104181 ** regPrev(1) = idx(1)
104182 ** ...
104183 **
104184 ** endDistinctTest:
104185 ** regRowid = idx(rowid)
104186 ** stat_push(P, regChng, regRowid)
104187 ** Next csr
104188 ** if !eof(csr) goto next_row;
104189 **
104190 ** end_of_scan:
104191 */
104192
104193 /* Make sure there are enough memory cells allocated to accommodate
104194 ** the regPrev array and a trailing rowid (the rowid slot is required
104195 ** when building a record to insert into the sample column of
104196 ** the sqlite_stat4 table. */
104197 pParse->nMem = MAX(pParse->nMem, regPrev+nColTest);
104198
104199 /* Open a read-only cursor on the index being analyzed. */
104200 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
104201 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
104202 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
104203 VdbeComment((v, "%s", pIdx->zName));
104204
104205 /* Invoke the stat_init() function. The arguments are:
104206 **
104207 ** (1) the number of columns in the index including the rowid
104208 ** (or for a WITHOUT ROWID table, the number of PK columns),
104209 ** (2) the number of columns in the key without the rowid/pk
104210 ** (3) the number of rows in the index,
104211 **
104212 **
104213 ** The third argument is only used for STAT3 and STAT4
104214 */
104215#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104216 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+3);
104217#endif
104218 sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat4+1);
104219 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regStat4+2);
104220 sqlite3VdbeAddOp4(v, OP_Function0, 0, regStat4+1, regStat4,
104221 (char*)&statInitFuncdef, P4_FUNCDEF);
104222 sqlite3VdbeChangeP5(v, 2+IsStat34);
104223
104224 /* Implementation of the following:
104225 **
104226 ** Rewind csr
104227 ** if eof(csr) goto end_of_scan;
104228 ** regChng = 0
104229 ** goto next_push_0;
104230 **
104231 */
104232 addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
104233 VdbeCoverage(v);
104234 sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng);
104235 addrNextRow = sqlite3VdbeCurrentAddr(v);
104236
104237 if( nColTest>0 ){
104238 int endDistinctTest = sqlite3VdbeMakeLabel(v);
104239 int *aGotoChng; /* Array of jump instruction addresses */
104240 aGotoChng = sqlite3DbMallocRawNN(db, sizeof(int)*nColTest);
104241 if( aGotoChng==0 ) continue;
104242
104243 /*
104244 ** next_row:
104245 ** regChng = 0
104246 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
104247 ** regChng = 1
104248 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
104249 ** ...
104250 ** regChng = N
104251 ** goto endDistinctTest
104252 */
104253 sqlite3VdbeAddOp0(v, OP_Goto);
104254 addrNextRow = sqlite3VdbeCurrentAddr(v);
104255 if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){
104256 /* For a single-column UNIQUE index, once we have found a non-NULL
104257 ** row, we know that all the rest will be distinct, so skip
104258 ** subsequent distinctness tests. */
104259 sqlite3VdbeAddOp2(v, OP_NotNull, regPrev, endDistinctTest);
104260 VdbeCoverage(v);
104261 }
104262 for(i=0; i<nColTest; i++){
104263 char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
104264 sqlite3VdbeAddOp2(v, OP_Integer, i, regChng);
104265 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp);
104266 aGotoChng[i] =
104267 sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ);
104268 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
104269 VdbeCoverage(v);
104270 }
104271 sqlite3VdbeAddOp2(v, OP_Integer, nColTest, regChng);
104272 sqlite3VdbeGoto(v, endDistinctTest);
104273
104274
104275 /*
104276 ** chng_addr_0:
104277 ** regPrev(0) = idx(0)
104278 ** chng_addr_1:
104279 ** regPrev(1) = idx(1)
104280 ** ...
104281 */
104282 sqlite3VdbeJumpHere(v, addrNextRow-1);
104283 for(i=0; i<nColTest; i++){
104284 sqlite3VdbeJumpHere(v, aGotoChng[i]);
104285 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regPrev+i);
104286 }
104287 sqlite3VdbeResolveLabel(v, endDistinctTest);
104288 sqlite3DbFree(db, aGotoChng);
104289 }
104290
104291 /*
104292 ** chng_addr_N:
104293 ** regRowid = idx(rowid) // STAT34 only
104294 ** stat_push(P, regChng, regRowid) // 3rd parameter STAT34 only
104295 ** Next csr
104296 ** if !eof(csr) goto next_row;
104297 */
104298#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104299 assert( regRowid==(regStat4+2) );
104300 if( HasRowid(pTab) ){
104301 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
104302 }else{
104303 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
104304 int j, k, regKey;
104305 regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
104306 for(j=0; j<pPk->nKeyCol; j++){
104307 k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
104308 assert( k>=0 && k<pIdx->nColumn );
104309 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, regKey+j);
104310 VdbeComment((v, "%s", pTab->aCol[pPk->aiColumn[j]].zName));
104311 }
104312 sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
104313 sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
104314 }
104315#endif
104316 assert( regChng==(regStat4+1) );
104317 sqlite3VdbeAddOp4(v, OP_Function0, 1, regStat4, regTemp,
104318 (char*)&statPushFuncdef, P4_FUNCDEF);
104319 sqlite3VdbeChangeP5(v, 2+IsStat34);
104320 sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v);
104321
104322 /* Add the entry to the stat1 table. */
104323 callStatGet(v, regStat4, STAT_GET_STAT1, regStat1);
104324 assert( "BBB"[0]==SQLITE_AFF_TEXT );
104325 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0);
104326 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
104327 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
104328#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
104329 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
104330#endif
104331 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
104332
104333 /* Add the entries to the stat3 or stat4 table. */
104334#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104335 {
104336 int regEq = regStat1;
104337 int regLt = regStat1+1;
104338 int regDLt = regStat1+2;
104339 int regSample = regStat1+3;
104340 int regCol = regStat1+4;
104341 int regSampleRowid = regCol + nCol;
104342 int addrNext;
104343 int addrIsNull;
104344 u8 seekOp = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
104345
104346 pParse->nMem = MAX(pParse->nMem, regCol+nCol);
104347
104348 addrNext = sqlite3VdbeCurrentAddr(v);
104349 callStatGet(v, regStat4, STAT_GET_ROWID, regSampleRowid);
104350 addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid);
104351 VdbeCoverage(v);
104352 callStatGet(v, regStat4, STAT_GET_NEQ, regEq);
104353 callStatGet(v, regStat4, STAT_GET_NLT, regLt);
104354 callStatGet(v, regStat4, STAT_GET_NDLT, regDLt);
104355 sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0);
104356 VdbeCoverage(v);
104357#ifdef SQLITE_ENABLE_STAT3
104358 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, 0, regSample);
104359#else
104360 for(i=0; i<nCol; i++){
104361 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, i, regCol+i);
104362 }
104363 sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol, regSample);
104364#endif
104365 sqlite3VdbeAddOp3(v, OP_MakeRecord, regTabname, 6, regTemp);
104366 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
104367 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid);
104368 sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
104369 sqlite3VdbeJumpHere(v, addrIsNull);
104370 }
104371#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
104372
104373 /* End of analysis */
104374 sqlite3VdbeJumpHere(v, addrRewind);
104375 }
104376
104377
104378 /* Create a single sqlite_stat1 entry containing NULL as the index
104379 ** name and the row count as the content.
104380 */
104381 if( pOnlyIdx==0 && needTableCnt ){
104382 VdbeComment((v, "%s", pTab->zName));
104383 sqlite3VdbeAddOp2(v, OP_Count, iTabCur, regStat1);
104384 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1); VdbeCoverage(v);
104385 sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
104386 assert( "BBB"[0]==SQLITE_AFF_TEXT );
104387 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0);
104388 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
104389 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
104390 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
104391#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
104392 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
104393#endif
104394 sqlite3VdbeJumpHere(v, jZeroRows);
104395 }
104396}
104397
104398
104399/*
104400** Generate code that will cause the most recent index analysis to
104401** be loaded into internal hash tables where is can be used.
104402*/
104403static void loadAnalysis(Parse *pParse, int iDb){
104404 Vdbe *v = sqlite3GetVdbe(pParse);
104405 if( v ){
104406 sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb);
104407 }
104408}
104409
104410/*
104411** Generate code that will do an analysis of an entire database
104412*/
104413static void analyzeDatabase(Parse *pParse, int iDb){
104414 sqlite3 *db = pParse->db;
104415 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
104416 HashElem *k;
104417 int iStatCur;
104418 int iMem;
104419 int iTab;
104420
104421 sqlite3BeginWriteOperation(pParse, 0, iDb);
104422 iStatCur = pParse->nTab;
104423 pParse->nTab += 3;
104424 openStatTable(pParse, iDb, iStatCur, 0, 0);
104425 iMem = pParse->nMem+1;
104426 iTab = pParse->nTab;
104427 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
104428 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
104429 Table *pTab = (Table*)sqliteHashData(k);
104430 analyzeOneTable(pParse, pTab, 0, iStatCur, iMem, iTab);
104431 }
104432 loadAnalysis(pParse, iDb);
104433}
104434
104435/*
104436** Generate code that will do an analysis of a single table in
104437** a database. If pOnlyIdx is not NULL then it is a single index
104438** in pTab that should be analyzed.
104439*/
104440static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
104441 int iDb;
104442 int iStatCur;
104443
104444 assert( pTab!=0 );
104445 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
104446 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
104447 sqlite3BeginWriteOperation(pParse, 0, iDb);
104448 iStatCur = pParse->nTab;
104449 pParse->nTab += 3;
104450 if( pOnlyIdx ){
104451 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
104452 }else{
104453 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
104454 }
104455 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
104456 loadAnalysis(pParse, iDb);
104457}
104458
104459/*
104460** Generate code for the ANALYZE command. The parser calls this routine
104461** when it recognizes an ANALYZE command.
104462**
104463** ANALYZE -- 1
104464** ANALYZE <database> -- 2
104465** ANALYZE ?<database>.?<tablename> -- 3
104466**
104467** Form 1 causes all indices in all attached databases to be analyzed.
104468** Form 2 analyzes all indices the single database named.
104469** Form 3 analyzes all indices associated with the named table.
104470*/
104471SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
104472 sqlite3 *db = pParse->db;
104473 int iDb;
104474 int i;
104475 char *z, *zDb;
104476 Table *pTab;
104477 Index *pIdx;
104478 Token *pTableName;
104479 Vdbe *v;
104480
104481 /* Read the database schema. If an error occurs, leave an error message
104482 ** and code in pParse and return NULL. */
104483 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
104484 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
104485 return;
104486 }
104487
104488 assert( pName2!=0 || pName1==0 );
104489 if( pName1==0 ){
104490 /* Form 1: Analyze everything */
104491 for(i=0; i<db->nDb; i++){
104492 if( i==1 ) continue; /* Do not analyze the TEMP database */
104493 analyzeDatabase(pParse, i);
104494 }
104495 }else if( pName2->n==0 && (iDb = sqlite3FindDb(db, pName1))>=0 ){
104496 /* Analyze the schema named as the argument */
104497 analyzeDatabase(pParse, iDb);
104498 }else{
104499 /* Form 3: Analyze the table or index named as an argument */
104500 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
104501 if( iDb>=0 ){
104502 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
104503 z = sqlite3NameFromToken(db, pTableName);
104504 if( z ){
104505 if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
104506 analyzeTable(pParse, pIdx->pTable, pIdx);
104507 }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
104508 analyzeTable(pParse, pTab, 0);
104509 }
104510 sqlite3DbFree(db, z);
104511 }
104512 }
104513 }
104514 if( db->nSqlExec==0 && (v = sqlite3GetVdbe(pParse))!=0 ){
104515 sqlite3VdbeAddOp0(v, OP_Expire);
104516 }
104517}
104518
104519/*
104520** Used to pass information from the analyzer reader through to the
104521** callback routine.
104522*/
104523typedef struct analysisInfo analysisInfo;
104524struct analysisInfo {
104525 sqlite3 *db;
104526 const char *zDatabase;
104527};
104528
104529/*
104530** The first argument points to a nul-terminated string containing a
104531** list of space separated integers. Read the first nOut of these into
104532** the array aOut[].
104533*/
104534static void decodeIntArray(
104535 char *zIntArray, /* String containing int array to decode */
104536 int nOut, /* Number of slots in aOut[] */
104537 tRowcnt *aOut, /* Store integers here */
104538 LogEst *aLog, /* Or, if aOut==0, here */
104539 Index *pIndex /* Handle extra flags for this index, if not NULL */
104540){
104541 char *z = zIntArray;
104542 int c;
104543 int i;
104544 tRowcnt v;
104545
104546#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104547 if( z==0 ) z = "";
104548#else
104549 assert( z!=0 );
104550#endif
104551 for(i=0; *z && i<nOut; i++){
104552 v = 0;
104553 while( (c=z[0])>='0' && c<='9' ){
104554 v = v*10 + c - '0';
104555 z++;
104556 }
104557#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104558 if( aOut ) aOut[i] = v;
104559 if( aLog ) aLog[i] = sqlite3LogEst(v);
104560#else
104561 assert( aOut==0 );
104562 UNUSED_PARAMETER(aOut);
104563 assert( aLog!=0 );
104564 aLog[i] = sqlite3LogEst(v);
104565#endif
104566 if( *z==' ' ) z++;
104567 }
104568#ifndef SQLITE_ENABLE_STAT3_OR_STAT4
104569 assert( pIndex!=0 ); {
104570#else
104571 if( pIndex ){
104572#endif
104573 pIndex->bUnordered = 0;
104574 pIndex->noSkipScan = 0;
104575 while( z[0] ){
104576 if( sqlite3_strglob("unordered*", z)==0 ){
104577 pIndex->bUnordered = 1;
104578 }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
104579 pIndex->szIdxRow = sqlite3LogEst(sqlite3Atoi(z+3));
104580 }else if( sqlite3_strglob("noskipscan*", z)==0 ){
104581 pIndex->noSkipScan = 1;
104582 }
104583#ifdef SQLITE_ENABLE_COSTMULT
104584 else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
104585 pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
104586 }
104587#endif
104588 while( z[0]!=0 && z[0]!=' ' ) z++;
104589 while( z[0]==' ' ) z++;
104590 }
104591 }
104592}
104593
104594/*
104595** This callback is invoked once for each index when reading the
104596** sqlite_stat1 table.
104597**
104598** argv[0] = name of the table
104599** argv[1] = name of the index (might be NULL)
104600** argv[2] = results of analysis - on integer for each column
104601**
104602** Entries for which argv[1]==NULL simply record the number of rows in
104603** the table.
104604*/
104605static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
104606 analysisInfo *pInfo = (analysisInfo*)pData;
104607 Index *pIndex;
104608 Table *pTable;
104609 const char *z;
104610
104611 assert( argc==3 );
104612 UNUSED_PARAMETER2(NotUsed, argc);
104613
104614 if( argv==0 || argv[0]==0 || argv[2]==0 ){
104615 return 0;
104616 }
104617 pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
104618 if( pTable==0 ){
104619 return 0;
104620 }
104621 if( argv[1]==0 ){
104622 pIndex = 0;
104623 }else if( sqlite3_stricmp(argv[0],argv[1])==0 ){
104624 pIndex = sqlite3PrimaryKeyIndex(pTable);
104625 }else{
104626 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
104627 }
104628 z = argv[2];
104629
104630 if( pIndex ){
104631 tRowcnt *aiRowEst = 0;
104632 int nCol = pIndex->nKeyCol+1;
104633#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104634 /* Index.aiRowEst may already be set here if there are duplicate
104635 ** sqlite_stat1 entries for this index. In that case just clobber
104636 ** the old data with the new instead of allocating a new array. */
104637 if( pIndex->aiRowEst==0 ){
104638 pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
104639 if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
104640 }
104641 aiRowEst = pIndex->aiRowEst;
104642#endif
104643 pIndex->bUnordered = 0;
104644 decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
104645 pIndex->hasStat1 = 1;
104646 if( pIndex->pPartIdxWhere==0 ){
104647 pTable->nRowLogEst = pIndex->aiRowLogEst[0];
104648 pTable->tabFlags |= TF_HasStat1;
104649 }
104650 }else{
104651 Index fakeIdx;
104652 fakeIdx.szIdxRow = pTable->szTabRow;
104653#ifdef SQLITE_ENABLE_COSTMULT
104654 fakeIdx.pTable = pTable;
104655#endif
104656 decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
104657 pTable->szTabRow = fakeIdx.szIdxRow;
104658 pTable->tabFlags |= TF_HasStat1;
104659 }
104660
104661 return 0;
104662}
104663
104664/*
104665** If the Index.aSample variable is not NULL, delete the aSample[] array
104666** and its contents.
104667*/
104668SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
104669#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104670 if( pIdx->aSample ){
104671 int j;
104672 for(j=0; j<pIdx->nSample; j++){
104673 IndexSample *p = &pIdx->aSample[j];
104674 sqlite3DbFree(db, p->p);
104675 }
104676 sqlite3DbFree(db, pIdx->aSample);
104677 }
104678 if( db && db->pnBytesFreed==0 ){
104679 pIdx->nSample = 0;
104680 pIdx->aSample = 0;
104681 }
104682#else
104683 UNUSED_PARAMETER(db);
104684 UNUSED_PARAMETER(pIdx);
104685#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
104686}
104687
104688#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104689/*
104690** Populate the pIdx->aAvgEq[] array based on the samples currently
104691** stored in pIdx->aSample[].
104692*/
104693static void initAvgEq(Index *pIdx){
104694 if( pIdx ){
104695 IndexSample *aSample = pIdx->aSample;
104696 IndexSample *pFinal = &aSample[pIdx->nSample-1];
104697 int iCol;
104698 int nCol = 1;
104699 if( pIdx->nSampleCol>1 ){
104700 /* If this is stat4 data, then calculate aAvgEq[] values for all
104701 ** sample columns except the last. The last is always set to 1, as
104702 ** once the trailing PK fields are considered all index keys are
104703 ** unique. */
104704 nCol = pIdx->nSampleCol-1;
104705 pIdx->aAvgEq[nCol] = 1;
104706 }
104707 for(iCol=0; iCol<nCol; iCol++){
104708 int nSample = pIdx->nSample;
104709 int i; /* Used to iterate through samples */
104710 tRowcnt sumEq = 0; /* Sum of the nEq values */
104711 tRowcnt avgEq = 0;
104712 tRowcnt nRow; /* Number of rows in index */
104713 i64 nSum100 = 0; /* Number of terms contributing to sumEq */
104714 i64 nDist100; /* Number of distinct values in index */
104715
104716 if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
104717 nRow = pFinal->anLt[iCol];
104718 nDist100 = (i64)100 * pFinal->anDLt[iCol];
104719 nSample--;
104720 }else{
104721 nRow = pIdx->aiRowEst[0];
104722 nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
104723 }
104724 pIdx->nRowEst0 = nRow;
104725
104726 /* Set nSum to the number of distinct (iCol+1) field prefixes that
104727 ** occur in the stat4 table for this index. Set sumEq to the sum of
104728 ** the nEq values for column iCol for the same set (adding the value
104729 ** only once where there exist duplicate prefixes). */
104730 for(i=0; i<nSample; i++){
104731 if( i==(pIdx->nSample-1)
104732 || aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol]
104733 ){
104734 sumEq += aSample[i].anEq[iCol];
104735 nSum100 += 100;
104736 }
104737 }
104738
104739 if( nDist100>nSum100 && sumEq<nRow ){
104740 avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
104741 }
104742 if( avgEq==0 ) avgEq = 1;
104743 pIdx->aAvgEq[iCol] = avgEq;
104744 }
104745 }
104746}
104747
104748/*
104749** Look up an index by name. Or, if the name of a WITHOUT ROWID table
104750** is supplied instead, find the PRIMARY KEY index for that table.
104751*/
104752static Index *findIndexOrPrimaryKey(
104753 sqlite3 *db,
104754 const char *zName,
104755 const char *zDb
104756){
104757 Index *pIdx = sqlite3FindIndex(db, zName, zDb);
104758 if( pIdx==0 ){
104759 Table *pTab = sqlite3FindTable(db, zName, zDb);
104760 if( pTab && !HasRowid(pTab) ) pIdx = sqlite3PrimaryKeyIndex(pTab);
104761 }
104762 return pIdx;
104763}
104764
104765/*
104766** Load the content from either the sqlite_stat4 or sqlite_stat3 table
104767** into the relevant Index.aSample[] arrays.
104768**
104769** Arguments zSql1 and zSql2 must point to SQL statements that return
104770** data equivalent to the following (statements are different for stat3,
104771** see the caller of this function for details):
104772**
104773** zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx
104774** zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4
104775**
104776** where %Q is replaced with the database name before the SQL is executed.
104777*/
104778static int loadStatTbl(
104779 sqlite3 *db, /* Database handle */
104780 int bStat3, /* Assume single column records only */
104781 const char *zSql1, /* SQL statement 1 (see above) */
104782 const char *zSql2, /* SQL statement 2 (see above) */
104783 const char *zDb /* Database name (e.g. "main") */
104784){
104785 int rc; /* Result codes from subroutines */
104786 sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
104787 char *zSql; /* Text of the SQL statement */
104788 Index *pPrevIdx = 0; /* Previous index in the loop */
104789 IndexSample *pSample; /* A slot in pIdx->aSample[] */
104790
104791 assert( db->lookaside.bDisable );
104792 zSql = sqlite3MPrintf(db, zSql1, zDb);
104793 if( !zSql ){
104794 return SQLITE_NOMEM_BKPT;
104795 }
104796 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
104797 sqlite3DbFree(db, zSql);
104798 if( rc ) return rc;
104799
104800 while( sqlite3_step(pStmt)==SQLITE_ROW ){
104801 int nIdxCol = 1; /* Number of columns in stat4 records */
104802
104803 char *zIndex; /* Index name */
104804 Index *pIdx; /* Pointer to the index object */
104805 int nSample; /* Number of samples */
104806 int nByte; /* Bytes of space required */
104807 int i; /* Bytes of space required */
104808 tRowcnt *pSpace;
104809
104810 zIndex = (char *)sqlite3_column_text(pStmt, 0);
104811 if( zIndex==0 ) continue;
104812 nSample = sqlite3_column_int(pStmt, 1);
104813 pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
104814 assert( pIdx==0 || bStat3 || pIdx->nSample==0 );
104815 /* Index.nSample is non-zero at this point if data has already been
104816 ** loaded from the stat4 table. In this case ignore stat3 data. */
104817 if( pIdx==0 || pIdx->nSample ) continue;
104818 if( bStat3==0 ){
104819 assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
104820 if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
104821 nIdxCol = pIdx->nKeyCol;
104822 }else{
104823 nIdxCol = pIdx->nColumn;
104824 }
104825 }
104826 pIdx->nSampleCol = nIdxCol;
104827 nByte = sizeof(IndexSample) * nSample;
104828 nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
104829 nByte += nIdxCol * sizeof(tRowcnt); /* Space for Index.aAvgEq[] */
104830
104831 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
104832 if( pIdx->aSample==0 ){
104833 sqlite3_finalize(pStmt);
104834 return SQLITE_NOMEM_BKPT;
104835 }
104836 pSpace = (tRowcnt*)&pIdx->aSample[nSample];
104837 pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
104838 for(i=0; i<nSample; i++){
104839 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
104840 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
104841 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
104842 }
104843 assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
104844 }
104845 rc = sqlite3_finalize(pStmt);
104846 if( rc ) return rc;
104847
104848 zSql = sqlite3MPrintf(db, zSql2, zDb);
104849 if( !zSql ){
104850 return SQLITE_NOMEM_BKPT;
104851 }
104852 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
104853 sqlite3DbFree(db, zSql);
104854 if( rc ) return rc;
104855
104856 while( sqlite3_step(pStmt)==SQLITE_ROW ){
104857 char *zIndex; /* Index name */
104858 Index *pIdx; /* Pointer to the index object */
104859 int nCol = 1; /* Number of columns in index */
104860
104861 zIndex = (char *)sqlite3_column_text(pStmt, 0);
104862 if( zIndex==0 ) continue;
104863 pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
104864 if( pIdx==0 ) continue;
104865 /* This next condition is true if data has already been loaded from
104866 ** the sqlite_stat4 table. In this case ignore stat3 data. */
104867 nCol = pIdx->nSampleCol;
104868 if( bStat3 && nCol>1 ) continue;
104869 if( pIdx!=pPrevIdx ){
104870 initAvgEq(pPrevIdx);
104871 pPrevIdx = pIdx;
104872 }
104873 pSample = &pIdx->aSample[pIdx->nSample];
104874 decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
104875 decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
104876 decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
104877
104878 /* Take a copy of the sample. Add two 0x00 bytes the end of the buffer.
104879 ** This is in case the sample record is corrupted. In that case, the
104880 ** sqlite3VdbeRecordCompare() may read up to two varints past the
104881 ** end of the allocated buffer before it realizes it is dealing with
104882 ** a corrupt record. Adding the two 0x00 bytes prevents this from causing
104883 ** a buffer overread. */
104884 pSample->n = sqlite3_column_bytes(pStmt, 4);
104885 pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
104886 if( pSample->p==0 ){
104887 sqlite3_finalize(pStmt);
104888 return SQLITE_NOMEM_BKPT;
104889 }
104890 if( pSample->n ){
104891 memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
104892 }
104893 pIdx->nSample++;
104894 }
104895 rc = sqlite3_finalize(pStmt);
104896 if( rc==SQLITE_OK ) initAvgEq(pPrevIdx);
104897 return rc;
104898}
104899
104900/*
104901** Load content from the sqlite_stat4 and sqlite_stat3 tables into
104902** the Index.aSample[] arrays of all indices.
104903*/
104904static int loadStat4(sqlite3 *db, const char *zDb){
104905 int rc = SQLITE_OK; /* Result codes from subroutines */
104906
104907 assert( db->lookaside.bDisable );
104908 if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){
104909 rc = loadStatTbl(db, 0,
104910 "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx",
104911 "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
104912 zDb
104913 );
104914 }
104915
104916 if( rc==SQLITE_OK && sqlite3FindTable(db, "sqlite_stat3", zDb) ){
104917 rc = loadStatTbl(db, 1,
104918 "SELECT idx,count(*) FROM %Q.sqlite_stat3 GROUP BY idx",
104919 "SELECT idx,neq,nlt,ndlt,sqlite_record(sample) FROM %Q.sqlite_stat3",
104920 zDb
104921 );
104922 }
104923
104924 return rc;
104925}
104926#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
104927
104928/*
104929** Load the content of the sqlite_stat1 and sqlite_stat3/4 tables. The
104930** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
104931** arrays. The contents of sqlite_stat3/4 are used to populate the
104932** Index.aSample[] arrays.
104933**
104934** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
104935** is returned. In this case, even if SQLITE_ENABLE_STAT3/4 was defined
104936** during compilation and the sqlite_stat3/4 table is present, no data is
104937** read from it.
104938**
104939** If SQLITE_ENABLE_STAT3/4 was defined during compilation and the
104940** sqlite_stat4 table is not present in the database, SQLITE_ERROR is
104941** returned. However, in this case, data is read from the sqlite_stat1
104942** table (if it is present) before returning.
104943**
104944** If an OOM error occurs, this function always sets db->mallocFailed.
104945** This means if the caller does not care about other errors, the return
104946** code may be ignored.
104947*/
104948SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
104949 analysisInfo sInfo;
104950 HashElem *i;
104951 char *zSql;
104952 int rc = SQLITE_OK;
104953 Schema *pSchema = db->aDb[iDb].pSchema;
104954
104955 assert( iDb>=0 && iDb<db->nDb );
104956 assert( db->aDb[iDb].pBt!=0 );
104957
104958 /* Clear any prior statistics */
104959 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
104960 for(i=sqliteHashFirst(&pSchema->tblHash); i; i=sqliteHashNext(i)){
104961 Table *pTab = sqliteHashData(i);
104962 pTab->tabFlags &= ~TF_HasStat1;
104963 }
104964 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
104965 Index *pIdx = sqliteHashData(i);
104966 pIdx->hasStat1 = 0;
104967#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104968 sqlite3DeleteIndexSamples(db, pIdx);
104969 pIdx->aSample = 0;
104970#endif
104971 }
104972
104973 /* Load new statistics out of the sqlite_stat1 table */
104974 sInfo.db = db;
104975 sInfo.zDatabase = db->aDb[iDb].zDbSName;
104976 if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)!=0 ){
104977 zSql = sqlite3MPrintf(db,
104978 "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
104979 if( zSql==0 ){
104980 rc = SQLITE_NOMEM_BKPT;
104981 }else{
104982 rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
104983 sqlite3DbFree(db, zSql);
104984 }
104985 }
104986
104987 /* Set appropriate defaults on all indexes not in the sqlite_stat1 table */
104988 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
104989 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
104990 Index *pIdx = sqliteHashData(i);
104991 if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
104992 }
104993
104994 /* Load the statistics from the sqlite_stat4 table. */
104995#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104996 if( rc==SQLITE_OK ){
104997 db->lookaside.bDisable++;
104998 rc = loadStat4(db, sInfo.zDatabase);
104999 db->lookaside.bDisable--;
105000 }
105001 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
105002 Index *pIdx = sqliteHashData(i);
105003 sqlite3_free(pIdx->aiRowEst);
105004 pIdx->aiRowEst = 0;
105005 }
105006#endif
105007
105008 if( rc==SQLITE_NOMEM ){
105009 sqlite3OomFault(db);
105010 }
105011 return rc;
105012}
105013
105014
105015#endif /* SQLITE_OMIT_ANALYZE */
105016
105017/************** End of analyze.c *********************************************/
105018/************** Begin file attach.c ******************************************/
105019/*
105020** 2003 April 6
105021**
105022** The author disclaims copyright to this source code. In place of
105023** a legal notice, here is a blessing:
105024**
105025** May you do good and not evil.
105026** May you find forgiveness for yourself and forgive others.
105027** May you share freely, never taking more than you give.
105028**
105029*************************************************************************
105030** This file contains code used to implement the ATTACH and DETACH commands.
105031*/
105032/* #include "sqliteInt.h" */
105033
105034#ifndef SQLITE_OMIT_ATTACH
105035/*
105036** Resolve an expression that was part of an ATTACH or DETACH statement. This
105037** is slightly different from resolving a normal SQL expression, because simple
105038** identifiers are treated as strings, not possible column names or aliases.
105039**
105040** i.e. if the parser sees:
105041**
105042** ATTACH DATABASE abc AS def
105043**
105044** it treats the two expressions as literal strings 'abc' and 'def' instead of
105045** looking for columns of the same name.
105046**
105047** This only applies to the root node of pExpr, so the statement:
105048**
105049** ATTACH DATABASE abc||def AS 'db2'
105050**
105051** will fail because neither abc or def can be resolved.
105052*/
105053static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
105054{
105055 int rc = SQLITE_OK;
105056 if( pExpr ){
105057 if( pExpr->op!=TK_ID ){
105058 rc = sqlite3ResolveExprNames(pName, pExpr);
105059 }else{
105060 pExpr->op = TK_STRING;
105061 }
105062 }
105063 return rc;
105064}
105065
105066/*
105067** An SQL user-function registered to do the work of an ATTACH statement. The
105068** three arguments to the function come directly from an attach statement:
105069**
105070** ATTACH DATABASE x AS y KEY z
105071**
105072** SELECT sqlite_attach(x, y, z)
105073**
105074** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
105075** third argument.
105076**
105077** If the db->init.reopenMemdb flags is set, then instead of attaching a
105078** new database, close the database on db->init.iDb and reopen it as an
105079** empty MemDB.
105080*/
105081static void attachFunc(
105082 sqlite3_context *context,
105083 int NotUsed,
105084 sqlite3_value **argv
105085){
105086 int i;
105087 int rc = 0;
105088 sqlite3 *db = sqlite3_context_db_handle(context);
105089 const char *zName;
105090 const char *zFile;
105091 char *zPath = 0;
105092 char *zErr = 0;
105093 unsigned int flags;
105094 Db *aNew; /* New array of Db pointers */
105095 Db *pNew; /* Db object for the newly attached database */
105096 char *zErrDyn = 0;
105097 sqlite3_vfs *pVfs;
105098
105099 UNUSED_PARAMETER(NotUsed);
105100 zFile = (const char *)sqlite3_value_text(argv[0]);
105101 zName = (const char *)sqlite3_value_text(argv[1]);
105102 if( zFile==0 ) zFile = "";
105103 if( zName==0 ) zName = "";
105104
105105#ifdef SQLITE_ENABLE_DESERIALIZE
105106# define REOPEN_AS_MEMDB(db) (db->init.reopenMemdb)
105107#else
105108# define REOPEN_AS_MEMDB(db) (0)
105109#endif
105110
105111 if( REOPEN_AS_MEMDB(db) ){
105112 /* This is not a real ATTACH. Instead, this routine is being called
105113 ** from sqlite3_deserialize() to close database db->init.iDb and
105114 ** reopen it as a MemDB */
105115 pVfs = sqlite3_vfs_find("memdb");
105116 if( pVfs==0 ) return;
105117 pNew = &db->aDb[db->init.iDb];
105118 if( pNew->pBt ) sqlite3BtreeClose(pNew->pBt);
105119 pNew->pBt = 0;
105120 pNew->pSchema = 0;
105121 rc = sqlite3BtreeOpen(pVfs, "x\0", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB);
105122 }else{
105123 /* This is a real ATTACH
105124 **
105125 ** Check for the following errors:
105126 **
105127 ** * Too many attached databases,
105128 ** * Transaction currently open
105129 ** * Specified database name already being used.
105130 */
105131 if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
105132 zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
105133 db->aLimit[SQLITE_LIMIT_ATTACHED]
105134 );
105135 goto attach_error;
105136 }
105137 for(i=0; i<db->nDb; i++){
105138 char *z = db->aDb[i].zDbSName;
105139 assert( z && zName );
105140 if( sqlite3StrICmp(z, zName)==0 ){
105141 zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
105142 goto attach_error;
105143 }
105144 }
105145
105146 /* Allocate the new entry in the db->aDb[] array and initialize the schema
105147 ** hash tables.
105148 */
105149 if( db->aDb==db->aDbStatic ){
105150 aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
105151 if( aNew==0 ) return;
105152 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
105153 }else{
105154 aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
105155 if( aNew==0 ) return;
105156 }
105157 db->aDb = aNew;
105158 pNew = &db->aDb[db->nDb];
105159 memset(pNew, 0, sizeof(*pNew));
105160
105161 /* Open the database file. If the btree is successfully opened, use
105162 ** it to obtain the database schema. At this point the schema may
105163 ** or may not be initialized.
105164 */
105165 flags = db->openFlags;
105166 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
105167 if( rc!=SQLITE_OK ){
105168 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
105169 sqlite3_result_error(context, zErr, -1);
105170 sqlite3_free(zErr);
105171 return;
105172 }
105173 assert( pVfs );
105174 flags |= SQLITE_OPEN_MAIN_DB;
105175 rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
105176 sqlite3_free( zPath );
105177 db->nDb++;
105178 }
105179 db->noSharedCache = 0;
105180 if( rc==SQLITE_CONSTRAINT ){
105181 rc = SQLITE_ERROR;
105182 zErrDyn = sqlite3MPrintf(db, "database is already attached");
105183 }else if( rc==SQLITE_OK ){
105184 Pager *pPager;
105185 pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
105186 if( !pNew->pSchema ){
105187 rc = SQLITE_NOMEM_BKPT;
105188 }else if( pNew->pSchema->file_format && pNew->pSchema->enc!=ENC(db) ){
105189 zErrDyn = sqlite3MPrintf(db,
105190 "attached databases must use the same text encoding as main database");
105191 rc = SQLITE_ERROR;
105192 }
105193 sqlite3BtreeEnter(pNew->pBt);
105194 pPager = sqlite3BtreePager(pNew->pBt);
105195 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
105196 sqlite3BtreeSecureDelete(pNew->pBt,
105197 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
105198#ifndef SQLITE_OMIT_PAGER_PRAGMAS
105199 sqlite3BtreeSetPagerFlags(pNew->pBt,
105200 PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
105201#endif
105202 sqlite3BtreeLeave(pNew->pBt);
105203 }
105204 pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
105205 if( !REOPEN_AS_MEMDB(db) ) pNew->zDbSName = sqlite3DbStrDup(db, zName);
105206 if( rc==SQLITE_OK && pNew->zDbSName==0 ){
105207 rc = SQLITE_NOMEM_BKPT;
105208 }
105209
105210
105211#ifdef SQLITE_HAS_CODEC
105212 if( rc==SQLITE_OK ){
105213 extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
105214 extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
105215 int nKey;
105216 char *zKey;
105217 int t = sqlite3_value_type(argv[2]);
105218 switch( t ){
105219 case SQLITE_INTEGER:
105220 case SQLITE_FLOAT:
105221 zErrDyn = sqlite3DbStrDup(db, "Invalid key value");
105222 rc = SQLITE_ERROR;
105223 break;
105224
105225 case SQLITE_TEXT:
105226 case SQLITE_BLOB:
105227 nKey = sqlite3_value_bytes(argv[2]);
105228 zKey = (char *)sqlite3_value_blob(argv[2]);
105229 rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
105230 break;
105231
105232 case SQLITE_NULL:
105233 /* No key specified. Use the key from the main database */
105234 sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
105235 if( nKey || sqlite3BtreeGetOptimalReserve(db->aDb[0].pBt)>0 ){
105236 rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
105237 }
105238 break;
105239 }
105240 }
105241#endif
105242
105243 /* If the file was opened successfully, read the schema for the new database.
105244 ** If this fails, or if opening the file failed, then close the file and
105245 ** remove the entry from the db->aDb[] array. i.e. put everything back the
105246 ** way we found it.
105247 */
105248 if( rc==SQLITE_OK ){
105249 sqlite3BtreeEnterAll(db);
105250 db->init.iDb = 0;
105251 db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
105252 rc = sqlite3Init(db, &zErrDyn);
105253 sqlite3BtreeLeaveAll(db);
105254 assert( zErrDyn==0 || rc!=SQLITE_OK );
105255 }
105256#ifdef SQLITE_USER_AUTHENTICATION
105257 if( rc==SQLITE_OK ){
105258 u8 newAuth = 0;
105259 rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
105260 if( newAuth<db->auth.authLevel ){
105261 rc = SQLITE_AUTH_USER;
105262 }
105263 }
105264#endif
105265 if( rc ){
105266 if( !REOPEN_AS_MEMDB(db) ){
105267 int iDb = db->nDb - 1;
105268 assert( iDb>=2 );
105269 if( db->aDb[iDb].pBt ){
105270 sqlite3BtreeClose(db->aDb[iDb].pBt);
105271 db->aDb[iDb].pBt = 0;
105272 db->aDb[iDb].pSchema = 0;
105273 }
105274 sqlite3ResetAllSchemasOfConnection(db);
105275 db->nDb = iDb;
105276 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
105277 sqlite3OomFault(db);
105278 sqlite3DbFree(db, zErrDyn);
105279 zErrDyn = sqlite3MPrintf(db, "out of memory");
105280 }else if( zErrDyn==0 ){
105281 zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
105282 }
105283 }
105284 goto attach_error;
105285 }
105286
105287 return;
105288
105289attach_error:
105290 /* Return an error if we get here */
105291 if( zErrDyn ){
105292 sqlite3_result_error(context, zErrDyn, -1);
105293 sqlite3DbFree(db, zErrDyn);
105294 }
105295 if( rc ) sqlite3_result_error_code(context, rc);
105296}
105297
105298/*
105299** An SQL user-function registered to do the work of an DETACH statement. The
105300** three arguments to the function come directly from a detach statement:
105301**
105302** DETACH DATABASE x
105303**
105304** SELECT sqlite_detach(x)
105305*/
105306static void detachFunc(
105307 sqlite3_context *context,
105308 int NotUsed,
105309 sqlite3_value **argv
105310){
105311 const char *zName = (const char *)sqlite3_value_text(argv[0]);
105312 sqlite3 *db = sqlite3_context_db_handle(context);
105313 int i;
105314 Db *pDb = 0;
105315 char zErr[128];
105316
105317 UNUSED_PARAMETER(NotUsed);
105318
105319 if( zName==0 ) zName = "";
105320 for(i=0; i<db->nDb; i++){
105321 pDb = &db->aDb[i];
105322 if( pDb->pBt==0 ) continue;
105323 if( sqlite3StrICmp(pDb->zDbSName, zName)==0 ) break;
105324 }
105325
105326 if( i>=db->nDb ){
105327 sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
105328 goto detach_error;
105329 }
105330 if( i<2 ){
105331 sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
105332 goto detach_error;
105333 }
105334 if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){
105335 sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
105336 goto detach_error;
105337 }
105338
105339 sqlite3BtreeClose(pDb->pBt);
105340 pDb->pBt = 0;
105341 pDb->pSchema = 0;
105342 sqlite3CollapseDatabaseArray(db);
105343 return;
105344
105345detach_error:
105346 sqlite3_result_error(context, zErr, -1);
105347}
105348
105349/*
105350** This procedure generates VDBE code for a single invocation of either the
105351** sqlite_detach() or sqlite_attach() SQL user functions.
105352*/
105353static void codeAttach(
105354 Parse *pParse, /* The parser context */
105355 int type, /* Either SQLITE_ATTACH or SQLITE_DETACH */
105356 FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */
105357 Expr *pAuthArg, /* Expression to pass to authorization callback */
105358 Expr *pFilename, /* Name of database file */
105359 Expr *pDbname, /* Name of the database to use internally */
105360 Expr *pKey /* Database key for encryption extension */
105361){
105362 int rc;
105363 NameContext sName;
105364 Vdbe *v;
105365 sqlite3* db = pParse->db;
105366 int regArgs;
105367
105368 if( pParse->nErr ) goto attach_end;
105369 memset(&sName, 0, sizeof(NameContext));
105370 sName.pParse = pParse;
105371
105372 if(
105373 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) ||
105374 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) ||
105375 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey))
105376 ){
105377 goto attach_end;
105378 }
105379
105380#ifndef SQLITE_OMIT_AUTHORIZATION
105381 if( pAuthArg ){
105382 char *zAuthArg;
105383 if( pAuthArg->op==TK_STRING ){
105384 zAuthArg = pAuthArg->u.zToken;
105385 }else{
105386 zAuthArg = 0;
105387 }
105388 rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
105389 if(rc!=SQLITE_OK ){
105390 goto attach_end;
105391 }
105392 }
105393#endif /* SQLITE_OMIT_AUTHORIZATION */
105394
105395
105396 v = sqlite3GetVdbe(pParse);
105397 regArgs = sqlite3GetTempRange(pParse, 4);
105398 sqlite3ExprCode(pParse, pFilename, regArgs);
105399 sqlite3ExprCode(pParse, pDbname, regArgs+1);
105400 sqlite3ExprCode(pParse, pKey, regArgs+2);
105401
105402 assert( v || db->mallocFailed );
105403 if( v ){
105404 sqlite3VdbeAddOp4(v, OP_Function0, 0, regArgs+3-pFunc->nArg, regArgs+3,
105405 (char *)pFunc, P4_FUNCDEF);
105406 assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
105407 sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
105408
105409 /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
105410 ** statement only). For DETACH, set it to false (expire all existing
105411 ** statements).
105412 */
105413 sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH));
105414 }
105415
105416attach_end:
105417 sqlite3ExprDelete(db, pFilename);
105418 sqlite3ExprDelete(db, pDbname);
105419 sqlite3ExprDelete(db, pKey);
105420}
105421
105422/*
105423** Called by the parser to compile a DETACH statement.
105424**
105425** DETACH pDbname
105426*/
105427SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){
105428 static const FuncDef detach_func = {
105429 1, /* nArg */
105430 SQLITE_UTF8, /* funcFlags */
105431 0, /* pUserData */
105432 0, /* pNext */
105433 detachFunc, /* xSFunc */
105434 0, /* xFinalize */
105435 0, 0, /* xValue, xInverse */
105436 "sqlite_detach", /* zName */
105437 {0}
105438 };
105439 codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname);
105440}
105441
105442/*
105443** Called by the parser to compile an ATTACH statement.
105444**
105445** ATTACH p AS pDbname KEY pKey
105446*/
105447SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
105448 static const FuncDef attach_func = {
105449 3, /* nArg */
105450 SQLITE_UTF8, /* funcFlags */
105451 0, /* pUserData */
105452 0, /* pNext */
105453 attachFunc, /* xSFunc */
105454 0, /* xFinalize */
105455 0, 0, /* xValue, xInverse */
105456 "sqlite_attach", /* zName */
105457 {0}
105458 };
105459 codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey);
105460}
105461#endif /* SQLITE_OMIT_ATTACH */
105462
105463/*
105464** Initialize a DbFixer structure. This routine must be called prior
105465** to passing the structure to one of the sqliteFixAAAA() routines below.
105466*/
105467SQLITE_PRIVATE void sqlite3FixInit(
105468 DbFixer *pFix, /* The fixer to be initialized */
105469 Parse *pParse, /* Error messages will be written here */
105470 int iDb, /* This is the database that must be used */
105471 const char *zType, /* "view", "trigger", or "index" */
105472 const Token *pName /* Name of the view, trigger, or index */
105473){
105474 sqlite3 *db;
105475
105476 db = pParse->db;
105477 assert( db->nDb>iDb );
105478 pFix->pParse = pParse;
105479 pFix->zDb = db->aDb[iDb].zDbSName;
105480 pFix->pSchema = db->aDb[iDb].pSchema;
105481 pFix->zType = zType;
105482 pFix->pName = pName;
105483 pFix->bVarOnly = (iDb==1);
105484}
105485
105486/*
105487** The following set of routines walk through the parse tree and assign
105488** a specific database to all table references where the database name
105489** was left unspecified in the original SQL statement. The pFix structure
105490** must have been initialized by a prior call to sqlite3FixInit().
105491**
105492** These routines are used to make sure that an index, trigger, or
105493** view in one database does not refer to objects in a different database.
105494** (Exception: indices, triggers, and views in the TEMP database are
105495** allowed to refer to anything.) If a reference is explicitly made
105496** to an object in a different database, an error message is added to
105497** pParse->zErrMsg and these routines return non-zero. If everything
105498** checks out, these routines return 0.
105499*/
105500SQLITE_PRIVATE int sqlite3FixSrcList(
105501 DbFixer *pFix, /* Context of the fixation */
105502 SrcList *pList /* The Source list to check and modify */
105503){
105504 int i;
105505 const char *zDb;
105506 struct SrcList_item *pItem;
105507
105508 if( NEVER(pList==0) ) return 0;
105509 zDb = pFix->zDb;
105510 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
105511 if( pFix->bVarOnly==0 ){
105512 if( pItem->zDatabase && sqlite3StrICmp(pItem->zDatabase, zDb) ){
105513 sqlite3ErrorMsg(pFix->pParse,
105514 "%s %T cannot reference objects in database %s",
105515 pFix->zType, pFix->pName, pItem->zDatabase);
105516 return 1;
105517 }
105518 sqlite3DbFree(pFix->pParse->db, pItem->zDatabase);
105519 pItem->zDatabase = 0;
105520 pItem->pSchema = pFix->pSchema;
105521 }
105522#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
105523 if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
105524 if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
105525#endif
105526 if( pItem->fg.isTabFunc && sqlite3FixExprList(pFix, pItem->u1.pFuncArg) ){
105527 return 1;
105528 }
105529 }
105530 return 0;
105531}
105532#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
105533SQLITE_PRIVATE int sqlite3FixSelect(
105534 DbFixer *pFix, /* Context of the fixation */
105535 Select *pSelect /* The SELECT statement to be fixed to one database */
105536){
105537 while( pSelect ){
105538 if( sqlite3FixExprList(pFix, pSelect->pEList) ){
105539 return 1;
105540 }
105541 if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
105542 return 1;
105543 }
105544 if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
105545 return 1;
105546 }
105547 if( sqlite3FixExprList(pFix, pSelect->pGroupBy) ){
105548 return 1;
105549 }
105550 if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
105551 return 1;
105552 }
105553 if( sqlite3FixExprList(pFix, pSelect->pOrderBy) ){
105554 return 1;
105555 }
105556 if( sqlite3FixExpr(pFix, pSelect->pLimit) ){
105557 return 1;
105558 }
105559 if( pSelect->pWith ){
105560 int i;
105561 for(i=0; i<pSelect->pWith->nCte; i++){
105562 if( sqlite3FixSelect(pFix, pSelect->pWith->a[i].pSelect) ){
105563 return 1;
105564 }
105565 }
105566 }
105567 pSelect = pSelect->pPrior;
105568 }
105569 return 0;
105570}
105571SQLITE_PRIVATE int sqlite3FixExpr(
105572 DbFixer *pFix, /* Context of the fixation */
105573 Expr *pExpr /* The expression to be fixed to one database */
105574){
105575 while( pExpr ){
105576 if( pExpr->op==TK_VARIABLE ){
105577 if( pFix->pParse->db->init.busy ){
105578 pExpr->op = TK_NULL;
105579 }else{
105580 sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
105581 return 1;
105582 }
105583 }
105584 if( ExprHasProperty(pExpr, EP_TokenOnly|EP_Leaf) ) break;
105585 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
105586 if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
105587 }else{
105588 if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1;
105589 }
105590 if( sqlite3FixExpr(pFix, pExpr->pRight) ){
105591 return 1;
105592 }
105593 pExpr = pExpr->pLeft;
105594 }
105595 return 0;
105596}
105597SQLITE_PRIVATE int sqlite3FixExprList(
105598 DbFixer *pFix, /* Context of the fixation */
105599 ExprList *pList /* The expression to be fixed to one database */
105600){
105601 int i;
105602 struct ExprList_item *pItem;
105603 if( pList==0 ) return 0;
105604 for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){
105605 if( sqlite3FixExpr(pFix, pItem->pExpr) ){
105606 return 1;
105607 }
105608 }
105609 return 0;
105610}
105611#endif
105612
105613#ifndef SQLITE_OMIT_TRIGGER
105614SQLITE_PRIVATE int sqlite3FixTriggerStep(
105615 DbFixer *pFix, /* Context of the fixation */
105616 TriggerStep *pStep /* The trigger step be fixed to one database */
105617){
105618 while( pStep ){
105619 if( sqlite3FixSelect(pFix, pStep->pSelect) ){
105620 return 1;
105621 }
105622 if( sqlite3FixExpr(pFix, pStep->pWhere) ){
105623 return 1;
105624 }
105625 if( sqlite3FixExprList(pFix, pStep->pExprList) ){
105626 return 1;
105627 }
105628#ifndef SQLITE_OMIT_UPSERT
105629 if( pStep->pUpsert ){
105630 Upsert *pUp = pStep->pUpsert;
105631 if( sqlite3FixExprList(pFix, pUp->pUpsertTarget)
105632 || sqlite3FixExpr(pFix, pUp->pUpsertTargetWhere)
105633 || sqlite3FixExprList(pFix, pUp->pUpsertSet)
105634 || sqlite3FixExpr(pFix, pUp->pUpsertWhere)
105635 ){
105636 return 1;
105637 }
105638 }
105639#endif
105640 pStep = pStep->pNext;
105641 }
105642 return 0;
105643}
105644#endif
105645
105646/************** End of attach.c **********************************************/
105647/************** Begin file auth.c ********************************************/
105648/*
105649** 2003 January 11
105650**
105651** The author disclaims copyright to this source code. In place of
105652** a legal notice, here is a blessing:
105653**
105654** May you do good and not evil.
105655** May you find forgiveness for yourself and forgive others.
105656** May you share freely, never taking more than you give.
105657**
105658*************************************************************************
105659** This file contains code used to implement the sqlite3_set_authorizer()
105660** API. This facility is an optional feature of the library. Embedded
105661** systems that do not need this facility may omit it by recompiling
105662** the library with -DSQLITE_OMIT_AUTHORIZATION=1
105663*/
105664/* #include "sqliteInt.h" */
105665
105666/*
105667** All of the code in this file may be omitted by defining a single
105668** macro.
105669*/
105670#ifndef SQLITE_OMIT_AUTHORIZATION
105671
105672/*
105673** Set or clear the access authorization function.
105674**
105675** The access authorization function is be called during the compilation
105676** phase to verify that the user has read and/or write access permission on
105677** various fields of the database. The first argument to the auth function
105678** is a copy of the 3rd argument to this routine. The second argument
105679** to the auth function is one of these constants:
105680**
105681** SQLITE_CREATE_INDEX
105682** SQLITE_CREATE_TABLE
105683** SQLITE_CREATE_TEMP_INDEX
105684** SQLITE_CREATE_TEMP_TABLE
105685** SQLITE_CREATE_TEMP_TRIGGER
105686** SQLITE_CREATE_TEMP_VIEW
105687** SQLITE_CREATE_TRIGGER
105688** SQLITE_CREATE_VIEW
105689** SQLITE_DELETE
105690** SQLITE_DROP_INDEX
105691** SQLITE_DROP_TABLE
105692** SQLITE_DROP_TEMP_INDEX
105693** SQLITE_DROP_TEMP_TABLE
105694** SQLITE_DROP_TEMP_TRIGGER
105695** SQLITE_DROP_TEMP_VIEW
105696** SQLITE_DROP_TRIGGER
105697** SQLITE_DROP_VIEW
105698** SQLITE_INSERT
105699** SQLITE_PRAGMA
105700** SQLITE_READ
105701** SQLITE_SELECT
105702** SQLITE_TRANSACTION
105703** SQLITE_UPDATE
105704**
105705** The third and fourth arguments to the auth function are the name of
105706** the table and the column that are being accessed. The auth function
105707** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE. If
105708** SQLITE_OK is returned, it means that access is allowed. SQLITE_DENY
105709** means that the SQL statement will never-run - the sqlite3_exec() call
105710** will return with an error. SQLITE_IGNORE means that the SQL statement
105711** should run but attempts to read the specified column will return NULL
105712** and attempts to write the column will be ignored.
105713**
105714** Setting the auth function to NULL disables this hook. The default
105715** setting of the auth function is NULL.
105716*/
105717SQLITE_API int sqlite3_set_authorizer(
105718 sqlite3 *db,
105719 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
105720 void *pArg
105721){
105722#ifdef SQLITE_ENABLE_API_ARMOR
105723 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
105724#endif
105725 sqlite3_mutex_enter(db->mutex);
105726 db->xAuth = (sqlite3_xauth)xAuth;
105727 db->pAuthArg = pArg;
105728 sqlite3ExpirePreparedStatements(db, 0);
105729 sqlite3_mutex_leave(db->mutex);
105730 return SQLITE_OK;
105731}
105732
105733/*
105734** Write an error message into pParse->zErrMsg that explains that the
105735** user-supplied authorization function returned an illegal value.
105736*/
105737static void sqliteAuthBadReturnCode(Parse *pParse){
105738 sqlite3ErrorMsg(pParse, "authorizer malfunction");
105739 pParse->rc = SQLITE_ERROR;
105740}
105741
105742/*
105743** Invoke the authorization callback for permission to read column zCol from
105744** table zTab in database zDb. This function assumes that an authorization
105745** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
105746**
105747** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
105748** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
105749** is treated as SQLITE_DENY. In this case an error is left in pParse.
105750*/
105751SQLITE_PRIVATE int sqlite3AuthReadCol(
105752 Parse *pParse, /* The parser context */
105753 const char *zTab, /* Table name */
105754 const char *zCol, /* Column name */
105755 int iDb /* Index of containing database. */
105756){
105757 sqlite3 *db = pParse->db; /* Database handle */
105758 char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
105759 int rc; /* Auth callback return code */
105760
105761 if( db->init.busy ) return SQLITE_OK;
105762 rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
105763#ifdef SQLITE_USER_AUTHENTICATION
105764 ,db->auth.zAuthUser
105765#endif
105766 );
105767 if( rc==SQLITE_DENY ){
105768 char *z = sqlite3_mprintf("%s.%s", zTab, zCol);
105769 if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf("%s.%z", zDb, z);
105770 sqlite3ErrorMsg(pParse, "access to %z is prohibited", z);
105771 pParse->rc = SQLITE_AUTH;
105772 }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){
105773 sqliteAuthBadReturnCode(pParse);
105774 }
105775 return rc;
105776}
105777
105778/*
105779** The pExpr should be a TK_COLUMN expression. The table referred to
105780** is in pTabList or else it is the NEW or OLD table of a trigger.
105781** Check to see if it is OK to read this particular column.
105782**
105783** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN
105784** instruction into a TK_NULL. If the auth function returns SQLITE_DENY,
105785** then generate an error.
105786*/
105787SQLITE_PRIVATE void sqlite3AuthRead(
105788 Parse *pParse, /* The parser context */
105789 Expr *pExpr, /* The expression to check authorization on */
105790 Schema *pSchema, /* The schema of the expression */
105791 SrcList *pTabList /* All table that pExpr might refer to */
105792){
105793 sqlite3 *db = pParse->db;
105794 Table *pTab = 0; /* The table being read */
105795 const char *zCol; /* Name of the column of the table */
105796 int iSrc; /* Index in pTabList->a[] of table being read */
105797 int iDb; /* The index of the database the expression refers to */
105798 int iCol; /* Index of column in table */
105799
105800 assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
105801 assert( !IN_RENAME_OBJECT || db->xAuth==0 );
105802 if( db->xAuth==0 ) return;
105803 iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
105804 if( iDb<0 ){
105805 /* An attempt to read a column out of a subquery or other
105806 ** temporary table. */
105807 return;
105808 }
105809
105810 if( pExpr->op==TK_TRIGGER ){
105811 pTab = pParse->pTriggerTab;
105812 }else{
105813 assert( pTabList );
105814 for(iSrc=0; ALWAYS(iSrc<pTabList->nSrc); iSrc++){
105815 if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
105816 pTab = pTabList->a[iSrc].pTab;
105817 break;
105818 }
105819 }
105820 }
105821 iCol = pExpr->iColumn;
105822 if( NEVER(pTab==0) ) return;
105823
105824 if( iCol>=0 ){
105825 assert( iCol<pTab->nCol );
105826 zCol = pTab->aCol[iCol].zName;
105827 }else if( pTab->iPKey>=0 ){
105828 assert( pTab->iPKey<pTab->nCol );
105829 zCol = pTab->aCol[pTab->iPKey].zName;
105830 }else{
105831 zCol = "ROWID";
105832 }
105833 assert( iDb>=0 && iDb<db->nDb );
105834 if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
105835 pExpr->op = TK_NULL;
105836 }
105837}
105838
105839/*
105840** Do an authorization check using the code and arguments given. Return
105841** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY. If SQLITE_DENY
105842** is returned, then the error count and error message in pParse are
105843** modified appropriately.
105844*/
105845SQLITE_PRIVATE int sqlite3AuthCheck(
105846 Parse *pParse,
105847 int code,
105848 const char *zArg1,
105849 const char *zArg2,
105850 const char *zArg3
105851){
105852 sqlite3 *db = pParse->db;
105853 int rc;
105854
105855 /* Don't do any authorization checks if the database is initialising
105856 ** or if the parser is being invoked from within sqlite3_declare_vtab.
105857 */
105858 assert( !IN_RENAME_OBJECT || db->xAuth==0 );
105859 if( db->init.busy || IN_SPECIAL_PARSE ){
105860 return SQLITE_OK;
105861 }
105862
105863 if( db->xAuth==0 ){
105864 return SQLITE_OK;
105865 }
105866
105867 /* EVIDENCE-OF: R-43249-19882 The third through sixth parameters to the
105868 ** callback are either NULL pointers or zero-terminated strings that
105869 ** contain additional details about the action to be authorized.
105870 **
105871 ** The following testcase() macros show that any of the 3rd through 6th
105872 ** parameters can be either NULL or a string. */
105873 testcase( zArg1==0 );
105874 testcase( zArg2==0 );
105875 testcase( zArg3==0 );
105876 testcase( pParse->zAuthContext==0 );
105877
105878 rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
105879#ifdef SQLITE_USER_AUTHENTICATION
105880 ,db->auth.zAuthUser
105881#endif
105882 );
105883 if( rc==SQLITE_DENY ){
105884 sqlite3ErrorMsg(pParse, "not authorized");
105885 pParse->rc = SQLITE_AUTH;
105886 }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
105887 rc = SQLITE_DENY;
105888 sqliteAuthBadReturnCode(pParse);
105889 }
105890 return rc;
105891}
105892
105893/*
105894** Push an authorization context. After this routine is called, the
105895** zArg3 argument to authorization callbacks will be zContext until
105896** popped. Or if pParse==0, this routine is a no-op.
105897*/
105898SQLITE_PRIVATE void sqlite3AuthContextPush(
105899 Parse *pParse,
105900 AuthContext *pContext,
105901 const char *zContext
105902){
105903 assert( pParse );
105904 pContext->pParse = pParse;
105905 pContext->zAuthContext = pParse->zAuthContext;
105906 pParse->zAuthContext = zContext;
105907}
105908
105909/*
105910** Pop an authorization context that was previously pushed
105911** by sqlite3AuthContextPush
105912*/
105913SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext *pContext){
105914 if( pContext->pParse ){
105915 pContext->pParse->zAuthContext = pContext->zAuthContext;
105916 pContext->pParse = 0;
105917 }
105918}
105919
105920#endif /* SQLITE_OMIT_AUTHORIZATION */
105921
105922/************** End of auth.c ************************************************/
105923/************** Begin file build.c *******************************************/
105924/*
105925** 2001 September 15
105926**
105927** The author disclaims copyright to this source code. In place of
105928** a legal notice, here is a blessing:
105929**
105930** May you do good and not evil.
105931** May you find forgiveness for yourself and forgive others.
105932** May you share freely, never taking more than you give.
105933**
105934*************************************************************************
105935** This file contains C code routines that are called by the SQLite parser
105936** when syntax rules are reduced. The routines in this file handle the
105937** following kinds of SQL syntax:
105938**
105939** CREATE TABLE
105940** DROP TABLE
105941** CREATE INDEX
105942** DROP INDEX
105943** creating ID lists
105944** BEGIN TRANSACTION
105945** COMMIT
105946** ROLLBACK
105947*/
105948/* #include "sqliteInt.h" */
105949
105950#ifndef SQLITE_OMIT_SHARED_CACHE
105951/*
105952** The TableLock structure is only used by the sqlite3TableLock() and
105953** codeTableLocks() functions.
105954*/
105955struct TableLock {
105956 int iDb; /* The database containing the table to be locked */
105957 int iTab; /* The root page of the table to be locked */
105958 u8 isWriteLock; /* True for write lock. False for a read lock */
105959 const char *zLockName; /* Name of the table */
105960};
105961
105962/*
105963** Record the fact that we want to lock a table at run-time.
105964**
105965** The table to be locked has root page iTab and is found in database iDb.
105966** A read or a write lock can be taken depending on isWritelock.
105967**
105968** This routine just records the fact that the lock is desired. The
105969** code to make the lock occur is generated by a later call to
105970** codeTableLocks() which occurs during sqlite3FinishCoding().
105971*/
105972SQLITE_PRIVATE void sqlite3TableLock(
105973 Parse *pParse, /* Parsing context */
105974 int iDb, /* Index of the database containing the table to lock */
105975 int iTab, /* Root page number of the table to be locked */
105976 u8 isWriteLock, /* True for a write lock */
105977 const char *zName /* Name of the table to be locked */
105978){
105979 Parse *pToplevel = sqlite3ParseToplevel(pParse);
105980 int i;
105981 int nBytes;
105982 TableLock *p;
105983 assert( iDb>=0 );
105984
105985 if( iDb==1 ) return;
105986 if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return;
105987 for(i=0; i<pToplevel->nTableLock; i++){
105988 p = &pToplevel->aTableLock[i];
105989 if( p->iDb==iDb && p->iTab==iTab ){
105990 p->isWriteLock = (p->isWriteLock || isWriteLock);
105991 return;
105992 }
105993 }
105994
105995 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
105996 pToplevel->aTableLock =
105997 sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
105998 if( pToplevel->aTableLock ){
105999 p = &pToplevel->aTableLock[pToplevel->nTableLock++];
106000 p->iDb = iDb;
106001 p->iTab = iTab;
106002 p->isWriteLock = isWriteLock;
106003 p->zLockName = zName;
106004 }else{
106005 pToplevel->nTableLock = 0;
106006 sqlite3OomFault(pToplevel->db);
106007 }
106008}
106009
106010/*
106011** Code an OP_TableLock instruction for each table locked by the
106012** statement (configured by calls to sqlite3TableLock()).
106013*/
106014static void codeTableLocks(Parse *pParse){
106015 int i;
106016 Vdbe *pVdbe;
106017
106018 pVdbe = sqlite3GetVdbe(pParse);
106019 assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */
106020
106021 for(i=0; i<pParse->nTableLock; i++){
106022 TableLock *p = &pParse->aTableLock[i];
106023 int p1 = p->iDb;
106024 sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
106025 p->zLockName, P4_STATIC);
106026 }
106027}
106028#else
106029 #define codeTableLocks(x)
106030#endif
106031
106032/*
106033** Return TRUE if the given yDbMask object is empty - if it contains no
106034** 1 bits. This routine is used by the DbMaskAllZero() and DbMaskNotZero()
106035** macros when SQLITE_MAX_ATTACHED is greater than 30.
106036*/
106037#if SQLITE_MAX_ATTACHED>30
106038SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask m){
106039 int i;
106040 for(i=0; i<sizeof(yDbMask); i++) if( m[i] ) return 0;
106041 return 1;
106042}
106043#endif
106044
106045/*
106046** This routine is called after a single SQL statement has been
106047** parsed and a VDBE program to execute that statement has been
106048** prepared. This routine puts the finishing touches on the
106049** VDBE program and resets the pParse structure for the next
106050** parse.
106051**
106052** Note that if an error occurred, it might be the case that
106053** no VDBE code was generated.
106054*/
106055SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
106056 sqlite3 *db;
106057 Vdbe *v;
106058
106059 assert( pParse->pToplevel==0 );
106060 db = pParse->db;
106061 if( pParse->nested ) return;
106062 if( db->mallocFailed || pParse->nErr ){
106063 if( pParse->rc==SQLITE_OK ) pParse->rc = SQLITE_ERROR;
106064 return;
106065 }
106066
106067 /* Begin by generating some termination code at the end of the
106068 ** vdbe program
106069 */
106070 v = sqlite3GetVdbe(pParse);
106071 assert( !pParse->isMultiWrite
106072 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
106073 if( v ){
106074 sqlite3VdbeAddOp0(v, OP_Halt);
106075
106076#if SQLITE_USER_AUTHENTICATION
106077 if( pParse->nTableLock>0 && db->init.busy==0 ){
106078 sqlite3UserAuthInit(db);
106079 if( db->auth.authLevel<UAUTH_User ){
106080 sqlite3ErrorMsg(pParse, "user not authenticated");
106081 pParse->rc = SQLITE_AUTH_USER;
106082 return;
106083 }
106084 }
106085#endif
106086
106087 /* The cookie mask contains one bit for each database file open.
106088 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
106089 ** set for each database that is used. Generate code to start a
106090 ** transaction on each used database and to verify the schema cookie
106091 ** on each used database.
106092 */
106093 if( db->mallocFailed==0
106094 && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr)
106095 ){
106096 int iDb, i;
106097 assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
106098 sqlite3VdbeJumpHere(v, 0);
106099 for(iDb=0; iDb<db->nDb; iDb++){
106100 Schema *pSchema;
106101 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
106102 sqlite3VdbeUsesBtree(v, iDb);
106103 pSchema = db->aDb[iDb].pSchema;
106104 sqlite3VdbeAddOp4Int(v,
106105 OP_Transaction, /* Opcode */
106106 iDb, /* P1 */
106107 DbMaskTest(pParse->writeMask,iDb), /* P2 */
106108 pSchema->schema_cookie, /* P3 */
106109 pSchema->iGeneration /* P4 */
106110 );
106111 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
106112 VdbeComment((v,
106113 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
106114 }
106115#ifndef SQLITE_OMIT_VIRTUALTABLE
106116 for(i=0; i<pParse->nVtabLock; i++){
106117 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
106118 sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
106119 }
106120 pParse->nVtabLock = 0;
106121#endif
106122
106123 /* Once all the cookies have been verified and transactions opened,
106124 ** obtain the required table-locks. This is a no-op unless the
106125 ** shared-cache feature is enabled.
106126 */
106127 codeTableLocks(pParse);
106128
106129 /* Initialize any AUTOINCREMENT data structures required.
106130 */
106131 sqlite3AutoincrementBegin(pParse);
106132
106133 /* Code constant expressions that where factored out of inner loops */
106134 if( pParse->pConstExpr ){
106135 ExprList *pEL = pParse->pConstExpr;
106136 pParse->okConstFactor = 0;
106137 for(i=0; i<pEL->nExpr; i++){
106138 sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg);
106139 }
106140 }
106141
106142 /* Finally, jump back to the beginning of the executable code. */
106143 sqlite3VdbeGoto(v, 1);
106144 }
106145 }
106146
106147
106148 /* Get the VDBE program ready for execution
106149 */
106150 if( v && pParse->nErr==0 && !db->mallocFailed ){
106151 /* A minimum of one cursor is required if autoincrement is used
106152 * See ticket [a696379c1f08866] */
106153 if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
106154 sqlite3VdbeMakeReady(v, pParse);
106155 pParse->rc = SQLITE_DONE;
106156 }else{
106157 pParse->rc = SQLITE_ERROR;
106158 }
106159}
106160
106161/*
106162** Run the parser and code generator recursively in order to generate
106163** code for the SQL statement given onto the end of the pParse context
106164** currently under construction. When the parser is run recursively
106165** this way, the final OP_Halt is not appended and other initialization
106166** and finalization steps are omitted because those are handling by the
106167** outermost parser.
106168**
106169** Not everything is nestable. This facility is designed to permit
106170** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER. Use
106171** care if you decide to try to use this routine for some other purposes.
106172*/
106173SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
106174 va_list ap;
106175 char *zSql;
106176 char *zErrMsg = 0;
106177 sqlite3 *db = pParse->db;
106178 char saveBuf[PARSE_TAIL_SZ];
106179
106180 if( pParse->nErr ) return;
106181 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
106182 va_start(ap, zFormat);
106183 zSql = sqlite3VMPrintf(db, zFormat, ap);
106184 va_end(ap);
106185 if( zSql==0 ){
106186 return; /* A malloc must have failed */
106187 }
106188 pParse->nested++;
106189 memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ);
106190 memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ);
106191 sqlite3RunParser(pParse, zSql, &zErrMsg);
106192 sqlite3DbFree(db, zErrMsg);
106193 sqlite3DbFree(db, zSql);
106194 memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ);
106195 pParse->nested--;
106196}
106197
106198#if SQLITE_USER_AUTHENTICATION
106199/*
106200** Return TRUE if zTable is the name of the system table that stores the
106201** list of users and their access credentials.
106202*/
106203SQLITE_PRIVATE int sqlite3UserAuthTable(const char *zTable){
106204 return sqlite3_stricmp(zTable, "sqlite_user")==0;
106205}
106206#endif
106207
106208/*
106209** Locate the in-memory structure that describes a particular database
106210** table given the name of that table and (optionally) the name of the
106211** database containing the table. Return NULL if not found.
106212**
106213** If zDatabase is 0, all databases are searched for the table and the
106214** first matching table is returned. (No checking for duplicate table
106215** names is done.) The search order is TEMP first, then MAIN, then any
106216** auxiliary databases added using the ATTACH command.
106217**
106218** See also sqlite3LocateTable().
106219*/
106220SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
106221 Table *p = 0;
106222 int i;
106223
106224 /* All mutexes are required for schema access. Make sure we hold them. */
106225 assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
106226#if SQLITE_USER_AUTHENTICATION
106227 /* Only the admin user is allowed to know that the sqlite_user table
106228 ** exists */
106229 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
106230 return 0;
106231 }
106232#endif
106233 while(1){
106234 for(i=OMIT_TEMPDB; i<db->nDb; i++){
106235 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
106236 if( zDatabase==0 || sqlite3StrICmp(zDatabase, db->aDb[j].zDbSName)==0 ){
106237 assert( sqlite3SchemaMutexHeld(db, j, 0) );
106238 p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName);
106239 if( p ) return p;
106240 }
106241 }
106242 /* Not found. If the name we were looking for was temp.sqlite_master
106243 ** then change the name to sqlite_temp_master and try again. */
106244 if( sqlite3StrICmp(zName, MASTER_NAME)!=0 ) break;
106245 if( sqlite3_stricmp(zDatabase, db->aDb[1].zDbSName)!=0 ) break;
106246 zName = TEMP_MASTER_NAME;
106247 }
106248 return 0;
106249}
106250
106251/*
106252** Locate the in-memory structure that describes a particular database
106253** table given the name of that table and (optionally) the name of the
106254** database containing the table. Return NULL if not found. Also leave an
106255** error message in pParse->zErrMsg.
106256**
106257** The difference between this routine and sqlite3FindTable() is that this
106258** routine leaves an error message in pParse->zErrMsg where
106259** sqlite3FindTable() does not.
106260*/
106261SQLITE_PRIVATE Table *sqlite3LocateTable(
106262 Parse *pParse, /* context in which to report errors */
106263 u32 flags, /* LOCATE_VIEW or LOCATE_NOERR */
106264 const char *zName, /* Name of the table we are looking for */
106265 const char *zDbase /* Name of the database. Might be NULL */
106266){
106267 Table *p;
106268 sqlite3 *db = pParse->db;
106269
106270 /* Read the database schema. If an error occurs, leave an error message
106271 ** and code in pParse and return NULL. */
106272 if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0
106273 && SQLITE_OK!=sqlite3ReadSchema(pParse)
106274 ){
106275 return 0;
106276 }
106277
106278 p = sqlite3FindTable(db, zName, zDbase);
106279 if( p==0 ){
106280 const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table";
106281#ifndef SQLITE_OMIT_VIRTUALTABLE
106282 /* If zName is the not the name of a table in the schema created using
106283 ** CREATE, then check to see if it is the name of an virtual table that
106284 ** can be an eponymous virtual table. */
106285 Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
106286 if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){
106287 pMod = sqlite3PragmaVtabRegister(db, zName);
106288 }
106289 if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
106290 return pMod->pEpoTab;
106291 }
106292#endif
106293 if( (flags & LOCATE_NOERR)==0 ){
106294 if( zDbase ){
106295 sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
106296 }else{
106297 sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
106298 }
106299 pParse->checkSchema = 1;
106300 }
106301 }
106302
106303 return p;
106304}
106305
106306/*
106307** Locate the table identified by *p.
106308**
106309** This is a wrapper around sqlite3LocateTable(). The difference between
106310** sqlite3LocateTable() and this function is that this function restricts
106311** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
106312** non-NULL if it is part of a view or trigger program definition. See
106313** sqlite3FixSrcList() for details.
106314*/
106315SQLITE_PRIVATE Table *sqlite3LocateTableItem(
106316 Parse *pParse,
106317 u32 flags,
106318 struct SrcList_item *p
106319){
106320 const char *zDb;
106321 assert( p->pSchema==0 || p->zDatabase==0 );
106322 if( p->pSchema ){
106323 int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
106324 zDb = pParse->db->aDb[iDb].zDbSName;
106325 }else{
106326 zDb = p->zDatabase;
106327 }
106328 return sqlite3LocateTable(pParse, flags, p->zName, zDb);
106329}
106330
106331/*
106332** Locate the in-memory structure that describes
106333** a particular index given the name of that index
106334** and the name of the database that contains the index.
106335** Return NULL if not found.
106336**
106337** If zDatabase is 0, all databases are searched for the
106338** table and the first matching index is returned. (No checking
106339** for duplicate index names is done.) The search order is
106340** TEMP first, then MAIN, then any auxiliary databases added
106341** using the ATTACH command.
106342*/
106343SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
106344 Index *p = 0;
106345 int i;
106346 /* All mutexes are required for schema access. Make sure we hold them. */
106347 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
106348 for(i=OMIT_TEMPDB; i<db->nDb; i++){
106349 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
106350 Schema *pSchema = db->aDb[j].pSchema;
106351 assert( pSchema );
106352 if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zDbSName) ) continue;
106353 assert( sqlite3SchemaMutexHeld(db, j, 0) );
106354 p = sqlite3HashFind(&pSchema->idxHash, zName);
106355 if( p ) break;
106356 }
106357 return p;
106358}
106359
106360/*
106361** Reclaim the memory used by an index
106362*/
106363SQLITE_PRIVATE void sqlite3FreeIndex(sqlite3 *db, Index *p){
106364#ifndef SQLITE_OMIT_ANALYZE
106365 sqlite3DeleteIndexSamples(db, p);
106366#endif
106367 sqlite3ExprDelete(db, p->pPartIdxWhere);
106368 sqlite3ExprListDelete(db, p->aColExpr);
106369 sqlite3DbFree(db, p->zColAff);
106370 if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
106371#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
106372 sqlite3_free(p->aiRowEst);
106373#endif
106374 sqlite3DbFree(db, p);
106375}
106376
106377/*
106378** For the index called zIdxName which is found in the database iDb,
106379** unlike that index from its Table then remove the index from
106380** the index hash table and free all memory structures associated
106381** with the index.
106382*/
106383SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
106384 Index *pIndex;
106385 Hash *pHash;
106386
106387 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
106388 pHash = &db->aDb[iDb].pSchema->idxHash;
106389 pIndex = sqlite3HashInsert(pHash, zIdxName, 0);
106390 if( ALWAYS(pIndex) ){
106391 if( pIndex->pTable->pIndex==pIndex ){
106392 pIndex->pTable->pIndex = pIndex->pNext;
106393 }else{
106394 Index *p;
106395 /* Justification of ALWAYS(); The index must be on the list of
106396 ** indices. */
106397 p = pIndex->pTable->pIndex;
106398 while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
106399 if( ALWAYS(p && p->pNext==pIndex) ){
106400 p->pNext = pIndex->pNext;
106401 }
106402 }
106403 sqlite3FreeIndex(db, pIndex);
106404 }
106405 db->mDbFlags |= DBFLAG_SchemaChange;
106406}
106407
106408/*
106409** Look through the list of open database files in db->aDb[] and if
106410** any have been closed, remove them from the list. Reallocate the
106411** db->aDb[] structure to a smaller size, if possible.
106412**
106413** Entry 0 (the "main" database) and entry 1 (the "temp" database)
106414** are never candidates for being collapsed.
106415*/
106416SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3 *db){
106417 int i, j;
106418 for(i=j=2; i<db->nDb; i++){
106419 struct Db *pDb = &db->aDb[i];
106420 if( pDb->pBt==0 ){
106421 sqlite3DbFree(db, pDb->zDbSName);
106422 pDb->zDbSName = 0;
106423 continue;
106424 }
106425 if( j<i ){
106426 db->aDb[j] = db->aDb[i];
106427 }
106428 j++;
106429 }
106430 db->nDb = j;
106431 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
106432 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
106433 sqlite3DbFree(db, db->aDb);
106434 db->aDb = db->aDbStatic;
106435 }
106436}
106437
106438/*
106439** Reset the schema for the database at index iDb. Also reset the
106440** TEMP schema. The reset is deferred if db->nSchemaLock is not zero.
106441** Deferred resets may be run by calling with iDb<0.
106442*/
106443SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3 *db, int iDb){
106444 int i;
106445 assert( iDb<db->nDb );
106446
106447 if( iDb>=0 ){
106448 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
106449 DbSetProperty(db, iDb, DB_ResetWanted);
106450 DbSetProperty(db, 1, DB_ResetWanted);
106451 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
106452 }
106453
106454 if( db->nSchemaLock==0 ){
106455 for(i=0; i<db->nDb; i++){
106456 if( DbHasProperty(db, i, DB_ResetWanted) ){
106457 sqlite3SchemaClear(db->aDb[i].pSchema);
106458 }
106459 }
106460 }
106461}
106462
106463/*
106464** Erase all schema information from all attached databases (including
106465** "main" and "temp") for a single database connection.
106466*/
106467SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){
106468 int i;
106469 sqlite3BtreeEnterAll(db);
106470 for(i=0; i<db->nDb; i++){
106471 Db *pDb = &db->aDb[i];
106472 if( pDb->pSchema ){
106473 if( db->nSchemaLock==0 ){
106474 sqlite3SchemaClear(pDb->pSchema);
106475 }else{
106476 DbSetProperty(db, i, DB_ResetWanted);
106477 }
106478 }
106479 }
106480 db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk);
106481 sqlite3VtabUnlockList(db);
106482 sqlite3BtreeLeaveAll(db);
106483 if( db->nSchemaLock==0 ){
106484 sqlite3CollapseDatabaseArray(db);
106485 }
106486}
106487
106488/*
106489** This routine is called when a commit occurs.
106490*/
106491SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){
106492 db->mDbFlags &= ~DBFLAG_SchemaChange;
106493}
106494
106495/*
106496** Delete memory allocated for the column names of a table or view (the
106497** Table.aCol[] array).
106498*/
106499SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){
106500 int i;
106501 Column *pCol;
106502 assert( pTable!=0 );
106503 if( (pCol = pTable->aCol)!=0 ){
106504 for(i=0; i<pTable->nCol; i++, pCol++){
106505 sqlite3DbFree(db, pCol->zName);
106506 sqlite3ExprDelete(db, pCol->pDflt);
106507 sqlite3DbFree(db, pCol->zColl);
106508 }
106509 sqlite3DbFree(db, pTable->aCol);
106510 }
106511}
106512
106513/*
106514** Remove the memory data structures associated with the given
106515** Table. No changes are made to disk by this routine.
106516**
106517** This routine just deletes the data structure. It does not unlink
106518** the table data structure from the hash table. But it does destroy
106519** memory structures of the indices and foreign keys associated with
106520** the table.
106521**
106522** The db parameter is optional. It is needed if the Table object
106523** contains lookaside memory. (Table objects in the schema do not use
106524** lookaside memory, but some ephemeral Table objects do.) Or the
106525** db parameter can be used with db->pnBytesFreed to measure the memory
106526** used by the Table object.
106527*/
106528static void SQLITE_NOINLINE deleteTable(sqlite3 *db, Table *pTable){
106529 Index *pIndex, *pNext;
106530
106531#ifdef SQLITE_DEBUG
106532 /* Record the number of outstanding lookaside allocations in schema Tables
106533 ** prior to doing any free() operations. Since schema Tables do not use
106534 ** lookaside, this number should not change. */
106535 int nLookaside = 0;
106536 if( db && (pTable->tabFlags & TF_Ephemeral)==0 ){
106537 nLookaside = sqlite3LookasideUsed(db, 0);
106538 }
106539#endif
106540
106541 /* Delete all indices associated with this table. */
106542 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
106543 pNext = pIndex->pNext;
106544 assert( pIndex->pSchema==pTable->pSchema
106545 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
106546 if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){
106547 char *zName = pIndex->zName;
106548 TESTONLY ( Index *pOld = ) sqlite3HashInsert(
106549 &pIndex->pSchema->idxHash, zName, 0
106550 );
106551 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
106552 assert( pOld==pIndex || pOld==0 );
106553 }
106554 sqlite3FreeIndex(db, pIndex);
106555 }
106556
106557 /* Delete any foreign keys attached to this table. */
106558 sqlite3FkDelete(db, pTable);
106559
106560 /* Delete the Table structure itself.
106561 */
106562#ifdef SQLITE_ENABLE_NORMALIZE
106563 if( pTable->pColHash ){
106564 sqlite3HashClear(pTable->pColHash);
106565 sqlite3_free(pTable->pColHash);
106566 }
106567#endif
106568 sqlite3DeleteColumnNames(db, pTable);
106569 sqlite3DbFree(db, pTable->zName);
106570 sqlite3DbFree(db, pTable->zColAff);
106571 sqlite3SelectDelete(db, pTable->pSelect);
106572 sqlite3ExprListDelete(db, pTable->pCheck);
106573#ifndef SQLITE_OMIT_VIRTUALTABLE
106574 sqlite3VtabClear(db, pTable);
106575#endif
106576 sqlite3DbFree(db, pTable);
106577
106578 /* Verify that no lookaside memory was used by schema tables */
106579 assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) );
106580}
106581SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
106582 /* Do not delete the table until the reference count reaches zero. */
106583 if( !pTable ) return;
106584 if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return;
106585 deleteTable(db, pTable);
106586}
106587
106588
106589/*
106590** Unlink the given table from the hash tables and the delete the
106591** table structure with all its indices and foreign keys.
106592*/
106593SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
106594 Table *p;
106595 Db *pDb;
106596
106597 assert( db!=0 );
106598 assert( iDb>=0 && iDb<db->nDb );
106599 assert( zTabName );
106600 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
106601 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */
106602 pDb = &db->aDb[iDb];
106603 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
106604 sqlite3DeleteTable(db, p);
106605 db->mDbFlags |= DBFLAG_SchemaChange;
106606}
106607
106608/*
106609** Given a token, return a string that consists of the text of that
106610** token. Space to hold the returned string
106611** is obtained from sqliteMalloc() and must be freed by the calling
106612** function.
106613**
106614** Any quotation marks (ex: "name", 'name', [name], or `name`) that
106615** surround the body of the token are removed.
106616**
106617** Tokens are often just pointers into the original SQL text and so
106618** are not \000 terminated and are not persistent. The returned string
106619** is \000 terminated and is persistent.
106620*/
106621SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
106622 char *zName;
106623 if( pName ){
106624 zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
106625 sqlite3Dequote(zName);
106626 }else{
106627 zName = 0;
106628 }
106629 return zName;
106630}
106631
106632/*
106633** Open the sqlite_master table stored in database number iDb for
106634** writing. The table is opened using cursor 0.
106635*/
106636SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *p, int iDb){
106637 Vdbe *v = sqlite3GetVdbe(p);
106638 sqlite3TableLock(p, iDb, MASTER_ROOT, 1, MASTER_NAME);
106639 sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, MASTER_ROOT, iDb, 5);
106640 if( p->nTab==0 ){
106641 p->nTab = 1;
106642 }
106643}
106644
106645/*
106646** Parameter zName points to a nul-terminated buffer containing the name
106647** of a database ("main", "temp" or the name of an attached db). This
106648** function returns the index of the named database in db->aDb[], or
106649** -1 if the named db cannot be found.
106650*/
106651SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){
106652 int i = -1; /* Database number */
106653 if( zName ){
106654 Db *pDb;
106655 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
106656 if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break;
106657 /* "main" is always an acceptable alias for the primary database
106658 ** even if it has been renamed using SQLITE_DBCONFIG_MAINDBNAME. */
106659 if( i==0 && 0==sqlite3_stricmp("main", zName) ) break;
106660 }
106661 }
106662 return i;
106663}
106664
106665/*
106666** The token *pName contains the name of a database (either "main" or
106667** "temp" or the name of an attached db). This routine returns the
106668** index of the named database in db->aDb[], or -1 if the named db
106669** does not exist.
106670*/
106671SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){
106672 int i; /* Database number */
106673 char *zName; /* Name we are searching for */
106674 zName = sqlite3NameFromToken(db, pName);
106675 i = sqlite3FindDbName(db, zName);
106676 sqlite3DbFree(db, zName);
106677 return i;
106678}
106679
106680/* The table or view or trigger name is passed to this routine via tokens
106681** pName1 and pName2. If the table name was fully qualified, for example:
106682**
106683** CREATE TABLE xxx.yyy (...);
106684**
106685** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
106686** the table name is not fully qualified, i.e.:
106687**
106688** CREATE TABLE yyy(...);
106689**
106690** Then pName1 is set to "yyy" and pName2 is "".
106691**
106692** This routine sets the *ppUnqual pointer to point at the token (pName1 or
106693** pName2) that stores the unqualified table name. The index of the
106694** database "xxx" is returned.
106695*/
106696SQLITE_PRIVATE int sqlite3TwoPartName(
106697 Parse *pParse, /* Parsing and code generating context */
106698 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */
106699 Token *pName2, /* The "yyy" in the name "xxx.yyy" */
106700 Token **pUnqual /* Write the unqualified object name here */
106701){
106702 int iDb; /* Database holding the object */
106703 sqlite3 *db = pParse->db;
106704
106705 assert( pName2!=0 );
106706 if( pName2->n>0 ){
106707 if( db->init.busy ) {
106708 sqlite3ErrorMsg(pParse, "corrupt database");
106709 return -1;
106710 }
106711 *pUnqual = pName2;
106712 iDb = sqlite3FindDb(db, pName1);
106713 if( iDb<0 ){
106714 sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
106715 return -1;
106716 }
106717 }else{
106718 assert( db->init.iDb==0 || db->init.busy || IN_RENAME_OBJECT
106719 || (db->mDbFlags & DBFLAG_Vacuum)!=0);
106720 iDb = db->init.iDb;
106721 *pUnqual = pName1;
106722 }
106723 return iDb;
106724}
106725
106726/*
106727** True if PRAGMA writable_schema is ON
106728*/
106729SQLITE_PRIVATE int sqlite3WritableSchema(sqlite3 *db){
106730 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 );
106731 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
106732 SQLITE_WriteSchema );
106733 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
106734 SQLITE_Defensive );
106735 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
106736 (SQLITE_WriteSchema|SQLITE_Defensive) );
106737 return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema;
106738}
106739
106740/*
106741** This routine is used to check if the UTF-8 string zName is a legal
106742** unqualified name for a new schema object (table, index, view or
106743** trigger). All names are legal except those that begin with the string
106744** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
106745** is reserved for internal use.
106746*/
106747SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *pParse, const char *zName){
106748 if( !pParse->db->init.busy && pParse->nested==0
106749 && sqlite3WritableSchema(pParse->db)==0
106750 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
106751 sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
106752 return SQLITE_ERROR;
106753 }
106754 return SQLITE_OK;
106755}
106756
106757/*
106758** Return the PRIMARY KEY index of a table
106759*/
106760SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table *pTab){
106761 Index *p;
106762 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
106763 return p;
106764}
106765
106766/*
106767** Return the column of index pIdx that corresponds to table
106768** column iCol. Return -1 if not found.
106769*/
106770SQLITE_PRIVATE i16 sqlite3ColumnOfIndex(Index *pIdx, i16 iCol){
106771 int i;
106772 for(i=0; i<pIdx->nColumn; i++){
106773 if( iCol==pIdx->aiColumn[i] ) return i;
106774 }
106775 return -1;
106776}
106777
106778/*
106779** Begin constructing a new table representation in memory. This is
106780** the first of several action routines that get called in response
106781** to a CREATE TABLE statement. In particular, this routine is called
106782** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
106783** flag is true if the table should be stored in the auxiliary database
106784** file instead of in the main database file. This is normally the case
106785** when the "TEMP" or "TEMPORARY" keyword occurs in between
106786** CREATE and TABLE.
106787**
106788** The new table record is initialized and put in pParse->pNewTable.
106789** As more of the CREATE TABLE statement is parsed, additional action
106790** routines will be called to add more information to this record.
106791** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
106792** is called to complete the construction of the new table record.
106793*/
106794SQLITE_PRIVATE void sqlite3StartTable(
106795 Parse *pParse, /* Parser context */
106796 Token *pName1, /* First part of the name of the table or view */
106797 Token *pName2, /* Second part of the name of the table or view */
106798 int isTemp, /* True if this is a TEMP table */
106799 int isView, /* True if this is a VIEW */
106800 int isVirtual, /* True if this is a VIRTUAL table */
106801 int noErr /* Do nothing if table already exists */
106802){
106803 Table *pTable;
106804 char *zName = 0; /* The name of the new table */
106805 sqlite3 *db = pParse->db;
106806 Vdbe *v;
106807 int iDb; /* Database number to create the table in */
106808 Token *pName; /* Unqualified name of the table to create */
106809
106810 if( db->init.busy && db->init.newTnum==1 ){
106811 /* Special case: Parsing the sqlite_master or sqlite_temp_master schema */
106812 iDb = db->init.iDb;
106813 zName = sqlite3DbStrDup(db, SCHEMA_TABLE(iDb));
106814 pName = pName1;
106815 }else{
106816 /* The common case */
106817 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
106818 if( iDb<0 ) return;
106819 if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
106820 /* If creating a temp table, the name may not be qualified. Unless
106821 ** the database name is "temp" anyway. */
106822 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
106823 return;
106824 }
106825 if( !OMIT_TEMPDB && isTemp ) iDb = 1;
106826 zName = sqlite3NameFromToken(db, pName);
106827 if( IN_RENAME_OBJECT ){
106828 sqlite3RenameTokenMap(pParse, (void*)zName, pName);
106829 }
106830 }
106831 pParse->sNameToken = *pName;
106832 if( zName==0 ) return;
106833 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
106834 goto begin_table_error;
106835 }
106836 if( db->init.iDb==1 ) isTemp = 1;
106837#ifndef SQLITE_OMIT_AUTHORIZATION
106838 assert( isTemp==0 || isTemp==1 );
106839 assert( isView==0 || isView==1 );
106840 {
106841 static const u8 aCode[] = {
106842 SQLITE_CREATE_TABLE,
106843 SQLITE_CREATE_TEMP_TABLE,
106844 SQLITE_CREATE_VIEW,
106845 SQLITE_CREATE_TEMP_VIEW
106846 };
106847 char *zDb = db->aDb[iDb].zDbSName;
106848 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
106849 goto begin_table_error;
106850 }
106851 if( !isVirtual && sqlite3AuthCheck(pParse, (int)aCode[isTemp+2*isView],
106852 zName, 0, zDb) ){
106853 goto begin_table_error;
106854 }
106855 }
106856#endif
106857
106858 /* Make sure the new table name does not collide with an existing
106859 ** index or table name in the same database. Issue an error message if
106860 ** it does. The exception is if the statement being parsed was passed
106861 ** to an sqlite3_declare_vtab() call. In that case only the column names
106862 ** and types will be used, so there is no need to test for namespace
106863 ** collisions.
106864 */
106865 if( !IN_SPECIAL_PARSE ){
106866 char *zDb = db->aDb[iDb].zDbSName;
106867 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
106868 goto begin_table_error;
106869 }
106870 pTable = sqlite3FindTable(db, zName, zDb);
106871 if( pTable ){
106872 if( !noErr ){
106873 sqlite3ErrorMsg(pParse, "table %T already exists", pName);
106874 }else{
106875 assert( !db->init.busy || CORRUPT_DB );
106876 sqlite3CodeVerifySchema(pParse, iDb);
106877 }
106878 goto begin_table_error;
106879 }
106880 if( sqlite3FindIndex(db, zName, zDb)!=0 ){
106881 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
106882 goto begin_table_error;
106883 }
106884 }
106885
106886 pTable = sqlite3DbMallocZero(db, sizeof(Table));
106887 if( pTable==0 ){
106888 assert( db->mallocFailed );
106889 pParse->rc = SQLITE_NOMEM_BKPT;
106890 pParse->nErr++;
106891 goto begin_table_error;
106892 }
106893 pTable->zName = zName;
106894 pTable->iPKey = -1;
106895 pTable->pSchema = db->aDb[iDb].pSchema;
106896 pTable->nTabRef = 1;
106897#ifdef SQLITE_DEFAULT_ROWEST
106898 pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST);
106899#else
106900 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
106901#endif
106902 assert( pParse->pNewTable==0 );
106903 pParse->pNewTable = pTable;
106904
106905 /* If this is the magic sqlite_sequence table used by autoincrement,
106906 ** then record a pointer to this table in the main database structure
106907 ** so that INSERT can find the table easily.
106908 */
106909#ifndef SQLITE_OMIT_AUTOINCREMENT
106910 if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
106911 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
106912 pTable->pSchema->pSeqTab = pTable;
106913 }
106914#endif
106915
106916 /* Begin generating the code that will insert the table record into
106917 ** the SQLITE_MASTER table. Note in particular that we must go ahead
106918 ** and allocate the record number for the table entry now. Before any
106919 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
106920 ** indices to be created and the table record must come before the
106921 ** indices. Hence, the record number for the table must be allocated
106922 ** now.
106923 */
106924 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
106925 int addr1;
106926 int fileFormat;
106927 int reg1, reg2, reg3;
106928 /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */
106929 static const char nullRow[] = { 6, 0, 0, 0, 0, 0 };
106930 sqlite3BeginWriteOperation(pParse, 1, iDb);
106931
106932#ifndef SQLITE_OMIT_VIRTUALTABLE
106933 if( isVirtual ){
106934 sqlite3VdbeAddOp0(v, OP_VBegin);
106935 }
106936#endif
106937
106938 /* If the file format and encoding in the database have not been set,
106939 ** set them now.
106940 */
106941 reg1 = pParse->regRowid = ++pParse->nMem;
106942 reg2 = pParse->regRoot = ++pParse->nMem;
106943 reg3 = ++pParse->nMem;
106944 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
106945 sqlite3VdbeUsesBtree(v, iDb);
106946 addr1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v);
106947 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
106948 1 : SQLITE_MAX_FILE_FORMAT;
106949 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, fileFormat);
106950 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, ENC(db));
106951 sqlite3VdbeJumpHere(v, addr1);
106952
106953 /* This just creates a place-holder record in the sqlite_master table.
106954 ** The record created does not contain anything yet. It will be replaced
106955 ** by the real entry in code generated at sqlite3EndTable().
106956 **
106957 ** The rowid for the new entry is left in register pParse->regRowid.
106958 ** The root page number of the new table is left in reg pParse->regRoot.
106959 ** The rowid and root page number values are needed by the code that
106960 ** sqlite3EndTable will generate.
106961 */
106962#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
106963 if( isView || isVirtual ){
106964 sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2);
106965 }else
106966#endif
106967 {
106968 pParse->addrCrTab =
106969 sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, reg2, BTREE_INTKEY);
106970 }
106971 sqlite3OpenMasterTable(pParse, iDb);
106972 sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);
106973 sqlite3VdbeAddOp4(v, OP_Blob, 6, reg3, 0, nullRow, P4_STATIC);
106974 sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);
106975 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
106976 sqlite3VdbeAddOp0(v, OP_Close);
106977 }
106978
106979 /* Normal (non-error) return. */
106980 return;
106981
106982 /* If an error occurs, we jump here */
106983begin_table_error:
106984 sqlite3DbFree(db, zName);
106985 return;
106986}
106987
106988/* Set properties of a table column based on the (magical)
106989** name of the column.
106990*/
106991#if SQLITE_ENABLE_HIDDEN_COLUMNS
106992SQLITE_PRIVATE void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){
106993 if( sqlite3_strnicmp(pCol->zName, "__hidden__", 10)==0 ){
106994 pCol->colFlags |= COLFLAG_HIDDEN;
106995 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){
106996 pTab->tabFlags |= TF_OOOHidden;
106997 }
106998}
106999#endif
107000
107001
107002/*
107003** Add a new column to the table currently being constructed.
107004**
107005** The parser calls this routine once for each column declaration
107006** in a CREATE TABLE statement. sqlite3StartTable() gets called
107007** first to get things going. Then this routine is called for each
107008** column.
107009*/
107010SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token *pName, Token *pType){
107011 Table *p;
107012 int i;
107013 char *z;
107014 char *zType;
107015 Column *pCol;
107016 sqlite3 *db = pParse->db;
107017 if( (p = pParse->pNewTable)==0 ) return;
107018 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
107019 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
107020 return;
107021 }
107022 z = sqlite3DbMallocRaw(db, pName->n + pType->n + 2);
107023 if( z==0 ) return;
107024 if( IN_RENAME_OBJECT ) sqlite3RenameTokenMap(pParse, (void*)z, pName);
107025 memcpy(z, pName->z, pName->n);
107026 z[pName->n] = 0;
107027 sqlite3Dequote(z);
107028 for(i=0; i<p->nCol; i++){
107029 if( sqlite3_stricmp(z, p->aCol[i].zName)==0 ){
107030 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
107031 sqlite3DbFree(db, z);
107032 return;
107033 }
107034 }
107035 if( (p->nCol & 0x7)==0 ){
107036 Column *aNew;
107037 aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
107038 if( aNew==0 ){
107039 sqlite3DbFree(db, z);
107040 return;
107041 }
107042 p->aCol = aNew;
107043 }
107044 pCol = &p->aCol[p->nCol];
107045 memset(pCol, 0, sizeof(p->aCol[0]));
107046 pCol->zName = z;
107047 sqlite3ColumnPropertiesFromName(p, pCol);
107048
107049 if( pType->n==0 ){
107050 /* If there is no type specified, columns have the default affinity
107051 ** 'BLOB' with a default size of 4 bytes. */
107052 pCol->affinity = SQLITE_AFF_BLOB;
107053 pCol->szEst = 1;
107054#ifdef SQLITE_ENABLE_SORTER_REFERENCES
107055 if( 4>=sqlite3GlobalConfig.szSorterRef ){
107056 pCol->colFlags |= COLFLAG_SORTERREF;
107057 }
107058#endif
107059 }else{
107060 zType = z + sqlite3Strlen30(z) + 1;
107061 memcpy(zType, pType->z, pType->n);
107062 zType[pType->n] = 0;
107063 sqlite3Dequote(zType);
107064 pCol->affinity = sqlite3AffinityType(zType, pCol);
107065 pCol->colFlags |= COLFLAG_HASTYPE;
107066 }
107067 p->nCol++;
107068 pParse->constraintName.n = 0;
107069}
107070
107071/*
107072** This routine is called by the parser while in the middle of
107073** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
107074** been seen on a column. This routine sets the notNull flag on
107075** the column currently under construction.
107076*/
107077SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
107078 Table *p;
107079 Column *pCol;
107080 p = pParse->pNewTable;
107081 if( p==0 || NEVER(p->nCol<1) ) return;
107082 pCol = &p->aCol[p->nCol-1];
107083 pCol->notNull = (u8)onError;
107084 p->tabFlags |= TF_HasNotNull;
107085
107086 /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created
107087 ** on this column. */
107088 if( pCol->colFlags & COLFLAG_UNIQUE ){
107089 Index *pIdx;
107090 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
107091 assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None );
107092 if( pIdx->aiColumn[0]==p->nCol-1 ){
107093 pIdx->uniqNotNull = 1;
107094 }
107095 }
107096 }
107097}
107098
107099/*
107100** Scan the column type name zType (length nType) and return the
107101** associated affinity type.
107102**
107103** This routine does a case-independent search of zType for the
107104** substrings in the following table. If one of the substrings is
107105** found, the corresponding affinity is returned. If zType contains
107106** more than one of the substrings, entries toward the top of
107107** the table take priority. For example, if zType is 'BLOBINT',
107108** SQLITE_AFF_INTEGER is returned.
107109**
107110** Substring | Affinity
107111** --------------------------------
107112** 'INT' | SQLITE_AFF_INTEGER
107113** 'CHAR' | SQLITE_AFF_TEXT
107114** 'CLOB' | SQLITE_AFF_TEXT
107115** 'TEXT' | SQLITE_AFF_TEXT
107116** 'BLOB' | SQLITE_AFF_BLOB
107117** 'REAL' | SQLITE_AFF_REAL
107118** 'FLOA' | SQLITE_AFF_REAL
107119** 'DOUB' | SQLITE_AFF_REAL
107120**
107121** If none of the substrings in the above table are found,
107122** SQLITE_AFF_NUMERIC is returned.
107123*/
107124SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn, Column *pCol){
107125 u32 h = 0;
107126 char aff = SQLITE_AFF_NUMERIC;
107127 const char *zChar = 0;
107128
107129 assert( zIn!=0 );
107130 while( zIn[0] ){
107131 h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
107132 zIn++;
107133 if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */
107134 aff = SQLITE_AFF_TEXT;
107135 zChar = zIn;
107136 }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */
107137 aff = SQLITE_AFF_TEXT;
107138 }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */
107139 aff = SQLITE_AFF_TEXT;
107140 }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */
107141 && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
107142 aff = SQLITE_AFF_BLOB;
107143 if( zIn[0]=='(' ) zChar = zIn;
107144#ifndef SQLITE_OMIT_FLOATING_POINT
107145 }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */
107146 && aff==SQLITE_AFF_NUMERIC ){
107147 aff = SQLITE_AFF_REAL;
107148 }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */
107149 && aff==SQLITE_AFF_NUMERIC ){
107150 aff = SQLITE_AFF_REAL;
107151 }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */
107152 && aff==SQLITE_AFF_NUMERIC ){
107153 aff = SQLITE_AFF_REAL;
107154#endif
107155 }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */
107156 aff = SQLITE_AFF_INTEGER;
107157 break;
107158 }
107159 }
107160
107161 /* If pCol is not NULL, store an estimate of the field size. The
107162 ** estimate is scaled so that the size of an integer is 1. */
107163 if( pCol ){
107164 int v = 0; /* default size is approx 4 bytes */
107165 if( aff<SQLITE_AFF_NUMERIC ){
107166 if( zChar ){
107167 while( zChar[0] ){
107168 if( sqlite3Isdigit(zChar[0]) ){
107169 /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
107170 sqlite3GetInt32(zChar, &v);
107171 break;
107172 }
107173 zChar++;
107174 }
107175 }else{
107176 v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
107177 }
107178 }
107179#ifdef SQLITE_ENABLE_SORTER_REFERENCES
107180 if( v>=sqlite3GlobalConfig.szSorterRef ){
107181 pCol->colFlags |= COLFLAG_SORTERREF;
107182 }
107183#endif
107184 v = v/4 + 1;
107185 if( v>255 ) v = 255;
107186 pCol->szEst = v;
107187 }
107188 return aff;
107189}
107190
107191/*
107192** The expression is the default value for the most recently added column
107193** of the table currently under construction.
107194**
107195** Default value expressions must be constant. Raise an exception if this
107196** is not the case.
107197**
107198** This routine is called by the parser while in the middle of
107199** parsing a CREATE TABLE statement.
107200*/
107201SQLITE_PRIVATE void sqlite3AddDefaultValue(
107202 Parse *pParse, /* Parsing context */
107203 Expr *pExpr, /* The parsed expression of the default value */
107204 const char *zStart, /* Start of the default value text */
107205 const char *zEnd /* First character past end of defaut value text */
107206){
107207 Table *p;
107208 Column *pCol;
107209 sqlite3 *db = pParse->db;
107210 p = pParse->pNewTable;
107211 if( p!=0 ){
107212 pCol = &(p->aCol[p->nCol-1]);
107213 if( !sqlite3ExprIsConstantOrFunction(pExpr, db->init.busy) ){
107214 sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
107215 pCol->zName);
107216 }else{
107217 /* A copy of pExpr is used instead of the original, as pExpr contains
107218 ** tokens that point to volatile memory.
107219 */
107220 Expr x;
107221 sqlite3ExprDelete(db, pCol->pDflt);
107222 memset(&x, 0, sizeof(x));
107223 x.op = TK_SPAN;
107224 x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd);
107225 x.pLeft = pExpr;
107226 x.flags = EP_Skip;
107227 pCol->pDflt = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE);
107228 sqlite3DbFree(db, x.u.zToken);
107229 }
107230 }
107231 if( IN_RENAME_OBJECT ){
107232 sqlite3RenameExprUnmap(pParse, pExpr);
107233 }
107234 sqlite3ExprDelete(db, pExpr);
107235}
107236
107237/*
107238** Backwards Compatibility Hack:
107239**
107240** Historical versions of SQLite accepted strings as column names in
107241** indexes and PRIMARY KEY constraints and in UNIQUE constraints. Example:
107242**
107243** CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim)
107244** CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC);
107245**
107246** This is goofy. But to preserve backwards compatibility we continue to
107247** accept it. This routine does the necessary conversion. It converts
107248** the expression given in its argument from a TK_STRING into a TK_ID
107249** if the expression is just a TK_STRING with an optional COLLATE clause.
107250** If the epxression is anything other than TK_STRING, the expression is
107251** unchanged.
107252*/
107253static void sqlite3StringToId(Expr *p){
107254 if( p->op==TK_STRING ){
107255 p->op = TK_ID;
107256 }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){
107257 p->pLeft->op = TK_ID;
107258 }
107259}
107260
107261/*
107262** Designate the PRIMARY KEY for the table. pList is a list of names
107263** of columns that form the primary key. If pList is NULL, then the
107264** most recently added column of the table is the primary key.
107265**
107266** A table can have at most one primary key. If the table already has
107267** a primary key (and this is the second primary key) then create an
107268** error.
107269**
107270** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
107271** then we will try to use that column as the rowid. Set the Table.iPKey
107272** field of the table under construction to be the index of the
107273** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
107274** no INTEGER PRIMARY KEY.
107275**
107276** If the key is not an INTEGER PRIMARY KEY, then create a unique
107277** index for the key. No index is created for INTEGER PRIMARY KEYs.
107278*/
107279SQLITE_PRIVATE void sqlite3AddPrimaryKey(
107280 Parse *pParse, /* Parsing context */
107281 ExprList *pList, /* List of field names to be indexed */
107282 int onError, /* What to do with a uniqueness conflict */
107283 int autoInc, /* True if the AUTOINCREMENT keyword is present */
107284 int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */
107285){
107286 Table *pTab = pParse->pNewTable;
107287 Column *pCol = 0;
107288 int iCol = -1, i;
107289 int nTerm;
107290 if( pTab==0 ) goto primary_key_exit;
107291 if( pTab->tabFlags & TF_HasPrimaryKey ){
107292 sqlite3ErrorMsg(pParse,
107293 "table \"%s\" has more than one primary key", pTab->zName);
107294 goto primary_key_exit;
107295 }
107296 pTab->tabFlags |= TF_HasPrimaryKey;
107297 if( pList==0 ){
107298 iCol = pTab->nCol - 1;
107299 pCol = &pTab->aCol[iCol];
107300 pCol->colFlags |= COLFLAG_PRIMKEY;
107301 nTerm = 1;
107302 }else{
107303 nTerm = pList->nExpr;
107304 for(i=0; i<nTerm; i++){
107305 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr);
107306 assert( pCExpr!=0 );
107307 sqlite3StringToId(pCExpr);
107308 if( pCExpr->op==TK_ID ){
107309 const char *zCName = pCExpr->u.zToken;
107310 for(iCol=0; iCol<pTab->nCol; iCol++){
107311 if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zName)==0 ){
107312 pCol = &pTab->aCol[iCol];
107313 pCol->colFlags |= COLFLAG_PRIMKEY;
107314 break;
107315 }
107316 }
107317 }
107318 }
107319 }
107320 if( nTerm==1
107321 && pCol
107322 && sqlite3StrICmp(sqlite3ColumnType(pCol,""), "INTEGER")==0
107323 && sortOrder!=SQLITE_SO_DESC
107324 ){
107325 if( IN_RENAME_OBJECT && pList ){
107326 sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pList->a[0].pExpr);
107327 }
107328 pTab->iPKey = iCol;
107329 pTab->keyConf = (u8)onError;
107330 assert( autoInc==0 || autoInc==1 );
107331 pTab->tabFlags |= autoInc*TF_Autoincrement;
107332 if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder;
107333 }else if( autoInc ){
107334#ifndef SQLITE_OMIT_AUTOINCREMENT
107335 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
107336 "INTEGER PRIMARY KEY");
107337#endif
107338 }else{
107339 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
107340 0, sortOrder, 0, SQLITE_IDXTYPE_PRIMARYKEY);
107341 pList = 0;
107342 }
107343
107344primary_key_exit:
107345 sqlite3ExprListDelete(pParse->db, pList);
107346 return;
107347}
107348
107349/*
107350** Add a new CHECK constraint to the table currently under construction.
107351*/
107352SQLITE_PRIVATE void sqlite3AddCheckConstraint(
107353 Parse *pParse, /* Parsing context */
107354 Expr *pCheckExpr /* The check expression */
107355){
107356#ifndef SQLITE_OMIT_CHECK
107357 Table *pTab = pParse->pNewTable;
107358 sqlite3 *db = pParse->db;
107359 if( pTab && !IN_DECLARE_VTAB
107360 && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
107361 ){
107362 pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
107363 if( pParse->constraintName.n ){
107364 sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
107365 }
107366 }else
107367#endif
107368 {
107369 sqlite3ExprDelete(pParse->db, pCheckExpr);
107370 }
107371}
107372
107373/*
107374** Set the collation function of the most recently parsed table column
107375** to the CollSeq given.
107376*/
107377SQLITE_PRIVATE void sqlite3AddCollateType(Parse *pParse, Token *pToken){
107378 Table *p;
107379 int i;
107380 char *zColl; /* Dequoted name of collation sequence */
107381 sqlite3 *db;
107382
107383 if( (p = pParse->pNewTable)==0 ) return;
107384 i = p->nCol-1;
107385 db = pParse->db;
107386 zColl = sqlite3NameFromToken(db, pToken);
107387 if( !zColl ) return;
107388
107389 if( sqlite3LocateCollSeq(pParse, zColl) ){
107390 Index *pIdx;
107391 sqlite3DbFree(db, p->aCol[i].zColl);
107392 p->aCol[i].zColl = zColl;
107393
107394 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
107395 ** then an index may have been created on this column before the
107396 ** collation type was added. Correct this if it is the case.
107397 */
107398 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
107399 assert( pIdx->nKeyCol==1 );
107400 if( pIdx->aiColumn[0]==i ){
107401 pIdx->azColl[0] = p->aCol[i].zColl;
107402 }
107403 }
107404 }else{
107405 sqlite3DbFree(db, zColl);
107406 }
107407}
107408
107409/*
107410** This function returns the collation sequence for database native text
107411** encoding identified by the string zName, length nName.
107412**
107413** If the requested collation sequence is not available, or not available
107414** in the database native encoding, the collation factory is invoked to
107415** request it. If the collation factory does not supply such a sequence,
107416** and the sequence is available in another text encoding, then that is
107417** returned instead.
107418**
107419** If no versions of the requested collations sequence are available, or
107420** another error occurs, NULL is returned and an error message written into
107421** pParse.
107422**
107423** This routine is a wrapper around sqlite3FindCollSeq(). This routine
107424** invokes the collation factory if the named collation cannot be found
107425** and generates an error message.
107426**
107427** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
107428*/
107429SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
107430 sqlite3 *db = pParse->db;
107431 u8 enc = ENC(db);
107432 u8 initbusy = db->init.busy;
107433 CollSeq *pColl;
107434
107435 pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
107436 if( !initbusy && (!pColl || !pColl->xCmp) ){
107437 pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName);
107438 }
107439
107440 return pColl;
107441}
107442
107443
107444/*
107445** Generate code that will increment the schema cookie.
107446**
107447** The schema cookie is used to determine when the schema for the
107448** database changes. After each schema change, the cookie value
107449** changes. When a process first reads the schema it records the
107450** cookie. Thereafter, whenever it goes to access the database,
107451** it checks the cookie to make sure the schema has not changed
107452** since it was last read.
107453**
107454** This plan is not completely bullet-proof. It is possible for
107455** the schema to change multiple times and for the cookie to be
107456** set back to prior value. But schema changes are infrequent
107457** and the probability of hitting the same cookie value is only
107458** 1 chance in 2^32. So we're safe enough.
107459**
107460** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
107461** the schema-version whenever the schema changes.
107462*/
107463SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
107464 sqlite3 *db = pParse->db;
107465 Vdbe *v = pParse->pVdbe;
107466 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
107467 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION,
107468 (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie));
107469}
107470
107471/*
107472** Measure the number of characters needed to output the given
107473** identifier. The number returned includes any quotes used
107474** but does not include the null terminator.
107475**
107476** The estimate is conservative. It might be larger that what is
107477** really needed.
107478*/
107479static int identLength(const char *z){
107480 int n;
107481 for(n=0; *z; n++, z++){
107482 if( *z=='"' ){ n++; }
107483 }
107484 return n + 2;
107485}
107486
107487/*
107488** The first parameter is a pointer to an output buffer. The second
107489** parameter is a pointer to an integer that contains the offset at
107490** which to write into the output buffer. This function copies the
107491** nul-terminated string pointed to by the third parameter, zSignedIdent,
107492** to the specified offset in the buffer and updates *pIdx to refer
107493** to the first byte after the last byte written before returning.
107494**
107495** If the string zSignedIdent consists entirely of alpha-numeric
107496** characters, does not begin with a digit and is not an SQL keyword,
107497** then it is copied to the output buffer exactly as it is. Otherwise,
107498** it is quoted using double-quotes.
107499*/
107500static void identPut(char *z, int *pIdx, char *zSignedIdent){
107501 unsigned char *zIdent = (unsigned char*)zSignedIdent;
107502 int i, j, needQuote;
107503 i = *pIdx;
107504
107505 for(j=0; zIdent[j]; j++){
107506 if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
107507 }
107508 needQuote = sqlite3Isdigit(zIdent[0])
107509 || sqlite3KeywordCode(zIdent, j)!=TK_ID
107510 || zIdent[j]!=0
107511 || j==0;
107512
107513 if( needQuote ) z[i++] = '"';
107514 for(j=0; zIdent[j]; j++){
107515 z[i++] = zIdent[j];
107516 if( zIdent[j]=='"' ) z[i++] = '"';
107517 }
107518 if( needQuote ) z[i++] = '"';
107519 z[i] = 0;
107520 *pIdx = i;
107521}
107522
107523/*
107524** Generate a CREATE TABLE statement appropriate for the given
107525** table. Memory to hold the text of the statement is obtained
107526** from sqliteMalloc() and must be freed by the calling function.
107527*/
107528static char *createTableStmt(sqlite3 *db, Table *p){
107529 int i, k, n;
107530 char *zStmt;
107531 char *zSep, *zSep2, *zEnd;
107532 Column *pCol;
107533 n = 0;
107534 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
107535 n += identLength(pCol->zName) + 5;
107536 }
107537 n += identLength(p->zName);
107538 if( n<50 ){
107539 zSep = "";
107540 zSep2 = ",";
107541 zEnd = ")";
107542 }else{
107543 zSep = "\n ";
107544 zSep2 = ",\n ";
107545 zEnd = "\n)";
107546 }
107547 n += 35 + 6*p->nCol;
107548 zStmt = sqlite3DbMallocRaw(0, n);
107549 if( zStmt==0 ){
107550 sqlite3OomFault(db);
107551 return 0;
107552 }
107553 sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
107554 k = sqlite3Strlen30(zStmt);
107555 identPut(zStmt, &k, p->zName);
107556 zStmt[k++] = '(';
107557 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
107558 static const char * const azType[] = {
107559 /* SQLITE_AFF_BLOB */ "",
107560 /* SQLITE_AFF_TEXT */ " TEXT",
107561 /* SQLITE_AFF_NUMERIC */ " NUM",
107562 /* SQLITE_AFF_INTEGER */ " INT",
107563 /* SQLITE_AFF_REAL */ " REAL"
107564 };
107565 int len;
107566 const char *zType;
107567
107568 sqlite3_snprintf(n-k, &zStmt[k], zSep);
107569 k += sqlite3Strlen30(&zStmt[k]);
107570 zSep = zSep2;
107571 identPut(zStmt, &k, pCol->zName);
107572 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
107573 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
107574 testcase( pCol->affinity==SQLITE_AFF_BLOB );
107575 testcase( pCol->affinity==SQLITE_AFF_TEXT );
107576 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
107577 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
107578 testcase( pCol->affinity==SQLITE_AFF_REAL );
107579
107580 zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
107581 len = sqlite3Strlen30(zType);
107582 assert( pCol->affinity==SQLITE_AFF_BLOB
107583 || pCol->affinity==sqlite3AffinityType(zType, 0) );
107584 memcpy(&zStmt[k], zType, len);
107585 k += len;
107586 assert( k<=n );
107587 }
107588 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
107589 return zStmt;
107590}
107591
107592/*
107593** Resize an Index object to hold N columns total. Return SQLITE_OK
107594** on success and SQLITE_NOMEM on an OOM error.
107595*/
107596static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){
107597 char *zExtra;
107598 int nByte;
107599 if( pIdx->nColumn>=N ) return SQLITE_OK;
107600 assert( pIdx->isResized==0 );
107601 nByte = (sizeof(char*) + sizeof(i16) + 1)*N;
107602 zExtra = sqlite3DbMallocZero(db, nByte);
107603 if( zExtra==0 ) return SQLITE_NOMEM_BKPT;
107604 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
107605 pIdx->azColl = (const char**)zExtra;
107606 zExtra += sizeof(char*)*N;
107607 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
107608 pIdx->aiColumn = (i16*)zExtra;
107609 zExtra += sizeof(i16)*N;
107610 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
107611 pIdx->aSortOrder = (u8*)zExtra;
107612 pIdx->nColumn = N;
107613 pIdx->isResized = 1;
107614 return SQLITE_OK;
107615}
107616
107617/*
107618** Estimate the total row width for a table.
107619*/
107620static void estimateTableWidth(Table *pTab){
107621 unsigned wTable = 0;
107622 const Column *pTabCol;
107623 int i;
107624 for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
107625 wTable += pTabCol->szEst;
107626 }
107627 if( pTab->iPKey<0 ) wTable++;
107628 pTab->szTabRow = sqlite3LogEst(wTable*4);
107629}
107630
107631/*
107632** Estimate the average size of a row for an index.
107633*/
107634static void estimateIndexWidth(Index *pIdx){
107635 unsigned wIndex = 0;
107636 int i;
107637 const Column *aCol = pIdx->pTable->aCol;
107638 for(i=0; i<pIdx->nColumn; i++){
107639 i16 x = pIdx->aiColumn[i];
107640 assert( x<pIdx->pTable->nCol );
107641 wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst;
107642 }
107643 pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
107644}
107645
107646/* Return true if value x is found any of the first nCol entries of aiCol[]
107647*/
107648static int hasColumn(const i16 *aiCol, int nCol, int x){
107649 while( nCol-- > 0 ) if( x==*(aiCol++) ) return 1;
107650 return 0;
107651}
107652
107653/* Recompute the colNotIdxed field of the Index.
107654**
107655** colNotIdxed is a bitmask that has a 0 bit representing each indexed
107656** columns that are within the first 63 columns of the table. The
107657** high-order bit of colNotIdxed is always 1. All unindexed columns
107658** of the table have a 1.
107659**
107660** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask
107661** to determine if the index is covering index.
107662*/
107663static void recomputeColumnsNotIndexed(Index *pIdx){
107664 Bitmask m = 0;
107665 int j;
107666 for(j=pIdx->nColumn-1; j>=0; j--){
107667 int x = pIdx->aiColumn[j];
107668 if( x>=0 ){
107669 testcase( x==BMS-1 );
107670 testcase( x==BMS-2 );
107671 if( x<BMS-1 ) m |= MASKBIT(x);
107672 }
107673 }
107674 pIdx->colNotIdxed = ~m;
107675 assert( (pIdx->colNotIdxed>>63)==1 );
107676}
107677
107678/*
107679** This routine runs at the end of parsing a CREATE TABLE statement that
107680** has a WITHOUT ROWID clause. The job of this routine is to convert both
107681** internal schema data structures and the generated VDBE code so that they
107682** are appropriate for a WITHOUT ROWID table instead of a rowid table.
107683** Changes include:
107684**
107685** (1) Set all columns of the PRIMARY KEY schema object to be NOT NULL.
107686** (2) Convert P3 parameter of the OP_CreateBtree from BTREE_INTKEY
107687** into BTREE_BLOBKEY.
107688** (3) Bypass the creation of the sqlite_master table entry
107689** for the PRIMARY KEY as the primary key index is now
107690** identified by the sqlite_master table entry of the table itself.
107691** (4) Set the Index.tnum of the PRIMARY KEY Index object in the
107692** schema to the rootpage from the main table.
107693** (5) Add all table columns to the PRIMARY KEY Index object
107694** so that the PRIMARY KEY is a covering index. The surplus
107695** columns are part of KeyInfo.nAllField and are not used for
107696** sorting or lookup or uniqueness checks.
107697** (6) Replace the rowid tail on all automatically generated UNIQUE
107698** indices with the PRIMARY KEY columns.
107699**
107700** For virtual tables, only (1) is performed.
107701*/
107702static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
107703 Index *pIdx;
107704 Index *pPk;
107705 int nPk;
107706 int i, j;
107707 sqlite3 *db = pParse->db;
107708 Vdbe *v = pParse->pVdbe;
107709
107710 /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables)
107711 */
107712 if( !db->init.imposterTable ){
107713 for(i=0; i<pTab->nCol; i++){
107714 if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 ){
107715 pTab->aCol[i].notNull = OE_Abort;
107716 }
107717 }
107718 }
107719
107720 /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY
107721 ** into BTREE_BLOBKEY.
107722 */
107723 if( pParse->addrCrTab ){
107724 assert( v );
107725 sqlite3VdbeChangeP3(v, pParse->addrCrTab, BTREE_BLOBKEY);
107726 }
107727
107728 /* Locate the PRIMARY KEY index. Or, if this table was originally
107729 ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index.
107730 */
107731 if( pTab->iPKey>=0 ){
107732 ExprList *pList;
107733 Token ipkToken;
107734 sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zName);
107735 pList = sqlite3ExprListAppend(pParse, 0,
107736 sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0));
107737 if( pList==0 ) return;
107738 pList->a[0].sortOrder = pParse->iPkSortOrder;
107739 assert( pParse->pNewTable==pTab );
107740 sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
107741 SQLITE_IDXTYPE_PRIMARYKEY);
107742 if( db->mallocFailed || pParse->nErr ) return;
107743 pPk = sqlite3PrimaryKeyIndex(pTab);
107744 pTab->iPKey = -1;
107745 }else{
107746 pPk = sqlite3PrimaryKeyIndex(pTab);
107747
107748 /*
107749 ** Remove all redundant columns from the PRIMARY KEY. For example, change
107750 ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later
107751 ** code assumes the PRIMARY KEY contains no repeated columns.
107752 */
107753 for(i=j=1; i<pPk->nKeyCol; i++){
107754 if( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) ){
107755 pPk->nColumn--;
107756 }else{
107757 pPk->aiColumn[j++] = pPk->aiColumn[i];
107758 }
107759 }
107760 pPk->nKeyCol = j;
107761 }
107762 assert( pPk!=0 );
107763 pPk->isCovering = 1;
107764 if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
107765 nPk = pPk->nKeyCol;
107766
107767 /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master
107768 ** table entry. This is only required if currently generating VDBE
107769 ** code for a CREATE TABLE (not when parsing one as part of reading
107770 ** a database schema). */
107771 if( v && pPk->tnum>0 ){
107772 assert( db->init.busy==0 );
107773 sqlite3VdbeChangeOpcode(v, pPk->tnum, OP_Goto);
107774 }
107775
107776 /* The root page of the PRIMARY KEY is the table root page */
107777 pPk->tnum = pTab->tnum;
107778
107779 /* Update the in-memory representation of all UNIQUE indices by converting
107780 ** the final rowid column into one or more columns of the PRIMARY KEY.
107781 */
107782 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
107783 int n;
107784 if( IsPrimaryKeyIndex(pIdx) ) continue;
107785 for(i=n=0; i<nPk; i++){
107786 if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ) n++;
107787 }
107788 if( n==0 ){
107789 /* This index is a superset of the primary key */
107790 pIdx->nColumn = pIdx->nKeyCol;
107791 continue;
107792 }
107793 if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
107794 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
107795 if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ){
107796 pIdx->aiColumn[j] = pPk->aiColumn[i];
107797 pIdx->azColl[j] = pPk->azColl[i];
107798 j++;
107799 }
107800 }
107801 assert( pIdx->nColumn>=pIdx->nKeyCol+n );
107802 assert( pIdx->nColumn>=j );
107803 }
107804
107805 /* Add all table columns to the PRIMARY KEY index
107806 */
107807 if( nPk<pTab->nCol ){
107808 if( resizeIndexObject(db, pPk, pTab->nCol) ) return;
107809 for(i=0, j=nPk; i<pTab->nCol; i++){
107810 if( !hasColumn(pPk->aiColumn, j, i) ){
107811 assert( j<pPk->nColumn );
107812 pPk->aiColumn[j] = i;
107813 pPk->azColl[j] = sqlite3StrBINARY;
107814 j++;
107815 }
107816 }
107817 assert( pPk->nColumn==j );
107818 assert( pTab->nCol==j );
107819 }else{
107820 pPk->nColumn = pTab->nCol;
107821 }
107822 recomputeColumnsNotIndexed(pPk);
107823}
107824
107825#ifndef SQLITE_OMIT_VIRTUALTABLE
107826/*
107827** Return true if zName is a shadow table name in the current database
107828** connection.
107829**
107830** zName is temporarily modified while this routine is running, but is
107831** restored to its original value prior to this routine returning.
107832*/
107833static int isShadowTableName(sqlite3 *db, char *zName){
107834 char *zTail; /* Pointer to the last "_" in zName */
107835 Table *pTab; /* Table that zName is a shadow of */
107836 Module *pMod; /* Module for the virtual table */
107837
107838 zTail = strrchr(zName, '_');
107839 if( zTail==0 ) return 0;
107840 *zTail = 0;
107841 pTab = sqlite3FindTable(db, zName, 0);
107842 *zTail = '_';
107843 if( pTab==0 ) return 0;
107844 if( !IsVirtual(pTab) ) return 0;
107845 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->azModuleArg[0]);
107846 if( pMod==0 ) return 0;
107847 if( pMod->pModule->iVersion<3 ) return 0;
107848 if( pMod->pModule->xShadowName==0 ) return 0;
107849 return pMod->pModule->xShadowName(zTail+1);
107850}
107851#else
107852# define isShadowTableName(x,y) 0
107853#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
107854
107855/*
107856** This routine is called to report the final ")" that terminates
107857** a CREATE TABLE statement.
107858**
107859** The table structure that other action routines have been building
107860** is added to the internal hash tables, assuming no errors have
107861** occurred.
107862**
107863** An entry for the table is made in the master table on disk, unless
107864** this is a temporary table or db->init.busy==1. When db->init.busy==1
107865** it means we are reading the sqlite_master table because we just
107866** connected to the database or because the sqlite_master table has
107867** recently changed, so the entry for this table already exists in
107868** the sqlite_master table. We do not want to create it again.
107869**
107870** If the pSelect argument is not NULL, it means that this routine
107871** was called to create a table generated from a
107872** "CREATE TABLE ... AS SELECT ..." statement. The column names of
107873** the new table will match the result set of the SELECT.
107874*/
107875SQLITE_PRIVATE void sqlite3EndTable(
107876 Parse *pParse, /* Parse context */
107877 Token *pCons, /* The ',' token after the last column defn. */
107878 Token *pEnd, /* The ')' before options in the CREATE TABLE */
107879 u8 tabOpts, /* Extra table options. Usually 0. */
107880 Select *pSelect /* Select from a "CREATE ... AS SELECT" */
107881){
107882 Table *p; /* The new table */
107883 sqlite3 *db = pParse->db; /* The database connection */
107884 int iDb; /* Database in which the table lives */
107885 Index *pIdx; /* An implied index of the table */
107886
107887 if( pEnd==0 && pSelect==0 ){
107888 return;
107889 }
107890 assert( !db->mallocFailed );
107891 p = pParse->pNewTable;
107892 if( p==0 ) return;
107893
107894 if( pSelect==0 && isShadowTableName(db, p->zName) ){
107895 p->tabFlags |= TF_Shadow;
107896 }
107897
107898 /* If the db->init.busy is 1 it means we are reading the SQL off the
107899 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
107900 ** So do not write to the disk again. Extract the root page number
107901 ** for the table from the db->init.newTnum field. (The page number
107902 ** should have been put there by the sqliteOpenCb routine.)
107903 **
107904 ** If the root page number is 1, that means this is the sqlite_master
107905 ** table itself. So mark it read-only.
107906 */
107907 if( db->init.busy ){
107908 if( pSelect ){
107909 sqlite3ErrorMsg(pParse, "");
107910 return;
107911 }
107912 p->tnum = db->init.newTnum;
107913 if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
107914 }
107915
107916 /* Special processing for WITHOUT ROWID Tables */
107917 if( tabOpts & TF_WithoutRowid ){
107918 if( (p->tabFlags & TF_Autoincrement) ){
107919 sqlite3ErrorMsg(pParse,
107920 "AUTOINCREMENT not allowed on WITHOUT ROWID tables");
107921 return;
107922 }
107923 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
107924 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
107925 }else{
107926 p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
107927 convertToWithoutRowidTable(pParse, p);
107928 }
107929 }
107930
107931 iDb = sqlite3SchemaToIndex(db, p->pSchema);
107932
107933#ifndef SQLITE_OMIT_CHECK
107934 /* Resolve names in all CHECK constraint expressions.
107935 */
107936 if( p->pCheck ){
107937 sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
107938 }
107939#endif /* !defined(SQLITE_OMIT_CHECK) */
107940
107941 /* Estimate the average row size for the table and for all implied indices */
107942 estimateTableWidth(p);
107943 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
107944 estimateIndexWidth(pIdx);
107945 }
107946
107947 /* If not initializing, then create a record for the new table
107948 ** in the SQLITE_MASTER table of the database.
107949 **
107950 ** If this is a TEMPORARY table, write the entry into the auxiliary
107951 ** file instead of into the main database file.
107952 */
107953 if( !db->init.busy ){
107954 int n;
107955 Vdbe *v;
107956 char *zType; /* "view" or "table" */
107957 char *zType2; /* "VIEW" or "TABLE" */
107958 char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */
107959
107960 v = sqlite3GetVdbe(pParse);
107961 if( NEVER(v==0) ) return;
107962
107963 sqlite3VdbeAddOp1(v, OP_Close, 0);
107964
107965 /*
107966 ** Initialize zType for the new view or table.
107967 */
107968 if( p->pSelect==0 ){
107969 /* A regular table */
107970 zType = "table";
107971 zType2 = "TABLE";
107972#ifndef SQLITE_OMIT_VIEW
107973 }else{
107974 /* A view */
107975 zType = "view";
107976 zType2 = "VIEW";
107977#endif
107978 }
107979
107980 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
107981 ** statement to populate the new table. The root-page number for the
107982 ** new table is in register pParse->regRoot.
107983 **
107984 ** Once the SELECT has been coded by sqlite3Select(), it is in a
107985 ** suitable state to query for the column names and types to be used
107986 ** by the new table.
107987 **
107988 ** A shared-cache write-lock is not required to write to the new table,
107989 ** as a schema-lock must have already been obtained to create it. Since
107990 ** a schema-lock excludes all other database users, the write-lock would
107991 ** be redundant.
107992 */
107993 if( pSelect ){
107994 SelectDest dest; /* Where the SELECT should store results */
107995 int regYield; /* Register holding co-routine entry-point */
107996 int addrTop; /* Top of the co-routine */
107997 int regRec; /* A record to be insert into the new table */
107998 int regRowid; /* Rowid of the next row to insert */
107999 int addrInsLoop; /* Top of the loop for inserting rows */
108000 Table *pSelTab; /* A table that describes the SELECT results */
108001
108002 regYield = ++pParse->nMem;
108003 regRec = ++pParse->nMem;
108004 regRowid = ++pParse->nMem;
108005 assert(pParse->nTab==1);
108006 sqlite3MayAbort(pParse);
108007 sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
108008 sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
108009 pParse->nTab = 2;
108010 addrTop = sqlite3VdbeCurrentAddr(v) + 1;
108011 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
108012 if( pParse->nErr ) return;
108013 pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
108014 if( pSelTab==0 ) return;
108015 assert( p->aCol==0 );
108016 p->nCol = pSelTab->nCol;
108017 p->aCol = pSelTab->aCol;
108018 pSelTab->nCol = 0;
108019 pSelTab->aCol = 0;
108020 sqlite3DeleteTable(db, pSelTab);
108021 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
108022 sqlite3Select(pParse, pSelect, &dest);
108023 if( pParse->nErr ) return;
108024 sqlite3VdbeEndCoroutine(v, regYield);
108025 sqlite3VdbeJumpHere(v, addrTop - 1);
108026 addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
108027 VdbeCoverage(v);
108028 sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec);
108029 sqlite3TableAffinity(v, p, 0);
108030 sqlite3VdbeAddOp2(v, OP_NewRowid, 1, regRowid);
108031 sqlite3VdbeAddOp3(v, OP_Insert, 1, regRec, regRowid);
108032 sqlite3VdbeGoto(v, addrInsLoop);
108033 sqlite3VdbeJumpHere(v, addrInsLoop);
108034 sqlite3VdbeAddOp1(v, OP_Close, 1);
108035 }
108036
108037 /* Compute the complete text of the CREATE statement */
108038 if( pSelect ){
108039 zStmt = createTableStmt(db, p);
108040 }else{
108041 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
108042 n = (int)(pEnd2->z - pParse->sNameToken.z);
108043 if( pEnd2->z[0]!=';' ) n += pEnd2->n;
108044 zStmt = sqlite3MPrintf(db,
108045 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
108046 );
108047 }
108048
108049 /* A slot for the record has already been allocated in the
108050 ** SQLITE_MASTER table. We just need to update that slot with all
108051 ** the information we've collected.
108052 */
108053 sqlite3NestedParse(pParse,
108054 "UPDATE %Q.%s "
108055 "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q "
108056 "WHERE rowid=#%d",
108057 db->aDb[iDb].zDbSName, MASTER_NAME,
108058 zType,
108059 p->zName,
108060 p->zName,
108061 pParse->regRoot,
108062 zStmt,
108063 pParse->regRowid
108064 );
108065 sqlite3DbFree(db, zStmt);
108066 sqlite3ChangeCookie(pParse, iDb);
108067
108068#ifndef SQLITE_OMIT_AUTOINCREMENT
108069 /* Check to see if we need to create an sqlite_sequence table for
108070 ** keeping track of autoincrement keys.
108071 */
108072 if( (p->tabFlags & TF_Autoincrement)!=0 ){
108073 Db *pDb = &db->aDb[iDb];
108074 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
108075 if( pDb->pSchema->pSeqTab==0 ){
108076 sqlite3NestedParse(pParse,
108077 "CREATE TABLE %Q.sqlite_sequence(name,seq)",
108078 pDb->zDbSName
108079 );
108080 }
108081 }
108082#endif
108083
108084 /* Reparse everything to update our internal data structures */
108085 sqlite3VdbeAddParseSchemaOp(v, iDb,
108086 sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName));
108087 }
108088
108089
108090 /* Add the table to the in-memory representation of the database.
108091 */
108092 if( db->init.busy ){
108093 Table *pOld;
108094 Schema *pSchema = p->pSchema;
108095 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
108096 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
108097 if( pOld ){
108098 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
108099 sqlite3OomFault(db);
108100 return;
108101 }
108102 pParse->pNewTable = 0;
108103 db->mDbFlags |= DBFLAG_SchemaChange;
108104
108105#ifndef SQLITE_OMIT_ALTERTABLE
108106 if( !p->pSelect ){
108107 const char *zName = (const char *)pParse->sNameToken.z;
108108 int nName;
108109 assert( !pSelect && pCons && pEnd );
108110 if( pCons->z==0 ){
108111 pCons = pEnd;
108112 }
108113 nName = (int)((const char *)pCons->z - zName);
108114 p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
108115 }
108116#endif
108117 }
108118}
108119
108120#ifndef SQLITE_OMIT_VIEW
108121/*
108122** The parser calls this routine in order to create a new VIEW
108123*/
108124SQLITE_PRIVATE void sqlite3CreateView(
108125 Parse *pParse, /* The parsing context */
108126 Token *pBegin, /* The CREATE token that begins the statement */
108127 Token *pName1, /* The token that holds the name of the view */
108128 Token *pName2, /* The token that holds the name of the view */
108129 ExprList *pCNames, /* Optional list of view column names */
108130 Select *pSelect, /* A SELECT statement that will become the new view */
108131 int isTemp, /* TRUE for a TEMPORARY view */
108132 int noErr /* Suppress error messages if VIEW already exists */
108133){
108134 Table *p;
108135 int n;
108136 const char *z;
108137 Token sEnd;
108138 DbFixer sFix;
108139 Token *pName = 0;
108140 int iDb;
108141 sqlite3 *db = pParse->db;
108142
108143 if( pParse->nVar>0 ){
108144 sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
108145 goto create_view_fail;
108146 }
108147 sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
108148 p = pParse->pNewTable;
108149 if( p==0 || pParse->nErr ) goto create_view_fail;
108150 sqlite3TwoPartName(pParse, pName1, pName2, &pName);
108151 iDb = sqlite3SchemaToIndex(db, p->pSchema);
108152 sqlite3FixInit(&sFix, pParse, iDb, "view", pName);
108153 if( sqlite3FixSelect(&sFix, pSelect) ) goto create_view_fail;
108154
108155 /* Make a copy of the entire SELECT statement that defines the view.
108156 ** This will force all the Expr.token.z values to be dynamically
108157 ** allocated rather than point to the input string - which means that
108158 ** they will persist after the current sqlite3_exec() call returns.
108159 */
108160 if( IN_RENAME_OBJECT ){
108161 p->pSelect = pSelect;
108162 pSelect = 0;
108163 }else{
108164 p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
108165 }
108166 p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE);
108167 if( db->mallocFailed ) goto create_view_fail;
108168
108169 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
108170 ** the end.
108171 */
108172 sEnd = pParse->sLastToken;
108173 assert( sEnd.z[0]!=0 || sEnd.n==0 );
108174 if( sEnd.z[0]!=';' ){
108175 sEnd.z += sEnd.n;
108176 }
108177 sEnd.n = 0;
108178 n = (int)(sEnd.z - pBegin->z);
108179 assert( n>0 );
108180 z = pBegin->z;
108181 while( sqlite3Isspace(z[n-1]) ){ n--; }
108182 sEnd.z = &z[n-1];
108183 sEnd.n = 1;
108184
108185 /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
108186 sqlite3EndTable(pParse, 0, &sEnd, 0, 0);
108187
108188create_view_fail:
108189 sqlite3SelectDelete(db, pSelect);
108190 if( IN_RENAME_OBJECT ){
108191 sqlite3RenameExprlistUnmap(pParse, pCNames);
108192 }
108193 sqlite3ExprListDelete(db, pCNames);
108194 return;
108195}
108196#endif /* SQLITE_OMIT_VIEW */
108197
108198#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
108199/*
108200** The Table structure pTable is really a VIEW. Fill in the names of
108201** the columns of the view in the pTable structure. Return the number
108202** of errors. If an error is seen leave an error message in pParse->zErrMsg.
108203*/
108204SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
108205 Table *pSelTab; /* A fake table from which we get the result set */
108206 Select *pSel; /* Copy of the SELECT that implements the view */
108207 int nErr = 0; /* Number of errors encountered */
108208 int n; /* Temporarily holds the number of cursors assigned */
108209 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
108210#ifndef SQLITE_OMIT_VIRTUALTABLE
108211 int rc;
108212#endif
108213#ifndef SQLITE_OMIT_AUTHORIZATION
108214 sqlite3_xauth xAuth; /* Saved xAuth pointer */
108215#endif
108216
108217 assert( pTable );
108218
108219#ifndef SQLITE_OMIT_VIRTUALTABLE
108220 db->nSchemaLock++;
108221 rc = sqlite3VtabCallConnect(pParse, pTable);
108222 db->nSchemaLock--;
108223 if( rc ){
108224 return 1;
108225 }
108226 if( IsVirtual(pTable) ) return 0;
108227#endif
108228
108229#ifndef SQLITE_OMIT_VIEW
108230 /* A positive nCol means the columns names for this view are
108231 ** already known.
108232 */
108233 if( pTable->nCol>0 ) return 0;
108234
108235 /* A negative nCol is a special marker meaning that we are currently
108236 ** trying to compute the column names. If we enter this routine with
108237 ** a negative nCol, it means two or more views form a loop, like this:
108238 **
108239 ** CREATE VIEW one AS SELECT * FROM two;
108240 ** CREATE VIEW two AS SELECT * FROM one;
108241 **
108242 ** Actually, the error above is now caught prior to reaching this point.
108243 ** But the following test is still important as it does come up
108244 ** in the following:
108245 **
108246 ** CREATE TABLE main.ex1(a);
108247 ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
108248 ** SELECT * FROM temp.ex1;
108249 */
108250 if( pTable->nCol<0 ){
108251 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
108252 return 1;
108253 }
108254 assert( pTable->nCol>=0 );
108255
108256 /* If we get this far, it means we need to compute the table names.
108257 ** Note that the call to sqlite3ResultSetOfSelect() will expand any
108258 ** "*" elements in the results set of the view and will assign cursors
108259 ** to the elements of the FROM clause. But we do not want these changes
108260 ** to be permanent. So the computation is done on a copy of the SELECT
108261 ** statement that defines the view.
108262 */
108263 assert( pTable->pSelect );
108264 pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
108265 if( pSel ){
108266#ifndef SQLITE_OMIT_ALTERTABLE
108267 u8 eParseMode = pParse->eParseMode;
108268 pParse->eParseMode = PARSE_MODE_NORMAL;
108269#endif
108270 n = pParse->nTab;
108271 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
108272 pTable->nCol = -1;
108273 db->lookaside.bDisable++;
108274#ifndef SQLITE_OMIT_AUTHORIZATION
108275 xAuth = db->xAuth;
108276 db->xAuth = 0;
108277 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
108278 db->xAuth = xAuth;
108279#else
108280 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
108281#endif
108282 pParse->nTab = n;
108283 if( pTable->pCheck ){
108284 /* CREATE VIEW name(arglist) AS ...
108285 ** The names of the columns in the table are taken from
108286 ** arglist which is stored in pTable->pCheck. The pCheck field
108287 ** normally holds CHECK constraints on an ordinary table, but for
108288 ** a VIEW it holds the list of column names.
108289 */
108290 sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
108291 &pTable->nCol, &pTable->aCol);
108292 if( db->mallocFailed==0
108293 && pParse->nErr==0
108294 && pTable->nCol==pSel->pEList->nExpr
108295 ){
108296 sqlite3SelectAddColumnTypeAndCollation(pParse, pTable, pSel);
108297 }
108298 }else if( pSelTab ){
108299 /* CREATE VIEW name AS... without an argument list. Construct
108300 ** the column names from the SELECT statement that defines the view.
108301 */
108302 assert( pTable->aCol==0 );
108303 pTable->nCol = pSelTab->nCol;
108304 pTable->aCol = pSelTab->aCol;
108305 pSelTab->nCol = 0;
108306 pSelTab->aCol = 0;
108307 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
108308 }else{
108309 pTable->nCol = 0;
108310 nErr++;
108311 }
108312 sqlite3DeleteTable(db, pSelTab);
108313 sqlite3SelectDelete(db, pSel);
108314 db->lookaside.bDisable--;
108315#ifndef SQLITE_OMIT_ALTERTABLE
108316 pParse->eParseMode = eParseMode;
108317#endif
108318 } else {
108319 nErr++;
108320 }
108321 pTable->pSchema->schemaFlags |= DB_UnresetViews;
108322 if( db->mallocFailed ){
108323 sqlite3DeleteColumnNames(db, pTable);
108324 pTable->aCol = 0;
108325 pTable->nCol = 0;
108326 }
108327#endif /* SQLITE_OMIT_VIEW */
108328 return nErr;
108329}
108330#endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
108331
108332#ifndef SQLITE_OMIT_VIEW
108333/*
108334** Clear the column names from every VIEW in database idx.
108335*/
108336static void sqliteViewResetAll(sqlite3 *db, int idx){
108337 HashElem *i;
108338 assert( sqlite3SchemaMutexHeld(db, idx, 0) );
108339 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
108340 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
108341 Table *pTab = sqliteHashData(i);
108342 if( pTab->pSelect ){
108343 sqlite3DeleteColumnNames(db, pTab);
108344 pTab->aCol = 0;
108345 pTab->nCol = 0;
108346 }
108347 }
108348 DbClearProperty(db, idx, DB_UnresetViews);
108349}
108350#else
108351# define sqliteViewResetAll(A,B)
108352#endif /* SQLITE_OMIT_VIEW */
108353
108354/*
108355** This function is called by the VDBE to adjust the internal schema
108356** used by SQLite when the btree layer moves a table root page. The
108357** root-page of a table or index in database iDb has changed from iFrom
108358** to iTo.
108359**
108360** Ticket #1728: The symbol table might still contain information
108361** on tables and/or indices that are the process of being deleted.
108362** If you are unlucky, one of those deleted indices or tables might
108363** have the same rootpage number as the real table or index that is
108364** being moved. So we cannot stop searching after the first match
108365** because the first match might be for one of the deleted indices
108366** or tables and not the table/index that is actually being moved.
108367** We must continue looping until all tables and indices with
108368** rootpage==iFrom have been converted to have a rootpage of iTo
108369** in order to be certain that we got the right one.
108370*/
108371#ifndef SQLITE_OMIT_AUTOVACUUM
108372SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iTo){
108373 HashElem *pElem;
108374 Hash *pHash;
108375 Db *pDb;
108376
108377 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
108378 pDb = &db->aDb[iDb];
108379 pHash = &pDb->pSchema->tblHash;
108380 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
108381 Table *pTab = sqliteHashData(pElem);
108382 if( pTab->tnum==iFrom ){
108383 pTab->tnum = iTo;
108384 }
108385 }
108386 pHash = &pDb->pSchema->idxHash;
108387 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
108388 Index *pIdx = sqliteHashData(pElem);
108389 if( pIdx->tnum==iFrom ){
108390 pIdx->tnum = iTo;
108391 }
108392 }
108393}
108394#endif
108395
108396/*
108397** Write code to erase the table with root-page iTable from database iDb.
108398** Also write code to modify the sqlite_master table and internal schema
108399** if a root-page of another table is moved by the btree-layer whilst
108400** erasing iTable (this can happen with an auto-vacuum database).
108401*/
108402static void destroyRootPage(Parse *pParse, int iTable, int iDb){
108403 Vdbe *v = sqlite3GetVdbe(pParse);
108404 int r1 = sqlite3GetTempReg(pParse);
108405 if( iTable<2 ) sqlite3ErrorMsg(pParse, "corrupt schema");
108406 sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
108407 sqlite3MayAbort(pParse);
108408#ifndef SQLITE_OMIT_AUTOVACUUM
108409 /* OP_Destroy stores an in integer r1. If this integer
108410 ** is non-zero, then it is the root page number of a table moved to
108411 ** location iTable. The following code modifies the sqlite_master table to
108412 ** reflect this.
108413 **
108414 ** The "#NNN" in the SQL is a special constant that means whatever value
108415 ** is in register NNN. See grammar rules associated with the TK_REGISTER
108416 ** token for additional information.
108417 */
108418 sqlite3NestedParse(pParse,
108419 "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d",
108420 pParse->db->aDb[iDb].zDbSName, MASTER_NAME, iTable, r1, r1);
108421#endif
108422 sqlite3ReleaseTempReg(pParse, r1);
108423}
108424
108425/*
108426** Write VDBE code to erase table pTab and all associated indices on disk.
108427** Code to update the sqlite_master tables and internal schema definitions
108428** in case a root-page belonging to another table is moved by the btree layer
108429** is also added (this can happen with an auto-vacuum database).
108430*/
108431static void destroyTable(Parse *pParse, Table *pTab){
108432 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
108433 ** is not defined), then it is important to call OP_Destroy on the
108434 ** table and index root-pages in order, starting with the numerically
108435 ** largest root-page number. This guarantees that none of the root-pages
108436 ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
108437 ** following were coded:
108438 **
108439 ** OP_Destroy 4 0
108440 ** ...
108441 ** OP_Destroy 5 0
108442 **
108443 ** and root page 5 happened to be the largest root-page number in the
108444 ** database, then root page 5 would be moved to page 4 by the
108445 ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
108446 ** a free-list page.
108447 */
108448 int iTab = pTab->tnum;
108449 int iDestroyed = 0;
108450
108451 while( 1 ){
108452 Index *pIdx;
108453 int iLargest = 0;
108454
108455 if( iDestroyed==0 || iTab<iDestroyed ){
108456 iLargest = iTab;
108457 }
108458 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
108459 int iIdx = pIdx->tnum;
108460 assert( pIdx->pSchema==pTab->pSchema );
108461 if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
108462 iLargest = iIdx;
108463 }
108464 }
108465 if( iLargest==0 ){
108466 return;
108467 }else{
108468 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
108469 assert( iDb>=0 && iDb<pParse->db->nDb );
108470 destroyRootPage(pParse, iLargest, iDb);
108471 iDestroyed = iLargest;
108472 }
108473 }
108474}
108475
108476/*
108477** Remove entries from the sqlite_statN tables (for N in (1,2,3))
108478** after a DROP INDEX or DROP TABLE command.
108479*/
108480static void sqlite3ClearStatTables(
108481 Parse *pParse, /* The parsing context */
108482 int iDb, /* The database number */
108483 const char *zType, /* "idx" or "tbl" */
108484 const char *zName /* Name of index or table */
108485){
108486 int i;
108487 const char *zDbName = pParse->db->aDb[iDb].zDbSName;
108488 for(i=1; i<=4; i++){
108489 char zTab[24];
108490 sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
108491 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
108492 sqlite3NestedParse(pParse,
108493 "DELETE FROM %Q.%s WHERE %s=%Q",
108494 zDbName, zTab, zType, zName
108495 );
108496 }
108497 }
108498}
108499
108500/*
108501** Generate code to drop a table.
108502*/
108503SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
108504 Vdbe *v;
108505 sqlite3 *db = pParse->db;
108506 Trigger *pTrigger;
108507 Db *pDb = &db->aDb[iDb];
108508
108509 v = sqlite3GetVdbe(pParse);
108510 assert( v!=0 );
108511 sqlite3BeginWriteOperation(pParse, 1, iDb);
108512
108513#ifndef SQLITE_OMIT_VIRTUALTABLE
108514 if( IsVirtual(pTab) ){
108515 sqlite3VdbeAddOp0(v, OP_VBegin);
108516 }
108517#endif
108518
108519 /* Drop all triggers associated with the table being dropped. Code
108520 ** is generated to remove entries from sqlite_master and/or
108521 ** sqlite_temp_master if required.
108522 */
108523 pTrigger = sqlite3TriggerList(pParse, pTab);
108524 while( pTrigger ){
108525 assert( pTrigger->pSchema==pTab->pSchema ||
108526 pTrigger->pSchema==db->aDb[1].pSchema );
108527 sqlite3DropTriggerPtr(pParse, pTrigger);
108528 pTrigger = pTrigger->pNext;
108529 }
108530
108531#ifndef SQLITE_OMIT_AUTOINCREMENT
108532 /* Remove any entries of the sqlite_sequence table associated with
108533 ** the table being dropped. This is done before the table is dropped
108534 ** at the btree level, in case the sqlite_sequence table needs to
108535 ** move as a result of the drop (can happen in auto-vacuum mode).
108536 */
108537 if( pTab->tabFlags & TF_Autoincrement ){
108538 sqlite3NestedParse(pParse,
108539 "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
108540 pDb->zDbSName, pTab->zName
108541 );
108542 }
108543#endif
108544
108545 /* Drop all SQLITE_MASTER table and index entries that refer to the
108546 ** table. The program name loops through the master table and deletes
108547 ** every row that refers to a table of the same name as the one being
108548 ** dropped. Triggers are handled separately because a trigger can be
108549 ** created in the temp database that refers to a table in another
108550 ** database.
108551 */
108552 sqlite3NestedParse(pParse,
108553 "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
108554 pDb->zDbSName, MASTER_NAME, pTab->zName);
108555 if( !isView && !IsVirtual(pTab) ){
108556 destroyTable(pParse, pTab);
108557 }
108558
108559 /* Remove the table entry from SQLite's internal schema and modify
108560 ** the schema cookie.
108561 */
108562 if( IsVirtual(pTab) ){
108563 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
108564 }
108565 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
108566 sqlite3ChangeCookie(pParse, iDb);
108567 sqliteViewResetAll(db, iDb);
108568}
108569
108570/*
108571** This routine is called to do the work of a DROP TABLE statement.
108572** pName is the name of the table to be dropped.
108573*/
108574SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
108575 Table *pTab;
108576 Vdbe *v;
108577 sqlite3 *db = pParse->db;
108578 int iDb;
108579
108580 if( db->mallocFailed ){
108581 goto exit_drop_table;
108582 }
108583 assert( pParse->nErr==0 );
108584 assert( pName->nSrc==1 );
108585 if( sqlite3ReadSchema(pParse) ) goto exit_drop_table;
108586 if( noErr ) db->suppressErr++;
108587 assert( isView==0 || isView==LOCATE_VIEW );
108588 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
108589 if( noErr ) db->suppressErr--;
108590
108591 if( pTab==0 ){
108592 if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
108593 goto exit_drop_table;
108594 }
108595 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
108596 assert( iDb>=0 && iDb<db->nDb );
108597
108598 /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
108599 ** it is initialized.
108600 */
108601 if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){
108602 goto exit_drop_table;
108603 }
108604#ifndef SQLITE_OMIT_AUTHORIZATION
108605 {
108606 int code;
108607 const char *zTab = SCHEMA_TABLE(iDb);
108608 const char *zDb = db->aDb[iDb].zDbSName;
108609 const char *zArg2 = 0;
108610 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
108611 goto exit_drop_table;
108612 }
108613 if( isView ){
108614 if( !OMIT_TEMPDB && iDb==1 ){
108615 code = SQLITE_DROP_TEMP_VIEW;
108616 }else{
108617 code = SQLITE_DROP_VIEW;
108618 }
108619#ifndef SQLITE_OMIT_VIRTUALTABLE
108620 }else if( IsVirtual(pTab) ){
108621 code = SQLITE_DROP_VTABLE;
108622 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
108623#endif
108624 }else{
108625 if( !OMIT_TEMPDB && iDb==1 ){
108626 code = SQLITE_DROP_TEMP_TABLE;
108627 }else{
108628 code = SQLITE_DROP_TABLE;
108629 }
108630 }
108631 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
108632 goto exit_drop_table;
108633 }
108634 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
108635 goto exit_drop_table;
108636 }
108637 }
108638#endif
108639 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
108640 && sqlite3StrNICmp(pTab->zName, "sqlite_stat", 11)!=0 ){
108641 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
108642 goto exit_drop_table;
108643 }
108644
108645#ifndef SQLITE_OMIT_VIEW
108646 /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
108647 ** on a table.
108648 */
108649 if( isView && pTab->pSelect==0 ){
108650 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
108651 goto exit_drop_table;
108652 }
108653 if( !isView && pTab->pSelect ){
108654 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
108655 goto exit_drop_table;
108656 }
108657#endif
108658
108659 /* Generate code to remove the table from the master table
108660 ** on disk.
108661 */
108662 v = sqlite3GetVdbe(pParse);
108663 if( v ){
108664 sqlite3BeginWriteOperation(pParse, 1, iDb);
108665 if( !isView ){
108666 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
108667 sqlite3FkDropTable(pParse, pName, pTab);
108668 }
108669 sqlite3CodeDropTable(pParse, pTab, iDb, isView);
108670 }
108671
108672exit_drop_table:
108673 sqlite3SrcListDelete(db, pName);
108674}
108675
108676/*
108677** This routine is called to create a new foreign key on the table
108678** currently under construction. pFromCol determines which columns
108679** in the current table point to the foreign key. If pFromCol==0 then
108680** connect the key to the last column inserted. pTo is the name of
108681** the table referred to (a.k.a the "parent" table). pToCol is a list
108682** of tables in the parent pTo table. flags contains all
108683** information about the conflict resolution algorithms specified
108684** in the ON DELETE, ON UPDATE and ON INSERT clauses.
108685**
108686** An FKey structure is created and added to the table currently
108687** under construction in the pParse->pNewTable field.
108688**
108689** The foreign key is set for IMMEDIATE processing. A subsequent call
108690** to sqlite3DeferForeignKey() might change this to DEFERRED.
108691*/
108692SQLITE_PRIVATE void sqlite3CreateForeignKey(
108693 Parse *pParse, /* Parsing context */
108694 ExprList *pFromCol, /* Columns in this table that point to other table */
108695 Token *pTo, /* Name of the other table */
108696 ExprList *pToCol, /* Columns in the other table */
108697 int flags /* Conflict resolution algorithms. */
108698){
108699 sqlite3 *db = pParse->db;
108700#ifndef SQLITE_OMIT_FOREIGN_KEY
108701 FKey *pFKey = 0;
108702 FKey *pNextTo;
108703 Table *p = pParse->pNewTable;
108704 int nByte;
108705 int i;
108706 int nCol;
108707 char *z;
108708
108709 assert( pTo!=0 );
108710 if( p==0 || IN_DECLARE_VTAB ) goto fk_end;
108711 if( pFromCol==0 ){
108712 int iCol = p->nCol-1;
108713 if( NEVER(iCol<0) ) goto fk_end;
108714 if( pToCol && pToCol->nExpr!=1 ){
108715 sqlite3ErrorMsg(pParse, "foreign key on %s"
108716 " should reference only one column of table %T",
108717 p->aCol[iCol].zName, pTo);
108718 goto fk_end;
108719 }
108720 nCol = 1;
108721 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
108722 sqlite3ErrorMsg(pParse,
108723 "number of columns in foreign key does not match the number of "
108724 "columns in the referenced table");
108725 goto fk_end;
108726 }else{
108727 nCol = pFromCol->nExpr;
108728 }
108729 nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
108730 if( pToCol ){
108731 for(i=0; i<pToCol->nExpr; i++){
108732 nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1;
108733 }
108734 }
108735 pFKey = sqlite3DbMallocZero(db, nByte );
108736 if( pFKey==0 ){
108737 goto fk_end;
108738 }
108739 pFKey->pFrom = p;
108740 pFKey->pNextFrom = p->pFKey;
108741 z = (char*)&pFKey->aCol[nCol];
108742 pFKey->zTo = z;
108743 if( IN_RENAME_OBJECT ){
108744 sqlite3RenameTokenMap(pParse, (void*)z, pTo);
108745 }
108746 memcpy(z, pTo->z, pTo->n);
108747 z[pTo->n] = 0;
108748 sqlite3Dequote(z);
108749 z += pTo->n+1;
108750 pFKey->nCol = nCol;
108751 if( pFromCol==0 ){
108752 pFKey->aCol[0].iFrom = p->nCol-1;
108753 }else{
108754 for(i=0; i<nCol; i++){
108755 int j;
108756 for(j=0; j<p->nCol; j++){
108757 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
108758 pFKey->aCol[i].iFrom = j;
108759 break;
108760 }
108761 }
108762 if( j>=p->nCol ){
108763 sqlite3ErrorMsg(pParse,
108764 "unknown column \"%s\" in foreign key definition",
108765 pFromCol->a[i].zName);
108766 goto fk_end;
108767 }
108768 if( IN_RENAME_OBJECT ){
108769 sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zName);
108770 }
108771 }
108772 }
108773 if( pToCol ){
108774 for(i=0; i<nCol; i++){
108775 int n = sqlite3Strlen30(pToCol->a[i].zName);
108776 pFKey->aCol[i].zCol = z;
108777 if( IN_RENAME_OBJECT ){
108778 sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zName);
108779 }
108780 memcpy(z, pToCol->a[i].zName, n);
108781 z[n] = 0;
108782 z += n+1;
108783 }
108784 }
108785 pFKey->isDeferred = 0;
108786 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */
108787 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */
108788
108789 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
108790 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
108791 pFKey->zTo, (void *)pFKey
108792 );
108793 if( pNextTo==pFKey ){
108794 sqlite3OomFault(db);
108795 goto fk_end;
108796 }
108797 if( pNextTo ){
108798 assert( pNextTo->pPrevTo==0 );
108799 pFKey->pNextTo = pNextTo;
108800 pNextTo->pPrevTo = pFKey;
108801 }
108802
108803 /* Link the foreign key to the table as the last step.
108804 */
108805 p->pFKey = pFKey;
108806 pFKey = 0;
108807
108808fk_end:
108809 sqlite3DbFree(db, pFKey);
108810#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
108811 sqlite3ExprListDelete(db, pFromCol);
108812 sqlite3ExprListDelete(db, pToCol);
108813}
108814
108815/*
108816** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
108817** clause is seen as part of a foreign key definition. The isDeferred
108818** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
108819** The behavior of the most recently created foreign key is adjusted
108820** accordingly.
108821*/
108822SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
108823#ifndef SQLITE_OMIT_FOREIGN_KEY
108824 Table *pTab;
108825 FKey *pFKey;
108826 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
108827 assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
108828 pFKey->isDeferred = (u8)isDeferred;
108829#endif
108830}
108831
108832/*
108833** Generate code that will erase and refill index *pIdx. This is
108834** used to initialize a newly created index or to recompute the
108835** content of an index in response to a REINDEX command.
108836**
108837** if memRootPage is not negative, it means that the index is newly
108838** created. The register specified by memRootPage contains the
108839** root page number of the index. If memRootPage is negative, then
108840** the index already exists and must be cleared before being refilled and
108841** the root page number of the index is taken from pIndex->tnum.
108842*/
108843static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
108844 Table *pTab = pIndex->pTable; /* The table that is indexed */
108845 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
108846 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
108847 int iSorter; /* Cursor opened by OpenSorter (if in use) */
108848 int addr1; /* Address of top of loop */
108849 int addr2; /* Address to jump to for next iteration */
108850 int tnum; /* Root page of index */
108851 int iPartIdxLabel; /* Jump to this label to skip a row */
108852 Vdbe *v; /* Generate code into this virtual machine */
108853 KeyInfo *pKey; /* KeyInfo for index */
108854 int regRecord; /* Register holding assembled index record */
108855 sqlite3 *db = pParse->db; /* The database connection */
108856 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
108857
108858#ifndef SQLITE_OMIT_AUTHORIZATION
108859 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
108860 db->aDb[iDb].zDbSName ) ){
108861 return;
108862 }
108863#endif
108864
108865 /* Require a write-lock on the table to perform this operation */
108866 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
108867
108868 v = sqlite3GetVdbe(pParse);
108869 if( v==0 ) return;
108870 if( memRootPage>=0 ){
108871 tnum = memRootPage;
108872 }else{
108873 tnum = pIndex->tnum;
108874 }
108875 pKey = sqlite3KeyInfoOfIndex(pParse, pIndex);
108876 assert( pKey!=0 || db->mallocFailed || pParse->nErr );
108877
108878 /* Open the sorter cursor if we are to use one. */
108879 iSorter = pParse->nTab++;
108880 sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*)
108881 sqlite3KeyInfoRef(pKey), P4_KEYINFO);
108882
108883 /* Open the table. Loop through all rows of the table, inserting index
108884 ** records into the sorter. */
108885 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
108886 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v);
108887 regRecord = sqlite3GetTempReg(pParse);
108888 sqlite3MultiWrite(pParse);
108889
108890 sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
108891 sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
108892 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
108893 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v);
108894 sqlite3VdbeJumpHere(v, addr1);
108895 if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
108896 sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb,
108897 (char *)pKey, P4_KEYINFO);
108898 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
108899
108900 addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v);
108901 if( IsUniqueIndex(pIndex) ){
108902 int j2 = sqlite3VdbeGoto(v, 1);
108903 addr2 = sqlite3VdbeCurrentAddr(v);
108904 sqlite3VdbeVerifyAbortable(v, OE_Abort);
108905 sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
108906 pIndex->nKeyCol); VdbeCoverage(v);
108907 sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
108908 sqlite3VdbeJumpHere(v, j2);
108909 }else{
108910 addr2 = sqlite3VdbeCurrentAddr(v);
108911 }
108912 sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx);
108913 sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx);
108914 sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord);
108915 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
108916 sqlite3ReleaseTempReg(pParse, regRecord);
108917 sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v);
108918 sqlite3VdbeJumpHere(v, addr1);
108919
108920 sqlite3VdbeAddOp1(v, OP_Close, iTab);
108921 sqlite3VdbeAddOp1(v, OP_Close, iIdx);
108922 sqlite3VdbeAddOp1(v, OP_Close, iSorter);
108923}
108924
108925/*
108926** Allocate heap space to hold an Index object with nCol columns.
108927**
108928** Increase the allocation size to provide an extra nExtra bytes
108929** of 8-byte aligned space after the Index object and return a
108930** pointer to this extra space in *ppExtra.
108931*/
108932SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(
108933 sqlite3 *db, /* Database connection */
108934 i16 nCol, /* Total number of columns in the index */
108935 int nExtra, /* Number of bytes of extra space to alloc */
108936 char **ppExtra /* Pointer to the "extra" space */
108937){
108938 Index *p; /* Allocated index object */
108939 int nByte; /* Bytes of space for Index object + arrays */
108940
108941 nByte = ROUND8(sizeof(Index)) + /* Index structure */
108942 ROUND8(sizeof(char*)*nCol) + /* Index.azColl */
108943 ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */
108944 sizeof(i16)*nCol + /* Index.aiColumn */
108945 sizeof(u8)*nCol); /* Index.aSortOrder */
108946 p = sqlite3DbMallocZero(db, nByte + nExtra);
108947 if( p ){
108948 char *pExtra = ((char*)p)+ROUND8(sizeof(Index));
108949 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
108950 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
108951 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
108952 p->aSortOrder = (u8*)pExtra;
108953 p->nColumn = nCol;
108954 p->nKeyCol = nCol - 1;
108955 *ppExtra = ((char*)p) + nByte;
108956 }
108957 return p;
108958}
108959
108960/*
108961** Create a new index for an SQL table. pName1.pName2 is the name of the index
108962** and pTblList is the name of the table that is to be indexed. Both will
108963** be NULL for a primary key or an index that is created to satisfy a
108964** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
108965** as the table to be indexed. pParse->pNewTable is a table that is
108966** currently being constructed by a CREATE TABLE statement.
108967**
108968** pList is a list of columns to be indexed. pList will be NULL if this
108969** is a primary key or unique-constraint on the most recent column added
108970** to the table currently under construction.
108971*/
108972SQLITE_PRIVATE void sqlite3CreateIndex(
108973 Parse *pParse, /* All information about this parse */
108974 Token *pName1, /* First part of index name. May be NULL */
108975 Token *pName2, /* Second part of index name. May be NULL */
108976 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
108977 ExprList *pList, /* A list of columns to be indexed */
108978 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
108979 Token *pStart, /* The CREATE token that begins this statement */
108980 Expr *pPIWhere, /* WHERE clause for partial indices */
108981 int sortOrder, /* Sort order of primary key when pList==NULL */
108982 int ifNotExist, /* Omit error if index already exists */
108983 u8 idxType /* The index type */
108984){
108985 Table *pTab = 0; /* Table to be indexed */
108986 Index *pIndex = 0; /* The index to be created */
108987 char *zName = 0; /* Name of the index */
108988 int nName; /* Number of characters in zName */
108989 int i, j;
108990 DbFixer sFix; /* For assigning database names to pTable */
108991 int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */
108992 sqlite3 *db = pParse->db;
108993 Db *pDb; /* The specific table containing the indexed database */
108994 int iDb; /* Index of the database that is being written */
108995 Token *pName = 0; /* Unqualified name of the index to create */
108996 struct ExprList_item *pListItem; /* For looping over pList */
108997 int nExtra = 0; /* Space allocated for zExtra[] */
108998 int nExtraCol; /* Number of extra columns needed */
108999 char *zExtra = 0; /* Extra space after the Index object */
109000 Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */
109001
109002 if( db->mallocFailed || pParse->nErr>0 ){
109003 goto exit_create_index;
109004 }
109005 if( IN_DECLARE_VTAB && idxType!=SQLITE_IDXTYPE_PRIMARYKEY ){
109006 goto exit_create_index;
109007 }
109008 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
109009 goto exit_create_index;
109010 }
109011
109012 /*
109013 ** Find the table that is to be indexed. Return early if not found.
109014 */
109015 if( pTblName!=0 ){
109016
109017 /* Use the two-part index name to determine the database
109018 ** to search for the table. 'Fix' the table name to this db
109019 ** before looking up the table.
109020 */
109021 assert( pName1 && pName2 );
109022 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
109023 if( iDb<0 ) goto exit_create_index;
109024 assert( pName && pName->z );
109025
109026#ifndef SQLITE_OMIT_TEMPDB
109027 /* If the index name was unqualified, check if the table
109028 ** is a temp table. If so, set the database to 1. Do not do this
109029 ** if initialising a database schema.
109030 */
109031 if( !db->init.busy ){
109032 pTab = sqlite3SrcListLookup(pParse, pTblName);
109033 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
109034 iDb = 1;
109035 }
109036 }
109037#endif
109038
109039 sqlite3FixInit(&sFix, pParse, iDb, "index", pName);
109040 if( sqlite3FixSrcList(&sFix, pTblName) ){
109041 /* Because the parser constructs pTblName from a single identifier,
109042 ** sqlite3FixSrcList can never fail. */
109043 assert(0);
109044 }
109045 pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
109046 assert( db->mallocFailed==0 || pTab==0 );
109047 if( pTab==0 ) goto exit_create_index;
109048 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
109049 sqlite3ErrorMsg(pParse,
109050 "cannot create a TEMP index on non-TEMP table \"%s\"",
109051 pTab->zName);
109052 goto exit_create_index;
109053 }
109054 if( !HasRowid(pTab) ) pPk = sqlite3PrimaryKeyIndex(pTab);
109055 }else{
109056 assert( pName==0 );
109057 assert( pStart==0 );
109058 pTab = pParse->pNewTable;
109059 if( !pTab ) goto exit_create_index;
109060 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
109061 }
109062 pDb = &db->aDb[iDb];
109063
109064 assert( pTab!=0 );
109065 assert( pParse->nErr==0 );
109066 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
109067 && db->init.busy==0
109068#if SQLITE_USER_AUTHENTICATION
109069 && sqlite3UserAuthTable(pTab->zName)==0
109070#endif
109071#ifdef SQLITE_ALLOW_SQLITE_MASTER_INDEX
109072 && sqlite3StrICmp(&pTab->zName[7],"master")!=0
109073#endif
109074 && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0
109075 ){
109076 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
109077 goto exit_create_index;
109078 }
109079#ifndef SQLITE_OMIT_VIEW
109080 if( pTab->pSelect ){
109081 sqlite3ErrorMsg(pParse, "views may not be indexed");
109082 goto exit_create_index;
109083 }
109084#endif
109085#ifndef SQLITE_OMIT_VIRTUALTABLE
109086 if( IsVirtual(pTab) ){
109087 sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
109088 goto exit_create_index;
109089 }
109090#endif
109091
109092 /*
109093 ** Find the name of the index. Make sure there is not already another
109094 ** index or table with the same name.
109095 **
109096 ** Exception: If we are reading the names of permanent indices from the
109097 ** sqlite_master table (because some other process changed the schema) and
109098 ** one of the index names collides with the name of a temporary table or
109099 ** index, then we will continue to process this index.
109100 **
109101 ** If pName==0 it means that we are
109102 ** dealing with a primary key or UNIQUE constraint. We have to invent our
109103 ** own name.
109104 */
109105 if( pName ){
109106 zName = sqlite3NameFromToken(db, pName);
109107 if( zName==0 ) goto exit_create_index;
109108 assert( pName->z!=0 );
109109 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
109110 goto exit_create_index;
109111 }
109112 if( !IN_RENAME_OBJECT ){
109113 if( !db->init.busy ){
109114 if( sqlite3FindTable(db, zName, 0)!=0 ){
109115 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
109116 goto exit_create_index;
109117 }
109118 }
109119 if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
109120 if( !ifNotExist ){
109121 sqlite3ErrorMsg(pParse, "index %s already exists", zName);
109122 }else{
109123 assert( !db->init.busy );
109124 sqlite3CodeVerifySchema(pParse, iDb);
109125 }
109126 goto exit_create_index;
109127 }
109128 }
109129 }else{
109130 int n;
109131 Index *pLoop;
109132 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
109133 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
109134 if( zName==0 ){
109135 goto exit_create_index;
109136 }
109137
109138 /* Automatic index names generated from within sqlite3_declare_vtab()
109139 ** must have names that are distinct from normal automatic index names.
109140 ** The following statement converts "sqlite3_autoindex..." into
109141 ** "sqlite3_butoindex..." in order to make the names distinct.
109142 ** The "vtab_err.test" test demonstrates the need of this statement. */
109143 if( IN_SPECIAL_PARSE ) zName[7]++;
109144 }
109145
109146 /* Check for authorization to create an index.
109147 */
109148#ifndef SQLITE_OMIT_AUTHORIZATION
109149 if( !IN_RENAME_OBJECT ){
109150 const char *zDb = pDb->zDbSName;
109151 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
109152 goto exit_create_index;
109153 }
109154 i = SQLITE_CREATE_INDEX;
109155 if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
109156 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
109157 goto exit_create_index;
109158 }
109159 }
109160#endif
109161
109162 /* If pList==0, it means this routine was called to make a primary
109163 ** key out of the last column added to the table under construction.
109164 ** So create a fake list to simulate this.
109165 */
109166 if( pList==0 ){
109167 Token prevCol;
109168 Column *pCol = &pTab->aCol[pTab->nCol-1];
109169 pCol->colFlags |= COLFLAG_UNIQUE;
109170 sqlite3TokenInit(&prevCol, pCol->zName);
109171 pList = sqlite3ExprListAppend(pParse, 0,
109172 sqlite3ExprAlloc(db, TK_ID, &prevCol, 0));
109173 if( pList==0 ) goto exit_create_index;
109174 assert( pList->nExpr==1 );
109175 sqlite3ExprListSetSortOrder(pList, sortOrder);
109176 }else{
109177 sqlite3ExprListCheckLength(pParse, pList, "index");
109178 }
109179
109180 /* Figure out how many bytes of space are required to store explicitly
109181 ** specified collation sequence names.
109182 */
109183 for(i=0; i<pList->nExpr; i++){
109184 Expr *pExpr = pList->a[i].pExpr;
109185 assert( pExpr!=0 );
109186 if( pExpr->op==TK_COLLATE ){
109187 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
109188 }
109189 }
109190
109191 /*
109192 ** Allocate the index structure.
109193 */
109194 nName = sqlite3Strlen30(zName);
109195 nExtraCol = pPk ? pPk->nKeyCol : 1;
109196 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
109197 nName + nExtra + 1, &zExtra);
109198 if( db->mallocFailed ){
109199 goto exit_create_index;
109200 }
109201 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
109202 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
109203 pIndex->zName = zExtra;
109204 zExtra += nName + 1;
109205 memcpy(pIndex->zName, zName, nName+1);
109206 pIndex->pTable = pTab;
109207 pIndex->onError = (u8)onError;
109208 pIndex->uniqNotNull = onError!=OE_None;
109209 pIndex->idxType = idxType;
109210 pIndex->pSchema = db->aDb[iDb].pSchema;
109211 pIndex->nKeyCol = pList->nExpr;
109212 if( pPIWhere ){
109213 sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0);
109214 pIndex->pPartIdxWhere = pPIWhere;
109215 pPIWhere = 0;
109216 }
109217 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
109218
109219 /* Check to see if we should honor DESC requests on index columns
109220 */
109221 if( pDb->pSchema->file_format>=4 ){
109222 sortOrderMask = -1; /* Honor DESC */
109223 }else{
109224 sortOrderMask = 0; /* Ignore DESC */
109225 }
109226
109227 /* Analyze the list of expressions that form the terms of the index and
109228 ** report any errors. In the common case where the expression is exactly
109229 ** a table column, store that column in aiColumn[]. For general expressions,
109230 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
109231 **
109232 ** TODO: Issue a warning if two or more columns of the index are identical.
109233 ** TODO: Issue a warning if the table primary key is used as part of the
109234 ** index key.
109235 */
109236 pListItem = pList->a;
109237 if( IN_RENAME_OBJECT ){
109238 pIndex->aColExpr = pList;
109239 pList = 0;
109240 }
109241 for(i=0; i<pIndex->nKeyCol; i++, pListItem++){
109242 Expr *pCExpr; /* The i-th index expression */
109243 int requestedSortOrder; /* ASC or DESC on the i-th expression */
109244 const char *zColl; /* Collation sequence name */
109245
109246 sqlite3StringToId(pListItem->pExpr);
109247 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0);
109248 if( pParse->nErr ) goto exit_create_index;
109249 pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr);
109250 if( pCExpr->op!=TK_COLUMN ){
109251 if( pTab==pParse->pNewTable ){
109252 sqlite3ErrorMsg(pParse, "expressions prohibited in PRIMARY KEY and "
109253 "UNIQUE constraints");
109254 goto exit_create_index;
109255 }
109256 if( pIndex->aColExpr==0 ){
109257 pIndex->aColExpr = pList;
109258 pList = 0;
109259 }
109260 j = XN_EXPR;
109261 pIndex->aiColumn[i] = XN_EXPR;
109262 pIndex->uniqNotNull = 0;
109263 }else{
109264 j = pCExpr->iColumn;
109265 assert( j<=0x7fff );
109266 if( j<0 ){
109267 j = pTab->iPKey;
109268 }else if( pTab->aCol[j].notNull==0 ){
109269 pIndex->uniqNotNull = 0;
109270 }
109271 pIndex->aiColumn[i] = (i16)j;
109272 }
109273 zColl = 0;
109274 if( pListItem->pExpr->op==TK_COLLATE ){
109275 int nColl;
109276 zColl = pListItem->pExpr->u.zToken;
109277 nColl = sqlite3Strlen30(zColl) + 1;
109278 assert( nExtra>=nColl );
109279 memcpy(zExtra, zColl, nColl);
109280 zColl = zExtra;
109281 zExtra += nColl;
109282 nExtra -= nColl;
109283 }else if( j>=0 ){
109284 zColl = pTab->aCol[j].zColl;
109285 }
109286 if( !zColl ) zColl = sqlite3StrBINARY;
109287 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
109288 goto exit_create_index;
109289 }
109290 pIndex->azColl[i] = zColl;
109291 requestedSortOrder = pListItem->sortOrder & sortOrderMask;
109292 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
109293 }
109294
109295 /* Append the table key to the end of the index. For WITHOUT ROWID
109296 ** tables (when pPk!=0) this will be the declared PRIMARY KEY. For
109297 ** normal tables (when pPk==0) this will be the rowid.
109298 */
109299 if( pPk ){
109300 for(j=0; j<pPk->nKeyCol; j++){
109301 int x = pPk->aiColumn[j];
109302 assert( x>=0 );
109303 if( hasColumn(pIndex->aiColumn, pIndex->nKeyCol, x) ){
109304 pIndex->nColumn--;
109305 }else{
109306 pIndex->aiColumn[i] = x;
109307 pIndex->azColl[i] = pPk->azColl[j];
109308 pIndex->aSortOrder[i] = pPk->aSortOrder[j];
109309 i++;
109310 }
109311 }
109312 assert( i==pIndex->nColumn );
109313 }else{
109314 pIndex->aiColumn[i] = XN_ROWID;
109315 pIndex->azColl[i] = sqlite3StrBINARY;
109316 }
109317 sqlite3DefaultRowEst(pIndex);
109318 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
109319
109320 /* If this index contains every column of its table, then mark
109321 ** it as a covering index */
109322 assert( HasRowid(pTab)
109323 || pTab->iPKey<0 || sqlite3ColumnOfIndex(pIndex, pTab->iPKey)>=0 );
109324 recomputeColumnsNotIndexed(pIndex);
109325 if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
109326 pIndex->isCovering = 1;
109327 for(j=0; j<pTab->nCol; j++){
109328 if( j==pTab->iPKey ) continue;
109329 if( sqlite3ColumnOfIndex(pIndex,j)>=0 ) continue;
109330 pIndex->isCovering = 0;
109331 break;
109332 }
109333 }
109334
109335 if( pTab==pParse->pNewTable ){
109336 /* This routine has been called to create an automatic index as a
109337 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
109338 ** a PRIMARY KEY or UNIQUE clause following the column definitions.
109339 ** i.e. one of:
109340 **
109341 ** CREATE TABLE t(x PRIMARY KEY, y);
109342 ** CREATE TABLE t(x, y, UNIQUE(x, y));
109343 **
109344 ** Either way, check to see if the table already has such an index. If
109345 ** so, don't bother creating this one. This only applies to
109346 ** automatically created indices. Users can do as they wish with
109347 ** explicit indices.
109348 **
109349 ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
109350 ** (and thus suppressing the second one) even if they have different
109351 ** sort orders.
109352 **
109353 ** If there are different collating sequences or if the columns of
109354 ** the constraint occur in different orders, then the constraints are
109355 ** considered distinct and both result in separate indices.
109356 */
109357 Index *pIdx;
109358 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
109359 int k;
109360 assert( IsUniqueIndex(pIdx) );
109361 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
109362 assert( IsUniqueIndex(pIndex) );
109363
109364 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
109365 for(k=0; k<pIdx->nKeyCol; k++){
109366 const char *z1;
109367 const char *z2;
109368 assert( pIdx->aiColumn[k]>=0 );
109369 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
109370 z1 = pIdx->azColl[k];
109371 z2 = pIndex->azColl[k];
109372 if( sqlite3StrICmp(z1, z2) ) break;
109373 }
109374 if( k==pIdx->nKeyCol ){
109375 if( pIdx->onError!=pIndex->onError ){
109376 /* This constraint creates the same index as a previous
109377 ** constraint specified somewhere in the CREATE TABLE statement.
109378 ** However the ON CONFLICT clauses are different. If both this
109379 ** constraint and the previous equivalent constraint have explicit
109380 ** ON CONFLICT clauses this is an error. Otherwise, use the
109381 ** explicitly specified behavior for the index.
109382 */
109383 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
109384 sqlite3ErrorMsg(pParse,
109385 "conflicting ON CONFLICT clauses specified", 0);
109386 }
109387 if( pIdx->onError==OE_Default ){
109388 pIdx->onError = pIndex->onError;
109389 }
109390 }
109391 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
109392 goto exit_create_index;
109393 }
109394 }
109395 }
109396
109397 if( !IN_RENAME_OBJECT ){
109398
109399 /* Link the new Index structure to its table and to the other
109400 ** in-memory database structures.
109401 */
109402 assert( pParse->nErr==0 );
109403 if( db->init.busy ){
109404 Index *p;
109405 assert( !IN_SPECIAL_PARSE );
109406 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
109407 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
109408 pIndex->zName, pIndex);
109409 if( p ){
109410 assert( p==pIndex ); /* Malloc must have failed */
109411 sqlite3OomFault(db);
109412 goto exit_create_index;
109413 }
109414 db->mDbFlags |= DBFLAG_SchemaChange;
109415 if( pTblName!=0 ){
109416 pIndex->tnum = db->init.newTnum;
109417 }
109418 }
109419
109420 /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the
109421 ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
109422 ** emit code to allocate the index rootpage on disk and make an entry for
109423 ** the index in the sqlite_master table and populate the index with
109424 ** content. But, do not do this if we are simply reading the sqlite_master
109425 ** table to parse the schema, or if this index is the PRIMARY KEY index
109426 ** of a WITHOUT ROWID table.
109427 **
109428 ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY
109429 ** or UNIQUE index in a CREATE TABLE statement. Since the table
109430 ** has just been created, it contains no data and the index initialization
109431 ** step can be skipped.
109432 */
109433 else if( HasRowid(pTab) || pTblName!=0 ){
109434 Vdbe *v;
109435 char *zStmt;
109436 int iMem = ++pParse->nMem;
109437
109438 v = sqlite3GetVdbe(pParse);
109439 if( v==0 ) goto exit_create_index;
109440
109441 sqlite3BeginWriteOperation(pParse, 1, iDb);
109442
109443 /* Create the rootpage for the index using CreateIndex. But before
109444 ** doing so, code a Noop instruction and store its address in
109445 ** Index.tnum. This is required in case this index is actually a
109446 ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In
109447 ** that case the convertToWithoutRowidTable() routine will replace
109448 ** the Noop with a Goto to jump over the VDBE code generated below. */
109449 pIndex->tnum = sqlite3VdbeAddOp0(v, OP_Noop);
109450 sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, iMem, BTREE_BLOBKEY);
109451
109452 /* Gather the complete text of the CREATE INDEX statement into
109453 ** the zStmt variable
109454 */
109455 if( pStart ){
109456 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
109457 if( pName->z[n-1]==';' ) n--;
109458 /* A named index with an explicit CREATE INDEX statement */
109459 zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
109460 onError==OE_None ? "" : " UNIQUE", n, pName->z);
109461 }else{
109462 /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
109463 /* zStmt = sqlite3MPrintf(""); */
109464 zStmt = 0;
109465 }
109466
109467 /* Add an entry in sqlite_master for this index
109468 */
109469 sqlite3NestedParse(pParse,
109470 "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
109471 db->aDb[iDb].zDbSName, MASTER_NAME,
109472 pIndex->zName,
109473 pTab->zName,
109474 iMem,
109475 zStmt
109476 );
109477 sqlite3DbFree(db, zStmt);
109478
109479 /* Fill the index with data and reparse the schema. Code an OP_Expire
109480 ** to invalidate all pre-compiled statements.
109481 */
109482 if( pTblName ){
109483 sqlite3RefillIndex(pParse, pIndex, iMem);
109484 sqlite3ChangeCookie(pParse, iDb);
109485 sqlite3VdbeAddParseSchemaOp(v, iDb,
109486 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
109487 sqlite3VdbeAddOp2(v, OP_Expire, 0, 1);
109488 }
109489
109490 sqlite3VdbeJumpHere(v, pIndex->tnum);
109491 }
109492 }
109493
109494 /* When adding an index to the list of indices for a table, make
109495 ** sure all indices labeled OE_Replace come after all those labeled
109496 ** OE_Ignore. This is necessary for the correct constraint check
109497 ** processing (in sqlite3GenerateConstraintChecks()) as part of
109498 ** UPDATE and INSERT statements.
109499 */
109500 if( db->init.busy || pTblName==0 ){
109501 if( onError!=OE_Replace || pTab->pIndex==0
109502 || pTab->pIndex->onError==OE_Replace){
109503 pIndex->pNext = pTab->pIndex;
109504 pTab->pIndex = pIndex;
109505 }else{
109506 Index *pOther = pTab->pIndex;
109507 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
109508 pOther = pOther->pNext;
109509 }
109510 pIndex->pNext = pOther->pNext;
109511 pOther->pNext = pIndex;
109512 }
109513 pIndex = 0;
109514 }
109515 else if( IN_RENAME_OBJECT ){
109516 assert( pParse->pNewIndex==0 );
109517 pParse->pNewIndex = pIndex;
109518 pIndex = 0;
109519 }
109520
109521 /* Clean up before exiting */
109522exit_create_index:
109523 if( pIndex ) sqlite3FreeIndex(db, pIndex);
109524 sqlite3ExprDelete(db, pPIWhere);
109525 sqlite3ExprListDelete(db, pList);
109526 sqlite3SrcListDelete(db, pTblName);
109527 sqlite3DbFree(db, zName);
109528}
109529
109530/*
109531** Fill the Index.aiRowEst[] array with default information - information
109532** to be used when we have not run the ANALYZE command.
109533**
109534** aiRowEst[0] is supposed to contain the number of elements in the index.
109535** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the
109536** number of rows in the table that match any particular value of the
109537** first column of the index. aiRowEst[2] is an estimate of the number
109538** of rows that match any particular combination of the first 2 columns
109539** of the index. And so forth. It must always be the case that
109540*
109541** aiRowEst[N]<=aiRowEst[N-1]
109542** aiRowEst[N]>=1
109543**
109544** Apart from that, we have little to go on besides intuition as to
109545** how aiRowEst[] should be initialized. The numbers generated here
109546** are based on typical values found in actual indices.
109547*/
109548SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
109549 /* 10, 9, 8, 7, 6 */
109550 LogEst aVal[] = { 33, 32, 30, 28, 26 };
109551 LogEst *a = pIdx->aiRowLogEst;
109552 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
109553 int i;
109554
109555 /* Indexes with default row estimates should not have stat1 data */
109556 assert( !pIdx->hasStat1 );
109557
109558 /* Set the first entry (number of rows in the index) to the estimated
109559 ** number of rows in the table, or half the number of rows in the table
109560 ** for a partial index. But do not let the estimate drop below 10. */
109561 a[0] = pIdx->pTable->nRowLogEst;
109562 if( pIdx->pPartIdxWhere!=0 ) a[0] -= 10; assert( 10==sqlite3LogEst(2) );
109563 if( a[0]<33 ) a[0] = 33; assert( 33==sqlite3LogEst(10) );
109564
109565 /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is
109566 ** 6 and each subsequent value (if any) is 5. */
109567 memcpy(&a[1], aVal, nCopy*sizeof(LogEst));
109568 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
109569 a[i] = 23; assert( 23==sqlite3LogEst(5) );
109570 }
109571
109572 assert( 0==sqlite3LogEst(1) );
109573 if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
109574}
109575
109576/*
109577** This routine will drop an existing named index. This routine
109578** implements the DROP INDEX statement.
109579*/
109580SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
109581 Index *pIndex;
109582 Vdbe *v;
109583 sqlite3 *db = pParse->db;
109584 int iDb;
109585
109586 assert( pParse->nErr==0 ); /* Never called with prior errors */
109587 if( db->mallocFailed ){
109588 goto exit_drop_index;
109589 }
109590 assert( pName->nSrc==1 );
109591 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
109592 goto exit_drop_index;
109593 }
109594 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
109595 if( pIndex==0 ){
109596 if( !ifExists ){
109597 sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
109598 }else{
109599 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
109600 }
109601 pParse->checkSchema = 1;
109602 goto exit_drop_index;
109603 }
109604 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
109605 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
109606 "or PRIMARY KEY constraint cannot be dropped", 0);
109607 goto exit_drop_index;
109608 }
109609 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
109610#ifndef SQLITE_OMIT_AUTHORIZATION
109611 {
109612 int code = SQLITE_DROP_INDEX;
109613 Table *pTab = pIndex->pTable;
109614 const char *zDb = db->aDb[iDb].zDbSName;
109615 const char *zTab = SCHEMA_TABLE(iDb);
109616 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
109617 goto exit_drop_index;
109618 }
109619 if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX;
109620 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
109621 goto exit_drop_index;
109622 }
109623 }
109624#endif
109625
109626 /* Generate code to remove the index and from the master table */
109627 v = sqlite3GetVdbe(pParse);
109628 if( v ){
109629 sqlite3BeginWriteOperation(pParse, 1, iDb);
109630 sqlite3NestedParse(pParse,
109631 "DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
109632 db->aDb[iDb].zDbSName, MASTER_NAME, pIndex->zName
109633 );
109634 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
109635 sqlite3ChangeCookie(pParse, iDb);
109636 destroyRootPage(pParse, pIndex->tnum, iDb);
109637 sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
109638 }
109639
109640exit_drop_index:
109641 sqlite3SrcListDelete(db, pName);
109642}
109643
109644/*
109645** pArray is a pointer to an array of objects. Each object in the
109646** array is szEntry bytes in size. This routine uses sqlite3DbRealloc()
109647** to extend the array so that there is space for a new object at the end.
109648**
109649** When this function is called, *pnEntry contains the current size of
109650** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
109651** in total).
109652**
109653** If the realloc() is successful (i.e. if no OOM condition occurs), the
109654** space allocated for the new object is zeroed, *pnEntry updated to
109655** reflect the new size of the array and a pointer to the new allocation
109656** returned. *pIdx is set to the index of the new array entry in this case.
109657**
109658** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
109659** unchanged and a copy of pArray returned.
109660*/
109661SQLITE_PRIVATE void *sqlite3ArrayAllocate(
109662 sqlite3 *db, /* Connection to notify of malloc failures */
109663 void *pArray, /* Array of objects. Might be reallocated */
109664 int szEntry, /* Size of each object in the array */
109665 int *pnEntry, /* Number of objects currently in use */
109666 int *pIdx /* Write the index of a new slot here */
109667){
109668 char *z;
109669 int n = *pnEntry;
109670 if( (n & (n-1))==0 ){
109671 int sz = (n==0) ? 1 : 2*n;
109672 void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry);
109673 if( pNew==0 ){
109674 *pIdx = -1;
109675 return pArray;
109676 }
109677 pArray = pNew;
109678 }
109679 z = (char*)pArray;
109680 memset(&z[n * szEntry], 0, szEntry);
109681 *pIdx = n;
109682 ++*pnEntry;
109683 return pArray;
109684}
109685
109686/*
109687** Append a new element to the given IdList. Create a new IdList if
109688** need be.
109689**
109690** A new IdList is returned, or NULL if malloc() fails.
109691*/
109692SQLITE_PRIVATE IdList *sqlite3IdListAppend(Parse *pParse, IdList *pList, Token *pToken){
109693 sqlite3 *db = pParse->db;
109694 int i;
109695 if( pList==0 ){
109696 pList = sqlite3DbMallocZero(db, sizeof(IdList) );
109697 if( pList==0 ) return 0;
109698 }
109699 pList->a = sqlite3ArrayAllocate(
109700 db,
109701 pList->a,
109702 sizeof(pList->a[0]),
109703 &pList->nId,
109704 &i
109705 );
109706 if( i<0 ){
109707 sqlite3IdListDelete(db, pList);
109708 return 0;
109709 }
109710 pList->a[i].zName = sqlite3NameFromToken(db, pToken);
109711 if( IN_RENAME_OBJECT && pList->a[i].zName ){
109712 sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken);
109713 }
109714 return pList;
109715}
109716
109717/*
109718** Delete an IdList.
109719*/
109720SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
109721 int i;
109722 if( pList==0 ) return;
109723 for(i=0; i<pList->nId; i++){
109724 sqlite3DbFree(db, pList->a[i].zName);
109725 }
109726 sqlite3DbFree(db, pList->a);
109727 sqlite3DbFreeNN(db, pList);
109728}
109729
109730/*
109731** Return the index in pList of the identifier named zId. Return -1
109732** if not found.
109733*/
109734SQLITE_PRIVATE int sqlite3IdListIndex(IdList *pList, const char *zName){
109735 int i;
109736 if( pList==0 ) return -1;
109737 for(i=0; i<pList->nId; i++){
109738 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
109739 }
109740 return -1;
109741}
109742
109743/*
109744** Expand the space allocated for the given SrcList object by
109745** creating nExtra new slots beginning at iStart. iStart is zero based.
109746** New slots are zeroed.
109747**
109748** For example, suppose a SrcList initially contains two entries: A,B.
109749** To append 3 new entries onto the end, do this:
109750**
109751** sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
109752**
109753** After the call above it would contain: A, B, nil, nil, nil.
109754** If the iStart argument had been 1 instead of 2, then the result
109755** would have been: A, nil, nil, nil, B. To prepend the new slots,
109756** the iStart value would be 0. The result then would
109757** be: nil, nil, nil, A, B.
109758**
109759** If a memory allocation fails the SrcList is unchanged. The
109760** db->mallocFailed flag will be set to true.
109761*/
109762SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(
109763 sqlite3 *db, /* Database connection to notify of OOM errors */
109764 SrcList *pSrc, /* The SrcList to be enlarged */
109765 int nExtra, /* Number of new slots to add to pSrc->a[] */
109766 int iStart /* Index in pSrc->a[] of first new slot */
109767){
109768 int i;
109769
109770 /* Sanity checking on calling parameters */
109771 assert( iStart>=0 );
109772 assert( nExtra>=1 );
109773 assert( pSrc!=0 );
109774 assert( iStart<=pSrc->nSrc );
109775
109776 /* Allocate additional space if needed */
109777 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
109778 SrcList *pNew;
109779 int nAlloc = pSrc->nSrc*2+nExtra;
109780 int nGot;
109781 pNew = sqlite3DbRealloc(db, pSrc,
109782 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
109783 if( pNew==0 ){
109784 assert( db->mallocFailed );
109785 return pSrc;
109786 }
109787 pSrc = pNew;
109788 nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1;
109789 pSrc->nAlloc = nGot;
109790 }
109791
109792 /* Move existing slots that come after the newly inserted slots
109793 ** out of the way */
109794 for(i=pSrc->nSrc-1; i>=iStart; i--){
109795 pSrc->a[i+nExtra] = pSrc->a[i];
109796 }
109797 pSrc->nSrc += nExtra;
109798
109799 /* Zero the newly allocated slots */
109800 memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
109801 for(i=iStart; i<iStart+nExtra; i++){
109802 pSrc->a[i].iCursor = -1;
109803 }
109804
109805 /* Return a pointer to the enlarged SrcList */
109806 return pSrc;
109807}
109808
109809
109810/*
109811** Append a new table name to the given SrcList. Create a new SrcList if
109812** need be. A new entry is created in the SrcList even if pTable is NULL.
109813**
109814** A SrcList is returned, or NULL if there is an OOM error. The returned
109815** SrcList might be the same as the SrcList that was input or it might be
109816** a new one. If an OOM error does occurs, then the prior value of pList
109817** that is input to this routine is automatically freed.
109818**
109819** If pDatabase is not null, it means that the table has an optional
109820** database name prefix. Like this: "database.table". The pDatabase
109821** points to the table name and the pTable points to the database name.
109822** The SrcList.a[].zName field is filled with the table name which might
109823** come from pTable (if pDatabase is NULL) or from pDatabase.
109824** SrcList.a[].zDatabase is filled with the database name from pTable,
109825** or with NULL if no database is specified.
109826**
109827** In other words, if call like this:
109828**
109829** sqlite3SrcListAppend(D,A,B,0);
109830**
109831** Then B is a table name and the database name is unspecified. If called
109832** like this:
109833**
109834** sqlite3SrcListAppend(D,A,B,C);
109835**
109836** Then C is the table name and B is the database name. If C is defined
109837** then so is B. In other words, we never have a case where:
109838**
109839** sqlite3SrcListAppend(D,A,0,C);
109840**
109841** Both pTable and pDatabase are assumed to be quoted. They are dequoted
109842** before being added to the SrcList.
109843*/
109844SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(
109845 sqlite3 *db, /* Connection to notify of malloc failures */
109846 SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */
109847 Token *pTable, /* Table to append */
109848 Token *pDatabase /* Database of the table */
109849){
109850 struct SrcList_item *pItem;
109851 assert( pDatabase==0 || pTable!=0 ); /* Cannot have C without B */
109852 assert( db!=0 );
109853 if( pList==0 ){
109854 pList = sqlite3DbMallocRawNN(db, sizeof(SrcList) );
109855 if( pList==0 ) return 0;
109856 pList->nAlloc = 1;
109857 pList->nSrc = 1;
109858 memset(&pList->a[0], 0, sizeof(pList->a[0]));
109859 pList->a[0].iCursor = -1;
109860 }else{
109861 pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
109862 }
109863 if( db->mallocFailed ){
109864 sqlite3SrcListDelete(db, pList);
109865 return 0;
109866 }
109867 pItem = &pList->a[pList->nSrc-1];
109868 if( pDatabase && pDatabase->z==0 ){
109869 pDatabase = 0;
109870 }
109871 if( pDatabase ){
109872 pItem->zName = sqlite3NameFromToken(db, pDatabase);
109873 pItem->zDatabase = sqlite3NameFromToken(db, pTable);
109874 }else{
109875 pItem->zName = sqlite3NameFromToken(db, pTable);
109876 pItem->zDatabase = 0;
109877 }
109878 return pList;
109879}
109880
109881/*
109882** Assign VdbeCursor index numbers to all tables in a SrcList
109883*/
109884SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
109885 int i;
109886 struct SrcList_item *pItem;
109887 assert(pList || pParse->db->mallocFailed );
109888 if( pList ){
109889 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
109890 if( pItem->iCursor>=0 ) break;
109891 pItem->iCursor = pParse->nTab++;
109892 if( pItem->pSelect ){
109893 sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
109894 }
109895 }
109896 }
109897}
109898
109899/*
109900** Delete an entire SrcList including all its substructure.
109901*/
109902SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
109903 int i;
109904 struct SrcList_item *pItem;
109905 if( pList==0 ) return;
109906 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
109907 sqlite3DbFree(db, pItem->zDatabase);
109908 sqlite3DbFree(db, pItem->zName);
109909 sqlite3DbFree(db, pItem->zAlias);
109910 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
109911 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
109912 sqlite3DeleteTable(db, pItem->pTab);
109913 sqlite3SelectDelete(db, pItem->pSelect);
109914 sqlite3ExprDelete(db, pItem->pOn);
109915 sqlite3IdListDelete(db, pItem->pUsing);
109916 }
109917 sqlite3DbFreeNN(db, pList);
109918}
109919
109920/*
109921** This routine is called by the parser to add a new term to the
109922** end of a growing FROM clause. The "p" parameter is the part of
109923** the FROM clause that has already been constructed. "p" is NULL
109924** if this is the first term of the FROM clause. pTable and pDatabase
109925** are the name of the table and database named in the FROM clause term.
109926** pDatabase is NULL if the database name qualifier is missing - the
109927** usual case. If the term has an alias, then pAlias points to the
109928** alias token. If the term is a subquery, then pSubquery is the
109929** SELECT statement that the subquery encodes. The pTable and
109930** pDatabase parameters are NULL for subqueries. The pOn and pUsing
109931** parameters are the content of the ON and USING clauses.
109932**
109933** Return a new SrcList which encodes is the FROM with the new
109934** term added.
109935*/
109936SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(
109937 Parse *pParse, /* Parsing context */
109938 SrcList *p, /* The left part of the FROM clause already seen */
109939 Token *pTable, /* Name of the table to add to the FROM clause */
109940 Token *pDatabase, /* Name of the database containing pTable */
109941 Token *pAlias, /* The right-hand side of the AS subexpression */
109942 Select *pSubquery, /* A subquery used in place of a table name */
109943 Expr *pOn, /* The ON clause of a join */
109944 IdList *pUsing /* The USING clause of a join */
109945){
109946 struct SrcList_item *pItem;
109947 sqlite3 *db = pParse->db;
109948 if( !p && (pOn || pUsing) ){
109949 sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s",
109950 (pOn ? "ON" : "USING")
109951 );
109952 goto append_from_error;
109953 }
109954 p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
109955 if( p==0 ){
109956 goto append_from_error;
109957 }
109958 assert( p->nSrc>0 );
109959 pItem = &p->a[p->nSrc-1];
109960 assert( (pTable==0)==(pDatabase==0) );
109961 assert( pItem->zName==0 || pDatabase!=0 );
109962 if( IN_RENAME_OBJECT && pItem->zName ){
109963 Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable;
109964 sqlite3RenameTokenMap(pParse, pItem->zName, pToken);
109965 }
109966 assert( pAlias!=0 );
109967 if( pAlias->n ){
109968 pItem->zAlias = sqlite3NameFromToken(db, pAlias);
109969 }
109970 pItem->pSelect = pSubquery;
109971 pItem->pOn = pOn;
109972 pItem->pUsing = pUsing;
109973 return p;
109974
109975 append_from_error:
109976 assert( p==0 );
109977 sqlite3ExprDelete(db, pOn);
109978 sqlite3IdListDelete(db, pUsing);
109979 sqlite3SelectDelete(db, pSubquery);
109980 return 0;
109981}
109982
109983/*
109984** Add an INDEXED BY or NOT INDEXED clause to the most recently added
109985** element of the source-list passed as the second argument.
109986*/
109987SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
109988 assert( pIndexedBy!=0 );
109989 if( p && pIndexedBy->n>0 ){
109990 struct SrcList_item *pItem;
109991 assert( p->nSrc>0 );
109992 pItem = &p->a[p->nSrc-1];
109993 assert( pItem->fg.notIndexed==0 );
109994 assert( pItem->fg.isIndexedBy==0 );
109995 assert( pItem->fg.isTabFunc==0 );
109996 if( pIndexedBy->n==1 && !pIndexedBy->z ){
109997 /* A "NOT INDEXED" clause was supplied. See parse.y
109998 ** construct "indexed_opt" for details. */
109999 pItem->fg.notIndexed = 1;
110000 }else{
110001 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
110002 pItem->fg.isIndexedBy = 1;
110003 }
110004 }
110005}
110006
110007/*
110008** Add the list of function arguments to the SrcList entry for a
110009** table-valued-function.
110010*/
110011SQLITE_PRIVATE void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){
110012 if( p ){
110013 struct SrcList_item *pItem = &p->a[p->nSrc-1];
110014 assert( pItem->fg.notIndexed==0 );
110015 assert( pItem->fg.isIndexedBy==0 );
110016 assert( pItem->fg.isTabFunc==0 );
110017 pItem->u1.pFuncArg = pList;
110018 pItem->fg.isTabFunc = 1;
110019 }else{
110020 sqlite3ExprListDelete(pParse->db, pList);
110021 }
110022}
110023
110024/*
110025** When building up a FROM clause in the parser, the join operator
110026** is initially attached to the left operand. But the code generator
110027** expects the join operator to be on the right operand. This routine
110028** Shifts all join operators from left to right for an entire FROM
110029** clause.
110030**
110031** Example: Suppose the join is like this:
110032**
110033** A natural cross join B
110034**
110035** The operator is "natural cross join". The A and B operands are stored
110036** in p->a[0] and p->a[1], respectively. The parser initially stores the
110037** operator with A. This routine shifts that operator over to B.
110038*/
110039SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
110040 if( p ){
110041 int i;
110042 for(i=p->nSrc-1; i>0; i--){
110043 p->a[i].fg.jointype = p->a[i-1].fg.jointype;
110044 }
110045 p->a[0].fg.jointype = 0;
110046 }
110047}
110048
110049/*
110050** Generate VDBE code for a BEGIN statement.
110051*/
110052SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
110053 sqlite3 *db;
110054 Vdbe *v;
110055 int i;
110056
110057 assert( pParse!=0 );
110058 db = pParse->db;
110059 assert( db!=0 );
110060 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
110061 return;
110062 }
110063 v = sqlite3GetVdbe(pParse);
110064 if( !v ) return;
110065 if( type!=TK_DEFERRED ){
110066 for(i=0; i<db->nDb; i++){
110067 sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
110068 sqlite3VdbeUsesBtree(v, i);
110069 }
110070 }
110071 sqlite3VdbeAddOp0(v, OP_AutoCommit);
110072}
110073
110074/*
110075** Generate VDBE code for a COMMIT or ROLLBACK statement.
110076** Code for ROLLBACK is generated if eType==TK_ROLLBACK. Otherwise
110077** code is generated for a COMMIT.
110078*/
110079SQLITE_PRIVATE void sqlite3EndTransaction(Parse *pParse, int eType){
110080 Vdbe *v;
110081 int isRollback;
110082
110083 assert( pParse!=0 );
110084 assert( pParse->db!=0 );
110085 assert( eType==TK_COMMIT || eType==TK_END || eType==TK_ROLLBACK );
110086 isRollback = eType==TK_ROLLBACK;
110087 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION,
110088 isRollback ? "ROLLBACK" : "COMMIT", 0, 0) ){
110089 return;
110090 }
110091 v = sqlite3GetVdbe(pParse);
110092 if( v ){
110093 sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, isRollback);
110094 }
110095}
110096
110097/*
110098** This function is called by the parser when it parses a command to create,
110099** release or rollback an SQL savepoint.
110100*/
110101SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
110102 char *zName = sqlite3NameFromToken(pParse->db, pName);
110103 if( zName ){
110104 Vdbe *v = sqlite3GetVdbe(pParse);
110105#ifndef SQLITE_OMIT_AUTHORIZATION
110106 static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
110107 assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
110108#endif
110109 if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){
110110 sqlite3DbFree(pParse->db, zName);
110111 return;
110112 }
110113 sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC);
110114 }
110115}
110116
110117/*
110118** Make sure the TEMP database is open and available for use. Return
110119** the number of errors. Leave any error messages in the pParse structure.
110120*/
110121SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){
110122 sqlite3 *db = pParse->db;
110123 if( db->aDb[1].pBt==0 && !pParse->explain ){
110124 int rc;
110125 Btree *pBt;
110126 static const int flags =
110127 SQLITE_OPEN_READWRITE |
110128 SQLITE_OPEN_CREATE |
110129 SQLITE_OPEN_EXCLUSIVE |
110130 SQLITE_OPEN_DELETEONCLOSE |
110131 SQLITE_OPEN_TEMP_DB;
110132
110133 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
110134 if( rc!=SQLITE_OK ){
110135 sqlite3ErrorMsg(pParse, "unable to open a temporary database "
110136 "file for storing temporary tables");
110137 pParse->rc = rc;
110138 return 1;
110139 }
110140 db->aDb[1].pBt = pBt;
110141 assert( db->aDb[1].pSchema );
110142 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
110143 sqlite3OomFault(db);
110144 return 1;
110145 }
110146 }
110147 return 0;
110148}
110149
110150/*
110151** Record the fact that the schema cookie will need to be verified
110152** for database iDb. The code to actually verify the schema cookie
110153** will occur at the end of the top-level VDBE and will be generated
110154** later, by sqlite3FinishCoding().
110155*/
110156SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
110157 Parse *pToplevel = sqlite3ParseToplevel(pParse);
110158
110159 assert( iDb>=0 && iDb<pParse->db->nDb );
110160 assert( pParse->db->aDb[iDb].pBt!=0 || iDb==1 );
110161 assert( iDb<SQLITE_MAX_ATTACHED+2 );
110162 assert( sqlite3SchemaMutexHeld(pParse->db, iDb, 0) );
110163 if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
110164 DbMaskSet(pToplevel->cookieMask, iDb);
110165 if( !OMIT_TEMPDB && iDb==1 ){
110166 sqlite3OpenTempDatabase(pToplevel);
110167 }
110168 }
110169}
110170
110171/*
110172** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each
110173** attached database. Otherwise, invoke it for the database named zDb only.
110174*/
110175SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
110176 sqlite3 *db = pParse->db;
110177 int i;
110178 for(i=0; i<db->nDb; i++){
110179 Db *pDb = &db->aDb[i];
110180 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
110181 sqlite3CodeVerifySchema(pParse, i);
110182 }
110183 }
110184}
110185
110186/*
110187** Generate VDBE code that prepares for doing an operation that
110188** might change the database.
110189**
110190** This routine starts a new transaction if we are not already within
110191** a transaction. If we are already within a transaction, then a checkpoint
110192** is set if the setStatement parameter is true. A checkpoint should
110193** be set for operations that might fail (due to a constraint) part of
110194** the way through and which will need to undo some writes without having to
110195** rollback the whole transaction. For operations where all constraints
110196** can be checked before any changes are made to the database, it is never
110197** necessary to undo a write and the checkpoint should not be set.
110198*/
110199SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
110200 Parse *pToplevel = sqlite3ParseToplevel(pParse);
110201 sqlite3CodeVerifySchema(pParse, iDb);
110202 DbMaskSet(pToplevel->writeMask, iDb);
110203 pToplevel->isMultiWrite |= setStatement;
110204}
110205
110206/*
110207** Indicate that the statement currently under construction might write
110208** more than one entry (example: deleting one row then inserting another,
110209** inserting multiple rows in a table, or inserting a row and index entries.)
110210** If an abort occurs after some of these writes have completed, then it will
110211** be necessary to undo the completed writes.
110212*/
110213SQLITE_PRIVATE void sqlite3MultiWrite(Parse *pParse){
110214 Parse *pToplevel = sqlite3ParseToplevel(pParse);
110215 pToplevel->isMultiWrite = 1;
110216}
110217
110218/*
110219** The code generator calls this routine if is discovers that it is
110220** possible to abort a statement prior to completion. In order to
110221** perform this abort without corrupting the database, we need to make
110222** sure that the statement is protected by a statement transaction.
110223**
110224** Technically, we only need to set the mayAbort flag if the
110225** isMultiWrite flag was previously set. There is a time dependency
110226** such that the abort must occur after the multiwrite. This makes
110227** some statements involving the REPLACE conflict resolution algorithm
110228** go a little faster. But taking advantage of this time dependency
110229** makes it more difficult to prove that the code is correct (in
110230** particular, it prevents us from writing an effective
110231** implementation of sqlite3AssertMayAbort()) and so we have chosen
110232** to take the safe route and skip the optimization.
110233*/
110234SQLITE_PRIVATE void sqlite3MayAbort(Parse *pParse){
110235 Parse *pToplevel = sqlite3ParseToplevel(pParse);
110236 pToplevel->mayAbort = 1;
110237}
110238
110239/*
110240** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
110241** error. The onError parameter determines which (if any) of the statement
110242** and/or current transaction is rolled back.
110243*/
110244SQLITE_PRIVATE void sqlite3HaltConstraint(
110245 Parse *pParse, /* Parsing context */
110246 int errCode, /* extended error code */
110247 int onError, /* Constraint type */
110248 char *p4, /* Error message */
110249 i8 p4type, /* P4_STATIC or P4_TRANSIENT */
110250 u8 p5Errmsg /* P5_ErrMsg type */
110251){
110252 Vdbe *v = sqlite3GetVdbe(pParse);
110253 assert( (errCode&0xff)==SQLITE_CONSTRAINT );
110254 if( onError==OE_Abort ){
110255 sqlite3MayAbort(pParse);
110256 }
110257 sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type);
110258 sqlite3VdbeChangeP5(v, p5Errmsg);
110259}
110260
110261/*
110262** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation.
110263*/
110264SQLITE_PRIVATE void sqlite3UniqueConstraint(
110265 Parse *pParse, /* Parsing context */
110266 int onError, /* Constraint type */
110267 Index *pIdx /* The index that triggers the constraint */
110268){
110269 char *zErr;
110270 int j;
110271 StrAccum errMsg;
110272 Table *pTab = pIdx->pTable;
110273
110274 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200);
110275 if( pIdx->aColExpr ){
110276 sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
110277 }else{
110278 for(j=0; j<pIdx->nKeyCol; j++){
110279 char *zCol;
110280 assert( pIdx->aiColumn[j]>=0 );
110281 zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
110282 if( j ) sqlite3_str_append(&errMsg, ", ", 2);
110283 sqlite3_str_appendall(&errMsg, pTab->zName);
110284 sqlite3_str_append(&errMsg, ".", 1);
110285 sqlite3_str_appendall(&errMsg, zCol);
110286 }
110287 }
110288 zErr = sqlite3StrAccumFinish(&errMsg);
110289 sqlite3HaltConstraint(pParse,
110290 IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY
110291 : SQLITE_CONSTRAINT_UNIQUE,
110292 onError, zErr, P4_DYNAMIC, P5_ConstraintUnique);
110293}
110294
110295
110296/*
110297** Code an OP_Halt due to non-unique rowid.
110298*/
110299SQLITE_PRIVATE void sqlite3RowidConstraint(
110300 Parse *pParse, /* Parsing context */
110301 int onError, /* Conflict resolution algorithm */
110302 Table *pTab /* The table with the non-unique rowid */
110303){
110304 char *zMsg;
110305 int rc;
110306 if( pTab->iPKey>=0 ){
110307 zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
110308 pTab->aCol[pTab->iPKey].zName);
110309 rc = SQLITE_CONSTRAINT_PRIMARYKEY;
110310 }else{
110311 zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
110312 rc = SQLITE_CONSTRAINT_ROWID;
110313 }
110314 sqlite3HaltConstraint(pParse, rc, onError, zMsg, P4_DYNAMIC,
110315 P5_ConstraintUnique);
110316}
110317
110318/*
110319** Check to see if pIndex uses the collating sequence pColl. Return
110320** true if it does and false if it does not.
110321*/
110322#ifndef SQLITE_OMIT_REINDEX
110323static int collationMatch(const char *zColl, Index *pIndex){
110324 int i;
110325 assert( zColl!=0 );
110326 for(i=0; i<pIndex->nColumn; i++){
110327 const char *z = pIndex->azColl[i];
110328 assert( z!=0 || pIndex->aiColumn[i]<0 );
110329 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
110330 return 1;
110331 }
110332 }
110333 return 0;
110334}
110335#endif
110336
110337/*
110338** Recompute all indices of pTab that use the collating sequence pColl.
110339** If pColl==0 then recompute all indices of pTab.
110340*/
110341#ifndef SQLITE_OMIT_REINDEX
110342static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
110343 Index *pIndex; /* An index associated with pTab */
110344
110345 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
110346 if( zColl==0 || collationMatch(zColl, pIndex) ){
110347 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
110348 sqlite3BeginWriteOperation(pParse, 0, iDb);
110349 sqlite3RefillIndex(pParse, pIndex, -1);
110350 }
110351 }
110352}
110353#endif
110354
110355/*
110356** Recompute all indices of all tables in all databases where the
110357** indices use the collating sequence pColl. If pColl==0 then recompute
110358** all indices everywhere.
110359*/
110360#ifndef SQLITE_OMIT_REINDEX
110361static void reindexDatabases(Parse *pParse, char const *zColl){
110362 Db *pDb; /* A single database */
110363 int iDb; /* The database index number */
110364 sqlite3 *db = pParse->db; /* The database connection */
110365 HashElem *k; /* For looping over tables in pDb */
110366 Table *pTab; /* A table in the database */
110367
110368 assert( sqlite3BtreeHoldsAllMutexes(db) ); /* Needed for schema access */
110369 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
110370 assert( pDb!=0 );
110371 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){
110372 pTab = (Table*)sqliteHashData(k);
110373 reindexTable(pParse, pTab, zColl);
110374 }
110375 }
110376}
110377#endif
110378
110379/*
110380** Generate code for the REINDEX command.
110381**
110382** REINDEX -- 1
110383** REINDEX <collation> -- 2
110384** REINDEX ?<database>.?<tablename> -- 3
110385** REINDEX ?<database>.?<indexname> -- 4
110386**
110387** Form 1 causes all indices in all attached databases to be rebuilt.
110388** Form 2 rebuilds all indices in all databases that use the named
110389** collating function. Forms 3 and 4 rebuild the named index or all
110390** indices associated with the named table.
110391*/
110392#ifndef SQLITE_OMIT_REINDEX
110393SQLITE_PRIVATE void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
110394 CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */
110395 char *z; /* Name of a table or index */
110396 const char *zDb; /* Name of the database */
110397 Table *pTab; /* A table in the database */
110398 Index *pIndex; /* An index associated with pTab */
110399 int iDb; /* The database index number */
110400 sqlite3 *db = pParse->db; /* The database connection */
110401 Token *pObjName; /* Name of the table or index to be reindexed */
110402
110403 /* Read the database schema. If an error occurs, leave an error message
110404 ** and code in pParse and return NULL. */
110405 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
110406 return;
110407 }
110408
110409 if( pName1==0 ){
110410 reindexDatabases(pParse, 0);
110411 return;
110412 }else if( NEVER(pName2==0) || pName2->z==0 ){
110413 char *zColl;
110414 assert( pName1->z );
110415 zColl = sqlite3NameFromToken(pParse->db, pName1);
110416 if( !zColl ) return;
110417 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
110418 if( pColl ){
110419 reindexDatabases(pParse, zColl);
110420 sqlite3DbFree(db, zColl);
110421 return;
110422 }
110423 sqlite3DbFree(db, zColl);
110424 }
110425 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
110426 if( iDb<0 ) return;
110427 z = sqlite3NameFromToken(db, pObjName);
110428 if( z==0 ) return;
110429 zDb = db->aDb[iDb].zDbSName;
110430 pTab = sqlite3FindTable(db, z, zDb);
110431 if( pTab ){
110432 reindexTable(pParse, pTab, 0);
110433 sqlite3DbFree(db, z);
110434 return;
110435 }
110436 pIndex = sqlite3FindIndex(db, z, zDb);
110437 sqlite3DbFree(db, z);
110438 if( pIndex ){
110439 sqlite3BeginWriteOperation(pParse, 0, iDb);
110440 sqlite3RefillIndex(pParse, pIndex, -1);
110441 return;
110442 }
110443 sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
110444}
110445#endif
110446
110447/*
110448** Return a KeyInfo structure that is appropriate for the given Index.
110449**
110450** The caller should invoke sqlite3KeyInfoUnref() on the returned object
110451** when it has finished using it.
110452*/
110453SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){
110454 int i;
110455 int nCol = pIdx->nColumn;
110456 int nKey = pIdx->nKeyCol;
110457 KeyInfo *pKey;
110458 if( pParse->nErr ) return 0;
110459 if( pIdx->uniqNotNull ){
110460 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
110461 }else{
110462 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
110463 }
110464 if( pKey ){
110465 assert( sqlite3KeyInfoIsWriteable(pKey) );
110466 for(i=0; i<nCol; i++){
110467 const char *zColl = pIdx->azColl[i];
110468 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
110469 sqlite3LocateCollSeq(pParse, zColl);
110470 pKey->aSortOrder[i] = pIdx->aSortOrder[i];
110471 }
110472 if( pParse->nErr ){
110473 assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ );
110474 if( pIdx->bNoQuery==0 ){
110475 /* Deactivate the index because it contains an unknown collating
110476 ** sequence. The only way to reactive the index is to reload the
110477 ** schema. Adding the missing collating sequence later does not
110478 ** reactive the index. The application had the chance to register
110479 ** the missing index using the collation-needed callback. For
110480 ** simplicity, SQLite will not give the application a second chance.
110481 */
110482 pIdx->bNoQuery = 1;
110483 pParse->rc = SQLITE_ERROR_RETRY;
110484 }
110485 sqlite3KeyInfoUnref(pKey);
110486 pKey = 0;
110487 }
110488 }
110489 return pKey;
110490}
110491
110492#ifndef SQLITE_OMIT_CTE
110493/*
110494** This routine is invoked once per CTE by the parser while parsing a
110495** WITH clause.
110496*/
110497SQLITE_PRIVATE With *sqlite3WithAdd(
110498 Parse *pParse, /* Parsing context */
110499 With *pWith, /* Existing WITH clause, or NULL */
110500 Token *pName, /* Name of the common-table */
110501 ExprList *pArglist, /* Optional column name list for the table */
110502 Select *pQuery /* Query used to initialize the table */
110503){
110504 sqlite3 *db = pParse->db;
110505 With *pNew;
110506 char *zName;
110507
110508 /* Check that the CTE name is unique within this WITH clause. If
110509 ** not, store an error in the Parse structure. */
110510 zName = sqlite3NameFromToken(pParse->db, pName);
110511 if( zName && pWith ){
110512 int i;
110513 for(i=0; i<pWith->nCte; i++){
110514 if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
110515 sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName);
110516 }
110517 }
110518 }
110519
110520 if( pWith ){
110521 int nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
110522 pNew = sqlite3DbRealloc(db, pWith, nByte);
110523 }else{
110524 pNew = sqlite3DbMallocZero(db, sizeof(*pWith));
110525 }
110526 assert( (pNew!=0 && zName!=0) || db->mallocFailed );
110527
110528 if( db->mallocFailed ){
110529 sqlite3ExprListDelete(db, pArglist);
110530 sqlite3SelectDelete(db, pQuery);
110531 sqlite3DbFree(db, zName);
110532 pNew = pWith;
110533 }else{
110534 pNew->a[pNew->nCte].pSelect = pQuery;
110535 pNew->a[pNew->nCte].pCols = pArglist;
110536 pNew->a[pNew->nCte].zName = zName;
110537 pNew->a[pNew->nCte].zCteErr = 0;
110538 pNew->nCte++;
110539 }
110540
110541 return pNew;
110542}
110543
110544/*
110545** Free the contents of the With object passed as the second argument.
110546*/
110547SQLITE_PRIVATE void sqlite3WithDelete(sqlite3 *db, With *pWith){
110548 if( pWith ){
110549 int i;
110550 for(i=0; i<pWith->nCte; i++){
110551 struct Cte *pCte = &pWith->a[i];
110552 sqlite3ExprListDelete(db, pCte->pCols);
110553 sqlite3SelectDelete(db, pCte->pSelect);
110554 sqlite3DbFree(db, pCte->zName);
110555 }
110556 sqlite3DbFree(db, pWith);
110557 }
110558}
110559#endif /* !defined(SQLITE_OMIT_CTE) */
110560
110561/************** End of build.c ***********************************************/
110562/************** Begin file callback.c ****************************************/
110563/*
110564** 2005 May 23
110565**
110566** The author disclaims copyright to this source code. In place of
110567** a legal notice, here is a blessing:
110568**
110569** May you do good and not evil.
110570** May you find forgiveness for yourself and forgive others.
110571** May you share freely, never taking more than you give.
110572**
110573*************************************************************************
110574**
110575** This file contains functions used to access the internal hash tables
110576** of user defined functions and collation sequences.
110577*/
110578
110579/* #include "sqliteInt.h" */
110580
110581/*
110582** Invoke the 'collation needed' callback to request a collation sequence
110583** in the encoding enc of name zName, length nName.
110584*/
110585static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
110586 assert( !db->xCollNeeded || !db->xCollNeeded16 );
110587 if( db->xCollNeeded ){
110588 char *zExternal = sqlite3DbStrDup(db, zName);
110589 if( !zExternal ) return;
110590 db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
110591 sqlite3DbFree(db, zExternal);
110592 }
110593#ifndef SQLITE_OMIT_UTF16
110594 if( db->xCollNeeded16 ){
110595 char const *zExternal;
110596 sqlite3_value *pTmp = sqlite3ValueNew(db);
110597 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
110598 zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
110599 if( zExternal ){
110600 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
110601 }
110602 sqlite3ValueFree(pTmp);
110603 }
110604#endif
110605}
110606
110607/*
110608** This routine is called if the collation factory fails to deliver a
110609** collation function in the best encoding but there may be other versions
110610** of this collation function (for other text encodings) available. Use one
110611** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
110612** possible.
110613*/
110614static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
110615 CollSeq *pColl2;
110616 char *z = pColl->zName;
110617 int i;
110618 static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
110619 for(i=0; i<3; i++){
110620 pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
110621 if( pColl2->xCmp!=0 ){
110622 memcpy(pColl, pColl2, sizeof(CollSeq));
110623 pColl->xDel = 0; /* Do not copy the destructor */
110624 return SQLITE_OK;
110625 }
110626 }
110627 return SQLITE_ERROR;
110628}
110629
110630/*
110631** This function is responsible for invoking the collation factory callback
110632** or substituting a collation sequence of a different encoding when the
110633** requested collation sequence is not available in the desired encoding.
110634**
110635** If it is not NULL, then pColl must point to the database native encoding
110636** collation sequence with name zName, length nName.
110637**
110638** The return value is either the collation sequence to be used in database
110639** db for collation type name zName, length nName, or NULL, if no collation
110640** sequence can be found. If no collation is found, leave an error message.
110641**
110642** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
110643*/
110644SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(
110645 Parse *pParse, /* Parsing context */
110646 u8 enc, /* The desired encoding for the collating sequence */
110647 CollSeq *pColl, /* Collating sequence with native encoding, or NULL */
110648 const char *zName /* Collating sequence name */
110649){
110650 CollSeq *p;
110651 sqlite3 *db = pParse->db;
110652
110653 p = pColl;
110654 if( !p ){
110655 p = sqlite3FindCollSeq(db, enc, zName, 0);
110656 }
110657 if( !p || !p->xCmp ){
110658 /* No collation sequence of this type for this encoding is registered.
110659 ** Call the collation factory to see if it can supply us with one.
110660 */
110661 callCollNeeded(db, enc, zName);
110662 p = sqlite3FindCollSeq(db, enc, zName, 0);
110663 }
110664 if( p && !p->xCmp && synthCollSeq(db, p) ){
110665 p = 0;
110666 }
110667 assert( !p || p->xCmp );
110668 if( p==0 ){
110669 sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
110670 pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ;
110671 }
110672 return p;
110673}
110674
110675/*
110676** This routine is called on a collation sequence before it is used to
110677** check that it is defined. An undefined collation sequence exists when
110678** a database is loaded that contains references to collation sequences
110679** that have not been defined by sqlite3_create_collation() etc.
110680**
110681** If required, this routine calls the 'collation needed' callback to
110682** request a definition of the collating sequence. If this doesn't work,
110683** an equivalent collating sequence that uses a text encoding different
110684** from the main database is substituted, if one is available.
110685*/
110686SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
110687 if( pColl && pColl->xCmp==0 ){
110688 const char *zName = pColl->zName;
110689 sqlite3 *db = pParse->db;
110690 CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName);
110691 if( !p ){
110692 return SQLITE_ERROR;
110693 }
110694 assert( p==pColl );
110695 }
110696 return SQLITE_OK;
110697}
110698
110699
110700
110701/*
110702** Locate and return an entry from the db.aCollSeq hash table. If the entry
110703** specified by zName and nName is not found and parameter 'create' is
110704** true, then create a new entry. Otherwise return NULL.
110705**
110706** Each pointer stored in the sqlite3.aCollSeq hash table contains an
110707** array of three CollSeq structures. The first is the collation sequence
110708** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
110709**
110710** Stored immediately after the three collation sequences is a copy of
110711** the collation sequence name. A pointer to this string is stored in
110712** each collation sequence structure.
110713*/
110714static CollSeq *findCollSeqEntry(
110715 sqlite3 *db, /* Database connection */
110716 const char *zName, /* Name of the collating sequence */
110717 int create /* Create a new entry if true */
110718){
110719 CollSeq *pColl;
110720 pColl = sqlite3HashFind(&db->aCollSeq, zName);
110721
110722 if( 0==pColl && create ){
110723 int nName = sqlite3Strlen30(zName) + 1;
110724 pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName);
110725 if( pColl ){
110726 CollSeq *pDel = 0;
110727 pColl[0].zName = (char*)&pColl[3];
110728 pColl[0].enc = SQLITE_UTF8;
110729 pColl[1].zName = (char*)&pColl[3];
110730 pColl[1].enc = SQLITE_UTF16LE;
110731 pColl[2].zName = (char*)&pColl[3];
110732 pColl[2].enc = SQLITE_UTF16BE;
110733 memcpy(pColl[0].zName, zName, nName);
110734 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
110735
110736 /* If a malloc() failure occurred in sqlite3HashInsert(), it will
110737 ** return the pColl pointer to be deleted (because it wasn't added
110738 ** to the hash table).
110739 */
110740 assert( pDel==0 || pDel==pColl );
110741 if( pDel!=0 ){
110742 sqlite3OomFault(db);
110743 sqlite3DbFree(db, pDel);
110744 pColl = 0;
110745 }
110746 }
110747 }
110748 return pColl;
110749}
110750
110751/*
110752** Parameter zName points to a UTF-8 encoded string nName bytes long.
110753** Return the CollSeq* pointer for the collation sequence named zName
110754** for the encoding 'enc' from the database 'db'.
110755**
110756** If the entry specified is not found and 'create' is true, then create a
110757** new entry. Otherwise return NULL.
110758**
110759** A separate function sqlite3LocateCollSeq() is a wrapper around
110760** this routine. sqlite3LocateCollSeq() invokes the collation factory
110761** if necessary and generates an error message if the collating sequence
110762** cannot be found.
110763**
110764** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
110765*/
110766SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(
110767 sqlite3 *db,
110768 u8 enc,
110769 const char *zName,
110770 int create
110771){
110772 CollSeq *pColl;
110773 if( zName ){
110774 pColl = findCollSeqEntry(db, zName, create);
110775 }else{
110776 pColl = db->pDfltColl;
110777 }
110778 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
110779 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
110780 if( pColl ) pColl += enc-1;
110781 return pColl;
110782}
110783
110784/* During the search for the best function definition, this procedure
110785** is called to test how well the function passed as the first argument
110786** matches the request for a function with nArg arguments in a system
110787** that uses encoding enc. The value returned indicates how well the
110788** request is matched. A higher value indicates a better match.
110789**
110790** If nArg is -1 that means to only return a match (non-zero) if p->nArg
110791** is also -1. In other words, we are searching for a function that
110792** takes a variable number of arguments.
110793**
110794** If nArg is -2 that means that we are searching for any function
110795** regardless of the number of arguments it uses, so return a positive
110796** match score for any
110797**
110798** The returned value is always between 0 and 6, as follows:
110799**
110800** 0: Not a match.
110801** 1: UTF8/16 conversion required and function takes any number of arguments.
110802** 2: UTF16 byte order change required and function takes any number of args.
110803** 3: encoding matches and function takes any number of arguments
110804** 4: UTF8/16 conversion required - argument count matches exactly
110805** 5: UTF16 byte order conversion required - argument count matches exactly
110806** 6: Perfect match: encoding and argument count match exactly.
110807**
110808** If nArg==(-2) then any function with a non-null xSFunc is
110809** a perfect match and any function with xSFunc NULL is
110810** a non-match.
110811*/
110812#define FUNC_PERFECT_MATCH 6 /* The score for a perfect match */
110813static int matchQuality(
110814 FuncDef *p, /* The function we are evaluating for match quality */
110815 int nArg, /* Desired number of arguments. (-1)==any */
110816 u8 enc /* Desired text encoding */
110817){
110818 int match;
110819
110820 /* nArg of -2 is a special case */
110821 if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH;
110822
110823 /* Wrong number of arguments means "no match" */
110824 if( p->nArg!=nArg && p->nArg>=0 ) return 0;
110825
110826 /* Give a better score to a function with a specific number of arguments
110827 ** than to function that accepts any number of arguments. */
110828 if( p->nArg==nArg ){
110829 match = 4;
110830 }else{
110831 match = 1;
110832 }
110833
110834 /* Bonus points if the text encoding matches */
110835 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
110836 match += 2; /* Exact encoding match */
110837 }else if( (enc & p->funcFlags & 2)!=0 ){
110838 match += 1; /* Both are UTF16, but with different byte orders */
110839 }
110840
110841 return match;
110842}
110843
110844/*
110845** Search a FuncDefHash for a function with the given name. Return
110846** a pointer to the matching FuncDef if found, or 0 if there is no match.
110847*/
110848static FuncDef *functionSearch(
110849 int h, /* Hash of the name */
110850 const char *zFunc /* Name of function */
110851){
110852 FuncDef *p;
110853 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
110854 if( sqlite3StrICmp(p->zName, zFunc)==0 ){
110855 return p;
110856 }
110857 }
110858 return 0;
110859}
110860#ifdef SQLITE_ENABLE_NORMALIZE
110861SQLITE_PRIVATE FuncDef *sqlite3FunctionSearchN(
110862 int h, /* Hash of the name */
110863 const char *zFunc, /* Name of function */
110864 int nFunc /* Length of the name */
110865){
110866 FuncDef *p;
110867 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
110868 if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 ){
110869 return p;
110870 }
110871 }
110872 return 0;
110873}
110874#endif /* SQLITE_ENABLE_NORMALIZE */
110875
110876/*
110877** Insert a new FuncDef into a FuncDefHash hash table.
110878*/
110879SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(
110880 FuncDef *aDef, /* List of global functions to be inserted */
110881 int nDef /* Length of the apDef[] list */
110882){
110883 int i;
110884 for(i=0; i<nDef; i++){
110885 FuncDef *pOther;
110886 const char *zName = aDef[i].zName;
110887 int nName = sqlite3Strlen30(zName);
110888 int h = SQLITE_FUNC_HASH(zName[0], nName);
110889 assert( zName[0]>='a' && zName[0]<='z' );
110890 pOther = functionSearch(h, zName);
110891 if( pOther ){
110892 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
110893 aDef[i].pNext = pOther->pNext;
110894 pOther->pNext = &aDef[i];
110895 }else{
110896 aDef[i].pNext = 0;
110897 aDef[i].u.pHash = sqlite3BuiltinFunctions.a[h];
110898 sqlite3BuiltinFunctions.a[h] = &aDef[i];
110899 }
110900 }
110901}
110902
110903
110904
110905/*
110906** Locate a user function given a name, a number of arguments and a flag
110907** indicating whether the function prefers UTF-16 over UTF-8. Return a
110908** pointer to the FuncDef structure that defines that function, or return
110909** NULL if the function does not exist.
110910**
110911** If the createFlag argument is true, then a new (blank) FuncDef
110912** structure is created and liked into the "db" structure if a
110913** no matching function previously existed.
110914**
110915** If nArg is -2, then the first valid function found is returned. A
110916** function is valid if xSFunc is non-zero. The nArg==(-2)
110917** case is used to see if zName is a valid function name for some number
110918** of arguments. If nArg is -2, then createFlag must be 0.
110919**
110920** If createFlag is false, then a function with the required name and
110921** number of arguments may be returned even if the eTextRep flag does not
110922** match that requested.
110923*/
110924SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
110925 sqlite3 *db, /* An open database */
110926 const char *zName, /* Name of the function. zero-terminated */
110927 int nArg, /* Number of arguments. -1 means any number */
110928 u8 enc, /* Preferred text encoding */
110929 u8 createFlag /* Create new entry if true and does not otherwise exist */
110930){
110931 FuncDef *p; /* Iterator variable */
110932 FuncDef *pBest = 0; /* Best match found so far */
110933 int bestScore = 0; /* Score of best match */
110934 int h; /* Hash value */
110935 int nName; /* Length of the name */
110936
110937 assert( nArg>=(-2) );
110938 assert( nArg>=(-1) || createFlag==0 );
110939 nName = sqlite3Strlen30(zName);
110940
110941 /* First search for a match amongst the application-defined functions.
110942 */
110943 p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
110944 while( p ){
110945 int score = matchQuality(p, nArg, enc);
110946 if( score>bestScore ){
110947 pBest = p;
110948 bestScore = score;
110949 }
110950 p = p->pNext;
110951 }
110952
110953 /* If no match is found, search the built-in functions.
110954 **
110955 ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
110956 ** functions even if a prior app-defined function was found. And give
110957 ** priority to built-in functions.
110958 **
110959 ** Except, if createFlag is true, that means that we are trying to
110960 ** install a new function. Whatever FuncDef structure is returned it will
110961 ** have fields overwritten with new information appropriate for the
110962 ** new function. But the FuncDefs for built-in functions are read-only.
110963 ** So we must not search for built-ins when creating a new function.
110964 */
110965 if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
110966 bestScore = 0;
110967 h = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zName[0]], nName);
110968 p = functionSearch(h, zName);
110969 while( p ){
110970 int score = matchQuality(p, nArg, enc);
110971 if( score>bestScore ){
110972 pBest = p;
110973 bestScore = score;
110974 }
110975 p = p->pNext;
110976 }
110977 }
110978
110979 /* If the createFlag parameter is true and the search did not reveal an
110980 ** exact match for the name, number of arguments and encoding, then add a
110981 ** new entry to the hash table and return it.
110982 */
110983 if( createFlag && bestScore<FUNC_PERFECT_MATCH &&
110984 (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
110985 FuncDef *pOther;
110986 u8 *z;
110987 pBest->zName = (const char*)&pBest[1];
110988 pBest->nArg = (u16)nArg;
110989 pBest->funcFlags = enc;
110990 memcpy((char*)&pBest[1], zName, nName+1);
110991 for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
110992 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
110993 if( pOther==pBest ){
110994 sqlite3DbFree(db, pBest);
110995 sqlite3OomFault(db);
110996 return 0;
110997 }else{
110998 pBest->pNext = pOther;
110999 }
111000 }
111001
111002 if( pBest && (pBest->xSFunc || createFlag) ){
111003 return pBest;
111004 }
111005 return 0;
111006}
111007
111008/*
111009** Free all resources held by the schema structure. The void* argument points
111010** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
111011** pointer itself, it just cleans up subsidiary resources (i.e. the contents
111012** of the schema hash tables).
111013**
111014** The Schema.cache_size variable is not cleared.
111015*/
111016SQLITE_PRIVATE void sqlite3SchemaClear(void *p){
111017 Hash temp1;
111018 Hash temp2;
111019 HashElem *pElem;
111020 Schema *pSchema = (Schema *)p;
111021
111022 temp1 = pSchema->tblHash;
111023 temp2 = pSchema->trigHash;
111024 sqlite3HashInit(&pSchema->trigHash);
111025 sqlite3HashClear(&pSchema->idxHash);
111026 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
111027 sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem));
111028 }
111029 sqlite3HashClear(&temp2);
111030 sqlite3HashInit(&pSchema->tblHash);
111031 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
111032 Table *pTab = sqliteHashData(pElem);
111033 sqlite3DeleteTable(0, pTab);
111034 }
111035 sqlite3HashClear(&temp1);
111036 sqlite3HashClear(&pSchema->fkeyHash);
111037 pSchema->pSeqTab = 0;
111038 if( pSchema->schemaFlags & DB_SchemaLoaded ){
111039 pSchema->iGeneration++;
111040 }
111041 pSchema->schemaFlags &= ~(DB_SchemaLoaded|DB_ResetWanted);
111042}
111043
111044/*
111045** Find and return the schema associated with a BTree. Create
111046** a new one if necessary.
111047*/
111048SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
111049 Schema * p;
111050 if( pBt ){
111051 p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
111052 }else{
111053 p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
111054 }
111055 if( !p ){
111056 sqlite3OomFault(db);
111057 }else if ( 0==p->file_format ){
111058 sqlite3HashInit(&p->tblHash);
111059 sqlite3HashInit(&p->idxHash);
111060 sqlite3HashInit(&p->trigHash);
111061 sqlite3HashInit(&p->fkeyHash);
111062 p->enc = SQLITE_UTF8;
111063 }
111064 return p;
111065}
111066
111067/************** End of callback.c ********************************************/
111068/************** Begin file delete.c ******************************************/
111069/*
111070** 2001 September 15
111071**
111072** The author disclaims copyright to this source code. In place of
111073** a legal notice, here is a blessing:
111074**
111075** May you do good and not evil.
111076** May you find forgiveness for yourself and forgive others.
111077** May you share freely, never taking more than you give.
111078**
111079*************************************************************************
111080** This file contains C code routines that are called by the parser
111081** in order to generate code for DELETE FROM statements.
111082*/
111083/* #include "sqliteInt.h" */
111084
111085/*
111086** While a SrcList can in general represent multiple tables and subqueries
111087** (as in the FROM clause of a SELECT statement) in this case it contains
111088** the name of a single table, as one might find in an INSERT, DELETE,
111089** or UPDATE statement. Look up that table in the symbol table and
111090** return a pointer. Set an error message and return NULL if the table
111091** name is not found or if any other error occurs.
111092**
111093** The following fields are initialized appropriate in pSrc:
111094**
111095** pSrc->a[0].pTab Pointer to the Table object
111096** pSrc->a[0].pIndex Pointer to the INDEXED BY index, if there is one
111097**
111098*/
111099SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
111100 struct SrcList_item *pItem = pSrc->a;
111101 Table *pTab;
111102 assert( pItem && pSrc->nSrc==1 );
111103 pTab = sqlite3LocateTableItem(pParse, 0, pItem);
111104 sqlite3DeleteTable(pParse->db, pItem->pTab);
111105 pItem->pTab = pTab;
111106 if( pTab ){
111107 pTab->nTabRef++;
111108 }
111109 if( sqlite3IndexedByLookup(pParse, pItem) ){
111110 pTab = 0;
111111 }
111112 return pTab;
111113}
111114
111115/* Return true if table pTab is read-only.
111116**
111117** A table is read-only if any of the following are true:
111118**
111119** 1) It is a virtual table and no implementation of the xUpdate method
111120** has been provided
111121**
111122** 2) It is a system table (i.e. sqlite_master), this call is not
111123** part of a nested parse and writable_schema pragma has not
111124** been specified
111125**
111126** 3) The table is a shadow table, the database connection is in
111127** defensive mode, and the current sqlite3_prepare()
111128** is for a top-level SQL statement.
111129*/
111130static int tabIsReadOnly(Parse *pParse, Table *pTab){
111131 sqlite3 *db;
111132 if( IsVirtual(pTab) ){
111133 return sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0;
111134 }
111135 if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0;
111136 db = pParse->db;
111137 if( (pTab->tabFlags & TF_Readonly)!=0 ){
111138 return sqlite3WritableSchema(db)==0 && pParse->nested==0;
111139 }
111140 assert( pTab->tabFlags & TF_Shadow );
111141 return (db->flags & SQLITE_Defensive)!=0
111142#ifndef SQLITE_OMIT_VIRTUALTABLE
111143 && db->pVtabCtx==0
111144#endif
111145 && db->nVdbeExec==0;
111146}
111147
111148/*
111149** Check to make sure the given table is writable. If it is not
111150** writable, generate an error message and return 1. If it is
111151** writable return 0;
111152*/
111153SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
111154 if( tabIsReadOnly(pParse, pTab) ){
111155 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
111156 return 1;
111157 }
111158#ifndef SQLITE_OMIT_VIEW
111159 if( !viewOk && pTab->pSelect ){
111160 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
111161 return 1;
111162 }
111163#endif
111164 return 0;
111165}
111166
111167
111168#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
111169/*
111170** Evaluate a view and store its result in an ephemeral table. The
111171** pWhere argument is an optional WHERE clause that restricts the
111172** set of rows in the view that are to be added to the ephemeral table.
111173*/
111174SQLITE_PRIVATE void sqlite3MaterializeView(
111175 Parse *pParse, /* Parsing context */
111176 Table *pView, /* View definition */
111177 Expr *pWhere, /* Optional WHERE clause to be added */
111178 ExprList *pOrderBy, /* Optional ORDER BY clause */
111179 Expr *pLimit, /* Optional LIMIT clause */
111180 int iCur /* Cursor number for ephemeral table */
111181){
111182 SelectDest dest;
111183 Select *pSel;
111184 SrcList *pFrom;
111185 sqlite3 *db = pParse->db;
111186 int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
111187 pWhere = sqlite3ExprDup(db, pWhere, 0);
111188 pFrom = sqlite3SrcListAppend(db, 0, 0, 0);
111189 if( pFrom ){
111190 assert( pFrom->nSrc==1 );
111191 pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
111192 pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
111193 assert( pFrom->a[0].pOn==0 );
111194 assert( pFrom->a[0].pUsing==0 );
111195 }
111196 pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, pOrderBy,
111197 SF_IncludeHidden, pLimit);
111198 sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
111199 sqlite3Select(pParse, pSel, &dest);
111200 sqlite3SelectDelete(db, pSel);
111201}
111202#endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
111203
111204#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
111205/*
111206** Generate an expression tree to implement the WHERE, ORDER BY,
111207** and LIMIT/OFFSET portion of DELETE and UPDATE statements.
111208**
111209** DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
111210** \__________________________/
111211** pLimitWhere (pInClause)
111212*/
111213SQLITE_PRIVATE Expr *sqlite3LimitWhere(
111214 Parse *pParse, /* The parser context */
111215 SrcList *pSrc, /* the FROM clause -- which tables to scan */
111216 Expr *pWhere, /* The WHERE clause. May be null */
111217 ExprList *pOrderBy, /* The ORDER BY clause. May be null */
111218 Expr *pLimit, /* The LIMIT clause. May be null */
111219 char *zStmtType /* Either DELETE or UPDATE. For err msgs. */
111220){
111221 sqlite3 *db = pParse->db;
111222 Expr *pLhs = NULL; /* LHS of IN(SELECT...) operator */
111223 Expr *pInClause = NULL; /* WHERE rowid IN ( select ) */
111224 ExprList *pEList = NULL; /* Expression list contaning only pSelectRowid */
111225 SrcList *pSelectSrc = NULL; /* SELECT rowid FROM x ... (dup of pSrc) */
111226 Select *pSelect = NULL; /* Complete SELECT tree */
111227 Table *pTab;
111228
111229 /* Check that there isn't an ORDER BY without a LIMIT clause.
111230 */
111231 if( pOrderBy && pLimit==0 ) {
111232 sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
111233 sqlite3ExprDelete(pParse->db, pWhere);
111234 sqlite3ExprListDelete(pParse->db, pOrderBy);
111235 return 0;
111236 }
111237
111238 /* We only need to generate a select expression if there
111239 ** is a limit/offset term to enforce.
111240 */
111241 if( pLimit == 0 ) {
111242 return pWhere;
111243 }
111244
111245 /* Generate a select expression tree to enforce the limit/offset
111246 ** term for the DELETE or UPDATE statement. For example:
111247 ** DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
111248 ** becomes:
111249 ** DELETE FROM table_a WHERE rowid IN (
111250 ** SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
111251 ** );
111252 */
111253
111254 pTab = pSrc->a[0].pTab;
111255 if( HasRowid(pTab) ){
111256 pLhs = sqlite3PExpr(pParse, TK_ROW, 0, 0);
111257 pEList = sqlite3ExprListAppend(
111258 pParse, 0, sqlite3PExpr(pParse, TK_ROW, 0, 0)
111259 );
111260 }else{
111261 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
111262 if( pPk->nKeyCol==1 ){
111263 const char *zName = pTab->aCol[pPk->aiColumn[0]].zName;
111264 pLhs = sqlite3Expr(db, TK_ID, zName);
111265 pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID, zName));
111266 }else{
111267 int i;
111268 for(i=0; i<pPk->nKeyCol; i++){
111269 Expr *p = sqlite3Expr(db, TK_ID, pTab->aCol[pPk->aiColumn[i]].zName);
111270 pEList = sqlite3ExprListAppend(pParse, pEList, p);
111271 }
111272 pLhs = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
111273 if( pLhs ){
111274 pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0);
111275 }
111276 }
111277 }
111278
111279 /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
111280 ** and the SELECT subtree. */
111281 pSrc->a[0].pTab = 0;
111282 pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
111283 pSrc->a[0].pTab = pTab;
111284 pSrc->a[0].pIBIndex = 0;
111285
111286 /* generate the SELECT expression tree. */
111287 pSelect = sqlite3SelectNew(pParse, pEList, pSelectSrc, pWhere, 0 ,0,
111288 pOrderBy,0,pLimit
111289 );
111290
111291 /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
111292 pInClause = sqlite3PExpr(pParse, TK_IN, pLhs, 0);
111293 sqlite3PExprAddSelect(pParse, pInClause, pSelect);
111294 return pInClause;
111295}
111296#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) */
111297 /* && !defined(SQLITE_OMIT_SUBQUERY) */
111298
111299/*
111300** Generate code for a DELETE FROM statement.
111301**
111302** DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
111303** \________/ \________________/
111304** pTabList pWhere
111305*/
111306SQLITE_PRIVATE void sqlite3DeleteFrom(
111307 Parse *pParse, /* The parser context */
111308 SrcList *pTabList, /* The table from which we should delete things */
111309 Expr *pWhere, /* The WHERE clause. May be null */
111310 ExprList *pOrderBy, /* ORDER BY clause. May be null */
111311 Expr *pLimit /* LIMIT clause. May be null */
111312){
111313 Vdbe *v; /* The virtual database engine */
111314 Table *pTab; /* The table from which records will be deleted */
111315 int i; /* Loop counter */
111316 WhereInfo *pWInfo; /* Information about the WHERE clause */
111317 Index *pIdx; /* For looping over indices of the table */
111318 int iTabCur; /* Cursor number for the table */
111319 int iDataCur = 0; /* VDBE cursor for the canonical data source */
111320 int iIdxCur = 0; /* Cursor number of the first index */
111321 int nIdx; /* Number of indices */
111322 sqlite3 *db; /* Main database structure */
111323 AuthContext sContext; /* Authorization context */
111324 NameContext sNC; /* Name context to resolve expressions in */
111325 int iDb; /* Database number */
111326 int memCnt = 0; /* Memory cell used for change counting */
111327 int rcauth; /* Value returned by authorization callback */
111328 int eOnePass; /* ONEPASS_OFF or _SINGLE or _MULTI */
111329 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
111330 u8 *aToOpen = 0; /* Open cursor iTabCur+j if aToOpen[j] is true */
111331 Index *pPk; /* The PRIMARY KEY index on the table */
111332 int iPk = 0; /* First of nPk registers holding PRIMARY KEY value */
111333 i16 nPk = 1; /* Number of columns in the PRIMARY KEY */
111334 int iKey; /* Memory cell holding key of row to be deleted */
111335 i16 nKey; /* Number of memory cells in the row key */
111336 int iEphCur = 0; /* Ephemeral table holding all primary key values */
111337 int iRowSet = 0; /* Register for rowset of rows to delete */
111338 int addrBypass = 0; /* Address of jump over the delete logic */
111339 int addrLoop = 0; /* Top of the delete loop */
111340 int addrEphOpen = 0; /* Instruction to open the Ephemeral table */
111341 int bComplex; /* True if there are triggers or FKs or
111342 ** subqueries in the WHERE clause */
111343
111344#ifndef SQLITE_OMIT_TRIGGER
111345 int isView; /* True if attempting to delete from a view */
111346 Trigger *pTrigger; /* List of table triggers, if required */
111347#endif
111348
111349 memset(&sContext, 0, sizeof(sContext));
111350 db = pParse->db;
111351 if( pParse->nErr || db->mallocFailed ){
111352 goto delete_from_cleanup;
111353 }
111354 assert( pTabList->nSrc==1 );
111355
111356
111357 /* Locate the table which we want to delete. This table has to be
111358 ** put in an SrcList structure because some of the subroutines we
111359 ** will be calling are designed to work with multiple tables and expect
111360 ** an SrcList* parameter instead of just a Table* parameter.
111361 */
111362 pTab = sqlite3SrcListLookup(pParse, pTabList);
111363 if( pTab==0 ) goto delete_from_cleanup;
111364
111365 /* Figure out if we have any triggers and if the table being
111366 ** deleted from is a view
111367 */
111368#ifndef SQLITE_OMIT_TRIGGER
111369 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
111370 isView = pTab->pSelect!=0;
111371#else
111372# define pTrigger 0
111373# define isView 0
111374#endif
111375 bComplex = pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0);
111376#ifdef SQLITE_OMIT_VIEW
111377# undef isView
111378# define isView 0
111379#endif
111380
111381#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
111382 if( !isView ){
111383 pWhere = sqlite3LimitWhere(
111384 pParse, pTabList, pWhere, pOrderBy, pLimit, "DELETE"
111385 );
111386 pOrderBy = 0;
111387 pLimit = 0;
111388 }
111389#endif
111390
111391 /* If pTab is really a view, make sure it has been initialized.
111392 */
111393 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
111394 goto delete_from_cleanup;
111395 }
111396
111397 if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
111398 goto delete_from_cleanup;
111399 }
111400 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
111401 assert( iDb<db->nDb );
111402 rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0,
111403 db->aDb[iDb].zDbSName);
111404 assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
111405 if( rcauth==SQLITE_DENY ){
111406 goto delete_from_cleanup;
111407 }
111408 assert(!isView || pTrigger);
111409
111410 /* Assign cursor numbers to the table and all its indices.
111411 */
111412 assert( pTabList->nSrc==1 );
111413 iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
111414 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
111415 pParse->nTab++;
111416 }
111417
111418 /* Start the view context
111419 */
111420 if( isView ){
111421 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
111422 }
111423
111424 /* Begin generating code.
111425 */
111426 v = sqlite3GetVdbe(pParse);
111427 if( v==0 ){
111428 goto delete_from_cleanup;
111429 }
111430 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
111431 sqlite3BeginWriteOperation(pParse, bComplex, iDb);
111432
111433 /* If we are trying to delete from a view, realize that view into
111434 ** an ephemeral table.
111435 */
111436#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
111437 if( isView ){
111438 sqlite3MaterializeView(pParse, pTab,
111439 pWhere, pOrderBy, pLimit, iTabCur
111440 );
111441 iDataCur = iIdxCur = iTabCur;
111442 pOrderBy = 0;
111443 pLimit = 0;
111444 }
111445#endif
111446
111447 /* Resolve the column names in the WHERE clause.
111448 */
111449 memset(&sNC, 0, sizeof(sNC));
111450 sNC.pParse = pParse;
111451 sNC.pSrcList = pTabList;
111452 if( sqlite3ResolveExprNames(&sNC, pWhere) ){
111453 goto delete_from_cleanup;
111454 }
111455
111456 /* Initialize the counter of the number of rows deleted, if
111457 ** we are counting rows.
111458 */
111459 if( (db->flags & SQLITE_CountRows)!=0
111460 && !pParse->nested
111461 && !pParse->pTriggerTab
111462 ){
111463 memCnt = ++pParse->nMem;
111464 sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt);
111465 }
111466
111467#ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
111468 /* Special case: A DELETE without a WHERE clause deletes everything.
111469 ** It is easier just to erase the whole table. Prior to version 3.6.5,
111470 ** this optimization caused the row change count (the value returned by
111471 ** API function sqlite3_count_changes) to be set incorrectly.
111472 **
111473 ** The "rcauth==SQLITE_OK" terms is the
111474 ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and
111475 ** the callback returns SQLITE_IGNORE then the DELETE operation proceeds but
111476 ** the truncate optimization is disabled and all rows are deleted
111477 ** individually.
111478 */
111479 if( rcauth==SQLITE_OK
111480 && pWhere==0
111481 && !bComplex
111482 && !IsVirtual(pTab)
111483#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
111484 && db->xPreUpdateCallback==0
111485#endif
111486 ){
111487 assert( !isView );
111488 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
111489 if( HasRowid(pTab) ){
111490 sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt ? memCnt : -1,
111491 pTab->zName, P4_STATIC);
111492 }
111493 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
111494 assert( pIdx->pSchema==pTab->pSchema );
111495 sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
111496 }
111497 }else
111498#endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
111499 {
111500 u16 wcf = WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK|WHERE_SEEK_TABLE;
111501 if( sNC.ncFlags & NC_VarSelect ) bComplex = 1;
111502 wcf |= (bComplex ? 0 : WHERE_ONEPASS_MULTIROW);
111503 if( HasRowid(pTab) ){
111504 /* For a rowid table, initialize the RowSet to an empty set */
111505 pPk = 0;
111506 nPk = 1;
111507 iRowSet = ++pParse->nMem;
111508 sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
111509 }else{
111510 /* For a WITHOUT ROWID table, create an ephemeral table used to
111511 ** hold all primary keys for rows to be deleted. */
111512 pPk = sqlite3PrimaryKeyIndex(pTab);
111513 assert( pPk!=0 );
111514 nPk = pPk->nKeyCol;
111515 iPk = pParse->nMem+1;
111516 pParse->nMem += nPk;
111517 iEphCur = pParse->nTab++;
111518 addrEphOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEphCur, nPk);
111519 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
111520 }
111521
111522 /* Construct a query to find the rowid or primary key for every row
111523 ** to be deleted, based on the WHERE clause. Set variable eOnePass
111524 ** to indicate the strategy used to implement this delete:
111525 **
111526 ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values.
111527 ** ONEPASS_SINGLE: One-pass approach - at most one row deleted.
111528 ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted.
111529 */
111530 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, wcf, iTabCur+1);
111531 if( pWInfo==0 ) goto delete_from_cleanup;
111532 eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
111533 assert( IsVirtual(pTab)==0 || eOnePass!=ONEPASS_MULTI );
111534 assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF );
111535 if( eOnePass!=ONEPASS_SINGLE ) sqlite3MultiWrite(pParse);
111536
111537 /* Keep track of the number of rows to be deleted */
111538 if( memCnt ){
111539 sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
111540 }
111541
111542 /* Extract the rowid or primary key for the current row */
111543 if( pPk ){
111544 for(i=0; i<nPk; i++){
111545 assert( pPk->aiColumn[i]>=0 );
111546 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
111547 pPk->aiColumn[i], iPk+i);
111548 }
111549 iKey = iPk;
111550 }else{
111551 iKey = ++pParse->nMem;
111552 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, -1, iKey);
111553 }
111554
111555 if( eOnePass!=ONEPASS_OFF ){
111556 /* For ONEPASS, no need to store the rowid/primary-key. There is only
111557 ** one, so just keep it in its register(s) and fall through to the
111558 ** delete code. */
111559 nKey = nPk; /* OP_Found will use an unpacked key */
111560 aToOpen = sqlite3DbMallocRawNN(db, nIdx+2);
111561 if( aToOpen==0 ){
111562 sqlite3WhereEnd(pWInfo);
111563 goto delete_from_cleanup;
111564 }
111565 memset(aToOpen, 1, nIdx+1);
111566 aToOpen[nIdx+1] = 0;
111567 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
111568 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
111569 if( addrEphOpen ) sqlite3VdbeChangeToNoop(v, addrEphOpen);
111570 }else{
111571 if( pPk ){
111572 /* Add the PK key for this row to the temporary table */
111573 iKey = ++pParse->nMem;
111574 nKey = 0; /* Zero tells OP_Found to use a composite key */
111575 sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey,
111576 sqlite3IndexAffinityStr(pParse->db, pPk), nPk);
111577 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEphCur, iKey, iPk, nPk);
111578 }else{
111579 /* Add the rowid of the row to be deleted to the RowSet */
111580 nKey = 1; /* OP_DeferredSeek always uses a single rowid */
111581 sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, iKey);
111582 }
111583 }
111584
111585 /* If this DELETE cannot use the ONEPASS strategy, this is the
111586 ** end of the WHERE loop */
111587 if( eOnePass!=ONEPASS_OFF ){
111588 addrBypass = sqlite3VdbeMakeLabel(v);
111589 }else{
111590 sqlite3WhereEnd(pWInfo);
111591 }
111592
111593 /* Unless this is a view, open cursors for the table we are
111594 ** deleting from and all its indices. If this is a view, then the
111595 ** only effect this statement has is to fire the INSTEAD OF
111596 ** triggers.
111597 */
111598 if( !isView ){
111599 int iAddrOnce = 0;
111600 if( eOnePass==ONEPASS_MULTI ){
111601 iAddrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
111602 }
111603 testcase( IsVirtual(pTab) );
111604 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, OPFLAG_FORDELETE,
111605 iTabCur, aToOpen, &iDataCur, &iIdxCur);
111606 assert( pPk || IsVirtual(pTab) || iDataCur==iTabCur );
111607 assert( pPk || IsVirtual(pTab) || iIdxCur==iDataCur+1 );
111608 if( eOnePass==ONEPASS_MULTI ) sqlite3VdbeJumpHere(v, iAddrOnce);
111609 }
111610
111611 /* Set up a loop over the rowids/primary-keys that were found in the
111612 ** where-clause loop above.
111613 */
111614 if( eOnePass!=ONEPASS_OFF ){
111615 assert( nKey==nPk ); /* OP_Found will use an unpacked key */
111616 if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){
111617 assert( pPk!=0 || pTab->pSelect!=0 );
111618 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, addrBypass, iKey, nKey);
111619 VdbeCoverage(v);
111620 }
111621 }else if( pPk ){
111622 addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind, iEphCur); VdbeCoverage(v);
111623 if( IsVirtual(pTab) ){
111624 sqlite3VdbeAddOp3(v, OP_Column, iEphCur, 0, iKey);
111625 }else{
111626 sqlite3VdbeAddOp2(v, OP_RowData, iEphCur, iKey);
111627 }
111628 assert( nKey==0 ); /* OP_Found will use a composite key */
111629 }else{
111630 addrLoop = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, 0, iKey);
111631 VdbeCoverage(v);
111632 assert( nKey==1 );
111633 }
111634
111635 /* Delete the row */
111636#ifndef SQLITE_OMIT_VIRTUALTABLE
111637 if( IsVirtual(pTab) ){
111638 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
111639 sqlite3VtabMakeWritable(pParse, pTab);
111640 assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
111641 sqlite3MayAbort(pParse);
111642 if( eOnePass==ONEPASS_SINGLE ){
111643 sqlite3VdbeAddOp1(v, OP_Close, iTabCur);
111644 if( sqlite3IsToplevel(pParse) ){
111645 pParse->isMultiWrite = 0;
111646 }
111647 }
111648 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB);
111649 sqlite3VdbeChangeP5(v, OE_Abort);
111650 }else
111651#endif
111652 {
111653 int count = (pParse->nested==0); /* True to count changes */
111654 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
111655 iKey, nKey, count, OE_Default, eOnePass, aiCurOnePass[1]);
111656 }
111657
111658 /* End of the loop over all rowids/primary-keys. */
111659 if( eOnePass!=ONEPASS_OFF ){
111660 sqlite3VdbeResolveLabel(v, addrBypass);
111661 sqlite3WhereEnd(pWInfo);
111662 }else if( pPk ){
111663 sqlite3VdbeAddOp2(v, OP_Next, iEphCur, addrLoop+1); VdbeCoverage(v);
111664 sqlite3VdbeJumpHere(v, addrLoop);
111665 }else{
111666 sqlite3VdbeGoto(v, addrLoop);
111667 sqlite3VdbeJumpHere(v, addrLoop);
111668 }
111669 } /* End non-truncate path */
111670
111671 /* Update the sqlite_sequence table by storing the content of the
111672 ** maximum rowid counter values recorded while inserting into
111673 ** autoincrement tables.
111674 */
111675 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
111676 sqlite3AutoincrementEnd(pParse);
111677 }
111678
111679 /* Return the number of rows that were deleted. If this routine is
111680 ** generating code because of a call to sqlite3NestedParse(), do not
111681 ** invoke the callback function.
111682 */
111683 if( memCnt ){
111684 sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1);
111685 sqlite3VdbeSetNumCols(v, 1);
111686 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC);
111687 }
111688
111689delete_from_cleanup:
111690 sqlite3AuthContextPop(&sContext);
111691 sqlite3SrcListDelete(db, pTabList);
111692 sqlite3ExprDelete(db, pWhere);
111693#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
111694 sqlite3ExprListDelete(db, pOrderBy);
111695 sqlite3ExprDelete(db, pLimit);
111696#endif
111697 sqlite3DbFree(db, aToOpen);
111698 return;
111699}
111700/* Make sure "isView" and other macros defined above are undefined. Otherwise
111701** they may interfere with compilation of other functions in this file
111702** (or in another file, if this file becomes part of the amalgamation). */
111703#ifdef isView
111704 #undef isView
111705#endif
111706#ifdef pTrigger
111707 #undef pTrigger
111708#endif
111709
111710/*
111711** This routine generates VDBE code that causes a single row of a
111712** single table to be deleted. Both the original table entry and
111713** all indices are removed.
111714**
111715** Preconditions:
111716**
111717** 1. iDataCur is an open cursor on the btree that is the canonical data
111718** store for the table. (This will be either the table itself,
111719** in the case of a rowid table, or the PRIMARY KEY index in the case
111720** of a WITHOUT ROWID table.)
111721**
111722** 2. Read/write cursors for all indices of pTab must be open as
111723** cursor number iIdxCur+i for the i-th index.
111724**
111725** 3. The primary key for the row to be deleted must be stored in a
111726** sequence of nPk memory cells starting at iPk. If nPk==0 that means
111727** that a search record formed from OP_MakeRecord is contained in the
111728** single memory location iPk.
111729**
111730** eMode:
111731** Parameter eMode may be passed either ONEPASS_OFF (0), ONEPASS_SINGLE, or
111732** ONEPASS_MULTI. If eMode is not ONEPASS_OFF, then the cursor
111733** iDataCur already points to the row to delete. If eMode is ONEPASS_OFF
111734** then this function must seek iDataCur to the entry identified by iPk
111735** and nPk before reading from it.
111736**
111737** If eMode is ONEPASS_MULTI, then this call is being made as part
111738** of a ONEPASS delete that affects multiple rows. In this case, if
111739** iIdxNoSeek is a valid cursor number (>=0) and is not the same as
111740** iDataCur, then its position should be preserved following the delete
111741** operation. Or, if iIdxNoSeek is not a valid cursor number, the
111742** position of iDataCur should be preserved instead.
111743**
111744** iIdxNoSeek:
111745** If iIdxNoSeek is a valid cursor number (>=0) not equal to iDataCur,
111746** then it identifies an index cursor (from within array of cursors
111747** starting at iIdxCur) that already points to the index entry to be deleted.
111748** Except, this optimization is disabled if there are BEFORE triggers since
111749** the trigger body might have moved the cursor.
111750*/
111751SQLITE_PRIVATE void sqlite3GenerateRowDelete(
111752 Parse *pParse, /* Parsing context */
111753 Table *pTab, /* Table containing the row to be deleted */
111754 Trigger *pTrigger, /* List of triggers to (potentially) fire */
111755 int iDataCur, /* Cursor from which column data is extracted */
111756 int iIdxCur, /* First index cursor */
111757 int iPk, /* First memory cell containing the PRIMARY KEY */
111758 i16 nPk, /* Number of PRIMARY KEY memory cells */
111759 u8 count, /* If non-zero, increment the row change counter */
111760 u8 onconf, /* Default ON CONFLICT policy for triggers */
111761 u8 eMode, /* ONEPASS_OFF, _SINGLE, or _MULTI. See above */
111762 int iIdxNoSeek /* Cursor number of cursor that does not need seeking */
111763){
111764 Vdbe *v = pParse->pVdbe; /* Vdbe */
111765 int iOld = 0; /* First register in OLD.* array */
111766 int iLabel; /* Label resolved to end of generated code */
111767 u8 opSeek; /* Seek opcode */
111768
111769 /* Vdbe is guaranteed to have been allocated by this stage. */
111770 assert( v );
111771 VdbeModuleComment((v, "BEGIN: GenRowDel(%d,%d,%d,%d)",
111772 iDataCur, iIdxCur, iPk, (int)nPk));
111773
111774 /* Seek cursor iCur to the row to delete. If this row no longer exists
111775 ** (this can happen if a trigger program has already deleted it), do
111776 ** not attempt to delete it or fire any DELETE triggers. */
111777 iLabel = sqlite3VdbeMakeLabel(v);
111778 opSeek = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
111779 if( eMode==ONEPASS_OFF ){
111780 sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
111781 VdbeCoverageIf(v, opSeek==OP_NotExists);
111782 VdbeCoverageIf(v, opSeek==OP_NotFound);
111783 }
111784
111785 /* If there are any triggers to fire, allocate a range of registers to
111786 ** use for the old.* references in the triggers. */
111787 if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
111788 u32 mask; /* Mask of OLD.* columns in use */
111789 int iCol; /* Iterator used while populating OLD.* */
111790 int addrStart; /* Start of BEFORE trigger programs */
111791
111792 /* TODO: Could use temporary registers here. Also could attempt to
111793 ** avoid copying the contents of the rowid register. */
111794 mask = sqlite3TriggerColmask(
111795 pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf
111796 );
111797 mask |= sqlite3FkOldmask(pParse, pTab);
111798 iOld = pParse->nMem+1;
111799 pParse->nMem += (1 + pTab->nCol);
111800
111801 /* Populate the OLD.* pseudo-table register array. These values will be
111802 ** used by any BEFORE and AFTER triggers that exist. */
111803 sqlite3VdbeAddOp2(v, OP_Copy, iPk, iOld);
111804 for(iCol=0; iCol<pTab->nCol; iCol++){
111805 testcase( mask!=0xffffffff && iCol==31 );
111806 testcase( mask!=0xffffffff && iCol==32 );
111807 if( mask==0xffffffff || (iCol<=31 && (mask & MASKBIT32(iCol))!=0) ){
111808 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, iCol, iOld+iCol+1);
111809 }
111810 }
111811
111812 /* Invoke BEFORE DELETE trigger programs. */
111813 addrStart = sqlite3VdbeCurrentAddr(v);
111814 sqlite3CodeRowTrigger(pParse, pTrigger,
111815 TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
111816 );
111817
111818 /* If any BEFORE triggers were coded, then seek the cursor to the
111819 ** row to be deleted again. It may be that the BEFORE triggers moved
111820 ** the cursor or already deleted the row that the cursor was
111821 ** pointing to.
111822 **
111823 ** Also disable the iIdxNoSeek optimization since the BEFORE trigger
111824 ** may have moved that cursor.
111825 */
111826 if( addrStart<sqlite3VdbeCurrentAddr(v) ){
111827 sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
111828 VdbeCoverageIf(v, opSeek==OP_NotExists);
111829 VdbeCoverageIf(v, opSeek==OP_NotFound);
111830 testcase( iIdxNoSeek>=0 );
111831 iIdxNoSeek = -1;
111832 }
111833
111834 /* Do FK processing. This call checks that any FK constraints that
111835 ** refer to this table (i.e. constraints attached to other tables)
111836 ** are not violated by deleting this row. */
111837 sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
111838 }
111839
111840 /* Delete the index and table entries. Skip this step if pTab is really
111841 ** a view (in which case the only effect of the DELETE statement is to
111842 ** fire the INSTEAD OF triggers).
111843 **
111844 ** If variable 'count' is non-zero, then this OP_Delete instruction should
111845 ** invoke the update-hook. The pre-update-hook, on the other hand should
111846 ** be invoked unless table pTab is a system table. The difference is that
111847 ** the update-hook is not invoked for rows removed by REPLACE, but the
111848 ** pre-update-hook is.
111849 */
111850 if( pTab->pSelect==0 ){
111851 u8 p5 = 0;
111852 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
111853 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
111854 if( pParse->nested==0 || 0==sqlite3_stricmp(pTab->zName, "sqlite_stat1") ){
111855 sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE);
111856 }
111857 if( eMode!=ONEPASS_OFF ){
111858 sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE);
111859 }
111860 if( iIdxNoSeek>=0 && iIdxNoSeek!=iDataCur ){
111861 sqlite3VdbeAddOp1(v, OP_Delete, iIdxNoSeek);
111862 }
111863 if( eMode==ONEPASS_MULTI ) p5 |= OPFLAG_SAVEPOSITION;
111864 sqlite3VdbeChangeP5(v, p5);
111865 }
111866
111867 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
111868 ** handle rows (possibly in other tables) that refer via a foreign key
111869 ** to the row just deleted. */
111870 sqlite3FkActions(pParse, pTab, 0, iOld, 0, 0);
111871
111872 /* Invoke AFTER DELETE trigger programs. */
111873 sqlite3CodeRowTrigger(pParse, pTrigger,
111874 TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel
111875 );
111876
111877 /* Jump here if the row had already been deleted before any BEFORE
111878 ** trigger programs were invoked. Or if a trigger program throws a
111879 ** RAISE(IGNORE) exception. */
111880 sqlite3VdbeResolveLabel(v, iLabel);
111881 VdbeModuleComment((v, "END: GenRowDel()"));
111882}
111883
111884/*
111885** This routine generates VDBE code that causes the deletion of all
111886** index entries associated with a single row of a single table, pTab
111887**
111888** Preconditions:
111889**
111890** 1. A read/write cursor "iDataCur" must be open on the canonical storage
111891** btree for the table pTab. (This will be either the table itself
111892** for rowid tables or to the primary key index for WITHOUT ROWID
111893** tables.)
111894**
111895** 2. Read/write cursors for all indices of pTab must be open as
111896** cursor number iIdxCur+i for the i-th index. (The pTab->pIndex
111897** index is the 0-th index.)
111898**
111899** 3. The "iDataCur" cursor must be already be positioned on the row
111900** that is to be deleted.
111901*/
111902SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(
111903 Parse *pParse, /* Parsing and code generating context */
111904 Table *pTab, /* Table containing the row to be deleted */
111905 int iDataCur, /* Cursor of table holding data. */
111906 int iIdxCur, /* First index cursor */
111907 int *aRegIdx, /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
111908 int iIdxNoSeek /* Do not delete from this cursor */
111909){
111910 int i; /* Index loop counter */
111911 int r1 = -1; /* Register holding an index key */
111912 int iPartIdxLabel; /* Jump destination for skipping partial index entries */
111913 Index *pIdx; /* Current index */
111914 Index *pPrior = 0; /* Prior index */
111915 Vdbe *v; /* The prepared statement under construction */
111916 Index *pPk; /* PRIMARY KEY index, or NULL for rowid tables */
111917
111918 v = pParse->pVdbe;
111919 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
111920 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
111921 assert( iIdxCur+i!=iDataCur || pPk==pIdx );
111922 if( aRegIdx!=0 && aRegIdx[i]==0 ) continue;
111923 if( pIdx==pPk ) continue;
111924 if( iIdxCur+i==iIdxNoSeek ) continue;
111925 VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
111926 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1,
111927 &iPartIdxLabel, pPrior, r1);
111928 sqlite3VdbeAddOp3(v, OP_IdxDelete, iIdxCur+i, r1,
111929 pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
111930 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
111931 pPrior = pIdx;
111932 }
111933}
111934
111935/*
111936** Generate code that will assemble an index key and stores it in register
111937** regOut. The key with be for index pIdx which is an index on pTab.
111938** iCur is the index of a cursor open on the pTab table and pointing to
111939** the entry that needs indexing. If pTab is a WITHOUT ROWID table, then
111940** iCur must be the cursor of the PRIMARY KEY index.
111941**
111942** Return a register number which is the first in a block of
111943** registers that holds the elements of the index key. The
111944** block of registers has already been deallocated by the time
111945** this routine returns.
111946**
111947** If *piPartIdxLabel is not NULL, fill it in with a label and jump
111948** to that label if pIdx is a partial index that should be skipped.
111949** The label should be resolved using sqlite3ResolvePartIdxLabel().
111950** A partial index should be skipped if its WHERE clause evaluates
111951** to false or null. If pIdx is not a partial index, *piPartIdxLabel
111952** will be set to zero which is an empty label that is ignored by
111953** sqlite3ResolvePartIdxLabel().
111954**
111955** The pPrior and regPrior parameters are used to implement a cache to
111956** avoid unnecessary register loads. If pPrior is not NULL, then it is
111957** a pointer to a different index for which an index key has just been
111958** computed into register regPrior. If the current pIdx index is generating
111959** its key into the same sequence of registers and if pPrior and pIdx share
111960** a column in common, then the register corresponding to that column already
111961** holds the correct value and the loading of that register is skipped.
111962** This optimization is helpful when doing a DELETE or an INTEGRITY_CHECK
111963** on a table with multiple indices, and especially with the ROWID or
111964** PRIMARY KEY columns of the index.
111965*/
111966SQLITE_PRIVATE int sqlite3GenerateIndexKey(
111967 Parse *pParse, /* Parsing context */
111968 Index *pIdx, /* The index for which to generate a key */
111969 int iDataCur, /* Cursor number from which to take column data */
111970 int regOut, /* Put the new key into this register if not 0 */
111971 int prefixOnly, /* Compute only a unique prefix of the key */
111972 int *piPartIdxLabel, /* OUT: Jump to this label to skip partial index */
111973 Index *pPrior, /* Previously generated index key */
111974 int regPrior /* Register holding previous generated key */
111975){
111976 Vdbe *v = pParse->pVdbe;
111977 int j;
111978 int regBase;
111979 int nCol;
111980
111981 if( piPartIdxLabel ){
111982 if( pIdx->pPartIdxWhere ){
111983 *piPartIdxLabel = sqlite3VdbeMakeLabel(v);
111984 pParse->iSelfTab = iDataCur + 1;
111985 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
111986 SQLITE_JUMPIFNULL);
111987 pParse->iSelfTab = 0;
111988 }else{
111989 *piPartIdxLabel = 0;
111990 }
111991 }
111992 nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
111993 regBase = sqlite3GetTempRange(pParse, nCol);
111994 if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
111995 for(j=0; j<nCol; j++){
111996 if( pPrior
111997 && pPrior->aiColumn[j]==pIdx->aiColumn[j]
111998 && pPrior->aiColumn[j]!=XN_EXPR
111999 ){
112000 /* This column was already computed by the previous index */
112001 continue;
112002 }
112003 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iDataCur, j, regBase+j);
112004 /* If the column affinity is REAL but the number is an integer, then it
112005 ** might be stored in the table as an integer (using a compact
112006 ** representation) then converted to REAL by an OP_RealAffinity opcode.
112007 ** But we are getting ready to store this value back into an index, where
112008 ** it should be converted by to INTEGER again. So omit the OP_RealAffinity
112009 ** opcode if it is present */
112010 sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity);
112011 }
112012 if( regOut ){
112013 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regOut);
112014 if( pIdx->pTable->pSelect ){
112015 const char *zAff = sqlite3IndexAffinityStr(pParse->db, pIdx);
112016 sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT);
112017 }
112018 }
112019 sqlite3ReleaseTempRange(pParse, regBase, nCol);
112020 return regBase;
112021}
112022
112023/*
112024** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
112025** because it was a partial index, then this routine should be called to
112026** resolve that label.
112027*/
112028SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse *pParse, int iLabel){
112029 if( iLabel ){
112030 sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
112031 }
112032}
112033
112034/************** End of delete.c **********************************************/
112035/************** Begin file func.c ********************************************/
112036/*
112037** 2002 February 23
112038**
112039** The author disclaims copyright to this source code. In place of
112040** a legal notice, here is a blessing:
112041**
112042** May you do good and not evil.
112043** May you find forgiveness for yourself and forgive others.
112044** May you share freely, never taking more than you give.
112045**
112046*************************************************************************
112047** This file contains the C-language implementations for many of the SQL
112048** functions of SQLite. (Some function, and in particular the date and
112049** time functions, are implemented separately.)
112050*/
112051/* #include "sqliteInt.h" */
112052/* #include <stdlib.h> */
112053/* #include <assert.h> */
112054/* #include "vdbeInt.h" */
112055
112056/*
112057** Return the collating function associated with a function.
112058*/
112059static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
112060 VdbeOp *pOp;
112061 assert( context->pVdbe!=0 );
112062 pOp = &context->pVdbe->aOp[context->iOp-1];
112063 assert( pOp->opcode==OP_CollSeq );
112064 assert( pOp->p4type==P4_COLLSEQ );
112065 return pOp->p4.pColl;
112066}
112067
112068/*
112069** Indicate that the accumulator load should be skipped on this
112070** iteration of the aggregate loop.
112071*/
112072static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
112073 assert( context->isError<=0 );
112074 context->isError = -1;
112075 context->skipFlag = 1;
112076}
112077
112078/*
112079** Implementation of the non-aggregate min() and max() functions
112080*/
112081static void minmaxFunc(
112082 sqlite3_context *context,
112083 int argc,
112084 sqlite3_value **argv
112085){
112086 int i;
112087 int mask; /* 0 for min() or 0xffffffff for max() */
112088 int iBest;
112089 CollSeq *pColl;
112090
112091 assert( argc>1 );
112092 mask = sqlite3_user_data(context)==0 ? 0 : -1;
112093 pColl = sqlite3GetFuncCollSeq(context);
112094 assert( pColl );
112095 assert( mask==-1 || mask==0 );
112096 iBest = 0;
112097 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
112098 for(i=1; i<argc; i++){
112099 if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
112100 if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
112101 testcase( mask==0 );
112102 iBest = i;
112103 }
112104 }
112105 sqlite3_result_value(context, argv[iBest]);
112106}
112107
112108/*
112109** Return the type of the argument.
112110*/
112111static void typeofFunc(
112112 sqlite3_context *context,
112113 int NotUsed,
112114 sqlite3_value **argv
112115){
112116 static const char *azType[] = { "integer", "real", "text", "blob", "null" };
112117 int i = sqlite3_value_type(argv[0]) - 1;
112118 UNUSED_PARAMETER(NotUsed);
112119 assert( i>=0 && i<ArraySize(azType) );
112120 assert( SQLITE_INTEGER==1 );
112121 assert( SQLITE_FLOAT==2 );
112122 assert( SQLITE_TEXT==3 );
112123 assert( SQLITE_BLOB==4 );
112124 assert( SQLITE_NULL==5 );
112125 /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns
112126 ** the datatype code for the initial datatype of the sqlite3_value object
112127 ** V. The returned value is one of SQLITE_INTEGER, SQLITE_FLOAT,
112128 ** SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL. */
112129 sqlite3_result_text(context, azType[i], -1, SQLITE_STATIC);
112130}
112131
112132
112133/*
112134** Implementation of the length() function
112135*/
112136static void lengthFunc(
112137 sqlite3_context *context,
112138 int argc,
112139 sqlite3_value **argv
112140){
112141 assert( argc==1 );
112142 UNUSED_PARAMETER(argc);
112143 switch( sqlite3_value_type(argv[0]) ){
112144 case SQLITE_BLOB:
112145 case SQLITE_INTEGER:
112146 case SQLITE_FLOAT: {
112147 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
112148 break;
112149 }
112150 case SQLITE_TEXT: {
112151 const unsigned char *z = sqlite3_value_text(argv[0]);
112152 const unsigned char *z0;
112153 unsigned char c;
112154 if( z==0 ) return;
112155 z0 = z;
112156 while( (c = *z)!=0 ){
112157 z++;
112158 if( c>=0xc0 ){
112159 while( (*z & 0xc0)==0x80 ){ z++; z0++; }
112160 }
112161 }
112162 sqlite3_result_int(context, (int)(z-z0));
112163 break;
112164 }
112165 default: {
112166 sqlite3_result_null(context);
112167 break;
112168 }
112169 }
112170}
112171
112172/*
112173** Implementation of the abs() function.
112174**
112175** IMP: R-23979-26855 The abs(X) function returns the absolute value of
112176** the numeric argument X.
112177*/
112178static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
112179 assert( argc==1 );
112180 UNUSED_PARAMETER(argc);
112181 switch( sqlite3_value_type(argv[0]) ){
112182 case SQLITE_INTEGER: {
112183 i64 iVal = sqlite3_value_int64(argv[0]);
112184 if( iVal<0 ){
112185 if( iVal==SMALLEST_INT64 ){
112186 /* IMP: R-31676-45509 If X is the integer -9223372036854775808
112187 ** then abs(X) throws an integer overflow error since there is no
112188 ** equivalent positive 64-bit two complement value. */
112189 sqlite3_result_error(context, "integer overflow", -1);
112190 return;
112191 }
112192 iVal = -iVal;
112193 }
112194 sqlite3_result_int64(context, iVal);
112195 break;
112196 }
112197 case SQLITE_NULL: {
112198 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
112199 sqlite3_result_null(context);
112200 break;
112201 }
112202 default: {
112203 /* Because sqlite3_value_double() returns 0.0 if the argument is not
112204 ** something that can be converted into a number, we have:
112205 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
112206 ** that cannot be converted to a numeric value.
112207 */
112208 double rVal = sqlite3_value_double(argv[0]);
112209 if( rVal<0 ) rVal = -rVal;
112210 sqlite3_result_double(context, rVal);
112211 break;
112212 }
112213 }
112214}
112215
112216/*
112217** Implementation of the instr() function.
112218**
112219** instr(haystack,needle) finds the first occurrence of needle
112220** in haystack and returns the number of previous characters plus 1,
112221** or 0 if needle does not occur within haystack.
112222**
112223** If both haystack and needle are BLOBs, then the result is one more than
112224** the number of bytes in haystack prior to the first occurrence of needle,
112225** or 0 if needle never occurs in haystack.
112226*/
112227static void instrFunc(
112228 sqlite3_context *context,
112229 int argc,
112230 sqlite3_value **argv
112231){
112232 const unsigned char *zHaystack;
112233 const unsigned char *zNeedle;
112234 int nHaystack;
112235 int nNeedle;
112236 int typeHaystack, typeNeedle;
112237 int N = 1;
112238 int isText;
112239
112240 UNUSED_PARAMETER(argc);
112241 typeHaystack = sqlite3_value_type(argv[0]);
112242 typeNeedle = sqlite3_value_type(argv[1]);
112243 if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return;
112244 nHaystack = sqlite3_value_bytes(argv[0]);
112245 nNeedle = sqlite3_value_bytes(argv[1]);
112246 if( nNeedle>0 ){
112247 if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
112248 zHaystack = sqlite3_value_blob(argv[0]);
112249 zNeedle = sqlite3_value_blob(argv[1]);
112250 isText = 0;
112251 }else{
112252 zHaystack = sqlite3_value_text(argv[0]);
112253 zNeedle = sqlite3_value_text(argv[1]);
112254 isText = 1;
112255 }
112256 if( zNeedle==0 || (nHaystack && zHaystack==0) ) return;
112257 while( nNeedle<=nHaystack && memcmp(zHaystack, zNeedle, nNeedle)!=0 ){
112258 N++;
112259 do{
112260 nHaystack--;
112261 zHaystack++;
112262 }while( isText && (zHaystack[0]&0xc0)==0x80 );
112263 }
112264 if( nNeedle>nHaystack ) N = 0;
112265 }
112266 sqlite3_result_int(context, N);
112267}
112268
112269/*
112270** Implementation of the printf() function.
112271*/
112272static void printfFunc(
112273 sqlite3_context *context,
112274 int argc,
112275 sqlite3_value **argv
112276){
112277 PrintfArguments x;
112278 StrAccum str;
112279 const char *zFormat;
112280 int n;
112281 sqlite3 *db = sqlite3_context_db_handle(context);
112282
112283 if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){
112284 x.nArg = argc-1;
112285 x.nUsed = 0;
112286 x.apArg = argv+1;
112287 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
112288 str.printfFlags = SQLITE_PRINTF_SQLFUNC;
112289 sqlite3_str_appendf(&str, zFormat, &x);
112290 n = str.nChar;
112291 sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n,
112292 SQLITE_DYNAMIC);
112293 }
112294}
112295
112296/*
112297** Implementation of the substr() function.
112298**
112299** substr(x,p1,p2) returns p2 characters of x[] beginning with p1.
112300** p1 is 1-indexed. So substr(x,1,1) returns the first character
112301** of x. If x is text, then we actually count UTF-8 characters.
112302** If x is a blob, then we count bytes.
112303**
112304** If p1 is negative, then we begin abs(p1) from the end of x[].
112305**
112306** If p2 is negative, return the p2 characters preceding p1.
112307*/
112308static void substrFunc(
112309 sqlite3_context *context,
112310 int argc,
112311 sqlite3_value **argv
112312){
112313 const unsigned char *z;
112314 const unsigned char *z2;
112315 int len;
112316 int p0type;
112317 i64 p1, p2;
112318 int negP2 = 0;
112319
112320 assert( argc==3 || argc==2 );
112321 if( sqlite3_value_type(argv[1])==SQLITE_NULL
112322 || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL)
112323 ){
112324 return;
112325 }
112326 p0type = sqlite3_value_type(argv[0]);
112327 p1 = sqlite3_value_int(argv[1]);
112328 if( p0type==SQLITE_BLOB ){
112329 len = sqlite3_value_bytes(argv[0]);
112330 z = sqlite3_value_blob(argv[0]);
112331 if( z==0 ) return;
112332 assert( len==sqlite3_value_bytes(argv[0]) );
112333 }else{
112334 z = sqlite3_value_text(argv[0]);
112335 if( z==0 ) return;
112336 len = 0;
112337 if( p1<0 ){
112338 for(z2=z; *z2; len++){
112339 SQLITE_SKIP_UTF8(z2);
112340 }
112341 }
112342 }
112343#ifdef SQLITE_SUBSTR_COMPATIBILITY
112344 /* If SUBSTR_COMPATIBILITY is defined then substr(X,0,N) work the same as
112345 ** as substr(X,1,N) - it returns the first N characters of X. This
112346 ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8]
112347 ** from 2009-02-02 for compatibility of applications that exploited the
112348 ** old buggy behavior. */
112349 if( p1==0 ) p1 = 1; /* <rdar://problem/6778339> */
112350#endif
112351 if( argc==3 ){
112352 p2 = sqlite3_value_int(argv[2]);
112353 if( p2<0 ){
112354 p2 = -p2;
112355 negP2 = 1;
112356 }
112357 }else{
112358 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
112359 }
112360 if( p1<0 ){
112361 p1 += len;
112362 if( p1<0 ){
112363 p2 += p1;
112364 if( p2<0 ) p2 = 0;
112365 p1 = 0;
112366 }
112367 }else if( p1>0 ){
112368 p1--;
112369 }else if( p2>0 ){
112370 p2--;
112371 }
112372 if( negP2 ){
112373 p1 -= p2;
112374 if( p1<0 ){
112375 p2 += p1;
112376 p1 = 0;
112377 }
112378 }
112379 assert( p1>=0 && p2>=0 );
112380 if( p0type!=SQLITE_BLOB ){
112381 while( *z && p1 ){
112382 SQLITE_SKIP_UTF8(z);
112383 p1--;
112384 }
112385 for(z2=z; *z2 && p2; p2--){
112386 SQLITE_SKIP_UTF8(z2);
112387 }
112388 sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT,
112389 SQLITE_UTF8);
112390 }else{
112391 if( p1+p2>len ){
112392 p2 = len-p1;
112393 if( p2<0 ) p2 = 0;
112394 }
112395 sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT);
112396 }
112397}
112398
112399/*
112400** Implementation of the round() function
112401*/
112402#ifndef SQLITE_OMIT_FLOATING_POINT
112403static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
112404 int n = 0;
112405 double r;
112406 char *zBuf;
112407 assert( argc==1 || argc==2 );
112408 if( argc==2 ){
112409 if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
112410 n = sqlite3_value_int(argv[1]);
112411 if( n>30 ) n = 30;
112412 if( n<0 ) n = 0;
112413 }
112414 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
112415 r = sqlite3_value_double(argv[0]);
112416 /* If Y==0 and X will fit in a 64-bit int,
112417 ** handle the rounding directly,
112418 ** otherwise use printf.
112419 */
112420 if( n==0 && r>=0 && r<LARGEST_INT64-1 ){
112421 r = (double)((sqlite_int64)(r+0.5));
112422 }else if( n==0 && r<0 && (-r)<LARGEST_INT64-1 ){
112423 r = -(double)((sqlite_int64)((-r)+0.5));
112424 }else{
112425 zBuf = sqlite3_mprintf("%.*f",n,r);
112426 if( zBuf==0 ){
112427 sqlite3_result_error_nomem(context);
112428 return;
112429 }
112430 sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
112431 sqlite3_free(zBuf);
112432 }
112433 sqlite3_result_double(context, r);
112434}
112435#endif
112436
112437/*
112438** Allocate nByte bytes of space using sqlite3Malloc(). If the
112439** allocation fails, call sqlite3_result_error_nomem() to notify
112440** the database handle that malloc() has failed and return NULL.
112441** If nByte is larger than the maximum string or blob length, then
112442** raise an SQLITE_TOOBIG exception and return NULL.
112443*/
112444static void *contextMalloc(sqlite3_context *context, i64 nByte){
112445 char *z;
112446 sqlite3 *db = sqlite3_context_db_handle(context);
112447 assert( nByte>0 );
112448 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
112449 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
112450 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
112451 sqlite3_result_error_toobig(context);
112452 z = 0;
112453 }else{
112454 z = sqlite3Malloc(nByte);
112455 if( !z ){
112456 sqlite3_result_error_nomem(context);
112457 }
112458 }
112459 return z;
112460}
112461
112462/*
112463** Implementation of the upper() and lower() SQL functions.
112464*/
112465static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
112466 char *z1;
112467 const char *z2;
112468 int i, n;
112469 UNUSED_PARAMETER(argc);
112470 z2 = (char*)sqlite3_value_text(argv[0]);
112471 n = sqlite3_value_bytes(argv[0]);
112472 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
112473 assert( z2==(char*)sqlite3_value_text(argv[0]) );
112474 if( z2 ){
112475 z1 = contextMalloc(context, ((i64)n)+1);
112476 if( z1 ){
112477 for(i=0; i<n; i++){
112478 z1[i] = (char)sqlite3Toupper(z2[i]);
112479 }
112480 sqlite3_result_text(context, z1, n, sqlite3_free);
112481 }
112482 }
112483}
112484static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
112485 char *z1;
112486 const char *z2;
112487 int i, n;
112488 UNUSED_PARAMETER(argc);
112489 z2 = (char*)sqlite3_value_text(argv[0]);
112490 n = sqlite3_value_bytes(argv[0]);
112491 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
112492 assert( z2==(char*)sqlite3_value_text(argv[0]) );
112493 if( z2 ){
112494 z1 = contextMalloc(context, ((i64)n)+1);
112495 if( z1 ){
112496 for(i=0; i<n; i++){
112497 z1[i] = sqlite3Tolower(z2[i]);
112498 }
112499 sqlite3_result_text(context, z1, n, sqlite3_free);
112500 }
112501 }
112502}
112503
112504/*
112505** Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented
112506** as VDBE code so that unused argument values do not have to be computed.
112507** However, we still need some kind of function implementation for this
112508** routines in the function table. The noopFunc macro provides this.
112509** noopFunc will never be called so it doesn't matter what the implementation
112510** is. We might as well use the "version()" function as a substitute.
112511*/
112512#define noopFunc versionFunc /* Substitute function - never called */
112513
112514/*
112515** Implementation of random(). Return a random integer.
112516*/
112517static void randomFunc(
112518 sqlite3_context *context,
112519 int NotUsed,
112520 sqlite3_value **NotUsed2
112521){
112522 sqlite_int64 r;
112523 UNUSED_PARAMETER2(NotUsed, NotUsed2);
112524 sqlite3_randomness(sizeof(r), &r);
112525 if( r<0 ){
112526 /* We need to prevent a random number of 0x8000000000000000
112527 ** (or -9223372036854775808) since when you do abs() of that
112528 ** number of you get the same value back again. To do this
112529 ** in a way that is testable, mask the sign bit off of negative
112530 ** values, resulting in a positive value. Then take the
112531 ** 2s complement of that positive value. The end result can
112532 ** therefore be no less than -9223372036854775807.
112533 */
112534 r = -(r & LARGEST_INT64);
112535 }
112536 sqlite3_result_int64(context, r);
112537}
112538
112539/*
112540** Implementation of randomblob(N). Return a random blob
112541** that is N bytes long.
112542*/
112543static void randomBlob(
112544 sqlite3_context *context,
112545 int argc,
112546 sqlite3_value **argv
112547){
112548 int n;
112549 unsigned char *p;
112550 assert( argc==1 );
112551 UNUSED_PARAMETER(argc);
112552 n = sqlite3_value_int(argv[0]);
112553 if( n<1 ){
112554 n = 1;
112555 }
112556 p = contextMalloc(context, n);
112557 if( p ){
112558 sqlite3_randomness(n, p);
112559 sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
112560 }
112561}
112562
112563/*
112564** Implementation of the last_insert_rowid() SQL function. The return
112565** value is the same as the sqlite3_last_insert_rowid() API function.
112566*/
112567static void last_insert_rowid(
112568 sqlite3_context *context,
112569 int NotUsed,
112570 sqlite3_value **NotUsed2
112571){
112572 sqlite3 *db = sqlite3_context_db_handle(context);
112573 UNUSED_PARAMETER2(NotUsed, NotUsed2);
112574 /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
112575 ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface
112576 ** function. */
112577 sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
112578}
112579
112580/*
112581** Implementation of the changes() SQL function.
112582**
112583** IMP: R-62073-11209 The changes() SQL function is a wrapper
112584** around the sqlite3_changes() C/C++ function and hence follows the same
112585** rules for counting changes.
112586*/
112587static void changes(
112588 sqlite3_context *context,
112589 int NotUsed,
112590 sqlite3_value **NotUsed2
112591){
112592 sqlite3 *db = sqlite3_context_db_handle(context);
112593 UNUSED_PARAMETER2(NotUsed, NotUsed2);
112594 sqlite3_result_int(context, sqlite3_changes(db));
112595}
112596
112597/*
112598** Implementation of the total_changes() SQL function. The return value is
112599** the same as the sqlite3_total_changes() API function.
112600*/
112601static void total_changes(
112602 sqlite3_context *context,
112603 int NotUsed,
112604 sqlite3_value **NotUsed2
112605){
112606 sqlite3 *db = sqlite3_context_db_handle(context);
112607 UNUSED_PARAMETER2(NotUsed, NotUsed2);
112608 /* IMP: R-52756-41993 This function is a wrapper around the
112609 ** sqlite3_total_changes() C/C++ interface. */
112610 sqlite3_result_int(context, sqlite3_total_changes(db));
112611}
112612
112613/*
112614** A structure defining how to do GLOB-style comparisons.
112615*/
112616struct compareInfo {
112617 u8 matchAll; /* "*" or "%" */
112618 u8 matchOne; /* "?" or "_" */
112619 u8 matchSet; /* "[" or 0 */
112620 u8 noCase; /* true to ignore case differences */
112621};
112622
112623/*
112624** For LIKE and GLOB matching on EBCDIC machines, assume that every
112625** character is exactly one byte in size. Also, provde the Utf8Read()
112626** macro for fast reading of the next character in the common case where
112627** the next character is ASCII.
112628*/
112629#if defined(SQLITE_EBCDIC)
112630# define sqlite3Utf8Read(A) (*((*A)++))
112631# define Utf8Read(A) (*(A++))
112632#else
112633# define Utf8Read(A) (A[0]<0x80?*(A++):sqlite3Utf8Read(&A))
112634#endif
112635
112636static const struct compareInfo globInfo = { '*', '?', '[', 0 };
112637/* The correct SQL-92 behavior is for the LIKE operator to ignore
112638** case. Thus 'a' LIKE 'A' would be true. */
112639static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 };
112640/* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
112641** is case sensitive causing 'a' LIKE 'A' to be false */
112642static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 };
112643
112644/*
112645** Possible error returns from patternMatch()
112646*/
112647#define SQLITE_MATCH 0
112648#define SQLITE_NOMATCH 1
112649#define SQLITE_NOWILDCARDMATCH 2
112650
112651/*
112652** Compare two UTF-8 strings for equality where the first string is
112653** a GLOB or LIKE expression. Return values:
112654**
112655** SQLITE_MATCH: Match
112656** SQLITE_NOMATCH: No match
112657** SQLITE_NOWILDCARDMATCH: No match in spite of having * or % wildcards.
112658**
112659** Globbing rules:
112660**
112661** '*' Matches any sequence of zero or more characters.
112662**
112663** '?' Matches exactly one character.
112664**
112665** [...] Matches one character from the enclosed list of
112666** characters.
112667**
112668** [^...] Matches one character not in the enclosed list.
112669**
112670** With the [...] and [^...] matching, a ']' character can be included
112671** in the list by making it the first character after '[' or '^'. A
112672** range of characters can be specified using '-'. Example:
112673** "[a-z]" matches any single lower-case letter. To match a '-', make
112674** it the last character in the list.
112675**
112676** Like matching rules:
112677**
112678** '%' Matches any sequence of zero or more characters
112679**
112680*** '_' Matches any one character
112681**
112682** Ec Where E is the "esc" character and c is any other
112683** character, including '%', '_', and esc, match exactly c.
112684**
112685** The comments within this routine usually assume glob matching.
112686**
112687** This routine is usually quick, but can be N**2 in the worst case.
112688*/
112689static int patternCompare(
112690 const u8 *zPattern, /* The glob pattern */
112691 const u8 *zString, /* The string to compare against the glob */
112692 const struct compareInfo *pInfo, /* Information about how to do the compare */
112693 u32 matchOther /* The escape char (LIKE) or '[' (GLOB) */
112694){
112695 u32 c, c2; /* Next pattern and input string chars */
112696 u32 matchOne = pInfo->matchOne; /* "?" or "_" */
112697 u32 matchAll = pInfo->matchAll; /* "*" or "%" */
112698 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */
112699 const u8 *zEscaped = 0; /* One past the last escaped input char */
112700
112701 while( (c = Utf8Read(zPattern))!=0 ){
112702 if( c==matchAll ){ /* Match "*" */
112703 /* Skip over multiple "*" characters in the pattern. If there
112704 ** are also "?" characters, skip those as well, but consume a
112705 ** single character of the input string for each "?" skipped */
112706 while( (c=Utf8Read(zPattern)) == matchAll || c == matchOne ){
112707 if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
112708 return SQLITE_NOWILDCARDMATCH;
112709 }
112710 }
112711 if( c==0 ){
112712 return SQLITE_MATCH; /* "*" at the end of the pattern matches */
112713 }else if( c==matchOther ){
112714 if( pInfo->matchSet==0 ){
112715 c = sqlite3Utf8Read(&zPattern);
112716 if( c==0 ) return SQLITE_NOWILDCARDMATCH;
112717 }else{
112718 /* "[...]" immediately follows the "*". We have to do a slow
112719 ** recursive search in this case, but it is an unusual case. */
112720 assert( matchOther<0x80 ); /* '[' is a single-byte character */
112721 while( *zString ){
112722 int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther);
112723 if( bMatch!=SQLITE_NOMATCH ) return bMatch;
112724 SQLITE_SKIP_UTF8(zString);
112725 }
112726 return SQLITE_NOWILDCARDMATCH;
112727 }
112728 }
112729
112730 /* At this point variable c contains the first character of the
112731 ** pattern string past the "*". Search in the input string for the
112732 ** first matching character and recursively continue the match from
112733 ** that point.
112734 **
112735 ** For a case-insensitive search, set variable cx to be the same as
112736 ** c but in the other case and search the input string for either
112737 ** c or cx.
112738 */
112739 if( c<=0x80 ){
112740 char zStop[3];
112741 int bMatch;
112742 if( noCase ){
112743 zStop[0] = sqlite3Toupper(c);
112744 zStop[1] = sqlite3Tolower(c);
112745 zStop[2] = 0;
112746 }else{
112747 zStop[0] = c;
112748 zStop[1] = 0;
112749 }
112750 while(1){
112751 zString += strcspn((const char*)zString, zStop);
112752 if( zString[0]==0 ) break;
112753 zString++;
112754 bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
112755 if( bMatch!=SQLITE_NOMATCH ) return bMatch;
112756 }
112757 }else{
112758 int bMatch;
112759 while( (c2 = Utf8Read(zString))!=0 ){
112760 if( c2!=c ) continue;
112761 bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
112762 if( bMatch!=SQLITE_NOMATCH ) return bMatch;
112763 }
112764 }
112765 return SQLITE_NOWILDCARDMATCH;
112766 }
112767 if( c==matchOther ){
112768 if( pInfo->matchSet==0 ){
112769 c = sqlite3Utf8Read(&zPattern);
112770 if( c==0 ) return SQLITE_NOMATCH;
112771 zEscaped = zPattern;
112772 }else{
112773 u32 prior_c = 0;
112774 int seen = 0;
112775 int invert = 0;
112776 c = sqlite3Utf8Read(&zString);
112777 if( c==0 ) return SQLITE_NOMATCH;
112778 c2 = sqlite3Utf8Read(&zPattern);
112779 if( c2=='^' ){
112780 invert = 1;
112781 c2 = sqlite3Utf8Read(&zPattern);
112782 }
112783 if( c2==']' ){
112784 if( c==']' ) seen = 1;
112785 c2 = sqlite3Utf8Read(&zPattern);
112786 }
112787 while( c2 && c2!=']' ){
112788 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
112789 c2 = sqlite3Utf8Read(&zPattern);
112790 if( c>=prior_c && c<=c2 ) seen = 1;
112791 prior_c = 0;
112792 }else{
112793 if( c==c2 ){
112794 seen = 1;
112795 }
112796 prior_c = c2;
112797 }
112798 c2 = sqlite3Utf8Read(&zPattern);
112799 }
112800 if( c2==0 || (seen ^ invert)==0 ){
112801 return SQLITE_NOMATCH;
112802 }
112803 continue;
112804 }
112805 }
112806 c2 = Utf8Read(zString);
112807 if( c==c2 ) continue;
112808 if( noCase && sqlite3Tolower(c)==sqlite3Tolower(c2) && c<0x80 && c2<0x80 ){
112809 continue;
112810 }
112811 if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue;
112812 return SQLITE_NOMATCH;
112813 }
112814 return *zString==0 ? SQLITE_MATCH : SQLITE_NOMATCH;
112815}
112816
112817/*
112818** The sqlite3_strglob() interface. Return 0 on a match (like strcmp()) and
112819** non-zero if there is no match.
112820*/
112821SQLITE_API int sqlite3_strglob(const char *zGlobPattern, const char *zString){
112822 return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '[');
112823}
112824
112825/*
112826** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
112827** a miss - like strcmp().
112828*/
112829SQLITE_API int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){
112830 return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc);
112831}
112832
112833/*
112834** Count the number of times that the LIKE operator (or GLOB which is
112835** just a variation of LIKE) gets called. This is used for testing
112836** only.
112837*/
112838#ifdef SQLITE_TEST
112839SQLITE_API int sqlite3_like_count = 0;
112840#endif
112841
112842
112843/*
112844** Implementation of the like() SQL function. This function implements
112845** the build-in LIKE operator. The first argument to the function is the
112846** pattern and the second argument is the string. So, the SQL statements:
112847**
112848** A LIKE B
112849**
112850** is implemented as like(B,A).
112851**
112852** This same function (with a different compareInfo structure) computes
112853** the GLOB operator.
112854*/
112855static void likeFunc(
112856 sqlite3_context *context,
112857 int argc,
112858 sqlite3_value **argv
112859){
112860 const unsigned char *zA, *zB;
112861 u32 escape;
112862 int nPat;
112863 sqlite3 *db = sqlite3_context_db_handle(context);
112864 struct compareInfo *pInfo = sqlite3_user_data(context);
112865
112866#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
112867 if( sqlite3_value_type(argv[0])==SQLITE_BLOB
112868 || sqlite3_value_type(argv[1])==SQLITE_BLOB
112869 ){
112870#ifdef SQLITE_TEST
112871 sqlite3_like_count++;
112872#endif
112873 sqlite3_result_int(context, 0);
112874 return;
112875 }
112876#endif
112877 zB = sqlite3_value_text(argv[0]);
112878 zA = sqlite3_value_text(argv[1]);
112879
112880 /* Limit the length of the LIKE or GLOB pattern to avoid problems
112881 ** of deep recursion and N*N behavior in patternCompare().
112882 */
112883 nPat = sqlite3_value_bytes(argv[0]);
112884 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
112885 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
112886 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
112887 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
112888 return;
112889 }
112890 assert( zB==sqlite3_value_text(argv[0]) ); /* Encoding did not change */
112891
112892 if( argc==3 ){
112893 /* The escape character string must consist of a single UTF-8 character.
112894 ** Otherwise, return an error.
112895 */
112896 const unsigned char *zEsc = sqlite3_value_text(argv[2]);
112897 if( zEsc==0 ) return;
112898 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
112899 sqlite3_result_error(context,
112900 "ESCAPE expression must be a single character", -1);
112901 return;
112902 }
112903 escape = sqlite3Utf8Read(&zEsc);
112904 }else{
112905 escape = pInfo->matchSet;
112906 }
112907 if( zA && zB ){
112908#ifdef SQLITE_TEST
112909 sqlite3_like_count++;
112910#endif
112911 sqlite3_result_int(context,
112912 patternCompare(zB, zA, pInfo, escape)==SQLITE_MATCH);
112913 }
112914}
112915
112916/*
112917** Implementation of the NULLIF(x,y) function. The result is the first
112918** argument if the arguments are different. The result is NULL if the
112919** arguments are equal to each other.
112920*/
112921static void nullifFunc(
112922 sqlite3_context *context,
112923 int NotUsed,
112924 sqlite3_value **argv
112925){
112926 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
112927 UNUSED_PARAMETER(NotUsed);
112928 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
112929 sqlite3_result_value(context, argv[0]);
112930 }
112931}
112932
112933/*
112934** Implementation of the sqlite_version() function. The result is the version
112935** of the SQLite library that is running.
112936*/
112937static void versionFunc(
112938 sqlite3_context *context,
112939 int NotUsed,
112940 sqlite3_value **NotUsed2
112941){
112942 UNUSED_PARAMETER2(NotUsed, NotUsed2);
112943 /* IMP: R-48699-48617 This function is an SQL wrapper around the
112944 ** sqlite3_libversion() C-interface. */
112945 sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
112946}
112947
112948/*
112949** Implementation of the sqlite_source_id() function. The result is a string
112950** that identifies the particular version of the source code used to build
112951** SQLite.
112952*/
112953static void sourceidFunc(
112954 sqlite3_context *context,
112955 int NotUsed,
112956 sqlite3_value **NotUsed2
112957){
112958 UNUSED_PARAMETER2(NotUsed, NotUsed2);
112959 /* IMP: R-24470-31136 This function is an SQL wrapper around the
112960 ** sqlite3_sourceid() C interface. */
112961 sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
112962}
112963
112964/*
112965** Implementation of the sqlite_log() function. This is a wrapper around
112966** sqlite3_log(). The return value is NULL. The function exists purely for
112967** its side-effects.
112968*/
112969static void errlogFunc(
112970 sqlite3_context *context,
112971 int argc,
112972 sqlite3_value **argv
112973){
112974 UNUSED_PARAMETER(argc);
112975 UNUSED_PARAMETER(context);
112976 sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1]));
112977}
112978
112979/*
112980** Implementation of the sqlite_compileoption_used() function.
112981** The result is an integer that identifies if the compiler option
112982** was used to build SQLite.
112983*/
112984#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
112985static void compileoptionusedFunc(
112986 sqlite3_context *context,
112987 int argc,
112988 sqlite3_value **argv
112989){
112990 const char *zOptName;
112991 assert( argc==1 );
112992 UNUSED_PARAMETER(argc);
112993 /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
112994 ** function is a wrapper around the sqlite3_compileoption_used() C/C++
112995 ** function.
112996 */
112997 if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){
112998 sqlite3_result_int(context, sqlite3_compileoption_used(zOptName));
112999 }
113000}
113001#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
113002
113003/*
113004** Implementation of the sqlite_compileoption_get() function.
113005** The result is a string that identifies the compiler options
113006** used to build SQLite.
113007*/
113008#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
113009static void compileoptiongetFunc(
113010 sqlite3_context *context,
113011 int argc,
113012 sqlite3_value **argv
113013){
113014 int n;
113015 assert( argc==1 );
113016 UNUSED_PARAMETER(argc);
113017 /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
113018 ** is a wrapper around the sqlite3_compileoption_get() C/C++ function.
113019 */
113020 n = sqlite3_value_int(argv[0]);
113021 sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
113022}
113023#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
113024
113025/* Array for converting from half-bytes (nybbles) into ASCII hex
113026** digits. */
113027static const char hexdigits[] = {
113028 '0', '1', '2', '3', '4', '5', '6', '7',
113029 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
113030};
113031
113032/*
113033** Implementation of the QUOTE() function. This function takes a single
113034** argument. If the argument is numeric, the return value is the same as
113035** the argument. If the argument is NULL, the return value is the string
113036** "NULL". Otherwise, the argument is enclosed in single quotes with
113037** single-quote escapes.
113038*/
113039static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
113040 assert( argc==1 );
113041 UNUSED_PARAMETER(argc);
113042 switch( sqlite3_value_type(argv[0]) ){
113043 case SQLITE_FLOAT: {
113044 double r1, r2;
113045 char zBuf[50];
113046 r1 = sqlite3_value_double(argv[0]);
113047 sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1);
113048 sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8);
113049 if( r1!=r2 ){
113050 sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1);
113051 }
113052 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
113053 break;
113054 }
113055 case SQLITE_INTEGER: {
113056 sqlite3_result_value(context, argv[0]);
113057 break;
113058 }
113059 case SQLITE_BLOB: {
113060 char *zText = 0;
113061 char const *zBlob = sqlite3_value_blob(argv[0]);
113062 int nBlob = sqlite3_value_bytes(argv[0]);
113063 assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
113064 zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4);
113065 if( zText ){
113066 int i;
113067 for(i=0; i<nBlob; i++){
113068 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
113069 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
113070 }
113071 zText[(nBlob*2)+2] = '\'';
113072 zText[(nBlob*2)+3] = '\0';
113073 zText[0] = 'X';
113074 zText[1] = '\'';
113075 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
113076 sqlite3_free(zText);
113077 }
113078 break;
113079 }
113080 case SQLITE_TEXT: {
113081 int i,j;
113082 u64 n;
113083 const unsigned char *zArg = sqlite3_value_text(argv[0]);
113084 char *z;
113085
113086 if( zArg==0 ) return;
113087 for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
113088 z = contextMalloc(context, ((i64)i)+((i64)n)+3);
113089 if( z ){
113090 z[0] = '\'';
113091 for(i=0, j=1; zArg[i]; i++){
113092 z[j++] = zArg[i];
113093 if( zArg[i]=='\'' ){
113094 z[j++] = '\'';
113095 }
113096 }
113097 z[j++] = '\'';
113098 z[j] = 0;
113099 sqlite3_result_text(context, z, j, sqlite3_free);
113100 }
113101 break;
113102 }
113103 default: {
113104 assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
113105 sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
113106 break;
113107 }
113108 }
113109}
113110
113111/*
113112** The unicode() function. Return the integer unicode code-point value
113113** for the first character of the input string.
113114*/
113115static void unicodeFunc(
113116 sqlite3_context *context,
113117 int argc,
113118 sqlite3_value **argv
113119){
113120 const unsigned char *z = sqlite3_value_text(argv[0]);
113121 (void)argc;
113122 if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z));
113123}
113124
113125/*
113126** The char() function takes zero or more arguments, each of which is
113127** an integer. It constructs a string where each character of the string
113128** is the unicode character for the corresponding integer argument.
113129*/
113130static void charFunc(
113131 sqlite3_context *context,
113132 int argc,
113133 sqlite3_value **argv
113134){
113135 unsigned char *z, *zOut;
113136 int i;
113137 zOut = z = sqlite3_malloc64( argc*4+1 );
113138 if( z==0 ){
113139 sqlite3_result_error_nomem(context);
113140 return;
113141 }
113142 for(i=0; i<argc; i++){
113143 sqlite3_int64 x;
113144 unsigned c;
113145 x = sqlite3_value_int64(argv[i]);
113146 if( x<0 || x>0x10ffff ) x = 0xfffd;
113147 c = (unsigned)(x & 0x1fffff);
113148 if( c<0x00080 ){
113149 *zOut++ = (u8)(c&0xFF);
113150 }else if( c<0x00800 ){
113151 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);
113152 *zOut++ = 0x80 + (u8)(c & 0x3F);
113153 }else if( c<0x10000 ){
113154 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);
113155 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
113156 *zOut++ = 0x80 + (u8)(c & 0x3F);
113157 }else{
113158 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);
113159 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);
113160 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
113161 *zOut++ = 0x80 + (u8)(c & 0x3F);
113162 } \
113163 }
113164 sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8);
113165}
113166
113167/*
113168** The hex() function. Interpret the argument as a blob. Return
113169** a hexadecimal rendering as text.
113170*/
113171static void hexFunc(
113172 sqlite3_context *context,
113173 int argc,
113174 sqlite3_value **argv
113175){
113176 int i, n;
113177 const unsigned char *pBlob;
113178 char *zHex, *z;
113179 assert( argc==1 );
113180 UNUSED_PARAMETER(argc);
113181 pBlob = sqlite3_value_blob(argv[0]);
113182 n = sqlite3_value_bytes(argv[0]);
113183 assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
113184 z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
113185 if( zHex ){
113186 for(i=0; i<n; i++, pBlob++){
113187 unsigned char c = *pBlob;
113188 *(z++) = hexdigits[(c>>4)&0xf];
113189 *(z++) = hexdigits[c&0xf];
113190 }
113191 *z = 0;
113192 sqlite3_result_text(context, zHex, n*2, sqlite3_free);
113193 }
113194}
113195
113196/*
113197** The zeroblob(N) function returns a zero-filled blob of size N bytes.
113198*/
113199static void zeroblobFunc(
113200 sqlite3_context *context,
113201 int argc,
113202 sqlite3_value **argv
113203){
113204 i64 n;
113205 int rc;
113206 assert( argc==1 );
113207 UNUSED_PARAMETER(argc);
113208 n = sqlite3_value_int64(argv[0]);
113209 if( n<0 ) n = 0;
113210 rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */
113211 if( rc ){
113212 sqlite3_result_error_code(context, rc);
113213 }
113214}
113215
113216/*
113217** The replace() function. Three arguments are all strings: call
113218** them A, B, and C. The result is also a string which is derived
113219** from A by replacing every occurrence of B with C. The match
113220** must be exact. Collating sequences are not used.
113221*/
113222static void replaceFunc(
113223 sqlite3_context *context,
113224 int argc,
113225 sqlite3_value **argv
113226){
113227 const unsigned char *zStr; /* The input string A */
113228 const unsigned char *zPattern; /* The pattern string B */
113229 const unsigned char *zRep; /* The replacement string C */
113230 unsigned char *zOut; /* The output */
113231 int nStr; /* Size of zStr */
113232 int nPattern; /* Size of zPattern */
113233 int nRep; /* Size of zRep */
113234 i64 nOut; /* Maximum size of zOut */
113235 int loopLimit; /* Last zStr[] that might match zPattern[] */
113236 int i, j; /* Loop counters */
113237 unsigned cntExpand; /* Number zOut expansions */
113238 sqlite3 *db = sqlite3_context_db_handle(context);
113239
113240 assert( argc==3 );
113241 UNUSED_PARAMETER(argc);
113242 zStr = sqlite3_value_text(argv[0]);
113243 if( zStr==0 ) return;
113244 nStr = sqlite3_value_bytes(argv[0]);
113245 assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */
113246 zPattern = sqlite3_value_text(argv[1]);
113247 if( zPattern==0 ){
113248 assert( sqlite3_value_type(argv[1])==SQLITE_NULL
113249 || sqlite3_context_db_handle(context)->mallocFailed );
113250 return;
113251 }
113252 if( zPattern[0]==0 ){
113253 assert( sqlite3_value_type(argv[1])!=SQLITE_NULL );
113254 sqlite3_result_value(context, argv[0]);
113255 return;
113256 }
113257 nPattern = sqlite3_value_bytes(argv[1]);
113258 assert( zPattern==sqlite3_value_text(argv[1]) ); /* No encoding change */
113259 zRep = sqlite3_value_text(argv[2]);
113260 if( zRep==0 ) return;
113261 nRep = sqlite3_value_bytes(argv[2]);
113262 assert( zRep==sqlite3_value_text(argv[2]) );
113263 nOut = nStr + 1;
113264 assert( nOut<SQLITE_MAX_LENGTH );
113265 zOut = contextMalloc(context, (i64)nOut);
113266 if( zOut==0 ){
113267 return;
113268 }
113269 loopLimit = nStr - nPattern;
113270 cntExpand = 0;
113271 for(i=j=0; i<=loopLimit; i++){
113272 if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
113273 zOut[j++] = zStr[i];
113274 }else{
113275 if( nRep>nPattern ){
113276 nOut += nRep - nPattern;
113277 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
113278 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
113279 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
113280 sqlite3_result_error_toobig(context);
113281 sqlite3_free(zOut);
113282 return;
113283 }
113284 cntExpand++;
113285 if( (cntExpand&(cntExpand-1))==0 ){
113286 /* Grow the size of the output buffer only on substitutions
113287 ** whose index is a power of two: 1, 2, 4, 8, 16, 32, ... */
113288 u8 *zOld;
113289 zOld = zOut;
113290 zOut = sqlite3_realloc64(zOut, (int)nOut + (nOut - nStr - 1));
113291 if( zOut==0 ){
113292 sqlite3_result_error_nomem(context);
113293 sqlite3_free(zOld);
113294 return;
113295 }
113296 }
113297 }
113298 memcpy(&zOut[j], zRep, nRep);
113299 j += nRep;
113300 i += nPattern-1;
113301 }
113302 }
113303 assert( j+nStr-i+1<=nOut );
113304 memcpy(&zOut[j], &zStr[i], nStr-i);
113305 j += nStr - i;
113306 assert( j<=nOut );
113307 zOut[j] = 0;
113308 sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
113309}
113310
113311/*
113312** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
113313** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
113314*/
113315static void trimFunc(
113316 sqlite3_context *context,
113317 int argc,
113318 sqlite3_value **argv
113319){
113320 const unsigned char *zIn; /* Input string */
113321 const unsigned char *zCharSet; /* Set of characters to trim */
113322 int nIn; /* Number of bytes in input */
113323 int flags; /* 1: trimleft 2: trimright 3: trim */
113324 int i; /* Loop counter */
113325 unsigned char *aLen = 0; /* Length of each character in zCharSet */
113326 unsigned char **azChar = 0; /* Individual characters in zCharSet */
113327 int nChar; /* Number of characters in zCharSet */
113328
113329 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
113330 return;
113331 }
113332 zIn = sqlite3_value_text(argv[0]);
113333 if( zIn==0 ) return;
113334 nIn = sqlite3_value_bytes(argv[0]);
113335 assert( zIn==sqlite3_value_text(argv[0]) );
113336 if( argc==1 ){
113337 static const unsigned char lenOne[] = { 1 };
113338 static unsigned char * const azOne[] = { (u8*)" " };
113339 nChar = 1;
113340 aLen = (u8*)lenOne;
113341 azChar = (unsigned char **)azOne;
113342 zCharSet = 0;
113343 }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
113344 return;
113345 }else{
113346 const unsigned char *z;
113347 for(z=zCharSet, nChar=0; *z; nChar++){
113348 SQLITE_SKIP_UTF8(z);
113349 }
113350 if( nChar>0 ){
113351 azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1));
113352 if( azChar==0 ){
113353 return;
113354 }
113355 aLen = (unsigned char*)&azChar[nChar];
113356 for(z=zCharSet, nChar=0; *z; nChar++){
113357 azChar[nChar] = (unsigned char *)z;
113358 SQLITE_SKIP_UTF8(z);
113359 aLen[nChar] = (u8)(z - azChar[nChar]);
113360 }
113361 }
113362 }
113363 if( nChar>0 ){
113364 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context));
113365 if( flags & 1 ){
113366 while( nIn>0 ){
113367 int len = 0;
113368 for(i=0; i<nChar; i++){
113369 len = aLen[i];
113370 if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break;
113371 }
113372 if( i>=nChar ) break;
113373 zIn += len;
113374 nIn -= len;
113375 }
113376 }
113377 if( flags & 2 ){
113378 while( nIn>0 ){
113379 int len = 0;
113380 for(i=0; i<nChar; i++){
113381 len = aLen[i];
113382 if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
113383 }
113384 if( i>=nChar ) break;
113385 nIn -= len;
113386 }
113387 }
113388 if( zCharSet ){
113389 sqlite3_free(azChar);
113390 }
113391 }
113392 sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
113393}
113394
113395
113396#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
113397/*
113398** The "unknown" function is automatically substituted in place of
113399** any unrecognized function name when doing an EXPLAIN or EXPLAIN QUERY PLAN
113400** when the SQLITE_ENABLE_UNKNOWN_FUNCTION compile-time option is used.
113401** When the "sqlite3" command-line shell is built using this functionality,
113402** that allows an EXPLAIN or EXPLAIN QUERY PLAN for complex queries
113403** involving application-defined functions to be examined in a generic
113404** sqlite3 shell.
113405*/
113406static void unknownFunc(
113407 sqlite3_context *context,
113408 int argc,
113409 sqlite3_value **argv
113410){
113411 /* no-op */
113412}
113413#endif /*SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION*/
113414
113415
113416/* IMP: R-25361-16150 This function is omitted from SQLite by default. It
113417** is only available if the SQLITE_SOUNDEX compile-time option is used
113418** when SQLite is built.
113419*/
113420#ifdef SQLITE_SOUNDEX
113421/*
113422** Compute the soundex encoding of a word.
113423**
113424** IMP: R-59782-00072 The soundex(X) function returns a string that is the
113425** soundex encoding of the string X.
113426*/
113427static void soundexFunc(
113428 sqlite3_context *context,
113429 int argc,
113430 sqlite3_value **argv
113431){
113432 char zResult[8];
113433 const u8 *zIn;
113434 int i, j;
113435 static const unsigned char iCode[] = {
113436 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
113437 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
113438 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
113439 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
113440 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
113441 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
113442 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
113443 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
113444 };
113445 assert( argc==1 );
113446 zIn = (u8*)sqlite3_value_text(argv[0]);
113447 if( zIn==0 ) zIn = (u8*)"";
113448 for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){}
113449 if( zIn[i] ){
113450 u8 prevcode = iCode[zIn[i]&0x7f];
113451 zResult[0] = sqlite3Toupper(zIn[i]);
113452 for(j=1; j<4 && zIn[i]; i++){
113453 int code = iCode[zIn[i]&0x7f];
113454 if( code>0 ){
113455 if( code!=prevcode ){
113456 prevcode = code;
113457 zResult[j++] = code + '0';
113458 }
113459 }else{
113460 prevcode = 0;
113461 }
113462 }
113463 while( j<4 ){
113464 zResult[j++] = '0';
113465 }
113466 zResult[j] = 0;
113467 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
113468 }else{
113469 /* IMP: R-64894-50321 The string "?000" is returned if the argument
113470 ** is NULL or contains no ASCII alphabetic characters. */
113471 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
113472 }
113473}
113474#endif /* SQLITE_SOUNDEX */
113475
113476#ifndef SQLITE_OMIT_LOAD_EXTENSION
113477/*
113478** A function that loads a shared-library extension then returns NULL.
113479*/
113480static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
113481 const char *zFile = (const char *)sqlite3_value_text(argv[0]);
113482 const char *zProc;
113483 sqlite3 *db = sqlite3_context_db_handle(context);
113484 char *zErrMsg = 0;
113485
113486 /* Disallow the load_extension() SQL function unless the SQLITE_LoadExtFunc
113487 ** flag is set. See the sqlite3_enable_load_extension() API.
113488 */
113489 if( (db->flags & SQLITE_LoadExtFunc)==0 ){
113490 sqlite3_result_error(context, "not authorized", -1);
113491 return;
113492 }
113493
113494 if( argc==2 ){
113495 zProc = (const char *)sqlite3_value_text(argv[1]);
113496 }else{
113497 zProc = 0;
113498 }
113499 if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
113500 sqlite3_result_error(context, zErrMsg, -1);
113501 sqlite3_free(zErrMsg);
113502 }
113503}
113504#endif
113505
113506
113507/*
113508** An instance of the following structure holds the context of a
113509** sum() or avg() aggregate computation.
113510*/
113511typedef struct SumCtx SumCtx;
113512struct SumCtx {
113513 double rSum; /* Floating point sum */
113514 i64 iSum; /* Integer sum */
113515 i64 cnt; /* Number of elements summed */
113516 u8 overflow; /* True if integer overflow seen */
113517 u8 approx; /* True if non-integer value was input to the sum */
113518};
113519
113520/*
113521** Routines used to compute the sum, average, and total.
113522**
113523** The SUM() function follows the (broken) SQL standard which means
113524** that it returns NULL if it sums over no inputs. TOTAL returns
113525** 0.0 in that case. In addition, TOTAL always returns a float where
113526** SUM might return an integer if it never encounters a floating point
113527** value. TOTAL never fails, but SUM might through an exception if
113528** it overflows an integer.
113529*/
113530static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
113531 SumCtx *p;
113532 int type;
113533 assert( argc==1 );
113534 UNUSED_PARAMETER(argc);
113535 p = sqlite3_aggregate_context(context, sizeof(*p));
113536 type = sqlite3_value_numeric_type(argv[0]);
113537 if( p && type!=SQLITE_NULL ){
113538 p->cnt++;
113539 if( type==SQLITE_INTEGER ){
113540 i64 v = sqlite3_value_int64(argv[0]);
113541 p->rSum += v;
113542 if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
113543 p->approx = p->overflow = 1;
113544 }
113545 }else{
113546 p->rSum += sqlite3_value_double(argv[0]);
113547 p->approx = 1;
113548 }
113549 }
113550}
113551#ifndef SQLITE_OMIT_WINDOWFUNC
113552static void sumInverse(sqlite3_context *context, int argc, sqlite3_value**argv){
113553 SumCtx *p;
113554 int type;
113555 assert( argc==1 );
113556 UNUSED_PARAMETER(argc);
113557 p = sqlite3_aggregate_context(context, sizeof(*p));
113558 type = sqlite3_value_numeric_type(argv[0]);
113559 /* p is always non-NULL because sumStep() will have been called first
113560 ** to initialize it */
113561 if( ALWAYS(p) && type!=SQLITE_NULL ){
113562 assert( p->cnt>0 );
113563 p->cnt--;
113564 assert( type==SQLITE_INTEGER || p->approx );
113565 if( type==SQLITE_INTEGER && p->approx==0 ){
113566 i64 v = sqlite3_value_int64(argv[0]);
113567 p->rSum -= v;
113568 p->iSum -= v;
113569 }else{
113570 p->rSum -= sqlite3_value_double(argv[0]);
113571 }
113572 }
113573}
113574#else
113575# define sumInverse 0
113576#endif /* SQLITE_OMIT_WINDOWFUNC */
113577static void sumFinalize(sqlite3_context *context){
113578 SumCtx *p;
113579 p = sqlite3_aggregate_context(context, 0);
113580 if( p && p->cnt>0 ){
113581 if( p->overflow ){
113582 sqlite3_result_error(context,"integer overflow",-1);
113583 }else if( p->approx ){
113584 sqlite3_result_double(context, p->rSum);
113585 }else{
113586 sqlite3_result_int64(context, p->iSum);
113587 }
113588 }
113589}
113590static void avgFinalize(sqlite3_context *context){
113591 SumCtx *p;
113592 p = sqlite3_aggregate_context(context, 0);
113593 if( p && p->cnt>0 ){
113594 sqlite3_result_double(context, p->rSum/(double)p->cnt);
113595 }
113596}
113597static void totalFinalize(sqlite3_context *context){
113598 SumCtx *p;
113599 p = sqlite3_aggregate_context(context, 0);
113600 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
113601 sqlite3_result_double(context, p ? p->rSum : (double)0);
113602}
113603
113604/*
113605** The following structure keeps track of state information for the
113606** count() aggregate function.
113607*/
113608typedef struct CountCtx CountCtx;
113609struct CountCtx {
113610 i64 n;
113611#ifdef SQLITE_DEBUG
113612 int bInverse; /* True if xInverse() ever called */
113613#endif
113614};
113615
113616/*
113617** Routines to implement the count() aggregate function.
113618*/
113619static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
113620 CountCtx *p;
113621 p = sqlite3_aggregate_context(context, sizeof(*p));
113622 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
113623 p->n++;
113624 }
113625
113626#ifndef SQLITE_OMIT_DEPRECATED
113627 /* The sqlite3_aggregate_count() function is deprecated. But just to make
113628 ** sure it still operates correctly, verify that its count agrees with our
113629 ** internal count when using count(*) and when the total count can be
113630 ** expressed as a 32-bit integer. */
113631 assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse
113632 || p->n==sqlite3_aggregate_count(context) );
113633#endif
113634}
113635static void countFinalize(sqlite3_context *context){
113636 CountCtx *p;
113637 p = sqlite3_aggregate_context(context, 0);
113638 sqlite3_result_int64(context, p ? p->n : 0);
113639}
113640#ifndef SQLITE_OMIT_WINDOWFUNC
113641static void countInverse(sqlite3_context *ctx, int argc, sqlite3_value **argv){
113642 CountCtx *p;
113643 p = sqlite3_aggregate_context(ctx, sizeof(*p));
113644 /* p is always non-NULL since countStep() will have been called first */
113645 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && ALWAYS(p) ){
113646 p->n--;
113647#ifdef SQLITE_DEBUG
113648 p->bInverse = 1;
113649#endif
113650 }
113651}
113652#else
113653# define countInverse 0
113654#endif /* SQLITE_OMIT_WINDOWFUNC */
113655
113656/*
113657** Routines to implement min() and max() aggregate functions.
113658*/
113659static void minmaxStep(
113660 sqlite3_context *context,
113661 int NotUsed,
113662 sqlite3_value **argv
113663){
113664 Mem *pArg = (Mem *)argv[0];
113665 Mem *pBest;
113666 UNUSED_PARAMETER(NotUsed);
113667
113668 pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
113669 if( !pBest ) return;
113670
113671 if( sqlite3_value_type(pArg)==SQLITE_NULL ){
113672 if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
113673 }else if( pBest->flags ){
113674 int max;
113675 int cmp;
113676 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
113677 /* This step function is used for both the min() and max() aggregates,
113678 ** the only difference between the two being that the sense of the
113679 ** comparison is inverted. For the max() aggregate, the
113680 ** sqlite3_user_data() function returns (void *)-1. For min() it
113681 ** returns (void *)db, where db is the sqlite3* database pointer.
113682 ** Therefore the next statement sets variable 'max' to 1 for the max()
113683 ** aggregate, or 0 for min().
113684 */
113685 max = sqlite3_user_data(context)!=0;
113686 cmp = sqlite3MemCompare(pBest, pArg, pColl);
113687 if( (max && cmp<0) || (!max && cmp>0) ){
113688 sqlite3VdbeMemCopy(pBest, pArg);
113689 }else{
113690 sqlite3SkipAccumulatorLoad(context);
113691 }
113692 }else{
113693 pBest->db = sqlite3_context_db_handle(context);
113694 sqlite3VdbeMemCopy(pBest, pArg);
113695 }
113696}
113697static void minMaxValueFinalize(sqlite3_context *context, int bValue){
113698 sqlite3_value *pRes;
113699 pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
113700 if( pRes ){
113701 if( pRes->flags ){
113702 sqlite3_result_value(context, pRes);
113703 }
113704 if( bValue==0 ) sqlite3VdbeMemRelease(pRes);
113705 }
113706}
113707#ifndef SQLITE_OMIT_WINDOWFUNC
113708static void minMaxValue(sqlite3_context *context){
113709 minMaxValueFinalize(context, 1);
113710}
113711#else
113712# define minMaxValue 0
113713#endif /* SQLITE_OMIT_WINDOWFUNC */
113714static void minMaxFinalize(sqlite3_context *context){
113715 minMaxValueFinalize(context, 0);
113716}
113717
113718/*
113719** group_concat(EXPR, ?SEPARATOR?)
113720*/
113721static void groupConcatStep(
113722 sqlite3_context *context,
113723 int argc,
113724 sqlite3_value **argv
113725){
113726 const char *zVal;
113727 StrAccum *pAccum;
113728 const char *zSep;
113729 int nVal, nSep;
113730 assert( argc==1 || argc==2 );
113731 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
113732 pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
113733
113734 if( pAccum ){
113735 sqlite3 *db = sqlite3_context_db_handle(context);
113736 int firstTerm = pAccum->mxAlloc==0;
113737 pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
113738 if( !firstTerm ){
113739 if( argc==2 ){
113740 zSep = (char*)sqlite3_value_text(argv[1]);
113741 nSep = sqlite3_value_bytes(argv[1]);
113742 }else{
113743 zSep = ",";
113744 nSep = 1;
113745 }
113746 if( zSep ) sqlite3_str_append(pAccum, zSep, nSep);
113747 }
113748 zVal = (char*)sqlite3_value_text(argv[0]);
113749 nVal = sqlite3_value_bytes(argv[0]);
113750 if( zVal ) sqlite3_str_append(pAccum, zVal, nVal);
113751 }
113752}
113753#ifndef SQLITE_OMIT_WINDOWFUNC
113754static void groupConcatInverse(
113755 sqlite3_context *context,
113756 int argc,
113757 sqlite3_value **argv
113758){
113759 int n;
113760 StrAccum *pAccum;
113761 assert( argc==1 || argc==2 );
113762 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
113763 pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
113764 /* pAccum is always non-NULL since groupConcatStep() will have always
113765 ** run frist to initialize it */
113766 if( ALWAYS(pAccum) ){
113767 n = sqlite3_value_bytes(argv[0]);
113768 if( argc==2 ){
113769 n += sqlite3_value_bytes(argv[1]);
113770 }else{
113771 n++;
113772 }
113773 if( n>=(int)pAccum->nChar ){
113774 pAccum->nChar = 0;
113775 }else{
113776 pAccum->nChar -= n;
113777 memmove(pAccum->zText, &pAccum->zText[n], pAccum->nChar);
113778 }
113779 if( pAccum->nChar==0 ) pAccum->mxAlloc = 0;
113780 }
113781}
113782#else
113783# define groupConcatInverse 0
113784#endif /* SQLITE_OMIT_WINDOWFUNC */
113785static void groupConcatFinalize(sqlite3_context *context){
113786 StrAccum *pAccum;
113787 pAccum = sqlite3_aggregate_context(context, 0);
113788 if( pAccum ){
113789 if( pAccum->accError==SQLITE_TOOBIG ){
113790 sqlite3_result_error_toobig(context);
113791 }else if( pAccum->accError==SQLITE_NOMEM ){
113792 sqlite3_result_error_nomem(context);
113793 }else{
113794 sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1,
113795 sqlite3_free);
113796 }
113797 }
113798}
113799#ifndef SQLITE_OMIT_WINDOWFUNC
113800static void groupConcatValue(sqlite3_context *context){
113801 sqlite3_str *pAccum;
113802 pAccum = (sqlite3_str*)sqlite3_aggregate_context(context, 0);
113803 if( pAccum ){
113804 if( pAccum->accError==SQLITE_TOOBIG ){
113805 sqlite3_result_error_toobig(context);
113806 }else if( pAccum->accError==SQLITE_NOMEM ){
113807 sqlite3_result_error_nomem(context);
113808 }else{
113809 const char *zText = sqlite3_str_value(pAccum);
113810 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
113811 }
113812 }
113813}
113814#else
113815# define groupConcatValue 0
113816#endif /* SQLITE_OMIT_WINDOWFUNC */
113817
113818/*
113819** This routine does per-connection function registration. Most
113820** of the built-in functions above are part of the global function set.
113821** This routine only deals with those that are not global.
113822*/
113823SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3 *db){
113824 int rc = sqlite3_overload_function(db, "MATCH", 2);
113825 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
113826 if( rc==SQLITE_NOMEM ){
113827 sqlite3OomFault(db);
113828 }
113829}
113830
113831/*
113832** Set the LIKEOPT flag on the 2-argument function with the given name.
113833*/
113834static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){
113835 FuncDef *pDef;
113836 pDef = sqlite3FindFunction(db, zName, 2, SQLITE_UTF8, 0);
113837 if( ALWAYS(pDef) ){
113838 pDef->funcFlags |= flagVal;
113839 }
113840}
113841
113842/*
113843** Register the built-in LIKE and GLOB functions. The caseSensitive
113844** parameter determines whether or not the LIKE operator is case
113845** sensitive. GLOB is always case sensitive.
113846*/
113847SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
113848 struct compareInfo *pInfo;
113849 if( caseSensitive ){
113850 pInfo = (struct compareInfo*)&likeInfoAlt;
113851 }else{
113852 pInfo = (struct compareInfo*)&likeInfoNorm;
113853 }
113854 sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0, 0, 0);
113855 sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0, 0, 0);
113856 sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8,
113857 (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0, 0, 0);
113858 setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
113859 setLikeOptFlag(db, "like",
113860 caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
113861}
113862
113863/*
113864** pExpr points to an expression which implements a function. If
113865** it is appropriate to apply the LIKE optimization to that function
113866** then set aWc[0] through aWc[2] to the wildcard characters and the
113867** escape character and then return TRUE. If the function is not a
113868** LIKE-style function then return FALSE.
113869**
113870** The expression "a LIKE b ESCAPE c" is only considered a valid LIKE
113871** operator if c is a string literal that is exactly one byte in length.
113872** That one byte is stored in aWc[3]. aWc[3] is set to zero if there is
113873** no ESCAPE clause.
113874**
113875** *pIsNocase is set to true if uppercase and lowercase are equivalent for
113876** the function (default for LIKE). If the function makes the distinction
113877** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to
113878** false.
113879*/
113880SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
113881 FuncDef *pDef;
113882 int nExpr;
113883 if( pExpr->op!=TK_FUNCTION || !pExpr->x.pList ){
113884 return 0;
113885 }
113886 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
113887 nExpr = pExpr->x.pList->nExpr;
113888 pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF8, 0);
113889 if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
113890 return 0;
113891 }
113892 if( nExpr<3 ){
113893 aWc[3] = 0;
113894 }else{
113895 Expr *pEscape = pExpr->x.pList->a[2].pExpr;
113896 char *zEscape;
113897 if( pEscape->op!=TK_STRING ) return 0;
113898 zEscape = pEscape->u.zToken;
113899 if( zEscape[0]==0 || zEscape[1]!=0 ) return 0;
113900 aWc[3] = zEscape[0];
113901 }
113902
113903 /* The memcpy() statement assumes that the wildcard characters are
113904 ** the first three statements in the compareInfo structure. The
113905 ** asserts() that follow verify that assumption
113906 */
113907 memcpy(aWc, pDef->pUserData, 3);
113908 assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
113909 assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
113910 assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
113911 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
113912 return 1;
113913}
113914
113915/*
113916** All of the FuncDef structures in the aBuiltinFunc[] array above
113917** to the global function hash table. This occurs at start-time (as
113918** a consequence of calling sqlite3_initialize()).
113919**
113920** After this routine runs
113921*/
113922SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void){
113923 /*
113924 ** The following array holds FuncDef structures for all of the functions
113925 ** defined in this file.
113926 **
113927 ** The array cannot be constant since changes are made to the
113928 ** FuncDef.pHash elements at start-time. The elements of this array
113929 ** are read-only after initialization is complete.
113930 **
113931 ** For peak efficiency, put the most frequently used function last.
113932 */
113933 static FuncDef aBuiltinFunc[] = {
113934#ifdef SQLITE_SOUNDEX
113935 FUNCTION(soundex, 1, 0, 0, soundexFunc ),
113936#endif
113937#ifndef SQLITE_OMIT_LOAD_EXTENSION
113938 VFUNCTION(load_extension, 1, 0, 0, loadExt ),
113939 VFUNCTION(load_extension, 2, 0, 0, loadExt ),
113940#endif
113941#if SQLITE_USER_AUTHENTICATION
113942 FUNCTION(sqlite_crypt, 2, 0, 0, sqlite3CryptFunc ),
113943#endif
113944#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
113945 DFUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ),
113946 DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ),
113947#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
113948 FUNCTION2(unlikely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
113949 FUNCTION2(likelihood, 2, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
113950 FUNCTION2(likely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
113951#ifdef SQLITE_DEBUG
113952 FUNCTION2(affinity, 1, 0, 0, noopFunc, SQLITE_FUNC_AFFINITY),
113953#endif
113954#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
113955 FUNCTION2(sqlite_offset, 1, 0, 0, noopFunc, SQLITE_FUNC_OFFSET|
113956 SQLITE_FUNC_TYPEOF),
113957#endif
113958 FUNCTION(ltrim, 1, 1, 0, trimFunc ),
113959 FUNCTION(ltrim, 2, 1, 0, trimFunc ),
113960 FUNCTION(rtrim, 1, 2, 0, trimFunc ),
113961 FUNCTION(rtrim, 2, 2, 0, trimFunc ),
113962 FUNCTION(trim, 1, 3, 0, trimFunc ),
113963 FUNCTION(trim, 2, 3, 0, trimFunc ),
113964 FUNCTION(min, -1, 0, 1, minmaxFunc ),
113965 FUNCTION(min, 0, 0, 1, 0 ),
113966 WAGGREGATE(min, 1, 0, 1, minmaxStep, minMaxFinalize, minMaxValue, 0,
113967 SQLITE_FUNC_MINMAX ),
113968 FUNCTION(max, -1, 1, 1, minmaxFunc ),
113969 FUNCTION(max, 0, 1, 1, 0 ),
113970 WAGGREGATE(max, 1, 1, 1, minmaxStep, minMaxFinalize, minMaxValue, 0,
113971 SQLITE_FUNC_MINMAX ),
113972 FUNCTION2(typeof, 1, 0, 0, typeofFunc, SQLITE_FUNC_TYPEOF),
113973 FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH),
113974 FUNCTION(instr, 2, 0, 0, instrFunc ),
113975 FUNCTION(printf, -1, 0, 0, printfFunc ),
113976 FUNCTION(unicode, 1, 0, 0, unicodeFunc ),
113977 FUNCTION(char, -1, 0, 0, charFunc ),
113978 FUNCTION(abs, 1, 0, 0, absFunc ),
113979#ifndef SQLITE_OMIT_FLOATING_POINT
113980 FUNCTION(round, 1, 0, 0, roundFunc ),
113981 FUNCTION(round, 2, 0, 0, roundFunc ),
113982#endif
113983 FUNCTION(upper, 1, 0, 0, upperFunc ),
113984 FUNCTION(lower, 1, 0, 0, lowerFunc ),
113985 FUNCTION(hex, 1, 0, 0, hexFunc ),
113986 FUNCTION2(ifnull, 2, 0, 0, noopFunc, SQLITE_FUNC_COALESCE),
113987 VFUNCTION(random, 0, 0, 0, randomFunc ),
113988 VFUNCTION(randomblob, 1, 0, 0, randomBlob ),
113989 FUNCTION(nullif, 2, 0, 1, nullifFunc ),
113990 DFUNCTION(sqlite_version, 0, 0, 0, versionFunc ),
113991 DFUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ),
113992 FUNCTION(sqlite_log, 2, 0, 0, errlogFunc ),
113993 FUNCTION(quote, 1, 0, 0, quoteFunc ),
113994 VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid),
113995 VFUNCTION(changes, 0, 0, 0, changes ),
113996 VFUNCTION(total_changes, 0, 0, 0, total_changes ),
113997 FUNCTION(replace, 3, 0, 0, replaceFunc ),
113998 FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ),
113999 FUNCTION(substr, 2, 0, 0, substrFunc ),
114000 FUNCTION(substr, 3, 0, 0, substrFunc ),
114001 WAGGREGATE(sum, 1,0,0, sumStep, sumFinalize, sumFinalize, sumInverse, 0),
114002 WAGGREGATE(total, 1,0,0, sumStep,totalFinalize,totalFinalize,sumInverse, 0),
114003 WAGGREGATE(avg, 1,0,0, sumStep, avgFinalize, avgFinalize, sumInverse, 0),
114004 WAGGREGATE(count, 0,0,0, countStep,
114005 countFinalize, countFinalize, countInverse, SQLITE_FUNC_COUNT ),
114006 WAGGREGATE(count, 1,0,0, countStep,
114007 countFinalize, countFinalize, countInverse, 0 ),
114008 WAGGREGATE(group_concat, 1, 0, 0, groupConcatStep,
114009 groupConcatFinalize, groupConcatValue, groupConcatInverse, 0),
114010 WAGGREGATE(group_concat, 2, 0, 0, groupConcatStep,
114011 groupConcatFinalize, groupConcatValue, groupConcatInverse, 0),
114012
114013 LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
114014#ifdef SQLITE_CASE_SENSITIVE_LIKE
114015 LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
114016 LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
114017#else
114018 LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
114019 LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
114020#endif
114021#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
114022 FUNCTION(unknown, -1, 0, 0, unknownFunc ),
114023#endif
114024 FUNCTION(coalesce, 1, 0, 0, 0 ),
114025 FUNCTION(coalesce, 0, 0, 0, 0 ),
114026 FUNCTION2(coalesce, -1, 0, 0, noopFunc, SQLITE_FUNC_COALESCE),
114027 };
114028#ifndef SQLITE_OMIT_ALTERTABLE
114029 sqlite3AlterFunctions();
114030#endif
114031 sqlite3WindowFunctions();
114032#if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4)
114033 sqlite3AnalyzeFunctions();
114034#endif
114035 sqlite3RegisterDateTimeFunctions();
114036 sqlite3InsertBuiltinFuncs(aBuiltinFunc, ArraySize(aBuiltinFunc));
114037
114038#if 0 /* Enable to print out how the built-in functions are hashed */
114039 {
114040 int i;
114041 FuncDef *p;
114042 for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
114043 printf("FUNC-HASH %02d:", i);
114044 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){
114045 int n = sqlite3Strlen30(p->zName);
114046 int h = p->zName[0] + n;
114047 printf(" %s(%d)", p->zName, h);
114048 }
114049 printf("\n");
114050 }
114051 }
114052#endif
114053}
114054
114055/************** End of func.c ************************************************/
114056/************** Begin file fkey.c ********************************************/
114057/*
114058**
114059** The author disclaims copyright to this source code. In place of
114060** a legal notice, here is a blessing:
114061**
114062** May you do good and not evil.
114063** May you find forgiveness for yourself and forgive others.
114064** May you share freely, never taking more than you give.
114065**
114066*************************************************************************
114067** This file contains code used by the compiler to add foreign key
114068** support to compiled SQL statements.
114069*/
114070/* #include "sqliteInt.h" */
114071
114072#ifndef SQLITE_OMIT_FOREIGN_KEY
114073#ifndef SQLITE_OMIT_TRIGGER
114074
114075/*
114076** Deferred and Immediate FKs
114077** --------------------------
114078**
114079** Foreign keys in SQLite come in two flavours: deferred and immediate.
114080** If an immediate foreign key constraint is violated,
114081** SQLITE_CONSTRAINT_FOREIGNKEY is returned and the current
114082** statement transaction rolled back. If a
114083** deferred foreign key constraint is violated, no action is taken
114084** immediately. However if the application attempts to commit the
114085** transaction before fixing the constraint violation, the attempt fails.
114086**
114087** Deferred constraints are implemented using a simple counter associated
114088** with the database handle. The counter is set to zero each time a
114089** database transaction is opened. Each time a statement is executed
114090** that causes a foreign key violation, the counter is incremented. Each
114091** time a statement is executed that removes an existing violation from
114092** the database, the counter is decremented. When the transaction is
114093** committed, the commit fails if the current value of the counter is
114094** greater than zero. This scheme has two big drawbacks:
114095**
114096** * When a commit fails due to a deferred foreign key constraint,
114097** there is no way to tell which foreign constraint is not satisfied,
114098** or which row it is not satisfied for.
114099**
114100** * If the database contains foreign key violations when the
114101** transaction is opened, this may cause the mechanism to malfunction.
114102**
114103** Despite these problems, this approach is adopted as it seems simpler
114104** than the alternatives.
114105**
114106** INSERT operations:
114107**
114108** I.1) For each FK for which the table is the child table, search
114109** the parent table for a match. If none is found increment the
114110** constraint counter.
114111**
114112** I.2) For each FK for which the table is the parent table,
114113** search the child table for rows that correspond to the new
114114** row in the parent table. Decrement the counter for each row
114115** found (as the constraint is now satisfied).
114116**
114117** DELETE operations:
114118**
114119** D.1) For each FK for which the table is the child table,
114120** search the parent table for a row that corresponds to the
114121** deleted row in the child table. If such a row is not found,
114122** decrement the counter.
114123**
114124** D.2) For each FK for which the table is the parent table, search
114125** the child table for rows that correspond to the deleted row
114126** in the parent table. For each found increment the counter.
114127**
114128** UPDATE operations:
114129**
114130** An UPDATE command requires that all 4 steps above are taken, but only
114131** for FK constraints for which the affected columns are actually
114132** modified (values must be compared at runtime).
114133**
114134** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2.
114135** This simplifies the implementation a bit.
114136**
114137** For the purposes of immediate FK constraints, the OR REPLACE conflict
114138** resolution is considered to delete rows before the new row is inserted.
114139** If a delete caused by OR REPLACE violates an FK constraint, an exception
114140** is thrown, even if the FK constraint would be satisfied after the new
114141** row is inserted.
114142**
114143** Immediate constraints are usually handled similarly. The only difference
114144** is that the counter used is stored as part of each individual statement
114145** object (struct Vdbe). If, after the statement has run, its immediate
114146** constraint counter is greater than zero,
114147** it returns SQLITE_CONSTRAINT_FOREIGNKEY
114148** and the statement transaction is rolled back. An exception is an INSERT
114149** statement that inserts a single row only (no triggers). In this case,
114150** instead of using a counter, an exception is thrown immediately if the
114151** INSERT violates a foreign key constraint. This is necessary as such
114152** an INSERT does not open a statement transaction.
114153**
114154** TODO: How should dropping a table be handled? How should renaming a
114155** table be handled?
114156**
114157**
114158** Query API Notes
114159** ---------------
114160**
114161** Before coding an UPDATE or DELETE row operation, the code-generator
114162** for those two operations needs to know whether or not the operation
114163** requires any FK processing and, if so, which columns of the original
114164** row are required by the FK processing VDBE code (i.e. if FKs were
114165** implemented using triggers, which of the old.* columns would be
114166** accessed). No information is required by the code-generator before
114167** coding an INSERT operation. The functions used by the UPDATE/DELETE
114168** generation code to query for this information are:
114169**
114170** sqlite3FkRequired() - Test to see if FK processing is required.
114171** sqlite3FkOldmask() - Query for the set of required old.* columns.
114172**
114173**
114174** Externally accessible module functions
114175** --------------------------------------
114176**
114177** sqlite3FkCheck() - Check for foreign key violations.
114178** sqlite3FkActions() - Code triggers for ON UPDATE/ON DELETE actions.
114179** sqlite3FkDelete() - Delete an FKey structure.
114180*/
114181
114182/*
114183** VDBE Calling Convention
114184** -----------------------
114185**
114186** Example:
114187**
114188** For the following INSERT statement:
114189**
114190** CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c);
114191** INSERT INTO t1 VALUES(1, 2, 3.1);
114192**
114193** Register (x): 2 (type integer)
114194** Register (x+1): 1 (type integer)
114195** Register (x+2): NULL (type NULL)
114196** Register (x+3): 3.1 (type real)
114197*/
114198
114199/*
114200** A foreign key constraint requires that the key columns in the parent
114201** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
114202** Given that pParent is the parent table for foreign key constraint pFKey,
114203** search the schema for a unique index on the parent key columns.
114204**
114205** If successful, zero is returned. If the parent key is an INTEGER PRIMARY
114206** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx
114207** is set to point to the unique index.
114208**
114209** If the parent key consists of a single column (the foreign key constraint
114210** is not a composite foreign key), output variable *paiCol is set to NULL.
114211** Otherwise, it is set to point to an allocated array of size N, where
114212** N is the number of columns in the parent key. The first element of the
114213** array is the index of the child table column that is mapped by the FK
114214** constraint to the parent table column stored in the left-most column
114215** of index *ppIdx. The second element of the array is the index of the
114216** child table column that corresponds to the second left-most column of
114217** *ppIdx, and so on.
114218**
114219** If the required index cannot be found, either because:
114220**
114221** 1) The named parent key columns do not exist, or
114222**
114223** 2) The named parent key columns do exist, but are not subject to a
114224** UNIQUE or PRIMARY KEY constraint, or
114225**
114226** 3) No parent key columns were provided explicitly as part of the
114227** foreign key definition, and the parent table does not have a
114228** PRIMARY KEY, or
114229**
114230** 4) No parent key columns were provided explicitly as part of the
114231** foreign key definition, and the PRIMARY KEY of the parent table
114232** consists of a different number of columns to the child key in
114233** the child table.
114234**
114235** then non-zero is returned, and a "foreign key mismatch" error loaded
114236** into pParse. If an OOM error occurs, non-zero is returned and the
114237** pParse->db->mallocFailed flag is set.
114238*/
114239SQLITE_PRIVATE int sqlite3FkLocateIndex(
114240 Parse *pParse, /* Parse context to store any error in */
114241 Table *pParent, /* Parent table of FK constraint pFKey */
114242 FKey *pFKey, /* Foreign key to find index for */
114243 Index **ppIdx, /* OUT: Unique index on parent table */
114244 int **paiCol /* OUT: Map of index columns in pFKey */
114245){
114246 Index *pIdx = 0; /* Value to return via *ppIdx */
114247 int *aiCol = 0; /* Value to return via *paiCol */
114248 int nCol = pFKey->nCol; /* Number of columns in parent key */
114249 char *zKey = pFKey->aCol[0].zCol; /* Name of left-most parent key column */
114250
114251 /* The caller is responsible for zeroing output parameters. */
114252 assert( ppIdx && *ppIdx==0 );
114253 assert( !paiCol || *paiCol==0 );
114254 assert( pParse );
114255
114256 /* If this is a non-composite (single column) foreign key, check if it
114257 ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx
114258 ** and *paiCol set to zero and return early.
114259 **
114260 ** Otherwise, for a composite foreign key (more than one column), allocate
114261 ** space for the aiCol array (returned via output parameter *paiCol).
114262 ** Non-composite foreign keys do not require the aiCol array.
114263 */
114264 if( nCol==1 ){
114265 /* The FK maps to the IPK if any of the following are true:
114266 **
114267 ** 1) There is an INTEGER PRIMARY KEY column and the FK is implicitly
114268 ** mapped to the primary key of table pParent, or
114269 ** 2) The FK is explicitly mapped to a column declared as INTEGER
114270 ** PRIMARY KEY.
114271 */
114272 if( pParent->iPKey>=0 ){
114273 if( !zKey ) return 0;
114274 if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
114275 }
114276 }else if( paiCol ){
114277 assert( nCol>1 );
114278 aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int));
114279 if( !aiCol ) return 1;
114280 *paiCol = aiCol;
114281 }
114282
114283 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
114284 if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) && pIdx->pPartIdxWhere==0 ){
114285 /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
114286 ** of columns. If each indexed column corresponds to a foreign key
114287 ** column of pFKey, then this index is a winner. */
114288
114289 if( zKey==0 ){
114290 /* If zKey is NULL, then this foreign key is implicitly mapped to
114291 ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be
114292 ** identified by the test. */
114293 if( IsPrimaryKeyIndex(pIdx) ){
114294 if( aiCol ){
114295 int i;
114296 for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
114297 }
114298 break;
114299 }
114300 }else{
114301 /* If zKey is non-NULL, then this foreign key was declared to
114302 ** map to an explicit list of columns in table pParent. Check if this
114303 ** index matches those columns. Also, check that the index uses
114304 ** the default collation sequences for each column. */
114305 int i, j;
114306 for(i=0; i<nCol; i++){
114307 i16 iCol = pIdx->aiColumn[i]; /* Index of column in parent tbl */
114308 const char *zDfltColl; /* Def. collation for column */
114309 char *zIdxCol; /* Name of indexed column */
114310
114311 if( iCol<0 ) break; /* No foreign keys against expression indexes */
114312
114313 /* If the index uses a collation sequence that is different from
114314 ** the default collation sequence for the column, this index is
114315 ** unusable. Bail out early in this case. */
114316 zDfltColl = pParent->aCol[iCol].zColl;
114317 if( !zDfltColl ) zDfltColl = sqlite3StrBINARY;
114318 if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
114319
114320 zIdxCol = pParent->aCol[iCol].zName;
114321 for(j=0; j<nCol; j++){
114322 if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
114323 if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
114324 break;
114325 }
114326 }
114327 if( j==nCol ) break;
114328 }
114329 if( i==nCol ) break; /* pIdx is usable */
114330 }
114331 }
114332 }
114333
114334 if( !pIdx ){
114335 if( !pParse->disableTriggers ){
114336 sqlite3ErrorMsg(pParse,
114337 "foreign key mismatch - \"%w\" referencing \"%w\"",
114338 pFKey->pFrom->zName, pFKey->zTo);
114339 }
114340 sqlite3DbFree(pParse->db, aiCol);
114341 return 1;
114342 }
114343
114344 *ppIdx = pIdx;
114345 return 0;
114346}
114347
114348/*
114349** This function is called when a row is inserted into or deleted from the
114350** child table of foreign key constraint pFKey. If an SQL UPDATE is executed
114351** on the child table of pFKey, this function is invoked twice for each row
114352** affected - once to "delete" the old row, and then again to "insert" the
114353** new row.
114354**
114355** Each time it is called, this function generates VDBE code to locate the
114356** row in the parent table that corresponds to the row being inserted into
114357** or deleted from the child table. If the parent row can be found, no
114358** special action is taken. Otherwise, if the parent row can *not* be
114359** found in the parent table:
114360**
114361** Operation | FK type | Action taken
114362** --------------------------------------------------------------------------
114363** INSERT immediate Increment the "immediate constraint counter".
114364**
114365** DELETE immediate Decrement the "immediate constraint counter".
114366**
114367** INSERT deferred Increment the "deferred constraint counter".
114368**
114369** DELETE deferred Decrement the "deferred constraint counter".
114370**
114371** These operations are identified in the comment at the top of this file
114372** (fkey.c) as "I.1" and "D.1".
114373*/
114374static void fkLookupParent(
114375 Parse *pParse, /* Parse context */
114376 int iDb, /* Index of database housing pTab */
114377 Table *pTab, /* Parent table of FK pFKey */
114378 Index *pIdx, /* Unique index on parent key columns in pTab */
114379 FKey *pFKey, /* Foreign key constraint */
114380 int *aiCol, /* Map from parent key columns to child table columns */
114381 int regData, /* Address of array containing child table row */
114382 int nIncr, /* Increment constraint counter by this */
114383 int isIgnore /* If true, pretend pTab contains all NULL values */
114384){
114385 int i; /* Iterator variable */
114386 Vdbe *v = sqlite3GetVdbe(pParse); /* Vdbe to add code to */
114387 int iCur = pParse->nTab - 1; /* Cursor number to use */
114388 int iOk = sqlite3VdbeMakeLabel(v); /* jump here if parent key found */
114389
114390 sqlite3VdbeVerifyAbortable(v,
114391 (!pFKey->isDeferred
114392 && !(pParse->db->flags & SQLITE_DeferFKs)
114393 && !pParse->pToplevel
114394 && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore);
114395
114396 /* If nIncr is less than zero, then check at runtime if there are any
114397 ** outstanding constraints to resolve. If there are not, there is no need
114398 ** to check if deleting this row resolves any outstanding violations.
114399 **
114400 ** Check if any of the key columns in the child table row are NULL. If
114401 ** any are, then the constraint is considered satisfied. No need to
114402 ** search for a matching row in the parent table. */
114403 if( nIncr<0 ){
114404 sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
114405 VdbeCoverage(v);
114406 }
114407 for(i=0; i<pFKey->nCol; i++){
114408 int iReg = aiCol[i] + regData + 1;
114409 sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iOk); VdbeCoverage(v);
114410 }
114411
114412 if( isIgnore==0 ){
114413 if( pIdx==0 ){
114414 /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
114415 ** column of the parent table (table pTab). */
114416 int iMustBeInt; /* Address of MustBeInt instruction */
114417 int regTemp = sqlite3GetTempReg(pParse);
114418
114419 /* Invoke MustBeInt to coerce the child key value to an integer (i.e.
114420 ** apply the affinity of the parent key). If this fails, then there
114421 ** is no matching parent key. Before using MustBeInt, make a copy of
114422 ** the value. Otherwise, the value inserted into the child key column
114423 ** will have INTEGER affinity applied to it, which may not be correct. */
114424 sqlite3VdbeAddOp2(v, OP_SCopy, aiCol[0]+1+regData, regTemp);
114425 iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt, regTemp, 0);
114426 VdbeCoverage(v);
114427
114428 /* If the parent table is the same as the child table, and we are about
114429 ** to increment the constraint-counter (i.e. this is an INSERT operation),
114430 ** then check if the row being inserted matches itself. If so, do not
114431 ** increment the constraint-counter. */
114432 if( pTab==pFKey->pFrom && nIncr==1 ){
114433 sqlite3VdbeAddOp3(v, OP_Eq, regData, iOk, regTemp); VdbeCoverage(v);
114434 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
114435 }
114436
114437 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
114438 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regTemp); VdbeCoverage(v);
114439 sqlite3VdbeGoto(v, iOk);
114440 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
114441 sqlite3VdbeJumpHere(v, iMustBeInt);
114442 sqlite3ReleaseTempReg(pParse, regTemp);
114443 }else{
114444 int nCol = pFKey->nCol;
114445 int regTemp = sqlite3GetTempRange(pParse, nCol);
114446 int regRec = sqlite3GetTempReg(pParse);
114447
114448 sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
114449 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
114450 for(i=0; i<nCol; i++){
114451 sqlite3VdbeAddOp2(v, OP_Copy, aiCol[i]+1+regData, regTemp+i);
114452 }
114453
114454 /* If the parent table is the same as the child table, and we are about
114455 ** to increment the constraint-counter (i.e. this is an INSERT operation),
114456 ** then check if the row being inserted matches itself. If so, do not
114457 ** increment the constraint-counter.
114458 **
114459 ** If any of the parent-key values are NULL, then the row cannot match
114460 ** itself. So set JUMPIFNULL to make sure we do the OP_Found if any
114461 ** of the parent-key values are NULL (at this point it is known that
114462 ** none of the child key values are).
114463 */
114464 if( pTab==pFKey->pFrom && nIncr==1 ){
114465 int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1;
114466 for(i=0; i<nCol; i++){
114467 int iChild = aiCol[i]+1+regData;
114468 int iParent = pIdx->aiColumn[i]+1+regData;
114469 assert( pIdx->aiColumn[i]>=0 );
114470 assert( aiCol[i]!=pTab->iPKey );
114471 if( pIdx->aiColumn[i]==pTab->iPKey ){
114472 /* The parent key is a composite key that includes the IPK column */
114473 iParent = regData;
114474 }
114475 sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent); VdbeCoverage(v);
114476 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
114477 }
114478 sqlite3VdbeGoto(v, iOk);
114479 }
114480
114481 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTemp, nCol, regRec,
114482 sqlite3IndexAffinityStr(pParse->db,pIdx), nCol);
114483 sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regRec, 0); VdbeCoverage(v);
114484
114485 sqlite3ReleaseTempReg(pParse, regRec);
114486 sqlite3ReleaseTempRange(pParse, regTemp, nCol);
114487 }
114488 }
114489
114490 if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
114491 && !pParse->pToplevel
114492 && !pParse->isMultiWrite
114493 ){
114494 /* Special case: If this is an INSERT statement that will insert exactly
114495 ** one row into the table, raise a constraint immediately instead of
114496 ** incrementing a counter. This is necessary as the VM code is being
114497 ** generated for will not open a statement transaction. */
114498 assert( nIncr==1 );
114499 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
114500 OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
114501 }else{
114502 if( nIncr>0 && pFKey->isDeferred==0 ){
114503 sqlite3MayAbort(pParse);
114504 }
114505 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
114506 }
114507
114508 sqlite3VdbeResolveLabel(v, iOk);
114509 sqlite3VdbeAddOp1(v, OP_Close, iCur);
114510}
114511
114512
114513/*
114514** Return an Expr object that refers to a memory register corresponding
114515** to column iCol of table pTab.
114516**
114517** regBase is the first of an array of register that contains the data
114518** for pTab. regBase itself holds the rowid. regBase+1 holds the first
114519** column. regBase+2 holds the second column, and so forth.
114520*/
114521static Expr *exprTableRegister(
114522 Parse *pParse, /* Parsing and code generating context */
114523 Table *pTab, /* The table whose content is at r[regBase]... */
114524 int regBase, /* Contents of table pTab */
114525 i16 iCol /* Which column of pTab is desired */
114526){
114527 Expr *pExpr;
114528 Column *pCol;
114529 const char *zColl;
114530 sqlite3 *db = pParse->db;
114531
114532 pExpr = sqlite3Expr(db, TK_REGISTER, 0);
114533 if( pExpr ){
114534 if( iCol>=0 && iCol!=pTab->iPKey ){
114535 pCol = &pTab->aCol[iCol];
114536 pExpr->iTable = regBase + iCol + 1;
114537 pExpr->affinity = pCol->affinity;
114538 zColl = pCol->zColl;
114539 if( zColl==0 ) zColl = db->pDfltColl->zName;
114540 pExpr = sqlite3ExprAddCollateString(pParse, pExpr, zColl);
114541 }else{
114542 pExpr->iTable = regBase;
114543 pExpr->affinity = SQLITE_AFF_INTEGER;
114544 }
114545 }
114546 return pExpr;
114547}
114548
114549/*
114550** Return an Expr object that refers to column iCol of table pTab which
114551** has cursor iCur.
114552*/
114553static Expr *exprTableColumn(
114554 sqlite3 *db, /* The database connection */
114555 Table *pTab, /* The table whose column is desired */
114556 int iCursor, /* The open cursor on the table */
114557 i16 iCol /* The column that is wanted */
114558){
114559 Expr *pExpr = sqlite3Expr(db, TK_COLUMN, 0);
114560 if( pExpr ){
114561 pExpr->y.pTab = pTab;
114562 pExpr->iTable = iCursor;
114563 pExpr->iColumn = iCol;
114564 }
114565 return pExpr;
114566}
114567
114568/*
114569** This function is called to generate code executed when a row is deleted
114570** from the parent table of foreign key constraint pFKey and, if pFKey is
114571** deferred, when a row is inserted into the same table. When generating
114572** code for an SQL UPDATE operation, this function may be called twice -
114573** once to "delete" the old row and once to "insert" the new row.
114574**
114575** Parameter nIncr is passed -1 when inserting a row (as this may decrease
114576** the number of FK violations in the db) or +1 when deleting one (as this
114577** may increase the number of FK constraint problems).
114578**
114579** The code generated by this function scans through the rows in the child
114580** table that correspond to the parent table row being deleted or inserted.
114581** For each child row found, one of the following actions is taken:
114582**
114583** Operation | FK type | Action taken
114584** --------------------------------------------------------------------------
114585** DELETE immediate Increment the "immediate constraint counter".
114586** Or, if the ON (UPDATE|DELETE) action is RESTRICT,
114587** throw a "FOREIGN KEY constraint failed" exception.
114588**
114589** INSERT immediate Decrement the "immediate constraint counter".
114590**
114591** DELETE deferred Increment the "deferred constraint counter".
114592** Or, if the ON (UPDATE|DELETE) action is RESTRICT,
114593** throw a "FOREIGN KEY constraint failed" exception.
114594**
114595** INSERT deferred Decrement the "deferred constraint counter".
114596**
114597** These operations are identified in the comment at the top of this file
114598** (fkey.c) as "I.2" and "D.2".
114599*/
114600static void fkScanChildren(
114601 Parse *pParse, /* Parse context */
114602 SrcList *pSrc, /* The child table to be scanned */
114603 Table *pTab, /* The parent table */
114604 Index *pIdx, /* Index on parent covering the foreign key */
114605 FKey *pFKey, /* The foreign key linking pSrc to pTab */
114606 int *aiCol, /* Map from pIdx cols to child table cols */
114607 int regData, /* Parent row data starts here */
114608 int nIncr /* Amount to increment deferred counter by */
114609){
114610 sqlite3 *db = pParse->db; /* Database handle */
114611 int i; /* Iterator variable */
114612 Expr *pWhere = 0; /* WHERE clause to scan with */
114613 NameContext sNameContext; /* Context used to resolve WHERE clause */
114614 WhereInfo *pWInfo; /* Context used by sqlite3WhereXXX() */
114615 int iFkIfZero = 0; /* Address of OP_FkIfZero */
114616 Vdbe *v = sqlite3GetVdbe(pParse);
114617
114618 assert( pIdx==0 || pIdx->pTable==pTab );
114619 assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
114620 assert( pIdx!=0 || pFKey->nCol==1 );
114621 assert( pIdx!=0 || HasRowid(pTab) );
114622
114623 if( nIncr<0 ){
114624 iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
114625 VdbeCoverage(v);
114626 }
114627
114628 /* Create an Expr object representing an SQL expression like:
114629 **
114630 ** <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
114631 **
114632 ** The collation sequence used for the comparison should be that of
114633 ** the parent key columns. The affinity of the parent key column should
114634 ** be applied to each child key value before the comparison takes place.
114635 */
114636 for(i=0; i<pFKey->nCol; i++){
114637 Expr *pLeft; /* Value from parent table row */
114638 Expr *pRight; /* Column ref to child table */
114639 Expr *pEq; /* Expression (pLeft = pRight) */
114640 i16 iCol; /* Index of column in child table */
114641 const char *zCol; /* Name of column in child table */
114642
114643 iCol = pIdx ? pIdx->aiColumn[i] : -1;
114644 pLeft = exprTableRegister(pParse, pTab, regData, iCol);
114645 iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
114646 assert( iCol>=0 );
114647 zCol = pFKey->pFrom->aCol[iCol].zName;
114648 pRight = sqlite3Expr(db, TK_ID, zCol);
114649 pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight);
114650 pWhere = sqlite3ExprAnd(db, pWhere, pEq);
114651 }
114652
114653 /* If the child table is the same as the parent table, then add terms
114654 ** to the WHERE clause that prevent this entry from being scanned.
114655 ** The added WHERE clause terms are like this:
114656 **
114657 ** $current_rowid!=rowid
114658 ** NOT( $current_a==a AND $current_b==b AND ... )
114659 **
114660 ** The first form is used for rowid tables. The second form is used
114661 ** for WITHOUT ROWID tables. In the second form, the primary key is
114662 ** (a,b,...)
114663 */
114664 if( pTab==pFKey->pFrom && nIncr>0 ){
114665 Expr *pNe; /* Expression (pLeft != pRight) */
114666 Expr *pLeft; /* Value from parent table row */
114667 Expr *pRight; /* Column ref to child table */
114668 if( HasRowid(pTab) ){
114669 pLeft = exprTableRegister(pParse, pTab, regData, -1);
114670 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
114671 pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight);
114672 }else{
114673 Expr *pEq, *pAll = 0;
114674 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
114675 assert( pIdx!=0 );
114676 for(i=0; i<pPk->nKeyCol; i++){
114677 i16 iCol = pIdx->aiColumn[i];
114678 assert( iCol>=0 );
114679 pLeft = exprTableRegister(pParse, pTab, regData, iCol);
114680 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, iCol);
114681 pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight);
114682 pAll = sqlite3ExprAnd(db, pAll, pEq);
114683 }
114684 pNe = sqlite3PExpr(pParse, TK_NOT, pAll, 0);
114685 }
114686 pWhere = sqlite3ExprAnd(db, pWhere, pNe);
114687 }
114688
114689 /* Resolve the references in the WHERE clause. */
114690 memset(&sNameContext, 0, sizeof(NameContext));
114691 sNameContext.pSrcList = pSrc;
114692 sNameContext.pParse = pParse;
114693 sqlite3ResolveExprNames(&sNameContext, pWhere);
114694
114695 /* Create VDBE to loop through the entries in pSrc that match the WHERE
114696 ** clause. For each row found, increment either the deferred or immediate
114697 ** foreign key constraint counter. */
114698 if( pParse->nErr==0 ){
114699 pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0);
114700 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
114701 if( pWInfo ){
114702 sqlite3WhereEnd(pWInfo);
114703 }
114704 }
114705
114706 /* Clean up the WHERE clause constructed above. */
114707 sqlite3ExprDelete(db, pWhere);
114708 if( iFkIfZero ){
114709 sqlite3VdbeJumpHere(v, iFkIfZero);
114710 }
114711}
114712
114713/*
114714** This function returns a linked list of FKey objects (connected by
114715** FKey.pNextTo) holding all children of table pTab. For example,
114716** given the following schema:
114717**
114718** CREATE TABLE t1(a PRIMARY KEY);
114719** CREATE TABLE t2(b REFERENCES t1(a);
114720**
114721** Calling this function with table "t1" as an argument returns a pointer
114722** to the FKey structure representing the foreign key constraint on table
114723** "t2". Calling this function with "t2" as the argument would return a
114724** NULL pointer (as there are no FK constraints for which t2 is the parent
114725** table).
114726*/
114727SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *pTab){
114728 return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName);
114729}
114730
114731/*
114732** The second argument is a Trigger structure allocated by the
114733** fkActionTrigger() routine. This function deletes the Trigger structure
114734** and all of its sub-components.
114735**
114736** The Trigger structure or any of its sub-components may be allocated from
114737** the lookaside buffer belonging to database handle dbMem.
114738*/
114739static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){
114740 if( p ){
114741 TriggerStep *pStep = p->step_list;
114742 sqlite3ExprDelete(dbMem, pStep->pWhere);
114743 sqlite3ExprListDelete(dbMem, pStep->pExprList);
114744 sqlite3SelectDelete(dbMem, pStep->pSelect);
114745 sqlite3ExprDelete(dbMem, p->pWhen);
114746 sqlite3DbFree(dbMem, p);
114747 }
114748}
114749
114750/*
114751** This function is called to generate code that runs when table pTab is
114752** being dropped from the database. The SrcList passed as the second argument
114753** to this function contains a single entry guaranteed to resolve to
114754** table pTab.
114755**
114756** Normally, no code is required. However, if either
114757**
114758** (a) The table is the parent table of a FK constraint, or
114759** (b) The table is the child table of a deferred FK constraint and it is
114760** determined at runtime that there are outstanding deferred FK
114761** constraint violations in the database,
114762**
114763** then the equivalent of "DELETE FROM <tbl>" is executed before dropping
114764** the table from the database. Triggers are disabled while running this
114765** DELETE, but foreign key actions are not.
114766*/
114767SQLITE_PRIVATE void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){
114768 sqlite3 *db = pParse->db;
114769 if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) ){
114770 int iSkip = 0;
114771 Vdbe *v = sqlite3GetVdbe(pParse);
114772
114773 assert( v ); /* VDBE has already been allocated */
114774 assert( pTab->pSelect==0 ); /* Not a view */
114775 if( sqlite3FkReferences(pTab)==0 ){
114776 /* Search for a deferred foreign key constraint for which this table
114777 ** is the child table. If one cannot be found, return without
114778 ** generating any VDBE code. If one can be found, then jump over
114779 ** the entire DELETE if there are no outstanding deferred constraints
114780 ** when this statement is run. */
114781 FKey *p;
114782 for(p=pTab->pFKey; p; p=p->pNextFrom){
114783 if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
114784 }
114785 if( !p ) return;
114786 iSkip = sqlite3VdbeMakeLabel(v);
114787 sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip); VdbeCoverage(v);
114788 }
114789
114790 pParse->disableTriggers = 1;
114791 sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0, 0, 0);
114792 pParse->disableTriggers = 0;
114793
114794 /* If the DELETE has generated immediate foreign key constraint
114795 ** violations, halt the VDBE and return an error at this point, before
114796 ** any modifications to the schema are made. This is because statement
114797 ** transactions are not able to rollback schema changes.
114798 **
114799 ** If the SQLITE_DeferFKs flag is set, then this is not required, as
114800 ** the statement transaction will not be rolled back even if FK
114801 ** constraints are violated.
114802 */
114803 if( (db->flags & SQLITE_DeferFKs)==0 ){
114804 sqlite3VdbeVerifyAbortable(v, OE_Abort);
114805 sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
114806 VdbeCoverage(v);
114807 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
114808 OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
114809 }
114810
114811 if( iSkip ){
114812 sqlite3VdbeResolveLabel(v, iSkip);
114813 }
114814 }
114815}
114816
114817
114818/*
114819** The second argument points to an FKey object representing a foreign key
114820** for which pTab is the child table. An UPDATE statement against pTab
114821** is currently being processed. For each column of the table that is
114822** actually updated, the corresponding element in the aChange[] array
114823** is zero or greater (if a column is unmodified the corresponding element
114824** is set to -1). If the rowid column is modified by the UPDATE statement
114825** the bChngRowid argument is non-zero.
114826**
114827** This function returns true if any of the columns that are part of the
114828** child key for FK constraint *p are modified.
114829*/
114830static int fkChildIsModified(
114831 Table *pTab, /* Table being updated */
114832 FKey *p, /* Foreign key for which pTab is the child */
114833 int *aChange, /* Array indicating modified columns */
114834 int bChngRowid /* True if rowid is modified by this update */
114835){
114836 int i;
114837 for(i=0; i<p->nCol; i++){
114838 int iChildKey = p->aCol[i].iFrom;
114839 if( aChange[iChildKey]>=0 ) return 1;
114840 if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
114841 }
114842 return 0;
114843}
114844
114845/*
114846** The second argument points to an FKey object representing a foreign key
114847** for which pTab is the parent table. An UPDATE statement against pTab
114848** is currently being processed. For each column of the table that is
114849** actually updated, the corresponding element in the aChange[] array
114850** is zero or greater (if a column is unmodified the corresponding element
114851** is set to -1). If the rowid column is modified by the UPDATE statement
114852** the bChngRowid argument is non-zero.
114853**
114854** This function returns true if any of the columns that are part of the
114855** parent key for FK constraint *p are modified.
114856*/
114857static int fkParentIsModified(
114858 Table *pTab,
114859 FKey *p,
114860 int *aChange,
114861 int bChngRowid
114862){
114863 int i;
114864 for(i=0; i<p->nCol; i++){
114865 char *zKey = p->aCol[i].zCol;
114866 int iKey;
114867 for(iKey=0; iKey<pTab->nCol; iKey++){
114868 if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
114869 Column *pCol = &pTab->aCol[iKey];
114870 if( zKey ){
114871 if( 0==sqlite3StrICmp(pCol->zName, zKey) ) return 1;
114872 }else if( pCol->colFlags & COLFLAG_PRIMKEY ){
114873 return 1;
114874 }
114875 }
114876 }
114877 }
114878 return 0;
114879}
114880
114881/*
114882** Return true if the parser passed as the first argument is being
114883** used to code a trigger that is really a "SET NULL" action belonging
114884** to trigger pFKey.
114885*/
114886static int isSetNullAction(Parse *pParse, FKey *pFKey){
114887 Parse *pTop = sqlite3ParseToplevel(pParse);
114888 if( pTop->pTriggerPrg ){
114889 Trigger *p = pTop->pTriggerPrg->pTrigger;
114890 if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull)
114891 || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull)
114892 ){
114893 return 1;
114894 }
114895 }
114896 return 0;
114897}
114898
114899/*
114900** This function is called when inserting, deleting or updating a row of
114901** table pTab to generate VDBE code to perform foreign key constraint
114902** processing for the operation.
114903**
114904** For a DELETE operation, parameter regOld is passed the index of the
114905** first register in an array of (pTab->nCol+1) registers containing the
114906** rowid of the row being deleted, followed by each of the column values
114907** of the row being deleted, from left to right. Parameter regNew is passed
114908** zero in this case.
114909**
114910** For an INSERT operation, regOld is passed zero and regNew is passed the
114911** first register of an array of (pTab->nCol+1) registers containing the new
114912** row data.
114913**
114914** For an UPDATE operation, this function is called twice. Once before
114915** the original record is deleted from the table using the calling convention
114916** described for DELETE. Then again after the original record is deleted
114917** but before the new record is inserted using the INSERT convention.
114918*/
114919SQLITE_PRIVATE void sqlite3FkCheck(
114920 Parse *pParse, /* Parse context */
114921 Table *pTab, /* Row is being deleted from this table */
114922 int regOld, /* Previous row data is stored here */
114923 int regNew, /* New row data is stored here */
114924 int *aChange, /* Array indicating UPDATEd columns (or 0) */
114925 int bChngRowid /* True if rowid is UPDATEd */
114926){
114927 sqlite3 *db = pParse->db; /* Database handle */
114928 FKey *pFKey; /* Used to iterate through FKs */
114929 int iDb; /* Index of database containing pTab */
114930 const char *zDb; /* Name of database containing pTab */
114931 int isIgnoreErrors = pParse->disableTriggers;
114932
114933 /* Exactly one of regOld and regNew should be non-zero. */
114934 assert( (regOld==0)!=(regNew==0) );
114935
114936 /* If foreign-keys are disabled, this function is a no-op. */
114937 if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
114938
114939 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
114940 zDb = db->aDb[iDb].zDbSName;
114941
114942 /* Loop through all the foreign key constraints for which pTab is the
114943 ** child table (the table that the foreign key definition is part of). */
114944 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
114945 Table *pTo; /* Parent table of foreign key pFKey */
114946 Index *pIdx = 0; /* Index on key columns in pTo */
114947 int *aiFree = 0;
114948 int *aiCol;
114949 int iCol;
114950 int i;
114951 int bIgnore = 0;
114952
114953 if( aChange
114954 && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
114955 && fkChildIsModified(pTab, pFKey, aChange, bChngRowid)==0
114956 ){
114957 continue;
114958 }
114959
114960 /* Find the parent table of this foreign key. Also find a unique index
114961 ** on the parent key columns in the parent table. If either of these
114962 ** schema items cannot be located, set an error in pParse and return
114963 ** early. */
114964 if( pParse->disableTriggers ){
114965 pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
114966 }else{
114967 pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
114968 }
114969 if( !pTo || sqlite3FkLocateIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
114970 assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) );
114971 if( !isIgnoreErrors || db->mallocFailed ) return;
114972 if( pTo==0 ){
114973 /* If isIgnoreErrors is true, then a table is being dropped. In this
114974 ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
114975 ** before actually dropping it in order to check FK constraints.
114976 ** If the parent table of an FK constraint on the current table is
114977 ** missing, behave as if it is empty. i.e. decrement the relevant
114978 ** FK counter for each row of the current table with non-NULL keys.
114979 */
114980 Vdbe *v = sqlite3GetVdbe(pParse);
114981 int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
114982 for(i=0; i<pFKey->nCol; i++){
114983 int iReg = pFKey->aCol[i].iFrom + regOld + 1;
114984 sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump); VdbeCoverage(v);
114985 }
114986 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
114987 }
114988 continue;
114989 }
114990 assert( pFKey->nCol==1 || (aiFree && pIdx) );
114991
114992 if( aiFree ){
114993 aiCol = aiFree;
114994 }else{
114995 iCol = pFKey->aCol[0].iFrom;
114996 aiCol = &iCol;
114997 }
114998 for(i=0; i<pFKey->nCol; i++){
114999 if( aiCol[i]==pTab->iPKey ){
115000 aiCol[i] = -1;
115001 }
115002 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
115003#ifndef SQLITE_OMIT_AUTHORIZATION
115004 /* Request permission to read the parent key columns. If the
115005 ** authorization callback returns SQLITE_IGNORE, behave as if any
115006 ** values read from the parent table are NULL. */
115007 if( db->xAuth ){
115008 int rcauth;
115009 char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName;
115010 rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
115011 bIgnore = (rcauth==SQLITE_IGNORE);
115012 }
115013#endif
115014 }
115015
115016 /* Take a shared-cache advisory read-lock on the parent table. Allocate
115017 ** a cursor to use to search the unique index on the parent key columns
115018 ** in the parent table. */
115019 sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
115020 pParse->nTab++;
115021
115022 if( regOld!=0 ){
115023 /* A row is being removed from the child table. Search for the parent.
115024 ** If the parent does not exist, removing the child row resolves an
115025 ** outstanding foreign key constraint violation. */
115026 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore);
115027 }
115028 if( regNew!=0 && !isSetNullAction(pParse, pFKey) ){
115029 /* A row is being added to the child table. If a parent row cannot
115030 ** be found, adding the child row has violated the FK constraint.
115031 **
115032 ** If this operation is being performed as part of a trigger program
115033 ** that is actually a "SET NULL" action belonging to this very
115034 ** foreign key, then omit this scan altogether. As all child key
115035 ** values are guaranteed to be NULL, it is not possible for adding
115036 ** this row to cause an FK violation. */
115037 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1, bIgnore);
115038 }
115039
115040 sqlite3DbFree(db, aiFree);
115041 }
115042
115043 /* Loop through all the foreign key constraints that refer to this table.
115044 ** (the "child" constraints) */
115045 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
115046 Index *pIdx = 0; /* Foreign key index for pFKey */
115047 SrcList *pSrc;
115048 int *aiCol = 0;
115049
115050 if( aChange && fkParentIsModified(pTab, pFKey, aChange, bChngRowid)==0 ){
115051 continue;
115052 }
115053
115054 if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs)
115055 && !pParse->pToplevel && !pParse->isMultiWrite
115056 ){
115057 assert( regOld==0 && regNew!=0 );
115058 /* Inserting a single row into a parent table cannot cause (or fix)
115059 ** an immediate foreign key violation. So do nothing in this case. */
115060 continue;
115061 }
115062
115063 if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
115064 if( !isIgnoreErrors || db->mallocFailed ) return;
115065 continue;
115066 }
115067 assert( aiCol || pFKey->nCol==1 );
115068
115069 /* Create a SrcList structure containing the child table. We need the
115070 ** child table as a SrcList for sqlite3WhereBegin() */
115071 pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
115072 if( pSrc ){
115073 struct SrcList_item *pItem = pSrc->a;
115074 pItem->pTab = pFKey->pFrom;
115075 pItem->zName = pFKey->pFrom->zName;
115076 pItem->pTab->nTabRef++;
115077 pItem->iCursor = pParse->nTab++;
115078
115079 if( regNew!=0 ){
115080 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
115081 }
115082 if( regOld!=0 ){
115083 int eAction = pFKey->aAction[aChange!=0];
115084 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1);
115085 /* If this is a deferred FK constraint, or a CASCADE or SET NULL
115086 ** action applies, then any foreign key violations caused by
115087 ** removing the parent key will be rectified by the action trigger.
115088 ** So do not set the "may-abort" flag in this case.
115089 **
115090 ** Note 1: If the FK is declared "ON UPDATE CASCADE", then the
115091 ** may-abort flag will eventually be set on this statement anyway
115092 ** (when this function is called as part of processing the UPDATE
115093 ** within the action trigger).
115094 **
115095 ** Note 2: At first glance it may seem like SQLite could simply omit
115096 ** all OP_FkCounter related scans when either CASCADE or SET NULL
115097 ** applies. The trouble starts if the CASCADE or SET NULL action
115098 ** trigger causes other triggers or action rules attached to the
115099 ** child table to fire. In these cases the fk constraint counters
115100 ** might be set incorrectly if any OP_FkCounter related scans are
115101 ** omitted. */
115102 if( !pFKey->isDeferred && eAction!=OE_Cascade && eAction!=OE_SetNull ){
115103 sqlite3MayAbort(pParse);
115104 }
115105 }
115106 pItem->zName = 0;
115107 sqlite3SrcListDelete(db, pSrc);
115108 }
115109 sqlite3DbFree(db, aiCol);
115110 }
115111}
115112
115113#define COLUMN_MASK(x) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))
115114
115115/*
115116** This function is called before generating code to update or delete a
115117** row contained in table pTab.
115118*/
115119SQLITE_PRIVATE u32 sqlite3FkOldmask(
115120 Parse *pParse, /* Parse context */
115121 Table *pTab /* Table being modified */
115122){
115123 u32 mask = 0;
115124 if( pParse->db->flags&SQLITE_ForeignKeys ){
115125 FKey *p;
115126 int i;
115127 for(p=pTab->pFKey; p; p=p->pNextFrom){
115128 for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
115129 }
115130 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
115131 Index *pIdx = 0;
115132 sqlite3FkLocateIndex(pParse, pTab, p, &pIdx, 0);
115133 if( pIdx ){
115134 for(i=0; i<pIdx->nKeyCol; i++){
115135 assert( pIdx->aiColumn[i]>=0 );
115136 mask |= COLUMN_MASK(pIdx->aiColumn[i]);
115137 }
115138 }
115139 }
115140 }
115141 return mask;
115142}
115143
115144
115145/*
115146** This function is called before generating code to update or delete a
115147** row contained in table pTab. If the operation is a DELETE, then
115148** parameter aChange is passed a NULL value. For an UPDATE, aChange points
115149** to an array of size N, where N is the number of columns in table pTab.
115150** If the i'th column is not modified by the UPDATE, then the corresponding
115151** entry in the aChange[] array is set to -1. If the column is modified,
115152** the value is 0 or greater. Parameter chngRowid is set to true if the
115153** UPDATE statement modifies the rowid fields of the table.
115154**
115155** If any foreign key processing will be required, this function returns
115156** non-zero. If there is no foreign key related processing, this function
115157** returns zero.
115158**
115159** For an UPDATE, this function returns 2 if:
115160**
115161** * There are any FKs for which pTab is the child and the parent table, or
115162** * the UPDATE modifies one or more parent keys for which the action is
115163** not "NO ACTION" (i.e. is CASCADE, SET DEFAULT or SET NULL).
115164**
115165** Or, assuming some other foreign key processing is required, 1.
115166*/
115167SQLITE_PRIVATE int sqlite3FkRequired(
115168 Parse *pParse, /* Parse context */
115169 Table *pTab, /* Table being modified */
115170 int *aChange, /* Non-NULL for UPDATE operations */
115171 int chngRowid /* True for UPDATE that affects rowid */
115172){
115173 int eRet = 0;
115174 if( pParse->db->flags&SQLITE_ForeignKeys ){
115175 if( !aChange ){
115176 /* A DELETE operation. Foreign key processing is required if the
115177 ** table in question is either the child or parent table for any
115178 ** foreign key constraint. */
115179 eRet = (sqlite3FkReferences(pTab) || pTab->pFKey);
115180 }else{
115181 /* This is an UPDATE. Foreign key processing is only required if the
115182 ** operation modifies one or more child or parent key columns. */
115183 FKey *p;
115184
115185 /* Check if any child key columns are being modified. */
115186 for(p=pTab->pFKey; p; p=p->pNextFrom){
115187 if( 0==sqlite3_stricmp(pTab->zName, p->zTo) ) return 2;
115188 if( fkChildIsModified(pTab, p, aChange, chngRowid) ){
115189 eRet = 1;
115190 }
115191 }
115192
115193 /* Check if any parent key columns are being modified. */
115194 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
115195 if( fkParentIsModified(pTab, p, aChange, chngRowid) ){
115196 if( p->aAction[1]!=OE_None ) return 2;
115197 eRet = 1;
115198 }
115199 }
115200 }
115201 }
115202 return eRet;
115203}
115204
115205/*
115206** This function is called when an UPDATE or DELETE operation is being
115207** compiled on table pTab, which is the parent table of foreign-key pFKey.
115208** If the current operation is an UPDATE, then the pChanges parameter is
115209** passed a pointer to the list of columns being modified. If it is a
115210** DELETE, pChanges is passed a NULL pointer.
115211**
115212** It returns a pointer to a Trigger structure containing a trigger
115213** equivalent to the ON UPDATE or ON DELETE action specified by pFKey.
115214** If the action is "NO ACTION" or "RESTRICT", then a NULL pointer is
115215** returned (these actions require no special handling by the triggers
115216** sub-system, code for them is created by fkScanChildren()).
115217**
115218** For example, if pFKey is the foreign key and pTab is table "p" in
115219** the following schema:
115220**
115221** CREATE TABLE p(pk PRIMARY KEY);
115222** CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE);
115223**
115224** then the returned trigger structure is equivalent to:
115225**
115226** CREATE TRIGGER ... DELETE ON p BEGIN
115227** DELETE FROM c WHERE ck = old.pk;
115228** END;
115229**
115230** The returned pointer is cached as part of the foreign key object. It
115231** is eventually freed along with the rest of the foreign key object by
115232** sqlite3FkDelete().
115233*/
115234static Trigger *fkActionTrigger(
115235 Parse *pParse, /* Parse context */
115236 Table *pTab, /* Table being updated or deleted from */
115237 FKey *pFKey, /* Foreign key to get action for */
115238 ExprList *pChanges /* Change-list for UPDATE, NULL for DELETE */
115239){
115240 sqlite3 *db = pParse->db; /* Database handle */
115241 int action; /* One of OE_None, OE_Cascade etc. */
115242 Trigger *pTrigger; /* Trigger definition to return */
115243 int iAction = (pChanges!=0); /* 1 for UPDATE, 0 for DELETE */
115244
115245 action = pFKey->aAction[iAction];
115246 if( action==OE_Restrict && (db->flags & SQLITE_DeferFKs) ){
115247 return 0;
115248 }
115249 pTrigger = pFKey->apTrigger[iAction];
115250
115251 if( action!=OE_None && !pTrigger ){
115252 char const *zFrom; /* Name of child table */
115253 int nFrom; /* Length in bytes of zFrom */
115254 Index *pIdx = 0; /* Parent key index for this FK */
115255 int *aiCol = 0; /* child table cols -> parent key cols */
115256 TriggerStep *pStep = 0; /* First (only) step of trigger program */
115257 Expr *pWhere = 0; /* WHERE clause of trigger step */
115258 ExprList *pList = 0; /* Changes list if ON UPDATE CASCADE */
115259 Select *pSelect = 0; /* If RESTRICT, "SELECT RAISE(...)" */
115260 int i; /* Iterator variable */
115261 Expr *pWhen = 0; /* WHEN clause for the trigger */
115262
115263 if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
115264 assert( aiCol || pFKey->nCol==1 );
115265
115266 for(i=0; i<pFKey->nCol; i++){
115267 Token tOld = { "old", 3 }; /* Literal "old" token */
115268 Token tNew = { "new", 3 }; /* Literal "new" token */
115269 Token tFromCol; /* Name of column in child table */
115270 Token tToCol; /* Name of column in parent table */
115271 int iFromCol; /* Idx of column in child table */
115272 Expr *pEq; /* tFromCol = OLD.tToCol */
115273
115274 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
115275 assert( iFromCol>=0 );
115276 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
115277 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
115278 sqlite3TokenInit(&tToCol,
115279 pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zName);
115280 sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zName);
115281
115282 /* Create the expression "OLD.zToCol = zFromCol". It is important
115283 ** that the "OLD.zToCol" term is on the LHS of the = operator, so
115284 ** that the affinity and collation sequence associated with the
115285 ** parent table are used for the comparison. */
115286 pEq = sqlite3PExpr(pParse, TK_EQ,
115287 sqlite3PExpr(pParse, TK_DOT,
115288 sqlite3ExprAlloc(db, TK_ID, &tOld, 0),
115289 sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)),
115290 sqlite3ExprAlloc(db, TK_ID, &tFromCol, 0)
115291 );
115292 pWhere = sqlite3ExprAnd(db, pWhere, pEq);
115293
115294 /* For ON UPDATE, construct the next term of the WHEN clause.
115295 ** The final WHEN clause will be like this:
115296 **
115297 ** WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
115298 */
115299 if( pChanges ){
115300 pEq = sqlite3PExpr(pParse, TK_IS,
115301 sqlite3PExpr(pParse, TK_DOT,
115302 sqlite3ExprAlloc(db, TK_ID, &tOld, 0),
115303 sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)),
115304 sqlite3PExpr(pParse, TK_DOT,
115305 sqlite3ExprAlloc(db, TK_ID, &tNew, 0),
115306 sqlite3ExprAlloc(db, TK_ID, &tToCol, 0))
115307 );
115308 pWhen = sqlite3ExprAnd(db, pWhen, pEq);
115309 }
115310
115311 if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){
115312 Expr *pNew;
115313 if( action==OE_Cascade ){
115314 pNew = sqlite3PExpr(pParse, TK_DOT,
115315 sqlite3ExprAlloc(db, TK_ID, &tNew, 0),
115316 sqlite3ExprAlloc(db, TK_ID, &tToCol, 0));
115317 }else if( action==OE_SetDflt ){
115318 Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt;
115319 if( pDflt ){
115320 pNew = sqlite3ExprDup(db, pDflt, 0);
115321 }else{
115322 pNew = sqlite3ExprAlloc(db, TK_NULL, 0, 0);
115323 }
115324 }else{
115325 pNew = sqlite3ExprAlloc(db, TK_NULL, 0, 0);
115326 }
115327 pList = sqlite3ExprListAppend(pParse, pList, pNew);
115328 sqlite3ExprListSetName(pParse, pList, &tFromCol, 0);
115329 }
115330 }
115331 sqlite3DbFree(db, aiCol);
115332
115333 zFrom = pFKey->pFrom->zName;
115334 nFrom = sqlite3Strlen30(zFrom);
115335
115336 if( action==OE_Restrict ){
115337 Token tFrom;
115338 Expr *pRaise;
115339
115340 tFrom.z = zFrom;
115341 tFrom.n = nFrom;
115342 pRaise = sqlite3Expr(db, TK_RAISE, "FOREIGN KEY constraint failed");
115343 if( pRaise ){
115344 pRaise->affinity = OE_Abort;
115345 }
115346 pSelect = sqlite3SelectNew(pParse,
115347 sqlite3ExprListAppend(pParse, 0, pRaise),
115348 sqlite3SrcListAppend(db, 0, &tFrom, 0),
115349 pWhere,
115350 0, 0, 0, 0, 0
115351 );
115352 pWhere = 0;
115353 }
115354
115355 /* Disable lookaside memory allocation */
115356 db->lookaside.bDisable++;
115357
115358 pTrigger = (Trigger *)sqlite3DbMallocZero(db,
115359 sizeof(Trigger) + /* struct Trigger */
115360 sizeof(TriggerStep) + /* Single step in trigger program */
115361 nFrom + 1 /* Space for pStep->zTarget */
115362 );
115363 if( pTrigger ){
115364 pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
115365 pStep->zTarget = (char *)&pStep[1];
115366 memcpy((char *)pStep->zTarget, zFrom, nFrom);
115367
115368 pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
115369 pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
115370 pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
115371 if( pWhen ){
115372 pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0);
115373 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
115374 }
115375 }
115376
115377 /* Re-enable the lookaside buffer, if it was disabled earlier. */
115378 db->lookaside.bDisable--;
115379
115380 sqlite3ExprDelete(db, pWhere);
115381 sqlite3ExprDelete(db, pWhen);
115382 sqlite3ExprListDelete(db, pList);
115383 sqlite3SelectDelete(db, pSelect);
115384 if( db->mallocFailed==1 ){
115385 fkTriggerDelete(db, pTrigger);
115386 return 0;
115387 }
115388 assert( pStep!=0 );
115389
115390 switch( action ){
115391 case OE_Restrict:
115392 pStep->op = TK_SELECT;
115393 break;
115394 case OE_Cascade:
115395 if( !pChanges ){
115396 pStep->op = TK_DELETE;
115397 break;
115398 }
115399 default:
115400 pStep->op = TK_UPDATE;
115401 }
115402 pStep->pTrig = pTrigger;
115403 pTrigger->pSchema = pTab->pSchema;
115404 pTrigger->pTabSchema = pTab->pSchema;
115405 pFKey->apTrigger[iAction] = pTrigger;
115406 pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
115407 }
115408
115409 return pTrigger;
115410}
115411
115412/*
115413** This function is called when deleting or updating a row to implement
115414** any required CASCADE, SET NULL or SET DEFAULT actions.
115415*/
115416SQLITE_PRIVATE void sqlite3FkActions(
115417 Parse *pParse, /* Parse context */
115418 Table *pTab, /* Table being updated or deleted from */
115419 ExprList *pChanges, /* Change-list for UPDATE, NULL for DELETE */
115420 int regOld, /* Address of array containing old row */
115421 int *aChange, /* Array indicating UPDATEd columns (or 0) */
115422 int bChngRowid /* True if rowid is UPDATEd */
115423){
115424 /* If foreign-key support is enabled, iterate through all FKs that
115425 ** refer to table pTab. If there is an action associated with the FK
115426 ** for this operation (either update or delete), invoke the associated
115427 ** trigger sub-program. */
115428 if( pParse->db->flags&SQLITE_ForeignKeys ){
115429 FKey *pFKey; /* Iterator variable */
115430 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
115431 if( aChange==0 || fkParentIsModified(pTab, pFKey, aChange, bChngRowid) ){
115432 Trigger *pAct = fkActionTrigger(pParse, pTab, pFKey, pChanges);
115433 if( pAct ){
115434 sqlite3CodeRowTriggerDirect(pParse, pAct, pTab, regOld, OE_Abort, 0);
115435 }
115436 }
115437 }
115438 }
115439}
115440
115441#endif /* ifndef SQLITE_OMIT_TRIGGER */
115442
115443/*
115444** Free all memory associated with foreign key definitions attached to
115445** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
115446** hash table.
115447*/
115448SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){
115449 FKey *pFKey; /* Iterator variable */
115450 FKey *pNext; /* Copy of pFKey->pNextFrom */
115451
115452 assert( db==0 || IsVirtual(pTab)
115453 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
115454 for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
115455
115456 /* Remove the FK from the fkeyHash hash table. */
115457 if( !db || db->pnBytesFreed==0 ){
115458 if( pFKey->pPrevTo ){
115459 pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
115460 }else{
115461 void *p = (void *)pFKey->pNextTo;
115462 const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
115463 sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, p);
115464 }
115465 if( pFKey->pNextTo ){
115466 pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
115467 }
115468 }
115469
115470 /* EV: R-30323-21917 Each foreign key constraint in SQLite is
115471 ** classified as either immediate or deferred.
115472 */
115473 assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
115474
115475 /* Delete any triggers created to implement actions for this FK. */
115476#ifndef SQLITE_OMIT_TRIGGER
115477 fkTriggerDelete(db, pFKey->apTrigger[0]);
115478 fkTriggerDelete(db, pFKey->apTrigger[1]);
115479#endif
115480
115481 pNext = pFKey->pNextFrom;
115482 sqlite3DbFree(db, pFKey);
115483 }
115484}
115485#endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */
115486
115487/************** End of fkey.c ************************************************/
115488/************** Begin file insert.c ******************************************/
115489/*
115490** 2001 September 15
115491**
115492** The author disclaims copyright to this source code. In place of
115493** a legal notice, here is a blessing:
115494**
115495** May you do good and not evil.
115496** May you find forgiveness for yourself and forgive others.
115497** May you share freely, never taking more than you give.
115498**
115499*************************************************************************
115500** This file contains C code routines that are called by the parser
115501** to handle INSERT statements in SQLite.
115502*/
115503/* #include "sqliteInt.h" */
115504
115505/*
115506** Generate code that will
115507**
115508** (1) acquire a lock for table pTab then
115509** (2) open pTab as cursor iCur.
115510**
115511** If pTab is a WITHOUT ROWID table, then it is the PRIMARY KEY index
115512** for that table that is actually opened.
115513*/
115514SQLITE_PRIVATE void sqlite3OpenTable(
115515 Parse *pParse, /* Generate code into this VDBE */
115516 int iCur, /* The cursor number of the table */
115517 int iDb, /* The database index in sqlite3.aDb[] */
115518 Table *pTab, /* The table to be opened */
115519 int opcode /* OP_OpenRead or OP_OpenWrite */
115520){
115521 Vdbe *v;
115522 assert( !IsVirtual(pTab) );
115523 v = sqlite3GetVdbe(pParse);
115524 assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
115525 sqlite3TableLock(pParse, iDb, pTab->tnum,
115526 (opcode==OP_OpenWrite)?1:0, pTab->zName);
115527 if( HasRowid(pTab) ){
115528 sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nCol);
115529 VdbeComment((v, "%s", pTab->zName));
115530 }else{
115531 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
115532 assert( pPk!=0 );
115533 assert( pPk->tnum==pTab->tnum );
115534 sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
115535 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
115536 VdbeComment((v, "%s", pTab->zName));
115537 }
115538}
115539
115540/*
115541** Return a pointer to the column affinity string associated with index
115542** pIdx. A column affinity string has one character for each column in
115543** the table, according to the affinity of the column:
115544**
115545** Character Column affinity
115546** ------------------------------
115547** 'A' BLOB
115548** 'B' TEXT
115549** 'C' NUMERIC
115550** 'D' INTEGER
115551** 'F' REAL
115552**
115553** An extra 'D' is appended to the end of the string to cover the
115554** rowid that appears as the last column in every index.
115555**
115556** Memory for the buffer containing the column index affinity string
115557** is managed along with the rest of the Index structure. It will be
115558** released when sqlite3DeleteIndex() is called.
115559*/
115560SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){
115561 if( !pIdx->zColAff ){
115562 /* The first time a column affinity string for a particular index is
115563 ** required, it is allocated and populated here. It is then stored as
115564 ** a member of the Index structure for subsequent use.
115565 **
115566 ** The column affinity string will eventually be deleted by
115567 ** sqliteDeleteIndex() when the Index structure itself is cleaned
115568 ** up.
115569 */
115570 int n;
115571 Table *pTab = pIdx->pTable;
115572 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
115573 if( !pIdx->zColAff ){
115574 sqlite3OomFault(db);
115575 return 0;
115576 }
115577 for(n=0; n<pIdx->nColumn; n++){
115578 i16 x = pIdx->aiColumn[n];
115579 if( x>=0 ){
115580 pIdx->zColAff[n] = pTab->aCol[x].affinity;
115581 }else if( x==XN_ROWID ){
115582 pIdx->zColAff[n] = SQLITE_AFF_INTEGER;
115583 }else{
115584 char aff;
115585 assert( x==XN_EXPR );
115586 assert( pIdx->aColExpr!=0 );
115587 aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
115588 if( aff==0 ) aff = SQLITE_AFF_BLOB;
115589 pIdx->zColAff[n] = aff;
115590 }
115591 }
115592 pIdx->zColAff[n] = 0;
115593 }
115594
115595 return pIdx->zColAff;
115596}
115597
115598/*
115599** Compute the affinity string for table pTab, if it has not already been
115600** computed. As an optimization, omit trailing SQLITE_AFF_BLOB affinities.
115601**
115602** If the affinity exists (if it is no entirely SQLITE_AFF_BLOB values) and
115603** if iReg>0 then code an OP_Affinity opcode that will set the affinities
115604** for register iReg and following. Or if affinities exists and iReg==0,
115605** then just set the P4 operand of the previous opcode (which should be
115606** an OP_MakeRecord) to the affinity string.
115607**
115608** A column affinity string has one character per column:
115609**
115610** Character Column affinity
115611** ------------------------------
115612** 'A' BLOB
115613** 'B' TEXT
115614** 'C' NUMERIC
115615** 'D' INTEGER
115616** 'E' REAL
115617*/
115618SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
115619 int i;
115620 char *zColAff = pTab->zColAff;
115621 if( zColAff==0 ){
115622 sqlite3 *db = sqlite3VdbeDb(v);
115623 zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
115624 if( !zColAff ){
115625 sqlite3OomFault(db);
115626 return;
115627 }
115628
115629 for(i=0; i<pTab->nCol; i++){
115630 zColAff[i] = pTab->aCol[i].affinity;
115631 }
115632 do{
115633 zColAff[i--] = 0;
115634 }while( i>=0 && zColAff[i]==SQLITE_AFF_BLOB );
115635 pTab->zColAff = zColAff;
115636 }
115637 assert( zColAff!=0 );
115638 i = sqlite3Strlen30NN(zColAff);
115639 if( i ){
115640 if( iReg ){
115641 sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i);
115642 }else{
115643 sqlite3VdbeChangeP4(v, -1, zColAff, i);
115644 }
115645 }
115646}
115647
115648/*
115649** Return non-zero if the table pTab in database iDb or any of its indices
115650** have been opened at any point in the VDBE program. This is used to see if
115651** a statement of the form "INSERT INTO <iDb, pTab> SELECT ..." can
115652** run without using a temporary table for the results of the SELECT.
115653*/
115654static int readsTable(Parse *p, int iDb, Table *pTab){
115655 Vdbe *v = sqlite3GetVdbe(p);
115656 int i;
115657 int iEnd = sqlite3VdbeCurrentAddr(v);
115658#ifndef SQLITE_OMIT_VIRTUALTABLE
115659 VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
115660#endif
115661
115662 for(i=1; i<iEnd; i++){
115663 VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
115664 assert( pOp!=0 );
115665 if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
115666 Index *pIndex;
115667 int tnum = pOp->p2;
115668 if( tnum==pTab->tnum ){
115669 return 1;
115670 }
115671 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
115672 if( tnum==pIndex->tnum ){
115673 return 1;
115674 }
115675 }
115676 }
115677#ifndef SQLITE_OMIT_VIRTUALTABLE
115678 if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
115679 assert( pOp->p4.pVtab!=0 );
115680 assert( pOp->p4type==P4_VTAB );
115681 return 1;
115682 }
115683#endif
115684 }
115685 return 0;
115686}
115687
115688#ifndef SQLITE_OMIT_AUTOINCREMENT
115689/*
115690** Locate or create an AutoincInfo structure associated with table pTab
115691** which is in database iDb. Return the register number for the register
115692** that holds the maximum rowid. Return zero if pTab is not an AUTOINCREMENT
115693** table. (Also return zero when doing a VACUUM since we do not want to
115694** update the AUTOINCREMENT counters during a VACUUM.)
115695**
115696** There is at most one AutoincInfo structure per table even if the
115697** same table is autoincremented multiple times due to inserts within
115698** triggers. A new AutoincInfo structure is created if this is the
115699** first use of table pTab. On 2nd and subsequent uses, the original
115700** AutoincInfo structure is used.
115701**
115702** Four consecutive registers are allocated:
115703**
115704** (1) The name of the pTab table.
115705** (2) The maximum ROWID of pTab.
115706** (3) The rowid in sqlite_sequence of pTab
115707** (4) The original value of the max ROWID in pTab, or NULL if none
115708**
115709** The 2nd register is the one that is returned. That is all the
115710** insert routine needs to know about.
115711*/
115712static int autoIncBegin(
115713 Parse *pParse, /* Parsing context */
115714 int iDb, /* Index of the database holding pTab */
115715 Table *pTab /* The table we are writing to */
115716){
115717 int memId = 0; /* Register holding maximum rowid */
115718 assert( pParse->db->aDb[iDb].pSchema!=0 );
115719 if( (pTab->tabFlags & TF_Autoincrement)!=0
115720 && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
115721 ){
115722 Parse *pToplevel = sqlite3ParseToplevel(pParse);
115723 AutoincInfo *pInfo;
115724 Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
115725
115726 /* Verify that the sqlite_sequence table exists and is an ordinary
115727 ** rowid table with exactly two columns.
115728 ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
115729 if( pSeqTab==0
115730 || !HasRowid(pSeqTab)
115731 || IsVirtual(pSeqTab)
115732 || pSeqTab->nCol!=2
115733 ){
115734 pParse->nErr++;
115735 pParse->rc = SQLITE_CORRUPT_SEQUENCE;
115736 return 0;
115737 }
115738
115739 pInfo = pToplevel->pAinc;
115740 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
115741 if( pInfo==0 ){
115742 pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
115743 if( pInfo==0 ) return 0;
115744 pInfo->pNext = pToplevel->pAinc;
115745 pToplevel->pAinc = pInfo;
115746 pInfo->pTab = pTab;
115747 pInfo->iDb = iDb;
115748 pToplevel->nMem++; /* Register to hold name of table */
115749 pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
115750 pToplevel->nMem +=2; /* Rowid in sqlite_sequence + orig max val */
115751 }
115752 memId = pInfo->regCtr;
115753 }
115754 return memId;
115755}
115756
115757/*
115758** This routine generates code that will initialize all of the
115759** register used by the autoincrement tracker.
115760*/
115761SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){
115762 AutoincInfo *p; /* Information about an AUTOINCREMENT */
115763 sqlite3 *db = pParse->db; /* The database connection */
115764 Db *pDb; /* Database only autoinc table */
115765 int memId; /* Register holding max rowid */
115766 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
115767
115768 /* This routine is never called during trigger-generation. It is
115769 ** only called from the top-level */
115770 assert( pParse->pTriggerTab==0 );
115771 assert( sqlite3IsToplevel(pParse) );
115772
115773 assert( v ); /* We failed long ago if this is not so */
115774 for(p = pParse->pAinc; p; p = p->pNext){
115775 static const int iLn = VDBE_OFFSET_LINENO(2);
115776 static const VdbeOpList autoInc[] = {
115777 /* 0 */ {OP_Null, 0, 0, 0},
115778 /* 1 */ {OP_Rewind, 0, 10, 0},
115779 /* 2 */ {OP_Column, 0, 0, 0},
115780 /* 3 */ {OP_Ne, 0, 9, 0},
115781 /* 4 */ {OP_Rowid, 0, 0, 0},
115782 /* 5 */ {OP_Column, 0, 1, 0},
115783 /* 6 */ {OP_AddImm, 0, 0, 0},
115784 /* 7 */ {OP_Copy, 0, 0, 0},
115785 /* 8 */ {OP_Goto, 0, 11, 0},
115786 /* 9 */ {OP_Next, 0, 2, 0},
115787 /* 10 */ {OP_Integer, 0, 0, 0},
115788 /* 11 */ {OP_Close, 0, 0, 0}
115789 };
115790 VdbeOp *aOp;
115791 pDb = &db->aDb[p->iDb];
115792 memId = p->regCtr;
115793 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
115794 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
115795 sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
115796 aOp = sqlite3VdbeAddOpList(v, ArraySize(autoInc), autoInc, iLn);
115797 if( aOp==0 ) break;
115798 aOp[0].p2 = memId;
115799 aOp[0].p3 = memId+2;
115800 aOp[2].p3 = memId;
115801 aOp[3].p1 = memId-1;
115802 aOp[3].p3 = memId;
115803 aOp[3].p5 = SQLITE_JUMPIFNULL;
115804 aOp[4].p2 = memId+1;
115805 aOp[5].p3 = memId;
115806 aOp[6].p1 = memId;
115807 aOp[7].p2 = memId+2;
115808 aOp[7].p1 = memId;
115809 aOp[10].p2 = memId;
115810 }
115811}
115812
115813/*
115814** Update the maximum rowid for an autoincrement calculation.
115815**
115816** This routine should be called when the regRowid register holds a
115817** new rowid that is about to be inserted. If that new rowid is
115818** larger than the maximum rowid in the memId memory cell, then the
115819** memory cell is updated.
115820*/
115821static void autoIncStep(Parse *pParse, int memId, int regRowid){
115822 if( memId>0 ){
115823 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
115824 }
115825}
115826
115827/*
115828** This routine generates the code needed to write autoincrement
115829** maximum rowid values back into the sqlite_sequence register.
115830** Every statement that might do an INSERT into an autoincrement
115831** table (either directly or through triggers) needs to call this
115832** routine just before the "exit" code.
115833*/
115834static SQLITE_NOINLINE void autoIncrementEnd(Parse *pParse){
115835 AutoincInfo *p;
115836 Vdbe *v = pParse->pVdbe;
115837 sqlite3 *db = pParse->db;
115838
115839 assert( v );
115840 for(p = pParse->pAinc; p; p = p->pNext){
115841 static const int iLn = VDBE_OFFSET_LINENO(2);
115842 static const VdbeOpList autoIncEnd[] = {
115843 /* 0 */ {OP_NotNull, 0, 2, 0},
115844 /* 1 */ {OP_NewRowid, 0, 0, 0},
115845 /* 2 */ {OP_MakeRecord, 0, 2, 0},
115846 /* 3 */ {OP_Insert, 0, 0, 0},
115847 /* 4 */ {OP_Close, 0, 0, 0}
115848 };
115849 VdbeOp *aOp;
115850 Db *pDb = &db->aDb[p->iDb];
115851 int iRec;
115852 int memId = p->regCtr;
115853
115854 iRec = sqlite3GetTempReg(pParse);
115855 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
115856 sqlite3VdbeAddOp3(v, OP_Le, memId+2, sqlite3VdbeCurrentAddr(v)+7, memId);
115857 VdbeCoverage(v);
115858 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
115859 aOp = sqlite3VdbeAddOpList(v, ArraySize(autoIncEnd), autoIncEnd, iLn);
115860 if( aOp==0 ) break;
115861 aOp[0].p1 = memId+1;
115862 aOp[1].p2 = memId+1;
115863 aOp[2].p1 = memId-1;
115864 aOp[2].p3 = iRec;
115865 aOp[3].p2 = iRec;
115866 aOp[3].p3 = memId+1;
115867 aOp[3].p5 = OPFLAG_APPEND;
115868 sqlite3ReleaseTempReg(pParse, iRec);
115869 }
115870}
115871SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
115872 if( pParse->pAinc ) autoIncrementEnd(pParse);
115873}
115874#else
115875/*
115876** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
115877** above are all no-ops
115878*/
115879# define autoIncBegin(A,B,C) (0)
115880# define autoIncStep(A,B,C)
115881#endif /* SQLITE_OMIT_AUTOINCREMENT */
115882
115883
115884/* Forward declaration */
115885static int xferOptimization(
115886 Parse *pParse, /* Parser context */
115887 Table *pDest, /* The table we are inserting into */
115888 Select *pSelect, /* A SELECT statement to use as the data source */
115889 int onError, /* How to handle constraint errors */
115890 int iDbDest /* The database of pDest */
115891);
115892
115893/*
115894** This routine is called to handle SQL of the following forms:
115895**
115896** insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),...
115897** insert into TABLE (IDLIST) select
115898** insert into TABLE (IDLIST) default values
115899**
115900** The IDLIST following the table name is always optional. If omitted,
115901** then a list of all (non-hidden) columns for the table is substituted.
115902** The IDLIST appears in the pColumn parameter. pColumn is NULL if IDLIST
115903** is omitted.
115904**
115905** For the pSelect parameter holds the values to be inserted for the
115906** first two forms shown above. A VALUES clause is really just short-hand
115907** for a SELECT statement that omits the FROM clause and everything else
115908** that follows. If the pSelect parameter is NULL, that means that the
115909** DEFAULT VALUES form of the INSERT statement is intended.
115910**
115911** The code generated follows one of four templates. For a simple
115912** insert with data coming from a single-row VALUES clause, the code executes
115913** once straight down through. Pseudo-code follows (we call this
115914** the "1st template"):
115915**
115916** open write cursor to <table> and its indices
115917** put VALUES clause expressions into registers
115918** write the resulting record into <table>
115919** cleanup
115920**
115921** The three remaining templates assume the statement is of the form
115922**
115923** INSERT INTO <table> SELECT ...
115924**
115925** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
115926** in other words if the SELECT pulls all columns from a single table
115927** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
115928** if <table2> and <table1> are distinct tables but have identical
115929** schemas, including all the same indices, then a special optimization
115930** is invoked that copies raw records from <table2> over to <table1>.
115931** See the xferOptimization() function for the implementation of this
115932** template. This is the 2nd template.
115933**
115934** open a write cursor to <table>
115935** open read cursor on <table2>
115936** transfer all records in <table2> over to <table>
115937** close cursors
115938** foreach index on <table>
115939** open a write cursor on the <table> index
115940** open a read cursor on the corresponding <table2> index
115941** transfer all records from the read to the write cursors
115942** close cursors
115943** end foreach
115944**
115945** The 3rd template is for when the second template does not apply
115946** and the SELECT clause does not read from <table> at any time.
115947** The generated code follows this template:
115948**
115949** X <- A
115950** goto B
115951** A: setup for the SELECT
115952** loop over the rows in the SELECT
115953** load values into registers R..R+n
115954** yield X
115955** end loop
115956** cleanup after the SELECT
115957** end-coroutine X
115958** B: open write cursor to <table> and its indices
115959** C: yield X, at EOF goto D
115960** insert the select result into <table> from R..R+n
115961** goto C
115962** D: cleanup
115963**
115964** The 4th template is used if the insert statement takes its
115965** values from a SELECT but the data is being inserted into a table
115966** that is also read as part of the SELECT. In the third form,
115967** we have to use an intermediate table to store the results of
115968** the select. The template is like this:
115969**
115970** X <- A
115971** goto B
115972** A: setup for the SELECT
115973** loop over the tables in the SELECT
115974** load value into register R..R+n
115975** yield X
115976** end loop
115977** cleanup after the SELECT
115978** end co-routine R
115979** B: open temp table
115980** L: yield X, at EOF goto M
115981** insert row from R..R+n into temp table
115982** goto L
115983** M: open write cursor to <table> and its indices
115984** rewind temp table
115985** C: loop over rows of intermediate table
115986** transfer values form intermediate table into <table>
115987** end loop
115988** D: cleanup
115989*/
115990SQLITE_PRIVATE void sqlite3Insert(
115991 Parse *pParse, /* Parser context */
115992 SrcList *pTabList, /* Name of table into which we are inserting */
115993 Select *pSelect, /* A SELECT statement to use as the data source */
115994 IdList *pColumn, /* Column names corresponding to IDLIST. */
115995 int onError, /* How to handle constraint errors */
115996 Upsert *pUpsert /* ON CONFLICT clauses for upsert, or NULL */
115997){
115998 sqlite3 *db; /* The main database structure */
115999 Table *pTab; /* The table to insert into. aka TABLE */
116000 int i, j; /* Loop counters */
116001 Vdbe *v; /* Generate code into this virtual machine */
116002 Index *pIdx; /* For looping over indices of the table */
116003 int nColumn; /* Number of columns in the data */
116004 int nHidden = 0; /* Number of hidden columns if TABLE is virtual */
116005 int iDataCur = 0; /* VDBE cursor that is the main data repository */
116006 int iIdxCur = 0; /* First index cursor */
116007 int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
116008 int endOfLoop; /* Label for the end of the insertion loop */
116009 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */
116010 int addrInsTop = 0; /* Jump to label "D" */
116011 int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */
116012 SelectDest dest; /* Destination for SELECT on rhs of INSERT */
116013 int iDb; /* Index of database holding TABLE */
116014 u8 useTempTable = 0; /* Store SELECT results in intermediate table */
116015 u8 appendFlag = 0; /* True if the insert is likely to be an append */
116016 u8 withoutRowid; /* 0 for normal table. 1 for WITHOUT ROWID table */
116017 u8 bIdListInOrder; /* True if IDLIST is in table order */
116018 ExprList *pList = 0; /* List of VALUES() to be inserted */
116019
116020 /* Register allocations */
116021 int regFromSelect = 0;/* Base register for data coming from SELECT */
116022 int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */
116023 int regRowCount = 0; /* Memory cell used for the row counter */
116024 int regIns; /* Block of regs holding rowid+data being inserted */
116025 int regRowid; /* registers holding insert rowid */
116026 int regData; /* register holding first column to insert */
116027 int *aRegIdx = 0; /* One register allocated to each index */
116028
116029#ifndef SQLITE_OMIT_TRIGGER
116030 int isView; /* True if attempting to insert into a view */
116031 Trigger *pTrigger; /* List of triggers on pTab, if required */
116032 int tmask; /* Mask of trigger times */
116033#endif
116034
116035 db = pParse->db;
116036 if( pParse->nErr || db->mallocFailed ){
116037 goto insert_cleanup;
116038 }
116039 dest.iSDParm = 0; /* Suppress a harmless compiler warning */
116040
116041 /* If the Select object is really just a simple VALUES() list with a
116042 ** single row (the common case) then keep that one row of values
116043 ** and discard the other (unused) parts of the pSelect object
116044 */
116045 if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
116046 pList = pSelect->pEList;
116047 pSelect->pEList = 0;
116048 sqlite3SelectDelete(db, pSelect);
116049 pSelect = 0;
116050 }
116051
116052 /* Locate the table into which we will be inserting new information.
116053 */
116054 assert( pTabList->nSrc==1 );
116055 pTab = sqlite3SrcListLookup(pParse, pTabList);
116056 if( pTab==0 ){
116057 goto insert_cleanup;
116058 }
116059 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
116060 assert( iDb<db->nDb );
116061 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0,
116062 db->aDb[iDb].zDbSName) ){
116063 goto insert_cleanup;
116064 }
116065 withoutRowid = !HasRowid(pTab);
116066
116067 /* Figure out if we have any triggers and if the table being
116068 ** inserted into is a view
116069 */
116070#ifndef SQLITE_OMIT_TRIGGER
116071 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
116072 isView = pTab->pSelect!=0;
116073#else
116074# define pTrigger 0
116075# define tmask 0
116076# define isView 0
116077#endif
116078#ifdef SQLITE_OMIT_VIEW
116079# undef isView
116080# define isView 0
116081#endif
116082 assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
116083
116084 /* If pTab is really a view, make sure it has been initialized.
116085 ** ViewGetColumnNames() is a no-op if pTab is not a view.
116086 */
116087 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
116088 goto insert_cleanup;
116089 }
116090
116091 /* Cannot insert into a read-only table.
116092 */
116093 if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
116094 goto insert_cleanup;
116095 }
116096
116097 /* Allocate a VDBE
116098 */
116099 v = sqlite3GetVdbe(pParse);
116100 if( v==0 ) goto insert_cleanup;
116101 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
116102 sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
116103
116104#ifndef SQLITE_OMIT_XFER_OPT
116105 /* If the statement is of the form
116106 **
116107 ** INSERT INTO <table1> SELECT * FROM <table2>;
116108 **
116109 ** Then special optimizations can be applied that make the transfer
116110 ** very fast and which reduce fragmentation of indices.
116111 **
116112 ** This is the 2nd template.
116113 */
116114 if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
116115 assert( !pTrigger );
116116 assert( pList==0 );
116117 goto insert_end;
116118 }
116119#endif /* SQLITE_OMIT_XFER_OPT */
116120
116121 /* If this is an AUTOINCREMENT table, look up the sequence number in the
116122 ** sqlite_sequence table and store it in memory cell regAutoinc.
116123 */
116124 regAutoinc = autoIncBegin(pParse, iDb, pTab);
116125
116126 /* Allocate registers for holding the rowid of the new row,
116127 ** the content of the new row, and the assembled row record.
116128 */
116129 regRowid = regIns = pParse->nMem+1;
116130 pParse->nMem += pTab->nCol + 1;
116131 if( IsVirtual(pTab) ){
116132 regRowid++;
116133 pParse->nMem++;
116134 }
116135 regData = regRowid+1;
116136
116137 /* If the INSERT statement included an IDLIST term, then make sure
116138 ** all elements of the IDLIST really are columns of the table and
116139 ** remember the column indices.
116140 **
116141 ** If the table has an INTEGER PRIMARY KEY column and that column
116142 ** is named in the IDLIST, then record in the ipkColumn variable
116143 ** the index into IDLIST of the primary key column. ipkColumn is
116144 ** the index of the primary key as it appears in IDLIST, not as
116145 ** is appears in the original table. (The index of the INTEGER
116146 ** PRIMARY KEY in the original table is pTab->iPKey.)
116147 */
116148 bIdListInOrder = (pTab->tabFlags & TF_OOOHidden)==0;
116149 if( pColumn ){
116150 for(i=0; i<pColumn->nId; i++){
116151 pColumn->a[i].idx = -1;
116152 }
116153 for(i=0; i<pColumn->nId; i++){
116154 for(j=0; j<pTab->nCol; j++){
116155 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
116156 pColumn->a[i].idx = j;
116157 if( i!=j ) bIdListInOrder = 0;
116158 if( j==pTab->iPKey ){
116159 ipkColumn = i; assert( !withoutRowid );
116160 }
116161 break;
116162 }
116163 }
116164 if( j>=pTab->nCol ){
116165 if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
116166 ipkColumn = i;
116167 bIdListInOrder = 0;
116168 }else{
116169 sqlite3ErrorMsg(pParse, "table %S has no column named %s",
116170 pTabList, 0, pColumn->a[i].zName);
116171 pParse->checkSchema = 1;
116172 goto insert_cleanup;
116173 }
116174 }
116175 }
116176 }
116177
116178 /* Figure out how many columns of data are supplied. If the data
116179 ** is coming from a SELECT statement, then generate a co-routine that
116180 ** produces a single row of the SELECT on each invocation. The
116181 ** co-routine is the common header to the 3rd and 4th templates.
116182 */
116183 if( pSelect ){
116184 /* Data is coming from a SELECT or from a multi-row VALUES clause.
116185 ** Generate a co-routine to run the SELECT. */
116186 int regYield; /* Register holding co-routine entry-point */
116187 int addrTop; /* Top of the co-routine */
116188 int rc; /* Result code */
116189
116190 regYield = ++pParse->nMem;
116191 addrTop = sqlite3VdbeCurrentAddr(v) + 1;
116192 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
116193 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
116194 dest.iSdst = bIdListInOrder ? regData : 0;
116195 dest.nSdst = pTab->nCol;
116196 rc = sqlite3Select(pParse, pSelect, &dest);
116197 regFromSelect = dest.iSdst;
116198 if( rc || db->mallocFailed || pParse->nErr ) goto insert_cleanup;
116199 sqlite3VdbeEndCoroutine(v, regYield);
116200 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
116201 assert( pSelect->pEList );
116202 nColumn = pSelect->pEList->nExpr;
116203
116204 /* Set useTempTable to TRUE if the result of the SELECT statement
116205 ** should be written into a temporary table (template 4). Set to
116206 ** FALSE if each output row of the SELECT can be written directly into
116207 ** the destination table (template 3).
116208 **
116209 ** A temp table must be used if the table being updated is also one
116210 ** of the tables being read by the SELECT statement. Also use a
116211 ** temp table in the case of row triggers.
116212 */
116213 if( pTrigger || readsTable(pParse, iDb, pTab) ){
116214 useTempTable = 1;
116215 }
116216
116217 if( useTempTable ){
116218 /* Invoke the coroutine to extract information from the SELECT
116219 ** and add it to a transient table srcTab. The code generated
116220 ** here is from the 4th template:
116221 **
116222 ** B: open temp table
116223 ** L: yield X, goto M at EOF
116224 ** insert row from R..R+n into temp table
116225 ** goto L
116226 ** M: ...
116227 */
116228 int regRec; /* Register to hold packed record */
116229 int regTempRowid; /* Register to hold temp table ROWID */
116230 int addrL; /* Label "L" */
116231
116232 srcTab = pParse->nTab++;
116233 regRec = sqlite3GetTempReg(pParse);
116234 regTempRowid = sqlite3GetTempReg(pParse);
116235 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
116236 addrL = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); VdbeCoverage(v);
116237 sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
116238 sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
116239 sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
116240 sqlite3VdbeGoto(v, addrL);
116241 sqlite3VdbeJumpHere(v, addrL);
116242 sqlite3ReleaseTempReg(pParse, regRec);
116243 sqlite3ReleaseTempReg(pParse, regTempRowid);
116244 }
116245 }else{
116246 /* This is the case if the data for the INSERT is coming from a
116247 ** single-row VALUES clause
116248 */
116249 NameContext sNC;
116250 memset(&sNC, 0, sizeof(sNC));
116251 sNC.pParse = pParse;
116252 srcTab = -1;
116253 assert( useTempTable==0 );
116254 if( pList ){
116255 nColumn = pList->nExpr;
116256 if( sqlite3ResolveExprListNames(&sNC, pList) ){
116257 goto insert_cleanup;
116258 }
116259 }else{
116260 nColumn = 0;
116261 }
116262 }
116263
116264 /* If there is no IDLIST term but the table has an integer primary
116265 ** key, the set the ipkColumn variable to the integer primary key
116266 ** column index in the original table definition.
116267 */
116268 if( pColumn==0 && nColumn>0 ){
116269 ipkColumn = pTab->iPKey;
116270 }
116271
116272 /* Make sure the number of columns in the source data matches the number
116273 ** of columns to be inserted into the table.
116274 */
116275 for(i=0; i<pTab->nCol; i++){
116276 nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
116277 }
116278 if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
116279 sqlite3ErrorMsg(pParse,
116280 "table %S has %d columns but %d values were supplied",
116281 pTabList, 0, pTab->nCol-nHidden, nColumn);
116282 goto insert_cleanup;
116283 }
116284 if( pColumn!=0 && nColumn!=pColumn->nId ){
116285 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
116286 goto insert_cleanup;
116287 }
116288
116289 /* Initialize the count of rows to be inserted
116290 */
116291 if( (db->flags & SQLITE_CountRows)!=0
116292 && !pParse->nested
116293 && !pParse->pTriggerTab
116294 ){
116295 regRowCount = ++pParse->nMem;
116296 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
116297 }
116298
116299 /* If this is not a view, open the table and and all indices */
116300 if( !isView ){
116301 int nIdx;
116302 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
116303 &iDataCur, &iIdxCur);
116304 aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+1));
116305 if( aRegIdx==0 ){
116306 goto insert_cleanup;
116307 }
116308 for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
116309 assert( pIdx );
116310 aRegIdx[i] = ++pParse->nMem;
116311 pParse->nMem += pIdx->nColumn;
116312 }
116313 }
116314#ifndef SQLITE_OMIT_UPSERT
116315 if( pUpsert ){
116316 pTabList->a[0].iCursor = iDataCur;
116317 pUpsert->pUpsertSrc = pTabList;
116318 pUpsert->regData = regData;
116319 pUpsert->iDataCur = iDataCur;
116320 pUpsert->iIdxCur = iIdxCur;
116321 if( pUpsert->pUpsertTarget ){
116322 sqlite3UpsertAnalyzeTarget(pParse, pTabList, pUpsert);
116323 }
116324 }
116325#endif
116326
116327
116328 /* This is the top of the main insertion loop */
116329 if( useTempTable ){
116330 /* This block codes the top of loop only. The complete loop is the
116331 ** following pseudocode (template 4):
116332 **
116333 ** rewind temp table, if empty goto D
116334 ** C: loop over rows of intermediate table
116335 ** transfer values form intermediate table into <table>
116336 ** end loop
116337 ** D: ...
116338 */
116339 addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab); VdbeCoverage(v);
116340 addrCont = sqlite3VdbeCurrentAddr(v);
116341 }else if( pSelect ){
116342 /* This block codes the top of loop only. The complete loop is the
116343 ** following pseudocode (template 3):
116344 **
116345 ** C: yield X, at EOF goto D
116346 ** insert the select result into <table> from R..R+n
116347 ** goto C
116348 ** D: ...
116349 */
116350 addrInsTop = addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
116351 VdbeCoverage(v);
116352 }
116353
116354 /* Run the BEFORE and INSTEAD OF triggers, if there are any
116355 */
116356 endOfLoop = sqlite3VdbeMakeLabel(v);
116357 if( tmask & TRIGGER_BEFORE ){
116358 int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
116359
116360 /* build the NEW.* reference row. Note that if there is an INTEGER
116361 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
116362 ** translated into a unique ID for the row. But on a BEFORE trigger,
116363 ** we do not know what the unique ID will be (because the insert has
116364 ** not happened yet) so we substitute a rowid of -1
116365 */
116366 if( ipkColumn<0 ){
116367 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
116368 }else{
116369 int addr1;
116370 assert( !withoutRowid );
116371 if( useTempTable ){
116372 sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regCols);
116373 }else{
116374 assert( pSelect==0 ); /* Otherwise useTempTable is true */
116375 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
116376 }
116377 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols); VdbeCoverage(v);
116378 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
116379 sqlite3VdbeJumpHere(v, addr1);
116380 sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols); VdbeCoverage(v);
116381 }
116382
116383 /* Cannot have triggers on a virtual table. If it were possible,
116384 ** this block would have to account for hidden column.
116385 */
116386 assert( !IsVirtual(pTab) );
116387
116388 /* Create the new column data
116389 */
116390 for(i=j=0; i<pTab->nCol; i++){
116391 if( pColumn ){
116392 for(j=0; j<pColumn->nId; j++){
116393 if( pColumn->a[j].idx==i ) break;
116394 }
116395 }
116396 if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId)
116397 || (pColumn==0 && IsOrdinaryHiddenColumn(&pTab->aCol[i])) ){
116398 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
116399 }else if( useTempTable ){
116400 sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1);
116401 }else{
116402 assert( pSelect==0 ); /* Otherwise useTempTable is true */
116403 sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
116404 }
116405 if( pColumn==0 && !IsOrdinaryHiddenColumn(&pTab->aCol[i]) ) j++;
116406 }
116407
116408 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
116409 ** do not attempt any conversions before assembling the record.
116410 ** If this is a real table, attempt conversions as required by the
116411 ** table column affinities.
116412 */
116413 if( !isView ){
116414 sqlite3TableAffinity(v, pTab, regCols+1);
116415 }
116416
116417 /* Fire BEFORE or INSTEAD OF triggers */
116418 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE,
116419 pTab, regCols-pTab->nCol-1, onError, endOfLoop);
116420
116421 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
116422 }
116423
116424 /* Compute the content of the next row to insert into a range of
116425 ** registers beginning at regIns.
116426 */
116427 if( !isView ){
116428 if( IsVirtual(pTab) ){
116429 /* The row that the VUpdate opcode will delete: none */
116430 sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
116431 }
116432 if( ipkColumn>=0 ){
116433 if( useTempTable ){
116434 sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regRowid);
116435 }else if( pSelect ){
116436 sqlite3VdbeAddOp2(v, OP_Copy, regFromSelect+ipkColumn, regRowid);
116437 }else{
116438 VdbeOp *pOp;
116439 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
116440 pOp = sqlite3VdbeGetOp(v, -1);
116441 assert( pOp!=0 );
116442 if( pOp->opcode==OP_Null && !IsVirtual(pTab) ){
116443 appendFlag = 1;
116444 pOp->opcode = OP_NewRowid;
116445 pOp->p1 = iDataCur;
116446 pOp->p2 = regRowid;
116447 pOp->p3 = regAutoinc;
116448 }
116449 }
116450 /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
116451 ** to generate a unique primary key value.
116452 */
116453 if( !appendFlag ){
116454 int addr1;
116455 if( !IsVirtual(pTab) ){
116456 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); VdbeCoverage(v);
116457 sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
116458 sqlite3VdbeJumpHere(v, addr1);
116459 }else{
116460 addr1 = sqlite3VdbeCurrentAddr(v);
116461 sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, addr1+2); VdbeCoverage(v);
116462 }
116463 sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); VdbeCoverage(v);
116464 }
116465 }else if( IsVirtual(pTab) || withoutRowid ){
116466 sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
116467 }else{
116468 sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
116469 appendFlag = 1;
116470 }
116471 autoIncStep(pParse, regAutoinc, regRowid);
116472
116473 /* Compute data for all columns of the new entry, beginning
116474 ** with the first column.
116475 */
116476 nHidden = 0;
116477 for(i=0; i<pTab->nCol; i++){
116478 int iRegStore = regRowid+1+i;
116479 if( i==pTab->iPKey ){
116480 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
116481 ** Whenever this column is read, the rowid will be substituted
116482 ** in its place. Hence, fill this column with a NULL to avoid
116483 ** taking up data space with information that will never be used.
116484 ** As there may be shallow copies of this value, make it a soft-NULL */
116485 sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore);
116486 continue;
116487 }
116488 if( pColumn==0 ){
116489 if( IsHiddenColumn(&pTab->aCol[i]) ){
116490 j = -1;
116491 nHidden++;
116492 }else{
116493 j = i - nHidden;
116494 }
116495 }else{
116496 for(j=0; j<pColumn->nId; j++){
116497 if( pColumn->a[j].idx==i ) break;
116498 }
116499 }
116500 if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
116501 sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
116502 }else if( useTempTable ){
116503 sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore);
116504 }else if( pSelect ){
116505 if( regFromSelect!=regData ){
116506 sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore);
116507 }
116508 }else{
116509 sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore);
116510 }
116511 }
116512
116513 /* Generate code to check constraints and generate index keys and
116514 ** do the insertion.
116515 */
116516#ifndef SQLITE_OMIT_VIRTUALTABLE
116517 if( IsVirtual(pTab) ){
116518 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
116519 sqlite3VtabMakeWritable(pParse, pTab);
116520 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
116521 sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
116522 sqlite3MayAbort(pParse);
116523 }else
116524#endif
116525 {
116526 int isReplace; /* Set to true if constraints may cause a replace */
116527 int bUseSeek; /* True to use OPFLAG_SEEKRESULT */
116528 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
116529 regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0, pUpsert
116530 );
116531 sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
116532
116533 /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE
116534 ** constraints or (b) there are no triggers and this table is not a
116535 ** parent table in a foreign key constraint. It is safe to set the
116536 ** flag in the second case as if any REPLACE constraint is hit, an
116537 ** OP_Delete or OP_IdxDelete instruction will be executed on each
116538 ** cursor that is disturbed. And these instructions both clear the
116539 ** VdbeCursor.seekResult variable, disabling the OPFLAG_USESEEKRESULT
116540 ** functionality. */
116541 bUseSeek = (isReplace==0 || (pTrigger==0 &&
116542 ((db->flags & SQLITE_ForeignKeys)==0 || sqlite3FkReferences(pTab)==0)
116543 ));
116544 sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
116545 regIns, aRegIdx, 0, appendFlag, bUseSeek
116546 );
116547 }
116548 }
116549
116550 /* Update the count of rows that are inserted
116551 */
116552 if( regRowCount ){
116553 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
116554 }
116555
116556 if( pTrigger ){
116557 /* Code AFTER triggers */
116558 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER,
116559 pTab, regData-2-pTab->nCol, onError, endOfLoop);
116560 }
116561
116562 /* The bottom of the main insertion loop, if the data source
116563 ** is a SELECT statement.
116564 */
116565 sqlite3VdbeResolveLabel(v, endOfLoop);
116566 if( useTempTable ){
116567 sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); VdbeCoverage(v);
116568 sqlite3VdbeJumpHere(v, addrInsTop);
116569 sqlite3VdbeAddOp1(v, OP_Close, srcTab);
116570 }else if( pSelect ){
116571 sqlite3VdbeGoto(v, addrCont);
116572 sqlite3VdbeJumpHere(v, addrInsTop);
116573 }
116574
116575insert_end:
116576 /* Update the sqlite_sequence table by storing the content of the
116577 ** maximum rowid counter values recorded while inserting into
116578 ** autoincrement tables.
116579 */
116580 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
116581 sqlite3AutoincrementEnd(pParse);
116582 }
116583
116584 /*
116585 ** Return the number of rows inserted. If this routine is
116586 ** generating code because of a call to sqlite3NestedParse(), do not
116587 ** invoke the callback function.
116588 */
116589 if( regRowCount ){
116590 sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
116591 sqlite3VdbeSetNumCols(v, 1);
116592 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
116593 }
116594
116595insert_cleanup:
116596 sqlite3SrcListDelete(db, pTabList);
116597 sqlite3ExprListDelete(db, pList);
116598 sqlite3UpsertDelete(db, pUpsert);
116599 sqlite3SelectDelete(db, pSelect);
116600 sqlite3IdListDelete(db, pColumn);
116601 sqlite3DbFree(db, aRegIdx);
116602}
116603
116604/* Make sure "isView" and other macros defined above are undefined. Otherwise
116605** they may interfere with compilation of other functions in this file
116606** (or in another file, if this file becomes part of the amalgamation). */
116607#ifdef isView
116608 #undef isView
116609#endif
116610#ifdef pTrigger
116611 #undef pTrigger
116612#endif
116613#ifdef tmask
116614 #undef tmask
116615#endif
116616
116617/*
116618** Meanings of bits in of pWalker->eCode for
116619** sqlite3ExprReferencesUpdatedColumn()
116620*/
116621#define CKCNSTRNT_COLUMN 0x01 /* CHECK constraint uses a changing column */
116622#define CKCNSTRNT_ROWID 0x02 /* CHECK constraint references the ROWID */
116623
116624/* This is the Walker callback from sqlite3ExprReferencesUpdatedColumn().
116625* Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
116626** expression node references any of the
116627** columns that are being modifed by an UPDATE statement.
116628*/
116629static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){
116630 if( pExpr->op==TK_COLUMN ){
116631 assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
116632 if( pExpr->iColumn>=0 ){
116633 if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
116634 pWalker->eCode |= CKCNSTRNT_COLUMN;
116635 }
116636 }else{
116637 pWalker->eCode |= CKCNSTRNT_ROWID;
116638 }
116639 }
116640 return WRC_Continue;
116641}
116642
116643/*
116644** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
116645** only columns that are modified by the UPDATE are those for which
116646** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true.
116647**
116648** Return true if CHECK constraint pExpr uses any of the
116649** changing columns (or the rowid if it is changing). In other words,
116650** return true if this CHECK constraint must be validated for
116651** the new row in the UPDATE statement.
116652**
116653** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
116654** The operation of this routine is the same - return true if an only if
116655** the expression uses one or more of columns identified by the second and
116656** third arguments.
116657*/
116658SQLITE_PRIVATE int sqlite3ExprReferencesUpdatedColumn(
116659 Expr *pExpr, /* The expression to be checked */
116660 int *aiChng, /* aiChng[x]>=0 if column x changed by the UPDATE */
116661 int chngRowid /* True if UPDATE changes the rowid */
116662){
116663 Walker w;
116664 memset(&w, 0, sizeof(w));
116665 w.eCode = 0;
116666 w.xExprCallback = checkConstraintExprNode;
116667 w.u.aiCol = aiChng;
116668 sqlite3WalkExpr(&w, pExpr);
116669 if( !chngRowid ){
116670 testcase( (w.eCode & CKCNSTRNT_ROWID)!=0 );
116671 w.eCode &= ~CKCNSTRNT_ROWID;
116672 }
116673 testcase( w.eCode==0 );
116674 testcase( w.eCode==CKCNSTRNT_COLUMN );
116675 testcase( w.eCode==CKCNSTRNT_ROWID );
116676 testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) );
116677 return w.eCode!=0;
116678}
116679
116680/*
116681** Generate code to do constraint checks prior to an INSERT or an UPDATE
116682** on table pTab.
116683**
116684** The regNewData parameter is the first register in a range that contains
116685** the data to be inserted or the data after the update. There will be
116686** pTab->nCol+1 registers in this range. The first register (the one
116687** that regNewData points to) will contain the new rowid, or NULL in the
116688** case of a WITHOUT ROWID table. The second register in the range will
116689** contain the content of the first table column. The third register will
116690** contain the content of the second table column. And so forth.
116691**
116692** The regOldData parameter is similar to regNewData except that it contains
116693** the data prior to an UPDATE rather than afterwards. regOldData is zero
116694** for an INSERT. This routine can distinguish between UPDATE and INSERT by
116695** checking regOldData for zero.
116696**
116697** For an UPDATE, the pkChng boolean is true if the true primary key (the
116698** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table)
116699** might be modified by the UPDATE. If pkChng is false, then the key of
116700** the iDataCur content table is guaranteed to be unchanged by the UPDATE.
116701**
116702** For an INSERT, the pkChng boolean indicates whether or not the rowid
116703** was explicitly specified as part of the INSERT statement. If pkChng
116704** is zero, it means that the either rowid is computed automatically or
116705** that the table is a WITHOUT ROWID table and has no rowid. On an INSERT,
116706** pkChng will only be true if the INSERT statement provides an integer
116707** value for either the rowid column or its INTEGER PRIMARY KEY alias.
116708**
116709** The code generated by this routine will store new index entries into
116710** registers identified by aRegIdx[]. No index entry is created for
116711** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is
116712** the same as the order of indices on the linked list of indices
116713** at pTab->pIndex.
116714**
116715** The caller must have already opened writeable cursors on the main
116716** table and all applicable indices (that is to say, all indices for which
116717** aRegIdx[] is not zero). iDataCur is the cursor for the main table when
116718** inserting or updating a rowid table, or the cursor for the PRIMARY KEY
116719** index when operating on a WITHOUT ROWID table. iIdxCur is the cursor
116720** for the first index in the pTab->pIndex list. Cursors for other indices
116721** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
116722**
116723** This routine also generates code to check constraints. NOT NULL,
116724** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
116725** then the appropriate action is performed. There are five possible
116726** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
116727**
116728** Constraint type Action What Happens
116729** --------------- ---------- ----------------------------------------
116730** any ROLLBACK The current transaction is rolled back and
116731** sqlite3_step() returns immediately with a
116732** return code of SQLITE_CONSTRAINT.
116733**
116734** any ABORT Back out changes from the current command
116735** only (do not do a complete rollback) then
116736** cause sqlite3_step() to return immediately
116737** with SQLITE_CONSTRAINT.
116738**
116739** any FAIL Sqlite3_step() returns immediately with a
116740** return code of SQLITE_CONSTRAINT. The
116741** transaction is not rolled back and any
116742** changes to prior rows are retained.
116743**
116744** any IGNORE The attempt in insert or update the current
116745** row is skipped, without throwing an error.
116746** Processing continues with the next row.
116747** (There is an immediate jump to ignoreDest.)
116748**
116749** NOT NULL REPLACE The NULL value is replace by the default
116750** value for that column. If the default value
116751** is NULL, the action is the same as ABORT.
116752**
116753** UNIQUE REPLACE The other row that conflicts with the row
116754** being inserted is removed.
116755**
116756** CHECK REPLACE Illegal. The results in an exception.
116757**
116758** Which action to take is determined by the overrideError parameter.
116759** Or if overrideError==OE_Default, then the pParse->onError parameter
116760** is used. Or if pParse->onError==OE_Default then the onError value
116761** for the constraint is used.
116762*/
116763SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
116764 Parse *pParse, /* The parser context */
116765 Table *pTab, /* The table being inserted or updated */
116766 int *aRegIdx, /* Use register aRegIdx[i] for index i. 0 for unused */
116767 int iDataCur, /* Canonical data cursor (main table or PK index) */
116768 int iIdxCur, /* First index cursor */
116769 int regNewData, /* First register in a range holding values to insert */
116770 int regOldData, /* Previous content. 0 for INSERTs */
116771 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
116772 u8 overrideError, /* Override onError to this if not OE_Default */
116773 int ignoreDest, /* Jump to this label on an OE_Ignore resolution */
116774 int *pbMayReplace, /* OUT: Set to true if constraint may cause a replace */
116775 int *aiChng, /* column i is unchanged if aiChng[i]<0 */
116776 Upsert *pUpsert /* ON CONFLICT clauses, if any. NULL otherwise */
116777){
116778 Vdbe *v; /* VDBE under constrution */
116779 Index *pIdx; /* Pointer to one of the indices */
116780 Index *pPk = 0; /* The PRIMARY KEY index */
116781 sqlite3 *db; /* Database connection */
116782 int i; /* loop counter */
116783 int ix; /* Index loop counter */
116784 int nCol; /* Number of columns */
116785 int onError; /* Conflict resolution strategy */
116786 int addr1; /* Address of jump instruction */
116787 int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
116788 int nPkField; /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
116789 Index *pUpIdx = 0; /* Index to which to apply the upsert */
116790 u8 isUpdate; /* True if this is an UPDATE operation */
116791 u8 bAffinityDone = 0; /* True if the OP_Affinity operation has been run */
116792 int upsertBypass = 0; /* Address of Goto to bypass upsert subroutine */
116793 int upsertJump = 0; /* Address of Goto that jumps into upsert subroutine */
116794 int ipkTop = 0; /* Top of the IPK uniqueness check */
116795 int ipkBottom = 0; /* OP_Goto at the end of the IPK uniqueness check */
116796
116797 isUpdate = regOldData!=0;
116798 db = pParse->db;
116799 v = sqlite3GetVdbe(pParse);
116800 assert( v!=0 );
116801 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
116802 nCol = pTab->nCol;
116803
116804 /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
116805 ** normal rowid tables. nPkField is the number of key fields in the
116806 ** pPk index or 1 for a rowid table. In other words, nPkField is the
116807 ** number of fields in the true primary key of the table. */
116808 if( HasRowid(pTab) ){
116809 pPk = 0;
116810 nPkField = 1;
116811 }else{
116812 pPk = sqlite3PrimaryKeyIndex(pTab);
116813 nPkField = pPk->nKeyCol;
116814 }
116815
116816 /* Record that this module has started */
116817 VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)",
116818 iDataCur, iIdxCur, regNewData, regOldData, pkChng));
116819
116820 /* Test all NOT NULL constraints.
116821 */
116822 for(i=0; i<nCol; i++){
116823 if( i==pTab->iPKey ){
116824 continue; /* ROWID is never NULL */
116825 }
116826 if( aiChng && aiChng[i]<0 ){
116827 /* Don't bother checking for NOT NULL on columns that do not change */
116828 continue;
116829 }
116830 onError = pTab->aCol[i].notNull;
116831 if( onError==OE_None ) continue; /* This column is allowed to be NULL */
116832 if( overrideError!=OE_Default ){
116833 onError = overrideError;
116834 }else if( onError==OE_Default ){
116835 onError = OE_Abort;
116836 }
116837 if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
116838 onError = OE_Abort;
116839 }
116840 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
116841 || onError==OE_Ignore || onError==OE_Replace );
116842 switch( onError ){
116843 case OE_Abort:
116844 sqlite3MayAbort(pParse);
116845 /* Fall through */
116846 case OE_Rollback:
116847 case OE_Fail: {
116848 char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
116849 pTab->aCol[i].zName);
116850 sqlite3VdbeAddOp3(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL, onError,
116851 regNewData+1+i);
116852 sqlite3VdbeAppendP4(v, zMsg, P4_DYNAMIC);
116853 sqlite3VdbeChangeP5(v, P5_ConstraintNotNull);
116854 VdbeCoverage(v);
116855 break;
116856 }
116857 case OE_Ignore: {
116858 sqlite3VdbeAddOp2(v, OP_IsNull, regNewData+1+i, ignoreDest);
116859 VdbeCoverage(v);
116860 break;
116861 }
116862 default: {
116863 assert( onError==OE_Replace );
116864 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regNewData+1+i);
116865 VdbeCoverage(v);
116866 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regNewData+1+i);
116867 sqlite3VdbeJumpHere(v, addr1);
116868 break;
116869 }
116870 }
116871 }
116872
116873 /* Test all CHECK constraints
116874 */
116875#ifndef SQLITE_OMIT_CHECK
116876 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
116877 ExprList *pCheck = pTab->pCheck;
116878 pParse->iSelfTab = -(regNewData+1);
116879 onError = overrideError!=OE_Default ? overrideError : OE_Abort;
116880 for(i=0; i<pCheck->nExpr; i++){
116881 int allOk;
116882 Expr *pExpr = pCheck->a[i].pExpr;
116883 if( aiChng
116884 && !sqlite3ExprReferencesUpdatedColumn(pExpr, aiChng, pkChng)
116885 ){
116886 /* The check constraints do not reference any of the columns being
116887 ** updated so there is no point it verifying the check constraint */
116888 continue;
116889 }
116890 allOk = sqlite3VdbeMakeLabel(v);
116891 sqlite3VdbeVerifyAbortable(v, onError);
116892 sqlite3ExprIfTrue(pParse, pExpr, allOk, SQLITE_JUMPIFNULL);
116893 if( onError==OE_Ignore ){
116894 sqlite3VdbeGoto(v, ignoreDest);
116895 }else{
116896 char *zName = pCheck->a[i].zName;
116897 if( zName==0 ) zName = pTab->zName;
116898 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
116899 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK,
116900 onError, zName, P4_TRANSIENT,
116901 P5_ConstraintCheck);
116902 }
116903 sqlite3VdbeResolveLabel(v, allOk);
116904 }
116905 pParse->iSelfTab = 0;
116906 }
116907#endif /* !defined(SQLITE_OMIT_CHECK) */
116908
116909 /* UNIQUE and PRIMARY KEY constraints should be handled in the following
116910 ** order:
116911 **
116912 ** (1) OE_Update
116913 ** (2) OE_Abort, OE_Fail, OE_Rollback, OE_Ignore
116914 ** (3) OE_Replace
116915 **
116916 ** OE_Fail and OE_Ignore must happen before any changes are made.
116917 ** OE_Update guarantees that only a single row will change, so it
116918 ** must happen before OE_Replace. Technically, OE_Abort and OE_Rollback
116919 ** could happen in any order, but they are grouped up front for
116920 ** convenience.
116921 **
116922 ** 2018-08-14: Ticket https://www.sqlite.org/src/info/908f001483982c43
116923 ** The order of constraints used to have OE_Update as (2) and OE_Abort
116924 ** and so forth as (1). But apparently PostgreSQL checks the OE_Update
116925 ** constraint before any others, so it had to be moved.
116926 **
116927 ** Constraint checking code is generated in this order:
116928 ** (A) The rowid constraint
116929 ** (B) Unique index constraints that do not have OE_Replace as their
116930 ** default conflict resolution strategy
116931 ** (C) Unique index that do use OE_Replace by default.
116932 **
116933 ** The ordering of (2) and (3) is accomplished by making sure the linked
116934 ** list of indexes attached to a table puts all OE_Replace indexes last
116935 ** in the list. See sqlite3CreateIndex() for where that happens.
116936 */
116937
116938 if( pUpsert ){
116939 if( pUpsert->pUpsertTarget==0 ){
116940 /* An ON CONFLICT DO NOTHING clause, without a constraint-target.
116941 ** Make all unique constraint resolution be OE_Ignore */
116942 assert( pUpsert->pUpsertSet==0 );
116943 overrideError = OE_Ignore;
116944 pUpsert = 0;
116945 }else if( (pUpIdx = pUpsert->pUpsertIdx)!=0 ){
116946 /* If the constraint-target uniqueness check must be run first.
116947 ** Jump to that uniqueness check now */
116948 upsertJump = sqlite3VdbeAddOp0(v, OP_Goto);
116949 VdbeComment((v, "UPSERT constraint goes first"));
116950 }
116951 }
116952
116953 /* If rowid is changing, make sure the new rowid does not previously
116954 ** exist in the table.
116955 */
116956 if( pkChng && pPk==0 ){
116957 int addrRowidOk = sqlite3VdbeMakeLabel(v);
116958
116959 /* Figure out what action to take in case of a rowid collision */
116960 onError = pTab->keyConf;
116961 if( overrideError!=OE_Default ){
116962 onError = overrideError;
116963 }else if( onError==OE_Default ){
116964 onError = OE_Abort;
116965 }
116966
116967 /* figure out whether or not upsert applies in this case */
116968 if( pUpsert && pUpsert->pUpsertIdx==0 ){
116969 if( pUpsert->pUpsertSet==0 ){
116970 onError = OE_Ignore; /* DO NOTHING is the same as INSERT OR IGNORE */
116971 }else{
116972 onError = OE_Update; /* DO UPDATE */
116973 }
116974 }
116975
116976 /* If the response to a rowid conflict is REPLACE but the response
116977 ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
116978 ** to defer the running of the rowid conflict checking until after
116979 ** the UNIQUE constraints have run.
116980 */
116981 if( onError==OE_Replace /* IPK rule is REPLACE */
116982 && onError!=overrideError /* Rules for other contraints are different */
116983 && pTab->pIndex /* There exist other constraints */
116984 ){
116985 ipkTop = sqlite3VdbeAddOp0(v, OP_Goto)+1;
116986 VdbeComment((v, "defer IPK REPLACE until last"));
116987 }
116988
116989 if( isUpdate ){
116990 /* pkChng!=0 does not mean that the rowid has changed, only that
116991 ** it might have changed. Skip the conflict logic below if the rowid
116992 ** is unchanged. */
116993 sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData);
116994 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
116995 VdbeCoverage(v);
116996 }
116997
116998 /* Check to see if the new rowid already exists in the table. Skip
116999 ** the following conflict logic if it does not. */
117000 VdbeNoopComment((v, "uniqueness check for ROWID"));
117001 sqlite3VdbeVerifyAbortable(v, onError);
117002 sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData);
117003 VdbeCoverage(v);
117004
117005 switch( onError ){
117006 default: {
117007 onError = OE_Abort;
117008 /* Fall thru into the next case */
117009 }
117010 case OE_Rollback:
117011 case OE_Abort:
117012 case OE_Fail: {
117013 testcase( onError==OE_Rollback );
117014 testcase( onError==OE_Abort );
117015 testcase( onError==OE_Fail );
117016 sqlite3RowidConstraint(pParse, onError, pTab);
117017 break;
117018 }
117019 case OE_Replace: {
117020 /* If there are DELETE triggers on this table and the
117021 ** recursive-triggers flag is set, call GenerateRowDelete() to
117022 ** remove the conflicting row from the table. This will fire
117023 ** the triggers and remove both the table and index b-tree entries.
117024 **
117025 ** Otherwise, if there are no triggers or the recursive-triggers
117026 ** flag is not set, but the table has one or more indexes, call
117027 ** GenerateRowIndexDelete(). This removes the index b-tree entries
117028 ** only. The table b-tree entry will be replaced by the new entry
117029 ** when it is inserted.
117030 **
117031 ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
117032 ** also invoke MultiWrite() to indicate that this VDBE may require
117033 ** statement rollback (if the statement is aborted after the delete
117034 ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
117035 ** but being more selective here allows statements like:
117036 **
117037 ** REPLACE INTO t(rowid) VALUES($newrowid)
117038 **
117039 ** to run without a statement journal if there are no indexes on the
117040 ** table.
117041 */
117042 Trigger *pTrigger = 0;
117043 if( db->flags&SQLITE_RecTriggers ){
117044 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
117045 }
117046 if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
117047 sqlite3MultiWrite(pParse);
117048 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
117049 regNewData, 1, 0, OE_Replace, 1, -1);
117050 }else{
117051#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
117052 assert( HasRowid(pTab) );
117053 /* This OP_Delete opcode fires the pre-update-hook only. It does
117054 ** not modify the b-tree. It is more efficient to let the coming
117055 ** OP_Insert replace the existing entry than it is to delete the
117056 ** existing entry and then insert a new one. */
117057 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, OPFLAG_ISNOOP);
117058 sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
117059#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
117060 if( pTab->pIndex ){
117061 sqlite3MultiWrite(pParse);
117062 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
117063 }
117064 }
117065 seenReplace = 1;
117066 break;
117067 }
117068#ifndef SQLITE_OMIT_UPSERT
117069 case OE_Update: {
117070 sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, 0, iDataCur);
117071 /* Fall through */
117072 }
117073#endif
117074 case OE_Ignore: {
117075 testcase( onError==OE_Ignore );
117076 sqlite3VdbeGoto(v, ignoreDest);
117077 break;
117078 }
117079 }
117080 sqlite3VdbeResolveLabel(v, addrRowidOk);
117081 if( ipkTop ){
117082 ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto);
117083 sqlite3VdbeJumpHere(v, ipkTop-1);
117084 }
117085 }
117086
117087 /* Test all UNIQUE constraints by creating entries for each UNIQUE
117088 ** index and making sure that duplicate entries do not already exist.
117089 ** Compute the revised record entries for indices as we go.
117090 **
117091 ** This loop also handles the case of the PRIMARY KEY index for a
117092 ** WITHOUT ROWID table.
117093 */
117094 for(ix=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, ix++){
117095 int regIdx; /* Range of registers hold conent for pIdx */
117096 int regR; /* Range of registers holding conflicting PK */
117097 int iThisCur; /* Cursor for this UNIQUE index */
117098 int addrUniqueOk; /* Jump here if the UNIQUE constraint is satisfied */
117099
117100 if( aRegIdx[ix]==0 ) continue; /* Skip indices that do not change */
117101 if( pUpIdx==pIdx ){
117102 addrUniqueOk = upsertJump+1;
117103 upsertBypass = sqlite3VdbeGoto(v, 0);
117104 VdbeComment((v, "Skip upsert subroutine"));
117105 sqlite3VdbeJumpHere(v, upsertJump);
117106 }else{
117107 addrUniqueOk = sqlite3VdbeMakeLabel(v);
117108 }
117109 if( bAffinityDone==0 && (pUpIdx==0 || pUpIdx==pIdx) ){
117110 sqlite3TableAffinity(v, pTab, regNewData+1);
117111 bAffinityDone = 1;
117112 }
117113 VdbeNoopComment((v, "uniqueness check for %s", pIdx->zName));
117114 iThisCur = iIdxCur+ix;
117115
117116
117117 /* Skip partial indices for which the WHERE clause is not true */
117118 if( pIdx->pPartIdxWhere ){
117119 sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]);
117120 pParse->iSelfTab = -(regNewData+1);
117121 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
117122 SQLITE_JUMPIFNULL);
117123 pParse->iSelfTab = 0;
117124 }
117125
117126 /* Create a record for this index entry as it should appear after
117127 ** the insert or update. Store that record in the aRegIdx[ix] register
117128 */
117129 regIdx = aRegIdx[ix]+1;
117130 for(i=0; i<pIdx->nColumn; i++){
117131 int iField = pIdx->aiColumn[i];
117132 int x;
117133 if( iField==XN_EXPR ){
117134 pParse->iSelfTab = -(regNewData+1);
117135 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
117136 pParse->iSelfTab = 0;
117137 VdbeComment((v, "%s column %d", pIdx->zName, i));
117138 }else{
117139 if( iField==XN_ROWID || iField==pTab->iPKey ){
117140 x = regNewData;
117141 }else{
117142 x = iField + regNewData + 1;
117143 }
117144 sqlite3VdbeAddOp2(v, iField<0 ? OP_IntCopy : OP_SCopy, x, regIdx+i);
117145 VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName));
117146 }
117147 }
117148 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
117149 VdbeComment((v, "for %s", pIdx->zName));
117150#ifdef SQLITE_ENABLE_NULL_TRIM
117151 if( pIdx->idxType==2 ) sqlite3SetMakeRecordP5(v, pIdx->pTable);
117152#endif
117153
117154 /* In an UPDATE operation, if this index is the PRIMARY KEY index
117155 ** of a WITHOUT ROWID table and there has been no change the
117156 ** primary key, then no collision is possible. The collision detection
117157 ** logic below can all be skipped. */
117158 if( isUpdate && pPk==pIdx && pkChng==0 ){
117159 sqlite3VdbeResolveLabel(v, addrUniqueOk);
117160 continue;
117161 }
117162
117163 /* Find out what action to take in case there is a uniqueness conflict */
117164 onError = pIdx->onError;
117165 if( onError==OE_None ){
117166 sqlite3VdbeResolveLabel(v, addrUniqueOk);
117167 continue; /* pIdx is not a UNIQUE index */
117168 }
117169 if( overrideError!=OE_Default ){
117170 onError = overrideError;
117171 }else if( onError==OE_Default ){
117172 onError = OE_Abort;
117173 }
117174
117175 /* Figure out if the upsert clause applies to this index */
117176 if( pUpIdx==pIdx ){
117177 if( pUpsert->pUpsertSet==0 ){
117178 onError = OE_Ignore; /* DO NOTHING is the same as INSERT OR IGNORE */
117179 }else{
117180 onError = OE_Update; /* DO UPDATE */
117181 }
117182 }
117183
117184 /* Collision detection may be omitted if all of the following are true:
117185 ** (1) The conflict resolution algorithm is REPLACE
117186 ** (2) The table is a WITHOUT ROWID table
117187 ** (3) There are no secondary indexes on the table
117188 ** (4) No delete triggers need to be fired if there is a conflict
117189 ** (5) No FK constraint counters need to be updated if a conflict occurs.
117190 */
117191 if( (ix==0 && pIdx->pNext==0) /* Condition 3 */
117192 && pPk==pIdx /* Condition 2 */
117193 && onError==OE_Replace /* Condition 1 */
117194 && ( 0==(db->flags&SQLITE_RecTriggers) || /* Condition 4 */
117195 0==sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0))
117196 && ( 0==(db->flags&SQLITE_ForeignKeys) || /* Condition 5 */
117197 (0==pTab->pFKey && 0==sqlite3FkReferences(pTab)))
117198 ){
117199 sqlite3VdbeResolveLabel(v, addrUniqueOk);
117200 continue;
117201 }
117202
117203 /* Check to see if the new index entry will be unique */
117204 sqlite3VdbeVerifyAbortable(v, onError);
117205 sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
117206 regIdx, pIdx->nKeyCol); VdbeCoverage(v);
117207
117208 /* Generate code to handle collisions */
117209 regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField);
117210 if( isUpdate || onError==OE_Replace ){
117211 if( HasRowid(pTab) ){
117212 sqlite3VdbeAddOp2(v, OP_IdxRowid, iThisCur, regR);
117213 /* Conflict only if the rowid of the existing index entry
117214 ** is different from old-rowid */
117215 if( isUpdate ){
117216 sqlite3VdbeAddOp3(v, OP_Eq, regR, addrUniqueOk, regOldData);
117217 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
117218 VdbeCoverage(v);
117219 }
117220 }else{
117221 int x;
117222 /* Extract the PRIMARY KEY from the end of the index entry and
117223 ** store it in registers regR..regR+nPk-1 */
117224 if( pIdx!=pPk ){
117225 for(i=0; i<pPk->nKeyCol; i++){
117226 assert( pPk->aiColumn[i]>=0 );
117227 x = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
117228 sqlite3VdbeAddOp3(v, OP_Column, iThisCur, x, regR+i);
117229 VdbeComment((v, "%s.%s", pTab->zName,
117230 pTab->aCol[pPk->aiColumn[i]].zName));
117231 }
117232 }
117233 if( isUpdate ){
117234 /* If currently processing the PRIMARY KEY of a WITHOUT ROWID
117235 ** table, only conflict if the new PRIMARY KEY values are actually
117236 ** different from the old.
117237 **
117238 ** For a UNIQUE index, only conflict if the PRIMARY KEY values
117239 ** of the matched index row are different from the original PRIMARY
117240 ** KEY values of this row before the update. */
117241 int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
117242 int op = OP_Ne;
117243 int regCmp = (IsPrimaryKeyIndex(pIdx) ? regIdx : regR);
117244
117245 for(i=0; i<pPk->nKeyCol; i++){
117246 char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
117247 x = pPk->aiColumn[i];
117248 assert( x>=0 );
117249 if( i==(pPk->nKeyCol-1) ){
117250 addrJump = addrUniqueOk;
117251 op = OP_Eq;
117252 }
117253 sqlite3VdbeAddOp4(v, op,
117254 regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ
117255 );
117256 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
117257 VdbeCoverageIf(v, op==OP_Eq);
117258 VdbeCoverageIf(v, op==OP_Ne);
117259 }
117260 }
117261 }
117262 }
117263
117264 /* Generate code that executes if the new index entry is not unique */
117265 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
117266 || onError==OE_Ignore || onError==OE_Replace || onError==OE_Update );
117267 switch( onError ){
117268 case OE_Rollback:
117269 case OE_Abort:
117270 case OE_Fail: {
117271 testcase( onError==OE_Rollback );
117272 testcase( onError==OE_Abort );
117273 testcase( onError==OE_Fail );
117274 sqlite3UniqueConstraint(pParse, onError, pIdx);
117275 break;
117276 }
117277#ifndef SQLITE_OMIT_UPSERT
117278 case OE_Update: {
117279 sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, pIdx, iIdxCur+ix);
117280 /* Fall through */
117281 }
117282#endif
117283 case OE_Ignore: {
117284 testcase( onError==OE_Ignore );
117285 sqlite3VdbeGoto(v, ignoreDest);
117286 break;
117287 }
117288 default: {
117289 Trigger *pTrigger = 0;
117290 assert( onError==OE_Replace );
117291 if( db->flags&SQLITE_RecTriggers ){
117292 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
117293 }
117294 if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
117295 sqlite3MultiWrite(pParse);
117296 }
117297 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
117298 regR, nPkField, 0, OE_Replace,
117299 (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur);
117300 seenReplace = 1;
117301 break;
117302 }
117303 }
117304 if( pUpIdx==pIdx ){
117305 sqlite3VdbeGoto(v, upsertJump+1);
117306 sqlite3VdbeJumpHere(v, upsertBypass);
117307 }else{
117308 sqlite3VdbeResolveLabel(v, addrUniqueOk);
117309 }
117310 if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField);
117311 }
117312
117313 /* If the IPK constraint is a REPLACE, run it last */
117314 if( ipkTop ){
117315 sqlite3VdbeGoto(v, ipkTop+1);
117316 VdbeComment((v, "Do IPK REPLACE"));
117317 sqlite3VdbeJumpHere(v, ipkBottom);
117318 }
117319
117320 *pbMayReplace = seenReplace;
117321 VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
117322}
117323
117324#ifdef SQLITE_ENABLE_NULL_TRIM
117325/*
117326** Change the P5 operand on the last opcode (which should be an OP_MakeRecord)
117327** to be the number of columns in table pTab that must not be NULL-trimmed.
117328**
117329** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
117330*/
117331SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){
117332 u16 i;
117333
117334 /* Records with omitted columns are only allowed for schema format
117335 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
117336 if( pTab->pSchema->file_format<2 ) return;
117337
117338 for(i=pTab->nCol-1; i>0; i--){
117339 if( pTab->aCol[i].pDflt!=0 ) break;
117340 if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break;
117341 }
117342 sqlite3VdbeChangeP5(v, i+1);
117343}
117344#endif
117345
117346/*
117347** This routine generates code to finish the INSERT or UPDATE operation
117348** that was started by a prior call to sqlite3GenerateConstraintChecks.
117349** A consecutive range of registers starting at regNewData contains the
117350** rowid and the content to be inserted.
117351**
117352** The arguments to this routine should be the same as the first six
117353** arguments to sqlite3GenerateConstraintChecks.
117354*/
117355SQLITE_PRIVATE void sqlite3CompleteInsertion(
117356 Parse *pParse, /* The parser context */
117357 Table *pTab, /* the table into which we are inserting */
117358 int iDataCur, /* Cursor of the canonical data source */
117359 int iIdxCur, /* First index cursor */
117360 int regNewData, /* Range of content */
117361 int *aRegIdx, /* Register used by each index. 0 for unused indices */
117362 int update_flags, /* True for UPDATE, False for INSERT */
117363 int appendBias, /* True if this is likely to be an append */
117364 int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
117365){
117366 Vdbe *v; /* Prepared statements under construction */
117367 Index *pIdx; /* An index being inserted or updated */
117368 u8 pik_flags; /* flag values passed to the btree insert */
117369 int regData; /* Content registers (after the rowid) */
117370 int regRec; /* Register holding assembled record for the table */
117371 int i; /* Loop counter */
117372 u8 bAffinityDone = 0; /* True if OP_Affinity has been run already */
117373
117374 assert( update_flags==0
117375 || update_flags==OPFLAG_ISUPDATE
117376 || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION)
117377 );
117378
117379 v = sqlite3GetVdbe(pParse);
117380 assert( v!=0 );
117381 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
117382 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
117383 if( aRegIdx[i]==0 ) continue;
117384 bAffinityDone = 1;
117385 if( pIdx->pPartIdxWhere ){
117386 sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
117387 VdbeCoverage(v);
117388 }
117389 pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0);
117390 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
117391 assert( pParse->nested==0 );
117392 pik_flags |= OPFLAG_NCHANGE;
117393 pik_flags |= (update_flags & OPFLAG_SAVEPOSITION);
117394#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
117395 if( update_flags==0 ){
117396 sqlite3VdbeAddOp4(v, OP_InsertInt,
117397 iIdxCur+i, aRegIdx[i], 0, (char*)pTab, P4_TABLE
117398 );
117399 sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP);
117400 }
117401#endif
117402 }
117403 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i],
117404 aRegIdx[i]+1,
117405 pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
117406 sqlite3VdbeChangeP5(v, pik_flags);
117407 }
117408 if( !HasRowid(pTab) ) return;
117409 regData = regNewData + 1;
117410 regRec = sqlite3GetTempReg(pParse);
117411 sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
117412 sqlite3SetMakeRecordP5(v, pTab);
117413 if( !bAffinityDone ){
117414 sqlite3TableAffinity(v, pTab, 0);
117415 }
117416 if( pParse->nested ){
117417 pik_flags = 0;
117418 }else{
117419 pik_flags = OPFLAG_NCHANGE;
117420 pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID);
117421 }
117422 if( appendBias ){
117423 pik_flags |= OPFLAG_APPEND;
117424 }
117425 if( useSeekResult ){
117426 pik_flags |= OPFLAG_USESEEKRESULT;
117427 }
117428 sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, regRec, regNewData);
117429 if( !pParse->nested ){
117430 sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
117431 }
117432 sqlite3VdbeChangeP5(v, pik_flags);
117433}
117434
117435/*
117436** Allocate cursors for the pTab table and all its indices and generate
117437** code to open and initialized those cursors.
117438**
117439** The cursor for the object that contains the complete data (normally
117440** the table itself, but the PRIMARY KEY index in the case of a WITHOUT
117441** ROWID table) is returned in *piDataCur. The first index cursor is
117442** returned in *piIdxCur. The number of indices is returned.
117443**
117444** Use iBase as the first cursor (either the *piDataCur for rowid tables
117445** or the first index for WITHOUT ROWID tables) if it is non-negative.
117446** If iBase is negative, then allocate the next available cursor.
117447**
117448** For a rowid table, *piDataCur will be exactly one less than *piIdxCur.
117449** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range
117450** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the
117451** pTab->pIndex list.
117452**
117453** If pTab is a virtual table, then this routine is a no-op and the
117454** *piDataCur and *piIdxCur values are left uninitialized.
117455*/
117456SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
117457 Parse *pParse, /* Parsing context */
117458 Table *pTab, /* Table to be opened */
117459 int op, /* OP_OpenRead or OP_OpenWrite */
117460 u8 p5, /* P5 value for OP_Open* opcodes (except on WITHOUT ROWID) */
117461 int iBase, /* Use this for the table cursor, if there is one */
117462 u8 *aToOpen, /* If not NULL: boolean for each table and index */
117463 int *piDataCur, /* Write the database source cursor number here */
117464 int *piIdxCur /* Write the first index cursor number here */
117465){
117466 int i;
117467 int iDb;
117468 int iDataCur;
117469 Index *pIdx;
117470 Vdbe *v;
117471
117472 assert( op==OP_OpenRead || op==OP_OpenWrite );
117473 assert( op==OP_OpenWrite || p5==0 );
117474 if( IsVirtual(pTab) ){
117475 /* This routine is a no-op for virtual tables. Leave the output
117476 ** variables *piDataCur and *piIdxCur uninitialized so that valgrind
117477 ** can detect if they are used by mistake in the caller. */
117478 return 0;
117479 }
117480 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
117481 v = sqlite3GetVdbe(pParse);
117482 assert( v!=0 );
117483 if( iBase<0 ) iBase = pParse->nTab;
117484 iDataCur = iBase++;
117485 if( piDataCur ) *piDataCur = iDataCur;
117486 if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){
117487 sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op);
117488 }else{
117489 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
117490 }
117491 if( piIdxCur ) *piIdxCur = iBase;
117492 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
117493 int iIdxCur = iBase++;
117494 assert( pIdx->pSchema==pTab->pSchema );
117495 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
117496 if( piDataCur ) *piDataCur = iIdxCur;
117497 p5 = 0;
117498 }
117499 if( aToOpen==0 || aToOpen[i+1] ){
117500 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
117501 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
117502 sqlite3VdbeChangeP5(v, p5);
117503 VdbeComment((v, "%s", pIdx->zName));
117504 }
117505 }
117506 if( iBase>pParse->nTab ) pParse->nTab = iBase;
117507 return i;
117508}
117509
117510
117511#ifdef SQLITE_TEST
117512/*
117513** The following global variable is incremented whenever the
117514** transfer optimization is used. This is used for testing
117515** purposes only - to make sure the transfer optimization really
117516** is happening when it is supposed to.
117517*/
117518SQLITE_API int sqlite3_xferopt_count;
117519#endif /* SQLITE_TEST */
117520
117521
117522#ifndef SQLITE_OMIT_XFER_OPT
117523/*
117524** Check to see if index pSrc is compatible as a source of data
117525** for index pDest in an insert transfer optimization. The rules
117526** for a compatible index:
117527**
117528** * The index is over the same set of columns
117529** * The same DESC and ASC markings occurs on all columns
117530** * The same onError processing (OE_Abort, OE_Ignore, etc)
117531** * The same collating sequence on each column
117532** * The index has the exact same WHERE clause
117533*/
117534static int xferCompatibleIndex(Index *pDest, Index *pSrc){
117535 int i;
117536 assert( pDest && pSrc );
117537 assert( pDest->pTable!=pSrc->pTable );
117538 if( pDest->nKeyCol!=pSrc->nKeyCol ){
117539 return 0; /* Different number of columns */
117540 }
117541 if( pDest->onError!=pSrc->onError ){
117542 return 0; /* Different conflict resolution strategies */
117543 }
117544 for(i=0; i<pSrc->nKeyCol; i++){
117545 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
117546 return 0; /* Different columns indexed */
117547 }
117548 if( pSrc->aiColumn[i]==XN_EXPR ){
117549 assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 );
117550 if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr,
117551 pDest->aColExpr->a[i].pExpr, -1)!=0 ){
117552 return 0; /* Different expressions in the index */
117553 }
117554 }
117555 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
117556 return 0; /* Different sort orders */
117557 }
117558 if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){
117559 return 0; /* Different collating sequences */
117560 }
117561 }
117562 if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
117563 return 0; /* Different WHERE clauses */
117564 }
117565
117566 /* If no test above fails then the indices must be compatible */
117567 return 1;
117568}
117569
117570/*
117571** Attempt the transfer optimization on INSERTs of the form
117572**
117573** INSERT INTO tab1 SELECT * FROM tab2;
117574**
117575** The xfer optimization transfers raw records from tab2 over to tab1.
117576** Columns are not decoded and reassembled, which greatly improves
117577** performance. Raw index records are transferred in the same way.
117578**
117579** The xfer optimization is only attempted if tab1 and tab2 are compatible.
117580** There are lots of rules for determining compatibility - see comments
117581** embedded in the code for details.
117582**
117583** This routine returns TRUE if the optimization is guaranteed to be used.
117584** Sometimes the xfer optimization will only work if the destination table
117585** is empty - a factor that can only be determined at run-time. In that
117586** case, this routine generates code for the xfer optimization but also
117587** does a test to see if the destination table is empty and jumps over the
117588** xfer optimization code if the test fails. In that case, this routine
117589** returns FALSE so that the caller will know to go ahead and generate
117590** an unoptimized transfer. This routine also returns FALSE if there
117591** is no chance that the xfer optimization can be applied.
117592**
117593** This optimization is particularly useful at making VACUUM run faster.
117594*/
117595static int xferOptimization(
117596 Parse *pParse, /* Parser context */
117597 Table *pDest, /* The table we are inserting into */
117598 Select *pSelect, /* A SELECT statement to use as the data source */
117599 int onError, /* How to handle constraint errors */
117600 int iDbDest /* The database of pDest */
117601){
117602 sqlite3 *db = pParse->db;
117603 ExprList *pEList; /* The result set of the SELECT */
117604 Table *pSrc; /* The table in the FROM clause of SELECT */
117605 Index *pSrcIdx, *pDestIdx; /* Source and destination indices */
117606 struct SrcList_item *pItem; /* An element of pSelect->pSrc */
117607 int i; /* Loop counter */
117608 int iDbSrc; /* The database of pSrc */
117609 int iSrc, iDest; /* Cursors from source and destination */
117610 int addr1, addr2; /* Loop addresses */
117611 int emptyDestTest = 0; /* Address of test for empty pDest */
117612 int emptySrcTest = 0; /* Address of test for empty pSrc */
117613 Vdbe *v; /* The VDBE we are building */
117614 int regAutoinc; /* Memory register used by AUTOINC */
117615 int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */
117616 int regData, regRowid; /* Registers holding data and rowid */
117617
117618 if( pSelect==0 ){
117619 return 0; /* Must be of the form INSERT INTO ... SELECT ... */
117620 }
117621 if( pParse->pWith || pSelect->pWith ){
117622 /* Do not attempt to process this query if there are an WITH clauses
117623 ** attached to it. Proceeding may generate a false "no such table: xxx"
117624 ** error if pSelect reads from a CTE named "xxx". */
117625 return 0;
117626 }
117627 if( sqlite3TriggerList(pParse, pDest) ){
117628 return 0; /* tab1 must not have triggers */
117629 }
117630#ifndef SQLITE_OMIT_VIRTUALTABLE
117631 if( IsVirtual(pDest) ){
117632 return 0; /* tab1 must not be a virtual table */
117633 }
117634#endif
117635 if( onError==OE_Default ){
117636 if( pDest->iPKey>=0 ) onError = pDest->keyConf;
117637 if( onError==OE_Default ) onError = OE_Abort;
117638 }
117639 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
117640 if( pSelect->pSrc->nSrc!=1 ){
117641 return 0; /* FROM clause must have exactly one term */
117642 }
117643 if( pSelect->pSrc->a[0].pSelect ){
117644 return 0; /* FROM clause cannot contain a subquery */
117645 }
117646 if( pSelect->pWhere ){
117647 return 0; /* SELECT may not have a WHERE clause */
117648 }
117649 if( pSelect->pOrderBy ){
117650 return 0; /* SELECT may not have an ORDER BY clause */
117651 }
117652 /* Do not need to test for a HAVING clause. If HAVING is present but
117653 ** there is no ORDER BY, we will get an error. */
117654 if( pSelect->pGroupBy ){
117655 return 0; /* SELECT may not have a GROUP BY clause */
117656 }
117657 if( pSelect->pLimit ){
117658 return 0; /* SELECT may not have a LIMIT clause */
117659 }
117660 if( pSelect->pPrior ){
117661 return 0; /* SELECT may not be a compound query */
117662 }
117663 if( pSelect->selFlags & SF_Distinct ){
117664 return 0; /* SELECT may not be DISTINCT */
117665 }
117666 pEList = pSelect->pEList;
117667 assert( pEList!=0 );
117668 if( pEList->nExpr!=1 ){
117669 return 0; /* The result set must have exactly one column */
117670 }
117671 assert( pEList->a[0].pExpr );
117672 if( pEList->a[0].pExpr->op!=TK_ASTERISK ){
117673 return 0; /* The result set must be the special operator "*" */
117674 }
117675
117676 /* At this point we have established that the statement is of the
117677 ** correct syntactic form to participate in this optimization. Now
117678 ** we have to check the semantics.
117679 */
117680 pItem = pSelect->pSrc->a;
117681 pSrc = sqlite3LocateTableItem(pParse, 0, pItem);
117682 if( pSrc==0 ){
117683 return 0; /* FROM clause does not contain a real table */
117684 }
117685 if( pSrc==pDest ){
117686 return 0; /* tab1 and tab2 may not be the same table */
117687 }
117688 if( HasRowid(pDest)!=HasRowid(pSrc) ){
117689 return 0; /* source and destination must both be WITHOUT ROWID or not */
117690 }
117691#ifndef SQLITE_OMIT_VIRTUALTABLE
117692 if( IsVirtual(pSrc) ){
117693 return 0; /* tab2 must not be a virtual table */
117694 }
117695#endif
117696 if( pSrc->pSelect ){
117697 return 0; /* tab2 may not be a view */
117698 }
117699 if( pDest->nCol!=pSrc->nCol ){
117700 return 0; /* Number of columns must be the same in tab1 and tab2 */
117701 }
117702 if( pDest->iPKey!=pSrc->iPKey ){
117703 return 0; /* Both tables must have the same INTEGER PRIMARY KEY */
117704 }
117705 for(i=0; i<pDest->nCol; i++){
117706 Column *pDestCol = &pDest->aCol[i];
117707 Column *pSrcCol = &pSrc->aCol[i];
117708#ifdef SQLITE_ENABLE_HIDDEN_COLUMNS
117709 if( (db->mDbFlags & DBFLAG_Vacuum)==0
117710 && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN
117711 ){
117712 return 0; /* Neither table may have __hidden__ columns */
117713 }
117714#endif
117715 if( pDestCol->affinity!=pSrcCol->affinity ){
117716 return 0; /* Affinity must be the same on all columns */
117717 }
117718 if( sqlite3_stricmp(pDestCol->zColl, pSrcCol->zColl)!=0 ){
117719 return 0; /* Collating sequence must be the same on all columns */
117720 }
117721 if( pDestCol->notNull && !pSrcCol->notNull ){
117722 return 0; /* tab2 must be NOT NULL if tab1 is */
117723 }
117724 /* Default values for second and subsequent columns need to match. */
117725 if( i>0 ){
117726 assert( pDestCol->pDflt==0 || pDestCol->pDflt->op==TK_SPAN );
117727 assert( pSrcCol->pDflt==0 || pSrcCol->pDflt->op==TK_SPAN );
117728 if( (pDestCol->pDflt==0)!=(pSrcCol->pDflt==0)
117729 || (pDestCol->pDflt && strcmp(pDestCol->pDflt->u.zToken,
117730 pSrcCol->pDflt->u.zToken)!=0)
117731 ){
117732 return 0; /* Default values must be the same for all columns */
117733 }
117734 }
117735 }
117736 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
117737 if( IsUniqueIndex(pDestIdx) ){
117738 destHasUniqueIdx = 1;
117739 }
117740 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
117741 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
117742 }
117743 if( pSrcIdx==0 ){
117744 return 0; /* pDestIdx has no corresponding index in pSrc */
117745 }
117746 }
117747#ifndef SQLITE_OMIT_CHECK
117748 if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
117749 return 0; /* Tables have different CHECK constraints. Ticket #2252 */
117750 }
117751#endif
117752#ifndef SQLITE_OMIT_FOREIGN_KEY
117753 /* Disallow the transfer optimization if the destination table constains
117754 ** any foreign key constraints. This is more restrictive than necessary.
117755 ** But the main beneficiary of the transfer optimization is the VACUUM
117756 ** command, and the VACUUM command disables foreign key constraints. So
117757 ** the extra complication to make this rule less restrictive is probably
117758 ** not worth the effort. Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
117759 */
117760 if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
117761 return 0;
117762 }
117763#endif
117764 if( (db->flags & SQLITE_CountRows)!=0 ){
117765 return 0; /* xfer opt does not play well with PRAGMA count_changes */
117766 }
117767
117768 /* If we get this far, it means that the xfer optimization is at
117769 ** least a possibility, though it might only work if the destination
117770 ** table (tab1) is initially empty.
117771 */
117772#ifdef SQLITE_TEST
117773 sqlite3_xferopt_count++;
117774#endif
117775 iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
117776 v = sqlite3GetVdbe(pParse);
117777 sqlite3CodeVerifySchema(pParse, iDbSrc);
117778 iSrc = pParse->nTab++;
117779 iDest = pParse->nTab++;
117780 regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
117781 regData = sqlite3GetTempReg(pParse);
117782 regRowid = sqlite3GetTempReg(pParse);
117783 sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
117784 assert( HasRowid(pDest) || destHasUniqueIdx );
117785 if( (db->mDbFlags & DBFLAG_Vacuum)==0 && (
117786 (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
117787 || destHasUniqueIdx /* (2) */
117788 || (onError!=OE_Abort && onError!=OE_Rollback) /* (3) */
117789 )){
117790 /* In some circumstances, we are able to run the xfer optimization
117791 ** only if the destination table is initially empty. Unless the
117792 ** DBFLAG_Vacuum flag is set, this block generates code to make
117793 ** that determination. If DBFLAG_Vacuum is set, then the destination
117794 ** table is always empty.
117795 **
117796 ** Conditions under which the destination must be empty:
117797 **
117798 ** (1) There is no INTEGER PRIMARY KEY but there are indices.
117799 ** (If the destination is not initially empty, the rowid fields
117800 ** of index entries might need to change.)
117801 **
117802 ** (2) The destination has a unique index. (The xfer optimization
117803 ** is unable to test uniqueness.)
117804 **
117805 ** (3) onError is something other than OE_Abort and OE_Rollback.
117806 */
117807 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); VdbeCoverage(v);
117808 emptyDestTest = sqlite3VdbeAddOp0(v, OP_Goto);
117809 sqlite3VdbeJumpHere(v, addr1);
117810 }
117811 if( HasRowid(pSrc) ){
117812 u8 insFlags;
117813 sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
117814 emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
117815 if( pDest->iPKey>=0 ){
117816 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
117817 sqlite3VdbeVerifyAbortable(v, onError);
117818 addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
117819 VdbeCoverage(v);
117820 sqlite3RowidConstraint(pParse, onError, pDest);
117821 sqlite3VdbeJumpHere(v, addr2);
117822 autoIncStep(pParse, regAutoinc, regRowid);
117823 }else if( pDest->pIndex==0 ){
117824 addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
117825 }else{
117826 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
117827 assert( (pDest->tabFlags & TF_Autoincrement)==0 );
117828 }
117829 sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
117830 if( db->mDbFlags & DBFLAG_Vacuum ){
117831 sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest);
117832 insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|
117833 OPFLAG_APPEND|OPFLAG_USESEEKRESULT;
117834 }else{
117835 insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND;
117836 }
117837 sqlite3VdbeAddOp4(v, OP_Insert, iDest, regData, regRowid,
117838 (char*)pDest, P4_TABLE);
117839 sqlite3VdbeChangeP5(v, insFlags);
117840 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); VdbeCoverage(v);
117841 sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
117842 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
117843 }else{
117844 sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
117845 sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
117846 }
117847 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
117848 u8 idxInsFlags = 0;
117849 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
117850 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
117851 }
117852 assert( pSrcIdx );
117853 sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
117854 sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
117855 VdbeComment((v, "%s", pSrcIdx->zName));
117856 sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
117857 sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
117858 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
117859 VdbeComment((v, "%s", pDestIdx->zName));
117860 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
117861 sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
117862 if( db->mDbFlags & DBFLAG_Vacuum ){
117863 /* This INSERT command is part of a VACUUM operation, which guarantees
117864 ** that the destination table is empty. If all indexed columns use
117865 ** collation sequence BINARY, then it can also be assumed that the
117866 ** index will be populated by inserting keys in strictly sorted
117867 ** order. In this case, instead of seeking within the b-tree as part
117868 ** of every OP_IdxInsert opcode, an OP_SeekEnd is added before the
117869 ** OP_IdxInsert to seek to the point within the b-tree where each key
117870 ** should be inserted. This is faster.
117871 **
117872 ** If any of the indexed columns use a collation sequence other than
117873 ** BINARY, this optimization is disabled. This is because the user
117874 ** might change the definition of a collation sequence and then run
117875 ** a VACUUM command. In that case keys may not be written in strictly
117876 ** sorted order. */
117877 for(i=0; i<pSrcIdx->nColumn; i++){
117878 const char *zColl = pSrcIdx->azColl[i];
117879 if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break;
117880 }
117881 if( i==pSrcIdx->nColumn ){
117882 idxInsFlags = OPFLAG_USESEEKRESULT;
117883 sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest);
117884 }
117885 }
117886 if( !HasRowid(pSrc) && pDestIdx->idxType==2 ){
117887 idxInsFlags |= OPFLAG_NCHANGE;
117888 }
117889 sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regData);
117890 sqlite3VdbeChangeP5(v, idxInsFlags|OPFLAG_APPEND);
117891 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v);
117892 sqlite3VdbeJumpHere(v, addr1);
117893 sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
117894 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
117895 }
117896 if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest);
117897 sqlite3ReleaseTempReg(pParse, regRowid);
117898 sqlite3ReleaseTempReg(pParse, regData);
117899 if( emptyDestTest ){
117900 sqlite3AutoincrementEnd(pParse);
117901 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
117902 sqlite3VdbeJumpHere(v, emptyDestTest);
117903 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
117904 return 0;
117905 }else{
117906 return 1;
117907 }
117908}
117909#endif /* SQLITE_OMIT_XFER_OPT */
117910
117911/************** End of insert.c **********************************************/
117912/************** Begin file legacy.c ******************************************/
117913/*
117914** 2001 September 15
117915**
117916** The author disclaims copyright to this source code. In place of
117917** a legal notice, here is a blessing:
117918**
117919** May you do good and not evil.
117920** May you find forgiveness for yourself and forgive others.
117921** May you share freely, never taking more than you give.
117922**
117923*************************************************************************
117924** Main file for the SQLite library. The routines in this file
117925** implement the programmer interface to the library. Routines in
117926** other files are for internal use by SQLite and should not be
117927** accessed by users of the library.
117928*/
117929
117930/* #include "sqliteInt.h" */
117931
117932/*
117933** Execute SQL code. Return one of the SQLITE_ success/failure
117934** codes. Also write an error message into memory obtained from
117935** malloc() and make *pzErrMsg point to that message.
117936**
117937** If the SQL is a query, then for each row in the query result
117938** the xCallback() function is called. pArg becomes the first
117939** argument to xCallback(). If xCallback=NULL then no callback
117940** is invoked, even for queries.
117941*/
117942SQLITE_API int sqlite3_exec(
117943 sqlite3 *db, /* The database on which the SQL executes */
117944 const char *zSql, /* The SQL to be executed */
117945 sqlite3_callback xCallback, /* Invoke this callback routine */
117946 void *pArg, /* First argument to xCallback() */
117947 char **pzErrMsg /* Write error messages here */
117948){
117949 int rc = SQLITE_OK; /* Return code */
117950 const char *zLeftover; /* Tail of unprocessed SQL */
117951 sqlite3_stmt *pStmt = 0; /* The current SQL statement */
117952 char **azCols = 0; /* Names of result columns */
117953 int callbackIsInit; /* True if callback data is initialized */
117954
117955 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
117956 if( zSql==0 ) zSql = "";
117957
117958 sqlite3_mutex_enter(db->mutex);
117959 sqlite3Error(db, SQLITE_OK);
117960 while( rc==SQLITE_OK && zSql[0] ){
117961 int nCol;
117962 char **azVals = 0;
117963
117964 pStmt = 0;
117965 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
117966 assert( rc==SQLITE_OK || pStmt==0 );
117967 if( rc!=SQLITE_OK ){
117968 continue;
117969 }
117970 if( !pStmt ){
117971 /* this happens for a comment or white-space */
117972 zSql = zLeftover;
117973 continue;
117974 }
117975
117976 callbackIsInit = 0;
117977 nCol = sqlite3_column_count(pStmt);
117978
117979 while( 1 ){
117980 int i;
117981 rc = sqlite3_step(pStmt);
117982
117983 /* Invoke the callback function if required */
117984 if( xCallback && (SQLITE_ROW==rc ||
117985 (SQLITE_DONE==rc && !callbackIsInit
117986 && db->flags&SQLITE_NullCallback)) ){
117987 if( !callbackIsInit ){
117988 azCols = sqlite3DbMallocRaw(db, (2*nCol+1)*sizeof(const char*));
117989 if( azCols==0 ){
117990 goto exec_out;
117991 }
117992 for(i=0; i<nCol; i++){
117993 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
117994 /* sqlite3VdbeSetColName() installs column names as UTF8
117995 ** strings so there is no way for sqlite3_column_name() to fail. */
117996 assert( azCols[i]!=0 );
117997 }
117998 callbackIsInit = 1;
117999 }
118000 if( rc==SQLITE_ROW ){
118001 azVals = &azCols[nCol];
118002 for(i=0; i<nCol; i++){
118003 azVals[i] = (char *)sqlite3_column_text(pStmt, i);
118004 if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
118005 sqlite3OomFault(db);
118006 goto exec_out;
118007 }
118008 }
118009 azVals[i] = 0;
118010 }
118011 if( xCallback(pArg, nCol, azVals, azCols) ){
118012 /* EVIDENCE-OF: R-38229-40159 If the callback function to
118013 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
118014 ** return SQLITE_ABORT. */
118015 rc = SQLITE_ABORT;
118016 sqlite3VdbeFinalize((Vdbe *)pStmt);
118017 pStmt = 0;
118018 sqlite3Error(db, SQLITE_ABORT);
118019 goto exec_out;
118020 }
118021 }
118022
118023 if( rc!=SQLITE_ROW ){
118024 rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
118025 pStmt = 0;
118026 zSql = zLeftover;
118027 while( sqlite3Isspace(zSql[0]) ) zSql++;
118028 break;
118029 }
118030 }
118031
118032 sqlite3DbFree(db, azCols);
118033 azCols = 0;
118034 }
118035
118036exec_out:
118037 if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
118038 sqlite3DbFree(db, azCols);
118039
118040 rc = sqlite3ApiExit(db, rc);
118041 if( rc!=SQLITE_OK && pzErrMsg ){
118042 *pzErrMsg = sqlite3DbStrDup(0, sqlite3_errmsg(db));
118043 if( *pzErrMsg==0 ){
118044 rc = SQLITE_NOMEM_BKPT;
118045 sqlite3Error(db, SQLITE_NOMEM);
118046 }
118047 }else if( pzErrMsg ){
118048 *pzErrMsg = 0;
118049 }
118050
118051 assert( (rc&db->errMask)==rc );
118052 sqlite3_mutex_leave(db->mutex);
118053 return rc;
118054}
118055
118056/************** End of legacy.c **********************************************/
118057/************** Begin file loadext.c *****************************************/
118058/*
118059** 2006 June 7
118060**
118061** The author disclaims copyright to this source code. In place of
118062** a legal notice, here is a blessing:
118063**
118064** May you do good and not evil.
118065** May you find forgiveness for yourself and forgive others.
118066** May you share freely, never taking more than you give.
118067**
118068*************************************************************************
118069** This file contains code used to dynamically load extensions into
118070** the SQLite library.
118071*/
118072
118073#ifndef SQLITE_CORE
118074 #define SQLITE_CORE 1 /* Disable the API redefinition in sqlite3ext.h */
118075#endif
118076/************** Include sqlite3ext.h in the middle of loadext.c **************/
118077/************** Begin file sqlite3ext.h **************************************/
118078/*
118079** 2006 June 7
118080**
118081** The author disclaims copyright to this source code. In place of
118082** a legal notice, here is a blessing:
118083**
118084** May you do good and not evil.
118085** May you find forgiveness for yourself and forgive others.
118086** May you share freely, never taking more than you give.
118087**
118088*************************************************************************
118089** This header file defines the SQLite interface for use by
118090** shared libraries that want to be imported as extensions into
118091** an SQLite instance. Shared libraries that intend to be loaded
118092** as extensions by SQLite should #include this file instead of
118093** sqlite3.h.
118094*/
118095#ifndef SQLITE3EXT_H
118096#define SQLITE3EXT_H
118097/* #include "sqlite3.h" */
118098
118099/*
118100** The following structure holds pointers to all of the SQLite API
118101** routines.
118102**
118103** WARNING: In order to maintain backwards compatibility, add new
118104** interfaces to the end of this structure only. If you insert new
118105** interfaces in the middle of this structure, then older different
118106** versions of SQLite will not be able to load each other's shared
118107** libraries!
118108*/
118109struct sqlite3_api_routines {
118110 void * (*aggregate_context)(sqlite3_context*,int nBytes);
118111 int (*aggregate_count)(sqlite3_context*);
118112 int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
118113 int (*bind_double)(sqlite3_stmt*,int,double);
118114 int (*bind_int)(sqlite3_stmt*,int,int);
118115 int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
118116 int (*bind_null)(sqlite3_stmt*,int);
118117 int (*bind_parameter_count)(sqlite3_stmt*);
118118 int (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
118119 const char * (*bind_parameter_name)(sqlite3_stmt*,int);
118120 int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
118121 int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
118122 int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
118123 int (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
118124 int (*busy_timeout)(sqlite3*,int ms);
118125 int (*changes)(sqlite3*);
118126 int (*close)(sqlite3*);
118127 int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
118128 int eTextRep,const char*));
118129 int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
118130 int eTextRep,const void*));
118131 const void * (*column_blob)(sqlite3_stmt*,int iCol);
118132 int (*column_bytes)(sqlite3_stmt*,int iCol);
118133 int (*column_bytes16)(sqlite3_stmt*,int iCol);
118134 int (*column_count)(sqlite3_stmt*pStmt);
118135 const char * (*column_database_name)(sqlite3_stmt*,int);
118136 const void * (*column_database_name16)(sqlite3_stmt*,int);
118137 const char * (*column_decltype)(sqlite3_stmt*,int i);
118138 const void * (*column_decltype16)(sqlite3_stmt*,int);
118139 double (*column_double)(sqlite3_stmt*,int iCol);
118140 int (*column_int)(sqlite3_stmt*,int iCol);
118141 sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol);
118142 const char * (*column_name)(sqlite3_stmt*,int);
118143 const void * (*column_name16)(sqlite3_stmt*,int);
118144 const char * (*column_origin_name)(sqlite3_stmt*,int);
118145 const void * (*column_origin_name16)(sqlite3_stmt*,int);
118146 const char * (*column_table_name)(sqlite3_stmt*,int);
118147 const void * (*column_table_name16)(sqlite3_stmt*,int);
118148 const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
118149 const void * (*column_text16)(sqlite3_stmt*,int iCol);
118150 int (*column_type)(sqlite3_stmt*,int iCol);
118151 sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
118152 void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
118153 int (*complete)(const char*sql);
118154 int (*complete16)(const void*sql);
118155 int (*create_collation)(sqlite3*,const char*,int,void*,
118156 int(*)(void*,int,const void*,int,const void*));
118157 int (*create_collation16)(sqlite3*,const void*,int,void*,
118158 int(*)(void*,int,const void*,int,const void*));
118159 int (*create_function)(sqlite3*,const char*,int,int,void*,
118160 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
118161 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
118162 void (*xFinal)(sqlite3_context*));
118163 int (*create_function16)(sqlite3*,const void*,int,int,void*,
118164 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
118165 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
118166 void (*xFinal)(sqlite3_context*));
118167 int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
118168 int (*data_count)(sqlite3_stmt*pStmt);
118169 sqlite3 * (*db_handle)(sqlite3_stmt*);
118170 int (*declare_vtab)(sqlite3*,const char*);
118171 int (*enable_shared_cache)(int);
118172 int (*errcode)(sqlite3*db);
118173 const char * (*errmsg)(sqlite3*);
118174 const void * (*errmsg16)(sqlite3*);
118175 int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
118176 int (*expired)(sqlite3_stmt*);
118177 int (*finalize)(sqlite3_stmt*pStmt);
118178 void (*free)(void*);
118179 void (*free_table)(char**result);
118180 int (*get_autocommit)(sqlite3*);
118181 void * (*get_auxdata)(sqlite3_context*,int);
118182 int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
118183 int (*global_recover)(void);
118184 void (*interruptx)(sqlite3*);
118185 sqlite_int64 (*last_insert_rowid)(sqlite3*);
118186 const char * (*libversion)(void);
118187 int (*libversion_number)(void);
118188 void *(*malloc)(int);
118189 char * (*mprintf)(const char*,...);
118190 int (*open)(const char*,sqlite3**);
118191 int (*open16)(const void*,sqlite3**);
118192 int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
118193 int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
118194 void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
118195 void (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
118196 void *(*realloc)(void*,int);
118197 int (*reset)(sqlite3_stmt*pStmt);
118198 void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
118199 void (*result_double)(sqlite3_context*,double);
118200 void (*result_error)(sqlite3_context*,const char*,int);
118201 void (*result_error16)(sqlite3_context*,const void*,int);
118202 void (*result_int)(sqlite3_context*,int);
118203 void (*result_int64)(sqlite3_context*,sqlite_int64);
118204 void (*result_null)(sqlite3_context*);
118205 void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
118206 void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
118207 void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
118208 void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
118209 void (*result_value)(sqlite3_context*,sqlite3_value*);
118210 void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
118211 int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
118212 const char*,const char*),void*);
118213 void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
118214 char * (*xsnprintf)(int,char*,const char*,...);
118215 int (*step)(sqlite3_stmt*);
118216 int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
118217 char const**,char const**,int*,int*,int*);
118218 void (*thread_cleanup)(void);
118219 int (*total_changes)(sqlite3*);
118220 void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
118221 int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
118222 void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
118223 sqlite_int64),void*);
118224 void * (*user_data)(sqlite3_context*);
118225 const void * (*value_blob)(sqlite3_value*);
118226 int (*value_bytes)(sqlite3_value*);
118227 int (*value_bytes16)(sqlite3_value*);
118228 double (*value_double)(sqlite3_value*);
118229 int (*value_int)(sqlite3_value*);
118230 sqlite_int64 (*value_int64)(sqlite3_value*);
118231 int (*value_numeric_type)(sqlite3_value*);
118232 const unsigned char * (*value_text)(sqlite3_value*);
118233 const void * (*value_text16)(sqlite3_value*);
118234 const void * (*value_text16be)(sqlite3_value*);
118235 const void * (*value_text16le)(sqlite3_value*);
118236 int (*value_type)(sqlite3_value*);
118237 char *(*vmprintf)(const char*,va_list);
118238 /* Added ??? */
118239 int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
118240 /* Added by 3.3.13 */
118241 int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
118242 int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
118243 int (*clear_bindings)(sqlite3_stmt*);
118244 /* Added by 3.4.1 */
118245 int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
118246 void (*xDestroy)(void *));
118247 /* Added by 3.5.0 */
118248 int (*bind_zeroblob)(sqlite3_stmt*,int,int);
118249 int (*blob_bytes)(sqlite3_blob*);
118250 int (*blob_close)(sqlite3_blob*);
118251 int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
118252 int,sqlite3_blob**);
118253 int (*blob_read)(sqlite3_blob*,void*,int,int);
118254 int (*blob_write)(sqlite3_blob*,const void*,int,int);
118255 int (*create_collation_v2)(sqlite3*,const char*,int,void*,
118256 int(*)(void*,int,const void*,int,const void*),
118257 void(*)(void*));
118258 int (*file_control)(sqlite3*,const char*,int,void*);
118259 sqlite3_int64 (*memory_highwater)(int);
118260 sqlite3_int64 (*memory_used)(void);
118261 sqlite3_mutex *(*mutex_alloc)(int);
118262 void (*mutex_enter)(sqlite3_mutex*);
118263 void (*mutex_free)(sqlite3_mutex*);
118264 void (*mutex_leave)(sqlite3_mutex*);
118265 int (*mutex_try)(sqlite3_mutex*);
118266 int (*open_v2)(const char*,sqlite3**,int,const char*);
118267 int (*release_memory)(int);
118268 void (*result_error_nomem)(sqlite3_context*);
118269 void (*result_error_toobig)(sqlite3_context*);
118270 int (*sleep)(int);
118271 void (*soft_heap_limit)(int);
118272 sqlite3_vfs *(*vfs_find)(const char*);
118273 int (*vfs_register)(sqlite3_vfs*,int);
118274 int (*vfs_unregister)(sqlite3_vfs*);
118275 int (*xthreadsafe)(void);
118276 void (*result_zeroblob)(sqlite3_context*,int);
118277 void (*result_error_code)(sqlite3_context*,int);
118278 int (*test_control)(int, ...);
118279 void (*randomness)(int,void*);
118280 sqlite3 *(*context_db_handle)(sqlite3_context*);
118281 int (*extended_result_codes)(sqlite3*,int);
118282 int (*limit)(sqlite3*,int,int);
118283 sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
118284 const char *(*sql)(sqlite3_stmt*);
118285 int (*status)(int,int*,int*,int);
118286 int (*backup_finish)(sqlite3_backup*);
118287 sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
118288 int (*backup_pagecount)(sqlite3_backup*);
118289 int (*backup_remaining)(sqlite3_backup*);
118290 int (*backup_step)(sqlite3_backup*,int);
118291 const char *(*compileoption_get)(int);
118292 int (*compileoption_used)(const char*);
118293 int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
118294 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
118295 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
118296 void (*xFinal)(sqlite3_context*),
118297 void(*xDestroy)(void*));
118298 int (*db_config)(sqlite3*,int,...);
118299 sqlite3_mutex *(*db_mutex)(sqlite3*);
118300 int (*db_status)(sqlite3*,int,int*,int*,int);
118301 int (*extended_errcode)(sqlite3*);
118302 void (*log)(int,const char*,...);
118303 sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
118304 const char *(*sourceid)(void);
118305 int (*stmt_status)(sqlite3_stmt*,int,int);
118306 int (*strnicmp)(const char*,const char*,int);
118307 int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
118308 int (*wal_autocheckpoint)(sqlite3*,int);
118309 int (*wal_checkpoint)(sqlite3*,const char*);
118310 void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
118311 int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
118312 int (*vtab_config)(sqlite3*,int op,...);
118313 int (*vtab_on_conflict)(sqlite3*);
118314 /* Version 3.7.16 and later */
118315 int (*close_v2)(sqlite3*);
118316 const char *(*db_filename)(sqlite3*,const char*);
118317 int (*db_readonly)(sqlite3*,const char*);
118318 int (*db_release_memory)(sqlite3*);
118319 const char *(*errstr)(int);
118320 int (*stmt_busy)(sqlite3_stmt*);
118321 int (*stmt_readonly)(sqlite3_stmt*);
118322 int (*stricmp)(const char*,const char*);
118323 int (*uri_boolean)(const char*,const char*,int);
118324 sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64);
118325 const char *(*uri_parameter)(const char*,const char*);
118326 char *(*xvsnprintf)(int,char*,const char*,va_list);
118327 int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*);
118328 /* Version 3.8.7 and later */
118329 int (*auto_extension)(void(*)(void));
118330 int (*bind_blob64)(sqlite3_stmt*,int,const void*,sqlite3_uint64,
118331 void(*)(void*));
118332 int (*bind_text64)(sqlite3_stmt*,int,const char*,sqlite3_uint64,
118333 void(*)(void*),unsigned char);
118334 int (*cancel_auto_extension)(void(*)(void));
118335 int (*load_extension)(sqlite3*,const char*,const char*,char**);
118336 void *(*malloc64)(sqlite3_uint64);
118337 sqlite3_uint64 (*msize)(void*);
118338 void *(*realloc64)(void*,sqlite3_uint64);
118339 void (*reset_auto_extension)(void);
118340 void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64,
118341 void(*)(void*));
118342 void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
118343 void(*)(void*), unsigned char);
118344 int (*strglob)(const char*,const char*);
118345 /* Version 3.8.11 and later */
118346 sqlite3_value *(*value_dup)(const sqlite3_value*);
118347 void (*value_free)(sqlite3_value*);
118348 int (*result_zeroblob64)(sqlite3_context*,sqlite3_uint64);
118349 int (*bind_zeroblob64)(sqlite3_stmt*, int, sqlite3_uint64);
118350 /* Version 3.9.0 and later */
118351 unsigned int (*value_subtype)(sqlite3_value*);
118352 void (*result_subtype)(sqlite3_context*,unsigned int);
118353 /* Version 3.10.0 and later */
118354 int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int);
118355 int (*strlike)(const char*,const char*,unsigned int);
118356 int (*db_cacheflush)(sqlite3*);
118357 /* Version 3.12.0 and later */
118358 int (*system_errno)(sqlite3*);
118359 /* Version 3.14.0 and later */
118360 int (*trace_v2)(sqlite3*,unsigned,int(*)(unsigned,void*,void*,void*),void*);
118361 char *(*expanded_sql)(sqlite3_stmt*);
118362 /* Version 3.18.0 and later */
118363 void (*set_last_insert_rowid)(sqlite3*,sqlite3_int64);
118364 /* Version 3.20.0 and later */
118365 int (*prepare_v3)(sqlite3*,const char*,int,unsigned int,
118366 sqlite3_stmt**,const char**);
118367 int (*prepare16_v3)(sqlite3*,const void*,int,unsigned int,
118368 sqlite3_stmt**,const void**);
118369 int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*,void(*)(void*));
118370 void (*result_pointer)(sqlite3_context*,void*,const char*,void(*)(void*));
118371 void *(*value_pointer)(sqlite3_value*,const char*);
118372 int (*vtab_nochange)(sqlite3_context*);
118373 int (*value_nochange)(sqlite3_value*);
118374 const char *(*vtab_collation)(sqlite3_index_info*,int);
118375 /* Version 3.24.0 and later */
118376 int (*keyword_count)(void);
118377 int (*keyword_name)(int,const char**,int*);
118378 int (*keyword_check)(const char*,int);
118379 sqlite3_str *(*str_new)(sqlite3*);
118380 char *(*str_finish)(sqlite3_str*);
118381 void (*str_appendf)(sqlite3_str*, const char *zFormat, ...);
118382 void (*str_vappendf)(sqlite3_str*, const char *zFormat, va_list);
118383 void (*str_append)(sqlite3_str*, const char *zIn, int N);
118384 void (*str_appendall)(sqlite3_str*, const char *zIn);
118385 void (*str_appendchar)(sqlite3_str*, int N, char C);
118386 void (*str_reset)(sqlite3_str*);
118387 int (*str_errcode)(sqlite3_str*);
118388 int (*str_length)(sqlite3_str*);
118389 char *(*str_value)(sqlite3_str*);
118390 /* Version 3.25.0 and later */
118391 int (*create_window_function)(sqlite3*,const char*,int,int,void*,
118392 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
118393 void (*xFinal)(sqlite3_context*),
118394 void (*xValue)(sqlite3_context*),
118395 void (*xInv)(sqlite3_context*,int,sqlite3_value**),
118396 void(*xDestroy)(void*));
118397 /* Version 3.26.0 and later */
118398 const char *(*normalized_sql)(sqlite3_stmt*);
118399};
118400
118401/*
118402** This is the function signature used for all extension entry points. It
118403** is also defined in the file "loadext.c".
118404*/
118405typedef int (*sqlite3_loadext_entry)(
118406 sqlite3 *db, /* Handle to the database. */
118407 char **pzErrMsg, /* Used to set error string on failure. */
118408 const sqlite3_api_routines *pThunk /* Extension API function pointers. */
118409);
118410
118411/*
118412** The following macros redefine the API routines so that they are
118413** redirected through the global sqlite3_api structure.
118414**
118415** This header file is also used by the loadext.c source file
118416** (part of the main SQLite library - not an extension) so that
118417** it can get access to the sqlite3_api_routines structure
118418** definition. But the main library does not want to redefine
118419** the API. So the redefinition macros are only valid if the
118420** SQLITE_CORE macros is undefined.
118421*/
118422#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
118423#define sqlite3_aggregate_context sqlite3_api->aggregate_context
118424#ifndef SQLITE_OMIT_DEPRECATED
118425#define sqlite3_aggregate_count sqlite3_api->aggregate_count
118426#endif
118427#define sqlite3_bind_blob sqlite3_api->bind_blob
118428#define sqlite3_bind_double sqlite3_api->bind_double
118429#define sqlite3_bind_int sqlite3_api->bind_int
118430#define sqlite3_bind_int64 sqlite3_api->bind_int64
118431#define sqlite3_bind_null sqlite3_api->bind_null
118432#define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
118433#define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
118434#define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
118435#define sqlite3_bind_text sqlite3_api->bind_text
118436#define sqlite3_bind_text16 sqlite3_api->bind_text16
118437#define sqlite3_bind_value sqlite3_api->bind_value
118438#define sqlite3_busy_handler sqlite3_api->busy_handler
118439#define sqlite3_busy_timeout sqlite3_api->busy_timeout
118440#define sqlite3_changes sqlite3_api->changes
118441#define sqlite3_close sqlite3_api->close
118442#define sqlite3_collation_needed sqlite3_api->collation_needed
118443#define sqlite3_collation_needed16 sqlite3_api->collation_needed16
118444#define sqlite3_column_blob sqlite3_api->column_blob
118445#define sqlite3_column_bytes sqlite3_api->column_bytes
118446#define sqlite3_column_bytes16 sqlite3_api->column_bytes16
118447#define sqlite3_column_count sqlite3_api->column_count
118448#define sqlite3_column_database_name sqlite3_api->column_database_name
118449#define sqlite3_column_database_name16 sqlite3_api->column_database_name16
118450#define sqlite3_column_decltype sqlite3_api->column_decltype
118451#define sqlite3_column_decltype16 sqlite3_api->column_decltype16
118452#define sqlite3_column_double sqlite3_api->column_double
118453#define sqlite3_column_int sqlite3_api->column_int
118454#define sqlite3_column_int64 sqlite3_api->column_int64
118455#define sqlite3_column_name sqlite3_api->column_name
118456#define sqlite3_column_name16 sqlite3_api->column_name16
118457#define sqlite3_column_origin_name sqlite3_api->column_origin_name
118458#define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
118459#define sqlite3_column_table_name sqlite3_api->column_table_name
118460#define sqlite3_column_table_name16 sqlite3_api->column_table_name16
118461#define sqlite3_column_text sqlite3_api->column_text
118462#define sqlite3_column_text16 sqlite3_api->column_text16
118463#define sqlite3_column_type sqlite3_api->column_type
118464#define sqlite3_column_value sqlite3_api->column_value
118465#define sqlite3_commit_hook sqlite3_api->commit_hook
118466#define sqlite3_complete sqlite3_api->complete
118467#define sqlite3_complete16 sqlite3_api->complete16
118468#define sqlite3_create_collation sqlite3_api->create_collation
118469#define sqlite3_create_collation16 sqlite3_api->create_collation16
118470#define sqlite3_create_function sqlite3_api->create_function
118471#define sqlite3_create_function16 sqlite3_api->create_function16
118472#define sqlite3_create_module sqlite3_api->create_module
118473#define sqlite3_create_module_v2 sqlite3_api->create_module_v2
118474#define sqlite3_data_count sqlite3_api->data_count
118475#define sqlite3_db_handle sqlite3_api->db_handle
118476#define sqlite3_declare_vtab sqlite3_api->declare_vtab
118477#define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
118478#define sqlite3_errcode sqlite3_api->errcode
118479#define sqlite3_errmsg sqlite3_api->errmsg
118480#define sqlite3_errmsg16 sqlite3_api->errmsg16
118481#define sqlite3_exec sqlite3_api->exec
118482#ifndef SQLITE_OMIT_DEPRECATED
118483#define sqlite3_expired sqlite3_api->expired
118484#endif
118485#define sqlite3_finalize sqlite3_api->finalize
118486#define sqlite3_free sqlite3_api->free
118487#define sqlite3_free_table sqlite3_api->free_table
118488#define sqlite3_get_autocommit sqlite3_api->get_autocommit
118489#define sqlite3_get_auxdata sqlite3_api->get_auxdata
118490#define sqlite3_get_table sqlite3_api->get_table
118491#ifndef SQLITE_OMIT_DEPRECATED
118492#define sqlite3_global_recover sqlite3_api->global_recover
118493#endif
118494#define sqlite3_interrupt sqlite3_api->interruptx
118495#define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
118496#define sqlite3_libversion sqlite3_api->libversion
118497#define sqlite3_libversion_number sqlite3_api->libversion_number
118498#define sqlite3_malloc sqlite3_api->malloc
118499#define sqlite3_mprintf sqlite3_api->mprintf
118500#define sqlite3_open sqlite3_api->open
118501#define sqlite3_open16 sqlite3_api->open16
118502#define sqlite3_prepare sqlite3_api->prepare
118503#define sqlite3_prepare16 sqlite3_api->prepare16
118504#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
118505#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
118506#define sqlite3_profile sqlite3_api->profile
118507#define sqlite3_progress_handler sqlite3_api->progress_handler
118508#define sqlite3_realloc sqlite3_api->realloc
118509#define sqlite3_reset sqlite3_api->reset
118510#define sqlite3_result_blob sqlite3_api->result_blob
118511#define sqlite3_result_double sqlite3_api->result_double
118512#define sqlite3_result_error sqlite3_api->result_error
118513#define sqlite3_result_error16 sqlite3_api->result_error16
118514#define sqlite3_result_int sqlite3_api->result_int
118515#define sqlite3_result_int64 sqlite3_api->result_int64
118516#define sqlite3_result_null sqlite3_api->result_null
118517#define sqlite3_result_text sqlite3_api->result_text
118518#define sqlite3_result_text16 sqlite3_api->result_text16
118519#define sqlite3_result_text16be sqlite3_api->result_text16be
118520#define sqlite3_result_text16le sqlite3_api->result_text16le
118521#define sqlite3_result_value sqlite3_api->result_value
118522#define sqlite3_rollback_hook sqlite3_api->rollback_hook
118523#define sqlite3_set_authorizer sqlite3_api->set_authorizer
118524#define sqlite3_set_auxdata sqlite3_api->set_auxdata
118525#define sqlite3_snprintf sqlite3_api->xsnprintf
118526#define sqlite3_step sqlite3_api->step
118527#define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
118528#define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
118529#define sqlite3_total_changes sqlite3_api->total_changes
118530#define sqlite3_trace sqlite3_api->trace
118531#ifndef SQLITE_OMIT_DEPRECATED
118532#define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
118533#endif
118534#define sqlite3_update_hook sqlite3_api->update_hook
118535#define sqlite3_user_data sqlite3_api->user_data
118536#define sqlite3_value_blob sqlite3_api->value_blob
118537#define sqlite3_value_bytes sqlite3_api->value_bytes
118538#define sqlite3_value_bytes16 sqlite3_api->value_bytes16
118539#define sqlite3_value_double sqlite3_api->value_double
118540#define sqlite3_value_int sqlite3_api->value_int
118541#define sqlite3_value_int64 sqlite3_api->value_int64
118542#define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
118543#define sqlite3_value_text sqlite3_api->value_text
118544#define sqlite3_value_text16 sqlite3_api->value_text16
118545#define sqlite3_value_text16be sqlite3_api->value_text16be
118546#define sqlite3_value_text16le sqlite3_api->value_text16le
118547#define sqlite3_value_type sqlite3_api->value_type
118548#define sqlite3_vmprintf sqlite3_api->vmprintf
118549#define sqlite3_vsnprintf sqlite3_api->xvsnprintf
118550#define sqlite3_overload_function sqlite3_api->overload_function
118551#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
118552#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
118553#define sqlite3_clear_bindings sqlite3_api->clear_bindings
118554#define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
118555#define sqlite3_blob_bytes sqlite3_api->blob_bytes
118556#define sqlite3_blob_close sqlite3_api->blob_close
118557#define sqlite3_blob_open sqlite3_api->blob_open
118558#define sqlite3_blob_read sqlite3_api->blob_read
118559#define sqlite3_blob_write sqlite3_api->blob_write
118560#define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
118561#define sqlite3_file_control sqlite3_api->file_control
118562#define sqlite3_memory_highwater sqlite3_api->memory_highwater
118563#define sqlite3_memory_used sqlite3_api->memory_used
118564#define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
118565#define sqlite3_mutex_enter sqlite3_api->mutex_enter
118566#define sqlite3_mutex_free sqlite3_api->mutex_free
118567#define sqlite3_mutex_leave sqlite3_api->mutex_leave
118568#define sqlite3_mutex_try sqlite3_api->mutex_try
118569#define sqlite3_open_v2 sqlite3_api->open_v2
118570#define sqlite3_release_memory sqlite3_api->release_memory
118571#define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
118572#define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
118573#define sqlite3_sleep sqlite3_api->sleep
118574#define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
118575#define sqlite3_vfs_find sqlite3_api->vfs_find
118576#define sqlite3_vfs_register sqlite3_api->vfs_register
118577#define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
118578#define sqlite3_threadsafe sqlite3_api->xthreadsafe
118579#define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
118580#define sqlite3_result_error_code sqlite3_api->result_error_code
118581#define sqlite3_test_control sqlite3_api->test_control
118582#define sqlite3_randomness sqlite3_api->randomness
118583#define sqlite3_context_db_handle sqlite3_api->context_db_handle
118584#define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
118585#define sqlite3_limit sqlite3_api->limit
118586#define sqlite3_next_stmt sqlite3_api->next_stmt
118587#define sqlite3_sql sqlite3_api->sql
118588#define sqlite3_status sqlite3_api->status
118589#define sqlite3_backup_finish sqlite3_api->backup_finish
118590#define sqlite3_backup_init sqlite3_api->backup_init
118591#define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
118592#define sqlite3_backup_remaining sqlite3_api->backup_remaining
118593#define sqlite3_backup_step sqlite3_api->backup_step
118594#define sqlite3_compileoption_get sqlite3_api->compileoption_get
118595#define sqlite3_compileoption_used sqlite3_api->compileoption_used
118596#define sqlite3_create_function_v2 sqlite3_api->create_function_v2
118597#define sqlite3_db_config sqlite3_api->db_config
118598#define sqlite3_db_mutex sqlite3_api->db_mutex
118599#define sqlite3_db_status sqlite3_api->db_status
118600#define sqlite3_extended_errcode sqlite3_api->extended_errcode
118601#define sqlite3_log sqlite3_api->log
118602#define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
118603#define sqlite3_sourceid sqlite3_api->sourceid
118604#define sqlite3_stmt_status sqlite3_api->stmt_status
118605#define sqlite3_strnicmp sqlite3_api->strnicmp
118606#define sqlite3_unlock_notify sqlite3_api->unlock_notify
118607#define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
118608#define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
118609#define sqlite3_wal_hook sqlite3_api->wal_hook
118610#define sqlite3_blob_reopen sqlite3_api->blob_reopen
118611#define sqlite3_vtab_config sqlite3_api->vtab_config
118612#define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
118613/* Version 3.7.16 and later */
118614#define sqlite3_close_v2 sqlite3_api->close_v2
118615#define sqlite3_db_filename sqlite3_api->db_filename
118616#define sqlite3_db_readonly sqlite3_api->db_readonly
118617#define sqlite3_db_release_memory sqlite3_api->db_release_memory
118618#define sqlite3_errstr sqlite3_api->errstr
118619#define sqlite3_stmt_busy sqlite3_api->stmt_busy
118620#define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
118621#define sqlite3_stricmp sqlite3_api->stricmp
118622#define sqlite3_uri_boolean sqlite3_api->uri_boolean
118623#define sqlite3_uri_int64 sqlite3_api->uri_int64
118624#define sqlite3_uri_parameter sqlite3_api->uri_parameter
118625#define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf
118626#define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
118627/* Version 3.8.7 and later */
118628#define sqlite3_auto_extension sqlite3_api->auto_extension
118629#define sqlite3_bind_blob64 sqlite3_api->bind_blob64
118630#define sqlite3_bind_text64 sqlite3_api->bind_text64
118631#define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
118632#define sqlite3_load_extension sqlite3_api->load_extension
118633#define sqlite3_malloc64 sqlite3_api->malloc64
118634#define sqlite3_msize sqlite3_api->msize
118635#define sqlite3_realloc64 sqlite3_api->realloc64
118636#define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
118637#define sqlite3_result_blob64 sqlite3_api->result_blob64
118638#define sqlite3_result_text64 sqlite3_api->result_text64
118639#define sqlite3_strglob sqlite3_api->strglob
118640/* Version 3.8.11 and later */
118641#define sqlite3_value_dup sqlite3_api->value_dup
118642#define sqlite3_value_free sqlite3_api->value_free
118643#define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
118644#define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
118645/* Version 3.9.0 and later */
118646#define sqlite3_value_subtype sqlite3_api->value_subtype
118647#define sqlite3_result_subtype sqlite3_api->result_subtype
118648/* Version 3.10.0 and later */
118649#define sqlite3_status64 sqlite3_api->status64
118650#define sqlite3_strlike sqlite3_api->strlike
118651#define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
118652/* Version 3.12.0 and later */
118653#define sqlite3_system_errno sqlite3_api->system_errno
118654/* Version 3.14.0 and later */
118655#define sqlite3_trace_v2 sqlite3_api->trace_v2
118656#define sqlite3_expanded_sql sqlite3_api->expanded_sql
118657/* Version 3.18.0 and later */
118658#define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid
118659/* Version 3.20.0 and later */
118660#define sqlite3_prepare_v3 sqlite3_api->prepare_v3
118661#define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3
118662#define sqlite3_bind_pointer sqlite3_api->bind_pointer
118663#define sqlite3_result_pointer sqlite3_api->result_pointer
118664#define sqlite3_value_pointer sqlite3_api->value_pointer
118665/* Version 3.22.0 and later */
118666#define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
118667#define sqlite3_value_nochange sqlite3_api->value_nochange
118668#define sqlite3_vtab_collation sqlite3_api->vtab_collation
118669/* Version 3.24.0 and later */
118670#define sqlite3_keyword_count sqlite3_api->keyword_count
118671#define sqlite3_keyword_name sqlite3_api->keyword_name
118672#define sqlite3_keyword_check sqlite3_api->keyword_check
118673#define sqlite3_str_new sqlite3_api->str_new
118674#define sqlite3_str_finish sqlite3_api->str_finish
118675#define sqlite3_str_appendf sqlite3_api->str_appendf
118676#define sqlite3_str_vappendf sqlite3_api->str_vappendf
118677#define sqlite3_str_append sqlite3_api->str_append
118678#define sqlite3_str_appendall sqlite3_api->str_appendall
118679#define sqlite3_str_appendchar sqlite3_api->str_appendchar
118680#define sqlite3_str_reset sqlite3_api->str_reset
118681#define sqlite3_str_errcode sqlite3_api->str_errcode
118682#define sqlite3_str_length sqlite3_api->str_length
118683#define sqlite3_str_value sqlite3_api->str_value
118684/* Version 3.25.0 and later */
118685#define sqlite3_create_window_function sqlite3_api->create_window_function
118686/* Version 3.26.0 and later */
118687#define sqlite3_normalized_sql sqlite3_api->normalized_sql
118688#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
118689
118690#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
118691 /* This case when the file really is being compiled as a loadable
118692 ** extension */
118693# define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api=0;
118694# define SQLITE_EXTENSION_INIT2(v) sqlite3_api=v;
118695# define SQLITE_EXTENSION_INIT3 \
118696 extern const sqlite3_api_routines *sqlite3_api;
118697#else
118698 /* This case when the file is being statically linked into the
118699 ** application */
118700# define SQLITE_EXTENSION_INIT1 /*no-op*/
118701# define SQLITE_EXTENSION_INIT2(v) (void)v; /* unused parameter */
118702# define SQLITE_EXTENSION_INIT3 /*no-op*/
118703#endif
118704
118705#endif /* SQLITE3EXT_H */
118706
118707/************** End of sqlite3ext.h ******************************************/
118708/************** Continuing where we left off in loadext.c ********************/
118709/* #include "sqliteInt.h" */
118710
118711#ifndef SQLITE_OMIT_LOAD_EXTENSION
118712/*
118713** Some API routines are omitted when various features are
118714** excluded from a build of SQLite. Substitute a NULL pointer
118715** for any missing APIs.
118716*/
118717#ifndef SQLITE_ENABLE_COLUMN_METADATA
118718# define sqlite3_column_database_name 0
118719# define sqlite3_column_database_name16 0
118720# define sqlite3_column_table_name 0
118721# define sqlite3_column_table_name16 0
118722# define sqlite3_column_origin_name 0
118723# define sqlite3_column_origin_name16 0
118724#endif
118725
118726#ifdef SQLITE_OMIT_AUTHORIZATION
118727# define sqlite3_set_authorizer 0
118728#endif
118729
118730#ifdef SQLITE_OMIT_UTF16
118731# define sqlite3_bind_text16 0
118732# define sqlite3_collation_needed16 0
118733# define sqlite3_column_decltype16 0
118734# define sqlite3_column_name16 0
118735# define sqlite3_column_text16 0
118736# define sqlite3_complete16 0
118737# define sqlite3_create_collation16 0
118738# define sqlite3_create_function16 0
118739# define sqlite3_errmsg16 0
118740# define sqlite3_open16 0
118741# define sqlite3_prepare16 0
118742# define sqlite3_prepare16_v2 0
118743# define sqlite3_prepare16_v3 0
118744# define sqlite3_result_error16 0
118745# define sqlite3_result_text16 0
118746# define sqlite3_result_text16be 0
118747# define sqlite3_result_text16le 0
118748# define sqlite3_value_text16 0
118749# define sqlite3_value_text16be 0
118750# define sqlite3_value_text16le 0
118751# define sqlite3_column_database_name16 0
118752# define sqlite3_column_table_name16 0
118753# define sqlite3_column_origin_name16 0
118754#endif
118755
118756#ifdef SQLITE_OMIT_COMPLETE
118757# define sqlite3_complete 0
118758# define sqlite3_complete16 0
118759#endif
118760
118761#ifdef SQLITE_OMIT_DECLTYPE
118762# define sqlite3_column_decltype16 0
118763# define sqlite3_column_decltype 0
118764#endif
118765
118766#ifdef SQLITE_OMIT_PROGRESS_CALLBACK
118767# define sqlite3_progress_handler 0
118768#endif
118769
118770#ifdef SQLITE_OMIT_VIRTUALTABLE
118771# define sqlite3_create_module 0
118772# define sqlite3_create_module_v2 0
118773# define sqlite3_declare_vtab 0
118774# define sqlite3_vtab_config 0
118775# define sqlite3_vtab_on_conflict 0
118776# define sqlite3_vtab_collation 0
118777#endif
118778
118779#ifdef SQLITE_OMIT_SHARED_CACHE
118780# define sqlite3_enable_shared_cache 0
118781#endif
118782
118783#if defined(SQLITE_OMIT_TRACE) || defined(SQLITE_OMIT_DEPRECATED)
118784# define sqlite3_profile 0
118785# define sqlite3_trace 0
118786#endif
118787
118788#ifdef SQLITE_OMIT_GET_TABLE
118789# define sqlite3_free_table 0
118790# define sqlite3_get_table 0
118791#endif
118792
118793#ifdef SQLITE_OMIT_INCRBLOB
118794#define sqlite3_bind_zeroblob 0
118795#define sqlite3_blob_bytes 0
118796#define sqlite3_blob_close 0
118797#define sqlite3_blob_open 0
118798#define sqlite3_blob_read 0
118799#define sqlite3_blob_write 0
118800#define sqlite3_blob_reopen 0
118801#endif
118802
118803#if defined(SQLITE_OMIT_TRACE)
118804# define sqlite3_trace_v2 0
118805#endif
118806
118807/*
118808** The following structure contains pointers to all SQLite API routines.
118809** A pointer to this structure is passed into extensions when they are
118810** loaded so that the extension can make calls back into the SQLite
118811** library.
118812**
118813** When adding new APIs, add them to the bottom of this structure
118814** in order to preserve backwards compatibility.
118815**
118816** Extensions that use newer APIs should first call the
118817** sqlite3_libversion_number() to make sure that the API they
118818** intend to use is supported by the library. Extensions should
118819** also check to make sure that the pointer to the function is
118820** not NULL before calling it.
118821*/
118822static const sqlite3_api_routines sqlite3Apis = {
118823 sqlite3_aggregate_context,
118824#ifndef SQLITE_OMIT_DEPRECATED
118825 sqlite3_aggregate_count,
118826#else
118827 0,
118828#endif
118829 sqlite3_bind_blob,
118830 sqlite3_bind_double,
118831 sqlite3_bind_int,
118832 sqlite3_bind_int64,
118833 sqlite3_bind_null,
118834 sqlite3_bind_parameter_count,
118835 sqlite3_bind_parameter_index,
118836 sqlite3_bind_parameter_name,
118837 sqlite3_bind_text,
118838 sqlite3_bind_text16,
118839 sqlite3_bind_value,
118840 sqlite3_busy_handler,
118841 sqlite3_busy_timeout,
118842 sqlite3_changes,
118843 sqlite3_close,
118844 sqlite3_collation_needed,
118845 sqlite3_collation_needed16,
118846 sqlite3_column_blob,
118847 sqlite3_column_bytes,
118848 sqlite3_column_bytes16,
118849 sqlite3_column_count,
118850 sqlite3_column_database_name,
118851 sqlite3_column_database_name16,
118852 sqlite3_column_decltype,
118853 sqlite3_column_decltype16,
118854 sqlite3_column_double,
118855 sqlite3_column_int,
118856 sqlite3_column_int64,
118857 sqlite3_column_name,
118858 sqlite3_column_name16,
118859 sqlite3_column_origin_name,
118860 sqlite3_column_origin_name16,
118861 sqlite3_column_table_name,
118862 sqlite3_column_table_name16,
118863 sqlite3_column_text,
118864 sqlite3_column_text16,
118865 sqlite3_column_type,
118866 sqlite3_column_value,
118867 sqlite3_commit_hook,
118868 sqlite3_complete,
118869 sqlite3_complete16,
118870 sqlite3_create_collation,
118871 sqlite3_create_collation16,
118872 sqlite3_create_function,
118873 sqlite3_create_function16,
118874 sqlite3_create_module,
118875 sqlite3_data_count,
118876 sqlite3_db_handle,
118877 sqlite3_declare_vtab,
118878 sqlite3_enable_shared_cache,
118879 sqlite3_errcode,
118880 sqlite3_errmsg,
118881 sqlite3_errmsg16,
118882 sqlite3_exec,
118883#ifndef SQLITE_OMIT_DEPRECATED
118884 sqlite3_expired,
118885#else
118886 0,
118887#endif
118888 sqlite3_finalize,
118889 sqlite3_free,
118890 sqlite3_free_table,
118891 sqlite3_get_autocommit,
118892 sqlite3_get_auxdata,
118893 sqlite3_get_table,
118894 0, /* Was sqlite3_global_recover(), but that function is deprecated */
118895 sqlite3_interrupt,
118896 sqlite3_last_insert_rowid,
118897 sqlite3_libversion,
118898 sqlite3_libversion_number,
118899 sqlite3_malloc,
118900 sqlite3_mprintf,
118901 sqlite3_open,
118902 sqlite3_open16,
118903 sqlite3_prepare,
118904 sqlite3_prepare16,
118905 sqlite3_profile,
118906 sqlite3_progress_handler,
118907 sqlite3_realloc,
118908 sqlite3_reset,
118909 sqlite3_result_blob,
118910 sqlite3_result_double,
118911 sqlite3_result_error,
118912 sqlite3_result_error16,
118913 sqlite3_result_int,
118914 sqlite3_result_int64,
118915 sqlite3_result_null,
118916 sqlite3_result_text,
118917 sqlite3_result_text16,
118918 sqlite3_result_text16be,
118919 sqlite3_result_text16le,
118920 sqlite3_result_value,
118921 sqlite3_rollback_hook,
118922 sqlite3_set_authorizer,
118923 sqlite3_set_auxdata,
118924 sqlite3_snprintf,
118925 sqlite3_step,
118926 sqlite3_table_column_metadata,
118927#ifndef SQLITE_OMIT_DEPRECATED
118928 sqlite3_thread_cleanup,
118929#else
118930 0,
118931#endif
118932 sqlite3_total_changes,
118933 sqlite3_trace,
118934#ifndef SQLITE_OMIT_DEPRECATED
118935 sqlite3_transfer_bindings,
118936#else
118937 0,
118938#endif
118939 sqlite3_update_hook,
118940 sqlite3_user_data,
118941 sqlite3_value_blob,
118942 sqlite3_value_bytes,
118943 sqlite3_value_bytes16,
118944 sqlite3_value_double,
118945 sqlite3_value_int,
118946 sqlite3_value_int64,
118947 sqlite3_value_numeric_type,
118948 sqlite3_value_text,
118949 sqlite3_value_text16,
118950 sqlite3_value_text16be,
118951 sqlite3_value_text16le,
118952 sqlite3_value_type,
118953 sqlite3_vmprintf,
118954 /*
118955 ** The original API set ends here. All extensions can call any
118956 ** of the APIs above provided that the pointer is not NULL. But
118957 ** before calling APIs that follow, extension should check the
118958 ** sqlite3_libversion_number() to make sure they are dealing with
118959 ** a library that is new enough to support that API.
118960 *************************************************************************
118961 */
118962 sqlite3_overload_function,
118963
118964 /*
118965 ** Added after 3.3.13
118966 */
118967 sqlite3_prepare_v2,
118968 sqlite3_prepare16_v2,
118969 sqlite3_clear_bindings,
118970
118971 /*
118972 ** Added for 3.4.1
118973 */
118974 sqlite3_create_module_v2,
118975
118976 /*
118977 ** Added for 3.5.0
118978 */
118979 sqlite3_bind_zeroblob,
118980 sqlite3_blob_bytes,
118981 sqlite3_blob_close,
118982 sqlite3_blob_open,
118983 sqlite3_blob_read,
118984 sqlite3_blob_write,
118985 sqlite3_create_collation_v2,
118986 sqlite3_file_control,
118987 sqlite3_memory_highwater,
118988 sqlite3_memory_used,
118989#ifdef SQLITE_MUTEX_OMIT
118990 0,
118991 0,
118992 0,
118993 0,
118994 0,
118995#else
118996 sqlite3_mutex_alloc,
118997 sqlite3_mutex_enter,
118998 sqlite3_mutex_free,
118999 sqlite3_mutex_leave,
119000 sqlite3_mutex_try,
119001#endif
119002 sqlite3_open_v2,
119003 sqlite3_release_memory,
119004 sqlite3_result_error_nomem,
119005 sqlite3_result_error_toobig,
119006 sqlite3_sleep,
119007 sqlite3_soft_heap_limit,
119008 sqlite3_vfs_find,
119009 sqlite3_vfs_register,
119010 sqlite3_vfs_unregister,
119011
119012 /*
119013 ** Added for 3.5.8
119014 */
119015 sqlite3_threadsafe,
119016 sqlite3_result_zeroblob,
119017 sqlite3_result_error_code,
119018 sqlite3_test_control,
119019 sqlite3_randomness,
119020 sqlite3_context_db_handle,
119021
119022 /*
119023 ** Added for 3.6.0
119024 */
119025 sqlite3_extended_result_codes,
119026 sqlite3_limit,
119027 sqlite3_next_stmt,
119028 sqlite3_sql,
119029 sqlite3_status,
119030
119031 /*
119032 ** Added for 3.7.4
119033 */
119034 sqlite3_backup_finish,
119035 sqlite3_backup_init,
119036 sqlite3_backup_pagecount,
119037 sqlite3_backup_remaining,
119038 sqlite3_backup_step,
119039#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
119040 sqlite3_compileoption_get,
119041 sqlite3_compileoption_used,
119042#else
119043 0,
119044 0,
119045#endif
119046 sqlite3_create_function_v2,
119047 sqlite3_db_config,
119048 sqlite3_db_mutex,
119049 sqlite3_db_status,
119050 sqlite3_extended_errcode,
119051 sqlite3_log,
119052 sqlite3_soft_heap_limit64,
119053 sqlite3_sourceid,
119054 sqlite3_stmt_status,
119055 sqlite3_strnicmp,
119056#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
119057 sqlite3_unlock_notify,
119058#else
119059 0,
119060#endif
119061#ifndef SQLITE_OMIT_WAL
119062 sqlite3_wal_autocheckpoint,
119063 sqlite3_wal_checkpoint,
119064 sqlite3_wal_hook,
119065#else
119066 0,
119067 0,
119068 0,
119069#endif
119070 sqlite3_blob_reopen,
119071 sqlite3_vtab_config,
119072 sqlite3_vtab_on_conflict,
119073 sqlite3_close_v2,
119074 sqlite3_db_filename,
119075 sqlite3_db_readonly,
119076 sqlite3_db_release_memory,
119077 sqlite3_errstr,
119078 sqlite3_stmt_busy,
119079 sqlite3_stmt_readonly,
119080 sqlite3_stricmp,
119081 sqlite3_uri_boolean,
119082 sqlite3_uri_int64,
119083 sqlite3_uri_parameter,
119084 sqlite3_vsnprintf,
119085 sqlite3_wal_checkpoint_v2,
119086 /* Version 3.8.7 and later */
119087 sqlite3_auto_extension,
119088 sqlite3_bind_blob64,
119089 sqlite3_bind_text64,
119090 sqlite3_cancel_auto_extension,
119091 sqlite3_load_extension,
119092 sqlite3_malloc64,
119093 sqlite3_msize,
119094 sqlite3_realloc64,
119095 sqlite3_reset_auto_extension,
119096 sqlite3_result_blob64,
119097 sqlite3_result_text64,
119098 sqlite3_strglob,
119099 /* Version 3.8.11 and later */
119100 (sqlite3_value*(*)(const sqlite3_value*))sqlite3_value_dup,
119101 sqlite3_value_free,
119102 sqlite3_result_zeroblob64,
119103 sqlite3_bind_zeroblob64,
119104 /* Version 3.9.0 and later */
119105 sqlite3_value_subtype,
119106 sqlite3_result_subtype,
119107 /* Version 3.10.0 and later */
119108 sqlite3_status64,
119109 sqlite3_strlike,
119110 sqlite3_db_cacheflush,
119111 /* Version 3.12.0 and later */
119112 sqlite3_system_errno,
119113 /* Version 3.14.0 and later */
119114 sqlite3_trace_v2,
119115 sqlite3_expanded_sql,
119116 /* Version 3.18.0 and later */
119117 sqlite3_set_last_insert_rowid,
119118 /* Version 3.20.0 and later */
119119 sqlite3_prepare_v3,
119120 sqlite3_prepare16_v3,
119121 sqlite3_bind_pointer,
119122 sqlite3_result_pointer,
119123 sqlite3_value_pointer,
119124 /* Version 3.22.0 and later */
119125 sqlite3_vtab_nochange,
119126 sqlite3_value_nochange,
119127 sqlite3_vtab_collation,
119128 /* Version 3.24.0 and later */
119129 sqlite3_keyword_count,
119130 sqlite3_keyword_name,
119131 sqlite3_keyword_check,
119132 sqlite3_str_new,
119133 sqlite3_str_finish,
119134 sqlite3_str_appendf,
119135 sqlite3_str_vappendf,
119136 sqlite3_str_append,
119137 sqlite3_str_appendall,
119138 sqlite3_str_appendchar,
119139 sqlite3_str_reset,
119140 sqlite3_str_errcode,
119141 sqlite3_str_length,
119142 sqlite3_str_value,
119143 /* Version 3.25.0 and later */
119144 sqlite3_create_window_function,
119145 /* Version 3.26.0 and later */
119146#ifdef SQLITE_ENABLE_NORMALIZE
119147 sqlite3_normalized_sql
119148#else
119149 0
119150#endif
119151};
119152
119153/*
119154** Attempt to load an SQLite extension library contained in the file
119155** zFile. The entry point is zProc. zProc may be 0 in which case a
119156** default entry point name (sqlite3_extension_init) is used. Use
119157** of the default name is recommended.
119158**
119159** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
119160**
119161** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
119162** error message text. The calling function should free this memory
119163** by calling sqlite3DbFree(db, ).
119164*/
119165static int sqlite3LoadExtension(
119166 sqlite3 *db, /* Load the extension into this database connection */
119167 const char *zFile, /* Name of the shared library containing extension */
119168 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
119169 char **pzErrMsg /* Put error message here if not 0 */
119170){
119171 sqlite3_vfs *pVfs = db->pVfs;
119172 void *handle;
119173 sqlite3_loadext_entry xInit;
119174 char *zErrmsg = 0;
119175 const char *zEntry;
119176 char *zAltEntry = 0;
119177 void **aHandle;
119178 u64 nMsg = 300 + sqlite3Strlen30(zFile);
119179 int ii;
119180 int rc;
119181
119182 /* Shared library endings to try if zFile cannot be loaded as written */
119183 static const char *azEndings[] = {
119184#if SQLITE_OS_WIN
119185 "dll"
119186#elif defined(__APPLE__)
119187 "dylib"
119188#else
119189 "so"
119190#endif
119191 };
119192
119193
119194 if( pzErrMsg ) *pzErrMsg = 0;
119195
119196 /* Ticket #1863. To avoid a creating security problems for older
119197 ** applications that relink against newer versions of SQLite, the
119198 ** ability to run load_extension is turned off by default. One
119199 ** must call either sqlite3_enable_load_extension(db) or
119200 ** sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, 0)
119201 ** to turn on extension loading.
119202 */
119203 if( (db->flags & SQLITE_LoadExtension)==0 ){
119204 if( pzErrMsg ){
119205 *pzErrMsg = sqlite3_mprintf("not authorized");
119206 }
119207 return SQLITE_ERROR;
119208 }
119209
119210 zEntry = zProc ? zProc : "sqlite3_extension_init";
119211
119212 handle = sqlite3OsDlOpen(pVfs, zFile);
119213#if SQLITE_OS_UNIX || SQLITE_OS_WIN
119214 for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){
119215 char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]);
119216 if( zAltFile==0 ) return SQLITE_NOMEM_BKPT;
119217 handle = sqlite3OsDlOpen(pVfs, zAltFile);
119218 sqlite3_free(zAltFile);
119219 }
119220#endif
119221 if( handle==0 ){
119222 if( pzErrMsg ){
119223 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
119224 if( zErrmsg ){
119225 sqlite3_snprintf(nMsg, zErrmsg,
119226 "unable to open shared library [%s]", zFile);
119227 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
119228 }
119229 }
119230 return SQLITE_ERROR;
119231 }
119232 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
119233
119234 /* If no entry point was specified and the default legacy
119235 ** entry point name "sqlite3_extension_init" was not found, then
119236 ** construct an entry point name "sqlite3_X_init" where the X is
119237 ** replaced by the lowercase value of every ASCII alphabetic
119238 ** character in the filename after the last "/" upto the first ".",
119239 ** and eliding the first three characters if they are "lib".
119240 ** Examples:
119241 **
119242 ** /usr/local/lib/libExample5.4.3.so ==> sqlite3_example_init
119243 ** C:/lib/mathfuncs.dll ==> sqlite3_mathfuncs_init
119244 */
119245 if( xInit==0 && zProc==0 ){
119246 int iFile, iEntry, c;
119247 int ncFile = sqlite3Strlen30(zFile);
119248 zAltEntry = sqlite3_malloc64(ncFile+30);
119249 if( zAltEntry==0 ){
119250 sqlite3OsDlClose(pVfs, handle);
119251 return SQLITE_NOMEM_BKPT;
119252 }
119253 memcpy(zAltEntry, "sqlite3_", 8);
119254 for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){}
119255 iFile++;
119256 if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3;
119257 for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){
119258 if( sqlite3Isalpha(c) ){
119259 zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c];
119260 }
119261 }
119262 memcpy(zAltEntry+iEntry, "_init", 6);
119263 zEntry = zAltEntry;
119264 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
119265 }
119266 if( xInit==0 ){
119267 if( pzErrMsg ){
119268 nMsg += sqlite3Strlen30(zEntry);
119269 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
119270 if( zErrmsg ){
119271 sqlite3_snprintf(nMsg, zErrmsg,
119272 "no entry point [%s] in shared library [%s]", zEntry, zFile);
119273 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
119274 }
119275 }
119276 sqlite3OsDlClose(pVfs, handle);
119277 sqlite3_free(zAltEntry);
119278 return SQLITE_ERROR;
119279 }
119280 sqlite3_free(zAltEntry);
119281 rc = xInit(db, &zErrmsg, &sqlite3Apis);
119282 if( rc ){
119283 if( rc==SQLITE_OK_LOAD_PERMANENTLY ) return SQLITE_OK;
119284 if( pzErrMsg ){
119285 *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
119286 }
119287 sqlite3_free(zErrmsg);
119288 sqlite3OsDlClose(pVfs, handle);
119289 return SQLITE_ERROR;
119290 }
119291
119292 /* Append the new shared library handle to the db->aExtension array. */
119293 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
119294 if( aHandle==0 ){
119295 return SQLITE_NOMEM_BKPT;
119296 }
119297 if( db->nExtension>0 ){
119298 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
119299 }
119300 sqlite3DbFree(db, db->aExtension);
119301 db->aExtension = aHandle;
119302
119303 db->aExtension[db->nExtension++] = handle;
119304 return SQLITE_OK;
119305}
119306SQLITE_API int sqlite3_load_extension(
119307 sqlite3 *db, /* Load the extension into this database connection */
119308 const char *zFile, /* Name of the shared library containing extension */
119309 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
119310 char **pzErrMsg /* Put error message here if not 0 */
119311){
119312 int rc;
119313 sqlite3_mutex_enter(db->mutex);
119314 rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
119315 rc = sqlite3ApiExit(db, rc);
119316 sqlite3_mutex_leave(db->mutex);
119317 return rc;
119318}
119319
119320/*
119321** Call this routine when the database connection is closing in order
119322** to clean up loaded extensions
119323*/
119324SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3 *db){
119325 int i;
119326 assert( sqlite3_mutex_held(db->mutex) );
119327 for(i=0; i<db->nExtension; i++){
119328 sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
119329 }
119330 sqlite3DbFree(db, db->aExtension);
119331}
119332
119333/*
119334** Enable or disable extension loading. Extension loading is disabled by
119335** default so as not to open security holes in older applications.
119336*/
119337SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
119338 sqlite3_mutex_enter(db->mutex);
119339 if( onoff ){
119340 db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc;
119341 }else{
119342 db->flags &= ~(SQLITE_LoadExtension|SQLITE_LoadExtFunc);
119343 }
119344 sqlite3_mutex_leave(db->mutex);
119345 return SQLITE_OK;
119346}
119347
119348#endif /* !defined(SQLITE_OMIT_LOAD_EXTENSION) */
119349
119350/*
119351** The following object holds the list of automatically loaded
119352** extensions.
119353**
119354** This list is shared across threads. The SQLITE_MUTEX_STATIC_MASTER
119355** mutex must be held while accessing this list.
119356*/
119357typedef struct sqlite3AutoExtList sqlite3AutoExtList;
119358static SQLITE_WSD struct sqlite3AutoExtList {
119359 u32 nExt; /* Number of entries in aExt[] */
119360 void (**aExt)(void); /* Pointers to the extension init functions */
119361} sqlite3Autoext = { 0, 0 };
119362
119363/* The "wsdAutoext" macro will resolve to the autoextension
119364** state vector. If writable static data is unsupported on the target,
119365** we have to locate the state vector at run-time. In the more common
119366** case where writable static data is supported, wsdStat can refer directly
119367** to the "sqlite3Autoext" state vector declared above.
119368*/
119369#ifdef SQLITE_OMIT_WSD
119370# define wsdAutoextInit \
119371 sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)
119372# define wsdAutoext x[0]
119373#else
119374# define wsdAutoextInit
119375# define wsdAutoext sqlite3Autoext
119376#endif
119377
119378
119379/*
119380** Register a statically linked extension that is automatically
119381** loaded by every new database connection.
119382*/
119383SQLITE_API int sqlite3_auto_extension(
119384 void (*xInit)(void)
119385){
119386 int rc = SQLITE_OK;
119387#ifndef SQLITE_OMIT_AUTOINIT
119388 rc = sqlite3_initialize();
119389 if( rc ){
119390 return rc;
119391 }else
119392#endif
119393 {
119394 u32 i;
119395#if SQLITE_THREADSAFE
119396 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
119397#endif
119398 wsdAutoextInit;
119399 sqlite3_mutex_enter(mutex);
119400 for(i=0; i<wsdAutoext.nExt; i++){
119401 if( wsdAutoext.aExt[i]==xInit ) break;
119402 }
119403 if( i==wsdAutoext.nExt ){
119404 u64 nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
119405 void (**aNew)(void);
119406 aNew = sqlite3_realloc64(wsdAutoext.aExt, nByte);
119407 if( aNew==0 ){
119408 rc = SQLITE_NOMEM_BKPT;
119409 }else{
119410 wsdAutoext.aExt = aNew;
119411 wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
119412 wsdAutoext.nExt++;
119413 }
119414 }
119415 sqlite3_mutex_leave(mutex);
119416 assert( (rc&0xff)==rc );
119417 return rc;
119418 }
119419}
119420
119421/*
119422** Cancel a prior call to sqlite3_auto_extension. Remove xInit from the
119423** set of routines that is invoked for each new database connection, if it
119424** is currently on the list. If xInit is not on the list, then this
119425** routine is a no-op.
119426**
119427** Return 1 if xInit was found on the list and removed. Return 0 if xInit
119428** was not on the list.
119429*/
119430SQLITE_API int sqlite3_cancel_auto_extension(
119431 void (*xInit)(void)
119432){
119433#if SQLITE_THREADSAFE
119434 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
119435#endif
119436 int i;
119437 int n = 0;
119438 wsdAutoextInit;
119439 sqlite3_mutex_enter(mutex);
119440 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
119441 if( wsdAutoext.aExt[i]==xInit ){
119442 wsdAutoext.nExt--;
119443 wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt];
119444 n++;
119445 break;
119446 }
119447 }
119448 sqlite3_mutex_leave(mutex);
119449 return n;
119450}
119451
119452/*
119453** Reset the automatic extension loading mechanism.
119454*/
119455SQLITE_API void sqlite3_reset_auto_extension(void){
119456#ifndef SQLITE_OMIT_AUTOINIT
119457 if( sqlite3_initialize()==SQLITE_OK )
119458#endif
119459 {
119460#if SQLITE_THREADSAFE
119461 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
119462#endif
119463 wsdAutoextInit;
119464 sqlite3_mutex_enter(mutex);
119465 sqlite3_free(wsdAutoext.aExt);
119466 wsdAutoext.aExt = 0;
119467 wsdAutoext.nExt = 0;
119468 sqlite3_mutex_leave(mutex);
119469 }
119470}
119471
119472/*
119473** Load all automatic extensions.
119474**
119475** If anything goes wrong, set an error in the database connection.
119476*/
119477SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
119478 u32 i;
119479 int go = 1;
119480 int rc;
119481 sqlite3_loadext_entry xInit;
119482
119483 wsdAutoextInit;
119484 if( wsdAutoext.nExt==0 ){
119485 /* Common case: early out without every having to acquire a mutex */
119486 return;
119487 }
119488 for(i=0; go; i++){
119489 char *zErrmsg;
119490#if SQLITE_THREADSAFE
119491 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
119492#endif
119493#ifdef SQLITE_OMIT_LOAD_EXTENSION
119494 const sqlite3_api_routines *pThunk = 0;
119495#else
119496 const sqlite3_api_routines *pThunk = &sqlite3Apis;
119497#endif
119498 sqlite3_mutex_enter(mutex);
119499 if( i>=wsdAutoext.nExt ){
119500 xInit = 0;
119501 go = 0;
119502 }else{
119503 xInit = (sqlite3_loadext_entry)wsdAutoext.aExt[i];
119504 }
119505 sqlite3_mutex_leave(mutex);
119506 zErrmsg = 0;
119507 if( xInit && (rc = xInit(db, &zErrmsg, pThunk))!=0 ){
119508 sqlite3ErrorWithMsg(db, rc,
119509 "automatic extension loading failed: %s", zErrmsg);
119510 go = 0;
119511 }
119512 sqlite3_free(zErrmsg);
119513 }
119514}
119515
119516/************** End of loadext.c *********************************************/
119517/************** Begin file pragma.c ******************************************/
119518/*
119519** 2003 April 6
119520**
119521** The author disclaims copyright to this source code. In place of
119522** a legal notice, here is a blessing:
119523**
119524** May you do good and not evil.
119525** May you find forgiveness for yourself and forgive others.
119526** May you share freely, never taking more than you give.
119527**
119528*************************************************************************
119529** This file contains code used to implement the PRAGMA command.
119530*/
119531/* #include "sqliteInt.h" */
119532
119533#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
119534# if defined(__APPLE__)
119535# define SQLITE_ENABLE_LOCKING_STYLE 1
119536# else
119537# define SQLITE_ENABLE_LOCKING_STYLE 0
119538# endif
119539#endif
119540
119541/***************************************************************************
119542** The "pragma.h" include file is an automatically generated file that
119543** that includes the PragType_XXXX macro definitions and the aPragmaName[]
119544** object. This ensures that the aPragmaName[] table is arranged in
119545** lexicographical order to facility a binary search of the pragma name.
119546** Do not edit pragma.h directly. Edit and rerun the script in at
119547** ../tool/mkpragmatab.tcl. */
119548/************** Include pragma.h in the middle of pragma.c *******************/
119549/************** Begin file pragma.h ******************************************/
119550/* DO NOT EDIT!
119551** This file is automatically generated by the script at
119552** ../tool/mkpragmatab.tcl. To update the set of pragmas, edit
119553** that script and rerun it.
119554*/
119555
119556/* The various pragma types */
119557#define PragTyp_HEADER_VALUE 0
119558#define PragTyp_AUTO_VACUUM 1
119559#define PragTyp_FLAG 2
119560#define PragTyp_BUSY_TIMEOUT 3
119561#define PragTyp_CACHE_SIZE 4
119562#define PragTyp_CACHE_SPILL 5
119563#define PragTyp_CASE_SENSITIVE_LIKE 6
119564#define PragTyp_COLLATION_LIST 7
119565#define PragTyp_COMPILE_OPTIONS 8
119566#define PragTyp_DATA_STORE_DIRECTORY 9
119567#define PragTyp_DATABASE_LIST 10
119568#define PragTyp_DEFAULT_CACHE_SIZE 11
119569#define PragTyp_ENCODING 12
119570#define PragTyp_FOREIGN_KEY_CHECK 13
119571#define PragTyp_FOREIGN_KEY_LIST 14
119572#define PragTyp_FUNCTION_LIST 15
119573#define PragTyp_INCREMENTAL_VACUUM 16
119574#define PragTyp_INDEX_INFO 17
119575#define PragTyp_INDEX_LIST 18
119576#define PragTyp_INTEGRITY_CHECK 19
119577#define PragTyp_JOURNAL_MODE 20
119578#define PragTyp_JOURNAL_SIZE_LIMIT 21
119579#define PragTyp_LOCK_PROXY_FILE 22
119580#define PragTyp_LOCKING_MODE 23
119581#define PragTyp_PAGE_COUNT 24
119582#define PragTyp_MMAP_SIZE 25
119583#define PragTyp_MODULE_LIST 26
119584#define PragTyp_OPTIMIZE 27
119585#define PragTyp_PAGE_SIZE 28
119586#define PragTyp_PRAGMA_LIST 29
119587#define PragTyp_SECURE_DELETE 30
119588#define PragTyp_SHRINK_MEMORY 31
119589#define PragTyp_SOFT_HEAP_LIMIT 32
119590#define PragTyp_SYNCHRONOUS 33
119591#define PragTyp_TABLE_INFO 34
119592#define PragTyp_TEMP_STORE 35
119593#define PragTyp_TEMP_STORE_DIRECTORY 36
119594#define PragTyp_THREADS 37
119595#define PragTyp_WAL_AUTOCHECKPOINT 38
119596#define PragTyp_WAL_CHECKPOINT 39
119597#define PragTyp_ACTIVATE_EXTENSIONS 40
119598#define PragTyp_HEXKEY 41
119599#define PragTyp_KEY 42
119600#define PragTyp_LOCK_STATUS 43
119601#define PragTyp_PARSER_TRACE 44
119602#define PragTyp_STATS 45
119603
119604/* Property flags associated with various pragma. */
119605#define PragFlg_NeedSchema 0x01 /* Force schema load before running */
119606#define PragFlg_NoColumns 0x02 /* OP_ResultRow called with zero columns */
119607#define PragFlg_NoColumns1 0x04 /* zero columns if RHS argument is present */
119608#define PragFlg_ReadOnly 0x08 /* Read-only HEADER_VALUE */
119609#define PragFlg_Result0 0x10 /* Acts as query when no argument */
119610#define PragFlg_Result1 0x20 /* Acts as query when has one argument */
119611#define PragFlg_SchemaOpt 0x40 /* Schema restricts name search if present */
119612#define PragFlg_SchemaReq 0x80 /* Schema required - "main" is default */
119613
119614/* Names of columns for pragmas that return multi-column result
119615** or that return single-column results where the name of the
119616** result column is different from the name of the pragma
119617*/
119618static const char *const pragCName[] = {
119619 /* 0 */ "id", /* Used by: foreign_key_list */
119620 /* 1 */ "seq",
119621 /* 2 */ "table",
119622 /* 3 */ "from",
119623 /* 4 */ "to",
119624 /* 5 */ "on_update",
119625 /* 6 */ "on_delete",
119626 /* 7 */ "match",
119627 /* 8 */ "cid", /* Used by: table_xinfo */
119628 /* 9 */ "name",
119629 /* 10 */ "type",
119630 /* 11 */ "notnull",
119631 /* 12 */ "dflt_value",
119632 /* 13 */ "pk",
119633 /* 14 */ "hidden",
119634 /* table_info reuses 8 */
119635 /* 15 */ "seqno", /* Used by: index_xinfo */
119636 /* 16 */ "cid",
119637 /* 17 */ "name",
119638 /* 18 */ "desc",
119639 /* 19 */ "coll",
119640 /* 20 */ "key",
119641 /* 21 */ "tbl", /* Used by: stats */
119642 /* 22 */ "idx",
119643 /* 23 */ "wdth",
119644 /* 24 */ "hght",
119645 /* 25 */ "flgs",
119646 /* 26 */ "seq", /* Used by: index_list */
119647 /* 27 */ "name",
119648 /* 28 */ "unique",
119649 /* 29 */ "origin",
119650 /* 30 */ "partial",
119651 /* 31 */ "table", /* Used by: foreign_key_check */
119652 /* 32 */ "rowid",
119653 /* 33 */ "parent",
119654 /* 34 */ "fkid",
119655 /* index_info reuses 15 */
119656 /* 35 */ "seq", /* Used by: database_list */
119657 /* 36 */ "name",
119658 /* 37 */ "file",
119659 /* 38 */ "busy", /* Used by: wal_checkpoint */
119660 /* 39 */ "log",
119661 /* 40 */ "checkpointed",
119662 /* 41 */ "name", /* Used by: function_list */
119663 /* 42 */ "builtin",
119664 /* collation_list reuses 26 */
119665 /* 43 */ "database", /* Used by: lock_status */
119666 /* 44 */ "status",
119667 /* 45 */ "cache_size", /* Used by: default_cache_size */
119668 /* module_list pragma_list reuses 9 */
119669 /* 46 */ "timeout", /* Used by: busy_timeout */
119670};
119671
119672/* Definitions of all built-in pragmas */
119673typedef struct PragmaName {
119674 const char *const zName; /* Name of pragma */
119675 u8 ePragTyp; /* PragTyp_XXX value */
119676 u8 mPragFlg; /* Zero or more PragFlg_XXX values */
119677 u8 iPragCName; /* Start of column names in pragCName[] */
119678 u8 nPragCName; /* Num of col names. 0 means use pragma name */
119679 u64 iArg; /* Extra argument */
119680} PragmaName;
119681static const PragmaName aPragmaName[] = {
119682#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
119683 {/* zName: */ "activate_extensions",
119684 /* ePragTyp: */ PragTyp_ACTIVATE_EXTENSIONS,
119685 /* ePragFlg: */ 0,
119686 /* ColNames: */ 0, 0,
119687 /* iArg: */ 0 },
119688#endif
119689#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
119690 {/* zName: */ "application_id",
119691 /* ePragTyp: */ PragTyp_HEADER_VALUE,
119692 /* ePragFlg: */ PragFlg_NoColumns1|PragFlg_Result0,
119693 /* ColNames: */ 0, 0,
119694 /* iArg: */ BTREE_APPLICATION_ID },
119695#endif
119696#if !defined(SQLITE_OMIT_AUTOVACUUM)
119697 {/* zName: */ "auto_vacuum",
119698 /* ePragTyp: */ PragTyp_AUTO_VACUUM,
119699 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
119700 /* ColNames: */ 0, 0,
119701 /* iArg: */ 0 },
119702#endif
119703#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119704#if !defined(SQLITE_OMIT_AUTOMATIC_INDEX)
119705 {/* zName: */ "automatic_index",
119706 /* ePragTyp: */ PragTyp_FLAG,
119707 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119708 /* ColNames: */ 0, 0,
119709 /* iArg: */ SQLITE_AutoIndex },
119710#endif
119711#endif
119712 {/* zName: */ "busy_timeout",
119713 /* ePragTyp: */ PragTyp_BUSY_TIMEOUT,
119714 /* ePragFlg: */ PragFlg_Result0,
119715 /* ColNames: */ 46, 1,
119716 /* iArg: */ 0 },
119717#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
119718 {/* zName: */ "cache_size",
119719 /* ePragTyp: */ PragTyp_CACHE_SIZE,
119720 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
119721 /* ColNames: */ 0, 0,
119722 /* iArg: */ 0 },
119723#endif
119724#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119725 {/* zName: */ "cache_spill",
119726 /* ePragTyp: */ PragTyp_CACHE_SPILL,
119727 /* ePragFlg: */ PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
119728 /* ColNames: */ 0, 0,
119729 /* iArg: */ 0 },
119730#endif
119731 {/* zName: */ "case_sensitive_like",
119732 /* ePragTyp: */ PragTyp_CASE_SENSITIVE_LIKE,
119733 /* ePragFlg: */ PragFlg_NoColumns,
119734 /* ColNames: */ 0, 0,
119735 /* iArg: */ 0 },
119736 {/* zName: */ "cell_size_check",
119737 /* ePragTyp: */ PragTyp_FLAG,
119738 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119739 /* ColNames: */ 0, 0,
119740 /* iArg: */ SQLITE_CellSizeCk },
119741#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119742 {/* zName: */ "checkpoint_fullfsync",
119743 /* ePragTyp: */ PragTyp_FLAG,
119744 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119745 /* ColNames: */ 0, 0,
119746 /* iArg: */ SQLITE_CkptFullFSync },
119747#endif
119748#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119749 {/* zName: */ "collation_list",
119750 /* ePragTyp: */ PragTyp_COLLATION_LIST,
119751 /* ePragFlg: */ PragFlg_Result0,
119752 /* ColNames: */ 26, 2,
119753 /* iArg: */ 0 },
119754#endif
119755#if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
119756 {/* zName: */ "compile_options",
119757 /* ePragTyp: */ PragTyp_COMPILE_OPTIONS,
119758 /* ePragFlg: */ PragFlg_Result0,
119759 /* ColNames: */ 0, 0,
119760 /* iArg: */ 0 },
119761#endif
119762#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119763 {/* zName: */ "count_changes",
119764 /* ePragTyp: */ PragTyp_FLAG,
119765 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119766 /* ColNames: */ 0, 0,
119767 /* iArg: */ SQLITE_CountRows },
119768#endif
119769#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN
119770 {/* zName: */ "data_store_directory",
119771 /* ePragTyp: */ PragTyp_DATA_STORE_DIRECTORY,
119772 /* ePragFlg: */ PragFlg_NoColumns1,
119773 /* ColNames: */ 0, 0,
119774 /* iArg: */ 0 },
119775#endif
119776#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
119777 {/* zName: */ "data_version",
119778 /* ePragTyp: */ PragTyp_HEADER_VALUE,
119779 /* ePragFlg: */ PragFlg_ReadOnly|PragFlg_Result0,
119780 /* ColNames: */ 0, 0,
119781 /* iArg: */ BTREE_DATA_VERSION },
119782#endif
119783#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119784 {/* zName: */ "database_list",
119785 /* ePragTyp: */ PragTyp_DATABASE_LIST,
119786 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0,
119787 /* ColNames: */ 35, 3,
119788 /* iArg: */ 0 },
119789#endif
119790#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
119791 {/* zName: */ "default_cache_size",
119792 /* ePragTyp: */ PragTyp_DEFAULT_CACHE_SIZE,
119793 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
119794 /* ColNames: */ 45, 1,
119795 /* iArg: */ 0 },
119796#endif
119797#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119798#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
119799 {/* zName: */ "defer_foreign_keys",
119800 /* ePragTyp: */ PragTyp_FLAG,
119801 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119802 /* ColNames: */ 0, 0,
119803 /* iArg: */ SQLITE_DeferFKs },
119804#endif
119805#endif
119806#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119807 {/* zName: */ "empty_result_callbacks",
119808 /* ePragTyp: */ PragTyp_FLAG,
119809 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119810 /* ColNames: */ 0, 0,
119811 /* iArg: */ SQLITE_NullCallback },
119812#endif
119813#if !defined(SQLITE_OMIT_UTF16)
119814 {/* zName: */ "encoding",
119815 /* ePragTyp: */ PragTyp_ENCODING,
119816 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119817 /* ColNames: */ 0, 0,
119818 /* iArg: */ 0 },
119819#endif
119820#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
119821 {/* zName: */ "foreign_key_check",
119822 /* ePragTyp: */ PragTyp_FOREIGN_KEY_CHECK,
119823 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0,
119824 /* ColNames: */ 31, 4,
119825 /* iArg: */ 0 },
119826#endif
119827#if !defined(SQLITE_OMIT_FOREIGN_KEY)
119828 {/* zName: */ "foreign_key_list",
119829 /* ePragTyp: */ PragTyp_FOREIGN_KEY_LIST,
119830 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
119831 /* ColNames: */ 0, 8,
119832 /* iArg: */ 0 },
119833#endif
119834#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119835#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
119836 {/* zName: */ "foreign_keys",
119837 /* ePragTyp: */ PragTyp_FLAG,
119838 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119839 /* ColNames: */ 0, 0,
119840 /* iArg: */ SQLITE_ForeignKeys },
119841#endif
119842#endif
119843#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
119844 {/* zName: */ "freelist_count",
119845 /* ePragTyp: */ PragTyp_HEADER_VALUE,
119846 /* ePragFlg: */ PragFlg_ReadOnly|PragFlg_Result0,
119847 /* ColNames: */ 0, 0,
119848 /* iArg: */ BTREE_FREE_PAGE_COUNT },
119849#endif
119850#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119851 {/* zName: */ "full_column_names",
119852 /* ePragTyp: */ PragTyp_FLAG,
119853 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119854 /* ColNames: */ 0, 0,
119855 /* iArg: */ SQLITE_FullColNames },
119856 {/* zName: */ "fullfsync",
119857 /* ePragTyp: */ PragTyp_FLAG,
119858 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119859 /* ColNames: */ 0, 0,
119860 /* iArg: */ SQLITE_FullFSync },
119861#endif
119862#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119863#if defined(SQLITE_INTROSPECTION_PRAGMAS)
119864 {/* zName: */ "function_list",
119865 /* ePragTyp: */ PragTyp_FUNCTION_LIST,
119866 /* ePragFlg: */ PragFlg_Result0,
119867 /* ColNames: */ 41, 2,
119868 /* iArg: */ 0 },
119869#endif
119870#endif
119871#if defined(SQLITE_HAS_CODEC)
119872 {/* zName: */ "hexkey",
119873 /* ePragTyp: */ PragTyp_HEXKEY,
119874 /* ePragFlg: */ 0,
119875 /* ColNames: */ 0, 0,
119876 /* iArg: */ 2 },
119877 {/* zName: */ "hexrekey",
119878 /* ePragTyp: */ PragTyp_HEXKEY,
119879 /* ePragFlg: */ 0,
119880 /* ColNames: */ 0, 0,
119881 /* iArg: */ 3 },
119882#endif
119883#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119884#if !defined(SQLITE_OMIT_CHECK)
119885 {/* zName: */ "ignore_check_constraints",
119886 /* ePragTyp: */ PragTyp_FLAG,
119887 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119888 /* ColNames: */ 0, 0,
119889 /* iArg: */ SQLITE_IgnoreChecks },
119890#endif
119891#endif
119892#if !defined(SQLITE_OMIT_AUTOVACUUM)
119893 {/* zName: */ "incremental_vacuum",
119894 /* ePragTyp: */ PragTyp_INCREMENTAL_VACUUM,
119895 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_NoColumns,
119896 /* ColNames: */ 0, 0,
119897 /* iArg: */ 0 },
119898#endif
119899#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119900 {/* zName: */ "index_info",
119901 /* ePragTyp: */ PragTyp_INDEX_INFO,
119902 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
119903 /* ColNames: */ 15, 3,
119904 /* iArg: */ 0 },
119905 {/* zName: */ "index_list",
119906 /* ePragTyp: */ PragTyp_INDEX_LIST,
119907 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
119908 /* ColNames: */ 26, 5,
119909 /* iArg: */ 0 },
119910 {/* zName: */ "index_xinfo",
119911 /* ePragTyp: */ PragTyp_INDEX_INFO,
119912 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
119913 /* ColNames: */ 15, 6,
119914 /* iArg: */ 1 },
119915#endif
119916#if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
119917 {/* zName: */ "integrity_check",
119918 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK,
119919 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_Result1,
119920 /* ColNames: */ 0, 0,
119921 /* iArg: */ 0 },
119922#endif
119923#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
119924 {/* zName: */ "journal_mode",
119925 /* ePragTyp: */ PragTyp_JOURNAL_MODE,
119926 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
119927 /* ColNames: */ 0, 0,
119928 /* iArg: */ 0 },
119929 {/* zName: */ "journal_size_limit",
119930 /* ePragTyp: */ PragTyp_JOURNAL_SIZE_LIMIT,
119931 /* ePragFlg: */ PragFlg_Result0|PragFlg_SchemaReq,
119932 /* ColNames: */ 0, 0,
119933 /* iArg: */ 0 },
119934#endif
119935#if defined(SQLITE_HAS_CODEC)
119936 {/* zName: */ "key",
119937 /* ePragTyp: */ PragTyp_KEY,
119938 /* ePragFlg: */ 0,
119939 /* ColNames: */ 0, 0,
119940 /* iArg: */ 0 },
119941#endif
119942#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119943 {/* zName: */ "legacy_alter_table",
119944 /* ePragTyp: */ PragTyp_FLAG,
119945 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119946 /* ColNames: */ 0, 0,
119947 /* iArg: */ SQLITE_LegacyAlter },
119948 {/* zName: */ "legacy_file_format",
119949 /* ePragTyp: */ PragTyp_FLAG,
119950 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119951 /* ColNames: */ 0, 0,
119952 /* iArg: */ SQLITE_LegacyFileFmt },
119953#endif
119954#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE
119955 {/* zName: */ "lock_proxy_file",
119956 /* ePragTyp: */ PragTyp_LOCK_PROXY_FILE,
119957 /* ePragFlg: */ PragFlg_NoColumns1,
119958 /* ColNames: */ 0, 0,
119959 /* iArg: */ 0 },
119960#endif
119961#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
119962 {/* zName: */ "lock_status",
119963 /* ePragTyp: */ PragTyp_LOCK_STATUS,
119964 /* ePragFlg: */ PragFlg_Result0,
119965 /* ColNames: */ 43, 2,
119966 /* iArg: */ 0 },
119967#endif
119968#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
119969 {/* zName: */ "locking_mode",
119970 /* ePragTyp: */ PragTyp_LOCKING_MODE,
119971 /* ePragFlg: */ PragFlg_Result0|PragFlg_SchemaReq,
119972 /* ColNames: */ 0, 0,
119973 /* iArg: */ 0 },
119974 {/* zName: */ "max_page_count",
119975 /* ePragTyp: */ PragTyp_PAGE_COUNT,
119976 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
119977 /* ColNames: */ 0, 0,
119978 /* iArg: */ 0 },
119979 {/* zName: */ "mmap_size",
119980 /* ePragTyp: */ PragTyp_MMAP_SIZE,
119981 /* ePragFlg: */ 0,
119982 /* ColNames: */ 0, 0,
119983 /* iArg: */ 0 },
119984#endif
119985#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119986#if !defined(SQLITE_OMIT_VIRTUALTABLE)
119987#if defined(SQLITE_INTROSPECTION_PRAGMAS)
119988 {/* zName: */ "module_list",
119989 /* ePragTyp: */ PragTyp_MODULE_LIST,
119990 /* ePragFlg: */ PragFlg_Result0,
119991 /* ColNames: */ 9, 1,
119992 /* iArg: */ 0 },
119993#endif
119994#endif
119995#endif
119996 {/* zName: */ "optimize",
119997 /* ePragTyp: */ PragTyp_OPTIMIZE,
119998 /* ePragFlg: */ PragFlg_Result1|PragFlg_NeedSchema,
119999 /* ColNames: */ 0, 0,
120000 /* iArg: */ 0 },
120001#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
120002 {/* zName: */ "page_count",
120003 /* ePragTyp: */ PragTyp_PAGE_COUNT,
120004 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
120005 /* ColNames: */ 0, 0,
120006 /* iArg: */ 0 },
120007 {/* zName: */ "page_size",
120008 /* ePragTyp: */ PragTyp_PAGE_SIZE,
120009 /* ePragFlg: */ PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
120010 /* ColNames: */ 0, 0,
120011 /* iArg: */ 0 },
120012#endif
120013#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_PARSER_TRACE)
120014 {/* zName: */ "parser_trace",
120015 /* ePragTyp: */ PragTyp_PARSER_TRACE,
120016 /* ePragFlg: */ 0,
120017 /* ColNames: */ 0, 0,
120018 /* iArg: */ 0 },
120019#endif
120020#if defined(SQLITE_INTROSPECTION_PRAGMAS)
120021 {/* zName: */ "pragma_list",
120022 /* ePragTyp: */ PragTyp_PRAGMA_LIST,
120023 /* ePragFlg: */ PragFlg_Result0,
120024 /* ColNames: */ 9, 1,
120025 /* iArg: */ 0 },
120026#endif
120027#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
120028 {/* zName: */ "query_only",
120029 /* ePragTyp: */ PragTyp_FLAG,
120030 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
120031 /* ColNames: */ 0, 0,
120032 /* iArg: */ SQLITE_QueryOnly },
120033#endif
120034#if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
120035 {/* zName: */ "quick_check",
120036 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK,
120037 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_Result1,
120038 /* ColNames: */ 0, 0,
120039 /* iArg: */ 0 },
120040#endif
120041#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
120042 {/* zName: */ "read_uncommitted",
120043 /* ePragTyp: */ PragTyp_FLAG,
120044 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
120045 /* ColNames: */ 0, 0,
120046 /* iArg: */ SQLITE_ReadUncommit },
120047 {/* zName: */ "recursive_triggers",
120048 /* ePragTyp: */ PragTyp_FLAG,
120049 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
120050 /* ColNames: */ 0, 0,
120051 /* iArg: */ SQLITE_RecTriggers },
120052#endif
120053#if defined(SQLITE_HAS_CODEC)
120054 {/* zName: */ "rekey",
120055 /* ePragTyp: */ PragTyp_KEY,
120056 /* ePragFlg: */ 0,
120057 /* ColNames: */ 0, 0,
120058 /* iArg: */ 1 },
120059#endif
120060#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
120061 {/* zName: */ "reverse_unordered_selects",
120062 /* ePragTyp: */ PragTyp_FLAG,
120063 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
120064 /* ColNames: */ 0, 0,
120065 /* iArg: */ SQLITE_ReverseOrder },
120066#endif
120067#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
120068 {/* zName: */ "schema_version",
120069 /* ePragTyp: */ PragTyp_HEADER_VALUE,
120070 /* ePragFlg: */ PragFlg_NoColumns1|PragFlg_Result0,
120071 /* ColNames: */ 0, 0,
120072 /* iArg: */ BTREE_SCHEMA_VERSION },
120073#endif
120074#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
120075 {/* zName: */ "secure_delete",
120076 /* ePragTyp: */ PragTyp_SECURE_DELETE,
120077 /* ePragFlg: */ PragFlg_Result0,
120078 /* ColNames: */ 0, 0,
120079 /* iArg: */ 0 },
120080#endif
120081#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
120082 {/* zName: */ "short_column_names",
120083 /* ePragTyp: */ PragTyp_FLAG,
120084 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
120085 /* ColNames: */ 0, 0,
120086 /* iArg: */ SQLITE_ShortColNames },
120087#endif
120088 {/* zName: */ "shrink_memory",
120089 /* ePragTyp: */ PragTyp_SHRINK_MEMORY,
120090 /* ePragFlg: */ PragFlg_NoColumns,
120091 /* ColNames: */ 0, 0,
120092 /* iArg: */ 0 },
120093 {/* zName: */ "soft_heap_limit",
120094 /* ePragTyp: */ PragTyp_SOFT_HEAP_LIMIT,
120095 /* ePragFlg: */ PragFlg_Result0,
120096 /* ColNames: */ 0, 0,
120097 /* iArg: */ 0 },
120098#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
120099#if defined(SQLITE_DEBUG)
120100 {/* zName: */ "sql_trace",
120101 /* ePragTyp: */ PragTyp_FLAG,
120102 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
120103 /* ColNames: */ 0, 0,
120104 /* iArg: */ SQLITE_SqlTrace },
120105#endif
120106#endif
120107#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && defined(SQLITE_DEBUG)
120108 {/* zName: */ "stats",
120109 /* ePragTyp: */ PragTyp_STATS,
120110 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
120111 /* ColNames: */ 21, 5,
120112 /* iArg: */ 0 },
120113#endif
120114#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
120115 {/* zName: */ "synchronous",
120116 /* ePragTyp: */ PragTyp_SYNCHRONOUS,
120117 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
120118 /* ColNames: */ 0, 0,
120119 /* iArg: */ 0 },
120120#endif
120121#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
120122 {/* zName: */ "table_info",
120123 /* ePragTyp: */ PragTyp_TABLE_INFO,
120124 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
120125 /* ColNames: */ 8, 6,
120126 /* iArg: */ 0 },
120127 {/* zName: */ "table_xinfo",
120128 /* ePragTyp: */ PragTyp_TABLE_INFO,
120129 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
120130 /* ColNames: */ 8, 7,
120131 /* iArg: */ 1 },
120132#endif
120133#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
120134 {/* zName: */ "temp_store",
120135 /* ePragTyp: */ PragTyp_TEMP_STORE,
120136 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
120137 /* ColNames: */ 0, 0,
120138 /* iArg: */ 0 },
120139 {/* zName: */ "temp_store_directory",
120140 /* ePragTyp: */ PragTyp_TEMP_STORE_DIRECTORY,
120141 /* ePragFlg: */ PragFlg_NoColumns1,
120142 /* ColNames: */ 0, 0,
120143 /* iArg: */ 0 },
120144#endif
120145#if defined(SQLITE_HAS_CODEC)
120146 {/* zName: */ "textkey",
120147 /* ePragTyp: */ PragTyp_KEY,
120148 /* ePragFlg: */ 0,
120149 /* ColNames: */ 0, 0,
120150 /* iArg: */ 4 },
120151 {/* zName: */ "textrekey",
120152 /* ePragTyp: */ PragTyp_KEY,
120153 /* ePragFlg: */ 0,
120154 /* ColNames: */ 0, 0,
120155 /* iArg: */ 5 },
120156#endif
120157 {/* zName: */ "threads",
120158 /* ePragTyp: */ PragTyp_THREADS,
120159 /* ePragFlg: */ PragFlg_Result0,
120160 /* ColNames: */ 0, 0,
120161 /* iArg: */ 0 },
120162#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
120163 {/* zName: */ "user_version",
120164 /* ePragTyp: */ PragTyp_HEADER_VALUE,
120165 /* ePragFlg: */ PragFlg_NoColumns1|PragFlg_Result0,
120166 /* ColNames: */ 0, 0,
120167 /* iArg: */ BTREE_USER_VERSION },
120168#endif
120169#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
120170#if defined(SQLITE_DEBUG)
120171 {/* zName: */ "vdbe_addoptrace",
120172 /* ePragTyp: */ PragTyp_FLAG,
120173 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
120174 /* ColNames: */ 0, 0,
120175 /* iArg: */ SQLITE_VdbeAddopTrace },
120176 {/* zName: */ "vdbe_debug",
120177 /* ePragTyp: */ PragTyp_FLAG,
120178 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
120179 /* ColNames: */ 0, 0,
120180 /* iArg: */ SQLITE_SqlTrace|SQLITE_VdbeListing|SQLITE_VdbeTrace },
120181 {/* zName: */ "vdbe_eqp",
120182 /* ePragTyp: */ PragTyp_FLAG,
120183 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
120184 /* ColNames: */ 0, 0,
120185 /* iArg: */ SQLITE_VdbeEQP },
120186 {/* zName: */ "vdbe_listing",
120187 /* ePragTyp: */ PragTyp_FLAG,
120188 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
120189 /* ColNames: */ 0, 0,
120190 /* iArg: */ SQLITE_VdbeListing },
120191 {/* zName: */ "vdbe_trace",
120192 /* ePragTyp: */ PragTyp_FLAG,
120193 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
120194 /* ColNames: */ 0, 0,
120195 /* iArg: */ SQLITE_VdbeTrace },
120196#endif
120197#endif
120198#if !defined(SQLITE_OMIT_WAL)
120199 {/* zName: */ "wal_autocheckpoint",
120200 /* ePragTyp: */ PragTyp_WAL_AUTOCHECKPOINT,
120201 /* ePragFlg: */ 0,
120202 /* ColNames: */ 0, 0,
120203 /* iArg: */ 0 },
120204 {/* zName: */ "wal_checkpoint",
120205 /* ePragTyp: */ PragTyp_WAL_CHECKPOINT,
120206 /* ePragFlg: */ PragFlg_NeedSchema,
120207 /* ColNames: */ 38, 3,
120208 /* iArg: */ 0 },
120209#endif
120210#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
120211 {/* zName: */ "writable_schema",
120212 /* ePragTyp: */ PragTyp_FLAG,
120213 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
120214 /* ColNames: */ 0, 0,
120215 /* iArg: */ SQLITE_WriteSchema|SQLITE_NoSchemaError },
120216#endif
120217};
120218/* Number of pragmas: 62 on by default, 81 total. */
120219
120220/************** End of pragma.h **********************************************/
120221/************** Continuing where we left off in pragma.c *********************/
120222
120223/*
120224** Interpret the given string as a safety level. Return 0 for OFF,
120225** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA. Return 1 for an empty or
120226** unrecognized string argument. The FULL and EXTRA option is disallowed
120227** if the omitFull parameter it 1.
120228**
120229** Note that the values returned are one less that the values that
120230** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
120231** to support legacy SQL code. The safety level used to be boolean
120232** and older scripts may have used numbers 0 for OFF and 1 for ON.
120233*/
120234static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){
120235 /* 123456789 123456789 123 */
120236 static const char zText[] = "onoffalseyestruextrafull";
120237 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 15, 20};
120238 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 5, 4};
120239 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 3, 2};
120240 /* on no off false yes true extra full */
120241 int i, n;
120242 if( sqlite3Isdigit(*z) ){
120243 return (u8)sqlite3Atoi(z);
120244 }
120245 n = sqlite3Strlen30(z);
120246 for(i=0; i<ArraySize(iLength); i++){
120247 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0
120248 && (!omitFull || iValue[i]<=1)
120249 ){
120250 return iValue[i];
120251 }
120252 }
120253 return dflt;
120254}
120255
120256/*
120257** Interpret the given string as a boolean value.
120258*/
120259SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z, u8 dflt){
120260 return getSafetyLevel(z,1,dflt)!=0;
120261}
120262
120263/* The sqlite3GetBoolean() function is used by other modules but the
120264** remainder of this file is specific to PRAGMA processing. So omit
120265** the rest of the file if PRAGMAs are omitted from the build.
120266*/
120267#if !defined(SQLITE_OMIT_PRAGMA)
120268
120269/*
120270** Interpret the given string as a locking mode value.
120271*/
120272static int getLockingMode(const char *z){
120273 if( z ){
120274 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
120275 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
120276 }
120277 return PAGER_LOCKINGMODE_QUERY;
120278}
120279
120280#ifndef SQLITE_OMIT_AUTOVACUUM
120281/*
120282** Interpret the given string as an auto-vacuum mode value.
120283**
120284** The following strings, "none", "full" and "incremental" are
120285** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
120286*/
120287static int getAutoVacuum(const char *z){
120288 int i;
120289 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
120290 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
120291 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
120292 i = sqlite3Atoi(z);
120293 return (u8)((i>=0&&i<=2)?i:0);
120294}
120295#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
120296
120297#ifndef SQLITE_OMIT_PAGER_PRAGMAS
120298/*
120299** Interpret the given string as a temp db location. Return 1 for file
120300** backed temporary databases, 2 for the Red-Black tree in memory database
120301** and 0 to use the compile-time default.
120302*/
120303static int getTempStore(const char *z){
120304 if( z[0]>='0' && z[0]<='2' ){
120305 return z[0] - '0';
120306 }else if( sqlite3StrICmp(z, "file")==0 ){
120307 return 1;
120308 }else if( sqlite3StrICmp(z, "memory")==0 ){
120309 return 2;
120310 }else{
120311 return 0;
120312 }
120313}
120314#endif /* SQLITE_PAGER_PRAGMAS */
120315
120316#ifndef SQLITE_OMIT_PAGER_PRAGMAS
120317/*
120318** Invalidate temp storage, either when the temp storage is changed
120319** from default, or when 'file' and the temp_store_directory has changed
120320*/
120321static int invalidateTempStorage(Parse *pParse){
120322 sqlite3 *db = pParse->db;
120323 if( db->aDb[1].pBt!=0 ){
120324 if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
120325 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
120326 "from within a transaction");
120327 return SQLITE_ERROR;
120328 }
120329 sqlite3BtreeClose(db->aDb[1].pBt);
120330 db->aDb[1].pBt = 0;
120331 sqlite3ResetAllSchemasOfConnection(db);
120332 }
120333 return SQLITE_OK;
120334}
120335#endif /* SQLITE_PAGER_PRAGMAS */
120336
120337#ifndef SQLITE_OMIT_PAGER_PRAGMAS
120338/*
120339** If the TEMP database is open, close it and mark the database schema
120340** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
120341** or DEFAULT_TEMP_STORE pragmas.
120342*/
120343static int changeTempStorage(Parse *pParse, const char *zStorageType){
120344 int ts = getTempStore(zStorageType);
120345 sqlite3 *db = pParse->db;
120346 if( db->temp_store==ts ) return SQLITE_OK;
120347 if( invalidateTempStorage( pParse ) != SQLITE_OK ){
120348 return SQLITE_ERROR;
120349 }
120350 db->temp_store = (u8)ts;
120351 return SQLITE_OK;
120352}
120353#endif /* SQLITE_PAGER_PRAGMAS */
120354
120355/*
120356** Set result column names for a pragma.
120357*/
120358static void setPragmaResultColumnNames(
120359 Vdbe *v, /* The query under construction */
120360 const PragmaName *pPragma /* The pragma */
120361){
120362 u8 n = pPragma->nPragCName;
120363 sqlite3VdbeSetNumCols(v, n==0 ? 1 : n);
120364 if( n==0 ){
120365 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, pPragma->zName, SQLITE_STATIC);
120366 }else{
120367 int i, j;
120368 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
120369 sqlite3VdbeSetColName(v, i, COLNAME_NAME, pragCName[j], SQLITE_STATIC);
120370 }
120371 }
120372}
120373
120374/*
120375** Generate code to return a single integer value.
120376*/
120377static void returnSingleInt(Vdbe *v, i64 value){
120378 sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, 1, 0, (const u8*)&value, P4_INT64);
120379 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
120380}
120381
120382/*
120383** Generate code to return a single text value.
120384*/
120385static void returnSingleText(
120386 Vdbe *v, /* Prepared statement under construction */
120387 const char *zValue /* Value to be returned */
120388){
120389 if( zValue ){
120390 sqlite3VdbeLoadString(v, 1, (const char*)zValue);
120391 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
120392 }
120393}
120394
120395
120396/*
120397** Set the safety_level and pager flags for pager iDb. Or if iDb<0
120398** set these values for all pagers.
120399*/
120400#ifndef SQLITE_OMIT_PAGER_PRAGMAS
120401static void setAllPagerFlags(sqlite3 *db){
120402 if( db->autoCommit ){
120403 Db *pDb = db->aDb;
120404 int n = db->nDb;
120405 assert( SQLITE_FullFSync==PAGER_FULLFSYNC );
120406 assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC );
120407 assert( SQLITE_CacheSpill==PAGER_CACHESPILL );
120408 assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)
120409 == PAGER_FLAGS_MASK );
120410 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
120411 while( (n--) > 0 ){
120412 if( pDb->pBt ){
120413 sqlite3BtreeSetPagerFlags(pDb->pBt,
120414 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
120415 }
120416 pDb++;
120417 }
120418 }
120419}
120420#else
120421# define setAllPagerFlags(X) /* no-op */
120422#endif
120423
120424
120425/*
120426** Return a human-readable name for a constraint resolution action.
120427*/
120428#ifndef SQLITE_OMIT_FOREIGN_KEY
120429static const char *actionName(u8 action){
120430 const char *zName;
120431 switch( action ){
120432 case OE_SetNull: zName = "SET NULL"; break;
120433 case OE_SetDflt: zName = "SET DEFAULT"; break;
120434 case OE_Cascade: zName = "CASCADE"; break;
120435 case OE_Restrict: zName = "RESTRICT"; break;
120436 default: zName = "NO ACTION";
120437 assert( action==OE_None ); break;
120438 }
120439 return zName;
120440}
120441#endif
120442
120443
120444/*
120445** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
120446** defined in pager.h. This function returns the associated lowercase
120447** journal-mode name.
120448*/
120449SQLITE_PRIVATE const char *sqlite3JournalModename(int eMode){
120450 static char * const azModeName[] = {
120451 "delete", "persist", "off", "truncate", "memory"
120452#ifndef SQLITE_OMIT_WAL
120453 , "wal"
120454#endif
120455 };
120456 assert( PAGER_JOURNALMODE_DELETE==0 );
120457 assert( PAGER_JOURNALMODE_PERSIST==1 );
120458 assert( PAGER_JOURNALMODE_OFF==2 );
120459 assert( PAGER_JOURNALMODE_TRUNCATE==3 );
120460 assert( PAGER_JOURNALMODE_MEMORY==4 );
120461 assert( PAGER_JOURNALMODE_WAL==5 );
120462 assert( eMode>=0 && eMode<=ArraySize(azModeName) );
120463
120464 if( eMode==ArraySize(azModeName) ) return 0;
120465 return azModeName[eMode];
120466}
120467
120468/*
120469** Locate a pragma in the aPragmaName[] array.
120470*/
120471static const PragmaName *pragmaLocate(const char *zName){
120472 int upr, lwr, mid = 0, rc;
120473 lwr = 0;
120474 upr = ArraySize(aPragmaName)-1;
120475 while( lwr<=upr ){
120476 mid = (lwr+upr)/2;
120477 rc = sqlite3_stricmp(zName, aPragmaName[mid].zName);
120478 if( rc==0 ) break;
120479 if( rc<0 ){
120480 upr = mid - 1;
120481 }else{
120482 lwr = mid + 1;
120483 }
120484 }
120485 return lwr>upr ? 0 : &aPragmaName[mid];
120486}
120487
120488/*
120489** Helper subroutine for PRAGMA integrity_check:
120490**
120491** Generate code to output a single-column result row with a value of the
120492** string held in register 3. Decrement the result count in register 1
120493** and halt if the maximum number of result rows have been issued.
120494*/
120495static int integrityCheckResultRow(Vdbe *v){
120496 int addr;
120497 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
120498 addr = sqlite3VdbeAddOp3(v, OP_IfPos, 1, sqlite3VdbeCurrentAddr(v)+2, 1);
120499 VdbeCoverage(v);
120500 sqlite3VdbeAddOp0(v, OP_Halt);
120501 return addr;
120502}
120503
120504/*
120505** Process a pragma statement.
120506**
120507** Pragmas are of this form:
120508**
120509** PRAGMA [schema.]id [= value]
120510**
120511** The identifier might also be a string. The value is a string, and
120512** identifier, or a number. If minusFlag is true, then the value is
120513** a number that was preceded by a minus sign.
120514**
120515** If the left side is "database.id" then pId1 is the database name
120516** and pId2 is the id. If the left side is just "id" then pId1 is the
120517** id and pId2 is any empty string.
120518*/
120519SQLITE_PRIVATE void sqlite3Pragma(
120520 Parse *pParse,
120521 Token *pId1, /* First part of [schema.]id field */
120522 Token *pId2, /* Second part of [schema.]id field, or NULL */
120523 Token *pValue, /* Token for <value>, or NULL */
120524 int minusFlag /* True if a '-' sign preceded <value> */
120525){
120526 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
120527 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
120528 const char *zDb = 0; /* The database name */
120529 Token *pId; /* Pointer to <id> token */
120530 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
120531 int iDb; /* Database index for <database> */
120532 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
120533 sqlite3 *db = pParse->db; /* The database connection */
120534 Db *pDb; /* The specific database being pragmaed */
120535 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
120536 const PragmaName *pPragma; /* The pragma */
120537
120538 if( v==0 ) return;
120539 sqlite3VdbeRunOnlyOnce(v);
120540 pParse->nMem = 2;
120541
120542 /* Interpret the [schema.] part of the pragma statement. iDb is the
120543 ** index of the database this pragma is being applied to in db.aDb[]. */
120544 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
120545 if( iDb<0 ) return;
120546 pDb = &db->aDb[iDb];
120547
120548 /* If the temp database has been explicitly named as part of the
120549 ** pragma, make sure it is open.
120550 */
120551 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
120552 return;
120553 }
120554
120555 zLeft = sqlite3NameFromToken(db, pId);
120556 if( !zLeft ) return;
120557 if( minusFlag ){
120558 zRight = sqlite3MPrintf(db, "-%T", pValue);
120559 }else{
120560 zRight = sqlite3NameFromToken(db, pValue);
120561 }
120562
120563 assert( pId2 );
120564 zDb = pId2->n>0 ? pDb->zDbSName : 0;
120565 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
120566 goto pragma_out;
120567 }
120568
120569 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
120570 ** connection. If it returns SQLITE_OK, then assume that the VFS
120571 ** handled the pragma and generate a no-op prepared statement.
120572 **
120573 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
120574 ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file
120575 ** object corresponding to the database file to which the pragma
120576 ** statement refers.
120577 **
120578 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
120579 ** file control is an array of pointers to strings (char**) in which the
120580 ** second element of the array is the name of the pragma and the third
120581 ** element is the argument to the pragma or NULL if the pragma has no
120582 ** argument.
120583 */
120584 aFcntl[0] = 0;
120585 aFcntl[1] = zLeft;
120586 aFcntl[2] = zRight;
120587 aFcntl[3] = 0;
120588 db->busyHandler.nBusy = 0;
120589 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
120590 if( rc==SQLITE_OK ){
120591 sqlite3VdbeSetNumCols(v, 1);
120592 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, aFcntl[0], SQLITE_TRANSIENT);
120593 returnSingleText(v, aFcntl[0]);
120594 sqlite3_free(aFcntl[0]);
120595 goto pragma_out;
120596 }
120597 if( rc!=SQLITE_NOTFOUND ){
120598 if( aFcntl[0] ){
120599 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
120600 sqlite3_free(aFcntl[0]);
120601 }
120602 pParse->nErr++;
120603 pParse->rc = rc;
120604 goto pragma_out;
120605 }
120606
120607 /* Locate the pragma in the lookup table */
120608 pPragma = pragmaLocate(zLeft);
120609 if( pPragma==0 ) goto pragma_out;
120610
120611 /* Make sure the database schema is loaded if the pragma requires that */
120612 if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
120613 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
120614 }
120615
120616 /* Register the result column names for pragmas that return results */
120617 if( (pPragma->mPragFlg & PragFlg_NoColumns)==0
120618 && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
120619 ){
120620 setPragmaResultColumnNames(v, pPragma);
120621 }
120622
120623 /* Jump to the appropriate pragma handler */
120624 switch( pPragma->ePragTyp ){
120625
120626#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
120627 /*
120628 ** PRAGMA [schema.]default_cache_size
120629 ** PRAGMA [schema.]default_cache_size=N
120630 **
120631 ** The first form reports the current persistent setting for the
120632 ** page cache size. The value returned is the maximum number of
120633 ** pages in the page cache. The second form sets both the current
120634 ** page cache size value and the persistent page cache size value
120635 ** stored in the database file.
120636 **
120637 ** Older versions of SQLite would set the default cache size to a
120638 ** negative number to indicate synchronous=OFF. These days, synchronous
120639 ** is always on by default regardless of the sign of the default cache
120640 ** size. But continue to take the absolute value of the default cache
120641 ** size of historical compatibility.
120642 */
120643 case PragTyp_DEFAULT_CACHE_SIZE: {
120644 static const int iLn = VDBE_OFFSET_LINENO(2);
120645 static const VdbeOpList getCacheSize[] = {
120646 { OP_Transaction, 0, 0, 0}, /* 0 */
120647 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
120648 { OP_IfPos, 1, 8, 0},
120649 { OP_Integer, 0, 2, 0},
120650 { OP_Subtract, 1, 2, 1},
120651 { OP_IfPos, 1, 8, 0},
120652 { OP_Integer, 0, 1, 0}, /* 6 */
120653 { OP_Noop, 0, 0, 0},
120654 { OP_ResultRow, 1, 1, 0},
120655 };
120656 VdbeOp *aOp;
120657 sqlite3VdbeUsesBtree(v, iDb);
120658 if( !zRight ){
120659 pParse->nMem += 2;
120660 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize));
120661 aOp = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize, iLn);
120662 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
120663 aOp[0].p1 = iDb;
120664 aOp[1].p1 = iDb;
120665 aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE;
120666 }else{
120667 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
120668 sqlite3BeginWriteOperation(pParse, 0, iDb);
120669 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, size);
120670 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
120671 pDb->pSchema->cache_size = size;
120672 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
120673 }
120674 break;
120675 }
120676#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
120677
120678#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
120679 /*
120680 ** PRAGMA [schema.]page_size
120681 ** PRAGMA [schema.]page_size=N
120682 **
120683 ** The first form reports the current setting for the
120684 ** database page size in bytes. The second form sets the
120685 ** database page size value. The value can only be set if
120686 ** the database has not yet been created.
120687 */
120688 case PragTyp_PAGE_SIZE: {
120689 Btree *pBt = pDb->pBt;
120690 assert( pBt!=0 );
120691 if( !zRight ){
120692 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
120693 returnSingleInt(v, size);
120694 }else{
120695 /* Malloc may fail when setting the page-size, as there is an internal
120696 ** buffer that the pager module resizes using sqlite3_realloc().
120697 */
120698 db->nextPagesize = sqlite3Atoi(zRight);
120699 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
120700 sqlite3OomFault(db);
120701 }
120702 }
120703 break;
120704 }
120705
120706 /*
120707 ** PRAGMA [schema.]secure_delete
120708 ** PRAGMA [schema.]secure_delete=ON/OFF/FAST
120709 **
120710 ** The first form reports the current setting for the
120711 ** secure_delete flag. The second form changes the secure_delete
120712 ** flag setting and reports the new value.
120713 */
120714 case PragTyp_SECURE_DELETE: {
120715 Btree *pBt = pDb->pBt;
120716 int b = -1;
120717 assert( pBt!=0 );
120718 if( zRight ){
120719 if( sqlite3_stricmp(zRight, "fast")==0 ){
120720 b = 2;
120721 }else{
120722 b = sqlite3GetBoolean(zRight, 0);
120723 }
120724 }
120725 if( pId2->n==0 && b>=0 ){
120726 int ii;
120727 for(ii=0; ii<db->nDb; ii++){
120728 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
120729 }
120730 }
120731 b = sqlite3BtreeSecureDelete(pBt, b);
120732 returnSingleInt(v, b);
120733 break;
120734 }
120735
120736 /*
120737 ** PRAGMA [schema.]max_page_count
120738 ** PRAGMA [schema.]max_page_count=N
120739 **
120740 ** The first form reports the current setting for the
120741 ** maximum number of pages in the database file. The
120742 ** second form attempts to change this setting. Both
120743 ** forms return the current setting.
120744 **
120745 ** The absolute value of N is used. This is undocumented and might
120746 ** change. The only purpose is to provide an easy way to test
120747 ** the sqlite3AbsInt32() function.
120748 **
120749 ** PRAGMA [schema.]page_count
120750 **
120751 ** Return the number of pages in the specified database.
120752 */
120753 case PragTyp_PAGE_COUNT: {
120754 int iReg;
120755 sqlite3CodeVerifySchema(pParse, iDb);
120756 iReg = ++pParse->nMem;
120757 if( sqlite3Tolower(zLeft[0])=='p' ){
120758 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
120759 }else{
120760 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
120761 sqlite3AbsInt32(sqlite3Atoi(zRight)));
120762 }
120763 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
120764 break;
120765 }
120766
120767 /*
120768 ** PRAGMA [schema.]locking_mode
120769 ** PRAGMA [schema.]locking_mode = (normal|exclusive)
120770 */
120771 case PragTyp_LOCKING_MODE: {
120772 const char *zRet = "normal";
120773 int eMode = getLockingMode(zRight);
120774
120775 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
120776 /* Simple "PRAGMA locking_mode;" statement. This is a query for
120777 ** the current default locking mode (which may be different to
120778 ** the locking-mode of the main database).
120779 */
120780 eMode = db->dfltLockMode;
120781 }else{
120782 Pager *pPager;
120783 if( pId2->n==0 ){
120784 /* This indicates that no database name was specified as part
120785 ** of the PRAGMA command. In this case the locking-mode must be
120786 ** set on all attached databases, as well as the main db file.
120787 **
120788 ** Also, the sqlite3.dfltLockMode variable is set so that
120789 ** any subsequently attached databases also use the specified
120790 ** locking mode.
120791 */
120792 int ii;
120793 assert(pDb==&db->aDb[0]);
120794 for(ii=2; ii<db->nDb; ii++){
120795 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
120796 sqlite3PagerLockingMode(pPager, eMode);
120797 }
120798 db->dfltLockMode = (u8)eMode;
120799 }
120800 pPager = sqlite3BtreePager(pDb->pBt);
120801 eMode = sqlite3PagerLockingMode(pPager, eMode);
120802 }
120803
120804 assert( eMode==PAGER_LOCKINGMODE_NORMAL
120805 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
120806 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
120807 zRet = "exclusive";
120808 }
120809 returnSingleText(v, zRet);
120810 break;
120811 }
120812
120813 /*
120814 ** PRAGMA [schema.]journal_mode
120815 ** PRAGMA [schema.]journal_mode =
120816 ** (delete|persist|off|truncate|memory|wal|off)
120817 */
120818 case PragTyp_JOURNAL_MODE: {
120819 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
120820 int ii; /* Loop counter */
120821
120822 if( zRight==0 ){
120823 /* If there is no "=MODE" part of the pragma, do a query for the
120824 ** current mode */
120825 eMode = PAGER_JOURNALMODE_QUERY;
120826 }else{
120827 const char *zMode;
120828 int n = sqlite3Strlen30(zRight);
120829 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
120830 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
120831 }
120832 if( !zMode ){
120833 /* If the "=MODE" part does not match any known journal mode,
120834 ** then do a query */
120835 eMode = PAGER_JOURNALMODE_QUERY;
120836 }
120837 }
120838 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
120839 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
120840 iDb = 0;
120841 pId2->n = 1;
120842 }
120843 for(ii=db->nDb-1; ii>=0; ii--){
120844 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
120845 sqlite3VdbeUsesBtree(v, ii);
120846 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
120847 }
120848 }
120849 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
120850 break;
120851 }
120852
120853 /*
120854 ** PRAGMA [schema.]journal_size_limit
120855 ** PRAGMA [schema.]journal_size_limit=N
120856 **
120857 ** Get or set the size limit on rollback journal files.
120858 */
120859 case PragTyp_JOURNAL_SIZE_LIMIT: {
120860 Pager *pPager = sqlite3BtreePager(pDb->pBt);
120861 i64 iLimit = -2;
120862 if( zRight ){
120863 sqlite3DecOrHexToI64(zRight, &iLimit);
120864 if( iLimit<-1 ) iLimit = -1;
120865 }
120866 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
120867 returnSingleInt(v, iLimit);
120868 break;
120869 }
120870
120871#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
120872
120873 /*
120874 ** PRAGMA [schema.]auto_vacuum
120875 ** PRAGMA [schema.]auto_vacuum=N
120876 **
120877 ** Get or set the value of the database 'auto-vacuum' parameter.
120878 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
120879 */
120880#ifndef SQLITE_OMIT_AUTOVACUUM
120881 case PragTyp_AUTO_VACUUM: {
120882 Btree *pBt = pDb->pBt;
120883 assert( pBt!=0 );
120884 if( !zRight ){
120885 returnSingleInt(v, sqlite3BtreeGetAutoVacuum(pBt));
120886 }else{
120887 int eAuto = getAutoVacuum(zRight);
120888 assert( eAuto>=0 && eAuto<=2 );
120889 db->nextAutovac = (u8)eAuto;
120890 /* Call SetAutoVacuum() to set initialize the internal auto and
120891 ** incr-vacuum flags. This is required in case this connection
120892 ** creates the database file. It is important that it is created
120893 ** as an auto-vacuum capable db.
120894 */
120895 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
120896 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
120897 /* When setting the auto_vacuum mode to either "full" or
120898 ** "incremental", write the value of meta[6] in the database
120899 ** file. Before writing to meta[6], check that meta[3] indicates
120900 ** that this really is an auto-vacuum capable database.
120901 */
120902 static const int iLn = VDBE_OFFSET_LINENO(2);
120903 static const VdbeOpList setMeta6[] = {
120904 { OP_Transaction, 0, 1, 0}, /* 0 */
120905 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
120906 { OP_If, 1, 0, 0}, /* 2 */
120907 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
120908 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 0}, /* 4 */
120909 };
120910 VdbeOp *aOp;
120911 int iAddr = sqlite3VdbeCurrentAddr(v);
120912 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6));
120913 aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
120914 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
120915 aOp[0].p1 = iDb;
120916 aOp[1].p1 = iDb;
120917 aOp[2].p2 = iAddr+4;
120918 aOp[4].p1 = iDb;
120919 aOp[4].p3 = eAuto - 1;
120920 sqlite3VdbeUsesBtree(v, iDb);
120921 }
120922 }
120923 break;
120924 }
120925#endif
120926
120927 /*
120928 ** PRAGMA [schema.]incremental_vacuum(N)
120929 **
120930 ** Do N steps of incremental vacuuming on a database.
120931 */
120932#ifndef SQLITE_OMIT_AUTOVACUUM
120933 case PragTyp_INCREMENTAL_VACUUM: {
120934 int iLimit, addr;
120935 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
120936 iLimit = 0x7fffffff;
120937 }
120938 sqlite3BeginWriteOperation(pParse, 0, iDb);
120939 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
120940 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v);
120941 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
120942 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
120943 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v);
120944 sqlite3VdbeJumpHere(v, addr);
120945 break;
120946 }
120947#endif
120948
120949#ifndef SQLITE_OMIT_PAGER_PRAGMAS
120950 /*
120951 ** PRAGMA [schema.]cache_size
120952 ** PRAGMA [schema.]cache_size=N
120953 **
120954 ** The first form reports the current local setting for the
120955 ** page cache size. The second form sets the local
120956 ** page cache size value. If N is positive then that is the
120957 ** number of pages in the cache. If N is negative, then the
120958 ** number of pages is adjusted so that the cache uses -N kibibytes
120959 ** of memory.
120960 */
120961 case PragTyp_CACHE_SIZE: {
120962 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
120963 if( !zRight ){
120964 returnSingleInt(v, pDb->pSchema->cache_size);
120965 }else{
120966 int size = sqlite3Atoi(zRight);
120967 pDb->pSchema->cache_size = size;
120968 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
120969 }
120970 break;
120971 }
120972
120973 /*
120974 ** PRAGMA [schema.]cache_spill
120975 ** PRAGMA cache_spill=BOOLEAN
120976 ** PRAGMA [schema.]cache_spill=N
120977 **
120978 ** The first form reports the current local setting for the
120979 ** page cache spill size. The second form turns cache spill on
120980 ** or off. When turnning cache spill on, the size is set to the
120981 ** current cache_size. The third form sets a spill size that
120982 ** may be different form the cache size.
120983 ** If N is positive then that is the
120984 ** number of pages in the cache. If N is negative, then the
120985 ** number of pages is adjusted so that the cache uses -N kibibytes
120986 ** of memory.
120987 **
120988 ** If the number of cache_spill pages is less then the number of
120989 ** cache_size pages, no spilling occurs until the page count exceeds
120990 ** the number of cache_size pages.
120991 **
120992 ** The cache_spill=BOOLEAN setting applies to all attached schemas,
120993 ** not just the schema specified.
120994 */
120995 case PragTyp_CACHE_SPILL: {
120996 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
120997 if( !zRight ){
120998 returnSingleInt(v,
120999 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
121000 sqlite3BtreeSetSpillSize(pDb->pBt,0));
121001 }else{
121002 int size = 1;
121003 if( sqlite3GetInt32(zRight, &size) ){
121004 sqlite3BtreeSetSpillSize(pDb->pBt, size);
121005 }
121006 if( sqlite3GetBoolean(zRight, size!=0) ){
121007 db->flags |= SQLITE_CacheSpill;
121008 }else{
121009 db->flags &= ~SQLITE_CacheSpill;
121010 }
121011 setAllPagerFlags(db);
121012 }
121013 break;
121014 }
121015
121016 /*
121017 ** PRAGMA [schema.]mmap_size(N)
121018 **
121019 ** Used to set mapping size limit. The mapping size limit is
121020 ** used to limit the aggregate size of all memory mapped regions of the
121021 ** database file. If this parameter is set to zero, then memory mapping
121022 ** is not used at all. If N is negative, then the default memory map
121023 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
121024 ** The parameter N is measured in bytes.
121025 **
121026 ** This value is advisory. The underlying VFS is free to memory map
121027 ** as little or as much as it wants. Except, if N is set to 0 then the
121028 ** upper layers will never invoke the xFetch interfaces to the VFS.
121029 */
121030 case PragTyp_MMAP_SIZE: {
121031 sqlite3_int64 sz;
121032#if SQLITE_MAX_MMAP_SIZE>0
121033 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
121034 if( zRight ){
121035 int ii;
121036 sqlite3DecOrHexToI64(zRight, &sz);
121037 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
121038 if( pId2->n==0 ) db->szMmap = sz;
121039 for(ii=db->nDb-1; ii>=0; ii--){
121040 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
121041 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
121042 }
121043 }
121044 }
121045 sz = -1;
121046 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
121047#else
121048 sz = 0;
121049 rc = SQLITE_OK;
121050#endif
121051 if( rc==SQLITE_OK ){
121052 returnSingleInt(v, sz);
121053 }else if( rc!=SQLITE_NOTFOUND ){
121054 pParse->nErr++;
121055 pParse->rc = rc;
121056 }
121057 break;
121058 }
121059
121060 /*
121061 ** PRAGMA temp_store
121062 ** PRAGMA temp_store = "default"|"memory"|"file"
121063 **
121064 ** Return or set the local value of the temp_store flag. Changing
121065 ** the local value does not make changes to the disk file and the default
121066 ** value will be restored the next time the database is opened.
121067 **
121068 ** Note that it is possible for the library compile-time options to
121069 ** override this setting
121070 */
121071 case PragTyp_TEMP_STORE: {
121072 if( !zRight ){
121073 returnSingleInt(v, db->temp_store);
121074 }else{
121075 changeTempStorage(pParse, zRight);
121076 }
121077 break;
121078 }
121079
121080 /*
121081 ** PRAGMA temp_store_directory
121082 ** PRAGMA temp_store_directory = ""|"directory_name"
121083 **
121084 ** Return or set the local value of the temp_store_directory flag. Changing
121085 ** the value sets a specific directory to be used for temporary files.
121086 ** Setting to a null string reverts to the default temporary directory search.
121087 ** If temporary directory is changed, then invalidateTempStorage.
121088 **
121089 */
121090 case PragTyp_TEMP_STORE_DIRECTORY: {
121091 if( !zRight ){
121092 returnSingleText(v, sqlite3_temp_directory);
121093 }else{
121094#ifndef SQLITE_OMIT_WSD
121095 if( zRight[0] ){
121096 int res;
121097 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
121098 if( rc!=SQLITE_OK || res==0 ){
121099 sqlite3ErrorMsg(pParse, "not a writable directory");
121100 goto pragma_out;
121101 }
121102 }
121103 if( SQLITE_TEMP_STORE==0
121104 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
121105 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
121106 ){
121107 invalidateTempStorage(pParse);
121108 }
121109 sqlite3_free(sqlite3_temp_directory);
121110 if( zRight[0] ){
121111 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
121112 }else{
121113 sqlite3_temp_directory = 0;
121114 }
121115#endif /* SQLITE_OMIT_WSD */
121116 }
121117 break;
121118 }
121119
121120#if SQLITE_OS_WIN
121121 /*
121122 ** PRAGMA data_store_directory
121123 ** PRAGMA data_store_directory = ""|"directory_name"
121124 **
121125 ** Return or set the local value of the data_store_directory flag. Changing
121126 ** the value sets a specific directory to be used for database files that
121127 ** were specified with a relative pathname. Setting to a null string reverts
121128 ** to the default database directory, which for database files specified with
121129 ** a relative path will probably be based on the current directory for the
121130 ** process. Database file specified with an absolute path are not impacted
121131 ** by this setting, regardless of its value.
121132 **
121133 */
121134 case PragTyp_DATA_STORE_DIRECTORY: {
121135 if( !zRight ){
121136 returnSingleText(v, sqlite3_data_directory);
121137 }else{
121138#ifndef SQLITE_OMIT_WSD
121139 if( zRight[0] ){
121140 int res;
121141 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
121142 if( rc!=SQLITE_OK || res==0 ){
121143 sqlite3ErrorMsg(pParse, "not a writable directory");
121144 goto pragma_out;
121145 }
121146 }
121147 sqlite3_free(sqlite3_data_directory);
121148 if( zRight[0] ){
121149 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
121150 }else{
121151 sqlite3_data_directory = 0;
121152 }
121153#endif /* SQLITE_OMIT_WSD */
121154 }
121155 break;
121156 }
121157#endif
121158
121159#if SQLITE_ENABLE_LOCKING_STYLE
121160 /*
121161 ** PRAGMA [schema.]lock_proxy_file
121162 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
121163 **
121164 ** Return or set the value of the lock_proxy_file flag. Changing
121165 ** the value sets a specific file to be used for database access locks.
121166 **
121167 */
121168 case PragTyp_LOCK_PROXY_FILE: {
121169 if( !zRight ){
121170 Pager *pPager = sqlite3BtreePager(pDb->pBt);
121171 char *proxy_file_path = NULL;
121172 sqlite3_file *pFile = sqlite3PagerFile(pPager);
121173 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
121174 &proxy_file_path);
121175 returnSingleText(v, proxy_file_path);
121176 }else{
121177 Pager *pPager = sqlite3BtreePager(pDb->pBt);
121178 sqlite3_file *pFile = sqlite3PagerFile(pPager);
121179 int res;
121180 if( zRight[0] ){
121181 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
121182 zRight);
121183 } else {
121184 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
121185 NULL);
121186 }
121187 if( res!=SQLITE_OK ){
121188 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
121189 goto pragma_out;
121190 }
121191 }
121192 break;
121193 }
121194#endif /* SQLITE_ENABLE_LOCKING_STYLE */
121195
121196 /*
121197 ** PRAGMA [schema.]synchronous
121198 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
121199 **
121200 ** Return or set the local value of the synchronous flag. Changing
121201 ** the local value does not make changes to the disk file and the
121202 ** default value will be restored the next time the database is
121203 ** opened.
121204 */
121205 case PragTyp_SYNCHRONOUS: {
121206 if( !zRight ){
121207 returnSingleInt(v, pDb->safety_level-1);
121208 }else{
121209 if( !db->autoCommit ){
121210 sqlite3ErrorMsg(pParse,
121211 "Safety level may not be changed inside a transaction");
121212 }else if( iDb!=1 ){
121213 int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK;
121214 if( iLevel==0 ) iLevel = 1;
121215 pDb->safety_level = iLevel;
121216 pDb->bSyncSet = 1;
121217 setAllPagerFlags(db);
121218 }
121219 }
121220 break;
121221 }
121222#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
121223
121224#ifndef SQLITE_OMIT_FLAG_PRAGMAS
121225 case PragTyp_FLAG: {
121226 if( zRight==0 ){
121227 setPragmaResultColumnNames(v, pPragma);
121228 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
121229 }else{
121230 u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */
121231 if( db->autoCommit==0 ){
121232 /* Foreign key support may not be enabled or disabled while not
121233 ** in auto-commit mode. */
121234 mask &= ~(SQLITE_ForeignKeys);
121235 }
121236#if SQLITE_USER_AUTHENTICATION
121237 if( db->auth.authLevel==UAUTH_User ){
121238 /* Do not allow non-admin users to modify the schema arbitrarily */
121239 mask &= ~(SQLITE_WriteSchema);
121240 }
121241#endif
121242
121243 if( sqlite3GetBoolean(zRight, 0) ){
121244 db->flags |= mask;
121245 }else{
121246 db->flags &= ~mask;
121247 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
121248 }
121249
121250 /* Many of the flag-pragmas modify the code generated by the SQL
121251 ** compiler (eg. count_changes). So add an opcode to expire all
121252 ** compiled SQL statements after modifying a pragma value.
121253 */
121254 sqlite3VdbeAddOp0(v, OP_Expire);
121255 setAllPagerFlags(db);
121256 }
121257 break;
121258 }
121259#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
121260
121261#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
121262 /*
121263 ** PRAGMA table_info(<table>)
121264 **
121265 ** Return a single row for each column of the named table. The columns of
121266 ** the returned data set are:
121267 **
121268 ** cid: Column id (numbered from left to right, starting at 0)
121269 ** name: Column name
121270 ** type: Column declaration type.
121271 ** notnull: True if 'NOT NULL' is part of column declaration
121272 ** dflt_value: The default value for the column, if any.
121273 ** pk: Non-zero for PK fields.
121274 */
121275 case PragTyp_TABLE_INFO: if( zRight ){
121276 Table *pTab;
121277 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
121278 if( pTab ){
121279 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
121280 int i, k;
121281 int nHidden = 0;
121282 Column *pCol;
121283 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
121284 pParse->nMem = 7;
121285 sqlite3CodeVerifySchema(pParse, iTabDb);
121286 sqlite3ViewGetColumnNames(pParse, pTab);
121287 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
121288 int isHidden = IsHiddenColumn(pCol);
121289 if( isHidden && pPragma->iArg==0 ){
121290 nHidden++;
121291 continue;
121292 }
121293 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
121294 k = 0;
121295 }else if( pPk==0 ){
121296 k = 1;
121297 }else{
121298 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
121299 }
121300 assert( pCol->pDflt==0 || pCol->pDflt->op==TK_SPAN );
121301 sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
121302 i-nHidden,
121303 pCol->zName,
121304 sqlite3ColumnType(pCol,""),
121305 pCol->notNull ? 1 : 0,
121306 pCol->pDflt ? pCol->pDflt->u.zToken : 0,
121307 k,
121308 isHidden);
121309 }
121310 }
121311 }
121312 break;
121313
121314#ifdef SQLITE_DEBUG
121315 case PragTyp_STATS: {
121316 Index *pIdx;
121317 HashElem *i;
121318 pParse->nMem = 5;
121319 sqlite3CodeVerifySchema(pParse, iDb);
121320 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
121321 Table *pTab = sqliteHashData(i);
121322 sqlite3VdbeMultiLoad(v, 1, "ssiii",
121323 pTab->zName,
121324 0,
121325 pTab->szTabRow,
121326 pTab->nRowLogEst,
121327 pTab->tabFlags);
121328 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
121329 sqlite3VdbeMultiLoad(v, 2, "siiiX",
121330 pIdx->zName,
121331 pIdx->szIdxRow,
121332 pIdx->aiRowLogEst[0],
121333 pIdx->hasStat1);
121334 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
121335 }
121336 }
121337 }
121338 break;
121339#endif
121340
121341 case PragTyp_INDEX_INFO: if( zRight ){
121342 Index *pIdx;
121343 Table *pTab;
121344 pIdx = sqlite3FindIndex(db, zRight, zDb);
121345 if( pIdx ){
121346 int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
121347 int i;
121348 int mx;
121349 if( pPragma->iArg ){
121350 /* PRAGMA index_xinfo (newer version with more rows and columns) */
121351 mx = pIdx->nColumn;
121352 pParse->nMem = 6;
121353 }else{
121354 /* PRAGMA index_info (legacy version) */
121355 mx = pIdx->nKeyCol;
121356 pParse->nMem = 3;
121357 }
121358 pTab = pIdx->pTable;
121359 sqlite3CodeVerifySchema(pParse, iIdxDb);
121360 assert( pParse->nMem<=pPragma->nPragCName );
121361 for(i=0; i<mx; i++){
121362 i16 cnum = pIdx->aiColumn[i];
121363 sqlite3VdbeMultiLoad(v, 1, "iisX", i, cnum,
121364 cnum<0 ? 0 : pTab->aCol[cnum].zName);
121365 if( pPragma->iArg ){
121366 sqlite3VdbeMultiLoad(v, 4, "isiX",
121367 pIdx->aSortOrder[i],
121368 pIdx->azColl[i],
121369 i<pIdx->nKeyCol);
121370 }
121371 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
121372 }
121373 }
121374 }
121375 break;
121376
121377 case PragTyp_INDEX_LIST: if( zRight ){
121378 Index *pIdx;
121379 Table *pTab;
121380 int i;
121381 pTab = sqlite3FindTable(db, zRight, zDb);
121382 if( pTab ){
121383 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
121384 pParse->nMem = 5;
121385 sqlite3CodeVerifySchema(pParse, iTabDb);
121386 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
121387 const char *azOrigin[] = { "c", "u", "pk" };
121388 sqlite3VdbeMultiLoad(v, 1, "isisi",
121389 i,
121390 pIdx->zName,
121391 IsUniqueIndex(pIdx),
121392 azOrigin[pIdx->idxType],
121393 pIdx->pPartIdxWhere!=0);
121394 }
121395 }
121396 }
121397 break;
121398
121399 case PragTyp_DATABASE_LIST: {
121400 int i;
121401 pParse->nMem = 3;
121402 for(i=0; i<db->nDb; i++){
121403 if( db->aDb[i].pBt==0 ) continue;
121404 assert( db->aDb[i].zDbSName!=0 );
121405 sqlite3VdbeMultiLoad(v, 1, "iss",
121406 i,
121407 db->aDb[i].zDbSName,
121408 sqlite3BtreeGetFilename(db->aDb[i].pBt));
121409 }
121410 }
121411 break;
121412
121413 case PragTyp_COLLATION_LIST: {
121414 int i = 0;
121415 HashElem *p;
121416 pParse->nMem = 2;
121417 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
121418 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
121419 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
121420 }
121421 }
121422 break;
121423
121424#ifdef SQLITE_INTROSPECTION_PRAGMAS
121425 case PragTyp_FUNCTION_LIST: {
121426 int i;
121427 HashElem *j;
121428 FuncDef *p;
121429 pParse->nMem = 2;
121430 for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
121431 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
121432 if( p->funcFlags & SQLITE_FUNC_INTERNAL ) continue;
121433 sqlite3VdbeMultiLoad(v, 1, "si", p->zName, 1);
121434 }
121435 }
121436 for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
121437 p = (FuncDef*)sqliteHashData(j);
121438 sqlite3VdbeMultiLoad(v, 1, "si", p->zName, 0);
121439 }
121440 }
121441 break;
121442
121443#ifndef SQLITE_OMIT_VIRTUALTABLE
121444 case PragTyp_MODULE_LIST: {
121445 HashElem *j;
121446 pParse->nMem = 1;
121447 for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){
121448 Module *pMod = (Module*)sqliteHashData(j);
121449 sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
121450 }
121451 }
121452 break;
121453#endif /* SQLITE_OMIT_VIRTUALTABLE */
121454
121455 case PragTyp_PRAGMA_LIST: {
121456 int i;
121457 for(i=0; i<ArraySize(aPragmaName); i++){
121458 sqlite3VdbeMultiLoad(v, 1, "s", aPragmaName[i].zName);
121459 }
121460 }
121461 break;
121462#endif /* SQLITE_INTROSPECTION_PRAGMAS */
121463
121464#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
121465
121466#ifndef SQLITE_OMIT_FOREIGN_KEY
121467 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
121468 FKey *pFK;
121469 Table *pTab;
121470 pTab = sqlite3FindTable(db, zRight, zDb);
121471 if( pTab ){
121472 pFK = pTab->pFKey;
121473 if( pFK ){
121474 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
121475 int i = 0;
121476 pParse->nMem = 8;
121477 sqlite3CodeVerifySchema(pParse, iTabDb);
121478 while(pFK){
121479 int j;
121480 for(j=0; j<pFK->nCol; j++){
121481 sqlite3VdbeMultiLoad(v, 1, "iissssss",
121482 i,
121483 j,
121484 pFK->zTo,
121485 pTab->aCol[pFK->aCol[j].iFrom].zName,
121486 pFK->aCol[j].zCol,
121487 actionName(pFK->aAction[1]), /* ON UPDATE */
121488 actionName(pFK->aAction[0]), /* ON DELETE */
121489 "NONE");
121490 }
121491 ++i;
121492 pFK = pFK->pNextFrom;
121493 }
121494 }
121495 }
121496 }
121497 break;
121498#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
121499
121500#ifndef SQLITE_OMIT_FOREIGN_KEY
121501#ifndef SQLITE_OMIT_TRIGGER
121502 case PragTyp_FOREIGN_KEY_CHECK: {
121503 FKey *pFK; /* A foreign key constraint */
121504 Table *pTab; /* Child table contain "REFERENCES" keyword */
121505 Table *pParent; /* Parent table that child points to */
121506 Index *pIdx; /* Index in the parent table */
121507 int i; /* Loop counter: Foreign key number for pTab */
121508 int j; /* Loop counter: Field of the foreign key */
121509 HashElem *k; /* Loop counter: Next table in schema */
121510 int x; /* result variable */
121511 int regResult; /* 3 registers to hold a result row */
121512 int regKey; /* Register to hold key for checking the FK */
121513 int regRow; /* Registers to hold a row from pTab */
121514 int addrTop; /* Top of a loop checking foreign keys */
121515 int addrOk; /* Jump here if the key is OK */
121516 int *aiCols; /* child to parent column mapping */
121517
121518 regResult = pParse->nMem+1;
121519 pParse->nMem += 4;
121520 regKey = ++pParse->nMem;
121521 regRow = ++pParse->nMem;
121522 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
121523 while( k ){
121524 int iTabDb;
121525 if( zRight ){
121526 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
121527 k = 0;
121528 }else{
121529 pTab = (Table*)sqliteHashData(k);
121530 k = sqliteHashNext(k);
121531 }
121532 if( pTab==0 || pTab->pFKey==0 ) continue;
121533 iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
121534 sqlite3CodeVerifySchema(pParse, iTabDb);
121535 sqlite3TableLock(pParse, iTabDb, pTab->tnum, 0, pTab->zName);
121536 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
121537 sqlite3OpenTable(pParse, 0, iTabDb, pTab, OP_OpenRead);
121538 sqlite3VdbeLoadString(v, regResult, pTab->zName);
121539 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
121540 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
121541 if( pParent==0 ) continue;
121542 pIdx = 0;
121543 sqlite3TableLock(pParse, iTabDb, pParent->tnum, 0, pParent->zName);
121544 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
121545 if( x==0 ){
121546 if( pIdx==0 ){
121547 sqlite3OpenTable(pParse, i, iTabDb, pParent, OP_OpenRead);
121548 }else{
121549 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iTabDb);
121550 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
121551 }
121552 }else{
121553 k = 0;
121554 break;
121555 }
121556 }
121557 assert( pParse->nErr>0 || pFK==0 );
121558 if( pFK ) break;
121559 if( pParse->nTab<i ) pParse->nTab = i;
121560 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
121561 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
121562 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
121563 pIdx = 0;
121564 aiCols = 0;
121565 if( pParent ){
121566 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
121567 assert( x==0 );
121568 }
121569 addrOk = sqlite3VdbeMakeLabel(v);
121570
121571 /* Generate code to read the child key values into registers
121572 ** regRow..regRow+n. If any of the child key values are NULL, this
121573 ** row cannot cause an FK violation. Jump directly to addrOk in
121574 ** this case. */
121575 for(j=0; j<pFK->nCol; j++){
121576 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
121577 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0, iCol, regRow+j);
121578 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
121579 }
121580
121581 /* Generate code to query the parent index for a matching parent
121582 ** key. If a match is found, jump to addrOk. */
121583 if( pIdx ){
121584 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
121585 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
121586 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
121587 VdbeCoverage(v);
121588 }else if( pParent ){
121589 int jmp = sqlite3VdbeCurrentAddr(v)+2;
121590 sqlite3VdbeAddOp3(v, OP_SeekRowid, i, jmp, regRow); VdbeCoverage(v);
121591 sqlite3VdbeGoto(v, addrOk);
121592 assert( pFK->nCol==1 );
121593 }
121594
121595 /* Generate code to report an FK violation to the caller. */
121596 if( HasRowid(pTab) ){
121597 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
121598 }else{
121599 sqlite3VdbeAddOp2(v, OP_Null, 0, regResult+1);
121600 }
121601 sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
121602 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
121603 sqlite3VdbeResolveLabel(v, addrOk);
121604 sqlite3DbFree(db, aiCols);
121605 }
121606 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
121607 sqlite3VdbeJumpHere(v, addrTop);
121608 }
121609 }
121610 break;
121611#endif /* !defined(SQLITE_OMIT_TRIGGER) */
121612#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
121613
121614#ifndef NDEBUG
121615 case PragTyp_PARSER_TRACE: {
121616 if( zRight ){
121617 if( sqlite3GetBoolean(zRight, 0) ){
121618 sqlite3ParserTrace(stdout, "parser: ");
121619 }else{
121620 sqlite3ParserTrace(0, 0);
121621 }
121622 }
121623 }
121624 break;
121625#endif
121626
121627 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
121628 ** used will be case sensitive or not depending on the RHS.
121629 */
121630 case PragTyp_CASE_SENSITIVE_LIKE: {
121631 if( zRight ){
121632 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
121633 }
121634 }
121635 break;
121636
121637#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
121638# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
121639#endif
121640
121641#ifndef SQLITE_OMIT_INTEGRITY_CHECK
121642 /* PRAGMA integrity_check
121643 ** PRAGMA integrity_check(N)
121644 ** PRAGMA quick_check
121645 ** PRAGMA quick_check(N)
121646 **
121647 ** Verify the integrity of the database.
121648 **
121649 ** The "quick_check" is reduced version of
121650 ** integrity_check designed to detect most database corruption
121651 ** without the overhead of cross-checking indexes. Quick_check
121652 ** is linear time wherease integrity_check is O(NlogN).
121653 */
121654 case PragTyp_INTEGRITY_CHECK: {
121655 int i, j, addr, mxErr;
121656
121657 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
121658
121659 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
121660 ** then iDb is set to the index of the database identified by <db>.
121661 ** In this case, the integrity of database iDb only is verified by
121662 ** the VDBE created below.
121663 **
121664 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
121665 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
121666 ** to -1 here, to indicate that the VDBE should verify the integrity
121667 ** of all attached databases. */
121668 assert( iDb>=0 );
121669 assert( iDb==0 || pId2->z );
121670 if( pId2->z==0 ) iDb = -1;
121671
121672 /* Initialize the VDBE program */
121673 pParse->nMem = 6;
121674
121675 /* Set the maximum error count */
121676 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
121677 if( zRight ){
121678 sqlite3GetInt32(zRight, &mxErr);
121679 if( mxErr<=0 ){
121680 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
121681 }
121682 }
121683 sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
121684
121685 /* Do an integrity check on each database file */
121686 for(i=0; i<db->nDb; i++){
121687 HashElem *x; /* For looping over tables in the schema */
121688 Hash *pTbls; /* Set of all tables in the schema */
121689 int *aRoot; /* Array of root page numbers of all btrees */
121690 int cnt = 0; /* Number of entries in aRoot[] */
121691 int mxIdx = 0; /* Maximum number of indexes for any table */
121692
121693 if( OMIT_TEMPDB && i==1 ) continue;
121694 if( iDb>=0 && i!=iDb ) continue;
121695
121696 sqlite3CodeVerifySchema(pParse, i);
121697
121698 /* Do an integrity check of the B-Tree
121699 **
121700 ** Begin by finding the root pages numbers
121701 ** for all tables and indices in the database.
121702 */
121703 assert( sqlite3SchemaMutexHeld(db, i, 0) );
121704 pTbls = &db->aDb[i].pSchema->tblHash;
121705 for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
121706 Table *pTab = sqliteHashData(x); /* Current table */
121707 Index *pIdx; /* An index on pTab */
121708 int nIdx; /* Number of indexes on pTab */
121709 if( HasRowid(pTab) ) cnt++;
121710 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
121711 if( nIdx>mxIdx ) mxIdx = nIdx;
121712 }
121713 aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1));
121714 if( aRoot==0 ) break;
121715 for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
121716 Table *pTab = sqliteHashData(x);
121717 Index *pIdx;
121718 if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum;
121719 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
121720 aRoot[++cnt] = pIdx->tnum;
121721 }
121722 }
121723 aRoot[0] = cnt;
121724
121725 /* Make sure sufficient number of registers have been allocated */
121726 pParse->nMem = MAX( pParse->nMem, 8+mxIdx );
121727 sqlite3ClearTempRegCache(pParse);
121728
121729 /* Do the b-tree integrity checks */
121730 sqlite3VdbeAddOp4(v, OP_IntegrityCk, 2, cnt, 1, (char*)aRoot,P4_INTARRAY);
121731 sqlite3VdbeChangeP5(v, (u8)i);
121732 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
121733 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
121734 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
121735 P4_DYNAMIC);
121736 sqlite3VdbeAddOp3(v, OP_Concat, 2, 3, 3);
121737 integrityCheckResultRow(v);
121738 sqlite3VdbeJumpHere(v, addr);
121739
121740 /* Make sure all the indices are constructed correctly.
121741 */
121742 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
121743 Table *pTab = sqliteHashData(x);
121744 Index *pIdx, *pPk;
121745 Index *pPrior = 0;
121746 int loopTop;
121747 int iDataCur, iIdxCur;
121748 int r1 = -1;
121749
121750 if( pTab->tnum<1 ) continue; /* Skip VIEWs or VIRTUAL TABLEs */
121751 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
121752 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
121753 1, 0, &iDataCur, &iIdxCur);
121754 /* reg[7] counts the number of entries in the table.
121755 ** reg[8+i] counts the number of entries in the i-th index
121756 */
121757 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
121758 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
121759 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
121760 }
121761 assert( pParse->nMem>=8+j );
121762 assert( sqlite3NoTempsInRange(pParse,1,7+j) );
121763 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
121764 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
121765 if( !isQuick ){
121766 /* Sanity check on record header decoding */
121767 sqlite3VdbeAddOp3(v, OP_Column, iDataCur, pTab->nCol-1, 3);
121768 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
121769 }
121770 /* Verify that all NOT NULL columns really are NOT NULL */
121771 for(j=0; j<pTab->nCol; j++){
121772 char *zErr;
121773 int jmp2;
121774 if( j==pTab->iPKey ) continue;
121775 if( pTab->aCol[j].notNull==0 ) continue;
121776 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
121777 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
121778 jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
121779 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
121780 pTab->aCol[j].zName);
121781 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
121782 integrityCheckResultRow(v);
121783 sqlite3VdbeJumpHere(v, jmp2);
121784 }
121785 /* Verify CHECK constraints */
121786 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
121787 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
121788 if( db->mallocFailed==0 ){
121789 int addrCkFault = sqlite3VdbeMakeLabel(v);
121790 int addrCkOk = sqlite3VdbeMakeLabel(v);
121791 char *zErr;
121792 int k;
121793 pParse->iSelfTab = iDataCur + 1;
121794 for(k=pCheck->nExpr-1; k>0; k--){
121795 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
121796 }
121797 sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
121798 SQLITE_JUMPIFNULL);
121799 sqlite3VdbeResolveLabel(v, addrCkFault);
121800 pParse->iSelfTab = 0;
121801 zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s",
121802 pTab->zName);
121803 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
121804 integrityCheckResultRow(v);
121805 sqlite3VdbeResolveLabel(v, addrCkOk);
121806 }
121807 sqlite3ExprListDelete(db, pCheck);
121808 }
121809 if( !isQuick ){ /* Omit the remaining tests for quick_check */
121810 /* Validate index entries for the current row */
121811 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
121812 int jmp2, jmp3, jmp4, jmp5;
121813 int ckUniq = sqlite3VdbeMakeLabel(v);
121814 if( pPk==pIdx ) continue;
121815 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
121816 pPrior, r1);
121817 pPrior = pIdx;
121818 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);/* increment entry count */
121819 /* Verify that an index entry exists for the current table row */
121820 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
121821 pIdx->nColumn); VdbeCoverage(v);
121822 sqlite3VdbeLoadString(v, 3, "row ");
121823 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
121824 sqlite3VdbeLoadString(v, 4, " missing from index ");
121825 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
121826 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
121827 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
121828 jmp4 = integrityCheckResultRow(v);
121829 sqlite3VdbeJumpHere(v, jmp2);
121830 /* For UNIQUE indexes, verify that only one entry exists with the
121831 ** current key. The entry is unique if (1) any column is NULL
121832 ** or (2) the next entry has a different key */
121833 if( IsUniqueIndex(pIdx) ){
121834 int uniqOk = sqlite3VdbeMakeLabel(v);
121835 int jmp6;
121836 int kk;
121837 for(kk=0; kk<pIdx->nKeyCol; kk++){
121838 int iCol = pIdx->aiColumn[kk];
121839 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
121840 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
121841 sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
121842 VdbeCoverage(v);
121843 }
121844 jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
121845 sqlite3VdbeGoto(v, uniqOk);
121846 sqlite3VdbeJumpHere(v, jmp6);
121847 sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
121848 pIdx->nKeyCol); VdbeCoverage(v);
121849 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
121850 sqlite3VdbeGoto(v, jmp5);
121851 sqlite3VdbeResolveLabel(v, uniqOk);
121852 }
121853 sqlite3VdbeJumpHere(v, jmp4);
121854 sqlite3ResolvePartIdxLabel(pParse, jmp3);
121855 }
121856 }
121857 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
121858 sqlite3VdbeJumpHere(v, loopTop-1);
121859#ifndef SQLITE_OMIT_BTREECOUNT
121860 if( !isQuick ){
121861 sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
121862 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
121863 if( pPk==pIdx ) continue;
121864 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
121865 addr = sqlite3VdbeAddOp3(v, OP_Eq, 8+j, 0, 3); VdbeCoverage(v);
121866 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
121867 sqlite3VdbeLoadString(v, 4, pIdx->zName);
121868 sqlite3VdbeAddOp3(v, OP_Concat, 4, 2, 3);
121869 integrityCheckResultRow(v);
121870 sqlite3VdbeJumpHere(v, addr);
121871 }
121872 }
121873#endif /* SQLITE_OMIT_BTREECOUNT */
121874 }
121875 }
121876 {
121877 static const int iLn = VDBE_OFFSET_LINENO(2);
121878 static const VdbeOpList endCode[] = {
121879 { OP_AddImm, 1, 0, 0}, /* 0 */
121880 { OP_IfNotZero, 1, 4, 0}, /* 1 */
121881 { OP_String8, 0, 3, 0}, /* 2 */
121882 { OP_ResultRow, 3, 1, 0}, /* 3 */
121883 { OP_Halt, 0, 0, 0}, /* 4 */
121884 { OP_String8, 0, 3, 0}, /* 5 */
121885 { OP_Goto, 0, 3, 0}, /* 6 */
121886 };
121887 VdbeOp *aOp;
121888
121889 aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
121890 if( aOp ){
121891 aOp[0].p2 = 1-mxErr;
121892 aOp[2].p4type = P4_STATIC;
121893 aOp[2].p4.z = "ok";
121894 aOp[5].p4type = P4_STATIC;
121895 aOp[5].p4.z = (char*)sqlite3ErrStr(SQLITE_CORRUPT);
121896 }
121897 sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
121898 }
121899 }
121900 break;
121901#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
121902
121903#ifndef SQLITE_OMIT_UTF16
121904 /*
121905 ** PRAGMA encoding
121906 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
121907 **
121908 ** In its first form, this pragma returns the encoding of the main
121909 ** database. If the database is not initialized, it is initialized now.
121910 **
121911 ** The second form of this pragma is a no-op if the main database file
121912 ** has not already been initialized. In this case it sets the default
121913 ** encoding that will be used for the main database file if a new file
121914 ** is created. If an existing main database file is opened, then the
121915 ** default text encoding for the existing database is used.
121916 **
121917 ** In all cases new databases created using the ATTACH command are
121918 ** created to use the same default text encoding as the main database. If
121919 ** the main database has not been initialized and/or created when ATTACH
121920 ** is executed, this is done before the ATTACH operation.
121921 **
121922 ** In the second form this pragma sets the text encoding to be used in
121923 ** new database files created using this database handle. It is only
121924 ** useful if invoked immediately after the main database i
121925 */
121926 case PragTyp_ENCODING: {
121927 static const struct EncName {
121928 char *zName;
121929 u8 enc;
121930 } encnames[] = {
121931 { "UTF8", SQLITE_UTF8 },
121932 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
121933 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
121934 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
121935 { "UTF16le", SQLITE_UTF16LE },
121936 { "UTF16be", SQLITE_UTF16BE },
121937 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
121938 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
121939 { 0, 0 }
121940 };
121941 const struct EncName *pEnc;
121942 if( !zRight ){ /* "PRAGMA encoding" */
121943 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
121944 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
121945 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
121946 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
121947 returnSingleText(v, encnames[ENC(pParse->db)].zName);
121948 }else{ /* "PRAGMA encoding = XXX" */
121949 /* Only change the value of sqlite.enc if the database handle is not
121950 ** initialized. If the main database exists, the new sqlite.enc value
121951 ** will be overwritten when the schema is next loaded. If it does not
121952 ** already exists, it will be created to use the new encoding value.
121953 */
121954 if(
121955 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
121956 DbHasProperty(db, 0, DB_Empty)
121957 ){
121958 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
121959 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
121960 SCHEMA_ENC(db) = ENC(db) =
121961 pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
121962 break;
121963 }
121964 }
121965 if( !pEnc->zName ){
121966 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
121967 }
121968 }
121969 }
121970 }
121971 break;
121972#endif /* SQLITE_OMIT_UTF16 */
121973
121974#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
121975 /*
121976 ** PRAGMA [schema.]schema_version
121977 ** PRAGMA [schema.]schema_version = <integer>
121978 **
121979 ** PRAGMA [schema.]user_version
121980 ** PRAGMA [schema.]user_version = <integer>
121981 **
121982 ** PRAGMA [schema.]freelist_count
121983 **
121984 ** PRAGMA [schema.]data_version
121985 **
121986 ** PRAGMA [schema.]application_id
121987 ** PRAGMA [schema.]application_id = <integer>
121988 **
121989 ** The pragma's schema_version and user_version are used to set or get
121990 ** the value of the schema-version and user-version, respectively. Both
121991 ** the schema-version and the user-version are 32-bit signed integers
121992 ** stored in the database header.
121993 **
121994 ** The schema-cookie is usually only manipulated internally by SQLite. It
121995 ** is incremented by SQLite whenever the database schema is modified (by
121996 ** creating or dropping a table or index). The schema version is used by
121997 ** SQLite each time a query is executed to ensure that the internal cache
121998 ** of the schema used when compiling the SQL query matches the schema of
121999 ** the database against which the compiled query is actually executed.
122000 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
122001 ** the schema-version is potentially dangerous and may lead to program
122002 ** crashes or database corruption. Use with caution!
122003 **
122004 ** The user-version is not used internally by SQLite. It may be used by
122005 ** applications for any purpose.
122006 */
122007 case PragTyp_HEADER_VALUE: {
122008 int iCookie = pPragma->iArg; /* Which cookie to read or write */
122009 sqlite3VdbeUsesBtree(v, iDb);
122010 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
122011 /* Write the specified cookie value */
122012 static const VdbeOpList setCookie[] = {
122013 { OP_Transaction, 0, 1, 0}, /* 0 */
122014 { OP_SetCookie, 0, 0, 0}, /* 1 */
122015 };
122016 VdbeOp *aOp;
122017 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
122018 aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
122019 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
122020 aOp[0].p1 = iDb;
122021 aOp[1].p1 = iDb;
122022 aOp[1].p2 = iCookie;
122023 aOp[1].p3 = sqlite3Atoi(zRight);
122024 }else{
122025 /* Read the specified cookie value */
122026 static const VdbeOpList readCookie[] = {
122027 { OP_Transaction, 0, 0, 0}, /* 0 */
122028 { OP_ReadCookie, 0, 1, 0}, /* 1 */
122029 { OP_ResultRow, 1, 1, 0}
122030 };
122031 VdbeOp *aOp;
122032 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie));
122033 aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie),readCookie,0);
122034 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
122035 aOp[0].p1 = iDb;
122036 aOp[1].p1 = iDb;
122037 aOp[1].p3 = iCookie;
122038 sqlite3VdbeReusable(v);
122039 }
122040 }
122041 break;
122042#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
122043
122044#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
122045 /*
122046 ** PRAGMA compile_options
122047 **
122048 ** Return the names of all compile-time options used in this build,
122049 ** one option per row.
122050 */
122051 case PragTyp_COMPILE_OPTIONS: {
122052 int i = 0;
122053 const char *zOpt;
122054 pParse->nMem = 1;
122055 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
122056 sqlite3VdbeLoadString(v, 1, zOpt);
122057 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
122058 }
122059 sqlite3VdbeReusable(v);
122060 }
122061 break;
122062#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
122063
122064#ifndef SQLITE_OMIT_WAL
122065 /*
122066 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
122067 **
122068 ** Checkpoint the database.
122069 */
122070 case PragTyp_WAL_CHECKPOINT: {
122071 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
122072 int eMode = SQLITE_CHECKPOINT_PASSIVE;
122073 if( zRight ){
122074 if( sqlite3StrICmp(zRight, "full")==0 ){
122075 eMode = SQLITE_CHECKPOINT_FULL;
122076 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
122077 eMode = SQLITE_CHECKPOINT_RESTART;
122078 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
122079 eMode = SQLITE_CHECKPOINT_TRUNCATE;
122080 }
122081 }
122082 pParse->nMem = 3;
122083 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
122084 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
122085 }
122086 break;
122087
122088 /*
122089 ** PRAGMA wal_autocheckpoint
122090 ** PRAGMA wal_autocheckpoint = N
122091 **
122092 ** Configure a database connection to automatically checkpoint a database
122093 ** after accumulating N frames in the log. Or query for the current value
122094 ** of N.
122095 */
122096 case PragTyp_WAL_AUTOCHECKPOINT: {
122097 if( zRight ){
122098 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
122099 }
122100 returnSingleInt(v,
122101 db->xWalCallback==sqlite3WalDefaultHook ?
122102 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
122103 }
122104 break;
122105#endif
122106
122107 /*
122108 ** PRAGMA shrink_memory
122109 **
122110 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
122111 ** connection on which it is invoked to free up as much memory as it
122112 ** can, by calling sqlite3_db_release_memory().
122113 */
122114 case PragTyp_SHRINK_MEMORY: {
122115 sqlite3_db_release_memory(db);
122116 break;
122117 }
122118
122119 /*
122120 ** PRAGMA optimize
122121 ** PRAGMA optimize(MASK)
122122 ** PRAGMA schema.optimize
122123 ** PRAGMA schema.optimize(MASK)
122124 **
122125 ** Attempt to optimize the database. All schemas are optimized in the first
122126 ** two forms, and only the specified schema is optimized in the latter two.
122127 **
122128 ** The details of optimizations performed by this pragma are expected
122129 ** to change and improve over time. Applications should anticipate that
122130 ** this pragma will perform new optimizations in future releases.
122131 **
122132 ** The optional argument is a bitmask of optimizations to perform:
122133 **
122134 ** 0x0001 Debugging mode. Do not actually perform any optimizations
122135 ** but instead return one line of text for each optimization
122136 ** that would have been done. Off by default.
122137 **
122138 ** 0x0002 Run ANALYZE on tables that might benefit. On by default.
122139 ** See below for additional information.
122140 **
122141 ** 0x0004 (Not yet implemented) Record usage and performance
122142 ** information from the current session in the
122143 ** database file so that it will be available to "optimize"
122144 ** pragmas run by future database connections.
122145 **
122146 ** 0x0008 (Not yet implemented) Create indexes that might have
122147 ** been helpful to recent queries
122148 **
122149 ** The default MASK is and always shall be 0xfffe. 0xfffe means perform all
122150 ** of the optimizations listed above except Debug Mode, including new
122151 ** optimizations that have not yet been invented. If new optimizations are
122152 ** ever added that should be off by default, those off-by-default
122153 ** optimizations will have bitmasks of 0x10000 or larger.
122154 **
122155 ** DETERMINATION OF WHEN TO RUN ANALYZE
122156 **
122157 ** In the current implementation, a table is analyzed if only if all of
122158 ** the following are true:
122159 **
122160 ** (1) MASK bit 0x02 is set.
122161 **
122162 ** (2) The query planner used sqlite_stat1-style statistics for one or
122163 ** more indexes of the table at some point during the lifetime of
122164 ** the current connection.
122165 **
122166 ** (3) One or more indexes of the table are currently unanalyzed OR
122167 ** the number of rows in the table has increased by 25 times or more
122168 ** since the last time ANALYZE was run.
122169 **
122170 ** The rules for when tables are analyzed are likely to change in
122171 ** future releases.
122172 */
122173 case PragTyp_OPTIMIZE: {
122174 int iDbLast; /* Loop termination point for the schema loop */
122175 int iTabCur; /* Cursor for a table whose size needs checking */
122176 HashElem *k; /* Loop over tables of a schema */
122177 Schema *pSchema; /* The current schema */
122178 Table *pTab; /* A table in the schema */
122179 Index *pIdx; /* An index of the table */
122180 LogEst szThreshold; /* Size threshold above which reanalysis is needd */
122181 char *zSubSql; /* SQL statement for the OP_SqlExec opcode */
122182 u32 opMask; /* Mask of operations to perform */
122183
122184 if( zRight ){
122185 opMask = (u32)sqlite3Atoi(zRight);
122186 if( (opMask & 0x02)==0 ) break;
122187 }else{
122188 opMask = 0xfffe;
122189 }
122190 iTabCur = pParse->nTab++;
122191 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
122192 if( iDb==1 ) continue;
122193 sqlite3CodeVerifySchema(pParse, iDb);
122194 pSchema = db->aDb[iDb].pSchema;
122195 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
122196 pTab = (Table*)sqliteHashData(k);
122197
122198 /* If table pTab has not been used in a way that would benefit from
122199 ** having analysis statistics during the current session, then skip it.
122200 ** This also has the effect of skipping virtual tables and views */
122201 if( (pTab->tabFlags & TF_StatsUsed)==0 ) continue;
122202
122203 /* Reanalyze if the table is 25 times larger than the last analysis */
122204 szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 );
122205 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
122206 if( !pIdx->hasStat1 ){
122207 szThreshold = 0; /* Always analyze if any index lacks statistics */
122208 break;
122209 }
122210 }
122211 if( szThreshold ){
122212 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
122213 sqlite3VdbeAddOp3(v, OP_IfSmaller, iTabCur,
122214 sqlite3VdbeCurrentAddr(v)+2+(opMask&1), szThreshold);
122215 VdbeCoverage(v);
122216 }
122217 zSubSql = sqlite3MPrintf(db, "ANALYZE \"%w\".\"%w\"",
122218 db->aDb[iDb].zDbSName, pTab->zName);
122219 if( opMask & 0x01 ){
122220 int r1 = sqlite3GetTempReg(pParse);
122221 sqlite3VdbeAddOp4(v, OP_String8, 0, r1, 0, zSubSql, P4_DYNAMIC);
122222 sqlite3VdbeAddOp2(v, OP_ResultRow, r1, 1);
122223 }else{
122224 sqlite3VdbeAddOp4(v, OP_SqlExec, 0, 0, 0, zSubSql, P4_DYNAMIC);
122225 }
122226 }
122227 }
122228 sqlite3VdbeAddOp0(v, OP_Expire);
122229 break;
122230 }
122231
122232 /*
122233 ** PRAGMA busy_timeout
122234 ** PRAGMA busy_timeout = N
122235 **
122236 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
122237 ** if one is set. If no busy handler or a different busy handler is set
122238 ** then 0 is returned. Setting the busy_timeout to 0 or negative
122239 ** disables the timeout.
122240 */
122241 /*case PragTyp_BUSY_TIMEOUT*/ default: {
122242 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
122243 if( zRight ){
122244 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
122245 }
122246 returnSingleInt(v, db->busyTimeout);
122247 break;
122248 }
122249
122250 /*
122251 ** PRAGMA soft_heap_limit
122252 ** PRAGMA soft_heap_limit = N
122253 **
122254 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
122255 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
122256 ** specified and is a non-negative integer.
122257 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
122258 ** returns the same integer that would be returned by the
122259 ** sqlite3_soft_heap_limit64(-1) C-language function.
122260 */
122261 case PragTyp_SOFT_HEAP_LIMIT: {
122262 sqlite3_int64 N;
122263 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
122264 sqlite3_soft_heap_limit64(N);
122265 }
122266 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
122267 break;
122268 }
122269
122270 /*
122271 ** PRAGMA threads
122272 ** PRAGMA threads = N
122273 **
122274 ** Configure the maximum number of worker threads. Return the new
122275 ** maximum, which might be less than requested.
122276 */
122277 case PragTyp_THREADS: {
122278 sqlite3_int64 N;
122279 if( zRight
122280 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK
122281 && N>=0
122282 ){
122283 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff));
122284 }
122285 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
122286 break;
122287 }
122288
122289#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
122290 /*
122291 ** Report the current state of file logs for all databases
122292 */
122293 case PragTyp_LOCK_STATUS: {
122294 static const char *const azLockName[] = {
122295 "unlocked", "shared", "reserved", "pending", "exclusive"
122296 };
122297 int i;
122298 pParse->nMem = 2;
122299 for(i=0; i<db->nDb; i++){
122300 Btree *pBt;
122301 const char *zState = "unknown";
122302 int j;
122303 if( db->aDb[i].zDbSName==0 ) continue;
122304 pBt = db->aDb[i].pBt;
122305 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
122306 zState = "closed";
122307 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
122308 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
122309 zState = azLockName[j];
122310 }
122311 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
122312 }
122313 break;
122314 }
122315#endif
122316
122317#ifdef SQLITE_HAS_CODEC
122318 /* Pragma iArg
122319 ** ---------- ------
122320 ** key 0
122321 ** rekey 1
122322 ** hexkey 2
122323 ** hexrekey 3
122324 ** textkey 4
122325 ** textrekey 5
122326 */
122327 case PragTyp_KEY: {
122328 if( zRight ){
122329 int n = pPragma->iArg<4 ? sqlite3Strlen30(zRight) : -1;
122330 if( (pPragma->iArg & 1)==0 ){
122331 sqlite3_key_v2(db, zDb, zRight, n);
122332 }else{
122333 sqlite3_rekey_v2(db, zDb, zRight, n);
122334 }
122335 }
122336 break;
122337 }
122338 case PragTyp_HEXKEY: {
122339 if( zRight ){
122340 u8 iByte;
122341 int i;
122342 char zKey[40];
122343 for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){
122344 iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
122345 if( (i&1)!=0 ) zKey[i/2] = iByte;
122346 }
122347 if( (pPragma->iArg & 1)==0 ){
122348 sqlite3_key_v2(db, zDb, zKey, i/2);
122349 }else{
122350 sqlite3_rekey_v2(db, zDb, zKey, i/2);
122351 }
122352 }
122353 break;
122354 }
122355#endif
122356#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
122357 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
122358#ifdef SQLITE_HAS_CODEC
122359 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
122360 sqlite3_activate_see(&zRight[4]);
122361 }
122362#endif
122363#ifdef SQLITE_ENABLE_CEROD
122364 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
122365 sqlite3_activate_cerod(&zRight[6]);
122366 }
122367#endif
122368 }
122369 break;
122370#endif
122371
122372 } /* End of the PRAGMA switch */
122373
122374 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
122375 ** purpose is to execute assert() statements to verify that if the
122376 ** PragFlg_NoColumns1 flag is set and the caller specified an argument
122377 ** to the PRAGMA, the implementation has not added any OP_ResultRow
122378 ** instructions to the VM. */
122379 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
122380 sqlite3VdbeVerifyNoResultRow(v);
122381 }
122382
122383pragma_out:
122384 sqlite3DbFree(db, zLeft);
122385 sqlite3DbFree(db, zRight);
122386}
122387#ifndef SQLITE_OMIT_VIRTUALTABLE
122388/*****************************************************************************
122389** Implementation of an eponymous virtual table that runs a pragma.
122390**
122391*/
122392typedef struct PragmaVtab PragmaVtab;
122393typedef struct PragmaVtabCursor PragmaVtabCursor;
122394struct PragmaVtab {
122395 sqlite3_vtab base; /* Base class. Must be first */
122396 sqlite3 *db; /* The database connection to which it belongs */
122397 const PragmaName *pName; /* Name of the pragma */
122398 u8 nHidden; /* Number of hidden columns */
122399 u8 iHidden; /* Index of the first hidden column */
122400};
122401struct PragmaVtabCursor {
122402 sqlite3_vtab_cursor base; /* Base class. Must be first */
122403 sqlite3_stmt *pPragma; /* The pragma statement to run */
122404 sqlite_int64 iRowid; /* Current rowid */
122405 char *azArg[2]; /* Value of the argument and schema */
122406};
122407
122408/*
122409** Pragma virtual table module xConnect method.
122410*/
122411static int pragmaVtabConnect(
122412 sqlite3 *db,
122413 void *pAux,
122414 int argc, const char *const*argv,
122415 sqlite3_vtab **ppVtab,
122416 char **pzErr
122417){
122418 const PragmaName *pPragma = (const PragmaName*)pAux;
122419 PragmaVtab *pTab = 0;
122420 int rc;
122421 int i, j;
122422 char cSep = '(';
122423 StrAccum acc;
122424 char zBuf[200];
122425
122426 UNUSED_PARAMETER(argc);
122427 UNUSED_PARAMETER(argv);
122428 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
122429 sqlite3_str_appendall(&acc, "CREATE TABLE x");
122430 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
122431 sqlite3_str_appendf(&acc, "%c\"%s\"", cSep, pragCName[j]);
122432 cSep = ',';
122433 }
122434 if( i==0 ){
122435 sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
122436 i++;
122437 }
122438 j = 0;
122439 if( pPragma->mPragFlg & PragFlg_Result1 ){
122440 sqlite3_str_appendall(&acc, ",arg HIDDEN");
122441 j++;
122442 }
122443 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
122444 sqlite3_str_appendall(&acc, ",schema HIDDEN");
122445 j++;
122446 }
122447 sqlite3_str_append(&acc, ")", 1);
122448 sqlite3StrAccumFinish(&acc);
122449 assert( strlen(zBuf) < sizeof(zBuf)-1 );
122450 rc = sqlite3_declare_vtab(db, zBuf);
122451 if( rc==SQLITE_OK ){
122452 pTab = (PragmaVtab*)sqlite3_malloc(sizeof(PragmaVtab));
122453 if( pTab==0 ){
122454 rc = SQLITE_NOMEM;
122455 }else{
122456 memset(pTab, 0, sizeof(PragmaVtab));
122457 pTab->pName = pPragma;
122458 pTab->db = db;
122459 pTab->iHidden = i;
122460 pTab->nHidden = j;
122461 }
122462 }else{
122463 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
122464 }
122465
122466 *ppVtab = (sqlite3_vtab*)pTab;
122467 return rc;
122468}
122469
122470/*
122471** Pragma virtual table module xDisconnect method.
122472*/
122473static int pragmaVtabDisconnect(sqlite3_vtab *pVtab){
122474 PragmaVtab *pTab = (PragmaVtab*)pVtab;
122475 sqlite3_free(pTab);
122476 return SQLITE_OK;
122477}
122478
122479/* Figure out the best index to use to search a pragma virtual table.
122480**
122481** There are not really any index choices. But we want to encourage the
122482** query planner to give == constraints on as many hidden parameters as
122483** possible, and especially on the first hidden parameter. So return a
122484** high cost if hidden parameters are unconstrained.
122485*/
122486static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
122487 PragmaVtab *pTab = (PragmaVtab*)tab;
122488 const struct sqlite3_index_constraint *pConstraint;
122489 int i, j;
122490 int seen[2];
122491
122492 pIdxInfo->estimatedCost = (double)1;
122493 if( pTab->nHidden==0 ){ return SQLITE_OK; }
122494 pConstraint = pIdxInfo->aConstraint;
122495 seen[0] = 0;
122496 seen[1] = 0;
122497 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
122498 if( pConstraint->usable==0 ) continue;
122499 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
122500 if( pConstraint->iColumn < pTab->iHidden ) continue;
122501 j = pConstraint->iColumn - pTab->iHidden;
122502 assert( j < 2 );
122503 seen[j] = i+1;
122504 }
122505 if( seen[0]==0 ){
122506 pIdxInfo->estimatedCost = (double)2147483647;
122507 pIdxInfo->estimatedRows = 2147483647;
122508 return SQLITE_OK;
122509 }
122510 j = seen[0]-1;
122511 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
122512 pIdxInfo->aConstraintUsage[j].omit = 1;
122513 if( seen[1]==0 ) return SQLITE_OK;
122514 pIdxInfo->estimatedCost = (double)20;
122515 pIdxInfo->estimatedRows = 20;
122516 j = seen[1]-1;
122517 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
122518 pIdxInfo->aConstraintUsage[j].omit = 1;
122519 return SQLITE_OK;
122520}
122521
122522/* Create a new cursor for the pragma virtual table */
122523static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){
122524 PragmaVtabCursor *pCsr;
122525 pCsr = (PragmaVtabCursor*)sqlite3_malloc(sizeof(*pCsr));
122526 if( pCsr==0 ) return SQLITE_NOMEM;
122527 memset(pCsr, 0, sizeof(PragmaVtabCursor));
122528 pCsr->base.pVtab = pVtab;
122529 *ppCursor = &pCsr->base;
122530 return SQLITE_OK;
122531}
122532
122533/* Clear all content from pragma virtual table cursor. */
122534static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){
122535 int i;
122536 sqlite3_finalize(pCsr->pPragma);
122537 pCsr->pPragma = 0;
122538 for(i=0; i<ArraySize(pCsr->azArg); i++){
122539 sqlite3_free(pCsr->azArg[i]);
122540 pCsr->azArg[i] = 0;
122541 }
122542}
122543
122544/* Close a pragma virtual table cursor */
122545static int pragmaVtabClose(sqlite3_vtab_cursor *cur){
122546 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)cur;
122547 pragmaVtabCursorClear(pCsr);
122548 sqlite3_free(pCsr);
122549 return SQLITE_OK;
122550}
122551
122552/* Advance the pragma virtual table cursor to the next row */
122553static int pragmaVtabNext(sqlite3_vtab_cursor *pVtabCursor){
122554 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
122555 int rc = SQLITE_OK;
122556
122557 /* Increment the xRowid value */
122558 pCsr->iRowid++;
122559 assert( pCsr->pPragma );
122560 if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
122561 rc = sqlite3_finalize(pCsr->pPragma);
122562 pCsr->pPragma = 0;
122563 pragmaVtabCursorClear(pCsr);
122564 }
122565 return rc;
122566}
122567
122568/*
122569** Pragma virtual table module xFilter method.
122570*/
122571static int pragmaVtabFilter(
122572 sqlite3_vtab_cursor *pVtabCursor,
122573 int idxNum, const char *idxStr,
122574 int argc, sqlite3_value **argv
122575){
122576 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
122577 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
122578 int rc;
122579 int i, j;
122580 StrAccum acc;
122581 char *zSql;
122582
122583 UNUSED_PARAMETER(idxNum);
122584 UNUSED_PARAMETER(idxStr);
122585 pragmaVtabCursorClear(pCsr);
122586 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
122587 for(i=0; i<argc; i++, j++){
122588 const char *zText = (const char*)sqlite3_value_text(argv[i]);
122589 assert( j<ArraySize(pCsr->azArg) );
122590 assert( pCsr->azArg[j]==0 );
122591 if( zText ){
122592 pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
122593 if( pCsr->azArg[j]==0 ){
122594 return SQLITE_NOMEM;
122595 }
122596 }
122597 }
122598 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
122599 sqlite3_str_appendall(&acc, "PRAGMA ");
122600 if( pCsr->azArg[1] ){
122601 sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
122602 }
122603 sqlite3_str_appendall(&acc, pTab->pName->zName);
122604 if( pCsr->azArg[0] ){
122605 sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
122606 }
122607 zSql = sqlite3StrAccumFinish(&acc);
122608 if( zSql==0 ) return SQLITE_NOMEM;
122609 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
122610 sqlite3_free(zSql);
122611 if( rc!=SQLITE_OK ){
122612 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
122613 return rc;
122614 }
122615 return pragmaVtabNext(pVtabCursor);
122616}
122617
122618/*
122619** Pragma virtual table module xEof method.
122620*/
122621static int pragmaVtabEof(sqlite3_vtab_cursor *pVtabCursor){
122622 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
122623 return (pCsr->pPragma==0);
122624}
122625
122626/* The xColumn method simply returns the corresponding column from
122627** the PRAGMA.
122628*/
122629static int pragmaVtabColumn(
122630 sqlite3_vtab_cursor *pVtabCursor,
122631 sqlite3_context *ctx,
122632 int i
122633){
122634 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
122635 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
122636 if( i<pTab->iHidden ){
122637 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
122638 }else{
122639 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
122640 }
122641 return SQLITE_OK;
122642}
122643
122644/*
122645** Pragma virtual table module xRowid method.
122646*/
122647static int pragmaVtabRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *p){
122648 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
122649 *p = pCsr->iRowid;
122650 return SQLITE_OK;
122651}
122652
122653/* The pragma virtual table object */
122654static const sqlite3_module pragmaVtabModule = {
122655 0, /* iVersion */
122656 0, /* xCreate - create a table */
122657 pragmaVtabConnect, /* xConnect - connect to an existing table */
122658 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
122659 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
122660 0, /* xDestroy - Drop a table */
122661 pragmaVtabOpen, /* xOpen - open a cursor */
122662 pragmaVtabClose, /* xClose - close a cursor */
122663 pragmaVtabFilter, /* xFilter - configure scan constraints */
122664 pragmaVtabNext, /* xNext - advance a cursor */
122665 pragmaVtabEof, /* xEof */
122666 pragmaVtabColumn, /* xColumn - read data */
122667 pragmaVtabRowid, /* xRowid - read data */
122668 0, /* xUpdate - write data */
122669 0, /* xBegin - begin transaction */
122670 0, /* xSync - sync transaction */
122671 0, /* xCommit - commit transaction */
122672 0, /* xRollback - rollback transaction */
122673 0, /* xFindFunction - function overloading */
122674 0, /* xRename - rename the table */
122675 0, /* xSavepoint */
122676 0, /* xRelease */
122677 0, /* xRollbackTo */
122678 0 /* xShadowName */
122679};
122680
122681/*
122682** Check to see if zTabName is really the name of a pragma. If it is,
122683** then register an eponymous virtual table for that pragma and return
122684** a pointer to the Module object for the new virtual table.
122685*/
122686SQLITE_PRIVATE Module *sqlite3PragmaVtabRegister(sqlite3 *db, const char *zName){
122687 const PragmaName *pName;
122688 assert( sqlite3_strnicmp(zName, "pragma_", 7)==0 );
122689 pName = pragmaLocate(zName+7);
122690 if( pName==0 ) return 0;
122691 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
122692 assert( sqlite3HashFind(&db->aModule, zName)==0 );
122693 return sqlite3VtabCreateModule(db, zName, &pragmaVtabModule, (void*)pName, 0);
122694}
122695
122696#endif /* SQLITE_OMIT_VIRTUALTABLE */
122697
122698#endif /* SQLITE_OMIT_PRAGMA */
122699
122700/************** End of pragma.c **********************************************/
122701/************** Begin file prepare.c *****************************************/
122702/*
122703** 2005 May 25
122704**
122705** The author disclaims copyright to this source code. In place of
122706** a legal notice, here is a blessing:
122707**
122708** May you do good and not evil.
122709** May you find forgiveness for yourself and forgive others.
122710** May you share freely, never taking more than you give.
122711**
122712*************************************************************************
122713** This file contains the implementation of the sqlite3_prepare()
122714** interface, and routines that contribute to loading the database schema
122715** from disk.
122716*/
122717/* #include "sqliteInt.h" */
122718
122719/*
122720** Fill the InitData structure with an error message that indicates
122721** that the database is corrupt.
122722*/
122723static void corruptSchema(
122724 InitData *pData, /* Initialization context */
122725 const char *zObj, /* Object being parsed at the point of error */
122726 const char *zExtra /* Error information */
122727){
122728 sqlite3 *db = pData->db;
122729 if( db->mallocFailed ){
122730 pData->rc = SQLITE_NOMEM_BKPT;
122731 }else if( pData->pzErrMsg[0]!=0 ){
122732 /* A error message has already been generated. Do not overwrite it */
122733 }else if( pData->mInitFlags & INITFLAG_AlterTable ){
122734 *pData->pzErrMsg = sqlite3DbStrDup(db, zExtra);
122735 pData->rc = SQLITE_ERROR;
122736 }else if( db->flags & SQLITE_WriteSchema ){
122737 pData->rc = SQLITE_CORRUPT_BKPT;
122738 }else{
122739 char *z;
122740 if( zObj==0 ) zObj = "?";
122741 z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj);
122742 if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
122743 *pData->pzErrMsg = z;
122744 pData->rc = SQLITE_CORRUPT_BKPT;
122745 }
122746}
122747
122748/*
122749** This is the callback routine for the code that initializes the
122750** database. See sqlite3Init() below for additional information.
122751** This routine is also called from the OP_ParseSchema opcode of the VDBE.
122752**
122753** Each callback contains the following information:
122754**
122755** argv[0] = name of thing being created
122756** argv[1] = root page number for table or index. 0 for trigger or view.
122757** argv[2] = SQL text for the CREATE statement.
122758**
122759*/
122760SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
122761 InitData *pData = (InitData*)pInit;
122762 sqlite3 *db = pData->db;
122763 int iDb = pData->iDb;
122764
122765 assert( argc==3 );
122766 UNUSED_PARAMETER2(NotUsed, argc);
122767 assert( sqlite3_mutex_held(db->mutex) );
122768 DbClearProperty(db, iDb, DB_Empty);
122769 if( db->mallocFailed ){
122770 corruptSchema(pData, argv[0], 0);
122771 return 1;
122772 }
122773
122774 assert( iDb>=0 && iDb<db->nDb );
122775 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
122776 if( argv[1]==0 ){
122777 corruptSchema(pData, argv[0], 0);
122778 }else if( sqlite3_strnicmp(argv[2],"create ",7)==0 ){
122779 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
122780 ** But because db->init.busy is set to 1, no VDBE code is generated
122781 ** or executed. All the parser does is build the internal data
122782 ** structures that describe the table, index, or view.
122783 */
122784 int rc;
122785 u8 saved_iDb = db->init.iDb;
122786 sqlite3_stmt *pStmt;
122787 TESTONLY(int rcp); /* Return code from sqlite3_prepare() */
122788
122789 assert( db->init.busy );
122790 db->init.iDb = iDb;
122791 db->init.newTnum = sqlite3Atoi(argv[1]);
122792 db->init.orphanTrigger = 0;
122793 TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
122794 rc = db->errCode;
122795 assert( (rc&0xFF)==(rcp&0xFF) );
122796 db->init.iDb = saved_iDb;
122797 /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
122798 if( SQLITE_OK!=rc ){
122799 if( db->init.orphanTrigger ){
122800 assert( iDb==1 );
122801 }else{
122802 pData->rc = rc;
122803 if( rc==SQLITE_NOMEM ){
122804 sqlite3OomFault(db);
122805 }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
122806 corruptSchema(pData, argv[0], sqlite3_errmsg(db));
122807 }
122808 }
122809 }
122810 sqlite3_finalize(pStmt);
122811 }else if( argv[0]==0 || (argv[2]!=0 && argv[2][0]!=0) ){
122812 corruptSchema(pData, argv[0], 0);
122813 }else{
122814 /* If the SQL column is blank it means this is an index that
122815 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
122816 ** constraint for a CREATE TABLE. The index should have already
122817 ** been created when we processed the CREATE TABLE. All we have
122818 ** to do here is record the root page number for that index.
122819 */
122820 Index *pIndex;
122821 pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zDbSName);
122822 if( pIndex==0 ){
122823 /* This can occur if there exists an index on a TEMP table which
122824 ** has the same name as another index on a permanent index. Since
122825 ** the permanent table is hidden by the TEMP table, we can also
122826 ** safely ignore the index on the permanent table.
122827 */
122828 /* Do Nothing */;
122829 }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){
122830 corruptSchema(pData, argv[0], "invalid rootpage");
122831 }
122832 }
122833 return 0;
122834}
122835
122836/*
122837** Attempt to read the database schema and initialize internal
122838** data structures for a single database file. The index of the
122839** database file is given by iDb. iDb==0 is used for the main
122840** database. iDb==1 should never be used. iDb>=2 is used for
122841** auxiliary databases. Return one of the SQLITE_ error codes to
122842** indicate success or failure.
122843*/
122844SQLITE_PRIVATE int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg, u32 mFlags){
122845 int rc;
122846 int i;
122847#ifndef SQLITE_OMIT_DEPRECATED
122848 int size;
122849#endif
122850 Db *pDb;
122851 char const *azArg[4];
122852 int meta[5];
122853 InitData initData;
122854 const char *zMasterName;
122855 int openedTransaction = 0;
122856
122857 assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
122858 assert( iDb>=0 && iDb<db->nDb );
122859 assert( db->aDb[iDb].pSchema );
122860 assert( sqlite3_mutex_held(db->mutex) );
122861 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
122862
122863 db->init.busy = 1;
122864
122865 /* Construct the in-memory representation schema tables (sqlite_master or
122866 ** sqlite_temp_master) by invoking the parser directly. The appropriate
122867 ** table name will be inserted automatically by the parser so we can just
122868 ** use the abbreviation "x" here. The parser will also automatically tag
122869 ** the schema table as read-only. */
122870 azArg[0] = zMasterName = SCHEMA_TABLE(iDb);
122871 azArg[1] = "1";
122872 azArg[2] = "CREATE TABLE x(type text,name text,tbl_name text,"
122873 "rootpage int,sql text)";
122874 azArg[3] = 0;
122875 initData.db = db;
122876 initData.iDb = iDb;
122877 initData.rc = SQLITE_OK;
122878 initData.pzErrMsg = pzErrMsg;
122879 initData.mInitFlags = mFlags;
122880 sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
122881 if( initData.rc ){
122882 rc = initData.rc;
122883 goto error_out;
122884 }
122885
122886 /* Create a cursor to hold the database open
122887 */
122888 pDb = &db->aDb[iDb];
122889 if( pDb->pBt==0 ){
122890 assert( iDb==1 );
122891 DbSetProperty(db, 1, DB_SchemaLoaded);
122892 rc = SQLITE_OK;
122893 goto error_out;
122894 }
122895
122896 /* If there is not already a read-only (or read-write) transaction opened
122897 ** on the b-tree database, open one now. If a transaction is opened, it
122898 ** will be closed before this function returns. */
122899 sqlite3BtreeEnter(pDb->pBt);
122900 if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
122901 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0);
122902 if( rc!=SQLITE_OK ){
122903 sqlite3SetString(pzErrMsg, db, sqlite3ErrStr(rc));
122904 goto initone_error_out;
122905 }
122906 openedTransaction = 1;
122907 }
122908
122909 /* Get the database meta information.
122910 **
122911 ** Meta values are as follows:
122912 ** meta[0] Schema cookie. Changes with each schema change.
122913 ** meta[1] File format of schema layer.
122914 ** meta[2] Size of the page cache.
122915 ** meta[3] Largest rootpage (auto/incr_vacuum mode)
122916 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
122917 ** meta[5] User version
122918 ** meta[6] Incremental vacuum mode
122919 ** meta[7] unused
122920 ** meta[8] unused
122921 ** meta[9] unused
122922 **
122923 ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
122924 ** the possible values of meta[4].
122925 */
122926 for(i=0; i<ArraySize(meta); i++){
122927 sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
122928 }
122929 if( (db->flags & SQLITE_ResetDatabase)!=0 ){
122930 memset(meta, 0, sizeof(meta));
122931 }
122932 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
122933
122934 /* If opening a non-empty database, check the text encoding. For the
122935 ** main database, set sqlite3.enc to the encoding of the main database.
122936 ** For an attached db, it is an error if the encoding is not the same
122937 ** as sqlite3.enc.
122938 */
122939 if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
122940 if( iDb==0 ){
122941#ifndef SQLITE_OMIT_UTF16
122942 u8 encoding;
122943 /* If opening the main database, set ENC(db). */
122944 encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
122945 if( encoding==0 ) encoding = SQLITE_UTF8;
122946 ENC(db) = encoding;
122947#else
122948 ENC(db) = SQLITE_UTF8;
122949#endif
122950 }else{
122951 /* If opening an attached database, the encoding much match ENC(db) */
122952 if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){
122953 sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
122954 " text encoding as main database");
122955 rc = SQLITE_ERROR;
122956 goto initone_error_out;
122957 }
122958 }
122959 }else{
122960 DbSetProperty(db, iDb, DB_Empty);
122961 }
122962 pDb->pSchema->enc = ENC(db);
122963
122964 if( pDb->pSchema->cache_size==0 ){
122965#ifndef SQLITE_OMIT_DEPRECATED
122966 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
122967 if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
122968 pDb->pSchema->cache_size = size;
122969#else
122970 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
122971#endif
122972 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
122973 }
122974
122975 /*
122976 ** file_format==1 Version 3.0.0.
122977 ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN
122978 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
122979 ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants
122980 */
122981 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
122982 if( pDb->pSchema->file_format==0 ){
122983 pDb->pSchema->file_format = 1;
122984 }
122985 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
122986 sqlite3SetString(pzErrMsg, db, "unsupported file format");
122987 rc = SQLITE_ERROR;
122988 goto initone_error_out;
122989 }
122990
122991 /* Ticket #2804: When we open a database in the newer file format,
122992 ** clear the legacy_file_format pragma flag so that a VACUUM will
122993 ** not downgrade the database and thus invalidate any descending
122994 ** indices that the user might have created.
122995 */
122996 if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
122997 db->flags &= ~SQLITE_LegacyFileFmt;
122998 }
122999
123000 /* Read the schema information out of the schema tables
123001 */
123002 assert( db->init.busy );
123003 {
123004 char *zSql;
123005 zSql = sqlite3MPrintf(db,
123006 "SELECT name, rootpage, sql FROM \"%w\".%s ORDER BY rowid",
123007 db->aDb[iDb].zDbSName, zMasterName);
123008#ifndef SQLITE_OMIT_AUTHORIZATION
123009 {
123010 sqlite3_xauth xAuth;
123011 xAuth = db->xAuth;
123012 db->xAuth = 0;
123013#endif
123014 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
123015#ifndef SQLITE_OMIT_AUTHORIZATION
123016 db->xAuth = xAuth;
123017 }
123018#endif
123019 if( rc==SQLITE_OK ) rc = initData.rc;
123020 sqlite3DbFree(db, zSql);
123021#ifndef SQLITE_OMIT_ANALYZE
123022 if( rc==SQLITE_OK ){
123023 sqlite3AnalysisLoad(db, iDb);
123024 }
123025#endif
123026 }
123027 if( db->mallocFailed ){
123028 rc = SQLITE_NOMEM_BKPT;
123029 sqlite3ResetAllSchemasOfConnection(db);
123030 }
123031 if( rc==SQLITE_OK || (db->flags&SQLITE_NoSchemaError)){
123032 /* Black magic: If the SQLITE_NoSchemaError flag is set, then consider
123033 ** the schema loaded, even if errors occurred. In this situation the
123034 ** current sqlite3_prepare() operation will fail, but the following one
123035 ** will attempt to compile the supplied statement against whatever subset
123036 ** of the schema was loaded before the error occurred. The primary
123037 ** purpose of this is to allow access to the sqlite_master table
123038 ** even when its contents have been corrupted.
123039 */
123040 DbSetProperty(db, iDb, DB_SchemaLoaded);
123041 rc = SQLITE_OK;
123042 }
123043
123044 /* Jump here for an error that occurs after successfully allocating
123045 ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
123046 ** before that point, jump to error_out.
123047 */
123048initone_error_out:
123049 if( openedTransaction ){
123050 sqlite3BtreeCommit(pDb->pBt);
123051 }
123052 sqlite3BtreeLeave(pDb->pBt);
123053
123054error_out:
123055 if( rc ){
123056 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
123057 sqlite3OomFault(db);
123058 }
123059 sqlite3ResetOneSchema(db, iDb);
123060 }
123061 db->init.busy = 0;
123062 return rc;
123063}
123064
123065/*
123066** Initialize all database files - the main database file, the file
123067** used to store temporary tables, and any additional database files
123068** created using ATTACH statements. Return a success code. If an
123069** error occurs, write an error message into *pzErrMsg.
123070**
123071** After a database is initialized, the DB_SchemaLoaded bit is set
123072** bit is set in the flags field of the Db structure. If the database
123073** file was of zero-length, then the DB_Empty flag is also set.
123074*/
123075SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
123076 int i, rc;
123077 int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
123078
123079 assert( sqlite3_mutex_held(db->mutex) );
123080 assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
123081 assert( db->init.busy==0 );
123082 ENC(db) = SCHEMA_ENC(db);
123083 assert( db->nDb>0 );
123084 /* Do the main schema first */
123085 if( !DbHasProperty(db, 0, DB_SchemaLoaded) ){
123086 rc = sqlite3InitOne(db, 0, pzErrMsg, 0);
123087 if( rc ) return rc;
123088 }
123089 /* All other schemas after the main schema. The "temp" schema must be last */
123090 for(i=db->nDb-1; i>0; i--){
123091 assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) );
123092 if( !DbHasProperty(db, i, DB_SchemaLoaded) ){
123093 rc = sqlite3InitOne(db, i, pzErrMsg, 0);
123094 if( rc ) return rc;
123095 }
123096 }
123097 if( commit_internal ){
123098 sqlite3CommitInternalChanges(db);
123099 }
123100 return SQLITE_OK;
123101}
123102
123103/*
123104** This routine is a no-op if the database schema is already initialized.
123105** Otherwise, the schema is loaded. An error code is returned.
123106*/
123107SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){
123108 int rc = SQLITE_OK;
123109 sqlite3 *db = pParse->db;
123110 assert( sqlite3_mutex_held(db->mutex) );
123111 if( !db->init.busy ){
123112 rc = sqlite3Init(db, &pParse->zErrMsg);
123113 if( rc!=SQLITE_OK ){
123114 pParse->rc = rc;
123115 pParse->nErr++;
123116 }else if( db->noSharedCache ){
123117 db->mDbFlags |= DBFLAG_SchemaKnownOk;
123118 }
123119 }
123120 return rc;
123121}
123122
123123
123124/*
123125** Check schema cookies in all databases. If any cookie is out
123126** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies
123127** make no changes to pParse->rc.
123128*/
123129static void schemaIsValid(Parse *pParse){
123130 sqlite3 *db = pParse->db;
123131 int iDb;
123132 int rc;
123133 int cookie;
123134
123135 assert( pParse->checkSchema );
123136 assert( sqlite3_mutex_held(db->mutex) );
123137 for(iDb=0; iDb<db->nDb; iDb++){
123138 int openedTransaction = 0; /* True if a transaction is opened */
123139 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
123140 if( pBt==0 ) continue;
123141
123142 /* If there is not already a read-only (or read-write) transaction opened
123143 ** on the b-tree database, open one now. If a transaction is opened, it
123144 ** will be closed immediately after reading the meta-value. */
123145 if( !sqlite3BtreeIsInReadTrans(pBt) ){
123146 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
123147 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
123148 sqlite3OomFault(db);
123149 }
123150 if( rc!=SQLITE_OK ) return;
123151 openedTransaction = 1;
123152 }
123153
123154 /* Read the schema cookie from the database. If it does not match the
123155 ** value stored as part of the in-memory schema representation,
123156 ** set Parse.rc to SQLITE_SCHEMA. */
123157 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
123158 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
123159 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
123160 sqlite3ResetOneSchema(db, iDb);
123161 pParse->rc = SQLITE_SCHEMA;
123162 }
123163
123164 /* Close the transaction, if one was opened. */
123165 if( openedTransaction ){
123166 sqlite3BtreeCommit(pBt);
123167 }
123168 }
123169}
123170
123171/*
123172** Convert a schema pointer into the iDb index that indicates
123173** which database file in db->aDb[] the schema refers to.
123174**
123175** If the same database is attached more than once, the first
123176** attached database is returned.
123177*/
123178SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
123179 int i = -1000000;
123180
123181 /* If pSchema is NULL, then return -1000000. This happens when code in
123182 ** expr.c is trying to resolve a reference to a transient table (i.e. one
123183 ** created by a sub-select). In this case the return value of this
123184 ** function should never be used.
123185 **
123186 ** We return -1000000 instead of the more usual -1 simply because using
123187 ** -1000000 as the incorrect index into db->aDb[] is much
123188 ** more likely to cause a segfault than -1 (of course there are assert()
123189 ** statements too, but it never hurts to play the odds).
123190 */
123191 assert( sqlite3_mutex_held(db->mutex) );
123192 if( pSchema ){
123193 for(i=0; 1; i++){
123194 assert( i<db->nDb );
123195 if( db->aDb[i].pSchema==pSchema ){
123196 break;
123197 }
123198 }
123199 assert( i>=0 && i<db->nDb );
123200 }
123201 return i;
123202}
123203
123204/*
123205** Free all memory allocations in the pParse object
123206*/
123207SQLITE_PRIVATE void sqlite3ParserReset(Parse *pParse){
123208 sqlite3 *db = pParse->db;
123209 sqlite3DbFree(db, pParse->aLabel);
123210 sqlite3ExprListDelete(db, pParse->pConstExpr);
123211 if( db ){
123212 assert( db->lookaside.bDisable >= pParse->disableLookaside );
123213 db->lookaside.bDisable -= pParse->disableLookaside;
123214 }
123215 pParse->disableLookaside = 0;
123216}
123217
123218/*
123219** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
123220*/
123221static int sqlite3Prepare(
123222 sqlite3 *db, /* Database handle. */
123223 const char *zSql, /* UTF-8 encoded SQL statement. */
123224 int nBytes, /* Length of zSql in bytes. */
123225 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
123226 Vdbe *pReprepare, /* VM being reprepared */
123227 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
123228 const char **pzTail /* OUT: End of parsed string */
123229){
123230 char *zErrMsg = 0; /* Error message */
123231 int rc = SQLITE_OK; /* Result code */
123232 int i; /* Loop counter */
123233 Parse sParse; /* Parsing context */
123234
123235 memset(&sParse, 0, PARSE_HDR_SZ);
123236 memset(PARSE_TAIL(&sParse), 0, PARSE_TAIL_SZ);
123237 sParse.pReprepare = pReprepare;
123238 assert( ppStmt && *ppStmt==0 );
123239 /* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */
123240 assert( sqlite3_mutex_held(db->mutex) );
123241
123242 /* For a long-term use prepared statement avoid the use of
123243 ** lookaside memory.
123244 */
123245 if( prepFlags & SQLITE_PREPARE_PERSISTENT ){
123246 sParse.disableLookaside++;
123247 db->lookaside.bDisable++;
123248 }
123249
123250 /* Check to verify that it is possible to get a read lock on all
123251 ** database schemas. The inability to get a read lock indicates that
123252 ** some other database connection is holding a write-lock, which in
123253 ** turn means that the other connection has made uncommitted changes
123254 ** to the schema.
123255 **
123256 ** Were we to proceed and prepare the statement against the uncommitted
123257 ** schema changes and if those schema changes are subsequently rolled
123258 ** back and different changes are made in their place, then when this
123259 ** prepared statement goes to run the schema cookie would fail to detect
123260 ** the schema change. Disaster would follow.
123261 **
123262 ** This thread is currently holding mutexes on all Btrees (because
123263 ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
123264 ** is not possible for another thread to start a new schema change
123265 ** while this routine is running. Hence, we do not need to hold
123266 ** locks on the schema, we just need to make sure nobody else is
123267 ** holding them.
123268 **
123269 ** Note that setting READ_UNCOMMITTED overrides most lock detection,
123270 ** but it does *not* override schema lock detection, so this all still
123271 ** works even if READ_UNCOMMITTED is set.
123272 */
123273 for(i=0; i<db->nDb; i++) {
123274 Btree *pBt = db->aDb[i].pBt;
123275 if( pBt ){
123276 assert( sqlite3BtreeHoldsMutex(pBt) );
123277 rc = sqlite3BtreeSchemaLocked(pBt);
123278 if( rc ){
123279 const char *zDb = db->aDb[i].zDbSName;
123280 sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb);
123281 testcase( db->flags & SQLITE_ReadUncommit );
123282 goto end_prepare;
123283 }
123284 }
123285 }
123286
123287 sqlite3VtabUnlockList(db);
123288
123289 sParse.db = db;
123290 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
123291 char *zSqlCopy;
123292 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
123293 testcase( nBytes==mxLen );
123294 testcase( nBytes==mxLen+1 );
123295 if( nBytes>mxLen ){
123296 sqlite3ErrorWithMsg(db, SQLITE_TOOBIG, "statement too long");
123297 rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
123298 goto end_prepare;
123299 }
123300 zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
123301 if( zSqlCopy ){
123302 sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
123303 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
123304 sqlite3DbFree(db, zSqlCopy);
123305 }else{
123306 sParse.zTail = &zSql[nBytes];
123307 }
123308 }else{
123309 sqlite3RunParser(&sParse, zSql, &zErrMsg);
123310 }
123311 assert( 0==sParse.nQueryLoop );
123312
123313 if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
123314 if( sParse.checkSchema ){
123315 schemaIsValid(&sParse);
123316 }
123317 if( db->mallocFailed ){
123318 sParse.rc = SQLITE_NOMEM_BKPT;
123319 }
123320 if( pzTail ){
123321 *pzTail = sParse.zTail;
123322 }
123323 rc = sParse.rc;
123324
123325#ifndef SQLITE_OMIT_EXPLAIN
123326 if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
123327 static const char * const azColName[] = {
123328 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
123329 "id", "parent", "notused", "detail"
123330 };
123331 int iFirst, mx;
123332 if( sParse.explain==2 ){
123333 sqlite3VdbeSetNumCols(sParse.pVdbe, 4);
123334 iFirst = 8;
123335 mx = 12;
123336 }else{
123337 sqlite3VdbeSetNumCols(sParse.pVdbe, 8);
123338 iFirst = 0;
123339 mx = 8;
123340 }
123341 for(i=iFirst; i<mx; i++){
123342 sqlite3VdbeSetColName(sParse.pVdbe, i-iFirst, COLNAME_NAME,
123343 azColName[i], SQLITE_STATIC);
123344 }
123345 }
123346#endif
123347
123348 if( db->init.busy==0 ){
123349 sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
123350 }
123351 if( sParse.pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
123352 sqlite3VdbeFinalize(sParse.pVdbe);
123353 assert(!(*ppStmt));
123354 }else{
123355 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
123356 }
123357
123358 if( zErrMsg ){
123359 sqlite3ErrorWithMsg(db, rc, "%s", zErrMsg);
123360 sqlite3DbFree(db, zErrMsg);
123361 }else{
123362 sqlite3Error(db, rc);
123363 }
123364
123365 /* Delete any TriggerPrg structures allocated while parsing this statement. */
123366 while( sParse.pTriggerPrg ){
123367 TriggerPrg *pT = sParse.pTriggerPrg;
123368 sParse.pTriggerPrg = pT->pNext;
123369 sqlite3DbFree(db, pT);
123370 }
123371
123372end_prepare:
123373
123374 sqlite3ParserReset(&sParse);
123375 return rc;
123376}
123377static int sqlite3LockAndPrepare(
123378 sqlite3 *db, /* Database handle. */
123379 const char *zSql, /* UTF-8 encoded SQL statement. */
123380 int nBytes, /* Length of zSql in bytes. */
123381 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
123382 Vdbe *pOld, /* VM being reprepared */
123383 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
123384 const char **pzTail /* OUT: End of parsed string */
123385){
123386 int rc;
123387 int cnt = 0;
123388
123389#ifdef SQLITE_ENABLE_API_ARMOR
123390 if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
123391#endif
123392 *ppStmt = 0;
123393 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
123394 return SQLITE_MISUSE_BKPT;
123395 }
123396 sqlite3_mutex_enter(db->mutex);
123397 sqlite3BtreeEnterAll(db);
123398 do{
123399 /* Make multiple attempts to compile the SQL, until it either succeeds
123400 ** or encounters a permanent error. A schema problem after one schema
123401 ** reset is considered a permanent error. */
123402 rc = sqlite3Prepare(db, zSql, nBytes, prepFlags, pOld, ppStmt, pzTail);
123403 assert( rc==SQLITE_OK || *ppStmt==0 );
123404 }while( rc==SQLITE_ERROR_RETRY
123405 || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,-1), cnt++)==0) );
123406 sqlite3BtreeLeaveAll(db);
123407 rc = sqlite3ApiExit(db, rc);
123408 assert( (rc&db->errMask)==rc );
123409 sqlite3_mutex_leave(db->mutex);
123410 return rc;
123411}
123412
123413#ifdef SQLITE_ENABLE_NORMALIZE
123414/*
123415** Checks if the specified token is a table, column, or function name,
123416** based on the databases associated with the statement being prepared.
123417** If the function fails, zero is returned and pRc is filled with the
123418** error code.
123419*/
123420static int shouldTreatAsIdentifier(
123421 sqlite3 *db, /* Database handle. */
123422 const char *zToken, /* Pointer to start of token to be checked */
123423 int nToken, /* Length of token to be checked */
123424 int *pRc /* Pointer to error code upon failure */
123425){
123426 int bFound = 0; /* Non-zero if token is an identifier name. */
123427 int i, j; /* Database and column loop indexes. */
123428 Schema *pSchema; /* Schema for current database. */
123429 Hash *pHash; /* Hash table of tables for current database. */
123430 HashElem *e; /* Hash element for hash table iteration. */
123431 Table *pTab; /* Database table for columns being checked. */
123432
123433 if( sqlite3IsRowidN(zToken, nToken) ){
123434 return 1;
123435 }
123436 if( nToken>0 ){
123437 int hash = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zToken[0]], nToken);
123438 if( sqlite3FunctionSearchN(hash, zToken, nToken) ) return 1;
123439 }
123440 assert( db!=0 );
123441 sqlite3_mutex_enter(db->mutex);
123442 sqlite3BtreeEnterAll(db);
123443 for(i=0; i<db->nDb; i++){
123444 pHash = &db->aFunc;
123445 if( sqlite3HashFindN(pHash, zToken, nToken) ){
123446 bFound = 1;
123447 break;
123448 }
123449 pSchema = db->aDb[i].pSchema;
123450 if( pSchema==0 ) continue;
123451 pHash = &pSchema->tblHash;
123452 if( sqlite3HashFindN(pHash, zToken, nToken) ){
123453 bFound = 1;
123454 break;
123455 }
123456 for(e=sqliteHashFirst(pHash); e; e=sqliteHashNext(e)){
123457 pTab = sqliteHashData(e);
123458 if( pTab==0 ) continue;
123459 pHash = pTab->pColHash;
123460 if( pHash==0 ){
123461 pTab->pColHash = pHash = sqlite3_malloc(sizeof(Hash));
123462 if( pHash ){
123463 sqlite3HashInit(pHash);
123464 for(j=0; j<pTab->nCol; j++){
123465 Column *pCol = &pTab->aCol[j];
123466 sqlite3HashInsert(pHash, pCol->zName, pCol);
123467 }
123468 }else{
123469 *pRc = SQLITE_NOMEM_BKPT;
123470 bFound = 0;
123471 goto done;
123472 }
123473 }
123474 if( pHash && sqlite3HashFindN(pHash, zToken, nToken) ){
123475 bFound = 1;
123476 goto done;
123477 }
123478 }
123479 }
123480done:
123481 sqlite3BtreeLeaveAll(db);
123482 sqlite3_mutex_leave(db->mutex);
123483 return bFound;
123484}
123485
123486/*
123487** Attempt to estimate the final output buffer size needed for the fully
123488** normalized version of the specified SQL string. This should take into
123489** account any potential expansion that could occur (e.g. via IN clauses
123490** being expanded, etc). This size returned is the total number of bytes
123491** including the NUL terminator.
123492*/
123493static int estimateNormalizedSize(
123494 const char *zSql, /* The original SQL string */
123495 int nSql, /* Length of original SQL string */
123496 u8 prepFlags /* The flags passed to sqlite3_prepare_v3() */
123497){
123498 int nOut = nSql + 4;
123499 const char *z = zSql;
123500 while( nOut<nSql*5 ){
123501 while( z[0]!=0 && z[0]!='I' && z[0]!='i' ){ z++; }
123502 if( z[0]==0 ) break;
123503 z++;
123504 if( z[0]!='N' && z[0]!='n' ) break;
123505 z++;
123506 while( sqlite3Isspace(z[0]) ){ z++; }
123507 if( z[0]!='(' ) break;
123508 z++;
123509 nOut += 5; /* ?,?,? */
123510 }
123511 return nOut;
123512}
123513
123514/*
123515** Copy the current token into the output buffer while dealing with quoted
123516** identifiers. By default, all letters will be converted into lowercase.
123517** If the bUpper flag is set, uppercase will be used. The piOut argument
123518** will be used to update the target index into the output string.
123519*/
123520static void copyNormalizedToken(
123521 const char *zSql, /* The original SQL string */
123522 int iIn, /* Current index into the original SQL string */
123523 int nToken, /* Number of bytes in the current token */
123524 int tokenFlags, /* Flags returned by the tokenizer */
123525 char *zOut, /* The output string */
123526 int *piOut /* Pointer to target index into the output string */
123527){
123528 int bQuoted = tokenFlags & SQLITE_TOKEN_QUOTED;
123529 int bKeyword = tokenFlags & SQLITE_TOKEN_KEYWORD;
123530 int j = *piOut, k = 0;
123531 for(; k<nToken; k++){
123532 if( bQuoted ){
123533 if( k==0 && iIn>0 ){
123534 zOut[j++] = '"';
123535 continue;
123536 }else if( k==nToken-1 ){
123537 zOut[j++] = '"';
123538 continue;
123539 }
123540 }
123541 if( bKeyword ){
123542 zOut[j++] = sqlite3Toupper(zSql[iIn+k]);
123543 }else{
123544 zOut[j++] = sqlite3Tolower(zSql[iIn+k]);
123545 }
123546 }
123547 *piOut = j;
123548}
123549
123550/*
123551** Perform normalization of the SQL contained in the prepared statement and
123552** store the result in the zNormSql field. The schema for the associated
123553** databases are consulted while performing the normalization in order to
123554** determine if a token appears to be an identifier. All identifiers are
123555** left intact in the normalized SQL and all literals are replaced with a
123556** single '?'.
123557*/
123558SQLITE_PRIVATE void sqlite3Normalize(
123559 Vdbe *pVdbe, /* VM being reprepared */
123560 const char *zSql, /* The original SQL string */
123561 int nSql, /* Size of the input string in bytes */
123562 u8 prepFlags /* The flags passed to sqlite3_prepare_v3() */
123563){
123564 sqlite3 *db; /* Database handle. */
123565 char *z; /* The output string */
123566 int nZ; /* Size of the output string in bytes */
123567 int i; /* Next character to read from zSql[] */
123568 int j; /* Next character to fill in on z[] */
123569 int tokenType = 0; /* Type of the next token */
123570 int prevTokenType = 0; /* Type of the previous token, except spaces */
123571 int n; /* Size of the next token */
123572 int nParen = 0; /* Nesting level of parenthesis */
123573 Hash inHash; /* Table of parenthesis levels to output index. */
123574
123575 db = sqlite3VdbeDb(pVdbe);
123576 assert( db!=0 );
123577 assert( pVdbe->zNormSql==0 );
123578 if( zSql==0 ) return;
123579 nZ = estimateNormalizedSize(zSql, nSql, prepFlags);
123580 z = sqlite3DbMallocRawNN(db, nZ);
123581 if( z==0 ) return;
123582 sqlite3HashInit(&inHash);
123583 for(i=j=0; i<nSql && zSql[i]; i+=n){
123584 int flags = 0;
123585 if( tokenType!=TK_SPACE ) prevTokenType = tokenType;
123586 n = sqlite3GetTokenNormalized((unsigned char*)zSql+i, &tokenType, &flags);
123587 switch( tokenType ){
123588 case TK_SPACE: {
123589 break;
123590 }
123591 case TK_ILLEGAL: {
123592 sqlite3DbFree(db, z);
123593 sqlite3HashClear(&inHash);
123594 return;
123595 }
123596 case TK_STRING:
123597 case TK_INTEGER:
123598 case TK_FLOAT:
123599 case TK_VARIABLE:
123600 case TK_BLOB: {
123601 z[j++] = '?';
123602 break;
123603 }
123604 case TK_LP:
123605 case TK_RP: {
123606 if( tokenType==TK_LP ){
123607 nParen++;
123608 if( prevTokenType==TK_IN ){
123609 assert( nParen<nSql );
123610 sqlite3HashInsert(&inHash, zSql+nParen, SQLITE_INT_TO_PTR(j));
123611 }
123612 }else{
123613 int jj;
123614 assert( nParen<nSql );
123615 jj = SQLITE_PTR_TO_INT(sqlite3HashFind(&inHash, zSql+nParen));
123616 if( jj>0 ){
123617 sqlite3HashInsert(&inHash, zSql+nParen, 0);
123618 assert( jj+6<nZ );
123619 memcpy(z+jj+1, "?,?,?", 5);
123620 j = jj+6;
123621 assert( nZ-1-j>=0 );
123622 assert( nZ-1-j<nZ );
123623 memset(z+j, 0, nZ-1-j);
123624 }
123625 nParen--;
123626 }
123627 assert( nParen>=0 );
123628 /* Fall through */
123629 }
123630 case TK_MINUS:
123631 case TK_SEMI:
123632 case TK_PLUS:
123633 case TK_STAR:
123634 case TK_SLASH:
123635 case TK_REM:
123636 case TK_EQ:
123637 case TK_LE:
123638 case TK_NE:
123639 case TK_LSHIFT:
123640 case TK_LT:
123641 case TK_RSHIFT:
123642 case TK_GT:
123643 case TK_GE:
123644 case TK_BITOR:
123645 case TK_CONCAT:
123646 case TK_COMMA:
123647 case TK_BITAND:
123648 case TK_BITNOT:
123649 case TK_DOT:
123650 case TK_IN:
123651 case TK_IS:
123652 case TK_NOT:
123653 case TK_NULL:
123654 case TK_ID: {
123655 if( tokenType==TK_NULL ){
123656 if( prevTokenType==TK_IS || prevTokenType==TK_NOT ){
123657 /* NULL is a keyword in this case, not a literal value */
123658 }else{
123659 /* Here the NULL is a literal value */
123660 z[j++] = '?';
123661 break;
123662 }
123663 }
123664 if( j>0 && sqlite3IsIdChar(z[j-1]) && sqlite3IsIdChar(zSql[i]) ){
123665 z[j++] = ' ';
123666 }
123667 if( tokenType==TK_ID ){
123668 int i2 = i, n2 = n, rc = SQLITE_OK;
123669 if( nParen>0 ){
123670 assert( nParen<nSql );
123671 sqlite3HashInsert(&inHash, zSql+nParen, 0);
123672 }
123673 if( flags&SQLITE_TOKEN_QUOTED ){ i2++; n2-=2; }
123674 if( shouldTreatAsIdentifier(db, zSql+i2, n2, &rc)==0 ){
123675 if( rc!=SQLITE_OK ){
123676 sqlite3DbFree(db, z);
123677 sqlite3HashClear(&inHash);
123678 return;
123679 }
123680 if( sqlite3_keyword_check(zSql+i2, n2)==0 ){
123681 z[j++] = '?';
123682 break;
123683 }
123684 }
123685 }
123686 copyNormalizedToken(zSql, i, n, flags, z, &j);
123687 break;
123688 }
123689 }
123690 }
123691 assert( j<nZ && "one" );
123692 while( j>0 && z[j-1]==' ' ){ j--; }
123693 if( j>0 && z[j-1]!=';' ){ z[j++] = ';'; }
123694 z[j] = 0;
123695 assert( j<nZ && "two" );
123696 pVdbe->zNormSql = z;
123697 sqlite3HashClear(&inHash);
123698}
123699#endif /* SQLITE_ENABLE_NORMALIZE */
123700
123701/*
123702** Rerun the compilation of a statement after a schema change.
123703**
123704** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
123705** if the statement cannot be recompiled because another connection has
123706** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error
123707** occurs, return SQLITE_SCHEMA.
123708*/
123709SQLITE_PRIVATE int sqlite3Reprepare(Vdbe *p){
123710 int rc;
123711 sqlite3_stmt *pNew;
123712 const char *zSql;
123713 sqlite3 *db;
123714 u8 prepFlags;
123715
123716 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
123717 zSql = sqlite3_sql((sqlite3_stmt *)p);
123718 assert( zSql!=0 ); /* Reprepare only called for prepare_v2() statements */
123719 db = sqlite3VdbeDb(p);
123720 assert( sqlite3_mutex_held(db->mutex) );
123721 prepFlags = sqlite3VdbePrepareFlags(p);
123722 rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0);
123723 if( rc ){
123724 if( rc==SQLITE_NOMEM ){
123725 sqlite3OomFault(db);
123726 }
123727 assert( pNew==0 );
123728 return rc;
123729 }else{
123730 assert( pNew!=0 );
123731 }
123732 sqlite3VdbeSwap((Vdbe*)pNew, p);
123733 sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
123734 sqlite3VdbeResetStepResult((Vdbe*)pNew);
123735 sqlite3VdbeFinalize((Vdbe*)pNew);
123736 return SQLITE_OK;
123737}
123738
123739
123740/*
123741** Two versions of the official API. Legacy and new use. In the legacy
123742** version, the original SQL text is not saved in the prepared statement
123743** and so if a schema change occurs, SQLITE_SCHEMA is returned by
123744** sqlite3_step(). In the new version, the original SQL text is retained
123745** and the statement is automatically recompiled if an schema change
123746** occurs.
123747*/
123748SQLITE_API int sqlite3_prepare(
123749 sqlite3 *db, /* Database handle. */
123750 const char *zSql, /* UTF-8 encoded SQL statement. */
123751 int nBytes, /* Length of zSql in bytes. */
123752 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
123753 const char **pzTail /* OUT: End of parsed string */
123754){
123755 int rc;
123756 rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail);
123757 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
123758 return rc;
123759}
123760SQLITE_API int sqlite3_prepare_v2(
123761 sqlite3 *db, /* Database handle. */
123762 const char *zSql, /* UTF-8 encoded SQL statement. */
123763 int nBytes, /* Length of zSql in bytes. */
123764 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
123765 const char **pzTail /* OUT: End of parsed string */
123766){
123767 int rc;
123768 /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
123769 ** exactly the same as sqlite3_prepare_v3() with a zero prepFlags
123770 ** parameter.
123771 **
123772 ** Proof in that the 5th parameter to sqlite3LockAndPrepare is 0 */
123773 rc = sqlite3LockAndPrepare(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,0,
123774 ppStmt,pzTail);
123775 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );
123776 return rc;
123777}
123778SQLITE_API int sqlite3_prepare_v3(
123779 sqlite3 *db, /* Database handle. */
123780 const char *zSql, /* UTF-8 encoded SQL statement. */
123781 int nBytes, /* Length of zSql in bytes. */
123782 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
123783 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
123784 const char **pzTail /* OUT: End of parsed string */
123785){
123786 int rc;
123787 /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
123788 ** sqlite3_prepare_v2() only in having the extra prepFlags parameter,
123789 ** which is a bit array consisting of zero or more of the
123790 ** SQLITE_PREPARE_* flags.
123791 **
123792 ** Proof by comparison to the implementation of sqlite3_prepare_v2()
123793 ** directly above. */
123794 rc = sqlite3LockAndPrepare(db,zSql,nBytes,
123795 SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK),
123796 0,ppStmt,pzTail);
123797 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );
123798 return rc;
123799}
123800
123801
123802#ifndef SQLITE_OMIT_UTF16
123803/*
123804** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
123805*/
123806static int sqlite3Prepare16(
123807 sqlite3 *db, /* Database handle. */
123808 const void *zSql, /* UTF-16 encoded SQL statement. */
123809 int nBytes, /* Length of zSql in bytes. */
123810 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
123811 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
123812 const void **pzTail /* OUT: End of parsed string */
123813){
123814 /* This function currently works by first transforming the UTF-16
123815 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
123816 ** tricky bit is figuring out the pointer to return in *pzTail.
123817 */
123818 char *zSql8;
123819 const char *zTail8 = 0;
123820 int rc = SQLITE_OK;
123821
123822#ifdef SQLITE_ENABLE_API_ARMOR
123823 if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
123824#endif
123825 *ppStmt = 0;
123826 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
123827 return SQLITE_MISUSE_BKPT;
123828 }
123829 if( nBytes>=0 ){
123830 int sz;
123831 const char *z = (const char*)zSql;
123832 for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){}
123833 nBytes = sz;
123834 }
123835 sqlite3_mutex_enter(db->mutex);
123836 zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);
123837 if( zSql8 ){
123838 rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8);
123839 }
123840
123841 if( zTail8 && pzTail ){
123842 /* If sqlite3_prepare returns a tail pointer, we calculate the
123843 ** equivalent pointer into the UTF-16 string by counting the unicode
123844 ** characters between zSql8 and zTail8, and then returning a pointer
123845 ** the same number of characters into the UTF-16 string.
123846 */
123847 int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
123848 *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
123849 }
123850 sqlite3DbFree(db, zSql8);
123851 rc = sqlite3ApiExit(db, rc);
123852 sqlite3_mutex_leave(db->mutex);
123853 return rc;
123854}
123855
123856/*
123857** Two versions of the official API. Legacy and new use. In the legacy
123858** version, the original SQL text is not saved in the prepared statement
123859** and so if a schema change occurs, SQLITE_SCHEMA is returned by
123860** sqlite3_step(). In the new version, the original SQL text is retained
123861** and the statement is automatically recompiled if an schema change
123862** occurs.
123863*/
123864SQLITE_API int sqlite3_prepare16(
123865 sqlite3 *db, /* Database handle. */
123866 const void *zSql, /* UTF-16 encoded SQL statement. */
123867 int nBytes, /* Length of zSql in bytes. */
123868 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
123869 const void **pzTail /* OUT: End of parsed string */
123870){
123871 int rc;
123872 rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
123873 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
123874 return rc;
123875}
123876SQLITE_API int sqlite3_prepare16_v2(
123877 sqlite3 *db, /* Database handle. */
123878 const void *zSql, /* UTF-16 encoded SQL statement. */
123879 int nBytes, /* Length of zSql in bytes. */
123880 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
123881 const void **pzTail /* OUT: End of parsed string */
123882){
123883 int rc;
123884 rc = sqlite3Prepare16(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,ppStmt,pzTail);
123885 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
123886 return rc;
123887}
123888SQLITE_API int sqlite3_prepare16_v3(
123889 sqlite3 *db, /* Database handle. */
123890 const void *zSql, /* UTF-16 encoded SQL statement. */
123891 int nBytes, /* Length of zSql in bytes. */
123892 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
123893 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
123894 const void **pzTail /* OUT: End of parsed string */
123895){
123896 int rc;
123897 rc = sqlite3Prepare16(db,zSql,nBytes,
123898 SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK),
123899 ppStmt,pzTail);
123900 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
123901 return rc;
123902}
123903
123904#endif /* SQLITE_OMIT_UTF16 */
123905
123906/************** End of prepare.c *********************************************/
123907/************** Begin file select.c ******************************************/
123908/*
123909** 2001 September 15
123910**
123911** The author disclaims copyright to this source code. In place of
123912** a legal notice, here is a blessing:
123913**
123914** May you do good and not evil.
123915** May you find forgiveness for yourself and forgive others.
123916** May you share freely, never taking more than you give.
123917**
123918*************************************************************************
123919** This file contains C code routines that are called by the parser
123920** to handle SELECT statements in SQLite.
123921*/
123922/* #include "sqliteInt.h" */
123923
123924/*
123925** Trace output macros
123926*/
123927#if SELECTTRACE_ENABLED
123928/***/ int sqlite3SelectTrace = 0;
123929# define SELECTTRACE(K,P,S,X) \
123930 if(sqlite3SelectTrace&(K)) \
123931 sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
123932 sqlite3DebugPrintf X
123933#else
123934# define SELECTTRACE(K,P,S,X)
123935#endif
123936
123937
123938/*
123939** An instance of the following object is used to record information about
123940** how to process the DISTINCT keyword, to simplify passing that information
123941** into the selectInnerLoop() routine.
123942*/
123943typedef struct DistinctCtx DistinctCtx;
123944struct DistinctCtx {
123945 u8 isTnct; /* True if the DISTINCT keyword is present */
123946 u8 eTnctType; /* One of the WHERE_DISTINCT_* operators */
123947 int tabTnct; /* Ephemeral table used for DISTINCT processing */
123948 int addrTnct; /* Address of OP_OpenEphemeral opcode for tabTnct */
123949};
123950
123951/*
123952** An instance of the following object is used to record information about
123953** the ORDER BY (or GROUP BY) clause of query is being coded.
123954**
123955** The aDefer[] array is used by the sorter-references optimization. For
123956** example, assuming there is no index that can be used for the ORDER BY,
123957** for the query:
123958**
123959** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10;
123960**
123961** it may be more efficient to add just the "a" values to the sorter, and
123962** retrieve the associated "bigblob" values directly from table t1 as the
123963** 10 smallest "a" values are extracted from the sorter.
123964**
123965** When the sorter-reference optimization is used, there is one entry in the
123966** aDefer[] array for each database table that may be read as values are
123967** extracted from the sorter.
123968*/
123969typedef struct SortCtx SortCtx;
123970struct SortCtx {
123971 ExprList *pOrderBy; /* The ORDER BY (or GROUP BY clause) */
123972 int nOBSat; /* Number of ORDER BY terms satisfied by indices */
123973 int iECursor; /* Cursor number for the sorter */
123974 int regReturn; /* Register holding block-output return address */
123975 int labelBkOut; /* Start label for the block-output subroutine */
123976 int addrSortIndex; /* Address of the OP_SorterOpen or OP_OpenEphemeral */
123977 int labelDone; /* Jump here when done, ex: LIMIT reached */
123978 int labelOBLopt; /* Jump here when sorter is full */
123979 u8 sortFlags; /* Zero or more SORTFLAG_* bits */
123980#ifdef SQLITE_ENABLE_SORTER_REFERENCES
123981 u8 nDefer; /* Number of valid entries in aDefer[] */
123982 struct DeferredCsr {
123983 Table *pTab; /* Table definition */
123984 int iCsr; /* Cursor number for table */
123985 int nKey; /* Number of PK columns for table pTab (>=1) */
123986 } aDefer[4];
123987#endif
123988 struct RowLoadInfo *pDeferredRowLoad; /* Deferred row loading info or NULL */
123989};
123990#define SORTFLAG_UseSorter 0x01 /* Use SorterOpen instead of OpenEphemeral */
123991
123992/*
123993** Delete all the content of a Select structure. Deallocate the structure
123994** itself only if bFree is true.
123995*/
123996static void clearSelect(sqlite3 *db, Select *p, int bFree){
123997 while( p ){
123998 Select *pPrior = p->pPrior;
123999 sqlite3ExprListDelete(db, p->pEList);
124000 sqlite3SrcListDelete(db, p->pSrc);
124001 sqlite3ExprDelete(db, p->pWhere);
124002 sqlite3ExprListDelete(db, p->pGroupBy);
124003 sqlite3ExprDelete(db, p->pHaving);
124004 sqlite3ExprListDelete(db, p->pOrderBy);
124005 sqlite3ExprDelete(db, p->pLimit);
124006#ifndef SQLITE_OMIT_WINDOWFUNC
124007 if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){
124008 sqlite3WindowListDelete(db, p->pWinDefn);
124009 }
124010#endif
124011 if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
124012 if( bFree ) sqlite3DbFreeNN(db, p);
124013 p = pPrior;
124014 bFree = 1;
124015 }
124016}
124017
124018/*
124019** Initialize a SelectDest structure.
124020*/
124021SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
124022 pDest->eDest = (u8)eDest;
124023 pDest->iSDParm = iParm;
124024 pDest->zAffSdst = 0;
124025 pDest->iSdst = 0;
124026 pDest->nSdst = 0;
124027}
124028
124029
124030/*
124031** Allocate a new Select structure and return a pointer to that
124032** structure.
124033*/
124034SQLITE_PRIVATE Select *sqlite3SelectNew(
124035 Parse *pParse, /* Parsing context */
124036 ExprList *pEList, /* which columns to include in the result */
124037 SrcList *pSrc, /* the FROM clause -- which tables to scan */
124038 Expr *pWhere, /* the WHERE clause */
124039 ExprList *pGroupBy, /* the GROUP BY clause */
124040 Expr *pHaving, /* the HAVING clause */
124041 ExprList *pOrderBy, /* the ORDER BY clause */
124042 u32 selFlags, /* Flag parameters, such as SF_Distinct */
124043 Expr *pLimit /* LIMIT value. NULL means not used */
124044){
124045 Select *pNew;
124046 Select standin;
124047 pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
124048 if( pNew==0 ){
124049 assert( pParse->db->mallocFailed );
124050 pNew = &standin;
124051 }
124052 if( pEList==0 ){
124053 pEList = sqlite3ExprListAppend(pParse, 0,
124054 sqlite3Expr(pParse->db,TK_ASTERISK,0));
124055 }
124056 pNew->pEList = pEList;
124057 pNew->op = TK_SELECT;
124058 pNew->selFlags = selFlags;
124059 pNew->iLimit = 0;
124060 pNew->iOffset = 0;
124061 pNew->selId = ++pParse->nSelect;
124062 pNew->addrOpenEphm[0] = -1;
124063 pNew->addrOpenEphm[1] = -1;
124064 pNew->nSelectRow = 0;
124065 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*pSrc));
124066 pNew->pSrc = pSrc;
124067 pNew->pWhere = pWhere;
124068 pNew->pGroupBy = pGroupBy;
124069 pNew->pHaving = pHaving;
124070 pNew->pOrderBy = pOrderBy;
124071 pNew->pPrior = 0;
124072 pNew->pNext = 0;
124073 pNew->pLimit = pLimit;
124074 pNew->pWith = 0;
124075#ifndef SQLITE_OMIT_WINDOWFUNC
124076 pNew->pWin = 0;
124077 pNew->pWinDefn = 0;
124078#endif
124079 if( pParse->db->mallocFailed ) {
124080 clearSelect(pParse->db, pNew, pNew!=&standin);
124081 pNew = 0;
124082 }else{
124083 assert( pNew->pSrc!=0 || pParse->nErr>0 );
124084 }
124085 assert( pNew!=&standin );
124086 return pNew;
124087}
124088
124089
124090/*
124091** Delete the given Select structure and all of its substructures.
124092*/
124093SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
124094 if( OK_IF_ALWAYS_TRUE(p) ) clearSelect(db, p, 1);
124095}
124096
124097/*
124098** Return a pointer to the right-most SELECT statement in a compound.
124099*/
124100static Select *findRightmost(Select *p){
124101 while( p->pNext ) p = p->pNext;
124102 return p;
124103}
124104
124105/*
124106** Given 1 to 3 identifiers preceding the JOIN keyword, determine the
124107** type of join. Return an integer constant that expresses that type
124108** in terms of the following bit values:
124109**
124110** JT_INNER
124111** JT_CROSS
124112** JT_OUTER
124113** JT_NATURAL
124114** JT_LEFT
124115** JT_RIGHT
124116**
124117** A full outer join is the combination of JT_LEFT and JT_RIGHT.
124118**
124119** If an illegal or unsupported join type is seen, then still return
124120** a join type, but put an error in the pParse structure.
124121*/
124122SQLITE_PRIVATE int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
124123 int jointype = 0;
124124 Token *apAll[3];
124125 Token *p;
124126 /* 0123456789 123456789 123456789 123 */
124127 static const char zKeyText[] = "naturaleftouterightfullinnercross";
124128 static const struct {
124129 u8 i; /* Beginning of keyword text in zKeyText[] */
124130 u8 nChar; /* Length of the keyword in characters */
124131 u8 code; /* Join type mask */
124132 } aKeyword[] = {
124133 /* natural */ { 0, 7, JT_NATURAL },
124134 /* left */ { 6, 4, JT_LEFT|JT_OUTER },
124135 /* outer */ { 10, 5, JT_OUTER },
124136 /* right */ { 14, 5, JT_RIGHT|JT_OUTER },
124137 /* full */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER },
124138 /* inner */ { 23, 5, JT_INNER },
124139 /* cross */ { 28, 5, JT_INNER|JT_CROSS },
124140 };
124141 int i, j;
124142 apAll[0] = pA;
124143 apAll[1] = pB;
124144 apAll[2] = pC;
124145 for(i=0; i<3 && apAll[i]; i++){
124146 p = apAll[i];
124147 for(j=0; j<ArraySize(aKeyword); j++){
124148 if( p->n==aKeyword[j].nChar
124149 && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
124150 jointype |= aKeyword[j].code;
124151 break;
124152 }
124153 }
124154 testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
124155 if( j>=ArraySize(aKeyword) ){
124156 jointype |= JT_ERROR;
124157 break;
124158 }
124159 }
124160 if(
124161 (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
124162 (jointype & JT_ERROR)!=0
124163 ){
124164 const char *zSp = " ";
124165 assert( pB!=0 );
124166 if( pC==0 ){ zSp++; }
124167 sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
124168 "%T %T%s%T", pA, pB, zSp, pC);
124169 jointype = JT_INNER;
124170 }else if( (jointype & JT_OUTER)!=0
124171 && (jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ){
124172 sqlite3ErrorMsg(pParse,
124173 "RIGHT and FULL OUTER JOINs are not currently supported");
124174 jointype = JT_INNER;
124175 }
124176 return jointype;
124177}
124178
124179/*
124180** Return the index of a column in a table. Return -1 if the column
124181** is not contained in the table.
124182*/
124183static int columnIndex(Table *pTab, const char *zCol){
124184 int i;
124185 for(i=0; i<pTab->nCol; i++){
124186 if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
124187 }
124188 return -1;
124189}
124190
124191/*
124192** Search the first N tables in pSrc, from left to right, looking for a
124193** table that has a column named zCol.
124194**
124195** When found, set *piTab and *piCol to the table index and column index
124196** of the matching column and return TRUE.
124197**
124198** If not found, return FALSE.
124199*/
124200static int tableAndColumnIndex(
124201 SrcList *pSrc, /* Array of tables to search */
124202 int N, /* Number of tables in pSrc->a[] to search */
124203 const char *zCol, /* Name of the column we are looking for */
124204 int *piTab, /* Write index of pSrc->a[] here */
124205 int *piCol /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
124206){
124207 int i; /* For looping over tables in pSrc */
124208 int iCol; /* Index of column matching zCol */
124209
124210 assert( (piTab==0)==(piCol==0) ); /* Both or neither are NULL */
124211 for(i=0; i<N; i++){
124212 iCol = columnIndex(pSrc->a[i].pTab, zCol);
124213 if( iCol>=0 ){
124214 if( piTab ){
124215 *piTab = i;
124216 *piCol = iCol;
124217 }
124218 return 1;
124219 }
124220 }
124221 return 0;
124222}
124223
124224/*
124225** This function is used to add terms implied by JOIN syntax to the
124226** WHERE clause expression of a SELECT statement. The new term, which
124227** is ANDed with the existing WHERE clause, is of the form:
124228**
124229** (tab1.col1 = tab2.col2)
124230**
124231** where tab1 is the iSrc'th table in SrcList pSrc and tab2 is the
124232** (iSrc+1)'th. Column col1 is column iColLeft of tab1, and col2 is
124233** column iColRight of tab2.
124234*/
124235static void addWhereTerm(
124236 Parse *pParse, /* Parsing context */
124237 SrcList *pSrc, /* List of tables in FROM clause */
124238 int iLeft, /* Index of first table to join in pSrc */
124239 int iColLeft, /* Index of column in first table */
124240 int iRight, /* Index of second table in pSrc */
124241 int iColRight, /* Index of column in second table */
124242 int isOuterJoin, /* True if this is an OUTER join */
124243 Expr **ppWhere /* IN/OUT: The WHERE clause to add to */
124244){
124245 sqlite3 *db = pParse->db;
124246 Expr *pE1;
124247 Expr *pE2;
124248 Expr *pEq;
124249
124250 assert( iLeft<iRight );
124251 assert( pSrc->nSrc>iRight );
124252 assert( pSrc->a[iLeft].pTab );
124253 assert( pSrc->a[iRight].pTab );
124254
124255 pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft);
124256 pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight);
124257
124258 pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2);
124259 if( pEq && isOuterJoin ){
124260 ExprSetProperty(pEq, EP_FromJoin);
124261 assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) );
124262 ExprSetVVAProperty(pEq, EP_NoReduce);
124263 pEq->iRightJoinTable = (i16)pE2->iTable;
124264 }
124265 *ppWhere = sqlite3ExprAnd(db, *ppWhere, pEq);
124266}
124267
124268/*
124269** Set the EP_FromJoin property on all terms of the given expression.
124270** And set the Expr.iRightJoinTable to iTable for every term in the
124271** expression.
124272**
124273** The EP_FromJoin property is used on terms of an expression to tell
124274** the LEFT OUTER JOIN processing logic that this term is part of the
124275** join restriction specified in the ON or USING clause and not a part
124276** of the more general WHERE clause. These terms are moved over to the
124277** WHERE clause during join processing but we need to remember that they
124278** originated in the ON or USING clause.
124279**
124280** The Expr.iRightJoinTable tells the WHERE clause processing that the
124281** expression depends on table iRightJoinTable even if that table is not
124282** explicitly mentioned in the expression. That information is needed
124283** for cases like this:
124284**
124285** SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
124286**
124287** The where clause needs to defer the handling of the t1.x=5
124288** term until after the t2 loop of the join. In that way, a
124289** NULL t2 row will be inserted whenever t1.x!=5. If we do not
124290** defer the handling of t1.x=5, it will be processed immediately
124291** after the t1 loop and rows with t1.x!=5 will never appear in
124292** the output, which is incorrect.
124293*/
124294static void setJoinExpr(Expr *p, int iTable){
124295 while( p ){
124296 ExprSetProperty(p, EP_FromJoin);
124297 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
124298 ExprSetVVAProperty(p, EP_NoReduce);
124299 p->iRightJoinTable = (i16)iTable;
124300 if( p->op==TK_FUNCTION && p->x.pList ){
124301 int i;
124302 for(i=0; i<p->x.pList->nExpr; i++){
124303 setJoinExpr(p->x.pList->a[i].pExpr, iTable);
124304 }
124305 }
124306 setJoinExpr(p->pLeft, iTable);
124307 p = p->pRight;
124308 }
124309}
124310
124311/* Undo the work of setJoinExpr(). In the expression tree p, convert every
124312** term that is marked with EP_FromJoin and iRightJoinTable==iTable into
124313** an ordinary term that omits the EP_FromJoin mark.
124314**
124315** This happens when a LEFT JOIN is simplified into an ordinary JOIN.
124316*/
124317static void unsetJoinExpr(Expr *p, int iTable){
124318 while( p ){
124319 if( ExprHasProperty(p, EP_FromJoin)
124320 && (iTable<0 || p->iRightJoinTable==iTable) ){
124321 ExprClearProperty(p, EP_FromJoin);
124322 }
124323 if( p->op==TK_FUNCTION && p->x.pList ){
124324 int i;
124325 for(i=0; i<p->x.pList->nExpr; i++){
124326 unsetJoinExpr(p->x.pList->a[i].pExpr, iTable);
124327 }
124328 }
124329 unsetJoinExpr(p->pLeft, iTable);
124330 p = p->pRight;
124331 }
124332}
124333
124334/*
124335** This routine processes the join information for a SELECT statement.
124336** ON and USING clauses are converted into extra terms of the WHERE clause.
124337** NATURAL joins also create extra WHERE clause terms.
124338**
124339** The terms of a FROM clause are contained in the Select.pSrc structure.
124340** The left most table is the first entry in Select.pSrc. The right-most
124341** table is the last entry. The join operator is held in the entry to
124342** the left. Thus entry 0 contains the join operator for the join between
124343** entries 0 and 1. Any ON or USING clauses associated with the join are
124344** also attached to the left entry.
124345**
124346** This routine returns the number of errors encountered.
124347*/
124348static int sqliteProcessJoin(Parse *pParse, Select *p){
124349 SrcList *pSrc; /* All tables in the FROM clause */
124350 int i, j; /* Loop counters */
124351 struct SrcList_item *pLeft; /* Left table being joined */
124352 struct SrcList_item *pRight; /* Right table being joined */
124353
124354 pSrc = p->pSrc;
124355 pLeft = &pSrc->a[0];
124356 pRight = &pLeft[1];
124357 for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
124358 Table *pRightTab = pRight->pTab;
124359 int isOuter;
124360
124361 if( NEVER(pLeft->pTab==0 || pRightTab==0) ) continue;
124362 isOuter = (pRight->fg.jointype & JT_OUTER)!=0;
124363
124364 /* When the NATURAL keyword is present, add WHERE clause terms for
124365 ** every column that the two tables have in common.
124366 */
124367 if( pRight->fg.jointype & JT_NATURAL ){
124368 if( pRight->pOn || pRight->pUsing ){
124369 sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
124370 "an ON or USING clause", 0);
124371 return 1;
124372 }
124373 for(j=0; j<pRightTab->nCol; j++){
124374 char *zName; /* Name of column in the right table */
124375 int iLeft; /* Matching left table */
124376 int iLeftCol; /* Matching column in the left table */
124377
124378 zName = pRightTab->aCol[j].zName;
124379 if( tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol) ){
124380 addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, j,
124381 isOuter, &p->pWhere);
124382 }
124383 }
124384 }
124385
124386 /* Disallow both ON and USING clauses in the same join
124387 */
124388 if( pRight->pOn && pRight->pUsing ){
124389 sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
124390 "clauses in the same join");
124391 return 1;
124392 }
124393
124394 /* Add the ON clause to the end of the WHERE clause, connected by
124395 ** an AND operator.
124396 */
124397 if( pRight->pOn ){
124398 if( isOuter ) setJoinExpr(pRight->pOn, pRight->iCursor);
124399 p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn);
124400 pRight->pOn = 0;
124401 }
124402
124403 /* Create extra terms on the WHERE clause for each column named
124404 ** in the USING clause. Example: If the two tables to be joined are
124405 ** A and B and the USING clause names X, Y, and Z, then add this
124406 ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
124407 ** Report an error if any column mentioned in the USING clause is
124408 ** not contained in both tables to be joined.
124409 */
124410 if( pRight->pUsing ){
124411 IdList *pList = pRight->pUsing;
124412 for(j=0; j<pList->nId; j++){
124413 char *zName; /* Name of the term in the USING clause */
124414 int iLeft; /* Table on the left with matching column name */
124415 int iLeftCol; /* Column number of matching column on the left */
124416 int iRightCol; /* Column number of matching column on the right */
124417
124418 zName = pList->a[j].zName;
124419 iRightCol = columnIndex(pRightTab, zName);
124420 if( iRightCol<0
124421 || !tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol)
124422 ){
124423 sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
124424 "not present in both tables", zName);
124425 return 1;
124426 }
124427 addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, iRightCol,
124428 isOuter, &p->pWhere);
124429 }
124430 }
124431 }
124432 return 0;
124433}
124434
124435/*
124436** An instance of this object holds information (beyond pParse and pSelect)
124437** needed to load the next result row that is to be added to the sorter.
124438*/
124439typedef struct RowLoadInfo RowLoadInfo;
124440struct RowLoadInfo {
124441 int regResult; /* Store results in array of registers here */
124442 u8 ecelFlags; /* Flag argument to ExprCodeExprList() */
124443#ifdef SQLITE_ENABLE_SORTER_REFERENCES
124444 ExprList *pExtra; /* Extra columns needed by sorter refs */
124445 int regExtraResult; /* Where to load the extra columns */
124446#endif
124447};
124448
124449/*
124450** This routine does the work of loading query data into an array of
124451** registers so that it can be added to the sorter.
124452*/
124453static void innerLoopLoadRow(
124454 Parse *pParse, /* Statement under construction */
124455 Select *pSelect, /* The query being coded */
124456 RowLoadInfo *pInfo /* Info needed to complete the row load */
124457){
124458 sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult,
124459 0, pInfo->ecelFlags);
124460#ifdef SQLITE_ENABLE_SORTER_REFERENCES
124461 if( pInfo->pExtra ){
124462 sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
124463 sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
124464 }
124465#endif
124466}
124467
124468/*
124469** Code the OP_MakeRecord instruction that generates the entry to be
124470** added into the sorter.
124471**
124472** Return the register in which the result is stored.
124473*/
124474static int makeSorterRecord(
124475 Parse *pParse,
124476 SortCtx *pSort,
124477 Select *pSelect,
124478 int regBase,
124479 int nBase
124480){
124481 int nOBSat = pSort->nOBSat;
124482 Vdbe *v = pParse->pVdbe;
124483 int regOut = ++pParse->nMem;
124484 if( pSort->pDeferredRowLoad ){
124485 innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad);
124486 }
124487 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regOut);
124488 return regOut;
124489}
124490
124491/*
124492** Generate code that will push the record in registers regData
124493** through regData+nData-1 onto the sorter.
124494*/
124495static void pushOntoSorter(
124496 Parse *pParse, /* Parser context */
124497 SortCtx *pSort, /* Information about the ORDER BY clause */
124498 Select *pSelect, /* The whole SELECT statement */
124499 int regData, /* First register holding data to be sorted */
124500 int regOrigData, /* First register holding data before packing */
124501 int nData, /* Number of elements in the regData data array */
124502 int nPrefixReg /* No. of reg prior to regData available for use */
124503){
124504 Vdbe *v = pParse->pVdbe; /* Stmt under construction */
124505 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
124506 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
124507 int nBase = nExpr + bSeq + nData; /* Fields in sorter record */
124508 int regBase; /* Regs for sorter record */
124509 int regRecord = 0; /* Assembled sorter record */
124510 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
124511 int op; /* Opcode to add sorter record to sorter */
124512 int iLimit; /* LIMIT counter */
124513 int iSkip = 0; /* End of the sorter insert loop */
124514
124515 assert( bSeq==0 || bSeq==1 );
124516
124517 /* Three cases:
124518 ** (1) The data to be sorted has already been packed into a Record
124519 ** by a prior OP_MakeRecord. In this case nData==1 and regData
124520 ** will be completely unrelated to regOrigData.
124521 ** (2) All output columns are included in the sort record. In that
124522 ** case regData==regOrigData.
124523 ** (3) Some output columns are omitted from the sort record due to
124524 ** the SQLITE_ENABLE_SORTER_REFERENCE optimization, or due to the
124525 ** SQLITE_ECEL_OMITREF optimization, or due to the
124526 ** SortCtx.pDeferredRowLoad optimiation. In any of these cases
124527 ** regOrigData is 0 to prevent this routine from trying to copy
124528 ** values that might not yet exist.
124529 */
124530 assert( nData==1 || regData==regOrigData || regOrigData==0 );
124531
124532 if( nPrefixReg ){
124533 assert( nPrefixReg==nExpr+bSeq );
124534 regBase = regData - nPrefixReg;
124535 }else{
124536 regBase = pParse->nMem + 1;
124537 pParse->nMem += nBase;
124538 }
124539 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
124540 iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
124541 pSort->labelDone = sqlite3VdbeMakeLabel(v);
124542 sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
124543 SQLITE_ECEL_DUP | (regOrigData? SQLITE_ECEL_REF : 0));
124544 if( bSeq ){
124545 sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
124546 }
124547 if( nPrefixReg==0 && nData>0 ){
124548 sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData);
124549 }
124550 if( nOBSat>0 ){
124551 int regPrevKey; /* The first nOBSat columns of the previous row */
124552 int addrFirst; /* Address of the OP_IfNot opcode */
124553 int addrJmp; /* Address of the OP_Jump opcode */
124554 VdbeOp *pOp; /* Opcode that opens the sorter */
124555 int nKey; /* Number of sorting key columns, including OP_Sequence */
124556 KeyInfo *pKI; /* Original KeyInfo on the sorter table */
124557
124558 regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
124559 regPrevKey = pParse->nMem+1;
124560 pParse->nMem += pSort->nOBSat;
124561 nKey = nExpr - pSort->nOBSat + bSeq;
124562 if( bSeq ){
124563 addrFirst = sqlite3VdbeAddOp1(v, OP_IfNot, regBase+nExpr);
124564 }else{
124565 addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor);
124566 }
124567 VdbeCoverage(v);
124568 sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat);
124569 pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
124570 if( pParse->db->mallocFailed ) return;
124571 pOp->p2 = nKey + nData;
124572 pKI = pOp->p4.pKeyInfo;
124573 memset(pKI->aSortOrder, 0, pKI->nKeyField); /* Makes OP_Jump testable */
124574 sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);
124575 testcase( pKI->nAllField > pKI->nKeyField+2 );
124576 pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat,
124577 pKI->nAllField-pKI->nKeyField-1);
124578 addrJmp = sqlite3VdbeCurrentAddr(v);
124579 sqlite3VdbeAddOp3(v, OP_Jump, addrJmp+1, 0, addrJmp+1); VdbeCoverage(v);
124580 pSort->labelBkOut = sqlite3VdbeMakeLabel(v);
124581 pSort->regReturn = ++pParse->nMem;
124582 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
124583 sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);
124584 if( iLimit ){
124585 sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone);
124586 VdbeCoverage(v);
124587 }
124588 sqlite3VdbeJumpHere(v, addrFirst);
124589 sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
124590 sqlite3VdbeJumpHere(v, addrJmp);
124591 }
124592 if( iLimit ){
124593 /* At this point the values for the new sorter entry are stored
124594 ** in an array of registers. They need to be composed into a record
124595 ** and inserted into the sorter if either (a) there are currently
124596 ** less than LIMIT+OFFSET items or (b) the new record is smaller than
124597 ** the largest record currently in the sorter. If (b) is true and there
124598 ** are already LIMIT+OFFSET items in the sorter, delete the largest
124599 ** entry before inserting the new one. This way there are never more
124600 ** than LIMIT+OFFSET items in the sorter.
124601 **
124602 ** If the new record does not need to be inserted into the sorter,
124603 ** jump to the next iteration of the loop. If the pSort->labelOBLopt
124604 ** value is not zero, then it is a label of where to jump. Otherwise,
124605 ** just bypass the row insert logic. See the header comment on the
124606 ** sqlite3WhereOrderByLimitOptLabel() function for additional info.
124607 */
124608 int iCsr = pSort->iECursor;
124609 sqlite3VdbeAddOp2(v, OP_IfNotZero, iLimit, sqlite3VdbeCurrentAddr(v)+4);
124610 VdbeCoverage(v);
124611 sqlite3VdbeAddOp2(v, OP_Last, iCsr, 0);
124612 iSkip = sqlite3VdbeAddOp4Int(v, OP_IdxLE,
124613 iCsr, 0, regBase+nOBSat, nExpr-nOBSat);
124614 VdbeCoverage(v);
124615 sqlite3VdbeAddOp1(v, OP_Delete, iCsr);
124616 }
124617 if( regRecord==0 ){
124618 regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
124619 }
124620 if( pSort->sortFlags & SORTFLAG_UseSorter ){
124621 op = OP_SorterInsert;
124622 }else{
124623 op = OP_IdxInsert;
124624 }
124625 sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
124626 regBase+nOBSat, nBase-nOBSat);
124627 if( iSkip ){
124628 sqlite3VdbeChangeP2(v, iSkip,
124629 pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(v));
124630 }
124631}
124632
124633/*
124634** Add code to implement the OFFSET
124635*/
124636static void codeOffset(
124637 Vdbe *v, /* Generate code into this VM */
124638 int iOffset, /* Register holding the offset counter */
124639 int iContinue /* Jump here to skip the current record */
124640){
124641 if( iOffset>0 ){
124642 sqlite3VdbeAddOp3(v, OP_IfPos, iOffset, iContinue, 1); VdbeCoverage(v);
124643 VdbeComment((v, "OFFSET"));
124644 }
124645}
124646
124647/*
124648** Add code that will check to make sure the N registers starting at iMem
124649** form a distinct entry. iTab is a sorting index that holds previously
124650** seen combinations of the N values. A new entry is made in iTab
124651** if the current N values are new.
124652**
124653** A jump to addrRepeat is made and the N+1 values are popped from the
124654** stack if the top N elements are not distinct.
124655*/
124656static void codeDistinct(
124657 Parse *pParse, /* Parsing and code generating context */
124658 int iTab, /* A sorting index used to test for distinctness */
124659 int addrRepeat, /* Jump to here if not distinct */
124660 int N, /* Number of elements */
124661 int iMem /* First element */
124662){
124663 Vdbe *v;
124664 int r1;
124665
124666 v = pParse->pVdbe;
124667 r1 = sqlite3GetTempReg(pParse);
124668 sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v);
124669 sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
124670 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r1, iMem, N);
124671 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
124672 sqlite3ReleaseTempReg(pParse, r1);
124673}
124674
124675#ifdef SQLITE_ENABLE_SORTER_REFERENCES
124676/*
124677** This function is called as part of inner-loop generation for a SELECT
124678** statement with an ORDER BY that is not optimized by an index. It
124679** determines the expressions, if any, that the sorter-reference
124680** optimization should be used for. The sorter-reference optimization
124681** is used for SELECT queries like:
124682**
124683** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10
124684**
124685** If the optimization is used for expression "bigblob", then instead of
124686** storing values read from that column in the sorter records, the PK of
124687** the row from table t1 is stored instead. Then, as records are extracted from
124688** the sorter to return to the user, the required value of bigblob is
124689** retrieved directly from table t1. If the values are very large, this
124690** can be more efficient than storing them directly in the sorter records.
124691**
124692** The ExprList_item.bSorterRef flag is set for each expression in pEList
124693** for which the sorter-reference optimization should be enabled.
124694** Additionally, the pSort->aDefer[] array is populated with entries
124695** for all cursors required to evaluate all selected expressions. Finally.
124696** output variable (*ppExtra) is set to an expression list containing
124697** expressions for all extra PK values that should be stored in the
124698** sorter records.
124699*/
124700static void selectExprDefer(
124701 Parse *pParse, /* Leave any error here */
124702 SortCtx *pSort, /* Sorter context */
124703 ExprList *pEList, /* Expressions destined for sorter */
124704 ExprList **ppExtra /* Expressions to append to sorter record */
124705){
124706 int i;
124707 int nDefer = 0;
124708 ExprList *pExtra = 0;
124709 for(i=0; i<pEList->nExpr; i++){
124710 struct ExprList_item *pItem = &pEList->a[i];
124711 if( pItem->u.x.iOrderByCol==0 ){
124712 Expr *pExpr = pItem->pExpr;
124713 Table *pTab = pExpr->y.pTab;
124714 if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 && pTab && !IsVirtual(pTab)
124715 && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)
124716 ){
124717 int j;
124718 for(j=0; j<nDefer; j++){
124719 if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
124720 }
124721 if( j==nDefer ){
124722 if( nDefer==ArraySize(pSort->aDefer) ){
124723 continue;
124724 }else{
124725 int nKey = 1;
124726 int k;
124727 Index *pPk = 0;
124728 if( !HasRowid(pTab) ){
124729 pPk = sqlite3PrimaryKeyIndex(pTab);
124730 nKey = pPk->nKeyCol;
124731 }
124732 for(k=0; k<nKey; k++){
124733 Expr *pNew = sqlite3PExpr(pParse, TK_COLUMN, 0, 0);
124734 if( pNew ){
124735 pNew->iTable = pExpr->iTable;
124736 pNew->y.pTab = pExpr->y.pTab;
124737 pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
124738 pExtra = sqlite3ExprListAppend(pParse, pExtra, pNew);
124739 }
124740 }
124741 pSort->aDefer[nDefer].pTab = pExpr->y.pTab;
124742 pSort->aDefer[nDefer].iCsr = pExpr->iTable;
124743 pSort->aDefer[nDefer].nKey = nKey;
124744 nDefer++;
124745 }
124746 }
124747 pItem->bSorterRef = 1;
124748 }
124749 }
124750 }
124751 pSort->nDefer = (u8)nDefer;
124752 *ppExtra = pExtra;
124753}
124754#endif
124755
124756/*
124757** This routine generates the code for the inside of the inner loop
124758** of a SELECT.
124759**
124760** If srcTab is negative, then the p->pEList expressions
124761** are evaluated in order to get the data for this row. If srcTab is
124762** zero or more, then data is pulled from srcTab and p->pEList is used only
124763** to get the number of columns and the collation sequence for each column.
124764*/
124765static void selectInnerLoop(
124766 Parse *pParse, /* The parser context */
124767 Select *p, /* The complete select statement being coded */
124768 int srcTab, /* Pull data from this table if non-negative */
124769 SortCtx *pSort, /* If not NULL, info on how to process ORDER BY */
124770 DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
124771 SelectDest *pDest, /* How to dispose of the results */
124772 int iContinue, /* Jump here to continue with next row */
124773 int iBreak /* Jump here to break out of the inner loop */
124774){
124775 Vdbe *v = pParse->pVdbe;
124776 int i;
124777 int hasDistinct; /* True if the DISTINCT keyword is present */
124778 int eDest = pDest->eDest; /* How to dispose of results */
124779 int iParm = pDest->iSDParm; /* First argument to disposal method */
124780 int nResultCol; /* Number of result columns */
124781 int nPrefixReg = 0; /* Number of extra registers before regResult */
124782 RowLoadInfo sRowLoadInfo; /* Info for deferred row loading */
124783
124784 /* Usually, regResult is the first cell in an array of memory cells
124785 ** containing the current result row. In this case regOrig is set to the
124786 ** same value. However, if the results are being sent to the sorter, the
124787 ** values for any expressions that are also part of the sort-key are omitted
124788 ** from this array. In this case regOrig is set to zero. */
124789 int regResult; /* Start of memory holding current results */
124790 int regOrig; /* Start of memory holding full result (or 0) */
124791
124792 assert( v );
124793 assert( p->pEList!=0 );
124794 hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
124795 if( pSort && pSort->pOrderBy==0 ) pSort = 0;
124796 if( pSort==0 && !hasDistinct ){
124797 assert( iContinue!=0 );
124798 codeOffset(v, p->iOffset, iContinue);
124799 }
124800
124801 /* Pull the requested columns.
124802 */
124803 nResultCol = p->pEList->nExpr;
124804
124805 if( pDest->iSdst==0 ){
124806 if( pSort ){
124807 nPrefixReg = pSort->pOrderBy->nExpr;
124808 if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++;
124809 pParse->nMem += nPrefixReg;
124810 }
124811 pDest->iSdst = pParse->nMem+1;
124812 pParse->nMem += nResultCol;
124813 }else if( pDest->iSdst+nResultCol > pParse->nMem ){
124814 /* This is an error condition that can result, for example, when a SELECT
124815 ** on the right-hand side of an INSERT contains more result columns than
124816 ** there are columns in the table on the left. The error will be caught
124817 ** and reported later. But we need to make sure enough memory is allocated
124818 ** to avoid other spurious errors in the meantime. */
124819 pParse->nMem += nResultCol;
124820 }
124821 pDest->nSdst = nResultCol;
124822 regOrig = regResult = pDest->iSdst;
124823 if( srcTab>=0 ){
124824 for(i=0; i<nResultCol; i++){
124825 sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
124826 VdbeComment((v, "%s", p->pEList->a[i].zName));
124827 }
124828 }else if( eDest!=SRT_Exists ){
124829#ifdef SQLITE_ENABLE_SORTER_REFERENCES
124830 ExprList *pExtra = 0;
124831#endif
124832 /* If the destination is an EXISTS(...) expression, the actual
124833 ** values returned by the SELECT are not required.
124834 */
124835 u8 ecelFlags; /* "ecel" is an abbreviation of "ExprCodeExprList" */
124836 ExprList *pEList;
124837 if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){
124838 ecelFlags = SQLITE_ECEL_DUP;
124839 }else{
124840 ecelFlags = 0;
124841 }
124842 if( pSort && hasDistinct==0 && eDest!=SRT_EphemTab && eDest!=SRT_Table ){
124843 /* For each expression in p->pEList that is a copy of an expression in
124844 ** the ORDER BY clause (pSort->pOrderBy), set the associated
124845 ** iOrderByCol value to one more than the index of the ORDER BY
124846 ** expression within the sort-key that pushOntoSorter() will generate.
124847 ** This allows the p->pEList field to be omitted from the sorted record,
124848 ** saving space and CPU cycles. */
124849 ecelFlags |= (SQLITE_ECEL_OMITREF|SQLITE_ECEL_REF);
124850
124851 for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
124852 int j;
124853 if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
124854 p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
124855 }
124856 }
124857#ifdef SQLITE_ENABLE_SORTER_REFERENCES
124858 selectExprDefer(pParse, pSort, p->pEList, &pExtra);
124859 if( pExtra && pParse->db->mallocFailed==0 ){
124860 /* If there are any extra PK columns to add to the sorter records,
124861 ** allocate extra memory cells and adjust the OpenEphemeral
124862 ** instruction to account for the larger records. This is only
124863 ** required if there are one or more WITHOUT ROWID tables with
124864 ** composite primary keys in the SortCtx.aDefer[] array. */
124865 VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
124866 pOp->p2 += (pExtra->nExpr - pSort->nDefer);
124867 pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
124868 pParse->nMem += pExtra->nExpr;
124869 }
124870#endif
124871
124872 /* Adjust nResultCol to account for columns that are omitted
124873 ** from the sorter by the optimizations in this branch */
124874 pEList = p->pEList;
124875 for(i=0; i<pEList->nExpr; i++){
124876 if( pEList->a[i].u.x.iOrderByCol>0
124877#ifdef SQLITE_ENABLE_SORTER_REFERENCES
124878 || pEList->a[i].bSorterRef
124879#endif
124880 ){
124881 nResultCol--;
124882 regOrig = 0;
124883 }
124884 }
124885
124886 testcase( regOrig );
124887 testcase( eDest==SRT_Set );
124888 testcase( eDest==SRT_Mem );
124889 testcase( eDest==SRT_Coroutine );
124890 testcase( eDest==SRT_Output );
124891 assert( eDest==SRT_Set || eDest==SRT_Mem
124892 || eDest==SRT_Coroutine || eDest==SRT_Output );
124893 }
124894 sRowLoadInfo.regResult = regResult;
124895 sRowLoadInfo.ecelFlags = ecelFlags;
124896#ifdef SQLITE_ENABLE_SORTER_REFERENCES
124897 sRowLoadInfo.pExtra = pExtra;
124898 sRowLoadInfo.regExtraResult = regResult + nResultCol;
124899 if( pExtra ) nResultCol += pExtra->nExpr;
124900#endif
124901 if( p->iLimit
124902 && (ecelFlags & SQLITE_ECEL_OMITREF)!=0
124903 && nPrefixReg>0
124904 ){
124905 assert( pSort!=0 );
124906 assert( hasDistinct==0 );
124907 pSort->pDeferredRowLoad = &sRowLoadInfo;
124908 regOrig = 0;
124909 }else{
124910 innerLoopLoadRow(pParse, p, &sRowLoadInfo);
124911 }
124912 }
124913
124914 /* If the DISTINCT keyword was present on the SELECT statement
124915 ** and this row has been seen before, then do not make this row
124916 ** part of the result.
124917 */
124918 if( hasDistinct ){
124919 switch( pDistinct->eTnctType ){
124920 case WHERE_DISTINCT_ORDERED: {
124921 VdbeOp *pOp; /* No longer required OpenEphemeral instr. */
124922 int iJump; /* Jump destination */
124923 int regPrev; /* Previous row content */
124924
124925 /* Allocate space for the previous row */
124926 regPrev = pParse->nMem+1;
124927 pParse->nMem += nResultCol;
124928
124929 /* Change the OP_OpenEphemeral coded earlier to an OP_Null
124930 ** sets the MEM_Cleared bit on the first register of the
124931 ** previous value. This will cause the OP_Ne below to always
124932 ** fail on the first iteration of the loop even if the first
124933 ** row is all NULLs.
124934 */
124935 sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
124936 pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
124937 pOp->opcode = OP_Null;
124938 pOp->p1 = 1;
124939 pOp->p2 = regPrev;
124940
124941 iJump = sqlite3VdbeCurrentAddr(v) + nResultCol;
124942 for(i=0; i<nResultCol; i++){
124943 CollSeq *pColl = sqlite3ExprCollSeq(pParse, p->pEList->a[i].pExpr);
124944 if( i<nResultCol-1 ){
124945 sqlite3VdbeAddOp3(v, OP_Ne, regResult+i, iJump, regPrev+i);
124946 VdbeCoverage(v);
124947 }else{
124948 sqlite3VdbeAddOp3(v, OP_Eq, regResult+i, iContinue, regPrev+i);
124949 VdbeCoverage(v);
124950 }
124951 sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
124952 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
124953 }
124954 assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );
124955 sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nResultCol-1);
124956 break;
124957 }
124958
124959 case WHERE_DISTINCT_UNIQUE: {
124960 sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
124961 break;
124962 }
124963
124964 default: {
124965 assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
124966 codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol,
124967 regResult);
124968 break;
124969 }
124970 }
124971 if( pSort==0 ){
124972 codeOffset(v, p->iOffset, iContinue);
124973 }
124974 }
124975
124976 switch( eDest ){
124977 /* In this mode, write each query result to the key of the temporary
124978 ** table iParm.
124979 */
124980#ifndef SQLITE_OMIT_COMPOUND_SELECT
124981 case SRT_Union: {
124982 int r1;
124983 r1 = sqlite3GetTempReg(pParse);
124984 sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
124985 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol);
124986 sqlite3ReleaseTempReg(pParse, r1);
124987 break;
124988 }
124989
124990 /* Construct a record from the query result, but instead of
124991 ** saving that record, use it as a key to delete elements from
124992 ** the temporary table iParm.
124993 */
124994 case SRT_Except: {
124995 sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nResultCol);
124996 break;
124997 }
124998#endif /* SQLITE_OMIT_COMPOUND_SELECT */
124999
125000 /* Store the result as data using a unique key.
125001 */
125002 case SRT_Fifo:
125003 case SRT_DistFifo:
125004 case SRT_Table:
125005 case SRT_EphemTab: {
125006 int r1 = sqlite3GetTempRange(pParse, nPrefixReg+1);
125007 testcase( eDest==SRT_Table );
125008 testcase( eDest==SRT_EphemTab );
125009 testcase( eDest==SRT_Fifo );
125010 testcase( eDest==SRT_DistFifo );
125011 sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1+nPrefixReg);
125012#ifndef SQLITE_OMIT_CTE
125013 if( eDest==SRT_DistFifo ){
125014 /* If the destination is DistFifo, then cursor (iParm+1) is open
125015 ** on an ephemeral index. If the current row is already present
125016 ** in the index, do not write it to the output. If not, add the
125017 ** current row to the index and proceed with writing it to the
125018 ** output table as well. */
125019 int addr = sqlite3VdbeCurrentAddr(v) + 4;
125020 sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0);
125021 VdbeCoverage(v);
125022 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm+1, r1,regResult,nResultCol);
125023 assert( pSort==0 );
125024 }
125025#endif
125026 if( pSort ){
125027 assert( regResult==regOrig );
125028 pushOntoSorter(pParse, pSort, p, r1+nPrefixReg, regOrig, 1, nPrefixReg);
125029 }else{
125030 int r2 = sqlite3GetTempReg(pParse);
125031 sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
125032 sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
125033 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
125034 sqlite3ReleaseTempReg(pParse, r2);
125035 }
125036 sqlite3ReleaseTempRange(pParse, r1, nPrefixReg+1);
125037 break;
125038 }
125039
125040#ifndef SQLITE_OMIT_SUBQUERY
125041 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
125042 ** then there should be a single item on the stack. Write this
125043 ** item into the set table with bogus data.
125044 */
125045 case SRT_Set: {
125046 if( pSort ){
125047 /* At first glance you would think we could optimize out the
125048 ** ORDER BY in this case since the order of entries in the set
125049 ** does not matter. But there might be a LIMIT clause, in which
125050 ** case the order does matter */
125051 pushOntoSorter(
125052 pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
125053 }else{
125054 int r1 = sqlite3GetTempReg(pParse);
125055 assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol );
125056 sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, nResultCol,
125057 r1, pDest->zAffSdst, nResultCol);
125058 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol);
125059 sqlite3ReleaseTempReg(pParse, r1);
125060 }
125061 break;
125062 }
125063
125064 /* If any row exist in the result set, record that fact and abort.
125065 */
125066 case SRT_Exists: {
125067 sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm);
125068 /* The LIMIT clause will terminate the loop for us */
125069 break;
125070 }
125071
125072 /* If this is a scalar select that is part of an expression, then
125073 ** store the results in the appropriate memory cell or array of
125074 ** memory cells and break out of the scan loop.
125075 */
125076 case SRT_Mem: {
125077 if( pSort ){
125078 assert( nResultCol<=pDest->nSdst );
125079 pushOntoSorter(
125080 pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
125081 }else{
125082 assert( nResultCol==pDest->nSdst );
125083 assert( regResult==iParm );
125084 /* The LIMIT clause will jump out of the loop for us */
125085 }
125086 break;
125087 }
125088#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
125089
125090 case SRT_Coroutine: /* Send data to a co-routine */
125091 case SRT_Output: { /* Return the results */
125092 testcase( eDest==SRT_Coroutine );
125093 testcase( eDest==SRT_Output );
125094 if( pSort ){
125095 pushOntoSorter(pParse, pSort, p, regResult, regOrig, nResultCol,
125096 nPrefixReg);
125097 }else if( eDest==SRT_Coroutine ){
125098 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
125099 }else{
125100 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nResultCol);
125101 }
125102 break;
125103 }
125104
125105#ifndef SQLITE_OMIT_CTE
125106 /* Write the results into a priority queue that is order according to
125107 ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
125108 ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
125109 ** pSO->nExpr columns, then make sure all keys are unique by adding a
125110 ** final OP_Sequence column. The last column is the record as a blob.
125111 */
125112 case SRT_DistQueue:
125113 case SRT_Queue: {
125114 int nKey;
125115 int r1, r2, r3;
125116 int addrTest = 0;
125117 ExprList *pSO;
125118 pSO = pDest->pOrderBy;
125119 assert( pSO );
125120 nKey = pSO->nExpr;
125121 r1 = sqlite3GetTempReg(pParse);
125122 r2 = sqlite3GetTempRange(pParse, nKey+2);
125123 r3 = r2+nKey+1;
125124 if( eDest==SRT_DistQueue ){
125125 /* If the destination is DistQueue, then cursor (iParm+1) is open
125126 ** on a second ephemeral index that holds all values every previously
125127 ** added to the queue. */
125128 addrTest = sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, 0,
125129 regResult, nResultCol);
125130 VdbeCoverage(v);
125131 }
125132 sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r3);
125133 if( eDest==SRT_DistQueue ){
125134 sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r3);
125135 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
125136 }
125137 for(i=0; i<nKey; i++){
125138 sqlite3VdbeAddOp2(v, OP_SCopy,
125139 regResult + pSO->a[i].u.x.iOrderByCol - 1,
125140 r2+i);
125141 }
125142 sqlite3VdbeAddOp2(v, OP_Sequence, iParm, r2+nKey);
125143 sqlite3VdbeAddOp3(v, OP_MakeRecord, r2, nKey+2, r1);
125144 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, r2, nKey+2);
125145 if( addrTest ) sqlite3VdbeJumpHere(v, addrTest);
125146 sqlite3ReleaseTempReg(pParse, r1);
125147 sqlite3ReleaseTempRange(pParse, r2, nKey+2);
125148 break;
125149 }
125150#endif /* SQLITE_OMIT_CTE */
125151
125152
125153
125154#if !defined(SQLITE_OMIT_TRIGGER)
125155 /* Discard the results. This is used for SELECT statements inside
125156 ** the body of a TRIGGER. The purpose of such selects is to call
125157 ** user-defined functions that have side effects. We do not care
125158 ** about the actual results of the select.
125159 */
125160 default: {
125161 assert( eDest==SRT_Discard );
125162 break;
125163 }
125164#endif
125165 }
125166
125167 /* Jump to the end of the loop if the LIMIT is reached. Except, if
125168 ** there is a sorter, in which case the sorter has already limited
125169 ** the output for us.
125170 */
125171 if( pSort==0 && p->iLimit ){
125172 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
125173 }
125174}
125175
125176/*
125177** Allocate a KeyInfo object sufficient for an index of N key columns and
125178** X extra columns.
125179*/
125180SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){
125181 int nExtra = (N+X)*(sizeof(CollSeq*)+1) - sizeof(CollSeq*);
125182 KeyInfo *p = sqlite3DbMallocRawNN(db, sizeof(KeyInfo) + nExtra);
125183 if( p ){
125184 p->aSortOrder = (u8*)&p->aColl[N+X];
125185 p->nKeyField = (u16)N;
125186 p->nAllField = (u16)(N+X);
125187 p->enc = ENC(db);
125188 p->db = db;
125189 p->nRef = 1;
125190 memset(&p[1], 0, nExtra);
125191 }else{
125192 sqlite3OomFault(db);
125193 }
125194 return p;
125195}
125196
125197/*
125198** Deallocate a KeyInfo object
125199*/
125200SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo *p){
125201 if( p ){
125202 assert( p->nRef>0 );
125203 p->nRef--;
125204 if( p->nRef==0 ) sqlite3DbFreeNN(p->db, p);
125205 }
125206}
125207
125208/*
125209** Make a new pointer to a KeyInfo object
125210*/
125211SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoRef(KeyInfo *p){
125212 if( p ){
125213 assert( p->nRef>0 );
125214 p->nRef++;
125215 }
125216 return p;
125217}
125218
125219#ifdef SQLITE_DEBUG
125220/*
125221** Return TRUE if a KeyInfo object can be change. The KeyInfo object
125222** can only be changed if this is just a single reference to the object.
125223**
125224** This routine is used only inside of assert() statements.
125225*/
125226SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
125227#endif /* SQLITE_DEBUG */
125228
125229/*
125230** Given an expression list, generate a KeyInfo structure that records
125231** the collating sequence for each expression in that expression list.
125232**
125233** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
125234** KeyInfo structure is appropriate for initializing a virtual index to
125235** implement that clause. If the ExprList is the result set of a SELECT
125236** then the KeyInfo structure is appropriate for initializing a virtual
125237** index to implement a DISTINCT test.
125238**
125239** Space to hold the KeyInfo structure is obtained from malloc. The calling
125240** function is responsible for seeing that this structure is eventually
125241** freed.
125242*/
125243SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoFromExprList(
125244 Parse *pParse, /* Parsing context */
125245 ExprList *pList, /* Form the KeyInfo object from this ExprList */
125246 int iStart, /* Begin with this column of pList */
125247 int nExtra /* Add this many extra columns to the end */
125248){
125249 int nExpr;
125250 KeyInfo *pInfo;
125251 struct ExprList_item *pItem;
125252 sqlite3 *db = pParse->db;
125253 int i;
125254
125255 nExpr = pList->nExpr;
125256 pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
125257 if( pInfo ){
125258 assert( sqlite3KeyInfoIsWriteable(pInfo) );
125259 for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
125260 pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr);
125261 pInfo->aSortOrder[i-iStart] = pItem->sortOrder;
125262 }
125263 }
125264 return pInfo;
125265}
125266
125267/*
125268** Name of the connection operator, used for error messages.
125269*/
125270static const char *selectOpName(int id){
125271 char *z;
125272 switch( id ){
125273 case TK_ALL: z = "UNION ALL"; break;
125274 case TK_INTERSECT: z = "INTERSECT"; break;
125275 case TK_EXCEPT: z = "EXCEPT"; break;
125276 default: z = "UNION"; break;
125277 }
125278 return z;
125279}
125280
125281#ifndef SQLITE_OMIT_EXPLAIN
125282/*
125283** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
125284** is a no-op. Otherwise, it adds a single row of output to the EQP result,
125285** where the caption is of the form:
125286**
125287** "USE TEMP B-TREE FOR xxx"
125288**
125289** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
125290** is determined by the zUsage argument.
125291*/
125292static void explainTempTable(Parse *pParse, const char *zUsage){
125293 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage));
125294}
125295
125296/*
125297** Assign expression b to lvalue a. A second, no-op, version of this macro
125298** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
125299** in sqlite3Select() to assign values to structure member variables that
125300** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
125301** code with #ifndef directives.
125302*/
125303# define explainSetInteger(a, b) a = b
125304
125305#else
125306/* No-op versions of the explainXXX() functions and macros. */
125307# define explainTempTable(y,z)
125308# define explainSetInteger(y,z)
125309#endif
125310
125311
125312/*
125313** If the inner loop was generated using a non-null pOrderBy argument,
125314** then the results were placed in a sorter. After the loop is terminated
125315** we need to run the sorter and output the results. The following
125316** routine generates the code needed to do that.
125317*/
125318static void generateSortTail(
125319 Parse *pParse, /* Parsing context */
125320 Select *p, /* The SELECT statement */
125321 SortCtx *pSort, /* Information on the ORDER BY clause */
125322 int nColumn, /* Number of columns of data */
125323 SelectDest *pDest /* Write the sorted results here */
125324){
125325 Vdbe *v = pParse->pVdbe; /* The prepared statement */
125326 int addrBreak = pSort->labelDone; /* Jump here to exit loop */
125327 int addrContinue = sqlite3VdbeMakeLabel(v); /* Jump here for next cycle */
125328 int addr; /* Top of output loop. Jump for Next. */
125329 int addrOnce = 0;
125330 int iTab;
125331 ExprList *pOrderBy = pSort->pOrderBy;
125332 int eDest = pDest->eDest;
125333 int iParm = pDest->iSDParm;
125334 int regRow;
125335 int regRowid;
125336 int iCol;
125337 int nKey; /* Number of key columns in sorter record */
125338 int iSortTab; /* Sorter cursor to read from */
125339 int i;
125340 int bSeq; /* True if sorter record includes seq. no. */
125341 int nRefKey = 0;
125342 struct ExprList_item *aOutEx = p->pEList->a;
125343
125344 assert( addrBreak<0 );
125345 if( pSort->labelBkOut ){
125346 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
125347 sqlite3VdbeGoto(v, addrBreak);
125348 sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
125349 }
125350
125351#ifdef SQLITE_ENABLE_SORTER_REFERENCES
125352 /* Open any cursors needed for sorter-reference expressions */
125353 for(i=0; i<pSort->nDefer; i++){
125354 Table *pTab = pSort->aDefer[i].pTab;
125355 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
125356 sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead);
125357 nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey);
125358 }
125359#endif
125360
125361 iTab = pSort->iECursor;
125362 if( eDest==SRT_Output || eDest==SRT_Coroutine || eDest==SRT_Mem ){
125363 regRowid = 0;
125364 regRow = pDest->iSdst;
125365 }else{
125366 regRowid = sqlite3GetTempReg(pParse);
125367 regRow = sqlite3GetTempRange(pParse, nColumn);
125368 }
125369 nKey = pOrderBy->nExpr - pSort->nOBSat;
125370 if( pSort->sortFlags & SORTFLAG_UseSorter ){
125371 int regSortOut = ++pParse->nMem;
125372 iSortTab = pParse->nTab++;
125373 if( pSort->labelBkOut ){
125374 addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
125375 }
125376 sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut,
125377 nKey+1+nColumn+nRefKey);
125378 if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
125379 addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
125380 VdbeCoverage(v);
125381 codeOffset(v, p->iOffset, addrContinue);
125382 sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab);
125383 bSeq = 0;
125384 }else{
125385 addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
125386 codeOffset(v, p->iOffset, addrContinue);
125387 iSortTab = iTab;
125388 bSeq = 1;
125389 }
125390 for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
125391#ifdef SQLITE_ENABLE_SORTER_REFERENCES
125392 if( aOutEx[i].bSorterRef ) continue;
125393#endif
125394 if( aOutEx[i].u.x.iOrderByCol==0 ) iCol++;
125395 }
125396#ifdef SQLITE_ENABLE_SORTER_REFERENCES
125397 if( pSort->nDefer ){
125398 int iKey = iCol+1;
125399 int regKey = sqlite3GetTempRange(pParse, nRefKey);
125400
125401 for(i=0; i<pSort->nDefer; i++){
125402 int iCsr = pSort->aDefer[i].iCsr;
125403 Table *pTab = pSort->aDefer[i].pTab;
125404 int nKey = pSort->aDefer[i].nKey;
125405
125406 sqlite3VdbeAddOp1(v, OP_NullRow, iCsr);
125407 if( HasRowid(pTab) ){
125408 sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey);
125409 sqlite3VdbeAddOp3(v, OP_SeekRowid, iCsr,
125410 sqlite3VdbeCurrentAddr(v)+1, regKey);
125411 }else{
125412 int k;
125413 int iJmp;
125414 assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey );
125415 for(k=0; k<nKey; k++){
125416 sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey+k);
125417 }
125418 iJmp = sqlite3VdbeCurrentAddr(v);
125419 sqlite3VdbeAddOp4Int(v, OP_SeekGE, iCsr, iJmp+2, regKey, nKey);
125420 sqlite3VdbeAddOp4Int(v, OP_IdxLE, iCsr, iJmp+3, regKey, nKey);
125421 sqlite3VdbeAddOp1(v, OP_NullRow, iCsr);
125422 }
125423 }
125424 sqlite3ReleaseTempRange(pParse, regKey, nRefKey);
125425 }
125426#endif
125427 for(i=nColumn-1; i>=0; i--){
125428#ifdef SQLITE_ENABLE_SORTER_REFERENCES
125429 if( aOutEx[i].bSorterRef ){
125430 sqlite3ExprCode(pParse, aOutEx[i].pExpr, regRow+i);
125431 }else
125432#endif
125433 {
125434 int iRead;
125435 if( aOutEx[i].u.x.iOrderByCol ){
125436 iRead = aOutEx[i].u.x.iOrderByCol-1;
125437 }else{
125438 iRead = iCol--;
125439 }
125440 sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iRead, regRow+i);
125441 VdbeComment((v, "%s", aOutEx[i].zName?aOutEx[i].zName : aOutEx[i].zSpan));
125442 }
125443 }
125444 switch( eDest ){
125445 case SRT_Table:
125446 case SRT_EphemTab: {
125447 sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
125448 sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
125449 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
125450 break;
125451 }
125452#ifndef SQLITE_OMIT_SUBQUERY
125453 case SRT_Set: {
125454 assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) );
125455 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, nColumn, regRowid,
125456 pDest->zAffSdst, nColumn);
125457 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, regRowid, regRow, nColumn);
125458 break;
125459 }
125460 case SRT_Mem: {
125461 /* The LIMIT clause will terminate the loop for us */
125462 break;
125463 }
125464#endif
125465 default: {
125466 assert( eDest==SRT_Output || eDest==SRT_Coroutine );
125467 testcase( eDest==SRT_Output );
125468 testcase( eDest==SRT_Coroutine );
125469 if( eDest==SRT_Output ){
125470 sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
125471 }else{
125472 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
125473 }
125474 break;
125475 }
125476 }
125477 if( regRowid ){
125478 if( eDest==SRT_Set ){
125479 sqlite3ReleaseTempRange(pParse, regRow, nColumn);
125480 }else{
125481 sqlite3ReleaseTempReg(pParse, regRow);
125482 }
125483 sqlite3ReleaseTempReg(pParse, regRowid);
125484 }
125485 /* The bottom of the loop
125486 */
125487 sqlite3VdbeResolveLabel(v, addrContinue);
125488 if( pSort->sortFlags & SORTFLAG_UseSorter ){
125489 sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr); VdbeCoverage(v);
125490 }else{
125491 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr); VdbeCoverage(v);
125492 }
125493 if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return, pSort->regReturn);
125494 sqlite3VdbeResolveLabel(v, addrBreak);
125495}
125496
125497/*
125498** Return a pointer to a string containing the 'declaration type' of the
125499** expression pExpr. The string may be treated as static by the caller.
125500**
125501** Also try to estimate the size of the returned value and return that
125502** result in *pEstWidth.
125503**
125504** The declaration type is the exact datatype definition extracted from the
125505** original CREATE TABLE statement if the expression is a column. The
125506** declaration type for a ROWID field is INTEGER. Exactly when an expression
125507** is considered a column can be complex in the presence of subqueries. The
125508** result-set expression in all of the following SELECT statements is
125509** considered a column by this function.
125510**
125511** SELECT col FROM tbl;
125512** SELECT (SELECT col FROM tbl;
125513** SELECT (SELECT col FROM tbl);
125514** SELECT abc FROM (SELECT col AS abc FROM tbl);
125515**
125516** The declaration type for any expression other than a column is NULL.
125517**
125518** This routine has either 3 or 6 parameters depending on whether or not
125519** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
125520*/
125521#ifdef SQLITE_ENABLE_COLUMN_METADATA
125522# define columnType(A,B,C,D,E) columnTypeImpl(A,B,C,D,E)
125523#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
125524# define columnType(A,B,C,D,E) columnTypeImpl(A,B)
125525#endif
125526static const char *columnTypeImpl(
125527 NameContext *pNC,
125528#ifndef SQLITE_ENABLE_COLUMN_METADATA
125529 Expr *pExpr
125530#else
125531 Expr *pExpr,
125532 const char **pzOrigDb,
125533 const char **pzOrigTab,
125534 const char **pzOrigCol
125535#endif
125536){
125537 char const *zType = 0;
125538 int j;
125539#ifdef SQLITE_ENABLE_COLUMN_METADATA
125540 char const *zOrigDb = 0;
125541 char const *zOrigTab = 0;
125542 char const *zOrigCol = 0;
125543#endif
125544
125545 assert( pExpr!=0 );
125546 assert( pNC->pSrcList!=0 );
125547 assert( pExpr->op!=TK_AGG_COLUMN ); /* This routine runes before aggregates
125548 ** are processed */
125549 switch( pExpr->op ){
125550 case TK_COLUMN: {
125551 /* The expression is a column. Locate the table the column is being
125552 ** extracted from in NameContext.pSrcList. This table may be real
125553 ** database table or a subquery.
125554 */
125555 Table *pTab = 0; /* Table structure column is extracted from */
125556 Select *pS = 0; /* Select the column is extracted from */
125557 int iCol = pExpr->iColumn; /* Index of column in pTab */
125558 while( pNC && !pTab ){
125559 SrcList *pTabList = pNC->pSrcList;
125560 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
125561 if( j<pTabList->nSrc ){
125562 pTab = pTabList->a[j].pTab;
125563 pS = pTabList->a[j].pSelect;
125564 }else{
125565 pNC = pNC->pNext;
125566 }
125567 }
125568
125569 if( pTab==0 ){
125570 /* At one time, code such as "SELECT new.x" within a trigger would
125571 ** cause this condition to run. Since then, we have restructured how
125572 ** trigger code is generated and so this condition is no longer
125573 ** possible. However, it can still be true for statements like
125574 ** the following:
125575 **
125576 ** CREATE TABLE t1(col INTEGER);
125577 ** SELECT (SELECT t1.col) FROM FROM t1;
125578 **
125579 ** when columnType() is called on the expression "t1.col" in the
125580 ** sub-select. In this case, set the column type to NULL, even
125581 ** though it should really be "INTEGER".
125582 **
125583 ** This is not a problem, as the column type of "t1.col" is never
125584 ** used. When columnType() is called on the expression
125585 ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
125586 ** branch below. */
125587 break;
125588 }
125589
125590 assert( pTab && pExpr->y.pTab==pTab );
125591 if( pS ){
125592 /* The "table" is actually a sub-select or a view in the FROM clause
125593 ** of the SELECT statement. Return the declaration type and origin
125594 ** data for the result-set column of the sub-select.
125595 */
125596 if( iCol>=0 && iCol<pS->pEList->nExpr ){
125597 /* If iCol is less than zero, then the expression requests the
125598 ** rowid of the sub-select or view. This expression is legal (see
125599 ** test case misc2.2.2) - it always evaluates to NULL.
125600 */
125601 NameContext sNC;
125602 Expr *p = pS->pEList->a[iCol].pExpr;
125603 sNC.pSrcList = pS->pSrc;
125604 sNC.pNext = pNC;
125605 sNC.pParse = pNC->pParse;
125606 zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol);
125607 }
125608 }else{
125609 /* A real table or a CTE table */
125610 assert( !pS );
125611#ifdef SQLITE_ENABLE_COLUMN_METADATA
125612 if( iCol<0 ) iCol = pTab->iPKey;
125613 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
125614 if( iCol<0 ){
125615 zType = "INTEGER";
125616 zOrigCol = "rowid";
125617 }else{
125618 zOrigCol = pTab->aCol[iCol].zName;
125619 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
125620 }
125621 zOrigTab = pTab->zName;
125622 if( pNC->pParse && pTab->pSchema ){
125623 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
125624 zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
125625 }
125626#else
125627 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
125628 if( iCol<0 ){
125629 zType = "INTEGER";
125630 }else{
125631 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
125632 }
125633#endif
125634 }
125635 break;
125636 }
125637#ifndef SQLITE_OMIT_SUBQUERY
125638 case TK_SELECT: {
125639 /* The expression is a sub-select. Return the declaration type and
125640 ** origin info for the single column in the result set of the SELECT
125641 ** statement.
125642 */
125643 NameContext sNC;
125644 Select *pS = pExpr->x.pSelect;
125645 Expr *p = pS->pEList->a[0].pExpr;
125646 assert( ExprHasProperty(pExpr, EP_xIsSelect) );
125647 sNC.pSrcList = pS->pSrc;
125648 sNC.pNext = pNC;
125649 sNC.pParse = pNC->pParse;
125650 zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
125651 break;
125652 }
125653#endif
125654 }
125655
125656#ifdef SQLITE_ENABLE_COLUMN_METADATA
125657 if( pzOrigDb ){
125658 assert( pzOrigTab && pzOrigCol );
125659 *pzOrigDb = zOrigDb;
125660 *pzOrigTab = zOrigTab;
125661 *pzOrigCol = zOrigCol;
125662 }
125663#endif
125664 return zType;
125665}
125666
125667/*
125668** Generate code that will tell the VDBE the declaration types of columns
125669** in the result set.
125670*/
125671static void generateColumnTypes(
125672 Parse *pParse, /* Parser context */
125673 SrcList *pTabList, /* List of tables */
125674 ExprList *pEList /* Expressions defining the result set */
125675){
125676#ifndef SQLITE_OMIT_DECLTYPE
125677 Vdbe *v = pParse->pVdbe;
125678 int i;
125679 NameContext sNC;
125680 sNC.pSrcList = pTabList;
125681 sNC.pParse = pParse;
125682 sNC.pNext = 0;
125683 for(i=0; i<pEList->nExpr; i++){
125684 Expr *p = pEList->a[i].pExpr;
125685 const char *zType;
125686#ifdef SQLITE_ENABLE_COLUMN_METADATA
125687 const char *zOrigDb = 0;
125688 const char *zOrigTab = 0;
125689 const char *zOrigCol = 0;
125690 zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
125691
125692 /* The vdbe must make its own copy of the column-type and other
125693 ** column specific strings, in case the schema is reset before this
125694 ** virtual machine is deleted.
125695 */
125696 sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT);
125697 sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
125698 sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
125699#else
125700 zType = columnType(&sNC, p, 0, 0, 0);
125701#endif
125702 sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
125703 }
125704#endif /* !defined(SQLITE_OMIT_DECLTYPE) */
125705}
125706
125707
125708/*
125709** Compute the column names for a SELECT statement.
125710**
125711** The only guarantee that SQLite makes about column names is that if the
125712** column has an AS clause assigning it a name, that will be the name used.
125713** That is the only documented guarantee. However, countless applications
125714** developed over the years have made baseless assumptions about column names
125715** and will break if those assumptions changes. Hence, use extreme caution
125716** when modifying this routine to avoid breaking legacy.
125717**
125718** See Also: sqlite3ColumnsFromExprList()
125719**
125720** The PRAGMA short_column_names and PRAGMA full_column_names settings are
125721** deprecated. The default setting is short=ON, full=OFF. 99.9% of all
125722** applications should operate this way. Nevertheless, we need to support the
125723** other modes for legacy:
125724**
125725** short=OFF, full=OFF: Column name is the text of the expression has it
125726** originally appears in the SELECT statement. In
125727** other words, the zSpan of the result expression.
125728**
125729** short=ON, full=OFF: (This is the default setting). If the result
125730** refers directly to a table column, then the
125731** result column name is just the table column
125732** name: COLUMN. Otherwise use zSpan.
125733**
125734** full=ON, short=ANY: If the result refers directly to a table column,
125735** then the result column name with the table name
125736** prefix, ex: TABLE.COLUMN. Otherwise use zSpan.
125737*/
125738static void generateColumnNames(
125739 Parse *pParse, /* Parser context */
125740 Select *pSelect /* Generate column names for this SELECT statement */
125741){
125742 Vdbe *v = pParse->pVdbe;
125743 int i;
125744 Table *pTab;
125745 SrcList *pTabList;
125746 ExprList *pEList;
125747 sqlite3 *db = pParse->db;
125748 int fullName; /* TABLE.COLUMN if no AS clause and is a direct table ref */
125749 int srcName; /* COLUMN or TABLE.COLUMN if no AS clause and is direct */
125750
125751#ifndef SQLITE_OMIT_EXPLAIN
125752 /* If this is an EXPLAIN, skip this step */
125753 if( pParse->explain ){
125754 return;
125755 }
125756#endif
125757
125758 if( pParse->colNamesSet ) return;
125759 /* Column names are determined by the left-most term of a compound select */
125760 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
125761 SELECTTRACE(1,pParse,pSelect,("generating column names\n"));
125762 pTabList = pSelect->pSrc;
125763 pEList = pSelect->pEList;
125764 assert( v!=0 );
125765 assert( pTabList!=0 );
125766 pParse->colNamesSet = 1;
125767 fullName = (db->flags & SQLITE_FullColNames)!=0;
125768 srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
125769 sqlite3VdbeSetNumCols(v, pEList->nExpr);
125770 for(i=0; i<pEList->nExpr; i++){
125771 Expr *p = pEList->a[i].pExpr;
125772
125773 assert( p!=0 );
125774 assert( p->op!=TK_AGG_COLUMN ); /* Agg processing has not run yet */
125775 assert( p->op!=TK_COLUMN || p->y.pTab!=0 ); /* Covering idx not yet coded */
125776 if( pEList->a[i].zName ){
125777 /* An AS clause always takes first priority */
125778 char *zName = pEList->a[i].zName;
125779 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
125780 }else if( srcName && p->op==TK_COLUMN ){
125781 char *zCol;
125782 int iCol = p->iColumn;
125783 pTab = p->y.pTab;
125784 assert( pTab!=0 );
125785 if( iCol<0 ) iCol = pTab->iPKey;
125786 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
125787 if( iCol<0 ){
125788 zCol = "rowid";
125789 }else{
125790 zCol = pTab->aCol[iCol].zName;
125791 }
125792 if( fullName ){
125793 char *zName = 0;
125794 zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
125795 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC);
125796 }else{
125797 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT);
125798 }
125799 }else{
125800 const char *z = pEList->a[i].zSpan;
125801 z = z==0 ? sqlite3MPrintf(db, "column%d", i+1) : sqlite3DbStrDup(db, z);
125802 sqlite3VdbeSetColName(v, i, COLNAME_NAME, z, SQLITE_DYNAMIC);
125803 }
125804 }
125805 generateColumnTypes(pParse, pTabList, pEList);
125806}
125807
125808/*
125809** Given an expression list (which is really the list of expressions
125810** that form the result set of a SELECT statement) compute appropriate
125811** column names for a table that would hold the expression list.
125812**
125813** All column names will be unique.
125814**
125815** Only the column names are computed. Column.zType, Column.zColl,
125816** and other fields of Column are zeroed.
125817**
125818** Return SQLITE_OK on success. If a memory allocation error occurs,
125819** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
125820**
125821** The only guarantee that SQLite makes about column names is that if the
125822** column has an AS clause assigning it a name, that will be the name used.
125823** That is the only documented guarantee. However, countless applications
125824** developed over the years have made baseless assumptions about column names
125825** and will break if those assumptions changes. Hence, use extreme caution
125826** when modifying this routine to avoid breaking legacy.
125827**
125828** See Also: generateColumnNames()
125829*/
125830SQLITE_PRIVATE int sqlite3ColumnsFromExprList(
125831 Parse *pParse, /* Parsing context */
125832 ExprList *pEList, /* Expr list from which to derive column names */
125833 i16 *pnCol, /* Write the number of columns here */
125834 Column **paCol /* Write the new column list here */
125835){
125836 sqlite3 *db = pParse->db; /* Database connection */
125837 int i, j; /* Loop counters */
125838 u32 cnt; /* Index added to make the name unique */
125839 Column *aCol, *pCol; /* For looping over result columns */
125840 int nCol; /* Number of columns in the result set */
125841 char *zName; /* Column name */
125842 int nName; /* Size of name in zName[] */
125843 Hash ht; /* Hash table of column names */
125844
125845 sqlite3HashInit(&ht);
125846 if( pEList ){
125847 nCol = pEList->nExpr;
125848 aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
125849 testcase( aCol==0 );
125850 if( nCol>32767 ) nCol = 32767;
125851 }else{
125852 nCol = 0;
125853 aCol = 0;
125854 }
125855 assert( nCol==(i16)nCol );
125856 *pnCol = nCol;
125857 *paCol = aCol;
125858
125859 for(i=0, pCol=aCol; i<nCol && !db->mallocFailed; i++, pCol++){
125860 /* Get an appropriate name for the column
125861 */
125862 if( (zName = pEList->a[i].zName)!=0 ){
125863 /* If the column contains an "AS <name>" phrase, use <name> as the name */
125864 }else{
125865 Expr *pColExpr = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
125866 while( pColExpr->op==TK_DOT ){
125867 pColExpr = pColExpr->pRight;
125868 assert( pColExpr!=0 );
125869 }
125870 assert( pColExpr->op!=TK_AGG_COLUMN );
125871 if( pColExpr->op==TK_COLUMN ){
125872 /* For columns use the column name name */
125873 int iCol = pColExpr->iColumn;
125874 Table *pTab = pColExpr->y.pTab;
125875 assert( pTab!=0 );
125876 if( iCol<0 ) iCol = pTab->iPKey;
125877 zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid";
125878 }else if( pColExpr->op==TK_ID ){
125879 assert( !ExprHasProperty(pColExpr, EP_IntValue) );
125880 zName = pColExpr->u.zToken;
125881 }else{
125882 /* Use the original text of the column expression as its name */
125883 zName = pEList->a[i].zSpan;
125884 }
125885 }
125886 if( zName ){
125887 zName = sqlite3DbStrDup(db, zName);
125888 }else{
125889 zName = sqlite3MPrintf(db,"column%d",i+1);
125890 }
125891
125892 /* Make sure the column name is unique. If the name is not unique,
125893 ** append an integer to the name so that it becomes unique.
125894 */
125895 cnt = 0;
125896 while( zName && sqlite3HashFind(&ht, zName)!=0 ){
125897 nName = sqlite3Strlen30(zName);
125898 if( nName>0 ){
125899 for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
125900 if( zName[j]==':' ) nName = j;
125901 }
125902 zName = sqlite3MPrintf(db, "%.*z:%u", nName, zName, ++cnt);
125903 if( cnt>3 ) sqlite3_randomness(sizeof(cnt), &cnt);
125904 }
125905 pCol->zName = zName;
125906 sqlite3ColumnPropertiesFromName(0, pCol);
125907 if( zName && sqlite3HashInsert(&ht, zName, pCol)==pCol ){
125908 sqlite3OomFault(db);
125909 }
125910 }
125911 sqlite3HashClear(&ht);
125912 if( db->mallocFailed ){
125913 for(j=0; j<i; j++){
125914 sqlite3DbFree(db, aCol[j].zName);
125915 }
125916 sqlite3DbFree(db, aCol);
125917 *paCol = 0;
125918 *pnCol = 0;
125919 return SQLITE_NOMEM_BKPT;
125920 }
125921 return SQLITE_OK;
125922}
125923
125924/*
125925** Add type and collation information to a column list based on
125926** a SELECT statement.
125927**
125928** The column list presumably came from selectColumnNamesFromExprList().
125929** The column list has only names, not types or collations. This
125930** routine goes through and adds the types and collations.
125931**
125932** This routine requires that all identifiers in the SELECT
125933** statement be resolved.
125934*/
125935SQLITE_PRIVATE void sqlite3SelectAddColumnTypeAndCollation(
125936 Parse *pParse, /* Parsing contexts */
125937 Table *pTab, /* Add column type information to this table */
125938 Select *pSelect /* SELECT used to determine types and collations */
125939){
125940 sqlite3 *db = pParse->db;
125941 NameContext sNC;
125942 Column *pCol;
125943 CollSeq *pColl;
125944 int i;
125945 Expr *p;
125946 struct ExprList_item *a;
125947
125948 assert( pSelect!=0 );
125949 assert( (pSelect->selFlags & SF_Resolved)!=0 );
125950 assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed );
125951 if( db->mallocFailed ) return;
125952 memset(&sNC, 0, sizeof(sNC));
125953 sNC.pSrcList = pSelect->pSrc;
125954 a = pSelect->pEList->a;
125955 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
125956 const char *zType;
125957 int n, m;
125958 p = a[i].pExpr;
125959 zType = columnType(&sNC, p, 0, 0, 0);
125960 /* pCol->szEst = ... // Column size est for SELECT tables never used */
125961 pCol->affinity = sqlite3ExprAffinity(p);
125962 if( zType ){
125963 m = sqlite3Strlen30(zType);
125964 n = sqlite3Strlen30(pCol->zName);
125965 pCol->zName = sqlite3DbReallocOrFree(db, pCol->zName, n+m+2);
125966 if( pCol->zName ){
125967 memcpy(&pCol->zName[n+1], zType, m+1);
125968 pCol->colFlags |= COLFLAG_HASTYPE;
125969 }
125970 }
125971 if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_BLOB;
125972 pColl = sqlite3ExprCollSeq(pParse, p);
125973 if( pColl && pCol->zColl==0 ){
125974 pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
125975 }
125976 }
125977 pTab->szTabRow = 1; /* Any non-zero value works */
125978}
125979
125980/*
125981** Given a SELECT statement, generate a Table structure that describes
125982** the result set of that SELECT.
125983*/
125984SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){
125985 Table *pTab;
125986 sqlite3 *db = pParse->db;
125987 int savedFlags;
125988
125989 savedFlags = db->flags;
125990 db->flags &= ~SQLITE_FullColNames;
125991 db->flags |= SQLITE_ShortColNames;
125992 sqlite3SelectPrep(pParse, pSelect, 0);
125993 if( pParse->nErr ) return 0;
125994 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
125995 db->flags = savedFlags;
125996 pTab = sqlite3DbMallocZero(db, sizeof(Table) );
125997 if( pTab==0 ){
125998 return 0;
125999 }
126000 /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
126001 ** is disabled */
126002 assert( db->lookaside.bDisable );
126003 pTab->nTabRef = 1;
126004 pTab->zName = 0;
126005 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
126006 sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
126007 sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSelect);
126008 pTab->iPKey = -1;
126009 if( db->mallocFailed ){
126010 sqlite3DeleteTable(db, pTab);
126011 return 0;
126012 }
126013 return pTab;
126014}
126015
126016/*
126017** Get a VDBE for the given parser context. Create a new one if necessary.
126018** If an error occurs, return NULL and leave a message in pParse.
126019*/
126020SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){
126021 if( pParse->pVdbe ){
126022 return pParse->pVdbe;
126023 }
126024 if( pParse->pToplevel==0
126025 && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
126026 ){
126027 pParse->okConstFactor = 1;
126028 }
126029 return sqlite3VdbeCreate(pParse);
126030}
126031
126032
126033/*
126034** Compute the iLimit and iOffset fields of the SELECT based on the
126035** pLimit expressions. pLimit->pLeft and pLimit->pRight hold the expressions
126036** that appear in the original SQL statement after the LIMIT and OFFSET
126037** keywords. Or NULL if those keywords are omitted. iLimit and iOffset
126038** are the integer memory register numbers for counters used to compute
126039** the limit and offset. If there is no limit and/or offset, then
126040** iLimit and iOffset are negative.
126041**
126042** This routine changes the values of iLimit and iOffset only if
126043** a limit or offset is defined by pLimit->pLeft and pLimit->pRight. iLimit
126044** and iOffset should have been preset to appropriate default values (zero)
126045** prior to calling this routine.
126046**
126047** The iOffset register (if it exists) is initialized to the value
126048** of the OFFSET. The iLimit register is initialized to LIMIT. Register
126049** iOffset+1 is initialized to LIMIT+OFFSET.
126050**
126051** Only if pLimit->pLeft!=0 do the limit registers get
126052** redefined. The UNION ALL operator uses this property to force
126053** the reuse of the same limit and offset registers across multiple
126054** SELECT statements.
126055*/
126056static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
126057 Vdbe *v = 0;
126058 int iLimit = 0;
126059 int iOffset;
126060 int n;
126061 Expr *pLimit = p->pLimit;
126062
126063 if( p->iLimit ) return;
126064
126065 /*
126066 ** "LIMIT -1" always shows all rows. There is some
126067 ** controversy about what the correct behavior should be.
126068 ** The current implementation interprets "LIMIT 0" to mean
126069 ** no rows.
126070 */
126071 if( pLimit ){
126072 assert( pLimit->op==TK_LIMIT );
126073 assert( pLimit->pLeft!=0 );
126074 p->iLimit = iLimit = ++pParse->nMem;
126075 v = sqlite3GetVdbe(pParse);
126076 assert( v!=0 );
126077 if( sqlite3ExprIsInteger(pLimit->pLeft, &n) ){
126078 sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit);
126079 VdbeComment((v, "LIMIT counter"));
126080 if( n==0 ){
126081 sqlite3VdbeGoto(v, iBreak);
126082 }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){
126083 p->nSelectRow = sqlite3LogEst((u64)n);
126084 p->selFlags |= SF_FixedLimit;
126085 }
126086 }else{
126087 sqlite3ExprCode(pParse, pLimit->pLeft, iLimit);
126088 sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); VdbeCoverage(v);
126089 VdbeComment((v, "LIMIT counter"));
126090 sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, iBreak); VdbeCoverage(v);
126091 }
126092 if( pLimit->pRight ){
126093 p->iOffset = iOffset = ++pParse->nMem;
126094 pParse->nMem++; /* Allocate an extra register for limit+offset */
126095 sqlite3ExprCode(pParse, pLimit->pRight, iOffset);
126096 sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); VdbeCoverage(v);
126097 VdbeComment((v, "OFFSET counter"));
126098 sqlite3VdbeAddOp3(v, OP_OffsetLimit, iLimit, iOffset+1, iOffset);
126099 VdbeComment((v, "LIMIT+OFFSET"));
126100 }
126101 }
126102}
126103
126104#ifndef SQLITE_OMIT_COMPOUND_SELECT
126105/*
126106** Return the appropriate collating sequence for the iCol-th column of
126107** the result set for the compound-select statement "p". Return NULL if
126108** the column has no default collating sequence.
126109**
126110** The collating sequence for the compound select is taken from the
126111** left-most term of the select that has a collating sequence.
126112*/
126113static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
126114 CollSeq *pRet;
126115 if( p->pPrior ){
126116 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
126117 }else{
126118 pRet = 0;
126119 }
126120 assert( iCol>=0 );
126121 /* iCol must be less than p->pEList->nExpr. Otherwise an error would
126122 ** have been thrown during name resolution and we would not have gotten
126123 ** this far */
126124 if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
126125 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
126126 }
126127 return pRet;
126128}
126129
126130/*
126131** The select statement passed as the second parameter is a compound SELECT
126132** with an ORDER BY clause. This function allocates and returns a KeyInfo
126133** structure suitable for implementing the ORDER BY.
126134**
126135** Space to hold the KeyInfo structure is obtained from malloc. The calling
126136** function is responsible for ensuring that this structure is eventually
126137** freed.
126138*/
126139static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){
126140 ExprList *pOrderBy = p->pOrderBy;
126141 int nOrderBy = p->pOrderBy->nExpr;
126142 sqlite3 *db = pParse->db;
126143 KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1);
126144 if( pRet ){
126145 int i;
126146 for(i=0; i<nOrderBy; i++){
126147 struct ExprList_item *pItem = &pOrderBy->a[i];
126148 Expr *pTerm = pItem->pExpr;
126149 CollSeq *pColl;
126150
126151 if( pTerm->flags & EP_Collate ){
126152 pColl = sqlite3ExprCollSeq(pParse, pTerm);
126153 }else{
126154 pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
126155 if( pColl==0 ) pColl = db->pDfltColl;
126156 pOrderBy->a[i].pExpr =
126157 sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
126158 }
126159 assert( sqlite3KeyInfoIsWriteable(pRet) );
126160 pRet->aColl[i] = pColl;
126161 pRet->aSortOrder[i] = pOrderBy->a[i].sortOrder;
126162 }
126163 }
126164
126165 return pRet;
126166}
126167
126168#ifndef SQLITE_OMIT_CTE
126169/*
126170** This routine generates VDBE code to compute the content of a WITH RECURSIVE
126171** query of the form:
126172**
126173** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
126174** \___________/ \_______________/
126175** p->pPrior p
126176**
126177**
126178** There is exactly one reference to the recursive-table in the FROM clause
126179** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
126180**
126181** The setup-query runs once to generate an initial set of rows that go
126182** into a Queue table. Rows are extracted from the Queue table one by
126183** one. Each row extracted from Queue is output to pDest. Then the single
126184** extracted row (now in the iCurrent table) becomes the content of the
126185** recursive-table for a recursive-query run. The output of the recursive-query
126186** is added back into the Queue table. Then another row is extracted from Queue
126187** and the iteration continues until the Queue table is empty.
126188**
126189** If the compound query operator is UNION then no duplicate rows are ever
126190** inserted into the Queue table. The iDistinct table keeps a copy of all rows
126191** that have ever been inserted into Queue and causes duplicates to be
126192** discarded. If the operator is UNION ALL, then duplicates are allowed.
126193**
126194** If the query has an ORDER BY, then entries in the Queue table are kept in
126195** ORDER BY order and the first entry is extracted for each cycle. Without
126196** an ORDER BY, the Queue table is just a FIFO.
126197**
126198** If a LIMIT clause is provided, then the iteration stops after LIMIT rows
126199** have been output to pDest. A LIMIT of zero means to output no rows and a
126200** negative LIMIT means to output all rows. If there is also an OFFSET clause
126201** with a positive value, then the first OFFSET outputs are discarded rather
126202** than being sent to pDest. The LIMIT count does not begin until after OFFSET
126203** rows have been skipped.
126204*/
126205static void generateWithRecursiveQuery(
126206 Parse *pParse, /* Parsing context */
126207 Select *p, /* The recursive SELECT to be coded */
126208 SelectDest *pDest /* What to do with query results */
126209){
126210 SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */
126211 int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */
126212 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
126213 Select *pSetup = p->pPrior; /* The setup query */
126214 int addrTop; /* Top of the loop */
126215 int addrCont, addrBreak; /* CONTINUE and BREAK addresses */
126216 int iCurrent = 0; /* The Current table */
126217 int regCurrent; /* Register holding Current table */
126218 int iQueue; /* The Queue table */
126219 int iDistinct = 0; /* To ensure unique results if UNION */
126220 int eDest = SRT_Fifo; /* How to write to Queue */
126221 SelectDest destQueue; /* SelectDest targetting the Queue table */
126222 int i; /* Loop counter */
126223 int rc; /* Result code */
126224 ExprList *pOrderBy; /* The ORDER BY clause */
126225 Expr *pLimit; /* Saved LIMIT and OFFSET */
126226 int regLimit, regOffset; /* Registers used by LIMIT and OFFSET */
126227
126228#ifndef SQLITE_OMIT_WINDOWFUNC
126229 if( p->pWin ){
126230 sqlite3ErrorMsg(pParse, "cannot use window functions in recursive queries");
126231 return;
126232 }
126233#endif
126234
126235 /* Obtain authorization to do a recursive query */
126236 if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE, 0, 0, 0) ) return;
126237
126238 /* Process the LIMIT and OFFSET clauses, if they exist */
126239 addrBreak = sqlite3VdbeMakeLabel(v);
126240 p->nSelectRow = 320; /* 4 billion rows */
126241 computeLimitRegisters(pParse, p, addrBreak);
126242 pLimit = p->pLimit;
126243 regLimit = p->iLimit;
126244 regOffset = p->iOffset;
126245 p->pLimit = 0;
126246 p->iLimit = p->iOffset = 0;
126247 pOrderBy = p->pOrderBy;
126248
126249 /* Locate the cursor number of the Current table */
126250 for(i=0; ALWAYS(i<pSrc->nSrc); i++){
126251 if( pSrc->a[i].fg.isRecursive ){
126252 iCurrent = pSrc->a[i].iCursor;
126253 break;
126254 }
126255 }
126256
126257 /* Allocate cursors numbers for Queue and Distinct. The cursor number for
126258 ** the Distinct table must be exactly one greater than Queue in order
126259 ** for the SRT_DistFifo and SRT_DistQueue destinations to work. */
126260 iQueue = pParse->nTab++;
126261 if( p->op==TK_UNION ){
126262 eDest = pOrderBy ? SRT_DistQueue : SRT_DistFifo;
126263 iDistinct = pParse->nTab++;
126264 }else{
126265 eDest = pOrderBy ? SRT_Queue : SRT_Fifo;
126266 }
126267 sqlite3SelectDestInit(&destQueue, eDest, iQueue);
126268
126269 /* Allocate cursors for Current, Queue, and Distinct. */
126270 regCurrent = ++pParse->nMem;
126271 sqlite3VdbeAddOp3(v, OP_OpenPseudo, iCurrent, regCurrent, nCol);
126272 if( pOrderBy ){
126273 KeyInfo *pKeyInfo = multiSelectOrderByKeyInfo(pParse, p, 1);
126274 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
126275 (char*)pKeyInfo, P4_KEYINFO);
126276 destQueue.pOrderBy = pOrderBy;
126277 }else{
126278 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iQueue, nCol);
126279 }
126280 VdbeComment((v, "Queue table"));
126281 if( iDistinct ){
126282 p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
126283 p->selFlags |= SF_UsesEphemeral;
126284 }
126285
126286 /* Detach the ORDER BY clause from the compound SELECT */
126287 p->pOrderBy = 0;
126288
126289 /* Store the results of the setup-query in Queue. */
126290 pSetup->pNext = 0;
126291 ExplainQueryPlan((pParse, 1, "SETUP"));
126292 rc = sqlite3Select(pParse, pSetup, &destQueue);
126293 pSetup->pNext = p;
126294 if( rc ) goto end_of_recursive_query;
126295
126296 /* Find the next row in the Queue and output that row */
126297 addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iQueue, addrBreak); VdbeCoverage(v);
126298
126299 /* Transfer the next row in Queue over to Current */
126300 sqlite3VdbeAddOp1(v, OP_NullRow, iCurrent); /* To reset column cache */
126301 if( pOrderBy ){
126302 sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
126303 }else{
126304 sqlite3VdbeAddOp2(v, OP_RowData, iQueue, regCurrent);
126305 }
126306 sqlite3VdbeAddOp1(v, OP_Delete, iQueue);
126307
126308 /* Output the single row in Current */
126309 addrCont = sqlite3VdbeMakeLabel(v);
126310 codeOffset(v, regOffset, addrCont);
126311 selectInnerLoop(pParse, p, iCurrent,
126312 0, 0, pDest, addrCont, addrBreak);
126313 if( regLimit ){
126314 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, regLimit, addrBreak);
126315 VdbeCoverage(v);
126316 }
126317 sqlite3VdbeResolveLabel(v, addrCont);
126318
126319 /* Execute the recursive SELECT taking the single row in Current as
126320 ** the value for the recursive-table. Store the results in the Queue.
126321 */
126322 if( p->selFlags & SF_Aggregate ){
126323 sqlite3ErrorMsg(pParse, "recursive aggregate queries not supported");
126324 }else{
126325 p->pPrior = 0;
126326 ExplainQueryPlan((pParse, 1, "RECURSIVE STEP"));
126327 sqlite3Select(pParse, p, &destQueue);
126328 assert( p->pPrior==0 );
126329 p->pPrior = pSetup;
126330 }
126331
126332 /* Keep running the loop until the Queue is empty */
126333 sqlite3VdbeGoto(v, addrTop);
126334 sqlite3VdbeResolveLabel(v, addrBreak);
126335
126336end_of_recursive_query:
126337 sqlite3ExprListDelete(pParse->db, p->pOrderBy);
126338 p->pOrderBy = pOrderBy;
126339 p->pLimit = pLimit;
126340 return;
126341}
126342#endif /* SQLITE_OMIT_CTE */
126343
126344/* Forward references */
126345static int multiSelectOrderBy(
126346 Parse *pParse, /* Parsing context */
126347 Select *p, /* The right-most of SELECTs to be coded */
126348 SelectDest *pDest /* What to do with query results */
126349);
126350
126351/*
126352** Handle the special case of a compound-select that originates from a
126353** VALUES clause. By handling this as a special case, we avoid deep
126354** recursion, and thus do not need to enforce the SQLITE_LIMIT_COMPOUND_SELECT
126355** on a VALUES clause.
126356**
126357** Because the Select object originates from a VALUES clause:
126358** (1) There is no LIMIT or OFFSET or else there is a LIMIT of exactly 1
126359** (2) All terms are UNION ALL
126360** (3) There is no ORDER BY clause
126361**
126362** The "LIMIT of exactly 1" case of condition (1) comes about when a VALUES
126363** clause occurs within scalar expression (ex: "SELECT (VALUES(1),(2),(3))").
126364** The sqlite3CodeSubselect will have added the LIMIT 1 clause in tht case.
126365** Since the limit is exactly 1, we only need to evalutes the left-most VALUES.
126366*/
126367static int multiSelectValues(
126368 Parse *pParse, /* Parsing context */
126369 Select *p, /* The right-most of SELECTs to be coded */
126370 SelectDest *pDest /* What to do with query results */
126371){
126372 int nRow = 1;
126373 int rc = 0;
126374 int bShowAll = p->pLimit==0;
126375 assert( p->selFlags & SF_MultiValue );
126376 do{
126377 assert( p->selFlags & SF_Values );
126378 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
126379 assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
126380 if( p->pPrior==0 ) break;
126381 assert( p->pPrior->pNext==p );
126382 p = p->pPrior;
126383 nRow += bShowAll;
126384 }while(1);
126385 ExplainQueryPlan((pParse, 0, "SCAN %d CONSTANT ROW%s", nRow,
126386 nRow==1 ? "" : "S"));
126387 while( p ){
126388 selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1);
126389 if( !bShowAll ) break;
126390 p->nSelectRow = nRow;
126391 p = p->pNext;
126392 }
126393 return rc;
126394}
126395
126396/*
126397** This routine is called to process a compound query form from
126398** two or more separate queries using UNION, UNION ALL, EXCEPT, or
126399** INTERSECT
126400**
126401** "p" points to the right-most of the two queries. the query on the
126402** left is p->pPrior. The left query could also be a compound query
126403** in which case this routine will be called recursively.
126404**
126405** The results of the total query are to be written into a destination
126406** of type eDest with parameter iParm.
126407**
126408** Example 1: Consider a three-way compound SQL statement.
126409**
126410** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
126411**
126412** This statement is parsed up as follows:
126413**
126414** SELECT c FROM t3
126415** |
126416** `-----> SELECT b FROM t2
126417** |
126418** `------> SELECT a FROM t1
126419**
126420** The arrows in the diagram above represent the Select.pPrior pointer.
126421** So if this routine is called with p equal to the t3 query, then
126422** pPrior will be the t2 query. p->op will be TK_UNION in this case.
126423**
126424** Notice that because of the way SQLite parses compound SELECTs, the
126425** individual selects always group from left to right.
126426*/
126427static int multiSelect(
126428 Parse *pParse, /* Parsing context */
126429 Select *p, /* The right-most of SELECTs to be coded */
126430 SelectDest *pDest /* What to do with query results */
126431){
126432 int rc = SQLITE_OK; /* Success code from a subroutine */
126433 Select *pPrior; /* Another SELECT immediately to our left */
126434 Vdbe *v; /* Generate code to this VDBE */
126435 SelectDest dest; /* Alternative data destination */
126436 Select *pDelete = 0; /* Chain of simple selects to delete */
126437 sqlite3 *db; /* Database connection */
126438
126439 /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only
126440 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
126441 */
126442 assert( p && p->pPrior ); /* Calling function guarantees this much */
126443 assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
126444 db = pParse->db;
126445 pPrior = p->pPrior;
126446 dest = *pDest;
126447 if( pPrior->pOrderBy || pPrior->pLimit ){
126448 sqlite3ErrorMsg(pParse,"%s clause should come after %s not before",
126449 pPrior->pOrderBy!=0 ? "ORDER BY" : "LIMIT", selectOpName(p->op));
126450 rc = 1;
126451 goto multi_select_end;
126452 }
126453
126454 v = sqlite3GetVdbe(pParse);
126455 assert( v!=0 ); /* The VDBE already created by calling function */
126456
126457 /* Create the destination temporary table if necessary
126458 */
126459 if( dest.eDest==SRT_EphemTab ){
126460 assert( p->pEList );
126461 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
126462 dest.eDest = SRT_Table;
126463 }
126464
126465 /* Special handling for a compound-select that originates as a VALUES clause.
126466 */
126467 if( p->selFlags & SF_MultiValue ){
126468 rc = multiSelectValues(pParse, p, &dest);
126469 goto multi_select_end;
126470 }
126471
126472 /* Make sure all SELECTs in the statement have the same number of elements
126473 ** in their result sets.
126474 */
126475 assert( p->pEList && pPrior->pEList );
126476 assert( p->pEList->nExpr==pPrior->pEList->nExpr );
126477
126478#ifndef SQLITE_OMIT_CTE
126479 if( p->selFlags & SF_Recursive ){
126480 generateWithRecursiveQuery(pParse, p, &dest);
126481 }else
126482#endif
126483
126484 /* Compound SELECTs that have an ORDER BY clause are handled separately.
126485 */
126486 if( p->pOrderBy ){
126487 return multiSelectOrderBy(pParse, p, pDest);
126488 }else{
126489
126490#ifndef SQLITE_OMIT_EXPLAIN
126491 if( pPrior->pPrior==0 ){
126492 ExplainQueryPlan((pParse, 1, "COMPOUND QUERY"));
126493 ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"));
126494 }
126495#endif
126496
126497 /* Generate code for the left and right SELECT statements.
126498 */
126499 switch( p->op ){
126500 case TK_ALL: {
126501 int addr = 0;
126502 int nLimit;
126503 assert( !pPrior->pLimit );
126504 pPrior->iLimit = p->iLimit;
126505 pPrior->iOffset = p->iOffset;
126506 pPrior->pLimit = p->pLimit;
126507 rc = sqlite3Select(pParse, pPrior, &dest);
126508 p->pLimit = 0;
126509 if( rc ){
126510 goto multi_select_end;
126511 }
126512 p->pPrior = 0;
126513 p->iLimit = pPrior->iLimit;
126514 p->iOffset = pPrior->iOffset;
126515 if( p->iLimit ){
126516 addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
126517 VdbeComment((v, "Jump ahead if LIMIT reached"));
126518 if( p->iOffset ){
126519 sqlite3VdbeAddOp3(v, OP_OffsetLimit,
126520 p->iLimit, p->iOffset+1, p->iOffset);
126521 }
126522 }
126523 ExplainQueryPlan((pParse, 1, "UNION ALL"));
126524 rc = sqlite3Select(pParse, p, &dest);
126525 testcase( rc!=SQLITE_OK );
126526 pDelete = p->pPrior;
126527 p->pPrior = pPrior;
126528 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
126529 if( pPrior->pLimit
126530 && sqlite3ExprIsInteger(pPrior->pLimit->pLeft, &nLimit)
126531 && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
126532 ){
126533 p->nSelectRow = sqlite3LogEst((u64)nLimit);
126534 }
126535 if( addr ){
126536 sqlite3VdbeJumpHere(v, addr);
126537 }
126538 break;
126539 }
126540 case TK_EXCEPT:
126541 case TK_UNION: {
126542 int unionTab; /* Cursor number of the temp table holding result */
126543 u8 op = 0; /* One of the SRT_ operations to apply to self */
126544 int priorOp; /* The SRT_ operation to apply to prior selects */
126545 Expr *pLimit; /* Saved values of p->nLimit */
126546 int addr;
126547 SelectDest uniondest;
126548
126549 testcase( p->op==TK_EXCEPT );
126550 testcase( p->op==TK_UNION );
126551 priorOp = SRT_Union;
126552 if( dest.eDest==priorOp ){
126553 /* We can reuse a temporary table generated by a SELECT to our
126554 ** right.
126555 */
126556 assert( p->pLimit==0 ); /* Not allowed on leftward elements */
126557 unionTab = dest.iSDParm;
126558 }else{
126559 /* We will need to create our own temporary table to hold the
126560 ** intermediate results.
126561 */
126562 unionTab = pParse->nTab++;
126563 assert( p->pOrderBy==0 );
126564 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
126565 assert( p->addrOpenEphm[0] == -1 );
126566 p->addrOpenEphm[0] = addr;
126567 findRightmost(p)->selFlags |= SF_UsesEphemeral;
126568 assert( p->pEList );
126569 }
126570
126571 /* Code the SELECT statements to our left
126572 */
126573 assert( !pPrior->pOrderBy );
126574 sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
126575 rc = sqlite3Select(pParse, pPrior, &uniondest);
126576 if( rc ){
126577 goto multi_select_end;
126578 }
126579
126580 /* Code the current SELECT statement
126581 */
126582 if( p->op==TK_EXCEPT ){
126583 op = SRT_Except;
126584 }else{
126585 assert( p->op==TK_UNION );
126586 op = SRT_Union;
126587 }
126588 p->pPrior = 0;
126589 pLimit = p->pLimit;
126590 p->pLimit = 0;
126591 uniondest.eDest = op;
126592 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
126593 selectOpName(p->op)));
126594 rc = sqlite3Select(pParse, p, &uniondest);
126595 testcase( rc!=SQLITE_OK );
126596 /* Query flattening in sqlite3Select() might refill p->pOrderBy.
126597 ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
126598 sqlite3ExprListDelete(db, p->pOrderBy);
126599 pDelete = p->pPrior;
126600 p->pPrior = pPrior;
126601 p->pOrderBy = 0;
126602 if( p->op==TK_UNION ){
126603 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
126604 }
126605 sqlite3ExprDelete(db, p->pLimit);
126606 p->pLimit = pLimit;
126607 p->iLimit = 0;
126608 p->iOffset = 0;
126609
126610 /* Convert the data in the temporary table into whatever form
126611 ** it is that we currently need.
126612 */
126613 assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
126614 if( dest.eDest!=priorOp ){
126615 int iCont, iBreak, iStart;
126616 assert( p->pEList );
126617 iBreak = sqlite3VdbeMakeLabel(v);
126618 iCont = sqlite3VdbeMakeLabel(v);
126619 computeLimitRegisters(pParse, p, iBreak);
126620 sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v);
126621 iStart = sqlite3VdbeCurrentAddr(v);
126622 selectInnerLoop(pParse, p, unionTab,
126623 0, 0, &dest, iCont, iBreak);
126624 sqlite3VdbeResolveLabel(v, iCont);
126625 sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v);
126626 sqlite3VdbeResolveLabel(v, iBreak);
126627 sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
126628 }
126629 break;
126630 }
126631 default: assert( p->op==TK_INTERSECT ); {
126632 int tab1, tab2;
126633 int iCont, iBreak, iStart;
126634 Expr *pLimit;
126635 int addr;
126636 SelectDest intersectdest;
126637 int r1;
126638
126639 /* INTERSECT is different from the others since it requires
126640 ** two temporary tables. Hence it has its own case. Begin
126641 ** by allocating the tables we will need.
126642 */
126643 tab1 = pParse->nTab++;
126644 tab2 = pParse->nTab++;
126645 assert( p->pOrderBy==0 );
126646
126647 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
126648 assert( p->addrOpenEphm[0] == -1 );
126649 p->addrOpenEphm[0] = addr;
126650 findRightmost(p)->selFlags |= SF_UsesEphemeral;
126651 assert( p->pEList );
126652
126653 /* Code the SELECTs to our left into temporary table "tab1".
126654 */
126655 sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
126656 rc = sqlite3Select(pParse, pPrior, &intersectdest);
126657 if( rc ){
126658 goto multi_select_end;
126659 }
126660
126661 /* Code the current SELECT into temporary table "tab2"
126662 */
126663 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
126664 assert( p->addrOpenEphm[1] == -1 );
126665 p->addrOpenEphm[1] = addr;
126666 p->pPrior = 0;
126667 pLimit = p->pLimit;
126668 p->pLimit = 0;
126669 intersectdest.iSDParm = tab2;
126670 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
126671 selectOpName(p->op)));
126672 rc = sqlite3Select(pParse, p, &intersectdest);
126673 testcase( rc!=SQLITE_OK );
126674 pDelete = p->pPrior;
126675 p->pPrior = pPrior;
126676 if( p->nSelectRow>pPrior->nSelectRow ){
126677 p->nSelectRow = pPrior->nSelectRow;
126678 }
126679 sqlite3ExprDelete(db, p->pLimit);
126680 p->pLimit = pLimit;
126681
126682 /* Generate code to take the intersection of the two temporary
126683 ** tables.
126684 */
126685 assert( p->pEList );
126686 iBreak = sqlite3VdbeMakeLabel(v);
126687 iCont = sqlite3VdbeMakeLabel(v);
126688 computeLimitRegisters(pParse, p, iBreak);
126689 sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v);
126690 r1 = sqlite3GetTempReg(pParse);
126691 iStart = sqlite3VdbeAddOp2(v, OP_RowData, tab1, r1);
126692 sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
126693 VdbeCoverage(v);
126694 sqlite3ReleaseTempReg(pParse, r1);
126695 selectInnerLoop(pParse, p, tab1,
126696 0, 0, &dest, iCont, iBreak);
126697 sqlite3VdbeResolveLabel(v, iCont);
126698 sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
126699 sqlite3VdbeResolveLabel(v, iBreak);
126700 sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
126701 sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
126702 break;
126703 }
126704 }
126705
126706 #ifndef SQLITE_OMIT_EXPLAIN
126707 if( p->pNext==0 ){
126708 ExplainQueryPlanPop(pParse);
126709 }
126710 #endif
126711 }
126712
126713 /* Compute collating sequences used by
126714 ** temporary tables needed to implement the compound select.
126715 ** Attach the KeyInfo structure to all temporary tables.
126716 **
126717 ** This section is run by the right-most SELECT statement only.
126718 ** SELECT statements to the left always skip this part. The right-most
126719 ** SELECT might also skip this part if it has no ORDER BY clause and
126720 ** no temp tables are required.
126721 */
126722 if( p->selFlags & SF_UsesEphemeral ){
126723 int i; /* Loop counter */
126724 KeyInfo *pKeyInfo; /* Collating sequence for the result set */
126725 Select *pLoop; /* For looping through SELECT statements */
126726 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
126727 int nCol; /* Number of columns in result set */
126728
126729 assert( p->pNext==0 );
126730 nCol = p->pEList->nExpr;
126731 pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1);
126732 if( !pKeyInfo ){
126733 rc = SQLITE_NOMEM_BKPT;
126734 goto multi_select_end;
126735 }
126736 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
126737 *apColl = multiSelectCollSeq(pParse, p, i);
126738 if( 0==*apColl ){
126739 *apColl = db->pDfltColl;
126740 }
126741 }
126742
126743 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
126744 for(i=0; i<2; i++){
126745 int addr = pLoop->addrOpenEphm[i];
126746 if( addr<0 ){
126747 /* If [0] is unused then [1] is also unused. So we can
126748 ** always safely abort as soon as the first unused slot is found */
126749 assert( pLoop->addrOpenEphm[1]<0 );
126750 break;
126751 }
126752 sqlite3VdbeChangeP2(v, addr, nCol);
126753 sqlite3VdbeChangeP4(v, addr, (char*)sqlite3KeyInfoRef(pKeyInfo),
126754 P4_KEYINFO);
126755 pLoop->addrOpenEphm[i] = -1;
126756 }
126757 }
126758 sqlite3KeyInfoUnref(pKeyInfo);
126759 }
126760
126761multi_select_end:
126762 pDest->iSdst = dest.iSdst;
126763 pDest->nSdst = dest.nSdst;
126764 sqlite3SelectDelete(db, pDelete);
126765 return rc;
126766}
126767#endif /* SQLITE_OMIT_COMPOUND_SELECT */
126768
126769/*
126770** Error message for when two or more terms of a compound select have different
126771** size result sets.
126772*/
126773SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){
126774 if( p->selFlags & SF_Values ){
126775 sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
126776 }else{
126777 sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
126778 " do not have the same number of result columns", selectOpName(p->op));
126779 }
126780}
126781
126782/*
126783** Code an output subroutine for a coroutine implementation of a
126784** SELECT statment.
126785**
126786** The data to be output is contained in pIn->iSdst. There are
126787** pIn->nSdst columns to be output. pDest is where the output should
126788** be sent.
126789**
126790** regReturn is the number of the register holding the subroutine
126791** return address.
126792**
126793** If regPrev>0 then it is the first register in a vector that
126794** records the previous output. mem[regPrev] is a flag that is false
126795** if there has been no previous output. If regPrev>0 then code is
126796** generated to suppress duplicates. pKeyInfo is used for comparing
126797** keys.
126798**
126799** If the LIMIT found in p->iLimit is reached, jump immediately to
126800** iBreak.
126801*/
126802static int generateOutputSubroutine(
126803 Parse *pParse, /* Parsing context */
126804 Select *p, /* The SELECT statement */
126805 SelectDest *pIn, /* Coroutine supplying data */
126806 SelectDest *pDest, /* Where to send the data */
126807 int regReturn, /* The return address register */
126808 int regPrev, /* Previous result register. No uniqueness if 0 */
126809 KeyInfo *pKeyInfo, /* For comparing with previous entry */
126810 int iBreak /* Jump here if we hit the LIMIT */
126811){
126812 Vdbe *v = pParse->pVdbe;
126813 int iContinue;
126814 int addr;
126815
126816 addr = sqlite3VdbeCurrentAddr(v);
126817 iContinue = sqlite3VdbeMakeLabel(v);
126818
126819 /* Suppress duplicates for UNION, EXCEPT, and INTERSECT
126820 */
126821 if( regPrev ){
126822 int addr1, addr2;
126823 addr1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev); VdbeCoverage(v);
126824 addr2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
126825 (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
126826 sqlite3VdbeAddOp3(v, OP_Jump, addr2+2, iContinue, addr2+2); VdbeCoverage(v);
126827 sqlite3VdbeJumpHere(v, addr1);
126828 sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
126829 sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
126830 }
126831 if( pParse->db->mallocFailed ) return 0;
126832
126833 /* Suppress the first OFFSET entries if there is an OFFSET clause
126834 */
126835 codeOffset(v, p->iOffset, iContinue);
126836
126837 assert( pDest->eDest!=SRT_Exists );
126838 assert( pDest->eDest!=SRT_Table );
126839 switch( pDest->eDest ){
126840 /* Store the result as data using a unique key.
126841 */
126842 case SRT_EphemTab: {
126843 int r1 = sqlite3GetTempReg(pParse);
126844 int r2 = sqlite3GetTempReg(pParse);
126845 sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
126846 sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
126847 sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
126848 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
126849 sqlite3ReleaseTempReg(pParse, r2);
126850 sqlite3ReleaseTempReg(pParse, r1);
126851 break;
126852 }
126853
126854#ifndef SQLITE_OMIT_SUBQUERY
126855 /* If we are creating a set for an "expr IN (SELECT ...)".
126856 */
126857 case SRT_Set: {
126858 int r1;
126859 testcase( pIn->nSdst>1 );
126860 r1 = sqlite3GetTempReg(pParse);
126861 sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst,
126862 r1, pDest->zAffSdst, pIn->nSdst);
126863 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pDest->iSDParm, r1,
126864 pIn->iSdst, pIn->nSdst);
126865 sqlite3ReleaseTempReg(pParse, r1);
126866 break;
126867 }
126868
126869 /* If this is a scalar select that is part of an expression, then
126870 ** store the results in the appropriate memory cell and break out
126871 ** of the scan loop.
126872 */
126873 case SRT_Mem: {
126874 assert( pIn->nSdst==1 || pParse->nErr>0 ); testcase( pIn->nSdst!=1 );
126875 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, 1);
126876 /* The LIMIT clause will jump out of the loop for us */
126877 break;
126878 }
126879#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
126880
126881 /* The results are stored in a sequence of registers
126882 ** starting at pDest->iSdst. Then the co-routine yields.
126883 */
126884 case SRT_Coroutine: {
126885 if( pDest->iSdst==0 ){
126886 pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
126887 pDest->nSdst = pIn->nSdst;
126888 }
126889 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
126890 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
126891 break;
126892 }
126893
126894 /* If none of the above, then the result destination must be
126895 ** SRT_Output. This routine is never called with any other
126896 ** destination other than the ones handled above or SRT_Output.
126897 **
126898 ** For SRT_Output, results are stored in a sequence of registers.
126899 ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
126900 ** return the next row of result.
126901 */
126902 default: {
126903 assert( pDest->eDest==SRT_Output );
126904 sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
126905 break;
126906 }
126907 }
126908
126909 /* Jump to the end of the loop if the LIMIT is reached.
126910 */
126911 if( p->iLimit ){
126912 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
126913 }
126914
126915 /* Generate the subroutine return
126916 */
126917 sqlite3VdbeResolveLabel(v, iContinue);
126918 sqlite3VdbeAddOp1(v, OP_Return, regReturn);
126919
126920 return addr;
126921}
126922
126923/*
126924** Alternative compound select code generator for cases when there
126925** is an ORDER BY clause.
126926**
126927** We assume a query of the following form:
126928**
126929** <selectA> <operator> <selectB> ORDER BY <orderbylist>
126930**
126931** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT. The idea
126932** is to code both <selectA> and <selectB> with the ORDER BY clause as
126933** co-routines. Then run the co-routines in parallel and merge the results
126934** into the output. In addition to the two coroutines (called selectA and
126935** selectB) there are 7 subroutines:
126936**
126937** outA: Move the output of the selectA coroutine into the output
126938** of the compound query.
126939**
126940** outB: Move the output of the selectB coroutine into the output
126941** of the compound query. (Only generated for UNION and
126942** UNION ALL. EXCEPT and INSERTSECT never output a row that
126943** appears only in B.)
126944**
126945** AltB: Called when there is data from both coroutines and A<B.
126946**
126947** AeqB: Called when there is data from both coroutines and A==B.
126948**
126949** AgtB: Called when there is data from both coroutines and A>B.
126950**
126951** EofA: Called when data is exhausted from selectA.
126952**
126953** EofB: Called when data is exhausted from selectB.
126954**
126955** The implementation of the latter five subroutines depend on which
126956** <operator> is used:
126957**
126958**
126959** UNION ALL UNION EXCEPT INTERSECT
126960** ------------- ----------------- -------------- -----------------
126961** AltB: outA, nextA outA, nextA outA, nextA nextA
126962**
126963** AeqB: outA, nextA nextA nextA outA, nextA
126964**
126965** AgtB: outB, nextB outB, nextB nextB nextB
126966**
126967** EofA: outB, nextB outB, nextB halt halt
126968**
126969** EofB: outA, nextA outA, nextA outA, nextA halt
126970**
126971** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
126972** causes an immediate jump to EofA and an EOF on B following nextB causes
126973** an immediate jump to EofB. Within EofA and EofB, and EOF on entry or
126974** following nextX causes a jump to the end of the select processing.
126975**
126976** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
126977** within the output subroutine. The regPrev register set holds the previously
126978** output value. A comparison is made against this value and the output
126979** is skipped if the next results would be the same as the previous.
126980**
126981** The implementation plan is to implement the two coroutines and seven
126982** subroutines first, then put the control logic at the bottom. Like this:
126983**
126984** goto Init
126985** coA: coroutine for left query (A)
126986** coB: coroutine for right query (B)
126987** outA: output one row of A
126988** outB: output one row of B (UNION and UNION ALL only)
126989** EofA: ...
126990** EofB: ...
126991** AltB: ...
126992** AeqB: ...
126993** AgtB: ...
126994** Init: initialize coroutine registers
126995** yield coA
126996** if eof(A) goto EofA
126997** yield coB
126998** if eof(B) goto EofB
126999** Cmpr: Compare A, B
127000** Jump AltB, AeqB, AgtB
127001** End: ...
127002**
127003** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
127004** actually called using Gosub and they do not Return. EofA and EofB loop
127005** until all data is exhausted then jump to the "end" labe. AltB, AeqB,
127006** and AgtB jump to either L2 or to one of EofA or EofB.
127007*/
127008#ifndef SQLITE_OMIT_COMPOUND_SELECT
127009static int multiSelectOrderBy(
127010 Parse *pParse, /* Parsing context */
127011 Select *p, /* The right-most of SELECTs to be coded */
127012 SelectDest *pDest /* What to do with query results */
127013){
127014 int i, j; /* Loop counters */
127015 Select *pPrior; /* Another SELECT immediately to our left */
127016 Vdbe *v; /* Generate code to this VDBE */
127017 SelectDest destA; /* Destination for coroutine A */
127018 SelectDest destB; /* Destination for coroutine B */
127019 int regAddrA; /* Address register for select-A coroutine */
127020 int regAddrB; /* Address register for select-B coroutine */
127021 int addrSelectA; /* Address of the select-A coroutine */
127022 int addrSelectB; /* Address of the select-B coroutine */
127023 int regOutA; /* Address register for the output-A subroutine */
127024 int regOutB; /* Address register for the output-B subroutine */
127025 int addrOutA; /* Address of the output-A subroutine */
127026 int addrOutB = 0; /* Address of the output-B subroutine */
127027 int addrEofA; /* Address of the select-A-exhausted subroutine */
127028 int addrEofA_noB; /* Alternate addrEofA if B is uninitialized */
127029 int addrEofB; /* Address of the select-B-exhausted subroutine */
127030 int addrAltB; /* Address of the A<B subroutine */
127031 int addrAeqB; /* Address of the A==B subroutine */
127032 int addrAgtB; /* Address of the A>B subroutine */
127033 int regLimitA; /* Limit register for select-A */
127034 int regLimitB; /* Limit register for select-A */
127035 int regPrev; /* A range of registers to hold previous output */
127036 int savedLimit; /* Saved value of p->iLimit */
127037 int savedOffset; /* Saved value of p->iOffset */
127038 int labelCmpr; /* Label for the start of the merge algorithm */
127039 int labelEnd; /* Label for the end of the overall SELECT stmt */
127040 int addr1; /* Jump instructions that get retargetted */
127041 int op; /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
127042 KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
127043 KeyInfo *pKeyMerge; /* Comparison information for merging rows */
127044 sqlite3 *db; /* Database connection */
127045 ExprList *pOrderBy; /* The ORDER BY clause */
127046 int nOrderBy; /* Number of terms in the ORDER BY clause */
127047 int *aPermute; /* Mapping from ORDER BY terms to result set columns */
127048
127049 assert( p->pOrderBy!=0 );
127050 assert( pKeyDup==0 ); /* "Managed" code needs this. Ticket #3382. */
127051 db = pParse->db;
127052 v = pParse->pVdbe;
127053 assert( v!=0 ); /* Already thrown the error if VDBE alloc failed */
127054 labelEnd = sqlite3VdbeMakeLabel(v);
127055 labelCmpr = sqlite3VdbeMakeLabel(v);
127056
127057
127058 /* Patch up the ORDER BY clause
127059 */
127060 op = p->op;
127061 pPrior = p->pPrior;
127062 assert( pPrior->pOrderBy==0 );
127063 pOrderBy = p->pOrderBy;
127064 assert( pOrderBy );
127065 nOrderBy = pOrderBy->nExpr;
127066
127067 /* For operators other than UNION ALL we have to make sure that
127068 ** the ORDER BY clause covers every term of the result set. Add
127069 ** terms to the ORDER BY clause as necessary.
127070 */
127071 if( op!=TK_ALL ){
127072 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
127073 struct ExprList_item *pItem;
127074 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
127075 assert( pItem->u.x.iOrderByCol>0 );
127076 if( pItem->u.x.iOrderByCol==i ) break;
127077 }
127078 if( j==nOrderBy ){
127079 Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
127080 if( pNew==0 ) return SQLITE_NOMEM_BKPT;
127081 pNew->flags |= EP_IntValue;
127082 pNew->u.iValue = i;
127083 p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
127084 if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
127085 }
127086 }
127087 }
127088
127089 /* Compute the comparison permutation and keyinfo that is used with
127090 ** the permutation used to determine if the next
127091 ** row of results comes from selectA or selectB. Also add explicit
127092 ** collations to the ORDER BY clause terms so that when the subqueries
127093 ** to the right and the left are evaluated, they use the correct
127094 ** collation.
127095 */
127096 aPermute = sqlite3DbMallocRawNN(db, sizeof(int)*(nOrderBy + 1));
127097 if( aPermute ){
127098 struct ExprList_item *pItem;
127099 aPermute[0] = nOrderBy;
127100 for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
127101 assert( pItem->u.x.iOrderByCol>0 );
127102 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
127103 aPermute[i] = pItem->u.x.iOrderByCol - 1;
127104 }
127105 pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
127106 }else{
127107 pKeyMerge = 0;
127108 }
127109
127110 /* Reattach the ORDER BY clause to the query.
127111 */
127112 p->pOrderBy = pOrderBy;
127113 pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
127114
127115 /* Allocate a range of temporary registers and the KeyInfo needed
127116 ** for the logic that removes duplicate result rows when the
127117 ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
127118 */
127119 if( op==TK_ALL ){
127120 regPrev = 0;
127121 }else{
127122 int nExpr = p->pEList->nExpr;
127123 assert( nOrderBy>=nExpr || db->mallocFailed );
127124 regPrev = pParse->nMem+1;
127125 pParse->nMem += nExpr+1;
127126 sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
127127 pKeyDup = sqlite3KeyInfoAlloc(db, nExpr, 1);
127128 if( pKeyDup ){
127129 assert( sqlite3KeyInfoIsWriteable(pKeyDup) );
127130 for(i=0; i<nExpr; i++){
127131 pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
127132 pKeyDup->aSortOrder[i] = 0;
127133 }
127134 }
127135 }
127136
127137 /* Separate the left and the right query from one another
127138 */
127139 p->pPrior = 0;
127140 pPrior->pNext = 0;
127141 sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
127142 if( pPrior->pPrior==0 ){
127143 sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
127144 }
127145
127146 /* Compute the limit registers */
127147 computeLimitRegisters(pParse, p, labelEnd);
127148 if( p->iLimit && op==TK_ALL ){
127149 regLimitA = ++pParse->nMem;
127150 regLimitB = ++pParse->nMem;
127151 sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
127152 regLimitA);
127153 sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB);
127154 }else{
127155 regLimitA = regLimitB = 0;
127156 }
127157 sqlite3ExprDelete(db, p->pLimit);
127158 p->pLimit = 0;
127159
127160 regAddrA = ++pParse->nMem;
127161 regAddrB = ++pParse->nMem;
127162 regOutA = ++pParse->nMem;
127163 regOutB = ++pParse->nMem;
127164 sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
127165 sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
127166
127167 ExplainQueryPlan((pParse, 1, "MERGE (%s)", selectOpName(p->op)));
127168
127169 /* Generate a coroutine to evaluate the SELECT statement to the
127170 ** left of the compound operator - the "A" select.
127171 */
127172 addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
127173 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
127174 VdbeComment((v, "left SELECT"));
127175 pPrior->iLimit = regLimitA;
127176 ExplainQueryPlan((pParse, 1, "LEFT"));
127177 sqlite3Select(pParse, pPrior, &destA);
127178 sqlite3VdbeEndCoroutine(v, regAddrA);
127179 sqlite3VdbeJumpHere(v, addr1);
127180
127181 /* Generate a coroutine to evaluate the SELECT statement on
127182 ** the right - the "B" select
127183 */
127184 addrSelectB = sqlite3VdbeCurrentAddr(v) + 1;
127185 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrB, 0, addrSelectB);
127186 VdbeComment((v, "right SELECT"));
127187 savedLimit = p->iLimit;
127188 savedOffset = p->iOffset;
127189 p->iLimit = regLimitB;
127190 p->iOffset = 0;
127191 ExplainQueryPlan((pParse, 1, "RIGHT"));
127192 sqlite3Select(pParse, p, &destB);
127193 p->iLimit = savedLimit;
127194 p->iOffset = savedOffset;
127195 sqlite3VdbeEndCoroutine(v, regAddrB);
127196
127197 /* Generate a subroutine that outputs the current row of the A
127198 ** select as the next output row of the compound select.
127199 */
127200 VdbeNoopComment((v, "Output routine for A"));
127201 addrOutA = generateOutputSubroutine(pParse,
127202 p, &destA, pDest, regOutA,
127203 regPrev, pKeyDup, labelEnd);
127204
127205 /* Generate a subroutine that outputs the current row of the B
127206 ** select as the next output row of the compound select.
127207 */
127208 if( op==TK_ALL || op==TK_UNION ){
127209 VdbeNoopComment((v, "Output routine for B"));
127210 addrOutB = generateOutputSubroutine(pParse,
127211 p, &destB, pDest, regOutB,
127212 regPrev, pKeyDup, labelEnd);
127213 }
127214 sqlite3KeyInfoUnref(pKeyDup);
127215
127216 /* Generate a subroutine to run when the results from select A
127217 ** are exhausted and only data in select B remains.
127218 */
127219 if( op==TK_EXCEPT || op==TK_INTERSECT ){
127220 addrEofA_noB = addrEofA = labelEnd;
127221 }else{
127222 VdbeNoopComment((v, "eof-A subroutine"));
127223 addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
127224 addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, labelEnd);
127225 VdbeCoverage(v);
127226 sqlite3VdbeGoto(v, addrEofA);
127227 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
127228 }
127229
127230 /* Generate a subroutine to run when the results from select B
127231 ** are exhausted and only data in select A remains.
127232 */
127233 if( op==TK_INTERSECT ){
127234 addrEofB = addrEofA;
127235 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
127236 }else{
127237 VdbeNoopComment((v, "eof-B subroutine"));
127238 addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
127239 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, labelEnd); VdbeCoverage(v);
127240 sqlite3VdbeGoto(v, addrEofB);
127241 }
127242
127243 /* Generate code to handle the case of A<B
127244 */
127245 VdbeNoopComment((v, "A-lt-B subroutine"));
127246 addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
127247 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
127248 sqlite3VdbeGoto(v, labelCmpr);
127249
127250 /* Generate code to handle the case of A==B
127251 */
127252 if( op==TK_ALL ){
127253 addrAeqB = addrAltB;
127254 }else if( op==TK_INTERSECT ){
127255 addrAeqB = addrAltB;
127256 addrAltB++;
127257 }else{
127258 VdbeNoopComment((v, "A-eq-B subroutine"));
127259 addrAeqB =
127260 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
127261 sqlite3VdbeGoto(v, labelCmpr);
127262 }
127263
127264 /* Generate code to handle the case of A>B
127265 */
127266 VdbeNoopComment((v, "A-gt-B subroutine"));
127267 addrAgtB = sqlite3VdbeCurrentAddr(v);
127268 if( op==TK_ALL || op==TK_UNION ){
127269 sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
127270 }
127271 sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
127272 sqlite3VdbeGoto(v, labelCmpr);
127273
127274 /* This code runs once to initialize everything.
127275 */
127276 sqlite3VdbeJumpHere(v, addr1);
127277 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA_noB); VdbeCoverage(v);
127278 sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
127279
127280 /* Implement the main merge loop
127281 */
127282 sqlite3VdbeResolveLabel(v, labelCmpr);
127283 sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
127284 sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
127285 (char*)pKeyMerge, P4_KEYINFO);
127286 sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE);
127287 sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB); VdbeCoverage(v);
127288
127289 /* Jump to the this point in order to terminate the query.
127290 */
127291 sqlite3VdbeResolveLabel(v, labelEnd);
127292
127293 /* Reassembly the compound query so that it will be freed correctly
127294 ** by the calling function */
127295 if( p->pPrior ){
127296 sqlite3SelectDelete(db, p->pPrior);
127297 }
127298 p->pPrior = pPrior;
127299 pPrior->pNext = p;
127300
127301 /*** TBD: Insert subroutine calls to close cursors on incomplete
127302 **** subqueries ****/
127303 ExplainQueryPlanPop(pParse);
127304 return pParse->nErr!=0;
127305}
127306#endif
127307
127308#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
127309
127310/* An instance of the SubstContext object describes an substitution edit
127311** to be performed on a parse tree.
127312**
127313** All references to columns in table iTable are to be replaced by corresponding
127314** expressions in pEList.
127315*/
127316typedef struct SubstContext {
127317 Parse *pParse; /* The parsing context */
127318 int iTable; /* Replace references to this table */
127319 int iNewTable; /* New table number */
127320 int isLeftJoin; /* Add TK_IF_NULL_ROW opcodes on each replacement */
127321 ExprList *pEList; /* Replacement expressions */
127322} SubstContext;
127323
127324/* Forward Declarations */
127325static void substExprList(SubstContext*, ExprList*);
127326static void substSelect(SubstContext*, Select*, int);
127327
127328/*
127329** Scan through the expression pExpr. Replace every reference to
127330** a column in table number iTable with a copy of the iColumn-th
127331** entry in pEList. (But leave references to the ROWID column
127332** unchanged.)
127333**
127334** This routine is part of the flattening procedure. A subquery
127335** whose result set is defined by pEList appears as entry in the
127336** FROM clause of a SELECT such that the VDBE cursor assigned to that
127337** FORM clause entry is iTable. This routine makes the necessary
127338** changes to pExpr so that it refers directly to the source table
127339** of the subquery rather the result set of the subquery.
127340*/
127341static Expr *substExpr(
127342 SubstContext *pSubst, /* Description of the substitution */
127343 Expr *pExpr /* Expr in which substitution occurs */
127344){
127345 if( pExpr==0 ) return 0;
127346 if( ExprHasProperty(pExpr, EP_FromJoin)
127347 && pExpr->iRightJoinTable==pSubst->iTable
127348 ){
127349 pExpr->iRightJoinTable = pSubst->iNewTable;
127350 }
127351 if( pExpr->op==TK_COLUMN && pExpr->iTable==pSubst->iTable ){
127352 if( pExpr->iColumn<0 ){
127353 pExpr->op = TK_NULL;
127354 }else{
127355 Expr *pNew;
127356 Expr *pCopy = pSubst->pEList->a[pExpr->iColumn].pExpr;
127357 Expr ifNullRow;
127358 assert( pSubst->pEList!=0 && pExpr->iColumn<pSubst->pEList->nExpr );
127359 assert( pExpr->pRight==0 );
127360 if( sqlite3ExprIsVector(pCopy) ){
127361 sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
127362 }else{
127363 sqlite3 *db = pSubst->pParse->db;
127364 if( pSubst->isLeftJoin && pCopy->op!=TK_COLUMN ){
127365 memset(&ifNullRow, 0, sizeof(ifNullRow));
127366 ifNullRow.op = TK_IF_NULL_ROW;
127367 ifNullRow.pLeft = pCopy;
127368 ifNullRow.iTable = pSubst->iNewTable;
127369 pCopy = &ifNullRow;
127370 }
127371 pNew = sqlite3ExprDup(db, pCopy, 0);
127372 if( pNew && pSubst->isLeftJoin ){
127373 ExprSetProperty(pNew, EP_CanBeNull);
127374 }
127375 if( pNew && ExprHasProperty(pExpr,EP_FromJoin) ){
127376 pNew->iRightJoinTable = pExpr->iRightJoinTable;
127377 ExprSetProperty(pNew, EP_FromJoin);
127378 }
127379 sqlite3ExprDelete(db, pExpr);
127380 pExpr = pNew;
127381 }
127382 }
127383 }else{
127384 if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
127385 pExpr->iTable = pSubst->iNewTable;
127386 }
127387 pExpr->pLeft = substExpr(pSubst, pExpr->pLeft);
127388 pExpr->pRight = substExpr(pSubst, pExpr->pRight);
127389 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
127390 substSelect(pSubst, pExpr->x.pSelect, 1);
127391 }else{
127392 substExprList(pSubst, pExpr->x.pList);
127393 }
127394 }
127395 return pExpr;
127396}
127397static void substExprList(
127398 SubstContext *pSubst, /* Description of the substitution */
127399 ExprList *pList /* List to scan and in which to make substitutes */
127400){
127401 int i;
127402 if( pList==0 ) return;
127403 for(i=0; i<pList->nExpr; i++){
127404 pList->a[i].pExpr = substExpr(pSubst, pList->a[i].pExpr);
127405 }
127406}
127407static void substSelect(
127408 SubstContext *pSubst, /* Description of the substitution */
127409 Select *p, /* SELECT statement in which to make substitutions */
127410 int doPrior /* Do substitutes on p->pPrior too */
127411){
127412 SrcList *pSrc;
127413 struct SrcList_item *pItem;
127414 int i;
127415 if( !p ) return;
127416 do{
127417 substExprList(pSubst, p->pEList);
127418 substExprList(pSubst, p->pGroupBy);
127419 substExprList(pSubst, p->pOrderBy);
127420 p->pHaving = substExpr(pSubst, p->pHaving);
127421 p->pWhere = substExpr(pSubst, p->pWhere);
127422 pSrc = p->pSrc;
127423 assert( pSrc!=0 );
127424 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
127425 substSelect(pSubst, pItem->pSelect, 1);
127426 if( pItem->fg.isTabFunc ){
127427 substExprList(pSubst, pItem->u1.pFuncArg);
127428 }
127429 }
127430 }while( doPrior && (p = p->pPrior)!=0 );
127431}
127432#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
127433
127434#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
127435/*
127436** This routine attempts to flatten subqueries as a performance optimization.
127437** This routine returns 1 if it makes changes and 0 if no flattening occurs.
127438**
127439** To understand the concept of flattening, consider the following
127440** query:
127441**
127442** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
127443**
127444** The default way of implementing this query is to execute the
127445** subquery first and store the results in a temporary table, then
127446** run the outer query on that temporary table. This requires two
127447** passes over the data. Furthermore, because the temporary table
127448** has no indices, the WHERE clause on the outer query cannot be
127449** optimized.
127450**
127451** This routine attempts to rewrite queries such as the above into
127452** a single flat select, like this:
127453**
127454** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
127455**
127456** The code generated for this simplification gives the same result
127457** but only has to scan the data once. And because indices might
127458** exist on the table t1, a complete scan of the data might be
127459** avoided.
127460**
127461** Flattening is subject to the following constraints:
127462**
127463** (**) We no longer attempt to flatten aggregate subqueries. Was:
127464** The subquery and the outer query cannot both be aggregates.
127465**
127466** (**) We no longer attempt to flatten aggregate subqueries. Was:
127467** (2) If the subquery is an aggregate then
127468** (2a) the outer query must not be a join and
127469** (2b) the outer query must not use subqueries
127470** other than the one FROM-clause subquery that is a candidate
127471** for flattening. (This is due to ticket [2f7170d73bf9abf80]
127472** from 2015-02-09.)
127473**
127474** (3) If the subquery is the right operand of a LEFT JOIN then
127475** (3a) the subquery may not be a join and
127476** (3b) the FROM clause of the subquery may not contain a virtual
127477** table and
127478** (3c) the outer query may not be an aggregate.
127479**
127480** (4) The subquery can not be DISTINCT.
127481**
127482** (**) At one point restrictions (4) and (5) defined a subset of DISTINCT
127483** sub-queries that were excluded from this optimization. Restriction
127484** (4) has since been expanded to exclude all DISTINCT subqueries.
127485**
127486** (**) We no longer attempt to flatten aggregate subqueries. Was:
127487** If the subquery is aggregate, the outer query may not be DISTINCT.
127488**
127489** (7) The subquery must have a FROM clause. TODO: For subqueries without
127490** A FROM clause, consider adding a FROM clause with the special
127491** table sqlite_once that consists of a single row containing a
127492** single NULL.
127493**
127494** (8) If the subquery uses LIMIT then the outer query may not be a join.
127495**
127496** (9) If the subquery uses LIMIT then the outer query may not be aggregate.
127497**
127498** (**) Restriction (10) was removed from the code on 2005-02-05 but we
127499** accidently carried the comment forward until 2014-09-15. Original
127500** constraint: "If the subquery is aggregate then the outer query
127501** may not use LIMIT."
127502**
127503** (11) The subquery and the outer query may not both have ORDER BY clauses.
127504**
127505** (**) Not implemented. Subsumed into restriction (3). Was previously
127506** a separate restriction deriving from ticket #350.
127507**
127508** (13) The subquery and outer query may not both use LIMIT.
127509**
127510** (14) The subquery may not use OFFSET.
127511**
127512** (15) If the outer query is part of a compound select, then the
127513** subquery may not use LIMIT.
127514** (See ticket #2339 and ticket [02a8e81d44]).
127515**
127516** (16) If the outer query is aggregate, then the subquery may not
127517** use ORDER BY. (Ticket #2942) This used to not matter
127518** until we introduced the group_concat() function.
127519**
127520** (17) If the subquery is a compound select, then
127521** (17a) all compound operators must be a UNION ALL, and
127522** (17b) no terms within the subquery compound may be aggregate
127523** or DISTINCT, and
127524** (17c) every term within the subquery compound must have a FROM clause
127525** (17d) the outer query may not be
127526** (17d1) aggregate, or
127527** (17d2) DISTINCT, or
127528** (17d3) a join.
127529**
127530** The parent and sub-query may contain WHERE clauses. Subject to
127531** rules (11), (13) and (14), they may also contain ORDER BY,
127532** LIMIT and OFFSET clauses. The subquery cannot use any compound
127533** operator other than UNION ALL because all the other compound
127534** operators have an implied DISTINCT which is disallowed by
127535** restriction (4).
127536**
127537** Also, each component of the sub-query must return the same number
127538** of result columns. This is actually a requirement for any compound
127539** SELECT statement, but all the code here does is make sure that no
127540** such (illegal) sub-query is flattened. The caller will detect the
127541** syntax error and return a detailed message.
127542**
127543** (18) If the sub-query is a compound select, then all terms of the
127544** ORDER BY clause of the parent must be simple references to
127545** columns of the sub-query.
127546**
127547** (19) If the subquery uses LIMIT then the outer query may not
127548** have a WHERE clause.
127549**
127550** (20) If the sub-query is a compound select, then it must not use
127551** an ORDER BY clause. Ticket #3773. We could relax this constraint
127552** somewhat by saying that the terms of the ORDER BY clause must
127553** appear as unmodified result columns in the outer query. But we
127554** have other optimizations in mind to deal with that case.
127555**
127556** (21) If the subquery uses LIMIT then the outer query may not be
127557** DISTINCT. (See ticket [752e1646fc]).
127558**
127559** (22) The subquery may not be a recursive CTE.
127560**
127561** (**) Subsumed into restriction (17d3). Was: If the outer query is
127562** a recursive CTE, then the sub-query may not be a compound query.
127563** This restriction is because transforming the
127564** parent to a compound query confuses the code that handles
127565** recursive queries in multiSelect().
127566**
127567** (**) We no longer attempt to flatten aggregate subqueries. Was:
127568** The subquery may not be an aggregate that uses the built-in min() or
127569** or max() functions. (Without this restriction, a query like:
127570** "SELECT x FROM (SELECT max(y), x FROM t1)" would not necessarily
127571** return the value X for which Y was maximal.)
127572**
127573** (25) If either the subquery or the parent query contains a window
127574** function in the select list or ORDER BY clause, flattening
127575** is not attempted.
127576**
127577**
127578** In this routine, the "p" parameter is a pointer to the outer query.
127579** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
127580** uses aggregates.
127581**
127582** If flattening is not attempted, this routine is a no-op and returns 0.
127583** If flattening is attempted this routine returns 1.
127584**
127585** All of the expression analysis must occur on both the outer query and
127586** the subquery before this routine runs.
127587*/
127588static int flattenSubquery(
127589 Parse *pParse, /* Parsing context */
127590 Select *p, /* The parent or outer SELECT statement */
127591 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
127592 int isAgg /* True if outer SELECT uses aggregate functions */
127593){
127594 const char *zSavedAuthContext = pParse->zAuthContext;
127595 Select *pParent; /* Current UNION ALL term of the other query */
127596 Select *pSub; /* The inner query or "subquery" */
127597 Select *pSub1; /* Pointer to the rightmost select in sub-query */
127598 SrcList *pSrc; /* The FROM clause of the outer query */
127599 SrcList *pSubSrc; /* The FROM clause of the subquery */
127600 int iParent; /* VDBE cursor number of the pSub result set temp table */
127601 int iNewParent = -1;/* Replacement table for iParent */
127602 int isLeftJoin = 0; /* True if pSub is the right side of a LEFT JOIN */
127603 int i; /* Loop counter */
127604 Expr *pWhere; /* The WHERE clause */
127605 struct SrcList_item *pSubitem; /* The subquery */
127606 sqlite3 *db = pParse->db;
127607
127608 /* Check to see if flattening is permitted. Return 0 if not.
127609 */
127610 assert( p!=0 );
127611 assert( p->pPrior==0 );
127612 if( OptimizationDisabled(db, SQLITE_QueryFlattener) ) return 0;
127613 pSrc = p->pSrc;
127614 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
127615 pSubitem = &pSrc->a[iFrom];
127616 iParent = pSubitem->iCursor;
127617 pSub = pSubitem->pSelect;
127618 assert( pSub!=0 );
127619
127620#ifndef SQLITE_OMIT_WINDOWFUNC
127621 if( p->pWin || pSub->pWin ) return 0; /* Restriction (25) */
127622#endif
127623
127624 pSubSrc = pSub->pSrc;
127625 assert( pSubSrc );
127626 /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
127627 ** not arbitrary expressions, we allowed some combining of LIMIT and OFFSET
127628 ** because they could be computed at compile-time. But when LIMIT and OFFSET
127629 ** became arbitrary expressions, we were forced to add restrictions (13)
127630 ** and (14). */
127631 if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
127632 if( pSub->pLimit && pSub->pLimit->pRight ) return 0; /* Restriction (14) */
127633 if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
127634 return 0; /* Restriction (15) */
127635 }
127636 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
127637 if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (4) */
127638 if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
127639 return 0; /* Restrictions (8)(9) */
127640 }
127641 if( p->pOrderBy && pSub->pOrderBy ){
127642 return 0; /* Restriction (11) */
127643 }
127644 if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
127645 if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
127646 if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
127647 return 0; /* Restriction (21) */
127648 }
127649 if( pSub->selFlags & (SF_Recursive) ){
127650 return 0; /* Restrictions (22) */
127651 }
127652
127653 /*
127654 ** If the subquery is the right operand of a LEFT JOIN, then the
127655 ** subquery may not be a join itself (3a). Example of why this is not
127656 ** allowed:
127657 **
127658 ** t1 LEFT OUTER JOIN (t2 JOIN t3)
127659 **
127660 ** If we flatten the above, we would get
127661 **
127662 ** (t1 LEFT OUTER JOIN t2) JOIN t3
127663 **
127664 ** which is not at all the same thing.
127665 **
127666 ** If the subquery is the right operand of a LEFT JOIN, then the outer
127667 ** query cannot be an aggregate. (3c) This is an artifact of the way
127668 ** aggregates are processed - there is no mechanism to determine if
127669 ** the LEFT JOIN table should be all-NULL.
127670 **
127671 ** See also tickets #306, #350, and #3300.
127672 */
127673 if( (pSubitem->fg.jointype & JT_OUTER)!=0 ){
127674 isLeftJoin = 1;
127675 if( pSubSrc->nSrc>1 || isAgg || IsVirtual(pSubSrc->a[0].pTab) ){
127676 /* (3a) (3c) (3b) */
127677 return 0;
127678 }
127679 }
127680#ifdef SQLITE_EXTRA_IFNULLROW
127681 else if( iFrom>0 && !isAgg ){
127682 /* Setting isLeftJoin to -1 causes OP_IfNullRow opcodes to be generated for
127683 ** every reference to any result column from subquery in a join, even
127684 ** though they are not necessary. This will stress-test the OP_IfNullRow
127685 ** opcode. */
127686 isLeftJoin = -1;
127687 }
127688#endif
127689
127690 /* Restriction (17): If the sub-query is a compound SELECT, then it must
127691 ** use only the UNION ALL operator. And none of the simple select queries
127692 ** that make up the compound SELECT are allowed to be aggregate or distinct
127693 ** queries.
127694 */
127695 if( pSub->pPrior ){
127696 if( pSub->pOrderBy ){
127697 return 0; /* Restriction (20) */
127698 }
127699 if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
127700 return 0; /* (17d1), (17d2), or (17d3) */
127701 }
127702 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
127703 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
127704 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
127705 assert( pSub->pSrc!=0 );
127706 assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
127707 if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 /* (17b) */
127708 || (pSub1->pPrior && pSub1->op!=TK_ALL) /* (17a) */
127709 || pSub1->pSrc->nSrc<1 /* (17c) */
127710 ){
127711 return 0;
127712 }
127713 testcase( pSub1->pSrc->nSrc>1 );
127714 }
127715
127716 /* Restriction (18). */
127717 if( p->pOrderBy ){
127718 int ii;
127719 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
127720 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
127721 }
127722 }
127723 }
127724
127725 /* Ex-restriction (23):
127726 ** The only way that the recursive part of a CTE can contain a compound
127727 ** subquery is for the subquery to be one term of a join. But if the
127728 ** subquery is a join, then the flattening has already been stopped by
127729 ** restriction (17d3)
127730 */
127731 assert( (p->selFlags & SF_Recursive)==0 || pSub->pPrior==0 );
127732
127733 /***** If we reach this point, flattening is permitted. *****/
127734 SELECTTRACE(1,pParse,p,("flatten %u.%p from term %d\n",
127735 pSub->selId, pSub, iFrom));
127736
127737 /* Authorize the subquery */
127738 pParse->zAuthContext = pSubitem->zName;
127739 TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
127740 testcase( i==SQLITE_DENY );
127741 pParse->zAuthContext = zSavedAuthContext;
127742
127743 /* If the sub-query is a compound SELECT statement, then (by restrictions
127744 ** 17 and 18 above) it must be a UNION ALL and the parent query must
127745 ** be of the form:
127746 **
127747 ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
127748 **
127749 ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
127750 ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
127751 ** OFFSET clauses and joins them to the left-hand-side of the original
127752 ** using UNION ALL operators. In this case N is the number of simple
127753 ** select statements in the compound sub-query.
127754 **
127755 ** Example:
127756 **
127757 ** SELECT a+1 FROM (
127758 ** SELECT x FROM tab
127759 ** UNION ALL
127760 ** SELECT y FROM tab
127761 ** UNION ALL
127762 ** SELECT abs(z*2) FROM tab2
127763 ** ) WHERE a!=5 ORDER BY 1
127764 **
127765 ** Transformed into:
127766 **
127767 ** SELECT x+1 FROM tab WHERE x+1!=5
127768 ** UNION ALL
127769 ** SELECT y+1 FROM tab WHERE y+1!=5
127770 ** UNION ALL
127771 ** SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
127772 ** ORDER BY 1
127773 **
127774 ** We call this the "compound-subquery flattening".
127775 */
127776 for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
127777 Select *pNew;
127778 ExprList *pOrderBy = p->pOrderBy;
127779 Expr *pLimit = p->pLimit;
127780 Select *pPrior = p->pPrior;
127781 p->pOrderBy = 0;
127782 p->pSrc = 0;
127783 p->pPrior = 0;
127784 p->pLimit = 0;
127785 pNew = sqlite3SelectDup(db, p, 0);
127786 p->pLimit = pLimit;
127787 p->pOrderBy = pOrderBy;
127788 p->pSrc = pSrc;
127789 p->op = TK_ALL;
127790 if( pNew==0 ){
127791 p->pPrior = pPrior;
127792 }else{
127793 pNew->pPrior = pPrior;
127794 if( pPrior ) pPrior->pNext = pNew;
127795 pNew->pNext = p;
127796 p->pPrior = pNew;
127797 SELECTTRACE(2,pParse,p,("compound-subquery flattener"
127798 " creates %u as peer\n",pNew->selId));
127799 }
127800 if( db->mallocFailed ) return 1;
127801 }
127802
127803 /* Begin flattening the iFrom-th entry of the FROM clause
127804 ** in the outer query.
127805 */
127806 pSub = pSub1 = pSubitem->pSelect;
127807
127808 /* Delete the transient table structure associated with the
127809 ** subquery
127810 */
127811 sqlite3DbFree(db, pSubitem->zDatabase);
127812 sqlite3DbFree(db, pSubitem->zName);
127813 sqlite3DbFree(db, pSubitem->zAlias);
127814 pSubitem->zDatabase = 0;
127815 pSubitem->zName = 0;
127816 pSubitem->zAlias = 0;
127817 pSubitem->pSelect = 0;
127818
127819 /* Defer deleting the Table object associated with the
127820 ** subquery until code generation is
127821 ** complete, since there may still exist Expr.pTab entries that
127822 ** refer to the subquery even after flattening. Ticket #3346.
127823 **
127824 ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
127825 */
127826 if( ALWAYS(pSubitem->pTab!=0) ){
127827 Table *pTabToDel = pSubitem->pTab;
127828 if( pTabToDel->nTabRef==1 ){
127829 Parse *pToplevel = sqlite3ParseToplevel(pParse);
127830 pTabToDel->pNextZombie = pToplevel->pZombieTab;
127831 pToplevel->pZombieTab = pTabToDel;
127832 }else{
127833 pTabToDel->nTabRef--;
127834 }
127835 pSubitem->pTab = 0;
127836 }
127837
127838 /* The following loop runs once for each term in a compound-subquery
127839 ** flattening (as described above). If we are doing a different kind
127840 ** of flattening - a flattening other than a compound-subquery flattening -
127841 ** then this loop only runs once.
127842 **
127843 ** This loop moves all of the FROM elements of the subquery into the
127844 ** the FROM clause of the outer query. Before doing this, remember
127845 ** the cursor number for the original outer query FROM element in
127846 ** iParent. The iParent cursor will never be used. Subsequent code
127847 ** will scan expressions looking for iParent references and replace
127848 ** those references with expressions that resolve to the subquery FROM
127849 ** elements we are now copying in.
127850 */
127851 for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
127852 int nSubSrc;
127853 u8 jointype = 0;
127854 pSubSrc = pSub->pSrc; /* FROM clause of subquery */
127855 nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
127856 pSrc = pParent->pSrc; /* FROM clause of the outer query */
127857
127858 if( pSrc ){
127859 assert( pParent==p ); /* First time through the loop */
127860 jointype = pSubitem->fg.jointype;
127861 }else{
127862 assert( pParent!=p ); /* 2nd and subsequent times through the loop */
127863 pSrc = pParent->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
127864 if( pSrc==0 ){
127865 assert( db->mallocFailed );
127866 break;
127867 }
127868 }
127869
127870 /* The subquery uses a single slot of the FROM clause of the outer
127871 ** query. If the subquery has more than one element in its FROM clause,
127872 ** then expand the outer query to make space for it to hold all elements
127873 ** of the subquery.
127874 **
127875 ** Example:
127876 **
127877 ** SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
127878 **
127879 ** The outer query has 3 slots in its FROM clause. One slot of the
127880 ** outer query (the middle slot) is used by the subquery. The next
127881 ** block of code will expand the outer query FROM clause to 4 slots.
127882 ** The middle slot is expanded to two slots in order to make space
127883 ** for the two elements in the FROM clause of the subquery.
127884 */
127885 if( nSubSrc>1 ){
127886 pParent->pSrc = pSrc = sqlite3SrcListEnlarge(db, pSrc, nSubSrc-1,iFrom+1);
127887 if( db->mallocFailed ){
127888 break;
127889 }
127890 }
127891
127892 /* Transfer the FROM clause terms from the subquery into the
127893 ** outer query.
127894 */
127895 for(i=0; i<nSubSrc; i++){
127896 sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
127897 assert( pSrc->a[i+iFrom].fg.isTabFunc==0 );
127898 pSrc->a[i+iFrom] = pSubSrc->a[i];
127899 iNewParent = pSubSrc->a[i].iCursor;
127900 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
127901 }
127902 pSrc->a[iFrom].fg.jointype = jointype;
127903
127904 /* Now begin substituting subquery result set expressions for
127905 ** references to the iParent in the outer query.
127906 **
127907 ** Example:
127908 **
127909 ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
127910 ** \ \_____________ subquery __________/ /
127911 ** \_____________________ outer query ______________________________/
127912 **
127913 ** We look at every expression in the outer query and every place we see
127914 ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
127915 */
127916 if( pSub->pOrderBy ){
127917 /* At this point, any non-zero iOrderByCol values indicate that the
127918 ** ORDER BY column expression is identical to the iOrderByCol'th
127919 ** expression returned by SELECT statement pSub. Since these values
127920 ** do not necessarily correspond to columns in SELECT statement pParent,
127921 ** zero them before transfering the ORDER BY clause.
127922 **
127923 ** Not doing this may cause an error if a subsequent call to this
127924 ** function attempts to flatten a compound sub-query into pParent
127925 ** (the only way this can happen is if the compound sub-query is
127926 ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
127927 ExprList *pOrderBy = pSub->pOrderBy;
127928 for(i=0; i<pOrderBy->nExpr; i++){
127929 pOrderBy->a[i].u.x.iOrderByCol = 0;
127930 }
127931 assert( pParent->pOrderBy==0 );
127932 pParent->pOrderBy = pOrderBy;
127933 pSub->pOrderBy = 0;
127934 }
127935 pWhere = sqlite3ExprDup(db, pSub->pWhere, 0);
127936 if( isLeftJoin>0 ){
127937 setJoinExpr(pWhere, iNewParent);
127938 }
127939 pParent->pWhere = sqlite3ExprAnd(db, pWhere, pParent->pWhere);
127940 if( db->mallocFailed==0 ){
127941 SubstContext x;
127942 x.pParse = pParse;
127943 x.iTable = iParent;
127944 x.iNewTable = iNewParent;
127945 x.isLeftJoin = isLeftJoin;
127946 x.pEList = pSub->pEList;
127947 substSelect(&x, pParent, 0);
127948 }
127949
127950 /* The flattened query is distinct if either the inner or the
127951 ** outer query is distinct.
127952 */
127953 pParent->selFlags |= pSub->selFlags & SF_Distinct;
127954
127955 /*
127956 ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
127957 **
127958 ** One is tempted to try to add a and b to combine the limits. But this
127959 ** does not work if either limit is negative.
127960 */
127961 if( pSub->pLimit ){
127962 pParent->pLimit = pSub->pLimit;
127963 pSub->pLimit = 0;
127964 }
127965 }
127966
127967 /* Finially, delete what is left of the subquery and return
127968 ** success.
127969 */
127970 sqlite3SelectDelete(db, pSub1);
127971
127972#if SELECTTRACE_ENABLED
127973 if( sqlite3SelectTrace & 0x100 ){
127974 SELECTTRACE(0x100,pParse,p,("After flattening:\n"));
127975 sqlite3TreeViewSelect(0, p, 0);
127976 }
127977#endif
127978
127979 return 1;
127980}
127981#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
127982
127983/*
127984** A structure to keep track of all of the column values that are fixed to
127985** a known value due to WHERE clause constraints of the form COLUMN=VALUE.
127986*/
127987typedef struct WhereConst WhereConst;
127988struct WhereConst {
127989 Parse *pParse; /* Parsing context */
127990 int nConst; /* Number for COLUMN=CONSTANT terms */
127991 int nChng; /* Number of times a constant is propagated */
127992 Expr **apExpr; /* [i*2] is COLUMN and [i*2+1] is VALUE */
127993};
127994
127995/*
127996** Add a new entry to the pConst object. Except, do not add duplicate
127997** pColumn entires.
127998*/
127999static void constInsert(
128000 WhereConst *pConst, /* The WhereConst into which we are inserting */
128001 Expr *pColumn, /* The COLUMN part of the constraint */
128002 Expr *pValue /* The VALUE part of the constraint */
128003){
128004 int i;
128005 assert( pColumn->op==TK_COLUMN );
128006
128007 /* 2018-10-25 ticket [cf5ed20f]
128008 ** Make sure the same pColumn is not inserted more than once */
128009 for(i=0; i<pConst->nConst; i++){
128010 const Expr *pExpr = pConst->apExpr[i*2];
128011 assert( pExpr->op==TK_COLUMN );
128012 if( pExpr->iTable==pColumn->iTable
128013 && pExpr->iColumn==pColumn->iColumn
128014 ){
128015 return; /* Already present. Return without doing anything. */
128016 }
128017 }
128018
128019 pConst->nConst++;
128020 pConst->apExpr = sqlite3DbReallocOrFree(pConst->pParse->db, pConst->apExpr,
128021 pConst->nConst*2*sizeof(Expr*));
128022 if( pConst->apExpr==0 ){
128023 pConst->nConst = 0;
128024 }else{
128025 if( ExprHasProperty(pValue, EP_FixedCol) ) pValue = pValue->pLeft;
128026 pConst->apExpr[pConst->nConst*2-2] = pColumn;
128027 pConst->apExpr[pConst->nConst*2-1] = pValue;
128028 }
128029}
128030
128031/*
128032** Find all terms of COLUMN=VALUE or VALUE=COLUMN in pExpr where VALUE
128033** is a constant expression and where the term must be true because it
128034** is part of the AND-connected terms of the expression. For each term
128035** found, add it to the pConst structure.
128036*/
128037static void findConstInWhere(WhereConst *pConst, Expr *pExpr){
128038 Expr *pRight, *pLeft;
128039 if( pExpr==0 ) return;
128040 if( ExprHasProperty(pExpr, EP_FromJoin) ) return;
128041 if( pExpr->op==TK_AND ){
128042 findConstInWhere(pConst, pExpr->pRight);
128043 findConstInWhere(pConst, pExpr->pLeft);
128044 return;
128045 }
128046 if( pExpr->op!=TK_EQ ) return;
128047 pRight = pExpr->pRight;
128048 pLeft = pExpr->pLeft;
128049 assert( pRight!=0 );
128050 assert( pLeft!=0 );
128051 if( pRight->op==TK_COLUMN
128052 && !ExprHasProperty(pRight, EP_FixedCol)
128053 && sqlite3ExprIsConstant(pLeft)
128054 && sqlite3IsBinary(sqlite3BinaryCompareCollSeq(pConst->pParse,pLeft,pRight))
128055 ){
128056 constInsert(pConst, pRight, pLeft);
128057 }else
128058 if( pLeft->op==TK_COLUMN
128059 && !ExprHasProperty(pLeft, EP_FixedCol)
128060 && sqlite3ExprIsConstant(pRight)
128061 && sqlite3IsBinary(sqlite3BinaryCompareCollSeq(pConst->pParse,pLeft,pRight))
128062 ){
128063 constInsert(pConst, pLeft, pRight);
128064 }
128065}
128066
128067/*
128068** This is a Walker expression callback. pExpr is a candidate expression
128069** to be replaced by a value. If pExpr is equivalent to one of the
128070** columns named in pWalker->u.pConst, then overwrite it with its
128071** corresponding value.
128072*/
128073static int propagateConstantExprRewrite(Walker *pWalker, Expr *pExpr){
128074 int i;
128075 WhereConst *pConst;
128076 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
128077 if( ExprHasProperty(pExpr, EP_FixedCol) ) return WRC_Continue;
128078 pConst = pWalker->u.pConst;
128079 for(i=0; i<pConst->nConst; i++){
128080 Expr *pColumn = pConst->apExpr[i*2];
128081 if( pColumn==pExpr ) continue;
128082 if( pColumn->iTable!=pExpr->iTable ) continue;
128083 if( pColumn->iColumn!=pExpr->iColumn ) continue;
128084 /* A match is found. Add the EP_FixedCol property */
128085 pConst->nChng++;
128086 ExprClearProperty(pExpr, EP_Leaf);
128087 ExprSetProperty(pExpr, EP_FixedCol);
128088 assert( pExpr->pLeft==0 );
128089 pExpr->pLeft = sqlite3ExprDup(pConst->pParse->db, pConst->apExpr[i*2+1], 0);
128090 break;
128091 }
128092 return WRC_Prune;
128093}
128094
128095/*
128096** The WHERE-clause constant propagation optimization.
128097**
128098** If the WHERE clause contains terms of the form COLUMN=CONSTANT or
128099** CONSTANT=COLUMN that must be tree (in other words, if the terms top-level
128100** AND-connected terms that are not part of a ON clause from a LEFT JOIN)
128101** then throughout the query replace all other occurrences of COLUMN
128102** with CONSTANT within the WHERE clause.
128103**
128104** For example, the query:
128105**
128106** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=t1.a AND t3.c=t2.b
128107**
128108** Is transformed into
128109**
128110** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=39 AND t3.c=39
128111**
128112** Return true if any transformations where made and false if not.
128113**
128114** Implementation note: Constant propagation is tricky due to affinity
128115** and collating sequence interactions. Consider this example:
128116**
128117** CREATE TABLE t1(a INT,b TEXT);
128118** INSERT INTO t1 VALUES(123,'0123');
128119** SELECT * FROM t1 WHERE a=123 AND b=a;
128120** SELECT * FROM t1 WHERE a=123 AND b=123;
128121**
128122** The two SELECT statements above should return different answers. b=a
128123** is alway true because the comparison uses numeric affinity, but b=123
128124** is false because it uses text affinity and '0123' is not the same as '123'.
128125** To work around this, the expression tree is not actually changed from
128126** "b=a" to "b=123" but rather the "a" in "b=a" is tagged with EP_FixedCol
128127** and the "123" value is hung off of the pLeft pointer. Code generator
128128** routines know to generate the constant "123" instead of looking up the
128129** column value. Also, to avoid collation problems, this optimization is
128130** only attempted if the "a=123" term uses the default BINARY collation.
128131*/
128132static int propagateConstants(
128133 Parse *pParse, /* The parsing context */
128134 Select *p /* The query in which to propagate constants */
128135){
128136 WhereConst x;
128137 Walker w;
128138 int nChng = 0;
128139 x.pParse = pParse;
128140 do{
128141 x.nConst = 0;
128142 x.nChng = 0;
128143 x.apExpr = 0;
128144 findConstInWhere(&x, p->pWhere);
128145 if( x.nConst ){
128146 memset(&w, 0, sizeof(w));
128147 w.pParse = pParse;
128148 w.xExprCallback = propagateConstantExprRewrite;
128149 w.xSelectCallback = sqlite3SelectWalkNoop;
128150 w.xSelectCallback2 = 0;
128151 w.walkerDepth = 0;
128152 w.u.pConst = &x;
128153 sqlite3WalkExpr(&w, p->pWhere);
128154 sqlite3DbFree(x.pParse->db, x.apExpr);
128155 nChng += x.nChng;
128156 }
128157 }while( x.nChng );
128158 return nChng;
128159}
128160
128161#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
128162/*
128163** Make copies of relevant WHERE clause terms of the outer query into
128164** the WHERE clause of subquery. Example:
128165**
128166** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
128167**
128168** Transformed into:
128169**
128170** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
128171** WHERE x=5 AND y=10;
128172**
128173** The hope is that the terms added to the inner query will make it more
128174** efficient.
128175**
128176** Do not attempt this optimization if:
128177**
128178** (1) (** This restriction was removed on 2017-09-29. We used to
128179** disallow this optimization for aggregate subqueries, but now
128180** it is allowed by putting the extra terms on the HAVING clause.
128181** The added HAVING clause is pointless if the subquery lacks
128182** a GROUP BY clause. But such a HAVING clause is also harmless
128183** so there does not appear to be any reason to add extra logic
128184** to suppress it. **)
128185**
128186** (2) The inner query is the recursive part of a common table expression.
128187**
128188** (3) The inner query has a LIMIT clause (since the changes to the WHERE
128189** clause would change the meaning of the LIMIT).
128190**
128191** (4) The inner query is the right operand of a LEFT JOIN and the
128192** expression to be pushed down does not come from the ON clause
128193** on that LEFT JOIN.
128194**
128195** (5) The WHERE clause expression originates in the ON or USING clause
128196** of a LEFT JOIN where iCursor is not the right-hand table of that
128197** left join. An example:
128198**
128199** SELECT *
128200** FROM (SELECT 1 AS a1 UNION ALL SELECT 2) AS aa
128201** JOIN (SELECT 1 AS b2 UNION ALL SELECT 2) AS bb ON (a1=b2)
128202** LEFT JOIN (SELECT 8 AS c3 UNION ALL SELECT 9) AS cc ON (b2=2);
128203**
128204** The correct answer is three rows: (1,1,NULL),(2,2,8),(2,2,9).
128205** But if the (b2=2) term were to be pushed down into the bb subquery,
128206** then the (1,1,NULL) row would be suppressed.
128207**
128208** (6) The inner query features one or more window-functions (since
128209** changes to the WHERE clause of the inner query could change the
128210** window over which window functions are calculated).
128211**
128212** Return 0 if no changes are made and non-zero if one or more WHERE clause
128213** terms are duplicated into the subquery.
128214*/
128215static int pushDownWhereTerms(
128216 Parse *pParse, /* Parse context (for malloc() and error reporting) */
128217 Select *pSubq, /* The subquery whose WHERE clause is to be augmented */
128218 Expr *pWhere, /* The WHERE clause of the outer query */
128219 int iCursor, /* Cursor number of the subquery */
128220 int isLeftJoin /* True if pSubq is the right term of a LEFT JOIN */
128221){
128222 Expr *pNew;
128223 int nChng = 0;
128224 if( pWhere==0 ) return 0;
128225 if( pSubq->selFlags & SF_Recursive ) return 0; /* restriction (2) */
128226
128227#ifndef SQLITE_OMIT_WINDOWFUNC
128228 if( pSubq->pWin ) return 0; /* restriction (6) */
128229#endif
128230
128231#ifdef SQLITE_DEBUG
128232 /* Only the first term of a compound can have a WITH clause. But make
128233 ** sure no other terms are marked SF_Recursive in case something changes
128234 ** in the future.
128235 */
128236 {
128237 Select *pX;
128238 for(pX=pSubq; pX; pX=pX->pPrior){
128239 assert( (pX->selFlags & (SF_Recursive))==0 );
128240 }
128241 }
128242#endif
128243
128244 if( pSubq->pLimit!=0 ){
128245 return 0; /* restriction (3) */
128246 }
128247 while( pWhere->op==TK_AND ){
128248 nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight,
128249 iCursor, isLeftJoin);
128250 pWhere = pWhere->pLeft;
128251 }
128252 if( isLeftJoin
128253 && (ExprHasProperty(pWhere,EP_FromJoin)==0
128254 || pWhere->iRightJoinTable!=iCursor)
128255 ){
128256 return 0; /* restriction (4) */
128257 }
128258 if( ExprHasProperty(pWhere,EP_FromJoin) && pWhere->iRightJoinTable!=iCursor ){
128259 return 0; /* restriction (5) */
128260 }
128261 if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){
128262 nChng++;
128263 while( pSubq ){
128264 SubstContext x;
128265 pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
128266 unsetJoinExpr(pNew, -1);
128267 x.pParse = pParse;
128268 x.iTable = iCursor;
128269 x.iNewTable = iCursor;
128270 x.isLeftJoin = 0;
128271 x.pEList = pSubq->pEList;
128272 pNew = substExpr(&x, pNew);
128273 if( pSubq->selFlags & SF_Aggregate ){
128274 pSubq->pHaving = sqlite3ExprAnd(pParse->db, pSubq->pHaving, pNew);
128275 }else{
128276 pSubq->pWhere = sqlite3ExprAnd(pParse->db, pSubq->pWhere, pNew);
128277 }
128278 pSubq = pSubq->pPrior;
128279 }
128280 }
128281 return nChng;
128282}
128283#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
128284
128285/*
128286** The pFunc is the only aggregate function in the query. Check to see
128287** if the query is a candidate for the min/max optimization.
128288**
128289** If the query is a candidate for the min/max optimization, then set
128290** *ppMinMax to be an ORDER BY clause to be used for the optimization
128291** and return either WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX depending on
128292** whether pFunc is a min() or max() function.
128293**
128294** If the query is not a candidate for the min/max optimization, return
128295** WHERE_ORDERBY_NORMAL (which must be zero).
128296**
128297** This routine must be called after aggregate functions have been
128298** located but before their arguments have been subjected to aggregate
128299** analysis.
128300*/
128301static u8 minMaxQuery(sqlite3 *db, Expr *pFunc, ExprList **ppMinMax){
128302 int eRet = WHERE_ORDERBY_NORMAL; /* Return value */
128303 ExprList *pEList = pFunc->x.pList; /* Arguments to agg function */
128304 const char *zFunc; /* Name of aggregate function pFunc */
128305 ExprList *pOrderBy;
128306 u8 sortOrder;
128307
128308 assert( *ppMinMax==0 );
128309 assert( pFunc->op==TK_AGG_FUNCTION );
128310 if( pEList==0 || pEList->nExpr!=1 ) return eRet;
128311 zFunc = pFunc->u.zToken;
128312 if( sqlite3StrICmp(zFunc, "min")==0 ){
128313 eRet = WHERE_ORDERBY_MIN;
128314 sortOrder = SQLITE_SO_ASC;
128315 }else if( sqlite3StrICmp(zFunc, "max")==0 ){
128316 eRet = WHERE_ORDERBY_MAX;
128317 sortOrder = SQLITE_SO_DESC;
128318 }else{
128319 return eRet;
128320 }
128321 *ppMinMax = pOrderBy = sqlite3ExprListDup(db, pEList, 0);
128322 assert( pOrderBy!=0 || db->mallocFailed );
128323 if( pOrderBy ) pOrderBy->a[0].sortOrder = sortOrder;
128324 return eRet;
128325}
128326
128327/*
128328** The select statement passed as the first argument is an aggregate query.
128329** The second argument is the associated aggregate-info object. This
128330** function tests if the SELECT is of the form:
128331**
128332** SELECT count(*) FROM <tbl>
128333**
128334** where table is a database table, not a sub-select or view. If the query
128335** does match this pattern, then a pointer to the Table object representing
128336** <tbl> is returned. Otherwise, 0 is returned.
128337*/
128338static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
128339 Table *pTab;
128340 Expr *pExpr;
128341
128342 assert( !p->pGroupBy );
128343
128344 if( p->pWhere || p->pEList->nExpr!=1
128345 || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect
128346 ){
128347 return 0;
128348 }
128349 pTab = p->pSrc->a[0].pTab;
128350 pExpr = p->pEList->a[0].pExpr;
128351 assert( pTab && !pTab->pSelect && pExpr );
128352
128353 if( IsVirtual(pTab) ) return 0;
128354 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
128355 if( NEVER(pAggInfo->nFunc==0) ) return 0;
128356 if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
128357 if( pExpr->flags&EP_Distinct ) return 0;
128358
128359 return pTab;
128360}
128361
128362/*
128363** If the source-list item passed as an argument was augmented with an
128364** INDEXED BY clause, then try to locate the specified index. If there
128365** was such a clause and the named index cannot be found, return
128366** SQLITE_ERROR and leave an error in pParse. Otherwise, populate
128367** pFrom->pIndex and return SQLITE_OK.
128368*/
128369SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
128370 if( pFrom->pTab && pFrom->fg.isIndexedBy ){
128371 Table *pTab = pFrom->pTab;
128372 char *zIndexedBy = pFrom->u1.zIndexedBy;
128373 Index *pIdx;
128374 for(pIdx=pTab->pIndex;
128375 pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy);
128376 pIdx=pIdx->pNext
128377 );
128378 if( !pIdx ){
128379 sqlite3ErrorMsg(pParse, "no such index: %s", zIndexedBy, 0);
128380 pParse->checkSchema = 1;
128381 return SQLITE_ERROR;
128382 }
128383 pFrom->pIBIndex = pIdx;
128384 }
128385 return SQLITE_OK;
128386}
128387/*
128388** Detect compound SELECT statements that use an ORDER BY clause with
128389** an alternative collating sequence.
128390**
128391** SELECT ... FROM t1 EXCEPT SELECT ... FROM t2 ORDER BY .. COLLATE ...
128392**
128393** These are rewritten as a subquery:
128394**
128395** SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2)
128396** ORDER BY ... COLLATE ...
128397**
128398** This transformation is necessary because the multiSelectOrderBy() routine
128399** above that generates the code for a compound SELECT with an ORDER BY clause
128400** uses a merge algorithm that requires the same collating sequence on the
128401** result columns as on the ORDER BY clause. See ticket
128402** http://www.sqlite.org/src/info/6709574d2a
128403**
128404** This transformation is only needed for EXCEPT, INTERSECT, and UNION.
128405** The UNION ALL operator works fine with multiSelectOrderBy() even when
128406** there are COLLATE terms in the ORDER BY.
128407*/
128408static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){
128409 int i;
128410 Select *pNew;
128411 Select *pX;
128412 sqlite3 *db;
128413 struct ExprList_item *a;
128414 SrcList *pNewSrc;
128415 Parse *pParse;
128416 Token dummy;
128417
128418 if( p->pPrior==0 ) return WRC_Continue;
128419 if( p->pOrderBy==0 ) return WRC_Continue;
128420 for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){}
128421 if( pX==0 ) return WRC_Continue;
128422 a = p->pOrderBy->a;
128423 for(i=p->pOrderBy->nExpr-1; i>=0; i--){
128424 if( a[i].pExpr->flags & EP_Collate ) break;
128425 }
128426 if( i<0 ) return WRC_Continue;
128427
128428 /* If we reach this point, that means the transformation is required. */
128429
128430 pParse = pWalker->pParse;
128431 db = pParse->db;
128432 pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
128433 if( pNew==0 ) return WRC_Abort;
128434 memset(&dummy, 0, sizeof(dummy));
128435 pNewSrc = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&dummy,pNew,0,0);
128436 if( pNewSrc==0 ) return WRC_Abort;
128437 *pNew = *p;
128438 p->pSrc = pNewSrc;
128439 p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0));
128440 p->op = TK_SELECT;
128441 p->pWhere = 0;
128442 pNew->pGroupBy = 0;
128443 pNew->pHaving = 0;
128444 pNew->pOrderBy = 0;
128445 p->pPrior = 0;
128446 p->pNext = 0;
128447 p->pWith = 0;
128448 p->selFlags &= ~SF_Compound;
128449 assert( (p->selFlags & SF_Converted)==0 );
128450 p->selFlags |= SF_Converted;
128451 assert( pNew->pPrior!=0 );
128452 pNew->pPrior->pNext = pNew;
128453 pNew->pLimit = 0;
128454 return WRC_Continue;
128455}
128456
128457/*
128458** Check to see if the FROM clause term pFrom has table-valued function
128459** arguments. If it does, leave an error message in pParse and return
128460** non-zero, since pFrom is not allowed to be a table-valued function.
128461*/
128462static int cannotBeFunction(Parse *pParse, struct SrcList_item *pFrom){
128463 if( pFrom->fg.isTabFunc ){
128464 sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName);
128465 return 1;
128466 }
128467 return 0;
128468}
128469
128470#ifndef SQLITE_OMIT_CTE
128471/*
128472** Argument pWith (which may be NULL) points to a linked list of nested
128473** WITH contexts, from inner to outermost. If the table identified by
128474** FROM clause element pItem is really a common-table-expression (CTE)
128475** then return a pointer to the CTE definition for that table. Otherwise
128476** return NULL.
128477**
128478** If a non-NULL value is returned, set *ppContext to point to the With
128479** object that the returned CTE belongs to.
128480*/
128481static struct Cte *searchWith(
128482 With *pWith, /* Current innermost WITH clause */
128483 struct SrcList_item *pItem, /* FROM clause element to resolve */
128484 With **ppContext /* OUT: WITH clause return value belongs to */
128485){
128486 const char *zName;
128487 if( pItem->zDatabase==0 && (zName = pItem->zName)!=0 ){
128488 With *p;
128489 for(p=pWith; p; p=p->pOuter){
128490 int i;
128491 for(i=0; i<p->nCte; i++){
128492 if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
128493 *ppContext = p;
128494 return &p->a[i];
128495 }
128496 }
128497 }
128498 }
128499 return 0;
128500}
128501
128502/* The code generator maintains a stack of active WITH clauses
128503** with the inner-most WITH clause being at the top of the stack.
128504**
128505** This routine pushes the WITH clause passed as the second argument
128506** onto the top of the stack. If argument bFree is true, then this
128507** WITH clause will never be popped from the stack. In this case it
128508** should be freed along with the Parse object. In other cases, when
128509** bFree==0, the With object will be freed along with the SELECT
128510** statement with which it is associated.
128511*/
128512SQLITE_PRIVATE void sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){
128513 assert( bFree==0 || (pParse->pWith==0 && pParse->pWithToFree==0) );
128514 if( pWith ){
128515 assert( pParse->pWith!=pWith );
128516 pWith->pOuter = pParse->pWith;
128517 pParse->pWith = pWith;
128518 if( bFree ) pParse->pWithToFree = pWith;
128519 }
128520}
128521
128522/*
128523** This function checks if argument pFrom refers to a CTE declared by
128524** a WITH clause on the stack currently maintained by the parser. And,
128525** if currently processing a CTE expression, if it is a recursive
128526** reference to the current CTE.
128527**
128528** If pFrom falls into either of the two categories above, pFrom->pTab
128529** and other fields are populated accordingly. The caller should check
128530** (pFrom->pTab!=0) to determine whether or not a successful match
128531** was found.
128532**
128533** Whether or not a match is found, SQLITE_OK is returned if no error
128534** occurs. If an error does occur, an error message is stored in the
128535** parser and some error code other than SQLITE_OK returned.
128536*/
128537static int withExpand(
128538 Walker *pWalker,
128539 struct SrcList_item *pFrom
128540){
128541 Parse *pParse = pWalker->pParse;
128542 sqlite3 *db = pParse->db;
128543 struct Cte *pCte; /* Matched CTE (or NULL if no match) */
128544 With *pWith; /* WITH clause that pCte belongs to */
128545
128546 assert( pFrom->pTab==0 );
128547
128548 pCte = searchWith(pParse->pWith, pFrom, &pWith);
128549 if( pCte ){
128550 Table *pTab;
128551 ExprList *pEList;
128552 Select *pSel;
128553 Select *pLeft; /* Left-most SELECT statement */
128554 int bMayRecursive; /* True if compound joined by UNION [ALL] */
128555 With *pSavedWith; /* Initial value of pParse->pWith */
128556
128557 /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
128558 ** recursive reference to CTE pCte. Leave an error in pParse and return
128559 ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
128560 ** In this case, proceed. */
128561 if( pCte->zCteErr ){
128562 sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName);
128563 return SQLITE_ERROR;
128564 }
128565 if( cannotBeFunction(pParse, pFrom) ) return SQLITE_ERROR;
128566
128567 assert( pFrom->pTab==0 );
128568 pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
128569 if( pTab==0 ) return WRC_Abort;
128570 pTab->nTabRef = 1;
128571 pTab->zName = sqlite3DbStrDup(db, pCte->zName);
128572 pTab->iPKey = -1;
128573 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
128574 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
128575 pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
128576 if( db->mallocFailed ) return SQLITE_NOMEM_BKPT;
128577 assert( pFrom->pSelect );
128578
128579 /* Check if this is a recursive CTE. */
128580 pSel = pFrom->pSelect;
128581 bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION );
128582 if( bMayRecursive ){
128583 int i;
128584 SrcList *pSrc = pFrom->pSelect->pSrc;
128585 for(i=0; i<pSrc->nSrc; i++){
128586 struct SrcList_item *pItem = &pSrc->a[i];
128587 if( pItem->zDatabase==0
128588 && pItem->zName!=0
128589 && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
128590 ){
128591 pItem->pTab = pTab;
128592 pItem->fg.isRecursive = 1;
128593 pTab->nTabRef++;
128594 pSel->selFlags |= SF_Recursive;
128595 }
128596 }
128597 }
128598
128599 /* Only one recursive reference is permitted. */
128600 if( pTab->nTabRef>2 ){
128601 sqlite3ErrorMsg(
128602 pParse, "multiple references to recursive table: %s", pCte->zName
128603 );
128604 return SQLITE_ERROR;
128605 }
128606 assert( pTab->nTabRef==1 ||
128607 ((pSel->selFlags&SF_Recursive) && pTab->nTabRef==2 ));
128608
128609 pCte->zCteErr = "circular reference: %s";
128610 pSavedWith = pParse->pWith;
128611 pParse->pWith = pWith;
128612 if( bMayRecursive ){
128613 Select *pPrior = pSel->pPrior;
128614 assert( pPrior->pWith==0 );
128615 pPrior->pWith = pSel->pWith;
128616 sqlite3WalkSelect(pWalker, pPrior);
128617 pPrior->pWith = 0;
128618 }else{
128619 sqlite3WalkSelect(pWalker, pSel);
128620 }
128621 pParse->pWith = pWith;
128622
128623 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
128624 pEList = pLeft->pEList;
128625 if( pCte->pCols ){
128626 if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
128627 sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns",
128628 pCte->zName, pEList->nExpr, pCte->pCols->nExpr
128629 );
128630 pParse->pWith = pSavedWith;
128631 return SQLITE_ERROR;
128632 }
128633 pEList = pCte->pCols;
128634 }
128635
128636 sqlite3ColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
128637 if( bMayRecursive ){
128638 if( pSel->selFlags & SF_Recursive ){
128639 pCte->zCteErr = "multiple recursive references: %s";
128640 }else{
128641 pCte->zCteErr = "recursive reference in a subquery: %s";
128642 }
128643 sqlite3WalkSelect(pWalker, pSel);
128644 }
128645 pCte->zCteErr = 0;
128646 pParse->pWith = pSavedWith;
128647 }
128648
128649 return SQLITE_OK;
128650}
128651#endif
128652
128653#ifndef SQLITE_OMIT_CTE
128654/*
128655** If the SELECT passed as the second argument has an associated WITH
128656** clause, pop it from the stack stored as part of the Parse object.
128657**
128658** This function is used as the xSelectCallback2() callback by
128659** sqlite3SelectExpand() when walking a SELECT tree to resolve table
128660** names and other FROM clause elements.
128661*/
128662static void selectPopWith(Walker *pWalker, Select *p){
128663 Parse *pParse = pWalker->pParse;
128664 if( OK_IF_ALWAYS_TRUE(pParse->pWith) && p->pPrior==0 ){
128665 With *pWith = findRightmost(p)->pWith;
128666 if( pWith!=0 ){
128667 assert( pParse->pWith==pWith );
128668 pParse->pWith = pWith->pOuter;
128669 }
128670 }
128671}
128672#else
128673#define selectPopWith 0
128674#endif
128675
128676/*
128677** The SrcList_item structure passed as the second argument represents a
128678** sub-query in the FROM clause of a SELECT statement. This function
128679** allocates and populates the SrcList_item.pTab object. If successful,
128680** SQLITE_OK is returned. Otherwise, if an OOM error is encountered,
128681** SQLITE_NOMEM.
128682*/
128683SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse *pParse, struct SrcList_item *pFrom){
128684 Select *pSel = pFrom->pSelect;
128685 Table *pTab;
128686
128687 assert( pSel );
128688 pFrom->pTab = pTab = sqlite3DbMallocZero(pParse->db, sizeof(Table));
128689 if( pTab==0 ) return SQLITE_NOMEM;
128690 pTab->nTabRef = 1;
128691 if( pFrom->zAlias ){
128692 pTab->zName = sqlite3DbStrDup(pParse->db, pFrom->zAlias);
128693 }else{
128694 pTab->zName = sqlite3MPrintf(pParse->db, "subquery_%u", pSel->selId);
128695 }
128696 while( pSel->pPrior ){ pSel = pSel->pPrior; }
128697 sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol);
128698 pTab->iPKey = -1;
128699 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
128700 pTab->tabFlags |= TF_Ephemeral;
128701
128702 return SQLITE_OK;
128703}
128704
128705/*
128706** This routine is a Walker callback for "expanding" a SELECT statement.
128707** "Expanding" means to do the following:
128708**
128709** (1) Make sure VDBE cursor numbers have been assigned to every
128710** element of the FROM clause.
128711**
128712** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
128713** defines FROM clause. When views appear in the FROM clause,
128714** fill pTabList->a[].pSelect with a copy of the SELECT statement
128715** that implements the view. A copy is made of the view's SELECT
128716** statement so that we can freely modify or delete that statement
128717** without worrying about messing up the persistent representation
128718** of the view.
128719**
128720** (3) Add terms to the WHERE clause to accommodate the NATURAL keyword
128721** on joins and the ON and USING clause of joins.
128722**
128723** (4) Scan the list of columns in the result set (pEList) looking
128724** for instances of the "*" operator or the TABLE.* operator.
128725** If found, expand each "*" to be every column in every table
128726** and TABLE.* to be every column in TABLE.
128727**
128728*/
128729static int selectExpander(Walker *pWalker, Select *p){
128730 Parse *pParse = pWalker->pParse;
128731 int i, j, k;
128732 SrcList *pTabList;
128733 ExprList *pEList;
128734 struct SrcList_item *pFrom;
128735 sqlite3 *db = pParse->db;
128736 Expr *pE, *pRight, *pExpr;
128737 u16 selFlags = p->selFlags;
128738 u32 elistFlags = 0;
128739
128740 p->selFlags |= SF_Expanded;
128741 if( db->mallocFailed ){
128742 return WRC_Abort;
128743 }
128744 assert( p->pSrc!=0 );
128745 if( (selFlags & SF_Expanded)!=0 ){
128746 return WRC_Prune;
128747 }
128748 pTabList = p->pSrc;
128749 pEList = p->pEList;
128750 sqlite3WithPush(pParse, p->pWith, 0);
128751
128752 /* Make sure cursor numbers have been assigned to all entries in
128753 ** the FROM clause of the SELECT statement.
128754 */
128755 sqlite3SrcListAssignCursors(pParse, pTabList);
128756
128757 /* Look up every table named in the FROM clause of the select. If
128758 ** an entry of the FROM clause is a subquery instead of a table or view,
128759 ** then create a transient table structure to describe the subquery.
128760 */
128761 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
128762 Table *pTab;
128763 assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 );
128764 if( pFrom->fg.isRecursive ) continue;
128765 assert( pFrom->pTab==0 );
128766#ifndef SQLITE_OMIT_CTE
128767 if( withExpand(pWalker, pFrom) ) return WRC_Abort;
128768 if( pFrom->pTab ) {} else
128769#endif
128770 if( pFrom->zName==0 ){
128771#ifndef SQLITE_OMIT_SUBQUERY
128772 Select *pSel = pFrom->pSelect;
128773 /* A sub-query in the FROM clause of a SELECT */
128774 assert( pSel!=0 );
128775 assert( pFrom->pTab==0 );
128776 if( sqlite3WalkSelect(pWalker, pSel) ) return WRC_Abort;
128777 if( sqlite3ExpandSubquery(pParse, pFrom) ) return WRC_Abort;
128778#endif
128779 }else{
128780 /* An ordinary table or view name in the FROM clause */
128781 assert( pFrom->pTab==0 );
128782 pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
128783 if( pTab==0 ) return WRC_Abort;
128784 if( pTab->nTabRef>=0xffff ){
128785 sqlite3ErrorMsg(pParse, "too many references to \"%s\": max 65535",
128786 pTab->zName);
128787 pFrom->pTab = 0;
128788 return WRC_Abort;
128789 }
128790 pTab->nTabRef++;
128791 if( !IsVirtual(pTab) && cannotBeFunction(pParse, pFrom) ){
128792 return WRC_Abort;
128793 }
128794#if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
128795 if( IsVirtual(pTab) || pTab->pSelect ){
128796 i16 nCol;
128797 if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
128798 assert( pFrom->pSelect==0 );
128799 pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
128800 nCol = pTab->nCol;
128801 pTab->nCol = -1;
128802 sqlite3WalkSelect(pWalker, pFrom->pSelect);
128803 pTab->nCol = nCol;
128804 }
128805#endif
128806 }
128807
128808 /* Locate the index named by the INDEXED BY clause, if any. */
128809 if( sqlite3IndexedByLookup(pParse, pFrom) ){
128810 return WRC_Abort;
128811 }
128812 }
128813
128814 /* Process NATURAL keywords, and ON and USING clauses of joins.
128815 */
128816 if( db->mallocFailed || sqliteProcessJoin(pParse, p) ){
128817 return WRC_Abort;
128818 }
128819
128820 /* For every "*" that occurs in the column list, insert the names of
128821 ** all columns in all tables. And for every TABLE.* insert the names
128822 ** of all columns in TABLE. The parser inserted a special expression
128823 ** with the TK_ASTERISK operator for each "*" that it found in the column
128824 ** list. The following code just has to locate the TK_ASTERISK
128825 ** expressions and expand each one to the list of all columns in
128826 ** all tables.
128827 **
128828 ** The first loop just checks to see if there are any "*" operators
128829 ** that need expanding.
128830 */
128831 for(k=0; k<pEList->nExpr; k++){
128832 pE = pEList->a[k].pExpr;
128833 if( pE->op==TK_ASTERISK ) break;
128834 assert( pE->op!=TK_DOT || pE->pRight!=0 );
128835 assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
128836 if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
128837 elistFlags |= pE->flags;
128838 }
128839 if( k<pEList->nExpr ){
128840 /*
128841 ** If we get here it means the result set contains one or more "*"
128842 ** operators that need to be expanded. Loop through each expression
128843 ** in the result set and expand them one by one.
128844 */
128845 struct ExprList_item *a = pEList->a;
128846 ExprList *pNew = 0;
128847 int flags = pParse->db->flags;
128848 int longNames = (flags & SQLITE_FullColNames)!=0
128849 && (flags & SQLITE_ShortColNames)==0;
128850
128851 for(k=0; k<pEList->nExpr; k++){
128852 pE = a[k].pExpr;
128853 elistFlags |= pE->flags;
128854 pRight = pE->pRight;
128855 assert( pE->op!=TK_DOT || pRight!=0 );
128856 if( pE->op!=TK_ASTERISK
128857 && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
128858 ){
128859 /* This particular expression does not need to be expanded.
128860 */
128861 pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
128862 if( pNew ){
128863 pNew->a[pNew->nExpr-1].zName = a[k].zName;
128864 pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan;
128865 a[k].zName = 0;
128866 a[k].zSpan = 0;
128867 }
128868 a[k].pExpr = 0;
128869 }else{
128870 /* This expression is a "*" or a "TABLE.*" and needs to be
128871 ** expanded. */
128872 int tableSeen = 0; /* Set to 1 when TABLE matches */
128873 char *zTName = 0; /* text of name of TABLE */
128874 if( pE->op==TK_DOT ){
128875 assert( pE->pLeft!=0 );
128876 assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
128877 zTName = pE->pLeft->u.zToken;
128878 }
128879 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
128880 Table *pTab = pFrom->pTab;
128881 Select *pSub = pFrom->pSelect;
128882 char *zTabName = pFrom->zAlias;
128883 const char *zSchemaName = 0;
128884 int iDb;
128885 if( zTabName==0 ){
128886 zTabName = pTab->zName;
128887 }
128888 if( db->mallocFailed ) break;
128889 if( pSub==0 || (pSub->selFlags & SF_NestedFrom)==0 ){
128890 pSub = 0;
128891 if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
128892 continue;
128893 }
128894 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
128895 zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*";
128896 }
128897 for(j=0; j<pTab->nCol; j++){
128898 char *zName = pTab->aCol[j].zName;
128899 char *zColname; /* The computed column name */
128900 char *zToFree; /* Malloced string that needs to be freed */
128901 Token sColname; /* Computed column name as a token */
128902
128903 assert( zName );
128904 if( zTName && pSub
128905 && sqlite3MatchSpanName(pSub->pEList->a[j].zSpan, 0, zTName, 0)==0
128906 ){
128907 continue;
128908 }
128909
128910 /* If a column is marked as 'hidden', omit it from the expanded
128911 ** result-set list unless the SELECT has the SF_IncludeHidden
128912 ** bit set.
128913 */
128914 if( (p->selFlags & SF_IncludeHidden)==0
128915 && IsHiddenColumn(&pTab->aCol[j])
128916 ){
128917 continue;
128918 }
128919 tableSeen = 1;
128920
128921 if( i>0 && zTName==0 ){
128922 if( (pFrom->fg.jointype & JT_NATURAL)!=0
128923 && tableAndColumnIndex(pTabList, i, zName, 0, 0)
128924 ){
128925 /* In a NATURAL join, omit the join columns from the
128926 ** table to the right of the join */
128927 continue;
128928 }
128929 if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){
128930 /* In a join with a USING clause, omit columns in the
128931 ** using clause from the table on the right. */
128932 continue;
128933 }
128934 }
128935 pRight = sqlite3Expr(db, TK_ID, zName);
128936 zColname = zName;
128937 zToFree = 0;
128938 if( longNames || pTabList->nSrc>1 ){
128939 Expr *pLeft;
128940 pLeft = sqlite3Expr(db, TK_ID, zTabName);
128941 pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
128942 if( zSchemaName ){
128943 pLeft = sqlite3Expr(db, TK_ID, zSchemaName);
128944 pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pExpr);
128945 }
128946 if( longNames ){
128947 zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
128948 zToFree = zColname;
128949 }
128950 }else{
128951 pExpr = pRight;
128952 }
128953 pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
128954 sqlite3TokenInit(&sColname, zColname);
128955 sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
128956 if( pNew && (p->selFlags & SF_NestedFrom)!=0 ){
128957 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
128958 if( pSub ){
128959 pX->zSpan = sqlite3DbStrDup(db, pSub->pEList->a[j].zSpan);
128960 testcase( pX->zSpan==0 );
128961 }else{
128962 pX->zSpan = sqlite3MPrintf(db, "%s.%s.%s",
128963 zSchemaName, zTabName, zColname);
128964 testcase( pX->zSpan==0 );
128965 }
128966 pX->bSpanIsTab = 1;
128967 }
128968 sqlite3DbFree(db, zToFree);
128969 }
128970 }
128971 if( !tableSeen ){
128972 if( zTName ){
128973 sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
128974 }else{
128975 sqlite3ErrorMsg(pParse, "no tables specified");
128976 }
128977 }
128978 }
128979 }
128980 sqlite3ExprListDelete(db, pEList);
128981 p->pEList = pNew;
128982 }
128983 if( p->pEList ){
128984 if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
128985 sqlite3ErrorMsg(pParse, "too many columns in result set");
128986 return WRC_Abort;
128987 }
128988 if( (elistFlags & (EP_HasFunc|EP_Subquery))!=0 ){
128989 p->selFlags |= SF_ComplexResult;
128990 }
128991 }
128992 return WRC_Continue;
128993}
128994
128995/*
128996** No-op routine for the parse-tree walker.
128997**
128998** When this routine is the Walker.xExprCallback then expression trees
128999** are walked without any actions being taken at each node. Presumably,
129000** when this routine is used for Walker.xExprCallback then
129001** Walker.xSelectCallback is set to do something useful for every
129002** subquery in the parser tree.
129003*/
129004SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
129005 UNUSED_PARAMETER2(NotUsed, NotUsed2);
129006 return WRC_Continue;
129007}
129008
129009/*
129010** No-op routine for the parse-tree walker for SELECT statements.
129011** subquery in the parser tree.
129012*/
129013SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker *NotUsed, Select *NotUsed2){
129014 UNUSED_PARAMETER2(NotUsed, NotUsed2);
129015 return WRC_Continue;
129016}
129017
129018#if SQLITE_DEBUG
129019/*
129020** Always assert. This xSelectCallback2 implementation proves that the
129021** xSelectCallback2 is never invoked.
129022*/
129023SQLITE_PRIVATE void sqlite3SelectWalkAssert2(Walker *NotUsed, Select *NotUsed2){
129024 UNUSED_PARAMETER2(NotUsed, NotUsed2);
129025 assert( 0 );
129026}
129027#endif
129028/*
129029** This routine "expands" a SELECT statement and all of its subqueries.
129030** For additional information on what it means to "expand" a SELECT
129031** statement, see the comment on the selectExpand worker callback above.
129032**
129033** Expanding a SELECT statement is the first step in processing a
129034** SELECT statement. The SELECT statement must be expanded before
129035** name resolution is performed.
129036**
129037** If anything goes wrong, an error message is written into pParse.
129038** The calling function can detect the problem by looking at pParse->nErr
129039** and/or pParse->db->mallocFailed.
129040*/
129041static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
129042 Walker w;
129043 w.xExprCallback = sqlite3ExprWalkNoop;
129044 w.pParse = pParse;
129045 if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){
129046 w.xSelectCallback = convertCompoundSelectToSubquery;
129047 w.xSelectCallback2 = 0;
129048 sqlite3WalkSelect(&w, pSelect);
129049 }
129050 w.xSelectCallback = selectExpander;
129051 w.xSelectCallback2 = selectPopWith;
129052 sqlite3WalkSelect(&w, pSelect);
129053}
129054
129055
129056#ifndef SQLITE_OMIT_SUBQUERY
129057/*
129058** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
129059** interface.
129060**
129061** For each FROM-clause subquery, add Column.zType and Column.zColl
129062** information to the Table structure that represents the result set
129063** of that subquery.
129064**
129065** The Table structure that represents the result set was constructed
129066** by selectExpander() but the type and collation information was omitted
129067** at that point because identifiers had not yet been resolved. This
129068** routine is called after identifier resolution.
129069*/
129070static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
129071 Parse *pParse;
129072 int i;
129073 SrcList *pTabList;
129074 struct SrcList_item *pFrom;
129075
129076 assert( p->selFlags & SF_Resolved );
129077 if( p->selFlags & SF_HasTypeInfo ) return;
129078 p->selFlags |= SF_HasTypeInfo;
129079 pParse = pWalker->pParse;
129080 pTabList = p->pSrc;
129081 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
129082 Table *pTab = pFrom->pTab;
129083 assert( pTab!=0 );
129084 if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
129085 /* A sub-query in the FROM clause of a SELECT */
129086 Select *pSel = pFrom->pSelect;
129087 if( pSel ){
129088 while( pSel->pPrior ) pSel = pSel->pPrior;
129089 sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSel);
129090 }
129091 }
129092 }
129093}
129094#endif
129095
129096
129097/*
129098** This routine adds datatype and collating sequence information to
129099** the Table structures of all FROM-clause subqueries in a
129100** SELECT statement.
129101**
129102** Use this routine after name resolution.
129103*/
129104static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
129105#ifndef SQLITE_OMIT_SUBQUERY
129106 Walker w;
129107 w.xSelectCallback = sqlite3SelectWalkNoop;
129108 w.xSelectCallback2 = selectAddSubqueryTypeInfo;
129109 w.xExprCallback = sqlite3ExprWalkNoop;
129110 w.pParse = pParse;
129111 sqlite3WalkSelect(&w, pSelect);
129112#endif
129113}
129114
129115
129116/*
129117** This routine sets up a SELECT statement for processing. The
129118** following is accomplished:
129119**
129120** * VDBE Cursor numbers are assigned to all FROM-clause terms.
129121** * Ephemeral Table objects are created for all FROM-clause subqueries.
129122** * ON and USING clauses are shifted into WHERE statements
129123** * Wildcards "*" and "TABLE.*" in result sets are expanded.
129124** * Identifiers in expression are matched to tables.
129125**
129126** This routine acts recursively on all subqueries within the SELECT.
129127*/
129128SQLITE_PRIVATE void sqlite3SelectPrep(
129129 Parse *pParse, /* The parser context */
129130 Select *p, /* The SELECT statement being coded. */
129131 NameContext *pOuterNC /* Name context for container */
129132){
129133 assert( p!=0 || pParse->db->mallocFailed );
129134 if( pParse->db->mallocFailed ) return;
129135 if( p->selFlags & SF_HasTypeInfo ) return;
129136 sqlite3SelectExpand(pParse, p);
129137 if( pParse->nErr || pParse->db->mallocFailed ) return;
129138 sqlite3ResolveSelectNames(pParse, p, pOuterNC);
129139 if( pParse->nErr || pParse->db->mallocFailed ) return;
129140 sqlite3SelectAddTypeInfo(pParse, p);
129141}
129142
129143/*
129144** Reset the aggregate accumulator.
129145**
129146** The aggregate accumulator is a set of memory cells that hold
129147** intermediate results while calculating an aggregate. This
129148** routine generates code that stores NULLs in all of those memory
129149** cells.
129150*/
129151static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
129152 Vdbe *v = pParse->pVdbe;
129153 int i;
129154 struct AggInfo_func *pFunc;
129155 int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
129156 if( nReg==0 ) return;
129157#ifdef SQLITE_DEBUG
129158 /* Verify that all AggInfo registers are within the range specified by
129159 ** AggInfo.mnReg..AggInfo.mxReg */
129160 assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 );
129161 for(i=0; i<pAggInfo->nColumn; i++){
129162 assert( pAggInfo->aCol[i].iMem>=pAggInfo->mnReg
129163 && pAggInfo->aCol[i].iMem<=pAggInfo->mxReg );
129164 }
129165 for(i=0; i<pAggInfo->nFunc; i++){
129166 assert( pAggInfo->aFunc[i].iMem>=pAggInfo->mnReg
129167 && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg );
129168 }
129169#endif
129170 sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->mnReg, pAggInfo->mxReg);
129171 for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
129172 if( pFunc->iDistinct>=0 ){
129173 Expr *pE = pFunc->pExpr;
129174 assert( !ExprHasProperty(pE, EP_xIsSelect) );
129175 if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
129176 sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
129177 "argument");
129178 pFunc->iDistinct = -1;
129179 }else{
129180 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pE->x.pList,0,0);
129181 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
129182 (char*)pKeyInfo, P4_KEYINFO);
129183 }
129184 }
129185 }
129186}
129187
129188/*
129189** Invoke the OP_AggFinalize opcode for every aggregate function
129190** in the AggInfo structure.
129191*/
129192static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
129193 Vdbe *v = pParse->pVdbe;
129194 int i;
129195 struct AggInfo_func *pF;
129196 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
129197 ExprList *pList = pF->pExpr->x.pList;
129198 assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
129199 sqlite3VdbeAddOp2(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0);
129200 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
129201 }
129202}
129203
129204
129205/*
129206** Update the accumulator memory cells for an aggregate based on
129207** the current cursor position.
129208**
129209** If regAcc is non-zero and there are no min() or max() aggregates
129210** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
129211** registers i register regAcc contains 0. The caller will take care
129212** of setting and clearing regAcc.
129213*/
129214static void updateAccumulator(Parse *pParse, int regAcc, AggInfo *pAggInfo){
129215 Vdbe *v = pParse->pVdbe;
129216 int i;
129217 int regHit = 0;
129218 int addrHitTest = 0;
129219 struct AggInfo_func *pF;
129220 struct AggInfo_col *pC;
129221
129222 pAggInfo->directMode = 1;
129223 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
129224 int nArg;
129225 int addrNext = 0;
129226 int regAgg;
129227 ExprList *pList = pF->pExpr->x.pList;
129228 assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
129229 if( pList ){
129230 nArg = pList->nExpr;
129231 regAgg = sqlite3GetTempRange(pParse, nArg);
129232 sqlite3ExprCodeExprList(pParse, pList, regAgg, 0, SQLITE_ECEL_DUP);
129233 }else{
129234 nArg = 0;
129235 regAgg = 0;
129236 }
129237 if( pF->iDistinct>=0 ){
129238 addrNext = sqlite3VdbeMakeLabel(v);
129239 testcase( nArg==0 ); /* Error condition */
129240 testcase( nArg>1 ); /* Also an error */
129241 codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
129242 }
129243 if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
129244 CollSeq *pColl = 0;
129245 struct ExprList_item *pItem;
129246 int j;
129247 assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */
129248 for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
129249 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
129250 }
129251 if( !pColl ){
129252 pColl = pParse->db->pDfltColl;
129253 }
129254 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
129255 sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ);
129256 }
129257 sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, pF->iMem);
129258 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
129259 sqlite3VdbeChangeP5(v, (u8)nArg);
129260 sqlite3ReleaseTempRange(pParse, regAgg, nArg);
129261 if( addrNext ){
129262 sqlite3VdbeResolveLabel(v, addrNext);
129263 }
129264 }
129265 if( regHit==0 && pAggInfo->nAccumulator ){
129266 regHit = regAcc;
129267 }
129268 if( regHit ){
129269 addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit); VdbeCoverage(v);
129270 }
129271 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
129272 sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
129273 }
129274 pAggInfo->directMode = 0;
129275 if( addrHitTest ){
129276 sqlite3VdbeJumpHere(v, addrHitTest);
129277 }
129278}
129279
129280/*
129281** Add a single OP_Explain instruction to the VDBE to explain a simple
129282** count(*) query ("SELECT count(*) FROM pTab").
129283*/
129284#ifndef SQLITE_OMIT_EXPLAIN
129285static void explainSimpleCount(
129286 Parse *pParse, /* Parse context */
129287 Table *pTab, /* Table being queried */
129288 Index *pIdx /* Index used to optimize scan, or NULL */
129289){
129290 if( pParse->explain==2 ){
129291 int bCover = (pIdx!=0 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pIdx)));
129292 sqlite3VdbeExplain(pParse, 0, "SCAN TABLE %s%s%s",
129293 pTab->zName,
129294 bCover ? " USING COVERING INDEX " : "",
129295 bCover ? pIdx->zName : ""
129296 );
129297 }
129298}
129299#else
129300# define explainSimpleCount(a,b,c)
129301#endif
129302
129303/*
129304** sqlite3WalkExpr() callback used by havingToWhere().
129305**
129306** If the node passed to the callback is a TK_AND node, return
129307** WRC_Continue to tell sqlite3WalkExpr() to iterate through child nodes.
129308**
129309** Otherwise, return WRC_Prune. In this case, also check if the
129310** sub-expression matches the criteria for being moved to the WHERE
129311** clause. If so, add it to the WHERE clause and replace the sub-expression
129312** within the HAVING expression with a constant "1".
129313*/
129314static int havingToWhereExprCb(Walker *pWalker, Expr *pExpr){
129315 if( pExpr->op!=TK_AND ){
129316 Select *pS = pWalker->u.pSelect;
129317 if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy) ){
129318 sqlite3 *db = pWalker->pParse->db;
129319 Expr *pNew = sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[1], 0);
129320 if( pNew ){
129321 Expr *pWhere = pS->pWhere;
129322 SWAP(Expr, *pNew, *pExpr);
129323 pNew = sqlite3ExprAnd(db, pWhere, pNew);
129324 pS->pWhere = pNew;
129325 pWalker->eCode = 1;
129326 }
129327 }
129328 return WRC_Prune;
129329 }
129330 return WRC_Continue;
129331}
129332
129333/*
129334** Transfer eligible terms from the HAVING clause of a query, which is
129335** processed after grouping, to the WHERE clause, which is processed before
129336** grouping. For example, the query:
129337**
129338** SELECT * FROM <tables> WHERE a=? GROUP BY b HAVING b=? AND c=?
129339**
129340** can be rewritten as:
129341**
129342** SELECT * FROM <tables> WHERE a=? AND b=? GROUP BY b HAVING c=?
129343**
129344** A term of the HAVING expression is eligible for transfer if it consists
129345** entirely of constants and expressions that are also GROUP BY terms that
129346** use the "BINARY" collation sequence.
129347*/
129348static void havingToWhere(Parse *pParse, Select *p){
129349 Walker sWalker;
129350 memset(&sWalker, 0, sizeof(sWalker));
129351 sWalker.pParse = pParse;
129352 sWalker.xExprCallback = havingToWhereExprCb;
129353 sWalker.u.pSelect = p;
129354 sqlite3WalkExpr(&sWalker, p->pHaving);
129355#if SELECTTRACE_ENABLED
129356 if( sWalker.eCode && (sqlite3SelectTrace & 0x100)!=0 ){
129357 SELECTTRACE(0x100,pParse,p,("Move HAVING terms into WHERE:\n"));
129358 sqlite3TreeViewSelect(0, p, 0);
129359 }
129360#endif
129361}
129362
129363/*
129364** Check to see if the pThis entry of pTabList is a self-join of a prior view.
129365** If it is, then return the SrcList_item for the prior view. If it is not,
129366** then return 0.
129367*/
129368static struct SrcList_item *isSelfJoinView(
129369 SrcList *pTabList, /* Search for self-joins in this FROM clause */
129370 struct SrcList_item *pThis /* Search for prior reference to this subquery */
129371){
129372 struct SrcList_item *pItem;
129373 for(pItem = pTabList->a; pItem<pThis; pItem++){
129374 if( pItem->pSelect==0 ) continue;
129375 if( pItem->fg.viaCoroutine ) continue;
129376 if( pItem->zName==0 ) continue;
129377 if( sqlite3_stricmp(pItem->zDatabase, pThis->zDatabase)!=0 ) continue;
129378 if( sqlite3_stricmp(pItem->zName, pThis->zName)!=0 ) continue;
129379 if( sqlite3ExprCompare(0,
129380 pThis->pSelect->pWhere, pItem->pSelect->pWhere, -1)
129381 ){
129382 /* The view was modified by some other optimization such as
129383 ** pushDownWhereTerms() */
129384 continue;
129385 }
129386 return pItem;
129387 }
129388 return 0;
129389}
129390
129391#ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION
129392/*
129393** Attempt to transform a query of the form
129394**
129395** SELECT count(*) FROM (SELECT x FROM t1 UNION ALL SELECT y FROM t2)
129396**
129397** Into this:
129398**
129399** SELECT (SELECT count(*) FROM t1)+(SELECT count(*) FROM t2)
129400**
129401** The transformation only works if all of the following are true:
129402**
129403** * The subquery is a UNION ALL of two or more terms
129404** * The subquery does not have a LIMIT clause
129405** * There is no WHERE or GROUP BY or HAVING clauses on the subqueries
129406** * The outer query is a simple count(*)
129407**
129408** Return TRUE if the optimization is undertaken.
129409*/
129410static int countOfViewOptimization(Parse *pParse, Select *p){
129411 Select *pSub, *pPrior;
129412 Expr *pExpr;
129413 Expr *pCount;
129414 sqlite3 *db;
129415 if( (p->selFlags & SF_Aggregate)==0 ) return 0; /* This is an aggregate */
129416 if( p->pEList->nExpr!=1 ) return 0; /* Single result column */
129417 pExpr = p->pEList->a[0].pExpr;
129418 if( pExpr->op!=TK_AGG_FUNCTION ) return 0; /* Result is an aggregate */
129419 if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0; /* Is count() */
129420 if( pExpr->x.pList!=0 ) return 0; /* Must be count(*) */
129421 if( p->pSrc->nSrc!=1 ) return 0; /* One table in FROM */
129422 pSub = p->pSrc->a[0].pSelect;
129423 if( pSub==0 ) return 0; /* The FROM is a subquery */
129424 if( pSub->pPrior==0 ) return 0; /* Must be a compound ry */
129425 do{
129426 if( pSub->op!=TK_ALL && pSub->pPrior ) return 0; /* Must be UNION ALL */
129427 if( pSub->pWhere ) return 0; /* No WHERE clause */
129428 if( pSub->pLimit ) return 0; /* No LIMIT clause */
129429 if( pSub->selFlags & SF_Aggregate ) return 0; /* Not an aggregate */
129430 pSub = pSub->pPrior; /* Repeat over compound */
129431 }while( pSub );
129432
129433 /* If we reach this point then it is OK to perform the transformation */
129434
129435 db = pParse->db;
129436 pCount = pExpr;
129437 pExpr = 0;
129438 pSub = p->pSrc->a[0].pSelect;
129439 p->pSrc->a[0].pSelect = 0;
129440 sqlite3SrcListDelete(db, p->pSrc);
129441 p->pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*p->pSrc));
129442 while( pSub ){
129443 Expr *pTerm;
129444 pPrior = pSub->pPrior;
129445 pSub->pPrior = 0;
129446 pSub->pNext = 0;
129447 pSub->selFlags |= SF_Aggregate;
129448 pSub->selFlags &= ~SF_Compound;
129449 pSub->nSelectRow = 0;
129450 sqlite3ExprListDelete(db, pSub->pEList);
129451 pTerm = pPrior ? sqlite3ExprDup(db, pCount, 0) : pCount;
129452 pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
129453 pTerm = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
129454 sqlite3PExprAddSelect(pParse, pTerm, pSub);
129455 if( pExpr==0 ){
129456 pExpr = pTerm;
129457 }else{
129458 pExpr = sqlite3PExpr(pParse, TK_PLUS, pTerm, pExpr);
129459 }
129460 pSub = pPrior;
129461 }
129462 p->pEList->a[0].pExpr = pExpr;
129463 p->selFlags &= ~SF_Aggregate;
129464
129465#if SELECTTRACE_ENABLED
129466 if( sqlite3SelectTrace & 0x400 ){
129467 SELECTTRACE(0x400,pParse,p,("After count-of-view optimization:\n"));
129468 sqlite3TreeViewSelect(0, p, 0);
129469 }
129470#endif
129471 return 1;
129472}
129473#endif /* SQLITE_COUNTOFVIEW_OPTIMIZATION */
129474
129475/*
129476** Generate code for the SELECT statement given in the p argument.
129477**
129478** The results are returned according to the SelectDest structure.
129479** See comments in sqliteInt.h for further information.
129480**
129481** This routine returns the number of errors. If any errors are
129482** encountered, then an appropriate error message is left in
129483** pParse->zErrMsg.
129484**
129485** This routine does NOT free the Select structure passed in. The
129486** calling function needs to do that.
129487*/
129488SQLITE_PRIVATE int sqlite3Select(
129489 Parse *pParse, /* The parser context */
129490 Select *p, /* The SELECT statement being coded. */
129491 SelectDest *pDest /* What to do with the query results */
129492){
129493 int i, j; /* Loop counters */
129494 WhereInfo *pWInfo; /* Return from sqlite3WhereBegin() */
129495 Vdbe *v; /* The virtual machine under construction */
129496 int isAgg; /* True for select lists like "count(*)" */
129497 ExprList *pEList = 0; /* List of columns to extract. */
129498 SrcList *pTabList; /* List of tables to select from */
129499 Expr *pWhere; /* The WHERE clause. May be NULL */
129500 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
129501 Expr *pHaving; /* The HAVING clause. May be NULL */
129502 int rc = 1; /* Value to return from this function */
129503 DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
129504 SortCtx sSort; /* Info on how to code the ORDER BY clause */
129505 AggInfo sAggInfo; /* Information used by aggregate queries */
129506 int iEnd; /* Address of the end of the query */
129507 sqlite3 *db; /* The database connection */
129508 ExprList *pMinMaxOrderBy = 0; /* Added ORDER BY for min/max queries */
129509 u8 minMaxFlag; /* Flag for min/max queries */
129510
129511 db = pParse->db;
129512 v = sqlite3GetVdbe(pParse);
129513 if( p==0 || db->mallocFailed || pParse->nErr ){
129514 return 1;
129515 }
129516 if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
129517 memset(&sAggInfo, 0, sizeof(sAggInfo));
129518#if SELECTTRACE_ENABLED
129519 SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain));
129520 if( sqlite3SelectTrace & 0x100 ){
129521 sqlite3TreeViewSelect(0, p, 0);
129522 }
129523#endif
129524
129525 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
129526 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
129527 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
129528 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
129529 if( IgnorableOrderby(pDest) ){
129530 assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
129531 pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
129532 pDest->eDest==SRT_Queue || pDest->eDest==SRT_DistFifo ||
129533 pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_Fifo);
129534 /* If ORDER BY makes no difference in the output then neither does
129535 ** DISTINCT so it can be removed too. */
129536 sqlite3ExprListDelete(db, p->pOrderBy);
129537 p->pOrderBy = 0;
129538 p->selFlags &= ~SF_Distinct;
129539 }
129540 sqlite3SelectPrep(pParse, p, 0);
129541 if( pParse->nErr || db->mallocFailed ){
129542 goto select_end;
129543 }
129544 assert( p->pEList!=0 );
129545#if SELECTTRACE_ENABLED
129546 if( sqlite3SelectTrace & 0x104 ){
129547 SELECTTRACE(0x104,pParse,p, ("after name resolution:\n"));
129548 sqlite3TreeViewSelect(0, p, 0);
129549 }
129550#endif
129551
129552 if( pDest->eDest==SRT_Output ){
129553 generateColumnNames(pParse, p);
129554 }
129555
129556#ifndef SQLITE_OMIT_WINDOWFUNC
129557 if( sqlite3WindowRewrite(pParse, p) ){
129558 goto select_end;
129559 }
129560#if SELECTTRACE_ENABLED
129561 if( sqlite3SelectTrace & 0x108 ){
129562 SELECTTRACE(0x104,pParse,p, ("after window rewrite:\n"));
129563 sqlite3TreeViewSelect(0, p, 0);
129564 }
129565#endif
129566#endif /* SQLITE_OMIT_WINDOWFUNC */
129567 pTabList = p->pSrc;
129568 isAgg = (p->selFlags & SF_Aggregate)!=0;
129569 memset(&sSort, 0, sizeof(sSort));
129570 sSort.pOrderBy = p->pOrderBy;
129571
129572 /* Try to various optimizations (flattening subqueries, and strength
129573 ** reduction of join operators) in the FROM clause up into the main query
129574 */
129575#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
129576 for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
129577 struct SrcList_item *pItem = &pTabList->a[i];
129578 Select *pSub = pItem->pSelect;
129579 Table *pTab = pItem->pTab;
129580
129581 /* Convert LEFT JOIN into JOIN if there are terms of the right table
129582 ** of the LEFT JOIN used in the WHERE clause.
129583 */
129584 if( (pItem->fg.jointype & JT_LEFT)!=0
129585 && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor)
129586 && OptimizationEnabled(db, SQLITE_SimplifyJoin)
129587 ){
129588 SELECTTRACE(0x100,pParse,p,
129589 ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
129590 pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
129591 unsetJoinExpr(p->pWhere, pItem->iCursor);
129592 }
129593
129594 /* No futher action if this term of the FROM clause is no a subquery */
129595 if( pSub==0 ) continue;
129596
129597 /* Catch mismatch in the declared columns of a view and the number of
129598 ** columns in the SELECT on the RHS */
129599 if( pTab->nCol!=pSub->pEList->nExpr ){
129600 sqlite3ErrorMsg(pParse, "expected %d columns for '%s' but got %d",
129601 pTab->nCol, pTab->zName, pSub->pEList->nExpr);
129602 goto select_end;
129603 }
129604
129605 /* Do not try to flatten an aggregate subquery.
129606 **
129607 ** Flattening an aggregate subquery is only possible if the outer query
129608 ** is not a join. But if the outer query is not a join, then the subquery
129609 ** will be implemented as a co-routine and there is no advantage to
129610 ** flattening in that case.
129611 */
129612 if( (pSub->selFlags & SF_Aggregate)!=0 ) continue;
129613 assert( pSub->pGroupBy==0 );
129614
129615 /* If the outer query contains a "complex" result set (that is,
129616 ** if the result set of the outer query uses functions or subqueries)
129617 ** and if the subquery contains an ORDER BY clause and if
129618 ** it will be implemented as a co-routine, then do not flatten. This
129619 ** restriction allows SQL constructs like this:
129620 **
129621 ** SELECT expensive_function(x)
129622 ** FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
129623 **
129624 ** The expensive_function() is only computed on the 10 rows that
129625 ** are output, rather than every row of the table.
129626 **
129627 ** The requirement that the outer query have a complex result set
129628 ** means that flattening does occur on simpler SQL constraints without
129629 ** the expensive_function() like:
129630 **
129631 ** SELECT x FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
129632 */
129633 if( pSub->pOrderBy!=0
129634 && i==0
129635 && (p->selFlags & SF_ComplexResult)!=0
129636 && (pTabList->nSrc==1
129637 || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0)
129638 ){
129639 continue;
129640 }
129641
129642 if( flattenSubquery(pParse, p, i, isAgg) ){
129643 /* This subquery can be absorbed into its parent. */
129644 i = -1;
129645 }
129646 pTabList = p->pSrc;
129647 if( db->mallocFailed ) goto select_end;
129648 if( !IgnorableOrderby(pDest) ){
129649 sSort.pOrderBy = p->pOrderBy;
129650 }
129651 }
129652#endif
129653
129654#ifndef SQLITE_OMIT_COMPOUND_SELECT
129655 /* Handle compound SELECT statements using the separate multiSelect()
129656 ** procedure.
129657 */
129658 if( p->pPrior ){
129659 rc = multiSelect(pParse, p, pDest);
129660#if SELECTTRACE_ENABLED
129661 SELECTTRACE(0x1,pParse,p,("end compound-select processing\n"));
129662 if( (sqlite3SelectTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
129663 sqlite3TreeViewSelect(0, p, 0);
129664 }
129665#endif
129666 if( p->pNext==0 ) ExplainQueryPlanPop(pParse);
129667 return rc;
129668 }
129669#endif
129670
129671 /* Do the WHERE-clause constant propagation optimization if this is
129672 ** a join. No need to speed time on this operation for non-join queries
129673 ** as the equivalent optimization will be handled by query planner in
129674 ** sqlite3WhereBegin().
129675 */
129676 if( pTabList->nSrc>1
129677 && OptimizationEnabled(db, SQLITE_PropagateConst)
129678 && propagateConstants(pParse, p)
129679 ){
129680#if SELECTTRACE_ENABLED
129681 if( sqlite3SelectTrace & 0x100 ){
129682 SELECTTRACE(0x100,pParse,p,("After constant propagation:\n"));
129683 sqlite3TreeViewSelect(0, p, 0);
129684 }
129685#endif
129686 }else{
129687 SELECTTRACE(0x100,pParse,p,("Constant propagation not helpful\n"));
129688 }
129689
129690#ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION
129691 if( OptimizationEnabled(db, SQLITE_QueryFlattener|SQLITE_CountOfView)
129692 && countOfViewOptimization(pParse, p)
129693 ){
129694 if( db->mallocFailed ) goto select_end;
129695 pEList = p->pEList;
129696 pTabList = p->pSrc;
129697 }
129698#endif
129699
129700 /* For each term in the FROM clause, do two things:
129701 ** (1) Authorized unreferenced tables
129702 ** (2) Generate code for all sub-queries
129703 */
129704 for(i=0; i<pTabList->nSrc; i++){
129705 struct SrcList_item *pItem = &pTabList->a[i];
129706 SelectDest dest;
129707 Select *pSub;
129708#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
129709 const char *zSavedAuthContext;
129710#endif
129711
129712 /* Issue SQLITE_READ authorizations with a fake column name for any
129713 ** tables that are referenced but from which no values are extracted.
129714 ** Examples of where these kinds of null SQLITE_READ authorizations
129715 ** would occur:
129716 **
129717 ** SELECT count(*) FROM t1; -- SQLITE_READ t1.""
129718 ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2.""
129719 **
129720 ** The fake column name is an empty string. It is possible for a table to
129721 ** have a column named by the empty string, in which case there is no way to
129722 ** distinguish between an unreferenced table and an actual reference to the
129723 ** "" column. The original design was for the fake column name to be a NULL,
129724 ** which would be unambiguous. But legacy authorization callbacks might
129725 ** assume the column name is non-NULL and segfault. The use of an empty
129726 ** string for the fake column name seems safer.
129727 */
129728 if( pItem->colUsed==0 ){
129729 sqlite3AuthCheck(pParse, SQLITE_READ, pItem->zName, "", pItem->zDatabase);
129730 }
129731
129732#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
129733 /* Generate code for all sub-queries in the FROM clause
129734 */
129735 pSub = pItem->pSelect;
129736 if( pSub==0 ) continue;
129737
129738 /* Sometimes the code for a subquery will be generated more than
129739 ** once, if the subquery is part of the WHERE clause in a LEFT JOIN,
129740 ** for example. In that case, do not regenerate the code to manifest
129741 ** a view or the co-routine to implement a view. The first instance
129742 ** is sufficient, though the subroutine to manifest the view does need
129743 ** to be invoked again. */
129744 if( pItem->addrFillSub ){
129745 if( pItem->fg.viaCoroutine==0 ){
129746 /* The subroutine that manifests the view might be a one-time routine,
129747 ** or it might need to be rerun on each iteration because it
129748 ** encodes a correlated subquery. */
129749 testcase( sqlite3VdbeGetOp(v, pItem->addrFillSub)->opcode==OP_Once );
129750 sqlite3VdbeAddOp2(v, OP_Gosub, pItem->regReturn, pItem->addrFillSub);
129751 }
129752 continue;
129753 }
129754
129755 /* Increment Parse.nHeight by the height of the largest expression
129756 ** tree referred to by this, the parent select. The child select
129757 ** may contain expression trees of at most
129758 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
129759 ** more conservative than necessary, but much easier than enforcing
129760 ** an exact limit.
129761 */
129762 pParse->nHeight += sqlite3SelectExprHeight(p);
129763
129764 /* Make copies of constant WHERE-clause terms in the outer query down
129765 ** inside the subquery. This can help the subquery to run more efficiently.
129766 */
129767 if( OptimizationEnabled(db, SQLITE_PushDown)
129768 && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem->iCursor,
129769 (pItem->fg.jointype & JT_OUTER)!=0)
129770 ){
129771#if SELECTTRACE_ENABLED
129772 if( sqlite3SelectTrace & 0x100 ){
129773 SELECTTRACE(0x100,pParse,p,
129774 ("After WHERE-clause push-down into subquery %d:\n", pSub->selId));
129775 sqlite3TreeViewSelect(0, p, 0);
129776 }
129777#endif
129778 }else{
129779 SELECTTRACE(0x100,pParse,p,("Push-down not possible\n"));
129780 }
129781
129782 zSavedAuthContext = pParse->zAuthContext;
129783 pParse->zAuthContext = pItem->zName;
129784
129785 /* Generate code to implement the subquery
129786 **
129787 ** The subquery is implemented as a co-routine if the subquery is
129788 ** guaranteed to be the outer loop (so that it does not need to be
129789 ** computed more than once)
129790 **
129791 ** TODO: Are there other reasons beside (1) to use a co-routine
129792 ** implementation?
129793 */
129794 if( i==0
129795 && (pTabList->nSrc==1
129796 || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0) /* (1) */
129797 ){
129798 /* Implement a co-routine that will return a single row of the result
129799 ** set on each invocation.
129800 */
129801 int addrTop = sqlite3VdbeCurrentAddr(v)+1;
129802
129803 pItem->regReturn = ++pParse->nMem;
129804 sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
129805 VdbeComment((v, "%s", pItem->pTab->zName));
129806 pItem->addrFillSub = addrTop;
129807 sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
129808 ExplainQueryPlan((pParse, 1, "CO-ROUTINE %u", pSub->selId));
129809 sqlite3Select(pParse, pSub, &dest);
129810 pItem->pTab->nRowLogEst = pSub->nSelectRow;
129811 pItem->fg.viaCoroutine = 1;
129812 pItem->regResult = dest.iSdst;
129813 sqlite3VdbeEndCoroutine(v, pItem->regReturn);
129814 sqlite3VdbeJumpHere(v, addrTop-1);
129815 sqlite3ClearTempRegCache(pParse);
129816 }else{
129817 /* Generate a subroutine that will fill an ephemeral table with
129818 ** the content of this subquery. pItem->addrFillSub will point
129819 ** to the address of the generated subroutine. pItem->regReturn
129820 ** is a register allocated to hold the subroutine return address
129821 */
129822 int topAddr;
129823 int onceAddr = 0;
129824 int retAddr;
129825 struct SrcList_item *pPrior;
129826
129827 assert( pItem->addrFillSub==0 );
129828 pItem->regReturn = ++pParse->nMem;
129829 topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
129830 pItem->addrFillSub = topAddr+1;
129831 if( pItem->fg.isCorrelated==0 ){
129832 /* If the subquery is not correlated and if we are not inside of
129833 ** a trigger, then we only need to compute the value of the subquery
129834 ** once. */
129835 onceAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
129836 VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName));
129837 }else{
129838 VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
129839 }
129840 pPrior = isSelfJoinView(pTabList, pItem);
129841 if( pPrior ){
129842 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor);
129843 assert( pPrior->pSelect!=0 );
129844 pSub->nSelectRow = pPrior->pSelect->nSelectRow;
129845 }else{
129846 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
129847 ExplainQueryPlan((pParse, 1, "MATERIALIZE %u", pSub->selId));
129848 sqlite3Select(pParse, pSub, &dest);
129849 }
129850 pItem->pTab->nRowLogEst = pSub->nSelectRow;
129851 if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
129852 retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
129853 VdbeComment((v, "end %s", pItem->pTab->zName));
129854 sqlite3VdbeChangeP1(v, topAddr, retAddr);
129855 sqlite3ClearTempRegCache(pParse);
129856 }
129857 if( db->mallocFailed ) goto select_end;
129858 pParse->nHeight -= sqlite3SelectExprHeight(p);
129859 pParse->zAuthContext = zSavedAuthContext;
129860#endif
129861 }
129862
129863 /* Various elements of the SELECT copied into local variables for
129864 ** convenience */
129865 pEList = p->pEList;
129866 pWhere = p->pWhere;
129867 pGroupBy = p->pGroupBy;
129868 pHaving = p->pHaving;
129869 sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
129870
129871#if SELECTTRACE_ENABLED
129872 if( sqlite3SelectTrace & 0x400 ){
129873 SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
129874 sqlite3TreeViewSelect(0, p, 0);
129875 }
129876#endif
129877
129878 /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
129879 ** if the select-list is the same as the ORDER BY list, then this query
129880 ** can be rewritten as a GROUP BY. In other words, this:
129881 **
129882 ** SELECT DISTINCT xyz FROM ... ORDER BY xyz
129883 **
129884 ** is transformed to:
129885 **
129886 ** SELECT xyz FROM ... GROUP BY xyz ORDER BY xyz
129887 **
129888 ** The second form is preferred as a single index (or temp-table) may be
129889 ** used for both the ORDER BY and DISTINCT processing. As originally
129890 ** written the query must use a temp-table for at least one of the ORDER
129891 ** BY and DISTINCT, and an index or separate temp-table for the other.
129892 */
129893 if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
129894 && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
129895 ){
129896 p->selFlags &= ~SF_Distinct;
129897 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
129898 /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
129899 ** the sDistinct.isTnct is still set. Hence, isTnct represents the
129900 ** original setting of the SF_Distinct flag, not the current setting */
129901 assert( sDistinct.isTnct );
129902
129903#if SELECTTRACE_ENABLED
129904 if( sqlite3SelectTrace & 0x400 ){
129905 SELECTTRACE(0x400,pParse,p,("Transform DISTINCT into GROUP BY:\n"));
129906 sqlite3TreeViewSelect(0, p, 0);
129907 }
129908#endif
129909 }
129910
129911 /* If there is an ORDER BY clause, then create an ephemeral index to
129912 ** do the sorting. But this sorting ephemeral index might end up
129913 ** being unused if the data can be extracted in pre-sorted order.
129914 ** If that is the case, then the OP_OpenEphemeral instruction will be
129915 ** changed to an OP_Noop once we figure out that the sorting index is
129916 ** not needed. The sSort.addrSortIndex variable is used to facilitate
129917 ** that change.
129918 */
129919 if( sSort.pOrderBy ){
129920 KeyInfo *pKeyInfo;
129921 pKeyInfo = sqlite3KeyInfoFromExprList(
129922 pParse, sSort.pOrderBy, 0, pEList->nExpr);
129923 sSort.iECursor = pParse->nTab++;
129924 sSort.addrSortIndex =
129925 sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
129926 sSort.iECursor, sSort.pOrderBy->nExpr+1+pEList->nExpr, 0,
129927 (char*)pKeyInfo, P4_KEYINFO
129928 );
129929 }else{
129930 sSort.addrSortIndex = -1;
129931 }
129932
129933 /* If the output is destined for a temporary table, open that table.
129934 */
129935 if( pDest->eDest==SRT_EphemTab ){
129936 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
129937 }
129938
129939 /* Set the limiter.
129940 */
129941 iEnd = sqlite3VdbeMakeLabel(v);
129942 if( (p->selFlags & SF_FixedLimit)==0 ){
129943 p->nSelectRow = 320; /* 4 billion rows */
129944 }
129945 computeLimitRegisters(pParse, p, iEnd);
129946 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
129947 sqlite3VdbeChangeOpcode(v, sSort.addrSortIndex, OP_SorterOpen);
129948 sSort.sortFlags |= SORTFLAG_UseSorter;
129949 }
129950
129951 /* Open an ephemeral index to use for the distinct set.
129952 */
129953 if( p->selFlags & SF_Distinct ){
129954 sDistinct.tabTnct = pParse->nTab++;
129955 sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
129956 sDistinct.tabTnct, 0, 0,
129957 (char*)sqlite3KeyInfoFromExprList(pParse, p->pEList,0,0),
129958 P4_KEYINFO);
129959 sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
129960 sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
129961 }else{
129962 sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
129963 }
129964
129965 if( !isAgg && pGroupBy==0 ){
129966 /* No aggregate functions and no GROUP BY clause */
129967 u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0)
129968 | (p->selFlags & SF_FixedLimit);
129969#ifndef SQLITE_OMIT_WINDOWFUNC
129970 Window *pWin = p->pWin; /* Master window object (or NULL) */
129971 if( pWin ){
129972 sqlite3WindowCodeInit(pParse, pWin);
129973 }
129974#endif
129975 assert( WHERE_USE_LIMIT==SF_FixedLimit );
129976
129977
129978 /* Begin the database scan. */
129979 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
129980 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy,
129981 p->pEList, wctrlFlags, p->nSelectRow);
129982 if( pWInfo==0 ) goto select_end;
129983 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
129984 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
129985 }
129986 if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){
129987 sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo);
129988 }
129989 if( sSort.pOrderBy ){
129990 sSort.nOBSat = sqlite3WhereIsOrdered(pWInfo);
129991 sSort.labelOBLopt = sqlite3WhereOrderByLimitOptLabel(pWInfo);
129992 if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
129993 sSort.pOrderBy = 0;
129994 }
129995 }
129996
129997 /* If sorting index that was created by a prior OP_OpenEphemeral
129998 ** instruction ended up not being needed, then change the OP_OpenEphemeral
129999 ** into an OP_Noop.
130000 */
130001 if( sSort.addrSortIndex>=0 && sSort.pOrderBy==0 ){
130002 sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
130003 }
130004
130005 assert( p->pEList==pEList );
130006#ifndef SQLITE_OMIT_WINDOWFUNC
130007 if( pWin ){
130008 int addrGosub = sqlite3VdbeMakeLabel(v);
130009 int iCont = sqlite3VdbeMakeLabel(v);
130010 int iBreak = sqlite3VdbeMakeLabel(v);
130011 int regGosub = ++pParse->nMem;
130012
130013 sqlite3WindowCodeStep(pParse, p, pWInfo, regGosub, addrGosub);
130014
130015 sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak);
130016 sqlite3VdbeResolveLabel(v, addrGosub);
130017 VdbeNoopComment((v, "inner-loop subroutine"));
130018 sSort.labelOBLopt = 0;
130019 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest, iCont, iBreak);
130020 sqlite3VdbeResolveLabel(v, iCont);
130021 sqlite3VdbeAddOp1(v, OP_Return, regGosub);
130022 VdbeComment((v, "end inner-loop subroutine"));
130023 sqlite3VdbeResolveLabel(v, iBreak);
130024 }else
130025#endif /* SQLITE_OMIT_WINDOWFUNC */
130026 {
130027 /* Use the standard inner loop. */
130028 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest,
130029 sqlite3WhereContinueLabel(pWInfo),
130030 sqlite3WhereBreakLabel(pWInfo));
130031
130032 /* End the database scan loop.
130033 */
130034 sqlite3WhereEnd(pWInfo);
130035 }
130036 }else{
130037 /* This case when there exist aggregate functions or a GROUP BY clause
130038 ** or both */
130039 NameContext sNC; /* Name context for processing aggregate information */
130040 int iAMem; /* First Mem address for storing current GROUP BY */
130041 int iBMem; /* First Mem address for previous GROUP BY */
130042 int iUseFlag; /* Mem address holding flag indicating that at least
130043 ** one row of the input to the aggregator has been
130044 ** processed */
130045 int iAbortFlag; /* Mem address which causes query abort if positive */
130046 int groupBySort; /* Rows come from source in GROUP BY order */
130047 int addrEnd; /* End of processing for this SELECT */
130048 int sortPTab = 0; /* Pseudotable used to decode sorting results */
130049 int sortOut = 0; /* Output register from the sorter */
130050 int orderByGrp = 0; /* True if the GROUP BY and ORDER BY are the same */
130051
130052 /* Remove any and all aliases between the result set and the
130053 ** GROUP BY clause.
130054 */
130055 if( pGroupBy ){
130056 int k; /* Loop counter */
130057 struct ExprList_item *pItem; /* For looping over expression in a list */
130058
130059 for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
130060 pItem->u.x.iAlias = 0;
130061 }
130062 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
130063 pItem->u.x.iAlias = 0;
130064 }
130065 assert( 66==sqlite3LogEst(100) );
130066 if( p->nSelectRow>66 ) p->nSelectRow = 66;
130067 }else{
130068 assert( 0==sqlite3LogEst(1) );
130069 p->nSelectRow = 0;
130070 }
130071
130072 /* If there is both a GROUP BY and an ORDER BY clause and they are
130073 ** identical, then it may be possible to disable the ORDER BY clause
130074 ** on the grounds that the GROUP BY will cause elements to come out
130075 ** in the correct order. It also may not - the GROUP BY might use a
130076 ** database index that causes rows to be grouped together as required
130077 ** but not actually sorted. Either way, record the fact that the
130078 ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
130079 ** variable. */
130080 if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
130081 orderByGrp = 1;
130082 }
130083
130084 /* Create a label to jump to when we want to abort the query */
130085 addrEnd = sqlite3VdbeMakeLabel(v);
130086
130087 /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
130088 ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
130089 ** SELECT statement.
130090 */
130091 memset(&sNC, 0, sizeof(sNC));
130092 sNC.pParse = pParse;
130093 sNC.pSrcList = pTabList;
130094 sNC.uNC.pAggInfo = &sAggInfo;
130095 VVA_ONLY( sNC.ncFlags = NC_UAggInfo; )
130096 sAggInfo.mnReg = pParse->nMem+1;
130097 sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
130098 sAggInfo.pGroupBy = pGroupBy;
130099 sqlite3ExprAnalyzeAggList(&sNC, pEList);
130100 sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy);
130101 if( pHaving ){
130102 if( pGroupBy ){
130103 assert( pWhere==p->pWhere );
130104 assert( pHaving==p->pHaving );
130105 assert( pGroupBy==p->pGroupBy );
130106 havingToWhere(pParse, p);
130107 pWhere = p->pWhere;
130108 }
130109 sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
130110 }
130111 sAggInfo.nAccumulator = sAggInfo.nColumn;
130112 if( p->pGroupBy==0 && p->pHaving==0 && sAggInfo.nFunc==1 ){
130113 minMaxFlag = minMaxQuery(db, sAggInfo.aFunc[0].pExpr, &pMinMaxOrderBy);
130114 }else{
130115 minMaxFlag = WHERE_ORDERBY_NORMAL;
130116 }
130117 for(i=0; i<sAggInfo.nFunc; i++){
130118 assert( !ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_xIsSelect) );
130119 sNC.ncFlags |= NC_InAggFunc;
130120 sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
130121 sNC.ncFlags &= ~NC_InAggFunc;
130122 }
130123 sAggInfo.mxReg = pParse->nMem;
130124 if( db->mallocFailed ) goto select_end;
130125#if SELECTTRACE_ENABLED
130126 if( sqlite3SelectTrace & 0x400 ){
130127 int ii;
130128 SELECTTRACE(0x400,pParse,p,("After aggregate analysis:\n"));
130129 sqlite3TreeViewSelect(0, p, 0);
130130 for(ii=0; ii<sAggInfo.nColumn; ii++){
130131 sqlite3DebugPrintf("agg-column[%d] iMem=%d\n",
130132 ii, sAggInfo.aCol[ii].iMem);
130133 sqlite3TreeViewExpr(0, sAggInfo.aCol[ii].pExpr, 0);
130134 }
130135 for(ii=0; ii<sAggInfo.nFunc; ii++){
130136 sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
130137 ii, sAggInfo.aFunc[ii].iMem);
130138 sqlite3TreeViewExpr(0, sAggInfo.aFunc[ii].pExpr, 0);
130139 }
130140 }
130141#endif
130142
130143
130144 /* Processing for aggregates with GROUP BY is very different and
130145 ** much more complex than aggregates without a GROUP BY.
130146 */
130147 if( pGroupBy ){
130148 KeyInfo *pKeyInfo; /* Keying information for the group by clause */
130149 int addr1; /* A-vs-B comparision jump */
130150 int addrOutputRow; /* Start of subroutine that outputs a result row */
130151 int regOutputRow; /* Return address register for output subroutine */
130152 int addrSetAbort; /* Set the abort flag and return */
130153 int addrTopOfLoop; /* Top of the input loop */
130154 int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
130155 int addrReset; /* Subroutine for resetting the accumulator */
130156 int regReset; /* Return address register for reset subroutine */
130157
130158 /* If there is a GROUP BY clause we might need a sorting index to
130159 ** implement it. Allocate that sorting index now. If it turns out
130160 ** that we do not need it after all, the OP_SorterOpen instruction
130161 ** will be converted into a Noop.
130162 */
130163 sAggInfo.sortingIdx = pParse->nTab++;
130164 pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pGroupBy,0,sAggInfo.nColumn);
130165 addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen,
130166 sAggInfo.sortingIdx, sAggInfo.nSortingColumn,
130167 0, (char*)pKeyInfo, P4_KEYINFO);
130168
130169 /* Initialize memory locations used by GROUP BY aggregate processing
130170 */
130171 iUseFlag = ++pParse->nMem;
130172 iAbortFlag = ++pParse->nMem;
130173 regOutputRow = ++pParse->nMem;
130174 addrOutputRow = sqlite3VdbeMakeLabel(v);
130175 regReset = ++pParse->nMem;
130176 addrReset = sqlite3VdbeMakeLabel(v);
130177 iAMem = pParse->nMem + 1;
130178 pParse->nMem += pGroupBy->nExpr;
130179 iBMem = pParse->nMem + 1;
130180 pParse->nMem += pGroupBy->nExpr;
130181 sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag);
130182 VdbeComment((v, "clear abort flag"));
130183 sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
130184
130185 /* Begin a loop that will extract all source rows in GROUP BY order.
130186 ** This might involve two separate loops with an OP_Sort in between, or
130187 ** it might be a single loop that uses an index to extract information
130188 ** in the right order to begin with.
130189 */
130190 sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
130191 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
130192 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0,
130193 WHERE_GROUPBY | (orderByGrp ? WHERE_SORTBYGROUP : 0), 0
130194 );
130195 if( pWInfo==0 ) goto select_end;
130196 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
130197 /* The optimizer is able to deliver rows in group by order so
130198 ** we do not have to sort. The OP_OpenEphemeral table will be
130199 ** cancelled later because we still need to use the pKeyInfo
130200 */
130201 groupBySort = 0;
130202 }else{
130203 /* Rows are coming out in undetermined order. We have to push
130204 ** each row into a sorting index, terminate the first loop,
130205 ** then loop over the sorting index in order to get the output
130206 ** in sorted order
130207 */
130208 int regBase;
130209 int regRecord;
130210 int nCol;
130211 int nGroupBy;
130212
130213 explainTempTable(pParse,
130214 (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
130215 "DISTINCT" : "GROUP BY");
130216
130217 groupBySort = 1;
130218 nGroupBy = pGroupBy->nExpr;
130219 nCol = nGroupBy;
130220 j = nGroupBy;
130221 for(i=0; i<sAggInfo.nColumn; i++){
130222 if( sAggInfo.aCol[i].iSorterColumn>=j ){
130223 nCol++;
130224 j++;
130225 }
130226 }
130227 regBase = sqlite3GetTempRange(pParse, nCol);
130228 sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0, 0);
130229 j = nGroupBy;
130230 for(i=0; i<sAggInfo.nColumn; i++){
130231 struct AggInfo_col *pCol = &sAggInfo.aCol[i];
130232 if( pCol->iSorterColumn>=j ){
130233 int r1 = j + regBase;
130234 sqlite3ExprCodeGetColumnOfTable(v,
130235 pCol->pTab, pCol->iTable, pCol->iColumn, r1);
130236 j++;
130237 }
130238 }
130239 regRecord = sqlite3GetTempReg(pParse);
130240 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
130241 sqlite3VdbeAddOp2(v, OP_SorterInsert, sAggInfo.sortingIdx, regRecord);
130242 sqlite3ReleaseTempReg(pParse, regRecord);
130243 sqlite3ReleaseTempRange(pParse, regBase, nCol);
130244 sqlite3WhereEnd(pWInfo);
130245 sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
130246 sortOut = sqlite3GetTempReg(pParse);
130247 sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
130248 sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
130249 VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
130250 sAggInfo.useSortingIdx = 1;
130251 }
130252
130253 /* If the index or temporary table used by the GROUP BY sort
130254 ** will naturally deliver rows in the order required by the ORDER BY
130255 ** clause, cancel the ephemeral table open coded earlier.
130256 **
130257 ** This is an optimization - the correct answer should result regardless.
130258 ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER to
130259 ** disable this optimization for testing purposes. */
130260 if( orderByGrp && OptimizationEnabled(db, SQLITE_GroupByOrder)
130261 && (groupBySort || sqlite3WhereIsSorted(pWInfo))
130262 ){
130263 sSort.pOrderBy = 0;
130264 sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
130265 }
130266
130267 /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
130268 ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
130269 ** Then compare the current GROUP BY terms against the GROUP BY terms
130270 ** from the previous row currently stored in a0, a1, a2...
130271 */
130272 addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
130273 if( groupBySort ){
130274 sqlite3VdbeAddOp3(v, OP_SorterData, sAggInfo.sortingIdx,
130275 sortOut, sortPTab);
130276 }
130277 for(j=0; j<pGroupBy->nExpr; j++){
130278 if( groupBySort ){
130279 sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
130280 }else{
130281 sAggInfo.directMode = 1;
130282 sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
130283 }
130284 }
130285 sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
130286 (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
130287 addr1 = sqlite3VdbeCurrentAddr(v);
130288 sqlite3VdbeAddOp3(v, OP_Jump, addr1+1, 0, addr1+1); VdbeCoverage(v);
130289
130290 /* Generate code that runs whenever the GROUP BY changes.
130291 ** Changes in the GROUP BY are detected by the previous code
130292 ** block. If there were no changes, this block is skipped.
130293 **
130294 ** This code copies current group by terms in b0,b1,b2,...
130295 ** over to a0,a1,a2. It then calls the output subroutine
130296 ** and resets the aggregate accumulator registers in preparation
130297 ** for the next GROUP BY batch.
130298 */
130299 sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
130300 sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
130301 VdbeComment((v, "output one row"));
130302 sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd); VdbeCoverage(v);
130303 VdbeComment((v, "check abort flag"));
130304 sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
130305 VdbeComment((v, "reset accumulator"));
130306
130307 /* Update the aggregate accumulators based on the content of
130308 ** the current row
130309 */
130310 sqlite3VdbeJumpHere(v, addr1);
130311 updateAccumulator(pParse, iUseFlag, &sAggInfo);
130312 sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag);
130313 VdbeComment((v, "indicate data in accumulator"));
130314
130315 /* End of the loop
130316 */
130317 if( groupBySort ){
130318 sqlite3VdbeAddOp2(v, OP_SorterNext, sAggInfo.sortingIdx, addrTopOfLoop);
130319 VdbeCoverage(v);
130320 }else{
130321 sqlite3WhereEnd(pWInfo);
130322 sqlite3VdbeChangeToNoop(v, addrSortingIdx);
130323 }
130324
130325 /* Output the final row of result
130326 */
130327 sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
130328 VdbeComment((v, "output final row"));
130329
130330 /* Jump over the subroutines
130331 */
130332 sqlite3VdbeGoto(v, addrEnd);
130333
130334 /* Generate a subroutine that outputs a single row of the result
130335 ** set. This subroutine first looks at the iUseFlag. If iUseFlag
130336 ** is less than or equal to zero, the subroutine is a no-op. If
130337 ** the processing calls for the query to abort, this subroutine
130338 ** increments the iAbortFlag memory location before returning in
130339 ** order to signal the caller to abort.
130340 */
130341 addrSetAbort = sqlite3VdbeCurrentAddr(v);
130342 sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
130343 VdbeComment((v, "set abort flag"));
130344 sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
130345 sqlite3VdbeResolveLabel(v, addrOutputRow);
130346 addrOutputRow = sqlite3VdbeCurrentAddr(v);
130347 sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
130348 VdbeCoverage(v);
130349 VdbeComment((v, "Groupby result generator entry point"));
130350 sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
130351 finalizeAggFunctions(pParse, &sAggInfo);
130352 sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
130353 selectInnerLoop(pParse, p, -1, &sSort,
130354 &sDistinct, pDest,
130355 addrOutputRow+1, addrSetAbort);
130356 sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
130357 VdbeComment((v, "end groupby result generator"));
130358
130359 /* Generate a subroutine that will reset the group-by accumulator
130360 */
130361 sqlite3VdbeResolveLabel(v, addrReset);
130362 resetAccumulator(pParse, &sAggInfo);
130363 sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag);
130364 VdbeComment((v, "indicate accumulator empty"));
130365 sqlite3VdbeAddOp1(v, OP_Return, regReset);
130366
130367 } /* endif pGroupBy. Begin aggregate queries without GROUP BY: */
130368 else {
130369#ifndef SQLITE_OMIT_BTREECOUNT
130370 Table *pTab;
130371 if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){
130372 /* If isSimpleCount() returns a pointer to a Table structure, then
130373 ** the SQL statement is of the form:
130374 **
130375 ** SELECT count(*) FROM <tbl>
130376 **
130377 ** where the Table structure returned represents table <tbl>.
130378 **
130379 ** This statement is so common that it is optimized specially. The
130380 ** OP_Count instruction is executed either on the intkey table that
130381 ** contains the data for table <tbl> or on one of its indexes. It
130382 ** is better to execute the op on an index, as indexes are almost
130383 ** always spread across less pages than their corresponding tables.
130384 */
130385 const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
130386 const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */
130387 Index *pIdx; /* Iterator variable */
130388 KeyInfo *pKeyInfo = 0; /* Keyinfo for scanned index */
130389 Index *pBest = 0; /* Best index found so far */
130390 int iRoot = pTab->tnum; /* Root page of scanned b-tree */
130391
130392 sqlite3CodeVerifySchema(pParse, iDb);
130393 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
130394
130395 /* Search for the index that has the lowest scan cost.
130396 **
130397 ** (2011-04-15) Do not do a full scan of an unordered index.
130398 **
130399 ** (2013-10-03) Do not count the entries in a partial index.
130400 **
130401 ** In practice the KeyInfo structure will not be used. It is only
130402 ** passed to keep OP_OpenRead happy.
130403 */
130404 if( !HasRowid(pTab) ) pBest = sqlite3PrimaryKeyIndex(pTab);
130405 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
130406 if( pIdx->bUnordered==0
130407 && pIdx->szIdxRow<pTab->szTabRow
130408 && pIdx->pPartIdxWhere==0
130409 && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
130410 ){
130411 pBest = pIdx;
130412 }
130413 }
130414 if( pBest ){
130415 iRoot = pBest->tnum;
130416 pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pBest);
130417 }
130418
130419 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
130420 sqlite3VdbeAddOp4Int(v, OP_OpenRead, iCsr, iRoot, iDb, 1);
130421 if( pKeyInfo ){
130422 sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
130423 }
130424 sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem);
130425 sqlite3VdbeAddOp1(v, OP_Close, iCsr);
130426 explainSimpleCount(pParse, pTab, pBest);
130427 }else
130428#endif /* SQLITE_OMIT_BTREECOUNT */
130429 {
130430 int regAcc = 0; /* "populate accumulators" flag */
130431
130432 /* If there are accumulator registers but no min() or max() functions,
130433 ** allocate register regAcc. Register regAcc will contain 0 the first
130434 ** time the inner loop runs, and 1 thereafter. The code generated
130435 ** by updateAccumulator() only updates the accumulator registers if
130436 ** regAcc contains 0. */
130437 if( sAggInfo.nAccumulator ){
130438 for(i=0; i<sAggInfo.nFunc; i++){
130439 if( sAggInfo.aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL ) break;
130440 }
130441 if( i==sAggInfo.nFunc ){
130442 regAcc = ++pParse->nMem;
130443 sqlite3VdbeAddOp2(v, OP_Integer, 0, regAcc);
130444 }
130445 }
130446
130447 /* This case runs if the aggregate has no GROUP BY clause. The
130448 ** processing is much simpler since there is only a single row
130449 ** of output.
130450 */
130451 assert( p->pGroupBy==0 );
130452 resetAccumulator(pParse, &sAggInfo);
130453
130454 /* If this query is a candidate for the min/max optimization, then
130455 ** minMaxFlag will have been previously set to either
130456 ** WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX and pMinMaxOrderBy will
130457 ** be an appropriate ORDER BY expression for the optimization.
130458 */
130459 assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 );
130460 assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
130461
130462 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
130463 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
130464 0, minMaxFlag, 0);
130465 if( pWInfo==0 ){
130466 goto select_end;
130467 }
130468 updateAccumulator(pParse, regAcc, &sAggInfo);
130469 if( regAcc ) sqlite3VdbeAddOp2(v, OP_Integer, 1, regAcc);
130470 if( sqlite3WhereIsOrdered(pWInfo)>0 ){
130471 sqlite3VdbeGoto(v, sqlite3WhereBreakLabel(pWInfo));
130472 VdbeComment((v, "%s() by index",
130473 (minMaxFlag==WHERE_ORDERBY_MIN?"min":"max")));
130474 }
130475 sqlite3WhereEnd(pWInfo);
130476 finalizeAggFunctions(pParse, &sAggInfo);
130477 }
130478
130479 sSort.pOrderBy = 0;
130480 sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
130481 selectInnerLoop(pParse, p, -1, 0, 0,
130482 pDest, addrEnd, addrEnd);
130483 }
130484 sqlite3VdbeResolveLabel(v, addrEnd);
130485
130486 } /* endif aggregate query */
130487
130488 if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){
130489 explainTempTable(pParse, "DISTINCT");
130490 }
130491
130492 /* If there is an ORDER BY clause, then we need to sort the results
130493 ** and send them to the callback one by one.
130494 */
130495 if( sSort.pOrderBy ){
130496 explainTempTable(pParse,
130497 sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
130498 assert( p->pEList==pEList );
130499 generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
130500 }
130501
130502 /* Jump here to skip this query
130503 */
130504 sqlite3VdbeResolveLabel(v, iEnd);
130505
130506 /* The SELECT has been coded. If there is an error in the Parse structure,
130507 ** set the return code to 1. Otherwise 0. */
130508 rc = (pParse->nErr>0);
130509
130510 /* Control jumps to here if an error is encountered above, or upon
130511 ** successful coding of the SELECT.
130512 */
130513select_end:
130514 sqlite3ExprListDelete(db, pMinMaxOrderBy);
130515 sqlite3DbFree(db, sAggInfo.aCol);
130516 sqlite3DbFree(db, sAggInfo.aFunc);
130517#if SELECTTRACE_ENABLED
130518 SELECTTRACE(0x1,pParse,p,("end processing\n"));
130519 if( (sqlite3SelectTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
130520 sqlite3TreeViewSelect(0, p, 0);
130521 }
130522#endif
130523 ExplainQueryPlanPop(pParse);
130524 return rc;
130525}
130526
130527/************** End of select.c **********************************************/
130528/************** Begin file table.c *******************************************/
130529/*
130530** 2001 September 15
130531**
130532** The author disclaims copyright to this source code. In place of
130533** a legal notice, here is a blessing:
130534**
130535** May you do good and not evil.
130536** May you find forgiveness for yourself and forgive others.
130537** May you share freely, never taking more than you give.
130538**
130539*************************************************************************
130540** This file contains the sqlite3_get_table() and sqlite3_free_table()
130541** interface routines. These are just wrappers around the main
130542** interface routine of sqlite3_exec().
130543**
130544** These routines are in a separate files so that they will not be linked
130545** if they are not used.
130546*/
130547/* #include "sqliteInt.h" */
130548
130549#ifndef SQLITE_OMIT_GET_TABLE
130550
130551/*
130552** This structure is used to pass data from sqlite3_get_table() through
130553** to the callback function is uses to build the result.
130554*/
130555typedef struct TabResult {
130556 char **azResult; /* Accumulated output */
130557 char *zErrMsg; /* Error message text, if an error occurs */
130558 u32 nAlloc; /* Slots allocated for azResult[] */
130559 u32 nRow; /* Number of rows in the result */
130560 u32 nColumn; /* Number of columns in the result */
130561 u32 nData; /* Slots used in azResult[]. (nRow+1)*nColumn */
130562 int rc; /* Return code from sqlite3_exec() */
130563} TabResult;
130564
130565/*
130566** This routine is called once for each row in the result table. Its job
130567** is to fill in the TabResult structure appropriately, allocating new
130568** memory as necessary.
130569*/
130570static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
130571 TabResult *p = (TabResult*)pArg; /* Result accumulator */
130572 int need; /* Slots needed in p->azResult[] */
130573 int i; /* Loop counter */
130574 char *z; /* A single column of result */
130575
130576 /* Make sure there is enough space in p->azResult to hold everything
130577 ** we need to remember from this invocation of the callback.
130578 */
130579 if( p->nRow==0 && argv!=0 ){
130580 need = nCol*2;
130581 }else{
130582 need = nCol;
130583 }
130584 if( p->nData + need > p->nAlloc ){
130585 char **azNew;
130586 p->nAlloc = p->nAlloc*2 + need;
130587 azNew = sqlite3_realloc64( p->azResult, sizeof(char*)*p->nAlloc );
130588 if( azNew==0 ) goto malloc_failed;
130589 p->azResult = azNew;
130590 }
130591
130592 /* If this is the first row, then generate an extra row containing
130593 ** the names of all columns.
130594 */
130595 if( p->nRow==0 ){
130596 p->nColumn = nCol;
130597 for(i=0; i<nCol; i++){
130598 z = sqlite3_mprintf("%s", colv[i]);
130599 if( z==0 ) goto malloc_failed;
130600 p->azResult[p->nData++] = z;
130601 }
130602 }else if( (int)p->nColumn!=nCol ){
130603 sqlite3_free(p->zErrMsg);
130604 p->zErrMsg = sqlite3_mprintf(
130605 "sqlite3_get_table() called with two or more incompatible queries"
130606 );
130607 p->rc = SQLITE_ERROR;
130608 return 1;
130609 }
130610
130611 /* Copy over the row data
130612 */
130613 if( argv!=0 ){
130614 for(i=0; i<nCol; i++){
130615 if( argv[i]==0 ){
130616 z = 0;
130617 }else{
130618 int n = sqlite3Strlen30(argv[i])+1;
130619 z = sqlite3_malloc64( n );
130620 if( z==0 ) goto malloc_failed;
130621 memcpy(z, argv[i], n);
130622 }
130623 p->azResult[p->nData++] = z;
130624 }
130625 p->nRow++;
130626 }
130627 return 0;
130628
130629malloc_failed:
130630 p->rc = SQLITE_NOMEM_BKPT;
130631 return 1;
130632}
130633
130634/*
130635** Query the database. But instead of invoking a callback for each row,
130636** malloc() for space to hold the result and return the entire results
130637** at the conclusion of the call.
130638**
130639** The result that is written to ***pazResult is held in memory obtained
130640** from malloc(). But the caller cannot free this memory directly.
130641** Instead, the entire table should be passed to sqlite3_free_table() when
130642** the calling procedure is finished using it.
130643*/
130644SQLITE_API int sqlite3_get_table(
130645 sqlite3 *db, /* The database on which the SQL executes */
130646 const char *zSql, /* The SQL to be executed */
130647 char ***pazResult, /* Write the result table here */
130648 int *pnRow, /* Write the number of rows in the result here */
130649 int *pnColumn, /* Write the number of columns of result here */
130650 char **pzErrMsg /* Write error messages here */
130651){
130652 int rc;
130653 TabResult res;
130654
130655#ifdef SQLITE_ENABLE_API_ARMOR
130656 if( !sqlite3SafetyCheckOk(db) || pazResult==0 ) return SQLITE_MISUSE_BKPT;
130657#endif
130658 *pazResult = 0;
130659 if( pnColumn ) *pnColumn = 0;
130660 if( pnRow ) *pnRow = 0;
130661 if( pzErrMsg ) *pzErrMsg = 0;
130662 res.zErrMsg = 0;
130663 res.nRow = 0;
130664 res.nColumn = 0;
130665 res.nData = 1;
130666 res.nAlloc = 20;
130667 res.rc = SQLITE_OK;
130668 res.azResult = sqlite3_malloc64(sizeof(char*)*res.nAlloc );
130669 if( res.azResult==0 ){
130670 db->errCode = SQLITE_NOMEM;
130671 return SQLITE_NOMEM_BKPT;
130672 }
130673 res.azResult[0] = 0;
130674 rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
130675 assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
130676 res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
130677 if( (rc&0xff)==SQLITE_ABORT ){
130678 sqlite3_free_table(&res.azResult[1]);
130679 if( res.zErrMsg ){
130680 if( pzErrMsg ){
130681 sqlite3_free(*pzErrMsg);
130682 *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
130683 }
130684 sqlite3_free(res.zErrMsg);
130685 }
130686 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
130687 return res.rc;
130688 }
130689 sqlite3_free(res.zErrMsg);
130690 if( rc!=SQLITE_OK ){
130691 sqlite3_free_table(&res.azResult[1]);
130692 return rc;
130693 }
130694 if( res.nAlloc>res.nData ){
130695 char **azNew;
130696 azNew = sqlite3_realloc64( res.azResult, sizeof(char*)*res.nData );
130697 if( azNew==0 ){
130698 sqlite3_free_table(&res.azResult[1]);
130699 db->errCode = SQLITE_NOMEM;
130700 return SQLITE_NOMEM_BKPT;
130701 }
130702 res.azResult = azNew;
130703 }
130704 *pazResult = &res.azResult[1];
130705 if( pnColumn ) *pnColumn = res.nColumn;
130706 if( pnRow ) *pnRow = res.nRow;
130707 return rc;
130708}
130709
130710/*
130711** This routine frees the space the sqlite3_get_table() malloced.
130712*/
130713SQLITE_API void sqlite3_free_table(
130714 char **azResult /* Result returned from sqlite3_get_table() */
130715){
130716 if( azResult ){
130717 int i, n;
130718 azResult--;
130719 assert( azResult!=0 );
130720 n = SQLITE_PTR_TO_INT(azResult[0]);
130721 for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
130722 sqlite3_free(azResult);
130723 }
130724}
130725
130726#endif /* SQLITE_OMIT_GET_TABLE */
130727
130728/************** End of table.c ***********************************************/
130729/************** Begin file trigger.c *****************************************/
130730/*
130731**
130732** The author disclaims copyright to this source code. In place of
130733** a legal notice, here is a blessing:
130734**
130735** May you do good and not evil.
130736** May you find forgiveness for yourself and forgive others.
130737** May you share freely, never taking more than you give.
130738**
130739*************************************************************************
130740** This file contains the implementation for TRIGGERs
130741*/
130742/* #include "sqliteInt.h" */
130743
130744#ifndef SQLITE_OMIT_TRIGGER
130745/*
130746** Delete a linked list of TriggerStep structures.
130747*/
130748SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
130749 while( pTriggerStep ){
130750 TriggerStep * pTmp = pTriggerStep;
130751 pTriggerStep = pTriggerStep->pNext;
130752
130753 sqlite3ExprDelete(db, pTmp->pWhere);
130754 sqlite3ExprListDelete(db, pTmp->pExprList);
130755 sqlite3SelectDelete(db, pTmp->pSelect);
130756 sqlite3IdListDelete(db, pTmp->pIdList);
130757 sqlite3UpsertDelete(db, pTmp->pUpsert);
130758 sqlite3DbFree(db, pTmp->zSpan);
130759
130760 sqlite3DbFree(db, pTmp);
130761 }
130762}
130763
130764/*
130765** Given table pTab, return a list of all the triggers attached to
130766** the table. The list is connected by Trigger.pNext pointers.
130767**
130768** All of the triggers on pTab that are in the same database as pTab
130769** are already attached to pTab->pTrigger. But there might be additional
130770** triggers on pTab in the TEMP schema. This routine prepends all
130771** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
130772** and returns the combined list.
130773**
130774** To state it another way: This routine returns a list of all triggers
130775** that fire off of pTab. The list will include any TEMP triggers on
130776** pTab as well as the triggers lised in pTab->pTrigger.
130777*/
130778SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
130779 Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
130780 Trigger *pList = 0; /* List of triggers to return */
130781
130782 if( pParse->disableTriggers ){
130783 return 0;
130784 }
130785
130786 if( pTmpSchema!=pTab->pSchema ){
130787 HashElem *p;
130788 assert( sqlite3SchemaMutexHeld(pParse->db, 0, pTmpSchema) );
130789 for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
130790 Trigger *pTrig = (Trigger *)sqliteHashData(p);
130791 if( pTrig->pTabSchema==pTab->pSchema
130792 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
130793 ){
130794 pTrig->pNext = (pList ? pList : pTab->pTrigger);
130795 pList = pTrig;
130796 }
130797 }
130798 }
130799
130800 return (pList ? pList : pTab->pTrigger);
130801}
130802
130803/*
130804** This is called by the parser when it sees a CREATE TRIGGER statement
130805** up to the point of the BEGIN before the trigger actions. A Trigger
130806** structure is generated based on the information available and stored
130807** in pParse->pNewTrigger. After the trigger actions have been parsed, the
130808** sqlite3FinishTrigger() function is called to complete the trigger
130809** construction process.
130810*/
130811SQLITE_PRIVATE void sqlite3BeginTrigger(
130812 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
130813 Token *pName1, /* The name of the trigger */
130814 Token *pName2, /* The name of the trigger */
130815 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
130816 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
130817 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
130818 SrcList *pTableName,/* The name of the table/view the trigger applies to */
130819 Expr *pWhen, /* WHEN clause */
130820 int isTemp, /* True if the TEMPORARY keyword is present */
130821 int noErr /* Suppress errors if the trigger already exists */
130822){
130823 Trigger *pTrigger = 0; /* The new trigger */
130824 Table *pTab; /* Table that the trigger fires off of */
130825 char *zName = 0; /* Name of the trigger */
130826 sqlite3 *db = pParse->db; /* The database connection */
130827 int iDb; /* The database to store the trigger in */
130828 Token *pName; /* The unqualified db name */
130829 DbFixer sFix; /* State vector for the DB fixer */
130830
130831 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
130832 assert( pName2!=0 );
130833 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
130834 assert( op>0 && op<0xff );
130835 if( isTemp ){
130836 /* If TEMP was specified, then the trigger name may not be qualified. */
130837 if( pName2->n>0 ){
130838 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
130839 goto trigger_cleanup;
130840 }
130841 iDb = 1;
130842 pName = pName1;
130843 }else{
130844 /* Figure out the db that the trigger will be created in */
130845 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
130846 if( iDb<0 ){
130847 goto trigger_cleanup;
130848 }
130849 }
130850 if( !pTableName || db->mallocFailed ){
130851 goto trigger_cleanup;
130852 }
130853
130854 /* A long-standing parser bug is that this syntax was allowed:
130855 **
130856 ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
130857 ** ^^^^^^^^
130858 **
130859 ** To maintain backwards compatibility, ignore the database
130860 ** name on pTableName if we are reparsing out of SQLITE_MASTER.
130861 */
130862 if( db->init.busy && iDb!=1 ){
130863 sqlite3DbFree(db, pTableName->a[0].zDatabase);
130864 pTableName->a[0].zDatabase = 0;
130865 }
130866
130867 /* If the trigger name was unqualified, and the table is a temp table,
130868 ** then set iDb to 1 to create the trigger in the temporary database.
130869 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
130870 ** exist, the error is caught by the block below.
130871 */
130872 pTab = sqlite3SrcListLookup(pParse, pTableName);
130873 if( db->init.busy==0 && pName2->n==0 && pTab
130874 && pTab->pSchema==db->aDb[1].pSchema ){
130875 iDb = 1;
130876 }
130877
130878 /* Ensure the table name matches database name and that the table exists */
130879 if( db->mallocFailed ) goto trigger_cleanup;
130880 assert( pTableName->nSrc==1 );
130881 sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName);
130882 if( sqlite3FixSrcList(&sFix, pTableName) ){
130883 goto trigger_cleanup;
130884 }
130885 pTab = sqlite3SrcListLookup(pParse, pTableName);
130886 if( !pTab ){
130887 /* The table does not exist. */
130888 if( db->init.iDb==1 ){
130889 /* Ticket #3810.
130890 ** Normally, whenever a table is dropped, all associated triggers are
130891 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
130892 ** and the table is dropped by a different database connection, the
130893 ** trigger is not visible to the database connection that does the
130894 ** drop so the trigger cannot be dropped. This results in an
130895 ** "orphaned trigger" - a trigger whose associated table is missing.
130896 */
130897 db->init.orphanTrigger = 1;
130898 }
130899 goto trigger_cleanup;
130900 }
130901 if( IsVirtual(pTab) ){
130902 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
130903 goto trigger_cleanup;
130904 }
130905
130906 /* Check that the trigger name is not reserved and that no trigger of the
130907 ** specified name exists */
130908 zName = sqlite3NameFromToken(db, pName);
130909 if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
130910 goto trigger_cleanup;
130911 }
130912 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
130913 if( !IN_RENAME_OBJECT ){
130914 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
130915 if( !noErr ){
130916 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
130917 }else{
130918 assert( !db->init.busy );
130919 sqlite3CodeVerifySchema(pParse, iDb);
130920 }
130921 goto trigger_cleanup;
130922 }
130923 }
130924
130925 /* Do not create a trigger on a system table */
130926 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
130927 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
130928 goto trigger_cleanup;
130929 }
130930
130931 /* INSTEAD of triggers are only for views and views only support INSTEAD
130932 ** of triggers.
130933 */
130934 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
130935 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
130936 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
130937 goto trigger_cleanup;
130938 }
130939 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
130940 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
130941 " trigger on table: %S", pTableName, 0);
130942 goto trigger_cleanup;
130943 }
130944
130945#ifndef SQLITE_OMIT_AUTHORIZATION
130946 if( !IN_RENAME_OBJECT ){
130947 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
130948 int code = SQLITE_CREATE_TRIGGER;
130949 const char *zDb = db->aDb[iTabDb].zDbSName;
130950 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
130951 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
130952 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
130953 goto trigger_cleanup;
130954 }
130955 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
130956 goto trigger_cleanup;
130957 }
130958 }
130959#endif
130960
130961 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
130962 ** cannot appear on views. So we might as well translate every
130963 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
130964 ** elsewhere.
130965 */
130966 if (tr_tm == TK_INSTEAD){
130967 tr_tm = TK_BEFORE;
130968 }
130969
130970 /* Build the Trigger object */
130971 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
130972 if( pTrigger==0 ) goto trigger_cleanup;
130973 pTrigger->zName = zName;
130974 zName = 0;
130975 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
130976 pTrigger->pSchema = db->aDb[iDb].pSchema;
130977 pTrigger->pTabSchema = pTab->pSchema;
130978 pTrigger->op = (u8)op;
130979 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
130980 if( IN_RENAME_OBJECT ){
130981 sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
130982 pTrigger->pWhen = pWhen;
130983 pWhen = 0;
130984 }else{
130985 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
130986 }
130987 pTrigger->pColumns = pColumns;
130988 pColumns = 0;
130989 assert( pParse->pNewTrigger==0 );
130990 pParse->pNewTrigger = pTrigger;
130991
130992trigger_cleanup:
130993 sqlite3DbFree(db, zName);
130994 sqlite3SrcListDelete(db, pTableName);
130995 sqlite3IdListDelete(db, pColumns);
130996 sqlite3ExprDelete(db, pWhen);
130997 if( !pParse->pNewTrigger ){
130998 sqlite3DeleteTrigger(db, pTrigger);
130999 }else{
131000 assert( pParse->pNewTrigger==pTrigger );
131001 }
131002}
131003
131004/*
131005** This routine is called after all of the trigger actions have been parsed
131006** in order to complete the process of building the trigger.
131007*/
131008SQLITE_PRIVATE void sqlite3FinishTrigger(
131009 Parse *pParse, /* Parser context */
131010 TriggerStep *pStepList, /* The triggered program */
131011 Token *pAll /* Token that describes the complete CREATE TRIGGER */
131012){
131013 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
131014 char *zName; /* Name of trigger */
131015 sqlite3 *db = pParse->db; /* The database */
131016 DbFixer sFix; /* Fixer object */
131017 int iDb; /* Database containing the trigger */
131018 Token nameToken; /* Trigger name for error reporting */
131019
131020 pParse->pNewTrigger = 0;
131021 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
131022 zName = pTrig->zName;
131023 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
131024 pTrig->step_list = pStepList;
131025 while( pStepList ){
131026 pStepList->pTrig = pTrig;
131027 pStepList = pStepList->pNext;
131028 }
131029 sqlite3TokenInit(&nameToken, pTrig->zName);
131030 sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
131031 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
131032 || sqlite3FixExpr(&sFix, pTrig->pWhen)
131033 ){
131034 goto triggerfinish_cleanup;
131035 }
131036
131037#ifndef SQLITE_OMIT_ALTERTABLE
131038 if( IN_RENAME_OBJECT ){
131039 assert( !db->init.busy );
131040 pParse->pNewTrigger = pTrig;
131041 pTrig = 0;
131042 }else
131043#endif
131044
131045 /* if we are not initializing,
131046 ** build the sqlite_master entry
131047 */
131048 if( !db->init.busy ){
131049 Vdbe *v;
131050 char *z;
131051
131052 /* Make an entry in the sqlite_master table */
131053 v = sqlite3GetVdbe(pParse);
131054 if( v==0 ) goto triggerfinish_cleanup;
131055 sqlite3BeginWriteOperation(pParse, 0, iDb);
131056 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
131057 testcase( z==0 );
131058 sqlite3NestedParse(pParse,
131059 "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
131060 db->aDb[iDb].zDbSName, MASTER_NAME, zName,
131061 pTrig->table, z);
131062 sqlite3DbFree(db, z);
131063 sqlite3ChangeCookie(pParse, iDb);
131064 sqlite3VdbeAddParseSchemaOp(v, iDb,
131065 sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
131066 }
131067
131068 if( db->init.busy ){
131069 Trigger *pLink = pTrig;
131070 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
131071 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
131072 pTrig = sqlite3HashInsert(pHash, zName, pTrig);
131073 if( pTrig ){
131074 sqlite3OomFault(db);
131075 }else if( pLink->pSchema==pLink->pTabSchema ){
131076 Table *pTab;
131077 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
131078 assert( pTab!=0 );
131079 pLink->pNext = pTab->pTrigger;
131080 pTab->pTrigger = pLink;
131081 }
131082 }
131083
131084triggerfinish_cleanup:
131085 sqlite3DeleteTrigger(db, pTrig);
131086 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
131087 sqlite3DeleteTriggerStep(db, pStepList);
131088}
131089
131090/*
131091** Duplicate a range of text from an SQL statement, then convert all
131092** whitespace characters into ordinary space characters.
131093*/
131094static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
131095 char *z = sqlite3DbSpanDup(db, zStart, zEnd);
131096 int i;
131097 if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i]) ) z[i] = ' ';
131098 return z;
131099}
131100
131101/*
131102** Turn a SELECT statement (that the pSelect parameter points to) into
131103** a trigger step. Return a pointer to a TriggerStep structure.
131104**
131105** The parser calls this routine when it finds a SELECT statement in
131106** body of a TRIGGER.
131107*/
131108SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(
131109 sqlite3 *db, /* Database connection */
131110 Select *pSelect, /* The SELECT statement */
131111 const char *zStart, /* Start of SQL text */
131112 const char *zEnd /* End of SQL text */
131113){
131114 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
131115 if( pTriggerStep==0 ) {
131116 sqlite3SelectDelete(db, pSelect);
131117 return 0;
131118 }
131119 pTriggerStep->op = TK_SELECT;
131120 pTriggerStep->pSelect = pSelect;
131121 pTriggerStep->orconf = OE_Default;
131122 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
131123 return pTriggerStep;
131124}
131125
131126/*
131127** Allocate space to hold a new trigger step. The allocated space
131128** holds both the TriggerStep object and the TriggerStep.target.z string.
131129**
131130** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
131131*/
131132static TriggerStep *triggerStepAllocate(
131133 Parse *pParse, /* Parser context */
131134 u8 op, /* Trigger opcode */
131135 Token *pName, /* The target name */
131136 const char *zStart, /* Start of SQL text */
131137 const char *zEnd /* End of SQL text */
131138){
131139 sqlite3 *db = pParse->db;
131140 TriggerStep *pTriggerStep;
131141
131142 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
131143 if( pTriggerStep ){
131144 char *z = (char*)&pTriggerStep[1];
131145 memcpy(z, pName->z, pName->n);
131146 sqlite3Dequote(z);
131147 pTriggerStep->zTarget = z;
131148 pTriggerStep->op = op;
131149 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
131150 if( IN_RENAME_OBJECT ){
131151 sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
131152 }
131153 }
131154 return pTriggerStep;
131155}
131156
131157/*
131158** Build a trigger step out of an INSERT statement. Return a pointer
131159** to the new trigger step.
131160**
131161** The parser calls this routine when it sees an INSERT inside the
131162** body of a trigger.
131163*/
131164SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(
131165 Parse *pParse, /* Parser */
131166 Token *pTableName, /* Name of the table into which we insert */
131167 IdList *pColumn, /* List of columns in pTableName to insert into */
131168 Select *pSelect, /* A SELECT statement that supplies values */
131169 u8 orconf, /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
131170 Upsert *pUpsert, /* ON CONFLICT clauses for upsert */
131171 const char *zStart, /* Start of SQL text */
131172 const char *zEnd /* End of SQL text */
131173){
131174 sqlite3 *db = pParse->db;
131175 TriggerStep *pTriggerStep;
131176
131177 assert(pSelect != 0 || db->mallocFailed);
131178
131179 pTriggerStep = triggerStepAllocate(pParse, TK_INSERT, pTableName,zStart,zEnd);
131180 if( pTriggerStep ){
131181 if( IN_RENAME_OBJECT ){
131182 pTriggerStep->pSelect = pSelect;
131183 pSelect = 0;
131184 }else{
131185 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
131186 }
131187 pTriggerStep->pIdList = pColumn;
131188 pTriggerStep->pUpsert = pUpsert;
131189 pTriggerStep->orconf = orconf;
131190 }else{
131191 testcase( pColumn );
131192 sqlite3IdListDelete(db, pColumn);
131193 testcase( pUpsert );
131194 sqlite3UpsertDelete(db, pUpsert);
131195 }
131196 sqlite3SelectDelete(db, pSelect);
131197
131198 return pTriggerStep;
131199}
131200
131201/*
131202** Construct a trigger step that implements an UPDATE statement and return
131203** a pointer to that trigger step. The parser calls this routine when it
131204** sees an UPDATE statement inside the body of a CREATE TRIGGER.
131205*/
131206SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(
131207 Parse *pParse, /* Parser */
131208 Token *pTableName, /* Name of the table to be updated */
131209 ExprList *pEList, /* The SET clause: list of column and new values */
131210 Expr *pWhere, /* The WHERE clause */
131211 u8 orconf, /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
131212 const char *zStart, /* Start of SQL text */
131213 const char *zEnd /* End of SQL text */
131214){
131215 sqlite3 *db = pParse->db;
131216 TriggerStep *pTriggerStep;
131217
131218 pTriggerStep = triggerStepAllocate(pParse, TK_UPDATE, pTableName,zStart,zEnd);
131219 if( pTriggerStep ){
131220 if( IN_RENAME_OBJECT ){
131221 pTriggerStep->pExprList = pEList;
131222 pTriggerStep->pWhere = pWhere;
131223 pEList = 0;
131224 pWhere = 0;
131225 }else{
131226 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
131227 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
131228 }
131229 pTriggerStep->orconf = orconf;
131230 }
131231 sqlite3ExprListDelete(db, pEList);
131232 sqlite3ExprDelete(db, pWhere);
131233 return pTriggerStep;
131234}
131235
131236/*
131237** Construct a trigger step that implements a DELETE statement and return
131238** a pointer to that trigger step. The parser calls this routine when it
131239** sees a DELETE statement inside the body of a CREATE TRIGGER.
131240*/
131241SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(
131242 Parse *pParse, /* Parser */
131243 Token *pTableName, /* The table from which rows are deleted */
131244 Expr *pWhere, /* The WHERE clause */
131245 const char *zStart, /* Start of SQL text */
131246 const char *zEnd /* End of SQL text */
131247){
131248 sqlite3 *db = pParse->db;
131249 TriggerStep *pTriggerStep;
131250
131251 pTriggerStep = triggerStepAllocate(pParse, TK_DELETE, pTableName,zStart,zEnd);
131252 if( pTriggerStep ){
131253 if( IN_RENAME_OBJECT ){
131254 pTriggerStep->pWhere = pWhere;
131255 pWhere = 0;
131256 }else{
131257 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
131258 }
131259 pTriggerStep->orconf = OE_Default;
131260 }
131261 sqlite3ExprDelete(db, pWhere);
131262 return pTriggerStep;
131263}
131264
131265/*
131266** Recursively delete a Trigger structure
131267*/
131268SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
131269 if( pTrigger==0 ) return;
131270 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
131271 sqlite3DbFree(db, pTrigger->zName);
131272 sqlite3DbFree(db, pTrigger->table);
131273 sqlite3ExprDelete(db, pTrigger->pWhen);
131274 sqlite3IdListDelete(db, pTrigger->pColumns);
131275 sqlite3DbFree(db, pTrigger);
131276}
131277
131278/*
131279** This function is called to drop a trigger from the database schema.
131280**
131281** This may be called directly from the parser and therefore identifies
131282** the trigger by name. The sqlite3DropTriggerPtr() routine does the
131283** same job as this routine except it takes a pointer to the trigger
131284** instead of the trigger name.
131285**/
131286SQLITE_PRIVATE void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
131287 Trigger *pTrigger = 0;
131288 int i;
131289 const char *zDb;
131290 const char *zName;
131291 sqlite3 *db = pParse->db;
131292
131293 if( db->mallocFailed ) goto drop_trigger_cleanup;
131294 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
131295 goto drop_trigger_cleanup;
131296 }
131297
131298 assert( pName->nSrc==1 );
131299 zDb = pName->a[0].zDatabase;
131300 zName = pName->a[0].zName;
131301 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
131302 for(i=OMIT_TEMPDB; i<db->nDb; i++){
131303 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
131304 if( zDb && sqlite3StrICmp(db->aDb[j].zDbSName, zDb) ) continue;
131305 assert( sqlite3SchemaMutexHeld(db, j, 0) );
131306 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
131307 if( pTrigger ) break;
131308 }
131309 if( !pTrigger ){
131310 if( !noErr ){
131311 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
131312 }else{
131313 sqlite3CodeVerifyNamedSchema(pParse, zDb);
131314 }
131315 pParse->checkSchema = 1;
131316 goto drop_trigger_cleanup;
131317 }
131318 sqlite3DropTriggerPtr(pParse, pTrigger);
131319
131320drop_trigger_cleanup:
131321 sqlite3SrcListDelete(db, pName);
131322}
131323
131324/*
131325** Return a pointer to the Table structure for the table that a trigger
131326** is set on.
131327*/
131328static Table *tableOfTrigger(Trigger *pTrigger){
131329 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
131330}
131331
131332
131333/*
131334** Drop a trigger given a pointer to that trigger.
131335*/
131336SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
131337 Table *pTable;
131338 Vdbe *v;
131339 sqlite3 *db = pParse->db;
131340 int iDb;
131341
131342 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
131343 assert( iDb>=0 && iDb<db->nDb );
131344 pTable = tableOfTrigger(pTrigger);
131345 assert( pTable );
131346 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
131347#ifndef SQLITE_OMIT_AUTHORIZATION
131348 {
131349 int code = SQLITE_DROP_TRIGGER;
131350 const char *zDb = db->aDb[iDb].zDbSName;
131351 const char *zTab = SCHEMA_TABLE(iDb);
131352 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
131353 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
131354 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
131355 return;
131356 }
131357 }
131358#endif
131359
131360 /* Generate code to destroy the database record of the trigger.
131361 */
131362 assert( pTable!=0 );
131363 if( (v = sqlite3GetVdbe(pParse))!=0 ){
131364 sqlite3NestedParse(pParse,
131365 "DELETE FROM %Q.%s WHERE name=%Q AND type='trigger'",
131366 db->aDb[iDb].zDbSName, MASTER_NAME, pTrigger->zName
131367 );
131368 sqlite3ChangeCookie(pParse, iDb);
131369 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
131370 }
131371}
131372
131373/*
131374** Remove a trigger from the hash tables of the sqlite* pointer.
131375*/
131376SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
131377 Trigger *pTrigger;
131378 Hash *pHash;
131379
131380 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
131381 pHash = &(db->aDb[iDb].pSchema->trigHash);
131382 pTrigger = sqlite3HashInsert(pHash, zName, 0);
131383 if( ALWAYS(pTrigger) ){
131384 if( pTrigger->pSchema==pTrigger->pTabSchema ){
131385 Table *pTab = tableOfTrigger(pTrigger);
131386 Trigger **pp;
131387 for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
131388 *pp = (*pp)->pNext;
131389 }
131390 sqlite3DeleteTrigger(db, pTrigger);
131391 db->mDbFlags |= DBFLAG_SchemaChange;
131392 }
131393}
131394
131395/*
131396** pEList is the SET clause of an UPDATE statement. Each entry
131397** in pEList is of the format <id>=<expr>. If any of the entries
131398** in pEList have an <id> which matches an identifier in pIdList,
131399** then return TRUE. If pIdList==NULL, then it is considered a
131400** wildcard that matches anything. Likewise if pEList==NULL then
131401** it matches anything so always return true. Return false only
131402** if there is no match.
131403*/
131404static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
131405 int e;
131406 if( pIdList==0 || NEVER(pEList==0) ) return 1;
131407 for(e=0; e<pEList->nExpr; e++){
131408 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
131409 }
131410 return 0;
131411}
131412
131413/*
131414** Return a list of all triggers on table pTab if there exists at least
131415** one trigger that must be fired when an operation of type 'op' is
131416** performed on the table, and, if that operation is an UPDATE, if at
131417** least one of the columns in pChanges is being modified.
131418*/
131419SQLITE_PRIVATE Trigger *sqlite3TriggersExist(
131420 Parse *pParse, /* Parse context */
131421 Table *pTab, /* The table the contains the triggers */
131422 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
131423 ExprList *pChanges, /* Columns that change in an UPDATE statement */
131424 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
131425){
131426 int mask = 0;
131427 Trigger *pList = 0;
131428 Trigger *p;
131429
131430 if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
131431 pList = sqlite3TriggerList(pParse, pTab);
131432 }
131433 assert( pList==0 || IsVirtual(pTab)==0 );
131434 for(p=pList; p; p=p->pNext){
131435 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
131436 mask |= p->tr_tm;
131437 }
131438 }
131439 if( pMask ){
131440 *pMask = mask;
131441 }
131442 return (mask ? pList : 0);
131443}
131444
131445/*
131446** Convert the pStep->zTarget string into a SrcList and return a pointer
131447** to that SrcList.
131448**
131449** This routine adds a specific database name, if needed, to the target when
131450** forming the SrcList. This prevents a trigger in one database from
131451** referring to a target in another database. An exception is when the
131452** trigger is in TEMP in which case it can refer to any other database it
131453** wants.
131454*/
131455static SrcList *targetSrcList(
131456 Parse *pParse, /* The parsing context */
131457 TriggerStep *pStep /* The trigger containing the target token */
131458){
131459 sqlite3 *db = pParse->db;
131460 int iDb; /* Index of the database to use */
131461 SrcList *pSrc; /* SrcList to be returned */
131462
131463 pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
131464 if( pSrc ){
131465 assert( pSrc->nSrc>0 );
131466 pSrc->a[pSrc->nSrc-1].zName = sqlite3DbStrDup(db, pStep->zTarget);
131467 iDb = sqlite3SchemaToIndex(db, pStep->pTrig->pSchema);
131468 if( iDb==0 || iDb>=2 ){
131469 const char *zDb;
131470 assert( iDb<db->nDb );
131471 zDb = db->aDb[iDb].zDbSName;
131472 pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, zDb);
131473 }
131474 }
131475 return pSrc;
131476}
131477
131478/*
131479** Generate VDBE code for the statements inside the body of a single
131480** trigger.
131481*/
131482static int codeTriggerProgram(
131483 Parse *pParse, /* The parser context */
131484 TriggerStep *pStepList, /* List of statements inside the trigger body */
131485 int orconf /* Conflict algorithm. (OE_Abort, etc) */
131486){
131487 TriggerStep *pStep;
131488 Vdbe *v = pParse->pVdbe;
131489 sqlite3 *db = pParse->db;
131490
131491 assert( pParse->pTriggerTab && pParse->pToplevel );
131492 assert( pStepList );
131493 assert( v!=0 );
131494 for(pStep=pStepList; pStep; pStep=pStep->pNext){
131495 /* Figure out the ON CONFLICT policy that will be used for this step
131496 ** of the trigger program. If the statement that caused this trigger
131497 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
131498 ** the ON CONFLICT policy that was specified as part of the trigger
131499 ** step statement. Example:
131500 **
131501 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
131502 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
131503 ** END;
131504 **
131505 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
131506 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
131507 */
131508 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
131509 assert( pParse->okConstFactor==0 );
131510
131511#ifndef SQLITE_OMIT_TRACE
131512 if( pStep->zSpan ){
131513 sqlite3VdbeAddOp4(v, OP_Trace, 0x7fffffff, 1, 0,
131514 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
131515 P4_DYNAMIC);
131516 }
131517#endif
131518
131519 switch( pStep->op ){
131520 case TK_UPDATE: {
131521 sqlite3Update(pParse,
131522 targetSrcList(pParse, pStep),
131523 sqlite3ExprListDup(db, pStep->pExprList, 0),
131524 sqlite3ExprDup(db, pStep->pWhere, 0),
131525 pParse->eOrconf, 0, 0, 0
131526 );
131527 break;
131528 }
131529 case TK_INSERT: {
131530 sqlite3Insert(pParse,
131531 targetSrcList(pParse, pStep),
131532 sqlite3SelectDup(db, pStep->pSelect, 0),
131533 sqlite3IdListDup(db, pStep->pIdList),
131534 pParse->eOrconf,
131535 sqlite3UpsertDup(db, pStep->pUpsert)
131536 );
131537 break;
131538 }
131539 case TK_DELETE: {
131540 sqlite3DeleteFrom(pParse,
131541 targetSrcList(pParse, pStep),
131542 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
131543 );
131544 break;
131545 }
131546 default: assert( pStep->op==TK_SELECT ); {
131547 SelectDest sDest;
131548 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
131549 sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
131550 sqlite3Select(pParse, pSelect, &sDest);
131551 sqlite3SelectDelete(db, pSelect);
131552 break;
131553 }
131554 }
131555 if( pStep->op!=TK_SELECT ){
131556 sqlite3VdbeAddOp0(v, OP_ResetCount);
131557 }
131558 }
131559
131560 return 0;
131561}
131562
131563#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
131564/*
131565** This function is used to add VdbeComment() annotations to a VDBE
131566** program. It is not used in production code, only for debugging.
131567*/
131568static const char *onErrorText(int onError){
131569 switch( onError ){
131570 case OE_Abort: return "abort";
131571 case OE_Rollback: return "rollback";
131572 case OE_Fail: return "fail";
131573 case OE_Replace: return "replace";
131574 case OE_Ignore: return "ignore";
131575 case OE_Default: return "default";
131576 }
131577 return "n/a";
131578}
131579#endif
131580
131581/*
131582** Parse context structure pFrom has just been used to create a sub-vdbe
131583** (trigger program). If an error has occurred, transfer error information
131584** from pFrom to pTo.
131585*/
131586static void transferParseError(Parse *pTo, Parse *pFrom){
131587 assert( pFrom->zErrMsg==0 || pFrom->nErr );
131588 assert( pTo->zErrMsg==0 || pTo->nErr );
131589 if( pTo->nErr==0 ){
131590 pTo->zErrMsg = pFrom->zErrMsg;
131591 pTo->nErr = pFrom->nErr;
131592 pTo->rc = pFrom->rc;
131593 }else{
131594 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
131595 }
131596}
131597
131598/*
131599** Create and populate a new TriggerPrg object with a sub-program
131600** implementing trigger pTrigger with ON CONFLICT policy orconf.
131601*/
131602static TriggerPrg *codeRowTrigger(
131603 Parse *pParse, /* Current parse context */
131604 Trigger *pTrigger, /* Trigger to code */
131605 Table *pTab, /* The table pTrigger is attached to */
131606 int orconf /* ON CONFLICT policy to code trigger program with */
131607){
131608 Parse *pTop = sqlite3ParseToplevel(pParse);
131609 sqlite3 *db = pParse->db; /* Database handle */
131610 TriggerPrg *pPrg; /* Value to return */
131611 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
131612 Vdbe *v; /* Temporary VM */
131613 NameContext sNC; /* Name context for sub-vdbe */
131614 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
131615 Parse *pSubParse; /* Parse context for sub-vdbe */
131616 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
131617
131618 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
131619 assert( pTop->pVdbe );
131620
131621 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
131622 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
131623 ** list of the top-level Parse object sooner rather than later. */
131624 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
131625 if( !pPrg ) return 0;
131626 pPrg->pNext = pTop->pTriggerPrg;
131627 pTop->pTriggerPrg = pPrg;
131628 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
131629 if( !pProgram ) return 0;
131630 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
131631 pPrg->pTrigger = pTrigger;
131632 pPrg->orconf = orconf;
131633 pPrg->aColmask[0] = 0xffffffff;
131634 pPrg->aColmask[1] = 0xffffffff;
131635
131636 /* Allocate and populate a new Parse context to use for coding the
131637 ** trigger sub-program. */
131638 pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
131639 if( !pSubParse ) return 0;
131640 memset(&sNC, 0, sizeof(sNC));
131641 sNC.pParse = pSubParse;
131642 pSubParse->db = db;
131643 pSubParse->pTriggerTab = pTab;
131644 pSubParse->pToplevel = pTop;
131645 pSubParse->zAuthContext = pTrigger->zName;
131646 pSubParse->eTriggerOp = pTrigger->op;
131647 pSubParse->nQueryLoop = pParse->nQueryLoop;
131648
131649 v = sqlite3GetVdbe(pSubParse);
131650 if( v ){
131651 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
131652 pTrigger->zName, onErrorText(orconf),
131653 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
131654 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
131655 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
131656 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
131657 pTab->zName
131658 ));
131659#ifndef SQLITE_OMIT_TRACE
131660 if( pTrigger->zName ){
131661 sqlite3VdbeChangeP4(v, -1,
131662 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
131663 );
131664 }
131665#endif
131666
131667 /* If one was specified, code the WHEN clause. If it evaluates to false
131668 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
131669 ** OP_Halt inserted at the end of the program. */
131670 if( pTrigger->pWhen ){
131671 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
131672 if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
131673 && db->mallocFailed==0
131674 ){
131675 iEndTrigger = sqlite3VdbeMakeLabel(v);
131676 sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
131677 }
131678 sqlite3ExprDelete(db, pWhen);
131679 }
131680
131681 /* Code the trigger program into the sub-vdbe. */
131682 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
131683
131684 /* Insert an OP_Halt at the end of the sub-program. */
131685 if( iEndTrigger ){
131686 sqlite3VdbeResolveLabel(v, iEndTrigger);
131687 }
131688 sqlite3VdbeAddOp0(v, OP_Halt);
131689 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
131690
131691 transferParseError(pParse, pSubParse);
131692 if( db->mallocFailed==0 && pParse->nErr==0 ){
131693 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
131694 }
131695 pProgram->nMem = pSubParse->nMem;
131696 pProgram->nCsr = pSubParse->nTab;
131697 pProgram->token = (void *)pTrigger;
131698 pPrg->aColmask[0] = pSubParse->oldmask;
131699 pPrg->aColmask[1] = pSubParse->newmask;
131700 sqlite3VdbeDelete(v);
131701 }
131702
131703 assert( !pSubParse->pAinc && !pSubParse->pZombieTab );
131704 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
131705 sqlite3ParserReset(pSubParse);
131706 sqlite3StackFree(db, pSubParse);
131707
131708 return pPrg;
131709}
131710
131711/*
131712** Return a pointer to a TriggerPrg object containing the sub-program for
131713** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
131714** TriggerPrg object exists, a new object is allocated and populated before
131715** being returned.
131716*/
131717static TriggerPrg *getRowTrigger(
131718 Parse *pParse, /* Current parse context */
131719 Trigger *pTrigger, /* Trigger to code */
131720 Table *pTab, /* The table trigger pTrigger is attached to */
131721 int orconf /* ON CONFLICT algorithm. */
131722){
131723 Parse *pRoot = sqlite3ParseToplevel(pParse);
131724 TriggerPrg *pPrg;
131725
131726 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
131727
131728 /* It may be that this trigger has already been coded (or is in the
131729 ** process of being coded). If this is the case, then an entry with
131730 ** a matching TriggerPrg.pTrigger field will be present somewhere
131731 ** in the Parse.pTriggerPrg list. Search for such an entry. */
131732 for(pPrg=pRoot->pTriggerPrg;
131733 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
131734 pPrg=pPrg->pNext
131735 );
131736
131737 /* If an existing TriggerPrg could not be located, create a new one. */
131738 if( !pPrg ){
131739 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
131740 }
131741
131742 return pPrg;
131743}
131744
131745/*
131746** Generate code for the trigger program associated with trigger p on
131747** table pTab. The reg, orconf and ignoreJump parameters passed to this
131748** function are the same as those described in the header function for
131749** sqlite3CodeRowTrigger()
131750*/
131751SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(
131752 Parse *pParse, /* Parse context */
131753 Trigger *p, /* Trigger to code */
131754 Table *pTab, /* The table to code triggers from */
131755 int reg, /* Reg array containing OLD.* and NEW.* values */
131756 int orconf, /* ON CONFLICT policy */
131757 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
131758){
131759 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
131760 TriggerPrg *pPrg;
131761 pPrg = getRowTrigger(pParse, p, pTab, orconf);
131762 assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
131763
131764 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
131765 ** is a pointer to the sub-vdbe containing the trigger program. */
131766 if( pPrg ){
131767 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
131768
131769 sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
131770 (const char *)pPrg->pProgram, P4_SUBPROGRAM);
131771 VdbeComment(
131772 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
131773
131774 /* Set the P5 operand of the OP_Program instruction to non-zero if
131775 ** recursive invocation of this trigger program is disallowed. Recursive
131776 ** invocation is disallowed if (a) the sub-program is really a trigger,
131777 ** not a foreign key action, and (b) the flag to enable recursive triggers
131778 ** is clear. */
131779 sqlite3VdbeChangeP5(v, (u8)bRecursive);
131780 }
131781}
131782
131783/*
131784** This is called to code the required FOR EACH ROW triggers for an operation
131785** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
131786** is given by the op parameter. The tr_tm parameter determines whether the
131787** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
131788** parameter pChanges is passed the list of columns being modified.
131789**
131790** If there are no triggers that fire at the specified time for the specified
131791** operation on pTab, this function is a no-op.
131792**
131793** The reg argument is the address of the first in an array of registers
131794** that contain the values substituted for the new.* and old.* references
131795** in the trigger program. If N is the number of columns in table pTab
131796** (a copy of pTab->nCol), then registers are populated as follows:
131797**
131798** Register Contains
131799** ------------------------------------------------------
131800** reg+0 OLD.rowid
131801** reg+1 OLD.* value of left-most column of pTab
131802** ... ...
131803** reg+N OLD.* value of right-most column of pTab
131804** reg+N+1 NEW.rowid
131805** reg+N+2 OLD.* value of left-most column of pTab
131806** ... ...
131807** reg+N+N+1 NEW.* value of right-most column of pTab
131808**
131809** For ON DELETE triggers, the registers containing the NEW.* values will
131810** never be accessed by the trigger program, so they are not allocated or
131811** populated by the caller (there is no data to populate them with anyway).
131812** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
131813** are never accessed, and so are not allocated by the caller. So, for an
131814** ON INSERT trigger, the value passed to this function as parameter reg
131815** is not a readable register, although registers (reg+N) through
131816** (reg+N+N+1) are.
131817**
131818** Parameter orconf is the default conflict resolution algorithm for the
131819** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
131820** is the instruction that control should jump to if a trigger program
131821** raises an IGNORE exception.
131822*/
131823SQLITE_PRIVATE void sqlite3CodeRowTrigger(
131824 Parse *pParse, /* Parse context */
131825 Trigger *pTrigger, /* List of triggers on table pTab */
131826 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
131827 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
131828 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
131829 Table *pTab, /* The table to code triggers from */
131830 int reg, /* The first in an array of registers (see above) */
131831 int orconf, /* ON CONFLICT policy */
131832 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
131833){
131834 Trigger *p; /* Used to iterate through pTrigger list */
131835
131836 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
131837 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
131838 assert( (op==TK_UPDATE)==(pChanges!=0) );
131839
131840 for(p=pTrigger; p; p=p->pNext){
131841
131842 /* Sanity checking: The schema for the trigger and for the table are
131843 ** always defined. The trigger must be in the same schema as the table
131844 ** or else it must be a TEMP trigger. */
131845 assert( p->pSchema!=0 );
131846 assert( p->pTabSchema!=0 );
131847 assert( p->pSchema==p->pTabSchema
131848 || p->pSchema==pParse->db->aDb[1].pSchema );
131849
131850 /* Determine whether we should code this trigger */
131851 if( p->op==op
131852 && p->tr_tm==tr_tm
131853 && checkColumnOverlap(p->pColumns, pChanges)
131854 ){
131855 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
131856 }
131857 }
131858}
131859
131860/*
131861** Triggers may access values stored in the old.* or new.* pseudo-table.
131862** This function returns a 32-bit bitmask indicating which columns of the
131863** old.* or new.* tables actually are used by triggers. This information
131864** may be used by the caller, for example, to avoid having to load the entire
131865** old.* record into memory when executing an UPDATE or DELETE command.
131866**
131867** Bit 0 of the returned mask is set if the left-most column of the
131868** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
131869** the second leftmost column value is required, and so on. If there
131870** are more than 32 columns in the table, and at least one of the columns
131871** with an index greater than 32 may be accessed, 0xffffffff is returned.
131872**
131873** It is not possible to determine if the old.rowid or new.rowid column is
131874** accessed by triggers. The caller must always assume that it is.
131875**
131876** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
131877** applies to the old.* table. If 1, the new.* table.
131878**
131879** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
131880** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
131881** included in the returned mask if the TRIGGER_BEFORE bit is set in the
131882** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
131883** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
131884*/
131885SQLITE_PRIVATE u32 sqlite3TriggerColmask(
131886 Parse *pParse, /* Parse context */
131887 Trigger *pTrigger, /* List of triggers on table pTab */
131888 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
131889 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
131890 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
131891 Table *pTab, /* The table to code triggers from */
131892 int orconf /* Default ON CONFLICT policy for trigger steps */
131893){
131894 const int op = pChanges ? TK_UPDATE : TK_DELETE;
131895 u32 mask = 0;
131896 Trigger *p;
131897
131898 assert( isNew==1 || isNew==0 );
131899 for(p=pTrigger; p; p=p->pNext){
131900 if( p->op==op && (tr_tm&p->tr_tm)
131901 && checkColumnOverlap(p->pColumns,pChanges)
131902 ){
131903 TriggerPrg *pPrg;
131904 pPrg = getRowTrigger(pParse, p, pTab, orconf);
131905 if( pPrg ){
131906 mask |= pPrg->aColmask[isNew];
131907 }
131908 }
131909 }
131910
131911 return mask;
131912}
131913
131914#endif /* !defined(SQLITE_OMIT_TRIGGER) */
131915
131916/************** End of trigger.c *********************************************/
131917/************** Begin file update.c ******************************************/
131918/*
131919** 2001 September 15
131920**
131921** The author disclaims copyright to this source code. In place of
131922** a legal notice, here is a blessing:
131923**
131924** May you do good and not evil.
131925** May you find forgiveness for yourself and forgive others.
131926** May you share freely, never taking more than you give.
131927**
131928*************************************************************************
131929** This file contains C code routines that are called by the parser
131930** to handle UPDATE statements.
131931*/
131932/* #include "sqliteInt.h" */
131933
131934#ifndef SQLITE_OMIT_VIRTUALTABLE
131935/* Forward declaration */
131936static void updateVirtualTable(
131937 Parse *pParse, /* The parsing context */
131938 SrcList *pSrc, /* The virtual table to be modified */
131939 Table *pTab, /* The virtual table */
131940 ExprList *pChanges, /* The columns to change in the UPDATE statement */
131941 Expr *pRowidExpr, /* Expression used to recompute the rowid */
131942 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
131943 Expr *pWhere, /* WHERE clause of the UPDATE statement */
131944 int onError /* ON CONFLICT strategy */
131945);
131946#endif /* SQLITE_OMIT_VIRTUALTABLE */
131947
131948/*
131949** The most recently coded instruction was an OP_Column to retrieve the
131950** i-th column of table pTab. This routine sets the P4 parameter of the
131951** OP_Column to the default value, if any.
131952**
131953** The default value of a column is specified by a DEFAULT clause in the
131954** column definition. This was either supplied by the user when the table
131955** was created, or added later to the table definition by an ALTER TABLE
131956** command. If the latter, then the row-records in the table btree on disk
131957** may not contain a value for the column and the default value, taken
131958** from the P4 parameter of the OP_Column instruction, is returned instead.
131959** If the former, then all row-records are guaranteed to include a value
131960** for the column and the P4 value is not required.
131961**
131962** Column definitions created by an ALTER TABLE command may only have
131963** literal default values specified: a number, null or a string. (If a more
131964** complicated default expression value was provided, it is evaluated
131965** when the ALTER TABLE is executed and one of the literal values written
131966** into the sqlite_master table.)
131967**
131968** Therefore, the P4 parameter is only required if the default value for
131969** the column is a literal number, string or null. The sqlite3ValueFromExpr()
131970** function is capable of transforming these types of expressions into
131971** sqlite3_value objects.
131972**
131973** If parameter iReg is not negative, code an OP_RealAffinity instruction
131974** on register iReg. This is used when an equivalent integer value is
131975** stored in place of an 8-byte floating point value in order to save
131976** space.
131977*/
131978SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
131979 assert( pTab!=0 );
131980 if( !pTab->pSelect ){
131981 sqlite3_value *pValue = 0;
131982 u8 enc = ENC(sqlite3VdbeDb(v));
131983 Column *pCol = &pTab->aCol[i];
131984 VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
131985 assert( i<pTab->nCol );
131986 sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
131987 pCol->affinity, &pValue);
131988 if( pValue ){
131989 sqlite3VdbeAppendP4(v, pValue, P4_MEM);
131990 }
131991 }
131992#ifndef SQLITE_OMIT_FLOATING_POINT
131993 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
131994 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
131995 }
131996#endif
131997}
131998
131999/*
132000** Check to see if column iCol of index pIdx references any of the
132001** columns defined by aXRef and chngRowid. Return true if it does
132002** and false if not. This is an optimization. False-positives are a
132003** performance degradation, but false-negatives can result in a corrupt
132004** index and incorrect answers.
132005**
132006** aXRef[j] will be non-negative if column j of the original table is
132007** being updated. chngRowid will be true if the rowid of the table is
132008** being updated.
132009*/
132010static int indexColumnIsBeingUpdated(
132011 Index *pIdx, /* The index to check */
132012 int iCol, /* Which column of the index to check */
132013 int *aXRef, /* aXRef[j]>=0 if column j is being updated */
132014 int chngRowid /* true if the rowid is being updated */
132015){
132016 i16 iIdxCol = pIdx->aiColumn[iCol];
132017 assert( iIdxCol!=XN_ROWID ); /* Cannot index rowid */
132018 if( iIdxCol>=0 ){
132019 return aXRef[iIdxCol]>=0;
132020 }
132021 assert( iIdxCol==XN_EXPR );
132022 assert( pIdx->aColExpr!=0 );
132023 assert( pIdx->aColExpr->a[iCol].pExpr!=0 );
132024 return sqlite3ExprReferencesUpdatedColumn(pIdx->aColExpr->a[iCol].pExpr,
132025 aXRef,chngRowid);
132026}
132027
132028/*
132029** Check to see if index pIdx is a partial index whose conditional
132030** expression might change values due to an UPDATE. Return true if
132031** the index is subject to change and false if the index is guaranteed
132032** to be unchanged. This is an optimization. False-positives are a
132033** performance degradation, but false-negatives can result in a corrupt
132034** index and incorrect answers.
132035**
132036** aXRef[j] will be non-negative if column j of the original table is
132037** being updated. chngRowid will be true if the rowid of the table is
132038** being updated.
132039*/
132040static int indexWhereClauseMightChange(
132041 Index *pIdx, /* The index to check */
132042 int *aXRef, /* aXRef[j]>=0 if column j is being updated */
132043 int chngRowid /* true if the rowid is being updated */
132044){
132045 if( pIdx->pPartIdxWhere==0 ) return 0;
132046 return sqlite3ExprReferencesUpdatedColumn(pIdx->pPartIdxWhere,
132047 aXRef, chngRowid);
132048}
132049
132050/*
132051** Process an UPDATE statement.
132052**
132053** UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
132054** \_______/ \________/ \______/ \________________/
132055* onError pTabList pChanges pWhere
132056*/
132057SQLITE_PRIVATE void sqlite3Update(
132058 Parse *pParse, /* The parser context */
132059 SrcList *pTabList, /* The table in which we should change things */
132060 ExprList *pChanges, /* Things to be changed */
132061 Expr *pWhere, /* The WHERE clause. May be null */
132062 int onError, /* How to handle constraint errors */
132063 ExprList *pOrderBy, /* ORDER BY clause. May be null */
132064 Expr *pLimit, /* LIMIT clause. May be null */
132065 Upsert *pUpsert /* ON CONFLICT clause, or null */
132066){
132067 int i, j; /* Loop counters */
132068 Table *pTab; /* The table to be updated */
132069 int addrTop = 0; /* VDBE instruction address of the start of the loop */
132070 WhereInfo *pWInfo; /* Information about the WHERE clause */
132071 Vdbe *v; /* The virtual database engine */
132072 Index *pIdx; /* For looping over indices */
132073 Index *pPk; /* The PRIMARY KEY index for WITHOUT ROWID tables */
132074 int nIdx; /* Number of indices that need updating */
132075 int iBaseCur; /* Base cursor number */
132076 int iDataCur; /* Cursor for the canonical data btree */
132077 int iIdxCur; /* Cursor for the first index */
132078 sqlite3 *db; /* The database structure */
132079 int *aRegIdx = 0; /* First register in array assigned to each index */
132080 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
132081 ** an expression for the i-th column of the table.
132082 ** aXRef[i]==-1 if the i-th column is not changed. */
132083 u8 *aToOpen; /* 1 for tables and indices to be opened */
132084 u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */
132085 u8 chngRowid; /* Rowid changed in a normal table */
132086 u8 chngKey; /* Either chngPk or chngRowid */
132087 Expr *pRowidExpr = 0; /* Expression defining the new record number */
132088 AuthContext sContext; /* The authorization context */
132089 NameContext sNC; /* The name-context to resolve expressions in */
132090 int iDb; /* Database containing the table being updated */
132091 int eOnePass; /* ONEPASS_XXX value from where.c */
132092 int hasFK; /* True if foreign key processing is required */
132093 int labelBreak; /* Jump here to break out of UPDATE loop */
132094 int labelContinue; /* Jump here to continue next step of UPDATE loop */
132095 int flags; /* Flags for sqlite3WhereBegin() */
132096
132097#ifndef SQLITE_OMIT_TRIGGER
132098 int isView; /* True when updating a view (INSTEAD OF trigger) */
132099 Trigger *pTrigger; /* List of triggers on pTab, if required */
132100 int tmask; /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
132101#endif
132102 int newmask; /* Mask of NEW.* columns accessed by BEFORE triggers */
132103 int iEph = 0; /* Ephemeral table holding all primary key values */
132104 int nKey = 0; /* Number of elements in regKey for WITHOUT ROWID */
132105 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
132106 int addrOpen = 0; /* Address of OP_OpenEphemeral */
132107 int iPk = 0; /* First of nPk cells holding PRIMARY KEY value */
132108 i16 nPk = 0; /* Number of components of the PRIMARY KEY */
132109 int bReplace = 0; /* True if REPLACE conflict resolution might happen */
132110
132111 /* Register Allocations */
132112 int regRowCount = 0; /* A count of rows changed */
132113 int regOldRowid = 0; /* The old rowid */
132114 int regNewRowid = 0; /* The new rowid */
132115 int regNew = 0; /* Content of the NEW.* table in triggers */
132116 int regOld = 0; /* Content of OLD.* table in triggers */
132117 int regRowSet = 0; /* Rowset of rows to be updated */
132118 int regKey = 0; /* composite PRIMARY KEY value */
132119
132120 memset(&sContext, 0, sizeof(sContext));
132121 db = pParse->db;
132122 if( pParse->nErr || db->mallocFailed ){
132123 goto update_cleanup;
132124 }
132125 assert( pTabList->nSrc==1 );
132126
132127 /* Locate the table which we want to update.
132128 */
132129 pTab = sqlite3SrcListLookup(pParse, pTabList);
132130 if( pTab==0 ) goto update_cleanup;
132131 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
132132
132133 /* Figure out if we have any triggers and if the table being
132134 ** updated is a view.
132135 */
132136#ifndef SQLITE_OMIT_TRIGGER
132137 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, &tmask);
132138 isView = pTab->pSelect!=0;
132139 assert( pTrigger || tmask==0 );
132140#else
132141# define pTrigger 0
132142# define isView 0
132143# define tmask 0
132144#endif
132145#ifdef SQLITE_OMIT_VIEW
132146# undef isView
132147# define isView 0
132148#endif
132149
132150#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
132151 if( !isView ){
132152 pWhere = sqlite3LimitWhere(
132153 pParse, pTabList, pWhere, pOrderBy, pLimit, "UPDATE"
132154 );
132155 pOrderBy = 0;
132156 pLimit = 0;
132157 }
132158#endif
132159
132160 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
132161 goto update_cleanup;
132162 }
132163 if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
132164 goto update_cleanup;
132165 }
132166
132167 /* Allocate a cursors for the main database table and for all indices.
132168 ** The index cursors might not be used, but if they are used they
132169 ** need to occur right after the database cursor. So go ahead and
132170 ** allocate enough space, just in case.
132171 */
132172 iBaseCur = iDataCur = pParse->nTab++;
132173 iIdxCur = iDataCur+1;
132174 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
132175 testcase( pPk!=0 && pPk!=pTab->pIndex );
132176 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
132177 if( pPk==pIdx ){
132178 iDataCur = pParse->nTab;
132179 }
132180 pParse->nTab++;
132181 }
132182 if( pUpsert ){
132183 /* On an UPSERT, reuse the same cursors already opened by INSERT */
132184 iDataCur = pUpsert->iDataCur;
132185 iIdxCur = pUpsert->iIdxCur;
132186 pParse->nTab = iBaseCur;
132187 }
132188 pTabList->a[0].iCursor = iDataCur;
132189
132190 /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].
132191 ** Initialize aXRef[] and aToOpen[] to their default values.
132192 */
132193 aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 );
132194 if( aXRef==0 ) goto update_cleanup;
132195 aRegIdx = aXRef+pTab->nCol;
132196 aToOpen = (u8*)(aRegIdx+nIdx);
132197 memset(aToOpen, 1, nIdx+1);
132198 aToOpen[nIdx+1] = 0;
132199 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
132200
132201 /* Initialize the name-context */
132202 memset(&sNC, 0, sizeof(sNC));
132203 sNC.pParse = pParse;
132204 sNC.pSrcList = pTabList;
132205 sNC.uNC.pUpsert = pUpsert;
132206 sNC.ncFlags = NC_UUpsert;
132207
132208 /* Resolve the column names in all the expressions of the
132209 ** of the UPDATE statement. Also find the column index
132210 ** for each column to be updated in the pChanges array. For each
132211 ** column to be updated, make sure we have authorization to change
132212 ** that column.
132213 */
132214 chngRowid = chngPk = 0;
132215 for(i=0; i<pChanges->nExpr; i++){
132216 if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
132217 goto update_cleanup;
132218 }
132219 for(j=0; j<pTab->nCol; j++){
132220 if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
132221 if( j==pTab->iPKey ){
132222 chngRowid = 1;
132223 pRowidExpr = pChanges->a[i].pExpr;
132224 }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){
132225 chngPk = 1;
132226 }
132227 aXRef[j] = i;
132228 break;
132229 }
132230 }
132231 if( j>=pTab->nCol ){
132232 if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zName) ){
132233 j = -1;
132234 chngRowid = 1;
132235 pRowidExpr = pChanges->a[i].pExpr;
132236 }else{
132237 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
132238 pParse->checkSchema = 1;
132239 goto update_cleanup;
132240 }
132241 }
132242#ifndef SQLITE_OMIT_AUTHORIZATION
132243 {
132244 int rc;
132245 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
132246 j<0 ? "ROWID" : pTab->aCol[j].zName,
132247 db->aDb[iDb].zDbSName);
132248 if( rc==SQLITE_DENY ){
132249 goto update_cleanup;
132250 }else if( rc==SQLITE_IGNORE ){
132251 aXRef[j] = -1;
132252 }
132253 }
132254#endif
132255 }
132256 assert( (chngRowid & chngPk)==0 );
132257 assert( chngRowid==0 || chngRowid==1 );
132258 assert( chngPk==0 || chngPk==1 );
132259 chngKey = chngRowid + chngPk;
132260
132261 /* The SET expressions are not actually used inside the WHERE loop.
132262 ** So reset the colUsed mask. Unless this is a virtual table. In that
132263 ** case, set all bits of the colUsed mask (to ensure that the virtual
132264 ** table implementation makes all columns available).
132265 */
132266 pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0;
132267
132268 hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);
132269
132270 /* There is one entry in the aRegIdx[] array for each index on the table
132271 ** being updated. Fill in aRegIdx[] with a register number that will hold
132272 ** the key for accessing each index.
132273 */
132274 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
132275 int reg;
132276 if( chngKey || hasFK>1 || pIdx==pPk
132277 || indexWhereClauseMightChange(pIdx,aXRef,chngRowid)
132278 ){
132279 reg = ++pParse->nMem;
132280 pParse->nMem += pIdx->nColumn;
132281 }else{
132282 reg = 0;
132283 for(i=0; i<pIdx->nKeyCol; i++){
132284 if( indexColumnIsBeingUpdated(pIdx, i, aXRef, chngRowid) ){
132285 reg = ++pParse->nMem;
132286 pParse->nMem += pIdx->nColumn;
132287 if( (onError==OE_Replace)
132288 || (onError==OE_Default && pIdx->onError==OE_Replace)
132289 ){
132290 bReplace = 1;
132291 }
132292 break;
132293 }
132294 }
132295 }
132296 if( reg==0 ) aToOpen[j+1] = 0;
132297 aRegIdx[j] = reg;
132298 }
132299 if( bReplace ){
132300 /* If REPLACE conflict resolution might be invoked, open cursors on all
132301 ** indexes in case they are needed to delete records. */
132302 memset(aToOpen, 1, nIdx+1);
132303 }
132304
132305 /* Begin generating code. */
132306 v = sqlite3GetVdbe(pParse);
132307 if( v==0 ) goto update_cleanup;
132308 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
132309 sqlite3BeginWriteOperation(pParse, pTrigger || hasFK, iDb);
132310
132311 /* Allocate required registers. */
132312 if( !IsVirtual(pTab) ){
132313 regRowSet = ++pParse->nMem;
132314 regOldRowid = regNewRowid = ++pParse->nMem;
132315 if( chngPk || pTrigger || hasFK ){
132316 regOld = pParse->nMem + 1;
132317 pParse->nMem += pTab->nCol;
132318 }
132319 if( chngKey || pTrigger || hasFK ){
132320 regNewRowid = ++pParse->nMem;
132321 }
132322 regNew = pParse->nMem + 1;
132323 pParse->nMem += pTab->nCol;
132324 }
132325
132326 /* Start the view context. */
132327 if( isView ){
132328 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
132329 }
132330
132331 /* If we are trying to update a view, realize that view into
132332 ** an ephemeral table.
132333 */
132334#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
132335 if( isView ){
132336 sqlite3MaterializeView(pParse, pTab,
132337 pWhere, pOrderBy, pLimit, iDataCur
132338 );
132339 pOrderBy = 0;
132340 pLimit = 0;
132341 }
132342#endif
132343
132344 /* Resolve the column names in all the expressions in the
132345 ** WHERE clause.
132346 */
132347 if( sqlite3ResolveExprNames(&sNC, pWhere) ){
132348 goto update_cleanup;
132349 }
132350
132351#ifndef SQLITE_OMIT_VIRTUALTABLE
132352 /* Virtual tables must be handled separately */
132353 if( IsVirtual(pTab) ){
132354 updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
132355 pWhere, onError);
132356 goto update_cleanup;
132357 }
132358#endif
132359
132360 /* Jump to labelBreak to abandon further processing of this UPDATE */
132361 labelContinue = labelBreak = sqlite3VdbeMakeLabel(v);
132362
132363 /* Not an UPSERT. Normal processing. Begin by
132364 ** initialize the count of updated rows */
132365 if( (db->flags&SQLITE_CountRows)!=0
132366 && !pParse->pTriggerTab
132367 && !pParse->nested
132368 && pUpsert==0
132369 ){
132370 regRowCount = ++pParse->nMem;
132371 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
132372 }
132373
132374 if( HasRowid(pTab) ){
132375 sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
132376 }else{
132377 assert( pPk!=0 );
132378 nPk = pPk->nKeyCol;
132379 iPk = pParse->nMem+1;
132380 pParse->nMem += nPk;
132381 regKey = ++pParse->nMem;
132382 if( pUpsert==0 ){
132383 iEph = pParse->nTab++;
132384 sqlite3VdbeAddOp3(v, OP_Null, 0, iPk, iPk+nPk-1);
132385 addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk);
132386 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
132387 }
132388 }
132389
132390 if( pUpsert ){
132391 /* If this is an UPSERT, then all cursors have already been opened by
132392 ** the outer INSERT and the data cursor should be pointing at the row
132393 ** that is to be updated. So bypass the code that searches for the
132394 ** row(s) to be updated.
132395 */
132396 pWInfo = 0;
132397 eOnePass = ONEPASS_SINGLE;
132398 sqlite3ExprIfFalse(pParse, pWhere, labelBreak, SQLITE_JUMPIFNULL);
132399 }else{
132400 /* Begin the database scan.
132401 **
132402 ** Do not consider a single-pass strategy for a multi-row update if
132403 ** there are any triggers or foreign keys to process, or rows may
132404 ** be deleted as a result of REPLACE conflict handling. Any of these
132405 ** things might disturb a cursor being used to scan through the table
132406 ** or index, causing a single-pass approach to malfunction. */
132407 flags = WHERE_ONEPASS_DESIRED|WHERE_SEEK_UNIQ_TABLE;
132408 if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
132409 flags |= WHERE_ONEPASS_MULTIROW;
132410 }
132411 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, flags, iIdxCur);
132412 if( pWInfo==0 ) goto update_cleanup;
132413
132414 /* A one-pass strategy that might update more than one row may not
132415 ** be used if any column of the index used for the scan is being
132416 ** updated. Otherwise, if there is an index on "b", statements like
132417 ** the following could create an infinite loop:
132418 **
132419 ** UPDATE t1 SET b=b+1 WHERE b>?
132420 **
132421 ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI
132422 ** strategy that uses an index for which one or more columns are being
132423 ** updated. */
132424 eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
132425 if( eOnePass!=ONEPASS_SINGLE ){
132426 sqlite3MultiWrite(pParse);
132427 if( eOnePass==ONEPASS_MULTI ){
132428 int iCur = aiCurOnePass[1];
132429 if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
132430 eOnePass = ONEPASS_OFF;
132431 }
132432 assert( iCur!=iDataCur || !HasRowid(pTab) );
132433 }
132434 }
132435 }
132436
132437 if( HasRowid(pTab) ){
132438 /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF
132439 ** mode, write the rowid into the FIFO. In either of the one-pass modes,
132440 ** leave it in register regOldRowid. */
132441 sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid);
132442 if( eOnePass==ONEPASS_OFF ){
132443 sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
132444 }
132445 }else{
132446 /* Read the PK of the current row into an array of registers. In
132447 ** ONEPASS_OFF mode, serialize the array into a record and store it in
132448 ** the ephemeral table. Or, in ONEPASS_SINGLE or MULTI mode, change
132449 ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table
132450 ** is not required) and leave the PK fields in the array of registers. */
132451 for(i=0; i<nPk; i++){
132452 assert( pPk->aiColumn[i]>=0 );
132453 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur,pPk->aiColumn[i],iPk+i);
132454 }
132455 if( eOnePass ){
132456 if( addrOpen ) sqlite3VdbeChangeToNoop(v, addrOpen);
132457 nKey = nPk;
132458 regKey = iPk;
132459 }else{
132460 sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey,
132461 sqlite3IndexAffinityStr(db, pPk), nPk);
132462 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEph, regKey, iPk, nPk);
132463 }
132464 }
132465
132466 if( pUpsert==0 ){
132467 if( eOnePass!=ONEPASS_MULTI ){
132468 sqlite3WhereEnd(pWInfo);
132469 }
132470
132471 if( !isView ){
132472 int addrOnce = 0;
132473
132474 /* Open every index that needs updating. */
132475 if( eOnePass!=ONEPASS_OFF ){
132476 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
132477 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
132478 }
132479
132480 if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
132481 addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
132482 }
132483 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, iBaseCur,
132484 aToOpen, 0, 0);
132485 if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
132486 }
132487
132488 /* Top of the update loop */
132489 if( eOnePass!=ONEPASS_OFF ){
132490 if( !isView && aiCurOnePass[0]!=iDataCur && aiCurOnePass[1]!=iDataCur ){
132491 assert( pPk );
132492 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey,nKey);
132493 VdbeCoverage(v);
132494 }
132495 if( eOnePass!=ONEPASS_SINGLE ){
132496 labelContinue = sqlite3VdbeMakeLabel(v);
132497 }
132498 sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
132499 VdbeCoverageIf(v, pPk==0);
132500 VdbeCoverageIf(v, pPk!=0);
132501 }else if( pPk ){
132502 labelContinue = sqlite3VdbeMakeLabel(v);
132503 sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v);
132504 addrTop = sqlite3VdbeAddOp2(v, OP_RowData, iEph, regKey);
132505 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey, 0);
132506 VdbeCoverage(v);
132507 }else{
132508 labelContinue = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet,labelBreak,
132509 regOldRowid);
132510 VdbeCoverage(v);
132511 sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
132512 VdbeCoverage(v);
132513 }
132514 }
132515
132516 /* If the rowid value will change, set register regNewRowid to
132517 ** contain the new value. If the rowid is not being modified,
132518 ** then regNewRowid is the same register as regOldRowid, which is
132519 ** already populated. */
132520 assert( chngKey || pTrigger || hasFK || regOldRowid==regNewRowid );
132521 if( chngRowid ){
132522 sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
132523 sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid); VdbeCoverage(v);
132524 }
132525
132526 /* Compute the old pre-UPDATE content of the row being changed, if that
132527 ** information is needed */
132528 if( chngPk || hasFK || pTrigger ){
132529 u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
132530 oldmask |= sqlite3TriggerColmask(pParse,
132531 pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError
132532 );
132533 for(i=0; i<pTab->nCol; i++){
132534 if( oldmask==0xffffffff
132535 || (i<32 && (oldmask & MASKBIT32(i))!=0)
132536 || (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0
132537 ){
132538 testcase( oldmask!=0xffffffff && i==31 );
132539 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regOld+i);
132540 }else{
132541 sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i);
132542 }
132543 }
132544 if( chngRowid==0 && pPk==0 ){
132545 sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid);
132546 }
132547 }
132548
132549 /* Populate the array of registers beginning at regNew with the new
132550 ** row data. This array is used to check constants, create the new
132551 ** table and index records, and as the values for any new.* references
132552 ** made by triggers.
132553 **
132554 ** If there are one or more BEFORE triggers, then do not populate the
132555 ** registers associated with columns that are (a) not modified by
132556 ** this UPDATE statement and (b) not accessed by new.* references. The
132557 ** values for registers not modified by the UPDATE must be reloaded from
132558 ** the database after the BEFORE triggers are fired anyway (as the trigger
132559 ** may have modified them). So not loading those that are not going to
132560 ** be used eliminates some redundant opcodes.
132561 */
132562 newmask = sqlite3TriggerColmask(
132563 pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError
132564 );
132565 for(i=0; i<pTab->nCol; i++){
132566 if( i==pTab->iPKey ){
132567 sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
132568 }else{
132569 j = aXRef[i];
132570 if( j>=0 ){
132571 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i);
132572 }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask & MASKBIT32(i)) ){
132573 /* This branch loads the value of a column that will not be changed
132574 ** into a register. This is done if there are no BEFORE triggers, or
132575 ** if there are one or more BEFORE triggers that use this value via
132576 ** a new.* reference in a trigger program.
132577 */
132578 testcase( i==31 );
132579 testcase( i==32 );
132580 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i);
132581 }else{
132582 sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
132583 }
132584 }
132585 }
132586
132587 /* Fire any BEFORE UPDATE triggers. This happens before constraints are
132588 ** verified. One could argue that this is wrong.
132589 */
132590 if( tmask&TRIGGER_BEFORE ){
132591 sqlite3TableAffinity(v, pTab, regNew);
132592 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
132593 TRIGGER_BEFORE, pTab, regOldRowid, onError, labelContinue);
132594
132595 /* The row-trigger may have deleted the row being updated. In this
132596 ** case, jump to the next row. No updates or AFTER triggers are
132597 ** required. This behavior - what happens when the row being updated
132598 ** is deleted or renamed by a BEFORE trigger - is left undefined in the
132599 ** documentation.
132600 */
132601 if( pPk ){
132602 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue,regKey,nKey);
132603 VdbeCoverage(v);
132604 }else{
132605 sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
132606 VdbeCoverage(v);
132607 }
132608
132609 /* After-BEFORE-trigger-reload-loop:
132610 ** If it did not delete it, the BEFORE trigger may still have modified
132611 ** some of the columns of the row being updated. Load the values for
132612 ** all columns not modified by the update statement into their registers
132613 ** in case this has happened. Only unmodified columns are reloaded.
132614 ** The values computed for modified columns use the values before the
132615 ** BEFORE trigger runs. See test case trigger1-18.0 (added 2018-04-26)
132616 ** for an example.
132617 */
132618 for(i=0; i<pTab->nCol; i++){
132619 if( aXRef[i]<0 && i!=pTab->iPKey ){
132620 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i);
132621 }
132622 }
132623 }
132624
132625 if( !isView ){
132626 int addr1 = 0; /* Address of jump instruction */
132627
132628 /* Do constraint checks. */
132629 assert( regOldRowid>0 );
132630 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
132631 regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace,
132632 aXRef, 0);
132633
132634 /* Do FK constraint checks. */
132635 if( hasFK ){
132636 sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngKey);
132637 }
132638
132639 /* Delete the index entries associated with the current record. */
132640 if( bReplace || chngKey ){
132641 if( pPk ){
132642 addr1 = sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, 0, regKey, nKey);
132643 }else{
132644 addr1 = sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, 0, regOldRowid);
132645 }
132646 VdbeCoverageNeverTaken(v);
132647 }
132648 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, -1);
132649
132650 /* If changing the rowid value, or if there are foreign key constraints
132651 ** to process, delete the old record. Otherwise, add a noop OP_Delete
132652 ** to invoke the pre-update hook.
132653 **
132654 ** That (regNew==regnewRowid+1) is true is also important for the
132655 ** pre-update hook. If the caller invokes preupdate_new(), the returned
132656 ** value is copied from memory cell (regNewRowid+1+iCol), where iCol
132657 ** is the column index supplied by the user.
132658 */
132659 assert( regNew==regNewRowid+1 );
132660#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
132661 sqlite3VdbeAddOp3(v, OP_Delete, iDataCur,
132662 OPFLAG_ISUPDATE | ((hasFK>1 || chngKey) ? 0 : OPFLAG_ISNOOP),
132663 regNewRowid
132664 );
132665 if( eOnePass==ONEPASS_MULTI ){
132666 assert( hasFK==0 && chngKey==0 );
132667 sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION);
132668 }
132669 if( !pParse->nested ){
132670 sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
132671 }
132672#else
132673 if( hasFK>1 || chngKey ){
132674 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0);
132675 }
132676#endif
132677 if( bReplace || chngKey ){
132678 sqlite3VdbeJumpHere(v, addr1);
132679 }
132680
132681 if( hasFK ){
132682 sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey);
132683 }
132684
132685 /* Insert the new index entries and the new record. */
132686 sqlite3CompleteInsertion(
132687 pParse, pTab, iDataCur, iIdxCur, regNewRowid, aRegIdx,
132688 OPFLAG_ISUPDATE | (eOnePass==ONEPASS_MULTI ? OPFLAG_SAVEPOSITION : 0),
132689 0, 0
132690 );
132691
132692 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
132693 ** handle rows (possibly in other tables) that refer via a foreign key
132694 ** to the row just updated. */
132695 if( hasFK ){
132696 sqlite3FkActions(pParse, pTab, pChanges, regOldRowid, aXRef, chngKey);
132697 }
132698 }
132699
132700 /* Increment the row counter
132701 */
132702 if( regRowCount ){
132703 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
132704 }
132705
132706 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
132707 TRIGGER_AFTER, pTab, regOldRowid, onError, labelContinue);
132708
132709 /* Repeat the above with the next record to be updated, until
132710 ** all record selected by the WHERE clause have been updated.
132711 */
132712 if( eOnePass==ONEPASS_SINGLE ){
132713 /* Nothing to do at end-of-loop for a single-pass */
132714 }else if( eOnePass==ONEPASS_MULTI ){
132715 sqlite3VdbeResolveLabel(v, labelContinue);
132716 sqlite3WhereEnd(pWInfo);
132717 }else if( pPk ){
132718 sqlite3VdbeResolveLabel(v, labelContinue);
132719 sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop); VdbeCoverage(v);
132720 }else{
132721 sqlite3VdbeGoto(v, labelContinue);
132722 }
132723 sqlite3VdbeResolveLabel(v, labelBreak);
132724
132725 /* Update the sqlite_sequence table by storing the content of the
132726 ** maximum rowid counter values recorded while inserting into
132727 ** autoincrement tables.
132728 */
132729 if( pParse->nested==0 && pParse->pTriggerTab==0 && pUpsert==0 ){
132730 sqlite3AutoincrementEnd(pParse);
132731 }
132732
132733 /*
132734 ** Return the number of rows that were changed, if we are tracking
132735 ** that information.
132736 */
132737 if( regRowCount ){
132738 sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
132739 sqlite3VdbeSetNumCols(v, 1);
132740 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
132741 }
132742
132743update_cleanup:
132744 sqlite3AuthContextPop(&sContext);
132745 sqlite3DbFree(db, aXRef); /* Also frees aRegIdx[] and aToOpen[] */
132746 sqlite3SrcListDelete(db, pTabList);
132747 sqlite3ExprListDelete(db, pChanges);
132748 sqlite3ExprDelete(db, pWhere);
132749#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
132750 sqlite3ExprListDelete(db, pOrderBy);
132751 sqlite3ExprDelete(db, pLimit);
132752#endif
132753 return;
132754}
132755/* Make sure "isView" and other macros defined above are undefined. Otherwise
132756** they may interfere with compilation of other functions in this file
132757** (or in another file, if this file becomes part of the amalgamation). */
132758#ifdef isView
132759 #undef isView
132760#endif
132761#ifdef pTrigger
132762 #undef pTrigger
132763#endif
132764
132765#ifndef SQLITE_OMIT_VIRTUALTABLE
132766/*
132767** Generate code for an UPDATE of a virtual table.
132768**
132769** There are two possible strategies - the default and the special
132770** "onepass" strategy. Onepass is only used if the virtual table
132771** implementation indicates that pWhere may match at most one row.
132772**
132773** The default strategy is to create an ephemeral table that contains
132774** for each row to be changed:
132775**
132776** (A) The original rowid of that row.
132777** (B) The revised rowid for the row.
132778** (C) The content of every column in the row.
132779**
132780** Then loop through the contents of this ephemeral table executing a
132781** VUpdate for each row. When finished, drop the ephemeral table.
132782**
132783** The "onepass" strategy does not use an ephemeral table. Instead, it
132784** stores the same values (A, B and C above) in a register array and
132785** makes a single invocation of VUpdate.
132786*/
132787static void updateVirtualTable(
132788 Parse *pParse, /* The parsing context */
132789 SrcList *pSrc, /* The virtual table to be modified */
132790 Table *pTab, /* The virtual table */
132791 ExprList *pChanges, /* The columns to change in the UPDATE statement */
132792 Expr *pRowid, /* Expression used to recompute the rowid */
132793 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
132794 Expr *pWhere, /* WHERE clause of the UPDATE statement */
132795 int onError /* ON CONFLICT strategy */
132796){
132797 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
132798 int ephemTab; /* Table holding the result of the SELECT */
132799 int i; /* Loop counter */
132800 sqlite3 *db = pParse->db; /* Database connection */
132801 const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
132802 WhereInfo *pWInfo;
132803 int nArg = 2 + pTab->nCol; /* Number of arguments to VUpdate */
132804 int regArg; /* First register in VUpdate arg array */
132805 int regRec; /* Register in which to assemble record */
132806 int regRowid; /* Register for ephem table rowid */
132807 int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */
132808 int aDummy[2]; /* Unused arg for sqlite3WhereOkOnePass() */
132809 int eOnePass; /* True to use onepass strategy */
132810 int addr; /* Address of OP_OpenEphemeral */
132811
132812 /* Allocate nArg registers in which to gather the arguments for VUpdate. Then
132813 ** create and open the ephemeral table in which the records created from
132814 ** these arguments will be temporarily stored. */
132815 assert( v );
132816 ephemTab = pParse->nTab++;
132817 addr= sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, nArg);
132818 regArg = pParse->nMem + 1;
132819 pParse->nMem += nArg;
132820 regRec = ++pParse->nMem;
132821 regRowid = ++pParse->nMem;
132822
132823 /* Start scanning the virtual table */
132824 pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0,0,WHERE_ONEPASS_DESIRED,0);
132825 if( pWInfo==0 ) return;
132826
132827 /* Populate the argument registers. */
132828 for(i=0; i<pTab->nCol; i++){
132829 if( aXRef[i]>=0 ){
132830 sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i);
132831 }else{
132832 sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, i, regArg+2+i);
132833 sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG);/* Enable sqlite3_vtab_nochange() */
132834 }
132835 }
132836 if( HasRowid(pTab) ){
132837 sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg);
132838 if( pRowid ){
132839 sqlite3ExprCode(pParse, pRowid, regArg+1);
132840 }else{
132841 sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg+1);
132842 }
132843 }else{
132844 Index *pPk; /* PRIMARY KEY index */
132845 i16 iPk; /* PRIMARY KEY column */
132846 pPk = sqlite3PrimaryKeyIndex(pTab);
132847 assert( pPk!=0 );
132848 assert( pPk->nKeyCol==1 );
132849 iPk = pPk->aiColumn[0];
132850 sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, iPk, regArg);
132851 sqlite3VdbeAddOp2(v, OP_SCopy, regArg+2+iPk, regArg+1);
132852 }
132853
132854 eOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy);
132855
132856 /* There is no ONEPASS_MULTI on virtual tables */
132857 assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
132858
132859 if( eOnePass ){
132860 /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
132861 ** above. */
132862 sqlite3VdbeChangeToNoop(v, addr);
132863 sqlite3VdbeAddOp1(v, OP_Close, iCsr);
132864 }else{
132865 /* Create a record from the argument register contents and insert it into
132866 ** the ephemeral table. */
132867 sqlite3MultiWrite(pParse);
132868 sqlite3VdbeAddOp3(v, OP_MakeRecord, regArg, nArg, regRec);
132869#ifdef SQLITE_DEBUG
132870 /* Signal an assert() within OP_MakeRecord that it is allowed to
132871 ** accept no-change records with serial_type 10 */
132872 sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG_MAGIC);
132873#endif
132874 sqlite3VdbeAddOp2(v, OP_NewRowid, ephemTab, regRowid);
132875 sqlite3VdbeAddOp3(v, OP_Insert, ephemTab, regRec, regRowid);
132876 }
132877
132878
132879 if( eOnePass==ONEPASS_OFF ){
132880 /* End the virtual table scan */
132881 sqlite3WhereEnd(pWInfo);
132882
132883 /* Begin scannning through the ephemeral table. */
132884 addr = sqlite3VdbeAddOp1(v, OP_Rewind, ephemTab); VdbeCoverage(v);
132885
132886 /* Extract arguments from the current row of the ephemeral table and
132887 ** invoke the VUpdate method. */
132888 for(i=0; i<nArg; i++){
132889 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i, regArg+i);
132890 }
132891 }
132892 sqlite3VtabMakeWritable(pParse, pTab);
132893 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, nArg, regArg, pVTab, P4_VTAB);
132894 sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
132895 sqlite3MayAbort(pParse);
132896
132897 /* End of the ephemeral table scan. Or, if using the onepass strategy,
132898 ** jump to here if the scan visited zero rows. */
132899 if( eOnePass==ONEPASS_OFF ){
132900 sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1); VdbeCoverage(v);
132901 sqlite3VdbeJumpHere(v, addr);
132902 sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
132903 }else{
132904 sqlite3WhereEnd(pWInfo);
132905 }
132906}
132907#endif /* SQLITE_OMIT_VIRTUALTABLE */
132908
132909/************** End of update.c **********************************************/
132910/************** Begin file upsert.c ******************************************/
132911/*
132912** 2018-04-12
132913**
132914** The author disclaims copyright to this source code. In place of
132915** a legal notice, here is a blessing:
132916**
132917** May you do good and not evil.
132918** May you find forgiveness for yourself and forgive others.
132919** May you share freely, never taking more than you give.
132920**
132921*************************************************************************
132922** This file contains code to implement various aspects of UPSERT
132923** processing and handling of the Upsert object.
132924*/
132925/* #include "sqliteInt.h" */
132926
132927#ifndef SQLITE_OMIT_UPSERT
132928/*
132929** Free a list of Upsert objects
132930*/
132931SQLITE_PRIVATE void sqlite3UpsertDelete(sqlite3 *db, Upsert *p){
132932 if( p ){
132933 sqlite3ExprListDelete(db, p->pUpsertTarget);
132934 sqlite3ExprDelete(db, p->pUpsertTargetWhere);
132935 sqlite3ExprListDelete(db, p->pUpsertSet);
132936 sqlite3ExprDelete(db, p->pUpsertWhere);
132937 sqlite3DbFree(db, p);
132938 }
132939}
132940
132941/*
132942** Duplicate an Upsert object.
132943*/
132944SQLITE_PRIVATE Upsert *sqlite3UpsertDup(sqlite3 *db, Upsert *p){
132945 if( p==0 ) return 0;
132946 return sqlite3UpsertNew(db,
132947 sqlite3ExprListDup(db, p->pUpsertTarget, 0),
132948 sqlite3ExprDup(db, p->pUpsertTargetWhere, 0),
132949 sqlite3ExprListDup(db, p->pUpsertSet, 0),
132950 sqlite3ExprDup(db, p->pUpsertWhere, 0)
132951 );
132952}
132953
132954/*
132955** Create a new Upsert object.
132956*/
132957SQLITE_PRIVATE Upsert *sqlite3UpsertNew(
132958 sqlite3 *db, /* Determines which memory allocator to use */
132959 ExprList *pTarget, /* Target argument to ON CONFLICT, or NULL */
132960 Expr *pTargetWhere, /* Optional WHERE clause on the target */
132961 ExprList *pSet, /* UPDATE columns, or NULL for a DO NOTHING */
132962 Expr *pWhere /* WHERE clause for the ON CONFLICT UPDATE */
132963){
132964 Upsert *pNew;
132965 pNew = sqlite3DbMallocRaw(db, sizeof(Upsert));
132966 if( pNew==0 ){
132967 sqlite3ExprListDelete(db, pTarget);
132968 sqlite3ExprDelete(db, pTargetWhere);
132969 sqlite3ExprListDelete(db, pSet);
132970 sqlite3ExprDelete(db, pWhere);
132971 return 0;
132972 }else{
132973 pNew->pUpsertTarget = pTarget;
132974 pNew->pUpsertTargetWhere = pTargetWhere;
132975 pNew->pUpsertSet = pSet;
132976 pNew->pUpsertWhere = pWhere;
132977 pNew->pUpsertIdx = 0;
132978 }
132979 return pNew;
132980}
132981
132982/*
132983** Analyze the ON CONFLICT clause described by pUpsert. Resolve all
132984** symbols in the conflict-target.
132985**
132986** Return SQLITE_OK if everything works, or an error code is something
132987** is wrong.
132988*/
132989SQLITE_PRIVATE int sqlite3UpsertAnalyzeTarget(
132990 Parse *pParse, /* The parsing context */
132991 SrcList *pTabList, /* Table into which we are inserting */
132992 Upsert *pUpsert /* The ON CONFLICT clauses */
132993){
132994 Table *pTab; /* That table into which we are inserting */
132995 int rc; /* Result code */
132996 int iCursor; /* Cursor used by pTab */
132997 Index *pIdx; /* One of the indexes of pTab */
132998 ExprList *pTarget; /* The conflict-target clause */
132999 Expr *pTerm; /* One term of the conflict-target clause */
133000 NameContext sNC; /* Context for resolving symbolic names */
133001 Expr sCol[2]; /* Index column converted into an Expr */
133002
133003 assert( pTabList->nSrc==1 );
133004 assert( pTabList->a[0].pTab!=0 );
133005 assert( pUpsert!=0 );
133006 assert( pUpsert->pUpsertTarget!=0 );
133007
133008 /* Resolve all symbolic names in the conflict-target clause, which
133009 ** includes both the list of columns and the optional partial-index
133010 ** WHERE clause.
133011 */
133012 memset(&sNC, 0, sizeof(sNC));
133013 sNC.pParse = pParse;
133014 sNC.pSrcList = pTabList;
133015 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
133016 if( rc ) return rc;
133017 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
133018 if( rc ) return rc;
133019
133020 /* Check to see if the conflict target matches the rowid. */
133021 pTab = pTabList->a[0].pTab;
133022 pTarget = pUpsert->pUpsertTarget;
133023 iCursor = pTabList->a[0].iCursor;
133024 if( HasRowid(pTab)
133025 && pTarget->nExpr==1
133026 && (pTerm = pTarget->a[0].pExpr)->op==TK_COLUMN
133027 && pTerm->iColumn==XN_ROWID
133028 ){
133029 /* The conflict-target is the rowid of the primary table */
133030 assert( pUpsert->pUpsertIdx==0 );
133031 return SQLITE_OK;
133032 }
133033
133034 /* Initialize sCol[0..1] to be an expression parse tree for a
133035 ** single column of an index. The sCol[0] node will be the TK_COLLATE
133036 ** operator and sCol[1] will be the TK_COLUMN operator. Code below
133037 ** will populate the specific collation and column number values
133038 ** prior to comparing against the conflict-target expression.
133039 */
133040 memset(sCol, 0, sizeof(sCol));
133041 sCol[0].op = TK_COLLATE;
133042 sCol[0].pLeft = &sCol[1];
133043 sCol[1].op = TK_COLUMN;
133044 sCol[1].iTable = pTabList->a[0].iCursor;
133045
133046 /* Check for matches against other indexes */
133047 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
133048 int ii, jj, nn;
133049 if( !IsUniqueIndex(pIdx) ) continue;
133050 if( pTarget->nExpr!=pIdx->nKeyCol ) continue;
133051 if( pIdx->pPartIdxWhere ){
133052 if( pUpsert->pUpsertTargetWhere==0 ) continue;
133053 if( sqlite3ExprCompare(pParse, pUpsert->pUpsertTargetWhere,
133054 pIdx->pPartIdxWhere, iCursor)!=0 ){
133055 continue;
133056 }
133057 }
133058 nn = pIdx->nKeyCol;
133059 for(ii=0; ii<nn; ii++){
133060 Expr *pExpr;
133061 sCol[0].u.zToken = (char*)pIdx->azColl[ii];
133062 if( pIdx->aiColumn[ii]==XN_EXPR ){
133063 assert( pIdx->aColExpr!=0 );
133064 assert( pIdx->aColExpr->nExpr>ii );
133065 pExpr = pIdx->aColExpr->a[ii].pExpr;
133066 if( pExpr->op!=TK_COLLATE ){
133067 sCol[0].pLeft = pExpr;
133068 pExpr = &sCol[0];
133069 }
133070 }else{
133071 sCol[0].pLeft = &sCol[1];
133072 sCol[1].iColumn = pIdx->aiColumn[ii];
133073 pExpr = &sCol[0];
133074 }
133075 for(jj=0; jj<nn; jj++){
133076 if( sqlite3ExprCompare(pParse, pTarget->a[jj].pExpr, pExpr,iCursor)<2 ){
133077 break; /* Column ii of the index matches column jj of target */
133078 }
133079 }
133080 if( jj>=nn ){
133081 /* The target contains no match for column jj of the index */
133082 break;
133083 }
133084 }
133085 if( ii<nn ){
133086 /* Column ii of the index did not match any term of the conflict target.
133087 ** Continue the search with the next index. */
133088 continue;
133089 }
133090 pUpsert->pUpsertIdx = pIdx;
133091 return SQLITE_OK;
133092 }
133093 sqlite3ErrorMsg(pParse, "ON CONFLICT clause does not match any "
133094 "PRIMARY KEY or UNIQUE constraint");
133095 return SQLITE_ERROR;
133096}
133097
133098/*
133099** Generate bytecode that does an UPDATE as part of an upsert.
133100**
133101** If pIdx is NULL, then the UNIQUE constraint that failed was the IPK.
133102** In this case parameter iCur is a cursor open on the table b-tree that
133103** currently points to the conflicting table row. Otherwise, if pIdx
133104** is not NULL, then pIdx is the constraint that failed and iCur is a
133105** cursor points to the conflicting row.
133106*/
133107SQLITE_PRIVATE void sqlite3UpsertDoUpdate(
133108 Parse *pParse, /* The parsing and code-generating context */
133109 Upsert *pUpsert, /* The ON CONFLICT clause for the upsert */
133110 Table *pTab, /* The table being updated */
133111 Index *pIdx, /* The UNIQUE constraint that failed */
133112 int iCur /* Cursor for pIdx (or pTab if pIdx==NULL) */
133113){
133114 Vdbe *v = pParse->pVdbe;
133115 sqlite3 *db = pParse->db;
133116 SrcList *pSrc; /* FROM clause for the UPDATE */
133117 int iDataCur;
133118
133119 assert( v!=0 );
133120 assert( pUpsert!=0 );
133121 VdbeNoopComment((v, "Begin DO UPDATE of UPSERT"));
133122 iDataCur = pUpsert->iDataCur;
133123 if( pIdx && iCur!=iDataCur ){
133124 if( HasRowid(pTab) ){
133125 int regRowid = sqlite3GetTempReg(pParse);
133126 sqlite3VdbeAddOp2(v, OP_IdxRowid, iCur, regRowid);
133127 sqlite3VdbeAddOp3(v, OP_SeekRowid, iDataCur, 0, regRowid);
133128 VdbeCoverage(v);
133129 sqlite3ReleaseTempReg(pParse, regRowid);
133130 }else{
133131 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
133132 int nPk = pPk->nKeyCol;
133133 int iPk = pParse->nMem+1;
133134 int i;
133135 pParse->nMem += nPk;
133136 for(i=0; i<nPk; i++){
133137 int k;
133138 assert( pPk->aiColumn[i]>=0 );
133139 k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
133140 sqlite3VdbeAddOp3(v, OP_Column, iCur, k, iPk+i);
133141 VdbeComment((v, "%s.%s", pIdx->zName,
133142 pTab->aCol[pPk->aiColumn[i]].zName));
133143 }
133144 sqlite3VdbeVerifyAbortable(v, OE_Abort);
133145 i = sqlite3VdbeAddOp4Int(v, OP_Found, iDataCur, 0, iPk, nPk);
133146 VdbeCoverage(v);
133147 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CORRUPT, OE_Abort, 0,
133148 "corrupt database", P4_STATIC);
133149 sqlite3VdbeJumpHere(v, i);
133150 }
133151 }
133152 /* pUpsert does not own pUpsertSrc - the outer INSERT statement does. So
133153 ** we have to make a copy before passing it down into sqlite3Update() */
133154 pSrc = sqlite3SrcListDup(db, pUpsert->pUpsertSrc, 0);
133155 sqlite3Update(pParse, pSrc, pUpsert->pUpsertSet,
133156 pUpsert->pUpsertWhere, OE_Abort, 0, 0, pUpsert);
133157 pUpsert->pUpsertSet = 0; /* Will have been deleted by sqlite3Update() */
133158 pUpsert->pUpsertWhere = 0; /* Will have been deleted by sqlite3Update() */
133159 VdbeNoopComment((v, "End DO UPDATE of UPSERT"));
133160}
133161
133162#endif /* SQLITE_OMIT_UPSERT */
133163
133164/************** End of upsert.c **********************************************/
133165/************** Begin file vacuum.c ******************************************/
133166/*
133167** 2003 April 6
133168**
133169** The author disclaims copyright to this source code. In place of
133170** a legal notice, here is a blessing:
133171**
133172** May you do good and not evil.
133173** May you find forgiveness for yourself and forgive others.
133174** May you share freely, never taking more than you give.
133175**
133176*************************************************************************
133177** This file contains code used to implement the VACUUM command.
133178**
133179** Most of the code in this file may be omitted by defining the
133180** SQLITE_OMIT_VACUUM macro.
133181*/
133182/* #include "sqliteInt.h" */
133183/* #include "vdbeInt.h" */
133184
133185#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
133186
133187/*
133188** Execute zSql on database db.
133189**
133190** If zSql returns rows, then each row will have exactly one
133191** column. (This will only happen if zSql begins with "SELECT".)
133192** Take each row of result and call execSql() again recursively.
133193**
133194** The execSqlF() routine does the same thing, except it accepts
133195** a format string as its third argument
133196*/
133197static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
133198 sqlite3_stmt *pStmt;
133199 int rc;
133200
133201 /* printf("SQL: [%s]\n", zSql); fflush(stdout); */
133202 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
133203 if( rc!=SQLITE_OK ) return rc;
133204 while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
133205 const char *zSubSql = (const char*)sqlite3_column_text(pStmt,0);
133206 assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 );
133207 /* The secondary SQL must be one of CREATE TABLE, CREATE INDEX,
133208 ** or INSERT. Historically there have been attacks that first
133209 ** corrupt the sqlite_master.sql field with other kinds of statements
133210 ** then run VACUUM to get those statements to execute at inappropriate
133211 ** times. */
133212 if( zSubSql
133213 && (strncmp(zSubSql,"CRE",3)==0 || strncmp(zSubSql,"INS",3)==0)
133214 ){
133215 rc = execSql(db, pzErrMsg, zSubSql);
133216 if( rc!=SQLITE_OK ) break;
133217 }
133218 }
133219 assert( rc!=SQLITE_ROW );
133220 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
133221 if( rc ){
133222 sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
133223 }
133224 (void)sqlite3_finalize(pStmt);
133225 return rc;
133226}
133227static int execSqlF(sqlite3 *db, char **pzErrMsg, const char *zSql, ...){
133228 char *z;
133229 va_list ap;
133230 int rc;
133231 va_start(ap, zSql);
133232 z = sqlite3VMPrintf(db, zSql, ap);
133233 va_end(ap);
133234 if( z==0 ) return SQLITE_NOMEM;
133235 rc = execSql(db, pzErrMsg, z);
133236 sqlite3DbFree(db, z);
133237 return rc;
133238}
133239
133240/*
133241** The VACUUM command is used to clean up the database,
133242** collapse free space, etc. It is modelled after the VACUUM command
133243** in PostgreSQL. The VACUUM command works as follows:
133244**
133245** (1) Create a new transient database file
133246** (2) Copy all content from the database being vacuumed into
133247** the new transient database file
133248** (3) Copy content from the transient database back into the
133249** original database.
133250**
133251** The transient database requires temporary disk space approximately
133252** equal to the size of the original database. The copy operation of
133253** step (3) requires additional temporary disk space approximately equal
133254** to the size of the original database for the rollback journal.
133255** Hence, temporary disk space that is approximately 2x the size of the
133256** original database is required. Every page of the database is written
133257** approximately 3 times: Once for step (2) and twice for step (3).
133258** Two writes per page are required in step (3) because the original
133259** database content must be written into the rollback journal prior to
133260** overwriting the database with the vacuumed content.
133261**
133262** Only 1x temporary space and only 1x writes would be required if
133263** the copy of step (3) were replaced by deleting the original database
133264** and renaming the transient database as the original. But that will
133265** not work if other processes are attached to the original database.
133266** And a power loss in between deleting the original and renaming the
133267** transient would cause the database file to appear to be deleted
133268** following reboot.
133269*/
133270SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse, Token *pNm){
133271 Vdbe *v = sqlite3GetVdbe(pParse);
133272 int iDb = 0;
133273 if( v==0 ) return;
133274 if( pNm ){
133275#ifndef SQLITE_BUG_COMPATIBLE_20160819
133276 /* Default behavior: Report an error if the argument to VACUUM is
133277 ** not recognized */
133278 iDb = sqlite3TwoPartName(pParse, pNm, pNm, &pNm);
133279 if( iDb<0 ) return;
133280#else
133281 /* When SQLITE_BUG_COMPATIBLE_20160819 is defined, unrecognized arguments
133282 ** to VACUUM are silently ignored. This is a back-out of a bug fix that
133283 ** occurred on 2016-08-19 (https://www.sqlite.org/src/info/083f9e6270).
133284 ** The buggy behavior is required for binary compatibility with some
133285 ** legacy applications. */
133286 iDb = sqlite3FindDb(pParse->db, pNm);
133287 if( iDb<0 ) iDb = 0;
133288#endif
133289 }
133290 if( iDb!=1 ){
133291 sqlite3VdbeAddOp1(v, OP_Vacuum, iDb);
133292 sqlite3VdbeUsesBtree(v, iDb);
133293 }
133294 return;
133295}
133296
133297/*
133298** This routine implements the OP_Vacuum opcode of the VDBE.
133299*/
133300SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db, int iDb){
133301 int rc = SQLITE_OK; /* Return code from service routines */
133302 Btree *pMain; /* The database being vacuumed */
133303 Btree *pTemp; /* The temporary database we vacuum into */
133304 u16 saved_mDbFlags; /* Saved value of db->mDbFlags */
133305 u32 saved_flags; /* Saved value of db->flags */
133306 int saved_nChange; /* Saved value of db->nChange */
133307 int saved_nTotalChange; /* Saved value of db->nTotalChange */
133308 u8 saved_mTrace; /* Saved trace settings */
133309 Db *pDb = 0; /* Database to detach at end of vacuum */
133310 int isMemDb; /* True if vacuuming a :memory: database */
133311 int nRes; /* Bytes of reserved space at the end of each page */
133312 int nDb; /* Number of attached databases */
133313 const char *zDbMain; /* Schema name of database to vacuum */
133314
133315 if( !db->autoCommit ){
133316 sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
133317 return SQLITE_ERROR;
133318 }
133319 if( db->nVdbeActive>1 ){
133320 sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
133321 return SQLITE_ERROR;
133322 }
133323
133324 /* Save the current value of the database flags so that it can be
133325 ** restored before returning. Then set the writable-schema flag, and
133326 ** disable CHECK and foreign key constraints. */
133327 saved_flags = db->flags;
133328 saved_mDbFlags = db->mDbFlags;
133329 saved_nChange = db->nChange;
133330 saved_nTotalChange = db->nTotalChange;
133331 saved_mTrace = db->mTrace;
133332 db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
133333 db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum;
133334 db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder
133335 | SQLITE_Defensive | SQLITE_CountRows);
133336 db->mTrace = 0;
133337
133338 zDbMain = db->aDb[iDb].zDbSName;
133339 pMain = db->aDb[iDb].pBt;
133340 isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
133341
133342 /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
133343 ** can be set to 'off' for this file, as it is not recovered if a crash
133344 ** occurs anyway. The integrity of the database is maintained by a
133345 ** (possibly synchronous) transaction opened on the main database before
133346 ** sqlite3BtreeCopyFile() is called.
133347 **
133348 ** An optimisation would be to use a non-journaled pager.
133349 ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but
133350 ** that actually made the VACUUM run slower. Very little journalling
133351 ** actually occurs when doing a vacuum since the vacuum_db is initially
133352 ** empty. Only the journal header is written. Apparently it takes more
133353 ** time to parse and run the PRAGMA to turn journalling off than it does
133354 ** to write the journal header file.
133355 */
133356 nDb = db->nDb;
133357 rc = execSql(db, pzErrMsg, "ATTACH''AS vacuum_db");
133358 if( rc!=SQLITE_OK ) goto end_of_vacuum;
133359 assert( (db->nDb-1)==nDb );
133360 pDb = &db->aDb[nDb];
133361 assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
133362 pTemp = pDb->pBt;
133363
133364 /* The call to execSql() to attach the temp database has left the file
133365 ** locked (as there was more than one active statement when the transaction
133366 ** to read the schema was concluded. Unlock it here so that this doesn't
133367 ** cause problems for the call to BtreeSetPageSize() below. */
133368 sqlite3BtreeCommit(pTemp);
133369
133370 nRes = sqlite3BtreeGetOptimalReserve(pMain);
133371
133372 /* A VACUUM cannot change the pagesize of an encrypted database. */
133373#ifdef SQLITE_HAS_CODEC
133374 if( db->nextPagesize ){
133375 extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
133376 int nKey;
133377 char *zKey;
133378 sqlite3CodecGetKey(db, iDb, (void**)&zKey, &nKey);
133379 if( nKey ) db->nextPagesize = 0;
133380 }
133381#endif
133382
133383 sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
133384 sqlite3BtreeSetSpillSize(pTemp, sqlite3BtreeSetSpillSize(pMain,0));
133385 sqlite3BtreeSetPagerFlags(pTemp, PAGER_SYNCHRONOUS_OFF|PAGER_CACHESPILL);
133386
133387 /* Begin a transaction and take an exclusive lock on the main database
133388 ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
133389 ** to ensure that we do not try to change the page-size on a WAL database.
133390 */
133391 rc = execSql(db, pzErrMsg, "BEGIN");
133392 if( rc!=SQLITE_OK ) goto end_of_vacuum;
133393 rc = sqlite3BtreeBeginTrans(pMain, 2, 0);
133394 if( rc!=SQLITE_OK ) goto end_of_vacuum;
133395
133396 /* Do not attempt to change the page size for a WAL database */
133397 if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
133398 ==PAGER_JOURNALMODE_WAL ){
133399 db->nextPagesize = 0;
133400 }
133401
133402 if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
133403 || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
133404 || NEVER(db->mallocFailed)
133405 ){
133406 rc = SQLITE_NOMEM_BKPT;
133407 goto end_of_vacuum;
133408 }
133409
133410#ifndef SQLITE_OMIT_AUTOVACUUM
133411 sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
133412 sqlite3BtreeGetAutoVacuum(pMain));
133413#endif
133414
133415 /* Query the schema of the main database. Create a mirror schema
133416 ** in the temporary database.
133417 */
133418 db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
133419 rc = execSqlF(db, pzErrMsg,
133420 "SELECT sql FROM \"%w\".sqlite_master"
133421 " WHERE type='table'AND name<>'sqlite_sequence'"
133422 " AND coalesce(rootpage,1)>0",
133423 zDbMain
133424 );
133425 if( rc!=SQLITE_OK ) goto end_of_vacuum;
133426 rc = execSqlF(db, pzErrMsg,
133427 "SELECT sql FROM \"%w\".sqlite_master"
133428 " WHERE type='index'",
133429 zDbMain
133430 );
133431 if( rc!=SQLITE_OK ) goto end_of_vacuum;
133432 db->init.iDb = 0;
133433
133434 /* Loop through the tables in the main database. For each, do
133435 ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
133436 ** the contents to the temporary database.
133437 */
133438 rc = execSqlF(db, pzErrMsg,
133439 "SELECT'INSERT INTO vacuum_db.'||quote(name)"
133440 "||' SELECT*FROM\"%w\".'||quote(name)"
133441 "FROM vacuum_db.sqlite_master "
133442 "WHERE type='table'AND coalesce(rootpage,1)>0",
133443 zDbMain
133444 );
133445 assert( (db->mDbFlags & DBFLAG_Vacuum)!=0 );
133446 db->mDbFlags &= ~DBFLAG_Vacuum;
133447 if( rc!=SQLITE_OK ) goto end_of_vacuum;
133448
133449 /* Copy the triggers, views, and virtual tables from the main database
133450 ** over to the temporary database. None of these objects has any
133451 ** associated storage, so all we have to do is copy their entries
133452 ** from the SQLITE_MASTER table.
133453 */
133454 rc = execSqlF(db, pzErrMsg,
133455 "INSERT INTO vacuum_db.sqlite_master"
133456 " SELECT*FROM \"%w\".sqlite_master"
133457 " WHERE type IN('view','trigger')"
133458 " OR(type='table'AND rootpage=0)",
133459 zDbMain
133460 );
133461 if( rc ) goto end_of_vacuum;
133462
133463 /* At this point, there is a write transaction open on both the
133464 ** vacuum database and the main database. Assuming no error occurs,
133465 ** both transactions are closed by this block - the main database
133466 ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
133467 ** call to sqlite3BtreeCommit().
133468 */
133469 {
133470 u32 meta;
133471 int i;
133472
133473 /* This array determines which meta meta values are preserved in the
133474 ** vacuum. Even entries are the meta value number and odd entries
133475 ** are an increment to apply to the meta value after the vacuum.
133476 ** The increment is used to increase the schema cookie so that other
133477 ** connections to the same database will know to reread the schema.
133478 */
133479 static const unsigned char aCopy[] = {
133480 BTREE_SCHEMA_VERSION, 1, /* Add one to the old schema cookie */
133481 BTREE_DEFAULT_CACHE_SIZE, 0, /* Preserve the default page cache size */
133482 BTREE_TEXT_ENCODING, 0, /* Preserve the text encoding */
133483 BTREE_USER_VERSION, 0, /* Preserve the user version */
133484 BTREE_APPLICATION_ID, 0, /* Preserve the application id */
133485 };
133486
133487 assert( 1==sqlite3BtreeIsInTrans(pTemp) );
133488 assert( 1==sqlite3BtreeIsInTrans(pMain) );
133489
133490 /* Copy Btree meta values */
133491 for(i=0; i<ArraySize(aCopy); i+=2){
133492 /* GetMeta() and UpdateMeta() cannot fail in this context because
133493 ** we already have page 1 loaded into cache and marked dirty. */
133494 sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
133495 rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
133496 if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum;
133497 }
133498
133499 rc = sqlite3BtreeCopyFile(pMain, pTemp);
133500 if( rc!=SQLITE_OK ) goto end_of_vacuum;
133501 rc = sqlite3BtreeCommit(pTemp);
133502 if( rc!=SQLITE_OK ) goto end_of_vacuum;
133503#ifndef SQLITE_OMIT_AUTOVACUUM
133504 sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
133505#endif
133506 }
133507
133508 assert( rc==SQLITE_OK );
133509 rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
133510
133511end_of_vacuum:
133512 /* Restore the original value of db->flags */
133513 db->init.iDb = 0;
133514 db->mDbFlags = saved_mDbFlags;
133515 db->flags = saved_flags;
133516 db->nChange = saved_nChange;
133517 db->nTotalChange = saved_nTotalChange;
133518 db->mTrace = saved_mTrace;
133519 sqlite3BtreeSetPageSize(pMain, -1, -1, 1);
133520
133521 /* Currently there is an SQL level transaction open on the vacuum
133522 ** database. No locks are held on any other files (since the main file
133523 ** was committed at the btree level). So it safe to end the transaction
133524 ** by manually setting the autoCommit flag to true and detaching the
133525 ** vacuum database. The vacuum_db journal file is deleted when the pager
133526 ** is closed by the DETACH.
133527 */
133528 db->autoCommit = 1;
133529
133530 if( pDb ){
133531 sqlite3BtreeClose(pDb->pBt);
133532 pDb->pBt = 0;
133533 pDb->pSchema = 0;
133534 }
133535
133536 /* This both clears the schemas and reduces the size of the db->aDb[]
133537 ** array. */
133538 sqlite3ResetAllSchemasOfConnection(db);
133539
133540 return rc;
133541}
133542
133543#endif /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */
133544
133545/************** End of vacuum.c **********************************************/
133546/************** Begin file vtab.c ********************************************/
133547/*
133548** 2006 June 10
133549**
133550** The author disclaims copyright to this source code. In place of
133551** a legal notice, here is a blessing:
133552**
133553** May you do good and not evil.
133554** May you find forgiveness for yourself and forgive others.
133555** May you share freely, never taking more than you give.
133556**
133557*************************************************************************
133558** This file contains code used to help implement virtual tables.
133559*/
133560#ifndef SQLITE_OMIT_VIRTUALTABLE
133561/* #include "sqliteInt.h" */
133562
133563/*
133564** Before a virtual table xCreate() or xConnect() method is invoked, the
133565** sqlite3.pVtabCtx member variable is set to point to an instance of
133566** this struct allocated on the stack. It is used by the implementation of
133567** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
133568** are invoked only from within xCreate and xConnect methods.
133569*/
133570struct VtabCtx {
133571 VTable *pVTable; /* The virtual table being constructed */
133572 Table *pTab; /* The Table object to which the virtual table belongs */
133573 VtabCtx *pPrior; /* Parent context (if any) */
133574 int bDeclared; /* True after sqlite3_declare_vtab() is called */
133575};
133576
133577/*
133578** Construct and install a Module object for a virtual table. When this
133579** routine is called, it is guaranteed that all appropriate locks are held
133580** and the module is not already part of the connection.
133581*/
133582SQLITE_PRIVATE Module *sqlite3VtabCreateModule(
133583 sqlite3 *db, /* Database in which module is registered */
133584 const char *zName, /* Name assigned to this module */
133585 const sqlite3_module *pModule, /* The definition of the module */
133586 void *pAux, /* Context pointer for xCreate/xConnect */
133587 void (*xDestroy)(void *) /* Module destructor function */
133588){
133589 Module *pMod;
133590 int nName = sqlite3Strlen30(zName);
133591 pMod = (Module *)sqlite3Malloc(sizeof(Module) + nName + 1);
133592 if( pMod==0 ){
133593 sqlite3OomFault(db);
133594 }else{
133595 Module *pDel;
133596 char *zCopy = (char *)(&pMod[1]);
133597 memcpy(zCopy, zName, nName+1);
133598 pMod->zName = zCopy;
133599 pMod->pModule = pModule;
133600 pMod->pAux = pAux;
133601 pMod->xDestroy = xDestroy;
133602 pMod->pEpoTab = 0;
133603 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
133604 assert( pDel==0 || pDel==pMod );
133605 if( pDel ){
133606 sqlite3OomFault(db);
133607 sqlite3DbFree(db, pDel);
133608 pMod = 0;
133609 }
133610 }
133611 return pMod;
133612}
133613
133614/*
133615** The actual function that does the work of creating a new module.
133616** This function implements the sqlite3_create_module() and
133617** sqlite3_create_module_v2() interfaces.
133618*/
133619static int createModule(
133620 sqlite3 *db, /* Database in which module is registered */
133621 const char *zName, /* Name assigned to this module */
133622 const sqlite3_module *pModule, /* The definition of the module */
133623 void *pAux, /* Context pointer for xCreate/xConnect */
133624 void (*xDestroy)(void *) /* Module destructor function */
133625){
133626 int rc = SQLITE_OK;
133627
133628 sqlite3_mutex_enter(db->mutex);
133629 if( sqlite3HashFind(&db->aModule, zName) ){
133630 rc = SQLITE_MISUSE_BKPT;
133631 }else{
133632 (void)sqlite3VtabCreateModule(db, zName, pModule, pAux, xDestroy);
133633 }
133634 rc = sqlite3ApiExit(db, rc);
133635 if( rc!=SQLITE_OK && xDestroy ) xDestroy(pAux);
133636 sqlite3_mutex_leave(db->mutex);
133637 return rc;
133638}
133639
133640
133641/*
133642** External API function used to create a new virtual-table module.
133643*/
133644SQLITE_API int sqlite3_create_module(
133645 sqlite3 *db, /* Database in which module is registered */
133646 const char *zName, /* Name assigned to this module */
133647 const sqlite3_module *pModule, /* The definition of the module */
133648 void *pAux /* Context pointer for xCreate/xConnect */
133649){
133650#ifdef SQLITE_ENABLE_API_ARMOR
133651 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
133652#endif
133653 return createModule(db, zName, pModule, pAux, 0);
133654}
133655
133656/*
133657** External API function used to create a new virtual-table module.
133658*/
133659SQLITE_API int sqlite3_create_module_v2(
133660 sqlite3 *db, /* Database in which module is registered */
133661 const char *zName, /* Name assigned to this module */
133662 const sqlite3_module *pModule, /* The definition of the module */
133663 void *pAux, /* Context pointer for xCreate/xConnect */
133664 void (*xDestroy)(void *) /* Module destructor function */
133665){
133666#ifdef SQLITE_ENABLE_API_ARMOR
133667 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
133668#endif
133669 return createModule(db, zName, pModule, pAux, xDestroy);
133670}
133671
133672/*
133673** Lock the virtual table so that it cannot be disconnected.
133674** Locks nest. Every lock should have a corresponding unlock.
133675** If an unlock is omitted, resources leaks will occur.
133676**
133677** If a disconnect is attempted while a virtual table is locked,
133678** the disconnect is deferred until all locks have been removed.
133679*/
133680SQLITE_PRIVATE void sqlite3VtabLock(VTable *pVTab){
133681 pVTab->nRef++;
133682}
133683
133684
133685/*
133686** pTab is a pointer to a Table structure representing a virtual-table.
133687** Return a pointer to the VTable object used by connection db to access
133688** this virtual-table, if one has been created, or NULL otherwise.
133689*/
133690SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
133691 VTable *pVtab;
133692 assert( IsVirtual(pTab) );
133693 for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
133694 return pVtab;
133695}
133696
133697/*
133698** Decrement the ref-count on a virtual table object. When the ref-count
133699** reaches zero, call the xDisconnect() method to delete the object.
133700*/
133701SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *pVTab){
133702 sqlite3 *db = pVTab->db;
133703
133704 assert( db );
133705 assert( pVTab->nRef>0 );
133706 assert( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ZOMBIE );
133707
133708 pVTab->nRef--;
133709 if( pVTab->nRef==0 ){
133710 sqlite3_vtab *p = pVTab->pVtab;
133711 if( p ){
133712 p->pModule->xDisconnect(p);
133713 }
133714 sqlite3DbFree(db, pVTab);
133715 }
133716}
133717
133718/*
133719** Table p is a virtual table. This function moves all elements in the
133720** p->pVTable list to the sqlite3.pDisconnect lists of their associated
133721** database connections to be disconnected at the next opportunity.
133722** Except, if argument db is not NULL, then the entry associated with
133723** connection db is left in the p->pVTable list.
133724*/
133725static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
133726 VTable *pRet = 0;
133727 VTable *pVTable = p->pVTable;
133728 p->pVTable = 0;
133729
133730 /* Assert that the mutex (if any) associated with the BtShared database
133731 ** that contains table p is held by the caller. See header comments
133732 ** above function sqlite3VtabUnlockList() for an explanation of why
133733 ** this makes it safe to access the sqlite3.pDisconnect list of any
133734 ** database connection that may have an entry in the p->pVTable list.
133735 */
133736 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
133737
133738 while( pVTable ){
133739 sqlite3 *db2 = pVTable->db;
133740 VTable *pNext = pVTable->pNext;
133741 assert( db2 );
133742 if( db2==db ){
133743 pRet = pVTable;
133744 p->pVTable = pRet;
133745 pRet->pNext = 0;
133746 }else{
133747 pVTable->pNext = db2->pDisconnect;
133748 db2->pDisconnect = pVTable;
133749 }
133750 pVTable = pNext;
133751 }
133752
133753 assert( !db || pRet );
133754 return pRet;
133755}
133756
133757/*
133758** Table *p is a virtual table. This function removes the VTable object
133759** for table *p associated with database connection db from the linked
133760** list in p->pVTab. It also decrements the VTable ref count. This is
133761** used when closing database connection db to free all of its VTable
133762** objects without disturbing the rest of the Schema object (which may
133763** be being used by other shared-cache connections).
133764*/
133765SQLITE_PRIVATE void sqlite3VtabDisconnect(sqlite3 *db, Table *p){
133766 VTable **ppVTab;
133767
133768 assert( IsVirtual(p) );
133769 assert( sqlite3BtreeHoldsAllMutexes(db) );
133770 assert( sqlite3_mutex_held(db->mutex) );
133771
133772 for(ppVTab=&p->pVTable; *ppVTab; ppVTab=&(*ppVTab)->pNext){
133773 if( (*ppVTab)->db==db ){
133774 VTable *pVTab = *ppVTab;
133775 *ppVTab = pVTab->pNext;
133776 sqlite3VtabUnlock(pVTab);
133777 break;
133778 }
133779 }
133780}
133781
133782
133783/*
133784** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
133785**
133786** This function may only be called when the mutexes associated with all
133787** shared b-tree databases opened using connection db are held by the
133788** caller. This is done to protect the sqlite3.pDisconnect list. The
133789** sqlite3.pDisconnect list is accessed only as follows:
133790**
133791** 1) By this function. In this case, all BtShared mutexes and the mutex
133792** associated with the database handle itself must be held.
133793**
133794** 2) By function vtabDisconnectAll(), when it adds a VTable entry to
133795** the sqlite3.pDisconnect list. In this case either the BtShared mutex
133796** associated with the database the virtual table is stored in is held
133797** or, if the virtual table is stored in a non-sharable database, then
133798** the database handle mutex is held.
133799**
133800** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously
133801** by multiple threads. It is thread-safe.
133802*/
133803SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3 *db){
133804 VTable *p = db->pDisconnect;
133805 db->pDisconnect = 0;
133806
133807 assert( sqlite3BtreeHoldsAllMutexes(db) );
133808 assert( sqlite3_mutex_held(db->mutex) );
133809
133810 if( p ){
133811 sqlite3ExpirePreparedStatements(db, 0);
133812 do {
133813 VTable *pNext = p->pNext;
133814 sqlite3VtabUnlock(p);
133815 p = pNext;
133816 }while( p );
133817 }
133818}
133819
133820/*
133821** Clear any and all virtual-table information from the Table record.
133822** This routine is called, for example, just before deleting the Table
133823** record.
133824**
133825** Since it is a virtual-table, the Table structure contains a pointer
133826** to the head of a linked list of VTable structures. Each VTable
133827** structure is associated with a single sqlite3* user of the schema.
133828** The reference count of the VTable structure associated with database
133829** connection db is decremented immediately (which may lead to the
133830** structure being xDisconnected and free). Any other VTable structures
133831** in the list are moved to the sqlite3.pDisconnect list of the associated
133832** database connection.
133833*/
133834SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){
133835 if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
133836 if( p->azModuleArg ){
133837 int i;
133838 for(i=0; i<p->nModuleArg; i++){
133839 if( i!=1 ) sqlite3DbFree(db, p->azModuleArg[i]);
133840 }
133841 sqlite3DbFree(db, p->azModuleArg);
133842 }
133843}
133844
133845/*
133846** Add a new module argument to pTable->azModuleArg[].
133847** The string is not copied - the pointer is stored. The
133848** string will be freed automatically when the table is
133849** deleted.
133850*/
133851static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
133852 int nBytes = sizeof(char *)*(2+pTable->nModuleArg);
133853 char **azModuleArg;
133854 azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
133855 if( azModuleArg==0 ){
133856 sqlite3DbFree(db, zArg);
133857 }else{
133858 int i = pTable->nModuleArg++;
133859 azModuleArg[i] = zArg;
133860 azModuleArg[i+1] = 0;
133861 pTable->azModuleArg = azModuleArg;
133862 }
133863}
133864
133865/*
133866** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
133867** statement. The module name has been parsed, but the optional list
133868** of parameters that follow the module name are still pending.
133869*/
133870SQLITE_PRIVATE void sqlite3VtabBeginParse(
133871 Parse *pParse, /* Parsing context */
133872 Token *pName1, /* Name of new table, or database name */
133873 Token *pName2, /* Name of new table or NULL */
133874 Token *pModuleName, /* Name of the module for the virtual table */
133875 int ifNotExists /* No error if the table already exists */
133876){
133877 Table *pTable; /* The new virtual table */
133878 sqlite3 *db; /* Database connection */
133879
133880 sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists);
133881 pTable = pParse->pNewTable;
133882 if( pTable==0 ) return;
133883 assert( 0==pTable->pIndex );
133884
133885 db = pParse->db;
133886
133887 assert( pTable->nModuleArg==0 );
133888 addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
133889 addModuleArgument(db, pTable, 0);
133890 addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
133891 assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
133892 || (pParse->sNameToken.z==pName1->z && pName2->z==0)
133893 );
133894 pParse->sNameToken.n = (int)(
133895 &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
133896 );
133897
133898#ifndef SQLITE_OMIT_AUTHORIZATION
133899 /* Creating a virtual table invokes the authorization callback twice.
133900 ** The first invocation, to obtain permission to INSERT a row into the
133901 ** sqlite_master table, has already been made by sqlite3StartTable().
133902 ** The second call, to obtain permission to create the table, is made now.
133903 */
133904 if( pTable->azModuleArg ){
133905 int iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
133906 assert( iDb>=0 ); /* The database the table is being created in */
133907 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
133908 pTable->azModuleArg[0], pParse->db->aDb[iDb].zDbSName);
133909 }
133910#endif
133911}
133912
133913/*
133914** This routine takes the module argument that has been accumulating
133915** in pParse->zArg[] and appends it to the list of arguments on the
133916** virtual table currently under construction in pParse->pTable.
133917*/
133918static void addArgumentToVtab(Parse *pParse){
133919 if( pParse->sArg.z && pParse->pNewTable ){
133920 const char *z = (const char*)pParse->sArg.z;
133921 int n = pParse->sArg.n;
133922 sqlite3 *db = pParse->db;
133923 addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
133924 }
133925}
133926
133927/*
133928** The parser calls this routine after the CREATE VIRTUAL TABLE statement
133929** has been completely parsed.
133930*/
133931SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
133932 Table *pTab = pParse->pNewTable; /* The table being constructed */
133933 sqlite3 *db = pParse->db; /* The database connection */
133934
133935 if( pTab==0 ) return;
133936 addArgumentToVtab(pParse);
133937 pParse->sArg.z = 0;
133938 if( pTab->nModuleArg<1 ) return;
133939
133940 /* If the CREATE VIRTUAL TABLE statement is being entered for the
133941 ** first time (in other words if the virtual table is actually being
133942 ** created now instead of just being read out of sqlite_master) then
133943 ** do additional initialization work and store the statement text
133944 ** in the sqlite_master table.
133945 */
133946 if( !db->init.busy ){
133947 char *zStmt;
133948 char *zWhere;
133949 int iDb;
133950 int iReg;
133951 Vdbe *v;
133952
133953 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
133954 if( pEnd ){
133955 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
133956 }
133957 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
133958
133959 /* A slot for the record has already been allocated in the
133960 ** SQLITE_MASTER table. We just need to update that slot with all
133961 ** the information we've collected.
133962 **
133963 ** The VM register number pParse->regRowid holds the rowid of an
133964 ** entry in the sqlite_master table tht was created for this vtab
133965 ** by sqlite3StartTable().
133966 */
133967 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
133968 sqlite3NestedParse(pParse,
133969 "UPDATE %Q.%s "
133970 "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
133971 "WHERE rowid=#%d",
133972 db->aDb[iDb].zDbSName, MASTER_NAME,
133973 pTab->zName,
133974 pTab->zName,
133975 zStmt,
133976 pParse->regRowid
133977 );
133978 sqlite3DbFree(db, zStmt);
133979 v = sqlite3GetVdbe(pParse);
133980 sqlite3ChangeCookie(pParse, iDb);
133981
133982 sqlite3VdbeAddOp0(v, OP_Expire);
133983 zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
133984 sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
133985
133986 iReg = ++pParse->nMem;
133987 sqlite3VdbeLoadString(v, iReg, pTab->zName);
133988 sqlite3VdbeAddOp2(v, OP_VCreate, iDb, iReg);
133989 }
133990
133991 /* If we are rereading the sqlite_master table create the in-memory
133992 ** record of the table. The xConnect() method is not called until
133993 ** the first time the virtual table is used in an SQL statement. This
133994 ** allows a schema that contains virtual tables to be loaded before
133995 ** the required virtual table implementations are registered. */
133996 else {
133997 Table *pOld;
133998 Schema *pSchema = pTab->pSchema;
133999 const char *zName = pTab->zName;
134000 assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
134001 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
134002 if( pOld ){
134003 sqlite3OomFault(db);
134004 assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */
134005 return;
134006 }
134007 pParse->pNewTable = 0;
134008 }
134009}
134010
134011/*
134012** The parser calls this routine when it sees the first token
134013** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
134014*/
134015SQLITE_PRIVATE void sqlite3VtabArgInit(Parse *pParse){
134016 addArgumentToVtab(pParse);
134017 pParse->sArg.z = 0;
134018 pParse->sArg.n = 0;
134019}
134020
134021/*
134022** The parser calls this routine for each token after the first token
134023** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
134024*/
134025SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse *pParse, Token *p){
134026 Token *pArg = &pParse->sArg;
134027 if( pArg->z==0 ){
134028 pArg->z = p->z;
134029 pArg->n = p->n;
134030 }else{
134031 assert(pArg->z <= p->z);
134032 pArg->n = (int)(&p->z[p->n] - pArg->z);
134033 }
134034}
134035
134036/*
134037** Invoke a virtual table constructor (either xCreate or xConnect). The
134038** pointer to the function to invoke is passed as the fourth parameter
134039** to this procedure.
134040*/
134041static int vtabCallConstructor(
134042 sqlite3 *db,
134043 Table *pTab,
134044 Module *pMod,
134045 int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
134046 char **pzErr
134047){
134048 VtabCtx sCtx;
134049 VTable *pVTable;
134050 int rc;
134051 const char *const*azArg = (const char *const*)pTab->azModuleArg;
134052 int nArg = pTab->nModuleArg;
134053 char *zErr = 0;
134054 char *zModuleName;
134055 int iDb;
134056 VtabCtx *pCtx;
134057
134058 /* Check that the virtual-table is not already being initialized */
134059 for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
134060 if( pCtx->pTab==pTab ){
134061 *pzErr = sqlite3MPrintf(db,
134062 "vtable constructor called recursively: %s", pTab->zName
134063 );
134064 return SQLITE_LOCKED;
134065 }
134066 }
134067
134068 zModuleName = sqlite3DbStrDup(db, pTab->zName);
134069 if( !zModuleName ){
134070 return SQLITE_NOMEM_BKPT;
134071 }
134072
134073 pVTable = sqlite3MallocZero(sizeof(VTable));
134074 if( !pVTable ){
134075 sqlite3OomFault(db);
134076 sqlite3DbFree(db, zModuleName);
134077 return SQLITE_NOMEM_BKPT;
134078 }
134079 pVTable->db = db;
134080 pVTable->pMod = pMod;
134081
134082 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
134083 pTab->azModuleArg[1] = db->aDb[iDb].zDbSName;
134084
134085 /* Invoke the virtual table constructor */
134086 assert( &db->pVtabCtx );
134087 assert( xConstruct );
134088 sCtx.pTab = pTab;
134089 sCtx.pVTable = pVTable;
134090 sCtx.pPrior = db->pVtabCtx;
134091 sCtx.bDeclared = 0;
134092 db->pVtabCtx = &sCtx;
134093 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
134094 db->pVtabCtx = sCtx.pPrior;
134095 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
134096 assert( sCtx.pTab==pTab );
134097
134098 if( SQLITE_OK!=rc ){
134099 if( zErr==0 ){
134100 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
134101 }else {
134102 *pzErr = sqlite3MPrintf(db, "%s", zErr);
134103 sqlite3_free(zErr);
134104 }
134105 sqlite3DbFree(db, pVTable);
134106 }else if( ALWAYS(pVTable->pVtab) ){
134107 /* Justification of ALWAYS(): A correct vtab constructor must allocate
134108 ** the sqlite3_vtab object if successful. */
134109 memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0]));
134110 pVTable->pVtab->pModule = pMod->pModule;
134111 pVTable->nRef = 1;
134112 if( sCtx.bDeclared==0 ){
134113 const char *zFormat = "vtable constructor did not declare schema: %s";
134114 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
134115 sqlite3VtabUnlock(pVTable);
134116 rc = SQLITE_ERROR;
134117 }else{
134118 int iCol;
134119 u8 oooHidden = 0;
134120 /* If everything went according to plan, link the new VTable structure
134121 ** into the linked list headed by pTab->pVTable. Then loop through the
134122 ** columns of the table to see if any of them contain the token "hidden".
134123 ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from
134124 ** the type string. */
134125 pVTable->pNext = pTab->pVTable;
134126 pTab->pVTable = pVTable;
134127
134128 for(iCol=0; iCol<pTab->nCol; iCol++){
134129 char *zType = sqlite3ColumnType(&pTab->aCol[iCol], "");
134130 int nType;
134131 int i = 0;
134132 nType = sqlite3Strlen30(zType);
134133 for(i=0; i<nType; i++){
134134 if( 0==sqlite3StrNICmp("hidden", &zType[i], 6)
134135 && (i==0 || zType[i-1]==' ')
134136 && (zType[i+6]=='\0' || zType[i+6]==' ')
134137 ){
134138 break;
134139 }
134140 }
134141 if( i<nType ){
134142 int j;
134143 int nDel = 6 + (zType[i+6] ? 1 : 0);
134144 for(j=i; (j+nDel)<=nType; j++){
134145 zType[j] = zType[j+nDel];
134146 }
134147 if( zType[i]=='\0' && i>0 ){
134148 assert(zType[i-1]==' ');
134149 zType[i-1] = '\0';
134150 }
134151 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
134152 oooHidden = TF_OOOHidden;
134153 }else{
134154 pTab->tabFlags |= oooHidden;
134155 }
134156 }
134157 }
134158 }
134159
134160 sqlite3DbFree(db, zModuleName);
134161 return rc;
134162}
134163
134164/*
134165** This function is invoked by the parser to call the xConnect() method
134166** of the virtual table pTab. If an error occurs, an error code is returned
134167** and an error left in pParse.
134168**
134169** This call is a no-op if table pTab is not a virtual table.
134170*/
134171SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
134172 sqlite3 *db = pParse->db;
134173 const char *zMod;
134174 Module *pMod;
134175 int rc;
134176
134177 assert( pTab );
134178 if( !IsVirtual(pTab) || sqlite3GetVTable(db, pTab) ){
134179 return SQLITE_OK;
134180 }
134181
134182 /* Locate the required virtual table module */
134183 zMod = pTab->azModuleArg[0];
134184 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
134185
134186 if( !pMod ){
134187 const char *zModule = pTab->azModuleArg[0];
134188 sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
134189 rc = SQLITE_ERROR;
134190 }else{
134191 char *zErr = 0;
134192 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
134193 if( rc!=SQLITE_OK ){
134194 sqlite3ErrorMsg(pParse, "%s", zErr);
134195 pParse->rc = rc;
134196 }
134197 sqlite3DbFree(db, zErr);
134198 }
134199
134200 return rc;
134201}
134202/*
134203** Grow the db->aVTrans[] array so that there is room for at least one
134204** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
134205*/
134206static int growVTrans(sqlite3 *db){
134207 const int ARRAY_INCR = 5;
134208
134209 /* Grow the sqlite3.aVTrans array if required */
134210 if( (db->nVTrans%ARRAY_INCR)==0 ){
134211 VTable **aVTrans;
134212 int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
134213 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
134214 if( !aVTrans ){
134215 return SQLITE_NOMEM_BKPT;
134216 }
134217 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
134218 db->aVTrans = aVTrans;
134219 }
134220
134221 return SQLITE_OK;
134222}
134223
134224/*
134225** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
134226** have already been reserved using growVTrans().
134227*/
134228static void addToVTrans(sqlite3 *db, VTable *pVTab){
134229 /* Add pVtab to the end of sqlite3.aVTrans */
134230 db->aVTrans[db->nVTrans++] = pVTab;
134231 sqlite3VtabLock(pVTab);
134232}
134233
134234/*
134235** This function is invoked by the vdbe to call the xCreate method
134236** of the virtual table named zTab in database iDb.
134237**
134238** If an error occurs, *pzErr is set to point to an English language
134239** description of the error and an SQLITE_XXX error code is returned.
134240** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
134241*/
134242SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
134243 int rc = SQLITE_OK;
134244 Table *pTab;
134245 Module *pMod;
134246 const char *zMod;
134247
134248 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
134249 assert( pTab && IsVirtual(pTab) && !pTab->pVTable );
134250
134251 /* Locate the required virtual table module */
134252 zMod = pTab->azModuleArg[0];
134253 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
134254
134255 /* If the module has been registered and includes a Create method,
134256 ** invoke it now. If the module has not been registered, return an
134257 ** error. Otherwise, do nothing.
134258 */
134259 if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){
134260 *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
134261 rc = SQLITE_ERROR;
134262 }else{
134263 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
134264 }
134265
134266 /* Justification of ALWAYS(): The xConstructor method is required to
134267 ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
134268 if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
134269 rc = growVTrans(db);
134270 if( rc==SQLITE_OK ){
134271 addToVTrans(db, sqlite3GetVTable(db, pTab));
134272 }
134273 }
134274
134275 return rc;
134276}
134277
134278/*
134279** This function is used to set the schema of a virtual table. It is only
134280** valid to call this function from within the xCreate() or xConnect() of a
134281** virtual table module.
134282*/
134283SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
134284 VtabCtx *pCtx;
134285 int rc = SQLITE_OK;
134286 Table *pTab;
134287 char *zErr = 0;
134288 Parse sParse;
134289
134290#ifdef SQLITE_ENABLE_API_ARMOR
134291 if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){
134292 return SQLITE_MISUSE_BKPT;
134293 }
134294#endif
134295 sqlite3_mutex_enter(db->mutex);
134296 pCtx = db->pVtabCtx;
134297 if( !pCtx || pCtx->bDeclared ){
134298 sqlite3Error(db, SQLITE_MISUSE);
134299 sqlite3_mutex_leave(db->mutex);
134300 return SQLITE_MISUSE_BKPT;
134301 }
134302 pTab = pCtx->pTab;
134303 assert( IsVirtual(pTab) );
134304
134305 memset(&sParse, 0, sizeof(sParse));
134306 sParse.eParseMode = PARSE_MODE_DECLARE_VTAB;
134307 sParse.db = db;
134308 sParse.nQueryLoop = 1;
134309 if( SQLITE_OK==sqlite3RunParser(&sParse, zCreateTable, &zErr)
134310 && sParse.pNewTable
134311 && !db->mallocFailed
134312 && !sParse.pNewTable->pSelect
134313 && !IsVirtual(sParse.pNewTable)
134314 ){
134315 if( !pTab->aCol ){
134316 Table *pNew = sParse.pNewTable;
134317 Index *pIdx;
134318 pTab->aCol = pNew->aCol;
134319 pTab->nCol = pNew->nCol;
134320 pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid|TF_NoVisibleRowid);
134321 pNew->nCol = 0;
134322 pNew->aCol = 0;
134323 assert( pTab->pIndex==0 );
134324 assert( HasRowid(pNew) || sqlite3PrimaryKeyIndex(pNew)!=0 );
134325 if( !HasRowid(pNew)
134326 && pCtx->pVTable->pMod->pModule->xUpdate!=0
134327 && sqlite3PrimaryKeyIndex(pNew)->nKeyCol!=1
134328 ){
134329 /* WITHOUT ROWID virtual tables must either be read-only (xUpdate==0)
134330 ** or else must have a single-column PRIMARY KEY */
134331 rc = SQLITE_ERROR;
134332 }
134333 pIdx = pNew->pIndex;
134334 if( pIdx ){
134335 assert( pIdx->pNext==0 );
134336 pTab->pIndex = pIdx;
134337 pNew->pIndex = 0;
134338 pIdx->pTable = pTab;
134339 }
134340 }
134341 pCtx->bDeclared = 1;
134342 }else{
134343 sqlite3ErrorWithMsg(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
134344 sqlite3DbFree(db, zErr);
134345 rc = SQLITE_ERROR;
134346 }
134347 sParse.eParseMode = PARSE_MODE_NORMAL;
134348
134349 if( sParse.pVdbe ){
134350 sqlite3VdbeFinalize(sParse.pVdbe);
134351 }
134352 sqlite3DeleteTable(db, sParse.pNewTable);
134353 sqlite3ParserReset(&sParse);
134354
134355 assert( (rc&0xff)==rc );
134356 rc = sqlite3ApiExit(db, rc);
134357 sqlite3_mutex_leave(db->mutex);
134358 return rc;
134359}
134360
134361/*
134362** This function is invoked by the vdbe to call the xDestroy method
134363** of the virtual table named zTab in database iDb. This occurs
134364** when a DROP TABLE is mentioned.
134365**
134366** This call is a no-op if zTab is not a virtual table.
134367*/
134368SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
134369 int rc = SQLITE_OK;
134370 Table *pTab;
134371
134372 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
134373 if( pTab!=0 && ALWAYS(pTab->pVTable!=0) ){
134374 VTable *p;
134375 int (*xDestroy)(sqlite3_vtab *);
134376 for(p=pTab->pVTable; p; p=p->pNext){
134377 assert( p->pVtab );
134378 if( p->pVtab->nRef>0 ){
134379 return SQLITE_LOCKED;
134380 }
134381 }
134382 p = vtabDisconnectAll(db, pTab);
134383 xDestroy = p->pMod->pModule->xDestroy;
134384 assert( xDestroy!=0 ); /* Checked before the virtual table is created */
134385 rc = xDestroy(p->pVtab);
134386 /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
134387 if( rc==SQLITE_OK ){
134388 assert( pTab->pVTable==p && p->pNext==0 );
134389 p->pVtab = 0;
134390 pTab->pVTable = 0;
134391 sqlite3VtabUnlock(p);
134392 }
134393 }
134394
134395 return rc;
134396}
134397
134398/*
134399** This function invokes either the xRollback or xCommit method
134400** of each of the virtual tables in the sqlite3.aVTrans array. The method
134401** called is identified by the second argument, "offset", which is
134402** the offset of the method to call in the sqlite3_module structure.
134403**
134404** The array is cleared after invoking the callbacks.
134405*/
134406static void callFinaliser(sqlite3 *db, int offset){
134407 int i;
134408 if( db->aVTrans ){
134409 VTable **aVTrans = db->aVTrans;
134410 db->aVTrans = 0;
134411 for(i=0; i<db->nVTrans; i++){
134412 VTable *pVTab = aVTrans[i];
134413 sqlite3_vtab *p = pVTab->pVtab;
134414 if( p ){
134415 int (*x)(sqlite3_vtab *);
134416 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
134417 if( x ) x(p);
134418 }
134419 pVTab->iSavepoint = 0;
134420 sqlite3VtabUnlock(pVTab);
134421 }
134422 sqlite3DbFree(db, aVTrans);
134423 db->nVTrans = 0;
134424 }
134425}
134426
134427/*
134428** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
134429** array. Return the error code for the first error that occurs, or
134430** SQLITE_OK if all xSync operations are successful.
134431**
134432** If an error message is available, leave it in p->zErrMsg.
134433*/
134434SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, Vdbe *p){
134435 int i;
134436 int rc = SQLITE_OK;
134437 VTable **aVTrans = db->aVTrans;
134438
134439 db->aVTrans = 0;
134440 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
134441 int (*x)(sqlite3_vtab *);
134442 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
134443 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
134444 rc = x(pVtab);
134445 sqlite3VtabImportErrmsg(p, pVtab);
134446 }
134447 }
134448 db->aVTrans = aVTrans;
134449 return rc;
134450}
134451
134452/*
134453** Invoke the xRollback method of all virtual tables in the
134454** sqlite3.aVTrans array. Then clear the array itself.
134455*/
134456SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db){
134457 callFinaliser(db, offsetof(sqlite3_module,xRollback));
134458 return SQLITE_OK;
134459}
134460
134461/*
134462** Invoke the xCommit method of all virtual tables in the
134463** sqlite3.aVTrans array. Then clear the array itself.
134464*/
134465SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db){
134466 callFinaliser(db, offsetof(sqlite3_module,xCommit));
134467 return SQLITE_OK;
134468}
134469
134470/*
134471** If the virtual table pVtab supports the transaction interface
134472** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
134473** not currently open, invoke the xBegin method now.
134474**
134475** If the xBegin call is successful, place the sqlite3_vtab pointer
134476** in the sqlite3.aVTrans array.
134477*/
134478SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
134479 int rc = SQLITE_OK;
134480 const sqlite3_module *pModule;
134481
134482 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
134483 ** than zero, then this function is being called from within a
134484 ** virtual module xSync() callback. It is illegal to write to
134485 ** virtual module tables in this case, so return SQLITE_LOCKED.
134486 */
134487 if( sqlite3VtabInSync(db) ){
134488 return SQLITE_LOCKED;
134489 }
134490 if( !pVTab ){
134491 return SQLITE_OK;
134492 }
134493 pModule = pVTab->pVtab->pModule;
134494
134495 if( pModule->xBegin ){
134496 int i;
134497
134498 /* If pVtab is already in the aVTrans array, return early */
134499 for(i=0; i<db->nVTrans; i++){
134500 if( db->aVTrans[i]==pVTab ){
134501 return SQLITE_OK;
134502 }
134503 }
134504
134505 /* Invoke the xBegin method. If successful, add the vtab to the
134506 ** sqlite3.aVTrans[] array. */
134507 rc = growVTrans(db);
134508 if( rc==SQLITE_OK ){
134509 rc = pModule->xBegin(pVTab->pVtab);
134510 if( rc==SQLITE_OK ){
134511 int iSvpt = db->nStatement + db->nSavepoint;
134512 addToVTrans(db, pVTab);
134513 if( iSvpt && pModule->xSavepoint ){
134514 pVTab->iSavepoint = iSvpt;
134515 rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1);
134516 }
134517 }
134518 }
134519 }
134520 return rc;
134521}
134522
134523/*
134524** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
134525** virtual tables that currently have an open transaction. Pass iSavepoint
134526** as the second argument to the virtual table method invoked.
134527**
134528** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is
134529** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is
134530** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with
134531** an open transaction is invoked.
134532**
134533** If any virtual table method returns an error code other than SQLITE_OK,
134534** processing is abandoned and the error returned to the caller of this
134535** function immediately. If all calls to virtual table methods are successful,
134536** SQLITE_OK is returned.
134537*/
134538SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
134539 int rc = SQLITE_OK;
134540
134541 assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN );
134542 assert( iSavepoint>=-1 );
134543 if( db->aVTrans ){
134544 int i;
134545 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
134546 VTable *pVTab = db->aVTrans[i];
134547 const sqlite3_module *pMod = pVTab->pMod->pModule;
134548 if( pVTab->pVtab && pMod->iVersion>=2 ){
134549 int (*xMethod)(sqlite3_vtab *, int);
134550 switch( op ){
134551 case SAVEPOINT_BEGIN:
134552 xMethod = pMod->xSavepoint;
134553 pVTab->iSavepoint = iSavepoint+1;
134554 break;
134555 case SAVEPOINT_ROLLBACK:
134556 xMethod = pMod->xRollbackTo;
134557 break;
134558 default:
134559 xMethod = pMod->xRelease;
134560 break;
134561 }
134562 if( xMethod && pVTab->iSavepoint>iSavepoint ){
134563 rc = xMethod(pVTab->pVtab, iSavepoint);
134564 }
134565 }
134566 }
134567 }
134568 return rc;
134569}
134570
134571/*
134572** The first parameter (pDef) is a function implementation. The
134573** second parameter (pExpr) is the first argument to this function.
134574** If pExpr is a column in a virtual table, then let the virtual
134575** table implementation have an opportunity to overload the function.
134576**
134577** This routine is used to allow virtual table implementations to
134578** overload MATCH, LIKE, GLOB, and REGEXP operators.
134579**
134580** Return either the pDef argument (indicating no change) or a
134581** new FuncDef structure that is marked as ephemeral using the
134582** SQLITE_FUNC_EPHEM flag.
134583*/
134584SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(
134585 sqlite3 *db, /* Database connection for reporting malloc problems */
134586 FuncDef *pDef, /* Function to possibly overload */
134587 int nArg, /* Number of arguments to the function */
134588 Expr *pExpr /* First argument to the function */
134589){
134590 Table *pTab;
134591 sqlite3_vtab *pVtab;
134592 sqlite3_module *pMod;
134593 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
134594 void *pArg = 0;
134595 FuncDef *pNew;
134596 int rc = 0;
134597
134598 /* Check to see the left operand is a column in a virtual table */
134599 if( NEVER(pExpr==0) ) return pDef;
134600 if( pExpr->op!=TK_COLUMN ) return pDef;
134601 pTab = pExpr->y.pTab;
134602 if( pTab==0 ) return pDef;
134603 if( !IsVirtual(pTab) ) return pDef;
134604 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
134605 assert( pVtab!=0 );
134606 assert( pVtab->pModule!=0 );
134607 pMod = (sqlite3_module *)pVtab->pModule;
134608 if( pMod->xFindFunction==0 ) return pDef;
134609
134610 /* Call the xFindFunction method on the virtual table implementation
134611 ** to see if the implementation wants to overload this function.
134612 **
134613 ** Though undocumented, we have historically always invoked xFindFunction
134614 ** with an all lower-case function name. Continue in this tradition to
134615 ** avoid any chance of an incompatibility.
134616 */
134617#ifdef SQLITE_DEBUG
134618 {
134619 int i;
134620 for(i=0; pDef->zName[i]; i++){
134621 unsigned char x = (unsigned char)pDef->zName[i];
134622 assert( x==sqlite3UpperToLower[x] );
134623 }
134624 }
134625#endif
134626 rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg);
134627 if( rc==0 ){
134628 return pDef;
134629 }
134630
134631 /* Create a new ephemeral function definition for the overloaded
134632 ** function */
134633 pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
134634 + sqlite3Strlen30(pDef->zName) + 1);
134635 if( pNew==0 ){
134636 return pDef;
134637 }
134638 *pNew = *pDef;
134639 pNew->zName = (const char*)&pNew[1];
134640 memcpy((char*)&pNew[1], pDef->zName, sqlite3Strlen30(pDef->zName)+1);
134641 pNew->xSFunc = xSFunc;
134642 pNew->pUserData = pArg;
134643 pNew->funcFlags |= SQLITE_FUNC_EPHEM;
134644 return pNew;
134645}
134646
134647/*
134648** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
134649** array so that an OP_VBegin will get generated for it. Add pTab to the
134650** array if it is missing. If pTab is already in the array, this routine
134651** is a no-op.
134652*/
134653SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
134654 Parse *pToplevel = sqlite3ParseToplevel(pParse);
134655 int i, n;
134656 Table **apVtabLock;
134657
134658 assert( IsVirtual(pTab) );
134659 for(i=0; i<pToplevel->nVtabLock; i++){
134660 if( pTab==pToplevel->apVtabLock[i] ) return;
134661 }
134662 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
134663 apVtabLock = sqlite3_realloc64(pToplevel->apVtabLock, n);
134664 if( apVtabLock ){
134665 pToplevel->apVtabLock = apVtabLock;
134666 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
134667 }else{
134668 sqlite3OomFault(pToplevel->db);
134669 }
134670}
134671
134672/*
134673** Check to see if virtual table module pMod can be have an eponymous
134674** virtual table instance. If it can, create one if one does not already
134675** exist. Return non-zero if the eponymous virtual table instance exists
134676** when this routine returns, and return zero if it does not exist.
134677**
134678** An eponymous virtual table instance is one that is named after its
134679** module, and more importantly, does not require a CREATE VIRTUAL TABLE
134680** statement in order to come into existance. Eponymous virtual table
134681** instances always exist. They cannot be DROP-ed.
134682**
134683** Any virtual table module for which xConnect and xCreate are the same
134684** method can have an eponymous virtual table instance.
134685*/
134686SQLITE_PRIVATE int sqlite3VtabEponymousTableInit(Parse *pParse, Module *pMod){
134687 const sqlite3_module *pModule = pMod->pModule;
134688 Table *pTab;
134689 char *zErr = 0;
134690 int rc;
134691 sqlite3 *db = pParse->db;
134692 if( pMod->pEpoTab ) return 1;
134693 if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0;
134694 pTab = sqlite3DbMallocZero(db, sizeof(Table));
134695 if( pTab==0 ) return 0;
134696 pTab->zName = sqlite3DbStrDup(db, pMod->zName);
134697 if( pTab->zName==0 ){
134698 sqlite3DbFree(db, pTab);
134699 return 0;
134700 }
134701 pMod->pEpoTab = pTab;
134702 pTab->nTabRef = 1;
134703 pTab->pSchema = db->aDb[0].pSchema;
134704 assert( pTab->nModuleArg==0 );
134705 pTab->iPKey = -1;
134706 addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName));
134707 addModuleArgument(db, pTab, 0);
134708 addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName));
134709 rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
134710 if( rc ){
134711 sqlite3ErrorMsg(pParse, "%s", zErr);
134712 sqlite3DbFree(db, zErr);
134713 sqlite3VtabEponymousTableClear(db, pMod);
134714 return 0;
134715 }
134716 return 1;
134717}
134718
134719/*
134720** Erase the eponymous virtual table instance associated with
134721** virtual table module pMod, if it exists.
134722*/
134723SQLITE_PRIVATE void sqlite3VtabEponymousTableClear(sqlite3 *db, Module *pMod){
134724 Table *pTab = pMod->pEpoTab;
134725 if( pTab!=0 ){
134726 /* Mark the table as Ephemeral prior to deleting it, so that the
134727 ** sqlite3DeleteTable() routine will know that it is not stored in
134728 ** the schema. */
134729 pTab->tabFlags |= TF_Ephemeral;
134730 sqlite3DeleteTable(db, pTab);
134731 pMod->pEpoTab = 0;
134732 }
134733}
134734
134735/*
134736** Return the ON CONFLICT resolution mode in effect for the virtual
134737** table update operation currently in progress.
134738**
134739** The results of this routine are undefined unless it is called from
134740** within an xUpdate method.
134741*/
134742SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){
134743 static const unsigned char aMap[] = {
134744 SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE
134745 };
134746#ifdef SQLITE_ENABLE_API_ARMOR
134747 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
134748#endif
134749 assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 );
134750 assert( OE_Ignore==4 && OE_Replace==5 );
134751 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
134752 return (int)aMap[db->vtabOnConflict-1];
134753}
134754
134755/*
134756** Call from within the xCreate() or xConnect() methods to provide
134757** the SQLite core with additional information about the behavior
134758** of the virtual table being implemented.
134759*/
134760SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){
134761 va_list ap;
134762 int rc = SQLITE_OK;
134763
134764#ifdef SQLITE_ENABLE_API_ARMOR
134765 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
134766#endif
134767 sqlite3_mutex_enter(db->mutex);
134768 va_start(ap, op);
134769 switch( op ){
134770 case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
134771 VtabCtx *p = db->pVtabCtx;
134772 if( !p ){
134773 rc = SQLITE_MISUSE_BKPT;
134774 }else{
134775 assert( p->pTab==0 || IsVirtual(p->pTab) );
134776 p->pVTable->bConstraint = (u8)va_arg(ap, int);
134777 }
134778 break;
134779 }
134780 default:
134781 rc = SQLITE_MISUSE_BKPT;
134782 break;
134783 }
134784 va_end(ap);
134785
134786 if( rc!=SQLITE_OK ) sqlite3Error(db, rc);
134787 sqlite3_mutex_leave(db->mutex);
134788 return rc;
134789}
134790
134791#endif /* SQLITE_OMIT_VIRTUALTABLE */
134792
134793/************** End of vtab.c ************************************************/
134794/************** Begin file wherecode.c ***************************************/
134795/*
134796** 2015-06-06
134797**
134798** The author disclaims copyright to this source code. In place of
134799** a legal notice, here is a blessing:
134800**
134801** May you do good and not evil.
134802** May you find forgiveness for yourself and forgive others.
134803** May you share freely, never taking more than you give.
134804**
134805*************************************************************************
134806** This module contains C code that generates VDBE code used to process
134807** the WHERE clause of SQL statements.
134808**
134809** This file was split off from where.c on 2015-06-06 in order to reduce the
134810** size of where.c and make it easier to edit. This file contains the routines
134811** that actually generate the bulk of the WHERE loop code. The original where.c
134812** file retains the code that does query planning and analysis.
134813*/
134814/* #include "sqliteInt.h" */
134815/************** Include whereInt.h in the middle of wherecode.c **************/
134816/************** Begin file whereInt.h ****************************************/
134817/*
134818** 2013-11-12
134819**
134820** The author disclaims copyright to this source code. In place of
134821** a legal notice, here is a blessing:
134822**
134823** May you do good and not evil.
134824** May you find forgiveness for yourself and forgive others.
134825** May you share freely, never taking more than you give.
134826**
134827*************************************************************************
134828**
134829** This file contains structure and macro definitions for the query
134830** planner logic in "where.c". These definitions are broken out into
134831** a separate source file for easier editing.
134832*/
134833
134834/*
134835** Trace output macros
134836*/
134837#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
134838/***/ extern int sqlite3WhereTrace;
134839#endif
134840#if defined(SQLITE_DEBUG) \
134841 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
134842# define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
134843# define WHERETRACE_ENABLED 1
134844#else
134845# define WHERETRACE(K,X)
134846#endif
134847
134848/* Forward references
134849*/
134850typedef struct WhereClause WhereClause;
134851typedef struct WhereMaskSet WhereMaskSet;
134852typedef struct WhereOrInfo WhereOrInfo;
134853typedef struct WhereAndInfo WhereAndInfo;
134854typedef struct WhereLevel WhereLevel;
134855typedef struct WhereLoop WhereLoop;
134856typedef struct WherePath WherePath;
134857typedef struct WhereTerm WhereTerm;
134858typedef struct WhereLoopBuilder WhereLoopBuilder;
134859typedef struct WhereScan WhereScan;
134860typedef struct WhereOrCost WhereOrCost;
134861typedef struct WhereOrSet WhereOrSet;
134862
134863/*
134864** This object contains information needed to implement a single nested
134865** loop in WHERE clause.
134866**
134867** Contrast this object with WhereLoop. This object describes the
134868** implementation of the loop. WhereLoop describes the algorithm.
134869** This object contains a pointer to the WhereLoop algorithm as one of
134870** its elements.
134871**
134872** The WhereInfo object contains a single instance of this object for
134873** each term in the FROM clause (which is to say, for each of the
134874** nested loops as implemented). The order of WhereLevel objects determines
134875** the loop nested order, with WhereInfo.a[0] being the outer loop and
134876** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
134877*/
134878struct WhereLevel {
134879 int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */
134880 int iTabCur; /* The VDBE cursor used to access the table */
134881 int iIdxCur; /* The VDBE cursor used to access pIdx */
134882 int addrBrk; /* Jump here to break out of the loop */
134883 int addrNxt; /* Jump here to start the next IN combination */
134884 int addrSkip; /* Jump here for next iteration of skip-scan */
134885 int addrCont; /* Jump here to continue with the next loop cycle */
134886 int addrFirst; /* First instruction of interior of the loop */
134887 int addrBody; /* Beginning of the body of this loop */
134888#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
134889 u32 iLikeRepCntr; /* LIKE range processing counter register (times 2) */
134890 int addrLikeRep; /* LIKE range processing address */
134891#endif
134892 u8 iFrom; /* Which entry in the FROM clause */
134893 u8 op, p3, p5; /* Opcode, P3 & P5 of the opcode that ends the loop */
134894 int p1, p2; /* Operands of the opcode used to ends the loop */
134895 union { /* Information that depends on pWLoop->wsFlags */
134896 struct {
134897 int nIn; /* Number of entries in aInLoop[] */
134898 struct InLoop {
134899 int iCur; /* The VDBE cursor used by this IN operator */
134900 int addrInTop; /* Top of the IN loop */
134901 int iBase; /* Base register of multi-key index record */
134902 int nPrefix; /* Number of prior entires in the key */
134903 u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */
134904 } *aInLoop; /* Information about each nested IN operator */
134905 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
134906 Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */
134907 } u;
134908 struct WhereLoop *pWLoop; /* The selected WhereLoop object */
134909 Bitmask notReady; /* FROM entries not usable at this level */
134910#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
134911 int addrVisit; /* Address at which row is visited */
134912#endif
134913};
134914
134915/*
134916** Each instance of this object represents an algorithm for evaluating one
134917** term of a join. Every term of the FROM clause will have at least
134918** one corresponding WhereLoop object (unless INDEXED BY constraints
134919** prevent a query solution - which is an error) and many terms of the
134920** FROM clause will have multiple WhereLoop objects, each describing a
134921** potential way of implementing that FROM-clause term, together with
134922** dependencies and cost estimates for using the chosen algorithm.
134923**
134924** Query planning consists of building up a collection of these WhereLoop
134925** objects, then computing a particular sequence of WhereLoop objects, with
134926** one WhereLoop object per FROM clause term, that satisfy all dependencies
134927** and that minimize the overall cost.
134928*/
134929struct WhereLoop {
134930 Bitmask prereq; /* Bitmask of other loops that must run first */
134931 Bitmask maskSelf; /* Bitmask identifying table iTab */
134932#ifdef SQLITE_DEBUG
134933 char cId; /* Symbolic ID of this loop for debugging use */
134934#endif
134935 u8 iTab; /* Position in FROM clause of table for this loop */
134936 u8 iSortIdx; /* Sorting index number. 0==None */
134937 LogEst rSetup; /* One-time setup cost (ex: create transient index) */
134938 LogEst rRun; /* Cost of running each loop */
134939 LogEst nOut; /* Estimated number of output rows */
134940 union {
134941 struct { /* Information for internal btree tables */
134942 u16 nEq; /* Number of equality constraints */
134943 u16 nBtm; /* Size of BTM vector */
134944 u16 nTop; /* Size of TOP vector */
134945 u16 nIdxCol; /* Index column used for ORDER BY */
134946 Index *pIndex; /* Index used, or NULL */
134947 } btree;
134948 struct { /* Information for virtual tables */
134949 int idxNum; /* Index number */
134950 u8 needFree; /* True if sqlite3_free(idxStr) is needed */
134951 i8 isOrdered; /* True if satisfies ORDER BY */
134952 u16 omitMask; /* Terms that may be omitted */
134953 char *idxStr; /* Index identifier string */
134954 } vtab;
134955 } u;
134956 u32 wsFlags; /* WHERE_* flags describing the plan */
134957 u16 nLTerm; /* Number of entries in aLTerm[] */
134958 u16 nSkip; /* Number of NULL aLTerm[] entries */
134959 /**** whereLoopXfer() copies fields above ***********************/
134960# define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot)
134961 u16 nLSlot; /* Number of slots allocated for aLTerm[] */
134962 WhereTerm **aLTerm; /* WhereTerms used */
134963 WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */
134964 WhereTerm *aLTermSpace[3]; /* Initial aLTerm[] space */
134965};
134966
134967/* This object holds the prerequisites and the cost of running a
134968** subquery on one operand of an OR operator in the WHERE clause.
134969** See WhereOrSet for additional information
134970*/
134971struct WhereOrCost {
134972 Bitmask prereq; /* Prerequisites */
134973 LogEst rRun; /* Cost of running this subquery */
134974 LogEst nOut; /* Number of outputs for this subquery */
134975};
134976
134977/* The WhereOrSet object holds a set of possible WhereOrCosts that
134978** correspond to the subquery(s) of OR-clause processing. Only the
134979** best N_OR_COST elements are retained.
134980*/
134981#define N_OR_COST 3
134982struct WhereOrSet {
134983 u16 n; /* Number of valid a[] entries */
134984 WhereOrCost a[N_OR_COST]; /* Set of best costs */
134985};
134986
134987/*
134988** Each instance of this object holds a sequence of WhereLoop objects
134989** that implement some or all of a query plan.
134990**
134991** Think of each WhereLoop object as a node in a graph with arcs
134992** showing dependencies and costs for travelling between nodes. (That is
134993** not a completely accurate description because WhereLoop costs are a
134994** vector, not a scalar, and because dependencies are many-to-one, not
134995** one-to-one as are graph nodes. But it is a useful visualization aid.)
134996** Then a WherePath object is a path through the graph that visits some
134997** or all of the WhereLoop objects once.
134998**
134999** The "solver" works by creating the N best WherePath objects of length
135000** 1. Then using those as a basis to compute the N best WherePath objects
135001** of length 2. And so forth until the length of WherePaths equals the
135002** number of nodes in the FROM clause. The best (lowest cost) WherePath
135003** at the end is the chosen query plan.
135004*/
135005struct WherePath {
135006 Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */
135007 Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */
135008 LogEst nRow; /* Estimated number of rows generated by this path */
135009 LogEst rCost; /* Total cost of this path */
135010 LogEst rUnsorted; /* Total cost of this path ignoring sorting costs */
135011 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
135012 WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */
135013};
135014
135015/*
135016** The query generator uses an array of instances of this structure to
135017** help it analyze the subexpressions of the WHERE clause. Each WHERE
135018** clause subexpression is separated from the others by AND operators,
135019** usually, or sometimes subexpressions separated by OR.
135020**
135021** All WhereTerms are collected into a single WhereClause structure.
135022** The following identity holds:
135023**
135024** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
135025**
135026** When a term is of the form:
135027**
135028** X <op> <expr>
135029**
135030** where X is a column name and <op> is one of certain operators,
135031** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
135032** cursor number and column number for X. WhereTerm.eOperator records
135033** the <op> using a bitmask encoding defined by WO_xxx below. The
135034** use of a bitmask encoding for the operator allows us to search
135035** quickly for terms that match any of several different operators.
135036**
135037** A WhereTerm might also be two or more subterms connected by OR:
135038**
135039** (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
135040**
135041** In this second case, wtFlag has the TERM_ORINFO bit set and eOperator==WO_OR
135042** and the WhereTerm.u.pOrInfo field points to auxiliary information that
135043** is collected about the OR clause.
135044**
135045** If a term in the WHERE clause does not match either of the two previous
135046** categories, then eOperator==0. The WhereTerm.pExpr field is still set
135047** to the original subexpression content and wtFlags is set up appropriately
135048** but no other fields in the WhereTerm object are meaningful.
135049**
135050** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
135051** but they do so indirectly. A single WhereMaskSet structure translates
135052** cursor number into bits and the translated bit is stored in the prereq
135053** fields. The translation is used in order to maximize the number of
135054** bits that will fit in a Bitmask. The VDBE cursor numbers might be
135055** spread out over the non-negative integers. For example, the cursor
135056** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The WhereMaskSet
135057** translates these sparse cursor numbers into consecutive integers
135058** beginning with 0 in order to make the best possible use of the available
135059** bits in the Bitmask. So, in the example above, the cursor numbers
135060** would be mapped into integers 0 through 7.
135061**
135062** The number of terms in a join is limited by the number of bits
135063** in prereqRight and prereqAll. The default is 64 bits, hence SQLite
135064** is only able to process joins with 64 or fewer tables.
135065*/
135066struct WhereTerm {
135067 Expr *pExpr; /* Pointer to the subexpression that is this term */
135068 WhereClause *pWC; /* The clause this term is part of */
135069 LogEst truthProb; /* Probability of truth for this expression */
135070 u16 wtFlags; /* TERM_xxx bit flags. See below */
135071 u16 eOperator; /* A WO_xx value describing <op> */
135072 u8 nChild; /* Number of children that must disable us */
135073 u8 eMatchOp; /* Op for vtab MATCH/LIKE/GLOB/REGEXP terms */
135074 int iParent; /* Disable pWC->a[iParent] when this term disabled */
135075 int leftCursor; /* Cursor number of X in "X <op> <expr>" */
135076 int iField; /* Field in (?,?,?) IN (SELECT...) vector */
135077 union {
135078 int leftColumn; /* Column number of X in "X <op> <expr>" */
135079 WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */
135080 WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */
135081 } u;
135082 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
135083 Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */
135084};
135085
135086/*
135087** Allowed values of WhereTerm.wtFlags
135088*/
135089#define TERM_DYNAMIC 0x01 /* Need to call sqlite3ExprDelete(db, pExpr) */
135090#define TERM_VIRTUAL 0x02 /* Added by the optimizer. Do not code */
135091#define TERM_CODED 0x04 /* This term is already coded */
135092#define TERM_COPIED 0x08 /* Has a child */
135093#define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
135094#define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
135095#define TERM_OR_OK 0x40 /* Used during OR-clause processing */
135096#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
135097# define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */
135098#else
135099# define TERM_VNULL 0x00 /* Disabled if not using stat3 */
135100#endif
135101#define TERM_LIKEOPT 0x100 /* Virtual terms from the LIKE optimization */
135102#define TERM_LIKECOND 0x200 /* Conditionally this LIKE operator term */
135103#define TERM_LIKE 0x400 /* The original LIKE operator */
135104#define TERM_IS 0x800 /* Term.pExpr is an IS operator */
135105#define TERM_VARSELECT 0x1000 /* Term.pExpr contains a correlated sub-query */
135106
135107/*
135108** An instance of the WhereScan object is used as an iterator for locating
135109** terms in the WHERE clause that are useful to the query planner.
135110*/
135111struct WhereScan {
135112 WhereClause *pOrigWC; /* Original, innermost WhereClause */
135113 WhereClause *pWC; /* WhereClause currently being scanned */
135114 const char *zCollName; /* Required collating sequence, if not NULL */
135115 Expr *pIdxExpr; /* Search for this index expression */
135116 char idxaff; /* Must match this affinity, if zCollName!=NULL */
135117 unsigned char nEquiv; /* Number of entries in aEquiv[] */
135118 unsigned char iEquiv; /* Next unused slot in aEquiv[] */
135119 u32 opMask; /* Acceptable operators */
135120 int k; /* Resume scanning at this->pWC->a[this->k] */
135121 int aiCur[11]; /* Cursors in the equivalence class */
135122 i16 aiColumn[11]; /* Corresponding column number in the eq-class */
135123};
135124
135125/*
135126** An instance of the following structure holds all information about a
135127** WHERE clause. Mostly this is a container for one or more WhereTerms.
135128**
135129** Explanation of pOuter: For a WHERE clause of the form
135130**
135131** a AND ((b AND c) OR (d AND e)) AND f
135132**
135133** There are separate WhereClause objects for the whole clause and for
135134** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the
135135** subclauses points to the WhereClause object for the whole clause.
135136*/
135137struct WhereClause {
135138 WhereInfo *pWInfo; /* WHERE clause processing context */
135139 WhereClause *pOuter; /* Outer conjunction */
135140 u8 op; /* Split operator. TK_AND or TK_OR */
135141 u8 hasOr; /* True if any a[].eOperator is WO_OR */
135142 int nTerm; /* Number of terms */
135143 int nSlot; /* Number of entries in a[] */
135144 WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */
135145#if defined(SQLITE_SMALL_STACK)
135146 WhereTerm aStatic[1]; /* Initial static space for a[] */
135147#else
135148 WhereTerm aStatic[8]; /* Initial static space for a[] */
135149#endif
135150};
135151
135152/*
135153** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
135154** a dynamically allocated instance of the following structure.
135155*/
135156struct WhereOrInfo {
135157 WhereClause wc; /* Decomposition into subterms */
135158 Bitmask indexable; /* Bitmask of all indexable tables in the clause */
135159};
135160
135161/*
135162** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
135163** a dynamically allocated instance of the following structure.
135164*/
135165struct WhereAndInfo {
135166 WhereClause wc; /* The subexpression broken out */
135167};
135168
135169/*
135170** An instance of the following structure keeps track of a mapping
135171** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
135172**
135173** The VDBE cursor numbers are small integers contained in
135174** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE
135175** clause, the cursor numbers might not begin with 0 and they might
135176** contain gaps in the numbering sequence. But we want to make maximum
135177** use of the bits in our bitmasks. This structure provides a mapping
135178** from the sparse cursor numbers into consecutive integers beginning
135179** with 0.
135180**
135181** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
135182** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
135183**
135184** For example, if the WHERE clause expression used these VDBE
135185** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure
135186** would map those cursor numbers into bits 0 through 5.
135187**
135188** Note that the mapping is not necessarily ordered. In the example
135189** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
135190** 57->5, 73->4. Or one of 719 other combinations might be used. It
135191** does not really matter. What is important is that sparse cursor
135192** numbers all get mapped into bit numbers that begin with 0 and contain
135193** no gaps.
135194*/
135195struct WhereMaskSet {
135196 int bVarSelect; /* Used by sqlite3WhereExprUsage() */
135197 int n; /* Number of assigned cursor values */
135198 int ix[BMS]; /* Cursor assigned to each bit */
135199};
135200
135201/*
135202** Initialize a WhereMaskSet object
135203*/
135204#define initMaskSet(P) (P)->n=0
135205
135206/*
135207** This object is a convenience wrapper holding all information needed
135208** to construct WhereLoop objects for a particular query.
135209*/
135210struct WhereLoopBuilder {
135211 WhereInfo *pWInfo; /* Information about this WHERE */
135212 WhereClause *pWC; /* WHERE clause terms */
135213 ExprList *pOrderBy; /* ORDER BY clause */
135214 WhereLoop *pNew; /* Template WhereLoop */
135215 WhereOrSet *pOrSet; /* Record best loops here, if not NULL */
135216#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
135217 UnpackedRecord *pRec; /* Probe for stat4 (if required) */
135218 int nRecValid; /* Number of valid fields currently in pRec */
135219#endif
135220 unsigned int bldFlags; /* SQLITE_BLDF_* flags */
135221 unsigned int iPlanLimit; /* Search limiter */
135222};
135223
135224/* Allowed values for WhereLoopBuider.bldFlags */
135225#define SQLITE_BLDF_INDEXED 0x0001 /* An index is used */
135226#define SQLITE_BLDF_UNIQUE 0x0002 /* All keys of a UNIQUE index used */
135227
135228/* The WhereLoopBuilder.iPlanLimit is used to limit the number of
135229** index+constraint combinations the query planner will consider for a
135230** particular query. If this parameter is unlimited, then certain
135231** pathological queries can spend excess time in the sqlite3WhereBegin()
135232** routine. The limit is high enough that is should not impact real-world
135233** queries.
135234**
135235** SQLITE_QUERY_PLANNER_LIMIT is the baseline limit. The limit is
135236** increased by SQLITE_QUERY_PLANNER_LIMIT_INCR before each term of the FROM
135237** clause is processed, so that every table in a join is guaranteed to be
135238** able to propose a some index+constraint combinations even if the initial
135239** baseline limit was exhausted by prior tables of the join.
135240*/
135241#ifndef SQLITE_QUERY_PLANNER_LIMIT
135242# define SQLITE_QUERY_PLANNER_LIMIT 20000
135243#endif
135244#ifndef SQLITE_QUERY_PLANNER_LIMIT_INCR
135245# define SQLITE_QUERY_PLANNER_LIMIT_INCR 1000
135246#endif
135247
135248/*
135249** The WHERE clause processing routine has two halves. The
135250** first part does the start of the WHERE loop and the second
135251** half does the tail of the WHERE loop. An instance of
135252** this structure is returned by the first half and passed
135253** into the second half to give some continuity.
135254**
135255** An instance of this object holds the complete state of the query
135256** planner.
135257*/
135258struct WhereInfo {
135259 Parse *pParse; /* Parsing and code generating context */
135260 SrcList *pTabList; /* List of tables in the join */
135261 ExprList *pOrderBy; /* The ORDER BY clause or NULL */
135262 ExprList *pResultSet; /* Result set of the query */
135263 Expr *pWhere; /* The complete WHERE clause */
135264 LogEst iLimit; /* LIMIT if wctrlFlags has WHERE_USE_LIMIT */
135265 int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */
135266 int iContinue; /* Jump here to continue with next record */
135267 int iBreak; /* Jump here to break out of the loop */
135268 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
135269 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
135270 u8 nLevel; /* Number of nested loop */
135271 i8 nOBSat; /* Number of ORDER BY terms satisfied by indices */
135272 u8 sorted; /* True if really sorted (not just grouped) */
135273 u8 eOnePass; /* ONEPASS_OFF, or _SINGLE, or _MULTI */
135274 u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */
135275 u8 eDistinct; /* One of the WHERE_DISTINCT_* values */
135276 u8 bOrderedInnerLoop; /* True if only the inner-most loop is ordered */
135277 int iTop; /* The very beginning of the WHERE loop */
135278 WhereLoop *pLoops; /* List of all WhereLoop objects */
135279 Bitmask revMask; /* Mask of ORDER BY terms that need reversing */
135280 LogEst nRowOut; /* Estimated number of output rows */
135281 WhereClause sWC; /* Decomposition of the WHERE clause */
135282 WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */
135283 WhereLevel a[1]; /* Information about each nest loop in WHERE */
135284};
135285
135286/*
135287** Private interfaces - callable only by other where.c routines.
135288**
135289** where.c:
135290*/
135291SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet*,int);
135292#ifdef WHERETRACE_ENABLED
135293SQLITE_PRIVATE void sqlite3WhereClausePrint(WhereClause *pWC);
135294#endif
135295SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
135296 WhereClause *pWC, /* The WHERE clause to be searched */
135297 int iCur, /* Cursor number of LHS */
135298 int iColumn, /* Column number of LHS */
135299 Bitmask notReady, /* RHS must not overlap with this mask */
135300 u32 op, /* Mask of WO_xx values describing operator */
135301 Index *pIdx /* Must be compatible with this index, if not NULL */
135302);
135303
135304/* wherecode.c: */
135305#ifndef SQLITE_OMIT_EXPLAIN
135306SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
135307 Parse *pParse, /* Parse context */
135308 SrcList *pTabList, /* Table list this loop refers to */
135309 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
135310 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
135311);
135312#else
135313# define sqlite3WhereExplainOneScan(u,v,w,x) 0
135314#endif /* SQLITE_OMIT_EXPLAIN */
135315#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
135316SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
135317 Vdbe *v, /* Vdbe to add scanstatus entry to */
135318 SrcList *pSrclist, /* FROM clause pLvl reads data from */
135319 WhereLevel *pLvl, /* Level to add scanstatus() entry for */
135320 int addrExplain /* Address of OP_Explain (or 0) */
135321);
135322#else
135323# define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d)
135324#endif
135325SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
135326 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
135327 int iLevel, /* Which level of pWInfo->a[] should be coded */
135328 Bitmask notReady /* Which tables are currently available */
135329);
135330
135331/* whereexpr.c: */
135332SQLITE_PRIVATE void sqlite3WhereClauseInit(WhereClause*,WhereInfo*);
135333SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause*);
135334SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause*,Expr*,u8);
135335SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*);
135336SQLITE_PRIVATE Bitmask sqlite3WhereExprUsageNN(WhereMaskSet*, Expr*);
135337SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*);
135338SQLITE_PRIVATE void sqlite3WhereExprAnalyze(SrcList*, WhereClause*);
135339SQLITE_PRIVATE void sqlite3WhereTabFuncArgs(Parse*, struct SrcList_item*, WhereClause*);
135340
135341
135342
135343
135344
135345/*
135346** Bitmasks for the operators on WhereTerm objects. These are all
135347** operators that are of interest to the query planner. An
135348** OR-ed combination of these values can be used when searching for
135349** particular WhereTerms within a WhereClause.
135350**
135351** Value constraints:
135352** WO_EQ == SQLITE_INDEX_CONSTRAINT_EQ
135353** WO_LT == SQLITE_INDEX_CONSTRAINT_LT
135354** WO_LE == SQLITE_INDEX_CONSTRAINT_LE
135355** WO_GT == SQLITE_INDEX_CONSTRAINT_GT
135356** WO_GE == SQLITE_INDEX_CONSTRAINT_GE
135357*/
135358#define WO_IN 0x0001
135359#define WO_EQ 0x0002
135360#define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
135361#define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
135362#define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
135363#define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
135364#define WO_AUX 0x0040 /* Op useful to virtual tables only */
135365#define WO_IS 0x0080
135366#define WO_ISNULL 0x0100
135367#define WO_OR 0x0200 /* Two or more OR-connected terms */
135368#define WO_AND 0x0400 /* Two or more AND-connected terms */
135369#define WO_EQUIV 0x0800 /* Of the form A==B, both columns */
135370#define WO_NOOP 0x1000 /* This term does not restrict search space */
135371
135372#define WO_ALL 0x1fff /* Mask of all possible WO_* values */
135373#define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */
135374
135375/*
135376** These are definitions of bits in the WhereLoop.wsFlags field.
135377** The particular combination of bits in each WhereLoop help to
135378** determine the algorithm that WhereLoop represents.
135379*/
135380#define WHERE_COLUMN_EQ 0x00000001 /* x=EXPR */
135381#define WHERE_COLUMN_RANGE 0x00000002 /* x<EXPR and/or x>EXPR */
135382#define WHERE_COLUMN_IN 0x00000004 /* x IN (...) */
135383#define WHERE_COLUMN_NULL 0x00000008 /* x IS NULL */
135384#define WHERE_CONSTRAINT 0x0000000f /* Any of the WHERE_COLUMN_xxx values */
135385#define WHERE_TOP_LIMIT 0x00000010 /* x<EXPR or x<=EXPR constraint */
135386#define WHERE_BTM_LIMIT 0x00000020 /* x>EXPR or x>=EXPR constraint */
135387#define WHERE_BOTH_LIMIT 0x00000030 /* Both x>EXPR and x<EXPR */
135388#define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */
135389#define WHERE_IPK 0x00000100 /* x is the INTEGER PRIMARY KEY */
135390#define WHERE_INDEXED 0x00000200 /* WhereLoop.u.btree.pIndex is valid */
135391#define WHERE_VIRTUALTABLE 0x00000400 /* WhereLoop.u.vtab is valid */
135392#define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */
135393#define WHERE_ONEROW 0x00001000 /* Selects no more than one row */
135394#define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */
135395#define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */
135396#define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
135397#define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/
135398#define WHERE_PARTIALIDX 0x00020000 /* The automatic index is partial */
135399#define WHERE_IN_EARLYOUT 0x00040000 /* Perhaps quit IN loops early */
135400
135401/************** End of whereInt.h ********************************************/
135402/************** Continuing where we left off in wherecode.c ******************/
135403
135404#ifndef SQLITE_OMIT_EXPLAIN
135405
135406/*
135407** Return the name of the i-th column of the pIdx index.
135408*/
135409static const char *explainIndexColumnName(Index *pIdx, int i){
135410 i = pIdx->aiColumn[i];
135411 if( i==XN_EXPR ) return "<expr>";
135412 if( i==XN_ROWID ) return "rowid";
135413 return pIdx->pTable->aCol[i].zName;
135414}
135415
135416/*
135417** This routine is a helper for explainIndexRange() below
135418**
135419** pStr holds the text of an expression that we are building up one term
135420** at a time. This routine adds a new term to the end of the expression.
135421** Terms are separated by AND so add the "AND" text for second and subsequent
135422** terms only.
135423*/
135424static void explainAppendTerm(
135425 StrAccum *pStr, /* The text expression being built */
135426 Index *pIdx, /* Index to read column names from */
135427 int nTerm, /* Number of terms */
135428 int iTerm, /* Zero-based index of first term. */
135429 int bAnd, /* Non-zero to append " AND " */
135430 const char *zOp /* Name of the operator */
135431){
135432 int i;
135433
135434 assert( nTerm>=1 );
135435 if( bAnd ) sqlite3_str_append(pStr, " AND ", 5);
135436
135437 if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1);
135438 for(i=0; i<nTerm; i++){
135439 if( i ) sqlite3_str_append(pStr, ",", 1);
135440 sqlite3_str_appendall(pStr, explainIndexColumnName(pIdx, iTerm+i));
135441 }
135442 if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1);
135443
135444 sqlite3_str_append(pStr, zOp, 1);
135445
135446 if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1);
135447 for(i=0; i<nTerm; i++){
135448 if( i ) sqlite3_str_append(pStr, ",", 1);
135449 sqlite3_str_append(pStr, "?", 1);
135450 }
135451 if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1);
135452}
135453
135454/*
135455** Argument pLevel describes a strategy for scanning table pTab. This
135456** function appends text to pStr that describes the subset of table
135457** rows scanned by the strategy in the form of an SQL expression.
135458**
135459** For example, if the query:
135460**
135461** SELECT * FROM t1 WHERE a=1 AND b>2;
135462**
135463** is run and there is an index on (a, b), then this function returns a
135464** string similar to:
135465**
135466** "a=? AND b>?"
135467*/
135468static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop){
135469 Index *pIndex = pLoop->u.btree.pIndex;
135470 u16 nEq = pLoop->u.btree.nEq;
135471 u16 nSkip = pLoop->nSkip;
135472 int i, j;
135473
135474 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
135475 sqlite3_str_append(pStr, " (", 2);
135476 for(i=0; i<nEq; i++){
135477 const char *z = explainIndexColumnName(pIndex, i);
135478 if( i ) sqlite3_str_append(pStr, " AND ", 5);
135479 sqlite3_str_appendf(pStr, i>=nSkip ? "%s=?" : "ANY(%s)", z);
135480 }
135481
135482 j = i;
135483 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
135484 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">");
135485 i = 1;
135486 }
135487 if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
135488 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<");
135489 }
135490 sqlite3_str_append(pStr, ")", 1);
135491}
135492
135493/*
135494** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
135495** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was
135496** defined at compile-time. If it is not a no-op, a single OP_Explain opcode
135497** is added to the output to describe the table scan strategy in pLevel.
135498**
135499** If an OP_Explain opcode is added to the VM, its address is returned.
135500** Otherwise, if no OP_Explain is coded, zero is returned.
135501*/
135502SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
135503 Parse *pParse, /* Parse context */
135504 SrcList *pTabList, /* Table list this loop refers to */
135505 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
135506 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
135507){
135508 int ret = 0;
135509#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
135510 if( sqlite3ParseToplevel(pParse)->explain==2 )
135511#endif
135512 {
135513 struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
135514 Vdbe *v = pParse->pVdbe; /* VM being constructed */
135515 sqlite3 *db = pParse->db; /* Database handle */
135516 int isSearch; /* True for a SEARCH. False for SCAN. */
135517 WhereLoop *pLoop; /* The controlling WhereLoop object */
135518 u32 flags; /* Flags that describe this loop */
135519 char *zMsg; /* Text to add to EQP output */
135520 StrAccum str; /* EQP output string */
135521 char zBuf[100]; /* Initial space for EQP output string */
135522
135523 pLoop = pLevel->pWLoop;
135524 flags = pLoop->wsFlags;
135525 if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_OR_SUBCLAUSE) ) return 0;
135526
135527 isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
135528 || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
135529 || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
135530
135531 sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
135532 sqlite3_str_appendall(&str, isSearch ? "SEARCH" : "SCAN");
135533 if( pItem->pSelect ){
135534 sqlite3_str_appendf(&str, " SUBQUERY %u", pItem->pSelect->selId);
135535 }else{
135536 sqlite3_str_appendf(&str, " TABLE %s", pItem->zName);
135537 }
135538
135539 if( pItem->zAlias ){
135540 sqlite3_str_appendf(&str, " AS %s", pItem->zAlias);
135541 }
135542 if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
135543 const char *zFmt = 0;
135544 Index *pIdx;
135545
135546 assert( pLoop->u.btree.pIndex!=0 );
135547 pIdx = pLoop->u.btree.pIndex;
135548 assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
135549 if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
135550 if( isSearch ){
135551 zFmt = "PRIMARY KEY";
135552 }
135553 }else if( flags & WHERE_PARTIALIDX ){
135554 zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
135555 }else if( flags & WHERE_AUTO_INDEX ){
135556 zFmt = "AUTOMATIC COVERING INDEX";
135557 }else if( flags & WHERE_IDX_ONLY ){
135558 zFmt = "COVERING INDEX %s";
135559 }else{
135560 zFmt = "INDEX %s";
135561 }
135562 if( zFmt ){
135563 sqlite3_str_append(&str, " USING ", 7);
135564 sqlite3_str_appendf(&str, zFmt, pIdx->zName);
135565 explainIndexRange(&str, pLoop);
135566 }
135567 }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
135568 const char *zRangeOp;
135569 if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
135570 zRangeOp = "=";
135571 }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
135572 zRangeOp = ">? AND rowid<";
135573 }else if( flags&WHERE_BTM_LIMIT ){
135574 zRangeOp = ">";
135575 }else{
135576 assert( flags&WHERE_TOP_LIMIT);
135577 zRangeOp = "<";
135578 }
135579 sqlite3_str_appendf(&str,
135580 " USING INTEGER PRIMARY KEY (rowid%s?)",zRangeOp);
135581 }
135582#ifndef SQLITE_OMIT_VIRTUALTABLE
135583 else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
135584 sqlite3_str_appendf(&str, " VIRTUAL TABLE INDEX %d:%s",
135585 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
135586 }
135587#endif
135588#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
135589 if( pLoop->nOut>=10 ){
135590 sqlite3_str_appendf(&str, " (~%llu rows)",
135591 sqlite3LogEstToInt(pLoop->nOut));
135592 }else{
135593 sqlite3_str_append(&str, " (~1 row)", 9);
135594 }
135595#endif
135596 zMsg = sqlite3StrAccumFinish(&str);
135597 ret = sqlite3VdbeAddOp4(v, OP_Explain, sqlite3VdbeCurrentAddr(v),
135598 pParse->addrExplain, 0, zMsg,P4_DYNAMIC);
135599 }
135600 return ret;
135601}
135602#endif /* SQLITE_OMIT_EXPLAIN */
135603
135604#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
135605/*
135606** Configure the VM passed as the first argument with an
135607** sqlite3_stmt_scanstatus() entry corresponding to the scan used to
135608** implement level pLvl. Argument pSrclist is a pointer to the FROM
135609** clause that the scan reads data from.
135610**
135611** If argument addrExplain is not 0, it must be the address of an
135612** OP_Explain instruction that describes the same loop.
135613*/
135614SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
135615 Vdbe *v, /* Vdbe to add scanstatus entry to */
135616 SrcList *pSrclist, /* FROM clause pLvl reads data from */
135617 WhereLevel *pLvl, /* Level to add scanstatus() entry for */
135618 int addrExplain /* Address of OP_Explain (or 0) */
135619){
135620 const char *zObj = 0;
135621 WhereLoop *pLoop = pLvl->pWLoop;
135622 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){
135623 zObj = pLoop->u.btree.pIndex->zName;
135624 }else{
135625 zObj = pSrclist->a[pLvl->iFrom].zName;
135626 }
135627 sqlite3VdbeScanStatus(
135628 v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
135629 );
135630}
135631#endif
135632
135633
135634/*
135635** Disable a term in the WHERE clause. Except, do not disable the term
135636** if it controls a LEFT OUTER JOIN and it did not originate in the ON
135637** or USING clause of that join.
135638**
135639** Consider the term t2.z='ok' in the following queries:
135640**
135641** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
135642** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
135643** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
135644**
135645** The t2.z='ok' is disabled in the in (2) because it originates
135646** in the ON clause. The term is disabled in (3) because it is not part
135647** of a LEFT OUTER JOIN. In (1), the term is not disabled.
135648**
135649** Disabling a term causes that term to not be tested in the inner loop
135650** of the join. Disabling is an optimization. When terms are satisfied
135651** by indices, we disable them to prevent redundant tests in the inner
135652** loop. We would get the correct results if nothing were ever disabled,
135653** but joins might run a little slower. The trick is to disable as much
135654** as we can without disabling too much. If we disabled in (1), we'd get
135655** the wrong answer. See ticket #813.
135656**
135657** If all the children of a term are disabled, then that term is also
135658** automatically disabled. In this way, terms get disabled if derived
135659** virtual terms are tested first. For example:
135660**
135661** x GLOB 'abc*' AND x>='abc' AND x<'acd'
135662** \___________/ \______/ \_____/
135663** parent child1 child2
135664**
135665** Only the parent term was in the original WHERE clause. The child1
135666** and child2 terms were added by the LIKE optimization. If both of
135667** the virtual child terms are valid, then testing of the parent can be
135668** skipped.
135669**
135670** Usually the parent term is marked as TERM_CODED. But if the parent
135671** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
135672** The TERM_LIKECOND marking indicates that the term should be coded inside
135673** a conditional such that is only evaluated on the second pass of a
135674** LIKE-optimization loop, when scanning BLOBs instead of strings.
135675*/
135676static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
135677 int nLoop = 0;
135678 assert( pTerm!=0 );
135679 while( (pTerm->wtFlags & TERM_CODED)==0
135680 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
135681 && (pLevel->notReady & pTerm->prereqAll)==0
135682 ){
135683 if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
135684 pTerm->wtFlags |= TERM_LIKECOND;
135685 }else{
135686 pTerm->wtFlags |= TERM_CODED;
135687 }
135688 if( pTerm->iParent<0 ) break;
135689 pTerm = &pTerm->pWC->a[pTerm->iParent];
135690 assert( pTerm!=0 );
135691 pTerm->nChild--;
135692 if( pTerm->nChild!=0 ) break;
135693 nLoop++;
135694 }
135695}
135696
135697/*
135698** Code an OP_Affinity opcode to apply the column affinity string zAff
135699** to the n registers starting at base.
135700**
135701** As an optimization, SQLITE_AFF_BLOB entries (which are no-ops) at the
135702** beginning and end of zAff are ignored. If all entries in zAff are
135703** SQLITE_AFF_BLOB, then no code gets generated.
135704**
135705** This routine makes its own copy of zAff so that the caller is free
135706** to modify zAff after this routine returns.
135707*/
135708static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
135709 Vdbe *v = pParse->pVdbe;
135710 if( zAff==0 ){
135711 assert( pParse->db->mallocFailed );
135712 return;
135713 }
135714 assert( v!=0 );
135715
135716 /* Adjust base and n to skip over SQLITE_AFF_BLOB entries at the beginning
135717 ** and end of the affinity string.
135718 */
135719 while( n>0 && zAff[0]==SQLITE_AFF_BLOB ){
135720 n--;
135721 base++;
135722 zAff++;
135723 }
135724 while( n>1 && zAff[n-1]==SQLITE_AFF_BLOB ){
135725 n--;
135726 }
135727
135728 /* Code the OP_Affinity opcode if there is anything left to do. */
135729 if( n>0 ){
135730 sqlite3VdbeAddOp4(v, OP_Affinity, base, n, 0, zAff, n);
135731 }
135732}
135733
135734/*
135735** Expression pRight, which is the RHS of a comparison operation, is
135736** either a vector of n elements or, if n==1, a scalar expression.
135737** Before the comparison operation, affinity zAff is to be applied
135738** to the pRight values. This function modifies characters within the
135739** affinity string to SQLITE_AFF_BLOB if either:
135740**
135741** * the comparison will be performed with no affinity, or
135742** * the affinity change in zAff is guaranteed not to change the value.
135743*/
135744static void updateRangeAffinityStr(
135745 Expr *pRight, /* RHS of comparison */
135746 int n, /* Number of vector elements in comparison */
135747 char *zAff /* Affinity string to modify */
135748){
135749 int i;
135750 for(i=0; i<n; i++){
135751 Expr *p = sqlite3VectorFieldSubexpr(pRight, i);
135752 if( sqlite3CompareAffinity(p, zAff[i])==SQLITE_AFF_BLOB
135753 || sqlite3ExprNeedsNoAffinityChange(p, zAff[i])
135754 ){
135755 zAff[i] = SQLITE_AFF_BLOB;
135756 }
135757 }
135758}
135759
135760
135761/*
135762** pX is an expression of the form: (vector) IN (SELECT ...)
135763** In other words, it is a vector IN operator with a SELECT clause on the
135764** LHS. But not all terms in the vector are indexable and the terms might
135765** not be in the correct order for indexing.
135766**
135767** This routine makes a copy of the input pX expression and then adjusts
135768** the vector on the LHS with corresponding changes to the SELECT so that
135769** the vector contains only index terms and those terms are in the correct
135770** order. The modified IN expression is returned. The caller is responsible
135771** for deleting the returned expression.
135772**
135773** Example:
135774**
135775** CREATE TABLE t1(a,b,c,d,e,f);
135776** CREATE INDEX t1x1 ON t1(e,c);
135777** SELECT * FROM t1 WHERE (a,b,c,d,e) IN (SELECT v,w,x,y,z FROM t2)
135778** \_______________________________________/
135779** The pX expression
135780**
135781** Since only columns e and c can be used with the index, in that order,
135782** the modified IN expression that is returned will be:
135783**
135784** (e,c) IN (SELECT z,x FROM t2)
135785**
135786** The reduced pX is different from the original (obviously) and thus is
135787** only used for indexing, to improve performance. The original unaltered
135788** IN expression must also be run on each output row for correctness.
135789*/
135790static Expr *removeUnindexableInClauseTerms(
135791 Parse *pParse, /* The parsing context */
135792 int iEq, /* Look at loop terms starting here */
135793 WhereLoop *pLoop, /* The current loop */
135794 Expr *pX /* The IN expression to be reduced */
135795){
135796 sqlite3 *db = pParse->db;
135797 Expr *pNew = sqlite3ExprDup(db, pX, 0);
135798 if( db->mallocFailed==0 ){
135799 ExprList *pOrigRhs = pNew->x.pSelect->pEList; /* Original unmodified RHS */
135800 ExprList *pOrigLhs = pNew->pLeft->x.pList; /* Original unmodified LHS */
135801 ExprList *pRhs = 0; /* New RHS after modifications */
135802 ExprList *pLhs = 0; /* New LHS after mods */
135803 int i; /* Loop counter */
135804 Select *pSelect; /* Pointer to the SELECT on the RHS */
135805
135806 for(i=iEq; i<pLoop->nLTerm; i++){
135807 if( pLoop->aLTerm[i]->pExpr==pX ){
135808 int iField = pLoop->aLTerm[i]->iField - 1;
135809 if( pOrigRhs->a[iField].pExpr==0 ) continue; /* Duplicate PK column */
135810 pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr);
135811 pOrigRhs->a[iField].pExpr = 0;
135812 assert( pOrigLhs->a[iField].pExpr!=0 );
135813 pLhs = sqlite3ExprListAppend(pParse, pLhs, pOrigLhs->a[iField].pExpr);
135814 pOrigLhs->a[iField].pExpr = 0;
135815 }
135816 }
135817 sqlite3ExprListDelete(db, pOrigRhs);
135818 sqlite3ExprListDelete(db, pOrigLhs);
135819 pNew->pLeft->x.pList = pLhs;
135820 pNew->x.pSelect->pEList = pRhs;
135821 if( pLhs && pLhs->nExpr==1 ){
135822 /* Take care here not to generate a TK_VECTOR containing only a
135823 ** single value. Since the parser never creates such a vector, some
135824 ** of the subroutines do not handle this case. */
135825 Expr *p = pLhs->a[0].pExpr;
135826 pLhs->a[0].pExpr = 0;
135827 sqlite3ExprDelete(db, pNew->pLeft);
135828 pNew->pLeft = p;
135829 }
135830 pSelect = pNew->x.pSelect;
135831 if( pSelect->pOrderBy ){
135832 /* If the SELECT statement has an ORDER BY clause, zero the
135833 ** iOrderByCol variables. These are set to non-zero when an
135834 ** ORDER BY term exactly matches one of the terms of the
135835 ** result-set. Since the result-set of the SELECT statement may
135836 ** have been modified or reordered, these variables are no longer
135837 ** set correctly. Since setting them is just an optimization,
135838 ** it's easiest just to zero them here. */
135839 ExprList *pOrderBy = pSelect->pOrderBy;
135840 for(i=0; i<pOrderBy->nExpr; i++){
135841 pOrderBy->a[i].u.x.iOrderByCol = 0;
135842 }
135843 }
135844
135845#if 0
135846 printf("For indexing, change the IN expr:\n");
135847 sqlite3TreeViewExpr(0, pX, 0);
135848 printf("Into:\n");
135849 sqlite3TreeViewExpr(0, pNew, 0);
135850#endif
135851 }
135852 return pNew;
135853}
135854
135855
135856/*
135857** Generate code for a single equality term of the WHERE clause. An equality
135858** term can be either X=expr or X IN (...). pTerm is the term to be
135859** coded.
135860**
135861** The current value for the constraint is left in a register, the index
135862** of which is returned. An attempt is made store the result in iTarget but
135863** this is only guaranteed for TK_ISNULL and TK_IN constraints. If the
135864** constraint is a TK_EQ or TK_IS, then the current value might be left in
135865** some other register and it is the caller's responsibility to compensate.
135866**
135867** For a constraint of the form X=expr, the expression is evaluated in
135868** straight-line code. For constraints of the form X IN (...)
135869** this routine sets up a loop that will iterate over all values of X.
135870*/
135871static int codeEqualityTerm(
135872 Parse *pParse, /* The parsing context */
135873 WhereTerm *pTerm, /* The term of the WHERE clause to be coded */
135874 WhereLevel *pLevel, /* The level of the FROM clause we are working on */
135875 int iEq, /* Index of the equality term within this level */
135876 int bRev, /* True for reverse-order IN operations */
135877 int iTarget /* Attempt to leave results in this register */
135878){
135879 Expr *pX = pTerm->pExpr;
135880 Vdbe *v = pParse->pVdbe;
135881 int iReg; /* Register holding results */
135882
135883 assert( pLevel->pWLoop->aLTerm[iEq]==pTerm );
135884 assert( iTarget>0 );
135885 if( pX->op==TK_EQ || pX->op==TK_IS ){
135886 iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
135887 }else if( pX->op==TK_ISNULL ){
135888 iReg = iTarget;
135889 sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
135890#ifndef SQLITE_OMIT_SUBQUERY
135891 }else{
135892 int eType = IN_INDEX_NOOP;
135893 int iTab;
135894 struct InLoop *pIn;
135895 WhereLoop *pLoop = pLevel->pWLoop;
135896 int i;
135897 int nEq = 0;
135898 int *aiMap = 0;
135899
135900 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
135901 && pLoop->u.btree.pIndex!=0
135902 && pLoop->u.btree.pIndex->aSortOrder[iEq]
135903 ){
135904 testcase( iEq==0 );
135905 testcase( bRev );
135906 bRev = !bRev;
135907 }
135908 assert( pX->op==TK_IN );
135909 iReg = iTarget;
135910
135911 for(i=0; i<iEq; i++){
135912 if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){
135913 disableTerm(pLevel, pTerm);
135914 return iTarget;
135915 }
135916 }
135917 for(i=iEq;i<pLoop->nLTerm; i++){
135918 assert( pLoop->aLTerm[i]!=0 );
135919 if( pLoop->aLTerm[i]->pExpr==pX ) nEq++;
135920 }
135921
135922 if( (pX->flags & EP_xIsSelect)==0 || pX->x.pSelect->pEList->nExpr==1 ){
135923 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, 0);
135924 }else{
135925 sqlite3 *db = pParse->db;
135926 pX = removeUnindexableInClauseTerms(pParse, iEq, pLoop, pX);
135927
135928 if( !db->mallocFailed ){
135929 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
135930 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, aiMap);
135931 pTerm->pExpr->iTable = pX->iTable;
135932 }
135933 sqlite3ExprDelete(db, pX);
135934 pX = pTerm->pExpr;
135935 }
135936
135937 if( eType==IN_INDEX_INDEX_DESC ){
135938 testcase( bRev );
135939 bRev = !bRev;
135940 }
135941 iTab = pX->iTable;
135942 sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
135943 VdbeCoverageIf(v, bRev);
135944 VdbeCoverageIf(v, !bRev);
135945 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
135946
135947 pLoop->wsFlags |= WHERE_IN_ABLE;
135948 if( pLevel->u.in.nIn==0 ){
135949 pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
135950 }
135951
135952 i = pLevel->u.in.nIn;
135953 pLevel->u.in.nIn += nEq;
135954 pLevel->u.in.aInLoop =
135955 sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
135956 sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
135957 pIn = pLevel->u.in.aInLoop;
135958 if( pIn ){
135959 int iMap = 0; /* Index in aiMap[] */
135960 pIn += i;
135961 for(i=iEq;i<pLoop->nLTerm; i++){
135962 if( pLoop->aLTerm[i]->pExpr==pX ){
135963 int iOut = iReg + i - iEq;
135964 if( eType==IN_INDEX_ROWID ){
135965 testcase( nEq>1 ); /* Happens with a UNIQUE index on ROWID */
135966 pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iOut);
135967 }else{
135968 int iCol = aiMap ? aiMap[iMap++] : 0;
135969 pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column,iTab, iCol, iOut);
135970 }
135971 sqlite3VdbeAddOp1(v, OP_IsNull, iOut); VdbeCoverage(v);
135972 if( i==iEq ){
135973 pIn->iCur = iTab;
135974 pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next;
135975 if( iEq>0 && (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ){
135976 pIn->iBase = iReg - i;
135977 pIn->nPrefix = i;
135978 pLoop->wsFlags |= WHERE_IN_EARLYOUT;
135979 }else{
135980 pIn->nPrefix = 0;
135981 }
135982 }else{
135983 pIn->eEndLoopOp = OP_Noop;
135984 }
135985 pIn++;
135986 }
135987 }
135988 }else{
135989 pLevel->u.in.nIn = 0;
135990 }
135991 sqlite3DbFree(pParse->db, aiMap);
135992#endif
135993 }
135994 disableTerm(pLevel, pTerm);
135995 return iReg;
135996}
135997
135998/*
135999** Generate code that will evaluate all == and IN constraints for an
136000** index scan.
136001**
136002** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
136003** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10
136004** The index has as many as three equality constraints, but in this
136005** example, the third "c" value is an inequality. So only two
136006** constraints are coded. This routine will generate code to evaluate
136007** a==5 and b IN (1,2,3). The current values for a and b will be stored
136008** in consecutive registers and the index of the first register is returned.
136009**
136010** In the example above nEq==2. But this subroutine works for any value
136011** of nEq including 0. If nEq==0, this routine is nearly a no-op.
136012** The only thing it does is allocate the pLevel->iMem memory cell and
136013** compute the affinity string.
136014**
136015** The nExtraReg parameter is 0 or 1. It is 0 if all WHERE clause constraints
136016** are == or IN and are covered by the nEq. nExtraReg is 1 if there is
136017** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
136018** occurs after the nEq quality constraints.
136019**
136020** This routine allocates a range of nEq+nExtraReg memory cells and returns
136021** the index of the first memory cell in that range. The code that
136022** calls this routine will use that memory range to store keys for
136023** start and termination conditions of the loop.
136024** key value of the loop. If one or more IN operators appear, then
136025** this routine allocates an additional nEq memory cells for internal
136026** use.
136027**
136028** Before returning, *pzAff is set to point to a buffer containing a
136029** copy of the column affinity string of the index allocated using
136030** sqlite3DbMalloc(). Except, entries in the copy of the string associated
136031** with equality constraints that use BLOB or NONE affinity are set to
136032** SQLITE_AFF_BLOB. This is to deal with SQL such as the following:
136033**
136034** CREATE TABLE t1(a TEXT PRIMARY KEY, b);
136035** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
136036**
136037** In the example above, the index on t1(a) has TEXT affinity. But since
136038** the right hand side of the equality constraint (t2.b) has BLOB/NONE affinity,
136039** no conversion should be attempted before using a t2.b value as part of
136040** a key to search the index. Hence the first byte in the returned affinity
136041** string in this example would be set to SQLITE_AFF_BLOB.
136042*/
136043static int codeAllEqualityTerms(
136044 Parse *pParse, /* Parsing context */
136045 WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */
136046 int bRev, /* Reverse the order of IN operators */
136047 int nExtraReg, /* Number of extra registers to allocate */
136048 char **pzAff /* OUT: Set to point to affinity string */
136049){
136050 u16 nEq; /* The number of == or IN constraints to code */
136051 u16 nSkip; /* Number of left-most columns to skip */
136052 Vdbe *v = pParse->pVdbe; /* The vm under construction */
136053 Index *pIdx; /* The index being used for this loop */
136054 WhereTerm *pTerm; /* A single constraint term */
136055 WhereLoop *pLoop; /* The WhereLoop object */
136056 int j; /* Loop counter */
136057 int regBase; /* Base register */
136058 int nReg; /* Number of registers to allocate */
136059 char *zAff; /* Affinity string to return */
136060
136061 /* This module is only called on query plans that use an index. */
136062 pLoop = pLevel->pWLoop;
136063 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
136064 nEq = pLoop->u.btree.nEq;
136065 nSkip = pLoop->nSkip;
136066 pIdx = pLoop->u.btree.pIndex;
136067 assert( pIdx!=0 );
136068
136069 /* Figure out how many memory cells we will need then allocate them.
136070 */
136071 regBase = pParse->nMem + 1;
136072 nReg = pLoop->u.btree.nEq + nExtraReg;
136073 pParse->nMem += nReg;
136074
136075 zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx));
136076 assert( zAff!=0 || pParse->db->mallocFailed );
136077
136078 if( nSkip ){
136079 int iIdxCur = pLevel->iIdxCur;
136080 sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur);
136081 VdbeCoverageIf(v, bRev==0);
136082 VdbeCoverageIf(v, bRev!=0);
136083 VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
136084 j = sqlite3VdbeAddOp0(v, OP_Goto);
136085 pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
136086 iIdxCur, 0, regBase, nSkip);
136087 VdbeCoverageIf(v, bRev==0);
136088 VdbeCoverageIf(v, bRev!=0);
136089 sqlite3VdbeJumpHere(v, j);
136090 for(j=0; j<nSkip; j++){
136091 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j);
136092 testcase( pIdx->aiColumn[j]==XN_EXPR );
136093 VdbeComment((v, "%s", explainIndexColumnName(pIdx, j)));
136094 }
136095 }
136096
136097 /* Evaluate the equality constraints
136098 */
136099 assert( zAff==0 || (int)strlen(zAff)>=nEq );
136100 for(j=nSkip; j<nEq; j++){
136101 int r1;
136102 pTerm = pLoop->aLTerm[j];
136103 assert( pTerm!=0 );
136104 /* The following testcase is true for indices with redundant columns.
136105 ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
136106 testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
136107 testcase( pTerm->wtFlags & TERM_VIRTUAL );
136108 r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
136109 if( r1!=regBase+j ){
136110 if( nReg==1 ){
136111 sqlite3ReleaseTempReg(pParse, regBase);
136112 regBase = r1;
136113 }else{
136114 sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
136115 }
136116 }
136117 if( pTerm->eOperator & WO_IN ){
136118 if( pTerm->pExpr->flags & EP_xIsSelect ){
136119 /* No affinity ever needs to be (or should be) applied to a value
136120 ** from the RHS of an "? IN (SELECT ...)" expression. The
136121 ** sqlite3FindInIndex() routine has already ensured that the
136122 ** affinity of the comparison has been applied to the value. */
136123 if( zAff ) zAff[j] = SQLITE_AFF_BLOB;
136124 }
136125 }else if( (pTerm->eOperator & WO_ISNULL)==0 ){
136126 Expr *pRight = pTerm->pExpr->pRight;
136127 if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
136128 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
136129 VdbeCoverage(v);
136130 }
136131 if( zAff ){
136132 if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB ){
136133 zAff[j] = SQLITE_AFF_BLOB;
136134 }
136135 if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
136136 zAff[j] = SQLITE_AFF_BLOB;
136137 }
136138 }
136139 }
136140 }
136141 *pzAff = zAff;
136142 return regBase;
136143}
136144
136145#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
136146/*
136147** If the most recently coded instruction is a constant range constraint
136148** (a string literal) that originated from the LIKE optimization, then
136149** set P3 and P5 on the OP_String opcode so that the string will be cast
136150** to a BLOB at appropriate times.
136151**
136152** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
136153** expression: "x>='ABC' AND x<'abd'". But this requires that the range
136154** scan loop run twice, once for strings and a second time for BLOBs.
136155** The OP_String opcodes on the second pass convert the upper and lower
136156** bound string constants to blobs. This routine makes the necessary changes
136157** to the OP_String opcodes for that to happen.
136158**
136159** Except, of course, if SQLITE_LIKE_DOESNT_MATCH_BLOBS is defined, then
136160** only the one pass through the string space is required, so this routine
136161** becomes a no-op.
136162*/
136163static void whereLikeOptimizationStringFixup(
136164 Vdbe *v, /* prepared statement under construction */
136165 WhereLevel *pLevel, /* The loop that contains the LIKE operator */
136166 WhereTerm *pTerm /* The upper or lower bound just coded */
136167){
136168 if( pTerm->wtFlags & TERM_LIKEOPT ){
136169 VdbeOp *pOp;
136170 assert( pLevel->iLikeRepCntr>0 );
136171 pOp = sqlite3VdbeGetOp(v, -1);
136172 assert( pOp!=0 );
136173 assert( pOp->opcode==OP_String8
136174 || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
136175 pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */
136176 pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */
136177 }
136178}
136179#else
136180# define whereLikeOptimizationStringFixup(A,B,C)
136181#endif
136182
136183#ifdef SQLITE_ENABLE_CURSOR_HINTS
136184/*
136185** Information is passed from codeCursorHint() down to individual nodes of
136186** the expression tree (by sqlite3WalkExpr()) using an instance of this
136187** structure.
136188*/
136189struct CCurHint {
136190 int iTabCur; /* Cursor for the main table */
136191 int iIdxCur; /* Cursor for the index, if pIdx!=0. Unused otherwise */
136192 Index *pIdx; /* The index used to access the table */
136193};
136194
136195/*
136196** This function is called for every node of an expression that is a candidate
136197** for a cursor hint on an index cursor. For TK_COLUMN nodes that reference
136198** the table CCurHint.iTabCur, verify that the same column can be
136199** accessed through the index. If it cannot, then set pWalker->eCode to 1.
136200*/
136201static int codeCursorHintCheckExpr(Walker *pWalker, Expr *pExpr){
136202 struct CCurHint *pHint = pWalker->u.pCCurHint;
136203 assert( pHint->pIdx!=0 );
136204 if( pExpr->op==TK_COLUMN
136205 && pExpr->iTable==pHint->iTabCur
136206 && sqlite3ColumnOfIndex(pHint->pIdx, pExpr->iColumn)<0
136207 ){
136208 pWalker->eCode = 1;
136209 }
136210 return WRC_Continue;
136211}
136212
136213/*
136214** Test whether or not expression pExpr, which was part of a WHERE clause,
136215** should be included in the cursor-hint for a table that is on the rhs
136216** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the
136217** expression is not suitable.
136218**
136219** An expression is unsuitable if it might evaluate to non NULL even if
136220** a TK_COLUMN node that does affect the value of the expression is set
136221** to NULL. For example:
136222**
136223** col IS NULL
136224** col IS NOT NULL
136225** coalesce(col, 1)
136226** CASE WHEN col THEN 0 ELSE 1 END
136227*/
136228static int codeCursorHintIsOrFunction(Walker *pWalker, Expr *pExpr){
136229 if( pExpr->op==TK_IS
136230 || pExpr->op==TK_ISNULL || pExpr->op==TK_ISNOT
136231 || pExpr->op==TK_NOTNULL || pExpr->op==TK_CASE
136232 ){
136233 pWalker->eCode = 1;
136234 }else if( pExpr->op==TK_FUNCTION ){
136235 int d1;
136236 char d2[4];
136237 if( 0==sqlite3IsLikeFunction(pWalker->pParse->db, pExpr, &d1, d2) ){
136238 pWalker->eCode = 1;
136239 }
136240 }
136241
136242 return WRC_Continue;
136243}
136244
136245
136246/*
136247** This function is called on every node of an expression tree used as an
136248** argument to the OP_CursorHint instruction. If the node is a TK_COLUMN
136249** that accesses any table other than the one identified by
136250** CCurHint.iTabCur, then do the following:
136251**
136252** 1) allocate a register and code an OP_Column instruction to read
136253** the specified column into the new register, and
136254**
136255** 2) transform the expression node to a TK_REGISTER node that reads
136256** from the newly populated register.
136257**
136258** Also, if the node is a TK_COLUMN that does access the table idenified
136259** by pCCurHint.iTabCur, and an index is being used (which we will
136260** know because CCurHint.pIdx!=0) then transform the TK_COLUMN into
136261** an access of the index rather than the original table.
136262*/
136263static int codeCursorHintFixExpr(Walker *pWalker, Expr *pExpr){
136264 int rc = WRC_Continue;
136265 struct CCurHint *pHint = pWalker->u.pCCurHint;
136266 if( pExpr->op==TK_COLUMN ){
136267 if( pExpr->iTable!=pHint->iTabCur ){
136268 int reg = ++pWalker->pParse->nMem; /* Register for column value */
136269 sqlite3ExprCode(pWalker->pParse, pExpr, reg);
136270 pExpr->op = TK_REGISTER;
136271 pExpr->iTable = reg;
136272 }else if( pHint->pIdx!=0 ){
136273 pExpr->iTable = pHint->iIdxCur;
136274 pExpr->iColumn = sqlite3ColumnOfIndex(pHint->pIdx, pExpr->iColumn);
136275 assert( pExpr->iColumn>=0 );
136276 }
136277 }else if( pExpr->op==TK_AGG_FUNCTION ){
136278 /* An aggregate function in the WHERE clause of a query means this must
136279 ** be a correlated sub-query, and expression pExpr is an aggregate from
136280 ** the parent context. Do not walk the function arguments in this case.
136281 **
136282 ** todo: It should be possible to replace this node with a TK_REGISTER
136283 ** expression, as the result of the expression must be stored in a
136284 ** register at this point. The same holds for TK_AGG_COLUMN nodes. */
136285 rc = WRC_Prune;
136286 }
136287 return rc;
136288}
136289
136290/*
136291** Insert an OP_CursorHint instruction if it is appropriate to do so.
136292*/
136293static void codeCursorHint(
136294 struct SrcList_item *pTabItem, /* FROM clause item */
136295 WhereInfo *pWInfo, /* The where clause */
136296 WhereLevel *pLevel, /* Which loop to provide hints for */
136297 WhereTerm *pEndRange /* Hint this end-of-scan boundary term if not NULL */
136298){
136299 Parse *pParse = pWInfo->pParse;
136300 sqlite3 *db = pParse->db;
136301 Vdbe *v = pParse->pVdbe;
136302 Expr *pExpr = 0;
136303 WhereLoop *pLoop = pLevel->pWLoop;
136304 int iCur;
136305 WhereClause *pWC;
136306 WhereTerm *pTerm;
136307 int i, j;
136308 struct CCurHint sHint;
136309 Walker sWalker;
136310
136311 if( OptimizationDisabled(db, SQLITE_CursorHints) ) return;
136312 iCur = pLevel->iTabCur;
136313 assert( iCur==pWInfo->pTabList->a[pLevel->iFrom].iCursor );
136314 sHint.iTabCur = iCur;
136315 sHint.iIdxCur = pLevel->iIdxCur;
136316 sHint.pIdx = pLoop->u.btree.pIndex;
136317 memset(&sWalker, 0, sizeof(sWalker));
136318 sWalker.pParse = pParse;
136319 sWalker.u.pCCurHint = &sHint;
136320 pWC = &pWInfo->sWC;
136321 for(i=0; i<pWC->nTerm; i++){
136322 pTerm = &pWC->a[i];
136323 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
136324 if( pTerm->prereqAll & pLevel->notReady ) continue;
136325
136326 /* Any terms specified as part of the ON(...) clause for any LEFT
136327 ** JOIN for which the current table is not the rhs are omitted
136328 ** from the cursor-hint.
136329 **
136330 ** If this table is the rhs of a LEFT JOIN, "IS" or "IS NULL" terms
136331 ** that were specified as part of the WHERE clause must be excluded.
136332 ** This is to address the following:
136333 **
136334 ** SELECT ... t1 LEFT JOIN t2 ON (t1.a=t2.b) WHERE t2.c IS NULL;
136335 **
136336 ** Say there is a single row in t2 that matches (t1.a=t2.b), but its
136337 ** t2.c values is not NULL. If the (t2.c IS NULL) constraint is
136338 ** pushed down to the cursor, this row is filtered out, causing
136339 ** SQLite to synthesize a row of NULL values. Which does match the
136340 ** WHERE clause, and so the query returns a row. Which is incorrect.
136341 **
136342 ** For the same reason, WHERE terms such as:
136343 **
136344 ** WHERE 1 = (t2.c IS NULL)
136345 **
136346 ** are also excluded. See codeCursorHintIsOrFunction() for details.
136347 */
136348 if( pTabItem->fg.jointype & JT_LEFT ){
136349 Expr *pExpr = pTerm->pExpr;
136350 if( !ExprHasProperty(pExpr, EP_FromJoin)
136351 || pExpr->iRightJoinTable!=pTabItem->iCursor
136352 ){
136353 sWalker.eCode = 0;
136354 sWalker.xExprCallback = codeCursorHintIsOrFunction;
136355 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
136356 if( sWalker.eCode ) continue;
136357 }
136358 }else{
136359 if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) continue;
136360 }
136361
136362 /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize
136363 ** the cursor. These terms are not needed as hints for a pure range
136364 ** scan (that has no == terms) so omit them. */
136365 if( pLoop->u.btree.nEq==0 && pTerm!=pEndRange ){
136366 for(j=0; j<pLoop->nLTerm && pLoop->aLTerm[j]!=pTerm; j++){}
136367 if( j<pLoop->nLTerm ) continue;
136368 }
136369
136370 /* No subqueries or non-deterministic functions allowed */
136371 if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue;
136372
136373 /* For an index scan, make sure referenced columns are actually in
136374 ** the index. */
136375 if( sHint.pIdx!=0 ){
136376 sWalker.eCode = 0;
136377 sWalker.xExprCallback = codeCursorHintCheckExpr;
136378 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
136379 if( sWalker.eCode ) continue;
136380 }
136381
136382 /* If we survive all prior tests, that means this term is worth hinting */
136383 pExpr = sqlite3ExprAnd(db, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0));
136384 }
136385 if( pExpr!=0 ){
136386 sWalker.xExprCallback = codeCursorHintFixExpr;
136387 sqlite3WalkExpr(&sWalker, pExpr);
136388 sqlite3VdbeAddOp4(v, OP_CursorHint,
136389 (sHint.pIdx ? sHint.iIdxCur : sHint.iTabCur), 0, 0,
136390 (const char*)pExpr, P4_EXPR);
136391 }
136392}
136393#else
136394# define codeCursorHint(A,B,C,D) /* No-op */
136395#endif /* SQLITE_ENABLE_CURSOR_HINTS */
136396
136397/*
136398** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains
136399** a rowid value just read from cursor iIdxCur, open on index pIdx. This
136400** function generates code to do a deferred seek of cursor iCur to the
136401** rowid stored in register iRowid.
136402**
136403** Normally, this is just:
136404**
136405** OP_DeferredSeek $iCur $iRowid
136406**
136407** However, if the scan currently being coded is a branch of an OR-loop and
136408** the statement currently being coded is a SELECT, then P3 of OP_DeferredSeek
136409** is set to iIdxCur and P4 is set to point to an array of integers
136410** containing one entry for each column of the table cursor iCur is open
136411** on. For each table column, if the column is the i'th column of the
136412** index, then the corresponding array entry is set to (i+1). If the column
136413** does not appear in the index at all, the array entry is set to 0.
136414*/
136415static void codeDeferredSeek(
136416 WhereInfo *pWInfo, /* Where clause context */
136417 Index *pIdx, /* Index scan is using */
136418 int iCur, /* Cursor for IPK b-tree */
136419 int iIdxCur /* Index cursor */
136420){
136421 Parse *pParse = pWInfo->pParse; /* Parse context */
136422 Vdbe *v = pParse->pVdbe; /* Vdbe to generate code within */
136423
136424 assert( iIdxCur>0 );
136425 assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 );
136426
136427 sqlite3VdbeAddOp3(v, OP_DeferredSeek, iIdxCur, 0, iCur);
136428 if( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)
136429 && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask)
136430 ){
136431 int i;
136432 Table *pTab = pIdx->pTable;
136433 int *ai = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*(pTab->nCol+1));
136434 if( ai ){
136435 ai[0] = pTab->nCol;
136436 for(i=0; i<pIdx->nColumn-1; i++){
136437 assert( pIdx->aiColumn[i]<pTab->nCol );
136438 if( pIdx->aiColumn[i]>=0 ) ai[pIdx->aiColumn[i]+1] = i+1;
136439 }
136440 sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY);
136441 }
136442 }
136443}
136444
136445/*
136446** If the expression passed as the second argument is a vector, generate
136447** code to write the first nReg elements of the vector into an array
136448** of registers starting with iReg.
136449**
136450** If the expression is not a vector, then nReg must be passed 1. In
136451** this case, generate code to evaluate the expression and leave the
136452** result in register iReg.
136453*/
136454static void codeExprOrVector(Parse *pParse, Expr *p, int iReg, int nReg){
136455 assert( nReg>0 );
136456 if( p && sqlite3ExprIsVector(p) ){
136457#ifndef SQLITE_OMIT_SUBQUERY
136458 if( (p->flags & EP_xIsSelect) ){
136459 Vdbe *v = pParse->pVdbe;
136460 int iSelect = sqlite3CodeSubselect(pParse, p, 0, 0);
136461 sqlite3VdbeAddOp3(v, OP_Copy, iSelect, iReg, nReg-1);
136462 }else
136463#endif
136464 {
136465 int i;
136466 ExprList *pList = p->x.pList;
136467 assert( nReg<=pList->nExpr );
136468 for(i=0; i<nReg; i++){
136469 sqlite3ExprCode(pParse, pList->a[i].pExpr, iReg+i);
136470 }
136471 }
136472 }else{
136473 assert( nReg==1 );
136474 sqlite3ExprCode(pParse, p, iReg);
136475 }
136476}
136477
136478/* An instance of the IdxExprTrans object carries information about a
136479** mapping from an expression on table columns into a column in an index
136480** down through the Walker.
136481*/
136482typedef struct IdxExprTrans {
136483 Expr *pIdxExpr; /* The index expression */
136484 int iTabCur; /* The cursor of the corresponding table */
136485 int iIdxCur; /* The cursor for the index */
136486 int iIdxCol; /* The column for the index */
136487} IdxExprTrans;
136488
136489/* The walker node callback used to transform matching expressions into
136490** a reference to an index column for an index on an expression.
136491**
136492** If pExpr matches, then transform it into a reference to the index column
136493** that contains the value of pExpr.
136494*/
136495static int whereIndexExprTransNode(Walker *p, Expr *pExpr){
136496 IdxExprTrans *pX = p->u.pIdxTrans;
136497 if( sqlite3ExprCompare(0, pExpr, pX->pIdxExpr, pX->iTabCur)==0 ){
136498 pExpr->op = TK_COLUMN;
136499 pExpr->iTable = pX->iIdxCur;
136500 pExpr->iColumn = pX->iIdxCol;
136501 pExpr->y.pTab = 0;
136502 return WRC_Prune;
136503 }else{
136504 return WRC_Continue;
136505 }
136506}
136507
136508/*
136509** For an indexes on expression X, locate every instance of expression X
136510** in pExpr and change that subexpression into a reference to the appropriate
136511** column of the index.
136512*/
136513static void whereIndexExprTrans(
136514 Index *pIdx, /* The Index */
136515 int iTabCur, /* Cursor of the table that is being indexed */
136516 int iIdxCur, /* Cursor of the index itself */
136517 WhereInfo *pWInfo /* Transform expressions in this WHERE clause */
136518){
136519 int iIdxCol; /* Column number of the index */
136520 ExprList *aColExpr; /* Expressions that are indexed */
136521 Walker w;
136522 IdxExprTrans x;
136523 aColExpr = pIdx->aColExpr;
136524 if( aColExpr==0 ) return; /* Not an index on expressions */
136525 memset(&w, 0, sizeof(w));
136526 w.xExprCallback = whereIndexExprTransNode;
136527 w.u.pIdxTrans = &x;
136528 x.iTabCur = iTabCur;
136529 x.iIdxCur = iIdxCur;
136530 for(iIdxCol=0; iIdxCol<aColExpr->nExpr; iIdxCol++){
136531 if( pIdx->aiColumn[iIdxCol]!=XN_EXPR ) continue;
136532 assert( aColExpr->a[iIdxCol].pExpr!=0 );
136533 x.iIdxCol = iIdxCol;
136534 x.pIdxExpr = aColExpr->a[iIdxCol].pExpr;
136535 sqlite3WalkExpr(&w, pWInfo->pWhere);
136536 sqlite3WalkExprList(&w, pWInfo->pOrderBy);
136537 sqlite3WalkExprList(&w, pWInfo->pResultSet);
136538 }
136539}
136540
136541/*
136542** Generate code for the start of the iLevel-th loop in the WHERE clause
136543** implementation described by pWInfo.
136544*/
136545SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
136546 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
136547 int iLevel, /* Which level of pWInfo->a[] should be coded */
136548 Bitmask notReady /* Which tables are currently available */
136549){
136550 int j, k; /* Loop counters */
136551 int iCur; /* The VDBE cursor for the table */
136552 int addrNxt; /* Where to jump to continue with the next IN case */
136553 int omitTable; /* True if we use the index only */
136554 int bRev; /* True if we need to scan in reverse order */
136555 WhereLevel *pLevel; /* The where level to be coded */
136556 WhereLoop *pLoop; /* The WhereLoop object being coded */
136557 WhereClause *pWC; /* Decomposition of the entire WHERE clause */
136558 WhereTerm *pTerm; /* A WHERE clause term */
136559 Parse *pParse; /* Parsing context */
136560 sqlite3 *db; /* Database connection */
136561 Vdbe *v; /* The prepared stmt under constructions */
136562 struct SrcList_item *pTabItem; /* FROM clause term being coded */
136563 int addrBrk; /* Jump here to break out of the loop */
136564 int addrHalt; /* addrBrk for the outermost loop */
136565 int addrCont; /* Jump here to continue with next cycle */
136566 int iRowidReg = 0; /* Rowid is stored in this register, if not zero */
136567 int iReleaseReg = 0; /* Temp register to free before returning */
136568 Index *pIdx = 0; /* Index used by loop (if any) */
136569 int iLoop; /* Iteration of constraint generator loop */
136570
136571 pParse = pWInfo->pParse;
136572 v = pParse->pVdbe;
136573 pWC = &pWInfo->sWC;
136574 db = pParse->db;
136575 pLevel = &pWInfo->a[iLevel];
136576 pLoop = pLevel->pWLoop;
136577 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
136578 iCur = pTabItem->iCursor;
136579 pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
136580 bRev = (pWInfo->revMask>>iLevel)&1;
136581 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
136582 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0;
136583 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
136584
136585 /* Create labels for the "break" and "continue" instructions
136586 ** for the current loop. Jump to addrBrk to break out of a loop.
136587 ** Jump to cont to go immediately to the next iteration of the
136588 ** loop.
136589 **
136590 ** When there is an IN operator, we also have a "addrNxt" label that
136591 ** means to continue with the next IN value combination. When
136592 ** there are no IN operators in the constraints, the "addrNxt" label
136593 ** is the same as "addrBrk".
136594 */
136595 addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
136596 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
136597
136598 /* If this is the right table of a LEFT OUTER JOIN, allocate and
136599 ** initialize a memory cell that records if this table matches any
136600 ** row of the left table of the join.
136601 */
136602 assert( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)
136603 || pLevel->iFrom>0 || (pTabItem[0].fg.jointype & JT_LEFT)==0
136604 );
136605 if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){
136606 pLevel->iLeftJoin = ++pParse->nMem;
136607 sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
136608 VdbeComment((v, "init LEFT JOIN no-match flag"));
136609 }
136610
136611 /* Compute a safe address to jump to if we discover that the table for
136612 ** this loop is empty and can never contribute content. */
136613 for(j=iLevel; j>0 && pWInfo->a[j].iLeftJoin==0; j--){}
136614 addrHalt = pWInfo->a[j].addrBrk;
136615
136616 /* Special case of a FROM clause subquery implemented as a co-routine */
136617 if( pTabItem->fg.viaCoroutine ){
136618 int regYield = pTabItem->regReturn;
136619 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
136620 pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
136621 VdbeCoverage(v);
136622 VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
136623 pLevel->op = OP_Goto;
136624 }else
136625
136626#ifndef SQLITE_OMIT_VIRTUALTABLE
136627 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
136628 /* Case 1: The table is a virtual-table. Use the VFilter and VNext
136629 ** to access the data.
136630 */
136631 int iReg; /* P3 Value for OP_VFilter */
136632 int addrNotFound;
136633 int nConstraint = pLoop->nLTerm;
136634 int iIn; /* Counter for IN constraints */
136635
136636 iReg = sqlite3GetTempRange(pParse, nConstraint+2);
136637 addrNotFound = pLevel->addrBrk;
136638 for(j=0; j<nConstraint; j++){
136639 int iTarget = iReg+j+2;
136640 pTerm = pLoop->aLTerm[j];
136641 if( NEVER(pTerm==0) ) continue;
136642 if( pTerm->eOperator & WO_IN ){
136643 codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
136644 addrNotFound = pLevel->addrNxt;
136645 }else{
136646 Expr *pRight = pTerm->pExpr->pRight;
136647 codeExprOrVector(pParse, pRight, iTarget, 1);
136648 }
136649 }
136650 sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
136651 sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
136652 sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
136653 pLoop->u.vtab.idxStr,
136654 pLoop->u.vtab.needFree ? P4_DYNAMIC : P4_STATIC);
136655 VdbeCoverage(v);
136656 pLoop->u.vtab.needFree = 0;
136657 pLevel->p1 = iCur;
136658 pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext;
136659 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
136660 iIn = pLevel->u.in.nIn;
136661 for(j=nConstraint-1; j>=0; j--){
136662 pTerm = pLoop->aLTerm[j];
136663 if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){
136664 disableTerm(pLevel, pTerm);
136665 }else if( (pTerm->eOperator & WO_IN)!=0 ){
136666 Expr *pCompare; /* The comparison operator */
136667 Expr *pRight; /* RHS of the comparison */
136668 VdbeOp *pOp; /* Opcode to access the value of the IN constraint */
136669
136670 /* Reload the constraint value into reg[iReg+j+2]. The same value
136671 ** was loaded into the same register prior to the OP_VFilter, but
136672 ** the xFilter implementation might have changed the datatype or
136673 ** encoding of the value in the register, so it *must* be reloaded. */
136674 assert( pLevel->u.in.aInLoop!=0 || db->mallocFailed );
136675 if( !db->mallocFailed ){
136676 assert( iIn>0 );
136677 pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[--iIn].addrInTop);
136678 assert( pOp->opcode==OP_Column || pOp->opcode==OP_Rowid );
136679 assert( pOp->opcode!=OP_Column || pOp->p3==iReg+j+2 );
136680 assert( pOp->opcode!=OP_Rowid || pOp->p2==iReg+j+2 );
136681 testcase( pOp->opcode==OP_Rowid );
136682 sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3);
136683 }
136684
136685 /* Generate code that will continue to the next row if
136686 ** the IN constraint is not satisfied */
136687 pCompare = sqlite3PExpr(pParse, TK_EQ, 0, 0);
136688 assert( pCompare!=0 || db->mallocFailed );
136689 if( pCompare ){
136690 pCompare->pLeft = pTerm->pExpr->pLeft;
136691 pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0);
136692 if( pRight ){
136693 pRight->iTable = iReg+j+2;
136694 sqlite3ExprIfFalse(pParse, pCompare, pLevel->addrCont, 0);
136695 }
136696 pCompare->pLeft = 0;
136697 sqlite3ExprDelete(db, pCompare);
136698 }
136699 }
136700 }
136701 /* These registers need to be preserved in case there is an IN operator
136702 ** loop. So we could deallocate the registers here (and potentially
136703 ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems
136704 ** simpler and safer to simply not reuse the registers.
136705 **
136706 ** sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
136707 */
136708 }else
136709#endif /* SQLITE_OMIT_VIRTUALTABLE */
136710
136711 if( (pLoop->wsFlags & WHERE_IPK)!=0
136712 && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
136713 ){
136714 /* Case 2: We can directly reference a single row using an
136715 ** equality comparison against the ROWID field. Or
136716 ** we reference multiple rows using a "rowid IN (...)"
136717 ** construct.
136718 */
136719 assert( pLoop->u.btree.nEq==1 );
136720 pTerm = pLoop->aLTerm[0];
136721 assert( pTerm!=0 );
136722 assert( pTerm->pExpr!=0 );
136723 assert( omitTable==0 );
136724 testcase( pTerm->wtFlags & TERM_VIRTUAL );
136725 iReleaseReg = ++pParse->nMem;
136726 iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
136727 if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
136728 addrNxt = pLevel->addrNxt;
136729 sqlite3VdbeAddOp3(v, OP_SeekRowid, iCur, addrNxt, iRowidReg);
136730 VdbeCoverage(v);
136731 pLevel->op = OP_Noop;
136732 }else if( (pLoop->wsFlags & WHERE_IPK)!=0
136733 && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
136734 ){
136735 /* Case 3: We have an inequality comparison against the ROWID field.
136736 */
136737 int testOp = OP_Noop;
136738 int start;
136739 int memEndValue = 0;
136740 WhereTerm *pStart, *pEnd;
136741
136742 assert( omitTable==0 );
136743 j = 0;
136744 pStart = pEnd = 0;
136745 if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
136746 if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
136747 assert( pStart!=0 || pEnd!=0 );
136748 if( bRev ){
136749 pTerm = pStart;
136750 pStart = pEnd;
136751 pEnd = pTerm;
136752 }
136753 codeCursorHint(pTabItem, pWInfo, pLevel, pEnd);
136754 if( pStart ){
136755 Expr *pX; /* The expression that defines the start bound */
136756 int r1, rTemp; /* Registers for holding the start boundary */
136757 int op; /* Cursor seek operation */
136758
136759 /* The following constant maps TK_xx codes into corresponding
136760 ** seek opcodes. It depends on a particular ordering of TK_xx
136761 */
136762 const u8 aMoveOp[] = {
136763 /* TK_GT */ OP_SeekGT,
136764 /* TK_LE */ OP_SeekLE,
136765 /* TK_LT */ OP_SeekLT,
136766 /* TK_GE */ OP_SeekGE
136767 };
136768 assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */
136769 assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */
136770 assert( TK_GE==TK_GT+3 ); /* ... is correcct. */
136771
136772 assert( (pStart->wtFlags & TERM_VNULL)==0 );
136773 testcase( pStart->wtFlags & TERM_VIRTUAL );
136774 pX = pStart->pExpr;
136775 assert( pX!=0 );
136776 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
136777 if( sqlite3ExprIsVector(pX->pRight) ){
136778 r1 = rTemp = sqlite3GetTempReg(pParse);
136779 codeExprOrVector(pParse, pX->pRight, r1, 1);
136780 testcase( pX->op==TK_GT );
136781 testcase( pX->op==TK_GE );
136782 testcase( pX->op==TK_LT );
136783 testcase( pX->op==TK_LE );
136784 op = aMoveOp[((pX->op - TK_GT - 1) & 0x3) | 0x1];
136785 assert( pX->op!=TK_GT || op==OP_SeekGE );
136786 assert( pX->op!=TK_GE || op==OP_SeekGE );
136787 assert( pX->op!=TK_LT || op==OP_SeekLE );
136788 assert( pX->op!=TK_LE || op==OP_SeekLE );
136789 }else{
136790 r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
136791 disableTerm(pLevel, pStart);
136792 op = aMoveOp[(pX->op - TK_GT)];
136793 }
136794 sqlite3VdbeAddOp3(v, op, iCur, addrBrk, r1);
136795 VdbeComment((v, "pk"));
136796 VdbeCoverageIf(v, pX->op==TK_GT);
136797 VdbeCoverageIf(v, pX->op==TK_LE);
136798 VdbeCoverageIf(v, pX->op==TK_LT);
136799 VdbeCoverageIf(v, pX->op==TK_GE);
136800 sqlite3ReleaseTempReg(pParse, rTemp);
136801 }else{
136802 sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrHalt);
136803 VdbeCoverageIf(v, bRev==0);
136804 VdbeCoverageIf(v, bRev!=0);
136805 }
136806 if( pEnd ){
136807 Expr *pX;
136808 pX = pEnd->pExpr;
136809 assert( pX!=0 );
136810 assert( (pEnd->wtFlags & TERM_VNULL)==0 );
136811 testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
136812 testcase( pEnd->wtFlags & TERM_VIRTUAL );
136813 memEndValue = ++pParse->nMem;
136814 codeExprOrVector(pParse, pX->pRight, memEndValue, 1);
136815 if( 0==sqlite3ExprIsVector(pX->pRight)
136816 && (pX->op==TK_LT || pX->op==TK_GT)
136817 ){
136818 testOp = bRev ? OP_Le : OP_Ge;
136819 }else{
136820 testOp = bRev ? OP_Lt : OP_Gt;
136821 }
136822 if( 0==sqlite3ExprIsVector(pX->pRight) ){
136823 disableTerm(pLevel, pEnd);
136824 }
136825 }
136826 start = sqlite3VdbeCurrentAddr(v);
136827 pLevel->op = bRev ? OP_Prev : OP_Next;
136828 pLevel->p1 = iCur;
136829 pLevel->p2 = start;
136830 assert( pLevel->p5==0 );
136831 if( testOp!=OP_Noop ){
136832 iRowidReg = ++pParse->nMem;
136833 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
136834 sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
136835 VdbeCoverageIf(v, testOp==OP_Le);
136836 VdbeCoverageIf(v, testOp==OP_Lt);
136837 VdbeCoverageIf(v, testOp==OP_Ge);
136838 VdbeCoverageIf(v, testOp==OP_Gt);
136839 sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
136840 }
136841 }else if( pLoop->wsFlags & WHERE_INDEXED ){
136842 /* Case 4: A scan using an index.
136843 **
136844 ** The WHERE clause may contain zero or more equality
136845 ** terms ("==" or "IN" operators) that refer to the N
136846 ** left-most columns of the index. It may also contain
136847 ** inequality constraints (>, <, >= or <=) on the indexed
136848 ** column that immediately follows the N equalities. Only
136849 ** the right-most column can be an inequality - the rest must
136850 ** use the "==" and "IN" operators. For example, if the
136851 ** index is on (x,y,z), then the following clauses are all
136852 ** optimized:
136853 **
136854 ** x=5
136855 ** x=5 AND y=10
136856 ** x=5 AND y<10
136857 ** x=5 AND y>5 AND y<10
136858 ** x=5 AND y=5 AND z<=10
136859 **
136860 ** The z<10 term of the following cannot be used, only
136861 ** the x=5 term:
136862 **
136863 ** x=5 AND z<10
136864 **
136865 ** N may be zero if there are inequality constraints.
136866 ** If there are no inequality constraints, then N is at
136867 ** least one.
136868 **
136869 ** This case is also used when there are no WHERE clause
136870 ** constraints but an index is selected anyway, in order
136871 ** to force the output order to conform to an ORDER BY.
136872 */
136873 static const u8 aStartOp[] = {
136874 0,
136875 0,
136876 OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */
136877 OP_Last, /* 3: (!start_constraints && startEq && bRev) */
136878 OP_SeekGT, /* 4: (start_constraints && !startEq && !bRev) */
136879 OP_SeekLT, /* 5: (start_constraints && !startEq && bRev) */
136880 OP_SeekGE, /* 6: (start_constraints && startEq && !bRev) */
136881 OP_SeekLE /* 7: (start_constraints && startEq && bRev) */
136882 };
136883 static const u8 aEndOp[] = {
136884 OP_IdxGE, /* 0: (end_constraints && !bRev && !endEq) */
136885 OP_IdxGT, /* 1: (end_constraints && !bRev && endEq) */
136886 OP_IdxLE, /* 2: (end_constraints && bRev && !endEq) */
136887 OP_IdxLT, /* 3: (end_constraints && bRev && endEq) */
136888 };
136889 u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */
136890 u16 nBtm = pLoop->u.btree.nBtm; /* Length of BTM vector */
136891 u16 nTop = pLoop->u.btree.nTop; /* Length of TOP vector */
136892 int regBase; /* Base register holding constraint values */
136893 WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */
136894 WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */
136895 int startEq; /* True if range start uses ==, >= or <= */
136896 int endEq; /* True if range end uses ==, >= or <= */
136897 int start_constraints; /* Start of range is constrained */
136898 int nConstraint; /* Number of constraint terms */
136899 int iIdxCur; /* The VDBE cursor for the index */
136900 int nExtraReg = 0; /* Number of extra registers needed */
136901 int op; /* Instruction opcode */
136902 char *zStartAff; /* Affinity for start of range constraint */
136903 char *zEndAff = 0; /* Affinity for end of range constraint */
136904 u8 bSeekPastNull = 0; /* True to seek past initial nulls */
136905 u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */
136906
136907 pIdx = pLoop->u.btree.pIndex;
136908 iIdxCur = pLevel->iIdxCur;
136909 assert( nEq>=pLoop->nSkip );
136910
136911 /* If this loop satisfies a sort order (pOrderBy) request that
136912 ** was passed to this function to implement a "SELECT min(x) ..."
136913 ** query, then the caller will only allow the loop to run for
136914 ** a single iteration. This means that the first row returned
136915 ** should not have a NULL value stored in 'x'. If column 'x' is
136916 ** the first one after the nEq equality constraints in the index,
136917 ** this requires some special handling.
136918 */
136919 assert( pWInfo->pOrderBy==0
136920 || pWInfo->pOrderBy->nExpr==1
136921 || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 );
136922 if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
136923 && pWInfo->nOBSat>0
136924 && (pIdx->nKeyCol>nEq)
136925 ){
136926 assert( pLoop->nSkip==0 );
136927 bSeekPastNull = 1;
136928 nExtraReg = 1;
136929 }
136930
136931 /* Find any inequality constraint terms for the start and end
136932 ** of the range.
136933 */
136934 j = nEq;
136935 if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
136936 pRangeStart = pLoop->aLTerm[j++];
136937 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm);
136938 /* Like optimization range constraints always occur in pairs */
136939 assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||
136940 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
136941 }
136942 if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
136943 pRangeEnd = pLoop->aLTerm[j++];
136944 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop);
136945#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
136946 if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
136947 assert( pRangeStart!=0 ); /* LIKE opt constraints */
136948 assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */
136949 pLevel->iLikeRepCntr = (u32)++pParse->nMem;
136950 sqlite3VdbeAddOp2(v, OP_Integer, 1, (int)pLevel->iLikeRepCntr);
136951 VdbeComment((v, "LIKE loop counter"));
136952 pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
136953 /* iLikeRepCntr actually stores 2x the counter register number. The
136954 ** bottom bit indicates whether the search order is ASC or DESC. */
136955 testcase( bRev );
136956 testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
136957 assert( (bRev & ~1)==0 );
136958 pLevel->iLikeRepCntr <<=1;
136959 pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC);
136960 }
136961#endif
136962 if( pRangeStart==0 ){
136963 j = pIdx->aiColumn[nEq];
136964 if( (j>=0 && pIdx->pTable->aCol[j].notNull==0) || j==XN_EXPR ){
136965 bSeekPastNull = 1;
136966 }
136967 }
136968 }
136969 assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
136970
136971 /* If we are doing a reverse order scan on an ascending index, or
136972 ** a forward order scan on a descending index, interchange the
136973 ** start and end terms (pRangeStart and pRangeEnd).
136974 */
136975 if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
136976 || (bRev && pIdx->nKeyCol==nEq)
136977 ){
136978 SWAP(WhereTerm *, pRangeEnd, pRangeStart);
136979 SWAP(u8, bSeekPastNull, bStopAtNull);
136980 SWAP(u8, nBtm, nTop);
136981 }
136982
136983 /* Generate code to evaluate all constraint terms using == or IN
136984 ** and store the values of those terms in an array of registers
136985 ** starting at regBase.
136986 */
136987 codeCursorHint(pTabItem, pWInfo, pLevel, pRangeEnd);
136988 regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
136989 assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
136990 if( zStartAff && nTop ){
136991 zEndAff = sqlite3DbStrDup(db, &zStartAff[nEq]);
136992 }
136993 addrNxt = pLevel->addrNxt;
136994
136995 testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
136996 testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
136997 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
136998 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
136999 startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
137000 endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
137001 start_constraints = pRangeStart || nEq>0;
137002
137003 /* Seek the index cursor to the start of the range. */
137004 nConstraint = nEq;
137005 if( pRangeStart ){
137006 Expr *pRight = pRangeStart->pExpr->pRight;
137007 codeExprOrVector(pParse, pRight, regBase+nEq, nBtm);
137008 whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
137009 if( (pRangeStart->wtFlags & TERM_VNULL)==0
137010 && sqlite3ExprCanBeNull(pRight)
137011 ){
137012 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
137013 VdbeCoverage(v);
137014 }
137015 if( zStartAff ){
137016 updateRangeAffinityStr(pRight, nBtm, &zStartAff[nEq]);
137017 }
137018 nConstraint += nBtm;
137019 testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
137020 if( sqlite3ExprIsVector(pRight)==0 ){
137021 disableTerm(pLevel, pRangeStart);
137022 }else{
137023 startEq = 1;
137024 }
137025 bSeekPastNull = 0;
137026 }else if( bSeekPastNull ){
137027 sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
137028 nConstraint++;
137029 startEq = 0;
137030 start_constraints = 1;
137031 }
137032 codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
137033 if( pLoop->nSkip>0 && nConstraint==pLoop->nSkip ){
137034 /* The skip-scan logic inside the call to codeAllEqualityConstraints()
137035 ** above has already left the cursor sitting on the correct row,
137036 ** so no further seeking is needed */
137037 }else{
137038 if( pLoop->wsFlags & WHERE_IN_EARLYOUT ){
137039 sqlite3VdbeAddOp1(v, OP_SeekHit, iIdxCur);
137040 }
137041 op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
137042 assert( op!=0 );
137043 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
137044 VdbeCoverage(v);
137045 VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind );
137046 VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last );
137047 VdbeCoverageIf(v, op==OP_SeekGT); testcase( op==OP_SeekGT );
137048 VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE );
137049 VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE );
137050 VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT );
137051 }
137052
137053 /* Load the value for the inequality constraint at the end of the
137054 ** range (if any).
137055 */
137056 nConstraint = nEq;
137057 if( pRangeEnd ){
137058 Expr *pRight = pRangeEnd->pExpr->pRight;
137059 codeExprOrVector(pParse, pRight, regBase+nEq, nTop);
137060 whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
137061 if( (pRangeEnd->wtFlags & TERM_VNULL)==0
137062 && sqlite3ExprCanBeNull(pRight)
137063 ){
137064 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
137065 VdbeCoverage(v);
137066 }
137067 if( zEndAff ){
137068 updateRangeAffinityStr(pRight, nTop, zEndAff);
137069 codeApplyAffinity(pParse, regBase+nEq, nTop, zEndAff);
137070 }else{
137071 assert( pParse->db->mallocFailed );
137072 }
137073 nConstraint += nTop;
137074 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
137075
137076 if( sqlite3ExprIsVector(pRight)==0 ){
137077 disableTerm(pLevel, pRangeEnd);
137078 }else{
137079 endEq = 1;
137080 }
137081 }else if( bStopAtNull ){
137082 sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
137083 endEq = 0;
137084 nConstraint++;
137085 }
137086 sqlite3DbFree(db, zStartAff);
137087 sqlite3DbFree(db, zEndAff);
137088
137089 /* Top of the loop body */
137090 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
137091
137092 /* Check if the index cursor is past the end of the range. */
137093 if( nConstraint ){
137094 op = aEndOp[bRev*2 + endEq];
137095 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
137096 testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT );
137097 testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE );
137098 testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT );
137099 testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE );
137100 }
137101
137102 if( pLoop->wsFlags & WHERE_IN_EARLYOUT ){
137103 sqlite3VdbeAddOp2(v, OP_SeekHit, iIdxCur, 1);
137104 }
137105
137106 /* Seek the table cursor, if required */
137107 if( omitTable ){
137108 /* pIdx is a covering index. No need to access the main table. */
137109 }else if( HasRowid(pIdx->pTable) ){
137110 if( (pWInfo->wctrlFlags & WHERE_SEEK_TABLE) || (
137111 (pWInfo->wctrlFlags & WHERE_SEEK_UNIQ_TABLE)
137112 && (pWInfo->eOnePass==ONEPASS_SINGLE)
137113 )){
137114 iRowidReg = ++pParse->nMem;
137115 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
137116 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowidReg);
137117 VdbeCoverage(v);
137118 }else{
137119 codeDeferredSeek(pWInfo, pIdx, iCur, iIdxCur);
137120 }
137121 }else if( iCur!=iIdxCur ){
137122 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
137123 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
137124 for(j=0; j<pPk->nKeyCol; j++){
137125 k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
137126 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
137127 }
137128 sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
137129 iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
137130 }
137131
137132 /* If pIdx is an index on one or more expressions, then look through
137133 ** all the expressions in pWInfo and try to transform matching expressions
137134 ** into reference to index columns.
137135 **
137136 ** Do not do this for the RHS of a LEFT JOIN. This is because the
137137 ** expression may be evaluated after OP_NullRow has been executed on
137138 ** the cursor. In this case it is important to do the full evaluation,
137139 ** as the result of the expression may not be NULL, even if all table
137140 ** column values are. https://www.sqlite.org/src/info/7fa8049685b50b5a
137141 */
137142 if( pLevel->iLeftJoin==0 ){
137143 whereIndexExprTrans(pIdx, iCur, iIdxCur, pWInfo);
137144 }
137145
137146 /* Record the instruction used to terminate the loop. */
137147 if( pLoop->wsFlags & WHERE_ONEROW ){
137148 pLevel->op = OP_Noop;
137149 }else if( bRev ){
137150 pLevel->op = OP_Prev;
137151 }else{
137152 pLevel->op = OP_Next;
137153 }
137154 pLevel->p1 = iIdxCur;
137155 pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
137156 if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
137157 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
137158 }else{
137159 assert( pLevel->p5==0 );
137160 }
137161 if( omitTable ) pIdx = 0;
137162 }else
137163
137164#ifndef SQLITE_OMIT_OR_OPTIMIZATION
137165 if( pLoop->wsFlags & WHERE_MULTI_OR ){
137166 /* Case 5: Two or more separately indexed terms connected by OR
137167 **
137168 ** Example:
137169 **
137170 ** CREATE TABLE t1(a,b,c,d);
137171 ** CREATE INDEX i1 ON t1(a);
137172 ** CREATE INDEX i2 ON t1(b);
137173 ** CREATE INDEX i3 ON t1(c);
137174 **
137175 ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
137176 **
137177 ** In the example, there are three indexed terms connected by OR.
137178 ** The top of the loop looks like this:
137179 **
137180 ** Null 1 # Zero the rowset in reg 1
137181 **
137182 ** Then, for each indexed term, the following. The arguments to
137183 ** RowSetTest are such that the rowid of the current row is inserted
137184 ** into the RowSet. If it is already present, control skips the
137185 ** Gosub opcode and jumps straight to the code generated by WhereEnd().
137186 **
137187 ** sqlite3WhereBegin(<term>)
137188 ** RowSetTest # Insert rowid into rowset
137189 ** Gosub 2 A
137190 ** sqlite3WhereEnd()
137191 **
137192 ** Following the above, code to terminate the loop. Label A, the target
137193 ** of the Gosub above, jumps to the instruction right after the Goto.
137194 **
137195 ** Null 1 # Zero the rowset in reg 1
137196 ** Goto B # The loop is finished.
137197 **
137198 ** A: <loop body> # Return data, whatever.
137199 **
137200 ** Return 2 # Jump back to the Gosub
137201 **
137202 ** B: <after the loop>
137203 **
137204 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
137205 ** use an ephemeral index instead of a RowSet to record the primary
137206 ** keys of the rows we have already seen.
137207 **
137208 */
137209 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
137210 SrcList *pOrTab; /* Shortened table list or OR-clause generation */
137211 Index *pCov = 0; /* Potential covering index (or NULL) */
137212 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
137213
137214 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
137215 int regRowset = 0; /* Register for RowSet object */
137216 int regRowid = 0; /* Register holding rowid */
137217 int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */
137218 int iRetInit; /* Address of regReturn init */
137219 int untestedTerms = 0; /* Some terms not completely tested */
137220 int ii; /* Loop counter */
137221 u16 wctrlFlags; /* Flags for sub-WHERE clause */
137222 Expr *pAndExpr = 0; /* An ".. AND (...)" expression */
137223 Table *pTab = pTabItem->pTab;
137224
137225 pTerm = pLoop->aLTerm[0];
137226 assert( pTerm!=0 );
137227 assert( pTerm->eOperator & WO_OR );
137228 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
137229 pOrWc = &pTerm->u.pOrInfo->wc;
137230 pLevel->op = OP_Return;
137231 pLevel->p1 = regReturn;
137232
137233 /* Set up a new SrcList in pOrTab containing the table being scanned
137234 ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
137235 ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
137236 */
137237 if( pWInfo->nLevel>1 ){
137238 int nNotReady; /* The number of notReady tables */
137239 struct SrcList_item *origSrc; /* Original list of tables */
137240 nNotReady = pWInfo->nLevel - iLevel - 1;
137241 pOrTab = sqlite3StackAllocRaw(db,
137242 sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
137243 if( pOrTab==0 ) return notReady;
137244 pOrTab->nAlloc = (u8)(nNotReady + 1);
137245 pOrTab->nSrc = pOrTab->nAlloc;
137246 memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
137247 origSrc = pWInfo->pTabList->a;
137248 for(k=1; k<=nNotReady; k++){
137249 memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
137250 }
137251 }else{
137252 pOrTab = pWInfo->pTabList;
137253 }
137254
137255 /* Initialize the rowset register to contain NULL. An SQL NULL is
137256 ** equivalent to an empty rowset. Or, create an ephemeral index
137257 ** capable of holding primary keys in the case of a WITHOUT ROWID.
137258 **
137259 ** Also initialize regReturn to contain the address of the instruction
137260 ** immediately following the OP_Return at the bottom of the loop. This
137261 ** is required in a few obscure LEFT JOIN cases where control jumps
137262 ** over the top of the loop into the body of it. In this case the
137263 ** correct response for the end-of-loop code (the OP_Return) is to
137264 ** fall through to the next instruction, just as an OP_Next does if
137265 ** called on an uninitialized cursor.
137266 */
137267 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
137268 if( HasRowid(pTab) ){
137269 regRowset = ++pParse->nMem;
137270 sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
137271 }else{
137272 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
137273 regRowset = pParse->nTab++;
137274 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
137275 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
137276 }
137277 regRowid = ++pParse->nMem;
137278 }
137279 iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
137280
137281 /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y
137282 ** Then for every term xN, evaluate as the subexpression: xN AND z
137283 ** That way, terms in y that are factored into the disjunction will
137284 ** be picked up by the recursive calls to sqlite3WhereBegin() below.
137285 **
137286 ** Actually, each subexpression is converted to "xN AND w" where w is
137287 ** the "interesting" terms of z - terms that did not originate in the
137288 ** ON or USING clause of a LEFT JOIN, and terms that are usable as
137289 ** indices.
137290 **
137291 ** This optimization also only applies if the (x1 OR x2 OR ...) term
137292 ** is not contained in the ON clause of a LEFT JOIN.
137293 ** See ticket http://www.sqlite.org/src/info/f2369304e4
137294 */
137295 if( pWC->nTerm>1 ){
137296 int iTerm;
137297 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
137298 Expr *pExpr = pWC->a[iTerm].pExpr;
137299 if( &pWC->a[iTerm] == pTerm ) continue;
137300 testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
137301 testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
137302 if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED))!=0 ) continue;
137303 if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
137304 testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
137305 pExpr = sqlite3ExprDup(db, pExpr, 0);
137306 pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
137307 }
137308 if( pAndExpr ){
137309 pAndExpr = sqlite3PExpr(pParse, TK_AND|TKFLG_DONTFOLD, 0, pAndExpr);
137310 }
137311 }
137312
137313 /* Run a separate WHERE clause for each term of the OR clause. After
137314 ** eliminating duplicates from other WHERE clauses, the action for each
137315 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
137316 */
137317 wctrlFlags = WHERE_OR_SUBCLAUSE | (pWInfo->wctrlFlags & WHERE_SEEK_TABLE);
137318 ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR"));
137319 for(ii=0; ii<pOrWc->nTerm; ii++){
137320 WhereTerm *pOrTerm = &pOrWc->a[ii];
137321 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
137322 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
137323 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
137324 int jmp1 = 0; /* Address of jump operation */
137325 assert( (pTabItem[0].fg.jointype & JT_LEFT)==0
137326 || ExprHasProperty(pOrExpr, EP_FromJoin)
137327 );
137328 if( pAndExpr ){
137329 pAndExpr->pLeft = pOrExpr;
137330 pOrExpr = pAndExpr;
137331 }
137332 /* Loop through table entries that match term pOrTerm. */
137333 WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
137334 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
137335 wctrlFlags, iCovCur);
137336 assert( pSubWInfo || pParse->nErr || db->mallocFailed );
137337 if( pSubWInfo ){
137338 WhereLoop *pSubLoop;
137339 int addrExplain = sqlite3WhereExplainOneScan(
137340 pParse, pOrTab, &pSubWInfo->a[0], 0
137341 );
137342 sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
137343
137344 /* This is the sub-WHERE clause body. First skip over
137345 ** duplicate rows from prior sub-WHERE clauses, and record the
137346 ** rowid (or PRIMARY KEY) for the current row so that the same
137347 ** row will be skipped in subsequent sub-WHERE clauses.
137348 */
137349 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
137350 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
137351 if( HasRowid(pTab) ){
137352 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, regRowid);
137353 jmp1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0,
137354 regRowid, iSet);
137355 VdbeCoverage(v);
137356 }else{
137357 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
137358 int nPk = pPk->nKeyCol;
137359 int iPk;
137360 int r;
137361
137362 /* Read the PK into an array of temp registers. */
137363 r = sqlite3GetTempRange(pParse, nPk);
137364 for(iPk=0; iPk<nPk; iPk++){
137365 int iCol = pPk->aiColumn[iPk];
137366 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol, r+iPk);
137367 }
137368
137369 /* Check if the temp table already contains this key. If so,
137370 ** the row has already been included in the result set and
137371 ** can be ignored (by jumping past the Gosub below). Otherwise,
137372 ** insert the key into the temp table and proceed with processing
137373 ** the row.
137374 **
137375 ** Use some of the same optimizations as OP_RowSetTest: If iSet
137376 ** is zero, assume that the key cannot already be present in
137377 ** the temp table. And if iSet is -1, assume that there is no
137378 ** need to insert the key into the temp table, as it will never
137379 ** be tested for. */
137380 if( iSet ){
137381 jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk);
137382 VdbeCoverage(v);
137383 }
137384 if( iSet>=0 ){
137385 sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid);
137386 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, regRowset, regRowid,
137387 r, nPk);
137388 if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
137389 }
137390
137391 /* Release the array of temp registers */
137392 sqlite3ReleaseTempRange(pParse, r, nPk);
137393 }
137394 }
137395
137396 /* Invoke the main loop body as a subroutine */
137397 sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
137398
137399 /* Jump here (skipping the main loop body subroutine) if the
137400 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
137401 if( jmp1 ) sqlite3VdbeJumpHere(v, jmp1);
137402
137403 /* The pSubWInfo->untestedTerms flag means that this OR term
137404 ** contained one or more AND term from a notReady table. The
137405 ** terms from the notReady table could not be tested and will
137406 ** need to be tested later.
137407 */
137408 if( pSubWInfo->untestedTerms ) untestedTerms = 1;
137409
137410 /* If all of the OR-connected terms are optimized using the same
137411 ** index, and the index is opened using the same cursor number
137412 ** by each call to sqlite3WhereBegin() made by this loop, it may
137413 ** be possible to use that index as a covering index.
137414 **
137415 ** If the call to sqlite3WhereBegin() above resulted in a scan that
137416 ** uses an index, and this is either the first OR-connected term
137417 ** processed or the index is the same as that used by all previous
137418 ** terms, set pCov to the candidate covering index. Otherwise, set
137419 ** pCov to NULL to indicate that no candidate covering index will
137420 ** be available.
137421 */
137422 pSubLoop = pSubWInfo->a[0].pWLoop;
137423 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
137424 if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
137425 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
137426 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
137427 ){
137428 assert( pSubWInfo->a[0].iIdxCur==iCovCur );
137429 pCov = pSubLoop->u.btree.pIndex;
137430 }else{
137431 pCov = 0;
137432 }
137433
137434 /* Finish the loop through table entries that match term pOrTerm. */
137435 sqlite3WhereEnd(pSubWInfo);
137436 }
137437 }
137438 }
137439 ExplainQueryPlanPop(pParse);
137440 pLevel->u.pCovidx = pCov;
137441 if( pCov ) pLevel->iIdxCur = iCovCur;
137442 if( pAndExpr ){
137443 pAndExpr->pLeft = 0;
137444 sqlite3ExprDelete(db, pAndExpr);
137445 }
137446 sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
137447 sqlite3VdbeGoto(v, pLevel->addrBrk);
137448 sqlite3VdbeResolveLabel(v, iLoopBody);
137449
137450 if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab);
137451 if( !untestedTerms ) disableTerm(pLevel, pTerm);
137452 }else
137453#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
137454
137455 {
137456 /* Case 6: There is no usable index. We must do a complete
137457 ** scan of the entire table.
137458 */
137459 static const u8 aStep[] = { OP_Next, OP_Prev };
137460 static const u8 aStart[] = { OP_Rewind, OP_Last };
137461 assert( bRev==0 || bRev==1 );
137462 if( pTabItem->fg.isRecursive ){
137463 /* Tables marked isRecursive have only a single row that is stored in
137464 ** a pseudo-cursor. No need to Rewind or Next such cursors. */
137465 pLevel->op = OP_Noop;
137466 }else{
137467 codeCursorHint(pTabItem, pWInfo, pLevel, 0);
137468 pLevel->op = aStep[bRev];
137469 pLevel->p1 = iCur;
137470 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrHalt);
137471 VdbeCoverageIf(v, bRev==0);
137472 VdbeCoverageIf(v, bRev!=0);
137473 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
137474 }
137475 }
137476
137477#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
137478 pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
137479#endif
137480
137481 /* Insert code to test every subexpression that can be completely
137482 ** computed using the current set of tables.
137483 **
137484 ** This loop may run between one and three times, depending on the
137485 ** constraints to be generated. The value of stack variable iLoop
137486 ** determines the constraints coded by each iteration, as follows:
137487 **
137488 ** iLoop==1: Code only expressions that are entirely covered by pIdx.
137489 ** iLoop==2: Code remaining expressions that do not contain correlated
137490 ** sub-queries.
137491 ** iLoop==3: Code all remaining expressions.
137492 **
137493 ** An effort is made to skip unnecessary iterations of the loop.
137494 */
137495 iLoop = (pIdx ? 1 : 2);
137496 do{
137497 int iNext = 0; /* Next value for iLoop */
137498 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
137499 Expr *pE;
137500 int skipLikeAddr = 0;
137501 testcase( pTerm->wtFlags & TERM_VIRTUAL );
137502 testcase( pTerm->wtFlags & TERM_CODED );
137503 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
137504 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
137505 testcase( pWInfo->untestedTerms==0
137506 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 );
137507 pWInfo->untestedTerms = 1;
137508 continue;
137509 }
137510 pE = pTerm->pExpr;
137511 assert( pE!=0 );
137512 if( (pTabItem->fg.jointype&JT_LEFT) && !ExprHasProperty(pE,EP_FromJoin) ){
137513 continue;
137514 }
137515
137516 if( iLoop==1 && !sqlite3ExprCoveredByIndex(pE, pLevel->iTabCur, pIdx) ){
137517 iNext = 2;
137518 continue;
137519 }
137520 if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT) ){
137521 if( iNext==0 ) iNext = 3;
137522 continue;
137523 }
137524
137525 if( (pTerm->wtFlags & TERM_LIKECOND)!=0 ){
137526 /* If the TERM_LIKECOND flag is set, that means that the range search
137527 ** is sufficient to guarantee that the LIKE operator is true, so we
137528 ** can skip the call to the like(A,B) function. But this only works
137529 ** for strings. So do not skip the call to the function on the pass
137530 ** that compares BLOBs. */
137531#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
137532 continue;
137533#else
137534 u32 x = pLevel->iLikeRepCntr;
137535 if( x>0 ){
137536 skipLikeAddr = sqlite3VdbeAddOp1(v, (x&1)?OP_IfNot:OP_If,(int)(x>>1));
137537 }
137538 VdbeCoverage(v);
137539#endif
137540 }
137541#ifdef WHERETRACE_ENABLED /* 0xffff */
137542 if( sqlite3WhereTrace ){
137543 VdbeNoopComment((v, "WhereTerm[%d] (%p) priority=%d",
137544 pWC->nTerm-j, pTerm, iLoop));
137545 }
137546#endif
137547 sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
137548 if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr);
137549 pTerm->wtFlags |= TERM_CODED;
137550 }
137551 iLoop = iNext;
137552 }while( iLoop>0 );
137553
137554 /* Insert code to test for implied constraints based on transitivity
137555 ** of the "==" operator.
137556 **
137557 ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
137558 ** and we are coding the t1 loop and the t2 loop has not yet coded,
137559 ** then we cannot use the "t1.a=t2.b" constraint, but we can code
137560 ** the implied "t1.a=123" constraint.
137561 */
137562 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
137563 Expr *pE, sEAlt;
137564 WhereTerm *pAlt;
137565 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
137566 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
137567 if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
137568 if( pTerm->leftCursor!=iCur ) continue;
137569 if( pLevel->iLeftJoin ) continue;
137570 pE = pTerm->pExpr;
137571 assert( !ExprHasProperty(pE, EP_FromJoin) );
137572 assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
137573 pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
137574 WO_EQ|WO_IN|WO_IS, 0);
137575 if( pAlt==0 ) continue;
137576 if( pAlt->wtFlags & (TERM_CODED) ) continue;
137577 if( (pAlt->eOperator & WO_IN)
137578 && (pAlt->pExpr->flags & EP_xIsSelect)
137579 && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
137580 ){
137581 continue;
137582 }
137583 testcase( pAlt->eOperator & WO_EQ );
137584 testcase( pAlt->eOperator & WO_IS );
137585 testcase( pAlt->eOperator & WO_IN );
137586 VdbeModuleComment((v, "begin transitive constraint"));
137587 sEAlt = *pAlt->pExpr;
137588 sEAlt.pLeft = pE->pLeft;
137589 sqlite3ExprIfFalse(pParse, &sEAlt, addrCont, SQLITE_JUMPIFNULL);
137590 }
137591
137592 /* For a LEFT OUTER JOIN, generate code that will record the fact that
137593 ** at least one row of the right table has matched the left table.
137594 */
137595 if( pLevel->iLeftJoin ){
137596 pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
137597 sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
137598 VdbeComment((v, "record LEFT JOIN hit"));
137599 for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
137600 testcase( pTerm->wtFlags & TERM_VIRTUAL );
137601 testcase( pTerm->wtFlags & TERM_CODED );
137602 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
137603 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
137604 assert( pWInfo->untestedTerms );
137605 continue;
137606 }
137607 assert( pTerm->pExpr );
137608 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
137609 pTerm->wtFlags |= TERM_CODED;
137610 }
137611 }
137612
137613 return pLevel->notReady;
137614}
137615
137616/************** End of wherecode.c *******************************************/
137617/************** Begin file whereexpr.c ***************************************/
137618/*
137619** 2015-06-08
137620**
137621** The author disclaims copyright to this source code. In place of
137622** a legal notice, here is a blessing:
137623**
137624** May you do good and not evil.
137625** May you find forgiveness for yourself and forgive others.
137626** May you share freely, never taking more than you give.
137627**
137628*************************************************************************
137629** This module contains C code that generates VDBE code used to process
137630** the WHERE clause of SQL statements.
137631**
137632** This file was originally part of where.c but was split out to improve
137633** readability and editabiliity. This file contains utility routines for
137634** analyzing Expr objects in the WHERE clause.
137635*/
137636/* #include "sqliteInt.h" */
137637/* #include "whereInt.h" */
137638
137639/* Forward declarations */
137640static void exprAnalyze(SrcList*, WhereClause*, int);
137641
137642/*
137643** Deallocate all memory associated with a WhereOrInfo object.
137644*/
137645static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
137646 sqlite3WhereClauseClear(&p->wc);
137647 sqlite3DbFree(db, p);
137648}
137649
137650/*
137651** Deallocate all memory associated with a WhereAndInfo object.
137652*/
137653static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
137654 sqlite3WhereClauseClear(&p->wc);
137655 sqlite3DbFree(db, p);
137656}
137657
137658/*
137659** Add a single new WhereTerm entry to the WhereClause object pWC.
137660** The new WhereTerm object is constructed from Expr p and with wtFlags.
137661** The index in pWC->a[] of the new WhereTerm is returned on success.
137662** 0 is returned if the new WhereTerm could not be added due to a memory
137663** allocation error. The memory allocation failure will be recorded in
137664** the db->mallocFailed flag so that higher-level functions can detect it.
137665**
137666** This routine will increase the size of the pWC->a[] array as necessary.
137667**
137668** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
137669** for freeing the expression p is assumed by the WhereClause object pWC.
137670** This is true even if this routine fails to allocate a new WhereTerm.
137671**
137672** WARNING: This routine might reallocate the space used to store
137673** WhereTerms. All pointers to WhereTerms should be invalidated after
137674** calling this routine. Such pointers may be reinitialized by referencing
137675** the pWC->a[] array.
137676*/
137677static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){
137678 WhereTerm *pTerm;
137679 int idx;
137680 testcase( wtFlags & TERM_VIRTUAL );
137681 if( pWC->nTerm>=pWC->nSlot ){
137682 WhereTerm *pOld = pWC->a;
137683 sqlite3 *db = pWC->pWInfo->pParse->db;
137684 pWC->a = sqlite3DbMallocRawNN(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
137685 if( pWC->a==0 ){
137686 if( wtFlags & TERM_DYNAMIC ){
137687 sqlite3ExprDelete(db, p);
137688 }
137689 pWC->a = pOld;
137690 return 0;
137691 }
137692 memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
137693 if( pOld!=pWC->aStatic ){
137694 sqlite3DbFree(db, pOld);
137695 }
137696 pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
137697 }
137698 pTerm = &pWC->a[idx = pWC->nTerm++];
137699 if( p && ExprHasProperty(p, EP_Unlikely) ){
137700 pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
137701 }else{
137702 pTerm->truthProb = 1;
137703 }
137704 pTerm->pExpr = sqlite3ExprSkipCollate(p);
137705 pTerm->wtFlags = wtFlags;
137706 pTerm->pWC = pWC;
137707 pTerm->iParent = -1;
137708 memset(&pTerm->eOperator, 0,
137709 sizeof(WhereTerm) - offsetof(WhereTerm,eOperator));
137710 return idx;
137711}
137712
137713/*
137714** Return TRUE if the given operator is one of the operators that is
137715** allowed for an indexable WHERE clause term. The allowed operators are
137716** "=", "<", ">", "<=", ">=", "IN", "IS", and "IS NULL"
137717*/
137718static int allowedOp(int op){
137719 assert( TK_GT>TK_EQ && TK_GT<TK_GE );
137720 assert( TK_LT>TK_EQ && TK_LT<TK_GE );
137721 assert( TK_LE>TK_EQ && TK_LE<TK_GE );
137722 assert( TK_GE==TK_EQ+4 );
137723 return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL || op==TK_IS;
137724}
137725
137726/*
137727** Commute a comparison operator. Expressions of the form "X op Y"
137728** are converted into "Y op X".
137729**
137730** If left/right precedence rules come into play when determining the
137731** collating sequence, then COLLATE operators are adjusted to ensure
137732** that the collating sequence does not change. For example:
137733** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on
137734** the left hand side of a comparison overrides any collation sequence
137735** attached to the right. For the same reason the EP_Collate flag
137736** is not commuted.
137737*/
137738static void exprCommute(Parse *pParse, Expr *pExpr){
137739 u16 expRight = (pExpr->pRight->flags & EP_Collate);
137740 u16 expLeft = (pExpr->pLeft->flags & EP_Collate);
137741 assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
137742 if( expRight==expLeft ){
137743 /* Either X and Y both have COLLATE operator or neither do */
137744 if( expRight ){
137745 /* Both X and Y have COLLATE operators. Make sure X is always
137746 ** used by clearing the EP_Collate flag from Y. */
137747 pExpr->pRight->flags &= ~EP_Collate;
137748 }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){
137749 /* Neither X nor Y have COLLATE operators, but X has a non-default
137750 ** collating sequence. So add the EP_Collate marker on X to cause
137751 ** it to be searched first. */
137752 pExpr->pLeft->flags |= EP_Collate;
137753 }
137754 }
137755 SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
137756 if( pExpr->op>=TK_GT ){
137757 assert( TK_LT==TK_GT+2 );
137758 assert( TK_GE==TK_LE+2 );
137759 assert( TK_GT>TK_EQ );
137760 assert( TK_GT<TK_LE );
137761 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
137762 pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
137763 }
137764}
137765
137766/*
137767** Translate from TK_xx operator to WO_xx bitmask.
137768*/
137769static u16 operatorMask(int op){
137770 u16 c;
137771 assert( allowedOp(op) );
137772 if( op==TK_IN ){
137773 c = WO_IN;
137774 }else if( op==TK_ISNULL ){
137775 c = WO_ISNULL;
137776 }else if( op==TK_IS ){
137777 c = WO_IS;
137778 }else{
137779 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
137780 c = (u16)(WO_EQ<<(op-TK_EQ));
137781 }
137782 assert( op!=TK_ISNULL || c==WO_ISNULL );
137783 assert( op!=TK_IN || c==WO_IN );
137784 assert( op!=TK_EQ || c==WO_EQ );
137785 assert( op!=TK_LT || c==WO_LT );
137786 assert( op!=TK_LE || c==WO_LE );
137787 assert( op!=TK_GT || c==WO_GT );
137788 assert( op!=TK_GE || c==WO_GE );
137789 assert( op!=TK_IS || c==WO_IS );
137790 return c;
137791}
137792
137793
137794#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
137795/*
137796** Check to see if the given expression is a LIKE or GLOB operator that
137797** can be optimized using inequality constraints. Return TRUE if it is
137798** so and false if not.
137799**
137800** In order for the operator to be optimizible, the RHS must be a string
137801** literal that does not begin with a wildcard. The LHS must be a column
137802** that may only be NULL, a string, or a BLOB, never a number. (This means
137803** that virtual tables cannot participate in the LIKE optimization.) The
137804** collating sequence for the column on the LHS must be appropriate for
137805** the operator.
137806*/
137807static int isLikeOrGlob(
137808 Parse *pParse, /* Parsing and code generating context */
137809 Expr *pExpr, /* Test this expression */
137810 Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */
137811 int *pisComplete, /* True if the only wildcard is % in the last character */
137812 int *pnoCase /* True if uppercase is equivalent to lowercase */
137813){
137814 const u8 *z = 0; /* String on RHS of LIKE operator */
137815 Expr *pRight, *pLeft; /* Right and left size of LIKE operator */
137816 ExprList *pList; /* List of operands to the LIKE operator */
137817 u8 c; /* One character in z[] */
137818 int cnt; /* Number of non-wildcard prefix characters */
137819 u8 wc[4]; /* Wildcard characters */
137820 sqlite3 *db = pParse->db; /* Database connection */
137821 sqlite3_value *pVal = 0;
137822 int op; /* Opcode of pRight */
137823 int rc; /* Result code to return */
137824
137825 if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, (char*)wc) ){
137826 return 0;
137827 }
137828#ifdef SQLITE_EBCDIC
137829 if( *pnoCase ) return 0;
137830#endif
137831 pList = pExpr->x.pList;
137832 pLeft = pList->a[1].pExpr;
137833
137834 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
137835 op = pRight->op;
137836 if( op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
137837 Vdbe *pReprepare = pParse->pReprepare;
137838 int iCol = pRight->iColumn;
137839 pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB);
137840 if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
137841 z = sqlite3_value_text(pVal);
137842 }
137843 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
137844 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
137845 }else if( op==TK_STRING ){
137846 z = (u8*)pRight->u.zToken;
137847 }
137848 if( z ){
137849
137850 /* Count the number of prefix characters prior to the first wildcard */
137851 cnt = 0;
137852 while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
137853 cnt++;
137854 if( c==wc[3] && z[cnt]!=0 ) cnt++;
137855 }
137856
137857 /* The optimization is possible only if (1) the pattern does not begin
137858 ** with a wildcard and if (2) the non-wildcard prefix does not end with
137859 ** an (illegal 0xff) character, or (3) the pattern does not consist of
137860 ** a single escape character. The second condition is necessary so
137861 ** that we can increment the prefix key to find an upper bound for the
137862 ** range search. The third is because the caller assumes that the pattern
137863 ** consists of at least one character after all escapes have been
137864 ** removed. */
137865 if( cnt!=0 && 255!=(u8)z[cnt-1] && (cnt>1 || z[0]!=wc[3]) ){
137866 Expr *pPrefix;
137867
137868 /* A "complete" match if the pattern ends with "*" or "%" */
137869 *pisComplete = c==wc[0] && z[cnt+1]==0;
137870
137871 /* Get the pattern prefix. Remove all escapes from the prefix. */
137872 pPrefix = sqlite3Expr(db, TK_STRING, (char*)z);
137873 if( pPrefix ){
137874 int iFrom, iTo;
137875 char *zNew = pPrefix->u.zToken;
137876 zNew[cnt] = 0;
137877 for(iFrom=iTo=0; iFrom<cnt; iFrom++){
137878 if( zNew[iFrom]==wc[3] ) iFrom++;
137879 zNew[iTo++] = zNew[iFrom];
137880 }
137881 zNew[iTo] = 0;
137882
137883 /* If the RHS begins with a digit or a minus sign, then the LHS must be
137884 ** an ordinary column (not a virtual table column) with TEXT affinity.
137885 ** Otherwise the LHS might be numeric and "lhs >= rhs" would be false
137886 ** even though "lhs LIKE rhs" is true. But if the RHS does not start
137887 ** with a digit or '-', then "lhs LIKE rhs" will always be false if
137888 ** the LHS is numeric and so the optimization still works.
137889 **
137890 ** 2018-09-10 ticket c94369cae9b561b1f996d0054bfab11389f9d033
137891 ** The RHS pattern must not be '/%' because the termination condition
137892 ** will then become "x<'0'" and if the affinity is numeric, will then
137893 ** be converted into "x<0", which is incorrect.
137894 */
137895 if( sqlite3Isdigit(zNew[0])
137896 || zNew[0]=='-'
137897 || (zNew[0]+1=='0' && iTo==1)
137898 ){
137899 if( pLeft->op!=TK_COLUMN
137900 || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT
137901 || IsVirtual(pLeft->y.pTab) /* Value might be numeric */
137902 ){
137903 sqlite3ExprDelete(db, pPrefix);
137904 sqlite3ValueFree(pVal);
137905 return 0;
137906 }
137907 }
137908 }
137909 *ppPrefix = pPrefix;
137910
137911 /* If the RHS pattern is a bound parameter, make arrangements to
137912 ** reprepare the statement when that parameter is rebound */
137913 if( op==TK_VARIABLE ){
137914 Vdbe *v = pParse->pVdbe;
137915 sqlite3VdbeSetVarmask(v, pRight->iColumn);
137916 if( *pisComplete && pRight->u.zToken[1] ){
137917 /* If the rhs of the LIKE expression is a variable, and the current
137918 ** value of the variable means there is no need to invoke the LIKE
137919 ** function, then no OP_Variable will be added to the program.
137920 ** This causes problems for the sqlite3_bind_parameter_name()
137921 ** API. To work around them, add a dummy OP_Variable here.
137922 */
137923 int r1 = sqlite3GetTempReg(pParse);
137924 sqlite3ExprCodeTarget(pParse, pRight, r1);
137925 sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
137926 sqlite3ReleaseTempReg(pParse, r1);
137927 }
137928 }
137929 }else{
137930 z = 0;
137931 }
137932 }
137933
137934 rc = (z!=0);
137935 sqlite3ValueFree(pVal);
137936 return rc;
137937}
137938#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
137939
137940
137941#ifndef SQLITE_OMIT_VIRTUALTABLE
137942/*
137943** Check to see if the pExpr expression is a form that needs to be passed
137944** to the xBestIndex method of virtual tables. Forms of interest include:
137945**
137946** Expression Virtual Table Operator
137947** ----------------------- ---------------------------------
137948** 1. column MATCH expr SQLITE_INDEX_CONSTRAINT_MATCH
137949** 2. column GLOB expr SQLITE_INDEX_CONSTRAINT_GLOB
137950** 3. column LIKE expr SQLITE_INDEX_CONSTRAINT_LIKE
137951** 4. column REGEXP expr SQLITE_INDEX_CONSTRAINT_REGEXP
137952** 5. column != expr SQLITE_INDEX_CONSTRAINT_NE
137953** 6. expr != column SQLITE_INDEX_CONSTRAINT_NE
137954** 7. column IS NOT expr SQLITE_INDEX_CONSTRAINT_ISNOT
137955** 8. expr IS NOT column SQLITE_INDEX_CONSTRAINT_ISNOT
137956** 9. column IS NOT NULL SQLITE_INDEX_CONSTRAINT_ISNOTNULL
137957**
137958** In every case, "column" must be a column of a virtual table. If there
137959** is a match, set *ppLeft to the "column" expression, set *ppRight to the
137960** "expr" expression (even though in forms (6) and (8) the column is on the
137961** right and the expression is on the left). Also set *peOp2 to the
137962** appropriate virtual table operator. The return value is 1 or 2 if there
137963** is a match. The usual return is 1, but if the RHS is also a column
137964** of virtual table in forms (5) or (7) then return 2.
137965**
137966** If the expression matches none of the patterns above, return 0.
137967*/
137968static int isAuxiliaryVtabOperator(
137969 sqlite3 *db, /* Parsing context */
137970 Expr *pExpr, /* Test this expression */
137971 unsigned char *peOp2, /* OUT: 0 for MATCH, or else an op2 value */
137972 Expr **ppLeft, /* Column expression to left of MATCH/op2 */
137973 Expr **ppRight /* Expression to left of MATCH/op2 */
137974){
137975 if( pExpr->op==TK_FUNCTION ){
137976 static const struct Op2 {
137977 const char *zOp;
137978 unsigned char eOp2;
137979 } aOp[] = {
137980 { "match", SQLITE_INDEX_CONSTRAINT_MATCH },
137981 { "glob", SQLITE_INDEX_CONSTRAINT_GLOB },
137982 { "like", SQLITE_INDEX_CONSTRAINT_LIKE },
137983 { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP }
137984 };
137985 ExprList *pList;
137986 Expr *pCol; /* Column reference */
137987 int i;
137988
137989 pList = pExpr->x.pList;
137990 if( pList==0 || pList->nExpr!=2 ){
137991 return 0;
137992 }
137993
137994 /* Built-in operators MATCH, GLOB, LIKE, and REGEXP attach to a
137995 ** virtual table on their second argument, which is the same as
137996 ** the left-hand side operand in their in-fix form.
137997 **
137998 ** vtab_column MATCH expression
137999 ** MATCH(expression,vtab_column)
138000 */
138001 pCol = pList->a[1].pExpr;
138002 if( pCol->op==TK_COLUMN && IsVirtual(pCol->y.pTab) ){
138003 for(i=0; i<ArraySize(aOp); i++){
138004 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
138005 *peOp2 = aOp[i].eOp2;
138006 *ppRight = pList->a[0].pExpr;
138007 *ppLeft = pCol;
138008 return 1;
138009 }
138010 }
138011 }
138012
138013 /* We can also match against the first column of overloaded
138014 ** functions where xFindFunction returns a value of at least
138015 ** SQLITE_INDEX_CONSTRAINT_FUNCTION.
138016 **
138017 ** OVERLOADED(vtab_column,expression)
138018 **
138019 ** Historically, xFindFunction expected to see lower-case function
138020 ** names. But for this use case, xFindFunction is expected to deal
138021 ** with function names in an arbitrary case.
138022 */
138023 pCol = pList->a[0].pExpr;
138024 if( pCol->op==TK_COLUMN && IsVirtual(pCol->y.pTab) ){
138025 sqlite3_vtab *pVtab;
138026 sqlite3_module *pMod;
138027 void (*xNotUsed)(sqlite3_context*,int,sqlite3_value**);
138028 void *pNotUsed;
138029 pVtab = sqlite3GetVTable(db, pCol->y.pTab)->pVtab;
138030 assert( pVtab!=0 );
138031 assert( pVtab->pModule!=0 );
138032 pMod = (sqlite3_module *)pVtab->pModule;
138033 if( pMod->xFindFunction!=0 ){
138034 i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed);
138035 if( i>=SQLITE_INDEX_CONSTRAINT_FUNCTION ){
138036 *peOp2 = i;
138037 *ppRight = pList->a[1].pExpr;
138038 *ppLeft = pCol;
138039 return 1;
138040 }
138041 }
138042 }
138043 }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
138044 int res = 0;
138045 Expr *pLeft = pExpr->pLeft;
138046 Expr *pRight = pExpr->pRight;
138047 if( pLeft->op==TK_COLUMN && IsVirtual(pLeft->y.pTab) ){
138048 res++;
138049 }
138050 if( pRight && pRight->op==TK_COLUMN && IsVirtual(pRight->y.pTab) ){
138051 res++;
138052 SWAP(Expr*, pLeft, pRight);
138053 }
138054 *ppLeft = pLeft;
138055 *ppRight = pRight;
138056 if( pExpr->op==TK_NE ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE;
138057 if( pExpr->op==TK_ISNOT ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT;
138058 if( pExpr->op==TK_NOTNULL ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOTNULL;
138059 return res;
138060 }
138061 return 0;
138062}
138063#endif /* SQLITE_OMIT_VIRTUALTABLE */
138064
138065/*
138066** If the pBase expression originated in the ON or USING clause of
138067** a join, then transfer the appropriate markings over to derived.
138068*/
138069static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
138070 if( pDerived ){
138071 pDerived->flags |= pBase->flags & EP_FromJoin;
138072 pDerived->iRightJoinTable = pBase->iRightJoinTable;
138073 }
138074}
138075
138076/*
138077** Mark term iChild as being a child of term iParent
138078*/
138079static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){
138080 pWC->a[iChild].iParent = iParent;
138081 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
138082 pWC->a[iParent].nChild++;
138083}
138084
138085/*
138086** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
138087** a conjunction, then return just pTerm when N==0. If N is exceeds
138088** the number of available subterms, return NULL.
138089*/
138090static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){
138091 if( pTerm->eOperator!=WO_AND ){
138092 return N==0 ? pTerm : 0;
138093 }
138094 if( N<pTerm->u.pAndInfo->wc.nTerm ){
138095 return &pTerm->u.pAndInfo->wc.a[N];
138096 }
138097 return 0;
138098}
138099
138100/*
138101** Subterms pOne and pTwo are contained within WHERE clause pWC. The
138102** two subterms are in disjunction - they are OR-ed together.
138103**
138104** If these two terms are both of the form: "A op B" with the same
138105** A and B values but different operators and if the operators are
138106** compatible (if one is = and the other is <, for example) then
138107** add a new virtual AND term to pWC that is the combination of the
138108** two.
138109**
138110** Some examples:
138111**
138112** x<y OR x=y --> x<=y
138113** x=y OR x=y --> x=y
138114** x<=y OR x<y --> x<=y
138115**
138116** The following is NOT generated:
138117**
138118** x<y OR x>y --> x!=y
138119*/
138120static void whereCombineDisjuncts(
138121 SrcList *pSrc, /* the FROM clause */
138122 WhereClause *pWC, /* The complete WHERE clause */
138123 WhereTerm *pOne, /* First disjunct */
138124 WhereTerm *pTwo /* Second disjunct */
138125){
138126 u16 eOp = pOne->eOperator | pTwo->eOperator;
138127 sqlite3 *db; /* Database connection (for malloc) */
138128 Expr *pNew; /* New virtual expression */
138129 int op; /* Operator for the combined expression */
138130 int idxNew; /* Index in pWC of the next virtual term */
138131
138132 if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
138133 if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
138134 if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp
138135 && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return;
138136 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
138137 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
138138 if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
138139 if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return;
138140 /* If we reach this point, it means the two subterms can be combined */
138141 if( (eOp & (eOp-1))!=0 ){
138142 if( eOp & (WO_LT|WO_LE) ){
138143 eOp = WO_LE;
138144 }else{
138145 assert( eOp & (WO_GT|WO_GE) );
138146 eOp = WO_GE;
138147 }
138148 }
138149 db = pWC->pWInfo->pParse->db;
138150 pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
138151 if( pNew==0 ) return;
138152 for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
138153 pNew->op = op;
138154 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
138155 exprAnalyze(pSrc, pWC, idxNew);
138156}
138157
138158#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
138159/*
138160** Analyze a term that consists of two or more OR-connected
138161** subterms. So in:
138162**
138163** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
138164** ^^^^^^^^^^^^^^^^^^^^
138165**
138166** This routine analyzes terms such as the middle term in the above example.
138167** A WhereOrTerm object is computed and attached to the term under
138168** analysis, regardless of the outcome of the analysis. Hence:
138169**
138170** WhereTerm.wtFlags |= TERM_ORINFO
138171** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object
138172**
138173** The term being analyzed must have two or more of OR-connected subterms.
138174** A single subterm might be a set of AND-connected sub-subterms.
138175** Examples of terms under analysis:
138176**
138177** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
138178** (B) x=expr1 OR expr2=x OR x=expr3
138179** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
138180** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
138181** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
138182** (F) x>A OR (x=A AND y>=B)
138183**
138184** CASE 1:
138185**
138186** If all subterms are of the form T.C=expr for some single column of C and
138187** a single table T (as shown in example B above) then create a new virtual
138188** term that is an equivalent IN expression. In other words, if the term
138189** being analyzed is:
138190**
138191** x = expr1 OR expr2 = x OR x = expr3
138192**
138193** then create a new virtual term like this:
138194**
138195** x IN (expr1,expr2,expr3)
138196**
138197** CASE 2:
138198**
138199** If there are exactly two disjuncts and one side has x>A and the other side
138200** has x=A (for the same x and A) then add a new virtual conjunct term to the
138201** WHERE clause of the form "x>=A". Example:
138202**
138203** x>A OR (x=A AND y>B) adds: x>=A
138204**
138205** The added conjunct can sometimes be helpful in query planning.
138206**
138207** CASE 3:
138208**
138209** If all subterms are indexable by a single table T, then set
138210**
138211** WhereTerm.eOperator = WO_OR
138212** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
138213**
138214** A subterm is "indexable" if it is of the form
138215** "T.C <op> <expr>" where C is any column of table T and
138216** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
138217** A subterm is also indexable if it is an AND of two or more
138218** subsubterms at least one of which is indexable. Indexable AND
138219** subterms have their eOperator set to WO_AND and they have
138220** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
138221**
138222** From another point of view, "indexable" means that the subterm could
138223** potentially be used with an index if an appropriate index exists.
138224** This analysis does not consider whether or not the index exists; that
138225** is decided elsewhere. This analysis only looks at whether subterms
138226** appropriate for indexing exist.
138227**
138228** All examples A through E above satisfy case 3. But if a term
138229** also satisfies case 1 (such as B) we know that the optimizer will
138230** always prefer case 1, so in that case we pretend that case 3 is not
138231** satisfied.
138232**
138233** It might be the case that multiple tables are indexable. For example,
138234** (E) above is indexable on tables P, Q, and R.
138235**
138236** Terms that satisfy case 3 are candidates for lookup by using
138237** separate indices to find rowids for each subterm and composing
138238** the union of all rowids using a RowSet object. This is similar
138239** to "bitmap indices" in other database engines.
138240**
138241** OTHERWISE:
138242**
138243** If none of cases 1, 2, or 3 apply, then leave the eOperator set to
138244** zero. This term is not useful for search.
138245*/
138246static void exprAnalyzeOrTerm(
138247 SrcList *pSrc, /* the FROM clause */
138248 WhereClause *pWC, /* the complete WHERE clause */
138249 int idxTerm /* Index of the OR-term to be analyzed */
138250){
138251 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
138252 Parse *pParse = pWInfo->pParse; /* Parser context */
138253 sqlite3 *db = pParse->db; /* Database connection */
138254 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
138255 Expr *pExpr = pTerm->pExpr; /* The expression of the term */
138256 int i; /* Loop counters */
138257 WhereClause *pOrWc; /* Breakup of pTerm into subterms */
138258 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
138259 WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */
138260 Bitmask chngToIN; /* Tables that might satisfy case 1 */
138261 Bitmask indexable; /* Tables that are indexable, satisfying case 2 */
138262
138263 /*
138264 ** Break the OR clause into its separate subterms. The subterms are
138265 ** stored in a WhereClause structure containing within the WhereOrInfo
138266 ** object that is attached to the original OR clause term.
138267 */
138268 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
138269 assert( pExpr->op==TK_OR );
138270 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
138271 if( pOrInfo==0 ) return;
138272 pTerm->wtFlags |= TERM_ORINFO;
138273 pOrWc = &pOrInfo->wc;
138274 memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic));
138275 sqlite3WhereClauseInit(pOrWc, pWInfo);
138276 sqlite3WhereSplit(pOrWc, pExpr, TK_OR);
138277 sqlite3WhereExprAnalyze(pSrc, pOrWc);
138278 if( db->mallocFailed ) return;
138279 assert( pOrWc->nTerm>=2 );
138280
138281 /*
138282 ** Compute the set of tables that might satisfy cases 1 or 3.
138283 */
138284 indexable = ~(Bitmask)0;
138285 chngToIN = ~(Bitmask)0;
138286 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
138287 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
138288 WhereAndInfo *pAndInfo;
138289 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
138290 chngToIN = 0;
138291 pAndInfo = sqlite3DbMallocRawNN(db, sizeof(*pAndInfo));
138292 if( pAndInfo ){
138293 WhereClause *pAndWC;
138294 WhereTerm *pAndTerm;
138295 int j;
138296 Bitmask b = 0;
138297 pOrTerm->u.pAndInfo = pAndInfo;
138298 pOrTerm->wtFlags |= TERM_ANDINFO;
138299 pOrTerm->eOperator = WO_AND;
138300 pAndWC = &pAndInfo->wc;
138301 memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic));
138302 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
138303 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
138304 sqlite3WhereExprAnalyze(pSrc, pAndWC);
138305 pAndWC->pOuter = pWC;
138306 if( !db->mallocFailed ){
138307 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
138308 assert( pAndTerm->pExpr );
138309 if( allowedOp(pAndTerm->pExpr->op)
138310 || pAndTerm->eOperator==WO_AUX
138311 ){
138312 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
138313 }
138314 }
138315 }
138316 indexable &= b;
138317 }
138318 }else if( pOrTerm->wtFlags & TERM_COPIED ){
138319 /* Skip this term for now. We revisit it when we process the
138320 ** corresponding TERM_VIRTUAL term */
138321 }else{
138322 Bitmask b;
138323 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
138324 if( pOrTerm->wtFlags & TERM_VIRTUAL ){
138325 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
138326 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
138327 }
138328 indexable &= b;
138329 if( (pOrTerm->eOperator & WO_EQ)==0 ){
138330 chngToIN = 0;
138331 }else{
138332 chngToIN &= b;
138333 }
138334 }
138335 }
138336
138337 /*
138338 ** Record the set of tables that satisfy case 3. The set might be
138339 ** empty.
138340 */
138341 pOrInfo->indexable = indexable;
138342 if( indexable ){
138343 pTerm->eOperator = WO_OR;
138344 pWC->hasOr = 1;
138345 }else{
138346 pTerm->eOperator = WO_OR;
138347 }
138348
138349 /* For a two-way OR, attempt to implementation case 2.
138350 */
138351 if( indexable && pOrWc->nTerm==2 ){
138352 int iOne = 0;
138353 WhereTerm *pOne;
138354 while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
138355 int iTwo = 0;
138356 WhereTerm *pTwo;
138357 while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
138358 whereCombineDisjuncts(pSrc, pWC, pOne, pTwo);
138359 }
138360 }
138361 }
138362
138363 /*
138364 ** chngToIN holds a set of tables that *might* satisfy case 1. But
138365 ** we have to do some additional checking to see if case 1 really
138366 ** is satisfied.
138367 **
138368 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
138369 ** that there is no possibility of transforming the OR clause into an
138370 ** IN operator because one or more terms in the OR clause contain
138371 ** something other than == on a column in the single table. The 1-bit
138372 ** case means that every term of the OR clause is of the form
138373 ** "table.column=expr" for some single table. The one bit that is set
138374 ** will correspond to the common table. We still need to check to make
138375 ** sure the same column is used on all terms. The 2-bit case is when
138376 ** the all terms are of the form "table1.column=table2.column". It
138377 ** might be possible to form an IN operator with either table1.column
138378 ** or table2.column as the LHS if either is common to every term of
138379 ** the OR clause.
138380 **
138381 ** Note that terms of the form "table.column1=table.column2" (the
138382 ** same table on both sizes of the ==) cannot be optimized.
138383 */
138384 if( chngToIN ){
138385 int okToChngToIN = 0; /* True if the conversion to IN is valid */
138386 int iColumn = -1; /* Column index on lhs of IN operator */
138387 int iCursor = -1; /* Table cursor common to all terms */
138388 int j = 0; /* Loop counter */
138389
138390 /* Search for a table and column that appears on one side or the
138391 ** other of the == operator in every subterm. That table and column
138392 ** will be recorded in iCursor and iColumn. There might not be any
138393 ** such table and column. Set okToChngToIN if an appropriate table
138394 ** and column is found but leave okToChngToIN false if not found.
138395 */
138396 for(j=0; j<2 && !okToChngToIN; j++){
138397 pOrTerm = pOrWc->a;
138398 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
138399 assert( pOrTerm->eOperator & WO_EQ );
138400 pOrTerm->wtFlags &= ~TERM_OR_OK;
138401 if( pOrTerm->leftCursor==iCursor ){
138402 /* This is the 2-bit case and we are on the second iteration and
138403 ** current term is from the first iteration. So skip this term. */
138404 assert( j==1 );
138405 continue;
138406 }
138407 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
138408 pOrTerm->leftCursor))==0 ){
138409 /* This term must be of the form t1.a==t2.b where t2 is in the
138410 ** chngToIN set but t1 is not. This term will be either preceded
138411 ** or follwed by an inverted copy (t2.b==t1.a). Skip this term
138412 ** and use its inversion. */
138413 testcase( pOrTerm->wtFlags & TERM_COPIED );
138414 testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
138415 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
138416 continue;
138417 }
138418 iColumn = pOrTerm->u.leftColumn;
138419 iCursor = pOrTerm->leftCursor;
138420 break;
138421 }
138422 if( i<0 ){
138423 /* No candidate table+column was found. This can only occur
138424 ** on the second iteration */
138425 assert( j==1 );
138426 assert( IsPowerOfTwo(chngToIN) );
138427 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
138428 break;
138429 }
138430 testcase( j==1 );
138431
138432 /* We have found a candidate table and column. Check to see if that
138433 ** table and column is common to every term in the OR clause */
138434 okToChngToIN = 1;
138435 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
138436 assert( pOrTerm->eOperator & WO_EQ );
138437 if( pOrTerm->leftCursor!=iCursor ){
138438 pOrTerm->wtFlags &= ~TERM_OR_OK;
138439 }else if( pOrTerm->u.leftColumn!=iColumn ){
138440 okToChngToIN = 0;
138441 }else{
138442 int affLeft, affRight;
138443 /* If the right-hand side is also a column, then the affinities
138444 ** of both right and left sides must be such that no type
138445 ** conversions are required on the right. (Ticket #2249)
138446 */
138447 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
138448 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
138449 if( affRight!=0 && affRight!=affLeft ){
138450 okToChngToIN = 0;
138451 }else{
138452 pOrTerm->wtFlags |= TERM_OR_OK;
138453 }
138454 }
138455 }
138456 }
138457
138458 /* At this point, okToChngToIN is true if original pTerm satisfies
138459 ** case 1. In that case, construct a new virtual term that is
138460 ** pTerm converted into an IN operator.
138461 */
138462 if( okToChngToIN ){
138463 Expr *pDup; /* A transient duplicate expression */
138464 ExprList *pList = 0; /* The RHS of the IN operator */
138465 Expr *pLeft = 0; /* The LHS of the IN operator */
138466 Expr *pNew; /* The complete IN operator */
138467
138468 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
138469 if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
138470 assert( pOrTerm->eOperator & WO_EQ );
138471 assert( pOrTerm->leftCursor==iCursor );
138472 assert( pOrTerm->u.leftColumn==iColumn );
138473 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
138474 pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
138475 pLeft = pOrTerm->pExpr->pLeft;
138476 }
138477 assert( pLeft!=0 );
138478 pDup = sqlite3ExprDup(db, pLeft, 0);
138479 pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0);
138480 if( pNew ){
138481 int idxNew;
138482 transferJoinMarkings(pNew, pExpr);
138483 assert( !ExprHasProperty(pNew, EP_xIsSelect) );
138484 pNew->x.pList = pList;
138485 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
138486 testcase( idxNew==0 );
138487 exprAnalyze(pSrc, pWC, idxNew);
138488 /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where used again */
138489 markTermAsChild(pWC, idxNew, idxTerm);
138490 }else{
138491 sqlite3ExprListDelete(db, pList);
138492 }
138493 }
138494 }
138495}
138496#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
138497
138498/*
138499** We already know that pExpr is a binary operator where both operands are
138500** column references. This routine checks to see if pExpr is an equivalence
138501** relation:
138502** 1. The SQLITE_Transitive optimization must be enabled
138503** 2. Must be either an == or an IS operator
138504** 3. Not originating in the ON clause of an OUTER JOIN
138505** 4. The affinities of A and B must be compatible
138506** 5a. Both operands use the same collating sequence OR
138507** 5b. The overall collating sequence is BINARY
138508** If this routine returns TRUE, that means that the RHS can be substituted
138509** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
138510** This is an optimization. No harm comes from returning 0. But if 1 is
138511** returned when it should not be, then incorrect answers might result.
138512*/
138513static int termIsEquivalence(Parse *pParse, Expr *pExpr){
138514 char aff1, aff2;
138515 CollSeq *pColl;
138516 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
138517 if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
138518 if( ExprHasProperty(pExpr, EP_FromJoin) ) return 0;
138519 aff1 = sqlite3ExprAffinity(pExpr->pLeft);
138520 aff2 = sqlite3ExprAffinity(pExpr->pRight);
138521 if( aff1!=aff2
138522 && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2))
138523 ){
138524 return 0;
138525 }
138526 pColl = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
138527 if( sqlite3IsBinary(pColl) ) return 1;
138528 return sqlite3ExprCollSeqMatch(pParse, pExpr->pLeft, pExpr->pRight);
138529}
138530
138531/*
138532** Recursively walk the expressions of a SELECT statement and generate
138533** a bitmask indicating which tables are used in that expression
138534** tree.
138535*/
138536static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){
138537 Bitmask mask = 0;
138538 while( pS ){
138539 SrcList *pSrc = pS->pSrc;
138540 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
138541 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
138542 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
138543 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
138544 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
138545 if( ALWAYS(pSrc!=0) ){
138546 int i;
138547 for(i=0; i<pSrc->nSrc; i++){
138548 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
138549 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn);
138550 if( pSrc->a[i].fg.isTabFunc ){
138551 mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
138552 }
138553 }
138554 }
138555 pS = pS->pPrior;
138556 }
138557 return mask;
138558}
138559
138560/*
138561** Expression pExpr is one operand of a comparison operator that might
138562** be useful for indexing. This routine checks to see if pExpr appears
138563** in any index. Return TRUE (1) if pExpr is an indexed term and return
138564** FALSE (0) if not. If TRUE is returned, also set aiCurCol[0] to the cursor
138565** number of the table that is indexed and aiCurCol[1] to the column number
138566** of the column that is indexed, or XN_EXPR (-2) if an expression is being
138567** indexed.
138568**
138569** If pExpr is a TK_COLUMN column reference, then this routine always returns
138570** true even if that particular column is not indexed, because the column
138571** might be added to an automatic index later.
138572*/
138573static SQLITE_NOINLINE int exprMightBeIndexed2(
138574 SrcList *pFrom, /* The FROM clause */
138575 Bitmask mPrereq, /* Bitmask of FROM clause terms referenced by pExpr */
138576 int *aiCurCol, /* Write the referenced table cursor and column here */
138577 Expr *pExpr /* An operand of a comparison operator */
138578){
138579 Index *pIdx;
138580 int i;
138581 int iCur;
138582 for(i=0; mPrereq>1; i++, mPrereq>>=1){}
138583 iCur = pFrom->a[i].iCursor;
138584 for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
138585 if( pIdx->aColExpr==0 ) continue;
138586 for(i=0; i<pIdx->nKeyCol; i++){
138587 if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
138588 if( sqlite3ExprCompareSkip(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
138589 aiCurCol[0] = iCur;
138590 aiCurCol[1] = XN_EXPR;
138591 return 1;
138592 }
138593 }
138594 }
138595 return 0;
138596}
138597static int exprMightBeIndexed(
138598 SrcList *pFrom, /* The FROM clause */
138599 Bitmask mPrereq, /* Bitmask of FROM clause terms referenced by pExpr */
138600 int *aiCurCol, /* Write the referenced table cursor & column here */
138601 Expr *pExpr, /* An operand of a comparison operator */
138602 int op /* The specific comparison operator */
138603){
138604 /* If this expression is a vector to the left or right of a
138605 ** inequality constraint (>, <, >= or <=), perform the processing
138606 ** on the first element of the vector. */
138607 assert( TK_GT+1==TK_LE && TK_GT+2==TK_LT && TK_GT+3==TK_GE );
138608 assert( TK_IS<TK_GE && TK_ISNULL<TK_GE && TK_IN<TK_GE );
138609 assert( op<=TK_GE );
138610 if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){
138611 pExpr = pExpr->x.pList->a[0].pExpr;
138612 }
138613
138614 if( pExpr->op==TK_COLUMN ){
138615 aiCurCol[0] = pExpr->iTable;
138616 aiCurCol[1] = pExpr->iColumn;
138617 return 1;
138618 }
138619 if( mPrereq==0 ) return 0; /* No table references */
138620 if( (mPrereq&(mPrereq-1))!=0 ) return 0; /* Refs more than one table */
138621 return exprMightBeIndexed2(pFrom,mPrereq,aiCurCol,pExpr);
138622}
138623
138624/*
138625** The input to this routine is an WhereTerm structure with only the
138626** "pExpr" field filled in. The job of this routine is to analyze the
138627** subexpression and populate all the other fields of the WhereTerm
138628** structure.
138629**
138630** If the expression is of the form "<expr> <op> X" it gets commuted
138631** to the standard form of "X <op> <expr>".
138632**
138633** If the expression is of the form "X <op> Y" where both X and Y are
138634** columns, then the original expression is unchanged and a new virtual
138635** term of the form "Y <op> X" is added to the WHERE clause and
138636** analyzed separately. The original term is marked with TERM_COPIED
138637** and the new term is marked with TERM_DYNAMIC (because it's pExpr
138638** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
138639** is a commuted copy of a prior term.) The original term has nChild=1
138640** and the copy has idxParent set to the index of the original term.
138641*/
138642static void exprAnalyze(
138643 SrcList *pSrc, /* the FROM clause */
138644 WhereClause *pWC, /* the WHERE clause */
138645 int idxTerm /* Index of the term to be analyzed */
138646){
138647 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
138648 WhereTerm *pTerm; /* The term to be analyzed */
138649 WhereMaskSet *pMaskSet; /* Set of table index masks */
138650 Expr *pExpr; /* The expression to be analyzed */
138651 Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */
138652 Bitmask prereqAll; /* Prerequesites of pExpr */
138653 Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */
138654 Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */
138655 int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */
138656 int noCase = 0; /* uppercase equivalent to lowercase */
138657 int op; /* Top-level operator. pExpr->op */
138658 Parse *pParse = pWInfo->pParse; /* Parsing context */
138659 sqlite3 *db = pParse->db; /* Database connection */
138660 unsigned char eOp2 = 0; /* op2 value for LIKE/REGEXP/GLOB */
138661 int nLeft; /* Number of elements on left side vector */
138662
138663 if( db->mallocFailed ){
138664 return;
138665 }
138666 pTerm = &pWC->a[idxTerm];
138667 pMaskSet = &pWInfo->sMaskSet;
138668 pExpr = pTerm->pExpr;
138669 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
138670 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
138671 op = pExpr->op;
138672 if( op==TK_IN ){
138673 assert( pExpr->pRight==0 );
138674 if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
138675 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
138676 pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
138677 }else{
138678 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
138679 }
138680 }else if( op==TK_ISNULL ){
138681 pTerm->prereqRight = 0;
138682 }else{
138683 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
138684 }
138685 pMaskSet->bVarSelect = 0;
138686 prereqAll = sqlite3WhereExprUsageNN(pMaskSet, pExpr);
138687 if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT;
138688 if( ExprHasProperty(pExpr, EP_FromJoin) ){
138689 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
138690 prereqAll |= x;
138691 extraRight = x-1; /* ON clause terms may not be used with an index
138692 ** on left table of a LEFT JOIN. Ticket #3015 */
138693 if( (prereqAll>>1)>=x ){
138694 sqlite3ErrorMsg(pParse, "ON clause references tables to its right");
138695 return;
138696 }
138697 }
138698 pTerm->prereqAll = prereqAll;
138699 pTerm->leftCursor = -1;
138700 pTerm->iParent = -1;
138701 pTerm->eOperator = 0;
138702 if( allowedOp(op) ){
138703 int aiCurCol[2];
138704 Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
138705 Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
138706 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
138707
138708 if( pTerm->iField>0 ){
138709 assert( op==TK_IN );
138710 assert( pLeft->op==TK_VECTOR );
138711 pLeft = pLeft->x.pList->a[pTerm->iField-1].pExpr;
138712 }
138713
138714 if( exprMightBeIndexed(pSrc, prereqLeft, aiCurCol, pLeft, op) ){
138715 pTerm->leftCursor = aiCurCol[0];
138716 pTerm->u.leftColumn = aiCurCol[1];
138717 pTerm->eOperator = operatorMask(op) & opMask;
138718 }
138719 if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
138720 if( pRight
138721 && exprMightBeIndexed(pSrc, pTerm->prereqRight, aiCurCol, pRight, op)
138722 ){
138723 WhereTerm *pNew;
138724 Expr *pDup;
138725 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
138726 assert( pTerm->iField==0 );
138727 if( pTerm->leftCursor>=0 ){
138728 int idxNew;
138729 pDup = sqlite3ExprDup(db, pExpr, 0);
138730 if( db->mallocFailed ){
138731 sqlite3ExprDelete(db, pDup);
138732 return;
138733 }
138734 idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
138735 if( idxNew==0 ) return;
138736 pNew = &pWC->a[idxNew];
138737 markTermAsChild(pWC, idxNew, idxTerm);
138738 if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
138739 pTerm = &pWC->a[idxTerm];
138740 pTerm->wtFlags |= TERM_COPIED;
138741
138742 if( termIsEquivalence(pParse, pDup) ){
138743 pTerm->eOperator |= WO_EQUIV;
138744 eExtraOp = WO_EQUIV;
138745 }
138746 }else{
138747 pDup = pExpr;
138748 pNew = pTerm;
138749 }
138750 exprCommute(pParse, pDup);
138751 pNew->leftCursor = aiCurCol[0];
138752 pNew->u.leftColumn = aiCurCol[1];
138753 testcase( (prereqLeft | extraRight) != prereqLeft );
138754 pNew->prereqRight = prereqLeft | extraRight;
138755 pNew->prereqAll = prereqAll;
138756 pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
138757 }
138758 }
138759
138760#ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
138761 /* If a term is the BETWEEN operator, create two new virtual terms
138762 ** that define the range that the BETWEEN implements. For example:
138763 **
138764 ** a BETWEEN b AND c
138765 **
138766 ** is converted into:
138767 **
138768 ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
138769 **
138770 ** The two new terms are added onto the end of the WhereClause object.
138771 ** The new terms are "dynamic" and are children of the original BETWEEN
138772 ** term. That means that if the BETWEEN term is coded, the children are
138773 ** skipped. Or, if the children are satisfied by an index, the original
138774 ** BETWEEN term is skipped.
138775 */
138776 else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
138777 ExprList *pList = pExpr->x.pList;
138778 int i;
138779 static const u8 ops[] = {TK_GE, TK_LE};
138780 assert( pList!=0 );
138781 assert( pList->nExpr==2 );
138782 for(i=0; i<2; i++){
138783 Expr *pNewExpr;
138784 int idxNew;
138785 pNewExpr = sqlite3PExpr(pParse, ops[i],
138786 sqlite3ExprDup(db, pExpr->pLeft, 0),
138787 sqlite3ExprDup(db, pList->a[i].pExpr, 0));
138788 transferJoinMarkings(pNewExpr, pExpr);
138789 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
138790 testcase( idxNew==0 );
138791 exprAnalyze(pSrc, pWC, idxNew);
138792 pTerm = &pWC->a[idxTerm];
138793 markTermAsChild(pWC, idxNew, idxTerm);
138794 }
138795 }
138796#endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
138797
138798#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
138799 /* Analyze a term that is composed of two or more subterms connected by
138800 ** an OR operator.
138801 */
138802 else if( pExpr->op==TK_OR ){
138803 assert( pWC->op==TK_AND );
138804 exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
138805 pTerm = &pWC->a[idxTerm];
138806 }
138807#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
138808
138809#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
138810 /* Add constraints to reduce the search space on a LIKE or GLOB
138811 ** operator.
138812 **
138813 ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints
138814 **
138815 ** x>='ABC' AND x<'abd' AND x LIKE 'aBc%'
138816 **
138817 ** The last character of the prefix "abc" is incremented to form the
138818 ** termination condition "abd". If case is not significant (the default
138819 ** for LIKE) then the lower-bound is made all uppercase and the upper-
138820 ** bound is made all lowercase so that the bounds also work when comparing
138821 ** BLOBs.
138822 */
138823 if( pWC->op==TK_AND
138824 && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
138825 ){
138826 Expr *pLeft; /* LHS of LIKE/GLOB operator */
138827 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
138828 Expr *pNewExpr1;
138829 Expr *pNewExpr2;
138830 int idxNew1;
138831 int idxNew2;
138832 const char *zCollSeqName; /* Name of collating sequence */
138833 const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC;
138834
138835 pLeft = pExpr->x.pList->a[1].pExpr;
138836 pStr2 = sqlite3ExprDup(db, pStr1, 0);
138837
138838 /* Convert the lower bound to upper-case and the upper bound to
138839 ** lower-case (upper-case is less than lower-case in ASCII) so that
138840 ** the range constraints also work for BLOBs
138841 */
138842 if( noCase && !pParse->db->mallocFailed ){
138843 int i;
138844 char c;
138845 pTerm->wtFlags |= TERM_LIKE;
138846 for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
138847 pStr1->u.zToken[i] = sqlite3Toupper(c);
138848 pStr2->u.zToken[i] = sqlite3Tolower(c);
138849 }
138850 }
138851
138852 if( !db->mallocFailed ){
138853 u8 c, *pC; /* Last character before the first wildcard */
138854 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
138855 c = *pC;
138856 if( noCase ){
138857 /* The point is to increment the last character before the first
138858 ** wildcard. But if we increment '@', that will push it into the
138859 ** alphabetic range where case conversions will mess up the
138860 ** inequality. To avoid this, make sure to also run the full
138861 ** LIKE on all candidate expressions by clearing the isComplete flag
138862 */
138863 if( c=='A'-1 ) isComplete = 0;
138864 c = sqlite3UpperToLower[c];
138865 }
138866 *pC = c + 1;
138867 }
138868 zCollSeqName = noCase ? "NOCASE" : sqlite3StrBINARY;
138869 pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
138870 pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
138871 sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName),
138872 pStr1);
138873 transferJoinMarkings(pNewExpr1, pExpr);
138874 idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags);
138875 testcase( idxNew1==0 );
138876 exprAnalyze(pSrc, pWC, idxNew1);
138877 pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
138878 pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
138879 sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName),
138880 pStr2);
138881 transferJoinMarkings(pNewExpr2, pExpr);
138882 idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags);
138883 testcase( idxNew2==0 );
138884 exprAnalyze(pSrc, pWC, idxNew2);
138885 pTerm = &pWC->a[idxTerm];
138886 if( isComplete ){
138887 markTermAsChild(pWC, idxNew1, idxTerm);
138888 markTermAsChild(pWC, idxNew2, idxTerm);
138889 }
138890 }
138891#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
138892
138893#ifndef SQLITE_OMIT_VIRTUALTABLE
138894 /* Add a WO_AUX auxiliary term to the constraint set if the
138895 ** current expression is of the form "column OP expr" where OP
138896 ** is an operator that gets passed into virtual tables but which is
138897 ** not normally optimized for ordinary tables. In other words, OP
138898 ** is one of MATCH, LIKE, GLOB, REGEXP, !=, IS, IS NOT, or NOT NULL.
138899 ** This information is used by the xBestIndex methods of
138900 ** virtual tables. The native query optimizer does not attempt
138901 ** to do anything with MATCH functions.
138902 */
138903 if( pWC->op==TK_AND ){
138904 Expr *pRight = 0, *pLeft = 0;
138905 int res = isAuxiliaryVtabOperator(db, pExpr, &eOp2, &pLeft, &pRight);
138906 while( res-- > 0 ){
138907 int idxNew;
138908 WhereTerm *pNewTerm;
138909 Bitmask prereqColumn, prereqExpr;
138910
138911 prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight);
138912 prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft);
138913 if( (prereqExpr & prereqColumn)==0 ){
138914 Expr *pNewExpr;
138915 pNewExpr = sqlite3PExpr(pParse, TK_MATCH,
138916 0, sqlite3ExprDup(db, pRight, 0));
138917 if( ExprHasProperty(pExpr, EP_FromJoin) && pNewExpr ){
138918 ExprSetProperty(pNewExpr, EP_FromJoin);
138919 }
138920 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
138921 testcase( idxNew==0 );
138922 pNewTerm = &pWC->a[idxNew];
138923 pNewTerm->prereqRight = prereqExpr;
138924 pNewTerm->leftCursor = pLeft->iTable;
138925 pNewTerm->u.leftColumn = pLeft->iColumn;
138926 pNewTerm->eOperator = WO_AUX;
138927 pNewTerm->eMatchOp = eOp2;
138928 markTermAsChild(pWC, idxNew, idxTerm);
138929 pTerm = &pWC->a[idxTerm];
138930 pTerm->wtFlags |= TERM_COPIED;
138931 pNewTerm->prereqAll = pTerm->prereqAll;
138932 }
138933 SWAP(Expr*, pLeft, pRight);
138934 }
138935 }
138936#endif /* SQLITE_OMIT_VIRTUALTABLE */
138937
138938 /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
138939 ** new terms for each component comparison - "a = ?" and "b = ?". The
138940 ** new terms completely replace the original vector comparison, which is
138941 ** no longer used.
138942 **
138943 ** This is only required if at least one side of the comparison operation
138944 ** is not a sub-select. */
138945 if( pWC->op==TK_AND
138946 && (pExpr->op==TK_EQ || pExpr->op==TK_IS)
138947 && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
138948 && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
138949 && ( (pExpr->pLeft->flags & EP_xIsSelect)==0
138950 || (pExpr->pRight->flags & EP_xIsSelect)==0)
138951 ){
138952 int i;
138953 for(i=0; i<nLeft; i++){
138954 int idxNew;
138955 Expr *pNew;
138956 Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i);
138957 Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i);
138958
138959 pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
138960 transferJoinMarkings(pNew, pExpr);
138961 idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC);
138962 exprAnalyze(pSrc, pWC, idxNew);
138963 }
138964 pTerm = &pWC->a[idxTerm];
138965 pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL; /* Disable the original */
138966 pTerm->eOperator = 0;
138967 }
138968
138969 /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
138970 ** a virtual term for each vector component. The expression object
138971 ** used by each such virtual term is pExpr (the full vector IN(...)
138972 ** expression). The WhereTerm.iField variable identifies the index within
138973 ** the vector on the LHS that the virtual term represents.
138974 **
138975 ** This only works if the RHS is a simple SELECT, not a compound
138976 */
138977 if( pWC->op==TK_AND && pExpr->op==TK_IN && pTerm->iField==0
138978 && pExpr->pLeft->op==TK_VECTOR
138979 && pExpr->x.pSelect->pPrior==0
138980 ){
138981 int i;
138982 for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){
138983 int idxNew;
138984 idxNew = whereClauseInsert(pWC, pExpr, TERM_VIRTUAL);
138985 pWC->a[idxNew].iField = i+1;
138986 exprAnalyze(pSrc, pWC, idxNew);
138987 markTermAsChild(pWC, idxNew, idxTerm);
138988 }
138989 }
138990
138991#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
138992 /* When sqlite_stat3 histogram data is available an operator of the
138993 ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
138994 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
138995 ** virtual term of that form.
138996 **
138997 ** Note that the virtual term must be tagged with TERM_VNULL.
138998 */
138999 if( pExpr->op==TK_NOTNULL
139000 && pExpr->pLeft->op==TK_COLUMN
139001 && pExpr->pLeft->iColumn>=0
139002 && !ExprHasProperty(pExpr, EP_FromJoin)
139003 && OptimizationEnabled(db, SQLITE_Stat34)
139004 ){
139005 Expr *pNewExpr;
139006 Expr *pLeft = pExpr->pLeft;
139007 int idxNew;
139008 WhereTerm *pNewTerm;
139009
139010 pNewExpr = sqlite3PExpr(pParse, TK_GT,
139011 sqlite3ExprDup(db, pLeft, 0),
139012 sqlite3ExprAlloc(db, TK_NULL, 0, 0));
139013
139014 idxNew = whereClauseInsert(pWC, pNewExpr,
139015 TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
139016 if( idxNew ){
139017 pNewTerm = &pWC->a[idxNew];
139018 pNewTerm->prereqRight = 0;
139019 pNewTerm->leftCursor = pLeft->iTable;
139020 pNewTerm->u.leftColumn = pLeft->iColumn;
139021 pNewTerm->eOperator = WO_GT;
139022 markTermAsChild(pWC, idxNew, idxTerm);
139023 pTerm = &pWC->a[idxTerm];
139024 pTerm->wtFlags |= TERM_COPIED;
139025 pNewTerm->prereqAll = pTerm->prereqAll;
139026 }
139027 }
139028#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
139029
139030 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
139031 ** an index for tables to the left of the join.
139032 */
139033 testcase( pTerm!=&pWC->a[idxTerm] );
139034 pTerm = &pWC->a[idxTerm];
139035 pTerm->prereqRight |= extraRight;
139036}
139037
139038/***************************************************************************
139039** Routines with file scope above. Interface to the rest of the where.c
139040** subsystem follows.
139041***************************************************************************/
139042
139043/*
139044** This routine identifies subexpressions in the WHERE clause where
139045** each subexpression is separated by the AND operator or some other
139046** operator specified in the op parameter. The WhereClause structure
139047** is filled with pointers to subexpressions. For example:
139048**
139049** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
139050** \________/ \_______________/ \________________/
139051** slot[0] slot[1] slot[2]
139052**
139053** The original WHERE clause in pExpr is unaltered. All this routine
139054** does is make slot[] entries point to substructure within pExpr.
139055**
139056** In the previous sentence and in the diagram, "slot[]" refers to
139057** the WhereClause.a[] array. The slot[] array grows as needed to contain
139058** all terms of the WHERE clause.
139059*/
139060SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
139061 Expr *pE2 = sqlite3ExprSkipCollate(pExpr);
139062 pWC->op = op;
139063 if( pE2==0 ) return;
139064 if( pE2->op!=op ){
139065 whereClauseInsert(pWC, pExpr, 0);
139066 }else{
139067 sqlite3WhereSplit(pWC, pE2->pLeft, op);
139068 sqlite3WhereSplit(pWC, pE2->pRight, op);
139069 }
139070}
139071
139072/*
139073** Initialize a preallocated WhereClause structure.
139074*/
139075SQLITE_PRIVATE void sqlite3WhereClauseInit(
139076 WhereClause *pWC, /* The WhereClause to be initialized */
139077 WhereInfo *pWInfo /* The WHERE processing context */
139078){
139079 pWC->pWInfo = pWInfo;
139080 pWC->hasOr = 0;
139081 pWC->pOuter = 0;
139082 pWC->nTerm = 0;
139083 pWC->nSlot = ArraySize(pWC->aStatic);
139084 pWC->a = pWC->aStatic;
139085}
139086
139087/*
139088** Deallocate a WhereClause structure. The WhereClause structure
139089** itself is not freed. This routine is the inverse of
139090** sqlite3WhereClauseInit().
139091*/
139092SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause *pWC){
139093 int i;
139094 WhereTerm *a;
139095 sqlite3 *db = pWC->pWInfo->pParse->db;
139096 for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
139097 if( a->wtFlags & TERM_DYNAMIC ){
139098 sqlite3ExprDelete(db, a->pExpr);
139099 }
139100 if( a->wtFlags & TERM_ORINFO ){
139101 whereOrInfoDelete(db, a->u.pOrInfo);
139102 }else if( a->wtFlags & TERM_ANDINFO ){
139103 whereAndInfoDelete(db, a->u.pAndInfo);
139104 }
139105 }
139106 if( pWC->a!=pWC->aStatic ){
139107 sqlite3DbFree(db, pWC->a);
139108 }
139109}
139110
139111
139112/*
139113** These routines walk (recursively) an expression tree and generate
139114** a bitmask indicating which tables are used in that expression
139115** tree.
139116*/
139117SQLITE_PRIVATE Bitmask sqlite3WhereExprUsageNN(WhereMaskSet *pMaskSet, Expr *p){
139118 Bitmask mask;
139119 if( p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
139120 return sqlite3WhereGetMask(pMaskSet, p->iTable);
139121 }else if( ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
139122 assert( p->op!=TK_IF_NULL_ROW );
139123 return 0;
139124 }
139125 mask = (p->op==TK_IF_NULL_ROW) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0;
139126 if( p->pLeft ) mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pLeft);
139127 if( p->pRight ){
139128 mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pRight);
139129 assert( p->x.pList==0 );
139130 }else if( ExprHasProperty(p, EP_xIsSelect) ){
139131 if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1;
139132 mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
139133 }else if( p->x.pList ){
139134 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
139135 }
139136 return mask;
139137}
139138SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
139139 return p ? sqlite3WhereExprUsageNN(pMaskSet,p) : 0;
139140}
139141SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){
139142 int i;
139143 Bitmask mask = 0;
139144 if( pList ){
139145 for(i=0; i<pList->nExpr; i++){
139146 mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
139147 }
139148 }
139149 return mask;
139150}
139151
139152
139153/*
139154** Call exprAnalyze on all terms in a WHERE clause.
139155**
139156** Note that exprAnalyze() might add new virtual terms onto the
139157** end of the WHERE clause. We do not want to analyze these new
139158** virtual terms, so start analyzing at the end and work forward
139159** so that the added virtual terms are never processed.
139160*/
139161SQLITE_PRIVATE void sqlite3WhereExprAnalyze(
139162 SrcList *pTabList, /* the FROM clause */
139163 WhereClause *pWC /* the WHERE clause to be analyzed */
139164){
139165 int i;
139166 for(i=pWC->nTerm-1; i>=0; i--){
139167 exprAnalyze(pTabList, pWC, i);
139168 }
139169}
139170
139171/*
139172** For table-valued-functions, transform the function arguments into
139173** new WHERE clause terms.
139174**
139175** Each function argument translates into an equality constraint against
139176** a HIDDEN column in the table.
139177*/
139178SQLITE_PRIVATE void sqlite3WhereTabFuncArgs(
139179 Parse *pParse, /* Parsing context */
139180 struct SrcList_item *pItem, /* The FROM clause term to process */
139181 WhereClause *pWC /* Xfer function arguments to here */
139182){
139183 Table *pTab;
139184 int j, k;
139185 ExprList *pArgs;
139186 Expr *pColRef;
139187 Expr *pTerm;
139188 if( pItem->fg.isTabFunc==0 ) return;
139189 pTab = pItem->pTab;
139190 assert( pTab!=0 );
139191 pArgs = pItem->u1.pFuncArg;
139192 if( pArgs==0 ) return;
139193 for(j=k=0; j<pArgs->nExpr; j++){
139194 Expr *pRhs;
139195 while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
139196 if( k>=pTab->nCol ){
139197 sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
139198 pTab->zName, j);
139199 return;
139200 }
139201 pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
139202 if( pColRef==0 ) return;
139203 pColRef->iTable = pItem->iCursor;
139204 pColRef->iColumn = k++;
139205 pColRef->y.pTab = pTab;
139206 pRhs = sqlite3PExpr(pParse, TK_UPLUS,
139207 sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
139208 pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef, pRhs);
139209 whereClauseInsert(pWC, pTerm, TERM_DYNAMIC);
139210 }
139211}
139212
139213/************** End of whereexpr.c *******************************************/
139214/************** Begin file where.c *******************************************/
139215/*
139216** 2001 September 15
139217**
139218** The author disclaims copyright to this source code. In place of
139219** a legal notice, here is a blessing:
139220**
139221** May you do good and not evil.
139222** May you find forgiveness for yourself and forgive others.
139223** May you share freely, never taking more than you give.
139224**
139225*************************************************************************
139226** This module contains C code that generates VDBE code used to process
139227** the WHERE clause of SQL statements. This module is responsible for
139228** generating the code that loops through a table looking for applicable
139229** rows. Indices are selected and used to speed the search when doing
139230** so is applicable. Because this module is responsible for selecting
139231** indices, you might also think of this module as the "query optimizer".
139232*/
139233/* #include "sqliteInt.h" */
139234/* #include "whereInt.h" */
139235
139236/*
139237** Extra information appended to the end of sqlite3_index_info but not
139238** visible to the xBestIndex function, at least not directly. The
139239** sqlite3_vtab_collation() interface knows how to reach it, however.
139240**
139241** This object is not an API and can be changed from one release to the
139242** next. As long as allocateIndexInfo() and sqlite3_vtab_collation()
139243** agree on the structure, all will be well.
139244*/
139245typedef struct HiddenIndexInfo HiddenIndexInfo;
139246struct HiddenIndexInfo {
139247 WhereClause *pWC; /* The Where clause being analyzed */
139248 Parse *pParse; /* The parsing context */
139249};
139250
139251/* Forward declaration of methods */
139252static int whereLoopResize(sqlite3*, WhereLoop*, int);
139253
139254/* Test variable that can be set to enable WHERE tracing */
139255#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
139256/***/ int sqlite3WhereTrace = 0;
139257#endif
139258
139259
139260/*
139261** Return the estimated number of output rows from a WHERE clause
139262*/
139263SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
139264 return pWInfo->nRowOut;
139265}
139266
139267/*
139268** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
139269** WHERE clause returns outputs for DISTINCT processing.
139270*/
139271SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
139272 return pWInfo->eDistinct;
139273}
139274
139275/*
139276** Return TRUE if the WHERE clause returns rows in ORDER BY order.
139277** Return FALSE if the output needs to be sorted.
139278*/
139279SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
139280 return pWInfo->nOBSat;
139281}
139282
139283/*
139284** In the ORDER BY LIMIT optimization, if the inner-most loop is known
139285** to emit rows in increasing order, and if the last row emitted by the
139286** inner-most loop did not fit within the sorter, then we can skip all
139287** subsequent rows for the current iteration of the inner loop (because they
139288** will not fit in the sorter either) and continue with the second inner
139289** loop - the loop immediately outside the inner-most.
139290**
139291** When a row does not fit in the sorter (because the sorter already
139292** holds LIMIT+OFFSET rows that are smaller), then a jump is made to the
139293** label returned by this function.
139294**
139295** If the ORDER BY LIMIT optimization applies, the jump destination should
139296** be the continuation for the second-inner-most loop. If the ORDER BY
139297** LIMIT optimization does not apply, then the jump destination should
139298** be the continuation for the inner-most loop.
139299**
139300** It is always safe for this routine to return the continuation of the
139301** inner-most loop, in the sense that a correct answer will result.
139302** Returning the continuation the second inner loop is an optimization
139303** that might make the code run a little faster, but should not change
139304** the final answer.
139305*/
139306SQLITE_PRIVATE int sqlite3WhereOrderByLimitOptLabel(WhereInfo *pWInfo){
139307 WhereLevel *pInner;
139308 if( !pWInfo->bOrderedInnerLoop ){
139309 /* The ORDER BY LIMIT optimization does not apply. Jump to the
139310 ** continuation of the inner-most loop. */
139311 return pWInfo->iContinue;
139312 }
139313 pInner = &pWInfo->a[pWInfo->nLevel-1];
139314 assert( pInner->addrNxt!=0 );
139315 return pInner->addrNxt;
139316}
139317
139318/*
139319** Return the VDBE address or label to jump to in order to continue
139320** immediately with the next row of a WHERE clause.
139321*/
139322SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
139323 assert( pWInfo->iContinue!=0 );
139324 return pWInfo->iContinue;
139325}
139326
139327/*
139328** Return the VDBE address or label to jump to in order to break
139329** out of a WHERE loop.
139330*/
139331SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){
139332 return pWInfo->iBreak;
139333}
139334
139335/*
139336** Return ONEPASS_OFF (0) if an UPDATE or DELETE statement is unable to
139337** operate directly on the rowis returned by a WHERE clause. Return
139338** ONEPASS_SINGLE (1) if the statement can operation directly because only
139339** a single row is to be changed. Return ONEPASS_MULTI (2) if the one-pass
139340** optimization can be used on multiple
139341**
139342** If the ONEPASS optimization is used (if this routine returns true)
139343** then also write the indices of open cursors used by ONEPASS
139344** into aiCur[0] and aiCur[1]. iaCur[0] gets the cursor of the data
139345** table and iaCur[1] gets the cursor used by an auxiliary index.
139346** Either value may be -1, indicating that cursor is not used.
139347** Any cursors returned will have been opened for writing.
139348**
139349** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
139350** unable to use the ONEPASS optimization.
139351*/
139352SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){
139353 memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
139354#ifdef WHERETRACE_ENABLED
139355 if( sqlite3WhereTrace && pWInfo->eOnePass!=ONEPASS_OFF ){
139356 sqlite3DebugPrintf("%s cursors: %d %d\n",
139357 pWInfo->eOnePass==ONEPASS_SINGLE ? "ONEPASS_SINGLE" : "ONEPASS_MULTI",
139358 aiCur[0], aiCur[1]);
139359 }
139360#endif
139361 return pWInfo->eOnePass;
139362}
139363
139364/*
139365** Move the content of pSrc into pDest
139366*/
139367static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){
139368 pDest->n = pSrc->n;
139369 memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
139370}
139371
139372/*
139373** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet.
139374**
139375** The new entry might overwrite an existing entry, or it might be
139376** appended, or it might be discarded. Do whatever is the right thing
139377** so that pSet keeps the N_OR_COST best entries seen so far.
139378*/
139379static int whereOrInsert(
139380 WhereOrSet *pSet, /* The WhereOrSet to be updated */
139381 Bitmask prereq, /* Prerequisites of the new entry */
139382 LogEst rRun, /* Run-cost of the new entry */
139383 LogEst nOut /* Number of outputs for the new entry */
139384){
139385 u16 i;
139386 WhereOrCost *p;
139387 for(i=pSet->n, p=pSet->a; i>0; i--, p++){
139388 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
139389 goto whereOrInsert_done;
139390 }
139391 if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
139392 return 0;
139393 }
139394 }
139395 if( pSet->n<N_OR_COST ){
139396 p = &pSet->a[pSet->n++];
139397 p->nOut = nOut;
139398 }else{
139399 p = pSet->a;
139400 for(i=1; i<pSet->n; i++){
139401 if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
139402 }
139403 if( p->rRun<=rRun ) return 0;
139404 }
139405whereOrInsert_done:
139406 p->prereq = prereq;
139407 p->rRun = rRun;
139408 if( p->nOut>nOut ) p->nOut = nOut;
139409 return 1;
139410}
139411
139412/*
139413** Return the bitmask for the given cursor number. Return 0 if
139414** iCursor is not in the set.
139415*/
139416SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet *pMaskSet, int iCursor){
139417 int i;
139418 assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
139419 for(i=0; i<pMaskSet->n; i++){
139420 if( pMaskSet->ix[i]==iCursor ){
139421 return MASKBIT(i);
139422 }
139423 }
139424 return 0;
139425}
139426
139427/*
139428** Create a new mask for cursor iCursor.
139429**
139430** There is one cursor per table in the FROM clause. The number of
139431** tables in the FROM clause is limited by a test early in the
139432** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
139433** array will never overflow.
139434*/
139435static void createMask(WhereMaskSet *pMaskSet, int iCursor){
139436 assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
139437 pMaskSet->ix[pMaskSet->n++] = iCursor;
139438}
139439
139440/*
139441** Advance to the next WhereTerm that matches according to the criteria
139442** established when the pScan object was initialized by whereScanInit().
139443** Return NULL if there are no more matching WhereTerms.
139444*/
139445static WhereTerm *whereScanNext(WhereScan *pScan){
139446 int iCur; /* The cursor on the LHS of the term */
139447 i16 iColumn; /* The column on the LHS of the term. -1 for IPK */
139448 Expr *pX; /* An expression being tested */
139449 WhereClause *pWC; /* Shorthand for pScan->pWC */
139450 WhereTerm *pTerm; /* The term being tested */
139451 int k = pScan->k; /* Where to start scanning */
139452
139453 assert( pScan->iEquiv<=pScan->nEquiv );
139454 pWC = pScan->pWC;
139455 while(1){
139456 iColumn = pScan->aiColumn[pScan->iEquiv-1];
139457 iCur = pScan->aiCur[pScan->iEquiv-1];
139458 assert( pWC!=0 );
139459 do{
139460 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
139461 if( pTerm->leftCursor==iCur
139462 && pTerm->u.leftColumn==iColumn
139463 && (iColumn!=XN_EXPR
139464 || sqlite3ExprCompareSkip(pTerm->pExpr->pLeft,
139465 pScan->pIdxExpr,iCur)==0)
139466 && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin))
139467 ){
139468 if( (pTerm->eOperator & WO_EQUIV)!=0
139469 && pScan->nEquiv<ArraySize(pScan->aiCur)
139470 && (pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight))->op==TK_COLUMN
139471 ){
139472 int j;
139473 for(j=0; j<pScan->nEquiv; j++){
139474 if( pScan->aiCur[j]==pX->iTable
139475 && pScan->aiColumn[j]==pX->iColumn ){
139476 break;
139477 }
139478 }
139479 if( j==pScan->nEquiv ){
139480 pScan->aiCur[j] = pX->iTable;
139481 pScan->aiColumn[j] = pX->iColumn;
139482 pScan->nEquiv++;
139483 }
139484 }
139485 if( (pTerm->eOperator & pScan->opMask)!=0 ){
139486 /* Verify the affinity and collating sequence match */
139487 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
139488 CollSeq *pColl;
139489 Parse *pParse = pWC->pWInfo->pParse;
139490 pX = pTerm->pExpr;
139491 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
139492 continue;
139493 }
139494 assert(pX->pLeft);
139495 pColl = sqlite3BinaryCompareCollSeq(pParse,
139496 pX->pLeft, pX->pRight);
139497 if( pColl==0 ) pColl = pParse->db->pDfltColl;
139498 if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
139499 continue;
139500 }
139501 }
139502 if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
139503 && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
139504 && pX->iTable==pScan->aiCur[0]
139505 && pX->iColumn==pScan->aiColumn[0]
139506 ){
139507 testcase( pTerm->eOperator & WO_IS );
139508 continue;
139509 }
139510 pScan->pWC = pWC;
139511 pScan->k = k+1;
139512 return pTerm;
139513 }
139514 }
139515 }
139516 pWC = pWC->pOuter;
139517 k = 0;
139518 }while( pWC!=0 );
139519 if( pScan->iEquiv>=pScan->nEquiv ) break;
139520 pWC = pScan->pOrigWC;
139521 k = 0;
139522 pScan->iEquiv++;
139523 }
139524 return 0;
139525}
139526
139527/*
139528** Initialize a WHERE clause scanner object. Return a pointer to the
139529** first match. Return NULL if there are no matches.
139530**
139531** The scanner will be searching the WHERE clause pWC. It will look
139532** for terms of the form "X <op> <expr>" where X is column iColumn of table
139533** iCur. Or if pIdx!=0 then X is column iColumn of index pIdx. pIdx
139534** must be one of the indexes of table iCur.
139535**
139536** The <op> must be one of the operators described by opMask.
139537**
139538** If the search is for X and the WHERE clause contains terms of the
139539** form X=Y then this routine might also return terms of the form
139540** "Y <op> <expr>". The number of levels of transitivity is limited,
139541** but is enough to handle most commonly occurring SQL statements.
139542**
139543** If X is not the INTEGER PRIMARY KEY then X must be compatible with
139544** index pIdx.
139545*/
139546static WhereTerm *whereScanInit(
139547 WhereScan *pScan, /* The WhereScan object being initialized */
139548 WhereClause *pWC, /* The WHERE clause to be scanned */
139549 int iCur, /* Cursor to scan for */
139550 int iColumn, /* Column to scan for */
139551 u32 opMask, /* Operator(s) to scan for */
139552 Index *pIdx /* Must be compatible with this index */
139553){
139554 pScan->pOrigWC = pWC;
139555 pScan->pWC = pWC;
139556 pScan->pIdxExpr = 0;
139557 pScan->idxaff = 0;
139558 pScan->zCollName = 0;
139559 if( pIdx ){
139560 int j = iColumn;
139561 iColumn = pIdx->aiColumn[j];
139562 if( iColumn==XN_EXPR ){
139563 pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
139564 pScan->zCollName = pIdx->azColl[j];
139565 }else if( iColumn==pIdx->pTable->iPKey ){
139566 iColumn = XN_ROWID;
139567 }else if( iColumn>=0 ){
139568 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
139569 pScan->zCollName = pIdx->azColl[j];
139570 }
139571 }else if( iColumn==XN_EXPR ){
139572 return 0;
139573 }
139574 pScan->opMask = opMask;
139575 pScan->k = 0;
139576 pScan->aiCur[0] = iCur;
139577 pScan->aiColumn[0] = iColumn;
139578 pScan->nEquiv = 1;
139579 pScan->iEquiv = 1;
139580 return whereScanNext(pScan);
139581}
139582
139583/*
139584** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
139585** where X is a reference to the iColumn of table iCur or of index pIdx
139586** if pIdx!=0 and <op> is one of the WO_xx operator codes specified by
139587** the op parameter. Return a pointer to the term. Return 0 if not found.
139588**
139589** If pIdx!=0 then it must be one of the indexes of table iCur.
139590** Search for terms matching the iColumn-th column of pIdx
139591** rather than the iColumn-th column of table iCur.
139592**
139593** The term returned might by Y=<expr> if there is another constraint in
139594** the WHERE clause that specifies that X=Y. Any such constraints will be
139595** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
139596** aiCur[]/iaColumn[] arrays hold X and all its equivalents. There are 11
139597** slots in aiCur[]/aiColumn[] so that means we can look for X plus up to 10
139598** other equivalent values. Hence a search for X will return <expr> if X=A1
139599** and A1=A2 and A2=A3 and ... and A9=A10 and A10=<expr>.
139600**
139601** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
139602** then try for the one with no dependencies on <expr> - in other words where
139603** <expr> is a constant expression of some kind. Only return entries of
139604** the form "X <op> Y" where Y is a column in another table if no terms of
139605** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
139606** exist, try to return a term that does not use WO_EQUIV.
139607*/
139608SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
139609 WhereClause *pWC, /* The WHERE clause to be searched */
139610 int iCur, /* Cursor number of LHS */
139611 int iColumn, /* Column number of LHS */
139612 Bitmask notReady, /* RHS must not overlap with this mask */
139613 u32 op, /* Mask of WO_xx values describing operator */
139614 Index *pIdx /* Must be compatible with this index, if not NULL */
139615){
139616 WhereTerm *pResult = 0;
139617 WhereTerm *p;
139618 WhereScan scan;
139619
139620 p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
139621 op &= WO_EQ|WO_IS;
139622 while( p ){
139623 if( (p->prereqRight & notReady)==0 ){
139624 if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
139625 testcase( p->eOperator & WO_IS );
139626 return p;
139627 }
139628 if( pResult==0 ) pResult = p;
139629 }
139630 p = whereScanNext(&scan);
139631 }
139632 return pResult;
139633}
139634
139635/*
139636** This function searches pList for an entry that matches the iCol-th column
139637** of index pIdx.
139638**
139639** If such an expression is found, its index in pList->a[] is returned. If
139640** no expression is found, -1 is returned.
139641*/
139642static int findIndexCol(
139643 Parse *pParse, /* Parse context */
139644 ExprList *pList, /* Expression list to search */
139645 int iBase, /* Cursor for table associated with pIdx */
139646 Index *pIdx, /* Index to match column of */
139647 int iCol /* Column of index to match */
139648){
139649 int i;
139650 const char *zColl = pIdx->azColl[iCol];
139651
139652 for(i=0; i<pList->nExpr; i++){
139653 Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr);
139654 if( p->op==TK_COLUMN
139655 && p->iColumn==pIdx->aiColumn[iCol]
139656 && p->iTable==iBase
139657 ){
139658 CollSeq *pColl = sqlite3ExprNNCollSeq(pParse, pList->a[i].pExpr);
139659 if( 0==sqlite3StrICmp(pColl->zName, zColl) ){
139660 return i;
139661 }
139662 }
139663 }
139664
139665 return -1;
139666}
139667
139668/*
139669** Return TRUE if the iCol-th column of index pIdx is NOT NULL
139670*/
139671static int indexColumnNotNull(Index *pIdx, int iCol){
139672 int j;
139673 assert( pIdx!=0 );
139674 assert( iCol>=0 && iCol<pIdx->nColumn );
139675 j = pIdx->aiColumn[iCol];
139676 if( j>=0 ){
139677 return pIdx->pTable->aCol[j].notNull;
139678 }else if( j==(-1) ){
139679 return 1;
139680 }else{
139681 assert( j==(-2) );
139682 return 0; /* Assume an indexed expression can always yield a NULL */
139683
139684 }
139685}
139686
139687/*
139688** Return true if the DISTINCT expression-list passed as the third argument
139689** is redundant.
139690**
139691** A DISTINCT list is redundant if any subset of the columns in the
139692** DISTINCT list are collectively unique and individually non-null.
139693*/
139694static int isDistinctRedundant(
139695 Parse *pParse, /* Parsing context */
139696 SrcList *pTabList, /* The FROM clause */
139697 WhereClause *pWC, /* The WHERE clause */
139698 ExprList *pDistinct /* The result set that needs to be DISTINCT */
139699){
139700 Table *pTab;
139701 Index *pIdx;
139702 int i;
139703 int iBase;
139704
139705 /* If there is more than one table or sub-select in the FROM clause of
139706 ** this query, then it will not be possible to show that the DISTINCT
139707 ** clause is redundant. */
139708 if( pTabList->nSrc!=1 ) return 0;
139709 iBase = pTabList->a[0].iCursor;
139710 pTab = pTabList->a[0].pTab;
139711
139712 /* If any of the expressions is an IPK column on table iBase, then return
139713 ** true. Note: The (p->iTable==iBase) part of this test may be false if the
139714 ** current SELECT is a correlated sub-query.
139715 */
139716 for(i=0; i<pDistinct->nExpr; i++){
139717 Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr);
139718 if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1;
139719 }
139720
139721 /* Loop through all indices on the table, checking each to see if it makes
139722 ** the DISTINCT qualifier redundant. It does so if:
139723 **
139724 ** 1. The index is itself UNIQUE, and
139725 **
139726 ** 2. All of the columns in the index are either part of the pDistinct
139727 ** list, or else the WHERE clause contains a term of the form "col=X",
139728 ** where X is a constant value. The collation sequences of the
139729 ** comparison and select-list expressions must match those of the index.
139730 **
139731 ** 3. All of those index columns for which the WHERE clause does not
139732 ** contain a "col=X" term are subject to a NOT NULL constraint.
139733 */
139734 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
139735 if( !IsUniqueIndex(pIdx) ) continue;
139736 for(i=0; i<pIdx->nKeyCol; i++){
139737 if( 0==sqlite3WhereFindTerm(pWC, iBase, i, ~(Bitmask)0, WO_EQ, pIdx) ){
139738 if( findIndexCol(pParse, pDistinct, iBase, pIdx, i)<0 ) break;
139739 if( indexColumnNotNull(pIdx, i)==0 ) break;
139740 }
139741 }
139742 if( i==pIdx->nKeyCol ){
139743 /* This index implies that the DISTINCT qualifier is redundant. */
139744 return 1;
139745 }
139746 }
139747
139748 return 0;
139749}
139750
139751
139752/*
139753** Estimate the logarithm of the input value to base 2.
139754*/
139755static LogEst estLog(LogEst N){
139756 return N<=10 ? 0 : sqlite3LogEst(N) - 33;
139757}
139758
139759/*
139760** Convert OP_Column opcodes to OP_Copy in previously generated code.
139761**
139762** This routine runs over generated VDBE code and translates OP_Column
139763** opcodes into OP_Copy when the table is being accessed via co-routine
139764** instead of via table lookup.
139765**
139766** If the bIncrRowid parameter is 0, then any OP_Rowid instructions on
139767** cursor iTabCur are transformed into OP_Null. Or, if bIncrRowid is non-zero,
139768** then each OP_Rowid is transformed into an instruction to increment the
139769** value stored in its output register.
139770*/
139771static void translateColumnToCopy(
139772 Parse *pParse, /* Parsing context */
139773 int iStart, /* Translate from this opcode to the end */
139774 int iTabCur, /* OP_Column/OP_Rowid references to this table */
139775 int iRegister, /* The first column is in this register */
139776 int bIncrRowid /* If non-zero, transform OP_rowid to OP_AddImm(1) */
139777){
139778 Vdbe *v = pParse->pVdbe;
139779 VdbeOp *pOp = sqlite3VdbeGetOp(v, iStart);
139780 int iEnd = sqlite3VdbeCurrentAddr(v);
139781 if( pParse->db->mallocFailed ) return;
139782 for(; iStart<iEnd; iStart++, pOp++){
139783 if( pOp->p1!=iTabCur ) continue;
139784 if( pOp->opcode==OP_Column ){
139785 pOp->opcode = OP_Copy;
139786 pOp->p1 = pOp->p2 + iRegister;
139787 pOp->p2 = pOp->p3;
139788 pOp->p3 = 0;
139789 }else if( pOp->opcode==OP_Rowid ){
139790 if( bIncrRowid ){
139791 /* Increment the value stored in the P2 operand of the OP_Rowid. */
139792 pOp->opcode = OP_AddImm;
139793 pOp->p1 = pOp->p2;
139794 pOp->p2 = 1;
139795 }else{
139796 pOp->opcode = OP_Null;
139797 pOp->p1 = 0;
139798 pOp->p3 = 0;
139799 }
139800 }
139801 }
139802}
139803
139804/*
139805** Two routines for printing the content of an sqlite3_index_info
139806** structure. Used for testing and debugging only. If neither
139807** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
139808** are no-ops.
139809*/
139810#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED)
139811static void TRACE_IDX_INPUTS(sqlite3_index_info *p){
139812 int i;
139813 if( !sqlite3WhereTrace ) return;
139814 for(i=0; i<p->nConstraint; i++){
139815 sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
139816 i,
139817 p->aConstraint[i].iColumn,
139818 p->aConstraint[i].iTermOffset,
139819 p->aConstraint[i].op,
139820 p->aConstraint[i].usable);
139821 }
139822 for(i=0; i<p->nOrderBy; i++){
139823 sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n",
139824 i,
139825 p->aOrderBy[i].iColumn,
139826 p->aOrderBy[i].desc);
139827 }
139828}
139829static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){
139830 int i;
139831 if( !sqlite3WhereTrace ) return;
139832 for(i=0; i<p->nConstraint; i++){
139833 sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n",
139834 i,
139835 p->aConstraintUsage[i].argvIndex,
139836 p->aConstraintUsage[i].omit);
139837 }
139838 sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum);
139839 sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr);
139840 sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed);
139841 sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost);
139842 sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows);
139843}
139844#else
139845#define TRACE_IDX_INPUTS(A)
139846#define TRACE_IDX_OUTPUTS(A)
139847#endif
139848
139849#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
139850/*
139851** Return TRUE if the WHERE clause term pTerm is of a form where it
139852** could be used with an index to access pSrc, assuming an appropriate
139853** index existed.
139854*/
139855static int termCanDriveIndex(
139856 WhereTerm *pTerm, /* WHERE clause term to check */
139857 struct SrcList_item *pSrc, /* Table we are trying to access */
139858 Bitmask notReady /* Tables in outer loops of the join */
139859){
139860 char aff;
139861 if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
139862 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0;
139863 if( (pSrc->fg.jointype & JT_LEFT)
139864 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
139865 && (pTerm->eOperator & WO_IS)
139866 ){
139867 /* Cannot use an IS term from the WHERE clause as an index driver for
139868 ** the RHS of a LEFT JOIN. Such a term can only be used if it is from
139869 ** the ON clause. */
139870 return 0;
139871 }
139872 if( (pTerm->prereqRight & notReady)!=0 ) return 0;
139873 if( pTerm->u.leftColumn<0 ) return 0;
139874 aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
139875 if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
139876 testcase( pTerm->pExpr->op==TK_IS );
139877 return 1;
139878}
139879#endif
139880
139881
139882#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
139883/*
139884** Generate code to construct the Index object for an automatic index
139885** and to set up the WhereLevel object pLevel so that the code generator
139886** makes use of the automatic index.
139887*/
139888static void constructAutomaticIndex(
139889 Parse *pParse, /* The parsing context */
139890 WhereClause *pWC, /* The WHERE clause */
139891 struct SrcList_item *pSrc, /* The FROM clause term to get the next index */
139892 Bitmask notReady, /* Mask of cursors that are not available */
139893 WhereLevel *pLevel /* Write new index here */
139894){
139895 int nKeyCol; /* Number of columns in the constructed index */
139896 WhereTerm *pTerm; /* A single term of the WHERE clause */
139897 WhereTerm *pWCEnd; /* End of pWC->a[] */
139898 Index *pIdx; /* Object describing the transient index */
139899 Vdbe *v; /* Prepared statement under construction */
139900 int addrInit; /* Address of the initialization bypass jump */
139901 Table *pTable; /* The table being indexed */
139902 int addrTop; /* Top of the index fill loop */
139903 int regRecord; /* Register holding an index record */
139904 int n; /* Column counter */
139905 int i; /* Loop counter */
139906 int mxBitCol; /* Maximum column in pSrc->colUsed */
139907 CollSeq *pColl; /* Collating sequence to on a column */
139908 WhereLoop *pLoop; /* The Loop object */
139909 char *zNotUsed; /* Extra space on the end of pIdx */
139910 Bitmask idxCols; /* Bitmap of columns used for indexing */
139911 Bitmask extraCols; /* Bitmap of additional columns */
139912 u8 sentWarning = 0; /* True if a warnning has been issued */
139913 Expr *pPartial = 0; /* Partial Index Expression */
139914 int iContinue = 0; /* Jump here to skip excluded rows */
139915 struct SrcList_item *pTabItem; /* FROM clause term being indexed */
139916 int addrCounter = 0; /* Address where integer counter is initialized */
139917 int regBase; /* Array of registers where record is assembled */
139918
139919 /* Generate code to skip over the creation and initialization of the
139920 ** transient index on 2nd and subsequent iterations of the loop. */
139921 v = pParse->pVdbe;
139922 assert( v!=0 );
139923 addrInit = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
139924
139925 /* Count the number of columns that will be added to the index
139926 ** and used to match WHERE clause constraints */
139927 nKeyCol = 0;
139928 pTable = pSrc->pTab;
139929 pWCEnd = &pWC->a[pWC->nTerm];
139930 pLoop = pLevel->pWLoop;
139931 idxCols = 0;
139932 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
139933 Expr *pExpr = pTerm->pExpr;
139934 assert( !ExprHasProperty(pExpr, EP_FromJoin) /* prereq always non-zero */
139935 || pExpr->iRightJoinTable!=pSrc->iCursor /* for the right-hand */
139936 || pLoop->prereq!=0 ); /* table of a LEFT JOIN */
139937 if( pLoop->prereq==0
139938 && (pTerm->wtFlags & TERM_VIRTUAL)==0
139939 && !ExprHasProperty(pExpr, EP_FromJoin)
139940 && sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor) ){
139941 pPartial = sqlite3ExprAnd(pParse->db, pPartial,
139942 sqlite3ExprDup(pParse->db, pExpr, 0));
139943 }
139944 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
139945 int iCol = pTerm->u.leftColumn;
139946 Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
139947 testcase( iCol==BMS );
139948 testcase( iCol==BMS-1 );
139949 if( !sentWarning ){
139950 sqlite3_log(SQLITE_WARNING_AUTOINDEX,
139951 "automatic index on %s(%s)", pTable->zName,
139952 pTable->aCol[iCol].zName);
139953 sentWarning = 1;
139954 }
139955 if( (idxCols & cMask)==0 ){
139956 if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){
139957 goto end_auto_index_create;
139958 }
139959 pLoop->aLTerm[nKeyCol++] = pTerm;
139960 idxCols |= cMask;
139961 }
139962 }
139963 }
139964 assert( nKeyCol>0 );
139965 pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
139966 pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED
139967 | WHERE_AUTO_INDEX;
139968
139969 /* Count the number of additional columns needed to create a
139970 ** covering index. A "covering index" is an index that contains all
139971 ** columns that are needed by the query. With a covering index, the
139972 ** original table never needs to be accessed. Automatic indices must
139973 ** be a covering index because the index will not be updated if the
139974 ** original table changes and the index and table cannot both be used
139975 ** if they go out of sync.
139976 */
139977 extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
139978 mxBitCol = MIN(BMS-1,pTable->nCol);
139979 testcase( pTable->nCol==BMS-1 );
139980 testcase( pTable->nCol==BMS-2 );
139981 for(i=0; i<mxBitCol; i++){
139982 if( extraCols & MASKBIT(i) ) nKeyCol++;
139983 }
139984 if( pSrc->colUsed & MASKBIT(BMS-1) ){
139985 nKeyCol += pTable->nCol - BMS + 1;
139986 }
139987
139988 /* Construct the Index object to describe this index */
139989 pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
139990 if( pIdx==0 ) goto end_auto_index_create;
139991 pLoop->u.btree.pIndex = pIdx;
139992 pIdx->zName = "auto-index";
139993 pIdx->pTable = pTable;
139994 n = 0;
139995 idxCols = 0;
139996 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
139997 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
139998 int iCol = pTerm->u.leftColumn;
139999 Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
140000 testcase( iCol==BMS-1 );
140001 testcase( iCol==BMS );
140002 if( (idxCols & cMask)==0 ){
140003 Expr *pX = pTerm->pExpr;
140004 idxCols |= cMask;
140005 pIdx->aiColumn[n] = pTerm->u.leftColumn;
140006 pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
140007 pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY;
140008 n++;
140009 }
140010 }
140011 }
140012 assert( (u32)n==pLoop->u.btree.nEq );
140013
140014 /* Add additional columns needed to make the automatic index into
140015 ** a covering index */
140016 for(i=0; i<mxBitCol; i++){
140017 if( extraCols & MASKBIT(i) ){
140018 pIdx->aiColumn[n] = i;
140019 pIdx->azColl[n] = sqlite3StrBINARY;
140020 n++;
140021 }
140022 }
140023 if( pSrc->colUsed & MASKBIT(BMS-1) ){
140024 for(i=BMS-1; i<pTable->nCol; i++){
140025 pIdx->aiColumn[n] = i;
140026 pIdx->azColl[n] = sqlite3StrBINARY;
140027 n++;
140028 }
140029 }
140030 assert( n==nKeyCol );
140031 pIdx->aiColumn[n] = XN_ROWID;
140032 pIdx->azColl[n] = sqlite3StrBINARY;
140033
140034 /* Create the automatic index */
140035 assert( pLevel->iIdxCur>=0 );
140036 pLevel->iIdxCur = pParse->nTab++;
140037 sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
140038 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
140039 VdbeComment((v, "for %s", pTable->zName));
140040
140041 /* Fill the automatic index with content */
140042 pTabItem = &pWC->pWInfo->pTabList->a[pLevel->iFrom];
140043 if( pTabItem->fg.viaCoroutine ){
140044 int regYield = pTabItem->regReturn;
140045 addrCounter = sqlite3VdbeAddOp2(v, OP_Integer, 0, 0);
140046 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
140047 addrTop = sqlite3VdbeAddOp1(v, OP_Yield, regYield);
140048 VdbeCoverage(v);
140049 VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
140050 }else{
140051 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
140052 }
140053 if( pPartial ){
140054 iContinue = sqlite3VdbeMakeLabel(v);
140055 sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL);
140056 pLoop->wsFlags |= WHERE_PARTIALIDX;
140057 }
140058 regRecord = sqlite3GetTempReg(pParse);
140059 regBase = sqlite3GenerateIndexKey(
140060 pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0
140061 );
140062 sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
140063 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
140064 if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue);
140065 if( pTabItem->fg.viaCoroutine ){
140066 sqlite3VdbeChangeP2(v, addrCounter, regBase+n);
140067 testcase( pParse->db->mallocFailed );
140068 translateColumnToCopy(pParse, addrTop, pLevel->iTabCur,
140069 pTabItem->regResult, 1);
140070 sqlite3VdbeGoto(v, addrTop);
140071 }else{
140072 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
140073 }
140074 sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
140075 sqlite3VdbeJumpHere(v, addrTop);
140076 sqlite3ReleaseTempReg(pParse, regRecord);
140077
140078 /* Jump here when skipping the initialization */
140079 sqlite3VdbeJumpHere(v, addrInit);
140080
140081end_auto_index_create:
140082 sqlite3ExprDelete(pParse->db, pPartial);
140083}
140084#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
140085
140086#ifndef SQLITE_OMIT_VIRTUALTABLE
140087/*
140088** Allocate and populate an sqlite3_index_info structure. It is the
140089** responsibility of the caller to eventually release the structure
140090** by passing the pointer returned by this function to sqlite3_free().
140091*/
140092static sqlite3_index_info *allocateIndexInfo(
140093 Parse *pParse, /* The parsing context */
140094 WhereClause *pWC, /* The WHERE clause being analyzed */
140095 Bitmask mUnusable, /* Ignore terms with these prereqs */
140096 struct SrcList_item *pSrc, /* The FROM clause term that is the vtab */
140097 ExprList *pOrderBy, /* The ORDER BY clause */
140098 u16 *pmNoOmit /* Mask of terms not to omit */
140099){
140100 int i, j;
140101 int nTerm;
140102 struct sqlite3_index_constraint *pIdxCons;
140103 struct sqlite3_index_orderby *pIdxOrderBy;
140104 struct sqlite3_index_constraint_usage *pUsage;
140105 struct HiddenIndexInfo *pHidden;
140106 WhereTerm *pTerm;
140107 int nOrderBy;
140108 sqlite3_index_info *pIdxInfo;
140109 u16 mNoOmit = 0;
140110
140111 /* Count the number of possible WHERE clause constraints referring
140112 ** to this virtual table */
140113 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
140114 if( pTerm->leftCursor != pSrc->iCursor ) continue;
140115 if( pTerm->prereqRight & mUnusable ) continue;
140116 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
140117 testcase( pTerm->eOperator & WO_IN );
140118 testcase( pTerm->eOperator & WO_ISNULL );
140119 testcase( pTerm->eOperator & WO_IS );
140120 testcase( pTerm->eOperator & WO_ALL );
140121 if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
140122 if( pTerm->wtFlags & TERM_VNULL ) continue;
140123 assert( pTerm->u.leftColumn>=(-1) );
140124 nTerm++;
140125 }
140126
140127 /* If the ORDER BY clause contains only columns in the current
140128 ** virtual table then allocate space for the aOrderBy part of
140129 ** the sqlite3_index_info structure.
140130 */
140131 nOrderBy = 0;
140132 if( pOrderBy ){
140133 int n = pOrderBy->nExpr;
140134 for(i=0; i<n; i++){
140135 Expr *pExpr = pOrderBy->a[i].pExpr;
140136 if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
140137 }
140138 if( i==n){
140139 nOrderBy = n;
140140 }
140141 }
140142
140143 /* Allocate the sqlite3_index_info structure
140144 */
140145 pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
140146 + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
140147 + sizeof(*pIdxOrderBy)*nOrderBy + sizeof(*pHidden) );
140148 if( pIdxInfo==0 ){
140149 sqlite3ErrorMsg(pParse, "out of memory");
140150 return 0;
140151 }
140152
140153 /* Initialize the structure. The sqlite3_index_info structure contains
140154 ** many fields that are declared "const" to prevent xBestIndex from
140155 ** changing them. We have to do some funky casting in order to
140156 ** initialize those fields.
140157 */
140158 pHidden = (struct HiddenIndexInfo*)&pIdxInfo[1];
140159 pIdxCons = (struct sqlite3_index_constraint*)&pHidden[1];
140160 pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
140161 pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
140162 *(int*)&pIdxInfo->nConstraint = nTerm;
140163 *(int*)&pIdxInfo->nOrderBy = nOrderBy;
140164 *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons;
140165 *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
140166 *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
140167 pUsage;
140168
140169 pHidden->pWC = pWC;
140170 pHidden->pParse = pParse;
140171 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
140172 u16 op;
140173 if( pTerm->leftCursor != pSrc->iCursor ) continue;
140174 if( pTerm->prereqRight & mUnusable ) continue;
140175 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
140176 testcase( pTerm->eOperator & WO_IN );
140177 testcase( pTerm->eOperator & WO_IS );
140178 testcase( pTerm->eOperator & WO_ISNULL );
140179 testcase( pTerm->eOperator & WO_ALL );
140180 if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
140181 if( pTerm->wtFlags & TERM_VNULL ) continue;
140182 if( (pSrc->fg.jointype & JT_LEFT)!=0
140183 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
140184 && (pTerm->eOperator & (WO_IS|WO_ISNULL))
140185 ){
140186 /* An "IS" term in the WHERE clause where the virtual table is the rhs
140187 ** of a LEFT JOIN. Do not pass this term to the virtual table
140188 ** implementation, as this can lead to incorrect results from SQL such
140189 ** as:
140190 **
140191 ** "LEFT JOIN vtab WHERE vtab.col IS NULL" */
140192 testcase( pTerm->eOperator & WO_ISNULL );
140193 testcase( pTerm->eOperator & WO_IS );
140194 continue;
140195 }
140196 assert( pTerm->u.leftColumn>=(-1) );
140197 pIdxCons[j].iColumn = pTerm->u.leftColumn;
140198 pIdxCons[j].iTermOffset = i;
140199 op = pTerm->eOperator & WO_ALL;
140200 if( op==WO_IN ) op = WO_EQ;
140201 if( op==WO_AUX ){
140202 pIdxCons[j].op = pTerm->eMatchOp;
140203 }else if( op & (WO_ISNULL|WO_IS) ){
140204 if( op==WO_ISNULL ){
140205 pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_ISNULL;
140206 }else{
140207 pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_IS;
140208 }
140209 }else{
140210 pIdxCons[j].op = (u8)op;
140211 /* The direct assignment in the previous line is possible only because
140212 ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The
140213 ** following asserts verify this fact. */
140214 assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
140215 assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
140216 assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
140217 assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
140218 assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
140219 assert( pTerm->eOperator&(WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_AUX) );
140220
140221 if( op & (WO_LT|WO_LE|WO_GT|WO_GE)
140222 && sqlite3ExprIsVector(pTerm->pExpr->pRight)
140223 ){
140224 if( i<16 ) mNoOmit |= (1 << i);
140225 if( op==WO_LT ) pIdxCons[j].op = WO_LE;
140226 if( op==WO_GT ) pIdxCons[j].op = WO_GE;
140227 }
140228 }
140229
140230 j++;
140231 }
140232 for(i=0; i<nOrderBy; i++){
140233 Expr *pExpr = pOrderBy->a[i].pExpr;
140234 pIdxOrderBy[i].iColumn = pExpr->iColumn;
140235 pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder;
140236 }
140237
140238 *pmNoOmit = mNoOmit;
140239 return pIdxInfo;
140240}
140241
140242/*
140243** The table object reference passed as the second argument to this function
140244** must represent a virtual table. This function invokes the xBestIndex()
140245** method of the virtual table with the sqlite3_index_info object that
140246** comes in as the 3rd argument to this function.
140247**
140248** If an error occurs, pParse is populated with an error message and an
140249** appropriate error code is returned. A return of SQLITE_CONSTRAINT from
140250** xBestIndex is not considered an error. SQLITE_CONSTRAINT indicates that
140251** the current configuration of "unusable" flags in sqlite3_index_info can
140252** not result in a valid plan.
140253**
140254** Whether or not an error is returned, it is the responsibility of the
140255** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
140256** that this is required.
140257*/
140258static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
140259 sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
140260 int rc;
140261
140262 TRACE_IDX_INPUTS(p);
140263 rc = pVtab->pModule->xBestIndex(pVtab, p);
140264 TRACE_IDX_OUTPUTS(p);
140265
140266 if( rc!=SQLITE_OK && rc!=SQLITE_CONSTRAINT ){
140267 if( rc==SQLITE_NOMEM ){
140268 sqlite3OomFault(pParse->db);
140269 }else if( !pVtab->zErrMsg ){
140270 sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
140271 }else{
140272 sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
140273 }
140274 }
140275 sqlite3_free(pVtab->zErrMsg);
140276 pVtab->zErrMsg = 0;
140277 return rc;
140278}
140279#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
140280
140281#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
140282/*
140283** Estimate the location of a particular key among all keys in an
140284** index. Store the results in aStat as follows:
140285**
140286** aStat[0] Est. number of rows less than pRec
140287** aStat[1] Est. number of rows equal to pRec
140288**
140289** Return the index of the sample that is the smallest sample that
140290** is greater than or equal to pRec. Note that this index is not an index
140291** into the aSample[] array - it is an index into a virtual set of samples
140292** based on the contents of aSample[] and the number of fields in record
140293** pRec.
140294*/
140295static int whereKeyStats(
140296 Parse *pParse, /* Database connection */
140297 Index *pIdx, /* Index to consider domain of */
140298 UnpackedRecord *pRec, /* Vector of values to consider */
140299 int roundUp, /* Round up if true. Round down if false */
140300 tRowcnt *aStat /* OUT: stats written here */
140301){
140302 IndexSample *aSample = pIdx->aSample;
140303 int iCol; /* Index of required stats in anEq[] etc. */
140304 int i; /* Index of first sample >= pRec */
140305 int iSample; /* Smallest sample larger than or equal to pRec */
140306 int iMin = 0; /* Smallest sample not yet tested */
140307 int iTest; /* Next sample to test */
140308 int res; /* Result of comparison operation */
140309 int nField; /* Number of fields in pRec */
140310 tRowcnt iLower = 0; /* anLt[] + anEq[] of largest sample pRec is > */
140311
140312#ifndef SQLITE_DEBUG
140313 UNUSED_PARAMETER( pParse );
140314#endif
140315 assert( pRec!=0 );
140316 assert( pIdx->nSample>0 );
140317 assert( pRec->nField>0 && pRec->nField<=pIdx->nSampleCol );
140318
140319 /* Do a binary search to find the first sample greater than or equal
140320 ** to pRec. If pRec contains a single field, the set of samples to search
140321 ** is simply the aSample[] array. If the samples in aSample[] contain more
140322 ** than one fields, all fields following the first are ignored.
140323 **
140324 ** If pRec contains N fields, where N is more than one, then as well as the
140325 ** samples in aSample[] (truncated to N fields), the search also has to
140326 ** consider prefixes of those samples. For example, if the set of samples
140327 ** in aSample is:
140328 **
140329 ** aSample[0] = (a, 5)
140330 ** aSample[1] = (a, 10)
140331 ** aSample[2] = (b, 5)
140332 ** aSample[3] = (c, 100)
140333 ** aSample[4] = (c, 105)
140334 **
140335 ** Then the search space should ideally be the samples above and the
140336 ** unique prefixes [a], [b] and [c]. But since that is hard to organize,
140337 ** the code actually searches this set:
140338 **
140339 ** 0: (a)
140340 ** 1: (a, 5)
140341 ** 2: (a, 10)
140342 ** 3: (a, 10)
140343 ** 4: (b)
140344 ** 5: (b, 5)
140345 ** 6: (c)
140346 ** 7: (c, 100)
140347 ** 8: (c, 105)
140348 ** 9: (c, 105)
140349 **
140350 ** For each sample in the aSample[] array, N samples are present in the
140351 ** effective sample array. In the above, samples 0 and 1 are based on
140352 ** sample aSample[0]. Samples 2 and 3 on aSample[1] etc.
140353 **
140354 ** Often, sample i of each block of N effective samples has (i+1) fields.
140355 ** Except, each sample may be extended to ensure that it is greater than or
140356 ** equal to the previous sample in the array. For example, in the above,
140357 ** sample 2 is the first sample of a block of N samples, so at first it
140358 ** appears that it should be 1 field in size. However, that would make it
140359 ** smaller than sample 1, so the binary search would not work. As a result,
140360 ** it is extended to two fields. The duplicates that this creates do not
140361 ** cause any problems.
140362 */
140363 nField = pRec->nField;
140364 iCol = 0;
140365 iSample = pIdx->nSample * nField;
140366 do{
140367 int iSamp; /* Index in aSample[] of test sample */
140368 int n; /* Number of fields in test sample */
140369
140370 iTest = (iMin+iSample)/2;
140371 iSamp = iTest / nField;
140372 if( iSamp>0 ){
140373 /* The proposed effective sample is a prefix of sample aSample[iSamp].
140374 ** Specifically, the shortest prefix of at least (1 + iTest%nField)
140375 ** fields that is greater than the previous effective sample. */
140376 for(n=(iTest % nField) + 1; n<nField; n++){
140377 if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break;
140378 }
140379 }else{
140380 n = iTest + 1;
140381 }
140382
140383 pRec->nField = n;
140384 res = sqlite3VdbeRecordCompare(aSample[iSamp].n, aSample[iSamp].p, pRec);
140385 if( res<0 ){
140386 iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1];
140387 iMin = iTest+1;
140388 }else if( res==0 && n<nField ){
140389 iLower = aSample[iSamp].anLt[n-1];
140390 iMin = iTest+1;
140391 res = -1;
140392 }else{
140393 iSample = iTest;
140394 iCol = n-1;
140395 }
140396 }while( res && iMin<iSample );
140397 i = iSample / nField;
140398
140399#ifdef SQLITE_DEBUG
140400 /* The following assert statements check that the binary search code
140401 ** above found the right answer. This block serves no purpose other
140402 ** than to invoke the asserts. */
140403 if( pParse->db->mallocFailed==0 ){
140404 if( res==0 ){
140405 /* If (res==0) is true, then pRec must be equal to sample i. */
140406 assert( i<pIdx->nSample );
140407 assert( iCol==nField-1 );
140408 pRec->nField = nField;
140409 assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)
140410 || pParse->db->mallocFailed
140411 );
140412 }else{
140413 /* Unless i==pIdx->nSample, indicating that pRec is larger than
140414 ** all samples in the aSample[] array, pRec must be smaller than the
140415 ** (iCol+1) field prefix of sample i. */
140416 assert( i<=pIdx->nSample && i>=0 );
140417 pRec->nField = iCol+1;
140418 assert( i==pIdx->nSample
140419 || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0
140420 || pParse->db->mallocFailed );
140421
140422 /* if i==0 and iCol==0, then record pRec is smaller than all samples
140423 ** in the aSample[] array. Otherwise, if (iCol>0) then pRec must
140424 ** be greater than or equal to the (iCol) field prefix of sample i.
140425 ** If (i>0), then pRec must also be greater than sample (i-1). */
140426 if( iCol>0 ){
140427 pRec->nField = iCol;
140428 assert( sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)<=0
140429 || pParse->db->mallocFailed );
140430 }
140431 if( i>0 ){
140432 pRec->nField = nField;
140433 assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
140434 || pParse->db->mallocFailed );
140435 }
140436 }
140437 }
140438#endif /* ifdef SQLITE_DEBUG */
140439
140440 if( res==0 ){
140441 /* Record pRec is equal to sample i */
140442 assert( iCol==nField-1 );
140443 aStat[0] = aSample[i].anLt[iCol];
140444 aStat[1] = aSample[i].anEq[iCol];
140445 }else{
140446 /* At this point, the (iCol+1) field prefix of aSample[i] is the first
140447 ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
140448 ** is larger than all samples in the array. */
140449 tRowcnt iUpper, iGap;
140450 if( i>=pIdx->nSample ){
140451 iUpper = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
140452 }else{
140453 iUpper = aSample[i].anLt[iCol];
140454 }
140455
140456 if( iLower>=iUpper ){
140457 iGap = 0;
140458 }else{
140459 iGap = iUpper - iLower;
140460 }
140461 if( roundUp ){
140462 iGap = (iGap*2)/3;
140463 }else{
140464 iGap = iGap/3;
140465 }
140466 aStat[0] = iLower + iGap;
140467 aStat[1] = pIdx->aAvgEq[nField-1];
140468 }
140469
140470 /* Restore the pRec->nField value before returning. */
140471 pRec->nField = nField;
140472 return i;
140473}
140474#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
140475
140476/*
140477** If it is not NULL, pTerm is a term that provides an upper or lower
140478** bound on a range scan. Without considering pTerm, it is estimated
140479** that the scan will visit nNew rows. This function returns the number
140480** estimated to be visited after taking pTerm into account.
140481**
140482** If the user explicitly specified a likelihood() value for this term,
140483** then the return value is the likelihood multiplied by the number of
140484** input rows. Otherwise, this function assumes that an "IS NOT NULL" term
140485** has a likelihood of 0.50, and any other term a likelihood of 0.25.
140486*/
140487static LogEst whereRangeAdjust(WhereTerm *pTerm, LogEst nNew){
140488 LogEst nRet = nNew;
140489 if( pTerm ){
140490 if( pTerm->truthProb<=0 ){
140491 nRet += pTerm->truthProb;
140492 }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){
140493 nRet -= 20; assert( 20==sqlite3LogEst(4) );
140494 }
140495 }
140496 return nRet;
140497}
140498
140499
140500#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
140501/*
140502** Return the affinity for a single column of an index.
140503*/
140504SQLITE_PRIVATE char sqlite3IndexColumnAffinity(sqlite3 *db, Index *pIdx, int iCol){
140505 assert( iCol>=0 && iCol<pIdx->nColumn );
140506 if( !pIdx->zColAff ){
140507 if( sqlite3IndexAffinityStr(db, pIdx)==0 ) return SQLITE_AFF_BLOB;
140508 }
140509 return pIdx->zColAff[iCol];
140510}
140511#endif
140512
140513
140514#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
140515/*
140516** This function is called to estimate the number of rows visited by a
140517** range-scan on a skip-scan index. For example:
140518**
140519** CREATE INDEX i1 ON t1(a, b, c);
140520** SELECT * FROM t1 WHERE a=? AND c BETWEEN ? AND ?;
140521**
140522** Value pLoop->nOut is currently set to the estimated number of rows
140523** visited for scanning (a=? AND b=?). This function reduces that estimate
140524** by some factor to account for the (c BETWEEN ? AND ?) expression based
140525** on the stat4 data for the index. this scan will be peformed multiple
140526** times (once for each (a,b) combination that matches a=?) is dealt with
140527** by the caller.
140528**
140529** It does this by scanning through all stat4 samples, comparing values
140530** extracted from pLower and pUpper with the corresponding column in each
140531** sample. If L and U are the number of samples found to be less than or
140532** equal to the values extracted from pLower and pUpper respectively, and
140533** N is the total number of samples, the pLoop->nOut value is adjusted
140534** as follows:
140535**
140536** nOut = nOut * ( min(U - L, 1) / N )
140537**
140538** If pLower is NULL, or a value cannot be extracted from the term, L is
140539** set to zero. If pUpper is NULL, or a value cannot be extracted from it,
140540** U is set to N.
140541**
140542** Normally, this function sets *pbDone to 1 before returning. However,
140543** if no value can be extracted from either pLower or pUpper (and so the
140544** estimate of the number of rows delivered remains unchanged), *pbDone
140545** is left as is.
140546**
140547** If an error occurs, an SQLite error code is returned. Otherwise,
140548** SQLITE_OK.
140549*/
140550static int whereRangeSkipScanEst(
140551 Parse *pParse, /* Parsing & code generating context */
140552 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
140553 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
140554 WhereLoop *pLoop, /* Update the .nOut value of this loop */
140555 int *pbDone /* Set to true if at least one expr. value extracted */
140556){
140557 Index *p = pLoop->u.btree.pIndex;
140558 int nEq = pLoop->u.btree.nEq;
140559 sqlite3 *db = pParse->db;
140560 int nLower = -1;
140561 int nUpper = p->nSample+1;
140562 int rc = SQLITE_OK;
140563 u8 aff = sqlite3IndexColumnAffinity(db, p, nEq);
140564 CollSeq *pColl;
140565
140566 sqlite3_value *p1 = 0; /* Value extracted from pLower */
140567 sqlite3_value *p2 = 0; /* Value extracted from pUpper */
140568 sqlite3_value *pVal = 0; /* Value extracted from record */
140569
140570 pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
140571 if( pLower ){
140572 rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
140573 nLower = 0;
140574 }
140575 if( pUpper && rc==SQLITE_OK ){
140576 rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
140577 nUpper = p2 ? 0 : p->nSample;
140578 }
140579
140580 if( p1 || p2 ){
140581 int i;
140582 int nDiff;
140583 for(i=0; rc==SQLITE_OK && i<p->nSample; i++){
140584 rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
140585 if( rc==SQLITE_OK && p1 ){
140586 int res = sqlite3MemCompare(p1, pVal, pColl);
140587 if( res>=0 ) nLower++;
140588 }
140589 if( rc==SQLITE_OK && p2 ){
140590 int res = sqlite3MemCompare(p2, pVal, pColl);
140591 if( res>=0 ) nUpper++;
140592 }
140593 }
140594 nDiff = (nUpper - nLower);
140595 if( nDiff<=0 ) nDiff = 1;
140596
140597 /* If there is both an upper and lower bound specified, and the
140598 ** comparisons indicate that they are close together, use the fallback
140599 ** method (assume that the scan visits 1/64 of the rows) for estimating
140600 ** the number of rows visited. Otherwise, estimate the number of rows
140601 ** using the method described in the header comment for this function. */
140602 if( nDiff!=1 || pUpper==0 || pLower==0 ){
140603 int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
140604 pLoop->nOut -= nAdjust;
140605 *pbDone = 1;
140606 WHERETRACE(0x10, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
140607 nLower, nUpper, nAdjust*-1, pLoop->nOut));
140608 }
140609
140610 }else{
140611 assert( *pbDone==0 );
140612 }
140613
140614 sqlite3ValueFree(p1);
140615 sqlite3ValueFree(p2);
140616 sqlite3ValueFree(pVal);
140617
140618 return rc;
140619}
140620#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
140621
140622/*
140623** This function is used to estimate the number of rows that will be visited
140624** by scanning an index for a range of values. The range may have an upper
140625** bound, a lower bound, or both. The WHERE clause terms that set the upper
140626** and lower bounds are represented by pLower and pUpper respectively. For
140627** example, assuming that index p is on t1(a):
140628**
140629** ... FROM t1 WHERE a > ? AND a < ? ...
140630** |_____| |_____|
140631** | |
140632** pLower pUpper
140633**
140634** If either of the upper or lower bound is not present, then NULL is passed in
140635** place of the corresponding WhereTerm.
140636**
140637** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index
140638** column subject to the range constraint. Or, equivalently, the number of
140639** equality constraints optimized by the proposed index scan. For example,
140640** assuming index p is on t1(a, b), and the SQL query is:
140641**
140642** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
140643**
140644** then nEq is set to 1 (as the range restricted column, b, is the second
140645** left-most column of the index). Or, if the query is:
140646**
140647** ... FROM t1 WHERE a > ? AND a < ? ...
140648**
140649** then nEq is set to 0.
140650**
140651** When this function is called, *pnOut is set to the sqlite3LogEst() of the
140652** number of rows that the index scan is expected to visit without
140653** considering the range constraints. If nEq is 0, then *pnOut is the number of
140654** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
140655** to account for the range constraints pLower and pUpper.
140656**
140657** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
140658** used, a single range inequality reduces the search space by a factor of 4.
140659** and a pair of constraints (x>? AND x<?) reduces the expected number of
140660** rows visited by a factor of 64.
140661*/
140662static int whereRangeScanEst(
140663 Parse *pParse, /* Parsing & code generating context */
140664 WhereLoopBuilder *pBuilder,
140665 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
140666 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
140667 WhereLoop *pLoop /* Modify the .nOut and maybe .rRun fields */
140668){
140669 int rc = SQLITE_OK;
140670 int nOut = pLoop->nOut;
140671 LogEst nNew;
140672
140673#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
140674 Index *p = pLoop->u.btree.pIndex;
140675 int nEq = pLoop->u.btree.nEq;
140676
140677 if( p->nSample>0 && nEq<p->nSampleCol
140678 && OptimizationEnabled(pParse->db, SQLITE_Stat34)
140679 ){
140680 if( nEq==pBuilder->nRecValid ){
140681 UnpackedRecord *pRec = pBuilder->pRec;
140682 tRowcnt a[2];
140683 int nBtm = pLoop->u.btree.nBtm;
140684 int nTop = pLoop->u.btree.nTop;
140685
140686 /* Variable iLower will be set to the estimate of the number of rows in
140687 ** the index that are less than the lower bound of the range query. The
140688 ** lower bound being the concatenation of $P and $L, where $P is the
140689 ** key-prefix formed by the nEq values matched against the nEq left-most
140690 ** columns of the index, and $L is the value in pLower.
140691 **
140692 ** Or, if pLower is NULL or $L cannot be extracted from it (because it
140693 ** is not a simple variable or literal value), the lower bound of the
140694 ** range is $P. Due to a quirk in the way whereKeyStats() works, even
140695 ** if $L is available, whereKeyStats() is called for both ($P) and
140696 ** ($P:$L) and the larger of the two returned values is used.
140697 **
140698 ** Similarly, iUpper is to be set to the estimate of the number of rows
140699 ** less than the upper bound of the range query. Where the upper bound
140700 ** is either ($P) or ($P:$U). Again, even if $U is available, both values
140701 ** of iUpper are requested of whereKeyStats() and the smaller used.
140702 **
140703 ** The number of rows between the two bounds is then just iUpper-iLower.
140704 */
140705 tRowcnt iLower; /* Rows less than the lower bound */
140706 tRowcnt iUpper; /* Rows less than the upper bound */
140707 int iLwrIdx = -2; /* aSample[] for the lower bound */
140708 int iUprIdx = -1; /* aSample[] for the upper bound */
140709
140710 if( pRec ){
140711 testcase( pRec->nField!=pBuilder->nRecValid );
140712 pRec->nField = pBuilder->nRecValid;
140713 }
140714 /* Determine iLower and iUpper using ($P) only. */
140715 if( nEq==0 ){
140716 iLower = 0;
140717 iUpper = p->nRowEst0;
140718 }else{
140719 /* Note: this call could be optimized away - since the same values must
140720 ** have been requested when testing key $P in whereEqualScanEst(). */
140721 whereKeyStats(pParse, p, pRec, 0, a);
140722 iLower = a[0];
140723 iUpper = a[0] + a[1];
140724 }
140725
140726 assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 );
140727 assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
140728 assert( p->aSortOrder!=0 );
140729 if( p->aSortOrder[nEq] ){
140730 /* The roles of pLower and pUpper are swapped for a DESC index */
140731 SWAP(WhereTerm*, pLower, pUpper);
140732 SWAP(int, nBtm, nTop);
140733 }
140734
140735 /* If possible, improve on the iLower estimate using ($P:$L). */
140736 if( pLower ){
140737 int n; /* Values extracted from pExpr */
140738 Expr *pExpr = pLower->pExpr->pRight;
140739 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nBtm, nEq, &n);
140740 if( rc==SQLITE_OK && n ){
140741 tRowcnt iNew;
140742 u16 mask = WO_GT|WO_LE;
140743 if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE|WO_LT);
140744 iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a);
140745 iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0);
140746 if( iNew>iLower ) iLower = iNew;
140747 nOut--;
140748 pLower = 0;
140749 }
140750 }
140751
140752 /* If possible, improve on the iUpper estimate using ($P:$U). */
140753 if( pUpper ){
140754 int n; /* Values extracted from pExpr */
140755 Expr *pExpr = pUpper->pExpr->pRight;
140756 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nTop, nEq, &n);
140757 if( rc==SQLITE_OK && n ){
140758 tRowcnt iNew;
140759 u16 mask = WO_GT|WO_LE;
140760 if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE|WO_LT);
140761 iUprIdx = whereKeyStats(pParse, p, pRec, 1, a);
140762 iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0);
140763 if( iNew<iUpper ) iUpper = iNew;
140764 nOut--;
140765 pUpper = 0;
140766 }
140767 }
140768
140769 pBuilder->pRec = pRec;
140770 if( rc==SQLITE_OK ){
140771 if( iUpper>iLower ){
140772 nNew = sqlite3LogEst(iUpper - iLower);
140773 /* TUNING: If both iUpper and iLower are derived from the same
140774 ** sample, then assume they are 4x more selective. This brings
140775 ** the estimated selectivity more in line with what it would be
140776 ** if estimated without the use of STAT3/4 tables. */
140777 if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) );
140778 }else{
140779 nNew = 10; assert( 10==sqlite3LogEst(2) );
140780 }
140781 if( nNew<nOut ){
140782 nOut = nNew;
140783 }
140784 WHERETRACE(0x10, ("STAT4 range scan: %u..%u est=%d\n",
140785 (u32)iLower, (u32)iUpper, nOut));
140786 }
140787 }else{
140788 int bDone = 0;
140789 rc = whereRangeSkipScanEst(pParse, pLower, pUpper, pLoop, &bDone);
140790 if( bDone ) return rc;
140791 }
140792 }
140793#else
140794 UNUSED_PARAMETER(pParse);
140795 UNUSED_PARAMETER(pBuilder);
140796 assert( pLower || pUpper );
140797#endif
140798 assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 );
140799 nNew = whereRangeAdjust(pLower, nOut);
140800 nNew = whereRangeAdjust(pUpper, nNew);
140801
140802 /* TUNING: If there is both an upper and lower limit and neither limit
140803 ** has an application-defined likelihood(), assume the range is
140804 ** reduced by an additional 75%. This means that, by default, an open-ended
140805 ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the
140806 ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to
140807 ** match 1/64 of the index. */
140808 if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){
140809 nNew -= 20;
140810 }
140811
140812 nOut -= (pLower!=0) + (pUpper!=0);
140813 if( nNew<10 ) nNew = 10;
140814 if( nNew<nOut ) nOut = nNew;
140815#if defined(WHERETRACE_ENABLED)
140816 if( pLoop->nOut>nOut ){
140817 WHERETRACE(0x10,("Range scan lowers nOut from %d to %d\n",
140818 pLoop->nOut, nOut));
140819 }
140820#endif
140821 pLoop->nOut = (LogEst)nOut;
140822 return rc;
140823}
140824
140825#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
140826/*
140827** Estimate the number of rows that will be returned based on
140828** an equality constraint x=VALUE and where that VALUE occurs in
140829** the histogram data. This only works when x is the left-most
140830** column of an index and sqlite_stat3 histogram data is available
140831** for that index. When pExpr==NULL that means the constraint is
140832** "x IS NULL" instead of "x=VALUE".
140833**
140834** Write the estimated row count into *pnRow and return SQLITE_OK.
140835** If unable to make an estimate, leave *pnRow unchanged and return
140836** non-zero.
140837**
140838** This routine can fail if it is unable to load a collating sequence
140839** required for string comparison, or if unable to allocate memory
140840** for a UTF conversion required for comparison. The error is stored
140841** in the pParse structure.
140842*/
140843static int whereEqualScanEst(
140844 Parse *pParse, /* Parsing & code generating context */
140845 WhereLoopBuilder *pBuilder,
140846 Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
140847 tRowcnt *pnRow /* Write the revised row estimate here */
140848){
140849 Index *p = pBuilder->pNew->u.btree.pIndex;
140850 int nEq = pBuilder->pNew->u.btree.nEq;
140851 UnpackedRecord *pRec = pBuilder->pRec;
140852 int rc; /* Subfunction return code */
140853 tRowcnt a[2]; /* Statistics */
140854 int bOk;
140855
140856 assert( nEq>=1 );
140857 assert( nEq<=p->nColumn );
140858 assert( p->aSample!=0 );
140859 assert( p->nSample>0 );
140860 assert( pBuilder->nRecValid<nEq );
140861
140862 /* If values are not available for all fields of the index to the left
140863 ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */
140864 if( pBuilder->nRecValid<(nEq-1) ){
140865 return SQLITE_NOTFOUND;
140866 }
140867
140868 /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue()
140869 ** below would return the same value. */
140870 if( nEq>=p->nColumn ){
140871 *pnRow = 1;
140872 return SQLITE_OK;
140873 }
140874
140875 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, 1, nEq-1, &bOk);
140876 pBuilder->pRec = pRec;
140877 if( rc!=SQLITE_OK ) return rc;
140878 if( bOk==0 ) return SQLITE_NOTFOUND;
140879 pBuilder->nRecValid = nEq;
140880
140881 whereKeyStats(pParse, p, pRec, 0, a);
140882 WHERETRACE(0x10,("equality scan regions %s(%d): %d\n",
140883 p->zName, nEq-1, (int)a[1]));
140884 *pnRow = a[1];
140885
140886 return rc;
140887}
140888#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
140889
140890#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
140891/*
140892** Estimate the number of rows that will be returned based on
140893** an IN constraint where the right-hand side of the IN operator
140894** is a list of values. Example:
140895**
140896** WHERE x IN (1,2,3,4)
140897**
140898** Write the estimated row count into *pnRow and return SQLITE_OK.
140899** If unable to make an estimate, leave *pnRow unchanged and return
140900** non-zero.
140901**
140902** This routine can fail if it is unable to load a collating sequence
140903** required for string comparison, or if unable to allocate memory
140904** for a UTF conversion required for comparison. The error is stored
140905** in the pParse structure.
140906*/
140907static int whereInScanEst(
140908 Parse *pParse, /* Parsing & code generating context */
140909 WhereLoopBuilder *pBuilder,
140910 ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
140911 tRowcnt *pnRow /* Write the revised row estimate here */
140912){
140913 Index *p = pBuilder->pNew->u.btree.pIndex;
140914 i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
140915 int nRecValid = pBuilder->nRecValid;
140916 int rc = SQLITE_OK; /* Subfunction return code */
140917 tRowcnt nEst; /* Number of rows for a single term */
140918 tRowcnt nRowEst = 0; /* New estimate of the number of rows */
140919 int i; /* Loop counter */
140920
140921 assert( p->aSample!=0 );
140922 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
140923 nEst = nRow0;
140924 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
140925 nRowEst += nEst;
140926 pBuilder->nRecValid = nRecValid;
140927 }
140928
140929 if( rc==SQLITE_OK ){
140930 if( nRowEst > nRow0 ) nRowEst = nRow0;
140931 *pnRow = nRowEst;
140932 WHERETRACE(0x10,("IN row estimate: est=%d\n", nRowEst));
140933 }
140934 assert( pBuilder->nRecValid==nRecValid );
140935 return rc;
140936}
140937#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
140938
140939
140940#ifdef WHERETRACE_ENABLED
140941/*
140942** Print the content of a WhereTerm object
140943*/
140944static void whereTermPrint(WhereTerm *pTerm, int iTerm){
140945 if( pTerm==0 ){
140946 sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm);
140947 }else{
140948 char zType[4];
140949 char zLeft[50];
140950 memcpy(zType, "...", 4);
140951 if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
140952 if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E';
140953 if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L';
140954 if( pTerm->eOperator & WO_SINGLE ){
140955 sqlite3_snprintf(sizeof(zLeft),zLeft,"left={%d:%d}",
140956 pTerm->leftCursor, pTerm->u.leftColumn);
140957 }else if( (pTerm->eOperator & WO_OR)!=0 && pTerm->u.pOrInfo!=0 ){
140958 sqlite3_snprintf(sizeof(zLeft),zLeft,"indexable=0x%lld",
140959 pTerm->u.pOrInfo->indexable);
140960 }else{
140961 sqlite3_snprintf(sizeof(zLeft),zLeft,"left=%d", pTerm->leftCursor);
140962 }
140963 sqlite3DebugPrintf(
140964 "TERM-%-3d %p %s %-12s prob=%-3d op=0x%03x wtFlags=0x%04x",
140965 iTerm, pTerm, zType, zLeft, pTerm->truthProb,
140966 pTerm->eOperator, pTerm->wtFlags);
140967 if( pTerm->iField ){
140968 sqlite3DebugPrintf(" iField=%d\n", pTerm->iField);
140969 }else{
140970 sqlite3DebugPrintf("\n");
140971 }
140972 sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
140973 }
140974}
140975#endif
140976
140977#ifdef WHERETRACE_ENABLED
140978/*
140979** Show the complete content of a WhereClause
140980*/
140981SQLITE_PRIVATE void sqlite3WhereClausePrint(WhereClause *pWC){
140982 int i;
140983 for(i=0; i<pWC->nTerm; i++){
140984 whereTermPrint(&pWC->a[i], i);
140985 }
140986}
140987#endif
140988
140989#ifdef WHERETRACE_ENABLED
140990/*
140991** Print a WhereLoop object for debugging purposes
140992*/
140993static void whereLoopPrint(WhereLoop *p, WhereClause *pWC){
140994 WhereInfo *pWInfo = pWC->pWInfo;
140995 int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
140996 struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab;
140997 Table *pTab = pItem->pTab;
140998 Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
140999 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
141000 p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
141001 sqlite3DebugPrintf(" %12s",
141002 pItem->zAlias ? pItem->zAlias : pTab->zName);
141003 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
141004 const char *zName;
141005 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
141006 if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){
141007 int i = sqlite3Strlen30(zName) - 1;
141008 while( zName[i]!='_' ) i--;
141009 zName += i;
141010 }
141011 sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
141012 }else{
141013 sqlite3DebugPrintf("%20s","");
141014 }
141015 }else{
141016 char *z;
141017 if( p->u.vtab.idxStr ){
141018 z = sqlite3_mprintf("(%d,\"%s\",%x)",
141019 p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
141020 }else{
141021 z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
141022 }
141023 sqlite3DebugPrintf(" %-19s", z);
141024 sqlite3_free(z);
141025 }
141026 if( p->wsFlags & WHERE_SKIPSCAN ){
141027 sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
141028 }else{
141029 sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm);
141030 }
141031 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
141032 if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){
141033 int i;
141034 for(i=0; i<p->nLTerm; i++){
141035 whereTermPrint(p->aLTerm[i], i);
141036 }
141037 }
141038}
141039#endif
141040
141041/*
141042** Convert bulk memory into a valid WhereLoop that can be passed
141043** to whereLoopClear harmlessly.
141044*/
141045static void whereLoopInit(WhereLoop *p){
141046 p->aLTerm = p->aLTermSpace;
141047 p->nLTerm = 0;
141048 p->nLSlot = ArraySize(p->aLTermSpace);
141049 p->wsFlags = 0;
141050}
141051
141052/*
141053** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact.
141054*/
141055static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){
141056 if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){
141057 if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
141058 sqlite3_free(p->u.vtab.idxStr);
141059 p->u.vtab.needFree = 0;
141060 p->u.vtab.idxStr = 0;
141061 }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){
141062 sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
141063 sqlite3DbFreeNN(db, p->u.btree.pIndex);
141064 p->u.btree.pIndex = 0;
141065 }
141066 }
141067}
141068
141069/*
141070** Deallocate internal memory used by a WhereLoop object
141071*/
141072static void whereLoopClear(sqlite3 *db, WhereLoop *p){
141073 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
141074 whereLoopClearUnion(db, p);
141075 whereLoopInit(p);
141076}
141077
141078/*
141079** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
141080*/
141081static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){
141082 WhereTerm **paNew;
141083 if( p->nLSlot>=n ) return SQLITE_OK;
141084 n = (n+7)&~7;
141085 paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n);
141086 if( paNew==0 ) return SQLITE_NOMEM_BKPT;
141087 memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
141088 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
141089 p->aLTerm = paNew;
141090 p->nLSlot = n;
141091 return SQLITE_OK;
141092}
141093
141094/*
141095** Transfer content from the second pLoop into the first.
141096*/
141097static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){
141098 whereLoopClearUnion(db, pTo);
141099 if( whereLoopResize(db, pTo, pFrom->nLTerm) ){
141100 memset(&pTo->u, 0, sizeof(pTo->u));
141101 return SQLITE_NOMEM_BKPT;
141102 }
141103 memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ);
141104 memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
141105 if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){
141106 pFrom->u.vtab.needFree = 0;
141107 }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){
141108 pFrom->u.btree.pIndex = 0;
141109 }
141110 return SQLITE_OK;
141111}
141112
141113/*
141114** Delete a WhereLoop object
141115*/
141116static void whereLoopDelete(sqlite3 *db, WhereLoop *p){
141117 whereLoopClear(db, p);
141118 sqlite3DbFreeNN(db, p);
141119}
141120
141121/*
141122** Free a WhereInfo structure
141123*/
141124static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
141125 int i;
141126 assert( pWInfo!=0 );
141127 for(i=0; i<pWInfo->nLevel; i++){
141128 WhereLevel *pLevel = &pWInfo->a[i];
141129 if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){
141130 sqlite3DbFree(db, pLevel->u.in.aInLoop);
141131 }
141132 }
141133 sqlite3WhereClauseClear(&pWInfo->sWC);
141134 while( pWInfo->pLoops ){
141135 WhereLoop *p = pWInfo->pLoops;
141136 pWInfo->pLoops = p->pNextLoop;
141137 whereLoopDelete(db, p);
141138 }
141139 sqlite3DbFreeNN(db, pWInfo);
141140}
141141
141142/*
141143** Return TRUE if all of the following are true:
141144**
141145** (1) X has the same or lower cost that Y
141146** (2) X uses fewer WHERE clause terms than Y
141147** (3) Every WHERE clause term used by X is also used by Y
141148** (4) X skips at least as many columns as Y
141149** (5) If X is a covering index, than Y is too
141150**
141151** Conditions (2) and (3) mean that X is a "proper subset" of Y.
141152** If X is a proper subset of Y then Y is a better choice and ought
141153** to have a lower cost. This routine returns TRUE when that cost
141154** relationship is inverted and needs to be adjusted. Constraint (4)
141155** was added because if X uses skip-scan less than Y it still might
141156** deserve a lower cost even if it is a proper subset of Y. Constraint (5)
141157** was added because a covering index probably deserves to have a lower cost
141158** than a non-covering index even if it is a proper subset.
141159*/
141160static int whereLoopCheaperProperSubset(
141161 const WhereLoop *pX, /* First WhereLoop to compare */
141162 const WhereLoop *pY /* Compare against this WhereLoop */
141163){
141164 int i, j;
141165 if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
141166 return 0; /* X is not a subset of Y */
141167 }
141168 if( pY->nSkip > pX->nSkip ) return 0;
141169 if( pX->rRun >= pY->rRun ){
141170 if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */
141171 if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */
141172 }
141173 for(i=pX->nLTerm-1; i>=0; i--){
141174 if( pX->aLTerm[i]==0 ) continue;
141175 for(j=pY->nLTerm-1; j>=0; j--){
141176 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
141177 }
141178 if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */
141179 }
141180 if( (pX->wsFlags&WHERE_IDX_ONLY)!=0
141181 && (pY->wsFlags&WHERE_IDX_ONLY)==0 ){
141182 return 0; /* Constraint (5) */
141183 }
141184 return 1; /* All conditions meet */
141185}
141186
141187/*
141188** Try to adjust the cost of WhereLoop pTemplate upwards or downwards so
141189** that:
141190**
141191** (1) pTemplate costs less than any other WhereLoops that are a proper
141192** subset of pTemplate
141193**
141194** (2) pTemplate costs more than any other WhereLoops for which pTemplate
141195** is a proper subset.
141196**
141197** To say "WhereLoop X is a proper subset of Y" means that X uses fewer
141198** WHERE clause terms than Y and that every WHERE clause term used by X is
141199** also used by Y.
141200*/
141201static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){
141202 if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
141203 for(; p; p=p->pNextLoop){
141204 if( p->iTab!=pTemplate->iTab ) continue;
141205 if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
141206 if( whereLoopCheaperProperSubset(p, pTemplate) ){
141207 /* Adjust pTemplate cost downward so that it is cheaper than its
141208 ** subset p. */
141209 WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
141210 pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut-1));
141211 pTemplate->rRun = p->rRun;
141212 pTemplate->nOut = p->nOut - 1;
141213 }else if( whereLoopCheaperProperSubset(pTemplate, p) ){
141214 /* Adjust pTemplate cost upward so that it is costlier than p since
141215 ** pTemplate is a proper subset of p */
141216 WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
141217 pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut+1));
141218 pTemplate->rRun = p->rRun;
141219 pTemplate->nOut = p->nOut + 1;
141220 }
141221 }
141222}
141223
141224/*
141225** Search the list of WhereLoops in *ppPrev looking for one that can be
141226** replaced by pTemplate.
141227**
141228** Return NULL if pTemplate does not belong on the WhereLoop list.
141229** In other words if pTemplate ought to be dropped from further consideration.
141230**
141231** If pX is a WhereLoop that pTemplate can replace, then return the
141232** link that points to pX.
141233**
141234** If pTemplate cannot replace any existing element of the list but needs
141235** to be added to the list as a new entry, then return a pointer to the
141236** tail of the list.
141237*/
141238static WhereLoop **whereLoopFindLesser(
141239 WhereLoop **ppPrev,
141240 const WhereLoop *pTemplate
141241){
141242 WhereLoop *p;
141243 for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
141244 if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
141245 /* If either the iTab or iSortIdx values for two WhereLoop are different
141246 ** then those WhereLoops need to be considered separately. Neither is
141247 ** a candidate to replace the other. */
141248 continue;
141249 }
141250 /* In the current implementation, the rSetup value is either zero
141251 ** or the cost of building an automatic index (NlogN) and the NlogN
141252 ** is the same for compatible WhereLoops. */
141253 assert( p->rSetup==0 || pTemplate->rSetup==0
141254 || p->rSetup==pTemplate->rSetup );
141255
141256 /* whereLoopAddBtree() always generates and inserts the automatic index
141257 ** case first. Hence compatible candidate WhereLoops never have a larger
141258 ** rSetup. Call this SETUP-INVARIANT */
141259 assert( p->rSetup>=pTemplate->rSetup );
141260
141261 /* Any loop using an appliation-defined index (or PRIMARY KEY or
141262 ** UNIQUE constraint) with one or more == constraints is better
141263 ** than an automatic index. Unless it is a skip-scan. */
141264 if( (p->wsFlags & WHERE_AUTO_INDEX)!=0
141265 && (pTemplate->nSkip)==0
141266 && (pTemplate->wsFlags & WHERE_INDEXED)!=0
141267 && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0
141268 && (p->prereq & pTemplate->prereq)==pTemplate->prereq
141269 ){
141270 break;
141271 }
141272
141273 /* If existing WhereLoop p is better than pTemplate, pTemplate can be
141274 ** discarded. WhereLoop p is better if:
141275 ** (1) p has no more dependencies than pTemplate, and
141276 ** (2) p has an equal or lower cost than pTemplate
141277 */
141278 if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */
141279 && p->rSetup<=pTemplate->rSetup /* (2a) */
141280 && p->rRun<=pTemplate->rRun /* (2b) */
141281 && p->nOut<=pTemplate->nOut /* (2c) */
141282 ){
141283 return 0; /* Discard pTemplate */
141284 }
141285
141286 /* If pTemplate is always better than p, then cause p to be overwritten
141287 ** with pTemplate. pTemplate is better than p if:
141288 ** (1) pTemplate has no more dependences than p, and
141289 ** (2) pTemplate has an equal or lower cost than p.
141290 */
141291 if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */
141292 && p->rRun>=pTemplate->rRun /* (2a) */
141293 && p->nOut>=pTemplate->nOut /* (2b) */
141294 ){
141295 assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
141296 break; /* Cause p to be overwritten by pTemplate */
141297 }
141298 }
141299 return ppPrev;
141300}
141301
141302/*
141303** Insert or replace a WhereLoop entry using the template supplied.
141304**
141305** An existing WhereLoop entry might be overwritten if the new template
141306** is better and has fewer dependencies. Or the template will be ignored
141307** and no insert will occur if an existing WhereLoop is faster and has
141308** fewer dependencies than the template. Otherwise a new WhereLoop is
141309** added based on the template.
141310**
141311** If pBuilder->pOrSet is not NULL then we care about only the
141312** prerequisites and rRun and nOut costs of the N best loops. That
141313** information is gathered in the pBuilder->pOrSet object. This special
141314** processing mode is used only for OR clause processing.
141315**
141316** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
141317** still might overwrite similar loops with the new template if the
141318** new template is better. Loops may be overwritten if the following
141319** conditions are met:
141320**
141321** (1) They have the same iTab.
141322** (2) They have the same iSortIdx.
141323** (3) The template has same or fewer dependencies than the current loop
141324** (4) The template has the same or lower cost than the current loop
141325*/
141326static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
141327 WhereLoop **ppPrev, *p;
141328 WhereInfo *pWInfo = pBuilder->pWInfo;
141329 sqlite3 *db = pWInfo->pParse->db;
141330 int rc;
141331
141332 /* Stop the search once we hit the query planner search limit */
141333 if( pBuilder->iPlanLimit==0 ){
141334 WHERETRACE(0xffffffff,("=== query planner search limit reached ===\n"));
141335 if( pBuilder->pOrSet ) pBuilder->pOrSet->n = 0;
141336 return SQLITE_DONE;
141337 }
141338 pBuilder->iPlanLimit--;
141339
141340 /* If pBuilder->pOrSet is defined, then only keep track of the costs
141341 ** and prereqs.
141342 */
141343 if( pBuilder->pOrSet!=0 ){
141344 if( pTemplate->nLTerm ){
141345#if WHERETRACE_ENABLED
141346 u16 n = pBuilder->pOrSet->n;
141347 int x =
141348#endif
141349 whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
141350 pTemplate->nOut);
141351#if WHERETRACE_ENABLED /* 0x8 */
141352 if( sqlite3WhereTrace & 0x8 ){
141353 sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n);
141354 whereLoopPrint(pTemplate, pBuilder->pWC);
141355 }
141356#endif
141357 }
141358 return SQLITE_OK;
141359 }
141360
141361 /* Look for an existing WhereLoop to replace with pTemplate
141362 */
141363 whereLoopAdjustCost(pWInfo->pLoops, pTemplate);
141364 ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate);
141365
141366 if( ppPrev==0 ){
141367 /* There already exists a WhereLoop on the list that is better
141368 ** than pTemplate, so just ignore pTemplate */
141369#if WHERETRACE_ENABLED /* 0x8 */
141370 if( sqlite3WhereTrace & 0x8 ){
141371 sqlite3DebugPrintf(" skip: ");
141372 whereLoopPrint(pTemplate, pBuilder->pWC);
141373 }
141374#endif
141375 return SQLITE_OK;
141376 }else{
141377 p = *ppPrev;
141378 }
141379
141380 /* If we reach this point it means that either p[] should be overwritten
141381 ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new
141382 ** WhereLoop and insert it.
141383 */
141384#if WHERETRACE_ENABLED /* 0x8 */
141385 if( sqlite3WhereTrace & 0x8 ){
141386 if( p!=0 ){
141387 sqlite3DebugPrintf("replace: ");
141388 whereLoopPrint(p, pBuilder->pWC);
141389 sqlite3DebugPrintf(" with: ");
141390 }else{
141391 sqlite3DebugPrintf(" add: ");
141392 }
141393 whereLoopPrint(pTemplate, pBuilder->pWC);
141394 }
141395#endif
141396 if( p==0 ){
141397 /* Allocate a new WhereLoop to add to the end of the list */
141398 *ppPrev = p = sqlite3DbMallocRawNN(db, sizeof(WhereLoop));
141399 if( p==0 ) return SQLITE_NOMEM_BKPT;
141400 whereLoopInit(p);
141401 p->pNextLoop = 0;
141402 }else{
141403 /* We will be overwriting WhereLoop p[]. But before we do, first
141404 ** go through the rest of the list and delete any other entries besides
141405 ** p[] that are also supplated by pTemplate */
141406 WhereLoop **ppTail = &p->pNextLoop;
141407 WhereLoop *pToDel;
141408 while( *ppTail ){
141409 ppTail = whereLoopFindLesser(ppTail, pTemplate);
141410 if( ppTail==0 ) break;
141411 pToDel = *ppTail;
141412 if( pToDel==0 ) break;
141413 *ppTail = pToDel->pNextLoop;
141414#if WHERETRACE_ENABLED /* 0x8 */
141415 if( sqlite3WhereTrace & 0x8 ){
141416 sqlite3DebugPrintf(" delete: ");
141417 whereLoopPrint(pToDel, pBuilder->pWC);
141418 }
141419#endif
141420 whereLoopDelete(db, pToDel);
141421 }
141422 }
141423 rc = whereLoopXfer(db, p, pTemplate);
141424 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
141425 Index *pIndex = p->u.btree.pIndex;
141426 if( pIndex && pIndex->tnum==0 ){
141427 p->u.btree.pIndex = 0;
141428 }
141429 }
141430 return rc;
141431}
141432
141433/*
141434** Adjust the WhereLoop.nOut value downward to account for terms of the
141435** WHERE clause that reference the loop but which are not used by an
141436** index.
141437*
141438** For every WHERE clause term that is not used by the index
141439** and which has a truth probability assigned by one of the likelihood(),
141440** likely(), or unlikely() SQL functions, reduce the estimated number
141441** of output rows by the probability specified.
141442**
141443** TUNING: For every WHERE clause term that is not used by the index
141444** and which does not have an assigned truth probability, heuristics
141445** described below are used to try to estimate the truth probability.
141446** TODO --> Perhaps this is something that could be improved by better
141447** table statistics.
141448**
141449** Heuristic 1: Estimate the truth probability as 93.75%. The 93.75%
141450** value corresponds to -1 in LogEst notation, so this means decrement
141451** the WhereLoop.nOut field for every such WHERE clause term.
141452**
141453** Heuristic 2: If there exists one or more WHERE clause terms of the
141454** form "x==EXPR" and EXPR is not a constant 0 or 1, then make sure the
141455** final output row estimate is no greater than 1/4 of the total number
141456** of rows in the table. In other words, assume that x==EXPR will filter
141457** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the
141458** "x" column is boolean or else -1 or 0 or 1 is a common default value
141459** on the "x" column and so in that case only cap the output row estimate
141460** at 1/2 instead of 1/4.
141461*/
141462static void whereLoopOutputAdjust(
141463 WhereClause *pWC, /* The WHERE clause */
141464 WhereLoop *pLoop, /* The loop to adjust downward */
141465 LogEst nRow /* Number of rows in the entire table */
141466){
141467 WhereTerm *pTerm, *pX;
141468 Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
141469 int i, j, k;
141470 LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */
141471
141472 assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
141473 for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){
141474 if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break;
141475 if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
141476 if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
141477 for(j=pLoop->nLTerm-1; j>=0; j--){
141478 pX = pLoop->aLTerm[j];
141479 if( pX==0 ) continue;
141480 if( pX==pTerm ) break;
141481 if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
141482 }
141483 if( j<0 ){
141484 if( pTerm->truthProb<=0 ){
141485 /* If a truth probability is specified using the likelihood() hints,
141486 ** then use the probability provided by the application. */
141487 pLoop->nOut += pTerm->truthProb;
141488 }else{
141489 /* In the absence of explicit truth probabilities, use heuristics to
141490 ** guess a reasonable truth probability. */
141491 pLoop->nOut--;
141492 if( pTerm->eOperator&(WO_EQ|WO_IS) ){
141493 Expr *pRight = pTerm->pExpr->pRight;
141494 testcase( pTerm->pExpr->op==TK_IS );
141495 if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
141496 k = 10;
141497 }else{
141498 k = 20;
141499 }
141500 if( iReduce<k ) iReduce = k;
141501 }
141502 }
141503 }
141504 }
141505 if( pLoop->nOut > nRow-iReduce ) pLoop->nOut = nRow - iReduce;
141506}
141507
141508/*
141509** Term pTerm is a vector range comparison operation. The first comparison
141510** in the vector can be optimized using column nEq of the index. This
141511** function returns the total number of vector elements that can be used
141512** as part of the range comparison.
141513**
141514** For example, if the query is:
141515**
141516** WHERE a = ? AND (b, c, d) > (?, ?, ?)
141517**
141518** and the index:
141519**
141520** CREATE INDEX ... ON (a, b, c, d, e)
141521**
141522** then this function would be invoked with nEq=1. The value returned in
141523** this case is 3.
141524*/
141525static int whereRangeVectorLen(
141526 Parse *pParse, /* Parsing context */
141527 int iCur, /* Cursor open on pIdx */
141528 Index *pIdx, /* The index to be used for a inequality constraint */
141529 int nEq, /* Number of prior equality constraints on same index */
141530 WhereTerm *pTerm /* The vector inequality constraint */
141531){
141532 int nCmp = sqlite3ExprVectorSize(pTerm->pExpr->pLeft);
141533 int i;
141534
141535 nCmp = MIN(nCmp, (pIdx->nColumn - nEq));
141536 for(i=1; i<nCmp; i++){
141537 /* Test if comparison i of pTerm is compatible with column (i+nEq)
141538 ** of the index. If not, exit the loop. */
141539 char aff; /* Comparison affinity */
141540 char idxaff = 0; /* Indexed columns affinity */
141541 CollSeq *pColl; /* Comparison collation sequence */
141542 Expr *pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr;
141543 Expr *pRhs = pTerm->pExpr->pRight;
141544 if( pRhs->flags & EP_xIsSelect ){
141545 pRhs = pRhs->x.pSelect->pEList->a[i].pExpr;
141546 }else{
141547 pRhs = pRhs->x.pList->a[i].pExpr;
141548 }
141549
141550 /* Check that the LHS of the comparison is a column reference to
141551 ** the right column of the right source table. And that the sort
141552 ** order of the index column is the same as the sort order of the
141553 ** leftmost index column. */
141554 if( pLhs->op!=TK_COLUMN
141555 || pLhs->iTable!=iCur
141556 || pLhs->iColumn!=pIdx->aiColumn[i+nEq]
141557 || pIdx->aSortOrder[i+nEq]!=pIdx->aSortOrder[nEq]
141558 ){
141559 break;
141560 }
141561
141562 testcase( pLhs->iColumn==XN_ROWID );
141563 aff = sqlite3CompareAffinity(pRhs, sqlite3ExprAffinity(pLhs));
141564 idxaff = sqlite3TableColumnAffinity(pIdx->pTable, pLhs->iColumn);
141565 if( aff!=idxaff ) break;
141566
141567 pColl = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs);
141568 if( pColl==0 ) break;
141569 if( sqlite3StrICmp(pColl->zName, pIdx->azColl[i+nEq]) ) break;
141570 }
141571 return i;
141572}
141573
141574/*
141575** Adjust the cost C by the costMult facter T. This only occurs if
141576** compiled with -DSQLITE_ENABLE_COSTMULT
141577*/
141578#ifdef SQLITE_ENABLE_COSTMULT
141579# define ApplyCostMultiplier(C,T) C += T
141580#else
141581# define ApplyCostMultiplier(C,T)
141582#endif
141583
141584/*
141585** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
141586** index pIndex. Try to match one more.
141587**
141588** When this function is called, pBuilder->pNew->nOut contains the
141589** number of rows expected to be visited by filtering using the nEq
141590** terms only. If it is modified, this value is restored before this
141591** function returns.
141592**
141593** If pProbe->tnum==0, that means pIndex is a fake index used for the
141594** INTEGER PRIMARY KEY.
141595*/
141596static int whereLoopAddBtreeIndex(
141597 WhereLoopBuilder *pBuilder, /* The WhereLoop factory */
141598 struct SrcList_item *pSrc, /* FROM clause term being analyzed */
141599 Index *pProbe, /* An index on pSrc */
141600 LogEst nInMul /* log(Number of iterations due to IN) */
141601){
141602 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */
141603 Parse *pParse = pWInfo->pParse; /* Parsing context */
141604 sqlite3 *db = pParse->db; /* Database connection malloc context */
141605 WhereLoop *pNew; /* Template WhereLoop under construction */
141606 WhereTerm *pTerm; /* A WhereTerm under consideration */
141607 int opMask; /* Valid operators for constraints */
141608 WhereScan scan; /* Iterator for WHERE terms */
141609 Bitmask saved_prereq; /* Original value of pNew->prereq */
141610 u16 saved_nLTerm; /* Original value of pNew->nLTerm */
141611 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */
141612 u16 saved_nBtm; /* Original value of pNew->u.btree.nBtm */
141613 u16 saved_nTop; /* Original value of pNew->u.btree.nTop */
141614 u16 saved_nSkip; /* Original value of pNew->nSkip */
141615 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
141616 LogEst saved_nOut; /* Original value of pNew->nOut */
141617 int rc = SQLITE_OK; /* Return code */
141618 LogEst rSize; /* Number of rows in the table */
141619 LogEst rLogSize; /* Logarithm of table size */
141620 WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */
141621
141622 pNew = pBuilder->pNew;
141623 if( db->mallocFailed ) return SQLITE_NOMEM_BKPT;
141624 WHERETRACE(0x800, ("BEGIN %s.addBtreeIdx(%s), nEq=%d\n",
141625 pProbe->pTable->zName,pProbe->zName, pNew->u.btree.nEq));
141626
141627 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
141628 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
141629 if( pNew->wsFlags & WHERE_BTM_LIMIT ){
141630 opMask = WO_LT|WO_LE;
141631 }else{
141632 assert( pNew->u.btree.nBtm==0 );
141633 opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE|WO_ISNULL|WO_IS;
141634 }
141635 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
141636
141637 assert( pNew->u.btree.nEq<pProbe->nColumn );
141638
141639 saved_nEq = pNew->u.btree.nEq;
141640 saved_nBtm = pNew->u.btree.nBtm;
141641 saved_nTop = pNew->u.btree.nTop;
141642 saved_nSkip = pNew->nSkip;
141643 saved_nLTerm = pNew->nLTerm;
141644 saved_wsFlags = pNew->wsFlags;
141645 saved_prereq = pNew->prereq;
141646 saved_nOut = pNew->nOut;
141647 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, saved_nEq,
141648 opMask, pProbe);
141649 pNew->rSetup = 0;
141650 rSize = pProbe->aiRowLogEst[0];
141651 rLogSize = estLog(rSize);
141652 for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
141653 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
141654 LogEst rCostIdx;
141655 LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */
141656 int nIn = 0;
141657#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
141658 int nRecValid = pBuilder->nRecValid;
141659#endif
141660 if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
141661 && indexColumnNotNull(pProbe, saved_nEq)
141662 ){
141663 continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
141664 }
141665 if( pTerm->prereqRight & pNew->maskSelf ) continue;
141666
141667 /* Do not allow the upper bound of a LIKE optimization range constraint
141668 ** to mix with a lower range bound from some other source */
141669 if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue;
141670
141671 /* Do not allow constraints from the WHERE clause to be used by the
141672 ** right table of a LEFT JOIN. Only constraints in the ON clause are
141673 ** allowed */
141674 if( (pSrc->fg.jointype & JT_LEFT)!=0
141675 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
141676 ){
141677 continue;
141678 }
141679
141680 if( IsUniqueIndex(pProbe) && saved_nEq==pProbe->nKeyCol-1 ){
141681 pBuilder->bldFlags |= SQLITE_BLDF_UNIQUE;
141682 }else{
141683 pBuilder->bldFlags |= SQLITE_BLDF_INDEXED;
141684 }
141685 pNew->wsFlags = saved_wsFlags;
141686 pNew->u.btree.nEq = saved_nEq;
141687 pNew->u.btree.nBtm = saved_nBtm;
141688 pNew->u.btree.nTop = saved_nTop;
141689 pNew->nLTerm = saved_nLTerm;
141690 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
141691 pNew->aLTerm[pNew->nLTerm++] = pTerm;
141692 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
141693
141694 assert( nInMul==0
141695 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0
141696 || (pNew->wsFlags & WHERE_COLUMN_IN)!=0
141697 || (pNew->wsFlags & WHERE_SKIPSCAN)!=0
141698 );
141699
141700 if( eOp & WO_IN ){
141701 Expr *pExpr = pTerm->pExpr;
141702 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
141703 /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */
141704 int i;
141705 nIn = 46; assert( 46==sqlite3LogEst(25) );
141706
141707 /* The expression may actually be of the form (x, y) IN (SELECT...).
141708 ** In this case there is a separate term for each of (x) and (y).
141709 ** However, the nIn multiplier should only be applied once, not once
141710 ** for each such term. The following loop checks that pTerm is the
141711 ** first such term in use, and sets nIn back to 0 if it is not. */
141712 for(i=0; i<pNew->nLTerm-1; i++){
141713 if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ) nIn = 0;
141714 }
141715 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
141716 /* "x IN (value, value, ...)" */
141717 nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
141718 assert( nIn>0 ); /* RHS always has 2 or more terms... The parser
141719 ** changes "x IN (?)" into "x=?". */
141720 }
141721 if( pProbe->hasStat1 ){
141722 LogEst M, logK, safetyMargin;
141723 /* Let:
141724 ** N = the total number of rows in the table
141725 ** K = the number of entries on the RHS of the IN operator
141726 ** M = the number of rows in the table that match terms to the
141727 ** to the left in the same index. If the IN operator is on
141728 ** the left-most index column, M==N.
141729 **
141730 ** Given the definitions above, it is better to omit the IN operator
141731 ** from the index lookup and instead do a scan of the M elements,
141732 ** testing each scanned row against the IN operator separately, if:
141733 **
141734 ** M*log(K) < K*log(N)
141735 **
141736 ** Our estimates for M, K, and N might be inaccurate, so we build in
141737 ** a safety margin of 2 (LogEst: 10) that favors using the IN operator
141738 ** with the index, as using an index has better worst-case behavior.
141739 ** If we do not have real sqlite_stat1 data, always prefer to use
141740 ** the index.
141741 */
141742 M = pProbe->aiRowLogEst[saved_nEq];
141743 logK = estLog(nIn);
141744 safetyMargin = 10; /* TUNING: extra weight for indexed IN */
141745 if( M + logK + safetyMargin < nIn + rLogSize ){
141746 WHERETRACE(0x40,
141747 ("Scan preferred over IN operator on column %d of \"%s\" (%d<%d)\n",
141748 saved_nEq, pProbe->zName, M+logK+10, nIn+rLogSize));
141749 continue;
141750 }else{
141751 WHERETRACE(0x40,
141752 ("IN operator preferred on column %d of \"%s\" (%d>=%d)\n",
141753 saved_nEq, pProbe->zName, M+logK+10, nIn+rLogSize));
141754 }
141755 }
141756 pNew->wsFlags |= WHERE_COLUMN_IN;
141757 }else if( eOp & (WO_EQ|WO_IS) ){
141758 int iCol = pProbe->aiColumn[saved_nEq];
141759 pNew->wsFlags |= WHERE_COLUMN_EQ;
141760 assert( saved_nEq==pNew->u.btree.nEq );
141761 if( iCol==XN_ROWID
141762 || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
141763 ){
141764 if( iCol==XN_ROWID || pProbe->uniqNotNull
141765 || (pProbe->nKeyCol==1 && pProbe->onError && eOp==WO_EQ)
141766 ){
141767 pNew->wsFlags |= WHERE_ONEROW;
141768 }else{
141769 pNew->wsFlags |= WHERE_UNQ_WANTED;
141770 }
141771 }
141772 }else if( eOp & WO_ISNULL ){
141773 pNew->wsFlags |= WHERE_COLUMN_NULL;
141774 }else if( eOp & (WO_GT|WO_GE) ){
141775 testcase( eOp & WO_GT );
141776 testcase( eOp & WO_GE );
141777 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
141778 pNew->u.btree.nBtm = whereRangeVectorLen(
141779 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
141780 );
141781 pBtm = pTerm;
141782 pTop = 0;
141783 if( pTerm->wtFlags & TERM_LIKEOPT ){
141784 /* Range contraints that come from the LIKE optimization are
141785 ** always used in pairs. */
141786 pTop = &pTerm[1];
141787 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
141788 assert( pTop->wtFlags & TERM_LIKEOPT );
141789 assert( pTop->eOperator==WO_LT );
141790 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
141791 pNew->aLTerm[pNew->nLTerm++] = pTop;
141792 pNew->wsFlags |= WHERE_TOP_LIMIT;
141793 pNew->u.btree.nTop = 1;
141794 }
141795 }else{
141796 assert( eOp & (WO_LT|WO_LE) );
141797 testcase( eOp & WO_LT );
141798 testcase( eOp & WO_LE );
141799 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
141800 pNew->u.btree.nTop = whereRangeVectorLen(
141801 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
141802 );
141803 pTop = pTerm;
141804 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
141805 pNew->aLTerm[pNew->nLTerm-2] : 0;
141806 }
141807
141808 /* At this point pNew->nOut is set to the number of rows expected to
141809 ** be visited by the index scan before considering term pTerm, or the
141810 ** values of nIn and nInMul. In other words, assuming that all
141811 ** "x IN(...)" terms are replaced with "x = ?". This block updates
141812 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */
141813 assert( pNew->nOut==saved_nOut );
141814 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
141815 /* Adjust nOut using stat3/stat4 data. Or, if there is no stat3/stat4
141816 ** data, using some other estimate. */
141817 whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
141818 }else{
141819 int nEq = ++pNew->u.btree.nEq;
141820 assert( eOp & (WO_ISNULL|WO_EQ|WO_IN|WO_IS) );
141821
141822 assert( pNew->nOut==saved_nOut );
141823 if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){
141824 assert( (eOp & WO_IN) || nIn==0 );
141825 testcase( eOp & WO_IN );
141826 pNew->nOut += pTerm->truthProb;
141827 pNew->nOut -= nIn;
141828 }else{
141829#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
141830 tRowcnt nOut = 0;
141831 if( nInMul==0
141832 && pProbe->nSample
141833 && pNew->u.btree.nEq<=pProbe->nSampleCol
141834 && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect))
141835 && OptimizationEnabled(db, SQLITE_Stat34)
141836 ){
141837 Expr *pExpr = pTerm->pExpr;
141838 if( (eOp & (WO_EQ|WO_ISNULL|WO_IS))!=0 ){
141839 testcase( eOp & WO_EQ );
141840 testcase( eOp & WO_IS );
141841 testcase( eOp & WO_ISNULL );
141842 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
141843 }else{
141844 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
141845 }
141846 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
141847 if( rc!=SQLITE_OK ) break; /* Jump out of the pTerm loop */
141848 if( nOut ){
141849 pNew->nOut = sqlite3LogEst(nOut);
141850 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
141851 pNew->nOut -= nIn;
141852 }
141853 }
141854 if( nOut==0 )
141855#endif
141856 {
141857 pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
141858 if( eOp & WO_ISNULL ){
141859 /* TUNING: If there is no likelihood() value, assume that a
141860 ** "col IS NULL" expression matches twice as many rows
141861 ** as (col=?). */
141862 pNew->nOut += 10;
141863 }
141864 }
141865 }
141866 }
141867
141868 /* Set rCostIdx to the cost of visiting selected rows in index. Add
141869 ** it to pNew->rRun, which is currently set to the cost of the index
141870 ** seek only. Then, if this is a non-covering index, add the cost of
141871 ** visiting the rows in the main table. */
141872 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
141873 pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
141874 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
141875 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
141876 }
141877 ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
141878
141879 nOutUnadjusted = pNew->nOut;
141880 pNew->rRun += nInMul + nIn;
141881 pNew->nOut += nInMul + nIn;
141882 whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
141883 rc = whereLoopInsert(pBuilder, pNew);
141884
141885 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
141886 pNew->nOut = saved_nOut;
141887 }else{
141888 pNew->nOut = nOutUnadjusted;
141889 }
141890
141891 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
141892 && pNew->u.btree.nEq<pProbe->nColumn
141893 ){
141894 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
141895 }
141896 pNew->nOut = saved_nOut;
141897#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
141898 pBuilder->nRecValid = nRecValid;
141899#endif
141900 }
141901 pNew->prereq = saved_prereq;
141902 pNew->u.btree.nEq = saved_nEq;
141903 pNew->u.btree.nBtm = saved_nBtm;
141904 pNew->u.btree.nTop = saved_nTop;
141905 pNew->nSkip = saved_nSkip;
141906 pNew->wsFlags = saved_wsFlags;
141907 pNew->nOut = saved_nOut;
141908 pNew->nLTerm = saved_nLTerm;
141909
141910 /* Consider using a skip-scan if there are no WHERE clause constraints
141911 ** available for the left-most terms of the index, and if the average
141912 ** number of repeats in the left-most terms is at least 18.
141913 **
141914 ** The magic number 18 is selected on the basis that scanning 17 rows
141915 ** is almost always quicker than an index seek (even though if the index
141916 ** contains fewer than 2^17 rows we assume otherwise in other parts of
141917 ** the code). And, even if it is not, it should not be too much slower.
141918 ** On the other hand, the extra seeks could end up being significantly
141919 ** more expensive. */
141920 assert( 42==sqlite3LogEst(18) );
141921 if( saved_nEq==saved_nSkip
141922 && saved_nEq+1<pProbe->nKeyCol
141923 && pProbe->noSkipScan==0
141924 && OptimizationEnabled(db, SQLITE_SkipScan)
141925 && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
141926 && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
141927 ){
141928 LogEst nIter;
141929 pNew->u.btree.nEq++;
141930 pNew->nSkip++;
141931 pNew->aLTerm[pNew->nLTerm++] = 0;
141932 pNew->wsFlags |= WHERE_SKIPSCAN;
141933 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
141934 pNew->nOut -= nIter;
141935 /* TUNING: Because uncertainties in the estimates for skip-scan queries,
141936 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
141937 nIter += 5;
141938 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul);
141939 pNew->nOut = saved_nOut;
141940 pNew->u.btree.nEq = saved_nEq;
141941 pNew->nSkip = saved_nSkip;
141942 pNew->wsFlags = saved_wsFlags;
141943 }
141944
141945 WHERETRACE(0x800, ("END %s.addBtreeIdx(%s), nEq=%d, rc=%d\n",
141946 pProbe->pTable->zName, pProbe->zName, saved_nEq, rc));
141947 return rc;
141948}
141949
141950/*
141951** Return True if it is possible that pIndex might be useful in
141952** implementing the ORDER BY clause in pBuilder.
141953**
141954** Return False if pBuilder does not contain an ORDER BY clause or
141955** if there is no way for pIndex to be useful in implementing that
141956** ORDER BY clause.
141957*/
141958static int indexMightHelpWithOrderBy(
141959 WhereLoopBuilder *pBuilder,
141960 Index *pIndex,
141961 int iCursor
141962){
141963 ExprList *pOB;
141964 ExprList *aColExpr;
141965 int ii, jj;
141966
141967 if( pIndex->bUnordered ) return 0;
141968 if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
141969 for(ii=0; ii<pOB->nExpr; ii++){
141970 Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr);
141971 if( pExpr->op==TK_COLUMN && pExpr->iTable==iCursor ){
141972 if( pExpr->iColumn<0 ) return 1;
141973 for(jj=0; jj<pIndex->nKeyCol; jj++){
141974 if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
141975 }
141976 }else if( (aColExpr = pIndex->aColExpr)!=0 ){
141977 for(jj=0; jj<pIndex->nKeyCol; jj++){
141978 if( pIndex->aiColumn[jj]!=XN_EXPR ) continue;
141979 if( sqlite3ExprCompareSkip(pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){
141980 return 1;
141981 }
141982 }
141983 }
141984 }
141985 return 0;
141986}
141987
141988/* Check to see if a partial index with pPartIndexWhere can be used
141989** in the current query. Return true if it can be and false if not.
141990*/
141991static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){
141992 int i;
141993 WhereTerm *pTerm;
141994 Parse *pParse = pWC->pWInfo->pParse;
141995 while( pWhere->op==TK_AND ){
141996 if( !whereUsablePartialIndex(iTab,pWC,pWhere->pLeft) ) return 0;
141997 pWhere = pWhere->pRight;
141998 }
141999 if( pParse->db->flags & SQLITE_EnableQPSG ) pParse = 0;
142000 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
142001 Expr *pExpr = pTerm->pExpr;
142002 if( (!ExprHasProperty(pExpr, EP_FromJoin) || pExpr->iRightJoinTable==iTab)
142003 && sqlite3ExprImpliesExpr(pParse, pExpr, pWhere, iTab)
142004 ){
142005 return 1;
142006 }
142007 }
142008 return 0;
142009}
142010
142011/*
142012** Add all WhereLoop objects for a single table of the join where the table
142013** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
142014** a b-tree table, not a virtual table.
142015**
142016** The costs (WhereLoop.rRun) of the b-tree loops added by this function
142017** are calculated as follows:
142018**
142019** For a full scan, assuming the table (or index) contains nRow rows:
142020**
142021** cost = nRow * 3.0 // full-table scan
142022** cost = nRow * K // scan of covering index
142023** cost = nRow * (K+3.0) // scan of non-covering index
142024**
142025** where K is a value between 1.1 and 3.0 set based on the relative
142026** estimated average size of the index and table records.
142027**
142028** For an index scan, where nVisit is the number of index rows visited
142029** by the scan, and nSeek is the number of seek operations required on
142030** the index b-tree:
142031**
142032** cost = nSeek * (log(nRow) + K * nVisit) // covering index
142033** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
142034**
142035** Normally, nSeek is 1. nSeek values greater than 1 come about if the
142036** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when
142037** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
142038**
142039** The estimated values (nRow, nVisit, nSeek) often contain a large amount
142040** of uncertainty. For this reason, scoring is designed to pick plans that
142041** "do the least harm" if the estimates are inaccurate. For example, a
142042** log(nRow) factor is omitted from a non-covering index scan in order to
142043** bias the scoring in favor of using an index, since the worst-case
142044** performance of using an index is far better than the worst-case performance
142045** of a full table scan.
142046*/
142047static int whereLoopAddBtree(
142048 WhereLoopBuilder *pBuilder, /* WHERE clause information */
142049 Bitmask mPrereq /* Extra prerequesites for using this table */
142050){
142051 WhereInfo *pWInfo; /* WHERE analysis context */
142052 Index *pProbe; /* An index we are evaluating */
142053 Index sPk; /* A fake index object for the primary key */
142054 LogEst aiRowEstPk[2]; /* The aiRowLogEst[] value for the sPk index */
142055 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
142056 SrcList *pTabList; /* The FROM clause */
142057 struct SrcList_item *pSrc; /* The FROM clause btree term to add */
142058 WhereLoop *pNew; /* Template WhereLoop object */
142059 int rc = SQLITE_OK; /* Return code */
142060 int iSortIdx = 1; /* Index number */
142061 int b; /* A boolean value */
142062 LogEst rSize; /* number of rows in the table */
142063 LogEst rLogSize; /* Logarithm of the number of rows in the table */
142064 WhereClause *pWC; /* The parsed WHERE clause */
142065 Table *pTab; /* Table being queried */
142066
142067 pNew = pBuilder->pNew;
142068 pWInfo = pBuilder->pWInfo;
142069 pTabList = pWInfo->pTabList;
142070 pSrc = pTabList->a + pNew->iTab;
142071 pTab = pSrc->pTab;
142072 pWC = pBuilder->pWC;
142073 assert( !IsVirtual(pSrc->pTab) );
142074
142075 if( pSrc->pIBIndex ){
142076 /* An INDEXED BY clause specifies a particular index to use */
142077 pProbe = pSrc->pIBIndex;
142078 }else if( !HasRowid(pTab) ){
142079 pProbe = pTab->pIndex;
142080 }else{
142081 /* There is no INDEXED BY clause. Create a fake Index object in local
142082 ** variable sPk to represent the rowid primary key index. Make this
142083 ** fake index the first in a chain of Index objects with all of the real
142084 ** indices to follow */
142085 Index *pFirst; /* First of real indices on the table */
142086 memset(&sPk, 0, sizeof(Index));
142087 sPk.nKeyCol = 1;
142088 sPk.nColumn = 1;
142089 sPk.aiColumn = &aiColumnPk;
142090 sPk.aiRowLogEst = aiRowEstPk;
142091 sPk.onError = OE_Replace;
142092 sPk.pTable = pTab;
142093 sPk.szIdxRow = pTab->szTabRow;
142094 aiRowEstPk[0] = pTab->nRowLogEst;
142095 aiRowEstPk[1] = 0;
142096 pFirst = pSrc->pTab->pIndex;
142097 if( pSrc->fg.notIndexed==0 ){
142098 /* The real indices of the table are only considered if the
142099 ** NOT INDEXED qualifier is omitted from the FROM clause */
142100 sPk.pNext = pFirst;
142101 }
142102 pProbe = &sPk;
142103 }
142104 rSize = pTab->nRowLogEst;
142105 rLogSize = estLog(rSize);
142106
142107#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
142108 /* Automatic indexes */
142109 if( !pBuilder->pOrSet /* Not part of an OR optimization */
142110 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0
142111 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
142112 && pSrc->pIBIndex==0 /* Has no INDEXED BY clause */
142113 && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */
142114 && HasRowid(pTab) /* Not WITHOUT ROWID table. (FIXME: Why not?) */
142115 && !pSrc->fg.isCorrelated /* Not a correlated subquery */
142116 && !pSrc->fg.isRecursive /* Not a recursive common table expression. */
142117 ){
142118 /* Generate auto-index WhereLoops */
142119 WhereTerm *pTerm;
142120 WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
142121 for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
142122 if( pTerm->prereqRight & pNew->maskSelf ) continue;
142123 if( termCanDriveIndex(pTerm, pSrc, 0) ){
142124 pNew->u.btree.nEq = 1;
142125 pNew->nSkip = 0;
142126 pNew->u.btree.pIndex = 0;
142127 pNew->nLTerm = 1;
142128 pNew->aLTerm[0] = pTerm;
142129 /* TUNING: One-time cost for computing the automatic index is
142130 ** estimated to be X*N*log2(N) where N is the number of rows in
142131 ** the table being indexed and where X is 7 (LogEst=28) for normal
142132 ** tables or 0.5 (LogEst=-10) for views and subqueries. The value
142133 ** of X is smaller for views and subqueries so that the query planner
142134 ** will be more aggressive about generating automatic indexes for
142135 ** those objects, since there is no opportunity to add schema
142136 ** indexes on subqueries and views. */
142137 pNew->rSetup = rLogSize + rSize;
142138 if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){
142139 pNew->rSetup += 28;
142140 }else{
142141 pNew->rSetup -= 10;
142142 }
142143 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
142144 if( pNew->rSetup<0 ) pNew->rSetup = 0;
142145 /* TUNING: Each index lookup yields 20 rows in the table. This
142146 ** is more than the usual guess of 10 rows, since we have no way
142147 ** of knowing how selective the index will ultimately be. It would
142148 ** not be unreasonable to make this value much larger. */
142149 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) );
142150 pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
142151 pNew->wsFlags = WHERE_AUTO_INDEX;
142152 pNew->prereq = mPrereq | pTerm->prereqRight;
142153 rc = whereLoopInsert(pBuilder, pNew);
142154 }
142155 }
142156 }
142157#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
142158
142159 /* Loop over all indices. If there was an INDEXED BY clause, then only
142160 ** consider index pProbe. */
142161 for(; rc==SQLITE_OK && pProbe;
142162 pProbe=(pSrc->pIBIndex ? 0 : pProbe->pNext), iSortIdx++
142163 ){
142164 if( pProbe->pPartIdxWhere!=0
142165 && !whereUsablePartialIndex(pSrc->iCursor, pWC, pProbe->pPartIdxWhere) ){
142166 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */
142167 continue; /* Partial index inappropriate for this query */
142168 }
142169 if( pProbe->bNoQuery ) continue;
142170 rSize = pProbe->aiRowLogEst[0];
142171 pNew->u.btree.nEq = 0;
142172 pNew->u.btree.nBtm = 0;
142173 pNew->u.btree.nTop = 0;
142174 pNew->nSkip = 0;
142175 pNew->nLTerm = 0;
142176 pNew->iSortIdx = 0;
142177 pNew->rSetup = 0;
142178 pNew->prereq = mPrereq;
142179 pNew->nOut = rSize;
142180 pNew->u.btree.pIndex = pProbe;
142181 b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
142182 /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */
142183 assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
142184 if( pProbe->tnum<=0 ){
142185 /* Integer primary key index */
142186 pNew->wsFlags = WHERE_IPK;
142187
142188 /* Full table scan */
142189 pNew->iSortIdx = b ? iSortIdx : 0;
142190 /* TUNING: Cost of full table scan is (N*3.0). */
142191 pNew->rRun = rSize + 16;
142192 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
142193 whereLoopOutputAdjust(pWC, pNew, rSize);
142194 rc = whereLoopInsert(pBuilder, pNew);
142195 pNew->nOut = rSize;
142196 if( rc ) break;
142197 }else{
142198 Bitmask m;
142199 if( pProbe->isCovering ){
142200 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
142201 m = 0;
142202 }else{
142203 m = pSrc->colUsed & pProbe->colNotIdxed;
142204 pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
142205 }
142206
142207 /* Full scan via index */
142208 if( b
142209 || !HasRowid(pTab)
142210 || pProbe->pPartIdxWhere!=0
142211 || ( m==0
142212 && pProbe->bUnordered==0
142213 && (pProbe->szIdxRow<pTab->szTabRow)
142214 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
142215 && sqlite3GlobalConfig.bUseCis
142216 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
142217 )
142218 ){
142219 pNew->iSortIdx = b ? iSortIdx : 0;
142220
142221 /* The cost of visiting the index rows is N*K, where K is
142222 ** between 1.1 and 3.0, depending on the relative sizes of the
142223 ** index and table rows. */
142224 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
142225 if( m!=0 ){
142226 /* If this is a non-covering index scan, add in the cost of
142227 ** doing table lookups. The cost will be 3x the number of
142228 ** lookups. Take into account WHERE clause terms that can be
142229 ** satisfied using just the index, and that do not require a
142230 ** table lookup. */
142231 LogEst nLookup = rSize + 16; /* Base cost: N*3 */
142232 int ii;
142233 int iCur = pSrc->iCursor;
142234 WhereClause *pWC2 = &pWInfo->sWC;
142235 for(ii=0; ii<pWC2->nTerm; ii++){
142236 WhereTerm *pTerm = &pWC2->a[ii];
142237 if( !sqlite3ExprCoveredByIndex(pTerm->pExpr, iCur, pProbe) ){
142238 break;
142239 }
142240 /* pTerm can be evaluated using just the index. So reduce
142241 ** the expected number of table lookups accordingly */
142242 if( pTerm->truthProb<=0 ){
142243 nLookup += pTerm->truthProb;
142244 }else{
142245 nLookup--;
142246 if( pTerm->eOperator & (WO_EQ|WO_IS) ) nLookup -= 19;
142247 }
142248 }
142249
142250 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, nLookup);
142251 }
142252 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
142253 whereLoopOutputAdjust(pWC, pNew, rSize);
142254 rc = whereLoopInsert(pBuilder, pNew);
142255 pNew->nOut = rSize;
142256 if( rc ) break;
142257 }
142258 }
142259
142260 pBuilder->bldFlags = 0;
142261 rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
142262 if( pBuilder->bldFlags==SQLITE_BLDF_INDEXED ){
142263 /* If a non-unique index is used, or if a prefix of the key for
142264 ** unique index is used (making the index functionally non-unique)
142265 ** then the sqlite_stat1 data becomes important for scoring the
142266 ** plan */
142267 pTab->tabFlags |= TF_StatsUsed;
142268 }
142269#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
142270 sqlite3Stat4ProbeFree(pBuilder->pRec);
142271 pBuilder->nRecValid = 0;
142272 pBuilder->pRec = 0;
142273#endif
142274 }
142275 return rc;
142276}
142277
142278#ifndef SQLITE_OMIT_VIRTUALTABLE
142279
142280/*
142281** Argument pIdxInfo is already populated with all constraints that may
142282** be used by the virtual table identified by pBuilder->pNew->iTab. This
142283** function marks a subset of those constraints usable, invokes the
142284** xBestIndex method and adds the returned plan to pBuilder.
142285**
142286** A constraint is marked usable if:
142287**
142288** * Argument mUsable indicates that its prerequisites are available, and
142289**
142290** * It is not one of the operators specified in the mExclude mask passed
142291** as the fourth argument (which in practice is either WO_IN or 0).
142292**
142293** Argument mPrereq is a mask of tables that must be scanned before the
142294** virtual table in question. These are added to the plans prerequisites
142295** before it is added to pBuilder.
142296**
142297** Output parameter *pbIn is set to true if the plan added to pBuilder
142298** uses one or more WO_IN terms, or false otherwise.
142299*/
142300static int whereLoopAddVirtualOne(
142301 WhereLoopBuilder *pBuilder,
142302 Bitmask mPrereq, /* Mask of tables that must be used. */
142303 Bitmask mUsable, /* Mask of usable tables */
142304 u16 mExclude, /* Exclude terms using these operators */
142305 sqlite3_index_info *pIdxInfo, /* Populated object for xBestIndex */
142306 u16 mNoOmit, /* Do not omit these constraints */
142307 int *pbIn /* OUT: True if plan uses an IN(...) op */
142308){
142309 WhereClause *pWC = pBuilder->pWC;
142310 struct sqlite3_index_constraint *pIdxCons;
142311 struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage;
142312 int i;
142313 int mxTerm;
142314 int rc = SQLITE_OK;
142315 WhereLoop *pNew = pBuilder->pNew;
142316 Parse *pParse = pBuilder->pWInfo->pParse;
142317 struct SrcList_item *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab];
142318 int nConstraint = pIdxInfo->nConstraint;
142319
142320 assert( (mUsable & mPrereq)==mPrereq );
142321 *pbIn = 0;
142322 pNew->prereq = mPrereq;
142323
142324 /* Set the usable flag on the subset of constraints identified by
142325 ** arguments mUsable and mExclude. */
142326 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
142327 for(i=0; i<nConstraint; i++, pIdxCons++){
142328 WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset];
142329 pIdxCons->usable = 0;
142330 if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight
142331 && (pTerm->eOperator & mExclude)==0
142332 ){
142333 pIdxCons->usable = 1;
142334 }
142335 }
142336
142337 /* Initialize the output fields of the sqlite3_index_info structure */
142338 memset(pUsage, 0, sizeof(pUsage[0])*nConstraint);
142339 assert( pIdxInfo->needToFreeIdxStr==0 );
142340 pIdxInfo->idxStr = 0;
142341 pIdxInfo->idxNum = 0;
142342 pIdxInfo->orderByConsumed = 0;
142343 pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
142344 pIdxInfo->estimatedRows = 25;
142345 pIdxInfo->idxFlags = 0;
142346 pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed;
142347
142348 /* Invoke the virtual table xBestIndex() method */
142349 rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo);
142350 if( rc ){
142351 if( rc==SQLITE_CONSTRAINT ){
142352 /* If the xBestIndex method returns SQLITE_CONSTRAINT, that means
142353 ** that the particular combination of parameters provided is unusable.
142354 ** Make no entries in the loop table.
142355 */
142356 WHERETRACE(0xffff, (" ^^^^--- non-viable plan rejected!\n"));
142357 return SQLITE_OK;
142358 }
142359 return rc;
142360 }
142361
142362 mxTerm = -1;
142363 assert( pNew->nLSlot>=nConstraint );
142364 for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0;
142365 pNew->u.vtab.omitMask = 0;
142366 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
142367 for(i=0; i<nConstraint; i++, pIdxCons++){
142368 int iTerm;
142369 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
142370 WhereTerm *pTerm;
142371 int j = pIdxCons->iTermOffset;
142372 if( iTerm>=nConstraint
142373 || j<0
142374 || j>=pWC->nTerm
142375 || pNew->aLTerm[iTerm]!=0
142376 || pIdxCons->usable==0
142377 ){
142378 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
142379 testcase( pIdxInfo->needToFreeIdxStr );
142380 return SQLITE_ERROR;
142381 }
142382 testcase( iTerm==nConstraint-1 );
142383 testcase( j==0 );
142384 testcase( j==pWC->nTerm-1 );
142385 pTerm = &pWC->a[j];
142386 pNew->prereq |= pTerm->prereqRight;
142387 assert( iTerm<pNew->nLSlot );
142388 pNew->aLTerm[iTerm] = pTerm;
142389 if( iTerm>mxTerm ) mxTerm = iTerm;
142390 testcase( iTerm==15 );
142391 testcase( iTerm==16 );
142392 if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm;
142393 if( (pTerm->eOperator & WO_IN)!=0 ){
142394 /* A virtual table that is constrained by an IN clause may not
142395 ** consume the ORDER BY clause because (1) the order of IN terms
142396 ** is not necessarily related to the order of output terms and
142397 ** (2) Multiple outputs from a single IN value will not merge
142398 ** together. */
142399 pIdxInfo->orderByConsumed = 0;
142400 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
142401 *pbIn = 1; assert( (mExclude & WO_IN)==0 );
142402 }
142403 }
142404 }
142405 pNew->u.vtab.omitMask &= ~mNoOmit;
142406
142407 pNew->nLTerm = mxTerm+1;
142408 for(i=0; i<=mxTerm; i++){
142409 if( pNew->aLTerm[i]==0 ){
142410 /* The non-zero argvIdx values must be contiguous. Raise an
142411 ** error if they are not */
142412 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
142413 testcase( pIdxInfo->needToFreeIdxStr );
142414 return SQLITE_ERROR;
142415 }
142416 }
142417 assert( pNew->nLTerm<=pNew->nLSlot );
142418 pNew->u.vtab.idxNum = pIdxInfo->idxNum;
142419 pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
142420 pIdxInfo->needToFreeIdxStr = 0;
142421 pNew->u.vtab.idxStr = pIdxInfo->idxStr;
142422 pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
142423 pIdxInfo->nOrderBy : 0);
142424 pNew->rSetup = 0;
142425 pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
142426 pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
142427
142428 /* Set the WHERE_ONEROW flag if the xBestIndex() method indicated
142429 ** that the scan will visit at most one row. Clear it otherwise. */
142430 if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){
142431 pNew->wsFlags |= WHERE_ONEROW;
142432 }else{
142433 pNew->wsFlags &= ~WHERE_ONEROW;
142434 }
142435 rc = whereLoopInsert(pBuilder, pNew);
142436 if( pNew->u.vtab.needFree ){
142437 sqlite3_free(pNew->u.vtab.idxStr);
142438 pNew->u.vtab.needFree = 0;
142439 }
142440 WHERETRACE(0xffff, (" bIn=%d prereqIn=%04llx prereqOut=%04llx\n",
142441 *pbIn, (sqlite3_uint64)mPrereq,
142442 (sqlite3_uint64)(pNew->prereq & ~mPrereq)));
142443
142444 return rc;
142445}
142446
142447/*
142448** If this function is invoked from within an xBestIndex() callback, it
142449** returns a pointer to a buffer containing the name of the collation
142450** sequence associated with element iCons of the sqlite3_index_info.aConstraint
142451** array. Or, if iCons is out of range or there is no active xBestIndex
142452** call, return NULL.
142453*/
142454SQLITE_API const char *sqlite3_vtab_collation(sqlite3_index_info *pIdxInfo, int iCons){
142455 HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
142456 const char *zRet = 0;
142457 if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
142458 CollSeq *pC = 0;
142459 int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset;
142460 Expr *pX = pHidden->pWC->a[iTerm].pExpr;
142461 if( pX->pLeft ){
142462 pC = sqlite3BinaryCompareCollSeq(pHidden->pParse, pX->pLeft, pX->pRight);
142463 }
142464 zRet = (pC ? pC->zName : sqlite3StrBINARY);
142465 }
142466 return zRet;
142467}
142468
142469/*
142470** Add all WhereLoop objects for a table of the join identified by
142471** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
142472**
142473** If there are no LEFT or CROSS JOIN joins in the query, both mPrereq and
142474** mUnusable are set to 0. Otherwise, mPrereq is a mask of all FROM clause
142475** entries that occur before the virtual table in the FROM clause and are
142476** separated from it by at least one LEFT or CROSS JOIN. Similarly, the
142477** mUnusable mask contains all FROM clause entries that occur after the
142478** virtual table and are separated from it by at least one LEFT or
142479** CROSS JOIN.
142480**
142481** For example, if the query were:
142482**
142483** ... FROM t1, t2 LEFT JOIN t3, t4, vt CROSS JOIN t5, t6;
142484**
142485** then mPrereq corresponds to (t1, t2) and mUnusable to (t5, t6).
142486**
142487** All the tables in mPrereq must be scanned before the current virtual
142488** table. So any terms for which all prerequisites are satisfied by
142489** mPrereq may be specified as "usable" in all calls to xBestIndex.
142490** Conversely, all tables in mUnusable must be scanned after the current
142491** virtual table, so any terms for which the prerequisites overlap with
142492** mUnusable should always be configured as "not-usable" for xBestIndex.
142493*/
142494static int whereLoopAddVirtual(
142495 WhereLoopBuilder *pBuilder, /* WHERE clause information */
142496 Bitmask mPrereq, /* Tables that must be scanned before this one */
142497 Bitmask mUnusable /* Tables that must be scanned after this one */
142498){
142499 int rc = SQLITE_OK; /* Return code */
142500 WhereInfo *pWInfo; /* WHERE analysis context */
142501 Parse *pParse; /* The parsing context */
142502 WhereClause *pWC; /* The WHERE clause */
142503 struct SrcList_item *pSrc; /* The FROM clause term to search */
142504 sqlite3_index_info *p; /* Object to pass to xBestIndex() */
142505 int nConstraint; /* Number of constraints in p */
142506 int bIn; /* True if plan uses IN(...) operator */
142507 WhereLoop *pNew;
142508 Bitmask mBest; /* Tables used by best possible plan */
142509 u16 mNoOmit;
142510
142511 assert( (mPrereq & mUnusable)==0 );
142512 pWInfo = pBuilder->pWInfo;
142513 pParse = pWInfo->pParse;
142514 pWC = pBuilder->pWC;
142515 pNew = pBuilder->pNew;
142516 pSrc = &pWInfo->pTabList->a[pNew->iTab];
142517 assert( IsVirtual(pSrc->pTab) );
142518 p = allocateIndexInfo(pParse, pWC, mUnusable, pSrc, pBuilder->pOrderBy,
142519 &mNoOmit);
142520 if( p==0 ) return SQLITE_NOMEM_BKPT;
142521 pNew->rSetup = 0;
142522 pNew->wsFlags = WHERE_VIRTUALTABLE;
142523 pNew->nLTerm = 0;
142524 pNew->u.vtab.needFree = 0;
142525 nConstraint = p->nConstraint;
142526 if( whereLoopResize(pParse->db, pNew, nConstraint) ){
142527 sqlite3DbFree(pParse->db, p);
142528 return SQLITE_NOMEM_BKPT;
142529 }
142530
142531 /* First call xBestIndex() with all constraints usable. */
142532 WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
142533 WHERETRACE(0x40, (" VirtualOne: all usable\n"));
142534 rc = whereLoopAddVirtualOne(pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn);
142535
142536 /* If the call to xBestIndex() with all terms enabled produced a plan
142537 ** that does not require any source tables (IOW: a plan with mBest==0),
142538 ** then there is no point in making any further calls to xBestIndex()
142539 ** since they will all return the same result (if the xBestIndex()
142540 ** implementation is sane). */
142541 if( rc==SQLITE_OK && (mBest = (pNew->prereq & ~mPrereq))!=0 ){
142542 int seenZero = 0; /* True if a plan with no prereqs seen */
142543 int seenZeroNoIN = 0; /* Plan with no prereqs and no IN(...) seen */
142544 Bitmask mPrev = 0;
142545 Bitmask mBestNoIn = 0;
142546
142547 /* If the plan produced by the earlier call uses an IN(...) term, call
142548 ** xBestIndex again, this time with IN(...) terms disabled. */
142549 if( bIn ){
142550 WHERETRACE(0x40, (" VirtualOne: all usable w/o IN\n"));
142551 rc = whereLoopAddVirtualOne(
142552 pBuilder, mPrereq, ALLBITS, WO_IN, p, mNoOmit, &bIn);
142553 assert( bIn==0 );
142554 mBestNoIn = pNew->prereq & ~mPrereq;
142555 if( mBestNoIn==0 ){
142556 seenZero = 1;
142557 seenZeroNoIN = 1;
142558 }
142559 }
142560
142561 /* Call xBestIndex once for each distinct value of (prereqRight & ~mPrereq)
142562 ** in the set of terms that apply to the current virtual table. */
142563 while( rc==SQLITE_OK ){
142564 int i;
142565 Bitmask mNext = ALLBITS;
142566 assert( mNext>0 );
142567 for(i=0; i<nConstraint; i++){
142568 Bitmask mThis = (
142569 pWC->a[p->aConstraint[i].iTermOffset].prereqRight & ~mPrereq
142570 );
142571 if( mThis>mPrev && mThis<mNext ) mNext = mThis;
142572 }
142573 mPrev = mNext;
142574 if( mNext==ALLBITS ) break;
142575 if( mNext==mBest || mNext==mBestNoIn ) continue;
142576 WHERETRACE(0x40, (" VirtualOne: mPrev=%04llx mNext=%04llx\n",
142577 (sqlite3_uint64)mPrev, (sqlite3_uint64)mNext));
142578 rc = whereLoopAddVirtualOne(
142579 pBuilder, mPrereq, mNext|mPrereq, 0, p, mNoOmit, &bIn);
142580 if( pNew->prereq==mPrereq ){
142581 seenZero = 1;
142582 if( bIn==0 ) seenZeroNoIN = 1;
142583 }
142584 }
142585
142586 /* If the calls to xBestIndex() in the above loop did not find a plan
142587 ** that requires no source tables at all (i.e. one guaranteed to be
142588 ** usable), make a call here with all source tables disabled */
142589 if( rc==SQLITE_OK && seenZero==0 ){
142590 WHERETRACE(0x40, (" VirtualOne: all disabled\n"));
142591 rc = whereLoopAddVirtualOne(
142592 pBuilder, mPrereq, mPrereq, 0, p, mNoOmit, &bIn);
142593 if( bIn==0 ) seenZeroNoIN = 1;
142594 }
142595
142596 /* If the calls to xBestIndex() have so far failed to find a plan
142597 ** that requires no source tables at all and does not use an IN(...)
142598 ** operator, make a final call to obtain one here. */
142599 if( rc==SQLITE_OK && seenZeroNoIN==0 ){
142600 WHERETRACE(0x40, (" VirtualOne: all disabled and w/o IN\n"));
142601 rc = whereLoopAddVirtualOne(
142602 pBuilder, mPrereq, mPrereq, WO_IN, p, mNoOmit, &bIn);
142603 }
142604 }
142605
142606 if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr);
142607 sqlite3DbFreeNN(pParse->db, p);
142608 WHERETRACE(0x800, ("END %s.addVirtual(), rc=%d\n", pSrc->pTab->zName, rc));
142609 return rc;
142610}
142611#endif /* SQLITE_OMIT_VIRTUALTABLE */
142612
142613/*
142614** Add WhereLoop entries to handle OR terms. This works for either
142615** btrees or virtual tables.
142616*/
142617static int whereLoopAddOr(
142618 WhereLoopBuilder *pBuilder,
142619 Bitmask mPrereq,
142620 Bitmask mUnusable
142621){
142622 WhereInfo *pWInfo = pBuilder->pWInfo;
142623 WhereClause *pWC;
142624 WhereLoop *pNew;
142625 WhereTerm *pTerm, *pWCEnd;
142626 int rc = SQLITE_OK;
142627 int iCur;
142628 WhereClause tempWC;
142629 WhereLoopBuilder sSubBuild;
142630 WhereOrSet sSum, sCur;
142631 struct SrcList_item *pItem;
142632
142633 pWC = pBuilder->pWC;
142634 pWCEnd = pWC->a + pWC->nTerm;
142635 pNew = pBuilder->pNew;
142636 memset(&sSum, 0, sizeof(sSum));
142637 pItem = pWInfo->pTabList->a + pNew->iTab;
142638 iCur = pItem->iCursor;
142639
142640 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
142641 if( (pTerm->eOperator & WO_OR)!=0
142642 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
142643 ){
142644 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
142645 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
142646 WhereTerm *pOrTerm;
142647 int once = 1;
142648 int i, j;
142649
142650 sSubBuild = *pBuilder;
142651 sSubBuild.pOrderBy = 0;
142652 sSubBuild.pOrSet = &sCur;
142653
142654 WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm));
142655 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
142656 if( (pOrTerm->eOperator & WO_AND)!=0 ){
142657 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
142658 }else if( pOrTerm->leftCursor==iCur ){
142659 tempWC.pWInfo = pWC->pWInfo;
142660 tempWC.pOuter = pWC;
142661 tempWC.op = TK_AND;
142662 tempWC.nTerm = 1;
142663 tempWC.a = pOrTerm;
142664 sSubBuild.pWC = &tempWC;
142665 }else{
142666 continue;
142667 }
142668 sCur.n = 0;
142669#ifdef WHERETRACE_ENABLED
142670 WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n",
142671 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
142672 if( sqlite3WhereTrace & 0x400 ){
142673 sqlite3WhereClausePrint(sSubBuild.pWC);
142674 }
142675#endif
142676#ifndef SQLITE_OMIT_VIRTUALTABLE
142677 if( IsVirtual(pItem->pTab) ){
142678 rc = whereLoopAddVirtual(&sSubBuild, mPrereq, mUnusable);
142679 }else
142680#endif
142681 {
142682 rc = whereLoopAddBtree(&sSubBuild, mPrereq);
142683 }
142684 if( rc==SQLITE_OK ){
142685 rc = whereLoopAddOr(&sSubBuild, mPrereq, mUnusable);
142686 }
142687 assert( rc==SQLITE_OK || sCur.n==0 );
142688 if( sCur.n==0 ){
142689 sSum.n = 0;
142690 break;
142691 }else if( once ){
142692 whereOrMove(&sSum, &sCur);
142693 once = 0;
142694 }else{
142695 WhereOrSet sPrev;
142696 whereOrMove(&sPrev, &sSum);
142697 sSum.n = 0;
142698 for(i=0; i<sPrev.n; i++){
142699 for(j=0; j<sCur.n; j++){
142700 whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq,
142701 sqlite3LogEstAdd(sPrev.a[i].rRun, sCur.a[j].rRun),
142702 sqlite3LogEstAdd(sPrev.a[i].nOut, sCur.a[j].nOut));
142703 }
142704 }
142705 }
142706 }
142707 pNew->nLTerm = 1;
142708 pNew->aLTerm[0] = pTerm;
142709 pNew->wsFlags = WHERE_MULTI_OR;
142710 pNew->rSetup = 0;
142711 pNew->iSortIdx = 0;
142712 memset(&pNew->u, 0, sizeof(pNew->u));
142713 for(i=0; rc==SQLITE_OK && i<sSum.n; i++){
142714 /* TUNING: Currently sSum.a[i].rRun is set to the sum of the costs
142715 ** of all sub-scans required by the OR-scan. However, due to rounding
142716 ** errors, it may be that the cost of the OR-scan is equal to its
142717 ** most expensive sub-scan. Add the smallest possible penalty
142718 ** (equivalent to multiplying the cost by 1.07) to ensure that
142719 ** this does not happen. Otherwise, for WHERE clauses such as the
142720 ** following where there is an index on "y":
142721 **
142722 ** WHERE likelihood(x=?, 0.99) OR y=?
142723 **
142724 ** the planner may elect to "OR" together a full-table scan and an
142725 ** index lookup. And other similarly odd results. */
142726 pNew->rRun = sSum.a[i].rRun + 1;
142727 pNew->nOut = sSum.a[i].nOut;
142728 pNew->prereq = sSum.a[i].prereq;
142729 rc = whereLoopInsert(pBuilder, pNew);
142730 }
142731 WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm));
142732 }
142733 }
142734 return rc;
142735}
142736
142737/*
142738** Add all WhereLoop objects for all tables
142739*/
142740static int whereLoopAddAll(WhereLoopBuilder *pBuilder){
142741 WhereInfo *pWInfo = pBuilder->pWInfo;
142742 Bitmask mPrereq = 0;
142743 Bitmask mPrior = 0;
142744 int iTab;
142745 SrcList *pTabList = pWInfo->pTabList;
142746 struct SrcList_item *pItem;
142747 struct SrcList_item *pEnd = &pTabList->a[pWInfo->nLevel];
142748 sqlite3 *db = pWInfo->pParse->db;
142749 int rc = SQLITE_OK;
142750 WhereLoop *pNew;
142751 u8 priorJointype = 0;
142752
142753 /* Loop over the tables in the join, from left to right */
142754 pNew = pBuilder->pNew;
142755 whereLoopInit(pNew);
142756 pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT;
142757 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
142758 Bitmask mUnusable = 0;
142759 pNew->iTab = iTab;
142760 pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR;
142761 pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
142762 if( ((pItem->fg.jointype|priorJointype) & (JT_LEFT|JT_CROSS))!=0 ){
142763 /* This condition is true when pItem is the FROM clause term on the
142764 ** right-hand-side of a LEFT or CROSS JOIN. */
142765 mPrereq = mPrior;
142766 }
142767 priorJointype = pItem->fg.jointype;
142768#ifndef SQLITE_OMIT_VIRTUALTABLE
142769 if( IsVirtual(pItem->pTab) ){
142770 struct SrcList_item *p;
142771 for(p=&pItem[1]; p<pEnd; p++){
142772 if( mUnusable || (p->fg.jointype & (JT_LEFT|JT_CROSS)) ){
142773 mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor);
142774 }
142775 }
142776 rc = whereLoopAddVirtual(pBuilder, mPrereq, mUnusable);
142777 }else
142778#endif /* SQLITE_OMIT_VIRTUALTABLE */
142779 {
142780 rc = whereLoopAddBtree(pBuilder, mPrereq);
142781 }
142782 if( rc==SQLITE_OK && pBuilder->pWC->hasOr ){
142783 rc = whereLoopAddOr(pBuilder, mPrereq, mUnusable);
142784 }
142785 mPrior |= pNew->maskSelf;
142786 if( rc || db->mallocFailed ){
142787 if( rc==SQLITE_DONE ){
142788 /* We hit the query planner search limit set by iPlanLimit */
142789 sqlite3_log(SQLITE_WARNING, "abbreviated query algorithm search");
142790 rc = SQLITE_OK;
142791 }else{
142792 break;
142793 }
142794 }
142795 }
142796
142797 whereLoopClear(db, pNew);
142798 return rc;
142799}
142800
142801/*
142802** Examine a WherePath (with the addition of the extra WhereLoop of the 6th
142803** parameters) to see if it outputs rows in the requested ORDER BY
142804** (or GROUP BY) without requiring a separate sort operation. Return N:
142805**
142806** N>0: N terms of the ORDER BY clause are satisfied
142807** N==0: No terms of the ORDER BY clause are satisfied
142808** N<0: Unknown yet how many terms of ORDER BY might be satisfied.
142809**
142810** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as
142811** strict. With GROUP BY and DISTINCT the only requirement is that
142812** equivalent rows appear immediately adjacent to one another. GROUP BY
142813** and DISTINCT do not require rows to appear in any particular order as long
142814** as equivalent rows are grouped together. Thus for GROUP BY and DISTINCT
142815** the pOrderBy terms can be matched in any order. With ORDER BY, the
142816** pOrderBy terms must be matched in strict left-to-right order.
142817*/
142818static i8 wherePathSatisfiesOrderBy(
142819 WhereInfo *pWInfo, /* The WHERE clause */
142820 ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */
142821 WherePath *pPath, /* The WherePath to check */
142822 u16 wctrlFlags, /* WHERE_GROUPBY or _DISTINCTBY or _ORDERBY_LIMIT */
142823 u16 nLoop, /* Number of entries in pPath->aLoop[] */
142824 WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */
142825 Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */
142826){
142827 u8 revSet; /* True if rev is known */
142828 u8 rev; /* Composite sort order */
142829 u8 revIdx; /* Index sort order */
142830 u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */
142831 u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */
142832 u8 isMatch; /* iColumn matches a term of the ORDER BY clause */
142833 u16 eqOpMask; /* Allowed equality operators */
142834 u16 nKeyCol; /* Number of key columns in pIndex */
142835 u16 nColumn; /* Total number of ordered columns in the index */
142836 u16 nOrderBy; /* Number terms in the ORDER BY clause */
142837 int iLoop; /* Index of WhereLoop in pPath being processed */
142838 int i, j; /* Loop counters */
142839 int iCur; /* Cursor number for current WhereLoop */
142840 int iColumn; /* A column number within table iCur */
142841 WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */
142842 WhereTerm *pTerm; /* A single term of the WHERE clause */
142843 Expr *pOBExpr; /* An expression from the ORDER BY clause */
142844 CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */
142845 Index *pIndex; /* The index associated with pLoop */
142846 sqlite3 *db = pWInfo->pParse->db; /* Database connection */
142847 Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */
142848 Bitmask obDone; /* Mask of all ORDER BY terms */
142849 Bitmask orderDistinctMask; /* Mask of all well-ordered loops */
142850 Bitmask ready; /* Mask of inner loops */
142851
142852 /*
142853 ** We say the WhereLoop is "one-row" if it generates no more than one
142854 ** row of output. A WhereLoop is one-row if all of the following are true:
142855 ** (a) All index columns match with WHERE_COLUMN_EQ.
142856 ** (b) The index is unique
142857 ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
142858 ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
142859 **
142860 ** We say the WhereLoop is "order-distinct" if the set of columns from
142861 ** that WhereLoop that are in the ORDER BY clause are different for every
142862 ** row of the WhereLoop. Every one-row WhereLoop is automatically
142863 ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause
142864 ** is not order-distinct. To be order-distinct is not quite the same as being
142865 ** UNIQUE since a UNIQUE column or index can have multiple rows that
142866 ** are NULL and NULL values are equivalent for the purpose of order-distinct.
142867 ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
142868 **
142869 ** The rowid for a table is always UNIQUE and NOT NULL so whenever the
142870 ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is
142871 ** automatically order-distinct.
142872 */
142873
142874 assert( pOrderBy!=0 );
142875 if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0;
142876
142877 nOrderBy = pOrderBy->nExpr;
142878 testcase( nOrderBy==BMS-1 );
142879 if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */
142880 isOrderDistinct = 1;
142881 obDone = MASKBIT(nOrderBy)-1;
142882 orderDistinctMask = 0;
142883 ready = 0;
142884 eqOpMask = WO_EQ | WO_IS | WO_ISNULL;
142885 if( wctrlFlags & WHERE_ORDERBY_LIMIT ) eqOpMask |= WO_IN;
142886 for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){
142887 if( iLoop>0 ) ready |= pLoop->maskSelf;
142888 if( iLoop<nLoop ){
142889 pLoop = pPath->aLoop[iLoop];
142890 if( wctrlFlags & WHERE_ORDERBY_LIMIT ) continue;
142891 }else{
142892 pLoop = pLast;
142893 }
142894 if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){
142895 if( pLoop->u.vtab.isOrdered ) obSat = obDone;
142896 break;
142897 }else{
142898 pLoop->u.btree.nIdxCol = 0;
142899 }
142900 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
142901
142902 /* Mark off any ORDER BY term X that is a column in the table of
142903 ** the current loop for which there is term in the WHERE
142904 ** clause of the form X IS NULL or X=? that reference only outer
142905 ** loops.
142906 */
142907 for(i=0; i<nOrderBy; i++){
142908 if( MASKBIT(i) & obSat ) continue;
142909 pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
142910 if( pOBExpr->op!=TK_COLUMN ) continue;
142911 if( pOBExpr->iTable!=iCur ) continue;
142912 pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
142913 ~ready, eqOpMask, 0);
142914 if( pTerm==0 ) continue;
142915 if( pTerm->eOperator==WO_IN ){
142916 /* IN terms are only valid for sorting in the ORDER BY LIMIT
142917 ** optimization, and then only if they are actually used
142918 ** by the query plan */
142919 assert( wctrlFlags & WHERE_ORDERBY_LIMIT );
142920 for(j=0; j<pLoop->nLTerm && pTerm!=pLoop->aLTerm[j]; j++){}
142921 if( j>=pLoop->nLTerm ) continue;
142922 }
142923 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
142924 if( sqlite3ExprCollSeqMatch(pWInfo->pParse,
142925 pOrderBy->a[i].pExpr, pTerm->pExpr)==0 ){
142926 continue;
142927 }
142928 testcase( pTerm->pExpr->op==TK_IS );
142929 }
142930 obSat |= MASKBIT(i);
142931 }
142932
142933 if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
142934 if( pLoop->wsFlags & WHERE_IPK ){
142935 pIndex = 0;
142936 nKeyCol = 0;
142937 nColumn = 1;
142938 }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
142939 return 0;
142940 }else{
142941 nKeyCol = pIndex->nKeyCol;
142942 nColumn = pIndex->nColumn;
142943 assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
142944 assert( pIndex->aiColumn[nColumn-1]==XN_ROWID
142945 || !HasRowid(pIndex->pTable));
142946 isOrderDistinct = IsUniqueIndex(pIndex);
142947 }
142948
142949 /* Loop through all columns of the index and deal with the ones
142950 ** that are not constrained by == or IN.
142951 */
142952 rev = revSet = 0;
142953 distinctColumns = 0;
142954 for(j=0; j<nColumn; j++){
142955 u8 bOnce = 1; /* True to run the ORDER BY search loop */
142956
142957 assert( j>=pLoop->u.btree.nEq
142958 || (pLoop->aLTerm[j]==0)==(j<pLoop->nSkip)
142959 );
142960 if( j<pLoop->u.btree.nEq && j>=pLoop->nSkip ){
142961 u16 eOp = pLoop->aLTerm[j]->eOperator;
142962
142963 /* Skip over == and IS and ISNULL terms. (Also skip IN terms when
142964 ** doing WHERE_ORDERBY_LIMIT processing).
142965 **
142966 ** If the current term is a column of an ((?,?) IN (SELECT...))
142967 ** expression for which the SELECT returns more than one column,
142968 ** check that it is the only column used by this loop. Otherwise,
142969 ** if it is one of two or more, none of the columns can be
142970 ** considered to match an ORDER BY term. */
142971 if( (eOp & eqOpMask)!=0 ){
142972 if( eOp & WO_ISNULL ){
142973 testcase( isOrderDistinct );
142974 isOrderDistinct = 0;
142975 }
142976 continue;
142977 }else if( ALWAYS(eOp & WO_IN) ){
142978 /* ALWAYS() justification: eOp is an equality operator due to the
142979 ** j<pLoop->u.btree.nEq constraint above. Any equality other
142980 ** than WO_IN is captured by the previous "if". So this one
142981 ** always has to be WO_IN. */
142982 Expr *pX = pLoop->aLTerm[j]->pExpr;
142983 for(i=j+1; i<pLoop->u.btree.nEq; i++){
142984 if( pLoop->aLTerm[i]->pExpr==pX ){
142985 assert( (pLoop->aLTerm[i]->eOperator & WO_IN) );
142986 bOnce = 0;
142987 break;
142988 }
142989 }
142990 }
142991 }
142992
142993 /* Get the column number in the table (iColumn) and sort order
142994 ** (revIdx) for the j-th column of the index.
142995 */
142996 if( pIndex ){
142997 iColumn = pIndex->aiColumn[j];
142998 revIdx = pIndex->aSortOrder[j];
142999 if( iColumn==pIndex->pTable->iPKey ) iColumn = XN_ROWID;
143000 }else{
143001 iColumn = XN_ROWID;
143002 revIdx = 0;
143003 }
143004
143005 /* An unconstrained column that might be NULL means that this
143006 ** WhereLoop is not well-ordered
143007 */
143008 if( isOrderDistinct
143009 && iColumn>=0
143010 && j>=pLoop->u.btree.nEq
143011 && pIndex->pTable->aCol[iColumn].notNull==0
143012 ){
143013 isOrderDistinct = 0;
143014 }
143015
143016 /* Find the ORDER BY term that corresponds to the j-th column
143017 ** of the index and mark that ORDER BY term off
143018 */
143019 isMatch = 0;
143020 for(i=0; bOnce && i<nOrderBy; i++){
143021 if( MASKBIT(i) & obSat ) continue;
143022 pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
143023 testcase( wctrlFlags & WHERE_GROUPBY );
143024 testcase( wctrlFlags & WHERE_DISTINCTBY );
143025 if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0;
143026 if( iColumn>=XN_ROWID ){
143027 if( pOBExpr->op!=TK_COLUMN ) continue;
143028 if( pOBExpr->iTable!=iCur ) continue;
143029 if( pOBExpr->iColumn!=iColumn ) continue;
143030 }else{
143031 Expr *pIdxExpr = pIndex->aColExpr->a[j].pExpr;
143032 if( sqlite3ExprCompareSkip(pOBExpr, pIdxExpr, iCur) ){
143033 continue;
143034 }
143035 }
143036 if( iColumn!=XN_ROWID ){
143037 pColl = sqlite3ExprNNCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
143038 if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
143039 }
143040 pLoop->u.btree.nIdxCol = j+1;
143041 isMatch = 1;
143042 break;
143043 }
143044 if( isMatch && (wctrlFlags & WHERE_GROUPBY)==0 ){
143045 /* Make sure the sort order is compatible in an ORDER BY clause.
143046 ** Sort order is irrelevant for a GROUP BY clause. */
143047 if( revSet ){
143048 if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) isMatch = 0;
143049 }else{
143050 rev = revIdx ^ pOrderBy->a[i].sortOrder;
143051 if( rev ) *pRevMask |= MASKBIT(iLoop);
143052 revSet = 1;
143053 }
143054 }
143055 if( isMatch ){
143056 if( iColumn==XN_ROWID ){
143057 testcase( distinctColumns==0 );
143058 distinctColumns = 1;
143059 }
143060 obSat |= MASKBIT(i);
143061 }else{
143062 /* No match found */
143063 if( j==0 || j<nKeyCol ){
143064 testcase( isOrderDistinct!=0 );
143065 isOrderDistinct = 0;
143066 }
143067 break;
143068 }
143069 } /* end Loop over all index columns */
143070 if( distinctColumns ){
143071 testcase( isOrderDistinct==0 );
143072 isOrderDistinct = 1;
143073 }
143074 } /* end-if not one-row */
143075
143076 /* Mark off any other ORDER BY terms that reference pLoop */
143077 if( isOrderDistinct ){
143078 orderDistinctMask |= pLoop->maskSelf;
143079 for(i=0; i<nOrderBy; i++){
143080 Expr *p;
143081 Bitmask mTerm;
143082 if( MASKBIT(i) & obSat ) continue;
143083 p = pOrderBy->a[i].pExpr;
143084 mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
143085 if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue;
143086 if( (mTerm&~orderDistinctMask)==0 ){
143087 obSat |= MASKBIT(i);
143088 }
143089 }
143090 }
143091 } /* End the loop over all WhereLoops from outer-most down to inner-most */
143092 if( obSat==obDone ) return (i8)nOrderBy;
143093 if( !isOrderDistinct ){
143094 for(i=nOrderBy-1; i>0; i--){
143095 Bitmask m = MASKBIT(i) - 1;
143096 if( (obSat&m)==m ) return i;
143097 }
143098 return 0;
143099 }
143100 return -1;
143101}
143102
143103
143104/*
143105** If the WHERE_GROUPBY flag is set in the mask passed to sqlite3WhereBegin(),
143106** the planner assumes that the specified pOrderBy list is actually a GROUP
143107** BY clause - and so any order that groups rows as required satisfies the
143108** request.
143109**
143110** Normally, in this case it is not possible for the caller to determine
143111** whether or not the rows are really being delivered in sorted order, or
143112** just in some other order that provides the required grouping. However,
143113** if the WHERE_SORTBYGROUP flag is also passed to sqlite3WhereBegin(), then
143114** this function may be called on the returned WhereInfo object. It returns
143115** true if the rows really will be sorted in the specified order, or false
143116** otherwise.
143117**
143118** For example, assuming:
143119**
143120** CREATE INDEX i1 ON t1(x, Y);
143121**
143122** then
143123**
143124** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1
143125** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0
143126*/
143127SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo *pWInfo){
143128 assert( pWInfo->wctrlFlags & WHERE_GROUPBY );
143129 assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP );
143130 return pWInfo->sorted;
143131}
143132
143133#ifdef WHERETRACE_ENABLED
143134/* For debugging use only: */
143135static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){
143136 static char zName[65];
143137 int i;
143138 for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
143139 if( pLast ) zName[i++] = pLast->cId;
143140 zName[i] = 0;
143141 return zName;
143142}
143143#endif
143144
143145/*
143146** Return the cost of sorting nRow rows, assuming that the keys have
143147** nOrderby columns and that the first nSorted columns are already in
143148** order.
143149*/
143150static LogEst whereSortingCost(
143151 WhereInfo *pWInfo,
143152 LogEst nRow,
143153 int nOrderBy,
143154 int nSorted
143155){
143156 /* TUNING: Estimated cost of a full external sort, where N is
143157 ** the number of rows to sort is:
143158 **
143159 ** cost = (3.0 * N * log(N)).
143160 **
143161 ** Or, if the order-by clause has X terms but only the last Y
143162 ** terms are out of order, then block-sorting will reduce the
143163 ** sorting cost to:
143164 **
143165 ** cost = (3.0 * N * log(N)) * (Y/X)
143166 **
143167 ** The (Y/X) term is implemented using stack variable rScale
143168 ** below. */
143169 LogEst rScale, rSortCost;
143170 assert( nOrderBy>0 && 66==sqlite3LogEst(100) );
143171 rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
143172 rSortCost = nRow + rScale + 16;
143173
143174 /* Multiple by log(M) where M is the number of output rows.
143175 ** Use the LIMIT for M if it is smaller */
143176 if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 && pWInfo->iLimit<nRow ){
143177 nRow = pWInfo->iLimit;
143178 }
143179 rSortCost += estLog(nRow);
143180 return rSortCost;
143181}
143182
143183/*
143184** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
143185** attempts to find the lowest cost path that visits each WhereLoop
143186** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
143187**
143188** Assume that the total number of output rows that will need to be sorted
143189** will be nRowEst (in the 10*log2 representation). Or, ignore sorting
143190** costs if nRowEst==0.
143191**
143192** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation
143193** error occurs.
143194*/
143195static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
143196 int mxChoice; /* Maximum number of simultaneous paths tracked */
143197 int nLoop; /* Number of terms in the join */
143198 Parse *pParse; /* Parsing context */
143199 sqlite3 *db; /* The database connection */
143200 int iLoop; /* Loop counter over the terms of the join */
143201 int ii, jj; /* Loop counters */
143202 int mxI = 0; /* Index of next entry to replace */
143203 int nOrderBy; /* Number of ORDER BY clause terms */
143204 LogEst mxCost = 0; /* Maximum cost of a set of paths */
143205 LogEst mxUnsorted = 0; /* Maximum unsorted cost of a set of path */
143206 int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */
143207 WherePath *aFrom; /* All nFrom paths at the previous level */
143208 WherePath *aTo; /* The nTo best paths at the current level */
143209 WherePath *pFrom; /* An element of aFrom[] that we are working on */
143210 WherePath *pTo; /* An element of aTo[] that we are working on */
143211 WhereLoop *pWLoop; /* One of the WhereLoop objects */
143212 WhereLoop **pX; /* Used to divy up the pSpace memory */
143213 LogEst *aSortCost = 0; /* Sorting and partial sorting costs */
143214 char *pSpace; /* Temporary memory used by this routine */
143215 int nSpace; /* Bytes of space allocated at pSpace */
143216
143217 pParse = pWInfo->pParse;
143218 db = pParse->db;
143219 nLoop = pWInfo->nLevel;
143220 /* TUNING: For simple queries, only the best path is tracked.
143221 ** For 2-way joins, the 5 best paths are followed.
143222 ** For joins of 3 or more tables, track the 10 best paths */
143223 mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10);
143224 assert( nLoop<=pWInfo->pTabList->nSrc );
143225 WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d)\n", nRowEst));
143226
143227 /* If nRowEst is zero and there is an ORDER BY clause, ignore it. In this
143228 ** case the purpose of this call is to estimate the number of rows returned
143229 ** by the overall query. Once this estimate has been obtained, the caller
143230 ** will invoke this function a second time, passing the estimate as the
143231 ** nRowEst parameter. */
143232 if( pWInfo->pOrderBy==0 || nRowEst==0 ){
143233 nOrderBy = 0;
143234 }else{
143235 nOrderBy = pWInfo->pOrderBy->nExpr;
143236 }
143237
143238 /* Allocate and initialize space for aTo, aFrom and aSortCost[] */
143239 nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
143240 nSpace += sizeof(LogEst) * nOrderBy;
143241 pSpace = sqlite3DbMallocRawNN(db, nSpace);
143242 if( pSpace==0 ) return SQLITE_NOMEM_BKPT;
143243 aTo = (WherePath*)pSpace;
143244 aFrom = aTo+mxChoice;
143245 memset(aFrom, 0, sizeof(aFrom[0]));
143246 pX = (WhereLoop**)(aFrom+mxChoice);
143247 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
143248 pFrom->aLoop = pX;
143249 }
143250 if( nOrderBy ){
143251 /* If there is an ORDER BY clause and it is not being ignored, set up
143252 ** space for the aSortCost[] array. Each element of the aSortCost array
143253 ** is either zero - meaning it has not yet been initialized - or the
143254 ** cost of sorting nRowEst rows of data where the first X terms of
143255 ** the ORDER BY clause are already in order, where X is the array
143256 ** index. */
143257 aSortCost = (LogEst*)pX;
143258 memset(aSortCost, 0, sizeof(LogEst) * nOrderBy);
143259 }
143260 assert( aSortCost==0 || &pSpace[nSpace]==(char*)&aSortCost[nOrderBy] );
143261 assert( aSortCost!=0 || &pSpace[nSpace]==(char*)pX );
143262
143263 /* Seed the search with a single WherePath containing zero WhereLoops.
143264 **
143265 ** TUNING: Do not let the number of iterations go above 28. If the cost
143266 ** of computing an automatic index is not paid back within the first 28
143267 ** rows, then do not use the automatic index. */
143268 aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) );
143269 nFrom = 1;
143270 assert( aFrom[0].isOrdered==0 );
143271 if( nOrderBy ){
143272 /* If nLoop is zero, then there are no FROM terms in the query. Since
143273 ** in this case the query may return a maximum of one row, the results
143274 ** are already in the requested order. Set isOrdered to nOrderBy to
143275 ** indicate this. Or, if nLoop is greater than zero, set isOrdered to
143276 ** -1, indicating that the result set may or may not be ordered,
143277 ** depending on the loops added to the current plan. */
143278 aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
143279 }
143280
143281 /* Compute successively longer WherePaths using the previous generation
143282 ** of WherePaths as the basis for the next. Keep track of the mxChoice
143283 ** best paths at each generation */
143284 for(iLoop=0; iLoop<nLoop; iLoop++){
143285 nTo = 0;
143286 for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
143287 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
143288 LogEst nOut; /* Rows visited by (pFrom+pWLoop) */
143289 LogEst rCost; /* Cost of path (pFrom+pWLoop) */
143290 LogEst rUnsorted; /* Unsorted cost of (pFrom+pWLoop) */
143291 i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */
143292 Bitmask maskNew; /* Mask of src visited by (..) */
143293 Bitmask revMask = 0; /* Mask of rev-order loops for (..) */
143294
143295 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
143296 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
143297 if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){
143298 /* Do not use an automatic index if the this loop is expected
143299 ** to run less than 1.25 times. It is tempting to also exclude
143300 ** automatic index usage on an outer loop, but sometimes an automatic
143301 ** index is useful in the outer loop of a correlated subquery. */
143302 assert( 10==sqlite3LogEst(2) );
143303 continue;
143304 }
143305
143306 /* At this point, pWLoop is a candidate to be the next loop.
143307 ** Compute its cost */
143308 rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
143309 rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
143310 nOut = pFrom->nRow + pWLoop->nOut;
143311 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
143312 if( isOrdered<0 ){
143313 isOrdered = wherePathSatisfiesOrderBy(pWInfo,
143314 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
143315 iLoop, pWLoop, &revMask);
143316 }else{
143317 revMask = pFrom->revLoop;
143318 }
143319 if( isOrdered>=0 && isOrdered<nOrderBy ){
143320 if( aSortCost[isOrdered]==0 ){
143321 aSortCost[isOrdered] = whereSortingCost(
143322 pWInfo, nRowEst, nOrderBy, isOrdered
143323 );
143324 }
143325 /* TUNING: Add a small extra penalty (5) to sorting as an
143326 ** extra encouragment to the query planner to select a plan
143327 ** where the rows emerge in the correct order without any sorting
143328 ** required. */
143329 rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]) + 5;
143330
143331 WHERETRACE(0x002,
143332 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
143333 aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
143334 rUnsorted, rCost));
143335 }else{
143336 rCost = rUnsorted;
143337 rUnsorted -= 2; /* TUNING: Slight bias in favor of no-sort plans */
143338 }
143339
143340 /* Check to see if pWLoop should be added to the set of
143341 ** mxChoice best-so-far paths.
143342 **
143343 ** First look for an existing path among best-so-far paths
143344 ** that covers the same set of loops and has the same isOrdered
143345 ** setting as the current path candidate.
143346 **
143347 ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
143348 ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
143349 ** of legal values for isOrdered, -1..64.
143350 */
143351 for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){
143352 if( pTo->maskLoop==maskNew
143353 && ((pTo->isOrdered^isOrdered)&0x80)==0
143354 ){
143355 testcase( jj==nTo-1 );
143356 break;
143357 }
143358 }
143359 if( jj>=nTo ){
143360 /* None of the existing best-so-far paths match the candidate. */
143361 if( nTo>=mxChoice
143362 && (rCost>mxCost || (rCost==mxCost && rUnsorted>=mxUnsorted))
143363 ){
143364 /* The current candidate is no better than any of the mxChoice
143365 ** paths currently in the best-so-far buffer. So discard
143366 ** this candidate as not viable. */
143367#ifdef WHERETRACE_ENABLED /* 0x4 */
143368 if( sqlite3WhereTrace&0x4 ){
143369 sqlite3DebugPrintf("Skip %s cost=%-3d,%3d,%3d order=%c\n",
143370 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
143371 isOrdered>=0 ? isOrdered+'0' : '?');
143372 }
143373#endif
143374 continue;
143375 }
143376 /* If we reach this points it means that the new candidate path
143377 ** needs to be added to the set of best-so-far paths. */
143378 if( nTo<mxChoice ){
143379 /* Increase the size of the aTo set by one */
143380 jj = nTo++;
143381 }else{
143382 /* New path replaces the prior worst to keep count below mxChoice */
143383 jj = mxI;
143384 }
143385 pTo = &aTo[jj];
143386#ifdef WHERETRACE_ENABLED /* 0x4 */
143387 if( sqlite3WhereTrace&0x4 ){
143388 sqlite3DebugPrintf("New %s cost=%-3d,%3d,%3d order=%c\n",
143389 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
143390 isOrdered>=0 ? isOrdered+'0' : '?');
143391 }
143392#endif
143393 }else{
143394 /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
143395 ** same set of loops and has the same isOrdered setting as the
143396 ** candidate path. Check to see if the candidate should replace
143397 ** pTo or if the candidate should be skipped.
143398 **
143399 ** The conditional is an expanded vector comparison equivalent to:
143400 ** (pTo->rCost,pTo->nRow,pTo->rUnsorted) <= (rCost,nOut,rUnsorted)
143401 */
143402 if( pTo->rCost<rCost
143403 || (pTo->rCost==rCost
143404 && (pTo->nRow<nOut
143405 || (pTo->nRow==nOut && pTo->rUnsorted<=rUnsorted)
143406 )
143407 )
143408 ){
143409#ifdef WHERETRACE_ENABLED /* 0x4 */
143410 if( sqlite3WhereTrace&0x4 ){
143411 sqlite3DebugPrintf(
143412 "Skip %s cost=%-3d,%3d,%3d order=%c",
143413 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
143414 isOrdered>=0 ? isOrdered+'0' : '?');
143415 sqlite3DebugPrintf(" vs %s cost=%-3d,%3d,%3d order=%c\n",
143416 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
143417 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
143418 }
143419#endif
143420 /* Discard the candidate path from further consideration */
143421 testcase( pTo->rCost==rCost );
143422 continue;
143423 }
143424 testcase( pTo->rCost==rCost+1 );
143425 /* Control reaches here if the candidate path is better than the
143426 ** pTo path. Replace pTo with the candidate. */
143427#ifdef WHERETRACE_ENABLED /* 0x4 */
143428 if( sqlite3WhereTrace&0x4 ){
143429 sqlite3DebugPrintf(
143430 "Update %s cost=%-3d,%3d,%3d order=%c",
143431 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
143432 isOrdered>=0 ? isOrdered+'0' : '?');
143433 sqlite3DebugPrintf(" was %s cost=%-3d,%3d,%3d order=%c\n",
143434 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
143435 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
143436 }
143437#endif
143438 }
143439 /* pWLoop is a winner. Add it to the set of best so far */
143440 pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
143441 pTo->revLoop = revMask;
143442 pTo->nRow = nOut;
143443 pTo->rCost = rCost;
143444 pTo->rUnsorted = rUnsorted;
143445 pTo->isOrdered = isOrdered;
143446 memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
143447 pTo->aLoop[iLoop] = pWLoop;
143448 if( nTo>=mxChoice ){
143449 mxI = 0;
143450 mxCost = aTo[0].rCost;
143451 mxUnsorted = aTo[0].nRow;
143452 for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){
143453 if( pTo->rCost>mxCost
143454 || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted)
143455 ){
143456 mxCost = pTo->rCost;
143457 mxUnsorted = pTo->rUnsorted;
143458 mxI = jj;
143459 }
143460 }
143461 }
143462 }
143463 }
143464
143465#ifdef WHERETRACE_ENABLED /* >=2 */
143466 if( sqlite3WhereTrace & 0x02 ){
143467 sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
143468 for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){
143469 sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
143470 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
143471 pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
143472 if( pTo->isOrdered>0 ){
143473 sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
143474 }else{
143475 sqlite3DebugPrintf("\n");
143476 }
143477 }
143478 }
143479#endif
143480
143481 /* Swap the roles of aFrom and aTo for the next generation */
143482 pFrom = aTo;
143483 aTo = aFrom;
143484 aFrom = pFrom;
143485 nFrom = nTo;
143486 }
143487
143488 if( nFrom==0 ){
143489 sqlite3ErrorMsg(pParse, "no query solution");
143490 sqlite3DbFreeNN(db, pSpace);
143491 return SQLITE_ERROR;
143492 }
143493
143494 /* Find the lowest cost path. pFrom will be left pointing to that path */
143495 pFrom = aFrom;
143496 for(ii=1; ii<nFrom; ii++){
143497 if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
143498 }
143499 assert( pWInfo->nLevel==nLoop );
143500 /* Load the lowest cost path into pWInfo */
143501 for(iLoop=0; iLoop<nLoop; iLoop++){
143502 WhereLevel *pLevel = pWInfo->a + iLoop;
143503 pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
143504 pLevel->iFrom = pWLoop->iTab;
143505 pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
143506 }
143507 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0
143508 && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
143509 && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
143510 && nRowEst
143511 ){
143512 Bitmask notUsed;
143513 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
143514 WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], &notUsed);
143515 if( rc==pWInfo->pResultSet->nExpr ){
143516 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
143517 }
143518 }
143519 pWInfo->bOrderedInnerLoop = 0;
143520 if( pWInfo->pOrderBy ){
143521 if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
143522 if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
143523 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
143524 }
143525 }else{
143526 pWInfo->nOBSat = pFrom->isOrdered;
143527 pWInfo->revMask = pFrom->revLoop;
143528 if( pWInfo->nOBSat<=0 ){
143529 pWInfo->nOBSat = 0;
143530 if( nLoop>0 ){
143531 u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags;
143532 if( (wsFlags & WHERE_ONEROW)==0
143533 && (wsFlags&(WHERE_IPK|WHERE_COLUMN_IN))!=(WHERE_IPK|WHERE_COLUMN_IN)
143534 ){
143535 Bitmask m = 0;
143536 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, pFrom,
143537 WHERE_ORDERBY_LIMIT, nLoop-1, pFrom->aLoop[nLoop-1], &m);
143538 testcase( wsFlags & WHERE_IPK );
143539 testcase( wsFlags & WHERE_COLUMN_IN );
143540 if( rc==pWInfo->pOrderBy->nExpr ){
143541 pWInfo->bOrderedInnerLoop = 1;
143542 pWInfo->revMask = m;
143543 }
143544 }
143545 }
143546 }
143547 }
143548 if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
143549 && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
143550 ){
143551 Bitmask revMask = 0;
143552 int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy,
143553 pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask
143554 );
143555 assert( pWInfo->sorted==0 );
143556 if( nOrder==pWInfo->pOrderBy->nExpr ){
143557 pWInfo->sorted = 1;
143558 pWInfo->revMask = revMask;
143559 }
143560 }
143561 }
143562
143563
143564 pWInfo->nRowOut = pFrom->nRow;
143565
143566 /* Free temporary memory and return success */
143567 sqlite3DbFreeNN(db, pSpace);
143568 return SQLITE_OK;
143569}
143570
143571/*
143572** Most queries use only a single table (they are not joins) and have
143573** simple == constraints against indexed fields. This routine attempts
143574** to plan those simple cases using much less ceremony than the
143575** general-purpose query planner, and thereby yield faster sqlite3_prepare()
143576** times for the common case.
143577**
143578** Return non-zero on success, if this query can be handled by this
143579** no-frills query planner. Return zero if this query needs the
143580** general-purpose query planner.
143581*/
143582static int whereShortCut(WhereLoopBuilder *pBuilder){
143583 WhereInfo *pWInfo;
143584 struct SrcList_item *pItem;
143585 WhereClause *pWC;
143586 WhereTerm *pTerm;
143587 WhereLoop *pLoop;
143588 int iCur;
143589 int j;
143590 Table *pTab;
143591 Index *pIdx;
143592
143593 pWInfo = pBuilder->pWInfo;
143594 if( pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE ) return 0;
143595 assert( pWInfo->pTabList->nSrc>=1 );
143596 pItem = pWInfo->pTabList->a;
143597 pTab = pItem->pTab;
143598 if( IsVirtual(pTab) ) return 0;
143599 if( pItem->fg.isIndexedBy ) return 0;
143600 iCur = pItem->iCursor;
143601 pWC = &pWInfo->sWC;
143602 pLoop = pBuilder->pNew;
143603 pLoop->wsFlags = 0;
143604 pLoop->nSkip = 0;
143605 pTerm = sqlite3WhereFindTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0);
143606 if( pTerm ){
143607 testcase( pTerm->eOperator & WO_IS );
143608 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
143609 pLoop->aLTerm[0] = pTerm;
143610 pLoop->nLTerm = 1;
143611 pLoop->u.btree.nEq = 1;
143612 /* TUNING: Cost of a rowid lookup is 10 */
143613 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
143614 }else{
143615 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
143616 int opMask;
143617 assert( pLoop->aLTermSpace==pLoop->aLTerm );
143618 if( !IsUniqueIndex(pIdx)
143619 || pIdx->pPartIdxWhere!=0
143620 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
143621 ) continue;
143622 opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
143623 for(j=0; j<pIdx->nKeyCol; j++){
143624 pTerm = sqlite3WhereFindTerm(pWC, iCur, j, 0, opMask, pIdx);
143625 if( pTerm==0 ) break;
143626 testcase( pTerm->eOperator & WO_IS );
143627 pLoop->aLTerm[j] = pTerm;
143628 }
143629 if( j!=pIdx->nKeyCol ) continue;
143630 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
143631 if( pIdx->isCovering || (pItem->colUsed & pIdx->colNotIdxed)==0 ){
143632 pLoop->wsFlags |= WHERE_IDX_ONLY;
143633 }
143634 pLoop->nLTerm = j;
143635 pLoop->u.btree.nEq = j;
143636 pLoop->u.btree.pIndex = pIdx;
143637 /* TUNING: Cost of a unique index lookup is 15 */
143638 pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */
143639 break;
143640 }
143641 }
143642 if( pLoop->wsFlags ){
143643 pLoop->nOut = (LogEst)1;
143644 pWInfo->a[0].pWLoop = pLoop;
143645 assert( pWInfo->sMaskSet.n==1 && iCur==pWInfo->sMaskSet.ix[0] );
143646 pLoop->maskSelf = 1; /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */
143647 pWInfo->a[0].iTabCur = iCur;
143648 pWInfo->nRowOut = 1;
143649 if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr;
143650 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
143651 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
143652 }
143653#ifdef SQLITE_DEBUG
143654 pLoop->cId = '0';
143655#endif
143656 return 1;
143657 }
143658 return 0;
143659}
143660
143661/*
143662** Helper function for exprIsDeterministic().
143663*/
143664static int exprNodeIsDeterministic(Walker *pWalker, Expr *pExpr){
143665 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_ConstFunc)==0 ){
143666 pWalker->eCode = 0;
143667 return WRC_Abort;
143668 }
143669 return WRC_Continue;
143670}
143671
143672/*
143673** Return true if the expression contains no non-deterministic SQL
143674** functions. Do not consider non-deterministic SQL functions that are
143675** part of sub-select statements.
143676*/
143677static int exprIsDeterministic(Expr *p){
143678 Walker w;
143679 memset(&w, 0, sizeof(w));
143680 w.eCode = 1;
143681 w.xExprCallback = exprNodeIsDeterministic;
143682 w.xSelectCallback = sqlite3SelectWalkFail;
143683 sqlite3WalkExpr(&w, p);
143684 return w.eCode;
143685}
143686
143687/*
143688** Generate the beginning of the loop used for WHERE clause processing.
143689** The return value is a pointer to an opaque structure that contains
143690** information needed to terminate the loop. Later, the calling routine
143691** should invoke sqlite3WhereEnd() with the return value of this function
143692** in order to complete the WHERE clause processing.
143693**
143694** If an error occurs, this routine returns NULL.
143695**
143696** The basic idea is to do a nested loop, one loop for each table in
143697** the FROM clause of a select. (INSERT and UPDATE statements are the
143698** same as a SELECT with only a single table in the FROM clause.) For
143699** example, if the SQL is this:
143700**
143701** SELECT * FROM t1, t2, t3 WHERE ...;
143702**
143703** Then the code generated is conceptually like the following:
143704**
143705** foreach row1 in t1 do \ Code generated
143706** foreach row2 in t2 do |-- by sqlite3WhereBegin()
143707** foreach row3 in t3 do /
143708** ...
143709** end \ Code generated
143710** end |-- by sqlite3WhereEnd()
143711** end /
143712**
143713** Note that the loops might not be nested in the order in which they
143714** appear in the FROM clause if a different order is better able to make
143715** use of indices. Note also that when the IN operator appears in
143716** the WHERE clause, it might result in additional nested loops for
143717** scanning through all values on the right-hand side of the IN.
143718**
143719** There are Btree cursors associated with each table. t1 uses cursor
143720** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
143721** And so forth. This routine generates code to open those VDBE cursors
143722** and sqlite3WhereEnd() generates the code to close them.
143723**
143724** The code that sqlite3WhereBegin() generates leaves the cursors named
143725** in pTabList pointing at their appropriate entries. The [...] code
143726** can use OP_Column and OP_Rowid opcodes on these cursors to extract
143727** data from the various tables of the loop.
143728**
143729** If the WHERE clause is empty, the foreach loops must each scan their
143730** entire tables. Thus a three-way join is an O(N^3) operation. But if
143731** the tables have indices and there are terms in the WHERE clause that
143732** refer to those indices, a complete table scan can be avoided and the
143733** code will run much faster. Most of the work of this routine is checking
143734** to see if there are indices that can be used to speed up the loop.
143735**
143736** Terms of the WHERE clause are also used to limit which rows actually
143737** make it to the "..." in the middle of the loop. After each "foreach",
143738** terms of the WHERE clause that use only terms in that loop and outer
143739** loops are evaluated and if false a jump is made around all subsequent
143740** inner loops (or around the "..." if the test occurs within the inner-
143741** most loop)
143742**
143743** OUTER JOINS
143744**
143745** An outer join of tables t1 and t2 is conceptally coded as follows:
143746**
143747** foreach row1 in t1 do
143748** flag = 0
143749** foreach row2 in t2 do
143750** start:
143751** ...
143752** flag = 1
143753** end
143754** if flag==0 then
143755** move the row2 cursor to a null row
143756** goto start
143757** fi
143758** end
143759**
143760** ORDER BY CLAUSE PROCESSING
143761**
143762** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause
143763** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement
143764** if there is one. If there is no ORDER BY clause or if this routine
143765** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
143766**
143767** The iIdxCur parameter is the cursor number of an index. If
143768** WHERE_OR_SUBCLAUSE is set, iIdxCur is the cursor number of an index
143769** to use for OR clause processing. The WHERE clause should use this
143770** specific cursor. If WHERE_ONEPASS_DESIRED is set, then iIdxCur is
143771** the first cursor in an array of cursors for all indices. iIdxCur should
143772** be used to compute the appropriate cursor depending on which index is
143773** used.
143774*/
143775SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
143776 Parse *pParse, /* The parser context */
143777 SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */
143778 Expr *pWhere, /* The WHERE clause */
143779 ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */
143780 ExprList *pResultSet, /* Query result set. Req'd for DISTINCT */
143781 u16 wctrlFlags, /* The WHERE_* flags defined in sqliteInt.h */
143782 int iAuxArg /* If WHERE_OR_SUBCLAUSE is set, index cursor number
143783 ** If WHERE_USE_LIMIT, then the limit amount */
143784){
143785 int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */
143786 int nTabList; /* Number of elements in pTabList */
143787 WhereInfo *pWInfo; /* Will become the return value of this function */
143788 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
143789 Bitmask notReady; /* Cursors that are not yet positioned */
143790 WhereLoopBuilder sWLB; /* The WhereLoop builder */
143791 WhereMaskSet *pMaskSet; /* The expression mask set */
143792 WhereLevel *pLevel; /* A single level in pWInfo->a[] */
143793 WhereLoop *pLoop; /* Pointer to a single WhereLoop object */
143794 int ii; /* Loop counter */
143795 sqlite3 *db; /* Database connection */
143796 int rc; /* Return code */
143797 u8 bFordelete = 0; /* OPFLAG_FORDELETE or zero, as appropriate */
143798
143799 assert( (wctrlFlags & WHERE_ONEPASS_MULTIROW)==0 || (
143800 (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0
143801 && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0
143802 ));
143803
143804 /* Only one of WHERE_OR_SUBCLAUSE or WHERE_USE_LIMIT */
143805 assert( (wctrlFlags & WHERE_OR_SUBCLAUSE)==0
143806 || (wctrlFlags & WHERE_USE_LIMIT)==0 );
143807
143808 /* Variable initialization */
143809 db = pParse->db;
143810 memset(&sWLB, 0, sizeof(sWLB));
143811
143812 /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */
143813 testcase( pOrderBy && pOrderBy->nExpr==BMS-1 );
143814 if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0;
143815 sWLB.pOrderBy = pOrderBy;
143816
143817 /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
143818 ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
143819 if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){
143820 wctrlFlags &= ~WHERE_WANT_DISTINCT;
143821 }
143822
143823 /* The number of tables in the FROM clause is limited by the number of
143824 ** bits in a Bitmask
143825 */
143826 testcase( pTabList->nSrc==BMS );
143827 if( pTabList->nSrc>BMS ){
143828 sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
143829 return 0;
143830 }
143831
143832 /* This function normally generates a nested loop for all tables in
143833 ** pTabList. But if the WHERE_OR_SUBCLAUSE flag is set, then we should
143834 ** only generate code for the first table in pTabList and assume that
143835 ** any cursors associated with subsequent tables are uninitialized.
143836 */
143837 nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE) ? 1 : pTabList->nSrc;
143838
143839 /* Allocate and initialize the WhereInfo structure that will become the
143840 ** return value. A single allocation is used to store the WhereInfo
143841 ** struct, the contents of WhereInfo.a[], the WhereClause structure
143842 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
143843 ** field (type Bitmask) it must be aligned on an 8-byte boundary on
143844 ** some architectures. Hence the ROUND8() below.
143845 */
143846 nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
143847 pWInfo = sqlite3DbMallocRawNN(db, nByteWInfo + sizeof(WhereLoop));
143848 if( db->mallocFailed ){
143849 sqlite3DbFree(db, pWInfo);
143850 pWInfo = 0;
143851 goto whereBeginError;
143852 }
143853 pWInfo->pParse = pParse;
143854 pWInfo->pTabList = pTabList;
143855 pWInfo->pOrderBy = pOrderBy;
143856 pWInfo->pWhere = pWhere;
143857 pWInfo->pResultSet = pResultSet;
143858 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
143859 pWInfo->nLevel = nTabList;
143860 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(v);
143861 pWInfo->wctrlFlags = wctrlFlags;
143862 pWInfo->iLimit = iAuxArg;
143863 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
143864 memset(&pWInfo->nOBSat, 0,
143865 offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
143866 memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
143867 assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
143868 pMaskSet = &pWInfo->sMaskSet;
143869 sWLB.pWInfo = pWInfo;
143870 sWLB.pWC = &pWInfo->sWC;
143871 sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo);
143872 assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) );
143873 whereLoopInit(sWLB.pNew);
143874#ifdef SQLITE_DEBUG
143875 sWLB.pNew->cId = '*';
143876#endif
143877
143878 /* Split the WHERE clause into separate subexpressions where each
143879 ** subexpression is separated by an AND operator.
143880 */
143881 initMaskSet(pMaskSet);
143882 sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo);
143883 sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND);
143884
143885 /* Special case: No FROM clause
143886 */
143887 if( nTabList==0 ){
143888 if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
143889 if( wctrlFlags & WHERE_WANT_DISTINCT ){
143890 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
143891 }
143892 ExplainQueryPlan((pParse, 0, "SCAN CONSTANT ROW"));
143893 }else{
143894 /* Assign a bit from the bitmask to every term in the FROM clause.
143895 **
143896 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
143897 **
143898 ** The rule of the previous sentence ensures thta if X is the bitmask for
143899 ** a table T, then X-1 is the bitmask for all other tables to the left of T.
143900 ** Knowing the bitmask for all tables to the left of a left join is
143901 ** important. Ticket #3015.
143902 **
143903 ** Note that bitmasks are created for all pTabList->nSrc tables in
143904 ** pTabList, not just the first nTabList tables. nTabList is normally
143905 ** equal to pTabList->nSrc but might be shortened to 1 if the
143906 ** WHERE_OR_SUBCLAUSE flag is set.
143907 */
143908 ii = 0;
143909 do{
143910 createMask(pMaskSet, pTabList->a[ii].iCursor);
143911 sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC);
143912 }while( (++ii)<pTabList->nSrc );
143913 #ifdef SQLITE_DEBUG
143914 {
143915 Bitmask mx = 0;
143916 for(ii=0; ii<pTabList->nSrc; ii++){
143917 Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
143918 assert( m>=mx );
143919 mx = m;
143920 }
143921 }
143922 #endif
143923 }
143924
143925 /* Analyze all of the subexpressions. */
143926 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
143927 if( db->mallocFailed ) goto whereBeginError;
143928
143929 /* Special case: WHERE terms that do not refer to any tables in the join
143930 ** (constant expressions). Evaluate each such term, and jump over all the
143931 ** generated code if the result is not true.
143932 **
143933 ** Do not do this if the expression contains non-deterministic functions
143934 ** that are not within a sub-select. This is not strictly required, but
143935 ** preserves SQLite's legacy behaviour in the following two cases:
143936 **
143937 ** FROM ... WHERE random()>0; -- eval random() once per row
143938 ** FROM ... WHERE (SELECT random())>0; -- eval random() once overall
143939 */
143940 for(ii=0; ii<sWLB.pWC->nTerm; ii++){
143941 WhereTerm *pT = &sWLB.pWC->a[ii];
143942 if( pT->wtFlags & TERM_VIRTUAL ) continue;
143943 if( pT->prereqAll==0 && (nTabList==0 || exprIsDeterministic(pT->pExpr)) ){
143944 sqlite3ExprIfFalse(pParse, pT->pExpr, pWInfo->iBreak, SQLITE_JUMPIFNULL);
143945 pT->wtFlags |= TERM_CODED;
143946 }
143947 }
143948
143949 if( wctrlFlags & WHERE_WANT_DISTINCT ){
143950 if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
143951 /* The DISTINCT marking is pointless. Ignore it. */
143952 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
143953 }else if( pOrderBy==0 ){
143954 /* Try to ORDER BY the result set to make distinct processing easier */
143955 pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
143956 pWInfo->pOrderBy = pResultSet;
143957 }
143958 }
143959
143960 /* Construct the WhereLoop objects */
143961#if defined(WHERETRACE_ENABLED)
143962 if( sqlite3WhereTrace & 0xffff ){
143963 sqlite3DebugPrintf("*** Optimizer Start *** (wctrlFlags: 0x%x",wctrlFlags);
143964 if( wctrlFlags & WHERE_USE_LIMIT ){
143965 sqlite3DebugPrintf(", limit: %d", iAuxArg);
143966 }
143967 sqlite3DebugPrintf(")\n");
143968 }
143969 if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */
143970 sqlite3WhereClausePrint(sWLB.pWC);
143971 }
143972#endif
143973
143974 if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
143975 rc = whereLoopAddAll(&sWLB);
143976 if( rc ) goto whereBeginError;
143977
143978#ifdef WHERETRACE_ENABLED
143979 if( sqlite3WhereTrace ){ /* Display all of the WhereLoop objects */
143980 WhereLoop *p;
143981 int i;
143982 static const char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
143983 "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
143984 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
143985 p->cId = zLabel[i%(sizeof(zLabel)-1)];
143986 whereLoopPrint(p, sWLB.pWC);
143987 }
143988 }
143989#endif
143990
143991 wherePathSolver(pWInfo, 0);
143992 if( db->mallocFailed ) goto whereBeginError;
143993 if( pWInfo->pOrderBy ){
143994 wherePathSolver(pWInfo, pWInfo->nRowOut+1);
143995 if( db->mallocFailed ) goto whereBeginError;
143996 }
143997 }
143998 if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
143999 pWInfo->revMask = ALLBITS;
144000 }
144001 if( pParse->nErr || NEVER(db->mallocFailed) ){
144002 goto whereBeginError;
144003 }
144004#ifdef WHERETRACE_ENABLED
144005 if( sqlite3WhereTrace ){
144006 sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
144007 if( pWInfo->nOBSat>0 ){
144008 sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
144009 }
144010 switch( pWInfo->eDistinct ){
144011 case WHERE_DISTINCT_UNIQUE: {
144012 sqlite3DebugPrintf(" DISTINCT=unique");
144013 break;
144014 }
144015 case WHERE_DISTINCT_ORDERED: {
144016 sqlite3DebugPrintf(" DISTINCT=ordered");
144017 break;
144018 }
144019 case WHERE_DISTINCT_UNORDERED: {
144020 sqlite3DebugPrintf(" DISTINCT=unordered");
144021 break;
144022 }
144023 }
144024 sqlite3DebugPrintf("\n");
144025 for(ii=0; ii<pWInfo->nLevel; ii++){
144026 whereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
144027 }
144028 }
144029#endif
144030
144031 /* Attempt to omit tables from the join that do not affect the result.
144032 ** For a table to not affect the result, the following must be true:
144033 **
144034 ** 1) The query must not be an aggregate.
144035 ** 2) The table must be the RHS of a LEFT JOIN.
144036 ** 3) Either the query must be DISTINCT, or else the ON or USING clause
144037 ** must contain a constraint that limits the scan of the table to
144038 ** at most a single row.
144039 ** 4) The table must not be referenced by any part of the query apart
144040 ** from its own USING or ON clause.
144041 **
144042 ** For example, given:
144043 **
144044 ** CREATE TABLE t1(ipk INTEGER PRIMARY KEY, v1);
144045 ** CREATE TABLE t2(ipk INTEGER PRIMARY KEY, v2);
144046 ** CREATE TABLE t3(ipk INTEGER PRIMARY KEY, v3);
144047 **
144048 ** then table t2 can be omitted from the following:
144049 **
144050 ** SELECT v1, v3 FROM t1
144051 ** LEFT JOIN t2 USING (t1.ipk=t2.ipk)
144052 ** LEFT JOIN t3 USING (t1.ipk=t3.ipk)
144053 **
144054 ** or from:
144055 **
144056 ** SELECT DISTINCT v1, v3 FROM t1
144057 ** LEFT JOIN t2
144058 ** LEFT JOIN t3 USING (t1.ipk=t3.ipk)
144059 */
144060 notReady = ~(Bitmask)0;
144061 if( pWInfo->nLevel>=2
144062 && pResultSet!=0 /* guarantees condition (1) above */
144063 && OptimizationEnabled(db, SQLITE_OmitNoopJoin)
144064 ){
144065 int i;
144066 Bitmask tabUsed = sqlite3WhereExprListUsage(pMaskSet, pResultSet);
144067 if( sWLB.pOrderBy ){
144068 tabUsed |= sqlite3WhereExprListUsage(pMaskSet, sWLB.pOrderBy);
144069 }
144070 for(i=pWInfo->nLevel-1; i>=1; i--){
144071 WhereTerm *pTerm, *pEnd;
144072 struct SrcList_item *pItem;
144073 pLoop = pWInfo->a[i].pWLoop;
144074 pItem = &pWInfo->pTabList->a[pLoop->iTab];
144075 if( (pItem->fg.jointype & JT_LEFT)==0 ) continue;
144076 if( (wctrlFlags & WHERE_WANT_DISTINCT)==0
144077 && (pLoop->wsFlags & WHERE_ONEROW)==0
144078 ){
144079 continue;
144080 }
144081 if( (tabUsed & pLoop->maskSelf)!=0 ) continue;
144082 pEnd = sWLB.pWC->a + sWLB.pWC->nTerm;
144083 for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
144084 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
144085 if( !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
144086 || pTerm->pExpr->iRightJoinTable!=pItem->iCursor
144087 ){
144088 break;
144089 }
144090 }
144091 }
144092 if( pTerm<pEnd ) continue;
144093 WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
144094 notReady &= ~pLoop->maskSelf;
144095 for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
144096 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
144097 pTerm->wtFlags |= TERM_CODED;
144098 }
144099 }
144100 if( i!=pWInfo->nLevel-1 ){
144101 int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel);
144102 memmove(&pWInfo->a[i], &pWInfo->a[i+1], nByte);
144103 }
144104 pWInfo->nLevel--;
144105 nTabList--;
144106 }
144107 }
144108 WHERETRACE(0xffff,("*** Optimizer Finished ***\n"));
144109 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
144110
144111 /* If the caller is an UPDATE or DELETE statement that is requesting
144112 ** to use a one-pass algorithm, determine if this is appropriate.
144113 **
144114 ** A one-pass approach can be used if the caller has requested one
144115 ** and either (a) the scan visits at most one row or (b) each
144116 ** of the following are true:
144117 **
144118 ** * the caller has indicated that a one-pass approach can be used
144119 ** with multiple rows (by setting WHERE_ONEPASS_MULTIROW), and
144120 ** * the table is not a virtual table, and
144121 ** * either the scan does not use the OR optimization or the caller
144122 ** is a DELETE operation (WHERE_DUPLICATES_OK is only specified
144123 ** for DELETE).
144124 **
144125 ** The last qualification is because an UPDATE statement uses
144126 ** WhereInfo.aiCurOnePass[1] to determine whether or not it really can
144127 ** use a one-pass approach, and this is not set accurately for scans
144128 ** that use the OR optimization.
144129 */
144130 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
144131 if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 ){
144132 int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
144133 int bOnerow = (wsFlags & WHERE_ONEROW)!=0;
144134 if( bOnerow || (
144135 0!=(wctrlFlags & WHERE_ONEPASS_MULTIROW)
144136 && 0==(wsFlags & WHERE_VIRTUALTABLE)
144137 && (0==(wsFlags & WHERE_MULTI_OR) || (wctrlFlags & WHERE_DUPLICATES_OK))
144138 )){
144139 pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI;
144140 if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){
144141 if( wctrlFlags & WHERE_ONEPASS_MULTIROW ){
144142 bFordelete = OPFLAG_FORDELETE;
144143 }
144144 pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY);
144145 }
144146 }
144147 }
144148
144149 /* Open all tables in the pTabList and any indices selected for
144150 ** searching those tables.
144151 */
144152 for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
144153 Table *pTab; /* Table to open */
144154 int iDb; /* Index of database containing table/index */
144155 struct SrcList_item *pTabItem;
144156
144157 pTabItem = &pTabList->a[pLevel->iFrom];
144158 pTab = pTabItem->pTab;
144159 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
144160 pLoop = pLevel->pWLoop;
144161 if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
144162 /* Do nothing */
144163 }else
144164#ifndef SQLITE_OMIT_VIRTUALTABLE
144165 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
144166 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
144167 int iCur = pTabItem->iCursor;
144168 sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
144169 }else if( IsVirtual(pTab) ){
144170 /* noop */
144171 }else
144172#endif
144173 if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
144174 && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0 ){
144175 int op = OP_OpenRead;
144176 if( pWInfo->eOnePass!=ONEPASS_OFF ){
144177 op = OP_OpenWrite;
144178 pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
144179 };
144180 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
144181 assert( pTabItem->iCursor==pLevel->iTabCur );
144182 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 );
144183 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS );
144184 if( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol<BMS && HasRowid(pTab) ){
144185 Bitmask b = pTabItem->colUsed;
144186 int n = 0;
144187 for(; b; b=b>>1, n++){}
144188 sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(n), P4_INT32);
144189 assert( n<=pTab->nCol );
144190 }
144191#ifdef SQLITE_ENABLE_CURSOR_HINTS
144192 if( pLoop->u.btree.pIndex!=0 ){
144193 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ|bFordelete);
144194 }else
144195#endif
144196 {
144197 sqlite3VdbeChangeP5(v, bFordelete);
144198 }
144199#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
144200 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0,
144201 (const u8*)&pTabItem->colUsed, P4_INT64);
144202#endif
144203 }else{
144204 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
144205 }
144206 if( pLoop->wsFlags & WHERE_INDEXED ){
144207 Index *pIx = pLoop->u.btree.pIndex;
144208 int iIndexCur;
144209 int op = OP_OpenRead;
144210 /* iAuxArg is always set to a positive value if ONEPASS is possible */
144211 assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
144212 if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIx)
144213 && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0
144214 ){
144215 /* This is one term of an OR-optimization using the PRIMARY KEY of a
144216 ** WITHOUT ROWID table. No need for a separate index */
144217 iIndexCur = pLevel->iTabCur;
144218 op = 0;
144219 }else if( pWInfo->eOnePass!=ONEPASS_OFF ){
144220 Index *pJ = pTabItem->pTab->pIndex;
144221 iIndexCur = iAuxArg;
144222 assert( wctrlFlags & WHERE_ONEPASS_DESIRED );
144223 while( ALWAYS(pJ) && pJ!=pIx ){
144224 iIndexCur++;
144225 pJ = pJ->pNext;
144226 }
144227 op = OP_OpenWrite;
144228 pWInfo->aiCurOnePass[1] = iIndexCur;
144229 }else if( iAuxArg && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 ){
144230 iIndexCur = iAuxArg;
144231 op = OP_ReopenIdx;
144232 }else{
144233 iIndexCur = pParse->nTab++;
144234 }
144235 pLevel->iIdxCur = iIndexCur;
144236 assert( pIx->pSchema==pTab->pSchema );
144237 assert( iIndexCur>=0 );
144238 if( op ){
144239 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
144240 sqlite3VdbeSetP4KeyInfo(pParse, pIx);
144241 if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0
144242 && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0
144243 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
144244 && pWInfo->eDistinct!=WHERE_DISTINCT_ORDERED
144245 ){
144246 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ); /* Hint to COMDB2 */
144247 }
144248 VdbeComment((v, "%s", pIx->zName));
144249#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
144250 {
144251 u64 colUsed = 0;
144252 int ii, jj;
144253 for(ii=0; ii<pIx->nColumn; ii++){
144254 jj = pIx->aiColumn[ii];
144255 if( jj<0 ) continue;
144256 if( jj>63 ) jj = 63;
144257 if( (pTabItem->colUsed & MASKBIT(jj))==0 ) continue;
144258 colUsed |= ((u64)1)<<(ii<63 ? ii : 63);
144259 }
144260 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, iIndexCur, 0, 0,
144261 (u8*)&colUsed, P4_INT64);
144262 }
144263#endif /* SQLITE_ENABLE_COLUMN_USED_MASK */
144264 }
144265 }
144266 if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
144267 }
144268 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
144269 if( db->mallocFailed ) goto whereBeginError;
144270
144271 /* Generate the code to do the search. Each iteration of the for
144272 ** loop below generates code for a single nested loop of the VM
144273 ** program.
144274 */
144275 for(ii=0; ii<nTabList; ii++){
144276 int addrExplain;
144277 int wsFlags;
144278 pLevel = &pWInfo->a[ii];
144279 wsFlags = pLevel->pWLoop->wsFlags;
144280#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
144281 if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
144282 constructAutomaticIndex(pParse, &pWInfo->sWC,
144283 &pTabList->a[pLevel->iFrom], notReady, pLevel);
144284 if( db->mallocFailed ) goto whereBeginError;
144285 }
144286#endif
144287 addrExplain = sqlite3WhereExplainOneScan(
144288 pParse, pTabList, pLevel, wctrlFlags
144289 );
144290 pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
144291 notReady = sqlite3WhereCodeOneLoopStart(pWInfo, ii, notReady);
144292 pWInfo->iContinue = pLevel->addrCont;
144293 if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_OR_SUBCLAUSE)==0 ){
144294 sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain);
144295 }
144296 }
144297
144298 /* Done. */
144299 VdbeModuleComment((v, "Begin WHERE-core"));
144300 return pWInfo;
144301
144302 /* Jump here if malloc fails */
144303whereBeginError:
144304 if( pWInfo ){
144305 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
144306 whereInfoFree(db, pWInfo);
144307 }
144308 return 0;
144309}
144310
144311/*
144312** Part of sqlite3WhereEnd() will rewrite opcodes to reference the
144313** index rather than the main table. In SQLITE_DEBUG mode, we want
144314** to trace those changes if PRAGMA vdbe_addoptrace=on. This routine
144315** does that.
144316*/
144317#ifndef SQLITE_DEBUG
144318# define OpcodeRewriteTrace(D,K,P) /* no-op */
144319#else
144320# define OpcodeRewriteTrace(D,K,P) sqlite3WhereOpcodeRewriteTrace(D,K,P)
144321 static void sqlite3WhereOpcodeRewriteTrace(
144322 sqlite3 *db,
144323 int pc,
144324 VdbeOp *pOp
144325 ){
144326 if( (db->flags & SQLITE_VdbeAddopTrace)==0 ) return;
144327 sqlite3VdbePrintOp(0, pc, pOp);
144328 }
144329#endif
144330
144331/*
144332** Generate the end of the WHERE loop. See comments on
144333** sqlite3WhereBegin() for additional information.
144334*/
144335SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
144336 Parse *pParse = pWInfo->pParse;
144337 Vdbe *v = pParse->pVdbe;
144338 int i;
144339 WhereLevel *pLevel;
144340 WhereLoop *pLoop;
144341 SrcList *pTabList = pWInfo->pTabList;
144342 sqlite3 *db = pParse->db;
144343
144344 /* Generate loop termination code.
144345 */
144346 VdbeModuleComment((v, "End WHERE-core"));
144347 for(i=pWInfo->nLevel-1; i>=0; i--){
144348 int addr;
144349 pLevel = &pWInfo->a[i];
144350 pLoop = pLevel->pWLoop;
144351 if( pLevel->op!=OP_Noop ){
144352#ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
144353 int addrSeek = 0;
144354 Index *pIdx;
144355 int n;
144356 if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
144357 && i==pWInfo->nLevel-1 /* Ticket [ef9318757b152e3] 2017-10-21 */
144358 && (pLoop->wsFlags & WHERE_INDEXED)!=0
144359 && (pIdx = pLoop->u.btree.pIndex)->hasStat1
144360 && (n = pLoop->u.btree.nIdxCol)>0
144361 && pIdx->aiRowLogEst[n]>=36
144362 ){
144363 int r1 = pParse->nMem+1;
144364 int j, op;
144365 for(j=0; j<n; j++){
144366 sqlite3VdbeAddOp3(v, OP_Column, pLevel->iIdxCur, j, r1+j);
144367 }
144368 pParse->nMem += n+1;
144369 op = pLevel->op==OP_Prev ? OP_SeekLT : OP_SeekGT;
144370 addrSeek = sqlite3VdbeAddOp4Int(v, op, pLevel->iIdxCur, 0, r1, n);
144371 VdbeCoverageIf(v, op==OP_SeekLT);
144372 VdbeCoverageIf(v, op==OP_SeekGT);
144373 sqlite3VdbeAddOp2(v, OP_Goto, 1, pLevel->p2);
144374 }
144375#endif /* SQLITE_DISABLE_SKIPAHEAD_DISTINCT */
144376 /* The common case: Advance to the next row */
144377 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
144378 sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
144379 sqlite3VdbeChangeP5(v, pLevel->p5);
144380 VdbeCoverage(v);
144381 VdbeCoverageIf(v, pLevel->op==OP_Next);
144382 VdbeCoverageIf(v, pLevel->op==OP_Prev);
144383 VdbeCoverageIf(v, pLevel->op==OP_VNext);
144384#ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
144385 if( addrSeek ) sqlite3VdbeJumpHere(v, addrSeek);
144386#endif
144387 }else{
144388 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
144389 }
144390 if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
144391 struct InLoop *pIn;
144392 int j;
144393 sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
144394 for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
144395 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
144396 if( pIn->eEndLoopOp!=OP_Noop ){
144397 if( pIn->nPrefix ){
144398 assert( pLoop->wsFlags & WHERE_IN_EARLYOUT );
144399 sqlite3VdbeAddOp4Int(v, OP_IfNoHope, pLevel->iIdxCur,
144400 sqlite3VdbeCurrentAddr(v)+2,
144401 pIn->iBase, pIn->nPrefix);
144402 VdbeCoverage(v);
144403 }
144404 sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
144405 VdbeCoverage(v);
144406 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev);
144407 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Next);
144408 }
144409 sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
144410 }
144411 }
144412 sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
144413 if( pLevel->addrSkip ){
144414 sqlite3VdbeGoto(v, pLevel->addrSkip);
144415 VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
144416 sqlite3VdbeJumpHere(v, pLevel->addrSkip);
144417 sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
144418 }
144419#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
144420 if( pLevel->addrLikeRep ){
144421 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, (int)(pLevel->iLikeRepCntr>>1),
144422 pLevel->addrLikeRep);
144423 VdbeCoverage(v);
144424 }
144425#endif
144426 if( pLevel->iLeftJoin ){
144427 int ws = pLoop->wsFlags;
144428 addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v);
144429 assert( (ws & WHERE_IDX_ONLY)==0 || (ws & WHERE_INDEXED)!=0 );
144430 if( (ws & WHERE_IDX_ONLY)==0 ){
144431 assert( pLevel->iTabCur==pTabList->a[pLevel->iFrom].iCursor );
144432 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iTabCur);
144433 }
144434 if( (ws & WHERE_INDEXED)
144435 || ((ws & WHERE_MULTI_OR) && pLevel->u.pCovidx)
144436 ){
144437 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
144438 }
144439 if( pLevel->op==OP_Return ){
144440 sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
144441 }else{
144442 sqlite3VdbeGoto(v, pLevel->addrFirst);
144443 }
144444 sqlite3VdbeJumpHere(v, addr);
144445 }
144446 VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
144447 pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
144448 }
144449
144450 /* The "break" point is here, just past the end of the outer loop.
144451 ** Set it.
144452 */
144453 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
144454
144455 assert( pWInfo->nLevel<=pTabList->nSrc );
144456 for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
144457 int k, last;
144458 VdbeOp *pOp;
144459 Index *pIdx = 0;
144460 struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
144461 Table *pTab = pTabItem->pTab;
144462 assert( pTab!=0 );
144463 pLoop = pLevel->pWLoop;
144464
144465 /* For a co-routine, change all OP_Column references to the table of
144466 ** the co-routine into OP_Copy of result contained in a register.
144467 ** OP_Rowid becomes OP_Null.
144468 */
144469 if( pTabItem->fg.viaCoroutine ){
144470 testcase( pParse->db->mallocFailed );
144471 translateColumnToCopy(pParse, pLevel->addrBody, pLevel->iTabCur,
144472 pTabItem->regResult, 0);
144473 continue;
144474 }
144475
144476 /* If this scan uses an index, make VDBE code substitutions to read data
144477 ** from the index instead of from the table where possible. In some cases
144478 ** this optimization prevents the table from ever being read, which can
144479 ** yield a significant performance boost.
144480 **
144481 ** Calls to the code generator in between sqlite3WhereBegin and
144482 ** sqlite3WhereEnd will have created code that references the table
144483 ** directly. This loop scans all that code looking for opcodes
144484 ** that reference the table and converts them into opcodes that
144485 ** reference the index.
144486 */
144487 if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
144488 pIdx = pLoop->u.btree.pIndex;
144489 }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
144490 pIdx = pLevel->u.pCovidx;
144491 }
144492 if( pIdx
144493 && (pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable))
144494 && !db->mallocFailed
144495 ){
144496 last = sqlite3VdbeCurrentAddr(v);
144497 k = pLevel->addrBody;
144498#ifdef SQLITE_DEBUG
144499 if( db->flags & SQLITE_VdbeAddopTrace ){
144500 printf("TRANSLATE opcodes in range %d..%d\n", k, last-1);
144501 }
144502#endif
144503 pOp = sqlite3VdbeGetOp(v, k);
144504 for(; k<last; k++, pOp++){
144505 if( pOp->p1!=pLevel->iTabCur ) continue;
144506 if( pOp->opcode==OP_Column
144507#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
144508 || pOp->opcode==OP_Offset
144509#endif
144510 ){
144511 int x = pOp->p2;
144512 assert( pIdx->pTable==pTab );
144513 if( !HasRowid(pTab) ){
144514 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
144515 x = pPk->aiColumn[x];
144516 assert( x>=0 );
144517 }
144518 x = sqlite3ColumnOfIndex(pIdx, x);
144519 if( x>=0 ){
144520 pOp->p2 = x;
144521 pOp->p1 = pLevel->iIdxCur;
144522 OpcodeRewriteTrace(db, k, pOp);
144523 }
144524 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0
144525 || pWInfo->eOnePass );
144526 }else if( pOp->opcode==OP_Rowid ){
144527 pOp->p1 = pLevel->iIdxCur;
144528 pOp->opcode = OP_IdxRowid;
144529 OpcodeRewriteTrace(db, k, pOp);
144530 }else if( pOp->opcode==OP_IfNullRow ){
144531 pOp->p1 = pLevel->iIdxCur;
144532 OpcodeRewriteTrace(db, k, pOp);
144533 }
144534 }
144535#ifdef SQLITE_DEBUG
144536 if( db->flags & SQLITE_VdbeAddopTrace ) printf("TRANSLATE complete\n");
144537#endif
144538 }
144539 }
144540
144541 /* Final cleanup
144542 */
144543 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
144544 whereInfoFree(db, pWInfo);
144545 return;
144546}
144547
144548/************** End of where.c ***********************************************/
144549/************** Begin file window.c ******************************************/
144550/*
144551** 2018 May 08
144552**
144553** The author disclaims copyright to this source code. In place of
144554** a legal notice, here is a blessing:
144555**
144556** May you do good and not evil.
144557** May you find forgiveness for yourself and forgive others.
144558** May you share freely, never taking more than you give.
144559**
144560*************************************************************************
144561*/
144562/* #include "sqliteInt.h" */
144563
144564#ifndef SQLITE_OMIT_WINDOWFUNC
144565
144566/*
144567** SELECT REWRITING
144568**
144569** Any SELECT statement that contains one or more window functions in
144570** either the select list or ORDER BY clause (the only two places window
144571** functions may be used) is transformed by function sqlite3WindowRewrite()
144572** in order to support window function processing. For example, with the
144573** schema:
144574**
144575** CREATE TABLE t1(a, b, c, d, e, f, g);
144576**
144577** the statement:
144578**
144579** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM t1 ORDER BY e;
144580**
144581** is transformed to:
144582**
144583** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM (
144584** SELECT a, e, c, d, b FROM t1 ORDER BY c, d
144585** ) ORDER BY e;
144586**
144587** The flattening optimization is disabled when processing this transformed
144588** SELECT statement. This allows the implementation of the window function
144589** (in this case max()) to process rows sorted in order of (c, d), which
144590** makes things easier for obvious reasons. More generally:
144591**
144592** * FROM, WHERE, GROUP BY and HAVING clauses are all moved to
144593** the sub-query.
144594**
144595** * ORDER BY, LIMIT and OFFSET remain part of the parent query.
144596**
144597** * Terminals from each of the expression trees that make up the
144598** select-list and ORDER BY expressions in the parent query are
144599** selected by the sub-query. For the purposes of the transformation,
144600** terminals are column references and aggregate functions.
144601**
144602** If there is more than one window function in the SELECT that uses
144603** the same window declaration (the OVER bit), then a single scan may
144604** be used to process more than one window function. For example:
144605**
144606** SELECT max(b) OVER (PARTITION BY c ORDER BY d),
144607** min(e) OVER (PARTITION BY c ORDER BY d)
144608** FROM t1;
144609**
144610** is transformed in the same way as the example above. However:
144611**
144612** SELECT max(b) OVER (PARTITION BY c ORDER BY d),
144613** min(e) OVER (PARTITION BY a ORDER BY b)
144614** FROM t1;
144615**
144616** Must be transformed to:
144617**
144618** SELECT max(b) OVER (PARTITION BY c ORDER BY d) FROM (
144619** SELECT e, min(e) OVER (PARTITION BY a ORDER BY b), c, d, b FROM
144620** SELECT a, e, c, d, b FROM t1 ORDER BY a, b
144621** ) ORDER BY c, d
144622** ) ORDER BY e;
144623**
144624** so that both min() and max() may process rows in the order defined by
144625** their respective window declarations.
144626**
144627** INTERFACE WITH SELECT.C
144628**
144629** When processing the rewritten SELECT statement, code in select.c calls
144630** sqlite3WhereBegin() to begin iterating through the results of the
144631** sub-query, which is always implemented as a co-routine. It then calls
144632** sqlite3WindowCodeStep() to process rows and finish the scan by calling
144633** sqlite3WhereEnd().
144634**
144635** sqlite3WindowCodeStep() generates VM code so that, for each row returned
144636** by the sub-query a sub-routine (OP_Gosub) coded by select.c is invoked.
144637** When the sub-routine is invoked:
144638**
144639** * The results of all window-functions for the row are stored
144640** in the associated Window.regResult registers.
144641**
144642** * The required terminal values are stored in the current row of
144643** temp table Window.iEphCsr.
144644**
144645** In some cases, depending on the window frame and the specific window
144646** functions invoked, sqlite3WindowCodeStep() caches each entire partition
144647** in a temp table before returning any rows. In other cases it does not.
144648** This detail is encapsulated within this file, the code generated by
144649** select.c is the same in either case.
144650**
144651** BUILT-IN WINDOW FUNCTIONS
144652**
144653** This implementation features the following built-in window functions:
144654**
144655** row_number()
144656** rank()
144657** dense_rank()
144658** percent_rank()
144659** cume_dist()
144660** ntile(N)
144661** lead(expr [, offset [, default]])
144662** lag(expr [, offset [, default]])
144663** first_value(expr)
144664** last_value(expr)
144665** nth_value(expr, N)
144666**
144667** These are the same built-in window functions supported by Postgres.
144668** Although the behaviour of aggregate window functions (functions that
144669** can be used as either aggregates or window funtions) allows them to
144670** be implemented using an API, built-in window functions are much more
144671** esoteric. Additionally, some window functions (e.g. nth_value())
144672** may only be implemented by caching the entire partition in memory.
144673** As such, some built-in window functions use the same API as aggregate
144674** window functions and some are implemented directly using VDBE
144675** instructions. Additionally, for those functions that use the API, the
144676** window frame is sometimes modified before the SELECT statement is
144677** rewritten. For example, regardless of the specified window frame, the
144678** row_number() function always uses:
144679**
144680** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
144681**
144682** See sqlite3WindowUpdate() for details.
144683**
144684** As well as some of the built-in window functions, aggregate window
144685** functions min() and max() are implemented using VDBE instructions if
144686** the start of the window frame is declared as anything other than
144687** UNBOUNDED PRECEDING.
144688*/
144689
144690/*
144691** Implementation of built-in window function row_number(). Assumes that the
144692** window frame has been coerced to:
144693**
144694** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
144695*/
144696static void row_numberStepFunc(
144697 sqlite3_context *pCtx,
144698 int nArg,
144699 sqlite3_value **apArg
144700){
144701 i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p));
144702 if( p ) (*p)++;
144703 UNUSED_PARAMETER(nArg);
144704 UNUSED_PARAMETER(apArg);
144705}
144706static void row_numberValueFunc(sqlite3_context *pCtx){
144707 i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p));
144708 sqlite3_result_int64(pCtx, (p ? *p : 0));
144709}
144710
144711/*
144712** Context object type used by rank(), dense_rank(), percent_rank() and
144713** cume_dist().
144714*/
144715struct CallCount {
144716 i64 nValue;
144717 i64 nStep;
144718 i64 nTotal;
144719};
144720
144721/*
144722** Implementation of built-in window function dense_rank(). Assumes that
144723** the window frame has been set to:
144724**
144725** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
144726*/
144727static void dense_rankStepFunc(
144728 sqlite3_context *pCtx,
144729 int nArg,
144730 sqlite3_value **apArg
144731){
144732 struct CallCount *p;
144733 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
144734 if( p ) p->nStep = 1;
144735 UNUSED_PARAMETER(nArg);
144736 UNUSED_PARAMETER(apArg);
144737}
144738static void dense_rankValueFunc(sqlite3_context *pCtx){
144739 struct CallCount *p;
144740 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
144741 if( p ){
144742 if( p->nStep ){
144743 p->nValue++;
144744 p->nStep = 0;
144745 }
144746 sqlite3_result_int64(pCtx, p->nValue);
144747 }
144748}
144749
144750/*
144751** Implementation of built-in window function rank(). Assumes that
144752** the window frame has been set to:
144753**
144754** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
144755*/
144756static void rankStepFunc(
144757 sqlite3_context *pCtx,
144758 int nArg,
144759 sqlite3_value **apArg
144760){
144761 struct CallCount *p;
144762 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
144763 if( p ){
144764 p->nStep++;
144765 if( p->nValue==0 ){
144766 p->nValue = p->nStep;
144767 }
144768 }
144769 UNUSED_PARAMETER(nArg);
144770 UNUSED_PARAMETER(apArg);
144771}
144772static void rankValueFunc(sqlite3_context *pCtx){
144773 struct CallCount *p;
144774 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
144775 if( p ){
144776 sqlite3_result_int64(pCtx, p->nValue);
144777 p->nValue = 0;
144778 }
144779}
144780
144781/*
144782** Implementation of built-in window function percent_rank(). Assumes that
144783** the window frame has been set to:
144784**
144785** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
144786*/
144787static void percent_rankStepFunc(
144788 sqlite3_context *pCtx,
144789 int nArg,
144790 sqlite3_value **apArg
144791){
144792 struct CallCount *p;
144793 UNUSED_PARAMETER(nArg); assert( nArg==1 );
144794
144795 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
144796 if( p ){
144797 if( p->nTotal==0 ){
144798 p->nTotal = sqlite3_value_int64(apArg[0]);
144799 }
144800 p->nStep++;
144801 if( p->nValue==0 ){
144802 p->nValue = p->nStep;
144803 }
144804 }
144805}
144806static void percent_rankValueFunc(sqlite3_context *pCtx){
144807 struct CallCount *p;
144808 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
144809 if( p ){
144810 if( p->nTotal>1 ){
144811 double r = (double)(p->nValue-1) / (double)(p->nTotal-1);
144812 sqlite3_result_double(pCtx, r);
144813 }else{
144814 sqlite3_result_double(pCtx, 0.0);
144815 }
144816 p->nValue = 0;
144817 }
144818}
144819
144820/*
144821** Implementation of built-in window function cume_dist(). Assumes that
144822** the window frame has been set to:
144823**
144824** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
144825*/
144826static void cume_distStepFunc(
144827 sqlite3_context *pCtx,
144828 int nArg,
144829 sqlite3_value **apArg
144830){
144831 struct CallCount *p;
144832 assert( nArg==1 ); UNUSED_PARAMETER(nArg);
144833
144834 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
144835 if( p ){
144836 if( p->nTotal==0 ){
144837 p->nTotal = sqlite3_value_int64(apArg[0]);
144838 }
144839 p->nStep++;
144840 }
144841}
144842static void cume_distValueFunc(sqlite3_context *pCtx){
144843 struct CallCount *p;
144844 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
144845 if( p && p->nTotal ){
144846 double r = (double)(p->nStep) / (double)(p->nTotal);
144847 sqlite3_result_double(pCtx, r);
144848 }
144849}
144850
144851/*
144852** Context object for ntile() window function.
144853*/
144854struct NtileCtx {
144855 i64 nTotal; /* Total rows in partition */
144856 i64 nParam; /* Parameter passed to ntile(N) */
144857 i64 iRow; /* Current row */
144858};
144859
144860/*
144861** Implementation of ntile(). This assumes that the window frame has
144862** been coerced to:
144863**
144864** ROWS UNBOUNDED PRECEDING AND CURRENT ROW
144865*/
144866static void ntileStepFunc(
144867 sqlite3_context *pCtx,
144868 int nArg,
144869 sqlite3_value **apArg
144870){
144871 struct NtileCtx *p;
144872 assert( nArg==2 ); UNUSED_PARAMETER(nArg);
144873 p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
144874 if( p ){
144875 if( p->nTotal==0 ){
144876 p->nParam = sqlite3_value_int64(apArg[0]);
144877 p->nTotal = sqlite3_value_int64(apArg[1]);
144878 if( p->nParam<=0 ){
144879 sqlite3_result_error(
144880 pCtx, "argument of ntile must be a positive integer", -1
144881 );
144882 }
144883 }
144884 p->iRow++;
144885 }
144886}
144887static void ntileValueFunc(sqlite3_context *pCtx){
144888 struct NtileCtx *p;
144889 p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
144890 if( p && p->nParam>0 ){
144891 int nSize = (p->nTotal / p->nParam);
144892 if( nSize==0 ){
144893 sqlite3_result_int64(pCtx, p->iRow);
144894 }else{
144895 i64 nLarge = p->nTotal - p->nParam*nSize;
144896 i64 iSmall = nLarge*(nSize+1);
144897 i64 iRow = p->iRow-1;
144898
144899 assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal );
144900
144901 if( iRow<iSmall ){
144902 sqlite3_result_int64(pCtx, 1 + iRow/(nSize+1));
144903 }else{
144904 sqlite3_result_int64(pCtx, 1 + nLarge + (iRow-iSmall)/nSize);
144905 }
144906 }
144907 }
144908}
144909
144910/*
144911** Context object for last_value() window function.
144912*/
144913struct LastValueCtx {
144914 sqlite3_value *pVal;
144915 int nVal;
144916};
144917
144918/*
144919** Implementation of last_value().
144920*/
144921static void last_valueStepFunc(
144922 sqlite3_context *pCtx,
144923 int nArg,
144924 sqlite3_value **apArg
144925){
144926 struct LastValueCtx *p;
144927 UNUSED_PARAMETER(nArg);
144928 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
144929 if( p ){
144930 sqlite3_value_free(p->pVal);
144931 p->pVal = sqlite3_value_dup(apArg[0]);
144932 if( p->pVal==0 ){
144933 sqlite3_result_error_nomem(pCtx);
144934 }else{
144935 p->nVal++;
144936 }
144937 }
144938}
144939static void last_valueInvFunc(
144940 sqlite3_context *pCtx,
144941 int nArg,
144942 sqlite3_value **apArg
144943){
144944 struct LastValueCtx *p;
144945 UNUSED_PARAMETER(nArg);
144946 UNUSED_PARAMETER(apArg);
144947 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
144948 if( ALWAYS(p) ){
144949 p->nVal--;
144950 if( p->nVal==0 ){
144951 sqlite3_value_free(p->pVal);
144952 p->pVal = 0;
144953 }
144954 }
144955}
144956static void last_valueValueFunc(sqlite3_context *pCtx){
144957 struct LastValueCtx *p;
144958 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
144959 if( p && p->pVal ){
144960 sqlite3_result_value(pCtx, p->pVal);
144961 }
144962}
144963static void last_valueFinalizeFunc(sqlite3_context *pCtx){
144964 struct LastValueCtx *p;
144965 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
144966 if( p && p->pVal ){
144967 sqlite3_result_value(pCtx, p->pVal);
144968 sqlite3_value_free(p->pVal);
144969 p->pVal = 0;
144970 }
144971}
144972
144973/*
144974** Static names for the built-in window function names. These static
144975** names are used, rather than string literals, so that FuncDef objects
144976** can be associated with a particular window function by direct
144977** comparison of the zName pointer. Example:
144978**
144979** if( pFuncDef->zName==row_valueName ){ ... }
144980*/
144981static const char row_numberName[] = "row_number";
144982static const char dense_rankName[] = "dense_rank";
144983static const char rankName[] = "rank";
144984static const char percent_rankName[] = "percent_rank";
144985static const char cume_distName[] = "cume_dist";
144986static const char ntileName[] = "ntile";
144987static const char last_valueName[] = "last_value";
144988static const char nth_valueName[] = "nth_value";
144989static const char first_valueName[] = "first_value";
144990static const char leadName[] = "lead";
144991static const char lagName[] = "lag";
144992
144993/*
144994** No-op implementations of xStep() and xFinalize(). Used as place-holders
144995** for built-in window functions that never call those interfaces.
144996**
144997** The noopValueFunc() is called but is expected to do nothing. The
144998** noopStepFunc() is never called, and so it is marked with NO_TEST to
144999** let the test coverage routine know not to expect this function to be
145000** invoked.
145001*/
145002static void noopStepFunc( /*NO_TEST*/
145003 sqlite3_context *p, /*NO_TEST*/
145004 int n, /*NO_TEST*/
145005 sqlite3_value **a /*NO_TEST*/
145006){ /*NO_TEST*/
145007 UNUSED_PARAMETER(p); /*NO_TEST*/
145008 UNUSED_PARAMETER(n); /*NO_TEST*/
145009 UNUSED_PARAMETER(a); /*NO_TEST*/
145010 assert(0); /*NO_TEST*/
145011} /*NO_TEST*/
145012static void noopValueFunc(sqlite3_context *p){ UNUSED_PARAMETER(p); /*no-op*/ }
145013
145014/* Window functions that use all window interfaces: xStep, xFinal,
145015** xValue, and xInverse */
145016#define WINDOWFUNCALL(name,nArg,extra) { \
145017 nArg, (SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \
145018 name ## StepFunc, name ## FinalizeFunc, name ## ValueFunc, \
145019 name ## InvFunc, name ## Name, {0} \
145020}
145021
145022/* Window functions that are implemented using bytecode and thus have
145023** no-op routines for their methods */
145024#define WINDOWFUNCNOOP(name,nArg,extra) { \
145025 nArg, (SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \
145026 noopStepFunc, noopValueFunc, noopValueFunc, \
145027 noopStepFunc, name ## Name, {0} \
145028}
145029
145030/* Window functions that use all window interfaces: xStep, the
145031** same routine for xFinalize and xValue and which never call
145032** xInverse. */
145033#define WINDOWFUNCX(name,nArg,extra) { \
145034 nArg, (SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \
145035 name ## StepFunc, name ## ValueFunc, name ## ValueFunc, \
145036 noopStepFunc, name ## Name, {0} \
145037}
145038
145039
145040/*
145041** Register those built-in window functions that are not also aggregates.
145042*/
145043SQLITE_PRIVATE void sqlite3WindowFunctions(void){
145044 static FuncDef aWindowFuncs[] = {
145045 WINDOWFUNCX(row_number, 0, 0),
145046 WINDOWFUNCX(dense_rank, 0, 0),
145047 WINDOWFUNCX(rank, 0, 0),
145048 WINDOWFUNCX(percent_rank, 0, SQLITE_FUNC_WINDOW_SIZE),
145049 WINDOWFUNCX(cume_dist, 0, SQLITE_FUNC_WINDOW_SIZE),
145050 WINDOWFUNCX(ntile, 1, SQLITE_FUNC_WINDOW_SIZE),
145051 WINDOWFUNCALL(last_value, 1, 0),
145052 WINDOWFUNCNOOP(nth_value, 2, 0),
145053 WINDOWFUNCNOOP(first_value, 1, 0),
145054 WINDOWFUNCNOOP(lead, 1, 0),
145055 WINDOWFUNCNOOP(lead, 2, 0),
145056 WINDOWFUNCNOOP(lead, 3, 0),
145057 WINDOWFUNCNOOP(lag, 1, 0),
145058 WINDOWFUNCNOOP(lag, 2, 0),
145059 WINDOWFUNCNOOP(lag, 3, 0),
145060 };
145061 sqlite3InsertBuiltinFuncs(aWindowFuncs, ArraySize(aWindowFuncs));
145062}
145063
145064/*
145065** This function is called immediately after resolving the function name
145066** for a window function within a SELECT statement. Argument pList is a
145067** linked list of WINDOW definitions for the current SELECT statement.
145068** Argument pFunc is the function definition just resolved and pWin
145069** is the Window object representing the associated OVER clause. This
145070** function updates the contents of pWin as follows:
145071**
145072** * If the OVER clause refered to a named window (as in "max(x) OVER win"),
145073** search list pList for a matching WINDOW definition, and update pWin
145074** accordingly. If no such WINDOW clause can be found, leave an error
145075** in pParse.
145076**
145077** * If the function is a built-in window function that requires the
145078** window to be coerced (see "BUILT-IN WINDOW FUNCTIONS" at the top
145079** of this file), pWin is updated here.
145080*/
145081SQLITE_PRIVATE void sqlite3WindowUpdate(
145082 Parse *pParse,
145083 Window *pList, /* List of named windows for this SELECT */
145084 Window *pWin, /* Window frame to update */
145085 FuncDef *pFunc /* Window function definition */
145086){
145087 if( pWin->zName && pWin->eType==0 ){
145088 Window *p;
145089 for(p=pList; p; p=p->pNextWin){
145090 if( sqlite3StrICmp(p->zName, pWin->zName)==0 ) break;
145091 }
145092 if( p==0 ){
145093 sqlite3ErrorMsg(pParse, "no such window: %s", pWin->zName);
145094 return;
145095 }
145096 pWin->pPartition = sqlite3ExprListDup(pParse->db, p->pPartition, 0);
145097 pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0);
145098 pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0);
145099 pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0);
145100 pWin->eStart = p->eStart;
145101 pWin->eEnd = p->eEnd;
145102 pWin->eType = p->eType;
145103 }
145104 if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){
145105 sqlite3 *db = pParse->db;
145106 if( pWin->pFilter ){
145107 sqlite3ErrorMsg(pParse,
145108 "FILTER clause may only be used with aggregate window functions"
145109 );
145110 }else
145111 if( pFunc->zName==row_numberName || pFunc->zName==ntileName ){
145112 sqlite3ExprDelete(db, pWin->pStart);
145113 sqlite3ExprDelete(db, pWin->pEnd);
145114 pWin->pStart = pWin->pEnd = 0;
145115 pWin->eType = TK_ROWS;
145116 pWin->eStart = TK_UNBOUNDED;
145117 pWin->eEnd = TK_CURRENT;
145118 }else
145119
145120 if( pFunc->zName==dense_rankName || pFunc->zName==rankName
145121 || pFunc->zName==percent_rankName || pFunc->zName==cume_distName
145122 ){
145123 sqlite3ExprDelete(db, pWin->pStart);
145124 sqlite3ExprDelete(db, pWin->pEnd);
145125 pWin->pStart = pWin->pEnd = 0;
145126 pWin->eType = TK_RANGE;
145127 pWin->eStart = TK_UNBOUNDED;
145128 pWin->eEnd = TK_CURRENT;
145129 }
145130 }
145131 pWin->pFunc = pFunc;
145132}
145133
145134/*
145135** Context object passed through sqlite3WalkExprList() to
145136** selectWindowRewriteExprCb() by selectWindowRewriteEList().
145137*/
145138typedef struct WindowRewrite WindowRewrite;
145139struct WindowRewrite {
145140 Window *pWin;
145141 SrcList *pSrc;
145142 ExprList *pSub;
145143 Select *pSubSelect; /* Current sub-select, if any */
145144};
145145
145146/*
145147** Callback function used by selectWindowRewriteEList(). If necessary,
145148** this function appends to the output expression-list and updates
145149** expression (*ppExpr) in place.
145150*/
145151static int selectWindowRewriteExprCb(Walker *pWalker, Expr *pExpr){
145152 struct WindowRewrite *p = pWalker->u.pRewrite;
145153 Parse *pParse = pWalker->pParse;
145154
145155 /* If this function is being called from within a scalar sub-select
145156 ** that used by the SELECT statement being processed, only process
145157 ** TK_COLUMN expressions that refer to it (the outer SELECT). Do
145158 ** not process aggregates or window functions at all, as they belong
145159 ** to the scalar sub-select. */
145160 if( p->pSubSelect ){
145161 if( pExpr->op!=TK_COLUMN ){
145162 return WRC_Continue;
145163 }else{
145164 int nSrc = p->pSrc->nSrc;
145165 int i;
145166 for(i=0; i<nSrc; i++){
145167 if( pExpr->iTable==p->pSrc->a[i].iCursor ) break;
145168 }
145169 if( i==nSrc ) return WRC_Continue;
145170 }
145171 }
145172
145173 switch( pExpr->op ){
145174
145175 case TK_FUNCTION:
145176 if( !ExprHasProperty(pExpr, EP_WinFunc) ){
145177 break;
145178 }else{
145179 Window *pWin;
145180 for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
145181 if( pExpr->y.pWin==pWin ){
145182 assert( pWin->pOwner==pExpr );
145183 return WRC_Prune;
145184 }
145185 }
145186 }
145187 /* Fall through. */
145188
145189 case TK_AGG_FUNCTION:
145190 case TK_COLUMN: {
145191 Expr *pDup = sqlite3ExprDup(pParse->db, pExpr, 0);
145192 p->pSub = sqlite3ExprListAppend(pParse, p->pSub, pDup);
145193 if( p->pSub ){
145194 assert( ExprHasProperty(pExpr, EP_Static)==0 );
145195 ExprSetProperty(pExpr, EP_Static);
145196 sqlite3ExprDelete(pParse->db, pExpr);
145197 ExprClearProperty(pExpr, EP_Static);
145198 memset(pExpr, 0, sizeof(Expr));
145199
145200 pExpr->op = TK_COLUMN;
145201 pExpr->iColumn = p->pSub->nExpr-1;
145202 pExpr->iTable = p->pWin->iEphCsr;
145203 }
145204
145205 break;
145206 }
145207
145208 default: /* no-op */
145209 break;
145210 }
145211
145212 return WRC_Continue;
145213}
145214static int selectWindowRewriteSelectCb(Walker *pWalker, Select *pSelect){
145215 struct WindowRewrite *p = pWalker->u.pRewrite;
145216 Select *pSave = p->pSubSelect;
145217 if( pSave==pSelect ){
145218 return WRC_Continue;
145219 }else{
145220 p->pSubSelect = pSelect;
145221 sqlite3WalkSelect(pWalker, pSelect);
145222 p->pSubSelect = pSave;
145223 }
145224 return WRC_Prune;
145225}
145226
145227
145228/*
145229** Iterate through each expression in expression-list pEList. For each:
145230**
145231** * TK_COLUMN,
145232** * aggregate function, or
145233** * window function with a Window object that is not a member of the
145234** Window list passed as the second argument (pWin).
145235**
145236** Append the node to output expression-list (*ppSub). And replace it
145237** with a TK_COLUMN that reads the (N-1)th element of table
145238** pWin->iEphCsr, where N is the number of elements in (*ppSub) after
145239** appending the new one.
145240*/
145241static void selectWindowRewriteEList(
145242 Parse *pParse,
145243 Window *pWin,
145244 SrcList *pSrc,
145245 ExprList *pEList, /* Rewrite expressions in this list */
145246 ExprList **ppSub /* IN/OUT: Sub-select expression-list */
145247){
145248 Walker sWalker;
145249 WindowRewrite sRewrite;
145250
145251 memset(&sWalker, 0, sizeof(Walker));
145252 memset(&sRewrite, 0, sizeof(WindowRewrite));
145253
145254 sRewrite.pSub = *ppSub;
145255 sRewrite.pWin = pWin;
145256 sRewrite.pSrc = pSrc;
145257
145258 sWalker.pParse = pParse;
145259 sWalker.xExprCallback = selectWindowRewriteExprCb;
145260 sWalker.xSelectCallback = selectWindowRewriteSelectCb;
145261 sWalker.u.pRewrite = &sRewrite;
145262
145263 (void)sqlite3WalkExprList(&sWalker, pEList);
145264
145265 *ppSub = sRewrite.pSub;
145266}
145267
145268/*
145269** Append a copy of each expression in expression-list pAppend to
145270** expression list pList. Return a pointer to the result list.
145271*/
145272static ExprList *exprListAppendList(
145273 Parse *pParse, /* Parsing context */
145274 ExprList *pList, /* List to which to append. Might be NULL */
145275 ExprList *pAppend /* List of values to append. Might be NULL */
145276){
145277 if( pAppend ){
145278 int i;
145279 int nInit = pList ? pList->nExpr : 0;
145280 for(i=0; i<pAppend->nExpr; i++){
145281 Expr *pDup = sqlite3ExprDup(pParse->db, pAppend->a[i].pExpr, 0);
145282 pList = sqlite3ExprListAppend(pParse, pList, pDup);
145283 if( pList ) pList->a[nInit+i].sortOrder = pAppend->a[i].sortOrder;
145284 }
145285 }
145286 return pList;
145287}
145288
145289/*
145290** If the SELECT statement passed as the second argument does not invoke
145291** any SQL window functions, this function is a no-op. Otherwise, it
145292** rewrites the SELECT statement so that window function xStep functions
145293** are invoked in the correct order as described under "SELECT REWRITING"
145294** at the top of this file.
145295*/
145296SQLITE_PRIVATE int sqlite3WindowRewrite(Parse *pParse, Select *p){
145297 int rc = SQLITE_OK;
145298 if( p->pWin && p->pPrior==0 ){
145299 Vdbe *v = sqlite3GetVdbe(pParse);
145300 sqlite3 *db = pParse->db;
145301 Select *pSub = 0; /* The subquery */
145302 SrcList *pSrc = p->pSrc;
145303 Expr *pWhere = p->pWhere;
145304 ExprList *pGroupBy = p->pGroupBy;
145305 Expr *pHaving = p->pHaving;
145306 ExprList *pSort = 0;
145307
145308 ExprList *pSublist = 0; /* Expression list for sub-query */
145309 Window *pMWin = p->pWin; /* Master window object */
145310 Window *pWin; /* Window object iterator */
145311
145312 p->pSrc = 0;
145313 p->pWhere = 0;
145314 p->pGroupBy = 0;
145315 p->pHaving = 0;
145316
145317 /* Create the ORDER BY clause for the sub-select. This is the concatenation
145318 ** of the window PARTITION and ORDER BY clauses. Then, if this makes it
145319 ** redundant, remove the ORDER BY from the parent SELECT. */
145320 pSort = sqlite3ExprListDup(db, pMWin->pPartition, 0);
145321 pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy);
145322 if( pSort && p->pOrderBy ){
145323 if( sqlite3ExprListCompare(pSort, p->pOrderBy, -1)==0 ){
145324 sqlite3ExprListDelete(db, p->pOrderBy);
145325 p->pOrderBy = 0;
145326 }
145327 }
145328
145329 /* Assign a cursor number for the ephemeral table used to buffer rows.
145330 ** The OpenEphemeral instruction is coded later, after it is known how
145331 ** many columns the table will have. */
145332 pMWin->iEphCsr = pParse->nTab++;
145333
145334 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pEList, &pSublist);
145335 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pOrderBy, &pSublist);
145336 pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
145337
145338 /* Append the PARTITION BY and ORDER BY expressions to the to the
145339 ** sub-select expression list. They are required to figure out where
145340 ** boundaries for partitions and sets of peer rows lie. */
145341 pSublist = exprListAppendList(pParse, pSublist, pMWin->pPartition);
145342 pSublist = exprListAppendList(pParse, pSublist, pMWin->pOrderBy);
145343
145344 /* Append the arguments passed to each window function to the
145345 ** sub-select expression list. Also allocate two registers for each
145346 ** window function - one for the accumulator, another for interim
145347 ** results. */
145348 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
145349 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
145350 pSublist = exprListAppendList(pParse, pSublist, pWin->pOwner->x.pList);
145351 if( pWin->pFilter ){
145352 Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0);
145353 pSublist = sqlite3ExprListAppend(pParse, pSublist, pFilter);
145354 }
145355 pWin->regAccum = ++pParse->nMem;
145356 pWin->regResult = ++pParse->nMem;
145357 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
145358 }
145359
145360 /* If there is no ORDER BY or PARTITION BY clause, and the window
145361 ** function accepts zero arguments, and there are no other columns
145362 ** selected (e.g. "SELECT row_number() OVER () FROM t1"), it is possible
145363 ** that pSublist is still NULL here. Add a constant expression here to
145364 ** keep everything legal in this case.
145365 */
145366 if( pSublist==0 ){
145367 pSublist = sqlite3ExprListAppend(pParse, 0,
145368 sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0)
145369 );
145370 }
145371
145372 pSub = sqlite3SelectNew(
145373 pParse, pSublist, pSrc, pWhere, pGroupBy, pHaving, pSort, 0, 0
145374 );
145375 p->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
145376 assert( p->pSrc || db->mallocFailed );
145377 if( p->pSrc ){
145378 p->pSrc->a[0].pSelect = pSub;
145379 sqlite3SrcListAssignCursors(pParse, p->pSrc);
145380 if( sqlite3ExpandSubquery(pParse, &p->pSrc->a[0]) ){
145381 rc = SQLITE_NOMEM;
145382 }else{
145383 pSub->selFlags |= SF_Expanded;
145384 p->selFlags &= ~SF_Aggregate;
145385 sqlite3SelectPrep(pParse, pSub, 0);
145386 }
145387
145388 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, pSublist->nExpr);
145389 }else{
145390 sqlite3SelectDelete(db, pSub);
145391 }
145392 if( db->mallocFailed ) rc = SQLITE_NOMEM;
145393 }
145394
145395 return rc;
145396}
145397
145398/*
145399** Free the Window object passed as the second argument.
145400*/
145401SQLITE_PRIVATE void sqlite3WindowDelete(sqlite3 *db, Window *p){
145402 if( p ){
145403 sqlite3ExprDelete(db, p->pFilter);
145404 sqlite3ExprListDelete(db, p->pPartition);
145405 sqlite3ExprListDelete(db, p->pOrderBy);
145406 sqlite3ExprDelete(db, p->pEnd);
145407 sqlite3ExprDelete(db, p->pStart);
145408 sqlite3DbFree(db, p->zName);
145409 sqlite3DbFree(db, p);
145410 }
145411}
145412
145413/*
145414** Free the linked list of Window objects starting at the second argument.
145415*/
145416SQLITE_PRIVATE void sqlite3WindowListDelete(sqlite3 *db, Window *p){
145417 while( p ){
145418 Window *pNext = p->pNextWin;
145419 sqlite3WindowDelete(db, p);
145420 p = pNext;
145421 }
145422}
145423
145424/*
145425** The argument expression is an PRECEDING or FOLLOWING offset. The
145426** value should be a non-negative integer. If the value is not a
145427** constant, change it to NULL. The fact that it is then a non-negative
145428** integer will be caught later. But it is important not to leave
145429** variable values in the expression tree.
145430*/
145431static Expr *sqlite3WindowOffsetExpr(Parse *pParse, Expr *pExpr){
145432 if( 0==sqlite3ExprIsConstant(pExpr) ){
145433 sqlite3ExprDelete(pParse->db, pExpr);
145434 pExpr = sqlite3ExprAlloc(pParse->db, TK_NULL, 0, 0);
145435 }
145436 return pExpr;
145437}
145438
145439/*
145440** Allocate and return a new Window object describing a Window Definition.
145441*/
145442SQLITE_PRIVATE Window *sqlite3WindowAlloc(
145443 Parse *pParse, /* Parsing context */
145444 int eType, /* Frame type. TK_RANGE or TK_ROWS */
145445 int eStart, /* Start type: CURRENT, PRECEDING, FOLLOWING, UNBOUNDED */
145446 Expr *pStart, /* Start window size if TK_PRECEDING or FOLLOWING */
145447 int eEnd, /* End type: CURRENT, FOLLOWING, TK_UNBOUNDED, PRECEDING */
145448 Expr *pEnd /* End window size if TK_FOLLOWING or PRECEDING */
145449){
145450 Window *pWin = 0;
145451
145452 /* Parser assures the following: */
145453 assert( eType==TK_RANGE || eType==TK_ROWS );
145454 assert( eStart==TK_CURRENT || eStart==TK_PRECEDING
145455 || eStart==TK_UNBOUNDED || eStart==TK_FOLLOWING );
145456 assert( eEnd==TK_CURRENT || eEnd==TK_FOLLOWING
145457 || eEnd==TK_UNBOUNDED || eEnd==TK_PRECEDING );
145458 assert( (eStart==TK_PRECEDING || eStart==TK_FOLLOWING)==(pStart!=0) );
145459 assert( (eEnd==TK_FOLLOWING || eEnd==TK_PRECEDING)==(pEnd!=0) );
145460
145461
145462 /* If a frame is declared "RANGE" (not "ROWS"), then it may not use
145463 ** either "<expr> PRECEDING" or "<expr> FOLLOWING".
145464 */
145465 if( eType==TK_RANGE && (pStart!=0 || pEnd!=0) ){
145466 sqlite3ErrorMsg(pParse, "RANGE must use only UNBOUNDED or CURRENT ROW");
145467 goto windowAllocErr;
145468 }
145469
145470 /* Additionally, the
145471 ** starting boundary type may not occur earlier in the following list than
145472 ** the ending boundary type:
145473 **
145474 ** UNBOUNDED PRECEDING
145475 ** <expr> PRECEDING
145476 ** CURRENT ROW
145477 ** <expr> FOLLOWING
145478 ** UNBOUNDED FOLLOWING
145479 **
145480 ** The parser ensures that "UNBOUNDED PRECEDING" cannot be used as an ending
145481 ** boundary, and than "UNBOUNDED FOLLOWING" cannot be used as a starting
145482 ** frame boundary.
145483 */
145484 if( (eStart==TK_CURRENT && eEnd==TK_PRECEDING)
145485 || (eStart==TK_FOLLOWING && (eEnd==TK_PRECEDING || eEnd==TK_CURRENT))
145486 ){
145487 sqlite3ErrorMsg(pParse, "unsupported frame delimiter for ROWS");
145488 goto windowAllocErr;
145489 }
145490
145491 pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
145492 if( pWin==0 ) goto windowAllocErr;
145493 pWin->eType = eType;
145494 pWin->eStart = eStart;
145495 pWin->eEnd = eEnd;
145496 pWin->pEnd = sqlite3WindowOffsetExpr(pParse, pEnd);
145497 pWin->pStart = sqlite3WindowOffsetExpr(pParse, pStart);
145498 return pWin;
145499
145500windowAllocErr:
145501 sqlite3ExprDelete(pParse->db, pEnd);
145502 sqlite3ExprDelete(pParse->db, pStart);
145503 return 0;
145504}
145505
145506/*
145507** Attach window object pWin to expression p.
145508*/
145509SQLITE_PRIVATE void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){
145510 if( p ){
145511 assert( p->op==TK_FUNCTION );
145512 /* This routine is only called for the parser. If pWin was not
145513 ** allocated due to an OOM, then the parser would fail before ever
145514 ** invoking this routine */
145515 if( ALWAYS(pWin) ){
145516 p->y.pWin = pWin;
145517 ExprSetProperty(p, EP_WinFunc);
145518 pWin->pOwner = p;
145519 if( p->flags & EP_Distinct ){
145520 sqlite3ErrorMsg(pParse,
145521 "DISTINCT is not supported for window functions");
145522 }
145523 }
145524 }else{
145525 sqlite3WindowDelete(pParse->db, pWin);
145526 }
145527}
145528
145529/*
145530** Return 0 if the two window objects are identical, or non-zero otherwise.
145531** Identical window objects can be processed in a single scan.
145532*/
145533SQLITE_PRIVATE int sqlite3WindowCompare(Parse *pParse, Window *p1, Window *p2){
145534 if( p1->eType!=p2->eType ) return 1;
145535 if( p1->eStart!=p2->eStart ) return 1;
145536 if( p1->eEnd!=p2->eEnd ) return 1;
145537 if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1;
145538 if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1;
145539 if( sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1) ) return 1;
145540 if( sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1) ) return 1;
145541 return 0;
145542}
145543
145544
145545/*
145546** This is called by code in select.c before it calls sqlite3WhereBegin()
145547** to begin iterating through the sub-query results. It is used to allocate
145548** and initialize registers and cursors used by sqlite3WindowCodeStep().
145549*/
145550SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse *pParse, Window *pMWin){
145551 Window *pWin;
145552 Vdbe *v = sqlite3GetVdbe(pParse);
145553 int nPart = (pMWin->pPartition ? pMWin->pPartition->nExpr : 0);
145554 nPart += (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
145555 if( nPart ){
145556 pMWin->regPart = pParse->nMem+1;
145557 pParse->nMem += nPart;
145558 sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nPart-1);
145559 }
145560
145561 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
145562 FuncDef *p = pWin->pFunc;
145563 if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){
145564 /* The inline versions of min() and max() require a single ephemeral
145565 ** table and 3 registers. The registers are used as follows:
145566 **
145567 ** regApp+0: slot to copy min()/max() argument to for MakeRecord
145568 ** regApp+1: integer value used to ensure keys are unique
145569 ** regApp+2: output of MakeRecord
145570 */
145571 ExprList *pList = pWin->pOwner->x.pList;
145572 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList, 0, 0);
145573 pWin->csrApp = pParse->nTab++;
145574 pWin->regApp = pParse->nMem+1;
145575 pParse->nMem += 3;
145576 if( pKeyInfo && pWin->pFunc->zName[1]=='i' ){
145577 assert( pKeyInfo->aSortOrder[0]==0 );
145578 pKeyInfo->aSortOrder[0] = 1;
145579 }
145580 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pWin->csrApp, 2);
145581 sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO);
145582 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
145583 }
145584 else if( p->zName==nth_valueName || p->zName==first_valueName ){
145585 /* Allocate two registers at pWin->regApp. These will be used to
145586 ** store the start and end index of the current frame. */
145587 assert( pMWin->iEphCsr );
145588 pWin->regApp = pParse->nMem+1;
145589 pWin->csrApp = pParse->nTab++;
145590 pParse->nMem += 2;
145591 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
145592 }
145593 else if( p->zName==leadName || p->zName==lagName ){
145594 assert( pMWin->iEphCsr );
145595 pWin->csrApp = pParse->nTab++;
145596 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
145597 }
145598 }
145599}
145600
145601/*
145602** A "PRECEDING <expr>" (eCond==0) or "FOLLOWING <expr>" (eCond==1) or the
145603** value of the second argument to nth_value() (eCond==2) has just been
145604** evaluated and the result left in register reg. This function generates VM
145605** code to check that the value is a non-negative integer and throws an
145606** exception if it is not.
145607*/
145608static void windowCheckIntValue(Parse *pParse, int reg, int eCond){
145609 static const char *azErr[] = {
145610 "frame starting offset must be a non-negative integer",
145611 "frame ending offset must be a non-negative integer",
145612 "second argument to nth_value must be a positive integer"
145613 };
145614 static int aOp[] = { OP_Ge, OP_Ge, OP_Gt };
145615 Vdbe *v = sqlite3GetVdbe(pParse);
145616 int regZero = sqlite3GetTempReg(pParse);
145617 assert( eCond==0 || eCond==1 || eCond==2 );
145618 sqlite3VdbeAddOp2(v, OP_Integer, 0, regZero);
145619 sqlite3VdbeAddOp2(v, OP_MustBeInt, reg, sqlite3VdbeCurrentAddr(v)+2);
145620 VdbeCoverageIf(v, eCond==0);
145621 VdbeCoverageIf(v, eCond==1);
145622 VdbeCoverageIf(v, eCond==2);
145623 sqlite3VdbeAddOp3(v, aOp[eCond], regZero, sqlite3VdbeCurrentAddr(v)+2, reg);
145624 VdbeCoverageNeverNullIf(v, eCond==0);
145625 VdbeCoverageNeverNullIf(v, eCond==1);
145626 VdbeCoverageNeverNullIf(v, eCond==2);
145627 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_ERROR, OE_Abort);
145628 sqlite3VdbeAppendP4(v, (void*)azErr[eCond], P4_STATIC);
145629 sqlite3ReleaseTempReg(pParse, regZero);
145630}
145631
145632/*
145633** Return the number of arguments passed to the window-function associated
145634** with the object passed as the only argument to this function.
145635*/
145636static int windowArgCount(Window *pWin){
145637 ExprList *pList = pWin->pOwner->x.pList;
145638 return (pList ? pList->nExpr : 0);
145639}
145640
145641/*
145642** Generate VM code to invoke either xStep() (if bInverse is 0) or
145643** xInverse (if bInverse is non-zero) for each window function in the
145644** linked list starting at pMWin. Or, for built-in window functions
145645** that do not use the standard function API, generate the required
145646** inline VM code.
145647**
145648** If argument csr is greater than or equal to 0, then argument reg is
145649** the first register in an array of registers guaranteed to be large
145650** enough to hold the array of arguments for each function. In this case
145651** the arguments are extracted from the current row of csr into the
145652** array of registers before invoking OP_AggStep or OP_AggInverse
145653**
145654** Or, if csr is less than zero, then the array of registers at reg is
145655** already populated with all columns from the current row of the sub-query.
145656**
145657** If argument regPartSize is non-zero, then it is a register containing the
145658** number of rows in the current partition.
145659*/
145660static void windowAggStep(
145661 Parse *pParse,
145662 Window *pMWin, /* Linked list of window functions */
145663 int csr, /* Read arguments from this cursor */
145664 int bInverse, /* True to invoke xInverse instead of xStep */
145665 int reg, /* Array of registers */
145666 int regPartSize /* Register containing size of partition */
145667){
145668 Vdbe *v = sqlite3GetVdbe(pParse);
145669 Window *pWin;
145670 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
145671 int flags = pWin->pFunc->funcFlags;
145672 int regArg;
145673 int nArg = windowArgCount(pWin);
145674
145675 if( csr>=0 ){
145676 int i;
145677 for(i=0; i<nArg; i++){
145678 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+i, reg+i);
145679 }
145680 regArg = reg;
145681 if( flags & SQLITE_FUNC_WINDOW_SIZE ){
145682 if( nArg==0 ){
145683 regArg = regPartSize;
145684 }else{
145685 sqlite3VdbeAddOp2(v, OP_SCopy, regPartSize, reg+nArg);
145686 }
145687 nArg++;
145688 }
145689 }else{
145690 assert( !(flags & SQLITE_FUNC_WINDOW_SIZE) );
145691 regArg = reg + pWin->iArgCol;
145692 }
145693
145694 if( (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX)
145695 && pWin->eStart!=TK_UNBOUNDED
145696 ){
145697 int addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regArg);
145698 VdbeCoverage(v);
145699 if( bInverse==0 ){
145700 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1, 1);
145701 sqlite3VdbeAddOp2(v, OP_SCopy, regArg, pWin->regApp);
145702 sqlite3VdbeAddOp3(v, OP_MakeRecord, pWin->regApp, 2, pWin->regApp+2);
145703 sqlite3VdbeAddOp2(v, OP_IdxInsert, pWin->csrApp, pWin->regApp+2);
145704 }else{
145705 sqlite3VdbeAddOp4Int(v, OP_SeekGE, pWin->csrApp, 0, regArg, 1);
145706 VdbeCoverageNeverTaken(v);
145707 sqlite3VdbeAddOp1(v, OP_Delete, pWin->csrApp);
145708 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
145709 }
145710 sqlite3VdbeJumpHere(v, addrIsNull);
145711 }else if( pWin->regApp ){
145712 assert( pWin->pFunc->zName==nth_valueName
145713 || pWin->pFunc->zName==first_valueName
145714 );
145715 assert( bInverse==0 || bInverse==1 );
145716 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1);
145717 }else if( pWin->pFunc->zName==leadName
145718 || pWin->pFunc->zName==lagName
145719 ){
145720 /* no-op */
145721 }else{
145722 int addrIf = 0;
145723 if( pWin->pFilter ){
145724 int regTmp;
145725 assert( nArg==0 || nArg==pWin->pOwner->x.pList->nExpr );
145726 assert( nArg || pWin->pOwner->x.pList==0 );
145727 if( csr>0 ){
145728 regTmp = sqlite3GetTempReg(pParse);
145729 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
145730 }else{
145731 regTmp = regArg + nArg;
145732 }
145733 addrIf = sqlite3VdbeAddOp3(v, OP_IfNot, regTmp, 0, 1);
145734 VdbeCoverage(v);
145735 if( csr>0 ){
145736 sqlite3ReleaseTempReg(pParse, regTmp);
145737 }
145738 }
145739 if( pWin->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
145740 CollSeq *pColl;
145741 assert( nArg>0 );
145742 pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
145743 sqlite3VdbeAddOp4(v, OP_CollSeq, 0,0,0, (const char*)pColl, P4_COLLSEQ);
145744 }
145745 sqlite3VdbeAddOp3(v, bInverse? OP_AggInverse : OP_AggStep,
145746 bInverse, regArg, pWin->regAccum);
145747 sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
145748 sqlite3VdbeChangeP5(v, (u8)nArg);
145749 if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
145750 }
145751 }
145752}
145753
145754/*
145755** Generate VM code to invoke either xValue() (bFinal==0) or xFinalize()
145756** (bFinal==1) for each window function in the linked list starting at
145757** pMWin. Or, for built-in window-functions that do not use the standard
145758** API, generate the equivalent VM code.
145759*/
145760static void windowAggFinal(Parse *pParse, Window *pMWin, int bFinal){
145761 Vdbe *v = sqlite3GetVdbe(pParse);
145762 Window *pWin;
145763
145764 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
145765 if( (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX)
145766 && pWin->eStart!=TK_UNBOUNDED
145767 ){
145768 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
145769 sqlite3VdbeAddOp1(v, OP_Last, pWin->csrApp);
145770 VdbeCoverage(v);
145771 sqlite3VdbeAddOp3(v, OP_Column, pWin->csrApp, 0, pWin->regResult);
145772 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
145773 if( bFinal ){
145774 sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
145775 }
145776 }else if( pWin->regApp ){
145777 }else{
145778 if( bFinal ){
145779 sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, windowArgCount(pWin));
145780 sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
145781 sqlite3VdbeAddOp2(v, OP_Copy, pWin->regAccum, pWin->regResult);
145782 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
145783 }else{
145784 sqlite3VdbeAddOp3(v, OP_AggValue, pWin->regAccum, windowArgCount(pWin),
145785 pWin->regResult);
145786 sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
145787 }
145788 }
145789 }
145790}
145791
145792/*
145793** This function generates VM code to invoke the sub-routine at address
145794** lblFlushPart once for each partition with the entire partition cached in
145795** the Window.iEphCsr temp table.
145796*/
145797static void windowPartitionCache(
145798 Parse *pParse,
145799 Select *p, /* The rewritten SELECT statement */
145800 WhereInfo *pWInfo, /* WhereInfo to call WhereEnd() on */
145801 int regFlushPart, /* Register to use with Gosub lblFlushPart */
145802 int lblFlushPart, /* Subroutine to Gosub to */
145803 int *pRegSize /* OUT: Register containing partition size */
145804){
145805 Window *pMWin = p->pWin;
145806 Vdbe *v = sqlite3GetVdbe(pParse);
145807 int iSubCsr = p->pSrc->a[0].iCursor;
145808 int nSub = p->pSrc->a[0].pTab->nCol;
145809 int k;
145810
145811 int reg = pParse->nMem+1;
145812 int regRecord = reg+nSub;
145813 int regRowid = regRecord+1;
145814
145815 *pRegSize = regRowid;
145816 pParse->nMem += nSub + 2;
145817
145818 /* Load the column values for the row returned by the sub-select
145819 ** into an array of registers starting at reg. */
145820 for(k=0; k<nSub; k++){
145821 sqlite3VdbeAddOp3(v, OP_Column, iSubCsr, k, reg+k);
145822 }
145823 sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, nSub, regRecord);
145824
145825 /* Check if this is the start of a new partition. If so, call the
145826 ** flush_partition sub-routine. */
145827 if( pMWin->pPartition ){
145828 int addr;
145829 ExprList *pPart = pMWin->pPartition;
145830 int nPart = pPart->nExpr;
145831 int regNewPart = reg + pMWin->nBufferCol;
145832 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
145833
145834 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart,nPart);
145835 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
145836 sqlite3VdbeAddOp3(v, OP_Jump, addr+2, addr+4, addr+2);
145837 VdbeCoverageEqNe(v);
145838 sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1);
145839 sqlite3VdbeAddOp2(v, OP_Gosub, regFlushPart, lblFlushPart);
145840 VdbeComment((v, "call flush_partition"));
145841 }
145842
145843 /* Buffer the current row in the ephemeral table. */
145844 sqlite3VdbeAddOp2(v, OP_NewRowid, pMWin->iEphCsr, regRowid);
145845 sqlite3VdbeAddOp3(v, OP_Insert, pMWin->iEphCsr, regRecord, regRowid);
145846
145847 /* End of the input loop */
145848 sqlite3WhereEnd(pWInfo);
145849
145850 /* Invoke "flush_partition" to deal with the final (or only) partition */
145851 sqlite3VdbeAddOp2(v, OP_Gosub, regFlushPart, lblFlushPart);
145852 VdbeComment((v, "call flush_partition"));
145853}
145854
145855/*
145856** Invoke the sub-routine at regGosub (generated by code in select.c) to
145857** return the current row of Window.iEphCsr. If all window functions are
145858** aggregate window functions that use the standard API, a single
145859** OP_Gosub instruction is all that this routine generates. Extra VM code
145860** for per-row processing is only generated for the following built-in window
145861** functions:
145862**
145863** nth_value()
145864** first_value()
145865** lag()
145866** lead()
145867*/
145868static void windowReturnOneRow(
145869 Parse *pParse,
145870 Window *pMWin,
145871 int regGosub,
145872 int addrGosub
145873){
145874 Vdbe *v = sqlite3GetVdbe(pParse);
145875 Window *pWin;
145876 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
145877 FuncDef *pFunc = pWin->pFunc;
145878 if( pFunc->zName==nth_valueName
145879 || pFunc->zName==first_valueName
145880 ){
145881 int csr = pWin->csrApp;
145882 int lbl = sqlite3VdbeMakeLabel(v);
145883 int tmpReg = sqlite3GetTempReg(pParse);
145884 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
145885
145886 if( pFunc->zName==nth_valueName ){
145887 sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+1,tmpReg);
145888 windowCheckIntValue(pParse, tmpReg, 2);
145889 }else{
145890 sqlite3VdbeAddOp2(v, OP_Integer, 1, tmpReg);
145891 }
145892 sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg);
145893 sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg);
145894 VdbeCoverageNeverNull(v);
145895 sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, 0, tmpReg);
145896 VdbeCoverageNeverTaken(v);
145897 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
145898 sqlite3VdbeResolveLabel(v, lbl);
145899 sqlite3ReleaseTempReg(pParse, tmpReg);
145900 }
145901 else if( pFunc->zName==leadName || pFunc->zName==lagName ){
145902 int nArg = pWin->pOwner->x.pList->nExpr;
145903 int iEph = pMWin->iEphCsr;
145904 int csr = pWin->csrApp;
145905 int lbl = sqlite3VdbeMakeLabel(v);
145906 int tmpReg = sqlite3GetTempReg(pParse);
145907
145908 if( nArg<3 ){
145909 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
145910 }else{
145911 sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+2, pWin->regResult);
145912 }
145913 sqlite3VdbeAddOp2(v, OP_Rowid, iEph, tmpReg);
145914 if( nArg<2 ){
145915 int val = (pFunc->zName==leadName ? 1 : -1);
145916 sqlite3VdbeAddOp2(v, OP_AddImm, tmpReg, val);
145917 }else{
145918 int op = (pFunc->zName==leadName ? OP_Add : OP_Subtract);
145919 int tmpReg2 = sqlite3GetTempReg(pParse);
145920 sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+1, tmpReg2);
145921 sqlite3VdbeAddOp3(v, op, tmpReg2, tmpReg, tmpReg);
145922 sqlite3ReleaseTempReg(pParse, tmpReg2);
145923 }
145924
145925 sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, lbl, tmpReg);
145926 VdbeCoverage(v);
145927 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
145928 sqlite3VdbeResolveLabel(v, lbl);
145929 sqlite3ReleaseTempReg(pParse, tmpReg);
145930 }
145931 }
145932 sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
145933}
145934
145935/*
145936** Invoke the code generated by windowReturnOneRow() and, optionally, the
145937** xInverse() function for each window function, for one or more rows
145938** from the Window.iEphCsr temp table. This routine generates VM code
145939** similar to:
145940**
145941** while( regCtr>0 ){
145942** regCtr--;
145943** windowReturnOneRow()
145944** if( bInverse ){
145945** AggInverse
145946** }
145947** Next (Window.iEphCsr)
145948** }
145949*/
145950static void windowReturnRows(
145951 Parse *pParse,
145952 Window *pMWin, /* List of window functions */
145953 int regCtr, /* Register containing number of rows */
145954 int regGosub, /* Register for Gosub addrGosub */
145955 int addrGosub, /* Address of sub-routine for ReturnOneRow */
145956 int regInvArg, /* Array of registers for xInverse args */
145957 int regInvSize /* Register containing size of partition */
145958){
145959 int addr;
145960 Vdbe *v = sqlite3GetVdbe(pParse);
145961 windowAggFinal(pParse, pMWin, 0);
145962 addr = sqlite3VdbeAddOp3(v, OP_IfPos, regCtr, sqlite3VdbeCurrentAddr(v)+2 ,1);
145963 VdbeCoverage(v);
145964 sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
145965 windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);
145966 if( regInvArg ){
145967 windowAggStep(pParse, pMWin, pMWin->iEphCsr, 1, regInvArg, regInvSize);
145968 }
145969 sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, addr);
145970 VdbeCoverage(v);
145971 sqlite3VdbeJumpHere(v, addr+1); /* The OP_Goto */
145972}
145973
145974/*
145975** Generate code to set the accumulator register for each window function
145976** in the linked list passed as the second argument to NULL. And perform
145977** any equivalent initialization required by any built-in window functions
145978** in the list.
145979*/
145980static int windowInitAccum(Parse *pParse, Window *pMWin){
145981 Vdbe *v = sqlite3GetVdbe(pParse);
145982 int regArg;
145983 int nArg = 0;
145984 Window *pWin;
145985 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
145986 FuncDef *pFunc = pWin->pFunc;
145987 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
145988 nArg = MAX(nArg, windowArgCount(pWin));
145989 if( pFunc->zName==nth_valueName
145990 || pFunc->zName==first_valueName
145991 ){
145992 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
145993 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
145994 }
145995
145996 if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){
145997 assert( pWin->eStart!=TK_UNBOUNDED );
145998 sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
145999 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
146000 }
146001 }
146002 regArg = pParse->nMem+1;
146003 pParse->nMem += nArg;
146004 return regArg;
146005}
146006
146007
146008/*
146009** This function does the work of sqlite3WindowCodeStep() for all "ROWS"
146010** window frame types except for "BETWEEN UNBOUNDED PRECEDING AND CURRENT
146011** ROW". Pseudo-code for each follows.
146012**
146013** ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
146014**
146015** ...
146016** if( new partition ){
146017** Gosub flush_partition
146018** }
146019** Insert (record in eph-table)
146020** sqlite3WhereEnd()
146021** Gosub flush_partition
146022**
146023** flush_partition:
146024** Once {
146025** OpenDup (iEphCsr -> csrStart)
146026** OpenDup (iEphCsr -> csrEnd)
146027** }
146028** regStart = <expr1> // PRECEDING expression
146029** regEnd = <expr2> // FOLLOWING expression
146030** if( regStart<0 || regEnd<0 ){ error! }
146031** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done
146032** Next(csrEnd) // if EOF skip Aggstep
146033** Aggstep (csrEnd)
146034** if( (regEnd--)<=0 ){
146035** AggFinal (xValue)
146036** Gosub addrGosub
146037** Next(csr) // if EOF goto flush_partition_done
146038** if( (regStart--)<=0 ){
146039** AggInverse (csrStart)
146040** Next(csrStart)
146041** }
146042** }
146043** flush_partition_done:
146044** ResetSorter (csr)
146045** Return
146046**
146047** ROWS BETWEEN <expr> PRECEDING AND CURRENT ROW
146048** ROWS BETWEEN CURRENT ROW AND <expr> FOLLOWING
146049** ROWS BETWEEN UNBOUNDED PRECEDING AND <expr> FOLLOWING
146050**
146051** These are similar to the above. For "CURRENT ROW", intialize the
146052** register to 0. For "UNBOUNDED PRECEDING" to infinity.
146053**
146054** ROWS BETWEEN <expr> PRECEDING AND UNBOUNDED FOLLOWING
146055** ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
146056**
146057** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done
146058** while( 1 ){
146059** Next(csrEnd) // Exit while(1) at EOF
146060** Aggstep (csrEnd)
146061** }
146062** while( 1 ){
146063** AggFinal (xValue)
146064** Gosub addrGosub
146065** Next(csr) // if EOF goto flush_partition_done
146066** if( (regStart--)<=0 ){
146067** AggInverse (csrStart)
146068** Next(csrStart)
146069** }
146070** }
146071**
146072** For the "CURRENT ROW AND UNBOUNDED FOLLOWING" case, the final if()
146073** condition is always true (as if regStart were initialized to 0).
146074**
146075** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
146076**
146077** This is the only RANGE case handled by this routine. It modifies the
146078** second while( 1 ) loop in "ROWS BETWEEN CURRENT ... UNBOUNDED..." to
146079** be:
146080**
146081** while( 1 ){
146082** AggFinal (xValue)
146083** while( 1 ){
146084** regPeer++
146085** Gosub addrGosub
146086** Next(csr) // if EOF goto flush_partition_done
146087** if( new peer ) break;
146088** }
146089** while( (regPeer--)>0 ){
146090** AggInverse (csrStart)
146091** Next(csrStart)
146092** }
146093** }
146094**
146095** ROWS BETWEEN <expr> FOLLOWING AND <expr> FOLLOWING
146096**
146097** regEnd = regEnd - regStart
146098** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done
146099** Aggstep (csrEnd)
146100** Next(csrEnd) // if EOF fall-through
146101** if( (regEnd--)<=0 ){
146102** if( (regStart--)<=0 ){
146103** AggFinal (xValue)
146104** Gosub addrGosub
146105** Next(csr) // if EOF goto flush_partition_done
146106** }
146107** AggInverse (csrStart)
146108** Next (csrStart)
146109** }
146110**
146111** ROWS BETWEEN <expr> PRECEDING AND <expr> PRECEDING
146112**
146113** Replace the bit after "Rewind" in the above with:
146114**
146115** if( (regEnd--)<=0 ){
146116** AggStep (csrEnd)
146117** Next (csrEnd)
146118** }
146119** AggFinal (xValue)
146120** Gosub addrGosub
146121** Next(csr) // if EOF goto flush_partition_done
146122** if( (regStart--)<=0 ){
146123** AggInverse (csr2)
146124** Next (csr2)
146125** }
146126**
146127*/
146128static void windowCodeRowExprStep(
146129 Parse *pParse,
146130 Select *p,
146131 WhereInfo *pWInfo,
146132 int regGosub,
146133 int addrGosub
146134){
146135 Window *pMWin = p->pWin;
146136 Vdbe *v = sqlite3GetVdbe(pParse);
146137 int regFlushPart; /* Register for "Gosub flush_partition" */
146138 int lblFlushPart; /* Label for "Gosub flush_partition" */
146139 int lblFlushDone; /* Label for "Gosub flush_partition_done" */
146140
146141 int regArg;
146142 int addr;
146143 int csrStart = pParse->nTab++;
146144 int csrEnd = pParse->nTab++;
146145 int regStart; /* Value of <expr> PRECEDING */
146146 int regEnd; /* Value of <expr> FOLLOWING */
146147 int addrGoto;
146148 int addrTop;
146149 int addrIfPos1 = 0;
146150 int addrIfPos2 = 0;
146151 int regSize = 0;
146152
146153 assert( pMWin->eStart==TK_PRECEDING
146154 || pMWin->eStart==TK_CURRENT
146155 || pMWin->eStart==TK_FOLLOWING
146156 || pMWin->eStart==TK_UNBOUNDED
146157 );
146158 assert( pMWin->eEnd==TK_FOLLOWING
146159 || pMWin->eEnd==TK_CURRENT
146160 || pMWin->eEnd==TK_UNBOUNDED
146161 || pMWin->eEnd==TK_PRECEDING
146162 );
146163
146164 /* Allocate register and label for the "flush_partition" sub-routine. */
146165 regFlushPart = ++pParse->nMem;
146166 lblFlushPart = sqlite3VdbeMakeLabel(v);
146167 lblFlushDone = sqlite3VdbeMakeLabel(v);
146168
146169 regStart = ++pParse->nMem;
146170 regEnd = ++pParse->nMem;
146171
146172 windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart, &regSize);
146173
146174 addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
146175
146176 /* Start of "flush_partition" */
146177 sqlite3VdbeResolveLabel(v, lblFlushPart);
146178 sqlite3VdbeAddOp2(v, OP_Once, 0, sqlite3VdbeCurrentAddr(v)+3);
146179 VdbeCoverage(v);
146180 VdbeComment((v, "Flush_partition subroutine"));
146181 sqlite3VdbeAddOp2(v, OP_OpenDup, csrStart, pMWin->iEphCsr);
146182 sqlite3VdbeAddOp2(v, OP_OpenDup, csrEnd, pMWin->iEphCsr);
146183
146184 /* If either regStart or regEnd are not non-negative integers, throw
146185 ** an exception. */
146186 if( pMWin->pStart ){
146187 sqlite3ExprCode(pParse, pMWin->pStart, regStart);
146188 windowCheckIntValue(pParse, regStart, 0);
146189 }
146190 if( pMWin->pEnd ){
146191 sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
146192 windowCheckIntValue(pParse, regEnd, 1);
146193 }
146194
146195 /* If this is "ROWS <expr1> FOLLOWING AND ROWS <expr2> FOLLOWING", do:
146196 **
146197 ** if( regEnd<regStart ){
146198 ** // The frame always consists of 0 rows
146199 ** regStart = regSize;
146200 ** }
146201 ** regEnd = regEnd - regStart;
146202 */
146203 if( pMWin->pEnd && pMWin->eStart==TK_FOLLOWING ){
146204 assert( pMWin->pStart!=0 );
146205 assert( pMWin->eEnd==TK_FOLLOWING );
146206 sqlite3VdbeAddOp3(v, OP_Ge, regStart, sqlite3VdbeCurrentAddr(v)+2, regEnd);
146207 VdbeCoverageNeverNull(v);
146208 sqlite3VdbeAddOp2(v, OP_Copy, regSize, regStart);
146209 sqlite3VdbeAddOp3(v, OP_Subtract, regStart, regEnd, regEnd);
146210 }
146211
146212 if( pMWin->pStart && pMWin->eEnd==TK_PRECEDING ){
146213 assert( pMWin->pEnd!=0 );
146214 assert( pMWin->eStart==TK_PRECEDING );
146215 sqlite3VdbeAddOp3(v, OP_Le, regStart, sqlite3VdbeCurrentAddr(v)+3, regEnd);
146216 VdbeCoverageNeverNull(v);
146217 sqlite3VdbeAddOp2(v, OP_Copy, regSize, regStart);
146218 sqlite3VdbeAddOp2(v, OP_Copy, regSize, regEnd);
146219 }
146220
146221 /* Initialize the accumulator register for each window function to NULL */
146222 regArg = windowInitAccum(pParse, pMWin);
146223
146224 sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr, lblFlushDone);
146225 VdbeCoverage(v);
146226 sqlite3VdbeAddOp2(v, OP_Rewind, csrStart, lblFlushDone);
146227 VdbeCoverageNeverTaken(v);
146228 sqlite3VdbeChangeP5(v, 1);
146229 sqlite3VdbeAddOp2(v, OP_Rewind, csrEnd, lblFlushDone);
146230 VdbeCoverageNeverTaken(v);
146231 sqlite3VdbeChangeP5(v, 1);
146232
146233 /* Invoke AggStep function for each window function using the row that
146234 ** csrEnd currently points to. Or, if csrEnd is already at EOF,
146235 ** do nothing. */
146236 addrTop = sqlite3VdbeCurrentAddr(v);
146237 if( pMWin->eEnd==TK_PRECEDING ){
146238 addrIfPos1 = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0 , 1);
146239 VdbeCoverage(v);
146240 }
146241 sqlite3VdbeAddOp2(v, OP_Next, csrEnd, sqlite3VdbeCurrentAddr(v)+2);
146242 VdbeCoverage(v);
146243 addr = sqlite3VdbeAddOp0(v, OP_Goto);
146244 windowAggStep(pParse, pMWin, csrEnd, 0, regArg, regSize);
146245 if( pMWin->eEnd==TK_UNBOUNDED ){
146246 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
146247 sqlite3VdbeJumpHere(v, addr);
146248 addrTop = sqlite3VdbeCurrentAddr(v);
146249 }else{
146250 sqlite3VdbeJumpHere(v, addr);
146251 if( pMWin->eEnd==TK_PRECEDING ){
146252 sqlite3VdbeJumpHere(v, addrIfPos1);
146253 }
146254 }
146255
146256 if( pMWin->eEnd==TK_FOLLOWING ){
146257 addrIfPos1 = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0 , 1);
146258 VdbeCoverage(v);
146259 }
146260 if( pMWin->eStart==TK_FOLLOWING ){
146261 addrIfPos2 = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0 , 1);
146262 VdbeCoverage(v);
146263 }
146264 windowAggFinal(pParse, pMWin, 0);
146265 windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);
146266 sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)+2);
146267 VdbeCoverage(v);
146268 sqlite3VdbeAddOp2(v, OP_Goto, 0, lblFlushDone);
146269 if( pMWin->eStart==TK_FOLLOWING ){
146270 sqlite3VdbeJumpHere(v, addrIfPos2);
146271 }
146272
146273 if( pMWin->eStart==TK_CURRENT
146274 || pMWin->eStart==TK_PRECEDING
146275 || pMWin->eStart==TK_FOLLOWING
146276 ){
146277 int lblSkipInverse = sqlite3VdbeMakeLabel(v);;
146278 if( pMWin->eStart==TK_PRECEDING ){
146279 sqlite3VdbeAddOp3(v, OP_IfPos, regStart, lblSkipInverse, 1);
146280 VdbeCoverage(v);
146281 }
146282 if( pMWin->eStart==TK_FOLLOWING ){
146283 sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+2);
146284 VdbeCoverage(v);
146285 sqlite3VdbeAddOp2(v, OP_Goto, 0, lblSkipInverse);
146286 }else{
146287 sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+1);
146288 VdbeCoverageAlwaysTaken(v);
146289 }
146290 windowAggStep(pParse, pMWin, csrStart, 1, regArg, regSize);
146291 sqlite3VdbeResolveLabel(v, lblSkipInverse);
146292 }
146293 if( pMWin->eEnd==TK_FOLLOWING ){
146294 sqlite3VdbeJumpHere(v, addrIfPos1);
146295 }
146296 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
146297
146298 /* flush_partition_done: */
146299 sqlite3VdbeResolveLabel(v, lblFlushDone);
146300 sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
146301 sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
146302 VdbeComment((v, "end flush_partition subroutine"));
146303
146304 /* Jump to here to skip over flush_partition */
146305 sqlite3VdbeJumpHere(v, addrGoto);
146306}
146307
146308/*
146309** This function does the work of sqlite3WindowCodeStep() for cases that
146310** would normally be handled by windowCodeDefaultStep() when there are
146311** one or more built-in window-functions that require the entire partition
146312** to be cached in a temp table before any rows can be returned. Additionally.
146313** "RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING" is always handled by
146314** this function.
146315**
146316** Pseudo-code corresponding to the VM code generated by this function
146317** for each type of window follows.
146318**
146319** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
146320**
146321** flush_partition:
146322** Once {
146323** OpenDup (iEphCsr -> csrLead)
146324** }
146325** Integer ctr 0
146326** foreach row (csrLead){
146327** if( new peer ){
146328** AggFinal (xValue)
146329** for(i=0; i<ctr; i++){
146330** Gosub addrGosub
146331** Next iEphCsr
146332** }
146333** Integer ctr 0
146334** }
146335** AggStep (csrLead)
146336** Incr ctr
146337** }
146338**
146339** AggFinal (xFinalize)
146340** for(i=0; i<ctr; i++){
146341** Gosub addrGosub
146342** Next iEphCsr
146343** }
146344**
146345** ResetSorter (csr)
146346** Return
146347**
146348** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
146349**
146350** As above, except that the "if( new peer )" branch is always taken.
146351**
146352** RANGE BETWEEN CURRENT ROW AND CURRENT ROW
146353**
146354** As above, except that each of the for() loops becomes:
146355**
146356** for(i=0; i<ctr; i++){
146357** Gosub addrGosub
146358** AggInverse (iEphCsr)
146359** Next iEphCsr
146360** }
146361**
146362** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
146363**
146364** flush_partition:
146365** Once {
146366** OpenDup (iEphCsr -> csrLead)
146367** }
146368** foreach row (csrLead) {
146369** AggStep (csrLead)
146370** }
146371** foreach row (iEphCsr) {
146372** Gosub addrGosub
146373** }
146374**
146375** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
146376**
146377** flush_partition:
146378** Once {
146379** OpenDup (iEphCsr -> csrLead)
146380** }
146381** foreach row (csrLead){
146382** AggStep (csrLead)
146383** }
146384** Rewind (csrLead)
146385** Integer ctr 0
146386** foreach row (csrLead){
146387** if( new peer ){
146388** AggFinal (xValue)
146389** for(i=0; i<ctr; i++){
146390** Gosub addrGosub
146391** AggInverse (iEphCsr)
146392** Next iEphCsr
146393** }
146394** Integer ctr 0
146395** }
146396** Incr ctr
146397** }
146398**
146399** AggFinal (xFinalize)
146400** for(i=0; i<ctr; i++){
146401** Gosub addrGosub
146402** Next iEphCsr
146403** }
146404**
146405** ResetSorter (csr)
146406** Return
146407*/
146408static void windowCodeCacheStep(
146409 Parse *pParse,
146410 Select *p,
146411 WhereInfo *pWInfo,
146412 int regGosub,
146413 int addrGosub
146414){
146415 Window *pMWin = p->pWin;
146416 Vdbe *v = sqlite3GetVdbe(pParse);
146417 int k;
146418 int addr;
146419 ExprList *pPart = pMWin->pPartition;
146420 ExprList *pOrderBy = pMWin->pOrderBy;
146421 int nPeer = pOrderBy ? pOrderBy->nExpr : 0;
146422 int regNewPeer;
146423
146424 int addrGoto; /* Address of Goto used to jump flush_par.. */
146425 int addrNext; /* Jump here for next iteration of loop */
146426 int regFlushPart;
146427 int lblFlushPart;
146428 int csrLead;
146429 int regCtr;
146430 int regArg; /* Register array to martial function args */
146431 int regSize;
146432 int lblEmpty;
146433 int bReverse = pMWin->pOrderBy && pMWin->eStart==TK_CURRENT
146434 && pMWin->eEnd==TK_UNBOUNDED;
146435
146436 assert( (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
146437 || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_UNBOUNDED)
146438 || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_CURRENT)
146439 || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED)
146440 );
146441
146442 lblEmpty = sqlite3VdbeMakeLabel(v);
146443 regNewPeer = pParse->nMem+1;
146444 pParse->nMem += nPeer;
146445
146446 /* Allocate register and label for the "flush_partition" sub-routine. */
146447 regFlushPart = ++pParse->nMem;
146448 lblFlushPart = sqlite3VdbeMakeLabel(v);
146449
146450 csrLead = pParse->nTab++;
146451 regCtr = ++pParse->nMem;
146452
146453 windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart, &regSize);
146454 addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
146455
146456 /* Start of "flush_partition" */
146457 sqlite3VdbeResolveLabel(v, lblFlushPart);
146458 sqlite3VdbeAddOp2(v, OP_Once, 0, sqlite3VdbeCurrentAddr(v)+2);
146459 VdbeCoverage(v);
146460 sqlite3VdbeAddOp2(v, OP_OpenDup, csrLead, pMWin->iEphCsr);
146461
146462 /* Initialize the accumulator register for each window function to NULL */
146463 regArg = windowInitAccum(pParse, pMWin);
146464
146465 sqlite3VdbeAddOp2(v, OP_Integer, 0, regCtr);
146466 sqlite3VdbeAddOp2(v, OP_Rewind, csrLead, lblEmpty);
146467 VdbeCoverage(v);
146468 sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr, lblEmpty);
146469 VdbeCoverageNeverTaken(v);
146470
146471 if( bReverse ){
146472 int addr2 = sqlite3VdbeCurrentAddr(v);
146473 windowAggStep(pParse, pMWin, csrLead, 0, regArg, regSize);
146474 sqlite3VdbeAddOp2(v, OP_Next, csrLead, addr2);
146475 VdbeCoverage(v);
146476 sqlite3VdbeAddOp2(v, OP_Rewind, csrLead, lblEmpty);
146477 VdbeCoverageNeverTaken(v);
146478 }
146479 addrNext = sqlite3VdbeCurrentAddr(v);
146480
146481 if( pOrderBy && (pMWin->eEnd==TK_CURRENT || pMWin->eStart==TK_CURRENT) ){
146482 int bCurrent = (pMWin->eStart==TK_CURRENT);
146483 int addrJump = 0; /* Address of OP_Jump below */
146484 if( pMWin->eType==TK_RANGE ){
146485 int iOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
146486 int regPeer = pMWin->regPart + (pPart ? pPart->nExpr : 0);
146487 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
146488 for(k=0; k<nPeer; k++){
146489 sqlite3VdbeAddOp3(v, OP_Column, csrLead, iOff+k, regNewPeer+k);
146490 }
146491 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPeer, regPeer, nPeer);
146492 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
146493 addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
146494 VdbeCoverage(v);
146495 sqlite3VdbeAddOp3(v, OP_Copy, regNewPeer, regPeer, nPeer-1);
146496 }
146497
146498 windowReturnRows(pParse, pMWin, regCtr, regGosub, addrGosub,
146499 (bCurrent ? regArg : 0), (bCurrent ? regSize : 0)
146500 );
146501 if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
146502 }
146503
146504 if( bReverse==0 ){
146505 windowAggStep(pParse, pMWin, csrLead, 0, regArg, regSize);
146506 }
146507 sqlite3VdbeAddOp2(v, OP_AddImm, regCtr, 1);
146508 sqlite3VdbeAddOp2(v, OP_Next, csrLead, addrNext);
146509 VdbeCoverage(v);
146510
146511 windowReturnRows(pParse, pMWin, regCtr, regGosub, addrGosub, 0, 0);
146512
146513 sqlite3VdbeResolveLabel(v, lblEmpty);
146514 sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
146515 sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
146516
146517 /* Jump to here to skip over flush_partition */
146518 sqlite3VdbeJumpHere(v, addrGoto);
146519}
146520
146521
146522/*
146523** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
146524**
146525** ...
146526** if( new partition ){
146527** AggFinal (xFinalize)
146528** Gosub addrGosub
146529** ResetSorter eph-table
146530** }
146531** else if( new peer ){
146532** AggFinal (xValue)
146533** Gosub addrGosub
146534** ResetSorter eph-table
146535** }
146536** AggStep
146537** Insert (record into eph-table)
146538** sqlite3WhereEnd()
146539** AggFinal (xFinalize)
146540** Gosub addrGosub
146541**
146542** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
146543**
146544** As above, except take no action for a "new peer". Invoke
146545** the sub-routine once only for each partition.
146546**
146547** RANGE BETWEEN CURRENT ROW AND CURRENT ROW
146548**
146549** As above, except that the "new peer" condition is handled in the
146550** same way as "new partition" (so there is no "else if" block).
146551**
146552** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
146553**
146554** As above, except assume every row is a "new peer".
146555*/
146556static void windowCodeDefaultStep(
146557 Parse *pParse,
146558 Select *p,
146559 WhereInfo *pWInfo,
146560 int regGosub,
146561 int addrGosub
146562){
146563 Window *pMWin = p->pWin;
146564 Vdbe *v = sqlite3GetVdbe(pParse);
146565 int k;
146566 int iSubCsr = p->pSrc->a[0].iCursor;
146567 int nSub = p->pSrc->a[0].pTab->nCol;
146568 int reg = pParse->nMem+1;
146569 int regRecord = reg+nSub;
146570 int regRowid = regRecord+1;
146571 int addr;
146572 ExprList *pPart = pMWin->pPartition;
146573 ExprList *pOrderBy = pMWin->pOrderBy;
146574
146575 assert( pMWin->eType==TK_RANGE
146576 || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
146577 );
146578
146579 assert( (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
146580 || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_UNBOUNDED)
146581 || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_CURRENT)
146582 || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED && !pOrderBy)
146583 );
146584
146585 if( pMWin->eEnd==TK_UNBOUNDED ){
146586 pOrderBy = 0;
146587 }
146588
146589 pParse->nMem += nSub + 2;
146590
146591 /* Load the individual column values of the row returned by
146592 ** the sub-select into an array of registers. */
146593 for(k=0; k<nSub; k++){
146594 sqlite3VdbeAddOp3(v, OP_Column, iSubCsr, k, reg+k);
146595 }
146596
146597 /* Check if this is the start of a new partition or peer group. */
146598 if( pPart || pOrderBy ){
146599 int nPart = (pPart ? pPart->nExpr : 0);
146600 int addrGoto = 0;
146601 int addrJump = 0;
146602 int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
146603
146604 if( pPart ){
146605 int regNewPart = reg + pMWin->nBufferCol;
146606 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
146607 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart,nPart);
146608 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
146609 addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
146610 VdbeCoverageEqNe(v);
146611 windowAggFinal(pParse, pMWin, 1);
146612 if( pOrderBy ){
146613 addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
146614 }
146615 }
146616
146617 if( pOrderBy ){
146618 int regNewPeer = reg + pMWin->nBufferCol + nPart;
146619 int regPeer = pMWin->regPart + nPart;
146620
146621 if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
146622 if( pMWin->eType==TK_RANGE ){
146623 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
146624 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPeer, regPeer, nPeer);
146625 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
146626 addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
146627 VdbeCoverage(v);
146628 }else{
146629 addrJump = 0;
146630 }
146631 windowAggFinal(pParse, pMWin, pMWin->eStart==TK_CURRENT);
146632 if( addrGoto ) sqlite3VdbeJumpHere(v, addrGoto);
146633 }
146634
146635 sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr,sqlite3VdbeCurrentAddr(v)+3);
146636 VdbeCoverage(v);
146637 sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
146638 sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)-1);
146639 VdbeCoverage(v);
146640
146641 sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
146642 sqlite3VdbeAddOp3(
146643 v, OP_Copy, reg+pMWin->nBufferCol, pMWin->regPart, nPart+nPeer-1
146644 );
146645
146646 if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
146647 }
146648
146649 /* Invoke step function for window functions */
146650 windowAggStep(pParse, pMWin, -1, 0, reg, 0);
146651
146652 /* Buffer the current row in the ephemeral table. */
146653 if( pMWin->nBufferCol>0 ){
146654 sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, pMWin->nBufferCol, regRecord);
146655 }else{
146656 sqlite3VdbeAddOp2(v, OP_Blob, 0, regRecord);
146657 sqlite3VdbeAppendP4(v, (void*)"", 0);
146658 }
146659 sqlite3VdbeAddOp2(v, OP_NewRowid, pMWin->iEphCsr, regRowid);
146660 sqlite3VdbeAddOp3(v, OP_Insert, pMWin->iEphCsr, regRecord, regRowid);
146661
146662 /* End the database scan loop. */
146663 sqlite3WhereEnd(pWInfo);
146664
146665 windowAggFinal(pParse, pMWin, 1);
146666 sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr,sqlite3VdbeCurrentAddr(v)+3);
146667 VdbeCoverage(v);
146668 sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
146669 sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)-1);
146670 VdbeCoverage(v);
146671}
146672
146673/*
146674** Allocate and return a duplicate of the Window object indicated by the
146675** third argument. Set the Window.pOwner field of the new object to
146676** pOwner.
146677*/
146678SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p){
146679 Window *pNew = 0;
146680 if( ALWAYS(p) ){
146681 pNew = sqlite3DbMallocZero(db, sizeof(Window));
146682 if( pNew ){
146683 pNew->zName = sqlite3DbStrDup(db, p->zName);
146684 pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
146685 pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
146686 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
146687 pNew->eType = p->eType;
146688 pNew->eEnd = p->eEnd;
146689 pNew->eStart = p->eStart;
146690 pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
146691 pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
146692 pNew->pOwner = pOwner;
146693 }
146694 }
146695 return pNew;
146696}
146697
146698/*
146699** Return a copy of the linked list of Window objects passed as the
146700** second argument.
146701*/
146702SQLITE_PRIVATE Window *sqlite3WindowListDup(sqlite3 *db, Window *p){
146703 Window *pWin;
146704 Window *pRet = 0;
146705 Window **pp = &pRet;
146706
146707 for(pWin=p; pWin; pWin=pWin->pNextWin){
146708 *pp = sqlite3WindowDup(db, 0, pWin);
146709 if( *pp==0 ) break;
146710 pp = &((*pp)->pNextWin);
146711 }
146712
146713 return pRet;
146714}
146715
146716/*
146717** sqlite3WhereBegin() has already been called for the SELECT statement
146718** passed as the second argument when this function is invoked. It generates
146719** code to populate the Window.regResult register for each window function and
146720** invoke the sub-routine at instruction addrGosub once for each row.
146721** This function calls sqlite3WhereEnd() before returning.
146722*/
146723SQLITE_PRIVATE void sqlite3WindowCodeStep(
146724 Parse *pParse, /* Parse context */
146725 Select *p, /* Rewritten SELECT statement */
146726 WhereInfo *pWInfo, /* Context returned by sqlite3WhereBegin() */
146727 int regGosub, /* Register for OP_Gosub */
146728 int addrGosub /* OP_Gosub here to return each row */
146729){
146730 Window *pMWin = p->pWin;
146731
146732 /* There are three different functions that may be used to do the work
146733 ** of this one, depending on the window frame and the specific built-in
146734 ** window functions used (if any).
146735 **
146736 ** windowCodeRowExprStep() handles all "ROWS" window frames, except for:
146737 **
146738 ** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
146739 **
146740 ** The exception is because windowCodeRowExprStep() implements all window
146741 ** frame types by caching the entire partition in a temp table, and
146742 ** "ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW" is easy enough to
146743 ** implement without such a cache.
146744 **
146745 ** windowCodeCacheStep() is used for:
146746 **
146747 ** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
146748 **
146749 ** It is also used for anything not handled by windowCodeRowExprStep()
146750 ** that invokes a built-in window function that requires the entire
146751 ** partition to be cached in a temp table before any rows are returned
146752 ** (e.g. nth_value() or percent_rank()).
146753 **
146754 ** Finally, assuming there is no built-in window function that requires
146755 ** the partition to be cached, windowCodeDefaultStep() is used for:
146756 **
146757 ** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
146758 ** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
146759 ** RANGE BETWEEN CURRENT ROW AND CURRENT ROW
146760 ** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
146761 **
146762 ** windowCodeDefaultStep() is the only one of the three functions that
146763 ** does not cache each partition in a temp table before beginning to
146764 ** return rows.
146765 */
146766 if( pMWin->eType==TK_ROWS
146767 && (pMWin->eStart!=TK_UNBOUNDED||pMWin->eEnd!=TK_CURRENT||!pMWin->pOrderBy)
146768 ){
146769 VdbeModuleComment((pParse->pVdbe, "Begin RowExprStep()"));
146770 windowCodeRowExprStep(pParse, p, pWInfo, regGosub, addrGosub);
146771 }else{
146772 Window *pWin;
146773 int bCache = 0; /* True to use CacheStep() */
146774
146775 if( pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED ){
146776 bCache = 1;
146777 }else{
146778 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
146779 FuncDef *pFunc = pWin->pFunc;
146780 if( (pFunc->funcFlags & SQLITE_FUNC_WINDOW_SIZE)
146781 || (pFunc->zName==nth_valueName)
146782 || (pFunc->zName==first_valueName)
146783 || (pFunc->zName==leadName)
146784 || (pFunc->zName==lagName)
146785 ){
146786 bCache = 1;
146787 break;
146788 }
146789 }
146790 }
146791
146792 /* Otherwise, call windowCodeDefaultStep(). */
146793 if( bCache ){
146794 VdbeModuleComment((pParse->pVdbe, "Begin CacheStep()"));
146795 windowCodeCacheStep(pParse, p, pWInfo, regGosub, addrGosub);
146796 }else{
146797 VdbeModuleComment((pParse->pVdbe, "Begin DefaultStep()"));
146798 windowCodeDefaultStep(pParse, p, pWInfo, regGosub, addrGosub);
146799 }
146800 }
146801}
146802
146803#endif /* SQLITE_OMIT_WINDOWFUNC */
146804
146805/************** End of window.c **********************************************/
146806/************** Begin file parse.c *******************************************/
146807/*
146808** 2000-05-29
146809**
146810** The author disclaims copyright to this source code. In place of
146811** a legal notice, here is a blessing:
146812**
146813** May you do good and not evil.
146814** May you find forgiveness for yourself and forgive others.
146815** May you share freely, never taking more than you give.
146816**
146817*************************************************************************
146818** Driver template for the LEMON parser generator.
146819**
146820** The "lemon" program processes an LALR(1) input grammar file, then uses
146821** this template to construct a parser. The "lemon" program inserts text
146822** at each "%%" line. Also, any "P-a-r-s-e" identifer prefix (without the
146823** interstitial "-" characters) contained in this template is changed into
146824** the value of the %name directive from the grammar. Otherwise, the content
146825** of this template is copied straight through into the generate parser
146826** source file.
146827**
146828** The following is the concatenation of all %include directives from the
146829** input grammar file:
146830*/
146831/* #include <stdio.h> */
146832/* #include <assert.h> */
146833/************ Begin %include sections from the grammar ************************/
146834
146835/* #include "sqliteInt.h" */
146836
146837/*
146838** Disable all error recovery processing in the parser push-down
146839** automaton.
146840*/
146841#define YYNOERRORRECOVERY 1
146842
146843/*
146844** Make yytestcase() the same as testcase()
146845*/
146846#define yytestcase(X) testcase(X)
146847
146848/*
146849** Indicate that sqlite3ParserFree() will never be called with a null
146850** pointer.
146851*/
146852#define YYPARSEFREENEVERNULL 1
146853
146854/*
146855** In the amalgamation, the parse.c file generated by lemon and the
146856** tokenize.c file are concatenated. In that case, sqlite3RunParser()
146857** has access to the the size of the yyParser object and so the parser
146858** engine can be allocated from stack. In that case, only the
146859** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
146860** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
146861** omitted.
146862*/
146863#ifdef SQLITE_AMALGAMATION
146864# define sqlite3Parser_ENGINEALWAYSONSTACK 1
146865#endif
146866
146867/*
146868** Alternative datatype for the argument to the malloc() routine passed
146869** into sqlite3ParserAlloc(). The default is size_t.
146870*/
146871#define YYMALLOCARGTYPE u64
146872
146873/*
146874** An instance of the following structure describes the event of a
146875** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
146876** TK_DELETE, or TK_INSTEAD. If the event is of the form
146877**
146878** UPDATE ON (a,b,c)
146879**
146880** Then the "b" IdList records the list "a,b,c".
146881*/
146882struct TrigEvent { int a; IdList * b; };
146883
146884struct FrameBound { int eType; Expr *pExpr; };
146885
146886/*
146887** Disable lookaside memory allocation for objects that might be
146888** shared across database connections.
146889*/
146890static void disableLookaside(Parse *pParse){
146891 pParse->disableLookaside++;
146892 pParse->db->lookaside.bDisable++;
146893}
146894
146895
146896 /*
146897 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
146898 ** all elements in the list. And make sure list length does not exceed
146899 ** SQLITE_LIMIT_COMPOUND_SELECT.
146900 */
146901 static void parserDoubleLinkSelect(Parse *pParse, Select *p){
146902 if( p->pPrior ){
146903 Select *pNext = 0, *pLoop;
146904 int mxSelect, cnt = 0;
146905 for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
146906 pLoop->pNext = pNext;
146907 pLoop->selFlags |= SF_Compound;
146908 }
146909 if( (p->selFlags & SF_MultiValue)==0 &&
146910 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
146911 cnt>mxSelect
146912 ){
146913 sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
146914 }
146915 }
146916 }
146917
146918
146919 /* Construct a new Expr object from a single identifier. Use the
146920 ** new Expr to populate pOut. Set the span of pOut to be the identifier
146921 ** that created the expression.
146922 */
146923 static Expr *tokenExpr(Parse *pParse, int op, Token t){
146924 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
146925 if( p ){
146926 /* memset(p, 0, sizeof(Expr)); */
146927 p->op = (u8)op;
146928 p->affinity = 0;
146929 p->flags = EP_Leaf;
146930 p->iAgg = -1;
146931 p->pLeft = p->pRight = 0;
146932 p->x.pList = 0;
146933 p->pAggInfo = 0;
146934 p->y.pTab = 0;
146935 p->op2 = 0;
146936 p->iTable = 0;
146937 p->iColumn = 0;
146938 p->u.zToken = (char*)&p[1];
146939 memcpy(p->u.zToken, t.z, t.n);
146940 p->u.zToken[t.n] = 0;
146941 if( sqlite3Isquote(p->u.zToken[0]) ){
146942 if( p->u.zToken[0]=='"' ) p->flags |= EP_DblQuoted;
146943 sqlite3Dequote(p->u.zToken);
146944 }
146945#if SQLITE_MAX_EXPR_DEPTH>0
146946 p->nHeight = 1;
146947#endif
146948 if( IN_RENAME_OBJECT ){
146949 return (Expr*)sqlite3RenameTokenMap(pParse, (void*)p, &t);
146950 }
146951 }
146952 return p;
146953 }
146954
146955
146956 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
146957 ** unary TK_ISNULL or TK_NOTNULL expression. */
146958 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
146959 sqlite3 *db = pParse->db;
146960 if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){
146961 pA->op = (u8)op;
146962 sqlite3ExprDelete(db, pA->pRight);
146963 pA->pRight = 0;
146964 }
146965 }
146966
146967 /* Add a single new term to an ExprList that is used to store a
146968 ** list of identifiers. Report an error if the ID list contains
146969 ** a COLLATE clause or an ASC or DESC keyword, except ignore the
146970 ** error while parsing a legacy schema.
146971 */
146972 static ExprList *parserAddExprIdListTerm(
146973 Parse *pParse,
146974 ExprList *pPrior,
146975 Token *pIdToken,
146976 int hasCollate,
146977 int sortOrder
146978 ){
146979 ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
146980 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
146981 && pParse->db->init.busy==0
146982 ){
146983 sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
146984 pIdToken->n, pIdToken->z);
146985 }
146986 sqlite3ExprListSetName(pParse, p, pIdToken, 1);
146987 return p;
146988 }
146989/**************** End of %include directives **********************************/
146990/* These constants specify the various numeric values for terminal symbols
146991** in a format understandable to "makeheaders". This section is blank unless
146992** "lemon" is run with the "-m" command-line option.
146993***************** Begin makeheaders token definitions *************************/
146994/**************** End makeheaders token definitions ***************************/
146995
146996/* The next sections is a series of control #defines.
146997** various aspects of the generated parser.
146998** YYCODETYPE is the data type used to store the integer codes
146999** that represent terminal and non-terminal symbols.
147000** "unsigned char" is used if there are fewer than
147001** 256 symbols. Larger types otherwise.
147002** YYNOCODE is a number of type YYCODETYPE that is not used for
147003** any terminal or nonterminal symbol.
147004** YYFALLBACK If defined, this indicates that one or more tokens
147005** (also known as: "terminal symbols") have fall-back
147006** values which should be used if the original symbol
147007** would not parse. This permits keywords to sometimes
147008** be used as identifiers, for example.
147009** YYACTIONTYPE is the data type used for "action codes" - numbers
147010** that indicate what to do in response to the next
147011** token.
147012** sqlite3ParserTOKENTYPE is the data type used for minor type for terminal
147013** symbols. Background: A "minor type" is a semantic
147014** value associated with a terminal or non-terminal
147015** symbols. For example, for an "ID" terminal symbol,
147016** the minor type might be the name of the identifier.
147017** Each non-terminal can have a different minor type.
147018** Terminal symbols all have the same minor type, though.
147019** This macros defines the minor type for terminal
147020** symbols.
147021** YYMINORTYPE is the data type used for all minor types.
147022** This is typically a union of many types, one of
147023** which is sqlite3ParserTOKENTYPE. The entry in the union
147024** for terminal symbols is called "yy0".
147025** YYSTACKDEPTH is the maximum depth of the parser's stack. If
147026** zero the stack is dynamically sized using realloc()
147027** sqlite3ParserARG_SDECL A static variable declaration for the %extra_argument
147028** sqlite3ParserARG_PDECL A parameter declaration for the %extra_argument
147029** sqlite3ParserARG_PARAM Code to pass %extra_argument as a subroutine parameter
147030** sqlite3ParserARG_STORE Code to store %extra_argument into yypParser
147031** sqlite3ParserARG_FETCH Code to extract %extra_argument from yypParser
147032** sqlite3ParserCTX_* As sqlite3ParserARG_ except for %extra_context
147033** YYERRORSYMBOL is the code number of the error symbol. If not
147034** defined, then do no error processing.
147035** YYNSTATE the combined number of states.
147036** YYNRULE the number of rules in the grammar
147037** YYNTOKEN Number of terminal symbols
147038** YY_MAX_SHIFT Maximum value for shift actions
147039** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
147040** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
147041** YY_ERROR_ACTION The yy_action[] code for syntax error
147042** YY_ACCEPT_ACTION The yy_action[] code for accept
147043** YY_NO_ACTION The yy_action[] code for no-op
147044** YY_MIN_REDUCE Minimum value for reduce actions
147045** YY_MAX_REDUCE Maximum value for reduce actions
147046*/
147047#ifndef INTERFACE
147048# define INTERFACE 1
147049#endif
147050/************* Begin control #defines *****************************************/
147051#define YYCODETYPE unsigned short int
147052#define YYNOCODE 277
147053#define YYACTIONTYPE unsigned short int
147054#define YYWILDCARD 91
147055#define sqlite3ParserTOKENTYPE Token
147056typedef union {
147057 int yyinit;
147058 sqlite3ParserTOKENTYPE yy0;
147059 Expr* yy18;
147060 struct TrigEvent yy34;
147061 IdList* yy48;
147062 int yy70;
147063 struct {int value; int mask;} yy111;
147064 struct FrameBound yy119;
147065 SrcList* yy135;
147066 TriggerStep* yy207;
147067 Window* yy327;
147068 Upsert* yy340;
147069 const char* yy392;
147070 ExprList* yy420;
147071 With* yy449;
147072 Select* yy489;
147073} YYMINORTYPE;
147074#ifndef YYSTACKDEPTH
147075#define YYSTACKDEPTH 100
147076#endif
147077#define sqlite3ParserARG_SDECL
147078#define sqlite3ParserARG_PDECL
147079#define sqlite3ParserARG_PARAM
147080#define sqlite3ParserARG_FETCH
147081#define sqlite3ParserARG_STORE
147082#define sqlite3ParserCTX_SDECL Parse *pParse;
147083#define sqlite3ParserCTX_PDECL ,Parse *pParse
147084#define sqlite3ParserCTX_PARAM ,pParse
147085#define sqlite3ParserCTX_FETCH Parse *pParse=yypParser->pParse;
147086#define sqlite3ParserCTX_STORE yypParser->pParse=pParse;
147087#define YYFALLBACK 1
147088#define YYNSTATE 521
147089#define YYNRULE 367
147090#define YYNTOKEN 155
147091#define YY_MAX_SHIFT 520
147092#define YY_MIN_SHIFTREDUCE 756
147093#define YY_MAX_SHIFTREDUCE 1122
147094#define YY_ERROR_ACTION 1123
147095#define YY_ACCEPT_ACTION 1124
147096#define YY_NO_ACTION 1125
147097#define YY_MIN_REDUCE 1126
147098#define YY_MAX_REDUCE 1492
147099/************* End control #defines *******************************************/
147100#define YY_NLOOKAHEAD ((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])))
147101
147102/* Define the yytestcase() macro to be a no-op if is not already defined
147103** otherwise.
147104**
147105** Applications can choose to define yytestcase() in the %include section
147106** to a macro that can assist in verifying code coverage. For production
147107** code the yytestcase() macro should be turned off. But it is useful
147108** for testing.
147109*/
147110#ifndef yytestcase
147111# define yytestcase(X)
147112#endif
147113
147114
147115/* Next are the tables used to determine what action to take based on the
147116** current state and lookahead token. These tables are used to implement
147117** functions that take a state number and lookahead value and return an
147118** action integer.
147119**
147120** Suppose the action integer is N. Then the action is determined as
147121** follows
147122**
147123** 0 <= N <= YY_MAX_SHIFT Shift N. That is, push the lookahead
147124** token onto the stack and goto state N.
147125**
147126** N between YY_MIN_SHIFTREDUCE Shift to an arbitrary state then
147127** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE.
147128**
147129** N == YY_ERROR_ACTION A syntax error has occurred.
147130**
147131** N == YY_ACCEPT_ACTION The parser accepts its input.
147132**
147133** N == YY_NO_ACTION No such action. Denotes unused
147134** slots in the yy_action[] table.
147135**
147136** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE
147137** and YY_MAX_REDUCE
147138**
147139** The action table is constructed as a single large table named yy_action[].
147140** Given state S and lookahead X, the action is computed as either:
147141**
147142** (A) N = yy_action[ yy_shift_ofst[S] + X ]
147143** (B) N = yy_default[S]
147144**
147145** The (A) formula is preferred. The B formula is used instead if
147146** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X.
147147**
147148** The formulas above are for computing the action when the lookahead is
147149** a terminal symbol. If the lookahead is a non-terminal (as occurs after
147150** a reduce action) then the yy_reduce_ofst[] array is used in place of
147151** the yy_shift_ofst[] array.
147152**
147153** The following are the tables generated in this section:
147154**
147155** yy_action[] A single table containing all actions.
147156** yy_lookahead[] A table containing the lookahead for each entry in
147157** yy_action. Used to detect hash collisions.
147158** yy_shift_ofst[] For each state, the offset into yy_action for
147159** shifting terminals.
147160** yy_reduce_ofst[] For each state, the offset into yy_action for
147161** shifting non-terminals after a reduce.
147162** yy_default[] Default action for each state.
147163**
147164*********** Begin parsing tables **********************************************/
147165#define YY_ACTTAB_COUNT (2009)
147166static const YYACTIONTYPE yy_action[] = {
147167 /* 0 */ 368, 105, 102, 197, 105, 102, 197, 515, 1124, 1,
147168 /* 10 */ 1, 520, 2, 1128, 515, 1192, 1171, 1456, 275, 370,
147169 /* 20 */ 127, 1389, 1197, 1197, 1192, 1166, 178, 1205, 64, 64,
147170 /* 30 */ 477, 887, 322, 428, 348, 37, 37, 808, 362, 888,
147171 /* 40 */ 509, 509, 509, 112, 113, 103, 1100, 1100, 953, 956,
147172 /* 50 */ 946, 946, 110, 110, 111, 111, 111, 111, 365, 252,
147173 /* 60 */ 252, 515, 252, 252, 497, 515, 309, 515, 459, 515,
147174 /* 70 */ 1079, 491, 512, 478, 6, 512, 809, 134, 498, 228,
147175 /* 80 */ 194, 428, 37, 37, 515, 208, 64, 64, 64, 64,
147176 /* 90 */ 13, 13, 109, 109, 109, 109, 108, 108, 107, 107,
147177 /* 100 */ 107, 106, 401, 258, 381, 13, 13, 398, 397, 428,
147178 /* 110 */ 252, 252, 370, 476, 405, 1104, 1079, 1080, 1081, 386,
147179 /* 120 */ 1106, 390, 497, 512, 497, 1423, 1419, 304, 1105, 307,
147180 /* 130 */ 1256, 496, 370, 499, 16, 16, 112, 113, 103, 1100,
147181 /* 140 */ 1100, 953, 956, 946, 946, 110, 110, 111, 111, 111,
147182 /* 150 */ 111, 262, 1107, 495, 1107, 401, 112, 113, 103, 1100,
147183 /* 160 */ 1100, 953, 956, 946, 946, 110, 110, 111, 111, 111,
147184 /* 170 */ 111, 129, 1425, 343, 1420, 339, 1059, 492, 1057, 263,
147185 /* 180 */ 73, 105, 102, 197, 994, 109, 109, 109, 109, 108,
147186 /* 190 */ 108, 107, 107, 107, 106, 401, 370, 111, 111, 111,
147187 /* 200 */ 111, 104, 492, 89, 1432, 109, 109, 109, 109, 108,
147188 /* 210 */ 108, 107, 107, 107, 106, 401, 111, 111, 111, 111,
147189 /* 220 */ 112, 113, 103, 1100, 1100, 953, 956, 946, 946, 110,
147190 /* 230 */ 110, 111, 111, 111, 111, 109, 109, 109, 109, 108,
147191 /* 240 */ 108, 107, 107, 107, 106, 401, 114, 108, 108, 107,
147192 /* 250 */ 107, 107, 106, 401, 109, 109, 109, 109, 108, 108,
147193 /* 260 */ 107, 107, 107, 106, 401, 152, 399, 399, 399, 109,
147194 /* 270 */ 109, 109, 109, 108, 108, 107, 107, 107, 106, 401,
147195 /* 280 */ 178, 493, 1412, 434, 1037, 1486, 1079, 515, 1486, 370,
147196 /* 290 */ 421, 297, 357, 412, 74, 1079, 109, 109, 109, 109,
147197 /* 300 */ 108, 108, 107, 107, 107, 106, 401, 1413, 37, 37,
147198 /* 310 */ 1431, 274, 506, 112, 113, 103, 1100, 1100, 953, 956,
147199 /* 320 */ 946, 946, 110, 110, 111, 111, 111, 111, 1436, 520,
147200 /* 330 */ 2, 1128, 1079, 1080, 1081, 430, 275, 1079, 127, 366,
147201 /* 340 */ 933, 1079, 1080, 1081, 220, 1205, 913, 458, 455, 454,
147202 /* 350 */ 392, 167, 515, 1035, 152, 445, 924, 453, 152, 874,
147203 /* 360 */ 923, 289, 109, 109, 109, 109, 108, 108, 107, 107,
147204 /* 370 */ 107, 106, 401, 13, 13, 261, 853, 252, 252, 227,
147205 /* 380 */ 106, 401, 370, 1079, 1080, 1081, 311, 388, 1079, 296,
147206 /* 390 */ 512, 923, 923, 925, 231, 323, 1255, 1388, 1423, 490,
147207 /* 400 */ 274, 506, 12, 208, 274, 506, 112, 113, 103, 1100,
147208 /* 410 */ 1100, 953, 956, 946, 946, 110, 110, 111, 111, 111,
147209 /* 420 */ 111, 1440, 286, 1128, 288, 1079, 1097, 247, 275, 1098,
147210 /* 430 */ 127, 387, 405, 389, 1079, 1080, 1081, 1205, 159, 238,
147211 /* 440 */ 255, 321, 461, 316, 460, 225, 790, 105, 102, 197,
147212 /* 450 */ 513, 314, 842, 842, 445, 109, 109, 109, 109, 108,
147213 /* 460 */ 108, 107, 107, 107, 106, 401, 515, 514, 515, 252,
147214 /* 470 */ 252, 1079, 1080, 1081, 435, 370, 1098, 933, 1460, 794,
147215 /* 480 */ 274, 506, 512, 105, 102, 197, 336, 63, 63, 64,
147216 /* 490 */ 64, 27, 790, 924, 287, 208, 1354, 923, 515, 112,
147217 /* 500 */ 113, 103, 1100, 1100, 953, 956, 946, 946, 110, 110,
147218 /* 510 */ 111, 111, 111, 111, 107, 107, 107, 106, 401, 49,
147219 /* 520 */ 49, 515, 28, 1079, 405, 497, 421, 297, 923, 923,
147220 /* 530 */ 925, 186, 468, 1079, 467, 999, 999, 442, 515, 1079,
147221 /* 540 */ 334, 515, 45, 45, 1083, 342, 173, 168, 109, 109,
147222 /* 550 */ 109, 109, 108, 108, 107, 107, 107, 106, 401, 13,
147223 /* 560 */ 13, 205, 13, 13, 252, 252, 1195, 1195, 370, 1079,
147224 /* 570 */ 1080, 1081, 787, 265, 5, 359, 494, 512, 469, 1079,
147225 /* 580 */ 1080, 1081, 398, 397, 1079, 1079, 1080, 1081, 3, 282,
147226 /* 590 */ 1079, 1083, 112, 113, 103, 1100, 1100, 953, 956, 946,
147227 /* 600 */ 946, 110, 110, 111, 111, 111, 111, 252, 252, 1015,
147228 /* 610 */ 220, 1079, 873, 458, 455, 454, 943, 943, 954, 957,
147229 /* 620 */ 512, 252, 252, 453, 1016, 1079, 445, 1107, 1209, 1107,
147230 /* 630 */ 1079, 1080, 1081, 515, 512, 426, 1079, 1080, 1081, 1017,
147231 /* 640 */ 512, 109, 109, 109, 109, 108, 108, 107, 107, 107,
147232 /* 650 */ 106, 401, 1052, 515, 50, 50, 515, 1079, 1080, 1081,
147233 /* 660 */ 828, 370, 1051, 379, 411, 1064, 1358, 207, 408, 773,
147234 /* 670 */ 829, 1079, 1080, 1081, 64, 64, 322, 64, 64, 1302,
147235 /* 680 */ 947, 411, 410, 1358, 1360, 112, 113, 103, 1100, 1100,
147236 /* 690 */ 953, 956, 946, 946, 110, 110, 111, 111, 111, 111,
147237 /* 700 */ 294, 482, 515, 1037, 1487, 515, 434, 1487, 354, 1120,
147238 /* 710 */ 483, 996, 913, 485, 466, 996, 132, 178, 33, 450,
147239 /* 720 */ 1203, 136, 406, 64, 64, 479, 64, 64, 419, 369,
147240 /* 730 */ 283, 1146, 252, 252, 109, 109, 109, 109, 108, 108,
147241 /* 740 */ 107, 107, 107, 106, 401, 512, 224, 440, 411, 266,
147242 /* 750 */ 1358, 266, 252, 252, 370, 296, 416, 284, 934, 396,
147243 /* 760 */ 976, 470, 400, 252, 252, 512, 9, 473, 231, 500,
147244 /* 770 */ 354, 1036, 1035, 1488, 355, 374, 512, 1121, 112, 113,
147245 /* 780 */ 103, 1100, 1100, 953, 956, 946, 946, 110, 110, 111,
147246 /* 790 */ 111, 111, 111, 252, 252, 1015, 515, 1347, 295, 252,
147247 /* 800 */ 252, 252, 252, 1098, 375, 249, 512, 445, 872, 322,
147248 /* 810 */ 1016, 480, 512, 195, 512, 434, 273, 15, 15, 515,
147249 /* 820 */ 314, 515, 95, 515, 93, 1017, 367, 109, 109, 109,
147250 /* 830 */ 109, 108, 108, 107, 107, 107, 106, 401, 515, 1121,
147251 /* 840 */ 39, 39, 51, 51, 52, 52, 503, 370, 515, 1204,
147252 /* 850 */ 1098, 918, 439, 341, 133, 436, 223, 222, 221, 53,
147253 /* 860 */ 53, 322, 1400, 761, 762, 763, 515, 370, 88, 54,
147254 /* 870 */ 54, 112, 113, 103, 1100, 1100, 953, 956, 946, 946,
147255 /* 880 */ 110, 110, 111, 111, 111, 111, 407, 55, 55, 196,
147256 /* 890 */ 515, 112, 113, 103, 1100, 1100, 953, 956, 946, 946,
147257 /* 900 */ 110, 110, 111, 111, 111, 111, 135, 264, 1149, 376,
147258 /* 910 */ 515, 40, 40, 515, 872, 515, 993, 515, 993, 116,
147259 /* 920 */ 109, 109, 109, 109, 108, 108, 107, 107, 107, 106,
147260 /* 930 */ 401, 41, 41, 515, 43, 43, 44, 44, 56, 56,
147261 /* 940 */ 109, 109, 109, 109, 108, 108, 107, 107, 107, 106,
147262 /* 950 */ 401, 515, 379, 515, 57, 57, 515, 799, 515, 379,
147263 /* 960 */ 515, 445, 200, 515, 323, 515, 1397, 515, 1459, 515,
147264 /* 970 */ 1287, 817, 58, 58, 14, 14, 515, 59, 59, 118,
147265 /* 980 */ 118, 60, 60, 515, 46, 46, 61, 61, 62, 62,
147266 /* 990 */ 47, 47, 515, 190, 189, 91, 515, 140, 140, 515,
147267 /* 1000 */ 394, 515, 277, 1200, 141, 141, 515, 1115, 515, 992,
147268 /* 1010 */ 515, 992, 515, 69, 69, 370, 278, 48, 48, 259,
147269 /* 1020 */ 65, 65, 119, 119, 246, 246, 260, 66, 66, 120,
147270 /* 1030 */ 120, 121, 121, 117, 117, 370, 515, 512, 383, 112,
147271 /* 1040 */ 113, 103, 1100, 1100, 953, 956, 946, 946, 110, 110,
147272 /* 1050 */ 111, 111, 111, 111, 515, 872, 515, 139, 139, 112,
147273 /* 1060 */ 113, 103, 1100, 1100, 953, 956, 946, 946, 110, 110,
147274 /* 1070 */ 111, 111, 111, 111, 1287, 138, 138, 125, 125, 515,
147275 /* 1080 */ 12, 515, 281, 1287, 515, 445, 131, 1287, 109, 109,
147276 /* 1090 */ 109, 109, 108, 108, 107, 107, 107, 106, 401, 515,
147277 /* 1100 */ 124, 124, 122, 122, 515, 123, 123, 515, 109, 109,
147278 /* 1110 */ 109, 109, 108, 108, 107, 107, 107, 106, 401, 515,
147279 /* 1120 */ 68, 68, 463, 783, 515, 70, 70, 302, 67, 67,
147280 /* 1130 */ 1032, 253, 253, 356, 1287, 191, 196, 1433, 465, 1301,
147281 /* 1140 */ 38, 38, 384, 94, 512, 42, 42, 177, 848, 274,
147282 /* 1150 */ 506, 385, 420, 847, 1356, 441, 508, 376, 377, 153,
147283 /* 1160 */ 423, 872, 432, 370, 224, 251, 194, 887, 182, 293,
147284 /* 1170 */ 783, 848, 88, 254, 466, 888, 847, 915, 807, 806,
147285 /* 1180 */ 230, 1241, 910, 370, 17, 413, 797, 112, 113, 103,
147286 /* 1190 */ 1100, 1100, 953, 956, 946, 946, 110, 110, 111, 111,
147287 /* 1200 */ 111, 111, 395, 814, 815, 1175, 983, 112, 101, 103,
147288 /* 1210 */ 1100, 1100, 953, 956, 946, 946, 110, 110, 111, 111,
147289 /* 1220 */ 111, 111, 375, 422, 427, 429, 298, 230, 230, 88,
147290 /* 1230 */ 1240, 451, 312, 797, 226, 88, 109, 109, 109, 109,
147291 /* 1240 */ 108, 108, 107, 107, 107, 106, 401, 86, 433, 979,
147292 /* 1250 */ 927, 881, 226, 983, 230, 415, 109, 109, 109, 109,
147293 /* 1260 */ 108, 108, 107, 107, 107, 106, 401, 320, 845, 781,
147294 /* 1270 */ 846, 100, 130, 100, 1403, 290, 370, 319, 1377, 1376,
147295 /* 1280 */ 437, 1449, 299, 1237, 303, 306, 308, 310, 1188, 1174,
147296 /* 1290 */ 1173, 1172, 315, 324, 325, 1228, 370, 927, 1249, 271,
147297 /* 1300 */ 1286, 113, 103, 1100, 1100, 953, 956, 946, 946, 110,
147298 /* 1310 */ 110, 111, 111, 111, 111, 1224, 1235, 502, 501, 1292,
147299 /* 1320 */ 1221, 1155, 103, 1100, 1100, 953, 956, 946, 946, 110,
147300 /* 1330 */ 110, 111, 111, 111, 111, 1148, 1137, 1136, 1138, 1443,
147301 /* 1340 */ 446, 244, 184, 98, 507, 188, 4, 353, 327, 109,
147302 /* 1350 */ 109, 109, 109, 108, 108, 107, 107, 107, 106, 401,
147303 /* 1360 */ 510, 329, 331, 199, 414, 456, 292, 285, 318, 109,
147304 /* 1370 */ 109, 109, 109, 108, 108, 107, 107, 107, 106, 401,
147305 /* 1380 */ 11, 1271, 1279, 402, 361, 192, 1171, 1351, 431, 505,
147306 /* 1390 */ 346, 1350, 333, 98, 507, 504, 4, 187, 1446, 1115,
147307 /* 1400 */ 233, 1396, 155, 1394, 1112, 152, 72, 75, 378, 425,
147308 /* 1410 */ 510, 165, 149, 157, 933, 1276, 86, 30, 1268, 417,
147309 /* 1420 */ 96, 96, 8, 160, 161, 162, 163, 97, 418, 402,
147310 /* 1430 */ 517, 516, 449, 402, 923, 210, 358, 424, 1282, 438,
147311 /* 1440 */ 169, 214, 360, 1345, 80, 504, 31, 444, 1365, 301,
147312 /* 1450 */ 245, 274, 506, 216, 174, 305, 488, 447, 217, 462,
147313 /* 1460 */ 1139, 487, 218, 363, 933, 923, 923, 925, 926, 24,
147314 /* 1470 */ 96, 96, 1191, 1190, 1189, 391, 1182, 97, 1163, 402,
147315 /* 1480 */ 517, 516, 799, 364, 923, 1162, 317, 1161, 98, 507,
147316 /* 1490 */ 1181, 4, 1458, 472, 393, 269, 270, 475, 481, 1232,
147317 /* 1500 */ 85, 1233, 326, 328, 232, 510, 495, 1231, 330, 98,
147318 /* 1510 */ 507, 1230, 4, 486, 335, 923, 923, 925, 926, 24,
147319 /* 1520 */ 1435, 1068, 404, 181, 336, 256, 510, 115, 402, 332,
147320 /* 1530 */ 352, 352, 351, 241, 349, 1214, 1414, 770, 338, 10,
147321 /* 1540 */ 504, 340, 272, 92, 1331, 1213, 87, 183, 484, 402,
147322 /* 1550 */ 201, 488, 280, 239, 344, 345, 489, 1145, 29, 933,
147323 /* 1560 */ 279, 504, 1074, 518, 240, 96, 96, 242, 243, 519,
147324 /* 1570 */ 1134, 1129, 97, 154, 402, 517, 516, 372, 373, 923,
147325 /* 1580 */ 933, 142, 143, 128, 1381, 267, 96, 96, 852, 757,
147326 /* 1590 */ 203, 144, 403, 97, 1382, 402, 517, 516, 204, 1380,
147327 /* 1600 */ 923, 146, 1379, 1159, 1158, 71, 1156, 276, 202, 185,
147328 /* 1610 */ 923, 923, 925, 926, 24, 198, 257, 126, 991, 989,
147329 /* 1620 */ 907, 98, 507, 156, 4, 145, 158, 206, 831, 209,
147330 /* 1630 */ 291, 923, 923, 925, 926, 24, 1005, 911, 510, 164,
147331 /* 1640 */ 147, 380, 371, 382, 166, 76, 77, 274, 506, 148,
147332 /* 1650 */ 78, 79, 1008, 211, 212, 1004, 137, 213, 18, 300,
147333 /* 1660 */ 230, 402, 997, 1109, 443, 215, 32, 170, 171, 772,
147334 /* 1670 */ 409, 448, 319, 504, 219, 172, 452, 81, 19, 457,
147335 /* 1680 */ 313, 20, 82, 268, 488, 150, 810, 179, 83, 487,
147336 /* 1690 */ 464, 151, 933, 180, 959, 84, 1040, 34, 96, 96,
147337 /* 1700 */ 471, 1041, 35, 474, 193, 97, 248, 402, 517, 516,
147338 /* 1710 */ 1068, 404, 923, 250, 256, 880, 229, 175, 875, 352,
147339 /* 1720 */ 352, 351, 241, 349, 100, 21, 770, 22, 1054, 1056,
147340 /* 1730 */ 7, 98, 507, 1045, 4, 337, 1058, 23, 974, 201,
147341 /* 1740 */ 176, 280, 88, 923, 923, 925, 926, 24, 510, 279,
147342 /* 1750 */ 960, 958, 962, 1014, 963, 1013, 235, 234, 25, 36,
147343 /* 1760 */ 99, 90, 507, 928, 4, 511, 350, 782, 26, 841,
147344 /* 1770 */ 236, 402, 347, 1069, 237, 1125, 1125, 1451, 510, 203,
147345 /* 1780 */ 1450, 1125, 1125, 504, 1125, 1125, 1125, 204, 1125, 1125,
147346 /* 1790 */ 146, 1125, 1125, 1125, 1125, 1125, 1125, 202, 1125, 1125,
147347 /* 1800 */ 1125, 402, 933, 1125, 1125, 1125, 1125, 1125, 96, 96,
147348 /* 1810 */ 1125, 1125, 1125, 504, 1125, 97, 1125, 402, 517, 516,
147349 /* 1820 */ 1125, 1125, 923, 1125, 1125, 1125, 1125, 1125, 1125, 1125,
147350 /* 1830 */ 1125, 371, 933, 1125, 1125, 1125, 274, 506, 96, 96,
147351 /* 1840 */ 1125, 1125, 1125, 1125, 1125, 97, 1125, 402, 517, 516,
147352 /* 1850 */ 1125, 1125, 923, 923, 923, 925, 926, 24, 1125, 409,
147353 /* 1860 */ 1125, 1125, 1125, 256, 1125, 1125, 1125, 1125, 352, 352,
147354 /* 1870 */ 351, 241, 349, 1125, 1125, 770, 1125, 1125, 1125, 1125,
147355 /* 1880 */ 1125, 1125, 1125, 923, 923, 925, 926, 24, 201, 1125,
147356 /* 1890 */ 280, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 279, 1125,
147357 /* 1900 */ 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125,
147358 /* 1910 */ 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125,
147359 /* 1920 */ 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 203, 1125,
147360 /* 1930 */ 1125, 1125, 1125, 1125, 1125, 1125, 204, 1125, 1125, 146,
147361 /* 1940 */ 1125, 1125, 1125, 1125, 1125, 1125, 202, 1125, 1125, 1125,
147362 /* 1950 */ 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125,
147363 /* 1960 */ 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125,
147364 /* 1970 */ 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125,
147365 /* 1980 */ 371, 1125, 1125, 1125, 1125, 274, 506, 1125, 1125, 1125,
147366 /* 1990 */ 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125,
147367 /* 2000 */ 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 409,
147368};
147369static const YYCODETYPE yy_lookahead[] = {
147370 /* 0 */ 184, 238, 239, 240, 238, 239, 240, 163, 155, 156,
147371 /* 10 */ 157, 158, 159, 160, 163, 191, 192, 183, 165, 19,
147372 /* 20 */ 167, 258, 202, 203, 200, 191, 163, 174, 184, 185,
147373 /* 30 */ 174, 31, 163, 163, 171, 184, 185, 35, 175, 39,
147374 /* 40 */ 179, 180, 181, 43, 44, 45, 46, 47, 48, 49,
147375 /* 50 */ 50, 51, 52, 53, 54, 55, 56, 57, 184, 206,
147376 /* 60 */ 207, 163, 206, 207, 220, 163, 16, 163, 66, 163,
147377 /* 70 */ 59, 270, 219, 229, 273, 219, 74, 208, 174, 223,
147378 /* 80 */ 224, 163, 184, 185, 163, 232, 184, 185, 184, 185,
147379 /* 90 */ 184, 185, 92, 93, 94, 95, 96, 97, 98, 99,
147380 /* 100 */ 100, 101, 102, 233, 198, 184, 185, 96, 97, 163,
147381 /* 110 */ 206, 207, 19, 163, 261, 104, 105, 106, 107, 198,
147382 /* 120 */ 109, 119, 220, 219, 220, 274, 275, 77, 117, 79,
147383 /* 130 */ 187, 229, 19, 229, 184, 185, 43, 44, 45, 46,
147384 /* 140 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
147385 /* 150 */ 57, 233, 141, 134, 143, 102, 43, 44, 45, 46,
147386 /* 160 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
147387 /* 170 */ 57, 152, 274, 216, 276, 218, 83, 163, 85, 233,
147388 /* 180 */ 67, 238, 239, 240, 11, 92, 93, 94, 95, 96,
147389 /* 190 */ 97, 98, 99, 100, 101, 102, 19, 54, 55, 56,
147390 /* 200 */ 57, 58, 163, 26, 163, 92, 93, 94, 95, 96,
147391 /* 210 */ 97, 98, 99, 100, 101, 102, 54, 55, 56, 57,
147392 /* 220 */ 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
147393 /* 230 */ 53, 54, 55, 56, 57, 92, 93, 94, 95, 96,
147394 /* 240 */ 97, 98, 99, 100, 101, 102, 69, 96, 97, 98,
147395 /* 250 */ 99, 100, 101, 102, 92, 93, 94, 95, 96, 97,
147396 /* 260 */ 98, 99, 100, 101, 102, 81, 179, 180, 181, 92,
147397 /* 270 */ 93, 94, 95, 96, 97, 98, 99, 100, 101, 102,
147398 /* 280 */ 163, 267, 268, 163, 22, 23, 59, 163, 26, 19,
147399 /* 290 */ 117, 118, 175, 109, 24, 59, 92, 93, 94, 95,
147400 /* 300 */ 96, 97, 98, 99, 100, 101, 102, 268, 184, 185,
147401 /* 310 */ 269, 127, 128, 43, 44, 45, 46, 47, 48, 49,
147402 /* 320 */ 50, 51, 52, 53, 54, 55, 56, 57, 157, 158,
147403 /* 330 */ 159, 160, 105, 106, 107, 163, 165, 59, 167, 184,
147404 /* 340 */ 90, 105, 106, 107, 108, 174, 73, 111, 112, 113,
147405 /* 350 */ 19, 22, 163, 91, 81, 163, 106, 121, 81, 132,
147406 /* 360 */ 110, 16, 92, 93, 94, 95, 96, 97, 98, 99,
147407 /* 370 */ 100, 101, 102, 184, 185, 255, 98, 206, 207, 26,
147408 /* 380 */ 101, 102, 19, 105, 106, 107, 23, 198, 59, 116,
147409 /* 390 */ 219, 141, 142, 143, 24, 163, 187, 205, 274, 275,
147410 /* 400 */ 127, 128, 182, 232, 127, 128, 43, 44, 45, 46,
147411 /* 410 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
147412 /* 420 */ 57, 158, 77, 160, 79, 59, 26, 182, 165, 59,
147413 /* 430 */ 167, 199, 261, 102, 105, 106, 107, 174, 72, 108,
147414 /* 440 */ 109, 110, 111, 112, 113, 114, 59, 238, 239, 240,
147415 /* 450 */ 123, 120, 125, 126, 163, 92, 93, 94, 95, 96,
147416 /* 460 */ 97, 98, 99, 100, 101, 102, 163, 163, 163, 206,
147417 /* 470 */ 207, 105, 106, 107, 254, 19, 106, 90, 197, 23,
147418 /* 480 */ 127, 128, 219, 238, 239, 240, 22, 184, 185, 184,
147419 /* 490 */ 185, 22, 105, 106, 149, 232, 205, 110, 163, 43,
147420 /* 500 */ 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
147421 /* 510 */ 54, 55, 56, 57, 98, 99, 100, 101, 102, 184,
147422 /* 520 */ 185, 163, 53, 59, 261, 220, 117, 118, 141, 142,
147423 /* 530 */ 143, 131, 174, 59, 229, 116, 117, 118, 163, 59,
147424 /* 540 */ 163, 163, 184, 185, 59, 242, 72, 22, 92, 93,
147425 /* 550 */ 94, 95, 96, 97, 98, 99, 100, 101, 102, 184,
147426 /* 560 */ 185, 24, 184, 185, 206, 207, 202, 203, 19, 105,
147427 /* 570 */ 106, 107, 23, 198, 22, 174, 198, 219, 220, 105,
147428 /* 580 */ 106, 107, 96, 97, 59, 105, 106, 107, 22, 174,
147429 /* 590 */ 59, 106, 43, 44, 45, 46, 47, 48, 49, 50,
147430 /* 600 */ 51, 52, 53, 54, 55, 56, 57, 206, 207, 12,
147431 /* 610 */ 108, 59, 132, 111, 112, 113, 46, 47, 48, 49,
147432 /* 620 */ 219, 206, 207, 121, 27, 59, 163, 141, 207, 143,
147433 /* 630 */ 105, 106, 107, 163, 219, 234, 105, 106, 107, 42,
147434 /* 640 */ 219, 92, 93, 94, 95, 96, 97, 98, 99, 100,
147435 /* 650 */ 101, 102, 76, 163, 184, 185, 163, 105, 106, 107,
147436 /* 660 */ 63, 19, 86, 163, 163, 23, 163, 130, 205, 21,
147437 /* 670 */ 73, 105, 106, 107, 184, 185, 163, 184, 185, 237,
147438 /* 680 */ 110, 180, 181, 180, 181, 43, 44, 45, 46, 47,
147439 /* 690 */ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
147440 /* 700 */ 174, 163, 163, 22, 23, 163, 163, 26, 22, 23,
147441 /* 710 */ 220, 29, 73, 220, 272, 33, 22, 163, 24, 19,
147442 /* 720 */ 174, 208, 259, 184, 185, 19, 184, 185, 80, 175,
147443 /* 730 */ 230, 174, 206, 207, 92, 93, 94, 95, 96, 97,
147444 /* 740 */ 98, 99, 100, 101, 102, 219, 46, 65, 247, 195,
147445 /* 750 */ 247, 197, 206, 207, 19, 116, 117, 118, 23, 220,
147446 /* 760 */ 112, 174, 220, 206, 207, 219, 22, 174, 24, 174,
147447 /* 770 */ 22, 23, 91, 264, 265, 168, 219, 91, 43, 44,
147448 /* 780 */ 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
147449 /* 790 */ 55, 56, 57, 206, 207, 12, 163, 149, 255, 206,
147450 /* 800 */ 207, 206, 207, 59, 104, 23, 219, 163, 26, 163,
147451 /* 810 */ 27, 105, 219, 163, 219, 163, 211, 184, 185, 163,
147452 /* 820 */ 120, 163, 146, 163, 148, 42, 221, 92, 93, 94,
147453 /* 830 */ 95, 96, 97, 98, 99, 100, 101, 102, 163, 91,
147454 /* 840 */ 184, 185, 184, 185, 184, 185, 63, 19, 163, 205,
147455 /* 850 */ 106, 23, 245, 163, 208, 248, 116, 117, 118, 184,
147456 /* 860 */ 185, 163, 163, 7, 8, 9, 163, 19, 26, 184,
147457 /* 870 */ 185, 43, 44, 45, 46, 47, 48, 49, 50, 51,
147458 /* 880 */ 52, 53, 54, 55, 56, 57, 163, 184, 185, 107,
147459 /* 890 */ 163, 43, 44, 45, 46, 47, 48, 49, 50, 51,
147460 /* 900 */ 52, 53, 54, 55, 56, 57, 208, 255, 177, 178,
147461 /* 910 */ 163, 184, 185, 163, 132, 163, 141, 163, 143, 22,
147462 /* 920 */ 92, 93, 94, 95, 96, 97, 98, 99, 100, 101,
147463 /* 930 */ 102, 184, 185, 163, 184, 185, 184, 185, 184, 185,
147464 /* 940 */ 92, 93, 94, 95, 96, 97, 98, 99, 100, 101,
147465 /* 950 */ 102, 163, 163, 163, 184, 185, 163, 115, 163, 163,
147466 /* 960 */ 163, 163, 15, 163, 163, 163, 163, 163, 23, 163,
147467 /* 970 */ 163, 26, 184, 185, 184, 185, 163, 184, 185, 184,
147468 /* 980 */ 185, 184, 185, 163, 184, 185, 184, 185, 184, 185,
147469 /* 990 */ 184, 185, 163, 96, 97, 147, 163, 184, 185, 163,
147470 /* 1000 */ 199, 163, 163, 205, 184, 185, 163, 60, 163, 141,
147471 /* 1010 */ 163, 143, 163, 184, 185, 19, 163, 184, 185, 230,
147472 /* 1020 */ 184, 185, 184, 185, 206, 207, 230, 184, 185, 184,
147473 /* 1030 */ 185, 184, 185, 184, 185, 19, 163, 219, 231, 43,
147474 /* 1040 */ 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
147475 /* 1050 */ 54, 55, 56, 57, 163, 26, 163, 184, 185, 43,
147476 /* 1060 */ 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
147477 /* 1070 */ 54, 55, 56, 57, 163, 184, 185, 184, 185, 163,
147478 /* 1080 */ 182, 163, 163, 163, 163, 163, 22, 163, 92, 93,
147479 /* 1090 */ 94, 95, 96, 97, 98, 99, 100, 101, 102, 163,
147480 /* 1100 */ 184, 185, 184, 185, 163, 184, 185, 163, 92, 93,
147481 /* 1110 */ 94, 95, 96, 97, 98, 99, 100, 101, 102, 163,
147482 /* 1120 */ 184, 185, 98, 59, 163, 184, 185, 205, 184, 185,
147483 /* 1130 */ 23, 206, 207, 26, 163, 26, 107, 153, 154, 237,
147484 /* 1140 */ 184, 185, 231, 147, 219, 184, 185, 249, 124, 127,
147485 /* 1150 */ 128, 231, 254, 129, 163, 231, 177, 178, 262, 263,
147486 /* 1160 */ 118, 132, 19, 19, 46, 223, 224, 31, 24, 23,
147487 /* 1170 */ 106, 124, 26, 22, 272, 39, 129, 23, 109, 110,
147488 /* 1180 */ 26, 163, 140, 19, 22, 234, 59, 43, 44, 45,
147489 /* 1190 */ 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
147490 /* 1200 */ 56, 57, 231, 7, 8, 193, 59, 43, 44, 45,
147491 /* 1210 */ 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
147492 /* 1220 */ 56, 57, 104, 61, 23, 23, 23, 26, 26, 26,
147493 /* 1230 */ 163, 23, 23, 106, 26, 26, 92, 93, 94, 95,
147494 /* 1240 */ 96, 97, 98, 99, 100, 101, 102, 138, 105, 23,
147495 /* 1250 */ 59, 23, 26, 106, 26, 163, 92, 93, 94, 95,
147496 /* 1260 */ 96, 97, 98, 99, 100, 101, 102, 110, 23, 23,
147497 /* 1270 */ 23, 26, 26, 26, 163, 163, 19, 120, 163, 163,
147498 /* 1280 */ 163, 130, 163, 163, 163, 163, 163, 163, 163, 193,
147499 /* 1290 */ 193, 163, 163, 163, 163, 225, 19, 106, 163, 222,
147500 /* 1300 */ 163, 44, 45, 46, 47, 48, 49, 50, 51, 52,
147501 /* 1310 */ 53, 54, 55, 56, 57, 163, 163, 203, 163, 163,
147502 /* 1320 */ 222, 163, 45, 46, 47, 48, 49, 50, 51, 52,
147503 /* 1330 */ 53, 54, 55, 56, 57, 163, 163, 163, 163, 163,
147504 /* 1340 */ 251, 250, 209, 19, 20, 182, 22, 161, 222, 92,
147505 /* 1350 */ 93, 94, 95, 96, 97, 98, 99, 100, 101, 102,
147506 /* 1360 */ 36, 222, 222, 260, 226, 188, 256, 226, 187, 92,
147507 /* 1370 */ 93, 94, 95, 96, 97, 98, 99, 100, 101, 102,
147508 /* 1380 */ 210, 213, 213, 59, 213, 196, 192, 187, 256, 244,
147509 /* 1390 */ 212, 187, 226, 19, 20, 71, 22, 210, 166, 60,
147510 /* 1400 */ 130, 170, 260, 170, 38, 81, 257, 257, 170, 104,
147511 /* 1410 */ 36, 22, 43, 201, 90, 236, 138, 235, 213, 18,
147512 /* 1420 */ 96, 97, 48, 204, 204, 204, 204, 103, 170, 105,
147513 /* 1430 */ 106, 107, 18, 59, 110, 169, 213, 213, 201, 170,
147514 /* 1440 */ 201, 169, 236, 213, 146, 71, 235, 62, 253, 252,
147515 /* 1450 */ 170, 127, 128, 169, 22, 170, 82, 189, 169, 104,
147516 /* 1460 */ 170, 87, 169, 189, 90, 141, 142, 143, 144, 145,
147517 /* 1470 */ 96, 97, 186, 186, 186, 64, 194, 103, 186, 105,
147518 /* 1480 */ 106, 107, 115, 189, 110, 188, 186, 186, 19, 20,
147519 /* 1490 */ 194, 22, 186, 189, 102, 246, 246, 189, 133, 228,
147520 /* 1500 */ 104, 228, 227, 227, 170, 36, 134, 228, 227, 19,
147521 /* 1510 */ 20, 228, 22, 84, 271, 141, 142, 143, 144, 145,
147522 /* 1520 */ 0, 1, 2, 216, 22, 5, 36, 137, 59, 227,
147523 /* 1530 */ 10, 11, 12, 13, 14, 217, 269, 17, 216, 22,
147524 /* 1540 */ 71, 170, 243, 146, 241, 217, 136, 215, 135, 59,
147525 /* 1550 */ 30, 82, 32, 25, 214, 213, 87, 173, 26, 90,
147526 /* 1560 */ 40, 71, 13, 172, 164, 96, 97, 164, 6, 162,
147527 /* 1570 */ 162, 162, 103, 263, 105, 106, 107, 266, 266, 110,
147528 /* 1580 */ 90, 176, 176, 190, 182, 190, 96, 97, 98, 4,
147529 /* 1590 */ 70, 176, 3, 103, 182, 105, 106, 107, 78, 182,
147530 /* 1600 */ 110, 81, 182, 182, 182, 182, 182, 151, 88, 22,
147531 /* 1610 */ 141, 142, 143, 144, 145, 15, 89, 16, 23, 23,
147532 /* 1620 */ 128, 19, 20, 139, 22, 119, 131, 24, 20, 133,
147533 /* 1630 */ 16, 141, 142, 143, 144, 145, 1, 140, 36, 131,
147534 /* 1640 */ 119, 61, 122, 37, 139, 53, 53, 127, 128, 119,
147535 /* 1650 */ 53, 53, 105, 34, 130, 1, 5, 104, 22, 149,
147536 /* 1660 */ 26, 59, 68, 75, 41, 130, 24, 68, 104, 20,
147537 /* 1670 */ 150, 19, 120, 71, 114, 22, 67, 22, 22, 67,
147538 /* 1680 */ 23, 22, 22, 67, 82, 37, 28, 23, 138, 87,
147539 /* 1690 */ 22, 153, 90, 23, 23, 26, 23, 22, 96, 97,
147540 /* 1700 */ 24, 23, 22, 24, 130, 103, 23, 105, 106, 107,
147541 /* 1710 */ 1, 2, 110, 23, 5, 105, 34, 22, 132, 10,
147542 /* 1720 */ 11, 12, 13, 14, 26, 34, 17, 34, 85, 83,
147543 /* 1730 */ 44, 19, 20, 23, 22, 24, 75, 34, 23, 30,
147544 /* 1740 */ 26, 32, 26, 141, 142, 143, 144, 145, 36, 40,
147545 /* 1750 */ 23, 23, 23, 23, 11, 23, 22, 26, 22, 22,
147546 /* 1760 */ 22, 19, 20, 23, 22, 26, 15, 23, 22, 124,
147547 /* 1770 */ 130, 59, 23, 1, 130, 277, 277, 130, 36, 70,
147548 /* 1780 */ 130, 277, 277, 71, 277, 277, 277, 78, 277, 277,
147549 /* 1790 */ 81, 277, 277, 277, 277, 277, 277, 88, 277, 277,
147550 /* 1800 */ 277, 59, 90, 277, 277, 277, 277, 277, 96, 97,
147551 /* 1810 */ 277, 277, 277, 71, 277, 103, 277, 105, 106, 107,
147552 /* 1820 */ 277, 277, 110, 277, 277, 277, 277, 277, 277, 277,
147553 /* 1830 */ 277, 122, 90, 277, 277, 277, 127, 128, 96, 97,
147554 /* 1840 */ 277, 277, 277, 277, 277, 103, 277, 105, 106, 107,
147555 /* 1850 */ 277, 277, 110, 141, 142, 143, 144, 145, 277, 150,
147556 /* 1860 */ 277, 277, 277, 5, 277, 277, 277, 277, 10, 11,
147557 /* 1870 */ 12, 13, 14, 277, 277, 17, 277, 277, 277, 277,
147558 /* 1880 */ 277, 277, 277, 141, 142, 143, 144, 145, 30, 277,
147559 /* 1890 */ 32, 277, 277, 277, 277, 277, 277, 277, 40, 277,
147560 /* 1900 */ 277, 277, 277, 277, 277, 277, 277, 277, 277, 277,
147561 /* 1910 */ 277, 277, 277, 277, 277, 277, 277, 277, 277, 277,
147562 /* 1920 */ 277, 277, 277, 277, 277, 277, 277, 277, 70, 277,
147563 /* 1930 */ 277, 277, 277, 277, 277, 277, 78, 277, 277, 81,
147564 /* 1940 */ 277, 277, 277, 277, 277, 277, 88, 277, 277, 277,
147565 /* 1950 */ 277, 277, 277, 277, 277, 277, 277, 277, 277, 277,
147566 /* 1960 */ 277, 277, 277, 277, 277, 277, 277, 277, 277, 277,
147567 /* 1970 */ 277, 277, 277, 277, 277, 277, 277, 277, 277, 277,
147568 /* 1980 */ 122, 277, 277, 277, 277, 127, 128, 277, 277, 277,
147569 /* 1990 */ 277, 277, 277, 277, 277, 277, 277, 277, 277, 277,
147570 /* 2000 */ 277, 277, 277, 277, 277, 277, 277, 277, 150, 277,
147571 /* 2010 */ 277, 277, 277, 277, 277, 277, 277, 277, 277,
147572};
147573#define YY_SHIFT_COUNT (520)
147574#define YY_SHIFT_MIN (0)
147575#define YY_SHIFT_MAX (1858)
147576static const unsigned short int yy_shift_ofst[] = {
147577 /* 0 */ 1709, 1520, 1858, 1324, 1324, 277, 1374, 1469, 1602, 1712,
147578 /* 10 */ 1712, 1712, 273, 0, 0, 113, 1016, 1712, 1712, 1712,
147579 /* 20 */ 1712, 1712, 1712, 1712, 1712, 1712, 1712, 11, 11, 236,
147580 /* 30 */ 184, 277, 277, 277, 277, 277, 277, 93, 177, 270,
147581 /* 40 */ 363, 456, 549, 642, 735, 828, 848, 996, 1144, 1016,
147582 /* 50 */ 1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016,
147583 /* 60 */ 1016, 1016, 1016, 1016, 1016, 1016, 1164, 1016, 1257, 1277,
147584 /* 70 */ 1277, 1490, 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712,
147585 /* 80 */ 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712,
147586 /* 90 */ 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712,
147587 /* 100 */ 1712, 1712, 1712, 1742, 1712, 1712, 1712, 1712, 1712, 1712,
147588 /* 110 */ 1712, 1712, 1712, 1712, 1712, 1712, 1712, 143, 162, 162,
147589 /* 120 */ 162, 162, 162, 204, 151, 416, 531, 648, 700, 531,
147590 /* 130 */ 486, 486, 531, 353, 353, 353, 353, 409, 279, 53,
147591 /* 140 */ 2009, 2009, 331, 331, 331, 329, 366, 329, 329, 597,
147592 /* 150 */ 597, 464, 474, 262, 681, 531, 531, 531, 531, 531,
147593 /* 160 */ 531, 531, 531, 531, 531, 531, 531, 531, 531, 531,
147594 /* 170 */ 531, 531, 531, 531, 531, 531, 531, 173, 485, 984,
147595 /* 180 */ 984, 576, 485, 19, 1022, 2009, 2009, 2009, 387, 250,
147596 /* 190 */ 250, 525, 502, 278, 552, 227, 480, 566, 531, 531,
147597 /* 200 */ 531, 531, 531, 531, 531, 531, 531, 531, 639, 531,
147598 /* 210 */ 531, 531, 531, 531, 531, 531, 531, 531, 531, 531,
147599 /* 220 */ 531, 2, 2, 2, 531, 531, 531, 531, 782, 531,
147600 /* 230 */ 531, 531, 744, 531, 531, 783, 531, 531, 531, 531,
147601 /* 240 */ 531, 531, 531, 531, 419, 682, 327, 370, 370, 370,
147602 /* 250 */ 370, 1029, 327, 327, 1024, 897, 856, 947, 1109, 706,
147603 /* 260 */ 706, 1143, 1109, 1109, 1143, 842, 945, 1118, 1136, 1136,
147604 /* 270 */ 1136, 706, 676, 400, 1047, 694, 1339, 1270, 1270, 1366,
147605 /* 280 */ 1366, 1270, 1305, 1389, 1369, 1278, 1401, 1401, 1401, 1401,
147606 /* 290 */ 1270, 1414, 1278, 1278, 1305, 1389, 1369, 1369, 1278, 1270,
147607 /* 300 */ 1414, 1298, 1385, 1270, 1414, 1432, 1270, 1414, 1270, 1414,
147608 /* 310 */ 1432, 1355, 1355, 1355, 1411, 1432, 1355, 1367, 1355, 1411,
147609 /* 320 */ 1355, 1355, 1432, 1392, 1392, 1432, 1365, 1396, 1365, 1396,
147610 /* 330 */ 1365, 1396, 1365, 1396, 1270, 1372, 1429, 1502, 1390, 1372,
147611 /* 340 */ 1517, 1270, 1397, 1390, 1410, 1413, 1278, 1528, 1532, 1549,
147612 /* 350 */ 1549, 1562, 1562, 1562, 2009, 2009, 2009, 2009, 2009, 2009,
147613 /* 360 */ 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009,
147614 /* 370 */ 570, 345, 686, 748, 50, 740, 1064, 1107, 469, 537,
147615 /* 380 */ 1042, 1146, 1162, 1154, 1201, 1202, 1203, 1208, 1209, 1127,
147616 /* 390 */ 1069, 1196, 1157, 1147, 1226, 1228, 1245, 775, 868, 1246,
147617 /* 400 */ 1247, 1191, 1151, 1585, 1589, 1587, 1456, 1600, 1527, 1601,
147618 /* 410 */ 1595, 1596, 1492, 1484, 1506, 1603, 1495, 1608, 1496, 1614,
147619 /* 420 */ 1635, 1508, 1497, 1521, 1580, 1606, 1505, 1592, 1593, 1597,
147620 /* 430 */ 1598, 1530, 1547, 1619, 1524, 1654, 1651, 1636, 1553, 1510,
147621 /* 440 */ 1594, 1634, 1599, 1588, 1623, 1535, 1564, 1642, 1649, 1652,
147622 /* 450 */ 1552, 1560, 1653, 1609, 1655, 1656, 1657, 1659, 1612, 1658,
147623 /* 460 */ 1660, 1616, 1648, 1664, 1550, 1668, 1538, 1670, 1671, 1669,
147624 /* 470 */ 1673, 1675, 1676, 1678, 1680, 1679, 1574, 1683, 1690, 1610,
147625 /* 480 */ 1682, 1695, 1586, 1698, 1691, 1698, 1693, 1643, 1661, 1646,
147626 /* 490 */ 1686, 1710, 1711, 1714, 1716, 1703, 1715, 1698, 1727, 1728,
147627 /* 500 */ 1729, 1730, 1731, 1732, 1734, 1743, 1736, 1737, 1740, 1744,
147628 /* 510 */ 1738, 1746, 1739, 1645, 1640, 1644, 1647, 1650, 1749, 1751,
147629 /* 520 */ 1772,
147630};
147631#define YY_REDUCE_COUNT (369)
147632#define YY_REDUCE_MIN (-237)
147633#define YY_REDUCE_MAX (1424)
147634static const short yy_reduce_ofst[] = {
147635 /* 0 */ -147, 171, 263, -96, 358, -144, -149, -102, 124, -156,
147636 /* 10 */ -98, 305, 401, -57, 209, -237, 245, -94, -79, 189,
147637 /* 20 */ 375, 490, 493, 378, 303, 539, 542, 501, 503, 554,
147638 /* 30 */ 415, 526, 546, 557, 587, 593, 595, -234, -234, -234,
147639 /* 40 */ -234, -234, -234, -234, -234, -234, -234, -234, -234, -234,
147640 /* 50 */ -234, -234, -234, -234, -234, -234, -234, -234, -234, -234,
147641 /* 60 */ -234, -234, -234, -234, -234, -234, -234, -234, -234, -234,
147642 /* 70 */ -234, -50, 335, 470, 633, 656, 658, 660, 675, 685,
147643 /* 80 */ 703, 727, 747, 750, 752, 754, 770, 788, 790, 793,
147644 /* 90 */ 795, 797, 800, 802, 804, 806, 813, 820, 829, 833,
147645 /* 100 */ 836, 838, 843, 845, 847, 849, 873, 891, 893, 916,
147646 /* 110 */ 918, 921, 936, 941, 944, 956, 961, -234, -234, -234,
147647 /* 120 */ -234, -234, -234, -234, -234, -234, 463, 607, -176, 14,
147648 /* 130 */ -139, 87, -137, 818, 925, 818, 925, 898, -234, -234,
147649 /* 140 */ -234, -234, -166, -166, -166, -130, -131, -82, -54, -180,
147650 /* 150 */ 364, 41, 513, 509, 509, 117, 500, 789, 796, 646,
147651 /* 160 */ 192, 291, 644, 798, 120, 807, 543, 911, 920, 652,
147652 /* 170 */ 924, 922, 232, 698, 801, 971, 39, 220, 731, 442,
147653 /* 180 */ 902, -199, 979, -43, 421, 896, 942, 605, -184, -126,
147654 /* 190 */ 155, 172, 281, 304, 377, 538, 650, 690, 699, 723,
147655 /* 200 */ 803, 839, 853, 919, 991, 1018, 1067, 1092, 951, 1111,
147656 /* 210 */ 1112, 1115, 1116, 1117, 1119, 1120, 1121, 1122, 1123, 1124,
147657 /* 220 */ 1125, 1012, 1096, 1097, 1128, 1129, 1130, 1131, 1070, 1135,
147658 /* 230 */ 1137, 1152, 1077, 1153, 1155, 1114, 1156, 304, 1158, 1172,
147659 /* 240 */ 1173, 1174, 1175, 1176, 1089, 1091, 1133, 1098, 1126, 1139,
147660 /* 250 */ 1140, 1070, 1133, 1133, 1170, 1163, 1186, 1103, 1168, 1138,
147661 /* 260 */ 1141, 1110, 1169, 1171, 1132, 1177, 1189, 1194, 1181, 1200,
147662 /* 270 */ 1204, 1166, 1145, 1178, 1187, 1232, 1142, 1231, 1233, 1149,
147663 /* 280 */ 1150, 1238, 1179, 1182, 1212, 1205, 1219, 1220, 1221, 1222,
147664 /* 290 */ 1258, 1266, 1223, 1224, 1206, 1211, 1237, 1239, 1230, 1269,
147665 /* 300 */ 1272, 1195, 1197, 1280, 1284, 1268, 1285, 1289, 1290, 1293,
147666 /* 310 */ 1274, 1286, 1287, 1288, 1282, 1294, 1292, 1297, 1300, 1296,
147667 /* 320 */ 1301, 1306, 1304, 1249, 1250, 1308, 1271, 1275, 1273, 1276,
147668 /* 330 */ 1279, 1281, 1283, 1302, 1334, 1307, 1243, 1267, 1318, 1322,
147669 /* 340 */ 1303, 1371, 1299, 1328, 1332, 1340, 1342, 1384, 1391, 1400,
147670 /* 350 */ 1403, 1407, 1408, 1409, 1311, 1312, 1310, 1405, 1402, 1412,
147671 /* 360 */ 1417, 1420, 1406, 1393, 1395, 1421, 1422, 1423, 1424, 1415,
147672};
147673static const YYACTIONTYPE yy_default[] = {
147674 /* 0 */ 1492, 1492, 1492, 1340, 1123, 1229, 1123, 1123, 1123, 1340,
147675 /* 10 */ 1340, 1340, 1123, 1259, 1259, 1391, 1154, 1123, 1123, 1123,
147676 /* 20 */ 1123, 1123, 1123, 1123, 1339, 1123, 1123, 1123, 1123, 1123,
147677 /* 30 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1265, 1123,
147678 /* 40 */ 1123, 1123, 1123, 1123, 1341, 1342, 1123, 1123, 1123, 1390,
147679 /* 50 */ 1392, 1275, 1274, 1273, 1272, 1373, 1246, 1270, 1263, 1267,
147680 /* 60 */ 1335, 1336, 1334, 1338, 1342, 1341, 1123, 1266, 1306, 1320,
147681 /* 70 */ 1305, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
147682 /* 80 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
147683 /* 90 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
147684 /* 100 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
147685 /* 110 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1314, 1319, 1325,
147686 /* 120 */ 1318, 1315, 1308, 1307, 1309, 1310, 1123, 1144, 1193, 1123,
147687 /* 130 */ 1123, 1123, 1123, 1409, 1408, 1123, 1123, 1154, 1311, 1312,
147688 /* 140 */ 1322, 1321, 1398, 1448, 1447, 1123, 1123, 1123, 1123, 1123,
147689 /* 150 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
147690 /* 160 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
147691 /* 170 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1154, 1150, 1300,
147692 /* 180 */ 1299, 1418, 1150, 1253, 1123, 1404, 1229, 1220, 1123, 1123,
147693 /* 190 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
147694 /* 200 */ 1123, 1395, 1393, 1123, 1355, 1123, 1123, 1123, 1123, 1123,
147695 /* 210 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
147696 /* 220 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
147697 /* 230 */ 1123, 1123, 1225, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
147698 /* 240 */ 1123, 1123, 1123, 1442, 1123, 1368, 1207, 1225, 1225, 1225,
147699 /* 250 */ 1225, 1227, 1208, 1206, 1219, 1154, 1130, 1484, 1269, 1248,
147700 /* 260 */ 1248, 1481, 1269, 1269, 1481, 1168, 1462, 1165, 1259, 1259,
147701 /* 270 */ 1259, 1248, 1337, 1226, 1219, 1123, 1484, 1234, 1234, 1483,
147702 /* 280 */ 1483, 1234, 1278, 1284, 1196, 1269, 1202, 1202, 1202, 1202,
147703 /* 290 */ 1234, 1141, 1269, 1269, 1278, 1284, 1196, 1196, 1269, 1234,
147704 /* 300 */ 1141, 1372, 1478, 1234, 1141, 1348, 1234, 1141, 1234, 1141,
147705 /* 310 */ 1348, 1194, 1194, 1194, 1183, 1348, 1194, 1168, 1194, 1183,
147706 /* 320 */ 1194, 1194, 1348, 1352, 1352, 1348, 1252, 1247, 1252, 1247,
147707 /* 330 */ 1252, 1247, 1252, 1247, 1234, 1253, 1417, 1123, 1264, 1253,
147708 /* 340 */ 1343, 1234, 1123, 1264, 1262, 1260, 1269, 1147, 1186, 1445,
147709 /* 350 */ 1445, 1441, 1441, 1441, 1489, 1489, 1404, 1457, 1154, 1154,
147710 /* 360 */ 1154, 1154, 1457, 1170, 1170, 1154, 1154, 1154, 1154, 1457,
147711 /* 370 */ 1123, 1123, 1123, 1123, 1123, 1123, 1452, 1123, 1357, 1238,
147712 /* 380 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
147713 /* 390 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
147714 /* 400 */ 1123, 1123, 1289, 1123, 1126, 1401, 1123, 1123, 1399, 1123,
147715 /* 410 */ 1123, 1123, 1123, 1123, 1123, 1239, 1123, 1123, 1123, 1123,
147716 /* 420 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
147717 /* 430 */ 1123, 1123, 1123, 1123, 1480, 1123, 1123, 1123, 1123, 1123,
147718 /* 440 */ 1123, 1371, 1370, 1123, 1123, 1236, 1123, 1123, 1123, 1123,
147719 /* 450 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
147720 /* 460 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
147721 /* 470 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
147722 /* 480 */ 1123, 1123, 1123, 1261, 1123, 1416, 1123, 1123, 1123, 1123,
147723 /* 490 */ 1123, 1123, 1123, 1430, 1254, 1123, 1123, 1471, 1123, 1123,
147724 /* 500 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
147725 /* 510 */ 1123, 1123, 1466, 1210, 1291, 1123, 1290, 1294, 1123, 1135,
147726 /* 520 */ 1123,
147727};
147728/********** End of lemon-generated parsing tables *****************************/
147729
147730/* The next table maps tokens (terminal symbols) into fallback tokens.
147731** If a construct like the following:
147732**
147733** %fallback ID X Y Z.
147734**
147735** appears in the grammar, then ID becomes a fallback token for X, Y,
147736** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
147737** but it does not parse, the type of the token is changed to ID and
147738** the parse is retried before an error is thrown.
147739**
147740** This feature can be used, for example, to cause some keywords in a language
147741** to revert to identifiers if they keyword does not apply in the context where
147742** it appears.
147743*/
147744#ifdef YYFALLBACK
147745static const YYCODETYPE yyFallback[] = {
147746 0, /* $ => nothing */
147747 0, /* SEMI => nothing */
147748 59, /* EXPLAIN => ID */
147749 59, /* QUERY => ID */
147750 59, /* PLAN => ID */
147751 59, /* BEGIN => ID */
147752 0, /* TRANSACTION => nothing */
147753 59, /* DEFERRED => ID */
147754 59, /* IMMEDIATE => ID */
147755 59, /* EXCLUSIVE => ID */
147756 0, /* COMMIT => nothing */
147757 59, /* END => ID */
147758 59, /* ROLLBACK => ID */
147759 59, /* SAVEPOINT => ID */
147760 59, /* RELEASE => ID */
147761 0, /* TO => nothing */
147762 0, /* TABLE => nothing */
147763 0, /* CREATE => nothing */
147764 59, /* IF => ID */
147765 0, /* NOT => nothing */
147766 0, /* EXISTS => nothing */
147767 59, /* TEMP => ID */
147768 0, /* LP => nothing */
147769 0, /* RP => nothing */
147770 0, /* AS => nothing */
147771 59, /* WITHOUT => ID */
147772 0, /* COMMA => nothing */
147773 59, /* ABORT => ID */
147774 59, /* ACTION => ID */
147775 59, /* AFTER => ID */
147776 59, /* ANALYZE => ID */
147777 59, /* ASC => ID */
147778 59, /* ATTACH => ID */
147779 59, /* BEFORE => ID */
147780 59, /* BY => ID */
147781 59, /* CASCADE => ID */
147782 59, /* CAST => ID */
147783 59, /* CONFLICT => ID */
147784 59, /* DATABASE => ID */
147785 59, /* DESC => ID */
147786 59, /* DETACH => ID */
147787 59, /* EACH => ID */
147788 59, /* FAIL => ID */
147789 0, /* OR => nothing */
147790 0, /* AND => nothing */
147791 0, /* IS => nothing */
147792 59, /* MATCH => ID */
147793 59, /* LIKE_KW => ID */
147794 0, /* BETWEEN => nothing */
147795 0, /* IN => nothing */
147796 0, /* ISNULL => nothing */
147797 0, /* NOTNULL => nothing */
147798 0, /* NE => nothing */
147799 0, /* EQ => nothing */
147800 0, /* GT => nothing */
147801 0, /* LE => nothing */
147802 0, /* LT => nothing */
147803 0, /* GE => nothing */
147804 0, /* ESCAPE => nothing */
147805 0, /* ID => nothing */
147806 59, /* COLUMNKW => ID */
147807 59, /* DO => ID */
147808 59, /* FOR => ID */
147809 59, /* IGNORE => ID */
147810 59, /* INITIALLY => ID */
147811 59, /* INSTEAD => ID */
147812 59, /* NO => ID */
147813 59, /* KEY => ID */
147814 59, /* OF => ID */
147815 59, /* OFFSET => ID */
147816 59, /* PRAGMA => ID */
147817 59, /* RAISE => ID */
147818 59, /* RECURSIVE => ID */
147819 59, /* REPLACE => ID */
147820 59, /* RESTRICT => ID */
147821 59, /* ROW => ID */
147822 59, /* ROWS => ID */
147823 59, /* TRIGGER => ID */
147824 59, /* VACUUM => ID */
147825 59, /* VIEW => ID */
147826 59, /* VIRTUAL => ID */
147827 59, /* WITH => ID */
147828 59, /* CURRENT => ID */
147829 59, /* FOLLOWING => ID */
147830 59, /* PARTITION => ID */
147831 59, /* PRECEDING => ID */
147832 59, /* RANGE => ID */
147833 59, /* UNBOUNDED => ID */
147834 59, /* REINDEX => ID */
147835 59, /* RENAME => ID */
147836 59, /* CTIME_KW => ID */
147837};
147838#endif /* YYFALLBACK */
147839
147840/* The following structure represents a single element of the
147841** parser's stack. Information stored includes:
147842**
147843** + The state number for the parser at this level of the stack.
147844**
147845** + The value of the token stored at this level of the stack.
147846** (In other words, the "major" token.)
147847**
147848** + The semantic value stored at this level of the stack. This is
147849** the information used by the action routines in the grammar.
147850** It is sometimes called the "minor" token.
147851**
147852** After the "shift" half of a SHIFTREDUCE action, the stateno field
147853** actually contains the reduce action for the second half of the
147854** SHIFTREDUCE.
147855*/
147856struct yyStackEntry {
147857 YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */
147858 YYCODETYPE major; /* The major token value. This is the code
147859 ** number for the token at this stack level */
147860 YYMINORTYPE minor; /* The user-supplied minor token value. This
147861 ** is the value of the token */
147862};
147863typedef struct yyStackEntry yyStackEntry;
147864
147865/* The state of the parser is completely contained in an instance of
147866** the following structure */
147867struct yyParser {
147868 yyStackEntry *yytos; /* Pointer to top element of the stack */
147869#ifdef YYTRACKMAXSTACKDEPTH
147870 int yyhwm; /* High-water mark of the stack */
147871#endif
147872#ifndef YYNOERRORRECOVERY
147873 int yyerrcnt; /* Shifts left before out of the error */
147874#endif
147875 sqlite3ParserARG_SDECL /* A place to hold %extra_argument */
147876 sqlite3ParserCTX_SDECL /* A place to hold %extra_context */
147877#if YYSTACKDEPTH<=0
147878 int yystksz; /* Current side of the stack */
147879 yyStackEntry *yystack; /* The parser's stack */
147880 yyStackEntry yystk0; /* First stack entry */
147881#else
147882 yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */
147883 yyStackEntry *yystackEnd; /* Last entry in the stack */
147884#endif
147885};
147886typedef struct yyParser yyParser;
147887
147888#ifndef NDEBUG
147889/* #include <stdio.h> */
147890static FILE *yyTraceFILE = 0;
147891static char *yyTracePrompt = 0;
147892#endif /* NDEBUG */
147893
147894#ifndef NDEBUG
147895/*
147896** Turn parser tracing on by giving a stream to which to write the trace
147897** and a prompt to preface each trace message. Tracing is turned off
147898** by making either argument NULL
147899**
147900** Inputs:
147901** <ul>
147902** <li> A FILE* to which trace output should be written.
147903** If NULL, then tracing is turned off.
147904** <li> A prefix string written at the beginning of every
147905** line of trace output. If NULL, then tracing is
147906** turned off.
147907** </ul>
147908**
147909** Outputs:
147910** None.
147911*/
147912SQLITE_PRIVATE void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){
147913 yyTraceFILE = TraceFILE;
147914 yyTracePrompt = zTracePrompt;
147915 if( yyTraceFILE==0 ) yyTracePrompt = 0;
147916 else if( yyTracePrompt==0 ) yyTraceFILE = 0;
147917}
147918#endif /* NDEBUG */
147919
147920#if defined(YYCOVERAGE) || !defined(NDEBUG)
147921/* For tracing shifts, the names of all terminals and nonterminals
147922** are required. The following table supplies these names */
147923static const char *const yyTokenName[] = {
147924 /* 0 */ "$",
147925 /* 1 */ "SEMI",
147926 /* 2 */ "EXPLAIN",
147927 /* 3 */ "QUERY",
147928 /* 4 */ "PLAN",
147929 /* 5 */ "BEGIN",
147930 /* 6 */ "TRANSACTION",
147931 /* 7 */ "DEFERRED",
147932 /* 8 */ "IMMEDIATE",
147933 /* 9 */ "EXCLUSIVE",
147934 /* 10 */ "COMMIT",
147935 /* 11 */ "END",
147936 /* 12 */ "ROLLBACK",
147937 /* 13 */ "SAVEPOINT",
147938 /* 14 */ "RELEASE",
147939 /* 15 */ "TO",
147940 /* 16 */ "TABLE",
147941 /* 17 */ "CREATE",
147942 /* 18 */ "IF",
147943 /* 19 */ "NOT",
147944 /* 20 */ "EXISTS",
147945 /* 21 */ "TEMP",
147946 /* 22 */ "LP",
147947 /* 23 */ "RP",
147948 /* 24 */ "AS",
147949 /* 25 */ "WITHOUT",
147950 /* 26 */ "COMMA",
147951 /* 27 */ "ABORT",
147952 /* 28 */ "ACTION",
147953 /* 29 */ "AFTER",
147954 /* 30 */ "ANALYZE",
147955 /* 31 */ "ASC",
147956 /* 32 */ "ATTACH",
147957 /* 33 */ "BEFORE",
147958 /* 34 */ "BY",
147959 /* 35 */ "CASCADE",
147960 /* 36 */ "CAST",
147961 /* 37 */ "CONFLICT",
147962 /* 38 */ "DATABASE",
147963 /* 39 */ "DESC",
147964 /* 40 */ "DETACH",
147965 /* 41 */ "EACH",
147966 /* 42 */ "FAIL",
147967 /* 43 */ "OR",
147968 /* 44 */ "AND",
147969 /* 45 */ "IS",
147970 /* 46 */ "MATCH",
147971 /* 47 */ "LIKE_KW",
147972 /* 48 */ "BETWEEN",
147973 /* 49 */ "IN",
147974 /* 50 */ "ISNULL",
147975 /* 51 */ "NOTNULL",
147976 /* 52 */ "NE",
147977 /* 53 */ "EQ",
147978 /* 54 */ "GT",
147979 /* 55 */ "LE",
147980 /* 56 */ "LT",
147981 /* 57 */ "GE",
147982 /* 58 */ "ESCAPE",
147983 /* 59 */ "ID",
147984 /* 60 */ "COLUMNKW",
147985 /* 61 */ "DO",
147986 /* 62 */ "FOR",
147987 /* 63 */ "IGNORE",
147988 /* 64 */ "INITIALLY",
147989 /* 65 */ "INSTEAD",
147990 /* 66 */ "NO",
147991 /* 67 */ "KEY",
147992 /* 68 */ "OF",
147993 /* 69 */ "OFFSET",
147994 /* 70 */ "PRAGMA",
147995 /* 71 */ "RAISE",
147996 /* 72 */ "RECURSIVE",
147997 /* 73 */ "REPLACE",
147998 /* 74 */ "RESTRICT",
147999 /* 75 */ "ROW",
148000 /* 76 */ "ROWS",
148001 /* 77 */ "TRIGGER",
148002 /* 78 */ "VACUUM",
148003 /* 79 */ "VIEW",
148004 /* 80 */ "VIRTUAL",
148005 /* 81 */ "WITH",
148006 /* 82 */ "CURRENT",
148007 /* 83 */ "FOLLOWING",
148008 /* 84 */ "PARTITION",
148009 /* 85 */ "PRECEDING",
148010 /* 86 */ "RANGE",
148011 /* 87 */ "UNBOUNDED",
148012 /* 88 */ "REINDEX",
148013 /* 89 */ "RENAME",
148014 /* 90 */ "CTIME_KW",
148015 /* 91 */ "ANY",
148016 /* 92 */ "BITAND",
148017 /* 93 */ "BITOR",
148018 /* 94 */ "LSHIFT",
148019 /* 95 */ "RSHIFT",
148020 /* 96 */ "PLUS",
148021 /* 97 */ "MINUS",
148022 /* 98 */ "STAR",
148023 /* 99 */ "SLASH",
148024 /* 100 */ "REM",
148025 /* 101 */ "CONCAT",
148026 /* 102 */ "COLLATE",
148027 /* 103 */ "BITNOT",
148028 /* 104 */ "ON",
148029 /* 105 */ "INDEXED",
148030 /* 106 */ "STRING",
148031 /* 107 */ "JOIN_KW",
148032 /* 108 */ "CONSTRAINT",
148033 /* 109 */ "DEFAULT",
148034 /* 110 */ "NULL",
148035 /* 111 */ "PRIMARY",
148036 /* 112 */ "UNIQUE",
148037 /* 113 */ "CHECK",
148038 /* 114 */ "REFERENCES",
148039 /* 115 */ "AUTOINCR",
148040 /* 116 */ "INSERT",
148041 /* 117 */ "DELETE",
148042 /* 118 */ "UPDATE",
148043 /* 119 */ "SET",
148044 /* 120 */ "DEFERRABLE",
148045 /* 121 */ "FOREIGN",
148046 /* 122 */ "DROP",
148047 /* 123 */ "UNION",
148048 /* 124 */ "ALL",
148049 /* 125 */ "EXCEPT",
148050 /* 126 */ "INTERSECT",
148051 /* 127 */ "SELECT",
148052 /* 128 */ "VALUES",
148053 /* 129 */ "DISTINCT",
148054 /* 130 */ "DOT",
148055 /* 131 */ "FROM",
148056 /* 132 */ "JOIN",
148057 /* 133 */ "USING",
148058 /* 134 */ "ORDER",
148059 /* 135 */ "GROUP",
148060 /* 136 */ "HAVING",
148061 /* 137 */ "LIMIT",
148062 /* 138 */ "WHERE",
148063 /* 139 */ "INTO",
148064 /* 140 */ "NOTHING",
148065 /* 141 */ "FLOAT",
148066 /* 142 */ "BLOB",
148067 /* 143 */ "INTEGER",
148068 /* 144 */ "VARIABLE",
148069 /* 145 */ "CASE",
148070 /* 146 */ "WHEN",
148071 /* 147 */ "THEN",
148072 /* 148 */ "ELSE",
148073 /* 149 */ "INDEX",
148074 /* 150 */ "ALTER",
148075 /* 151 */ "ADD",
148076 /* 152 */ "WINDOW",
148077 /* 153 */ "OVER",
148078 /* 154 */ "FILTER",
148079 /* 155 */ "input",
148080 /* 156 */ "cmdlist",
148081 /* 157 */ "ecmd",
148082 /* 158 */ "cmdx",
148083 /* 159 */ "explain",
148084 /* 160 */ "cmd",
148085 /* 161 */ "transtype",
148086 /* 162 */ "trans_opt",
148087 /* 163 */ "nm",
148088 /* 164 */ "savepoint_opt",
148089 /* 165 */ "create_table",
148090 /* 166 */ "create_table_args",
148091 /* 167 */ "createkw",
148092 /* 168 */ "temp",
148093 /* 169 */ "ifnotexists",
148094 /* 170 */ "dbnm",
148095 /* 171 */ "columnlist",
148096 /* 172 */ "conslist_opt",
148097 /* 173 */ "table_options",
148098 /* 174 */ "select",
148099 /* 175 */ "columnname",
148100 /* 176 */ "carglist",
148101 /* 177 */ "typetoken",
148102 /* 178 */ "typename",
148103 /* 179 */ "signed",
148104 /* 180 */ "plus_num",
148105 /* 181 */ "minus_num",
148106 /* 182 */ "scanpt",
148107 /* 183 */ "ccons",
148108 /* 184 */ "term",
148109 /* 185 */ "expr",
148110 /* 186 */ "onconf",
148111 /* 187 */ "sortorder",
148112 /* 188 */ "autoinc",
148113 /* 189 */ "eidlist_opt",
148114 /* 190 */ "refargs",
148115 /* 191 */ "defer_subclause",
148116 /* 192 */ "refarg",
148117 /* 193 */ "refact",
148118 /* 194 */ "init_deferred_pred_opt",
148119 /* 195 */ "conslist",
148120 /* 196 */ "tconscomma",
148121 /* 197 */ "tcons",
148122 /* 198 */ "sortlist",
148123 /* 199 */ "eidlist",
148124 /* 200 */ "defer_subclause_opt",
148125 /* 201 */ "orconf",
148126 /* 202 */ "resolvetype",
148127 /* 203 */ "raisetype",
148128 /* 204 */ "ifexists",
148129 /* 205 */ "fullname",
148130 /* 206 */ "selectnowith",
148131 /* 207 */ "oneselect",
148132 /* 208 */ "wqlist",
148133 /* 209 */ "multiselect_op",
148134 /* 210 */ "distinct",
148135 /* 211 */ "selcollist",
148136 /* 212 */ "from",
148137 /* 213 */ "where_opt",
148138 /* 214 */ "groupby_opt",
148139 /* 215 */ "having_opt",
148140 /* 216 */ "orderby_opt",
148141 /* 217 */ "limit_opt",
148142 /* 218 */ "window_clause",
148143 /* 219 */ "values",
148144 /* 220 */ "nexprlist",
148145 /* 221 */ "sclp",
148146 /* 222 */ "as",
148147 /* 223 */ "seltablist",
148148 /* 224 */ "stl_prefix",
148149 /* 225 */ "joinop",
148150 /* 226 */ "indexed_opt",
148151 /* 227 */ "on_opt",
148152 /* 228 */ "using_opt",
148153 /* 229 */ "exprlist",
148154 /* 230 */ "xfullname",
148155 /* 231 */ "idlist",
148156 /* 232 */ "with",
148157 /* 233 */ "setlist",
148158 /* 234 */ "insert_cmd",
148159 /* 235 */ "idlist_opt",
148160 /* 236 */ "upsert",
148161 /* 237 */ "over_clause",
148162 /* 238 */ "likeop",
148163 /* 239 */ "between_op",
148164 /* 240 */ "in_op",
148165 /* 241 */ "paren_exprlist",
148166 /* 242 */ "case_operand",
148167 /* 243 */ "case_exprlist",
148168 /* 244 */ "case_else",
148169 /* 245 */ "uniqueflag",
148170 /* 246 */ "collate",
148171 /* 247 */ "nmnum",
148172 /* 248 */ "trigger_decl",
148173 /* 249 */ "trigger_cmd_list",
148174 /* 250 */ "trigger_time",
148175 /* 251 */ "trigger_event",
148176 /* 252 */ "foreach_clause",
148177 /* 253 */ "when_clause",
148178 /* 254 */ "trigger_cmd",
148179 /* 255 */ "trnm",
148180 /* 256 */ "tridxby",
148181 /* 257 */ "database_kw_opt",
148182 /* 258 */ "key_opt",
148183 /* 259 */ "add_column_fullname",
148184 /* 260 */ "kwcolumn_opt",
148185 /* 261 */ "create_vtab",
148186 /* 262 */ "vtabarglist",
148187 /* 263 */ "vtabarg",
148188 /* 264 */ "vtabargtoken",
148189 /* 265 */ "lp",
148190 /* 266 */ "anylist",
148191 /* 267 */ "windowdefn_list",
148192 /* 268 */ "windowdefn",
148193 /* 269 */ "window",
148194 /* 270 */ "frame_opt",
148195 /* 271 */ "part_opt",
148196 /* 272 */ "filter_opt",
148197 /* 273 */ "range_or_rows",
148198 /* 274 */ "frame_bound",
148199 /* 275 */ "frame_bound_s",
148200 /* 276 */ "frame_bound_e",
148201};
148202#endif /* defined(YYCOVERAGE) || !defined(NDEBUG) */
148203
148204#ifndef NDEBUG
148205/* For tracing reduce actions, the names of all rules are required.
148206*/
148207static const char *const yyRuleName[] = {
148208 /* 0 */ "explain ::= EXPLAIN",
148209 /* 1 */ "explain ::= EXPLAIN QUERY PLAN",
148210 /* 2 */ "cmdx ::= cmd",
148211 /* 3 */ "cmd ::= BEGIN transtype trans_opt",
148212 /* 4 */ "transtype ::=",
148213 /* 5 */ "transtype ::= DEFERRED",
148214 /* 6 */ "transtype ::= IMMEDIATE",
148215 /* 7 */ "transtype ::= EXCLUSIVE",
148216 /* 8 */ "cmd ::= COMMIT|END trans_opt",
148217 /* 9 */ "cmd ::= ROLLBACK trans_opt",
148218 /* 10 */ "cmd ::= SAVEPOINT nm",
148219 /* 11 */ "cmd ::= RELEASE savepoint_opt nm",
148220 /* 12 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm",
148221 /* 13 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm",
148222 /* 14 */ "createkw ::= CREATE",
148223 /* 15 */ "ifnotexists ::=",
148224 /* 16 */ "ifnotexists ::= IF NOT EXISTS",
148225 /* 17 */ "temp ::= TEMP",
148226 /* 18 */ "temp ::=",
148227 /* 19 */ "create_table_args ::= LP columnlist conslist_opt RP table_options",
148228 /* 20 */ "create_table_args ::= AS select",
148229 /* 21 */ "table_options ::=",
148230 /* 22 */ "table_options ::= WITHOUT nm",
148231 /* 23 */ "columnname ::= nm typetoken",
148232 /* 24 */ "typetoken ::=",
148233 /* 25 */ "typetoken ::= typename LP signed RP",
148234 /* 26 */ "typetoken ::= typename LP signed COMMA signed RP",
148235 /* 27 */ "typename ::= typename ID|STRING",
148236 /* 28 */ "scanpt ::=",
148237 /* 29 */ "ccons ::= CONSTRAINT nm",
148238 /* 30 */ "ccons ::= DEFAULT scanpt term scanpt",
148239 /* 31 */ "ccons ::= DEFAULT LP expr RP",
148240 /* 32 */ "ccons ::= DEFAULT PLUS term scanpt",
148241 /* 33 */ "ccons ::= DEFAULT MINUS term scanpt",
148242 /* 34 */ "ccons ::= DEFAULT scanpt ID|INDEXED",
148243 /* 35 */ "ccons ::= NOT NULL onconf",
148244 /* 36 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
148245 /* 37 */ "ccons ::= UNIQUE onconf",
148246 /* 38 */ "ccons ::= CHECK LP expr RP",
148247 /* 39 */ "ccons ::= REFERENCES nm eidlist_opt refargs",
148248 /* 40 */ "ccons ::= defer_subclause",
148249 /* 41 */ "ccons ::= COLLATE ID|STRING",
148250 /* 42 */ "autoinc ::=",
148251 /* 43 */ "autoinc ::= AUTOINCR",
148252 /* 44 */ "refargs ::=",
148253 /* 45 */ "refargs ::= refargs refarg",
148254 /* 46 */ "refarg ::= MATCH nm",
148255 /* 47 */ "refarg ::= ON INSERT refact",
148256 /* 48 */ "refarg ::= ON DELETE refact",
148257 /* 49 */ "refarg ::= ON UPDATE refact",
148258 /* 50 */ "refact ::= SET NULL",
148259 /* 51 */ "refact ::= SET DEFAULT",
148260 /* 52 */ "refact ::= CASCADE",
148261 /* 53 */ "refact ::= RESTRICT",
148262 /* 54 */ "refact ::= NO ACTION",
148263 /* 55 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
148264 /* 56 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
148265 /* 57 */ "init_deferred_pred_opt ::=",
148266 /* 58 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
148267 /* 59 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
148268 /* 60 */ "conslist_opt ::=",
148269 /* 61 */ "tconscomma ::= COMMA",
148270 /* 62 */ "tcons ::= CONSTRAINT nm",
148271 /* 63 */ "tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf",
148272 /* 64 */ "tcons ::= UNIQUE LP sortlist RP onconf",
148273 /* 65 */ "tcons ::= CHECK LP expr RP onconf",
148274 /* 66 */ "tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt",
148275 /* 67 */ "defer_subclause_opt ::=",
148276 /* 68 */ "onconf ::=",
148277 /* 69 */ "onconf ::= ON CONFLICT resolvetype",
148278 /* 70 */ "orconf ::=",
148279 /* 71 */ "orconf ::= OR resolvetype",
148280 /* 72 */ "resolvetype ::= IGNORE",
148281 /* 73 */ "resolvetype ::= REPLACE",
148282 /* 74 */ "cmd ::= DROP TABLE ifexists fullname",
148283 /* 75 */ "ifexists ::= IF EXISTS",
148284 /* 76 */ "ifexists ::=",
148285 /* 77 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select",
148286 /* 78 */ "cmd ::= DROP VIEW ifexists fullname",
148287 /* 79 */ "cmd ::= select",
148288 /* 80 */ "select ::= WITH wqlist selectnowith",
148289 /* 81 */ "select ::= WITH RECURSIVE wqlist selectnowith",
148290 /* 82 */ "select ::= selectnowith",
148291 /* 83 */ "selectnowith ::= selectnowith multiselect_op oneselect",
148292 /* 84 */ "multiselect_op ::= UNION",
148293 /* 85 */ "multiselect_op ::= UNION ALL",
148294 /* 86 */ "multiselect_op ::= EXCEPT|INTERSECT",
148295 /* 87 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
148296 /* 88 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt",
148297 /* 89 */ "values ::= VALUES LP nexprlist RP",
148298 /* 90 */ "values ::= values COMMA LP nexprlist RP",
148299 /* 91 */ "distinct ::= DISTINCT",
148300 /* 92 */ "distinct ::= ALL",
148301 /* 93 */ "distinct ::=",
148302 /* 94 */ "sclp ::=",
148303 /* 95 */ "selcollist ::= sclp scanpt expr scanpt as",
148304 /* 96 */ "selcollist ::= sclp scanpt STAR",
148305 /* 97 */ "selcollist ::= sclp scanpt nm DOT STAR",
148306 /* 98 */ "as ::= AS nm",
148307 /* 99 */ "as ::=",
148308 /* 100 */ "from ::=",
148309 /* 101 */ "from ::= FROM seltablist",
148310 /* 102 */ "stl_prefix ::= seltablist joinop",
148311 /* 103 */ "stl_prefix ::=",
148312 /* 104 */ "seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt",
148313 /* 105 */ "seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt",
148314 /* 106 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt",
148315 /* 107 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt",
148316 /* 108 */ "dbnm ::=",
148317 /* 109 */ "dbnm ::= DOT nm",
148318 /* 110 */ "fullname ::= nm",
148319 /* 111 */ "fullname ::= nm DOT nm",
148320 /* 112 */ "xfullname ::= nm",
148321 /* 113 */ "xfullname ::= nm DOT nm",
148322 /* 114 */ "xfullname ::= nm DOT nm AS nm",
148323 /* 115 */ "xfullname ::= nm AS nm",
148324 /* 116 */ "joinop ::= COMMA|JOIN",
148325 /* 117 */ "joinop ::= JOIN_KW JOIN",
148326 /* 118 */ "joinop ::= JOIN_KW nm JOIN",
148327 /* 119 */ "joinop ::= JOIN_KW nm nm JOIN",
148328 /* 120 */ "on_opt ::= ON expr",
148329 /* 121 */ "on_opt ::=",
148330 /* 122 */ "indexed_opt ::=",
148331 /* 123 */ "indexed_opt ::= INDEXED BY nm",
148332 /* 124 */ "indexed_opt ::= NOT INDEXED",
148333 /* 125 */ "using_opt ::= USING LP idlist RP",
148334 /* 126 */ "using_opt ::=",
148335 /* 127 */ "orderby_opt ::=",
148336 /* 128 */ "orderby_opt ::= ORDER BY sortlist",
148337 /* 129 */ "sortlist ::= sortlist COMMA expr sortorder",
148338 /* 130 */ "sortlist ::= expr sortorder",
148339 /* 131 */ "sortorder ::= ASC",
148340 /* 132 */ "sortorder ::= DESC",
148341 /* 133 */ "sortorder ::=",
148342 /* 134 */ "groupby_opt ::=",
148343 /* 135 */ "groupby_opt ::= GROUP BY nexprlist",
148344 /* 136 */ "having_opt ::=",
148345 /* 137 */ "having_opt ::= HAVING expr",
148346 /* 138 */ "limit_opt ::=",
148347 /* 139 */ "limit_opt ::= LIMIT expr",
148348 /* 140 */ "limit_opt ::= LIMIT expr OFFSET expr",
148349 /* 141 */ "limit_opt ::= LIMIT expr COMMA expr",
148350 /* 142 */ "cmd ::= with DELETE FROM xfullname indexed_opt where_opt",
148351 /* 143 */ "where_opt ::=",
148352 /* 144 */ "where_opt ::= WHERE expr",
148353 /* 145 */ "cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist where_opt",
148354 /* 146 */ "setlist ::= setlist COMMA nm EQ expr",
148355 /* 147 */ "setlist ::= setlist COMMA LP idlist RP EQ expr",
148356 /* 148 */ "setlist ::= nm EQ expr",
148357 /* 149 */ "setlist ::= LP idlist RP EQ expr",
148358 /* 150 */ "cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert",
148359 /* 151 */ "cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES",
148360 /* 152 */ "upsert ::=",
148361 /* 153 */ "upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt",
148362 /* 154 */ "upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING",
148363 /* 155 */ "upsert ::= ON CONFLICT DO NOTHING",
148364 /* 156 */ "insert_cmd ::= INSERT orconf",
148365 /* 157 */ "insert_cmd ::= REPLACE",
148366 /* 158 */ "idlist_opt ::=",
148367 /* 159 */ "idlist_opt ::= LP idlist RP",
148368 /* 160 */ "idlist ::= idlist COMMA nm",
148369 /* 161 */ "idlist ::= nm",
148370 /* 162 */ "expr ::= LP expr RP",
148371 /* 163 */ "expr ::= ID|INDEXED",
148372 /* 164 */ "expr ::= JOIN_KW",
148373 /* 165 */ "expr ::= nm DOT nm",
148374 /* 166 */ "expr ::= nm DOT nm DOT nm",
148375 /* 167 */ "term ::= NULL|FLOAT|BLOB",
148376 /* 168 */ "term ::= STRING",
148377 /* 169 */ "term ::= INTEGER",
148378 /* 170 */ "expr ::= VARIABLE",
148379 /* 171 */ "expr ::= expr COLLATE ID|STRING",
148380 /* 172 */ "expr ::= CAST LP expr AS typetoken RP",
148381 /* 173 */ "expr ::= ID|INDEXED LP distinct exprlist RP",
148382 /* 174 */ "expr ::= ID|INDEXED LP STAR RP",
148383 /* 175 */ "expr ::= ID|INDEXED LP distinct exprlist RP over_clause",
148384 /* 176 */ "expr ::= ID|INDEXED LP STAR RP over_clause",
148385 /* 177 */ "term ::= CTIME_KW",
148386 /* 178 */ "expr ::= LP nexprlist COMMA expr RP",
148387 /* 179 */ "expr ::= expr AND expr",
148388 /* 180 */ "expr ::= expr OR expr",
148389 /* 181 */ "expr ::= expr LT|GT|GE|LE expr",
148390 /* 182 */ "expr ::= expr EQ|NE expr",
148391 /* 183 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
148392 /* 184 */ "expr ::= expr PLUS|MINUS expr",
148393 /* 185 */ "expr ::= expr STAR|SLASH|REM expr",
148394 /* 186 */ "expr ::= expr CONCAT expr",
148395 /* 187 */ "likeop ::= NOT LIKE_KW|MATCH",
148396 /* 188 */ "expr ::= expr likeop expr",
148397 /* 189 */ "expr ::= expr likeop expr ESCAPE expr",
148398 /* 190 */ "expr ::= expr ISNULL|NOTNULL",
148399 /* 191 */ "expr ::= expr NOT NULL",
148400 /* 192 */ "expr ::= expr IS expr",
148401 /* 193 */ "expr ::= expr IS NOT expr",
148402 /* 194 */ "expr ::= NOT expr",
148403 /* 195 */ "expr ::= BITNOT expr",
148404 /* 196 */ "expr ::= PLUS|MINUS expr",
148405 /* 197 */ "between_op ::= BETWEEN",
148406 /* 198 */ "between_op ::= NOT BETWEEN",
148407 /* 199 */ "expr ::= expr between_op expr AND expr",
148408 /* 200 */ "in_op ::= IN",
148409 /* 201 */ "in_op ::= NOT IN",
148410 /* 202 */ "expr ::= expr in_op LP exprlist RP",
148411 /* 203 */ "expr ::= LP select RP",
148412 /* 204 */ "expr ::= expr in_op LP select RP",
148413 /* 205 */ "expr ::= expr in_op nm dbnm paren_exprlist",
148414 /* 206 */ "expr ::= EXISTS LP select RP",
148415 /* 207 */ "expr ::= CASE case_operand case_exprlist case_else END",
148416 /* 208 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
148417 /* 209 */ "case_exprlist ::= WHEN expr THEN expr",
148418 /* 210 */ "case_else ::= ELSE expr",
148419 /* 211 */ "case_else ::=",
148420 /* 212 */ "case_operand ::= expr",
148421 /* 213 */ "case_operand ::=",
148422 /* 214 */ "exprlist ::=",
148423 /* 215 */ "nexprlist ::= nexprlist COMMA expr",
148424 /* 216 */ "nexprlist ::= expr",
148425 /* 217 */ "paren_exprlist ::=",
148426 /* 218 */ "paren_exprlist ::= LP exprlist RP",
148427 /* 219 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt",
148428 /* 220 */ "uniqueflag ::= UNIQUE",
148429 /* 221 */ "uniqueflag ::=",
148430 /* 222 */ "eidlist_opt ::=",
148431 /* 223 */ "eidlist_opt ::= LP eidlist RP",
148432 /* 224 */ "eidlist ::= eidlist COMMA nm collate sortorder",
148433 /* 225 */ "eidlist ::= nm collate sortorder",
148434 /* 226 */ "collate ::=",
148435 /* 227 */ "collate ::= COLLATE ID|STRING",
148436 /* 228 */ "cmd ::= DROP INDEX ifexists fullname",
148437 /* 229 */ "cmd ::= VACUUM",
148438 /* 230 */ "cmd ::= VACUUM nm",
148439 /* 231 */ "cmd ::= PRAGMA nm dbnm",
148440 /* 232 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
148441 /* 233 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
148442 /* 234 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
148443 /* 235 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
148444 /* 236 */ "plus_num ::= PLUS INTEGER|FLOAT",
148445 /* 237 */ "minus_num ::= MINUS INTEGER|FLOAT",
148446 /* 238 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
148447 /* 239 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
148448 /* 240 */ "trigger_time ::= BEFORE|AFTER",
148449 /* 241 */ "trigger_time ::= INSTEAD OF",
148450 /* 242 */ "trigger_time ::=",
148451 /* 243 */ "trigger_event ::= DELETE|INSERT",
148452 /* 244 */ "trigger_event ::= UPDATE",
148453 /* 245 */ "trigger_event ::= UPDATE OF idlist",
148454 /* 246 */ "when_clause ::=",
148455 /* 247 */ "when_clause ::= WHEN expr",
148456 /* 248 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
148457 /* 249 */ "trigger_cmd_list ::= trigger_cmd SEMI",
148458 /* 250 */ "trnm ::= nm DOT nm",
148459 /* 251 */ "tridxby ::= INDEXED BY nm",
148460 /* 252 */ "tridxby ::= NOT INDEXED",
148461 /* 253 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt scanpt",
148462 /* 254 */ "trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt",
148463 /* 255 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt",
148464 /* 256 */ "trigger_cmd ::= scanpt select scanpt",
148465 /* 257 */ "expr ::= RAISE LP IGNORE RP",
148466 /* 258 */ "expr ::= RAISE LP raisetype COMMA nm RP",
148467 /* 259 */ "raisetype ::= ROLLBACK",
148468 /* 260 */ "raisetype ::= ABORT",
148469 /* 261 */ "raisetype ::= FAIL",
148470 /* 262 */ "cmd ::= DROP TRIGGER ifexists fullname",
148471 /* 263 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
148472 /* 264 */ "cmd ::= DETACH database_kw_opt expr",
148473 /* 265 */ "key_opt ::=",
148474 /* 266 */ "key_opt ::= KEY expr",
148475 /* 267 */ "cmd ::= REINDEX",
148476 /* 268 */ "cmd ::= REINDEX nm dbnm",
148477 /* 269 */ "cmd ::= ANALYZE",
148478 /* 270 */ "cmd ::= ANALYZE nm dbnm",
148479 /* 271 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
148480 /* 272 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist",
148481 /* 273 */ "add_column_fullname ::= fullname",
148482 /* 274 */ "cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm",
148483 /* 275 */ "cmd ::= create_vtab",
148484 /* 276 */ "cmd ::= create_vtab LP vtabarglist RP",
148485 /* 277 */ "create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm",
148486 /* 278 */ "vtabarg ::=",
148487 /* 279 */ "vtabargtoken ::= ANY",
148488 /* 280 */ "vtabargtoken ::= lp anylist RP",
148489 /* 281 */ "lp ::= LP",
148490 /* 282 */ "with ::= WITH wqlist",
148491 /* 283 */ "with ::= WITH RECURSIVE wqlist",
148492 /* 284 */ "wqlist ::= nm eidlist_opt AS LP select RP",
148493 /* 285 */ "wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP",
148494 /* 286 */ "windowdefn_list ::= windowdefn",
148495 /* 287 */ "windowdefn_list ::= windowdefn_list COMMA windowdefn",
148496 /* 288 */ "windowdefn ::= nm AS window",
148497 /* 289 */ "window ::= LP part_opt orderby_opt frame_opt RP",
148498 /* 290 */ "part_opt ::= PARTITION BY nexprlist",
148499 /* 291 */ "part_opt ::=",
148500 /* 292 */ "frame_opt ::=",
148501 /* 293 */ "frame_opt ::= range_or_rows frame_bound_s",
148502 /* 294 */ "frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e",
148503 /* 295 */ "range_or_rows ::= RANGE",
148504 /* 296 */ "range_or_rows ::= ROWS",
148505 /* 297 */ "frame_bound_s ::= frame_bound",
148506 /* 298 */ "frame_bound_s ::= UNBOUNDED PRECEDING",
148507 /* 299 */ "frame_bound_e ::= frame_bound",
148508 /* 300 */ "frame_bound_e ::= UNBOUNDED FOLLOWING",
148509 /* 301 */ "frame_bound ::= expr PRECEDING",
148510 /* 302 */ "frame_bound ::= CURRENT ROW",
148511 /* 303 */ "frame_bound ::= expr FOLLOWING",
148512 /* 304 */ "window_clause ::= WINDOW windowdefn_list",
148513 /* 305 */ "over_clause ::= filter_opt OVER window",
148514 /* 306 */ "over_clause ::= filter_opt OVER nm",
148515 /* 307 */ "filter_opt ::=",
148516 /* 308 */ "filter_opt ::= FILTER LP WHERE expr RP",
148517 /* 309 */ "input ::= cmdlist",
148518 /* 310 */ "cmdlist ::= cmdlist ecmd",
148519 /* 311 */ "cmdlist ::= ecmd",
148520 /* 312 */ "ecmd ::= SEMI",
148521 /* 313 */ "ecmd ::= cmdx SEMI",
148522 /* 314 */ "ecmd ::= explain cmdx",
148523 /* 315 */ "trans_opt ::=",
148524 /* 316 */ "trans_opt ::= TRANSACTION",
148525 /* 317 */ "trans_opt ::= TRANSACTION nm",
148526 /* 318 */ "savepoint_opt ::= SAVEPOINT",
148527 /* 319 */ "savepoint_opt ::=",
148528 /* 320 */ "cmd ::= create_table create_table_args",
148529 /* 321 */ "columnlist ::= columnlist COMMA columnname carglist",
148530 /* 322 */ "columnlist ::= columnname carglist",
148531 /* 323 */ "nm ::= ID|INDEXED",
148532 /* 324 */ "nm ::= STRING",
148533 /* 325 */ "nm ::= JOIN_KW",
148534 /* 326 */ "typetoken ::= typename",
148535 /* 327 */ "typename ::= ID|STRING",
148536 /* 328 */ "signed ::= plus_num",
148537 /* 329 */ "signed ::= minus_num",
148538 /* 330 */ "carglist ::= carglist ccons",
148539 /* 331 */ "carglist ::=",
148540 /* 332 */ "ccons ::= NULL onconf",
148541 /* 333 */ "conslist_opt ::= COMMA conslist",
148542 /* 334 */ "conslist ::= conslist tconscomma tcons",
148543 /* 335 */ "conslist ::= tcons",
148544 /* 336 */ "tconscomma ::=",
148545 /* 337 */ "defer_subclause_opt ::= defer_subclause",
148546 /* 338 */ "resolvetype ::= raisetype",
148547 /* 339 */ "selectnowith ::= oneselect",
148548 /* 340 */ "oneselect ::= values",
148549 /* 341 */ "sclp ::= selcollist COMMA",
148550 /* 342 */ "as ::= ID|STRING",
148551 /* 343 */ "expr ::= term",
148552 /* 344 */ "likeop ::= LIKE_KW|MATCH",
148553 /* 345 */ "exprlist ::= nexprlist",
148554 /* 346 */ "nmnum ::= plus_num",
148555 /* 347 */ "nmnum ::= nm",
148556 /* 348 */ "nmnum ::= ON",
148557 /* 349 */ "nmnum ::= DELETE",
148558 /* 350 */ "nmnum ::= DEFAULT",
148559 /* 351 */ "plus_num ::= INTEGER|FLOAT",
148560 /* 352 */ "foreach_clause ::=",
148561 /* 353 */ "foreach_clause ::= FOR EACH ROW",
148562 /* 354 */ "trnm ::= nm",
148563 /* 355 */ "tridxby ::=",
148564 /* 356 */ "database_kw_opt ::= DATABASE",
148565 /* 357 */ "database_kw_opt ::=",
148566 /* 358 */ "kwcolumn_opt ::=",
148567 /* 359 */ "kwcolumn_opt ::= COLUMNKW",
148568 /* 360 */ "vtabarglist ::= vtabarg",
148569 /* 361 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
148570 /* 362 */ "vtabarg ::= vtabarg vtabargtoken",
148571 /* 363 */ "anylist ::=",
148572 /* 364 */ "anylist ::= anylist LP anylist RP",
148573 /* 365 */ "anylist ::= anylist ANY",
148574 /* 366 */ "with ::=",
148575};
148576#endif /* NDEBUG */
148577
148578
148579#if YYSTACKDEPTH<=0
148580/*
148581** Try to increase the size of the parser stack. Return the number
148582** of errors. Return 0 on success.
148583*/
148584static int yyGrowStack(yyParser *p){
148585 int newSize;
148586 int idx;
148587 yyStackEntry *pNew;
148588
148589 newSize = p->yystksz*2 + 100;
148590 idx = p->yytos ? (int)(p->yytos - p->yystack) : 0;
148591 if( p->yystack==&p->yystk0 ){
148592 pNew = malloc(newSize*sizeof(pNew[0]));
148593 if( pNew ) pNew[0] = p->yystk0;
148594 }else{
148595 pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
148596 }
148597 if( pNew ){
148598 p->yystack = pNew;
148599 p->yytos = &p->yystack[idx];
148600#ifndef NDEBUG
148601 if( yyTraceFILE ){
148602 fprintf(yyTraceFILE,"%sStack grows from %d to %d entries.\n",
148603 yyTracePrompt, p->yystksz, newSize);
148604 }
148605#endif
148606 p->yystksz = newSize;
148607 }
148608 return pNew==0;
148609}
148610#endif
148611
148612/* Datatype of the argument to the memory allocated passed as the
148613** second argument to sqlite3ParserAlloc() below. This can be changed by
148614** putting an appropriate #define in the %include section of the input
148615** grammar.
148616*/
148617#ifndef YYMALLOCARGTYPE
148618# define YYMALLOCARGTYPE size_t
148619#endif
148620
148621/* Initialize a new parser that has already been allocated.
148622*/
148623SQLITE_PRIVATE void sqlite3ParserInit(void *yypRawParser sqlite3ParserCTX_PDECL){
148624 yyParser *yypParser = (yyParser*)yypRawParser;
148625 sqlite3ParserCTX_STORE
148626#ifdef YYTRACKMAXSTACKDEPTH
148627 yypParser->yyhwm = 0;
148628#endif
148629#if YYSTACKDEPTH<=0
148630 yypParser->yytos = NULL;
148631 yypParser->yystack = NULL;
148632 yypParser->yystksz = 0;
148633 if( yyGrowStack(yypParser) ){
148634 yypParser->yystack = &yypParser->yystk0;
148635 yypParser->yystksz = 1;
148636 }
148637#endif
148638#ifndef YYNOERRORRECOVERY
148639 yypParser->yyerrcnt = -1;
148640#endif
148641 yypParser->yytos = yypParser->yystack;
148642 yypParser->yystack[0].stateno = 0;
148643 yypParser->yystack[0].major = 0;
148644#if YYSTACKDEPTH>0
148645 yypParser->yystackEnd = &yypParser->yystack[YYSTACKDEPTH-1];
148646#endif
148647}
148648
148649#ifndef sqlite3Parser_ENGINEALWAYSONSTACK
148650/*
148651** This function allocates a new parser.
148652** The only argument is a pointer to a function which works like
148653** malloc.
148654**
148655** Inputs:
148656** A pointer to the function used to allocate memory.
148657**
148658** Outputs:
148659** A pointer to a parser. This pointer is used in subsequent calls
148660** to sqlite3Parser and sqlite3ParserFree.
148661*/
148662SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(YYMALLOCARGTYPE) sqlite3ParserCTX_PDECL){
148663 yyParser *yypParser;
148664 yypParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) );
148665 if( yypParser ){
148666 sqlite3ParserCTX_STORE
148667 sqlite3ParserInit(yypParser sqlite3ParserCTX_PARAM);
148668 }
148669 return (void*)yypParser;
148670}
148671#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
148672
148673
148674/* The following function deletes the "minor type" or semantic value
148675** associated with a symbol. The symbol can be either a terminal
148676** or nonterminal. "yymajor" is the symbol code, and "yypminor" is
148677** a pointer to the value to be deleted. The code used to do the
148678** deletions is derived from the %destructor and/or %token_destructor
148679** directives of the input grammar.
148680*/
148681static void yy_destructor(
148682 yyParser *yypParser, /* The parser */
148683 YYCODETYPE yymajor, /* Type code for object to destroy */
148684 YYMINORTYPE *yypminor /* The object to be destroyed */
148685){
148686 sqlite3ParserARG_FETCH
148687 sqlite3ParserCTX_FETCH
148688 switch( yymajor ){
148689 /* Here is inserted the actions which take place when a
148690 ** terminal or non-terminal is destroyed. This can happen
148691 ** when the symbol is popped from the stack during a
148692 ** reduce or during error processing or when a parser is
148693 ** being destroyed before it is finished parsing.
148694 **
148695 ** Note: during a reduce, the only symbols destroyed are those
148696 ** which appear on the RHS of the rule, but which are *not* used
148697 ** inside the C code.
148698 */
148699/********* Begin destructor definitions ***************************************/
148700 case 174: /* select */
148701 case 206: /* selectnowith */
148702 case 207: /* oneselect */
148703 case 219: /* values */
148704{
148705sqlite3SelectDelete(pParse->db, (yypminor->yy489));
148706}
148707 break;
148708 case 184: /* term */
148709 case 185: /* expr */
148710 case 213: /* where_opt */
148711 case 215: /* having_opt */
148712 case 227: /* on_opt */
148713 case 242: /* case_operand */
148714 case 244: /* case_else */
148715 case 253: /* when_clause */
148716 case 258: /* key_opt */
148717 case 272: /* filter_opt */
148718{
148719sqlite3ExprDelete(pParse->db, (yypminor->yy18));
148720}
148721 break;
148722 case 189: /* eidlist_opt */
148723 case 198: /* sortlist */
148724 case 199: /* eidlist */
148725 case 211: /* selcollist */
148726 case 214: /* groupby_opt */
148727 case 216: /* orderby_opt */
148728 case 220: /* nexprlist */
148729 case 221: /* sclp */
148730 case 229: /* exprlist */
148731 case 233: /* setlist */
148732 case 241: /* paren_exprlist */
148733 case 243: /* case_exprlist */
148734 case 271: /* part_opt */
148735{
148736sqlite3ExprListDelete(pParse->db, (yypminor->yy420));
148737}
148738 break;
148739 case 205: /* fullname */
148740 case 212: /* from */
148741 case 223: /* seltablist */
148742 case 224: /* stl_prefix */
148743 case 230: /* xfullname */
148744{
148745sqlite3SrcListDelete(pParse->db, (yypminor->yy135));
148746}
148747 break;
148748 case 208: /* wqlist */
148749{
148750sqlite3WithDelete(pParse->db, (yypminor->yy449));
148751}
148752 break;
148753 case 218: /* window_clause */
148754 case 267: /* windowdefn_list */
148755{
148756sqlite3WindowListDelete(pParse->db, (yypminor->yy327));
148757}
148758 break;
148759 case 228: /* using_opt */
148760 case 231: /* idlist */
148761 case 235: /* idlist_opt */
148762{
148763sqlite3IdListDelete(pParse->db, (yypminor->yy48));
148764}
148765 break;
148766 case 237: /* over_clause */
148767 case 268: /* windowdefn */
148768 case 269: /* window */
148769 case 270: /* frame_opt */
148770{
148771sqlite3WindowDelete(pParse->db, (yypminor->yy327));
148772}
148773 break;
148774 case 249: /* trigger_cmd_list */
148775 case 254: /* trigger_cmd */
148776{
148777sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy207));
148778}
148779 break;
148780 case 251: /* trigger_event */
148781{
148782sqlite3IdListDelete(pParse->db, (yypminor->yy34).b);
148783}
148784 break;
148785 case 274: /* frame_bound */
148786 case 275: /* frame_bound_s */
148787 case 276: /* frame_bound_e */
148788{
148789sqlite3ExprDelete(pParse->db, (yypminor->yy119).pExpr);
148790}
148791 break;
148792/********* End destructor definitions *****************************************/
148793 default: break; /* If no destructor action specified: do nothing */
148794 }
148795}
148796
148797/*
148798** Pop the parser's stack once.
148799**
148800** If there is a destructor routine associated with the token which
148801** is popped from the stack, then call it.
148802*/
148803static void yy_pop_parser_stack(yyParser *pParser){
148804 yyStackEntry *yytos;
148805 assert( pParser->yytos!=0 );
148806 assert( pParser->yytos > pParser->yystack );
148807 yytos = pParser->yytos--;
148808#ifndef NDEBUG
148809 if( yyTraceFILE ){
148810 fprintf(yyTraceFILE,"%sPopping %s\n",
148811 yyTracePrompt,
148812 yyTokenName[yytos->major]);
148813 }
148814#endif
148815 yy_destructor(pParser, yytos->major, &yytos->minor);
148816}
148817
148818/*
148819** Clear all secondary memory allocations from the parser
148820*/
148821SQLITE_PRIVATE void sqlite3ParserFinalize(void *p){
148822 yyParser *pParser = (yyParser*)p;
148823 while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
148824#if YYSTACKDEPTH<=0
148825 if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
148826#endif
148827}
148828
148829#ifndef sqlite3Parser_ENGINEALWAYSONSTACK
148830/*
148831** Deallocate and destroy a parser. Destructors are called for
148832** all stack elements before shutting the parser down.
148833**
148834** If the YYPARSEFREENEVERNULL macro exists (for example because it
148835** is defined in a %include section of the input grammar) then it is
148836** assumed that the input pointer is never NULL.
148837*/
148838SQLITE_PRIVATE void sqlite3ParserFree(
148839 void *p, /* The parser to be deleted */
148840 void (*freeProc)(void*) /* Function used to reclaim memory */
148841){
148842#ifndef YYPARSEFREENEVERNULL
148843 if( p==0 ) return;
148844#endif
148845 sqlite3ParserFinalize(p);
148846 (*freeProc)(p);
148847}
148848#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
148849
148850/*
148851** Return the peak depth of the stack for a parser.
148852*/
148853#ifdef YYTRACKMAXSTACKDEPTH
148854SQLITE_PRIVATE int sqlite3ParserStackPeak(void *p){
148855 yyParser *pParser = (yyParser*)p;
148856 return pParser->yyhwm;
148857}
148858#endif
148859
148860/* This array of booleans keeps track of the parser statement
148861** coverage. The element yycoverage[X][Y] is set when the parser
148862** is in state X and has a lookahead token Y. In a well-tested
148863** systems, every element of this matrix should end up being set.
148864*/
148865#if defined(YYCOVERAGE)
148866static unsigned char yycoverage[YYNSTATE][YYNTOKEN];
148867#endif
148868
148869/*
148870** Write into out a description of every state/lookahead combination that
148871**
148872** (1) has not been used by the parser, and
148873** (2) is not a syntax error.
148874**
148875** Return the number of missed state/lookahead combinations.
148876*/
148877#if defined(YYCOVERAGE)
148878SQLITE_PRIVATE int sqlite3ParserCoverage(FILE *out){
148879 int stateno, iLookAhead, i;
148880 int nMissed = 0;
148881 for(stateno=0; stateno<YYNSTATE; stateno++){
148882 i = yy_shift_ofst[stateno];
148883 for(iLookAhead=0; iLookAhead<YYNTOKEN; iLookAhead++){
148884 if( yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
148885 if( yycoverage[stateno][iLookAhead]==0 ) nMissed++;
148886 if( out ){
148887 fprintf(out,"State %d lookahead %s %s\n", stateno,
148888 yyTokenName[iLookAhead],
148889 yycoverage[stateno][iLookAhead] ? "ok" : "missed");
148890 }
148891 }
148892 }
148893 return nMissed;
148894}
148895#endif
148896
148897/*
148898** Find the appropriate action for a parser given the terminal
148899** look-ahead token iLookAhead.
148900*/
148901static YYACTIONTYPE yy_find_shift_action(
148902 YYCODETYPE iLookAhead, /* The look-ahead token */
148903 YYACTIONTYPE stateno /* Current state number */
148904){
148905 int i;
148906
148907 if( stateno>YY_MAX_SHIFT ) return stateno;
148908 assert( stateno <= YY_SHIFT_COUNT );
148909#if defined(YYCOVERAGE)
148910 yycoverage[stateno][iLookAhead] = 1;
148911#endif
148912 do{
148913 i = yy_shift_ofst[stateno];
148914 assert( i>=0 );
148915 /* assert( i+YYNTOKEN<=(int)YY_NLOOKAHEAD ); */
148916 assert( iLookAhead!=YYNOCODE );
148917 assert( iLookAhead < YYNTOKEN );
148918 i += iLookAhead;
148919 if( i>=YY_NLOOKAHEAD || yy_lookahead[i]!=iLookAhead ){
148920#ifdef YYFALLBACK
148921 YYCODETYPE iFallback; /* Fallback token */
148922 if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
148923 && (iFallback = yyFallback[iLookAhead])!=0 ){
148924#ifndef NDEBUG
148925 if( yyTraceFILE ){
148926 fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
148927 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
148928 }
148929#endif
148930 assert( yyFallback[iFallback]==0 ); /* Fallback loop must terminate */
148931 iLookAhead = iFallback;
148932 continue;
148933 }
148934#endif
148935#ifdef YYWILDCARD
148936 {
148937 int j = i - iLookAhead + YYWILDCARD;
148938 if(
148939#if YY_SHIFT_MIN+YYWILDCARD<0
148940 j>=0 &&
148941#endif
148942#if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT
148943 j<YY_ACTTAB_COUNT &&
148944#endif
148945 j<(int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])) &&
148946 yy_lookahead[j]==YYWILDCARD && iLookAhead>0
148947 ){
148948#ifndef NDEBUG
148949 if( yyTraceFILE ){
148950 fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
148951 yyTracePrompt, yyTokenName[iLookAhead],
148952 yyTokenName[YYWILDCARD]);
148953 }
148954#endif /* NDEBUG */
148955 return yy_action[j];
148956 }
148957 }
148958#endif /* YYWILDCARD */
148959 return yy_default[stateno];
148960 }else{
148961 return yy_action[i];
148962 }
148963 }while(1);
148964}
148965
148966/*
148967** Find the appropriate action for a parser given the non-terminal
148968** look-ahead token iLookAhead.
148969*/
148970static YYACTIONTYPE yy_find_reduce_action(
148971 YYACTIONTYPE stateno, /* Current state number */
148972 YYCODETYPE iLookAhead /* The look-ahead token */
148973){
148974 int i;
148975#ifdef YYERRORSYMBOL
148976 if( stateno>YY_REDUCE_COUNT ){
148977 return yy_default[stateno];
148978 }
148979#else
148980 assert( stateno<=YY_REDUCE_COUNT );
148981#endif
148982 i = yy_reduce_ofst[stateno];
148983 assert( iLookAhead!=YYNOCODE );
148984 i += iLookAhead;
148985#ifdef YYERRORSYMBOL
148986 if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
148987 return yy_default[stateno];
148988 }
148989#else
148990 assert( i>=0 && i<YY_ACTTAB_COUNT );
148991 assert( yy_lookahead[i]==iLookAhead );
148992#endif
148993 return yy_action[i];
148994}
148995
148996/*
148997** The following routine is called if the stack overflows.
148998*/
148999static void yyStackOverflow(yyParser *yypParser){
149000 sqlite3ParserARG_FETCH
149001 sqlite3ParserCTX_FETCH
149002#ifndef NDEBUG
149003 if( yyTraceFILE ){
149004 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
149005 }
149006#endif
149007 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
149008 /* Here code is inserted which will execute if the parser
149009 ** stack every overflows */
149010/******** Begin %stack_overflow code ******************************************/
149011
149012 sqlite3ErrorMsg(pParse, "parser stack overflow");
149013/******** End %stack_overflow code ********************************************/
149014 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument var */
149015 sqlite3ParserCTX_STORE
149016}
149017
149018/*
149019** Print tracing information for a SHIFT action
149020*/
149021#ifndef NDEBUG
149022static void yyTraceShift(yyParser *yypParser, int yyNewState, const char *zTag){
149023 if( yyTraceFILE ){
149024 if( yyNewState<YYNSTATE ){
149025 fprintf(yyTraceFILE,"%s%s '%s', go to state %d\n",
149026 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
149027 yyNewState);
149028 }else{
149029 fprintf(yyTraceFILE,"%s%s '%s', pending reduce %d\n",
149030 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
149031 yyNewState - YY_MIN_REDUCE);
149032 }
149033 }
149034}
149035#else
149036# define yyTraceShift(X,Y,Z)
149037#endif
149038
149039/*
149040** Perform a shift action.
149041*/
149042static void yy_shift(
149043 yyParser *yypParser, /* The parser to be shifted */
149044 YYACTIONTYPE yyNewState, /* The new state to shift in */
149045 YYCODETYPE yyMajor, /* The major token to shift in */
149046 sqlite3ParserTOKENTYPE yyMinor /* The minor token to shift in */
149047){
149048 yyStackEntry *yytos;
149049 yypParser->yytos++;
149050#ifdef YYTRACKMAXSTACKDEPTH
149051 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
149052 yypParser->yyhwm++;
149053 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) );
149054 }
149055#endif
149056#if YYSTACKDEPTH>0
149057 if( yypParser->yytos>yypParser->yystackEnd ){
149058 yypParser->yytos--;
149059 yyStackOverflow(yypParser);
149060 return;
149061 }
149062#else
149063 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){
149064 if( yyGrowStack(yypParser) ){
149065 yypParser->yytos--;
149066 yyStackOverflow(yypParser);
149067 return;
149068 }
149069 }
149070#endif
149071 if( yyNewState > YY_MAX_SHIFT ){
149072 yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
149073 }
149074 yytos = yypParser->yytos;
149075 yytos->stateno = yyNewState;
149076 yytos->major = yyMajor;
149077 yytos->minor.yy0 = yyMinor;
149078 yyTraceShift(yypParser, yyNewState, "Shift");
149079}
149080
149081/* The following table contains information about every rule that
149082** is used during the reduce.
149083*/
149084static const struct {
149085 YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */
149086 signed char nrhs; /* Negative of the number of RHS symbols in the rule */
149087} yyRuleInfo[] = {
149088 { 159, -1 }, /* (0) explain ::= EXPLAIN */
149089 { 159, -3 }, /* (1) explain ::= EXPLAIN QUERY PLAN */
149090 { 158, -1 }, /* (2) cmdx ::= cmd */
149091 { 160, -3 }, /* (3) cmd ::= BEGIN transtype trans_opt */
149092 { 161, 0 }, /* (4) transtype ::= */
149093 { 161, -1 }, /* (5) transtype ::= DEFERRED */
149094 { 161, -1 }, /* (6) transtype ::= IMMEDIATE */
149095 { 161, -1 }, /* (7) transtype ::= EXCLUSIVE */
149096 { 160, -2 }, /* (8) cmd ::= COMMIT|END trans_opt */
149097 { 160, -2 }, /* (9) cmd ::= ROLLBACK trans_opt */
149098 { 160, -2 }, /* (10) cmd ::= SAVEPOINT nm */
149099 { 160, -3 }, /* (11) cmd ::= RELEASE savepoint_opt nm */
149100 { 160, -5 }, /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
149101 { 165, -6 }, /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
149102 { 167, -1 }, /* (14) createkw ::= CREATE */
149103 { 169, 0 }, /* (15) ifnotexists ::= */
149104 { 169, -3 }, /* (16) ifnotexists ::= IF NOT EXISTS */
149105 { 168, -1 }, /* (17) temp ::= TEMP */
149106 { 168, 0 }, /* (18) temp ::= */
149107 { 166, -5 }, /* (19) create_table_args ::= LP columnlist conslist_opt RP table_options */
149108 { 166, -2 }, /* (20) create_table_args ::= AS select */
149109 { 173, 0 }, /* (21) table_options ::= */
149110 { 173, -2 }, /* (22) table_options ::= WITHOUT nm */
149111 { 175, -2 }, /* (23) columnname ::= nm typetoken */
149112 { 177, 0 }, /* (24) typetoken ::= */
149113 { 177, -4 }, /* (25) typetoken ::= typename LP signed RP */
149114 { 177, -6 }, /* (26) typetoken ::= typename LP signed COMMA signed RP */
149115 { 178, -2 }, /* (27) typename ::= typename ID|STRING */
149116 { 182, 0 }, /* (28) scanpt ::= */
149117 { 183, -2 }, /* (29) ccons ::= CONSTRAINT nm */
149118 { 183, -4 }, /* (30) ccons ::= DEFAULT scanpt term scanpt */
149119 { 183, -4 }, /* (31) ccons ::= DEFAULT LP expr RP */
149120 { 183, -4 }, /* (32) ccons ::= DEFAULT PLUS term scanpt */
149121 { 183, -4 }, /* (33) ccons ::= DEFAULT MINUS term scanpt */
149122 { 183, -3 }, /* (34) ccons ::= DEFAULT scanpt ID|INDEXED */
149123 { 183, -3 }, /* (35) ccons ::= NOT NULL onconf */
149124 { 183, -5 }, /* (36) ccons ::= PRIMARY KEY sortorder onconf autoinc */
149125 { 183, -2 }, /* (37) ccons ::= UNIQUE onconf */
149126 { 183, -4 }, /* (38) ccons ::= CHECK LP expr RP */
149127 { 183, -4 }, /* (39) ccons ::= REFERENCES nm eidlist_opt refargs */
149128 { 183, -1 }, /* (40) ccons ::= defer_subclause */
149129 { 183, -2 }, /* (41) ccons ::= COLLATE ID|STRING */
149130 { 188, 0 }, /* (42) autoinc ::= */
149131 { 188, -1 }, /* (43) autoinc ::= AUTOINCR */
149132 { 190, 0 }, /* (44) refargs ::= */
149133 { 190, -2 }, /* (45) refargs ::= refargs refarg */
149134 { 192, -2 }, /* (46) refarg ::= MATCH nm */
149135 { 192, -3 }, /* (47) refarg ::= ON INSERT refact */
149136 { 192, -3 }, /* (48) refarg ::= ON DELETE refact */
149137 { 192, -3 }, /* (49) refarg ::= ON UPDATE refact */
149138 { 193, -2 }, /* (50) refact ::= SET NULL */
149139 { 193, -2 }, /* (51) refact ::= SET DEFAULT */
149140 { 193, -1 }, /* (52) refact ::= CASCADE */
149141 { 193, -1 }, /* (53) refact ::= RESTRICT */
149142 { 193, -2 }, /* (54) refact ::= NO ACTION */
149143 { 191, -3 }, /* (55) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
149144 { 191, -2 }, /* (56) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
149145 { 194, 0 }, /* (57) init_deferred_pred_opt ::= */
149146 { 194, -2 }, /* (58) init_deferred_pred_opt ::= INITIALLY DEFERRED */
149147 { 194, -2 }, /* (59) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
149148 { 172, 0 }, /* (60) conslist_opt ::= */
149149 { 196, -1 }, /* (61) tconscomma ::= COMMA */
149150 { 197, -2 }, /* (62) tcons ::= CONSTRAINT nm */
149151 { 197, -7 }, /* (63) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
149152 { 197, -5 }, /* (64) tcons ::= UNIQUE LP sortlist RP onconf */
149153 { 197, -5 }, /* (65) tcons ::= CHECK LP expr RP onconf */
149154 { 197, -10 }, /* (66) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
149155 { 200, 0 }, /* (67) defer_subclause_opt ::= */
149156 { 186, 0 }, /* (68) onconf ::= */
149157 { 186, -3 }, /* (69) onconf ::= ON CONFLICT resolvetype */
149158 { 201, 0 }, /* (70) orconf ::= */
149159 { 201, -2 }, /* (71) orconf ::= OR resolvetype */
149160 { 202, -1 }, /* (72) resolvetype ::= IGNORE */
149161 { 202, -1 }, /* (73) resolvetype ::= REPLACE */
149162 { 160, -4 }, /* (74) cmd ::= DROP TABLE ifexists fullname */
149163 { 204, -2 }, /* (75) ifexists ::= IF EXISTS */
149164 { 204, 0 }, /* (76) ifexists ::= */
149165 { 160, -9 }, /* (77) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
149166 { 160, -4 }, /* (78) cmd ::= DROP VIEW ifexists fullname */
149167 { 160, -1 }, /* (79) cmd ::= select */
149168 { 174, -3 }, /* (80) select ::= WITH wqlist selectnowith */
149169 { 174, -4 }, /* (81) select ::= WITH RECURSIVE wqlist selectnowith */
149170 { 174, -1 }, /* (82) select ::= selectnowith */
149171 { 206, -3 }, /* (83) selectnowith ::= selectnowith multiselect_op oneselect */
149172 { 209, -1 }, /* (84) multiselect_op ::= UNION */
149173 { 209, -2 }, /* (85) multiselect_op ::= UNION ALL */
149174 { 209, -1 }, /* (86) multiselect_op ::= EXCEPT|INTERSECT */
149175 { 207, -9 }, /* (87) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
149176 { 207, -10 }, /* (88) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
149177 { 219, -4 }, /* (89) values ::= VALUES LP nexprlist RP */
149178 { 219, -5 }, /* (90) values ::= values COMMA LP nexprlist RP */
149179 { 210, -1 }, /* (91) distinct ::= DISTINCT */
149180 { 210, -1 }, /* (92) distinct ::= ALL */
149181 { 210, 0 }, /* (93) distinct ::= */
149182 { 221, 0 }, /* (94) sclp ::= */
149183 { 211, -5 }, /* (95) selcollist ::= sclp scanpt expr scanpt as */
149184 { 211, -3 }, /* (96) selcollist ::= sclp scanpt STAR */
149185 { 211, -5 }, /* (97) selcollist ::= sclp scanpt nm DOT STAR */
149186 { 222, -2 }, /* (98) as ::= AS nm */
149187 { 222, 0 }, /* (99) as ::= */
149188 { 212, 0 }, /* (100) from ::= */
149189 { 212, -2 }, /* (101) from ::= FROM seltablist */
149190 { 224, -2 }, /* (102) stl_prefix ::= seltablist joinop */
149191 { 224, 0 }, /* (103) stl_prefix ::= */
149192 { 223, -7 }, /* (104) seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
149193 { 223, -9 }, /* (105) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt */
149194 { 223, -7 }, /* (106) seltablist ::= stl_prefix LP select RP as on_opt using_opt */
149195 { 223, -7 }, /* (107) seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
149196 { 170, 0 }, /* (108) dbnm ::= */
149197 { 170, -2 }, /* (109) dbnm ::= DOT nm */
149198 { 205, -1 }, /* (110) fullname ::= nm */
149199 { 205, -3 }, /* (111) fullname ::= nm DOT nm */
149200 { 230, -1 }, /* (112) xfullname ::= nm */
149201 { 230, -3 }, /* (113) xfullname ::= nm DOT nm */
149202 { 230, -5 }, /* (114) xfullname ::= nm DOT nm AS nm */
149203 { 230, -3 }, /* (115) xfullname ::= nm AS nm */
149204 { 225, -1 }, /* (116) joinop ::= COMMA|JOIN */
149205 { 225, -2 }, /* (117) joinop ::= JOIN_KW JOIN */
149206 { 225, -3 }, /* (118) joinop ::= JOIN_KW nm JOIN */
149207 { 225, -4 }, /* (119) joinop ::= JOIN_KW nm nm JOIN */
149208 { 227, -2 }, /* (120) on_opt ::= ON expr */
149209 { 227, 0 }, /* (121) on_opt ::= */
149210 { 226, 0 }, /* (122) indexed_opt ::= */
149211 { 226, -3 }, /* (123) indexed_opt ::= INDEXED BY nm */
149212 { 226, -2 }, /* (124) indexed_opt ::= NOT INDEXED */
149213 { 228, -4 }, /* (125) using_opt ::= USING LP idlist RP */
149214 { 228, 0 }, /* (126) using_opt ::= */
149215 { 216, 0 }, /* (127) orderby_opt ::= */
149216 { 216, -3 }, /* (128) orderby_opt ::= ORDER BY sortlist */
149217 { 198, -4 }, /* (129) sortlist ::= sortlist COMMA expr sortorder */
149218 { 198, -2 }, /* (130) sortlist ::= expr sortorder */
149219 { 187, -1 }, /* (131) sortorder ::= ASC */
149220 { 187, -1 }, /* (132) sortorder ::= DESC */
149221 { 187, 0 }, /* (133) sortorder ::= */
149222 { 214, 0 }, /* (134) groupby_opt ::= */
149223 { 214, -3 }, /* (135) groupby_opt ::= GROUP BY nexprlist */
149224 { 215, 0 }, /* (136) having_opt ::= */
149225 { 215, -2 }, /* (137) having_opt ::= HAVING expr */
149226 { 217, 0 }, /* (138) limit_opt ::= */
149227 { 217, -2 }, /* (139) limit_opt ::= LIMIT expr */
149228 { 217, -4 }, /* (140) limit_opt ::= LIMIT expr OFFSET expr */
149229 { 217, -4 }, /* (141) limit_opt ::= LIMIT expr COMMA expr */
149230 { 160, -6 }, /* (142) cmd ::= with DELETE FROM xfullname indexed_opt where_opt */
149231 { 213, 0 }, /* (143) where_opt ::= */
149232 { 213, -2 }, /* (144) where_opt ::= WHERE expr */
149233 { 160, -8 }, /* (145) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist where_opt */
149234 { 233, -5 }, /* (146) setlist ::= setlist COMMA nm EQ expr */
149235 { 233, -7 }, /* (147) setlist ::= setlist COMMA LP idlist RP EQ expr */
149236 { 233, -3 }, /* (148) setlist ::= nm EQ expr */
149237 { 233, -5 }, /* (149) setlist ::= LP idlist RP EQ expr */
149238 { 160, -7 }, /* (150) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
149239 { 160, -7 }, /* (151) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES */
149240 { 236, 0 }, /* (152) upsert ::= */
149241 { 236, -11 }, /* (153) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt */
149242 { 236, -8 }, /* (154) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING */
149243 { 236, -4 }, /* (155) upsert ::= ON CONFLICT DO NOTHING */
149244 { 234, -2 }, /* (156) insert_cmd ::= INSERT orconf */
149245 { 234, -1 }, /* (157) insert_cmd ::= REPLACE */
149246 { 235, 0 }, /* (158) idlist_opt ::= */
149247 { 235, -3 }, /* (159) idlist_opt ::= LP idlist RP */
149248 { 231, -3 }, /* (160) idlist ::= idlist COMMA nm */
149249 { 231, -1 }, /* (161) idlist ::= nm */
149250 { 185, -3 }, /* (162) expr ::= LP expr RP */
149251 { 185, -1 }, /* (163) expr ::= ID|INDEXED */
149252 { 185, -1 }, /* (164) expr ::= JOIN_KW */
149253 { 185, -3 }, /* (165) expr ::= nm DOT nm */
149254 { 185, -5 }, /* (166) expr ::= nm DOT nm DOT nm */
149255 { 184, -1 }, /* (167) term ::= NULL|FLOAT|BLOB */
149256 { 184, -1 }, /* (168) term ::= STRING */
149257 { 184, -1 }, /* (169) term ::= INTEGER */
149258 { 185, -1 }, /* (170) expr ::= VARIABLE */
149259 { 185, -3 }, /* (171) expr ::= expr COLLATE ID|STRING */
149260 { 185, -6 }, /* (172) expr ::= CAST LP expr AS typetoken RP */
149261 { 185, -5 }, /* (173) expr ::= ID|INDEXED LP distinct exprlist RP */
149262 { 185, -4 }, /* (174) expr ::= ID|INDEXED LP STAR RP */
149263 { 185, -6 }, /* (175) expr ::= ID|INDEXED LP distinct exprlist RP over_clause */
149264 { 185, -5 }, /* (176) expr ::= ID|INDEXED LP STAR RP over_clause */
149265 { 184, -1 }, /* (177) term ::= CTIME_KW */
149266 { 185, -5 }, /* (178) expr ::= LP nexprlist COMMA expr RP */
149267 { 185, -3 }, /* (179) expr ::= expr AND expr */
149268 { 185, -3 }, /* (180) expr ::= expr OR expr */
149269 { 185, -3 }, /* (181) expr ::= expr LT|GT|GE|LE expr */
149270 { 185, -3 }, /* (182) expr ::= expr EQ|NE expr */
149271 { 185, -3 }, /* (183) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
149272 { 185, -3 }, /* (184) expr ::= expr PLUS|MINUS expr */
149273 { 185, -3 }, /* (185) expr ::= expr STAR|SLASH|REM expr */
149274 { 185, -3 }, /* (186) expr ::= expr CONCAT expr */
149275 { 238, -2 }, /* (187) likeop ::= NOT LIKE_KW|MATCH */
149276 { 185, -3 }, /* (188) expr ::= expr likeop expr */
149277 { 185, -5 }, /* (189) expr ::= expr likeop expr ESCAPE expr */
149278 { 185, -2 }, /* (190) expr ::= expr ISNULL|NOTNULL */
149279 { 185, -3 }, /* (191) expr ::= expr NOT NULL */
149280 { 185, -3 }, /* (192) expr ::= expr IS expr */
149281 { 185, -4 }, /* (193) expr ::= expr IS NOT expr */
149282 { 185, -2 }, /* (194) expr ::= NOT expr */
149283 { 185, -2 }, /* (195) expr ::= BITNOT expr */
149284 { 185, -2 }, /* (196) expr ::= PLUS|MINUS expr */
149285 { 239, -1 }, /* (197) between_op ::= BETWEEN */
149286 { 239, -2 }, /* (198) between_op ::= NOT BETWEEN */
149287 { 185, -5 }, /* (199) expr ::= expr between_op expr AND expr */
149288 { 240, -1 }, /* (200) in_op ::= IN */
149289 { 240, -2 }, /* (201) in_op ::= NOT IN */
149290 { 185, -5 }, /* (202) expr ::= expr in_op LP exprlist RP */
149291 { 185, -3 }, /* (203) expr ::= LP select RP */
149292 { 185, -5 }, /* (204) expr ::= expr in_op LP select RP */
149293 { 185, -5 }, /* (205) expr ::= expr in_op nm dbnm paren_exprlist */
149294 { 185, -4 }, /* (206) expr ::= EXISTS LP select RP */
149295 { 185, -5 }, /* (207) expr ::= CASE case_operand case_exprlist case_else END */
149296 { 243, -5 }, /* (208) case_exprlist ::= case_exprlist WHEN expr THEN expr */
149297 { 243, -4 }, /* (209) case_exprlist ::= WHEN expr THEN expr */
149298 { 244, -2 }, /* (210) case_else ::= ELSE expr */
149299 { 244, 0 }, /* (211) case_else ::= */
149300 { 242, -1 }, /* (212) case_operand ::= expr */
149301 { 242, 0 }, /* (213) case_operand ::= */
149302 { 229, 0 }, /* (214) exprlist ::= */
149303 { 220, -3 }, /* (215) nexprlist ::= nexprlist COMMA expr */
149304 { 220, -1 }, /* (216) nexprlist ::= expr */
149305 { 241, 0 }, /* (217) paren_exprlist ::= */
149306 { 241, -3 }, /* (218) paren_exprlist ::= LP exprlist RP */
149307 { 160, -12 }, /* (219) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
149308 { 245, -1 }, /* (220) uniqueflag ::= UNIQUE */
149309 { 245, 0 }, /* (221) uniqueflag ::= */
149310 { 189, 0 }, /* (222) eidlist_opt ::= */
149311 { 189, -3 }, /* (223) eidlist_opt ::= LP eidlist RP */
149312 { 199, -5 }, /* (224) eidlist ::= eidlist COMMA nm collate sortorder */
149313 { 199, -3 }, /* (225) eidlist ::= nm collate sortorder */
149314 { 246, 0 }, /* (226) collate ::= */
149315 { 246, -2 }, /* (227) collate ::= COLLATE ID|STRING */
149316 { 160, -4 }, /* (228) cmd ::= DROP INDEX ifexists fullname */
149317 { 160, -1 }, /* (229) cmd ::= VACUUM */
149318 { 160, -2 }, /* (230) cmd ::= VACUUM nm */
149319 { 160, -3 }, /* (231) cmd ::= PRAGMA nm dbnm */
149320 { 160, -5 }, /* (232) cmd ::= PRAGMA nm dbnm EQ nmnum */
149321 { 160, -6 }, /* (233) cmd ::= PRAGMA nm dbnm LP nmnum RP */
149322 { 160, -5 }, /* (234) cmd ::= PRAGMA nm dbnm EQ minus_num */
149323 { 160, -6 }, /* (235) cmd ::= PRAGMA nm dbnm LP minus_num RP */
149324 { 180, -2 }, /* (236) plus_num ::= PLUS INTEGER|FLOAT */
149325 { 181, -2 }, /* (237) minus_num ::= MINUS INTEGER|FLOAT */
149326 { 160, -5 }, /* (238) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
149327 { 248, -11 }, /* (239) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
149328 { 250, -1 }, /* (240) trigger_time ::= BEFORE|AFTER */
149329 { 250, -2 }, /* (241) trigger_time ::= INSTEAD OF */
149330 { 250, 0 }, /* (242) trigger_time ::= */
149331 { 251, -1 }, /* (243) trigger_event ::= DELETE|INSERT */
149332 { 251, -1 }, /* (244) trigger_event ::= UPDATE */
149333 { 251, -3 }, /* (245) trigger_event ::= UPDATE OF idlist */
149334 { 253, 0 }, /* (246) when_clause ::= */
149335 { 253, -2 }, /* (247) when_clause ::= WHEN expr */
149336 { 249, -3 }, /* (248) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
149337 { 249, -2 }, /* (249) trigger_cmd_list ::= trigger_cmd SEMI */
149338 { 255, -3 }, /* (250) trnm ::= nm DOT nm */
149339 { 256, -3 }, /* (251) tridxby ::= INDEXED BY nm */
149340 { 256, -2 }, /* (252) tridxby ::= NOT INDEXED */
149341 { 254, -8 }, /* (253) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt scanpt */
149342 { 254, -8 }, /* (254) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
149343 { 254, -6 }, /* (255) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
149344 { 254, -3 }, /* (256) trigger_cmd ::= scanpt select scanpt */
149345 { 185, -4 }, /* (257) expr ::= RAISE LP IGNORE RP */
149346 { 185, -6 }, /* (258) expr ::= RAISE LP raisetype COMMA nm RP */
149347 { 203, -1 }, /* (259) raisetype ::= ROLLBACK */
149348 { 203, -1 }, /* (260) raisetype ::= ABORT */
149349 { 203, -1 }, /* (261) raisetype ::= FAIL */
149350 { 160, -4 }, /* (262) cmd ::= DROP TRIGGER ifexists fullname */
149351 { 160, -6 }, /* (263) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
149352 { 160, -3 }, /* (264) cmd ::= DETACH database_kw_opt expr */
149353 { 258, 0 }, /* (265) key_opt ::= */
149354 { 258, -2 }, /* (266) key_opt ::= KEY expr */
149355 { 160, -1 }, /* (267) cmd ::= REINDEX */
149356 { 160, -3 }, /* (268) cmd ::= REINDEX nm dbnm */
149357 { 160, -1 }, /* (269) cmd ::= ANALYZE */
149358 { 160, -3 }, /* (270) cmd ::= ANALYZE nm dbnm */
149359 { 160, -6 }, /* (271) cmd ::= ALTER TABLE fullname RENAME TO nm */
149360 { 160, -7 }, /* (272) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
149361 { 259, -1 }, /* (273) add_column_fullname ::= fullname */
149362 { 160, -8 }, /* (274) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
149363 { 160, -1 }, /* (275) cmd ::= create_vtab */
149364 { 160, -4 }, /* (276) cmd ::= create_vtab LP vtabarglist RP */
149365 { 261, -8 }, /* (277) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
149366 { 263, 0 }, /* (278) vtabarg ::= */
149367 { 264, -1 }, /* (279) vtabargtoken ::= ANY */
149368 { 264, -3 }, /* (280) vtabargtoken ::= lp anylist RP */
149369 { 265, -1 }, /* (281) lp ::= LP */
149370 { 232, -2 }, /* (282) with ::= WITH wqlist */
149371 { 232, -3 }, /* (283) with ::= WITH RECURSIVE wqlist */
149372 { 208, -6 }, /* (284) wqlist ::= nm eidlist_opt AS LP select RP */
149373 { 208, -8 }, /* (285) wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP */
149374 { 267, -1 }, /* (286) windowdefn_list ::= windowdefn */
149375 { 267, -3 }, /* (287) windowdefn_list ::= windowdefn_list COMMA windowdefn */
149376 { 268, -3 }, /* (288) windowdefn ::= nm AS window */
149377 { 269, -5 }, /* (289) window ::= LP part_opt orderby_opt frame_opt RP */
149378 { 271, -3 }, /* (290) part_opt ::= PARTITION BY nexprlist */
149379 { 271, 0 }, /* (291) part_opt ::= */
149380 { 270, 0 }, /* (292) frame_opt ::= */
149381 { 270, -2 }, /* (293) frame_opt ::= range_or_rows frame_bound_s */
149382 { 270, -5 }, /* (294) frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e */
149383 { 273, -1 }, /* (295) range_or_rows ::= RANGE */
149384 { 273, -1 }, /* (296) range_or_rows ::= ROWS */
149385 { 275, -1 }, /* (297) frame_bound_s ::= frame_bound */
149386 { 275, -2 }, /* (298) frame_bound_s ::= UNBOUNDED PRECEDING */
149387 { 276, -1 }, /* (299) frame_bound_e ::= frame_bound */
149388 { 276, -2 }, /* (300) frame_bound_e ::= UNBOUNDED FOLLOWING */
149389 { 274, -2 }, /* (301) frame_bound ::= expr PRECEDING */
149390 { 274, -2 }, /* (302) frame_bound ::= CURRENT ROW */
149391 { 274, -2 }, /* (303) frame_bound ::= expr FOLLOWING */
149392 { 218, -2 }, /* (304) window_clause ::= WINDOW windowdefn_list */
149393 { 237, -3 }, /* (305) over_clause ::= filter_opt OVER window */
149394 { 237, -3 }, /* (306) over_clause ::= filter_opt OVER nm */
149395 { 272, 0 }, /* (307) filter_opt ::= */
149396 { 272, -5 }, /* (308) filter_opt ::= FILTER LP WHERE expr RP */
149397 { 155, -1 }, /* (309) input ::= cmdlist */
149398 { 156, -2 }, /* (310) cmdlist ::= cmdlist ecmd */
149399 { 156, -1 }, /* (311) cmdlist ::= ecmd */
149400 { 157, -1 }, /* (312) ecmd ::= SEMI */
149401 { 157, -2 }, /* (313) ecmd ::= cmdx SEMI */
149402 { 157, -2 }, /* (314) ecmd ::= explain cmdx */
149403 { 162, 0 }, /* (315) trans_opt ::= */
149404 { 162, -1 }, /* (316) trans_opt ::= TRANSACTION */
149405 { 162, -2 }, /* (317) trans_opt ::= TRANSACTION nm */
149406 { 164, -1 }, /* (318) savepoint_opt ::= SAVEPOINT */
149407 { 164, 0 }, /* (319) savepoint_opt ::= */
149408 { 160, -2 }, /* (320) cmd ::= create_table create_table_args */
149409 { 171, -4 }, /* (321) columnlist ::= columnlist COMMA columnname carglist */
149410 { 171, -2 }, /* (322) columnlist ::= columnname carglist */
149411 { 163, -1 }, /* (323) nm ::= ID|INDEXED */
149412 { 163, -1 }, /* (324) nm ::= STRING */
149413 { 163, -1 }, /* (325) nm ::= JOIN_KW */
149414 { 177, -1 }, /* (326) typetoken ::= typename */
149415 { 178, -1 }, /* (327) typename ::= ID|STRING */
149416 { 179, -1 }, /* (328) signed ::= plus_num */
149417 { 179, -1 }, /* (329) signed ::= minus_num */
149418 { 176, -2 }, /* (330) carglist ::= carglist ccons */
149419 { 176, 0 }, /* (331) carglist ::= */
149420 { 183, -2 }, /* (332) ccons ::= NULL onconf */
149421 { 172, -2 }, /* (333) conslist_opt ::= COMMA conslist */
149422 { 195, -3 }, /* (334) conslist ::= conslist tconscomma tcons */
149423 { 195, -1 }, /* (335) conslist ::= tcons */
149424 { 196, 0 }, /* (336) tconscomma ::= */
149425 { 200, -1 }, /* (337) defer_subclause_opt ::= defer_subclause */
149426 { 202, -1 }, /* (338) resolvetype ::= raisetype */
149427 { 206, -1 }, /* (339) selectnowith ::= oneselect */
149428 { 207, -1 }, /* (340) oneselect ::= values */
149429 { 221, -2 }, /* (341) sclp ::= selcollist COMMA */
149430 { 222, -1 }, /* (342) as ::= ID|STRING */
149431 { 185, -1 }, /* (343) expr ::= term */
149432 { 238, -1 }, /* (344) likeop ::= LIKE_KW|MATCH */
149433 { 229, -1 }, /* (345) exprlist ::= nexprlist */
149434 { 247, -1 }, /* (346) nmnum ::= plus_num */
149435 { 247, -1 }, /* (347) nmnum ::= nm */
149436 { 247, -1 }, /* (348) nmnum ::= ON */
149437 { 247, -1 }, /* (349) nmnum ::= DELETE */
149438 { 247, -1 }, /* (350) nmnum ::= DEFAULT */
149439 { 180, -1 }, /* (351) plus_num ::= INTEGER|FLOAT */
149440 { 252, 0 }, /* (352) foreach_clause ::= */
149441 { 252, -3 }, /* (353) foreach_clause ::= FOR EACH ROW */
149442 { 255, -1 }, /* (354) trnm ::= nm */
149443 { 256, 0 }, /* (355) tridxby ::= */
149444 { 257, -1 }, /* (356) database_kw_opt ::= DATABASE */
149445 { 257, 0 }, /* (357) database_kw_opt ::= */
149446 { 260, 0 }, /* (358) kwcolumn_opt ::= */
149447 { 260, -1 }, /* (359) kwcolumn_opt ::= COLUMNKW */
149448 { 262, -1 }, /* (360) vtabarglist ::= vtabarg */
149449 { 262, -3 }, /* (361) vtabarglist ::= vtabarglist COMMA vtabarg */
149450 { 263, -2 }, /* (362) vtabarg ::= vtabarg vtabargtoken */
149451 { 266, 0 }, /* (363) anylist ::= */
149452 { 266, -4 }, /* (364) anylist ::= anylist LP anylist RP */
149453 { 266, -2 }, /* (365) anylist ::= anylist ANY */
149454 { 232, 0 }, /* (366) with ::= */
149455};
149456
149457static void yy_accept(yyParser*); /* Forward Declaration */
149458
149459/*
149460** Perform a reduce action and the shift that must immediately
149461** follow the reduce.
149462**
149463** The yyLookahead and yyLookaheadToken parameters provide reduce actions
149464** access to the lookahead token (if any). The yyLookahead will be YYNOCODE
149465** if the lookahead token has already been consumed. As this procedure is
149466** only called from one place, optimizing compilers will in-line it, which
149467** means that the extra parameters have no performance impact.
149468*/
149469static YYACTIONTYPE yy_reduce(
149470 yyParser *yypParser, /* The parser */
149471 unsigned int yyruleno, /* Number of the rule by which to reduce */
149472 int yyLookahead, /* Lookahead token, or YYNOCODE if none */
149473 sqlite3ParserTOKENTYPE yyLookaheadToken /* Value of the lookahead token */
149474 sqlite3ParserCTX_PDECL /* %extra_context */
149475){
149476 int yygoto; /* The next state */
149477 YYACTIONTYPE yyact; /* The next action */
149478 yyStackEntry *yymsp; /* The top of the parser's stack */
149479 int yysize; /* Amount to pop the stack */
149480 sqlite3ParserARG_FETCH
149481 (void)yyLookahead;
149482 (void)yyLookaheadToken;
149483 yymsp = yypParser->yytos;
149484#ifndef NDEBUG
149485 if( yyTraceFILE && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
149486 yysize = yyRuleInfo[yyruleno].nrhs;
149487 if( yysize ){
149488 fprintf(yyTraceFILE, "%sReduce %d [%s], go to state %d.\n",
149489 yyTracePrompt,
149490 yyruleno, yyRuleName[yyruleno], yymsp[yysize].stateno);
149491 }else{
149492 fprintf(yyTraceFILE, "%sReduce %d [%s].\n",
149493 yyTracePrompt, yyruleno, yyRuleName[yyruleno]);
149494 }
149495 }
149496#endif /* NDEBUG */
149497
149498 /* Check that the stack is large enough to grow by a single entry
149499 ** if the RHS of the rule is empty. This ensures that there is room
149500 ** enough on the stack to push the LHS value */
149501 if( yyRuleInfo[yyruleno].nrhs==0 ){
149502#ifdef YYTRACKMAXSTACKDEPTH
149503 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
149504 yypParser->yyhwm++;
149505 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack));
149506 }
149507#endif
149508#if YYSTACKDEPTH>0
149509 if( yypParser->yytos>=yypParser->yystackEnd ){
149510 yyStackOverflow(yypParser);
149511 /* The call to yyStackOverflow() above pops the stack until it is
149512 ** empty, causing the main parser loop to exit. So the return value
149513 ** is never used and does not matter. */
149514 return 0;
149515 }
149516#else
149517 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz-1] ){
149518 if( yyGrowStack(yypParser) ){
149519 yyStackOverflow(yypParser);
149520 /* The call to yyStackOverflow() above pops the stack until it is
149521 ** empty, causing the main parser loop to exit. So the return value
149522 ** is never used and does not matter. */
149523 return 0;
149524 }
149525 yymsp = yypParser->yytos;
149526 }
149527#endif
149528 }
149529
149530 switch( yyruleno ){
149531 /* Beginning here are the reduction cases. A typical example
149532 ** follows:
149533 ** case 0:
149534 ** #line <lineno> <grammarfile>
149535 ** { ... } // User supplied code
149536 ** #line <lineno> <thisfile>
149537 ** break;
149538 */
149539/********** Begin reduce actions **********************************************/
149540 YYMINORTYPE yylhsminor;
149541 case 0: /* explain ::= EXPLAIN */
149542{ pParse->explain = 1; }
149543 break;
149544 case 1: /* explain ::= EXPLAIN QUERY PLAN */
149545{ pParse->explain = 2; }
149546 break;
149547 case 2: /* cmdx ::= cmd */
149548{ sqlite3FinishCoding(pParse); }
149549 break;
149550 case 3: /* cmd ::= BEGIN transtype trans_opt */
149551{sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy70);}
149552 break;
149553 case 4: /* transtype ::= */
149554{yymsp[1].minor.yy70 = TK_DEFERRED;}
149555 break;
149556 case 5: /* transtype ::= DEFERRED */
149557 case 6: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==6);
149558 case 7: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==7);
149559{yymsp[0].minor.yy70 = yymsp[0].major; /*A-overwrites-X*/}
149560 break;
149561 case 8: /* cmd ::= COMMIT|END trans_opt */
149562 case 9: /* cmd ::= ROLLBACK trans_opt */ yytestcase(yyruleno==9);
149563{sqlite3EndTransaction(pParse,yymsp[-1].major);}
149564 break;
149565 case 10: /* cmd ::= SAVEPOINT nm */
149566{
149567 sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &yymsp[0].minor.yy0);
149568}
149569 break;
149570 case 11: /* cmd ::= RELEASE savepoint_opt nm */
149571{
149572 sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &yymsp[0].minor.yy0);
149573}
149574 break;
149575 case 12: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
149576{
149577 sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &yymsp[0].minor.yy0);
149578}
149579 break;
149580 case 13: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */
149581{
149582 sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy70,0,0,yymsp[-2].minor.yy70);
149583}
149584 break;
149585 case 14: /* createkw ::= CREATE */
149586{disableLookaside(pParse);}
149587 break;
149588 case 15: /* ifnotexists ::= */
149589 case 18: /* temp ::= */ yytestcase(yyruleno==18);
149590 case 21: /* table_options ::= */ yytestcase(yyruleno==21);
149591 case 42: /* autoinc ::= */ yytestcase(yyruleno==42);
149592 case 57: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==57);
149593 case 67: /* defer_subclause_opt ::= */ yytestcase(yyruleno==67);
149594 case 76: /* ifexists ::= */ yytestcase(yyruleno==76);
149595 case 93: /* distinct ::= */ yytestcase(yyruleno==93);
149596 case 226: /* collate ::= */ yytestcase(yyruleno==226);
149597{yymsp[1].minor.yy70 = 0;}
149598 break;
149599 case 16: /* ifnotexists ::= IF NOT EXISTS */
149600{yymsp[-2].minor.yy70 = 1;}
149601 break;
149602 case 17: /* temp ::= TEMP */
149603 case 43: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==43);
149604{yymsp[0].minor.yy70 = 1;}
149605 break;
149606 case 19: /* create_table_args ::= LP columnlist conslist_opt RP table_options */
149607{
149608 sqlite3EndTable(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,yymsp[0].minor.yy70,0);
149609}
149610 break;
149611 case 20: /* create_table_args ::= AS select */
149612{
149613 sqlite3EndTable(pParse,0,0,0,yymsp[0].minor.yy489);
149614 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy489);
149615}
149616 break;
149617 case 22: /* table_options ::= WITHOUT nm */
149618{
149619 if( yymsp[0].minor.yy0.n==5 && sqlite3_strnicmp(yymsp[0].minor.yy0.z,"rowid",5)==0 ){
149620 yymsp[-1].minor.yy70 = TF_WithoutRowid | TF_NoVisibleRowid;
149621 }else{
149622 yymsp[-1].minor.yy70 = 0;
149623 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.z);
149624 }
149625}
149626 break;
149627 case 23: /* columnname ::= nm typetoken */
149628{sqlite3AddColumn(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);}
149629 break;
149630 case 24: /* typetoken ::= */
149631 case 60: /* conslist_opt ::= */ yytestcase(yyruleno==60);
149632 case 99: /* as ::= */ yytestcase(yyruleno==99);
149633{yymsp[1].minor.yy0.n = 0; yymsp[1].minor.yy0.z = 0;}
149634 break;
149635 case 25: /* typetoken ::= typename LP signed RP */
149636{
149637 yymsp[-3].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
149638}
149639 break;
149640 case 26: /* typetoken ::= typename LP signed COMMA signed RP */
149641{
149642 yymsp[-5].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
149643}
149644 break;
149645 case 27: /* typename ::= typename ID|STRING */
149646{yymsp[-1].minor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
149647 break;
149648 case 28: /* scanpt ::= */
149649{
149650 assert( yyLookahead!=YYNOCODE );
149651 yymsp[1].minor.yy392 = yyLookaheadToken.z;
149652}
149653 break;
149654 case 29: /* ccons ::= CONSTRAINT nm */
149655 case 62: /* tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==62);
149656{pParse->constraintName = yymsp[0].minor.yy0;}
149657 break;
149658 case 30: /* ccons ::= DEFAULT scanpt term scanpt */
149659{sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy18,yymsp[-2].minor.yy392,yymsp[0].minor.yy392);}
149660 break;
149661 case 31: /* ccons ::= DEFAULT LP expr RP */
149662{sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy18,yymsp[-2].minor.yy0.z+1,yymsp[0].minor.yy0.z);}
149663 break;
149664 case 32: /* ccons ::= DEFAULT PLUS term scanpt */
149665{sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy18,yymsp[-2].minor.yy0.z,yymsp[0].minor.yy392);}
149666 break;
149667 case 33: /* ccons ::= DEFAULT MINUS term scanpt */
149668{
149669 Expr *p = sqlite3PExpr(pParse, TK_UMINUS, yymsp[-1].minor.yy18, 0);
149670 sqlite3AddDefaultValue(pParse,p,yymsp[-2].minor.yy0.z,yymsp[0].minor.yy392);
149671}
149672 break;
149673 case 34: /* ccons ::= DEFAULT scanpt ID|INDEXED */
149674{
149675 Expr *p = tokenExpr(pParse, TK_STRING, yymsp[0].minor.yy0);
149676 if( p ){
149677 sqlite3ExprIdToTrueFalse(p);
149678 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
149679 }
149680 sqlite3AddDefaultValue(pParse,p,yymsp[0].minor.yy0.z,yymsp[0].minor.yy0.z+yymsp[0].minor.yy0.n);
149681}
149682 break;
149683 case 35: /* ccons ::= NOT NULL onconf */
149684{sqlite3AddNotNull(pParse, yymsp[0].minor.yy70);}
149685 break;
149686 case 36: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
149687{sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy70,yymsp[0].minor.yy70,yymsp[-2].minor.yy70);}
149688 break;
149689 case 37: /* ccons ::= UNIQUE onconf */
149690{sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy70,0,0,0,0,
149691 SQLITE_IDXTYPE_UNIQUE);}
149692 break;
149693 case 38: /* ccons ::= CHECK LP expr RP */
149694{sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy18);}
149695 break;
149696 case 39: /* ccons ::= REFERENCES nm eidlist_opt refargs */
149697{sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy420,yymsp[0].minor.yy70);}
149698 break;
149699 case 40: /* ccons ::= defer_subclause */
149700{sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy70);}
149701 break;
149702 case 41: /* ccons ::= COLLATE ID|STRING */
149703{sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);}
149704 break;
149705 case 44: /* refargs ::= */
149706{ yymsp[1].minor.yy70 = OE_None*0x0101; /* EV: R-19803-45884 */}
149707 break;
149708 case 45: /* refargs ::= refargs refarg */
149709{ yymsp[-1].minor.yy70 = (yymsp[-1].minor.yy70 & ~yymsp[0].minor.yy111.mask) | yymsp[0].minor.yy111.value; }
149710 break;
149711 case 46: /* refarg ::= MATCH nm */
149712{ yymsp[-1].minor.yy111.value = 0; yymsp[-1].minor.yy111.mask = 0x000000; }
149713 break;
149714 case 47: /* refarg ::= ON INSERT refact */
149715{ yymsp[-2].minor.yy111.value = 0; yymsp[-2].minor.yy111.mask = 0x000000; }
149716 break;
149717 case 48: /* refarg ::= ON DELETE refact */
149718{ yymsp[-2].minor.yy111.value = yymsp[0].minor.yy70; yymsp[-2].minor.yy111.mask = 0x0000ff; }
149719 break;
149720 case 49: /* refarg ::= ON UPDATE refact */
149721{ yymsp[-2].minor.yy111.value = yymsp[0].minor.yy70<<8; yymsp[-2].minor.yy111.mask = 0x00ff00; }
149722 break;
149723 case 50: /* refact ::= SET NULL */
149724{ yymsp[-1].minor.yy70 = OE_SetNull; /* EV: R-33326-45252 */}
149725 break;
149726 case 51: /* refact ::= SET DEFAULT */
149727{ yymsp[-1].minor.yy70 = OE_SetDflt; /* EV: R-33326-45252 */}
149728 break;
149729 case 52: /* refact ::= CASCADE */
149730{ yymsp[0].minor.yy70 = OE_Cascade; /* EV: R-33326-45252 */}
149731 break;
149732 case 53: /* refact ::= RESTRICT */
149733{ yymsp[0].minor.yy70 = OE_Restrict; /* EV: R-33326-45252 */}
149734 break;
149735 case 54: /* refact ::= NO ACTION */
149736{ yymsp[-1].minor.yy70 = OE_None; /* EV: R-33326-45252 */}
149737 break;
149738 case 55: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
149739{yymsp[-2].minor.yy70 = 0;}
149740 break;
149741 case 56: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
149742 case 71: /* orconf ::= OR resolvetype */ yytestcase(yyruleno==71);
149743 case 156: /* insert_cmd ::= INSERT orconf */ yytestcase(yyruleno==156);
149744{yymsp[-1].minor.yy70 = yymsp[0].minor.yy70;}
149745 break;
149746 case 58: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */
149747 case 75: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==75);
149748 case 198: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==198);
149749 case 201: /* in_op ::= NOT IN */ yytestcase(yyruleno==201);
149750 case 227: /* collate ::= COLLATE ID|STRING */ yytestcase(yyruleno==227);
149751{yymsp[-1].minor.yy70 = 1;}
149752 break;
149753 case 59: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
149754{yymsp[-1].minor.yy70 = 0;}
149755 break;
149756 case 61: /* tconscomma ::= COMMA */
149757{pParse->constraintName.n = 0;}
149758 break;
149759 case 63: /* tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
149760{sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy420,yymsp[0].minor.yy70,yymsp[-2].minor.yy70,0);}
149761 break;
149762 case 64: /* tcons ::= UNIQUE LP sortlist RP onconf */
149763{sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy420,yymsp[0].minor.yy70,0,0,0,0,
149764 SQLITE_IDXTYPE_UNIQUE);}
149765 break;
149766 case 65: /* tcons ::= CHECK LP expr RP onconf */
149767{sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy18);}
149768 break;
149769 case 66: /* tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
149770{
149771 sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy420, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy420, yymsp[-1].minor.yy70);
149772 sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy70);
149773}
149774 break;
149775 case 68: /* onconf ::= */
149776 case 70: /* orconf ::= */ yytestcase(yyruleno==70);
149777{yymsp[1].minor.yy70 = OE_Default;}
149778 break;
149779 case 69: /* onconf ::= ON CONFLICT resolvetype */
149780{yymsp[-2].minor.yy70 = yymsp[0].minor.yy70;}
149781 break;
149782 case 72: /* resolvetype ::= IGNORE */
149783{yymsp[0].minor.yy70 = OE_Ignore;}
149784 break;
149785 case 73: /* resolvetype ::= REPLACE */
149786 case 157: /* insert_cmd ::= REPLACE */ yytestcase(yyruleno==157);
149787{yymsp[0].minor.yy70 = OE_Replace;}
149788 break;
149789 case 74: /* cmd ::= DROP TABLE ifexists fullname */
149790{
149791 sqlite3DropTable(pParse, yymsp[0].minor.yy135, 0, yymsp[-1].minor.yy70);
149792}
149793 break;
149794 case 77: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
149795{
149796 sqlite3CreateView(pParse, &yymsp[-8].minor.yy0, &yymsp[-4].minor.yy0, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy420, yymsp[0].minor.yy489, yymsp[-7].minor.yy70, yymsp[-5].minor.yy70);
149797}
149798 break;
149799 case 78: /* cmd ::= DROP VIEW ifexists fullname */
149800{
149801 sqlite3DropTable(pParse, yymsp[0].minor.yy135, 1, yymsp[-1].minor.yy70);
149802}
149803 break;
149804 case 79: /* cmd ::= select */
149805{
149806 SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0};
149807 sqlite3Select(pParse, yymsp[0].minor.yy489, &dest);
149808 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy489);
149809}
149810 break;
149811 case 80: /* select ::= WITH wqlist selectnowith */
149812{
149813 Select *p = yymsp[0].minor.yy489;
149814 if( p ){
149815 p->pWith = yymsp[-1].minor.yy449;
149816 parserDoubleLinkSelect(pParse, p);
149817 }else{
149818 sqlite3WithDelete(pParse->db, yymsp[-1].minor.yy449);
149819 }
149820 yymsp[-2].minor.yy489 = p;
149821}
149822 break;
149823 case 81: /* select ::= WITH RECURSIVE wqlist selectnowith */
149824{
149825 Select *p = yymsp[0].minor.yy489;
149826 if( p ){
149827 p->pWith = yymsp[-1].minor.yy449;
149828 parserDoubleLinkSelect(pParse, p);
149829 }else{
149830 sqlite3WithDelete(pParse->db, yymsp[-1].minor.yy449);
149831 }
149832 yymsp[-3].minor.yy489 = p;
149833}
149834 break;
149835 case 82: /* select ::= selectnowith */
149836{
149837 Select *p = yymsp[0].minor.yy489;
149838 if( p ){
149839 parserDoubleLinkSelect(pParse, p);
149840 }
149841 yymsp[0].minor.yy489 = p; /*A-overwrites-X*/
149842}
149843 break;
149844 case 83: /* selectnowith ::= selectnowith multiselect_op oneselect */
149845{
149846 Select *pRhs = yymsp[0].minor.yy489;
149847 Select *pLhs = yymsp[-2].minor.yy489;
149848 if( pRhs && pRhs->pPrior ){
149849 SrcList *pFrom;
149850 Token x;
149851 x.n = 0;
149852 parserDoubleLinkSelect(pParse, pRhs);
149853 pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
149854 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
149855 }
149856 if( pRhs ){
149857 pRhs->op = (u8)yymsp[-1].minor.yy70;
149858 pRhs->pPrior = pLhs;
149859 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
149860 pRhs->selFlags &= ~SF_MultiValue;
149861 if( yymsp[-1].minor.yy70!=TK_ALL ) pParse->hasCompound = 1;
149862 }else{
149863 sqlite3SelectDelete(pParse->db, pLhs);
149864 }
149865 yymsp[-2].minor.yy489 = pRhs;
149866}
149867 break;
149868 case 84: /* multiselect_op ::= UNION */
149869 case 86: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==86);
149870{yymsp[0].minor.yy70 = yymsp[0].major; /*A-overwrites-OP*/}
149871 break;
149872 case 85: /* multiselect_op ::= UNION ALL */
149873{yymsp[-1].minor.yy70 = TK_ALL;}
149874 break;
149875 case 87: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
149876{
149877 yymsp[-8].minor.yy489 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy420,yymsp[-5].minor.yy135,yymsp[-4].minor.yy18,yymsp[-3].minor.yy420,yymsp[-2].minor.yy18,yymsp[-1].minor.yy420,yymsp[-7].minor.yy70,yymsp[0].minor.yy18);
149878}
149879 break;
149880 case 88: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
149881{
149882 yymsp[-9].minor.yy489 = sqlite3SelectNew(pParse,yymsp[-7].minor.yy420,yymsp[-6].minor.yy135,yymsp[-5].minor.yy18,yymsp[-4].minor.yy420,yymsp[-3].minor.yy18,yymsp[-1].minor.yy420,yymsp[-8].minor.yy70,yymsp[0].minor.yy18);
149883 if( yymsp[-9].minor.yy489 ){
149884 yymsp[-9].minor.yy489->pWinDefn = yymsp[-2].minor.yy327;
149885 }else{
149886 sqlite3WindowListDelete(pParse->db, yymsp[-2].minor.yy327);
149887 }
149888}
149889 break;
149890 case 89: /* values ::= VALUES LP nexprlist RP */
149891{
149892 yymsp[-3].minor.yy489 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy420,0,0,0,0,0,SF_Values,0);
149893}
149894 break;
149895 case 90: /* values ::= values COMMA LP nexprlist RP */
149896{
149897 Select *pRight, *pLeft = yymsp[-4].minor.yy489;
149898 pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy420,0,0,0,0,0,SF_Values|SF_MultiValue,0);
149899 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
149900 if( pRight ){
149901 pRight->op = TK_ALL;
149902 pRight->pPrior = pLeft;
149903 yymsp[-4].minor.yy489 = pRight;
149904 }else{
149905 yymsp[-4].minor.yy489 = pLeft;
149906 }
149907}
149908 break;
149909 case 91: /* distinct ::= DISTINCT */
149910{yymsp[0].minor.yy70 = SF_Distinct;}
149911 break;
149912 case 92: /* distinct ::= ALL */
149913{yymsp[0].minor.yy70 = SF_All;}
149914 break;
149915 case 94: /* sclp ::= */
149916 case 127: /* orderby_opt ::= */ yytestcase(yyruleno==127);
149917 case 134: /* groupby_opt ::= */ yytestcase(yyruleno==134);
149918 case 214: /* exprlist ::= */ yytestcase(yyruleno==214);
149919 case 217: /* paren_exprlist ::= */ yytestcase(yyruleno==217);
149920 case 222: /* eidlist_opt ::= */ yytestcase(yyruleno==222);
149921{yymsp[1].minor.yy420 = 0;}
149922 break;
149923 case 95: /* selcollist ::= sclp scanpt expr scanpt as */
149924{
149925 yymsp[-4].minor.yy420 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy420, yymsp[-2].minor.yy18);
149926 if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy420, &yymsp[0].minor.yy0, 1);
149927 sqlite3ExprListSetSpan(pParse,yymsp[-4].minor.yy420,yymsp[-3].minor.yy392,yymsp[-1].minor.yy392);
149928}
149929 break;
149930 case 96: /* selcollist ::= sclp scanpt STAR */
149931{
149932 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
149933 yymsp[-2].minor.yy420 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy420, p);
149934}
149935 break;
149936 case 97: /* selcollist ::= sclp scanpt nm DOT STAR */
149937{
149938 Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0);
149939 Expr *pLeft = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1);
149940 Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
149941 yymsp[-4].minor.yy420 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy420, pDot);
149942}
149943 break;
149944 case 98: /* as ::= AS nm */
149945 case 109: /* dbnm ::= DOT nm */ yytestcase(yyruleno==109);
149946 case 236: /* plus_num ::= PLUS INTEGER|FLOAT */ yytestcase(yyruleno==236);
149947 case 237: /* minus_num ::= MINUS INTEGER|FLOAT */ yytestcase(yyruleno==237);
149948{yymsp[-1].minor.yy0 = yymsp[0].minor.yy0;}
149949 break;
149950 case 100: /* from ::= */
149951{yymsp[1].minor.yy135 = sqlite3DbMallocZero(pParse->db, sizeof(*yymsp[1].minor.yy135));}
149952 break;
149953 case 101: /* from ::= FROM seltablist */
149954{
149955 yymsp[-1].minor.yy135 = yymsp[0].minor.yy135;
149956 sqlite3SrcListShiftJoinType(yymsp[-1].minor.yy135);
149957}
149958 break;
149959 case 102: /* stl_prefix ::= seltablist joinop */
149960{
149961 if( ALWAYS(yymsp[-1].minor.yy135 && yymsp[-1].minor.yy135->nSrc>0) ) yymsp[-1].minor.yy135->a[yymsp[-1].minor.yy135->nSrc-1].fg.jointype = (u8)yymsp[0].minor.yy70;
149962}
149963 break;
149964 case 103: /* stl_prefix ::= */
149965{yymsp[1].minor.yy135 = 0;}
149966 break;
149967 case 104: /* seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
149968{
149969 yymsp[-6].minor.yy135 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy135,&yymsp[-5].minor.yy0,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,0,yymsp[-1].minor.yy18,yymsp[0].minor.yy48);
149970 sqlite3SrcListIndexedBy(pParse, yymsp[-6].minor.yy135, &yymsp[-2].minor.yy0);
149971}
149972 break;
149973 case 105: /* seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt */
149974{
149975 yymsp[-8].minor.yy135 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-8].minor.yy135,&yymsp[-7].minor.yy0,&yymsp[-6].minor.yy0,&yymsp[-2].minor.yy0,0,yymsp[-1].minor.yy18,yymsp[0].minor.yy48);
149976 sqlite3SrcListFuncArgs(pParse, yymsp[-8].minor.yy135, yymsp[-4].minor.yy420);
149977}
149978 break;
149979 case 106: /* seltablist ::= stl_prefix LP select RP as on_opt using_opt */
149980{
149981 yymsp[-6].minor.yy135 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy135,0,0,&yymsp[-2].minor.yy0,yymsp[-4].minor.yy489,yymsp[-1].minor.yy18,yymsp[0].minor.yy48);
149982 }
149983 break;
149984 case 107: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
149985{
149986 if( yymsp[-6].minor.yy135==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy18==0 && yymsp[0].minor.yy48==0 ){
149987 yymsp[-6].minor.yy135 = yymsp[-4].minor.yy135;
149988 }else if( yymsp[-4].minor.yy135->nSrc==1 ){
149989 yymsp[-6].minor.yy135 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy135,0,0,&yymsp[-2].minor.yy0,0,yymsp[-1].minor.yy18,yymsp[0].minor.yy48);
149990 if( yymsp[-6].minor.yy135 ){
149991 struct SrcList_item *pNew = &yymsp[-6].minor.yy135->a[yymsp[-6].minor.yy135->nSrc-1];
149992 struct SrcList_item *pOld = yymsp[-4].minor.yy135->a;
149993 pNew->zName = pOld->zName;
149994 pNew->zDatabase = pOld->zDatabase;
149995 pNew->pSelect = pOld->pSelect;
149996 pOld->zName = pOld->zDatabase = 0;
149997 pOld->pSelect = 0;
149998 }
149999 sqlite3SrcListDelete(pParse->db, yymsp[-4].minor.yy135);
150000 }else{
150001 Select *pSubquery;
150002 sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy135);
150003 pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy135,0,0,0,0,SF_NestedFrom,0);
150004 yymsp[-6].minor.yy135 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy135,0,0,&yymsp[-2].minor.yy0,pSubquery,yymsp[-1].minor.yy18,yymsp[0].minor.yy48);
150005 }
150006 }
150007 break;
150008 case 108: /* dbnm ::= */
150009 case 122: /* indexed_opt ::= */ yytestcase(yyruleno==122);
150010{yymsp[1].minor.yy0.z=0; yymsp[1].minor.yy0.n=0;}
150011 break;
150012 case 110: /* fullname ::= nm */
150013{
150014 yylhsminor.yy135 = sqlite3SrcListAppend(pParse->db,0,&yymsp[0].minor.yy0,0);
150015 if( IN_RENAME_OBJECT && yylhsminor.yy135 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy135->a[0].zName, &yymsp[0].minor.yy0);
150016}
150017 yymsp[0].minor.yy135 = yylhsminor.yy135;
150018 break;
150019 case 111: /* fullname ::= nm DOT nm */
150020{
150021 yylhsminor.yy135 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
150022 if( IN_RENAME_OBJECT && yylhsminor.yy135 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy135->a[0].zName, &yymsp[0].minor.yy0);
150023}
150024 yymsp[-2].minor.yy135 = yylhsminor.yy135;
150025 break;
150026 case 112: /* xfullname ::= nm */
150027{yymsp[0].minor.yy135 = sqlite3SrcListAppend(pParse->db,0,&yymsp[0].minor.yy0,0); /*A-overwrites-X*/}
150028 break;
150029 case 113: /* xfullname ::= nm DOT nm */
150030{yymsp[-2].minor.yy135 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-X*/}
150031 break;
150032 case 114: /* xfullname ::= nm DOT nm AS nm */
150033{
150034 yymsp[-4].minor.yy135 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-4].minor.yy0,&yymsp[-2].minor.yy0); /*A-overwrites-X*/
150035 if( yymsp[-4].minor.yy135 ) yymsp[-4].minor.yy135->a[0].zAlias = sqlite3NameFromToken(pParse->db, &yymsp[0].minor.yy0);
150036}
150037 break;
150038 case 115: /* xfullname ::= nm AS nm */
150039{
150040 yymsp[-2].minor.yy135 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-2].minor.yy0,0); /*A-overwrites-X*/
150041 if( yymsp[-2].minor.yy135 ) yymsp[-2].minor.yy135->a[0].zAlias = sqlite3NameFromToken(pParse->db, &yymsp[0].minor.yy0);
150042}
150043 break;
150044 case 116: /* joinop ::= COMMA|JOIN */
150045{ yymsp[0].minor.yy70 = JT_INNER; }
150046 break;
150047 case 117: /* joinop ::= JOIN_KW JOIN */
150048{yymsp[-1].minor.yy70 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); /*X-overwrites-A*/}
150049 break;
150050 case 118: /* joinop ::= JOIN_KW nm JOIN */
150051{yymsp[-2].minor.yy70 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); /*X-overwrites-A*/}
150052 break;
150053 case 119: /* joinop ::= JOIN_KW nm nm JOIN */
150054{yymsp[-3].minor.yy70 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);/*X-overwrites-A*/}
150055 break;
150056 case 120: /* on_opt ::= ON expr */
150057 case 137: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==137);
150058 case 144: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==144);
150059 case 210: /* case_else ::= ELSE expr */ yytestcase(yyruleno==210);
150060{yymsp[-1].minor.yy18 = yymsp[0].minor.yy18;}
150061 break;
150062 case 121: /* on_opt ::= */
150063 case 136: /* having_opt ::= */ yytestcase(yyruleno==136);
150064 case 138: /* limit_opt ::= */ yytestcase(yyruleno==138);
150065 case 143: /* where_opt ::= */ yytestcase(yyruleno==143);
150066 case 211: /* case_else ::= */ yytestcase(yyruleno==211);
150067 case 213: /* case_operand ::= */ yytestcase(yyruleno==213);
150068{yymsp[1].minor.yy18 = 0;}
150069 break;
150070 case 123: /* indexed_opt ::= INDEXED BY nm */
150071{yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;}
150072 break;
150073 case 124: /* indexed_opt ::= NOT INDEXED */
150074{yymsp[-1].minor.yy0.z=0; yymsp[-1].minor.yy0.n=1;}
150075 break;
150076 case 125: /* using_opt ::= USING LP idlist RP */
150077{yymsp[-3].minor.yy48 = yymsp[-1].minor.yy48;}
150078 break;
150079 case 126: /* using_opt ::= */
150080 case 158: /* idlist_opt ::= */ yytestcase(yyruleno==158);
150081{yymsp[1].minor.yy48 = 0;}
150082 break;
150083 case 128: /* orderby_opt ::= ORDER BY sortlist */
150084 case 135: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==135);
150085{yymsp[-2].minor.yy420 = yymsp[0].minor.yy420;}
150086 break;
150087 case 129: /* sortlist ::= sortlist COMMA expr sortorder */
150088{
150089 yymsp[-3].minor.yy420 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy420,yymsp[-1].minor.yy18);
150090 sqlite3ExprListSetSortOrder(yymsp[-3].minor.yy420,yymsp[0].minor.yy70);
150091}
150092 break;
150093 case 130: /* sortlist ::= expr sortorder */
150094{
150095 yymsp[-1].minor.yy420 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy18); /*A-overwrites-Y*/
150096 sqlite3ExprListSetSortOrder(yymsp[-1].minor.yy420,yymsp[0].minor.yy70);
150097}
150098 break;
150099 case 131: /* sortorder ::= ASC */
150100{yymsp[0].minor.yy70 = SQLITE_SO_ASC;}
150101 break;
150102 case 132: /* sortorder ::= DESC */
150103{yymsp[0].minor.yy70 = SQLITE_SO_DESC;}
150104 break;
150105 case 133: /* sortorder ::= */
150106{yymsp[1].minor.yy70 = SQLITE_SO_UNDEFINED;}
150107 break;
150108 case 139: /* limit_opt ::= LIMIT expr */
150109{yymsp[-1].minor.yy18 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[0].minor.yy18,0);}
150110 break;
150111 case 140: /* limit_opt ::= LIMIT expr OFFSET expr */
150112{yymsp[-3].minor.yy18 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[-2].minor.yy18,yymsp[0].minor.yy18);}
150113 break;
150114 case 141: /* limit_opt ::= LIMIT expr COMMA expr */
150115{yymsp[-3].minor.yy18 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[0].minor.yy18,yymsp[-2].minor.yy18);}
150116 break;
150117 case 142: /* cmd ::= with DELETE FROM xfullname indexed_opt where_opt */
150118{
150119 sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy135, &yymsp[-1].minor.yy0);
150120 sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy135,yymsp[0].minor.yy18,0,0);
150121}
150122 break;
150123 case 145: /* cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist where_opt */
150124{
150125 sqlite3SrcListIndexedBy(pParse, yymsp[-4].minor.yy135, &yymsp[-3].minor.yy0);
150126 sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy420,"set list");
150127 sqlite3Update(pParse,yymsp[-4].minor.yy135,yymsp[-1].minor.yy420,yymsp[0].minor.yy18,yymsp[-5].minor.yy70,0,0,0);
150128}
150129 break;
150130 case 146: /* setlist ::= setlist COMMA nm EQ expr */
150131{
150132 yymsp[-4].minor.yy420 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy420, yymsp[0].minor.yy18);
150133 sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy420, &yymsp[-2].minor.yy0, 1);
150134}
150135 break;
150136 case 147: /* setlist ::= setlist COMMA LP idlist RP EQ expr */
150137{
150138 yymsp[-6].minor.yy420 = sqlite3ExprListAppendVector(pParse, yymsp[-6].minor.yy420, yymsp[-3].minor.yy48, yymsp[0].minor.yy18);
150139}
150140 break;
150141 case 148: /* setlist ::= nm EQ expr */
150142{
150143 yylhsminor.yy420 = sqlite3ExprListAppend(pParse, 0, yymsp[0].minor.yy18);
150144 sqlite3ExprListSetName(pParse, yylhsminor.yy420, &yymsp[-2].minor.yy0, 1);
150145}
150146 yymsp[-2].minor.yy420 = yylhsminor.yy420;
150147 break;
150148 case 149: /* setlist ::= LP idlist RP EQ expr */
150149{
150150 yymsp[-4].minor.yy420 = sqlite3ExprListAppendVector(pParse, 0, yymsp[-3].minor.yy48, yymsp[0].minor.yy18);
150151}
150152 break;
150153 case 150: /* cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
150154{
150155 sqlite3Insert(pParse, yymsp[-3].minor.yy135, yymsp[-1].minor.yy489, yymsp[-2].minor.yy48, yymsp[-5].minor.yy70, yymsp[0].minor.yy340);
150156}
150157 break;
150158 case 151: /* cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES */
150159{
150160 sqlite3Insert(pParse, yymsp[-3].minor.yy135, 0, yymsp[-2].minor.yy48, yymsp[-5].minor.yy70, 0);
150161}
150162 break;
150163 case 152: /* upsert ::= */
150164{ yymsp[1].minor.yy340 = 0; }
150165 break;
150166 case 153: /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt */
150167{ yymsp[-10].minor.yy340 = sqlite3UpsertNew(pParse->db,yymsp[-7].minor.yy420,yymsp[-5].minor.yy18,yymsp[-1].minor.yy420,yymsp[0].minor.yy18);}
150168 break;
150169 case 154: /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING */
150170{ yymsp[-7].minor.yy340 = sqlite3UpsertNew(pParse->db,yymsp[-4].minor.yy420,yymsp[-2].minor.yy18,0,0); }
150171 break;
150172 case 155: /* upsert ::= ON CONFLICT DO NOTHING */
150173{ yymsp[-3].minor.yy340 = sqlite3UpsertNew(pParse->db,0,0,0,0); }
150174 break;
150175 case 159: /* idlist_opt ::= LP idlist RP */
150176{yymsp[-2].minor.yy48 = yymsp[-1].minor.yy48;}
150177 break;
150178 case 160: /* idlist ::= idlist COMMA nm */
150179{yymsp[-2].minor.yy48 = sqlite3IdListAppend(pParse,yymsp[-2].minor.yy48,&yymsp[0].minor.yy0);}
150180 break;
150181 case 161: /* idlist ::= nm */
150182{yymsp[0].minor.yy48 = sqlite3IdListAppend(pParse,0,&yymsp[0].minor.yy0); /*A-overwrites-Y*/}
150183 break;
150184 case 162: /* expr ::= LP expr RP */
150185{yymsp[-2].minor.yy18 = yymsp[-1].minor.yy18;}
150186 break;
150187 case 163: /* expr ::= ID|INDEXED */
150188 case 164: /* expr ::= JOIN_KW */ yytestcase(yyruleno==164);
150189{yymsp[0].minor.yy18=tokenExpr(pParse,TK_ID,yymsp[0].minor.yy0); /*A-overwrites-X*/}
150190 break;
150191 case 165: /* expr ::= nm DOT nm */
150192{
150193 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1);
150194 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[0].minor.yy0, 1);
150195 if( IN_RENAME_OBJECT ){
150196 sqlite3RenameTokenMap(pParse, (void*)temp2, &yymsp[0].minor.yy0);
150197 sqlite3RenameTokenMap(pParse, (void*)temp1, &yymsp[-2].minor.yy0);
150198 }
150199 yylhsminor.yy18 = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
150200}
150201 yymsp[-2].minor.yy18 = yylhsminor.yy18;
150202 break;
150203 case 166: /* expr ::= nm DOT nm DOT nm */
150204{
150205 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-4].minor.yy0, 1);
150206 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1);
150207 Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[0].minor.yy0, 1);
150208 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3);
150209 if( IN_RENAME_OBJECT ){
150210 sqlite3RenameTokenMap(pParse, (void*)temp3, &yymsp[0].minor.yy0);
150211 sqlite3RenameTokenMap(pParse, (void*)temp2, &yymsp[-2].minor.yy0);
150212 }
150213 yylhsminor.yy18 = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
150214}
150215 yymsp[-4].minor.yy18 = yylhsminor.yy18;
150216 break;
150217 case 167: /* term ::= NULL|FLOAT|BLOB */
150218 case 168: /* term ::= STRING */ yytestcase(yyruleno==168);
150219{yymsp[0].minor.yy18=tokenExpr(pParse,yymsp[0].major,yymsp[0].minor.yy0); /*A-overwrites-X*/}
150220 break;
150221 case 169: /* term ::= INTEGER */
150222{
150223 yylhsminor.yy18 = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &yymsp[0].minor.yy0, 1);
150224}
150225 yymsp[0].minor.yy18 = yylhsminor.yy18;
150226 break;
150227 case 170: /* expr ::= VARIABLE */
150228{
150229 if( !(yymsp[0].minor.yy0.z[0]=='#' && sqlite3Isdigit(yymsp[0].minor.yy0.z[1])) ){
150230 u32 n = yymsp[0].minor.yy0.n;
150231 yymsp[0].minor.yy18 = tokenExpr(pParse, TK_VARIABLE, yymsp[0].minor.yy0);
150232 sqlite3ExprAssignVarNumber(pParse, yymsp[0].minor.yy18, n);
150233 }else{
150234 /* When doing a nested parse, one can include terms in an expression
150235 ** that look like this: #1 #2 ... These terms refer to registers
150236 ** in the virtual machine. #N is the N-th register. */
150237 Token t = yymsp[0].minor.yy0; /*A-overwrites-X*/
150238 assert( t.n>=2 );
150239 if( pParse->nested==0 ){
150240 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
150241 yymsp[0].minor.yy18 = 0;
150242 }else{
150243 yymsp[0].minor.yy18 = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
150244 if( yymsp[0].minor.yy18 ) sqlite3GetInt32(&t.z[1], &yymsp[0].minor.yy18->iTable);
150245 }
150246 }
150247}
150248 break;
150249 case 171: /* expr ::= expr COLLATE ID|STRING */
150250{
150251 yymsp[-2].minor.yy18 = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy18, &yymsp[0].minor.yy0, 1);
150252}
150253 break;
150254 case 172: /* expr ::= CAST LP expr AS typetoken RP */
150255{
150256 yymsp[-5].minor.yy18 = sqlite3ExprAlloc(pParse->db, TK_CAST, &yymsp[-1].minor.yy0, 1);
150257 sqlite3ExprAttachSubtrees(pParse->db, yymsp[-5].minor.yy18, yymsp[-3].minor.yy18, 0);
150258}
150259 break;
150260 case 173: /* expr ::= ID|INDEXED LP distinct exprlist RP */
150261{
150262 yylhsminor.yy18 = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy420, &yymsp[-4].minor.yy0, yymsp[-2].minor.yy70);
150263}
150264 yymsp[-4].minor.yy18 = yylhsminor.yy18;
150265 break;
150266 case 174: /* expr ::= ID|INDEXED LP STAR RP */
150267{
150268 yylhsminor.yy18 = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0, 0);
150269}
150270 yymsp[-3].minor.yy18 = yylhsminor.yy18;
150271 break;
150272 case 175: /* expr ::= ID|INDEXED LP distinct exprlist RP over_clause */
150273{
150274 yylhsminor.yy18 = sqlite3ExprFunction(pParse, yymsp[-2].minor.yy420, &yymsp[-5].minor.yy0, yymsp[-3].minor.yy70);
150275 sqlite3WindowAttach(pParse, yylhsminor.yy18, yymsp[0].minor.yy327);
150276}
150277 yymsp[-5].minor.yy18 = yylhsminor.yy18;
150278 break;
150279 case 176: /* expr ::= ID|INDEXED LP STAR RP over_clause */
150280{
150281 yylhsminor.yy18 = sqlite3ExprFunction(pParse, 0, &yymsp[-4].minor.yy0, 0);
150282 sqlite3WindowAttach(pParse, yylhsminor.yy18, yymsp[0].minor.yy327);
150283}
150284 yymsp[-4].minor.yy18 = yylhsminor.yy18;
150285 break;
150286 case 177: /* term ::= CTIME_KW */
150287{
150288 yylhsminor.yy18 = sqlite3ExprFunction(pParse, 0, &yymsp[0].minor.yy0, 0);
150289}
150290 yymsp[0].minor.yy18 = yylhsminor.yy18;
150291 break;
150292 case 178: /* expr ::= LP nexprlist COMMA expr RP */
150293{
150294 ExprList *pList = sqlite3ExprListAppend(pParse, yymsp[-3].minor.yy420, yymsp[-1].minor.yy18);
150295 yymsp[-4].minor.yy18 = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
150296 if( yymsp[-4].minor.yy18 ){
150297 yymsp[-4].minor.yy18->x.pList = pList;
150298 }else{
150299 sqlite3ExprListDelete(pParse->db, pList);
150300 }
150301}
150302 break;
150303 case 179: /* expr ::= expr AND expr */
150304 case 180: /* expr ::= expr OR expr */ yytestcase(yyruleno==180);
150305 case 181: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==181);
150306 case 182: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==182);
150307 case 183: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==183);
150308 case 184: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==184);
150309 case 185: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==185);
150310 case 186: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==186);
150311{yymsp[-2].minor.yy18=sqlite3PExpr(pParse,yymsp[-1].major,yymsp[-2].minor.yy18,yymsp[0].minor.yy18);}
150312 break;
150313 case 187: /* likeop ::= NOT LIKE_KW|MATCH */
150314{yymsp[-1].minor.yy0=yymsp[0].minor.yy0; yymsp[-1].minor.yy0.n|=0x80000000; /*yymsp[-1].minor.yy0-overwrite-yymsp[0].minor.yy0*/}
150315 break;
150316 case 188: /* expr ::= expr likeop expr */
150317{
150318 ExprList *pList;
150319 int bNot = yymsp[-1].minor.yy0.n & 0x80000000;
150320 yymsp[-1].minor.yy0.n &= 0x7fffffff;
150321 pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy18);
150322 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy18);
150323 yymsp[-2].minor.yy18 = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy0, 0);
150324 if( bNot ) yymsp[-2].minor.yy18 = sqlite3PExpr(pParse, TK_NOT, yymsp[-2].minor.yy18, 0);
150325 if( yymsp[-2].minor.yy18 ) yymsp[-2].minor.yy18->flags |= EP_InfixFunc;
150326}
150327 break;
150328 case 189: /* expr ::= expr likeop expr ESCAPE expr */
150329{
150330 ExprList *pList;
150331 int bNot = yymsp[-3].minor.yy0.n & 0x80000000;
150332 yymsp[-3].minor.yy0.n &= 0x7fffffff;
150333 pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy18);
150334 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy18);
150335 pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy18);
150336 yymsp[-4].minor.yy18 = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy0, 0);
150337 if( bNot ) yymsp[-4].minor.yy18 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy18, 0);
150338 if( yymsp[-4].minor.yy18 ) yymsp[-4].minor.yy18->flags |= EP_InfixFunc;
150339}
150340 break;
150341 case 190: /* expr ::= expr ISNULL|NOTNULL */
150342{yymsp[-1].minor.yy18 = sqlite3PExpr(pParse,yymsp[0].major,yymsp[-1].minor.yy18,0);}
150343 break;
150344 case 191: /* expr ::= expr NOT NULL */
150345{yymsp[-2].minor.yy18 = sqlite3PExpr(pParse,TK_NOTNULL,yymsp[-2].minor.yy18,0);}
150346 break;
150347 case 192: /* expr ::= expr IS expr */
150348{
150349 yymsp[-2].minor.yy18 = sqlite3PExpr(pParse,TK_IS,yymsp[-2].minor.yy18,yymsp[0].minor.yy18);
150350 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy18, yymsp[-2].minor.yy18, TK_ISNULL);
150351}
150352 break;
150353 case 193: /* expr ::= expr IS NOT expr */
150354{
150355 yymsp[-3].minor.yy18 = sqlite3PExpr(pParse,TK_ISNOT,yymsp[-3].minor.yy18,yymsp[0].minor.yy18);
150356 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy18, yymsp[-3].minor.yy18, TK_NOTNULL);
150357}
150358 break;
150359 case 194: /* expr ::= NOT expr */
150360 case 195: /* expr ::= BITNOT expr */ yytestcase(yyruleno==195);
150361{yymsp[-1].minor.yy18 = sqlite3PExpr(pParse, yymsp[-1].major, yymsp[0].minor.yy18, 0);/*A-overwrites-B*/}
150362 break;
150363 case 196: /* expr ::= PLUS|MINUS expr */
150364{
150365 yymsp[-1].minor.yy18 = sqlite3PExpr(pParse, yymsp[-1].major==TK_PLUS ? TK_UPLUS : TK_UMINUS, yymsp[0].minor.yy18, 0);
150366 /*A-overwrites-B*/
150367}
150368 break;
150369 case 197: /* between_op ::= BETWEEN */
150370 case 200: /* in_op ::= IN */ yytestcase(yyruleno==200);
150371{yymsp[0].minor.yy70 = 0;}
150372 break;
150373 case 199: /* expr ::= expr between_op expr AND expr */
150374{
150375 ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy18);
150376 pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy18);
150377 yymsp[-4].minor.yy18 = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy18, 0);
150378 if( yymsp[-4].minor.yy18 ){
150379 yymsp[-4].minor.yy18->x.pList = pList;
150380 }else{
150381 sqlite3ExprListDelete(pParse->db, pList);
150382 }
150383 if( yymsp[-3].minor.yy70 ) yymsp[-4].minor.yy18 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy18, 0);
150384}
150385 break;
150386 case 202: /* expr ::= expr in_op LP exprlist RP */
150387{
150388 if( yymsp[-1].minor.yy420==0 ){
150389 /* Expressions of the form
150390 **
150391 ** expr1 IN ()
150392 ** expr1 NOT IN ()
150393 **
150394 ** simplify to constants 0 (false) and 1 (true), respectively,
150395 ** regardless of the value of expr1.
150396 */
150397 sqlite3ExprDelete(pParse->db, yymsp[-4].minor.yy18);
150398 yymsp[-4].minor.yy18 = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[yymsp[-3].minor.yy70],1);
150399 }else if( yymsp[-1].minor.yy420->nExpr==1 ){
150400 /* Expressions of the form:
150401 **
150402 ** expr1 IN (?1)
150403 ** expr1 NOT IN (?2)
150404 **
150405 ** with exactly one value on the RHS can be simplified to something
150406 ** like this:
150407 **
150408 ** expr1 == ?1
150409 ** expr1 <> ?2
150410 **
150411 ** But, the RHS of the == or <> is marked with the EP_Generic flag
150412 ** so that it may not contribute to the computation of comparison
150413 ** affinity or the collating sequence to use for comparison. Otherwise,
150414 ** the semantics would be subtly different from IN or NOT IN.
150415 */
150416 Expr *pRHS = yymsp[-1].minor.yy420->a[0].pExpr;
150417 yymsp[-1].minor.yy420->a[0].pExpr = 0;
150418 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy420);
150419 /* pRHS cannot be NULL because a malloc error would have been detected
150420 ** before now and control would have never reached this point */
150421 if( ALWAYS(pRHS) ){
150422 pRHS->flags &= ~EP_Collate;
150423 pRHS->flags |= EP_Generic;
150424 }
150425 yymsp[-4].minor.yy18 = sqlite3PExpr(pParse, yymsp[-3].minor.yy70 ? TK_NE : TK_EQ, yymsp[-4].minor.yy18, pRHS);
150426 }else{
150427 yymsp[-4].minor.yy18 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy18, 0);
150428 if( yymsp[-4].minor.yy18 ){
150429 yymsp[-4].minor.yy18->x.pList = yymsp[-1].minor.yy420;
150430 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy18);
150431 }else{
150432 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy420);
150433 }
150434 if( yymsp[-3].minor.yy70 ) yymsp[-4].minor.yy18 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy18, 0);
150435 }
150436 }
150437 break;
150438 case 203: /* expr ::= LP select RP */
150439{
150440 yymsp[-2].minor.yy18 = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
150441 sqlite3PExprAddSelect(pParse, yymsp[-2].minor.yy18, yymsp[-1].minor.yy489);
150442 }
150443 break;
150444 case 204: /* expr ::= expr in_op LP select RP */
150445{
150446 yymsp[-4].minor.yy18 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy18, 0);
150447 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy18, yymsp[-1].minor.yy489);
150448 if( yymsp[-3].minor.yy70 ) yymsp[-4].minor.yy18 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy18, 0);
150449 }
150450 break;
150451 case 205: /* expr ::= expr in_op nm dbnm paren_exprlist */
150452{
150453 SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);
150454 Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
150455 if( yymsp[0].minor.yy420 ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, yymsp[0].minor.yy420);
150456 yymsp[-4].minor.yy18 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy18, 0);
150457 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy18, pSelect);
150458 if( yymsp[-3].minor.yy70 ) yymsp[-4].minor.yy18 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy18, 0);
150459 }
150460 break;
150461 case 206: /* expr ::= EXISTS LP select RP */
150462{
150463 Expr *p;
150464 p = yymsp[-3].minor.yy18 = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
150465 sqlite3PExprAddSelect(pParse, p, yymsp[-1].minor.yy489);
150466 }
150467 break;
150468 case 207: /* expr ::= CASE case_operand case_exprlist case_else END */
150469{
150470 yymsp[-4].minor.yy18 = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy18, 0);
150471 if( yymsp[-4].minor.yy18 ){
150472 yymsp[-4].minor.yy18->x.pList = yymsp[-1].minor.yy18 ? sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy420,yymsp[-1].minor.yy18) : yymsp[-2].minor.yy420;
150473 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy18);
150474 }else{
150475 sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy420);
150476 sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy18);
150477 }
150478}
150479 break;
150480 case 208: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
150481{
150482 yymsp[-4].minor.yy420 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy420, yymsp[-2].minor.yy18);
150483 yymsp[-4].minor.yy420 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy420, yymsp[0].minor.yy18);
150484}
150485 break;
150486 case 209: /* case_exprlist ::= WHEN expr THEN expr */
150487{
150488 yymsp[-3].minor.yy420 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy18);
150489 yymsp[-3].minor.yy420 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy420, yymsp[0].minor.yy18);
150490}
150491 break;
150492 case 212: /* case_operand ::= expr */
150493{yymsp[0].minor.yy18 = yymsp[0].minor.yy18; /*A-overwrites-X*/}
150494 break;
150495 case 215: /* nexprlist ::= nexprlist COMMA expr */
150496{yymsp[-2].minor.yy420 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy420,yymsp[0].minor.yy18);}
150497 break;
150498 case 216: /* nexprlist ::= expr */
150499{yymsp[0].minor.yy420 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy18); /*A-overwrites-Y*/}
150500 break;
150501 case 218: /* paren_exprlist ::= LP exprlist RP */
150502 case 223: /* eidlist_opt ::= LP eidlist RP */ yytestcase(yyruleno==223);
150503{yymsp[-2].minor.yy420 = yymsp[-1].minor.yy420;}
150504 break;
150505 case 219: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
150506{
150507 sqlite3CreateIndex(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0,
150508 sqlite3SrcListAppend(pParse->db,0,&yymsp[-4].minor.yy0,0), yymsp[-2].minor.yy420, yymsp[-10].minor.yy70,
150509 &yymsp[-11].minor.yy0, yymsp[0].minor.yy18, SQLITE_SO_ASC, yymsp[-8].minor.yy70, SQLITE_IDXTYPE_APPDEF);
150510 if( IN_RENAME_OBJECT && pParse->pNewIndex ){
150511 sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &yymsp[-4].minor.yy0);
150512 }
150513}
150514 break;
150515 case 220: /* uniqueflag ::= UNIQUE */
150516 case 260: /* raisetype ::= ABORT */ yytestcase(yyruleno==260);
150517{yymsp[0].minor.yy70 = OE_Abort;}
150518 break;
150519 case 221: /* uniqueflag ::= */
150520{yymsp[1].minor.yy70 = OE_None;}
150521 break;
150522 case 224: /* eidlist ::= eidlist COMMA nm collate sortorder */
150523{
150524 yymsp[-4].minor.yy420 = parserAddExprIdListTerm(pParse, yymsp[-4].minor.yy420, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy70, yymsp[0].minor.yy70);
150525}
150526 break;
150527 case 225: /* eidlist ::= nm collate sortorder */
150528{
150529 yymsp[-2].minor.yy420 = parserAddExprIdListTerm(pParse, 0, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy70, yymsp[0].minor.yy70); /*A-overwrites-Y*/
150530}
150531 break;
150532 case 228: /* cmd ::= DROP INDEX ifexists fullname */
150533{sqlite3DropIndex(pParse, yymsp[0].minor.yy135, yymsp[-1].minor.yy70);}
150534 break;
150535 case 229: /* cmd ::= VACUUM */
150536{sqlite3Vacuum(pParse,0);}
150537 break;
150538 case 230: /* cmd ::= VACUUM nm */
150539{sqlite3Vacuum(pParse,&yymsp[0].minor.yy0);}
150540 break;
150541 case 231: /* cmd ::= PRAGMA nm dbnm */
150542{sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
150543 break;
150544 case 232: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
150545{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
150546 break;
150547 case 233: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
150548{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
150549 break;
150550 case 234: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
150551{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
150552 break;
150553 case 235: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
150554{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
150555 break;
150556 case 238: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
150557{
150558 Token all;
150559 all.z = yymsp[-3].minor.yy0.z;
150560 all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
150561 sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy207, &all);
150562}
150563 break;
150564 case 239: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
150565{
150566 sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy70, yymsp[-4].minor.yy34.a, yymsp[-4].minor.yy34.b, yymsp[-2].minor.yy135, yymsp[0].minor.yy18, yymsp[-10].minor.yy70, yymsp[-8].minor.yy70);
150567 yymsp[-10].minor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0); /*A-overwrites-T*/
150568}
150569 break;
150570 case 240: /* trigger_time ::= BEFORE|AFTER */
150571{ yymsp[0].minor.yy70 = yymsp[0].major; /*A-overwrites-X*/ }
150572 break;
150573 case 241: /* trigger_time ::= INSTEAD OF */
150574{ yymsp[-1].minor.yy70 = TK_INSTEAD;}
150575 break;
150576 case 242: /* trigger_time ::= */
150577{ yymsp[1].minor.yy70 = TK_BEFORE; }
150578 break;
150579 case 243: /* trigger_event ::= DELETE|INSERT */
150580 case 244: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==244);
150581{yymsp[0].minor.yy34.a = yymsp[0].major; /*A-overwrites-X*/ yymsp[0].minor.yy34.b = 0;}
150582 break;
150583 case 245: /* trigger_event ::= UPDATE OF idlist */
150584{yymsp[-2].minor.yy34.a = TK_UPDATE; yymsp[-2].minor.yy34.b = yymsp[0].minor.yy48;}
150585 break;
150586 case 246: /* when_clause ::= */
150587 case 265: /* key_opt ::= */ yytestcase(yyruleno==265);
150588 case 307: /* filter_opt ::= */ yytestcase(yyruleno==307);
150589{ yymsp[1].minor.yy18 = 0; }
150590 break;
150591 case 247: /* when_clause ::= WHEN expr */
150592 case 266: /* key_opt ::= KEY expr */ yytestcase(yyruleno==266);
150593{ yymsp[-1].minor.yy18 = yymsp[0].minor.yy18; }
150594 break;
150595 case 248: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
150596{
150597 assert( yymsp[-2].minor.yy207!=0 );
150598 yymsp[-2].minor.yy207->pLast->pNext = yymsp[-1].minor.yy207;
150599 yymsp[-2].minor.yy207->pLast = yymsp[-1].minor.yy207;
150600}
150601 break;
150602 case 249: /* trigger_cmd_list ::= trigger_cmd SEMI */
150603{
150604 assert( yymsp[-1].minor.yy207!=0 );
150605 yymsp[-1].minor.yy207->pLast = yymsp[-1].minor.yy207;
150606}
150607 break;
150608 case 250: /* trnm ::= nm DOT nm */
150609{
150610 yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;
150611 sqlite3ErrorMsg(pParse,
150612 "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
150613 "statements within triggers");
150614}
150615 break;
150616 case 251: /* tridxby ::= INDEXED BY nm */
150617{
150618 sqlite3ErrorMsg(pParse,
150619 "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
150620 "within triggers");
150621}
150622 break;
150623 case 252: /* tridxby ::= NOT INDEXED */
150624{
150625 sqlite3ErrorMsg(pParse,
150626 "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
150627 "within triggers");
150628}
150629 break;
150630 case 253: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt scanpt */
150631{yylhsminor.yy207 = sqlite3TriggerUpdateStep(pParse, &yymsp[-5].minor.yy0, yymsp[-2].minor.yy420, yymsp[-1].minor.yy18, yymsp[-6].minor.yy70, yymsp[-7].minor.yy0.z, yymsp[0].minor.yy392);}
150632 yymsp[-7].minor.yy207 = yylhsminor.yy207;
150633 break;
150634 case 254: /* trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
150635{
150636 yylhsminor.yy207 = sqlite3TriggerInsertStep(pParse,&yymsp[-4].minor.yy0,yymsp[-3].minor.yy48,yymsp[-2].minor.yy489,yymsp[-6].minor.yy70,yymsp[-1].minor.yy340,yymsp[-7].minor.yy392,yymsp[0].minor.yy392);/*yylhsminor.yy207-overwrites-yymsp[-6].minor.yy70*/
150637}
150638 yymsp[-7].minor.yy207 = yylhsminor.yy207;
150639 break;
150640 case 255: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
150641{yylhsminor.yy207 = sqlite3TriggerDeleteStep(pParse, &yymsp[-3].minor.yy0, yymsp[-1].minor.yy18, yymsp[-5].minor.yy0.z, yymsp[0].minor.yy392);}
150642 yymsp[-5].minor.yy207 = yylhsminor.yy207;
150643 break;
150644 case 256: /* trigger_cmd ::= scanpt select scanpt */
150645{yylhsminor.yy207 = sqlite3TriggerSelectStep(pParse->db, yymsp[-1].minor.yy489, yymsp[-2].minor.yy392, yymsp[0].minor.yy392); /*yylhsminor.yy207-overwrites-yymsp[-1].minor.yy489*/}
150646 yymsp[-2].minor.yy207 = yylhsminor.yy207;
150647 break;
150648 case 257: /* expr ::= RAISE LP IGNORE RP */
150649{
150650 yymsp[-3].minor.yy18 = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
150651 if( yymsp[-3].minor.yy18 ){
150652 yymsp[-3].minor.yy18->affinity = OE_Ignore;
150653 }
150654}
150655 break;
150656 case 258: /* expr ::= RAISE LP raisetype COMMA nm RP */
150657{
150658 yymsp[-5].minor.yy18 = sqlite3ExprAlloc(pParse->db, TK_RAISE, &yymsp[-1].minor.yy0, 1);
150659 if( yymsp[-5].minor.yy18 ) {
150660 yymsp[-5].minor.yy18->affinity = (char)yymsp[-3].minor.yy70;
150661 }
150662}
150663 break;
150664 case 259: /* raisetype ::= ROLLBACK */
150665{yymsp[0].minor.yy70 = OE_Rollback;}
150666 break;
150667 case 261: /* raisetype ::= FAIL */
150668{yymsp[0].minor.yy70 = OE_Fail;}
150669 break;
150670 case 262: /* cmd ::= DROP TRIGGER ifexists fullname */
150671{
150672 sqlite3DropTrigger(pParse,yymsp[0].minor.yy135,yymsp[-1].minor.yy70);
150673}
150674 break;
150675 case 263: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
150676{
150677 sqlite3Attach(pParse, yymsp[-3].minor.yy18, yymsp[-1].minor.yy18, yymsp[0].minor.yy18);
150678}
150679 break;
150680 case 264: /* cmd ::= DETACH database_kw_opt expr */
150681{
150682 sqlite3Detach(pParse, yymsp[0].minor.yy18);
150683}
150684 break;
150685 case 267: /* cmd ::= REINDEX */
150686{sqlite3Reindex(pParse, 0, 0);}
150687 break;
150688 case 268: /* cmd ::= REINDEX nm dbnm */
150689{sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
150690 break;
150691 case 269: /* cmd ::= ANALYZE */
150692{sqlite3Analyze(pParse, 0, 0);}
150693 break;
150694 case 270: /* cmd ::= ANALYZE nm dbnm */
150695{sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
150696 break;
150697 case 271: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
150698{
150699 sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy135,&yymsp[0].minor.yy0);
150700}
150701 break;
150702 case 272: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
150703{
150704 yymsp[-1].minor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-1].minor.yy0.z) + pParse->sLastToken.n;
150705 sqlite3AlterFinishAddColumn(pParse, &yymsp[-1].minor.yy0);
150706}
150707 break;
150708 case 273: /* add_column_fullname ::= fullname */
150709{
150710 disableLookaside(pParse);
150711 sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy135);
150712}
150713 break;
150714 case 274: /* cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
150715{
150716 sqlite3AlterRenameColumn(pParse, yymsp[-5].minor.yy135, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0);
150717}
150718 break;
150719 case 275: /* cmd ::= create_vtab */
150720{sqlite3VtabFinishParse(pParse,0);}
150721 break;
150722 case 276: /* cmd ::= create_vtab LP vtabarglist RP */
150723{sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);}
150724 break;
150725 case 277: /* create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
150726{
150727 sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, yymsp[-4].minor.yy70);
150728}
150729 break;
150730 case 278: /* vtabarg ::= */
150731{sqlite3VtabArgInit(pParse);}
150732 break;
150733 case 279: /* vtabargtoken ::= ANY */
150734 case 280: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==280);
150735 case 281: /* lp ::= LP */ yytestcase(yyruleno==281);
150736{sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
150737 break;
150738 case 282: /* with ::= WITH wqlist */
150739 case 283: /* with ::= WITH RECURSIVE wqlist */ yytestcase(yyruleno==283);
150740{ sqlite3WithPush(pParse, yymsp[0].minor.yy449, 1); }
150741 break;
150742 case 284: /* wqlist ::= nm eidlist_opt AS LP select RP */
150743{
150744 yymsp[-5].minor.yy449 = sqlite3WithAdd(pParse, 0, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy420, yymsp[-1].minor.yy489); /*A-overwrites-X*/
150745}
150746 break;
150747 case 285: /* wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP */
150748{
150749 yymsp[-7].minor.yy449 = sqlite3WithAdd(pParse, yymsp[-7].minor.yy449, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy420, yymsp[-1].minor.yy489);
150750}
150751 break;
150752 case 286: /* windowdefn_list ::= windowdefn */
150753{ yylhsminor.yy327 = yymsp[0].minor.yy327; }
150754 yymsp[0].minor.yy327 = yylhsminor.yy327;
150755 break;
150756 case 287: /* windowdefn_list ::= windowdefn_list COMMA windowdefn */
150757{
150758 assert( yymsp[0].minor.yy327!=0 );
150759 yymsp[0].minor.yy327->pNextWin = yymsp[-2].minor.yy327;
150760 yylhsminor.yy327 = yymsp[0].minor.yy327;
150761}
150762 yymsp[-2].minor.yy327 = yylhsminor.yy327;
150763 break;
150764 case 288: /* windowdefn ::= nm AS window */
150765{
150766 if( ALWAYS(yymsp[0].minor.yy327) ){
150767 yymsp[0].minor.yy327->zName = sqlite3DbStrNDup(pParse->db, yymsp[-2].minor.yy0.z, yymsp[-2].minor.yy0.n);
150768 }
150769 yylhsminor.yy327 = yymsp[0].minor.yy327;
150770}
150771 yymsp[-2].minor.yy327 = yylhsminor.yy327;
150772 break;
150773 case 289: /* window ::= LP part_opt orderby_opt frame_opt RP */
150774{
150775 yymsp[-4].minor.yy327 = yymsp[-1].minor.yy327;
150776 if( ALWAYS(yymsp[-4].minor.yy327) ){
150777 yymsp[-4].minor.yy327->pPartition = yymsp[-3].minor.yy420;
150778 yymsp[-4].minor.yy327->pOrderBy = yymsp[-2].minor.yy420;
150779 }
150780}
150781 break;
150782 case 290: /* part_opt ::= PARTITION BY nexprlist */
150783{ yymsp[-2].minor.yy420 = yymsp[0].minor.yy420; }
150784 break;
150785 case 291: /* part_opt ::= */
150786{ yymsp[1].minor.yy420 = 0; }
150787 break;
150788 case 292: /* frame_opt ::= */
150789{
150790 yymsp[1].minor.yy327 = sqlite3WindowAlloc(pParse, TK_RANGE, TK_UNBOUNDED, 0, TK_CURRENT, 0);
150791}
150792 break;
150793 case 293: /* frame_opt ::= range_or_rows frame_bound_s */
150794{
150795 yylhsminor.yy327 = sqlite3WindowAlloc(pParse, yymsp[-1].minor.yy70, yymsp[0].minor.yy119.eType, yymsp[0].minor.yy119.pExpr, TK_CURRENT, 0);
150796}
150797 yymsp[-1].minor.yy327 = yylhsminor.yy327;
150798 break;
150799 case 294: /* frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e */
150800{
150801 yylhsminor.yy327 = sqlite3WindowAlloc(pParse, yymsp[-4].minor.yy70, yymsp[-2].minor.yy119.eType, yymsp[-2].minor.yy119.pExpr, yymsp[0].minor.yy119.eType, yymsp[0].minor.yy119.pExpr);
150802}
150803 yymsp[-4].minor.yy327 = yylhsminor.yy327;
150804 break;
150805 case 295: /* range_or_rows ::= RANGE */
150806{ yymsp[0].minor.yy70 = TK_RANGE; }
150807 break;
150808 case 296: /* range_or_rows ::= ROWS */
150809{ yymsp[0].minor.yy70 = TK_ROWS; }
150810 break;
150811 case 297: /* frame_bound_s ::= frame_bound */
150812 case 299: /* frame_bound_e ::= frame_bound */ yytestcase(yyruleno==299);
150813{ yylhsminor.yy119 = yymsp[0].minor.yy119; }
150814 yymsp[0].minor.yy119 = yylhsminor.yy119;
150815 break;
150816 case 298: /* frame_bound_s ::= UNBOUNDED PRECEDING */
150817 case 300: /* frame_bound_e ::= UNBOUNDED FOLLOWING */ yytestcase(yyruleno==300);
150818{yymsp[-1].minor.yy119.eType = TK_UNBOUNDED; yymsp[-1].minor.yy119.pExpr = 0;}
150819 break;
150820 case 301: /* frame_bound ::= expr PRECEDING */
150821{ yylhsminor.yy119.eType = TK_PRECEDING; yylhsminor.yy119.pExpr = yymsp[-1].minor.yy18; }
150822 yymsp[-1].minor.yy119 = yylhsminor.yy119;
150823 break;
150824 case 302: /* frame_bound ::= CURRENT ROW */
150825{ yymsp[-1].minor.yy119.eType = TK_CURRENT ; yymsp[-1].minor.yy119.pExpr = 0; }
150826 break;
150827 case 303: /* frame_bound ::= expr FOLLOWING */
150828{ yylhsminor.yy119.eType = TK_FOLLOWING; yylhsminor.yy119.pExpr = yymsp[-1].minor.yy18; }
150829 yymsp[-1].minor.yy119 = yylhsminor.yy119;
150830 break;
150831 case 304: /* window_clause ::= WINDOW windowdefn_list */
150832{ yymsp[-1].minor.yy327 = yymsp[0].minor.yy327; }
150833 break;
150834 case 305: /* over_clause ::= filter_opt OVER window */
150835{
150836 yylhsminor.yy327 = yymsp[0].minor.yy327;
150837 assert( yylhsminor.yy327!=0 );
150838 yylhsminor.yy327->pFilter = yymsp[-2].minor.yy18;
150839}
150840 yymsp[-2].minor.yy327 = yylhsminor.yy327;
150841 break;
150842 case 306: /* over_clause ::= filter_opt OVER nm */
150843{
150844 yylhsminor.yy327 = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
150845 if( yylhsminor.yy327 ){
150846 yylhsminor.yy327->zName = sqlite3DbStrNDup(pParse->db, yymsp[0].minor.yy0.z, yymsp[0].minor.yy0.n);
150847 yylhsminor.yy327->pFilter = yymsp[-2].minor.yy18;
150848 }else{
150849 sqlite3ExprDelete(pParse->db, yymsp[-2].minor.yy18);
150850 }
150851}
150852 yymsp[-2].minor.yy327 = yylhsminor.yy327;
150853 break;
150854 case 308: /* filter_opt ::= FILTER LP WHERE expr RP */
150855{ yymsp[-4].minor.yy18 = yymsp[-1].minor.yy18; }
150856 break;
150857 default:
150858 /* (309) input ::= cmdlist */ yytestcase(yyruleno==309);
150859 /* (310) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==310);
150860 /* (311) cmdlist ::= ecmd (OPTIMIZED OUT) */ assert(yyruleno!=311);
150861 /* (312) ecmd ::= SEMI */ yytestcase(yyruleno==312);
150862 /* (313) ecmd ::= cmdx SEMI */ yytestcase(yyruleno==313);
150863 /* (314) ecmd ::= explain cmdx */ yytestcase(yyruleno==314);
150864 /* (315) trans_opt ::= */ yytestcase(yyruleno==315);
150865 /* (316) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==316);
150866 /* (317) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==317);
150867 /* (318) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==318);
150868 /* (319) savepoint_opt ::= */ yytestcase(yyruleno==319);
150869 /* (320) cmd ::= create_table create_table_args */ yytestcase(yyruleno==320);
150870 /* (321) columnlist ::= columnlist COMMA columnname carglist */ yytestcase(yyruleno==321);
150871 /* (322) columnlist ::= columnname carglist */ yytestcase(yyruleno==322);
150872 /* (323) nm ::= ID|INDEXED */ yytestcase(yyruleno==323);
150873 /* (324) nm ::= STRING */ yytestcase(yyruleno==324);
150874 /* (325) nm ::= JOIN_KW */ yytestcase(yyruleno==325);
150875 /* (326) typetoken ::= typename */ yytestcase(yyruleno==326);
150876 /* (327) typename ::= ID|STRING */ yytestcase(yyruleno==327);
150877 /* (328) signed ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=328);
150878 /* (329) signed ::= minus_num (OPTIMIZED OUT) */ assert(yyruleno!=329);
150879 /* (330) carglist ::= carglist ccons */ yytestcase(yyruleno==330);
150880 /* (331) carglist ::= */ yytestcase(yyruleno==331);
150881 /* (332) ccons ::= NULL onconf */ yytestcase(yyruleno==332);
150882 /* (333) conslist_opt ::= COMMA conslist */ yytestcase(yyruleno==333);
150883 /* (334) conslist ::= conslist tconscomma tcons */ yytestcase(yyruleno==334);
150884 /* (335) conslist ::= tcons (OPTIMIZED OUT) */ assert(yyruleno!=335);
150885 /* (336) tconscomma ::= */ yytestcase(yyruleno==336);
150886 /* (337) defer_subclause_opt ::= defer_subclause (OPTIMIZED OUT) */ assert(yyruleno!=337);
150887 /* (338) resolvetype ::= raisetype (OPTIMIZED OUT) */ assert(yyruleno!=338);
150888 /* (339) selectnowith ::= oneselect (OPTIMIZED OUT) */ assert(yyruleno!=339);
150889 /* (340) oneselect ::= values */ yytestcase(yyruleno==340);
150890 /* (341) sclp ::= selcollist COMMA */ yytestcase(yyruleno==341);
150891 /* (342) as ::= ID|STRING */ yytestcase(yyruleno==342);
150892 /* (343) expr ::= term (OPTIMIZED OUT) */ assert(yyruleno!=343);
150893 /* (344) likeop ::= LIKE_KW|MATCH */ yytestcase(yyruleno==344);
150894 /* (345) exprlist ::= nexprlist */ yytestcase(yyruleno==345);
150895 /* (346) nmnum ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=346);
150896 /* (347) nmnum ::= nm (OPTIMIZED OUT) */ assert(yyruleno!=347);
150897 /* (348) nmnum ::= ON */ yytestcase(yyruleno==348);
150898 /* (349) nmnum ::= DELETE */ yytestcase(yyruleno==349);
150899 /* (350) nmnum ::= DEFAULT */ yytestcase(yyruleno==350);
150900 /* (351) plus_num ::= INTEGER|FLOAT */ yytestcase(yyruleno==351);
150901 /* (352) foreach_clause ::= */ yytestcase(yyruleno==352);
150902 /* (353) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==353);
150903 /* (354) trnm ::= nm */ yytestcase(yyruleno==354);
150904 /* (355) tridxby ::= */ yytestcase(yyruleno==355);
150905 /* (356) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==356);
150906 /* (357) database_kw_opt ::= */ yytestcase(yyruleno==357);
150907 /* (358) kwcolumn_opt ::= */ yytestcase(yyruleno==358);
150908 /* (359) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==359);
150909 /* (360) vtabarglist ::= vtabarg */ yytestcase(yyruleno==360);
150910 /* (361) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==361);
150911 /* (362) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==362);
150912 /* (363) anylist ::= */ yytestcase(yyruleno==363);
150913 /* (364) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==364);
150914 /* (365) anylist ::= anylist ANY */ yytestcase(yyruleno==365);
150915 /* (366) with ::= */ yytestcase(yyruleno==366);
150916 break;
150917/********** End reduce actions ************************************************/
150918 };
150919 assert( yyruleno<sizeof(yyRuleInfo)/sizeof(yyRuleInfo[0]) );
150920 yygoto = yyRuleInfo[yyruleno].lhs;
150921 yysize = yyRuleInfo[yyruleno].nrhs;
150922 yyact = yy_find_reduce_action(yymsp[yysize].stateno,(YYCODETYPE)yygoto);
150923
150924 /* There are no SHIFTREDUCE actions on nonterminals because the table
150925 ** generator has simplified them to pure REDUCE actions. */
150926 assert( !(yyact>YY_MAX_SHIFT && yyact<=YY_MAX_SHIFTREDUCE) );
150927
150928 /* It is not possible for a REDUCE to be followed by an error */
150929 assert( yyact!=YY_ERROR_ACTION );
150930
150931 yymsp += yysize+1;
150932 yypParser->yytos = yymsp;
150933 yymsp->stateno = (YYACTIONTYPE)yyact;
150934 yymsp->major = (YYCODETYPE)yygoto;
150935 yyTraceShift(yypParser, yyact, "... then shift");
150936 return yyact;
150937}
150938
150939/*
150940** The following code executes when the parse fails
150941*/
150942#ifndef YYNOERRORRECOVERY
150943static void yy_parse_failed(
150944 yyParser *yypParser /* The parser */
150945){
150946 sqlite3ParserARG_FETCH
150947 sqlite3ParserCTX_FETCH
150948#ifndef NDEBUG
150949 if( yyTraceFILE ){
150950 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
150951 }
150952#endif
150953 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
150954 /* Here code is inserted which will be executed whenever the
150955 ** parser fails */
150956/************ Begin %parse_failure code ***************************************/
150957/************ End %parse_failure code *****************************************/
150958 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
150959 sqlite3ParserCTX_STORE
150960}
150961#endif /* YYNOERRORRECOVERY */
150962
150963/*
150964** The following code executes when a syntax error first occurs.
150965*/
150966static void yy_syntax_error(
150967 yyParser *yypParser, /* The parser */
150968 int yymajor, /* The major type of the error token */
150969 sqlite3ParserTOKENTYPE yyminor /* The minor type of the error token */
150970){
150971 sqlite3ParserARG_FETCH
150972 sqlite3ParserCTX_FETCH
150973#define TOKEN yyminor
150974/************ Begin %syntax_error code ****************************************/
150975
150976 UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */
150977 if( TOKEN.z[0] ){
150978 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
150979 }else{
150980 sqlite3ErrorMsg(pParse, "incomplete input");
150981 }
150982/************ End %syntax_error code ******************************************/
150983 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
150984 sqlite3ParserCTX_STORE
150985}
150986
150987/*
150988** The following is executed when the parser accepts
150989*/
150990static void yy_accept(
150991 yyParser *yypParser /* The parser */
150992){
150993 sqlite3ParserARG_FETCH
150994 sqlite3ParserCTX_FETCH
150995#ifndef NDEBUG
150996 if( yyTraceFILE ){
150997 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
150998 }
150999#endif
151000#ifndef YYNOERRORRECOVERY
151001 yypParser->yyerrcnt = -1;
151002#endif
151003 assert( yypParser->yytos==yypParser->yystack );
151004 /* Here code is inserted which will be executed whenever the
151005 ** parser accepts */
151006/*********** Begin %parse_accept code *****************************************/
151007/*********** End %parse_accept code *******************************************/
151008 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
151009 sqlite3ParserCTX_STORE
151010}
151011
151012/* The main parser program.
151013** The first argument is a pointer to a structure obtained from
151014** "sqlite3ParserAlloc" which describes the current state of the parser.
151015** The second argument is the major token number. The third is
151016** the minor token. The fourth optional argument is whatever the
151017** user wants (and specified in the grammar) and is available for
151018** use by the action routines.
151019**
151020** Inputs:
151021** <ul>
151022** <li> A pointer to the parser (an opaque structure.)
151023** <li> The major token number.
151024** <li> The minor token number.
151025** <li> An option argument of a grammar-specified type.
151026** </ul>
151027**
151028** Outputs:
151029** None.
151030*/
151031SQLITE_PRIVATE void sqlite3Parser(
151032 void *yyp, /* The parser */
151033 int yymajor, /* The major token code number */
151034 sqlite3ParserTOKENTYPE yyminor /* The value for the token */
151035 sqlite3ParserARG_PDECL /* Optional %extra_argument parameter */
151036){
151037 YYMINORTYPE yyminorunion;
151038 YYACTIONTYPE yyact; /* The parser action. */
151039#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
151040 int yyendofinput; /* True if we are at the end of input */
151041#endif
151042#ifdef YYERRORSYMBOL
151043 int yyerrorhit = 0; /* True if yymajor has invoked an error */
151044#endif
151045 yyParser *yypParser = (yyParser*)yyp; /* The parser */
151046 sqlite3ParserCTX_FETCH
151047 sqlite3ParserARG_STORE
151048
151049 assert( yypParser->yytos!=0 );
151050#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
151051 yyendofinput = (yymajor==0);
151052#endif
151053
151054 yyact = yypParser->yytos->stateno;
151055#ifndef NDEBUG
151056 if( yyTraceFILE ){
151057 if( yyact < YY_MIN_REDUCE ){
151058 fprintf(yyTraceFILE,"%sInput '%s' in state %d\n",
151059 yyTracePrompt,yyTokenName[yymajor],yyact);
151060 }else{
151061 fprintf(yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
151062 yyTracePrompt,yyTokenName[yymajor],yyact-YY_MIN_REDUCE);
151063 }
151064 }
151065#endif
151066
151067 do{
151068 assert( yyact==yypParser->yytos->stateno );
151069 yyact = yy_find_shift_action((YYCODETYPE)yymajor,yyact);
151070 if( yyact >= YY_MIN_REDUCE ){
151071 yyact = yy_reduce(yypParser,yyact-YY_MIN_REDUCE,yymajor,
151072 yyminor sqlite3ParserCTX_PARAM);
151073 }else if( yyact <= YY_MAX_SHIFTREDUCE ){
151074 yy_shift(yypParser,yyact,(YYCODETYPE)yymajor,yyminor);
151075#ifndef YYNOERRORRECOVERY
151076 yypParser->yyerrcnt--;
151077#endif
151078 break;
151079 }else if( yyact==YY_ACCEPT_ACTION ){
151080 yypParser->yytos--;
151081 yy_accept(yypParser);
151082 return;
151083 }else{
151084 assert( yyact == YY_ERROR_ACTION );
151085 yyminorunion.yy0 = yyminor;
151086#ifdef YYERRORSYMBOL
151087 int yymx;
151088#endif
151089#ifndef NDEBUG
151090 if( yyTraceFILE ){
151091 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
151092 }
151093#endif
151094#ifdef YYERRORSYMBOL
151095 /* A syntax error has occurred.
151096 ** The response to an error depends upon whether or not the
151097 ** grammar defines an error token "ERROR".
151098 **
151099 ** This is what we do if the grammar does define ERROR:
151100 **
151101 ** * Call the %syntax_error function.
151102 **
151103 ** * Begin popping the stack until we enter a state where
151104 ** it is legal to shift the error symbol, then shift
151105 ** the error symbol.
151106 **
151107 ** * Set the error count to three.
151108 **
151109 ** * Begin accepting and shifting new tokens. No new error
151110 ** processing will occur until three tokens have been
151111 ** shifted successfully.
151112 **
151113 */
151114 if( yypParser->yyerrcnt<0 ){
151115 yy_syntax_error(yypParser,yymajor,yyminor);
151116 }
151117 yymx = yypParser->yytos->major;
151118 if( yymx==YYERRORSYMBOL || yyerrorhit ){
151119#ifndef NDEBUG
151120 if( yyTraceFILE ){
151121 fprintf(yyTraceFILE,"%sDiscard input token %s\n",
151122 yyTracePrompt,yyTokenName[yymajor]);
151123 }
151124#endif
151125 yy_destructor(yypParser, (YYCODETYPE)yymajor, &yyminorunion);
151126 yymajor = YYNOCODE;
151127 }else{
151128 while( yypParser->yytos >= yypParser->yystack
151129 && (yyact = yy_find_reduce_action(
151130 yypParser->yytos->stateno,
151131 YYERRORSYMBOL)) > YY_MAX_SHIFTREDUCE
151132 ){
151133 yy_pop_parser_stack(yypParser);
151134 }
151135 if( yypParser->yytos < yypParser->yystack || yymajor==0 ){
151136 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
151137 yy_parse_failed(yypParser);
151138#ifndef YYNOERRORRECOVERY
151139 yypParser->yyerrcnt = -1;
151140#endif
151141 yymajor = YYNOCODE;
151142 }else if( yymx!=YYERRORSYMBOL ){
151143 yy_shift(yypParser,yyact,YYERRORSYMBOL,yyminor);
151144 }
151145 }
151146 yypParser->yyerrcnt = 3;
151147 yyerrorhit = 1;
151148 if( yymajor==YYNOCODE ) break;
151149 yyact = yypParser->yytos->stateno;
151150#elif defined(YYNOERRORRECOVERY)
151151 /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
151152 ** do any kind of error recovery. Instead, simply invoke the syntax
151153 ** error routine and continue going as if nothing had happened.
151154 **
151155 ** Applications can set this macro (for example inside %include) if
151156 ** they intend to abandon the parse upon the first syntax error seen.
151157 */
151158 yy_syntax_error(yypParser,yymajor, yyminor);
151159 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
151160 break;
151161#else /* YYERRORSYMBOL is not defined */
151162 /* This is what we do if the grammar does not define ERROR:
151163 **
151164 ** * Report an error message, and throw away the input token.
151165 **
151166 ** * If the input token is $, then fail the parse.
151167 **
151168 ** As before, subsequent error messages are suppressed until
151169 ** three input tokens have been successfully shifted.
151170 */
151171 if( yypParser->yyerrcnt<=0 ){
151172 yy_syntax_error(yypParser,yymajor, yyminor);
151173 }
151174 yypParser->yyerrcnt = 3;
151175 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
151176 if( yyendofinput ){
151177 yy_parse_failed(yypParser);
151178#ifndef YYNOERRORRECOVERY
151179 yypParser->yyerrcnt = -1;
151180#endif
151181 }
151182 break;
151183#endif
151184 }
151185 }while( yypParser->yytos>yypParser->yystack );
151186#ifndef NDEBUG
151187 if( yyTraceFILE ){
151188 yyStackEntry *i;
151189 char cDiv = '[';
151190 fprintf(yyTraceFILE,"%sReturn. Stack=",yyTracePrompt);
151191 for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){
151192 fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]);
151193 cDiv = ' ';
151194 }
151195 fprintf(yyTraceFILE,"]\n");
151196 }
151197#endif
151198 return;
151199}
151200
151201/*
151202** Return the fallback token corresponding to canonical token iToken, or
151203** 0 if iToken has no fallback.
151204*/
151205SQLITE_PRIVATE int sqlite3ParserFallback(int iToken){
151206#ifdef YYFALLBACK
151207 if( iToken<(int)(sizeof(yyFallback)/sizeof(yyFallback[0])) ){
151208 return yyFallback[iToken];
151209 }
151210#else
151211 (void)iToken;
151212#endif
151213 return 0;
151214}
151215
151216/************** End of parse.c ***********************************************/
151217/************** Begin file tokenize.c ****************************************/
151218/*
151219** 2001 September 15
151220**
151221** The author disclaims copyright to this source code. In place of
151222** a legal notice, here is a blessing:
151223**
151224** May you do good and not evil.
151225** May you find forgiveness for yourself and forgive others.
151226** May you share freely, never taking more than you give.
151227**
151228*************************************************************************
151229** An tokenizer for SQL
151230**
151231** This file contains C code that splits an SQL input string up into
151232** individual tokens and sends those tokens one-by-one over to the
151233** parser for analysis.
151234*/
151235/* #include "sqliteInt.h" */
151236/* #include <stdlib.h> */
151237
151238/* Character classes for tokenizing
151239**
151240** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented
151241** using a lookup table, whereas a switch() directly on c uses a binary search.
151242** The lookup table is much faster. To maximize speed, and to ensure that
151243** a lookup table is used, all of the classes need to be small integers and
151244** all of them need to be used within the switch.
151245*/
151246#define CC_X 0 /* The letter 'x', or start of BLOB literal */
151247#define CC_KYWD 1 /* Alphabetics or '_'. Usable in a keyword */
151248#define CC_ID 2 /* unicode characters usable in IDs */
151249#define CC_DIGIT 3 /* Digits */
151250#define CC_DOLLAR 4 /* '$' */
151251#define CC_VARALPHA 5 /* '@', '#', ':'. Alphabetic SQL variables */
151252#define CC_VARNUM 6 /* '?'. Numeric SQL variables */
151253#define CC_SPACE 7 /* Space characters */
151254#define CC_QUOTE 8 /* '"', '\'', or '`'. String literals, quoted ids */
151255#define CC_QUOTE2 9 /* '['. [...] style quoted ids */
151256#define CC_PIPE 10 /* '|'. Bitwise OR or concatenate */
151257#define CC_MINUS 11 /* '-'. Minus or SQL-style comment */
151258#define CC_LT 12 /* '<'. Part of < or <= or <> */
151259#define CC_GT 13 /* '>'. Part of > or >= */
151260#define CC_EQ 14 /* '='. Part of = or == */
151261#define CC_BANG 15 /* '!'. Part of != */
151262#define CC_SLASH 16 /* '/'. / or c-style comment */
151263#define CC_LP 17 /* '(' */
151264#define CC_RP 18 /* ')' */
151265#define CC_SEMI 19 /* ';' */
151266#define CC_PLUS 20 /* '+' */
151267#define CC_STAR 21 /* '*' */
151268#define CC_PERCENT 22 /* '%' */
151269#define CC_COMMA 23 /* ',' */
151270#define CC_AND 24 /* '&' */
151271#define CC_TILDA 25 /* '~' */
151272#define CC_DOT 26 /* '.' */
151273#define CC_ILLEGAL 27 /* Illegal character */
151274#define CC_NUL 28 /* 0x00 */
151275
151276static const unsigned char aiClass[] = {
151277#ifdef SQLITE_ASCII
151278/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
151279/* 0x */ 28, 27, 27, 27, 27, 27, 27, 27, 27, 7, 7, 27, 7, 7, 27, 27,
151280/* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
151281/* 2x */ 7, 15, 8, 5, 4, 22, 24, 8, 17, 18, 21, 20, 23, 11, 26, 16,
151282/* 3x */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 19, 12, 14, 13, 6,
151283/* 4x */ 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
151284/* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 9, 27, 27, 27, 1,
151285/* 6x */ 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
151286/* 7x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 27, 10, 27, 25, 27,
151287/* 8x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
151288/* 9x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
151289/* Ax */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
151290/* Bx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
151291/* Cx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
151292/* Dx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
151293/* Ex */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
151294/* Fx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
151295#endif
151296#ifdef SQLITE_EBCDIC
151297/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
151298/* 0x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 7, 7, 27, 27,
151299/* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
151300/* 2x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
151301/* 3x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
151302/* 4x */ 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 26, 12, 17, 20, 10,
151303/* 5x */ 24, 27, 27, 27, 27, 27, 27, 27, 27, 27, 15, 4, 21, 18, 19, 27,
151304/* 6x */ 11, 16, 27, 27, 27, 27, 27, 27, 27, 27, 27, 23, 22, 1, 13, 6,
151305/* 7x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 5, 5, 5, 8, 14, 8,
151306/* 8x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
151307/* 9x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
151308/* Ax */ 27, 25, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27,
151309/* Bx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 9, 27, 27, 27, 27, 27,
151310/* Cx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
151311/* Dx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
151312/* Ex */ 27, 27, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27,
151313/* Fx */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 27, 27, 27, 27, 27, 27,
151314#endif
151315};
151316
151317/*
151318** The charMap() macro maps alphabetic characters (only) into their
151319** lower-case ASCII equivalent. On ASCII machines, this is just
151320** an upper-to-lower case map. On EBCDIC machines we also need
151321** to adjust the encoding. The mapping is only valid for alphabetics
151322** which are the only characters for which this feature is used.
151323**
151324** Used by keywordhash.h
151325*/
151326#ifdef SQLITE_ASCII
151327# define charMap(X) sqlite3UpperToLower[(unsigned char)X]
151328#endif
151329#ifdef SQLITE_EBCDIC
151330# define charMap(X) ebcdicToAscii[(unsigned char)X]
151331const unsigned char ebcdicToAscii[] = {
151332/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
151333 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
151334 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
151335 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
151336 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */
151337 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */
151338 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */
151339 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */
151340 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */
151341 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */
151342 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */
151343 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */
151344 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
151345 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */
151346 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */
151347 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */
151348 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */
151349};
151350#endif
151351
151352/*
151353** The sqlite3KeywordCode function looks up an identifier to determine if
151354** it is a keyword. If it is a keyword, the token code of that keyword is
151355** returned. If the input is not a keyword, TK_ID is returned.
151356**
151357** The implementation of this routine was generated by a program,
151358** mkkeywordhash.c, located in the tool subdirectory of the distribution.
151359** The output of the mkkeywordhash.c program is written into a file
151360** named keywordhash.h and then included into this source file by
151361** the #include below.
151362*/
151363/************** Include keywordhash.h in the middle of tokenize.c ************/
151364/************** Begin file keywordhash.h *************************************/
151365/***** This file contains automatically generated code ******
151366**
151367** The code in this file has been automatically generated by
151368**
151369** sqlite/tool/mkkeywordhash.c
151370**
151371** The code in this file implements a function that determines whether
151372** or not a given identifier is really an SQL keyword. The same thing
151373** might be implemented more directly using a hand-written hash table.
151374** But by using this automatically generated code, the size of the code
151375** is substantially reduced. This is important for embedded applications
151376** on platforms with limited memory.
151377*/
151378/* Hash score: 208 */
151379/* zKWText[] encodes 923 bytes of keyword text in 614 bytes */
151380/* REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT */
151381/* ABLEFTHENDEFERRABLELSEXCEPTRANSACTIONATURALTERAISEXCLUSIVE */
151382/* XISTSAVEPOINTERSECTRIGGEREFERENCESCONSTRAINTOFFSETEMPORARY */
151383/* UNIQUERYWITHOUTERELEASEATTACHAVINGROUPDATEBEGINNERANGEBETWEEN */
151384/* OTHINGLOBYCASCADELETECASECOLLATECREATECURRENT_DATEDETACH */
151385/* IMMEDIATEJOINSERTLIKEMATCHPLANALYZEPRAGMABORTVALUESVIRTUALIMIT */
151386/* WHENOTNULLWHERECURSIVEAFTERENAMEANDEFAULTAUTOINCREMENTCAST */
151387/* COLUMNCOMMITCONFLICTCROSSCURRENT_TIMESTAMPARTITIONDEFERRED */
151388/* ISTINCTDROPRECEDINGFAILFILTEREPLACEFOLLOWINGFROMFULLIFISNULL */
151389/* ORDERESTRICTOVERIGHTROLLBACKROWSUNBOUNDEDUNIONUSINGVACUUMVIEW */
151390/* INDOWINITIALLYPRIMARY */
151391static const char zKWText[613] = {
151392 'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H',
151393 'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G',
151394 'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A',
151395 'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F',
151396 'E','R','R','A','B','L','E','L','S','E','X','C','E','P','T','R','A','N',
151397 'S','A','C','T','I','O','N','A','T','U','R','A','L','T','E','R','A','I',
151398 'S','E','X','C','L','U','S','I','V','E','X','I','S','T','S','A','V','E',
151399 'P','O','I','N','T','E','R','S','E','C','T','R','I','G','G','E','R','E',
151400 'F','E','R','E','N','C','E','S','C','O','N','S','T','R','A','I','N','T',
151401 'O','F','F','S','E','T','E','M','P','O','R','A','R','Y','U','N','I','Q',
151402 'U','E','R','Y','W','I','T','H','O','U','T','E','R','E','L','E','A','S',
151403 'E','A','T','T','A','C','H','A','V','I','N','G','R','O','U','P','D','A',
151404 'T','E','B','E','G','I','N','N','E','R','A','N','G','E','B','E','T','W',
151405 'E','E','N','O','T','H','I','N','G','L','O','B','Y','C','A','S','C','A',
151406 'D','E','L','E','T','E','C','A','S','E','C','O','L','L','A','T','E','C',
151407 'R','E','A','T','E','C','U','R','R','E','N','T','_','D','A','T','E','D',
151408 'E','T','A','C','H','I','M','M','E','D','I','A','T','E','J','O','I','N',
151409 'S','E','R','T','L','I','K','E','M','A','T','C','H','P','L','A','N','A',
151410 'L','Y','Z','E','P','R','A','G','M','A','B','O','R','T','V','A','L','U',
151411 'E','S','V','I','R','T','U','A','L','I','M','I','T','W','H','E','N','O',
151412 'T','N','U','L','L','W','H','E','R','E','C','U','R','S','I','V','E','A',
151413 'F','T','E','R','E','N','A','M','E','A','N','D','E','F','A','U','L','T',
151414 'A','U','T','O','I','N','C','R','E','M','E','N','T','C','A','S','T','C',
151415 'O','L','U','M','N','C','O','M','M','I','T','C','O','N','F','L','I','C',
151416 'T','C','R','O','S','S','C','U','R','R','E','N','T','_','T','I','M','E',
151417 'S','T','A','M','P','A','R','T','I','T','I','O','N','D','E','F','E','R',
151418 'R','E','D','I','S','T','I','N','C','T','D','R','O','P','R','E','C','E',
151419 'D','I','N','G','F','A','I','L','F','I','L','T','E','R','E','P','L','A',
151420 'C','E','F','O','L','L','O','W','I','N','G','F','R','O','M','F','U','L',
151421 'L','I','F','I','S','N','U','L','L','O','R','D','E','R','E','S','T','R',
151422 'I','C','T','O','V','E','R','I','G','H','T','R','O','L','L','B','A','C',
151423 'K','R','O','W','S','U','N','B','O','U','N','D','E','D','U','N','I','O',
151424 'N','U','S','I','N','G','V','A','C','U','U','M','V','I','E','W','I','N',
151425 'D','O','W','I','N','I','T','I','A','L','L','Y','P','R','I','M','A','R',
151426 'Y',
151427};
151428/* aKWHash[i] is the hash value for the i-th keyword */
151429static const unsigned char aKWHash[127] = {
151430 74, 109, 124, 72, 106, 45, 0, 0, 81, 0, 76, 61, 0,
151431 42, 12, 77, 15, 0, 123, 84, 54, 118, 125, 19, 0, 0,
151432 130, 0, 128, 121, 0, 22, 96, 0, 9, 0, 0, 115, 69,
151433 0, 67, 6, 0, 48, 93, 136, 0, 126, 104, 0, 0, 44,
151434 0, 107, 24, 0, 17, 0, 131, 53, 23, 0, 5, 62, 132,
151435 99, 0, 0, 135, 110, 60, 134, 57, 113, 55, 0, 94, 0,
151436 103, 26, 0, 102, 0, 0, 0, 98, 95, 100, 105, 117, 14,
151437 39, 116, 0, 80, 0, 133, 114, 92, 59, 0, 129, 79, 119,
151438 86, 46, 83, 0, 0, 97, 40, 122, 120, 0, 127, 0, 0,
151439 29, 0, 89, 87, 88, 0, 20, 85, 111, 56,
151440};
151441/* aKWNext[] forms the hash collision chain. If aKWHash[i]==0
151442** then the i-th keyword has no more hash collisions. Otherwise,
151443** the next keyword with the same hash is aKWHash[i]-1. */
151444static const unsigned char aKWNext[136] = {
151445 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0,
151446 0, 2, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0,
151447 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
151448 0, 0, 0, 0, 33, 0, 21, 0, 0, 0, 0, 0, 50,
151449 0, 43, 3, 47, 0, 0, 32, 0, 0, 0, 0, 0, 0,
151450 0, 1, 64, 0, 0, 65, 0, 41, 0, 38, 0, 0, 0,
151451 0, 0, 49, 75, 0, 0, 30, 0, 58, 0, 0, 0, 31,
151452 63, 16, 34, 10, 0, 0, 0, 0, 0, 0, 0, 11, 70,
151453 91, 0, 0, 8, 0, 108, 0, 101, 28, 52, 68, 0, 112,
151454 0, 73, 51, 0, 90, 27, 37, 0, 71, 36, 82, 0, 35,
151455 66, 25, 18, 0, 0, 78,
151456};
151457/* aKWLen[i] is the length (in bytes) of the i-th keyword */
151458static const unsigned char aKWLen[136] = {
151459 7, 7, 5, 4, 6, 4, 5, 3, 6, 7, 3, 6, 6,
151460 7, 7, 3, 8, 2, 6, 5, 4, 4, 3, 10, 4, 6,
151461 11, 6, 2, 7, 5, 5, 9, 6, 9, 9, 7, 10, 10,
151462 4, 6, 2, 3, 9, 4, 2, 6, 5, 7, 4, 5, 7,
151463 6, 6, 5, 6, 5, 5, 5, 7, 7, 4, 2, 7, 3,
151464 6, 4, 7, 6, 12, 6, 9, 4, 6, 4, 5, 4, 7,
151465 6, 5, 6, 7, 5, 4, 7, 3, 2, 4, 5, 9, 5,
151466 6, 3, 7, 13, 2, 2, 4, 6, 6, 8, 5, 17, 12,
151467 7, 9, 8, 8, 2, 4, 9, 4, 6, 7, 9, 4, 4,
151468 2, 6, 5, 8, 4, 5, 8, 4, 3, 9, 5, 5, 6,
151469 4, 6, 2, 9, 3, 7,
151470};
151471/* aKWOffset[i] is the index into zKWText[] of the start of
151472** the text for the i-th keyword. */
151473static const unsigned short int aKWOffset[136] = {
151474 0, 2, 2, 8, 9, 14, 16, 20, 23, 25, 25, 29, 33,
151475 36, 41, 46, 48, 53, 54, 59, 62, 65, 67, 69, 78, 81,
151476 86, 91, 95, 96, 101, 105, 109, 117, 122, 128, 136, 142, 152,
151477 159, 162, 162, 165, 167, 167, 171, 176, 179, 184, 184, 188, 192,
151478 199, 204, 209, 212, 218, 221, 225, 230, 236, 242, 245, 247, 248,
151479 252, 258, 262, 269, 275, 287, 293, 302, 304, 310, 314, 319, 321,
151480 328, 333, 338, 344, 350, 355, 358, 358, 358, 361, 365, 368, 377,
151481 381, 387, 389, 396, 398, 400, 409, 413, 419, 425, 433, 438, 438,
151482 438, 454, 463, 470, 471, 478, 481, 490, 494, 499, 506, 515, 519,
151483 523, 525, 531, 535, 543, 546, 551, 559, 559, 563, 572, 577, 582,
151484 588, 591, 594, 597, 602, 606,
151485};
151486/* aKWCode[i] is the parser symbol code for the i-th keyword */
151487static const unsigned char aKWCode[136] = {
151488 TK_REINDEX, TK_INDEXED, TK_INDEX, TK_DESC, TK_ESCAPE,
151489 TK_EACH, TK_CHECK, TK_KEY, TK_BEFORE, TK_FOREIGN,
151490 TK_FOR, TK_IGNORE, TK_LIKE_KW, TK_EXPLAIN, TK_INSTEAD,
151491 TK_ADD, TK_DATABASE, TK_AS, TK_SELECT, TK_TABLE,
151492 TK_JOIN_KW, TK_THEN, TK_END, TK_DEFERRABLE, TK_ELSE,
151493 TK_EXCEPT, TK_TRANSACTION,TK_ACTION, TK_ON, TK_JOIN_KW,
151494 TK_ALTER, TK_RAISE, TK_EXCLUSIVE, TK_EXISTS, TK_SAVEPOINT,
151495 TK_INTERSECT, TK_TRIGGER, TK_REFERENCES, TK_CONSTRAINT, TK_INTO,
151496 TK_OFFSET, TK_OF, TK_SET, TK_TEMP, TK_TEMP,
151497 TK_OR, TK_UNIQUE, TK_QUERY, TK_WITHOUT, TK_WITH,
151498 TK_JOIN_KW, TK_RELEASE, TK_ATTACH, TK_HAVING, TK_GROUP,
151499 TK_UPDATE, TK_BEGIN, TK_JOIN_KW, TK_RANGE, TK_BETWEEN,
151500 TK_NOTHING, TK_LIKE_KW, TK_BY, TK_CASCADE, TK_ASC,
151501 TK_DELETE, TK_CASE, TK_COLLATE, TK_CREATE, TK_CTIME_KW,
151502 TK_DETACH, TK_IMMEDIATE, TK_JOIN, TK_INSERT, TK_LIKE_KW,
151503 TK_MATCH, TK_PLAN, TK_ANALYZE, TK_PRAGMA, TK_ABORT,
151504 TK_VALUES, TK_VIRTUAL, TK_LIMIT, TK_WHEN, TK_NOTNULL,
151505 TK_NOT, TK_NO, TK_NULL, TK_WHERE, TK_RECURSIVE,
151506 TK_AFTER, TK_RENAME, TK_AND, TK_DEFAULT, TK_AUTOINCR,
151507 TK_TO, TK_IN, TK_CAST, TK_COLUMNKW, TK_COMMIT,
151508 TK_CONFLICT, TK_JOIN_KW, TK_CTIME_KW, TK_CTIME_KW, TK_CURRENT,
151509 TK_PARTITION, TK_DEFERRED, TK_DISTINCT, TK_IS, TK_DROP,
151510 TK_PRECEDING, TK_FAIL, TK_FILTER, TK_REPLACE, TK_FOLLOWING,
151511 TK_FROM, TK_JOIN_KW, TK_IF, TK_ISNULL, TK_ORDER,
151512 TK_RESTRICT, TK_OVER, TK_JOIN_KW, TK_ROLLBACK, TK_ROWS,
151513 TK_ROW, TK_UNBOUNDED, TK_UNION, TK_USING, TK_VACUUM,
151514 TK_VIEW, TK_WINDOW, TK_DO, TK_INITIALLY, TK_ALL,
151515 TK_PRIMARY,
151516};
151517/* Check to see if z[0..n-1] is a keyword. If it is, write the
151518** parser symbol code for that keyword into *pType. Always
151519** return the integer n (the length of the token). */
151520static int keywordCode(const char *z, int n, int *pType){
151521 int i, j;
151522 const char *zKW;
151523 if( n>=2 ){
151524 i = ((charMap(z[0])*4) ^ (charMap(z[n-1])*3) ^ n) % 127;
151525 for(i=((int)aKWHash[i])-1; i>=0; i=((int)aKWNext[i])-1){
151526 if( aKWLen[i]!=n ) continue;
151527 j = 0;
151528 zKW = &zKWText[aKWOffset[i]];
151529#ifdef SQLITE_ASCII
151530 while( j<n && (z[j]&~0x20)==zKW[j] ){ j++; }
151531#endif
151532#ifdef SQLITE_EBCDIC
151533 while( j<n && toupper(z[j])==zKW[j] ){ j++; }
151534#endif
151535 if( j<n ) continue;
151536 testcase( i==0 ); /* REINDEX */
151537 testcase( i==1 ); /* INDEXED */
151538 testcase( i==2 ); /* INDEX */
151539 testcase( i==3 ); /* DESC */
151540 testcase( i==4 ); /* ESCAPE */
151541 testcase( i==5 ); /* EACH */
151542 testcase( i==6 ); /* CHECK */
151543 testcase( i==7 ); /* KEY */
151544 testcase( i==8 ); /* BEFORE */
151545 testcase( i==9 ); /* FOREIGN */
151546 testcase( i==10 ); /* FOR */
151547 testcase( i==11 ); /* IGNORE */
151548 testcase( i==12 ); /* REGEXP */
151549 testcase( i==13 ); /* EXPLAIN */
151550 testcase( i==14 ); /* INSTEAD */
151551 testcase( i==15 ); /* ADD */
151552 testcase( i==16 ); /* DATABASE */
151553 testcase( i==17 ); /* AS */
151554 testcase( i==18 ); /* SELECT */
151555 testcase( i==19 ); /* TABLE */
151556 testcase( i==20 ); /* LEFT */
151557 testcase( i==21 ); /* THEN */
151558 testcase( i==22 ); /* END */
151559 testcase( i==23 ); /* DEFERRABLE */
151560 testcase( i==24 ); /* ELSE */
151561 testcase( i==25 ); /* EXCEPT */
151562 testcase( i==26 ); /* TRANSACTION */
151563 testcase( i==27 ); /* ACTION */
151564 testcase( i==28 ); /* ON */
151565 testcase( i==29 ); /* NATURAL */
151566 testcase( i==30 ); /* ALTER */
151567 testcase( i==31 ); /* RAISE */
151568 testcase( i==32 ); /* EXCLUSIVE */
151569 testcase( i==33 ); /* EXISTS */
151570 testcase( i==34 ); /* SAVEPOINT */
151571 testcase( i==35 ); /* INTERSECT */
151572 testcase( i==36 ); /* TRIGGER */
151573 testcase( i==37 ); /* REFERENCES */
151574 testcase( i==38 ); /* CONSTRAINT */
151575 testcase( i==39 ); /* INTO */
151576 testcase( i==40 ); /* OFFSET */
151577 testcase( i==41 ); /* OF */
151578 testcase( i==42 ); /* SET */
151579 testcase( i==43 ); /* TEMPORARY */
151580 testcase( i==44 ); /* TEMP */
151581 testcase( i==45 ); /* OR */
151582 testcase( i==46 ); /* UNIQUE */
151583 testcase( i==47 ); /* QUERY */
151584 testcase( i==48 ); /* WITHOUT */
151585 testcase( i==49 ); /* WITH */
151586 testcase( i==50 ); /* OUTER */
151587 testcase( i==51 ); /* RELEASE */
151588 testcase( i==52 ); /* ATTACH */
151589 testcase( i==53 ); /* HAVING */
151590 testcase( i==54 ); /* GROUP */
151591 testcase( i==55 ); /* UPDATE */
151592 testcase( i==56 ); /* BEGIN */
151593 testcase( i==57 ); /* INNER */
151594 testcase( i==58 ); /* RANGE */
151595 testcase( i==59 ); /* BETWEEN */
151596 testcase( i==60 ); /* NOTHING */
151597 testcase( i==61 ); /* GLOB */
151598 testcase( i==62 ); /* BY */
151599 testcase( i==63 ); /* CASCADE */
151600 testcase( i==64 ); /* ASC */
151601 testcase( i==65 ); /* DELETE */
151602 testcase( i==66 ); /* CASE */
151603 testcase( i==67 ); /* COLLATE */
151604 testcase( i==68 ); /* CREATE */
151605 testcase( i==69 ); /* CURRENT_DATE */
151606 testcase( i==70 ); /* DETACH */
151607 testcase( i==71 ); /* IMMEDIATE */
151608 testcase( i==72 ); /* JOIN */
151609 testcase( i==73 ); /* INSERT */
151610 testcase( i==74 ); /* LIKE */
151611 testcase( i==75 ); /* MATCH */
151612 testcase( i==76 ); /* PLAN */
151613 testcase( i==77 ); /* ANALYZE */
151614 testcase( i==78 ); /* PRAGMA */
151615 testcase( i==79 ); /* ABORT */
151616 testcase( i==80 ); /* VALUES */
151617 testcase( i==81 ); /* VIRTUAL */
151618 testcase( i==82 ); /* LIMIT */
151619 testcase( i==83 ); /* WHEN */
151620 testcase( i==84 ); /* NOTNULL */
151621 testcase( i==85 ); /* NOT */
151622 testcase( i==86 ); /* NO */
151623 testcase( i==87 ); /* NULL */
151624 testcase( i==88 ); /* WHERE */
151625 testcase( i==89 ); /* RECURSIVE */
151626 testcase( i==90 ); /* AFTER */
151627 testcase( i==91 ); /* RENAME */
151628 testcase( i==92 ); /* AND */
151629 testcase( i==93 ); /* DEFAULT */
151630 testcase( i==94 ); /* AUTOINCREMENT */
151631 testcase( i==95 ); /* TO */
151632 testcase( i==96 ); /* IN */
151633 testcase( i==97 ); /* CAST */
151634 testcase( i==98 ); /* COLUMN */
151635 testcase( i==99 ); /* COMMIT */
151636 testcase( i==100 ); /* CONFLICT */
151637 testcase( i==101 ); /* CROSS */
151638 testcase( i==102 ); /* CURRENT_TIMESTAMP */
151639 testcase( i==103 ); /* CURRENT_TIME */
151640 testcase( i==104 ); /* CURRENT */
151641 testcase( i==105 ); /* PARTITION */
151642 testcase( i==106 ); /* DEFERRED */
151643 testcase( i==107 ); /* DISTINCT */
151644 testcase( i==108 ); /* IS */
151645 testcase( i==109 ); /* DROP */
151646 testcase( i==110 ); /* PRECEDING */
151647 testcase( i==111 ); /* FAIL */
151648 testcase( i==112 ); /* FILTER */
151649 testcase( i==113 ); /* REPLACE */
151650 testcase( i==114 ); /* FOLLOWING */
151651 testcase( i==115 ); /* FROM */
151652 testcase( i==116 ); /* FULL */
151653 testcase( i==117 ); /* IF */
151654 testcase( i==118 ); /* ISNULL */
151655 testcase( i==119 ); /* ORDER */
151656 testcase( i==120 ); /* RESTRICT */
151657 testcase( i==121 ); /* OVER */
151658 testcase( i==122 ); /* RIGHT */
151659 testcase( i==123 ); /* ROLLBACK */
151660 testcase( i==124 ); /* ROWS */
151661 testcase( i==125 ); /* ROW */
151662 testcase( i==126 ); /* UNBOUNDED */
151663 testcase( i==127 ); /* UNION */
151664 testcase( i==128 ); /* USING */
151665 testcase( i==129 ); /* VACUUM */
151666 testcase( i==130 ); /* VIEW */
151667 testcase( i==131 ); /* WINDOW */
151668 testcase( i==132 ); /* DO */
151669 testcase( i==133 ); /* INITIALLY */
151670 testcase( i==134 ); /* ALL */
151671 testcase( i==135 ); /* PRIMARY */
151672 *pType = aKWCode[i];
151673 break;
151674 }
151675 }
151676 return n;
151677}
151678SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char *z, int n){
151679 int id = TK_ID;
151680 keywordCode((char*)z, n, &id);
151681 return id;
151682}
151683#define SQLITE_N_KEYWORD 136
151684SQLITE_API int sqlite3_keyword_name(int i,const char **pzName,int *pnName){
151685 if( i<0 || i>=SQLITE_N_KEYWORD ) return SQLITE_ERROR;
151686 *pzName = zKWText + aKWOffset[i];
151687 *pnName = aKWLen[i];
151688 return SQLITE_OK;
151689}
151690SQLITE_API int sqlite3_keyword_count(void){ return SQLITE_N_KEYWORD; }
151691SQLITE_API int sqlite3_keyword_check(const char *zName, int nName){
151692 return TK_ID!=sqlite3KeywordCode((const u8*)zName, nName);
151693}
151694
151695/************** End of keywordhash.h *****************************************/
151696/************** Continuing where we left off in tokenize.c *******************/
151697
151698
151699/*
151700** If X is a character that can be used in an identifier then
151701** IdChar(X) will be true. Otherwise it is false.
151702**
151703** For ASCII, any character with the high-order bit set is
151704** allowed in an identifier. For 7-bit characters,
151705** sqlite3IsIdChar[X] must be 1.
151706**
151707** For EBCDIC, the rules are more complex but have the same
151708** end result.
151709**
151710** Ticket #1066. the SQL standard does not allow '$' in the
151711** middle of identifiers. But many SQL implementations do.
151712** SQLite will allow '$' in identifiers for compatibility.
151713** But the feature is undocumented.
151714*/
151715#ifdef SQLITE_ASCII
151716#define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
151717#endif
151718#ifdef SQLITE_EBCDIC
151719SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[] = {
151720/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
151721 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */
151722 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */
151723 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */
151724 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */
151725 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */
151726 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */
151727 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */
151728 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
151729 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */
151730 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */
151731 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
151732 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
151733};
151734#define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
151735#endif
151736
151737/* Make the IdChar function accessible from ctime.c and alter.c */
151738SQLITE_PRIVATE int sqlite3IsIdChar(u8 c){ return IdChar(c); }
151739
151740#ifndef SQLITE_OMIT_WINDOWFUNC
151741/*
151742** Return the id of the next token in string (*pz). Before returning, set
151743** (*pz) to point to the byte following the parsed token.
151744*/
151745static int getToken(const unsigned char **pz){
151746 const unsigned char *z = *pz;
151747 int t; /* Token type to return */
151748 do {
151749 z += sqlite3GetToken(z, &t);
151750 }while( t==TK_SPACE );
151751 if( t==TK_ID
151752 || t==TK_STRING
151753 || t==TK_JOIN_KW
151754 || t==TK_WINDOW
151755 || t==TK_OVER
151756 || sqlite3ParserFallback(t)==TK_ID
151757 ){
151758 t = TK_ID;
151759 }
151760 *pz = z;
151761 return t;
151762}
151763
151764/*
151765** The following three functions are called immediately after the tokenizer
151766** reads the keywords WINDOW, OVER and FILTER, respectively, to determine
151767** whether the token should be treated as a keyword or an SQL identifier.
151768** This cannot be handled by the usual lemon %fallback method, due to
151769** the ambiguity in some constructions. e.g.
151770**
151771** SELECT sum(x) OVER ...
151772**
151773** In the above, "OVER" might be a keyword, or it might be an alias for the
151774** sum(x) expression. If a "%fallback ID OVER" directive were added to
151775** grammar, then SQLite would always treat "OVER" as an alias, making it
151776** impossible to call a window-function without a FILTER clause.
151777**
151778** WINDOW is treated as a keyword if:
151779**
151780** * the following token is an identifier, or a keyword that can fallback
151781** to being an identifier, and
151782** * the token after than one is TK_AS.
151783**
151784** OVER is a keyword if:
151785**
151786** * the previous token was TK_RP, and
151787** * the next token is either TK_LP or an identifier.
151788**
151789** FILTER is a keyword if:
151790**
151791** * the previous token was TK_RP, and
151792** * the next token is TK_LP.
151793*/
151794static int analyzeWindowKeyword(const unsigned char *z){
151795 int t;
151796 t = getToken(&z);
151797 if( t!=TK_ID ) return TK_ID;
151798 t = getToken(&z);
151799 if( t!=TK_AS ) return TK_ID;
151800 return TK_WINDOW;
151801}
151802static int analyzeOverKeyword(const unsigned char *z, int lastToken){
151803 if( lastToken==TK_RP ){
151804 int t = getToken(&z);
151805 if( t==TK_LP || t==TK_ID ) return TK_OVER;
151806 }
151807 return TK_ID;
151808}
151809static int analyzeFilterKeyword(const unsigned char *z, int lastToken){
151810 if( lastToken==TK_RP && getToken(&z)==TK_LP ){
151811 return TK_FILTER;
151812 }
151813 return TK_ID;
151814}
151815#endif /* SQLITE_OMIT_WINDOWFUNC */
151816
151817/*
151818** Return the length (in bytes) of the token that begins at z[0].
151819** Store the token type in *tokenType before returning.
151820*/
151821SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
151822 int i, c;
151823 switch( aiClass[*z] ){ /* Switch on the character-class of the first byte
151824 ** of the token. See the comment on the CC_ defines
151825 ** above. */
151826 case CC_SPACE: {
151827 testcase( z[0]==' ' );
151828 testcase( z[0]=='\t' );
151829 testcase( z[0]=='\n' );
151830 testcase( z[0]=='\f' );
151831 testcase( z[0]=='\r' );
151832 for(i=1; sqlite3Isspace(z[i]); i++){}
151833 *tokenType = TK_SPACE;
151834 return i;
151835 }
151836 case CC_MINUS: {
151837 if( z[1]=='-' ){
151838 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
151839 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
151840 return i;
151841 }
151842 *tokenType = TK_MINUS;
151843 return 1;
151844 }
151845 case CC_LP: {
151846 *tokenType = TK_LP;
151847 return 1;
151848 }
151849 case CC_RP: {
151850 *tokenType = TK_RP;
151851 return 1;
151852 }
151853 case CC_SEMI: {
151854 *tokenType = TK_SEMI;
151855 return 1;
151856 }
151857 case CC_PLUS: {
151858 *tokenType = TK_PLUS;
151859 return 1;
151860 }
151861 case CC_STAR: {
151862 *tokenType = TK_STAR;
151863 return 1;
151864 }
151865 case CC_SLASH: {
151866 if( z[1]!='*' || z[2]==0 ){
151867 *tokenType = TK_SLASH;
151868 return 1;
151869 }
151870 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
151871 if( c ) i++;
151872 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
151873 return i;
151874 }
151875 case CC_PERCENT: {
151876 *tokenType = TK_REM;
151877 return 1;
151878 }
151879 case CC_EQ: {
151880 *tokenType = TK_EQ;
151881 return 1 + (z[1]=='=');
151882 }
151883 case CC_LT: {
151884 if( (c=z[1])=='=' ){
151885 *tokenType = TK_LE;
151886 return 2;
151887 }else if( c=='>' ){
151888 *tokenType = TK_NE;
151889 return 2;
151890 }else if( c=='<' ){
151891 *tokenType = TK_LSHIFT;
151892 return 2;
151893 }else{
151894 *tokenType = TK_LT;
151895 return 1;
151896 }
151897 }
151898 case CC_GT: {
151899 if( (c=z[1])=='=' ){
151900 *tokenType = TK_GE;
151901 return 2;
151902 }else if( c=='>' ){
151903 *tokenType = TK_RSHIFT;
151904 return 2;
151905 }else{
151906 *tokenType = TK_GT;
151907 return 1;
151908 }
151909 }
151910 case CC_BANG: {
151911 if( z[1]!='=' ){
151912 *tokenType = TK_ILLEGAL;
151913 return 1;
151914 }else{
151915 *tokenType = TK_NE;
151916 return 2;
151917 }
151918 }
151919 case CC_PIPE: {
151920 if( z[1]!='|' ){
151921 *tokenType = TK_BITOR;
151922 return 1;
151923 }else{
151924 *tokenType = TK_CONCAT;
151925 return 2;
151926 }
151927 }
151928 case CC_COMMA: {
151929 *tokenType = TK_COMMA;
151930 return 1;
151931 }
151932 case CC_AND: {
151933 *tokenType = TK_BITAND;
151934 return 1;
151935 }
151936 case CC_TILDA: {
151937 *tokenType = TK_BITNOT;
151938 return 1;
151939 }
151940 case CC_QUOTE: {
151941 int delim = z[0];
151942 testcase( delim=='`' );
151943 testcase( delim=='\'' );
151944 testcase( delim=='"' );
151945 for(i=1; (c=z[i])!=0; i++){
151946 if( c==delim ){
151947 if( z[i+1]==delim ){
151948 i++;
151949 }else{
151950 break;
151951 }
151952 }
151953 }
151954 if( c=='\'' ){
151955 *tokenType = TK_STRING;
151956 return i+1;
151957 }else if( c!=0 ){
151958 *tokenType = TK_ID;
151959 return i+1;
151960 }else{
151961 *tokenType = TK_ILLEGAL;
151962 return i;
151963 }
151964 }
151965 case CC_DOT: {
151966#ifndef SQLITE_OMIT_FLOATING_POINT
151967 if( !sqlite3Isdigit(z[1]) )
151968#endif
151969 {
151970 *tokenType = TK_DOT;
151971 return 1;
151972 }
151973 /* If the next character is a digit, this is a floating point
151974 ** number that begins with ".". Fall thru into the next case */
151975 }
151976 case CC_DIGIT: {
151977 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
151978 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
151979 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
151980 testcase( z[0]=='9' );
151981 *tokenType = TK_INTEGER;
151982#ifndef SQLITE_OMIT_HEX_INTEGER
151983 if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){
151984 for(i=3; sqlite3Isxdigit(z[i]); i++){}
151985 return i;
151986 }
151987#endif
151988 for(i=0; sqlite3Isdigit(z[i]); i++){}
151989#ifndef SQLITE_OMIT_FLOATING_POINT
151990 if( z[i]=='.' ){
151991 i++;
151992 while( sqlite3Isdigit(z[i]) ){ i++; }
151993 *tokenType = TK_FLOAT;
151994 }
151995 if( (z[i]=='e' || z[i]=='E') &&
151996 ( sqlite3Isdigit(z[i+1])
151997 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
151998 )
151999 ){
152000 i += 2;
152001 while( sqlite3Isdigit(z[i]) ){ i++; }
152002 *tokenType = TK_FLOAT;
152003 }
152004#endif
152005 while( IdChar(z[i]) ){
152006 *tokenType = TK_ILLEGAL;
152007 i++;
152008 }
152009 return i;
152010 }
152011 case CC_QUOTE2: {
152012 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
152013 *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
152014 return i;
152015 }
152016 case CC_VARNUM: {
152017 *tokenType = TK_VARIABLE;
152018 for(i=1; sqlite3Isdigit(z[i]); i++){}
152019 return i;
152020 }
152021 case CC_DOLLAR:
152022 case CC_VARALPHA: {
152023 int n = 0;
152024 testcase( z[0]=='$' ); testcase( z[0]=='@' );
152025 testcase( z[0]==':' ); testcase( z[0]=='#' );
152026 *tokenType = TK_VARIABLE;
152027 for(i=1; (c=z[i])!=0; i++){
152028 if( IdChar(c) ){
152029 n++;
152030#ifndef SQLITE_OMIT_TCL_VARIABLE
152031 }else if( c=='(' && n>0 ){
152032 do{
152033 i++;
152034 }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
152035 if( c==')' ){
152036 i++;
152037 }else{
152038 *tokenType = TK_ILLEGAL;
152039 }
152040 break;
152041 }else if( c==':' && z[i+1]==':' ){
152042 i++;
152043#endif
152044 }else{
152045 break;
152046 }
152047 }
152048 if( n==0 ) *tokenType = TK_ILLEGAL;
152049 return i;
152050 }
152051 case CC_KYWD: {
152052 for(i=1; aiClass[z[i]]<=CC_KYWD; i++){}
152053 if( IdChar(z[i]) ){
152054 /* This token started out using characters that can appear in keywords,
152055 ** but z[i] is a character not allowed within keywords, so this must
152056 ** be an identifier instead */
152057 i++;
152058 break;
152059 }
152060 *tokenType = TK_ID;
152061 return keywordCode((char*)z, i, tokenType);
152062 }
152063 case CC_X: {
152064#ifndef SQLITE_OMIT_BLOB_LITERAL
152065 testcase( z[0]=='x' ); testcase( z[0]=='X' );
152066 if( z[1]=='\'' ){
152067 *tokenType = TK_BLOB;
152068 for(i=2; sqlite3Isxdigit(z[i]); i++){}
152069 if( z[i]!='\'' || i%2 ){
152070 *tokenType = TK_ILLEGAL;
152071 while( z[i] && z[i]!='\'' ){ i++; }
152072 }
152073 if( z[i] ) i++;
152074 return i;
152075 }
152076#endif
152077 /* If it is not a BLOB literal, then it must be an ID, since no
152078 ** SQL keywords start with the letter 'x'. Fall through */
152079 }
152080 case CC_ID: {
152081 i = 1;
152082 break;
152083 }
152084 case CC_NUL: {
152085 *tokenType = TK_ILLEGAL;
152086 return 0;
152087 }
152088 default: {
152089 *tokenType = TK_ILLEGAL;
152090 return 1;
152091 }
152092 }
152093 while( IdChar(z[i]) ){ i++; }
152094 *tokenType = TK_ID;
152095 return i;
152096}
152097
152098#ifdef SQLITE_ENABLE_NORMALIZE
152099/*
152100** Return the length (in bytes) of the token that begins at z[0].
152101** Store the token type in *tokenType before returning. If flags has
152102** SQLITE_TOKEN_NORMALIZE flag enabled, use the identifier token type
152103** for keywords. Add SQLITE_TOKEN_QUOTED to flags if the token was
152104** actually a quoted identifier. Add SQLITE_TOKEN_KEYWORD to flags
152105** if the token was recognized as a keyword; this is useful when the
152106** SQLITE_TOKEN_NORMALIZE flag is used, because it enables the caller
152107** to differentiate between a keyword being treated as an identifier
152108** (for normalization purposes) and an actual identifier.
152109*/
152110SQLITE_PRIVATE int sqlite3GetTokenNormalized(
152111 const unsigned char *z,
152112 int *tokenType,
152113 int *flags
152114){
152115 int n;
152116 unsigned char iClass = aiClass[*z];
152117 if( iClass==CC_KYWD ){
152118 int i;
152119 for(i=1; aiClass[z[i]]<=CC_KYWD; i++){}
152120 if( IdChar(z[i]) ){
152121 /* This token started out using characters that can appear in keywords,
152122 ** but z[i] is a character not allowed within keywords, so this must
152123 ** be an identifier instead */
152124 i++;
152125 while( IdChar(z[i]) ){ i++; }
152126 *tokenType = TK_ID;
152127 return i;
152128 }
152129 *tokenType = TK_ID;
152130 n = keywordCode((char*)z, i, tokenType);
152131 /* If the token is no longer considered to be an identifier, then it is a
152132 ** keyword of some kind. Make the token back into an identifier and then
152133 ** set the SQLITE_TOKEN_KEYWORD flag. Several non-identifier tokens are
152134 ** used verbatim, including IN, IS, NOT, and NULL. */
152135 switch( *tokenType ){
152136 case TK_ID: {
152137 /* do nothing, handled by caller */
152138 break;
152139 }
152140 case TK_IN:
152141 case TK_IS:
152142 case TK_NOT:
152143 case TK_NULL: {
152144 *flags |= SQLITE_TOKEN_KEYWORD;
152145 break;
152146 }
152147 default: {
152148 *tokenType = TK_ID;
152149 *flags |= SQLITE_TOKEN_KEYWORD;
152150 break;
152151 }
152152 }
152153 }else{
152154 n = sqlite3GetToken(z, tokenType);
152155 /* If the token is considered to be an identifier and the character class
152156 ** of the first character is a quote, set the SQLITE_TOKEN_QUOTED flag. */
152157 if( *tokenType==TK_ID && (iClass==CC_QUOTE || iClass==CC_QUOTE2) ){
152158 *flags |= SQLITE_TOKEN_QUOTED;
152159 }
152160 }
152161 return n;
152162}
152163#endif /* SQLITE_ENABLE_NORMALIZE */
152164
152165/*
152166** Run the parser on the given SQL string. The parser structure is
152167** passed in. An SQLITE_ status code is returned. If an error occurs
152168** then an and attempt is made to write an error message into
152169** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
152170** error message.
152171*/
152172SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
152173 int nErr = 0; /* Number of errors encountered */
152174 void *pEngine; /* The LEMON-generated LALR(1) parser */
152175 int n = 0; /* Length of the next token token */
152176 int tokenType; /* type of the next token */
152177 int lastTokenParsed = -1; /* type of the previous token */
152178 sqlite3 *db = pParse->db; /* The database connection */
152179 int mxSqlLen; /* Max length of an SQL string */
152180#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
152181 yyParser sEngine; /* Space to hold the Lemon-generated Parser object */
152182#endif
152183
152184 assert( zSql!=0 );
152185 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
152186 if( db->nVdbeActive==0 ){
152187 db->u1.isInterrupted = 0;
152188 }
152189 pParse->rc = SQLITE_OK;
152190 pParse->zTail = zSql;
152191 assert( pzErrMsg!=0 );
152192 /* sqlite3ParserTrace(stdout, "parser: "); */
152193#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
152194 pEngine = &sEngine;
152195 sqlite3ParserInit(pEngine, pParse);
152196#else
152197 pEngine = sqlite3ParserAlloc(sqlite3Malloc, pParse);
152198 if( pEngine==0 ){
152199 sqlite3OomFault(db);
152200 return SQLITE_NOMEM_BKPT;
152201 }
152202#endif
152203 assert( pParse->pNewTable==0 );
152204 assert( pParse->pNewTrigger==0 );
152205 assert( pParse->nVar==0 );
152206 assert( pParse->pVList==0 );
152207 while( 1 ){
152208 n = sqlite3GetToken((u8*)zSql, &tokenType);
152209 mxSqlLen -= n;
152210 if( mxSqlLen<0 ){
152211 pParse->rc = SQLITE_TOOBIG;
152212 break;
152213 }
152214#ifndef SQLITE_OMIT_WINDOWFUNC
152215 if( tokenType>=TK_WINDOW ){
152216 assert( tokenType==TK_SPACE || tokenType==TK_OVER || tokenType==TK_FILTER
152217 || tokenType==TK_ILLEGAL || tokenType==TK_WINDOW
152218 );
152219#else
152220 if( tokenType>=TK_SPACE ){
152221 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
152222#endif /* SQLITE_OMIT_WINDOWFUNC */
152223 if( db->u1.isInterrupted ){
152224 pParse->rc = SQLITE_INTERRUPT;
152225 break;
152226 }
152227 if( tokenType==TK_SPACE ){
152228 zSql += n;
152229 continue;
152230 }
152231 if( zSql[0]==0 ){
152232 /* Upon reaching the end of input, call the parser two more times
152233 ** with tokens TK_SEMI and 0, in that order. */
152234 if( lastTokenParsed==TK_SEMI ){
152235 tokenType = 0;
152236 }else if( lastTokenParsed==0 ){
152237 break;
152238 }else{
152239 tokenType = TK_SEMI;
152240 }
152241 n = 0;
152242#ifndef SQLITE_OMIT_WINDOWFUNC
152243 }else if( tokenType==TK_WINDOW ){
152244 assert( n==6 );
152245 tokenType = analyzeWindowKeyword((const u8*)&zSql[6]);
152246 }else if( tokenType==TK_OVER ){
152247 assert( n==4 );
152248 tokenType = analyzeOverKeyword((const u8*)&zSql[4], lastTokenParsed);
152249 }else if( tokenType==TK_FILTER ){
152250 assert( n==6 );
152251 tokenType = analyzeFilterKeyword((const u8*)&zSql[6], lastTokenParsed);
152252#endif /* SQLITE_OMIT_WINDOWFUNC */
152253 }else{
152254 sqlite3ErrorMsg(pParse, "unrecognized token: \"%.*s\"", n, zSql);
152255 break;
152256 }
152257 }
152258 pParse->sLastToken.z = zSql;
152259 pParse->sLastToken.n = n;
152260 sqlite3Parser(pEngine, tokenType, pParse->sLastToken);
152261 lastTokenParsed = tokenType;
152262 zSql += n;
152263 if( pParse->rc!=SQLITE_OK || db->mallocFailed ) break;
152264 }
152265 assert( nErr==0 );
152266#ifdef YYTRACKMAXSTACKDEPTH
152267 sqlite3_mutex_enter(sqlite3MallocMutex());
152268 sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
152269 sqlite3ParserStackPeak(pEngine)
152270 );
152271 sqlite3_mutex_leave(sqlite3MallocMutex());
152272#endif /* YYDEBUG */
152273#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
152274 sqlite3ParserFinalize(pEngine);
152275#else
152276 sqlite3ParserFree(pEngine, sqlite3_free);
152277#endif
152278 if( db->mallocFailed ){
152279 pParse->rc = SQLITE_NOMEM_BKPT;
152280 }
152281 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
152282 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
152283 }
152284 assert( pzErrMsg!=0 );
152285 if( pParse->zErrMsg ){
152286 *pzErrMsg = pParse->zErrMsg;
152287 sqlite3_log(pParse->rc, "%s in \"%s\"",
152288 *pzErrMsg, pParse->zTail);
152289 pParse->zErrMsg = 0;
152290 nErr++;
152291 }
152292 pParse->zTail = zSql;
152293 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
152294 sqlite3VdbeDelete(pParse->pVdbe);
152295 pParse->pVdbe = 0;
152296 }
152297#ifndef SQLITE_OMIT_SHARED_CACHE
152298 if( pParse->nested==0 ){
152299 sqlite3DbFree(db, pParse->aTableLock);
152300 pParse->aTableLock = 0;
152301 pParse->nTableLock = 0;
152302 }
152303#endif
152304#ifndef SQLITE_OMIT_VIRTUALTABLE
152305 sqlite3_free(pParse->apVtabLock);
152306#endif
152307
152308 if( !IN_SPECIAL_PARSE ){
152309 /* If the pParse->declareVtab flag is set, do not delete any table
152310 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
152311 ** will take responsibility for freeing the Table structure.
152312 */
152313 sqlite3DeleteTable(db, pParse->pNewTable);
152314 }
152315 if( !IN_RENAME_OBJECT ){
152316 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
152317 }
152318
152319 if( pParse->pWithToFree ) sqlite3WithDelete(db, pParse->pWithToFree);
152320 sqlite3DbFree(db, pParse->pVList);
152321 while( pParse->pAinc ){
152322 AutoincInfo *p = pParse->pAinc;
152323 pParse->pAinc = p->pNext;
152324 sqlite3DbFreeNN(db, p);
152325 }
152326 while( pParse->pZombieTab ){
152327 Table *p = pParse->pZombieTab;
152328 pParse->pZombieTab = p->pNextZombie;
152329 sqlite3DeleteTable(db, p);
152330 }
152331 assert( nErr==0 || pParse->rc!=SQLITE_OK );
152332 return nErr;
152333}
152334
152335/************** End of tokenize.c ********************************************/
152336/************** Begin file complete.c ****************************************/
152337/*
152338** 2001 September 15
152339**
152340** The author disclaims copyright to this source code. In place of
152341** a legal notice, here is a blessing:
152342**
152343** May you do good and not evil.
152344** May you find forgiveness for yourself and forgive others.
152345** May you share freely, never taking more than you give.
152346**
152347*************************************************************************
152348** An tokenizer for SQL
152349**
152350** This file contains C code that implements the sqlite3_complete() API.
152351** This code used to be part of the tokenizer.c source file. But by
152352** separating it out, the code will be automatically omitted from
152353** static links that do not use it.
152354*/
152355/* #include "sqliteInt.h" */
152356#ifndef SQLITE_OMIT_COMPLETE
152357
152358/*
152359** This is defined in tokenize.c. We just have to import the definition.
152360*/
152361#ifndef SQLITE_AMALGAMATION
152362#ifdef SQLITE_ASCII
152363#define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
152364#endif
152365#ifdef SQLITE_EBCDIC
152366SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[];
152367#define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
152368#endif
152369#endif /* SQLITE_AMALGAMATION */
152370
152371
152372/*
152373** Token types used by the sqlite3_complete() routine. See the header
152374** comments on that procedure for additional information.
152375*/
152376#define tkSEMI 0
152377#define tkWS 1
152378#define tkOTHER 2
152379#ifndef SQLITE_OMIT_TRIGGER
152380#define tkEXPLAIN 3
152381#define tkCREATE 4
152382#define tkTEMP 5
152383#define tkTRIGGER 6
152384#define tkEND 7
152385#endif
152386
152387/*
152388** Return TRUE if the given SQL string ends in a semicolon.
152389**
152390** Special handling is require for CREATE TRIGGER statements.
152391** Whenever the CREATE TRIGGER keywords are seen, the statement
152392** must end with ";END;".
152393**
152394** This implementation uses a state machine with 8 states:
152395**
152396** (0) INVALID We have not yet seen a non-whitespace character.
152397**
152398** (1) START At the beginning or end of an SQL statement. This routine
152399** returns 1 if it ends in the START state and 0 if it ends
152400** in any other state.
152401**
152402** (2) NORMAL We are in the middle of statement which ends with a single
152403** semicolon.
152404**
152405** (3) EXPLAIN The keyword EXPLAIN has been seen at the beginning of
152406** a statement.
152407**
152408** (4) CREATE The keyword CREATE has been seen at the beginning of a
152409** statement, possibly preceded by EXPLAIN and/or followed by
152410** TEMP or TEMPORARY
152411**
152412** (5) TRIGGER We are in the middle of a trigger definition that must be
152413** ended by a semicolon, the keyword END, and another semicolon.
152414**
152415** (6) SEMI We've seen the first semicolon in the ";END;" that occurs at
152416** the end of a trigger definition.
152417**
152418** (7) END We've seen the ";END" of the ";END;" that occurs at the end
152419** of a trigger definition.
152420**
152421** Transitions between states above are determined by tokens extracted
152422** from the input. The following tokens are significant:
152423**
152424** (0) tkSEMI A semicolon.
152425** (1) tkWS Whitespace.
152426** (2) tkOTHER Any other SQL token.
152427** (3) tkEXPLAIN The "explain" keyword.
152428** (4) tkCREATE The "create" keyword.
152429** (5) tkTEMP The "temp" or "temporary" keyword.
152430** (6) tkTRIGGER The "trigger" keyword.
152431** (7) tkEND The "end" keyword.
152432**
152433** Whitespace never causes a state transition and is always ignored.
152434** This means that a SQL string of all whitespace is invalid.
152435**
152436** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
152437** to recognize the end of a trigger can be omitted. All we have to do
152438** is look for a semicolon that is not part of an string or comment.
152439*/
152440SQLITE_API int sqlite3_complete(const char *zSql){
152441 u8 state = 0; /* Current state, using numbers defined in header comment */
152442 u8 token; /* Value of the next token */
152443
152444#ifndef SQLITE_OMIT_TRIGGER
152445 /* A complex statement machine used to detect the end of a CREATE TRIGGER
152446 ** statement. This is the normal case.
152447 */
152448 static const u8 trans[8][8] = {
152449 /* Token: */
152450 /* State: ** SEMI WS OTHER EXPLAIN CREATE TEMP TRIGGER END */
152451 /* 0 INVALID: */ { 1, 0, 2, 3, 4, 2, 2, 2, },
152452 /* 1 START: */ { 1, 1, 2, 3, 4, 2, 2, 2, },
152453 /* 2 NORMAL: */ { 1, 2, 2, 2, 2, 2, 2, 2, },
152454 /* 3 EXPLAIN: */ { 1, 3, 3, 2, 4, 2, 2, 2, },
152455 /* 4 CREATE: */ { 1, 4, 2, 2, 2, 4, 5, 2, },
152456 /* 5 TRIGGER: */ { 6, 5, 5, 5, 5, 5, 5, 5, },
152457 /* 6 SEMI: */ { 6, 6, 5, 5, 5, 5, 5, 7, },
152458 /* 7 END: */ { 1, 7, 5, 5, 5, 5, 5, 5, },
152459 };
152460#else
152461 /* If triggers are not supported by this compile then the statement machine
152462 ** used to detect the end of a statement is much simpler
152463 */
152464 static const u8 trans[3][3] = {
152465 /* Token: */
152466 /* State: ** SEMI WS OTHER */
152467 /* 0 INVALID: */ { 1, 0, 2, },
152468 /* 1 START: */ { 1, 1, 2, },
152469 /* 2 NORMAL: */ { 1, 2, 2, },
152470 };
152471#endif /* SQLITE_OMIT_TRIGGER */
152472
152473#ifdef SQLITE_ENABLE_API_ARMOR
152474 if( zSql==0 ){
152475 (void)SQLITE_MISUSE_BKPT;
152476 return 0;
152477 }
152478#endif
152479
152480 while( *zSql ){
152481 switch( *zSql ){
152482 case ';': { /* A semicolon */
152483 token = tkSEMI;
152484 break;
152485 }
152486 case ' ':
152487 case '\r':
152488 case '\t':
152489 case '\n':
152490 case '\f': { /* White space is ignored */
152491 token = tkWS;
152492 break;
152493 }
152494 case '/': { /* C-style comments */
152495 if( zSql[1]!='*' ){
152496 token = tkOTHER;
152497 break;
152498 }
152499 zSql += 2;
152500 while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
152501 if( zSql[0]==0 ) return 0;
152502 zSql++;
152503 token = tkWS;
152504 break;
152505 }
152506 case '-': { /* SQL-style comments from "--" to end of line */
152507 if( zSql[1]!='-' ){
152508 token = tkOTHER;
152509 break;
152510 }
152511 while( *zSql && *zSql!='\n' ){ zSql++; }
152512 if( *zSql==0 ) return state==1;
152513 token = tkWS;
152514 break;
152515 }
152516 case '[': { /* Microsoft-style identifiers in [...] */
152517 zSql++;
152518 while( *zSql && *zSql!=']' ){ zSql++; }
152519 if( *zSql==0 ) return 0;
152520 token = tkOTHER;
152521 break;
152522 }
152523 case '`': /* Grave-accent quoted symbols used by MySQL */
152524 case '"': /* single- and double-quoted strings */
152525 case '\'': {
152526 int c = *zSql;
152527 zSql++;
152528 while( *zSql && *zSql!=c ){ zSql++; }
152529 if( *zSql==0 ) return 0;
152530 token = tkOTHER;
152531 break;
152532 }
152533 default: {
152534#ifdef SQLITE_EBCDIC
152535 unsigned char c;
152536#endif
152537 if( IdChar((u8)*zSql) ){
152538 /* Keywords and unquoted identifiers */
152539 int nId;
152540 for(nId=1; IdChar(zSql[nId]); nId++){}
152541#ifdef SQLITE_OMIT_TRIGGER
152542 token = tkOTHER;
152543#else
152544 switch( *zSql ){
152545 case 'c': case 'C': {
152546 if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
152547 token = tkCREATE;
152548 }else{
152549 token = tkOTHER;
152550 }
152551 break;
152552 }
152553 case 't': case 'T': {
152554 if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
152555 token = tkTRIGGER;
152556 }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
152557 token = tkTEMP;
152558 }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
152559 token = tkTEMP;
152560 }else{
152561 token = tkOTHER;
152562 }
152563 break;
152564 }
152565 case 'e': case 'E': {
152566 if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
152567 token = tkEND;
152568 }else
152569#ifndef SQLITE_OMIT_EXPLAIN
152570 if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
152571 token = tkEXPLAIN;
152572 }else
152573#endif
152574 {
152575 token = tkOTHER;
152576 }
152577 break;
152578 }
152579 default: {
152580 token = tkOTHER;
152581 break;
152582 }
152583 }
152584#endif /* SQLITE_OMIT_TRIGGER */
152585 zSql += nId-1;
152586 }else{
152587 /* Operators and special symbols */
152588 token = tkOTHER;
152589 }
152590 break;
152591 }
152592 }
152593 state = trans[state][token];
152594 zSql++;
152595 }
152596 return state==1;
152597}
152598
152599#ifndef SQLITE_OMIT_UTF16
152600/*
152601** This routine is the same as the sqlite3_complete() routine described
152602** above, except that the parameter is required to be UTF-16 encoded, not
152603** UTF-8.
152604*/
152605SQLITE_API int sqlite3_complete16(const void *zSql){
152606 sqlite3_value *pVal;
152607 char const *zSql8;
152608 int rc;
152609
152610#ifndef SQLITE_OMIT_AUTOINIT
152611 rc = sqlite3_initialize();
152612 if( rc ) return rc;
152613#endif
152614 pVal = sqlite3ValueNew(0);
152615 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
152616 zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
152617 if( zSql8 ){
152618 rc = sqlite3_complete(zSql8);
152619 }else{
152620 rc = SQLITE_NOMEM_BKPT;
152621 }
152622 sqlite3ValueFree(pVal);
152623 return rc & 0xff;
152624}
152625#endif /* SQLITE_OMIT_UTF16 */
152626#endif /* SQLITE_OMIT_COMPLETE */
152627
152628/************** End of complete.c ********************************************/
152629/************** Begin file main.c ********************************************/
152630/*
152631** 2001 September 15
152632**
152633** The author disclaims copyright to this source code. In place of
152634** a legal notice, here is a blessing:
152635**
152636** May you do good and not evil.
152637** May you find forgiveness for yourself and forgive others.
152638** May you share freely, never taking more than you give.
152639**
152640*************************************************************************
152641** Main file for the SQLite library. The routines in this file
152642** implement the programmer interface to the library. Routines in
152643** other files are for internal use by SQLite and should not be
152644** accessed by users of the library.
152645*/
152646/* #include "sqliteInt.h" */
152647
152648#ifdef SQLITE_ENABLE_FTS3
152649/************** Include fts3.h in the middle of main.c ***********************/
152650/************** Begin file fts3.h ********************************************/
152651/*
152652** 2006 Oct 10
152653**
152654** The author disclaims copyright to this source code. In place of
152655** a legal notice, here is a blessing:
152656**
152657** May you do good and not evil.
152658** May you find forgiveness for yourself and forgive others.
152659** May you share freely, never taking more than you give.
152660**
152661******************************************************************************
152662**
152663** This header file is used by programs that want to link against the
152664** FTS3 library. All it does is declare the sqlite3Fts3Init() interface.
152665*/
152666/* #include "sqlite3.h" */
152667
152668#if 0
152669extern "C" {
152670#endif /* __cplusplus */
152671
152672SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db);
152673
152674#if 0
152675} /* extern "C" */
152676#endif /* __cplusplus */
152677
152678/************** End of fts3.h ************************************************/
152679/************** Continuing where we left off in main.c ***********************/
152680#endif
152681#ifdef SQLITE_ENABLE_RTREE
152682/************** Include rtree.h in the middle of main.c **********************/
152683/************** Begin file rtree.h *******************************************/
152684/*
152685** 2008 May 26
152686**
152687** The author disclaims copyright to this source code. In place of
152688** a legal notice, here is a blessing:
152689**
152690** May you do good and not evil.
152691** May you find forgiveness for yourself and forgive others.
152692** May you share freely, never taking more than you give.
152693**
152694******************************************************************************
152695**
152696** This header file is used by programs that want to link against the
152697** RTREE library. All it does is declare the sqlite3RtreeInit() interface.
152698*/
152699/* #include "sqlite3.h" */
152700
152701#ifdef SQLITE_OMIT_VIRTUALTABLE
152702# undef SQLITE_ENABLE_RTREE
152703#endif
152704
152705#if 0
152706extern "C" {
152707#endif /* __cplusplus */
152708
152709SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db);
152710
152711#if 0
152712} /* extern "C" */
152713#endif /* __cplusplus */
152714
152715/************** End of rtree.h ***********************************************/
152716/************** Continuing where we left off in main.c ***********************/
152717#endif
152718#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS)
152719/************** Include sqliteicu.h in the middle of main.c ******************/
152720/************** Begin file sqliteicu.h ***************************************/
152721/*
152722** 2008 May 26
152723**
152724** The author disclaims copyright to this source code. In place of
152725** a legal notice, here is a blessing:
152726**
152727** May you do good and not evil.
152728** May you find forgiveness for yourself and forgive others.
152729** May you share freely, never taking more than you give.
152730**
152731******************************************************************************
152732**
152733** This header file is used by programs that want to link against the
152734** ICU extension. All it does is declare the sqlite3IcuInit() interface.
152735*/
152736/* #include "sqlite3.h" */
152737
152738#if 0
152739extern "C" {
152740#endif /* __cplusplus */
152741
152742SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db);
152743
152744#if 0
152745} /* extern "C" */
152746#endif /* __cplusplus */
152747
152748
152749/************** End of sqliteicu.h *******************************************/
152750/************** Continuing where we left off in main.c ***********************/
152751#endif
152752#ifdef SQLITE_ENABLE_JSON1
152753SQLITE_PRIVATE int sqlite3Json1Init(sqlite3*);
152754#endif
152755#ifdef SQLITE_ENABLE_STMTVTAB
152756SQLITE_PRIVATE int sqlite3StmtVtabInit(sqlite3*);
152757#endif
152758#ifdef SQLITE_ENABLE_FTS5
152759SQLITE_PRIVATE int sqlite3Fts5Init(sqlite3*);
152760#endif
152761
152762#ifndef SQLITE_AMALGAMATION
152763/* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
152764** contains the text of SQLITE_VERSION macro.
152765*/
152766SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
152767#endif
152768
152769/* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
152770** a pointer to the to the sqlite3_version[] string constant.
152771*/
152772SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; }
152773
152774/* IMPLEMENTATION-OF: R-25063-23286 The sqlite3_sourceid() function returns a
152775** pointer to a string constant whose value is the same as the
152776** SQLITE_SOURCE_ID C preprocessor macro. Except if SQLite is built using
152777** an edited copy of the amalgamation, then the last four characters of
152778** the hash might be different from SQLITE_SOURCE_ID.
152779*/
152780/* SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } */
152781
152782/* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
152783** returns an integer equal to SQLITE_VERSION_NUMBER.
152784*/
152785SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
152786
152787/* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
152788** zero if and only if SQLite was compiled with mutexing code omitted due to
152789** the SQLITE_THREADSAFE compile-time option being set to 0.
152790*/
152791SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
152792
152793/*
152794** When compiling the test fixture or with debugging enabled (on Win32),
152795** this variable being set to non-zero will cause OSTRACE macros to emit
152796** extra diagnostic information.
152797*/
152798#ifdef SQLITE_HAVE_OS_TRACE
152799# ifndef SQLITE_DEBUG_OS_TRACE
152800# define SQLITE_DEBUG_OS_TRACE 0
152801# endif
152802 int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
152803#endif
152804
152805#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
152806/*
152807** If the following function pointer is not NULL and if
152808** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
152809** I/O active are written using this function. These messages
152810** are intended for debugging activity only.
152811*/
152812SQLITE_API void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...) = 0;
152813#endif
152814
152815/*
152816** If the following global variable points to a string which is the
152817** name of a directory, then that directory will be used to store
152818** temporary files.
152819**
152820** See also the "PRAGMA temp_store_directory" SQL command.
152821*/
152822SQLITE_API char *sqlite3_temp_directory = 0;
152823
152824/*
152825** If the following global variable points to a string which is the
152826** name of a directory, then that directory will be used to store
152827** all database files specified with a relative pathname.
152828**
152829** See also the "PRAGMA data_store_directory" SQL command.
152830*/
152831SQLITE_API char *sqlite3_data_directory = 0;
152832
152833/*
152834** Initialize SQLite.
152835**
152836** This routine must be called to initialize the memory allocation,
152837** VFS, and mutex subsystems prior to doing any serious work with
152838** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT
152839** this routine will be called automatically by key routines such as
152840** sqlite3_open().
152841**
152842** This routine is a no-op except on its very first call for the process,
152843** or for the first call after a call to sqlite3_shutdown.
152844**
152845** The first thread to call this routine runs the initialization to
152846** completion. If subsequent threads call this routine before the first
152847** thread has finished the initialization process, then the subsequent
152848** threads must block until the first thread finishes with the initialization.
152849**
152850** The first thread might call this routine recursively. Recursive
152851** calls to this routine should not block, of course. Otherwise the
152852** initialization process would never complete.
152853**
152854** Let X be the first thread to enter this routine. Let Y be some other
152855** thread. Then while the initial invocation of this routine by X is
152856** incomplete, it is required that:
152857**
152858** * Calls to this routine from Y must block until the outer-most
152859** call by X completes.
152860**
152861** * Recursive calls to this routine from thread X return immediately
152862** without blocking.
152863*/
152864SQLITE_API int sqlite3_initialize(void){
152865 MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */
152866 int rc; /* Result code */
152867#ifdef SQLITE_EXTRA_INIT
152868 int bRunExtraInit = 0; /* Extra initialization needed */
152869#endif
152870
152871#ifdef SQLITE_OMIT_WSD
152872 rc = sqlite3_wsd_init(4096, 24);
152873 if( rc!=SQLITE_OK ){
152874 return rc;
152875 }
152876#endif
152877
152878 /* If the following assert() fails on some obscure processor/compiler
152879 ** combination, the work-around is to set the correct pointer
152880 ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
152881 assert( SQLITE_PTRSIZE==sizeof(char*) );
152882
152883 /* If SQLite is already completely initialized, then this call
152884 ** to sqlite3_initialize() should be a no-op. But the initialization
152885 ** must be complete. So isInit must not be set until the very end
152886 ** of this routine.
152887 */
152888 if( sqlite3GlobalConfig.isInit ) return SQLITE_OK;
152889
152890 /* Make sure the mutex subsystem is initialized. If unable to
152891 ** initialize the mutex subsystem, return early with the error.
152892 ** If the system is so sick that we are unable to allocate a mutex,
152893 ** there is not much SQLite is going to be able to do.
152894 **
152895 ** The mutex subsystem must take care of serializing its own
152896 ** initialization.
152897 */
152898 rc = sqlite3MutexInit();
152899 if( rc ) return rc;
152900
152901 /* Initialize the malloc() system and the recursive pInitMutex mutex.
152902 ** This operation is protected by the STATIC_MASTER mutex. Note that
152903 ** MutexAlloc() is called for a static mutex prior to initializing the
152904 ** malloc subsystem - this implies that the allocation of a static
152905 ** mutex must not require support from the malloc subsystem.
152906 */
152907 MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
152908 sqlite3_mutex_enter(pMaster);
152909 sqlite3GlobalConfig.isMutexInit = 1;
152910 if( !sqlite3GlobalConfig.isMallocInit ){
152911 rc = sqlite3MallocInit();
152912 }
152913 if( rc==SQLITE_OK ){
152914 sqlite3GlobalConfig.isMallocInit = 1;
152915 if( !sqlite3GlobalConfig.pInitMutex ){
152916 sqlite3GlobalConfig.pInitMutex =
152917 sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
152918 if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
152919 rc = SQLITE_NOMEM_BKPT;
152920 }
152921 }
152922 }
152923 if( rc==SQLITE_OK ){
152924 sqlite3GlobalConfig.nRefInitMutex++;
152925 }
152926 sqlite3_mutex_leave(pMaster);
152927
152928 /* If rc is not SQLITE_OK at this point, then either the malloc
152929 ** subsystem could not be initialized or the system failed to allocate
152930 ** the pInitMutex mutex. Return an error in either case. */
152931 if( rc!=SQLITE_OK ){
152932 return rc;
152933 }
152934
152935 /* Do the rest of the initialization under the recursive mutex so
152936 ** that we will be able to handle recursive calls into
152937 ** sqlite3_initialize(). The recursive calls normally come through
152938 ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
152939 ** recursive calls might also be possible.
152940 **
152941 ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
152942 ** to the xInit method, so the xInit method need not be threadsafe.
152943 **
152944 ** The following mutex is what serializes access to the appdef pcache xInit
152945 ** methods. The sqlite3_pcache_methods.xInit() all is embedded in the
152946 ** call to sqlite3PcacheInitialize().
152947 */
152948 sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
152949 if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
152950 sqlite3GlobalConfig.inProgress = 1;
152951#ifdef SQLITE_ENABLE_SQLLOG
152952 {
152953 extern void sqlite3_init_sqllog(void);
152954 sqlite3_init_sqllog();
152955 }
152956#endif
152957 memset(&sqlite3BuiltinFunctions, 0, sizeof(sqlite3BuiltinFunctions));
152958 sqlite3RegisterBuiltinFunctions();
152959 if( sqlite3GlobalConfig.isPCacheInit==0 ){
152960 rc = sqlite3PcacheInitialize();
152961 }
152962 if( rc==SQLITE_OK ){
152963 sqlite3GlobalConfig.isPCacheInit = 1;
152964 rc = sqlite3OsInit();
152965 }
152966#ifdef SQLITE_ENABLE_DESERIALIZE
152967 if( rc==SQLITE_OK ){
152968 rc = sqlite3MemdbInit();
152969 }
152970#endif
152971 if( rc==SQLITE_OK ){
152972 sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage,
152973 sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
152974 sqlite3GlobalConfig.isInit = 1;
152975#ifdef SQLITE_EXTRA_INIT
152976 bRunExtraInit = 1;
152977#endif
152978 }
152979 sqlite3GlobalConfig.inProgress = 0;
152980 }
152981 sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);
152982
152983 /* Go back under the static mutex and clean up the recursive
152984 ** mutex to prevent a resource leak.
152985 */
152986 sqlite3_mutex_enter(pMaster);
152987 sqlite3GlobalConfig.nRefInitMutex--;
152988 if( sqlite3GlobalConfig.nRefInitMutex<=0 ){
152989 assert( sqlite3GlobalConfig.nRefInitMutex==0 );
152990 sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex);
152991 sqlite3GlobalConfig.pInitMutex = 0;
152992 }
152993 sqlite3_mutex_leave(pMaster);
152994
152995 /* The following is just a sanity check to make sure SQLite has
152996 ** been compiled correctly. It is important to run this code, but
152997 ** we don't want to run it too often and soak up CPU cycles for no
152998 ** reason. So we run it once during initialization.
152999 */
153000#ifndef NDEBUG
153001#ifndef SQLITE_OMIT_FLOATING_POINT
153002 /* This section of code's only "output" is via assert() statements. */
153003 if( rc==SQLITE_OK ){
153004 u64 x = (((u64)1)<<63)-1;
153005 double y;
153006 assert(sizeof(x)==8);
153007 assert(sizeof(x)==sizeof(y));
153008 memcpy(&y, &x, 8);
153009 assert( sqlite3IsNaN(y) );
153010 }
153011#endif
153012#endif
153013
153014 /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
153015 ** compile-time option.
153016 */
153017#ifdef SQLITE_EXTRA_INIT
153018 if( bRunExtraInit ){
153019 int SQLITE_EXTRA_INIT(const char*);
153020 rc = SQLITE_EXTRA_INIT(0);
153021 }
153022#endif
153023
153024 return rc;
153025}
153026
153027/*
153028** Undo the effects of sqlite3_initialize(). Must not be called while
153029** there are outstanding database connections or memory allocations or
153030** while any part of SQLite is otherwise in use in any thread. This
153031** routine is not threadsafe. But it is safe to invoke this routine
153032** on when SQLite is already shut down. If SQLite is already shut down
153033** when this routine is invoked, then this routine is a harmless no-op.
153034*/
153035SQLITE_API int sqlite3_shutdown(void){
153036#ifdef SQLITE_OMIT_WSD
153037 int rc = sqlite3_wsd_init(4096, 24);
153038 if( rc!=SQLITE_OK ){
153039 return rc;
153040 }
153041#endif
153042
153043 if( sqlite3GlobalConfig.isInit ){
153044#ifdef SQLITE_EXTRA_SHUTDOWN
153045 void SQLITE_EXTRA_SHUTDOWN(void);
153046 SQLITE_EXTRA_SHUTDOWN();
153047#endif
153048 sqlite3_os_end();
153049 sqlite3_reset_auto_extension();
153050 sqlite3GlobalConfig.isInit = 0;
153051 }
153052 if( sqlite3GlobalConfig.isPCacheInit ){
153053 sqlite3PcacheShutdown();
153054 sqlite3GlobalConfig.isPCacheInit = 0;
153055 }
153056 if( sqlite3GlobalConfig.isMallocInit ){
153057 sqlite3MallocEnd();
153058 sqlite3GlobalConfig.isMallocInit = 0;
153059
153060#ifndef SQLITE_OMIT_SHUTDOWN_DIRECTORIES
153061 /* The heap subsystem has now been shutdown and these values are supposed
153062 ** to be NULL or point to memory that was obtained from sqlite3_malloc(),
153063 ** which would rely on that heap subsystem; therefore, make sure these
153064 ** values cannot refer to heap memory that was just invalidated when the
153065 ** heap subsystem was shutdown. This is only done if the current call to
153066 ** this function resulted in the heap subsystem actually being shutdown.
153067 */
153068 sqlite3_data_directory = 0;
153069 sqlite3_temp_directory = 0;
153070#endif
153071 }
153072 if( sqlite3GlobalConfig.isMutexInit ){
153073 sqlite3MutexEnd();
153074 sqlite3GlobalConfig.isMutexInit = 0;
153075 }
153076
153077 return SQLITE_OK;
153078}
153079
153080/*
153081** This API allows applications to modify the global configuration of
153082** the SQLite library at run-time.
153083**
153084** This routine should only be called when there are no outstanding
153085** database connections or memory allocations. This routine is not
153086** threadsafe. Failure to heed these warnings can lead to unpredictable
153087** behavior.
153088*/
153089SQLITE_API int sqlite3_config(int op, ...){
153090 va_list ap;
153091 int rc = SQLITE_OK;
153092
153093 /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
153094 ** the SQLite library is in use. */
153095 if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT;
153096
153097 va_start(ap, op);
153098 switch( op ){
153099
153100 /* Mutex configuration options are only available in a threadsafe
153101 ** compile.
153102 */
153103#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */
153104 case SQLITE_CONFIG_SINGLETHREAD: {
153105 /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to
153106 ** Single-thread. */
153107 sqlite3GlobalConfig.bCoreMutex = 0; /* Disable mutex on core */
153108 sqlite3GlobalConfig.bFullMutex = 0; /* Disable mutex on connections */
153109 break;
153110 }
153111#endif
153112#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */
153113 case SQLITE_CONFIG_MULTITHREAD: {
153114 /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to
153115 ** Multi-thread. */
153116 sqlite3GlobalConfig.bCoreMutex = 1; /* Enable mutex on core */
153117 sqlite3GlobalConfig.bFullMutex = 0; /* Disable mutex on connections */
153118 break;
153119 }
153120#endif
153121#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */
153122 case SQLITE_CONFIG_SERIALIZED: {
153123 /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to
153124 ** Serialized. */
153125 sqlite3GlobalConfig.bCoreMutex = 1; /* Enable mutex on core */
153126 sqlite3GlobalConfig.bFullMutex = 1; /* Enable mutex on connections */
153127 break;
153128 }
153129#endif
153130#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */
153131 case SQLITE_CONFIG_MUTEX: {
153132 /* Specify an alternative mutex implementation */
153133 sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
153134 break;
153135 }
153136#endif
153137#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */
153138 case SQLITE_CONFIG_GETMUTEX: {
153139 /* Retrieve the current mutex implementation */
153140 *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
153141 break;
153142 }
153143#endif
153144
153145 case SQLITE_CONFIG_MALLOC: {
153146 /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a
153147 ** single argument which is a pointer to an instance of the
153148 ** sqlite3_mem_methods structure. The argument specifies alternative
153149 ** low-level memory allocation routines to be used in place of the memory
153150 ** allocation routines built into SQLite. */
153151 sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
153152 break;
153153 }
153154 case SQLITE_CONFIG_GETMALLOC: {
153155 /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a
153156 ** single argument which is a pointer to an instance of the
153157 ** sqlite3_mem_methods structure. The sqlite3_mem_methods structure is
153158 ** filled with the currently defined memory allocation routines. */
153159 if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
153160 *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
153161 break;
153162 }
153163 case SQLITE_CONFIG_MEMSTATUS: {
153164 /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
153165 ** single argument of type int, interpreted as a boolean, which enables
153166 ** or disables the collection of memory allocation statistics. */
153167 sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
153168 break;
153169 }
153170 case SQLITE_CONFIG_SMALL_MALLOC: {
153171 sqlite3GlobalConfig.bSmallMalloc = va_arg(ap, int);
153172 break;
153173 }
153174 case SQLITE_CONFIG_PAGECACHE: {
153175 /* EVIDENCE-OF: R-18761-36601 There are three arguments to
153176 ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem),
153177 ** the size of each page cache line (sz), and the number of cache lines
153178 ** (N). */
153179 sqlite3GlobalConfig.pPage = va_arg(ap, void*);
153180 sqlite3GlobalConfig.szPage = va_arg(ap, int);
153181 sqlite3GlobalConfig.nPage = va_arg(ap, int);
153182 break;
153183 }
153184 case SQLITE_CONFIG_PCACHE_HDRSZ: {
153185 /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
153186 ** a single parameter which is a pointer to an integer and writes into
153187 ** that integer the number of extra bytes per page required for each page
153188 ** in SQLITE_CONFIG_PAGECACHE. */
153189 *va_arg(ap, int*) =
153190 sqlite3HeaderSizeBtree() +
153191 sqlite3HeaderSizePcache() +
153192 sqlite3HeaderSizePcache1();
153193 break;
153194 }
153195
153196 case SQLITE_CONFIG_PCACHE: {
153197 /* no-op */
153198 break;
153199 }
153200 case SQLITE_CONFIG_GETPCACHE: {
153201 /* now an error */
153202 rc = SQLITE_ERROR;
153203 break;
153204 }
153205
153206 case SQLITE_CONFIG_PCACHE2: {
153207 /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
153208 ** single argument which is a pointer to an sqlite3_pcache_methods2
153209 ** object. This object specifies the interface to a custom page cache
153210 ** implementation. */
153211 sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*);
153212 break;
153213 }
153214 case SQLITE_CONFIG_GETPCACHE2: {
153215 /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
153216 ** single argument which is a pointer to an sqlite3_pcache_methods2
153217 ** object. SQLite copies of the current page cache implementation into
153218 ** that object. */
153219 if( sqlite3GlobalConfig.pcache2.xInit==0 ){
153220 sqlite3PCacheSetDefault();
153221 }
153222 *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2;
153223 break;
153224 }
153225
153226/* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only
153227** available if SQLite is compiled with either SQLITE_ENABLE_MEMSYS3 or
153228** SQLITE_ENABLE_MEMSYS5 and returns SQLITE_ERROR if invoked otherwise. */
153229#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
153230 case SQLITE_CONFIG_HEAP: {
153231 /* EVIDENCE-OF: R-19854-42126 There are three arguments to
153232 ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the
153233 ** number of bytes in the memory buffer, and the minimum allocation size.
153234 */
153235 sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
153236 sqlite3GlobalConfig.nHeap = va_arg(ap, int);
153237 sqlite3GlobalConfig.mnReq = va_arg(ap, int);
153238
153239 if( sqlite3GlobalConfig.mnReq<1 ){
153240 sqlite3GlobalConfig.mnReq = 1;
153241 }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){
153242 /* cap min request size at 2^12 */
153243 sqlite3GlobalConfig.mnReq = (1<<12);
153244 }
153245
153246 if( sqlite3GlobalConfig.pHeap==0 ){
153247 /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer)
153248 ** is NULL, then SQLite reverts to using its default memory allocator
153249 ** (the system malloc() implementation), undoing any prior invocation of
153250 ** SQLITE_CONFIG_MALLOC.
153251 **
153252 ** Setting sqlite3GlobalConfig.m to all zeros will cause malloc to
153253 ** revert to its default implementation when sqlite3_initialize() is run
153254 */
153255 memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
153256 }else{
153257 /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the
153258 ** alternative memory allocator is engaged to handle all of SQLites
153259 ** memory allocation needs. */
153260#ifdef SQLITE_ENABLE_MEMSYS3
153261 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
153262#endif
153263#ifdef SQLITE_ENABLE_MEMSYS5
153264 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
153265#endif
153266 }
153267 break;
153268 }
153269#endif
153270
153271 case SQLITE_CONFIG_LOOKASIDE: {
153272 sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
153273 sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
153274 break;
153275 }
153276
153277 /* Record a pointer to the logger function and its first argument.
153278 ** The default is NULL. Logging is disabled if the function pointer is
153279 ** NULL.
153280 */
153281 case SQLITE_CONFIG_LOG: {
153282 /* MSVC is picky about pulling func ptrs from va lists.
153283 ** http://support.microsoft.com/kb/47961
153284 ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));
153285 */
153286 typedef void(*LOGFUNC_t)(void*,int,const char*);
153287 sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t);
153288 sqlite3GlobalConfig.pLogArg = va_arg(ap, void*);
153289 break;
153290 }
153291
153292 /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames
153293 ** can be changed at start-time using the
153294 ** sqlite3_config(SQLITE_CONFIG_URI,1) or
153295 ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls.
153296 */
153297 case SQLITE_CONFIG_URI: {
153298 /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single
153299 ** argument of type int. If non-zero, then URI handling is globally
153300 ** enabled. If the parameter is zero, then URI handling is globally
153301 ** disabled. */
153302 sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
153303 break;
153304 }
153305
153306 case SQLITE_CONFIG_COVERING_INDEX_SCAN: {
153307 /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN
153308 ** option takes a single integer argument which is interpreted as a
153309 ** boolean in order to enable or disable the use of covering indices for
153310 ** full table scans in the query optimizer. */
153311 sqlite3GlobalConfig.bUseCis = va_arg(ap, int);
153312 break;
153313 }
153314
153315#ifdef SQLITE_ENABLE_SQLLOG
153316 case SQLITE_CONFIG_SQLLOG: {
153317 typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int);
153318 sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t);
153319 sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *);
153320 break;
153321 }
153322#endif
153323
153324 case SQLITE_CONFIG_MMAP_SIZE: {
153325 /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit
153326 ** integer (sqlite3_int64) values that are the default mmap size limit
153327 ** (the default setting for PRAGMA mmap_size) and the maximum allowed
153328 ** mmap size limit. */
153329 sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64);
153330 sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64);
153331 /* EVIDENCE-OF: R-53367-43190 If either argument to this option is
153332 ** negative, then that argument is changed to its compile-time default.
153333 **
153334 ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be
153335 ** silently truncated if necessary so that it does not exceed the
153336 ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE
153337 ** compile-time option.
153338 */
153339 if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){
153340 mxMmap = SQLITE_MAX_MMAP_SIZE;
153341 }
153342 if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE;
153343 if( szMmap>mxMmap) szMmap = mxMmap;
153344 sqlite3GlobalConfig.mxMmap = mxMmap;
153345 sqlite3GlobalConfig.szMmap = szMmap;
153346 break;
153347 }
153348
153349#if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */
153350 case SQLITE_CONFIG_WIN32_HEAPSIZE: {
153351 /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit
153352 ** unsigned integer value that specifies the maximum size of the created
153353 ** heap. */
153354 sqlite3GlobalConfig.nHeap = va_arg(ap, int);
153355 break;
153356 }
153357#endif
153358
153359 case SQLITE_CONFIG_PMASZ: {
153360 sqlite3GlobalConfig.szPma = va_arg(ap, unsigned int);
153361 break;
153362 }
153363
153364 case SQLITE_CONFIG_STMTJRNL_SPILL: {
153365 sqlite3GlobalConfig.nStmtSpill = va_arg(ap, int);
153366 break;
153367 }
153368
153369#ifdef SQLITE_ENABLE_SORTER_REFERENCES
153370 case SQLITE_CONFIG_SORTERREF_SIZE: {
153371 int iVal = va_arg(ap, int);
153372 if( iVal<0 ){
153373 iVal = SQLITE_DEFAULT_SORTERREF_SIZE;
153374 }
153375 sqlite3GlobalConfig.szSorterRef = (u32)iVal;
153376 break;
153377 }
153378#endif /* SQLITE_ENABLE_SORTER_REFERENCES */
153379
153380 default: {
153381 rc = SQLITE_ERROR;
153382 break;
153383 }
153384 }
153385 va_end(ap);
153386 return rc;
153387}
153388
153389/*
153390** Set up the lookaside buffers for a database connection.
153391** Return SQLITE_OK on success.
153392** If lookaside is already active, return SQLITE_BUSY.
153393**
153394** The sz parameter is the number of bytes in each lookaside slot.
153395** The cnt parameter is the number of slots. If pStart is NULL the
153396** space for the lookaside memory is obtained from sqlite3_malloc().
153397** If pStart is not NULL then it is sz*cnt bytes of memory to use for
153398** the lookaside memory.
153399*/
153400static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
153401#ifndef SQLITE_OMIT_LOOKASIDE
153402 void *pStart;
153403
153404 if( sqlite3LookasideUsed(db,0)>0 ){
153405 return SQLITE_BUSY;
153406 }
153407 /* Free any existing lookaside buffer for this handle before
153408 ** allocating a new one so we don't have to have space for
153409 ** both at the same time.
153410 */
153411 if( db->lookaside.bMalloced ){
153412 sqlite3_free(db->lookaside.pStart);
153413 }
153414 /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger
153415 ** than a pointer to be useful.
153416 */
153417 sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
153418 if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
153419 if( cnt<0 ) cnt = 0;
153420 if( sz==0 || cnt==0 ){
153421 sz = 0;
153422 pStart = 0;
153423 }else if( pBuf==0 ){
153424 sqlite3BeginBenignMalloc();
153425 pStart = sqlite3Malloc( sz*cnt ); /* IMP: R-61949-35727 */
153426 sqlite3EndBenignMalloc();
153427 if( pStart ) cnt = sqlite3MallocSize(pStart)/sz;
153428 }else{
153429 pStart = pBuf;
153430 }
153431 db->lookaside.pStart = pStart;
153432 db->lookaside.pInit = 0;
153433 db->lookaside.pFree = 0;
153434 db->lookaside.sz = (u16)sz;
153435 if( pStart ){
153436 int i;
153437 LookasideSlot *p;
153438 assert( sz > (int)sizeof(LookasideSlot*) );
153439 db->lookaside.nSlot = cnt;
153440 p = (LookasideSlot*)pStart;
153441 for(i=cnt-1; i>=0; i--){
153442 p->pNext = db->lookaside.pInit;
153443 db->lookaside.pInit = p;
153444 p = (LookasideSlot*)&((u8*)p)[sz];
153445 }
153446 db->lookaside.pEnd = p;
153447 db->lookaside.bDisable = 0;
153448 db->lookaside.bMalloced = pBuf==0 ?1:0;
153449 }else{
153450 db->lookaside.pStart = db;
153451 db->lookaside.pEnd = db;
153452 db->lookaside.bDisable = 1;
153453 db->lookaside.bMalloced = 0;
153454 db->lookaside.nSlot = 0;
153455 }
153456#endif /* SQLITE_OMIT_LOOKASIDE */
153457 return SQLITE_OK;
153458}
153459
153460/*
153461** Return the mutex associated with a database connection.
153462*/
153463SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
153464#ifdef SQLITE_ENABLE_API_ARMOR
153465 if( !sqlite3SafetyCheckOk(db) ){
153466 (void)SQLITE_MISUSE_BKPT;
153467 return 0;
153468 }
153469#endif
153470 return db->mutex;
153471}
153472
153473/*
153474** Free up as much memory as we can from the given database
153475** connection.
153476*/
153477SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){
153478 int i;
153479
153480#ifdef SQLITE_ENABLE_API_ARMOR
153481 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
153482#endif
153483 sqlite3_mutex_enter(db->mutex);
153484 sqlite3BtreeEnterAll(db);
153485 for(i=0; i<db->nDb; i++){
153486 Btree *pBt = db->aDb[i].pBt;
153487 if( pBt ){
153488 Pager *pPager = sqlite3BtreePager(pBt);
153489 sqlite3PagerShrink(pPager);
153490 }
153491 }
153492 sqlite3BtreeLeaveAll(db);
153493 sqlite3_mutex_leave(db->mutex);
153494 return SQLITE_OK;
153495}
153496
153497/*
153498** Flush any dirty pages in the pager-cache for any attached database
153499** to disk.
153500*/
153501SQLITE_API int sqlite3_db_cacheflush(sqlite3 *db){
153502 int i;
153503 int rc = SQLITE_OK;
153504 int bSeenBusy = 0;
153505
153506#ifdef SQLITE_ENABLE_API_ARMOR
153507 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
153508#endif
153509 sqlite3_mutex_enter(db->mutex);
153510 sqlite3BtreeEnterAll(db);
153511 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
153512 Btree *pBt = db->aDb[i].pBt;
153513 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
153514 Pager *pPager = sqlite3BtreePager(pBt);
153515 rc = sqlite3PagerFlush(pPager);
153516 if( rc==SQLITE_BUSY ){
153517 bSeenBusy = 1;
153518 rc = SQLITE_OK;
153519 }
153520 }
153521 }
153522 sqlite3BtreeLeaveAll(db);
153523 sqlite3_mutex_leave(db->mutex);
153524 return ((rc==SQLITE_OK && bSeenBusy) ? SQLITE_BUSY : rc);
153525}
153526
153527/*
153528** Configuration settings for an individual database connection
153529*/
153530SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
153531 va_list ap;
153532 int rc;
153533 va_start(ap, op);
153534 switch( op ){
153535 case SQLITE_DBCONFIG_MAINDBNAME: {
153536 /* IMP: R-06824-28531 */
153537 /* IMP: R-36257-52125 */
153538 db->aDb[0].zDbSName = va_arg(ap,char*);
153539 rc = SQLITE_OK;
153540 break;
153541 }
153542 case SQLITE_DBCONFIG_LOOKASIDE: {
153543 void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
153544 int sz = va_arg(ap, int); /* IMP: R-47871-25994 */
153545 int cnt = va_arg(ap, int); /* IMP: R-04460-53386 */
153546 rc = setupLookaside(db, pBuf, sz, cnt);
153547 break;
153548 }
153549 default: {
153550 static const struct {
153551 int op; /* The opcode */
153552 u32 mask; /* Mask of the bit in sqlite3.flags to set/clear */
153553 } aFlagOp[] = {
153554 { SQLITE_DBCONFIG_ENABLE_FKEY, SQLITE_ForeignKeys },
153555 { SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger },
153556 { SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, SQLITE_Fts3Tokenizer },
153557 { SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, SQLITE_LoadExtension },
153558 { SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE, SQLITE_NoCkptOnClose },
153559 { SQLITE_DBCONFIG_ENABLE_QPSG, SQLITE_EnableQPSG },
153560 { SQLITE_DBCONFIG_TRIGGER_EQP, SQLITE_TriggerEQP },
153561 { SQLITE_DBCONFIG_RESET_DATABASE, SQLITE_ResetDatabase },
153562 { SQLITE_DBCONFIG_DEFENSIVE, SQLITE_Defensive },
153563 };
153564 unsigned int i;
153565 rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
153566 for(i=0; i<ArraySize(aFlagOp); i++){
153567 if( aFlagOp[i].op==op ){
153568 int onoff = va_arg(ap, int);
153569 int *pRes = va_arg(ap, int*);
153570 u32 oldFlags = db->flags;
153571 if( onoff>0 ){
153572 db->flags |= aFlagOp[i].mask;
153573 }else if( onoff==0 ){
153574 db->flags &= ~aFlagOp[i].mask;
153575 }
153576 if( oldFlags!=db->flags ){
153577 sqlite3ExpirePreparedStatements(db, 0);
153578 }
153579 if( pRes ){
153580 *pRes = (db->flags & aFlagOp[i].mask)!=0;
153581 }
153582 rc = SQLITE_OK;
153583 break;
153584 }
153585 }
153586 break;
153587 }
153588 }
153589 va_end(ap);
153590 return rc;
153591}
153592
153593
153594/*
153595** Return true if the buffer z[0..n-1] contains all spaces.
153596*/
153597static int allSpaces(const char *z, int n){
153598 while( n>0 && z[n-1]==' ' ){ n--; }
153599 return n==0;
153600}
153601
153602/*
153603** This is the default collating function named "BINARY" which is always
153604** available.
153605**
153606** If the padFlag argument is not NULL then space padding at the end
153607** of strings is ignored. This implements the RTRIM collation.
153608*/
153609static int binCollFunc(
153610 void *padFlag,
153611 int nKey1, const void *pKey1,
153612 int nKey2, const void *pKey2
153613){
153614 int rc, n;
153615 n = nKey1<nKey2 ? nKey1 : nKey2;
153616 /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares
153617 ** strings byte by byte using the memcmp() function from the standard C
153618 ** library. */
153619 assert( pKey1 && pKey2 );
153620 rc = memcmp(pKey1, pKey2, n);
153621 if( rc==0 ){
153622 if( padFlag
153623 && allSpaces(((char*)pKey1)+n, nKey1-n)
153624 && allSpaces(((char*)pKey2)+n, nKey2-n)
153625 ){
153626 /* EVIDENCE-OF: R-31624-24737 RTRIM is like BINARY except that extra
153627 ** spaces at the end of either string do not change the result. In other
153628 ** words, strings will compare equal to one another as long as they
153629 ** differ only in the number of spaces at the end.
153630 */
153631 }else{
153632 rc = nKey1 - nKey2;
153633 }
153634 }
153635 return rc;
153636}
153637
153638/*
153639** Return true if CollSeq is the default built-in BINARY.
153640*/
153641SQLITE_PRIVATE int sqlite3IsBinary(const CollSeq *p){
153642 assert( p==0 || p->xCmp!=binCollFunc || p->pUser!=0
153643 || strcmp(p->zName,"BINARY")==0 );
153644 return p==0 || (p->xCmp==binCollFunc && p->pUser==0);
153645}
153646
153647/*
153648** Another built-in collating sequence: NOCASE.
153649**
153650** This collating sequence is intended to be used for "case independent
153651** comparison". SQLite's knowledge of upper and lower case equivalents
153652** extends only to the 26 characters used in the English language.
153653**
153654** At the moment there is only a UTF-8 implementation.
153655*/
153656static int nocaseCollatingFunc(
153657 void *NotUsed,
153658 int nKey1, const void *pKey1,
153659 int nKey2, const void *pKey2
153660){
153661 int r = sqlite3StrNICmp(
153662 (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
153663 UNUSED_PARAMETER(NotUsed);
153664 if( 0==r ){
153665 r = nKey1-nKey2;
153666 }
153667 return r;
153668}
153669
153670/*
153671** Return the ROWID of the most recent insert
153672*/
153673SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
153674#ifdef SQLITE_ENABLE_API_ARMOR
153675 if( !sqlite3SafetyCheckOk(db) ){
153676 (void)SQLITE_MISUSE_BKPT;
153677 return 0;
153678 }
153679#endif
153680 return db->lastRowid;
153681}
153682
153683/*
153684** Set the value returned by the sqlite3_last_insert_rowid() API function.
153685*/
153686SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3 *db, sqlite3_int64 iRowid){
153687#ifdef SQLITE_ENABLE_API_ARMOR
153688 if( !sqlite3SafetyCheckOk(db) ){
153689 (void)SQLITE_MISUSE_BKPT;
153690 return;
153691 }
153692#endif
153693 sqlite3_mutex_enter(db->mutex);
153694 db->lastRowid = iRowid;
153695 sqlite3_mutex_leave(db->mutex);
153696}
153697
153698/*
153699** Return the number of changes in the most recent call to sqlite3_exec().
153700*/
153701SQLITE_API int sqlite3_changes(sqlite3 *db){
153702#ifdef SQLITE_ENABLE_API_ARMOR
153703 if( !sqlite3SafetyCheckOk(db) ){
153704 (void)SQLITE_MISUSE_BKPT;
153705 return 0;
153706 }
153707#endif
153708 return db->nChange;
153709}
153710
153711/*
153712** Return the number of changes since the database handle was opened.
153713*/
153714SQLITE_API int sqlite3_total_changes(sqlite3 *db){
153715#ifdef SQLITE_ENABLE_API_ARMOR
153716 if( !sqlite3SafetyCheckOk(db) ){
153717 (void)SQLITE_MISUSE_BKPT;
153718 return 0;
153719 }
153720#endif
153721 return db->nTotalChange;
153722}
153723
153724/*
153725** Close all open savepoints. This function only manipulates fields of the
153726** database handle object, it does not close any savepoints that may be open
153727** at the b-tree/pager level.
153728*/
153729SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *db){
153730 while( db->pSavepoint ){
153731 Savepoint *pTmp = db->pSavepoint;
153732 db->pSavepoint = pTmp->pNext;
153733 sqlite3DbFree(db, pTmp);
153734 }
153735 db->nSavepoint = 0;
153736 db->nStatement = 0;
153737 db->isTransactionSavepoint = 0;
153738}
153739
153740/*
153741** Invoke the destructor function associated with FuncDef p, if any. Except,
153742** if this is not the last copy of the function, do not invoke it. Multiple
153743** copies of a single function are created when create_function() is called
153744** with SQLITE_ANY as the encoding.
153745*/
153746static void functionDestroy(sqlite3 *db, FuncDef *p){
153747 FuncDestructor *pDestructor = p->u.pDestructor;
153748 if( pDestructor ){
153749 pDestructor->nRef--;
153750 if( pDestructor->nRef==0 ){
153751 pDestructor->xDestroy(pDestructor->pUserData);
153752 sqlite3DbFree(db, pDestructor);
153753 }
153754 }
153755}
153756
153757/*
153758** Disconnect all sqlite3_vtab objects that belong to database connection
153759** db. This is called when db is being closed.
153760*/
153761static void disconnectAllVtab(sqlite3 *db){
153762#ifndef SQLITE_OMIT_VIRTUALTABLE
153763 int i;
153764 HashElem *p;
153765 sqlite3BtreeEnterAll(db);
153766 for(i=0; i<db->nDb; i++){
153767 Schema *pSchema = db->aDb[i].pSchema;
153768 if( pSchema ){
153769 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
153770 Table *pTab = (Table *)sqliteHashData(p);
153771 if( IsVirtual(pTab) ) sqlite3VtabDisconnect(db, pTab);
153772 }
153773 }
153774 }
153775 for(p=sqliteHashFirst(&db->aModule); p; p=sqliteHashNext(p)){
153776 Module *pMod = (Module *)sqliteHashData(p);
153777 if( pMod->pEpoTab ){
153778 sqlite3VtabDisconnect(db, pMod->pEpoTab);
153779 }
153780 }
153781 sqlite3VtabUnlockList(db);
153782 sqlite3BtreeLeaveAll(db);
153783#else
153784 UNUSED_PARAMETER(db);
153785#endif
153786}
153787
153788/*
153789** Return TRUE if database connection db has unfinalized prepared
153790** statements or unfinished sqlite3_backup objects.
153791*/
153792static int connectionIsBusy(sqlite3 *db){
153793 int j;
153794 assert( sqlite3_mutex_held(db->mutex) );
153795 if( db->pVdbe ) return 1;
153796 for(j=0; j<db->nDb; j++){
153797 Btree *pBt = db->aDb[j].pBt;
153798 if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1;
153799 }
153800 return 0;
153801}
153802
153803/*
153804** Close an existing SQLite database
153805*/
153806static int sqlite3Close(sqlite3 *db, int forceZombie){
153807 if( !db ){
153808 /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or
153809 ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */
153810 return SQLITE_OK;
153811 }
153812 if( !sqlite3SafetyCheckSickOrOk(db) ){
153813 return SQLITE_MISUSE_BKPT;
153814 }
153815 sqlite3_mutex_enter(db->mutex);
153816 if( db->mTrace & SQLITE_TRACE_CLOSE ){
153817 db->xTrace(SQLITE_TRACE_CLOSE, db->pTraceArg, db, 0);
153818 }
153819
153820 /* Force xDisconnect calls on all virtual tables */
153821 disconnectAllVtab(db);
153822
153823 /* If a transaction is open, the disconnectAllVtab() call above
153824 ** will not have called the xDisconnect() method on any virtual
153825 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
153826 ** call will do so. We need to do this before the check for active
153827 ** SQL statements below, as the v-table implementation may be storing
153828 ** some prepared statements internally.
153829 */
153830 sqlite3VtabRollback(db);
153831
153832 /* Legacy behavior (sqlite3_close() behavior) is to return
153833 ** SQLITE_BUSY if the connection can not be closed immediately.
153834 */
153835 if( !forceZombie && connectionIsBusy(db) ){
153836 sqlite3ErrorWithMsg(db, SQLITE_BUSY, "unable to close due to unfinalized "
153837 "statements or unfinished backups");
153838 sqlite3_mutex_leave(db->mutex);
153839 return SQLITE_BUSY;
153840 }
153841
153842#ifdef SQLITE_ENABLE_SQLLOG
153843 if( sqlite3GlobalConfig.xSqllog ){
153844 /* Closing the handle. Fourth parameter is passed the value 2. */
153845 sqlite3GlobalConfig.xSqllog(sqlite3GlobalConfig.pSqllogArg, db, 0, 2);
153846 }
153847#endif
153848
153849 /* Convert the connection into a zombie and then close it.
153850 */
153851 db->magic = SQLITE_MAGIC_ZOMBIE;
153852 sqlite3LeaveMutexAndCloseZombie(db);
153853 return SQLITE_OK;
153854}
153855
153856/*
153857** Two variations on the public interface for closing a database
153858** connection. The sqlite3_close() version returns SQLITE_BUSY and
153859** leaves the connection option if there are unfinalized prepared
153860** statements or unfinished sqlite3_backups. The sqlite3_close_v2()
153861** version forces the connection to become a zombie if there are
153862** unclosed resources, and arranges for deallocation when the last
153863** prepare statement or sqlite3_backup closes.
153864*/
153865SQLITE_API int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); }
153866SQLITE_API int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); }
153867
153868
153869/*
153870** Close the mutex on database connection db.
153871**
153872** Furthermore, if database connection db is a zombie (meaning that there
153873** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and
153874** every sqlite3_stmt has now been finalized and every sqlite3_backup has
153875** finished, then free all resources.
153876*/
153877SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
153878 HashElem *i; /* Hash table iterator */
153879 int j;
153880
153881 /* If there are outstanding sqlite3_stmt or sqlite3_backup objects
153882 ** or if the connection has not yet been closed by sqlite3_close_v2(),
153883 ** then just leave the mutex and return.
153884 */
153885 if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){
153886 sqlite3_mutex_leave(db->mutex);
153887 return;
153888 }
153889
153890 /* If we reach this point, it means that the database connection has
153891 ** closed all sqlite3_stmt and sqlite3_backup objects and has been
153892 ** passed to sqlite3_close (meaning that it is a zombie). Therefore,
153893 ** go ahead and free all resources.
153894 */
153895
153896 /* If a transaction is open, roll it back. This also ensures that if
153897 ** any database schemas have been modified by an uncommitted transaction
153898 ** they are reset. And that the required b-tree mutex is held to make
153899 ** the pager rollback and schema reset an atomic operation. */
153900 sqlite3RollbackAll(db, SQLITE_OK);
153901
153902 /* Free any outstanding Savepoint structures. */
153903 sqlite3CloseSavepoints(db);
153904
153905 /* Close all database connections */
153906 for(j=0; j<db->nDb; j++){
153907 struct Db *pDb = &db->aDb[j];
153908 if( pDb->pBt ){
153909 sqlite3BtreeClose(pDb->pBt);
153910 pDb->pBt = 0;
153911 if( j!=1 ){
153912 pDb->pSchema = 0;
153913 }
153914 }
153915 }
153916 /* Clear the TEMP schema separately and last */
153917 if( db->aDb[1].pSchema ){
153918 sqlite3SchemaClear(db->aDb[1].pSchema);
153919 }
153920 sqlite3VtabUnlockList(db);
153921
153922 /* Free up the array of auxiliary databases */
153923 sqlite3CollapseDatabaseArray(db);
153924 assert( db->nDb<=2 );
153925 assert( db->aDb==db->aDbStatic );
153926
153927 /* Tell the code in notify.c that the connection no longer holds any
153928 ** locks and does not require any further unlock-notify callbacks.
153929 */
153930 sqlite3ConnectionClosed(db);
153931
153932 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
153933 FuncDef *pNext, *p;
153934 p = sqliteHashData(i);
153935 do{
153936 functionDestroy(db, p);
153937 pNext = p->pNext;
153938 sqlite3DbFree(db, p);
153939 p = pNext;
153940 }while( p );
153941 }
153942 sqlite3HashClear(&db->aFunc);
153943 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
153944 CollSeq *pColl = (CollSeq *)sqliteHashData(i);
153945 /* Invoke any destructors registered for collation sequence user data. */
153946 for(j=0; j<3; j++){
153947 if( pColl[j].xDel ){
153948 pColl[j].xDel(pColl[j].pUser);
153949 }
153950 }
153951 sqlite3DbFree(db, pColl);
153952 }
153953 sqlite3HashClear(&db->aCollSeq);
153954#ifndef SQLITE_OMIT_VIRTUALTABLE
153955 for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
153956 Module *pMod = (Module *)sqliteHashData(i);
153957 if( pMod->xDestroy ){
153958 pMod->xDestroy(pMod->pAux);
153959 }
153960 sqlite3VtabEponymousTableClear(db, pMod);
153961 sqlite3DbFree(db, pMod);
153962 }
153963 sqlite3HashClear(&db->aModule);
153964#endif
153965
153966 sqlite3Error(db, SQLITE_OK); /* Deallocates any cached error strings. */
153967 sqlite3ValueFree(db->pErr);
153968 sqlite3CloseExtensions(db);
153969#if SQLITE_USER_AUTHENTICATION
153970 sqlite3_free(db->auth.zAuthUser);
153971 sqlite3_free(db->auth.zAuthPW);
153972#endif
153973
153974 db->magic = SQLITE_MAGIC_ERROR;
153975
153976 /* The temp-database schema is allocated differently from the other schema
153977 ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
153978 ** So it needs to be freed here. Todo: Why not roll the temp schema into
153979 ** the same sqliteMalloc() as the one that allocates the database
153980 ** structure?
153981 */
153982 sqlite3DbFree(db, db->aDb[1].pSchema);
153983 sqlite3_mutex_leave(db->mutex);
153984 db->magic = SQLITE_MAGIC_CLOSED;
153985 sqlite3_mutex_free(db->mutex);
153986 assert( sqlite3LookasideUsed(db,0)==0 );
153987 if( db->lookaside.bMalloced ){
153988 sqlite3_free(db->lookaside.pStart);
153989 }
153990 sqlite3_free(db);
153991}
153992
153993/*
153994** Rollback all database files. If tripCode is not SQLITE_OK, then
153995** any write cursors are invalidated ("tripped" - as in "tripping a circuit
153996** breaker") and made to return tripCode if there are any further
153997** attempts to use that cursor. Read cursors remain open and valid
153998** but are "saved" in case the table pages are moved around.
153999*/
154000SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){
154001 int i;
154002 int inTrans = 0;
154003 int schemaChange;
154004 assert( sqlite3_mutex_held(db->mutex) );
154005 sqlite3BeginBenignMalloc();
154006
154007 /* Obtain all b-tree mutexes before making any calls to BtreeRollback().
154008 ** This is important in case the transaction being rolled back has
154009 ** modified the database schema. If the b-tree mutexes are not taken
154010 ** here, then another shared-cache connection might sneak in between
154011 ** the database rollback and schema reset, which can cause false
154012 ** corruption reports in some cases. */
154013 sqlite3BtreeEnterAll(db);
154014 schemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0 && db->init.busy==0;
154015
154016 for(i=0; i<db->nDb; i++){
154017 Btree *p = db->aDb[i].pBt;
154018 if( p ){
154019 if( sqlite3BtreeIsInTrans(p) ){
154020 inTrans = 1;
154021 }
154022 sqlite3BtreeRollback(p, tripCode, !schemaChange);
154023 }
154024 }
154025 sqlite3VtabRollback(db);
154026 sqlite3EndBenignMalloc();
154027
154028 if( schemaChange ){
154029 sqlite3ExpirePreparedStatements(db, 0);
154030 sqlite3ResetAllSchemasOfConnection(db);
154031 }
154032 sqlite3BtreeLeaveAll(db);
154033
154034 /* Any deferred constraint violations have now been resolved. */
154035 db->nDeferredCons = 0;
154036 db->nDeferredImmCons = 0;
154037 db->flags &= ~SQLITE_DeferFKs;
154038
154039 /* If one has been configured, invoke the rollback-hook callback */
154040 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
154041 db->xRollbackCallback(db->pRollbackArg);
154042 }
154043}
154044
154045/*
154046** Return a static string containing the name corresponding to the error code
154047** specified in the argument.
154048*/
154049#if defined(SQLITE_NEED_ERR_NAME)
154050SQLITE_PRIVATE const char *sqlite3ErrName(int rc){
154051 const char *zName = 0;
154052 int i, origRc = rc;
154053 for(i=0; i<2 && zName==0; i++, rc &= 0xff){
154054 switch( rc ){
154055 case SQLITE_OK: zName = "SQLITE_OK"; break;
154056 case SQLITE_ERROR: zName = "SQLITE_ERROR"; break;
154057 case SQLITE_ERROR_SNAPSHOT: zName = "SQLITE_ERROR_SNAPSHOT"; break;
154058 case SQLITE_INTERNAL: zName = "SQLITE_INTERNAL"; break;
154059 case SQLITE_PERM: zName = "SQLITE_PERM"; break;
154060 case SQLITE_ABORT: zName = "SQLITE_ABORT"; break;
154061 case SQLITE_ABORT_ROLLBACK: zName = "SQLITE_ABORT_ROLLBACK"; break;
154062 case SQLITE_BUSY: zName = "SQLITE_BUSY"; break;
154063 case SQLITE_BUSY_RECOVERY: zName = "SQLITE_BUSY_RECOVERY"; break;
154064 case SQLITE_BUSY_SNAPSHOT: zName = "SQLITE_BUSY_SNAPSHOT"; break;
154065 case SQLITE_LOCKED: zName = "SQLITE_LOCKED"; break;
154066 case SQLITE_LOCKED_SHAREDCACHE: zName = "SQLITE_LOCKED_SHAREDCACHE";break;
154067 case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break;
154068 case SQLITE_READONLY: zName = "SQLITE_READONLY"; break;
154069 case SQLITE_READONLY_RECOVERY: zName = "SQLITE_READONLY_RECOVERY"; break;
154070 case SQLITE_READONLY_CANTINIT: zName = "SQLITE_READONLY_CANTINIT"; break;
154071 case SQLITE_READONLY_ROLLBACK: zName = "SQLITE_READONLY_ROLLBACK"; break;
154072 case SQLITE_READONLY_DBMOVED: zName = "SQLITE_READONLY_DBMOVED"; break;
154073 case SQLITE_READONLY_DIRECTORY: zName = "SQLITE_READONLY_DIRECTORY";break;
154074 case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break;
154075 case SQLITE_IOERR: zName = "SQLITE_IOERR"; break;
154076 case SQLITE_IOERR_READ: zName = "SQLITE_IOERR_READ"; break;
154077 case SQLITE_IOERR_SHORT_READ: zName = "SQLITE_IOERR_SHORT_READ"; break;
154078 case SQLITE_IOERR_WRITE: zName = "SQLITE_IOERR_WRITE"; break;
154079 case SQLITE_IOERR_FSYNC: zName = "SQLITE_IOERR_FSYNC"; break;
154080 case SQLITE_IOERR_DIR_FSYNC: zName = "SQLITE_IOERR_DIR_FSYNC"; break;
154081 case SQLITE_IOERR_TRUNCATE: zName = "SQLITE_IOERR_TRUNCATE"; break;
154082 case SQLITE_IOERR_FSTAT: zName = "SQLITE_IOERR_FSTAT"; break;
154083 case SQLITE_IOERR_UNLOCK: zName = "SQLITE_IOERR_UNLOCK"; break;
154084 case SQLITE_IOERR_RDLOCK: zName = "SQLITE_IOERR_RDLOCK"; break;
154085 case SQLITE_IOERR_DELETE: zName = "SQLITE_IOERR_DELETE"; break;
154086 case SQLITE_IOERR_NOMEM: zName = "SQLITE_IOERR_NOMEM"; break;
154087 case SQLITE_IOERR_ACCESS: zName = "SQLITE_IOERR_ACCESS"; break;
154088 case SQLITE_IOERR_CHECKRESERVEDLOCK:
154089 zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
154090 case SQLITE_IOERR_LOCK: zName = "SQLITE_IOERR_LOCK"; break;
154091 case SQLITE_IOERR_CLOSE: zName = "SQLITE_IOERR_CLOSE"; break;
154092 case SQLITE_IOERR_DIR_CLOSE: zName = "SQLITE_IOERR_DIR_CLOSE"; break;
154093 case SQLITE_IOERR_SHMOPEN: zName = "SQLITE_IOERR_SHMOPEN"; break;
154094 case SQLITE_IOERR_SHMSIZE: zName = "SQLITE_IOERR_SHMSIZE"; break;
154095 case SQLITE_IOERR_SHMLOCK: zName = "SQLITE_IOERR_SHMLOCK"; break;
154096 case SQLITE_IOERR_SHMMAP: zName = "SQLITE_IOERR_SHMMAP"; break;
154097 case SQLITE_IOERR_SEEK: zName = "SQLITE_IOERR_SEEK"; break;
154098 case SQLITE_IOERR_DELETE_NOENT: zName = "SQLITE_IOERR_DELETE_NOENT";break;
154099 case SQLITE_IOERR_MMAP: zName = "SQLITE_IOERR_MMAP"; break;
154100 case SQLITE_IOERR_GETTEMPPATH: zName = "SQLITE_IOERR_GETTEMPPATH"; break;
154101 case SQLITE_IOERR_CONVPATH: zName = "SQLITE_IOERR_CONVPATH"; break;
154102 case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break;
154103 case SQLITE_CORRUPT_VTAB: zName = "SQLITE_CORRUPT_VTAB"; break;
154104 case SQLITE_NOTFOUND: zName = "SQLITE_NOTFOUND"; break;
154105 case SQLITE_FULL: zName = "SQLITE_FULL"; break;
154106 case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break;
154107 case SQLITE_CANTOPEN_NOTEMPDIR: zName = "SQLITE_CANTOPEN_NOTEMPDIR";break;
154108 case SQLITE_CANTOPEN_ISDIR: zName = "SQLITE_CANTOPEN_ISDIR"; break;
154109 case SQLITE_CANTOPEN_FULLPATH: zName = "SQLITE_CANTOPEN_FULLPATH"; break;
154110 case SQLITE_CANTOPEN_CONVPATH: zName = "SQLITE_CANTOPEN_CONVPATH"; break;
154111 case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break;
154112 case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break;
154113 case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break;
154114 case SQLITE_TOOBIG: zName = "SQLITE_TOOBIG"; break;
154115 case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break;
154116 case SQLITE_CONSTRAINT_UNIQUE: zName = "SQLITE_CONSTRAINT_UNIQUE"; break;
154117 case SQLITE_CONSTRAINT_TRIGGER: zName = "SQLITE_CONSTRAINT_TRIGGER";break;
154118 case SQLITE_CONSTRAINT_FOREIGNKEY:
154119 zName = "SQLITE_CONSTRAINT_FOREIGNKEY"; break;
154120 case SQLITE_CONSTRAINT_CHECK: zName = "SQLITE_CONSTRAINT_CHECK"; break;
154121 case SQLITE_CONSTRAINT_PRIMARYKEY:
154122 zName = "SQLITE_CONSTRAINT_PRIMARYKEY"; break;
154123 case SQLITE_CONSTRAINT_NOTNULL: zName = "SQLITE_CONSTRAINT_NOTNULL";break;
154124 case SQLITE_CONSTRAINT_COMMITHOOK:
154125 zName = "SQLITE_CONSTRAINT_COMMITHOOK"; break;
154126 case SQLITE_CONSTRAINT_VTAB: zName = "SQLITE_CONSTRAINT_VTAB"; break;
154127 case SQLITE_CONSTRAINT_FUNCTION:
154128 zName = "SQLITE_CONSTRAINT_FUNCTION"; break;
154129 case SQLITE_CONSTRAINT_ROWID: zName = "SQLITE_CONSTRAINT_ROWID"; break;
154130 case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break;
154131 case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break;
154132 case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break;
154133 case SQLITE_AUTH: zName = "SQLITE_AUTH"; break;
154134 case SQLITE_FORMAT: zName = "SQLITE_FORMAT"; break;
154135 case SQLITE_RANGE: zName = "SQLITE_RANGE"; break;
154136 case SQLITE_NOTADB: zName = "SQLITE_NOTADB"; break;
154137 case SQLITE_ROW: zName = "SQLITE_ROW"; break;
154138 case SQLITE_NOTICE: zName = "SQLITE_NOTICE"; break;
154139 case SQLITE_NOTICE_RECOVER_WAL: zName = "SQLITE_NOTICE_RECOVER_WAL";break;
154140 case SQLITE_NOTICE_RECOVER_ROLLBACK:
154141 zName = "SQLITE_NOTICE_RECOVER_ROLLBACK"; break;
154142 case SQLITE_WARNING: zName = "SQLITE_WARNING"; break;
154143 case SQLITE_WARNING_AUTOINDEX: zName = "SQLITE_WARNING_AUTOINDEX"; break;
154144 case SQLITE_DONE: zName = "SQLITE_DONE"; break;
154145 }
154146 }
154147 if( zName==0 ){
154148 static char zBuf[50];
154149 sqlite3_snprintf(sizeof(zBuf), zBuf, "SQLITE_UNKNOWN(%d)", origRc);
154150 zName = zBuf;
154151 }
154152 return zName;
154153}
154154#endif
154155
154156/*
154157** Return a static string that describes the kind of error specified in the
154158** argument.
154159*/
154160SQLITE_PRIVATE const char *sqlite3ErrStr(int rc){
154161 static const char* const aMsg[] = {
154162 /* SQLITE_OK */ "not an error",
154163 /* SQLITE_ERROR */ "SQL logic error",
154164 /* SQLITE_INTERNAL */ 0,
154165 /* SQLITE_PERM */ "access permission denied",
154166 /* SQLITE_ABORT */ "query aborted",
154167 /* SQLITE_BUSY */ "database is locked",
154168 /* SQLITE_LOCKED */ "database table is locked",
154169 /* SQLITE_NOMEM */ "out of memory",
154170 /* SQLITE_READONLY */ "attempt to write a readonly database",
154171 /* SQLITE_INTERRUPT */ "interrupted",
154172 /* SQLITE_IOERR */ "disk I/O error",
154173 /* SQLITE_CORRUPT */ "database disk image is malformed",
154174 /* SQLITE_NOTFOUND */ "unknown operation",
154175 /* SQLITE_FULL */ "database or disk is full",
154176 /* SQLITE_CANTOPEN */ "unable to open database file",
154177 /* SQLITE_PROTOCOL */ "locking protocol",
154178 /* SQLITE_EMPTY */ 0,
154179 /* SQLITE_SCHEMA */ "database schema has changed",
154180 /* SQLITE_TOOBIG */ "string or blob too big",
154181 /* SQLITE_CONSTRAINT */ "constraint failed",
154182 /* SQLITE_MISMATCH */ "datatype mismatch",
154183 /* SQLITE_MISUSE */ "bad parameter or other API misuse",
154184#ifdef SQLITE_DISABLE_LFS
154185 /* SQLITE_NOLFS */ "large file support is disabled",
154186#else
154187 /* SQLITE_NOLFS */ 0,
154188#endif
154189 /* SQLITE_AUTH */ "authorization denied",
154190 /* SQLITE_FORMAT */ 0,
154191 /* SQLITE_RANGE */ "column index out of range",
154192 /* SQLITE_NOTADB */ "file is not a database",
154193 /* SQLITE_NOTICE */ "notification message",
154194 /* SQLITE_WARNING */ "warning message",
154195 };
154196 const char *zErr = "unknown error";
154197 switch( rc ){
154198 case SQLITE_ABORT_ROLLBACK: {
154199 zErr = "abort due to ROLLBACK";
154200 break;
154201 }
154202 case SQLITE_ROW: {
154203 zErr = "another row available";
154204 break;
154205 }
154206 case SQLITE_DONE: {
154207 zErr = "no more rows available";
154208 break;
154209 }
154210 default: {
154211 rc &= 0xff;
154212 if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){
154213 zErr = aMsg[rc];
154214 }
154215 break;
154216 }
154217 }
154218 return zErr;
154219}
154220
154221/*
154222** This routine implements a busy callback that sleeps and tries
154223** again until a timeout value is reached. The timeout value is
154224** an integer number of milliseconds passed in as the first
154225** argument.
154226**
154227** Return non-zero to retry the lock. Return zero to stop trying
154228** and cause SQLite to return SQLITE_BUSY.
154229*/
154230static int sqliteDefaultBusyCallback(
154231 void *ptr, /* Database connection */
154232 int count, /* Number of times table has been busy */
154233 sqlite3_file *pFile /* The file on which the lock occurred */
154234){
154235#if SQLITE_OS_WIN || HAVE_USLEEP
154236 /* This case is for systems that have support for sleeping for fractions of
154237 ** a second. Examples: All windows systems, unix systems with usleep() */
154238 static const u8 delays[] =
154239 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
154240 static const u8 totals[] =
154241 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
154242# define NDELAY ArraySize(delays)
154243 sqlite3 *db = (sqlite3 *)ptr;
154244 int tmout = db->busyTimeout;
154245 int delay, prior;
154246
154247#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
154248 if( sqlite3OsFileControl(pFile,SQLITE_FCNTL_LOCK_TIMEOUT,&tmout)==SQLITE_OK ){
154249 if( count ){
154250 tmout = 0;
154251 sqlite3OsFileControl(pFile, SQLITE_FCNTL_LOCK_TIMEOUT, &tmout);
154252 return 0;
154253 }else{
154254 return 1;
154255 }
154256 }
154257#else
154258 UNUSED_PARAMETER(pFile);
154259#endif
154260 assert( count>=0 );
154261 if( count < NDELAY ){
154262 delay = delays[count];
154263 prior = totals[count];
154264 }else{
154265 delay = delays[NDELAY-1];
154266 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
154267 }
154268 if( prior + delay > tmout ){
154269 delay = tmout - prior;
154270 if( delay<=0 ) return 0;
154271 }
154272 sqlite3OsSleep(db->pVfs, delay*1000);
154273 return 1;
154274#else
154275 /* This case for unix systems that lack usleep() support. Sleeping
154276 ** must be done in increments of whole seconds */
154277 sqlite3 *db = (sqlite3 *)ptr;
154278 int tmout = ((sqlite3 *)ptr)->busyTimeout;
154279 UNUSED_PARAMETER(pFile);
154280 if( (count+1)*1000 > tmout ){
154281 return 0;
154282 }
154283 sqlite3OsSleep(db->pVfs, 1000000);
154284 return 1;
154285#endif
154286}
154287
154288/*
154289** Invoke the given busy handler.
154290**
154291** This routine is called when an operation failed to acquire a
154292** lock on VFS file pFile.
154293**
154294** If this routine returns non-zero, the lock is retried. If it
154295** returns 0, the operation aborts with an SQLITE_BUSY error.
154296*/
154297SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p, sqlite3_file *pFile){
154298 int rc;
154299 if( p->xBusyHandler==0 || p->nBusy<0 ) return 0;
154300 if( p->bExtraFileArg ){
154301 /* Add an extra parameter with the pFile pointer to the end of the
154302 ** callback argument list */
154303 int (*xTra)(void*,int,sqlite3_file*);
154304 xTra = (int(*)(void*,int,sqlite3_file*))p->xBusyHandler;
154305 rc = xTra(p->pBusyArg, p->nBusy, pFile);
154306 }else{
154307 /* Legacy style busy handler callback */
154308 rc = p->xBusyHandler(p->pBusyArg, p->nBusy);
154309 }
154310 if( rc==0 ){
154311 p->nBusy = -1;
154312 }else{
154313 p->nBusy++;
154314 }
154315 return rc;
154316}
154317
154318/*
154319** This routine sets the busy callback for an Sqlite database to the
154320** given callback function with the given argument.
154321*/
154322SQLITE_API int sqlite3_busy_handler(
154323 sqlite3 *db,
154324 int (*xBusy)(void*,int),
154325 void *pArg
154326){
154327#ifdef SQLITE_ENABLE_API_ARMOR
154328 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
154329#endif
154330 sqlite3_mutex_enter(db->mutex);
154331 db->busyHandler.xBusyHandler = xBusy;
154332 db->busyHandler.pBusyArg = pArg;
154333 db->busyHandler.nBusy = 0;
154334 db->busyHandler.bExtraFileArg = 0;
154335 db->busyTimeout = 0;
154336 sqlite3_mutex_leave(db->mutex);
154337 return SQLITE_OK;
154338}
154339
154340#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
154341/*
154342** This routine sets the progress callback for an Sqlite database to the
154343** given callback function with the given argument. The progress callback will
154344** be invoked every nOps opcodes.
154345*/
154346SQLITE_API void sqlite3_progress_handler(
154347 sqlite3 *db,
154348 int nOps,
154349 int (*xProgress)(void*),
154350 void *pArg
154351){
154352#ifdef SQLITE_ENABLE_API_ARMOR
154353 if( !sqlite3SafetyCheckOk(db) ){
154354 (void)SQLITE_MISUSE_BKPT;
154355 return;
154356 }
154357#endif
154358 sqlite3_mutex_enter(db->mutex);
154359 if( nOps>0 ){
154360 db->xProgress = xProgress;
154361 db->nProgressOps = (unsigned)nOps;
154362 db->pProgressArg = pArg;
154363 }else{
154364 db->xProgress = 0;
154365 db->nProgressOps = 0;
154366 db->pProgressArg = 0;
154367 }
154368 sqlite3_mutex_leave(db->mutex);
154369}
154370#endif
154371
154372
154373/*
154374** This routine installs a default busy handler that waits for the
154375** specified number of milliseconds before returning 0.
154376*/
154377SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
154378#ifdef SQLITE_ENABLE_API_ARMOR
154379 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
154380#endif
154381 if( ms>0 ){
154382 sqlite3_busy_handler(db, (int(*)(void*,int))sqliteDefaultBusyCallback,
154383 (void*)db);
154384 db->busyTimeout = ms;
154385 db->busyHandler.bExtraFileArg = 1;
154386 }else{
154387 sqlite3_busy_handler(db, 0, 0);
154388 }
154389 return SQLITE_OK;
154390}
154391
154392/*
154393** Cause any pending operation to stop at its earliest opportunity.
154394*/
154395SQLITE_API void sqlite3_interrupt(sqlite3 *db){
154396#ifdef SQLITE_ENABLE_API_ARMOR
154397 if( !sqlite3SafetyCheckOk(db) && (db==0 || db->magic!=SQLITE_MAGIC_ZOMBIE) ){
154398 (void)SQLITE_MISUSE_BKPT;
154399 return;
154400 }
154401#endif
154402 db->u1.isInterrupted = 1;
154403}
154404
154405
154406/*
154407** This function is exactly the same as sqlite3_create_function(), except
154408** that it is designed to be called by internal code. The difference is
154409** that if a malloc() fails in sqlite3_create_function(), an error code
154410** is returned and the mallocFailed flag cleared.
154411*/
154412SQLITE_PRIVATE int sqlite3CreateFunc(
154413 sqlite3 *db,
154414 const char *zFunctionName,
154415 int nArg,
154416 int enc,
154417 void *pUserData,
154418 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
154419 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
154420 void (*xFinal)(sqlite3_context*),
154421 void (*xValue)(sqlite3_context*),
154422 void (*xInverse)(sqlite3_context*,int,sqlite3_value **),
154423 FuncDestructor *pDestructor
154424){
154425 FuncDef *p;
154426 int nName;
154427 int extraFlags;
154428
154429 assert( sqlite3_mutex_held(db->mutex) );
154430 assert( xValue==0 || xSFunc==0 );
154431 if( zFunctionName==0 /* Must have a valid name */
154432 || (xSFunc!=0 && xFinal!=0) /* Not both xSFunc and xFinal */
154433 || ((xFinal==0)!=(xStep==0)) /* Both or neither of xFinal and xStep */
154434 || ((xValue==0)!=(xInverse==0)) /* Both or neither of xValue, xInverse */
154435 || (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG)
154436 || (255<(nName = sqlite3Strlen30( zFunctionName)))
154437 ){
154438 return SQLITE_MISUSE_BKPT;
154439 }
154440
154441 assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC );
154442 extraFlags = enc & SQLITE_DETERMINISTIC;
154443 enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY);
154444
154445#ifndef SQLITE_OMIT_UTF16
154446 /* If SQLITE_UTF16 is specified as the encoding type, transform this
154447 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
154448 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
154449 **
154450 ** If SQLITE_ANY is specified, add three versions of the function
154451 ** to the hash table.
154452 */
154453 if( enc==SQLITE_UTF16 ){
154454 enc = SQLITE_UTF16NATIVE;
154455 }else if( enc==SQLITE_ANY ){
154456 int rc;
154457 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8|extraFlags,
154458 pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor);
154459 if( rc==SQLITE_OK ){
154460 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE|extraFlags,
154461 pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor);
154462 }
154463 if( rc!=SQLITE_OK ){
154464 return rc;
154465 }
154466 enc = SQLITE_UTF16BE;
154467 }
154468#else
154469 enc = SQLITE_UTF8;
154470#endif
154471
154472 /* Check if an existing function is being overridden or deleted. If so,
154473 ** and there are active VMs, then return SQLITE_BUSY. If a function
154474 ** is being overridden/deleted but there are no active VMs, allow the
154475 ** operation to continue but invalidate all precompiled statements.
154476 */
154477 p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 0);
154478 if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==(u32)enc && p->nArg==nArg ){
154479 if( db->nVdbeActive ){
154480 sqlite3ErrorWithMsg(db, SQLITE_BUSY,
154481 "unable to delete/modify user-function due to active statements");
154482 assert( !db->mallocFailed );
154483 return SQLITE_BUSY;
154484 }else{
154485 sqlite3ExpirePreparedStatements(db, 0);
154486 }
154487 }
154488
154489 p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 1);
154490 assert(p || db->mallocFailed);
154491 if( !p ){
154492 return SQLITE_NOMEM_BKPT;
154493 }
154494
154495 /* If an older version of the function with a configured destructor is
154496 ** being replaced invoke the destructor function here. */
154497 functionDestroy(db, p);
154498
154499 if( pDestructor ){
154500 pDestructor->nRef++;
154501 }
154502 p->u.pDestructor = pDestructor;
154503 p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags;
154504 testcase( p->funcFlags & SQLITE_DETERMINISTIC );
154505 p->xSFunc = xSFunc ? xSFunc : xStep;
154506 p->xFinalize = xFinal;
154507 p->xValue = xValue;
154508 p->xInverse = xInverse;
154509 p->pUserData = pUserData;
154510 p->nArg = (u16)nArg;
154511 return SQLITE_OK;
154512}
154513
154514/*
154515** Worker function used by utf-8 APIs that create new functions:
154516**
154517** sqlite3_create_function()
154518** sqlite3_create_function_v2()
154519** sqlite3_create_window_function()
154520*/
154521static int createFunctionApi(
154522 sqlite3 *db,
154523 const char *zFunc,
154524 int nArg,
154525 int enc,
154526 void *p,
154527 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**),
154528 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
154529 void (*xFinal)(sqlite3_context*),
154530 void (*xValue)(sqlite3_context*),
154531 void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
154532 void(*xDestroy)(void*)
154533){
154534 int rc = SQLITE_ERROR;
154535 FuncDestructor *pArg = 0;
154536
154537#ifdef SQLITE_ENABLE_API_ARMOR
154538 if( !sqlite3SafetyCheckOk(db) ){
154539 return SQLITE_MISUSE_BKPT;
154540 }
154541#endif
154542 sqlite3_mutex_enter(db->mutex);
154543 if( xDestroy ){
154544 pArg = (FuncDestructor *)sqlite3Malloc(sizeof(FuncDestructor));
154545 if( !pArg ){
154546 sqlite3OomFault(db);
154547 xDestroy(p);
154548 goto out;
154549 }
154550 pArg->nRef = 0;
154551 pArg->xDestroy = xDestroy;
154552 pArg->pUserData = p;
154553 }
154554 rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p,
154555 xSFunc, xStep, xFinal, xValue, xInverse, pArg
154556 );
154557 if( pArg && pArg->nRef==0 ){
154558 assert( rc!=SQLITE_OK );
154559 xDestroy(p);
154560 sqlite3_free(pArg);
154561 }
154562
154563 out:
154564 rc = sqlite3ApiExit(db, rc);
154565 sqlite3_mutex_leave(db->mutex);
154566 return rc;
154567}
154568
154569/*
154570** Create new user functions.
154571*/
154572SQLITE_API int sqlite3_create_function(
154573 sqlite3 *db,
154574 const char *zFunc,
154575 int nArg,
154576 int enc,
154577 void *p,
154578 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
154579 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
154580 void (*xFinal)(sqlite3_context*)
154581){
154582 return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep,
154583 xFinal, 0, 0, 0);
154584}
154585SQLITE_API int sqlite3_create_function_v2(
154586 sqlite3 *db,
154587 const char *zFunc,
154588 int nArg,
154589 int enc,
154590 void *p,
154591 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
154592 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
154593 void (*xFinal)(sqlite3_context*),
154594 void (*xDestroy)(void *)
154595){
154596 return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep,
154597 xFinal, 0, 0, xDestroy);
154598}
154599SQLITE_API int sqlite3_create_window_function(
154600 sqlite3 *db,
154601 const char *zFunc,
154602 int nArg,
154603 int enc,
154604 void *p,
154605 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
154606 void (*xFinal)(sqlite3_context*),
154607 void (*xValue)(sqlite3_context*),
154608 void (*xInverse)(sqlite3_context*,int,sqlite3_value **),
154609 void (*xDestroy)(void *)
154610){
154611 return createFunctionApi(db, zFunc, nArg, enc, p, 0, xStep,
154612 xFinal, xValue, xInverse, xDestroy);
154613}
154614
154615#ifndef SQLITE_OMIT_UTF16
154616SQLITE_API int sqlite3_create_function16(
154617 sqlite3 *db,
154618 const void *zFunctionName,
154619 int nArg,
154620 int eTextRep,
154621 void *p,
154622 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**),
154623 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
154624 void (*xFinal)(sqlite3_context*)
154625){
154626 int rc;
154627 char *zFunc8;
154628
154629#ifdef SQLITE_ENABLE_API_ARMOR
154630 if( !sqlite3SafetyCheckOk(db) || zFunctionName==0 ) return SQLITE_MISUSE_BKPT;
154631#endif
154632 sqlite3_mutex_enter(db->mutex);
154633 assert( !db->mallocFailed );
154634 zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
154635 rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xSFunc,xStep,xFinal,0,0,0);
154636 sqlite3DbFree(db, zFunc8);
154637 rc = sqlite3ApiExit(db, rc);
154638 sqlite3_mutex_leave(db->mutex);
154639 return rc;
154640}
154641#endif
154642
154643
154644/*
154645** The following is the implementation of an SQL function that always
154646** fails with an error message stating that the function is used in the
154647** wrong context. The sqlite3_overload_function() API might construct
154648** SQL function that use this routine so that the functions will exist
154649** for name resolution but are actually overloaded by the xFindFunction
154650** method of virtual tables.
154651*/
154652static void sqlite3InvalidFunction(
154653 sqlite3_context *context, /* The function calling context */
154654 int NotUsed, /* Number of arguments to the function */
154655 sqlite3_value **NotUsed2 /* Value of each argument */
154656){
154657 const char *zName = (const char*)sqlite3_user_data(context);
154658 char *zErr;
154659 UNUSED_PARAMETER2(NotUsed, NotUsed2);
154660 zErr = sqlite3_mprintf(
154661 "unable to use function %s in the requested context", zName);
154662 sqlite3_result_error(context, zErr, -1);
154663 sqlite3_free(zErr);
154664}
154665
154666/*
154667** Declare that a function has been overloaded by a virtual table.
154668**
154669** If the function already exists as a regular global function, then
154670** this routine is a no-op. If the function does not exist, then create
154671** a new one that always throws a run-time error.
154672**
154673** When virtual tables intend to provide an overloaded function, they
154674** should call this routine to make sure the global function exists.
154675** A global function must exist in order for name resolution to work
154676** properly.
154677*/
154678SQLITE_API int sqlite3_overload_function(
154679 sqlite3 *db,
154680 const char *zName,
154681 int nArg
154682){
154683 int rc;
154684 char *zCopy;
154685
154686#ifdef SQLITE_ENABLE_API_ARMOR
154687 if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
154688 return SQLITE_MISUSE_BKPT;
154689 }
154690#endif
154691 sqlite3_mutex_enter(db->mutex);
154692 rc = sqlite3FindFunction(db, zName, nArg, SQLITE_UTF8, 0)!=0;
154693 sqlite3_mutex_leave(db->mutex);
154694 if( rc ) return SQLITE_OK;
154695 zCopy = sqlite3_mprintf(zName);
154696 if( zCopy==0 ) return SQLITE_NOMEM;
154697 return sqlite3_create_function_v2(db, zName, nArg, SQLITE_UTF8,
154698 zCopy, sqlite3InvalidFunction, 0, 0, sqlite3_free);
154699}
154700
154701#ifndef SQLITE_OMIT_TRACE
154702/*
154703** Register a trace function. The pArg from the previously registered trace
154704** is returned.
154705**
154706** A NULL trace function means that no tracing is executes. A non-NULL
154707** trace is a pointer to a function that is invoked at the start of each
154708** SQL statement.
154709*/
154710#ifndef SQLITE_OMIT_DEPRECATED
154711SQLITE_API void *sqlite3_trace(sqlite3 *db, void(*xTrace)(void*,const char*), void *pArg){
154712 void *pOld;
154713
154714#ifdef SQLITE_ENABLE_API_ARMOR
154715 if( !sqlite3SafetyCheckOk(db) ){
154716 (void)SQLITE_MISUSE_BKPT;
154717 return 0;
154718 }
154719#endif
154720 sqlite3_mutex_enter(db->mutex);
154721 pOld = db->pTraceArg;
154722 db->mTrace = xTrace ? SQLITE_TRACE_LEGACY : 0;
154723 db->xTrace = (int(*)(u32,void*,void*,void*))xTrace;
154724 db->pTraceArg = pArg;
154725 sqlite3_mutex_leave(db->mutex);
154726 return pOld;
154727}
154728#endif /* SQLITE_OMIT_DEPRECATED */
154729
154730/* Register a trace callback using the version-2 interface.
154731*/
154732SQLITE_API int sqlite3_trace_v2(
154733 sqlite3 *db, /* Trace this connection */
154734 unsigned mTrace, /* Mask of events to be traced */
154735 int(*xTrace)(unsigned,void*,void*,void*), /* Callback to invoke */
154736 void *pArg /* Context */
154737){
154738#ifdef SQLITE_ENABLE_API_ARMOR
154739 if( !sqlite3SafetyCheckOk(db) ){
154740 return SQLITE_MISUSE_BKPT;
154741 }
154742#endif
154743 sqlite3_mutex_enter(db->mutex);
154744 if( mTrace==0 ) xTrace = 0;
154745 if( xTrace==0 ) mTrace = 0;
154746 db->mTrace = mTrace;
154747 db->xTrace = xTrace;
154748 db->pTraceArg = pArg;
154749 sqlite3_mutex_leave(db->mutex);
154750 return SQLITE_OK;
154751}
154752
154753#ifndef SQLITE_OMIT_DEPRECATED
154754/*
154755** Register a profile function. The pArg from the previously registered
154756** profile function is returned.
154757**
154758** A NULL profile function means that no profiling is executes. A non-NULL
154759** profile is a pointer to a function that is invoked at the conclusion of
154760** each SQL statement that is run.
154761*/
154762SQLITE_API void *sqlite3_profile(
154763 sqlite3 *db,
154764 void (*xProfile)(void*,const char*,sqlite_uint64),
154765 void *pArg
154766){
154767 void *pOld;
154768
154769#ifdef SQLITE_ENABLE_API_ARMOR
154770 if( !sqlite3SafetyCheckOk(db) ){
154771 (void)SQLITE_MISUSE_BKPT;
154772 return 0;
154773 }
154774#endif
154775 sqlite3_mutex_enter(db->mutex);
154776 pOld = db->pProfileArg;
154777 db->xProfile = xProfile;
154778 db->pProfileArg = pArg;
154779 sqlite3_mutex_leave(db->mutex);
154780 return pOld;
154781}
154782#endif /* SQLITE_OMIT_DEPRECATED */
154783#endif /* SQLITE_OMIT_TRACE */
154784
154785/*
154786** Register a function to be invoked when a transaction commits.
154787** If the invoked function returns non-zero, then the commit becomes a
154788** rollback.
154789*/
154790SQLITE_API void *sqlite3_commit_hook(
154791 sqlite3 *db, /* Attach the hook to this database */
154792 int (*xCallback)(void*), /* Function to invoke on each commit */
154793 void *pArg /* Argument to the function */
154794){
154795 void *pOld;
154796
154797#ifdef SQLITE_ENABLE_API_ARMOR
154798 if( !sqlite3SafetyCheckOk(db) ){
154799 (void)SQLITE_MISUSE_BKPT;
154800 return 0;
154801 }
154802#endif
154803 sqlite3_mutex_enter(db->mutex);
154804 pOld = db->pCommitArg;
154805 db->xCommitCallback = xCallback;
154806 db->pCommitArg = pArg;
154807 sqlite3_mutex_leave(db->mutex);
154808 return pOld;
154809}
154810
154811/*
154812** Register a callback to be invoked each time a row is updated,
154813** inserted or deleted using this database connection.
154814*/
154815SQLITE_API void *sqlite3_update_hook(
154816 sqlite3 *db, /* Attach the hook to this database */
154817 void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
154818 void *pArg /* Argument to the function */
154819){
154820 void *pRet;
154821
154822#ifdef SQLITE_ENABLE_API_ARMOR
154823 if( !sqlite3SafetyCheckOk(db) ){
154824 (void)SQLITE_MISUSE_BKPT;
154825 return 0;
154826 }
154827#endif
154828 sqlite3_mutex_enter(db->mutex);
154829 pRet = db->pUpdateArg;
154830 db->xUpdateCallback = xCallback;
154831 db->pUpdateArg = pArg;
154832 sqlite3_mutex_leave(db->mutex);
154833 return pRet;
154834}
154835
154836/*
154837** Register a callback to be invoked each time a transaction is rolled
154838** back by this database connection.
154839*/
154840SQLITE_API void *sqlite3_rollback_hook(
154841 sqlite3 *db, /* Attach the hook to this database */
154842 void (*xCallback)(void*), /* Callback function */
154843 void *pArg /* Argument to the function */
154844){
154845 void *pRet;
154846
154847#ifdef SQLITE_ENABLE_API_ARMOR
154848 if( !sqlite3SafetyCheckOk(db) ){
154849 (void)SQLITE_MISUSE_BKPT;
154850 return 0;
154851 }
154852#endif
154853 sqlite3_mutex_enter(db->mutex);
154854 pRet = db->pRollbackArg;
154855 db->xRollbackCallback = xCallback;
154856 db->pRollbackArg = pArg;
154857 sqlite3_mutex_leave(db->mutex);
154858 return pRet;
154859}
154860
154861#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
154862/*
154863** Register a callback to be invoked each time a row is updated,
154864** inserted or deleted using this database connection.
154865*/
154866SQLITE_API void *sqlite3_preupdate_hook(
154867 sqlite3 *db, /* Attach the hook to this database */
154868 void(*xCallback)( /* Callback function */
154869 void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64),
154870 void *pArg /* First callback argument */
154871){
154872 void *pRet;
154873 sqlite3_mutex_enter(db->mutex);
154874 pRet = db->pPreUpdateArg;
154875 db->xPreUpdateCallback = xCallback;
154876 db->pPreUpdateArg = pArg;
154877 sqlite3_mutex_leave(db->mutex);
154878 return pRet;
154879}
154880#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
154881
154882#ifndef SQLITE_OMIT_WAL
154883/*
154884** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
154885** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
154886** is greater than sqlite3.pWalArg cast to an integer (the value configured by
154887** wal_autocheckpoint()).
154888*/
154889SQLITE_PRIVATE int sqlite3WalDefaultHook(
154890 void *pClientData, /* Argument */
154891 sqlite3 *db, /* Connection */
154892 const char *zDb, /* Database */
154893 int nFrame /* Size of WAL */
154894){
154895 if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){
154896 sqlite3BeginBenignMalloc();
154897 sqlite3_wal_checkpoint(db, zDb);
154898 sqlite3EndBenignMalloc();
154899 }
154900 return SQLITE_OK;
154901}
154902#endif /* SQLITE_OMIT_WAL */
154903
154904/*
154905** Configure an sqlite3_wal_hook() callback to automatically checkpoint
154906** a database after committing a transaction if there are nFrame or
154907** more frames in the log file. Passing zero or a negative value as the
154908** nFrame parameter disables automatic checkpoints entirely.
154909**
154910** The callback registered by this function replaces any existing callback
154911** registered using sqlite3_wal_hook(). Likewise, registering a callback
154912** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
154913** configured by this function.
154914*/
154915SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
154916#ifdef SQLITE_OMIT_WAL
154917 UNUSED_PARAMETER(db);
154918 UNUSED_PARAMETER(nFrame);
154919#else
154920#ifdef SQLITE_ENABLE_API_ARMOR
154921 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
154922#endif
154923 if( nFrame>0 ){
154924 sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
154925 }else{
154926 sqlite3_wal_hook(db, 0, 0);
154927 }
154928#endif
154929 return SQLITE_OK;
154930}
154931
154932/*
154933** Register a callback to be invoked each time a transaction is written
154934** into the write-ahead-log by this database connection.
154935*/
154936SQLITE_API void *sqlite3_wal_hook(
154937 sqlite3 *db, /* Attach the hook to this db handle */
154938 int(*xCallback)(void *, sqlite3*, const char*, int),
154939 void *pArg /* First argument passed to xCallback() */
154940){
154941#ifndef SQLITE_OMIT_WAL
154942 void *pRet;
154943#ifdef SQLITE_ENABLE_API_ARMOR
154944 if( !sqlite3SafetyCheckOk(db) ){
154945 (void)SQLITE_MISUSE_BKPT;
154946 return 0;
154947 }
154948#endif
154949 sqlite3_mutex_enter(db->mutex);
154950 pRet = db->pWalArg;
154951 db->xWalCallback = xCallback;
154952 db->pWalArg = pArg;
154953 sqlite3_mutex_leave(db->mutex);
154954 return pRet;
154955#else
154956 return 0;
154957#endif
154958}
154959
154960/*
154961** Checkpoint database zDb.
154962*/
154963SQLITE_API int sqlite3_wal_checkpoint_v2(
154964 sqlite3 *db, /* Database handle */
154965 const char *zDb, /* Name of attached database (or NULL) */
154966 int eMode, /* SQLITE_CHECKPOINT_* value */
154967 int *pnLog, /* OUT: Size of WAL log in frames */
154968 int *pnCkpt /* OUT: Total number of frames checkpointed */
154969){
154970#ifdef SQLITE_OMIT_WAL
154971 return SQLITE_OK;
154972#else
154973 int rc; /* Return code */
154974 int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */
154975
154976#ifdef SQLITE_ENABLE_API_ARMOR
154977 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
154978#endif
154979
154980 /* Initialize the output variables to -1 in case an error occurs. */
154981 if( pnLog ) *pnLog = -1;
154982 if( pnCkpt ) *pnCkpt = -1;
154983
154984 assert( SQLITE_CHECKPOINT_PASSIVE==0 );
154985 assert( SQLITE_CHECKPOINT_FULL==1 );
154986 assert( SQLITE_CHECKPOINT_RESTART==2 );
154987 assert( SQLITE_CHECKPOINT_TRUNCATE==3 );
154988 if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_TRUNCATE ){
154989 /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint
154990 ** mode: */
154991 return SQLITE_MISUSE;
154992 }
154993
154994 sqlite3_mutex_enter(db->mutex);
154995 if( zDb && zDb[0] ){
154996 iDb = sqlite3FindDbName(db, zDb);
154997 }
154998 if( iDb<0 ){
154999 rc = SQLITE_ERROR;
155000 sqlite3ErrorWithMsg(db, SQLITE_ERROR, "unknown database: %s", zDb);
155001 }else{
155002 db->busyHandler.nBusy = 0;
155003 rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
155004 sqlite3Error(db, rc);
155005 }
155006 rc = sqlite3ApiExit(db, rc);
155007
155008 /* If there are no active statements, clear the interrupt flag at this
155009 ** point. */
155010 if( db->nVdbeActive==0 ){
155011 db->u1.isInterrupted = 0;
155012 }
155013
155014 sqlite3_mutex_leave(db->mutex);
155015 return rc;
155016#endif
155017}
155018
155019
155020/*
155021** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
155022** to contains a zero-length string, all attached databases are
155023** checkpointed.
155024*/
155025SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
155026 /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to
155027 ** sqlite3_wal_checkpoint_v2(D,X,SQLITE_CHECKPOINT_PASSIVE,0,0). */
155028 return sqlite3_wal_checkpoint_v2(db,zDb,SQLITE_CHECKPOINT_PASSIVE,0,0);
155029}
155030
155031#ifndef SQLITE_OMIT_WAL
155032/*
155033** Run a checkpoint on database iDb. This is a no-op if database iDb is
155034** not currently open in WAL mode.
155035**
155036** If a transaction is open on the database being checkpointed, this
155037** function returns SQLITE_LOCKED and a checkpoint is not attempted. If
155038** an error occurs while running the checkpoint, an SQLite error code is
155039** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
155040**
155041** The mutex on database handle db should be held by the caller. The mutex
155042** associated with the specific b-tree being checkpointed is taken by
155043** this function while the checkpoint is running.
155044**
155045** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are
155046** checkpointed. If an error is encountered it is returned immediately -
155047** no attempt is made to checkpoint any remaining databases.
155048**
155049** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL, RESTART
155050** or TRUNCATE.
155051*/
155052SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
155053 int rc = SQLITE_OK; /* Return code */
155054 int i; /* Used to iterate through attached dbs */
155055 int bBusy = 0; /* True if SQLITE_BUSY has been encountered */
155056
155057 assert( sqlite3_mutex_held(db->mutex) );
155058 assert( !pnLog || *pnLog==-1 );
155059 assert( !pnCkpt || *pnCkpt==-1 );
155060
155061 for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
155062 if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){
155063 rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
155064 pnLog = 0;
155065 pnCkpt = 0;
155066 if( rc==SQLITE_BUSY ){
155067 bBusy = 1;
155068 rc = SQLITE_OK;
155069 }
155070 }
155071 }
155072
155073 return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc;
155074}
155075#endif /* SQLITE_OMIT_WAL */
155076
155077/*
155078** This function returns true if main-memory should be used instead of
155079** a temporary file for transient pager files and statement journals.
155080** The value returned depends on the value of db->temp_store (runtime
155081** parameter) and the compile time value of SQLITE_TEMP_STORE. The
155082** following table describes the relationship between these two values
155083** and this functions return value.
155084**
155085** SQLITE_TEMP_STORE db->temp_store Location of temporary database
155086** ----------------- -------------- ------------------------------
155087** 0 any file (return 0)
155088** 1 1 file (return 0)
155089** 1 2 memory (return 1)
155090** 1 0 file (return 0)
155091** 2 1 file (return 0)
155092** 2 2 memory (return 1)
155093** 2 0 memory (return 1)
155094** 3 any memory (return 1)
155095*/
155096SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3 *db){
155097#if SQLITE_TEMP_STORE==1
155098 return ( db->temp_store==2 );
155099#endif
155100#if SQLITE_TEMP_STORE==2
155101 return ( db->temp_store!=1 );
155102#endif
155103#if SQLITE_TEMP_STORE==3
155104 UNUSED_PARAMETER(db);
155105 return 1;
155106#endif
155107#if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
155108 UNUSED_PARAMETER(db);
155109 return 0;
155110#endif
155111}
155112
155113/*
155114** Return UTF-8 encoded English language explanation of the most recent
155115** error.
155116*/
155117SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){
155118 const char *z;
155119 if( !db ){
155120 return sqlite3ErrStr(SQLITE_NOMEM_BKPT);
155121 }
155122 if( !sqlite3SafetyCheckSickOrOk(db) ){
155123 return sqlite3ErrStr(SQLITE_MISUSE_BKPT);
155124 }
155125 sqlite3_mutex_enter(db->mutex);
155126 if( db->mallocFailed ){
155127 z = sqlite3ErrStr(SQLITE_NOMEM_BKPT);
155128 }else{
155129 testcase( db->pErr==0 );
155130 z = (char*)sqlite3_value_text(db->pErr);
155131 assert( !db->mallocFailed );
155132 if( z==0 ){
155133 z = sqlite3ErrStr(db->errCode);
155134 }
155135 }
155136 sqlite3_mutex_leave(db->mutex);
155137 return z;
155138}
155139
155140#ifndef SQLITE_OMIT_UTF16
155141/*
155142** Return UTF-16 encoded English language explanation of the most recent
155143** error.
155144*/
155145SQLITE_API const void *sqlite3_errmsg16(sqlite3 *db){
155146 static const u16 outOfMem[] = {
155147 'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
155148 };
155149 static const u16 misuse[] = {
155150 'b', 'a', 'd', ' ', 'p', 'a', 'r', 'a', 'm', 'e', 't', 'e', 'r', ' ',
155151 'o', 'r', ' ', 'o', 't', 'h', 'e', 'r', ' ', 'A', 'P', 'I', ' ',
155152 'm', 'i', 's', 'u', 's', 'e', 0
155153 };
155154
155155 const void *z;
155156 if( !db ){
155157 return (void *)outOfMem;
155158 }
155159 if( !sqlite3SafetyCheckSickOrOk(db) ){
155160 return (void *)misuse;
155161 }
155162 sqlite3_mutex_enter(db->mutex);
155163 if( db->mallocFailed ){
155164 z = (void *)outOfMem;
155165 }else{
155166 z = sqlite3_value_text16(db->pErr);
155167 if( z==0 ){
155168 sqlite3ErrorWithMsg(db, db->errCode, sqlite3ErrStr(db->errCode));
155169 z = sqlite3_value_text16(db->pErr);
155170 }
155171 /* A malloc() may have failed within the call to sqlite3_value_text16()
155172 ** above. If this is the case, then the db->mallocFailed flag needs to
155173 ** be cleared before returning. Do this directly, instead of via
155174 ** sqlite3ApiExit(), to avoid setting the database handle error message.
155175 */
155176 sqlite3OomClear(db);
155177 }
155178 sqlite3_mutex_leave(db->mutex);
155179 return z;
155180}
155181#endif /* SQLITE_OMIT_UTF16 */
155182
155183/*
155184** Return the most recent error code generated by an SQLite routine. If NULL is
155185** passed to this function, we assume a malloc() failed during sqlite3_open().
155186*/
155187SQLITE_API int sqlite3_errcode(sqlite3 *db){
155188 if( db && !sqlite3SafetyCheckSickOrOk(db) ){
155189 return SQLITE_MISUSE_BKPT;
155190 }
155191 if( !db || db->mallocFailed ){
155192 return SQLITE_NOMEM_BKPT;
155193 }
155194 return db->errCode & db->errMask;
155195}
155196SQLITE_API int sqlite3_extended_errcode(sqlite3 *db){
155197 if( db && !sqlite3SafetyCheckSickOrOk(db) ){
155198 return SQLITE_MISUSE_BKPT;
155199 }
155200 if( !db || db->mallocFailed ){
155201 return SQLITE_NOMEM_BKPT;
155202 }
155203 return db->errCode;
155204}
155205SQLITE_API int sqlite3_system_errno(sqlite3 *db){
155206 return db ? db->iSysErrno : 0;
155207}
155208
155209/*
155210** Return a string that describes the kind of error specified in the
155211** argument. For now, this simply calls the internal sqlite3ErrStr()
155212** function.
155213*/
155214SQLITE_API const char *sqlite3_errstr(int rc){
155215 return sqlite3ErrStr(rc);
155216}
155217
155218/*
155219** Create a new collating function for database "db". The name is zName
155220** and the encoding is enc.
155221*/
155222static int createCollation(
155223 sqlite3* db,
155224 const char *zName,
155225 u8 enc,
155226 void* pCtx,
155227 int(*xCompare)(void*,int,const void*,int,const void*),
155228 void(*xDel)(void*)
155229){
155230 CollSeq *pColl;
155231 int enc2;
155232
155233 assert( sqlite3_mutex_held(db->mutex) );
155234
155235 /* If SQLITE_UTF16 is specified as the encoding type, transform this
155236 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
155237 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
155238 */
155239 enc2 = enc;
155240 testcase( enc2==SQLITE_UTF16 );
155241 testcase( enc2==SQLITE_UTF16_ALIGNED );
155242 if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){
155243 enc2 = SQLITE_UTF16NATIVE;
155244 }
155245 if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){
155246 return SQLITE_MISUSE_BKPT;
155247 }
155248
155249 /* Check if this call is removing or replacing an existing collation
155250 ** sequence. If so, and there are active VMs, return busy. If there
155251 ** are no active VMs, invalidate any pre-compiled statements.
155252 */
155253 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
155254 if( pColl && pColl->xCmp ){
155255 if( db->nVdbeActive ){
155256 sqlite3ErrorWithMsg(db, SQLITE_BUSY,
155257 "unable to delete/modify collation sequence due to active statements");
155258 return SQLITE_BUSY;
155259 }
155260 sqlite3ExpirePreparedStatements(db, 0);
155261
155262 /* If collation sequence pColl was created directly by a call to
155263 ** sqlite3_create_collation, and not generated by synthCollSeq(),
155264 ** then any copies made by synthCollSeq() need to be invalidated.
155265 ** Also, collation destructor - CollSeq.xDel() - function may need
155266 ** to be called.
155267 */
155268 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
155269 CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName);
155270 int j;
155271 for(j=0; j<3; j++){
155272 CollSeq *p = &aColl[j];
155273 if( p->enc==pColl->enc ){
155274 if( p->xDel ){
155275 p->xDel(p->pUser);
155276 }
155277 p->xCmp = 0;
155278 }
155279 }
155280 }
155281 }
155282
155283 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
155284 if( pColl==0 ) return SQLITE_NOMEM_BKPT;
155285 pColl->xCmp = xCompare;
155286 pColl->pUser = pCtx;
155287 pColl->xDel = xDel;
155288 pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
155289 sqlite3Error(db, SQLITE_OK);
155290 return SQLITE_OK;
155291}
155292
155293
155294/*
155295** This array defines hard upper bounds on limit values. The
155296** initializer must be kept in sync with the SQLITE_LIMIT_*
155297** #defines in sqlite3.h.
155298*/
155299static const int aHardLimit[] = {
155300 SQLITE_MAX_LENGTH,
155301 SQLITE_MAX_SQL_LENGTH,
155302 SQLITE_MAX_COLUMN,
155303 SQLITE_MAX_EXPR_DEPTH,
155304 SQLITE_MAX_COMPOUND_SELECT,
155305 SQLITE_MAX_VDBE_OP,
155306 SQLITE_MAX_FUNCTION_ARG,
155307 SQLITE_MAX_ATTACHED,
155308 SQLITE_MAX_LIKE_PATTERN_LENGTH,
155309 SQLITE_MAX_VARIABLE_NUMBER, /* IMP: R-38091-32352 */
155310 SQLITE_MAX_TRIGGER_DEPTH,
155311 SQLITE_MAX_WORKER_THREADS,
155312};
155313
155314/*
155315** Make sure the hard limits are set to reasonable values
155316*/
155317#if SQLITE_MAX_LENGTH<100
155318# error SQLITE_MAX_LENGTH must be at least 100
155319#endif
155320#if SQLITE_MAX_SQL_LENGTH<100
155321# error SQLITE_MAX_SQL_LENGTH must be at least 100
155322#endif
155323#if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH
155324# error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH
155325#endif
155326#if SQLITE_MAX_COMPOUND_SELECT<2
155327# error SQLITE_MAX_COMPOUND_SELECT must be at least 2
155328#endif
155329#if SQLITE_MAX_VDBE_OP<40
155330# error SQLITE_MAX_VDBE_OP must be at least 40
155331#endif
155332#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>127
155333# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 127
155334#endif
155335#if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>125
155336# error SQLITE_MAX_ATTACHED must be between 0 and 125
155337#endif
155338#if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
155339# error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
155340#endif
155341#if SQLITE_MAX_COLUMN>32767
155342# error SQLITE_MAX_COLUMN must not exceed 32767
155343#endif
155344#if SQLITE_MAX_TRIGGER_DEPTH<1
155345# error SQLITE_MAX_TRIGGER_DEPTH must be at least 1
155346#endif
155347#if SQLITE_MAX_WORKER_THREADS<0 || SQLITE_MAX_WORKER_THREADS>50
155348# error SQLITE_MAX_WORKER_THREADS must be between 0 and 50
155349#endif
155350
155351
155352/*
155353** Change the value of a limit. Report the old value.
155354** If an invalid limit index is supplied, report -1.
155355** Make no changes but still report the old value if the
155356** new limit is negative.
155357**
155358** A new lower limit does not shrink existing constructs.
155359** It merely prevents new constructs that exceed the limit
155360** from forming.
155361*/
155362SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
155363 int oldLimit;
155364
155365#ifdef SQLITE_ENABLE_API_ARMOR
155366 if( !sqlite3SafetyCheckOk(db) ){
155367 (void)SQLITE_MISUSE_BKPT;
155368 return -1;
155369 }
155370#endif
155371
155372 /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
155373 ** there is a hard upper bound set at compile-time by a C preprocessor
155374 ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
155375 ** "_MAX_".)
155376 */
155377 assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH );
155378 assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH );
155379 assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN );
155380 assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH );
155381 assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT);
155382 assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP );
155383 assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG );
155384 assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED );
155385 assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]==
155386 SQLITE_MAX_LIKE_PATTERN_LENGTH );
155387 assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER);
155388 assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH );
155389 assert( aHardLimit[SQLITE_LIMIT_WORKER_THREADS]==SQLITE_MAX_WORKER_THREADS );
155390 assert( SQLITE_LIMIT_WORKER_THREADS==(SQLITE_N_LIMIT-1) );
155391
155392
155393 if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
155394 return -1;
155395 }
155396 oldLimit = db->aLimit[limitId];
155397 if( newLimit>=0 ){ /* IMP: R-52476-28732 */
155398 if( newLimit>aHardLimit[limitId] ){
155399 newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */
155400 }
155401 db->aLimit[limitId] = newLimit;
155402 }
155403 return oldLimit; /* IMP: R-53341-35419 */
155404}
155405
155406/*
155407** This function is used to parse both URIs and non-URI filenames passed by the
155408** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
155409** URIs specified as part of ATTACH statements.
155410**
155411** The first argument to this function is the name of the VFS to use (or
155412** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx"
155413** query parameter. The second argument contains the URI (or non-URI filename)
155414** itself. When this function is called the *pFlags variable should contain
155415** the default flags to open the database handle with. The value stored in
155416** *pFlags may be updated before returning if the URI filename contains
155417** "cache=xxx" or "mode=xxx" query parameters.
155418**
155419** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to
155420** the VFS that should be used to open the database file. *pzFile is set to
155421** point to a buffer containing the name of the file to open. It is the
155422** responsibility of the caller to eventually call sqlite3_free() to release
155423** this buffer.
155424**
155425** If an error occurs, then an SQLite error code is returned and *pzErrMsg
155426** may be set to point to a buffer containing an English language error
155427** message. It is the responsibility of the caller to eventually release
155428** this buffer by calling sqlite3_free().
155429*/
155430SQLITE_PRIVATE int sqlite3ParseUri(
155431 const char *zDefaultVfs, /* VFS to use if no "vfs=xxx" query option */
155432 const char *zUri, /* Nul-terminated URI to parse */
155433 unsigned int *pFlags, /* IN/OUT: SQLITE_OPEN_XXX flags */
155434 sqlite3_vfs **ppVfs, /* OUT: VFS to use */
155435 char **pzFile, /* OUT: Filename component of URI */
155436 char **pzErrMsg /* OUT: Error message (if rc!=SQLITE_OK) */
155437){
155438 int rc = SQLITE_OK;
155439 unsigned int flags = *pFlags;
155440 const char *zVfs = zDefaultVfs;
155441 char *zFile;
155442 char c;
155443 int nUri = sqlite3Strlen30(zUri);
155444
155445 assert( *pzErrMsg==0 );
155446
155447 if( ((flags & SQLITE_OPEN_URI) /* IMP: R-48725-32206 */
155448 || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */
155449 && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
155450 ){
155451 char *zOpt;
155452 int eState; /* Parser state when parsing URI */
155453 int iIn; /* Input character index */
155454 int iOut = 0; /* Output character index */
155455 u64 nByte = nUri+2; /* Bytes of space to allocate */
155456
155457 /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen
155458 ** method that there may be extra parameters following the file-name. */
155459 flags |= SQLITE_OPEN_URI;
155460
155461 for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
155462 zFile = sqlite3_malloc64(nByte);
155463 if( !zFile ) return SQLITE_NOMEM_BKPT;
155464
155465 iIn = 5;
155466#ifdef SQLITE_ALLOW_URI_AUTHORITY
155467 if( strncmp(zUri+5, "///", 3)==0 ){
155468 iIn = 7;
155469 /* The following condition causes URIs with five leading / characters
155470 ** like file://///host/path to be converted into UNCs like //host/path.
155471 ** The correct URI for that UNC has only two or four leading / characters
155472 ** file://host/path or file:////host/path. But 5 leading slashes is a
155473 ** common error, we are told, so we handle it as a special case. */
155474 if( strncmp(zUri+7, "///", 3)==0 ){ iIn++; }
155475 }else if( strncmp(zUri+5, "//localhost/", 12)==0 ){
155476 iIn = 16;
155477 }
155478#else
155479 /* Discard the scheme and authority segments of the URI. */
155480 if( zUri[5]=='/' && zUri[6]=='/' ){
155481 iIn = 7;
155482 while( zUri[iIn] && zUri[iIn]!='/' ) iIn++;
155483 if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){
155484 *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s",
155485 iIn-7, &zUri[7]);
155486 rc = SQLITE_ERROR;
155487 goto parse_uri_out;
155488 }
155489 }
155490#endif
155491
155492 /* Copy the filename and any query parameters into the zFile buffer.
155493 ** Decode %HH escape codes along the way.
155494 **
155495 ** Within this loop, variable eState may be set to 0, 1 or 2, depending
155496 ** on the parsing context. As follows:
155497 **
155498 ** 0: Parsing file-name.
155499 ** 1: Parsing name section of a name=value query parameter.
155500 ** 2: Parsing value section of a name=value query parameter.
155501 */
155502 eState = 0;
155503 while( (c = zUri[iIn])!=0 && c!='#' ){
155504 iIn++;
155505 if( c=='%'
155506 && sqlite3Isxdigit(zUri[iIn])
155507 && sqlite3Isxdigit(zUri[iIn+1])
155508 ){
155509 int octet = (sqlite3HexToInt(zUri[iIn++]) << 4);
155510 octet += sqlite3HexToInt(zUri[iIn++]);
155511
155512 assert( octet>=0 && octet<256 );
155513 if( octet==0 ){
155514#ifndef SQLITE_ENABLE_URI_00_ERROR
155515 /* This branch is taken when "%00" appears within the URI. In this
155516 ** case we ignore all text in the remainder of the path, name or
155517 ** value currently being parsed. So ignore the current character
155518 ** and skip to the next "?", "=" or "&", as appropriate. */
155519 while( (c = zUri[iIn])!=0 && c!='#'
155520 && (eState!=0 || c!='?')
155521 && (eState!=1 || (c!='=' && c!='&'))
155522 && (eState!=2 || c!='&')
155523 ){
155524 iIn++;
155525 }
155526 continue;
155527#else
155528 /* If ENABLE_URI_00_ERROR is defined, "%00" in a URI is an error. */
155529 *pzErrMsg = sqlite3_mprintf("unexpected %%00 in uri");
155530 rc = SQLITE_ERROR;
155531 goto parse_uri_out;
155532#endif
155533 }
155534 c = octet;
155535 }else if( eState==1 && (c=='&' || c=='=') ){
155536 if( zFile[iOut-1]==0 ){
155537 /* An empty option name. Ignore this option altogether. */
155538 while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
155539 continue;
155540 }
155541 if( c=='&' ){
155542 zFile[iOut++] = '\0';
155543 }else{
155544 eState = 2;
155545 }
155546 c = 0;
155547 }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){
155548 c = 0;
155549 eState = 1;
155550 }
155551 zFile[iOut++] = c;
155552 }
155553 if( eState==1 ) zFile[iOut++] = '\0';
155554 zFile[iOut++] = '\0';
155555 zFile[iOut++] = '\0';
155556
155557 /* Check if there were any options specified that should be interpreted
155558 ** here. Options that are interpreted here include "vfs" and those that
155559 ** correspond to flags that may be passed to the sqlite3_open_v2()
155560 ** method. */
155561 zOpt = &zFile[sqlite3Strlen30(zFile)+1];
155562 while( zOpt[0] ){
155563 int nOpt = sqlite3Strlen30(zOpt);
155564 char *zVal = &zOpt[nOpt+1];
155565 int nVal = sqlite3Strlen30(zVal);
155566
155567 if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){
155568 zVfs = zVal;
155569 }else{
155570 struct OpenMode {
155571 const char *z;
155572 int mode;
155573 } *aMode = 0;
155574 char *zModeType = 0;
155575 int mask = 0;
155576 int limit = 0;
155577
155578 if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){
155579 static struct OpenMode aCacheMode[] = {
155580 { "shared", SQLITE_OPEN_SHAREDCACHE },
155581 { "private", SQLITE_OPEN_PRIVATECACHE },
155582 { 0, 0 }
155583 };
155584
155585 mask = SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_PRIVATECACHE;
155586 aMode = aCacheMode;
155587 limit = mask;
155588 zModeType = "cache";
155589 }
155590 if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){
155591 static struct OpenMode aOpenMode[] = {
155592 { "ro", SQLITE_OPEN_READONLY },
155593 { "rw", SQLITE_OPEN_READWRITE },
155594 { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
155595 { "memory", SQLITE_OPEN_MEMORY },
155596 { 0, 0 }
155597 };
155598
155599 mask = SQLITE_OPEN_READONLY | SQLITE_OPEN_READWRITE
155600 | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY;
155601 aMode = aOpenMode;
155602 limit = mask & flags;
155603 zModeType = "access";
155604 }
155605
155606 if( aMode ){
155607 int i;
155608 int mode = 0;
155609 for(i=0; aMode[i].z; i++){
155610 const char *z = aMode[i].z;
155611 if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){
155612 mode = aMode[i].mode;
155613 break;
155614 }
155615 }
155616 if( mode==0 ){
155617 *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal);
155618 rc = SQLITE_ERROR;
155619 goto parse_uri_out;
155620 }
155621 if( (mode & ~SQLITE_OPEN_MEMORY)>limit ){
155622 *pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s",
155623 zModeType, zVal);
155624 rc = SQLITE_PERM;
155625 goto parse_uri_out;
155626 }
155627 flags = (flags & ~mask) | mode;
155628 }
155629 }
155630
155631 zOpt = &zVal[nVal+1];
155632 }
155633
155634 }else{
155635 zFile = sqlite3_malloc64(nUri+2);
155636 if( !zFile ) return SQLITE_NOMEM_BKPT;
155637 if( nUri ){
155638 memcpy(zFile, zUri, nUri);
155639 }
155640 zFile[nUri] = '\0';
155641 zFile[nUri+1] = '\0';
155642 flags &= ~SQLITE_OPEN_URI;
155643 }
155644
155645 *ppVfs = sqlite3_vfs_find(zVfs);
155646 if( *ppVfs==0 ){
155647 *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs);
155648 rc = SQLITE_ERROR;
155649 }
155650 parse_uri_out:
155651 if( rc!=SQLITE_OK ){
155652 sqlite3_free(zFile);
155653 zFile = 0;
155654 }
155655 *pFlags = flags;
155656 *pzFile = zFile;
155657 return rc;
155658}
155659
155660
155661/*
155662** This routine does the work of opening a database on behalf of
155663** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
155664** is UTF-8 encoded.
155665*/
155666static int openDatabase(
155667 const char *zFilename, /* Database filename UTF-8 encoded */
155668 sqlite3 **ppDb, /* OUT: Returned database handle */
155669 unsigned int flags, /* Operational flags */
155670 const char *zVfs /* Name of the VFS to use */
155671){
155672 sqlite3 *db; /* Store allocated handle here */
155673 int rc; /* Return code */
155674 int isThreadsafe; /* True for threadsafe connections */
155675 char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */
155676 char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */
155677
155678#ifdef SQLITE_ENABLE_API_ARMOR
155679 if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
155680#endif
155681 *ppDb = 0;
155682#ifndef SQLITE_OMIT_AUTOINIT
155683 rc = sqlite3_initialize();
155684 if( rc ) return rc;
155685#endif
155686
155687 if( sqlite3GlobalConfig.bCoreMutex==0 ){
155688 isThreadsafe = 0;
155689 }else if( flags & SQLITE_OPEN_NOMUTEX ){
155690 isThreadsafe = 0;
155691 }else if( flags & SQLITE_OPEN_FULLMUTEX ){
155692 isThreadsafe = 1;
155693 }else{
155694 isThreadsafe = sqlite3GlobalConfig.bFullMutex;
155695 }
155696
155697 if( flags & SQLITE_OPEN_PRIVATECACHE ){
155698 flags &= ~SQLITE_OPEN_SHAREDCACHE;
155699 }else if( sqlite3GlobalConfig.sharedCacheEnabled ){
155700 flags |= SQLITE_OPEN_SHAREDCACHE;
155701 }
155702
155703 /* Remove harmful bits from the flags parameter
155704 **
155705 ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
155706 ** dealt with in the previous code block. Besides these, the only
155707 ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
155708 ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE,
155709 ** SQLITE_OPEN_PRIVATECACHE, and some reserved bits. Silently mask
155710 ** off all other flags.
155711 */
155712 flags &= ~( SQLITE_OPEN_DELETEONCLOSE |
155713 SQLITE_OPEN_EXCLUSIVE |
155714 SQLITE_OPEN_MAIN_DB |
155715 SQLITE_OPEN_TEMP_DB |
155716 SQLITE_OPEN_TRANSIENT_DB |
155717 SQLITE_OPEN_MAIN_JOURNAL |
155718 SQLITE_OPEN_TEMP_JOURNAL |
155719 SQLITE_OPEN_SUBJOURNAL |
155720 SQLITE_OPEN_MASTER_JOURNAL |
155721 SQLITE_OPEN_NOMUTEX |
155722 SQLITE_OPEN_FULLMUTEX |
155723 SQLITE_OPEN_WAL
155724 );
155725
155726 /* Allocate the sqlite data structure */
155727 db = sqlite3MallocZero( sizeof(sqlite3) );
155728 if( db==0 ) goto opendb_out;
155729 if( isThreadsafe
155730#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
155731 || sqlite3GlobalConfig.bCoreMutex
155732#endif
155733 ){
155734 db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
155735 if( db->mutex==0 ){
155736 sqlite3_free(db);
155737 db = 0;
155738 goto opendb_out;
155739 }
155740 if( isThreadsafe==0 ){
155741 sqlite3MutexWarnOnContention(db->mutex);
155742 }
155743 }
155744 sqlite3_mutex_enter(db->mutex);
155745 db->errMask = 0xff;
155746 db->nDb = 2;
155747 db->magic = SQLITE_MAGIC_BUSY;
155748 db->aDb = db->aDbStatic;
155749 db->lookaside.bDisable = 1;
155750
155751 assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
155752 memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
155753 db->aLimit[SQLITE_LIMIT_WORKER_THREADS] = SQLITE_DEFAULT_WORKER_THREADS;
155754 db->autoCommit = 1;
155755 db->nextAutovac = -1;
155756 db->szMmap = sqlite3GlobalConfig.szMmap;
155757 db->nextPagesize = 0;
155758 db->nMaxSorterMmap = 0x7FFFFFFF;
155759 db->flags |= SQLITE_ShortColNames | SQLITE_EnableTrigger | SQLITE_CacheSpill
155760#if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX
155761 | SQLITE_AutoIndex
155762#endif
155763#if SQLITE_DEFAULT_CKPTFULLFSYNC
155764 | SQLITE_CkptFullFSync
155765#endif
155766#if SQLITE_DEFAULT_FILE_FORMAT<4
155767 | SQLITE_LegacyFileFmt
155768#endif
155769#ifdef SQLITE_ENABLE_LOAD_EXTENSION
155770 | SQLITE_LoadExtension
155771#endif
155772#if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
155773 | SQLITE_RecTriggers
155774#endif
155775#if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS
155776 | SQLITE_ForeignKeys
155777#endif
155778#if defined(SQLITE_REVERSE_UNORDERED_SELECTS)
155779 | SQLITE_ReverseOrder
155780#endif
155781#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
155782 | SQLITE_CellSizeCk
155783#endif
155784#if defined(SQLITE_ENABLE_FTS3_TOKENIZER)
155785 | SQLITE_Fts3Tokenizer
155786#endif
155787#if defined(SQLITE_ENABLE_QPSG)
155788 | SQLITE_EnableQPSG
155789#endif
155790#if defined(SQLITE_DEFAULT_DEFENSIVE)
155791 | SQLITE_Defensive
155792#endif
155793 ;
155794 sqlite3HashInit(&db->aCollSeq);
155795#ifndef SQLITE_OMIT_VIRTUALTABLE
155796 sqlite3HashInit(&db->aModule);
155797#endif
155798
155799 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
155800 ** and UTF-16, so add a version for each to avoid any unnecessary
155801 ** conversions. The only error that can occur here is a malloc() failure.
155802 **
155803 ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating
155804 ** functions:
155805 */
155806 createCollation(db, sqlite3StrBINARY, SQLITE_UTF8, 0, binCollFunc, 0);
155807 createCollation(db, sqlite3StrBINARY, SQLITE_UTF16BE, 0, binCollFunc, 0);
155808 createCollation(db, sqlite3StrBINARY, SQLITE_UTF16LE, 0, binCollFunc, 0);
155809 createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
155810 createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0);
155811 if( db->mallocFailed ){
155812 goto opendb_out;
155813 }
155814 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
155815 ** strings is BINARY.
155816 */
155817 db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, sqlite3StrBINARY, 0);
155818 assert( db->pDfltColl!=0 );
155819
155820 /* Parse the filename/URI argument
155821 **
155822 ** Only allow sensible combinations of bits in the flags argument.
155823 ** Throw an error if any non-sense combination is used. If we
155824 ** do not block illegal combinations here, it could trigger
155825 ** assert() statements in deeper layers. Sensible combinations
155826 ** are:
155827 **
155828 ** 1: SQLITE_OPEN_READONLY
155829 ** 2: SQLITE_OPEN_READWRITE
155830 ** 6: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
155831 */
155832 db->openFlags = flags;
155833 assert( SQLITE_OPEN_READONLY == 0x01 );
155834 assert( SQLITE_OPEN_READWRITE == 0x02 );
155835 assert( SQLITE_OPEN_CREATE == 0x04 );
155836 testcase( (1<<(flags&7))==0x02 ); /* READONLY */
155837 testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
155838 testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
155839 if( ((1<<(flags&7)) & 0x46)==0 ){
155840 rc = SQLITE_MISUSE_BKPT; /* IMP: R-65497-44594 */
155841 }else{
155842 rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
155843 }
155844 if( rc!=SQLITE_OK ){
155845 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
155846 sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
155847 sqlite3_free(zErrMsg);
155848 goto opendb_out;
155849 }
155850
155851 /* Open the backend database driver */
155852 rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
155853 flags | SQLITE_OPEN_MAIN_DB);
155854 if( rc!=SQLITE_OK ){
155855 if( rc==SQLITE_IOERR_NOMEM ){
155856 rc = SQLITE_NOMEM_BKPT;
155857 }
155858 sqlite3Error(db, rc);
155859 goto opendb_out;
155860 }
155861 sqlite3BtreeEnter(db->aDb[0].pBt);
155862 db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
155863 if( !db->mallocFailed ) ENC(db) = SCHEMA_ENC(db);
155864 sqlite3BtreeLeave(db->aDb[0].pBt);
155865 db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
155866
155867 /* The default safety_level for the main database is FULL; for the temp
155868 ** database it is OFF. This matches the pager layer defaults.
155869 */
155870 db->aDb[0].zDbSName = "main";
155871 db->aDb[0].safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
155872 db->aDb[1].zDbSName = "temp";
155873 db->aDb[1].safety_level = PAGER_SYNCHRONOUS_OFF;
155874
155875 db->magic = SQLITE_MAGIC_OPEN;
155876 if( db->mallocFailed ){
155877 goto opendb_out;
155878 }
155879
155880 /* Register all built-in functions, but do not attempt to read the
155881 ** database schema yet. This is delayed until the first time the database
155882 ** is accessed.
155883 */
155884 sqlite3Error(db, SQLITE_OK);
155885 sqlite3RegisterPerConnectionBuiltinFunctions(db);
155886 rc = sqlite3_errcode(db);
155887
155888#ifdef SQLITE_ENABLE_FTS5
155889 /* Register any built-in FTS5 module before loading the automatic
155890 ** extensions. This allows automatic extensions to register FTS5
155891 ** tokenizers and auxiliary functions. */
155892 if( !db->mallocFailed && rc==SQLITE_OK ){
155893 rc = sqlite3Fts5Init(db);
155894 }
155895#endif
155896
155897 /* Load automatic extensions - extensions that have been registered
155898 ** using the sqlite3_automatic_extension() API.
155899 */
155900 if( rc==SQLITE_OK ){
155901 sqlite3AutoLoadExtensions(db);
155902 rc = sqlite3_errcode(db);
155903 if( rc!=SQLITE_OK ){
155904 goto opendb_out;
155905 }
155906 }
155907
155908#ifdef SQLITE_ENABLE_FTS1
155909 if( !db->mallocFailed ){
155910 extern int sqlite3Fts1Init(sqlite3*);
155911 rc = sqlite3Fts1Init(db);
155912 }
155913#endif
155914
155915#ifdef SQLITE_ENABLE_FTS2
155916 if( !db->mallocFailed && rc==SQLITE_OK ){
155917 extern int sqlite3Fts2Init(sqlite3*);
155918 rc = sqlite3Fts2Init(db);
155919 }
155920#endif
155921
155922#ifdef SQLITE_ENABLE_FTS3 /* automatically defined by SQLITE_ENABLE_FTS4 */
155923 if( !db->mallocFailed && rc==SQLITE_OK ){
155924 rc = sqlite3Fts3Init(db);
155925 }
155926#endif
155927
155928#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS)
155929 if( !db->mallocFailed && rc==SQLITE_OK ){
155930 rc = sqlite3IcuInit(db);
155931 }
155932#endif
155933
155934#ifdef SQLITE_ENABLE_RTREE
155935 if( !db->mallocFailed && rc==SQLITE_OK){
155936 rc = sqlite3RtreeInit(db);
155937 }
155938#endif
155939
155940#ifdef SQLITE_ENABLE_DBPAGE_VTAB
155941 if( !db->mallocFailed && rc==SQLITE_OK){
155942 rc = sqlite3DbpageRegister(db);
155943 }
155944#endif
155945
155946#ifdef SQLITE_ENABLE_DBSTAT_VTAB
155947 if( !db->mallocFailed && rc==SQLITE_OK){
155948 rc = sqlite3DbstatRegister(db);
155949 }
155950#endif
155951
155952#ifdef SQLITE_ENABLE_JSON1
155953 if( !db->mallocFailed && rc==SQLITE_OK){
155954 rc = sqlite3Json1Init(db);
155955 }
155956#endif
155957
155958#ifdef SQLITE_ENABLE_STMTVTAB
155959 if( !db->mallocFailed && rc==SQLITE_OK){
155960 rc = sqlite3StmtVtabInit(db);
155961 }
155962#endif
155963
155964 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
155965 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
155966 ** mode. Doing nothing at all also makes NORMAL the default.
155967 */
155968#ifdef SQLITE_DEFAULT_LOCKING_MODE
155969 db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
155970 sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
155971 SQLITE_DEFAULT_LOCKING_MODE);
155972#endif
155973
155974 if( rc ) sqlite3Error(db, rc);
155975
155976 /* Enable the lookaside-malloc subsystem */
155977 setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
155978 sqlite3GlobalConfig.nLookaside);
155979
155980 sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
155981
155982opendb_out:
155983 if( db ){
155984 assert( db->mutex!=0 || isThreadsafe==0
155985 || sqlite3GlobalConfig.bFullMutex==0 );
155986 sqlite3_mutex_leave(db->mutex);
155987 }
155988 rc = sqlite3_errcode(db);
155989 assert( db!=0 || rc==SQLITE_NOMEM );
155990 if( rc==SQLITE_NOMEM ){
155991 sqlite3_close(db);
155992 db = 0;
155993 }else if( rc!=SQLITE_OK ){
155994 db->magic = SQLITE_MAGIC_SICK;
155995 }
155996 *ppDb = db;
155997#ifdef SQLITE_ENABLE_SQLLOG
155998 if( sqlite3GlobalConfig.xSqllog ){
155999 /* Opening a db handle. Fourth parameter is passed 0. */
156000 void *pArg = sqlite3GlobalConfig.pSqllogArg;
156001 sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0);
156002 }
156003#endif
156004#if defined(SQLITE_HAS_CODEC)
156005 if( rc==SQLITE_OK ){
156006 const char *zKey;
156007 if( (zKey = sqlite3_uri_parameter(zOpen, "hexkey"))!=0 && zKey[0] ){
156008 u8 iByte;
156009 int i;
156010 char zDecoded[40];
156011 for(i=0, iByte=0; i<sizeof(zDecoded)*2 && sqlite3Isxdigit(zKey[i]); i++){
156012 iByte = (iByte<<4) + sqlite3HexToInt(zKey[i]);
156013 if( (i&1)!=0 ) zDecoded[i/2] = iByte;
156014 }
156015 sqlite3_key_v2(db, 0, zDecoded, i/2);
156016 }else if( (zKey = sqlite3_uri_parameter(zOpen, "key"))!=0 ){
156017 sqlite3_key_v2(db, 0, zKey, sqlite3Strlen30(zKey));
156018 }
156019 }
156020#endif
156021 sqlite3_free(zOpen);
156022 return rc & 0xff;
156023}
156024
156025/*
156026** Open a new database handle.
156027*/
156028SQLITE_API int sqlite3_open(
156029 const char *zFilename,
156030 sqlite3 **ppDb
156031){
156032 return openDatabase(zFilename, ppDb,
156033 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
156034}
156035SQLITE_API int sqlite3_open_v2(
156036 const char *filename, /* Database filename (UTF-8) */
156037 sqlite3 **ppDb, /* OUT: SQLite db handle */
156038 int flags, /* Flags */
156039 const char *zVfs /* Name of VFS module to use */
156040){
156041 return openDatabase(filename, ppDb, (unsigned int)flags, zVfs);
156042}
156043
156044#ifndef SQLITE_OMIT_UTF16
156045/*
156046** Open a new database handle.
156047*/
156048SQLITE_API int sqlite3_open16(
156049 const void *zFilename,
156050 sqlite3 **ppDb
156051){
156052 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
156053 sqlite3_value *pVal;
156054 int rc;
156055
156056#ifdef SQLITE_ENABLE_API_ARMOR
156057 if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
156058#endif
156059 *ppDb = 0;
156060#ifndef SQLITE_OMIT_AUTOINIT
156061 rc = sqlite3_initialize();
156062 if( rc ) return rc;
156063#endif
156064 if( zFilename==0 ) zFilename = "\000\000";
156065 pVal = sqlite3ValueNew(0);
156066 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
156067 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
156068 if( zFilename8 ){
156069 rc = openDatabase(zFilename8, ppDb,
156070 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
156071 assert( *ppDb || rc==SQLITE_NOMEM );
156072 if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
156073 SCHEMA_ENC(*ppDb) = ENC(*ppDb) = SQLITE_UTF16NATIVE;
156074 }
156075 }else{
156076 rc = SQLITE_NOMEM_BKPT;
156077 }
156078 sqlite3ValueFree(pVal);
156079
156080 return rc & 0xff;
156081}
156082#endif /* SQLITE_OMIT_UTF16 */
156083
156084/*
156085** Register a new collation sequence with the database handle db.
156086*/
156087SQLITE_API int sqlite3_create_collation(
156088 sqlite3* db,
156089 const char *zName,
156090 int enc,
156091 void* pCtx,
156092 int(*xCompare)(void*,int,const void*,int,const void*)
156093){
156094 return sqlite3_create_collation_v2(db, zName, enc, pCtx, xCompare, 0);
156095}
156096
156097/*
156098** Register a new collation sequence with the database handle db.
156099*/
156100SQLITE_API int sqlite3_create_collation_v2(
156101 sqlite3* db,
156102 const char *zName,
156103 int enc,
156104 void* pCtx,
156105 int(*xCompare)(void*,int,const void*,int,const void*),
156106 void(*xDel)(void*)
156107){
156108 int rc;
156109
156110#ifdef SQLITE_ENABLE_API_ARMOR
156111 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
156112#endif
156113 sqlite3_mutex_enter(db->mutex);
156114 assert( !db->mallocFailed );
156115 rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel);
156116 rc = sqlite3ApiExit(db, rc);
156117 sqlite3_mutex_leave(db->mutex);
156118 return rc;
156119}
156120
156121#ifndef SQLITE_OMIT_UTF16
156122/*
156123** Register a new collation sequence with the database handle db.
156124*/
156125SQLITE_API int sqlite3_create_collation16(
156126 sqlite3* db,
156127 const void *zName,
156128 int enc,
156129 void* pCtx,
156130 int(*xCompare)(void*,int,const void*,int,const void*)
156131){
156132 int rc = SQLITE_OK;
156133 char *zName8;
156134
156135#ifdef SQLITE_ENABLE_API_ARMOR
156136 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
156137#endif
156138 sqlite3_mutex_enter(db->mutex);
156139 assert( !db->mallocFailed );
156140 zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
156141 if( zName8 ){
156142 rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0);
156143 sqlite3DbFree(db, zName8);
156144 }
156145 rc = sqlite3ApiExit(db, rc);
156146 sqlite3_mutex_leave(db->mutex);
156147 return rc;
156148}
156149#endif /* SQLITE_OMIT_UTF16 */
156150
156151/*
156152** Register a collation sequence factory callback with the database handle
156153** db. Replace any previously installed collation sequence factory.
156154*/
156155SQLITE_API int sqlite3_collation_needed(
156156 sqlite3 *db,
156157 void *pCollNeededArg,
156158 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
156159){
156160#ifdef SQLITE_ENABLE_API_ARMOR
156161 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
156162#endif
156163 sqlite3_mutex_enter(db->mutex);
156164 db->xCollNeeded = xCollNeeded;
156165 db->xCollNeeded16 = 0;
156166 db->pCollNeededArg = pCollNeededArg;
156167 sqlite3_mutex_leave(db->mutex);
156168 return SQLITE_OK;
156169}
156170
156171#ifndef SQLITE_OMIT_UTF16
156172/*
156173** Register a collation sequence factory callback with the database handle
156174** db. Replace any previously installed collation sequence factory.
156175*/
156176SQLITE_API int sqlite3_collation_needed16(
156177 sqlite3 *db,
156178 void *pCollNeededArg,
156179 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
156180){
156181#ifdef SQLITE_ENABLE_API_ARMOR
156182 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
156183#endif
156184 sqlite3_mutex_enter(db->mutex);
156185 db->xCollNeeded = 0;
156186 db->xCollNeeded16 = xCollNeeded16;
156187 db->pCollNeededArg = pCollNeededArg;
156188 sqlite3_mutex_leave(db->mutex);
156189 return SQLITE_OK;
156190}
156191#endif /* SQLITE_OMIT_UTF16 */
156192
156193#ifndef SQLITE_OMIT_DEPRECATED
156194/*
156195** This function is now an anachronism. It used to be used to recover from a
156196** malloc() failure, but SQLite now does this automatically.
156197*/
156198SQLITE_API int sqlite3_global_recover(void){
156199 return SQLITE_OK;
156200}
156201#endif
156202
156203/*
156204** Test to see whether or not the database connection is in autocommit
156205** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on
156206** by default. Autocommit is disabled by a BEGIN statement and reenabled
156207** by the next COMMIT or ROLLBACK.
156208*/
156209SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
156210#ifdef SQLITE_ENABLE_API_ARMOR
156211 if( !sqlite3SafetyCheckOk(db) ){
156212 (void)SQLITE_MISUSE_BKPT;
156213 return 0;
156214 }
156215#endif
156216 return db->autoCommit;
156217}
156218
156219/*
156220** The following routines are substitutes for constants SQLITE_CORRUPT,
156221** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_NOMEM and possibly other error
156222** constants. They serve two purposes:
156223**
156224** 1. Serve as a convenient place to set a breakpoint in a debugger
156225** to detect when version error conditions occurs.
156226**
156227** 2. Invoke sqlite3_log() to provide the source code location where
156228** a low-level error is first detected.
156229*/
156230SQLITE_PRIVATE int sqlite3ReportError(int iErr, int lineno, const char *zType){
156231 sqlite3_log(iErr, "%s at line %d of [%.10s]",
156232 zType, lineno, 20+sqlite3_sourceid());
156233 return iErr;
156234}
156235SQLITE_PRIVATE int sqlite3CorruptError(int lineno){
156236 testcase( sqlite3GlobalConfig.xLog!=0 );
156237 return sqlite3ReportError(SQLITE_CORRUPT, lineno, "database corruption");
156238}
156239SQLITE_PRIVATE int sqlite3MisuseError(int lineno){
156240 testcase( sqlite3GlobalConfig.xLog!=0 );
156241 return sqlite3ReportError(SQLITE_MISUSE, lineno, "misuse");
156242}
156243SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
156244 testcase( sqlite3GlobalConfig.xLog!=0 );
156245 return sqlite3ReportError(SQLITE_CANTOPEN, lineno, "cannot open file");
156246}
156247#ifdef SQLITE_DEBUG
156248SQLITE_PRIVATE int sqlite3CorruptPgnoError(int lineno, Pgno pgno){
156249 char zMsg[100];
156250 sqlite3_snprintf(sizeof(zMsg), zMsg, "database corruption page %d", pgno);
156251 testcase( sqlite3GlobalConfig.xLog!=0 );
156252 return sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg);
156253}
156254SQLITE_PRIVATE int sqlite3NomemError(int lineno){
156255 testcase( sqlite3GlobalConfig.xLog!=0 );
156256 return sqlite3ReportError(SQLITE_NOMEM, lineno, "OOM");
156257}
156258SQLITE_PRIVATE int sqlite3IoerrnomemError(int lineno){
156259 testcase( sqlite3GlobalConfig.xLog!=0 );
156260 return sqlite3ReportError(SQLITE_IOERR_NOMEM, lineno, "I/O OOM error");
156261}
156262#endif
156263
156264#ifndef SQLITE_OMIT_DEPRECATED
156265/*
156266** This is a convenience routine that makes sure that all thread-specific
156267** data for this thread has been deallocated.
156268**
156269** SQLite no longer uses thread-specific data so this routine is now a
156270** no-op. It is retained for historical compatibility.
156271*/
156272SQLITE_API void sqlite3_thread_cleanup(void){
156273}
156274#endif
156275
156276/*
156277** Return meta information about a specific column of a database table.
156278** See comment in sqlite3.h (sqlite.h.in) for details.
156279*/
156280SQLITE_API int sqlite3_table_column_metadata(
156281 sqlite3 *db, /* Connection handle */
156282 const char *zDbName, /* Database name or NULL */
156283 const char *zTableName, /* Table name */
156284 const char *zColumnName, /* Column name */
156285 char const **pzDataType, /* OUTPUT: Declared data type */
156286 char const **pzCollSeq, /* OUTPUT: Collation sequence name */
156287 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
156288 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
156289 int *pAutoinc /* OUTPUT: True if column is auto-increment */
156290){
156291 int rc;
156292 char *zErrMsg = 0;
156293 Table *pTab = 0;
156294 Column *pCol = 0;
156295 int iCol = 0;
156296 char const *zDataType = 0;
156297 char const *zCollSeq = 0;
156298 int notnull = 0;
156299 int primarykey = 0;
156300 int autoinc = 0;
156301
156302
156303#ifdef SQLITE_ENABLE_API_ARMOR
156304 if( !sqlite3SafetyCheckOk(db) || zTableName==0 ){
156305 return SQLITE_MISUSE_BKPT;
156306 }
156307#endif
156308
156309 /* Ensure the database schema has been loaded */
156310 sqlite3_mutex_enter(db->mutex);
156311 sqlite3BtreeEnterAll(db);
156312 rc = sqlite3Init(db, &zErrMsg);
156313 if( SQLITE_OK!=rc ){
156314 goto error_out;
156315 }
156316
156317 /* Locate the table in question */
156318 pTab = sqlite3FindTable(db, zTableName, zDbName);
156319 if( !pTab || pTab->pSelect ){
156320 pTab = 0;
156321 goto error_out;
156322 }
156323
156324 /* Find the column for which info is requested */
156325 if( zColumnName==0 ){
156326 /* Query for existance of table only */
156327 }else{
156328 for(iCol=0; iCol<pTab->nCol; iCol++){
156329 pCol = &pTab->aCol[iCol];
156330 if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
156331 break;
156332 }
156333 }
156334 if( iCol==pTab->nCol ){
156335 if( HasRowid(pTab) && sqlite3IsRowid(zColumnName) ){
156336 iCol = pTab->iPKey;
156337 pCol = iCol>=0 ? &pTab->aCol[iCol] : 0;
156338 }else{
156339 pTab = 0;
156340 goto error_out;
156341 }
156342 }
156343 }
156344
156345 /* The following block stores the meta information that will be returned
156346 ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
156347 ** and autoinc. At this point there are two possibilities:
156348 **
156349 ** 1. The specified column name was rowid", "oid" or "_rowid_"
156350 ** and there is no explicitly declared IPK column.
156351 **
156352 ** 2. The table is not a view and the column name identified an
156353 ** explicitly declared column. Copy meta information from *pCol.
156354 */
156355 if( pCol ){
156356 zDataType = sqlite3ColumnType(pCol,0);
156357 zCollSeq = pCol->zColl;
156358 notnull = pCol->notNull!=0;
156359 primarykey = (pCol->colFlags & COLFLAG_PRIMKEY)!=0;
156360 autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
156361 }else{
156362 zDataType = "INTEGER";
156363 primarykey = 1;
156364 }
156365 if( !zCollSeq ){
156366 zCollSeq = sqlite3StrBINARY;
156367 }
156368
156369error_out:
156370 sqlite3BtreeLeaveAll(db);
156371
156372 /* Whether the function call succeeded or failed, set the output parameters
156373 ** to whatever their local counterparts contain. If an error did occur,
156374 ** this has the effect of zeroing all output parameters.
156375 */
156376 if( pzDataType ) *pzDataType = zDataType;
156377 if( pzCollSeq ) *pzCollSeq = zCollSeq;
156378 if( pNotNull ) *pNotNull = notnull;
156379 if( pPrimaryKey ) *pPrimaryKey = primarykey;
156380 if( pAutoinc ) *pAutoinc = autoinc;
156381
156382 if( SQLITE_OK==rc && !pTab ){
156383 sqlite3DbFree(db, zErrMsg);
156384 zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
156385 zColumnName);
156386 rc = SQLITE_ERROR;
156387 }
156388 sqlite3ErrorWithMsg(db, rc, (zErrMsg?"%s":0), zErrMsg);
156389 sqlite3DbFree(db, zErrMsg);
156390 rc = sqlite3ApiExit(db, rc);
156391 sqlite3_mutex_leave(db->mutex);
156392 return rc;
156393}
156394
156395/*
156396** Sleep for a little while. Return the amount of time slept.
156397*/
156398SQLITE_API int sqlite3_sleep(int ms){
156399 sqlite3_vfs *pVfs;
156400 int rc;
156401 pVfs = sqlite3_vfs_find(0);
156402 if( pVfs==0 ) return 0;
156403
156404 /* This function works in milliseconds, but the underlying OsSleep()
156405 ** API uses microseconds. Hence the 1000's.
156406 */
156407 rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
156408 return rc;
156409}
156410
156411/*
156412** Enable or disable the extended result codes.
156413*/
156414SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
156415#ifdef SQLITE_ENABLE_API_ARMOR
156416 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
156417#endif
156418 sqlite3_mutex_enter(db->mutex);
156419 db->errMask = onoff ? 0xffffffff : 0xff;
156420 sqlite3_mutex_leave(db->mutex);
156421 return SQLITE_OK;
156422}
156423
156424/*
156425** Invoke the xFileControl method on a particular database.
156426*/
156427SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
156428 int rc = SQLITE_ERROR;
156429 Btree *pBtree;
156430
156431#ifdef SQLITE_ENABLE_API_ARMOR
156432 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
156433#endif
156434 sqlite3_mutex_enter(db->mutex);
156435 pBtree = sqlite3DbNameToBtree(db, zDbName);
156436 if( pBtree ){
156437 Pager *pPager;
156438 sqlite3_file *fd;
156439 sqlite3BtreeEnter(pBtree);
156440 pPager = sqlite3BtreePager(pBtree);
156441 assert( pPager!=0 );
156442 fd = sqlite3PagerFile(pPager);
156443 assert( fd!=0 );
156444 if( op==SQLITE_FCNTL_FILE_POINTER ){
156445 *(sqlite3_file**)pArg = fd;
156446 rc = SQLITE_OK;
156447 }else if( op==SQLITE_FCNTL_VFS_POINTER ){
156448 *(sqlite3_vfs**)pArg = sqlite3PagerVfs(pPager);
156449 rc = SQLITE_OK;
156450 }else if( op==SQLITE_FCNTL_JOURNAL_POINTER ){
156451 *(sqlite3_file**)pArg = sqlite3PagerJrnlFile(pPager);
156452 rc = SQLITE_OK;
156453 }else if( op==SQLITE_FCNTL_DATA_VERSION ){
156454 *(unsigned int*)pArg = sqlite3PagerDataVersion(pPager);
156455 rc = SQLITE_OK;
156456 }else{
156457 rc = sqlite3OsFileControl(fd, op, pArg);
156458 }
156459 sqlite3BtreeLeave(pBtree);
156460 }
156461 sqlite3_mutex_leave(db->mutex);
156462 return rc;
156463}
156464
156465/*
156466** Interface to the testing logic.
156467*/
156468SQLITE_API int sqlite3_test_control(int op, ...){
156469 int rc = 0;
156470#ifdef SQLITE_UNTESTABLE
156471 UNUSED_PARAMETER(op);
156472#else
156473 va_list ap;
156474 va_start(ap, op);
156475 switch( op ){
156476
156477 /*
156478 ** Save the current state of the PRNG.
156479 */
156480 case SQLITE_TESTCTRL_PRNG_SAVE: {
156481 sqlite3PrngSaveState();
156482 break;
156483 }
156484
156485 /*
156486 ** Restore the state of the PRNG to the last state saved using
156487 ** PRNG_SAVE. If PRNG_SAVE has never before been called, then
156488 ** this verb acts like PRNG_RESET.
156489 */
156490 case SQLITE_TESTCTRL_PRNG_RESTORE: {
156491 sqlite3PrngRestoreState();
156492 break;
156493 }
156494
156495 /*
156496 ** Reset the PRNG back to its uninitialized state. The next call
156497 ** to sqlite3_randomness() will reseed the PRNG using a single call
156498 ** to the xRandomness method of the default VFS.
156499 */
156500 case SQLITE_TESTCTRL_PRNG_RESET: {
156501 sqlite3_randomness(0,0);
156502 break;
156503 }
156504
156505 /*
156506 ** sqlite3_test_control(BITVEC_TEST, size, program)
156507 **
156508 ** Run a test against a Bitvec object of size. The program argument
156509 ** is an array of integers that defines the test. Return -1 on a
156510 ** memory allocation error, 0 on success, or non-zero for an error.
156511 ** See the sqlite3BitvecBuiltinTest() for additional information.
156512 */
156513 case SQLITE_TESTCTRL_BITVEC_TEST: {
156514 int sz = va_arg(ap, int);
156515 int *aProg = va_arg(ap, int*);
156516 rc = sqlite3BitvecBuiltinTest(sz, aProg);
156517 break;
156518 }
156519
156520 /*
156521 ** sqlite3_test_control(FAULT_INSTALL, xCallback)
156522 **
156523 ** Arrange to invoke xCallback() whenever sqlite3FaultSim() is called,
156524 ** if xCallback is not NULL.
156525 **
156526 ** As a test of the fault simulator mechanism itself, sqlite3FaultSim(0)
156527 ** is called immediately after installing the new callback and the return
156528 ** value from sqlite3FaultSim(0) becomes the return from
156529 ** sqlite3_test_control().
156530 */
156531 case SQLITE_TESTCTRL_FAULT_INSTALL: {
156532 /* MSVC is picky about pulling func ptrs from va lists.
156533 ** http://support.microsoft.com/kb/47961
156534 ** sqlite3GlobalConfig.xTestCallback = va_arg(ap, int(*)(int));
156535 */
156536 typedef int(*TESTCALLBACKFUNC_t)(int);
156537 sqlite3GlobalConfig.xTestCallback = va_arg(ap, TESTCALLBACKFUNC_t);
156538 rc = sqlite3FaultSim(0);
156539 break;
156540 }
156541
156542 /*
156543 ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
156544 **
156545 ** Register hooks to call to indicate which malloc() failures
156546 ** are benign.
156547 */
156548 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: {
156549 typedef void (*void_function)(void);
156550 void_function xBenignBegin;
156551 void_function xBenignEnd;
156552 xBenignBegin = va_arg(ap, void_function);
156553 xBenignEnd = va_arg(ap, void_function);
156554 sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
156555 break;
156556 }
156557
156558 /*
156559 ** sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
156560 **
156561 ** Set the PENDING byte to the value in the argument, if X>0.
156562 ** Make no changes if X==0. Return the value of the pending byte
156563 ** as it existing before this routine was called.
156564 **
156565 ** IMPORTANT: Changing the PENDING byte from 0x40000000 results in
156566 ** an incompatible database file format. Changing the PENDING byte
156567 ** while any database connection is open results in undefined and
156568 ** deleterious behavior.
156569 */
156570 case SQLITE_TESTCTRL_PENDING_BYTE: {
156571 rc = PENDING_BYTE;
156572#ifndef SQLITE_OMIT_WSD
156573 {
156574 unsigned int newVal = va_arg(ap, unsigned int);
156575 if( newVal ) sqlite3PendingByte = newVal;
156576 }
156577#endif
156578 break;
156579 }
156580
156581 /*
156582 ** sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
156583 **
156584 ** This action provides a run-time test to see whether or not
156585 ** assert() was enabled at compile-time. If X is true and assert()
156586 ** is enabled, then the return value is true. If X is true and
156587 ** assert() is disabled, then the return value is zero. If X is
156588 ** false and assert() is enabled, then the assertion fires and the
156589 ** process aborts. If X is false and assert() is disabled, then the
156590 ** return value is zero.
156591 */
156592 case SQLITE_TESTCTRL_ASSERT: {
156593 volatile int x = 0;
156594 assert( /*side-effects-ok*/ (x = va_arg(ap,int))!=0 );
156595 rc = x;
156596 break;
156597 }
156598
156599
156600 /*
156601 ** sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
156602 **
156603 ** This action provides a run-time test to see how the ALWAYS and
156604 ** NEVER macros were defined at compile-time.
156605 **
156606 ** The return value is ALWAYS(X) if X is true, or 0 if X is false.
156607 **
156608 ** The recommended test is X==2. If the return value is 2, that means
156609 ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
156610 ** default setting. If the return value is 1, then ALWAYS() is either
156611 ** hard-coded to true or else it asserts if its argument is false.
156612 ** The first behavior (hard-coded to true) is the case if
156613 ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
156614 ** behavior (assert if the argument to ALWAYS() is false) is the case if
156615 ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
156616 **
156617 ** The run-time test procedure might look something like this:
156618 **
156619 ** if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
156620 ** // ALWAYS() and NEVER() are no-op pass-through macros
156621 ** }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
156622 ** // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
156623 ** }else{
156624 ** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0.
156625 ** }
156626 */
156627 case SQLITE_TESTCTRL_ALWAYS: {
156628 int x = va_arg(ap,int);
156629 rc = x ? ALWAYS(x) : 0;
156630 break;
156631 }
156632
156633 /*
156634 ** sqlite3_test_control(SQLITE_TESTCTRL_BYTEORDER);
156635 **
156636 ** The integer returned reveals the byte-order of the computer on which
156637 ** SQLite is running:
156638 **
156639 ** 1 big-endian, determined at run-time
156640 ** 10 little-endian, determined at run-time
156641 ** 432101 big-endian, determined at compile-time
156642 ** 123410 little-endian, determined at compile-time
156643 */
156644 case SQLITE_TESTCTRL_BYTEORDER: {
156645 rc = SQLITE_BYTEORDER*100 + SQLITE_LITTLEENDIAN*10 + SQLITE_BIGENDIAN;
156646 break;
156647 }
156648
156649 /* sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N)
156650 **
156651 ** Set the nReserve size to N for the main database on the database
156652 ** connection db.
156653 */
156654 case SQLITE_TESTCTRL_RESERVE: {
156655 sqlite3 *db = va_arg(ap, sqlite3*);
156656 int x = va_arg(ap,int);
156657 sqlite3_mutex_enter(db->mutex);
156658 sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0);
156659 sqlite3_mutex_leave(db->mutex);
156660 break;
156661 }
156662
156663 /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
156664 **
156665 ** Enable or disable various optimizations for testing purposes. The
156666 ** argument N is a bitmask of optimizations to be disabled. For normal
156667 ** operation N should be 0. The idea is that a test program (like the
156668 ** SQL Logic Test or SLT test module) can run the same SQL multiple times
156669 ** with various optimizations disabled to verify that the same answer
156670 ** is obtained in every case.
156671 */
156672 case SQLITE_TESTCTRL_OPTIMIZATIONS: {
156673 sqlite3 *db = va_arg(ap, sqlite3*);
156674 db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
156675 break;
156676 }
156677
156678 /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff);
156679 **
156680 ** If parameter onoff is non-zero, subsequent calls to localtime()
156681 ** and its variants fail. If onoff is zero, undo this setting.
156682 */
156683 case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
156684 sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
156685 break;
156686 }
156687
156688 /* sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCS, int onoff);
156689 **
156690 ** If parameter onoff is non-zero, internal-use-only SQL functions
156691 ** are visible to ordinary SQL. This is useful for testing but is
156692 ** unsafe because invalid parameters to those internal-use-only functions
156693 ** can result in crashes or segfaults.
156694 */
156695 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: {
156696 sqlite3GlobalConfig.bInternalFunctions = va_arg(ap, int);
156697 break;
156698 }
156699
156700 /* sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int);
156701 **
156702 ** Set or clear a flag that indicates that the database file is always well-
156703 ** formed and never corrupt. This flag is clear by default, indicating that
156704 ** database files might have arbitrary corruption. Setting the flag during
156705 ** testing causes certain assert() statements in the code to be activated
156706 ** that demonstrat invariants on well-formed database files.
156707 */
156708 case SQLITE_TESTCTRL_NEVER_CORRUPT: {
156709 sqlite3GlobalConfig.neverCorrupt = va_arg(ap, int);
156710 break;
156711 }
156712
156713 /* Set the threshold at which OP_Once counters reset back to zero.
156714 ** By default this is 0x7ffffffe (over 2 billion), but that value is
156715 ** too big to test in a reasonable amount of time, so this control is
156716 ** provided to set a small and easily reachable reset value.
156717 */
156718 case SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD: {
156719 sqlite3GlobalConfig.iOnceResetThreshold = va_arg(ap, int);
156720 break;
156721 }
156722
156723 /* sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, xCallback, ptr);
156724 **
156725 ** Set the VDBE coverage callback function to xCallback with context
156726 ** pointer ptr.
156727 */
156728 case SQLITE_TESTCTRL_VDBE_COVERAGE: {
156729#ifdef SQLITE_VDBE_COVERAGE
156730 typedef void (*branch_callback)(void*,unsigned int,
156731 unsigned char,unsigned char);
156732 sqlite3GlobalConfig.xVdbeBranch = va_arg(ap,branch_callback);
156733 sqlite3GlobalConfig.pVdbeBranchArg = va_arg(ap,void*);
156734#endif
156735 break;
156736 }
156737
156738 /* sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, nMax); */
156739 case SQLITE_TESTCTRL_SORTER_MMAP: {
156740 sqlite3 *db = va_arg(ap, sqlite3*);
156741 db->nMaxSorterMmap = va_arg(ap, int);
156742 break;
156743 }
156744
156745 /* sqlite3_test_control(SQLITE_TESTCTRL_ISINIT);
156746 **
156747 ** Return SQLITE_OK if SQLite has been initialized and SQLITE_ERROR if
156748 ** not.
156749 */
156750 case SQLITE_TESTCTRL_ISINIT: {
156751 if( sqlite3GlobalConfig.isInit==0 ) rc = SQLITE_ERROR;
156752 break;
156753 }
156754
156755 /* sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, dbName, onOff, tnum);
156756 **
156757 ** This test control is used to create imposter tables. "db" is a pointer
156758 ** to the database connection. dbName is the database name (ex: "main" or
156759 ** "temp") which will receive the imposter. "onOff" turns imposter mode on
156760 ** or off. "tnum" is the root page of the b-tree to which the imposter
156761 ** table should connect.
156762 **
156763 ** Enable imposter mode only when the schema has already been parsed. Then
156764 ** run a single CREATE TABLE statement to construct the imposter table in
156765 ** the parsed schema. Then turn imposter mode back off again.
156766 **
156767 ** If onOff==0 and tnum>0 then reset the schema for all databases, causing
156768 ** the schema to be reparsed the next time it is needed. This has the
156769 ** effect of erasing all imposter tables.
156770 */
156771 case SQLITE_TESTCTRL_IMPOSTER: {
156772 sqlite3 *db = va_arg(ap, sqlite3*);
156773 sqlite3_mutex_enter(db->mutex);
156774 db->init.iDb = sqlite3FindDbName(db, va_arg(ap,const char*));
156775 db->init.busy = db->init.imposterTable = va_arg(ap,int);
156776 db->init.newTnum = va_arg(ap,int);
156777 if( db->init.busy==0 && db->init.newTnum>0 ){
156778 sqlite3ResetAllSchemasOfConnection(db);
156779 }
156780 sqlite3_mutex_leave(db->mutex);
156781 break;
156782 }
156783
156784#if defined(YYCOVERAGE)
156785 /* sqlite3_test_control(SQLITE_TESTCTRL_PARSER_COVERAGE, FILE *out)
156786 **
156787 ** This test control (only available when SQLite is compiled with
156788 ** -DYYCOVERAGE) writes a report onto "out" that shows all
156789 ** state/lookahead combinations in the parser state machine
156790 ** which are never exercised. If any state is missed, make the
156791 ** return code SQLITE_ERROR.
156792 */
156793 case SQLITE_TESTCTRL_PARSER_COVERAGE: {
156794 FILE *out = va_arg(ap, FILE*);
156795 if( sqlite3ParserCoverage(out) ) rc = SQLITE_ERROR;
156796 break;
156797 }
156798#endif /* defined(YYCOVERAGE) */
156799 }
156800 va_end(ap);
156801#endif /* SQLITE_UNTESTABLE */
156802 return rc;
156803}
156804
156805/*
156806** This is a utility routine, useful to VFS implementations, that checks
156807** to see if a database file was a URI that contained a specific query
156808** parameter, and if so obtains the value of the query parameter.
156809**
156810** The zFilename argument is the filename pointer passed into the xOpen()
156811** method of a VFS implementation. The zParam argument is the name of the
156812** query parameter we seek. This routine returns the value of the zParam
156813** parameter if it exists. If the parameter does not exist, this routine
156814** returns a NULL pointer.
156815*/
156816SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
156817 if( zFilename==0 || zParam==0 ) return 0;
156818 zFilename += sqlite3Strlen30(zFilename) + 1;
156819 while( zFilename[0] ){
156820 int x = strcmp(zFilename, zParam);
156821 zFilename += sqlite3Strlen30(zFilename) + 1;
156822 if( x==0 ) return zFilename;
156823 zFilename += sqlite3Strlen30(zFilename) + 1;
156824 }
156825 return 0;
156826}
156827
156828/*
156829** Return a boolean value for a query parameter.
156830*/
156831SQLITE_API int sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){
156832 const char *z = sqlite3_uri_parameter(zFilename, zParam);
156833 bDflt = bDflt!=0;
156834 return z ? sqlite3GetBoolean(z, bDflt) : bDflt;
156835}
156836
156837/*
156838** Return a 64-bit integer value for a query parameter.
156839*/
156840SQLITE_API sqlite3_int64 sqlite3_uri_int64(
156841 const char *zFilename, /* Filename as passed to xOpen */
156842 const char *zParam, /* URI parameter sought */
156843 sqlite3_int64 bDflt /* return if parameter is missing */
156844){
156845 const char *z = sqlite3_uri_parameter(zFilename, zParam);
156846 sqlite3_int64 v;
156847 if( z && sqlite3DecOrHexToI64(z, &v)==0 ){
156848 bDflt = v;
156849 }
156850 return bDflt;
156851}
156852
156853/*
156854** Return the Btree pointer identified by zDbName. Return NULL if not found.
156855*/
156856SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){
156857 int iDb = zDbName ? sqlite3FindDbName(db, zDbName) : 0;
156858 return iDb<0 ? 0 : db->aDb[iDb].pBt;
156859}
156860
156861/*
156862** Return the filename of the database associated with a database
156863** connection.
156864*/
156865SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
156866 Btree *pBt;
156867#ifdef SQLITE_ENABLE_API_ARMOR
156868 if( !sqlite3SafetyCheckOk(db) ){
156869 (void)SQLITE_MISUSE_BKPT;
156870 return 0;
156871 }
156872#endif
156873 pBt = sqlite3DbNameToBtree(db, zDbName);
156874 return pBt ? sqlite3BtreeGetFilename(pBt) : 0;
156875}
156876
156877/*
156878** Return 1 if database is read-only or 0 if read/write. Return -1 if
156879** no such database exists.
156880*/
156881SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
156882 Btree *pBt;
156883#ifdef SQLITE_ENABLE_API_ARMOR
156884 if( !sqlite3SafetyCheckOk(db) ){
156885 (void)SQLITE_MISUSE_BKPT;
156886 return -1;
156887 }
156888#endif
156889 pBt = sqlite3DbNameToBtree(db, zDbName);
156890 return pBt ? sqlite3BtreeIsReadonly(pBt) : -1;
156891}
156892
156893#ifdef SQLITE_ENABLE_SNAPSHOT
156894/*
156895** Obtain a snapshot handle for the snapshot of database zDb currently
156896** being read by handle db.
156897*/
156898SQLITE_API int sqlite3_snapshot_get(
156899 sqlite3 *db,
156900 const char *zDb,
156901 sqlite3_snapshot **ppSnapshot
156902){
156903 int rc = SQLITE_ERROR;
156904#ifndef SQLITE_OMIT_WAL
156905
156906#ifdef SQLITE_ENABLE_API_ARMOR
156907 if( !sqlite3SafetyCheckOk(db) ){
156908 return SQLITE_MISUSE_BKPT;
156909 }
156910#endif
156911 sqlite3_mutex_enter(db->mutex);
156912
156913 if( db->autoCommit==0 ){
156914 int iDb = sqlite3FindDbName(db, zDb);
156915 if( iDb==0 || iDb>1 ){
156916 Btree *pBt = db->aDb[iDb].pBt;
156917 if( 0==sqlite3BtreeIsInTrans(pBt) ){
156918 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
156919 if( rc==SQLITE_OK ){
156920 rc = sqlite3PagerSnapshotGet(sqlite3BtreePager(pBt), ppSnapshot);
156921 }
156922 }
156923 }
156924 }
156925
156926 sqlite3_mutex_leave(db->mutex);
156927#endif /* SQLITE_OMIT_WAL */
156928 return rc;
156929}
156930
156931/*
156932** Open a read-transaction on the snapshot idendified by pSnapshot.
156933*/
156934SQLITE_API int sqlite3_snapshot_open(
156935 sqlite3 *db,
156936 const char *zDb,
156937 sqlite3_snapshot *pSnapshot
156938){
156939 int rc = SQLITE_ERROR;
156940#ifndef SQLITE_OMIT_WAL
156941
156942#ifdef SQLITE_ENABLE_API_ARMOR
156943 if( !sqlite3SafetyCheckOk(db) ){
156944 return SQLITE_MISUSE_BKPT;
156945 }
156946#endif
156947 sqlite3_mutex_enter(db->mutex);
156948 if( db->autoCommit==0 ){
156949 int iDb;
156950 iDb = sqlite3FindDbName(db, zDb);
156951 if( iDb==0 || iDb>1 ){
156952 Btree *pBt = db->aDb[iDb].pBt;
156953 if( sqlite3BtreeIsInTrans(pBt)==0 ){
156954 Pager *pPager = sqlite3BtreePager(pBt);
156955 int bUnlock = 0;
156956 if( sqlite3BtreeIsInReadTrans(pBt) ){
156957 if( db->nVdbeActive==0 ){
156958 rc = sqlite3PagerSnapshotCheck(pPager, pSnapshot);
156959 if( rc==SQLITE_OK ){
156960 bUnlock = 1;
156961 rc = sqlite3BtreeCommit(pBt);
156962 }
156963 }
156964 }else{
156965 rc = SQLITE_OK;
156966 }
156967 if( rc==SQLITE_OK ){
156968 rc = sqlite3PagerSnapshotOpen(pPager, pSnapshot);
156969 }
156970 if( rc==SQLITE_OK ){
156971 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
156972 sqlite3PagerSnapshotOpen(pPager, 0);
156973 }
156974 if( bUnlock ){
156975 sqlite3PagerSnapshotUnlock(pPager);
156976 }
156977 }
156978 }
156979 }
156980
156981 sqlite3_mutex_leave(db->mutex);
156982#endif /* SQLITE_OMIT_WAL */
156983 return rc;
156984}
156985
156986/*
156987** Recover as many snapshots as possible from the wal file associated with
156988** schema zDb of database db.
156989*/
156990SQLITE_API int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb){
156991 int rc = SQLITE_ERROR;
156992 int iDb;
156993#ifndef SQLITE_OMIT_WAL
156994
156995#ifdef SQLITE_ENABLE_API_ARMOR
156996 if( !sqlite3SafetyCheckOk(db) ){
156997 return SQLITE_MISUSE_BKPT;
156998 }
156999#endif
157000
157001 sqlite3_mutex_enter(db->mutex);
157002 iDb = sqlite3FindDbName(db, zDb);
157003 if( iDb==0 || iDb>1 ){
157004 Btree *pBt = db->aDb[iDb].pBt;
157005 if( 0==sqlite3BtreeIsInReadTrans(pBt) ){
157006 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
157007 if( rc==SQLITE_OK ){
157008 rc = sqlite3PagerSnapshotRecover(sqlite3BtreePager(pBt));
157009 sqlite3BtreeCommit(pBt);
157010 }
157011 }
157012 }
157013 sqlite3_mutex_leave(db->mutex);
157014#endif /* SQLITE_OMIT_WAL */
157015 return rc;
157016}
157017
157018/*
157019** Free a snapshot handle obtained from sqlite3_snapshot_get().
157020*/
157021SQLITE_API void sqlite3_snapshot_free(sqlite3_snapshot *pSnapshot){
157022 sqlite3_free(pSnapshot);
157023}
157024#endif /* SQLITE_ENABLE_SNAPSHOT */
157025
157026#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
157027/*
157028** Given the name of a compile-time option, return true if that option
157029** was used and false if not.
157030**
157031** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
157032** is not required for a match.
157033*/
157034SQLITE_API int sqlite3_compileoption_used(const char *zOptName){
157035 int i, n;
157036 int nOpt;
157037 const char **azCompileOpt;
157038
157039#if SQLITE_ENABLE_API_ARMOR
157040 if( zOptName==0 ){
157041 (void)SQLITE_MISUSE_BKPT;
157042 return 0;
157043 }
157044#endif
157045
157046 azCompileOpt = sqlite3CompileOptions(&nOpt);
157047
157048 if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
157049 n = sqlite3Strlen30(zOptName);
157050
157051 /* Since nOpt is normally in single digits, a linear search is
157052 ** adequate. No need for a binary search. */
157053 for(i=0; i<nOpt; i++){
157054 if( sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0
157055 && sqlite3IsIdChar((unsigned char)azCompileOpt[i][n])==0
157056 ){
157057 return 1;
157058 }
157059 }
157060 return 0;
157061}
157062
157063/*
157064** Return the N-th compile-time option string. If N is out of range,
157065** return a NULL pointer.
157066*/
157067SQLITE_API const char *sqlite3_compileoption_get(int N){
157068 int nOpt;
157069 const char **azCompileOpt;
157070 azCompileOpt = sqlite3CompileOptions(&nOpt);
157071 if( N>=0 && N<nOpt ){
157072 return azCompileOpt[N];
157073 }
157074 return 0;
157075}
157076#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
157077
157078/************** End of main.c ************************************************/
157079/************** Begin file notify.c ******************************************/
157080/*
157081** 2009 March 3
157082**
157083** The author disclaims copyright to this source code. In place of
157084** a legal notice, here is a blessing:
157085**
157086** May you do good and not evil.
157087** May you find forgiveness for yourself and forgive others.
157088** May you share freely, never taking more than you give.
157089**
157090*************************************************************************
157091**
157092** This file contains the implementation of the sqlite3_unlock_notify()
157093** API method and its associated functionality.
157094*/
157095/* #include "sqliteInt.h" */
157096/* #include "btreeInt.h" */
157097
157098/* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
157099#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
157100
157101/*
157102** Public interfaces:
157103**
157104** sqlite3ConnectionBlocked()
157105** sqlite3ConnectionUnlocked()
157106** sqlite3ConnectionClosed()
157107** sqlite3_unlock_notify()
157108*/
157109
157110#define assertMutexHeld() \
157111 assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) )
157112
157113/*
157114** Head of a linked list of all sqlite3 objects created by this process
157115** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection
157116** is not NULL. This variable may only accessed while the STATIC_MASTER
157117** mutex is held.
157118*/
157119static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0;
157120
157121#ifndef NDEBUG
157122/*
157123** This function is a complex assert() that verifies the following
157124** properties of the blocked connections list:
157125**
157126** 1) Each entry in the list has a non-NULL value for either
157127** pUnlockConnection or pBlockingConnection, or both.
157128**
157129** 2) All entries in the list that share a common value for
157130** xUnlockNotify are grouped together.
157131**
157132** 3) If the argument db is not NULL, then none of the entries in the
157133** blocked connections list have pUnlockConnection or pBlockingConnection
157134** set to db. This is used when closing connection db.
157135*/
157136static void checkListProperties(sqlite3 *db){
157137 sqlite3 *p;
157138 for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
157139 int seen = 0;
157140 sqlite3 *p2;
157141
157142 /* Verify property (1) */
157143 assert( p->pUnlockConnection || p->pBlockingConnection );
157144
157145 /* Verify property (2) */
157146 for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
157147 if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
157148 assert( p2->xUnlockNotify==p->xUnlockNotify || !seen );
157149 assert( db==0 || p->pUnlockConnection!=db );
157150 assert( db==0 || p->pBlockingConnection!=db );
157151 }
157152 }
157153}
157154#else
157155# define checkListProperties(x)
157156#endif
157157
157158/*
157159** Remove connection db from the blocked connections list. If connection
157160** db is not currently a part of the list, this function is a no-op.
157161*/
157162static void removeFromBlockedList(sqlite3 *db){
157163 sqlite3 **pp;
157164 assertMutexHeld();
157165 for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
157166 if( *pp==db ){
157167 *pp = (*pp)->pNextBlocked;
157168 break;
157169 }
157170 }
157171}
157172
157173/*
157174** Add connection db to the blocked connections list. It is assumed
157175** that it is not already a part of the list.
157176*/
157177static void addToBlockedList(sqlite3 *db){
157178 sqlite3 **pp;
157179 assertMutexHeld();
157180 for(
157181 pp=&sqlite3BlockedList;
157182 *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify;
157183 pp=&(*pp)->pNextBlocked
157184 );
157185 db->pNextBlocked = *pp;
157186 *pp = db;
157187}
157188
157189/*
157190** Obtain the STATIC_MASTER mutex.
157191*/
157192static void enterMutex(void){
157193 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
157194 checkListProperties(0);
157195}
157196
157197/*
157198** Release the STATIC_MASTER mutex.
157199*/
157200static void leaveMutex(void){
157201 assertMutexHeld();
157202 checkListProperties(0);
157203 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
157204}
157205
157206/*
157207** Register an unlock-notify callback.
157208**
157209** This is called after connection "db" has attempted some operation
157210** but has received an SQLITE_LOCKED error because another connection
157211** (call it pOther) in the same process was busy using the same shared
157212** cache. pOther is found by looking at db->pBlockingConnection.
157213**
157214** If there is no blocking connection, the callback is invoked immediately,
157215** before this routine returns.
157216**
157217** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate
157218** a deadlock.
157219**
157220** Otherwise, make arrangements to invoke xNotify when pOther drops
157221** its locks.
157222**
157223** Each call to this routine overrides any prior callbacks registered
157224** on the same "db". If xNotify==0 then any prior callbacks are immediately
157225** cancelled.
157226*/
157227SQLITE_API int sqlite3_unlock_notify(
157228 sqlite3 *db,
157229 void (*xNotify)(void **, int),
157230 void *pArg
157231){
157232 int rc = SQLITE_OK;
157233
157234 sqlite3_mutex_enter(db->mutex);
157235 enterMutex();
157236
157237 if( xNotify==0 ){
157238 removeFromBlockedList(db);
157239 db->pBlockingConnection = 0;
157240 db->pUnlockConnection = 0;
157241 db->xUnlockNotify = 0;
157242 db->pUnlockArg = 0;
157243 }else if( 0==db->pBlockingConnection ){
157244 /* The blocking transaction has been concluded. Or there never was a
157245 ** blocking transaction. In either case, invoke the notify callback
157246 ** immediately.
157247 */
157248 xNotify(&pArg, 1);
157249 }else{
157250 sqlite3 *p;
157251
157252 for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
157253 if( p ){
157254 rc = SQLITE_LOCKED; /* Deadlock detected. */
157255 }else{
157256 db->pUnlockConnection = db->pBlockingConnection;
157257 db->xUnlockNotify = xNotify;
157258 db->pUnlockArg = pArg;
157259 removeFromBlockedList(db);
157260 addToBlockedList(db);
157261 }
157262 }
157263
157264 leaveMutex();
157265 assert( !db->mallocFailed );
157266 sqlite3ErrorWithMsg(db, rc, (rc?"database is deadlocked":0));
157267 sqlite3_mutex_leave(db->mutex);
157268 return rc;
157269}
157270
157271/*
157272** This function is called while stepping or preparing a statement
157273** associated with connection db. The operation will return SQLITE_LOCKED
157274** to the user because it requires a lock that will not be available
157275** until connection pBlocker concludes its current transaction.
157276*/
157277SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *db, sqlite3 *pBlocker){
157278 enterMutex();
157279 if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
157280 addToBlockedList(db);
157281 }
157282 db->pBlockingConnection = pBlocker;
157283 leaveMutex();
157284}
157285
157286/*
157287** This function is called when
157288** the transaction opened by database db has just finished. Locks held
157289** by database connection db have been released.
157290**
157291** This function loops through each entry in the blocked connections
157292** list and does the following:
157293**
157294** 1) If the sqlite3.pBlockingConnection member of a list entry is
157295** set to db, then set pBlockingConnection=0.
157296**
157297** 2) If the sqlite3.pUnlockConnection member of a list entry is
157298** set to db, then invoke the configured unlock-notify callback and
157299** set pUnlockConnection=0.
157300**
157301** 3) If the two steps above mean that pBlockingConnection==0 and
157302** pUnlockConnection==0, remove the entry from the blocked connections
157303** list.
157304*/
157305SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db){
157306 void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
157307 int nArg = 0; /* Number of entries in aArg[] */
157308 sqlite3 **pp; /* Iterator variable */
157309 void **aArg; /* Arguments to the unlock callback */
157310 void **aDyn = 0; /* Dynamically allocated space for aArg[] */
157311 void *aStatic[16]; /* Starter space for aArg[]. No malloc required */
157312
157313 aArg = aStatic;
157314 enterMutex(); /* Enter STATIC_MASTER mutex */
157315
157316 /* This loop runs once for each entry in the blocked-connections list. */
157317 for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
157318 sqlite3 *p = *pp;
157319
157320 /* Step 1. */
157321 if( p->pBlockingConnection==db ){
157322 p->pBlockingConnection = 0;
157323 }
157324
157325 /* Step 2. */
157326 if( p->pUnlockConnection==db ){
157327 assert( p->xUnlockNotify );
157328 if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
157329 xUnlockNotify(aArg, nArg);
157330 nArg = 0;
157331 }
157332
157333 sqlite3BeginBenignMalloc();
157334 assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) );
157335 assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn );
157336 if( (!aDyn && nArg==(int)ArraySize(aStatic))
157337 || (aDyn && nArg==(int)(sqlite3MallocSize(aDyn)/sizeof(void*)))
157338 ){
157339 /* The aArg[] array needs to grow. */
157340 void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2);
157341 if( pNew ){
157342 memcpy(pNew, aArg, nArg*sizeof(void *));
157343 sqlite3_free(aDyn);
157344 aDyn = aArg = pNew;
157345 }else{
157346 /* This occurs when the array of context pointers that need to
157347 ** be passed to the unlock-notify callback is larger than the
157348 ** aStatic[] array allocated on the stack and the attempt to
157349 ** allocate a larger array from the heap has failed.
157350 **
157351 ** This is a difficult situation to handle. Returning an error
157352 ** code to the caller is insufficient, as even if an error code
157353 ** is returned the transaction on connection db will still be
157354 ** closed and the unlock-notify callbacks on blocked connections
157355 ** will go unissued. This might cause the application to wait
157356 ** indefinitely for an unlock-notify callback that will never
157357 ** arrive.
157358 **
157359 ** Instead, invoke the unlock-notify callback with the context
157360 ** array already accumulated. We can then clear the array and
157361 ** begin accumulating any further context pointers without
157362 ** requiring any dynamic allocation. This is sub-optimal because
157363 ** it means that instead of one callback with a large array of
157364 ** context pointers the application will receive two or more
157365 ** callbacks with smaller arrays of context pointers, which will
157366 ** reduce the applications ability to prioritize multiple
157367 ** connections. But it is the best that can be done under the
157368 ** circumstances.
157369 */
157370 xUnlockNotify(aArg, nArg);
157371 nArg = 0;
157372 }
157373 }
157374 sqlite3EndBenignMalloc();
157375
157376 aArg[nArg++] = p->pUnlockArg;
157377 xUnlockNotify = p->xUnlockNotify;
157378 p->pUnlockConnection = 0;
157379 p->xUnlockNotify = 0;
157380 p->pUnlockArg = 0;
157381 }
157382
157383 /* Step 3. */
157384 if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
157385 /* Remove connection p from the blocked connections list. */
157386 *pp = p->pNextBlocked;
157387 p->pNextBlocked = 0;
157388 }else{
157389 pp = &p->pNextBlocked;
157390 }
157391 }
157392
157393 if( nArg!=0 ){
157394 xUnlockNotify(aArg, nArg);
157395 }
157396 sqlite3_free(aDyn);
157397 leaveMutex(); /* Leave STATIC_MASTER mutex */
157398}
157399
157400/*
157401** This is called when the database connection passed as an argument is
157402** being closed. The connection is removed from the blocked list.
157403*/
157404SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db){
157405 sqlite3ConnectionUnlocked(db);
157406 enterMutex();
157407 removeFromBlockedList(db);
157408 checkListProperties(db);
157409 leaveMutex();
157410}
157411#endif
157412
157413/************** End of notify.c **********************************************/
157414/************** Begin file fts3.c ********************************************/
157415/*
157416** 2006 Oct 10
157417**
157418** The author disclaims copyright to this source code. In place of
157419** a legal notice, here is a blessing:
157420**
157421** May you do good and not evil.
157422** May you find forgiveness for yourself and forgive others.
157423** May you share freely, never taking more than you give.
157424**
157425******************************************************************************
157426**
157427** This is an SQLite module implementing full-text search.
157428*/
157429
157430/*
157431** The code in this file is only compiled if:
157432**
157433** * The FTS3 module is being built as an extension
157434** (in which case SQLITE_CORE is not defined), or
157435**
157436** * The FTS3 module is being built into the core of
157437** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
157438*/
157439
157440/* The full-text index is stored in a series of b+tree (-like)
157441** structures called segments which map terms to doclists. The
157442** structures are like b+trees in layout, but are constructed from the
157443** bottom up in optimal fashion and are not updatable. Since trees
157444** are built from the bottom up, things will be described from the
157445** bottom up.
157446**
157447**
157448**** Varints ****
157449** The basic unit of encoding is a variable-length integer called a
157450** varint. We encode variable-length integers in little-endian order
157451** using seven bits * per byte as follows:
157452**
157453** KEY:
157454** A = 0xxxxxxx 7 bits of data and one flag bit
157455** B = 1xxxxxxx 7 bits of data and one flag bit
157456**
157457** 7 bits - A
157458** 14 bits - BA
157459** 21 bits - BBA
157460** and so on.
157461**
157462** This is similar in concept to how sqlite encodes "varints" but
157463** the encoding is not the same. SQLite varints are big-endian
157464** are are limited to 9 bytes in length whereas FTS3 varints are
157465** little-endian and can be up to 10 bytes in length (in theory).
157466**
157467** Example encodings:
157468**
157469** 1: 0x01
157470** 127: 0x7f
157471** 128: 0x81 0x00
157472**
157473**
157474**** Document lists ****
157475** A doclist (document list) holds a docid-sorted list of hits for a
157476** given term. Doclists hold docids and associated token positions.
157477** A docid is the unique integer identifier for a single document.
157478** A position is the index of a word within the document. The first
157479** word of the document has a position of 0.
157480**
157481** FTS3 used to optionally store character offsets using a compile-time
157482** option. But that functionality is no longer supported.
157483**
157484** A doclist is stored like this:
157485**
157486** array {
157487** varint docid; (delta from previous doclist)
157488** array { (position list for column 0)
157489** varint position; (2 more than the delta from previous position)
157490** }
157491** array {
157492** varint POS_COLUMN; (marks start of position list for new column)
157493** varint column; (index of new column)
157494** array {
157495** varint position; (2 more than the delta from previous position)
157496** }
157497** }
157498** varint POS_END; (marks end of positions for this document.
157499** }
157500**
157501** Here, array { X } means zero or more occurrences of X, adjacent in
157502** memory. A "position" is an index of a token in the token stream
157503** generated by the tokenizer. Note that POS_END and POS_COLUMN occur
157504** in the same logical place as the position element, and act as sentinals
157505** ending a position list array. POS_END is 0. POS_COLUMN is 1.
157506** The positions numbers are not stored literally but rather as two more
157507** than the difference from the prior position, or the just the position plus
157508** 2 for the first position. Example:
157509**
157510** label: A B C D E F G H I J K
157511** value: 123 5 9 1 1 14 35 0 234 72 0
157512**
157513** The 123 value is the first docid. For column zero in this document
157514** there are two matches at positions 3 and 10 (5-2 and 9-2+3). The 1
157515** at D signals the start of a new column; the 1 at E indicates that the
157516** new column is column number 1. There are two positions at 12 and 45
157517** (14-2 and 35-2+12). The 0 at H indicate the end-of-document. The
157518** 234 at I is the delta to next docid (357). It has one position 70
157519** (72-2) and then terminates with the 0 at K.
157520**
157521** A "position-list" is the list of positions for multiple columns for
157522** a single docid. A "column-list" is the set of positions for a single
157523** column. Hence, a position-list consists of one or more column-lists,
157524** a document record consists of a docid followed by a position-list and
157525** a doclist consists of one or more document records.
157526**
157527** A bare doclist omits the position information, becoming an
157528** array of varint-encoded docids.
157529**
157530**** Segment leaf nodes ****
157531** Segment leaf nodes store terms and doclists, ordered by term. Leaf
157532** nodes are written using LeafWriter, and read using LeafReader (to
157533** iterate through a single leaf node's data) and LeavesReader (to
157534** iterate through a segment's entire leaf layer). Leaf nodes have
157535** the format:
157536**
157537** varint iHeight; (height from leaf level, always 0)
157538** varint nTerm; (length of first term)
157539** char pTerm[nTerm]; (content of first term)
157540** varint nDoclist; (length of term's associated doclist)
157541** char pDoclist[nDoclist]; (content of doclist)
157542** array {
157543** (further terms are delta-encoded)
157544** varint nPrefix; (length of prefix shared with previous term)
157545** varint nSuffix; (length of unshared suffix)
157546** char pTermSuffix[nSuffix];(unshared suffix of next term)
157547** varint nDoclist; (length of term's associated doclist)
157548** char pDoclist[nDoclist]; (content of doclist)
157549** }
157550**
157551** Here, array { X } means zero or more occurrences of X, adjacent in
157552** memory.
157553**
157554** Leaf nodes are broken into blocks which are stored contiguously in
157555** the %_segments table in sorted order. This means that when the end
157556** of a node is reached, the next term is in the node with the next
157557** greater node id.
157558**
157559** New data is spilled to a new leaf node when the current node
157560** exceeds LEAF_MAX bytes (default 2048). New data which itself is
157561** larger than STANDALONE_MIN (default 1024) is placed in a standalone
157562** node (a leaf node with a single term and doclist). The goal of
157563** these settings is to pack together groups of small doclists while
157564** making it efficient to directly access large doclists. The
157565** assumption is that large doclists represent terms which are more
157566** likely to be query targets.
157567**
157568** TODO(shess) It may be useful for blocking decisions to be more
157569** dynamic. For instance, it may make more sense to have a 2.5k leaf
157570** node rather than splitting into 2k and .5k nodes. My intuition is
157571** that this might extend through 2x or 4x the pagesize.
157572**
157573**
157574**** Segment interior nodes ****
157575** Segment interior nodes store blockids for subtree nodes and terms
157576** to describe what data is stored by the each subtree. Interior
157577** nodes are written using InteriorWriter, and read using
157578** InteriorReader. InteriorWriters are created as needed when
157579** SegmentWriter creates new leaf nodes, or when an interior node
157580** itself grows too big and must be split. The format of interior
157581** nodes:
157582**
157583** varint iHeight; (height from leaf level, always >0)
157584** varint iBlockid; (block id of node's leftmost subtree)
157585** optional {
157586** varint nTerm; (length of first term)
157587** char pTerm[nTerm]; (content of first term)
157588** array {
157589** (further terms are delta-encoded)
157590** varint nPrefix; (length of shared prefix with previous term)
157591** varint nSuffix; (length of unshared suffix)
157592** char pTermSuffix[nSuffix]; (unshared suffix of next term)
157593** }
157594** }
157595**
157596** Here, optional { X } means an optional element, while array { X }
157597** means zero or more occurrences of X, adjacent in memory.
157598**
157599** An interior node encodes n terms separating n+1 subtrees. The
157600** subtree blocks are contiguous, so only the first subtree's blockid
157601** is encoded. The subtree at iBlockid will contain all terms less
157602** than the first term encoded (or all terms if no term is encoded).
157603** Otherwise, for terms greater than or equal to pTerm[i] but less
157604** than pTerm[i+1], the subtree for that term will be rooted at
157605** iBlockid+i. Interior nodes only store enough term data to
157606** distinguish adjacent children (if the rightmost term of the left
157607** child is "something", and the leftmost term of the right child is
157608** "wicked", only "w" is stored).
157609**
157610** New data is spilled to a new interior node at the same height when
157611** the current node exceeds INTERIOR_MAX bytes (default 2048).
157612** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing
157613** interior nodes and making the tree too skinny. The interior nodes
157614** at a given height are naturally tracked by interior nodes at
157615** height+1, and so on.
157616**
157617**
157618**** Segment directory ****
157619** The segment directory in table %_segdir stores meta-information for
157620** merging and deleting segments, and also the root node of the
157621** segment's tree.
157622**
157623** The root node is the top node of the segment's tree after encoding
157624** the entire segment, restricted to ROOT_MAX bytes (default 1024).
157625** This could be either a leaf node or an interior node. If the top
157626** node requires more than ROOT_MAX bytes, it is flushed to %_segments
157627** and a new root interior node is generated (which should always fit
157628** within ROOT_MAX because it only needs space for 2 varints, the
157629** height and the blockid of the previous root).
157630**
157631** The meta-information in the segment directory is:
157632** level - segment level (see below)
157633** idx - index within level
157634** - (level,idx uniquely identify a segment)
157635** start_block - first leaf node
157636** leaves_end_block - last leaf node
157637** end_block - last block (including interior nodes)
157638** root - contents of root node
157639**
157640** If the root node is a leaf node, then start_block,
157641** leaves_end_block, and end_block are all 0.
157642**
157643**
157644**** Segment merging ****
157645** To amortize update costs, segments are grouped into levels and
157646** merged in batches. Each increase in level represents exponentially
157647** more documents.
157648**
157649** New documents (actually, document updates) are tokenized and
157650** written individually (using LeafWriter) to a level 0 segment, with
157651** incrementing idx. When idx reaches MERGE_COUNT (default 16), all
157652** level 0 segments are merged into a single level 1 segment. Level 1
157653** is populated like level 0, and eventually MERGE_COUNT level 1
157654** segments are merged to a single level 2 segment (representing
157655** MERGE_COUNT^2 updates), and so on.
157656**
157657** A segment merge traverses all segments at a given level in
157658** parallel, performing a straightforward sorted merge. Since segment
157659** leaf nodes are written in to the %_segments table in order, this
157660** merge traverses the underlying sqlite disk structures efficiently.
157661** After the merge, all segment blocks from the merged level are
157662** deleted.
157663**
157664** MERGE_COUNT controls how often we merge segments. 16 seems to be
157665** somewhat of a sweet spot for insertion performance. 32 and 64 show
157666** very similar performance numbers to 16 on insertion, though they're
157667** a tiny bit slower (perhaps due to more overhead in merge-time
157668** sorting). 8 is about 20% slower than 16, 4 about 50% slower than
157669** 16, 2 about 66% slower than 16.
157670**
157671** At query time, high MERGE_COUNT increases the number of segments
157672** which need to be scanned and merged. For instance, with 100k docs
157673** inserted:
157674**
157675** MERGE_COUNT segments
157676** 16 25
157677** 8 12
157678** 4 10
157679** 2 6
157680**
157681** This appears to have only a moderate impact on queries for very
157682** frequent terms (which are somewhat dominated by segment merge
157683** costs), and infrequent and non-existent terms still seem to be fast
157684** even with many segments.
157685**
157686** TODO(shess) That said, it would be nice to have a better query-side
157687** argument for MERGE_COUNT of 16. Also, it is possible/likely that
157688** optimizations to things like doclist merging will swing the sweet
157689** spot around.
157690**
157691**
157692**
157693**** Handling of deletions and updates ****
157694** Since we're using a segmented structure, with no docid-oriented
157695** index into the term index, we clearly cannot simply update the term
157696** index when a document is deleted or updated. For deletions, we
157697** write an empty doclist (varint(docid) varint(POS_END)), for updates
157698** we simply write the new doclist. Segment merges overwrite older
157699** data for a particular docid with newer data, so deletes or updates
157700** will eventually overtake the earlier data and knock it out. The
157701** query logic likewise merges doclists so that newer data knocks out
157702** older data.
157703*/
157704
157705/************** Include fts3Int.h in the middle of fts3.c ********************/
157706/************** Begin file fts3Int.h *****************************************/
157707/*
157708** 2009 Nov 12
157709**
157710** The author disclaims copyright to this source code. In place of
157711** a legal notice, here is a blessing:
157712**
157713** May you do good and not evil.
157714** May you find forgiveness for yourself and forgive others.
157715** May you share freely, never taking more than you give.
157716**
157717******************************************************************************
157718**
157719*/
157720#ifndef _FTSINT_H
157721#define _FTSINT_H
157722
157723#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
157724# define NDEBUG 1
157725#endif
157726
157727/* FTS3/FTS4 require virtual tables */
157728#ifdef SQLITE_OMIT_VIRTUALTABLE
157729# undef SQLITE_ENABLE_FTS3
157730# undef SQLITE_ENABLE_FTS4
157731#endif
157732
157733/*
157734** FTS4 is really an extension for FTS3. It is enabled using the
157735** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also all
157736** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
157737*/
157738#if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
157739# define SQLITE_ENABLE_FTS3
157740#endif
157741
157742#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
157743
157744/* If not building as part of the core, include sqlite3ext.h. */
157745#ifndef SQLITE_CORE
157746/* # include "sqlite3ext.h" */
157747SQLITE_EXTENSION_INIT3
157748#endif
157749
157750/* #include "sqlite3.h" */
157751/************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/
157752/************** Begin file fts3_tokenizer.h **********************************/
157753/*
157754** 2006 July 10
157755**
157756** The author disclaims copyright to this source code.
157757**
157758*************************************************************************
157759** Defines the interface to tokenizers used by fulltext-search. There
157760** are three basic components:
157761**
157762** sqlite3_tokenizer_module is a singleton defining the tokenizer
157763** interface functions. This is essentially the class structure for
157764** tokenizers.
157765**
157766** sqlite3_tokenizer is used to define a particular tokenizer, perhaps
157767** including customization information defined at creation time.
157768**
157769** sqlite3_tokenizer_cursor is generated by a tokenizer to generate
157770** tokens from a particular input.
157771*/
157772#ifndef _FTS3_TOKENIZER_H_
157773#define _FTS3_TOKENIZER_H_
157774
157775/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
157776** If tokenizers are to be allowed to call sqlite3_*() functions, then
157777** we will need a way to register the API consistently.
157778*/
157779/* #include "sqlite3.h" */
157780
157781/*
157782** Structures used by the tokenizer interface. When a new tokenizer
157783** implementation is registered, the caller provides a pointer to
157784** an sqlite3_tokenizer_module containing pointers to the callback
157785** functions that make up an implementation.
157786**
157787** When an fts3 table is created, it passes any arguments passed to
157788** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the
157789** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer
157790** implementation. The xCreate() function in turn returns an
157791** sqlite3_tokenizer structure representing the specific tokenizer to
157792** be used for the fts3 table (customized by the tokenizer clause arguments).
157793**
157794** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen()
157795** method is called. It returns an sqlite3_tokenizer_cursor object
157796** that may be used to tokenize a specific input buffer based on
157797** the tokenization rules supplied by a specific sqlite3_tokenizer
157798** object.
157799*/
157800typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module;
157801typedef struct sqlite3_tokenizer sqlite3_tokenizer;
157802typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor;
157803
157804struct sqlite3_tokenizer_module {
157805
157806 /*
157807 ** Structure version. Should always be set to 0 or 1.
157808 */
157809 int iVersion;
157810
157811 /*
157812 ** Create a new tokenizer. The values in the argv[] array are the
157813 ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL
157814 ** TABLE statement that created the fts3 table. For example, if
157815 ** the following SQL is executed:
157816 **
157817 ** CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
157818 **
157819 ** then argc is set to 2, and the argv[] array contains pointers
157820 ** to the strings "arg1" and "arg2".
157821 **
157822 ** This method should return either SQLITE_OK (0), or an SQLite error
157823 ** code. If SQLITE_OK is returned, then *ppTokenizer should be set
157824 ** to point at the newly created tokenizer structure. The generic
157825 ** sqlite3_tokenizer.pModule variable should not be initialized by
157826 ** this callback. The caller will do so.
157827 */
157828 int (*xCreate)(
157829 int argc, /* Size of argv array */
157830 const char *const*argv, /* Tokenizer argument strings */
157831 sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */
157832 );
157833
157834 /*
157835 ** Destroy an existing tokenizer. The fts3 module calls this method
157836 ** exactly once for each successful call to xCreate().
157837 */
157838 int (*xDestroy)(sqlite3_tokenizer *pTokenizer);
157839
157840 /*
157841 ** Create a tokenizer cursor to tokenize an input buffer. The caller
157842 ** is responsible for ensuring that the input buffer remains valid
157843 ** until the cursor is closed (using the xClose() method).
157844 */
157845 int (*xOpen)(
157846 sqlite3_tokenizer *pTokenizer, /* Tokenizer object */
157847 const char *pInput, int nBytes, /* Input buffer */
157848 sqlite3_tokenizer_cursor **ppCursor /* OUT: Created tokenizer cursor */
157849 );
157850
157851 /*
157852 ** Destroy an existing tokenizer cursor. The fts3 module calls this
157853 ** method exactly once for each successful call to xOpen().
157854 */
157855 int (*xClose)(sqlite3_tokenizer_cursor *pCursor);
157856
157857 /*
157858 ** Retrieve the next token from the tokenizer cursor pCursor. This
157859 ** method should either return SQLITE_OK and set the values of the
157860 ** "OUT" variables identified below, or SQLITE_DONE to indicate that
157861 ** the end of the buffer has been reached, or an SQLite error code.
157862 **
157863 ** *ppToken should be set to point at a buffer containing the
157864 ** normalized version of the token (i.e. after any case-folding and/or
157865 ** stemming has been performed). *pnBytes should be set to the length
157866 ** of this buffer in bytes. The input text that generated the token is
157867 ** identified by the byte offsets returned in *piStartOffset and
157868 ** *piEndOffset. *piStartOffset should be set to the index of the first
157869 ** byte of the token in the input buffer. *piEndOffset should be set
157870 ** to the index of the first byte just past the end of the token in
157871 ** the input buffer.
157872 **
157873 ** The buffer *ppToken is set to point at is managed by the tokenizer
157874 ** implementation. It is only required to be valid until the next call
157875 ** to xNext() or xClose().
157876 */
157877 /* TODO(shess) current implementation requires pInput to be
157878 ** nul-terminated. This should either be fixed, or pInput/nBytes
157879 ** should be converted to zInput.
157880 */
157881 int (*xNext)(
157882 sqlite3_tokenizer_cursor *pCursor, /* Tokenizer cursor */
157883 const char **ppToken, int *pnBytes, /* OUT: Normalized text for token */
157884 int *piStartOffset, /* OUT: Byte offset of token in input buffer */
157885 int *piEndOffset, /* OUT: Byte offset of end of token in input buffer */
157886 int *piPosition /* OUT: Number of tokens returned before this one */
157887 );
157888
157889 /***********************************************************************
157890 ** Methods below this point are only available if iVersion>=1.
157891 */
157892
157893 /*
157894 ** Configure the language id of a tokenizer cursor.
157895 */
157896 int (*xLanguageid)(sqlite3_tokenizer_cursor *pCsr, int iLangid);
157897};
157898
157899struct sqlite3_tokenizer {
157900 const sqlite3_tokenizer_module *pModule; /* The module for this tokenizer */
157901 /* Tokenizer implementations will typically add additional fields */
157902};
157903
157904struct sqlite3_tokenizer_cursor {
157905 sqlite3_tokenizer *pTokenizer; /* Tokenizer for this cursor. */
157906 /* Tokenizer implementations will typically add additional fields */
157907};
157908
157909int fts3_global_term_cnt(int iTerm, int iCol);
157910int fts3_term_cnt(int iTerm, int iCol);
157911
157912
157913#endif /* _FTS3_TOKENIZER_H_ */
157914
157915/************** End of fts3_tokenizer.h **************************************/
157916/************** Continuing where we left off in fts3Int.h ********************/
157917/************** Include fts3_hash.h in the middle of fts3Int.h ***************/
157918/************** Begin file fts3_hash.h ***************************************/
157919/*
157920** 2001 September 22
157921**
157922** The author disclaims copyright to this source code. In place of
157923** a legal notice, here is a blessing:
157924**
157925** May you do good and not evil.
157926** May you find forgiveness for yourself and forgive others.
157927** May you share freely, never taking more than you give.
157928**
157929*************************************************************************
157930** This is the header file for the generic hash-table implementation
157931** used in SQLite. We've modified it slightly to serve as a standalone
157932** hash table implementation for the full-text indexing module.
157933**
157934*/
157935#ifndef _FTS3_HASH_H_
157936#define _FTS3_HASH_H_
157937
157938/* Forward declarations of structures. */
157939typedef struct Fts3Hash Fts3Hash;
157940typedef struct Fts3HashElem Fts3HashElem;
157941
157942/* A complete hash table is an instance of the following structure.
157943** The internals of this structure are intended to be opaque -- client
157944** code should not attempt to access or modify the fields of this structure
157945** directly. Change this structure only by using the routines below.
157946** However, many of the "procedures" and "functions" for modifying and
157947** accessing this structure are really macros, so we can't really make
157948** this structure opaque.
157949*/
157950struct Fts3Hash {
157951 char keyClass; /* HASH_INT, _POINTER, _STRING, _BINARY */
157952 char copyKey; /* True if copy of key made on insert */
157953 int count; /* Number of entries in this table */
157954 Fts3HashElem *first; /* The first element of the array */
157955 int htsize; /* Number of buckets in the hash table */
157956 struct _fts3ht { /* the hash table */
157957 int count; /* Number of entries with this hash */
157958 Fts3HashElem *chain; /* Pointer to first entry with this hash */
157959 } *ht;
157960};
157961
157962/* Each element in the hash table is an instance of the following
157963** structure. All elements are stored on a single doubly-linked list.
157964**
157965** Again, this structure is intended to be opaque, but it can't really
157966** be opaque because it is used by macros.
157967*/
157968struct Fts3HashElem {
157969 Fts3HashElem *next, *prev; /* Next and previous elements in the table */
157970 void *data; /* Data associated with this element */
157971 void *pKey; int nKey; /* Key associated with this element */
157972};
157973
157974/*
157975** There are 2 different modes of operation for a hash table:
157976**
157977** FTS3_HASH_STRING pKey points to a string that is nKey bytes long
157978** (including the null-terminator, if any). Case
157979** is respected in comparisons.
157980**
157981** FTS3_HASH_BINARY pKey points to binary data nKey bytes long.
157982** memcmp() is used to compare keys.
157983**
157984** A copy of the key is made if the copyKey parameter to fts3HashInit is 1.
157985*/
157986#define FTS3_HASH_STRING 1
157987#define FTS3_HASH_BINARY 2
157988
157989/*
157990** Access routines. To delete, insert a NULL pointer.
157991*/
157992SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey);
157993SQLITE_PRIVATE void *sqlite3Fts3HashInsert(Fts3Hash*, const void *pKey, int nKey, void *pData);
157994SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash*, const void *pKey, int nKey);
157995SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash*);
157996SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(const Fts3Hash *, const void *, int);
157997
157998/*
157999** Shorthand for the functions above
158000*/
158001#define fts3HashInit sqlite3Fts3HashInit
158002#define fts3HashInsert sqlite3Fts3HashInsert
158003#define fts3HashFind sqlite3Fts3HashFind
158004#define fts3HashClear sqlite3Fts3HashClear
158005#define fts3HashFindElem sqlite3Fts3HashFindElem
158006
158007/*
158008** Macros for looping over all elements of a hash table. The idiom is
158009** like this:
158010**
158011** Fts3Hash h;
158012** Fts3HashElem *p;
158013** ...
158014** for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){
158015** SomeStructure *pData = fts3HashData(p);
158016** // do something with pData
158017** }
158018*/
158019#define fts3HashFirst(H) ((H)->first)
158020#define fts3HashNext(E) ((E)->next)
158021#define fts3HashData(E) ((E)->data)
158022#define fts3HashKey(E) ((E)->pKey)
158023#define fts3HashKeysize(E) ((E)->nKey)
158024
158025/*
158026** Number of entries in a hash table
158027*/
158028#define fts3HashCount(H) ((H)->count)
158029
158030#endif /* _FTS3_HASH_H_ */
158031
158032/************** End of fts3_hash.h *******************************************/
158033/************** Continuing where we left off in fts3Int.h ********************/
158034
158035/*
158036** This constant determines the maximum depth of an FTS expression tree
158037** that the library will create and use. FTS uses recursion to perform
158038** various operations on the query tree, so the disadvantage of a large
158039** limit is that it may allow very large queries to use large amounts
158040** of stack space (perhaps causing a stack overflow).
158041*/
158042#ifndef SQLITE_FTS3_MAX_EXPR_DEPTH
158043# define SQLITE_FTS3_MAX_EXPR_DEPTH 12
158044#endif
158045
158046
158047/*
158048** This constant controls how often segments are merged. Once there are
158049** FTS3_MERGE_COUNT segments of level N, they are merged into a single
158050** segment of level N+1.
158051*/
158052#define FTS3_MERGE_COUNT 16
158053
158054/*
158055** This is the maximum amount of data (in bytes) to store in the
158056** Fts3Table.pendingTerms hash table. Normally, the hash table is
158057** populated as documents are inserted/updated/deleted in a transaction
158058** and used to create a new segment when the transaction is committed.
158059** However if this limit is reached midway through a transaction, a new
158060** segment is created and the hash table cleared immediately.
158061*/
158062#define FTS3_MAX_PENDING_DATA (1*1024*1024)
158063
158064/*
158065** Macro to return the number of elements in an array. SQLite has a
158066** similar macro called ArraySize(). Use a different name to avoid
158067** a collision when building an amalgamation with built-in FTS3.
158068*/
158069#define SizeofArray(X) ((int)(sizeof(X)/sizeof(X[0])))
158070
158071
158072#ifndef MIN
158073# define MIN(x,y) ((x)<(y)?(x):(y))
158074#endif
158075#ifndef MAX
158076# define MAX(x,y) ((x)>(y)?(x):(y))
158077#endif
158078
158079/*
158080** Maximum length of a varint encoded integer. The varint format is different
158081** from that used by SQLite, so the maximum length is 10, not 9.
158082*/
158083#define FTS3_VARINT_MAX 10
158084
158085/*
158086** FTS4 virtual tables may maintain multiple indexes - one index of all terms
158087** in the document set and zero or more prefix indexes. All indexes are stored
158088** as one or more b+-trees in the %_segments and %_segdir tables.
158089**
158090** It is possible to determine which index a b+-tree belongs to based on the
158091** value stored in the "%_segdir.level" column. Given this value L, the index
158092** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with
158093** level values between 0 and 1023 (inclusive) belong to index 0, all levels
158094** between 1024 and 2047 to index 1, and so on.
158095**
158096** It is considered impossible for an index to use more than 1024 levels. In
158097** theory though this may happen, but only after at least
158098** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables.
158099*/
158100#define FTS3_SEGDIR_MAXLEVEL 1024
158101#define FTS3_SEGDIR_MAXLEVEL_STR "1024"
158102
158103/*
158104** The testcase() macro is only used by the amalgamation. If undefined,
158105** make it a no-op.
158106*/
158107#ifndef testcase
158108# define testcase(X)
158109#endif
158110
158111/*
158112** Terminator values for position-lists and column-lists.
158113*/
158114#define POS_COLUMN (1) /* Column-list terminator */
158115#define POS_END (0) /* Position-list terminator */
158116
158117/*
158118** This section provides definitions to allow the
158119** FTS3 extension to be compiled outside of the
158120** amalgamation.
158121*/
158122#ifndef SQLITE_AMALGAMATION
158123/*
158124** Macros indicating that conditional expressions are always true or
158125** false.
158126*/
158127#ifdef SQLITE_COVERAGE_TEST
158128# define ALWAYS(x) (1)
158129# define NEVER(X) (0)
158130#elif defined(SQLITE_DEBUG)
158131# define ALWAYS(x) sqlite3Fts3Always((x)!=0)
158132# define NEVER(x) sqlite3Fts3Never((x)!=0)
158133SQLITE_PRIVATE int sqlite3Fts3Always(int b);
158134SQLITE_PRIVATE int sqlite3Fts3Never(int b);
158135#else
158136# define ALWAYS(x) (x)
158137# define NEVER(x) (x)
158138#endif
158139
158140/*
158141** Internal types used by SQLite.
158142*/
158143typedef unsigned char u8; /* 1-byte (or larger) unsigned integer */
158144typedef short int i16; /* 2-byte (or larger) signed integer */
158145typedef unsigned int u32; /* 4-byte unsigned integer */
158146typedef sqlite3_uint64 u64; /* 8-byte unsigned integer */
158147typedef sqlite3_int64 i64; /* 8-byte signed integer */
158148
158149/*
158150** Macro used to suppress compiler warnings for unused parameters.
158151*/
158152#define UNUSED_PARAMETER(x) (void)(x)
158153
158154/*
158155** Activate assert() only if SQLITE_TEST is enabled.
158156*/
158157#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
158158# define NDEBUG 1
158159#endif
158160
158161/*
158162** The TESTONLY macro is used to enclose variable declarations or
158163** other bits of code that are needed to support the arguments
158164** within testcase() and assert() macros.
158165*/
158166#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
158167# define TESTONLY(X) X
158168#else
158169# define TESTONLY(X)
158170#endif
158171
158172#endif /* SQLITE_AMALGAMATION */
158173
158174#ifdef SQLITE_DEBUG
158175SQLITE_PRIVATE int sqlite3Fts3Corrupt(void);
158176# define FTS_CORRUPT_VTAB sqlite3Fts3Corrupt()
158177#else
158178# define FTS_CORRUPT_VTAB SQLITE_CORRUPT_VTAB
158179#endif
158180
158181typedef struct Fts3Table Fts3Table;
158182typedef struct Fts3Cursor Fts3Cursor;
158183typedef struct Fts3Expr Fts3Expr;
158184typedef struct Fts3Phrase Fts3Phrase;
158185typedef struct Fts3PhraseToken Fts3PhraseToken;
158186
158187typedef struct Fts3Doclist Fts3Doclist;
158188typedef struct Fts3SegFilter Fts3SegFilter;
158189typedef struct Fts3DeferredToken Fts3DeferredToken;
158190typedef struct Fts3SegReader Fts3SegReader;
158191typedef struct Fts3MultiSegReader Fts3MultiSegReader;
158192
158193typedef struct MatchinfoBuffer MatchinfoBuffer;
158194
158195/*
158196** A connection to a fulltext index is an instance of the following
158197** structure. The xCreate and xConnect methods create an instance
158198** of this structure and xDestroy and xDisconnect free that instance.
158199** All other methods receive a pointer to the structure as one of their
158200** arguments.
158201*/
158202struct Fts3Table {
158203 sqlite3_vtab base; /* Base class used by SQLite core */
158204 sqlite3 *db; /* The database connection */
158205 const char *zDb; /* logical database name */
158206 const char *zName; /* virtual table name */
158207 int nColumn; /* number of named columns in virtual table */
158208 char **azColumn; /* column names. malloced */
158209 u8 *abNotindexed; /* True for 'notindexed' columns */
158210 sqlite3_tokenizer *pTokenizer; /* tokenizer for inserts and queries */
158211 char *zContentTbl; /* content=xxx option, or NULL */
158212 char *zLanguageid; /* languageid=xxx option, or NULL */
158213 int nAutoincrmerge; /* Value configured by 'automerge' */
158214 u32 nLeafAdd; /* Number of leaf blocks added this trans */
158215
158216 /* Precompiled statements used by the implementation. Each of these
158217 ** statements is run and reset within a single virtual table API call.
158218 */
158219 sqlite3_stmt *aStmt[40];
158220 sqlite3_stmt *pSeekStmt; /* Cache for fts3CursorSeekStmt() */
158221
158222 char *zReadExprlist;
158223 char *zWriteExprlist;
158224
158225 int nNodeSize; /* Soft limit for node size */
158226 u8 bFts4; /* True for FTS4, false for FTS3 */
158227 u8 bHasStat; /* True if %_stat table exists (2==unknown) */
158228 u8 bHasDocsize; /* True if %_docsize table exists */
158229 u8 bDescIdx; /* True if doclists are in reverse order */
158230 u8 bIgnoreSavepoint; /* True to ignore xSavepoint invocations */
158231 int nPgsz; /* Page size for host database */
158232 char *zSegmentsTbl; /* Name of %_segments table */
158233 sqlite3_blob *pSegments; /* Blob handle open on %_segments table */
158234
158235 /*
158236 ** The following array of hash tables is used to buffer pending index
158237 ** updates during transactions. All pending updates buffered at any one
158238 ** time must share a common language-id (see the FTS4 langid= feature).
158239 ** The current language id is stored in variable iPrevLangid.
158240 **
158241 ** A single FTS4 table may have multiple full-text indexes. For each index
158242 ** there is an entry in the aIndex[] array. Index 0 is an index of all the
158243 ** terms that appear in the document set. Each subsequent index in aIndex[]
158244 ** is an index of prefixes of a specific length.
158245 **
158246 ** Variable nPendingData contains an estimate the memory consumed by the
158247 ** pending data structures, including hash table overhead, but not including
158248 ** malloc overhead. When nPendingData exceeds nMaxPendingData, all hash
158249 ** tables are flushed to disk. Variable iPrevDocid is the docid of the most
158250 ** recently inserted record.
158251 */
158252 int nIndex; /* Size of aIndex[] */
158253 struct Fts3Index {
158254 int nPrefix; /* Prefix length (0 for main terms index) */
158255 Fts3Hash hPending; /* Pending terms table for this index */
158256 } *aIndex;
158257 int nMaxPendingData; /* Max pending data before flush to disk */
158258 int nPendingData; /* Current bytes of pending data */
158259 sqlite_int64 iPrevDocid; /* Docid of most recently inserted document */
158260 int iPrevLangid; /* Langid of recently inserted document */
158261 int bPrevDelete; /* True if last operation was a delete */
158262
158263#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
158264 /* State variables used for validating that the transaction control
158265 ** methods of the virtual table are called at appropriate times. These
158266 ** values do not contribute to FTS functionality; they are used for
158267 ** verifying the operation of the SQLite core.
158268 */
158269 int inTransaction; /* True after xBegin but before xCommit/xRollback */
158270 int mxSavepoint; /* Largest valid xSavepoint integer */
158271#endif
158272
158273#ifdef SQLITE_TEST
158274 /* True to disable the incremental doclist optimization. This is controled
158275 ** by special insert command 'test-no-incr-doclist'. */
158276 int bNoIncrDoclist;
158277#endif
158278};
158279
158280/*
158281** When the core wants to read from the virtual table, it creates a
158282** virtual table cursor (an instance of the following structure) using
158283** the xOpen method. Cursors are destroyed using the xClose method.
158284*/
158285struct Fts3Cursor {
158286 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
158287 i16 eSearch; /* Search strategy (see below) */
158288 u8 isEof; /* True if at End Of Results */
158289 u8 isRequireSeek; /* True if must seek pStmt to %_content row */
158290 u8 bSeekStmt; /* True if pStmt is a seek */
158291 sqlite3_stmt *pStmt; /* Prepared statement in use by the cursor */
158292 Fts3Expr *pExpr; /* Parsed MATCH query string */
158293 int iLangid; /* Language being queried for */
158294 int nPhrase; /* Number of matchable phrases in query */
158295 Fts3DeferredToken *pDeferred; /* Deferred search tokens, if any */
158296 sqlite3_int64 iPrevId; /* Previous id read from aDoclist */
158297 char *pNextId; /* Pointer into the body of aDoclist */
158298 char *aDoclist; /* List of docids for full-text queries */
158299 int nDoclist; /* Size of buffer at aDoclist */
158300 u8 bDesc; /* True to sort in descending order */
158301 int eEvalmode; /* An FTS3_EVAL_XX constant */
158302 int nRowAvg; /* Average size of database rows, in pages */
158303 sqlite3_int64 nDoc; /* Documents in table */
158304 i64 iMinDocid; /* Minimum docid to return */
158305 i64 iMaxDocid; /* Maximum docid to return */
158306 int isMatchinfoNeeded; /* True when aMatchinfo[] needs filling in */
158307 MatchinfoBuffer *pMIBuffer; /* Buffer for matchinfo data */
158308};
158309
158310#define FTS3_EVAL_FILTER 0
158311#define FTS3_EVAL_NEXT 1
158312#define FTS3_EVAL_MATCHINFO 2
158313
158314/*
158315** The Fts3Cursor.eSearch member is always set to one of the following.
158316** Actualy, Fts3Cursor.eSearch can be greater than or equal to
158317** FTS3_FULLTEXT_SEARCH. If so, then Fts3Cursor.eSearch - 2 is the index
158318** of the column to be searched. For example, in
158319**
158320** CREATE VIRTUAL TABLE ex1 USING fts3(a,b,c,d);
158321** SELECT docid FROM ex1 WHERE b MATCH 'one two three';
158322**
158323** Because the LHS of the MATCH operator is 2nd column "b",
158324** Fts3Cursor.eSearch will be set to FTS3_FULLTEXT_SEARCH+1. (+0 for a,
158325** +1 for b, +2 for c, +3 for d.) If the LHS of MATCH were "ex1"
158326** indicating that all columns should be searched,
158327** then eSearch would be set to FTS3_FULLTEXT_SEARCH+4.
158328*/
158329#define FTS3_FULLSCAN_SEARCH 0 /* Linear scan of %_content table */
158330#define FTS3_DOCID_SEARCH 1 /* Lookup by rowid on %_content table */
158331#define FTS3_FULLTEXT_SEARCH 2 /* Full-text index search */
158332
158333/*
158334** The lower 16-bits of the sqlite3_index_info.idxNum value set by
158335** the xBestIndex() method contains the Fts3Cursor.eSearch value described
158336** above. The upper 16-bits contain a combination of the following
158337** bits, used to describe extra constraints on full-text searches.
158338*/
158339#define FTS3_HAVE_LANGID 0x00010000 /* languageid=? */
158340#define FTS3_HAVE_DOCID_GE 0x00020000 /* docid>=? */
158341#define FTS3_HAVE_DOCID_LE 0x00040000 /* docid<=? */
158342
158343struct Fts3Doclist {
158344 char *aAll; /* Array containing doclist (or NULL) */
158345 int nAll; /* Size of a[] in bytes */
158346 char *pNextDocid; /* Pointer to next docid */
158347
158348 sqlite3_int64 iDocid; /* Current docid (if pList!=0) */
158349 int bFreeList; /* True if pList should be sqlite3_free()d */
158350 char *pList; /* Pointer to position list following iDocid */
158351 int nList; /* Length of position list */
158352};
158353
158354/*
158355** A "phrase" is a sequence of one or more tokens that must match in
158356** sequence. A single token is the base case and the most common case.
158357** For a sequence of tokens contained in double-quotes (i.e. "one two three")
158358** nToken will be the number of tokens in the string.
158359*/
158360struct Fts3PhraseToken {
158361 char *z; /* Text of the token */
158362 int n; /* Number of bytes in buffer z */
158363 int isPrefix; /* True if token ends with a "*" character */
158364 int bFirst; /* True if token must appear at position 0 */
158365
158366 /* Variables above this point are populated when the expression is
158367 ** parsed (by code in fts3_expr.c). Below this point the variables are
158368 ** used when evaluating the expression. */
158369 Fts3DeferredToken *pDeferred; /* Deferred token object for this token */
158370 Fts3MultiSegReader *pSegcsr; /* Segment-reader for this token */
158371};
158372
158373struct Fts3Phrase {
158374 /* Cache of doclist for this phrase. */
158375 Fts3Doclist doclist;
158376 int bIncr; /* True if doclist is loaded incrementally */
158377 int iDoclistToken;
158378
158379 /* Used by sqlite3Fts3EvalPhrasePoslist() if this is a descendent of an
158380 ** OR condition. */
158381 char *pOrPoslist;
158382 i64 iOrDocid;
158383
158384 /* Variables below this point are populated by fts3_expr.c when parsing
158385 ** a MATCH expression. Everything above is part of the evaluation phase.
158386 */
158387 int nToken; /* Number of tokens in the phrase */
158388 int iColumn; /* Index of column this phrase must match */
158389 Fts3PhraseToken aToken[1]; /* One entry for each token in the phrase */
158390};
158391
158392/*
158393** A tree of these objects forms the RHS of a MATCH operator.
158394**
158395** If Fts3Expr.eType is FTSQUERY_PHRASE and isLoaded is true, then aDoclist
158396** points to a malloced buffer, size nDoclist bytes, containing the results
158397** of this phrase query in FTS3 doclist format. As usual, the initial
158398** "Length" field found in doclists stored on disk is omitted from this
158399** buffer.
158400**
158401** Variable aMI is used only for FTSQUERY_NEAR nodes to store the global
158402** matchinfo data. If it is not NULL, it points to an array of size nCol*3,
158403** where nCol is the number of columns in the queried FTS table. The array
158404** is populated as follows:
158405**
158406** aMI[iCol*3 + 0] = Undefined
158407** aMI[iCol*3 + 1] = Number of occurrences
158408** aMI[iCol*3 + 2] = Number of rows containing at least one instance
158409**
158410** The aMI array is allocated using sqlite3_malloc(). It should be freed
158411** when the expression node is.
158412*/
158413struct Fts3Expr {
158414 int eType; /* One of the FTSQUERY_XXX values defined below */
158415 int nNear; /* Valid if eType==FTSQUERY_NEAR */
158416 Fts3Expr *pParent; /* pParent->pLeft==this or pParent->pRight==this */
158417 Fts3Expr *pLeft; /* Left operand */
158418 Fts3Expr *pRight; /* Right operand */
158419 Fts3Phrase *pPhrase; /* Valid if eType==FTSQUERY_PHRASE */
158420
158421 /* The following are used by the fts3_eval.c module. */
158422 sqlite3_int64 iDocid; /* Current docid */
158423 u8 bEof; /* True this expression is at EOF already */
158424 u8 bStart; /* True if iDocid is valid */
158425 u8 bDeferred; /* True if this expression is entirely deferred */
158426
158427 /* The following are used by the fts3_snippet.c module. */
158428 int iPhrase; /* Index of this phrase in matchinfo() results */
158429 u32 *aMI; /* See above */
158430};
158431
158432/*
158433** Candidate values for Fts3Query.eType. Note that the order of the first
158434** four values is in order of precedence when parsing expressions. For
158435** example, the following:
158436**
158437** "a OR b AND c NOT d NEAR e"
158438**
158439** is equivalent to:
158440**
158441** "a OR (b AND (c NOT (d NEAR e)))"
158442*/
158443#define FTSQUERY_NEAR 1
158444#define FTSQUERY_NOT 2
158445#define FTSQUERY_AND 3
158446#define FTSQUERY_OR 4
158447#define FTSQUERY_PHRASE 5
158448
158449
158450/* fts3_write.c */
158451SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*);
158452SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *);
158453SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *);
158454SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *);
158455SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(int, int, sqlite3_int64,
158456 sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**);
158457SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
158458 Fts3Table*,int,const char*,int,int,Fts3SegReader**);
158459SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *);
158460SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table*, int, int, int, sqlite3_stmt **);
158461SQLITE_PRIVATE int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*, int*);
158462
158463SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
158464SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
158465
158466#ifndef SQLITE_DISABLE_FTS4_DEFERRED
158467SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
158468SQLITE_PRIVATE int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
158469SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
158470SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
158471SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
158472#else
158473# define sqlite3Fts3FreeDeferredTokens(x)
158474# define sqlite3Fts3DeferToken(x,y,z) SQLITE_OK
158475# define sqlite3Fts3CacheDeferredDoclists(x) SQLITE_OK
158476# define sqlite3Fts3FreeDeferredDoclists(x)
158477# define sqlite3Fts3DeferredTokenList(x,y,z) SQLITE_OK
158478#endif
158479
158480SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *);
158481SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *, int *);
158482
158483/* Special values interpreted by sqlite3SegReaderCursor() */
158484#define FTS3_SEGCURSOR_PENDING -1
158485#define FTS3_SEGCURSOR_ALL -2
158486
158487SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3MultiSegReader*, Fts3SegFilter*);
158488SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3MultiSegReader *);
158489SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(Fts3MultiSegReader *);
158490
158491SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(Fts3Table *,
158492 int, int, int, const char *, int, int, int, Fts3MultiSegReader *);
158493
158494/* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
158495#define FTS3_SEGMENT_REQUIRE_POS 0x00000001
158496#define FTS3_SEGMENT_IGNORE_EMPTY 0x00000002
158497#define FTS3_SEGMENT_COLUMN_FILTER 0x00000004
158498#define FTS3_SEGMENT_PREFIX 0x00000008
158499#define FTS3_SEGMENT_SCAN 0x00000010
158500#define FTS3_SEGMENT_FIRST 0x00000020
158501
158502/* Type passed as 4th argument to SegmentReaderIterate() */
158503struct Fts3SegFilter {
158504 const char *zTerm;
158505 int nTerm;
158506 int iCol;
158507 int flags;
158508};
158509
158510struct Fts3MultiSegReader {
158511 /* Used internally by sqlite3Fts3SegReaderXXX() calls */
158512 Fts3SegReader **apSegment; /* Array of Fts3SegReader objects */
158513 int nSegment; /* Size of apSegment array */
158514 int nAdvance; /* How many seg-readers to advance */
158515 Fts3SegFilter *pFilter; /* Pointer to filter object */
158516 char *aBuffer; /* Buffer to merge doclists in */
158517 int nBuffer; /* Allocated size of aBuffer[] in bytes */
158518
158519 int iColFilter; /* If >=0, filter for this column */
158520 int bRestart;
158521
158522 /* Used by fts3.c only. */
158523 int nCost; /* Cost of running iterator */
158524 int bLookup; /* True if a lookup of a single entry. */
158525
158526 /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */
158527 char *zTerm; /* Pointer to term buffer */
158528 int nTerm; /* Size of zTerm in bytes */
158529 char *aDoclist; /* Pointer to doclist buffer */
158530 int nDoclist; /* Size of aDoclist[] in bytes */
158531};
158532
158533SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table*,int,int);
158534
158535#define fts3GetVarint32(p, piVal) ( \
158536 (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, piVal) : (*piVal=*(u8*)(p), 1) \
158537)
158538
158539/* fts3.c */
158540SQLITE_PRIVATE void sqlite3Fts3ErrMsg(char**,const char*,...);
158541SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64);
158542SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
158543SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *);
158544SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64);
158545SQLITE_PRIVATE void sqlite3Fts3Dequote(char *);
158546SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
158547SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
158548SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *);
158549SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int*, Fts3Table*);
158550SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc);
158551
158552/* fts3_tokenizer.c */
158553SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *);
158554SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
158555SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *,
158556 sqlite3_tokenizer **, char **
158557);
158558SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char);
158559
158560/* fts3_snippet.c */
158561SQLITE_PRIVATE void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
158562SQLITE_PRIVATE void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
158563 const char *, const char *, int, int
158564);
158565SQLITE_PRIVATE void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
158566SQLITE_PRIVATE void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p);
158567
158568/* fts3_expr.c */
158569SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
158570 char **, int, int, int, const char *, int, Fts3Expr **, char **
158571);
158572SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *);
158573#ifdef SQLITE_TEST
158574SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash*);
158575SQLITE_PRIVATE int sqlite3Fts3InitTerm(sqlite3 *db);
158576#endif
158577
158578SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(sqlite3_tokenizer *, int, const char *, int,
158579 sqlite3_tokenizer_cursor **
158580);
158581
158582/* fts3_aux.c */
158583SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db);
158584
158585SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *);
158586
158587SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
158588 Fts3Table*, Fts3MultiSegReader*, int, const char*, int);
158589SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
158590 Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
158591SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **);
158592SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
158593SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
158594
158595/* fts3_tokenize_vtab.c */
158596SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *);
158597
158598/* fts3_unicode2.c (functions generated by parsing unicode text files) */
158599#ifndef SQLITE_DISABLE_FTS3_UNICODE
158600SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int, int);
158601SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int);
158602SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int);
158603#endif
158604
158605#endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */
158606#endif /* _FTSINT_H */
158607
158608/************** End of fts3Int.h *********************************************/
158609/************** Continuing where we left off in fts3.c ***********************/
158610#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
158611
158612#if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE)
158613# define SQLITE_CORE 1
158614#endif
158615
158616/* #include <assert.h> */
158617/* #include <stdlib.h> */
158618/* #include <stddef.h> */
158619/* #include <stdio.h> */
158620/* #include <string.h> */
158621/* #include <stdarg.h> */
158622
158623/* #include "fts3.h" */
158624#ifndef SQLITE_CORE
158625/* # include "sqlite3ext.h" */
158626 SQLITE_EXTENSION_INIT1
158627#endif
158628
158629static int fts3EvalNext(Fts3Cursor *pCsr);
158630static int fts3EvalStart(Fts3Cursor *pCsr);
158631static int fts3TermSegReaderCursor(
158632 Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **);
158633
158634#ifndef SQLITE_AMALGAMATION
158635# if defined(SQLITE_DEBUG)
158636SQLITE_PRIVATE int sqlite3Fts3Always(int b) { assert( b ); return b; }
158637SQLITE_PRIVATE int sqlite3Fts3Never(int b) { assert( !b ); return b; }
158638# endif
158639#endif
158640
158641/*
158642** Write a 64-bit variable-length integer to memory starting at p[0].
158643** The length of data written will be between 1 and FTS3_VARINT_MAX bytes.
158644** The number of bytes written is returned.
158645*/
158646SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
158647 unsigned char *q = (unsigned char *) p;
158648 sqlite_uint64 vu = v;
158649 do{
158650 *q++ = (unsigned char) ((vu & 0x7f) | 0x80);
158651 vu >>= 7;
158652 }while( vu!=0 );
158653 q[-1] &= 0x7f; /* turn off high bit in final byte */
158654 assert( q - (unsigned char *)p <= FTS3_VARINT_MAX );
158655 return (int) (q - (unsigned char *)p);
158656}
158657
158658#define GETVARINT_STEP(v, ptr, shift, mask1, mask2, var, ret) \
158659 v = (v & mask1) | ( (*ptr++) << shift ); \
158660 if( (v & mask2)==0 ){ var = v; return ret; }
158661#define GETVARINT_INIT(v, ptr, shift, mask1, mask2, var, ret) \
158662 v = (*ptr++); \
158663 if( (v & mask2)==0 ){ var = v; return ret; }
158664
158665/*
158666** Read a 64-bit variable-length integer from memory starting at p[0].
158667** Return the number of bytes read, or 0 on error.
158668** The value is stored in *v.
158669*/
158670SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *pBuf, sqlite_int64 *v){
158671 const unsigned char *p = (const unsigned char*)pBuf;
158672 const unsigned char *pStart = p;
158673 u32 a;
158674 u64 b;
158675 int shift;
158676
158677 GETVARINT_INIT(a, p, 0, 0x00, 0x80, *v, 1);
158678 GETVARINT_STEP(a, p, 7, 0x7F, 0x4000, *v, 2);
158679 GETVARINT_STEP(a, p, 14, 0x3FFF, 0x200000, *v, 3);
158680 GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *v, 4);
158681 b = (a & 0x0FFFFFFF );
158682
158683 for(shift=28; shift<=63; shift+=7){
158684 u64 c = *p++;
158685 b += (c&0x7F) << shift;
158686 if( (c & 0x80)==0 ) break;
158687 }
158688 *v = b;
158689 return (int)(p - pStart);
158690}
158691
158692/*
158693** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to
158694** a non-negative 32-bit integer before it is returned.
158695*/
158696SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *p, int *pi){
158697 u32 a;
158698
158699#ifndef fts3GetVarint32
158700 GETVARINT_INIT(a, p, 0, 0x00, 0x80, *pi, 1);
158701#else
158702 a = (*p++);
158703 assert( a & 0x80 );
158704#endif
158705
158706 GETVARINT_STEP(a, p, 7, 0x7F, 0x4000, *pi, 2);
158707 GETVARINT_STEP(a, p, 14, 0x3FFF, 0x200000, *pi, 3);
158708 GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *pi, 4);
158709 a = (a & 0x0FFFFFFF );
158710 *pi = (int)(a | ((u32)(*p & 0x07) << 28));
158711 assert( 0==(a & 0x80000000) );
158712 assert( *pi>=0 );
158713 return 5;
158714}
158715
158716/*
158717** Return the number of bytes required to encode v as a varint
158718*/
158719SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64 v){
158720 int i = 0;
158721 do{
158722 i++;
158723 v >>= 7;
158724 }while( v!=0 );
158725 return i;
158726}
158727
158728/*
158729** Convert an SQL-style quoted string into a normal string by removing
158730** the quote characters. The conversion is done in-place. If the
158731** input does not begin with a quote character, then this routine
158732** is a no-op.
158733**
158734** Examples:
158735**
158736** "abc" becomes abc
158737** 'xyz' becomes xyz
158738** [pqr] becomes pqr
158739** `mno` becomes mno
158740**
158741*/
158742SQLITE_PRIVATE void sqlite3Fts3Dequote(char *z){
158743 char quote; /* Quote character (if any ) */
158744
158745 quote = z[0];
158746 if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
158747 int iIn = 1; /* Index of next byte to read from input */
158748 int iOut = 0; /* Index of next byte to write to output */
158749
158750 /* If the first byte was a '[', then the close-quote character is a ']' */
158751 if( quote=='[' ) quote = ']';
158752
158753 while( z[iIn] ){
158754 if( z[iIn]==quote ){
158755 if( z[iIn+1]!=quote ) break;
158756 z[iOut++] = quote;
158757 iIn += 2;
158758 }else{
158759 z[iOut++] = z[iIn++];
158760 }
158761 }
158762 z[iOut] = '\0';
158763 }
158764}
158765
158766/*
158767** Read a single varint from the doclist at *pp and advance *pp to point
158768** to the first byte past the end of the varint. Add the value of the varint
158769** to *pVal.
158770*/
158771static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){
158772 sqlite3_int64 iVal;
158773 *pp += sqlite3Fts3GetVarint(*pp, &iVal);
158774 *pVal += iVal;
158775}
158776
158777/*
158778** When this function is called, *pp points to the first byte following a
158779** varint that is part of a doclist (or position-list, or any other list
158780** of varints). This function moves *pp to point to the start of that varint,
158781** and sets *pVal by the varint value.
158782**
158783** Argument pStart points to the first byte of the doclist that the
158784** varint is part of.
158785*/
158786static void fts3GetReverseVarint(
158787 char **pp,
158788 char *pStart,
158789 sqlite3_int64 *pVal
158790){
158791 sqlite3_int64 iVal;
158792 char *p;
158793
158794 /* Pointer p now points at the first byte past the varint we are
158795 ** interested in. So, unless the doclist is corrupt, the 0x80 bit is
158796 ** clear on character p[-1]. */
158797 for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
158798 p++;
158799 *pp = p;
158800
158801 sqlite3Fts3GetVarint(p, &iVal);
158802 *pVal = iVal;
158803}
158804
158805/*
158806** The xDisconnect() virtual table method.
158807*/
158808static int fts3DisconnectMethod(sqlite3_vtab *pVtab){
158809 Fts3Table *p = (Fts3Table *)pVtab;
158810 int i;
158811
158812 assert( p->nPendingData==0 );
158813 assert( p->pSegments==0 );
158814
158815 /* Free any prepared statements held */
158816 sqlite3_finalize(p->pSeekStmt);
158817 for(i=0; i<SizeofArray(p->aStmt); i++){
158818 sqlite3_finalize(p->aStmt[i]);
158819 }
158820 sqlite3_free(p->zSegmentsTbl);
158821 sqlite3_free(p->zReadExprlist);
158822 sqlite3_free(p->zWriteExprlist);
158823 sqlite3_free(p->zContentTbl);
158824 sqlite3_free(p->zLanguageid);
158825
158826 /* Invoke the tokenizer destructor to free the tokenizer. */
158827 p->pTokenizer->pModule->xDestroy(p->pTokenizer);
158828
158829 sqlite3_free(p);
158830 return SQLITE_OK;
158831}
158832
158833/*
158834** Write an error message into *pzErr
158835*/
158836SQLITE_PRIVATE void sqlite3Fts3ErrMsg(char **pzErr, const char *zFormat, ...){
158837 va_list ap;
158838 sqlite3_free(*pzErr);
158839 va_start(ap, zFormat);
158840 *pzErr = sqlite3_vmprintf(zFormat, ap);
158841 va_end(ap);
158842}
158843
158844/*
158845** Construct one or more SQL statements from the format string given
158846** and then evaluate those statements. The success code is written
158847** into *pRc.
158848**
158849** If *pRc is initially non-zero then this routine is a no-op.
158850*/
158851static void fts3DbExec(
158852 int *pRc, /* Success code */
158853 sqlite3 *db, /* Database in which to run SQL */
158854 const char *zFormat, /* Format string for SQL */
158855 ... /* Arguments to the format string */
158856){
158857 va_list ap;
158858 char *zSql;
158859 if( *pRc ) return;
158860 va_start(ap, zFormat);
158861 zSql = sqlite3_vmprintf(zFormat, ap);
158862 va_end(ap);
158863 if( zSql==0 ){
158864 *pRc = SQLITE_NOMEM;
158865 }else{
158866 *pRc = sqlite3_exec(db, zSql, 0, 0, 0);
158867 sqlite3_free(zSql);
158868 }
158869}
158870
158871/*
158872** The xDestroy() virtual table method.
158873*/
158874static int fts3DestroyMethod(sqlite3_vtab *pVtab){
158875 Fts3Table *p = (Fts3Table *)pVtab;
158876 int rc = SQLITE_OK; /* Return code */
158877 const char *zDb = p->zDb; /* Name of database (e.g. "main", "temp") */
158878 sqlite3 *db = p->db; /* Database handle */
158879
158880 /* Drop the shadow tables */
158881 if( p->zContentTbl==0 ){
158882 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_content'", zDb, p->zName);
158883 }
158884 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segments'", zDb,p->zName);
158885 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segdir'", zDb, p->zName);
158886 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_docsize'", zDb, p->zName);
158887 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_stat'", zDb, p->zName);
158888
158889 /* If everything has worked, invoke fts3DisconnectMethod() to free the
158890 ** memory associated with the Fts3Table structure and return SQLITE_OK.
158891 ** Otherwise, return an SQLite error code.
158892 */
158893 return (rc==SQLITE_OK ? fts3DisconnectMethod(pVtab) : rc);
158894}
158895
158896
158897/*
158898** Invoke sqlite3_declare_vtab() to declare the schema for the FTS3 table
158899** passed as the first argument. This is done as part of the xConnect()
158900** and xCreate() methods.
158901**
158902** If *pRc is non-zero when this function is called, it is a no-op.
158903** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
158904** before returning.
158905*/
158906static void fts3DeclareVtab(int *pRc, Fts3Table *p){
158907 if( *pRc==SQLITE_OK ){
158908 int i; /* Iterator variable */
158909 int rc; /* Return code */
158910 char *zSql; /* SQL statement passed to declare_vtab() */
158911 char *zCols; /* List of user defined columns */
158912 const char *zLanguageid;
158913
158914 zLanguageid = (p->zLanguageid ? p->zLanguageid : "__langid");
158915 sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
158916
158917 /* Create a list of user columns for the virtual table */
158918 zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]);
158919 for(i=1; zCols && i<p->nColumn; i++){
158920 zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]);
158921 }
158922
158923 /* Create the whole "CREATE TABLE" statement to pass to SQLite */
158924 zSql = sqlite3_mprintf(
158925 "CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN, %Q HIDDEN)",
158926 zCols, p->zName, zLanguageid
158927 );
158928 if( !zCols || !zSql ){
158929 rc = SQLITE_NOMEM;
158930 }else{
158931 rc = sqlite3_declare_vtab(p->db, zSql);
158932 }
158933
158934 sqlite3_free(zSql);
158935 sqlite3_free(zCols);
158936 *pRc = rc;
158937 }
158938}
158939
158940/*
158941** Create the %_stat table if it does not already exist.
158942*/
158943SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int *pRc, Fts3Table *p){
158944 fts3DbExec(pRc, p->db,
158945 "CREATE TABLE IF NOT EXISTS %Q.'%q_stat'"
158946 "(id INTEGER PRIMARY KEY, value BLOB);",
158947 p->zDb, p->zName
158948 );
158949 if( (*pRc)==SQLITE_OK ) p->bHasStat = 1;
158950}
158951
158952/*
158953** Create the backing store tables (%_content, %_segments and %_segdir)
158954** required by the FTS3 table passed as the only argument. This is done
158955** as part of the vtab xCreate() method.
158956**
158957** If the p->bHasDocsize boolean is true (indicating that this is an
158958** FTS4 table, not an FTS3 table) then also create the %_docsize and
158959** %_stat tables required by FTS4.
158960*/
158961static int fts3CreateTables(Fts3Table *p){
158962 int rc = SQLITE_OK; /* Return code */
158963 int i; /* Iterator variable */
158964 sqlite3 *db = p->db; /* The database connection */
158965
158966 if( p->zContentTbl==0 ){
158967 const char *zLanguageid = p->zLanguageid;
158968 char *zContentCols; /* Columns of %_content table */
158969
158970 /* Create a list of user columns for the content table */
158971 zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY");
158972 for(i=0; zContentCols && i<p->nColumn; i++){
158973 char *z = p->azColumn[i];
158974 zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z);
158975 }
158976 if( zLanguageid && zContentCols ){
158977 zContentCols = sqlite3_mprintf("%z, langid", zContentCols, zLanguageid);
158978 }
158979 if( zContentCols==0 ) rc = SQLITE_NOMEM;
158980
158981 /* Create the content table */
158982 fts3DbExec(&rc, db,
158983 "CREATE TABLE %Q.'%q_content'(%s)",
158984 p->zDb, p->zName, zContentCols
158985 );
158986 sqlite3_free(zContentCols);
158987 }
158988
158989 /* Create other tables */
158990 fts3DbExec(&rc, db,
158991 "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);",
158992 p->zDb, p->zName
158993 );
158994 fts3DbExec(&rc, db,
158995 "CREATE TABLE %Q.'%q_segdir'("
158996 "level INTEGER,"
158997 "idx INTEGER,"
158998 "start_block INTEGER,"
158999 "leaves_end_block INTEGER,"
159000 "end_block INTEGER,"
159001 "root BLOB,"
159002 "PRIMARY KEY(level, idx)"
159003 ");",
159004 p->zDb, p->zName
159005 );
159006 if( p->bHasDocsize ){
159007 fts3DbExec(&rc, db,
159008 "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
159009 p->zDb, p->zName
159010 );
159011 }
159012 assert( p->bHasStat==p->bFts4 );
159013 if( p->bHasStat ){
159014 sqlite3Fts3CreateStatTable(&rc, p);
159015 }
159016 return rc;
159017}
159018
159019/*
159020** Store the current database page-size in bytes in p->nPgsz.
159021**
159022** If *pRc is non-zero when this function is called, it is a no-op.
159023** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
159024** before returning.
159025*/
159026static void fts3DatabasePageSize(int *pRc, Fts3Table *p){
159027 if( *pRc==SQLITE_OK ){
159028 int rc; /* Return code */
159029 char *zSql; /* SQL text "PRAGMA %Q.page_size" */
159030 sqlite3_stmt *pStmt; /* Compiled "PRAGMA %Q.page_size" statement */
159031
159032 zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb);
159033 if( !zSql ){
159034 rc = SQLITE_NOMEM;
159035 }else{
159036 rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
159037 if( rc==SQLITE_OK ){
159038 sqlite3_step(pStmt);
159039 p->nPgsz = sqlite3_column_int(pStmt, 0);
159040 rc = sqlite3_finalize(pStmt);
159041 }else if( rc==SQLITE_AUTH ){
159042 p->nPgsz = 1024;
159043 rc = SQLITE_OK;
159044 }
159045 }
159046 assert( p->nPgsz>0 || rc!=SQLITE_OK );
159047 sqlite3_free(zSql);
159048 *pRc = rc;
159049 }
159050}
159051
159052/*
159053** "Special" FTS4 arguments are column specifications of the following form:
159054**
159055** <key> = <value>
159056**
159057** There may not be whitespace surrounding the "=" character. The <value>
159058** term may be quoted, but the <key> may not.
159059*/
159060static int fts3IsSpecialColumn(
159061 const char *z,
159062 int *pnKey,
159063 char **pzValue
159064){
159065 char *zValue;
159066 const char *zCsr = z;
159067
159068 while( *zCsr!='=' ){
159069 if( *zCsr=='\0' ) return 0;
159070 zCsr++;
159071 }
159072
159073 *pnKey = (int)(zCsr-z);
159074 zValue = sqlite3_mprintf("%s", &zCsr[1]);
159075 if( zValue ){
159076 sqlite3Fts3Dequote(zValue);
159077 }
159078 *pzValue = zValue;
159079 return 1;
159080}
159081
159082/*
159083** Append the output of a printf() style formatting to an existing string.
159084*/
159085static void fts3Appendf(
159086 int *pRc, /* IN/OUT: Error code */
159087 char **pz, /* IN/OUT: Pointer to string buffer */
159088 const char *zFormat, /* Printf format string to append */
159089 ... /* Arguments for printf format string */
159090){
159091 if( *pRc==SQLITE_OK ){
159092 va_list ap;
159093 char *z;
159094 va_start(ap, zFormat);
159095 z = sqlite3_vmprintf(zFormat, ap);
159096 va_end(ap);
159097 if( z && *pz ){
159098 char *z2 = sqlite3_mprintf("%s%s", *pz, z);
159099 sqlite3_free(z);
159100 z = z2;
159101 }
159102 if( z==0 ) *pRc = SQLITE_NOMEM;
159103 sqlite3_free(*pz);
159104 *pz = z;
159105 }
159106}
159107
159108/*
159109** Return a copy of input string zInput enclosed in double-quotes (") and
159110** with all double quote characters escaped. For example:
159111**
159112** fts3QuoteId("un \"zip\"") -> "un \"\"zip\"\""
159113**
159114** The pointer returned points to memory obtained from sqlite3_malloc(). It
159115** is the callers responsibility to call sqlite3_free() to release this
159116** memory.
159117*/
159118static char *fts3QuoteId(char const *zInput){
159119 int nRet;
159120 char *zRet;
159121 nRet = 2 + (int)strlen(zInput)*2 + 1;
159122 zRet = sqlite3_malloc(nRet);
159123 if( zRet ){
159124 int i;
159125 char *z = zRet;
159126 *(z++) = '"';
159127 for(i=0; zInput[i]; i++){
159128 if( zInput[i]=='"' ) *(z++) = '"';
159129 *(z++) = zInput[i];
159130 }
159131 *(z++) = '"';
159132 *(z++) = '\0';
159133 }
159134 return zRet;
159135}
159136
159137/*
159138** Return a list of comma separated SQL expressions and a FROM clause that
159139** could be used in a SELECT statement such as the following:
159140**
159141** SELECT <list of expressions> FROM %_content AS x ...
159142**
159143** to return the docid, followed by each column of text data in order
159144** from left to write. If parameter zFunc is not NULL, then instead of
159145** being returned directly each column of text data is passed to an SQL
159146** function named zFunc first. For example, if zFunc is "unzip" and the
159147** table has the three user-defined columns "a", "b", and "c", the following
159148** string is returned:
159149**
159150** "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c') FROM %_content AS x"
159151**
159152** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
159153** is the responsibility of the caller to eventually free it.
159154**
159155** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
159156** a NULL pointer is returned). Otherwise, if an OOM error is encountered
159157** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
159158** no error occurs, *pRc is left unmodified.
159159*/
159160static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){
159161 char *zRet = 0;
159162 char *zFree = 0;
159163 char *zFunction;
159164 int i;
159165
159166 if( p->zContentTbl==0 ){
159167 if( !zFunc ){
159168 zFunction = "";
159169 }else{
159170 zFree = zFunction = fts3QuoteId(zFunc);
159171 }
159172 fts3Appendf(pRc, &zRet, "docid");
159173 for(i=0; i<p->nColumn; i++){
159174 fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
159175 }
159176 if( p->zLanguageid ){
159177 fts3Appendf(pRc, &zRet, ", x.%Q", "langid");
159178 }
159179 sqlite3_free(zFree);
159180 }else{
159181 fts3Appendf(pRc, &zRet, "rowid");
159182 for(i=0; i<p->nColumn; i++){
159183 fts3Appendf(pRc, &zRet, ", x.'%q'", p->azColumn[i]);
159184 }
159185 if( p->zLanguageid ){
159186 fts3Appendf(pRc, &zRet, ", x.%Q", p->zLanguageid);
159187 }
159188 }
159189 fts3Appendf(pRc, &zRet, " FROM '%q'.'%q%s' AS x",
159190 p->zDb,
159191 (p->zContentTbl ? p->zContentTbl : p->zName),
159192 (p->zContentTbl ? "" : "_content")
159193 );
159194 return zRet;
159195}
159196
159197/*
159198** Return a list of N comma separated question marks, where N is the number
159199** of columns in the %_content table (one for the docid plus one for each
159200** user-defined text column).
159201**
159202** If argument zFunc is not NULL, then all but the first question mark
159203** is preceded by zFunc and an open bracket, and followed by a closed
159204** bracket. For example, if zFunc is "zip" and the FTS3 table has three
159205** user-defined text columns, the following string is returned:
159206**
159207** "?, zip(?), zip(?), zip(?)"
159208**
159209** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
159210** is the responsibility of the caller to eventually free it.
159211**
159212** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
159213** a NULL pointer is returned). Otherwise, if an OOM error is encountered
159214** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
159215** no error occurs, *pRc is left unmodified.
159216*/
159217static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){
159218 char *zRet = 0;
159219 char *zFree = 0;
159220 char *zFunction;
159221 int i;
159222
159223 if( !zFunc ){
159224 zFunction = "";
159225 }else{
159226 zFree = zFunction = fts3QuoteId(zFunc);
159227 }
159228 fts3Appendf(pRc, &zRet, "?");
159229 for(i=0; i<p->nColumn; i++){
159230 fts3Appendf(pRc, &zRet, ",%s(?)", zFunction);
159231 }
159232 if( p->zLanguageid ){
159233 fts3Appendf(pRc, &zRet, ", ?");
159234 }
159235 sqlite3_free(zFree);
159236 return zRet;
159237}
159238
159239/*
159240** This function interprets the string at (*pp) as a non-negative integer
159241** value. It reads the integer and sets *pnOut to the value read, then
159242** sets *pp to point to the byte immediately following the last byte of
159243** the integer value.
159244**
159245** Only decimal digits ('0'..'9') may be part of an integer value.
159246**
159247** If *pp does not being with a decimal digit SQLITE_ERROR is returned and
159248** the output value undefined. Otherwise SQLITE_OK is returned.
159249**
159250** This function is used when parsing the "prefix=" FTS4 parameter.
159251*/
159252static int fts3GobbleInt(const char **pp, int *pnOut){
159253 const int MAX_NPREFIX = 10000000;
159254 const char *p; /* Iterator pointer */
159255 int nInt = 0; /* Output value */
159256
159257 for(p=*pp; p[0]>='0' && p[0]<='9'; p++){
159258 nInt = nInt * 10 + (p[0] - '0');
159259 if( nInt>MAX_NPREFIX ){
159260 nInt = 0;
159261 break;
159262 }
159263 }
159264 if( p==*pp ) return SQLITE_ERROR;
159265 *pnOut = nInt;
159266 *pp = p;
159267 return SQLITE_OK;
159268}
159269
159270/*
159271** This function is called to allocate an array of Fts3Index structures
159272** representing the indexes maintained by the current FTS table. FTS tables
159273** always maintain the main "terms" index, but may also maintain one or
159274** more "prefix" indexes, depending on the value of the "prefix=" parameter
159275** (if any) specified as part of the CREATE VIRTUAL TABLE statement.
159276**
159277** Argument zParam is passed the value of the "prefix=" option if one was
159278** specified, or NULL otherwise.
159279**
159280** If no error occurs, SQLITE_OK is returned and *apIndex set to point to
159281** the allocated array. *pnIndex is set to the number of elements in the
159282** array. If an error does occur, an SQLite error code is returned.
159283**
159284** Regardless of whether or not an error is returned, it is the responsibility
159285** of the caller to call sqlite3_free() on the output array to free it.
159286*/
159287static int fts3PrefixParameter(
159288 const char *zParam, /* ABC in prefix=ABC parameter to parse */
159289 int *pnIndex, /* OUT: size of *apIndex[] array */
159290 struct Fts3Index **apIndex /* OUT: Array of indexes for this table */
159291){
159292 struct Fts3Index *aIndex; /* Allocated array */
159293 int nIndex = 1; /* Number of entries in array */
159294
159295 if( zParam && zParam[0] ){
159296 const char *p;
159297 nIndex++;
159298 for(p=zParam; *p; p++){
159299 if( *p==',' ) nIndex++;
159300 }
159301 }
159302
159303 aIndex = sqlite3_malloc(sizeof(struct Fts3Index) * nIndex);
159304 *apIndex = aIndex;
159305 if( !aIndex ){
159306 return SQLITE_NOMEM;
159307 }
159308
159309 memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex);
159310 if( zParam ){
159311 const char *p = zParam;
159312 int i;
159313 for(i=1; i<nIndex; i++){
159314 int nPrefix = 0;
159315 if( fts3GobbleInt(&p, &nPrefix) ) return SQLITE_ERROR;
159316 assert( nPrefix>=0 );
159317 if( nPrefix==0 ){
159318 nIndex--;
159319 i--;
159320 }else{
159321 aIndex[i].nPrefix = nPrefix;
159322 }
159323 p++;
159324 }
159325 }
159326
159327 *pnIndex = nIndex;
159328 return SQLITE_OK;
159329}
159330
159331/*
159332** This function is called when initializing an FTS4 table that uses the
159333** content=xxx option. It determines the number of and names of the columns
159334** of the new FTS4 table.
159335**
159336** The third argument passed to this function is the value passed to the
159337** config=xxx option (i.e. "xxx"). This function queries the database for
159338** a table of that name. If found, the output variables are populated
159339** as follows:
159340**
159341** *pnCol: Set to the number of columns table xxx has,
159342**
159343** *pnStr: Set to the total amount of space required to store a copy
159344** of each columns name, including the nul-terminator.
159345**
159346** *pazCol: Set to point to an array of *pnCol strings. Each string is
159347** the name of the corresponding column in table xxx. The array
159348** and its contents are allocated using a single allocation. It
159349** is the responsibility of the caller to free this allocation
159350** by eventually passing the *pazCol value to sqlite3_free().
159351**
159352** If the table cannot be found, an error code is returned and the output
159353** variables are undefined. Or, if an OOM is encountered, SQLITE_NOMEM is
159354** returned (and the output variables are undefined).
159355*/
159356static int fts3ContentColumns(
159357 sqlite3 *db, /* Database handle */
159358 const char *zDb, /* Name of db (i.e. "main", "temp" etc.) */
159359 const char *zTbl, /* Name of content table */
159360 const char ***pazCol, /* OUT: Malloc'd array of column names */
159361 int *pnCol, /* OUT: Size of array *pazCol */
159362 int *pnStr, /* OUT: Bytes of string content */
159363 char **pzErr /* OUT: error message */
159364){
159365 int rc = SQLITE_OK; /* Return code */
159366 char *zSql; /* "SELECT *" statement on zTbl */
159367 sqlite3_stmt *pStmt = 0; /* Compiled version of zSql */
159368
159369 zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zTbl);
159370 if( !zSql ){
159371 rc = SQLITE_NOMEM;
159372 }else{
159373 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
159374 if( rc!=SQLITE_OK ){
159375 sqlite3Fts3ErrMsg(pzErr, "%s", sqlite3_errmsg(db));
159376 }
159377 }
159378 sqlite3_free(zSql);
159379
159380 if( rc==SQLITE_OK ){
159381 const char **azCol; /* Output array */
159382 int nStr = 0; /* Size of all column names (incl. 0x00) */
159383 int nCol; /* Number of table columns */
159384 int i; /* Used to iterate through columns */
159385
159386 /* Loop through the returned columns. Set nStr to the number of bytes of
159387 ** space required to store a copy of each column name, including the
159388 ** nul-terminator byte. */
159389 nCol = sqlite3_column_count(pStmt);
159390 for(i=0; i<nCol; i++){
159391 const char *zCol = sqlite3_column_name(pStmt, i);
159392 nStr += (int)strlen(zCol) + 1;
159393 }
159394
159395 /* Allocate and populate the array to return. */
159396 azCol = (const char **)sqlite3_malloc(sizeof(char *) * nCol + nStr);
159397 if( azCol==0 ){
159398 rc = SQLITE_NOMEM;
159399 }else{
159400 char *p = (char *)&azCol[nCol];
159401 for(i=0; i<nCol; i++){
159402 const char *zCol = sqlite3_column_name(pStmt, i);
159403 int n = (int)strlen(zCol)+1;
159404 memcpy(p, zCol, n);
159405 azCol[i] = p;
159406 p += n;
159407 }
159408 }
159409 sqlite3_finalize(pStmt);
159410
159411 /* Set the output variables. */
159412 *pnCol = nCol;
159413 *pnStr = nStr;
159414 *pazCol = azCol;
159415 }
159416
159417 return rc;
159418}
159419
159420/*
159421** This function is the implementation of both the xConnect and xCreate
159422** methods of the FTS3 virtual table.
159423**
159424** The argv[] array contains the following:
159425**
159426** argv[0] -> module name ("fts3" or "fts4")
159427** argv[1] -> database name
159428** argv[2] -> table name
159429** argv[...] -> "column name" and other module argument fields.
159430*/
159431static int fts3InitVtab(
159432 int isCreate, /* True for xCreate, false for xConnect */
159433 sqlite3 *db, /* The SQLite database connection */
159434 void *pAux, /* Hash table containing tokenizers */
159435 int argc, /* Number of elements in argv array */
159436 const char * const *argv, /* xCreate/xConnect argument array */
159437 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
159438 char **pzErr /* Write any error message here */
159439){
159440 Fts3Hash *pHash = (Fts3Hash *)pAux;
159441 Fts3Table *p = 0; /* Pointer to allocated vtab */
159442 int rc = SQLITE_OK; /* Return code */
159443 int i; /* Iterator variable */
159444 int nByte; /* Size of allocation used for *p */
159445 int iCol; /* Column index */
159446 int nString = 0; /* Bytes required to hold all column names */
159447 int nCol = 0; /* Number of columns in the FTS table */
159448 char *zCsr; /* Space for holding column names */
159449 int nDb; /* Bytes required to hold database name */
159450 int nName; /* Bytes required to hold table name */
159451 int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
159452 const char **aCol; /* Array of column names */
159453 sqlite3_tokenizer *pTokenizer = 0; /* Tokenizer for this table */
159454
159455 int nIndex = 0; /* Size of aIndex[] array */
159456 struct Fts3Index *aIndex = 0; /* Array of indexes for this table */
159457
159458 /* The results of parsing supported FTS4 key=value options: */
159459 int bNoDocsize = 0; /* True to omit %_docsize table */
159460 int bDescIdx = 0; /* True to store descending indexes */
159461 char *zPrefix = 0; /* Prefix parameter value (or NULL) */
159462 char *zCompress = 0; /* compress=? parameter (or NULL) */
159463 char *zUncompress = 0; /* uncompress=? parameter (or NULL) */
159464 char *zContent = 0; /* content=? parameter (or NULL) */
159465 char *zLanguageid = 0; /* languageid=? parameter (or NULL) */
159466 char **azNotindexed = 0; /* The set of notindexed= columns */
159467 int nNotindexed = 0; /* Size of azNotindexed[] array */
159468
159469 assert( strlen(argv[0])==4 );
159470 assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)
159471 || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)
159472 );
159473
159474 nDb = (int)strlen(argv[1]) + 1;
159475 nName = (int)strlen(argv[2]) + 1;
159476
159477 nByte = sizeof(const char *) * (argc-2);
159478 aCol = (const char **)sqlite3_malloc(nByte);
159479 if( aCol ){
159480 memset((void*)aCol, 0, nByte);
159481 azNotindexed = (char **)sqlite3_malloc(nByte);
159482 }
159483 if( azNotindexed ){
159484 memset(azNotindexed, 0, nByte);
159485 }
159486 if( !aCol || !azNotindexed ){
159487 rc = SQLITE_NOMEM;
159488 goto fts3_init_out;
159489 }
159490
159491 /* Loop through all of the arguments passed by the user to the FTS3/4
159492 ** module (i.e. all the column names and special arguments). This loop
159493 ** does the following:
159494 **
159495 ** + Figures out the number of columns the FTSX table will have, and
159496 ** the number of bytes of space that must be allocated to store copies
159497 ** of the column names.
159498 **
159499 ** + If there is a tokenizer specification included in the arguments,
159500 ** initializes the tokenizer pTokenizer.
159501 */
159502 for(i=3; rc==SQLITE_OK && i<argc; i++){
159503 char const *z = argv[i];
159504 int nKey;
159505 char *zVal;
159506
159507 /* Check if this is a tokenizer specification */
159508 if( !pTokenizer
159509 && strlen(z)>8
159510 && 0==sqlite3_strnicmp(z, "tokenize", 8)
159511 && 0==sqlite3Fts3IsIdChar(z[8])
159512 ){
159513 rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr);
159514 }
159515
159516 /* Check if it is an FTS4 special argument. */
159517 else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){
159518 struct Fts4Option {
159519 const char *zOpt;
159520 int nOpt;
159521 } aFts4Opt[] = {
159522 { "matchinfo", 9 }, /* 0 -> MATCHINFO */
159523 { "prefix", 6 }, /* 1 -> PREFIX */
159524 { "compress", 8 }, /* 2 -> COMPRESS */
159525 { "uncompress", 10 }, /* 3 -> UNCOMPRESS */
159526 { "order", 5 }, /* 4 -> ORDER */
159527 { "content", 7 }, /* 5 -> CONTENT */
159528 { "languageid", 10 }, /* 6 -> LANGUAGEID */
159529 { "notindexed", 10 } /* 7 -> NOTINDEXED */
159530 };
159531
159532 int iOpt;
159533 if( !zVal ){
159534 rc = SQLITE_NOMEM;
159535 }else{
159536 for(iOpt=0; iOpt<SizeofArray(aFts4Opt); iOpt++){
159537 struct Fts4Option *pOp = &aFts4Opt[iOpt];
159538 if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){
159539 break;
159540 }
159541 }
159542 switch( iOpt ){
159543 case 0: /* MATCHINFO */
159544 if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){
159545 sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo: %s", zVal);
159546 rc = SQLITE_ERROR;
159547 }
159548 bNoDocsize = 1;
159549 break;
159550
159551 case 1: /* PREFIX */
159552 sqlite3_free(zPrefix);
159553 zPrefix = zVal;
159554 zVal = 0;
159555 break;
159556
159557 case 2: /* COMPRESS */
159558 sqlite3_free(zCompress);
159559 zCompress = zVal;
159560 zVal = 0;
159561 break;
159562
159563 case 3: /* UNCOMPRESS */
159564 sqlite3_free(zUncompress);
159565 zUncompress = zVal;
159566 zVal = 0;
159567 break;
159568
159569 case 4: /* ORDER */
159570 if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3))
159571 && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4))
159572 ){
159573 sqlite3Fts3ErrMsg(pzErr, "unrecognized order: %s", zVal);
159574 rc = SQLITE_ERROR;
159575 }
159576 bDescIdx = (zVal[0]=='d' || zVal[0]=='D');
159577 break;
159578
159579 case 5: /* CONTENT */
159580 sqlite3_free(zContent);
159581 zContent = zVal;
159582 zVal = 0;
159583 break;
159584
159585 case 6: /* LANGUAGEID */
159586 assert( iOpt==6 );
159587 sqlite3_free(zLanguageid);
159588 zLanguageid = zVal;
159589 zVal = 0;
159590 break;
159591
159592 case 7: /* NOTINDEXED */
159593 azNotindexed[nNotindexed++] = zVal;
159594 zVal = 0;
159595 break;
159596
159597 default:
159598 assert( iOpt==SizeofArray(aFts4Opt) );
159599 sqlite3Fts3ErrMsg(pzErr, "unrecognized parameter: %s", z);
159600 rc = SQLITE_ERROR;
159601 break;
159602 }
159603 sqlite3_free(zVal);
159604 }
159605 }
159606
159607 /* Otherwise, the argument is a column name. */
159608 else {
159609 nString += (int)(strlen(z) + 1);
159610 aCol[nCol++] = z;
159611 }
159612 }
159613
159614 /* If a content=xxx option was specified, the following:
159615 **
159616 ** 1. Ignore any compress= and uncompress= options.
159617 **
159618 ** 2. If no column names were specified as part of the CREATE VIRTUAL
159619 ** TABLE statement, use all columns from the content table.
159620 */
159621 if( rc==SQLITE_OK && zContent ){
159622 sqlite3_free(zCompress);
159623 sqlite3_free(zUncompress);
159624 zCompress = 0;
159625 zUncompress = 0;
159626 if( nCol==0 ){
159627 sqlite3_free((void*)aCol);
159628 aCol = 0;
159629 rc = fts3ContentColumns(db, argv[1], zContent,&aCol,&nCol,&nString,pzErr);
159630
159631 /* If a languageid= option was specified, remove the language id
159632 ** column from the aCol[] array. */
159633 if( rc==SQLITE_OK && zLanguageid ){
159634 int j;
159635 for(j=0; j<nCol; j++){
159636 if( sqlite3_stricmp(zLanguageid, aCol[j])==0 ){
159637 int k;
159638 for(k=j; k<nCol; k++) aCol[k] = aCol[k+1];
159639 nCol--;
159640 break;
159641 }
159642 }
159643 }
159644 }
159645 }
159646 if( rc!=SQLITE_OK ) goto fts3_init_out;
159647
159648 if( nCol==0 ){
159649 assert( nString==0 );
159650 aCol[0] = "content";
159651 nString = 8;
159652 nCol = 1;
159653 }
159654
159655 if( pTokenizer==0 ){
159656 rc = sqlite3Fts3InitTokenizer(pHash, "simple", &pTokenizer, pzErr);
159657 if( rc!=SQLITE_OK ) goto fts3_init_out;
159658 }
159659 assert( pTokenizer );
159660
159661 rc = fts3PrefixParameter(zPrefix, &nIndex, &aIndex);
159662 if( rc==SQLITE_ERROR ){
159663 assert( zPrefix );
159664 sqlite3Fts3ErrMsg(pzErr, "error parsing prefix parameter: %s", zPrefix);
159665 }
159666 if( rc!=SQLITE_OK ) goto fts3_init_out;
159667
159668 /* Allocate and populate the Fts3Table structure. */
159669 nByte = sizeof(Fts3Table) + /* Fts3Table */
159670 nCol * sizeof(char *) + /* azColumn */
159671 nIndex * sizeof(struct Fts3Index) + /* aIndex */
159672 nCol * sizeof(u8) + /* abNotindexed */
159673 nName + /* zName */
159674 nDb + /* zDb */
159675 nString; /* Space for azColumn strings */
159676 p = (Fts3Table*)sqlite3_malloc(nByte);
159677 if( p==0 ){
159678 rc = SQLITE_NOMEM;
159679 goto fts3_init_out;
159680 }
159681 memset(p, 0, nByte);
159682 p->db = db;
159683 p->nColumn = nCol;
159684 p->nPendingData = 0;
159685 p->azColumn = (char **)&p[1];
159686 p->pTokenizer = pTokenizer;
159687 p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
159688 p->bHasDocsize = (isFts4 && bNoDocsize==0);
159689 p->bHasStat = (u8)isFts4;
159690 p->bFts4 = (u8)isFts4;
159691 p->bDescIdx = (u8)bDescIdx;
159692 p->nAutoincrmerge = 0xff; /* 0xff means setting unknown */
159693 p->zContentTbl = zContent;
159694 p->zLanguageid = zLanguageid;
159695 zContent = 0;
159696 zLanguageid = 0;
159697 TESTONLY( p->inTransaction = -1 );
159698 TESTONLY( p->mxSavepoint = -1 );
159699
159700 p->aIndex = (struct Fts3Index *)&p->azColumn[nCol];
159701 memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex);
159702 p->nIndex = nIndex;
159703 for(i=0; i<nIndex; i++){
159704 fts3HashInit(&p->aIndex[i].hPending, FTS3_HASH_STRING, 1);
159705 }
159706 p->abNotindexed = (u8 *)&p->aIndex[nIndex];
159707
159708 /* Fill in the zName and zDb fields of the vtab structure. */
159709 zCsr = (char *)&p->abNotindexed[nCol];
159710 p->zName = zCsr;
159711 memcpy(zCsr, argv[2], nName);
159712 zCsr += nName;
159713 p->zDb = zCsr;
159714 memcpy(zCsr, argv[1], nDb);
159715 zCsr += nDb;
159716
159717 /* Fill in the azColumn array */
159718 for(iCol=0; iCol<nCol; iCol++){
159719 char *z;
159720 int n = 0;
159721 z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
159722 if( n>0 ){
159723 memcpy(zCsr, z, n);
159724 }
159725 zCsr[n] = '\0';
159726 sqlite3Fts3Dequote(zCsr);
159727 p->azColumn[iCol] = zCsr;
159728 zCsr += n+1;
159729 assert( zCsr <= &((char *)p)[nByte] );
159730 }
159731
159732 /* Fill in the abNotindexed array */
159733 for(iCol=0; iCol<nCol; iCol++){
159734 int n = (int)strlen(p->azColumn[iCol]);
159735 for(i=0; i<nNotindexed; i++){
159736 char *zNot = azNotindexed[i];
159737 if( zNot && n==(int)strlen(zNot)
159738 && 0==sqlite3_strnicmp(p->azColumn[iCol], zNot, n)
159739 ){
159740 p->abNotindexed[iCol] = 1;
159741 sqlite3_free(zNot);
159742 azNotindexed[i] = 0;
159743 }
159744 }
159745 }
159746 for(i=0; i<nNotindexed; i++){
159747 if( azNotindexed[i] ){
159748 sqlite3Fts3ErrMsg(pzErr, "no such column: %s", azNotindexed[i]);
159749 rc = SQLITE_ERROR;
159750 }
159751 }
159752
159753 if( rc==SQLITE_OK && (zCompress==0)!=(zUncompress==0) ){
159754 char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
159755 rc = SQLITE_ERROR;
159756 sqlite3Fts3ErrMsg(pzErr, "missing %s parameter in fts4 constructor", zMiss);
159757 }
159758 p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
159759 p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
159760 if( rc!=SQLITE_OK ) goto fts3_init_out;
159761
159762 /* If this is an xCreate call, create the underlying tables in the
159763 ** database. TODO: For xConnect(), it could verify that said tables exist.
159764 */
159765 if( isCreate ){
159766 rc = fts3CreateTables(p);
159767 }
159768
159769 /* Check to see if a legacy fts3 table has been "upgraded" by the
159770 ** addition of a %_stat table so that it can use incremental merge.
159771 */
159772 if( !isFts4 && !isCreate ){
159773 p->bHasStat = 2;
159774 }
159775
159776 /* Figure out the page-size for the database. This is required in order to
159777 ** estimate the cost of loading large doclists from the database. */
159778 fts3DatabasePageSize(&rc, p);
159779 p->nNodeSize = p->nPgsz-35;
159780
159781 /* Declare the table schema to SQLite. */
159782 fts3DeclareVtab(&rc, p);
159783
159784fts3_init_out:
159785 sqlite3_free(zPrefix);
159786 sqlite3_free(aIndex);
159787 sqlite3_free(zCompress);
159788 sqlite3_free(zUncompress);
159789 sqlite3_free(zContent);
159790 sqlite3_free(zLanguageid);
159791 for(i=0; i<nNotindexed; i++) sqlite3_free(azNotindexed[i]);
159792 sqlite3_free((void *)aCol);
159793 sqlite3_free((void *)azNotindexed);
159794 if( rc!=SQLITE_OK ){
159795 if( p ){
159796 fts3DisconnectMethod((sqlite3_vtab *)p);
159797 }else if( pTokenizer ){
159798 pTokenizer->pModule->xDestroy(pTokenizer);
159799 }
159800 }else{
159801 assert( p->pSegments==0 );
159802 *ppVTab = &p->base;
159803 }
159804 return rc;
159805}
159806
159807/*
159808** The xConnect() and xCreate() methods for the virtual table. All the
159809** work is done in function fts3InitVtab().
159810*/
159811static int fts3ConnectMethod(
159812 sqlite3 *db, /* Database connection */
159813 void *pAux, /* Pointer to tokenizer hash table */
159814 int argc, /* Number of elements in argv array */
159815 const char * const *argv, /* xCreate/xConnect argument array */
159816 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
159817 char **pzErr /* OUT: sqlite3_malloc'd error message */
159818){
159819 return fts3InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
159820}
159821static int fts3CreateMethod(
159822 sqlite3 *db, /* Database connection */
159823 void *pAux, /* Pointer to tokenizer hash table */
159824 int argc, /* Number of elements in argv array */
159825 const char * const *argv, /* xCreate/xConnect argument array */
159826 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
159827 char **pzErr /* OUT: sqlite3_malloc'd error message */
159828){
159829 return fts3InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
159830}
159831
159832/*
159833** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
159834** extension is currently being used by a version of SQLite too old to
159835** support estimatedRows. In that case this function is a no-op.
159836*/
159837static void fts3SetEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
159838#if SQLITE_VERSION_NUMBER>=3008002
159839 if( sqlite3_libversion_number()>=3008002 ){
159840 pIdxInfo->estimatedRows = nRow;
159841 }
159842#endif
159843}
159844
159845/*
159846** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
159847** extension is currently being used by a version of SQLite too old to
159848** support index-info flags. In that case this function is a no-op.
159849*/
159850static void fts3SetUniqueFlag(sqlite3_index_info *pIdxInfo){
159851#if SQLITE_VERSION_NUMBER>=3008012
159852 if( sqlite3_libversion_number()>=3008012 ){
159853 pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE;
159854 }
159855#endif
159856}
159857
159858/*
159859** Implementation of the xBestIndex method for FTS3 tables. There
159860** are three possible strategies, in order of preference:
159861**
159862** 1. Direct lookup by rowid or docid.
159863** 2. Full-text search using a MATCH operator on a non-docid column.
159864** 3. Linear scan of %_content table.
159865*/
159866static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
159867 Fts3Table *p = (Fts3Table *)pVTab;
159868 int i; /* Iterator variable */
159869 int iCons = -1; /* Index of constraint to use */
159870
159871 int iLangidCons = -1; /* Index of langid=x constraint, if present */
159872 int iDocidGe = -1; /* Index of docid>=x constraint, if present */
159873 int iDocidLe = -1; /* Index of docid<=x constraint, if present */
159874 int iIdx;
159875
159876 /* By default use a full table scan. This is an expensive option,
159877 ** so search through the constraints to see if a more efficient
159878 ** strategy is possible.
159879 */
159880 pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
159881 pInfo->estimatedCost = 5000000;
159882 for(i=0; i<pInfo->nConstraint; i++){
159883 int bDocid; /* True if this constraint is on docid */
159884 struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
159885 if( pCons->usable==0 ){
159886 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH ){
159887 /* There exists an unusable MATCH constraint. This means that if
159888 ** the planner does elect to use the results of this call as part
159889 ** of the overall query plan the user will see an "unable to use
159890 ** function MATCH in the requested context" error. To discourage
159891 ** this, return a very high cost here. */
159892 pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
159893 pInfo->estimatedCost = 1e50;
159894 fts3SetEstimatedRows(pInfo, ((sqlite3_int64)1) << 50);
159895 return SQLITE_OK;
159896 }
159897 continue;
159898 }
159899
159900 bDocid = (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1);
159901
159902 /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */
159903 if( iCons<0 && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ && bDocid ){
159904 pInfo->idxNum = FTS3_DOCID_SEARCH;
159905 pInfo->estimatedCost = 1.0;
159906 iCons = i;
159907 }
159908
159909 /* A MATCH constraint. Use a full-text search.
159910 **
159911 ** If there is more than one MATCH constraint available, use the first
159912 ** one encountered. If there is both a MATCH constraint and a direct
159913 ** rowid/docid lookup, prefer the MATCH strategy. This is done even
159914 ** though the rowid/docid lookup is faster than a MATCH query, selecting
159915 ** it would lead to an "unable to use function MATCH in the requested
159916 ** context" error.
159917 */
159918 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH
159919 && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
159920 ){
159921 pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn;
159922 pInfo->estimatedCost = 2.0;
159923 iCons = i;
159924 }
159925
159926 /* Equality constraint on the langid column */
159927 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ
159928 && pCons->iColumn==p->nColumn + 2
159929 ){
159930 iLangidCons = i;
159931 }
159932
159933 if( bDocid ){
159934 switch( pCons->op ){
159935 case SQLITE_INDEX_CONSTRAINT_GE:
159936 case SQLITE_INDEX_CONSTRAINT_GT:
159937 iDocidGe = i;
159938 break;
159939
159940 case SQLITE_INDEX_CONSTRAINT_LE:
159941 case SQLITE_INDEX_CONSTRAINT_LT:
159942 iDocidLe = i;
159943 break;
159944 }
159945 }
159946 }
159947
159948 /* If using a docid=? or rowid=? strategy, set the UNIQUE flag. */
159949 if( pInfo->idxNum==FTS3_DOCID_SEARCH ) fts3SetUniqueFlag(pInfo);
159950
159951 iIdx = 1;
159952 if( iCons>=0 ){
159953 pInfo->aConstraintUsage[iCons].argvIndex = iIdx++;
159954 pInfo->aConstraintUsage[iCons].omit = 1;
159955 }
159956 if( iLangidCons>=0 ){
159957 pInfo->idxNum |= FTS3_HAVE_LANGID;
159958 pInfo->aConstraintUsage[iLangidCons].argvIndex = iIdx++;
159959 }
159960 if( iDocidGe>=0 ){
159961 pInfo->idxNum |= FTS3_HAVE_DOCID_GE;
159962 pInfo->aConstraintUsage[iDocidGe].argvIndex = iIdx++;
159963 }
159964 if( iDocidLe>=0 ){
159965 pInfo->idxNum |= FTS3_HAVE_DOCID_LE;
159966 pInfo->aConstraintUsage[iDocidLe].argvIndex = iIdx++;
159967 }
159968
159969 /* Regardless of the strategy selected, FTS can deliver rows in rowid (or
159970 ** docid) order. Both ascending and descending are possible.
159971 */
159972 if( pInfo->nOrderBy==1 ){
159973 struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0];
159974 if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){
159975 if( pOrder->desc ){
159976 pInfo->idxStr = "DESC";
159977 }else{
159978 pInfo->idxStr = "ASC";
159979 }
159980 pInfo->orderByConsumed = 1;
159981 }
159982 }
159983
159984 assert( p->pSegments==0 );
159985 return SQLITE_OK;
159986}
159987
159988/*
159989** Implementation of xOpen method.
159990*/
159991static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
159992 sqlite3_vtab_cursor *pCsr; /* Allocated cursor */
159993
159994 UNUSED_PARAMETER(pVTab);
159995
159996 /* Allocate a buffer large enough for an Fts3Cursor structure. If the
159997 ** allocation succeeds, zero it and return SQLITE_OK. Otherwise,
159998 ** if the allocation fails, return SQLITE_NOMEM.
159999 */
160000 *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor));
160001 if( !pCsr ){
160002 return SQLITE_NOMEM;
160003 }
160004 memset(pCsr, 0, sizeof(Fts3Cursor));
160005 return SQLITE_OK;
160006}
160007
160008/*
160009** Finalize the statement handle at pCsr->pStmt.
160010**
160011** Or, if that statement handle is one created by fts3CursorSeekStmt(),
160012** and the Fts3Table.pSeekStmt slot is currently NULL, save the statement
160013** pointer there instead of finalizing it.
160014*/
160015static void fts3CursorFinalizeStmt(Fts3Cursor *pCsr){
160016 if( pCsr->bSeekStmt ){
160017 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
160018 if( p->pSeekStmt==0 ){
160019 p->pSeekStmt = pCsr->pStmt;
160020 sqlite3_reset(pCsr->pStmt);
160021 pCsr->pStmt = 0;
160022 }
160023 pCsr->bSeekStmt = 0;
160024 }
160025 sqlite3_finalize(pCsr->pStmt);
160026}
160027
160028/*
160029** Free all resources currently held by the cursor passed as the only
160030** argument.
160031*/
160032static void fts3ClearCursor(Fts3Cursor *pCsr){
160033 fts3CursorFinalizeStmt(pCsr);
160034 sqlite3Fts3FreeDeferredTokens(pCsr);
160035 sqlite3_free(pCsr->aDoclist);
160036 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
160037 sqlite3Fts3ExprFree(pCsr->pExpr);
160038 memset(&(&pCsr->base)[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
160039}
160040
160041/*
160042** Close the cursor. For additional information see the documentation
160043** on the xClose method of the virtual table interface.
160044*/
160045static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
160046 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
160047 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
160048 fts3ClearCursor(pCsr);
160049 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
160050 sqlite3_free(pCsr);
160051 return SQLITE_OK;
160052}
160053
160054/*
160055** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
160056** compose and prepare an SQL statement of the form:
160057**
160058** "SELECT <columns> FROM %_content WHERE rowid = ?"
160059**
160060** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
160061** it. If an error occurs, return an SQLite error code.
160062*/
160063static int fts3CursorSeekStmt(Fts3Cursor *pCsr){
160064 int rc = SQLITE_OK;
160065 if( pCsr->pStmt==0 ){
160066 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
160067 char *zSql;
160068 if( p->pSeekStmt ){
160069 pCsr->pStmt = p->pSeekStmt;
160070 p->pSeekStmt = 0;
160071 }else{
160072 zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
160073 if( !zSql ) return SQLITE_NOMEM;
160074 rc = sqlite3_prepare_v3(p->db, zSql,-1,SQLITE_PREPARE_PERSISTENT,&pCsr->pStmt,0);
160075 sqlite3_free(zSql);
160076 }
160077 if( rc==SQLITE_OK ) pCsr->bSeekStmt = 1;
160078 }
160079 return rc;
160080}
160081
160082/*
160083** Position the pCsr->pStmt statement so that it is on the row
160084** of the %_content table that contains the last match. Return
160085** SQLITE_OK on success.
160086*/
160087static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
160088 int rc = SQLITE_OK;
160089 if( pCsr->isRequireSeek ){
160090 rc = fts3CursorSeekStmt(pCsr);
160091 if( rc==SQLITE_OK ){
160092 sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
160093 pCsr->isRequireSeek = 0;
160094 if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
160095 return SQLITE_OK;
160096 }else{
160097 rc = sqlite3_reset(pCsr->pStmt);
160098 if( rc==SQLITE_OK && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){
160099 /* If no row was found and no error has occurred, then the %_content
160100 ** table is missing a row that is present in the full-text index.
160101 ** The data structures are corrupt. */
160102 rc = FTS_CORRUPT_VTAB;
160103 pCsr->isEof = 1;
160104 }
160105 }
160106 }
160107 }
160108
160109 if( rc!=SQLITE_OK && pContext ){
160110 sqlite3_result_error_code(pContext, rc);
160111 }
160112 return rc;
160113}
160114
160115/*
160116** This function is used to process a single interior node when searching
160117** a b-tree for a term or term prefix. The node data is passed to this
160118** function via the zNode/nNode parameters. The term to search for is
160119** passed in zTerm/nTerm.
160120**
160121** If piFirst is not NULL, then this function sets *piFirst to the blockid
160122** of the child node that heads the sub-tree that may contain the term.
160123**
160124** If piLast is not NULL, then *piLast is set to the right-most child node
160125** that heads a sub-tree that may contain a term for which zTerm/nTerm is
160126** a prefix.
160127**
160128** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
160129*/
160130static int fts3ScanInteriorNode(
160131 const char *zTerm, /* Term to select leaves for */
160132 int nTerm, /* Size of term zTerm in bytes */
160133 const char *zNode, /* Buffer containing segment interior node */
160134 int nNode, /* Size of buffer at zNode */
160135 sqlite3_int64 *piFirst, /* OUT: Selected child node */
160136 sqlite3_int64 *piLast /* OUT: Selected child node */
160137){
160138 int rc = SQLITE_OK; /* Return code */
160139 const char *zCsr = zNode; /* Cursor to iterate through node */
160140 const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
160141 char *zBuffer = 0; /* Buffer to load terms into */
160142 i64 nAlloc = 0; /* Size of allocated buffer */
160143 int isFirstTerm = 1; /* True when processing first term on page */
160144 sqlite3_int64 iChild; /* Block id of child node to descend to */
160145
160146 /* Skip over the 'height' varint that occurs at the start of every
160147 ** interior node. Then load the blockid of the left-child of the b-tree
160148 ** node into variable iChild.
160149 **
160150 ** Even if the data structure on disk is corrupted, this (reading two
160151 ** varints from the buffer) does not risk an overread. If zNode is a
160152 ** root node, then the buffer comes from a SELECT statement. SQLite does
160153 ** not make this guarantee explicitly, but in practice there are always
160154 ** either more than 20 bytes of allocated space following the nNode bytes of
160155 ** contents, or two zero bytes. Or, if the node is read from the %_segments
160156 ** table, then there are always 20 bytes of zeroed padding following the
160157 ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details).
160158 */
160159 zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
160160 zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
160161 if( zCsr>zEnd ){
160162 return FTS_CORRUPT_VTAB;
160163 }
160164
160165 while( zCsr<zEnd && (piFirst || piLast) ){
160166 int cmp; /* memcmp() result */
160167 int nSuffix; /* Size of term suffix */
160168 int nPrefix = 0; /* Size of term prefix */
160169 int nBuffer; /* Total term size */
160170
160171 /* Load the next term on the node into zBuffer. Use realloc() to expand
160172 ** the size of zBuffer if required. */
160173 if( !isFirstTerm ){
160174 zCsr += fts3GetVarint32(zCsr, &nPrefix);
160175 }
160176 isFirstTerm = 0;
160177 zCsr += fts3GetVarint32(zCsr, &nSuffix);
160178
160179 assert( nPrefix>=0 && nSuffix>=0 );
160180 if( nPrefix>zCsr-zNode || nSuffix>zEnd-zCsr ){
160181 rc = FTS_CORRUPT_VTAB;
160182 goto finish_scan;
160183 }
160184 if( (i64)nPrefix+nSuffix>nAlloc ){
160185 char *zNew;
160186 nAlloc = ((i64)nPrefix+nSuffix) * 2;
160187 zNew = (char *)sqlite3_realloc64(zBuffer, nAlloc);
160188 if( !zNew ){
160189 rc = SQLITE_NOMEM;
160190 goto finish_scan;
160191 }
160192 zBuffer = zNew;
160193 }
160194 assert( zBuffer );
160195 memcpy(&zBuffer[nPrefix], zCsr, nSuffix);
160196 nBuffer = nPrefix + nSuffix;
160197 zCsr += nSuffix;
160198
160199 /* Compare the term we are searching for with the term just loaded from
160200 ** the interior node. If the specified term is greater than or equal
160201 ** to the term from the interior node, then all terms on the sub-tree
160202 ** headed by node iChild are smaller than zTerm. No need to search
160203 ** iChild.
160204 **
160205 ** If the interior node term is larger than the specified term, then
160206 ** the tree headed by iChild may contain the specified term.
160207 */
160208 cmp = memcmp(zTerm, zBuffer, (nBuffer>nTerm ? nTerm : nBuffer));
160209 if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){
160210 *piFirst = iChild;
160211 piFirst = 0;
160212 }
160213
160214 if( piLast && cmp<0 ){
160215 *piLast = iChild;
160216 piLast = 0;
160217 }
160218
160219 iChild++;
160220 };
160221
160222 if( piFirst ) *piFirst = iChild;
160223 if( piLast ) *piLast = iChild;
160224
160225 finish_scan:
160226 sqlite3_free(zBuffer);
160227 return rc;
160228}
160229
160230
160231/*
160232** The buffer pointed to by argument zNode (size nNode bytes) contains an
160233** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes)
160234** contains a term. This function searches the sub-tree headed by the zNode
160235** node for the range of leaf nodes that may contain the specified term
160236** or terms for which the specified term is a prefix.
160237**
160238** If piLeaf is not NULL, then *piLeaf is set to the blockid of the
160239** left-most leaf node in the tree that may contain the specified term.
160240** If piLeaf2 is not NULL, then *piLeaf2 is set to the blockid of the
160241** right-most leaf node that may contain a term for which the specified
160242** term is a prefix.
160243**
160244** It is possible that the range of returned leaf nodes does not contain
160245** the specified term or any terms for which it is a prefix. However, if the
160246** segment does contain any such terms, they are stored within the identified
160247** range. Because this function only inspects interior segment nodes (and
160248** never loads leaf nodes into memory), it is not possible to be sure.
160249**
160250** If an error occurs, an error code other than SQLITE_OK is returned.
160251*/
160252static int fts3SelectLeaf(
160253 Fts3Table *p, /* Virtual table handle */
160254 const char *zTerm, /* Term to select leaves for */
160255 int nTerm, /* Size of term zTerm in bytes */
160256 const char *zNode, /* Buffer containing segment interior node */
160257 int nNode, /* Size of buffer at zNode */
160258 sqlite3_int64 *piLeaf, /* Selected leaf node */
160259 sqlite3_int64 *piLeaf2 /* Selected leaf node */
160260){
160261 int rc = SQLITE_OK; /* Return code */
160262 int iHeight; /* Height of this node in tree */
160263
160264 assert( piLeaf || piLeaf2 );
160265
160266 fts3GetVarint32(zNode, &iHeight);
160267 rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2);
160268 assert( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) );
160269
160270 if( rc==SQLITE_OK && iHeight>1 ){
160271 char *zBlob = 0; /* Blob read from %_segments table */
160272 int nBlob = 0; /* Size of zBlob in bytes */
160273
160274 if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){
160275 rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob, 0);
160276 if( rc==SQLITE_OK ){
160277 rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, 0);
160278 }
160279 sqlite3_free(zBlob);
160280 piLeaf = 0;
160281 zBlob = 0;
160282 }
160283
160284 if( rc==SQLITE_OK ){
160285 rc = sqlite3Fts3ReadBlock(p, piLeaf?*piLeaf:*piLeaf2, &zBlob, &nBlob, 0);
160286 }
160287 if( rc==SQLITE_OK ){
160288 rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2);
160289 }
160290 sqlite3_free(zBlob);
160291 }
160292
160293 return rc;
160294}
160295
160296/*
160297** This function is used to create delta-encoded serialized lists of FTS3
160298** varints. Each call to this function appends a single varint to a list.
160299*/
160300static void fts3PutDeltaVarint(
160301 char **pp, /* IN/OUT: Output pointer */
160302 sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */
160303 sqlite3_int64 iVal /* Write this value to the list */
160304){
160305 assert( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) );
160306 *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev);
160307 *piPrev = iVal;
160308}
160309
160310/*
160311** When this function is called, *ppPoslist is assumed to point to the
160312** start of a position-list. After it returns, *ppPoslist points to the
160313** first byte after the position-list.
160314**
160315** A position list is list of positions (delta encoded) and columns for
160316** a single document record of a doclist. So, in other words, this
160317** routine advances *ppPoslist so that it points to the next docid in
160318** the doclist, or to the first byte past the end of the doclist.
160319**
160320** If pp is not NULL, then the contents of the position list are copied
160321** to *pp. *pp is set to point to the first byte past the last byte copied
160322** before this function returns.
160323*/
160324static void fts3PoslistCopy(char **pp, char **ppPoslist){
160325 char *pEnd = *ppPoslist;
160326 char c = 0;
160327
160328 /* The end of a position list is marked by a zero encoded as an FTS3
160329 ** varint. A single POS_END (0) byte. Except, if the 0 byte is preceded by
160330 ** a byte with the 0x80 bit set, then it is not a varint 0, but the tail
160331 ** of some other, multi-byte, value.
160332 **
160333 ** The following while-loop moves pEnd to point to the first byte that is not
160334 ** immediately preceded by a byte with the 0x80 bit set. Then increments
160335 ** pEnd once more so that it points to the byte immediately following the
160336 ** last byte in the position-list.
160337 */
160338 while( *pEnd | c ){
160339 c = *pEnd++ & 0x80;
160340 testcase( c!=0 && (*pEnd)==0 );
160341 }
160342 pEnd++; /* Advance past the POS_END terminator byte */
160343
160344 if( pp ){
160345 int n = (int)(pEnd - *ppPoslist);
160346 char *p = *pp;
160347 memcpy(p, *ppPoslist, n);
160348 p += n;
160349 *pp = p;
160350 }
160351 *ppPoslist = pEnd;
160352}
160353
160354/*
160355** When this function is called, *ppPoslist is assumed to point to the
160356** start of a column-list. After it returns, *ppPoslist points to the
160357** to the terminator (POS_COLUMN or POS_END) byte of the column-list.
160358**
160359** A column-list is list of delta-encoded positions for a single column
160360** within a single document within a doclist.
160361**
160362** The column-list is terminated either by a POS_COLUMN varint (1) or
160363** a POS_END varint (0). This routine leaves *ppPoslist pointing to
160364** the POS_COLUMN or POS_END that terminates the column-list.
160365**
160366** If pp is not NULL, then the contents of the column-list are copied
160367** to *pp. *pp is set to point to the first byte past the last byte copied
160368** before this function returns. The POS_COLUMN or POS_END terminator
160369** is not copied into *pp.
160370*/
160371static void fts3ColumnlistCopy(char **pp, char **ppPoslist){
160372 char *pEnd = *ppPoslist;
160373 char c = 0;
160374
160375 /* A column-list is terminated by either a 0x01 or 0x00 byte that is
160376 ** not part of a multi-byte varint.
160377 */
160378 while( 0xFE & (*pEnd | c) ){
160379 c = *pEnd++ & 0x80;
160380 testcase( c!=0 && ((*pEnd)&0xfe)==0 );
160381 }
160382 if( pp ){
160383 int n = (int)(pEnd - *ppPoslist);
160384 char *p = *pp;
160385 memcpy(p, *ppPoslist, n);
160386 p += n;
160387 *pp = p;
160388 }
160389 *ppPoslist = pEnd;
160390}
160391
160392/*
160393** Value used to signify the end of an position-list. This is safe because
160394** it is not possible to have a document with 2^31 terms.
160395*/
160396#define POSITION_LIST_END 0x7fffffff
160397
160398/*
160399** This function is used to help parse position-lists. When this function is
160400** called, *pp may point to the start of the next varint in the position-list
160401** being parsed, or it may point to 1 byte past the end of the position-list
160402** (in which case **pp will be a terminator bytes POS_END (0) or
160403** (1)).
160404**
160405** If *pp points past the end of the current position-list, set *pi to
160406** POSITION_LIST_END and return. Otherwise, read the next varint from *pp,
160407** increment the current value of *pi by the value read, and set *pp to
160408** point to the next value before returning.
160409**
160410** Before calling this routine *pi must be initialized to the value of
160411** the previous position, or zero if we are reading the first position
160412** in the position-list. Because positions are delta-encoded, the value
160413** of the previous position is needed in order to compute the value of
160414** the next position.
160415*/
160416static void fts3ReadNextPos(
160417 char **pp, /* IN/OUT: Pointer into position-list buffer */
160418 sqlite3_int64 *pi /* IN/OUT: Value read from position-list */
160419){
160420 if( (**pp)&0xFE ){
160421 fts3GetDeltaVarint(pp, pi);
160422 *pi -= 2;
160423 }else{
160424 *pi = POSITION_LIST_END;
160425 }
160426}
160427
160428/*
160429** If parameter iCol is not 0, write an POS_COLUMN (1) byte followed by
160430** the value of iCol encoded as a varint to *pp. This will start a new
160431** column list.
160432**
160433** Set *pp to point to the byte just after the last byte written before
160434** returning (do not modify it if iCol==0). Return the total number of bytes
160435** written (0 if iCol==0).
160436*/
160437static int fts3PutColNumber(char **pp, int iCol){
160438 int n = 0; /* Number of bytes written */
160439 if( iCol ){
160440 char *p = *pp; /* Output pointer */
160441 n = 1 + sqlite3Fts3PutVarint(&p[1], iCol);
160442 *p = 0x01;
160443 *pp = &p[n];
160444 }
160445 return n;
160446}
160447
160448/*
160449** Compute the union of two position lists. The output written
160450** into *pp contains all positions of both *pp1 and *pp2 in sorted
160451** order and with any duplicates removed. All pointers are
160452** updated appropriately. The caller is responsible for insuring
160453** that there is enough space in *pp to hold the complete output.
160454*/
160455static void fts3PoslistMerge(
160456 char **pp, /* Output buffer */
160457 char **pp1, /* Left input list */
160458 char **pp2 /* Right input list */
160459){
160460 char *p = *pp;
160461 char *p1 = *pp1;
160462 char *p2 = *pp2;
160463
160464 while( *p1 || *p2 ){
160465 int iCol1; /* The current column index in pp1 */
160466 int iCol2; /* The current column index in pp2 */
160467
160468 if( *p1==POS_COLUMN ) fts3GetVarint32(&p1[1], &iCol1);
160469 else if( *p1==POS_END ) iCol1 = POSITION_LIST_END;
160470 else iCol1 = 0;
160471
160472 if( *p2==POS_COLUMN ) fts3GetVarint32(&p2[1], &iCol2);
160473 else if( *p2==POS_END ) iCol2 = POSITION_LIST_END;
160474 else iCol2 = 0;
160475
160476 if( iCol1==iCol2 ){
160477 sqlite3_int64 i1 = 0; /* Last position from pp1 */
160478 sqlite3_int64 i2 = 0; /* Last position from pp2 */
160479 sqlite3_int64 iPrev = 0;
160480 int n = fts3PutColNumber(&p, iCol1);
160481 p1 += n;
160482 p2 += n;
160483
160484 /* At this point, both p1 and p2 point to the start of column-lists
160485 ** for the same column (the column with index iCol1 and iCol2).
160486 ** A column-list is a list of non-negative delta-encoded varints, each
160487 ** incremented by 2 before being stored. Each list is terminated by a
160488 ** POS_END (0) or POS_COLUMN (1). The following block merges the two lists
160489 ** and writes the results to buffer p. p is left pointing to the byte
160490 ** after the list written. No terminator (POS_END or POS_COLUMN) is
160491 ** written to the output.
160492 */
160493 fts3GetDeltaVarint(&p1, &i1);
160494 fts3GetDeltaVarint(&p2, &i2);
160495 do {
160496 fts3PutDeltaVarint(&p, &iPrev, (i1<i2) ? i1 : i2);
160497 iPrev -= 2;
160498 if( i1==i2 ){
160499 fts3ReadNextPos(&p1, &i1);
160500 fts3ReadNextPos(&p2, &i2);
160501 }else if( i1<i2 ){
160502 fts3ReadNextPos(&p1, &i1);
160503 }else{
160504 fts3ReadNextPos(&p2, &i2);
160505 }
160506 }while( i1!=POSITION_LIST_END || i2!=POSITION_LIST_END );
160507 }else if( iCol1<iCol2 ){
160508 p1 += fts3PutColNumber(&p, iCol1);
160509 fts3ColumnlistCopy(&p, &p1);
160510 }else{
160511 p2 += fts3PutColNumber(&p, iCol2);
160512 fts3ColumnlistCopy(&p, &p2);
160513 }
160514 }
160515
160516 *p++ = POS_END;
160517 *pp = p;
160518 *pp1 = p1 + 1;
160519 *pp2 = p2 + 1;
160520}
160521
160522/*
160523** This function is used to merge two position lists into one. When it is
160524** called, *pp1 and *pp2 must both point to position lists. A position-list is
160525** the part of a doclist that follows each document id. For example, if a row
160526** contains:
160527**
160528** 'a b c'|'x y z'|'a b b a'
160529**
160530** Then the position list for this row for token 'b' would consist of:
160531**
160532** 0x02 0x01 0x02 0x03 0x03 0x00
160533**
160534** When this function returns, both *pp1 and *pp2 are left pointing to the
160535** byte following the 0x00 terminator of their respective position lists.
160536**
160537** If isSaveLeft is 0, an entry is added to the output position list for
160538** each position in *pp2 for which there exists one or more positions in
160539** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e.
160540** when the *pp1 token appears before the *pp2 token, but not more than nToken
160541** slots before it.
160542**
160543** e.g. nToken==1 searches for adjacent positions.
160544*/
160545static int fts3PoslistPhraseMerge(
160546 char **pp, /* IN/OUT: Preallocated output buffer */
160547 int nToken, /* Maximum difference in token positions */
160548 int isSaveLeft, /* Save the left position */
160549 int isExact, /* If *pp1 is exactly nTokens before *pp2 */
160550 char **pp1, /* IN/OUT: Left input list */
160551 char **pp2 /* IN/OUT: Right input list */
160552){
160553 char *p = *pp;
160554 char *p1 = *pp1;
160555 char *p2 = *pp2;
160556 int iCol1 = 0;
160557 int iCol2 = 0;
160558
160559 /* Never set both isSaveLeft and isExact for the same invocation. */
160560 assert( isSaveLeft==0 || isExact==0 );
160561
160562 assert( p!=0 && *p1!=0 && *p2!=0 );
160563 if( *p1==POS_COLUMN ){
160564 p1++;
160565 p1 += fts3GetVarint32(p1, &iCol1);
160566 }
160567 if( *p2==POS_COLUMN ){
160568 p2++;
160569 p2 += fts3GetVarint32(p2, &iCol2);
160570 }
160571
160572 while( 1 ){
160573 if( iCol1==iCol2 ){
160574 char *pSave = p;
160575 sqlite3_int64 iPrev = 0;
160576 sqlite3_int64 iPos1 = 0;
160577 sqlite3_int64 iPos2 = 0;
160578
160579 if( iCol1 ){
160580 *p++ = POS_COLUMN;
160581 p += sqlite3Fts3PutVarint(p, iCol1);
160582 }
160583
160584 assert( *p1!=POS_END && *p1!=POS_COLUMN );
160585 assert( *p2!=POS_END && *p2!=POS_COLUMN );
160586 fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
160587 fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
160588
160589 while( 1 ){
160590 if( iPos2==iPos1+nToken
160591 || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken)
160592 ){
160593 sqlite3_int64 iSave;
160594 iSave = isSaveLeft ? iPos1 : iPos2;
160595 fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2;
160596 pSave = 0;
160597 assert( p );
160598 }
160599 if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){
160600 if( (*p2&0xFE)==0 ) break;
160601 fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
160602 }else{
160603 if( (*p1&0xFE)==0 ) break;
160604 fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
160605 }
160606 }
160607
160608 if( pSave ){
160609 assert( pp && p );
160610 p = pSave;
160611 }
160612
160613 fts3ColumnlistCopy(0, &p1);
160614 fts3ColumnlistCopy(0, &p2);
160615 assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 );
160616 if( 0==*p1 || 0==*p2 ) break;
160617
160618 p1++;
160619 p1 += fts3GetVarint32(p1, &iCol1);
160620 p2++;
160621 p2 += fts3GetVarint32(p2, &iCol2);
160622 }
160623
160624 /* Advance pointer p1 or p2 (whichever corresponds to the smaller of
160625 ** iCol1 and iCol2) so that it points to either the 0x00 that marks the
160626 ** end of the position list, or the 0x01 that precedes the next
160627 ** column-number in the position list.
160628 */
160629 else if( iCol1<iCol2 ){
160630 fts3ColumnlistCopy(0, &p1);
160631 if( 0==*p1 ) break;
160632 p1++;
160633 p1 += fts3GetVarint32(p1, &iCol1);
160634 }else{
160635 fts3ColumnlistCopy(0, &p2);
160636 if( 0==*p2 ) break;
160637 p2++;
160638 p2 += fts3GetVarint32(p2, &iCol2);
160639 }
160640 }
160641
160642 fts3PoslistCopy(0, &p2);
160643 fts3PoslistCopy(0, &p1);
160644 *pp1 = p1;
160645 *pp2 = p2;
160646 if( *pp==p ){
160647 return 0;
160648 }
160649 *p++ = 0x00;
160650 *pp = p;
160651 return 1;
160652}
160653
160654/*
160655** Merge two position-lists as required by the NEAR operator. The argument
160656** position lists correspond to the left and right phrases of an expression
160657** like:
160658**
160659** "phrase 1" NEAR "phrase number 2"
160660**
160661** Position list *pp1 corresponds to the left-hand side of the NEAR
160662** expression and *pp2 to the right. As usual, the indexes in the position
160663** lists are the offsets of the last token in each phrase (tokens "1" and "2"
160664** in the example above).
160665**
160666** The output position list - written to *pp - is a copy of *pp2 with those
160667** entries that are not sufficiently NEAR entries in *pp1 removed.
160668*/
160669static int fts3PoslistNearMerge(
160670 char **pp, /* Output buffer */
160671 char *aTmp, /* Temporary buffer space */
160672 int nRight, /* Maximum difference in token positions */
160673 int nLeft, /* Maximum difference in token positions */
160674 char **pp1, /* IN/OUT: Left input list */
160675 char **pp2 /* IN/OUT: Right input list */
160676){
160677 char *p1 = *pp1;
160678 char *p2 = *pp2;
160679
160680 char *pTmp1 = aTmp;
160681 char *pTmp2;
160682 char *aTmp2;
160683 int res = 1;
160684
160685 fts3PoslistPhraseMerge(&pTmp1, nRight, 0, 0, pp1, pp2);
160686 aTmp2 = pTmp2 = pTmp1;
160687 *pp1 = p1;
160688 *pp2 = p2;
160689 fts3PoslistPhraseMerge(&pTmp2, nLeft, 1, 0, pp2, pp1);
160690 if( pTmp1!=aTmp && pTmp2!=aTmp2 ){
160691 fts3PoslistMerge(pp, &aTmp, &aTmp2);
160692 }else if( pTmp1!=aTmp ){
160693 fts3PoslistCopy(pp, &aTmp);
160694 }else if( pTmp2!=aTmp2 ){
160695 fts3PoslistCopy(pp, &aTmp2);
160696 }else{
160697 res = 0;
160698 }
160699
160700 return res;
160701}
160702
160703/*
160704** An instance of this function is used to merge together the (potentially
160705** large number of) doclists for each term that matches a prefix query.
160706** See function fts3TermSelectMerge() for details.
160707*/
160708typedef struct TermSelect TermSelect;
160709struct TermSelect {
160710 char *aaOutput[16]; /* Malloc'd output buffers */
160711 int anOutput[16]; /* Size each output buffer in bytes */
160712};
160713
160714/*
160715** This function is used to read a single varint from a buffer. Parameter
160716** pEnd points 1 byte past the end of the buffer. When this function is
160717** called, if *pp points to pEnd or greater, then the end of the buffer
160718** has been reached. In this case *pp is set to 0 and the function returns.
160719**
160720** If *pp does not point to or past pEnd, then a single varint is read
160721** from *pp. *pp is then set to point 1 byte past the end of the read varint.
160722**
160723** If bDescIdx is false, the value read is added to *pVal before returning.
160724** If it is true, the value read is subtracted from *pVal before this
160725** function returns.
160726*/
160727static void fts3GetDeltaVarint3(
160728 char **pp, /* IN/OUT: Point to read varint from */
160729 char *pEnd, /* End of buffer */
160730 int bDescIdx, /* True if docids are descending */
160731 sqlite3_int64 *pVal /* IN/OUT: Integer value */
160732){
160733 if( *pp>=pEnd ){
160734 *pp = 0;
160735 }else{
160736 sqlite3_int64 iVal;
160737 *pp += sqlite3Fts3GetVarint(*pp, &iVal);
160738 if( bDescIdx ){
160739 *pVal -= iVal;
160740 }else{
160741 *pVal += iVal;
160742 }
160743 }
160744}
160745
160746/*
160747** This function is used to write a single varint to a buffer. The varint
160748** is written to *pp. Before returning, *pp is set to point 1 byte past the
160749** end of the value written.
160750**
160751** If *pbFirst is zero when this function is called, the value written to
160752** the buffer is that of parameter iVal.
160753**
160754** If *pbFirst is non-zero when this function is called, then the value
160755** written is either (iVal-*piPrev) (if bDescIdx is zero) or (*piPrev-iVal)
160756** (if bDescIdx is non-zero).
160757**
160758** Before returning, this function always sets *pbFirst to 1 and *piPrev
160759** to the value of parameter iVal.
160760*/
160761static void fts3PutDeltaVarint3(
160762 char **pp, /* IN/OUT: Output pointer */
160763 int bDescIdx, /* True for descending docids */
160764 sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */
160765 int *pbFirst, /* IN/OUT: True after first int written */
160766 sqlite3_int64 iVal /* Write this value to the list */
160767){
160768 sqlite3_int64 iWrite;
160769 if( bDescIdx==0 || *pbFirst==0 ){
160770 iWrite = iVal - *piPrev;
160771 }else{
160772 iWrite = *piPrev - iVal;
160773 }
160774 assert( *pbFirst || *piPrev==0 );
160775 assert( *pbFirst==0 || iWrite>0 );
160776 *pp += sqlite3Fts3PutVarint(*pp, iWrite);
160777 *piPrev = iVal;
160778 *pbFirst = 1;
160779}
160780
160781
160782/*
160783** This macro is used by various functions that merge doclists. The two
160784** arguments are 64-bit docid values. If the value of the stack variable
160785** bDescDoclist is 0 when this macro is invoked, then it returns (i1-i2).
160786** Otherwise, (i2-i1).
160787**
160788** Using this makes it easier to write code that can merge doclists that are
160789** sorted in either ascending or descending order.
160790*/
160791#define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i1-i2))
160792
160793/*
160794** This function does an "OR" merge of two doclists (output contains all
160795** positions contained in either argument doclist). If the docids in the
160796** input doclists are sorted in ascending order, parameter bDescDoclist
160797** should be false. If they are sorted in ascending order, it should be
160798** passed a non-zero value.
160799**
160800** If no error occurs, *paOut is set to point at an sqlite3_malloc'd buffer
160801** containing the output doclist and SQLITE_OK is returned. In this case
160802** *pnOut is set to the number of bytes in the output doclist.
160803**
160804** If an error occurs, an SQLite error code is returned. The output values
160805** are undefined in this case.
160806*/
160807static int fts3DoclistOrMerge(
160808 int bDescDoclist, /* True if arguments are desc */
160809 char *a1, int n1, /* First doclist */
160810 char *a2, int n2, /* Second doclist */
160811 char **paOut, int *pnOut /* OUT: Malloc'd doclist */
160812){
160813 sqlite3_int64 i1 = 0;
160814 sqlite3_int64 i2 = 0;
160815 sqlite3_int64 iPrev = 0;
160816 char *pEnd1 = &a1[n1];
160817 char *pEnd2 = &a2[n2];
160818 char *p1 = a1;
160819 char *p2 = a2;
160820 char *p;
160821 char *aOut;
160822 int bFirstOut = 0;
160823
160824 *paOut = 0;
160825 *pnOut = 0;
160826
160827 /* Allocate space for the output. Both the input and output doclists
160828 ** are delta encoded. If they are in ascending order (bDescDoclist==0),
160829 ** then the first docid in each list is simply encoded as a varint. For
160830 ** each subsequent docid, the varint stored is the difference between the
160831 ** current and previous docid (a positive number - since the list is in
160832 ** ascending order).
160833 **
160834 ** The first docid written to the output is therefore encoded using the
160835 ** same number of bytes as it is in whichever of the input lists it is
160836 ** read from. And each subsequent docid read from the same input list
160837 ** consumes either the same or less bytes as it did in the input (since
160838 ** the difference between it and the previous value in the output must
160839 ** be a positive value less than or equal to the delta value read from
160840 ** the input list). The same argument applies to all but the first docid
160841 ** read from the 'other' list. And to the contents of all position lists
160842 ** that will be copied and merged from the input to the output.
160843 **
160844 ** However, if the first docid copied to the output is a negative number,
160845 ** then the encoding of the first docid from the 'other' input list may
160846 ** be larger in the output than it was in the input (since the delta value
160847 ** may be a larger positive integer than the actual docid).
160848 **
160849 ** The space required to store the output is therefore the sum of the
160850 ** sizes of the two inputs, plus enough space for exactly one of the input
160851 ** docids to grow.
160852 **
160853 ** A symetric argument may be made if the doclists are in descending
160854 ** order.
160855 */
160856 aOut = sqlite3_malloc(n1+n2+FTS3_VARINT_MAX-1);
160857 if( !aOut ) return SQLITE_NOMEM;
160858
160859 p = aOut;
160860 fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
160861 fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
160862 while( p1 || p2 ){
160863 sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
160864
160865 if( p2 && p1 && iDiff==0 ){
160866 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
160867 fts3PoslistMerge(&p, &p1, &p2);
160868 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
160869 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
160870 }else if( !p2 || (p1 && iDiff<0) ){
160871 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
160872 fts3PoslistCopy(&p, &p1);
160873 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
160874 }else{
160875 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i2);
160876 fts3PoslistCopy(&p, &p2);
160877 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
160878 }
160879 }
160880
160881 *paOut = aOut;
160882 *pnOut = (int)(p-aOut);
160883 assert( *pnOut<=n1+n2+FTS3_VARINT_MAX-1 );
160884 return SQLITE_OK;
160885}
160886
160887/*
160888** This function does a "phrase" merge of two doclists. In a phrase merge,
160889** the output contains a copy of each position from the right-hand input
160890** doclist for which there is a position in the left-hand input doclist
160891** exactly nDist tokens before it.
160892**
160893** If the docids in the input doclists are sorted in ascending order,
160894** parameter bDescDoclist should be false. If they are sorted in ascending
160895** order, it should be passed a non-zero value.
160896**
160897** The right-hand input doclist is overwritten by this function.
160898*/
160899static int fts3DoclistPhraseMerge(
160900 int bDescDoclist, /* True if arguments are desc */
160901 int nDist, /* Distance from left to right (1=adjacent) */
160902 char *aLeft, int nLeft, /* Left doclist */
160903 char **paRight, int *pnRight /* IN/OUT: Right/output doclist */
160904){
160905 sqlite3_int64 i1 = 0;
160906 sqlite3_int64 i2 = 0;
160907 sqlite3_int64 iPrev = 0;
160908 char *aRight = *paRight;
160909 char *pEnd1 = &aLeft[nLeft];
160910 char *pEnd2 = &aRight[*pnRight];
160911 char *p1 = aLeft;
160912 char *p2 = aRight;
160913 char *p;
160914 int bFirstOut = 0;
160915 char *aOut;
160916
160917 assert( nDist>0 );
160918 if( bDescDoclist ){
160919 aOut = sqlite3_malloc(*pnRight + FTS3_VARINT_MAX);
160920 if( aOut==0 ) return SQLITE_NOMEM;
160921 }else{
160922 aOut = aRight;
160923 }
160924 p = aOut;
160925
160926 fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
160927 fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
160928
160929 while( p1 && p2 ){
160930 sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
160931 if( iDiff==0 ){
160932 char *pSave = p;
160933 sqlite3_int64 iPrevSave = iPrev;
160934 int bFirstOutSave = bFirstOut;
160935
160936 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
160937 if( 0==fts3PoslistPhraseMerge(&p, nDist, 0, 1, &p1, &p2) ){
160938 p = pSave;
160939 iPrev = iPrevSave;
160940 bFirstOut = bFirstOutSave;
160941 }
160942 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
160943 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
160944 }else if( iDiff<0 ){
160945 fts3PoslistCopy(0, &p1);
160946 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
160947 }else{
160948 fts3PoslistCopy(0, &p2);
160949 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
160950 }
160951 }
160952
160953 *pnRight = (int)(p - aOut);
160954 if( bDescDoclist ){
160955 sqlite3_free(aRight);
160956 *paRight = aOut;
160957 }
160958
160959 return SQLITE_OK;
160960}
160961
160962/*
160963** Argument pList points to a position list nList bytes in size. This
160964** function checks to see if the position list contains any entries for
160965** a token in position 0 (of any column). If so, it writes argument iDelta
160966** to the output buffer pOut, followed by a position list consisting only
160967** of the entries from pList at position 0, and terminated by an 0x00 byte.
160968** The value returned is the number of bytes written to pOut (if any).
160969*/
160970SQLITE_PRIVATE int sqlite3Fts3FirstFilter(
160971 sqlite3_int64 iDelta, /* Varint that may be written to pOut */
160972 char *pList, /* Position list (no 0x00 term) */
160973 int nList, /* Size of pList in bytes */
160974 char *pOut /* Write output here */
160975){
160976 int nOut = 0;
160977 int bWritten = 0; /* True once iDelta has been written */
160978 char *p = pList;
160979 char *pEnd = &pList[nList];
160980
160981 if( *p!=0x01 ){
160982 if( *p==0x02 ){
160983 nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
160984 pOut[nOut++] = 0x02;
160985 bWritten = 1;
160986 }
160987 fts3ColumnlistCopy(0, &p);
160988 }
160989
160990 while( p<pEnd ){
160991 sqlite3_int64 iCol;
160992 p++;
160993 p += sqlite3Fts3GetVarint(p, &iCol);
160994 if( *p==0x02 ){
160995 if( bWritten==0 ){
160996 nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
160997 bWritten = 1;
160998 }
160999 pOut[nOut++] = 0x01;
161000 nOut += sqlite3Fts3PutVarint(&pOut[nOut], iCol);
161001 pOut[nOut++] = 0x02;
161002 }
161003 fts3ColumnlistCopy(0, &p);
161004 }
161005 if( bWritten ){
161006 pOut[nOut++] = 0x00;
161007 }
161008
161009 return nOut;
161010}
161011
161012
161013/*
161014** Merge all doclists in the TermSelect.aaOutput[] array into a single
161015** doclist stored in TermSelect.aaOutput[0]. If successful, delete all
161016** other doclists (except the aaOutput[0] one) and return SQLITE_OK.
161017**
161018** If an OOM error occurs, return SQLITE_NOMEM. In this case it is
161019** the responsibility of the caller to free any doclists left in the
161020** TermSelect.aaOutput[] array.
161021*/
161022static int fts3TermSelectFinishMerge(Fts3Table *p, TermSelect *pTS){
161023 char *aOut = 0;
161024 int nOut = 0;
161025 int i;
161026
161027 /* Loop through the doclists in the aaOutput[] array. Merge them all
161028 ** into a single doclist.
161029 */
161030 for(i=0; i<SizeofArray(pTS->aaOutput); i++){
161031 if( pTS->aaOutput[i] ){
161032 if( !aOut ){
161033 aOut = pTS->aaOutput[i];
161034 nOut = pTS->anOutput[i];
161035 pTS->aaOutput[i] = 0;
161036 }else{
161037 int nNew;
161038 char *aNew;
161039
161040 int rc = fts3DoclistOrMerge(p->bDescIdx,
161041 pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, &aNew, &nNew
161042 );
161043 if( rc!=SQLITE_OK ){
161044 sqlite3_free(aOut);
161045 return rc;
161046 }
161047
161048 sqlite3_free(pTS->aaOutput[i]);
161049 sqlite3_free(aOut);
161050 pTS->aaOutput[i] = 0;
161051 aOut = aNew;
161052 nOut = nNew;
161053 }
161054 }
161055 }
161056
161057 pTS->aaOutput[0] = aOut;
161058 pTS->anOutput[0] = nOut;
161059 return SQLITE_OK;
161060}
161061
161062/*
161063** Merge the doclist aDoclist/nDoclist into the TermSelect object passed
161064** as the first argument. The merge is an "OR" merge (see function
161065** fts3DoclistOrMerge() for details).
161066**
161067** This function is called with the doclist for each term that matches
161068** a queried prefix. It merges all these doclists into one, the doclist
161069** for the specified prefix. Since there can be a very large number of
161070** doclists to merge, the merging is done pair-wise using the TermSelect
161071** object.
161072**
161073** This function returns SQLITE_OK if the merge is successful, or an
161074** SQLite error code (SQLITE_NOMEM) if an error occurs.
161075*/
161076static int fts3TermSelectMerge(
161077 Fts3Table *p, /* FTS table handle */
161078 TermSelect *pTS, /* TermSelect object to merge into */
161079 char *aDoclist, /* Pointer to doclist */
161080 int nDoclist /* Size of aDoclist in bytes */
161081){
161082 if( pTS->aaOutput[0]==0 ){
161083 /* If this is the first term selected, copy the doclist to the output
161084 ** buffer using memcpy().
161085 **
161086 ** Add FTS3_VARINT_MAX bytes of unused space to the end of the
161087 ** allocation. This is so as to ensure that the buffer is big enough
161088 ** to hold the current doclist AND'd with any other doclist. If the
161089 ** doclists are stored in order=ASC order, this padding would not be
161090 ** required (since the size of [doclistA AND doclistB] is always less
161091 ** than or equal to the size of [doclistA] in that case). But this is
161092 ** not true for order=DESC. For example, a doclist containing (1, -1)
161093 ** may be smaller than (-1), as in the first example the -1 may be stored
161094 ** as a single-byte delta, whereas in the second it must be stored as a
161095 ** FTS3_VARINT_MAX byte varint.
161096 **
161097 ** Similar padding is added in the fts3DoclistOrMerge() function.
161098 */
161099 pTS->aaOutput[0] = sqlite3_malloc(nDoclist + FTS3_VARINT_MAX + 1);
161100 pTS->anOutput[0] = nDoclist;
161101 if( pTS->aaOutput[0] ){
161102 memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
161103 }else{
161104 return SQLITE_NOMEM;
161105 }
161106 }else{
161107 char *aMerge = aDoclist;
161108 int nMerge = nDoclist;
161109 int iOut;
161110
161111 for(iOut=0; iOut<SizeofArray(pTS->aaOutput); iOut++){
161112 if( pTS->aaOutput[iOut]==0 ){
161113 assert( iOut>0 );
161114 pTS->aaOutput[iOut] = aMerge;
161115 pTS->anOutput[iOut] = nMerge;
161116 break;
161117 }else{
161118 char *aNew;
161119 int nNew;
161120
161121 int rc = fts3DoclistOrMerge(p->bDescIdx, aMerge, nMerge,
161122 pTS->aaOutput[iOut], pTS->anOutput[iOut], &aNew, &nNew
161123 );
161124 if( rc!=SQLITE_OK ){
161125 if( aMerge!=aDoclist ) sqlite3_free(aMerge);
161126 return rc;
161127 }
161128
161129 if( aMerge!=aDoclist ) sqlite3_free(aMerge);
161130 sqlite3_free(pTS->aaOutput[iOut]);
161131 pTS->aaOutput[iOut] = 0;
161132
161133 aMerge = aNew;
161134 nMerge = nNew;
161135 if( (iOut+1)==SizeofArray(pTS->aaOutput) ){
161136 pTS->aaOutput[iOut] = aMerge;
161137 pTS->anOutput[iOut] = nMerge;
161138 }
161139 }
161140 }
161141 }
161142 return SQLITE_OK;
161143}
161144
161145/*
161146** Append SegReader object pNew to the end of the pCsr->apSegment[] array.
161147*/
161148static int fts3SegReaderCursorAppend(
161149 Fts3MultiSegReader *pCsr,
161150 Fts3SegReader *pNew
161151){
161152 if( (pCsr->nSegment%16)==0 ){
161153 Fts3SegReader **apNew;
161154 int nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
161155 apNew = (Fts3SegReader **)sqlite3_realloc(pCsr->apSegment, nByte);
161156 if( !apNew ){
161157 sqlite3Fts3SegReaderFree(pNew);
161158 return SQLITE_NOMEM;
161159 }
161160 pCsr->apSegment = apNew;
161161 }
161162 pCsr->apSegment[pCsr->nSegment++] = pNew;
161163 return SQLITE_OK;
161164}
161165
161166/*
161167** Add seg-reader objects to the Fts3MultiSegReader object passed as the
161168** 8th argument.
161169**
161170** This function returns SQLITE_OK if successful, or an SQLite error code
161171** otherwise.
161172*/
161173static int fts3SegReaderCursor(
161174 Fts3Table *p, /* FTS3 table handle */
161175 int iLangid, /* Language id */
161176 int iIndex, /* Index to search (from 0 to p->nIndex-1) */
161177 int iLevel, /* Level of segments to scan */
161178 const char *zTerm, /* Term to query for */
161179 int nTerm, /* Size of zTerm in bytes */
161180 int isPrefix, /* True for a prefix search */
161181 int isScan, /* True to scan from zTerm to EOF */
161182 Fts3MultiSegReader *pCsr /* Cursor object to populate */
161183){
161184 int rc = SQLITE_OK; /* Error code */
161185 sqlite3_stmt *pStmt = 0; /* Statement to iterate through segments */
161186 int rc2; /* Result of sqlite3_reset() */
161187
161188 /* If iLevel is less than 0 and this is not a scan, include a seg-reader
161189 ** for the pending-terms. If this is a scan, then this call must be being
161190 ** made by an fts4aux module, not an FTS table. In this case calling
161191 ** Fts3SegReaderPending might segfault, as the data structures used by
161192 ** fts4aux are not completely populated. So it's easiest to filter these
161193 ** calls out here. */
161194 if( iLevel<0 && p->aIndex ){
161195 Fts3SegReader *pSeg = 0;
161196 rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix||isScan, &pSeg);
161197 if( rc==SQLITE_OK && pSeg ){
161198 rc = fts3SegReaderCursorAppend(pCsr, pSeg);
161199 }
161200 }
161201
161202 if( iLevel!=FTS3_SEGCURSOR_PENDING ){
161203 if( rc==SQLITE_OK ){
161204 rc = sqlite3Fts3AllSegdirs(p, iLangid, iIndex, iLevel, &pStmt);
161205 }
161206
161207 while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
161208 Fts3SegReader *pSeg = 0;
161209
161210 /* Read the values returned by the SELECT into local variables. */
161211 sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1);
161212 sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2);
161213 sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3);
161214 int nRoot = sqlite3_column_bytes(pStmt, 4);
161215 char const *zRoot = sqlite3_column_blob(pStmt, 4);
161216
161217 /* If zTerm is not NULL, and this segment is not stored entirely on its
161218 ** root node, the range of leaves scanned can be reduced. Do this. */
161219 if( iStartBlock && zTerm ){
161220 sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0);
161221 rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi);
161222 if( rc!=SQLITE_OK ) goto finished;
161223 if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock;
161224 }
161225
161226 rc = sqlite3Fts3SegReaderNew(pCsr->nSegment+1,
161227 (isPrefix==0 && isScan==0),
161228 iStartBlock, iLeavesEndBlock,
161229 iEndBlock, zRoot, nRoot, &pSeg
161230 );
161231 if( rc!=SQLITE_OK ) goto finished;
161232 rc = fts3SegReaderCursorAppend(pCsr, pSeg);
161233 }
161234 }
161235
161236 finished:
161237 rc2 = sqlite3_reset(pStmt);
161238 if( rc==SQLITE_DONE ) rc = rc2;
161239
161240 return rc;
161241}
161242
161243/*
161244** Set up a cursor object for iterating through a full-text index or a
161245** single level therein.
161246*/
161247SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
161248 Fts3Table *p, /* FTS3 table handle */
161249 int iLangid, /* Language-id to search */
161250 int iIndex, /* Index to search (from 0 to p->nIndex-1) */
161251 int iLevel, /* Level of segments to scan */
161252 const char *zTerm, /* Term to query for */
161253 int nTerm, /* Size of zTerm in bytes */
161254 int isPrefix, /* True for a prefix search */
161255 int isScan, /* True to scan from zTerm to EOF */
161256 Fts3MultiSegReader *pCsr /* Cursor object to populate */
161257){
161258 assert( iIndex>=0 && iIndex<p->nIndex );
161259 assert( iLevel==FTS3_SEGCURSOR_ALL
161260 || iLevel==FTS3_SEGCURSOR_PENDING
161261 || iLevel>=0
161262 );
161263 assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
161264 assert( FTS3_SEGCURSOR_ALL<0 && FTS3_SEGCURSOR_PENDING<0 );
161265 assert( isPrefix==0 || isScan==0 );
161266
161267 memset(pCsr, 0, sizeof(Fts3MultiSegReader));
161268 return fts3SegReaderCursor(
161269 p, iLangid, iIndex, iLevel, zTerm, nTerm, isPrefix, isScan, pCsr
161270 );
161271}
161272
161273/*
161274** In addition to its current configuration, have the Fts3MultiSegReader
161275** passed as the 4th argument also scan the doclist for term zTerm/nTerm.
161276**
161277** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
161278*/
161279static int fts3SegReaderCursorAddZero(
161280 Fts3Table *p, /* FTS virtual table handle */
161281 int iLangid,
161282 const char *zTerm, /* Term to scan doclist of */
161283 int nTerm, /* Number of bytes in zTerm */
161284 Fts3MultiSegReader *pCsr /* Fts3MultiSegReader to modify */
161285){
161286 return fts3SegReaderCursor(p,
161287 iLangid, 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0,pCsr
161288 );
161289}
161290
161291/*
161292** Open an Fts3MultiSegReader to scan the doclist for term zTerm/nTerm. Or,
161293** if isPrefix is true, to scan the doclist for all terms for which
161294** zTerm/nTerm is a prefix. If successful, return SQLITE_OK and write
161295** a pointer to the new Fts3MultiSegReader to *ppSegcsr. Otherwise, return
161296** an SQLite error code.
161297**
161298** It is the responsibility of the caller to free this object by eventually
161299** passing it to fts3SegReaderCursorFree()
161300**
161301** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
161302** Output parameter *ppSegcsr is set to 0 if an error occurs.
161303*/
161304static int fts3TermSegReaderCursor(
161305 Fts3Cursor *pCsr, /* Virtual table cursor handle */
161306 const char *zTerm, /* Term to query for */
161307 int nTerm, /* Size of zTerm in bytes */
161308 int isPrefix, /* True for a prefix search */
161309 Fts3MultiSegReader **ppSegcsr /* OUT: Allocated seg-reader cursor */
161310){
161311 Fts3MultiSegReader *pSegcsr; /* Object to allocate and return */
161312 int rc = SQLITE_NOMEM; /* Return code */
161313
161314 pSegcsr = sqlite3_malloc(sizeof(Fts3MultiSegReader));
161315 if( pSegcsr ){
161316 int i;
161317 int bFound = 0; /* True once an index has been found */
161318 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
161319
161320 if( isPrefix ){
161321 for(i=1; bFound==0 && i<p->nIndex; i++){
161322 if( p->aIndex[i].nPrefix==nTerm ){
161323 bFound = 1;
161324 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
161325 i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0, pSegcsr
161326 );
161327 pSegcsr->bLookup = 1;
161328 }
161329 }
161330
161331 for(i=1; bFound==0 && i<p->nIndex; i++){
161332 if( p->aIndex[i].nPrefix==nTerm+1 ){
161333 bFound = 1;
161334 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
161335 i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 1, 0, pSegcsr
161336 );
161337 if( rc==SQLITE_OK ){
161338 rc = fts3SegReaderCursorAddZero(
161339 p, pCsr->iLangid, zTerm, nTerm, pSegcsr
161340 );
161341 }
161342 }
161343 }
161344 }
161345
161346 if( bFound==0 ){
161347 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
161348 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix, 0, pSegcsr
161349 );
161350 pSegcsr->bLookup = !isPrefix;
161351 }
161352 }
161353
161354 *ppSegcsr = pSegcsr;
161355 return rc;
161356}
161357
161358/*
161359** Free an Fts3MultiSegReader allocated by fts3TermSegReaderCursor().
161360*/
161361static void fts3SegReaderCursorFree(Fts3MultiSegReader *pSegcsr){
161362 sqlite3Fts3SegReaderFinish(pSegcsr);
161363 sqlite3_free(pSegcsr);
161364}
161365
161366/*
161367** This function retrieves the doclist for the specified term (or term
161368** prefix) from the database.
161369*/
161370static int fts3TermSelect(
161371 Fts3Table *p, /* Virtual table handle */
161372 Fts3PhraseToken *pTok, /* Token to query for */
161373 int iColumn, /* Column to query (or -ve for all columns) */
161374 int *pnOut, /* OUT: Size of buffer at *ppOut */
161375 char **ppOut /* OUT: Malloced result buffer */
161376){
161377 int rc; /* Return code */
161378 Fts3MultiSegReader *pSegcsr; /* Seg-reader cursor for this term */
161379 TermSelect tsc; /* Object for pair-wise doclist merging */
161380 Fts3SegFilter filter; /* Segment term filter configuration */
161381
161382 pSegcsr = pTok->pSegcsr;
161383 memset(&tsc, 0, sizeof(TermSelect));
161384
161385 filter.flags = FTS3_SEGMENT_IGNORE_EMPTY | FTS3_SEGMENT_REQUIRE_POS
161386 | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
161387 | (pTok->bFirst ? FTS3_SEGMENT_FIRST : 0)
161388 | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
161389 filter.iCol = iColumn;
161390 filter.zTerm = pTok->z;
161391 filter.nTerm = pTok->n;
161392
161393 rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter);
161394 while( SQLITE_OK==rc
161395 && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr))
161396 ){
161397 rc = fts3TermSelectMerge(p, &tsc, pSegcsr->aDoclist, pSegcsr->nDoclist);
161398 }
161399
161400 if( rc==SQLITE_OK ){
161401 rc = fts3TermSelectFinishMerge(p, &tsc);
161402 }
161403 if( rc==SQLITE_OK ){
161404 *ppOut = tsc.aaOutput[0];
161405 *pnOut = tsc.anOutput[0];
161406 }else{
161407 int i;
161408 for(i=0; i<SizeofArray(tsc.aaOutput); i++){
161409 sqlite3_free(tsc.aaOutput[i]);
161410 }
161411 }
161412
161413 fts3SegReaderCursorFree(pSegcsr);
161414 pTok->pSegcsr = 0;
161415 return rc;
161416}
161417
161418/*
161419** This function counts the total number of docids in the doclist stored
161420** in buffer aList[], size nList bytes.
161421**
161422** If the isPoslist argument is true, then it is assumed that the doclist
161423** contains a position-list following each docid. Otherwise, it is assumed
161424** that the doclist is simply a list of docids stored as delta encoded
161425** varints.
161426*/
161427static int fts3DoclistCountDocids(char *aList, int nList){
161428 int nDoc = 0; /* Return value */
161429 if( aList ){
161430 char *aEnd = &aList[nList]; /* Pointer to one byte after EOF */
161431 char *p = aList; /* Cursor */
161432 while( p<aEnd ){
161433 nDoc++;
161434 while( (*p++)&0x80 ); /* Skip docid varint */
161435 fts3PoslistCopy(0, &p); /* Skip over position list */
161436 }
161437 }
161438
161439 return nDoc;
161440}
161441
161442/*
161443** Advance the cursor to the next row in the %_content table that
161444** matches the search criteria. For a MATCH search, this will be
161445** the next row that matches. For a full-table scan, this will be
161446** simply the next row in the %_content table. For a docid lookup,
161447** this routine simply sets the EOF flag.
161448**
161449** Return SQLITE_OK if nothing goes wrong. SQLITE_OK is returned
161450** even if we reach end-of-file. The fts3EofMethod() will be called
161451** subsequently to determine whether or not an EOF was hit.
161452*/
161453static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){
161454 int rc;
161455 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
161456 if( pCsr->eSearch==FTS3_DOCID_SEARCH || pCsr->eSearch==FTS3_FULLSCAN_SEARCH ){
161457 if( SQLITE_ROW!=sqlite3_step(pCsr->pStmt) ){
161458 pCsr->isEof = 1;
161459 rc = sqlite3_reset(pCsr->pStmt);
161460 }else{
161461 pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0);
161462 rc = SQLITE_OK;
161463 }
161464 }else{
161465 rc = fts3EvalNext((Fts3Cursor *)pCursor);
161466 }
161467 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
161468 return rc;
161469}
161470
161471/*
161472** The following are copied from sqliteInt.h.
161473**
161474** Constants for the largest and smallest possible 64-bit signed integers.
161475** These macros are designed to work correctly on both 32-bit and 64-bit
161476** compilers.
161477*/
161478#ifndef SQLITE_AMALGAMATION
161479# define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
161480# define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
161481#endif
161482
161483/*
161484** If the numeric type of argument pVal is "integer", then return it
161485** converted to a 64-bit signed integer. Otherwise, return a copy of
161486** the second parameter, iDefault.
161487*/
161488static sqlite3_int64 fts3DocidRange(sqlite3_value *pVal, i64 iDefault){
161489 if( pVal ){
161490 int eType = sqlite3_value_numeric_type(pVal);
161491 if( eType==SQLITE_INTEGER ){
161492 return sqlite3_value_int64(pVal);
161493 }
161494 }
161495 return iDefault;
161496}
161497
161498/*
161499** This is the xFilter interface for the virtual table. See
161500** the virtual table xFilter method documentation for additional
161501** information.
161502**
161503** If idxNum==FTS3_FULLSCAN_SEARCH then do a full table scan against
161504** the %_content table.
161505**
161506** If idxNum==FTS3_DOCID_SEARCH then do a docid lookup for a single entry
161507** in the %_content table.
161508**
161509** If idxNum>=FTS3_FULLTEXT_SEARCH then use the full text index. The
161510** column on the left-hand side of the MATCH operator is column
161511** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed. argv[0] is the right-hand
161512** side of the MATCH operator.
161513*/
161514static int fts3FilterMethod(
161515 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
161516 int idxNum, /* Strategy index */
161517 const char *idxStr, /* Unused */
161518 int nVal, /* Number of elements in apVal */
161519 sqlite3_value **apVal /* Arguments for the indexing scheme */
161520){
161521 int rc = SQLITE_OK;
161522 char *zSql; /* SQL statement used to access %_content */
161523 int eSearch;
161524 Fts3Table *p = (Fts3Table *)pCursor->pVtab;
161525 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
161526
161527 sqlite3_value *pCons = 0; /* The MATCH or rowid constraint, if any */
161528 sqlite3_value *pLangid = 0; /* The "langid = ?" constraint, if any */
161529 sqlite3_value *pDocidGe = 0; /* The "docid >= ?" constraint, if any */
161530 sqlite3_value *pDocidLe = 0; /* The "docid <= ?" constraint, if any */
161531 int iIdx;
161532
161533 UNUSED_PARAMETER(idxStr);
161534 UNUSED_PARAMETER(nVal);
161535
161536 eSearch = (idxNum & 0x0000FFFF);
161537 assert( eSearch>=0 && eSearch<=(FTS3_FULLTEXT_SEARCH+p->nColumn) );
161538 assert( p->pSegments==0 );
161539
161540 /* Collect arguments into local variables */
161541 iIdx = 0;
161542 if( eSearch!=FTS3_FULLSCAN_SEARCH ) pCons = apVal[iIdx++];
161543 if( idxNum & FTS3_HAVE_LANGID ) pLangid = apVal[iIdx++];
161544 if( idxNum & FTS3_HAVE_DOCID_GE ) pDocidGe = apVal[iIdx++];
161545 if( idxNum & FTS3_HAVE_DOCID_LE ) pDocidLe = apVal[iIdx++];
161546 assert( iIdx==nVal );
161547
161548 /* In case the cursor has been used before, clear it now. */
161549 fts3ClearCursor(pCsr);
161550
161551 /* Set the lower and upper bounds on docids to return */
161552 pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64);
161553 pCsr->iMaxDocid = fts3DocidRange(pDocidLe, LARGEST_INT64);
161554
161555 if( idxStr ){
161556 pCsr->bDesc = (idxStr[0]=='D');
161557 }else{
161558 pCsr->bDesc = p->bDescIdx;
161559 }
161560 pCsr->eSearch = (i16)eSearch;
161561
161562 if( eSearch!=FTS3_DOCID_SEARCH && eSearch!=FTS3_FULLSCAN_SEARCH ){
161563 int iCol = eSearch-FTS3_FULLTEXT_SEARCH;
161564 const char *zQuery = (const char *)sqlite3_value_text(pCons);
161565
161566 if( zQuery==0 && sqlite3_value_type(pCons)!=SQLITE_NULL ){
161567 return SQLITE_NOMEM;
161568 }
161569
161570 pCsr->iLangid = 0;
161571 if( pLangid ) pCsr->iLangid = sqlite3_value_int(pLangid);
161572
161573 assert( p->base.zErrMsg==0 );
161574 rc = sqlite3Fts3ExprParse(p->pTokenizer, pCsr->iLangid,
161575 p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr,
161576 &p->base.zErrMsg
161577 );
161578 if( rc!=SQLITE_OK ){
161579 return rc;
161580 }
161581
161582 rc = fts3EvalStart(pCsr);
161583 sqlite3Fts3SegmentsClose(p);
161584 if( rc!=SQLITE_OK ) return rc;
161585 pCsr->pNextId = pCsr->aDoclist;
161586 pCsr->iPrevId = 0;
161587 }
161588
161589 /* Compile a SELECT statement for this cursor. For a full-table-scan, the
161590 ** statement loops through all rows of the %_content table. For a
161591 ** full-text query or docid lookup, the statement retrieves a single
161592 ** row by docid.
161593 */
161594 if( eSearch==FTS3_FULLSCAN_SEARCH ){
161595 if( pDocidGe || pDocidLe ){
161596 zSql = sqlite3_mprintf(
161597 "SELECT %s WHERE rowid BETWEEN %lld AND %lld ORDER BY rowid %s",
161598 p->zReadExprlist, pCsr->iMinDocid, pCsr->iMaxDocid,
161599 (pCsr->bDesc ? "DESC" : "ASC")
161600 );
161601 }else{
161602 zSql = sqlite3_mprintf("SELECT %s ORDER BY rowid %s",
161603 p->zReadExprlist, (pCsr->bDesc ? "DESC" : "ASC")
161604 );
161605 }
161606 if( zSql ){
161607 rc = sqlite3_prepare_v3(p->db,zSql,-1,SQLITE_PREPARE_PERSISTENT,&pCsr->pStmt,0);
161608 sqlite3_free(zSql);
161609 }else{
161610 rc = SQLITE_NOMEM;
161611 }
161612 }else if( eSearch==FTS3_DOCID_SEARCH ){
161613 rc = fts3CursorSeekStmt(pCsr);
161614 if( rc==SQLITE_OK ){
161615 rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons);
161616 }
161617 }
161618 if( rc!=SQLITE_OK ) return rc;
161619
161620 return fts3NextMethod(pCursor);
161621}
161622
161623/*
161624** This is the xEof method of the virtual table. SQLite calls this
161625** routine to find out if it has reached the end of a result set.
161626*/
161627static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){
161628 Fts3Cursor *pCsr = (Fts3Cursor*)pCursor;
161629 if( pCsr->isEof ){
161630 fts3ClearCursor(pCsr);
161631 pCsr->isEof = 1;
161632 }
161633 return pCsr->isEof;
161634}
161635
161636/*
161637** This is the xRowid method. The SQLite core calls this routine to
161638** retrieve the rowid for the current row of the result set. fts3
161639** exposes %_content.docid as the rowid for the virtual table. The
161640** rowid should be written to *pRowid.
161641*/
161642static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
161643 Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
161644 *pRowid = pCsr->iPrevId;
161645 return SQLITE_OK;
161646}
161647
161648/*
161649** This is the xColumn method, called by SQLite to request a value from
161650** the row that the supplied cursor currently points to.
161651**
161652** If:
161653**
161654** (iCol < p->nColumn) -> The value of the iCol'th user column.
161655** (iCol == p->nColumn) -> Magic column with the same name as the table.
161656** (iCol == p->nColumn+1) -> Docid column
161657** (iCol == p->nColumn+2) -> Langid column
161658*/
161659static int fts3ColumnMethod(
161660 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
161661 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
161662 int iCol /* Index of column to read value from */
161663){
161664 int rc = SQLITE_OK; /* Return Code */
161665 Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
161666 Fts3Table *p = (Fts3Table *)pCursor->pVtab;
161667
161668 /* The column value supplied by SQLite must be in range. */
161669 assert( iCol>=0 && iCol<=p->nColumn+2 );
161670
161671 switch( iCol-p->nColumn ){
161672 case 0:
161673 /* The special 'table-name' column */
161674 sqlite3_result_pointer(pCtx, pCsr, "fts3cursor", 0);
161675 break;
161676
161677 case 1:
161678 /* The docid column */
161679 sqlite3_result_int64(pCtx, pCsr->iPrevId);
161680 break;
161681
161682 case 2:
161683 if( pCsr->pExpr ){
161684 sqlite3_result_int64(pCtx, pCsr->iLangid);
161685 break;
161686 }else if( p->zLanguageid==0 ){
161687 sqlite3_result_int(pCtx, 0);
161688 break;
161689 }else{
161690 iCol = p->nColumn;
161691 /* fall-through */
161692 }
161693
161694 default:
161695 /* A user column. Or, if this is a full-table scan, possibly the
161696 ** language-id column. Seek the cursor. */
161697 rc = fts3CursorSeek(0, pCsr);
161698 if( rc==SQLITE_OK && sqlite3_data_count(pCsr->pStmt)-1>iCol ){
161699 sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1));
161700 }
161701 break;
161702 }
161703
161704 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
161705 return rc;
161706}
161707
161708/*
161709** This function is the implementation of the xUpdate callback used by
161710** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
161711** inserted, updated or deleted.
161712*/
161713static int fts3UpdateMethod(
161714 sqlite3_vtab *pVtab, /* Virtual table handle */
161715 int nArg, /* Size of argument array */
161716 sqlite3_value **apVal, /* Array of arguments */
161717 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
161718){
161719 return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid);
161720}
161721
161722/*
161723** Implementation of xSync() method. Flush the contents of the pending-terms
161724** hash-table to the database.
161725*/
161726static int fts3SyncMethod(sqlite3_vtab *pVtab){
161727
161728 /* Following an incremental-merge operation, assuming that the input
161729 ** segments are not completely consumed (the usual case), they are updated
161730 ** in place to remove the entries that have already been merged. This
161731 ** involves updating the leaf block that contains the smallest unmerged
161732 ** entry and each block (if any) between the leaf and the root node. So
161733 ** if the height of the input segment b-trees is N, and input segments
161734 ** are merged eight at a time, updating the input segments at the end
161735 ** of an incremental-merge requires writing (8*(1+N)) blocks. N is usually
161736 ** small - often between 0 and 2. So the overhead of the incremental
161737 ** merge is somewhere between 8 and 24 blocks. To avoid this overhead
161738 ** dwarfing the actual productive work accomplished, the incremental merge
161739 ** is only attempted if it will write at least 64 leaf blocks. Hence
161740 ** nMinMerge.
161741 **
161742 ** Of course, updating the input segments also involves deleting a bunch
161743 ** of blocks from the segments table. But this is not considered overhead
161744 ** as it would also be required by a crisis-merge that used the same input
161745 ** segments.
161746 */
161747 const u32 nMinMerge = 64; /* Minimum amount of incr-merge work to do */
161748
161749 Fts3Table *p = (Fts3Table*)pVtab;
161750 int rc;
161751 i64 iLastRowid = sqlite3_last_insert_rowid(p->db);
161752
161753 rc = sqlite3Fts3PendingTermsFlush(p);
161754 if( rc==SQLITE_OK
161755 && p->nLeafAdd>(nMinMerge/16)
161756 && p->nAutoincrmerge && p->nAutoincrmerge!=0xff
161757 ){
161758 int mxLevel = 0; /* Maximum relative level value in db */
161759 int A; /* Incr-merge parameter A */
161760
161761 rc = sqlite3Fts3MaxLevel(p, &mxLevel);
161762 assert( rc==SQLITE_OK || mxLevel==0 );
161763 A = p->nLeafAdd * mxLevel;
161764 A += (A/2);
161765 if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge);
161766 }
161767 sqlite3Fts3SegmentsClose(p);
161768 sqlite3_set_last_insert_rowid(p->db, iLastRowid);
161769 return rc;
161770}
161771
161772/*
161773** If it is currently unknown whether or not the FTS table has an %_stat
161774** table (if p->bHasStat==2), attempt to determine this (set p->bHasStat
161775** to 0 or 1). Return SQLITE_OK if successful, or an SQLite error code
161776** if an error occurs.
161777*/
161778static int fts3SetHasStat(Fts3Table *p){
161779 int rc = SQLITE_OK;
161780 if( p->bHasStat==2 ){
161781 char *zTbl = sqlite3_mprintf("%s_stat", p->zName);
161782 if( zTbl ){
161783 int res = sqlite3_table_column_metadata(p->db, p->zDb, zTbl, 0,0,0,0,0,0);
161784 sqlite3_free(zTbl);
161785 p->bHasStat = (res==SQLITE_OK);
161786 }else{
161787 rc = SQLITE_NOMEM;
161788 }
161789 }
161790 return rc;
161791}
161792
161793/*
161794** Implementation of xBegin() method.
161795*/
161796static int fts3BeginMethod(sqlite3_vtab *pVtab){
161797 Fts3Table *p = (Fts3Table*)pVtab;
161798 UNUSED_PARAMETER(pVtab);
161799 assert( p->pSegments==0 );
161800 assert( p->nPendingData==0 );
161801 assert( p->inTransaction!=1 );
161802 TESTONLY( p->inTransaction = 1 );
161803 TESTONLY( p->mxSavepoint = -1; );
161804 p->nLeafAdd = 0;
161805 return fts3SetHasStat(p);
161806}
161807
161808/*
161809** Implementation of xCommit() method. This is a no-op. The contents of
161810** the pending-terms hash-table have already been flushed into the database
161811** by fts3SyncMethod().
161812*/
161813static int fts3CommitMethod(sqlite3_vtab *pVtab){
161814 TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
161815 UNUSED_PARAMETER(pVtab);
161816 assert( p->nPendingData==0 );
161817 assert( p->inTransaction!=0 );
161818 assert( p->pSegments==0 );
161819 TESTONLY( p->inTransaction = 0 );
161820 TESTONLY( p->mxSavepoint = -1; );
161821 return SQLITE_OK;
161822}
161823
161824/*
161825** Implementation of xRollback(). Discard the contents of the pending-terms
161826** hash-table. Any changes made to the database are reverted by SQLite.
161827*/
161828static int fts3RollbackMethod(sqlite3_vtab *pVtab){
161829 Fts3Table *p = (Fts3Table*)pVtab;
161830 sqlite3Fts3PendingTermsClear(p);
161831 assert( p->inTransaction!=0 );
161832 TESTONLY( p->inTransaction = 0 );
161833 TESTONLY( p->mxSavepoint = -1; );
161834 return SQLITE_OK;
161835}
161836
161837/*
161838** When called, *ppPoslist must point to the byte immediately following the
161839** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function
161840** moves *ppPoslist so that it instead points to the first byte of the
161841** same position list.
161842*/
161843static void fts3ReversePoslist(char *pStart, char **ppPoslist){
161844 char *p = &(*ppPoslist)[-2];
161845 char c = 0;
161846
161847 /* Skip backwards passed any trailing 0x00 bytes added by NearTrim() */
161848 while( p>pStart && (c=*p--)==0 );
161849
161850 /* Search backwards for a varint with value zero (the end of the previous
161851 ** poslist). This is an 0x00 byte preceded by some byte that does not
161852 ** have the 0x80 bit set. */
161853 while( p>pStart && (*p & 0x80) | c ){
161854 c = *p--;
161855 }
161856 assert( p==pStart || c==0 );
161857
161858 /* At this point p points to that preceding byte without the 0x80 bit
161859 ** set. So to find the start of the poslist, skip forward 2 bytes then
161860 ** over a varint.
161861 **
161862 ** Normally. The other case is that p==pStart and the poslist to return
161863 ** is the first in the doclist. In this case do not skip forward 2 bytes.
161864 ** The second part of the if condition (c==0 && *ppPoslist>&p[2])
161865 ** is required for cases where the first byte of a doclist and the
161866 ** doclist is empty. For example, if the first docid is 10, a doclist
161867 ** that begins with:
161868 **
161869 ** 0x0A 0x00 <next docid delta varint>
161870 */
161871 if( p>pStart || (c==0 && *ppPoslist>&p[2]) ){ p = &p[2]; }
161872 while( *p++&0x80 );
161873 *ppPoslist = p;
161874}
161875
161876/*
161877** Helper function used by the implementation of the overloaded snippet(),
161878** offsets() and optimize() SQL functions.
161879**
161880** If the value passed as the third argument is a blob of size
161881** sizeof(Fts3Cursor*), then the blob contents are copied to the
161882** output variable *ppCsr and SQLITE_OK is returned. Otherwise, an error
161883** message is written to context pContext and SQLITE_ERROR returned. The
161884** string passed via zFunc is used as part of the error message.
161885*/
161886static int fts3FunctionArg(
161887 sqlite3_context *pContext, /* SQL function call context */
161888 const char *zFunc, /* Function name */
161889 sqlite3_value *pVal, /* argv[0] passed to function */
161890 Fts3Cursor **ppCsr /* OUT: Store cursor handle here */
161891){
161892 int rc;
161893 *ppCsr = (Fts3Cursor*)sqlite3_value_pointer(pVal, "fts3cursor");
161894 if( (*ppCsr)!=0 ){
161895 rc = SQLITE_OK;
161896 }else{
161897 char *zErr = sqlite3_mprintf("illegal first argument to %s", zFunc);
161898 sqlite3_result_error(pContext, zErr, -1);
161899 sqlite3_free(zErr);
161900 rc = SQLITE_ERROR;
161901 }
161902 return rc;
161903}
161904
161905/*
161906** Implementation of the snippet() function for FTS3
161907*/
161908static void fts3SnippetFunc(
161909 sqlite3_context *pContext, /* SQLite function call context */
161910 int nVal, /* Size of apVal[] array */
161911 sqlite3_value **apVal /* Array of arguments */
161912){
161913 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
161914 const char *zStart = "<b>";
161915 const char *zEnd = "</b>";
161916 const char *zEllipsis = "<b>...</b>";
161917 int iCol = -1;
161918 int nToken = 15; /* Default number of tokens in snippet */
161919
161920 /* There must be at least one argument passed to this function (otherwise
161921 ** the non-overloaded version would have been called instead of this one).
161922 */
161923 assert( nVal>=1 );
161924
161925 if( nVal>6 ){
161926 sqlite3_result_error(pContext,
161927 "wrong number of arguments to function snippet()", -1);
161928 return;
161929 }
161930 if( fts3FunctionArg(pContext, "snippet", apVal[0], &pCsr) ) return;
161931
161932 switch( nVal ){
161933 case 6: nToken = sqlite3_value_int(apVal[5]);
161934 case 5: iCol = sqlite3_value_int(apVal[4]);
161935 case 4: zEllipsis = (const char*)sqlite3_value_text(apVal[3]);
161936 case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]);
161937 case 2: zStart = (const char*)sqlite3_value_text(apVal[1]);
161938 }
161939 if( !zEllipsis || !zEnd || !zStart ){
161940 sqlite3_result_error_nomem(pContext);
161941 }else if( nToken==0 ){
161942 sqlite3_result_text(pContext, "", -1, SQLITE_STATIC);
161943 }else if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
161944 sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken);
161945 }
161946}
161947
161948/*
161949** Implementation of the offsets() function for FTS3
161950*/
161951static void fts3OffsetsFunc(
161952 sqlite3_context *pContext, /* SQLite function call context */
161953 int nVal, /* Size of argument array */
161954 sqlite3_value **apVal /* Array of arguments */
161955){
161956 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
161957
161958 UNUSED_PARAMETER(nVal);
161959
161960 assert( nVal==1 );
161961 if( fts3FunctionArg(pContext, "offsets", apVal[0], &pCsr) ) return;
161962 assert( pCsr );
161963 if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
161964 sqlite3Fts3Offsets(pContext, pCsr);
161965 }
161966}
161967
161968/*
161969** Implementation of the special optimize() function for FTS3. This
161970** function merges all segments in the database to a single segment.
161971** Example usage is:
161972**
161973** SELECT optimize(t) FROM t LIMIT 1;
161974**
161975** where 't' is the name of an FTS3 table.
161976*/
161977static void fts3OptimizeFunc(
161978 sqlite3_context *pContext, /* SQLite function call context */
161979 int nVal, /* Size of argument array */
161980 sqlite3_value **apVal /* Array of arguments */
161981){
161982 int rc; /* Return code */
161983 Fts3Table *p; /* Virtual table handle */
161984 Fts3Cursor *pCursor; /* Cursor handle passed through apVal[0] */
161985
161986 UNUSED_PARAMETER(nVal);
161987
161988 assert( nVal==1 );
161989 if( fts3FunctionArg(pContext, "optimize", apVal[0], &pCursor) ) return;
161990 p = (Fts3Table *)pCursor->base.pVtab;
161991 assert( p );
161992
161993 rc = sqlite3Fts3Optimize(p);
161994
161995 switch( rc ){
161996 case SQLITE_OK:
161997 sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC);
161998 break;
161999 case SQLITE_DONE:
162000 sqlite3_result_text(pContext, "Index already optimal", -1, SQLITE_STATIC);
162001 break;
162002 default:
162003 sqlite3_result_error_code(pContext, rc);
162004 break;
162005 }
162006}
162007
162008/*
162009** Implementation of the matchinfo() function for FTS3
162010*/
162011static void fts3MatchinfoFunc(
162012 sqlite3_context *pContext, /* SQLite function call context */
162013 int nVal, /* Size of argument array */
162014 sqlite3_value **apVal /* Array of arguments */
162015){
162016 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
162017 assert( nVal==1 || nVal==2 );
162018 if( SQLITE_OK==fts3FunctionArg(pContext, "matchinfo", apVal[0], &pCsr) ){
162019 const char *zArg = 0;
162020 if( nVal>1 ){
162021 zArg = (const char *)sqlite3_value_text(apVal[1]);
162022 }
162023 sqlite3Fts3Matchinfo(pContext, pCsr, zArg);
162024 }
162025}
162026
162027/*
162028** This routine implements the xFindFunction method for the FTS3
162029** virtual table.
162030*/
162031static int fts3FindFunctionMethod(
162032 sqlite3_vtab *pVtab, /* Virtual table handle */
162033 int nArg, /* Number of SQL function arguments */
162034 const char *zName, /* Name of SQL function */
162035 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
162036 void **ppArg /* Unused */
162037){
162038 struct Overloaded {
162039 const char *zName;
162040 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
162041 } aOverload[] = {
162042 { "snippet", fts3SnippetFunc },
162043 { "offsets", fts3OffsetsFunc },
162044 { "optimize", fts3OptimizeFunc },
162045 { "matchinfo", fts3MatchinfoFunc },
162046 };
162047 int i; /* Iterator variable */
162048
162049 UNUSED_PARAMETER(pVtab);
162050 UNUSED_PARAMETER(nArg);
162051 UNUSED_PARAMETER(ppArg);
162052
162053 for(i=0; i<SizeofArray(aOverload); i++){
162054 if( strcmp(zName, aOverload[i].zName)==0 ){
162055 *pxFunc = aOverload[i].xFunc;
162056 return 1;
162057 }
162058 }
162059
162060 /* No function of the specified name was found. Return 0. */
162061 return 0;
162062}
162063
162064/*
162065** Implementation of FTS3 xRename method. Rename an fts3 table.
162066*/
162067static int fts3RenameMethod(
162068 sqlite3_vtab *pVtab, /* Virtual table handle */
162069 const char *zName /* New name of table */
162070){
162071 Fts3Table *p = (Fts3Table *)pVtab;
162072 sqlite3 *db = p->db; /* Database connection */
162073 int rc; /* Return Code */
162074
162075 /* At this point it must be known if the %_stat table exists or not.
162076 ** So bHasStat may not be 2. */
162077 rc = fts3SetHasStat(p);
162078
162079 /* As it happens, the pending terms table is always empty here. This is
162080 ** because an "ALTER TABLE RENAME TABLE" statement inside a transaction
162081 ** always opens a savepoint transaction. And the xSavepoint() method
162082 ** flushes the pending terms table. But leave the (no-op) call to
162083 ** PendingTermsFlush() in in case that changes.
162084 */
162085 assert( p->nPendingData==0 );
162086 if( rc==SQLITE_OK ){
162087 rc = sqlite3Fts3PendingTermsFlush(p);
162088 }
162089
162090 if( p->zContentTbl==0 ){
162091 fts3DbExec(&rc, db,
162092 "ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';",
162093 p->zDb, p->zName, zName
162094 );
162095 }
162096
162097 if( p->bHasDocsize ){
162098 fts3DbExec(&rc, db,
162099 "ALTER TABLE %Q.'%q_docsize' RENAME TO '%q_docsize';",
162100 p->zDb, p->zName, zName
162101 );
162102 }
162103 if( p->bHasStat ){
162104 fts3DbExec(&rc, db,
162105 "ALTER TABLE %Q.'%q_stat' RENAME TO '%q_stat';",
162106 p->zDb, p->zName, zName
162107 );
162108 }
162109 fts3DbExec(&rc, db,
162110 "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';",
162111 p->zDb, p->zName, zName
162112 );
162113 fts3DbExec(&rc, db,
162114 "ALTER TABLE %Q.'%q_segdir' RENAME TO '%q_segdir';",
162115 p->zDb, p->zName, zName
162116 );
162117 return rc;
162118}
162119
162120/*
162121** The xSavepoint() method.
162122**
162123** Flush the contents of the pending-terms table to disk.
162124*/
162125static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
162126 int rc = SQLITE_OK;
162127 UNUSED_PARAMETER(iSavepoint);
162128 assert( ((Fts3Table *)pVtab)->inTransaction );
162129 assert( ((Fts3Table *)pVtab)->mxSavepoint <= iSavepoint );
162130 TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint );
162131 if( ((Fts3Table *)pVtab)->bIgnoreSavepoint==0 ){
162132 rc = fts3SyncMethod(pVtab);
162133 }
162134 return rc;
162135}
162136
162137/*
162138** The xRelease() method.
162139**
162140** This is a no-op.
162141*/
162142static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
162143 TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
162144 UNUSED_PARAMETER(iSavepoint);
162145 UNUSED_PARAMETER(pVtab);
162146 assert( p->inTransaction );
162147 assert( p->mxSavepoint >= iSavepoint );
162148 TESTONLY( p->mxSavepoint = iSavepoint-1 );
162149 return SQLITE_OK;
162150}
162151
162152/*
162153** The xRollbackTo() method.
162154**
162155** Discard the contents of the pending terms table.
162156*/
162157static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
162158 Fts3Table *p = (Fts3Table*)pVtab;
162159 UNUSED_PARAMETER(iSavepoint);
162160 assert( p->inTransaction );
162161 assert( p->mxSavepoint >= iSavepoint );
162162 TESTONLY( p->mxSavepoint = iSavepoint );
162163 sqlite3Fts3PendingTermsClear(p);
162164 return SQLITE_OK;
162165}
162166
162167/*
162168** Return true if zName is the extension on one of the shadow tables used
162169** by this module.
162170*/
162171static int fts3ShadowName(const char *zName){
162172 static const char *azName[] = {
162173 "content", "docsize", "segdir", "segments", "stat",
162174 };
162175 unsigned int i;
162176 for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
162177 if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
162178 }
162179 return 0;
162180}
162181
162182static const sqlite3_module fts3Module = {
162183 /* iVersion */ 3,
162184 /* xCreate */ fts3CreateMethod,
162185 /* xConnect */ fts3ConnectMethod,
162186 /* xBestIndex */ fts3BestIndexMethod,
162187 /* xDisconnect */ fts3DisconnectMethod,
162188 /* xDestroy */ fts3DestroyMethod,
162189 /* xOpen */ fts3OpenMethod,
162190 /* xClose */ fts3CloseMethod,
162191 /* xFilter */ fts3FilterMethod,
162192 /* xNext */ fts3NextMethod,
162193 /* xEof */ fts3EofMethod,
162194 /* xColumn */ fts3ColumnMethod,
162195 /* xRowid */ fts3RowidMethod,
162196 /* xUpdate */ fts3UpdateMethod,
162197 /* xBegin */ fts3BeginMethod,
162198 /* xSync */ fts3SyncMethod,
162199 /* xCommit */ fts3CommitMethod,
162200 /* xRollback */ fts3RollbackMethod,
162201 /* xFindFunction */ fts3FindFunctionMethod,
162202 /* xRename */ fts3RenameMethod,
162203 /* xSavepoint */ fts3SavepointMethod,
162204 /* xRelease */ fts3ReleaseMethod,
162205 /* xRollbackTo */ fts3RollbackToMethod,
162206 /* xShadowName */ fts3ShadowName,
162207};
162208
162209/*
162210** This function is registered as the module destructor (called when an
162211** FTS3 enabled database connection is closed). It frees the memory
162212** allocated for the tokenizer hash table.
162213*/
162214static void hashDestroy(void *p){
162215 Fts3Hash *pHash = (Fts3Hash *)p;
162216 sqlite3Fts3HashClear(pHash);
162217 sqlite3_free(pHash);
162218}
162219
162220/*
162221** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are
162222** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c
162223** respectively. The following three forward declarations are for functions
162224** declared in these files used to retrieve the respective implementations.
162225**
162226** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
162227** to by the argument to point to the "simple" tokenizer implementation.
162228** And so on.
162229*/
162230SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
162231SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
162232#ifndef SQLITE_DISABLE_FTS3_UNICODE
162233SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const**ppModule);
162234#endif
162235#ifdef SQLITE_ENABLE_ICU
162236SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
162237#endif
162238
162239/*
162240** Initialize the fts3 extension. If this extension is built as part
162241** of the sqlite library, then this function is called directly by
162242** SQLite. If fts3 is built as a dynamically loadable extension, this
162243** function is called by the sqlite3_extension_init() entry point.
162244*/
162245SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){
162246 int rc = SQLITE_OK;
162247 Fts3Hash *pHash = 0;
162248 const sqlite3_tokenizer_module *pSimple = 0;
162249 const sqlite3_tokenizer_module *pPorter = 0;
162250#ifndef SQLITE_DISABLE_FTS3_UNICODE
162251 const sqlite3_tokenizer_module *pUnicode = 0;
162252#endif
162253
162254#ifdef SQLITE_ENABLE_ICU
162255 const sqlite3_tokenizer_module *pIcu = 0;
162256 sqlite3Fts3IcuTokenizerModule(&pIcu);
162257#endif
162258
162259#ifndef SQLITE_DISABLE_FTS3_UNICODE
162260 sqlite3Fts3UnicodeTokenizer(&pUnicode);
162261#endif
162262
162263#ifdef SQLITE_TEST
162264 rc = sqlite3Fts3InitTerm(db);
162265 if( rc!=SQLITE_OK ) return rc;
162266#endif
162267
162268 rc = sqlite3Fts3InitAux(db);
162269 if( rc!=SQLITE_OK ) return rc;
162270
162271 sqlite3Fts3SimpleTokenizerModule(&pSimple);
162272 sqlite3Fts3PorterTokenizerModule(&pPorter);
162273
162274 /* Allocate and initialize the hash-table used to store tokenizers. */
162275 pHash = sqlite3_malloc(sizeof(Fts3Hash));
162276 if( !pHash ){
162277 rc = SQLITE_NOMEM;
162278 }else{
162279 sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
162280 }
162281
162282 /* Load the built-in tokenizers into the hash table */
162283 if( rc==SQLITE_OK ){
162284 if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple)
162285 || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter)
162286
162287#ifndef SQLITE_DISABLE_FTS3_UNICODE
162288 || sqlite3Fts3HashInsert(pHash, "unicode61", 10, (void *)pUnicode)
162289#endif
162290#ifdef SQLITE_ENABLE_ICU
162291 || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu))
162292#endif
162293 ){
162294 rc = SQLITE_NOMEM;
162295 }
162296 }
162297
162298#ifdef SQLITE_TEST
162299 if( rc==SQLITE_OK ){
162300 rc = sqlite3Fts3ExprInitTestInterface(db, pHash);
162301 }
162302#endif
162303
162304 /* Create the virtual table wrapper around the hash-table and overload
162305 ** the four scalar functions. If this is successful, register the
162306 ** module with sqlite.
162307 */
162308 if( SQLITE_OK==rc
162309 && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer"))
162310 && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
162311 && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1))
162312 && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1))
162313 && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2))
162314 && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1))
162315 ){
162316 rc = sqlite3_create_module_v2(
162317 db, "fts3", &fts3Module, (void *)pHash, hashDestroy
162318 );
162319 if( rc==SQLITE_OK ){
162320 rc = sqlite3_create_module_v2(
162321 db, "fts4", &fts3Module, (void *)pHash, 0
162322 );
162323 }
162324 if( rc==SQLITE_OK ){
162325 rc = sqlite3Fts3InitTok(db, (void *)pHash);
162326 }
162327 return rc;
162328 }
162329
162330
162331 /* An error has occurred. Delete the hash table and return the error code. */
162332 assert( rc!=SQLITE_OK );
162333 if( pHash ){
162334 sqlite3Fts3HashClear(pHash);
162335 sqlite3_free(pHash);
162336 }
162337 return rc;
162338}
162339
162340/*
162341** Allocate an Fts3MultiSegReader for each token in the expression headed
162342** by pExpr.
162343**
162344** An Fts3SegReader object is a cursor that can seek or scan a range of
162345** entries within a single segment b-tree. An Fts3MultiSegReader uses multiple
162346** Fts3SegReader objects internally to provide an interface to seek or scan
162347** within the union of all segments of a b-tree. Hence the name.
162348**
162349** If the allocated Fts3MultiSegReader just seeks to a single entry in a
162350** segment b-tree (if the term is not a prefix or it is a prefix for which
162351** there exists prefix b-tree of the right length) then it may be traversed
162352** and merged incrementally. Otherwise, it has to be merged into an in-memory
162353** doclist and then traversed.
162354*/
162355static void fts3EvalAllocateReaders(
162356 Fts3Cursor *pCsr, /* FTS cursor handle */
162357 Fts3Expr *pExpr, /* Allocate readers for this expression */
162358 int *pnToken, /* OUT: Total number of tokens in phrase. */
162359 int *pnOr, /* OUT: Total number of OR nodes in expr. */
162360 int *pRc /* IN/OUT: Error code */
162361){
162362 if( pExpr && SQLITE_OK==*pRc ){
162363 if( pExpr->eType==FTSQUERY_PHRASE ){
162364 int i;
162365 int nToken = pExpr->pPhrase->nToken;
162366 *pnToken += nToken;
162367 for(i=0; i<nToken; i++){
162368 Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i];
162369 int rc = fts3TermSegReaderCursor(pCsr,
162370 pToken->z, pToken->n, pToken->isPrefix, &pToken->pSegcsr
162371 );
162372 if( rc!=SQLITE_OK ){
162373 *pRc = rc;
162374 return;
162375 }
162376 }
162377 assert( pExpr->pPhrase->iDoclistToken==0 );
162378 pExpr->pPhrase->iDoclistToken = -1;
162379 }else{
162380 *pnOr += (pExpr->eType==FTSQUERY_OR);
162381 fts3EvalAllocateReaders(pCsr, pExpr->pLeft, pnToken, pnOr, pRc);
162382 fts3EvalAllocateReaders(pCsr, pExpr->pRight, pnToken, pnOr, pRc);
162383 }
162384 }
162385}
162386
162387/*
162388** Arguments pList/nList contain the doclist for token iToken of phrase p.
162389** It is merged into the main doclist stored in p->doclist.aAll/nAll.
162390**
162391** This function assumes that pList points to a buffer allocated using
162392** sqlite3_malloc(). This function takes responsibility for eventually
162393** freeing the buffer.
162394**
162395** SQLITE_OK is returned if successful, or SQLITE_NOMEM if an error occurs.
162396*/
162397static int fts3EvalPhraseMergeToken(
162398 Fts3Table *pTab, /* FTS Table pointer */
162399 Fts3Phrase *p, /* Phrase to merge pList/nList into */
162400 int iToken, /* Token pList/nList corresponds to */
162401 char *pList, /* Pointer to doclist */
162402 int nList /* Number of bytes in pList */
162403){
162404 int rc = SQLITE_OK;
162405 assert( iToken!=p->iDoclistToken );
162406
162407 if( pList==0 ){
162408 sqlite3_free(p->doclist.aAll);
162409 p->doclist.aAll = 0;
162410 p->doclist.nAll = 0;
162411 }
162412
162413 else if( p->iDoclistToken<0 ){
162414 p->doclist.aAll = pList;
162415 p->doclist.nAll = nList;
162416 }
162417
162418 else if( p->doclist.aAll==0 ){
162419 sqlite3_free(pList);
162420 }
162421
162422 else {
162423 char *pLeft;
162424 char *pRight;
162425 int nLeft;
162426 int nRight;
162427 int nDiff;
162428
162429 if( p->iDoclistToken<iToken ){
162430 pLeft = p->doclist.aAll;
162431 nLeft = p->doclist.nAll;
162432 pRight = pList;
162433 nRight = nList;
162434 nDiff = iToken - p->iDoclistToken;
162435 }else{
162436 pRight = p->doclist.aAll;
162437 nRight = p->doclist.nAll;
162438 pLeft = pList;
162439 nLeft = nList;
162440 nDiff = p->iDoclistToken - iToken;
162441 }
162442
162443 rc = fts3DoclistPhraseMerge(
162444 pTab->bDescIdx, nDiff, pLeft, nLeft, &pRight, &nRight
162445 );
162446 sqlite3_free(pLeft);
162447 p->doclist.aAll = pRight;
162448 p->doclist.nAll = nRight;
162449 }
162450
162451 if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
162452 return rc;
162453}
162454
162455/*
162456** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist
162457** does not take deferred tokens into account.
162458**
162459** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
162460*/
162461static int fts3EvalPhraseLoad(
162462 Fts3Cursor *pCsr, /* FTS Cursor handle */
162463 Fts3Phrase *p /* Phrase object */
162464){
162465 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
162466 int iToken;
162467 int rc = SQLITE_OK;
162468
162469 for(iToken=0; rc==SQLITE_OK && iToken<p->nToken; iToken++){
162470 Fts3PhraseToken *pToken = &p->aToken[iToken];
162471 assert( pToken->pDeferred==0 || pToken->pSegcsr==0 );
162472
162473 if( pToken->pSegcsr ){
162474 int nThis = 0;
162475 char *pThis = 0;
162476 rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis);
162477 if( rc==SQLITE_OK ){
162478 rc = fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
162479 }
162480 }
162481 assert( pToken->pSegcsr==0 );
162482 }
162483
162484 return rc;
162485}
162486
162487#ifndef SQLITE_DISABLE_FTS4_DEFERRED
162488/*
162489** This function is called on each phrase after the position lists for
162490** any deferred tokens have been loaded into memory. It updates the phrases
162491** current position list to include only those positions that are really
162492** instances of the phrase (after considering deferred tokens). If this
162493** means that the phrase does not appear in the current row, doclist.pList
162494** and doclist.nList are both zeroed.
162495**
162496** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
162497*/
162498static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
162499 int iToken; /* Used to iterate through phrase tokens */
162500 char *aPoslist = 0; /* Position list for deferred tokens */
162501 int nPoslist = 0; /* Number of bytes in aPoslist */
162502 int iPrev = -1; /* Token number of previous deferred token */
162503
162504 assert( pPhrase->doclist.bFreeList==0 );
162505
162506 for(iToken=0; iToken<pPhrase->nToken; iToken++){
162507 Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
162508 Fts3DeferredToken *pDeferred = pToken->pDeferred;
162509
162510 if( pDeferred ){
162511 char *pList;
162512 int nList;
162513 int rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList);
162514 if( rc!=SQLITE_OK ) return rc;
162515
162516 if( pList==0 ){
162517 sqlite3_free(aPoslist);
162518 pPhrase->doclist.pList = 0;
162519 pPhrase->doclist.nList = 0;
162520 return SQLITE_OK;
162521
162522 }else if( aPoslist==0 ){
162523 aPoslist = pList;
162524 nPoslist = nList;
162525
162526 }else{
162527 char *aOut = pList;
162528 char *p1 = aPoslist;
162529 char *p2 = aOut;
162530
162531 assert( iPrev>=0 );
162532 fts3PoslistPhraseMerge(&aOut, iToken-iPrev, 0, 1, &p1, &p2);
162533 sqlite3_free(aPoslist);
162534 aPoslist = pList;
162535 nPoslist = (int)(aOut - aPoslist);
162536 if( nPoslist==0 ){
162537 sqlite3_free(aPoslist);
162538 pPhrase->doclist.pList = 0;
162539 pPhrase->doclist.nList = 0;
162540 return SQLITE_OK;
162541 }
162542 }
162543 iPrev = iToken;
162544 }
162545 }
162546
162547 if( iPrev>=0 ){
162548 int nMaxUndeferred = pPhrase->iDoclistToken;
162549 if( nMaxUndeferred<0 ){
162550 pPhrase->doclist.pList = aPoslist;
162551 pPhrase->doclist.nList = nPoslist;
162552 pPhrase->doclist.iDocid = pCsr->iPrevId;
162553 pPhrase->doclist.bFreeList = 1;
162554 }else{
162555 int nDistance;
162556 char *p1;
162557 char *p2;
162558 char *aOut;
162559
162560 if( nMaxUndeferred>iPrev ){
162561 p1 = aPoslist;
162562 p2 = pPhrase->doclist.pList;
162563 nDistance = nMaxUndeferred - iPrev;
162564 }else{
162565 p1 = pPhrase->doclist.pList;
162566 p2 = aPoslist;
162567 nDistance = iPrev - nMaxUndeferred;
162568 }
162569
162570 aOut = (char *)sqlite3_malloc(nPoslist+8);
162571 if( !aOut ){
162572 sqlite3_free(aPoslist);
162573 return SQLITE_NOMEM;
162574 }
162575
162576 pPhrase->doclist.pList = aOut;
162577 if( fts3PoslistPhraseMerge(&aOut, nDistance, 0, 1, &p1, &p2) ){
162578 pPhrase->doclist.bFreeList = 1;
162579 pPhrase->doclist.nList = (int)(aOut - pPhrase->doclist.pList);
162580 }else{
162581 sqlite3_free(aOut);
162582 pPhrase->doclist.pList = 0;
162583 pPhrase->doclist.nList = 0;
162584 }
162585 sqlite3_free(aPoslist);
162586 }
162587 }
162588
162589 return SQLITE_OK;
162590}
162591#endif /* SQLITE_DISABLE_FTS4_DEFERRED */
162592
162593/*
162594** Maximum number of tokens a phrase may have to be considered for the
162595** incremental doclists strategy.
162596*/
162597#define MAX_INCR_PHRASE_TOKENS 4
162598
162599/*
162600** This function is called for each Fts3Phrase in a full-text query
162601** expression to initialize the mechanism for returning rows. Once this
162602** function has been called successfully on an Fts3Phrase, it may be
162603** used with fts3EvalPhraseNext() to iterate through the matching docids.
162604**
162605** If parameter bOptOk is true, then the phrase may (or may not) use the
162606** incremental loading strategy. Otherwise, the entire doclist is loaded into
162607** memory within this call.
162608**
162609** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
162610*/
162611static int fts3EvalPhraseStart(Fts3Cursor *pCsr, int bOptOk, Fts3Phrase *p){
162612 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
162613 int rc = SQLITE_OK; /* Error code */
162614 int i;
162615
162616 /* Determine if doclists may be loaded from disk incrementally. This is
162617 ** possible if the bOptOk argument is true, the FTS doclists will be
162618 ** scanned in forward order, and the phrase consists of
162619 ** MAX_INCR_PHRASE_TOKENS or fewer tokens, none of which are are "^first"
162620 ** tokens or prefix tokens that cannot use a prefix-index. */
162621 int bHaveIncr = 0;
162622 int bIncrOk = (bOptOk
162623 && pCsr->bDesc==pTab->bDescIdx
162624 && p->nToken<=MAX_INCR_PHRASE_TOKENS && p->nToken>0
162625#ifdef SQLITE_TEST
162626 && pTab->bNoIncrDoclist==0
162627#endif
162628 );
162629 for(i=0; bIncrOk==1 && i<p->nToken; i++){
162630 Fts3PhraseToken *pToken = &p->aToken[i];
162631 if( pToken->bFirst || (pToken->pSegcsr!=0 && !pToken->pSegcsr->bLookup) ){
162632 bIncrOk = 0;
162633 }
162634 if( pToken->pSegcsr ) bHaveIncr = 1;
162635 }
162636
162637 if( bIncrOk && bHaveIncr ){
162638 /* Use the incremental approach. */
162639 int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn);
162640 for(i=0; rc==SQLITE_OK && i<p->nToken; i++){
162641 Fts3PhraseToken *pToken = &p->aToken[i];
162642 Fts3MultiSegReader *pSegcsr = pToken->pSegcsr;
162643 if( pSegcsr ){
162644 rc = sqlite3Fts3MsrIncrStart(pTab, pSegcsr, iCol, pToken->z, pToken->n);
162645 }
162646 }
162647 p->bIncr = 1;
162648 }else{
162649 /* Load the full doclist for the phrase into memory. */
162650 rc = fts3EvalPhraseLoad(pCsr, p);
162651 p->bIncr = 0;
162652 }
162653
162654 assert( rc!=SQLITE_OK || p->nToken<1 || p->aToken[0].pSegcsr==0 || p->bIncr );
162655 return rc;
162656}
162657
162658/*
162659** This function is used to iterate backwards (from the end to start)
162660** through doclists. It is used by this module to iterate through phrase
162661** doclists in reverse and by the fts3_write.c module to iterate through
162662** pending-terms lists when writing to databases with "order=desc".
162663**
162664** The doclist may be sorted in ascending (parameter bDescIdx==0) or
162665** descending (parameter bDescIdx==1) order of docid. Regardless, this
162666** function iterates from the end of the doclist to the beginning.
162667*/
162668SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(
162669 int bDescIdx, /* True if the doclist is desc */
162670 char *aDoclist, /* Pointer to entire doclist */
162671 int nDoclist, /* Length of aDoclist in bytes */
162672 char **ppIter, /* IN/OUT: Iterator pointer */
162673 sqlite3_int64 *piDocid, /* IN/OUT: Docid pointer */
162674 int *pnList, /* OUT: List length pointer */
162675 u8 *pbEof /* OUT: End-of-file flag */
162676){
162677 char *p = *ppIter;
162678
162679 assert( nDoclist>0 );
162680 assert( *pbEof==0 );
162681 assert( p || *piDocid==0 );
162682 assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) );
162683
162684 if( p==0 ){
162685 sqlite3_int64 iDocid = 0;
162686 char *pNext = 0;
162687 char *pDocid = aDoclist;
162688 char *pEnd = &aDoclist[nDoclist];
162689 int iMul = 1;
162690
162691 while( pDocid<pEnd ){
162692 sqlite3_int64 iDelta;
162693 pDocid += sqlite3Fts3GetVarint(pDocid, &iDelta);
162694 iDocid += (iMul * iDelta);
162695 pNext = pDocid;
162696 fts3PoslistCopy(0, &pDocid);
162697 while( pDocid<pEnd && *pDocid==0 ) pDocid++;
162698 iMul = (bDescIdx ? -1 : 1);
162699 }
162700
162701 *pnList = (int)(pEnd - pNext);
162702 *ppIter = pNext;
162703 *piDocid = iDocid;
162704 }else{
162705 int iMul = (bDescIdx ? -1 : 1);
162706 sqlite3_int64 iDelta;
162707 fts3GetReverseVarint(&p, aDoclist, &iDelta);
162708 *piDocid -= (iMul * iDelta);
162709
162710 if( p==aDoclist ){
162711 *pbEof = 1;
162712 }else{
162713 char *pSave = p;
162714 fts3ReversePoslist(aDoclist, &p);
162715 *pnList = (int)(pSave - p);
162716 }
162717 *ppIter = p;
162718 }
162719}
162720
162721/*
162722** Iterate forwards through a doclist.
162723*/
162724SQLITE_PRIVATE void sqlite3Fts3DoclistNext(
162725 int bDescIdx, /* True if the doclist is desc */
162726 char *aDoclist, /* Pointer to entire doclist */
162727 int nDoclist, /* Length of aDoclist in bytes */
162728 char **ppIter, /* IN/OUT: Iterator pointer */
162729 sqlite3_int64 *piDocid, /* IN/OUT: Docid pointer */
162730 u8 *pbEof /* OUT: End-of-file flag */
162731){
162732 char *p = *ppIter;
162733
162734 assert( nDoclist>0 );
162735 assert( *pbEof==0 );
162736 assert( p || *piDocid==0 );
162737 assert( !p || (p>=aDoclist && p<=&aDoclist[nDoclist]) );
162738
162739 if( p==0 ){
162740 p = aDoclist;
162741 p += sqlite3Fts3GetVarint(p, piDocid);
162742 }else{
162743 fts3PoslistCopy(0, &p);
162744 while( p<&aDoclist[nDoclist] && *p==0 ) p++;
162745 if( p>=&aDoclist[nDoclist] ){
162746 *pbEof = 1;
162747 }else{
162748 sqlite3_int64 iVar;
162749 p += sqlite3Fts3GetVarint(p, &iVar);
162750 *piDocid += ((bDescIdx ? -1 : 1) * iVar);
162751 }
162752 }
162753
162754 *ppIter = p;
162755}
162756
162757/*
162758** Advance the iterator pDL to the next entry in pDL->aAll/nAll. Set *pbEof
162759** to true if EOF is reached.
162760*/
162761static void fts3EvalDlPhraseNext(
162762 Fts3Table *pTab,
162763 Fts3Doclist *pDL,
162764 u8 *pbEof
162765){
162766 char *pIter; /* Used to iterate through aAll */
162767 char *pEnd = &pDL->aAll[pDL->nAll]; /* 1 byte past end of aAll */
162768
162769 if( pDL->pNextDocid ){
162770 pIter = pDL->pNextDocid;
162771 }else{
162772 pIter = pDL->aAll;
162773 }
162774
162775 if( pIter>=pEnd ){
162776 /* We have already reached the end of this doclist. EOF. */
162777 *pbEof = 1;
162778 }else{
162779 sqlite3_int64 iDelta;
162780 pIter += sqlite3Fts3GetVarint(pIter, &iDelta);
162781 if( pTab->bDescIdx==0 || pDL->pNextDocid==0 ){
162782 pDL->iDocid += iDelta;
162783 }else{
162784 pDL->iDocid -= iDelta;
162785 }
162786 pDL->pList = pIter;
162787 fts3PoslistCopy(0, &pIter);
162788 pDL->nList = (int)(pIter - pDL->pList);
162789
162790 /* pIter now points just past the 0x00 that terminates the position-
162791 ** list for document pDL->iDocid. However, if this position-list was
162792 ** edited in place by fts3EvalNearTrim(), then pIter may not actually
162793 ** point to the start of the next docid value. The following line deals
162794 ** with this case by advancing pIter past the zero-padding added by
162795 ** fts3EvalNearTrim(). */
162796 while( pIter<pEnd && *pIter==0 ) pIter++;
162797
162798 pDL->pNextDocid = pIter;
162799 assert( pIter>=&pDL->aAll[pDL->nAll] || *pIter );
162800 *pbEof = 0;
162801 }
162802}
162803
162804/*
162805** Helper type used by fts3EvalIncrPhraseNext() and incrPhraseTokenNext().
162806*/
162807typedef struct TokenDoclist TokenDoclist;
162808struct TokenDoclist {
162809 int bIgnore;
162810 sqlite3_int64 iDocid;
162811 char *pList;
162812 int nList;
162813};
162814
162815/*
162816** Token pToken is an incrementally loaded token that is part of a
162817** multi-token phrase. Advance it to the next matching document in the
162818** database and populate output variable *p with the details of the new
162819** entry. Or, if the iterator has reached EOF, set *pbEof to true.
162820**
162821** If an error occurs, return an SQLite error code. Otherwise, return
162822** SQLITE_OK.
162823*/
162824static int incrPhraseTokenNext(
162825 Fts3Table *pTab, /* Virtual table handle */
162826 Fts3Phrase *pPhrase, /* Phrase to advance token of */
162827 int iToken, /* Specific token to advance */
162828 TokenDoclist *p, /* OUT: Docid and doclist for new entry */
162829 u8 *pbEof /* OUT: True if iterator is at EOF */
162830){
162831 int rc = SQLITE_OK;
162832
162833 if( pPhrase->iDoclistToken==iToken ){
162834 assert( p->bIgnore==0 );
162835 assert( pPhrase->aToken[iToken].pSegcsr==0 );
162836 fts3EvalDlPhraseNext(pTab, &pPhrase->doclist, pbEof);
162837 p->pList = pPhrase->doclist.pList;
162838 p->nList = pPhrase->doclist.nList;
162839 p->iDocid = pPhrase->doclist.iDocid;
162840 }else{
162841 Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
162842 assert( pToken->pDeferred==0 );
162843 assert( pToken->pSegcsr || pPhrase->iDoclistToken>=0 );
162844 if( pToken->pSegcsr ){
162845 assert( p->bIgnore==0 );
162846 rc = sqlite3Fts3MsrIncrNext(
162847 pTab, pToken->pSegcsr, &p->iDocid, &p->pList, &p->nList
162848 );
162849 if( p->pList==0 ) *pbEof = 1;
162850 }else{
162851 p->bIgnore = 1;
162852 }
162853 }
162854
162855 return rc;
162856}
162857
162858
162859/*
162860** The phrase iterator passed as the second argument:
162861**
162862** * features at least one token that uses an incremental doclist, and
162863**
162864** * does not contain any deferred tokens.
162865**
162866** Advance it to the next matching documnent in the database and populate
162867** the Fts3Doclist.pList and nList fields.
162868**
162869** If there is no "next" entry and no error occurs, then *pbEof is set to
162870** 1 before returning. Otherwise, if no error occurs and the iterator is
162871** successfully advanced, *pbEof is set to 0.
162872**
162873** If an error occurs, return an SQLite error code. Otherwise, return
162874** SQLITE_OK.
162875*/
162876static int fts3EvalIncrPhraseNext(
162877 Fts3Cursor *pCsr, /* FTS Cursor handle */
162878 Fts3Phrase *p, /* Phrase object to advance to next docid */
162879 u8 *pbEof /* OUT: Set to 1 if EOF */
162880){
162881 int rc = SQLITE_OK;
162882 Fts3Doclist *pDL = &p->doclist;
162883 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
162884 u8 bEof = 0;
162885
162886 /* This is only called if it is guaranteed that the phrase has at least
162887 ** one incremental token. In which case the bIncr flag is set. */
162888 assert( p->bIncr==1 );
162889
162890 if( p->nToken==1 ){
162891 rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr,
162892 &pDL->iDocid, &pDL->pList, &pDL->nList
162893 );
162894 if( pDL->pList==0 ) bEof = 1;
162895 }else{
162896 int bDescDoclist = pCsr->bDesc;
162897 struct TokenDoclist a[MAX_INCR_PHRASE_TOKENS];
162898
162899 memset(a, 0, sizeof(a));
162900 assert( p->nToken<=MAX_INCR_PHRASE_TOKENS );
162901 assert( p->iDoclistToken<MAX_INCR_PHRASE_TOKENS );
162902
162903 while( bEof==0 ){
162904 int bMaxSet = 0;
162905 sqlite3_int64 iMax = 0; /* Largest docid for all iterators */
162906 int i; /* Used to iterate through tokens */
162907
162908 /* Advance the iterator for each token in the phrase once. */
162909 for(i=0; rc==SQLITE_OK && i<p->nToken && bEof==0; i++){
162910 rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
162911 if( a[i].bIgnore==0 && (bMaxSet==0 || DOCID_CMP(iMax, a[i].iDocid)<0) ){
162912 iMax = a[i].iDocid;
162913 bMaxSet = 1;
162914 }
162915 }
162916 assert( rc!=SQLITE_OK || (p->nToken>=1 && a[p->nToken-1].bIgnore==0) );
162917 assert( rc!=SQLITE_OK || bMaxSet );
162918
162919 /* Keep advancing iterators until they all point to the same document */
162920 for(i=0; i<p->nToken; i++){
162921 while( rc==SQLITE_OK && bEof==0
162922 && a[i].bIgnore==0 && DOCID_CMP(a[i].iDocid, iMax)<0
162923 ){
162924 rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
162925 if( DOCID_CMP(a[i].iDocid, iMax)>0 ){
162926 iMax = a[i].iDocid;
162927 i = 0;
162928 }
162929 }
162930 }
162931
162932 /* Check if the current entries really are a phrase match */
162933 if( bEof==0 ){
162934 int nList = 0;
162935 int nByte = a[p->nToken-1].nList;
162936 char *aDoclist = sqlite3_malloc(nByte+1);
162937 if( !aDoclist ) return SQLITE_NOMEM;
162938 memcpy(aDoclist, a[p->nToken-1].pList, nByte+1);
162939
162940 for(i=0; i<(p->nToken-1); i++){
162941 if( a[i].bIgnore==0 ){
162942 char *pL = a[i].pList;
162943 char *pR = aDoclist;
162944 char *pOut = aDoclist;
162945 int nDist = p->nToken-1-i;
162946 int res = fts3PoslistPhraseMerge(&pOut, nDist, 0, 1, &pL, &pR);
162947 if( res==0 ) break;
162948 nList = (int)(pOut - aDoclist);
162949 }
162950 }
162951 if( i==(p->nToken-1) ){
162952 pDL->iDocid = iMax;
162953 pDL->pList = aDoclist;
162954 pDL->nList = nList;
162955 pDL->bFreeList = 1;
162956 break;
162957 }
162958 sqlite3_free(aDoclist);
162959 }
162960 }
162961 }
162962
162963 *pbEof = bEof;
162964 return rc;
162965}
162966
162967/*
162968** Attempt to move the phrase iterator to point to the next matching docid.
162969** If an error occurs, return an SQLite error code. Otherwise, return
162970** SQLITE_OK.
162971**
162972** If there is no "next" entry and no error occurs, then *pbEof is set to
162973** 1 before returning. Otherwise, if no error occurs and the iterator is
162974** successfully advanced, *pbEof is set to 0.
162975*/
162976static int fts3EvalPhraseNext(
162977 Fts3Cursor *pCsr, /* FTS Cursor handle */
162978 Fts3Phrase *p, /* Phrase object to advance to next docid */
162979 u8 *pbEof /* OUT: Set to 1 if EOF */
162980){
162981 int rc = SQLITE_OK;
162982 Fts3Doclist *pDL = &p->doclist;
162983 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
162984
162985 if( p->bIncr ){
162986 rc = fts3EvalIncrPhraseNext(pCsr, p, pbEof);
162987 }else if( pCsr->bDesc!=pTab->bDescIdx && pDL->nAll ){
162988 sqlite3Fts3DoclistPrev(pTab->bDescIdx, pDL->aAll, pDL->nAll,
162989 &pDL->pNextDocid, &pDL->iDocid, &pDL->nList, pbEof
162990 );
162991 pDL->pList = pDL->pNextDocid;
162992 }else{
162993 fts3EvalDlPhraseNext(pTab, pDL, pbEof);
162994 }
162995
162996 return rc;
162997}
162998
162999/*
163000**
163001** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
163002** Otherwise, fts3EvalPhraseStart() is called on all phrases within the
163003** expression. Also the Fts3Expr.bDeferred variable is set to true for any
163004** expressions for which all descendent tokens are deferred.
163005**
163006** If parameter bOptOk is zero, then it is guaranteed that the
163007** Fts3Phrase.doclist.aAll/nAll variables contain the entire doclist for
163008** each phrase in the expression (subject to deferred token processing).
163009** Or, if bOptOk is non-zero, then one or more tokens within the expression
163010** may be loaded incrementally, meaning doclist.aAll/nAll is not available.
163011**
163012** If an error occurs within this function, *pRc is set to an SQLite error
163013** code before returning.
163014*/
163015static void fts3EvalStartReaders(
163016 Fts3Cursor *pCsr, /* FTS Cursor handle */
163017 Fts3Expr *pExpr, /* Expression to initialize phrases in */
163018 int *pRc /* IN/OUT: Error code */
163019){
163020 if( pExpr && SQLITE_OK==*pRc ){
163021 if( pExpr->eType==FTSQUERY_PHRASE ){
163022 int nToken = pExpr->pPhrase->nToken;
163023 if( nToken ){
163024 int i;
163025 for(i=0; i<nToken; i++){
163026 if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break;
163027 }
163028 pExpr->bDeferred = (i==nToken);
163029 }
163030 *pRc = fts3EvalPhraseStart(pCsr, 1, pExpr->pPhrase);
163031 }else{
163032 fts3EvalStartReaders(pCsr, pExpr->pLeft, pRc);
163033 fts3EvalStartReaders(pCsr, pExpr->pRight, pRc);
163034 pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred);
163035 }
163036 }
163037}
163038
163039/*
163040** An array of the following structures is assembled as part of the process
163041** of selecting tokens to defer before the query starts executing (as part
163042** of the xFilter() method). There is one element in the array for each
163043** token in the FTS expression.
163044**
163045** Tokens are divided into AND/NEAR clusters. All tokens in a cluster belong
163046** to phrases that are connected only by AND and NEAR operators (not OR or
163047** NOT). When determining tokens to defer, each AND/NEAR cluster is considered
163048** separately. The root of a tokens AND/NEAR cluster is stored in
163049** Fts3TokenAndCost.pRoot.
163050*/
163051typedef struct Fts3TokenAndCost Fts3TokenAndCost;
163052struct Fts3TokenAndCost {
163053 Fts3Phrase *pPhrase; /* The phrase the token belongs to */
163054 int iToken; /* Position of token in phrase */
163055 Fts3PhraseToken *pToken; /* The token itself */
163056 Fts3Expr *pRoot; /* Root of NEAR/AND cluster */
163057 int nOvfl; /* Number of overflow pages to load doclist */
163058 int iCol; /* The column the token must match */
163059};
163060
163061/*
163062** This function is used to populate an allocated Fts3TokenAndCost array.
163063**
163064** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
163065** Otherwise, if an error occurs during execution, *pRc is set to an
163066** SQLite error code.
163067*/
163068static void fts3EvalTokenCosts(
163069 Fts3Cursor *pCsr, /* FTS Cursor handle */
163070 Fts3Expr *pRoot, /* Root of current AND/NEAR cluster */
163071 Fts3Expr *pExpr, /* Expression to consider */
163072 Fts3TokenAndCost **ppTC, /* Write new entries to *(*ppTC)++ */
163073 Fts3Expr ***ppOr, /* Write new OR root to *(*ppOr)++ */
163074 int *pRc /* IN/OUT: Error code */
163075){
163076 if( *pRc==SQLITE_OK ){
163077 if( pExpr->eType==FTSQUERY_PHRASE ){
163078 Fts3Phrase *pPhrase = pExpr->pPhrase;
163079 int i;
163080 for(i=0; *pRc==SQLITE_OK && i<pPhrase->nToken; i++){
163081 Fts3TokenAndCost *pTC = (*ppTC)++;
163082 pTC->pPhrase = pPhrase;
163083 pTC->iToken = i;
163084 pTC->pRoot = pRoot;
163085 pTC->pToken = &pPhrase->aToken[i];
163086 pTC->iCol = pPhrase->iColumn;
163087 *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl);
163088 }
163089 }else if( pExpr->eType!=FTSQUERY_NOT ){
163090 assert( pExpr->eType==FTSQUERY_OR
163091 || pExpr->eType==FTSQUERY_AND
163092 || pExpr->eType==FTSQUERY_NEAR
163093 );
163094 assert( pExpr->pLeft && pExpr->pRight );
163095 if( pExpr->eType==FTSQUERY_OR ){
163096 pRoot = pExpr->pLeft;
163097 **ppOr = pRoot;
163098 (*ppOr)++;
163099 }
163100 fts3EvalTokenCosts(pCsr, pRoot, pExpr->pLeft, ppTC, ppOr, pRc);
163101 if( pExpr->eType==FTSQUERY_OR ){
163102 pRoot = pExpr->pRight;
163103 **ppOr = pRoot;
163104 (*ppOr)++;
163105 }
163106 fts3EvalTokenCosts(pCsr, pRoot, pExpr->pRight, ppTC, ppOr, pRc);
163107 }
163108 }
163109}
163110
163111/*
163112** Determine the average document (row) size in pages. If successful,
163113** write this value to *pnPage and return SQLITE_OK. Otherwise, return
163114** an SQLite error code.
163115**
163116** The average document size in pages is calculated by first calculating
163117** determining the average size in bytes, B. If B is less than the amount
163118** of data that will fit on a single leaf page of an intkey table in
163119** this database, then the average docsize is 1. Otherwise, it is 1 plus
163120** the number of overflow pages consumed by a record B bytes in size.
163121*/
163122static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){
163123 int rc = SQLITE_OK;
163124 if( pCsr->nRowAvg==0 ){
163125 /* The average document size, which is required to calculate the cost
163126 ** of each doclist, has not yet been determined. Read the required
163127 ** data from the %_stat table to calculate it.
163128 **
163129 ** Entry 0 of the %_stat table is a blob containing (nCol+1) FTS3
163130 ** varints, where nCol is the number of columns in the FTS3 table.
163131 ** The first varint is the number of documents currently stored in
163132 ** the table. The following nCol varints contain the total amount of
163133 ** data stored in all rows of each column of the table, from left
163134 ** to right.
163135 */
163136 Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
163137 sqlite3_stmt *pStmt;
163138 sqlite3_int64 nDoc = 0;
163139 sqlite3_int64 nByte = 0;
163140 const char *pEnd;
163141 const char *a;
163142
163143 rc = sqlite3Fts3SelectDoctotal(p, &pStmt);
163144 if( rc!=SQLITE_OK ) return rc;
163145 a = sqlite3_column_blob(pStmt, 0);
163146 assert( a );
163147
163148 pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
163149 a += sqlite3Fts3GetVarint(a, &nDoc);
163150 while( a<pEnd ){
163151 a += sqlite3Fts3GetVarint(a, &nByte);
163152 }
163153 if( nDoc==0 || nByte==0 ){
163154 sqlite3_reset(pStmt);
163155 return FTS_CORRUPT_VTAB;
163156 }
163157
163158 pCsr->nDoc = nDoc;
163159 pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
163160 assert( pCsr->nRowAvg>0 );
163161 rc = sqlite3_reset(pStmt);
163162 }
163163
163164 *pnPage = pCsr->nRowAvg;
163165 return rc;
163166}
163167
163168/*
163169** This function is called to select the tokens (if any) that will be
163170** deferred. The array aTC[] has already been populated when this is
163171** called.
163172**
163173** This function is called once for each AND/NEAR cluster in the
163174** expression. Each invocation determines which tokens to defer within
163175** the cluster with root node pRoot. See comments above the definition
163176** of struct Fts3TokenAndCost for more details.
163177**
163178** If no error occurs, SQLITE_OK is returned and sqlite3Fts3DeferToken()
163179** called on each token to defer. Otherwise, an SQLite error code is
163180** returned.
163181*/
163182static int fts3EvalSelectDeferred(
163183 Fts3Cursor *pCsr, /* FTS Cursor handle */
163184 Fts3Expr *pRoot, /* Consider tokens with this root node */
163185 Fts3TokenAndCost *aTC, /* Array of expression tokens and costs */
163186 int nTC /* Number of entries in aTC[] */
163187){
163188 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
163189 int nDocSize = 0; /* Number of pages per doc loaded */
163190 int rc = SQLITE_OK; /* Return code */
163191 int ii; /* Iterator variable for various purposes */
163192 int nOvfl = 0; /* Total overflow pages used by doclists */
163193 int nToken = 0; /* Total number of tokens in cluster */
163194
163195 int nMinEst = 0; /* The minimum count for any phrase so far. */
163196 int nLoad4 = 1; /* (Phrases that will be loaded)^4. */
163197
163198 /* Tokens are never deferred for FTS tables created using the content=xxx
163199 ** option. The reason being that it is not guaranteed that the content
163200 ** table actually contains the same data as the index. To prevent this from
163201 ** causing any problems, the deferred token optimization is completely
163202 ** disabled for content=xxx tables. */
163203 if( pTab->zContentTbl ){
163204 return SQLITE_OK;
163205 }
163206
163207 /* Count the tokens in this AND/NEAR cluster. If none of the doclists
163208 ** associated with the tokens spill onto overflow pages, or if there is
163209 ** only 1 token, exit early. No tokens to defer in this case. */
163210 for(ii=0; ii<nTC; ii++){
163211 if( aTC[ii].pRoot==pRoot ){
163212 nOvfl += aTC[ii].nOvfl;
163213 nToken++;
163214 }
163215 }
163216 if( nOvfl==0 || nToken<2 ) return SQLITE_OK;
163217
163218 /* Obtain the average docsize (in pages). */
163219 rc = fts3EvalAverageDocsize(pCsr, &nDocSize);
163220 assert( rc!=SQLITE_OK || nDocSize>0 );
163221
163222
163223 /* Iterate through all tokens in this AND/NEAR cluster, in ascending order
163224 ** of the number of overflow pages that will be loaded by the pager layer
163225 ** to retrieve the entire doclist for the token from the full-text index.
163226 ** Load the doclists for tokens that are either:
163227 **
163228 ** a. The cheapest token in the entire query (i.e. the one visited by the
163229 ** first iteration of this loop), or
163230 **
163231 ** b. Part of a multi-token phrase.
163232 **
163233 ** After each token doclist is loaded, merge it with the others from the
163234 ** same phrase and count the number of documents that the merged doclist
163235 ** contains. Set variable "nMinEst" to the smallest number of documents in
163236 ** any phrase doclist for which 1 or more token doclists have been loaded.
163237 ** Let nOther be the number of other phrases for which it is certain that
163238 ** one or more tokens will not be deferred.
163239 **
163240 ** Then, for each token, defer it if loading the doclist would result in
163241 ** loading N or more overflow pages into memory, where N is computed as:
163242 **
163243 ** (nMinEst + 4^nOther - 1) / (4^nOther)
163244 */
163245 for(ii=0; ii<nToken && rc==SQLITE_OK; ii++){
163246 int iTC; /* Used to iterate through aTC[] array. */
163247 Fts3TokenAndCost *pTC = 0; /* Set to cheapest remaining token. */
163248
163249 /* Set pTC to point to the cheapest remaining token. */
163250 for(iTC=0; iTC<nTC; iTC++){
163251 if( aTC[iTC].pToken && aTC[iTC].pRoot==pRoot
163252 && (!pTC || aTC[iTC].nOvfl<pTC->nOvfl)
163253 ){
163254 pTC = &aTC[iTC];
163255 }
163256 }
163257 assert( pTC );
163258
163259 if( ii && pTC->nOvfl>=((nMinEst+(nLoad4/4)-1)/(nLoad4/4))*nDocSize ){
163260 /* The number of overflow pages to load for this (and therefore all
163261 ** subsequent) tokens is greater than the estimated number of pages
163262 ** that will be loaded if all subsequent tokens are deferred.
163263 */
163264 Fts3PhraseToken *pToken = pTC->pToken;
163265 rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol);
163266 fts3SegReaderCursorFree(pToken->pSegcsr);
163267 pToken->pSegcsr = 0;
163268 }else{
163269 /* Set nLoad4 to the value of (4^nOther) for the next iteration of the
163270 ** for-loop. Except, limit the value to 2^24 to prevent it from
163271 ** overflowing the 32-bit integer it is stored in. */
163272 if( ii<12 ) nLoad4 = nLoad4*4;
163273
163274 if( ii==0 || (pTC->pPhrase->nToken>1 && ii!=nToken-1) ){
163275 /* Either this is the cheapest token in the entire query, or it is
163276 ** part of a multi-token phrase. Either way, the entire doclist will
163277 ** (eventually) be loaded into memory. It may as well be now. */
163278 Fts3PhraseToken *pToken = pTC->pToken;
163279 int nList = 0;
163280 char *pList = 0;
163281 rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList);
163282 assert( rc==SQLITE_OK || pList==0 );
163283 if( rc==SQLITE_OK ){
163284 rc = fts3EvalPhraseMergeToken(
163285 pTab, pTC->pPhrase, pTC->iToken,pList,nList
163286 );
163287 }
163288 if( rc==SQLITE_OK ){
163289 int nCount;
163290 nCount = fts3DoclistCountDocids(
163291 pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll
163292 );
163293 if( ii==0 || nCount<nMinEst ) nMinEst = nCount;
163294 }
163295 }
163296 }
163297 pTC->pToken = 0;
163298 }
163299
163300 return rc;
163301}
163302
163303/*
163304** This function is called from within the xFilter method. It initializes
163305** the full-text query currently stored in pCsr->pExpr. To iterate through
163306** the results of a query, the caller does:
163307**
163308** fts3EvalStart(pCsr);
163309** while( 1 ){
163310** fts3EvalNext(pCsr);
163311** if( pCsr->bEof ) break;
163312** ... return row pCsr->iPrevId to the caller ...
163313** }
163314*/
163315static int fts3EvalStart(Fts3Cursor *pCsr){
163316 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
163317 int rc = SQLITE_OK;
163318 int nToken = 0;
163319 int nOr = 0;
163320
163321 /* Allocate a MultiSegReader for each token in the expression. */
163322 fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc);
163323
163324 /* Determine which, if any, tokens in the expression should be deferred. */
163325#ifndef SQLITE_DISABLE_FTS4_DEFERRED
163326 if( rc==SQLITE_OK && nToken>1 && pTab->bFts4 ){
163327 Fts3TokenAndCost *aTC;
163328 Fts3Expr **apOr;
163329 aTC = (Fts3TokenAndCost *)sqlite3_malloc(
163330 sizeof(Fts3TokenAndCost) * nToken
163331 + sizeof(Fts3Expr *) * nOr * 2
163332 );
163333 apOr = (Fts3Expr **)&aTC[nToken];
163334
163335 if( !aTC ){
163336 rc = SQLITE_NOMEM;
163337 }else{
163338 int ii;
163339 Fts3TokenAndCost *pTC = aTC;
163340 Fts3Expr **ppOr = apOr;
163341
163342 fts3EvalTokenCosts(pCsr, 0, pCsr->pExpr, &pTC, &ppOr, &rc);
163343 nToken = (int)(pTC-aTC);
163344 nOr = (int)(ppOr-apOr);
163345
163346 if( rc==SQLITE_OK ){
163347 rc = fts3EvalSelectDeferred(pCsr, 0, aTC, nToken);
163348 for(ii=0; rc==SQLITE_OK && ii<nOr; ii++){
163349 rc = fts3EvalSelectDeferred(pCsr, apOr[ii], aTC, nToken);
163350 }
163351 }
163352
163353 sqlite3_free(aTC);
163354 }
163355 }
163356#endif
163357
163358 fts3EvalStartReaders(pCsr, pCsr->pExpr, &rc);
163359 return rc;
163360}
163361
163362/*
163363** Invalidate the current position list for phrase pPhrase.
163364*/
163365static void fts3EvalInvalidatePoslist(Fts3Phrase *pPhrase){
163366 if( pPhrase->doclist.bFreeList ){
163367 sqlite3_free(pPhrase->doclist.pList);
163368 }
163369 pPhrase->doclist.pList = 0;
163370 pPhrase->doclist.nList = 0;
163371 pPhrase->doclist.bFreeList = 0;
163372}
163373
163374/*
163375** This function is called to edit the position list associated with
163376** the phrase object passed as the fifth argument according to a NEAR
163377** condition. For example:
163378**
163379** abc NEAR/5 "def ghi"
163380**
163381** Parameter nNear is passed the NEAR distance of the expression (5 in
163382** the example above). When this function is called, *paPoslist points to
163383** the position list, and *pnToken is the number of phrase tokens in, the
163384** phrase on the other side of the NEAR operator to pPhrase. For example,
163385** if pPhrase refers to the "def ghi" phrase, then *paPoslist points to
163386** the position list associated with phrase "abc".
163387**
163388** All positions in the pPhrase position list that are not sufficiently
163389** close to a position in the *paPoslist position list are removed. If this
163390** leaves 0 positions, zero is returned. Otherwise, non-zero.
163391**
163392** Before returning, *paPoslist is set to point to the position lsit
163393** associated with pPhrase. And *pnToken is set to the number of tokens in
163394** pPhrase.
163395*/
163396static int fts3EvalNearTrim(
163397 int nNear, /* NEAR distance. As in "NEAR/nNear". */
163398 char *aTmp, /* Temporary space to use */
163399 char **paPoslist, /* IN/OUT: Position list */
163400 int *pnToken, /* IN/OUT: Tokens in phrase of *paPoslist */
163401 Fts3Phrase *pPhrase /* The phrase object to trim the doclist of */
163402){
163403 int nParam1 = nNear + pPhrase->nToken;
163404 int nParam2 = nNear + *pnToken;
163405 int nNew;
163406 char *p2;
163407 char *pOut;
163408 int res;
163409
163410 assert( pPhrase->doclist.pList );
163411
163412 p2 = pOut = pPhrase->doclist.pList;
163413 res = fts3PoslistNearMerge(
163414 &pOut, aTmp, nParam1, nParam2, paPoslist, &p2
163415 );
163416 if( res ){
163417 nNew = (int)(pOut - pPhrase->doclist.pList) - 1;
163418 assert( pPhrase->doclist.pList[nNew]=='\0' );
163419 assert( nNew<=pPhrase->doclist.nList && nNew>0 );
163420 memset(&pPhrase->doclist.pList[nNew], 0, pPhrase->doclist.nList - nNew);
163421 pPhrase->doclist.nList = nNew;
163422 *paPoslist = pPhrase->doclist.pList;
163423 *pnToken = pPhrase->nToken;
163424 }
163425
163426 return res;
163427}
163428
163429/*
163430** This function is a no-op if *pRc is other than SQLITE_OK when it is called.
163431** Otherwise, it advances the expression passed as the second argument to
163432** point to the next matching row in the database. Expressions iterate through
163433** matching rows in docid order. Ascending order if Fts3Cursor.bDesc is zero,
163434** or descending if it is non-zero.
163435**
163436** If an error occurs, *pRc is set to an SQLite error code. Otherwise, if
163437** successful, the following variables in pExpr are set:
163438**
163439** Fts3Expr.bEof (non-zero if EOF - there is no next row)
163440** Fts3Expr.iDocid (valid if bEof==0. The docid of the next row)
163441**
163442** If the expression is of type FTSQUERY_PHRASE, and the expression is not
163443** at EOF, then the following variables are populated with the position list
163444** for the phrase for the visited row:
163445**
163446** FTs3Expr.pPhrase->doclist.nList (length of pList in bytes)
163447** FTs3Expr.pPhrase->doclist.pList (pointer to position list)
163448**
163449** It says above that this function advances the expression to the next
163450** matching row. This is usually true, but there are the following exceptions:
163451**
163452** 1. Deferred tokens are not taken into account. If a phrase consists
163453** entirely of deferred tokens, it is assumed to match every row in
163454** the db. In this case the position-list is not populated at all.
163455**
163456** Or, if a phrase contains one or more deferred tokens and one or
163457** more non-deferred tokens, then the expression is advanced to the
163458** next possible match, considering only non-deferred tokens. In other
163459** words, if the phrase is "A B C", and "B" is deferred, the expression
163460** is advanced to the next row that contains an instance of "A * C",
163461** where "*" may match any single token. The position list in this case
163462** is populated as for "A * C" before returning.
163463**
163464** 2. NEAR is treated as AND. If the expression is "x NEAR y", it is
163465** advanced to point to the next row that matches "x AND y".
163466**
163467** See sqlite3Fts3EvalTestDeferred() for details on testing if a row is
163468** really a match, taking into account deferred tokens and NEAR operators.
163469*/
163470static void fts3EvalNextRow(
163471 Fts3Cursor *pCsr, /* FTS Cursor handle */
163472 Fts3Expr *pExpr, /* Expr. to advance to next matching row */
163473 int *pRc /* IN/OUT: Error code */
163474){
163475 if( *pRc==SQLITE_OK ){
163476 int bDescDoclist = pCsr->bDesc; /* Used by DOCID_CMP() macro */
163477 assert( pExpr->bEof==0 );
163478 pExpr->bStart = 1;
163479
163480 switch( pExpr->eType ){
163481 case FTSQUERY_NEAR:
163482 case FTSQUERY_AND: {
163483 Fts3Expr *pLeft = pExpr->pLeft;
163484 Fts3Expr *pRight = pExpr->pRight;
163485 assert( !pLeft->bDeferred || !pRight->bDeferred );
163486
163487 if( pLeft->bDeferred ){
163488 /* LHS is entirely deferred. So we assume it matches every row.
163489 ** Advance the RHS iterator to find the next row visited. */
163490 fts3EvalNextRow(pCsr, pRight, pRc);
163491 pExpr->iDocid = pRight->iDocid;
163492 pExpr->bEof = pRight->bEof;
163493 }else if( pRight->bDeferred ){
163494 /* RHS is entirely deferred. So we assume it matches every row.
163495 ** Advance the LHS iterator to find the next row visited. */
163496 fts3EvalNextRow(pCsr, pLeft, pRc);
163497 pExpr->iDocid = pLeft->iDocid;
163498 pExpr->bEof = pLeft->bEof;
163499 }else{
163500 /* Neither the RHS or LHS are deferred. */
163501 fts3EvalNextRow(pCsr, pLeft, pRc);
163502 fts3EvalNextRow(pCsr, pRight, pRc);
163503 while( !pLeft->bEof && !pRight->bEof && *pRc==SQLITE_OK ){
163504 sqlite3_int64 iDiff = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
163505 if( iDiff==0 ) break;
163506 if( iDiff<0 ){
163507 fts3EvalNextRow(pCsr, pLeft, pRc);
163508 }else{
163509 fts3EvalNextRow(pCsr, pRight, pRc);
163510 }
163511 }
163512 pExpr->iDocid = pLeft->iDocid;
163513 pExpr->bEof = (pLeft->bEof || pRight->bEof);
163514 if( pExpr->eType==FTSQUERY_NEAR && pExpr->bEof ){
163515 assert( pRight->eType==FTSQUERY_PHRASE );
163516 if( pRight->pPhrase->doclist.aAll ){
163517 Fts3Doclist *pDl = &pRight->pPhrase->doclist;
163518 while( *pRc==SQLITE_OK && pRight->bEof==0 ){
163519 memset(pDl->pList, 0, pDl->nList);
163520 fts3EvalNextRow(pCsr, pRight, pRc);
163521 }
163522 }
163523 if( pLeft->pPhrase && pLeft->pPhrase->doclist.aAll ){
163524 Fts3Doclist *pDl = &pLeft->pPhrase->doclist;
163525 while( *pRc==SQLITE_OK && pLeft->bEof==0 ){
163526 memset(pDl->pList, 0, pDl->nList);
163527 fts3EvalNextRow(pCsr, pLeft, pRc);
163528 }
163529 }
163530 }
163531 }
163532 break;
163533 }
163534
163535 case FTSQUERY_OR: {
163536 Fts3Expr *pLeft = pExpr->pLeft;
163537 Fts3Expr *pRight = pExpr->pRight;
163538 sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
163539
163540 assert( pLeft->bStart || pLeft->iDocid==pRight->iDocid );
163541 assert( pRight->bStart || pLeft->iDocid==pRight->iDocid );
163542
163543 if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
163544 fts3EvalNextRow(pCsr, pLeft, pRc);
163545 }else if( pLeft->bEof || iCmp>0 ){
163546 fts3EvalNextRow(pCsr, pRight, pRc);
163547 }else{
163548 fts3EvalNextRow(pCsr, pLeft, pRc);
163549 fts3EvalNextRow(pCsr, pRight, pRc);
163550 }
163551
163552 pExpr->bEof = (pLeft->bEof && pRight->bEof);
163553 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
163554 if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
163555 pExpr->iDocid = pLeft->iDocid;
163556 }else{
163557 pExpr->iDocid = pRight->iDocid;
163558 }
163559
163560 break;
163561 }
163562
163563 case FTSQUERY_NOT: {
163564 Fts3Expr *pLeft = pExpr->pLeft;
163565 Fts3Expr *pRight = pExpr->pRight;
163566
163567 if( pRight->bStart==0 ){
163568 fts3EvalNextRow(pCsr, pRight, pRc);
163569 assert( *pRc!=SQLITE_OK || pRight->bStart );
163570 }
163571
163572 fts3EvalNextRow(pCsr, pLeft, pRc);
163573 if( pLeft->bEof==0 ){
163574 while( !*pRc
163575 && !pRight->bEof
163576 && DOCID_CMP(pLeft->iDocid, pRight->iDocid)>0
163577 ){
163578 fts3EvalNextRow(pCsr, pRight, pRc);
163579 }
163580 }
163581 pExpr->iDocid = pLeft->iDocid;
163582 pExpr->bEof = pLeft->bEof;
163583 break;
163584 }
163585
163586 default: {
163587 Fts3Phrase *pPhrase = pExpr->pPhrase;
163588 fts3EvalInvalidatePoslist(pPhrase);
163589 *pRc = fts3EvalPhraseNext(pCsr, pPhrase, &pExpr->bEof);
163590 pExpr->iDocid = pPhrase->doclist.iDocid;
163591 break;
163592 }
163593 }
163594 }
163595}
163596
163597/*
163598** If *pRc is not SQLITE_OK, or if pExpr is not the root node of a NEAR
163599** cluster, then this function returns 1 immediately.
163600**
163601** Otherwise, it checks if the current row really does match the NEAR
163602** expression, using the data currently stored in the position lists
163603** (Fts3Expr->pPhrase.doclist.pList/nList) for each phrase in the expression.
163604**
163605** If the current row is a match, the position list associated with each
163606** phrase in the NEAR expression is edited in place to contain only those
163607** phrase instances sufficiently close to their peers to satisfy all NEAR
163608** constraints. In this case it returns 1. If the NEAR expression does not
163609** match the current row, 0 is returned. The position lists may or may not
163610** be edited if 0 is returned.
163611*/
163612static int fts3EvalNearTest(Fts3Expr *pExpr, int *pRc){
163613 int res = 1;
163614
163615 /* The following block runs if pExpr is the root of a NEAR query.
163616 ** For example, the query:
163617 **
163618 ** "w" NEAR "x" NEAR "y" NEAR "z"
163619 **
163620 ** which is represented in tree form as:
163621 **
163622 ** |
163623 ** +--NEAR--+ <-- root of NEAR query
163624 ** | |
163625 ** +--NEAR--+ "z"
163626 ** | |
163627 ** +--NEAR--+ "y"
163628 ** | |
163629 ** "w" "x"
163630 **
163631 ** The right-hand child of a NEAR node is always a phrase. The
163632 ** left-hand child may be either a phrase or a NEAR node. There are
163633 ** no exceptions to this - it's the way the parser in fts3_expr.c works.
163634 */
163635 if( *pRc==SQLITE_OK
163636 && pExpr->eType==FTSQUERY_NEAR
163637 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
163638 ){
163639 Fts3Expr *p;
163640 int nTmp = 0; /* Bytes of temp space */
163641 char *aTmp; /* Temp space for PoslistNearMerge() */
163642
163643 /* Allocate temporary working space. */
163644 for(p=pExpr; p->pLeft; p=p->pLeft){
163645 assert( p->pRight->pPhrase->doclist.nList>0 );
163646 nTmp += p->pRight->pPhrase->doclist.nList;
163647 }
163648 nTmp += p->pPhrase->doclist.nList;
163649 aTmp = sqlite3_malloc(nTmp*2);
163650 if( !aTmp ){
163651 *pRc = SQLITE_NOMEM;
163652 res = 0;
163653 }else{
163654 char *aPoslist = p->pPhrase->doclist.pList;
163655 int nToken = p->pPhrase->nToken;
163656
163657 for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
163658 Fts3Phrase *pPhrase = p->pRight->pPhrase;
163659 int nNear = p->nNear;
163660 res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
163661 }
163662
163663 aPoslist = pExpr->pRight->pPhrase->doclist.pList;
163664 nToken = pExpr->pRight->pPhrase->nToken;
163665 for(p=pExpr->pLeft; p && res; p=p->pLeft){
163666 int nNear;
163667 Fts3Phrase *pPhrase;
163668 assert( p->pParent && p->pParent->pLeft==p );
163669 nNear = p->pParent->nNear;
163670 pPhrase = (
163671 p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
163672 );
163673 res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
163674 }
163675 }
163676
163677 sqlite3_free(aTmp);
163678 }
163679
163680 return res;
163681}
163682
163683/*
163684** This function is a helper function for sqlite3Fts3EvalTestDeferred().
163685** Assuming no error occurs or has occurred, It returns non-zero if the
163686** expression passed as the second argument matches the row that pCsr
163687** currently points to, or zero if it does not.
163688**
163689** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
163690** If an error occurs during execution of this function, *pRc is set to
163691** the appropriate SQLite error code. In this case the returned value is
163692** undefined.
163693*/
163694static int fts3EvalTestExpr(
163695 Fts3Cursor *pCsr, /* FTS cursor handle */
163696 Fts3Expr *pExpr, /* Expr to test. May or may not be root. */
163697 int *pRc /* IN/OUT: Error code */
163698){
163699 int bHit = 1; /* Return value */
163700 if( *pRc==SQLITE_OK ){
163701 switch( pExpr->eType ){
163702 case FTSQUERY_NEAR:
163703 case FTSQUERY_AND:
163704 bHit = (
163705 fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
163706 && fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
163707 && fts3EvalNearTest(pExpr, pRc)
163708 );
163709
163710 /* If the NEAR expression does not match any rows, zero the doclist for
163711 ** all phrases involved in the NEAR. This is because the snippet(),
163712 ** offsets() and matchinfo() functions are not supposed to recognize
163713 ** any instances of phrases that are part of unmatched NEAR queries.
163714 ** For example if this expression:
163715 **
163716 ** ... MATCH 'a OR (b NEAR c)'
163717 **
163718 ** is matched against a row containing:
163719 **
163720 ** 'a b d e'
163721 **
163722 ** then any snippet() should ony highlight the "a" term, not the "b"
163723 ** (as "b" is part of a non-matching NEAR clause).
163724 */
163725 if( bHit==0
163726 && pExpr->eType==FTSQUERY_NEAR
163727 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
163728 ){
163729 Fts3Expr *p;
163730 for(p=pExpr; p->pPhrase==0; p=p->pLeft){
163731 if( p->pRight->iDocid==pCsr->iPrevId ){
163732 fts3EvalInvalidatePoslist(p->pRight->pPhrase);
163733 }
163734 }
163735 if( p->iDocid==pCsr->iPrevId ){
163736 fts3EvalInvalidatePoslist(p->pPhrase);
163737 }
163738 }
163739
163740 break;
163741
163742 case FTSQUERY_OR: {
163743 int bHit1 = fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc);
163744 int bHit2 = fts3EvalTestExpr(pCsr, pExpr->pRight, pRc);
163745 bHit = bHit1 || bHit2;
163746 break;
163747 }
163748
163749 case FTSQUERY_NOT:
163750 bHit = (
163751 fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
163752 && !fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
163753 );
163754 break;
163755
163756 default: {
163757#ifndef SQLITE_DISABLE_FTS4_DEFERRED
163758 if( pCsr->pDeferred
163759 && (pExpr->iDocid==pCsr->iPrevId || pExpr->bDeferred)
163760 ){
163761 Fts3Phrase *pPhrase = pExpr->pPhrase;
163762 assert( pExpr->bDeferred || pPhrase->doclist.bFreeList==0 );
163763 if( pExpr->bDeferred ){
163764 fts3EvalInvalidatePoslist(pPhrase);
163765 }
163766 *pRc = fts3EvalDeferredPhrase(pCsr, pPhrase);
163767 bHit = (pPhrase->doclist.pList!=0);
163768 pExpr->iDocid = pCsr->iPrevId;
163769 }else
163770#endif
163771 {
163772 bHit = (pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId);
163773 }
163774 break;
163775 }
163776 }
163777 }
163778 return bHit;
163779}
163780
163781/*
163782** This function is called as the second part of each xNext operation when
163783** iterating through the results of a full-text query. At this point the
163784** cursor points to a row that matches the query expression, with the
163785** following caveats:
163786**
163787** * Up until this point, "NEAR" operators in the expression have been
163788** treated as "AND".
163789**
163790** * Deferred tokens have not yet been considered.
163791**
163792** If *pRc is not SQLITE_OK when this function is called, it immediately
163793** returns 0. Otherwise, it tests whether or not after considering NEAR
163794** operators and deferred tokens the current row is still a match for the
163795** expression. It returns 1 if both of the following are true:
163796**
163797** 1. *pRc is SQLITE_OK when this function returns, and
163798**
163799** 2. After scanning the current FTS table row for the deferred tokens,
163800** it is determined that the row does *not* match the query.
163801**
163802** Or, if no error occurs and it seems the current row does match the FTS
163803** query, return 0.
163804*/
163805SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc){
163806 int rc = *pRc;
163807 int bMiss = 0;
163808 if( rc==SQLITE_OK ){
163809
163810 /* If there are one or more deferred tokens, load the current row into
163811 ** memory and scan it to determine the position list for each deferred
163812 ** token. Then, see if this row is really a match, considering deferred
163813 ** tokens and NEAR operators (neither of which were taken into account
163814 ** earlier, by fts3EvalNextRow()).
163815 */
163816 if( pCsr->pDeferred ){
163817 rc = fts3CursorSeek(0, pCsr);
163818 if( rc==SQLITE_OK ){
163819 rc = sqlite3Fts3CacheDeferredDoclists(pCsr);
163820 }
163821 }
163822 bMiss = (0==fts3EvalTestExpr(pCsr, pCsr->pExpr, &rc));
163823
163824 /* Free the position-lists accumulated for each deferred token above. */
163825 sqlite3Fts3FreeDeferredDoclists(pCsr);
163826 *pRc = rc;
163827 }
163828 return (rc==SQLITE_OK && bMiss);
163829}
163830
163831/*
163832** Advance to the next document that matches the FTS expression in
163833** Fts3Cursor.pExpr.
163834*/
163835static int fts3EvalNext(Fts3Cursor *pCsr){
163836 int rc = SQLITE_OK; /* Return Code */
163837 Fts3Expr *pExpr = pCsr->pExpr;
163838 assert( pCsr->isEof==0 );
163839 if( pExpr==0 ){
163840 pCsr->isEof = 1;
163841 }else{
163842 do {
163843 if( pCsr->isRequireSeek==0 ){
163844 sqlite3_reset(pCsr->pStmt);
163845 }
163846 assert( sqlite3_data_count(pCsr->pStmt)==0 );
163847 fts3EvalNextRow(pCsr, pExpr, &rc);
163848 pCsr->isEof = pExpr->bEof;
163849 pCsr->isRequireSeek = 1;
163850 pCsr->isMatchinfoNeeded = 1;
163851 pCsr->iPrevId = pExpr->iDocid;
163852 }while( pCsr->isEof==0 && sqlite3Fts3EvalTestDeferred(pCsr, &rc) );
163853 }
163854
163855 /* Check if the cursor is past the end of the docid range specified
163856 ** by Fts3Cursor.iMinDocid/iMaxDocid. If so, set the EOF flag. */
163857 if( rc==SQLITE_OK && (
163858 (pCsr->bDesc==0 && pCsr->iPrevId>pCsr->iMaxDocid)
163859 || (pCsr->bDesc!=0 && pCsr->iPrevId<pCsr->iMinDocid)
163860 )){
163861 pCsr->isEof = 1;
163862 }
163863
163864 return rc;
163865}
163866
163867/*
163868** Restart interation for expression pExpr so that the next call to
163869** fts3EvalNext() visits the first row. Do not allow incremental
163870** loading or merging of phrase doclists for this iteration.
163871**
163872** If *pRc is other than SQLITE_OK when this function is called, it is
163873** a no-op. If an error occurs within this function, *pRc is set to an
163874** SQLite error code before returning.
163875*/
163876static void fts3EvalRestart(
163877 Fts3Cursor *pCsr,
163878 Fts3Expr *pExpr,
163879 int *pRc
163880){
163881 if( pExpr && *pRc==SQLITE_OK ){
163882 Fts3Phrase *pPhrase = pExpr->pPhrase;
163883
163884 if( pPhrase ){
163885 fts3EvalInvalidatePoslist(pPhrase);
163886 if( pPhrase->bIncr ){
163887 int i;
163888 for(i=0; i<pPhrase->nToken; i++){
163889 Fts3PhraseToken *pToken = &pPhrase->aToken[i];
163890 assert( pToken->pDeferred==0 );
163891 if( pToken->pSegcsr ){
163892 sqlite3Fts3MsrIncrRestart(pToken->pSegcsr);
163893 }
163894 }
163895 *pRc = fts3EvalPhraseStart(pCsr, 0, pPhrase);
163896 }
163897 pPhrase->doclist.pNextDocid = 0;
163898 pPhrase->doclist.iDocid = 0;
163899 pPhrase->pOrPoslist = 0;
163900 }
163901
163902 pExpr->iDocid = 0;
163903 pExpr->bEof = 0;
163904 pExpr->bStart = 0;
163905
163906 fts3EvalRestart(pCsr, pExpr->pLeft, pRc);
163907 fts3EvalRestart(pCsr, pExpr->pRight, pRc);
163908 }
163909}
163910
163911/*
163912** After allocating the Fts3Expr.aMI[] array for each phrase in the
163913** expression rooted at pExpr, the cursor iterates through all rows matched
163914** by pExpr, calling this function for each row. This function increments
163915** the values in Fts3Expr.aMI[] according to the position-list currently
163916** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase
163917** expression nodes.
163918*/
163919static void fts3EvalUpdateCounts(Fts3Expr *pExpr){
163920 if( pExpr ){
163921 Fts3Phrase *pPhrase = pExpr->pPhrase;
163922 if( pPhrase && pPhrase->doclist.pList ){
163923 int iCol = 0;
163924 char *p = pPhrase->doclist.pList;
163925
163926 assert( *p );
163927 while( 1 ){
163928 u8 c = 0;
163929 int iCnt = 0;
163930 while( 0xFE & (*p | c) ){
163931 if( (c&0x80)==0 ) iCnt++;
163932 c = *p++ & 0x80;
163933 }
163934
163935 /* aMI[iCol*3 + 1] = Number of occurrences
163936 ** aMI[iCol*3 + 2] = Number of rows containing at least one instance
163937 */
163938 pExpr->aMI[iCol*3 + 1] += iCnt;
163939 pExpr->aMI[iCol*3 + 2] += (iCnt>0);
163940 if( *p==0x00 ) break;
163941 p++;
163942 p += fts3GetVarint32(p, &iCol);
163943 }
163944 }
163945
163946 fts3EvalUpdateCounts(pExpr->pLeft);
163947 fts3EvalUpdateCounts(pExpr->pRight);
163948 }
163949}
163950
163951/*
163952** Expression pExpr must be of type FTSQUERY_PHRASE.
163953**
163954** If it is not already allocated and populated, this function allocates and
163955** populates the Fts3Expr.aMI[] array for expression pExpr. If pExpr is part
163956** of a NEAR expression, then it also allocates and populates the same array
163957** for all other phrases that are part of the NEAR expression.
163958**
163959** SQLITE_OK is returned if the aMI[] array is successfully allocated and
163960** populated. Otherwise, if an error occurs, an SQLite error code is returned.
163961*/
163962static int fts3EvalGatherStats(
163963 Fts3Cursor *pCsr, /* Cursor object */
163964 Fts3Expr *pExpr /* FTSQUERY_PHRASE expression */
163965){
163966 int rc = SQLITE_OK; /* Return code */
163967
163968 assert( pExpr->eType==FTSQUERY_PHRASE );
163969 if( pExpr->aMI==0 ){
163970 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
163971 Fts3Expr *pRoot; /* Root of NEAR expression */
163972 Fts3Expr *p; /* Iterator used for several purposes */
163973
163974 sqlite3_int64 iPrevId = pCsr->iPrevId;
163975 sqlite3_int64 iDocid;
163976 u8 bEof;
163977
163978 /* Find the root of the NEAR expression */
163979 pRoot = pExpr;
163980 while( pRoot->pParent && pRoot->pParent->eType==FTSQUERY_NEAR ){
163981 pRoot = pRoot->pParent;
163982 }
163983 iDocid = pRoot->iDocid;
163984 bEof = pRoot->bEof;
163985 assert( pRoot->bStart );
163986
163987 /* Allocate space for the aMSI[] array of each FTSQUERY_PHRASE node */
163988 for(p=pRoot; p; p=p->pLeft){
163989 Fts3Expr *pE = (p->eType==FTSQUERY_PHRASE?p:p->pRight);
163990 assert( pE->aMI==0 );
163991 pE->aMI = (u32 *)sqlite3_malloc(pTab->nColumn * 3 * sizeof(u32));
163992 if( !pE->aMI ) return SQLITE_NOMEM;
163993 memset(pE->aMI, 0, pTab->nColumn * 3 * sizeof(u32));
163994 }
163995
163996 fts3EvalRestart(pCsr, pRoot, &rc);
163997
163998 while( pCsr->isEof==0 && rc==SQLITE_OK ){
163999
164000 do {
164001 /* Ensure the %_content statement is reset. */
164002 if( pCsr->isRequireSeek==0 ) sqlite3_reset(pCsr->pStmt);
164003 assert( sqlite3_data_count(pCsr->pStmt)==0 );
164004
164005 /* Advance to the next document */
164006 fts3EvalNextRow(pCsr, pRoot, &rc);
164007 pCsr->isEof = pRoot->bEof;
164008 pCsr->isRequireSeek = 1;
164009 pCsr->isMatchinfoNeeded = 1;
164010 pCsr->iPrevId = pRoot->iDocid;
164011 }while( pCsr->isEof==0
164012 && pRoot->eType==FTSQUERY_NEAR
164013 && sqlite3Fts3EvalTestDeferred(pCsr, &rc)
164014 );
164015
164016 if( rc==SQLITE_OK && pCsr->isEof==0 ){
164017 fts3EvalUpdateCounts(pRoot);
164018 }
164019 }
164020
164021 pCsr->isEof = 0;
164022 pCsr->iPrevId = iPrevId;
164023
164024 if( bEof ){
164025 pRoot->bEof = bEof;
164026 }else{
164027 /* Caution: pRoot may iterate through docids in ascending or descending
164028 ** order. For this reason, even though it seems more defensive, the
164029 ** do loop can not be written:
164030 **
164031 ** do {...} while( pRoot->iDocid<iDocid && rc==SQLITE_OK );
164032 */
164033 fts3EvalRestart(pCsr, pRoot, &rc);
164034 do {
164035 fts3EvalNextRow(pCsr, pRoot, &rc);
164036 assert( pRoot->bEof==0 );
164037 }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK );
164038 }
164039 }
164040 return rc;
164041}
164042
164043/*
164044** This function is used by the matchinfo() module to query a phrase
164045** expression node for the following information:
164046**
164047** 1. The total number of occurrences of the phrase in each column of
164048** the FTS table (considering all rows), and
164049**
164050** 2. For each column, the number of rows in the table for which the
164051** column contains at least one instance of the phrase.
164052**
164053** If no error occurs, SQLITE_OK is returned and the values for each column
164054** written into the array aiOut as follows:
164055**
164056** aiOut[iCol*3 + 1] = Number of occurrences
164057** aiOut[iCol*3 + 2] = Number of rows containing at least one instance
164058**
164059** Caveats:
164060**
164061** * If a phrase consists entirely of deferred tokens, then all output
164062** values are set to the number of documents in the table. In other
164063** words we assume that very common tokens occur exactly once in each
164064** column of each row of the table.
164065**
164066** * If a phrase contains some deferred tokens (and some non-deferred
164067** tokens), count the potential occurrence identified by considering
164068** the non-deferred tokens instead of actual phrase occurrences.
164069**
164070** * If the phrase is part of a NEAR expression, then only phrase instances
164071** that meet the NEAR constraint are included in the counts.
164072*/
164073SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(
164074 Fts3Cursor *pCsr, /* FTS cursor handle */
164075 Fts3Expr *pExpr, /* Phrase expression */
164076 u32 *aiOut /* Array to write results into (see above) */
164077){
164078 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
164079 int rc = SQLITE_OK;
164080 int iCol;
164081
164082 if( pExpr->bDeferred && pExpr->pParent->eType!=FTSQUERY_NEAR ){
164083 assert( pCsr->nDoc>0 );
164084 for(iCol=0; iCol<pTab->nColumn; iCol++){
164085 aiOut[iCol*3 + 1] = (u32)pCsr->nDoc;
164086 aiOut[iCol*3 + 2] = (u32)pCsr->nDoc;
164087 }
164088 }else{
164089 rc = fts3EvalGatherStats(pCsr, pExpr);
164090 if( rc==SQLITE_OK ){
164091 assert( pExpr->aMI );
164092 for(iCol=0; iCol<pTab->nColumn; iCol++){
164093 aiOut[iCol*3 + 1] = pExpr->aMI[iCol*3 + 1];
164094 aiOut[iCol*3 + 2] = pExpr->aMI[iCol*3 + 2];
164095 }
164096 }
164097 }
164098
164099 return rc;
164100}
164101
164102/*
164103** The expression pExpr passed as the second argument to this function
164104** must be of type FTSQUERY_PHRASE.
164105**
164106** The returned value is either NULL or a pointer to a buffer containing
164107** a position-list indicating the occurrences of the phrase in column iCol
164108** of the current row.
164109**
164110** More specifically, the returned buffer contains 1 varint for each
164111** occurrence of the phrase in the column, stored using the normal (delta+2)
164112** compression and is terminated by either an 0x01 or 0x00 byte. For example,
164113** if the requested column contains "a b X c d X X" and the position-list
164114** for 'X' is requested, the buffer returned may contain:
164115**
164116** 0x04 0x05 0x03 0x01 or 0x04 0x05 0x03 0x00
164117**
164118** This function works regardless of whether or not the phrase is deferred,
164119** incremental, or neither.
164120*/
164121SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(
164122 Fts3Cursor *pCsr, /* FTS3 cursor object */
164123 Fts3Expr *pExpr, /* Phrase to return doclist for */
164124 int iCol, /* Column to return position list for */
164125 char **ppOut /* OUT: Pointer to position list */
164126){
164127 Fts3Phrase *pPhrase = pExpr->pPhrase;
164128 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
164129 char *pIter;
164130 int iThis;
164131 sqlite3_int64 iDocid;
164132
164133 /* If this phrase is applies specifically to some column other than
164134 ** column iCol, return a NULL pointer. */
164135 *ppOut = 0;
164136 assert( iCol>=0 && iCol<pTab->nColumn );
164137 if( (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol) ){
164138 return SQLITE_OK;
164139 }
164140
164141 iDocid = pExpr->iDocid;
164142 pIter = pPhrase->doclist.pList;
164143 if( iDocid!=pCsr->iPrevId || pExpr->bEof ){
164144 int rc = SQLITE_OK;
164145 int bDescDoclist = pTab->bDescIdx; /* For DOCID_CMP macro */
164146 int bOr = 0;
164147 u8 bTreeEof = 0;
164148 Fts3Expr *p; /* Used to iterate from pExpr to root */
164149 Fts3Expr *pNear; /* Most senior NEAR ancestor (or pExpr) */
164150 int bMatch;
164151
164152 /* Check if this phrase descends from an OR expression node. If not,
164153 ** return NULL. Otherwise, the entry that corresponds to docid
164154 ** pCsr->iPrevId may lie earlier in the doclist buffer. Or, if the
164155 ** tree that the node is part of has been marked as EOF, but the node
164156 ** itself is not EOF, then it may point to an earlier entry. */
164157 pNear = pExpr;
164158 for(p=pExpr->pParent; p; p=p->pParent){
164159 if( p->eType==FTSQUERY_OR ) bOr = 1;
164160 if( p->eType==FTSQUERY_NEAR ) pNear = p;
164161 if( p->bEof ) bTreeEof = 1;
164162 }
164163 if( bOr==0 ) return SQLITE_OK;
164164
164165 /* This is the descendent of an OR node. In this case we cannot use
164166 ** an incremental phrase. Load the entire doclist for the phrase
164167 ** into memory in this case. */
164168 if( pPhrase->bIncr ){
164169 int bEofSave = pNear->bEof;
164170 fts3EvalRestart(pCsr, pNear, &rc);
164171 while( rc==SQLITE_OK && !pNear->bEof ){
164172 fts3EvalNextRow(pCsr, pNear, &rc);
164173 if( bEofSave==0 && pNear->iDocid==iDocid ) break;
164174 }
164175 assert( rc!=SQLITE_OK || pPhrase->bIncr==0 );
164176 }
164177 if( bTreeEof ){
164178 while( rc==SQLITE_OK && !pNear->bEof ){
164179 fts3EvalNextRow(pCsr, pNear, &rc);
164180 }
164181 }
164182 if( rc!=SQLITE_OK ) return rc;
164183
164184 bMatch = 1;
164185 for(p=pNear; p; p=p->pLeft){
164186 u8 bEof = 0;
164187 Fts3Expr *pTest = p;
164188 Fts3Phrase *pPh;
164189 assert( pTest->eType==FTSQUERY_NEAR || pTest->eType==FTSQUERY_PHRASE );
164190 if( pTest->eType==FTSQUERY_NEAR ) pTest = pTest->pRight;
164191 assert( pTest->eType==FTSQUERY_PHRASE );
164192 pPh = pTest->pPhrase;
164193
164194 pIter = pPh->pOrPoslist;
164195 iDocid = pPh->iOrDocid;
164196 if( pCsr->bDesc==bDescDoclist ){
164197 bEof = !pPh->doclist.nAll ||
164198 (pIter >= (pPh->doclist.aAll + pPh->doclist.nAll));
164199 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){
164200 sqlite3Fts3DoclistNext(
164201 bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
164202 &pIter, &iDocid, &bEof
164203 );
164204 }
164205 }else{
164206 bEof = !pPh->doclist.nAll || (pIter && pIter<=pPh->doclist.aAll);
164207 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){
164208 int dummy;
164209 sqlite3Fts3DoclistPrev(
164210 bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
164211 &pIter, &iDocid, &dummy, &bEof
164212 );
164213 }
164214 }
164215 pPh->pOrPoslist = pIter;
164216 pPh->iOrDocid = iDocid;
164217 if( bEof || iDocid!=pCsr->iPrevId ) bMatch = 0;
164218 }
164219
164220 if( bMatch ){
164221 pIter = pPhrase->pOrPoslist;
164222 }else{
164223 pIter = 0;
164224 }
164225 }
164226 if( pIter==0 ) return SQLITE_OK;
164227
164228 if( *pIter==0x01 ){
164229 pIter++;
164230 pIter += fts3GetVarint32(pIter, &iThis);
164231 }else{
164232 iThis = 0;
164233 }
164234 while( iThis<iCol ){
164235 fts3ColumnlistCopy(0, &pIter);
164236 if( *pIter==0x00 ) return SQLITE_OK;
164237 pIter++;
164238 pIter += fts3GetVarint32(pIter, &iThis);
164239 }
164240 if( *pIter==0x00 ){
164241 pIter = 0;
164242 }
164243
164244 *ppOut = ((iCol==iThis)?pIter:0);
164245 return SQLITE_OK;
164246}
164247
164248/*
164249** Free all components of the Fts3Phrase structure that were allocated by
164250** the eval module. Specifically, this means to free:
164251**
164252** * the contents of pPhrase->doclist, and
164253** * any Fts3MultiSegReader objects held by phrase tokens.
164254*/
164255SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *pPhrase){
164256 if( pPhrase ){
164257 int i;
164258 sqlite3_free(pPhrase->doclist.aAll);
164259 fts3EvalInvalidatePoslist(pPhrase);
164260 memset(&pPhrase->doclist, 0, sizeof(Fts3Doclist));
164261 for(i=0; i<pPhrase->nToken; i++){
164262 fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr);
164263 pPhrase->aToken[i].pSegcsr = 0;
164264 }
164265 }
164266}
164267
164268
164269/*
164270** Return SQLITE_CORRUPT_VTAB.
164271*/
164272#ifdef SQLITE_DEBUG
164273SQLITE_PRIVATE int sqlite3Fts3Corrupt(){
164274 return SQLITE_CORRUPT_VTAB;
164275}
164276#endif
164277
164278#if !SQLITE_CORE
164279/*
164280** Initialize API pointer table, if required.
164281*/
164282#ifdef _WIN32
164283__declspec(dllexport)
164284#endif
164285SQLITE_API int sqlite3_fts3_init(
164286 sqlite3 *db,
164287 char **pzErrMsg,
164288 const sqlite3_api_routines *pApi
164289){
164290 SQLITE_EXTENSION_INIT2(pApi)
164291 return sqlite3Fts3Init(db);
164292}
164293#endif
164294
164295#endif
164296
164297/************** End of fts3.c ************************************************/
164298/************** Begin file fts3_aux.c ****************************************/
164299/*
164300** 2011 Jan 27
164301**
164302** The author disclaims copyright to this source code. In place of
164303** a legal notice, here is a blessing:
164304**
164305** May you do good and not evil.
164306** May you find forgiveness for yourself and forgive others.
164307** May you share freely, never taking more than you give.
164308**
164309******************************************************************************
164310**
164311*/
164312/* #include "fts3Int.h" */
164313#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
164314
164315/* #include <string.h> */
164316/* #include <assert.h> */
164317
164318typedef struct Fts3auxTable Fts3auxTable;
164319typedef struct Fts3auxCursor Fts3auxCursor;
164320
164321struct Fts3auxTable {
164322 sqlite3_vtab base; /* Base class used by SQLite core */
164323 Fts3Table *pFts3Tab;
164324};
164325
164326struct Fts3auxCursor {
164327 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
164328 Fts3MultiSegReader csr; /* Must be right after "base" */
164329 Fts3SegFilter filter;
164330 char *zStop;
164331 int nStop; /* Byte-length of string zStop */
164332 int iLangid; /* Language id to query */
164333 int isEof; /* True if cursor is at EOF */
164334 sqlite3_int64 iRowid; /* Current rowid */
164335
164336 int iCol; /* Current value of 'col' column */
164337 int nStat; /* Size of aStat[] array */
164338 struct Fts3auxColstats {
164339 sqlite3_int64 nDoc; /* 'documents' values for current csr row */
164340 sqlite3_int64 nOcc; /* 'occurrences' values for current csr row */
164341 } *aStat;
164342};
164343
164344/*
164345** Schema of the terms table.
164346*/
164347#define FTS3_AUX_SCHEMA \
164348 "CREATE TABLE x(term, col, documents, occurrences, languageid HIDDEN)"
164349
164350/*
164351** This function does all the work for both the xConnect and xCreate methods.
164352** These tables have no persistent representation of their own, so xConnect
164353** and xCreate are identical operations.
164354*/
164355static int fts3auxConnectMethod(
164356 sqlite3 *db, /* Database connection */
164357 void *pUnused, /* Unused */
164358 int argc, /* Number of elements in argv array */
164359 const char * const *argv, /* xCreate/xConnect argument array */
164360 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
164361 char **pzErr /* OUT: sqlite3_malloc'd error message */
164362){
164363 char const *zDb; /* Name of database (e.g. "main") */
164364 char const *zFts3; /* Name of fts3 table */
164365 int nDb; /* Result of strlen(zDb) */
164366 int nFts3; /* Result of strlen(zFts3) */
164367 int nByte; /* Bytes of space to allocate here */
164368 int rc; /* value returned by declare_vtab() */
164369 Fts3auxTable *p; /* Virtual table object to return */
164370
164371 UNUSED_PARAMETER(pUnused);
164372
164373 /* The user should invoke this in one of two forms:
164374 **
164375 ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table);
164376 ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table-db, fts4-table);
164377 */
164378 if( argc!=4 && argc!=5 ) goto bad_args;
164379
164380 zDb = argv[1];
164381 nDb = (int)strlen(zDb);
164382 if( argc==5 ){
164383 if( nDb==4 && 0==sqlite3_strnicmp("temp", zDb, 4) ){
164384 zDb = argv[3];
164385 nDb = (int)strlen(zDb);
164386 zFts3 = argv[4];
164387 }else{
164388 goto bad_args;
164389 }
164390 }else{
164391 zFts3 = argv[3];
164392 }
164393 nFts3 = (int)strlen(zFts3);
164394
164395 rc = sqlite3_declare_vtab(db, FTS3_AUX_SCHEMA);
164396 if( rc!=SQLITE_OK ) return rc;
164397
164398 nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2;
164399 p = (Fts3auxTable *)sqlite3_malloc(nByte);
164400 if( !p ) return SQLITE_NOMEM;
164401 memset(p, 0, nByte);
164402
164403 p->pFts3Tab = (Fts3Table *)&p[1];
164404 p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
164405 p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
164406 p->pFts3Tab->db = db;
164407 p->pFts3Tab->nIndex = 1;
164408
164409 memcpy((char *)p->pFts3Tab->zDb, zDb, nDb);
164410 memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
164411 sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
164412
164413 *ppVtab = (sqlite3_vtab *)p;
164414 return SQLITE_OK;
164415
164416 bad_args:
164417 sqlite3Fts3ErrMsg(pzErr, "invalid arguments to fts4aux constructor");
164418 return SQLITE_ERROR;
164419}
164420
164421/*
164422** This function does the work for both the xDisconnect and xDestroy methods.
164423** These tables have no persistent representation of their own, so xDisconnect
164424** and xDestroy are identical operations.
164425*/
164426static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){
164427 Fts3auxTable *p = (Fts3auxTable *)pVtab;
164428 Fts3Table *pFts3 = p->pFts3Tab;
164429 int i;
164430
164431 /* Free any prepared statements held */
164432 for(i=0; i<SizeofArray(pFts3->aStmt); i++){
164433 sqlite3_finalize(pFts3->aStmt[i]);
164434 }
164435 sqlite3_free(pFts3->zSegmentsTbl);
164436 sqlite3_free(p);
164437 return SQLITE_OK;
164438}
164439
164440#define FTS4AUX_EQ_CONSTRAINT 1
164441#define FTS4AUX_GE_CONSTRAINT 2
164442#define FTS4AUX_LE_CONSTRAINT 4
164443
164444/*
164445** xBestIndex - Analyze a WHERE and ORDER BY clause.
164446*/
164447static int fts3auxBestIndexMethod(
164448 sqlite3_vtab *pVTab,
164449 sqlite3_index_info *pInfo
164450){
164451 int i;
164452 int iEq = -1;
164453 int iGe = -1;
164454 int iLe = -1;
164455 int iLangid = -1;
164456 int iNext = 1; /* Next free argvIndex value */
164457
164458 UNUSED_PARAMETER(pVTab);
164459
164460 /* This vtab delivers always results in "ORDER BY term ASC" order. */
164461 if( pInfo->nOrderBy==1
164462 && pInfo->aOrderBy[0].iColumn==0
164463 && pInfo->aOrderBy[0].desc==0
164464 ){
164465 pInfo->orderByConsumed = 1;
164466 }
164467
164468 /* Search for equality and range constraints on the "term" column.
164469 ** And equality constraints on the hidden "languageid" column. */
164470 for(i=0; i<pInfo->nConstraint; i++){
164471 if( pInfo->aConstraint[i].usable ){
164472 int op = pInfo->aConstraint[i].op;
164473 int iCol = pInfo->aConstraint[i].iColumn;
164474
164475 if( iCol==0 ){
164476 if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iEq = i;
164477 if( op==SQLITE_INDEX_CONSTRAINT_LT ) iLe = i;
164478 if( op==SQLITE_INDEX_CONSTRAINT_LE ) iLe = i;
164479 if( op==SQLITE_INDEX_CONSTRAINT_GT ) iGe = i;
164480 if( op==SQLITE_INDEX_CONSTRAINT_GE ) iGe = i;
164481 }
164482 if( iCol==4 ){
164483 if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iLangid = i;
164484 }
164485 }
164486 }
164487
164488 if( iEq>=0 ){
164489 pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT;
164490 pInfo->aConstraintUsage[iEq].argvIndex = iNext++;
164491 pInfo->estimatedCost = 5;
164492 }else{
164493 pInfo->idxNum = 0;
164494 pInfo->estimatedCost = 20000;
164495 if( iGe>=0 ){
164496 pInfo->idxNum += FTS4AUX_GE_CONSTRAINT;
164497 pInfo->aConstraintUsage[iGe].argvIndex = iNext++;
164498 pInfo->estimatedCost /= 2;
164499 }
164500 if( iLe>=0 ){
164501 pInfo->idxNum += FTS4AUX_LE_CONSTRAINT;
164502 pInfo->aConstraintUsage[iLe].argvIndex = iNext++;
164503 pInfo->estimatedCost /= 2;
164504 }
164505 }
164506 if( iLangid>=0 ){
164507 pInfo->aConstraintUsage[iLangid].argvIndex = iNext++;
164508 pInfo->estimatedCost--;
164509 }
164510
164511 return SQLITE_OK;
164512}
164513
164514/*
164515** xOpen - Open a cursor.
164516*/
164517static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
164518 Fts3auxCursor *pCsr; /* Pointer to cursor object to return */
164519
164520 UNUSED_PARAMETER(pVTab);
164521
164522 pCsr = (Fts3auxCursor *)sqlite3_malloc(sizeof(Fts3auxCursor));
164523 if( !pCsr ) return SQLITE_NOMEM;
164524 memset(pCsr, 0, sizeof(Fts3auxCursor));
164525
164526 *ppCsr = (sqlite3_vtab_cursor *)pCsr;
164527 return SQLITE_OK;
164528}
164529
164530/*
164531** xClose - Close a cursor.
164532*/
164533static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){
164534 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
164535 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
164536
164537 sqlite3Fts3SegmentsClose(pFts3);
164538 sqlite3Fts3SegReaderFinish(&pCsr->csr);
164539 sqlite3_free((void *)pCsr->filter.zTerm);
164540 sqlite3_free(pCsr->zStop);
164541 sqlite3_free(pCsr->aStat);
164542 sqlite3_free(pCsr);
164543 return SQLITE_OK;
164544}
164545
164546static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){
164547 if( nSize>pCsr->nStat ){
164548 struct Fts3auxColstats *aNew;
164549 aNew = (struct Fts3auxColstats *)sqlite3_realloc(pCsr->aStat,
164550 sizeof(struct Fts3auxColstats) * nSize
164551 );
164552 if( aNew==0 ) return SQLITE_NOMEM;
164553 memset(&aNew[pCsr->nStat], 0,
164554 sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
164555 );
164556 pCsr->aStat = aNew;
164557 pCsr->nStat = nSize;
164558 }
164559 return SQLITE_OK;
164560}
164561
164562/*
164563** xNext - Advance the cursor to the next row, if any.
164564*/
164565static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){
164566 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
164567 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
164568 int rc;
164569
164570 /* Increment our pretend rowid value. */
164571 pCsr->iRowid++;
164572
164573 for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
164574 if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK;
164575 }
164576
164577 rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
164578 if( rc==SQLITE_ROW ){
164579 int i = 0;
164580 int nDoclist = pCsr->csr.nDoclist;
164581 char *aDoclist = pCsr->csr.aDoclist;
164582 int iCol;
164583
164584 int eState = 0;
164585
164586 if( pCsr->zStop ){
164587 int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
164588 int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n);
164589 if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
164590 pCsr->isEof = 1;
164591 return SQLITE_OK;
164592 }
164593 }
164594
164595 if( fts3auxGrowStatArray(pCsr, 2) ) return SQLITE_NOMEM;
164596 memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat);
164597 iCol = 0;
164598
164599 while( i<nDoclist ){
164600 sqlite3_int64 v = 0;
164601
164602 i += sqlite3Fts3GetVarint(&aDoclist[i], &v);
164603 switch( eState ){
164604 /* State 0. In this state the integer just read was a docid. */
164605 case 0:
164606 pCsr->aStat[0].nDoc++;
164607 eState = 1;
164608 iCol = 0;
164609 break;
164610
164611 /* State 1. In this state we are expecting either a 1, indicating
164612 ** that the following integer will be a column number, or the
164613 ** start of a position list for column 0.
164614 **
164615 ** The only difference between state 1 and state 2 is that if the
164616 ** integer encountered in state 1 is not 0 or 1, then we need to
164617 ** increment the column 0 "nDoc" count for this term.
164618 */
164619 case 1:
164620 assert( iCol==0 );
164621 if( v>1 ){
164622 pCsr->aStat[1].nDoc++;
164623 }
164624 eState = 2;
164625 /* fall through */
164626
164627 case 2:
164628 if( v==0 ){ /* 0x00. Next integer will be a docid. */
164629 eState = 0;
164630 }else if( v==1 ){ /* 0x01. Next integer will be a column number. */
164631 eState = 3;
164632 }else{ /* 2 or greater. A position. */
164633 pCsr->aStat[iCol+1].nOcc++;
164634 pCsr->aStat[0].nOcc++;
164635 }
164636 break;
164637
164638 /* State 3. The integer just read is a column number. */
164639 default: assert( eState==3 );
164640 iCol = (int)v;
164641 if( fts3auxGrowStatArray(pCsr, iCol+2) ) return SQLITE_NOMEM;
164642 pCsr->aStat[iCol+1].nDoc++;
164643 eState = 2;
164644 break;
164645 }
164646 }
164647
164648 pCsr->iCol = 0;
164649 rc = SQLITE_OK;
164650 }else{
164651 pCsr->isEof = 1;
164652 }
164653 return rc;
164654}
164655
164656/*
164657** xFilter - Initialize a cursor to point at the start of its data.
164658*/
164659static int fts3auxFilterMethod(
164660 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
164661 int idxNum, /* Strategy index */
164662 const char *idxStr, /* Unused */
164663 int nVal, /* Number of elements in apVal */
164664 sqlite3_value **apVal /* Arguments for the indexing scheme */
164665){
164666 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
164667 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
164668 int rc;
164669 int isScan = 0;
164670 int iLangVal = 0; /* Language id to query */
164671
164672 int iEq = -1; /* Index of term=? value in apVal */
164673 int iGe = -1; /* Index of term>=? value in apVal */
164674 int iLe = -1; /* Index of term<=? value in apVal */
164675 int iLangid = -1; /* Index of languageid=? value in apVal */
164676 int iNext = 0;
164677
164678 UNUSED_PARAMETER(nVal);
164679 UNUSED_PARAMETER(idxStr);
164680
164681 assert( idxStr==0 );
164682 assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0
164683 || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT
164684 || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT)
164685 );
164686
164687 if( idxNum==FTS4AUX_EQ_CONSTRAINT ){
164688 iEq = iNext++;
164689 }else{
164690 isScan = 1;
164691 if( idxNum & FTS4AUX_GE_CONSTRAINT ){
164692 iGe = iNext++;
164693 }
164694 if( idxNum & FTS4AUX_LE_CONSTRAINT ){
164695 iLe = iNext++;
164696 }
164697 }
164698 if( iNext<nVal ){
164699 iLangid = iNext++;
164700 }
164701
164702 /* In case this cursor is being reused, close and zero it. */
164703 testcase(pCsr->filter.zTerm);
164704 sqlite3Fts3SegReaderFinish(&pCsr->csr);
164705 sqlite3_free((void *)pCsr->filter.zTerm);
164706 sqlite3_free(pCsr->aStat);
164707 memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
164708
164709 pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
164710 if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN;
164711
164712 if( iEq>=0 || iGe>=0 ){
164713 const unsigned char *zStr = sqlite3_value_text(apVal[0]);
164714 assert( (iEq==0 && iGe==-1) || (iEq==-1 && iGe==0) );
164715 if( zStr ){
164716 pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr);
164717 pCsr->filter.nTerm = sqlite3_value_bytes(apVal[0]);
164718 if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM;
164719 }
164720 }
164721
164722 if( iLe>=0 ){
164723 pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iLe]));
164724 pCsr->nStop = sqlite3_value_bytes(apVal[iLe]);
164725 if( pCsr->zStop==0 ) return SQLITE_NOMEM;
164726 }
164727
164728 if( iLangid>=0 ){
164729 iLangVal = sqlite3_value_int(apVal[iLangid]);
164730
164731 /* If the user specified a negative value for the languageid, use zero
164732 ** instead. This works, as the "languageid=?" constraint will also
164733 ** be tested by the VDBE layer. The test will always be false (since
164734 ** this module will not return a row with a negative languageid), and
164735 ** so the overall query will return zero rows. */
164736 if( iLangVal<0 ) iLangVal = 0;
164737 }
164738 pCsr->iLangid = iLangVal;
164739
164740 rc = sqlite3Fts3SegReaderCursor(pFts3, iLangVal, 0, FTS3_SEGCURSOR_ALL,
164741 pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr
164742 );
164743 if( rc==SQLITE_OK ){
164744 rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
164745 }
164746
164747 if( rc==SQLITE_OK ) rc = fts3auxNextMethod(pCursor);
164748 return rc;
164749}
164750
164751/*
164752** xEof - Return true if the cursor is at EOF, or false otherwise.
164753*/
164754static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){
164755 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
164756 return pCsr->isEof;
164757}
164758
164759/*
164760** xColumn - Return a column value.
164761*/
164762static int fts3auxColumnMethod(
164763 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
164764 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
164765 int iCol /* Index of column to read value from */
164766){
164767 Fts3auxCursor *p = (Fts3auxCursor *)pCursor;
164768
164769 assert( p->isEof==0 );
164770 switch( iCol ){
164771 case 0: /* term */
164772 sqlite3_result_text(pCtx, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT);
164773 break;
164774
164775 case 1: /* col */
164776 if( p->iCol ){
164777 sqlite3_result_int(pCtx, p->iCol-1);
164778 }else{
164779 sqlite3_result_text(pCtx, "*", -1, SQLITE_STATIC);
164780 }
164781 break;
164782
164783 case 2: /* documents */
164784 sqlite3_result_int64(pCtx, p->aStat[p->iCol].nDoc);
164785 break;
164786
164787 case 3: /* occurrences */
164788 sqlite3_result_int64(pCtx, p->aStat[p->iCol].nOcc);
164789 break;
164790
164791 default: /* languageid */
164792 assert( iCol==4 );
164793 sqlite3_result_int(pCtx, p->iLangid);
164794 break;
164795 }
164796
164797 return SQLITE_OK;
164798}
164799
164800/*
164801** xRowid - Return the current rowid for the cursor.
164802*/
164803static int fts3auxRowidMethod(
164804 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
164805 sqlite_int64 *pRowid /* OUT: Rowid value */
164806){
164807 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
164808 *pRowid = pCsr->iRowid;
164809 return SQLITE_OK;
164810}
164811
164812/*
164813** Register the fts3aux module with database connection db. Return SQLITE_OK
164814** if successful or an error code if sqlite3_create_module() fails.
164815*/
164816SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db){
164817 static const sqlite3_module fts3aux_module = {
164818 0, /* iVersion */
164819 fts3auxConnectMethod, /* xCreate */
164820 fts3auxConnectMethod, /* xConnect */
164821 fts3auxBestIndexMethod, /* xBestIndex */
164822 fts3auxDisconnectMethod, /* xDisconnect */
164823 fts3auxDisconnectMethod, /* xDestroy */
164824 fts3auxOpenMethod, /* xOpen */
164825 fts3auxCloseMethod, /* xClose */
164826 fts3auxFilterMethod, /* xFilter */
164827 fts3auxNextMethod, /* xNext */
164828 fts3auxEofMethod, /* xEof */
164829 fts3auxColumnMethod, /* xColumn */
164830 fts3auxRowidMethod, /* xRowid */
164831 0, /* xUpdate */
164832 0, /* xBegin */
164833 0, /* xSync */
164834 0, /* xCommit */
164835 0, /* xRollback */
164836 0, /* xFindFunction */
164837 0, /* xRename */
164838 0, /* xSavepoint */
164839 0, /* xRelease */
164840 0, /* xRollbackTo */
164841 0 /* xShadowName */
164842 };
164843 int rc; /* Return code */
164844
164845 rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0);
164846 return rc;
164847}
164848
164849#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
164850
164851/************** End of fts3_aux.c ********************************************/
164852/************** Begin file fts3_expr.c ***************************************/
164853/*
164854** 2008 Nov 28
164855**
164856** The author disclaims copyright to this source code. In place of
164857** a legal notice, here is a blessing:
164858**
164859** May you do good and not evil.
164860** May you find forgiveness for yourself and forgive others.
164861** May you share freely, never taking more than you give.
164862**
164863******************************************************************************
164864**
164865** This module contains code that implements a parser for fts3 query strings
164866** (the right-hand argument to the MATCH operator). Because the supported
164867** syntax is relatively simple, the whole tokenizer/parser system is
164868** hand-coded.
164869*/
164870/* #include "fts3Int.h" */
164871#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
164872
164873/*
164874** By default, this module parses the legacy syntax that has been
164875** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS
164876** is defined, then it uses the new syntax. The differences between
164877** the new and the old syntaxes are:
164878**
164879** a) The new syntax supports parenthesis. The old does not.
164880**
164881** b) The new syntax supports the AND and NOT operators. The old does not.
164882**
164883** c) The old syntax supports the "-" token qualifier. This is not
164884** supported by the new syntax (it is replaced by the NOT operator).
164885**
164886** d) When using the old syntax, the OR operator has a greater precedence
164887** than an implicit AND. When using the new, both implicity and explicit
164888** AND operators have a higher precedence than OR.
164889**
164890** If compiled with SQLITE_TEST defined, then this module exports the
164891** symbol "int sqlite3_fts3_enable_parentheses". Setting this variable
164892** to zero causes the module to use the old syntax. If it is set to
164893** non-zero the new syntax is activated. This is so both syntaxes can
164894** be tested using a single build of testfixture.
164895**
164896** The following describes the syntax supported by the fts3 MATCH
164897** operator in a similar format to that used by the lemon parser
164898** generator. This module does not use actually lemon, it uses a
164899** custom parser.
164900**
164901** query ::= andexpr (OR andexpr)*.
164902**
164903** andexpr ::= notexpr (AND? notexpr)*.
164904**
164905** notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*.
164906** notexpr ::= LP query RP.
164907**
164908** nearexpr ::= phrase (NEAR distance_opt nearexpr)*.
164909**
164910** distance_opt ::= .
164911** distance_opt ::= / INTEGER.
164912**
164913** phrase ::= TOKEN.
164914** phrase ::= COLUMN:TOKEN.
164915** phrase ::= "TOKEN TOKEN TOKEN...".
164916*/
164917
164918#ifdef SQLITE_TEST
164919SQLITE_API int sqlite3_fts3_enable_parentheses = 0;
164920#else
164921# ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
164922# define sqlite3_fts3_enable_parentheses 1
164923# else
164924# define sqlite3_fts3_enable_parentheses 0
164925# endif
164926#endif
164927
164928/*
164929** Default span for NEAR operators.
164930*/
164931#define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10
164932
164933/* #include <string.h> */
164934/* #include <assert.h> */
164935
164936/*
164937** isNot:
164938** This variable is used by function getNextNode(). When getNextNode() is
164939** called, it sets ParseContext.isNot to true if the 'next node' is a
164940** FTSQUERY_PHRASE with a unary "-" attached to it. i.e. "mysql" in the
164941** FTS3 query "sqlite -mysql". Otherwise, ParseContext.isNot is set to
164942** zero.
164943*/
164944typedef struct ParseContext ParseContext;
164945struct ParseContext {
164946 sqlite3_tokenizer *pTokenizer; /* Tokenizer module */
164947 int iLangid; /* Language id used with tokenizer */
164948 const char **azCol; /* Array of column names for fts3 table */
164949 int bFts4; /* True to allow FTS4-only syntax */
164950 int nCol; /* Number of entries in azCol[] */
164951 int iDefaultCol; /* Default column to query */
164952 int isNot; /* True if getNextNode() sees a unary - */
164953 sqlite3_context *pCtx; /* Write error message here */
164954 int nNest; /* Number of nested brackets */
164955};
164956
164957/*
164958** This function is equivalent to the standard isspace() function.
164959**
164960** The standard isspace() can be awkward to use safely, because although it
164961** is defined to accept an argument of type int, its behavior when passed
164962** an integer that falls outside of the range of the unsigned char type
164963** is undefined (and sometimes, "undefined" means segfault). This wrapper
164964** is defined to accept an argument of type char, and always returns 0 for
164965** any values that fall outside of the range of the unsigned char type (i.e.
164966** negative values).
164967*/
164968static int fts3isspace(char c){
164969 return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
164970}
164971
164972/*
164973** Allocate nByte bytes of memory using sqlite3_malloc(). If successful,
164974** zero the memory before returning a pointer to it. If unsuccessful,
164975** return NULL.
164976*/
164977static void *fts3MallocZero(int nByte){
164978 void *pRet = sqlite3_malloc(nByte);
164979 if( pRet ) memset(pRet, 0, nByte);
164980 return pRet;
164981}
164982
164983SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(
164984 sqlite3_tokenizer *pTokenizer,
164985 int iLangid,
164986 const char *z,
164987 int n,
164988 sqlite3_tokenizer_cursor **ppCsr
164989){
164990 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
164991 sqlite3_tokenizer_cursor *pCsr = 0;
164992 int rc;
164993
164994 rc = pModule->xOpen(pTokenizer, z, n, &pCsr);
164995 assert( rc==SQLITE_OK || pCsr==0 );
164996 if( rc==SQLITE_OK ){
164997 pCsr->pTokenizer = pTokenizer;
164998 if( pModule->iVersion>=1 ){
164999 rc = pModule->xLanguageid(pCsr, iLangid);
165000 if( rc!=SQLITE_OK ){
165001 pModule->xClose(pCsr);
165002 pCsr = 0;
165003 }
165004 }
165005 }
165006 *ppCsr = pCsr;
165007 return rc;
165008}
165009
165010/*
165011** Function getNextNode(), which is called by fts3ExprParse(), may itself
165012** call fts3ExprParse(). So this forward declaration is required.
165013*/
165014static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *);
165015
165016/*
165017** Extract the next token from buffer z (length n) using the tokenizer
165018** and other information (column names etc.) in pParse. Create an Fts3Expr
165019** structure of type FTSQUERY_PHRASE containing a phrase consisting of this
165020** single token and set *ppExpr to point to it. If the end of the buffer is
165021** reached before a token is found, set *ppExpr to zero. It is the
165022** responsibility of the caller to eventually deallocate the allocated
165023** Fts3Expr structure (if any) by passing it to sqlite3_free().
165024**
165025** Return SQLITE_OK if successful, or SQLITE_NOMEM if a memory allocation
165026** fails.
165027*/
165028static int getNextToken(
165029 ParseContext *pParse, /* fts3 query parse context */
165030 int iCol, /* Value for Fts3Phrase.iColumn */
165031 const char *z, int n, /* Input string */
165032 Fts3Expr **ppExpr, /* OUT: expression */
165033 int *pnConsumed /* OUT: Number of bytes consumed */
165034){
165035 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
165036 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
165037 int rc;
165038 sqlite3_tokenizer_cursor *pCursor;
165039 Fts3Expr *pRet = 0;
165040 int i = 0;
165041
165042 /* Set variable i to the maximum number of bytes of input to tokenize. */
165043 for(i=0; i<n; i++){
165044 if( sqlite3_fts3_enable_parentheses && (z[i]=='(' || z[i]==')') ) break;
165045 if( z[i]=='"' ) break;
165046 }
165047
165048 *pnConsumed = i;
165049 rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, i, &pCursor);
165050 if( rc==SQLITE_OK ){
165051 const char *zToken;
165052 int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0;
165053 int nByte; /* total space to allocate */
165054
165055 rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
165056 if( rc==SQLITE_OK ){
165057 nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken;
165058 pRet = (Fts3Expr *)fts3MallocZero(nByte);
165059 if( !pRet ){
165060 rc = SQLITE_NOMEM;
165061 }else{
165062 pRet->eType = FTSQUERY_PHRASE;
165063 pRet->pPhrase = (Fts3Phrase *)&pRet[1];
165064 pRet->pPhrase->nToken = 1;
165065 pRet->pPhrase->iColumn = iCol;
165066 pRet->pPhrase->aToken[0].n = nToken;
165067 pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1];
165068 memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken);
165069
165070 if( iEnd<n && z[iEnd]=='*' ){
165071 pRet->pPhrase->aToken[0].isPrefix = 1;
165072 iEnd++;
165073 }
165074
165075 while( 1 ){
165076 if( !sqlite3_fts3_enable_parentheses
165077 && iStart>0 && z[iStart-1]=='-'
165078 ){
165079 pParse->isNot = 1;
165080 iStart--;
165081 }else if( pParse->bFts4 && iStart>0 && z[iStart-1]=='^' ){
165082 pRet->pPhrase->aToken[0].bFirst = 1;
165083 iStart--;
165084 }else{
165085 break;
165086 }
165087 }
165088
165089 }
165090 *pnConsumed = iEnd;
165091 }else if( i && rc==SQLITE_DONE ){
165092 rc = SQLITE_OK;
165093 }
165094
165095 pModule->xClose(pCursor);
165096 }
165097
165098 *ppExpr = pRet;
165099 return rc;
165100}
165101
165102
165103/*
165104** Enlarge a memory allocation. If an out-of-memory allocation occurs,
165105** then free the old allocation.
165106*/
165107static void *fts3ReallocOrFree(void *pOrig, int nNew){
165108 void *pRet = sqlite3_realloc(pOrig, nNew);
165109 if( !pRet ){
165110 sqlite3_free(pOrig);
165111 }
165112 return pRet;
165113}
165114
165115/*
165116** Buffer zInput, length nInput, contains the contents of a quoted string
165117** that appeared as part of an fts3 query expression. Neither quote character
165118** is included in the buffer. This function attempts to tokenize the entire
165119** input buffer and create an Fts3Expr structure of type FTSQUERY_PHRASE
165120** containing the results.
165121**
165122** If successful, SQLITE_OK is returned and *ppExpr set to point at the
165123** allocated Fts3Expr structure. Otherwise, either SQLITE_NOMEM (out of memory
165124** error) or SQLITE_ERROR (tokenization error) is returned and *ppExpr set
165125** to 0.
165126*/
165127static int getNextString(
165128 ParseContext *pParse, /* fts3 query parse context */
165129 const char *zInput, int nInput, /* Input string */
165130 Fts3Expr **ppExpr /* OUT: expression */
165131){
165132 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
165133 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
165134 int rc;
165135 Fts3Expr *p = 0;
165136 sqlite3_tokenizer_cursor *pCursor = 0;
165137 char *zTemp = 0;
165138 int nTemp = 0;
165139
165140 const int nSpace = sizeof(Fts3Expr) + sizeof(Fts3Phrase);
165141 int nToken = 0;
165142
165143 /* The final Fts3Expr data structure, including the Fts3Phrase,
165144 ** Fts3PhraseToken structures token buffers are all stored as a single
165145 ** allocation so that the expression can be freed with a single call to
165146 ** sqlite3_free(). Setting this up requires a two pass approach.
165147 **
165148 ** The first pass, in the block below, uses a tokenizer cursor to iterate
165149 ** through the tokens in the expression. This pass uses fts3ReallocOrFree()
165150 ** to assemble data in two dynamic buffers:
165151 **
165152 ** Buffer p: Points to the Fts3Expr structure, followed by the Fts3Phrase
165153 ** structure, followed by the array of Fts3PhraseToken
165154 ** structures. This pass only populates the Fts3PhraseToken array.
165155 **
165156 ** Buffer zTemp: Contains copies of all tokens.
165157 **
165158 ** The second pass, in the block that begins "if( rc==SQLITE_DONE )" below,
165159 ** appends buffer zTemp to buffer p, and fills in the Fts3Expr and Fts3Phrase
165160 ** structures.
165161 */
165162 rc = sqlite3Fts3OpenTokenizer(
165163 pTokenizer, pParse->iLangid, zInput, nInput, &pCursor);
165164 if( rc==SQLITE_OK ){
165165 int ii;
165166 for(ii=0; rc==SQLITE_OK; ii++){
165167 const char *zByte;
165168 int nByte = 0, iBegin = 0, iEnd = 0, iPos = 0;
165169 rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
165170 if( rc==SQLITE_OK ){
165171 Fts3PhraseToken *pToken;
165172
165173 p = fts3ReallocOrFree(p, nSpace + ii*sizeof(Fts3PhraseToken));
165174 if( !p ) goto no_mem;
165175
165176 zTemp = fts3ReallocOrFree(zTemp, nTemp + nByte);
165177 if( !zTemp ) goto no_mem;
165178
165179 assert( nToken==ii );
165180 pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii];
165181 memset(pToken, 0, sizeof(Fts3PhraseToken));
165182
165183 memcpy(&zTemp[nTemp], zByte, nByte);
165184 nTemp += nByte;
165185
165186 pToken->n = nByte;
165187 pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*');
165188 pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^');
165189 nToken = ii+1;
165190 }
165191 }
165192
165193 pModule->xClose(pCursor);
165194 pCursor = 0;
165195 }
165196
165197 if( rc==SQLITE_DONE ){
165198 int jj;
165199 char *zBuf = 0;
165200
165201 p = fts3ReallocOrFree(p, nSpace + nToken*sizeof(Fts3PhraseToken) + nTemp);
165202 if( !p ) goto no_mem;
165203 memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p);
165204 p->eType = FTSQUERY_PHRASE;
165205 p->pPhrase = (Fts3Phrase *)&p[1];
165206 p->pPhrase->iColumn = pParse->iDefaultCol;
165207 p->pPhrase->nToken = nToken;
165208
165209 zBuf = (char *)&p->pPhrase->aToken[nToken];
165210 if( zTemp ){
165211 memcpy(zBuf, zTemp, nTemp);
165212 sqlite3_free(zTemp);
165213 }else{
165214 assert( nTemp==0 );
165215 }
165216
165217 for(jj=0; jj<p->pPhrase->nToken; jj++){
165218 p->pPhrase->aToken[jj].z = zBuf;
165219 zBuf += p->pPhrase->aToken[jj].n;
165220 }
165221 rc = SQLITE_OK;
165222 }
165223
165224 *ppExpr = p;
165225 return rc;
165226no_mem:
165227
165228 if( pCursor ){
165229 pModule->xClose(pCursor);
165230 }
165231 sqlite3_free(zTemp);
165232 sqlite3_free(p);
165233 *ppExpr = 0;
165234 return SQLITE_NOMEM;
165235}
165236
165237/*
165238** The output variable *ppExpr is populated with an allocated Fts3Expr
165239** structure, or set to 0 if the end of the input buffer is reached.
165240**
165241** Returns an SQLite error code. SQLITE_OK if everything works, SQLITE_NOMEM
165242** if a malloc failure occurs, or SQLITE_ERROR if a parse error is encountered.
165243** If SQLITE_ERROR is returned, pContext is populated with an error message.
165244*/
165245static int getNextNode(
165246 ParseContext *pParse, /* fts3 query parse context */
165247 const char *z, int n, /* Input string */
165248 Fts3Expr **ppExpr, /* OUT: expression */
165249 int *pnConsumed /* OUT: Number of bytes consumed */
165250){
165251 static const struct Fts3Keyword {
165252 char *z; /* Keyword text */
165253 unsigned char n; /* Length of the keyword */
165254 unsigned char parenOnly; /* Only valid in paren mode */
165255 unsigned char eType; /* Keyword code */
165256 } aKeyword[] = {
165257 { "OR" , 2, 0, FTSQUERY_OR },
165258 { "AND", 3, 1, FTSQUERY_AND },
165259 { "NOT", 3, 1, FTSQUERY_NOT },
165260 { "NEAR", 4, 0, FTSQUERY_NEAR }
165261 };
165262 int ii;
165263 int iCol;
165264 int iColLen;
165265 int rc;
165266 Fts3Expr *pRet = 0;
165267
165268 const char *zInput = z;
165269 int nInput = n;
165270
165271 pParse->isNot = 0;
165272
165273 /* Skip over any whitespace before checking for a keyword, an open or
165274 ** close bracket, or a quoted string.
165275 */
165276 while( nInput>0 && fts3isspace(*zInput) ){
165277 nInput--;
165278 zInput++;
165279 }
165280 if( nInput==0 ){
165281 return SQLITE_DONE;
165282 }
165283
165284 /* See if we are dealing with a keyword. */
165285 for(ii=0; ii<(int)(sizeof(aKeyword)/sizeof(struct Fts3Keyword)); ii++){
165286 const struct Fts3Keyword *pKey = &aKeyword[ii];
165287
165288 if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses)!=0 ){
165289 continue;
165290 }
165291
165292 if( nInput>=pKey->n && 0==memcmp(zInput, pKey->z, pKey->n) ){
165293 int nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM;
165294 int nKey = pKey->n;
165295 char cNext;
165296
165297 /* If this is a "NEAR" keyword, check for an explicit nearness. */
165298 if( pKey->eType==FTSQUERY_NEAR ){
165299 assert( nKey==4 );
165300 if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){
165301 nNear = 0;
165302 for(nKey=5; zInput[nKey]>='0' && zInput[nKey]<='9'; nKey++){
165303 nNear = nNear * 10 + (zInput[nKey] - '0');
165304 }
165305 }
165306 }
165307
165308 /* At this point this is probably a keyword. But for that to be true,
165309 ** the next byte must contain either whitespace, an open or close
165310 ** parenthesis, a quote character, or EOF.
165311 */
165312 cNext = zInput[nKey];
165313 if( fts3isspace(cNext)
165314 || cNext=='"' || cNext=='(' || cNext==')' || cNext==0
165315 ){
165316 pRet = (Fts3Expr *)fts3MallocZero(sizeof(Fts3Expr));
165317 if( !pRet ){
165318 return SQLITE_NOMEM;
165319 }
165320 pRet->eType = pKey->eType;
165321 pRet->nNear = nNear;
165322 *ppExpr = pRet;
165323 *pnConsumed = (int)((zInput - z) + nKey);
165324 return SQLITE_OK;
165325 }
165326
165327 /* Turns out that wasn't a keyword after all. This happens if the
165328 ** user has supplied a token such as "ORacle". Continue.
165329 */
165330 }
165331 }
165332
165333 /* See if we are dealing with a quoted phrase. If this is the case, then
165334 ** search for the closing quote and pass the whole string to getNextString()
165335 ** for processing. This is easy to do, as fts3 has no syntax for escaping
165336 ** a quote character embedded in a string.
165337 */
165338 if( *zInput=='"' ){
165339 for(ii=1; ii<nInput && zInput[ii]!='"'; ii++);
165340 *pnConsumed = (int)((zInput - z) + ii + 1);
165341 if( ii==nInput ){
165342 return SQLITE_ERROR;
165343 }
165344 return getNextString(pParse, &zInput[1], ii-1, ppExpr);
165345 }
165346
165347 if( sqlite3_fts3_enable_parentheses ){
165348 if( *zInput=='(' ){
165349 int nConsumed = 0;
165350 pParse->nNest++;
165351 rc = fts3ExprParse(pParse, zInput+1, nInput-1, ppExpr, &nConsumed);
165352 if( rc==SQLITE_OK && !*ppExpr ){ rc = SQLITE_DONE; }
165353 *pnConsumed = (int)(zInput - z) + 1 + nConsumed;
165354 return rc;
165355 }else if( *zInput==')' ){
165356 pParse->nNest--;
165357 *pnConsumed = (int)((zInput - z) + 1);
165358 *ppExpr = 0;
165359 return SQLITE_DONE;
165360 }
165361 }
165362
165363 /* If control flows to this point, this must be a regular token, or
165364 ** the end of the input. Read a regular token using the sqlite3_tokenizer
165365 ** interface. Before doing so, figure out if there is an explicit
165366 ** column specifier for the token.
165367 **
165368 ** TODO: Strangely, it is not possible to associate a column specifier
165369 ** with a quoted phrase, only with a single token. Not sure if this was
165370 ** an implementation artifact or an intentional decision when fts3 was
165371 ** first implemented. Whichever it was, this module duplicates the
165372 ** limitation.
165373 */
165374 iCol = pParse->iDefaultCol;
165375 iColLen = 0;
165376 for(ii=0; ii<pParse->nCol; ii++){
165377 const char *zStr = pParse->azCol[ii];
165378 int nStr = (int)strlen(zStr);
165379 if( nInput>nStr && zInput[nStr]==':'
165380 && sqlite3_strnicmp(zStr, zInput, nStr)==0
165381 ){
165382 iCol = ii;
165383 iColLen = (int)((zInput - z) + nStr + 1);
165384 break;
165385 }
165386 }
165387 rc = getNextToken(pParse, iCol, &z[iColLen], n-iColLen, ppExpr, pnConsumed);
165388 *pnConsumed += iColLen;
165389 return rc;
165390}
165391
165392/*
165393** The argument is an Fts3Expr structure for a binary operator (any type
165394** except an FTSQUERY_PHRASE). Return an integer value representing the
165395** precedence of the operator. Lower values have a higher precedence (i.e.
165396** group more tightly). For example, in the C language, the == operator
165397** groups more tightly than ||, and would therefore have a higher precedence.
165398**
165399** When using the new fts3 query syntax (when SQLITE_ENABLE_FTS3_PARENTHESIS
165400** is defined), the order of the operators in precedence from highest to
165401** lowest is:
165402**
165403** NEAR
165404** NOT
165405** AND (including implicit ANDs)
165406** OR
165407**
165408** Note that when using the old query syntax, the OR operator has a higher
165409** precedence than the AND operator.
165410*/
165411static int opPrecedence(Fts3Expr *p){
165412 assert( p->eType!=FTSQUERY_PHRASE );
165413 if( sqlite3_fts3_enable_parentheses ){
165414 return p->eType;
165415 }else if( p->eType==FTSQUERY_NEAR ){
165416 return 1;
165417 }else if( p->eType==FTSQUERY_OR ){
165418 return 2;
165419 }
165420 assert( p->eType==FTSQUERY_AND );
165421 return 3;
165422}
165423
165424/*
165425** Argument ppHead contains a pointer to the current head of a query
165426** expression tree being parsed. pPrev is the expression node most recently
165427** inserted into the tree. This function adds pNew, which is always a binary
165428** operator node, into the expression tree based on the relative precedence
165429** of pNew and the existing nodes of the tree. This may result in the head
165430** of the tree changing, in which case *ppHead is set to the new root node.
165431*/
165432static void insertBinaryOperator(
165433 Fts3Expr **ppHead, /* Pointer to the root node of a tree */
165434 Fts3Expr *pPrev, /* Node most recently inserted into the tree */
165435 Fts3Expr *pNew /* New binary node to insert into expression tree */
165436){
165437 Fts3Expr *pSplit = pPrev;
165438 while( pSplit->pParent && opPrecedence(pSplit->pParent)<=opPrecedence(pNew) ){
165439 pSplit = pSplit->pParent;
165440 }
165441
165442 if( pSplit->pParent ){
165443 assert( pSplit->pParent->pRight==pSplit );
165444 pSplit->pParent->pRight = pNew;
165445 pNew->pParent = pSplit->pParent;
165446 }else{
165447 *ppHead = pNew;
165448 }
165449 pNew->pLeft = pSplit;
165450 pSplit->pParent = pNew;
165451}
165452
165453/*
165454** Parse the fts3 query expression found in buffer z, length n. This function
165455** returns either when the end of the buffer is reached or an unmatched
165456** closing bracket - ')' - is encountered.
165457**
165458** If successful, SQLITE_OK is returned, *ppExpr is set to point to the
165459** parsed form of the expression and *pnConsumed is set to the number of
165460** bytes read from buffer z. Otherwise, *ppExpr is set to 0 and SQLITE_NOMEM
165461** (out of memory error) or SQLITE_ERROR (parse error) is returned.
165462*/
165463static int fts3ExprParse(
165464 ParseContext *pParse, /* fts3 query parse context */
165465 const char *z, int n, /* Text of MATCH query */
165466 Fts3Expr **ppExpr, /* OUT: Parsed query structure */
165467 int *pnConsumed /* OUT: Number of bytes consumed */
165468){
165469 Fts3Expr *pRet = 0;
165470 Fts3Expr *pPrev = 0;
165471 Fts3Expr *pNotBranch = 0; /* Only used in legacy parse mode */
165472 int nIn = n;
165473 const char *zIn = z;
165474 int rc = SQLITE_OK;
165475 int isRequirePhrase = 1;
165476
165477 while( rc==SQLITE_OK ){
165478 Fts3Expr *p = 0;
165479 int nByte = 0;
165480
165481 rc = getNextNode(pParse, zIn, nIn, &p, &nByte);
165482 assert( nByte>0 || (rc!=SQLITE_OK && p==0) );
165483 if( rc==SQLITE_OK ){
165484 if( p ){
165485 int isPhrase;
165486
165487 if( !sqlite3_fts3_enable_parentheses
165488 && p->eType==FTSQUERY_PHRASE && pParse->isNot
165489 ){
165490 /* Create an implicit NOT operator. */
165491 Fts3Expr *pNot = fts3MallocZero(sizeof(Fts3Expr));
165492 if( !pNot ){
165493 sqlite3Fts3ExprFree(p);
165494 rc = SQLITE_NOMEM;
165495 goto exprparse_out;
165496 }
165497 pNot->eType = FTSQUERY_NOT;
165498 pNot->pRight = p;
165499 p->pParent = pNot;
165500 if( pNotBranch ){
165501 pNot->pLeft = pNotBranch;
165502 pNotBranch->pParent = pNot;
165503 }
165504 pNotBranch = pNot;
165505 p = pPrev;
165506 }else{
165507 int eType = p->eType;
165508 isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft);
165509
165510 /* The isRequirePhrase variable is set to true if a phrase or
165511 ** an expression contained in parenthesis is required. If a
165512 ** binary operator (AND, OR, NOT or NEAR) is encounted when
165513 ** isRequirePhrase is set, this is a syntax error.
165514 */
165515 if( !isPhrase && isRequirePhrase ){
165516 sqlite3Fts3ExprFree(p);
165517 rc = SQLITE_ERROR;
165518 goto exprparse_out;
165519 }
165520
165521 if( isPhrase && !isRequirePhrase ){
165522 /* Insert an implicit AND operator. */
165523 Fts3Expr *pAnd;
165524 assert( pRet && pPrev );
165525 pAnd = fts3MallocZero(sizeof(Fts3Expr));
165526 if( !pAnd ){
165527 sqlite3Fts3ExprFree(p);
165528 rc = SQLITE_NOMEM;
165529 goto exprparse_out;
165530 }
165531 pAnd->eType = FTSQUERY_AND;
165532 insertBinaryOperator(&pRet, pPrev, pAnd);
165533 pPrev = pAnd;
165534 }
165535
165536 /* This test catches attempts to make either operand of a NEAR
165537 ** operator something other than a phrase. For example, either of
165538 ** the following:
165539 **
165540 ** (bracketed expression) NEAR phrase
165541 ** phrase NEAR (bracketed expression)
165542 **
165543 ** Return an error in either case.
165544 */
165545 if( pPrev && (
165546 (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE)
165547 || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR)
165548 )){
165549 sqlite3Fts3ExprFree(p);
165550 rc = SQLITE_ERROR;
165551 goto exprparse_out;
165552 }
165553
165554 if( isPhrase ){
165555 if( pRet ){
165556 assert( pPrev && pPrev->pLeft && pPrev->pRight==0 );
165557 pPrev->pRight = p;
165558 p->pParent = pPrev;
165559 }else{
165560 pRet = p;
165561 }
165562 }else{
165563 insertBinaryOperator(&pRet, pPrev, p);
165564 }
165565 isRequirePhrase = !isPhrase;
165566 }
165567 pPrev = p;
165568 }
165569 assert( nByte>0 );
165570 }
165571 assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) );
165572 nIn -= nByte;
165573 zIn += nByte;
165574 }
165575
165576 if( rc==SQLITE_DONE && pRet && isRequirePhrase ){
165577 rc = SQLITE_ERROR;
165578 }
165579
165580 if( rc==SQLITE_DONE ){
165581 rc = SQLITE_OK;
165582 if( !sqlite3_fts3_enable_parentheses && pNotBranch ){
165583 if( !pRet ){
165584 rc = SQLITE_ERROR;
165585 }else{
165586 Fts3Expr *pIter = pNotBranch;
165587 while( pIter->pLeft ){
165588 pIter = pIter->pLeft;
165589 }
165590 pIter->pLeft = pRet;
165591 pRet->pParent = pIter;
165592 pRet = pNotBranch;
165593 }
165594 }
165595 }
165596 *pnConsumed = n - nIn;
165597
165598exprparse_out:
165599 if( rc!=SQLITE_OK ){
165600 sqlite3Fts3ExprFree(pRet);
165601 sqlite3Fts3ExprFree(pNotBranch);
165602 pRet = 0;
165603 }
165604 *ppExpr = pRet;
165605 return rc;
165606}
165607
165608/*
165609** Return SQLITE_ERROR if the maximum depth of the expression tree passed
165610** as the only argument is more than nMaxDepth.
165611*/
165612static int fts3ExprCheckDepth(Fts3Expr *p, int nMaxDepth){
165613 int rc = SQLITE_OK;
165614 if( p ){
165615 if( nMaxDepth<0 ){
165616 rc = SQLITE_TOOBIG;
165617 }else{
165618 rc = fts3ExprCheckDepth(p->pLeft, nMaxDepth-1);
165619 if( rc==SQLITE_OK ){
165620 rc = fts3ExprCheckDepth(p->pRight, nMaxDepth-1);
165621 }
165622 }
165623 }
165624 return rc;
165625}
165626
165627/*
165628** This function attempts to transform the expression tree at (*pp) to
165629** an equivalent but more balanced form. The tree is modified in place.
165630** If successful, SQLITE_OK is returned and (*pp) set to point to the
165631** new root expression node.
165632**
165633** nMaxDepth is the maximum allowable depth of the balanced sub-tree.
165634**
165635** Otherwise, if an error occurs, an SQLite error code is returned and
165636** expression (*pp) freed.
165637*/
165638static int fts3ExprBalance(Fts3Expr **pp, int nMaxDepth){
165639 int rc = SQLITE_OK; /* Return code */
165640 Fts3Expr *pRoot = *pp; /* Initial root node */
165641 Fts3Expr *pFree = 0; /* List of free nodes. Linked by pParent. */
165642 int eType = pRoot->eType; /* Type of node in this tree */
165643
165644 if( nMaxDepth==0 ){
165645 rc = SQLITE_ERROR;
165646 }
165647
165648 if( rc==SQLITE_OK ){
165649 if( (eType==FTSQUERY_AND || eType==FTSQUERY_OR) ){
165650 Fts3Expr **apLeaf;
165651 apLeaf = (Fts3Expr **)sqlite3_malloc(sizeof(Fts3Expr *) * nMaxDepth);
165652 if( 0==apLeaf ){
165653 rc = SQLITE_NOMEM;
165654 }else{
165655 memset(apLeaf, 0, sizeof(Fts3Expr *) * nMaxDepth);
165656 }
165657
165658 if( rc==SQLITE_OK ){
165659 int i;
165660 Fts3Expr *p;
165661
165662 /* Set $p to point to the left-most leaf in the tree of eType nodes. */
165663 for(p=pRoot; p->eType==eType; p=p->pLeft){
165664 assert( p->pParent==0 || p->pParent->pLeft==p );
165665 assert( p->pLeft && p->pRight );
165666 }
165667
165668 /* This loop runs once for each leaf in the tree of eType nodes. */
165669 while( 1 ){
165670 int iLvl;
165671 Fts3Expr *pParent = p->pParent; /* Current parent of p */
165672
165673 assert( pParent==0 || pParent->pLeft==p );
165674 p->pParent = 0;
165675 if( pParent ){
165676 pParent->pLeft = 0;
165677 }else{
165678 pRoot = 0;
165679 }
165680 rc = fts3ExprBalance(&p, nMaxDepth-1);
165681 if( rc!=SQLITE_OK ) break;
165682
165683 for(iLvl=0; p && iLvl<nMaxDepth; iLvl++){
165684 if( apLeaf[iLvl]==0 ){
165685 apLeaf[iLvl] = p;
165686 p = 0;
165687 }else{
165688 assert( pFree );
165689 pFree->pLeft = apLeaf[iLvl];
165690 pFree->pRight = p;
165691 pFree->pLeft->pParent = pFree;
165692 pFree->pRight->pParent = pFree;
165693
165694 p = pFree;
165695 pFree = pFree->pParent;
165696 p->pParent = 0;
165697 apLeaf[iLvl] = 0;
165698 }
165699 }
165700 if( p ){
165701 sqlite3Fts3ExprFree(p);
165702 rc = SQLITE_TOOBIG;
165703 break;
165704 }
165705
165706 /* If that was the last leaf node, break out of the loop */
165707 if( pParent==0 ) break;
165708
165709 /* Set $p to point to the next leaf in the tree of eType nodes */
165710 for(p=pParent->pRight; p->eType==eType; p=p->pLeft);
165711
165712 /* Remove pParent from the original tree. */
165713 assert( pParent->pParent==0 || pParent->pParent->pLeft==pParent );
165714 pParent->pRight->pParent = pParent->pParent;
165715 if( pParent->pParent ){
165716 pParent->pParent->pLeft = pParent->pRight;
165717 }else{
165718 assert( pParent==pRoot );
165719 pRoot = pParent->pRight;
165720 }
165721
165722 /* Link pParent into the free node list. It will be used as an
165723 ** internal node of the new tree. */
165724 pParent->pParent = pFree;
165725 pFree = pParent;
165726 }
165727
165728 if( rc==SQLITE_OK ){
165729 p = 0;
165730 for(i=0; i<nMaxDepth; i++){
165731 if( apLeaf[i] ){
165732 if( p==0 ){
165733 p = apLeaf[i];
165734 p->pParent = 0;
165735 }else{
165736 assert( pFree!=0 );
165737 pFree->pRight = p;
165738 pFree->pLeft = apLeaf[i];
165739 pFree->pLeft->pParent = pFree;
165740 pFree->pRight->pParent = pFree;
165741
165742 p = pFree;
165743 pFree = pFree->pParent;
165744 p->pParent = 0;
165745 }
165746 }
165747 }
165748 pRoot = p;
165749 }else{
165750 /* An error occurred. Delete the contents of the apLeaf[] array
165751 ** and pFree list. Everything else is cleaned up by the call to
165752 ** sqlite3Fts3ExprFree(pRoot) below. */
165753 Fts3Expr *pDel;
165754 for(i=0; i<nMaxDepth; i++){
165755 sqlite3Fts3ExprFree(apLeaf[i]);
165756 }
165757 while( (pDel=pFree)!=0 ){
165758 pFree = pDel->pParent;
165759 sqlite3_free(pDel);
165760 }
165761 }
165762
165763 assert( pFree==0 );
165764 sqlite3_free( apLeaf );
165765 }
165766 }else if( eType==FTSQUERY_NOT ){
165767 Fts3Expr *pLeft = pRoot->pLeft;
165768 Fts3Expr *pRight = pRoot->pRight;
165769
165770 pRoot->pLeft = 0;
165771 pRoot->pRight = 0;
165772 pLeft->pParent = 0;
165773 pRight->pParent = 0;
165774
165775 rc = fts3ExprBalance(&pLeft, nMaxDepth-1);
165776 if( rc==SQLITE_OK ){
165777 rc = fts3ExprBalance(&pRight, nMaxDepth-1);
165778 }
165779
165780 if( rc!=SQLITE_OK ){
165781 sqlite3Fts3ExprFree(pRight);
165782 sqlite3Fts3ExprFree(pLeft);
165783 }else{
165784 assert( pLeft && pRight );
165785 pRoot->pLeft = pLeft;
165786 pLeft->pParent = pRoot;
165787 pRoot->pRight = pRight;
165788 pRight->pParent = pRoot;
165789 }
165790 }
165791 }
165792
165793 if( rc!=SQLITE_OK ){
165794 sqlite3Fts3ExprFree(pRoot);
165795 pRoot = 0;
165796 }
165797 *pp = pRoot;
165798 return rc;
165799}
165800
165801/*
165802** This function is similar to sqlite3Fts3ExprParse(), with the following
165803** differences:
165804**
165805** 1. It does not do expression rebalancing.
165806** 2. It does not check that the expression does not exceed the
165807** maximum allowable depth.
165808** 3. Even if it fails, *ppExpr may still be set to point to an
165809** expression tree. It should be deleted using sqlite3Fts3ExprFree()
165810** in this case.
165811*/
165812static int fts3ExprParseUnbalanced(
165813 sqlite3_tokenizer *pTokenizer, /* Tokenizer module */
165814 int iLangid, /* Language id for tokenizer */
165815 char **azCol, /* Array of column names for fts3 table */
165816 int bFts4, /* True to allow FTS4-only syntax */
165817 int nCol, /* Number of entries in azCol[] */
165818 int iDefaultCol, /* Default column to query */
165819 const char *z, int n, /* Text of MATCH query */
165820 Fts3Expr **ppExpr /* OUT: Parsed query structure */
165821){
165822 int nParsed;
165823 int rc;
165824 ParseContext sParse;
165825
165826 memset(&sParse, 0, sizeof(ParseContext));
165827 sParse.pTokenizer = pTokenizer;
165828 sParse.iLangid = iLangid;
165829 sParse.azCol = (const char **)azCol;
165830 sParse.nCol = nCol;
165831 sParse.iDefaultCol = iDefaultCol;
165832 sParse.bFts4 = bFts4;
165833 if( z==0 ){
165834 *ppExpr = 0;
165835 return SQLITE_OK;
165836 }
165837 if( n<0 ){
165838 n = (int)strlen(z);
165839 }
165840 rc = fts3ExprParse(&sParse, z, n, ppExpr, &nParsed);
165841 assert( rc==SQLITE_OK || *ppExpr==0 );
165842
165843 /* Check for mismatched parenthesis */
165844 if( rc==SQLITE_OK && sParse.nNest ){
165845 rc = SQLITE_ERROR;
165846 }
165847
165848 return rc;
165849}
165850
165851/*
165852** Parameters z and n contain a pointer to and length of a buffer containing
165853** an fts3 query expression, respectively. This function attempts to parse the
165854** query expression and create a tree of Fts3Expr structures representing the
165855** parsed expression. If successful, *ppExpr is set to point to the head
165856** of the parsed expression tree and SQLITE_OK is returned. If an error
165857** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
165858** error) is returned and *ppExpr is set to 0.
165859**
165860** If parameter n is a negative number, then z is assumed to point to a
165861** nul-terminated string and the length is determined using strlen().
165862**
165863** The first parameter, pTokenizer, is passed the fts3 tokenizer module to
165864** use to normalize query tokens while parsing the expression. The azCol[]
165865** array, which is assumed to contain nCol entries, should contain the names
165866** of each column in the target fts3 table, in order from left to right.
165867** Column names must be nul-terminated strings.
165868**
165869** The iDefaultCol parameter should be passed the index of the table column
165870** that appears on the left-hand-side of the MATCH operator (the default
165871** column to match against for tokens for which a column name is not explicitly
165872** specified as part of the query string), or -1 if tokens may by default
165873** match any table column.
165874*/
165875SQLITE_PRIVATE int sqlite3Fts3ExprParse(
165876 sqlite3_tokenizer *pTokenizer, /* Tokenizer module */
165877 int iLangid, /* Language id for tokenizer */
165878 char **azCol, /* Array of column names for fts3 table */
165879 int bFts4, /* True to allow FTS4-only syntax */
165880 int nCol, /* Number of entries in azCol[] */
165881 int iDefaultCol, /* Default column to query */
165882 const char *z, int n, /* Text of MATCH query */
165883 Fts3Expr **ppExpr, /* OUT: Parsed query structure */
165884 char **pzErr /* OUT: Error message (sqlite3_malloc) */
165885){
165886 int rc = fts3ExprParseUnbalanced(
165887 pTokenizer, iLangid, azCol, bFts4, nCol, iDefaultCol, z, n, ppExpr
165888 );
165889
165890 /* Rebalance the expression. And check that its depth does not exceed
165891 ** SQLITE_FTS3_MAX_EXPR_DEPTH. */
165892 if( rc==SQLITE_OK && *ppExpr ){
165893 rc = fts3ExprBalance(ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH);
165894 if( rc==SQLITE_OK ){
165895 rc = fts3ExprCheckDepth(*ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH);
165896 }
165897 }
165898
165899 if( rc!=SQLITE_OK ){
165900 sqlite3Fts3ExprFree(*ppExpr);
165901 *ppExpr = 0;
165902 if( rc==SQLITE_TOOBIG ){
165903 sqlite3Fts3ErrMsg(pzErr,
165904 "FTS expression tree is too large (maximum depth %d)",
165905 SQLITE_FTS3_MAX_EXPR_DEPTH
165906 );
165907 rc = SQLITE_ERROR;
165908 }else if( rc==SQLITE_ERROR ){
165909 sqlite3Fts3ErrMsg(pzErr, "malformed MATCH expression: [%s]", z);
165910 }
165911 }
165912
165913 return rc;
165914}
165915
165916/*
165917** Free a single node of an expression tree.
165918*/
165919static void fts3FreeExprNode(Fts3Expr *p){
165920 assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 );
165921 sqlite3Fts3EvalPhraseCleanup(p->pPhrase);
165922 sqlite3_free(p->aMI);
165923 sqlite3_free(p);
165924}
165925
165926/*
165927** Free a parsed fts3 query expression allocated by sqlite3Fts3ExprParse().
165928**
165929** This function would be simpler if it recursively called itself. But
165930** that would mean passing a sufficiently large expression to ExprParse()
165931** could cause a stack overflow.
165932*/
165933SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *pDel){
165934 Fts3Expr *p;
165935 assert( pDel==0 || pDel->pParent==0 );
165936 for(p=pDel; p && (p->pLeft||p->pRight); p=(p->pLeft ? p->pLeft : p->pRight)){
165937 assert( p->pParent==0 || p==p->pParent->pRight || p==p->pParent->pLeft );
165938 }
165939 while( p ){
165940 Fts3Expr *pParent = p->pParent;
165941 fts3FreeExprNode(p);
165942 if( pParent && p==pParent->pLeft && pParent->pRight ){
165943 p = pParent->pRight;
165944 while( p && (p->pLeft || p->pRight) ){
165945 assert( p==p->pParent->pRight || p==p->pParent->pLeft );
165946 p = (p->pLeft ? p->pLeft : p->pRight);
165947 }
165948 }else{
165949 p = pParent;
165950 }
165951 }
165952}
165953
165954/****************************************************************************
165955*****************************************************************************
165956** Everything after this point is just test code.
165957*/
165958
165959#ifdef SQLITE_TEST
165960
165961/* #include <stdio.h> */
165962
165963/*
165964** Return a pointer to a buffer containing a text representation of the
165965** expression passed as the first argument. The buffer is obtained from
165966** sqlite3_malloc(). It is the responsibility of the caller to use
165967** sqlite3_free() to release the memory. If an OOM condition is encountered,
165968** NULL is returned.
165969**
165970** If the second argument is not NULL, then its contents are prepended to
165971** the returned expression text and then freed using sqlite3_free().
165972*/
165973static char *exprToString(Fts3Expr *pExpr, char *zBuf){
165974 if( pExpr==0 ){
165975 return sqlite3_mprintf("");
165976 }
165977 switch( pExpr->eType ){
165978 case FTSQUERY_PHRASE: {
165979 Fts3Phrase *pPhrase = pExpr->pPhrase;
165980 int i;
165981 zBuf = sqlite3_mprintf(
165982 "%zPHRASE %d 0", zBuf, pPhrase->iColumn);
165983 for(i=0; zBuf && i<pPhrase->nToken; i++){
165984 zBuf = sqlite3_mprintf("%z %.*s%s", zBuf,
165985 pPhrase->aToken[i].n, pPhrase->aToken[i].z,
165986 (pPhrase->aToken[i].isPrefix?"+":"")
165987 );
165988 }
165989 return zBuf;
165990 }
165991
165992 case FTSQUERY_NEAR:
165993 zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear);
165994 break;
165995 case FTSQUERY_NOT:
165996 zBuf = sqlite3_mprintf("%zNOT ", zBuf);
165997 break;
165998 case FTSQUERY_AND:
165999 zBuf = sqlite3_mprintf("%zAND ", zBuf);
166000 break;
166001 case FTSQUERY_OR:
166002 zBuf = sqlite3_mprintf("%zOR ", zBuf);
166003 break;
166004 }
166005
166006 if( zBuf ) zBuf = sqlite3_mprintf("%z{", zBuf);
166007 if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf);
166008 if( zBuf ) zBuf = sqlite3_mprintf("%z} {", zBuf);
166009
166010 if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf);
166011 if( zBuf ) zBuf = sqlite3_mprintf("%z}", zBuf);
166012
166013 return zBuf;
166014}
166015
166016/*
166017** This is the implementation of a scalar SQL function used to test the
166018** expression parser. It should be called as follows:
166019**
166020** fts3_exprtest(<tokenizer>, <expr>, <column 1>, ...);
166021**
166022** The first argument, <tokenizer>, is the name of the fts3 tokenizer used
166023** to parse the query expression (see README.tokenizers). The second argument
166024** is the query expression to parse. Each subsequent argument is the name
166025** of a column of the fts3 table that the query expression may refer to.
166026** For example:
166027**
166028** SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2');
166029*/
166030static void fts3ExprTestCommon(
166031 int bRebalance,
166032 sqlite3_context *context,
166033 int argc,
166034 sqlite3_value **argv
166035){
166036 sqlite3_tokenizer *pTokenizer = 0;
166037 int rc;
166038 char **azCol = 0;
166039 const char *zExpr;
166040 int nExpr;
166041 int nCol;
166042 int ii;
166043 Fts3Expr *pExpr;
166044 char *zBuf = 0;
166045 Fts3Hash *pHash = (Fts3Hash*)sqlite3_user_data(context);
166046 const char *zTokenizer = 0;
166047 char *zErr = 0;
166048
166049 if( argc<3 ){
166050 sqlite3_result_error(context,
166051 "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
166052 );
166053 return;
166054 }
166055
166056 zTokenizer = (const char*)sqlite3_value_text(argv[0]);
166057 rc = sqlite3Fts3InitTokenizer(pHash, zTokenizer, &pTokenizer, &zErr);
166058 if( rc!=SQLITE_OK ){
166059 if( rc==SQLITE_NOMEM ){
166060 sqlite3_result_error_nomem(context);
166061 }else{
166062 sqlite3_result_error(context, zErr, -1);
166063 }
166064 sqlite3_free(zErr);
166065 return;
166066 }
166067
166068 zExpr = (const char *)sqlite3_value_text(argv[1]);
166069 nExpr = sqlite3_value_bytes(argv[1]);
166070 nCol = argc-2;
166071 azCol = (char **)sqlite3_malloc(nCol*sizeof(char *));
166072 if( !azCol ){
166073 sqlite3_result_error_nomem(context);
166074 goto exprtest_out;
166075 }
166076 for(ii=0; ii<nCol; ii++){
166077 azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
166078 }
166079
166080 if( bRebalance ){
166081 char *zDummy = 0;
166082 rc = sqlite3Fts3ExprParse(
166083 pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr, &zDummy
166084 );
166085 assert( rc==SQLITE_OK || pExpr==0 );
166086 sqlite3_free(zDummy);
166087 }else{
166088 rc = fts3ExprParseUnbalanced(
166089 pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr
166090 );
166091 }
166092
166093 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){
166094 sqlite3Fts3ExprFree(pExpr);
166095 sqlite3_result_error(context, "Error parsing expression", -1);
166096 }else if( rc==SQLITE_NOMEM || !(zBuf = exprToString(pExpr, 0)) ){
166097 sqlite3_result_error_nomem(context);
166098 }else{
166099 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
166100 sqlite3_free(zBuf);
166101 }
166102
166103 sqlite3Fts3ExprFree(pExpr);
166104
166105exprtest_out:
166106 if( pTokenizer ){
166107 rc = pTokenizer->pModule->xDestroy(pTokenizer);
166108 }
166109 sqlite3_free(azCol);
166110}
166111
166112static void fts3ExprTest(
166113 sqlite3_context *context,
166114 int argc,
166115 sqlite3_value **argv
166116){
166117 fts3ExprTestCommon(0, context, argc, argv);
166118}
166119static void fts3ExprTestRebalance(
166120 sqlite3_context *context,
166121 int argc,
166122 sqlite3_value **argv
166123){
166124 fts3ExprTestCommon(1, context, argc, argv);
166125}
166126
166127/*
166128** Register the query expression parser test function fts3_exprtest()
166129** with database connection db.
166130*/
166131SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash *pHash){
166132 int rc = sqlite3_create_function(
166133 db, "fts3_exprtest", -1, SQLITE_UTF8, (void*)pHash, fts3ExprTest, 0, 0
166134 );
166135 if( rc==SQLITE_OK ){
166136 rc = sqlite3_create_function(db, "fts3_exprtest_rebalance",
166137 -1, SQLITE_UTF8, (void*)pHash, fts3ExprTestRebalance, 0, 0
166138 );
166139 }
166140 return rc;
166141}
166142
166143#endif
166144#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
166145
166146/************** End of fts3_expr.c *******************************************/
166147/************** Begin file fts3_hash.c ***************************************/
166148/*
166149** 2001 September 22
166150**
166151** The author disclaims copyright to this source code. In place of
166152** a legal notice, here is a blessing:
166153**
166154** May you do good and not evil.
166155** May you find forgiveness for yourself and forgive others.
166156** May you share freely, never taking more than you give.
166157**
166158*************************************************************************
166159** This is the implementation of generic hash-tables used in SQLite.
166160** We've modified it slightly to serve as a standalone hash table
166161** implementation for the full-text indexing module.
166162*/
166163
166164/*
166165** The code in this file is only compiled if:
166166**
166167** * The FTS3 module is being built as an extension
166168** (in which case SQLITE_CORE is not defined), or
166169**
166170** * The FTS3 module is being built into the core of
166171** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
166172*/
166173/* #include "fts3Int.h" */
166174#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
166175
166176/* #include <assert.h> */
166177/* #include <stdlib.h> */
166178/* #include <string.h> */
166179
166180/* #include "fts3_hash.h" */
166181
166182/*
166183** Malloc and Free functions
166184*/
166185static void *fts3HashMalloc(int n){
166186 void *p = sqlite3_malloc(n);
166187 if( p ){
166188 memset(p, 0, n);
166189 }
166190 return p;
166191}
166192static void fts3HashFree(void *p){
166193 sqlite3_free(p);
166194}
166195
166196/* Turn bulk memory into a hash table object by initializing the
166197** fields of the Hash structure.
166198**
166199** "pNew" is a pointer to the hash table that is to be initialized.
166200** keyClass is one of the constants
166201** FTS3_HASH_BINARY or FTS3_HASH_STRING. The value of keyClass
166202** determines what kind of key the hash table will use. "copyKey" is
166203** true if the hash table should make its own private copy of keys and
166204** false if it should just use the supplied pointer.
166205*/
166206SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey){
166207 assert( pNew!=0 );
166208 assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY );
166209 pNew->keyClass = keyClass;
166210 pNew->copyKey = copyKey;
166211 pNew->first = 0;
166212 pNew->count = 0;
166213 pNew->htsize = 0;
166214 pNew->ht = 0;
166215}
166216
166217/* Remove all entries from a hash table. Reclaim all memory.
166218** Call this routine to delete a hash table or to reset a hash table
166219** to the empty state.
166220*/
166221SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash *pH){
166222 Fts3HashElem *elem; /* For looping over all elements of the table */
166223
166224 assert( pH!=0 );
166225 elem = pH->first;
166226 pH->first = 0;
166227 fts3HashFree(pH->ht);
166228 pH->ht = 0;
166229 pH->htsize = 0;
166230 while( elem ){
166231 Fts3HashElem *next_elem = elem->next;
166232 if( pH->copyKey && elem->pKey ){
166233 fts3HashFree(elem->pKey);
166234 }
166235 fts3HashFree(elem);
166236 elem = next_elem;
166237 }
166238 pH->count = 0;
166239}
166240
166241/*
166242** Hash and comparison functions when the mode is FTS3_HASH_STRING
166243*/
166244static int fts3StrHash(const void *pKey, int nKey){
166245 const char *z = (const char *)pKey;
166246 unsigned h = 0;
166247 if( nKey<=0 ) nKey = (int) strlen(z);
166248 while( nKey > 0 ){
166249 h = (h<<3) ^ h ^ *z++;
166250 nKey--;
166251 }
166252 return (int)(h & 0x7fffffff);
166253}
166254static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
166255 if( n1!=n2 ) return 1;
166256 return strncmp((const char*)pKey1,(const char*)pKey2,n1);
166257}
166258
166259/*
166260** Hash and comparison functions when the mode is FTS3_HASH_BINARY
166261*/
166262static int fts3BinHash(const void *pKey, int nKey){
166263 int h = 0;
166264 const char *z = (const char *)pKey;
166265 while( nKey-- > 0 ){
166266 h = (h<<3) ^ h ^ *(z++);
166267 }
166268 return h & 0x7fffffff;
166269}
166270static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){
166271 if( n1!=n2 ) return 1;
166272 return memcmp(pKey1,pKey2,n1);
166273}
166274
166275/*
166276** Return a pointer to the appropriate hash function given the key class.
166277**
166278** The C syntax in this function definition may be unfamilar to some
166279** programmers, so we provide the following additional explanation:
166280**
166281** The name of the function is "ftsHashFunction". The function takes a
166282** single parameter "keyClass". The return value of ftsHashFunction()
166283** is a pointer to another function. Specifically, the return value
166284** of ftsHashFunction() is a pointer to a function that takes two parameters
166285** with types "const void*" and "int" and returns an "int".
166286*/
166287static int (*ftsHashFunction(int keyClass))(const void*,int){
166288 if( keyClass==FTS3_HASH_STRING ){
166289 return &fts3StrHash;
166290 }else{
166291 assert( keyClass==FTS3_HASH_BINARY );
166292 return &fts3BinHash;
166293 }
166294}
166295
166296/*
166297** Return a pointer to the appropriate hash function given the key class.
166298**
166299** For help in interpreted the obscure C code in the function definition,
166300** see the header comment on the previous function.
166301*/
166302static int (*ftsCompareFunction(int keyClass))(const void*,int,const void*,int){
166303 if( keyClass==FTS3_HASH_STRING ){
166304 return &fts3StrCompare;
166305 }else{
166306 assert( keyClass==FTS3_HASH_BINARY );
166307 return &fts3BinCompare;
166308 }
166309}
166310
166311/* Link an element into the hash table
166312*/
166313static void fts3HashInsertElement(
166314 Fts3Hash *pH, /* The complete hash table */
166315 struct _fts3ht *pEntry, /* The entry into which pNew is inserted */
166316 Fts3HashElem *pNew /* The element to be inserted */
166317){
166318 Fts3HashElem *pHead; /* First element already in pEntry */
166319 pHead = pEntry->chain;
166320 if( pHead ){
166321 pNew->next = pHead;
166322 pNew->prev = pHead->prev;
166323 if( pHead->prev ){ pHead->prev->next = pNew; }
166324 else { pH->first = pNew; }
166325 pHead->prev = pNew;
166326 }else{
166327 pNew->next = pH->first;
166328 if( pH->first ){ pH->first->prev = pNew; }
166329 pNew->prev = 0;
166330 pH->first = pNew;
166331 }
166332 pEntry->count++;
166333 pEntry->chain = pNew;
166334}
166335
166336
166337/* Resize the hash table so that it cantains "new_size" buckets.
166338** "new_size" must be a power of 2. The hash table might fail
166339** to resize if sqliteMalloc() fails.
166340**
166341** Return non-zero if a memory allocation error occurs.
166342*/
166343static int fts3Rehash(Fts3Hash *pH, int new_size){
166344 struct _fts3ht *new_ht; /* The new hash table */
166345 Fts3HashElem *elem, *next_elem; /* For looping over existing elements */
166346 int (*xHash)(const void*,int); /* The hash function */
166347
166348 assert( (new_size & (new_size-1))==0 );
166349 new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) );
166350 if( new_ht==0 ) return 1;
166351 fts3HashFree(pH->ht);
166352 pH->ht = new_ht;
166353 pH->htsize = new_size;
166354 xHash = ftsHashFunction(pH->keyClass);
166355 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
166356 int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
166357 next_elem = elem->next;
166358 fts3HashInsertElement(pH, &new_ht[h], elem);
166359 }
166360 return 0;
166361}
166362
166363/* This function (for internal use only) locates an element in an
166364** hash table that matches the given key. The hash for this key has
166365** already been computed and is passed as the 4th parameter.
166366*/
166367static Fts3HashElem *fts3FindElementByHash(
166368 const Fts3Hash *pH, /* The pH to be searched */
166369 const void *pKey, /* The key we are searching for */
166370 int nKey,
166371 int h /* The hash for this key. */
166372){
166373 Fts3HashElem *elem; /* Used to loop thru the element list */
166374 int count; /* Number of elements left to test */
166375 int (*xCompare)(const void*,int,const void*,int); /* comparison function */
166376
166377 if( pH->ht ){
166378 struct _fts3ht *pEntry = &pH->ht[h];
166379 elem = pEntry->chain;
166380 count = pEntry->count;
166381 xCompare = ftsCompareFunction(pH->keyClass);
166382 while( count-- && elem ){
166383 if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
166384 return elem;
166385 }
166386 elem = elem->next;
166387 }
166388 }
166389 return 0;
166390}
166391
166392/* Remove a single entry from the hash table given a pointer to that
166393** element and a hash on the element's key.
166394*/
166395static void fts3RemoveElementByHash(
166396 Fts3Hash *pH, /* The pH containing "elem" */
166397 Fts3HashElem* elem, /* The element to be removed from the pH */
166398 int h /* Hash value for the element */
166399){
166400 struct _fts3ht *pEntry;
166401 if( elem->prev ){
166402 elem->prev->next = elem->next;
166403 }else{
166404 pH->first = elem->next;
166405 }
166406 if( elem->next ){
166407 elem->next->prev = elem->prev;
166408 }
166409 pEntry = &pH->ht[h];
166410 if( pEntry->chain==elem ){
166411 pEntry->chain = elem->next;
166412 }
166413 pEntry->count--;
166414 if( pEntry->count<=0 ){
166415 pEntry->chain = 0;
166416 }
166417 if( pH->copyKey && elem->pKey ){
166418 fts3HashFree(elem->pKey);
166419 }
166420 fts3HashFree( elem );
166421 pH->count--;
166422 if( pH->count<=0 ){
166423 assert( pH->first==0 );
166424 assert( pH->count==0 );
166425 fts3HashClear(pH);
166426 }
166427}
166428
166429SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(
166430 const Fts3Hash *pH,
166431 const void *pKey,
166432 int nKey
166433){
166434 int h; /* A hash on key */
166435 int (*xHash)(const void*,int); /* The hash function */
166436
166437 if( pH==0 || pH->ht==0 ) return 0;
166438 xHash = ftsHashFunction(pH->keyClass);
166439 assert( xHash!=0 );
166440 h = (*xHash)(pKey,nKey);
166441 assert( (pH->htsize & (pH->htsize-1))==0 );
166442 return fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1));
166443}
166444
166445/*
166446** Attempt to locate an element of the hash table pH with a key
166447** that matches pKey,nKey. Return the data for this element if it is
166448** found, or NULL if there is no match.
166449*/
166450SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash *pH, const void *pKey, int nKey){
166451 Fts3HashElem *pElem; /* The element that matches key (if any) */
166452
166453 pElem = sqlite3Fts3HashFindElem(pH, pKey, nKey);
166454 return pElem ? pElem->data : 0;
166455}
166456
166457/* Insert an element into the hash table pH. The key is pKey,nKey
166458** and the data is "data".
166459**
166460** If no element exists with a matching key, then a new
166461** element is created. A copy of the key is made if the copyKey
166462** flag is set. NULL is returned.
166463**
166464** If another element already exists with the same key, then the
166465** new data replaces the old data and the old data is returned.
166466** The key is not copied in this instance. If a malloc fails, then
166467** the new data is returned and the hash table is unchanged.
166468**
166469** If the "data" parameter to this function is NULL, then the
166470** element corresponding to "key" is removed from the hash table.
166471*/
166472SQLITE_PRIVATE void *sqlite3Fts3HashInsert(
166473 Fts3Hash *pH, /* The hash table to insert into */
166474 const void *pKey, /* The key */
166475 int nKey, /* Number of bytes in the key */
166476 void *data /* The data */
166477){
166478 int hraw; /* Raw hash value of the key */
166479 int h; /* the hash of the key modulo hash table size */
166480 Fts3HashElem *elem; /* Used to loop thru the element list */
166481 Fts3HashElem *new_elem; /* New element added to the pH */
166482 int (*xHash)(const void*,int); /* The hash function */
166483
166484 assert( pH!=0 );
166485 xHash = ftsHashFunction(pH->keyClass);
166486 assert( xHash!=0 );
166487 hraw = (*xHash)(pKey, nKey);
166488 assert( (pH->htsize & (pH->htsize-1))==0 );
166489 h = hraw & (pH->htsize-1);
166490 elem = fts3FindElementByHash(pH,pKey,nKey,h);
166491 if( elem ){
166492 void *old_data = elem->data;
166493 if( data==0 ){
166494 fts3RemoveElementByHash(pH,elem,h);
166495 }else{
166496 elem->data = data;
166497 }
166498 return old_data;
166499 }
166500 if( data==0 ) return 0;
166501 if( (pH->htsize==0 && fts3Rehash(pH,8))
166502 || (pH->count>=pH->htsize && fts3Rehash(pH, pH->htsize*2))
166503 ){
166504 pH->count = 0;
166505 return data;
166506 }
166507 assert( pH->htsize>0 );
166508 new_elem = (Fts3HashElem*)fts3HashMalloc( sizeof(Fts3HashElem) );
166509 if( new_elem==0 ) return data;
166510 if( pH->copyKey && pKey!=0 ){
166511 new_elem->pKey = fts3HashMalloc( nKey );
166512 if( new_elem->pKey==0 ){
166513 fts3HashFree(new_elem);
166514 return data;
166515 }
166516 memcpy((void*)new_elem->pKey, pKey, nKey);
166517 }else{
166518 new_elem->pKey = (void*)pKey;
166519 }
166520 new_elem->nKey = nKey;
166521 pH->count++;
166522 assert( pH->htsize>0 );
166523 assert( (pH->htsize & (pH->htsize-1))==0 );
166524 h = hraw & (pH->htsize-1);
166525 fts3HashInsertElement(pH, &pH->ht[h], new_elem);
166526 new_elem->data = data;
166527 return 0;
166528}
166529
166530#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
166531
166532/************** End of fts3_hash.c *******************************************/
166533/************** Begin file fts3_porter.c *************************************/
166534/*
166535** 2006 September 30
166536**
166537** The author disclaims copyright to this source code. In place of
166538** a legal notice, here is a blessing:
166539**
166540** May you do good and not evil.
166541** May you find forgiveness for yourself and forgive others.
166542** May you share freely, never taking more than you give.
166543**
166544*************************************************************************
166545** Implementation of the full-text-search tokenizer that implements
166546** a Porter stemmer.
166547*/
166548
166549/*
166550** The code in this file is only compiled if:
166551**
166552** * The FTS3 module is being built as an extension
166553** (in which case SQLITE_CORE is not defined), or
166554**
166555** * The FTS3 module is being built into the core of
166556** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
166557*/
166558/* #include "fts3Int.h" */
166559#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
166560
166561/* #include <assert.h> */
166562/* #include <stdlib.h> */
166563/* #include <stdio.h> */
166564/* #include <string.h> */
166565
166566/* #include "fts3_tokenizer.h" */
166567
166568/*
166569** Class derived from sqlite3_tokenizer
166570*/
166571typedef struct porter_tokenizer {
166572 sqlite3_tokenizer base; /* Base class */
166573} porter_tokenizer;
166574
166575/*
166576** Class derived from sqlite3_tokenizer_cursor
166577*/
166578typedef struct porter_tokenizer_cursor {
166579 sqlite3_tokenizer_cursor base;
166580 const char *zInput; /* input we are tokenizing */
166581 int nInput; /* size of the input */
166582 int iOffset; /* current position in zInput */
166583 int iToken; /* index of next token to be returned */
166584 char *zToken; /* storage for current token */
166585 int nAllocated; /* space allocated to zToken buffer */
166586} porter_tokenizer_cursor;
166587
166588
166589/*
166590** Create a new tokenizer instance.
166591*/
166592static int porterCreate(
166593 int argc, const char * const *argv,
166594 sqlite3_tokenizer **ppTokenizer
166595){
166596 porter_tokenizer *t;
166597
166598 UNUSED_PARAMETER(argc);
166599 UNUSED_PARAMETER(argv);
166600
166601 t = (porter_tokenizer *) sqlite3_malloc(sizeof(*t));
166602 if( t==NULL ) return SQLITE_NOMEM;
166603 memset(t, 0, sizeof(*t));
166604 *ppTokenizer = &t->base;
166605 return SQLITE_OK;
166606}
166607
166608/*
166609** Destroy a tokenizer
166610*/
166611static int porterDestroy(sqlite3_tokenizer *pTokenizer){
166612 sqlite3_free(pTokenizer);
166613 return SQLITE_OK;
166614}
166615
166616/*
166617** Prepare to begin tokenizing a particular string. The input
166618** string to be tokenized is zInput[0..nInput-1]. A cursor
166619** used to incrementally tokenize this string is returned in
166620** *ppCursor.
166621*/
166622static int porterOpen(
166623 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
166624 const char *zInput, int nInput, /* String to be tokenized */
166625 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
166626){
166627 porter_tokenizer_cursor *c;
166628
166629 UNUSED_PARAMETER(pTokenizer);
166630
166631 c = (porter_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
166632 if( c==NULL ) return SQLITE_NOMEM;
166633
166634 c->zInput = zInput;
166635 if( zInput==0 ){
166636 c->nInput = 0;
166637 }else if( nInput<0 ){
166638 c->nInput = (int)strlen(zInput);
166639 }else{
166640 c->nInput = nInput;
166641 }
166642 c->iOffset = 0; /* start tokenizing at the beginning */
166643 c->iToken = 0;
166644 c->zToken = NULL; /* no space allocated, yet. */
166645 c->nAllocated = 0;
166646
166647 *ppCursor = &c->base;
166648 return SQLITE_OK;
166649}
166650
166651/*
166652** Close a tokenization cursor previously opened by a call to
166653** porterOpen() above.
166654*/
166655static int porterClose(sqlite3_tokenizer_cursor *pCursor){
166656 porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
166657 sqlite3_free(c->zToken);
166658 sqlite3_free(c);
166659 return SQLITE_OK;
166660}
166661/*
166662** Vowel or consonant
166663*/
166664static const char cType[] = {
166665 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
166666 1, 1, 1, 2, 1
166667};
166668
166669/*
166670** isConsonant() and isVowel() determine if their first character in
166671** the string they point to is a consonant or a vowel, according
166672** to Porter ruls.
166673**
166674** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'.
166675** 'Y' is a consonant unless it follows another consonant,
166676** in which case it is a vowel.
166677**
166678** In these routine, the letters are in reverse order. So the 'y' rule
166679** is that 'y' is a consonant unless it is followed by another
166680** consonent.
166681*/
166682static int isVowel(const char*);
166683static int isConsonant(const char *z){
166684 int j;
166685 char x = *z;
166686 if( x==0 ) return 0;
166687 assert( x>='a' && x<='z' );
166688 j = cType[x-'a'];
166689 if( j<2 ) return j;
166690 return z[1]==0 || isVowel(z + 1);
166691}
166692static int isVowel(const char *z){
166693 int j;
166694 char x = *z;
166695 if( x==0 ) return 0;
166696 assert( x>='a' && x<='z' );
166697 j = cType[x-'a'];
166698 if( j<2 ) return 1-j;
166699 return isConsonant(z + 1);
166700}
166701
166702/*
166703** Let any sequence of one or more vowels be represented by V and let
166704** C be sequence of one or more consonants. Then every word can be
166705** represented as:
166706**
166707** [C] (VC){m} [V]
166708**
166709** In prose: A word is an optional consonant followed by zero or
166710** vowel-consonant pairs followed by an optional vowel. "m" is the
166711** number of vowel consonant pairs. This routine computes the value
166712** of m for the first i bytes of a word.
166713**
166714** Return true if the m-value for z is 1 or more. In other words,
166715** return true if z contains at least one vowel that is followed
166716** by a consonant.
166717**
166718** In this routine z[] is in reverse order. So we are really looking
166719** for an instance of a consonant followed by a vowel.
166720*/
166721static int m_gt_0(const char *z){
166722 while( isVowel(z) ){ z++; }
166723 if( *z==0 ) return 0;
166724 while( isConsonant(z) ){ z++; }
166725 return *z!=0;
166726}
166727
166728/* Like mgt0 above except we are looking for a value of m which is
166729** exactly 1
166730*/
166731static int m_eq_1(const char *z){
166732 while( isVowel(z) ){ z++; }
166733 if( *z==0 ) return 0;
166734 while( isConsonant(z) ){ z++; }
166735 if( *z==0 ) return 0;
166736 while( isVowel(z) ){ z++; }
166737 if( *z==0 ) return 1;
166738 while( isConsonant(z) ){ z++; }
166739 return *z==0;
166740}
166741
166742/* Like mgt0 above except we are looking for a value of m>1 instead
166743** or m>0
166744*/
166745static int m_gt_1(const char *z){
166746 while( isVowel(z) ){ z++; }
166747 if( *z==0 ) return 0;
166748 while( isConsonant(z) ){ z++; }
166749 if( *z==0 ) return 0;
166750 while( isVowel(z) ){ z++; }
166751 if( *z==0 ) return 0;
166752 while( isConsonant(z) ){ z++; }
166753 return *z!=0;
166754}
166755
166756/*
166757** Return TRUE if there is a vowel anywhere within z[0..n-1]
166758*/
166759static int hasVowel(const char *z){
166760 while( isConsonant(z) ){ z++; }
166761 return *z!=0;
166762}
166763
166764/*
166765** Return TRUE if the word ends in a double consonant.
166766**
166767** The text is reversed here. So we are really looking at
166768** the first two characters of z[].
166769*/
166770static int doubleConsonant(const char *z){
166771 return isConsonant(z) && z[0]==z[1];
166772}
166773
166774/*
166775** Return TRUE if the word ends with three letters which
166776** are consonant-vowel-consonent and where the final consonant
166777** is not 'w', 'x', or 'y'.
166778**
166779** The word is reversed here. So we are really checking the
166780** first three letters and the first one cannot be in [wxy].
166781*/
166782static int star_oh(const char *z){
166783 return
166784 isConsonant(z) &&
166785 z[0]!='w' && z[0]!='x' && z[0]!='y' &&
166786 isVowel(z+1) &&
166787 isConsonant(z+2);
166788}
166789
166790/*
166791** If the word ends with zFrom and xCond() is true for the stem
166792** of the word that preceeds the zFrom ending, then change the
166793** ending to zTo.
166794**
166795** The input word *pz and zFrom are both in reverse order. zTo
166796** is in normal order.
166797**
166798** Return TRUE if zFrom matches. Return FALSE if zFrom does not
166799** match. Not that TRUE is returned even if xCond() fails and
166800** no substitution occurs.
166801*/
166802static int stem(
166803 char **pz, /* The word being stemmed (Reversed) */
166804 const char *zFrom, /* If the ending matches this... (Reversed) */
166805 const char *zTo, /* ... change the ending to this (not reversed) */
166806 int (*xCond)(const char*) /* Condition that must be true */
166807){
166808 char *z = *pz;
166809 while( *zFrom && *zFrom==*z ){ z++; zFrom++; }
166810 if( *zFrom!=0 ) return 0;
166811 if( xCond && !xCond(z) ) return 1;
166812 while( *zTo ){
166813 *(--z) = *(zTo++);
166814 }
166815 *pz = z;
166816 return 1;
166817}
166818
166819/*
166820** This is the fallback stemmer used when the porter stemmer is
166821** inappropriate. The input word is copied into the output with
166822** US-ASCII case folding. If the input word is too long (more
166823** than 20 bytes if it contains no digits or more than 6 bytes if
166824** it contains digits) then word is truncated to 20 or 6 bytes
166825** by taking 10 or 3 bytes from the beginning and end.
166826*/
166827static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
166828 int i, mx, j;
166829 int hasDigit = 0;
166830 for(i=0; i<nIn; i++){
166831 char c = zIn[i];
166832 if( c>='A' && c<='Z' ){
166833 zOut[i] = c - 'A' + 'a';
166834 }else{
166835 if( c>='0' && c<='9' ) hasDigit = 1;
166836 zOut[i] = c;
166837 }
166838 }
166839 mx = hasDigit ? 3 : 10;
166840 if( nIn>mx*2 ){
166841 for(j=mx, i=nIn-mx; i<nIn; i++, j++){
166842 zOut[j] = zOut[i];
166843 }
166844 i = j;
166845 }
166846 zOut[i] = 0;
166847 *pnOut = i;
166848}
166849
166850
166851/*
166852** Stem the input word zIn[0..nIn-1]. Store the output in zOut.
166853** zOut is at least big enough to hold nIn bytes. Write the actual
166854** size of the output word (exclusive of the '\0' terminator) into *pnOut.
166855**
166856** Any upper-case characters in the US-ASCII character set ([A-Z])
166857** are converted to lower case. Upper-case UTF characters are
166858** unchanged.
166859**
166860** Words that are longer than about 20 bytes are stemmed by retaining
166861** a few bytes from the beginning and the end of the word. If the
166862** word contains digits, 3 bytes are taken from the beginning and
166863** 3 bytes from the end. For long words without digits, 10 bytes
166864** are taken from each end. US-ASCII case folding still applies.
166865**
166866** If the input word contains not digits but does characters not
166867** in [a-zA-Z] then no stemming is attempted and this routine just
166868** copies the input into the input into the output with US-ASCII
166869** case folding.
166870**
166871** Stemming never increases the length of the word. So there is
166872** no chance of overflowing the zOut buffer.
166873*/
166874static void porter_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
166875 int i, j;
166876 char zReverse[28];
166877 char *z, *z2;
166878 if( nIn<3 || nIn>=(int)sizeof(zReverse)-7 ){
166879 /* The word is too big or too small for the porter stemmer.
166880 ** Fallback to the copy stemmer */
166881 copy_stemmer(zIn, nIn, zOut, pnOut);
166882 return;
166883 }
166884 for(i=0, j=sizeof(zReverse)-6; i<nIn; i++, j--){
166885 char c = zIn[i];
166886 if( c>='A' && c<='Z' ){
166887 zReverse[j] = c + 'a' - 'A';
166888 }else if( c>='a' && c<='z' ){
166889 zReverse[j] = c;
166890 }else{
166891 /* The use of a character not in [a-zA-Z] means that we fallback
166892 ** to the copy stemmer */
166893 copy_stemmer(zIn, nIn, zOut, pnOut);
166894 return;
166895 }
166896 }
166897 memset(&zReverse[sizeof(zReverse)-5], 0, 5);
166898 z = &zReverse[j+1];
166899
166900
166901 /* Step 1a */
166902 if( z[0]=='s' ){
166903 if(
166904 !stem(&z, "sess", "ss", 0) &&
166905 !stem(&z, "sei", "i", 0) &&
166906 !stem(&z, "ss", "ss", 0)
166907 ){
166908 z++;
166909 }
166910 }
166911
166912 /* Step 1b */
166913 z2 = z;
166914 if( stem(&z, "dee", "ee", m_gt_0) ){
166915 /* Do nothing. The work was all in the test */
166916 }else if(
166917 (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel))
166918 && z!=z2
166919 ){
166920 if( stem(&z, "ta", "ate", 0) ||
166921 stem(&z, "lb", "ble", 0) ||
166922 stem(&z, "zi", "ize", 0) ){
166923 /* Do nothing. The work was all in the test */
166924 }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){
166925 z++;
166926 }else if( m_eq_1(z) && star_oh(z) ){
166927 *(--z) = 'e';
166928 }
166929 }
166930
166931 /* Step 1c */
166932 if( z[0]=='y' && hasVowel(z+1) ){
166933 z[0] = 'i';
166934 }
166935
166936 /* Step 2 */
166937 switch( z[1] ){
166938 case 'a':
166939 if( !stem(&z, "lanoita", "ate", m_gt_0) ){
166940 stem(&z, "lanoit", "tion", m_gt_0);
166941 }
166942 break;
166943 case 'c':
166944 if( !stem(&z, "icne", "ence", m_gt_0) ){
166945 stem(&z, "icna", "ance", m_gt_0);
166946 }
166947 break;
166948 case 'e':
166949 stem(&z, "rezi", "ize", m_gt_0);
166950 break;
166951 case 'g':
166952 stem(&z, "igol", "log", m_gt_0);
166953 break;
166954 case 'l':
166955 if( !stem(&z, "ilb", "ble", m_gt_0)
166956 && !stem(&z, "illa", "al", m_gt_0)
166957 && !stem(&z, "iltne", "ent", m_gt_0)
166958 && !stem(&z, "ile", "e", m_gt_0)
166959 ){
166960 stem(&z, "ilsuo", "ous", m_gt_0);
166961 }
166962 break;
166963 case 'o':
166964 if( !stem(&z, "noitazi", "ize", m_gt_0)
166965 && !stem(&z, "noita", "ate", m_gt_0)
166966 ){
166967 stem(&z, "rota", "ate", m_gt_0);
166968 }
166969 break;
166970 case 's':
166971 if( !stem(&z, "msila", "al", m_gt_0)
166972 && !stem(&z, "ssenevi", "ive", m_gt_0)
166973 && !stem(&z, "ssenluf", "ful", m_gt_0)
166974 ){
166975 stem(&z, "ssensuo", "ous", m_gt_0);
166976 }
166977 break;
166978 case 't':
166979 if( !stem(&z, "itila", "al", m_gt_0)
166980 && !stem(&z, "itivi", "ive", m_gt_0)
166981 ){
166982 stem(&z, "itilib", "ble", m_gt_0);
166983 }
166984 break;
166985 }
166986
166987 /* Step 3 */
166988 switch( z[0] ){
166989 case 'e':
166990 if( !stem(&z, "etaci", "ic", m_gt_0)
166991 && !stem(&z, "evita", "", m_gt_0)
166992 ){
166993 stem(&z, "ezila", "al", m_gt_0);
166994 }
166995 break;
166996 case 'i':
166997 stem(&z, "itici", "ic", m_gt_0);
166998 break;
166999 case 'l':
167000 if( !stem(&z, "laci", "ic", m_gt_0) ){
167001 stem(&z, "luf", "", m_gt_0);
167002 }
167003 break;
167004 case 's':
167005 stem(&z, "ssen", "", m_gt_0);
167006 break;
167007 }
167008
167009 /* Step 4 */
167010 switch( z[1] ){
167011 case 'a':
167012 if( z[0]=='l' && m_gt_1(z+2) ){
167013 z += 2;
167014 }
167015 break;
167016 case 'c':
167017 if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e') && m_gt_1(z+4) ){
167018 z += 4;
167019 }
167020 break;
167021 case 'e':
167022 if( z[0]=='r' && m_gt_1(z+2) ){
167023 z += 2;
167024 }
167025 break;
167026 case 'i':
167027 if( z[0]=='c' && m_gt_1(z+2) ){
167028 z += 2;
167029 }
167030 break;
167031 case 'l':
167032 if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){
167033 z += 4;
167034 }
167035 break;
167036 case 'n':
167037 if( z[0]=='t' ){
167038 if( z[2]=='a' ){
167039 if( m_gt_1(z+3) ){
167040 z += 3;
167041 }
167042 }else if( z[2]=='e' ){
167043 if( !stem(&z, "tneme", "", m_gt_1)
167044 && !stem(&z, "tnem", "", m_gt_1)
167045 ){
167046 stem(&z, "tne", "", m_gt_1);
167047 }
167048 }
167049 }
167050 break;
167051 case 'o':
167052 if( z[0]=='u' ){
167053 if( m_gt_1(z+2) ){
167054 z += 2;
167055 }
167056 }else if( z[3]=='s' || z[3]=='t' ){
167057 stem(&z, "noi", "", m_gt_1);
167058 }
167059 break;
167060 case 's':
167061 if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){
167062 z += 3;
167063 }
167064 break;
167065 case 't':
167066 if( !stem(&z, "eta", "", m_gt_1) ){
167067 stem(&z, "iti", "", m_gt_1);
167068 }
167069 break;
167070 case 'u':
167071 if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){
167072 z += 3;
167073 }
167074 break;
167075 case 'v':
167076 case 'z':
167077 if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){
167078 z += 3;
167079 }
167080 break;
167081 }
167082
167083 /* Step 5a */
167084 if( z[0]=='e' ){
167085 if( m_gt_1(z+1) ){
167086 z++;
167087 }else if( m_eq_1(z+1) && !star_oh(z+1) ){
167088 z++;
167089 }
167090 }
167091
167092 /* Step 5b */
167093 if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){
167094 z++;
167095 }
167096
167097 /* z[] is now the stemmed word in reverse order. Flip it back
167098 ** around into forward order and return.
167099 */
167100 *pnOut = i = (int)strlen(z);
167101 zOut[i] = 0;
167102 while( *z ){
167103 zOut[--i] = *(z++);
167104 }
167105}
167106
167107/*
167108** Characters that can be part of a token. We assume any character
167109** whose value is greater than 0x80 (any UTF character) can be
167110** part of a token. In other words, delimiters all must have
167111** values of 0x7f or lower.
167112*/
167113static const char porterIdChar[] = {
167114/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
167115 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
167116 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
167117 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
167118 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
167119 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
167120};
167121#define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30]))
167122
167123/*
167124** Extract the next token from a tokenization cursor. The cursor must
167125** have been opened by a prior call to porterOpen().
167126*/
167127static int porterNext(
167128 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by porterOpen */
167129 const char **pzToken, /* OUT: *pzToken is the token text */
167130 int *pnBytes, /* OUT: Number of bytes in token */
167131 int *piStartOffset, /* OUT: Starting offset of token */
167132 int *piEndOffset, /* OUT: Ending offset of token */
167133 int *piPosition /* OUT: Position integer of token */
167134){
167135 porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
167136 const char *z = c->zInput;
167137
167138 while( c->iOffset<c->nInput ){
167139 int iStartOffset, ch;
167140
167141 /* Scan past delimiter characters */
167142 while( c->iOffset<c->nInput && isDelim(z[c->iOffset]) ){
167143 c->iOffset++;
167144 }
167145
167146 /* Count non-delimiter characters. */
167147 iStartOffset = c->iOffset;
167148 while( c->iOffset<c->nInput && !isDelim(z[c->iOffset]) ){
167149 c->iOffset++;
167150 }
167151
167152 if( c->iOffset>iStartOffset ){
167153 int n = c->iOffset-iStartOffset;
167154 if( n>c->nAllocated ){
167155 char *pNew;
167156 c->nAllocated = n+20;
167157 pNew = sqlite3_realloc(c->zToken, c->nAllocated);
167158 if( !pNew ) return SQLITE_NOMEM;
167159 c->zToken = pNew;
167160 }
167161 porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
167162 *pzToken = c->zToken;
167163 *piStartOffset = iStartOffset;
167164 *piEndOffset = c->iOffset;
167165 *piPosition = c->iToken++;
167166 return SQLITE_OK;
167167 }
167168 }
167169 return SQLITE_DONE;
167170}
167171
167172/*
167173** The set of routines that implement the porter-stemmer tokenizer
167174*/
167175static const sqlite3_tokenizer_module porterTokenizerModule = {
167176 0,
167177 porterCreate,
167178 porterDestroy,
167179 porterOpen,
167180 porterClose,
167181 porterNext,
167182 0
167183};
167184
167185/*
167186** Allocate a new porter tokenizer. Return a pointer to the new
167187** tokenizer in *ppModule
167188*/
167189SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(
167190 sqlite3_tokenizer_module const**ppModule
167191){
167192 *ppModule = &porterTokenizerModule;
167193}
167194
167195#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
167196
167197/************** End of fts3_porter.c *****************************************/
167198/************** Begin file fts3_tokenizer.c **********************************/
167199/*
167200** 2007 June 22
167201**
167202** The author disclaims copyright to this source code. In place of
167203** a legal notice, here is a blessing:
167204**
167205** May you do good and not evil.
167206** May you find forgiveness for yourself and forgive others.
167207** May you share freely, never taking more than you give.
167208**
167209******************************************************************************
167210**
167211** This is part of an SQLite module implementing full-text search.
167212** This particular file implements the generic tokenizer interface.
167213*/
167214
167215/*
167216** The code in this file is only compiled if:
167217**
167218** * The FTS3 module is being built as an extension
167219** (in which case SQLITE_CORE is not defined), or
167220**
167221** * The FTS3 module is being built into the core of
167222** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
167223*/
167224/* #include "fts3Int.h" */
167225#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
167226
167227/* #include <assert.h> */
167228/* #include <string.h> */
167229
167230/*
167231** Return true if the two-argument version of fts3_tokenizer()
167232** has been activated via a prior call to sqlite3_db_config(db,
167233** SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
167234*/
167235static int fts3TokenizerEnabled(sqlite3_context *context){
167236 sqlite3 *db = sqlite3_context_db_handle(context);
167237 int isEnabled = 0;
167238 sqlite3_db_config(db,SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER,-1,&isEnabled);
167239 return isEnabled;
167240}
167241
167242/*
167243** Implementation of the SQL scalar function for accessing the underlying
167244** hash table. This function may be called as follows:
167245**
167246** SELECT <function-name>(<key-name>);
167247** SELECT <function-name>(<key-name>, <pointer>);
167248**
167249** where <function-name> is the name passed as the second argument
167250** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer').
167251**
167252** If the <pointer> argument is specified, it must be a blob value
167253** containing a pointer to be stored as the hash data corresponding
167254** to the string <key-name>. If <pointer> is not specified, then
167255** the string <key-name> must already exist in the has table. Otherwise,
167256** an error is returned.
167257**
167258** Whether or not the <pointer> argument is specified, the value returned
167259** is a blob containing the pointer stored as the hash data corresponding
167260** to string <key-name> (after the hash-table is updated, if applicable).
167261*/
167262static void fts3TokenizerFunc(
167263 sqlite3_context *context,
167264 int argc,
167265 sqlite3_value **argv
167266){
167267 Fts3Hash *pHash;
167268 void *pPtr = 0;
167269 const unsigned char *zName;
167270 int nName;
167271
167272 assert( argc==1 || argc==2 );
167273
167274 pHash = (Fts3Hash *)sqlite3_user_data(context);
167275
167276 zName = sqlite3_value_text(argv[0]);
167277 nName = sqlite3_value_bytes(argv[0])+1;
167278
167279 if( argc==2 ){
167280 if( fts3TokenizerEnabled(context) ){
167281 void *pOld;
167282 int n = sqlite3_value_bytes(argv[1]);
167283 if( zName==0 || n!=sizeof(pPtr) ){
167284 sqlite3_result_error(context, "argument type mismatch", -1);
167285 return;
167286 }
167287 pPtr = *(void **)sqlite3_value_blob(argv[1]);
167288 pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
167289 if( pOld==pPtr ){
167290 sqlite3_result_error(context, "out of memory", -1);
167291 }
167292 }else{
167293 sqlite3_result_error(context, "fts3tokenize disabled", -1);
167294 return;
167295 }
167296 }else{
167297 if( zName ){
167298 pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
167299 }
167300 if( !pPtr ){
167301 char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
167302 sqlite3_result_error(context, zErr, -1);
167303 sqlite3_free(zErr);
167304 return;
167305 }
167306 }
167307 sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT);
167308}
167309
167310SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char c){
167311 static const char isFtsIdChar[] = {
167312 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
167313 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
167314 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
167315 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
167316 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
167317 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
167318 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
167319 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
167320 };
167321 return (c&0x80 || isFtsIdChar[(int)(c)]);
167322}
167323
167324SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *zStr, int *pn){
167325 const char *z1;
167326 const char *z2 = 0;
167327
167328 /* Find the start of the next token. */
167329 z1 = zStr;
167330 while( z2==0 ){
167331 char c = *z1;
167332 switch( c ){
167333 case '\0': return 0; /* No more tokens here */
167334 case '\'':
167335 case '"':
167336 case '`': {
167337 z2 = z1;
167338 while( *++z2 && (*z2!=c || *++z2==c) );
167339 break;
167340 }
167341 case '[':
167342 z2 = &z1[1];
167343 while( *z2 && z2[0]!=']' ) z2++;
167344 if( *z2 ) z2++;
167345 break;
167346
167347 default:
167348 if( sqlite3Fts3IsIdChar(*z1) ){
167349 z2 = &z1[1];
167350 while( sqlite3Fts3IsIdChar(*z2) ) z2++;
167351 }else{
167352 z1++;
167353 }
167354 }
167355 }
167356
167357 *pn = (int)(z2-z1);
167358 return z1;
167359}
167360
167361SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(
167362 Fts3Hash *pHash, /* Tokenizer hash table */
167363 const char *zArg, /* Tokenizer name */
167364 sqlite3_tokenizer **ppTok, /* OUT: Tokenizer (if applicable) */
167365 char **pzErr /* OUT: Set to malloced error message */
167366){
167367 int rc;
167368 char *z = (char *)zArg;
167369 int n = 0;
167370 char *zCopy;
167371 char *zEnd; /* Pointer to nul-term of zCopy */
167372 sqlite3_tokenizer_module *m;
167373
167374 zCopy = sqlite3_mprintf("%s", zArg);
167375 if( !zCopy ) return SQLITE_NOMEM;
167376 zEnd = &zCopy[strlen(zCopy)];
167377
167378 z = (char *)sqlite3Fts3NextToken(zCopy, &n);
167379 if( z==0 ){
167380 assert( n==0 );
167381 z = zCopy;
167382 }
167383 z[n] = '\0';
167384 sqlite3Fts3Dequote(z);
167385
167386 m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1);
167387 if( !m ){
167388 sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", z);
167389 rc = SQLITE_ERROR;
167390 }else{
167391 char const **aArg = 0;
167392 int iArg = 0;
167393 z = &z[n+1];
167394 while( z<zEnd && (NULL!=(z = (char *)sqlite3Fts3NextToken(z, &n))) ){
167395 int nNew = sizeof(char *)*(iArg+1);
167396 char const **aNew = (const char **)sqlite3_realloc((void *)aArg, nNew);
167397 if( !aNew ){
167398 sqlite3_free(zCopy);
167399 sqlite3_free((void *)aArg);
167400 return SQLITE_NOMEM;
167401 }
167402 aArg = aNew;
167403 aArg[iArg++] = z;
167404 z[n] = '\0';
167405 sqlite3Fts3Dequote(z);
167406 z = &z[n+1];
167407 }
167408 rc = m->xCreate(iArg, aArg, ppTok);
167409 assert( rc!=SQLITE_OK || *ppTok );
167410 if( rc!=SQLITE_OK ){
167411 sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer");
167412 }else{
167413 (*ppTok)->pModule = m;
167414 }
167415 sqlite3_free((void *)aArg);
167416 }
167417
167418 sqlite3_free(zCopy);
167419 return rc;
167420}
167421
167422
167423#ifdef SQLITE_TEST
167424
167425#if defined(INCLUDE_SQLITE_TCL_H)
167426# include "sqlite_tcl.h"
167427#else
167428# include "tcl.h"
167429#endif
167430/* #include <string.h> */
167431
167432/*
167433** Implementation of a special SQL scalar function for testing tokenizers
167434** designed to be used in concert with the Tcl testing framework. This
167435** function must be called with two or more arguments:
167436**
167437** SELECT <function-name>(<key-name>, ..., <input-string>);
167438**
167439** where <function-name> is the name passed as the second argument
167440** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
167441** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
167442**
167443** The return value is a string that may be interpreted as a Tcl
167444** list. For each token in the <input-string>, three elements are
167445** added to the returned list. The first is the token position, the
167446** second is the token text (folded, stemmed, etc.) and the third is the
167447** substring of <input-string> associated with the token. For example,
167448** using the built-in "simple" tokenizer:
167449**
167450** SELECT fts_tokenizer_test('simple', 'I don't see how');
167451**
167452** will return the string:
167453**
167454** "{0 i I 1 dont don't 2 see see 3 how how}"
167455**
167456*/
167457static void testFunc(
167458 sqlite3_context *context,
167459 int argc,
167460 sqlite3_value **argv
167461){
167462 Fts3Hash *pHash;
167463 sqlite3_tokenizer_module *p;
167464 sqlite3_tokenizer *pTokenizer = 0;
167465 sqlite3_tokenizer_cursor *pCsr = 0;
167466
167467 const char *zErr = 0;
167468
167469 const char *zName;
167470 int nName;
167471 const char *zInput;
167472 int nInput;
167473
167474 const char *azArg[64];
167475
167476 const char *zToken;
167477 int nToken = 0;
167478 int iStart = 0;
167479 int iEnd = 0;
167480 int iPos = 0;
167481 int i;
167482
167483 Tcl_Obj *pRet;
167484
167485 if( argc<2 ){
167486 sqlite3_result_error(context, "insufficient arguments", -1);
167487 return;
167488 }
167489
167490 nName = sqlite3_value_bytes(argv[0]);
167491 zName = (const char *)sqlite3_value_text(argv[0]);
167492 nInput = sqlite3_value_bytes(argv[argc-1]);
167493 zInput = (const char *)sqlite3_value_text(argv[argc-1]);
167494
167495 pHash = (Fts3Hash *)sqlite3_user_data(context);
167496 p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
167497
167498 if( !p ){
167499 char *zErr2 = sqlite3_mprintf("unknown tokenizer: %s", zName);
167500 sqlite3_result_error(context, zErr2, -1);
167501 sqlite3_free(zErr2);
167502 return;
167503 }
167504
167505 pRet = Tcl_NewObj();
167506 Tcl_IncrRefCount(pRet);
167507
167508 for(i=1; i<argc-1; i++){
167509 azArg[i-1] = (const char *)sqlite3_value_text(argv[i]);
167510 }
167511
167512 if( SQLITE_OK!=p->xCreate(argc-2, azArg, &pTokenizer) ){
167513 zErr = "error in xCreate()";
167514 goto finish;
167515 }
167516 pTokenizer->pModule = p;
167517 if( sqlite3Fts3OpenTokenizer(pTokenizer, 0, zInput, nInput, &pCsr) ){
167518 zErr = "error in xOpen()";
167519 goto finish;
167520 }
167521
167522 while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
167523 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos));
167524 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
167525 zToken = &zInput[iStart];
167526 nToken = iEnd-iStart;
167527 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
167528 }
167529
167530 if( SQLITE_OK!=p->xClose(pCsr) ){
167531 zErr = "error in xClose()";
167532 goto finish;
167533 }
167534 if( SQLITE_OK!=p->xDestroy(pTokenizer) ){
167535 zErr = "error in xDestroy()";
167536 goto finish;
167537 }
167538
167539finish:
167540 if( zErr ){
167541 sqlite3_result_error(context, zErr, -1);
167542 }else{
167543 sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
167544 }
167545 Tcl_DecrRefCount(pRet);
167546}
167547
167548static
167549int registerTokenizer(
167550 sqlite3 *db,
167551 char *zName,
167552 const sqlite3_tokenizer_module *p
167553){
167554 int rc;
167555 sqlite3_stmt *pStmt;
167556 const char zSql[] = "SELECT fts3_tokenizer(?, ?)";
167557
167558 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
167559 if( rc!=SQLITE_OK ){
167560 return rc;
167561 }
167562
167563 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
167564 sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC);
167565 sqlite3_step(pStmt);
167566
167567 return sqlite3_finalize(pStmt);
167568}
167569
167570
167571static
167572int queryTokenizer(
167573 sqlite3 *db,
167574 char *zName,
167575 const sqlite3_tokenizer_module **pp
167576){
167577 int rc;
167578 sqlite3_stmt *pStmt;
167579 const char zSql[] = "SELECT fts3_tokenizer(?)";
167580
167581 *pp = 0;
167582 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
167583 if( rc!=SQLITE_OK ){
167584 return rc;
167585 }
167586
167587 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
167588 if( SQLITE_ROW==sqlite3_step(pStmt) ){
167589 if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
167590 memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
167591 }
167592 }
167593
167594 return sqlite3_finalize(pStmt);
167595}
167596
167597SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
167598
167599/*
167600** Implementation of the scalar function fts3_tokenizer_internal_test().
167601** This function is used for testing only, it is not included in the
167602** build unless SQLITE_TEST is defined.
167603**
167604** The purpose of this is to test that the fts3_tokenizer() function
167605** can be used as designed by the C-code in the queryTokenizer and
167606** registerTokenizer() functions above. These two functions are repeated
167607** in the README.tokenizer file as an example, so it is important to
167608** test them.
167609**
167610** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar
167611** function with no arguments. An assert() will fail if a problem is
167612** detected. i.e.:
167613**
167614** SELECT fts3_tokenizer_internal_test();
167615**
167616*/
167617static void intTestFunc(
167618 sqlite3_context *context,
167619 int argc,
167620 sqlite3_value **argv
167621){
167622 int rc;
167623 const sqlite3_tokenizer_module *p1;
167624 const sqlite3_tokenizer_module *p2;
167625 sqlite3 *db = (sqlite3 *)sqlite3_user_data(context);
167626
167627 UNUSED_PARAMETER(argc);
167628 UNUSED_PARAMETER(argv);
167629
167630 /* Test the query function */
167631 sqlite3Fts3SimpleTokenizerModule(&p1);
167632 rc = queryTokenizer(db, "simple", &p2);
167633 assert( rc==SQLITE_OK );
167634 assert( p1==p2 );
167635 rc = queryTokenizer(db, "nosuchtokenizer", &p2);
167636 assert( rc==SQLITE_ERROR );
167637 assert( p2==0 );
167638 assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") );
167639
167640 /* Test the storage function */
167641 if( fts3TokenizerEnabled(context) ){
167642 rc = registerTokenizer(db, "nosuchtokenizer", p1);
167643 assert( rc==SQLITE_OK );
167644 rc = queryTokenizer(db, "nosuchtokenizer", &p2);
167645 assert( rc==SQLITE_OK );
167646 assert( p2==p1 );
167647 }
167648
167649 sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
167650}
167651
167652#endif
167653
167654/*
167655** Set up SQL objects in database db used to access the contents of
167656** the hash table pointed to by argument pHash. The hash table must
167657** been initialized to use string keys, and to take a private copy
167658** of the key when a value is inserted. i.e. by a call similar to:
167659**
167660** sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
167661**
167662** This function adds a scalar function (see header comment above
167663** fts3TokenizerFunc() in this file for details) and, if ENABLE_TABLE is
167664** defined at compilation time, a temporary virtual table (see header
167665** comment above struct HashTableVtab) to the database schema. Both
167666** provide read/write access to the contents of *pHash.
167667**
167668** The third argument to this function, zName, is used as the name
167669** of both the scalar and, if created, the virtual table.
167670*/
167671SQLITE_PRIVATE int sqlite3Fts3InitHashTable(
167672 sqlite3 *db,
167673 Fts3Hash *pHash,
167674 const char *zName
167675){
167676 int rc = SQLITE_OK;
167677 void *p = (void *)pHash;
167678 const int any = SQLITE_ANY;
167679
167680#ifdef SQLITE_TEST
167681 char *zTest = 0;
167682 char *zTest2 = 0;
167683 void *pdb = (void *)db;
167684 zTest = sqlite3_mprintf("%s_test", zName);
167685 zTest2 = sqlite3_mprintf("%s_internal_test", zName);
167686 if( !zTest || !zTest2 ){
167687 rc = SQLITE_NOMEM;
167688 }
167689#endif
167690
167691 if( SQLITE_OK==rc ){
167692 rc = sqlite3_create_function(db, zName, 1, any, p, fts3TokenizerFunc, 0, 0);
167693 }
167694 if( SQLITE_OK==rc ){
167695 rc = sqlite3_create_function(db, zName, 2, any, p, fts3TokenizerFunc, 0, 0);
167696 }
167697#ifdef SQLITE_TEST
167698 if( SQLITE_OK==rc ){
167699 rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0);
167700 }
167701 if( SQLITE_OK==rc ){
167702 rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
167703 }
167704#endif
167705
167706#ifdef SQLITE_TEST
167707 sqlite3_free(zTest);
167708 sqlite3_free(zTest2);
167709#endif
167710
167711 return rc;
167712}
167713
167714#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
167715
167716/************** End of fts3_tokenizer.c **************************************/
167717/************** Begin file fts3_tokenizer1.c *********************************/
167718/*
167719** 2006 Oct 10
167720**
167721** The author disclaims copyright to this source code. In place of
167722** a legal notice, here is a blessing:
167723**
167724** May you do good and not evil.
167725** May you find forgiveness for yourself and forgive others.
167726** May you share freely, never taking more than you give.
167727**
167728******************************************************************************
167729**
167730** Implementation of the "simple" full-text-search tokenizer.
167731*/
167732
167733/*
167734** The code in this file is only compiled if:
167735**
167736** * The FTS3 module is being built as an extension
167737** (in which case SQLITE_CORE is not defined), or
167738**
167739** * The FTS3 module is being built into the core of
167740** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
167741*/
167742/* #include "fts3Int.h" */
167743#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
167744
167745/* #include <assert.h> */
167746/* #include <stdlib.h> */
167747/* #include <stdio.h> */
167748/* #include <string.h> */
167749
167750/* #include "fts3_tokenizer.h" */
167751
167752typedef struct simple_tokenizer {
167753 sqlite3_tokenizer base;
167754 char delim[128]; /* flag ASCII delimiters */
167755} simple_tokenizer;
167756
167757typedef struct simple_tokenizer_cursor {
167758 sqlite3_tokenizer_cursor base;
167759 const char *pInput; /* input we are tokenizing */
167760 int nBytes; /* size of the input */
167761 int iOffset; /* current position in pInput */
167762 int iToken; /* index of next token to be returned */
167763 char *pToken; /* storage for current token */
167764 int nTokenAllocated; /* space allocated to zToken buffer */
167765} simple_tokenizer_cursor;
167766
167767
167768static int simpleDelim(simple_tokenizer *t, unsigned char c){
167769 return c<0x80 && t->delim[c];
167770}
167771static int fts3_isalnum(int x){
167772 return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z');
167773}
167774
167775/*
167776** Create a new tokenizer instance.
167777*/
167778static int simpleCreate(
167779 int argc, const char * const *argv,
167780 sqlite3_tokenizer **ppTokenizer
167781){
167782 simple_tokenizer *t;
167783
167784 t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t));
167785 if( t==NULL ) return SQLITE_NOMEM;
167786 memset(t, 0, sizeof(*t));
167787
167788 /* TODO(shess) Delimiters need to remain the same from run to run,
167789 ** else we need to reindex. One solution would be a meta-table to
167790 ** track such information in the database, then we'd only want this
167791 ** information on the initial create.
167792 */
167793 if( argc>1 ){
167794 int i, n = (int)strlen(argv[1]);
167795 for(i=0; i<n; i++){
167796 unsigned char ch = argv[1][i];
167797 /* We explicitly don't support UTF-8 delimiters for now. */
167798 if( ch>=0x80 ){
167799 sqlite3_free(t);
167800 return SQLITE_ERROR;
167801 }
167802 t->delim[ch] = 1;
167803 }
167804 } else {
167805 /* Mark non-alphanumeric ASCII characters as delimiters */
167806 int i;
167807 for(i=1; i<0x80; i++){
167808 t->delim[i] = !fts3_isalnum(i) ? -1 : 0;
167809 }
167810 }
167811
167812 *ppTokenizer = &t->base;
167813 return SQLITE_OK;
167814}
167815
167816/*
167817** Destroy a tokenizer
167818*/
167819static int simpleDestroy(sqlite3_tokenizer *pTokenizer){
167820 sqlite3_free(pTokenizer);
167821 return SQLITE_OK;
167822}
167823
167824/*
167825** Prepare to begin tokenizing a particular string. The input
167826** string to be tokenized is pInput[0..nBytes-1]. A cursor
167827** used to incrementally tokenize this string is returned in
167828** *ppCursor.
167829*/
167830static int simpleOpen(
167831 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
167832 const char *pInput, int nBytes, /* String to be tokenized */
167833 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
167834){
167835 simple_tokenizer_cursor *c;
167836
167837 UNUSED_PARAMETER(pTokenizer);
167838
167839 c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
167840 if( c==NULL ) return SQLITE_NOMEM;
167841
167842 c->pInput = pInput;
167843 if( pInput==0 ){
167844 c->nBytes = 0;
167845 }else if( nBytes<0 ){
167846 c->nBytes = (int)strlen(pInput);
167847 }else{
167848 c->nBytes = nBytes;
167849 }
167850 c->iOffset = 0; /* start tokenizing at the beginning */
167851 c->iToken = 0;
167852 c->pToken = NULL; /* no space allocated, yet. */
167853 c->nTokenAllocated = 0;
167854
167855 *ppCursor = &c->base;
167856 return SQLITE_OK;
167857}
167858
167859/*
167860** Close a tokenization cursor previously opened by a call to
167861** simpleOpen() above.
167862*/
167863static int simpleClose(sqlite3_tokenizer_cursor *pCursor){
167864 simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
167865 sqlite3_free(c->pToken);
167866 sqlite3_free(c);
167867 return SQLITE_OK;
167868}
167869
167870/*
167871** Extract the next token from a tokenization cursor. The cursor must
167872** have been opened by a prior call to simpleOpen().
167873*/
167874static int simpleNext(
167875 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */
167876 const char **ppToken, /* OUT: *ppToken is the token text */
167877 int *pnBytes, /* OUT: Number of bytes in token */
167878 int *piStartOffset, /* OUT: Starting offset of token */
167879 int *piEndOffset, /* OUT: Ending offset of token */
167880 int *piPosition /* OUT: Position integer of token */
167881){
167882 simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
167883 simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
167884 unsigned char *p = (unsigned char *)c->pInput;
167885
167886 while( c->iOffset<c->nBytes ){
167887 int iStartOffset;
167888
167889 /* Scan past delimiter characters */
167890 while( c->iOffset<c->nBytes && simpleDelim(t, p[c->iOffset]) ){
167891 c->iOffset++;
167892 }
167893
167894 /* Count non-delimiter characters. */
167895 iStartOffset = c->iOffset;
167896 while( c->iOffset<c->nBytes && !simpleDelim(t, p[c->iOffset]) ){
167897 c->iOffset++;
167898 }
167899
167900 if( c->iOffset>iStartOffset ){
167901 int i, n = c->iOffset-iStartOffset;
167902 if( n>c->nTokenAllocated ){
167903 char *pNew;
167904 c->nTokenAllocated = n+20;
167905 pNew = sqlite3_realloc(c->pToken, c->nTokenAllocated);
167906 if( !pNew ) return SQLITE_NOMEM;
167907 c->pToken = pNew;
167908 }
167909 for(i=0; i<n; i++){
167910 /* TODO(shess) This needs expansion to handle UTF-8
167911 ** case-insensitivity.
167912 */
167913 unsigned char ch = p[iStartOffset+i];
167914 c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch);
167915 }
167916 *ppToken = c->pToken;
167917 *pnBytes = n;
167918 *piStartOffset = iStartOffset;
167919 *piEndOffset = c->iOffset;
167920 *piPosition = c->iToken++;
167921
167922 return SQLITE_OK;
167923 }
167924 }
167925 return SQLITE_DONE;
167926}
167927
167928/*
167929** The set of routines that implement the simple tokenizer
167930*/
167931static const sqlite3_tokenizer_module simpleTokenizerModule = {
167932 0,
167933 simpleCreate,
167934 simpleDestroy,
167935 simpleOpen,
167936 simpleClose,
167937 simpleNext,
167938 0,
167939};
167940
167941/*
167942** Allocate a new simple tokenizer. Return a pointer to the new
167943** tokenizer in *ppModule
167944*/
167945SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(
167946 sqlite3_tokenizer_module const**ppModule
167947){
167948 *ppModule = &simpleTokenizerModule;
167949}
167950
167951#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
167952
167953/************** End of fts3_tokenizer1.c *************************************/
167954/************** Begin file fts3_tokenize_vtab.c ******************************/
167955/*
167956** 2013 Apr 22
167957**
167958** The author disclaims copyright to this source code. In place of
167959** a legal notice, here is a blessing:
167960**
167961** May you do good and not evil.
167962** May you find forgiveness for yourself and forgive others.
167963** May you share freely, never taking more than you give.
167964**
167965******************************************************************************
167966**
167967** This file contains code for the "fts3tokenize" virtual table module.
167968** An fts3tokenize virtual table is created as follows:
167969**
167970** CREATE VIRTUAL TABLE <tbl> USING fts3tokenize(
167971** <tokenizer-name>, <arg-1>, ...
167972** );
167973**
167974** The table created has the following schema:
167975**
167976** CREATE TABLE <tbl>(input, token, start, end, position)
167977**
167978** When queried, the query must include a WHERE clause of type:
167979**
167980** input = <string>
167981**
167982** The virtual table module tokenizes this <string>, using the FTS3
167983** tokenizer specified by the arguments to the CREATE VIRTUAL TABLE
167984** statement and returns one row for each token in the result. With
167985** fields set as follows:
167986**
167987** input: Always set to a copy of <string>
167988** token: A token from the input.
167989** start: Byte offset of the token within the input <string>.
167990** end: Byte offset of the byte immediately following the end of the
167991** token within the input string.
167992** pos: Token offset of token within input.
167993**
167994*/
167995/* #include "fts3Int.h" */
167996#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
167997
167998/* #include <string.h> */
167999/* #include <assert.h> */
168000
168001typedef struct Fts3tokTable Fts3tokTable;
168002typedef struct Fts3tokCursor Fts3tokCursor;
168003
168004/*
168005** Virtual table structure.
168006*/
168007struct Fts3tokTable {
168008 sqlite3_vtab base; /* Base class used by SQLite core */
168009 const sqlite3_tokenizer_module *pMod;
168010 sqlite3_tokenizer *pTok;
168011};
168012
168013/*
168014** Virtual table cursor structure.
168015*/
168016struct Fts3tokCursor {
168017 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
168018 char *zInput; /* Input string */
168019 sqlite3_tokenizer_cursor *pCsr; /* Cursor to iterate through zInput */
168020 int iRowid; /* Current 'rowid' value */
168021 const char *zToken; /* Current 'token' value */
168022 int nToken; /* Size of zToken in bytes */
168023 int iStart; /* Current 'start' value */
168024 int iEnd; /* Current 'end' value */
168025 int iPos; /* Current 'pos' value */
168026};
168027
168028/*
168029** Query FTS for the tokenizer implementation named zName.
168030*/
168031static int fts3tokQueryTokenizer(
168032 Fts3Hash *pHash,
168033 const char *zName,
168034 const sqlite3_tokenizer_module **pp,
168035 char **pzErr
168036){
168037 sqlite3_tokenizer_module *p;
168038 int nName = (int)strlen(zName);
168039
168040 p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
168041 if( !p ){
168042 sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", zName);
168043 return SQLITE_ERROR;
168044 }
168045
168046 *pp = p;
168047 return SQLITE_OK;
168048}
168049
168050/*
168051** The second argument, argv[], is an array of pointers to nul-terminated
168052** strings. This function makes a copy of the array and strings into a
168053** single block of memory. It then dequotes any of the strings that appear
168054** to be quoted.
168055**
168056** If successful, output parameter *pazDequote is set to point at the
168057** array of dequoted strings and SQLITE_OK is returned. The caller is
168058** responsible for eventually calling sqlite3_free() to free the array
168059** in this case. Or, if an error occurs, an SQLite error code is returned.
168060** The final value of *pazDequote is undefined in this case.
168061*/
168062static int fts3tokDequoteArray(
168063 int argc, /* Number of elements in argv[] */
168064 const char * const *argv, /* Input array */
168065 char ***pazDequote /* Output array */
168066){
168067 int rc = SQLITE_OK; /* Return code */
168068 if( argc==0 ){
168069 *pazDequote = 0;
168070 }else{
168071 int i;
168072 int nByte = 0;
168073 char **azDequote;
168074
168075 for(i=0; i<argc; i++){
168076 nByte += (int)(strlen(argv[i]) + 1);
168077 }
168078
168079 *pazDequote = azDequote = sqlite3_malloc(sizeof(char *)*argc + nByte);
168080 if( azDequote==0 ){
168081 rc = SQLITE_NOMEM;
168082 }else{
168083 char *pSpace = (char *)&azDequote[argc];
168084 for(i=0; i<argc; i++){
168085 int n = (int)strlen(argv[i]);
168086 azDequote[i] = pSpace;
168087 memcpy(pSpace, argv[i], n+1);
168088 sqlite3Fts3Dequote(pSpace);
168089 pSpace += (n+1);
168090 }
168091 }
168092 }
168093
168094 return rc;
168095}
168096
168097/*
168098** Schema of the tokenizer table.
168099*/
168100#define FTS3_TOK_SCHEMA "CREATE TABLE x(input, token, start, end, position)"
168101
168102/*
168103** This function does all the work for both the xConnect and xCreate methods.
168104** These tables have no persistent representation of their own, so xConnect
168105** and xCreate are identical operations.
168106**
168107** argv[0]: module name
168108** argv[1]: database name
168109** argv[2]: table name
168110** argv[3]: first argument (tokenizer name)
168111*/
168112static int fts3tokConnectMethod(
168113 sqlite3 *db, /* Database connection */
168114 void *pHash, /* Hash table of tokenizers */
168115 int argc, /* Number of elements in argv array */
168116 const char * const *argv, /* xCreate/xConnect argument array */
168117 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
168118 char **pzErr /* OUT: sqlite3_malloc'd error message */
168119){
168120 Fts3tokTable *pTab = 0;
168121 const sqlite3_tokenizer_module *pMod = 0;
168122 sqlite3_tokenizer *pTok = 0;
168123 int rc;
168124 char **azDequote = 0;
168125 int nDequote;
168126
168127 rc = sqlite3_declare_vtab(db, FTS3_TOK_SCHEMA);
168128 if( rc!=SQLITE_OK ) return rc;
168129
168130 nDequote = argc-3;
168131 rc = fts3tokDequoteArray(nDequote, &argv[3], &azDequote);
168132
168133 if( rc==SQLITE_OK ){
168134 const char *zModule;
168135 if( nDequote<1 ){
168136 zModule = "simple";
168137 }else{
168138 zModule = azDequote[0];
168139 }
168140 rc = fts3tokQueryTokenizer((Fts3Hash*)pHash, zModule, &pMod, pzErr);
168141 }
168142
168143 assert( (rc==SQLITE_OK)==(pMod!=0) );
168144 if( rc==SQLITE_OK ){
168145 const char * const *azArg = (const char * const *)&azDequote[1];
168146 rc = pMod->xCreate((nDequote>1 ? nDequote-1 : 0), azArg, &pTok);
168147 }
168148
168149 if( rc==SQLITE_OK ){
168150 pTab = (Fts3tokTable *)sqlite3_malloc(sizeof(Fts3tokTable));
168151 if( pTab==0 ){
168152 rc = SQLITE_NOMEM;
168153 }
168154 }
168155
168156 if( rc==SQLITE_OK ){
168157 memset(pTab, 0, sizeof(Fts3tokTable));
168158 pTab->pMod = pMod;
168159 pTab->pTok = pTok;
168160 *ppVtab = &pTab->base;
168161 }else{
168162 if( pTok ){
168163 pMod->xDestroy(pTok);
168164 }
168165 }
168166
168167 sqlite3_free(azDequote);
168168 return rc;
168169}
168170
168171/*
168172** This function does the work for both the xDisconnect and xDestroy methods.
168173** These tables have no persistent representation of their own, so xDisconnect
168174** and xDestroy are identical operations.
168175*/
168176static int fts3tokDisconnectMethod(sqlite3_vtab *pVtab){
168177 Fts3tokTable *pTab = (Fts3tokTable *)pVtab;
168178
168179 pTab->pMod->xDestroy(pTab->pTok);
168180 sqlite3_free(pTab);
168181 return SQLITE_OK;
168182}
168183
168184/*
168185** xBestIndex - Analyze a WHERE and ORDER BY clause.
168186*/
168187static int fts3tokBestIndexMethod(
168188 sqlite3_vtab *pVTab,
168189 sqlite3_index_info *pInfo
168190){
168191 int i;
168192 UNUSED_PARAMETER(pVTab);
168193
168194 for(i=0; i<pInfo->nConstraint; i++){
168195 if( pInfo->aConstraint[i].usable
168196 && pInfo->aConstraint[i].iColumn==0
168197 && pInfo->aConstraint[i].op==SQLITE_INDEX_CONSTRAINT_EQ
168198 ){
168199 pInfo->idxNum = 1;
168200 pInfo->aConstraintUsage[i].argvIndex = 1;
168201 pInfo->aConstraintUsage[i].omit = 1;
168202 pInfo->estimatedCost = 1;
168203 return SQLITE_OK;
168204 }
168205 }
168206
168207 pInfo->idxNum = 0;
168208 assert( pInfo->estimatedCost>1000000.0 );
168209
168210 return SQLITE_OK;
168211}
168212
168213/*
168214** xOpen - Open a cursor.
168215*/
168216static int fts3tokOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
168217 Fts3tokCursor *pCsr;
168218 UNUSED_PARAMETER(pVTab);
168219
168220 pCsr = (Fts3tokCursor *)sqlite3_malloc(sizeof(Fts3tokCursor));
168221 if( pCsr==0 ){
168222 return SQLITE_NOMEM;
168223 }
168224 memset(pCsr, 0, sizeof(Fts3tokCursor));
168225
168226 *ppCsr = (sqlite3_vtab_cursor *)pCsr;
168227 return SQLITE_OK;
168228}
168229
168230/*
168231** Reset the tokenizer cursor passed as the only argument. As if it had
168232** just been returned by fts3tokOpenMethod().
168233*/
168234static void fts3tokResetCursor(Fts3tokCursor *pCsr){
168235 if( pCsr->pCsr ){
168236 Fts3tokTable *pTab = (Fts3tokTable *)(pCsr->base.pVtab);
168237 pTab->pMod->xClose(pCsr->pCsr);
168238 pCsr->pCsr = 0;
168239 }
168240 sqlite3_free(pCsr->zInput);
168241 pCsr->zInput = 0;
168242 pCsr->zToken = 0;
168243 pCsr->nToken = 0;
168244 pCsr->iStart = 0;
168245 pCsr->iEnd = 0;
168246 pCsr->iPos = 0;
168247 pCsr->iRowid = 0;
168248}
168249
168250/*
168251** xClose - Close a cursor.
168252*/
168253static int fts3tokCloseMethod(sqlite3_vtab_cursor *pCursor){
168254 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
168255
168256 fts3tokResetCursor(pCsr);
168257 sqlite3_free(pCsr);
168258 return SQLITE_OK;
168259}
168260
168261/*
168262** xNext - Advance the cursor to the next row, if any.
168263*/
168264static int fts3tokNextMethod(sqlite3_vtab_cursor *pCursor){
168265 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
168266 Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
168267 int rc; /* Return code */
168268
168269 pCsr->iRowid++;
168270 rc = pTab->pMod->xNext(pCsr->pCsr,
168271 &pCsr->zToken, &pCsr->nToken,
168272 &pCsr->iStart, &pCsr->iEnd, &pCsr->iPos
168273 );
168274
168275 if( rc!=SQLITE_OK ){
168276 fts3tokResetCursor(pCsr);
168277 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
168278 }
168279
168280 return rc;
168281}
168282
168283/*
168284** xFilter - Initialize a cursor to point at the start of its data.
168285*/
168286static int fts3tokFilterMethod(
168287 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
168288 int idxNum, /* Strategy index */
168289 const char *idxStr, /* Unused */
168290 int nVal, /* Number of elements in apVal */
168291 sqlite3_value **apVal /* Arguments for the indexing scheme */
168292){
168293 int rc = SQLITE_ERROR;
168294 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
168295 Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
168296 UNUSED_PARAMETER(idxStr);
168297 UNUSED_PARAMETER(nVal);
168298
168299 fts3tokResetCursor(pCsr);
168300 if( idxNum==1 ){
168301 const char *zByte = (const char *)sqlite3_value_text(apVal[0]);
168302 int nByte = sqlite3_value_bytes(apVal[0]);
168303 pCsr->zInput = sqlite3_malloc(nByte+1);
168304 if( pCsr->zInput==0 ){
168305 rc = SQLITE_NOMEM;
168306 }else{
168307 memcpy(pCsr->zInput, zByte, nByte);
168308 pCsr->zInput[nByte] = 0;
168309 rc = pTab->pMod->xOpen(pTab->pTok, pCsr->zInput, nByte, &pCsr->pCsr);
168310 if( rc==SQLITE_OK ){
168311 pCsr->pCsr->pTokenizer = pTab->pTok;
168312 }
168313 }
168314 }
168315
168316 if( rc!=SQLITE_OK ) return rc;
168317 return fts3tokNextMethod(pCursor);
168318}
168319
168320/*
168321** xEof - Return true if the cursor is at EOF, or false otherwise.
168322*/
168323static int fts3tokEofMethod(sqlite3_vtab_cursor *pCursor){
168324 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
168325 return (pCsr->zToken==0);
168326}
168327
168328/*
168329** xColumn - Return a column value.
168330*/
168331static int fts3tokColumnMethod(
168332 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
168333 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
168334 int iCol /* Index of column to read value from */
168335){
168336 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
168337
168338 /* CREATE TABLE x(input, token, start, end, position) */
168339 switch( iCol ){
168340 case 0:
168341 sqlite3_result_text(pCtx, pCsr->zInput, -1, SQLITE_TRANSIENT);
168342 break;
168343 case 1:
168344 sqlite3_result_text(pCtx, pCsr->zToken, pCsr->nToken, SQLITE_TRANSIENT);
168345 break;
168346 case 2:
168347 sqlite3_result_int(pCtx, pCsr->iStart);
168348 break;
168349 case 3:
168350 sqlite3_result_int(pCtx, pCsr->iEnd);
168351 break;
168352 default:
168353 assert( iCol==4 );
168354 sqlite3_result_int(pCtx, pCsr->iPos);
168355 break;
168356 }
168357 return SQLITE_OK;
168358}
168359
168360/*
168361** xRowid - Return the current rowid for the cursor.
168362*/
168363static int fts3tokRowidMethod(
168364 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
168365 sqlite_int64 *pRowid /* OUT: Rowid value */
168366){
168367 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
168368 *pRowid = (sqlite3_int64)pCsr->iRowid;
168369 return SQLITE_OK;
168370}
168371
168372/*
168373** Register the fts3tok module with database connection db. Return SQLITE_OK
168374** if successful or an error code if sqlite3_create_module() fails.
168375*/
168376SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash){
168377 static const sqlite3_module fts3tok_module = {
168378 0, /* iVersion */
168379 fts3tokConnectMethod, /* xCreate */
168380 fts3tokConnectMethod, /* xConnect */
168381 fts3tokBestIndexMethod, /* xBestIndex */
168382 fts3tokDisconnectMethod, /* xDisconnect */
168383 fts3tokDisconnectMethod, /* xDestroy */
168384 fts3tokOpenMethod, /* xOpen */
168385 fts3tokCloseMethod, /* xClose */
168386 fts3tokFilterMethod, /* xFilter */
168387 fts3tokNextMethod, /* xNext */
168388 fts3tokEofMethod, /* xEof */
168389 fts3tokColumnMethod, /* xColumn */
168390 fts3tokRowidMethod, /* xRowid */
168391 0, /* xUpdate */
168392 0, /* xBegin */
168393 0, /* xSync */
168394 0, /* xCommit */
168395 0, /* xRollback */
168396 0, /* xFindFunction */
168397 0, /* xRename */
168398 0, /* xSavepoint */
168399 0, /* xRelease */
168400 0, /* xRollbackTo */
168401 0 /* xShadowName */
168402 };
168403 int rc; /* Return code */
168404
168405 rc = sqlite3_create_module(db, "fts3tokenize", &fts3tok_module, (void*)pHash);
168406 return rc;
168407}
168408
168409#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
168410
168411/************** End of fts3_tokenize_vtab.c **********************************/
168412/************** Begin file fts3_write.c **************************************/
168413/*
168414** 2009 Oct 23
168415**
168416** The author disclaims copyright to this source code. In place of
168417** a legal notice, here is a blessing:
168418**
168419** May you do good and not evil.
168420** May you find forgiveness for yourself and forgive others.
168421** May you share freely, never taking more than you give.
168422**
168423******************************************************************************
168424**
168425** This file is part of the SQLite FTS3 extension module. Specifically,
168426** this file contains code to insert, update and delete rows from FTS3
168427** tables. It also contains code to merge FTS3 b-tree segments. Some
168428** of the sub-routines used to merge segments are also used by the query
168429** code in fts3.c.
168430*/
168431
168432/* #include "fts3Int.h" */
168433#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
168434
168435/* #include <string.h> */
168436/* #include <assert.h> */
168437/* #include <stdlib.h> */
168438
168439
168440#define FTS_MAX_APPENDABLE_HEIGHT 16
168441
168442/*
168443** When full-text index nodes are loaded from disk, the buffer that they
168444** are loaded into has the following number of bytes of padding at the end
168445** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
168446** of 920 bytes is allocated for it.
168447**
168448** This means that if we have a pointer into a buffer containing node data,
168449** it is always safe to read up to two varints from it without risking an
168450** overread, even if the node data is corrupted.
168451*/
168452#define FTS3_NODE_PADDING (FTS3_VARINT_MAX*2)
168453
168454/*
168455** Under certain circumstances, b-tree nodes (doclists) can be loaded into
168456** memory incrementally instead of all at once. This can be a big performance
168457** win (reduced IO and CPU) if SQLite stops calling the virtual table xNext()
168458** method before retrieving all query results (as may happen, for example,
168459** if a query has a LIMIT clause).
168460**
168461** Incremental loading is used for b-tree nodes FTS3_NODE_CHUNK_THRESHOLD
168462** bytes and larger. Nodes are loaded in chunks of FTS3_NODE_CHUNKSIZE bytes.
168463** The code is written so that the hard lower-limit for each of these values
168464** is 1. Clearly such small values would be inefficient, but can be useful
168465** for testing purposes.
168466**
168467** If this module is built with SQLITE_TEST defined, these constants may
168468** be overridden at runtime for testing purposes. File fts3_test.c contains
168469** a Tcl interface to read and write the values.
168470*/
168471#ifdef SQLITE_TEST
168472int test_fts3_node_chunksize = (4*1024);
168473int test_fts3_node_chunk_threshold = (4*1024)*4;
168474# define FTS3_NODE_CHUNKSIZE test_fts3_node_chunksize
168475# define FTS3_NODE_CHUNK_THRESHOLD test_fts3_node_chunk_threshold
168476#else
168477# define FTS3_NODE_CHUNKSIZE (4*1024)
168478# define FTS3_NODE_CHUNK_THRESHOLD (FTS3_NODE_CHUNKSIZE*4)
168479#endif
168480
168481/*
168482** The two values that may be meaningfully bound to the :1 parameter in
168483** statements SQL_REPLACE_STAT and SQL_SELECT_STAT.
168484*/
168485#define FTS_STAT_DOCTOTAL 0
168486#define FTS_STAT_INCRMERGEHINT 1
168487#define FTS_STAT_AUTOINCRMERGE 2
168488
168489/*
168490** If FTS_LOG_MERGES is defined, call sqlite3_log() to report each automatic
168491** and incremental merge operation that takes place. This is used for
168492** debugging FTS only, it should not usually be turned on in production
168493** systems.
168494*/
168495#ifdef FTS3_LOG_MERGES
168496static void fts3LogMerge(int nMerge, sqlite3_int64 iAbsLevel){
168497 sqlite3_log(SQLITE_OK, "%d-way merge from level %d", nMerge, (int)iAbsLevel);
168498}
168499#else
168500#define fts3LogMerge(x, y)
168501#endif
168502
168503
168504typedef struct PendingList PendingList;
168505typedef struct SegmentNode SegmentNode;
168506typedef struct SegmentWriter SegmentWriter;
168507
168508/*
168509** An instance of the following data structure is used to build doclists
168510** incrementally. See function fts3PendingListAppend() for details.
168511*/
168512struct PendingList {
168513 int nData;
168514 char *aData;
168515 int nSpace;
168516 sqlite3_int64 iLastDocid;
168517 sqlite3_int64 iLastCol;
168518 sqlite3_int64 iLastPos;
168519};
168520
168521
168522/*
168523** Each cursor has a (possibly empty) linked list of the following objects.
168524*/
168525struct Fts3DeferredToken {
168526 Fts3PhraseToken *pToken; /* Pointer to corresponding expr token */
168527 int iCol; /* Column token must occur in */
168528 Fts3DeferredToken *pNext; /* Next in list of deferred tokens */
168529 PendingList *pList; /* Doclist is assembled here */
168530};
168531
168532/*
168533** An instance of this structure is used to iterate through the terms on
168534** a contiguous set of segment b-tree leaf nodes. Although the details of
168535** this structure are only manipulated by code in this file, opaque handles
168536** of type Fts3SegReader* are also used by code in fts3.c to iterate through
168537** terms when querying the full-text index. See functions:
168538**
168539** sqlite3Fts3SegReaderNew()
168540** sqlite3Fts3SegReaderFree()
168541** sqlite3Fts3SegReaderIterate()
168542**
168543** Methods used to manipulate Fts3SegReader structures:
168544**
168545** fts3SegReaderNext()
168546** fts3SegReaderFirstDocid()
168547** fts3SegReaderNextDocid()
168548*/
168549struct Fts3SegReader {
168550 int iIdx; /* Index within level, or 0x7FFFFFFF for PT */
168551 u8 bLookup; /* True for a lookup only */
168552 u8 rootOnly; /* True for a root-only reader */
168553
168554 sqlite3_int64 iStartBlock; /* Rowid of first leaf block to traverse */
168555 sqlite3_int64 iLeafEndBlock; /* Rowid of final leaf block to traverse */
168556 sqlite3_int64 iEndBlock; /* Rowid of final block in segment (or 0) */
168557 sqlite3_int64 iCurrentBlock; /* Current leaf block (or 0) */
168558
168559 char *aNode; /* Pointer to node data (or NULL) */
168560 int nNode; /* Size of buffer at aNode (or 0) */
168561 int nPopulate; /* If >0, bytes of buffer aNode[] loaded */
168562 sqlite3_blob *pBlob; /* If not NULL, blob handle to read node */
168563
168564 Fts3HashElem **ppNextElem;
168565
168566 /* Variables set by fts3SegReaderNext(). These may be read directly
168567 ** by the caller. They are valid from the time SegmentReaderNew() returns
168568 ** until SegmentReaderNext() returns something other than SQLITE_OK
168569 ** (i.e. SQLITE_DONE).
168570 */
168571 int nTerm; /* Number of bytes in current term */
168572 char *zTerm; /* Pointer to current term */
168573 int nTermAlloc; /* Allocated size of zTerm buffer */
168574 char *aDoclist; /* Pointer to doclist of current entry */
168575 int nDoclist; /* Size of doclist in current entry */
168576
168577 /* The following variables are used by fts3SegReaderNextDocid() to iterate
168578 ** through the current doclist (aDoclist/nDoclist).
168579 */
168580 char *pOffsetList;
168581 int nOffsetList; /* For descending pending seg-readers only */
168582 sqlite3_int64 iDocid;
168583};
168584
168585#define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
168586#define fts3SegReaderIsRootOnly(p) ((p)->rootOnly!=0)
168587
168588/*
168589** An instance of this structure is used to create a segment b-tree in the
168590** database. The internal details of this type are only accessed by the
168591** following functions:
168592**
168593** fts3SegWriterAdd()
168594** fts3SegWriterFlush()
168595** fts3SegWriterFree()
168596*/
168597struct SegmentWriter {
168598 SegmentNode *pTree; /* Pointer to interior tree structure */
168599 sqlite3_int64 iFirst; /* First slot in %_segments written */
168600 sqlite3_int64 iFree; /* Next free slot in %_segments */
168601 char *zTerm; /* Pointer to previous term buffer */
168602 int nTerm; /* Number of bytes in zTerm */
168603 int nMalloc; /* Size of malloc'd buffer at zMalloc */
168604 char *zMalloc; /* Malloc'd space (possibly) used for zTerm */
168605 int nSize; /* Size of allocation at aData */
168606 int nData; /* Bytes of data in aData */
168607 char *aData; /* Pointer to block from malloc() */
168608 i64 nLeafData; /* Number of bytes of leaf data written */
168609};
168610
168611/*
168612** Type SegmentNode is used by the following three functions to create
168613** the interior part of the segment b+-tree structures (everything except
168614** the leaf nodes). These functions and type are only ever used by code
168615** within the fts3SegWriterXXX() family of functions described above.
168616**
168617** fts3NodeAddTerm()
168618** fts3NodeWrite()
168619** fts3NodeFree()
168620**
168621** When a b+tree is written to the database (either as a result of a merge
168622** or the pending-terms table being flushed), leaves are written into the
168623** database file as soon as they are completely populated. The interior of
168624** the tree is assembled in memory and written out only once all leaves have
168625** been populated and stored. This is Ok, as the b+-tree fanout is usually
168626** very large, meaning that the interior of the tree consumes relatively
168627** little memory.
168628*/
168629struct SegmentNode {
168630 SegmentNode *pParent; /* Parent node (or NULL for root node) */
168631 SegmentNode *pRight; /* Pointer to right-sibling */
168632 SegmentNode *pLeftmost; /* Pointer to left-most node of this depth */
168633 int nEntry; /* Number of terms written to node so far */
168634 char *zTerm; /* Pointer to previous term buffer */
168635 int nTerm; /* Number of bytes in zTerm */
168636 int nMalloc; /* Size of malloc'd buffer at zMalloc */
168637 char *zMalloc; /* Malloc'd space (possibly) used for zTerm */
168638 int nData; /* Bytes of valid data so far */
168639 char *aData; /* Node data */
168640};
168641
168642/*
168643** Valid values for the second argument to fts3SqlStmt().
168644*/
168645#define SQL_DELETE_CONTENT 0
168646#define SQL_IS_EMPTY 1
168647#define SQL_DELETE_ALL_CONTENT 2
168648#define SQL_DELETE_ALL_SEGMENTS 3
168649#define SQL_DELETE_ALL_SEGDIR 4
168650#define SQL_DELETE_ALL_DOCSIZE 5
168651#define SQL_DELETE_ALL_STAT 6
168652#define SQL_SELECT_CONTENT_BY_ROWID 7
168653#define SQL_NEXT_SEGMENT_INDEX 8
168654#define SQL_INSERT_SEGMENTS 9
168655#define SQL_NEXT_SEGMENTS_ID 10
168656#define SQL_INSERT_SEGDIR 11
168657#define SQL_SELECT_LEVEL 12
168658#define SQL_SELECT_LEVEL_RANGE 13
168659#define SQL_SELECT_LEVEL_COUNT 14
168660#define SQL_SELECT_SEGDIR_MAX_LEVEL 15
168661#define SQL_DELETE_SEGDIR_LEVEL 16
168662#define SQL_DELETE_SEGMENTS_RANGE 17
168663#define SQL_CONTENT_INSERT 18
168664#define SQL_DELETE_DOCSIZE 19
168665#define SQL_REPLACE_DOCSIZE 20
168666#define SQL_SELECT_DOCSIZE 21
168667#define SQL_SELECT_STAT 22
168668#define SQL_REPLACE_STAT 23
168669
168670#define SQL_SELECT_ALL_PREFIX_LEVEL 24
168671#define SQL_DELETE_ALL_TERMS_SEGDIR 25
168672#define SQL_DELETE_SEGDIR_RANGE 26
168673#define SQL_SELECT_ALL_LANGID 27
168674#define SQL_FIND_MERGE_LEVEL 28
168675#define SQL_MAX_LEAF_NODE_ESTIMATE 29
168676#define SQL_DELETE_SEGDIR_ENTRY 30
168677#define SQL_SHIFT_SEGDIR_ENTRY 31
168678#define SQL_SELECT_SEGDIR 32
168679#define SQL_CHOMP_SEGDIR 33
168680#define SQL_SEGMENT_IS_APPENDABLE 34
168681#define SQL_SELECT_INDEXES 35
168682#define SQL_SELECT_MXLEVEL 36
168683
168684#define SQL_SELECT_LEVEL_RANGE2 37
168685#define SQL_UPDATE_LEVEL_IDX 38
168686#define SQL_UPDATE_LEVEL 39
168687
168688/*
168689** This function is used to obtain an SQLite prepared statement handle
168690** for the statement identified by the second argument. If successful,
168691** *pp is set to the requested statement handle and SQLITE_OK returned.
168692** Otherwise, an SQLite error code is returned and *pp is set to 0.
168693**
168694** If argument apVal is not NULL, then it must point to an array with
168695** at least as many entries as the requested statement has bound
168696** parameters. The values are bound to the statements parameters before
168697** returning.
168698*/
168699static int fts3SqlStmt(
168700 Fts3Table *p, /* Virtual table handle */
168701 int eStmt, /* One of the SQL_XXX constants above */
168702 sqlite3_stmt **pp, /* OUT: Statement handle */
168703 sqlite3_value **apVal /* Values to bind to statement */
168704){
168705 const char *azSql[] = {
168706/* 0 */ "DELETE FROM %Q.'%q_content' WHERE rowid = ?",
168707/* 1 */ "SELECT NOT EXISTS(SELECT docid FROM %Q.'%q_content' WHERE rowid!=?)",
168708/* 2 */ "DELETE FROM %Q.'%q_content'",
168709/* 3 */ "DELETE FROM %Q.'%q_segments'",
168710/* 4 */ "DELETE FROM %Q.'%q_segdir'",
168711/* 5 */ "DELETE FROM %Q.'%q_docsize'",
168712/* 6 */ "DELETE FROM %Q.'%q_stat'",
168713/* 7 */ "SELECT %s WHERE rowid=?",
168714/* 8 */ "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
168715/* 9 */ "REPLACE INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
168716/* 10 */ "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
168717/* 11 */ "REPLACE INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
168718
168719 /* Return segments in order from oldest to newest.*/
168720/* 12 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
168721 "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC",
168722/* 13 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
168723 "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?"
168724 "ORDER BY level DESC, idx ASC",
168725
168726/* 14 */ "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?",
168727/* 15 */ "SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
168728
168729/* 16 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
168730/* 17 */ "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
168731/* 18 */ "INSERT INTO %Q.'%q_content' VALUES(%s)",
168732/* 19 */ "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
168733/* 20 */ "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
168734/* 21 */ "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
168735/* 22 */ "SELECT value FROM %Q.'%q_stat' WHERE id=?",
168736/* 23 */ "REPLACE INTO %Q.'%q_stat' VALUES(?,?)",
168737/* 24 */ "",
168738/* 25 */ "",
168739
168740/* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
168741/* 27 */ "SELECT ? UNION SELECT level / (1024 * ?) FROM %Q.'%q_segdir'",
168742
168743/* This statement is used to determine which level to read the input from
168744** when performing an incremental merge. It returns the absolute level number
168745** of the oldest level in the db that contains at least ? segments. Or,
168746** if no level in the FTS index contains more than ? segments, the statement
168747** returns zero rows. */
168748/* 28 */ "SELECT level, count(*) AS cnt FROM %Q.'%q_segdir' "
168749 " GROUP BY level HAVING cnt>=?"
168750 " ORDER BY (level %% 1024) ASC LIMIT 1",
168751
168752/* Estimate the upper limit on the number of leaf nodes in a new segment
168753** created by merging the oldest :2 segments from absolute level :1. See
168754** function sqlite3Fts3Incrmerge() for details. */
168755/* 29 */ "SELECT 2 * total(1 + leaves_end_block - start_block) "
168756 " FROM %Q.'%q_segdir' WHERE level = ? AND idx < ?",
168757
168758/* SQL_DELETE_SEGDIR_ENTRY
168759** Delete the %_segdir entry on absolute level :1 with index :2. */
168760/* 30 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
168761
168762/* SQL_SHIFT_SEGDIR_ENTRY
168763** Modify the idx value for the segment with idx=:3 on absolute level :2
168764** to :1. */
168765/* 31 */ "UPDATE %Q.'%q_segdir' SET idx = ? WHERE level=? AND idx=?",
168766
168767/* SQL_SELECT_SEGDIR
168768** Read a single entry from the %_segdir table. The entry from absolute
168769** level :1 with index value :2. */
168770/* 32 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
168771 "FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
168772
168773/* SQL_CHOMP_SEGDIR
168774** Update the start_block (:1) and root (:2) fields of the %_segdir
168775** entry located on absolute level :3 with index :4. */
168776/* 33 */ "UPDATE %Q.'%q_segdir' SET start_block = ?, root = ?"
168777 "WHERE level = ? AND idx = ?",
168778
168779/* SQL_SEGMENT_IS_APPENDABLE
168780** Return a single row if the segment with end_block=? is appendable. Or
168781** no rows otherwise. */
168782/* 34 */ "SELECT 1 FROM %Q.'%q_segments' WHERE blockid=? AND block IS NULL",
168783
168784/* SQL_SELECT_INDEXES
168785** Return the list of valid segment indexes for absolute level ? */
168786/* 35 */ "SELECT idx FROM %Q.'%q_segdir' WHERE level=? ORDER BY 1 ASC",
168787
168788/* SQL_SELECT_MXLEVEL
168789** Return the largest relative level in the FTS index or indexes. */
168790/* 36 */ "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'",
168791
168792 /* Return segments in order from oldest to newest.*/
168793/* 37 */ "SELECT level, idx, end_block "
168794 "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ? "
168795 "ORDER BY level DESC, idx ASC",
168796
168797 /* Update statements used while promoting segments */
168798/* 38 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=-1,idx=? "
168799 "WHERE level=? AND idx=?",
168800/* 39 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=? WHERE level=-1"
168801
168802 };
168803 int rc = SQLITE_OK;
168804 sqlite3_stmt *pStmt;
168805
168806 assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
168807 assert( eStmt<SizeofArray(azSql) && eStmt>=0 );
168808
168809 pStmt = p->aStmt[eStmt];
168810 if( !pStmt ){
168811 char *zSql;
168812 if( eStmt==SQL_CONTENT_INSERT ){
168813 zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
168814 }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){
168815 zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist);
168816 }else{
168817 zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
168818 }
168819 if( !zSql ){
168820 rc = SQLITE_NOMEM;
168821 }else{
168822 rc = sqlite3_prepare_v3(p->db, zSql, -1, SQLITE_PREPARE_PERSISTENT,
168823 &pStmt, NULL);
168824 sqlite3_free(zSql);
168825 assert( rc==SQLITE_OK || pStmt==0 );
168826 p->aStmt[eStmt] = pStmt;
168827 }
168828 }
168829 if( apVal ){
168830 int i;
168831 int nParam = sqlite3_bind_parameter_count(pStmt);
168832 for(i=0; rc==SQLITE_OK && i<nParam; i++){
168833 rc = sqlite3_bind_value(pStmt, i+1, apVal[i]);
168834 }
168835 }
168836 *pp = pStmt;
168837 return rc;
168838}
168839
168840
168841static int fts3SelectDocsize(
168842 Fts3Table *pTab, /* FTS3 table handle */
168843 sqlite3_int64 iDocid, /* Docid to bind for SQL_SELECT_DOCSIZE */
168844 sqlite3_stmt **ppStmt /* OUT: Statement handle */
168845){
168846 sqlite3_stmt *pStmt = 0; /* Statement requested from fts3SqlStmt() */
168847 int rc; /* Return code */
168848
168849 rc = fts3SqlStmt(pTab, SQL_SELECT_DOCSIZE, &pStmt, 0);
168850 if( rc==SQLITE_OK ){
168851 sqlite3_bind_int64(pStmt, 1, iDocid);
168852 rc = sqlite3_step(pStmt);
168853 if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){
168854 rc = sqlite3_reset(pStmt);
168855 if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
168856 pStmt = 0;
168857 }else{
168858 rc = SQLITE_OK;
168859 }
168860 }
168861
168862 *ppStmt = pStmt;
168863 return rc;
168864}
168865
168866SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(
168867 Fts3Table *pTab, /* Fts3 table handle */
168868 sqlite3_stmt **ppStmt /* OUT: Statement handle */
168869){
168870 sqlite3_stmt *pStmt = 0;
168871 int rc;
168872 rc = fts3SqlStmt(pTab, SQL_SELECT_STAT, &pStmt, 0);
168873 if( rc==SQLITE_OK ){
168874 sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
168875 if( sqlite3_step(pStmt)!=SQLITE_ROW
168876 || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB
168877 ){
168878 rc = sqlite3_reset(pStmt);
168879 if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
168880 pStmt = 0;
168881 }
168882 }
168883 *ppStmt = pStmt;
168884 return rc;
168885}
168886
168887SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(
168888 Fts3Table *pTab, /* Fts3 table handle */
168889 sqlite3_int64 iDocid, /* Docid to read size data for */
168890 sqlite3_stmt **ppStmt /* OUT: Statement handle */
168891){
168892 return fts3SelectDocsize(pTab, iDocid, ppStmt);
168893}
168894
168895/*
168896** Similar to fts3SqlStmt(). Except, after binding the parameters in
168897** array apVal[] to the SQL statement identified by eStmt, the statement
168898** is executed.
168899**
168900** Returns SQLITE_OK if the statement is successfully executed, or an
168901** SQLite error code otherwise.
168902*/
168903static void fts3SqlExec(
168904 int *pRC, /* Result code */
168905 Fts3Table *p, /* The FTS3 table */
168906 int eStmt, /* Index of statement to evaluate */
168907 sqlite3_value **apVal /* Parameters to bind */
168908){
168909 sqlite3_stmt *pStmt;
168910 int rc;
168911 if( *pRC ) return;
168912 rc = fts3SqlStmt(p, eStmt, &pStmt, apVal);
168913 if( rc==SQLITE_OK ){
168914 sqlite3_step(pStmt);
168915 rc = sqlite3_reset(pStmt);
168916 }
168917 *pRC = rc;
168918}
168919
168920
168921/*
168922** This function ensures that the caller has obtained an exclusive
168923** shared-cache table-lock on the %_segdir table. This is required before
168924** writing data to the fts3 table. If this lock is not acquired first, then
168925** the caller may end up attempting to take this lock as part of committing
168926** a transaction, causing SQLite to return SQLITE_LOCKED or
168927** LOCKED_SHAREDCACHEto a COMMIT command.
168928**
168929** It is best to avoid this because if FTS3 returns any error when
168930** committing a transaction, the whole transaction will be rolled back.
168931** And this is not what users expect when they get SQLITE_LOCKED_SHAREDCACHE.
168932** It can still happen if the user locks the underlying tables directly
168933** instead of accessing them via FTS.
168934*/
168935static int fts3Writelock(Fts3Table *p){
168936 int rc = SQLITE_OK;
168937
168938 if( p->nPendingData==0 ){
168939 sqlite3_stmt *pStmt;
168940 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pStmt, 0);
168941 if( rc==SQLITE_OK ){
168942 sqlite3_bind_null(pStmt, 1);
168943 sqlite3_step(pStmt);
168944 rc = sqlite3_reset(pStmt);
168945 }
168946 }
168947
168948 return rc;
168949}
168950
168951/*
168952** FTS maintains a separate indexes for each language-id (a 32-bit integer).
168953** Within each language id, a separate index is maintained to store the
168954** document terms, and each configured prefix size (configured the FTS
168955** "prefix=" option). And each index consists of multiple levels ("relative
168956** levels").
168957**
168958** All three of these values (the language id, the specific index and the
168959** level within the index) are encoded in 64-bit integer values stored
168960** in the %_segdir table on disk. This function is used to convert three
168961** separate component values into the single 64-bit integer value that
168962** can be used to query the %_segdir table.
168963**
168964** Specifically, each language-id/index combination is allocated 1024
168965** 64-bit integer level values ("absolute levels"). The main terms index
168966** for language-id 0 is allocate values 0-1023. The first prefix index
168967** (if any) for language-id 0 is allocated values 1024-2047. And so on.
168968** Language 1 indexes are allocated immediately following language 0.
168969**
168970** So, for a system with nPrefix prefix indexes configured, the block of
168971** absolute levels that corresponds to language-id iLangid and index
168972** iIndex starts at absolute level ((iLangid * (nPrefix+1) + iIndex) * 1024).
168973*/
168974static sqlite3_int64 getAbsoluteLevel(
168975 Fts3Table *p, /* FTS3 table handle */
168976 int iLangid, /* Language id */
168977 int iIndex, /* Index in p->aIndex[] */
168978 int iLevel /* Level of segments */
168979){
168980 sqlite3_int64 iBase; /* First absolute level for iLangid/iIndex */
168981 assert( iLangid>=0 );
168982 assert( p->nIndex>0 );
168983 assert( iIndex>=0 && iIndex<p->nIndex );
168984
168985 iBase = ((sqlite3_int64)iLangid * p->nIndex + iIndex) * FTS3_SEGDIR_MAXLEVEL;
168986 return iBase + iLevel;
168987}
168988
168989/*
168990** Set *ppStmt to a statement handle that may be used to iterate through
168991** all rows in the %_segdir table, from oldest to newest. If successful,
168992** return SQLITE_OK. If an error occurs while preparing the statement,
168993** return an SQLite error code.
168994**
168995** There is only ever one instance of this SQL statement compiled for
168996** each FTS3 table.
168997**
168998** The statement returns the following columns from the %_segdir table:
168999**
169000** 0: idx
169001** 1: start_block
169002** 2: leaves_end_block
169003** 3: end_block
169004** 4: root
169005*/
169006SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(
169007 Fts3Table *p, /* FTS3 table */
169008 int iLangid, /* Language being queried */
169009 int iIndex, /* Index for p->aIndex[] */
169010 int iLevel, /* Level to select (relative level) */
169011 sqlite3_stmt **ppStmt /* OUT: Compiled statement */
169012){
169013 int rc;
169014 sqlite3_stmt *pStmt = 0;
169015
169016 assert( iLevel==FTS3_SEGCURSOR_ALL || iLevel>=0 );
169017 assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
169018 assert( iIndex>=0 && iIndex<p->nIndex );
169019
169020 if( iLevel<0 ){
169021 /* "SELECT * FROM %_segdir WHERE level BETWEEN ? AND ? ORDER BY ..." */
169022 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE, &pStmt, 0);
169023 if( rc==SQLITE_OK ){
169024 sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
169025 sqlite3_bind_int64(pStmt, 2,
169026 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
169027 );
169028 }
169029 }else{
169030 /* "SELECT * FROM %_segdir WHERE level = ? ORDER BY ..." */
169031 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
169032 if( rc==SQLITE_OK ){
169033 sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex,iLevel));
169034 }
169035 }
169036 *ppStmt = pStmt;
169037 return rc;
169038}
169039
169040
169041/*
169042** Append a single varint to a PendingList buffer. SQLITE_OK is returned
169043** if successful, or an SQLite error code otherwise.
169044**
169045** This function also serves to allocate the PendingList structure itself.
169046** For example, to create a new PendingList structure containing two
169047** varints:
169048**
169049** PendingList *p = 0;
169050** fts3PendingListAppendVarint(&p, 1);
169051** fts3PendingListAppendVarint(&p, 2);
169052*/
169053static int fts3PendingListAppendVarint(
169054 PendingList **pp, /* IN/OUT: Pointer to PendingList struct */
169055 sqlite3_int64 i /* Value to append to data */
169056){
169057 PendingList *p = *pp;
169058
169059 /* Allocate or grow the PendingList as required. */
169060 if( !p ){
169061 p = sqlite3_malloc(sizeof(*p) + 100);
169062 if( !p ){
169063 return SQLITE_NOMEM;
169064 }
169065 p->nSpace = 100;
169066 p->aData = (char *)&p[1];
169067 p->nData = 0;
169068 }
169069 else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){
169070 int nNew = p->nSpace * 2;
169071 p = sqlite3_realloc(p, sizeof(*p) + nNew);
169072 if( !p ){
169073 sqlite3_free(*pp);
169074 *pp = 0;
169075 return SQLITE_NOMEM;
169076 }
169077 p->nSpace = nNew;
169078 p->aData = (char *)&p[1];
169079 }
169080
169081 /* Append the new serialized varint to the end of the list. */
169082 p->nData += sqlite3Fts3PutVarint(&p->aData[p->nData], i);
169083 p->aData[p->nData] = '\0';
169084 *pp = p;
169085 return SQLITE_OK;
169086}
169087
169088/*
169089** Add a docid/column/position entry to a PendingList structure. Non-zero
169090** is returned if the structure is sqlite3_realloced as part of adding
169091** the entry. Otherwise, zero.
169092**
169093** If an OOM error occurs, *pRc is set to SQLITE_NOMEM before returning.
169094** Zero is always returned in this case. Otherwise, if no OOM error occurs,
169095** it is set to SQLITE_OK.
169096*/
169097static int fts3PendingListAppend(
169098 PendingList **pp, /* IN/OUT: PendingList structure */
169099 sqlite3_int64 iDocid, /* Docid for entry to add */
169100 sqlite3_int64 iCol, /* Column for entry to add */
169101 sqlite3_int64 iPos, /* Position of term for entry to add */
169102 int *pRc /* OUT: Return code */
169103){
169104 PendingList *p = *pp;
169105 int rc = SQLITE_OK;
169106
169107 assert( !p || p->iLastDocid<=iDocid );
169108
169109 if( !p || p->iLastDocid!=iDocid ){
169110 sqlite3_int64 iDelta = iDocid - (p ? p->iLastDocid : 0);
169111 if( p ){
169112 assert( p->nData<p->nSpace );
169113 assert( p->aData[p->nData]==0 );
169114 p->nData++;
169115 }
169116 if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iDelta)) ){
169117 goto pendinglistappend_out;
169118 }
169119 p->iLastCol = -1;
169120 p->iLastPos = 0;
169121 p->iLastDocid = iDocid;
169122 }
169123 if( iCol>0 && p->iLastCol!=iCol ){
169124 if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, 1))
169125 || SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iCol))
169126 ){
169127 goto pendinglistappend_out;
169128 }
169129 p->iLastCol = iCol;
169130 p->iLastPos = 0;
169131 }
169132 if( iCol>=0 ){
169133 assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) );
169134 rc = fts3PendingListAppendVarint(&p, 2+iPos-p->iLastPos);
169135 if( rc==SQLITE_OK ){
169136 p->iLastPos = iPos;
169137 }
169138 }
169139
169140 pendinglistappend_out:
169141 *pRc = rc;
169142 if( p!=*pp ){
169143 *pp = p;
169144 return 1;
169145 }
169146 return 0;
169147}
169148
169149/*
169150** Free a PendingList object allocated by fts3PendingListAppend().
169151*/
169152static void fts3PendingListDelete(PendingList *pList){
169153 sqlite3_free(pList);
169154}
169155
169156/*
169157** Add an entry to one of the pending-terms hash tables.
169158*/
169159static int fts3PendingTermsAddOne(
169160 Fts3Table *p,
169161 int iCol,
169162 int iPos,
169163 Fts3Hash *pHash, /* Pending terms hash table to add entry to */
169164 const char *zToken,
169165 int nToken
169166){
169167 PendingList *pList;
169168 int rc = SQLITE_OK;
169169
169170 pList = (PendingList *)fts3HashFind(pHash, zToken, nToken);
169171 if( pList ){
169172 p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem));
169173 }
169174 if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){
169175 if( pList==fts3HashInsert(pHash, zToken, nToken, pList) ){
169176 /* Malloc failed while inserting the new entry. This can only
169177 ** happen if there was no previous entry for this token.
169178 */
169179 assert( 0==fts3HashFind(pHash, zToken, nToken) );
169180 sqlite3_free(pList);
169181 rc = SQLITE_NOMEM;
169182 }
169183 }
169184 if( rc==SQLITE_OK ){
169185 p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem));
169186 }
169187 return rc;
169188}
169189
169190/*
169191** Tokenize the nul-terminated string zText and add all tokens to the
169192** pending-terms hash-table. The docid used is that currently stored in
169193** p->iPrevDocid, and the column is specified by argument iCol.
169194**
169195** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
169196*/
169197static int fts3PendingTermsAdd(
169198 Fts3Table *p, /* Table into which text will be inserted */
169199 int iLangid, /* Language id to use */
169200 const char *zText, /* Text of document to be inserted */
169201 int iCol, /* Column into which text is being inserted */
169202 u32 *pnWord /* IN/OUT: Incr. by number tokens inserted */
169203){
169204 int rc;
169205 int iStart = 0;
169206 int iEnd = 0;
169207 int iPos = 0;
169208 int nWord = 0;
169209
169210 char const *zToken;
169211 int nToken = 0;
169212
169213 sqlite3_tokenizer *pTokenizer = p->pTokenizer;
169214 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
169215 sqlite3_tokenizer_cursor *pCsr;
169216 int (*xNext)(sqlite3_tokenizer_cursor *pCursor,
169217 const char**,int*,int*,int*,int*);
169218
169219 assert( pTokenizer && pModule );
169220
169221 /* If the user has inserted a NULL value, this function may be called with
169222 ** zText==0. In this case, add zero token entries to the hash table and
169223 ** return early. */
169224 if( zText==0 ){
169225 *pnWord = 0;
169226 return SQLITE_OK;
169227 }
169228
169229 rc = sqlite3Fts3OpenTokenizer(pTokenizer, iLangid, zText, -1, &pCsr);
169230 if( rc!=SQLITE_OK ){
169231 return rc;
169232 }
169233
169234 xNext = pModule->xNext;
169235 while( SQLITE_OK==rc
169236 && SQLITE_OK==(rc = xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos))
169237 ){
169238 int i;
169239 if( iPos>=nWord ) nWord = iPos+1;
169240
169241 /* Positions cannot be negative; we use -1 as a terminator internally.
169242 ** Tokens must have a non-zero length.
169243 */
169244 if( iPos<0 || !zToken || nToken<=0 ){
169245 rc = SQLITE_ERROR;
169246 break;
169247 }
169248
169249 /* Add the term to the terms index */
169250 rc = fts3PendingTermsAddOne(
169251 p, iCol, iPos, &p->aIndex[0].hPending, zToken, nToken
169252 );
169253
169254 /* Add the term to each of the prefix indexes that it is not too
169255 ** short for. */
169256 for(i=1; rc==SQLITE_OK && i<p->nIndex; i++){
169257 struct Fts3Index *pIndex = &p->aIndex[i];
169258 if( nToken<pIndex->nPrefix ) continue;
169259 rc = fts3PendingTermsAddOne(
169260 p, iCol, iPos, &pIndex->hPending, zToken, pIndex->nPrefix
169261 );
169262 }
169263 }
169264
169265 pModule->xClose(pCsr);
169266 *pnWord += nWord;
169267 return (rc==SQLITE_DONE ? SQLITE_OK : rc);
169268}
169269
169270/*
169271** Calling this function indicates that subsequent calls to
169272** fts3PendingTermsAdd() are to add term/position-list pairs for the
169273** contents of the document with docid iDocid.
169274*/
169275static int fts3PendingTermsDocid(
169276 Fts3Table *p, /* Full-text table handle */
169277 int bDelete, /* True if this op is a delete */
169278 int iLangid, /* Language id of row being written */
169279 sqlite_int64 iDocid /* Docid of row being written */
169280){
169281 assert( iLangid>=0 );
169282 assert( bDelete==1 || bDelete==0 );
169283
169284 /* TODO(shess) Explore whether partially flushing the buffer on
169285 ** forced-flush would provide better performance. I suspect that if
169286 ** we ordered the doclists by size and flushed the largest until the
169287 ** buffer was half empty, that would let the less frequent terms
169288 ** generate longer doclists.
169289 */
169290 if( iDocid<p->iPrevDocid
169291 || (iDocid==p->iPrevDocid && p->bPrevDelete==0)
169292 || p->iPrevLangid!=iLangid
169293 || p->nPendingData>p->nMaxPendingData
169294 ){
169295 int rc = sqlite3Fts3PendingTermsFlush(p);
169296 if( rc!=SQLITE_OK ) return rc;
169297 }
169298 p->iPrevDocid = iDocid;
169299 p->iPrevLangid = iLangid;
169300 p->bPrevDelete = bDelete;
169301 return SQLITE_OK;
169302}
169303
169304/*
169305** Discard the contents of the pending-terms hash tables.
169306*/
169307SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *p){
169308 int i;
169309 for(i=0; i<p->nIndex; i++){
169310 Fts3HashElem *pElem;
169311 Fts3Hash *pHash = &p->aIndex[i].hPending;
169312 for(pElem=fts3HashFirst(pHash); pElem; pElem=fts3HashNext(pElem)){
169313 PendingList *pList = (PendingList *)fts3HashData(pElem);
169314 fts3PendingListDelete(pList);
169315 }
169316 fts3HashClear(pHash);
169317 }
169318 p->nPendingData = 0;
169319}
169320
169321/*
169322** This function is called by the xUpdate() method as part of an INSERT
169323** operation. It adds entries for each term in the new record to the
169324** pendingTerms hash table.
169325**
169326** Argument apVal is the same as the similarly named argument passed to
169327** fts3InsertData(). Parameter iDocid is the docid of the new row.
169328*/
169329static int fts3InsertTerms(
169330 Fts3Table *p,
169331 int iLangid,
169332 sqlite3_value **apVal,
169333 u32 *aSz
169334){
169335 int i; /* Iterator variable */
169336 for(i=2; i<p->nColumn+2; i++){
169337 int iCol = i-2;
169338 if( p->abNotindexed[iCol]==0 ){
169339 const char *zText = (const char *)sqlite3_value_text(apVal[i]);
169340 int rc = fts3PendingTermsAdd(p, iLangid, zText, iCol, &aSz[iCol]);
169341 if( rc!=SQLITE_OK ){
169342 return rc;
169343 }
169344 aSz[p->nColumn] += sqlite3_value_bytes(apVal[i]);
169345 }
169346 }
169347 return SQLITE_OK;
169348}
169349
169350/*
169351** This function is called by the xUpdate() method for an INSERT operation.
169352** The apVal parameter is passed a copy of the apVal argument passed by
169353** SQLite to the xUpdate() method. i.e:
169354**
169355** apVal[0] Not used for INSERT.
169356** apVal[1] rowid
169357** apVal[2] Left-most user-defined column
169358** ...
169359** apVal[p->nColumn+1] Right-most user-defined column
169360** apVal[p->nColumn+2] Hidden column with same name as table
169361** apVal[p->nColumn+3] Hidden "docid" column (alias for rowid)
169362** apVal[p->nColumn+4] Hidden languageid column
169363*/
169364static int fts3InsertData(
169365 Fts3Table *p, /* Full-text table */
169366 sqlite3_value **apVal, /* Array of values to insert */
169367 sqlite3_int64 *piDocid /* OUT: Docid for row just inserted */
169368){
169369 int rc; /* Return code */
169370 sqlite3_stmt *pContentInsert; /* INSERT INTO %_content VALUES(...) */
169371
169372 if( p->zContentTbl ){
169373 sqlite3_value *pRowid = apVal[p->nColumn+3];
169374 if( sqlite3_value_type(pRowid)==SQLITE_NULL ){
169375 pRowid = apVal[1];
169376 }
169377 if( sqlite3_value_type(pRowid)!=SQLITE_INTEGER ){
169378 return SQLITE_CONSTRAINT;
169379 }
169380 *piDocid = sqlite3_value_int64(pRowid);
169381 return SQLITE_OK;
169382 }
169383
169384 /* Locate the statement handle used to insert data into the %_content
169385 ** table. The SQL for this statement is:
169386 **
169387 ** INSERT INTO %_content VALUES(?, ?, ?, ...)
169388 **
169389 ** The statement features N '?' variables, where N is the number of user
169390 ** defined columns in the FTS3 table, plus one for the docid field.
169391 */
169392 rc = fts3SqlStmt(p, SQL_CONTENT_INSERT, &pContentInsert, &apVal[1]);
169393 if( rc==SQLITE_OK && p->zLanguageid ){
169394 rc = sqlite3_bind_int(
169395 pContentInsert, p->nColumn+2,
169396 sqlite3_value_int(apVal[p->nColumn+4])
169397 );
169398 }
169399 if( rc!=SQLITE_OK ) return rc;
169400
169401 /* There is a quirk here. The users INSERT statement may have specified
169402 ** a value for the "rowid" field, for the "docid" field, or for both.
169403 ** Which is a problem, since "rowid" and "docid" are aliases for the
169404 ** same value. For example:
169405 **
169406 ** INSERT INTO fts3tbl(rowid, docid) VALUES(1, 2);
169407 **
169408 ** In FTS3, this is an error. It is an error to specify non-NULL values
169409 ** for both docid and some other rowid alias.
169410 */
169411 if( SQLITE_NULL!=sqlite3_value_type(apVal[3+p->nColumn]) ){
169412 if( SQLITE_NULL==sqlite3_value_type(apVal[0])
169413 && SQLITE_NULL!=sqlite3_value_type(apVal[1])
169414 ){
169415 /* A rowid/docid conflict. */
169416 return SQLITE_ERROR;
169417 }
169418 rc = sqlite3_bind_value(pContentInsert, 1, apVal[3+p->nColumn]);
169419 if( rc!=SQLITE_OK ) return rc;
169420 }
169421
169422 /* Execute the statement to insert the record. Set *piDocid to the
169423 ** new docid value.
169424 */
169425 sqlite3_step(pContentInsert);
169426 rc = sqlite3_reset(pContentInsert);
169427
169428 *piDocid = sqlite3_last_insert_rowid(p->db);
169429 return rc;
169430}
169431
169432
169433
169434/*
169435** Remove all data from the FTS3 table. Clear the hash table containing
169436** pending terms.
169437*/
169438static int fts3DeleteAll(Fts3Table *p, int bContent){
169439 int rc = SQLITE_OK; /* Return code */
169440
169441 /* Discard the contents of the pending-terms hash table. */
169442 sqlite3Fts3PendingTermsClear(p);
169443
169444 /* Delete everything from the shadow tables. Except, leave %_content as
169445 ** is if bContent is false. */
169446 assert( p->zContentTbl==0 || bContent==0 );
169447 if( bContent ) fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT, 0);
169448 fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGMENTS, 0);
169449 fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0);
169450 if( p->bHasDocsize ){
169451 fts3SqlExec(&rc, p, SQL_DELETE_ALL_DOCSIZE, 0);
169452 }
169453 if( p->bHasStat ){
169454 fts3SqlExec(&rc, p, SQL_DELETE_ALL_STAT, 0);
169455 }
169456 return rc;
169457}
169458
169459/*
169460**
169461*/
169462static int langidFromSelect(Fts3Table *p, sqlite3_stmt *pSelect){
169463 int iLangid = 0;
169464 if( p->zLanguageid ) iLangid = sqlite3_column_int(pSelect, p->nColumn+1);
169465 return iLangid;
169466}
169467
169468/*
169469** The first element in the apVal[] array is assumed to contain the docid
169470** (an integer) of a row about to be deleted. Remove all terms from the
169471** full-text index.
169472*/
169473static void fts3DeleteTerms(
169474 int *pRC, /* Result code */
169475 Fts3Table *p, /* The FTS table to delete from */
169476 sqlite3_value *pRowid, /* The docid to be deleted */
169477 u32 *aSz, /* Sizes of deleted document written here */
169478 int *pbFound /* OUT: Set to true if row really does exist */
169479){
169480 int rc;
169481 sqlite3_stmt *pSelect;
169482
169483 assert( *pbFound==0 );
169484 if( *pRC ) return;
169485 rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pSelect, &pRowid);
169486 if( rc==SQLITE_OK ){
169487 if( SQLITE_ROW==sqlite3_step(pSelect) ){
169488 int i;
169489 int iLangid = langidFromSelect(p, pSelect);
169490 i64 iDocid = sqlite3_column_int64(pSelect, 0);
169491 rc = fts3PendingTermsDocid(p, 1, iLangid, iDocid);
169492 for(i=1; rc==SQLITE_OK && i<=p->nColumn; i++){
169493 int iCol = i-1;
169494 if( p->abNotindexed[iCol]==0 ){
169495 const char *zText = (const char *)sqlite3_column_text(pSelect, i);
169496 rc = fts3PendingTermsAdd(p, iLangid, zText, -1, &aSz[iCol]);
169497 aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i);
169498 }
169499 }
169500 if( rc!=SQLITE_OK ){
169501 sqlite3_reset(pSelect);
169502 *pRC = rc;
169503 return;
169504 }
169505 *pbFound = 1;
169506 }
169507 rc = sqlite3_reset(pSelect);
169508 }else{
169509 sqlite3_reset(pSelect);
169510 }
169511 *pRC = rc;
169512}
169513
169514/*
169515** Forward declaration to account for the circular dependency between
169516** functions fts3SegmentMerge() and fts3AllocateSegdirIdx().
169517*/
169518static int fts3SegmentMerge(Fts3Table *, int, int, int);
169519
169520/*
169521** This function allocates a new level iLevel index in the segdir table.
169522** Usually, indexes are allocated within a level sequentially starting
169523** with 0, so the allocated index is one greater than the value returned
169524** by:
169525**
169526** SELECT max(idx) FROM %_segdir WHERE level = :iLevel
169527**
169528** However, if there are already FTS3_MERGE_COUNT indexes at the requested
169529** level, they are merged into a single level (iLevel+1) segment and the
169530** allocated index is 0.
169531**
169532** If successful, *piIdx is set to the allocated index slot and SQLITE_OK
169533** returned. Otherwise, an SQLite error code is returned.
169534*/
169535static int fts3AllocateSegdirIdx(
169536 Fts3Table *p,
169537 int iLangid, /* Language id */
169538 int iIndex, /* Index for p->aIndex */
169539 int iLevel,
169540 int *piIdx
169541){
169542 int rc; /* Return Code */
169543 sqlite3_stmt *pNextIdx; /* Query for next idx at level iLevel */
169544 int iNext = 0; /* Result of query pNextIdx */
169545
169546 assert( iLangid>=0 );
169547 assert( p->nIndex>=1 );
169548
169549 /* Set variable iNext to the next available segdir index at level iLevel. */
169550 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pNextIdx, 0);
169551 if( rc==SQLITE_OK ){
169552 sqlite3_bind_int64(
169553 pNextIdx, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
169554 );
169555 if( SQLITE_ROW==sqlite3_step(pNextIdx) ){
169556 iNext = sqlite3_column_int(pNextIdx, 0);
169557 }
169558 rc = sqlite3_reset(pNextIdx);
169559 }
169560
169561 if( rc==SQLITE_OK ){
169562 /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already
169563 ** full, merge all segments in level iLevel into a single iLevel+1
169564 ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
169565 ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
169566 */
169567 if( iNext>=FTS3_MERGE_COUNT ){
169568 fts3LogMerge(16, getAbsoluteLevel(p, iLangid, iIndex, iLevel));
169569 rc = fts3SegmentMerge(p, iLangid, iIndex, iLevel);
169570 *piIdx = 0;
169571 }else{
169572 *piIdx = iNext;
169573 }
169574 }
169575
169576 return rc;
169577}
169578
169579/*
169580** The %_segments table is declared as follows:
169581**
169582** CREATE TABLE %_segments(blockid INTEGER PRIMARY KEY, block BLOB)
169583**
169584** This function reads data from a single row of the %_segments table. The
169585** specific row is identified by the iBlockid parameter. If paBlob is not
169586** NULL, then a buffer is allocated using sqlite3_malloc() and populated
169587** with the contents of the blob stored in the "block" column of the
169588** identified table row is. Whether or not paBlob is NULL, *pnBlob is set
169589** to the size of the blob in bytes before returning.
169590**
169591** If an error occurs, or the table does not contain the specified row,
169592** an SQLite error code is returned. Otherwise, SQLITE_OK is returned. If
169593** paBlob is non-NULL, then it is the responsibility of the caller to
169594** eventually free the returned buffer.
169595**
169596** This function may leave an open sqlite3_blob* handle in the
169597** Fts3Table.pSegments variable. This handle is reused by subsequent calls
169598** to this function. The handle may be closed by calling the
169599** sqlite3Fts3SegmentsClose() function. Reusing a blob handle is a handy
169600** performance improvement, but the blob handle should always be closed
169601** before control is returned to the user (to prevent a lock being held
169602** on the database file for longer than necessary). Thus, any virtual table
169603** method (xFilter etc.) that may directly or indirectly call this function
169604** must call sqlite3Fts3SegmentsClose() before returning.
169605*/
169606SQLITE_PRIVATE int sqlite3Fts3ReadBlock(
169607 Fts3Table *p, /* FTS3 table handle */
169608 sqlite3_int64 iBlockid, /* Access the row with blockid=$iBlockid */
169609 char **paBlob, /* OUT: Blob data in malloc'd buffer */
169610 int *pnBlob, /* OUT: Size of blob data */
169611 int *pnLoad /* OUT: Bytes actually loaded */
169612){
169613 int rc; /* Return code */
169614
169615 /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
169616 assert( pnBlob );
169617
169618 if( p->pSegments ){
169619 rc = sqlite3_blob_reopen(p->pSegments, iBlockid);
169620 }else{
169621 if( 0==p->zSegmentsTbl ){
169622 p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName);
169623 if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM;
169624 }
169625 rc = sqlite3_blob_open(
169626 p->db, p->zDb, p->zSegmentsTbl, "block", iBlockid, 0, &p->pSegments
169627 );
169628 }
169629
169630 if( rc==SQLITE_OK ){
169631 int nByte = sqlite3_blob_bytes(p->pSegments);
169632 *pnBlob = nByte;
169633 if( paBlob ){
169634 char *aByte = sqlite3_malloc(nByte + FTS3_NODE_PADDING);
169635 if( !aByte ){
169636 rc = SQLITE_NOMEM;
169637 }else{
169638 if( pnLoad && nByte>(FTS3_NODE_CHUNK_THRESHOLD) ){
169639 nByte = FTS3_NODE_CHUNKSIZE;
169640 *pnLoad = nByte;
169641 }
169642 rc = sqlite3_blob_read(p->pSegments, aByte, nByte, 0);
169643 memset(&aByte[nByte], 0, FTS3_NODE_PADDING);
169644 if( rc!=SQLITE_OK ){
169645 sqlite3_free(aByte);
169646 aByte = 0;
169647 }
169648 }
169649 *paBlob = aByte;
169650 }
169651 }
169652
169653 return rc;
169654}
169655
169656/*
169657** Close the blob handle at p->pSegments, if it is open. See comments above
169658** the sqlite3Fts3ReadBlock() function for details.
169659*/
169660SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *p){
169661 sqlite3_blob_close(p->pSegments);
169662 p->pSegments = 0;
169663}
169664
169665static int fts3SegReaderIncrRead(Fts3SegReader *pReader){
169666 int nRead; /* Number of bytes to read */
169667 int rc; /* Return code */
169668
169669 nRead = MIN(pReader->nNode - pReader->nPopulate, FTS3_NODE_CHUNKSIZE);
169670 rc = sqlite3_blob_read(
169671 pReader->pBlob,
169672 &pReader->aNode[pReader->nPopulate],
169673 nRead,
169674 pReader->nPopulate
169675 );
169676
169677 if( rc==SQLITE_OK ){
169678 pReader->nPopulate += nRead;
169679 memset(&pReader->aNode[pReader->nPopulate], 0, FTS3_NODE_PADDING);
169680 if( pReader->nPopulate==pReader->nNode ){
169681 sqlite3_blob_close(pReader->pBlob);
169682 pReader->pBlob = 0;
169683 pReader->nPopulate = 0;
169684 }
169685 }
169686 return rc;
169687}
169688
169689static int fts3SegReaderRequire(Fts3SegReader *pReader, char *pFrom, int nByte){
169690 int rc = SQLITE_OK;
169691 assert( !pReader->pBlob
169692 || (pFrom>=pReader->aNode && pFrom<&pReader->aNode[pReader->nNode])
169693 );
169694 while( pReader->pBlob && rc==SQLITE_OK
169695 && (pFrom - pReader->aNode + nByte)>pReader->nPopulate
169696 ){
169697 rc = fts3SegReaderIncrRead(pReader);
169698 }
169699 return rc;
169700}
169701
169702/*
169703** Set an Fts3SegReader cursor to point at EOF.
169704*/
169705static void fts3SegReaderSetEof(Fts3SegReader *pSeg){
169706 if( !fts3SegReaderIsRootOnly(pSeg) ){
169707 sqlite3_free(pSeg->aNode);
169708 sqlite3_blob_close(pSeg->pBlob);
169709 pSeg->pBlob = 0;
169710 }
169711 pSeg->aNode = 0;
169712}
169713
169714/*
169715** Move the iterator passed as the first argument to the next term in the
169716** segment. If successful, SQLITE_OK is returned. If there is no next term,
169717** SQLITE_DONE. Otherwise, an SQLite error code.
169718*/
169719static int fts3SegReaderNext(
169720 Fts3Table *p,
169721 Fts3SegReader *pReader,
169722 int bIncr
169723){
169724 int rc; /* Return code of various sub-routines */
169725 char *pNext; /* Cursor variable */
169726 int nPrefix; /* Number of bytes in term prefix */
169727 int nSuffix; /* Number of bytes in term suffix */
169728
169729 if( !pReader->aDoclist ){
169730 pNext = pReader->aNode;
169731 }else{
169732 pNext = &pReader->aDoclist[pReader->nDoclist];
169733 }
169734
169735 if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){
169736
169737 if( fts3SegReaderIsPending(pReader) ){
169738 Fts3HashElem *pElem = *(pReader->ppNextElem);
169739 sqlite3_free(pReader->aNode);
169740 pReader->aNode = 0;
169741 if( pElem ){
169742 char *aCopy;
169743 PendingList *pList = (PendingList *)fts3HashData(pElem);
169744 int nCopy = pList->nData+1;
169745 pReader->zTerm = (char *)fts3HashKey(pElem);
169746 pReader->nTerm = fts3HashKeysize(pElem);
169747 aCopy = (char*)sqlite3_malloc(nCopy);
169748 if( !aCopy ) return SQLITE_NOMEM;
169749 memcpy(aCopy, pList->aData, nCopy);
169750 pReader->nNode = pReader->nDoclist = nCopy;
169751 pReader->aNode = pReader->aDoclist = aCopy;
169752 pReader->ppNextElem++;
169753 assert( pReader->aNode );
169754 }
169755 return SQLITE_OK;
169756 }
169757
169758 fts3SegReaderSetEof(pReader);
169759
169760 /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf
169761 ** blocks have already been traversed. */
169762 assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock );
169763 if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
169764 return SQLITE_OK;
169765 }
169766
169767 rc = sqlite3Fts3ReadBlock(
169768 p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode,
169769 (bIncr ? &pReader->nPopulate : 0)
169770 );
169771 if( rc!=SQLITE_OK ) return rc;
169772 assert( pReader->pBlob==0 );
169773 if( bIncr && pReader->nPopulate<pReader->nNode ){
169774 pReader->pBlob = p->pSegments;
169775 p->pSegments = 0;
169776 }
169777 pNext = pReader->aNode;
169778 }
169779
169780 assert( !fts3SegReaderIsPending(pReader) );
169781
169782 rc = fts3SegReaderRequire(pReader, pNext, FTS3_VARINT_MAX*2);
169783 if( rc!=SQLITE_OK ) return rc;
169784
169785 /* Because of the FTS3_NODE_PADDING bytes of padding, the following is
169786 ** safe (no risk of overread) even if the node data is corrupted. */
169787 pNext += fts3GetVarint32(pNext, &nPrefix);
169788 pNext += fts3GetVarint32(pNext, &nSuffix);
169789 if( nSuffix<=0
169790 || (&pReader->aNode[pReader->nNode] - pNext)<nSuffix
169791 || nPrefix>pReader->nTermAlloc
169792 ){
169793 return FTS_CORRUPT_VTAB;
169794 }
169795
169796 /* Both nPrefix and nSuffix were read by fts3GetVarint32() and so are
169797 ** between 0 and 0x7FFFFFFF. But the sum of the two may cause integer
169798 ** overflow - hence the (i64) casts. */
169799 if( (i64)nPrefix+nSuffix>(i64)pReader->nTermAlloc ){
169800 i64 nNew = ((i64)nPrefix+nSuffix)*2;
169801 char *zNew = sqlite3_realloc64(pReader->zTerm, nNew);
169802 if( !zNew ){
169803 return SQLITE_NOMEM;
169804 }
169805 pReader->zTerm = zNew;
169806 pReader->nTermAlloc = nNew;
169807 }
169808
169809 rc = fts3SegReaderRequire(pReader, pNext, nSuffix+FTS3_VARINT_MAX);
169810 if( rc!=SQLITE_OK ) return rc;
169811
169812 memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix);
169813 pReader->nTerm = nPrefix+nSuffix;
169814 pNext += nSuffix;
169815 pNext += fts3GetVarint32(pNext, &pReader->nDoclist);
169816 pReader->aDoclist = pNext;
169817 pReader->pOffsetList = 0;
169818
169819 /* Check that the doclist does not appear to extend past the end of the
169820 ** b-tree node. And that the final byte of the doclist is 0x00. If either
169821 ** of these statements is untrue, then the data structure is corrupt.
169822 */
169823 if( (&pReader->aNode[pReader->nNode] - pReader->aDoclist)<pReader->nDoclist
169824 || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
169825 ){
169826 return FTS_CORRUPT_VTAB;
169827 }
169828 return SQLITE_OK;
169829}
169830
169831/*
169832** Set the SegReader to point to the first docid in the doclist associated
169833** with the current term.
169834*/
169835static int fts3SegReaderFirstDocid(Fts3Table *pTab, Fts3SegReader *pReader){
169836 int rc = SQLITE_OK;
169837 assert( pReader->aDoclist );
169838 assert( !pReader->pOffsetList );
169839 if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
169840 u8 bEof = 0;
169841 pReader->iDocid = 0;
169842 pReader->nOffsetList = 0;
169843 sqlite3Fts3DoclistPrev(0,
169844 pReader->aDoclist, pReader->nDoclist, &pReader->pOffsetList,
169845 &pReader->iDocid, &pReader->nOffsetList, &bEof
169846 );
169847 }else{
169848 rc = fts3SegReaderRequire(pReader, pReader->aDoclist, FTS3_VARINT_MAX);
169849 if( rc==SQLITE_OK ){
169850 int n = sqlite3Fts3GetVarint(pReader->aDoclist, &pReader->iDocid);
169851 pReader->pOffsetList = &pReader->aDoclist[n];
169852 }
169853 }
169854 return rc;
169855}
169856
169857/*
169858** Advance the SegReader to point to the next docid in the doclist
169859** associated with the current term.
169860**
169861** If arguments ppOffsetList and pnOffsetList are not NULL, then
169862** *ppOffsetList is set to point to the first column-offset list
169863** in the doclist entry (i.e. immediately past the docid varint).
169864** *pnOffsetList is set to the length of the set of column-offset
169865** lists, not including the nul-terminator byte. For example:
169866*/
169867static int fts3SegReaderNextDocid(
169868 Fts3Table *pTab,
169869 Fts3SegReader *pReader, /* Reader to advance to next docid */
169870 char **ppOffsetList, /* OUT: Pointer to current position-list */
169871 int *pnOffsetList /* OUT: Length of *ppOffsetList in bytes */
169872){
169873 int rc = SQLITE_OK;
169874 char *p = pReader->pOffsetList;
169875 char c = 0;
169876
169877 assert( p );
169878
169879 if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
169880 /* A pending-terms seg-reader for an FTS4 table that uses order=desc.
169881 ** Pending-terms doclists are always built up in ascending order, so
169882 ** we have to iterate through them backwards here. */
169883 u8 bEof = 0;
169884 if( ppOffsetList ){
169885 *ppOffsetList = pReader->pOffsetList;
169886 *pnOffsetList = pReader->nOffsetList - 1;
169887 }
169888 sqlite3Fts3DoclistPrev(0,
169889 pReader->aDoclist, pReader->nDoclist, &p, &pReader->iDocid,
169890 &pReader->nOffsetList, &bEof
169891 );
169892 if( bEof ){
169893 pReader->pOffsetList = 0;
169894 }else{
169895 pReader->pOffsetList = p;
169896 }
169897 }else{
169898 char *pEnd = &pReader->aDoclist[pReader->nDoclist];
169899
169900 /* Pointer p currently points at the first byte of an offset list. The
169901 ** following block advances it to point one byte past the end of
169902 ** the same offset list. */
169903 while( 1 ){
169904
169905 /* The following line of code (and the "p++" below the while() loop) is
169906 ** normally all that is required to move pointer p to the desired
169907 ** position. The exception is if this node is being loaded from disk
169908 ** incrementally and pointer "p" now points to the first byte past
169909 ** the populated part of pReader->aNode[].
169910 */
169911 while( *p | c ) c = *p++ & 0x80;
169912 assert( *p==0 );
169913
169914 if( pReader->pBlob==0 || p<&pReader->aNode[pReader->nPopulate] ) break;
169915 rc = fts3SegReaderIncrRead(pReader);
169916 if( rc!=SQLITE_OK ) return rc;
169917 }
169918 p++;
169919
169920 /* If required, populate the output variables with a pointer to and the
169921 ** size of the previous offset-list.
169922 */
169923 if( ppOffsetList ){
169924 *ppOffsetList = pReader->pOffsetList;
169925 *pnOffsetList = (int)(p - pReader->pOffsetList - 1);
169926 }
169927
169928 /* List may have been edited in place by fts3EvalNearTrim() */
169929 while( p<pEnd && *p==0 ) p++;
169930
169931 /* If there are no more entries in the doclist, set pOffsetList to
169932 ** NULL. Otherwise, set Fts3SegReader.iDocid to the next docid and
169933 ** Fts3SegReader.pOffsetList to point to the next offset list before
169934 ** returning.
169935 */
169936 if( p>=pEnd ){
169937 pReader->pOffsetList = 0;
169938 }else{
169939 rc = fts3SegReaderRequire(pReader, p, FTS3_VARINT_MAX);
169940 if( rc==SQLITE_OK ){
169941 sqlite3_int64 iDelta;
169942 pReader->pOffsetList = p + sqlite3Fts3GetVarint(p, &iDelta);
169943 if( pTab->bDescIdx ){
169944 pReader->iDocid -= iDelta;
169945 }else{
169946 pReader->iDocid += iDelta;
169947 }
169948 }
169949 }
169950 }
169951
169952 return SQLITE_OK;
169953}
169954
169955
169956SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(
169957 Fts3Cursor *pCsr,
169958 Fts3MultiSegReader *pMsr,
169959 int *pnOvfl
169960){
169961 Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
169962 int nOvfl = 0;
169963 int ii;
169964 int rc = SQLITE_OK;
169965 int pgsz = p->nPgsz;
169966
169967 assert( p->bFts4 );
169968 assert( pgsz>0 );
169969
169970 for(ii=0; rc==SQLITE_OK && ii<pMsr->nSegment; ii++){
169971 Fts3SegReader *pReader = pMsr->apSegment[ii];
169972 if( !fts3SegReaderIsPending(pReader)
169973 && !fts3SegReaderIsRootOnly(pReader)
169974 ){
169975 sqlite3_int64 jj;
169976 for(jj=pReader->iStartBlock; jj<=pReader->iLeafEndBlock; jj++){
169977 int nBlob;
169978 rc = sqlite3Fts3ReadBlock(p, jj, 0, &nBlob, 0);
169979 if( rc!=SQLITE_OK ) break;
169980 if( (nBlob+35)>pgsz ){
169981 nOvfl += (nBlob + 34)/pgsz;
169982 }
169983 }
169984 }
169985 }
169986 *pnOvfl = nOvfl;
169987 return rc;
169988}
169989
169990/*
169991** Free all allocations associated with the iterator passed as the
169992** second argument.
169993*/
169994SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){
169995 if( pReader ){
169996 if( !fts3SegReaderIsPending(pReader) ){
169997 sqlite3_free(pReader->zTerm);
169998 }
169999 if( !fts3SegReaderIsRootOnly(pReader) ){
170000 sqlite3_free(pReader->aNode);
170001 }
170002 sqlite3_blob_close(pReader->pBlob);
170003 }
170004 sqlite3_free(pReader);
170005}
170006
170007/*
170008** Allocate a new SegReader object.
170009*/
170010SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(
170011 int iAge, /* Segment "age". */
170012 int bLookup, /* True for a lookup only */
170013 sqlite3_int64 iStartLeaf, /* First leaf to traverse */
170014 sqlite3_int64 iEndLeaf, /* Final leaf to traverse */
170015 sqlite3_int64 iEndBlock, /* Final block of segment */
170016 const char *zRoot, /* Buffer containing root node */
170017 int nRoot, /* Size of buffer containing root node */
170018 Fts3SegReader **ppReader /* OUT: Allocated Fts3SegReader */
170019){
170020 Fts3SegReader *pReader; /* Newly allocated SegReader object */
170021 int nExtra = 0; /* Bytes to allocate segment root node */
170022
170023 assert( iStartLeaf<=iEndLeaf );
170024 if( iStartLeaf==0 ){
170025 nExtra = nRoot + FTS3_NODE_PADDING;
170026 }
170027
170028 pReader = (Fts3SegReader *)sqlite3_malloc(sizeof(Fts3SegReader) + nExtra);
170029 if( !pReader ){
170030 return SQLITE_NOMEM;
170031 }
170032 memset(pReader, 0, sizeof(Fts3SegReader));
170033 pReader->iIdx = iAge;
170034 pReader->bLookup = bLookup!=0;
170035 pReader->iStartBlock = iStartLeaf;
170036 pReader->iLeafEndBlock = iEndLeaf;
170037 pReader->iEndBlock = iEndBlock;
170038
170039 if( nExtra ){
170040 /* The entire segment is stored in the root node. */
170041 pReader->aNode = (char *)&pReader[1];
170042 pReader->rootOnly = 1;
170043 pReader->nNode = nRoot;
170044 memcpy(pReader->aNode, zRoot, nRoot);
170045 memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
170046 }else{
170047 pReader->iCurrentBlock = iStartLeaf-1;
170048 }
170049 *ppReader = pReader;
170050 return SQLITE_OK;
170051}
170052
170053/*
170054** This is a comparison function used as a qsort() callback when sorting
170055** an array of pending terms by term. This occurs as part of flushing
170056** the contents of the pending-terms hash table to the database.
170057*/
170058static int SQLITE_CDECL fts3CompareElemByTerm(
170059 const void *lhs,
170060 const void *rhs
170061){
170062 char *z1 = fts3HashKey(*(Fts3HashElem **)lhs);
170063 char *z2 = fts3HashKey(*(Fts3HashElem **)rhs);
170064 int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs);
170065 int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs);
170066
170067 int n = (n1<n2 ? n1 : n2);
170068 int c = memcmp(z1, z2, n);
170069 if( c==0 ){
170070 c = n1 - n2;
170071 }
170072 return c;
170073}
170074
170075/*
170076** This function is used to allocate an Fts3SegReader that iterates through
170077** a subset of the terms stored in the Fts3Table.pendingTerms array.
170078**
170079** If the isPrefixIter parameter is zero, then the returned SegReader iterates
170080** through each term in the pending-terms table. Or, if isPrefixIter is
170081** non-zero, it iterates through each term and its prefixes. For example, if
170082** the pending terms hash table contains the terms "sqlite", "mysql" and
170083** "firebird", then the iterator visits the following 'terms' (in the order
170084** shown):
170085**
170086** f fi fir fire fireb firebi firebir firebird
170087** m my mys mysq mysql
170088** s sq sql sqli sqlit sqlite
170089**
170090** Whereas if isPrefixIter is zero, the terms visited are:
170091**
170092** firebird mysql sqlite
170093*/
170094SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
170095 Fts3Table *p, /* Virtual table handle */
170096 int iIndex, /* Index for p->aIndex */
170097 const char *zTerm, /* Term to search for */
170098 int nTerm, /* Size of buffer zTerm */
170099 int bPrefix, /* True for a prefix iterator */
170100 Fts3SegReader **ppReader /* OUT: SegReader for pending-terms */
170101){
170102 Fts3SegReader *pReader = 0; /* Fts3SegReader object to return */
170103 Fts3HashElem *pE; /* Iterator variable */
170104 Fts3HashElem **aElem = 0; /* Array of term hash entries to scan */
170105 int nElem = 0; /* Size of array at aElem */
170106 int rc = SQLITE_OK; /* Return Code */
170107 Fts3Hash *pHash;
170108
170109 pHash = &p->aIndex[iIndex].hPending;
170110 if( bPrefix ){
170111 int nAlloc = 0; /* Size of allocated array at aElem */
170112
170113 for(pE=fts3HashFirst(pHash); pE; pE=fts3HashNext(pE)){
170114 char *zKey = (char *)fts3HashKey(pE);
170115 int nKey = fts3HashKeysize(pE);
170116 if( nTerm==0 || (nKey>=nTerm && 0==memcmp(zKey, zTerm, nTerm)) ){
170117 if( nElem==nAlloc ){
170118 Fts3HashElem **aElem2;
170119 nAlloc += 16;
170120 aElem2 = (Fts3HashElem **)sqlite3_realloc(
170121 aElem, nAlloc*sizeof(Fts3HashElem *)
170122 );
170123 if( !aElem2 ){
170124 rc = SQLITE_NOMEM;
170125 nElem = 0;
170126 break;
170127 }
170128 aElem = aElem2;
170129 }
170130
170131 aElem[nElem++] = pE;
170132 }
170133 }
170134
170135 /* If more than one term matches the prefix, sort the Fts3HashElem
170136 ** objects in term order using qsort(). This uses the same comparison
170137 ** callback as is used when flushing terms to disk.
170138 */
170139 if( nElem>1 ){
170140 qsort(aElem, nElem, sizeof(Fts3HashElem *), fts3CompareElemByTerm);
170141 }
170142
170143 }else{
170144 /* The query is a simple term lookup that matches at most one term in
170145 ** the index. All that is required is a straight hash-lookup.
170146 **
170147 ** Because the stack address of pE may be accessed via the aElem pointer
170148 ** below, the "Fts3HashElem *pE" must be declared so that it is valid
170149 ** within this entire function, not just this "else{...}" block.
170150 */
170151 pE = fts3HashFindElem(pHash, zTerm, nTerm);
170152 if( pE ){
170153 aElem = &pE;
170154 nElem = 1;
170155 }
170156 }
170157
170158 if( nElem>0 ){
170159 int nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *);
170160 pReader = (Fts3SegReader *)sqlite3_malloc(nByte);
170161 if( !pReader ){
170162 rc = SQLITE_NOMEM;
170163 }else{
170164 memset(pReader, 0, nByte);
170165 pReader->iIdx = 0x7FFFFFFF;
170166 pReader->ppNextElem = (Fts3HashElem **)&pReader[1];
170167 memcpy(pReader->ppNextElem, aElem, nElem*sizeof(Fts3HashElem *));
170168 }
170169 }
170170
170171 if( bPrefix ){
170172 sqlite3_free(aElem);
170173 }
170174 *ppReader = pReader;
170175 return rc;
170176}
170177
170178/*
170179** Compare the entries pointed to by two Fts3SegReader structures.
170180** Comparison is as follows:
170181**
170182** 1) EOF is greater than not EOF.
170183**
170184** 2) The current terms (if any) are compared using memcmp(). If one
170185** term is a prefix of another, the longer term is considered the
170186** larger.
170187**
170188** 3) By segment age. An older segment is considered larger.
170189*/
170190static int fts3SegReaderCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
170191 int rc;
170192 if( pLhs->aNode && pRhs->aNode ){
170193 int rc2 = pLhs->nTerm - pRhs->nTerm;
170194 if( rc2<0 ){
170195 rc = memcmp(pLhs->zTerm, pRhs->zTerm, pLhs->nTerm);
170196 }else{
170197 rc = memcmp(pLhs->zTerm, pRhs->zTerm, pRhs->nTerm);
170198 }
170199 if( rc==0 ){
170200 rc = rc2;
170201 }
170202 }else{
170203 rc = (pLhs->aNode==0) - (pRhs->aNode==0);
170204 }
170205 if( rc==0 ){
170206 rc = pRhs->iIdx - pLhs->iIdx;
170207 }
170208 assert( rc!=0 );
170209 return rc;
170210}
170211
170212/*
170213** A different comparison function for SegReader structures. In this
170214** version, it is assumed that each SegReader points to an entry in
170215** a doclist for identical terms. Comparison is made as follows:
170216**
170217** 1) EOF (end of doclist in this case) is greater than not EOF.
170218**
170219** 2) By current docid.
170220**
170221** 3) By segment age. An older segment is considered larger.
170222*/
170223static int fts3SegReaderDoclistCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
170224 int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
170225 if( rc==0 ){
170226 if( pLhs->iDocid==pRhs->iDocid ){
170227 rc = pRhs->iIdx - pLhs->iIdx;
170228 }else{
170229 rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1;
170230 }
170231 }
170232 assert( pLhs->aNode && pRhs->aNode );
170233 return rc;
170234}
170235static int fts3SegReaderDoclistCmpRev(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
170236 int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
170237 if( rc==0 ){
170238 if( pLhs->iDocid==pRhs->iDocid ){
170239 rc = pRhs->iIdx - pLhs->iIdx;
170240 }else{
170241 rc = (pLhs->iDocid < pRhs->iDocid) ? 1 : -1;
170242 }
170243 }
170244 assert( pLhs->aNode && pRhs->aNode );
170245 return rc;
170246}
170247
170248/*
170249** Compare the term that the Fts3SegReader object passed as the first argument
170250** points to with the term specified by arguments zTerm and nTerm.
170251**
170252** If the pSeg iterator is already at EOF, return 0. Otherwise, return
170253** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are
170254** equal, or +ve if the pSeg term is greater than zTerm/nTerm.
170255*/
170256static int fts3SegReaderTermCmp(
170257 Fts3SegReader *pSeg, /* Segment reader object */
170258 const char *zTerm, /* Term to compare to */
170259 int nTerm /* Size of term zTerm in bytes */
170260){
170261 int res = 0;
170262 if( pSeg->aNode ){
170263 if( pSeg->nTerm>nTerm ){
170264 res = memcmp(pSeg->zTerm, zTerm, nTerm);
170265 }else{
170266 res = memcmp(pSeg->zTerm, zTerm, pSeg->nTerm);
170267 }
170268 if( res==0 ){
170269 res = pSeg->nTerm-nTerm;
170270 }
170271 }
170272 return res;
170273}
170274
170275/*
170276** Argument apSegment is an array of nSegment elements. It is known that
170277** the final (nSegment-nSuspect) members are already in sorted order
170278** (according to the comparison function provided). This function shuffles
170279** the array around until all entries are in sorted order.
170280*/
170281static void fts3SegReaderSort(
170282 Fts3SegReader **apSegment, /* Array to sort entries of */
170283 int nSegment, /* Size of apSegment array */
170284 int nSuspect, /* Unsorted entry count */
170285 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) /* Comparison function */
170286){
170287 int i; /* Iterator variable */
170288
170289 assert( nSuspect<=nSegment );
170290
170291 if( nSuspect==nSegment ) nSuspect--;
170292 for(i=nSuspect-1; i>=0; i--){
170293 int j;
170294 for(j=i; j<(nSegment-1); j++){
170295 Fts3SegReader *pTmp;
170296 if( xCmp(apSegment[j], apSegment[j+1])<0 ) break;
170297 pTmp = apSegment[j+1];
170298 apSegment[j+1] = apSegment[j];
170299 apSegment[j] = pTmp;
170300 }
170301 }
170302
170303#ifndef NDEBUG
170304 /* Check that the list really is sorted now. */
170305 for(i=0; i<(nSuspect-1); i++){
170306 assert( xCmp(apSegment[i], apSegment[i+1])<0 );
170307 }
170308#endif
170309}
170310
170311/*
170312** Insert a record into the %_segments table.
170313*/
170314static int fts3WriteSegment(
170315 Fts3Table *p, /* Virtual table handle */
170316 sqlite3_int64 iBlock, /* Block id for new block */
170317 char *z, /* Pointer to buffer containing block data */
170318 int n /* Size of buffer z in bytes */
170319){
170320 sqlite3_stmt *pStmt;
170321 int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS, &pStmt, 0);
170322 if( rc==SQLITE_OK ){
170323 sqlite3_bind_int64(pStmt, 1, iBlock);
170324 sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
170325 sqlite3_step(pStmt);
170326 rc = sqlite3_reset(pStmt);
170327 sqlite3_bind_null(pStmt, 2);
170328 }
170329 return rc;
170330}
170331
170332/*
170333** Find the largest relative level number in the table. If successful, set
170334** *pnMax to this value and return SQLITE_OK. Otherwise, if an error occurs,
170335** set *pnMax to zero and return an SQLite error code.
170336*/
170337SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *p, int *pnMax){
170338 int rc;
170339 int mxLevel = 0;
170340 sqlite3_stmt *pStmt = 0;
170341
170342 rc = fts3SqlStmt(p, SQL_SELECT_MXLEVEL, &pStmt, 0);
170343 if( rc==SQLITE_OK ){
170344 if( SQLITE_ROW==sqlite3_step(pStmt) ){
170345 mxLevel = sqlite3_column_int(pStmt, 0);
170346 }
170347 rc = sqlite3_reset(pStmt);
170348 }
170349 *pnMax = mxLevel;
170350 return rc;
170351}
170352
170353/*
170354** Insert a record into the %_segdir table.
170355*/
170356static int fts3WriteSegdir(
170357 Fts3Table *p, /* Virtual table handle */
170358 sqlite3_int64 iLevel, /* Value for "level" field (absolute level) */
170359 int iIdx, /* Value for "idx" field */
170360 sqlite3_int64 iStartBlock, /* Value for "start_block" field */
170361 sqlite3_int64 iLeafEndBlock, /* Value for "leaves_end_block" field */
170362 sqlite3_int64 iEndBlock, /* Value for "end_block" field */
170363 sqlite3_int64 nLeafData, /* Bytes of leaf data in segment */
170364 char *zRoot, /* Blob value for "root" field */
170365 int nRoot /* Number of bytes in buffer zRoot */
170366){
170367 sqlite3_stmt *pStmt;
170368 int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR, &pStmt, 0);
170369 if( rc==SQLITE_OK ){
170370 sqlite3_bind_int64(pStmt, 1, iLevel);
170371 sqlite3_bind_int(pStmt, 2, iIdx);
170372 sqlite3_bind_int64(pStmt, 3, iStartBlock);
170373 sqlite3_bind_int64(pStmt, 4, iLeafEndBlock);
170374 if( nLeafData==0 ){
170375 sqlite3_bind_int64(pStmt, 5, iEndBlock);
170376 }else{
170377 char *zEnd = sqlite3_mprintf("%lld %lld", iEndBlock, nLeafData);
170378 if( !zEnd ) return SQLITE_NOMEM;
170379 sqlite3_bind_text(pStmt, 5, zEnd, -1, sqlite3_free);
170380 }
170381 sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
170382 sqlite3_step(pStmt);
170383 rc = sqlite3_reset(pStmt);
170384 sqlite3_bind_null(pStmt, 6);
170385 }
170386 return rc;
170387}
170388
170389/*
170390** Return the size of the common prefix (if any) shared by zPrev and
170391** zNext, in bytes. For example,
170392**
170393** fts3PrefixCompress("abc", 3, "abcdef", 6) // returns 3
170394** fts3PrefixCompress("abX", 3, "abcdef", 6) // returns 2
170395** fts3PrefixCompress("abX", 3, "Xbcdef", 6) // returns 0
170396*/
170397static int fts3PrefixCompress(
170398 const char *zPrev, /* Buffer containing previous term */
170399 int nPrev, /* Size of buffer zPrev in bytes */
170400 const char *zNext, /* Buffer containing next term */
170401 int nNext /* Size of buffer zNext in bytes */
170402){
170403 int n;
170404 UNUSED_PARAMETER(nNext);
170405 for(n=0; n<nPrev && zPrev[n]==zNext[n]; n++);
170406 return n;
170407}
170408
170409/*
170410** Add term zTerm to the SegmentNode. It is guaranteed that zTerm is larger
170411** (according to memcmp) than the previous term.
170412*/
170413static int fts3NodeAddTerm(
170414 Fts3Table *p, /* Virtual table handle */
170415 SegmentNode **ppTree, /* IN/OUT: SegmentNode handle */
170416 int isCopyTerm, /* True if zTerm/nTerm is transient */
170417 const char *zTerm, /* Pointer to buffer containing term */
170418 int nTerm /* Size of term in bytes */
170419){
170420 SegmentNode *pTree = *ppTree;
170421 int rc;
170422 SegmentNode *pNew;
170423
170424 /* First try to append the term to the current node. Return early if
170425 ** this is possible.
170426 */
170427 if( pTree ){
170428 int nData = pTree->nData; /* Current size of node in bytes */
170429 int nReq = nData; /* Required space after adding zTerm */
170430 int nPrefix; /* Number of bytes of prefix compression */
170431 int nSuffix; /* Suffix length */
170432
170433 nPrefix = fts3PrefixCompress(pTree->zTerm, pTree->nTerm, zTerm, nTerm);
170434 nSuffix = nTerm-nPrefix;
170435
170436 nReq += sqlite3Fts3VarintLen(nPrefix)+sqlite3Fts3VarintLen(nSuffix)+nSuffix;
170437 if( nReq<=p->nNodeSize || !pTree->zTerm ){
170438
170439 if( nReq>p->nNodeSize ){
170440 /* An unusual case: this is the first term to be added to the node
170441 ** and the static node buffer (p->nNodeSize bytes) is not large
170442 ** enough. Use a separately malloced buffer instead This wastes
170443 ** p->nNodeSize bytes, but since this scenario only comes about when
170444 ** the database contain two terms that share a prefix of almost 2KB,
170445 ** this is not expected to be a serious problem.
170446 */
170447 assert( pTree->aData==(char *)&pTree[1] );
170448 pTree->aData = (char *)sqlite3_malloc(nReq);
170449 if( !pTree->aData ){
170450 return SQLITE_NOMEM;
170451 }
170452 }
170453
170454 if( pTree->zTerm ){
170455 /* There is no prefix-length field for first term in a node */
170456 nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nPrefix);
170457 }
170458
170459 nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nSuffix);
170460 memcpy(&pTree->aData[nData], &zTerm[nPrefix], nSuffix);
170461 pTree->nData = nData + nSuffix;
170462 pTree->nEntry++;
170463
170464 if( isCopyTerm ){
170465 if( pTree->nMalloc<nTerm ){
170466 char *zNew = sqlite3_realloc(pTree->zMalloc, nTerm*2);
170467 if( !zNew ){
170468 return SQLITE_NOMEM;
170469 }
170470 pTree->nMalloc = nTerm*2;
170471 pTree->zMalloc = zNew;
170472 }
170473 pTree->zTerm = pTree->zMalloc;
170474 memcpy(pTree->zTerm, zTerm, nTerm);
170475 pTree->nTerm = nTerm;
170476 }else{
170477 pTree->zTerm = (char *)zTerm;
170478 pTree->nTerm = nTerm;
170479 }
170480 return SQLITE_OK;
170481 }
170482 }
170483
170484 /* If control flows to here, it was not possible to append zTerm to the
170485 ** current node. Create a new node (a right-sibling of the current node).
170486 ** If this is the first node in the tree, the term is added to it.
170487 **
170488 ** Otherwise, the term is not added to the new node, it is left empty for
170489 ** now. Instead, the term is inserted into the parent of pTree. If pTree
170490 ** has no parent, one is created here.
170491 */
170492 pNew = (SegmentNode *)sqlite3_malloc(sizeof(SegmentNode) + p->nNodeSize);
170493 if( !pNew ){
170494 return SQLITE_NOMEM;
170495 }
170496 memset(pNew, 0, sizeof(SegmentNode));
170497 pNew->nData = 1 + FTS3_VARINT_MAX;
170498 pNew->aData = (char *)&pNew[1];
170499
170500 if( pTree ){
170501 SegmentNode *pParent = pTree->pParent;
170502 rc = fts3NodeAddTerm(p, &pParent, isCopyTerm, zTerm, nTerm);
170503 if( pTree->pParent==0 ){
170504 pTree->pParent = pParent;
170505 }
170506 pTree->pRight = pNew;
170507 pNew->pLeftmost = pTree->pLeftmost;
170508 pNew->pParent = pParent;
170509 pNew->zMalloc = pTree->zMalloc;
170510 pNew->nMalloc = pTree->nMalloc;
170511 pTree->zMalloc = 0;
170512 }else{
170513 pNew->pLeftmost = pNew;
170514 rc = fts3NodeAddTerm(p, &pNew, isCopyTerm, zTerm, nTerm);
170515 }
170516
170517 *ppTree = pNew;
170518 return rc;
170519}
170520
170521/*
170522** Helper function for fts3NodeWrite().
170523*/
170524static int fts3TreeFinishNode(
170525 SegmentNode *pTree,
170526 int iHeight,
170527 sqlite3_int64 iLeftChild
170528){
170529 int nStart;
170530 assert( iHeight>=1 && iHeight<128 );
170531 nStart = FTS3_VARINT_MAX - sqlite3Fts3VarintLen(iLeftChild);
170532 pTree->aData[nStart] = (char)iHeight;
170533 sqlite3Fts3PutVarint(&pTree->aData[nStart+1], iLeftChild);
170534 return nStart;
170535}
170536
170537/*
170538** Write the buffer for the segment node pTree and all of its peers to the
170539** database. Then call this function recursively to write the parent of
170540** pTree and its peers to the database.
170541**
170542** Except, if pTree is a root node, do not write it to the database. Instead,
170543** set output variables *paRoot and *pnRoot to contain the root node.
170544**
170545** If successful, SQLITE_OK is returned and output variable *piLast is
170546** set to the largest blockid written to the database (or zero if no
170547** blocks were written to the db). Otherwise, an SQLite error code is
170548** returned.
170549*/
170550static int fts3NodeWrite(
170551 Fts3Table *p, /* Virtual table handle */
170552 SegmentNode *pTree, /* SegmentNode handle */
170553 int iHeight, /* Height of this node in tree */
170554 sqlite3_int64 iLeaf, /* Block id of first leaf node */
170555 sqlite3_int64 iFree, /* Block id of next free slot in %_segments */
170556 sqlite3_int64 *piLast, /* OUT: Block id of last entry written */
170557 char **paRoot, /* OUT: Data for root node */
170558 int *pnRoot /* OUT: Size of root node in bytes */
170559){
170560 int rc = SQLITE_OK;
170561
170562 if( !pTree->pParent ){
170563 /* Root node of the tree. */
170564 int nStart = fts3TreeFinishNode(pTree, iHeight, iLeaf);
170565 *piLast = iFree-1;
170566 *pnRoot = pTree->nData - nStart;
170567 *paRoot = &pTree->aData[nStart];
170568 }else{
170569 SegmentNode *pIter;
170570 sqlite3_int64 iNextFree = iFree;
170571 sqlite3_int64 iNextLeaf = iLeaf;
170572 for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK; pIter=pIter->pRight){
170573 int nStart = fts3TreeFinishNode(pIter, iHeight, iNextLeaf);
170574 int nWrite = pIter->nData - nStart;
170575
170576 rc = fts3WriteSegment(p, iNextFree, &pIter->aData[nStart], nWrite);
170577 iNextFree++;
170578 iNextLeaf += (pIter->nEntry+1);
170579 }
170580 if( rc==SQLITE_OK ){
170581 assert( iNextLeaf==iFree );
170582 rc = fts3NodeWrite(
170583 p, pTree->pParent, iHeight+1, iFree, iNextFree, piLast, paRoot, pnRoot
170584 );
170585 }
170586 }
170587
170588 return rc;
170589}
170590
170591/*
170592** Free all memory allocations associated with the tree pTree.
170593*/
170594static void fts3NodeFree(SegmentNode *pTree){
170595 if( pTree ){
170596 SegmentNode *p = pTree->pLeftmost;
170597 fts3NodeFree(p->pParent);
170598 while( p ){
170599 SegmentNode *pRight = p->pRight;
170600 if( p->aData!=(char *)&p[1] ){
170601 sqlite3_free(p->aData);
170602 }
170603 assert( pRight==0 || p->zMalloc==0 );
170604 sqlite3_free(p->zMalloc);
170605 sqlite3_free(p);
170606 p = pRight;
170607 }
170608 }
170609}
170610
170611/*
170612** Add a term to the segment being constructed by the SegmentWriter object
170613** *ppWriter. When adding the first term to a segment, *ppWriter should
170614** be passed NULL. This function will allocate a new SegmentWriter object
170615** and return it via the input/output variable *ppWriter in this case.
170616**
170617** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
170618*/
170619static int fts3SegWriterAdd(
170620 Fts3Table *p, /* Virtual table handle */
170621 SegmentWriter **ppWriter, /* IN/OUT: SegmentWriter handle */
170622 int isCopyTerm, /* True if buffer zTerm must be copied */
170623 const char *zTerm, /* Pointer to buffer containing term */
170624 int nTerm, /* Size of term in bytes */
170625 const char *aDoclist, /* Pointer to buffer containing doclist */
170626 int nDoclist /* Size of doclist in bytes */
170627){
170628 int nPrefix; /* Size of term prefix in bytes */
170629 int nSuffix; /* Size of term suffix in bytes */
170630 int nReq; /* Number of bytes required on leaf page */
170631 int nData;
170632 SegmentWriter *pWriter = *ppWriter;
170633
170634 if( !pWriter ){
170635 int rc;
170636 sqlite3_stmt *pStmt;
170637
170638 /* Allocate the SegmentWriter structure */
170639 pWriter = (SegmentWriter *)sqlite3_malloc(sizeof(SegmentWriter));
170640 if( !pWriter ) return SQLITE_NOMEM;
170641 memset(pWriter, 0, sizeof(SegmentWriter));
170642 *ppWriter = pWriter;
170643
170644 /* Allocate a buffer in which to accumulate data */
170645 pWriter->aData = (char *)sqlite3_malloc(p->nNodeSize);
170646 if( !pWriter->aData ) return SQLITE_NOMEM;
170647 pWriter->nSize = p->nNodeSize;
170648
170649 /* Find the next free blockid in the %_segments table */
170650 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pStmt, 0);
170651 if( rc!=SQLITE_OK ) return rc;
170652 if( SQLITE_ROW==sqlite3_step(pStmt) ){
170653 pWriter->iFree = sqlite3_column_int64(pStmt, 0);
170654 pWriter->iFirst = pWriter->iFree;
170655 }
170656 rc = sqlite3_reset(pStmt);
170657 if( rc!=SQLITE_OK ) return rc;
170658 }
170659 nData = pWriter->nData;
170660
170661 nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm);
170662 nSuffix = nTerm-nPrefix;
170663
170664 /* Figure out how many bytes are required by this new entry */
170665 nReq = sqlite3Fts3VarintLen(nPrefix) + /* varint containing prefix size */
170666 sqlite3Fts3VarintLen(nSuffix) + /* varint containing suffix size */
170667 nSuffix + /* Term suffix */
170668 sqlite3Fts3VarintLen(nDoclist) + /* Size of doclist */
170669 nDoclist; /* Doclist data */
170670
170671 if( nData>0 && nData+nReq>p->nNodeSize ){
170672 int rc;
170673
170674 /* The current leaf node is full. Write it out to the database. */
170675 rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData);
170676 if( rc!=SQLITE_OK ) return rc;
170677 p->nLeafAdd++;
170678
170679 /* Add the current term to the interior node tree. The term added to
170680 ** the interior tree must:
170681 **
170682 ** a) be greater than the largest term on the leaf node just written
170683 ** to the database (still available in pWriter->zTerm), and
170684 **
170685 ** b) be less than or equal to the term about to be added to the new
170686 ** leaf node (zTerm/nTerm).
170687 **
170688 ** In other words, it must be the prefix of zTerm 1 byte longer than
170689 ** the common prefix (if any) of zTerm and pWriter->zTerm.
170690 */
170691 assert( nPrefix<nTerm );
170692 rc = fts3NodeAddTerm(p, &pWriter->pTree, isCopyTerm, zTerm, nPrefix+1);
170693 if( rc!=SQLITE_OK ) return rc;
170694
170695 nData = 0;
170696 pWriter->nTerm = 0;
170697
170698 nPrefix = 0;
170699 nSuffix = nTerm;
170700 nReq = 1 + /* varint containing prefix size */
170701 sqlite3Fts3VarintLen(nTerm) + /* varint containing suffix size */
170702 nTerm + /* Term suffix */
170703 sqlite3Fts3VarintLen(nDoclist) + /* Size of doclist */
170704 nDoclist; /* Doclist data */
170705 }
170706
170707 /* Increase the total number of bytes written to account for the new entry. */
170708 pWriter->nLeafData += nReq;
170709
170710 /* If the buffer currently allocated is too small for this entry, realloc
170711 ** the buffer to make it large enough.
170712 */
170713 if( nReq>pWriter->nSize ){
170714 char *aNew = sqlite3_realloc(pWriter->aData, nReq);
170715 if( !aNew ) return SQLITE_NOMEM;
170716 pWriter->aData = aNew;
170717 pWriter->nSize = nReq;
170718 }
170719 assert( nData+nReq<=pWriter->nSize );
170720
170721 /* Append the prefix-compressed term and doclist to the buffer. */
170722 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix);
170723 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix);
170724 memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix);
170725 nData += nSuffix;
170726 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist);
170727 memcpy(&pWriter->aData[nData], aDoclist, nDoclist);
170728 pWriter->nData = nData + nDoclist;
170729
170730 /* Save the current term so that it can be used to prefix-compress the next.
170731 ** If the isCopyTerm parameter is true, then the buffer pointed to by
170732 ** zTerm is transient, so take a copy of the term data. Otherwise, just
170733 ** store a copy of the pointer.
170734 */
170735 if( isCopyTerm ){
170736 if( nTerm>pWriter->nMalloc ){
170737 char *zNew = sqlite3_realloc(pWriter->zMalloc, nTerm*2);
170738 if( !zNew ){
170739 return SQLITE_NOMEM;
170740 }
170741 pWriter->nMalloc = nTerm*2;
170742 pWriter->zMalloc = zNew;
170743 pWriter->zTerm = zNew;
170744 }
170745 assert( pWriter->zTerm==pWriter->zMalloc );
170746 memcpy(pWriter->zTerm, zTerm, nTerm);
170747 }else{
170748 pWriter->zTerm = (char *)zTerm;
170749 }
170750 pWriter->nTerm = nTerm;
170751
170752 return SQLITE_OK;
170753}
170754
170755/*
170756** Flush all data associated with the SegmentWriter object pWriter to the
170757** database. This function must be called after all terms have been added
170758** to the segment using fts3SegWriterAdd(). If successful, SQLITE_OK is
170759** returned. Otherwise, an SQLite error code.
170760*/
170761static int fts3SegWriterFlush(
170762 Fts3Table *p, /* Virtual table handle */
170763 SegmentWriter *pWriter, /* SegmentWriter to flush to the db */
170764 sqlite3_int64 iLevel, /* Value for 'level' column of %_segdir */
170765 int iIdx /* Value for 'idx' column of %_segdir */
170766){
170767 int rc; /* Return code */
170768 if( pWriter->pTree ){
170769 sqlite3_int64 iLast = 0; /* Largest block id written to database */
170770 sqlite3_int64 iLastLeaf; /* Largest leaf block id written to db */
170771 char *zRoot = NULL; /* Pointer to buffer containing root node */
170772 int nRoot = 0; /* Size of buffer zRoot */
170773
170774 iLastLeaf = pWriter->iFree;
170775 rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, pWriter->nData);
170776 if( rc==SQLITE_OK ){
170777 rc = fts3NodeWrite(p, pWriter->pTree, 1,
170778 pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
170779 }
170780 if( rc==SQLITE_OK ){
170781 rc = fts3WriteSegdir(p, iLevel, iIdx,
170782 pWriter->iFirst, iLastLeaf, iLast, pWriter->nLeafData, zRoot, nRoot);
170783 }
170784 }else{
170785 /* The entire tree fits on the root node. Write it to the segdir table. */
170786 rc = fts3WriteSegdir(p, iLevel, iIdx,
170787 0, 0, 0, pWriter->nLeafData, pWriter->aData, pWriter->nData);
170788 }
170789 p->nLeafAdd++;
170790 return rc;
170791}
170792
170793/*
170794** Release all memory held by the SegmentWriter object passed as the
170795** first argument.
170796*/
170797static void fts3SegWriterFree(SegmentWriter *pWriter){
170798 if( pWriter ){
170799 sqlite3_free(pWriter->aData);
170800 sqlite3_free(pWriter->zMalloc);
170801 fts3NodeFree(pWriter->pTree);
170802 sqlite3_free(pWriter);
170803 }
170804}
170805
170806/*
170807** The first value in the apVal[] array is assumed to contain an integer.
170808** This function tests if there exist any documents with docid values that
170809** are different from that integer. i.e. if deleting the document with docid
170810** pRowid would mean the FTS3 table were empty.
170811**
170812** If successful, *pisEmpty is set to true if the table is empty except for
170813** document pRowid, or false otherwise, and SQLITE_OK is returned. If an
170814** error occurs, an SQLite error code is returned.
170815*/
170816static int fts3IsEmpty(Fts3Table *p, sqlite3_value *pRowid, int *pisEmpty){
170817 sqlite3_stmt *pStmt;
170818 int rc;
170819 if( p->zContentTbl ){
170820 /* If using the content=xxx option, assume the table is never empty */
170821 *pisEmpty = 0;
170822 rc = SQLITE_OK;
170823 }else{
170824 rc = fts3SqlStmt(p, SQL_IS_EMPTY, &pStmt, &pRowid);
170825 if( rc==SQLITE_OK ){
170826 if( SQLITE_ROW==sqlite3_step(pStmt) ){
170827 *pisEmpty = sqlite3_column_int(pStmt, 0);
170828 }
170829 rc = sqlite3_reset(pStmt);
170830 }
170831 }
170832 return rc;
170833}
170834
170835/*
170836** Set *pnMax to the largest segment level in the database for the index
170837** iIndex.
170838**
170839** Segment levels are stored in the 'level' column of the %_segdir table.
170840**
170841** Return SQLITE_OK if successful, or an SQLite error code if not.
170842*/
170843static int fts3SegmentMaxLevel(
170844 Fts3Table *p,
170845 int iLangid,
170846 int iIndex,
170847 sqlite3_int64 *pnMax
170848){
170849 sqlite3_stmt *pStmt;
170850 int rc;
170851 assert( iIndex>=0 && iIndex<p->nIndex );
170852
170853 /* Set pStmt to the compiled version of:
170854 **
170855 ** SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
170856 **
170857 ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
170858 */
170859 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
170860 if( rc!=SQLITE_OK ) return rc;
170861 sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
170862 sqlite3_bind_int64(pStmt, 2,
170863 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
170864 );
170865 if( SQLITE_ROW==sqlite3_step(pStmt) ){
170866 *pnMax = sqlite3_column_int64(pStmt, 0);
170867 }
170868 return sqlite3_reset(pStmt);
170869}
170870
170871/*
170872** iAbsLevel is an absolute level that may be assumed to exist within
170873** the database. This function checks if it is the largest level number
170874** within its index. Assuming no error occurs, *pbMax is set to 1 if
170875** iAbsLevel is indeed the largest level, or 0 otherwise, and SQLITE_OK
170876** is returned. If an error occurs, an error code is returned and the
170877** final value of *pbMax is undefined.
170878*/
170879static int fts3SegmentIsMaxLevel(Fts3Table *p, i64 iAbsLevel, int *pbMax){
170880
170881 /* Set pStmt to the compiled version of:
170882 **
170883 ** SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
170884 **
170885 ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
170886 */
170887 sqlite3_stmt *pStmt;
170888 int rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
170889 if( rc!=SQLITE_OK ) return rc;
170890 sqlite3_bind_int64(pStmt, 1, iAbsLevel+1);
170891 sqlite3_bind_int64(pStmt, 2,
170892 ((iAbsLevel/FTS3_SEGDIR_MAXLEVEL)+1) * FTS3_SEGDIR_MAXLEVEL
170893 );
170894
170895 *pbMax = 0;
170896 if( SQLITE_ROW==sqlite3_step(pStmt) ){
170897 *pbMax = sqlite3_column_type(pStmt, 0)==SQLITE_NULL;
170898 }
170899 return sqlite3_reset(pStmt);
170900}
170901
170902/*
170903** Delete all entries in the %_segments table associated with the segment
170904** opened with seg-reader pSeg. This function does not affect the contents
170905** of the %_segdir table.
170906*/
170907static int fts3DeleteSegment(
170908 Fts3Table *p, /* FTS table handle */
170909 Fts3SegReader *pSeg /* Segment to delete */
170910){
170911 int rc = SQLITE_OK; /* Return code */
170912 if( pSeg->iStartBlock ){
170913 sqlite3_stmt *pDelete; /* SQL statement to delete rows */
170914 rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDelete, 0);
170915 if( rc==SQLITE_OK ){
170916 sqlite3_bind_int64(pDelete, 1, pSeg->iStartBlock);
170917 sqlite3_bind_int64(pDelete, 2, pSeg->iEndBlock);
170918 sqlite3_step(pDelete);
170919 rc = sqlite3_reset(pDelete);
170920 }
170921 }
170922 return rc;
170923}
170924
170925/*
170926** This function is used after merging multiple segments into a single large
170927** segment to delete the old, now redundant, segment b-trees. Specifically,
170928** it:
170929**
170930** 1) Deletes all %_segments entries for the segments associated with
170931** each of the SegReader objects in the array passed as the third
170932** argument, and
170933**
170934** 2) deletes all %_segdir entries with level iLevel, or all %_segdir
170935** entries regardless of level if (iLevel<0).
170936**
170937** SQLITE_OK is returned if successful, otherwise an SQLite error code.
170938*/
170939static int fts3DeleteSegdir(
170940 Fts3Table *p, /* Virtual table handle */
170941 int iLangid, /* Language id */
170942 int iIndex, /* Index for p->aIndex */
170943 int iLevel, /* Level of %_segdir entries to delete */
170944 Fts3SegReader **apSegment, /* Array of SegReader objects */
170945 int nReader /* Size of array apSegment */
170946){
170947 int rc = SQLITE_OK; /* Return Code */
170948 int i; /* Iterator variable */
170949 sqlite3_stmt *pDelete = 0; /* SQL statement to delete rows */
170950
170951 for(i=0; rc==SQLITE_OK && i<nReader; i++){
170952 rc = fts3DeleteSegment(p, apSegment[i]);
170953 }
170954 if( rc!=SQLITE_OK ){
170955 return rc;
170956 }
170957
170958 assert( iLevel>=0 || iLevel==FTS3_SEGCURSOR_ALL );
170959 if( iLevel==FTS3_SEGCURSOR_ALL ){
170960 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_RANGE, &pDelete, 0);
170961 if( rc==SQLITE_OK ){
170962 sqlite3_bind_int64(pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
170963 sqlite3_bind_int64(pDelete, 2,
170964 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
170965 );
170966 }
170967 }else{
170968 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pDelete, 0);
170969 if( rc==SQLITE_OK ){
170970 sqlite3_bind_int64(
170971 pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
170972 );
170973 }
170974 }
170975
170976 if( rc==SQLITE_OK ){
170977 sqlite3_step(pDelete);
170978 rc = sqlite3_reset(pDelete);
170979 }
170980
170981 return rc;
170982}
170983
170984/*
170985** When this function is called, buffer *ppList (size *pnList bytes) contains
170986** a position list that may (or may not) feature multiple columns. This
170987** function adjusts the pointer *ppList and the length *pnList so that they
170988** identify the subset of the position list that corresponds to column iCol.
170989**
170990** If there are no entries in the input position list for column iCol, then
170991** *pnList is set to zero before returning.
170992**
170993** If parameter bZero is non-zero, then any part of the input list following
170994** the end of the output list is zeroed before returning.
170995*/
170996static void fts3ColumnFilter(
170997 int iCol, /* Column to filter on */
170998 int bZero, /* Zero out anything following *ppList */
170999 char **ppList, /* IN/OUT: Pointer to position list */
171000 int *pnList /* IN/OUT: Size of buffer *ppList in bytes */
171001){
171002 char *pList = *ppList;
171003 int nList = *pnList;
171004 char *pEnd = &pList[nList];
171005 int iCurrent = 0;
171006 char *p = pList;
171007
171008 assert( iCol>=0 );
171009 while( 1 ){
171010 char c = 0;
171011 while( p<pEnd && (c | *p)&0xFE ) c = *p++ & 0x80;
171012
171013 if( iCol==iCurrent ){
171014 nList = (int)(p - pList);
171015 break;
171016 }
171017
171018 nList -= (int)(p - pList);
171019 pList = p;
171020 if( nList==0 ){
171021 break;
171022 }
171023 p = &pList[1];
171024 p += fts3GetVarint32(p, &iCurrent);
171025 }
171026
171027 if( bZero && &pList[nList]!=pEnd ){
171028 memset(&pList[nList], 0, pEnd - &pList[nList]);
171029 }
171030 *ppList = pList;
171031 *pnList = nList;
171032}
171033
171034/*
171035** Cache data in the Fts3MultiSegReader.aBuffer[] buffer (overwriting any
171036** existing data). Grow the buffer if required.
171037**
171038** If successful, return SQLITE_OK. Otherwise, if an OOM error is encountered
171039** trying to resize the buffer, return SQLITE_NOMEM.
171040*/
171041static int fts3MsrBufferData(
171042 Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
171043 char *pList,
171044 int nList
171045){
171046 if( nList>pMsr->nBuffer ){
171047 char *pNew;
171048 pMsr->nBuffer = nList*2;
171049 pNew = (char *)sqlite3_realloc(pMsr->aBuffer, pMsr->nBuffer);
171050 if( !pNew ) return SQLITE_NOMEM;
171051 pMsr->aBuffer = pNew;
171052 }
171053
171054 memcpy(pMsr->aBuffer, pList, nList);
171055 return SQLITE_OK;
171056}
171057
171058SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
171059 Fts3Table *p, /* Virtual table handle */
171060 Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
171061 sqlite3_int64 *piDocid, /* OUT: Docid value */
171062 char **paPoslist, /* OUT: Pointer to position list */
171063 int *pnPoslist /* OUT: Size of position list in bytes */
171064){
171065 int nMerge = pMsr->nAdvance;
171066 Fts3SegReader **apSegment = pMsr->apSegment;
171067 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
171068 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
171069 );
171070
171071 if( nMerge==0 ){
171072 *paPoslist = 0;
171073 return SQLITE_OK;
171074 }
171075
171076 while( 1 ){
171077 Fts3SegReader *pSeg;
171078 pSeg = pMsr->apSegment[0];
171079
171080 if( pSeg->pOffsetList==0 ){
171081 *paPoslist = 0;
171082 break;
171083 }else{
171084 int rc;
171085 char *pList;
171086 int nList;
171087 int j;
171088 sqlite3_int64 iDocid = apSegment[0]->iDocid;
171089
171090 rc = fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
171091 j = 1;
171092 while( rc==SQLITE_OK
171093 && j<nMerge
171094 && apSegment[j]->pOffsetList
171095 && apSegment[j]->iDocid==iDocid
171096 ){
171097 rc = fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
171098 j++;
171099 }
171100 if( rc!=SQLITE_OK ) return rc;
171101 fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp);
171102
171103 if( nList>0 && fts3SegReaderIsPending(apSegment[0]) ){
171104 rc = fts3MsrBufferData(pMsr, pList, nList+1);
171105 if( rc!=SQLITE_OK ) return rc;
171106 assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 );
171107 pList = pMsr->aBuffer;
171108 }
171109
171110 if( pMsr->iColFilter>=0 ){
171111 fts3ColumnFilter(pMsr->iColFilter, 1, &pList, &nList);
171112 }
171113
171114 if( nList>0 ){
171115 *paPoslist = pList;
171116 *piDocid = iDocid;
171117 *pnPoslist = nList;
171118 break;
171119 }
171120 }
171121 }
171122
171123 return SQLITE_OK;
171124}
171125
171126static int fts3SegReaderStart(
171127 Fts3Table *p, /* Virtual table handle */
171128 Fts3MultiSegReader *pCsr, /* Cursor object */
171129 const char *zTerm, /* Term searched for (or NULL) */
171130 int nTerm /* Length of zTerm in bytes */
171131){
171132 int i;
171133 int nSeg = pCsr->nSegment;
171134
171135 /* If the Fts3SegFilter defines a specific term (or term prefix) to search
171136 ** for, then advance each segment iterator until it points to a term of
171137 ** equal or greater value than the specified term. This prevents many
171138 ** unnecessary merge/sort operations for the case where single segment
171139 ** b-tree leaf nodes contain more than one term.
171140 */
171141 for(i=0; pCsr->bRestart==0 && i<pCsr->nSegment; i++){
171142 int res = 0;
171143 Fts3SegReader *pSeg = pCsr->apSegment[i];
171144 do {
171145 int rc = fts3SegReaderNext(p, pSeg, 0);
171146 if( rc!=SQLITE_OK ) return rc;
171147 }while( zTerm && (res = fts3SegReaderTermCmp(pSeg, zTerm, nTerm))<0 );
171148
171149 if( pSeg->bLookup && res!=0 ){
171150 fts3SegReaderSetEof(pSeg);
171151 }
171152 }
171153 fts3SegReaderSort(pCsr->apSegment, nSeg, nSeg, fts3SegReaderCmp);
171154
171155 return SQLITE_OK;
171156}
171157
171158SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(
171159 Fts3Table *p, /* Virtual table handle */
171160 Fts3MultiSegReader *pCsr, /* Cursor object */
171161 Fts3SegFilter *pFilter /* Restrictions on range of iteration */
171162){
171163 pCsr->pFilter = pFilter;
171164 return fts3SegReaderStart(p, pCsr, pFilter->zTerm, pFilter->nTerm);
171165}
171166
171167SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
171168 Fts3Table *p, /* Virtual table handle */
171169 Fts3MultiSegReader *pCsr, /* Cursor object */
171170 int iCol, /* Column to match on. */
171171 const char *zTerm, /* Term to iterate through a doclist for */
171172 int nTerm /* Number of bytes in zTerm */
171173){
171174 int i;
171175 int rc;
171176 int nSegment = pCsr->nSegment;
171177 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
171178 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
171179 );
171180
171181 assert( pCsr->pFilter==0 );
171182 assert( zTerm && nTerm>0 );
171183
171184 /* Advance each segment iterator until it points to the term zTerm/nTerm. */
171185 rc = fts3SegReaderStart(p, pCsr, zTerm, nTerm);
171186 if( rc!=SQLITE_OK ) return rc;
171187
171188 /* Determine how many of the segments actually point to zTerm/nTerm. */
171189 for(i=0; i<nSegment; i++){
171190 Fts3SegReader *pSeg = pCsr->apSegment[i];
171191 if( !pSeg->aNode || fts3SegReaderTermCmp(pSeg, zTerm, nTerm) ){
171192 break;
171193 }
171194 }
171195 pCsr->nAdvance = i;
171196
171197 /* Advance each of the segments to point to the first docid. */
171198 for(i=0; i<pCsr->nAdvance; i++){
171199 rc = fts3SegReaderFirstDocid(p, pCsr->apSegment[i]);
171200 if( rc!=SQLITE_OK ) return rc;
171201 }
171202 fts3SegReaderSort(pCsr->apSegment, i, i, xCmp);
171203
171204 assert( iCol<0 || iCol<p->nColumn );
171205 pCsr->iColFilter = iCol;
171206
171207 return SQLITE_OK;
171208}
171209
171210/*
171211** This function is called on a MultiSegReader that has been started using
171212** sqlite3Fts3MsrIncrStart(). One or more calls to MsrIncrNext() may also
171213** have been made. Calling this function puts the MultiSegReader in such
171214** a state that if the next two calls are:
171215**
171216** sqlite3Fts3SegReaderStart()
171217** sqlite3Fts3SegReaderStep()
171218**
171219** then the entire doclist for the term is available in
171220** MultiSegReader.aDoclist/nDoclist.
171221*/
171222SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr){
171223 int i; /* Used to iterate through segment-readers */
171224
171225 assert( pCsr->zTerm==0 );
171226 assert( pCsr->nTerm==0 );
171227 assert( pCsr->aDoclist==0 );
171228 assert( pCsr->nDoclist==0 );
171229
171230 pCsr->nAdvance = 0;
171231 pCsr->bRestart = 1;
171232 for(i=0; i<pCsr->nSegment; i++){
171233 pCsr->apSegment[i]->pOffsetList = 0;
171234 pCsr->apSegment[i]->nOffsetList = 0;
171235 pCsr->apSegment[i]->iDocid = 0;
171236 }
171237
171238 return SQLITE_OK;
171239}
171240
171241
171242SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(
171243 Fts3Table *p, /* Virtual table handle */
171244 Fts3MultiSegReader *pCsr /* Cursor object */
171245){
171246 int rc = SQLITE_OK;
171247
171248 int isIgnoreEmpty = (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
171249 int isRequirePos = (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
171250 int isColFilter = (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
171251 int isPrefix = (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX);
171252 int isScan = (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN);
171253 int isFirst = (pCsr->pFilter->flags & FTS3_SEGMENT_FIRST);
171254
171255 Fts3SegReader **apSegment = pCsr->apSegment;
171256 int nSegment = pCsr->nSegment;
171257 Fts3SegFilter *pFilter = pCsr->pFilter;
171258 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
171259 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
171260 );
171261
171262 if( pCsr->nSegment==0 ) return SQLITE_OK;
171263
171264 do {
171265 int nMerge;
171266 int i;
171267
171268 /* Advance the first pCsr->nAdvance entries in the apSegment[] array
171269 ** forward. Then sort the list in order of current term again.
171270 */
171271 for(i=0; i<pCsr->nAdvance; i++){
171272 Fts3SegReader *pSeg = apSegment[i];
171273 if( pSeg->bLookup ){
171274 fts3SegReaderSetEof(pSeg);
171275 }else{
171276 rc = fts3SegReaderNext(p, pSeg, 0);
171277 }
171278 if( rc!=SQLITE_OK ) return rc;
171279 }
171280 fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp);
171281 pCsr->nAdvance = 0;
171282
171283 /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
171284 assert( rc==SQLITE_OK );
171285 if( apSegment[0]->aNode==0 ) break;
171286
171287 pCsr->nTerm = apSegment[0]->nTerm;
171288 pCsr->zTerm = apSegment[0]->zTerm;
171289
171290 /* If this is a prefix-search, and if the term that apSegment[0] points
171291 ** to does not share a suffix with pFilter->zTerm/nTerm, then all
171292 ** required callbacks have been made. In this case exit early.
171293 **
171294 ** Similarly, if this is a search for an exact match, and the first term
171295 ** of segment apSegment[0] is not a match, exit early.
171296 */
171297 if( pFilter->zTerm && !isScan ){
171298 if( pCsr->nTerm<pFilter->nTerm
171299 || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
171300 || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm)
171301 ){
171302 break;
171303 }
171304 }
171305
171306 nMerge = 1;
171307 while( nMerge<nSegment
171308 && apSegment[nMerge]->aNode
171309 && apSegment[nMerge]->nTerm==pCsr->nTerm
171310 && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm)
171311 ){
171312 nMerge++;
171313 }
171314
171315 assert( isIgnoreEmpty || (isRequirePos && !isColFilter) );
171316 if( nMerge==1
171317 && !isIgnoreEmpty
171318 && !isFirst
171319 && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])==0)
171320 ){
171321 pCsr->nDoclist = apSegment[0]->nDoclist;
171322 if( fts3SegReaderIsPending(apSegment[0]) ){
171323 rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, pCsr->nDoclist);
171324 pCsr->aDoclist = pCsr->aBuffer;
171325 }else{
171326 pCsr->aDoclist = apSegment[0]->aDoclist;
171327 }
171328 if( rc==SQLITE_OK ) rc = SQLITE_ROW;
171329 }else{
171330 int nDoclist = 0; /* Size of doclist */
171331 sqlite3_int64 iPrev = 0; /* Previous docid stored in doclist */
171332
171333 /* The current term of the first nMerge entries in the array
171334 ** of Fts3SegReader objects is the same. The doclists must be merged
171335 ** and a single term returned with the merged doclist.
171336 */
171337 for(i=0; i<nMerge; i++){
171338 fts3SegReaderFirstDocid(p, apSegment[i]);
171339 }
171340 fts3SegReaderSort(apSegment, nMerge, nMerge, xCmp);
171341 while( apSegment[0]->pOffsetList ){
171342 int j; /* Number of segments that share a docid */
171343 char *pList = 0;
171344 int nList = 0;
171345 int nByte;
171346 sqlite3_int64 iDocid = apSegment[0]->iDocid;
171347 fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
171348 j = 1;
171349 while( j<nMerge
171350 && apSegment[j]->pOffsetList
171351 && apSegment[j]->iDocid==iDocid
171352 ){
171353 fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
171354 j++;
171355 }
171356
171357 if( isColFilter ){
171358 fts3ColumnFilter(pFilter->iCol, 0, &pList, &nList);
171359 }
171360
171361 if( !isIgnoreEmpty || nList>0 ){
171362
171363 /* Calculate the 'docid' delta value to write into the merged
171364 ** doclist. */
171365 sqlite3_int64 iDelta;
171366 if( p->bDescIdx && nDoclist>0 ){
171367 iDelta = iPrev - iDocid;
171368 }else{
171369 iDelta = iDocid - iPrev;
171370 }
171371 assert( iDelta>0 || (nDoclist==0 && iDelta==iDocid) );
171372 assert( nDoclist>0 || iDelta==iDocid );
171373
171374 nByte = sqlite3Fts3VarintLen(iDelta) + (isRequirePos?nList+1:0);
171375 if( nDoclist+nByte>pCsr->nBuffer ){
171376 char *aNew;
171377 pCsr->nBuffer = (nDoclist+nByte)*2;
171378 aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer);
171379 if( !aNew ){
171380 return SQLITE_NOMEM;
171381 }
171382 pCsr->aBuffer = aNew;
171383 }
171384
171385 if( isFirst ){
171386 char *a = &pCsr->aBuffer[nDoclist];
171387 int nWrite;
171388
171389 nWrite = sqlite3Fts3FirstFilter(iDelta, pList, nList, a);
171390 if( nWrite ){
171391 iPrev = iDocid;
171392 nDoclist += nWrite;
171393 }
171394 }else{
171395 nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta);
171396 iPrev = iDocid;
171397 if( isRequirePos ){
171398 memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
171399 nDoclist += nList;
171400 pCsr->aBuffer[nDoclist++] = '\0';
171401 }
171402 }
171403 }
171404
171405 fts3SegReaderSort(apSegment, nMerge, j, xCmp);
171406 }
171407 if( nDoclist>0 ){
171408 pCsr->aDoclist = pCsr->aBuffer;
171409 pCsr->nDoclist = nDoclist;
171410 rc = SQLITE_ROW;
171411 }
171412 }
171413 pCsr->nAdvance = nMerge;
171414 }while( rc==SQLITE_OK );
171415
171416 return rc;
171417}
171418
171419
171420SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(
171421 Fts3MultiSegReader *pCsr /* Cursor object */
171422){
171423 if( pCsr ){
171424 int i;
171425 for(i=0; i<pCsr->nSegment; i++){
171426 sqlite3Fts3SegReaderFree(pCsr->apSegment[i]);
171427 }
171428 sqlite3_free(pCsr->apSegment);
171429 sqlite3_free(pCsr->aBuffer);
171430
171431 pCsr->nSegment = 0;
171432 pCsr->apSegment = 0;
171433 pCsr->aBuffer = 0;
171434 }
171435}
171436
171437/*
171438** Decode the "end_block" field, selected by column iCol of the SELECT
171439** statement passed as the first argument.
171440**
171441** The "end_block" field may contain either an integer, or a text field
171442** containing the text representation of two non-negative integers separated
171443** by one or more space (0x20) characters. In the first case, set *piEndBlock
171444** to the integer value and *pnByte to zero before returning. In the second,
171445** set *piEndBlock to the first value and *pnByte to the second.
171446*/
171447static void fts3ReadEndBlockField(
171448 sqlite3_stmt *pStmt,
171449 int iCol,
171450 i64 *piEndBlock,
171451 i64 *pnByte
171452){
171453 const unsigned char *zText = sqlite3_column_text(pStmt, iCol);
171454 if( zText ){
171455 int i;
171456 int iMul = 1;
171457 i64 iVal = 0;
171458 for(i=0; zText[i]>='0' && zText[i]<='9'; i++){
171459 iVal = iVal*10 + (zText[i] - '0');
171460 }
171461 *piEndBlock = iVal;
171462 while( zText[i]==' ' ) i++;
171463 iVal = 0;
171464 if( zText[i]=='-' ){
171465 i++;
171466 iMul = -1;
171467 }
171468 for(/* no-op */; zText[i]>='0' && zText[i]<='9'; i++){
171469 iVal = iVal*10 + (zText[i] - '0');
171470 }
171471 *pnByte = (iVal * (i64)iMul);
171472 }
171473}
171474
171475
171476/*
171477** A segment of size nByte bytes has just been written to absolute level
171478** iAbsLevel. Promote any segments that should be promoted as a result.
171479*/
171480static int fts3PromoteSegments(
171481 Fts3Table *p, /* FTS table handle */
171482 sqlite3_int64 iAbsLevel, /* Absolute level just updated */
171483 sqlite3_int64 nByte /* Size of new segment at iAbsLevel */
171484){
171485 int rc = SQLITE_OK;
171486 sqlite3_stmt *pRange;
171487
171488 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE2, &pRange, 0);
171489
171490 if( rc==SQLITE_OK ){
171491 int bOk = 0;
171492 i64 iLast = (iAbsLevel/FTS3_SEGDIR_MAXLEVEL + 1) * FTS3_SEGDIR_MAXLEVEL - 1;
171493 i64 nLimit = (nByte*3)/2;
171494
171495 /* Loop through all entries in the %_segdir table corresponding to
171496 ** segments in this index on levels greater than iAbsLevel. If there is
171497 ** at least one such segment, and it is possible to determine that all
171498 ** such segments are smaller than nLimit bytes in size, they will be
171499 ** promoted to level iAbsLevel. */
171500 sqlite3_bind_int64(pRange, 1, iAbsLevel+1);
171501 sqlite3_bind_int64(pRange, 2, iLast);
171502 while( SQLITE_ROW==sqlite3_step(pRange) ){
171503 i64 nSize = 0, dummy;
171504 fts3ReadEndBlockField(pRange, 2, &dummy, &nSize);
171505 if( nSize<=0 || nSize>nLimit ){
171506 /* If nSize==0, then the %_segdir.end_block field does not not
171507 ** contain a size value. This happens if it was written by an
171508 ** old version of FTS. In this case it is not possible to determine
171509 ** the size of the segment, and so segment promotion does not
171510 ** take place. */
171511 bOk = 0;
171512 break;
171513 }
171514 bOk = 1;
171515 }
171516 rc = sqlite3_reset(pRange);
171517
171518 if( bOk ){
171519 int iIdx = 0;
171520 sqlite3_stmt *pUpdate1 = 0;
171521 sqlite3_stmt *pUpdate2 = 0;
171522
171523 if( rc==SQLITE_OK ){
171524 rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL_IDX, &pUpdate1, 0);
171525 }
171526 if( rc==SQLITE_OK ){
171527 rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL, &pUpdate2, 0);
171528 }
171529
171530 if( rc==SQLITE_OK ){
171531
171532 /* Loop through all %_segdir entries for segments in this index with
171533 ** levels equal to or greater than iAbsLevel. As each entry is visited,
171534 ** updated it to set (level = -1) and (idx = N), where N is 0 for the
171535 ** oldest segment in the range, 1 for the next oldest, and so on.
171536 **
171537 ** In other words, move all segments being promoted to level -1,
171538 ** setting the "idx" fields as appropriate to keep them in the same
171539 ** order. The contents of level -1 (which is never used, except
171540 ** transiently here), will be moved back to level iAbsLevel below. */
171541 sqlite3_bind_int64(pRange, 1, iAbsLevel);
171542 while( SQLITE_ROW==sqlite3_step(pRange) ){
171543 sqlite3_bind_int(pUpdate1, 1, iIdx++);
171544 sqlite3_bind_int(pUpdate1, 2, sqlite3_column_int(pRange, 0));
171545 sqlite3_bind_int(pUpdate1, 3, sqlite3_column_int(pRange, 1));
171546 sqlite3_step(pUpdate1);
171547 rc = sqlite3_reset(pUpdate1);
171548 if( rc!=SQLITE_OK ){
171549 sqlite3_reset(pRange);
171550 break;
171551 }
171552 }
171553 }
171554 if( rc==SQLITE_OK ){
171555 rc = sqlite3_reset(pRange);
171556 }
171557
171558 /* Move level -1 to level iAbsLevel */
171559 if( rc==SQLITE_OK ){
171560 sqlite3_bind_int64(pUpdate2, 1, iAbsLevel);
171561 sqlite3_step(pUpdate2);
171562 rc = sqlite3_reset(pUpdate2);
171563 }
171564 }
171565 }
171566
171567
171568 return rc;
171569}
171570
171571/*
171572** Merge all level iLevel segments in the database into a single
171573** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
171574** single segment with a level equal to the numerically largest level
171575** currently present in the database.
171576**
171577** If this function is called with iLevel<0, but there is only one
171578** segment in the database, SQLITE_DONE is returned immediately.
171579** Otherwise, if successful, SQLITE_OK is returned. If an error occurs,
171580** an SQLite error code is returned.
171581*/
171582static int fts3SegmentMerge(
171583 Fts3Table *p,
171584 int iLangid, /* Language id to merge */
171585 int iIndex, /* Index in p->aIndex[] to merge */
171586 int iLevel /* Level to merge */
171587){
171588 int rc; /* Return code */
171589 int iIdx = 0; /* Index of new segment */
171590 sqlite3_int64 iNewLevel = 0; /* Level/index to create new segment at */
171591 SegmentWriter *pWriter = 0; /* Used to write the new, merged, segment */
171592 Fts3SegFilter filter; /* Segment term filter condition */
171593 Fts3MultiSegReader csr; /* Cursor to iterate through level(s) */
171594 int bIgnoreEmpty = 0; /* True to ignore empty segments */
171595 i64 iMaxLevel = 0; /* Max level number for this index/langid */
171596
171597 assert( iLevel==FTS3_SEGCURSOR_ALL
171598 || iLevel==FTS3_SEGCURSOR_PENDING
171599 || iLevel>=0
171600 );
171601 assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
171602 assert( iIndex>=0 && iIndex<p->nIndex );
171603
171604 rc = sqlite3Fts3SegReaderCursor(p, iLangid, iIndex, iLevel, 0, 0, 1, 0, &csr);
171605 if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished;
171606
171607 if( iLevel!=FTS3_SEGCURSOR_PENDING ){
171608 rc = fts3SegmentMaxLevel(p, iLangid, iIndex, &iMaxLevel);
171609 if( rc!=SQLITE_OK ) goto finished;
171610 }
171611
171612 if( iLevel==FTS3_SEGCURSOR_ALL ){
171613 /* This call is to merge all segments in the database to a single
171614 ** segment. The level of the new segment is equal to the numerically
171615 ** greatest segment level currently present in the database for this
171616 ** index. The idx of the new segment is always 0. */
171617 if( csr.nSegment==1 && 0==fts3SegReaderIsPending(csr.apSegment[0]) ){
171618 rc = SQLITE_DONE;
171619 goto finished;
171620 }
171621 iNewLevel = iMaxLevel;
171622 bIgnoreEmpty = 1;
171623
171624 }else{
171625 /* This call is to merge all segments at level iLevel. find the next
171626 ** available segment index at level iLevel+1. The call to
171627 ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to
171628 ** a single iLevel+2 segment if necessary. */
171629 assert( FTS3_SEGCURSOR_PENDING==-1 );
171630 iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, iLevel+1);
171631 rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, iLevel+1, &iIdx);
171632 bIgnoreEmpty = (iLevel!=FTS3_SEGCURSOR_PENDING) && (iNewLevel>iMaxLevel);
171633 }
171634 if( rc!=SQLITE_OK ) goto finished;
171635
171636 assert( csr.nSegment>0 );
171637 assert( iNewLevel>=getAbsoluteLevel(p, iLangid, iIndex, 0) );
171638 assert( iNewLevel<getAbsoluteLevel(p, iLangid, iIndex,FTS3_SEGDIR_MAXLEVEL) );
171639
171640 memset(&filter, 0, sizeof(Fts3SegFilter));
171641 filter.flags = FTS3_SEGMENT_REQUIRE_POS;
171642 filter.flags |= (bIgnoreEmpty ? FTS3_SEGMENT_IGNORE_EMPTY : 0);
171643
171644 rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
171645 while( SQLITE_OK==rc ){
171646 rc = sqlite3Fts3SegReaderStep(p, &csr);
171647 if( rc!=SQLITE_ROW ) break;
171648 rc = fts3SegWriterAdd(p, &pWriter, 1,
171649 csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
171650 }
171651 if( rc!=SQLITE_OK ) goto finished;
171652 assert( pWriter || bIgnoreEmpty );
171653
171654 if( iLevel!=FTS3_SEGCURSOR_PENDING ){
171655 rc = fts3DeleteSegdir(
171656 p, iLangid, iIndex, iLevel, csr.apSegment, csr.nSegment
171657 );
171658 if( rc!=SQLITE_OK ) goto finished;
171659 }
171660 if( pWriter ){
171661 rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
171662 if( rc==SQLITE_OK ){
171663 if( iLevel==FTS3_SEGCURSOR_PENDING || iNewLevel<iMaxLevel ){
171664 rc = fts3PromoteSegments(p, iNewLevel, pWriter->nLeafData);
171665 }
171666 }
171667 }
171668
171669 finished:
171670 fts3SegWriterFree(pWriter);
171671 sqlite3Fts3SegReaderFinish(&csr);
171672 return rc;
171673}
171674
171675
171676/*
171677** Flush the contents of pendingTerms to level 0 segments.
171678*/
171679SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
171680 int rc = SQLITE_OK;
171681 int i;
171682
171683 for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
171684 rc = fts3SegmentMerge(p, p->iPrevLangid, i, FTS3_SEGCURSOR_PENDING);
171685 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
171686 }
171687 sqlite3Fts3PendingTermsClear(p);
171688
171689 /* Determine the auto-incr-merge setting if unknown. If enabled,
171690 ** estimate the number of leaf blocks of content to be written
171691 */
171692 if( rc==SQLITE_OK && p->bHasStat
171693 && p->nAutoincrmerge==0xff && p->nLeafAdd>0
171694 ){
171695 sqlite3_stmt *pStmt = 0;
171696 rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
171697 if( rc==SQLITE_OK ){
171698 sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
171699 rc = sqlite3_step(pStmt);
171700 if( rc==SQLITE_ROW ){
171701 p->nAutoincrmerge = sqlite3_column_int(pStmt, 0);
171702 if( p->nAutoincrmerge==1 ) p->nAutoincrmerge = 8;
171703 }else if( rc==SQLITE_DONE ){
171704 p->nAutoincrmerge = 0;
171705 }
171706 rc = sqlite3_reset(pStmt);
171707 }
171708 }
171709 return rc;
171710}
171711
171712/*
171713** Encode N integers as varints into a blob.
171714*/
171715static void fts3EncodeIntArray(
171716 int N, /* The number of integers to encode */
171717 u32 *a, /* The integer values */
171718 char *zBuf, /* Write the BLOB here */
171719 int *pNBuf /* Write number of bytes if zBuf[] used here */
171720){
171721 int i, j;
171722 for(i=j=0; i<N; i++){
171723 j += sqlite3Fts3PutVarint(&zBuf[j], (sqlite3_int64)a[i]);
171724 }
171725 *pNBuf = j;
171726}
171727
171728/*
171729** Decode a blob of varints into N integers
171730*/
171731static void fts3DecodeIntArray(
171732 int N, /* The number of integers to decode */
171733 u32 *a, /* Write the integer values */
171734 const char *zBuf, /* The BLOB containing the varints */
171735 int nBuf /* size of the BLOB */
171736){
171737 int i, j;
171738 UNUSED_PARAMETER(nBuf);
171739 for(i=j=0; i<N; i++){
171740 sqlite3_int64 x;
171741 j += sqlite3Fts3GetVarint(&zBuf[j], &x);
171742 assert(j<=nBuf);
171743 a[i] = (u32)(x & 0xffffffff);
171744 }
171745}
171746
171747/*
171748** Insert the sizes (in tokens) for each column of the document
171749** with docid equal to p->iPrevDocid. The sizes are encoded as
171750** a blob of varints.
171751*/
171752static void fts3InsertDocsize(
171753 int *pRC, /* Result code */
171754 Fts3Table *p, /* Table into which to insert */
171755 u32 *aSz /* Sizes of each column, in tokens */
171756){
171757 char *pBlob; /* The BLOB encoding of the document size */
171758 int nBlob; /* Number of bytes in the BLOB */
171759 sqlite3_stmt *pStmt; /* Statement used to insert the encoding */
171760 int rc; /* Result code from subfunctions */
171761
171762 if( *pRC ) return;
171763 pBlob = sqlite3_malloc( 10*p->nColumn );
171764 if( pBlob==0 ){
171765 *pRC = SQLITE_NOMEM;
171766 return;
171767 }
171768 fts3EncodeIntArray(p->nColumn, aSz, pBlob, &nBlob);
171769 rc = fts3SqlStmt(p, SQL_REPLACE_DOCSIZE, &pStmt, 0);
171770 if( rc ){
171771 sqlite3_free(pBlob);
171772 *pRC = rc;
171773 return;
171774 }
171775 sqlite3_bind_int64(pStmt, 1, p->iPrevDocid);
171776 sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, sqlite3_free);
171777 sqlite3_step(pStmt);
171778 *pRC = sqlite3_reset(pStmt);
171779}
171780
171781/*
171782** Record 0 of the %_stat table contains a blob consisting of N varints,
171783** where N is the number of user defined columns in the fts3 table plus
171784** two. If nCol is the number of user defined columns, then values of the
171785** varints are set as follows:
171786**
171787** Varint 0: Total number of rows in the table.
171788**
171789** Varint 1..nCol: For each column, the total number of tokens stored in
171790** the column for all rows of the table.
171791**
171792** Varint 1+nCol: The total size, in bytes, of all text values in all
171793** columns of all rows of the table.
171794**
171795*/
171796static void fts3UpdateDocTotals(
171797 int *pRC, /* The result code */
171798 Fts3Table *p, /* Table being updated */
171799 u32 *aSzIns, /* Size increases */
171800 u32 *aSzDel, /* Size decreases */
171801 int nChng /* Change in the number of documents */
171802){
171803 char *pBlob; /* Storage for BLOB written into %_stat */
171804 int nBlob; /* Size of BLOB written into %_stat */
171805 u32 *a; /* Array of integers that becomes the BLOB */
171806 sqlite3_stmt *pStmt; /* Statement for reading and writing */
171807 int i; /* Loop counter */
171808 int rc; /* Result code from subfunctions */
171809
171810 const int nStat = p->nColumn+2;
171811
171812 if( *pRC ) return;
171813 a = sqlite3_malloc( (sizeof(u32)+10)*nStat );
171814 if( a==0 ){
171815 *pRC = SQLITE_NOMEM;
171816 return;
171817 }
171818 pBlob = (char*)&a[nStat];
171819 rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
171820 if( rc ){
171821 sqlite3_free(a);
171822 *pRC = rc;
171823 return;
171824 }
171825 sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
171826 if( sqlite3_step(pStmt)==SQLITE_ROW ){
171827 fts3DecodeIntArray(nStat, a,
171828 sqlite3_column_blob(pStmt, 0),
171829 sqlite3_column_bytes(pStmt, 0));
171830 }else{
171831 memset(a, 0, sizeof(u32)*(nStat) );
171832 }
171833 rc = sqlite3_reset(pStmt);
171834 if( rc!=SQLITE_OK ){
171835 sqlite3_free(a);
171836 *pRC = rc;
171837 return;
171838 }
171839 if( nChng<0 && a[0]<(u32)(-nChng) ){
171840 a[0] = 0;
171841 }else{
171842 a[0] += nChng;
171843 }
171844 for(i=0; i<p->nColumn+1; i++){
171845 u32 x = a[i+1];
171846 if( x+aSzIns[i] < aSzDel[i] ){
171847 x = 0;
171848 }else{
171849 x = x + aSzIns[i] - aSzDel[i];
171850 }
171851 a[i+1] = x;
171852 }
171853 fts3EncodeIntArray(nStat, a, pBlob, &nBlob);
171854 rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
171855 if( rc ){
171856 sqlite3_free(a);
171857 *pRC = rc;
171858 return;
171859 }
171860 sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
171861 sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC);
171862 sqlite3_step(pStmt);
171863 *pRC = sqlite3_reset(pStmt);
171864 sqlite3_bind_null(pStmt, 2);
171865 sqlite3_free(a);
171866}
171867
171868/*
171869** Merge the entire database so that there is one segment for each
171870** iIndex/iLangid combination.
171871*/
171872static int fts3DoOptimize(Fts3Table *p, int bReturnDone){
171873 int bSeenDone = 0;
171874 int rc;
171875 sqlite3_stmt *pAllLangid = 0;
171876
171877 rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
171878 if( rc==SQLITE_OK ){
171879 int rc2;
171880 sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
171881 sqlite3_bind_int(pAllLangid, 2, p->nIndex);
171882 while( sqlite3_step(pAllLangid)==SQLITE_ROW ){
171883 int i;
171884 int iLangid = sqlite3_column_int(pAllLangid, 0);
171885 for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
171886 rc = fts3SegmentMerge(p, iLangid, i, FTS3_SEGCURSOR_ALL);
171887 if( rc==SQLITE_DONE ){
171888 bSeenDone = 1;
171889 rc = SQLITE_OK;
171890 }
171891 }
171892 }
171893 rc2 = sqlite3_reset(pAllLangid);
171894 if( rc==SQLITE_OK ) rc = rc2;
171895 }
171896
171897 sqlite3Fts3SegmentsClose(p);
171898 sqlite3Fts3PendingTermsClear(p);
171899
171900 return (rc==SQLITE_OK && bReturnDone && bSeenDone) ? SQLITE_DONE : rc;
171901}
171902
171903/*
171904** This function is called when the user executes the following statement:
171905**
171906** INSERT INTO <tbl>(<tbl>) VALUES('rebuild');
171907**
171908** The entire FTS index is discarded and rebuilt. If the table is one
171909** created using the content=xxx option, then the new index is based on
171910** the current contents of the xxx table. Otherwise, it is rebuilt based
171911** on the contents of the %_content table.
171912*/
171913static int fts3DoRebuild(Fts3Table *p){
171914 int rc; /* Return Code */
171915
171916 rc = fts3DeleteAll(p, 0);
171917 if( rc==SQLITE_OK ){
171918 u32 *aSz = 0;
171919 u32 *aSzIns = 0;
171920 u32 *aSzDel = 0;
171921 sqlite3_stmt *pStmt = 0;
171922 int nEntry = 0;
171923
171924 /* Compose and prepare an SQL statement to loop through the content table */
171925 char *zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
171926 if( !zSql ){
171927 rc = SQLITE_NOMEM;
171928 }else{
171929 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
171930 sqlite3_free(zSql);
171931 }
171932
171933 if( rc==SQLITE_OK ){
171934 int nByte = sizeof(u32) * (p->nColumn+1)*3;
171935 aSz = (u32 *)sqlite3_malloc(nByte);
171936 if( aSz==0 ){
171937 rc = SQLITE_NOMEM;
171938 }else{
171939 memset(aSz, 0, nByte);
171940 aSzIns = &aSz[p->nColumn+1];
171941 aSzDel = &aSzIns[p->nColumn+1];
171942 }
171943 }
171944
171945 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
171946 int iCol;
171947 int iLangid = langidFromSelect(p, pStmt);
171948 rc = fts3PendingTermsDocid(p, 0, iLangid, sqlite3_column_int64(pStmt, 0));
171949 memset(aSz, 0, sizeof(aSz[0]) * (p->nColumn+1));
171950 for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
171951 if( p->abNotindexed[iCol]==0 ){
171952 const char *z = (const char *) sqlite3_column_text(pStmt, iCol+1);
171953 rc = fts3PendingTermsAdd(p, iLangid, z, iCol, &aSz[iCol]);
171954 aSz[p->nColumn] += sqlite3_column_bytes(pStmt, iCol+1);
171955 }
171956 }
171957 if( p->bHasDocsize ){
171958 fts3InsertDocsize(&rc, p, aSz);
171959 }
171960 if( rc!=SQLITE_OK ){
171961 sqlite3_finalize(pStmt);
171962 pStmt = 0;
171963 }else{
171964 nEntry++;
171965 for(iCol=0; iCol<=p->nColumn; iCol++){
171966 aSzIns[iCol] += aSz[iCol];
171967 }
171968 }
171969 }
171970 if( p->bFts4 ){
171971 fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nEntry);
171972 }
171973 sqlite3_free(aSz);
171974
171975 if( pStmt ){
171976 int rc2 = sqlite3_finalize(pStmt);
171977 if( rc==SQLITE_OK ){
171978 rc = rc2;
171979 }
171980 }
171981 }
171982
171983 return rc;
171984}
171985
171986
171987/*
171988** This function opens a cursor used to read the input data for an
171989** incremental merge operation. Specifically, it opens a cursor to scan
171990** the oldest nSeg segments (idx=0 through idx=(nSeg-1)) in absolute
171991** level iAbsLevel.
171992*/
171993static int fts3IncrmergeCsr(
171994 Fts3Table *p, /* FTS3 table handle */
171995 sqlite3_int64 iAbsLevel, /* Absolute level to open */
171996 int nSeg, /* Number of segments to merge */
171997 Fts3MultiSegReader *pCsr /* Cursor object to populate */
171998){
171999 int rc; /* Return Code */
172000 sqlite3_stmt *pStmt = 0; /* Statement used to read %_segdir entry */
172001 int nByte; /* Bytes allocated at pCsr->apSegment[] */
172002
172003 /* Allocate space for the Fts3MultiSegReader.aCsr[] array */
172004 memset(pCsr, 0, sizeof(*pCsr));
172005 nByte = sizeof(Fts3SegReader *) * nSeg;
172006 pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc(nByte);
172007
172008 if( pCsr->apSegment==0 ){
172009 rc = SQLITE_NOMEM;
172010 }else{
172011 memset(pCsr->apSegment, 0, nByte);
172012 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
172013 }
172014 if( rc==SQLITE_OK ){
172015 int i;
172016 int rc2;
172017 sqlite3_bind_int64(pStmt, 1, iAbsLevel);
172018 assert( pCsr->nSegment==0 );
172019 for(i=0; rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW && i<nSeg; i++){
172020 rc = sqlite3Fts3SegReaderNew(i, 0,
172021 sqlite3_column_int64(pStmt, 1), /* segdir.start_block */
172022 sqlite3_column_int64(pStmt, 2), /* segdir.leaves_end_block */
172023 sqlite3_column_int64(pStmt, 3), /* segdir.end_block */
172024 sqlite3_column_blob(pStmt, 4), /* segdir.root */
172025 sqlite3_column_bytes(pStmt, 4), /* segdir.root */
172026 &pCsr->apSegment[i]
172027 );
172028 pCsr->nSegment++;
172029 }
172030 rc2 = sqlite3_reset(pStmt);
172031 if( rc==SQLITE_OK ) rc = rc2;
172032 }
172033
172034 return rc;
172035}
172036
172037typedef struct IncrmergeWriter IncrmergeWriter;
172038typedef struct NodeWriter NodeWriter;
172039typedef struct Blob Blob;
172040typedef struct NodeReader NodeReader;
172041
172042/*
172043** An instance of the following structure is used as a dynamic buffer
172044** to build up nodes or other blobs of data in.
172045**
172046** The function blobGrowBuffer() is used to extend the allocation.
172047*/
172048struct Blob {
172049 char *a; /* Pointer to allocation */
172050 int n; /* Number of valid bytes of data in a[] */
172051 int nAlloc; /* Allocated size of a[] (nAlloc>=n) */
172052};
172053
172054/*
172055** This structure is used to build up buffers containing segment b-tree
172056** nodes (blocks).
172057*/
172058struct NodeWriter {
172059 sqlite3_int64 iBlock; /* Current block id */
172060 Blob key; /* Last key written to the current block */
172061 Blob block; /* Current block image */
172062};
172063
172064/*
172065** An object of this type contains the state required to create or append
172066** to an appendable b-tree segment.
172067*/
172068struct IncrmergeWriter {
172069 int nLeafEst; /* Space allocated for leaf blocks */
172070 int nWork; /* Number of leaf pages flushed */
172071 sqlite3_int64 iAbsLevel; /* Absolute level of input segments */
172072 int iIdx; /* Index of *output* segment in iAbsLevel+1 */
172073 sqlite3_int64 iStart; /* Block number of first allocated block */
172074 sqlite3_int64 iEnd; /* Block number of last allocated block */
172075 sqlite3_int64 nLeafData; /* Bytes of leaf page data so far */
172076 u8 bNoLeafData; /* If true, store 0 for segment size */
172077 NodeWriter aNodeWriter[FTS_MAX_APPENDABLE_HEIGHT];
172078};
172079
172080/*
172081** An object of the following type is used to read data from a single
172082** FTS segment node. See the following functions:
172083**
172084** nodeReaderInit()
172085** nodeReaderNext()
172086** nodeReaderRelease()
172087*/
172088struct NodeReader {
172089 const char *aNode;
172090 int nNode;
172091 int iOff; /* Current offset within aNode[] */
172092
172093 /* Output variables. Containing the current node entry. */
172094 sqlite3_int64 iChild; /* Pointer to child node */
172095 Blob term; /* Current term */
172096 const char *aDoclist; /* Pointer to doclist */
172097 int nDoclist; /* Size of doclist in bytes */
172098};
172099
172100/*
172101** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
172102** Otherwise, if the allocation at pBlob->a is not already at least nMin
172103** bytes in size, extend (realloc) it to be so.
172104**
172105** If an OOM error occurs, set *pRc to SQLITE_NOMEM and leave pBlob->a
172106** unmodified. Otherwise, if the allocation succeeds, update pBlob->nAlloc
172107** to reflect the new size of the pBlob->a[] buffer.
172108*/
172109static void blobGrowBuffer(Blob *pBlob, int nMin, int *pRc){
172110 if( *pRc==SQLITE_OK && nMin>pBlob->nAlloc ){
172111 int nAlloc = nMin;
172112 char *a = (char *)sqlite3_realloc(pBlob->a, nAlloc);
172113 if( a ){
172114 pBlob->nAlloc = nAlloc;
172115 pBlob->a = a;
172116 }else{
172117 *pRc = SQLITE_NOMEM;
172118 }
172119 }
172120}
172121
172122/*
172123** Attempt to advance the node-reader object passed as the first argument to
172124** the next entry on the node.
172125**
172126** Return an error code if an error occurs (SQLITE_NOMEM is possible).
172127** Otherwise return SQLITE_OK. If there is no next entry on the node
172128** (e.g. because the current entry is the last) set NodeReader->aNode to
172129** NULL to indicate EOF. Otherwise, populate the NodeReader structure output
172130** variables for the new entry.
172131*/
172132static int nodeReaderNext(NodeReader *p){
172133 int bFirst = (p->term.n==0); /* True for first term on the node */
172134 int nPrefix = 0; /* Bytes to copy from previous term */
172135 int nSuffix = 0; /* Bytes to append to the prefix */
172136 int rc = SQLITE_OK; /* Return code */
172137
172138 assert( p->aNode );
172139 if( p->iChild && bFirst==0 ) p->iChild++;
172140 if( p->iOff>=p->nNode ){
172141 /* EOF */
172142 p->aNode = 0;
172143 }else{
172144 if( bFirst==0 ){
172145 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nPrefix);
172146 }
172147 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nSuffix);
172148
172149 if( nPrefix>p->iOff || nSuffix>p->nNode-p->iOff ){
172150 return SQLITE_CORRUPT_VTAB;
172151 }
172152 blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc);
172153 if( rc==SQLITE_OK ){
172154 memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix);
172155 p->term.n = nPrefix+nSuffix;
172156 p->iOff += nSuffix;
172157 if( p->iChild==0 ){
172158 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist);
172159 if( (p->nNode-p->iOff)<p->nDoclist ){
172160 return SQLITE_CORRUPT_VTAB;
172161 }
172162 p->aDoclist = &p->aNode[p->iOff];
172163 p->iOff += p->nDoclist;
172164 }
172165 }
172166 }
172167
172168 assert( p->iOff<=p->nNode );
172169 return rc;
172170}
172171
172172/*
172173** Release all dynamic resources held by node-reader object *p.
172174*/
172175static void nodeReaderRelease(NodeReader *p){
172176 sqlite3_free(p->term.a);
172177}
172178
172179/*
172180** Initialize a node-reader object to read the node in buffer aNode/nNode.
172181**
172182** If successful, SQLITE_OK is returned and the NodeReader object set to
172183** point to the first entry on the node (if any). Otherwise, an SQLite
172184** error code is returned.
172185*/
172186static int nodeReaderInit(NodeReader *p, const char *aNode, int nNode){
172187 memset(p, 0, sizeof(NodeReader));
172188 p->aNode = aNode;
172189 p->nNode = nNode;
172190
172191 /* Figure out if this is a leaf or an internal node. */
172192 if( p->aNode[0] ){
172193 /* An internal node. */
172194 p->iOff = 1 + sqlite3Fts3GetVarint(&p->aNode[1], &p->iChild);
172195 }else{
172196 p->iOff = 1;
172197 }
172198
172199 return nodeReaderNext(p);
172200}
172201
172202/*
172203** This function is called while writing an FTS segment each time a leaf o
172204** node is finished and written to disk. The key (zTerm/nTerm) is guaranteed
172205** to be greater than the largest key on the node just written, but smaller
172206** than or equal to the first key that will be written to the next leaf
172207** node.
172208**
172209** The block id of the leaf node just written to disk may be found in
172210** (pWriter->aNodeWriter[0].iBlock) when this function is called.
172211*/
172212static int fts3IncrmergePush(
172213 Fts3Table *p, /* Fts3 table handle */
172214 IncrmergeWriter *pWriter, /* Writer object */
172215 const char *zTerm, /* Term to write to internal node */
172216 int nTerm /* Bytes at zTerm */
172217){
172218 sqlite3_int64 iPtr = pWriter->aNodeWriter[0].iBlock;
172219 int iLayer;
172220
172221 assert( nTerm>0 );
172222 for(iLayer=1; ALWAYS(iLayer<FTS_MAX_APPENDABLE_HEIGHT); iLayer++){
172223 sqlite3_int64 iNextPtr = 0;
172224 NodeWriter *pNode = &pWriter->aNodeWriter[iLayer];
172225 int rc = SQLITE_OK;
172226 int nPrefix;
172227 int nSuffix;
172228 int nSpace;
172229
172230 /* Figure out how much space the key will consume if it is written to
172231 ** the current node of layer iLayer. Due to the prefix compression,
172232 ** the space required changes depending on which node the key is to
172233 ** be added to. */
172234 nPrefix = fts3PrefixCompress(pNode->key.a, pNode->key.n, zTerm, nTerm);
172235 nSuffix = nTerm - nPrefix;
172236 nSpace = sqlite3Fts3VarintLen(nPrefix);
172237 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
172238
172239 if( pNode->key.n==0 || (pNode->block.n + nSpace)<=p->nNodeSize ){
172240 /* If the current node of layer iLayer contains zero keys, or if adding
172241 ** the key to it will not cause it to grow to larger than nNodeSize
172242 ** bytes in size, write the key here. */
172243
172244 Blob *pBlk = &pNode->block;
172245 if( pBlk->n==0 ){
172246 blobGrowBuffer(pBlk, p->nNodeSize, &rc);
172247 if( rc==SQLITE_OK ){
172248 pBlk->a[0] = (char)iLayer;
172249 pBlk->n = 1 + sqlite3Fts3PutVarint(&pBlk->a[1], iPtr);
172250 }
172251 }
172252 blobGrowBuffer(pBlk, pBlk->n + nSpace, &rc);
172253 blobGrowBuffer(&pNode->key, nTerm, &rc);
172254
172255 if( rc==SQLITE_OK ){
172256 if( pNode->key.n ){
172257 pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nPrefix);
172258 }
172259 pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nSuffix);
172260 memcpy(&pBlk->a[pBlk->n], &zTerm[nPrefix], nSuffix);
172261 pBlk->n += nSuffix;
172262
172263 memcpy(pNode->key.a, zTerm, nTerm);
172264 pNode->key.n = nTerm;
172265 }
172266 }else{
172267 /* Otherwise, flush the current node of layer iLayer to disk.
172268 ** Then allocate a new, empty sibling node. The key will be written
172269 ** into the parent of this node. */
172270 rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
172271
172272 assert( pNode->block.nAlloc>=p->nNodeSize );
172273 pNode->block.a[0] = (char)iLayer;
172274 pNode->block.n = 1 + sqlite3Fts3PutVarint(&pNode->block.a[1], iPtr+1);
172275
172276 iNextPtr = pNode->iBlock;
172277 pNode->iBlock++;
172278 pNode->key.n = 0;
172279 }
172280
172281 if( rc!=SQLITE_OK || iNextPtr==0 ) return rc;
172282 iPtr = iNextPtr;
172283 }
172284
172285 assert( 0 );
172286 return 0;
172287}
172288
172289/*
172290** Append a term and (optionally) doclist to the FTS segment node currently
172291** stored in blob *pNode. The node need not contain any terms, but the
172292** header must be written before this function is called.
172293**
172294** A node header is a single 0x00 byte for a leaf node, or a height varint
172295** followed by the left-hand-child varint for an internal node.
172296**
172297** The term to be appended is passed via arguments zTerm/nTerm. For a
172298** leaf node, the doclist is passed as aDoclist/nDoclist. For an internal
172299** node, both aDoclist and nDoclist must be passed 0.
172300**
172301** If the size of the value in blob pPrev is zero, then this is the first
172302** term written to the node. Otherwise, pPrev contains a copy of the
172303** previous term. Before this function returns, it is updated to contain a
172304** copy of zTerm/nTerm.
172305**
172306** It is assumed that the buffer associated with pNode is already large
172307** enough to accommodate the new entry. The buffer associated with pPrev
172308** is extended by this function if requrired.
172309**
172310** If an error (i.e. OOM condition) occurs, an SQLite error code is
172311** returned. Otherwise, SQLITE_OK.
172312*/
172313static int fts3AppendToNode(
172314 Blob *pNode, /* Current node image to append to */
172315 Blob *pPrev, /* Buffer containing previous term written */
172316 const char *zTerm, /* New term to write */
172317 int nTerm, /* Size of zTerm in bytes */
172318 const char *aDoclist, /* Doclist (or NULL) to write */
172319 int nDoclist /* Size of aDoclist in bytes */
172320){
172321 int rc = SQLITE_OK; /* Return code */
172322 int bFirst = (pPrev->n==0); /* True if this is the first term written */
172323 int nPrefix; /* Size of term prefix in bytes */
172324 int nSuffix; /* Size of term suffix in bytes */
172325
172326 /* Node must have already been started. There must be a doclist for a
172327 ** leaf node, and there must not be a doclist for an internal node. */
172328 assert( pNode->n>0 );
172329 assert( (pNode->a[0]=='\0')==(aDoclist!=0) );
172330
172331 blobGrowBuffer(pPrev, nTerm, &rc);
172332 if( rc!=SQLITE_OK ) return rc;
172333
172334 nPrefix = fts3PrefixCompress(pPrev->a, pPrev->n, zTerm, nTerm);
172335 nSuffix = nTerm - nPrefix;
172336 memcpy(pPrev->a, zTerm, nTerm);
172337 pPrev->n = nTerm;
172338
172339 if( bFirst==0 ){
172340 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nPrefix);
172341 }
172342 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nSuffix);
172343 memcpy(&pNode->a[pNode->n], &zTerm[nPrefix], nSuffix);
172344 pNode->n += nSuffix;
172345
172346 if( aDoclist ){
172347 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nDoclist);
172348 memcpy(&pNode->a[pNode->n], aDoclist, nDoclist);
172349 pNode->n += nDoclist;
172350 }
172351
172352 assert( pNode->n<=pNode->nAlloc );
172353
172354 return SQLITE_OK;
172355}
172356
172357/*
172358** Append the current term and doclist pointed to by cursor pCsr to the
172359** appendable b-tree segment opened for writing by pWriter.
172360**
172361** Return SQLITE_OK if successful, or an SQLite error code otherwise.
172362*/
172363static int fts3IncrmergeAppend(
172364 Fts3Table *p, /* Fts3 table handle */
172365 IncrmergeWriter *pWriter, /* Writer object */
172366 Fts3MultiSegReader *pCsr /* Cursor containing term and doclist */
172367){
172368 const char *zTerm = pCsr->zTerm;
172369 int nTerm = pCsr->nTerm;
172370 const char *aDoclist = pCsr->aDoclist;
172371 int nDoclist = pCsr->nDoclist;
172372 int rc = SQLITE_OK; /* Return code */
172373 int nSpace; /* Total space in bytes required on leaf */
172374 int nPrefix; /* Size of prefix shared with previous term */
172375 int nSuffix; /* Size of suffix (nTerm - nPrefix) */
172376 NodeWriter *pLeaf; /* Object used to write leaf nodes */
172377
172378 pLeaf = &pWriter->aNodeWriter[0];
172379 nPrefix = fts3PrefixCompress(pLeaf->key.a, pLeaf->key.n, zTerm, nTerm);
172380 nSuffix = nTerm - nPrefix;
172381
172382 nSpace = sqlite3Fts3VarintLen(nPrefix);
172383 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
172384 nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
172385
172386 /* If the current block is not empty, and if adding this term/doclist
172387 ** to the current block would make it larger than Fts3Table.nNodeSize
172388 ** bytes, write this block out to the database. */
172389 if( pLeaf->block.n>0 && (pLeaf->block.n + nSpace)>p->nNodeSize ){
172390 rc = fts3WriteSegment(p, pLeaf->iBlock, pLeaf->block.a, pLeaf->block.n);
172391 pWriter->nWork++;
172392
172393 /* Add the current term to the parent node. The term added to the
172394 ** parent must:
172395 **
172396 ** a) be greater than the largest term on the leaf node just written
172397 ** to the database (still available in pLeaf->key), and
172398 **
172399 ** b) be less than or equal to the term about to be added to the new
172400 ** leaf node (zTerm/nTerm).
172401 **
172402 ** In other words, it must be the prefix of zTerm 1 byte longer than
172403 ** the common prefix (if any) of zTerm and pWriter->zTerm.
172404 */
172405 if( rc==SQLITE_OK ){
172406 rc = fts3IncrmergePush(p, pWriter, zTerm, nPrefix+1);
172407 }
172408
172409 /* Advance to the next output block */
172410 pLeaf->iBlock++;
172411 pLeaf->key.n = 0;
172412 pLeaf->block.n = 0;
172413
172414 nSuffix = nTerm;
172415 nSpace = 1;
172416 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
172417 nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
172418 }
172419
172420 pWriter->nLeafData += nSpace;
172421 blobGrowBuffer(&pLeaf->block, pLeaf->block.n + nSpace, &rc);
172422 if( rc==SQLITE_OK ){
172423 if( pLeaf->block.n==0 ){
172424 pLeaf->block.n = 1;
172425 pLeaf->block.a[0] = '\0';
172426 }
172427 rc = fts3AppendToNode(
172428 &pLeaf->block, &pLeaf->key, zTerm, nTerm, aDoclist, nDoclist
172429 );
172430 }
172431
172432 return rc;
172433}
172434
172435/*
172436** This function is called to release all dynamic resources held by the
172437** merge-writer object pWriter, and if no error has occurred, to flush
172438** all outstanding node buffers held by pWriter to disk.
172439**
172440** If *pRc is not SQLITE_OK when this function is called, then no attempt
172441** is made to write any data to disk. Instead, this function serves only
172442** to release outstanding resources.
172443**
172444** Otherwise, if *pRc is initially SQLITE_OK and an error occurs while
172445** flushing buffers to disk, *pRc is set to an SQLite error code before
172446** returning.
172447*/
172448static void fts3IncrmergeRelease(
172449 Fts3Table *p, /* FTS3 table handle */
172450 IncrmergeWriter *pWriter, /* Merge-writer object */
172451 int *pRc /* IN/OUT: Error code */
172452){
172453 int i; /* Used to iterate through non-root layers */
172454 int iRoot; /* Index of root in pWriter->aNodeWriter */
172455 NodeWriter *pRoot; /* NodeWriter for root node */
172456 int rc = *pRc; /* Error code */
172457
172458 /* Set iRoot to the index in pWriter->aNodeWriter[] of the output segment
172459 ** root node. If the segment fits entirely on a single leaf node, iRoot
172460 ** will be set to 0. If the root node is the parent of the leaves, iRoot
172461 ** will be 1. And so on. */
172462 for(iRoot=FTS_MAX_APPENDABLE_HEIGHT-1; iRoot>=0; iRoot--){
172463 NodeWriter *pNode = &pWriter->aNodeWriter[iRoot];
172464 if( pNode->block.n>0 ) break;
172465 assert( *pRc || pNode->block.nAlloc==0 );
172466 assert( *pRc || pNode->key.nAlloc==0 );
172467 sqlite3_free(pNode->block.a);
172468 sqlite3_free(pNode->key.a);
172469 }
172470
172471 /* Empty output segment. This is a no-op. */
172472 if( iRoot<0 ) return;
172473
172474 /* The entire output segment fits on a single node. Normally, this means
172475 ** the node would be stored as a blob in the "root" column of the %_segdir
172476 ** table. However, this is not permitted in this case. The problem is that
172477 ** space has already been reserved in the %_segments table, and so the
172478 ** start_block and end_block fields of the %_segdir table must be populated.
172479 ** And, by design or by accident, released versions of FTS cannot handle
172480 ** segments that fit entirely on the root node with start_block!=0.
172481 **
172482 ** Instead, create a synthetic root node that contains nothing but a
172483 ** pointer to the single content node. So that the segment consists of a
172484 ** single leaf and a single interior (root) node.
172485 **
172486 ** Todo: Better might be to defer allocating space in the %_segments
172487 ** table until we are sure it is needed.
172488 */
172489 if( iRoot==0 ){
172490 Blob *pBlock = &pWriter->aNodeWriter[1].block;
172491 blobGrowBuffer(pBlock, 1 + FTS3_VARINT_MAX, &rc);
172492 if( rc==SQLITE_OK ){
172493 pBlock->a[0] = 0x01;
172494 pBlock->n = 1 + sqlite3Fts3PutVarint(
172495 &pBlock->a[1], pWriter->aNodeWriter[0].iBlock
172496 );
172497 }
172498 iRoot = 1;
172499 }
172500 pRoot = &pWriter->aNodeWriter[iRoot];
172501
172502 /* Flush all currently outstanding nodes to disk. */
172503 for(i=0; i<iRoot; i++){
172504 NodeWriter *pNode = &pWriter->aNodeWriter[i];
172505 if( pNode->block.n>0 && rc==SQLITE_OK ){
172506 rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
172507 }
172508 sqlite3_free(pNode->block.a);
172509 sqlite3_free(pNode->key.a);
172510 }
172511
172512 /* Write the %_segdir record. */
172513 if( rc==SQLITE_OK ){
172514 rc = fts3WriteSegdir(p,
172515 pWriter->iAbsLevel+1, /* level */
172516 pWriter->iIdx, /* idx */
172517 pWriter->iStart, /* start_block */
172518 pWriter->aNodeWriter[0].iBlock, /* leaves_end_block */
172519 pWriter->iEnd, /* end_block */
172520 (pWriter->bNoLeafData==0 ? pWriter->nLeafData : 0), /* end_block */
172521 pRoot->block.a, pRoot->block.n /* root */
172522 );
172523 }
172524 sqlite3_free(pRoot->block.a);
172525 sqlite3_free(pRoot->key.a);
172526
172527 *pRc = rc;
172528}
172529
172530/*
172531** Compare the term in buffer zLhs (size in bytes nLhs) with that in
172532** zRhs (size in bytes nRhs) using memcmp. If one term is a prefix of
172533** the other, it is considered to be smaller than the other.
172534**
172535** Return -ve if zLhs is smaller than zRhs, 0 if it is equal, or +ve
172536** if it is greater.
172537*/
172538static int fts3TermCmp(
172539 const char *zLhs, int nLhs, /* LHS of comparison */
172540 const char *zRhs, int nRhs /* RHS of comparison */
172541){
172542 int nCmp = MIN(nLhs, nRhs);
172543 int res;
172544
172545 res = memcmp(zLhs, zRhs, nCmp);
172546 if( res==0 ) res = nLhs - nRhs;
172547
172548 return res;
172549}
172550
172551
172552/*
172553** Query to see if the entry in the %_segments table with blockid iEnd is
172554** NULL. If no error occurs and the entry is NULL, set *pbRes 1 before
172555** returning. Otherwise, set *pbRes to 0.
172556**
172557** Or, if an error occurs while querying the database, return an SQLite
172558** error code. The final value of *pbRes is undefined in this case.
172559**
172560** This is used to test if a segment is an "appendable" segment. If it
172561** is, then a NULL entry has been inserted into the %_segments table
172562** with blockid %_segdir.end_block.
172563*/
172564static int fts3IsAppendable(Fts3Table *p, sqlite3_int64 iEnd, int *pbRes){
172565 int bRes = 0; /* Result to set *pbRes to */
172566 sqlite3_stmt *pCheck = 0; /* Statement to query database with */
172567 int rc; /* Return code */
172568
172569 rc = fts3SqlStmt(p, SQL_SEGMENT_IS_APPENDABLE, &pCheck, 0);
172570 if( rc==SQLITE_OK ){
172571 sqlite3_bind_int64(pCheck, 1, iEnd);
172572 if( SQLITE_ROW==sqlite3_step(pCheck) ) bRes = 1;
172573 rc = sqlite3_reset(pCheck);
172574 }
172575
172576 *pbRes = bRes;
172577 return rc;
172578}
172579
172580/*
172581** This function is called when initializing an incremental-merge operation.
172582** It checks if the existing segment with index value iIdx at absolute level
172583** (iAbsLevel+1) can be appended to by the incremental merge. If it can, the
172584** merge-writer object *pWriter is initialized to write to it.
172585**
172586** An existing segment can be appended to by an incremental merge if:
172587**
172588** * It was initially created as an appendable segment (with all required
172589** space pre-allocated), and
172590**
172591** * The first key read from the input (arguments zKey and nKey) is
172592** greater than the largest key currently stored in the potential
172593** output segment.
172594*/
172595static int fts3IncrmergeLoad(
172596 Fts3Table *p, /* Fts3 table handle */
172597 sqlite3_int64 iAbsLevel, /* Absolute level of input segments */
172598 int iIdx, /* Index of candidate output segment */
172599 const char *zKey, /* First key to write */
172600 int nKey, /* Number of bytes in nKey */
172601 IncrmergeWriter *pWriter /* Populate this object */
172602){
172603 int rc; /* Return code */
172604 sqlite3_stmt *pSelect = 0; /* SELECT to read %_segdir entry */
172605
172606 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pSelect, 0);
172607 if( rc==SQLITE_OK ){
172608 sqlite3_int64 iStart = 0; /* Value of %_segdir.start_block */
172609 sqlite3_int64 iLeafEnd = 0; /* Value of %_segdir.leaves_end_block */
172610 sqlite3_int64 iEnd = 0; /* Value of %_segdir.end_block */
172611 const char *aRoot = 0; /* Pointer to %_segdir.root buffer */
172612 int nRoot = 0; /* Size of aRoot[] in bytes */
172613 int rc2; /* Return code from sqlite3_reset() */
172614 int bAppendable = 0; /* Set to true if segment is appendable */
172615
172616 /* Read the %_segdir entry for index iIdx absolute level (iAbsLevel+1) */
172617 sqlite3_bind_int64(pSelect, 1, iAbsLevel+1);
172618 sqlite3_bind_int(pSelect, 2, iIdx);
172619 if( sqlite3_step(pSelect)==SQLITE_ROW ){
172620 iStart = sqlite3_column_int64(pSelect, 1);
172621 iLeafEnd = sqlite3_column_int64(pSelect, 2);
172622 fts3ReadEndBlockField(pSelect, 3, &iEnd, &pWriter->nLeafData);
172623 if( pWriter->nLeafData<0 ){
172624 pWriter->nLeafData = pWriter->nLeafData * -1;
172625 }
172626 pWriter->bNoLeafData = (pWriter->nLeafData==0);
172627 nRoot = sqlite3_column_bytes(pSelect, 4);
172628 aRoot = sqlite3_column_blob(pSelect, 4);
172629 }else{
172630 return sqlite3_reset(pSelect);
172631 }
172632
172633 /* Check for the zero-length marker in the %_segments table */
172634 rc = fts3IsAppendable(p, iEnd, &bAppendable);
172635
172636 /* Check that zKey/nKey is larger than the largest key the candidate */
172637 if( rc==SQLITE_OK && bAppendable ){
172638 char *aLeaf = 0;
172639 int nLeaf = 0;
172640
172641 rc = sqlite3Fts3ReadBlock(p, iLeafEnd, &aLeaf, &nLeaf, 0);
172642 if( rc==SQLITE_OK ){
172643 NodeReader reader;
172644 for(rc = nodeReaderInit(&reader, aLeaf, nLeaf);
172645 rc==SQLITE_OK && reader.aNode;
172646 rc = nodeReaderNext(&reader)
172647 ){
172648 assert( reader.aNode );
172649 }
172650 if( fts3TermCmp(zKey, nKey, reader.term.a, reader.term.n)<=0 ){
172651 bAppendable = 0;
172652 }
172653 nodeReaderRelease(&reader);
172654 }
172655 sqlite3_free(aLeaf);
172656 }
172657
172658 if( rc==SQLITE_OK && bAppendable ){
172659 /* It is possible to append to this segment. Set up the IncrmergeWriter
172660 ** object to do so. */
172661 int i;
172662 int nHeight = (int)aRoot[0];
172663 NodeWriter *pNode;
172664
172665 pWriter->nLeafEst = (int)((iEnd - iStart) + 1)/FTS_MAX_APPENDABLE_HEIGHT;
172666 pWriter->iStart = iStart;
172667 pWriter->iEnd = iEnd;
172668 pWriter->iAbsLevel = iAbsLevel;
172669 pWriter->iIdx = iIdx;
172670
172671 for(i=nHeight+1; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
172672 pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
172673 }
172674
172675 pNode = &pWriter->aNodeWriter[nHeight];
172676 pNode->iBlock = pWriter->iStart + pWriter->nLeafEst*nHeight;
172677 blobGrowBuffer(&pNode->block, MAX(nRoot, p->nNodeSize), &rc);
172678 if( rc==SQLITE_OK ){
172679 memcpy(pNode->block.a, aRoot, nRoot);
172680 pNode->block.n = nRoot;
172681 }
172682
172683 for(i=nHeight; i>=0 && rc==SQLITE_OK; i--){
172684 NodeReader reader;
172685 pNode = &pWriter->aNodeWriter[i];
172686
172687 rc = nodeReaderInit(&reader, pNode->block.a, pNode->block.n);
172688 while( reader.aNode && rc==SQLITE_OK ) rc = nodeReaderNext(&reader);
172689 blobGrowBuffer(&pNode->key, reader.term.n, &rc);
172690 if( rc==SQLITE_OK ){
172691 memcpy(pNode->key.a, reader.term.a, reader.term.n);
172692 pNode->key.n = reader.term.n;
172693 if( i>0 ){
172694 char *aBlock = 0;
172695 int nBlock = 0;
172696 pNode = &pWriter->aNodeWriter[i-1];
172697 pNode->iBlock = reader.iChild;
172698 rc = sqlite3Fts3ReadBlock(p, reader.iChild, &aBlock, &nBlock, 0);
172699 blobGrowBuffer(&pNode->block, MAX(nBlock, p->nNodeSize), &rc);
172700 if( rc==SQLITE_OK ){
172701 memcpy(pNode->block.a, aBlock, nBlock);
172702 pNode->block.n = nBlock;
172703 }
172704 sqlite3_free(aBlock);
172705 }
172706 }
172707 nodeReaderRelease(&reader);
172708 }
172709 }
172710
172711 rc2 = sqlite3_reset(pSelect);
172712 if( rc==SQLITE_OK ) rc = rc2;
172713 }
172714
172715 return rc;
172716}
172717
172718/*
172719** Determine the largest segment index value that exists within absolute
172720** level iAbsLevel+1. If no error occurs, set *piIdx to this value plus
172721** one before returning SQLITE_OK. Or, if there are no segments at all
172722** within level iAbsLevel, set *piIdx to zero.
172723**
172724** If an error occurs, return an SQLite error code. The final value of
172725** *piIdx is undefined in this case.
172726*/
172727static int fts3IncrmergeOutputIdx(
172728 Fts3Table *p, /* FTS Table handle */
172729 sqlite3_int64 iAbsLevel, /* Absolute index of input segments */
172730 int *piIdx /* OUT: Next free index at iAbsLevel+1 */
172731){
172732 int rc;
172733 sqlite3_stmt *pOutputIdx = 0; /* SQL used to find output index */
172734
172735 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pOutputIdx, 0);
172736 if( rc==SQLITE_OK ){
172737 sqlite3_bind_int64(pOutputIdx, 1, iAbsLevel+1);
172738 sqlite3_step(pOutputIdx);
172739 *piIdx = sqlite3_column_int(pOutputIdx, 0);
172740 rc = sqlite3_reset(pOutputIdx);
172741 }
172742
172743 return rc;
172744}
172745
172746/*
172747** Allocate an appendable output segment on absolute level iAbsLevel+1
172748** with idx value iIdx.
172749**
172750** In the %_segdir table, a segment is defined by the values in three
172751** columns:
172752**
172753** start_block
172754** leaves_end_block
172755** end_block
172756**
172757** When an appendable segment is allocated, it is estimated that the
172758** maximum number of leaf blocks that may be required is the sum of the
172759** number of leaf blocks consumed by the input segments, plus the number
172760** of input segments, multiplied by two. This value is stored in stack
172761** variable nLeafEst.
172762**
172763** A total of 16*nLeafEst blocks are allocated when an appendable segment
172764** is created ((1 + end_block - start_block)==16*nLeafEst). The contiguous
172765** array of leaf nodes starts at the first block allocated. The array
172766** of interior nodes that are parents of the leaf nodes start at block
172767** (start_block + (1 + end_block - start_block) / 16). And so on.
172768**
172769** In the actual code below, the value "16" is replaced with the
172770** pre-processor macro FTS_MAX_APPENDABLE_HEIGHT.
172771*/
172772static int fts3IncrmergeWriter(
172773 Fts3Table *p, /* Fts3 table handle */
172774 sqlite3_int64 iAbsLevel, /* Absolute level of input segments */
172775 int iIdx, /* Index of new output segment */
172776 Fts3MultiSegReader *pCsr, /* Cursor that data will be read from */
172777 IncrmergeWriter *pWriter /* Populate this object */
172778){
172779 int rc; /* Return Code */
172780 int i; /* Iterator variable */
172781 int nLeafEst = 0; /* Blocks allocated for leaf nodes */
172782 sqlite3_stmt *pLeafEst = 0; /* SQL used to determine nLeafEst */
172783 sqlite3_stmt *pFirstBlock = 0; /* SQL used to determine first block */
172784
172785 /* Calculate nLeafEst. */
172786 rc = fts3SqlStmt(p, SQL_MAX_LEAF_NODE_ESTIMATE, &pLeafEst, 0);
172787 if( rc==SQLITE_OK ){
172788 sqlite3_bind_int64(pLeafEst, 1, iAbsLevel);
172789 sqlite3_bind_int64(pLeafEst, 2, pCsr->nSegment);
172790 if( SQLITE_ROW==sqlite3_step(pLeafEst) ){
172791 nLeafEst = sqlite3_column_int(pLeafEst, 0);
172792 }
172793 rc = sqlite3_reset(pLeafEst);
172794 }
172795 if( rc!=SQLITE_OK ) return rc;
172796
172797 /* Calculate the first block to use in the output segment */
172798 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pFirstBlock, 0);
172799 if( rc==SQLITE_OK ){
172800 if( SQLITE_ROW==sqlite3_step(pFirstBlock) ){
172801 pWriter->iStart = sqlite3_column_int64(pFirstBlock, 0);
172802 pWriter->iEnd = pWriter->iStart - 1;
172803 pWriter->iEnd += nLeafEst * FTS_MAX_APPENDABLE_HEIGHT;
172804 }
172805 rc = sqlite3_reset(pFirstBlock);
172806 }
172807 if( rc!=SQLITE_OK ) return rc;
172808
172809 /* Insert the marker in the %_segments table to make sure nobody tries
172810 ** to steal the space just allocated. This is also used to identify
172811 ** appendable segments. */
172812 rc = fts3WriteSegment(p, pWriter->iEnd, 0, 0);
172813 if( rc!=SQLITE_OK ) return rc;
172814
172815 pWriter->iAbsLevel = iAbsLevel;
172816 pWriter->nLeafEst = nLeafEst;
172817 pWriter->iIdx = iIdx;
172818
172819 /* Set up the array of NodeWriter objects */
172820 for(i=0; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
172821 pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
172822 }
172823 return SQLITE_OK;
172824}
172825
172826/*
172827** Remove an entry from the %_segdir table. This involves running the
172828** following two statements:
172829**
172830** DELETE FROM %_segdir WHERE level = :iAbsLevel AND idx = :iIdx
172831** UPDATE %_segdir SET idx = idx - 1 WHERE level = :iAbsLevel AND idx > :iIdx
172832**
172833** The DELETE statement removes the specific %_segdir level. The UPDATE
172834** statement ensures that the remaining segments have contiguously allocated
172835** idx values.
172836*/
172837static int fts3RemoveSegdirEntry(
172838 Fts3Table *p, /* FTS3 table handle */
172839 sqlite3_int64 iAbsLevel, /* Absolute level to delete from */
172840 int iIdx /* Index of %_segdir entry to delete */
172841){
172842 int rc; /* Return code */
172843 sqlite3_stmt *pDelete = 0; /* DELETE statement */
172844
172845 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_ENTRY, &pDelete, 0);
172846 if( rc==SQLITE_OK ){
172847 sqlite3_bind_int64(pDelete, 1, iAbsLevel);
172848 sqlite3_bind_int(pDelete, 2, iIdx);
172849 sqlite3_step(pDelete);
172850 rc = sqlite3_reset(pDelete);
172851 }
172852
172853 return rc;
172854}
172855
172856/*
172857** One or more segments have just been removed from absolute level iAbsLevel.
172858** Update the 'idx' values of the remaining segments in the level so that
172859** the idx values are a contiguous sequence starting from 0.
172860*/
172861static int fts3RepackSegdirLevel(
172862 Fts3Table *p, /* FTS3 table handle */
172863 sqlite3_int64 iAbsLevel /* Absolute level to repack */
172864){
172865 int rc; /* Return code */
172866 int *aIdx = 0; /* Array of remaining idx values */
172867 int nIdx = 0; /* Valid entries in aIdx[] */
172868 int nAlloc = 0; /* Allocated size of aIdx[] */
172869 int i; /* Iterator variable */
172870 sqlite3_stmt *pSelect = 0; /* Select statement to read idx values */
172871 sqlite3_stmt *pUpdate = 0; /* Update statement to modify idx values */
172872
172873 rc = fts3SqlStmt(p, SQL_SELECT_INDEXES, &pSelect, 0);
172874 if( rc==SQLITE_OK ){
172875 int rc2;
172876 sqlite3_bind_int64(pSelect, 1, iAbsLevel);
172877 while( SQLITE_ROW==sqlite3_step(pSelect) ){
172878 if( nIdx>=nAlloc ){
172879 int *aNew;
172880 nAlloc += 16;
172881 aNew = sqlite3_realloc(aIdx, nAlloc*sizeof(int));
172882 if( !aNew ){
172883 rc = SQLITE_NOMEM;
172884 break;
172885 }
172886 aIdx = aNew;
172887 }
172888 aIdx[nIdx++] = sqlite3_column_int(pSelect, 0);
172889 }
172890 rc2 = sqlite3_reset(pSelect);
172891 if( rc==SQLITE_OK ) rc = rc2;
172892 }
172893
172894 if( rc==SQLITE_OK ){
172895 rc = fts3SqlStmt(p, SQL_SHIFT_SEGDIR_ENTRY, &pUpdate, 0);
172896 }
172897 if( rc==SQLITE_OK ){
172898 sqlite3_bind_int64(pUpdate, 2, iAbsLevel);
172899 }
172900
172901 assert( p->bIgnoreSavepoint==0 );
172902 p->bIgnoreSavepoint = 1;
172903 for(i=0; rc==SQLITE_OK && i<nIdx; i++){
172904 if( aIdx[i]!=i ){
172905 sqlite3_bind_int(pUpdate, 3, aIdx[i]);
172906 sqlite3_bind_int(pUpdate, 1, i);
172907 sqlite3_step(pUpdate);
172908 rc = sqlite3_reset(pUpdate);
172909 }
172910 }
172911 p->bIgnoreSavepoint = 0;
172912
172913 sqlite3_free(aIdx);
172914 return rc;
172915}
172916
172917static void fts3StartNode(Blob *pNode, int iHeight, sqlite3_int64 iChild){
172918 pNode->a[0] = (char)iHeight;
172919 if( iChild ){
172920 assert( pNode->nAlloc>=1+sqlite3Fts3VarintLen(iChild) );
172921 pNode->n = 1 + sqlite3Fts3PutVarint(&pNode->a[1], iChild);
172922 }else{
172923 assert( pNode->nAlloc>=1 );
172924 pNode->n = 1;
172925 }
172926}
172927
172928/*
172929** The first two arguments are a pointer to and the size of a segment b-tree
172930** node. The node may be a leaf or an internal node.
172931**
172932** This function creates a new node image in blob object *pNew by copying
172933** all terms that are greater than or equal to zTerm/nTerm (for leaf nodes)
172934** or greater than zTerm/nTerm (for internal nodes) from aNode/nNode.
172935*/
172936static int fts3TruncateNode(
172937 const char *aNode, /* Current node image */
172938 int nNode, /* Size of aNode in bytes */
172939 Blob *pNew, /* OUT: Write new node image here */
172940 const char *zTerm, /* Omit all terms smaller than this */
172941 int nTerm, /* Size of zTerm in bytes */
172942 sqlite3_int64 *piBlock /* OUT: Block number in next layer down */
172943){
172944 NodeReader reader; /* Reader object */
172945 Blob prev = {0, 0, 0}; /* Previous term written to new node */
172946 int rc = SQLITE_OK; /* Return code */
172947 int bLeaf = aNode[0]=='\0'; /* True for a leaf node */
172948
172949 /* Allocate required output space */
172950 blobGrowBuffer(pNew, nNode, &rc);
172951 if( rc!=SQLITE_OK ) return rc;
172952 pNew->n = 0;
172953
172954 /* Populate new node buffer */
172955 for(rc = nodeReaderInit(&reader, aNode, nNode);
172956 rc==SQLITE_OK && reader.aNode;
172957 rc = nodeReaderNext(&reader)
172958 ){
172959 if( pNew->n==0 ){
172960 int res = fts3TermCmp(reader.term.a, reader.term.n, zTerm, nTerm);
172961 if( res<0 || (bLeaf==0 && res==0) ) continue;
172962 fts3StartNode(pNew, (int)aNode[0], reader.iChild);
172963 *piBlock = reader.iChild;
172964 }
172965 rc = fts3AppendToNode(
172966 pNew, &prev, reader.term.a, reader.term.n,
172967 reader.aDoclist, reader.nDoclist
172968 );
172969 if( rc!=SQLITE_OK ) break;
172970 }
172971 if( pNew->n==0 ){
172972 fts3StartNode(pNew, (int)aNode[0], reader.iChild);
172973 *piBlock = reader.iChild;
172974 }
172975 assert( pNew->n<=pNew->nAlloc );
172976
172977 nodeReaderRelease(&reader);
172978 sqlite3_free(prev.a);
172979 return rc;
172980}
172981
172982/*
172983** Remove all terms smaller than zTerm/nTerm from segment iIdx in absolute
172984** level iAbsLevel. This may involve deleting entries from the %_segments
172985** table, and modifying existing entries in both the %_segments and %_segdir
172986** tables.
172987**
172988** SQLITE_OK is returned if the segment is updated successfully. Or an
172989** SQLite error code otherwise.
172990*/
172991static int fts3TruncateSegment(
172992 Fts3Table *p, /* FTS3 table handle */
172993 sqlite3_int64 iAbsLevel, /* Absolute level of segment to modify */
172994 int iIdx, /* Index within level of segment to modify */
172995 const char *zTerm, /* Remove terms smaller than this */
172996 int nTerm /* Number of bytes in buffer zTerm */
172997){
172998 int rc = SQLITE_OK; /* Return code */
172999 Blob root = {0,0,0}; /* New root page image */
173000 Blob block = {0,0,0}; /* Buffer used for any other block */
173001 sqlite3_int64 iBlock = 0; /* Block id */
173002 sqlite3_int64 iNewStart = 0; /* New value for iStartBlock */
173003 sqlite3_int64 iOldStart = 0; /* Old value for iStartBlock */
173004 sqlite3_stmt *pFetch = 0; /* Statement used to fetch segdir */
173005
173006 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pFetch, 0);
173007 if( rc==SQLITE_OK ){
173008 int rc2; /* sqlite3_reset() return code */
173009 sqlite3_bind_int64(pFetch, 1, iAbsLevel);
173010 sqlite3_bind_int(pFetch, 2, iIdx);
173011 if( SQLITE_ROW==sqlite3_step(pFetch) ){
173012 const char *aRoot = sqlite3_column_blob(pFetch, 4);
173013 int nRoot = sqlite3_column_bytes(pFetch, 4);
173014 iOldStart = sqlite3_column_int64(pFetch, 1);
173015 rc = fts3TruncateNode(aRoot, nRoot, &root, zTerm, nTerm, &iBlock);
173016 }
173017 rc2 = sqlite3_reset(pFetch);
173018 if( rc==SQLITE_OK ) rc = rc2;
173019 }
173020
173021 while( rc==SQLITE_OK && iBlock ){
173022 char *aBlock = 0;
173023 int nBlock = 0;
173024 iNewStart = iBlock;
173025
173026 rc = sqlite3Fts3ReadBlock(p, iBlock, &aBlock, &nBlock, 0);
173027 if( rc==SQLITE_OK ){
173028 rc = fts3TruncateNode(aBlock, nBlock, &block, zTerm, nTerm, &iBlock);
173029 }
173030 if( rc==SQLITE_OK ){
173031 rc = fts3WriteSegment(p, iNewStart, block.a, block.n);
173032 }
173033 sqlite3_free(aBlock);
173034 }
173035
173036 /* Variable iNewStart now contains the first valid leaf node. */
173037 if( rc==SQLITE_OK && iNewStart ){
173038 sqlite3_stmt *pDel = 0;
173039 rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDel, 0);
173040 if( rc==SQLITE_OK ){
173041 sqlite3_bind_int64(pDel, 1, iOldStart);
173042 sqlite3_bind_int64(pDel, 2, iNewStart-1);
173043 sqlite3_step(pDel);
173044 rc = sqlite3_reset(pDel);
173045 }
173046 }
173047
173048 if( rc==SQLITE_OK ){
173049 sqlite3_stmt *pChomp = 0;
173050 rc = fts3SqlStmt(p, SQL_CHOMP_SEGDIR, &pChomp, 0);
173051 if( rc==SQLITE_OK ){
173052 sqlite3_bind_int64(pChomp, 1, iNewStart);
173053 sqlite3_bind_blob(pChomp, 2, root.a, root.n, SQLITE_STATIC);
173054 sqlite3_bind_int64(pChomp, 3, iAbsLevel);
173055 sqlite3_bind_int(pChomp, 4, iIdx);
173056 sqlite3_step(pChomp);
173057 rc = sqlite3_reset(pChomp);
173058 sqlite3_bind_null(pChomp, 2);
173059 }
173060 }
173061
173062 sqlite3_free(root.a);
173063 sqlite3_free(block.a);
173064 return rc;
173065}
173066
173067/*
173068** This function is called after an incrmental-merge operation has run to
173069** merge (or partially merge) two or more segments from absolute level
173070** iAbsLevel.
173071**
173072** Each input segment is either removed from the db completely (if all of
173073** its data was copied to the output segment by the incrmerge operation)
173074** or modified in place so that it no longer contains those entries that
173075** have been duplicated in the output segment.
173076*/
173077static int fts3IncrmergeChomp(
173078 Fts3Table *p, /* FTS table handle */
173079 sqlite3_int64 iAbsLevel, /* Absolute level containing segments */
173080 Fts3MultiSegReader *pCsr, /* Chomp all segments opened by this cursor */
173081 int *pnRem /* Number of segments not deleted */
173082){
173083 int i;
173084 int nRem = 0;
173085 int rc = SQLITE_OK;
173086
173087 for(i=pCsr->nSegment-1; i>=0 && rc==SQLITE_OK; i--){
173088 Fts3SegReader *pSeg = 0;
173089 int j;
173090
173091 /* Find the Fts3SegReader object with Fts3SegReader.iIdx==i. It is hiding
173092 ** somewhere in the pCsr->apSegment[] array. */
173093 for(j=0; ALWAYS(j<pCsr->nSegment); j++){
173094 pSeg = pCsr->apSegment[j];
173095 if( pSeg->iIdx==i ) break;
173096 }
173097 assert( j<pCsr->nSegment && pSeg->iIdx==i );
173098
173099 if( pSeg->aNode==0 ){
173100 /* Seg-reader is at EOF. Remove the entire input segment. */
173101 rc = fts3DeleteSegment(p, pSeg);
173102 if( rc==SQLITE_OK ){
173103 rc = fts3RemoveSegdirEntry(p, iAbsLevel, pSeg->iIdx);
173104 }
173105 *pnRem = 0;
173106 }else{
173107 /* The incremental merge did not copy all the data from this
173108 ** segment to the upper level. The segment is modified in place
173109 ** so that it contains no keys smaller than zTerm/nTerm. */
173110 const char *zTerm = pSeg->zTerm;
173111 int nTerm = pSeg->nTerm;
173112 rc = fts3TruncateSegment(p, iAbsLevel, pSeg->iIdx, zTerm, nTerm);
173113 nRem++;
173114 }
173115 }
173116
173117 if( rc==SQLITE_OK && nRem!=pCsr->nSegment ){
173118 rc = fts3RepackSegdirLevel(p, iAbsLevel);
173119 }
173120
173121 *pnRem = nRem;
173122 return rc;
173123}
173124
173125/*
173126** Store an incr-merge hint in the database.
173127*/
173128static int fts3IncrmergeHintStore(Fts3Table *p, Blob *pHint){
173129 sqlite3_stmt *pReplace = 0;
173130 int rc; /* Return code */
173131
173132 rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pReplace, 0);
173133 if( rc==SQLITE_OK ){
173134 sqlite3_bind_int(pReplace, 1, FTS_STAT_INCRMERGEHINT);
173135 sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC);
173136 sqlite3_step(pReplace);
173137 rc = sqlite3_reset(pReplace);
173138 sqlite3_bind_null(pReplace, 2);
173139 }
173140
173141 return rc;
173142}
173143
173144/*
173145** Load an incr-merge hint from the database. The incr-merge hint, if one
173146** exists, is stored in the rowid==1 row of the %_stat table.
173147**
173148** If successful, populate blob *pHint with the value read from the %_stat
173149** table and return SQLITE_OK. Otherwise, if an error occurs, return an
173150** SQLite error code.
173151*/
173152static int fts3IncrmergeHintLoad(Fts3Table *p, Blob *pHint){
173153 sqlite3_stmt *pSelect = 0;
173154 int rc;
173155
173156 pHint->n = 0;
173157 rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pSelect, 0);
173158 if( rc==SQLITE_OK ){
173159 int rc2;
173160 sqlite3_bind_int(pSelect, 1, FTS_STAT_INCRMERGEHINT);
173161 if( SQLITE_ROW==sqlite3_step(pSelect) ){
173162 const char *aHint = sqlite3_column_blob(pSelect, 0);
173163 int nHint = sqlite3_column_bytes(pSelect, 0);
173164 if( aHint ){
173165 blobGrowBuffer(pHint, nHint, &rc);
173166 if( rc==SQLITE_OK ){
173167 memcpy(pHint->a, aHint, nHint);
173168 pHint->n = nHint;
173169 }
173170 }
173171 }
173172 rc2 = sqlite3_reset(pSelect);
173173 if( rc==SQLITE_OK ) rc = rc2;
173174 }
173175
173176 return rc;
173177}
173178
173179/*
173180** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
173181** Otherwise, append an entry to the hint stored in blob *pHint. Each entry
173182** consists of two varints, the absolute level number of the input segments
173183** and the number of input segments.
173184**
173185** If successful, leave *pRc set to SQLITE_OK and return. If an error occurs,
173186** set *pRc to an SQLite error code before returning.
173187*/
173188static void fts3IncrmergeHintPush(
173189 Blob *pHint, /* Hint blob to append to */
173190 i64 iAbsLevel, /* First varint to store in hint */
173191 int nInput, /* Second varint to store in hint */
173192 int *pRc /* IN/OUT: Error code */
173193){
173194 blobGrowBuffer(pHint, pHint->n + 2*FTS3_VARINT_MAX, pRc);
173195 if( *pRc==SQLITE_OK ){
173196 pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], iAbsLevel);
173197 pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], (i64)nInput);
173198 }
173199}
173200
173201/*
173202** Read the last entry (most recently pushed) from the hint blob *pHint
173203** and then remove the entry. Write the two values read to *piAbsLevel and
173204** *pnInput before returning.
173205**
173206** If no error occurs, return SQLITE_OK. If the hint blob in *pHint does
173207** not contain at least two valid varints, return SQLITE_CORRUPT_VTAB.
173208*/
173209static int fts3IncrmergeHintPop(Blob *pHint, i64 *piAbsLevel, int *pnInput){
173210 const int nHint = pHint->n;
173211 int i;
173212
173213 i = pHint->n-2;
173214 while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
173215 while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
173216
173217 pHint->n = i;
173218 i += sqlite3Fts3GetVarint(&pHint->a[i], piAbsLevel);
173219 i += fts3GetVarint32(&pHint->a[i], pnInput);
173220 if( i!=nHint ) return FTS_CORRUPT_VTAB;
173221
173222 return SQLITE_OK;
173223}
173224
173225
173226/*
173227** Attempt an incremental merge that writes nMerge leaf blocks.
173228**
173229** Incremental merges happen nMin segments at a time. The segments
173230** to be merged are the nMin oldest segments (the ones with the smallest
173231** values for the _segdir.idx field) in the highest level that contains
173232** at least nMin segments. Multiple merges might occur in an attempt to
173233** write the quota of nMerge leaf blocks.
173234*/
173235SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
173236 int rc; /* Return code */
173237 int nRem = nMerge; /* Number of leaf pages yet to be written */
173238 Fts3MultiSegReader *pCsr; /* Cursor used to read input data */
173239 Fts3SegFilter *pFilter; /* Filter used with cursor pCsr */
173240 IncrmergeWriter *pWriter; /* Writer object */
173241 int nSeg = 0; /* Number of input segments */
173242 sqlite3_int64 iAbsLevel = 0; /* Absolute level number to work on */
173243 Blob hint = {0, 0, 0}; /* Hint read from %_stat table */
173244 int bDirtyHint = 0; /* True if blob 'hint' has been modified */
173245
173246 /* Allocate space for the cursor, filter and writer objects */
173247 const int nAlloc = sizeof(*pCsr) + sizeof(*pFilter) + sizeof(*pWriter);
173248 pWriter = (IncrmergeWriter *)sqlite3_malloc(nAlloc);
173249 if( !pWriter ) return SQLITE_NOMEM;
173250 pFilter = (Fts3SegFilter *)&pWriter[1];
173251 pCsr = (Fts3MultiSegReader *)&pFilter[1];
173252
173253 rc = fts3IncrmergeHintLoad(p, &hint);
173254 while( rc==SQLITE_OK && nRem>0 ){
173255 const i64 nMod = FTS3_SEGDIR_MAXLEVEL * p->nIndex;
173256 sqlite3_stmt *pFindLevel = 0; /* SQL used to determine iAbsLevel */
173257 int bUseHint = 0; /* True if attempting to append */
173258 int iIdx = 0; /* Largest idx in level (iAbsLevel+1) */
173259
173260 /* Search the %_segdir table for the absolute level with the smallest
173261 ** relative level number that contains at least nMin segments, if any.
173262 ** If one is found, set iAbsLevel to the absolute level number and
173263 ** nSeg to nMin. If no level with at least nMin segments can be found,
173264 ** set nSeg to -1.
173265 */
173266 rc = fts3SqlStmt(p, SQL_FIND_MERGE_LEVEL, &pFindLevel, 0);
173267 sqlite3_bind_int(pFindLevel, 1, MAX(2, nMin));
173268 if( sqlite3_step(pFindLevel)==SQLITE_ROW ){
173269 iAbsLevel = sqlite3_column_int64(pFindLevel, 0);
173270 nSeg = sqlite3_column_int(pFindLevel, 1);
173271 assert( nSeg>=2 );
173272 }else{
173273 nSeg = -1;
173274 }
173275 rc = sqlite3_reset(pFindLevel);
173276
173277 /* If the hint read from the %_stat table is not empty, check if the
173278 ** last entry in it specifies a relative level smaller than or equal
173279 ** to the level identified by the block above (if any). If so, this
173280 ** iteration of the loop will work on merging at the hinted level.
173281 */
173282 if( rc==SQLITE_OK && hint.n ){
173283 int nHint = hint.n;
173284 sqlite3_int64 iHintAbsLevel = 0; /* Hint level */
173285 int nHintSeg = 0; /* Hint number of segments */
173286
173287 rc = fts3IncrmergeHintPop(&hint, &iHintAbsLevel, &nHintSeg);
173288 if( nSeg<0 || (iAbsLevel % nMod) >= (iHintAbsLevel % nMod) ){
173289 iAbsLevel = iHintAbsLevel;
173290 nSeg = nHintSeg;
173291 bUseHint = 1;
173292 bDirtyHint = 1;
173293 }else{
173294 /* This undoes the effect of the HintPop() above - so that no entry
173295 ** is removed from the hint blob. */
173296 hint.n = nHint;
173297 }
173298 }
173299
173300 /* If nSeg is less that zero, then there is no level with at least
173301 ** nMin segments and no hint in the %_stat table. No work to do.
173302 ** Exit early in this case. */
173303 if( nSeg<0 ) break;
173304
173305 /* Open a cursor to iterate through the contents of the oldest nSeg
173306 ** indexes of absolute level iAbsLevel. If this cursor is opened using
173307 ** the 'hint' parameters, it is possible that there are less than nSeg
173308 ** segments available in level iAbsLevel. In this case, no work is
173309 ** done on iAbsLevel - fall through to the next iteration of the loop
173310 ** to start work on some other level. */
173311 memset(pWriter, 0, nAlloc);
173312 pFilter->flags = FTS3_SEGMENT_REQUIRE_POS;
173313
173314 if( rc==SQLITE_OK ){
173315 rc = fts3IncrmergeOutputIdx(p, iAbsLevel, &iIdx);
173316 assert( bUseHint==1 || bUseHint==0 );
173317 if( iIdx==0 || (bUseHint && iIdx==1) ){
173318 int bIgnore = 0;
173319 rc = fts3SegmentIsMaxLevel(p, iAbsLevel+1, &bIgnore);
173320 if( bIgnore ){
173321 pFilter->flags |= FTS3_SEGMENT_IGNORE_EMPTY;
173322 }
173323 }
173324 }
173325
173326 if( rc==SQLITE_OK ){
173327 rc = fts3IncrmergeCsr(p, iAbsLevel, nSeg, pCsr);
173328 }
173329 if( SQLITE_OK==rc && pCsr->nSegment==nSeg
173330 && SQLITE_OK==(rc = sqlite3Fts3SegReaderStart(p, pCsr, pFilter))
173331 && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pCsr))
173332 ){
173333 if( bUseHint && iIdx>0 ){
173334 const char *zKey = pCsr->zTerm;
173335 int nKey = pCsr->nTerm;
173336 rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx-1, zKey, nKey, pWriter);
173337 }else{
173338 rc = fts3IncrmergeWriter(p, iAbsLevel, iIdx, pCsr, pWriter);
173339 }
173340
173341 if( rc==SQLITE_OK && pWriter->nLeafEst ){
173342 fts3LogMerge(nSeg, iAbsLevel);
173343 do {
173344 rc = fts3IncrmergeAppend(p, pWriter, pCsr);
173345 if( rc==SQLITE_OK ) rc = sqlite3Fts3SegReaderStep(p, pCsr);
173346 if( pWriter->nWork>=nRem && rc==SQLITE_ROW ) rc = SQLITE_OK;
173347 }while( rc==SQLITE_ROW );
173348
173349 /* Update or delete the input segments */
173350 if( rc==SQLITE_OK ){
173351 nRem -= (1 + pWriter->nWork);
173352 rc = fts3IncrmergeChomp(p, iAbsLevel, pCsr, &nSeg);
173353 if( nSeg!=0 ){
173354 bDirtyHint = 1;
173355 fts3IncrmergeHintPush(&hint, iAbsLevel, nSeg, &rc);
173356 }
173357 }
173358 }
173359
173360 if( nSeg!=0 ){
173361 pWriter->nLeafData = pWriter->nLeafData * -1;
173362 }
173363 fts3IncrmergeRelease(p, pWriter, &rc);
173364 if( nSeg==0 && pWriter->bNoLeafData==0 ){
173365 fts3PromoteSegments(p, iAbsLevel+1, pWriter->nLeafData);
173366 }
173367 }
173368
173369 sqlite3Fts3SegReaderFinish(pCsr);
173370 }
173371
173372 /* Write the hint values into the %_stat table for the next incr-merger */
173373 if( bDirtyHint && rc==SQLITE_OK ){
173374 rc = fts3IncrmergeHintStore(p, &hint);
173375 }
173376
173377 sqlite3_free(pWriter);
173378 sqlite3_free(hint.a);
173379 return rc;
173380}
173381
173382/*
173383** Convert the text beginning at *pz into an integer and return
173384** its value. Advance *pz to point to the first character past
173385** the integer.
173386**
173387** This function used for parameters to merge= and incrmerge=
173388** commands.
173389*/
173390static int fts3Getint(const char **pz){
173391 const char *z = *pz;
173392 int i = 0;
173393 while( (*z)>='0' && (*z)<='9' && i<214748363 ) i = 10*i + *(z++) - '0';
173394 *pz = z;
173395 return i;
173396}
173397
173398/*
173399** Process statements of the form:
173400**
173401** INSERT INTO table(table) VALUES('merge=A,B');
173402**
173403** A and B are integers that decode to be the number of leaf pages
173404** written for the merge, and the minimum number of segments on a level
173405** before it will be selected for a merge, respectively.
173406*/
173407static int fts3DoIncrmerge(
173408 Fts3Table *p, /* FTS3 table handle */
173409 const char *zParam /* Nul-terminated string containing "A,B" */
173410){
173411 int rc;
173412 int nMin = (FTS3_MERGE_COUNT / 2);
173413 int nMerge = 0;
173414 const char *z = zParam;
173415
173416 /* Read the first integer value */
173417 nMerge = fts3Getint(&z);
173418
173419 /* If the first integer value is followed by a ',', read the second
173420 ** integer value. */
173421 if( z[0]==',' && z[1]!='\0' ){
173422 z++;
173423 nMin = fts3Getint(&z);
173424 }
173425
173426 if( z[0]!='\0' || nMin<2 ){
173427 rc = SQLITE_ERROR;
173428 }else{
173429 rc = SQLITE_OK;
173430 if( !p->bHasStat ){
173431 assert( p->bFts4==0 );
173432 sqlite3Fts3CreateStatTable(&rc, p);
173433 }
173434 if( rc==SQLITE_OK ){
173435 rc = sqlite3Fts3Incrmerge(p, nMerge, nMin);
173436 }
173437 sqlite3Fts3SegmentsClose(p);
173438 }
173439 return rc;
173440}
173441
173442/*
173443** Process statements of the form:
173444**
173445** INSERT INTO table(table) VALUES('automerge=X');
173446**
173447** where X is an integer. X==0 means to turn automerge off. X!=0 means
173448** turn it on. The setting is persistent.
173449*/
173450static int fts3DoAutoincrmerge(
173451 Fts3Table *p, /* FTS3 table handle */
173452 const char *zParam /* Nul-terminated string containing boolean */
173453){
173454 int rc = SQLITE_OK;
173455 sqlite3_stmt *pStmt = 0;
173456 p->nAutoincrmerge = fts3Getint(&zParam);
173457 if( p->nAutoincrmerge==1 || p->nAutoincrmerge>FTS3_MERGE_COUNT ){
173458 p->nAutoincrmerge = 8;
173459 }
173460 if( !p->bHasStat ){
173461 assert( p->bFts4==0 );
173462 sqlite3Fts3CreateStatTable(&rc, p);
173463 if( rc ) return rc;
173464 }
173465 rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
173466 if( rc ) return rc;
173467 sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
173468 sqlite3_bind_int(pStmt, 2, p->nAutoincrmerge);
173469 sqlite3_step(pStmt);
173470 rc = sqlite3_reset(pStmt);
173471 return rc;
173472}
173473
173474/*
173475** Return a 64-bit checksum for the FTS index entry specified by the
173476** arguments to this function.
173477*/
173478static u64 fts3ChecksumEntry(
173479 const char *zTerm, /* Pointer to buffer containing term */
173480 int nTerm, /* Size of zTerm in bytes */
173481 int iLangid, /* Language id for current row */
173482 int iIndex, /* Index (0..Fts3Table.nIndex-1) */
173483 i64 iDocid, /* Docid for current row. */
173484 int iCol, /* Column number */
173485 int iPos /* Position */
173486){
173487 int i;
173488 u64 ret = (u64)iDocid;
173489
173490 ret += (ret<<3) + iLangid;
173491 ret += (ret<<3) + iIndex;
173492 ret += (ret<<3) + iCol;
173493 ret += (ret<<3) + iPos;
173494 for(i=0; i<nTerm; i++) ret += (ret<<3) + zTerm[i];
173495
173496 return ret;
173497}
173498
173499/*
173500** Return a checksum of all entries in the FTS index that correspond to
173501** language id iLangid. The checksum is calculated by XORing the checksums
173502** of each individual entry (see fts3ChecksumEntry()) together.
173503**
173504** If successful, the checksum value is returned and *pRc set to SQLITE_OK.
173505** Otherwise, if an error occurs, *pRc is set to an SQLite error code. The
173506** return value is undefined in this case.
173507*/
173508static u64 fts3ChecksumIndex(
173509 Fts3Table *p, /* FTS3 table handle */
173510 int iLangid, /* Language id to return cksum for */
173511 int iIndex, /* Index to cksum (0..p->nIndex-1) */
173512 int *pRc /* OUT: Return code */
173513){
173514 Fts3SegFilter filter;
173515 Fts3MultiSegReader csr;
173516 int rc;
173517 u64 cksum = 0;
173518
173519 assert( *pRc==SQLITE_OK );
173520
173521 memset(&filter, 0, sizeof(filter));
173522 memset(&csr, 0, sizeof(csr));
173523 filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
173524 filter.flags |= FTS3_SEGMENT_SCAN;
173525
173526 rc = sqlite3Fts3SegReaderCursor(
173527 p, iLangid, iIndex, FTS3_SEGCURSOR_ALL, 0, 0, 0, 1,&csr
173528 );
173529 if( rc==SQLITE_OK ){
173530 rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
173531 }
173532
173533 if( rc==SQLITE_OK ){
173534 while( SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, &csr)) ){
173535 char *pCsr = csr.aDoclist;
173536 char *pEnd = &pCsr[csr.nDoclist];
173537
173538 i64 iDocid = 0;
173539 i64 iCol = 0;
173540 i64 iPos = 0;
173541
173542 pCsr += sqlite3Fts3GetVarint(pCsr, &iDocid);
173543 while( pCsr<pEnd ){
173544 i64 iVal = 0;
173545 pCsr += sqlite3Fts3GetVarint(pCsr, &iVal);
173546 if( pCsr<pEnd ){
173547 if( iVal==0 || iVal==1 ){
173548 iCol = 0;
173549 iPos = 0;
173550 if( iVal ){
173551 pCsr += sqlite3Fts3GetVarint(pCsr, &iCol);
173552 }else{
173553 pCsr += sqlite3Fts3GetVarint(pCsr, &iVal);
173554 iDocid += iVal;
173555 }
173556 }else{
173557 iPos += (iVal - 2);
173558 cksum = cksum ^ fts3ChecksumEntry(
173559 csr.zTerm, csr.nTerm, iLangid, iIndex, iDocid,
173560 (int)iCol, (int)iPos
173561 );
173562 }
173563 }
173564 }
173565 }
173566 }
173567 sqlite3Fts3SegReaderFinish(&csr);
173568
173569 *pRc = rc;
173570 return cksum;
173571}
173572
173573/*
173574** Check if the contents of the FTS index match the current contents of the
173575** content table. If no error occurs and the contents do match, set *pbOk
173576** to true and return SQLITE_OK. Or if the contents do not match, set *pbOk
173577** to false before returning.
173578**
173579** If an error occurs (e.g. an OOM or IO error), return an SQLite error
173580** code. The final value of *pbOk is undefined in this case.
173581*/
173582static int fts3IntegrityCheck(Fts3Table *p, int *pbOk){
173583 int rc = SQLITE_OK; /* Return code */
173584 u64 cksum1 = 0; /* Checksum based on FTS index contents */
173585 u64 cksum2 = 0; /* Checksum based on %_content contents */
173586 sqlite3_stmt *pAllLangid = 0; /* Statement to return all language-ids */
173587
173588 /* This block calculates the checksum according to the FTS index. */
173589 rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
173590 if( rc==SQLITE_OK ){
173591 int rc2;
173592 sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
173593 sqlite3_bind_int(pAllLangid, 2, p->nIndex);
173594 while( rc==SQLITE_OK && sqlite3_step(pAllLangid)==SQLITE_ROW ){
173595 int iLangid = sqlite3_column_int(pAllLangid, 0);
173596 int i;
173597 for(i=0; i<p->nIndex; i++){
173598 cksum1 = cksum1 ^ fts3ChecksumIndex(p, iLangid, i, &rc);
173599 }
173600 }
173601 rc2 = sqlite3_reset(pAllLangid);
173602 if( rc==SQLITE_OK ) rc = rc2;
173603 }
173604
173605 /* This block calculates the checksum according to the %_content table */
173606 if( rc==SQLITE_OK ){
173607 sqlite3_tokenizer_module const *pModule = p->pTokenizer->pModule;
173608 sqlite3_stmt *pStmt = 0;
173609 char *zSql;
173610
173611 zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
173612 if( !zSql ){
173613 rc = SQLITE_NOMEM;
173614 }else{
173615 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
173616 sqlite3_free(zSql);
173617 }
173618
173619 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
173620 i64 iDocid = sqlite3_column_int64(pStmt, 0);
173621 int iLang = langidFromSelect(p, pStmt);
173622 int iCol;
173623
173624 for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
173625 if( p->abNotindexed[iCol]==0 ){
173626 const char *zText = (const char *)sqlite3_column_text(pStmt, iCol+1);
173627 int nText = sqlite3_column_bytes(pStmt, iCol+1);
173628 sqlite3_tokenizer_cursor *pT = 0;
173629
173630 rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, nText,&pT);
173631 while( rc==SQLITE_OK ){
173632 char const *zToken; /* Buffer containing token */
173633 int nToken = 0; /* Number of bytes in token */
173634 int iDum1 = 0, iDum2 = 0; /* Dummy variables */
173635 int iPos = 0; /* Position of token in zText */
173636
173637 rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
173638 if( rc==SQLITE_OK ){
173639 int i;
173640 cksum2 = cksum2 ^ fts3ChecksumEntry(
173641 zToken, nToken, iLang, 0, iDocid, iCol, iPos
173642 );
173643 for(i=1; i<p->nIndex; i++){
173644 if( p->aIndex[i].nPrefix<=nToken ){
173645 cksum2 = cksum2 ^ fts3ChecksumEntry(
173646 zToken, p->aIndex[i].nPrefix, iLang, i, iDocid, iCol, iPos
173647 );
173648 }
173649 }
173650 }
173651 }
173652 if( pT ) pModule->xClose(pT);
173653 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
173654 }
173655 }
173656 }
173657
173658 sqlite3_finalize(pStmt);
173659 }
173660
173661 *pbOk = (cksum1==cksum2);
173662 return rc;
173663}
173664
173665/*
173666** Run the integrity-check. If no error occurs and the current contents of
173667** the FTS index are correct, return SQLITE_OK. Or, if the contents of the
173668** FTS index are incorrect, return SQLITE_CORRUPT_VTAB.
173669**
173670** Or, if an error (e.g. an OOM or IO error) occurs, return an SQLite
173671** error code.
173672**
173673** The integrity-check works as follows. For each token and indexed token
173674** prefix in the document set, a 64-bit checksum is calculated (by code
173675** in fts3ChecksumEntry()) based on the following:
173676**
173677** + The index number (0 for the main index, 1 for the first prefix
173678** index etc.),
173679** + The token (or token prefix) text itself,
173680** + The language-id of the row it appears in,
173681** + The docid of the row it appears in,
173682** + The column it appears in, and
173683** + The tokens position within that column.
173684**
173685** The checksums for all entries in the index are XORed together to create
173686** a single checksum for the entire index.
173687**
173688** The integrity-check code calculates the same checksum in two ways:
173689**
173690** 1. By scanning the contents of the FTS index, and
173691** 2. By scanning and tokenizing the content table.
173692**
173693** If the two checksums are identical, the integrity-check is deemed to have
173694** passed.
173695*/
173696static int fts3DoIntegrityCheck(
173697 Fts3Table *p /* FTS3 table handle */
173698){
173699 int rc;
173700 int bOk = 0;
173701 rc = fts3IntegrityCheck(p, &bOk);
173702 if( rc==SQLITE_OK && bOk==0 ) rc = FTS_CORRUPT_VTAB;
173703 return rc;
173704}
173705
173706/*
173707** Handle a 'special' INSERT of the form:
173708**
173709** "INSERT INTO tbl(tbl) VALUES(<expr>)"
173710**
173711** Argument pVal contains the result of <expr>. Currently the only
173712** meaningful value to insert is the text 'optimize'.
173713*/
173714static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
173715 int rc; /* Return Code */
173716 const char *zVal = (const char *)sqlite3_value_text(pVal);
173717 int nVal = sqlite3_value_bytes(pVal);
173718
173719 if( !zVal ){
173720 return SQLITE_NOMEM;
173721 }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
173722 rc = fts3DoOptimize(p, 0);
173723 }else if( nVal==7 && 0==sqlite3_strnicmp(zVal, "rebuild", 7) ){
173724 rc = fts3DoRebuild(p);
173725 }else if( nVal==15 && 0==sqlite3_strnicmp(zVal, "integrity-check", 15) ){
173726 rc = fts3DoIntegrityCheck(p);
173727 }else if( nVal>6 && 0==sqlite3_strnicmp(zVal, "merge=", 6) ){
173728 rc = fts3DoIncrmerge(p, &zVal[6]);
173729 }else if( nVal>10 && 0==sqlite3_strnicmp(zVal, "automerge=", 10) ){
173730 rc = fts3DoAutoincrmerge(p, &zVal[10]);
173731#ifdef SQLITE_TEST
173732 }else if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
173733 p->nNodeSize = atoi(&zVal[9]);
173734 rc = SQLITE_OK;
173735 }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
173736 p->nMaxPendingData = atoi(&zVal[11]);
173737 rc = SQLITE_OK;
173738 }else if( nVal>21 && 0==sqlite3_strnicmp(zVal, "test-no-incr-doclist=", 21) ){
173739 p->bNoIncrDoclist = atoi(&zVal[21]);
173740 rc = SQLITE_OK;
173741#endif
173742 }else{
173743 rc = SQLITE_ERROR;
173744 }
173745
173746 return rc;
173747}
173748
173749#ifndef SQLITE_DISABLE_FTS4_DEFERRED
173750/*
173751** Delete all cached deferred doclists. Deferred doclists are cached
173752** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function.
173753*/
173754SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *pCsr){
173755 Fts3DeferredToken *pDef;
173756 for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){
173757 fts3PendingListDelete(pDef->pList);
173758 pDef->pList = 0;
173759 }
173760}
173761
173762/*
173763** Free all entries in the pCsr->pDeffered list. Entries are added to
173764** this list using sqlite3Fts3DeferToken().
173765*/
173766SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *pCsr){
173767 Fts3DeferredToken *pDef;
173768 Fts3DeferredToken *pNext;
173769 for(pDef=pCsr->pDeferred; pDef; pDef=pNext){
173770 pNext = pDef->pNext;
173771 fts3PendingListDelete(pDef->pList);
173772 sqlite3_free(pDef);
173773 }
173774 pCsr->pDeferred = 0;
173775}
173776
173777/*
173778** Generate deferred-doclists for all tokens in the pCsr->pDeferred list
173779** based on the row that pCsr currently points to.
173780**
173781** A deferred-doclist is like any other doclist with position information
173782** included, except that it only contains entries for a single row of the
173783** table, not for all rows.
173784*/
173785SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *pCsr){
173786 int rc = SQLITE_OK; /* Return code */
173787 if( pCsr->pDeferred ){
173788 int i; /* Used to iterate through table columns */
173789 sqlite3_int64 iDocid; /* Docid of the row pCsr points to */
173790 Fts3DeferredToken *pDef; /* Used to iterate through deferred tokens */
173791
173792 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
173793 sqlite3_tokenizer *pT = p->pTokenizer;
173794 sqlite3_tokenizer_module const *pModule = pT->pModule;
173795
173796 assert( pCsr->isRequireSeek==0 );
173797 iDocid = sqlite3_column_int64(pCsr->pStmt, 0);
173798
173799 for(i=0; i<p->nColumn && rc==SQLITE_OK; i++){
173800 if( p->abNotindexed[i]==0 ){
173801 const char *zText = (const char *)sqlite3_column_text(pCsr->pStmt, i+1);
173802 sqlite3_tokenizer_cursor *pTC = 0;
173803
173804 rc = sqlite3Fts3OpenTokenizer(pT, pCsr->iLangid, zText, -1, &pTC);
173805 while( rc==SQLITE_OK ){
173806 char const *zToken; /* Buffer containing token */
173807 int nToken = 0; /* Number of bytes in token */
173808 int iDum1 = 0, iDum2 = 0; /* Dummy variables */
173809 int iPos = 0; /* Position of token in zText */
173810
173811 rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
173812 for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
173813 Fts3PhraseToken *pPT = pDef->pToken;
173814 if( (pDef->iCol>=p->nColumn || pDef->iCol==i)
173815 && (pPT->bFirst==0 || iPos==0)
173816 && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken))
173817 && (0==memcmp(zToken, pPT->z, pPT->n))
173818 ){
173819 fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc);
173820 }
173821 }
173822 }
173823 if( pTC ) pModule->xClose(pTC);
173824 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
173825 }
173826 }
173827
173828 for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
173829 if( pDef->pList ){
173830 rc = fts3PendingListAppendVarint(&pDef->pList, 0);
173831 }
173832 }
173833 }
173834
173835 return rc;
173836}
173837
173838SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(
173839 Fts3DeferredToken *p,
173840 char **ppData,
173841 int *pnData
173842){
173843 char *pRet;
173844 int nSkip;
173845 sqlite3_int64 dummy;
173846
173847 *ppData = 0;
173848 *pnData = 0;
173849
173850 if( p->pList==0 ){
173851 return SQLITE_OK;
173852 }
173853
173854 pRet = (char *)sqlite3_malloc(p->pList->nData);
173855 if( !pRet ) return SQLITE_NOMEM;
173856
173857 nSkip = sqlite3Fts3GetVarint(p->pList->aData, &dummy);
173858 *pnData = p->pList->nData - nSkip;
173859 *ppData = pRet;
173860
173861 memcpy(pRet, &p->pList->aData[nSkip], *pnData);
173862 return SQLITE_OK;
173863}
173864
173865/*
173866** Add an entry for token pToken to the pCsr->pDeferred list.
173867*/
173868SQLITE_PRIVATE int sqlite3Fts3DeferToken(
173869 Fts3Cursor *pCsr, /* Fts3 table cursor */
173870 Fts3PhraseToken *pToken, /* Token to defer */
173871 int iCol /* Column that token must appear in (or -1) */
173872){
173873 Fts3DeferredToken *pDeferred;
173874 pDeferred = sqlite3_malloc(sizeof(*pDeferred));
173875 if( !pDeferred ){
173876 return SQLITE_NOMEM;
173877 }
173878 memset(pDeferred, 0, sizeof(*pDeferred));
173879 pDeferred->pToken = pToken;
173880 pDeferred->pNext = pCsr->pDeferred;
173881 pDeferred->iCol = iCol;
173882 pCsr->pDeferred = pDeferred;
173883
173884 assert( pToken->pDeferred==0 );
173885 pToken->pDeferred = pDeferred;
173886
173887 return SQLITE_OK;
173888}
173889#endif
173890
173891/*
173892** SQLite value pRowid contains the rowid of a row that may or may not be
173893** present in the FTS3 table. If it is, delete it and adjust the contents
173894** of subsiduary data structures accordingly.
173895*/
173896static int fts3DeleteByRowid(
173897 Fts3Table *p,
173898 sqlite3_value *pRowid,
173899 int *pnChng, /* IN/OUT: Decrement if row is deleted */
173900 u32 *aSzDel
173901){
173902 int rc = SQLITE_OK; /* Return code */
173903 int bFound = 0; /* True if *pRowid really is in the table */
173904
173905 fts3DeleteTerms(&rc, p, pRowid, aSzDel, &bFound);
173906 if( bFound && rc==SQLITE_OK ){
173907 int isEmpty = 0; /* Deleting *pRowid leaves the table empty */
173908 rc = fts3IsEmpty(p, pRowid, &isEmpty);
173909 if( rc==SQLITE_OK ){
173910 if( isEmpty ){
173911 /* Deleting this row means the whole table is empty. In this case
173912 ** delete the contents of all three tables and throw away any
173913 ** data in the pendingTerms hash table. */
173914 rc = fts3DeleteAll(p, 1);
173915 *pnChng = 0;
173916 memset(aSzDel, 0, sizeof(u32) * (p->nColumn+1) * 2);
173917 }else{
173918 *pnChng = *pnChng - 1;
173919 if( p->zContentTbl==0 ){
173920 fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, &pRowid);
173921 }
173922 if( p->bHasDocsize ){
173923 fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE, &pRowid);
173924 }
173925 }
173926 }
173927 }
173928
173929 return rc;
173930}
173931
173932/*
173933** This function does the work for the xUpdate method of FTS3 virtual
173934** tables. The schema of the virtual table being:
173935**
173936** CREATE TABLE <table name>(
173937** <user columns>,
173938** <table name> HIDDEN,
173939** docid HIDDEN,
173940** <langid> HIDDEN
173941** );
173942**
173943**
173944*/
173945SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(
173946 sqlite3_vtab *pVtab, /* FTS3 vtab object */
173947 int nArg, /* Size of argument array */
173948 sqlite3_value **apVal, /* Array of arguments */
173949 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
173950){
173951 Fts3Table *p = (Fts3Table *)pVtab;
173952 int rc = SQLITE_OK; /* Return Code */
173953 u32 *aSzIns = 0; /* Sizes of inserted documents */
173954 u32 *aSzDel = 0; /* Sizes of deleted documents */
173955 int nChng = 0; /* Net change in number of documents */
173956 int bInsertDone = 0;
173957
173958 /* At this point it must be known if the %_stat table exists or not.
173959 ** So bHasStat may not be 2. */
173960 assert( p->bHasStat==0 || p->bHasStat==1 );
173961
173962 assert( p->pSegments==0 );
173963 assert(
173964 nArg==1 /* DELETE operations */
173965 || nArg==(2 + p->nColumn + 3) /* INSERT or UPDATE operations */
173966 );
173967
173968 /* Check for a "special" INSERT operation. One of the form:
173969 **
173970 ** INSERT INTO xyz(xyz) VALUES('command');
173971 */
173972 if( nArg>1
173973 && sqlite3_value_type(apVal[0])==SQLITE_NULL
173974 && sqlite3_value_type(apVal[p->nColumn+2])!=SQLITE_NULL
173975 ){
173976 rc = fts3SpecialInsert(p, apVal[p->nColumn+2]);
173977 goto update_out;
173978 }
173979
173980 if( nArg>1 && sqlite3_value_int(apVal[2 + p->nColumn + 2])<0 ){
173981 rc = SQLITE_CONSTRAINT;
173982 goto update_out;
173983 }
173984
173985 /* Allocate space to hold the change in document sizes */
173986 aSzDel = sqlite3_malloc( sizeof(aSzDel[0])*(p->nColumn+1)*2 );
173987 if( aSzDel==0 ){
173988 rc = SQLITE_NOMEM;
173989 goto update_out;
173990 }
173991 aSzIns = &aSzDel[p->nColumn+1];
173992 memset(aSzDel, 0, sizeof(aSzDel[0])*(p->nColumn+1)*2);
173993
173994 rc = fts3Writelock(p);
173995 if( rc!=SQLITE_OK ) goto update_out;
173996
173997 /* If this is an INSERT operation, or an UPDATE that modifies the rowid
173998 ** value, then this operation requires constraint handling.
173999 **
174000 ** If the on-conflict mode is REPLACE, this means that the existing row
174001 ** should be deleted from the database before inserting the new row. Or,
174002 ** if the on-conflict mode is other than REPLACE, then this method must
174003 ** detect the conflict and return SQLITE_CONSTRAINT before beginning to
174004 ** modify the database file.
174005 */
174006 if( nArg>1 && p->zContentTbl==0 ){
174007 /* Find the value object that holds the new rowid value. */
174008 sqlite3_value *pNewRowid = apVal[3+p->nColumn];
174009 if( sqlite3_value_type(pNewRowid)==SQLITE_NULL ){
174010 pNewRowid = apVal[1];
174011 }
174012
174013 if( sqlite3_value_type(pNewRowid)!=SQLITE_NULL && (
174014 sqlite3_value_type(apVal[0])==SQLITE_NULL
174015 || sqlite3_value_int64(apVal[0])!=sqlite3_value_int64(pNewRowid)
174016 )){
174017 /* The new rowid is not NULL (in this case the rowid will be
174018 ** automatically assigned and there is no chance of a conflict), and
174019 ** the statement is either an INSERT or an UPDATE that modifies the
174020 ** rowid column. So if the conflict mode is REPLACE, then delete any
174021 ** existing row with rowid=pNewRowid.
174022 **
174023 ** Or, if the conflict mode is not REPLACE, insert the new record into
174024 ** the %_content table. If we hit the duplicate rowid constraint (or any
174025 ** other error) while doing so, return immediately.
174026 **
174027 ** This branch may also run if pNewRowid contains a value that cannot
174028 ** be losslessly converted to an integer. In this case, the eventual
174029 ** call to fts3InsertData() (either just below or further on in this
174030 ** function) will return SQLITE_MISMATCH. If fts3DeleteByRowid is
174031 ** invoked, it will delete zero rows (since no row will have
174032 ** docid=$pNewRowid if $pNewRowid is not an integer value).
174033 */
174034 if( sqlite3_vtab_on_conflict(p->db)==SQLITE_REPLACE ){
174035 rc = fts3DeleteByRowid(p, pNewRowid, &nChng, aSzDel);
174036 }else{
174037 rc = fts3InsertData(p, apVal, pRowid);
174038 bInsertDone = 1;
174039 }
174040 }
174041 }
174042 if( rc!=SQLITE_OK ){
174043 goto update_out;
174044 }
174045
174046 /* If this is a DELETE or UPDATE operation, remove the old record. */
174047 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
174048 assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
174049 rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
174050 }
174051
174052 /* If this is an INSERT or UPDATE operation, insert the new record. */
174053 if( nArg>1 && rc==SQLITE_OK ){
174054 int iLangid = sqlite3_value_int(apVal[2 + p->nColumn + 2]);
174055 if( bInsertDone==0 ){
174056 rc = fts3InsertData(p, apVal, pRowid);
174057 if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
174058 rc = FTS_CORRUPT_VTAB;
174059 }
174060 }
174061 if( rc==SQLITE_OK ){
174062 rc = fts3PendingTermsDocid(p, 0, iLangid, *pRowid);
174063 }
174064 if( rc==SQLITE_OK ){
174065 assert( p->iPrevDocid==*pRowid );
174066 rc = fts3InsertTerms(p, iLangid, apVal, aSzIns);
174067 }
174068 if( p->bHasDocsize ){
174069 fts3InsertDocsize(&rc, p, aSzIns);
174070 }
174071 nChng++;
174072 }
174073
174074 if( p->bFts4 ){
174075 fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nChng);
174076 }
174077
174078 update_out:
174079 sqlite3_free(aSzDel);
174080 sqlite3Fts3SegmentsClose(p);
174081 return rc;
174082}
174083
174084/*
174085** Flush any data in the pending-terms hash table to disk. If successful,
174086** merge all segments in the database (including the new segment, if
174087** there was any data to flush) into a single segment.
174088*/
174089SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *p){
174090 int rc;
174091 rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
174092 if( rc==SQLITE_OK ){
174093 rc = fts3DoOptimize(p, 1);
174094 if( rc==SQLITE_OK || rc==SQLITE_DONE ){
174095 int rc2 = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
174096 if( rc2!=SQLITE_OK ) rc = rc2;
174097 }else{
174098 sqlite3_exec(p->db, "ROLLBACK TO fts3", 0, 0, 0);
174099 sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
174100 }
174101 }
174102 sqlite3Fts3SegmentsClose(p);
174103 return rc;
174104}
174105
174106#endif
174107
174108/************** End of fts3_write.c ******************************************/
174109/************** Begin file fts3_snippet.c ************************************/
174110/*
174111** 2009 Oct 23
174112**
174113** The author disclaims copyright to this source code. In place of
174114** a legal notice, here is a blessing:
174115**
174116** May you do good and not evil.
174117** May you find forgiveness for yourself and forgive others.
174118** May you share freely, never taking more than you give.
174119**
174120******************************************************************************
174121*/
174122
174123/* #include "fts3Int.h" */
174124#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
174125
174126/* #include <string.h> */
174127/* #include <assert.h> */
174128
174129/*
174130** Characters that may appear in the second argument to matchinfo().
174131*/
174132#define FTS3_MATCHINFO_NPHRASE 'p' /* 1 value */
174133#define FTS3_MATCHINFO_NCOL 'c' /* 1 value */
174134#define FTS3_MATCHINFO_NDOC 'n' /* 1 value */
174135#define FTS3_MATCHINFO_AVGLENGTH 'a' /* nCol values */
174136#define FTS3_MATCHINFO_LENGTH 'l' /* nCol values */
174137#define FTS3_MATCHINFO_LCS 's' /* nCol values */
174138#define FTS3_MATCHINFO_HITS 'x' /* 3*nCol*nPhrase values */
174139#define FTS3_MATCHINFO_LHITS 'y' /* nCol*nPhrase values */
174140#define FTS3_MATCHINFO_LHITS_BM 'b' /* nCol*nPhrase values */
174141
174142/*
174143** The default value for the second argument to matchinfo().
174144*/
174145#define FTS3_MATCHINFO_DEFAULT "pcx"
174146
174147
174148/*
174149** Used as an fts3ExprIterate() context when loading phrase doclists to
174150** Fts3Expr.aDoclist[]/nDoclist.
174151*/
174152typedef struct LoadDoclistCtx LoadDoclistCtx;
174153struct LoadDoclistCtx {
174154 Fts3Cursor *pCsr; /* FTS3 Cursor */
174155 int nPhrase; /* Number of phrases seen so far */
174156 int nToken; /* Number of tokens seen so far */
174157};
174158
174159/*
174160** The following types are used as part of the implementation of the
174161** fts3BestSnippet() routine.
174162*/
174163typedef struct SnippetIter SnippetIter;
174164typedef struct SnippetPhrase SnippetPhrase;
174165typedef struct SnippetFragment SnippetFragment;
174166
174167struct SnippetIter {
174168 Fts3Cursor *pCsr; /* Cursor snippet is being generated from */
174169 int iCol; /* Extract snippet from this column */
174170 int nSnippet; /* Requested snippet length (in tokens) */
174171 int nPhrase; /* Number of phrases in query */
174172 SnippetPhrase *aPhrase; /* Array of size nPhrase */
174173 int iCurrent; /* First token of current snippet */
174174};
174175
174176struct SnippetPhrase {
174177 int nToken; /* Number of tokens in phrase */
174178 char *pList; /* Pointer to start of phrase position list */
174179 int iHead; /* Next value in position list */
174180 char *pHead; /* Position list data following iHead */
174181 int iTail; /* Next value in trailing position list */
174182 char *pTail; /* Position list data following iTail */
174183};
174184
174185struct SnippetFragment {
174186 int iCol; /* Column snippet is extracted from */
174187 int iPos; /* Index of first token in snippet */
174188 u64 covered; /* Mask of query phrases covered */
174189 u64 hlmask; /* Mask of snippet terms to highlight */
174190};
174191
174192/*
174193** This type is used as an fts3ExprIterate() context object while
174194** accumulating the data returned by the matchinfo() function.
174195*/
174196typedef struct MatchInfo MatchInfo;
174197struct MatchInfo {
174198 Fts3Cursor *pCursor; /* FTS3 Cursor */
174199 int nCol; /* Number of columns in table */
174200 int nPhrase; /* Number of matchable phrases in query */
174201 sqlite3_int64 nDoc; /* Number of docs in database */
174202 char flag;
174203 u32 *aMatchinfo; /* Pre-allocated buffer */
174204};
174205
174206/*
174207** An instance of this structure is used to manage a pair of buffers, each
174208** (nElem * sizeof(u32)) bytes in size. See the MatchinfoBuffer code below
174209** for details.
174210*/
174211struct MatchinfoBuffer {
174212 u8 aRef[3];
174213 int nElem;
174214 int bGlobal; /* Set if global data is loaded */
174215 char *zMatchinfo;
174216 u32 aMatchinfo[1];
174217};
174218
174219
174220/*
174221** The snippet() and offsets() functions both return text values. An instance
174222** of the following structure is used to accumulate those values while the
174223** functions are running. See fts3StringAppend() for details.
174224*/
174225typedef struct StrBuffer StrBuffer;
174226struct StrBuffer {
174227 char *z; /* Pointer to buffer containing string */
174228 int n; /* Length of z in bytes (excl. nul-term) */
174229 int nAlloc; /* Allocated size of buffer z in bytes */
174230};
174231
174232
174233/*************************************************************************
174234** Start of MatchinfoBuffer code.
174235*/
174236
174237/*
174238** Allocate a two-slot MatchinfoBuffer object.
174239*/
174240static MatchinfoBuffer *fts3MIBufferNew(int nElem, const char *zMatchinfo){
174241 MatchinfoBuffer *pRet;
174242 int nByte = sizeof(u32) * (2*nElem + 1) + sizeof(MatchinfoBuffer);
174243 int nStr = (int)strlen(zMatchinfo);
174244
174245 pRet = sqlite3_malloc(nByte + nStr+1);
174246 if( pRet ){
174247 memset(pRet, 0, nByte);
174248 pRet->aMatchinfo[0] = (u8*)(&pRet->aMatchinfo[1]) - (u8*)pRet;
174249 pRet->aMatchinfo[1+nElem] = pRet->aMatchinfo[0] + sizeof(u32)*(nElem+1);
174250 pRet->nElem = nElem;
174251 pRet->zMatchinfo = ((char*)pRet) + nByte;
174252 memcpy(pRet->zMatchinfo, zMatchinfo, nStr+1);
174253 pRet->aRef[0] = 1;
174254 }
174255
174256 return pRet;
174257}
174258
174259static void fts3MIBufferFree(void *p){
174260 MatchinfoBuffer *pBuf = (MatchinfoBuffer*)((u8*)p - ((u32*)p)[-1]);
174261
174262 assert( (u32*)p==&pBuf->aMatchinfo[1]
174263 || (u32*)p==&pBuf->aMatchinfo[pBuf->nElem+2]
174264 );
174265 if( (u32*)p==&pBuf->aMatchinfo[1] ){
174266 pBuf->aRef[1] = 0;
174267 }else{
174268 pBuf->aRef[2] = 0;
174269 }
174270
174271 if( pBuf->aRef[0]==0 && pBuf->aRef[1]==0 && pBuf->aRef[2]==0 ){
174272 sqlite3_free(pBuf);
174273 }
174274}
174275
174276static void (*fts3MIBufferAlloc(MatchinfoBuffer *p, u32 **paOut))(void*){
174277 void (*xRet)(void*) = 0;
174278 u32 *aOut = 0;
174279
174280 if( p->aRef[1]==0 ){
174281 p->aRef[1] = 1;
174282 aOut = &p->aMatchinfo[1];
174283 xRet = fts3MIBufferFree;
174284 }
174285 else if( p->aRef[2]==0 ){
174286 p->aRef[2] = 1;
174287 aOut = &p->aMatchinfo[p->nElem+2];
174288 xRet = fts3MIBufferFree;
174289 }else{
174290 aOut = (u32*)sqlite3_malloc(p->nElem * sizeof(u32));
174291 if( aOut ){
174292 xRet = sqlite3_free;
174293 if( p->bGlobal ) memcpy(aOut, &p->aMatchinfo[1], p->nElem*sizeof(u32));
174294 }
174295 }
174296
174297 *paOut = aOut;
174298 return xRet;
174299}
174300
174301static void fts3MIBufferSetGlobal(MatchinfoBuffer *p){
174302 p->bGlobal = 1;
174303 memcpy(&p->aMatchinfo[2+p->nElem], &p->aMatchinfo[1], p->nElem*sizeof(u32));
174304}
174305
174306/*
174307** Free a MatchinfoBuffer object allocated using fts3MIBufferNew()
174308*/
174309SQLITE_PRIVATE void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p){
174310 if( p ){
174311 assert( p->aRef[0]==1 );
174312 p->aRef[0] = 0;
174313 if( p->aRef[0]==0 && p->aRef[1]==0 && p->aRef[2]==0 ){
174314 sqlite3_free(p);
174315 }
174316 }
174317}
174318
174319/*
174320** End of MatchinfoBuffer code.
174321*************************************************************************/
174322
174323
174324/*
174325** This function is used to help iterate through a position-list. A position
174326** list is a list of unique integers, sorted from smallest to largest. Each
174327** element of the list is represented by an FTS3 varint that takes the value
174328** of the difference between the current element and the previous one plus
174329** two. For example, to store the position-list:
174330**
174331** 4 9 113
174332**
174333** the three varints:
174334**
174335** 6 7 106
174336**
174337** are encoded.
174338**
174339** When this function is called, *pp points to the start of an element of
174340** the list. *piPos contains the value of the previous entry in the list.
174341** After it returns, *piPos contains the value of the next element of the
174342** list and *pp is advanced to the following varint.
174343*/
174344static void fts3GetDeltaPosition(char **pp, int *piPos){
174345 int iVal;
174346 *pp += fts3GetVarint32(*pp, &iVal);
174347 *piPos += (iVal-2);
174348}
174349
174350/*
174351** Helper function for fts3ExprIterate() (see below).
174352*/
174353static int fts3ExprIterate2(
174354 Fts3Expr *pExpr, /* Expression to iterate phrases of */
174355 int *piPhrase, /* Pointer to phrase counter */
174356 int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */
174357 void *pCtx /* Second argument to pass to callback */
174358){
174359 int rc; /* Return code */
174360 int eType = pExpr->eType; /* Type of expression node pExpr */
174361
174362 if( eType!=FTSQUERY_PHRASE ){
174363 assert( pExpr->pLeft && pExpr->pRight );
174364 rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx);
174365 if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){
174366 rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx);
174367 }
174368 }else{
174369 rc = x(pExpr, *piPhrase, pCtx);
174370 (*piPhrase)++;
174371 }
174372 return rc;
174373}
174374
174375/*
174376** Iterate through all phrase nodes in an FTS3 query, except those that
174377** are part of a sub-tree that is the right-hand-side of a NOT operator.
174378** For each phrase node found, the supplied callback function is invoked.
174379**
174380** If the callback function returns anything other than SQLITE_OK,
174381** the iteration is abandoned and the error code returned immediately.
174382** Otherwise, SQLITE_OK is returned after a callback has been made for
174383** all eligible phrase nodes.
174384*/
174385static int fts3ExprIterate(
174386 Fts3Expr *pExpr, /* Expression to iterate phrases of */
174387 int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */
174388 void *pCtx /* Second argument to pass to callback */
174389){
174390 int iPhrase = 0; /* Variable used as the phrase counter */
174391 return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx);
174392}
174393
174394
174395/*
174396** This is an fts3ExprIterate() callback used while loading the doclists
174397** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
174398** fts3ExprLoadDoclists().
174399*/
174400static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
174401 int rc = SQLITE_OK;
174402 Fts3Phrase *pPhrase = pExpr->pPhrase;
174403 LoadDoclistCtx *p = (LoadDoclistCtx *)ctx;
174404
174405 UNUSED_PARAMETER(iPhrase);
174406
174407 p->nPhrase++;
174408 p->nToken += pPhrase->nToken;
174409
174410 return rc;
174411}
174412
174413/*
174414** Load the doclists for each phrase in the query associated with FTS3 cursor
174415** pCsr.
174416**
174417** If pnPhrase is not NULL, then *pnPhrase is set to the number of matchable
174418** phrases in the expression (all phrases except those directly or
174419** indirectly descended from the right-hand-side of a NOT operator). If
174420** pnToken is not NULL, then it is set to the number of tokens in all
174421** matchable phrases of the expression.
174422*/
174423static int fts3ExprLoadDoclists(
174424 Fts3Cursor *pCsr, /* Fts3 cursor for current query */
174425 int *pnPhrase, /* OUT: Number of phrases in query */
174426 int *pnToken /* OUT: Number of tokens in query */
174427){
174428 int rc; /* Return Code */
174429 LoadDoclistCtx sCtx = {0,0,0}; /* Context for fts3ExprIterate() */
174430 sCtx.pCsr = pCsr;
174431 rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb, (void *)&sCtx);
174432 if( pnPhrase ) *pnPhrase = sCtx.nPhrase;
174433 if( pnToken ) *pnToken = sCtx.nToken;
174434 return rc;
174435}
174436
174437static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
174438 (*(int *)ctx)++;
174439 pExpr->iPhrase = iPhrase;
174440 return SQLITE_OK;
174441}
174442static int fts3ExprPhraseCount(Fts3Expr *pExpr){
174443 int nPhrase = 0;
174444 (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase);
174445 return nPhrase;
174446}
174447
174448/*
174449** Advance the position list iterator specified by the first two
174450** arguments so that it points to the first element with a value greater
174451** than or equal to parameter iNext.
174452*/
174453static void fts3SnippetAdvance(char **ppIter, int *piIter, int iNext){
174454 char *pIter = *ppIter;
174455 if( pIter ){
174456 int iIter = *piIter;
174457
174458 while( iIter<iNext ){
174459 if( 0==(*pIter & 0xFE) ){
174460 iIter = -1;
174461 pIter = 0;
174462 break;
174463 }
174464 fts3GetDeltaPosition(&pIter, &iIter);
174465 }
174466
174467 *piIter = iIter;
174468 *ppIter = pIter;
174469 }
174470}
174471
174472/*
174473** Advance the snippet iterator to the next candidate snippet.
174474*/
174475static int fts3SnippetNextCandidate(SnippetIter *pIter){
174476 int i; /* Loop counter */
174477
174478 if( pIter->iCurrent<0 ){
174479 /* The SnippetIter object has just been initialized. The first snippet
174480 ** candidate always starts at offset 0 (even if this candidate has a
174481 ** score of 0.0).
174482 */
174483 pIter->iCurrent = 0;
174484
174485 /* Advance the 'head' iterator of each phrase to the first offset that
174486 ** is greater than or equal to (iNext+nSnippet).
174487 */
174488 for(i=0; i<pIter->nPhrase; i++){
174489 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
174490 fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, pIter->nSnippet);
174491 }
174492 }else{
174493 int iStart;
174494 int iEnd = 0x7FFFFFFF;
174495
174496 for(i=0; i<pIter->nPhrase; i++){
174497 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
174498 if( pPhrase->pHead && pPhrase->iHead<iEnd ){
174499 iEnd = pPhrase->iHead;
174500 }
174501 }
174502 if( iEnd==0x7FFFFFFF ){
174503 return 1;
174504 }
174505
174506 pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1;
174507 for(i=0; i<pIter->nPhrase; i++){
174508 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
174509 fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, iEnd+1);
174510 fts3SnippetAdvance(&pPhrase->pTail, &pPhrase->iTail, iStart);
174511 }
174512 }
174513
174514 return 0;
174515}
174516
174517/*
174518** Retrieve information about the current candidate snippet of snippet
174519** iterator pIter.
174520*/
174521static void fts3SnippetDetails(
174522 SnippetIter *pIter, /* Snippet iterator */
174523 u64 mCovered, /* Bitmask of phrases already covered */
174524 int *piToken, /* OUT: First token of proposed snippet */
174525 int *piScore, /* OUT: "Score" for this snippet */
174526 u64 *pmCover, /* OUT: Bitmask of phrases covered */
174527 u64 *pmHighlight /* OUT: Bitmask of terms to highlight */
174528){
174529 int iStart = pIter->iCurrent; /* First token of snippet */
174530 int iScore = 0; /* Score of this snippet */
174531 int i; /* Loop counter */
174532 u64 mCover = 0; /* Mask of phrases covered by this snippet */
174533 u64 mHighlight = 0; /* Mask of tokens to highlight in snippet */
174534
174535 for(i=0; i<pIter->nPhrase; i++){
174536 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
174537 if( pPhrase->pTail ){
174538 char *pCsr = pPhrase->pTail;
174539 int iCsr = pPhrase->iTail;
174540
174541 while( iCsr<(iStart+pIter->nSnippet) ){
174542 int j;
174543 u64 mPhrase = (u64)1 << i;
174544 u64 mPos = (u64)1 << (iCsr - iStart);
174545 assert( iCsr>=iStart );
174546 if( (mCover|mCovered)&mPhrase ){
174547 iScore++;
174548 }else{
174549 iScore += 1000;
174550 }
174551 mCover |= mPhrase;
174552
174553 for(j=0; j<pPhrase->nToken; j++){
174554 mHighlight |= (mPos>>j);
174555 }
174556
174557 if( 0==(*pCsr & 0x0FE) ) break;
174558 fts3GetDeltaPosition(&pCsr, &iCsr);
174559 }
174560 }
174561 }
174562
174563 /* Set the output variables before returning. */
174564 *piToken = iStart;
174565 *piScore = iScore;
174566 *pmCover = mCover;
174567 *pmHighlight = mHighlight;
174568}
174569
174570/*
174571** This function is an fts3ExprIterate() callback used by fts3BestSnippet().
174572** Each invocation populates an element of the SnippetIter.aPhrase[] array.
174573*/
174574static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){
174575 SnippetIter *p = (SnippetIter *)ctx;
174576 SnippetPhrase *pPhrase = &p->aPhrase[iPhrase];
174577 char *pCsr;
174578 int rc;
174579
174580 pPhrase->nToken = pExpr->pPhrase->nToken;
174581 rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pCsr);
174582 assert( rc==SQLITE_OK || pCsr==0 );
174583 if( pCsr ){
174584 int iFirst = 0;
174585 pPhrase->pList = pCsr;
174586 fts3GetDeltaPosition(&pCsr, &iFirst);
174587 assert( iFirst>=0 );
174588 pPhrase->pHead = pCsr;
174589 pPhrase->pTail = pCsr;
174590 pPhrase->iHead = iFirst;
174591 pPhrase->iTail = iFirst;
174592 }else{
174593 assert( rc!=SQLITE_OK || (
174594 pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0
174595 ));
174596 }
174597
174598 return rc;
174599}
174600
174601/*
174602** Select the fragment of text consisting of nFragment contiguous tokens
174603** from column iCol that represent the "best" snippet. The best snippet
174604** is the snippet with the highest score, where scores are calculated
174605** by adding:
174606**
174607** (a) +1 point for each occurrence of a matchable phrase in the snippet.
174608**
174609** (b) +1000 points for the first occurrence of each matchable phrase in
174610** the snippet for which the corresponding mCovered bit is not set.
174611**
174612** The selected snippet parameters are stored in structure *pFragment before
174613** returning. The score of the selected snippet is stored in *piScore
174614** before returning.
174615*/
174616static int fts3BestSnippet(
174617 int nSnippet, /* Desired snippet length */
174618 Fts3Cursor *pCsr, /* Cursor to create snippet for */
174619 int iCol, /* Index of column to create snippet from */
174620 u64 mCovered, /* Mask of phrases already covered */
174621 u64 *pmSeen, /* IN/OUT: Mask of phrases seen */
174622 SnippetFragment *pFragment, /* OUT: Best snippet found */
174623 int *piScore /* OUT: Score of snippet pFragment */
174624){
174625 int rc; /* Return Code */
174626 int nList; /* Number of phrases in expression */
174627 SnippetIter sIter; /* Iterates through snippet candidates */
174628 int nByte; /* Number of bytes of space to allocate */
174629 int iBestScore = -1; /* Best snippet score found so far */
174630 int i; /* Loop counter */
174631
174632 memset(&sIter, 0, sizeof(sIter));
174633
174634 /* Iterate through the phrases in the expression to count them. The same
174635 ** callback makes sure the doclists are loaded for each phrase.
174636 */
174637 rc = fts3ExprLoadDoclists(pCsr, &nList, 0);
174638 if( rc!=SQLITE_OK ){
174639 return rc;
174640 }
174641
174642 /* Now that it is known how many phrases there are, allocate and zero
174643 ** the required space using malloc().
174644 */
174645 nByte = sizeof(SnippetPhrase) * nList;
174646 sIter.aPhrase = (SnippetPhrase *)sqlite3_malloc(nByte);
174647 if( !sIter.aPhrase ){
174648 return SQLITE_NOMEM;
174649 }
174650 memset(sIter.aPhrase, 0, nByte);
174651
174652 /* Initialize the contents of the SnippetIter object. Then iterate through
174653 ** the set of phrases in the expression to populate the aPhrase[] array.
174654 */
174655 sIter.pCsr = pCsr;
174656 sIter.iCol = iCol;
174657 sIter.nSnippet = nSnippet;
174658 sIter.nPhrase = nList;
174659 sIter.iCurrent = -1;
174660 rc = fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void*)&sIter);
174661 if( rc==SQLITE_OK ){
174662
174663 /* Set the *pmSeen output variable. */
174664 for(i=0; i<nList; i++){
174665 if( sIter.aPhrase[i].pHead ){
174666 *pmSeen |= (u64)1 << i;
174667 }
174668 }
174669
174670 /* Loop through all candidate snippets. Store the best snippet in
174671 ** *pFragment. Store its associated 'score' in iBestScore.
174672 */
174673 pFragment->iCol = iCol;
174674 while( !fts3SnippetNextCandidate(&sIter) ){
174675 int iPos;
174676 int iScore;
174677 u64 mCover;
174678 u64 mHighlite;
174679 fts3SnippetDetails(&sIter, mCovered, &iPos, &iScore, &mCover,&mHighlite);
174680 assert( iScore>=0 );
174681 if( iScore>iBestScore ){
174682 pFragment->iPos = iPos;
174683 pFragment->hlmask = mHighlite;
174684 pFragment->covered = mCover;
174685 iBestScore = iScore;
174686 }
174687 }
174688
174689 *piScore = iBestScore;
174690 }
174691 sqlite3_free(sIter.aPhrase);
174692 return rc;
174693}
174694
174695
174696/*
174697** Append a string to the string-buffer passed as the first argument.
174698**
174699** If nAppend is negative, then the length of the string zAppend is
174700** determined using strlen().
174701*/
174702static int fts3StringAppend(
174703 StrBuffer *pStr, /* Buffer to append to */
174704 const char *zAppend, /* Pointer to data to append to buffer */
174705 int nAppend /* Size of zAppend in bytes (or -1) */
174706){
174707 if( nAppend<0 ){
174708 nAppend = (int)strlen(zAppend);
174709 }
174710
174711 /* If there is insufficient space allocated at StrBuffer.z, use realloc()
174712 ** to grow the buffer until so that it is big enough to accomadate the
174713 ** appended data.
174714 */
174715 if( pStr->n+nAppend+1>=pStr->nAlloc ){
174716 int nAlloc = pStr->nAlloc+nAppend+100;
174717 char *zNew = sqlite3_realloc(pStr->z, nAlloc);
174718 if( !zNew ){
174719 return SQLITE_NOMEM;
174720 }
174721 pStr->z = zNew;
174722 pStr->nAlloc = nAlloc;
174723 }
174724 assert( pStr->z!=0 && (pStr->nAlloc >= pStr->n+nAppend+1) );
174725
174726 /* Append the data to the string buffer. */
174727 memcpy(&pStr->z[pStr->n], zAppend, nAppend);
174728 pStr->n += nAppend;
174729 pStr->z[pStr->n] = '\0';
174730
174731 return SQLITE_OK;
174732}
174733
174734/*
174735** The fts3BestSnippet() function often selects snippets that end with a
174736** query term. That is, the final term of the snippet is always a term
174737** that requires highlighting. For example, if 'X' is a highlighted term
174738** and '.' is a non-highlighted term, BestSnippet() may select:
174739**
174740** ........X.....X
174741**
174742** This function "shifts" the beginning of the snippet forward in the
174743** document so that there are approximately the same number of
174744** non-highlighted terms to the right of the final highlighted term as there
174745** are to the left of the first highlighted term. For example, to this:
174746**
174747** ....X.....X....
174748**
174749** This is done as part of extracting the snippet text, not when selecting
174750** the snippet. Snippet selection is done based on doclists only, so there
174751** is no way for fts3BestSnippet() to know whether or not the document
174752** actually contains terms that follow the final highlighted term.
174753*/
174754static int fts3SnippetShift(
174755 Fts3Table *pTab, /* FTS3 table snippet comes from */
174756 int iLangid, /* Language id to use in tokenizing */
174757 int nSnippet, /* Number of tokens desired for snippet */
174758 const char *zDoc, /* Document text to extract snippet from */
174759 int nDoc, /* Size of buffer zDoc in bytes */
174760 int *piPos, /* IN/OUT: First token of snippet */
174761 u64 *pHlmask /* IN/OUT: Mask of tokens to highlight */
174762){
174763 u64 hlmask = *pHlmask; /* Local copy of initial highlight-mask */
174764
174765 if( hlmask ){
174766 int nLeft; /* Tokens to the left of first highlight */
174767 int nRight; /* Tokens to the right of last highlight */
174768 int nDesired; /* Ideal number of tokens to shift forward */
174769
174770 for(nLeft=0; !(hlmask & ((u64)1 << nLeft)); nLeft++);
174771 for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++);
174772 nDesired = (nLeft-nRight)/2;
174773
174774 /* Ideally, the start of the snippet should be pushed forward in the
174775 ** document nDesired tokens. This block checks if there are actually
174776 ** nDesired tokens to the right of the snippet. If so, *piPos and
174777 ** *pHlMask are updated to shift the snippet nDesired tokens to the
174778 ** right. Otherwise, the snippet is shifted by the number of tokens
174779 ** available.
174780 */
174781 if( nDesired>0 ){
174782 int nShift; /* Number of tokens to shift snippet by */
174783 int iCurrent = 0; /* Token counter */
174784 int rc; /* Return Code */
174785 sqlite3_tokenizer_module *pMod;
174786 sqlite3_tokenizer_cursor *pC;
174787 pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
174788
174789 /* Open a cursor on zDoc/nDoc. Check if there are (nSnippet+nDesired)
174790 ** or more tokens in zDoc/nDoc.
174791 */
174792 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, iLangid, zDoc, nDoc, &pC);
174793 if( rc!=SQLITE_OK ){
174794 return rc;
174795 }
174796 while( rc==SQLITE_OK && iCurrent<(nSnippet+nDesired) ){
174797 const char *ZDUMMY; int DUMMY1 = 0, DUMMY2 = 0, DUMMY3 = 0;
174798 rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent);
174799 }
174800 pMod->xClose(pC);
174801 if( rc!=SQLITE_OK && rc!=SQLITE_DONE ){ return rc; }
174802
174803 nShift = (rc==SQLITE_DONE)+iCurrent-nSnippet;
174804 assert( nShift<=nDesired );
174805 if( nShift>0 ){
174806 *piPos += nShift;
174807 *pHlmask = hlmask >> nShift;
174808 }
174809 }
174810 }
174811 return SQLITE_OK;
174812}
174813
174814/*
174815** Extract the snippet text for fragment pFragment from cursor pCsr and
174816** append it to string buffer pOut.
174817*/
174818static int fts3SnippetText(
174819 Fts3Cursor *pCsr, /* FTS3 Cursor */
174820 SnippetFragment *pFragment, /* Snippet to extract */
174821 int iFragment, /* Fragment number */
174822 int isLast, /* True for final fragment in snippet */
174823 int nSnippet, /* Number of tokens in extracted snippet */
174824 const char *zOpen, /* String inserted before highlighted term */
174825 const char *zClose, /* String inserted after highlighted term */
174826 const char *zEllipsis, /* String inserted between snippets */
174827 StrBuffer *pOut /* Write output here */
174828){
174829 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
174830 int rc; /* Return code */
174831 const char *zDoc; /* Document text to extract snippet from */
174832 int nDoc; /* Size of zDoc in bytes */
174833 int iCurrent = 0; /* Current token number of document */
174834 int iEnd = 0; /* Byte offset of end of current token */
174835 int isShiftDone = 0; /* True after snippet is shifted */
174836 int iPos = pFragment->iPos; /* First token of snippet */
174837 u64 hlmask = pFragment->hlmask; /* Highlight-mask for snippet */
174838 int iCol = pFragment->iCol+1; /* Query column to extract text from */
174839 sqlite3_tokenizer_module *pMod; /* Tokenizer module methods object */
174840 sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor open on zDoc/nDoc */
174841
174842 zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol);
174843 if( zDoc==0 ){
174844 if( sqlite3_column_type(pCsr->pStmt, iCol)!=SQLITE_NULL ){
174845 return SQLITE_NOMEM;
174846 }
174847 return SQLITE_OK;
174848 }
174849 nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol);
174850
174851 /* Open a token cursor on the document. */
174852 pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
174853 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid, zDoc,nDoc,&pC);
174854 if( rc!=SQLITE_OK ){
174855 return rc;
174856 }
174857
174858 while( rc==SQLITE_OK ){
174859 const char *ZDUMMY; /* Dummy argument used with tokenizer */
174860 int DUMMY1 = -1; /* Dummy argument used with tokenizer */
174861 int iBegin = 0; /* Offset in zDoc of start of token */
174862 int iFin = 0; /* Offset in zDoc of end of token */
174863 int isHighlight = 0; /* True for highlighted terms */
174864
174865 /* Variable DUMMY1 is initialized to a negative value above. Elsewhere
174866 ** in the FTS code the variable that the third argument to xNext points to
174867 ** is initialized to zero before the first (*but not necessarily
174868 ** subsequent*) call to xNext(). This is done for a particular application
174869 ** that needs to know whether or not the tokenizer is being used for
174870 ** snippet generation or for some other purpose.
174871 **
174872 ** Extreme care is required when writing code to depend on this
174873 ** initialization. It is not a documented part of the tokenizer interface.
174874 ** If a tokenizer is used directly by any code outside of FTS, this
174875 ** convention might not be respected. */
174876 rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent);
174877 if( rc!=SQLITE_OK ){
174878 if( rc==SQLITE_DONE ){
174879 /* Special case - the last token of the snippet is also the last token
174880 ** of the column. Append any punctuation that occurred between the end
174881 ** of the previous token and the end of the document to the output.
174882 ** Then break out of the loop. */
174883 rc = fts3StringAppend(pOut, &zDoc[iEnd], -1);
174884 }
174885 break;
174886 }
174887 if( iCurrent<iPos ){ continue; }
174888
174889 if( !isShiftDone ){
174890 int n = nDoc - iBegin;
174891 rc = fts3SnippetShift(
174892 pTab, pCsr->iLangid, nSnippet, &zDoc[iBegin], n, &iPos, &hlmask
174893 );
174894 isShiftDone = 1;
174895
174896 /* Now that the shift has been done, check if the initial "..." are
174897 ** required. They are required if (a) this is not the first fragment,
174898 ** or (b) this fragment does not begin at position 0 of its column.
174899 */
174900 if( rc==SQLITE_OK ){
174901 if( iPos>0 || iFragment>0 ){
174902 rc = fts3StringAppend(pOut, zEllipsis, -1);
174903 }else if( iBegin ){
174904 rc = fts3StringAppend(pOut, zDoc, iBegin);
174905 }
174906 }
174907 if( rc!=SQLITE_OK || iCurrent<iPos ) continue;
174908 }
174909
174910 if( iCurrent>=(iPos+nSnippet) ){
174911 if( isLast ){
174912 rc = fts3StringAppend(pOut, zEllipsis, -1);
174913 }
174914 break;
174915 }
174916
174917 /* Set isHighlight to true if this term should be highlighted. */
174918 isHighlight = (hlmask & ((u64)1 << (iCurrent-iPos)))!=0;
174919
174920 if( iCurrent>iPos ) rc = fts3StringAppend(pOut, &zDoc[iEnd], iBegin-iEnd);
174921 if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zOpen, -1);
174922 if( rc==SQLITE_OK ) rc = fts3StringAppend(pOut, &zDoc[iBegin], iFin-iBegin);
174923 if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zClose, -1);
174924
174925 iEnd = iFin;
174926 }
174927
174928 pMod->xClose(pC);
174929 return rc;
174930}
174931
174932
174933/*
174934** This function is used to count the entries in a column-list (a
174935** delta-encoded list of term offsets within a single column of a single
174936** row). When this function is called, *ppCollist should point to the
174937** beginning of the first varint in the column-list (the varint that
174938** contains the position of the first matching term in the column data).
174939** Before returning, *ppCollist is set to point to the first byte after
174940** the last varint in the column-list (either the 0x00 signifying the end
174941** of the position-list, or the 0x01 that precedes the column number of
174942** the next column in the position-list).
174943**
174944** The number of elements in the column-list is returned.
174945*/
174946static int fts3ColumnlistCount(char **ppCollist){
174947 char *pEnd = *ppCollist;
174948 char c = 0;
174949 int nEntry = 0;
174950
174951 /* A column-list is terminated by either a 0x01 or 0x00. */
174952 while( 0xFE & (*pEnd | c) ){
174953 c = *pEnd++ & 0x80;
174954 if( !c ) nEntry++;
174955 }
174956
174957 *ppCollist = pEnd;
174958 return nEntry;
174959}
174960
174961/*
174962** This function gathers 'y' or 'b' data for a single phrase.
174963*/
174964static void fts3ExprLHits(
174965 Fts3Expr *pExpr, /* Phrase expression node */
174966 MatchInfo *p /* Matchinfo context */
174967){
174968 Fts3Table *pTab = (Fts3Table *)p->pCursor->base.pVtab;
174969 int iStart;
174970 Fts3Phrase *pPhrase = pExpr->pPhrase;
174971 char *pIter = pPhrase->doclist.pList;
174972 int iCol = 0;
174973
174974 assert( p->flag==FTS3_MATCHINFO_LHITS_BM || p->flag==FTS3_MATCHINFO_LHITS );
174975 if( p->flag==FTS3_MATCHINFO_LHITS ){
174976 iStart = pExpr->iPhrase * p->nCol;
174977 }else{
174978 iStart = pExpr->iPhrase * ((p->nCol + 31) / 32);
174979 }
174980
174981 while( 1 ){
174982 int nHit = fts3ColumnlistCount(&pIter);
174983 if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){
174984 if( p->flag==FTS3_MATCHINFO_LHITS ){
174985 p->aMatchinfo[iStart + iCol] = (u32)nHit;
174986 }else if( nHit ){
174987 p->aMatchinfo[iStart + (iCol+1)/32] |= (1 << (iCol&0x1F));
174988 }
174989 }
174990 assert( *pIter==0x00 || *pIter==0x01 );
174991 if( *pIter!=0x01 ) break;
174992 pIter++;
174993 pIter += fts3GetVarint32(pIter, &iCol);
174994 }
174995}
174996
174997/*
174998** Gather the results for matchinfo directives 'y' and 'b'.
174999*/
175000static void fts3ExprLHitGather(
175001 Fts3Expr *pExpr,
175002 MatchInfo *p
175003){
175004 assert( (pExpr->pLeft==0)==(pExpr->pRight==0) );
175005 if( pExpr->bEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){
175006 if( pExpr->pLeft ){
175007 fts3ExprLHitGather(pExpr->pLeft, p);
175008 fts3ExprLHitGather(pExpr->pRight, p);
175009 }else{
175010 fts3ExprLHits(pExpr, p);
175011 }
175012 }
175013}
175014
175015/*
175016** fts3ExprIterate() callback used to collect the "global" matchinfo stats
175017** for a single query.
175018**
175019** fts3ExprIterate() callback to load the 'global' elements of a
175020** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements
175021** of the matchinfo array that are constant for all rows returned by the
175022** current query.
175023**
175024** Argument pCtx is actually a pointer to a struct of type MatchInfo. This
175025** function populates Matchinfo.aMatchinfo[] as follows:
175026**
175027** for(iCol=0; iCol<nCol; iCol++){
175028** aMatchinfo[3*iPhrase*nCol + 3*iCol + 1] = X;
175029** aMatchinfo[3*iPhrase*nCol + 3*iCol + 2] = Y;
175030** }
175031**
175032** where X is the number of matches for phrase iPhrase is column iCol of all
175033** rows of the table. Y is the number of rows for which column iCol contains
175034** at least one instance of phrase iPhrase.
175035**
175036** If the phrase pExpr consists entirely of deferred tokens, then all X and
175037** Y values are set to nDoc, where nDoc is the number of documents in the
175038** file system. This is done because the full-text index doclist is required
175039** to calculate these values properly, and the full-text index doclist is
175040** not available for deferred tokens.
175041*/
175042static int fts3ExprGlobalHitsCb(
175043 Fts3Expr *pExpr, /* Phrase expression node */
175044 int iPhrase, /* Phrase number (numbered from zero) */
175045 void *pCtx /* Pointer to MatchInfo structure */
175046){
175047 MatchInfo *p = (MatchInfo *)pCtx;
175048 return sqlite3Fts3EvalPhraseStats(
175049 p->pCursor, pExpr, &p->aMatchinfo[3*iPhrase*p->nCol]
175050 );
175051}
175052
175053/*
175054** fts3ExprIterate() callback used to collect the "local" part of the
175055** FTS3_MATCHINFO_HITS array. The local stats are those elements of the
175056** array that are different for each row returned by the query.
175057*/
175058static int fts3ExprLocalHitsCb(
175059 Fts3Expr *pExpr, /* Phrase expression node */
175060 int iPhrase, /* Phrase number */
175061 void *pCtx /* Pointer to MatchInfo structure */
175062){
175063 int rc = SQLITE_OK;
175064 MatchInfo *p = (MatchInfo *)pCtx;
175065 int iStart = iPhrase * p->nCol * 3;
175066 int i;
175067
175068 for(i=0; i<p->nCol && rc==SQLITE_OK; i++){
175069 char *pCsr;
175070 rc = sqlite3Fts3EvalPhrasePoslist(p->pCursor, pExpr, i, &pCsr);
175071 if( pCsr ){
175072 p->aMatchinfo[iStart+i*3] = fts3ColumnlistCount(&pCsr);
175073 }else{
175074 p->aMatchinfo[iStart+i*3] = 0;
175075 }
175076 }
175077
175078 return rc;
175079}
175080
175081static int fts3MatchinfoCheck(
175082 Fts3Table *pTab,
175083 char cArg,
175084 char **pzErr
175085){
175086 if( (cArg==FTS3_MATCHINFO_NPHRASE)
175087 || (cArg==FTS3_MATCHINFO_NCOL)
175088 || (cArg==FTS3_MATCHINFO_NDOC && pTab->bFts4)
175089 || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4)
175090 || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
175091 || (cArg==FTS3_MATCHINFO_LCS)
175092 || (cArg==FTS3_MATCHINFO_HITS)
175093 || (cArg==FTS3_MATCHINFO_LHITS)
175094 || (cArg==FTS3_MATCHINFO_LHITS_BM)
175095 ){
175096 return SQLITE_OK;
175097 }
175098 sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo request: %c", cArg);
175099 return SQLITE_ERROR;
175100}
175101
175102static int fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
175103 int nVal; /* Number of integers output by cArg */
175104
175105 switch( cArg ){
175106 case FTS3_MATCHINFO_NDOC:
175107 case FTS3_MATCHINFO_NPHRASE:
175108 case FTS3_MATCHINFO_NCOL:
175109 nVal = 1;
175110 break;
175111
175112 case FTS3_MATCHINFO_AVGLENGTH:
175113 case FTS3_MATCHINFO_LENGTH:
175114 case FTS3_MATCHINFO_LCS:
175115 nVal = pInfo->nCol;
175116 break;
175117
175118 case FTS3_MATCHINFO_LHITS:
175119 nVal = pInfo->nCol * pInfo->nPhrase;
175120 break;
175121
175122 case FTS3_MATCHINFO_LHITS_BM:
175123 nVal = pInfo->nPhrase * ((pInfo->nCol + 31) / 32);
175124 break;
175125
175126 default:
175127 assert( cArg==FTS3_MATCHINFO_HITS );
175128 nVal = pInfo->nCol * pInfo->nPhrase * 3;
175129 break;
175130 }
175131
175132 return nVal;
175133}
175134
175135static int fts3MatchinfoSelectDoctotal(
175136 Fts3Table *pTab,
175137 sqlite3_stmt **ppStmt,
175138 sqlite3_int64 *pnDoc,
175139 const char **paLen
175140){
175141 sqlite3_stmt *pStmt;
175142 const char *a;
175143 sqlite3_int64 nDoc;
175144
175145 if( !*ppStmt ){
175146 int rc = sqlite3Fts3SelectDoctotal(pTab, ppStmt);
175147 if( rc!=SQLITE_OK ) return rc;
175148 }
175149 pStmt = *ppStmt;
175150 assert( sqlite3_data_count(pStmt)==1 );
175151
175152 a = sqlite3_column_blob(pStmt, 0);
175153 a += sqlite3Fts3GetVarint(a, &nDoc);
175154 if( nDoc==0 ) return FTS_CORRUPT_VTAB;
175155 *pnDoc = (u32)nDoc;
175156
175157 if( paLen ) *paLen = a;
175158 return SQLITE_OK;
175159}
175160
175161/*
175162** An instance of the following structure is used to store state while
175163** iterating through a multi-column position-list corresponding to the
175164** hits for a single phrase on a single row in order to calculate the
175165** values for a matchinfo() FTS3_MATCHINFO_LCS request.
175166*/
175167typedef struct LcsIterator LcsIterator;
175168struct LcsIterator {
175169 Fts3Expr *pExpr; /* Pointer to phrase expression */
175170 int iPosOffset; /* Tokens count up to end of this phrase */
175171 char *pRead; /* Cursor used to iterate through aDoclist */
175172 int iPos; /* Current position */
175173};
175174
175175/*
175176** If LcsIterator.iCol is set to the following value, the iterator has
175177** finished iterating through all offsets for all columns.
175178*/
175179#define LCS_ITERATOR_FINISHED 0x7FFFFFFF;
175180
175181static int fts3MatchinfoLcsCb(
175182 Fts3Expr *pExpr, /* Phrase expression node */
175183 int iPhrase, /* Phrase number (numbered from zero) */
175184 void *pCtx /* Pointer to MatchInfo structure */
175185){
175186 LcsIterator *aIter = (LcsIterator *)pCtx;
175187 aIter[iPhrase].pExpr = pExpr;
175188 return SQLITE_OK;
175189}
175190
175191/*
175192** Advance the iterator passed as an argument to the next position. Return
175193** 1 if the iterator is at EOF or if it now points to the start of the
175194** position list for the next column.
175195*/
175196static int fts3LcsIteratorAdvance(LcsIterator *pIter){
175197 char *pRead = pIter->pRead;
175198 sqlite3_int64 iRead;
175199 int rc = 0;
175200
175201 pRead += sqlite3Fts3GetVarint(pRead, &iRead);
175202 if( iRead==0 || iRead==1 ){
175203 pRead = 0;
175204 rc = 1;
175205 }else{
175206 pIter->iPos += (int)(iRead-2);
175207 }
175208
175209 pIter->pRead = pRead;
175210 return rc;
175211}
175212
175213/*
175214** This function implements the FTS3_MATCHINFO_LCS matchinfo() flag.
175215**
175216** If the call is successful, the longest-common-substring lengths for each
175217** column are written into the first nCol elements of the pInfo->aMatchinfo[]
175218** array before returning. SQLITE_OK is returned in this case.
175219**
175220** Otherwise, if an error occurs, an SQLite error code is returned and the
175221** data written to the first nCol elements of pInfo->aMatchinfo[] is
175222** undefined.
175223*/
175224static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){
175225 LcsIterator *aIter;
175226 int i;
175227 int iCol;
175228 int nToken = 0;
175229
175230 /* Allocate and populate the array of LcsIterator objects. The array
175231 ** contains one element for each matchable phrase in the query.
175232 **/
175233 aIter = sqlite3_malloc(sizeof(LcsIterator) * pCsr->nPhrase);
175234 if( !aIter ) return SQLITE_NOMEM;
175235 memset(aIter, 0, sizeof(LcsIterator) * pCsr->nPhrase);
175236 (void)fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter);
175237
175238 for(i=0; i<pInfo->nPhrase; i++){
175239 LcsIterator *pIter = &aIter[i];
175240 nToken -= pIter->pExpr->pPhrase->nToken;
175241 pIter->iPosOffset = nToken;
175242 }
175243
175244 for(iCol=0; iCol<pInfo->nCol; iCol++){
175245 int nLcs = 0; /* LCS value for this column */
175246 int nLive = 0; /* Number of iterators in aIter not at EOF */
175247
175248 for(i=0; i<pInfo->nPhrase; i++){
175249 int rc;
175250 LcsIterator *pIt = &aIter[i];
175251 rc = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol, &pIt->pRead);
175252 if( rc!=SQLITE_OK ) return rc;
175253 if( pIt->pRead ){
175254 pIt->iPos = pIt->iPosOffset;
175255 fts3LcsIteratorAdvance(&aIter[i]);
175256 nLive++;
175257 }
175258 }
175259
175260 while( nLive>0 ){
175261 LcsIterator *pAdv = 0; /* The iterator to advance by one position */
175262 int nThisLcs = 0; /* LCS for the current iterator positions */
175263
175264 for(i=0; i<pInfo->nPhrase; i++){
175265 LcsIterator *pIter = &aIter[i];
175266 if( pIter->pRead==0 ){
175267 /* This iterator is already at EOF for this column. */
175268 nThisLcs = 0;
175269 }else{
175270 if( pAdv==0 || pIter->iPos<pAdv->iPos ){
175271 pAdv = pIter;
175272 }
175273 if( nThisLcs==0 || pIter->iPos==pIter[-1].iPos ){
175274 nThisLcs++;
175275 }else{
175276 nThisLcs = 1;
175277 }
175278 if( nThisLcs>nLcs ) nLcs = nThisLcs;
175279 }
175280 }
175281 if( fts3LcsIteratorAdvance(pAdv) ) nLive--;
175282 }
175283
175284 pInfo->aMatchinfo[iCol] = nLcs;
175285 }
175286
175287 sqlite3_free(aIter);
175288 return SQLITE_OK;
175289}
175290
175291/*
175292** Populate the buffer pInfo->aMatchinfo[] with an array of integers to
175293** be returned by the matchinfo() function. Argument zArg contains the
175294** format string passed as the second argument to matchinfo (or the
175295** default value "pcx" if no second argument was specified). The format
175296** string has already been validated and the pInfo->aMatchinfo[] array
175297** is guaranteed to be large enough for the output.
175298**
175299** If bGlobal is true, then populate all fields of the matchinfo() output.
175300** If it is false, then assume that those fields that do not change between
175301** rows (i.e. FTS3_MATCHINFO_NPHRASE, NCOL, NDOC, AVGLENGTH and part of HITS)
175302** have already been populated.
175303**
175304** Return SQLITE_OK if successful, or an SQLite error code if an error
175305** occurs. If a value other than SQLITE_OK is returned, the state the
175306** pInfo->aMatchinfo[] buffer is left in is undefined.
175307*/
175308static int fts3MatchinfoValues(
175309 Fts3Cursor *pCsr, /* FTS3 cursor object */
175310 int bGlobal, /* True to grab the global stats */
175311 MatchInfo *pInfo, /* Matchinfo context object */
175312 const char *zArg /* Matchinfo format string */
175313){
175314 int rc = SQLITE_OK;
175315 int i;
175316 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
175317 sqlite3_stmt *pSelect = 0;
175318
175319 for(i=0; rc==SQLITE_OK && zArg[i]; i++){
175320 pInfo->flag = zArg[i];
175321 switch( zArg[i] ){
175322 case FTS3_MATCHINFO_NPHRASE:
175323 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
175324 break;
175325
175326 case FTS3_MATCHINFO_NCOL:
175327 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol;
175328 break;
175329
175330 case FTS3_MATCHINFO_NDOC:
175331 if( bGlobal ){
175332 sqlite3_int64 nDoc = 0;
175333 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, 0);
175334 pInfo->aMatchinfo[0] = (u32)nDoc;
175335 }
175336 break;
175337
175338 case FTS3_MATCHINFO_AVGLENGTH:
175339 if( bGlobal ){
175340 sqlite3_int64 nDoc; /* Number of rows in table */
175341 const char *a; /* Aggregate column length array */
175342
175343 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a);
175344 if( rc==SQLITE_OK ){
175345 int iCol;
175346 for(iCol=0; iCol<pInfo->nCol; iCol++){
175347 u32 iVal;
175348 sqlite3_int64 nToken;
175349 a += sqlite3Fts3GetVarint(a, &nToken);
175350 iVal = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc);
175351 pInfo->aMatchinfo[iCol] = iVal;
175352 }
175353 }
175354 }
175355 break;
175356
175357 case FTS3_MATCHINFO_LENGTH: {
175358 sqlite3_stmt *pSelectDocsize = 0;
175359 rc = sqlite3Fts3SelectDocsize(pTab, pCsr->iPrevId, &pSelectDocsize);
175360 if( rc==SQLITE_OK ){
175361 int iCol;
175362 const char *a = sqlite3_column_blob(pSelectDocsize, 0);
175363 for(iCol=0; iCol<pInfo->nCol; iCol++){
175364 sqlite3_int64 nToken;
175365 a += sqlite3Fts3GetVarint(a, &nToken);
175366 pInfo->aMatchinfo[iCol] = (u32)nToken;
175367 }
175368 }
175369 sqlite3_reset(pSelectDocsize);
175370 break;
175371 }
175372
175373 case FTS3_MATCHINFO_LCS:
175374 rc = fts3ExprLoadDoclists(pCsr, 0, 0);
175375 if( rc==SQLITE_OK ){
175376 rc = fts3MatchinfoLcs(pCsr, pInfo);
175377 }
175378 break;
175379
175380 case FTS3_MATCHINFO_LHITS_BM:
175381 case FTS3_MATCHINFO_LHITS: {
175382 int nZero = fts3MatchinfoSize(pInfo, zArg[i]) * sizeof(u32);
175383 memset(pInfo->aMatchinfo, 0, nZero);
175384 fts3ExprLHitGather(pCsr->pExpr, pInfo);
175385 break;
175386 }
175387
175388 default: {
175389 Fts3Expr *pExpr;
175390 assert( zArg[i]==FTS3_MATCHINFO_HITS );
175391 pExpr = pCsr->pExpr;
175392 rc = fts3ExprLoadDoclists(pCsr, 0, 0);
175393 if( rc!=SQLITE_OK ) break;
175394 if( bGlobal ){
175395 if( pCsr->pDeferred ){
175396 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc, 0);
175397 if( rc!=SQLITE_OK ) break;
175398 }
175399 rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo);
175400 sqlite3Fts3EvalTestDeferred(pCsr, &rc);
175401 if( rc!=SQLITE_OK ) break;
175402 }
175403 (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo);
175404 break;
175405 }
175406 }
175407
175408 pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, zArg[i]);
175409 }
175410
175411 sqlite3_reset(pSelect);
175412 return rc;
175413}
175414
175415
175416/*
175417** Populate pCsr->aMatchinfo[] with data for the current row. The
175418** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
175419*/
175420static void fts3GetMatchinfo(
175421 sqlite3_context *pCtx, /* Return results here */
175422 Fts3Cursor *pCsr, /* FTS3 Cursor object */
175423 const char *zArg /* Second argument to matchinfo() function */
175424){
175425 MatchInfo sInfo;
175426 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
175427 int rc = SQLITE_OK;
175428 int bGlobal = 0; /* Collect 'global' stats as well as local */
175429
175430 u32 *aOut = 0;
175431 void (*xDestroyOut)(void*) = 0;
175432
175433 memset(&sInfo, 0, sizeof(MatchInfo));
175434 sInfo.pCursor = pCsr;
175435 sInfo.nCol = pTab->nColumn;
175436
175437 /* If there is cached matchinfo() data, but the format string for the
175438 ** cache does not match the format string for this request, discard
175439 ** the cached data. */
175440 if( pCsr->pMIBuffer && strcmp(pCsr->pMIBuffer->zMatchinfo, zArg) ){
175441 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
175442 pCsr->pMIBuffer = 0;
175443 }
175444
175445 /* If Fts3Cursor.pMIBuffer is NULL, then this is the first time the
175446 ** matchinfo function has been called for this query. In this case
175447 ** allocate the array used to accumulate the matchinfo data and
175448 ** initialize those elements that are constant for every row.
175449 */
175450 if( pCsr->pMIBuffer==0 ){
175451 int nMatchinfo = 0; /* Number of u32 elements in match-info */
175452 int i; /* Used to iterate through zArg */
175453
175454 /* Determine the number of phrases in the query */
175455 pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr);
175456 sInfo.nPhrase = pCsr->nPhrase;
175457
175458 /* Determine the number of integers in the buffer returned by this call. */
175459 for(i=0; zArg[i]; i++){
175460 char *zErr = 0;
175461 if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
175462 sqlite3_result_error(pCtx, zErr, -1);
175463 sqlite3_free(zErr);
175464 return;
175465 }
175466 nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]);
175467 }
175468
175469 /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */
175470 pCsr->pMIBuffer = fts3MIBufferNew(nMatchinfo, zArg);
175471 if( !pCsr->pMIBuffer ) rc = SQLITE_NOMEM;
175472
175473 pCsr->isMatchinfoNeeded = 1;
175474 bGlobal = 1;
175475 }
175476
175477 if( rc==SQLITE_OK ){
175478 xDestroyOut = fts3MIBufferAlloc(pCsr->pMIBuffer, &aOut);
175479 if( xDestroyOut==0 ){
175480 rc = SQLITE_NOMEM;
175481 }
175482 }
175483
175484 if( rc==SQLITE_OK ){
175485 sInfo.aMatchinfo = aOut;
175486 sInfo.nPhrase = pCsr->nPhrase;
175487 rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg);
175488 if( bGlobal ){
175489 fts3MIBufferSetGlobal(pCsr->pMIBuffer);
175490 }
175491 }
175492
175493 if( rc!=SQLITE_OK ){
175494 sqlite3_result_error_code(pCtx, rc);
175495 if( xDestroyOut ) xDestroyOut(aOut);
175496 }else{
175497 int n = pCsr->pMIBuffer->nElem * sizeof(u32);
175498 sqlite3_result_blob(pCtx, aOut, n, xDestroyOut);
175499 }
175500}
175501
175502/*
175503** Implementation of snippet() function.
175504*/
175505SQLITE_PRIVATE void sqlite3Fts3Snippet(
175506 sqlite3_context *pCtx, /* SQLite function call context */
175507 Fts3Cursor *pCsr, /* Cursor object */
175508 const char *zStart, /* Snippet start text - "<b>" */
175509 const char *zEnd, /* Snippet end text - "</b>" */
175510 const char *zEllipsis, /* Snippet ellipsis text - "<b>...</b>" */
175511 int iCol, /* Extract snippet from this column */
175512 int nToken /* Approximate number of tokens in snippet */
175513){
175514 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
175515 int rc = SQLITE_OK;
175516 int i;
175517 StrBuffer res = {0, 0, 0};
175518
175519 /* The returned text includes up to four fragments of text extracted from
175520 ** the data in the current row. The first iteration of the for(...) loop
175521 ** below attempts to locate a single fragment of text nToken tokens in
175522 ** size that contains at least one instance of all phrases in the query
175523 ** expression that appear in the current row. If such a fragment of text
175524 ** cannot be found, the second iteration of the loop attempts to locate
175525 ** a pair of fragments, and so on.
175526 */
175527 int nSnippet = 0; /* Number of fragments in this snippet */
175528 SnippetFragment aSnippet[4]; /* Maximum of 4 fragments per snippet */
175529 int nFToken = -1; /* Number of tokens in each fragment */
175530
175531 if( !pCsr->pExpr ){
175532 sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
175533 return;
175534 }
175535
175536 for(nSnippet=1; 1; nSnippet++){
175537
175538 int iSnip; /* Loop counter 0..nSnippet-1 */
175539 u64 mCovered = 0; /* Bitmask of phrases covered by snippet */
175540 u64 mSeen = 0; /* Bitmask of phrases seen by BestSnippet() */
175541
175542 if( nToken>=0 ){
175543 nFToken = (nToken+nSnippet-1) / nSnippet;
175544 }else{
175545 nFToken = -1 * nToken;
175546 }
175547
175548 for(iSnip=0; iSnip<nSnippet; iSnip++){
175549 int iBestScore = -1; /* Best score of columns checked so far */
175550 int iRead; /* Used to iterate through columns */
175551 SnippetFragment *pFragment = &aSnippet[iSnip];
175552
175553 memset(pFragment, 0, sizeof(*pFragment));
175554
175555 /* Loop through all columns of the table being considered for snippets.
175556 ** If the iCol argument to this function was negative, this means all
175557 ** columns of the FTS3 table. Otherwise, only column iCol is considered.
175558 */
175559 for(iRead=0; iRead<pTab->nColumn; iRead++){
175560 SnippetFragment sF = {0, 0, 0, 0};
175561 int iS = 0;
175562 if( iCol>=0 && iRead!=iCol ) continue;
175563
175564 /* Find the best snippet of nFToken tokens in column iRead. */
175565 rc = fts3BestSnippet(nFToken, pCsr, iRead, mCovered, &mSeen, &sF, &iS);
175566 if( rc!=SQLITE_OK ){
175567 goto snippet_out;
175568 }
175569 if( iS>iBestScore ){
175570 *pFragment = sF;
175571 iBestScore = iS;
175572 }
175573 }
175574
175575 mCovered |= pFragment->covered;
175576 }
175577
175578 /* If all query phrases seen by fts3BestSnippet() are present in at least
175579 ** one of the nSnippet snippet fragments, break out of the loop.
175580 */
175581 assert( (mCovered&mSeen)==mCovered );
175582 if( mSeen==mCovered || nSnippet==SizeofArray(aSnippet) ) break;
175583 }
175584
175585 assert( nFToken>0 );
175586
175587 for(i=0; i<nSnippet && rc==SQLITE_OK; i++){
175588 rc = fts3SnippetText(pCsr, &aSnippet[i],
175589 i, (i==nSnippet-1), nFToken, zStart, zEnd, zEllipsis, &res
175590 );
175591 }
175592
175593 snippet_out:
175594 sqlite3Fts3SegmentsClose(pTab);
175595 if( rc!=SQLITE_OK ){
175596 sqlite3_result_error_code(pCtx, rc);
175597 sqlite3_free(res.z);
175598 }else{
175599 sqlite3_result_text(pCtx, res.z, -1, sqlite3_free);
175600 }
175601}
175602
175603
175604typedef struct TermOffset TermOffset;
175605typedef struct TermOffsetCtx TermOffsetCtx;
175606
175607struct TermOffset {
175608 char *pList; /* Position-list */
175609 int iPos; /* Position just read from pList */
175610 int iOff; /* Offset of this term from read positions */
175611};
175612
175613struct TermOffsetCtx {
175614 Fts3Cursor *pCsr;
175615 int iCol; /* Column of table to populate aTerm for */
175616 int iTerm;
175617 sqlite3_int64 iDocid;
175618 TermOffset *aTerm;
175619};
175620
175621/*
175622** This function is an fts3ExprIterate() callback used by sqlite3Fts3Offsets().
175623*/
175624static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){
175625 TermOffsetCtx *p = (TermOffsetCtx *)ctx;
175626 int nTerm; /* Number of tokens in phrase */
175627 int iTerm; /* For looping through nTerm phrase terms */
175628 char *pList; /* Pointer to position list for phrase */
175629 int iPos = 0; /* First position in position-list */
175630 int rc;
175631
175632 UNUSED_PARAMETER(iPhrase);
175633 rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pList);
175634 nTerm = pExpr->pPhrase->nToken;
175635 if( pList ){
175636 fts3GetDeltaPosition(&pList, &iPos);
175637 assert( iPos>=0 );
175638 }
175639
175640 for(iTerm=0; iTerm<nTerm; iTerm++){
175641 TermOffset *pT = &p->aTerm[p->iTerm++];
175642 pT->iOff = nTerm-iTerm-1;
175643 pT->pList = pList;
175644 pT->iPos = iPos;
175645 }
175646
175647 return rc;
175648}
175649
175650/*
175651** Implementation of offsets() function.
175652*/
175653SQLITE_PRIVATE void sqlite3Fts3Offsets(
175654 sqlite3_context *pCtx, /* SQLite function call context */
175655 Fts3Cursor *pCsr /* Cursor object */
175656){
175657 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
175658 sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule;
175659 int rc; /* Return Code */
175660 int nToken; /* Number of tokens in query */
175661 int iCol; /* Column currently being processed */
175662 StrBuffer res = {0, 0, 0}; /* Result string */
175663 TermOffsetCtx sCtx; /* Context for fts3ExprTermOffsetInit() */
175664
175665 if( !pCsr->pExpr ){
175666 sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
175667 return;
175668 }
175669
175670 memset(&sCtx, 0, sizeof(sCtx));
175671 assert( pCsr->isRequireSeek==0 );
175672
175673 /* Count the number of terms in the query */
175674 rc = fts3ExprLoadDoclists(pCsr, 0, &nToken);
175675 if( rc!=SQLITE_OK ) goto offsets_out;
175676
175677 /* Allocate the array of TermOffset iterators. */
175678 sCtx.aTerm = (TermOffset *)sqlite3_malloc(sizeof(TermOffset)*nToken);
175679 if( 0==sCtx.aTerm ){
175680 rc = SQLITE_NOMEM;
175681 goto offsets_out;
175682 }
175683 sCtx.iDocid = pCsr->iPrevId;
175684 sCtx.pCsr = pCsr;
175685
175686 /* Loop through the table columns, appending offset information to
175687 ** string-buffer res for each column.
175688 */
175689 for(iCol=0; iCol<pTab->nColumn; iCol++){
175690 sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor */
175691 const char *ZDUMMY; /* Dummy argument used with xNext() */
175692 int NDUMMY = 0; /* Dummy argument used with xNext() */
175693 int iStart = 0;
175694 int iEnd = 0;
175695 int iCurrent = 0;
175696 const char *zDoc;
175697 int nDoc;
175698
175699 /* Initialize the contents of sCtx.aTerm[] for column iCol. There is
175700 ** no way that this operation can fail, so the return code from
175701 ** fts3ExprIterate() can be discarded.
175702 */
175703 sCtx.iCol = iCol;
175704 sCtx.iTerm = 0;
175705 (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void*)&sCtx);
175706
175707 /* Retreive the text stored in column iCol. If an SQL NULL is stored
175708 ** in column iCol, jump immediately to the next iteration of the loop.
175709 ** If an OOM occurs while retrieving the data (this can happen if SQLite
175710 ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM
175711 ** to the caller.
175712 */
175713 zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol+1);
175714 nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
175715 if( zDoc==0 ){
175716 if( sqlite3_column_type(pCsr->pStmt, iCol+1)==SQLITE_NULL ){
175717 continue;
175718 }
175719 rc = SQLITE_NOMEM;
175720 goto offsets_out;
175721 }
175722
175723 /* Initialize a tokenizer iterator to iterate through column iCol. */
175724 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid,
175725 zDoc, nDoc, &pC
175726 );
175727 if( rc!=SQLITE_OK ) goto offsets_out;
175728
175729 rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
175730 while( rc==SQLITE_OK ){
175731 int i; /* Used to loop through terms */
175732 int iMinPos = 0x7FFFFFFF; /* Position of next token */
175733 TermOffset *pTerm = 0; /* TermOffset associated with next token */
175734
175735 for(i=0; i<nToken; i++){
175736 TermOffset *pT = &sCtx.aTerm[i];
175737 if( pT->pList && (pT->iPos-pT->iOff)<iMinPos ){
175738 iMinPos = pT->iPos-pT->iOff;
175739 pTerm = pT;
175740 }
175741 }
175742
175743 if( !pTerm ){
175744 /* All offsets for this column have been gathered. */
175745 rc = SQLITE_DONE;
175746 }else{
175747 assert( iCurrent<=iMinPos );
175748 if( 0==(0xFE&*pTerm->pList) ){
175749 pTerm->pList = 0;
175750 }else{
175751 fts3GetDeltaPosition(&pTerm->pList, &pTerm->iPos);
175752 }
175753 while( rc==SQLITE_OK && iCurrent<iMinPos ){
175754 rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
175755 }
175756 if( rc==SQLITE_OK ){
175757 char aBuffer[64];
175758 sqlite3_snprintf(sizeof(aBuffer), aBuffer,
175759 "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
175760 );
175761 rc = fts3StringAppend(&res, aBuffer, -1);
175762 }else if( rc==SQLITE_DONE && pTab->zContentTbl==0 ){
175763 rc = FTS_CORRUPT_VTAB;
175764 }
175765 }
175766 }
175767 if( rc==SQLITE_DONE ){
175768 rc = SQLITE_OK;
175769 }
175770
175771 pMod->xClose(pC);
175772 if( rc!=SQLITE_OK ) goto offsets_out;
175773 }
175774
175775 offsets_out:
175776 sqlite3_free(sCtx.aTerm);
175777 assert( rc!=SQLITE_DONE );
175778 sqlite3Fts3SegmentsClose(pTab);
175779 if( rc!=SQLITE_OK ){
175780 sqlite3_result_error_code(pCtx, rc);
175781 sqlite3_free(res.z);
175782 }else{
175783 sqlite3_result_text(pCtx, res.z, res.n-1, sqlite3_free);
175784 }
175785 return;
175786}
175787
175788/*
175789** Implementation of matchinfo() function.
175790*/
175791SQLITE_PRIVATE void sqlite3Fts3Matchinfo(
175792 sqlite3_context *pContext, /* Function call context */
175793 Fts3Cursor *pCsr, /* FTS3 table cursor */
175794 const char *zArg /* Second arg to matchinfo() function */
175795){
175796 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
175797 const char *zFormat;
175798
175799 if( zArg ){
175800 zFormat = zArg;
175801 }else{
175802 zFormat = FTS3_MATCHINFO_DEFAULT;
175803 }
175804
175805 if( !pCsr->pExpr ){
175806 sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC);
175807 return;
175808 }else{
175809 /* Retrieve matchinfo() data. */
175810 fts3GetMatchinfo(pContext, pCsr, zFormat);
175811 sqlite3Fts3SegmentsClose(pTab);
175812 }
175813}
175814
175815#endif
175816
175817/************** End of fts3_snippet.c ****************************************/
175818/************** Begin file fts3_unicode.c ************************************/
175819/*
175820** 2012 May 24
175821**
175822** The author disclaims copyright to this source code. In place of
175823** a legal notice, here is a blessing:
175824**
175825** May you do good and not evil.
175826** May you find forgiveness for yourself and forgive others.
175827** May you share freely, never taking more than you give.
175828**
175829******************************************************************************
175830**
175831** Implementation of the "unicode" full-text-search tokenizer.
175832*/
175833
175834#ifndef SQLITE_DISABLE_FTS3_UNICODE
175835
175836/* #include "fts3Int.h" */
175837#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
175838
175839/* #include <assert.h> */
175840/* #include <stdlib.h> */
175841/* #include <stdio.h> */
175842/* #include <string.h> */
175843
175844/* #include "fts3_tokenizer.h" */
175845
175846/*
175847** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
175848** from the sqlite3 source file utf.c. If this file is compiled as part
175849** of the amalgamation, they are not required.
175850*/
175851#ifndef SQLITE_AMALGAMATION
175852
175853static const unsigned char sqlite3Utf8Trans1[] = {
175854 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
175855 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
175856 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
175857 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
175858 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
175859 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
175860 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
175861 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
175862};
175863
175864#define READ_UTF8(zIn, zTerm, c) \
175865 c = *(zIn++); \
175866 if( c>=0xc0 ){ \
175867 c = sqlite3Utf8Trans1[c-0xc0]; \
175868 while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \
175869 c = (c<<6) + (0x3f & *(zIn++)); \
175870 } \
175871 if( c<0x80 \
175872 || (c&0xFFFFF800)==0xD800 \
175873 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
175874 }
175875
175876#define WRITE_UTF8(zOut, c) { \
175877 if( c<0x00080 ){ \
175878 *zOut++ = (u8)(c&0xFF); \
175879 } \
175880 else if( c<0x00800 ){ \
175881 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \
175882 *zOut++ = 0x80 + (u8)(c & 0x3F); \
175883 } \
175884 else if( c<0x10000 ){ \
175885 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \
175886 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
175887 *zOut++ = 0x80 + (u8)(c & 0x3F); \
175888 }else{ \
175889 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); \
175890 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); \
175891 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
175892 *zOut++ = 0x80 + (u8)(c & 0x3F); \
175893 } \
175894}
175895
175896#endif /* ifndef SQLITE_AMALGAMATION */
175897
175898typedef struct unicode_tokenizer unicode_tokenizer;
175899typedef struct unicode_cursor unicode_cursor;
175900
175901struct unicode_tokenizer {
175902 sqlite3_tokenizer base;
175903 int bRemoveDiacritic;
175904 int nException;
175905 int *aiException;
175906};
175907
175908struct unicode_cursor {
175909 sqlite3_tokenizer_cursor base;
175910 const unsigned char *aInput; /* Input text being tokenized */
175911 int nInput; /* Size of aInput[] in bytes */
175912 int iOff; /* Current offset within aInput[] */
175913 int iToken; /* Index of next token to be returned */
175914 char *zToken; /* storage for current token */
175915 int nAlloc; /* space allocated at zToken */
175916};
175917
175918
175919/*
175920** Destroy a tokenizer allocated by unicodeCreate().
175921*/
175922static int unicodeDestroy(sqlite3_tokenizer *pTokenizer){
175923 if( pTokenizer ){
175924 unicode_tokenizer *p = (unicode_tokenizer *)pTokenizer;
175925 sqlite3_free(p->aiException);
175926 sqlite3_free(p);
175927 }
175928 return SQLITE_OK;
175929}
175930
175931/*
175932** As part of a tokenchars= or separators= option, the CREATE VIRTUAL TABLE
175933** statement has specified that the tokenizer for this table shall consider
175934** all characters in string zIn/nIn to be separators (if bAlnum==0) or
175935** token characters (if bAlnum==1).
175936**
175937** For each codepoint in the zIn/nIn string, this function checks if the
175938** sqlite3FtsUnicodeIsalnum() function already returns the desired result.
175939** If so, no action is taken. Otherwise, the codepoint is added to the
175940** unicode_tokenizer.aiException[] array. For the purposes of tokenization,
175941** the return value of sqlite3FtsUnicodeIsalnum() is inverted for all
175942** codepoints in the aiException[] array.
175943**
175944** If a standalone diacritic mark (one that sqlite3FtsUnicodeIsdiacritic()
175945** identifies as a diacritic) occurs in the zIn/nIn string it is ignored.
175946** It is not possible to change the behavior of the tokenizer with respect
175947** to these codepoints.
175948*/
175949static int unicodeAddExceptions(
175950 unicode_tokenizer *p, /* Tokenizer to add exceptions to */
175951 int bAlnum, /* Replace Isalnum() return value with this */
175952 const char *zIn, /* Array of characters to make exceptions */
175953 int nIn /* Length of z in bytes */
175954){
175955 const unsigned char *z = (const unsigned char *)zIn;
175956 const unsigned char *zTerm = &z[nIn];
175957 unsigned int iCode;
175958 int nEntry = 0;
175959
175960 assert( bAlnum==0 || bAlnum==1 );
175961
175962 while( z<zTerm ){
175963 READ_UTF8(z, zTerm, iCode);
175964 assert( (sqlite3FtsUnicodeIsalnum((int)iCode) & 0xFFFFFFFE)==0 );
175965 if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum
175966 && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0
175967 ){
175968 nEntry++;
175969 }
175970 }
175971
175972 if( nEntry ){
175973 int *aNew; /* New aiException[] array */
175974 int nNew; /* Number of valid entries in array aNew[] */
175975
175976 aNew = sqlite3_realloc(p->aiException, (p->nException+nEntry)*sizeof(int));
175977 if( aNew==0 ) return SQLITE_NOMEM;
175978 nNew = p->nException;
175979
175980 z = (const unsigned char *)zIn;
175981 while( z<zTerm ){
175982 READ_UTF8(z, zTerm, iCode);
175983 if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum
175984 && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0
175985 ){
175986 int i, j;
175987 for(i=0; i<nNew && aNew[i]<(int)iCode; i++);
175988 for(j=nNew; j>i; j--) aNew[j] = aNew[j-1];
175989 aNew[i] = (int)iCode;
175990 nNew++;
175991 }
175992 }
175993 p->aiException = aNew;
175994 p->nException = nNew;
175995 }
175996
175997 return SQLITE_OK;
175998}
175999
176000/*
176001** Return true if the p->aiException[] array contains the value iCode.
176002*/
176003static int unicodeIsException(unicode_tokenizer *p, int iCode){
176004 if( p->nException>0 ){
176005 int *a = p->aiException;
176006 int iLo = 0;
176007 int iHi = p->nException-1;
176008
176009 while( iHi>=iLo ){
176010 int iTest = (iHi + iLo) / 2;
176011 if( iCode==a[iTest] ){
176012 return 1;
176013 }else if( iCode>a[iTest] ){
176014 iLo = iTest+1;
176015 }else{
176016 iHi = iTest-1;
176017 }
176018 }
176019 }
176020
176021 return 0;
176022}
176023
176024/*
176025** Return true if, for the purposes of tokenization, codepoint iCode is
176026** considered a token character (not a separator).
176027*/
176028static int unicodeIsAlnum(unicode_tokenizer *p, int iCode){
176029 assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
176030 return sqlite3FtsUnicodeIsalnum(iCode) ^ unicodeIsException(p, iCode);
176031}
176032
176033/*
176034** Create a new tokenizer instance.
176035*/
176036static int unicodeCreate(
176037 int nArg, /* Size of array argv[] */
176038 const char * const *azArg, /* Tokenizer creation arguments */
176039 sqlite3_tokenizer **pp /* OUT: New tokenizer handle */
176040){
176041 unicode_tokenizer *pNew; /* New tokenizer object */
176042 int i;
176043 int rc = SQLITE_OK;
176044
176045 pNew = (unicode_tokenizer *) sqlite3_malloc(sizeof(unicode_tokenizer));
176046 if( pNew==NULL ) return SQLITE_NOMEM;
176047 memset(pNew, 0, sizeof(unicode_tokenizer));
176048 pNew->bRemoveDiacritic = 1;
176049
176050 for(i=0; rc==SQLITE_OK && i<nArg; i++){
176051 const char *z = azArg[i];
176052 int n = (int)strlen(z);
176053
176054 if( n==19 && memcmp("remove_diacritics=1", z, 19)==0 ){
176055 pNew->bRemoveDiacritic = 1;
176056 }
176057 else if( n==19 && memcmp("remove_diacritics=0", z, 19)==0 ){
176058 pNew->bRemoveDiacritic = 0;
176059 }
176060 else if( n>=11 && memcmp("tokenchars=", z, 11)==0 ){
176061 rc = unicodeAddExceptions(pNew, 1, &z[11], n-11);
176062 }
176063 else if( n>=11 && memcmp("separators=", z, 11)==0 ){
176064 rc = unicodeAddExceptions(pNew, 0, &z[11], n-11);
176065 }
176066 else{
176067 /* Unrecognized argument */
176068 rc = SQLITE_ERROR;
176069 }
176070 }
176071
176072 if( rc!=SQLITE_OK ){
176073 unicodeDestroy((sqlite3_tokenizer *)pNew);
176074 pNew = 0;
176075 }
176076 *pp = (sqlite3_tokenizer *)pNew;
176077 return rc;
176078}
176079
176080/*
176081** Prepare to begin tokenizing a particular string. The input
176082** string to be tokenized is pInput[0..nBytes-1]. A cursor
176083** used to incrementally tokenize this string is returned in
176084** *ppCursor.
176085*/
176086static int unicodeOpen(
176087 sqlite3_tokenizer *p, /* The tokenizer */
176088 const char *aInput, /* Input string */
176089 int nInput, /* Size of string aInput in bytes */
176090 sqlite3_tokenizer_cursor **pp /* OUT: New cursor object */
176091){
176092 unicode_cursor *pCsr;
176093
176094 pCsr = (unicode_cursor *)sqlite3_malloc(sizeof(unicode_cursor));
176095 if( pCsr==0 ){
176096 return SQLITE_NOMEM;
176097 }
176098 memset(pCsr, 0, sizeof(unicode_cursor));
176099
176100 pCsr->aInput = (const unsigned char *)aInput;
176101 if( aInput==0 ){
176102 pCsr->nInput = 0;
176103 }else if( nInput<0 ){
176104 pCsr->nInput = (int)strlen(aInput);
176105 }else{
176106 pCsr->nInput = nInput;
176107 }
176108
176109 *pp = &pCsr->base;
176110 UNUSED_PARAMETER(p);
176111 return SQLITE_OK;
176112}
176113
176114/*
176115** Close a tokenization cursor previously opened by a call to
176116** simpleOpen() above.
176117*/
176118static int unicodeClose(sqlite3_tokenizer_cursor *pCursor){
176119 unicode_cursor *pCsr = (unicode_cursor *) pCursor;
176120 sqlite3_free(pCsr->zToken);
176121 sqlite3_free(pCsr);
176122 return SQLITE_OK;
176123}
176124
176125/*
176126** Extract the next token from a tokenization cursor. The cursor must
176127** have been opened by a prior call to simpleOpen().
176128*/
176129static int unicodeNext(
176130 sqlite3_tokenizer_cursor *pC, /* Cursor returned by simpleOpen */
176131 const char **paToken, /* OUT: Token text */
176132 int *pnToken, /* OUT: Number of bytes at *paToken */
176133 int *piStart, /* OUT: Starting offset of token */
176134 int *piEnd, /* OUT: Ending offset of token */
176135 int *piPos /* OUT: Position integer of token */
176136){
176137 unicode_cursor *pCsr = (unicode_cursor *)pC;
176138 unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
176139 unsigned int iCode = 0;
176140 char *zOut;
176141 const unsigned char *z = &pCsr->aInput[pCsr->iOff];
176142 const unsigned char *zStart = z;
176143 const unsigned char *zEnd;
176144 const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
176145
176146 /* Scan past any delimiter characters before the start of the next token.
176147 ** Return SQLITE_DONE early if this takes us all the way to the end of
176148 ** the input. */
176149 while( z<zTerm ){
176150 READ_UTF8(z, zTerm, iCode);
176151 if( unicodeIsAlnum(p, (int)iCode) ) break;
176152 zStart = z;
176153 }
176154 if( zStart>=zTerm ) return SQLITE_DONE;
176155
176156 zOut = pCsr->zToken;
176157 do {
176158 int iOut;
176159
176160 /* Grow the output buffer if required. */
176161 if( (zOut-pCsr->zToken)>=(pCsr->nAlloc-4) ){
176162 char *zNew = sqlite3_realloc(pCsr->zToken, pCsr->nAlloc+64);
176163 if( !zNew ) return SQLITE_NOMEM;
176164 zOut = &zNew[zOut - pCsr->zToken];
176165 pCsr->zToken = zNew;
176166 pCsr->nAlloc += 64;
176167 }
176168
176169 /* Write the folded case of the last character read to the output */
176170 zEnd = z;
176171 iOut = sqlite3FtsUnicodeFold((int)iCode, p->bRemoveDiacritic);
176172 if( iOut ){
176173 WRITE_UTF8(zOut, iOut);
176174 }
176175
176176 /* If the cursor is not at EOF, read the next character */
176177 if( z>=zTerm ) break;
176178 READ_UTF8(z, zTerm, iCode);
176179 }while( unicodeIsAlnum(p, (int)iCode)
176180 || sqlite3FtsUnicodeIsdiacritic((int)iCode)
176181 );
176182
176183 /* Set the output variables and return. */
176184 pCsr->iOff = (int)(z - pCsr->aInput);
176185 *paToken = pCsr->zToken;
176186 *pnToken = (int)(zOut - pCsr->zToken);
176187 *piStart = (int)(zStart - pCsr->aInput);
176188 *piEnd = (int)(zEnd - pCsr->aInput);
176189 *piPos = pCsr->iToken++;
176190 return SQLITE_OK;
176191}
176192
176193/*
176194** Set *ppModule to a pointer to the sqlite3_tokenizer_module
176195** structure for the unicode tokenizer.
176196*/
176197SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const **ppModule){
176198 static const sqlite3_tokenizer_module module = {
176199 0,
176200 unicodeCreate,
176201 unicodeDestroy,
176202 unicodeOpen,
176203 unicodeClose,
176204 unicodeNext,
176205 0,
176206 };
176207 *ppModule = &module;
176208}
176209
176210#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
176211#endif /* ifndef SQLITE_DISABLE_FTS3_UNICODE */
176212
176213/************** End of fts3_unicode.c ****************************************/
176214/************** Begin file fts3_unicode2.c ***********************************/
176215/*
176216** 2012 May 25
176217**
176218** The author disclaims copyright to this source code. In place of
176219** a legal notice, here is a blessing:
176220**
176221** May you do good and not evil.
176222** May you find forgiveness for yourself and forgive others.
176223** May you share freely, never taking more than you give.
176224**
176225******************************************************************************
176226*/
176227
176228/*
176229** DO NOT EDIT THIS MACHINE GENERATED FILE.
176230*/
176231
176232#ifndef SQLITE_DISABLE_FTS3_UNICODE
176233#if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4)
176234
176235/* #include <assert.h> */
176236
176237/*
176238** Return true if the argument corresponds to a unicode codepoint
176239** classified as either a letter or a number. Otherwise false.
176240**
176241** The results are undefined if the value passed to this function
176242** is less than zero.
176243*/
176244SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int c){
176245 /* Each unsigned integer in the following array corresponds to a contiguous
176246 ** range of unicode codepoints that are not either letters or numbers (i.e.
176247 ** codepoints for which this function should return 0).
176248 **
176249 ** The most significant 22 bits in each 32-bit value contain the first
176250 ** codepoint in the range. The least significant 10 bits are used to store
176251 ** the size of the range (always at least 1). In other words, the value
176252 ** ((C<<22) + N) represents a range of N codepoints starting with codepoint
176253 ** C. It is not possible to represent a range larger than 1023 codepoints
176254 ** using this format.
176255 */
176256 static const unsigned int aEntry[] = {
176257 0x00000030, 0x0000E807, 0x00016C06, 0x0001EC2F, 0x0002AC07,
176258 0x0002D001, 0x0002D803, 0x0002EC01, 0x0002FC01, 0x00035C01,
176259 0x0003DC01, 0x000B0804, 0x000B480E, 0x000B9407, 0x000BB401,
176260 0x000BBC81, 0x000DD401, 0x000DF801, 0x000E1002, 0x000E1C01,
176261 0x000FD801, 0x00120808, 0x00156806, 0x00162402, 0x00163C01,
176262 0x00164437, 0x0017CC02, 0x00180005, 0x00181816, 0x00187802,
176263 0x00192C15, 0x0019A804, 0x0019C001, 0x001B5001, 0x001B580F,
176264 0x001B9C07, 0x001BF402, 0x001C000E, 0x001C3C01, 0x001C4401,
176265 0x001CC01B, 0x001E980B, 0x001FAC09, 0x001FD804, 0x00205804,
176266 0x00206C09, 0x00209403, 0x0020A405, 0x0020C00F, 0x00216403,
176267 0x00217801, 0x0023901B, 0x00240004, 0x0024E803, 0x0024F812,
176268 0x00254407, 0x00258804, 0x0025C001, 0x00260403, 0x0026F001,
176269 0x0026F807, 0x00271C02, 0x00272C03, 0x00275C01, 0x00278802,
176270 0x0027C802, 0x0027E802, 0x00280403, 0x0028F001, 0x0028F805,
176271 0x00291C02, 0x00292C03, 0x00294401, 0x0029C002, 0x0029D401,
176272 0x002A0403, 0x002AF001, 0x002AF808, 0x002B1C03, 0x002B2C03,
176273 0x002B8802, 0x002BC002, 0x002C0403, 0x002CF001, 0x002CF807,
176274 0x002D1C02, 0x002D2C03, 0x002D5802, 0x002D8802, 0x002DC001,
176275 0x002E0801, 0x002EF805, 0x002F1803, 0x002F2804, 0x002F5C01,
176276 0x002FCC08, 0x00300403, 0x0030F807, 0x00311803, 0x00312804,
176277 0x00315402, 0x00318802, 0x0031FC01, 0x00320802, 0x0032F001,
176278 0x0032F807, 0x00331803, 0x00332804, 0x00335402, 0x00338802,
176279 0x00340802, 0x0034F807, 0x00351803, 0x00352804, 0x00355C01,
176280 0x00358802, 0x0035E401, 0x00360802, 0x00372801, 0x00373C06,
176281 0x00375801, 0x00376008, 0x0037C803, 0x0038C401, 0x0038D007,
176282 0x0038FC01, 0x00391C09, 0x00396802, 0x003AC401, 0x003AD006,
176283 0x003AEC02, 0x003B2006, 0x003C041F, 0x003CD00C, 0x003DC417,
176284 0x003E340B, 0x003E6424, 0x003EF80F, 0x003F380D, 0x0040AC14,
176285 0x00412806, 0x00415804, 0x00417803, 0x00418803, 0x00419C07,
176286 0x0041C404, 0x0042080C, 0x00423C01, 0x00426806, 0x0043EC01,
176287 0x004D740C, 0x004E400A, 0x00500001, 0x0059B402, 0x005A0001,
176288 0x005A6C02, 0x005BAC03, 0x005C4803, 0x005CC805, 0x005D4802,
176289 0x005DC802, 0x005ED023, 0x005F6004, 0x005F7401, 0x0060000F,
176290 0x0062A401, 0x0064800C, 0x0064C00C, 0x00650001, 0x00651002,
176291 0x0066C011, 0x00672002, 0x00677822, 0x00685C05, 0x00687802,
176292 0x0069540A, 0x0069801D, 0x0069FC01, 0x006A8007, 0x006AA006,
176293 0x006C0005, 0x006CD011, 0x006D6823, 0x006E0003, 0x006E840D,
176294 0x006F980E, 0x006FF004, 0x00709014, 0x0070EC05, 0x0071F802,
176295 0x00730008, 0x00734019, 0x0073B401, 0x0073C803, 0x00770027,
176296 0x0077F004, 0x007EF401, 0x007EFC03, 0x007F3403, 0x007F7403,
176297 0x007FB403, 0x007FF402, 0x00800065, 0x0081A806, 0x0081E805,
176298 0x00822805, 0x0082801A, 0x00834021, 0x00840002, 0x00840C04,
176299 0x00842002, 0x00845001, 0x00845803, 0x00847806, 0x00849401,
176300 0x00849C01, 0x0084A401, 0x0084B801, 0x0084E802, 0x00850005,
176301 0x00852804, 0x00853C01, 0x00864264, 0x00900027, 0x0091000B,
176302 0x0092704E, 0x00940200, 0x009C0475, 0x009E53B9, 0x00AD400A,
176303 0x00B39406, 0x00B3BC03, 0x00B3E404, 0x00B3F802, 0x00B5C001,
176304 0x00B5FC01, 0x00B7804F, 0x00B8C00C, 0x00BA001A, 0x00BA6C59,
176305 0x00BC00D6, 0x00BFC00C, 0x00C00005, 0x00C02019, 0x00C0A807,
176306 0x00C0D802, 0x00C0F403, 0x00C26404, 0x00C28001, 0x00C3EC01,
176307 0x00C64002, 0x00C6580A, 0x00C70024, 0x00C8001F, 0x00C8A81E,
176308 0x00C94001, 0x00C98020, 0x00CA2827, 0x00CB003F, 0x00CC0100,
176309 0x01370040, 0x02924037, 0x0293F802, 0x02983403, 0x0299BC10,
176310 0x029A7C01, 0x029BC008, 0x029C0017, 0x029C8002, 0x029E2402,
176311 0x02A00801, 0x02A01801, 0x02A02C01, 0x02A08C09, 0x02A0D804,
176312 0x02A1D004, 0x02A20002, 0x02A2D011, 0x02A33802, 0x02A38012,
176313 0x02A3E003, 0x02A4980A, 0x02A51C0D, 0x02A57C01, 0x02A60004,
176314 0x02A6CC1B, 0x02A77802, 0x02A8A40E, 0x02A90C01, 0x02A93002,
176315 0x02A97004, 0x02A9DC03, 0x02A9EC01, 0x02AAC001, 0x02AAC803,
176316 0x02AADC02, 0x02AAF802, 0x02AB0401, 0x02AB7802, 0x02ABAC07,
176317 0x02ABD402, 0x02AF8C0B, 0x03600001, 0x036DFC02, 0x036FFC02,
176318 0x037FFC01, 0x03EC7801, 0x03ECA401, 0x03EEC810, 0x03F4F802,
176319 0x03F7F002, 0x03F8001A, 0x03F88007, 0x03F8C023, 0x03F95013,
176320 0x03F9A004, 0x03FBFC01, 0x03FC040F, 0x03FC6807, 0x03FCEC06,
176321 0x03FD6C0B, 0x03FF8007, 0x03FFA007, 0x03FFE405, 0x04040003,
176322 0x0404DC09, 0x0405E411, 0x0406400C, 0x0407402E, 0x040E7C01,
176323 0x040F4001, 0x04215C01, 0x04247C01, 0x0424FC01, 0x04280403,
176324 0x04281402, 0x04283004, 0x0428E003, 0x0428FC01, 0x04294009,
176325 0x0429FC01, 0x042CE407, 0x04400003, 0x0440E016, 0x04420003,
176326 0x0442C012, 0x04440003, 0x04449C0E, 0x04450004, 0x04460003,
176327 0x0446CC0E, 0x04471404, 0x045AAC0D, 0x0491C004, 0x05BD442E,
176328 0x05BE3C04, 0x074000F6, 0x07440027, 0x0744A4B5, 0x07480046,
176329 0x074C0057, 0x075B0401, 0x075B6C01, 0x075BEC01, 0x075C5401,
176330 0x075CD401, 0x075D3C01, 0x075DBC01, 0x075E2401, 0x075EA401,
176331 0x075F0C01, 0x07BBC002, 0x07C0002C, 0x07C0C064, 0x07C2800F,
176332 0x07C2C40E, 0x07C3040F, 0x07C3440F, 0x07C4401F, 0x07C4C03C,
176333 0x07C5C02B, 0x07C7981D, 0x07C8402B, 0x07C90009, 0x07C94002,
176334 0x07CC0021, 0x07CCC006, 0x07CCDC46, 0x07CE0014, 0x07CE8025,
176335 0x07CF1805, 0x07CF8011, 0x07D0003F, 0x07D10001, 0x07D108B6,
176336 0x07D3E404, 0x07D4003E, 0x07D50004, 0x07D54018, 0x07D7EC46,
176337 0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401, 0x38008060,
176338 0x380400F0,
176339 };
176340 static const unsigned int aAscii[4] = {
176341 0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001,
176342 };
176343
176344 if( (unsigned int)c<128 ){
176345 return ( (aAscii[c >> 5] & ((unsigned int)1 << (c & 0x001F)))==0 );
176346 }else if( (unsigned int)c<(1<<22) ){
176347 unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
176348 int iRes = 0;
176349 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
176350 int iLo = 0;
176351 while( iHi>=iLo ){
176352 int iTest = (iHi + iLo) / 2;
176353 if( key >= aEntry[iTest] ){
176354 iRes = iTest;
176355 iLo = iTest+1;
176356 }else{
176357 iHi = iTest-1;
176358 }
176359 }
176360 assert( aEntry[0]<key );
176361 assert( key>=aEntry[iRes] );
176362 return (((unsigned int)c) >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
176363 }
176364 return 1;
176365}
176366
176367
176368/*
176369** If the argument is a codepoint corresponding to a lowercase letter
176370** in the ASCII range with a diacritic added, return the codepoint
176371** of the ASCII letter only. For example, if passed 235 - "LATIN
176372** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
176373** E"). The resuls of passing a codepoint that corresponds to an
176374** uppercase letter are undefined.
176375*/
176376static int remove_diacritic(int c){
176377 unsigned short aDia[] = {
176378 0, 1797, 1848, 1859, 1891, 1928, 1940, 1995,
176379 2024, 2040, 2060, 2110, 2168, 2206, 2264, 2286,
176380 2344, 2383, 2472, 2488, 2516, 2596, 2668, 2732,
176381 2782, 2842, 2894, 2954, 2984, 3000, 3028, 3336,
176382 3456, 3696, 3712, 3728, 3744, 3896, 3912, 3928,
176383 3968, 4008, 4040, 4106, 4138, 4170, 4202, 4234,
176384 4266, 4296, 4312, 4344, 4408, 4424, 4472, 4504,
176385 6148, 6198, 6264, 6280, 6360, 6429, 6505, 6529,
176386 61448, 61468, 61534, 61592, 61642, 61688, 61704, 61726,
176387 61784, 61800, 61836, 61880, 61914, 61948, 61998, 62122,
176388 62154, 62200, 62218, 62302, 62364, 62442, 62478, 62536,
176389 62554, 62584, 62604, 62640, 62648, 62656, 62664, 62730,
176390 62924, 63050, 63082, 63274, 63390,
176391 };
176392 char aChar[] = {
176393 '\0', 'a', 'c', 'e', 'i', 'n', 'o', 'u', 'y', 'y', 'a', 'c',
176394 'd', 'e', 'e', 'g', 'h', 'i', 'j', 'k', 'l', 'n', 'o', 'r',
176395 's', 't', 'u', 'u', 'w', 'y', 'z', 'o', 'u', 'a', 'i', 'o',
176396 'u', 'g', 'k', 'o', 'j', 'g', 'n', 'a', 'e', 'i', 'o', 'r',
176397 'u', 's', 't', 'h', 'a', 'e', 'o', 'y', '\0', '\0', '\0', '\0',
176398 '\0', '\0', '\0', '\0', 'a', 'b', 'd', 'd', 'e', 'f', 'g', 'h',
176399 'h', 'i', 'k', 'l', 'l', 'm', 'n', 'p', 'r', 'r', 's', 't',
176400 'u', 'v', 'w', 'w', 'x', 'y', 'z', 'h', 't', 'w', 'y', 'a',
176401 'e', 'i', 'o', 'u', 'y',
176402 };
176403
176404 unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
176405 int iRes = 0;
176406 int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
176407 int iLo = 0;
176408 while( iHi>=iLo ){
176409 int iTest = (iHi + iLo) / 2;
176410 if( key >= aDia[iTest] ){
176411 iRes = iTest;
176412 iLo = iTest+1;
176413 }else{
176414 iHi = iTest-1;
176415 }
176416 }
176417 assert( key>=aDia[iRes] );
176418 return ((c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : (int)aChar[iRes]);
176419}
176420
176421
176422/*
176423** Return true if the argument interpreted as a unicode codepoint
176424** is a diacritical modifier character.
176425*/
176426SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int c){
176427 unsigned int mask0 = 0x08029FDF;
176428 unsigned int mask1 = 0x000361F8;
176429 if( c<768 || c>817 ) return 0;
176430 return (c < 768+32) ?
176431 (mask0 & (1 << (c-768))) :
176432 (mask1 & (1 << (c-768-32)));
176433}
176434
176435
176436/*
176437** Interpret the argument as a unicode codepoint. If the codepoint
176438** is an upper case character that has a lower case equivalent,
176439** return the codepoint corresponding to the lower case version.
176440** Otherwise, return a copy of the argument.
176441**
176442** The results are undefined if the value passed to this function
176443** is less than zero.
176444*/
176445SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int c, int bRemoveDiacritic){
176446 /* Each entry in the following array defines a rule for folding a range
176447 ** of codepoints to lower case. The rule applies to a range of nRange
176448 ** codepoints starting at codepoint iCode.
176449 **
176450 ** If the least significant bit in flags is clear, then the rule applies
176451 ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
176452 ** need to be folded). Or, if it is set, then the rule only applies to
176453 ** every second codepoint in the range, starting with codepoint C.
176454 **
176455 ** The 7 most significant bits in flags are an index into the aiOff[]
176456 ** array. If a specific codepoint C does require folding, then its lower
176457 ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
176458 **
176459 ** The contents of this array are generated by parsing the CaseFolding.txt
176460 ** file distributed as part of the "Unicode Character Database". See
176461 ** http://www.unicode.org for details.
176462 */
176463 static const struct TableEntry {
176464 unsigned short iCode;
176465 unsigned char flags;
176466 unsigned char nRange;
176467 } aEntry[] = {
176468 {65, 14, 26}, {181, 64, 1}, {192, 14, 23},
176469 {216, 14, 7}, {256, 1, 48}, {306, 1, 6},
176470 {313, 1, 16}, {330, 1, 46}, {376, 116, 1},
176471 {377, 1, 6}, {383, 104, 1}, {385, 50, 1},
176472 {386, 1, 4}, {390, 44, 1}, {391, 0, 1},
176473 {393, 42, 2}, {395, 0, 1}, {398, 32, 1},
176474 {399, 38, 1}, {400, 40, 1}, {401, 0, 1},
176475 {403, 42, 1}, {404, 46, 1}, {406, 52, 1},
176476 {407, 48, 1}, {408, 0, 1}, {412, 52, 1},
176477 {413, 54, 1}, {415, 56, 1}, {416, 1, 6},
176478 {422, 60, 1}, {423, 0, 1}, {425, 60, 1},
176479 {428, 0, 1}, {430, 60, 1}, {431, 0, 1},
176480 {433, 58, 2}, {435, 1, 4}, {439, 62, 1},
176481 {440, 0, 1}, {444, 0, 1}, {452, 2, 1},
176482 {453, 0, 1}, {455, 2, 1}, {456, 0, 1},
176483 {458, 2, 1}, {459, 1, 18}, {478, 1, 18},
176484 {497, 2, 1}, {498, 1, 4}, {502, 122, 1},
176485 {503, 134, 1}, {504, 1, 40}, {544, 110, 1},
176486 {546, 1, 18}, {570, 70, 1}, {571, 0, 1},
176487 {573, 108, 1}, {574, 68, 1}, {577, 0, 1},
176488 {579, 106, 1}, {580, 28, 1}, {581, 30, 1},
176489 {582, 1, 10}, {837, 36, 1}, {880, 1, 4},
176490 {886, 0, 1}, {902, 18, 1}, {904, 16, 3},
176491 {908, 26, 1}, {910, 24, 2}, {913, 14, 17},
176492 {931, 14, 9}, {962, 0, 1}, {975, 4, 1},
176493 {976, 140, 1}, {977, 142, 1}, {981, 146, 1},
176494 {982, 144, 1}, {984, 1, 24}, {1008, 136, 1},
176495 {1009, 138, 1}, {1012, 130, 1}, {1013, 128, 1},
176496 {1015, 0, 1}, {1017, 152, 1}, {1018, 0, 1},
176497 {1021, 110, 3}, {1024, 34, 16}, {1040, 14, 32},
176498 {1120, 1, 34}, {1162, 1, 54}, {1216, 6, 1},
176499 {1217, 1, 14}, {1232, 1, 88}, {1329, 22, 38},
176500 {4256, 66, 38}, {4295, 66, 1}, {4301, 66, 1},
176501 {7680, 1, 150}, {7835, 132, 1}, {7838, 96, 1},
176502 {7840, 1, 96}, {7944, 150, 8}, {7960, 150, 6},
176503 {7976, 150, 8}, {7992, 150, 8}, {8008, 150, 6},
176504 {8025, 151, 8}, {8040, 150, 8}, {8072, 150, 8},
176505 {8088, 150, 8}, {8104, 150, 8}, {8120, 150, 2},
176506 {8122, 126, 2}, {8124, 148, 1}, {8126, 100, 1},
176507 {8136, 124, 4}, {8140, 148, 1}, {8152, 150, 2},
176508 {8154, 120, 2}, {8168, 150, 2}, {8170, 118, 2},
176509 {8172, 152, 1}, {8184, 112, 2}, {8186, 114, 2},
176510 {8188, 148, 1}, {8486, 98, 1}, {8490, 92, 1},
176511 {8491, 94, 1}, {8498, 12, 1}, {8544, 8, 16},
176512 {8579, 0, 1}, {9398, 10, 26}, {11264, 22, 47},
176513 {11360, 0, 1}, {11362, 88, 1}, {11363, 102, 1},
176514 {11364, 90, 1}, {11367, 1, 6}, {11373, 84, 1},
176515 {11374, 86, 1}, {11375, 80, 1}, {11376, 82, 1},
176516 {11378, 0, 1}, {11381, 0, 1}, {11390, 78, 2},
176517 {11392, 1, 100}, {11499, 1, 4}, {11506, 0, 1},
176518 {42560, 1, 46}, {42624, 1, 24}, {42786, 1, 14},
176519 {42802, 1, 62}, {42873, 1, 4}, {42877, 76, 1},
176520 {42878, 1, 10}, {42891, 0, 1}, {42893, 74, 1},
176521 {42896, 1, 4}, {42912, 1, 10}, {42922, 72, 1},
176522 {65313, 14, 26},
176523 };
176524 static const unsigned short aiOff[] = {
176525 1, 2, 8, 15, 16, 26, 28, 32,
176526 37, 38, 40, 48, 63, 64, 69, 71,
176527 79, 80, 116, 202, 203, 205, 206, 207,
176528 209, 210, 211, 213, 214, 217, 218, 219,
176529 775, 7264, 10792, 10795, 23228, 23256, 30204, 54721,
176530 54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274,
176531 57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406,
176532 65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462,
176533 65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511,
176534 65514, 65521, 65527, 65528, 65529,
176535 };
176536
176537 int ret = c;
176538
176539 assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 );
176540
176541 if( c<128 ){
176542 if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
176543 }else if( c<65536 ){
176544 const struct TableEntry *p;
176545 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
176546 int iLo = 0;
176547 int iRes = -1;
176548
176549 assert( c>aEntry[0].iCode );
176550 while( iHi>=iLo ){
176551 int iTest = (iHi + iLo) / 2;
176552 int cmp = (c - aEntry[iTest].iCode);
176553 if( cmp>=0 ){
176554 iRes = iTest;
176555 iLo = iTest+1;
176556 }else{
176557 iHi = iTest-1;
176558 }
176559 }
176560
176561 assert( iRes>=0 && c>=aEntry[iRes].iCode );
176562 p = &aEntry[iRes];
176563 if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
176564 ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
176565 assert( ret>0 );
176566 }
176567
176568 if( bRemoveDiacritic ) ret = remove_diacritic(ret);
176569 }
176570
176571 else if( c>=66560 && c<66600 ){
176572 ret = c + 40;
176573 }
176574
176575 return ret;
176576}
176577#endif /* defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) */
176578#endif /* !defined(SQLITE_DISABLE_FTS3_UNICODE) */
176579
176580/************** End of fts3_unicode2.c ***************************************/
176581/************** Begin file json1.c *******************************************/
176582/*
176583** 2015-08-12
176584**
176585** The author disclaims copyright to this source code. In place of
176586** a legal notice, here is a blessing:
176587**
176588** May you do good and not evil.
176589** May you find forgiveness for yourself and forgive others.
176590** May you share freely, never taking more than you give.
176591**
176592******************************************************************************
176593**
176594** This SQLite extension implements JSON functions. The interface is
176595** modeled after MySQL JSON functions:
176596**
176597** https://dev.mysql.com/doc/refman/5.7/en/json.html
176598**
176599** For the time being, all JSON is stored as pure text. (We might add
176600** a JSONB type in the future which stores a binary encoding of JSON in
176601** a BLOB, but there is no support for JSONB in the current implementation.
176602** This implementation parses JSON text at 250 MB/s, so it is hard to see
176603** how JSONB might improve on that.)
176604*/
176605#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1)
176606#if !defined(SQLITEINT_H)
176607/* #include "sqlite3ext.h" */
176608#endif
176609SQLITE_EXTENSION_INIT1
176610/* #include <assert.h> */
176611/* #include <string.h> */
176612/* #include <stdlib.h> */
176613/* #include <stdarg.h> */
176614
176615/* Mark a function parameter as unused, to suppress nuisance compiler
176616** warnings. */
176617#ifndef UNUSED_PARAM
176618# define UNUSED_PARAM(X) (void)(X)
176619#endif
176620
176621#ifndef LARGEST_INT64
176622# define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
176623# define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
176624#endif
176625
176626/*
176627** Versions of isspace(), isalnum() and isdigit() to which it is safe
176628** to pass signed char values.
176629*/
176630#ifdef sqlite3Isdigit
176631 /* Use the SQLite core versions if this routine is part of the
176632 ** SQLite amalgamation */
176633# define safe_isdigit(x) sqlite3Isdigit(x)
176634# define safe_isalnum(x) sqlite3Isalnum(x)
176635# define safe_isxdigit(x) sqlite3Isxdigit(x)
176636#else
176637 /* Use the standard library for separate compilation */
176638#include <ctype.h> /* amalgamator: keep */
176639# define safe_isdigit(x) isdigit((unsigned char)(x))
176640# define safe_isalnum(x) isalnum((unsigned char)(x))
176641# define safe_isxdigit(x) isxdigit((unsigned char)(x))
176642#endif
176643
176644/*
176645** Growing our own isspace() routine this way is twice as fast as
176646** the library isspace() function, resulting in a 7% overall performance
176647** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
176648*/
176649static const char jsonIsSpace[] = {
176650 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
176651 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
176652 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
176653 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
176654 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
176655 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
176656 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
176657 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
176658 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
176659 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
176660 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
176661 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
176662 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
176663 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
176664 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
176665 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
176666};
176667#define safe_isspace(x) (jsonIsSpace[(unsigned char)x])
176668
176669#ifndef SQLITE_AMALGAMATION
176670 /* Unsigned integer types. These are already defined in the sqliteInt.h,
176671 ** but the definitions need to be repeated for separate compilation. */
176672 typedef sqlite3_uint64 u64;
176673 typedef unsigned int u32;
176674 typedef unsigned short int u16;
176675 typedef unsigned char u8;
176676#endif
176677
176678/* Objects */
176679typedef struct JsonString JsonString;
176680typedef struct JsonNode JsonNode;
176681typedef struct JsonParse JsonParse;
176682
176683/* An instance of this object represents a JSON string
176684** under construction. Really, this is a generic string accumulator
176685** that can be and is used to create strings other than JSON.
176686*/
176687struct JsonString {
176688 sqlite3_context *pCtx; /* Function context - put error messages here */
176689 char *zBuf; /* Append JSON content here */
176690 u64 nAlloc; /* Bytes of storage available in zBuf[] */
176691 u64 nUsed; /* Bytes of zBuf[] currently used */
176692 u8 bStatic; /* True if zBuf is static space */
176693 u8 bErr; /* True if an error has been encountered */
176694 char zSpace[100]; /* Initial static space */
176695};
176696
176697/* JSON type values
176698*/
176699#define JSON_NULL 0
176700#define JSON_TRUE 1
176701#define JSON_FALSE 2
176702#define JSON_INT 3
176703#define JSON_REAL 4
176704#define JSON_STRING 5
176705#define JSON_ARRAY 6
176706#define JSON_OBJECT 7
176707
176708/* The "subtype" set for JSON values */
176709#define JSON_SUBTYPE 74 /* Ascii for "J" */
176710
176711/*
176712** Names of the various JSON types:
176713*/
176714static const char * const jsonType[] = {
176715 "null", "true", "false", "integer", "real", "text", "array", "object"
176716};
176717
176718/* Bit values for the JsonNode.jnFlag field
176719*/
176720#define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */
176721#define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */
176722#define JNODE_REMOVE 0x04 /* Do not output */
176723#define JNODE_REPLACE 0x08 /* Replace with JsonNode.u.iReplace */
176724#define JNODE_PATCH 0x10 /* Patch with JsonNode.u.pPatch */
176725#define JNODE_APPEND 0x20 /* More ARRAY/OBJECT entries at u.iAppend */
176726#define JNODE_LABEL 0x40 /* Is a label of an object */
176727
176728
176729/* A single node of parsed JSON
176730*/
176731struct JsonNode {
176732 u8 eType; /* One of the JSON_ type values */
176733 u8 jnFlags; /* JNODE flags */
176734 u32 n; /* Bytes of content, or number of sub-nodes */
176735 union {
176736 const char *zJContent; /* Content for INT, REAL, and STRING */
176737 u32 iAppend; /* More terms for ARRAY and OBJECT */
176738 u32 iKey; /* Key for ARRAY objects in json_tree() */
176739 u32 iReplace; /* Replacement content for JNODE_REPLACE */
176740 JsonNode *pPatch; /* Node chain of patch for JNODE_PATCH */
176741 } u;
176742};
176743
176744/* A completely parsed JSON string
176745*/
176746struct JsonParse {
176747 u32 nNode; /* Number of slots of aNode[] used */
176748 u32 nAlloc; /* Number of slots of aNode[] allocated */
176749 JsonNode *aNode; /* Array of nodes containing the parse */
176750 const char *zJson; /* Original JSON string */
176751 u32 *aUp; /* Index of parent of each node */
176752 u8 oom; /* Set to true if out of memory */
176753 u8 nErr; /* Number of errors seen */
176754 u16 iDepth; /* Nesting depth */
176755 int nJson; /* Length of the zJson string in bytes */
176756 u32 iHold; /* Replace cache line with the lowest iHold value */
176757};
176758
176759/*
176760** Maximum nesting depth of JSON for this implementation.
176761**
176762** This limit is needed to avoid a stack overflow in the recursive
176763** descent parser. A depth of 2000 is far deeper than any sane JSON
176764** should go.
176765*/
176766#define JSON_MAX_DEPTH 2000
176767
176768/**************************************************************************
176769** Utility routines for dealing with JsonString objects
176770**************************************************************************/
176771
176772/* Set the JsonString object to an empty string
176773*/
176774static void jsonZero(JsonString *p){
176775 p->zBuf = p->zSpace;
176776 p->nAlloc = sizeof(p->zSpace);
176777 p->nUsed = 0;
176778 p->bStatic = 1;
176779}
176780
176781/* Initialize the JsonString object
176782*/
176783static void jsonInit(JsonString *p, sqlite3_context *pCtx){
176784 p->pCtx = pCtx;
176785 p->bErr = 0;
176786 jsonZero(p);
176787}
176788
176789
176790/* Free all allocated memory and reset the JsonString object back to its
176791** initial state.
176792*/
176793static void jsonReset(JsonString *p){
176794 if( !p->bStatic ) sqlite3_free(p->zBuf);
176795 jsonZero(p);
176796}
176797
176798
176799/* Report an out-of-memory (OOM) condition
176800*/
176801static void jsonOom(JsonString *p){
176802 p->bErr = 1;
176803 sqlite3_result_error_nomem(p->pCtx);
176804 jsonReset(p);
176805}
176806
176807/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
176808** Return zero on success. Return non-zero on an OOM error
176809*/
176810static int jsonGrow(JsonString *p, u32 N){
176811 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
176812 char *zNew;
176813 if( p->bStatic ){
176814 if( p->bErr ) return 1;
176815 zNew = sqlite3_malloc64(nTotal);
176816 if( zNew==0 ){
176817 jsonOom(p);
176818 return SQLITE_NOMEM;
176819 }
176820 memcpy(zNew, p->zBuf, (size_t)p->nUsed);
176821 p->zBuf = zNew;
176822 p->bStatic = 0;
176823 }else{
176824 zNew = sqlite3_realloc64(p->zBuf, nTotal);
176825 if( zNew==0 ){
176826 jsonOom(p);
176827 return SQLITE_NOMEM;
176828 }
176829 p->zBuf = zNew;
176830 }
176831 p->nAlloc = nTotal;
176832 return SQLITE_OK;
176833}
176834
176835/* Append N bytes from zIn onto the end of the JsonString string.
176836*/
176837static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
176838 if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
176839 memcpy(p->zBuf+p->nUsed, zIn, N);
176840 p->nUsed += N;
176841}
176842
176843/* Append formatted text (not to exceed N bytes) to the JsonString.
176844*/
176845static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
176846 va_list ap;
176847 if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return;
176848 va_start(ap, zFormat);
176849 sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
176850 va_end(ap);
176851 p->nUsed += (int)strlen(p->zBuf+p->nUsed);
176852}
176853
176854/* Append a single character
176855*/
176856static void jsonAppendChar(JsonString *p, char c){
176857 if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
176858 p->zBuf[p->nUsed++] = c;
176859}
176860
176861/* Append a comma separator to the output buffer, if the previous
176862** character is not '[' or '{'.
176863*/
176864static void jsonAppendSeparator(JsonString *p){
176865 char c;
176866 if( p->nUsed==0 ) return;
176867 c = p->zBuf[p->nUsed-1];
176868 if( c!='[' && c!='{' ) jsonAppendChar(p, ',');
176869}
176870
176871/* Append the N-byte string in zIn to the end of the JsonString string
176872** under construction. Enclose the string in "..." and escape
176873** any double-quotes or backslash characters contained within the
176874** string.
176875*/
176876static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
176877 u32 i;
176878 if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return;
176879 p->zBuf[p->nUsed++] = '"';
176880 for(i=0; i<N; i++){
176881 unsigned char c = ((unsigned const char*)zIn)[i];
176882 if( c=='"' || c=='\\' ){
176883 json_simple_escape:
176884 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
176885 p->zBuf[p->nUsed++] = '\\';
176886 }else if( c<=0x1f ){
176887 static const char aSpecial[] = {
176888 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
176889 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
176890 };
176891 assert( sizeof(aSpecial)==32 );
176892 assert( aSpecial['\b']=='b' );
176893 assert( aSpecial['\f']=='f' );
176894 assert( aSpecial['\n']=='n' );
176895 assert( aSpecial['\r']=='r' );
176896 assert( aSpecial['\t']=='t' );
176897 if( aSpecial[c] ){
176898 c = aSpecial[c];
176899 goto json_simple_escape;
176900 }
176901 if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return;
176902 p->zBuf[p->nUsed++] = '\\';
176903 p->zBuf[p->nUsed++] = 'u';
176904 p->zBuf[p->nUsed++] = '0';
176905 p->zBuf[p->nUsed++] = '0';
176906 p->zBuf[p->nUsed++] = '0' + (c>>4);
176907 c = "0123456789abcdef"[c&0xf];
176908 }
176909 p->zBuf[p->nUsed++] = c;
176910 }
176911 p->zBuf[p->nUsed++] = '"';
176912 assert( p->nUsed<p->nAlloc );
176913}
176914
176915/*
176916** Append a function parameter value to the JSON string under
176917** construction.
176918*/
176919static void jsonAppendValue(
176920 JsonString *p, /* Append to this JSON string */
176921 sqlite3_value *pValue /* Value to append */
176922){
176923 switch( sqlite3_value_type(pValue) ){
176924 case SQLITE_NULL: {
176925 jsonAppendRaw(p, "null", 4);
176926 break;
176927 }
176928 case SQLITE_INTEGER:
176929 case SQLITE_FLOAT: {
176930 const char *z = (const char*)sqlite3_value_text(pValue);
176931 u32 n = (u32)sqlite3_value_bytes(pValue);
176932 jsonAppendRaw(p, z, n);
176933 break;
176934 }
176935 case SQLITE_TEXT: {
176936 const char *z = (const char*)sqlite3_value_text(pValue);
176937 u32 n = (u32)sqlite3_value_bytes(pValue);
176938 if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){
176939 jsonAppendRaw(p, z, n);
176940 }else{
176941 jsonAppendString(p, z, n);
176942 }
176943 break;
176944 }
176945 default: {
176946 if( p->bErr==0 ){
176947 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
176948 p->bErr = 2;
176949 jsonReset(p);
176950 }
176951 break;
176952 }
176953 }
176954}
176955
176956
176957/* Make the JSON in p the result of the SQL function.
176958*/
176959static void jsonResult(JsonString *p){
176960 if( p->bErr==0 ){
176961 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
176962 p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
176963 SQLITE_UTF8);
176964 jsonZero(p);
176965 }
176966 assert( p->bStatic );
176967}
176968
176969/**************************************************************************
176970** Utility routines for dealing with JsonNode and JsonParse objects
176971**************************************************************************/
176972
176973/*
176974** Return the number of consecutive JsonNode slots need to represent
176975** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and
176976** OBJECT types, the number might be larger.
176977**
176978** Appended elements are not counted. The value returned is the number
176979** by which the JsonNode counter should increment in order to go to the
176980** next peer value.
176981*/
176982static u32 jsonNodeSize(JsonNode *pNode){
176983 return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
176984}
176985
176986/*
176987** Reclaim all memory allocated by a JsonParse object. But do not
176988** delete the JsonParse object itself.
176989*/
176990static void jsonParseReset(JsonParse *pParse){
176991 sqlite3_free(pParse->aNode);
176992 pParse->aNode = 0;
176993 pParse->nNode = 0;
176994 pParse->nAlloc = 0;
176995 sqlite3_free(pParse->aUp);
176996 pParse->aUp = 0;
176997}
176998
176999/*
177000** Free a JsonParse object that was obtained from sqlite3_malloc().
177001*/
177002static void jsonParseFree(JsonParse *pParse){
177003 jsonParseReset(pParse);
177004 sqlite3_free(pParse);
177005}
177006
177007/*
177008** Convert the JsonNode pNode into a pure JSON string and
177009** append to pOut. Subsubstructure is also included. Return
177010** the number of JsonNode objects that are encoded.
177011*/
177012static void jsonRenderNode(
177013 JsonNode *pNode, /* The node to render */
177014 JsonString *pOut, /* Write JSON here */
177015 sqlite3_value **aReplace /* Replacement values */
177016){
177017 if( pNode->jnFlags & (JNODE_REPLACE|JNODE_PATCH) ){
177018 if( pNode->jnFlags & JNODE_REPLACE ){
177019 jsonAppendValue(pOut, aReplace[pNode->u.iReplace]);
177020 return;
177021 }
177022 pNode = pNode->u.pPatch;
177023 }
177024 switch( pNode->eType ){
177025 default: {
177026 assert( pNode->eType==JSON_NULL );
177027 jsonAppendRaw(pOut, "null", 4);
177028 break;
177029 }
177030 case JSON_TRUE: {
177031 jsonAppendRaw(pOut, "true", 4);
177032 break;
177033 }
177034 case JSON_FALSE: {
177035 jsonAppendRaw(pOut, "false", 5);
177036 break;
177037 }
177038 case JSON_STRING: {
177039 if( pNode->jnFlags & JNODE_RAW ){
177040 jsonAppendString(pOut, pNode->u.zJContent, pNode->n);
177041 break;
177042 }
177043 /* Fall through into the next case */
177044 }
177045 case JSON_REAL:
177046 case JSON_INT: {
177047 jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
177048 break;
177049 }
177050 case JSON_ARRAY: {
177051 u32 j = 1;
177052 jsonAppendChar(pOut, '[');
177053 for(;;){
177054 while( j<=pNode->n ){
177055 if( (pNode[j].jnFlags & JNODE_REMOVE)==0 ){
177056 jsonAppendSeparator(pOut);
177057 jsonRenderNode(&pNode[j], pOut, aReplace);
177058 }
177059 j += jsonNodeSize(&pNode[j]);
177060 }
177061 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
177062 pNode = &pNode[pNode->u.iAppend];
177063 j = 1;
177064 }
177065 jsonAppendChar(pOut, ']');
177066 break;
177067 }
177068 case JSON_OBJECT: {
177069 u32 j = 1;
177070 jsonAppendChar(pOut, '{');
177071 for(;;){
177072 while( j<=pNode->n ){
177073 if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
177074 jsonAppendSeparator(pOut);
177075 jsonRenderNode(&pNode[j], pOut, aReplace);
177076 jsonAppendChar(pOut, ':');
177077 jsonRenderNode(&pNode[j+1], pOut, aReplace);
177078 }
177079 j += 1 + jsonNodeSize(&pNode[j+1]);
177080 }
177081 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
177082 pNode = &pNode[pNode->u.iAppend];
177083 j = 1;
177084 }
177085 jsonAppendChar(pOut, '}');
177086 break;
177087 }
177088 }
177089}
177090
177091/*
177092** Return a JsonNode and all its descendents as a JSON string.
177093*/
177094static void jsonReturnJson(
177095 JsonNode *pNode, /* Node to return */
177096 sqlite3_context *pCtx, /* Return value for this function */
177097 sqlite3_value **aReplace /* Array of replacement values */
177098){
177099 JsonString s;
177100 jsonInit(&s, pCtx);
177101 jsonRenderNode(pNode, &s, aReplace);
177102 jsonResult(&s);
177103 sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
177104}
177105
177106/*
177107** Make the JsonNode the return value of the function.
177108*/
177109static void jsonReturn(
177110 JsonNode *pNode, /* Node to return */
177111 sqlite3_context *pCtx, /* Return value for this function */
177112 sqlite3_value **aReplace /* Array of replacement values */
177113){
177114 switch( pNode->eType ){
177115 default: {
177116 assert( pNode->eType==JSON_NULL );
177117 sqlite3_result_null(pCtx);
177118 break;
177119 }
177120 case JSON_TRUE: {
177121 sqlite3_result_int(pCtx, 1);
177122 break;
177123 }
177124 case JSON_FALSE: {
177125 sqlite3_result_int(pCtx, 0);
177126 break;
177127 }
177128 case JSON_INT: {
177129 sqlite3_int64 i = 0;
177130 const char *z = pNode->u.zJContent;
177131 if( z[0]=='-' ){ z++; }
177132 while( z[0]>='0' && z[0]<='9' ){
177133 unsigned v = *(z++) - '0';
177134 if( i>=LARGEST_INT64/10 ){
177135 if( i>LARGEST_INT64/10 ) goto int_as_real;
177136 if( z[0]>='0' && z[0]<='9' ) goto int_as_real;
177137 if( v==9 ) goto int_as_real;
177138 if( v==8 ){
177139 if( pNode->u.zJContent[0]=='-' ){
177140 sqlite3_result_int64(pCtx, SMALLEST_INT64);
177141 goto int_done;
177142 }else{
177143 goto int_as_real;
177144 }
177145 }
177146 }
177147 i = i*10 + v;
177148 }
177149 if( pNode->u.zJContent[0]=='-' ){ i = -i; }
177150 sqlite3_result_int64(pCtx, i);
177151 int_done:
177152 break;
177153 int_as_real: /* fall through to real */;
177154 }
177155 case JSON_REAL: {
177156 double r;
177157#ifdef SQLITE_AMALGAMATION
177158 const char *z = pNode->u.zJContent;
177159 sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8);
177160#else
177161 r = strtod(pNode->u.zJContent, 0);
177162#endif
177163 sqlite3_result_double(pCtx, r);
177164 break;
177165 }
177166 case JSON_STRING: {
177167#if 0 /* Never happens because JNODE_RAW is only set by json_set(),
177168 ** json_insert() and json_replace() and those routines do not
177169 ** call jsonReturn() */
177170 if( pNode->jnFlags & JNODE_RAW ){
177171 sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
177172 SQLITE_TRANSIENT);
177173 }else
177174#endif
177175 assert( (pNode->jnFlags & JNODE_RAW)==0 );
177176 if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
177177 /* JSON formatted without any backslash-escapes */
177178 sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
177179 SQLITE_TRANSIENT);
177180 }else{
177181 /* Translate JSON formatted string into raw text */
177182 u32 i;
177183 u32 n = pNode->n;
177184 const char *z = pNode->u.zJContent;
177185 char *zOut;
177186 u32 j;
177187 zOut = sqlite3_malloc( n+1 );
177188 if( zOut==0 ){
177189 sqlite3_result_error_nomem(pCtx);
177190 break;
177191 }
177192 for(i=1, j=0; i<n-1; i++){
177193 char c = z[i];
177194 if( c!='\\' ){
177195 zOut[j++] = c;
177196 }else{
177197 c = z[++i];
177198 if( c=='u' ){
177199 u32 v = 0, k;
177200 for(k=0; k<4; i++, k++){
177201 assert( i<n-2 );
177202 c = z[i+1];
177203 assert( safe_isxdigit(c) );
177204 if( c<='9' ) v = v*16 + c - '0';
177205 else if( c<='F' ) v = v*16 + c - 'A' + 10;
177206 else v = v*16 + c - 'a' + 10;
177207 }
177208 if( v==0 ) break;
177209 if( v<=0x7f ){
177210 zOut[j++] = (char)v;
177211 }else if( v<=0x7ff ){
177212 zOut[j++] = (char)(0xc0 | (v>>6));
177213 zOut[j++] = 0x80 | (v&0x3f);
177214 }else{
177215 zOut[j++] = (char)(0xe0 | (v>>12));
177216 zOut[j++] = 0x80 | ((v>>6)&0x3f);
177217 zOut[j++] = 0x80 | (v&0x3f);
177218 }
177219 }else{
177220 if( c=='b' ){
177221 c = '\b';
177222 }else if( c=='f' ){
177223 c = '\f';
177224 }else if( c=='n' ){
177225 c = '\n';
177226 }else if( c=='r' ){
177227 c = '\r';
177228 }else if( c=='t' ){
177229 c = '\t';
177230 }
177231 zOut[j++] = c;
177232 }
177233 }
177234 }
177235 zOut[j] = 0;
177236 sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
177237 }
177238 break;
177239 }
177240 case JSON_ARRAY:
177241 case JSON_OBJECT: {
177242 jsonReturnJson(pNode, pCtx, aReplace);
177243 break;
177244 }
177245 }
177246}
177247
177248/* Forward reference */
177249static int jsonParseAddNode(JsonParse*,u32,u32,const char*);
177250
177251/*
177252** A macro to hint to the compiler that a function should not be
177253** inlined.
177254*/
177255#if defined(__GNUC__)
177256# define JSON_NOINLINE __attribute__((noinline))
177257#elif defined(_MSC_VER) && _MSC_VER>=1310
177258# define JSON_NOINLINE __declspec(noinline)
177259#else
177260# define JSON_NOINLINE
177261#endif
177262
177263
177264static JSON_NOINLINE int jsonParseAddNodeExpand(
177265 JsonParse *pParse, /* Append the node to this object */
177266 u32 eType, /* Node type */
177267 u32 n, /* Content size or sub-node count */
177268 const char *zContent /* Content */
177269){
177270 u32 nNew;
177271 JsonNode *pNew;
177272 assert( pParse->nNode>=pParse->nAlloc );
177273 if( pParse->oom ) return -1;
177274 nNew = pParse->nAlloc*2 + 10;
177275 pNew = sqlite3_realloc(pParse->aNode, sizeof(JsonNode)*nNew);
177276 if( pNew==0 ){
177277 pParse->oom = 1;
177278 return -1;
177279 }
177280 pParse->nAlloc = nNew;
177281 pParse->aNode = pNew;
177282 assert( pParse->nNode<pParse->nAlloc );
177283 return jsonParseAddNode(pParse, eType, n, zContent);
177284}
177285
177286/*
177287** Create a new JsonNode instance based on the arguments and append that
177288** instance to the JsonParse. Return the index in pParse->aNode[] of the
177289** new node, or -1 if a memory allocation fails.
177290*/
177291static int jsonParseAddNode(
177292 JsonParse *pParse, /* Append the node to this object */
177293 u32 eType, /* Node type */
177294 u32 n, /* Content size or sub-node count */
177295 const char *zContent /* Content */
177296){
177297 JsonNode *p;
177298 if( pParse->nNode>=pParse->nAlloc ){
177299 return jsonParseAddNodeExpand(pParse, eType, n, zContent);
177300 }
177301 p = &pParse->aNode[pParse->nNode];
177302 p->eType = (u8)eType;
177303 p->jnFlags = 0;
177304 p->n = n;
177305 p->u.zJContent = zContent;
177306 return pParse->nNode++;
177307}
177308
177309/*
177310** Return true if z[] begins with 4 (or more) hexadecimal digits
177311*/
177312static int jsonIs4Hex(const char *z){
177313 int i;
177314 for(i=0; i<4; i++) if( !safe_isxdigit(z[i]) ) return 0;
177315 return 1;
177316}
177317
177318/*
177319** Parse a single JSON value which begins at pParse->zJson[i]. Return the
177320** index of the first character past the end of the value parsed.
177321**
177322** Return negative for a syntax error. Special cases: return -2 if the
177323** first non-whitespace character is '}' and return -3 if the first
177324** non-whitespace character is ']'.
177325*/
177326static int jsonParseValue(JsonParse *pParse, u32 i){
177327 char c;
177328 u32 j;
177329 int iThis;
177330 int x;
177331 JsonNode *pNode;
177332 const char *z = pParse->zJson;
177333 while( safe_isspace(z[i]) ){ i++; }
177334 if( (c = z[i])=='{' ){
177335 /* Parse object */
177336 iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
177337 if( iThis<0 ) return -1;
177338 for(j=i+1;;j++){
177339 while( safe_isspace(z[j]) ){ j++; }
177340 if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
177341 x = jsonParseValue(pParse, j);
177342 if( x<0 ){
177343 pParse->iDepth--;
177344 if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1;
177345 return -1;
177346 }
177347 if( pParse->oom ) return -1;
177348 pNode = &pParse->aNode[pParse->nNode-1];
177349 if( pNode->eType!=JSON_STRING ) return -1;
177350 pNode->jnFlags |= JNODE_LABEL;
177351 j = x;
177352 while( safe_isspace(z[j]) ){ j++; }
177353 if( z[j]!=':' ) return -1;
177354 j++;
177355 x = jsonParseValue(pParse, j);
177356 pParse->iDepth--;
177357 if( x<0 ) return -1;
177358 j = x;
177359 while( safe_isspace(z[j]) ){ j++; }
177360 c = z[j];
177361 if( c==',' ) continue;
177362 if( c!='}' ) return -1;
177363 break;
177364 }
177365 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
177366 return j+1;
177367 }else if( c=='[' ){
177368 /* Parse array */
177369 iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
177370 if( iThis<0 ) return -1;
177371 for(j=i+1;;j++){
177372 while( safe_isspace(z[j]) ){ j++; }
177373 if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
177374 x = jsonParseValue(pParse, j);
177375 pParse->iDepth--;
177376 if( x<0 ){
177377 if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1;
177378 return -1;
177379 }
177380 j = x;
177381 while( safe_isspace(z[j]) ){ j++; }
177382 c = z[j];
177383 if( c==',' ) continue;
177384 if( c!=']' ) return -1;
177385 break;
177386 }
177387 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
177388 return j+1;
177389 }else if( c=='"' ){
177390 /* Parse string */
177391 u8 jnFlags = 0;
177392 j = i+1;
177393 for(;;){
177394 c = z[j];
177395 if( (c & ~0x1f)==0 ){
177396 /* Control characters are not allowed in strings */
177397 return -1;
177398 }
177399 if( c=='\\' ){
177400 c = z[++j];
177401 if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f'
177402 || c=='n' || c=='r' || c=='t'
177403 || (c=='u' && jsonIs4Hex(z+j+1)) ){
177404 jnFlags = JNODE_ESCAPE;
177405 }else{
177406 return -1;
177407 }
177408 }else if( c=='"' ){
177409 break;
177410 }
177411 j++;
177412 }
177413 jsonParseAddNode(pParse, JSON_STRING, j+1-i, &z[i]);
177414 if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
177415 return j+1;
177416 }else if( c=='n'
177417 && strncmp(z+i,"null",4)==0
177418 && !safe_isalnum(z[i+4]) ){
177419 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
177420 return i+4;
177421 }else if( c=='t'
177422 && strncmp(z+i,"true",4)==0
177423 && !safe_isalnum(z[i+4]) ){
177424 jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
177425 return i+4;
177426 }else if( c=='f'
177427 && strncmp(z+i,"false",5)==0
177428 && !safe_isalnum(z[i+5]) ){
177429 jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
177430 return i+5;
177431 }else if( c=='-' || (c>='0' && c<='9') ){
177432 /* Parse number */
177433 u8 seenDP = 0;
177434 u8 seenE = 0;
177435 assert( '-' < '0' );
177436 if( c<='0' ){
177437 j = c=='-' ? i+1 : i;
177438 if( z[j]=='0' && z[j+1]>='0' && z[j+1]<='9' ) return -1;
177439 }
177440 j = i+1;
177441 for(;; j++){
177442 c = z[j];
177443 if( c>='0' && c<='9' ) continue;
177444 if( c=='.' ){
177445 if( z[j-1]=='-' ) return -1;
177446 if( seenDP ) return -1;
177447 seenDP = 1;
177448 continue;
177449 }
177450 if( c=='e' || c=='E' ){
177451 if( z[j-1]<'0' ) return -1;
177452 if( seenE ) return -1;
177453 seenDP = seenE = 1;
177454 c = z[j+1];
177455 if( c=='+' || c=='-' ){
177456 j++;
177457 c = z[j+1];
177458 }
177459 if( c<'0' || c>'9' ) return -1;
177460 continue;
177461 }
177462 break;
177463 }
177464 if( z[j-1]<'0' ) return -1;
177465 jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT,
177466 j - i, &z[i]);
177467 return j;
177468 }else if( c=='}' ){
177469 return -2; /* End of {...} */
177470 }else if( c==']' ){
177471 return -3; /* End of [...] */
177472 }else if( c==0 ){
177473 return 0; /* End of file */
177474 }else{
177475 return -1; /* Syntax error */
177476 }
177477}
177478
177479/*
177480** Parse a complete JSON string. Return 0 on success or non-zero if there
177481** are any errors. If an error occurs, free all memory associated with
177482** pParse.
177483**
177484** pParse is uninitialized when this routine is called.
177485*/
177486static int jsonParse(
177487 JsonParse *pParse, /* Initialize and fill this JsonParse object */
177488 sqlite3_context *pCtx, /* Report errors here */
177489 const char *zJson /* Input JSON text to be parsed */
177490){
177491 int i;
177492 memset(pParse, 0, sizeof(*pParse));
177493 if( zJson==0 ) return 1;
177494 pParse->zJson = zJson;
177495 i = jsonParseValue(pParse, 0);
177496 if( pParse->oom ) i = -1;
177497 if( i>0 ){
177498 assert( pParse->iDepth==0 );
177499 while( safe_isspace(zJson[i]) ) i++;
177500 if( zJson[i] ) i = -1;
177501 }
177502 if( i<=0 ){
177503 if( pCtx!=0 ){
177504 if( pParse->oom ){
177505 sqlite3_result_error_nomem(pCtx);
177506 }else{
177507 sqlite3_result_error(pCtx, "malformed JSON", -1);
177508 }
177509 }
177510 jsonParseReset(pParse);
177511 return 1;
177512 }
177513 return 0;
177514}
177515
177516/* Mark node i of pParse as being a child of iParent. Call recursively
177517** to fill in all the descendants of node i.
177518*/
177519static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){
177520 JsonNode *pNode = &pParse->aNode[i];
177521 u32 j;
177522 pParse->aUp[i] = iParent;
177523 switch( pNode->eType ){
177524 case JSON_ARRAY: {
177525 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){
177526 jsonParseFillInParentage(pParse, i+j, i);
177527 }
177528 break;
177529 }
177530 case JSON_OBJECT: {
177531 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){
177532 pParse->aUp[i+j] = i;
177533 jsonParseFillInParentage(pParse, i+j+1, i);
177534 }
177535 break;
177536 }
177537 default: {
177538 break;
177539 }
177540 }
177541}
177542
177543/*
177544** Compute the parentage of all nodes in a completed parse.
177545*/
177546static int jsonParseFindParents(JsonParse *pParse){
177547 u32 *aUp;
177548 assert( pParse->aUp==0 );
177549 aUp = pParse->aUp = sqlite3_malloc( sizeof(u32)*pParse->nNode );
177550 if( aUp==0 ){
177551 pParse->oom = 1;
177552 return SQLITE_NOMEM;
177553 }
177554 jsonParseFillInParentage(pParse, 0, 0);
177555 return SQLITE_OK;
177556}
177557
177558/*
177559** Magic number used for the JSON parse cache in sqlite3_get_auxdata()
177560*/
177561#define JSON_CACHE_ID (-429938) /* First cache entry */
177562#define JSON_CACHE_SZ 4 /* Max number of cache entries */
177563
177564/*
177565** Obtain a complete parse of the JSON found in the first argument
177566** of the argv array. Use the sqlite3_get_auxdata() cache for this
177567** parse if it is available. If the cache is not available or if it
177568** is no longer valid, parse the JSON again and return the new parse,
177569** and also register the new parse so that it will be available for
177570** future sqlite3_get_auxdata() calls.
177571*/
177572static JsonParse *jsonParseCached(
177573 sqlite3_context *pCtx,
177574 sqlite3_value **argv,
177575 sqlite3_context *pErrCtx
177576){
177577 const char *zJson = (const char*)sqlite3_value_text(argv[0]);
177578 int nJson = sqlite3_value_bytes(argv[0]);
177579 JsonParse *p;
177580 JsonParse *pMatch = 0;
177581 int iKey;
177582 int iMinKey = 0;
177583 u32 iMinHold = 0xffffffff;
177584 u32 iMaxHold = 0;
177585 if( zJson==0 ) return 0;
177586 for(iKey=0; iKey<JSON_CACHE_SZ; iKey++){
177587 p = (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iKey);
177588 if( p==0 ){
177589 iMinKey = iKey;
177590 break;
177591 }
177592 if( pMatch==0
177593 && p->nJson==nJson
177594 && memcmp(p->zJson,zJson,nJson)==0
177595 ){
177596 p->nErr = 0;
177597 pMatch = p;
177598 }else if( p->iHold<iMinHold ){
177599 iMinHold = p->iHold;
177600 iMinKey = iKey;
177601 }
177602 if( p->iHold>iMaxHold ){
177603 iMaxHold = p->iHold;
177604 }
177605 }
177606 if( pMatch ){
177607 pMatch->nErr = 0;
177608 pMatch->iHold = iMaxHold+1;
177609 return pMatch;
177610 }
177611 p = sqlite3_malloc( sizeof(*p) + nJson + 1 );
177612 if( p==0 ){
177613 sqlite3_result_error_nomem(pCtx);
177614 return 0;
177615 }
177616 memset(p, 0, sizeof(*p));
177617 p->zJson = (char*)&p[1];
177618 memcpy((char*)p->zJson, zJson, nJson+1);
177619 if( jsonParse(p, pErrCtx, p->zJson) ){
177620 sqlite3_free(p);
177621 return 0;
177622 }
177623 p->nJson = nJson;
177624 p->iHold = iMaxHold+1;
177625 sqlite3_set_auxdata(pCtx, JSON_CACHE_ID+iMinKey, p,
177626 (void(*)(void*))jsonParseFree);
177627 return (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iMinKey);
177628}
177629
177630/*
177631** Compare the OBJECT label at pNode against zKey,nKey. Return true on
177632** a match.
177633*/
177634static int jsonLabelCompare(JsonNode *pNode, const char *zKey, u32 nKey){
177635 if( pNode->jnFlags & JNODE_RAW ){
177636 if( pNode->n!=nKey ) return 0;
177637 return strncmp(pNode->u.zJContent, zKey, nKey)==0;
177638 }else{
177639 if( pNode->n!=nKey+2 ) return 0;
177640 return strncmp(pNode->u.zJContent+1, zKey, nKey)==0;
177641 }
177642}
177643
177644/* forward declaration */
177645static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**);
177646
177647/*
177648** Search along zPath to find the node specified. Return a pointer
177649** to that node, or NULL if zPath is malformed or if there is no such
177650** node.
177651**
177652** If pApnd!=0, then try to append new nodes to complete zPath if it is
177653** possible to do so and if no existing node corresponds to zPath. If
177654** new nodes are appended *pApnd is set to 1.
177655*/
177656static JsonNode *jsonLookupStep(
177657 JsonParse *pParse, /* The JSON to search */
177658 u32 iRoot, /* Begin the search at this node */
177659 const char *zPath, /* The path to search */
177660 int *pApnd, /* Append nodes to complete path if not NULL */
177661 const char **pzErr /* Make *pzErr point to any syntax error in zPath */
177662){
177663 u32 i, j, nKey;
177664 const char *zKey;
177665 JsonNode *pRoot = &pParse->aNode[iRoot];
177666 if( zPath[0]==0 ) return pRoot;
177667 if( zPath[0]=='.' ){
177668 if( pRoot->eType!=JSON_OBJECT ) return 0;
177669 zPath++;
177670 if( zPath[0]=='"' ){
177671 zKey = zPath + 1;
177672 for(i=1; zPath[i] && zPath[i]!='"'; i++){}
177673 nKey = i-1;
177674 if( zPath[i] ){
177675 i++;
177676 }else{
177677 *pzErr = zPath;
177678 return 0;
177679 }
177680 }else{
177681 zKey = zPath;
177682 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
177683 nKey = i;
177684 }
177685 if( nKey==0 ){
177686 *pzErr = zPath;
177687 return 0;
177688 }
177689 j = 1;
177690 for(;;){
177691 while( j<=pRoot->n ){
177692 if( jsonLabelCompare(pRoot+j, zKey, nKey) ){
177693 return jsonLookupStep(pParse, iRoot+j+1, &zPath[i], pApnd, pzErr);
177694 }
177695 j++;
177696 j += jsonNodeSize(&pRoot[j]);
177697 }
177698 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
177699 iRoot += pRoot->u.iAppend;
177700 pRoot = &pParse->aNode[iRoot];
177701 j = 1;
177702 }
177703 if( pApnd ){
177704 u32 iStart, iLabel;
177705 JsonNode *pNode;
177706 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
177707 iLabel = jsonParseAddNode(pParse, JSON_STRING, i, zPath);
177708 zPath += i;
177709 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
177710 if( pParse->oom ) return 0;
177711 if( pNode ){
177712 pRoot = &pParse->aNode[iRoot];
177713 pRoot->u.iAppend = iStart - iRoot;
177714 pRoot->jnFlags |= JNODE_APPEND;
177715 pParse->aNode[iLabel].jnFlags |= JNODE_RAW;
177716 }
177717 return pNode;
177718 }
177719 }else if( zPath[0]=='[' && safe_isdigit(zPath[1]) ){
177720 if( pRoot->eType!=JSON_ARRAY ) return 0;
177721 i = 0;
177722 j = 1;
177723 while( safe_isdigit(zPath[j]) ){
177724 i = i*10 + zPath[j] - '0';
177725 j++;
177726 }
177727 if( zPath[j]!=']' ){
177728 *pzErr = zPath;
177729 return 0;
177730 }
177731 zPath += j + 1;
177732 j = 1;
177733 for(;;){
177734 while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){
177735 if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--;
177736 j += jsonNodeSize(&pRoot[j]);
177737 }
177738 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
177739 iRoot += pRoot->u.iAppend;
177740 pRoot = &pParse->aNode[iRoot];
177741 j = 1;
177742 }
177743 if( j<=pRoot->n ){
177744 return jsonLookupStep(pParse, iRoot+j, zPath, pApnd, pzErr);
177745 }
177746 if( i==0 && pApnd ){
177747 u32 iStart;
177748 JsonNode *pNode;
177749 iStart = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0);
177750 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
177751 if( pParse->oom ) return 0;
177752 if( pNode ){
177753 pRoot = &pParse->aNode[iRoot];
177754 pRoot->u.iAppend = iStart - iRoot;
177755 pRoot->jnFlags |= JNODE_APPEND;
177756 }
177757 return pNode;
177758 }
177759 }else{
177760 *pzErr = zPath;
177761 }
177762 return 0;
177763}
177764
177765/*
177766** Append content to pParse that will complete zPath. Return a pointer
177767** to the inserted node, or return NULL if the append fails.
177768*/
177769static JsonNode *jsonLookupAppend(
177770 JsonParse *pParse, /* Append content to the JSON parse */
177771 const char *zPath, /* Description of content to append */
177772 int *pApnd, /* Set this flag to 1 */
177773 const char **pzErr /* Make this point to any syntax error */
177774){
177775 *pApnd = 1;
177776 if( zPath[0]==0 ){
177777 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
177778 return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
177779 }
177780 if( zPath[0]=='.' ){
177781 jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
177782 }else if( strncmp(zPath,"[0]",3)==0 ){
177783 jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
177784 }else{
177785 return 0;
177786 }
177787 if( pParse->oom ) return 0;
177788 return jsonLookupStep(pParse, pParse->nNode-1, zPath, pApnd, pzErr);
177789}
177790
177791/*
177792** Return the text of a syntax error message on a JSON path. Space is
177793** obtained from sqlite3_malloc().
177794*/
177795static char *jsonPathSyntaxError(const char *zErr){
177796 return sqlite3_mprintf("JSON path error near '%q'", zErr);
177797}
177798
177799/*
177800** Do a node lookup using zPath. Return a pointer to the node on success.
177801** Return NULL if not found or if there is an error.
177802**
177803** On an error, write an error message into pCtx and increment the
177804** pParse->nErr counter.
177805**
177806** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if
177807** nodes are appended.
177808*/
177809static JsonNode *jsonLookup(
177810 JsonParse *pParse, /* The JSON to search */
177811 const char *zPath, /* The path to search */
177812 int *pApnd, /* Append nodes to complete path if not NULL */
177813 sqlite3_context *pCtx /* Report errors here, if not NULL */
177814){
177815 const char *zErr = 0;
177816 JsonNode *pNode = 0;
177817 char *zMsg;
177818
177819 if( zPath==0 ) return 0;
177820 if( zPath[0]!='$' ){
177821 zErr = zPath;
177822 goto lookup_err;
177823 }
177824 zPath++;
177825 pNode = jsonLookupStep(pParse, 0, zPath, pApnd, &zErr);
177826 if( zErr==0 ) return pNode;
177827
177828lookup_err:
177829 pParse->nErr++;
177830 assert( zErr!=0 && pCtx!=0 );
177831 zMsg = jsonPathSyntaxError(zErr);
177832 if( zMsg ){
177833 sqlite3_result_error(pCtx, zMsg, -1);
177834 sqlite3_free(zMsg);
177835 }else{
177836 sqlite3_result_error_nomem(pCtx);
177837 }
177838 return 0;
177839}
177840
177841
177842/*
177843** Report the wrong number of arguments for json_insert(), json_replace()
177844** or json_set().
177845*/
177846static void jsonWrongNumArgs(
177847 sqlite3_context *pCtx,
177848 const char *zFuncName
177849){
177850 char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
177851 zFuncName);
177852 sqlite3_result_error(pCtx, zMsg, -1);
177853 sqlite3_free(zMsg);
177854}
177855
177856/*
177857** Mark all NULL entries in the Object passed in as JNODE_REMOVE.
177858*/
177859static void jsonRemoveAllNulls(JsonNode *pNode){
177860 int i, n;
177861 assert( pNode->eType==JSON_OBJECT );
177862 n = pNode->n;
177863 for(i=2; i<=n; i += jsonNodeSize(&pNode[i])+1){
177864 switch( pNode[i].eType ){
177865 case JSON_NULL:
177866 pNode[i].jnFlags |= JNODE_REMOVE;
177867 break;
177868 case JSON_OBJECT:
177869 jsonRemoveAllNulls(&pNode[i]);
177870 break;
177871 }
177872 }
177873}
177874
177875
177876/****************************************************************************
177877** SQL functions used for testing and debugging
177878****************************************************************************/
177879
177880#ifdef SQLITE_DEBUG
177881/*
177882** The json_parse(JSON) function returns a string which describes
177883** a parse of the JSON provided. Or it returns NULL if JSON is not
177884** well-formed.
177885*/
177886static void jsonParseFunc(
177887 sqlite3_context *ctx,
177888 int argc,
177889 sqlite3_value **argv
177890){
177891 JsonString s; /* Output string - not real JSON */
177892 JsonParse x; /* The parse */
177893 u32 i;
177894
177895 assert( argc==1 );
177896 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
177897 jsonParseFindParents(&x);
177898 jsonInit(&s, ctx);
177899 for(i=0; i<x.nNode; i++){
177900 const char *zType;
177901 if( x.aNode[i].jnFlags & JNODE_LABEL ){
177902 assert( x.aNode[i].eType==JSON_STRING );
177903 zType = "label";
177904 }else{
177905 zType = jsonType[x.aNode[i].eType];
177906 }
177907 jsonPrintf(100, &s,"node %3u: %7s n=%-4d up=%-4d",
177908 i, zType, x.aNode[i].n, x.aUp[i]);
177909 if( x.aNode[i].u.zJContent!=0 ){
177910 jsonAppendRaw(&s, " ", 1);
177911 jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
177912 }
177913 jsonAppendRaw(&s, "\n", 1);
177914 }
177915 jsonParseReset(&x);
177916 jsonResult(&s);
177917}
177918
177919/*
177920** The json_test1(JSON) function return true (1) if the input is JSON
177921** text generated by another json function. It returns (0) if the input
177922** is not known to be JSON.
177923*/
177924static void jsonTest1Func(
177925 sqlite3_context *ctx,
177926 int argc,
177927 sqlite3_value **argv
177928){
177929 UNUSED_PARAM(argc);
177930 sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE);
177931}
177932#endif /* SQLITE_DEBUG */
177933
177934/****************************************************************************
177935** Scalar SQL function implementations
177936****************************************************************************/
177937
177938/*
177939** Implementation of the json_QUOTE(VALUE) function. Return a JSON value
177940** corresponding to the SQL value input. Mostly this means putting
177941** double-quotes around strings and returning the unquoted string "null"
177942** when given a NULL input.
177943*/
177944static void jsonQuoteFunc(
177945 sqlite3_context *ctx,
177946 int argc,
177947 sqlite3_value **argv
177948){
177949 JsonString jx;
177950 UNUSED_PARAM(argc);
177951
177952 jsonInit(&jx, ctx);
177953 jsonAppendValue(&jx, argv[0]);
177954 jsonResult(&jx);
177955 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
177956}
177957
177958/*
177959** Implementation of the json_array(VALUE,...) function. Return a JSON
177960** array that contains all values given in arguments. Or if any argument
177961** is a BLOB, throw an error.
177962*/
177963static void jsonArrayFunc(
177964 sqlite3_context *ctx,
177965 int argc,
177966 sqlite3_value **argv
177967){
177968 int i;
177969 JsonString jx;
177970
177971 jsonInit(&jx, ctx);
177972 jsonAppendChar(&jx, '[');
177973 for(i=0; i<argc; i++){
177974 jsonAppendSeparator(&jx);
177975 jsonAppendValue(&jx, argv[i]);
177976 }
177977 jsonAppendChar(&jx, ']');
177978 jsonResult(&jx);
177979 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
177980}
177981
177982
177983/*
177984** json_array_length(JSON)
177985** json_array_length(JSON, PATH)
177986**
177987** Return the number of elements in the top-level JSON array.
177988** Return 0 if the input is not a well-formed JSON array.
177989*/
177990static void jsonArrayLengthFunc(
177991 sqlite3_context *ctx,
177992 int argc,
177993 sqlite3_value **argv
177994){
177995 JsonParse *p; /* The parse */
177996 sqlite3_int64 n = 0;
177997 u32 i;
177998 JsonNode *pNode;
177999
178000 p = jsonParseCached(ctx, argv, ctx);
178001 if( p==0 ) return;
178002 assert( p->nNode );
178003 if( argc==2 ){
178004 const char *zPath = (const char*)sqlite3_value_text(argv[1]);
178005 pNode = jsonLookup(p, zPath, 0, ctx);
178006 }else{
178007 pNode = p->aNode;
178008 }
178009 if( pNode==0 ){
178010 return;
178011 }
178012 if( pNode->eType==JSON_ARRAY ){
178013 assert( (pNode->jnFlags & JNODE_APPEND)==0 );
178014 for(i=1; i<=pNode->n; n++){
178015 i += jsonNodeSize(&pNode[i]);
178016 }
178017 }
178018 sqlite3_result_int64(ctx, n);
178019}
178020
178021/*
178022** json_extract(JSON, PATH, ...)
178023**
178024** Return the element described by PATH. Return NULL if there is no
178025** PATH element. If there are multiple PATHs, then return a JSON array
178026** with the result from each path. Throw an error if the JSON or any PATH
178027** is malformed.
178028*/
178029static void jsonExtractFunc(
178030 sqlite3_context *ctx,
178031 int argc,
178032 sqlite3_value **argv
178033){
178034 JsonParse *p; /* The parse */
178035 JsonNode *pNode;
178036 const char *zPath;
178037 JsonString jx;
178038 int i;
178039
178040 if( argc<2 ) return;
178041 p = jsonParseCached(ctx, argv, ctx);
178042 if( p==0 ) return;
178043 jsonInit(&jx, ctx);
178044 jsonAppendChar(&jx, '[');
178045 for(i=1; i<argc; i++){
178046 zPath = (const char*)sqlite3_value_text(argv[i]);
178047 pNode = jsonLookup(p, zPath, 0, ctx);
178048 if( p->nErr ) break;
178049 if( argc>2 ){
178050 jsonAppendSeparator(&jx);
178051 if( pNode ){
178052 jsonRenderNode(pNode, &jx, 0);
178053 }else{
178054 jsonAppendRaw(&jx, "null", 4);
178055 }
178056 }else if( pNode ){
178057 jsonReturn(pNode, ctx, 0);
178058 }
178059 }
178060 if( argc>2 && i==argc ){
178061 jsonAppendChar(&jx, ']');
178062 jsonResult(&jx);
178063 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
178064 }
178065 jsonReset(&jx);
178066}
178067
178068/* This is the RFC 7396 MergePatch algorithm.
178069*/
178070static JsonNode *jsonMergePatch(
178071 JsonParse *pParse, /* The JSON parser that contains the TARGET */
178072 u32 iTarget, /* Node of the TARGET in pParse */
178073 JsonNode *pPatch /* The PATCH */
178074){
178075 u32 i, j;
178076 u32 iRoot;
178077 JsonNode *pTarget;
178078 if( pPatch->eType!=JSON_OBJECT ){
178079 return pPatch;
178080 }
178081 assert( iTarget>=0 && iTarget<pParse->nNode );
178082 pTarget = &pParse->aNode[iTarget];
178083 assert( (pPatch->jnFlags & JNODE_APPEND)==0 );
178084 if( pTarget->eType!=JSON_OBJECT ){
178085 jsonRemoveAllNulls(pPatch);
178086 return pPatch;
178087 }
178088 iRoot = iTarget;
178089 for(i=1; i<pPatch->n; i += jsonNodeSize(&pPatch[i+1])+1){
178090 u32 nKey;
178091 const char *zKey;
178092 assert( pPatch[i].eType==JSON_STRING );
178093 assert( pPatch[i].jnFlags & JNODE_LABEL );
178094 nKey = pPatch[i].n;
178095 zKey = pPatch[i].u.zJContent;
178096 assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
178097 for(j=1; j<pTarget->n; j += jsonNodeSize(&pTarget[j+1])+1 ){
178098 assert( pTarget[j].eType==JSON_STRING );
178099 assert( pTarget[j].jnFlags & JNODE_LABEL );
178100 assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
178101 if( pTarget[j].n==nKey && strncmp(pTarget[j].u.zJContent,zKey,nKey)==0 ){
178102 if( pTarget[j+1].jnFlags & (JNODE_REMOVE|JNODE_PATCH) ) break;
178103 if( pPatch[i+1].eType==JSON_NULL ){
178104 pTarget[j+1].jnFlags |= JNODE_REMOVE;
178105 }else{
178106 JsonNode *pNew = jsonMergePatch(pParse, iTarget+j+1, &pPatch[i+1]);
178107 if( pNew==0 ) return 0;
178108 pTarget = &pParse->aNode[iTarget];
178109 if( pNew!=&pTarget[j+1] ){
178110 pTarget[j+1].u.pPatch = pNew;
178111 pTarget[j+1].jnFlags |= JNODE_PATCH;
178112 }
178113 }
178114 break;
178115 }
178116 }
178117 if( j>=pTarget->n && pPatch[i+1].eType!=JSON_NULL ){
178118 int iStart, iPatch;
178119 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
178120 jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
178121 iPatch = jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
178122 if( pParse->oom ) return 0;
178123 jsonRemoveAllNulls(pPatch);
178124 pTarget = &pParse->aNode[iTarget];
178125 pParse->aNode[iRoot].jnFlags |= JNODE_APPEND;
178126 pParse->aNode[iRoot].u.iAppend = iStart - iRoot;
178127 iRoot = iStart;
178128 pParse->aNode[iPatch].jnFlags |= JNODE_PATCH;
178129 pParse->aNode[iPatch].u.pPatch = &pPatch[i+1];
178130 }
178131 }
178132 return pTarget;
178133}
178134
178135/*
178136** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON
178137** object that is the result of running the RFC 7396 MergePatch() algorithm
178138** on the two arguments.
178139*/
178140static void jsonPatchFunc(
178141 sqlite3_context *ctx,
178142 int argc,
178143 sqlite3_value **argv
178144){
178145 JsonParse x; /* The JSON that is being patched */
178146 JsonParse y; /* The patch */
178147 JsonNode *pResult; /* The result of the merge */
178148
178149 UNUSED_PARAM(argc);
178150 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
178151 if( jsonParse(&y, ctx, (const char*)sqlite3_value_text(argv[1])) ){
178152 jsonParseReset(&x);
178153 return;
178154 }
178155 pResult = jsonMergePatch(&x, 0, y.aNode);
178156 assert( pResult!=0 || x.oom );
178157 if( pResult ){
178158 jsonReturnJson(pResult, ctx, 0);
178159 }else{
178160 sqlite3_result_error_nomem(ctx);
178161 }
178162 jsonParseReset(&x);
178163 jsonParseReset(&y);
178164}
178165
178166
178167/*
178168** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
178169** object that contains all name/value given in arguments. Or if any name
178170** is not a string or if any value is a BLOB, throw an error.
178171*/
178172static void jsonObjectFunc(
178173 sqlite3_context *ctx,
178174 int argc,
178175 sqlite3_value **argv
178176){
178177 int i;
178178 JsonString jx;
178179 const char *z;
178180 u32 n;
178181
178182 if( argc&1 ){
178183 sqlite3_result_error(ctx, "json_object() requires an even number "
178184 "of arguments", -1);
178185 return;
178186 }
178187 jsonInit(&jx, ctx);
178188 jsonAppendChar(&jx, '{');
178189 for(i=0; i<argc; i+=2){
178190 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
178191 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
178192 jsonReset(&jx);
178193 return;
178194 }
178195 jsonAppendSeparator(&jx);
178196 z = (const char*)sqlite3_value_text(argv[i]);
178197 n = (u32)sqlite3_value_bytes(argv[i]);
178198 jsonAppendString(&jx, z, n);
178199 jsonAppendChar(&jx, ':');
178200 jsonAppendValue(&jx, argv[i+1]);
178201 }
178202 jsonAppendChar(&jx, '}');
178203 jsonResult(&jx);
178204 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
178205}
178206
178207
178208/*
178209** json_remove(JSON, PATH, ...)
178210**
178211** Remove the named elements from JSON and return the result. malformed
178212** JSON or PATH arguments result in an error.
178213*/
178214static void jsonRemoveFunc(
178215 sqlite3_context *ctx,
178216 int argc,
178217 sqlite3_value **argv
178218){
178219 JsonParse x; /* The parse */
178220 JsonNode *pNode;
178221 const char *zPath;
178222 u32 i;
178223
178224 if( argc<1 ) return;
178225 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
178226 assert( x.nNode );
178227 for(i=1; i<(u32)argc; i++){
178228 zPath = (const char*)sqlite3_value_text(argv[i]);
178229 if( zPath==0 ) goto remove_done;
178230 pNode = jsonLookup(&x, zPath, 0, ctx);
178231 if( x.nErr ) goto remove_done;
178232 if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
178233 }
178234 if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
178235 jsonReturnJson(x.aNode, ctx, 0);
178236 }
178237remove_done:
178238 jsonParseReset(&x);
178239}
178240
178241/*
178242** json_replace(JSON, PATH, VALUE, ...)
178243**
178244** Replace the value at PATH with VALUE. If PATH does not already exist,
178245** this routine is a no-op. If JSON or PATH is malformed, throw an error.
178246*/
178247static void jsonReplaceFunc(
178248 sqlite3_context *ctx,
178249 int argc,
178250 sqlite3_value **argv
178251){
178252 JsonParse x; /* The parse */
178253 JsonNode *pNode;
178254 const char *zPath;
178255 u32 i;
178256
178257 if( argc<1 ) return;
178258 if( (argc&1)==0 ) {
178259 jsonWrongNumArgs(ctx, "replace");
178260 return;
178261 }
178262 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
178263 assert( x.nNode );
178264 for(i=1; i<(u32)argc; i+=2){
178265 zPath = (const char*)sqlite3_value_text(argv[i]);
178266 pNode = jsonLookup(&x, zPath, 0, ctx);
178267 if( x.nErr ) goto replace_err;
178268 if( pNode ){
178269 pNode->jnFlags |= (u8)JNODE_REPLACE;
178270 pNode->u.iReplace = i + 1;
178271 }
178272 }
178273 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
178274 sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
178275 }else{
178276 jsonReturnJson(x.aNode, ctx, argv);
178277 }
178278replace_err:
178279 jsonParseReset(&x);
178280}
178281
178282/*
178283** json_set(JSON, PATH, VALUE, ...)
178284**
178285** Set the value at PATH to VALUE. Create the PATH if it does not already
178286** exist. Overwrite existing values that do exist.
178287** If JSON or PATH is malformed, throw an error.
178288**
178289** json_insert(JSON, PATH, VALUE, ...)
178290**
178291** Create PATH and initialize it to VALUE. If PATH already exists, this
178292** routine is a no-op. If JSON or PATH is malformed, throw an error.
178293*/
178294static void jsonSetFunc(
178295 sqlite3_context *ctx,
178296 int argc,
178297 sqlite3_value **argv
178298){
178299 JsonParse x; /* The parse */
178300 JsonNode *pNode;
178301 const char *zPath;
178302 u32 i;
178303 int bApnd;
178304 int bIsSet = *(int*)sqlite3_user_data(ctx);
178305
178306 if( argc<1 ) return;
178307 if( (argc&1)==0 ) {
178308 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
178309 return;
178310 }
178311 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
178312 assert( x.nNode );
178313 for(i=1; i<(u32)argc; i+=2){
178314 zPath = (const char*)sqlite3_value_text(argv[i]);
178315 bApnd = 0;
178316 pNode = jsonLookup(&x, zPath, &bApnd, ctx);
178317 if( x.oom ){
178318 sqlite3_result_error_nomem(ctx);
178319 goto jsonSetDone;
178320 }else if( x.nErr ){
178321 goto jsonSetDone;
178322 }else if( pNode && (bApnd || bIsSet) ){
178323 pNode->jnFlags |= (u8)JNODE_REPLACE;
178324 pNode->u.iReplace = i + 1;
178325 }
178326 }
178327 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
178328 sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
178329 }else{
178330 jsonReturnJson(x.aNode, ctx, argv);
178331 }
178332jsonSetDone:
178333 jsonParseReset(&x);
178334}
178335
178336/*
178337** json_type(JSON)
178338** json_type(JSON, PATH)
178339**
178340** Return the top-level "type" of a JSON string. Throw an error if
178341** either the JSON or PATH inputs are not well-formed.
178342*/
178343static void jsonTypeFunc(
178344 sqlite3_context *ctx,
178345 int argc,
178346 sqlite3_value **argv
178347){
178348 JsonParse *p; /* The parse */
178349 const char *zPath;
178350 JsonNode *pNode;
178351
178352 p = jsonParseCached(ctx, argv, ctx);
178353 if( p==0 ) return;
178354 if( argc==2 ){
178355 zPath = (const char*)sqlite3_value_text(argv[1]);
178356 pNode = jsonLookup(p, zPath, 0, ctx);
178357 }else{
178358 pNode = p->aNode;
178359 }
178360 if( pNode ){
178361 sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC);
178362 }
178363}
178364
178365/*
178366** json_valid(JSON)
178367**
178368** Return 1 if JSON is a well-formed JSON string according to RFC-7159.
178369** Return 0 otherwise.
178370*/
178371static void jsonValidFunc(
178372 sqlite3_context *ctx,
178373 int argc,
178374 sqlite3_value **argv
178375){
178376 JsonParse *p; /* The parse */
178377 UNUSED_PARAM(argc);
178378 p = jsonParseCached(ctx, argv, 0);
178379 sqlite3_result_int(ctx, p!=0);
178380}
178381
178382
178383/****************************************************************************
178384** Aggregate SQL function implementations
178385****************************************************************************/
178386/*
178387** json_group_array(VALUE)
178388**
178389** Return a JSON array composed of all values in the aggregate.
178390*/
178391static void jsonArrayStep(
178392 sqlite3_context *ctx,
178393 int argc,
178394 sqlite3_value **argv
178395){
178396 JsonString *pStr;
178397 UNUSED_PARAM(argc);
178398 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
178399 if( pStr ){
178400 if( pStr->zBuf==0 ){
178401 jsonInit(pStr, ctx);
178402 jsonAppendChar(pStr, '[');
178403 }else{
178404 jsonAppendChar(pStr, ',');
178405 pStr->pCtx = ctx;
178406 }
178407 jsonAppendValue(pStr, argv[0]);
178408 }
178409}
178410static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
178411 JsonString *pStr;
178412 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
178413 if( pStr ){
178414 pStr->pCtx = ctx;
178415 jsonAppendChar(pStr, ']');
178416 if( pStr->bErr ){
178417 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
178418 assert( pStr->bStatic );
178419 }else if( isFinal ){
178420 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
178421 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
178422 pStr->bStatic = 1;
178423 }else{
178424 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
178425 pStr->nUsed--;
178426 }
178427 }else{
178428 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
178429 }
178430 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
178431}
178432static void jsonArrayValue(sqlite3_context *ctx){
178433 jsonArrayCompute(ctx, 0);
178434}
178435static void jsonArrayFinal(sqlite3_context *ctx){
178436 jsonArrayCompute(ctx, 1);
178437}
178438
178439#ifndef SQLITE_OMIT_WINDOWFUNC
178440/*
178441** This method works for both json_group_array() and json_group_object().
178442** It works by removing the first element of the group by searching forward
178443** to the first comma (",") that is not within a string and deleting all
178444** text through that comma.
178445*/
178446static void jsonGroupInverse(
178447 sqlite3_context *ctx,
178448 int argc,
178449 sqlite3_value **argv
178450){
178451 int i;
178452 int inStr = 0;
178453 char *z;
178454 JsonString *pStr;
178455 UNUSED_PARAM(argc);
178456 UNUSED_PARAM(argv);
178457 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
178458#ifdef NEVER
178459 /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
178460 ** always have been called to initalize it */
178461 if( NEVER(!pStr) ) return;
178462#endif
178463 z = pStr->zBuf;
178464 for(i=1; z[i]!=',' || inStr; i++){
178465 assert( i<pStr->nUsed );
178466 if( z[i]=='"' ){
178467 inStr = !inStr;
178468 }else if( z[i]=='\\' ){
178469 i++;
178470 }
178471 }
178472 pStr->nUsed -= i;
178473 memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1);
178474}
178475#else
178476# define jsonGroupInverse 0
178477#endif
178478
178479
178480/*
178481** json_group_obj(NAME,VALUE)
178482**
178483** Return a JSON object composed of all names and values in the aggregate.
178484*/
178485static void jsonObjectStep(
178486 sqlite3_context *ctx,
178487 int argc,
178488 sqlite3_value **argv
178489){
178490 JsonString *pStr;
178491 const char *z;
178492 u32 n;
178493 UNUSED_PARAM(argc);
178494 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
178495 if( pStr ){
178496 if( pStr->zBuf==0 ){
178497 jsonInit(pStr, ctx);
178498 jsonAppendChar(pStr, '{');
178499 }else{
178500 jsonAppendChar(pStr, ',');
178501 pStr->pCtx = ctx;
178502 }
178503 z = (const char*)sqlite3_value_text(argv[0]);
178504 n = (u32)sqlite3_value_bytes(argv[0]);
178505 jsonAppendString(pStr, z, n);
178506 jsonAppendChar(pStr, ':');
178507 jsonAppendValue(pStr, argv[1]);
178508 }
178509}
178510static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
178511 JsonString *pStr;
178512 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
178513 if( pStr ){
178514 jsonAppendChar(pStr, '}');
178515 if( pStr->bErr ){
178516 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
178517 assert( pStr->bStatic );
178518 }else if( isFinal ){
178519 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
178520 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
178521 pStr->bStatic = 1;
178522 }else{
178523 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
178524 pStr->nUsed--;
178525 }
178526 }else{
178527 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);
178528 }
178529 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
178530}
178531static void jsonObjectValue(sqlite3_context *ctx){
178532 jsonObjectCompute(ctx, 0);
178533}
178534static void jsonObjectFinal(sqlite3_context *ctx){
178535 jsonObjectCompute(ctx, 1);
178536}
178537
178538
178539
178540#ifndef SQLITE_OMIT_VIRTUALTABLE
178541/****************************************************************************
178542** The json_each virtual table
178543****************************************************************************/
178544typedef struct JsonEachCursor JsonEachCursor;
178545struct JsonEachCursor {
178546 sqlite3_vtab_cursor base; /* Base class - must be first */
178547 u32 iRowid; /* The rowid */
178548 u32 iBegin; /* The first node of the scan */
178549 u32 i; /* Index in sParse.aNode[] of current row */
178550 u32 iEnd; /* EOF when i equals or exceeds this value */
178551 u8 eType; /* Type of top-level element */
178552 u8 bRecursive; /* True for json_tree(). False for json_each() */
178553 char *zJson; /* Input JSON */
178554 char *zRoot; /* Path by which to filter zJson */
178555 JsonParse sParse; /* Parse of the input JSON */
178556};
178557
178558/* Constructor for the json_each virtual table */
178559static int jsonEachConnect(
178560 sqlite3 *db,
178561 void *pAux,
178562 int argc, const char *const*argv,
178563 sqlite3_vtab **ppVtab,
178564 char **pzErr
178565){
178566 sqlite3_vtab *pNew;
178567 int rc;
178568
178569/* Column numbers */
178570#define JEACH_KEY 0
178571#define JEACH_VALUE 1
178572#define JEACH_TYPE 2
178573#define JEACH_ATOM 3
178574#define JEACH_ID 4
178575#define JEACH_PARENT 5
178576#define JEACH_FULLKEY 6
178577#define JEACH_PATH 7
178578/* The xBestIndex method assumes that the JSON and ROOT columns are
178579** the last two columns in the table. Should this ever changes, be
178580** sure to update the xBestIndex method. */
178581#define JEACH_JSON 8
178582#define JEACH_ROOT 9
178583
178584 UNUSED_PARAM(pzErr);
178585 UNUSED_PARAM(argv);
178586 UNUSED_PARAM(argc);
178587 UNUSED_PARAM(pAux);
178588 rc = sqlite3_declare_vtab(db,
178589 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
178590 "json HIDDEN,root HIDDEN)");
178591 if( rc==SQLITE_OK ){
178592 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
178593 if( pNew==0 ) return SQLITE_NOMEM;
178594 memset(pNew, 0, sizeof(*pNew));
178595 }
178596 return rc;
178597}
178598
178599/* destructor for json_each virtual table */
178600static int jsonEachDisconnect(sqlite3_vtab *pVtab){
178601 sqlite3_free(pVtab);
178602 return SQLITE_OK;
178603}
178604
178605/* constructor for a JsonEachCursor object for json_each(). */
178606static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
178607 JsonEachCursor *pCur;
178608
178609 UNUSED_PARAM(p);
178610 pCur = sqlite3_malloc( sizeof(*pCur) );
178611 if( pCur==0 ) return SQLITE_NOMEM;
178612 memset(pCur, 0, sizeof(*pCur));
178613 *ppCursor = &pCur->base;
178614 return SQLITE_OK;
178615}
178616
178617/* constructor for a JsonEachCursor object for json_tree(). */
178618static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
178619 int rc = jsonEachOpenEach(p, ppCursor);
178620 if( rc==SQLITE_OK ){
178621 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
178622 pCur->bRecursive = 1;
178623 }
178624 return rc;
178625}
178626
178627/* Reset a JsonEachCursor back to its original state. Free any memory
178628** held. */
178629static void jsonEachCursorReset(JsonEachCursor *p){
178630 sqlite3_free(p->zJson);
178631 sqlite3_free(p->zRoot);
178632 jsonParseReset(&p->sParse);
178633 p->iRowid = 0;
178634 p->i = 0;
178635 p->iEnd = 0;
178636 p->eType = 0;
178637 p->zJson = 0;
178638 p->zRoot = 0;
178639}
178640
178641/* Destructor for a jsonEachCursor object */
178642static int jsonEachClose(sqlite3_vtab_cursor *cur){
178643 JsonEachCursor *p = (JsonEachCursor*)cur;
178644 jsonEachCursorReset(p);
178645 sqlite3_free(cur);
178646 return SQLITE_OK;
178647}
178648
178649/* Return TRUE if the jsonEachCursor object has been advanced off the end
178650** of the JSON object */
178651static int jsonEachEof(sqlite3_vtab_cursor *cur){
178652 JsonEachCursor *p = (JsonEachCursor*)cur;
178653 return p->i >= p->iEnd;
178654}
178655
178656/* Advance the cursor to the next element for json_tree() */
178657static int jsonEachNext(sqlite3_vtab_cursor *cur){
178658 JsonEachCursor *p = (JsonEachCursor*)cur;
178659 if( p->bRecursive ){
178660 if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++;
178661 p->i++;
178662 p->iRowid++;
178663 if( p->i<p->iEnd ){
178664 u32 iUp = p->sParse.aUp[p->i];
178665 JsonNode *pUp = &p->sParse.aNode[iUp];
178666 p->eType = pUp->eType;
178667 if( pUp->eType==JSON_ARRAY ){
178668 if( iUp==p->i-1 ){
178669 pUp->u.iKey = 0;
178670 }else{
178671 pUp->u.iKey++;
178672 }
178673 }
178674 }
178675 }else{
178676 switch( p->eType ){
178677 case JSON_ARRAY: {
178678 p->i += jsonNodeSize(&p->sParse.aNode[p->i]);
178679 p->iRowid++;
178680 break;
178681 }
178682 case JSON_OBJECT: {
178683 p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]);
178684 p->iRowid++;
178685 break;
178686 }
178687 default: {
178688 p->i = p->iEnd;
178689 break;
178690 }
178691 }
178692 }
178693 return SQLITE_OK;
178694}
178695
178696/* Append the name of the path for element i to pStr
178697*/
178698static void jsonEachComputePath(
178699 JsonEachCursor *p, /* The cursor */
178700 JsonString *pStr, /* Write the path here */
178701 u32 i /* Path to this element */
178702){
178703 JsonNode *pNode, *pUp;
178704 u32 iUp;
178705 if( i==0 ){
178706 jsonAppendChar(pStr, '$');
178707 return;
178708 }
178709 iUp = p->sParse.aUp[i];
178710 jsonEachComputePath(p, pStr, iUp);
178711 pNode = &p->sParse.aNode[i];
178712 pUp = &p->sParse.aNode[iUp];
178713 if( pUp->eType==JSON_ARRAY ){
178714 jsonPrintf(30, pStr, "[%d]", pUp->u.iKey);
178715 }else{
178716 assert( pUp->eType==JSON_OBJECT );
178717 if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--;
178718 assert( pNode->eType==JSON_STRING );
178719 assert( pNode->jnFlags & JNODE_LABEL );
178720 jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1);
178721 }
178722}
178723
178724/* Return the value of a column */
178725static int jsonEachColumn(
178726 sqlite3_vtab_cursor *cur, /* The cursor */
178727 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
178728 int i /* Which column to return */
178729){
178730 JsonEachCursor *p = (JsonEachCursor*)cur;
178731 JsonNode *pThis = &p->sParse.aNode[p->i];
178732 switch( i ){
178733 case JEACH_KEY: {
178734 if( p->i==0 ) break;
178735 if( p->eType==JSON_OBJECT ){
178736 jsonReturn(pThis, ctx, 0);
178737 }else if( p->eType==JSON_ARRAY ){
178738 u32 iKey;
178739 if( p->bRecursive ){
178740 if( p->iRowid==0 ) break;
178741 iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey;
178742 }else{
178743 iKey = p->iRowid;
178744 }
178745 sqlite3_result_int64(ctx, (sqlite3_int64)iKey);
178746 }
178747 break;
178748 }
178749 case JEACH_VALUE: {
178750 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
178751 jsonReturn(pThis, ctx, 0);
178752 break;
178753 }
178754 case JEACH_TYPE: {
178755 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
178756 sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC);
178757 break;
178758 }
178759 case JEACH_ATOM: {
178760 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
178761 if( pThis->eType>=JSON_ARRAY ) break;
178762 jsonReturn(pThis, ctx, 0);
178763 break;
178764 }
178765 case JEACH_ID: {
178766 sqlite3_result_int64(ctx,
178767 (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
178768 break;
178769 }
178770 case JEACH_PARENT: {
178771 if( p->i>p->iBegin && p->bRecursive ){
178772 sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]);
178773 }
178774 break;
178775 }
178776 case JEACH_FULLKEY: {
178777 JsonString x;
178778 jsonInit(&x, ctx);
178779 if( p->bRecursive ){
178780 jsonEachComputePath(p, &x, p->i);
178781 }else{
178782 if( p->zRoot ){
178783 jsonAppendRaw(&x, p->zRoot, (int)strlen(p->zRoot));
178784 }else{
178785 jsonAppendChar(&x, '$');
178786 }
178787 if( p->eType==JSON_ARRAY ){
178788 jsonPrintf(30, &x, "[%d]", p->iRowid);
178789 }else if( p->eType==JSON_OBJECT ){
178790 jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
178791 }
178792 }
178793 jsonResult(&x);
178794 break;
178795 }
178796 case JEACH_PATH: {
178797 if( p->bRecursive ){
178798 JsonString x;
178799 jsonInit(&x, ctx);
178800 jsonEachComputePath(p, &x, p->sParse.aUp[p->i]);
178801 jsonResult(&x);
178802 break;
178803 }
178804 /* For json_each() path and root are the same so fall through
178805 ** into the root case */
178806 }
178807 default: {
178808 const char *zRoot = p->zRoot;
178809 if( zRoot==0 ) zRoot = "$";
178810 sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC);
178811 break;
178812 }
178813 case JEACH_JSON: {
178814 assert( i==JEACH_JSON );
178815 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC);
178816 break;
178817 }
178818 }
178819 return SQLITE_OK;
178820}
178821
178822/* Return the current rowid value */
178823static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
178824 JsonEachCursor *p = (JsonEachCursor*)cur;
178825 *pRowid = p->iRowid;
178826 return SQLITE_OK;
178827}
178828
178829/* The query strategy is to look for an equality constraint on the json
178830** column. Without such a constraint, the table cannot operate. idxNum is
178831** 1 if the constraint is found, 3 if the constraint and zRoot are found,
178832** and 0 otherwise.
178833*/
178834static int jsonEachBestIndex(
178835 sqlite3_vtab *tab,
178836 sqlite3_index_info *pIdxInfo
178837){
178838 int i; /* Loop counter or computed array index */
178839 int aIdx[2]; /* Index of constraints for JSON and ROOT */
178840 int unusableMask = 0; /* Mask of unusable JSON and ROOT constraints */
178841 int idxMask = 0; /* Mask of usable == constraints JSON and ROOT */
178842 const struct sqlite3_index_constraint *pConstraint;
178843
178844 /* This implementation assumes that JSON and ROOT are the last two
178845 ** columns in the table */
178846 assert( JEACH_ROOT == JEACH_JSON+1 );
178847 UNUSED_PARAM(tab);
178848 aIdx[0] = aIdx[1] = -1;
178849 pConstraint = pIdxInfo->aConstraint;
178850 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
178851 int iCol;
178852 int iMask;
178853 if( pConstraint->iColumn < JEACH_JSON ) continue;
178854 iCol = pConstraint->iColumn - JEACH_JSON;
178855 assert( iCol==0 || iCol==1 );
178856 iMask = 1 << iCol;
178857 if( pConstraint->usable==0 ){
178858 unusableMask |= iMask;
178859 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
178860 aIdx[iCol] = i;
178861 idxMask |= iMask;
178862 }
178863 }
178864 if( (unusableMask & ~idxMask)!=0 ){
178865 /* If there are any unusable constraints on JSON or ROOT, then reject
178866 ** this entire plan */
178867 return SQLITE_CONSTRAINT;
178868 }
178869 if( aIdx[0]<0 ){
178870 /* No JSON input. Leave estimatedCost at the huge value that it was
178871 ** initialized to to discourage the query planner from selecting this
178872 ** plan. */
178873 pIdxInfo->idxNum = 0;
178874 }else{
178875 pIdxInfo->estimatedCost = 1.0;
178876 i = aIdx[0];
178877 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
178878 pIdxInfo->aConstraintUsage[i].omit = 1;
178879 if( aIdx[1]<0 ){
178880 pIdxInfo->idxNum = 1; /* Only JSON supplied. Plan 1 */
178881 }else{
178882 i = aIdx[1];
178883 pIdxInfo->aConstraintUsage[i].argvIndex = 2;
178884 pIdxInfo->aConstraintUsage[i].omit = 1;
178885 pIdxInfo->idxNum = 3; /* Both JSON and ROOT are supplied. Plan 3 */
178886 }
178887 }
178888 return SQLITE_OK;
178889}
178890
178891/* Start a search on a new JSON string */
178892static int jsonEachFilter(
178893 sqlite3_vtab_cursor *cur,
178894 int idxNum, const char *idxStr,
178895 int argc, sqlite3_value **argv
178896){
178897 JsonEachCursor *p = (JsonEachCursor*)cur;
178898 const char *z;
178899 const char *zRoot = 0;
178900 sqlite3_int64 n;
178901
178902 UNUSED_PARAM(idxStr);
178903 UNUSED_PARAM(argc);
178904 jsonEachCursorReset(p);
178905 if( idxNum==0 ) return SQLITE_OK;
178906 z = (const char*)sqlite3_value_text(argv[0]);
178907 if( z==0 ) return SQLITE_OK;
178908 n = sqlite3_value_bytes(argv[0]);
178909 p->zJson = sqlite3_malloc64( n+1 );
178910 if( p->zJson==0 ) return SQLITE_NOMEM;
178911 memcpy(p->zJson, z, (size_t)n+1);
178912 if( jsonParse(&p->sParse, 0, p->zJson) ){
178913 int rc = SQLITE_NOMEM;
178914 if( p->sParse.oom==0 ){
178915 sqlite3_free(cur->pVtab->zErrMsg);
178916 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
178917 if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR;
178918 }
178919 jsonEachCursorReset(p);
178920 return rc;
178921 }else if( p->bRecursive && jsonParseFindParents(&p->sParse) ){
178922 jsonEachCursorReset(p);
178923 return SQLITE_NOMEM;
178924 }else{
178925 JsonNode *pNode = 0;
178926 if( idxNum==3 ){
178927 const char *zErr = 0;
178928 zRoot = (const char*)sqlite3_value_text(argv[1]);
178929 if( zRoot==0 ) return SQLITE_OK;
178930 n = sqlite3_value_bytes(argv[1]);
178931 p->zRoot = sqlite3_malloc64( n+1 );
178932 if( p->zRoot==0 ) return SQLITE_NOMEM;
178933 memcpy(p->zRoot, zRoot, (size_t)n+1);
178934 if( zRoot[0]!='$' ){
178935 zErr = zRoot;
178936 }else{
178937 pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr);
178938 }
178939 if( zErr ){
178940 sqlite3_free(cur->pVtab->zErrMsg);
178941 cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
178942 jsonEachCursorReset(p);
178943 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
178944 }else if( pNode==0 ){
178945 return SQLITE_OK;
178946 }
178947 }else{
178948 pNode = p->sParse.aNode;
178949 }
178950 p->iBegin = p->i = (int)(pNode - p->sParse.aNode);
178951 p->eType = pNode->eType;
178952 if( p->eType>=JSON_ARRAY ){
178953 pNode->u.iKey = 0;
178954 p->iEnd = p->i + pNode->n + 1;
178955 if( p->bRecursive ){
178956 p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType;
178957 if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){
178958 p->i--;
178959 }
178960 }else{
178961 p->i++;
178962 }
178963 }else{
178964 p->iEnd = p->i+1;
178965 }
178966 }
178967 return SQLITE_OK;
178968}
178969
178970/* The methods of the json_each virtual table */
178971static sqlite3_module jsonEachModule = {
178972 0, /* iVersion */
178973 0, /* xCreate */
178974 jsonEachConnect, /* xConnect */
178975 jsonEachBestIndex, /* xBestIndex */
178976 jsonEachDisconnect, /* xDisconnect */
178977 0, /* xDestroy */
178978 jsonEachOpenEach, /* xOpen - open a cursor */
178979 jsonEachClose, /* xClose - close a cursor */
178980 jsonEachFilter, /* xFilter - configure scan constraints */
178981 jsonEachNext, /* xNext - advance a cursor */
178982 jsonEachEof, /* xEof - check for end of scan */
178983 jsonEachColumn, /* xColumn - read data */
178984 jsonEachRowid, /* xRowid - read data */
178985 0, /* xUpdate */
178986 0, /* xBegin */
178987 0, /* xSync */
178988 0, /* xCommit */
178989 0, /* xRollback */
178990 0, /* xFindMethod */
178991 0, /* xRename */
178992 0, /* xSavepoint */
178993 0, /* xRelease */
178994 0, /* xRollbackTo */
178995 0 /* xShadowName */
178996};
178997
178998/* The methods of the json_tree virtual table. */
178999static sqlite3_module jsonTreeModule = {
179000 0, /* iVersion */
179001 0, /* xCreate */
179002 jsonEachConnect, /* xConnect */
179003 jsonEachBestIndex, /* xBestIndex */
179004 jsonEachDisconnect, /* xDisconnect */
179005 0, /* xDestroy */
179006 jsonEachOpenTree, /* xOpen - open a cursor */
179007 jsonEachClose, /* xClose - close a cursor */
179008 jsonEachFilter, /* xFilter - configure scan constraints */
179009 jsonEachNext, /* xNext - advance a cursor */
179010 jsonEachEof, /* xEof - check for end of scan */
179011 jsonEachColumn, /* xColumn - read data */
179012 jsonEachRowid, /* xRowid - read data */
179013 0, /* xUpdate */
179014 0, /* xBegin */
179015 0, /* xSync */
179016 0, /* xCommit */
179017 0, /* xRollback */
179018 0, /* xFindMethod */
179019 0, /* xRename */
179020 0, /* xSavepoint */
179021 0, /* xRelease */
179022 0, /* xRollbackTo */
179023 0 /* xShadowName */
179024};
179025#endif /* SQLITE_OMIT_VIRTUALTABLE */
179026
179027/****************************************************************************
179028** The following routines are the only publically visible identifiers in this
179029** file. Call the following routines in order to register the various SQL
179030** functions and the virtual table implemented by this file.
179031****************************************************************************/
179032
179033SQLITE_PRIVATE int sqlite3Json1Init(sqlite3 *db){
179034 int rc = SQLITE_OK;
179035 unsigned int i;
179036 static const struct {
179037 const char *zName;
179038 int nArg;
179039 int flag;
179040 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
179041 } aFunc[] = {
179042 { "json", 1, 0, jsonRemoveFunc },
179043 { "json_array", -1, 0, jsonArrayFunc },
179044 { "json_array_length", 1, 0, jsonArrayLengthFunc },
179045 { "json_array_length", 2, 0, jsonArrayLengthFunc },
179046 { "json_extract", -1, 0, jsonExtractFunc },
179047 { "json_insert", -1, 0, jsonSetFunc },
179048 { "json_object", -1, 0, jsonObjectFunc },
179049 { "json_patch", 2, 0, jsonPatchFunc },
179050 { "json_quote", 1, 0, jsonQuoteFunc },
179051 { "json_remove", -1, 0, jsonRemoveFunc },
179052 { "json_replace", -1, 0, jsonReplaceFunc },
179053 { "json_set", -1, 1, jsonSetFunc },
179054 { "json_type", 1, 0, jsonTypeFunc },
179055 { "json_type", 2, 0, jsonTypeFunc },
179056 { "json_valid", 1, 0, jsonValidFunc },
179057
179058#if SQLITE_DEBUG
179059 /* DEBUG and TESTING functions */
179060 { "json_parse", 1, 0, jsonParseFunc },
179061 { "json_test1", 1, 0, jsonTest1Func },
179062#endif
179063 };
179064 static const struct {
179065 const char *zName;
179066 int nArg;
179067 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
179068 void (*xFinal)(sqlite3_context*);
179069 void (*xValue)(sqlite3_context*);
179070 } aAgg[] = {
179071 { "json_group_array", 1,
179072 jsonArrayStep, jsonArrayFinal, jsonArrayValue },
179073 { "json_group_object", 2,
179074 jsonObjectStep, jsonObjectFinal, jsonObjectValue },
179075 };
179076#ifndef SQLITE_OMIT_VIRTUALTABLE
179077 static const struct {
179078 const char *zName;
179079 sqlite3_module *pModule;
179080 } aMod[] = {
179081 { "json_each", &jsonEachModule },
179082 { "json_tree", &jsonTreeModule },
179083 };
179084#endif
179085 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
179086 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
179087 SQLITE_UTF8 | SQLITE_DETERMINISTIC,
179088 (void*)&aFunc[i].flag,
179089 aFunc[i].xFunc, 0, 0);
179090 }
179091#ifndef SQLITE_OMIT_WINDOWFUNC
179092 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
179093 rc = sqlite3_create_window_function(db, aAgg[i].zName, aAgg[i].nArg,
179094 SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0,
179095 aAgg[i].xStep, aAgg[i].xFinal,
179096 aAgg[i].xValue, jsonGroupInverse, 0);
179097 }
179098#endif
179099#ifndef SQLITE_OMIT_VIRTUALTABLE
179100 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
179101 rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
179102 }
179103#endif
179104 return rc;
179105}
179106
179107
179108#ifndef SQLITE_CORE
179109#ifdef _WIN32
179110__declspec(dllexport)
179111#endif
179112SQLITE_API int sqlite3_json_init(
179113 sqlite3 *db,
179114 char **pzErrMsg,
179115 const sqlite3_api_routines *pApi
179116){
179117 SQLITE_EXTENSION_INIT2(pApi);
179118 (void)pzErrMsg; /* Unused parameter */
179119 return sqlite3Json1Init(db);
179120}
179121#endif
179122#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) */
179123
179124/************** End of json1.c ***********************************************/
179125/************** Begin file rtree.c *******************************************/
179126/*
179127** 2001 September 15
179128**
179129** The author disclaims copyright to this source code. In place of
179130** a legal notice, here is a blessing:
179131**
179132** May you do good and not evil.
179133** May you find forgiveness for yourself and forgive others.
179134** May you share freely, never taking more than you give.
179135**
179136*************************************************************************
179137** This file contains code for implementations of the r-tree and r*-tree
179138** algorithms packaged as an SQLite virtual table module.
179139*/
179140
179141/*
179142** Database Format of R-Tree Tables
179143** --------------------------------
179144**
179145** The data structure for a single virtual r-tree table is stored in three
179146** native SQLite tables declared as follows. In each case, the '%' character
179147** in the table name is replaced with the user-supplied name of the r-tree
179148** table.
179149**
179150** CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB)
179151** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
179152** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
179153**
179154** The data for each node of the r-tree structure is stored in the %_node
179155** table. For each node that is not the root node of the r-tree, there is
179156** an entry in the %_parent table associating the node with its parent.
179157** And for each row of data in the table, there is an entry in the %_rowid
179158** table that maps from the entries rowid to the id of the node that it
179159** is stored on. If the r-tree contains auxiliary columns, those are stored
179160** on the end of the %_rowid table.
179161**
179162** The root node of an r-tree always exists, even if the r-tree table is
179163** empty. The nodeno of the root node is always 1. All other nodes in the
179164** table must be the same size as the root node. The content of each node
179165** is formatted as follows:
179166**
179167** 1. If the node is the root node (node 1), then the first 2 bytes
179168** of the node contain the tree depth as a big-endian integer.
179169** For non-root nodes, the first 2 bytes are left unused.
179170**
179171** 2. The next 2 bytes contain the number of entries currently
179172** stored in the node.
179173**
179174** 3. The remainder of the node contains the node entries. Each entry
179175** consists of a single 8-byte integer followed by an even number
179176** of 4-byte coordinates. For leaf nodes the integer is the rowid
179177** of a record. For internal nodes it is the node number of a
179178** child page.
179179*/
179180
179181#if !defined(SQLITE_CORE) \
179182 || (defined(SQLITE_ENABLE_RTREE) && !defined(SQLITE_OMIT_VIRTUALTABLE))
179183
179184#ifndef SQLITE_CORE
179185/* #include "sqlite3ext.h" */
179186 SQLITE_EXTENSION_INIT1
179187#else
179188/* #include "sqlite3.h" */
179189#endif
179190
179191/* #include <string.h> */
179192/* #include <assert.h> */
179193/* #include <stdio.h> */
179194
179195#ifndef SQLITE_AMALGAMATION
179196#include "sqlite3rtree.h"
179197typedef sqlite3_int64 i64;
179198typedef sqlite3_uint64 u64;
179199typedef unsigned char u8;
179200typedef unsigned short u16;
179201typedef unsigned int u32;
179202#endif
179203
179204/* The following macro is used to suppress compiler warnings.
179205*/
179206#ifndef UNUSED_PARAMETER
179207# define UNUSED_PARAMETER(x) (void)(x)
179208#endif
179209
179210typedef struct Rtree Rtree;
179211typedef struct RtreeCursor RtreeCursor;
179212typedef struct RtreeNode RtreeNode;
179213typedef struct RtreeCell RtreeCell;
179214typedef struct RtreeConstraint RtreeConstraint;
179215typedef struct RtreeMatchArg RtreeMatchArg;
179216typedef struct RtreeGeomCallback RtreeGeomCallback;
179217typedef union RtreeCoord RtreeCoord;
179218typedef struct RtreeSearchPoint RtreeSearchPoint;
179219
179220/* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
179221#define RTREE_MAX_DIMENSIONS 5
179222
179223/* Maximum number of auxiliary columns */
179224#define RTREE_MAX_AUX_COLUMN 100
179225
179226/* Size of hash table Rtree.aHash. This hash table is not expected to
179227** ever contain very many entries, so a fixed number of buckets is
179228** used.
179229*/
179230#define HASHSIZE 97
179231
179232/* The xBestIndex method of this virtual table requires an estimate of
179233** the number of rows in the virtual table to calculate the costs of
179234** various strategies. If possible, this estimate is loaded from the
179235** sqlite_stat1 table (with RTREE_MIN_ROWEST as a hard-coded minimum).
179236** Otherwise, if no sqlite_stat1 entry is available, use
179237** RTREE_DEFAULT_ROWEST.
179238*/
179239#define RTREE_DEFAULT_ROWEST 1048576
179240#define RTREE_MIN_ROWEST 100
179241
179242/*
179243** An rtree virtual-table object.
179244*/
179245struct Rtree {
179246 sqlite3_vtab base; /* Base class. Must be first */
179247 sqlite3 *db; /* Host database connection */
179248 int iNodeSize; /* Size in bytes of each node in the node table */
179249 u8 nDim; /* Number of dimensions */
179250 u8 nDim2; /* Twice the number of dimensions */
179251 u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
179252 u8 nBytesPerCell; /* Bytes consumed per cell */
179253 u8 inWrTrans; /* True if inside write transaction */
179254 u8 nAux; /* # of auxiliary columns in %_rowid */
179255 u8 nAuxNotNull; /* Number of initial not-null aux columns */
179256 int iDepth; /* Current depth of the r-tree structure */
179257 char *zDb; /* Name of database containing r-tree table */
179258 char *zName; /* Name of r-tree table */
179259 u32 nBusy; /* Current number of users of this structure */
179260 i64 nRowEst; /* Estimated number of rows in this table */
179261 u32 nCursor; /* Number of open cursors */
179262 u32 nNodeRef; /* Number RtreeNodes with positive nRef */
179263 char *zReadAuxSql; /* SQL for statement to read aux data */
179264
179265 /* List of nodes removed during a CondenseTree operation. List is
179266 ** linked together via the pointer normally used for hash chains -
179267 ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
179268 ** headed by the node (leaf nodes have RtreeNode.iNode==0).
179269 */
179270 RtreeNode *pDeleted;
179271 int iReinsertHeight; /* Height of sub-trees Reinsert() has run on */
179272
179273 /* Blob I/O on xxx_node */
179274 sqlite3_blob *pNodeBlob;
179275
179276 /* Statements to read/write/delete a record from xxx_node */
179277 sqlite3_stmt *pWriteNode;
179278 sqlite3_stmt *pDeleteNode;
179279
179280 /* Statements to read/write/delete a record from xxx_rowid */
179281 sqlite3_stmt *pReadRowid;
179282 sqlite3_stmt *pWriteRowid;
179283 sqlite3_stmt *pDeleteRowid;
179284
179285 /* Statements to read/write/delete a record from xxx_parent */
179286 sqlite3_stmt *pReadParent;
179287 sqlite3_stmt *pWriteParent;
179288 sqlite3_stmt *pDeleteParent;
179289
179290 /* Statement for writing to the "aux:" fields, if there are any */
179291 sqlite3_stmt *pWriteAux;
179292
179293 RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */
179294};
179295
179296/* Possible values for Rtree.eCoordType: */
179297#define RTREE_COORD_REAL32 0
179298#define RTREE_COORD_INT32 1
179299
179300/*
179301** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will
179302** only deal with integer coordinates. No floating point operations
179303** will be done.
179304*/
179305#ifdef SQLITE_RTREE_INT_ONLY
179306 typedef sqlite3_int64 RtreeDValue; /* High accuracy coordinate */
179307 typedef int RtreeValue; /* Low accuracy coordinate */
179308# define RTREE_ZERO 0
179309#else
179310 typedef double RtreeDValue; /* High accuracy coordinate */
179311 typedef float RtreeValue; /* Low accuracy coordinate */
179312# define RTREE_ZERO 0.0
179313#endif
179314
179315/*
179316** When doing a search of an r-tree, instances of the following structure
179317** record intermediate results from the tree walk.
179318**
179319** The id is always a node-id. For iLevel>=1 the id is the node-id of
179320** the node that the RtreeSearchPoint represents. When iLevel==0, however,
179321** the id is of the parent node and the cell that RtreeSearchPoint
179322** represents is the iCell-th entry in the parent node.
179323*/
179324struct RtreeSearchPoint {
179325 RtreeDValue rScore; /* The score for this node. Smallest goes first. */
179326 sqlite3_int64 id; /* Node ID */
179327 u8 iLevel; /* 0=entries. 1=leaf node. 2+ for higher */
179328 u8 eWithin; /* PARTLY_WITHIN or FULLY_WITHIN */
179329 u8 iCell; /* Cell index within the node */
179330};
179331
179332/*
179333** The minimum number of cells allowed for a node is a third of the
179334** maximum. In Gutman's notation:
179335**
179336** m = M/3
179337**
179338** If an R*-tree "Reinsert" operation is required, the same number of
179339** cells are removed from the overfull node and reinserted into the tree.
179340*/
179341#define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
179342#define RTREE_REINSERT(p) RTREE_MINCELLS(p)
179343#define RTREE_MAXCELLS 51
179344
179345/*
179346** The smallest possible node-size is (512-64)==448 bytes. And the largest
179347** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates).
179348** Therefore all non-root nodes must contain at least 3 entries. Since
179349** 3^40 is greater than 2^64, an r-tree structure always has a depth of
179350** 40 or less.
179351*/
179352#define RTREE_MAX_DEPTH 40
179353
179354
179355/*
179356** Number of entries in the cursor RtreeNode cache. The first entry is
179357** used to cache the RtreeNode for RtreeCursor.sPoint. The remaining
179358** entries cache the RtreeNode for the first elements of the priority queue.
179359*/
179360#define RTREE_CACHE_SZ 5
179361
179362/*
179363** An rtree cursor object.
179364*/
179365struct RtreeCursor {
179366 sqlite3_vtab_cursor base; /* Base class. Must be first */
179367 u8 atEOF; /* True if at end of search */
179368 u8 bPoint; /* True if sPoint is valid */
179369 u8 bAuxValid; /* True if pReadAux is valid */
179370 int iStrategy; /* Copy of idxNum search parameter */
179371 int nConstraint; /* Number of entries in aConstraint */
179372 RtreeConstraint *aConstraint; /* Search constraints. */
179373 int nPointAlloc; /* Number of slots allocated for aPoint[] */
179374 int nPoint; /* Number of slots used in aPoint[] */
179375 int mxLevel; /* iLevel value for root of the tree */
179376 RtreeSearchPoint *aPoint; /* Priority queue for search points */
179377 sqlite3_stmt *pReadAux; /* Statement to read aux-data */
179378 RtreeSearchPoint sPoint; /* Cached next search point */
179379 RtreeNode *aNode[RTREE_CACHE_SZ]; /* Rtree node cache */
179380 u32 anQueue[RTREE_MAX_DEPTH+1]; /* Number of queued entries by iLevel */
179381};
179382
179383/* Return the Rtree of a RtreeCursor */
179384#define RTREE_OF_CURSOR(X) ((Rtree*)((X)->base.pVtab))
179385
179386/*
179387** A coordinate can be either a floating point number or a integer. All
179388** coordinates within a single R-Tree are always of the same time.
179389*/
179390union RtreeCoord {
179391 RtreeValue f; /* Floating point value */
179392 int i; /* Integer value */
179393 u32 u; /* Unsigned for byte-order conversions */
179394};
179395
179396/*
179397** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
179398** formatted as a RtreeDValue (double or int64). This macro assumes that local
179399** variable pRtree points to the Rtree structure associated with the
179400** RtreeCoord.
179401*/
179402#ifdef SQLITE_RTREE_INT_ONLY
179403# define DCOORD(coord) ((RtreeDValue)coord.i)
179404#else
179405# define DCOORD(coord) ( \
179406 (pRtree->eCoordType==RTREE_COORD_REAL32) ? \
179407 ((double)coord.f) : \
179408 ((double)coord.i) \
179409 )
179410#endif
179411
179412/*
179413** A search constraint.
179414*/
179415struct RtreeConstraint {
179416 int iCoord; /* Index of constrained coordinate */
179417 int op; /* Constraining operation */
179418 union {
179419 RtreeDValue rValue; /* Constraint value. */
179420 int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*);
179421 int (*xQueryFunc)(sqlite3_rtree_query_info*);
179422 } u;
179423 sqlite3_rtree_query_info *pInfo; /* xGeom and xQueryFunc argument */
179424};
179425
179426/* Possible values for RtreeConstraint.op */
179427#define RTREE_EQ 0x41 /* A */
179428#define RTREE_LE 0x42 /* B */
179429#define RTREE_LT 0x43 /* C */
179430#define RTREE_GE 0x44 /* D */
179431#define RTREE_GT 0x45 /* E */
179432#define RTREE_MATCH 0x46 /* F: Old-style sqlite3_rtree_geometry_callback() */
179433#define RTREE_QUERY 0x47 /* G: New-style sqlite3_rtree_query_callback() */
179434
179435
179436/*
179437** An rtree structure node.
179438*/
179439struct RtreeNode {
179440 RtreeNode *pParent; /* Parent node */
179441 i64 iNode; /* The node number */
179442 int nRef; /* Number of references to this node */
179443 int isDirty; /* True if the node needs to be written to disk */
179444 u8 *zData; /* Content of the node, as should be on disk */
179445 RtreeNode *pNext; /* Next node in this hash collision chain */
179446};
179447
179448/* Return the number of cells in a node */
179449#define NCELL(pNode) readInt16(&(pNode)->zData[2])
179450
179451/*
179452** A single cell from a node, deserialized
179453*/
179454struct RtreeCell {
179455 i64 iRowid; /* Node or entry ID */
179456 RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2]; /* Bounding box coordinates */
179457};
179458
179459
179460/*
179461** This object becomes the sqlite3_user_data() for the SQL functions
179462** that are created by sqlite3_rtree_geometry_callback() and
179463** sqlite3_rtree_query_callback() and which appear on the right of MATCH
179464** operators in order to constrain a search.
179465**
179466** xGeom and xQueryFunc are the callback functions. Exactly one of
179467** xGeom and xQueryFunc fields is non-NULL, depending on whether the
179468** SQL function was created using sqlite3_rtree_geometry_callback() or
179469** sqlite3_rtree_query_callback().
179470**
179471** This object is deleted automatically by the destructor mechanism in
179472** sqlite3_create_function_v2().
179473*/
179474struct RtreeGeomCallback {
179475 int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
179476 int (*xQueryFunc)(sqlite3_rtree_query_info*);
179477 void (*xDestructor)(void*);
179478 void *pContext;
179479};
179480
179481/*
179482** An instance of this structure (in the form of a BLOB) is returned by
179483** the SQL functions that sqlite3_rtree_geometry_callback() and
179484** sqlite3_rtree_query_callback() create, and is read as the right-hand
179485** operand to the MATCH operator of an R-Tree.
179486*/
179487struct RtreeMatchArg {
179488 u32 iSize; /* Size of this object */
179489 RtreeGeomCallback cb; /* Info about the callback functions */
179490 int nParam; /* Number of parameters to the SQL function */
179491 sqlite3_value **apSqlParam; /* Original SQL parameter values */
179492 RtreeDValue aParam[1]; /* Values for parameters to the SQL function */
179493};
179494
179495#ifndef MAX
179496# define MAX(x,y) ((x) < (y) ? (y) : (x))
179497#endif
179498#ifndef MIN
179499# define MIN(x,y) ((x) > (y) ? (y) : (x))
179500#endif
179501
179502/* What version of GCC is being used. 0 means GCC is not being used .
179503** Note that the GCC_VERSION macro will also be set correctly when using
179504** clang, since clang works hard to be gcc compatible. So the gcc
179505** optimizations will also work when compiling with clang.
179506*/
179507#ifndef GCC_VERSION
179508#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
179509# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
179510#else
179511# define GCC_VERSION 0
179512#endif
179513#endif
179514
179515/* The testcase() macro should already be defined in the amalgamation. If
179516** it is not, make it a no-op.
179517*/
179518#ifndef SQLITE_AMALGAMATION
179519# define testcase(X)
179520#endif
179521
179522/*
179523** Macros to determine whether the machine is big or little endian,
179524** and whether or not that determination is run-time or compile-time.
179525**
179526** For best performance, an attempt is made to guess at the byte-order
179527** using C-preprocessor macros. If that is unsuccessful, or if
179528** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
179529** at run-time.
179530*/
179531#ifndef SQLITE_BYTEORDER
179532#if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
179533 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
179534 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
179535 defined(__arm__)
179536# define SQLITE_BYTEORDER 1234
179537#elif defined(sparc) || defined(__ppc__)
179538# define SQLITE_BYTEORDER 4321
179539#else
179540# define SQLITE_BYTEORDER 0 /* 0 means "unknown at compile-time" */
179541#endif
179542#endif
179543
179544
179545/* What version of MSVC is being used. 0 means MSVC is not being used */
179546#ifndef MSVC_VERSION
179547#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
179548# define MSVC_VERSION _MSC_VER
179549#else
179550# define MSVC_VERSION 0
179551#endif
179552#endif
179553
179554/*
179555** Functions to deserialize a 16 bit integer, 32 bit real number and
179556** 64 bit integer. The deserialized value is returned.
179557*/
179558static int readInt16(u8 *p){
179559 return (p[0]<<8) + p[1];
179560}
179561static void readCoord(u8 *p, RtreeCoord *pCoord){
179562 assert( ((((char*)p) - (char*)0)&3)==0 ); /* p is always 4-byte aligned */
179563#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
179564 pCoord->u = _byteswap_ulong(*(u32*)p);
179565#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
179566 pCoord->u = __builtin_bswap32(*(u32*)p);
179567#elif SQLITE_BYTEORDER==4321
179568 pCoord->u = *(u32*)p;
179569#else
179570 pCoord->u = (
179571 (((u32)p[0]) << 24) +
179572 (((u32)p[1]) << 16) +
179573 (((u32)p[2]) << 8) +
179574 (((u32)p[3]) << 0)
179575 );
179576#endif
179577}
179578static i64 readInt64(u8 *p){
179579#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
179580 u64 x;
179581 memcpy(&x, p, 8);
179582 return (i64)_byteswap_uint64(x);
179583#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
179584 u64 x;
179585 memcpy(&x, p, 8);
179586 return (i64)__builtin_bswap64(x);
179587#elif SQLITE_BYTEORDER==4321
179588 i64 x;
179589 memcpy(&x, p, 8);
179590 return x;
179591#else
179592 return (i64)(
179593 (((u64)p[0]) << 56) +
179594 (((u64)p[1]) << 48) +
179595 (((u64)p[2]) << 40) +
179596 (((u64)p[3]) << 32) +
179597 (((u64)p[4]) << 24) +
179598 (((u64)p[5]) << 16) +
179599 (((u64)p[6]) << 8) +
179600 (((u64)p[7]) << 0)
179601 );
179602#endif
179603}
179604
179605/*
179606** Functions to serialize a 16 bit integer, 32 bit real number and
179607** 64 bit integer. The value returned is the number of bytes written
179608** to the argument buffer (always 2, 4 and 8 respectively).
179609*/
179610static void writeInt16(u8 *p, int i){
179611 p[0] = (i>> 8)&0xFF;
179612 p[1] = (i>> 0)&0xFF;
179613}
179614static int writeCoord(u8 *p, RtreeCoord *pCoord){
179615 u32 i;
179616 assert( ((((char*)p) - (char*)0)&3)==0 ); /* p is always 4-byte aligned */
179617 assert( sizeof(RtreeCoord)==4 );
179618 assert( sizeof(u32)==4 );
179619#if SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
179620 i = __builtin_bswap32(pCoord->u);
179621 memcpy(p, &i, 4);
179622#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
179623 i = _byteswap_ulong(pCoord->u);
179624 memcpy(p, &i, 4);
179625#elif SQLITE_BYTEORDER==4321
179626 i = pCoord->u;
179627 memcpy(p, &i, 4);
179628#else
179629 i = pCoord->u;
179630 p[0] = (i>>24)&0xFF;
179631 p[1] = (i>>16)&0xFF;
179632 p[2] = (i>> 8)&0xFF;
179633 p[3] = (i>> 0)&0xFF;
179634#endif
179635 return 4;
179636}
179637static int writeInt64(u8 *p, i64 i){
179638#if SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
179639 i = (i64)__builtin_bswap64((u64)i);
179640 memcpy(p, &i, 8);
179641#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
179642 i = (i64)_byteswap_uint64((u64)i);
179643 memcpy(p, &i, 8);
179644#elif SQLITE_BYTEORDER==4321
179645 memcpy(p, &i, 8);
179646#else
179647 p[0] = (i>>56)&0xFF;
179648 p[1] = (i>>48)&0xFF;
179649 p[2] = (i>>40)&0xFF;
179650 p[3] = (i>>32)&0xFF;
179651 p[4] = (i>>24)&0xFF;
179652 p[5] = (i>>16)&0xFF;
179653 p[6] = (i>> 8)&0xFF;
179654 p[7] = (i>> 0)&0xFF;
179655#endif
179656 return 8;
179657}
179658
179659/*
179660** Increment the reference count of node p.
179661*/
179662static void nodeReference(RtreeNode *p){
179663 if( p ){
179664 assert( p->nRef>0 );
179665 p->nRef++;
179666 }
179667}
179668
179669/*
179670** Clear the content of node p (set all bytes to 0x00).
179671*/
179672static void nodeZero(Rtree *pRtree, RtreeNode *p){
179673 memset(&p->zData[2], 0, pRtree->iNodeSize-2);
179674 p->isDirty = 1;
179675}
179676
179677/*
179678** Given a node number iNode, return the corresponding key to use
179679** in the Rtree.aHash table.
179680*/
179681static int nodeHash(i64 iNode){
179682 return iNode % HASHSIZE;
179683}
179684
179685/*
179686** Search the node hash table for node iNode. If found, return a pointer
179687** to it. Otherwise, return 0.
179688*/
179689static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){
179690 RtreeNode *p;
179691 for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext);
179692 return p;
179693}
179694
179695/*
179696** Add node pNode to the node hash table.
179697*/
179698static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){
179699 int iHash;
179700 assert( pNode->pNext==0 );
179701 iHash = nodeHash(pNode->iNode);
179702 pNode->pNext = pRtree->aHash[iHash];
179703 pRtree->aHash[iHash] = pNode;
179704}
179705
179706/*
179707** Remove node pNode from the node hash table.
179708*/
179709static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){
179710 RtreeNode **pp;
179711 if( pNode->iNode!=0 ){
179712 pp = &pRtree->aHash[nodeHash(pNode->iNode)];
179713 for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp); }
179714 *pp = pNode->pNext;
179715 pNode->pNext = 0;
179716 }
179717}
179718
179719/*
179720** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0),
179721** indicating that node has not yet been assigned a node number. It is
179722** assigned a node number when nodeWrite() is called to write the
179723** node contents out to the database.
179724*/
179725static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){
179726 RtreeNode *pNode;
179727 pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize);
179728 if( pNode ){
179729 memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize);
179730 pNode->zData = (u8 *)&pNode[1];
179731 pNode->nRef = 1;
179732 pRtree->nNodeRef++;
179733 pNode->pParent = pParent;
179734 pNode->isDirty = 1;
179735 nodeReference(pParent);
179736 }
179737 return pNode;
179738}
179739
179740/*
179741** Clear the Rtree.pNodeBlob object
179742*/
179743static void nodeBlobReset(Rtree *pRtree){
179744 if( pRtree->pNodeBlob && pRtree->inWrTrans==0 && pRtree->nCursor==0 ){
179745 sqlite3_blob *pBlob = pRtree->pNodeBlob;
179746 pRtree->pNodeBlob = 0;
179747 sqlite3_blob_close(pBlob);
179748 }
179749}
179750
179751/*
179752** Obtain a reference to an r-tree node.
179753*/
179754static int nodeAcquire(
179755 Rtree *pRtree, /* R-tree structure */
179756 i64 iNode, /* Node number to load */
179757 RtreeNode *pParent, /* Either the parent node or NULL */
179758 RtreeNode **ppNode /* OUT: Acquired node */
179759){
179760 int rc = SQLITE_OK;
179761 RtreeNode *pNode = 0;
179762
179763 /* Check if the requested node is already in the hash table. If so,
179764 ** increase its reference count and return it.
179765 */
179766 if( (pNode = nodeHashLookup(pRtree, iNode))!=0 ){
179767 assert( !pParent || !pNode->pParent || pNode->pParent==pParent );
179768 if( pParent && !pNode->pParent ){
179769 pParent->nRef++;
179770 pNode->pParent = pParent;
179771 }
179772 pNode->nRef++;
179773 *ppNode = pNode;
179774 return SQLITE_OK;
179775 }
179776
179777 if( pRtree->pNodeBlob ){
179778 sqlite3_blob *pBlob = pRtree->pNodeBlob;
179779 pRtree->pNodeBlob = 0;
179780 rc = sqlite3_blob_reopen(pBlob, iNode);
179781 pRtree->pNodeBlob = pBlob;
179782 if( rc ){
179783 nodeBlobReset(pRtree);
179784 if( rc==SQLITE_NOMEM ) return SQLITE_NOMEM;
179785 }
179786 }
179787 if( pRtree->pNodeBlob==0 ){
179788 char *zTab = sqlite3_mprintf("%s_node", pRtree->zName);
179789 if( zTab==0 ) return SQLITE_NOMEM;
179790 rc = sqlite3_blob_open(pRtree->db, pRtree->zDb, zTab, "data", iNode, 0,
179791 &pRtree->pNodeBlob);
179792 sqlite3_free(zTab);
179793 }
179794 if( rc ){
179795 nodeBlobReset(pRtree);
179796 *ppNode = 0;
179797 /* If unable to open an sqlite3_blob on the desired row, that can only
179798 ** be because the shadow tables hold erroneous data. */
179799 if( rc==SQLITE_ERROR ) rc = SQLITE_CORRUPT_VTAB;
179800 }else if( pRtree->iNodeSize==sqlite3_blob_bytes(pRtree->pNodeBlob) ){
179801 pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
179802 if( !pNode ){
179803 rc = SQLITE_NOMEM;
179804 }else{
179805 pNode->pParent = pParent;
179806 pNode->zData = (u8 *)&pNode[1];
179807 pNode->nRef = 1;
179808 pRtree->nNodeRef++;
179809 pNode->iNode = iNode;
179810 pNode->isDirty = 0;
179811 pNode->pNext = 0;
179812 rc = sqlite3_blob_read(pRtree->pNodeBlob, pNode->zData,
179813 pRtree->iNodeSize, 0);
179814 nodeReference(pParent);
179815 }
179816 }
179817
179818 /* If the root node was just loaded, set pRtree->iDepth to the height
179819 ** of the r-tree structure. A height of zero means all data is stored on
179820 ** the root node. A height of one means the children of the root node
179821 ** are the leaves, and so on. If the depth as specified on the root node
179822 ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
179823 */
179824 if( pNode && iNode==1 ){
179825 pRtree->iDepth = readInt16(pNode->zData);
179826 if( pRtree->iDepth>RTREE_MAX_DEPTH ){
179827 rc = SQLITE_CORRUPT_VTAB;
179828 }
179829 }
179830
179831 /* If no error has occurred so far, check if the "number of entries"
179832 ** field on the node is too large. If so, set the return code to
179833 ** SQLITE_CORRUPT_VTAB.
179834 */
179835 if( pNode && rc==SQLITE_OK ){
179836 if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){
179837 rc = SQLITE_CORRUPT_VTAB;
179838 }
179839 }
179840
179841 if( rc==SQLITE_OK ){
179842 if( pNode!=0 ){
179843 nodeHashInsert(pRtree, pNode);
179844 }else{
179845 rc = SQLITE_CORRUPT_VTAB;
179846 }
179847 *ppNode = pNode;
179848 }else{
179849 if( pNode ){
179850 pRtree->nNodeRef--;
179851 sqlite3_free(pNode);
179852 }
179853 *ppNode = 0;
179854 }
179855
179856 return rc;
179857}
179858
179859/*
179860** Overwrite cell iCell of node pNode with the contents of pCell.
179861*/
179862static void nodeOverwriteCell(
179863 Rtree *pRtree, /* The overall R-Tree */
179864 RtreeNode *pNode, /* The node into which the cell is to be written */
179865 RtreeCell *pCell, /* The cell to write */
179866 int iCell /* Index into pNode into which pCell is written */
179867){
179868 int ii;
179869 u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
179870 p += writeInt64(p, pCell->iRowid);
179871 for(ii=0; ii<pRtree->nDim2; ii++){
179872 p += writeCoord(p, &pCell->aCoord[ii]);
179873 }
179874 pNode->isDirty = 1;
179875}
179876
179877/*
179878** Remove the cell with index iCell from node pNode.
179879*/
179880static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
179881 u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
179882 u8 *pSrc = &pDst[pRtree->nBytesPerCell];
179883 int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell;
179884 memmove(pDst, pSrc, nByte);
179885 writeInt16(&pNode->zData[2], NCELL(pNode)-1);
179886 pNode->isDirty = 1;
179887}
179888
179889/*
179890** Insert the contents of cell pCell into node pNode. If the insert
179891** is successful, return SQLITE_OK.
179892**
179893** If there is not enough free space in pNode, return SQLITE_FULL.
179894*/
179895static int nodeInsertCell(
179896 Rtree *pRtree, /* The overall R-Tree */
179897 RtreeNode *pNode, /* Write new cell into this node */
179898 RtreeCell *pCell /* The cell to be inserted */
179899){
179900 int nCell; /* Current number of cells in pNode */
179901 int nMaxCell; /* Maximum number of cells for pNode */
179902
179903 nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
179904 nCell = NCELL(pNode);
179905
179906 assert( nCell<=nMaxCell );
179907 if( nCell<nMaxCell ){
179908 nodeOverwriteCell(pRtree, pNode, pCell, nCell);
179909 writeInt16(&pNode->zData[2], nCell+1);
179910 pNode->isDirty = 1;
179911 }
179912
179913 return (nCell==nMaxCell);
179914}
179915
179916/*
179917** If the node is dirty, write it out to the database.
179918*/
179919static int nodeWrite(Rtree *pRtree, RtreeNode *pNode){
179920 int rc = SQLITE_OK;
179921 if( pNode->isDirty ){
179922 sqlite3_stmt *p = pRtree->pWriteNode;
179923 if( pNode->iNode ){
179924 sqlite3_bind_int64(p, 1, pNode->iNode);
179925 }else{
179926 sqlite3_bind_null(p, 1);
179927 }
179928 sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC);
179929 sqlite3_step(p);
179930 pNode->isDirty = 0;
179931 rc = sqlite3_reset(p);
179932 sqlite3_bind_null(p, 2);
179933 if( pNode->iNode==0 && rc==SQLITE_OK ){
179934 pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
179935 nodeHashInsert(pRtree, pNode);
179936 }
179937 }
179938 return rc;
179939}
179940
179941/*
179942** Release a reference to a node. If the node is dirty and the reference
179943** count drops to zero, the node data is written to the database.
179944*/
179945static int nodeRelease(Rtree *pRtree, RtreeNode *pNode){
179946 int rc = SQLITE_OK;
179947 if( pNode ){
179948 assert( pNode->nRef>0 );
179949 assert( pRtree->nNodeRef>0 );
179950 pNode->nRef--;
179951 if( pNode->nRef==0 ){
179952 pRtree->nNodeRef--;
179953 if( pNode->iNode==1 ){
179954 pRtree->iDepth = -1;
179955 }
179956 if( pNode->pParent ){
179957 rc = nodeRelease(pRtree, pNode->pParent);
179958 }
179959 if( rc==SQLITE_OK ){
179960 rc = nodeWrite(pRtree, pNode);
179961 }
179962 nodeHashDelete(pRtree, pNode);
179963 sqlite3_free(pNode);
179964 }
179965 }
179966 return rc;
179967}
179968
179969/*
179970** Return the 64-bit integer value associated with cell iCell of
179971** node pNode. If pNode is a leaf node, this is a rowid. If it is
179972** an internal node, then the 64-bit integer is a child page number.
179973*/
179974static i64 nodeGetRowid(
179975 Rtree *pRtree, /* The overall R-Tree */
179976 RtreeNode *pNode, /* The node from which to extract the ID */
179977 int iCell /* The cell index from which to extract the ID */
179978){
179979 assert( iCell<NCELL(pNode) );
179980 return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
179981}
179982
179983/*
179984** Return coordinate iCoord from cell iCell in node pNode.
179985*/
179986static void nodeGetCoord(
179987 Rtree *pRtree, /* The overall R-Tree */
179988 RtreeNode *pNode, /* The node from which to extract a coordinate */
179989 int iCell, /* The index of the cell within the node */
179990 int iCoord, /* Which coordinate to extract */
179991 RtreeCoord *pCoord /* OUT: Space to write result to */
179992){
179993 readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
179994}
179995
179996/*
179997** Deserialize cell iCell of node pNode. Populate the structure pointed
179998** to by pCell with the results.
179999*/
180000static void nodeGetCell(
180001 Rtree *pRtree, /* The overall R-Tree */
180002 RtreeNode *pNode, /* The node containing the cell to be read */
180003 int iCell, /* Index of the cell within the node */
180004 RtreeCell *pCell /* OUT: Write the cell contents here */
180005){
180006 u8 *pData;
180007 RtreeCoord *pCoord;
180008 int ii = 0;
180009 pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
180010 pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell);
180011 pCoord = pCell->aCoord;
180012 do{
180013 readCoord(pData, &pCoord[ii]);
180014 readCoord(pData+4, &pCoord[ii+1]);
180015 pData += 8;
180016 ii += 2;
180017 }while( ii<pRtree->nDim2 );
180018}
180019
180020
180021/* Forward declaration for the function that does the work of
180022** the virtual table module xCreate() and xConnect() methods.
180023*/
180024static int rtreeInit(
180025 sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **, int
180026);
180027
180028/*
180029** Rtree virtual table module xCreate method.
180030*/
180031static int rtreeCreate(
180032 sqlite3 *db,
180033 void *pAux,
180034 int argc, const char *const*argv,
180035 sqlite3_vtab **ppVtab,
180036 char **pzErr
180037){
180038 return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
180039}
180040
180041/*
180042** Rtree virtual table module xConnect method.
180043*/
180044static int rtreeConnect(
180045 sqlite3 *db,
180046 void *pAux,
180047 int argc, const char *const*argv,
180048 sqlite3_vtab **ppVtab,
180049 char **pzErr
180050){
180051 return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
180052}
180053
180054/*
180055** Increment the r-tree reference count.
180056*/
180057static void rtreeReference(Rtree *pRtree){
180058 pRtree->nBusy++;
180059}
180060
180061/*
180062** Decrement the r-tree reference count. When the reference count reaches
180063** zero the structure is deleted.
180064*/
180065static void rtreeRelease(Rtree *pRtree){
180066 pRtree->nBusy--;
180067 if( pRtree->nBusy==0 ){
180068 pRtree->inWrTrans = 0;
180069 assert( pRtree->nCursor==0 );
180070 nodeBlobReset(pRtree);
180071 assert( pRtree->nNodeRef==0 );
180072 sqlite3_finalize(pRtree->pWriteNode);
180073 sqlite3_finalize(pRtree->pDeleteNode);
180074 sqlite3_finalize(pRtree->pReadRowid);
180075 sqlite3_finalize(pRtree->pWriteRowid);
180076 sqlite3_finalize(pRtree->pDeleteRowid);
180077 sqlite3_finalize(pRtree->pReadParent);
180078 sqlite3_finalize(pRtree->pWriteParent);
180079 sqlite3_finalize(pRtree->pDeleteParent);
180080 sqlite3_finalize(pRtree->pWriteAux);
180081 sqlite3_free(pRtree->zReadAuxSql);
180082 sqlite3_free(pRtree);
180083 }
180084}
180085
180086/*
180087** Rtree virtual table module xDisconnect method.
180088*/
180089static int rtreeDisconnect(sqlite3_vtab *pVtab){
180090 rtreeRelease((Rtree *)pVtab);
180091 return SQLITE_OK;
180092}
180093
180094/*
180095** Rtree virtual table module xDestroy method.
180096*/
180097static int rtreeDestroy(sqlite3_vtab *pVtab){
180098 Rtree *pRtree = (Rtree *)pVtab;
180099 int rc;
180100 char *zCreate = sqlite3_mprintf(
180101 "DROP TABLE '%q'.'%q_node';"
180102 "DROP TABLE '%q'.'%q_rowid';"
180103 "DROP TABLE '%q'.'%q_parent';",
180104 pRtree->zDb, pRtree->zName,
180105 pRtree->zDb, pRtree->zName,
180106 pRtree->zDb, pRtree->zName
180107 );
180108 if( !zCreate ){
180109 rc = SQLITE_NOMEM;
180110 }else{
180111 nodeBlobReset(pRtree);
180112 rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
180113 sqlite3_free(zCreate);
180114 }
180115 if( rc==SQLITE_OK ){
180116 rtreeRelease(pRtree);
180117 }
180118
180119 return rc;
180120}
180121
180122/*
180123** Rtree virtual table module xOpen method.
180124*/
180125static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
180126 int rc = SQLITE_NOMEM;
180127 Rtree *pRtree = (Rtree *)pVTab;
180128 RtreeCursor *pCsr;
180129
180130 pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor));
180131 if( pCsr ){
180132 memset(pCsr, 0, sizeof(RtreeCursor));
180133 pCsr->base.pVtab = pVTab;
180134 rc = SQLITE_OK;
180135 pRtree->nCursor++;
180136 }
180137 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
180138
180139 return rc;
180140}
180141
180142
180143/*
180144** Free the RtreeCursor.aConstraint[] array and its contents.
180145*/
180146static void freeCursorConstraints(RtreeCursor *pCsr){
180147 if( pCsr->aConstraint ){
180148 int i; /* Used to iterate through constraint array */
180149 for(i=0; i<pCsr->nConstraint; i++){
180150 sqlite3_rtree_query_info *pInfo = pCsr->aConstraint[i].pInfo;
180151 if( pInfo ){
180152 if( pInfo->xDelUser ) pInfo->xDelUser(pInfo->pUser);
180153 sqlite3_free(pInfo);
180154 }
180155 }
180156 sqlite3_free(pCsr->aConstraint);
180157 pCsr->aConstraint = 0;
180158 }
180159}
180160
180161/*
180162** Rtree virtual table module xClose method.
180163*/
180164static int rtreeClose(sqlite3_vtab_cursor *cur){
180165 Rtree *pRtree = (Rtree *)(cur->pVtab);
180166 int ii;
180167 RtreeCursor *pCsr = (RtreeCursor *)cur;
180168 assert( pRtree->nCursor>0 );
180169 freeCursorConstraints(pCsr);
180170 sqlite3_finalize(pCsr->pReadAux);
180171 sqlite3_free(pCsr->aPoint);
180172 for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
180173 sqlite3_free(pCsr);
180174 pRtree->nCursor--;
180175 nodeBlobReset(pRtree);
180176 return SQLITE_OK;
180177}
180178
180179/*
180180** Rtree virtual table module xEof method.
180181**
180182** Return non-zero if the cursor does not currently point to a valid
180183** record (i.e if the scan has finished), or zero otherwise.
180184*/
180185static int rtreeEof(sqlite3_vtab_cursor *cur){
180186 RtreeCursor *pCsr = (RtreeCursor *)cur;
180187 return pCsr->atEOF;
180188}
180189
180190/*
180191** Convert raw bits from the on-disk RTree record into a coordinate value.
180192** The on-disk format is big-endian and needs to be converted for little-
180193** endian platforms. The on-disk record stores integer coordinates if
180194** eInt is true and it stores 32-bit floating point records if eInt is
180195** false. a[] is the four bytes of the on-disk record to be decoded.
180196** Store the results in "r".
180197**
180198** There are five versions of this macro. The last one is generic. The
180199** other four are various architectures-specific optimizations.
180200*/
180201#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
180202#define RTREE_DECODE_COORD(eInt, a, r) { \
180203 RtreeCoord c; /* Coordinate decoded */ \
180204 c.u = _byteswap_ulong(*(u32*)a); \
180205 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
180206}
180207#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
180208#define RTREE_DECODE_COORD(eInt, a, r) { \
180209 RtreeCoord c; /* Coordinate decoded */ \
180210 c.u = __builtin_bswap32(*(u32*)a); \
180211 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
180212}
180213#elif SQLITE_BYTEORDER==1234
180214#define RTREE_DECODE_COORD(eInt, a, r) { \
180215 RtreeCoord c; /* Coordinate decoded */ \
180216 memcpy(&c.u,a,4); \
180217 c.u = ((c.u>>24)&0xff)|((c.u>>8)&0xff00)| \
180218 ((c.u&0xff)<<24)|((c.u&0xff00)<<8); \
180219 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
180220}
180221#elif SQLITE_BYTEORDER==4321
180222#define RTREE_DECODE_COORD(eInt, a, r) { \
180223 RtreeCoord c; /* Coordinate decoded */ \
180224 memcpy(&c.u,a,4); \
180225 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
180226}
180227#else
180228#define RTREE_DECODE_COORD(eInt, a, r) { \
180229 RtreeCoord c; /* Coordinate decoded */ \
180230 c.u = ((u32)a[0]<<24) + ((u32)a[1]<<16) \
180231 +((u32)a[2]<<8) + a[3]; \
180232 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
180233}
180234#endif
180235
180236/*
180237** Check the RTree node or entry given by pCellData and p against the MATCH
180238** constraint pConstraint.
180239*/
180240static int rtreeCallbackConstraint(
180241 RtreeConstraint *pConstraint, /* The constraint to test */
180242 int eInt, /* True if RTree holding integer coordinates */
180243 u8 *pCellData, /* Raw cell content */
180244 RtreeSearchPoint *pSearch, /* Container of this cell */
180245 sqlite3_rtree_dbl *prScore, /* OUT: score for the cell */
180246 int *peWithin /* OUT: visibility of the cell */
180247){
180248 sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */
180249 int nCoord = pInfo->nCoord; /* No. of coordinates */
180250 int rc; /* Callback return code */
180251 RtreeCoord c; /* Translator union */
180252 sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS*2]; /* Decoded coordinates */
180253
180254 assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY );
180255 assert( nCoord==2 || nCoord==4 || nCoord==6 || nCoord==8 || nCoord==10 );
180256
180257 if( pConstraint->op==RTREE_QUERY && pSearch->iLevel==1 ){
180258 pInfo->iRowid = readInt64(pCellData);
180259 }
180260 pCellData += 8;
180261#ifndef SQLITE_RTREE_INT_ONLY
180262 if( eInt==0 ){
180263 switch( nCoord ){
180264 case 10: readCoord(pCellData+36, &c); aCoord[9] = c.f;
180265 readCoord(pCellData+32, &c); aCoord[8] = c.f;
180266 case 8: readCoord(pCellData+28, &c); aCoord[7] = c.f;
180267 readCoord(pCellData+24, &c); aCoord[6] = c.f;
180268 case 6: readCoord(pCellData+20, &c); aCoord[5] = c.f;
180269 readCoord(pCellData+16, &c); aCoord[4] = c.f;
180270 case 4: readCoord(pCellData+12, &c); aCoord[3] = c.f;
180271 readCoord(pCellData+8, &c); aCoord[2] = c.f;
180272 default: readCoord(pCellData+4, &c); aCoord[1] = c.f;
180273 readCoord(pCellData, &c); aCoord[0] = c.f;
180274 }
180275 }else
180276#endif
180277 {
180278 switch( nCoord ){
180279 case 10: readCoord(pCellData+36, &c); aCoord[9] = c.i;
180280 readCoord(pCellData+32, &c); aCoord[8] = c.i;
180281 case 8: readCoord(pCellData+28, &c); aCoord[7] = c.i;
180282 readCoord(pCellData+24, &c); aCoord[6] = c.i;
180283 case 6: readCoord(pCellData+20, &c); aCoord[5] = c.i;
180284 readCoord(pCellData+16, &c); aCoord[4] = c.i;
180285 case 4: readCoord(pCellData+12, &c); aCoord[3] = c.i;
180286 readCoord(pCellData+8, &c); aCoord[2] = c.i;
180287 default: readCoord(pCellData+4, &c); aCoord[1] = c.i;
180288 readCoord(pCellData, &c); aCoord[0] = c.i;
180289 }
180290 }
180291 if( pConstraint->op==RTREE_MATCH ){
180292 int eWithin = 0;
180293 rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo,
180294 nCoord, aCoord, &eWithin);
180295 if( eWithin==0 ) *peWithin = NOT_WITHIN;
180296 *prScore = RTREE_ZERO;
180297 }else{
180298 pInfo->aCoord = aCoord;
180299 pInfo->iLevel = pSearch->iLevel - 1;
180300 pInfo->rScore = pInfo->rParentScore = pSearch->rScore;
180301 pInfo->eWithin = pInfo->eParentWithin = pSearch->eWithin;
180302 rc = pConstraint->u.xQueryFunc(pInfo);
180303 if( pInfo->eWithin<*peWithin ) *peWithin = pInfo->eWithin;
180304 if( pInfo->rScore<*prScore || *prScore<RTREE_ZERO ){
180305 *prScore = pInfo->rScore;
180306 }
180307 }
180308 return rc;
180309}
180310
180311/*
180312** Check the internal RTree node given by pCellData against constraint p.
180313** If this constraint cannot be satisfied by any child within the node,
180314** set *peWithin to NOT_WITHIN.
180315*/
180316static void rtreeNonleafConstraint(
180317 RtreeConstraint *p, /* The constraint to test */
180318 int eInt, /* True if RTree holds integer coordinates */
180319 u8 *pCellData, /* Raw cell content as appears on disk */
180320 int *peWithin /* Adjust downward, as appropriate */
180321){
180322 sqlite3_rtree_dbl val; /* Coordinate value convert to a double */
180323
180324 /* p->iCoord might point to either a lower or upper bound coordinate
180325 ** in a coordinate pair. But make pCellData point to the lower bound.
180326 */
180327 pCellData += 8 + 4*(p->iCoord&0xfe);
180328
180329 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
180330 || p->op==RTREE_GT || p->op==RTREE_EQ );
180331 assert( ((((char*)pCellData) - (char*)0)&3)==0 ); /* 4-byte aligned */
180332 switch( p->op ){
180333 case RTREE_LE:
180334 case RTREE_LT:
180335 case RTREE_EQ:
180336 RTREE_DECODE_COORD(eInt, pCellData, val);
180337 /* val now holds the lower bound of the coordinate pair */
180338 if( p->u.rValue>=val ) return;
180339 if( p->op!=RTREE_EQ ) break; /* RTREE_LE and RTREE_LT end here */
180340 /* Fall through for the RTREE_EQ case */
180341
180342 default: /* RTREE_GT or RTREE_GE, or fallthrough of RTREE_EQ */
180343 pCellData += 4;
180344 RTREE_DECODE_COORD(eInt, pCellData, val);
180345 /* val now holds the upper bound of the coordinate pair */
180346 if( p->u.rValue<=val ) return;
180347 }
180348 *peWithin = NOT_WITHIN;
180349}
180350
180351/*
180352** Check the leaf RTree cell given by pCellData against constraint p.
180353** If this constraint is not satisfied, set *peWithin to NOT_WITHIN.
180354** If the constraint is satisfied, leave *peWithin unchanged.
180355**
180356** The constraint is of the form: xN op $val
180357**
180358** The op is given by p->op. The xN is p->iCoord-th coordinate in
180359** pCellData. $val is given by p->u.rValue.
180360*/
180361static void rtreeLeafConstraint(
180362 RtreeConstraint *p, /* The constraint to test */
180363 int eInt, /* True if RTree holds integer coordinates */
180364 u8 *pCellData, /* Raw cell content as appears on disk */
180365 int *peWithin /* Adjust downward, as appropriate */
180366){
180367 RtreeDValue xN; /* Coordinate value converted to a double */
180368
180369 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
180370 || p->op==RTREE_GT || p->op==RTREE_EQ );
180371 pCellData += 8 + p->iCoord*4;
180372 assert( ((((char*)pCellData) - (char*)0)&3)==0 ); /* 4-byte aligned */
180373 RTREE_DECODE_COORD(eInt, pCellData, xN);
180374 switch( p->op ){
180375 case RTREE_LE: if( xN <= p->u.rValue ) return; break;
180376 case RTREE_LT: if( xN < p->u.rValue ) return; break;
180377 case RTREE_GE: if( xN >= p->u.rValue ) return; break;
180378 case RTREE_GT: if( xN > p->u.rValue ) return; break;
180379 default: if( xN == p->u.rValue ) return; break;
180380 }
180381 *peWithin = NOT_WITHIN;
180382}
180383
180384/*
180385** One of the cells in node pNode is guaranteed to have a 64-bit
180386** integer value equal to iRowid. Return the index of this cell.
180387*/
180388static int nodeRowidIndex(
180389 Rtree *pRtree,
180390 RtreeNode *pNode,
180391 i64 iRowid,
180392 int *piIndex
180393){
180394 int ii;
180395 int nCell = NCELL(pNode);
180396 assert( nCell<200 );
180397 for(ii=0; ii<nCell; ii++){
180398 if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){
180399 *piIndex = ii;
180400 return SQLITE_OK;
180401 }
180402 }
180403 return SQLITE_CORRUPT_VTAB;
180404}
180405
180406/*
180407** Return the index of the cell containing a pointer to node pNode
180408** in its parent. If pNode is the root node, return -1.
180409*/
180410static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode, int *piIndex){
180411 RtreeNode *pParent = pNode->pParent;
180412 if( pParent ){
180413 return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex);
180414 }
180415 *piIndex = -1;
180416 return SQLITE_OK;
180417}
180418
180419/*
180420** Compare two search points. Return negative, zero, or positive if the first
180421** is less than, equal to, or greater than the second.
180422**
180423** The rScore is the primary key. Smaller rScore values come first.
180424** If the rScore is a tie, then use iLevel as the tie breaker with smaller
180425** iLevel values coming first. In this way, if rScore is the same for all
180426** SearchPoints, then iLevel becomes the deciding factor and the result
180427** is a depth-first search, which is the desired default behavior.
180428*/
180429static int rtreeSearchPointCompare(
180430 const RtreeSearchPoint *pA,
180431 const RtreeSearchPoint *pB
180432){
180433 if( pA->rScore<pB->rScore ) return -1;
180434 if( pA->rScore>pB->rScore ) return +1;
180435 if( pA->iLevel<pB->iLevel ) return -1;
180436 if( pA->iLevel>pB->iLevel ) return +1;
180437 return 0;
180438}
180439
180440/*
180441** Interchange two search points in a cursor.
180442*/
180443static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){
180444 RtreeSearchPoint t = p->aPoint[i];
180445 assert( i<j );
180446 p->aPoint[i] = p->aPoint[j];
180447 p->aPoint[j] = t;
180448 i++; j++;
180449 if( i<RTREE_CACHE_SZ ){
180450 if( j>=RTREE_CACHE_SZ ){
180451 nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]);
180452 p->aNode[i] = 0;
180453 }else{
180454 RtreeNode *pTemp = p->aNode[i];
180455 p->aNode[i] = p->aNode[j];
180456 p->aNode[j] = pTemp;
180457 }
180458 }
180459}
180460
180461/*
180462** Return the search point with the lowest current score.
180463*/
180464static RtreeSearchPoint *rtreeSearchPointFirst(RtreeCursor *pCur){
180465 return pCur->bPoint ? &pCur->sPoint : pCur->nPoint ? pCur->aPoint : 0;
180466}
180467
180468/*
180469** Get the RtreeNode for the search point with the lowest score.
180470*/
180471static RtreeNode *rtreeNodeOfFirstSearchPoint(RtreeCursor *pCur, int *pRC){
180472 sqlite3_int64 id;
180473 int ii = 1 - pCur->bPoint;
180474 assert( ii==0 || ii==1 );
180475 assert( pCur->bPoint || pCur->nPoint );
180476 if( pCur->aNode[ii]==0 ){
180477 assert( pRC!=0 );
180478 id = ii ? pCur->aPoint[0].id : pCur->sPoint.id;
180479 *pRC = nodeAcquire(RTREE_OF_CURSOR(pCur), id, 0, &pCur->aNode[ii]);
180480 }
180481 return pCur->aNode[ii];
180482}
180483
180484/*
180485** Push a new element onto the priority queue
180486*/
180487static RtreeSearchPoint *rtreeEnqueue(
180488 RtreeCursor *pCur, /* The cursor */
180489 RtreeDValue rScore, /* Score for the new search point */
180490 u8 iLevel /* Level for the new search point */
180491){
180492 int i, j;
180493 RtreeSearchPoint *pNew;
180494 if( pCur->nPoint>=pCur->nPointAlloc ){
180495 int nNew = pCur->nPointAlloc*2 + 8;
180496 pNew = sqlite3_realloc(pCur->aPoint, nNew*sizeof(pCur->aPoint[0]));
180497 if( pNew==0 ) return 0;
180498 pCur->aPoint = pNew;
180499 pCur->nPointAlloc = nNew;
180500 }
180501 i = pCur->nPoint++;
180502 pNew = pCur->aPoint + i;
180503 pNew->rScore = rScore;
180504 pNew->iLevel = iLevel;
180505 assert( iLevel<=RTREE_MAX_DEPTH );
180506 while( i>0 ){
180507 RtreeSearchPoint *pParent;
180508 j = (i-1)/2;
180509 pParent = pCur->aPoint + j;
180510 if( rtreeSearchPointCompare(pNew, pParent)>=0 ) break;
180511 rtreeSearchPointSwap(pCur, j, i);
180512 i = j;
180513 pNew = pParent;
180514 }
180515 return pNew;
180516}
180517
180518/*
180519** Allocate a new RtreeSearchPoint and return a pointer to it. Return
180520** NULL if malloc fails.
180521*/
180522static RtreeSearchPoint *rtreeSearchPointNew(
180523 RtreeCursor *pCur, /* The cursor */
180524 RtreeDValue rScore, /* Score for the new search point */
180525 u8 iLevel /* Level for the new search point */
180526){
180527 RtreeSearchPoint *pNew, *pFirst;
180528 pFirst = rtreeSearchPointFirst(pCur);
180529 pCur->anQueue[iLevel]++;
180530 if( pFirst==0
180531 || pFirst->rScore>rScore
180532 || (pFirst->rScore==rScore && pFirst->iLevel>iLevel)
180533 ){
180534 if( pCur->bPoint ){
180535 int ii;
180536 pNew = rtreeEnqueue(pCur, rScore, iLevel);
180537 if( pNew==0 ) return 0;
180538 ii = (int)(pNew - pCur->aPoint) + 1;
180539 if( ii<RTREE_CACHE_SZ ){
180540 assert( pCur->aNode[ii]==0 );
180541 pCur->aNode[ii] = pCur->aNode[0];
180542 }else{
180543 nodeRelease(RTREE_OF_CURSOR(pCur), pCur->aNode[0]);
180544 }
180545 pCur->aNode[0] = 0;
180546 *pNew = pCur->sPoint;
180547 }
180548 pCur->sPoint.rScore = rScore;
180549 pCur->sPoint.iLevel = iLevel;
180550 pCur->bPoint = 1;
180551 return &pCur->sPoint;
180552 }else{
180553 return rtreeEnqueue(pCur, rScore, iLevel);
180554 }
180555}
180556
180557#if 0
180558/* Tracing routines for the RtreeSearchPoint queue */
180559static void tracePoint(RtreeSearchPoint *p, int idx, RtreeCursor *pCur){
180560 if( idx<0 ){ printf(" s"); }else{ printf("%2d", idx); }
180561 printf(" %d.%05lld.%02d %g %d",
180562 p->iLevel, p->id, p->iCell, p->rScore, p->eWithin
180563 );
180564 idx++;
180565 if( idx<RTREE_CACHE_SZ ){
180566 printf(" %p\n", pCur->aNode[idx]);
180567 }else{
180568 printf("\n");
180569 }
180570}
180571static void traceQueue(RtreeCursor *pCur, const char *zPrefix){
180572 int ii;
180573 printf("=== %9s ", zPrefix);
180574 if( pCur->bPoint ){
180575 tracePoint(&pCur->sPoint, -1, pCur);
180576 }
180577 for(ii=0; ii<pCur->nPoint; ii++){
180578 if( ii>0 || pCur->bPoint ) printf(" ");
180579 tracePoint(&pCur->aPoint[ii], ii, pCur);
180580 }
180581}
180582# define RTREE_QUEUE_TRACE(A,B) traceQueue(A,B)
180583#else
180584# define RTREE_QUEUE_TRACE(A,B) /* no-op */
180585#endif
180586
180587/* Remove the search point with the lowest current score.
180588*/
180589static void rtreeSearchPointPop(RtreeCursor *p){
180590 int i, j, k, n;
180591 i = 1 - p->bPoint;
180592 assert( i==0 || i==1 );
180593 if( p->aNode[i] ){
180594 nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]);
180595 p->aNode[i] = 0;
180596 }
180597 if( p->bPoint ){
180598 p->anQueue[p->sPoint.iLevel]--;
180599 p->bPoint = 0;
180600 }else if( p->nPoint ){
180601 p->anQueue[p->aPoint[0].iLevel]--;
180602 n = --p->nPoint;
180603 p->aPoint[0] = p->aPoint[n];
180604 if( n<RTREE_CACHE_SZ-1 ){
180605 p->aNode[1] = p->aNode[n+1];
180606 p->aNode[n+1] = 0;
180607 }
180608 i = 0;
180609 while( (j = i*2+1)<n ){
180610 k = j+1;
180611 if( k<n && rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[j])<0 ){
180612 if( rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[i])<0 ){
180613 rtreeSearchPointSwap(p, i, k);
180614 i = k;
180615 }else{
180616 break;
180617 }
180618 }else{
180619 if( rtreeSearchPointCompare(&p->aPoint[j], &p->aPoint[i])<0 ){
180620 rtreeSearchPointSwap(p, i, j);
180621 i = j;
180622 }else{
180623 break;
180624 }
180625 }
180626 }
180627 }
180628}
180629
180630
180631/*
180632** Continue the search on cursor pCur until the front of the queue
180633** contains an entry suitable for returning as a result-set row,
180634** or until the RtreeSearchPoint queue is empty, indicating that the
180635** query has completed.
180636*/
180637static int rtreeStepToLeaf(RtreeCursor *pCur){
180638 RtreeSearchPoint *p;
180639 Rtree *pRtree = RTREE_OF_CURSOR(pCur);
180640 RtreeNode *pNode;
180641 int eWithin;
180642 int rc = SQLITE_OK;
180643 int nCell;
180644 int nConstraint = pCur->nConstraint;
180645 int ii;
180646 int eInt;
180647 RtreeSearchPoint x;
180648
180649 eInt = pRtree->eCoordType==RTREE_COORD_INT32;
180650 while( (p = rtreeSearchPointFirst(pCur))!=0 && p->iLevel>0 ){
180651 pNode = rtreeNodeOfFirstSearchPoint(pCur, &rc);
180652 if( rc ) return rc;
180653 nCell = NCELL(pNode);
180654 assert( nCell<200 );
180655 while( p->iCell<nCell ){
180656 sqlite3_rtree_dbl rScore = (sqlite3_rtree_dbl)-1;
180657 u8 *pCellData = pNode->zData + (4+pRtree->nBytesPerCell*p->iCell);
180658 eWithin = FULLY_WITHIN;
180659 for(ii=0; ii<nConstraint; ii++){
180660 RtreeConstraint *pConstraint = pCur->aConstraint + ii;
180661 if( pConstraint->op>=RTREE_MATCH ){
180662 rc = rtreeCallbackConstraint(pConstraint, eInt, pCellData, p,
180663 &rScore, &eWithin);
180664 if( rc ) return rc;
180665 }else if( p->iLevel==1 ){
180666 rtreeLeafConstraint(pConstraint, eInt, pCellData, &eWithin);
180667 }else{
180668 rtreeNonleafConstraint(pConstraint, eInt, pCellData, &eWithin);
180669 }
180670 if( eWithin==NOT_WITHIN ) break;
180671 }
180672 p->iCell++;
180673 if( eWithin==NOT_WITHIN ) continue;
180674 x.iLevel = p->iLevel - 1;
180675 if( x.iLevel ){
180676 x.id = readInt64(pCellData);
180677 x.iCell = 0;
180678 }else{
180679 x.id = p->id;
180680 x.iCell = p->iCell - 1;
180681 }
180682 if( p->iCell>=nCell ){
180683 RTREE_QUEUE_TRACE(pCur, "POP-S:");
180684 rtreeSearchPointPop(pCur);
180685 }
180686 if( rScore<RTREE_ZERO ) rScore = RTREE_ZERO;
180687 p = rtreeSearchPointNew(pCur, rScore, x.iLevel);
180688 if( p==0 ) return SQLITE_NOMEM;
180689 p->eWithin = (u8)eWithin;
180690 p->id = x.id;
180691 p->iCell = x.iCell;
180692 RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
180693 break;
180694 }
180695 if( p->iCell>=nCell ){
180696 RTREE_QUEUE_TRACE(pCur, "POP-Se:");
180697 rtreeSearchPointPop(pCur);
180698 }
180699 }
180700 pCur->atEOF = p==0;
180701 return SQLITE_OK;
180702}
180703
180704/*
180705** Rtree virtual table module xNext method.
180706*/
180707static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
180708 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
180709 int rc = SQLITE_OK;
180710
180711 /* Move to the next entry that matches the configured constraints. */
180712 RTREE_QUEUE_TRACE(pCsr, "POP-Nx:");
180713 if( pCsr->bAuxValid ){
180714 pCsr->bAuxValid = 0;
180715 sqlite3_reset(pCsr->pReadAux);
180716 }
180717 rtreeSearchPointPop(pCsr);
180718 rc = rtreeStepToLeaf(pCsr);
180719 return rc;
180720}
180721
180722/*
180723** Rtree virtual table module xRowid method.
180724*/
180725static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
180726 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
180727 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
180728 int rc = SQLITE_OK;
180729 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
180730 if( rc==SQLITE_OK && p ){
180731 *pRowid = nodeGetRowid(RTREE_OF_CURSOR(pCsr), pNode, p->iCell);
180732 }
180733 return rc;
180734}
180735
180736/*
180737** Rtree virtual table module xColumn method.
180738*/
180739static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
180740 Rtree *pRtree = (Rtree *)cur->pVtab;
180741 RtreeCursor *pCsr = (RtreeCursor *)cur;
180742 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
180743 RtreeCoord c;
180744 int rc = SQLITE_OK;
180745 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
180746
180747 if( rc ) return rc;
180748 if( p==0 ) return SQLITE_OK;
180749 if( i==0 ){
180750 sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
180751 }else if( i<=pRtree->nDim2 ){
180752 nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
180753#ifndef SQLITE_RTREE_INT_ONLY
180754 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
180755 sqlite3_result_double(ctx, c.f);
180756 }else
180757#endif
180758 {
180759 assert( pRtree->eCoordType==RTREE_COORD_INT32 );
180760 sqlite3_result_int(ctx, c.i);
180761 }
180762 }else{
180763 if( !pCsr->bAuxValid ){
180764 if( pCsr->pReadAux==0 ){
180765 rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0,
180766 &pCsr->pReadAux, 0);
180767 if( rc ) return rc;
180768 }
180769 sqlite3_bind_int64(pCsr->pReadAux, 1,
180770 nodeGetRowid(pRtree, pNode, p->iCell));
180771 rc = sqlite3_step(pCsr->pReadAux);
180772 if( rc==SQLITE_ROW ){
180773 pCsr->bAuxValid = 1;
180774 }else{
180775 sqlite3_reset(pCsr->pReadAux);
180776 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
180777 return rc;
180778 }
180779 }
180780 sqlite3_result_value(ctx,
180781 sqlite3_column_value(pCsr->pReadAux, i - pRtree->nDim2 + 1));
180782 }
180783 return SQLITE_OK;
180784}
180785
180786/*
180787** Use nodeAcquire() to obtain the leaf node containing the record with
180788** rowid iRowid. If successful, set *ppLeaf to point to the node and
180789** return SQLITE_OK. If there is no such record in the table, set
180790** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf
180791** to zero and return an SQLite error code.
180792*/
180793static int findLeafNode(
180794 Rtree *pRtree, /* RTree to search */
180795 i64 iRowid, /* The rowid searching for */
180796 RtreeNode **ppLeaf, /* Write the node here */
180797 sqlite3_int64 *piNode /* Write the node-id here */
180798){
180799 int rc;
180800 *ppLeaf = 0;
180801 sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid);
180802 if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW ){
180803 i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0);
180804 if( piNode ) *piNode = iNode;
180805 rc = nodeAcquire(pRtree, iNode, 0, ppLeaf);
180806 sqlite3_reset(pRtree->pReadRowid);
180807 }else{
180808 rc = sqlite3_reset(pRtree->pReadRowid);
180809 }
180810 return rc;
180811}
180812
180813/*
180814** This function is called to configure the RtreeConstraint object passed
180815** as the second argument for a MATCH constraint. The value passed as the
180816** first argument to this function is the right-hand operand to the MATCH
180817** operator.
180818*/
180819static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
180820 RtreeMatchArg *pBlob, *pSrc; /* BLOB returned by geometry function */
180821 sqlite3_rtree_query_info *pInfo; /* Callback information */
180822
180823 pSrc = sqlite3_value_pointer(pValue, "RtreeMatchArg");
180824 if( pSrc==0 ) return SQLITE_ERROR;
180825 pInfo = (sqlite3_rtree_query_info*)
180826 sqlite3_malloc64( sizeof(*pInfo)+pSrc->iSize );
180827 if( !pInfo ) return SQLITE_NOMEM;
180828 memset(pInfo, 0, sizeof(*pInfo));
180829 pBlob = (RtreeMatchArg*)&pInfo[1];
180830 memcpy(pBlob, pSrc, pSrc->iSize);
180831 pInfo->pContext = pBlob->cb.pContext;
180832 pInfo->nParam = pBlob->nParam;
180833 pInfo->aParam = pBlob->aParam;
180834 pInfo->apSqlParam = pBlob->apSqlParam;
180835
180836 if( pBlob->cb.xGeom ){
180837 pCons->u.xGeom = pBlob->cb.xGeom;
180838 }else{
180839 pCons->op = RTREE_QUERY;
180840 pCons->u.xQueryFunc = pBlob->cb.xQueryFunc;
180841 }
180842 pCons->pInfo = pInfo;
180843 return SQLITE_OK;
180844}
180845
180846/*
180847** Rtree virtual table module xFilter method.
180848*/
180849static int rtreeFilter(
180850 sqlite3_vtab_cursor *pVtabCursor,
180851 int idxNum, const char *idxStr,
180852 int argc, sqlite3_value **argv
180853){
180854 Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
180855 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
180856 RtreeNode *pRoot = 0;
180857 int ii;
180858 int rc = SQLITE_OK;
180859 int iCell = 0;
180860 sqlite3_stmt *pStmt;
180861
180862 rtreeReference(pRtree);
180863
180864 /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
180865 freeCursorConstraints(pCsr);
180866 sqlite3_free(pCsr->aPoint);
180867 pStmt = pCsr->pReadAux;
180868 memset(pCsr, 0, sizeof(RtreeCursor));
180869 pCsr->base.pVtab = (sqlite3_vtab*)pRtree;
180870 pCsr->pReadAux = pStmt;
180871
180872 pCsr->iStrategy = idxNum;
180873 if( idxNum==1 ){
180874 /* Special case - lookup by rowid. */
180875 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
180876 RtreeSearchPoint *p; /* Search point for the leaf */
180877 i64 iRowid = sqlite3_value_int64(argv[0]);
180878 i64 iNode = 0;
180879 rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
180880 if( rc==SQLITE_OK && pLeaf!=0 ){
180881 p = rtreeSearchPointNew(pCsr, RTREE_ZERO, 0);
180882 assert( p!=0 ); /* Always returns pCsr->sPoint */
180883 pCsr->aNode[0] = pLeaf;
180884 p->id = iNode;
180885 p->eWithin = PARTLY_WITHIN;
180886 rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
180887 p->iCell = (u8)iCell;
180888 RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
180889 }else{
180890 pCsr->atEOF = 1;
180891 }
180892 }else{
180893 /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
180894 ** with the configured constraints.
180895 */
180896 rc = nodeAcquire(pRtree, 1, 0, &pRoot);
180897 if( rc==SQLITE_OK && argc>0 ){
180898 pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc);
180899 pCsr->nConstraint = argc;
180900 if( !pCsr->aConstraint ){
180901 rc = SQLITE_NOMEM;
180902 }else{
180903 memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
180904 memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1));
180905 assert( (idxStr==0 && argc==0)
180906 || (idxStr && (int)strlen(idxStr)==argc*2) );
180907 for(ii=0; ii<argc; ii++){
180908 RtreeConstraint *p = &pCsr->aConstraint[ii];
180909 p->op = idxStr[ii*2];
180910 p->iCoord = idxStr[ii*2+1]-'0';
180911 if( p->op>=RTREE_MATCH ){
180912 /* A MATCH operator. The right-hand-side must be a blob that
180913 ** can be cast into an RtreeMatchArg object. One created using
180914 ** an sqlite3_rtree_geometry_callback() SQL user function.
180915 */
180916 rc = deserializeGeometry(argv[ii], p);
180917 if( rc!=SQLITE_OK ){
180918 break;
180919 }
180920 p->pInfo->nCoord = pRtree->nDim2;
180921 p->pInfo->anQueue = pCsr->anQueue;
180922 p->pInfo->mxLevel = pRtree->iDepth + 1;
180923 }else{
180924#ifdef SQLITE_RTREE_INT_ONLY
180925 p->u.rValue = sqlite3_value_int64(argv[ii]);
180926#else
180927 p->u.rValue = sqlite3_value_double(argv[ii]);
180928#endif
180929 }
180930 }
180931 }
180932 }
180933 if( rc==SQLITE_OK ){
180934 RtreeSearchPoint *pNew;
180935 pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, (u8)(pRtree->iDepth+1));
180936 if( pNew==0 ) return SQLITE_NOMEM;
180937 pNew->id = 1;
180938 pNew->iCell = 0;
180939 pNew->eWithin = PARTLY_WITHIN;
180940 assert( pCsr->bPoint==1 );
180941 pCsr->aNode[0] = pRoot;
180942 pRoot = 0;
180943 RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
180944 rc = rtreeStepToLeaf(pCsr);
180945 }
180946 }
180947
180948 nodeRelease(pRtree, pRoot);
180949 rtreeRelease(pRtree);
180950 return rc;
180951}
180952
180953/*
180954** Rtree virtual table module xBestIndex method. There are three
180955** table scan strategies to choose from (in order from most to
180956** least desirable):
180957**
180958** idxNum idxStr Strategy
180959** ------------------------------------------------
180960** 1 Unused Direct lookup by rowid.
180961** 2 See below R-tree query or full-table scan.
180962** ------------------------------------------------
180963**
180964** If strategy 1 is used, then idxStr is not meaningful. If strategy
180965** 2 is used, idxStr is formatted to contain 2 bytes for each
180966** constraint used. The first two bytes of idxStr correspond to
180967** the constraint in sqlite3_index_info.aConstraintUsage[] with
180968** (argvIndex==1) etc.
180969**
180970** The first of each pair of bytes in idxStr identifies the constraint
180971** operator as follows:
180972**
180973** Operator Byte Value
180974** ----------------------
180975** = 0x41 ('A')
180976** <= 0x42 ('B')
180977** < 0x43 ('C')
180978** >= 0x44 ('D')
180979** > 0x45 ('E')
180980** MATCH 0x46 ('F')
180981** ----------------------
180982**
180983** The second of each pair of bytes identifies the coordinate column
180984** to which the constraint applies. The leftmost coordinate column
180985** is 'a', the second from the left 'b' etc.
180986*/
180987static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
180988 Rtree *pRtree = (Rtree*)tab;
180989 int rc = SQLITE_OK;
180990 int ii;
180991 int bMatch = 0; /* True if there exists a MATCH constraint */
180992 i64 nRow; /* Estimated rows returned by this scan */
180993
180994 int iIdx = 0;
180995 char zIdxStr[RTREE_MAX_DIMENSIONS*8+1];
180996 memset(zIdxStr, 0, sizeof(zIdxStr));
180997
180998 /* Check if there exists a MATCH constraint - even an unusable one. If there
180999 ** is, do not consider the lookup-by-rowid plan as using such a plan would
181000 ** require the VDBE to evaluate the MATCH constraint, which is not currently
181001 ** possible. */
181002 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
181003 if( pIdxInfo->aConstraint[ii].op==SQLITE_INDEX_CONSTRAINT_MATCH ){
181004 bMatch = 1;
181005 }
181006 }
181007
181008 assert( pIdxInfo->idxStr==0 );
181009 for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
181010 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
181011
181012 if( bMatch==0 && p->usable
181013 && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ
181014 ){
181015 /* We have an equality constraint on the rowid. Use strategy 1. */
181016 int jj;
181017 for(jj=0; jj<ii; jj++){
181018 pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
181019 pIdxInfo->aConstraintUsage[jj].omit = 0;
181020 }
181021 pIdxInfo->idxNum = 1;
181022 pIdxInfo->aConstraintUsage[ii].argvIndex = 1;
181023 pIdxInfo->aConstraintUsage[jj].omit = 1;
181024
181025 /* This strategy involves a two rowid lookups on an B-Tree structures
181026 ** and then a linear search of an R-Tree node. This should be
181027 ** considered almost as quick as a direct rowid lookup (for which
181028 ** sqlite uses an internal cost of 0.0). It is expected to return
181029 ** a single row.
181030 */
181031 pIdxInfo->estimatedCost = 30.0;
181032 pIdxInfo->estimatedRows = 1;
181033 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
181034 return SQLITE_OK;
181035 }
181036
181037 if( p->usable
181038 && ((p->iColumn>0 && p->iColumn<=pRtree->nDim2)
181039 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH)
181040 ){
181041 u8 op;
181042 switch( p->op ){
181043 case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break;
181044 case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break;
181045 case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break;
181046 case SQLITE_INDEX_CONSTRAINT_LT: op = RTREE_LT; break;
181047 case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break;
181048 default:
181049 assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
181050 op = RTREE_MATCH;
181051 break;
181052 }
181053 zIdxStr[iIdx++] = op;
181054 zIdxStr[iIdx++] = (char)(p->iColumn - 1 + '0');
181055 pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
181056 pIdxInfo->aConstraintUsage[ii].omit = 1;
181057 }
181058 }
181059
181060 pIdxInfo->idxNum = 2;
181061 pIdxInfo->needToFreeIdxStr = 1;
181062 if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){
181063 return SQLITE_NOMEM;
181064 }
181065
181066 nRow = pRtree->nRowEst >> (iIdx/2);
181067 pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
181068 pIdxInfo->estimatedRows = nRow;
181069
181070 return rc;
181071}
181072
181073/*
181074** Return the N-dimensional volumn of the cell stored in *p.
181075*/
181076static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
181077 RtreeDValue area = (RtreeDValue)1;
181078 assert( pRtree->nDim>=1 && pRtree->nDim<=5 );
181079#ifndef SQLITE_RTREE_INT_ONLY
181080 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
181081 switch( pRtree->nDim ){
181082 case 5: area = p->aCoord[9].f - p->aCoord[8].f;
181083 case 4: area *= p->aCoord[7].f - p->aCoord[6].f;
181084 case 3: area *= p->aCoord[5].f - p->aCoord[4].f;
181085 case 2: area *= p->aCoord[3].f - p->aCoord[2].f;
181086 default: area *= p->aCoord[1].f - p->aCoord[0].f;
181087 }
181088 }else
181089#endif
181090 {
181091 switch( pRtree->nDim ){
181092 case 5: area = p->aCoord[9].i - p->aCoord[8].i;
181093 case 4: area *= p->aCoord[7].i - p->aCoord[6].i;
181094 case 3: area *= p->aCoord[5].i - p->aCoord[4].i;
181095 case 2: area *= p->aCoord[3].i - p->aCoord[2].i;
181096 default: area *= p->aCoord[1].i - p->aCoord[0].i;
181097 }
181098 }
181099 return area;
181100}
181101
181102/*
181103** Return the margin length of cell p. The margin length is the sum
181104** of the objects size in each dimension.
181105*/
181106static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
181107 RtreeDValue margin = 0;
181108 int ii = pRtree->nDim2 - 2;
181109 do{
181110 margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
181111 ii -= 2;
181112 }while( ii>=0 );
181113 return margin;
181114}
181115
181116/*
181117** Store the union of cells p1 and p2 in p1.
181118*/
181119static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
181120 int ii = 0;
181121 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
181122 do{
181123 p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
181124 p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
181125 ii += 2;
181126 }while( ii<pRtree->nDim2 );
181127 }else{
181128 do{
181129 p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
181130 p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
181131 ii += 2;
181132 }while( ii<pRtree->nDim2 );
181133 }
181134}
181135
181136/*
181137** Return true if the area covered by p2 is a subset of the area covered
181138** by p1. False otherwise.
181139*/
181140static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
181141 int ii;
181142 int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
181143 for(ii=0; ii<pRtree->nDim2; ii+=2){
181144 RtreeCoord *a1 = &p1->aCoord[ii];
181145 RtreeCoord *a2 = &p2->aCoord[ii];
181146 if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f))
181147 || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i))
181148 ){
181149 return 0;
181150 }
181151 }
181152 return 1;
181153}
181154
181155/*
181156** Return the amount cell p would grow by if it were unioned with pCell.
181157*/
181158static RtreeDValue cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
181159 RtreeDValue area;
181160 RtreeCell cell;
181161 memcpy(&cell, p, sizeof(RtreeCell));
181162 area = cellArea(pRtree, &cell);
181163 cellUnion(pRtree, &cell, pCell);
181164 return (cellArea(pRtree, &cell)-area);
181165}
181166
181167static RtreeDValue cellOverlap(
181168 Rtree *pRtree,
181169 RtreeCell *p,
181170 RtreeCell *aCell,
181171 int nCell
181172){
181173 int ii;
181174 RtreeDValue overlap = RTREE_ZERO;
181175 for(ii=0; ii<nCell; ii++){
181176 int jj;
181177 RtreeDValue o = (RtreeDValue)1;
181178 for(jj=0; jj<pRtree->nDim2; jj+=2){
181179 RtreeDValue x1, x2;
181180 x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
181181 x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
181182 if( x2<x1 ){
181183 o = (RtreeDValue)0;
181184 break;
181185 }else{
181186 o = o * (x2-x1);
181187 }
181188 }
181189 overlap += o;
181190 }
181191 return overlap;
181192}
181193
181194
181195/*
181196** This function implements the ChooseLeaf algorithm from Gutman[84].
181197** ChooseSubTree in r*tree terminology.
181198*/
181199static int ChooseLeaf(
181200 Rtree *pRtree, /* Rtree table */
181201 RtreeCell *pCell, /* Cell to insert into rtree */
181202 int iHeight, /* Height of sub-tree rooted at pCell */
181203 RtreeNode **ppLeaf /* OUT: Selected leaf page */
181204){
181205 int rc;
181206 int ii;
181207 RtreeNode *pNode = 0;
181208 rc = nodeAcquire(pRtree, 1, 0, &pNode);
181209
181210 for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
181211 int iCell;
181212 sqlite3_int64 iBest = 0;
181213
181214 RtreeDValue fMinGrowth = RTREE_ZERO;
181215 RtreeDValue fMinArea = RTREE_ZERO;
181216
181217 int nCell = NCELL(pNode);
181218 RtreeCell cell;
181219 RtreeNode *pChild;
181220
181221 RtreeCell *aCell = 0;
181222
181223 /* Select the child node which will be enlarged the least if pCell
181224 ** is inserted into it. Resolve ties by choosing the entry with
181225 ** the smallest area.
181226 */
181227 for(iCell=0; iCell<nCell; iCell++){
181228 int bBest = 0;
181229 RtreeDValue growth;
181230 RtreeDValue area;
181231 nodeGetCell(pRtree, pNode, iCell, &cell);
181232 growth = cellGrowth(pRtree, &cell, pCell);
181233 area = cellArea(pRtree, &cell);
181234 if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){
181235 bBest = 1;
181236 }
181237 if( bBest ){
181238 fMinGrowth = growth;
181239 fMinArea = area;
181240 iBest = cell.iRowid;
181241 }
181242 }
181243
181244 sqlite3_free(aCell);
181245 rc = nodeAcquire(pRtree, iBest, pNode, &pChild);
181246 nodeRelease(pRtree, pNode);
181247 pNode = pChild;
181248 }
181249
181250 *ppLeaf = pNode;
181251 return rc;
181252}
181253
181254/*
181255** A cell with the same content as pCell has just been inserted into
181256** the node pNode. This function updates the bounding box cells in
181257** all ancestor elements.
181258*/
181259static int AdjustTree(
181260 Rtree *pRtree, /* Rtree table */
181261 RtreeNode *pNode, /* Adjust ancestry of this node. */
181262 RtreeCell *pCell /* This cell was just inserted */
181263){
181264 RtreeNode *p = pNode;
181265 while( p->pParent ){
181266 RtreeNode *pParent = p->pParent;
181267 RtreeCell cell;
181268 int iCell;
181269
181270 if( nodeParentIndex(pRtree, p, &iCell) ){
181271 return SQLITE_CORRUPT_VTAB;
181272 }
181273
181274 nodeGetCell(pRtree, pParent, iCell, &cell);
181275 if( !cellContains(pRtree, &cell, pCell) ){
181276 cellUnion(pRtree, &cell, pCell);
181277 nodeOverwriteCell(pRtree, pParent, &cell, iCell);
181278 }
181279
181280 p = pParent;
181281 }
181282 return SQLITE_OK;
181283}
181284
181285/*
181286** Write mapping (iRowid->iNode) to the <rtree>_rowid table.
181287*/
181288static int rowidWrite(Rtree *pRtree, sqlite3_int64 iRowid, sqlite3_int64 iNode){
181289 sqlite3_bind_int64(pRtree->pWriteRowid, 1, iRowid);
181290 sqlite3_bind_int64(pRtree->pWriteRowid, 2, iNode);
181291 sqlite3_step(pRtree->pWriteRowid);
181292 return sqlite3_reset(pRtree->pWriteRowid);
181293}
181294
181295/*
181296** Write mapping (iNode->iPar) to the <rtree>_parent table.
181297*/
181298static int parentWrite(Rtree *pRtree, sqlite3_int64 iNode, sqlite3_int64 iPar){
181299 sqlite3_bind_int64(pRtree->pWriteParent, 1, iNode);
181300 sqlite3_bind_int64(pRtree->pWriteParent, 2, iPar);
181301 sqlite3_step(pRtree->pWriteParent);
181302 return sqlite3_reset(pRtree->pWriteParent);
181303}
181304
181305static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int);
181306
181307
181308/*
181309** Arguments aIdx, aDistance and aSpare all point to arrays of size
181310** nIdx. The aIdx array contains the set of integers from 0 to
181311** (nIdx-1) in no particular order. This function sorts the values
181312** in aIdx according to the indexed values in aDistance. For
181313** example, assuming the inputs:
181314**
181315** aIdx = { 0, 1, 2, 3 }
181316** aDistance = { 5.0, 2.0, 7.0, 6.0 }
181317**
181318** this function sets the aIdx array to contain:
181319**
181320** aIdx = { 0, 1, 2, 3 }
181321**
181322** The aSpare array is used as temporary working space by the
181323** sorting algorithm.
181324*/
181325static void SortByDistance(
181326 int *aIdx,
181327 int nIdx,
181328 RtreeDValue *aDistance,
181329 int *aSpare
181330){
181331 if( nIdx>1 ){
181332 int iLeft = 0;
181333 int iRight = 0;
181334
181335 int nLeft = nIdx/2;
181336 int nRight = nIdx-nLeft;
181337 int *aLeft = aIdx;
181338 int *aRight = &aIdx[nLeft];
181339
181340 SortByDistance(aLeft, nLeft, aDistance, aSpare);
181341 SortByDistance(aRight, nRight, aDistance, aSpare);
181342
181343 memcpy(aSpare, aLeft, sizeof(int)*nLeft);
181344 aLeft = aSpare;
181345
181346 while( iLeft<nLeft || iRight<nRight ){
181347 if( iLeft==nLeft ){
181348 aIdx[iLeft+iRight] = aRight[iRight];
181349 iRight++;
181350 }else if( iRight==nRight ){
181351 aIdx[iLeft+iRight] = aLeft[iLeft];
181352 iLeft++;
181353 }else{
181354 RtreeDValue fLeft = aDistance[aLeft[iLeft]];
181355 RtreeDValue fRight = aDistance[aRight[iRight]];
181356 if( fLeft<fRight ){
181357 aIdx[iLeft+iRight] = aLeft[iLeft];
181358 iLeft++;
181359 }else{
181360 aIdx[iLeft+iRight] = aRight[iRight];
181361 iRight++;
181362 }
181363 }
181364 }
181365
181366#if 0
181367 /* Check that the sort worked */
181368 {
181369 int jj;
181370 for(jj=1; jj<nIdx; jj++){
181371 RtreeDValue left = aDistance[aIdx[jj-1]];
181372 RtreeDValue right = aDistance[aIdx[jj]];
181373 assert( left<=right );
181374 }
181375 }
181376#endif
181377 }
181378}
181379
181380/*
181381** Arguments aIdx, aCell and aSpare all point to arrays of size
181382** nIdx. The aIdx array contains the set of integers from 0 to
181383** (nIdx-1) in no particular order. This function sorts the values
181384** in aIdx according to dimension iDim of the cells in aCell. The
181385** minimum value of dimension iDim is considered first, the
181386** maximum used to break ties.
181387**
181388** The aSpare array is used as temporary working space by the
181389** sorting algorithm.
181390*/
181391static void SortByDimension(
181392 Rtree *pRtree,
181393 int *aIdx,
181394 int nIdx,
181395 int iDim,
181396 RtreeCell *aCell,
181397 int *aSpare
181398){
181399 if( nIdx>1 ){
181400
181401 int iLeft = 0;
181402 int iRight = 0;
181403
181404 int nLeft = nIdx/2;
181405 int nRight = nIdx-nLeft;
181406 int *aLeft = aIdx;
181407 int *aRight = &aIdx[nLeft];
181408
181409 SortByDimension(pRtree, aLeft, nLeft, iDim, aCell, aSpare);
181410 SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare);
181411
181412 memcpy(aSpare, aLeft, sizeof(int)*nLeft);
181413 aLeft = aSpare;
181414 while( iLeft<nLeft || iRight<nRight ){
181415 RtreeDValue xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
181416 RtreeDValue xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
181417 RtreeDValue xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
181418 RtreeDValue xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
181419 if( (iLeft!=nLeft) && ((iRight==nRight)
181420 || (xleft1<xright1)
181421 || (xleft1==xright1 && xleft2<xright2)
181422 )){
181423 aIdx[iLeft+iRight] = aLeft[iLeft];
181424 iLeft++;
181425 }else{
181426 aIdx[iLeft+iRight] = aRight[iRight];
181427 iRight++;
181428 }
181429 }
181430
181431#if 0
181432 /* Check that the sort worked */
181433 {
181434 int jj;
181435 for(jj=1; jj<nIdx; jj++){
181436 RtreeDValue xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
181437 RtreeDValue xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
181438 RtreeDValue xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
181439 RtreeDValue xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
181440 assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) );
181441 }
181442 }
181443#endif
181444 }
181445}
181446
181447/*
181448** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
181449*/
181450static int splitNodeStartree(
181451 Rtree *pRtree,
181452 RtreeCell *aCell,
181453 int nCell,
181454 RtreeNode *pLeft,
181455 RtreeNode *pRight,
181456 RtreeCell *pBboxLeft,
181457 RtreeCell *pBboxRight
181458){
181459 int **aaSorted;
181460 int *aSpare;
181461 int ii;
181462
181463 int iBestDim = 0;
181464 int iBestSplit = 0;
181465 RtreeDValue fBestMargin = RTREE_ZERO;
181466
181467 int nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
181468
181469 aaSorted = (int **)sqlite3_malloc(nByte);
181470 if( !aaSorted ){
181471 return SQLITE_NOMEM;
181472 }
181473
181474 aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell];
181475 memset(aaSorted, 0, nByte);
181476 for(ii=0; ii<pRtree->nDim; ii++){
181477 int jj;
181478 aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell];
181479 for(jj=0; jj<nCell; jj++){
181480 aaSorted[ii][jj] = jj;
181481 }
181482 SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
181483 }
181484
181485 for(ii=0; ii<pRtree->nDim; ii++){
181486 RtreeDValue margin = RTREE_ZERO;
181487 RtreeDValue fBestOverlap = RTREE_ZERO;
181488 RtreeDValue fBestArea = RTREE_ZERO;
181489 int iBestLeft = 0;
181490 int nLeft;
181491
181492 for(
181493 nLeft=RTREE_MINCELLS(pRtree);
181494 nLeft<=(nCell-RTREE_MINCELLS(pRtree));
181495 nLeft++
181496 ){
181497 RtreeCell left;
181498 RtreeCell right;
181499 int kk;
181500 RtreeDValue overlap;
181501 RtreeDValue area;
181502
181503 memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell));
181504 memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
181505 for(kk=1; kk<(nCell-1); kk++){
181506 if( kk<nLeft ){
181507 cellUnion(pRtree, &left, &aCell[aaSorted[ii][kk]]);
181508 }else{
181509 cellUnion(pRtree, &right, &aCell[aaSorted[ii][kk]]);
181510 }
181511 }
181512 margin += cellMargin(pRtree, &left);
181513 margin += cellMargin(pRtree, &right);
181514 overlap = cellOverlap(pRtree, &left, &right, 1);
181515 area = cellArea(pRtree, &left) + cellArea(pRtree, &right);
181516 if( (nLeft==RTREE_MINCELLS(pRtree))
181517 || (overlap<fBestOverlap)
181518 || (overlap==fBestOverlap && area<fBestArea)
181519 ){
181520 iBestLeft = nLeft;
181521 fBestOverlap = overlap;
181522 fBestArea = area;
181523 }
181524 }
181525
181526 if( ii==0 || margin<fBestMargin ){
181527 iBestDim = ii;
181528 fBestMargin = margin;
181529 iBestSplit = iBestLeft;
181530 }
181531 }
181532
181533 memcpy(pBboxLeft, &aCell[aaSorted[iBestDim][0]], sizeof(RtreeCell));
181534 memcpy(pBboxRight, &aCell[aaSorted[iBestDim][iBestSplit]], sizeof(RtreeCell));
181535 for(ii=0; ii<nCell; ii++){
181536 RtreeNode *pTarget = (ii<iBestSplit)?pLeft:pRight;
181537 RtreeCell *pBbox = (ii<iBestSplit)?pBboxLeft:pBboxRight;
181538 RtreeCell *pCell = &aCell[aaSorted[iBestDim][ii]];
181539 nodeInsertCell(pRtree, pTarget, pCell);
181540 cellUnion(pRtree, pBbox, pCell);
181541 }
181542
181543 sqlite3_free(aaSorted);
181544 return SQLITE_OK;
181545}
181546
181547
181548static int updateMapping(
181549 Rtree *pRtree,
181550 i64 iRowid,
181551 RtreeNode *pNode,
181552 int iHeight
181553){
181554 int (*xSetMapping)(Rtree *, sqlite3_int64, sqlite3_int64);
181555 xSetMapping = ((iHeight==0)?rowidWrite:parentWrite);
181556 if( iHeight>0 ){
181557 RtreeNode *pChild = nodeHashLookup(pRtree, iRowid);
181558 if( pChild ){
181559 nodeRelease(pRtree, pChild->pParent);
181560 nodeReference(pNode);
181561 pChild->pParent = pNode;
181562 }
181563 }
181564 return xSetMapping(pRtree, iRowid, pNode->iNode);
181565}
181566
181567static int SplitNode(
181568 Rtree *pRtree,
181569 RtreeNode *pNode,
181570 RtreeCell *pCell,
181571 int iHeight
181572){
181573 int i;
181574 int newCellIsRight = 0;
181575
181576 int rc = SQLITE_OK;
181577 int nCell = NCELL(pNode);
181578 RtreeCell *aCell;
181579 int *aiUsed;
181580
181581 RtreeNode *pLeft = 0;
181582 RtreeNode *pRight = 0;
181583
181584 RtreeCell leftbbox;
181585 RtreeCell rightbbox;
181586
181587 /* Allocate an array and populate it with a copy of pCell and
181588 ** all cells from node pLeft. Then zero the original node.
181589 */
181590 aCell = sqlite3_malloc((sizeof(RtreeCell)+sizeof(int))*(nCell+1));
181591 if( !aCell ){
181592 rc = SQLITE_NOMEM;
181593 goto splitnode_out;
181594 }
181595 aiUsed = (int *)&aCell[nCell+1];
181596 memset(aiUsed, 0, sizeof(int)*(nCell+1));
181597 for(i=0; i<nCell; i++){
181598 nodeGetCell(pRtree, pNode, i, &aCell[i]);
181599 }
181600 nodeZero(pRtree, pNode);
181601 memcpy(&aCell[nCell], pCell, sizeof(RtreeCell));
181602 nCell++;
181603
181604 if( pNode->iNode==1 ){
181605 pRight = nodeNew(pRtree, pNode);
181606 pLeft = nodeNew(pRtree, pNode);
181607 pRtree->iDepth++;
181608 pNode->isDirty = 1;
181609 writeInt16(pNode->zData, pRtree->iDepth);
181610 }else{
181611 pLeft = pNode;
181612 pRight = nodeNew(pRtree, pLeft->pParent);
181613 pLeft->nRef++;
181614 }
181615
181616 if( !pLeft || !pRight ){
181617 rc = SQLITE_NOMEM;
181618 goto splitnode_out;
181619 }
181620
181621 memset(pLeft->zData, 0, pRtree->iNodeSize);
181622 memset(pRight->zData, 0, pRtree->iNodeSize);
181623
181624 rc = splitNodeStartree(pRtree, aCell, nCell, pLeft, pRight,
181625 &leftbbox, &rightbbox);
181626 if( rc!=SQLITE_OK ){
181627 goto splitnode_out;
181628 }
181629
181630 /* Ensure both child nodes have node numbers assigned to them by calling
181631 ** nodeWrite(). Node pRight always needs a node number, as it was created
181632 ** by nodeNew() above. But node pLeft sometimes already has a node number.
181633 ** In this case avoid the all to nodeWrite().
181634 */
181635 if( SQLITE_OK!=(rc = nodeWrite(pRtree, pRight))
181636 || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pLeft)))
181637 ){
181638 goto splitnode_out;
181639 }
181640
181641 rightbbox.iRowid = pRight->iNode;
181642 leftbbox.iRowid = pLeft->iNode;
181643
181644 if( pNode->iNode==1 ){
181645 rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1);
181646 if( rc!=SQLITE_OK ){
181647 goto splitnode_out;
181648 }
181649 }else{
181650 RtreeNode *pParent = pLeft->pParent;
181651 int iCell;
181652 rc = nodeParentIndex(pRtree, pLeft, &iCell);
181653 if( rc==SQLITE_OK ){
181654 nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell);
181655 rc = AdjustTree(pRtree, pParent, &leftbbox);
181656 }
181657 if( rc!=SQLITE_OK ){
181658 goto splitnode_out;
181659 }
181660 }
181661 if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){
181662 goto splitnode_out;
181663 }
181664
181665 for(i=0; i<NCELL(pRight); i++){
181666 i64 iRowid = nodeGetRowid(pRtree, pRight, i);
181667 rc = updateMapping(pRtree, iRowid, pRight, iHeight);
181668 if( iRowid==pCell->iRowid ){
181669 newCellIsRight = 1;
181670 }
181671 if( rc!=SQLITE_OK ){
181672 goto splitnode_out;
181673 }
181674 }
181675 if( pNode->iNode==1 ){
181676 for(i=0; i<NCELL(pLeft); i++){
181677 i64 iRowid = nodeGetRowid(pRtree, pLeft, i);
181678 rc = updateMapping(pRtree, iRowid, pLeft, iHeight);
181679 if( rc!=SQLITE_OK ){
181680 goto splitnode_out;
181681 }
181682 }
181683 }else if( newCellIsRight==0 ){
181684 rc = updateMapping(pRtree, pCell->iRowid, pLeft, iHeight);
181685 }
181686
181687 if( rc==SQLITE_OK ){
181688 rc = nodeRelease(pRtree, pRight);
181689 pRight = 0;
181690 }
181691 if( rc==SQLITE_OK ){
181692 rc = nodeRelease(pRtree, pLeft);
181693 pLeft = 0;
181694 }
181695
181696splitnode_out:
181697 nodeRelease(pRtree, pRight);
181698 nodeRelease(pRtree, pLeft);
181699 sqlite3_free(aCell);
181700 return rc;
181701}
181702
181703/*
181704** If node pLeaf is not the root of the r-tree and its pParent pointer is
181705** still NULL, load all ancestor nodes of pLeaf into memory and populate
181706** the pLeaf->pParent chain all the way up to the root node.
181707**
181708** This operation is required when a row is deleted (or updated - an update
181709** is implemented as a delete followed by an insert). SQLite provides the
181710** rowid of the row to delete, which can be used to find the leaf on which
181711** the entry resides (argument pLeaf). Once the leaf is located, this
181712** function is called to determine its ancestry.
181713*/
181714static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){
181715 int rc = SQLITE_OK;
181716 RtreeNode *pChild = pLeaf;
181717 while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){
181718 int rc2 = SQLITE_OK; /* sqlite3_reset() return code */
181719 sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode);
181720 rc = sqlite3_step(pRtree->pReadParent);
181721 if( rc==SQLITE_ROW ){
181722 RtreeNode *pTest; /* Used to test for reference loops */
181723 i64 iNode; /* Node number of parent node */
181724
181725 /* Before setting pChild->pParent, test that we are not creating a
181726 ** loop of references (as we would if, say, pChild==pParent). We don't
181727 ** want to do this as it leads to a memory leak when trying to delete
181728 ** the referenced counted node structures.
181729 */
181730 iNode = sqlite3_column_int64(pRtree->pReadParent, 0);
181731 for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent);
181732 if( !pTest ){
181733 rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent);
181734 }
181735 }
181736 rc = sqlite3_reset(pRtree->pReadParent);
181737 if( rc==SQLITE_OK ) rc = rc2;
181738 if( rc==SQLITE_OK && !pChild->pParent ) rc = SQLITE_CORRUPT_VTAB;
181739 pChild = pChild->pParent;
181740 }
181741 return rc;
181742}
181743
181744static int deleteCell(Rtree *, RtreeNode *, int, int);
181745
181746static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){
181747 int rc;
181748 int rc2;
181749 RtreeNode *pParent = 0;
181750 int iCell;
181751
181752 assert( pNode->nRef==1 );
181753
181754 /* Remove the entry in the parent cell. */
181755 rc = nodeParentIndex(pRtree, pNode, &iCell);
181756 if( rc==SQLITE_OK ){
181757 pParent = pNode->pParent;
181758 pNode->pParent = 0;
181759 rc = deleteCell(pRtree, pParent, iCell, iHeight+1);
181760 }
181761 rc2 = nodeRelease(pRtree, pParent);
181762 if( rc==SQLITE_OK ){
181763 rc = rc2;
181764 }
181765 if( rc!=SQLITE_OK ){
181766 return rc;
181767 }
181768
181769 /* Remove the xxx_node entry. */
181770 sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode);
181771 sqlite3_step(pRtree->pDeleteNode);
181772 if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteNode)) ){
181773 return rc;
181774 }
181775
181776 /* Remove the xxx_parent entry. */
181777 sqlite3_bind_int64(pRtree->pDeleteParent, 1, pNode->iNode);
181778 sqlite3_step(pRtree->pDeleteParent);
181779 if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteParent)) ){
181780 return rc;
181781 }
181782
181783 /* Remove the node from the in-memory hash table and link it into
181784 ** the Rtree.pDeleted list. Its contents will be re-inserted later on.
181785 */
181786 nodeHashDelete(pRtree, pNode);
181787 pNode->iNode = iHeight;
181788 pNode->pNext = pRtree->pDeleted;
181789 pNode->nRef++;
181790 pRtree->pDeleted = pNode;
181791
181792 return SQLITE_OK;
181793}
181794
181795static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){
181796 RtreeNode *pParent = pNode->pParent;
181797 int rc = SQLITE_OK;
181798 if( pParent ){
181799 int ii;
181800 int nCell = NCELL(pNode);
181801 RtreeCell box; /* Bounding box for pNode */
181802 nodeGetCell(pRtree, pNode, 0, &box);
181803 for(ii=1; ii<nCell; ii++){
181804 RtreeCell cell;
181805 nodeGetCell(pRtree, pNode, ii, &cell);
181806 cellUnion(pRtree, &box, &cell);
181807 }
181808 box.iRowid = pNode->iNode;
181809 rc = nodeParentIndex(pRtree, pNode, &ii);
181810 if( rc==SQLITE_OK ){
181811 nodeOverwriteCell(pRtree, pParent, &box, ii);
181812 rc = fixBoundingBox(pRtree, pParent);
181813 }
181814 }
181815 return rc;
181816}
181817
181818/*
181819** Delete the cell at index iCell of node pNode. After removing the
181820** cell, adjust the r-tree data structure if required.
181821*/
181822static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){
181823 RtreeNode *pParent;
181824 int rc;
181825
181826 if( SQLITE_OK!=(rc = fixLeafParent(pRtree, pNode)) ){
181827 return rc;
181828 }
181829
181830 /* Remove the cell from the node. This call just moves bytes around
181831 ** the in-memory node image, so it cannot fail.
181832 */
181833 nodeDeleteCell(pRtree, pNode, iCell);
181834
181835 /* If the node is not the tree root and now has less than the minimum
181836 ** number of cells, remove it from the tree. Otherwise, update the
181837 ** cell in the parent node so that it tightly contains the updated
181838 ** node.
181839 */
181840 pParent = pNode->pParent;
181841 assert( pParent || pNode->iNode==1 );
181842 if( pParent ){
181843 if( NCELL(pNode)<RTREE_MINCELLS(pRtree) ){
181844 rc = removeNode(pRtree, pNode, iHeight);
181845 }else{
181846 rc = fixBoundingBox(pRtree, pNode);
181847 }
181848 }
181849
181850 return rc;
181851}
181852
181853static int Reinsert(
181854 Rtree *pRtree,
181855 RtreeNode *pNode,
181856 RtreeCell *pCell,
181857 int iHeight
181858){
181859 int *aOrder;
181860 int *aSpare;
181861 RtreeCell *aCell;
181862 RtreeDValue *aDistance;
181863 int nCell;
181864 RtreeDValue aCenterCoord[RTREE_MAX_DIMENSIONS];
181865 int iDim;
181866 int ii;
181867 int rc = SQLITE_OK;
181868 int n;
181869
181870 memset(aCenterCoord, 0, sizeof(RtreeDValue)*RTREE_MAX_DIMENSIONS);
181871
181872 nCell = NCELL(pNode)+1;
181873 n = (nCell+1)&(~1);
181874
181875 /* Allocate the buffers used by this operation. The allocation is
181876 ** relinquished before this function returns.
181877 */
181878 aCell = (RtreeCell *)sqlite3_malloc(n * (
181879 sizeof(RtreeCell) + /* aCell array */
181880 sizeof(int) + /* aOrder array */
181881 sizeof(int) + /* aSpare array */
181882 sizeof(RtreeDValue) /* aDistance array */
181883 ));
181884 if( !aCell ){
181885 return SQLITE_NOMEM;
181886 }
181887 aOrder = (int *)&aCell[n];
181888 aSpare = (int *)&aOrder[n];
181889 aDistance = (RtreeDValue *)&aSpare[n];
181890
181891 for(ii=0; ii<nCell; ii++){
181892 if( ii==(nCell-1) ){
181893 memcpy(&aCell[ii], pCell, sizeof(RtreeCell));
181894 }else{
181895 nodeGetCell(pRtree, pNode, ii, &aCell[ii]);
181896 }
181897 aOrder[ii] = ii;
181898 for(iDim=0; iDim<pRtree->nDim; iDim++){
181899 aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2]);
181900 aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2+1]);
181901 }
181902 }
181903 for(iDim=0; iDim<pRtree->nDim; iDim++){
181904 aCenterCoord[iDim] = (aCenterCoord[iDim]/(nCell*(RtreeDValue)2));
181905 }
181906
181907 for(ii=0; ii<nCell; ii++){
181908 aDistance[ii] = RTREE_ZERO;
181909 for(iDim=0; iDim<pRtree->nDim; iDim++){
181910 RtreeDValue coord = (DCOORD(aCell[ii].aCoord[iDim*2+1]) -
181911 DCOORD(aCell[ii].aCoord[iDim*2]));
181912 aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
181913 }
181914 }
181915
181916 SortByDistance(aOrder, nCell, aDistance, aSpare);
181917 nodeZero(pRtree, pNode);
181918
181919 for(ii=0; rc==SQLITE_OK && ii<(nCell-(RTREE_MINCELLS(pRtree)+1)); ii++){
181920 RtreeCell *p = &aCell[aOrder[ii]];
181921 nodeInsertCell(pRtree, pNode, p);
181922 if( p->iRowid==pCell->iRowid ){
181923 if( iHeight==0 ){
181924 rc = rowidWrite(pRtree, p->iRowid, pNode->iNode);
181925 }else{
181926 rc = parentWrite(pRtree, p->iRowid, pNode->iNode);
181927 }
181928 }
181929 }
181930 if( rc==SQLITE_OK ){
181931 rc = fixBoundingBox(pRtree, pNode);
181932 }
181933 for(; rc==SQLITE_OK && ii<nCell; ii++){
181934 /* Find a node to store this cell in. pNode->iNode currently contains
181935 ** the height of the sub-tree headed by the cell.
181936 */
181937 RtreeNode *pInsert;
181938 RtreeCell *p = &aCell[aOrder[ii]];
181939 rc = ChooseLeaf(pRtree, p, iHeight, &pInsert);
181940 if( rc==SQLITE_OK ){
181941 int rc2;
181942 rc = rtreeInsertCell(pRtree, pInsert, p, iHeight);
181943 rc2 = nodeRelease(pRtree, pInsert);
181944 if( rc==SQLITE_OK ){
181945 rc = rc2;
181946 }
181947 }
181948 }
181949
181950 sqlite3_free(aCell);
181951 return rc;
181952}
181953
181954/*
181955** Insert cell pCell into node pNode. Node pNode is the head of a
181956** subtree iHeight high (leaf nodes have iHeight==0).
181957*/
181958static int rtreeInsertCell(
181959 Rtree *pRtree,
181960 RtreeNode *pNode,
181961 RtreeCell *pCell,
181962 int iHeight
181963){
181964 int rc = SQLITE_OK;
181965 if( iHeight>0 ){
181966 RtreeNode *pChild = nodeHashLookup(pRtree, pCell->iRowid);
181967 if( pChild ){
181968 nodeRelease(pRtree, pChild->pParent);
181969 nodeReference(pNode);
181970 pChild->pParent = pNode;
181971 }
181972 }
181973 if( nodeInsertCell(pRtree, pNode, pCell) ){
181974 if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){
181975 rc = SplitNode(pRtree, pNode, pCell, iHeight);
181976 }else{
181977 pRtree->iReinsertHeight = iHeight;
181978 rc = Reinsert(pRtree, pNode, pCell, iHeight);
181979 }
181980 }else{
181981 rc = AdjustTree(pRtree, pNode, pCell);
181982 if( rc==SQLITE_OK ){
181983 if( iHeight==0 ){
181984 rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode);
181985 }else{
181986 rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode);
181987 }
181988 }
181989 }
181990 return rc;
181991}
181992
181993static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){
181994 int ii;
181995 int rc = SQLITE_OK;
181996 int nCell = NCELL(pNode);
181997
181998 for(ii=0; rc==SQLITE_OK && ii<nCell; ii++){
181999 RtreeNode *pInsert;
182000 RtreeCell cell;
182001 nodeGetCell(pRtree, pNode, ii, &cell);
182002
182003 /* Find a node to store this cell in. pNode->iNode currently contains
182004 ** the height of the sub-tree headed by the cell.
182005 */
182006 rc = ChooseLeaf(pRtree, &cell, (int)pNode->iNode, &pInsert);
182007 if( rc==SQLITE_OK ){
182008 int rc2;
182009 rc = rtreeInsertCell(pRtree, pInsert, &cell, (int)pNode->iNode);
182010 rc2 = nodeRelease(pRtree, pInsert);
182011 if( rc==SQLITE_OK ){
182012 rc = rc2;
182013 }
182014 }
182015 }
182016 return rc;
182017}
182018
182019/*
182020** Select a currently unused rowid for a new r-tree record.
182021*/
182022static int rtreeNewRowid(Rtree *pRtree, i64 *piRowid){
182023 int rc;
182024 sqlite3_bind_null(pRtree->pWriteRowid, 1);
182025 sqlite3_bind_null(pRtree->pWriteRowid, 2);
182026 sqlite3_step(pRtree->pWriteRowid);
182027 rc = sqlite3_reset(pRtree->pWriteRowid);
182028 *piRowid = sqlite3_last_insert_rowid(pRtree->db);
182029 return rc;
182030}
182031
182032/*
182033** Remove the entry with rowid=iDelete from the r-tree structure.
182034*/
182035static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
182036 int rc; /* Return code */
182037 RtreeNode *pLeaf = 0; /* Leaf node containing record iDelete */
182038 int iCell; /* Index of iDelete cell in pLeaf */
182039 RtreeNode *pRoot = 0; /* Root node of rtree structure */
182040
182041
182042 /* Obtain a reference to the root node to initialize Rtree.iDepth */
182043 rc = nodeAcquire(pRtree, 1, 0, &pRoot);
182044
182045 /* Obtain a reference to the leaf node that contains the entry
182046 ** about to be deleted.
182047 */
182048 if( rc==SQLITE_OK ){
182049 rc = findLeafNode(pRtree, iDelete, &pLeaf, 0);
182050 }
182051
182052 /* Delete the cell in question from the leaf node. */
182053 if( rc==SQLITE_OK ){
182054 int rc2;
182055 rc = nodeRowidIndex(pRtree, pLeaf, iDelete, &iCell);
182056 if( rc==SQLITE_OK ){
182057 rc = deleteCell(pRtree, pLeaf, iCell, 0);
182058 }
182059 rc2 = nodeRelease(pRtree, pLeaf);
182060 if( rc==SQLITE_OK ){
182061 rc = rc2;
182062 }
182063 }
182064
182065 /* Delete the corresponding entry in the <rtree>_rowid table. */
182066 if( rc==SQLITE_OK ){
182067 sqlite3_bind_int64(pRtree->pDeleteRowid, 1, iDelete);
182068 sqlite3_step(pRtree->pDeleteRowid);
182069 rc = sqlite3_reset(pRtree->pDeleteRowid);
182070 }
182071
182072 /* Check if the root node now has exactly one child. If so, remove
182073 ** it, schedule the contents of the child for reinsertion and
182074 ** reduce the tree height by one.
182075 **
182076 ** This is equivalent to copying the contents of the child into
182077 ** the root node (the operation that Gutman's paper says to perform
182078 ** in this scenario).
182079 */
182080 if( rc==SQLITE_OK && pRtree->iDepth>0 && NCELL(pRoot)==1 ){
182081 int rc2;
182082 RtreeNode *pChild = 0;
182083 i64 iChild = nodeGetRowid(pRtree, pRoot, 0);
182084 rc = nodeAcquire(pRtree, iChild, pRoot, &pChild);
182085 if( rc==SQLITE_OK ){
182086 rc = removeNode(pRtree, pChild, pRtree->iDepth-1);
182087 }
182088 rc2 = nodeRelease(pRtree, pChild);
182089 if( rc==SQLITE_OK ) rc = rc2;
182090 if( rc==SQLITE_OK ){
182091 pRtree->iDepth--;
182092 writeInt16(pRoot->zData, pRtree->iDepth);
182093 pRoot->isDirty = 1;
182094 }
182095 }
182096
182097 /* Re-insert the contents of any underfull nodes removed from the tree. */
182098 for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
182099 if( rc==SQLITE_OK ){
182100 rc = reinsertNodeContent(pRtree, pLeaf);
182101 }
182102 pRtree->pDeleted = pLeaf->pNext;
182103 pRtree->nNodeRef--;
182104 sqlite3_free(pLeaf);
182105 }
182106
182107 /* Release the reference to the root node. */
182108 if( rc==SQLITE_OK ){
182109 rc = nodeRelease(pRtree, pRoot);
182110 }else{
182111 nodeRelease(pRtree, pRoot);
182112 }
182113
182114 return rc;
182115}
182116
182117/*
182118** Rounding constants for float->double conversion.
182119*/
182120#define RNDTOWARDS (1.0 - 1.0/8388608.0) /* Round towards zero */
182121#define RNDAWAY (1.0 + 1.0/8388608.0) /* Round away from zero */
182122
182123#if !defined(SQLITE_RTREE_INT_ONLY)
182124/*
182125** Convert an sqlite3_value into an RtreeValue (presumably a float)
182126** while taking care to round toward negative or positive, respectively.
182127*/
182128static RtreeValue rtreeValueDown(sqlite3_value *v){
182129 double d = sqlite3_value_double(v);
182130 float f = (float)d;
182131 if( f>d ){
182132 f = (float)(d*(d<0 ? RNDAWAY : RNDTOWARDS));
182133 }
182134 return f;
182135}
182136static RtreeValue rtreeValueUp(sqlite3_value *v){
182137 double d = sqlite3_value_double(v);
182138 float f = (float)d;
182139 if( f<d ){
182140 f = (float)(d*(d<0 ? RNDTOWARDS : RNDAWAY));
182141 }
182142 return f;
182143}
182144#endif /* !defined(SQLITE_RTREE_INT_ONLY) */
182145
182146/*
182147** A constraint has failed while inserting a row into an rtree table.
182148** Assuming no OOM error occurs, this function sets the error message
182149** (at pRtree->base.zErrMsg) to an appropriate value and returns
182150** SQLITE_CONSTRAINT.
182151**
182152** Parameter iCol is the index of the leftmost column involved in the
182153** constraint failure. If it is 0, then the constraint that failed is
182154** the unique constraint on the id column. Otherwise, it is the rtree
182155** (c1<=c2) constraint on columns iCol and iCol+1 that has failed.
182156**
182157** If an OOM occurs, SQLITE_NOMEM is returned instead of SQLITE_CONSTRAINT.
182158*/
182159static int rtreeConstraintError(Rtree *pRtree, int iCol){
182160 sqlite3_stmt *pStmt = 0;
182161 char *zSql;
182162 int rc;
182163
182164 assert( iCol==0 || iCol%2 );
182165 zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", pRtree->zDb, pRtree->zName);
182166 if( zSql ){
182167 rc = sqlite3_prepare_v2(pRtree->db, zSql, -1, &pStmt, 0);
182168 }else{
182169 rc = SQLITE_NOMEM;
182170 }
182171 sqlite3_free(zSql);
182172
182173 if( rc==SQLITE_OK ){
182174 if( iCol==0 ){
182175 const char *zCol = sqlite3_column_name(pStmt, 0);
182176 pRtree->base.zErrMsg = sqlite3_mprintf(
182177 "UNIQUE constraint failed: %s.%s", pRtree->zName, zCol
182178 );
182179 }else{
182180 const char *zCol1 = sqlite3_column_name(pStmt, iCol);
182181 const char *zCol2 = sqlite3_column_name(pStmt, iCol+1);
182182 pRtree->base.zErrMsg = sqlite3_mprintf(
182183 "rtree constraint failed: %s.(%s<=%s)", pRtree->zName, zCol1, zCol2
182184 );
182185 }
182186 }
182187
182188 sqlite3_finalize(pStmt);
182189 return (rc==SQLITE_OK ? SQLITE_CONSTRAINT : rc);
182190}
182191
182192
182193
182194/*
182195** The xUpdate method for rtree module virtual tables.
182196*/
182197static int rtreeUpdate(
182198 sqlite3_vtab *pVtab,
182199 int nData,
182200 sqlite3_value **aData,
182201 sqlite_int64 *pRowid
182202){
182203 Rtree *pRtree = (Rtree *)pVtab;
182204 int rc = SQLITE_OK;
182205 RtreeCell cell; /* New cell to insert if nData>1 */
182206 int bHaveRowid = 0; /* Set to 1 after new rowid is determined */
182207
182208 if( pRtree->nNodeRef ){
182209 /* Unable to write to the btree while another cursor is reading from it,
182210 ** since the write might do a rebalance which would disrupt the read
182211 ** cursor. */
182212 return SQLITE_LOCKED_VTAB;
182213 }
182214 rtreeReference(pRtree);
182215 assert(nData>=1);
182216
182217 cell.iRowid = 0; /* Used only to suppress a compiler warning */
182218
182219 /* Constraint handling. A write operation on an r-tree table may return
182220 ** SQLITE_CONSTRAINT for two reasons:
182221 **
182222 ** 1. A duplicate rowid value, or
182223 ** 2. The supplied data violates the "x2>=x1" constraint.
182224 **
182225 ** In the first case, if the conflict-handling mode is REPLACE, then
182226 ** the conflicting row can be removed before proceeding. In the second
182227 ** case, SQLITE_CONSTRAINT must be returned regardless of the
182228 ** conflict-handling mode specified by the user.
182229 */
182230 if( nData>1 ){
182231 int ii;
182232 int nn = nData - 4;
182233
182234 if( nn > pRtree->nDim2 ) nn = pRtree->nDim2;
182235 /* Populate the cell.aCoord[] array. The first coordinate is aData[3].
182236 **
182237 ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
182238 ** with "column" that are interpreted as table constraints.
182239 ** Example: CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
182240 ** This problem was discovered after years of use, so we silently ignore
182241 ** these kinds of misdeclared tables to avoid breaking any legacy.
182242 */
182243
182244#ifndef SQLITE_RTREE_INT_ONLY
182245 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
182246 for(ii=0; ii<nn; ii+=2){
182247 cell.aCoord[ii].f = rtreeValueDown(aData[ii+3]);
182248 cell.aCoord[ii+1].f = rtreeValueUp(aData[ii+4]);
182249 if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
182250 rc = rtreeConstraintError(pRtree, ii+1);
182251 goto constraint;
182252 }
182253 }
182254 }else
182255#endif
182256 {
182257 for(ii=0; ii<nn; ii+=2){
182258 cell.aCoord[ii].i = sqlite3_value_int(aData[ii+3]);
182259 cell.aCoord[ii+1].i = sqlite3_value_int(aData[ii+4]);
182260 if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
182261 rc = rtreeConstraintError(pRtree, ii+1);
182262 goto constraint;
182263 }
182264 }
182265 }
182266
182267 /* If a rowid value was supplied, check if it is already present in
182268 ** the table. If so, the constraint has failed. */
182269 if( sqlite3_value_type(aData[2])!=SQLITE_NULL ){
182270 cell.iRowid = sqlite3_value_int64(aData[2]);
182271 if( sqlite3_value_type(aData[0])==SQLITE_NULL
182272 || sqlite3_value_int64(aData[0])!=cell.iRowid
182273 ){
182274 int steprc;
182275 sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
182276 steprc = sqlite3_step(pRtree->pReadRowid);
182277 rc = sqlite3_reset(pRtree->pReadRowid);
182278 if( SQLITE_ROW==steprc ){
182279 if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){
182280 rc = rtreeDeleteRowid(pRtree, cell.iRowid);
182281 }else{
182282 rc = rtreeConstraintError(pRtree, 0);
182283 goto constraint;
182284 }
182285 }
182286 }
182287 bHaveRowid = 1;
182288 }
182289 }
182290
182291 /* If aData[0] is not an SQL NULL value, it is the rowid of a
182292 ** record to delete from the r-tree table. The following block does
182293 ** just that.
182294 */
182295 if( sqlite3_value_type(aData[0])!=SQLITE_NULL ){
182296 rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(aData[0]));
182297 }
182298
182299 /* If the aData[] array contains more than one element, elements
182300 ** (aData[2]..aData[argc-1]) contain a new record to insert into
182301 ** the r-tree structure.
182302 */
182303 if( rc==SQLITE_OK && nData>1 ){
182304 /* Insert the new record into the r-tree */
182305 RtreeNode *pLeaf = 0;
182306
182307 /* Figure out the rowid of the new row. */
182308 if( bHaveRowid==0 ){
182309 rc = rtreeNewRowid(pRtree, &cell.iRowid);
182310 }
182311 *pRowid = cell.iRowid;
182312
182313 if( rc==SQLITE_OK ){
182314 rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
182315 }
182316 if( rc==SQLITE_OK ){
182317 int rc2;
182318 pRtree->iReinsertHeight = -1;
182319 rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
182320 rc2 = nodeRelease(pRtree, pLeaf);
182321 if( rc==SQLITE_OK ){
182322 rc = rc2;
182323 }
182324 }
182325 if( pRtree->nAux ){
182326 sqlite3_stmt *pUp = pRtree->pWriteAux;
182327 int jj;
182328 sqlite3_bind_int64(pUp, 1, *pRowid);
182329 for(jj=0; jj<pRtree->nAux; jj++){
182330 sqlite3_bind_value(pUp, jj+2, aData[pRtree->nDim2+3+jj]);
182331 }
182332 sqlite3_step(pUp);
182333 rc = sqlite3_reset(pUp);
182334 }
182335 }
182336
182337constraint:
182338 rtreeRelease(pRtree);
182339 return rc;
182340}
182341
182342/*
182343** Called when a transaction starts.
182344*/
182345static int rtreeBeginTransaction(sqlite3_vtab *pVtab){
182346 Rtree *pRtree = (Rtree *)pVtab;
182347 assert( pRtree->inWrTrans==0 );
182348 pRtree->inWrTrans++;
182349 return SQLITE_OK;
182350}
182351
182352/*
182353** Called when a transaction completes (either by COMMIT or ROLLBACK).
182354** The sqlite3_blob object should be released at this point.
182355*/
182356static int rtreeEndTransaction(sqlite3_vtab *pVtab){
182357 Rtree *pRtree = (Rtree *)pVtab;
182358 pRtree->inWrTrans = 0;
182359 nodeBlobReset(pRtree);
182360 return SQLITE_OK;
182361}
182362
182363/*
182364** The xRename method for rtree module virtual tables.
182365*/
182366static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
182367 Rtree *pRtree = (Rtree *)pVtab;
182368 int rc = SQLITE_NOMEM;
182369 char *zSql = sqlite3_mprintf(
182370 "ALTER TABLE %Q.'%q_node' RENAME TO \"%w_node\";"
182371 "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";"
182372 "ALTER TABLE %Q.'%q_rowid' RENAME TO \"%w_rowid\";"
182373 , pRtree->zDb, pRtree->zName, zNewName
182374 , pRtree->zDb, pRtree->zName, zNewName
182375 , pRtree->zDb, pRtree->zName, zNewName
182376 );
182377 if( zSql ){
182378 nodeBlobReset(pRtree);
182379 rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
182380 sqlite3_free(zSql);
182381 }
182382 return rc;
182383}
182384
182385/*
182386** The xSavepoint method.
182387**
182388** This module does not need to do anything to support savepoints. However,
182389** it uses this hook to close any open blob handle. This is done because a
182390** DROP TABLE command - which fortunately always opens a savepoint - cannot
182391** succeed if there are any open blob handles. i.e. if the blob handle were
182392** not closed here, the following would fail:
182393**
182394** BEGIN;
182395** INSERT INTO rtree...
182396** DROP TABLE <tablename>; -- Would fail with SQLITE_LOCKED
182397** COMMIT;
182398*/
182399static int rtreeSavepoint(sqlite3_vtab *pVtab, int iSavepoint){
182400 Rtree *pRtree = (Rtree *)pVtab;
182401 u8 iwt = pRtree->inWrTrans;
182402 UNUSED_PARAMETER(iSavepoint);
182403 pRtree->inWrTrans = 0;
182404 nodeBlobReset(pRtree);
182405 pRtree->inWrTrans = iwt;
182406 return SQLITE_OK;
182407}
182408
182409/*
182410** This function populates the pRtree->nRowEst variable with an estimate
182411** of the number of rows in the virtual table. If possible, this is based
182412** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST.
182413*/
182414static int rtreeQueryStat1(sqlite3 *db, Rtree *pRtree){
182415 const char *zFmt = "SELECT stat FROM %Q.sqlite_stat1 WHERE tbl = '%q_rowid'";
182416 char *zSql;
182417 sqlite3_stmt *p;
182418 int rc;
182419 i64 nRow = 0;
182420
182421 rc = sqlite3_table_column_metadata(
182422 db, pRtree->zDb, "sqlite_stat1",0,0,0,0,0,0
182423 );
182424 if( rc!=SQLITE_OK ){
182425 pRtree->nRowEst = RTREE_DEFAULT_ROWEST;
182426 return rc==SQLITE_ERROR ? SQLITE_OK : rc;
182427 }
182428 zSql = sqlite3_mprintf(zFmt, pRtree->zDb, pRtree->zName);
182429 if( zSql==0 ){
182430 rc = SQLITE_NOMEM;
182431 }else{
182432 rc = sqlite3_prepare_v2(db, zSql, -1, &p, 0);
182433 if( rc==SQLITE_OK ){
182434 if( sqlite3_step(p)==SQLITE_ROW ) nRow = sqlite3_column_int64(p, 0);
182435 rc = sqlite3_finalize(p);
182436 }else if( rc!=SQLITE_NOMEM ){
182437 rc = SQLITE_OK;
182438 }
182439
182440 if( rc==SQLITE_OK ){
182441 if( nRow==0 ){
182442 pRtree->nRowEst = RTREE_DEFAULT_ROWEST;
182443 }else{
182444 pRtree->nRowEst = MAX(nRow, RTREE_MIN_ROWEST);
182445 }
182446 }
182447 sqlite3_free(zSql);
182448 }
182449
182450 return rc;
182451}
182452
182453
182454/*
182455** Return true if zName is the extension on one of the shadow tables used
182456** by this module.
182457*/
182458static int rtreeShadowName(const char *zName){
182459 static const char *azName[] = {
182460 "node", "parent", "rowid"
182461 };
182462 unsigned int i;
182463 for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
182464 if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
182465 }
182466 return 0;
182467}
182468
182469static sqlite3_module rtreeModule = {
182470 3, /* iVersion */
182471 rtreeCreate, /* xCreate - create a table */
182472 rtreeConnect, /* xConnect - connect to an existing table */
182473 rtreeBestIndex, /* xBestIndex - Determine search strategy */
182474 rtreeDisconnect, /* xDisconnect - Disconnect from a table */
182475 rtreeDestroy, /* xDestroy - Drop a table */
182476 rtreeOpen, /* xOpen - open a cursor */
182477 rtreeClose, /* xClose - close a cursor */
182478 rtreeFilter, /* xFilter - configure scan constraints */
182479 rtreeNext, /* xNext - advance a cursor */
182480 rtreeEof, /* xEof */
182481 rtreeColumn, /* xColumn - read data */
182482 rtreeRowid, /* xRowid - read data */
182483 rtreeUpdate, /* xUpdate - write data */
182484 rtreeBeginTransaction, /* xBegin - begin transaction */
182485 rtreeEndTransaction, /* xSync - sync transaction */
182486 rtreeEndTransaction, /* xCommit - commit transaction */
182487 rtreeEndTransaction, /* xRollback - rollback transaction */
182488 0, /* xFindFunction - function overloading */
182489 rtreeRename, /* xRename - rename the table */
182490 rtreeSavepoint, /* xSavepoint */
182491 0, /* xRelease */
182492 0, /* xRollbackTo */
182493 rtreeShadowName /* xShadowName */
182494};
182495
182496static int rtreeSqlInit(
182497 Rtree *pRtree,
182498 sqlite3 *db,
182499 const char *zDb,
182500 const char *zPrefix,
182501 int isCreate
182502){
182503 int rc = SQLITE_OK;
182504
182505 #define N_STATEMENT 8
182506 static const char *azSql[N_STATEMENT] = {
182507 /* Write the xxx_node table */
182508 "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(?1, ?2)",
182509 "DELETE FROM '%q'.'%q_node' WHERE nodeno = ?1",
182510
182511 /* Read and write the xxx_rowid table */
182512 "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = ?1",
182513 "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(?1, ?2)",
182514 "DELETE FROM '%q'.'%q_rowid' WHERE rowid = ?1",
182515
182516 /* Read and write the xxx_parent table */
182517 "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = ?1",
182518 "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(?1, ?2)",
182519 "DELETE FROM '%q'.'%q_parent' WHERE nodeno = ?1"
182520 };
182521 sqlite3_stmt **appStmt[N_STATEMENT];
182522 int i;
182523
182524 pRtree->db = db;
182525
182526 if( isCreate ){
182527 char *zCreate;
182528 sqlite3_str *p = sqlite3_str_new(db);
182529 int ii;
182530 sqlite3_str_appendf(p,
182531 "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY,nodeno",
182532 zDb, zPrefix);
182533 for(ii=0; ii<pRtree->nAux; ii++){
182534 sqlite3_str_appendf(p,",a%d",ii);
182535 }
182536 sqlite3_str_appendf(p,
182537 ");CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY,data);",
182538 zDb, zPrefix);
182539 sqlite3_str_appendf(p,
182540 "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY,parentnode);",
182541 zDb, zPrefix);
182542 sqlite3_str_appendf(p,
182543 "INSERT INTO \"%w\".\"%w_node\"VALUES(1,zeroblob(%d))",
182544 zDb, zPrefix, pRtree->iNodeSize);
182545 zCreate = sqlite3_str_finish(p);
182546 if( !zCreate ){
182547 return SQLITE_NOMEM;
182548 }
182549 rc = sqlite3_exec(db, zCreate, 0, 0, 0);
182550 sqlite3_free(zCreate);
182551 if( rc!=SQLITE_OK ){
182552 return rc;
182553 }
182554 }
182555
182556 appStmt[0] = &pRtree->pWriteNode;
182557 appStmt[1] = &pRtree->pDeleteNode;
182558 appStmt[2] = &pRtree->pReadRowid;
182559 appStmt[3] = &pRtree->pWriteRowid;
182560 appStmt[4] = &pRtree->pDeleteRowid;
182561 appStmt[5] = &pRtree->pReadParent;
182562 appStmt[6] = &pRtree->pWriteParent;
182563 appStmt[7] = &pRtree->pDeleteParent;
182564
182565 rc = rtreeQueryStat1(db, pRtree);
182566 for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
182567 char *zSql;
182568 const char *zFormat;
182569 if( i!=3 || pRtree->nAux==0 ){
182570 zFormat = azSql[i];
182571 }else {
182572 /* An UPSERT is very slightly slower than REPLACE, but it is needed
182573 ** if there are auxiliary columns */
182574 zFormat = "INSERT INTO\"%w\".\"%w_rowid\"(rowid,nodeno)VALUES(?1,?2)"
182575 "ON CONFLICT(rowid)DO UPDATE SET nodeno=excluded.nodeno";
182576 }
182577 zSql = sqlite3_mprintf(zFormat, zDb, zPrefix);
182578 if( zSql ){
182579 rc = sqlite3_prepare_v3(db, zSql, -1, SQLITE_PREPARE_PERSISTENT,
182580 appStmt[i], 0);
182581 }else{
182582 rc = SQLITE_NOMEM;
182583 }
182584 sqlite3_free(zSql);
182585 }
182586 if( pRtree->nAux ){
182587 pRtree->zReadAuxSql = sqlite3_mprintf(
182588 "SELECT * FROM \"%w\".\"%w_rowid\" WHERE rowid=?1",
182589 zDb, zPrefix);
182590 if( pRtree->zReadAuxSql==0 ){
182591 rc = SQLITE_NOMEM;
182592 }else{
182593 sqlite3_str *p = sqlite3_str_new(db);
182594 int ii;
182595 char *zSql;
182596 sqlite3_str_appendf(p, "UPDATE \"%w\".\"%w_rowid\"SET ", zDb, zPrefix);
182597 for(ii=0; ii<pRtree->nAux; ii++){
182598 if( ii ) sqlite3_str_append(p, ",", 1);
182599 if( ii<pRtree->nAuxNotNull ){
182600 sqlite3_str_appendf(p,"a%d=coalesce(?%d,a%d)",ii,ii+2,ii);
182601 }else{
182602 sqlite3_str_appendf(p,"a%d=?%d",ii,ii+2);
182603 }
182604 }
182605 sqlite3_str_appendf(p, " WHERE rowid=?1");
182606 zSql = sqlite3_str_finish(p);
182607 if( zSql==0 ){
182608 rc = SQLITE_NOMEM;
182609 }else{
182610 rc = sqlite3_prepare_v3(db, zSql, -1, SQLITE_PREPARE_PERSISTENT,
182611 &pRtree->pWriteAux, 0);
182612 sqlite3_free(zSql);
182613 }
182614 }
182615 }
182616
182617 return rc;
182618}
182619
182620/*
182621** The second argument to this function contains the text of an SQL statement
182622** that returns a single integer value. The statement is compiled and executed
182623** using database connection db. If successful, the integer value returned
182624** is written to *piVal and SQLITE_OK returned. Otherwise, an SQLite error
182625** code is returned and the value of *piVal after returning is not defined.
182626*/
182627static int getIntFromStmt(sqlite3 *db, const char *zSql, int *piVal){
182628 int rc = SQLITE_NOMEM;
182629 if( zSql ){
182630 sqlite3_stmt *pStmt = 0;
182631 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
182632 if( rc==SQLITE_OK ){
182633 if( SQLITE_ROW==sqlite3_step(pStmt) ){
182634 *piVal = sqlite3_column_int(pStmt, 0);
182635 }
182636 rc = sqlite3_finalize(pStmt);
182637 }
182638 }
182639 return rc;
182640}
182641
182642/*
182643** This function is called from within the xConnect() or xCreate() method to
182644** determine the node-size used by the rtree table being created or connected
182645** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned.
182646** Otherwise, an SQLite error code is returned.
182647**
182648** If this function is being called as part of an xConnect(), then the rtree
182649** table already exists. In this case the node-size is determined by inspecting
182650** the root node of the tree.
182651**
182652** Otherwise, for an xCreate(), use 64 bytes less than the database page-size.
182653** This ensures that each node is stored on a single database page. If the
182654** database page-size is so large that more than RTREE_MAXCELLS entries
182655** would fit in a single node, use a smaller node-size.
182656*/
182657static int getNodeSize(
182658 sqlite3 *db, /* Database handle */
182659 Rtree *pRtree, /* Rtree handle */
182660 int isCreate, /* True for xCreate, false for xConnect */
182661 char **pzErr /* OUT: Error message, if any */
182662){
182663 int rc;
182664 char *zSql;
182665 if( isCreate ){
182666 int iPageSize = 0;
182667 zSql = sqlite3_mprintf("PRAGMA %Q.page_size", pRtree->zDb);
182668 rc = getIntFromStmt(db, zSql, &iPageSize);
182669 if( rc==SQLITE_OK ){
182670 pRtree->iNodeSize = iPageSize-64;
182671 if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)<pRtree->iNodeSize ){
182672 pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS;
182673 }
182674 }else{
182675 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
182676 }
182677 }else{
182678 zSql = sqlite3_mprintf(
182679 "SELECT length(data) FROM '%q'.'%q_node' WHERE nodeno = 1",
182680 pRtree->zDb, pRtree->zName
182681 );
182682 rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize);
182683 if( rc!=SQLITE_OK ){
182684 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
182685 }else if( pRtree->iNodeSize<(512-64) ){
182686 rc = SQLITE_CORRUPT_VTAB;
182687 *pzErr = sqlite3_mprintf("undersize RTree blobs in \"%q_node\"",
182688 pRtree->zName);
182689 }
182690 }
182691
182692 sqlite3_free(zSql);
182693 return rc;
182694}
182695
182696/*
182697** This function is the implementation of both the xConnect and xCreate
182698** methods of the r-tree virtual table.
182699**
182700** argv[0] -> module name
182701** argv[1] -> database name
182702** argv[2] -> table name
182703** argv[...] -> column names...
182704*/
182705static int rtreeInit(
182706 sqlite3 *db, /* Database connection */
182707 void *pAux, /* One of the RTREE_COORD_* constants */
182708 int argc, const char *const*argv, /* Parameters to CREATE TABLE statement */
182709 sqlite3_vtab **ppVtab, /* OUT: New virtual table */
182710 char **pzErr, /* OUT: Error message, if any */
182711 int isCreate /* True for xCreate, false for xConnect */
182712){
182713 int rc = SQLITE_OK;
182714 Rtree *pRtree;
182715 int nDb; /* Length of string argv[1] */
182716 int nName; /* Length of string argv[2] */
182717 int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32);
182718 sqlite3_str *pSql;
182719 char *zSql;
182720 int ii = 4;
182721 int iErr;
182722
182723 const char *aErrMsg[] = {
182724 0, /* 0 */
182725 "Wrong number of columns for an rtree table", /* 1 */
182726 "Too few columns for an rtree table", /* 2 */
182727 "Too many columns for an rtree table", /* 3 */
182728 "Auxiliary rtree columns must be last" /* 4 */
182729 };
182730
182731 assert( RTREE_MAX_AUX_COLUMN<256 ); /* Aux columns counted by a u8 */
182732 if( argc>RTREE_MAX_AUX_COLUMN+3 ){
182733 *pzErr = sqlite3_mprintf("%s", aErrMsg[3]);
182734 return SQLITE_ERROR;
182735 }
182736
182737 sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
182738
182739 /* Allocate the sqlite3_vtab structure */
182740 nDb = (int)strlen(argv[1]);
182741 nName = (int)strlen(argv[2]);
182742 pRtree = (Rtree *)sqlite3_malloc(sizeof(Rtree)+nDb+nName+2);
182743 if( !pRtree ){
182744 return SQLITE_NOMEM;
182745 }
182746 memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
182747 pRtree->nBusy = 1;
182748 pRtree->base.pModule = &rtreeModule;
182749 pRtree->zDb = (char *)&pRtree[1];
182750 pRtree->zName = &pRtree->zDb[nDb+1];
182751 pRtree->eCoordType = (u8)eCoordType;
182752 memcpy(pRtree->zDb, argv[1], nDb);
182753 memcpy(pRtree->zName, argv[2], nName);
182754
182755
182756 /* Create/Connect to the underlying relational database schema. If
182757 ** that is successful, call sqlite3_declare_vtab() to configure
182758 ** the r-tree table schema.
182759 */
182760 pSql = sqlite3_str_new(db);
182761 sqlite3_str_appendf(pSql, "CREATE TABLE x(%s", argv[3]);
182762 for(ii=4; ii<argc; ii++){
182763 if( argv[ii][0]=='+' ){
182764 pRtree->nAux++;
182765 sqlite3_str_appendf(pSql, ",%s", argv[ii]+1);
182766 }else if( pRtree->nAux>0 ){
182767 break;
182768 }else{
182769 pRtree->nDim2++;
182770 sqlite3_str_appendf(pSql, ",%s", argv[ii]);
182771 }
182772 }
182773 sqlite3_str_appendf(pSql, ");");
182774 zSql = sqlite3_str_finish(pSql);
182775 if( !zSql ){
182776 rc = SQLITE_NOMEM;
182777 }else if( ii<argc ){
182778 *pzErr = sqlite3_mprintf("%s", aErrMsg[4]);
182779 rc = SQLITE_ERROR;
182780 }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
182781 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
182782 }
182783 sqlite3_free(zSql);
182784 if( rc ) goto rtreeInit_fail;
182785 pRtree->nDim = pRtree->nDim2/2;
182786 if( pRtree->nDim<1 ){
182787 iErr = 2;
182788 }else if( pRtree->nDim2>RTREE_MAX_DIMENSIONS*2 ){
182789 iErr = 3;
182790 }else if( pRtree->nDim2 % 2 ){
182791 iErr = 1;
182792 }else{
182793 iErr = 0;
182794 }
182795 if( iErr ){
182796 *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
182797 goto rtreeInit_fail;
182798 }
182799 pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
182800
182801 /* Figure out the node size to use. */
182802 rc = getNodeSize(db, pRtree, isCreate, pzErr);
182803 if( rc ) goto rtreeInit_fail;
182804 rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate);
182805 if( rc ){
182806 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
182807 goto rtreeInit_fail;
182808 }
182809
182810 *ppVtab = (sqlite3_vtab *)pRtree;
182811 return SQLITE_OK;
182812
182813rtreeInit_fail:
182814 if( rc==SQLITE_OK ) rc = SQLITE_ERROR;
182815 assert( *ppVtab==0 );
182816 assert( pRtree->nBusy==1 );
182817 rtreeRelease(pRtree);
182818 return rc;
182819}
182820
182821
182822/*
182823** Implementation of a scalar function that decodes r-tree nodes to
182824** human readable strings. This can be used for debugging and analysis.
182825**
182826** The scalar function takes two arguments: (1) the number of dimensions
182827** to the rtree (between 1 and 5, inclusive) and (2) a blob of data containing
182828** an r-tree node. For a two-dimensional r-tree structure called "rt", to
182829** deserialize all nodes, a statement like:
182830**
182831** SELECT rtreenode(2, data) FROM rt_node;
182832**
182833** The human readable string takes the form of a Tcl list with one
182834** entry for each cell in the r-tree node. Each entry is itself a
182835** list, containing the 8-byte rowid/pageno followed by the
182836** <num-dimension>*2 coordinates.
182837*/
182838static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
182839 char *zText = 0;
182840 RtreeNode node;
182841 Rtree tree;
182842 int ii;
182843
182844 UNUSED_PARAMETER(nArg);
182845 memset(&node, 0, sizeof(RtreeNode));
182846 memset(&tree, 0, sizeof(Rtree));
182847 tree.nDim = (u8)sqlite3_value_int(apArg[0]);
182848 tree.nDim2 = tree.nDim*2;
182849 tree.nBytesPerCell = 8 + 8 * tree.nDim;
182850 node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
182851
182852 for(ii=0; ii<NCELL(&node); ii++){
182853 char zCell[512];
182854 int nCell = 0;
182855 RtreeCell cell;
182856 int jj;
182857
182858 nodeGetCell(&tree, &node, ii, &cell);
182859 sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
182860 nCell = (int)strlen(zCell);
182861 for(jj=0; jj<tree.nDim2; jj++){
182862#ifndef SQLITE_RTREE_INT_ONLY
182863 sqlite3_snprintf(512-nCell,&zCell[nCell], " %g",
182864 (double)cell.aCoord[jj].f);
182865#else
182866 sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
182867 cell.aCoord[jj].i);
182868#endif
182869 nCell = (int)strlen(zCell);
182870 }
182871
182872 if( zText ){
182873 char *zTextNew = sqlite3_mprintf("%s {%s}", zText, zCell);
182874 sqlite3_free(zText);
182875 zText = zTextNew;
182876 }else{
182877 zText = sqlite3_mprintf("{%s}", zCell);
182878 }
182879 }
182880
182881 sqlite3_result_text(ctx, zText, -1, sqlite3_free);
182882}
182883
182884/* This routine implements an SQL function that returns the "depth" parameter
182885** from the front of a blob that is an r-tree node. For example:
182886**
182887** SELECT rtreedepth(data) FROM rt_node WHERE nodeno=1;
182888**
182889** The depth value is 0 for all nodes other than the root node, and the root
182890** node always has nodeno=1, so the example above is the primary use for this
182891** routine. This routine is intended for testing and analysis only.
182892*/
182893static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
182894 UNUSED_PARAMETER(nArg);
182895 if( sqlite3_value_type(apArg[0])!=SQLITE_BLOB
182896 || sqlite3_value_bytes(apArg[0])<2
182897 ){
182898 sqlite3_result_error(ctx, "Invalid argument to rtreedepth()", -1);
182899 }else{
182900 u8 *zBlob = (u8 *)sqlite3_value_blob(apArg[0]);
182901 sqlite3_result_int(ctx, readInt16(zBlob));
182902 }
182903}
182904
182905/*
182906** Context object passed between the various routines that make up the
182907** implementation of integrity-check function rtreecheck().
182908*/
182909typedef struct RtreeCheck RtreeCheck;
182910struct RtreeCheck {
182911 sqlite3 *db; /* Database handle */
182912 const char *zDb; /* Database containing rtree table */
182913 const char *zTab; /* Name of rtree table */
182914 int bInt; /* True for rtree_i32 table */
182915 int nDim; /* Number of dimensions for this rtree tbl */
182916 sqlite3_stmt *pGetNode; /* Statement used to retrieve nodes */
182917 sqlite3_stmt *aCheckMapping[2]; /* Statements to query %_parent/%_rowid */
182918 int nLeaf; /* Number of leaf cells in table */
182919 int nNonLeaf; /* Number of non-leaf cells in table */
182920 int rc; /* Return code */
182921 char *zReport; /* Message to report */
182922 int nErr; /* Number of lines in zReport */
182923};
182924
182925#define RTREE_CHECK_MAX_ERROR 100
182926
182927/*
182928** Reset SQL statement pStmt. If the sqlite3_reset() call returns an error,
182929** and RtreeCheck.rc==SQLITE_OK, set RtreeCheck.rc to the error code.
182930*/
182931static void rtreeCheckReset(RtreeCheck *pCheck, sqlite3_stmt *pStmt){
182932 int rc = sqlite3_reset(pStmt);
182933 if( pCheck->rc==SQLITE_OK ) pCheck->rc = rc;
182934}
182935
182936/*
182937** The second and subsequent arguments to this function are a format string
182938** and printf style arguments. This function formats the string and attempts
182939** to compile it as an SQL statement.
182940**
182941** If successful, a pointer to the new SQL statement is returned. Otherwise,
182942** NULL is returned and an error code left in RtreeCheck.rc.
182943*/
182944static sqlite3_stmt *rtreeCheckPrepare(
182945 RtreeCheck *pCheck, /* RtreeCheck object */
182946 const char *zFmt, ... /* Format string and trailing args */
182947){
182948 va_list ap;
182949 char *z;
182950 sqlite3_stmt *pRet = 0;
182951
182952 va_start(ap, zFmt);
182953 z = sqlite3_vmprintf(zFmt, ap);
182954
182955 if( pCheck->rc==SQLITE_OK ){
182956 if( z==0 ){
182957 pCheck->rc = SQLITE_NOMEM;
182958 }else{
182959 pCheck->rc = sqlite3_prepare_v2(pCheck->db, z, -1, &pRet, 0);
182960 }
182961 }
182962
182963 sqlite3_free(z);
182964 va_end(ap);
182965 return pRet;
182966}
182967
182968/*
182969** The second and subsequent arguments to this function are a printf()
182970** style format string and arguments. This function formats the string and
182971** appends it to the report being accumuated in pCheck.
182972*/
182973static void rtreeCheckAppendMsg(RtreeCheck *pCheck, const char *zFmt, ...){
182974 va_list ap;
182975 va_start(ap, zFmt);
182976 if( pCheck->rc==SQLITE_OK && pCheck->nErr<RTREE_CHECK_MAX_ERROR ){
182977 char *z = sqlite3_vmprintf(zFmt, ap);
182978 if( z==0 ){
182979 pCheck->rc = SQLITE_NOMEM;
182980 }else{
182981 pCheck->zReport = sqlite3_mprintf("%z%s%z",
182982 pCheck->zReport, (pCheck->zReport ? "\n" : ""), z
182983 );
182984 if( pCheck->zReport==0 ){
182985 pCheck->rc = SQLITE_NOMEM;
182986 }
182987 }
182988 pCheck->nErr++;
182989 }
182990 va_end(ap);
182991}
182992
182993/*
182994** This function is a no-op if there is already an error code stored
182995** in the RtreeCheck object indicated by the first argument. NULL is
182996** returned in this case.
182997**
182998** Otherwise, the contents of rtree table node iNode are loaded from
182999** the database and copied into a buffer obtained from sqlite3_malloc().
183000** If no error occurs, a pointer to the buffer is returned and (*pnNode)
183001** is set to the size of the buffer in bytes.
183002**
183003** Or, if an error does occur, NULL is returned and an error code left
183004** in the RtreeCheck object. The final value of *pnNode is undefined in
183005** this case.
183006*/
183007static u8 *rtreeCheckGetNode(RtreeCheck *pCheck, i64 iNode, int *pnNode){
183008 u8 *pRet = 0; /* Return value */
183009
183010 assert( pCheck->rc==SQLITE_OK );
183011 if( pCheck->pGetNode==0 ){
183012 pCheck->pGetNode = rtreeCheckPrepare(pCheck,
183013 "SELECT data FROM %Q.'%q_node' WHERE nodeno=?",
183014 pCheck->zDb, pCheck->zTab
183015 );
183016 }
183017
183018 if( pCheck->rc==SQLITE_OK ){
183019 sqlite3_bind_int64(pCheck->pGetNode, 1, iNode);
183020 if( sqlite3_step(pCheck->pGetNode)==SQLITE_ROW ){
183021 int nNode = sqlite3_column_bytes(pCheck->pGetNode, 0);
183022 const u8 *pNode = (const u8*)sqlite3_column_blob(pCheck->pGetNode, 0);
183023 pRet = sqlite3_malloc(nNode);
183024 if( pRet==0 ){
183025 pCheck->rc = SQLITE_NOMEM;
183026 }else{
183027 memcpy(pRet, pNode, nNode);
183028 *pnNode = nNode;
183029 }
183030 }
183031 rtreeCheckReset(pCheck, pCheck->pGetNode);
183032 if( pCheck->rc==SQLITE_OK && pRet==0 ){
183033 rtreeCheckAppendMsg(pCheck, "Node %lld missing from database", iNode);
183034 }
183035 }
183036
183037 return pRet;
183038}
183039
183040/*
183041** This function is used to check that the %_parent (if bLeaf==0) or %_rowid
183042** (if bLeaf==1) table contains a specified entry. The schemas of the
183043** two tables are:
183044**
183045** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
183046** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
183047**
183048** In both cases, this function checks that there exists an entry with
183049** IPK value iKey and the second column set to iVal.
183050**
183051*/
183052static void rtreeCheckMapping(
183053 RtreeCheck *pCheck, /* RtreeCheck object */
183054 int bLeaf, /* True for a leaf cell, false for interior */
183055 i64 iKey, /* Key for mapping */
183056 i64 iVal /* Expected value for mapping */
183057){
183058 int rc;
183059 sqlite3_stmt *pStmt;
183060 const char *azSql[2] = {
183061 "SELECT parentnode FROM %Q.'%q_parent' WHERE nodeno=?1",
183062 "SELECT nodeno FROM %Q.'%q_rowid' WHERE rowid=?1"
183063 };
183064
183065 assert( bLeaf==0 || bLeaf==1 );
183066 if( pCheck->aCheckMapping[bLeaf]==0 ){
183067 pCheck->aCheckMapping[bLeaf] = rtreeCheckPrepare(pCheck,
183068 azSql[bLeaf], pCheck->zDb, pCheck->zTab
183069 );
183070 }
183071 if( pCheck->rc!=SQLITE_OK ) return;
183072
183073 pStmt = pCheck->aCheckMapping[bLeaf];
183074 sqlite3_bind_int64(pStmt, 1, iKey);
183075 rc = sqlite3_step(pStmt);
183076 if( rc==SQLITE_DONE ){
183077 rtreeCheckAppendMsg(pCheck, "Mapping (%lld -> %lld) missing from %s table",
183078 iKey, iVal, (bLeaf ? "%_rowid" : "%_parent")
183079 );
183080 }else if( rc==SQLITE_ROW ){
183081 i64 ii = sqlite3_column_int64(pStmt, 0);
183082 if( ii!=iVal ){
183083 rtreeCheckAppendMsg(pCheck,
183084 "Found (%lld -> %lld) in %s table, expected (%lld -> %lld)",
183085 iKey, ii, (bLeaf ? "%_rowid" : "%_parent"), iKey, iVal
183086 );
183087 }
183088 }
183089 rtreeCheckReset(pCheck, pStmt);
183090}
183091
183092/*
183093** Argument pCell points to an array of coordinates stored on an rtree page.
183094** This function checks that the coordinates are internally consistent (no
183095** x1>x2 conditions) and adds an error message to the RtreeCheck object
183096** if they are not.
183097**
183098** Additionally, if pParent is not NULL, then it is assumed to point to
183099** the array of coordinates on the parent page that bound the page
183100** containing pCell. In this case it is also verified that the two
183101** sets of coordinates are mutually consistent and an error message added
183102** to the RtreeCheck object if they are not.
183103*/
183104static void rtreeCheckCellCoord(
183105 RtreeCheck *pCheck,
183106 i64 iNode, /* Node id to use in error messages */
183107 int iCell, /* Cell number to use in error messages */
183108 u8 *pCell, /* Pointer to cell coordinates */
183109 u8 *pParent /* Pointer to parent coordinates */
183110){
183111 RtreeCoord c1, c2;
183112 RtreeCoord p1, p2;
183113 int i;
183114
183115 for(i=0; i<pCheck->nDim; i++){
183116 readCoord(&pCell[4*2*i], &c1);
183117 readCoord(&pCell[4*(2*i + 1)], &c2);
183118
183119 /* printf("%e, %e\n", c1.u.f, c2.u.f); */
183120 if( pCheck->bInt ? c1.i>c2.i : c1.f>c2.f ){
183121 rtreeCheckAppendMsg(pCheck,
183122 "Dimension %d of cell %d on node %lld is corrupt", i, iCell, iNode
183123 );
183124 }
183125
183126 if( pParent ){
183127 readCoord(&pParent[4*2*i], &p1);
183128 readCoord(&pParent[4*(2*i + 1)], &p2);
183129
183130 if( (pCheck->bInt ? c1.i<p1.i : c1.f<p1.f)
183131 || (pCheck->bInt ? c2.i>p2.i : c2.f>p2.f)
183132 ){
183133 rtreeCheckAppendMsg(pCheck,
183134 "Dimension %d of cell %d on node %lld is corrupt relative to parent"
183135 , i, iCell, iNode
183136 );
183137 }
183138 }
183139 }
183140}
183141
183142/*
183143** Run rtreecheck() checks on node iNode, which is at depth iDepth within
183144** the r-tree structure. Argument aParent points to the array of coordinates
183145** that bound node iNode on the parent node.
183146**
183147** If any problems are discovered, an error message is appended to the
183148** report accumulated in the RtreeCheck object.
183149*/
183150static void rtreeCheckNode(
183151 RtreeCheck *pCheck,
183152 int iDepth, /* Depth of iNode (0==leaf) */
183153 u8 *aParent, /* Buffer containing parent coords */
183154 i64 iNode /* Node to check */
183155){
183156 u8 *aNode = 0;
183157 int nNode = 0;
183158
183159 assert( iNode==1 || aParent!=0 );
183160 assert( pCheck->nDim>0 );
183161
183162 aNode = rtreeCheckGetNode(pCheck, iNode, &nNode);
183163 if( aNode ){
183164 if( nNode<4 ){
183165 rtreeCheckAppendMsg(pCheck,
183166 "Node %lld is too small (%d bytes)", iNode, nNode
183167 );
183168 }else{
183169 int nCell; /* Number of cells on page */
183170 int i; /* Used to iterate through cells */
183171 if( aParent==0 ){
183172 iDepth = readInt16(aNode);
183173 if( iDepth>RTREE_MAX_DEPTH ){
183174 rtreeCheckAppendMsg(pCheck, "Rtree depth out of range (%d)", iDepth);
183175 sqlite3_free(aNode);
183176 return;
183177 }
183178 }
183179 nCell = readInt16(&aNode[2]);
183180 if( (4 + nCell*(8 + pCheck->nDim*2*4))>nNode ){
183181 rtreeCheckAppendMsg(pCheck,
183182 "Node %lld is too small for cell count of %d (%d bytes)",
183183 iNode, nCell, nNode
183184 );
183185 }else{
183186 for(i=0; i<nCell; i++){
183187 u8 *pCell = &aNode[4 + i*(8 + pCheck->nDim*2*4)];
183188 i64 iVal = readInt64(pCell);
183189 rtreeCheckCellCoord(pCheck, iNode, i, &pCell[8], aParent);
183190
183191 if( iDepth>0 ){
183192 rtreeCheckMapping(pCheck, 0, iVal, iNode);
183193 rtreeCheckNode(pCheck, iDepth-1, &pCell[8], iVal);
183194 pCheck->nNonLeaf++;
183195 }else{
183196 rtreeCheckMapping(pCheck, 1, iVal, iNode);
183197 pCheck->nLeaf++;
183198 }
183199 }
183200 }
183201 }
183202 sqlite3_free(aNode);
183203 }
183204}
183205
183206/*
183207** The second argument to this function must be either "_rowid" or
183208** "_parent". This function checks that the number of entries in the
183209** %_rowid or %_parent table is exactly nExpect. If not, it adds
183210** an error message to the report in the RtreeCheck object indicated
183211** by the first argument.
183212*/
183213static void rtreeCheckCount(RtreeCheck *pCheck, const char *zTbl, i64 nExpect){
183214 if( pCheck->rc==SQLITE_OK ){
183215 sqlite3_stmt *pCount;
183216 pCount = rtreeCheckPrepare(pCheck, "SELECT count(*) FROM %Q.'%q%s'",
183217 pCheck->zDb, pCheck->zTab, zTbl
183218 );
183219 if( pCount ){
183220 if( sqlite3_step(pCount)==SQLITE_ROW ){
183221 i64 nActual = sqlite3_column_int64(pCount, 0);
183222 if( nActual!=nExpect ){
183223 rtreeCheckAppendMsg(pCheck, "Wrong number of entries in %%%s table"
183224 " - expected %lld, actual %lld" , zTbl, nExpect, nActual
183225 );
183226 }
183227 }
183228 pCheck->rc = sqlite3_finalize(pCount);
183229 }
183230 }
183231}
183232
183233/*
183234** This function does the bulk of the work for the rtree integrity-check.
183235** It is called by rtreecheck(), which is the SQL function implementation.
183236*/
183237static int rtreeCheckTable(
183238 sqlite3 *db, /* Database handle to access db through */
183239 const char *zDb, /* Name of db ("main", "temp" etc.) */
183240 const char *zTab, /* Name of rtree table to check */
183241 char **pzReport /* OUT: sqlite3_malloc'd report text */
183242){
183243 RtreeCheck check; /* Common context for various routines */
183244 sqlite3_stmt *pStmt = 0; /* Used to find column count of rtree table */
183245 int bEnd = 0; /* True if transaction should be closed */
183246 int nAux = 0; /* Number of extra columns. */
183247
183248 /* Initialize the context object */
183249 memset(&check, 0, sizeof(check));
183250 check.db = db;
183251 check.zDb = zDb;
183252 check.zTab = zTab;
183253
183254 /* If there is not already an open transaction, open one now. This is
183255 ** to ensure that the queries run as part of this integrity-check operate
183256 ** on a consistent snapshot. */
183257 if( sqlite3_get_autocommit(db) ){
183258 check.rc = sqlite3_exec(db, "BEGIN", 0, 0, 0);
183259 bEnd = 1;
183260 }
183261
183262 /* Find the number of auxiliary columns */
183263 if( check.rc==SQLITE_OK ){
183264 pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.'%q_rowid'", zDb, zTab);
183265 if( pStmt ){
183266 nAux = sqlite3_column_count(pStmt) - 2;
183267 sqlite3_finalize(pStmt);
183268 }
183269 check.rc = SQLITE_OK;
183270 }
183271
183272 /* Find number of dimensions in the rtree table. */
183273 pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.%Q", zDb, zTab);
183274 if( pStmt ){
183275 int rc;
183276 check.nDim = (sqlite3_column_count(pStmt) - 1 - nAux) / 2;
183277 if( check.nDim<1 ){
183278 rtreeCheckAppendMsg(&check, "Schema corrupt or not an rtree");
183279 }else if( SQLITE_ROW==sqlite3_step(pStmt) ){
183280 check.bInt = (sqlite3_column_type(pStmt, 1)==SQLITE_INTEGER);
183281 }
183282 rc = sqlite3_finalize(pStmt);
183283 if( rc!=SQLITE_CORRUPT ) check.rc = rc;
183284 }
183285
183286 /* Do the actual integrity-check */
183287 if( check.nDim>=1 ){
183288 if( check.rc==SQLITE_OK ){
183289 rtreeCheckNode(&check, 0, 0, 1);
183290 }
183291 rtreeCheckCount(&check, "_rowid", check.nLeaf);
183292 rtreeCheckCount(&check, "_parent", check.nNonLeaf);
183293 }
183294
183295 /* Finalize SQL statements used by the integrity-check */
183296 sqlite3_finalize(check.pGetNode);
183297 sqlite3_finalize(check.aCheckMapping[0]);
183298 sqlite3_finalize(check.aCheckMapping[1]);
183299
183300 /* If one was opened, close the transaction */
183301 if( bEnd ){
183302 int rc = sqlite3_exec(db, "END", 0, 0, 0);
183303 if( check.rc==SQLITE_OK ) check.rc = rc;
183304 }
183305 *pzReport = check.zReport;
183306 return check.rc;
183307}
183308
183309/*
183310** Usage:
183311**
183312** rtreecheck(<rtree-table>);
183313** rtreecheck(<database>, <rtree-table>);
183314**
183315** Invoking this SQL function runs an integrity-check on the named rtree
183316** table. The integrity-check verifies the following:
183317**
183318** 1. For each cell in the r-tree structure (%_node table), that:
183319**
183320** a) for each dimension, (coord1 <= coord2).
183321**
183322** b) unless the cell is on the root node, that the cell is bounded
183323** by the parent cell on the parent node.
183324**
183325** c) for leaf nodes, that there is an entry in the %_rowid
183326** table corresponding to the cell's rowid value that
183327** points to the correct node.
183328**
183329** d) for cells on non-leaf nodes, that there is an entry in the
183330** %_parent table mapping from the cell's child node to the
183331** node that it resides on.
183332**
183333** 2. That there are the same number of entries in the %_rowid table
183334** as there are leaf cells in the r-tree structure, and that there
183335** is a leaf cell that corresponds to each entry in the %_rowid table.
183336**
183337** 3. That there are the same number of entries in the %_parent table
183338** as there are non-leaf cells in the r-tree structure, and that
183339** there is a non-leaf cell that corresponds to each entry in the
183340** %_parent table.
183341*/
183342static void rtreecheck(
183343 sqlite3_context *ctx,
183344 int nArg,
183345 sqlite3_value **apArg
183346){
183347 if( nArg!=1 && nArg!=2 ){
183348 sqlite3_result_error(ctx,
183349 "wrong number of arguments to function rtreecheck()", -1
183350 );
183351 }else{
183352 int rc;
183353 char *zReport = 0;
183354 const char *zDb = (const char*)sqlite3_value_text(apArg[0]);
183355 const char *zTab;
183356 if( nArg==1 ){
183357 zTab = zDb;
183358 zDb = "main";
183359 }else{
183360 zTab = (const char*)sqlite3_value_text(apArg[1]);
183361 }
183362 rc = rtreeCheckTable(sqlite3_context_db_handle(ctx), zDb, zTab, &zReport);
183363 if( rc==SQLITE_OK ){
183364 sqlite3_result_text(ctx, zReport ? zReport : "ok", -1, SQLITE_TRANSIENT);
183365 }else{
183366 sqlite3_result_error_code(ctx, rc);
183367 }
183368 sqlite3_free(zReport);
183369 }
183370}
183371
183372/* Conditionally include the geopoly code */
183373#ifdef SQLITE_ENABLE_GEOPOLY
183374/************** Include geopoly.c in the middle of rtree.c *******************/
183375/************** Begin file geopoly.c *****************************************/
183376/*
183377** 2018-05-25
183378**
183379** The author disclaims copyright to this source code. In place of
183380** a legal notice, here is a blessing:
183381**
183382** May you do good and not evil.
183383** May you find forgiveness for yourself and forgive others.
183384** May you share freely, never taking more than you give.
183385**
183386******************************************************************************
183387**
183388** This file implements an alternative R-Tree virtual table that
183389** uses polygons to express the boundaries of 2-dimensional objects.
183390**
183391** This file is #include-ed onto the end of "rtree.c" so that it has
183392** access to all of the R-Tree internals.
183393*/
183394/* #include <stdlib.h> */
183395
183396/* Enable -DGEOPOLY_ENABLE_DEBUG for debugging facilities */
183397#ifdef GEOPOLY_ENABLE_DEBUG
183398 static int geo_debug = 0;
183399# define GEODEBUG(X) if(geo_debug)printf X
183400#else
183401# define GEODEBUG(X)
183402#endif
183403
183404#ifndef JSON_NULL /* The following stuff repeats things found in json1 */
183405/*
183406** Versions of isspace(), isalnum() and isdigit() to which it is safe
183407** to pass signed char values.
183408*/
183409#ifdef sqlite3Isdigit
183410 /* Use the SQLite core versions if this routine is part of the
183411 ** SQLite amalgamation */
183412# define safe_isdigit(x) sqlite3Isdigit(x)
183413# define safe_isalnum(x) sqlite3Isalnum(x)
183414# define safe_isxdigit(x) sqlite3Isxdigit(x)
183415#else
183416 /* Use the standard library for separate compilation */
183417#include <ctype.h> /* amalgamator: keep */
183418# define safe_isdigit(x) isdigit((unsigned char)(x))
183419# define safe_isalnum(x) isalnum((unsigned char)(x))
183420# define safe_isxdigit(x) isxdigit((unsigned char)(x))
183421#endif
183422
183423/*
183424** Growing our own isspace() routine this way is twice as fast as
183425** the library isspace() function.
183426*/
183427static const char geopolyIsSpace[] = {
183428 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
183429 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
183430 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
183431 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
183432 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
183433 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
183434 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
183435 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
183436 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
183437 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
183438 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
183439 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
183440 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
183441 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
183442 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
183443 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
183444};
183445#define safe_isspace(x) (geopolyIsSpace[(unsigned char)x])
183446#endif /* JSON NULL - back to original code */
183447
183448/* Compiler and version */
183449#ifndef GCC_VERSION
183450#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
183451# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
183452#else
183453# define GCC_VERSION 0
183454#endif
183455#endif
183456#ifndef MSVC_VERSION
183457#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
183458# define MSVC_VERSION _MSC_VER
183459#else
183460# define MSVC_VERSION 0
183461#endif
183462#endif
183463
183464/* Datatype for coordinates
183465*/
183466typedef float GeoCoord;
183467
183468/*
183469** Internal representation of a polygon.
183470**
183471** The polygon consists of a sequence of vertexes. There is a line
183472** segment between each pair of vertexes, and one final segment from
183473** the last vertex back to the first. (This differs from the GeoJSON
183474** standard in which the final vertex is a repeat of the first.)
183475**
183476** The polygon follows the right-hand rule. The area to the right of
183477** each segment is "outside" and the area to the left is "inside".
183478**
183479** The on-disk representation consists of a 4-byte header followed by
183480** the values. The 4-byte header is:
183481**
183482** encoding (1 byte) 0=big-endian, 1=little-endian
183483** nvertex (3 bytes) Number of vertexes as a big-endian integer
183484**
183485** Enough space is allocated for 4 coordinates, to work around over-zealous
183486** warnings coming from some compiler (notably, clang). In reality, the size
183487** of each GeoPoly memory allocate is adjusted as necessary so that the
183488** GeoPoly.a[] array at the end is the appropriate size.
183489*/
183490typedef struct GeoPoly GeoPoly;
183491struct GeoPoly {
183492 int nVertex; /* Number of vertexes */
183493 unsigned char hdr[4]; /* Header for on-disk representation */
183494 GeoCoord a[8]; /* 2*nVertex values. X (longitude) first, then Y */
183495};
183496
183497/* The size of a memory allocation needed for a GeoPoly object sufficient
183498** to hold N coordinate pairs.
183499*/
183500#define GEOPOLY_SZ(N) (sizeof(GeoPoly) + sizeof(GeoCoord)*2*((N)-4))
183501
183502/*
183503** State of a parse of a GeoJSON input.
183504*/
183505typedef struct GeoParse GeoParse;
183506struct GeoParse {
183507 const unsigned char *z; /* Unparsed input */
183508 int nVertex; /* Number of vertexes in a[] */
183509 int nAlloc; /* Space allocated to a[] */
183510 int nErr; /* Number of errors encountered */
183511 GeoCoord *a; /* Array of vertexes. From sqlite3_malloc64() */
183512};
183513
183514/* Do a 4-byte byte swap */
183515static void geopolySwab32(unsigned char *a){
183516 unsigned char t = a[0];
183517 a[0] = a[3];
183518 a[3] = t;
183519 t = a[1];
183520 a[1] = a[2];
183521 a[2] = t;
183522}
183523
183524/* Skip whitespace. Return the next non-whitespace character. */
183525static char geopolySkipSpace(GeoParse *p){
183526 while( safe_isspace(p->z[0]) ) p->z++;
183527 return p->z[0];
183528}
183529
183530/* Parse out a number. Write the value into *pVal if pVal!=0.
183531** return non-zero on success and zero if the next token is not a number.
183532*/
183533static int geopolyParseNumber(GeoParse *p, GeoCoord *pVal){
183534 char c = geopolySkipSpace(p);
183535 const unsigned char *z = p->z;
183536 int j = 0;
183537 int seenDP = 0;
183538 int seenE = 0;
183539 if( c=='-' ){
183540 j = 1;
183541 c = z[j];
183542 }
183543 if( c=='0' && z[j+1]>='0' && z[j+1]<='9' ) return 0;
183544 for(;; j++){
183545 c = z[j];
183546 if( safe_isdigit(c) ) continue;
183547 if( c=='.' ){
183548 if( z[j-1]=='-' ) return 0;
183549 if( seenDP ) return 0;
183550 seenDP = 1;
183551 continue;
183552 }
183553 if( c=='e' || c=='E' ){
183554 if( z[j-1]<'0' ) return 0;
183555 if( seenE ) return -1;
183556 seenDP = seenE = 1;
183557 c = z[j+1];
183558 if( c=='+' || c=='-' ){
183559 j++;
183560 c = z[j+1];
183561 }
183562 if( c<'0' || c>'9' ) return 0;
183563 continue;
183564 }
183565 break;
183566 }
183567 if( z[j-1]<'0' ) return 0;
183568 if( pVal ){
183569#ifdef SQLITE_AMALGAMATION
183570 /* The sqlite3AtoF() routine is much much faster than atof(), if it
183571 ** is available */
183572 double r;
183573 (void)sqlite3AtoF((const char*)p->z, &r, j, SQLITE_UTF8);
183574 *pVal = r;
183575#else
183576 *pVal = (GeoCoord)atof((const char*)p->z);
183577#endif
183578 }
183579 p->z += j;
183580 return 1;
183581}
183582
183583/*
183584** If the input is a well-formed JSON array of coordinates with at least
183585** four coordinates and where each coordinate is itself a two-value array,
183586** then convert the JSON into a GeoPoly object and return a pointer to
183587** that object.
183588**
183589** If any error occurs, return NULL.
183590*/
183591static GeoPoly *geopolyParseJson(const unsigned char *z, int *pRc){
183592 GeoParse s;
183593 int rc = SQLITE_OK;
183594 memset(&s, 0, sizeof(s));
183595 s.z = z;
183596 if( geopolySkipSpace(&s)=='[' ){
183597 s.z++;
183598 while( geopolySkipSpace(&s)=='[' ){
183599 int ii = 0;
183600 char c;
183601 s.z++;
183602 if( s.nVertex>=s.nAlloc ){
183603 GeoCoord *aNew;
183604 s.nAlloc = s.nAlloc*2 + 16;
183605 aNew = sqlite3_realloc64(s.a, s.nAlloc*sizeof(GeoCoord)*2 );
183606 if( aNew==0 ){
183607 rc = SQLITE_NOMEM;
183608 s.nErr++;
183609 break;
183610 }
183611 s.a = aNew;
183612 }
183613 while( geopolyParseNumber(&s, ii<=1 ? &s.a[s.nVertex*2+ii] : 0) ){
183614 ii++;
183615 if( ii==2 ) s.nVertex++;
183616 c = geopolySkipSpace(&s);
183617 s.z++;
183618 if( c==',' ) continue;
183619 if( c==']' && ii>=2 ) break;
183620 s.nErr++;
183621 rc = SQLITE_ERROR;
183622 goto parse_json_err;
183623 }
183624 if( geopolySkipSpace(&s)==',' ){
183625 s.z++;
183626 continue;
183627 }
183628 break;
183629 }
183630 if( geopolySkipSpace(&s)==']'
183631 && s.nVertex>=4
183632 && s.a[0]==s.a[s.nVertex*2-2]
183633 && s.a[1]==s.a[s.nVertex*2-1]
183634 && (s.z++, geopolySkipSpace(&s)==0)
183635 ){
183636 GeoPoly *pOut;
183637 int x = 1;
183638 s.nVertex--; /* Remove the redundant vertex at the end */
183639 pOut = sqlite3_malloc64( GEOPOLY_SZ(s.nVertex) );
183640 x = 1;
183641 if( pOut==0 ) goto parse_json_err;
183642 pOut->nVertex = s.nVertex;
183643 memcpy(pOut->a, s.a, s.nVertex*2*sizeof(GeoCoord));
183644 pOut->hdr[0] = *(unsigned char*)&x;
183645 pOut->hdr[1] = (s.nVertex>>16)&0xff;
183646 pOut->hdr[2] = (s.nVertex>>8)&0xff;
183647 pOut->hdr[3] = s.nVertex&0xff;
183648 sqlite3_free(s.a);
183649 if( pRc ) *pRc = SQLITE_OK;
183650 return pOut;
183651 }else{
183652 s.nErr++;
183653 rc = SQLITE_ERROR;
183654 }
183655 }
183656parse_json_err:
183657 if( pRc ) *pRc = rc;
183658 sqlite3_free(s.a);
183659 return 0;
183660}
183661
183662/*
183663** Given a function parameter, try to interpret it as a polygon, either
183664** in the binary format or JSON text. Compute a GeoPoly object and
183665** return a pointer to that object. Or if the input is not a well-formed
183666** polygon, put an error message in sqlite3_context and return NULL.
183667*/
183668static GeoPoly *geopolyFuncParam(
183669 sqlite3_context *pCtx, /* Context for error messages */
183670 sqlite3_value *pVal, /* The value to decode */
183671 int *pRc /* Write error here */
183672){
183673 GeoPoly *p = 0;
183674 int nByte;
183675 if( sqlite3_value_type(pVal)==SQLITE_BLOB
183676 && (nByte = sqlite3_value_bytes(pVal))>=(4+6*sizeof(GeoCoord))
183677 ){
183678 const unsigned char *a = sqlite3_value_blob(pVal);
183679 int nVertex;
183680 nVertex = (a[1]<<16) + (a[2]<<8) + a[3];
183681 if( (a[0]==0 || a[0]==1)
183682 && (nVertex*2*sizeof(GeoCoord) + 4)==(unsigned int)nByte
183683 ){
183684 p = sqlite3_malloc64( sizeof(*p) + (nVertex-1)*2*sizeof(GeoCoord) );
183685 if( p==0 ){
183686 if( pRc ) *pRc = SQLITE_NOMEM;
183687 if( pCtx ) sqlite3_result_error_nomem(pCtx);
183688 }else{
183689 int x = 1;
183690 p->nVertex = nVertex;
183691 memcpy(p->hdr, a, nByte);
183692 if( a[0] != *(unsigned char*)&x ){
183693 int ii;
183694 for(ii=0; ii<nVertex*2; ii++){
183695 geopolySwab32((unsigned char*)&p->a[ii]);
183696 }
183697 p->hdr[0] ^= 1;
183698 }
183699 }
183700 }
183701 if( pRc ) *pRc = SQLITE_OK;
183702 return p;
183703 }else if( sqlite3_value_type(pVal)==SQLITE_TEXT ){
183704 const unsigned char *zJson = sqlite3_value_text(pVal);
183705 if( zJson==0 ){
183706 if( pRc ) *pRc = SQLITE_NOMEM;
183707 return 0;
183708 }
183709 return geopolyParseJson(zJson, pRc);
183710 }else{
183711 if( pRc ) *pRc = SQLITE_ERROR;
183712 return 0;
183713 }
183714}
183715
183716/*
183717** Implementation of the geopoly_blob(X) function.
183718**
183719** If the input is a well-formed Geopoly BLOB or JSON string
183720** then return the BLOB representation of the polygon. Otherwise
183721** return NULL.
183722*/
183723static void geopolyBlobFunc(
183724 sqlite3_context *context,
183725 int argc,
183726 sqlite3_value **argv
183727){
183728 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
183729 if( p ){
183730 sqlite3_result_blob(context, p->hdr,
183731 4+8*p->nVertex, SQLITE_TRANSIENT);
183732 sqlite3_free(p);
183733 }
183734}
183735
183736/*
183737** SQL function: geopoly_json(X)
183738**
183739** Interpret X as a polygon and render it as a JSON array
183740** of coordinates. Or, if X is not a valid polygon, return NULL.
183741*/
183742static void geopolyJsonFunc(
183743 sqlite3_context *context,
183744 int argc,
183745 sqlite3_value **argv
183746){
183747 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
183748 if( p ){
183749 sqlite3 *db = sqlite3_context_db_handle(context);
183750 sqlite3_str *x = sqlite3_str_new(db);
183751 int i;
183752 sqlite3_str_append(x, "[", 1);
183753 for(i=0; i<p->nVertex; i++){
183754 sqlite3_str_appendf(x, "[%!g,%!g],", p->a[i*2], p->a[i*2+1]);
183755 }
183756 sqlite3_str_appendf(x, "[%!g,%!g]]", p->a[0], p->a[1]);
183757 sqlite3_result_text(context, sqlite3_str_finish(x), -1, sqlite3_free);
183758 sqlite3_free(p);
183759 }
183760}
183761
183762/*
183763** SQL function: geopoly_svg(X, ....)
183764**
183765** Interpret X as a polygon and render it as a SVG <polyline>.
183766** Additional arguments are added as attributes to the <polyline>.
183767*/
183768static void geopolySvgFunc(
183769 sqlite3_context *context,
183770 int argc,
183771 sqlite3_value **argv
183772){
183773 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
183774 if( p ){
183775 sqlite3 *db = sqlite3_context_db_handle(context);
183776 sqlite3_str *x = sqlite3_str_new(db);
183777 int i;
183778 char cSep = '\'';
183779 sqlite3_str_appendf(x, "<polyline points=");
183780 for(i=0; i<p->nVertex; i++){
183781 sqlite3_str_appendf(x, "%c%g,%g", cSep, p->a[i*2], p->a[i*2+1]);
183782 cSep = ' ';
183783 }
183784 sqlite3_str_appendf(x, " %g,%g'", p->a[0], p->a[1]);
183785 for(i=1; i<argc; i++){
183786 const char *z = (const char*)sqlite3_value_text(argv[i]);
183787 if( z && z[0] ){
183788 sqlite3_str_appendf(x, " %s", z);
183789 }
183790 }
183791 sqlite3_str_appendf(x, "></polyline>");
183792 sqlite3_result_text(context, sqlite3_str_finish(x), -1, sqlite3_free);
183793 sqlite3_free(p);
183794 }
183795}
183796
183797/*
183798** SQL Function: geopoly_xform(poly, A, B, C, D, E, F)
183799**
183800** Transform and/or translate a polygon as follows:
183801**
183802** x1 = A*x0 + B*y0 + E
183803** y1 = C*x0 + D*y0 + F
183804**
183805** For a translation:
183806**
183807** geopoly_xform(poly, 1, 0, 0, 1, x-offset, y-offset)
183808**
183809** Rotate by R around the point (0,0):
183810**
183811** geopoly_xform(poly, cos(R), sin(R), -sin(R), cos(R), 0, 0)
183812*/
183813static void geopolyXformFunc(
183814 sqlite3_context *context,
183815 int argc,
183816 sqlite3_value **argv
183817){
183818 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
183819 double A = sqlite3_value_double(argv[1]);
183820 double B = sqlite3_value_double(argv[2]);
183821 double C = sqlite3_value_double(argv[3]);
183822 double D = sqlite3_value_double(argv[4]);
183823 double E = sqlite3_value_double(argv[5]);
183824 double F = sqlite3_value_double(argv[6]);
183825 GeoCoord x1, y1, x0, y0;
183826 int ii;
183827 if( p ){
183828 for(ii=0; ii<p->nVertex; ii++){
183829 x0 = p->a[ii*2];
183830 y0 = p->a[ii*2+1];
183831 x1 = (GeoCoord)(A*x0 + B*y0 + E);
183832 y1 = (GeoCoord)(C*x0 + D*y0 + F);
183833 p->a[ii*2] = x1;
183834 p->a[ii*2+1] = y1;
183835 }
183836 sqlite3_result_blob(context, p->hdr,
183837 4+8*p->nVertex, SQLITE_TRANSIENT);
183838 sqlite3_free(p);
183839 }
183840}
183841
183842/*
183843** Compute the area enclosed by the polygon.
183844**
183845** This routine can also be used to detect polygons that rotate in
183846** the wrong direction. Polygons are suppose to be counter-clockwise (CCW).
183847** This routine returns a negative value for clockwise (CW) polygons.
183848*/
183849static double geopolyArea(GeoPoly *p){
183850 double rArea = 0.0;
183851 int ii;
183852 for(ii=0; ii<p->nVertex-1; ii++){
183853 rArea += (p->a[ii*2] - p->a[ii*2+2]) /* (x0 - x1) */
183854 * (p->a[ii*2+1] + p->a[ii*2+3]) /* (y0 + y1) */
183855 * 0.5;
183856 }
183857 rArea += (p->a[ii*2] - p->a[0]) /* (xN - x0) */
183858 * (p->a[ii*2+1] + p->a[1]) /* (yN + y0) */
183859 * 0.5;
183860 return rArea;
183861}
183862
183863/*
183864** Implementation of the geopoly_area(X) function.
183865**
183866** If the input is a well-formed Geopoly BLOB then return the area
183867** enclosed by the polygon. If the polygon circulates clockwise instead
183868** of counterclockwise (as it should) then return the negative of the
183869** enclosed area. Otherwise return NULL.
183870*/
183871static void geopolyAreaFunc(
183872 sqlite3_context *context,
183873 int argc,
183874 sqlite3_value **argv
183875){
183876 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
183877 if( p ){
183878 sqlite3_result_double(context, geopolyArea(p));
183879 sqlite3_free(p);
183880 }
183881}
183882
183883/*
183884** Implementation of the geopoly_ccw(X) function.
183885**
183886** If the rotation of polygon X is clockwise (incorrect) instead of
183887** counter-clockwise (the correct winding order according to RFC7946)
183888** then reverse the order of the vertexes in polygon X.
183889**
183890** In other words, this routine returns a CCW polygon regardless of the
183891** winding order of its input.
183892**
183893** Use this routine to sanitize historical inputs that that sometimes
183894** contain polygons that wind in the wrong direction.
183895*/
183896static void geopolyCcwFunc(
183897 sqlite3_context *context,
183898 int argc,
183899 sqlite3_value **argv
183900){
183901 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
183902 if( p ){
183903 if( geopolyArea(p)<0.0 ){
183904 int ii, jj;
183905 for(ii=2, jj=p->nVertex*2 - 2; ii<jj; ii+=2, jj-=2){
183906 GeoCoord t = p->a[ii];
183907 p->a[ii] = p->a[jj];
183908 p->a[jj] = t;
183909 t = p->a[ii+1];
183910 p->a[ii+1] = p->a[jj+1];
183911 p->a[jj+1] = t;
183912 }
183913 }
183914 sqlite3_result_blob(context, p->hdr,
183915 4+8*p->nVertex, SQLITE_TRANSIENT);
183916 sqlite3_free(p);
183917 }
183918}
183919
183920#define GEOPOLY_PI 3.1415926535897932385
183921
183922/* Fast approximation for sine(X) for X between -0.5*pi and 2*pi
183923*/
183924static double geopolySine(double r){
183925 assert( r>=-0.5*GEOPOLY_PI && r<=2.0*GEOPOLY_PI );
183926 if( r>=1.5*GEOPOLY_PI ){
183927 r -= 2.0*GEOPOLY_PI;
183928 }
183929 if( r>=0.5*GEOPOLY_PI ){
183930 return -geopolySine(r-GEOPOLY_PI);
183931 }else{
183932 double r2 = r*r;
183933 double r3 = r2*r;
183934 double r5 = r3*r2;
183935 return 0.9996949*r - 0.1656700*r3 + 0.0075134*r5;
183936 }
183937}
183938
183939/*
183940** Function: geopoly_regular(X,Y,R,N)
183941**
183942** Construct a simple, convex, regular polygon centered at X, Y
183943** with circumradius R and with N sides.
183944*/
183945static void geopolyRegularFunc(
183946 sqlite3_context *context,
183947 int argc,
183948 sqlite3_value **argv
183949){
183950 double x = sqlite3_value_double(argv[0]);
183951 double y = sqlite3_value_double(argv[1]);
183952 double r = sqlite3_value_double(argv[2]);
183953 int n = sqlite3_value_int(argv[3]);
183954 int i;
183955 GeoPoly *p;
183956
183957 if( n<3 || r<=0.0 ) return;
183958 if( n>1000 ) n = 1000;
183959 p = sqlite3_malloc64( sizeof(*p) + (n-1)*2*sizeof(GeoCoord) );
183960 if( p==0 ){
183961 sqlite3_result_error_nomem(context);
183962 return;
183963 }
183964 i = 1;
183965 p->hdr[0] = *(unsigned char*)&i;
183966 p->hdr[1] = 0;
183967 p->hdr[2] = (n>>8)&0xff;
183968 p->hdr[3] = n&0xff;
183969 for(i=0; i<n; i++){
183970 double rAngle = 2.0*GEOPOLY_PI*i/n;
183971 p->a[i*2] = x - r*geopolySine(rAngle-0.5*GEOPOLY_PI);
183972 p->a[i*2+1] = y + r*geopolySine(rAngle);
183973 }
183974 sqlite3_result_blob(context, p->hdr, 4+8*n, SQLITE_TRANSIENT);
183975 sqlite3_free(p);
183976}
183977
183978/*
183979** If pPoly is a polygon, compute its bounding box. Then:
183980**
183981** (1) if aCoord!=0 store the bounding box in aCoord, returning NULL
183982** (2) otherwise, compute a GeoPoly for the bounding box and return the
183983** new GeoPoly
183984**
183985** If pPoly is NULL but aCoord is not NULL, then compute a new GeoPoly from
183986** the bounding box in aCoord and return a pointer to that GeoPoly.
183987*/
183988static GeoPoly *geopolyBBox(
183989 sqlite3_context *context, /* For recording the error */
183990 sqlite3_value *pPoly, /* The polygon */
183991 RtreeCoord *aCoord, /* Results here */
183992 int *pRc /* Error code here */
183993){
183994 GeoPoly *pOut = 0;
183995 GeoPoly *p;
183996 float mnX, mxX, mnY, mxY;
183997 if( pPoly==0 && aCoord!=0 ){
183998 p = 0;
183999 mnX = aCoord[0].f;
184000 mxX = aCoord[1].f;
184001 mnY = aCoord[2].f;
184002 mxY = aCoord[3].f;
184003 goto geopolyBboxFill;
184004 }else{
184005 p = geopolyFuncParam(context, pPoly, pRc);
184006 }
184007 if( p ){
184008 int ii;
184009 mnX = mxX = p->a[0];
184010 mnY = mxY = p->a[1];
184011 for(ii=1; ii<p->nVertex; ii++){
184012 double r = p->a[ii*2];
184013 if( r<mnX ) mnX = (float)r;
184014 else if( r>mxX ) mxX = (float)r;
184015 r = p->a[ii*2+1];
184016 if( r<mnY ) mnY = (float)r;
184017 else if( r>mxY ) mxY = (float)r;
184018 }
184019 if( pRc ) *pRc = SQLITE_OK;
184020 if( aCoord==0 ){
184021 geopolyBboxFill:
184022 pOut = sqlite3_realloc(p, GEOPOLY_SZ(4));
184023 if( pOut==0 ){
184024 sqlite3_free(p);
184025 if( context ) sqlite3_result_error_nomem(context);
184026 if( pRc ) *pRc = SQLITE_NOMEM;
184027 return 0;
184028 }
184029 pOut->nVertex = 4;
184030 ii = 1;
184031 pOut->hdr[0] = *(unsigned char*)&ii;
184032 pOut->hdr[1] = 0;
184033 pOut->hdr[2] = 0;
184034 pOut->hdr[3] = 4;
184035 pOut->a[0] = mnX;
184036 pOut->a[1] = mnY;
184037 pOut->a[2] = mxX;
184038 pOut->a[3] = mnY;
184039 pOut->a[4] = mxX;
184040 pOut->a[5] = mxY;
184041 pOut->a[6] = mnX;
184042 pOut->a[7] = mxY;
184043 }else{
184044 sqlite3_free(p);
184045 aCoord[0].f = mnX;
184046 aCoord[1].f = mxX;
184047 aCoord[2].f = mnY;
184048 aCoord[3].f = mxY;
184049 }
184050 }
184051 return pOut;
184052}
184053
184054/*
184055** Implementation of the geopoly_bbox(X) SQL function.
184056*/
184057static void geopolyBBoxFunc(
184058 sqlite3_context *context,
184059 int argc,
184060 sqlite3_value **argv
184061){
184062 GeoPoly *p = geopolyBBox(context, argv[0], 0, 0);
184063 if( p ){
184064 sqlite3_result_blob(context, p->hdr,
184065 4+8*p->nVertex, SQLITE_TRANSIENT);
184066 sqlite3_free(p);
184067 }
184068}
184069
184070/*
184071** State vector for the geopoly_group_bbox() aggregate function.
184072*/
184073typedef struct GeoBBox GeoBBox;
184074struct GeoBBox {
184075 int isInit;
184076 RtreeCoord a[4];
184077};
184078
184079
184080/*
184081** Implementation of the geopoly_group_bbox(X) aggregate SQL function.
184082*/
184083static void geopolyBBoxStep(
184084 sqlite3_context *context,
184085 int argc,
184086 sqlite3_value **argv
184087){
184088 RtreeCoord a[4];
184089 int rc = SQLITE_OK;
184090 (void)geopolyBBox(context, argv[0], a, &rc);
184091 if( rc==SQLITE_OK ){
184092 GeoBBox *pBBox;
184093 pBBox = (GeoBBox*)sqlite3_aggregate_context(context, sizeof(*pBBox));
184094 if( pBBox==0 ) return;
184095 if( pBBox->isInit==0 ){
184096 pBBox->isInit = 1;
184097 memcpy(pBBox->a, a, sizeof(RtreeCoord)*4);
184098 }else{
184099 if( a[0].f < pBBox->a[0].f ) pBBox->a[0] = a[0];
184100 if( a[1].f > pBBox->a[1].f ) pBBox->a[1] = a[1];
184101 if( a[2].f < pBBox->a[2].f ) pBBox->a[2] = a[2];
184102 if( a[3].f > pBBox->a[3].f ) pBBox->a[3] = a[3];
184103 }
184104 }
184105}
184106static void geopolyBBoxFinal(
184107 sqlite3_context *context
184108){
184109 GeoPoly *p;
184110 GeoBBox *pBBox;
184111 pBBox = (GeoBBox*)sqlite3_aggregate_context(context, 0);
184112 if( pBBox==0 ) return;
184113 p = geopolyBBox(context, 0, pBBox->a, 0);
184114 if( p ){
184115 sqlite3_result_blob(context, p->hdr,
184116 4+8*p->nVertex, SQLITE_TRANSIENT);
184117 sqlite3_free(p);
184118 }
184119}
184120
184121
184122/*
184123** Determine if point (x0,y0) is beneath line segment (x1,y1)->(x2,y2).
184124** Returns:
184125**
184126** +2 x0,y0 is on the line segement
184127**
184128** +1 x0,y0 is beneath line segment
184129**
184130** 0 x0,y0 is not on or beneath the line segment or the line segment
184131** is vertical and x0,y0 is not on the line segment
184132**
184133** The left-most coordinate min(x1,x2) is not considered to be part of
184134** the line segment for the purposes of this analysis.
184135*/
184136static int pointBeneathLine(
184137 double x0, double y0,
184138 double x1, double y1,
184139 double x2, double y2
184140){
184141 double y;
184142 if( x0==x1 && y0==y1 ) return 2;
184143 if( x1<x2 ){
184144 if( x0<=x1 || x0>x2 ) return 0;
184145 }else if( x1>x2 ){
184146 if( x0<=x2 || x0>x1 ) return 0;
184147 }else{
184148 /* Vertical line segment */
184149 if( x0!=x1 ) return 0;
184150 if( y0<y1 && y0<y2 ) return 0;
184151 if( y0>y1 && y0>y2 ) return 0;
184152 return 2;
184153 }
184154 y = y1 + (y2-y1)*(x0-x1)/(x2-x1);
184155 if( y0==y ) return 2;
184156 if( y0<y ) return 1;
184157 return 0;
184158}
184159
184160/*
184161** SQL function: geopoly_contains_point(P,X,Y)
184162**
184163** Return +2 if point X,Y is within polygon P.
184164** Return +1 if point X,Y is on the polygon boundary.
184165** Return 0 if point X,Y is outside the polygon
184166*/
184167static void geopolyContainsPointFunc(
184168 sqlite3_context *context,
184169 int argc,
184170 sqlite3_value **argv
184171){
184172 GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
184173 double x0 = sqlite3_value_double(argv[1]);
184174 double y0 = sqlite3_value_double(argv[2]);
184175 int v = 0;
184176 int cnt = 0;
184177 int ii;
184178 if( p1==0 ) return;
184179 for(ii=0; ii<p1->nVertex-1; ii++){
184180 v = pointBeneathLine(x0,y0,p1->a[ii*2],p1->a[ii*2+1],
184181 p1->a[ii*2+2],p1->a[ii*2+3]);
184182 if( v==2 ) break;
184183 cnt += v;
184184 }
184185 if( v!=2 ){
184186 v = pointBeneathLine(x0,y0,p1->a[ii*2],p1->a[ii*2+1],
184187 p1->a[0],p1->a[1]);
184188 }
184189 if( v==2 ){
184190 sqlite3_result_int(context, 1);
184191 }else if( ((v+cnt)&1)==0 ){
184192 sqlite3_result_int(context, 0);
184193 }else{
184194 sqlite3_result_int(context, 2);
184195 }
184196 sqlite3_free(p1);
184197}
184198
184199/* Forward declaration */
184200static int geopolyOverlap(GeoPoly *p1, GeoPoly *p2);
184201
184202/*
184203** SQL function: geopoly_within(P1,P2)
184204**
184205** Return +2 if P1 and P2 are the same polygon
184206** Return +1 if P2 is contained within P1
184207** Return 0 if any part of P2 is on the outside of P1
184208**
184209*/
184210static void geopolyWithinFunc(
184211 sqlite3_context *context,
184212 int argc,
184213 sqlite3_value **argv
184214){
184215 GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
184216 GeoPoly *p2 = geopolyFuncParam(context, argv[1], 0);
184217 if( p1 && p2 ){
184218 int x = geopolyOverlap(p1, p2);
184219 if( x<0 ){
184220 sqlite3_result_error_nomem(context);
184221 }else{
184222 sqlite3_result_int(context, x==2 ? 1 : x==4 ? 2 : 0);
184223 }
184224 }
184225 sqlite3_free(p1);
184226 sqlite3_free(p2);
184227}
184228
184229/* Objects used by the overlap algorihm. */
184230typedef struct GeoEvent GeoEvent;
184231typedef struct GeoSegment GeoSegment;
184232typedef struct GeoOverlap GeoOverlap;
184233struct GeoEvent {
184234 double x; /* X coordinate at which event occurs */
184235 int eType; /* 0 for ADD, 1 for REMOVE */
184236 GeoSegment *pSeg; /* The segment to be added or removed */
184237 GeoEvent *pNext; /* Next event in the sorted list */
184238};
184239struct GeoSegment {
184240 double C, B; /* y = C*x + B */
184241 double y; /* Current y value */
184242 float y0; /* Initial y value */
184243 unsigned char side; /* 1 for p1, 2 for p2 */
184244 unsigned int idx; /* Which segment within the side */
184245 GeoSegment *pNext; /* Next segment in a list sorted by y */
184246};
184247struct GeoOverlap {
184248 GeoEvent *aEvent; /* Array of all events */
184249 GeoSegment *aSegment; /* Array of all segments */
184250 int nEvent; /* Number of events */
184251 int nSegment; /* Number of segments */
184252};
184253
184254/*
184255** Add a single segment and its associated events.
184256*/
184257static void geopolyAddOneSegment(
184258 GeoOverlap *p,
184259 GeoCoord x0,
184260 GeoCoord y0,
184261 GeoCoord x1,
184262 GeoCoord y1,
184263 unsigned char side,
184264 unsigned int idx
184265){
184266 GeoSegment *pSeg;
184267 GeoEvent *pEvent;
184268 if( x0==x1 ) return; /* Ignore vertical segments */
184269 if( x0>x1 ){
184270 GeoCoord t = x0;
184271 x0 = x1;
184272 x1 = t;
184273 t = y0;
184274 y0 = y1;
184275 y1 = t;
184276 }
184277 pSeg = p->aSegment + p->nSegment;
184278 p->nSegment++;
184279 pSeg->C = (y1-y0)/(x1-x0);
184280 pSeg->B = y1 - x1*pSeg->C;
184281 pSeg->y0 = y0;
184282 pSeg->side = side;
184283 pSeg->idx = idx;
184284 pEvent = p->aEvent + p->nEvent;
184285 p->nEvent++;
184286 pEvent->x = x0;
184287 pEvent->eType = 0;
184288 pEvent->pSeg = pSeg;
184289 pEvent = p->aEvent + p->nEvent;
184290 p->nEvent++;
184291 pEvent->x = x1;
184292 pEvent->eType = 1;
184293 pEvent->pSeg = pSeg;
184294}
184295
184296
184297
184298/*
184299** Insert all segments and events for polygon pPoly.
184300*/
184301static void geopolyAddSegments(
184302 GeoOverlap *p, /* Add segments to this Overlap object */
184303 GeoPoly *pPoly, /* Take all segments from this polygon */
184304 unsigned char side /* The side of pPoly */
184305){
184306 unsigned int i;
184307 GeoCoord *x;
184308 for(i=0; i<(unsigned)pPoly->nVertex-1; i++){
184309 x = pPoly->a + (i*2);
184310 geopolyAddOneSegment(p, x[0], x[1], x[2], x[3], side, i);
184311 }
184312 x = pPoly->a + (i*2);
184313 geopolyAddOneSegment(p, x[0], x[1], pPoly->a[0], pPoly->a[1], side, i);
184314}
184315
184316/*
184317** Merge two lists of sorted events by X coordinate
184318*/
184319static GeoEvent *geopolyEventMerge(GeoEvent *pLeft, GeoEvent *pRight){
184320 GeoEvent head, *pLast;
184321 head.pNext = 0;
184322 pLast = &head;
184323 while( pRight && pLeft ){
184324 if( pRight->x <= pLeft->x ){
184325 pLast->pNext = pRight;
184326 pLast = pRight;
184327 pRight = pRight->pNext;
184328 }else{
184329 pLast->pNext = pLeft;
184330 pLast = pLeft;
184331 pLeft = pLeft->pNext;
184332 }
184333 }
184334 pLast->pNext = pRight ? pRight : pLeft;
184335 return head.pNext;
184336}
184337
184338/*
184339** Sort an array of nEvent event objects into a list.
184340*/
184341static GeoEvent *geopolySortEventsByX(GeoEvent *aEvent, int nEvent){
184342 int mx = 0;
184343 int i, j;
184344 GeoEvent *p;
184345 GeoEvent *a[50];
184346 for(i=0; i<nEvent; i++){
184347 p = &aEvent[i];
184348 p->pNext = 0;
184349 for(j=0; j<mx && a[j]; j++){
184350 p = geopolyEventMerge(a[j], p);
184351 a[j] = 0;
184352 }
184353 a[j] = p;
184354 if( j>=mx ) mx = j+1;
184355 }
184356 p = 0;
184357 for(i=0; i<mx; i++){
184358 p = geopolyEventMerge(a[i], p);
184359 }
184360 return p;
184361}
184362
184363/*
184364** Merge two lists of sorted segments by Y, and then by C.
184365*/
184366static GeoSegment *geopolySegmentMerge(GeoSegment *pLeft, GeoSegment *pRight){
184367 GeoSegment head, *pLast;
184368 head.pNext = 0;
184369 pLast = &head;
184370 while( pRight && pLeft ){
184371 double r = pRight->y - pLeft->y;
184372 if( r==0.0 ) r = pRight->C - pLeft->C;
184373 if( r<0.0 ){
184374 pLast->pNext = pRight;
184375 pLast = pRight;
184376 pRight = pRight->pNext;
184377 }else{
184378 pLast->pNext = pLeft;
184379 pLast = pLeft;
184380 pLeft = pLeft->pNext;
184381 }
184382 }
184383 pLast->pNext = pRight ? pRight : pLeft;
184384 return head.pNext;
184385}
184386
184387/*
184388** Sort a list of GeoSegments in order of increasing Y and in the event of
184389** a tie, increasing C (slope).
184390*/
184391static GeoSegment *geopolySortSegmentsByYAndC(GeoSegment *pList){
184392 int mx = 0;
184393 int i;
184394 GeoSegment *p;
184395 GeoSegment *a[50];
184396 while( pList ){
184397 p = pList;
184398 pList = pList->pNext;
184399 p->pNext = 0;
184400 for(i=0; i<mx && a[i]; i++){
184401 p = geopolySegmentMerge(a[i], p);
184402 a[i] = 0;
184403 }
184404 a[i] = p;
184405 if( i>=mx ) mx = i+1;
184406 }
184407 p = 0;
184408 for(i=0; i<mx; i++){
184409 p = geopolySegmentMerge(a[i], p);
184410 }
184411 return p;
184412}
184413
184414/*
184415** Determine the overlap between two polygons
184416*/
184417static int geopolyOverlap(GeoPoly *p1, GeoPoly *p2){
184418 int nVertex = p1->nVertex + p2->nVertex + 2;
184419 GeoOverlap *p;
184420 int nByte;
184421 GeoEvent *pThisEvent;
184422 double rX;
184423 int rc = 0;
184424 int needSort = 0;
184425 GeoSegment *pActive = 0;
184426 GeoSegment *pSeg;
184427 unsigned char aOverlap[4];
184428
184429 nByte = sizeof(GeoEvent)*nVertex*2
184430 + sizeof(GeoSegment)*nVertex
184431 + sizeof(GeoOverlap);
184432 p = sqlite3_malloc( nByte );
184433 if( p==0 ) return -1;
184434 p->aEvent = (GeoEvent*)&p[1];
184435 p->aSegment = (GeoSegment*)&p->aEvent[nVertex*2];
184436 p->nEvent = p->nSegment = 0;
184437 geopolyAddSegments(p, p1, 1);
184438 geopolyAddSegments(p, p2, 2);
184439 pThisEvent = geopolySortEventsByX(p->aEvent, p->nEvent);
184440 rX = pThisEvent->x==0.0 ? -1.0 : 0.0;
184441 memset(aOverlap, 0, sizeof(aOverlap));
184442 while( pThisEvent ){
184443 if( pThisEvent->x!=rX ){
184444 GeoSegment *pPrev = 0;
184445 int iMask = 0;
184446 GEODEBUG(("Distinct X: %g\n", pThisEvent->x));
184447 rX = pThisEvent->x;
184448 if( needSort ){
184449 GEODEBUG(("SORT\n"));
184450 pActive = geopolySortSegmentsByYAndC(pActive);
184451 needSort = 0;
184452 }
184453 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
184454 if( pPrev ){
184455 if( pPrev->y!=pSeg->y ){
184456 GEODEBUG(("MASK: %d\n", iMask));
184457 aOverlap[iMask] = 1;
184458 }
184459 }
184460 iMask ^= pSeg->side;
184461 pPrev = pSeg;
184462 }
184463 pPrev = 0;
184464 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
184465 double y = pSeg->C*rX + pSeg->B;
184466 GEODEBUG(("Segment %d.%d %g->%g\n", pSeg->side, pSeg->idx, pSeg->y, y));
184467 pSeg->y = y;
184468 if( pPrev ){
184469 if( pPrev->y>pSeg->y && pPrev->side!=pSeg->side ){
184470 rc = 1;
184471 GEODEBUG(("Crossing: %d.%d and %d.%d\n",
184472 pPrev->side, pPrev->idx,
184473 pSeg->side, pSeg->idx));
184474 goto geopolyOverlapDone;
184475 }else if( pPrev->y!=pSeg->y ){
184476 GEODEBUG(("MASK: %d\n", iMask));
184477 aOverlap[iMask] = 1;
184478 }
184479 }
184480 iMask ^= pSeg->side;
184481 pPrev = pSeg;
184482 }
184483 }
184484 GEODEBUG(("%s %d.%d C=%g B=%g\n",
184485 pThisEvent->eType ? "RM " : "ADD",
184486 pThisEvent->pSeg->side, pThisEvent->pSeg->idx,
184487 pThisEvent->pSeg->C,
184488 pThisEvent->pSeg->B));
184489 if( pThisEvent->eType==0 ){
184490 /* Add a segment */
184491 pSeg = pThisEvent->pSeg;
184492 pSeg->y = pSeg->y0;
184493 pSeg->pNext = pActive;
184494 pActive = pSeg;
184495 needSort = 1;
184496 }else{
184497 /* Remove a segment */
184498 if( pActive==pThisEvent->pSeg ){
184499 pActive = pActive->pNext;
184500 }else{
184501 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
184502 if( pSeg->pNext==pThisEvent->pSeg ){
184503 pSeg->pNext = pSeg->pNext->pNext;
184504 break;
184505 }
184506 }
184507 }
184508 }
184509 pThisEvent = pThisEvent->pNext;
184510 }
184511 if( aOverlap[3]==0 ){
184512 rc = 0;
184513 }else if( aOverlap[1]!=0 && aOverlap[2]==0 ){
184514 rc = 3;
184515 }else if( aOverlap[1]==0 && aOverlap[2]!=0 ){
184516 rc = 2;
184517 }else if( aOverlap[1]==0 && aOverlap[2]==0 ){
184518 rc = 4;
184519 }else{
184520 rc = 1;
184521 }
184522
184523geopolyOverlapDone:
184524 sqlite3_free(p);
184525 return rc;
184526}
184527
184528/*
184529** SQL function: geopoly_overlap(P1,P2)
184530**
184531** Determine whether or not P1 and P2 overlap. Return value:
184532**
184533** 0 The two polygons are disjoint
184534** 1 They overlap
184535** 2 P1 is completely contained within P2
184536** 3 P2 is completely contained within P1
184537** 4 P1 and P2 are the same polygon
184538** NULL Either P1 or P2 or both are not valid polygons
184539*/
184540static void geopolyOverlapFunc(
184541 sqlite3_context *context,
184542 int argc,
184543 sqlite3_value **argv
184544){
184545 GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
184546 GeoPoly *p2 = geopolyFuncParam(context, argv[1], 0);
184547 if( p1 && p2 ){
184548 int x = geopolyOverlap(p1, p2);
184549 if( x<0 ){
184550 sqlite3_result_error_nomem(context);
184551 }else{
184552 sqlite3_result_int(context, x);
184553 }
184554 }
184555 sqlite3_free(p1);
184556 sqlite3_free(p2);
184557}
184558
184559/*
184560** Enable or disable debugging output
184561*/
184562static void geopolyDebugFunc(
184563 sqlite3_context *context,
184564 int argc,
184565 sqlite3_value **argv
184566){
184567#ifdef GEOPOLY_ENABLE_DEBUG
184568 geo_debug = sqlite3_value_int(argv[0]);
184569#endif
184570}
184571
184572/*
184573** This function is the implementation of both the xConnect and xCreate
184574** methods of the geopoly virtual table.
184575**
184576** argv[0] -> module name
184577** argv[1] -> database name
184578** argv[2] -> table name
184579** argv[...] -> column names...
184580*/
184581static int geopolyInit(
184582 sqlite3 *db, /* Database connection */
184583 void *pAux, /* One of the RTREE_COORD_* constants */
184584 int argc, const char *const*argv, /* Parameters to CREATE TABLE statement */
184585 sqlite3_vtab **ppVtab, /* OUT: New virtual table */
184586 char **pzErr, /* OUT: Error message, if any */
184587 int isCreate /* True for xCreate, false for xConnect */
184588){
184589 int rc = SQLITE_OK;
184590 Rtree *pRtree;
184591 int nDb; /* Length of string argv[1] */
184592 int nName; /* Length of string argv[2] */
184593 sqlite3_str *pSql;
184594 char *zSql;
184595 int ii;
184596
184597 sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
184598
184599 /* Allocate the sqlite3_vtab structure */
184600 nDb = (int)strlen(argv[1]);
184601 nName = (int)strlen(argv[2]);
184602 pRtree = (Rtree *)sqlite3_malloc(sizeof(Rtree)+nDb+nName+2);
184603 if( !pRtree ){
184604 return SQLITE_NOMEM;
184605 }
184606 memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
184607 pRtree->nBusy = 1;
184608 pRtree->base.pModule = &rtreeModule;
184609 pRtree->zDb = (char *)&pRtree[1];
184610 pRtree->zName = &pRtree->zDb[nDb+1];
184611 pRtree->eCoordType = RTREE_COORD_REAL32;
184612 pRtree->nDim = 2;
184613 pRtree->nDim2 = 4;
184614 memcpy(pRtree->zDb, argv[1], nDb);
184615 memcpy(pRtree->zName, argv[2], nName);
184616
184617
184618 /* Create/Connect to the underlying relational database schema. If
184619 ** that is successful, call sqlite3_declare_vtab() to configure
184620 ** the r-tree table schema.
184621 */
184622 pSql = sqlite3_str_new(db);
184623 sqlite3_str_appendf(pSql, "CREATE TABLE x(_shape");
184624 pRtree->nAux = 1; /* Add one for _shape */
184625 pRtree->nAuxNotNull = 1; /* The _shape column is always not-null */
184626 for(ii=3; ii<argc; ii++){
184627 pRtree->nAux++;
184628 sqlite3_str_appendf(pSql, ",%s", argv[ii]);
184629 }
184630 sqlite3_str_appendf(pSql, ");");
184631 zSql = sqlite3_str_finish(pSql);
184632 if( !zSql ){
184633 rc = SQLITE_NOMEM;
184634 }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
184635 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
184636 }
184637 sqlite3_free(zSql);
184638 if( rc ) goto geopolyInit_fail;
184639 pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
184640
184641 /* Figure out the node size to use. */
184642 rc = getNodeSize(db, pRtree, isCreate, pzErr);
184643 if( rc ) goto geopolyInit_fail;
184644 rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate);
184645 if( rc ){
184646 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
184647 goto geopolyInit_fail;
184648 }
184649
184650 *ppVtab = (sqlite3_vtab *)pRtree;
184651 return SQLITE_OK;
184652
184653geopolyInit_fail:
184654 if( rc==SQLITE_OK ) rc = SQLITE_ERROR;
184655 assert( *ppVtab==0 );
184656 assert( pRtree->nBusy==1 );
184657 rtreeRelease(pRtree);
184658 return rc;
184659}
184660
184661
184662/*
184663** GEOPOLY virtual table module xCreate method.
184664*/
184665static int geopolyCreate(
184666 sqlite3 *db,
184667 void *pAux,
184668 int argc, const char *const*argv,
184669 sqlite3_vtab **ppVtab,
184670 char **pzErr
184671){
184672 return geopolyInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
184673}
184674
184675/*
184676** GEOPOLY virtual table module xConnect method.
184677*/
184678static int geopolyConnect(
184679 sqlite3 *db,
184680 void *pAux,
184681 int argc, const char *const*argv,
184682 sqlite3_vtab **ppVtab,
184683 char **pzErr
184684){
184685 return geopolyInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
184686}
184687
184688
184689/*
184690** GEOPOLY virtual table module xFilter method.
184691**
184692** Query plans:
184693**
184694** 1 rowid lookup
184695** 2 search for objects overlapping the same bounding box
184696** that contains polygon argv[0]
184697** 3 search for objects overlapping the same bounding box
184698** that contains polygon argv[0]
184699** 4 full table scan
184700*/
184701static int geopolyFilter(
184702 sqlite3_vtab_cursor *pVtabCursor, /* The cursor to initialize */
184703 int idxNum, /* Query plan */
184704 const char *idxStr, /* Not Used */
184705 int argc, sqlite3_value **argv /* Parameters to the query plan */
184706){
184707 Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
184708 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
184709 RtreeNode *pRoot = 0;
184710 int rc = SQLITE_OK;
184711 int iCell = 0;
184712 sqlite3_stmt *pStmt;
184713
184714 rtreeReference(pRtree);
184715
184716 /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
184717 freeCursorConstraints(pCsr);
184718 sqlite3_free(pCsr->aPoint);
184719 pStmt = pCsr->pReadAux;
184720 memset(pCsr, 0, sizeof(RtreeCursor));
184721 pCsr->base.pVtab = (sqlite3_vtab*)pRtree;
184722 pCsr->pReadAux = pStmt;
184723
184724 pCsr->iStrategy = idxNum;
184725 if( idxNum==1 ){
184726 /* Special case - lookup by rowid. */
184727 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
184728 RtreeSearchPoint *p; /* Search point for the leaf */
184729 i64 iRowid = sqlite3_value_int64(argv[0]);
184730 i64 iNode = 0;
184731 rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
184732 if( rc==SQLITE_OK && pLeaf!=0 ){
184733 p = rtreeSearchPointNew(pCsr, RTREE_ZERO, 0);
184734 assert( p!=0 ); /* Always returns pCsr->sPoint */
184735 pCsr->aNode[0] = pLeaf;
184736 p->id = iNode;
184737 p->eWithin = PARTLY_WITHIN;
184738 rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
184739 p->iCell = (u8)iCell;
184740 RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
184741 }else{
184742 pCsr->atEOF = 1;
184743 }
184744 }else{
184745 /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
184746 ** with the configured constraints.
184747 */
184748 rc = nodeAcquire(pRtree, 1, 0, &pRoot);
184749 if( rc==SQLITE_OK && idxNum<=3 ){
184750 RtreeCoord bbox[4];
184751 RtreeConstraint *p;
184752 assert( argc==1 );
184753 geopolyBBox(0, argv[0], bbox, &rc);
184754 if( rc ){
184755 goto geopoly_filter_end;
184756 }
184757 pCsr->aConstraint = p = sqlite3_malloc(sizeof(RtreeConstraint)*4);
184758 pCsr->nConstraint = 4;
184759 if( p==0 ){
184760 rc = SQLITE_NOMEM;
184761 }else{
184762 memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*4);
184763 memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1));
184764 if( idxNum==2 ){
184765 /* Overlap query */
184766 p->op = 'B';
184767 p->iCoord = 0;
184768 p->u.rValue = bbox[1].f;
184769 p++;
184770 p->op = 'D';
184771 p->iCoord = 1;
184772 p->u.rValue = bbox[0].f;
184773 p++;
184774 p->op = 'B';
184775 p->iCoord = 2;
184776 p->u.rValue = bbox[3].f;
184777 p++;
184778 p->op = 'D';
184779 p->iCoord = 3;
184780 p->u.rValue = bbox[2].f;
184781 }else{
184782 /* Within query */
184783 p->op = 'D';
184784 p->iCoord = 0;
184785 p->u.rValue = bbox[0].f;
184786 p++;
184787 p->op = 'B';
184788 p->iCoord = 1;
184789 p->u.rValue = bbox[1].f;
184790 p++;
184791 p->op = 'D';
184792 p->iCoord = 2;
184793 p->u.rValue = bbox[2].f;
184794 p++;
184795 p->op = 'B';
184796 p->iCoord = 3;
184797 p->u.rValue = bbox[3].f;
184798 }
184799 }
184800 }
184801 if( rc==SQLITE_OK ){
184802 RtreeSearchPoint *pNew;
184803 pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, (u8)(pRtree->iDepth+1));
184804 if( pNew==0 ){
184805 rc = SQLITE_NOMEM;
184806 goto geopoly_filter_end;
184807 }
184808 pNew->id = 1;
184809 pNew->iCell = 0;
184810 pNew->eWithin = PARTLY_WITHIN;
184811 assert( pCsr->bPoint==1 );
184812 pCsr->aNode[0] = pRoot;
184813 pRoot = 0;
184814 RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
184815 rc = rtreeStepToLeaf(pCsr);
184816 }
184817 }
184818
184819geopoly_filter_end:
184820 nodeRelease(pRtree, pRoot);
184821 rtreeRelease(pRtree);
184822 return rc;
184823}
184824
184825/*
184826** Rtree virtual table module xBestIndex method. There are three
184827** table scan strategies to choose from (in order from most to
184828** least desirable):
184829**
184830** idxNum idxStr Strategy
184831** ------------------------------------------------
184832** 1 "rowid" Direct lookup by rowid.
184833** 2 "rtree" R-tree overlap query using geopoly_overlap()
184834** 3 "rtree" R-tree within query using geopoly_within()
184835** 4 "fullscan" full-table scan.
184836** ------------------------------------------------
184837*/
184838static int geopolyBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
184839 int ii;
184840 int iRowidTerm = -1;
184841 int iFuncTerm = -1;
184842 int idxNum = 0;
184843
184844 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
184845 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
184846 if( !p->usable ) continue;
184847 if( p->iColumn<0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
184848 iRowidTerm = ii;
184849 break;
184850 }
184851 if( p->iColumn==0 && p->op>=SQLITE_INDEX_CONSTRAINT_FUNCTION ){
184852 /* p->op==SQLITE_INDEX_CONSTRAINT_FUNCTION for geopoly_overlap()
184853 ** p->op==(SQLITE_INDEX_CONTRAINT_FUNCTION+1) for geopoly_within().
184854 ** See geopolyFindFunction() */
184855 iFuncTerm = ii;
184856 idxNum = p->op - SQLITE_INDEX_CONSTRAINT_FUNCTION + 2;
184857 }
184858 }
184859
184860 if( iRowidTerm>=0 ){
184861 pIdxInfo->idxNum = 1;
184862 pIdxInfo->idxStr = "rowid";
184863 pIdxInfo->aConstraintUsage[iRowidTerm].argvIndex = 1;
184864 pIdxInfo->aConstraintUsage[iRowidTerm].omit = 1;
184865 pIdxInfo->estimatedCost = 30.0;
184866 pIdxInfo->estimatedRows = 1;
184867 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
184868 return SQLITE_OK;
184869 }
184870 if( iFuncTerm>=0 ){
184871 pIdxInfo->idxNum = idxNum;
184872 pIdxInfo->idxStr = "rtree";
184873 pIdxInfo->aConstraintUsage[iFuncTerm].argvIndex = 1;
184874 pIdxInfo->aConstraintUsage[iFuncTerm].omit = 0;
184875 pIdxInfo->estimatedCost = 300.0;
184876 pIdxInfo->estimatedRows = 10;
184877 return SQLITE_OK;
184878 }
184879 pIdxInfo->idxNum = 4;
184880 pIdxInfo->idxStr = "fullscan";
184881 pIdxInfo->estimatedCost = 3000000.0;
184882 pIdxInfo->estimatedRows = 100000;
184883 return SQLITE_OK;
184884}
184885
184886
184887/*
184888** GEOPOLY virtual table module xColumn method.
184889*/
184890static int geopolyColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
184891 Rtree *pRtree = (Rtree *)cur->pVtab;
184892 RtreeCursor *pCsr = (RtreeCursor *)cur;
184893 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
184894 int rc = SQLITE_OK;
184895 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
184896
184897 if( rc ) return rc;
184898 if( p==0 ) return SQLITE_OK;
184899 if( i==0 && sqlite3_vtab_nochange(ctx) ) return SQLITE_OK;
184900 if( i<=pRtree->nAux ){
184901 if( !pCsr->bAuxValid ){
184902 if( pCsr->pReadAux==0 ){
184903 rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0,
184904 &pCsr->pReadAux, 0);
184905 if( rc ) return rc;
184906 }
184907 sqlite3_bind_int64(pCsr->pReadAux, 1,
184908 nodeGetRowid(pRtree, pNode, p->iCell));
184909 rc = sqlite3_step(pCsr->pReadAux);
184910 if( rc==SQLITE_ROW ){
184911 pCsr->bAuxValid = 1;
184912 }else{
184913 sqlite3_reset(pCsr->pReadAux);
184914 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
184915 return rc;
184916 }
184917 }
184918 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pReadAux, i+2));
184919 }
184920 return SQLITE_OK;
184921}
184922
184923
184924/*
184925** The xUpdate method for GEOPOLY module virtual tables.
184926**
184927** For DELETE:
184928**
184929** argv[0] = the rowid to be deleted
184930**
184931** For INSERT:
184932**
184933** argv[0] = SQL NULL
184934** argv[1] = rowid to insert, or an SQL NULL to select automatically
184935** argv[2] = _shape column
184936** argv[3] = first application-defined column....
184937**
184938** For UPDATE:
184939**
184940** argv[0] = rowid to modify. Never NULL
184941** argv[1] = rowid after the change. Never NULL
184942** argv[2] = new value for _shape
184943** argv[3] = new value for first application-defined column....
184944*/
184945static int geopolyUpdate(
184946 sqlite3_vtab *pVtab,
184947 int nData,
184948 sqlite3_value **aData,
184949 sqlite_int64 *pRowid
184950){
184951 Rtree *pRtree = (Rtree *)pVtab;
184952 int rc = SQLITE_OK;
184953 RtreeCell cell; /* New cell to insert if nData>1 */
184954 i64 oldRowid; /* The old rowid */
184955 int oldRowidValid; /* True if oldRowid is valid */
184956 i64 newRowid; /* The new rowid */
184957 int newRowidValid; /* True if newRowid is valid */
184958 int coordChange = 0; /* Change in coordinates */
184959
184960 if( pRtree->nNodeRef ){
184961 /* Unable to write to the btree while another cursor is reading from it,
184962 ** since the write might do a rebalance which would disrupt the read
184963 ** cursor. */
184964 return SQLITE_LOCKED_VTAB;
184965 }
184966 rtreeReference(pRtree);
184967 assert(nData>=1);
184968
184969 oldRowidValid = sqlite3_value_type(aData[0])!=SQLITE_NULL;;
184970 oldRowid = oldRowidValid ? sqlite3_value_int64(aData[0]) : 0;
184971 newRowidValid = nData>1 && sqlite3_value_type(aData[1])!=SQLITE_NULL;
184972 newRowid = newRowidValid ? sqlite3_value_int64(aData[1]) : 0;
184973 cell.iRowid = newRowid;
184974
184975 if( nData>1 /* not a DELETE */
184976 && (!oldRowidValid /* INSERT */
184977 || !sqlite3_value_nochange(aData[2]) /* UPDATE _shape */
184978 || oldRowid!=newRowid) /* Rowid change */
184979 ){
184980 geopolyBBox(0, aData[2], cell.aCoord, &rc);
184981 if( rc ){
184982 if( rc==SQLITE_ERROR ){
184983 pVtab->zErrMsg =
184984 sqlite3_mprintf("_shape does not contain a valid polygon");
184985 }
184986 goto geopoly_update_end;
184987 }
184988 coordChange = 1;
184989
184990 /* If a rowid value was supplied, check if it is already present in
184991 ** the table. If so, the constraint has failed. */
184992 if( newRowidValid && (!oldRowidValid || oldRowid!=newRowid) ){
184993 int steprc;
184994 sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
184995 steprc = sqlite3_step(pRtree->pReadRowid);
184996 rc = sqlite3_reset(pRtree->pReadRowid);
184997 if( SQLITE_ROW==steprc ){
184998 if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){
184999 rc = rtreeDeleteRowid(pRtree, cell.iRowid);
185000 }else{
185001 rc = rtreeConstraintError(pRtree, 0);
185002 }
185003 }
185004 }
185005 }
185006
185007 /* If aData[0] is not an SQL NULL value, it is the rowid of a
185008 ** record to delete from the r-tree table. The following block does
185009 ** just that.
185010 */
185011 if( rc==SQLITE_OK && (nData==1 || (coordChange && oldRowidValid)) ){
185012 rc = rtreeDeleteRowid(pRtree, oldRowid);
185013 }
185014
185015 /* If the aData[] array contains more than one element, elements
185016 ** (aData[2]..aData[argc-1]) contain a new record to insert into
185017 ** the r-tree structure.
185018 */
185019 if( rc==SQLITE_OK && nData>1 && coordChange ){
185020 /* Insert the new record into the r-tree */
185021 RtreeNode *pLeaf = 0;
185022 if( !newRowidValid ){
185023 rc = rtreeNewRowid(pRtree, &cell.iRowid);
185024 }
185025 *pRowid = cell.iRowid;
185026 if( rc==SQLITE_OK ){
185027 rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
185028 }
185029 if( rc==SQLITE_OK ){
185030 int rc2;
185031 pRtree->iReinsertHeight = -1;
185032 rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
185033 rc2 = nodeRelease(pRtree, pLeaf);
185034 if( rc==SQLITE_OK ){
185035 rc = rc2;
185036 }
185037 }
185038 }
185039
185040 /* Change the data */
185041 if( rc==SQLITE_OK && nData>1 ){
185042 sqlite3_stmt *pUp = pRtree->pWriteAux;
185043 int jj;
185044 int nChange = 0;
185045 sqlite3_bind_int64(pUp, 1, cell.iRowid);
185046 assert( pRtree->nAux>=1 );
185047 if( sqlite3_value_nochange(aData[2]) ){
185048 sqlite3_bind_null(pUp, 2);
185049 }else{
185050 GeoPoly *p = 0;
185051 if( sqlite3_value_type(aData[2])==SQLITE_TEXT
185052 && (p = geopolyFuncParam(0, aData[2], &rc))!=0
185053 && rc==SQLITE_OK
185054 ){
185055 sqlite3_bind_blob(pUp, 2, p->hdr, 4+8*p->nVertex, SQLITE_TRANSIENT);
185056 }else{
185057 sqlite3_bind_value(pUp, 2, aData[2]);
185058 }
185059 sqlite3_free(p);
185060 nChange = 1;
185061 }
185062 for(jj=1; jj<pRtree->nAux; jj++){
185063 nChange++;
185064 sqlite3_bind_value(pUp, jj+2, aData[jj+2]);
185065 }
185066 if( nChange ){
185067 sqlite3_step(pUp);
185068 rc = sqlite3_reset(pUp);
185069 }
185070 }
185071
185072geopoly_update_end:
185073 rtreeRelease(pRtree);
185074 return rc;
185075}
185076
185077/*
185078** Report that geopoly_overlap() is an overloaded function suitable
185079** for use in xBestIndex.
185080*/
185081static int geopolyFindFunction(
185082 sqlite3_vtab *pVtab,
185083 int nArg,
185084 const char *zName,
185085 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
185086 void **ppArg
185087){
185088 if( sqlite3_stricmp(zName, "geopoly_overlap")==0 ){
185089 *pxFunc = geopolyOverlapFunc;
185090 *ppArg = 0;
185091 return SQLITE_INDEX_CONSTRAINT_FUNCTION;
185092 }
185093 if( sqlite3_stricmp(zName, "geopoly_within")==0 ){
185094 *pxFunc = geopolyWithinFunc;
185095 *ppArg = 0;
185096 return SQLITE_INDEX_CONSTRAINT_FUNCTION+1;
185097 }
185098 return 0;
185099}
185100
185101
185102static sqlite3_module geopolyModule = {
185103 3, /* iVersion */
185104 geopolyCreate, /* xCreate - create a table */
185105 geopolyConnect, /* xConnect - connect to an existing table */
185106 geopolyBestIndex, /* xBestIndex - Determine search strategy */
185107 rtreeDisconnect, /* xDisconnect - Disconnect from a table */
185108 rtreeDestroy, /* xDestroy - Drop a table */
185109 rtreeOpen, /* xOpen - open a cursor */
185110 rtreeClose, /* xClose - close a cursor */
185111 geopolyFilter, /* xFilter - configure scan constraints */
185112 rtreeNext, /* xNext - advance a cursor */
185113 rtreeEof, /* xEof */
185114 geopolyColumn, /* xColumn - read data */
185115 rtreeRowid, /* xRowid - read data */
185116 geopolyUpdate, /* xUpdate - write data */
185117 rtreeBeginTransaction, /* xBegin - begin transaction */
185118 rtreeEndTransaction, /* xSync - sync transaction */
185119 rtreeEndTransaction, /* xCommit - commit transaction */
185120 rtreeEndTransaction, /* xRollback - rollback transaction */
185121 geopolyFindFunction, /* xFindFunction - function overloading */
185122 rtreeRename, /* xRename - rename the table */
185123 rtreeSavepoint, /* xSavepoint */
185124 0, /* xRelease */
185125 0, /* xRollbackTo */
185126 rtreeShadowName /* xShadowName */
185127};
185128
185129static int sqlite3_geopoly_init(sqlite3 *db){
185130 int rc = SQLITE_OK;
185131 static const struct {
185132 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
185133 signed char nArg;
185134 unsigned char bPure;
185135 const char *zName;
185136 } aFunc[] = {
185137 { geopolyAreaFunc, 1, 1, "geopoly_area" },
185138 { geopolyBlobFunc, 1, 1, "geopoly_blob" },
185139 { geopolyJsonFunc, 1, 1, "geopoly_json" },
185140 { geopolySvgFunc, -1, 1, "geopoly_svg" },
185141 { geopolyWithinFunc, 2, 1, "geopoly_within" },
185142 { geopolyContainsPointFunc, 3, 1, "geopoly_contains_point" },
185143 { geopolyOverlapFunc, 2, 1, "geopoly_overlap" },
185144 { geopolyDebugFunc, 1, 0, "geopoly_debug" },
185145 { geopolyBBoxFunc, 1, 1, "geopoly_bbox" },
185146 { geopolyXformFunc, 7, 1, "geopoly_xform" },
185147 { geopolyRegularFunc, 4, 1, "geopoly_regular" },
185148 { geopolyCcwFunc, 1, 1, "geopoly_ccw" },
185149 };
185150 static const struct {
185151 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
185152 void (*xFinal)(sqlite3_context*);
185153 const char *zName;
185154 } aAgg[] = {
185155 { geopolyBBoxStep, geopolyBBoxFinal, "geopoly_group_bbox" },
185156 };
185157 int i;
185158 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
185159 int enc = aFunc[i].bPure ? SQLITE_UTF8|SQLITE_DETERMINISTIC : SQLITE_UTF8;
185160 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
185161 enc, 0,
185162 aFunc[i].xFunc, 0, 0);
185163 }
185164 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
185165 rc = sqlite3_create_function(db, aAgg[i].zName, 1, SQLITE_UTF8, 0,
185166 0, aAgg[i].xStep, aAgg[i].xFinal);
185167 }
185168 if( rc==SQLITE_OK ){
185169 rc = sqlite3_create_module_v2(db, "geopoly", &geopolyModule, 0, 0);
185170 }
185171 return rc;
185172}
185173
185174/************** End of geopoly.c *********************************************/
185175/************** Continuing where we left off in rtree.c **********************/
185176#endif
185177
185178/*
185179** Register the r-tree module with database handle db. This creates the
185180** virtual table module "rtree" and the debugging/analysis scalar
185181** function "rtreenode".
185182*/
185183SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db){
185184 const int utf8 = SQLITE_UTF8;
185185 int rc;
185186
185187 rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
185188 if( rc==SQLITE_OK ){
185189 rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
185190 }
185191 if( rc==SQLITE_OK ){
185192 rc = sqlite3_create_function(db, "rtreecheck", -1, utf8, 0,rtreecheck, 0,0);
185193 }
185194 if( rc==SQLITE_OK ){
185195#ifdef SQLITE_RTREE_INT_ONLY
185196 void *c = (void *)RTREE_COORD_INT32;
185197#else
185198 void *c = (void *)RTREE_COORD_REAL32;
185199#endif
185200 rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
185201 }
185202 if( rc==SQLITE_OK ){
185203 void *c = (void *)RTREE_COORD_INT32;
185204 rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
185205 }
185206#ifdef SQLITE_ENABLE_GEOPOLY
185207 if( rc==SQLITE_OK ){
185208 rc = sqlite3_geopoly_init(db);
185209 }
185210#endif
185211
185212 return rc;
185213}
185214
185215/*
185216** This routine deletes the RtreeGeomCallback object that was attached
185217** one of the SQL functions create by sqlite3_rtree_geometry_callback()
185218** or sqlite3_rtree_query_callback(). In other words, this routine is the
185219** destructor for an RtreeGeomCallback objecct. This routine is called when
185220** the corresponding SQL function is deleted.
185221*/
185222static void rtreeFreeCallback(void *p){
185223 RtreeGeomCallback *pInfo = (RtreeGeomCallback*)p;
185224 if( pInfo->xDestructor ) pInfo->xDestructor(pInfo->pContext);
185225 sqlite3_free(p);
185226}
185227
185228/*
185229** This routine frees the BLOB that is returned by geomCallback().
185230*/
185231static void rtreeMatchArgFree(void *pArg){
185232 int i;
185233 RtreeMatchArg *p = (RtreeMatchArg*)pArg;
185234 for(i=0; i<p->nParam; i++){
185235 sqlite3_value_free(p->apSqlParam[i]);
185236 }
185237 sqlite3_free(p);
185238}
185239
185240/*
185241** Each call to sqlite3_rtree_geometry_callback() or
185242** sqlite3_rtree_query_callback() creates an ordinary SQLite
185243** scalar function that is implemented by this routine.
185244**
185245** All this function does is construct an RtreeMatchArg object that
185246** contains the geometry-checking callback routines and a list of
185247** parameters to this function, then return that RtreeMatchArg object
185248** as a BLOB.
185249**
185250** The R-Tree MATCH operator will read the returned BLOB, deserialize
185251** the RtreeMatchArg object, and use the RtreeMatchArg object to figure
185252** out which elements of the R-Tree should be returned by the query.
185253*/
185254static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
185255 RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
185256 RtreeMatchArg *pBlob;
185257 int nBlob;
185258 int memErr = 0;
185259
185260 nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue)
185261 + nArg*sizeof(sqlite3_value*);
185262 pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob);
185263 if( !pBlob ){
185264 sqlite3_result_error_nomem(ctx);
185265 }else{
185266 int i;
185267 pBlob->iSize = nBlob;
185268 pBlob->cb = pGeomCtx[0];
185269 pBlob->apSqlParam = (sqlite3_value**)&pBlob->aParam[nArg];
185270 pBlob->nParam = nArg;
185271 for(i=0; i<nArg; i++){
185272 pBlob->apSqlParam[i] = sqlite3_value_dup(aArg[i]);
185273 if( pBlob->apSqlParam[i]==0 ) memErr = 1;
185274#ifdef SQLITE_RTREE_INT_ONLY
185275 pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
185276#else
185277 pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
185278#endif
185279 }
185280 if( memErr ){
185281 sqlite3_result_error_nomem(ctx);
185282 rtreeMatchArgFree(pBlob);
185283 }else{
185284 sqlite3_result_pointer(ctx, pBlob, "RtreeMatchArg", rtreeMatchArgFree);
185285 }
185286 }
185287}
185288
185289/*
185290** Register a new geometry function for use with the r-tree MATCH operator.
185291*/
185292SQLITE_API int sqlite3_rtree_geometry_callback(
185293 sqlite3 *db, /* Register SQL function on this connection */
185294 const char *zGeom, /* Name of the new SQL function */
185295 int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*), /* Callback */
185296 void *pContext /* Extra data associated with the callback */
185297){
185298 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
185299
185300 /* Allocate and populate the context object. */
185301 pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
185302 if( !pGeomCtx ) return SQLITE_NOMEM;
185303 pGeomCtx->xGeom = xGeom;
185304 pGeomCtx->xQueryFunc = 0;
185305 pGeomCtx->xDestructor = 0;
185306 pGeomCtx->pContext = pContext;
185307 return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY,
185308 (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback
185309 );
185310}
185311
185312/*
185313** Register a new 2nd-generation geometry function for use with the
185314** r-tree MATCH operator.
185315*/
185316SQLITE_API int sqlite3_rtree_query_callback(
185317 sqlite3 *db, /* Register SQL function on this connection */
185318 const char *zQueryFunc, /* Name of new SQL function */
185319 int (*xQueryFunc)(sqlite3_rtree_query_info*), /* Callback */
185320 void *pContext, /* Extra data passed into the callback */
185321 void (*xDestructor)(void*) /* Destructor for the extra data */
185322){
185323 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
185324
185325 /* Allocate and populate the context object. */
185326 pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
185327 if( !pGeomCtx ) return SQLITE_NOMEM;
185328 pGeomCtx->xGeom = 0;
185329 pGeomCtx->xQueryFunc = xQueryFunc;
185330 pGeomCtx->xDestructor = xDestructor;
185331 pGeomCtx->pContext = pContext;
185332 return sqlite3_create_function_v2(db, zQueryFunc, -1, SQLITE_ANY,
185333 (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback
185334 );
185335}
185336
185337#if !SQLITE_CORE
185338#ifdef _WIN32
185339__declspec(dllexport)
185340#endif
185341SQLITE_API int sqlite3_rtree_init(
185342 sqlite3 *db,
185343 char **pzErrMsg,
185344 const sqlite3_api_routines *pApi
185345){
185346 SQLITE_EXTENSION_INIT2(pApi)
185347 return sqlite3RtreeInit(db);
185348}
185349#endif
185350
185351#endif
185352
185353/************** End of rtree.c ***********************************************/
185354/************** Begin file icu.c *********************************************/
185355/*
185356** 2007 May 6
185357**
185358** The author disclaims copyright to this source code. In place of
185359** a legal notice, here is a blessing:
185360**
185361** May you do good and not evil.
185362** May you find forgiveness for yourself and forgive others.
185363** May you share freely, never taking more than you give.
185364**
185365*************************************************************************
185366** $Id: icu.c,v 1.7 2007/12/13 21:54:11 drh Exp $
185367**
185368** This file implements an integration between the ICU library
185369** ("International Components for Unicode", an open-source library
185370** for handling unicode data) and SQLite. The integration uses
185371** ICU to provide the following to SQLite:
185372**
185373** * An implementation of the SQL regexp() function (and hence REGEXP
185374** operator) using the ICU uregex_XX() APIs.
185375**
185376** * Implementations of the SQL scalar upper() and lower() functions
185377** for case mapping.
185378**
185379** * Integration of ICU and SQLite collation sequences.
185380**
185381** * An implementation of the LIKE operator that uses ICU to
185382** provide case-independent matching.
185383*/
185384
185385#if !defined(SQLITE_CORE) \
185386 || defined(SQLITE_ENABLE_ICU) \
185387 || defined(SQLITE_ENABLE_ICU_COLLATIONS)
185388
185389/* Include ICU headers */
185390#include <unicode/utypes.h>
185391#include <unicode/uregex.h>
185392#include <unicode/ustring.h>
185393#include <unicode/ucol.h>
185394
185395/* #include <assert.h> */
185396
185397#ifndef SQLITE_CORE
185398/* #include "sqlite3ext.h" */
185399 SQLITE_EXTENSION_INIT1
185400#else
185401/* #include "sqlite3.h" */
185402#endif
185403
185404/*
185405** This function is called when an ICU function called from within
185406** the implementation of an SQL scalar function returns an error.
185407**
185408** The scalar function context passed as the first argument is
185409** loaded with an error message based on the following two args.
185410*/
185411static void icuFunctionError(
185412 sqlite3_context *pCtx, /* SQLite scalar function context */
185413 const char *zName, /* Name of ICU function that failed */
185414 UErrorCode e /* Error code returned by ICU function */
185415){
185416 char zBuf[128];
185417 sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
185418 zBuf[127] = '\0';
185419 sqlite3_result_error(pCtx, zBuf, -1);
185420}
185421
185422#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
185423
185424/*
185425** Maximum length (in bytes) of the pattern in a LIKE or GLOB
185426** operator.
185427*/
185428#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
185429# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
185430#endif
185431
185432/*
185433** Version of sqlite3_free() that is always a function, never a macro.
185434*/
185435static void xFree(void *p){
185436 sqlite3_free(p);
185437}
185438
185439/*
185440** This lookup table is used to help decode the first byte of
185441** a multi-byte UTF8 character. It is copied here from SQLite source
185442** code file utf8.c.
185443*/
185444static const unsigned char icuUtf8Trans1[] = {
185445 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
185446 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
185447 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
185448 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
185449 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
185450 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
185451 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
185452 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
185453};
185454
185455#define SQLITE_ICU_READ_UTF8(zIn, c) \
185456 c = *(zIn++); \
185457 if( c>=0xc0 ){ \
185458 c = icuUtf8Trans1[c-0xc0]; \
185459 while( (*zIn & 0xc0)==0x80 ){ \
185460 c = (c<<6) + (0x3f & *(zIn++)); \
185461 } \
185462 }
185463
185464#define SQLITE_ICU_SKIP_UTF8(zIn) \
185465 assert( *zIn ); \
185466 if( *(zIn++)>=0xc0 ){ \
185467 while( (*zIn & 0xc0)==0x80 ){zIn++;} \
185468 }
185469
185470
185471/*
185472** Compare two UTF-8 strings for equality where the first string is
185473** a "LIKE" expression. Return true (1) if they are the same and
185474** false (0) if they are different.
185475*/
185476static int icuLikeCompare(
185477 const uint8_t *zPattern, /* LIKE pattern */
185478 const uint8_t *zString, /* The UTF-8 string to compare against */
185479 const UChar32 uEsc /* The escape character */
185480){
185481 static const uint32_t MATCH_ONE = (uint32_t)'_';
185482 static const uint32_t MATCH_ALL = (uint32_t)'%';
185483
185484 int prevEscape = 0; /* True if the previous character was uEsc */
185485
185486 while( 1 ){
185487
185488 /* Read (and consume) the next character from the input pattern. */
185489 uint32_t uPattern;
185490 SQLITE_ICU_READ_UTF8(zPattern, uPattern);
185491 if( uPattern==0 ) break;
185492
185493 /* There are now 4 possibilities:
185494 **
185495 ** 1. uPattern is an unescaped match-all character "%",
185496 ** 2. uPattern is an unescaped match-one character "_",
185497 ** 3. uPattern is an unescaped escape character, or
185498 ** 4. uPattern is to be handled as an ordinary character
185499 */
185500 if( !prevEscape && uPattern==MATCH_ALL ){
185501 /* Case 1. */
185502 uint8_t c;
185503
185504 /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
185505 ** MATCH_ALL. For each MATCH_ONE, skip one character in the
185506 ** test string.
185507 */
185508 while( (c=*zPattern) == MATCH_ALL || c == MATCH_ONE ){
185509 if( c==MATCH_ONE ){
185510 if( *zString==0 ) return 0;
185511 SQLITE_ICU_SKIP_UTF8(zString);
185512 }
185513 zPattern++;
185514 }
185515
185516 if( *zPattern==0 ) return 1;
185517
185518 while( *zString ){
185519 if( icuLikeCompare(zPattern, zString, uEsc) ){
185520 return 1;
185521 }
185522 SQLITE_ICU_SKIP_UTF8(zString);
185523 }
185524 return 0;
185525
185526 }else if( !prevEscape && uPattern==MATCH_ONE ){
185527 /* Case 2. */
185528 if( *zString==0 ) return 0;
185529 SQLITE_ICU_SKIP_UTF8(zString);
185530
185531 }else if( !prevEscape && uPattern==(uint32_t)uEsc){
185532 /* Case 3. */
185533 prevEscape = 1;
185534
185535 }else{
185536 /* Case 4. */
185537 uint32_t uString;
185538 SQLITE_ICU_READ_UTF8(zString, uString);
185539 uString = (uint32_t)u_foldCase((UChar32)uString, U_FOLD_CASE_DEFAULT);
185540 uPattern = (uint32_t)u_foldCase((UChar32)uPattern, U_FOLD_CASE_DEFAULT);
185541 if( uString!=uPattern ){
185542 return 0;
185543 }
185544 prevEscape = 0;
185545 }
185546 }
185547
185548 return *zString==0;
185549}
185550
185551/*
185552** Implementation of the like() SQL function. This function implements
185553** the build-in LIKE operator. The first argument to the function is the
185554** pattern and the second argument is the string. So, the SQL statements:
185555**
185556** A LIKE B
185557**
185558** is implemented as like(B, A). If there is an escape character E,
185559**
185560** A LIKE B ESCAPE E
185561**
185562** is mapped to like(B, A, E).
185563*/
185564static void icuLikeFunc(
185565 sqlite3_context *context,
185566 int argc,
185567 sqlite3_value **argv
185568){
185569 const unsigned char *zA = sqlite3_value_text(argv[0]);
185570 const unsigned char *zB = sqlite3_value_text(argv[1]);
185571 UChar32 uEsc = 0;
185572
185573 /* Limit the length of the LIKE or GLOB pattern to avoid problems
185574 ** of deep recursion and N*N behavior in patternCompare().
185575 */
185576 if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){
185577 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
185578 return;
185579 }
185580
185581
185582 if( argc==3 ){
185583 /* The escape character string must consist of a single UTF-8 character.
185584 ** Otherwise, return an error.
185585 */
185586 int nE= sqlite3_value_bytes(argv[2]);
185587 const unsigned char *zE = sqlite3_value_text(argv[2]);
185588 int i = 0;
185589 if( zE==0 ) return;
185590 U8_NEXT(zE, i, nE, uEsc);
185591 if( i!=nE){
185592 sqlite3_result_error(context,
185593 "ESCAPE expression must be a single character", -1);
185594 return;
185595 }
185596 }
185597
185598 if( zA && zB ){
185599 sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc));
185600 }
185601}
185602
185603/*
185604** Function to delete compiled regexp objects. Registered as
185605** a destructor function with sqlite3_set_auxdata().
185606*/
185607static void icuRegexpDelete(void *p){
185608 URegularExpression *pExpr = (URegularExpression *)p;
185609 uregex_close(pExpr);
185610}
185611
185612/*
185613** Implementation of SQLite REGEXP operator. This scalar function takes
185614** two arguments. The first is a regular expression pattern to compile
185615** the second is a string to match against that pattern. If either
185616** argument is an SQL NULL, then NULL Is returned. Otherwise, the result
185617** is 1 if the string matches the pattern, or 0 otherwise.
185618**
185619** SQLite maps the regexp() function to the regexp() operator such
185620** that the following two are equivalent:
185621**
185622** zString REGEXP zPattern
185623** regexp(zPattern, zString)
185624**
185625** Uses the following ICU regexp APIs:
185626**
185627** uregex_open()
185628** uregex_matches()
185629** uregex_close()
185630*/
185631static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
185632 UErrorCode status = U_ZERO_ERROR;
185633 URegularExpression *pExpr;
185634 UBool res;
185635 const UChar *zString = sqlite3_value_text16(apArg[1]);
185636
185637 (void)nArg; /* Unused parameter */
185638
185639 /* If the left hand side of the regexp operator is NULL,
185640 ** then the result is also NULL.
185641 */
185642 if( !zString ){
185643 return;
185644 }
185645
185646 pExpr = sqlite3_get_auxdata(p, 0);
185647 if( !pExpr ){
185648 const UChar *zPattern = sqlite3_value_text16(apArg[0]);
185649 if( !zPattern ){
185650 return;
185651 }
185652 pExpr = uregex_open(zPattern, -1, 0, 0, &status);
185653
185654 if( U_SUCCESS(status) ){
185655 sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
185656 }else{
185657 assert(!pExpr);
185658 icuFunctionError(p, "uregex_open", status);
185659 return;
185660 }
185661 }
185662
185663 /* Configure the text that the regular expression operates on. */
185664 uregex_setText(pExpr, zString, -1, &status);
185665 if( !U_SUCCESS(status) ){
185666 icuFunctionError(p, "uregex_setText", status);
185667 return;
185668 }
185669
185670 /* Attempt the match */
185671 res = uregex_matches(pExpr, 0, &status);
185672 if( !U_SUCCESS(status) ){
185673 icuFunctionError(p, "uregex_matches", status);
185674 return;
185675 }
185676
185677 /* Set the text that the regular expression operates on to a NULL
185678 ** pointer. This is not really necessary, but it is tidier than
185679 ** leaving the regular expression object configured with an invalid
185680 ** pointer after this function returns.
185681 */
185682 uregex_setText(pExpr, 0, 0, &status);
185683
185684 /* Return 1 or 0. */
185685 sqlite3_result_int(p, res ? 1 : 0);
185686}
185687
185688/*
185689** Implementations of scalar functions for case mapping - upper() and
185690** lower(). Function upper() converts its input to upper-case (ABC).
185691** Function lower() converts to lower-case (abc).
185692**
185693** ICU provides two types of case mapping, "general" case mapping and
185694** "language specific". Refer to ICU documentation for the differences
185695** between the two.
185696**
185697** To utilise "general" case mapping, the upper() or lower() scalar
185698** functions are invoked with one argument:
185699**
185700** upper('ABC') -> 'abc'
185701** lower('abc') -> 'ABC'
185702**
185703** To access ICU "language specific" case mapping, upper() or lower()
185704** should be invoked with two arguments. The second argument is the name
185705** of the locale to use. Passing an empty string ("") or SQL NULL value
185706** as the second argument is the same as invoking the 1 argument version
185707** of upper() or lower().
185708**
185709** lower('I', 'en_us') -> 'i'
185710** lower('I', 'tr_tr') -> '\u131' (small dotless i)
185711**
185712** http://www.icu-project.org/userguide/posix.html#case_mappings
185713*/
185714static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
185715 const UChar *zInput; /* Pointer to input string */
185716 UChar *zOutput = 0; /* Pointer to output buffer */
185717 int nInput; /* Size of utf-16 input string in bytes */
185718 int nOut; /* Size of output buffer in bytes */
185719 int cnt;
185720 int bToUpper; /* True for toupper(), false for tolower() */
185721 UErrorCode status;
185722 const char *zLocale = 0;
185723
185724 assert(nArg==1 || nArg==2);
185725 bToUpper = (sqlite3_user_data(p)!=0);
185726 if( nArg==2 ){
185727 zLocale = (const char *)sqlite3_value_text(apArg[1]);
185728 }
185729
185730 zInput = sqlite3_value_text16(apArg[0]);
185731 if( !zInput ){
185732 return;
185733 }
185734 nOut = nInput = sqlite3_value_bytes16(apArg[0]);
185735 if( nOut==0 ){
185736 sqlite3_result_text16(p, "", 0, SQLITE_STATIC);
185737 return;
185738 }
185739
185740 for(cnt=0; cnt<2; cnt++){
185741 UChar *zNew = sqlite3_realloc(zOutput, nOut);
185742 if( zNew==0 ){
185743 sqlite3_free(zOutput);
185744 sqlite3_result_error_nomem(p);
185745 return;
185746 }
185747 zOutput = zNew;
185748 status = U_ZERO_ERROR;
185749 if( bToUpper ){
185750 nOut = 2*u_strToUpper(zOutput,nOut/2,zInput,nInput/2,zLocale,&status);
185751 }else{
185752 nOut = 2*u_strToLower(zOutput,nOut/2,zInput,nInput/2,zLocale,&status);
185753 }
185754
185755 if( U_SUCCESS(status) ){
185756 sqlite3_result_text16(p, zOutput, nOut, xFree);
185757 }else if( status==U_BUFFER_OVERFLOW_ERROR ){
185758 assert( cnt==0 );
185759 continue;
185760 }else{
185761 icuFunctionError(p, bToUpper ? "u_strToUpper" : "u_strToLower", status);
185762 }
185763 return;
185764 }
185765 assert( 0 ); /* Unreachable */
185766}
185767
185768#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) */
185769
185770/*
185771** Collation sequence destructor function. The pCtx argument points to
185772** a UCollator structure previously allocated using ucol_open().
185773*/
185774static void icuCollationDel(void *pCtx){
185775 UCollator *p = (UCollator *)pCtx;
185776 ucol_close(p);
185777}
185778
185779/*
185780** Collation sequence comparison function. The pCtx argument points to
185781** a UCollator structure previously allocated using ucol_open().
185782*/
185783static int icuCollationColl(
185784 void *pCtx,
185785 int nLeft,
185786 const void *zLeft,
185787 int nRight,
185788 const void *zRight
185789){
185790 UCollationResult res;
185791 UCollator *p = (UCollator *)pCtx;
185792 res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2);
185793 switch( res ){
185794 case UCOL_LESS: return -1;
185795 case UCOL_GREATER: return +1;
185796 case UCOL_EQUAL: return 0;
185797 }
185798 assert(!"Unexpected return value from ucol_strcoll()");
185799 return 0;
185800}
185801
185802/*
185803** Implementation of the scalar function icu_load_collation().
185804**
185805** This scalar function is used to add ICU collation based collation
185806** types to an SQLite database connection. It is intended to be called
185807** as follows:
185808**
185809** SELECT icu_load_collation(<locale>, <collation-name>);
185810**
185811** Where <locale> is a string containing an ICU locale identifier (i.e.
185812** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
185813** collation sequence to create.
185814*/
185815static void icuLoadCollation(
185816 sqlite3_context *p,
185817 int nArg,
185818 sqlite3_value **apArg
185819){
185820 sqlite3 *db = (sqlite3 *)sqlite3_user_data(p);
185821 UErrorCode status = U_ZERO_ERROR;
185822 const char *zLocale; /* Locale identifier - (eg. "jp_JP") */
185823 const char *zName; /* SQL Collation sequence name (eg. "japanese") */
185824 UCollator *pUCollator; /* ICU library collation object */
185825 int rc; /* Return code from sqlite3_create_collation_x() */
185826
185827 assert(nArg==2);
185828 (void)nArg; /* Unused parameter */
185829 zLocale = (const char *)sqlite3_value_text(apArg[0]);
185830 zName = (const char *)sqlite3_value_text(apArg[1]);
185831
185832 if( !zLocale || !zName ){
185833 return;
185834 }
185835
185836 pUCollator = ucol_open(zLocale, &status);
185837 if( !U_SUCCESS(status) ){
185838 icuFunctionError(p, "ucol_open", status);
185839 return;
185840 }
185841 assert(p);
185842
185843 rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF16, (void *)pUCollator,
185844 icuCollationColl, icuCollationDel
185845 );
185846 if( rc!=SQLITE_OK ){
185847 ucol_close(pUCollator);
185848 sqlite3_result_error(p, "Error registering collation function", -1);
185849 }
185850}
185851
185852/*
185853** Register the ICU extension functions with database db.
185854*/
185855SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
185856 static const struct IcuScalar {
185857 const char *zName; /* Function name */
185858 unsigned char nArg; /* Number of arguments */
185859 unsigned short enc; /* Optimal text encoding */
185860 unsigned char iContext; /* sqlite3_user_data() context */
185861 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
185862 } scalars[] = {
185863 {"icu_load_collation", 2, SQLITE_UTF8, 1, icuLoadCollation},
185864#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
185865 {"regexp", 2, SQLITE_ANY|SQLITE_DETERMINISTIC, 0, icuRegexpFunc},
185866 {"lower", 1, SQLITE_UTF16|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
185867 {"lower", 2, SQLITE_UTF16|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
185868 {"upper", 1, SQLITE_UTF16|SQLITE_DETERMINISTIC, 1, icuCaseFunc16},
185869 {"upper", 2, SQLITE_UTF16|SQLITE_DETERMINISTIC, 1, icuCaseFunc16},
185870 {"lower", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
185871 {"lower", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
185872 {"upper", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, 1, icuCaseFunc16},
185873 {"upper", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 1, icuCaseFunc16},
185874 {"like", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuLikeFunc},
185875 {"like", 3, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuLikeFunc},
185876#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) */
185877 };
185878 int rc = SQLITE_OK;
185879 int i;
185880
185881 for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
185882 const struct IcuScalar *p = &scalars[i];
185883 rc = sqlite3_create_function(
185884 db, p->zName, p->nArg, p->enc,
185885 p->iContext ? (void*)db : (void*)0,
185886 p->xFunc, 0, 0
185887 );
185888 }
185889
185890 return rc;
185891}
185892
185893#if !SQLITE_CORE
185894#ifdef _WIN32
185895__declspec(dllexport)
185896#endif
185897SQLITE_API int sqlite3_icu_init(
185898 sqlite3 *db,
185899 char **pzErrMsg,
185900 const sqlite3_api_routines *pApi
185901){
185902 SQLITE_EXTENSION_INIT2(pApi)
185903 return sqlite3IcuInit(db);
185904}
185905#endif
185906
185907#endif
185908
185909/************** End of icu.c *************************************************/
185910/************** Begin file fts3_icu.c ****************************************/
185911/*
185912** 2007 June 22
185913**
185914** The author disclaims copyright to this source code. In place of
185915** a legal notice, here is a blessing:
185916**
185917** May you do good and not evil.
185918** May you find forgiveness for yourself and forgive others.
185919** May you share freely, never taking more than you give.
185920**
185921*************************************************************************
185922** This file implements a tokenizer for fts3 based on the ICU library.
185923*/
185924/* #include "fts3Int.h" */
185925#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
185926#ifdef SQLITE_ENABLE_ICU
185927
185928/* #include <assert.h> */
185929/* #include <string.h> */
185930/* #include "fts3_tokenizer.h" */
185931
185932#include <unicode/ubrk.h>
185933/* #include <unicode/ucol.h> */
185934/* #include <unicode/ustring.h> */
185935#include <unicode/utf16.h>
185936
185937typedef struct IcuTokenizer IcuTokenizer;
185938typedef struct IcuCursor IcuCursor;
185939
185940struct IcuTokenizer {
185941 sqlite3_tokenizer base;
185942 char *zLocale;
185943};
185944
185945struct IcuCursor {
185946 sqlite3_tokenizer_cursor base;
185947
185948 UBreakIterator *pIter; /* ICU break-iterator object */
185949 int nChar; /* Number of UChar elements in pInput */
185950 UChar *aChar; /* Copy of input using utf-16 encoding */
185951 int *aOffset; /* Offsets of each character in utf-8 input */
185952
185953 int nBuffer;
185954 char *zBuffer;
185955
185956 int iToken;
185957};
185958
185959/*
185960** Create a new tokenizer instance.
185961*/
185962static int icuCreate(
185963 int argc, /* Number of entries in argv[] */
185964 const char * const *argv, /* Tokenizer creation arguments */
185965 sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */
185966){
185967 IcuTokenizer *p;
185968 int n = 0;
185969
185970 if( argc>0 ){
185971 n = strlen(argv[0])+1;
185972 }
185973 p = (IcuTokenizer *)sqlite3_malloc(sizeof(IcuTokenizer)+n);
185974 if( !p ){
185975 return SQLITE_NOMEM;
185976 }
185977 memset(p, 0, sizeof(IcuTokenizer));
185978
185979 if( n ){
185980 p->zLocale = (char *)&p[1];
185981 memcpy(p->zLocale, argv[0], n);
185982 }
185983
185984 *ppTokenizer = (sqlite3_tokenizer *)p;
185985
185986 return SQLITE_OK;
185987}
185988
185989/*
185990** Destroy a tokenizer
185991*/
185992static int icuDestroy(sqlite3_tokenizer *pTokenizer){
185993 IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
185994 sqlite3_free(p);
185995 return SQLITE_OK;
185996}
185997
185998/*
185999** Prepare to begin tokenizing a particular string. The input
186000** string to be tokenized is pInput[0..nBytes-1]. A cursor
186001** used to incrementally tokenize this string is returned in
186002** *ppCursor.
186003*/
186004static int icuOpen(
186005 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
186006 const char *zInput, /* Input string */
186007 int nInput, /* Length of zInput in bytes */
186008 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
186009){
186010 IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
186011 IcuCursor *pCsr;
186012
186013 const int32_t opt = U_FOLD_CASE_DEFAULT;
186014 UErrorCode status = U_ZERO_ERROR;
186015 int nChar;
186016
186017 UChar32 c;
186018 int iInput = 0;
186019 int iOut = 0;
186020
186021 *ppCursor = 0;
186022
186023 if( zInput==0 ){
186024 nInput = 0;
186025 zInput = "";
186026 }else if( nInput<0 ){
186027 nInput = strlen(zInput);
186028 }
186029 nChar = nInput+1;
186030 pCsr = (IcuCursor *)sqlite3_malloc(
186031 sizeof(IcuCursor) + /* IcuCursor */
186032 ((nChar+3)&~3) * sizeof(UChar) + /* IcuCursor.aChar[] */
186033 (nChar+1) * sizeof(int) /* IcuCursor.aOffset[] */
186034 );
186035 if( !pCsr ){
186036 return SQLITE_NOMEM;
186037 }
186038 memset(pCsr, 0, sizeof(IcuCursor));
186039 pCsr->aChar = (UChar *)&pCsr[1];
186040 pCsr->aOffset = (int *)&pCsr->aChar[(nChar+3)&~3];
186041
186042 pCsr->aOffset[iOut] = iInput;
186043 U8_NEXT(zInput, iInput, nInput, c);
186044 while( c>0 ){
186045 int isError = 0;
186046 c = u_foldCase(c, opt);
186047 U16_APPEND(pCsr->aChar, iOut, nChar, c, isError);
186048 if( isError ){
186049 sqlite3_free(pCsr);
186050 return SQLITE_ERROR;
186051 }
186052 pCsr->aOffset[iOut] = iInput;
186053
186054 if( iInput<nInput ){
186055 U8_NEXT(zInput, iInput, nInput, c);
186056 }else{
186057 c = 0;
186058 }
186059 }
186060
186061 pCsr->pIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status);
186062 if( !U_SUCCESS(status) ){
186063 sqlite3_free(pCsr);
186064 return SQLITE_ERROR;
186065 }
186066 pCsr->nChar = iOut;
186067
186068 ubrk_first(pCsr->pIter);
186069 *ppCursor = (sqlite3_tokenizer_cursor *)pCsr;
186070 return SQLITE_OK;
186071}
186072
186073/*
186074** Close a tokenization cursor previously opened by a call to icuOpen().
186075*/
186076static int icuClose(sqlite3_tokenizer_cursor *pCursor){
186077 IcuCursor *pCsr = (IcuCursor *)pCursor;
186078 ubrk_close(pCsr->pIter);
186079 sqlite3_free(pCsr->zBuffer);
186080 sqlite3_free(pCsr);
186081 return SQLITE_OK;
186082}
186083
186084/*
186085** Extract the next token from a tokenization cursor.
186086*/
186087static int icuNext(
186088 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */
186089 const char **ppToken, /* OUT: *ppToken is the token text */
186090 int *pnBytes, /* OUT: Number of bytes in token */
186091 int *piStartOffset, /* OUT: Starting offset of token */
186092 int *piEndOffset, /* OUT: Ending offset of token */
186093 int *piPosition /* OUT: Position integer of token */
186094){
186095 IcuCursor *pCsr = (IcuCursor *)pCursor;
186096
186097 int iStart = 0;
186098 int iEnd = 0;
186099 int nByte = 0;
186100
186101 while( iStart==iEnd ){
186102 UChar32 c;
186103
186104 iStart = ubrk_current(pCsr->pIter);
186105 iEnd = ubrk_next(pCsr->pIter);
186106 if( iEnd==UBRK_DONE ){
186107 return SQLITE_DONE;
186108 }
186109
186110 while( iStart<iEnd ){
186111 int iWhite = iStart;
186112 U16_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
186113 if( u_isspace(c) ){
186114 iStart = iWhite;
186115 }else{
186116 break;
186117 }
186118 }
186119 assert(iStart<=iEnd);
186120 }
186121
186122 do {
186123 UErrorCode status = U_ZERO_ERROR;
186124 if( nByte ){
186125 char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte);
186126 if( !zNew ){
186127 return SQLITE_NOMEM;
186128 }
186129 pCsr->zBuffer = zNew;
186130 pCsr->nBuffer = nByte;
186131 }
186132
186133 u_strToUTF8(
186134 pCsr->zBuffer, pCsr->nBuffer, &nByte, /* Output vars */
186135 &pCsr->aChar[iStart], iEnd-iStart, /* Input vars */
186136 &status /* Output success/failure */
186137 );
186138 } while( nByte>pCsr->nBuffer );
186139
186140 *ppToken = pCsr->zBuffer;
186141 *pnBytes = nByte;
186142 *piStartOffset = pCsr->aOffset[iStart];
186143 *piEndOffset = pCsr->aOffset[iEnd];
186144 *piPosition = pCsr->iToken++;
186145
186146 return SQLITE_OK;
186147}
186148
186149/*
186150** The set of routines that implement the simple tokenizer
186151*/
186152static const sqlite3_tokenizer_module icuTokenizerModule = {
186153 0, /* iVersion */
186154 icuCreate, /* xCreate */
186155 icuDestroy, /* xCreate */
186156 icuOpen, /* xOpen */
186157 icuClose, /* xClose */
186158 icuNext, /* xNext */
186159 0, /* xLanguageid */
186160};
186161
186162/*
186163** Set *ppModule to point at the implementation of the ICU tokenizer.
186164*/
186165SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(
186166 sqlite3_tokenizer_module const**ppModule
186167){
186168 *ppModule = &icuTokenizerModule;
186169}
186170
186171#endif /* defined(SQLITE_ENABLE_ICU) */
186172#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
186173
186174/************** End of fts3_icu.c ********************************************/
186175/************** Begin file sqlite3rbu.c **************************************/
186176/*
186177** 2014 August 30
186178**
186179** The author disclaims copyright to this source code. In place of
186180** a legal notice, here is a blessing:
186181**
186182** May you do good and not evil.
186183** May you find forgiveness for yourself and forgive others.
186184** May you share freely, never taking more than you give.
186185**
186186*************************************************************************
186187**
186188**
186189** OVERVIEW
186190**
186191** The RBU extension requires that the RBU update be packaged as an
186192** SQLite database. The tables it expects to find are described in
186193** sqlite3rbu.h. Essentially, for each table xyz in the target database
186194** that the user wishes to write to, a corresponding data_xyz table is
186195** created in the RBU database and populated with one row for each row to
186196** update, insert or delete from the target table.
186197**
186198** The update proceeds in three stages:
186199**
186200** 1) The database is updated. The modified database pages are written
186201** to a *-oal file. A *-oal file is just like a *-wal file, except
186202** that it is named "<database>-oal" instead of "<database>-wal".
186203** Because regular SQLite clients do not look for file named
186204** "<database>-oal", they go on using the original database in
186205** rollback mode while the *-oal file is being generated.
186206**
186207** During this stage RBU does not update the database by writing
186208** directly to the target tables. Instead it creates "imposter"
186209** tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses
186210** to update each b-tree individually. All updates required by each
186211** b-tree are completed before moving on to the next, and all
186212** updates are done in sorted key order.
186213**
186214** 2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
186215** location using a call to rename(2). Before doing this the RBU
186216** module takes an EXCLUSIVE lock on the database file, ensuring
186217** that there are no other active readers.
186218**
186219** Once the EXCLUSIVE lock is released, any other database readers
186220** detect the new *-wal file and read the database in wal mode. At
186221** this point they see the new version of the database - including
186222** the updates made as part of the RBU update.
186223**
186224** 3) The new *-wal file is checkpointed. This proceeds in the same way
186225** as a regular database checkpoint, except that a single frame is
186226** checkpointed each time sqlite3rbu_step() is called. If the RBU
186227** handle is closed before the entire *-wal file is checkpointed,
186228** the checkpoint progress is saved in the RBU database and the
186229** checkpoint can be resumed by another RBU client at some point in
186230** the future.
186231**
186232** POTENTIAL PROBLEMS
186233**
186234** The rename() call might not be portable. And RBU is not currently
186235** syncing the directory after renaming the file.
186236**
186237** When state is saved, any commit to the *-oal file and the commit to
186238** the RBU update database are not atomic. So if the power fails at the
186239** wrong moment they might get out of sync. As the main database will be
186240** committed before the RBU update database this will likely either just
186241** pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE
186242** constraint violations).
186243**
186244** If some client does modify the target database mid RBU update, or some
186245** other error occurs, the RBU extension will keep throwing errors. It's
186246** not really clear how to get out of this state. The system could just
186247** by delete the RBU update database and *-oal file and have the device
186248** download the update again and start over.
186249**
186250** At present, for an UPDATE, both the new.* and old.* records are
186251** collected in the rbu_xyz table. And for both UPDATEs and DELETEs all
186252** fields are collected. This means we're probably writing a lot more
186253** data to disk when saving the state of an ongoing update to the RBU
186254** update database than is strictly necessary.
186255**
186256*/
186257
186258/* #include <assert.h> */
186259/* #include <string.h> */
186260/* #include <stdio.h> */
186261
186262/* #include "sqlite3.h" */
186263
186264#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU)
186265/************** Include sqlite3rbu.h in the middle of sqlite3rbu.c ***********/
186266/************** Begin file sqlite3rbu.h **************************************/
186267/*
186268** 2014 August 30
186269**
186270** The author disclaims copyright to this source code. In place of
186271** a legal notice, here is a blessing:
186272**
186273** May you do good and not evil.
186274** May you find forgiveness for yourself and forgive others.
186275** May you share freely, never taking more than you give.
186276**
186277*************************************************************************
186278**
186279** This file contains the public interface for the RBU extension.
186280*/
186281
186282/*
186283** SUMMARY
186284**
186285** Writing a transaction containing a large number of operations on
186286** b-tree indexes that are collectively larger than the available cache
186287** memory can be very inefficient.
186288**
186289** The problem is that in order to update a b-tree, the leaf page (at least)
186290** containing the entry being inserted or deleted must be modified. If the
186291** working set of leaves is larger than the available cache memory, then a
186292** single leaf that is modified more than once as part of the transaction
186293** may be loaded from or written to the persistent media multiple times.
186294** Additionally, because the index updates are likely to be applied in
186295** random order, access to pages within the database is also likely to be in
186296** random order, which is itself quite inefficient.
186297**
186298** One way to improve the situation is to sort the operations on each index
186299** by index key before applying them to the b-tree. This leads to an IO
186300** pattern that resembles a single linear scan through the index b-tree,
186301** and all but guarantees each modified leaf page is loaded and stored
186302** exactly once. SQLite uses this trick to improve the performance of
186303** CREATE INDEX commands. This extension allows it to be used to improve
186304** the performance of large transactions on existing databases.
186305**
186306** Additionally, this extension allows the work involved in writing the
186307** large transaction to be broken down into sub-transactions performed
186308** sequentially by separate processes. This is useful if the system cannot
186309** guarantee that a single update process will run for long enough to apply
186310** the entire update, for example because the update is being applied on a
186311** mobile device that is frequently rebooted. Even after the writer process
186312** has committed one or more sub-transactions, other database clients continue
186313** to read from the original database snapshot. In other words, partially
186314** applied transactions are not visible to other clients.
186315**
186316** "RBU" stands for "Resumable Bulk Update". As in a large database update
186317** transmitted via a wireless network to a mobile device. A transaction
186318** applied using this extension is hence refered to as an "RBU update".
186319**
186320**
186321** LIMITATIONS
186322**
186323** An "RBU update" transaction is subject to the following limitations:
186324**
186325** * The transaction must consist of INSERT, UPDATE and DELETE operations
186326** only.
186327**
186328** * INSERT statements may not use any default values.
186329**
186330** * UPDATE and DELETE statements must identify their target rows by
186331** non-NULL PRIMARY KEY values. Rows with NULL values stored in PRIMARY
186332** KEY fields may not be updated or deleted. If the table being written
186333** has no PRIMARY KEY, affected rows must be identified by rowid.
186334**
186335** * UPDATE statements may not modify PRIMARY KEY columns.
186336**
186337** * No triggers will be fired.
186338**
186339** * No foreign key violations are detected or reported.
186340**
186341** * CHECK constraints are not enforced.
186342**
186343** * No constraint handling mode except for "OR ROLLBACK" is supported.
186344**
186345**
186346** PREPARATION
186347**
186348** An "RBU update" is stored as a separate SQLite database. A database
186349** containing an RBU update is an "RBU database". For each table in the
186350** target database to be updated, the RBU database should contain a table
186351** named "data_<target name>" containing the same set of columns as the
186352** target table, and one more - "rbu_control". The data_% table should
186353** have no PRIMARY KEY or UNIQUE constraints, but each column should have
186354** the same type as the corresponding column in the target database.
186355** The "rbu_control" column should have no type at all. For example, if
186356** the target database contains:
186357**
186358** CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c UNIQUE);
186359**
186360** Then the RBU database should contain:
186361**
186362** CREATE TABLE data_t1(a INTEGER, b TEXT, c, rbu_control);
186363**
186364** The order of the columns in the data_% table does not matter.
186365**
186366** Instead of a regular table, the RBU database may also contain virtual
186367** tables or view named using the data_<target> naming scheme.
186368**
186369** Instead of the plain data_<target> naming scheme, RBU database tables
186370** may also be named data<integer>_<target>, where <integer> is any sequence
186371** of zero or more numeric characters (0-9). This can be significant because
186372** tables within the RBU database are always processed in order sorted by
186373** name. By judicious selection of the <integer> portion of the names
186374** of the RBU tables the user can therefore control the order in which they
186375** are processed. This can be useful, for example, to ensure that "external
186376** content" FTS4 tables are updated before their underlying content tables.
186377**
186378** If the target database table is a virtual table or a table that has no
186379** PRIMARY KEY declaration, the data_% table must also contain a column
186380** named "rbu_rowid". This column is mapped to the tables implicit primary
186381** key column - "rowid". Virtual tables for which the "rowid" column does
186382** not function like a primary key value cannot be updated using RBU. For
186383** example, if the target db contains either of the following:
186384**
186385** CREATE VIRTUAL TABLE x1 USING fts3(a, b);
186386** CREATE TABLE x1(a, b)
186387**
186388** then the RBU database should contain:
186389**
186390** CREATE TABLE data_x1(a, b, rbu_rowid, rbu_control);
186391**
186392** All non-hidden columns (i.e. all columns matched by "SELECT *") of the
186393** target table must be present in the input table. For virtual tables,
186394** hidden columns are optional - they are updated by RBU if present in
186395** the input table, or not otherwise. For example, to write to an fts4
186396** table with a hidden languageid column such as:
186397**
186398** CREATE VIRTUAL TABLE ft1 USING fts4(a, b, languageid='langid');
186399**
186400** Either of the following input table schemas may be used:
186401**
186402** CREATE TABLE data_ft1(a, b, langid, rbu_rowid, rbu_control);
186403** CREATE TABLE data_ft1(a, b, rbu_rowid, rbu_control);
186404**
186405** For each row to INSERT into the target database as part of the RBU
186406** update, the corresponding data_% table should contain a single record
186407** with the "rbu_control" column set to contain integer value 0. The
186408** other columns should be set to the values that make up the new record
186409** to insert.
186410**
186411** If the target database table has an INTEGER PRIMARY KEY, it is not
186412** possible to insert a NULL value into the IPK column. Attempting to
186413** do so results in an SQLITE_MISMATCH error.
186414**
186415** For each row to DELETE from the target database as part of the RBU
186416** update, the corresponding data_% table should contain a single record
186417** with the "rbu_control" column set to contain integer value 1. The
186418** real primary key values of the row to delete should be stored in the
186419** corresponding columns of the data_% table. The values stored in the
186420** other columns are not used.
186421**
186422** For each row to UPDATE from the target database as part of the RBU
186423** update, the corresponding data_% table should contain a single record
186424** with the "rbu_control" column set to contain a value of type text.
186425** The real primary key values identifying the row to update should be
186426** stored in the corresponding columns of the data_% table row, as should
186427** the new values of all columns being update. The text value in the
186428** "rbu_control" column must contain the same number of characters as
186429** there are columns in the target database table, and must consist entirely
186430** of 'x' and '.' characters (or in some special cases 'd' - see below). For
186431** each column that is being updated, the corresponding character is set to
186432** 'x'. For those that remain as they are, the corresponding character of the
186433** rbu_control value should be set to '.'. For example, given the tables
186434** above, the update statement:
186435**
186436** UPDATE t1 SET c = 'usa' WHERE a = 4;
186437**
186438** is represented by the data_t1 row created by:
186439**
186440** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..x');
186441**
186442** Instead of an 'x' character, characters of the rbu_control value specified
186443** for UPDATEs may also be set to 'd'. In this case, instead of updating the
186444** target table with the value stored in the corresponding data_% column, the
186445** user-defined SQL function "rbu_delta()" is invoked and the result stored in
186446** the target table column. rbu_delta() is invoked with two arguments - the
186447** original value currently stored in the target table column and the
186448** value specified in the data_xxx table.
186449**
186450** For example, this row:
186451**
186452** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..d');
186453**
186454** is similar to an UPDATE statement such as:
186455**
186456** UPDATE t1 SET c = rbu_delta(c, 'usa') WHERE a = 4;
186457**
186458** Finally, if an 'f' character appears in place of a 'd' or 's' in an
186459** ota_control string, the contents of the data_xxx table column is assumed
186460** to be a "fossil delta" - a patch to be applied to a blob value in the
186461** format used by the fossil source-code management system. In this case
186462** the existing value within the target database table must be of type BLOB.
186463** It is replaced by the result of applying the specified fossil delta to
186464** itself.
186465**
186466** If the target database table is a virtual table or a table with no PRIMARY
186467** KEY, the rbu_control value should not include a character corresponding
186468** to the rbu_rowid value. For example, this:
186469**
186470** INSERT INTO data_ft1(a, b, rbu_rowid, rbu_control)
186471** VALUES(NULL, 'usa', 12, '.x');
186472**
186473** causes a result similar to:
186474**
186475** UPDATE ft1 SET b = 'usa' WHERE rowid = 12;
186476**
186477** The data_xxx tables themselves should have no PRIMARY KEY declarations.
186478** However, RBU is more efficient if reading the rows in from each data_xxx
186479** table in "rowid" order is roughly the same as reading them sorted by
186480** the PRIMARY KEY of the corresponding target database table. In other
186481** words, rows should be sorted using the destination table PRIMARY KEY
186482** fields before they are inserted into the data_xxx tables.
186483**
186484** USAGE
186485**
186486** The API declared below allows an application to apply an RBU update
186487** stored on disk to an existing target database. Essentially, the
186488** application:
186489**
186490** 1) Opens an RBU handle using the sqlite3rbu_open() function.
186491**
186492** 2) Registers any required virtual table modules with the database
186493** handle returned by sqlite3rbu_db(). Also, if required, register
186494** the rbu_delta() implementation.
186495**
186496** 3) Calls the sqlite3rbu_step() function one or more times on
186497** the new handle. Each call to sqlite3rbu_step() performs a single
186498** b-tree operation, so thousands of calls may be required to apply
186499** a complete update.
186500**
186501** 4) Calls sqlite3rbu_close() to close the RBU update handle. If
186502** sqlite3rbu_step() has been called enough times to completely
186503** apply the update to the target database, then the RBU database
186504** is marked as fully applied. Otherwise, the state of the RBU
186505** update application is saved in the RBU database for later
186506** resumption.
186507**
186508** See comments below for more detail on APIs.
186509**
186510** If an update is only partially applied to the target database by the
186511** time sqlite3rbu_close() is called, various state information is saved
186512** within the RBU database. This allows subsequent processes to automatically
186513** resume the RBU update from where it left off.
186514**
186515** To remove all RBU extension state information, returning an RBU database
186516** to its original contents, it is sufficient to drop all tables that begin
186517** with the prefix "rbu_"
186518**
186519** DATABASE LOCKING
186520**
186521** An RBU update may not be applied to a database in WAL mode. Attempting
186522** to do so is an error (SQLITE_ERROR).
186523**
186524** While an RBU handle is open, a SHARED lock may be held on the target
186525** database file. This means it is possible for other clients to read the
186526** database, but not to write it.
186527**
186528** If an RBU update is started and then suspended before it is completed,
186529** then an external client writes to the database, then attempting to resume
186530** the suspended RBU update is also an error (SQLITE_BUSY).
186531*/
186532
186533#ifndef _SQLITE3RBU_H
186534#define _SQLITE3RBU_H
186535
186536/* #include "sqlite3.h" ** Required for error code definitions ** */
186537
186538#if 0
186539extern "C" {
186540#endif
186541
186542typedef struct sqlite3rbu sqlite3rbu;
186543
186544/*
186545** Open an RBU handle.
186546**
186547** Argument zTarget is the path to the target database. Argument zRbu is
186548** the path to the RBU database. Each call to this function must be matched
186549** by a call to sqlite3rbu_close(). When opening the databases, RBU passes
186550** the SQLITE_CONFIG_URI flag to sqlite3_open_v2(). So if either zTarget
186551** or zRbu begin with "file:", it will be interpreted as an SQLite
186552** database URI, not a regular file name.
186553**
186554** If the zState argument is passed a NULL value, the RBU extension stores
186555** the current state of the update (how many rows have been updated, which
186556** indexes are yet to be updated etc.) within the RBU database itself. This
186557** can be convenient, as it means that the RBU application does not need to
186558** organize removing a separate state file after the update is concluded.
186559** Or, if zState is non-NULL, it must be a path to a database file in which
186560** the RBU extension can store the state of the update.
186561**
186562** When resuming an RBU update, the zState argument must be passed the same
186563** value as when the RBU update was started.
186564**
186565** Once the RBU update is finished, the RBU extension does not
186566** automatically remove any zState database file, even if it created it.
186567**
186568** By default, RBU uses the default VFS to access the files on disk. To
186569** use a VFS other than the default, an SQLite "file:" URI containing a
186570** "vfs=..." option may be passed as the zTarget option.
186571**
186572** IMPORTANT NOTE FOR ZIPVFS USERS: The RBU extension works with all of
186573** SQLite's built-in VFSs, including the multiplexor VFS. However it does
186574** not work out of the box with zipvfs. Refer to the comment describing
186575** the zipvfs_create_vfs() API below for details on using RBU with zipvfs.
186576*/
186577SQLITE_API sqlite3rbu *sqlite3rbu_open(
186578 const char *zTarget,
186579 const char *zRbu,
186580 const char *zState
186581);
186582
186583/*
186584** Open an RBU handle to perform an RBU vacuum on database file zTarget.
186585** An RBU vacuum is similar to SQLite's built-in VACUUM command, except
186586** that it can be suspended and resumed like an RBU update.
186587**
186588** The second argument to this function identifies a database in which
186589** to store the state of the RBU vacuum operation if it is suspended. The
186590** first time sqlite3rbu_vacuum() is called, to start an RBU vacuum
186591** operation, the state database should either not exist or be empty
186592** (contain no tables). If an RBU vacuum is suspended by calling
186593** sqlite3rbu_close() on the RBU handle before sqlite3rbu_step() has
186594** returned SQLITE_DONE, the vacuum state is stored in the state database.
186595** The vacuum can be resumed by calling this function to open a new RBU
186596** handle specifying the same target and state databases.
186597**
186598** If the second argument passed to this function is NULL, then the
186599** name of the state database is "<database>-vacuum", where <database>
186600** is the name of the target database file. In this case, on UNIX, if the
186601** state database is not already present in the file-system, it is created
186602** with the same permissions as the target db is made.
186603**
186604** This function does not delete the state database after an RBU vacuum
186605** is completed, even if it created it. However, if the call to
186606** sqlite3rbu_close() returns any value other than SQLITE_OK, the contents
186607** of the state tables within the state database are zeroed. This way,
186608** the next call to sqlite3rbu_vacuum() opens a handle that starts a
186609** new RBU vacuum operation.
186610**
186611** As with sqlite3rbu_open(), Zipvfs users should rever to the comment
186612** describing the sqlite3rbu_create_vfs() API function below for
186613** a description of the complications associated with using RBU with
186614** zipvfs databases.
186615*/
186616SQLITE_API sqlite3rbu *sqlite3rbu_vacuum(
186617 const char *zTarget,
186618 const char *zState
186619);
186620
186621/*
186622** Configure a limit for the amount of temp space that may be used by
186623** the RBU handle passed as the first argument. The new limit is specified
186624** in bytes by the second parameter. If it is positive, the limit is updated.
186625** If the second parameter to this function is passed zero, then the limit
186626** is removed entirely. If the second parameter is negative, the limit is
186627** not modified (this is useful for querying the current limit).
186628**
186629** In all cases the returned value is the current limit in bytes (zero
186630** indicates unlimited).
186631**
186632** If the temp space limit is exceeded during operation, an SQLITE_FULL
186633** error is returned.
186634*/
186635SQLITE_API sqlite3_int64 sqlite3rbu_temp_size_limit(sqlite3rbu*, sqlite3_int64);
186636
186637/*
186638** Return the current amount of temp file space, in bytes, currently used by
186639** the RBU handle passed as the only argument.
186640*/
186641SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu*);
186642
186643/*
186644** Internally, each RBU connection uses a separate SQLite database
186645** connection to access the target and rbu update databases. This
186646** API allows the application direct access to these database handles.
186647**
186648** The first argument passed to this function must be a valid, open, RBU
186649** handle. The second argument should be passed zero to access the target
186650** database handle, or non-zero to access the rbu update database handle.
186651** Accessing the underlying database handles may be useful in the
186652** following scenarios:
186653**
186654** * If any target tables are virtual tables, it may be necessary to
186655** call sqlite3_create_module() on the target database handle to
186656** register the required virtual table implementations.
186657**
186658** * If the data_xxx tables in the RBU source database are virtual
186659** tables, the application may need to call sqlite3_create_module() on
186660** the rbu update db handle to any required virtual table
186661** implementations.
186662**
186663** * If the application uses the "rbu_delta()" feature described above,
186664** it must use sqlite3_create_function() or similar to register the
186665** rbu_delta() implementation with the target database handle.
186666**
186667** If an error has occurred, either while opening or stepping the RBU object,
186668** this function may return NULL. The error code and message may be collected
186669** when sqlite3rbu_close() is called.
186670**
186671** Database handles returned by this function remain valid until the next
186672** call to any sqlite3rbu_xxx() function other than sqlite3rbu_db().
186673*/
186674SQLITE_API sqlite3 *sqlite3rbu_db(sqlite3rbu*, int bRbu);
186675
186676/*
186677** Do some work towards applying the RBU update to the target db.
186678**
186679** Return SQLITE_DONE if the update has been completely applied, or
186680** SQLITE_OK if no error occurs but there remains work to do to apply
186681** the RBU update. If an error does occur, some other error code is
186682** returned.
186683**
186684** Once a call to sqlite3rbu_step() has returned a value other than
186685** SQLITE_OK, all subsequent calls on the same RBU handle are no-ops
186686** that immediately return the same value.
186687*/
186688SQLITE_API int sqlite3rbu_step(sqlite3rbu *pRbu);
186689
186690/*
186691** Force RBU to save its state to disk.
186692**
186693** If a power failure or application crash occurs during an update, following
186694** system recovery RBU may resume the update from the point at which the state
186695** was last saved. In other words, from the most recent successful call to
186696** sqlite3rbu_close() or this function.
186697**
186698** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
186699*/
186700SQLITE_API int sqlite3rbu_savestate(sqlite3rbu *pRbu);
186701
186702/*
186703** Close an RBU handle.
186704**
186705** If the RBU update has been completely applied, mark the RBU database
186706** as fully applied. Otherwise, assuming no error has occurred, save the
186707** current state of the RBU update appliation to the RBU database.
186708**
186709** If an error has already occurred as part of an sqlite3rbu_step()
186710** or sqlite3rbu_open() call, or if one occurs within this function, an
186711** SQLite error code is returned. Additionally, if pzErrmsg is not NULL,
186712** *pzErrmsg may be set to point to a buffer containing a utf-8 formatted
186713** English language error message. It is the responsibility of the caller to
186714** eventually free any such buffer using sqlite3_free().
186715**
186716** Otherwise, if no error occurs, this function returns SQLITE_OK if the
186717** update has been partially applied, or SQLITE_DONE if it has been
186718** completely applied.
186719*/
186720SQLITE_API int sqlite3rbu_close(sqlite3rbu *pRbu, char **pzErrmsg);
186721
186722/*
186723** Return the total number of key-value operations (inserts, deletes or
186724** updates) that have been performed on the target database since the
186725** current RBU update was started.
186726*/
186727SQLITE_API sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu);
186728
186729/*
186730** Obtain permyriadage (permyriadage is to 10000 as percentage is to 100)
186731** progress indications for the two stages of an RBU update. This API may
186732** be useful for driving GUI progress indicators and similar.
186733**
186734** An RBU update is divided into two stages:
186735**
186736** * Stage 1, in which changes are accumulated in an oal/wal file, and
186737** * Stage 2, in which the contents of the wal file are copied into the
186738** main database.
186739**
186740** The update is visible to non-RBU clients during stage 2. During stage 1
186741** non-RBU reader clients may see the original database.
186742**
186743** If this API is called during stage 2 of the update, output variable
186744** (*pnOne) is set to 10000 to indicate that stage 1 has finished and (*pnTwo)
186745** to a value between 0 and 10000 to indicate the permyriadage progress of
186746** stage 2. A value of 5000 indicates that stage 2 is half finished,
186747** 9000 indicates that it is 90% finished, and so on.
186748**
186749** If this API is called during stage 1 of the update, output variable
186750** (*pnTwo) is set to 0 to indicate that stage 2 has not yet started. The
186751** value to which (*pnOne) is set depends on whether or not the RBU
186752** database contains an "rbu_count" table. The rbu_count table, if it
186753** exists, must contain the same columns as the following:
186754**
186755** CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
186756**
186757** There must be one row in the table for each source (data_xxx) table within
186758** the RBU database. The 'tbl' column should contain the name of the source
186759** table. The 'cnt' column should contain the number of rows within the
186760** source table.
186761**
186762** If the rbu_count table is present and populated correctly and this
186763** API is called during stage 1, the *pnOne output variable is set to the
186764** permyriadage progress of the same stage. If the rbu_count table does
186765** not exist, then (*pnOne) is set to -1 during stage 1. If the rbu_count
186766** table exists but is not correctly populated, the value of the *pnOne
186767** output variable during stage 1 is undefined.
186768*/
186769SQLITE_API void sqlite3rbu_bp_progress(sqlite3rbu *pRbu, int *pnOne, int*pnTwo);
186770
186771/*
186772** Obtain an indication as to the current stage of an RBU update or vacuum.
186773** This function always returns one of the SQLITE_RBU_STATE_XXX constants
186774** defined in this file. Return values should be interpreted as follows:
186775**
186776** SQLITE_RBU_STATE_OAL:
186777** RBU is currently building a *-oal file. The next call to sqlite3rbu_step()
186778** may either add further data to the *-oal file, or compute data that will
186779** be added by a subsequent call.
186780**
186781** SQLITE_RBU_STATE_MOVE:
186782** RBU has finished building the *-oal file. The next call to sqlite3rbu_step()
186783** will move the *-oal file to the equivalent *-wal path. If the current
186784** operation is an RBU update, then the updated version of the database
186785** file will become visible to ordinary SQLite clients following the next
186786** call to sqlite3rbu_step().
186787**
186788** SQLITE_RBU_STATE_CHECKPOINT:
186789** RBU is currently performing an incremental checkpoint. The next call to
186790** sqlite3rbu_step() will copy a page of data from the *-wal file into
186791** the target database file.
186792**
186793** SQLITE_RBU_STATE_DONE:
186794** The RBU operation has finished. Any subsequent calls to sqlite3rbu_step()
186795** will immediately return SQLITE_DONE.
186796**
186797** SQLITE_RBU_STATE_ERROR:
186798** An error has occurred. Any subsequent calls to sqlite3rbu_step() will
186799** immediately return the SQLite error code associated with the error.
186800*/
186801#define SQLITE_RBU_STATE_OAL 1
186802#define SQLITE_RBU_STATE_MOVE 2
186803#define SQLITE_RBU_STATE_CHECKPOINT 3
186804#define SQLITE_RBU_STATE_DONE 4
186805#define SQLITE_RBU_STATE_ERROR 5
186806
186807SQLITE_API int sqlite3rbu_state(sqlite3rbu *pRbu);
186808
186809/*
186810** Create an RBU VFS named zName that accesses the underlying file-system
186811** via existing VFS zParent. Or, if the zParent parameter is passed NULL,
186812** then the new RBU VFS uses the default system VFS to access the file-system.
186813** The new object is registered as a non-default VFS with SQLite before
186814** returning.
186815**
186816** Part of the RBU implementation uses a custom VFS object. Usually, this
186817** object is created and deleted automatically by RBU.
186818**
186819** The exception is for applications that also use zipvfs. In this case,
186820** the custom VFS must be explicitly created by the user before the RBU
186821** handle is opened. The RBU VFS should be installed so that the zipvfs
186822** VFS uses the RBU VFS, which in turn uses any other VFS layers in use
186823** (for example multiplexor) to access the file-system. For example,
186824** to assemble an RBU enabled VFS stack that uses both zipvfs and
186825** multiplexor (error checking omitted):
186826**
186827** // Create a VFS named "multiplex" (not the default).
186828** sqlite3_multiplex_initialize(0, 0);
186829**
186830** // Create an rbu VFS named "rbu" that uses multiplexor. If the
186831** // second argument were replaced with NULL, the "rbu" VFS would
186832** // access the file-system via the system default VFS, bypassing the
186833** // multiplexor.
186834** sqlite3rbu_create_vfs("rbu", "multiplex");
186835**
186836** // Create a zipvfs VFS named "zipvfs" that uses rbu.
186837** zipvfs_create_vfs_v3("zipvfs", "rbu", 0, xCompressorAlgorithmDetector);
186838**
186839** // Make zipvfs the default VFS.
186840** sqlite3_vfs_register(sqlite3_vfs_find("zipvfs"), 1);
186841**
186842** Because the default VFS created above includes a RBU functionality, it
186843** may be used by RBU clients. Attempting to use RBU with a zipvfs VFS stack
186844** that does not include the RBU layer results in an error.
186845**
186846** The overhead of adding the "rbu" VFS to the system is negligible for
186847** non-RBU users. There is no harm in an application accessing the
186848** file-system via "rbu" all the time, even if it only uses RBU functionality
186849** occasionally.
186850*/
186851SQLITE_API int sqlite3rbu_create_vfs(const char *zName, const char *zParent);
186852
186853/*
186854** Deregister and destroy an RBU vfs created by an earlier call to
186855** sqlite3rbu_create_vfs().
186856**
186857** VFS objects are not reference counted. If a VFS object is destroyed
186858** before all database handles that use it have been closed, the results
186859** are undefined.
186860*/
186861SQLITE_API void sqlite3rbu_destroy_vfs(const char *zName);
186862
186863#if 0
186864} /* end of the 'extern "C"' block */
186865#endif
186866
186867#endif /* _SQLITE3RBU_H */
186868
186869/************** End of sqlite3rbu.h ******************************************/
186870/************** Continuing where we left off in sqlite3rbu.c *****************/
186871
186872#if defined(_WIN32_WCE)
186873/* #include "windows.h" */
186874#endif
186875
186876/* Maximum number of prepared UPDATE statements held by this module */
186877#define SQLITE_RBU_UPDATE_CACHESIZE 16
186878
186879/* Delta checksums disabled by default. Compile with -DRBU_ENABLE_DELTA_CKSUM
186880** to enable checksum verification.
186881*/
186882#ifndef RBU_ENABLE_DELTA_CKSUM
186883# define RBU_ENABLE_DELTA_CKSUM 0
186884#endif
186885
186886/*
186887** Swap two objects of type TYPE.
186888*/
186889#if !defined(SQLITE_AMALGAMATION)
186890# define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
186891#endif
186892
186893/*
186894** The rbu_state table is used to save the state of a partially applied
186895** update so that it can be resumed later. The table consists of integer
186896** keys mapped to values as follows:
186897**
186898** RBU_STATE_STAGE:
186899** May be set to integer values 1, 2, 4 or 5. As follows:
186900** 1: the *-rbu file is currently under construction.
186901** 2: the *-rbu file has been constructed, but not yet moved
186902** to the *-wal path.
186903** 4: the checkpoint is underway.
186904** 5: the rbu update has been checkpointed.
186905**
186906** RBU_STATE_TBL:
186907** Only valid if STAGE==1. The target database name of the table
186908** currently being written.
186909**
186910** RBU_STATE_IDX:
186911** Only valid if STAGE==1. The target database name of the index
186912** currently being written, or NULL if the main table is currently being
186913** updated.
186914**
186915** RBU_STATE_ROW:
186916** Only valid if STAGE==1. Number of rows already processed for the current
186917** table/index.
186918**
186919** RBU_STATE_PROGRESS:
186920** Trbul number of sqlite3rbu_step() calls made so far as part of this
186921** rbu update.
186922**
186923** RBU_STATE_CKPT:
186924** Valid if STAGE==4. The 64-bit checksum associated with the wal-index
186925** header created by recovering the *-wal file. This is used to detect
186926** cases when another client appends frames to the *-wal file in the
186927** middle of an incremental checkpoint (an incremental checkpoint cannot
186928** be continued if this happens).
186929**
186930** RBU_STATE_COOKIE:
186931** Valid if STAGE==1. The current change-counter cookie value in the
186932** target db file.
186933**
186934** RBU_STATE_OALSZ:
186935** Valid if STAGE==1. The size in bytes of the *-oal file.
186936**
186937** RBU_STATE_DATATBL:
186938** Only valid if STAGE==1. The RBU database name of the table
186939** currently being read.
186940*/
186941#define RBU_STATE_STAGE 1
186942#define RBU_STATE_TBL 2
186943#define RBU_STATE_IDX 3
186944#define RBU_STATE_ROW 4
186945#define RBU_STATE_PROGRESS 5
186946#define RBU_STATE_CKPT 6
186947#define RBU_STATE_COOKIE 7
186948#define RBU_STATE_OALSZ 8
186949#define RBU_STATE_PHASEONESTEP 9
186950#define RBU_STATE_DATATBL 10
186951
186952#define RBU_STAGE_OAL 1
186953#define RBU_STAGE_MOVE 2
186954#define RBU_STAGE_CAPTURE 3
186955#define RBU_STAGE_CKPT 4
186956#define RBU_STAGE_DONE 5
186957
186958
186959#define RBU_CREATE_STATE \
186960 "CREATE TABLE IF NOT EXISTS %s.rbu_state(k INTEGER PRIMARY KEY, v)"
186961
186962typedef struct RbuFrame RbuFrame;
186963typedef struct RbuObjIter RbuObjIter;
186964typedef struct RbuState RbuState;
186965typedef struct rbu_vfs rbu_vfs;
186966typedef struct rbu_file rbu_file;
186967typedef struct RbuUpdateStmt RbuUpdateStmt;
186968
186969#if !defined(SQLITE_AMALGAMATION)
186970typedef unsigned int u32;
186971typedef unsigned short u16;
186972typedef unsigned char u8;
186973typedef sqlite3_int64 i64;
186974#endif
186975
186976/*
186977** These values must match the values defined in wal.c for the equivalent
186978** locks. These are not magic numbers as they are part of the SQLite file
186979** format.
186980*/
186981#define WAL_LOCK_WRITE 0
186982#define WAL_LOCK_CKPT 1
186983#define WAL_LOCK_READ0 3
186984
186985#define SQLITE_FCNTL_RBUCNT 5149216
186986
186987/*
186988** A structure to store values read from the rbu_state table in memory.
186989*/
186990struct RbuState {
186991 int eStage;
186992 char *zTbl;
186993 char *zDataTbl;
186994 char *zIdx;
186995 i64 iWalCksum;
186996 int nRow;
186997 i64 nProgress;
186998 u32 iCookie;
186999 i64 iOalSz;
187000 i64 nPhaseOneStep;
187001};
187002
187003struct RbuUpdateStmt {
187004 char *zMask; /* Copy of update mask used with pUpdate */
187005 sqlite3_stmt *pUpdate; /* Last update statement (or NULL) */
187006 RbuUpdateStmt *pNext;
187007};
187008
187009/*
187010** An iterator of this type is used to iterate through all objects in
187011** the target database that require updating. For each such table, the
187012** iterator visits, in order:
187013**
187014** * the table itself,
187015** * each index of the table (zero or more points to visit), and
187016** * a special "cleanup table" state.
187017**
187018** abIndexed:
187019** If the table has no indexes on it, abIndexed is set to NULL. Otherwise,
187020** it points to an array of flags nTblCol elements in size. The flag is
187021** set for each column that is either a part of the PK or a part of an
187022** index. Or clear otherwise.
187023**
187024*/
187025struct RbuObjIter {
187026 sqlite3_stmt *pTblIter; /* Iterate through tables */
187027 sqlite3_stmt *pIdxIter; /* Index iterator */
187028 int nTblCol; /* Size of azTblCol[] array */
187029 char **azTblCol; /* Array of unquoted target column names */
187030 char **azTblType; /* Array of target column types */
187031 int *aiSrcOrder; /* src table col -> target table col */
187032 u8 *abTblPk; /* Array of flags, set on target PK columns */
187033 u8 *abNotNull; /* Array of flags, set on NOT NULL columns */
187034 u8 *abIndexed; /* Array of flags, set on indexed & PK cols */
187035 int eType; /* Table type - an RBU_PK_XXX value */
187036
187037 /* Output variables. zTbl==0 implies EOF. */
187038 int bCleanup; /* True in "cleanup" state */
187039 const char *zTbl; /* Name of target db table */
187040 const char *zDataTbl; /* Name of rbu db table (or null) */
187041 const char *zIdx; /* Name of target db index (or null) */
187042 int iTnum; /* Root page of current object */
187043 int iPkTnum; /* If eType==EXTERNAL, root of PK index */
187044 int bUnique; /* Current index is unique */
187045 int nIndex; /* Number of aux. indexes on table zTbl */
187046
187047 /* Statements created by rbuObjIterPrepareAll() */
187048 int nCol; /* Number of columns in current object */
187049 sqlite3_stmt *pSelect; /* Source data */
187050 sqlite3_stmt *pInsert; /* Statement for INSERT operations */
187051 sqlite3_stmt *pDelete; /* Statement for DELETE ops */
187052 sqlite3_stmt *pTmpInsert; /* Insert into rbu_tmp_$zDataTbl */
187053
187054 /* Last UPDATE used (for PK b-tree updates only), or NULL. */
187055 RbuUpdateStmt *pRbuUpdate;
187056};
187057
187058/*
187059** Values for RbuObjIter.eType
187060**
187061** 0: Table does not exist (error)
187062** 1: Table has an implicit rowid.
187063** 2: Table has an explicit IPK column.
187064** 3: Table has an external PK index.
187065** 4: Table is WITHOUT ROWID.
187066** 5: Table is a virtual table.
187067*/
187068#define RBU_PK_NOTABLE 0
187069#define RBU_PK_NONE 1
187070#define RBU_PK_IPK 2
187071#define RBU_PK_EXTERNAL 3
187072#define RBU_PK_WITHOUT_ROWID 4
187073#define RBU_PK_VTAB 5
187074
187075
187076/*
187077** Within the RBU_STAGE_OAL stage, each call to sqlite3rbu_step() performs
187078** one of the following operations.
187079*/
187080#define RBU_INSERT 1 /* Insert on a main table b-tree */
187081#define RBU_DELETE 2 /* Delete a row from a main table b-tree */
187082#define RBU_REPLACE 3 /* Delete and then insert a row */
187083#define RBU_IDX_DELETE 4 /* Delete a row from an aux. index b-tree */
187084#define RBU_IDX_INSERT 5 /* Insert on an aux. index b-tree */
187085
187086#define RBU_UPDATE 6 /* Update a row in a main table b-tree */
187087
187088/*
187089** A single step of an incremental checkpoint - frame iWalFrame of the wal
187090** file should be copied to page iDbPage of the database file.
187091*/
187092struct RbuFrame {
187093 u32 iDbPage;
187094 u32 iWalFrame;
187095};
187096
187097/*
187098** RBU handle.
187099**
187100** nPhaseOneStep:
187101** If the RBU database contains an rbu_count table, this value is set to
187102** a running estimate of the number of b-tree operations required to
187103** finish populating the *-oal file. This allows the sqlite3_bp_progress()
187104** API to calculate the permyriadage progress of populating the *-oal file
187105** using the formula:
187106**
187107** permyriadage = (10000 * nProgress) / nPhaseOneStep
187108**
187109** nPhaseOneStep is initialized to the sum of:
187110**
187111** nRow * (nIndex + 1)
187112**
187113** for all source tables in the RBU database, where nRow is the number
187114** of rows in the source table and nIndex the number of indexes on the
187115** corresponding target database table.
187116**
187117** This estimate is accurate if the RBU update consists entirely of
187118** INSERT operations. However, it is inaccurate if:
187119**
187120** * the RBU update contains any UPDATE operations. If the PK specified
187121** for an UPDATE operation does not exist in the target table, then
187122** no b-tree operations are required on index b-trees. Or if the
187123** specified PK does exist, then (nIndex*2) such operations are
187124** required (one delete and one insert on each index b-tree).
187125**
187126** * the RBU update contains any DELETE operations for which the specified
187127** PK does not exist. In this case no operations are required on index
187128** b-trees.
187129**
187130** * the RBU update contains REPLACE operations. These are similar to
187131** UPDATE operations.
187132**
187133** nPhaseOneStep is updated to account for the conditions above during the
187134** first pass of each source table. The updated nPhaseOneStep value is
187135** stored in the rbu_state table if the RBU update is suspended.
187136*/
187137struct sqlite3rbu {
187138 int eStage; /* Value of RBU_STATE_STAGE field */
187139 sqlite3 *dbMain; /* target database handle */
187140 sqlite3 *dbRbu; /* rbu database handle */
187141 char *zTarget; /* Path to target db */
187142 char *zRbu; /* Path to rbu db */
187143 char *zState; /* Path to state db (or NULL if zRbu) */
187144 char zStateDb[5]; /* Db name for state ("stat" or "main") */
187145 int rc; /* Value returned by last rbu_step() call */
187146 char *zErrmsg; /* Error message if rc!=SQLITE_OK */
187147 int nStep; /* Rows processed for current object */
187148 int nProgress; /* Rows processed for all objects */
187149 RbuObjIter objiter; /* Iterator for skipping through tbl/idx */
187150 const char *zVfsName; /* Name of automatically created rbu vfs */
187151 rbu_file *pTargetFd; /* File handle open on target db */
187152 int nPagePerSector; /* Pages per sector for pTargetFd */
187153 i64 iOalSz;
187154 i64 nPhaseOneStep;
187155
187156 /* The following state variables are used as part of the incremental
187157 ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
187158 ** function rbuSetupCheckpoint() for details. */
187159 u32 iMaxFrame; /* Largest iWalFrame value in aFrame[] */
187160 u32 mLock;
187161 int nFrame; /* Entries in aFrame[] array */
187162 int nFrameAlloc; /* Allocated size of aFrame[] array */
187163 RbuFrame *aFrame;
187164 int pgsz;
187165 u8 *aBuf;
187166 i64 iWalCksum;
187167 i64 szTemp; /* Current size of all temp files in use */
187168 i64 szTempLimit; /* Total size limit for temp files */
187169
187170 /* Used in RBU vacuum mode only */
187171 int nRbu; /* Number of RBU VFS in the stack */
187172 rbu_file *pRbuFd; /* Fd for main db of dbRbu */
187173};
187174
187175/*
187176** An rbu VFS is implemented using an instance of this structure.
187177**
187178** Variable pRbu is only non-NULL for automatically created RBU VFS objects.
187179** It is NULL for RBU VFS objects created explicitly using
187180** sqlite3rbu_create_vfs(). It is used to track the total amount of temp
187181** space used by the RBU handle.
187182*/
187183struct rbu_vfs {
187184 sqlite3_vfs base; /* rbu VFS shim methods */
187185 sqlite3_vfs *pRealVfs; /* Underlying VFS */
187186 sqlite3_mutex *mutex; /* Mutex to protect pMain */
187187 sqlite3rbu *pRbu; /* Owner RBU object */
187188 rbu_file *pMain; /* List of main db files */
187189 rbu_file *pMainRbu; /* List of main db files with pRbu!=0 */
187190};
187191
187192/*
187193** Each file opened by an rbu VFS is represented by an instance of
187194** the following structure.
187195**
187196** If this is a temporary file (pRbu!=0 && flags&DELETE_ON_CLOSE), variable
187197** "sz" is set to the current size of the database file.
187198*/
187199struct rbu_file {
187200 sqlite3_file base; /* sqlite3_file methods */
187201 sqlite3_file *pReal; /* Underlying file handle */
187202 rbu_vfs *pRbuVfs; /* Pointer to the rbu_vfs object */
187203 sqlite3rbu *pRbu; /* Pointer to rbu object (rbu target only) */
187204 i64 sz; /* Size of file in bytes (temp only) */
187205
187206 int openFlags; /* Flags this file was opened with */
187207 u32 iCookie; /* Cookie value for main db files */
187208 u8 iWriteVer; /* "write-version" value for main db files */
187209 u8 bNolock; /* True to fail EXCLUSIVE locks */
187210
187211 int nShm; /* Number of entries in apShm[] array */
187212 char **apShm; /* Array of mmap'd *-shm regions */
187213 char *zDel; /* Delete this when closing file */
187214
187215 const char *zWal; /* Wal filename for this main db file */
187216 rbu_file *pWalFd; /* Wal file descriptor for this main db */
187217 rbu_file *pMainNext; /* Next MAIN_DB file */
187218 rbu_file *pMainRbuNext; /* Next MAIN_DB file with pRbu!=0 */
187219};
187220
187221/*
187222** True for an RBU vacuum handle, or false otherwise.
187223*/
187224#define rbuIsVacuum(p) ((p)->zTarget==0)
187225
187226
187227/*************************************************************************
187228** The following three functions, found below:
187229**
187230** rbuDeltaGetInt()
187231** rbuDeltaChecksum()
187232** rbuDeltaApply()
187233**
187234** are lifted from the fossil source code (http://fossil-scm.org). They
187235** are used to implement the scalar SQL function rbu_fossil_delta().
187236*/
187237
187238/*
187239** Read bytes from *pz and convert them into a positive integer. When
187240** finished, leave *pz pointing to the first character past the end of
187241** the integer. The *pLen parameter holds the length of the string
187242** in *pz and is decremented once for each character in the integer.
187243*/
187244static unsigned int rbuDeltaGetInt(const char **pz, int *pLen){
187245 static const signed char zValue[] = {
187246 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
187247 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
187248 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
187249 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
187250 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
187251 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, 36,
187252 -1, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
187253 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, -1, -1, -1, 63, -1,
187254 };
187255 unsigned int v = 0;
187256 int c;
187257 unsigned char *z = (unsigned char*)*pz;
187258 unsigned char *zStart = z;
187259 while( (c = zValue[0x7f&*(z++)])>=0 ){
187260 v = (v<<6) + c;
187261 }
187262 z--;
187263 *pLen -= z - zStart;
187264 *pz = (char*)z;
187265 return v;
187266}
187267
187268#if RBU_ENABLE_DELTA_CKSUM
187269/*
187270** Compute a 32-bit checksum on the N-byte buffer. Return the result.
187271*/
187272static unsigned int rbuDeltaChecksum(const char *zIn, size_t N){
187273 const unsigned char *z = (const unsigned char *)zIn;
187274 unsigned sum0 = 0;
187275 unsigned sum1 = 0;
187276 unsigned sum2 = 0;
187277 unsigned sum3 = 0;
187278 while(N >= 16){
187279 sum0 += ((unsigned)z[0] + z[4] + z[8] + z[12]);
187280 sum1 += ((unsigned)z[1] + z[5] + z[9] + z[13]);
187281 sum2 += ((unsigned)z[2] + z[6] + z[10]+ z[14]);
187282 sum3 += ((unsigned)z[3] + z[7] + z[11]+ z[15]);
187283 z += 16;
187284 N -= 16;
187285 }
187286 while(N >= 4){
187287 sum0 += z[0];
187288 sum1 += z[1];
187289 sum2 += z[2];
187290 sum3 += z[3];
187291 z += 4;
187292 N -= 4;
187293 }
187294 sum3 += (sum2 << 8) + (sum1 << 16) + (sum0 << 24);
187295 switch(N){
187296 case 3: sum3 += (z[2] << 8);
187297 case 2: sum3 += (z[1] << 16);
187298 case 1: sum3 += (z[0] << 24);
187299 default: ;
187300 }
187301 return sum3;
187302}
187303#endif
187304
187305/*
187306** Apply a delta.
187307**
187308** The output buffer should be big enough to hold the whole output
187309** file and a NUL terminator at the end. The delta_output_size()
187310** routine will determine this size for you.
187311**
187312** The delta string should be null-terminated. But the delta string
187313** may contain embedded NUL characters (if the input and output are
187314** binary files) so we also have to pass in the length of the delta in
187315** the lenDelta parameter.
187316**
187317** This function returns the size of the output file in bytes (excluding
187318** the final NUL terminator character). Except, if the delta string is
187319** malformed or intended for use with a source file other than zSrc,
187320** then this routine returns -1.
187321**
187322** Refer to the delta_create() documentation above for a description
187323** of the delta file format.
187324*/
187325static int rbuDeltaApply(
187326 const char *zSrc, /* The source or pattern file */
187327 int lenSrc, /* Length of the source file */
187328 const char *zDelta, /* Delta to apply to the pattern */
187329 int lenDelta, /* Length of the delta */
187330 char *zOut /* Write the output into this preallocated buffer */
187331){
187332 unsigned int limit;
187333 unsigned int total = 0;
187334#if RBU_ENABLE_DELTA_CKSUM
187335 char *zOrigOut = zOut;
187336#endif
187337
187338 limit = rbuDeltaGetInt(&zDelta, &lenDelta);
187339 if( *zDelta!='\n' ){
187340 /* ERROR: size integer not terminated by "\n" */
187341 return -1;
187342 }
187343 zDelta++; lenDelta--;
187344 while( *zDelta && lenDelta>0 ){
187345 unsigned int cnt, ofst;
187346 cnt = rbuDeltaGetInt(&zDelta, &lenDelta);
187347 switch( zDelta[0] ){
187348 case '@': {
187349 zDelta++; lenDelta--;
187350 ofst = rbuDeltaGetInt(&zDelta, &lenDelta);
187351 if( lenDelta>0 && zDelta[0]!=',' ){
187352 /* ERROR: copy command not terminated by ',' */
187353 return -1;
187354 }
187355 zDelta++; lenDelta--;
187356 total += cnt;
187357 if( total>limit ){
187358 /* ERROR: copy exceeds output file size */
187359 return -1;
187360 }
187361 if( (int)(ofst+cnt) > lenSrc ){
187362 /* ERROR: copy extends past end of input */
187363 return -1;
187364 }
187365 memcpy(zOut, &zSrc[ofst], cnt);
187366 zOut += cnt;
187367 break;
187368 }
187369 case ':': {
187370 zDelta++; lenDelta--;
187371 total += cnt;
187372 if( total>limit ){
187373 /* ERROR: insert command gives an output larger than predicted */
187374 return -1;
187375 }
187376 if( (int)cnt>lenDelta ){
187377 /* ERROR: insert count exceeds size of delta */
187378 return -1;
187379 }
187380 memcpy(zOut, zDelta, cnt);
187381 zOut += cnt;
187382 zDelta += cnt;
187383 lenDelta -= cnt;
187384 break;
187385 }
187386 case ';': {
187387 zDelta++; lenDelta--;
187388 zOut[0] = 0;
187389#if RBU_ENABLE_DELTA_CKSUM
187390 if( cnt!=rbuDeltaChecksum(zOrigOut, total) ){
187391 /* ERROR: bad checksum */
187392 return -1;
187393 }
187394#endif
187395 if( total!=limit ){
187396 /* ERROR: generated size does not match predicted size */
187397 return -1;
187398 }
187399 return total;
187400 }
187401 default: {
187402 /* ERROR: unknown delta operator */
187403 return -1;
187404 }
187405 }
187406 }
187407 /* ERROR: unterminated delta */
187408 return -1;
187409}
187410
187411static int rbuDeltaOutputSize(const char *zDelta, int lenDelta){
187412 int size;
187413 size = rbuDeltaGetInt(&zDelta, &lenDelta);
187414 if( *zDelta!='\n' ){
187415 /* ERROR: size integer not terminated by "\n" */
187416 return -1;
187417 }
187418 return size;
187419}
187420
187421/*
187422** End of code taken from fossil.
187423*************************************************************************/
187424
187425/*
187426** Implementation of SQL scalar function rbu_fossil_delta().
187427**
187428** This function applies a fossil delta patch to a blob. Exactly two
187429** arguments must be passed to this function. The first is the blob to
187430** patch and the second the patch to apply. If no error occurs, this
187431** function returns the patched blob.
187432*/
187433static void rbuFossilDeltaFunc(
187434 sqlite3_context *context,
187435 int argc,
187436 sqlite3_value **argv
187437){
187438 const char *aDelta;
187439 int nDelta;
187440 const char *aOrig;
187441 int nOrig;
187442
187443 int nOut;
187444 int nOut2;
187445 char *aOut;
187446
187447 assert( argc==2 );
187448
187449 nOrig = sqlite3_value_bytes(argv[0]);
187450 aOrig = (const char*)sqlite3_value_blob(argv[0]);
187451 nDelta = sqlite3_value_bytes(argv[1]);
187452 aDelta = (const char*)sqlite3_value_blob(argv[1]);
187453
187454 /* Figure out the size of the output */
187455 nOut = rbuDeltaOutputSize(aDelta, nDelta);
187456 if( nOut<0 ){
187457 sqlite3_result_error(context, "corrupt fossil delta", -1);
187458 return;
187459 }
187460
187461 aOut = sqlite3_malloc(nOut+1);
187462 if( aOut==0 ){
187463 sqlite3_result_error_nomem(context);
187464 }else{
187465 nOut2 = rbuDeltaApply(aOrig, nOrig, aDelta, nDelta, aOut);
187466 if( nOut2!=nOut ){
187467 sqlite3_result_error(context, "corrupt fossil delta", -1);
187468 }else{
187469 sqlite3_result_blob(context, aOut, nOut, sqlite3_free);
187470 }
187471 }
187472}
187473
187474
187475/*
187476** Prepare the SQL statement in buffer zSql against database handle db.
187477** If successful, set *ppStmt to point to the new statement and return
187478** SQLITE_OK.
187479**
187480** Otherwise, if an error does occur, set *ppStmt to NULL and return
187481** an SQLite error code. Additionally, set output variable *pzErrmsg to
187482** point to a buffer containing an error message. It is the responsibility
187483** of the caller to (eventually) free this buffer using sqlite3_free().
187484*/
187485static int prepareAndCollectError(
187486 sqlite3 *db,
187487 sqlite3_stmt **ppStmt,
187488 char **pzErrmsg,
187489 const char *zSql
187490){
187491 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
187492 if( rc!=SQLITE_OK ){
187493 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
187494 *ppStmt = 0;
187495 }
187496 return rc;
187497}
187498
187499/*
187500** Reset the SQL statement passed as the first argument. Return a copy
187501** of the value returned by sqlite3_reset().
187502**
187503** If an error has occurred, then set *pzErrmsg to point to a buffer
187504** containing an error message. It is the responsibility of the caller
187505** to eventually free this buffer using sqlite3_free().
187506*/
187507static int resetAndCollectError(sqlite3_stmt *pStmt, char **pzErrmsg){
187508 int rc = sqlite3_reset(pStmt);
187509 if( rc!=SQLITE_OK ){
187510 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(sqlite3_db_handle(pStmt)));
187511 }
187512 return rc;
187513}
187514
187515/*
187516** Unless it is NULL, argument zSql points to a buffer allocated using
187517** sqlite3_malloc containing an SQL statement. This function prepares the SQL
187518** statement against database db and frees the buffer. If statement
187519** compilation is successful, *ppStmt is set to point to the new statement
187520** handle and SQLITE_OK is returned.
187521**
187522** Otherwise, if an error occurs, *ppStmt is set to NULL and an error code
187523** returned. In this case, *pzErrmsg may also be set to point to an error
187524** message. It is the responsibility of the caller to free this error message
187525** buffer using sqlite3_free().
187526**
187527** If argument zSql is NULL, this function assumes that an OOM has occurred.
187528** In this case SQLITE_NOMEM is returned and *ppStmt set to NULL.
187529*/
187530static int prepareFreeAndCollectError(
187531 sqlite3 *db,
187532 sqlite3_stmt **ppStmt,
187533 char **pzErrmsg,
187534 char *zSql
187535){
187536 int rc;
187537 assert( *pzErrmsg==0 );
187538 if( zSql==0 ){
187539 rc = SQLITE_NOMEM;
187540 *ppStmt = 0;
187541 }else{
187542 rc = prepareAndCollectError(db, ppStmt, pzErrmsg, zSql);
187543 sqlite3_free(zSql);
187544 }
187545 return rc;
187546}
187547
187548/*
187549** Free the RbuObjIter.azTblCol[] and RbuObjIter.abTblPk[] arrays allocated
187550** by an earlier call to rbuObjIterCacheTableInfo().
187551*/
187552static void rbuObjIterFreeCols(RbuObjIter *pIter){
187553 int i;
187554 for(i=0; i<pIter->nTblCol; i++){
187555 sqlite3_free(pIter->azTblCol[i]);
187556 sqlite3_free(pIter->azTblType[i]);
187557 }
187558 sqlite3_free(pIter->azTblCol);
187559 pIter->azTblCol = 0;
187560 pIter->azTblType = 0;
187561 pIter->aiSrcOrder = 0;
187562 pIter->abTblPk = 0;
187563 pIter->abNotNull = 0;
187564 pIter->nTblCol = 0;
187565 pIter->eType = 0; /* Invalid value */
187566}
187567
187568/*
187569** Finalize all statements and free all allocations that are specific to
187570** the current object (table/index pair).
187571*/
187572static void rbuObjIterClearStatements(RbuObjIter *pIter){
187573 RbuUpdateStmt *pUp;
187574
187575 sqlite3_finalize(pIter->pSelect);
187576 sqlite3_finalize(pIter->pInsert);
187577 sqlite3_finalize(pIter->pDelete);
187578 sqlite3_finalize(pIter->pTmpInsert);
187579 pUp = pIter->pRbuUpdate;
187580 while( pUp ){
187581 RbuUpdateStmt *pTmp = pUp->pNext;
187582 sqlite3_finalize(pUp->pUpdate);
187583 sqlite3_free(pUp);
187584 pUp = pTmp;
187585 }
187586
187587 pIter->pSelect = 0;
187588 pIter->pInsert = 0;
187589 pIter->pDelete = 0;
187590 pIter->pRbuUpdate = 0;
187591 pIter->pTmpInsert = 0;
187592 pIter->nCol = 0;
187593}
187594
187595/*
187596** Clean up any resources allocated as part of the iterator object passed
187597** as the only argument.
187598*/
187599static void rbuObjIterFinalize(RbuObjIter *pIter){
187600 rbuObjIterClearStatements(pIter);
187601 sqlite3_finalize(pIter->pTblIter);
187602 sqlite3_finalize(pIter->pIdxIter);
187603 rbuObjIterFreeCols(pIter);
187604 memset(pIter, 0, sizeof(RbuObjIter));
187605}
187606
187607/*
187608** Advance the iterator to the next position.
187609**
187610** If no error occurs, SQLITE_OK is returned and the iterator is left
187611** pointing to the next entry. Otherwise, an error code and message is
187612** left in the RBU handle passed as the first argument. A copy of the
187613** error code is returned.
187614*/
187615static int rbuObjIterNext(sqlite3rbu *p, RbuObjIter *pIter){
187616 int rc = p->rc;
187617 if( rc==SQLITE_OK ){
187618
187619 /* Free any SQLite statements used while processing the previous object */
187620 rbuObjIterClearStatements(pIter);
187621 if( pIter->zIdx==0 ){
187622 rc = sqlite3_exec(p->dbMain,
187623 "DROP TRIGGER IF EXISTS temp.rbu_insert_tr;"
187624 "DROP TRIGGER IF EXISTS temp.rbu_update1_tr;"
187625 "DROP TRIGGER IF EXISTS temp.rbu_update2_tr;"
187626 "DROP TRIGGER IF EXISTS temp.rbu_delete_tr;"
187627 , 0, 0, &p->zErrmsg
187628 );
187629 }
187630
187631 if( rc==SQLITE_OK ){
187632 if( pIter->bCleanup ){
187633 rbuObjIterFreeCols(pIter);
187634 pIter->bCleanup = 0;
187635 rc = sqlite3_step(pIter->pTblIter);
187636 if( rc!=SQLITE_ROW ){
187637 rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
187638 pIter->zTbl = 0;
187639 }else{
187640 pIter->zTbl = (const char*)sqlite3_column_text(pIter->pTblIter, 0);
187641 pIter->zDataTbl = (const char*)sqlite3_column_text(pIter->pTblIter,1);
187642 rc = (pIter->zDataTbl && pIter->zTbl) ? SQLITE_OK : SQLITE_NOMEM;
187643 }
187644 }else{
187645 if( pIter->zIdx==0 ){
187646 sqlite3_stmt *pIdx = pIter->pIdxIter;
187647 rc = sqlite3_bind_text(pIdx, 1, pIter->zTbl, -1, SQLITE_STATIC);
187648 }
187649 if( rc==SQLITE_OK ){
187650 rc = sqlite3_step(pIter->pIdxIter);
187651 if( rc!=SQLITE_ROW ){
187652 rc = resetAndCollectError(pIter->pIdxIter, &p->zErrmsg);
187653 pIter->bCleanup = 1;
187654 pIter->zIdx = 0;
187655 }else{
187656 pIter->zIdx = (const char*)sqlite3_column_text(pIter->pIdxIter, 0);
187657 pIter->iTnum = sqlite3_column_int(pIter->pIdxIter, 1);
187658 pIter->bUnique = sqlite3_column_int(pIter->pIdxIter, 2);
187659 rc = pIter->zIdx ? SQLITE_OK : SQLITE_NOMEM;
187660 }
187661 }
187662 }
187663 }
187664 }
187665
187666 if( rc!=SQLITE_OK ){
187667 rbuObjIterFinalize(pIter);
187668 p->rc = rc;
187669 }
187670 return rc;
187671}
187672
187673
187674/*
187675** The implementation of the rbu_target_name() SQL function. This function
187676** accepts one or two arguments. The first argument is the name of a table -
187677** the name of a table in the RBU database. The second, if it is present, is 1
187678** for a view or 0 for a table.
187679**
187680** For a non-vacuum RBU handle, if the table name matches the pattern:
187681**
187682** data[0-9]_<name>
187683**
187684** where <name> is any sequence of 1 or more characters, <name> is returned.
187685** Otherwise, if the only argument does not match the above pattern, an SQL
187686** NULL is returned.
187687**
187688** "data_t1" -> "t1"
187689** "data0123_t2" -> "t2"
187690** "dataAB_t3" -> NULL
187691**
187692** For an rbu vacuum handle, a copy of the first argument is returned if
187693** the second argument is either missing or 0 (not a view).
187694*/
187695static void rbuTargetNameFunc(
187696 sqlite3_context *pCtx,
187697 int argc,
187698 sqlite3_value **argv
187699){
187700 sqlite3rbu *p = sqlite3_user_data(pCtx);
187701 const char *zIn;
187702 assert( argc==1 || argc==2 );
187703
187704 zIn = (const char*)sqlite3_value_text(argv[0]);
187705 if( zIn ){
187706 if( rbuIsVacuum(p) ){
187707 if( argc==1 || 0==sqlite3_value_int(argv[1]) ){
187708 sqlite3_result_text(pCtx, zIn, -1, SQLITE_STATIC);
187709 }
187710 }else{
187711 if( strlen(zIn)>4 && memcmp("data", zIn, 4)==0 ){
187712 int i;
187713 for(i=4; zIn[i]>='0' && zIn[i]<='9'; i++);
187714 if( zIn[i]=='_' && zIn[i+1] ){
187715 sqlite3_result_text(pCtx, &zIn[i+1], -1, SQLITE_STATIC);
187716 }
187717 }
187718 }
187719 }
187720}
187721
187722/*
187723** Initialize the iterator structure passed as the second argument.
187724**
187725** If no error occurs, SQLITE_OK is returned and the iterator is left
187726** pointing to the first entry. Otherwise, an error code and message is
187727** left in the RBU handle passed as the first argument. A copy of the
187728** error code is returned.
187729*/
187730static int rbuObjIterFirst(sqlite3rbu *p, RbuObjIter *pIter){
187731 int rc;
187732 memset(pIter, 0, sizeof(RbuObjIter));
187733
187734 rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pTblIter, &p->zErrmsg,
187735 sqlite3_mprintf(
187736 "SELECT rbu_target_name(name, type='view') AS target, name "
187737 "FROM sqlite_master "
187738 "WHERE type IN ('table', 'view') AND target IS NOT NULL "
187739 " %s "
187740 "ORDER BY name"
187741 , rbuIsVacuum(p) ? "AND rootpage!=0 AND rootpage IS NOT NULL" : ""));
187742
187743 if( rc==SQLITE_OK ){
187744 rc = prepareAndCollectError(p->dbMain, &pIter->pIdxIter, &p->zErrmsg,
187745 "SELECT name, rootpage, sql IS NULL OR substr(8, 6)=='UNIQUE' "
187746 " FROM main.sqlite_master "
187747 " WHERE type='index' AND tbl_name = ?"
187748 );
187749 }
187750
187751 pIter->bCleanup = 1;
187752 p->rc = rc;
187753 return rbuObjIterNext(p, pIter);
187754}
187755
187756/*
187757** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
187758** an error code is stored in the RBU handle passed as the first argument.
187759**
187760** If an error has already occurred (p->rc is already set to something other
187761** than SQLITE_OK), then this function returns NULL without modifying the
187762** stored error code. In this case it still calls sqlite3_free() on any
187763** printf() parameters associated with %z conversions.
187764*/
187765static char *rbuMPrintf(sqlite3rbu *p, const char *zFmt, ...){
187766 char *zSql = 0;
187767 va_list ap;
187768 va_start(ap, zFmt);
187769 zSql = sqlite3_vmprintf(zFmt, ap);
187770 if( p->rc==SQLITE_OK ){
187771 if( zSql==0 ) p->rc = SQLITE_NOMEM;
187772 }else{
187773 sqlite3_free(zSql);
187774 zSql = 0;
187775 }
187776 va_end(ap);
187777 return zSql;
187778}
187779
187780/*
187781** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
187782** arguments are the usual subsitution values. This function performs
187783** the printf() style substitutions and executes the result as an SQL
187784** statement on the RBU handles database.
187785**
187786** If an error occurs, an error code and error message is stored in the
187787** RBU handle. If an error has already occurred when this function is
187788** called, it is a no-op.
187789*/
187790static int rbuMPrintfExec(sqlite3rbu *p, sqlite3 *db, const char *zFmt, ...){
187791 va_list ap;
187792 char *zSql;
187793 va_start(ap, zFmt);
187794 zSql = sqlite3_vmprintf(zFmt, ap);
187795 if( p->rc==SQLITE_OK ){
187796 if( zSql==0 ){
187797 p->rc = SQLITE_NOMEM;
187798 }else{
187799 p->rc = sqlite3_exec(db, zSql, 0, 0, &p->zErrmsg);
187800 }
187801 }
187802 sqlite3_free(zSql);
187803 va_end(ap);
187804 return p->rc;
187805}
187806
187807/*
187808** Attempt to allocate and return a pointer to a zeroed block of nByte
187809** bytes.
187810**
187811** If an error (i.e. an OOM condition) occurs, return NULL and leave an
187812** error code in the rbu handle passed as the first argument. Or, if an
187813** error has already occurred when this function is called, return NULL
187814** immediately without attempting the allocation or modifying the stored
187815** error code.
187816*/
187817static void *rbuMalloc(sqlite3rbu *p, int nByte){
187818 void *pRet = 0;
187819 if( p->rc==SQLITE_OK ){
187820 assert( nByte>0 );
187821 pRet = sqlite3_malloc64(nByte);
187822 if( pRet==0 ){
187823 p->rc = SQLITE_NOMEM;
187824 }else{
187825 memset(pRet, 0, nByte);
187826 }
187827 }
187828 return pRet;
187829}
187830
187831
187832/*
187833** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
187834** there is room for at least nCol elements. If an OOM occurs, store an
187835** error code in the RBU handle passed as the first argument.
187836*/
187837static void rbuAllocateIterArrays(sqlite3rbu *p, RbuObjIter *pIter, int nCol){
187838 int nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol;
187839 char **azNew;
187840
187841 azNew = (char**)rbuMalloc(p, nByte);
187842 if( azNew ){
187843 pIter->azTblCol = azNew;
187844 pIter->azTblType = &azNew[nCol];
187845 pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
187846 pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
187847 pIter->abNotNull = (u8*)&pIter->abTblPk[nCol];
187848 pIter->abIndexed = (u8*)&pIter->abNotNull[nCol];
187849 }
187850}
187851
187852/*
187853** The first argument must be a nul-terminated string. This function
187854** returns a copy of the string in memory obtained from sqlite3_malloc().
187855** It is the responsibility of the caller to eventually free this memory
187856** using sqlite3_free().
187857**
187858** If an OOM condition is encountered when attempting to allocate memory,
187859** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
187860** if the allocation succeeds, (*pRc) is left unchanged.
187861*/
187862static char *rbuStrndup(const char *zStr, int *pRc){
187863 char *zRet = 0;
187864
187865 assert( *pRc==SQLITE_OK );
187866 if( zStr ){
187867 size_t nCopy = strlen(zStr) + 1;
187868 zRet = (char*)sqlite3_malloc64(nCopy);
187869 if( zRet ){
187870 memcpy(zRet, zStr, nCopy);
187871 }else{
187872 *pRc = SQLITE_NOMEM;
187873 }
187874 }
187875
187876 return zRet;
187877}
187878
187879/*
187880** Finalize the statement passed as the second argument.
187881**
187882** If the sqlite3_finalize() call indicates that an error occurs, and the
187883** rbu handle error code is not already set, set the error code and error
187884** message accordingly.
187885*/
187886static void rbuFinalize(sqlite3rbu *p, sqlite3_stmt *pStmt){
187887 sqlite3 *db = sqlite3_db_handle(pStmt);
187888 int rc = sqlite3_finalize(pStmt);
187889 if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){
187890 p->rc = rc;
187891 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
187892 }
187893}
187894
187895/* Determine the type of a table.
187896**
187897** peType is of type (int*), a pointer to an output parameter of type
187898** (int). This call sets the output parameter as follows, depending
187899** on the type of the table specified by parameters dbName and zTbl.
187900**
187901** RBU_PK_NOTABLE: No such table.
187902** RBU_PK_NONE: Table has an implicit rowid.
187903** RBU_PK_IPK: Table has an explicit IPK column.
187904** RBU_PK_EXTERNAL: Table has an external PK index.
187905** RBU_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
187906** RBU_PK_VTAB: Table is a virtual table.
187907**
187908** Argument *piPk is also of type (int*), and also points to an output
187909** parameter. Unless the table has an external primary key index
187910** (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
187911** if the table does have an external primary key index, then *piPk
187912** is set to the root page number of the primary key index before
187913** returning.
187914**
187915** ALGORITHM:
187916**
187917** if( no entry exists in sqlite_master ){
187918** return RBU_PK_NOTABLE
187919** }else if( sql for the entry starts with "CREATE VIRTUAL" ){
187920** return RBU_PK_VTAB
187921** }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
187922** if( the index that is the pk exists in sqlite_master ){
187923** *piPK = rootpage of that index.
187924** return RBU_PK_EXTERNAL
187925** }else{
187926** return RBU_PK_WITHOUT_ROWID
187927** }
187928** }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
187929** return RBU_PK_IPK
187930** }else{
187931** return RBU_PK_NONE
187932** }
187933*/
187934static void rbuTableType(
187935 sqlite3rbu *p,
187936 const char *zTab,
187937 int *peType,
187938 int *piTnum,
187939 int *piPk
187940){
187941 /*
187942 ** 0) SELECT count(*) FROM sqlite_master where name=%Q AND IsVirtual(%Q)
187943 ** 1) PRAGMA index_list = ?
187944 ** 2) SELECT count(*) FROM sqlite_master where name=%Q
187945 ** 3) PRAGMA table_info = ?
187946 */
187947 sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
187948
187949 *peType = RBU_PK_NOTABLE;
187950 *piPk = 0;
187951
187952 assert( p->rc==SQLITE_OK );
187953 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg,
187954 sqlite3_mprintf(
187955 "SELECT (sql LIKE 'create virtual%%'), rootpage"
187956 " FROM sqlite_master"
187957 " WHERE name=%Q", zTab
187958 ));
187959 if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){
187960 /* Either an error, or no such table. */
187961 goto rbuTableType_end;
187962 }
187963 if( sqlite3_column_int(aStmt[0], 0) ){
187964 *peType = RBU_PK_VTAB; /* virtual table */
187965 goto rbuTableType_end;
187966 }
187967 *piTnum = sqlite3_column_int(aStmt[0], 1);
187968
187969 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg,
187970 sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
187971 );
187972 if( p->rc ) goto rbuTableType_end;
187973 while( sqlite3_step(aStmt[1])==SQLITE_ROW ){
187974 const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
187975 const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
187976 if( zOrig && zIdx && zOrig[0]=='p' ){
187977 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg,
187978 sqlite3_mprintf(
187979 "SELECT rootpage FROM sqlite_master WHERE name = %Q", zIdx
187980 ));
187981 if( p->rc==SQLITE_OK ){
187982 if( sqlite3_step(aStmt[2])==SQLITE_ROW ){
187983 *piPk = sqlite3_column_int(aStmt[2], 0);
187984 *peType = RBU_PK_EXTERNAL;
187985 }else{
187986 *peType = RBU_PK_WITHOUT_ROWID;
187987 }
187988 }
187989 goto rbuTableType_end;
187990 }
187991 }
187992
187993 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg,
187994 sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
187995 );
187996 if( p->rc==SQLITE_OK ){
187997 while( sqlite3_step(aStmt[3])==SQLITE_ROW ){
187998 if( sqlite3_column_int(aStmt[3],5)>0 ){
187999 *peType = RBU_PK_IPK; /* explicit IPK column */
188000 goto rbuTableType_end;
188001 }
188002 }
188003 *peType = RBU_PK_NONE;
188004 }
188005
188006rbuTableType_end: {
188007 unsigned int i;
188008 for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
188009 rbuFinalize(p, aStmt[i]);
188010 }
188011 }
188012}
188013
188014/*
188015** This is a helper function for rbuObjIterCacheTableInfo(). It populates
188016** the pIter->abIndexed[] array.
188017*/
188018static void rbuObjIterCacheIndexedCols(sqlite3rbu *p, RbuObjIter *pIter){
188019 sqlite3_stmt *pList = 0;
188020 int bIndex = 0;
188021
188022 if( p->rc==SQLITE_OK ){
188023 memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
188024 p->rc = prepareFreeAndCollectError(p->dbMain, &pList, &p->zErrmsg,
188025 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
188026 );
188027 }
188028
188029 pIter->nIndex = 0;
188030 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pList) ){
188031 const char *zIdx = (const char*)sqlite3_column_text(pList, 1);
188032 sqlite3_stmt *pXInfo = 0;
188033 if( zIdx==0 ) break;
188034 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
188035 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
188036 );
188037 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
188038 int iCid = sqlite3_column_int(pXInfo, 1);
188039 if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
188040 }
188041 rbuFinalize(p, pXInfo);
188042 bIndex = 1;
188043 pIter->nIndex++;
188044 }
188045
188046 if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
188047 /* "PRAGMA index_list" includes the main PK b-tree */
188048 pIter->nIndex--;
188049 }
188050
188051 rbuFinalize(p, pList);
188052 if( bIndex==0 ) pIter->abIndexed = 0;
188053}
188054
188055
188056/*
188057** If they are not already populated, populate the pIter->azTblCol[],
188058** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
188059** the table (not index) that the iterator currently points to.
188060**
188061** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
188062** an error does occur, an error code and error message are also left in
188063** the RBU handle.
188064*/
188065static int rbuObjIterCacheTableInfo(sqlite3rbu *p, RbuObjIter *pIter){
188066 if( pIter->azTblCol==0 ){
188067 sqlite3_stmt *pStmt = 0;
188068 int nCol = 0;
188069 int i; /* for() loop iterator variable */
188070 int bRbuRowid = 0; /* If input table has column "rbu_rowid" */
188071 int iOrder = 0;
188072 int iTnum = 0;
188073
188074 /* Figure out the type of table this step will deal with. */
188075 assert( pIter->eType==0 );
188076 rbuTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
188077 if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_NOTABLE ){
188078 p->rc = SQLITE_ERROR;
188079 p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
188080 }
188081 if( p->rc ) return p->rc;
188082 if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
188083
188084 assert( pIter->eType==RBU_PK_NONE || pIter->eType==RBU_PK_IPK
188085 || pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_WITHOUT_ROWID
188086 || pIter->eType==RBU_PK_VTAB
188087 );
188088
188089 /* Populate the azTblCol[] and nTblCol variables based on the columns
188090 ** of the input table. Ignore any input table columns that begin with
188091 ** "rbu_". */
188092 p->rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
188093 sqlite3_mprintf("SELECT * FROM '%q'", pIter->zDataTbl)
188094 );
188095 if( p->rc==SQLITE_OK ){
188096 nCol = sqlite3_column_count(pStmt);
188097 rbuAllocateIterArrays(p, pIter, nCol);
188098 }
188099 for(i=0; p->rc==SQLITE_OK && i<nCol; i++){
188100 const char *zName = (const char*)sqlite3_column_name(pStmt, i);
188101 if( sqlite3_strnicmp("rbu_", zName, 4) ){
188102 char *zCopy = rbuStrndup(zName, &p->rc);
188103 pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
188104 pIter->azTblCol[pIter->nTblCol++] = zCopy;
188105 }
188106 else if( 0==sqlite3_stricmp("rbu_rowid", zName) ){
188107 bRbuRowid = 1;
188108 }
188109 }
188110 sqlite3_finalize(pStmt);
188111 pStmt = 0;
188112
188113 if( p->rc==SQLITE_OK
188114 && rbuIsVacuum(p)==0
188115 && bRbuRowid!=(pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
188116 ){
188117 p->rc = SQLITE_ERROR;
188118 p->zErrmsg = sqlite3_mprintf(
188119 "table %q %s rbu_rowid column", pIter->zDataTbl,
188120 (bRbuRowid ? "may not have" : "requires")
188121 );
188122 }
188123
188124 /* Check that all non-HIDDEN columns in the destination table are also
188125 ** present in the input table. Populate the abTblPk[], azTblType[] and
188126 ** aiTblOrder[] arrays at the same time. */
188127 if( p->rc==SQLITE_OK ){
188128 p->rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
188129 sqlite3_mprintf("PRAGMA table_info(%Q)", pIter->zTbl)
188130 );
188131 }
188132 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
188133 const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
188134 if( zName==0 ) break; /* An OOM - finalize() below returns S_NOMEM */
188135 for(i=iOrder; i<pIter->nTblCol; i++){
188136 if( 0==strcmp(zName, pIter->azTblCol[i]) ) break;
188137 }
188138 if( i==pIter->nTblCol ){
188139 p->rc = SQLITE_ERROR;
188140 p->zErrmsg = sqlite3_mprintf("column missing from %q: %s",
188141 pIter->zDataTbl, zName
188142 );
188143 }else{
188144 int iPk = sqlite3_column_int(pStmt, 5);
188145 int bNotNull = sqlite3_column_int(pStmt, 3);
188146 const char *zType = (const char*)sqlite3_column_text(pStmt, 2);
188147
188148 if( i!=iOrder ){
188149 SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]);
188150 SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]);
188151 }
188152
188153 pIter->azTblType[iOrder] = rbuStrndup(zType, &p->rc);
188154 pIter->abTblPk[iOrder] = (iPk!=0);
188155 pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
188156 iOrder++;
188157 }
188158 }
188159
188160 rbuFinalize(p, pStmt);
188161 rbuObjIterCacheIndexedCols(p, pIter);
188162 assert( pIter->eType!=RBU_PK_VTAB || pIter->abIndexed==0 );
188163 assert( pIter->eType!=RBU_PK_VTAB || pIter->nIndex==0 );
188164 }
188165
188166 return p->rc;
188167}
188168
188169/*
188170** This function constructs and returns a pointer to a nul-terminated
188171** string containing some SQL clause or list based on one or more of the
188172** column names currently stored in the pIter->azTblCol[] array.
188173*/
188174static char *rbuObjIterGetCollist(
188175 sqlite3rbu *p, /* RBU object */
188176 RbuObjIter *pIter /* Object iterator for column names */
188177){
188178 char *zList = 0;
188179 const char *zSep = "";
188180 int i;
188181 for(i=0; i<pIter->nTblCol; i++){
188182 const char *z = pIter->azTblCol[i];
188183 zList = rbuMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
188184 zSep = ", ";
188185 }
188186 return zList;
188187}
188188
188189/*
188190** This function is used to create a SELECT list (the list of SQL
188191** expressions that follows a SELECT keyword) for a SELECT statement
188192** used to read from an data_xxx or rbu_tmp_xxx table while updating the
188193** index object currently indicated by the iterator object passed as the
188194** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
188195** to obtain the required information.
188196**
188197** If the index is of the following form:
188198**
188199** CREATE INDEX i1 ON t1(c, b COLLATE nocase);
188200**
188201** and "t1" is a table with an explicit INTEGER PRIMARY KEY column
188202** "ipk", the returned string is:
188203**
188204** "`c` COLLATE 'BINARY', `b` COLLATE 'NOCASE', `ipk` COLLATE 'BINARY'"
188205**
188206** As well as the returned string, three other malloc'd strings are
188207** returned via output parameters. As follows:
188208**
188209** pzImposterCols: ...
188210** pzImposterPk: ...
188211** pzWhere: ...
188212*/
188213static char *rbuObjIterGetIndexCols(
188214 sqlite3rbu *p, /* RBU object */
188215 RbuObjIter *pIter, /* Object iterator for column names */
188216 char **pzImposterCols, /* OUT: Columns for imposter table */
188217 char **pzImposterPk, /* OUT: Imposter PK clause */
188218 char **pzWhere, /* OUT: WHERE clause */
188219 int *pnBind /* OUT: Trbul number of columns */
188220){
188221 int rc = p->rc; /* Error code */
188222 int rc2; /* sqlite3_finalize() return code */
188223 char *zRet = 0; /* String to return */
188224 char *zImpCols = 0; /* String to return via *pzImposterCols */
188225 char *zImpPK = 0; /* String to return via *pzImposterPK */
188226 char *zWhere = 0; /* String to return via *pzWhere */
188227 int nBind = 0; /* Value to return via *pnBind */
188228 const char *zCom = ""; /* Set to ", " later on */
188229 const char *zAnd = ""; /* Set to " AND " later on */
188230 sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = ? */
188231
188232 if( rc==SQLITE_OK ){
188233 assert( p->zErrmsg==0 );
188234 rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
188235 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
188236 );
188237 }
188238
188239 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
188240 int iCid = sqlite3_column_int(pXInfo, 1);
188241 int bDesc = sqlite3_column_int(pXInfo, 3);
188242 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
188243 const char *zCol;
188244 const char *zType;
188245
188246 if( iCid<0 ){
188247 /* An integer primary key. If the table has an explicit IPK, use
188248 ** its name. Otherwise, use "rbu_rowid". */
188249 if( pIter->eType==RBU_PK_IPK ){
188250 int i;
188251 for(i=0; pIter->abTblPk[i]==0; i++);
188252 assert( i<pIter->nTblCol );
188253 zCol = pIter->azTblCol[i];
188254 }else if( rbuIsVacuum(p) ){
188255 zCol = "_rowid_";
188256 }else{
188257 zCol = "rbu_rowid";
188258 }
188259 zType = "INTEGER";
188260 }else{
188261 zCol = pIter->azTblCol[iCid];
188262 zType = pIter->azTblType[iCid];
188263 }
188264
188265 zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom, zCol, zCollate);
188266 if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
188267 const char *zOrder = (bDesc ? " DESC" : "");
188268 zImpPK = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\"%s",
188269 zImpPK, zCom, nBind, zCol, zOrder
188270 );
188271 }
188272 zImpCols = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\" %s COLLATE %Q",
188273 zImpCols, zCom, nBind, zCol, zType, zCollate
188274 );
188275 zWhere = sqlite3_mprintf(
188276 "%z%s\"rbu_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
188277 );
188278 if( zRet==0 || zImpPK==0 || zImpCols==0 || zWhere==0 ) rc = SQLITE_NOMEM;
188279 zCom = ", ";
188280 zAnd = " AND ";
188281 nBind++;
188282 }
188283
188284 rc2 = sqlite3_finalize(pXInfo);
188285 if( rc==SQLITE_OK ) rc = rc2;
188286
188287 if( rc!=SQLITE_OK ){
188288 sqlite3_free(zRet);
188289 sqlite3_free(zImpCols);
188290 sqlite3_free(zImpPK);
188291 sqlite3_free(zWhere);
188292 zRet = 0;
188293 zImpCols = 0;
188294 zImpPK = 0;
188295 zWhere = 0;
188296 p->rc = rc;
188297 }
188298
188299 *pzImposterCols = zImpCols;
188300 *pzImposterPk = zImpPK;
188301 *pzWhere = zWhere;
188302 *pnBind = nBind;
188303 return zRet;
188304}
188305
188306/*
188307** Assuming the current table columns are "a", "b" and "c", and the zObj
188308** paramter is passed "old", return a string of the form:
188309**
188310** "old.a, old.b, old.b"
188311**
188312** With the column names escaped.
188313**
188314** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append
188315** the text ", old._rowid_" to the returned value.
188316*/
188317static char *rbuObjIterGetOldlist(
188318 sqlite3rbu *p,
188319 RbuObjIter *pIter,
188320 const char *zObj
188321){
188322 char *zList = 0;
188323 if( p->rc==SQLITE_OK && pIter->abIndexed ){
188324 const char *zS = "";
188325 int i;
188326 for(i=0; i<pIter->nTblCol; i++){
188327 if( pIter->abIndexed[i] ){
188328 const char *zCol = pIter->azTblCol[i];
188329 zList = sqlite3_mprintf("%z%s%s.\"%w\"", zList, zS, zObj, zCol);
188330 }else{
188331 zList = sqlite3_mprintf("%z%sNULL", zList, zS);
188332 }
188333 zS = ", ";
188334 if( zList==0 ){
188335 p->rc = SQLITE_NOMEM;
188336 break;
188337 }
188338 }
188339
188340 /* For a table with implicit rowids, append "old._rowid_" to the list. */
188341 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
188342 zList = rbuMPrintf(p, "%z, %s._rowid_", zList, zObj);
188343 }
188344 }
188345 return zList;
188346}
188347
188348/*
188349** Return an expression that can be used in a WHERE clause to match the
188350** primary key of the current table. For example, if the table is:
188351**
188352** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c));
188353**
188354** Return the string:
188355**
188356** "b = ?1 AND c = ?2"
188357*/
188358static char *rbuObjIterGetWhere(
188359 sqlite3rbu *p,
188360 RbuObjIter *pIter
188361){
188362 char *zList = 0;
188363 if( pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE ){
188364 zList = rbuMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
188365 }else if( pIter->eType==RBU_PK_EXTERNAL ){
188366 const char *zSep = "";
188367 int i;
188368 for(i=0; i<pIter->nTblCol; i++){
188369 if( pIter->abTblPk[i] ){
188370 zList = rbuMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
188371 zSep = " AND ";
188372 }
188373 }
188374 zList = rbuMPrintf(p,
188375 "_rowid_ = (SELECT id FROM rbu_imposter2 WHERE %z)", zList
188376 );
188377
188378 }else{
188379 const char *zSep = "";
188380 int i;
188381 for(i=0; i<pIter->nTblCol; i++){
188382 if( pIter->abTblPk[i] ){
188383 const char *zCol = pIter->azTblCol[i];
188384 zList = rbuMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
188385 zSep = " AND ";
188386 }
188387 }
188388 }
188389 return zList;
188390}
188391
188392/*
188393** The SELECT statement iterating through the keys for the current object
188394** (p->objiter.pSelect) currently points to a valid row. However, there
188395** is something wrong with the rbu_control value in the rbu_control value
188396** stored in the (p->nCol+1)'th column. Set the error code and error message
188397** of the RBU handle to something reflecting this.
188398*/
188399static void rbuBadControlError(sqlite3rbu *p){
188400 p->rc = SQLITE_ERROR;
188401 p->zErrmsg = sqlite3_mprintf("invalid rbu_control value");
188402}
188403
188404
188405/*
188406** Return a nul-terminated string containing the comma separated list of
188407** assignments that should be included following the "SET" keyword of
188408** an UPDATE statement used to update the table object that the iterator
188409** passed as the second argument currently points to if the rbu_control
188410** column of the data_xxx table entry is set to zMask.
188411**
188412** The memory for the returned string is obtained from sqlite3_malloc().
188413** It is the responsibility of the caller to eventually free it using
188414** sqlite3_free().
188415**
188416** If an OOM error is encountered when allocating space for the new
188417** string, an error code is left in the rbu handle passed as the first
188418** argument and NULL is returned. Or, if an error has already occurred
188419** when this function is called, NULL is returned immediately, without
188420** attempting the allocation or modifying the stored error code.
188421*/
188422static char *rbuObjIterGetSetlist(
188423 sqlite3rbu *p,
188424 RbuObjIter *pIter,
188425 const char *zMask
188426){
188427 char *zList = 0;
188428 if( p->rc==SQLITE_OK ){
188429 int i;
188430
188431 if( (int)strlen(zMask)!=pIter->nTblCol ){
188432 rbuBadControlError(p);
188433 }else{
188434 const char *zSep = "";
188435 for(i=0; i<pIter->nTblCol; i++){
188436 char c = zMask[pIter->aiSrcOrder[i]];
188437 if( c=='x' ){
188438 zList = rbuMPrintf(p, "%z%s\"%w\"=?%d",
188439 zList, zSep, pIter->azTblCol[i], i+1
188440 );
188441 zSep = ", ";
188442 }
188443 else if( c=='d' ){
188444 zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_delta(\"%w\", ?%d)",
188445 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
188446 );
188447 zSep = ", ";
188448 }
188449 else if( c=='f' ){
188450 zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_fossil_delta(\"%w\", ?%d)",
188451 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
188452 );
188453 zSep = ", ";
188454 }
188455 }
188456 }
188457 }
188458 return zList;
188459}
188460
188461/*
188462** Return a nul-terminated string consisting of nByte comma separated
188463** "?" expressions. For example, if nByte is 3, return a pointer to
188464** a buffer containing the string "?,?,?".
188465**
188466** The memory for the returned string is obtained from sqlite3_malloc().
188467** It is the responsibility of the caller to eventually free it using
188468** sqlite3_free().
188469**
188470** If an OOM error is encountered when allocating space for the new
188471** string, an error code is left in the rbu handle passed as the first
188472** argument and NULL is returned. Or, if an error has already occurred
188473** when this function is called, NULL is returned immediately, without
188474** attempting the allocation or modifying the stored error code.
188475*/
188476static char *rbuObjIterGetBindlist(sqlite3rbu *p, int nBind){
188477 char *zRet = 0;
188478 int nByte = nBind*2 + 1;
188479
188480 zRet = (char*)rbuMalloc(p, nByte);
188481 if( zRet ){
188482 int i;
188483 for(i=0; i<nBind; i++){
188484 zRet[i*2] = '?';
188485 zRet[i*2+1] = (i+1==nBind) ? '\0' : ',';
188486 }
188487 }
188488 return zRet;
188489}
188490
188491/*
188492** The iterator currently points to a table (not index) of type
188493** RBU_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
188494** declaration for the corresponding imposter table. For example,
188495** if the iterator points to a table created as:
188496**
188497** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
188498**
188499** this function returns:
188500**
188501** PRIMARY KEY("b", "a" DESC)
188502*/
188503static char *rbuWithoutRowidPK(sqlite3rbu *p, RbuObjIter *pIter){
188504 char *z = 0;
188505 assert( pIter->zIdx==0 );
188506 if( p->rc==SQLITE_OK ){
188507 const char *zSep = "PRIMARY KEY(";
188508 sqlite3_stmt *pXList = 0; /* PRAGMA index_list = (pIter->zTbl) */
188509 sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = <pk-index> */
188510
188511 p->rc = prepareFreeAndCollectError(p->dbMain, &pXList, &p->zErrmsg,
188512 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
188513 );
188514 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXList) ){
188515 const char *zOrig = (const char*)sqlite3_column_text(pXList,3);
188516 if( zOrig && strcmp(zOrig, "pk")==0 ){
188517 const char *zIdx = (const char*)sqlite3_column_text(pXList,1);
188518 if( zIdx ){
188519 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
188520 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
188521 );
188522 }
188523 break;
188524 }
188525 }
188526 rbuFinalize(p, pXList);
188527
188528 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
188529 if( sqlite3_column_int(pXInfo, 5) ){
188530 /* int iCid = sqlite3_column_int(pXInfo, 0); */
188531 const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
188532 const char *zDesc = sqlite3_column_int(pXInfo, 3) ? " DESC" : "";
188533 z = rbuMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
188534 zSep = ", ";
188535 }
188536 }
188537 z = rbuMPrintf(p, "%z)", z);
188538 rbuFinalize(p, pXInfo);
188539 }
188540 return z;
188541}
188542
188543/*
188544** This function creates the second imposter table used when writing to
188545** a table b-tree where the table has an external primary key. If the
188546** iterator passed as the second argument does not currently point to
188547** a table (not index) with an external primary key, this function is a
188548** no-op.
188549**
188550** Assuming the iterator does point to a table with an external PK, this
188551** function creates a WITHOUT ROWID imposter table named "rbu_imposter2"
188552** used to access that PK index. For example, if the target table is
188553** declared as follows:
188554**
188555** CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
188556**
188557** then the imposter table schema is:
188558**
188559** CREATE TABLE rbu_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
188560**
188561*/
188562static void rbuCreateImposterTable2(sqlite3rbu *p, RbuObjIter *pIter){
188563 if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_EXTERNAL ){
188564 int tnum = pIter->iPkTnum; /* Root page of PK index */
188565 sqlite3_stmt *pQuery = 0; /* SELECT name ... WHERE rootpage = $tnum */
188566 const char *zIdx = 0; /* Name of PK index */
188567 sqlite3_stmt *pXInfo = 0; /* PRAGMA main.index_xinfo = $zIdx */
188568 const char *zComma = "";
188569 char *zCols = 0; /* Used to build up list of table cols */
188570 char *zPk = 0; /* Used to build up table PK declaration */
188571
188572 /* Figure out the name of the primary key index for the current table.
188573 ** This is needed for the argument to "PRAGMA index_xinfo". Set
188574 ** zIdx to point to a nul-terminated string containing this name. */
188575 p->rc = prepareAndCollectError(p->dbMain, &pQuery, &p->zErrmsg,
188576 "SELECT name FROM sqlite_master WHERE rootpage = ?"
188577 );
188578 if( p->rc==SQLITE_OK ){
188579 sqlite3_bind_int(pQuery, 1, tnum);
188580 if( SQLITE_ROW==sqlite3_step(pQuery) ){
188581 zIdx = (const char*)sqlite3_column_text(pQuery, 0);
188582 }
188583 }
188584 if( zIdx ){
188585 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
188586 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
188587 );
188588 }
188589 rbuFinalize(p, pQuery);
188590
188591 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
188592 int bKey = sqlite3_column_int(pXInfo, 5);
188593 if( bKey ){
188594 int iCid = sqlite3_column_int(pXInfo, 1);
188595 int bDesc = sqlite3_column_int(pXInfo, 3);
188596 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
188597 zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %Q", zCols, zComma,
188598 iCid, pIter->azTblType[iCid], zCollate
188599 );
188600 zPk = rbuMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
188601 zComma = ", ";
188602 }
188603 }
188604 zCols = rbuMPrintf(p, "%z, id INTEGER", zCols);
188605 rbuFinalize(p, pXInfo);
188606
188607 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
188608 rbuMPrintfExec(p, p->dbMain,
188609 "CREATE TABLE rbu_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
188610 zCols, zPk
188611 );
188612 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
188613 }
188614}
188615
188616/*
188617** If an error has already occurred when this function is called, it
188618** immediately returns zero (without doing any work). Or, if an error
188619** occurs during the execution of this function, it sets the error code
188620** in the sqlite3rbu object indicated by the first argument and returns
188621** zero.
188622**
188623** The iterator passed as the second argument is guaranteed to point to
188624** a table (not an index) when this function is called. This function
188625** attempts to create any imposter table required to write to the main
188626** table b-tree of the table before returning. Non-zero is returned if
188627** an imposter table are created, or zero otherwise.
188628**
188629** An imposter table is required in all cases except RBU_PK_VTAB. Only
188630** virtual tables are written to directly. The imposter table has the
188631** same schema as the actual target table (less any UNIQUE constraints).
188632** More precisely, the "same schema" means the same columns, types,
188633** collation sequences. For tables that do not have an external PRIMARY
188634** KEY, it also means the same PRIMARY KEY declaration.
188635*/
188636static void rbuCreateImposterTable(sqlite3rbu *p, RbuObjIter *pIter){
188637 if( p->rc==SQLITE_OK && pIter->eType!=RBU_PK_VTAB ){
188638 int tnum = pIter->iTnum;
188639 const char *zComma = "";
188640 char *zSql = 0;
188641 int iCol;
188642 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
188643
188644 for(iCol=0; p->rc==SQLITE_OK && iCol<pIter->nTblCol; iCol++){
188645 const char *zPk = "";
188646 const char *zCol = pIter->azTblCol[iCol];
188647 const char *zColl = 0;
188648
188649 p->rc = sqlite3_table_column_metadata(
188650 p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
188651 );
188652
188653 if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){
188654 /* If the target table column is an "INTEGER PRIMARY KEY", add
188655 ** "PRIMARY KEY" to the imposter table column declaration. */
188656 zPk = "PRIMARY KEY ";
188657 }
188658 zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %Q%s",
188659 zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
188660 (pIter->abNotNull[iCol] ? " NOT NULL" : "")
188661 );
188662 zComma = ", ";
188663 }
188664
188665 if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
188666 char *zPk = rbuWithoutRowidPK(p, pIter);
188667 if( zPk ){
188668 zSql = rbuMPrintf(p, "%z, %z", zSql, zPk);
188669 }
188670 }
188671
188672 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
188673 rbuMPrintfExec(p, p->dbMain, "CREATE TABLE \"rbu_imp_%w\"(%z)%s",
188674 pIter->zTbl, zSql,
188675 (pIter->eType==RBU_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
188676 );
188677 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
188678 }
188679}
188680
188681/*
188682** Prepare a statement used to insert rows into the "rbu_tmp_xxx" table.
188683** Specifically a statement of the form:
188684**
188685** INSERT INTO rbu_tmp_xxx VALUES(?, ?, ? ...);
188686**
188687** The number of bound variables is equal to the number of columns in
188688** the target table, plus one (for the rbu_control column), plus one more
188689** (for the rbu_rowid column) if the target table is an implicit IPK or
188690** virtual table.
188691*/
188692static void rbuObjIterPrepareTmpInsert(
188693 sqlite3rbu *p,
188694 RbuObjIter *pIter,
188695 const char *zCollist,
188696 const char *zRbuRowid
188697){
188698 int bRbuRowid = (pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE);
188699 char *zBind = rbuObjIterGetBindlist(p, pIter->nTblCol + 1 + bRbuRowid);
188700 if( zBind ){
188701 assert( pIter->pTmpInsert==0 );
188702 p->rc = prepareFreeAndCollectError(
188703 p->dbRbu, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
188704 "INSERT INTO %s.'rbu_tmp_%q'(rbu_control,%s%s) VALUES(%z)",
188705 p->zStateDb, pIter->zDataTbl, zCollist, zRbuRowid, zBind
188706 ));
188707 }
188708}
188709
188710static void rbuTmpInsertFunc(
188711 sqlite3_context *pCtx,
188712 int nVal,
188713 sqlite3_value **apVal
188714){
188715 sqlite3rbu *p = sqlite3_user_data(pCtx);
188716 int rc = SQLITE_OK;
188717 int i;
188718
188719 assert( sqlite3_value_int(apVal[0])!=0
188720 || p->objiter.eType==RBU_PK_EXTERNAL
188721 || p->objiter.eType==RBU_PK_NONE
188722 );
188723 if( sqlite3_value_int(apVal[0])!=0 ){
188724 p->nPhaseOneStep += p->objiter.nIndex;
188725 }
188726
188727 for(i=0; rc==SQLITE_OK && i<nVal; i++){
188728 rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
188729 }
188730 if( rc==SQLITE_OK ){
188731 sqlite3_step(p->objiter.pTmpInsert);
188732 rc = sqlite3_reset(p->objiter.pTmpInsert);
188733 }
188734
188735 if( rc!=SQLITE_OK ){
188736 sqlite3_result_error_code(pCtx, rc);
188737 }
188738}
188739
188740/*
188741** Ensure that the SQLite statement handles required to update the
188742** target database object currently indicated by the iterator passed
188743** as the second argument are available.
188744*/
188745static int rbuObjIterPrepareAll(
188746 sqlite3rbu *p,
188747 RbuObjIter *pIter,
188748 int nOffset /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
188749){
188750 assert( pIter->bCleanup==0 );
188751 if( pIter->pSelect==0 && rbuObjIterCacheTableInfo(p, pIter)==SQLITE_OK ){
188752 const int tnum = pIter->iTnum;
188753 char *zCollist = 0; /* List of indexed columns */
188754 char **pz = &p->zErrmsg;
188755 const char *zIdx = pIter->zIdx;
188756 char *zLimit = 0;
188757
188758 if( nOffset ){
188759 zLimit = sqlite3_mprintf(" LIMIT -1 OFFSET %d", nOffset);
188760 if( !zLimit ) p->rc = SQLITE_NOMEM;
188761 }
188762
188763 if( zIdx ){
188764 const char *zTbl = pIter->zTbl;
188765 char *zImposterCols = 0; /* Columns for imposter table */
188766 char *zImposterPK = 0; /* Primary key declaration for imposter */
188767 char *zWhere = 0; /* WHERE clause on PK columns */
188768 char *zBind = 0;
188769 int nBind = 0;
188770
188771 assert( pIter->eType!=RBU_PK_VTAB );
188772 zCollist = rbuObjIterGetIndexCols(
188773 p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
188774 );
188775 zBind = rbuObjIterGetBindlist(p, nBind);
188776
188777 /* Create the imposter table used to write to this index. */
188778 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
188779 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1,tnum);
188780 rbuMPrintfExec(p, p->dbMain,
188781 "CREATE TABLE \"rbu_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
188782 zTbl, zImposterCols, zImposterPK
188783 );
188784 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
188785
188786 /* Create the statement to insert index entries */
188787 pIter->nCol = nBind;
188788 if( p->rc==SQLITE_OK ){
188789 p->rc = prepareFreeAndCollectError(
188790 p->dbMain, &pIter->pInsert, &p->zErrmsg,
188791 sqlite3_mprintf("INSERT INTO \"rbu_imp_%w\" VALUES(%s)", zTbl, zBind)
188792 );
188793 }
188794
188795 /* And to delete index entries */
188796 if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK ){
188797 p->rc = prepareFreeAndCollectError(
188798 p->dbMain, &pIter->pDelete, &p->zErrmsg,
188799 sqlite3_mprintf("DELETE FROM \"rbu_imp_%w\" WHERE %s", zTbl, zWhere)
188800 );
188801 }
188802
188803 /* Create the SELECT statement to read keys in sorted order */
188804 if( p->rc==SQLITE_OK ){
188805 char *zSql;
188806 if( rbuIsVacuum(p) ){
188807 zSql = sqlite3_mprintf(
188808 "SELECT %s, 0 AS rbu_control FROM '%q' ORDER BY %s%s",
188809 zCollist,
188810 pIter->zDataTbl,
188811 zCollist, zLimit
188812 );
188813 }else
188814
188815 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
188816 zSql = sqlite3_mprintf(
188817 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' ORDER BY %s%s",
188818 zCollist, p->zStateDb, pIter->zDataTbl,
188819 zCollist, zLimit
188820 );
188821 }else{
188822 zSql = sqlite3_mprintf(
188823 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' "
188824 "UNION ALL "
188825 "SELECT %s, rbu_control FROM '%q' "
188826 "WHERE typeof(rbu_control)='integer' AND rbu_control!=1 "
188827 "ORDER BY %s%s",
188828 zCollist, p->zStateDb, pIter->zDataTbl,
188829 zCollist, pIter->zDataTbl,
188830 zCollist, zLimit
188831 );
188832 }
188833 p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz, zSql);
188834 }
188835
188836 sqlite3_free(zImposterCols);
188837 sqlite3_free(zImposterPK);
188838 sqlite3_free(zWhere);
188839 sqlite3_free(zBind);
188840 }else{
188841 int bRbuRowid = (pIter->eType==RBU_PK_VTAB)
188842 ||(pIter->eType==RBU_PK_NONE)
188843 ||(pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p));
188844 const char *zTbl = pIter->zTbl; /* Table this step applies to */
188845 const char *zWrite; /* Imposter table name */
188846
188847 char *zBindings = rbuObjIterGetBindlist(p, pIter->nTblCol + bRbuRowid);
188848 char *zWhere = rbuObjIterGetWhere(p, pIter);
188849 char *zOldlist = rbuObjIterGetOldlist(p, pIter, "old");
188850 char *zNewlist = rbuObjIterGetOldlist(p, pIter, "new");
188851
188852 zCollist = rbuObjIterGetCollist(p, pIter);
188853 pIter->nCol = pIter->nTblCol;
188854
188855 /* Create the imposter table or tables (if required). */
188856 rbuCreateImposterTable(p, pIter);
188857 rbuCreateImposterTable2(p, pIter);
188858 zWrite = (pIter->eType==RBU_PK_VTAB ? "" : "rbu_imp_");
188859
188860 /* Create the INSERT statement to write to the target PK b-tree */
188861 if( p->rc==SQLITE_OK ){
188862 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
188863 sqlite3_mprintf(
188864 "INSERT INTO \"%s%w\"(%s%s) VALUES(%s)",
188865 zWrite, zTbl, zCollist, (bRbuRowid ? ", _rowid_" : ""), zBindings
188866 )
188867 );
188868 }
188869
188870 /* Create the DELETE statement to write to the target PK b-tree.
188871 ** Because it only performs INSERT operations, this is not required for
188872 ** an rbu vacuum handle. */
188873 if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK ){
188874 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pDelete, pz,
188875 sqlite3_mprintf(
188876 "DELETE FROM \"%s%w\" WHERE %s", zWrite, zTbl, zWhere
188877 )
188878 );
188879 }
188880
188881 if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
188882 const char *zRbuRowid = "";
188883 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
188884 zRbuRowid = ", rbu_rowid";
188885 }
188886
188887 /* Create the rbu_tmp_xxx table and the triggers to populate it. */
188888 rbuMPrintfExec(p, p->dbRbu,
188889 "CREATE TABLE IF NOT EXISTS %s.'rbu_tmp_%q' AS "
188890 "SELECT *%s FROM '%q' WHERE 0;"
188891 , p->zStateDb, pIter->zDataTbl
188892 , (pIter->eType==RBU_PK_EXTERNAL ? ", 0 AS rbu_rowid" : "")
188893 , pIter->zDataTbl
188894 );
188895
188896 rbuMPrintfExec(p, p->dbMain,
188897 "CREATE TEMP TRIGGER rbu_delete_tr BEFORE DELETE ON \"%s%w\" "
188898 "BEGIN "
188899 " SELECT rbu_tmp_insert(3, %s);"
188900 "END;"
188901
188902 "CREATE TEMP TRIGGER rbu_update1_tr BEFORE UPDATE ON \"%s%w\" "
188903 "BEGIN "
188904 " SELECT rbu_tmp_insert(3, %s);"
188905 "END;"
188906
188907 "CREATE TEMP TRIGGER rbu_update2_tr AFTER UPDATE ON \"%s%w\" "
188908 "BEGIN "
188909 " SELECT rbu_tmp_insert(4, %s);"
188910 "END;",
188911 zWrite, zTbl, zOldlist,
188912 zWrite, zTbl, zOldlist,
188913 zWrite, zTbl, zNewlist
188914 );
188915
188916 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
188917 rbuMPrintfExec(p, p->dbMain,
188918 "CREATE TEMP TRIGGER rbu_insert_tr AFTER INSERT ON \"%s%w\" "
188919 "BEGIN "
188920 " SELECT rbu_tmp_insert(0, %s);"
188921 "END;",
188922 zWrite, zTbl, zNewlist
188923 );
188924 }
188925
188926 rbuObjIterPrepareTmpInsert(p, pIter, zCollist, zRbuRowid);
188927 }
188928
188929 /* Create the SELECT statement to read keys from data_xxx */
188930 if( p->rc==SQLITE_OK ){
188931 const char *zRbuRowid = "";
188932 if( bRbuRowid ){
188933 zRbuRowid = rbuIsVacuum(p) ? ",_rowid_ " : ",rbu_rowid";
188934 }
188935 p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz,
188936 sqlite3_mprintf(
188937 "SELECT %s,%s rbu_control%s FROM '%q'%s",
188938 zCollist,
188939 (rbuIsVacuum(p) ? "0 AS " : ""),
188940 zRbuRowid,
188941 pIter->zDataTbl, zLimit
188942 )
188943 );
188944 }
188945
188946 sqlite3_free(zWhere);
188947 sqlite3_free(zOldlist);
188948 sqlite3_free(zNewlist);
188949 sqlite3_free(zBindings);
188950 }
188951 sqlite3_free(zCollist);
188952 sqlite3_free(zLimit);
188953 }
188954
188955 return p->rc;
188956}
188957
188958/*
188959** Set output variable *ppStmt to point to an UPDATE statement that may
188960** be used to update the imposter table for the main table b-tree of the
188961** table object that pIter currently points to, assuming that the
188962** rbu_control column of the data_xyz table contains zMask.
188963**
188964** If the zMask string does not specify any columns to update, then this
188965** is not an error. Output variable *ppStmt is set to NULL in this case.
188966*/
188967static int rbuGetUpdateStmt(
188968 sqlite3rbu *p, /* RBU handle */
188969 RbuObjIter *pIter, /* Object iterator */
188970 const char *zMask, /* rbu_control value ('x.x.') */
188971 sqlite3_stmt **ppStmt /* OUT: UPDATE statement handle */
188972){
188973 RbuUpdateStmt **pp;
188974 RbuUpdateStmt *pUp = 0;
188975 int nUp = 0;
188976
188977 /* In case an error occurs */
188978 *ppStmt = 0;
188979
188980 /* Search for an existing statement. If one is found, shift it to the front
188981 ** of the LRU queue and return immediately. Otherwise, leave nUp pointing
188982 ** to the number of statements currently in the cache and pUp to the
188983 ** last object in the list. */
188984 for(pp=&pIter->pRbuUpdate; *pp; pp=&((*pp)->pNext)){
188985 pUp = *pp;
188986 if( strcmp(pUp->zMask, zMask)==0 ){
188987 *pp = pUp->pNext;
188988 pUp->pNext = pIter->pRbuUpdate;
188989 pIter->pRbuUpdate = pUp;
188990 *ppStmt = pUp->pUpdate;
188991 return SQLITE_OK;
188992 }
188993 nUp++;
188994 }
188995 assert( pUp==0 || pUp->pNext==0 );
188996
188997 if( nUp>=SQLITE_RBU_UPDATE_CACHESIZE ){
188998 for(pp=&pIter->pRbuUpdate; *pp!=pUp; pp=&((*pp)->pNext));
188999 *pp = 0;
189000 sqlite3_finalize(pUp->pUpdate);
189001 pUp->pUpdate = 0;
189002 }else{
189003 pUp = (RbuUpdateStmt*)rbuMalloc(p, sizeof(RbuUpdateStmt)+pIter->nTblCol+1);
189004 }
189005
189006 if( pUp ){
189007 char *zWhere = rbuObjIterGetWhere(p, pIter);
189008 char *zSet = rbuObjIterGetSetlist(p, pIter, zMask);
189009 char *zUpdate = 0;
189010
189011 pUp->zMask = (char*)&pUp[1];
189012 memcpy(pUp->zMask, zMask, pIter->nTblCol);
189013 pUp->pNext = pIter->pRbuUpdate;
189014 pIter->pRbuUpdate = pUp;
189015
189016 if( zSet ){
189017 const char *zPrefix = "";
189018
189019 if( pIter->eType!=RBU_PK_VTAB ) zPrefix = "rbu_imp_";
189020 zUpdate = sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s",
189021 zPrefix, pIter->zTbl, zSet, zWhere
189022 );
189023 p->rc = prepareFreeAndCollectError(
189024 p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate
189025 );
189026 *ppStmt = pUp->pUpdate;
189027 }
189028 sqlite3_free(zWhere);
189029 sqlite3_free(zSet);
189030 }
189031
189032 return p->rc;
189033}
189034
189035static sqlite3 *rbuOpenDbhandle(
189036 sqlite3rbu *p,
189037 const char *zName,
189038 int bUseVfs
189039){
189040 sqlite3 *db = 0;
189041 if( p->rc==SQLITE_OK ){
189042 const int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_URI;
189043 p->rc = sqlite3_open_v2(zName, &db, flags, bUseVfs ? p->zVfsName : 0);
189044 if( p->rc ){
189045 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
189046 sqlite3_close(db);
189047 db = 0;
189048 }
189049 }
189050 return db;
189051}
189052
189053/*
189054** Free an RbuState object allocated by rbuLoadState().
189055*/
189056static void rbuFreeState(RbuState *p){
189057 if( p ){
189058 sqlite3_free(p->zTbl);
189059 sqlite3_free(p->zDataTbl);
189060 sqlite3_free(p->zIdx);
189061 sqlite3_free(p);
189062 }
189063}
189064
189065/*
189066** Allocate an RbuState object and load the contents of the rbu_state
189067** table into it. Return a pointer to the new object. It is the
189068** responsibility of the caller to eventually free the object using
189069** sqlite3_free().
189070**
189071** If an error occurs, leave an error code and message in the rbu handle
189072** and return NULL.
189073*/
189074static RbuState *rbuLoadState(sqlite3rbu *p){
189075 RbuState *pRet = 0;
189076 sqlite3_stmt *pStmt = 0;
189077 int rc;
189078 int rc2;
189079
189080 pRet = (RbuState*)rbuMalloc(p, sizeof(RbuState));
189081 if( pRet==0 ) return 0;
189082
189083 rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
189084 sqlite3_mprintf("SELECT k, v FROM %s.rbu_state", p->zStateDb)
189085 );
189086 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
189087 switch( sqlite3_column_int(pStmt, 0) ){
189088 case RBU_STATE_STAGE:
189089 pRet->eStage = sqlite3_column_int(pStmt, 1);
189090 if( pRet->eStage!=RBU_STAGE_OAL
189091 && pRet->eStage!=RBU_STAGE_MOVE
189092 && pRet->eStage!=RBU_STAGE_CKPT
189093 ){
189094 p->rc = SQLITE_CORRUPT;
189095 }
189096 break;
189097
189098 case RBU_STATE_TBL:
189099 pRet->zTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
189100 break;
189101
189102 case RBU_STATE_IDX:
189103 pRet->zIdx = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
189104 break;
189105
189106 case RBU_STATE_ROW:
189107 pRet->nRow = sqlite3_column_int(pStmt, 1);
189108 break;
189109
189110 case RBU_STATE_PROGRESS:
189111 pRet->nProgress = sqlite3_column_int64(pStmt, 1);
189112 break;
189113
189114 case RBU_STATE_CKPT:
189115 pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
189116 break;
189117
189118 case RBU_STATE_COOKIE:
189119 pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
189120 break;
189121
189122 case RBU_STATE_OALSZ:
189123 pRet->iOalSz = (u32)sqlite3_column_int64(pStmt, 1);
189124 break;
189125
189126 case RBU_STATE_PHASEONESTEP:
189127 pRet->nPhaseOneStep = sqlite3_column_int64(pStmt, 1);
189128 break;
189129
189130 case RBU_STATE_DATATBL:
189131 pRet->zDataTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
189132 break;
189133
189134 default:
189135 rc = SQLITE_CORRUPT;
189136 break;
189137 }
189138 }
189139 rc2 = sqlite3_finalize(pStmt);
189140 if( rc==SQLITE_OK ) rc = rc2;
189141
189142 p->rc = rc;
189143 return pRet;
189144}
189145
189146
189147/*
189148** Open the database handle and attach the RBU database as "rbu". If an
189149** error occurs, leave an error code and message in the RBU handle.
189150*/
189151static void rbuOpenDatabase(sqlite3rbu *p, int *pbRetry){
189152 assert( p->rc || (p->dbMain==0 && p->dbRbu==0) );
189153 assert( p->rc || rbuIsVacuum(p) || p->zTarget!=0 );
189154
189155 /* Open the RBU database */
189156 p->dbRbu = rbuOpenDbhandle(p, p->zRbu, 1);
189157
189158 if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
189159 sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
189160 if( p->zState==0 ){
189161 const char *zFile = sqlite3_db_filename(p->dbRbu, "main");
189162 p->zState = rbuMPrintf(p, "file://%s-vacuum?modeof=%s", zFile, zFile);
189163 }
189164 }
189165
189166 /* If using separate RBU and state databases, attach the state database to
189167 ** the RBU db handle now. */
189168 if( p->zState ){
189169 rbuMPrintfExec(p, p->dbRbu, "ATTACH %Q AS stat", p->zState);
189170 memcpy(p->zStateDb, "stat", 4);
189171 }else{
189172 memcpy(p->zStateDb, "main", 4);
189173 }
189174
189175#if 0
189176 if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
189177 p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, 0);
189178 }
189179#endif
189180
189181 /* If it has not already been created, create the rbu_state table */
189182 rbuMPrintfExec(p, p->dbRbu, RBU_CREATE_STATE, p->zStateDb);
189183
189184#if 0
189185 if( rbuIsVacuum(p) ){
189186 if( p->rc==SQLITE_OK ){
189187 int rc2;
189188 int bOk = 0;
189189 sqlite3_stmt *pCnt = 0;
189190 p->rc = prepareAndCollectError(p->dbRbu, &pCnt, &p->zErrmsg,
189191 "SELECT count(*) FROM stat.sqlite_master"
189192 );
189193 if( p->rc==SQLITE_OK
189194 && sqlite3_step(pCnt)==SQLITE_ROW
189195 && 1==sqlite3_column_int(pCnt, 0)
189196 ){
189197 bOk = 1;
189198 }
189199 rc2 = sqlite3_finalize(pCnt);
189200 if( p->rc==SQLITE_OK ) p->rc = rc2;
189201
189202 if( p->rc==SQLITE_OK && bOk==0 ){
189203 p->rc = SQLITE_ERROR;
189204 p->zErrmsg = sqlite3_mprintf("invalid state database");
189205 }
189206
189207 if( p->rc==SQLITE_OK ){
189208 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
189209 }
189210 }
189211 }
189212#endif
189213
189214 if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
189215 int bOpen = 0;
189216 int rc;
189217 p->nRbu = 0;
189218 p->pRbuFd = 0;
189219 rc = sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
189220 if( rc!=SQLITE_NOTFOUND ) p->rc = rc;
189221 if( p->eStage>=RBU_STAGE_MOVE ){
189222 bOpen = 1;
189223 }else{
189224 RbuState *pState = rbuLoadState(p);
189225 if( pState ){
189226 bOpen = (pState->eStage>=RBU_STAGE_MOVE);
189227 rbuFreeState(pState);
189228 }
189229 }
189230 if( bOpen ) p->dbMain = rbuOpenDbhandle(p, p->zRbu, p->nRbu<=1);
189231 }
189232
189233 p->eStage = 0;
189234 if( p->rc==SQLITE_OK && p->dbMain==0 ){
189235 if( !rbuIsVacuum(p) ){
189236 p->dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
189237 }else if( p->pRbuFd->pWalFd ){
189238 if( pbRetry ){
189239 p->pRbuFd->bNolock = 0;
189240 sqlite3_close(p->dbRbu);
189241 sqlite3_close(p->dbMain);
189242 p->dbMain = 0;
189243 p->dbRbu = 0;
189244 *pbRetry = 1;
189245 return;
189246 }
189247 p->rc = SQLITE_ERROR;
189248 p->zErrmsg = sqlite3_mprintf("cannot vacuum wal mode database");
189249 }else{
189250 char *zTarget;
189251 char *zExtra = 0;
189252 if( strlen(p->zRbu)>=5 && 0==memcmp("file:", p->zRbu, 5) ){
189253 zExtra = &p->zRbu[5];
189254 while( *zExtra ){
189255 if( *zExtra++=='?' ) break;
189256 }
189257 if( *zExtra=='\0' ) zExtra = 0;
189258 }
189259
189260 zTarget = sqlite3_mprintf("file:%s-vacuum?rbu_memory=1%s%s",
189261 sqlite3_db_filename(p->dbRbu, "main"),
189262 (zExtra==0 ? "" : "&"), (zExtra==0 ? "" : zExtra)
189263 );
189264
189265 if( zTarget==0 ){
189266 p->rc = SQLITE_NOMEM;
189267 return;
189268 }
189269 p->dbMain = rbuOpenDbhandle(p, zTarget, p->nRbu<=1);
189270 sqlite3_free(zTarget);
189271 }
189272 }
189273
189274 if( p->rc==SQLITE_OK ){
189275 p->rc = sqlite3_create_function(p->dbMain,
189276 "rbu_tmp_insert", -1, SQLITE_UTF8, (void*)p, rbuTmpInsertFunc, 0, 0
189277 );
189278 }
189279
189280 if( p->rc==SQLITE_OK ){
189281 p->rc = sqlite3_create_function(p->dbMain,
189282 "rbu_fossil_delta", 2, SQLITE_UTF8, 0, rbuFossilDeltaFunc, 0, 0
189283 );
189284 }
189285
189286 if( p->rc==SQLITE_OK ){
189287 p->rc = sqlite3_create_function(p->dbRbu,
189288 "rbu_target_name", -1, SQLITE_UTF8, (void*)p, rbuTargetNameFunc, 0, 0
189289 );
189290 }
189291
189292 if( p->rc==SQLITE_OK ){
189293 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
189294 }
189295 rbuMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_master");
189296
189297 /* Mark the database file just opened as an RBU target database. If
189298 ** this call returns SQLITE_NOTFOUND, then the RBU vfs is not in use.
189299 ** This is an error. */
189300 if( p->rc==SQLITE_OK ){
189301 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
189302 }
189303
189304 if( p->rc==SQLITE_NOTFOUND ){
189305 p->rc = SQLITE_ERROR;
189306 p->zErrmsg = sqlite3_mprintf("rbu vfs not found");
189307 }
189308}
189309
189310/*
189311** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
189312** It is a no-op unless SQLITE_ENABLE_8_3_NAMES is defined.
189313**
189314** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
189315** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
189316** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
189317** three characters, then shorten the suffix on z[] to be the last three
189318** characters of the original suffix.
189319**
189320** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
189321** do the suffix shortening regardless of URI parameter.
189322**
189323** Examples:
189324**
189325** test.db-journal => test.nal
189326** test.db-wal => test.wal
189327** test.db-shm => test.shm
189328** test.db-mj7f3319fa => test.9fa
189329*/
189330static void rbuFileSuffix3(const char *zBase, char *z){
189331#ifdef SQLITE_ENABLE_8_3_NAMES
189332#if SQLITE_ENABLE_8_3_NAMES<2
189333 if( sqlite3_uri_boolean(zBase, "8_3_names", 0) )
189334#endif
189335 {
189336 int i, sz;
189337 sz = (int)strlen(z)&0xffffff;
189338 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
189339 if( z[i]=='.' && sz>i+4 ) memmove(&z[i+1], &z[sz-3], 4);
189340 }
189341#endif
189342}
189343
189344/*
189345** Return the current wal-index header checksum for the target database
189346** as a 64-bit integer.
189347**
189348** The checksum is store in the first page of xShmMap memory as an 8-byte
189349** blob starting at byte offset 40.
189350*/
189351static i64 rbuShmChecksum(sqlite3rbu *p){
189352 i64 iRet = 0;
189353 if( p->rc==SQLITE_OK ){
189354 sqlite3_file *pDb = p->pTargetFd->pReal;
189355 u32 volatile *ptr;
189356 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
189357 if( p->rc==SQLITE_OK ){
189358 iRet = ((i64)ptr[10] << 32) + ptr[11];
189359 }
189360 }
189361 return iRet;
189362}
189363
189364/*
189365** This function is called as part of initializing or reinitializing an
189366** incremental checkpoint.
189367**
189368** It populates the sqlite3rbu.aFrame[] array with the set of
189369** (wal frame -> db page) copy operations required to checkpoint the
189370** current wal file, and obtains the set of shm locks required to safely
189371** perform the copy operations directly on the file-system.
189372**
189373** If argument pState is not NULL, then the incremental checkpoint is
189374** being resumed. In this case, if the checksum of the wal-index-header
189375** following recovery is not the same as the checksum saved in the RbuState
189376** object, then the rbu handle is set to DONE state. This occurs if some
189377** other client appends a transaction to the wal file in the middle of
189378** an incremental checkpoint.
189379*/
189380static void rbuSetupCheckpoint(sqlite3rbu *p, RbuState *pState){
189381
189382 /* If pState is NULL, then the wal file may not have been opened and
189383 ** recovered. Running a read-statement here to ensure that doing so
189384 ** does not interfere with the "capture" process below. */
189385 if( pState==0 ){
189386 p->eStage = 0;
189387 if( p->rc==SQLITE_OK ){
189388 p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_master", 0, 0, 0);
189389 }
189390 }
189391
189392 /* Assuming no error has occurred, run a "restart" checkpoint with the
189393 ** sqlite3rbu.eStage variable set to CAPTURE. This turns on the following
189394 ** special behaviour in the rbu VFS:
189395 **
189396 ** * If the exclusive shm WRITER or READ0 lock cannot be obtained,
189397 ** the checkpoint fails with SQLITE_BUSY (normally SQLite would
189398 ** proceed with running a passive checkpoint instead of failing).
189399 **
189400 ** * Attempts to read from the *-wal file or write to the database file
189401 ** do not perform any IO. Instead, the frame/page combinations that
189402 ** would be read/written are recorded in the sqlite3rbu.aFrame[]
189403 ** array.
189404 **
189405 ** * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER,
189406 ** READ0 and CHECKPOINT locks taken as part of the checkpoint are
189407 ** no-ops. These locks will not be released until the connection
189408 ** is closed.
189409 **
189410 ** * Attempting to xSync() the database file causes an SQLITE_INTERNAL
189411 ** error.
189412 **
189413 ** As a result, unless an error (i.e. OOM or SQLITE_BUSY) occurs, the
189414 ** checkpoint below fails with SQLITE_INTERNAL, and leaves the aFrame[]
189415 ** array populated with a set of (frame -> page) mappings. Because the
189416 ** WRITER, CHECKPOINT and READ0 locks are still held, it is safe to copy
189417 ** data from the wal file into the database file according to the
189418 ** contents of aFrame[].
189419 */
189420 if( p->rc==SQLITE_OK ){
189421 int rc2;
189422 p->eStage = RBU_STAGE_CAPTURE;
189423 rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
189424 if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
189425 }
189426
189427 if( p->rc==SQLITE_OK && p->nFrame>0 ){
189428 p->eStage = RBU_STAGE_CKPT;
189429 p->nStep = (pState ? pState->nRow : 0);
189430 p->aBuf = rbuMalloc(p, p->pgsz);
189431 p->iWalCksum = rbuShmChecksum(p);
189432 }
189433
189434 if( p->rc==SQLITE_OK ){
189435 if( p->nFrame==0 || (pState && pState->iWalCksum!=p->iWalCksum) ){
189436 p->rc = SQLITE_DONE;
189437 p->eStage = RBU_STAGE_DONE;
189438 }else{
189439 int nSectorSize;
189440 sqlite3_file *pDb = p->pTargetFd->pReal;
189441 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
189442 assert( p->nPagePerSector==0 );
189443 nSectorSize = pDb->pMethods->xSectorSize(pDb);
189444 if( nSectorSize>p->pgsz ){
189445 p->nPagePerSector = nSectorSize / p->pgsz;
189446 }else{
189447 p->nPagePerSector = 1;
189448 }
189449
189450 /* Call xSync() on the wal file. This causes SQLite to sync the
189451 ** directory in which the target database and the wal file reside, in
189452 ** case it has not been synced since the rename() call in
189453 ** rbuMoveOalFile(). */
189454 p->rc = pWal->pMethods->xSync(pWal, SQLITE_SYNC_NORMAL);
189455 }
189456 }
189457}
189458
189459/*
189460** Called when iAmt bytes are read from offset iOff of the wal file while
189461** the rbu object is in capture mode. Record the frame number of the frame
189462** being read in the aFrame[] array.
189463*/
189464static int rbuCaptureWalRead(sqlite3rbu *pRbu, i64 iOff, int iAmt){
189465 const u32 mReq = (1<<WAL_LOCK_WRITE)|(1<<WAL_LOCK_CKPT)|(1<<WAL_LOCK_READ0);
189466 u32 iFrame;
189467
189468 if( pRbu->mLock!=mReq ){
189469 pRbu->rc = SQLITE_BUSY;
189470 return SQLITE_INTERNAL;
189471 }
189472
189473 pRbu->pgsz = iAmt;
189474 if( pRbu->nFrame==pRbu->nFrameAlloc ){
189475 int nNew = (pRbu->nFrameAlloc ? pRbu->nFrameAlloc : 64) * 2;
189476 RbuFrame *aNew;
189477 aNew = (RbuFrame*)sqlite3_realloc64(pRbu->aFrame, nNew * sizeof(RbuFrame));
189478 if( aNew==0 ) return SQLITE_NOMEM;
189479 pRbu->aFrame = aNew;
189480 pRbu->nFrameAlloc = nNew;
189481 }
189482
189483 iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
189484 if( pRbu->iMaxFrame<iFrame ) pRbu->iMaxFrame = iFrame;
189485 pRbu->aFrame[pRbu->nFrame].iWalFrame = iFrame;
189486 pRbu->aFrame[pRbu->nFrame].iDbPage = 0;
189487 pRbu->nFrame++;
189488 return SQLITE_OK;
189489}
189490
189491/*
189492** Called when a page of data is written to offset iOff of the database
189493** file while the rbu handle is in capture mode. Record the page number
189494** of the page being written in the aFrame[] array.
189495*/
189496static int rbuCaptureDbWrite(sqlite3rbu *pRbu, i64 iOff){
189497 pRbu->aFrame[pRbu->nFrame-1].iDbPage = (u32)(iOff / pRbu->pgsz) + 1;
189498 return SQLITE_OK;
189499}
189500
189501/*
189502** This is called as part of an incremental checkpoint operation. Copy
189503** a single frame of data from the wal file into the database file, as
189504** indicated by the RbuFrame object.
189505*/
189506static void rbuCheckpointFrame(sqlite3rbu *p, RbuFrame *pFrame){
189507 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
189508 sqlite3_file *pDb = p->pTargetFd->pReal;
189509 i64 iOff;
189510
189511 assert( p->rc==SQLITE_OK );
189512 iOff = (i64)(pFrame->iWalFrame-1) * (p->pgsz + 24) + 32 + 24;
189513 p->rc = pWal->pMethods->xRead(pWal, p->aBuf, p->pgsz, iOff);
189514 if( p->rc ) return;
189515
189516 iOff = (i64)(pFrame->iDbPage-1) * p->pgsz;
189517 p->rc = pDb->pMethods->xWrite(pDb, p->aBuf, p->pgsz, iOff);
189518}
189519
189520
189521/*
189522** Take an EXCLUSIVE lock on the database file.
189523*/
189524static void rbuLockDatabase(sqlite3rbu *p){
189525 sqlite3_file *pReal = p->pTargetFd->pReal;
189526 assert( p->rc==SQLITE_OK );
189527 p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_SHARED);
189528 if( p->rc==SQLITE_OK ){
189529 p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_EXCLUSIVE);
189530 }
189531}
189532
189533#if defined(_WIN32_WCE)
189534static LPWSTR rbuWinUtf8ToUnicode(const char *zFilename){
189535 int nChar;
189536 LPWSTR zWideFilename;
189537
189538 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
189539 if( nChar==0 ){
189540 return 0;
189541 }
189542 zWideFilename = sqlite3_malloc64( nChar*sizeof(zWideFilename[0]) );
189543 if( zWideFilename==0 ){
189544 return 0;
189545 }
189546 memset(zWideFilename, 0, nChar*sizeof(zWideFilename[0]));
189547 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
189548 nChar);
189549 if( nChar==0 ){
189550 sqlite3_free(zWideFilename);
189551 zWideFilename = 0;
189552 }
189553 return zWideFilename;
189554}
189555#endif
189556
189557/*
189558** The RBU handle is currently in RBU_STAGE_OAL state, with a SHARED lock
189559** on the database file. This proc moves the *-oal file to the *-wal path,
189560** then reopens the database file (this time in vanilla, non-oal, WAL mode).
189561** If an error occurs, leave an error code and error message in the rbu
189562** handle.
189563*/
189564static void rbuMoveOalFile(sqlite3rbu *p){
189565 const char *zBase = sqlite3_db_filename(p->dbMain, "main");
189566 const char *zMove = zBase;
189567 char *zOal;
189568 char *zWal;
189569
189570 if( rbuIsVacuum(p) ){
189571 zMove = sqlite3_db_filename(p->dbRbu, "main");
189572 }
189573 zOal = sqlite3_mprintf("%s-oal", zMove);
189574 zWal = sqlite3_mprintf("%s-wal", zMove);
189575
189576 assert( p->eStage==RBU_STAGE_MOVE );
189577 assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
189578 if( zWal==0 || zOal==0 ){
189579 p->rc = SQLITE_NOMEM;
189580 }else{
189581 /* Move the *-oal file to *-wal. At this point connection p->db is
189582 ** holding a SHARED lock on the target database file (because it is
189583 ** in WAL mode). So no other connection may be writing the db.
189584 **
189585 ** In order to ensure that there are no database readers, an EXCLUSIVE
189586 ** lock is obtained here before the *-oal is moved to *-wal.
189587 */
189588 rbuLockDatabase(p);
189589 if( p->rc==SQLITE_OK ){
189590 rbuFileSuffix3(zBase, zWal);
189591 rbuFileSuffix3(zBase, zOal);
189592
189593 /* Re-open the databases. */
189594 rbuObjIterFinalize(&p->objiter);
189595 sqlite3_close(p->dbRbu);
189596 sqlite3_close(p->dbMain);
189597 p->dbMain = 0;
189598 p->dbRbu = 0;
189599
189600#if defined(_WIN32_WCE)
189601 {
189602 LPWSTR zWideOal;
189603 LPWSTR zWideWal;
189604
189605 zWideOal = rbuWinUtf8ToUnicode(zOal);
189606 if( zWideOal ){
189607 zWideWal = rbuWinUtf8ToUnicode(zWal);
189608 if( zWideWal ){
189609 if( MoveFileW(zWideOal, zWideWal) ){
189610 p->rc = SQLITE_OK;
189611 }else{
189612 p->rc = SQLITE_IOERR;
189613 }
189614 sqlite3_free(zWideWal);
189615 }else{
189616 p->rc = SQLITE_IOERR_NOMEM;
189617 }
189618 sqlite3_free(zWideOal);
189619 }else{
189620 p->rc = SQLITE_IOERR_NOMEM;
189621 }
189622 }
189623#else
189624 p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
189625#endif
189626
189627 if( p->rc==SQLITE_OK ){
189628 rbuOpenDatabase(p, 0);
189629 rbuSetupCheckpoint(p, 0);
189630 }
189631 }
189632 }
189633
189634 sqlite3_free(zWal);
189635 sqlite3_free(zOal);
189636}
189637
189638/*
189639** The SELECT statement iterating through the keys for the current object
189640** (p->objiter.pSelect) currently points to a valid row. This function
189641** determines the type of operation requested by this row and returns
189642** one of the following values to indicate the result:
189643**
189644** * RBU_INSERT
189645** * RBU_DELETE
189646** * RBU_IDX_DELETE
189647** * RBU_UPDATE
189648**
189649** If RBU_UPDATE is returned, then output variable *pzMask is set to
189650** point to the text value indicating the columns to update.
189651**
189652** If the rbu_control field contains an invalid value, an error code and
189653** message are left in the RBU handle and zero returned.
189654*/
189655static int rbuStepType(sqlite3rbu *p, const char **pzMask){
189656 int iCol = p->objiter.nCol; /* Index of rbu_control column */
189657 int res = 0; /* Return value */
189658
189659 switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
189660 case SQLITE_INTEGER: {
189661 int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
189662 switch( iVal ){
189663 case 0: res = RBU_INSERT; break;
189664 case 1: res = RBU_DELETE; break;
189665 case 2: res = RBU_REPLACE; break;
189666 case 3: res = RBU_IDX_DELETE; break;
189667 case 4: res = RBU_IDX_INSERT; break;
189668 }
189669 break;
189670 }
189671
189672 case SQLITE_TEXT: {
189673 const unsigned char *z = sqlite3_column_text(p->objiter.pSelect, iCol);
189674 if( z==0 ){
189675 p->rc = SQLITE_NOMEM;
189676 }else{
189677 *pzMask = (const char*)z;
189678 }
189679 res = RBU_UPDATE;
189680
189681 break;
189682 }
189683
189684 default:
189685 break;
189686 }
189687
189688 if( res==0 ){
189689 rbuBadControlError(p);
189690 }
189691 return res;
189692}
189693
189694#ifdef SQLITE_DEBUG
189695/*
189696** Assert that column iCol of statement pStmt is named zName.
189697*/
189698static void assertColumnName(sqlite3_stmt *pStmt, int iCol, const char *zName){
189699 const char *zCol = sqlite3_column_name(pStmt, iCol);
189700 assert( 0==sqlite3_stricmp(zName, zCol) );
189701}
189702#else
189703# define assertColumnName(x,y,z)
189704#endif
189705
189706/*
189707** Argument eType must be one of RBU_INSERT, RBU_DELETE, RBU_IDX_INSERT or
189708** RBU_IDX_DELETE. This function performs the work of a single
189709** sqlite3rbu_step() call for the type of operation specified by eType.
189710*/
189711static void rbuStepOneOp(sqlite3rbu *p, int eType){
189712 RbuObjIter *pIter = &p->objiter;
189713 sqlite3_value *pVal;
189714 sqlite3_stmt *pWriter;
189715 int i;
189716
189717 assert( p->rc==SQLITE_OK );
189718 assert( eType!=RBU_DELETE || pIter->zIdx==0 );
189719 assert( eType==RBU_DELETE || eType==RBU_IDX_DELETE
189720 || eType==RBU_INSERT || eType==RBU_IDX_INSERT
189721 );
189722
189723 /* If this is a delete, decrement nPhaseOneStep by nIndex. If the DELETE
189724 ** statement below does actually delete a row, nPhaseOneStep will be
189725 ** incremented by the same amount when SQL function rbu_tmp_insert()
189726 ** is invoked by the trigger. */
189727 if( eType==RBU_DELETE ){
189728 p->nPhaseOneStep -= p->objiter.nIndex;
189729 }
189730
189731 if( eType==RBU_IDX_DELETE || eType==RBU_DELETE ){
189732 pWriter = pIter->pDelete;
189733 }else{
189734 pWriter = pIter->pInsert;
189735 }
189736
189737 for(i=0; i<pIter->nCol; i++){
189738 /* If this is an INSERT into a table b-tree and the table has an
189739 ** explicit INTEGER PRIMARY KEY, check that this is not an attempt
189740 ** to write a NULL into the IPK column. That is not permitted. */
189741 if( eType==RBU_INSERT
189742 && pIter->zIdx==0 && pIter->eType==RBU_PK_IPK && pIter->abTblPk[i]
189743 && sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL
189744 ){
189745 p->rc = SQLITE_MISMATCH;
189746 p->zErrmsg = sqlite3_mprintf("datatype mismatch");
189747 return;
189748 }
189749
189750 if( eType==RBU_DELETE && pIter->abTblPk[i]==0 ){
189751 continue;
189752 }
189753
189754 pVal = sqlite3_column_value(pIter->pSelect, i);
189755 p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
189756 if( p->rc ) return;
189757 }
189758 if( pIter->zIdx==0 ){
189759 if( pIter->eType==RBU_PK_VTAB
189760 || pIter->eType==RBU_PK_NONE
189761 || (pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p))
189762 ){
189763 /* For a virtual table, or a table with no primary key, the
189764 ** SELECT statement is:
189765 **
189766 ** SELECT <cols>, rbu_control, rbu_rowid FROM ....
189767 **
189768 ** Hence column_value(pIter->nCol+1).
189769 */
189770 assertColumnName(pIter->pSelect, pIter->nCol+1,
189771 rbuIsVacuum(p) ? "rowid" : "rbu_rowid"
189772 );
189773 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
189774 p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
189775 }
189776 }
189777 if( p->rc==SQLITE_OK ){
189778 sqlite3_step(pWriter);
189779 p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
189780 }
189781}
189782
189783/*
189784** This function does the work for an sqlite3rbu_step() call.
189785**
189786** The object-iterator (p->objiter) currently points to a valid object,
189787** and the input cursor (p->objiter.pSelect) currently points to a valid
189788** input row. Perform whatever processing is required and return.
189789**
189790** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
189791** and message is left in the RBU handle and a copy of the error code
189792** returned.
189793*/
189794static int rbuStep(sqlite3rbu *p){
189795 RbuObjIter *pIter = &p->objiter;
189796 const char *zMask = 0;
189797 int eType = rbuStepType(p, &zMask);
189798
189799 if( eType ){
189800 assert( eType==RBU_INSERT || eType==RBU_DELETE
189801 || eType==RBU_REPLACE || eType==RBU_IDX_DELETE
189802 || eType==RBU_IDX_INSERT || eType==RBU_UPDATE
189803 );
189804 assert( eType!=RBU_UPDATE || pIter->zIdx==0 );
189805
189806 if( pIter->zIdx==0 && (eType==RBU_IDX_DELETE || eType==RBU_IDX_INSERT) ){
189807 rbuBadControlError(p);
189808 }
189809 else if( eType==RBU_REPLACE ){
189810 if( pIter->zIdx==0 ){
189811 p->nPhaseOneStep += p->objiter.nIndex;
189812 rbuStepOneOp(p, RBU_DELETE);
189813 }
189814 if( p->rc==SQLITE_OK ) rbuStepOneOp(p, RBU_INSERT);
189815 }
189816 else if( eType!=RBU_UPDATE ){
189817 rbuStepOneOp(p, eType);
189818 }
189819 else{
189820 sqlite3_value *pVal;
189821 sqlite3_stmt *pUpdate = 0;
189822 assert( eType==RBU_UPDATE );
189823 p->nPhaseOneStep -= p->objiter.nIndex;
189824 rbuGetUpdateStmt(p, pIter, zMask, &pUpdate);
189825 if( pUpdate ){
189826 int i;
189827 for(i=0; p->rc==SQLITE_OK && i<pIter->nCol; i++){
189828 char c = zMask[pIter->aiSrcOrder[i]];
189829 pVal = sqlite3_column_value(pIter->pSelect, i);
189830 if( pIter->abTblPk[i] || c!='.' ){
189831 p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
189832 }
189833 }
189834 if( p->rc==SQLITE_OK
189835 && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
189836 ){
189837 /* Bind the rbu_rowid value to column _rowid_ */
189838 assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
189839 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
189840 p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
189841 }
189842 if( p->rc==SQLITE_OK ){
189843 sqlite3_step(pUpdate);
189844 p->rc = resetAndCollectError(pUpdate, &p->zErrmsg);
189845 }
189846 }
189847 }
189848 }
189849 return p->rc;
189850}
189851
189852/*
189853** Increment the schema cookie of the main database opened by p->dbMain.
189854**
189855** Or, if this is an RBU vacuum, set the schema cookie of the main db
189856** opened by p->dbMain to one more than the schema cookie of the main
189857** db opened by p->dbRbu.
189858*/
189859static void rbuIncrSchemaCookie(sqlite3rbu *p){
189860 if( p->rc==SQLITE_OK ){
189861 sqlite3 *dbread = (rbuIsVacuum(p) ? p->dbRbu : p->dbMain);
189862 int iCookie = 1000000;
189863 sqlite3_stmt *pStmt;
189864
189865 p->rc = prepareAndCollectError(dbread, &pStmt, &p->zErrmsg,
189866 "PRAGMA schema_version"
189867 );
189868 if( p->rc==SQLITE_OK ){
189869 /* Coverage: it may be that this sqlite3_step() cannot fail. There
189870 ** is already a transaction open, so the prepared statement cannot
189871 ** throw an SQLITE_SCHEMA exception. The only database page the
189872 ** statement reads is page 1, which is guaranteed to be in the cache.
189873 ** And no memory allocations are required. */
189874 if( SQLITE_ROW==sqlite3_step(pStmt) ){
189875 iCookie = sqlite3_column_int(pStmt, 0);
189876 }
189877 rbuFinalize(p, pStmt);
189878 }
189879 if( p->rc==SQLITE_OK ){
189880 rbuMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
189881 }
189882 }
189883}
189884
189885/*
189886** Update the contents of the rbu_state table within the rbu database. The
189887** value stored in the RBU_STATE_STAGE column is eStage. All other values
189888** are determined by inspecting the rbu handle passed as the first argument.
189889*/
189890static void rbuSaveState(sqlite3rbu *p, int eStage){
189891 if( p->rc==SQLITE_OK || p->rc==SQLITE_DONE ){
189892 sqlite3_stmt *pInsert = 0;
189893 rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
189894 int rc;
189895
189896 assert( p->zErrmsg==0 );
189897 rc = prepareFreeAndCollectError(p->dbRbu, &pInsert, &p->zErrmsg,
189898 sqlite3_mprintf(
189899 "INSERT OR REPLACE INTO %s.rbu_state(k, v) VALUES "
189900 "(%d, %d), "
189901 "(%d, %Q), "
189902 "(%d, %Q), "
189903 "(%d, %d), "
189904 "(%d, %d), "
189905 "(%d, %lld), "
189906 "(%d, %lld), "
189907 "(%d, %lld), "
189908 "(%d, %lld), "
189909 "(%d, %Q) ",
189910 p->zStateDb,
189911 RBU_STATE_STAGE, eStage,
189912 RBU_STATE_TBL, p->objiter.zTbl,
189913 RBU_STATE_IDX, p->objiter.zIdx,
189914 RBU_STATE_ROW, p->nStep,
189915 RBU_STATE_PROGRESS, p->nProgress,
189916 RBU_STATE_CKPT, p->iWalCksum,
189917 RBU_STATE_COOKIE, (i64)pFd->iCookie,
189918 RBU_STATE_OALSZ, p->iOalSz,
189919 RBU_STATE_PHASEONESTEP, p->nPhaseOneStep,
189920 RBU_STATE_DATATBL, p->objiter.zDataTbl
189921 )
189922 );
189923 assert( pInsert==0 || rc==SQLITE_OK );
189924
189925 if( rc==SQLITE_OK ){
189926 sqlite3_step(pInsert);
189927 rc = sqlite3_finalize(pInsert);
189928 }
189929 if( rc!=SQLITE_OK ) p->rc = rc;
189930 }
189931}
189932
189933
189934/*
189935** The second argument passed to this function is the name of a PRAGMA
189936** setting - "page_size", "auto_vacuum", "user_version" or "application_id".
189937** This function executes the following on sqlite3rbu.dbRbu:
189938**
189939** "PRAGMA main.$zPragma"
189940**
189941** where $zPragma is the string passed as the second argument, then
189942** on sqlite3rbu.dbMain:
189943**
189944** "PRAGMA main.$zPragma = $val"
189945**
189946** where $val is the value returned by the first PRAGMA invocation.
189947**
189948** In short, it copies the value of the specified PRAGMA setting from
189949** dbRbu to dbMain.
189950*/
189951static void rbuCopyPragma(sqlite3rbu *p, const char *zPragma){
189952 if( p->rc==SQLITE_OK ){
189953 sqlite3_stmt *pPragma = 0;
189954 p->rc = prepareFreeAndCollectError(p->dbRbu, &pPragma, &p->zErrmsg,
189955 sqlite3_mprintf("PRAGMA main.%s", zPragma)
189956 );
189957 if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPragma) ){
189958 p->rc = rbuMPrintfExec(p, p->dbMain, "PRAGMA main.%s = %d",
189959 zPragma, sqlite3_column_int(pPragma, 0)
189960 );
189961 }
189962 rbuFinalize(p, pPragma);
189963 }
189964}
189965
189966/*
189967** The RBU handle passed as the only argument has just been opened and
189968** the state database is empty. If this RBU handle was opened for an
189969** RBU vacuum operation, create the schema in the target db.
189970*/
189971static void rbuCreateTargetSchema(sqlite3rbu *p){
189972 sqlite3_stmt *pSql = 0;
189973 sqlite3_stmt *pInsert = 0;
189974
189975 assert( rbuIsVacuum(p) );
189976 p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=1", 0,0, &p->zErrmsg);
189977 if( p->rc==SQLITE_OK ){
189978 p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
189979 "SELECT sql FROM sqlite_master WHERE sql!='' AND rootpage!=0"
189980 " AND name!='sqlite_sequence' "
189981 " ORDER BY type DESC"
189982 );
189983 }
189984
189985 while( p->rc==SQLITE_OK && sqlite3_step(pSql)==SQLITE_ROW ){
189986 const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
189987 p->rc = sqlite3_exec(p->dbMain, zSql, 0, 0, &p->zErrmsg);
189988 }
189989 rbuFinalize(p, pSql);
189990 if( p->rc!=SQLITE_OK ) return;
189991
189992 if( p->rc==SQLITE_OK ){
189993 p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
189994 "SELECT * FROM sqlite_master WHERE rootpage=0 OR rootpage IS NULL"
189995 );
189996 }
189997
189998 if( p->rc==SQLITE_OK ){
189999 p->rc = prepareAndCollectError(p->dbMain, &pInsert, &p->zErrmsg,
190000 "INSERT INTO sqlite_master VALUES(?,?,?,?,?)"
190001 );
190002 }
190003
190004 while( p->rc==SQLITE_OK && sqlite3_step(pSql)==SQLITE_ROW ){
190005 int i;
190006 for(i=0; i<5; i++){
190007 sqlite3_bind_value(pInsert, i+1, sqlite3_column_value(pSql, i));
190008 }
190009 sqlite3_step(pInsert);
190010 p->rc = sqlite3_reset(pInsert);
190011 }
190012 if( p->rc==SQLITE_OK ){
190013 p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=0",0,0,&p->zErrmsg);
190014 }
190015
190016 rbuFinalize(p, pSql);
190017 rbuFinalize(p, pInsert);
190018}
190019
190020/*
190021** Step the RBU object.
190022*/
190023SQLITE_API int sqlite3rbu_step(sqlite3rbu *p){
190024 if( p ){
190025 switch( p->eStage ){
190026 case RBU_STAGE_OAL: {
190027 RbuObjIter *pIter = &p->objiter;
190028
190029 /* If this is an RBU vacuum operation and the state table was empty
190030 ** when this handle was opened, create the target database schema. */
190031 if( rbuIsVacuum(p) && p->nProgress==0 && p->rc==SQLITE_OK ){
190032 rbuCreateTargetSchema(p);
190033 rbuCopyPragma(p, "user_version");
190034 rbuCopyPragma(p, "application_id");
190035 }
190036
190037 while( p->rc==SQLITE_OK && pIter->zTbl ){
190038
190039 if( pIter->bCleanup ){
190040 /* Clean up the rbu_tmp_xxx table for the previous table. It
190041 ** cannot be dropped as there are currently active SQL statements.
190042 ** But the contents can be deleted. */
190043 if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
190044 rbuMPrintfExec(p, p->dbRbu,
190045 "DELETE FROM %s.'rbu_tmp_%q'", p->zStateDb, pIter->zDataTbl
190046 );
190047 }
190048 }else{
190049 rbuObjIterPrepareAll(p, pIter, 0);
190050
190051 /* Advance to the next row to process. */
190052 if( p->rc==SQLITE_OK ){
190053 int rc = sqlite3_step(pIter->pSelect);
190054 if( rc==SQLITE_ROW ){
190055 p->nProgress++;
190056 p->nStep++;
190057 return rbuStep(p);
190058 }
190059 p->rc = sqlite3_reset(pIter->pSelect);
190060 p->nStep = 0;
190061 }
190062 }
190063
190064 rbuObjIterNext(p, pIter);
190065 }
190066
190067 if( p->rc==SQLITE_OK ){
190068 assert( pIter->zTbl==0 );
190069 rbuSaveState(p, RBU_STAGE_MOVE);
190070 rbuIncrSchemaCookie(p);
190071 if( p->rc==SQLITE_OK ){
190072 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
190073 }
190074 if( p->rc==SQLITE_OK ){
190075 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
190076 }
190077 p->eStage = RBU_STAGE_MOVE;
190078 }
190079 break;
190080 }
190081
190082 case RBU_STAGE_MOVE: {
190083 if( p->rc==SQLITE_OK ){
190084 rbuMoveOalFile(p);
190085 p->nProgress++;
190086 }
190087 break;
190088 }
190089
190090 case RBU_STAGE_CKPT: {
190091 if( p->rc==SQLITE_OK ){
190092 if( p->nStep>=p->nFrame ){
190093 sqlite3_file *pDb = p->pTargetFd->pReal;
190094
190095 /* Sync the db file */
190096 p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
190097
190098 /* Update nBackfill */
190099 if( p->rc==SQLITE_OK ){
190100 void volatile *ptr;
190101 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, &ptr);
190102 if( p->rc==SQLITE_OK ){
190103 ((u32 volatile*)ptr)[24] = p->iMaxFrame;
190104 }
190105 }
190106
190107 if( p->rc==SQLITE_OK ){
190108 p->eStage = RBU_STAGE_DONE;
190109 p->rc = SQLITE_DONE;
190110 }
190111 }else{
190112 /* At one point the following block copied a single frame from the
190113 ** wal file to the database file. So that one call to sqlite3rbu_step()
190114 ** checkpointed a single frame.
190115 **
190116 ** However, if the sector-size is larger than the page-size, and the
190117 ** application calls sqlite3rbu_savestate() or close() immediately
190118 ** after this step, then rbu_step() again, then a power failure occurs,
190119 ** then the database page written here may be damaged. Work around
190120 ** this by checkpointing frames until the next page in the aFrame[]
190121 ** lies on a different disk sector to the current one. */
190122 u32 iSector;
190123 do{
190124 RbuFrame *pFrame = &p->aFrame[p->nStep];
190125 iSector = (pFrame->iDbPage-1) / p->nPagePerSector;
190126 rbuCheckpointFrame(p, pFrame);
190127 p->nStep++;
190128 }while( p->nStep<p->nFrame
190129 && iSector==((p->aFrame[p->nStep].iDbPage-1) / p->nPagePerSector)
190130 && p->rc==SQLITE_OK
190131 );
190132 }
190133 p->nProgress++;
190134 }
190135 break;
190136 }
190137
190138 default:
190139 break;
190140 }
190141 return p->rc;
190142 }else{
190143 return SQLITE_NOMEM;
190144 }
190145}
190146
190147/*
190148** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
190149** otherwise. Either or both argument may be NULL. Two NULL values are
190150** considered equal, and NULL is considered distinct from all other values.
190151*/
190152static int rbuStrCompare(const char *z1, const char *z2){
190153 if( z1==0 && z2==0 ) return 0;
190154 if( z1==0 || z2==0 ) return 1;
190155 return (sqlite3_stricmp(z1, z2)!=0);
190156}
190157
190158/*
190159** This function is called as part of sqlite3rbu_open() when initializing
190160** an rbu handle in OAL stage. If the rbu update has not started (i.e.
190161** the rbu_state table was empty) it is a no-op. Otherwise, it arranges
190162** things so that the next call to sqlite3rbu_step() continues on from
190163** where the previous rbu handle left off.
190164**
190165** If an error occurs, an error code and error message are left in the
190166** rbu handle passed as the first argument.
190167*/
190168static void rbuSetupOal(sqlite3rbu *p, RbuState *pState){
190169 assert( p->rc==SQLITE_OK );
190170 if( pState->zTbl ){
190171 RbuObjIter *pIter = &p->objiter;
190172 int rc = SQLITE_OK;
190173
190174 while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup
190175 || rbuStrCompare(pIter->zIdx, pState->zIdx)
190176 || (pState->zDataTbl==0 && rbuStrCompare(pIter->zTbl, pState->zTbl))
190177 || (pState->zDataTbl && rbuStrCompare(pIter->zDataTbl, pState->zDataTbl))
190178 )){
190179 rc = rbuObjIterNext(p, pIter);
190180 }
190181
190182 if( rc==SQLITE_OK && !pIter->zTbl ){
190183 rc = SQLITE_ERROR;
190184 p->zErrmsg = sqlite3_mprintf("rbu_state mismatch error");
190185 }
190186
190187 if( rc==SQLITE_OK ){
190188 p->nStep = pState->nRow;
190189 rc = rbuObjIterPrepareAll(p, &p->objiter, p->nStep);
190190 }
190191
190192 p->rc = rc;
190193 }
190194}
190195
190196/*
190197** If there is a "*-oal" file in the file-system corresponding to the
190198** target database in the file-system, delete it. If an error occurs,
190199** leave an error code and error message in the rbu handle.
190200*/
190201static void rbuDeleteOalFile(sqlite3rbu *p){
190202 char *zOal = rbuMPrintf(p, "%s-oal", p->zTarget);
190203 if( zOal ){
190204 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
190205 assert( pVfs && p->rc==SQLITE_OK && p->zErrmsg==0 );
190206 pVfs->xDelete(pVfs, zOal, 0);
190207 sqlite3_free(zOal);
190208 }
190209}
190210
190211/*
190212** Allocate a private rbu VFS for the rbu handle passed as the only
190213** argument. This VFS will be used unless the call to sqlite3rbu_open()
190214** specified a URI with a vfs=? option in place of a target database
190215** file name.
190216*/
190217static void rbuCreateVfs(sqlite3rbu *p){
190218 int rnd;
190219 char zRnd[64];
190220
190221 assert( p->rc==SQLITE_OK );
190222 sqlite3_randomness(sizeof(int), (void*)&rnd);
190223 sqlite3_snprintf(sizeof(zRnd), zRnd, "rbu_vfs_%d", rnd);
190224 p->rc = sqlite3rbu_create_vfs(zRnd, 0);
190225 if( p->rc==SQLITE_OK ){
190226 sqlite3_vfs *pVfs = sqlite3_vfs_find(zRnd);
190227 assert( pVfs );
190228 p->zVfsName = pVfs->zName;
190229 ((rbu_vfs*)pVfs)->pRbu = p;
190230 }
190231}
190232
190233/*
190234** Destroy the private VFS created for the rbu handle passed as the only
190235** argument by an earlier call to rbuCreateVfs().
190236*/
190237static void rbuDeleteVfs(sqlite3rbu *p){
190238 if( p->zVfsName ){
190239 sqlite3rbu_destroy_vfs(p->zVfsName);
190240 p->zVfsName = 0;
190241 }
190242}
190243
190244/*
190245** This user-defined SQL function is invoked with a single argument - the
190246** name of a table expected to appear in the target database. It returns
190247** the number of auxilliary indexes on the table.
190248*/
190249static void rbuIndexCntFunc(
190250 sqlite3_context *pCtx,
190251 int nVal,
190252 sqlite3_value **apVal
190253){
190254 sqlite3rbu *p = (sqlite3rbu*)sqlite3_user_data(pCtx);
190255 sqlite3_stmt *pStmt = 0;
190256 char *zErrmsg = 0;
190257 int rc;
190258
190259 assert( nVal==1 );
190260
190261 rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &zErrmsg,
190262 sqlite3_mprintf("SELECT count(*) FROM sqlite_master "
190263 "WHERE type='index' AND tbl_name = %Q", sqlite3_value_text(apVal[0]))
190264 );
190265 if( rc!=SQLITE_OK ){
190266 sqlite3_result_error(pCtx, zErrmsg, -1);
190267 }else{
190268 int nIndex = 0;
190269 if( SQLITE_ROW==sqlite3_step(pStmt) ){
190270 nIndex = sqlite3_column_int(pStmt, 0);
190271 }
190272 rc = sqlite3_finalize(pStmt);
190273 if( rc==SQLITE_OK ){
190274 sqlite3_result_int(pCtx, nIndex);
190275 }else{
190276 sqlite3_result_error(pCtx, sqlite3_errmsg(p->dbMain), -1);
190277 }
190278 }
190279
190280 sqlite3_free(zErrmsg);
190281}
190282
190283/*
190284** If the RBU database contains the rbu_count table, use it to initialize
190285** the sqlite3rbu.nPhaseOneStep variable. The schema of the rbu_count table
190286** is assumed to contain the same columns as:
190287**
190288** CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
190289**
190290** There should be one row in the table for each data_xxx table in the
190291** database. The 'tbl' column should contain the name of a data_xxx table,
190292** and the cnt column the number of rows it contains.
190293**
190294** sqlite3rbu.nPhaseOneStep is initialized to the sum of (1 + nIndex) * cnt
190295** for all rows in the rbu_count table, where nIndex is the number of
190296** indexes on the corresponding target database table.
190297*/
190298static void rbuInitPhaseOneSteps(sqlite3rbu *p){
190299 if( p->rc==SQLITE_OK ){
190300 sqlite3_stmt *pStmt = 0;
190301 int bExists = 0; /* True if rbu_count exists */
190302
190303 p->nPhaseOneStep = -1;
190304
190305 p->rc = sqlite3_create_function(p->dbRbu,
190306 "rbu_index_cnt", 1, SQLITE_UTF8, (void*)p, rbuIndexCntFunc, 0, 0
190307 );
190308
190309 /* Check for the rbu_count table. If it does not exist, or if an error
190310 ** occurs, nPhaseOneStep will be left set to -1. */
190311 if( p->rc==SQLITE_OK ){
190312 p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
190313 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'rbu_count'"
190314 );
190315 }
190316 if( p->rc==SQLITE_OK ){
190317 if( SQLITE_ROW==sqlite3_step(pStmt) ){
190318 bExists = 1;
190319 }
190320 p->rc = sqlite3_finalize(pStmt);
190321 }
190322
190323 if( p->rc==SQLITE_OK && bExists ){
190324 p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
190325 "SELECT sum(cnt * (1 + rbu_index_cnt(rbu_target_name(tbl))))"
190326 "FROM rbu_count"
190327 );
190328 if( p->rc==SQLITE_OK ){
190329 if( SQLITE_ROW==sqlite3_step(pStmt) ){
190330 p->nPhaseOneStep = sqlite3_column_int64(pStmt, 0);
190331 }
190332 p->rc = sqlite3_finalize(pStmt);
190333 }
190334 }
190335 }
190336}
190337
190338
190339static sqlite3rbu *openRbuHandle(
190340 const char *zTarget,
190341 const char *zRbu,
190342 const char *zState
190343){
190344 sqlite3rbu *p;
190345 size_t nTarget = zTarget ? strlen(zTarget) : 0;
190346 size_t nRbu = strlen(zRbu);
190347 size_t nByte = sizeof(sqlite3rbu) + nTarget+1 + nRbu+1;
190348
190349 p = (sqlite3rbu*)sqlite3_malloc64(nByte);
190350 if( p ){
190351 RbuState *pState = 0;
190352
190353 /* Create the custom VFS. */
190354 memset(p, 0, sizeof(sqlite3rbu));
190355 rbuCreateVfs(p);
190356
190357 /* Open the target, RBU and state databases */
190358 if( p->rc==SQLITE_OK ){
190359 char *pCsr = (char*)&p[1];
190360 int bRetry = 0;
190361 if( zTarget ){
190362 p->zTarget = pCsr;
190363 memcpy(p->zTarget, zTarget, nTarget+1);
190364 pCsr += nTarget+1;
190365 }
190366 p->zRbu = pCsr;
190367 memcpy(p->zRbu, zRbu, nRbu+1);
190368 pCsr += nRbu+1;
190369 if( zState ){
190370 p->zState = rbuMPrintf(p, "%s", zState);
190371 }
190372
190373 /* If the first attempt to open the database file fails and the bRetry
190374 ** flag it set, this means that the db was not opened because it seemed
190375 ** to be a wal-mode db. But, this may have happened due to an earlier
190376 ** RBU vacuum operation leaving an old wal file in the directory.
190377 ** If this is the case, it will have been checkpointed and deleted
190378 ** when the handle was closed and a second attempt to open the
190379 ** database may succeed. */
190380 rbuOpenDatabase(p, &bRetry);
190381 if( bRetry ){
190382 rbuOpenDatabase(p, 0);
190383 }
190384 }
190385
190386 if( p->rc==SQLITE_OK ){
190387 pState = rbuLoadState(p);
190388 assert( pState || p->rc!=SQLITE_OK );
190389 if( p->rc==SQLITE_OK ){
190390
190391 if( pState->eStage==0 ){
190392 rbuDeleteOalFile(p);
190393 rbuInitPhaseOneSteps(p);
190394 p->eStage = RBU_STAGE_OAL;
190395 }else{
190396 p->eStage = pState->eStage;
190397 p->nPhaseOneStep = pState->nPhaseOneStep;
190398 }
190399 p->nProgress = pState->nProgress;
190400 p->iOalSz = pState->iOalSz;
190401 }
190402 }
190403 assert( p->rc!=SQLITE_OK || p->eStage!=0 );
190404
190405 if( p->rc==SQLITE_OK && p->pTargetFd->pWalFd ){
190406 if( p->eStage==RBU_STAGE_OAL ){
190407 p->rc = SQLITE_ERROR;
190408 p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
190409 }else if( p->eStage==RBU_STAGE_MOVE ){
190410 p->eStage = RBU_STAGE_CKPT;
190411 p->nStep = 0;
190412 }
190413 }
190414
190415 if( p->rc==SQLITE_OK
190416 && (p->eStage==RBU_STAGE_OAL || p->eStage==RBU_STAGE_MOVE)
190417 && pState->eStage!=0
190418 ){
190419 rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
190420 if( pFd->iCookie!=pState->iCookie ){
190421 /* At this point (pTargetFd->iCookie) contains the value of the
190422 ** change-counter cookie (the thing that gets incremented when a
190423 ** transaction is committed in rollback mode) currently stored on
190424 ** page 1 of the database file. */
190425 p->rc = SQLITE_BUSY;
190426 p->zErrmsg = sqlite3_mprintf("database modified during rbu %s",
190427 (rbuIsVacuum(p) ? "vacuum" : "update")
190428 );
190429 }
190430 }
190431
190432 if( p->rc==SQLITE_OK ){
190433 if( p->eStage==RBU_STAGE_OAL ){
190434 sqlite3 *db = p->dbMain;
190435 p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, &p->zErrmsg);
190436
190437 /* Point the object iterator at the first object */
190438 if( p->rc==SQLITE_OK ){
190439 p->rc = rbuObjIterFirst(p, &p->objiter);
190440 }
190441
190442 /* If the RBU database contains no data_xxx tables, declare the RBU
190443 ** update finished. */
190444 if( p->rc==SQLITE_OK && p->objiter.zTbl==0 ){
190445 p->rc = SQLITE_DONE;
190446 p->eStage = RBU_STAGE_DONE;
190447 }else{
190448 if( p->rc==SQLITE_OK && pState->eStage==0 && rbuIsVacuum(p) ){
190449 rbuCopyPragma(p, "page_size");
190450 rbuCopyPragma(p, "auto_vacuum");
190451 }
190452
190453 /* Open transactions both databases. The *-oal file is opened or
190454 ** created at this point. */
190455 if( p->rc==SQLITE_OK ){
190456 p->rc = sqlite3_exec(db, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
190457 }
190458
190459 /* Check if the main database is a zipvfs db. If it is, set the upper
190460 ** level pager to use "journal_mode=off". This prevents it from
190461 ** generating a large journal using a temp file. */
190462 if( p->rc==SQLITE_OK ){
190463 int frc = sqlite3_file_control(db, "main", SQLITE_FCNTL_ZIPVFS, 0);
190464 if( frc==SQLITE_OK ){
190465 p->rc = sqlite3_exec(
190466 db, "PRAGMA journal_mode=off",0,0,&p->zErrmsg);
190467 }
190468 }
190469
190470 if( p->rc==SQLITE_OK ){
190471 rbuSetupOal(p, pState);
190472 }
190473 }
190474 }else if( p->eStage==RBU_STAGE_MOVE ){
190475 /* no-op */
190476 }else if( p->eStage==RBU_STAGE_CKPT ){
190477 rbuSetupCheckpoint(p, pState);
190478 }else if( p->eStage==RBU_STAGE_DONE ){
190479 p->rc = SQLITE_DONE;
190480 }else{
190481 p->rc = SQLITE_CORRUPT;
190482 }
190483 }
190484
190485 rbuFreeState(pState);
190486 }
190487
190488 return p;
190489}
190490
190491/*
190492** Allocate and return an RBU handle with all fields zeroed except for the
190493** error code, which is set to SQLITE_MISUSE.
190494*/
190495static sqlite3rbu *rbuMisuseError(void){
190496 sqlite3rbu *pRet;
190497 pRet = sqlite3_malloc64(sizeof(sqlite3rbu));
190498 if( pRet ){
190499 memset(pRet, 0, sizeof(sqlite3rbu));
190500 pRet->rc = SQLITE_MISUSE;
190501 }
190502 return pRet;
190503}
190504
190505/*
190506** Open and return a new RBU handle.
190507*/
190508SQLITE_API sqlite3rbu *sqlite3rbu_open(
190509 const char *zTarget,
190510 const char *zRbu,
190511 const char *zState
190512){
190513 if( zTarget==0 || zRbu==0 ){ return rbuMisuseError(); }
190514 /* TODO: Check that zTarget and zRbu are non-NULL */
190515 return openRbuHandle(zTarget, zRbu, zState);
190516}
190517
190518/*
190519** Open a handle to begin or resume an RBU VACUUM operation.
190520*/
190521SQLITE_API sqlite3rbu *sqlite3rbu_vacuum(
190522 const char *zTarget,
190523 const char *zState
190524){
190525 if( zTarget==0 ){ return rbuMisuseError(); }
190526 /* TODO: Check that both arguments are non-NULL */
190527 return openRbuHandle(0, zTarget, zState);
190528}
190529
190530/*
190531** Return the database handle used by pRbu.
190532*/
190533SQLITE_API sqlite3 *sqlite3rbu_db(sqlite3rbu *pRbu, int bRbu){
190534 sqlite3 *db = 0;
190535 if( pRbu ){
190536 db = (bRbu ? pRbu->dbRbu : pRbu->dbMain);
190537 }
190538 return db;
190539}
190540
190541
190542/*
190543** If the error code currently stored in the RBU handle is SQLITE_CONSTRAINT,
190544** then edit any error message string so as to remove all occurrences of
190545** the pattern "rbu_imp_[0-9]*".
190546*/
190547static void rbuEditErrmsg(sqlite3rbu *p){
190548 if( p->rc==SQLITE_CONSTRAINT && p->zErrmsg ){
190549 unsigned int i;
190550 size_t nErrmsg = strlen(p->zErrmsg);
190551 for(i=0; i<(nErrmsg-8); i++){
190552 if( memcmp(&p->zErrmsg[i], "rbu_imp_", 8)==0 ){
190553 int nDel = 8;
190554 while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
190555 memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
190556 nErrmsg -= nDel;
190557 }
190558 }
190559 }
190560}
190561
190562/*
190563** Close the RBU handle.
190564*/
190565SQLITE_API int sqlite3rbu_close(sqlite3rbu *p, char **pzErrmsg){
190566 int rc;
190567 if( p ){
190568
190569 /* Commit the transaction to the *-oal file. */
190570 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
190571 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
190572 }
190573
190574 /* Sync the db file if currently doing an incremental checkpoint */
190575 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
190576 sqlite3_file *pDb = p->pTargetFd->pReal;
190577 p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
190578 }
190579
190580 rbuSaveState(p, p->eStage);
190581
190582 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
190583 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
190584 }
190585
190586 /* Close any open statement handles. */
190587 rbuObjIterFinalize(&p->objiter);
190588
190589 /* If this is an RBU vacuum handle and the vacuum has either finished
190590 ** successfully or encountered an error, delete the contents of the
190591 ** state table. This causes the next call to sqlite3rbu_vacuum()
190592 ** specifying the current target and state databases to start a new
190593 ** vacuum from scratch. */
190594 if( rbuIsVacuum(p) && p->rc!=SQLITE_OK && p->dbRbu ){
190595 int rc2 = sqlite3_exec(p->dbRbu, "DELETE FROM stat.rbu_state", 0, 0, 0);
190596 if( p->rc==SQLITE_DONE && rc2!=SQLITE_OK ) p->rc = rc2;
190597 }
190598
190599 /* Close the open database handle and VFS object. */
190600 sqlite3_close(p->dbRbu);
190601 sqlite3_close(p->dbMain);
190602 assert( p->szTemp==0 );
190603 rbuDeleteVfs(p);
190604 sqlite3_free(p->aBuf);
190605 sqlite3_free(p->aFrame);
190606
190607 rbuEditErrmsg(p);
190608 rc = p->rc;
190609 if( pzErrmsg ){
190610 *pzErrmsg = p->zErrmsg;
190611 }else{
190612 sqlite3_free(p->zErrmsg);
190613 }
190614 sqlite3_free(p->zState);
190615 sqlite3_free(p);
190616 }else{
190617 rc = SQLITE_NOMEM;
190618 *pzErrmsg = 0;
190619 }
190620 return rc;
190621}
190622
190623/*
190624** Return the total number of key-value operations (inserts, deletes or
190625** updates) that have been performed on the target database since the
190626** current RBU update was started.
190627*/
190628SQLITE_API sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu){
190629 return pRbu->nProgress;
190630}
190631
190632/*
190633** Return permyriadage progress indications for the two main stages of
190634** an RBU update.
190635*/
190636SQLITE_API void sqlite3rbu_bp_progress(sqlite3rbu *p, int *pnOne, int *pnTwo){
190637 const int MAX_PROGRESS = 10000;
190638 switch( p->eStage ){
190639 case RBU_STAGE_OAL:
190640 if( p->nPhaseOneStep>0 ){
190641 *pnOne = (int)(MAX_PROGRESS * (i64)p->nProgress/(i64)p->nPhaseOneStep);
190642 }else{
190643 *pnOne = -1;
190644 }
190645 *pnTwo = 0;
190646 break;
190647
190648 case RBU_STAGE_MOVE:
190649 *pnOne = MAX_PROGRESS;
190650 *pnTwo = 0;
190651 break;
190652
190653 case RBU_STAGE_CKPT:
190654 *pnOne = MAX_PROGRESS;
190655 *pnTwo = (int)(MAX_PROGRESS * (i64)p->nStep / (i64)p->nFrame);
190656 break;
190657
190658 case RBU_STAGE_DONE:
190659 *pnOne = MAX_PROGRESS;
190660 *pnTwo = MAX_PROGRESS;
190661 break;
190662
190663 default:
190664 assert( 0 );
190665 }
190666}
190667
190668/*
190669** Return the current state of the RBU vacuum or update operation.
190670*/
190671SQLITE_API int sqlite3rbu_state(sqlite3rbu *p){
190672 int aRes[] = {
190673 0, SQLITE_RBU_STATE_OAL, SQLITE_RBU_STATE_MOVE,
190674 0, SQLITE_RBU_STATE_CHECKPOINT, SQLITE_RBU_STATE_DONE
190675 };
190676
190677 assert( RBU_STAGE_OAL==1 );
190678 assert( RBU_STAGE_MOVE==2 );
190679 assert( RBU_STAGE_CKPT==4 );
190680 assert( RBU_STAGE_DONE==5 );
190681 assert( aRes[RBU_STAGE_OAL]==SQLITE_RBU_STATE_OAL );
190682 assert( aRes[RBU_STAGE_MOVE]==SQLITE_RBU_STATE_MOVE );
190683 assert( aRes[RBU_STAGE_CKPT]==SQLITE_RBU_STATE_CHECKPOINT );
190684 assert( aRes[RBU_STAGE_DONE]==SQLITE_RBU_STATE_DONE );
190685
190686 if( p->rc!=SQLITE_OK && p->rc!=SQLITE_DONE ){
190687 return SQLITE_RBU_STATE_ERROR;
190688 }else{
190689 assert( p->rc!=SQLITE_DONE || p->eStage==RBU_STAGE_DONE );
190690 assert( p->eStage==RBU_STAGE_OAL
190691 || p->eStage==RBU_STAGE_MOVE
190692 || p->eStage==RBU_STAGE_CKPT
190693 || p->eStage==RBU_STAGE_DONE
190694 );
190695 return aRes[p->eStage];
190696 }
190697}
190698
190699SQLITE_API int sqlite3rbu_savestate(sqlite3rbu *p){
190700 int rc = p->rc;
190701 if( rc==SQLITE_DONE ) return SQLITE_OK;
190702
190703 assert( p->eStage>=RBU_STAGE_OAL && p->eStage<=RBU_STAGE_DONE );
190704 if( p->eStage==RBU_STAGE_OAL ){
190705 assert( rc!=SQLITE_DONE );
190706 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, 0);
190707 }
190708
190709 /* Sync the db file */
190710 if( rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
190711 sqlite3_file *pDb = p->pTargetFd->pReal;
190712 rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
190713 }
190714
190715 p->rc = rc;
190716 rbuSaveState(p, p->eStage);
190717 rc = p->rc;
190718
190719 if( p->eStage==RBU_STAGE_OAL ){
190720 assert( rc!=SQLITE_DONE );
190721 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
190722 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbRbu, "BEGIN IMMEDIATE", 0, 0, 0);
190723 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0,0);
190724 }
190725
190726 p->rc = rc;
190727 return rc;
190728}
190729
190730/**************************************************************************
190731** Beginning of RBU VFS shim methods. The VFS shim modifies the behaviour
190732** of a standard VFS in the following ways:
190733**
190734** 1. Whenever the first page of a main database file is read or
190735** written, the value of the change-counter cookie is stored in
190736** rbu_file.iCookie. Similarly, the value of the "write-version"
190737** database header field is stored in rbu_file.iWriteVer. This ensures
190738** that the values are always trustworthy within an open transaction.
190739**
190740** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (rbu_file.pWalFd)
190741** member variable of the associated database file descriptor is set
190742** to point to the new file. A mutex protected linked list of all main
190743** db fds opened using a particular RBU VFS is maintained at
190744** rbu_vfs.pMain to facilitate this.
190745**
190746** 3. Using a new file-control "SQLITE_FCNTL_RBU", a main db rbu_file
190747** object can be marked as the target database of an RBU update. This
190748** turns on the following extra special behaviour:
190749**
190750** 3a. If xAccess() is called to check if there exists a *-wal file
190751** associated with an RBU target database currently in RBU_STAGE_OAL
190752** stage (preparing the *-oal file), the following special handling
190753** applies:
190754**
190755** * if the *-wal file does exist, return SQLITE_CANTOPEN. An RBU
190756** target database may not be in wal mode already.
190757**
190758** * if the *-wal file does not exist, set the output parameter to
190759** non-zero (to tell SQLite that it does exist) anyway.
190760**
190761** Then, when xOpen() is called to open the *-wal file associated with
190762** the RBU target in RBU_STAGE_OAL stage, instead of opening the *-wal
190763** file, the rbu vfs opens the corresponding *-oal file instead.
190764**
190765** 3b. The *-shm pages returned by xShmMap() for a target db file in
190766** RBU_STAGE_OAL mode are actually stored in heap memory. This is to
190767** avoid creating a *-shm file on disk. Additionally, xShmLock() calls
190768** are no-ops on target database files in RBU_STAGE_OAL mode. This is
190769** because assert() statements in some VFS implementations fail if
190770** xShmLock() is called before xShmMap().
190771**
190772** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
190773** mode except RBU_STAGE_DONE (all work completed and checkpointed), it
190774** fails with an SQLITE_BUSY error. This is to stop RBU connections
190775** from automatically checkpointing a *-wal (or *-oal) file from within
190776** sqlite3_close().
190777**
190778** 3d. In RBU_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
190779** all xWrite() calls on the target database file perform no IO.
190780** Instead the frame and page numbers that would be read and written
190781** are recorded. Additionally, successful attempts to obtain exclusive
190782** xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target
190783** database file are recorded. xShmLock() calls to unlock the same
190784** locks are no-ops (so that once obtained, these locks are never
190785** relinquished). Finally, calls to xSync() on the target database
190786** file fail with SQLITE_INTERNAL errors.
190787*/
190788
190789static void rbuUnlockShm(rbu_file *p){
190790 assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
190791 if( p->pRbu ){
190792 int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
190793 int i;
190794 for(i=0; i<SQLITE_SHM_NLOCK;i++){
190795 if( (1<<i) & p->pRbu->mLock ){
190796 xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK|SQLITE_SHM_EXCLUSIVE);
190797 }
190798 }
190799 p->pRbu->mLock = 0;
190800 }
190801}
190802
190803/*
190804*/
190805static int rbuUpdateTempSize(rbu_file *pFd, sqlite3_int64 nNew){
190806 sqlite3rbu *pRbu = pFd->pRbu;
190807 i64 nDiff = nNew - pFd->sz;
190808 pRbu->szTemp += nDiff;
190809 pFd->sz = nNew;
190810 assert( pRbu->szTemp>=0 );
190811 if( pRbu->szTempLimit && pRbu->szTemp>pRbu->szTempLimit ) return SQLITE_FULL;
190812 return SQLITE_OK;
190813}
190814
190815/*
190816** Add an item to the main-db lists, if it is not already present.
190817**
190818** There are two main-db lists. One for all file descriptors, and one
190819** for all file descriptors with rbu_file.pDb!=0. If the argument has
190820** rbu_file.pDb!=0, then it is assumed to already be present on the
190821** main list and is only added to the pDb!=0 list.
190822*/
190823static void rbuMainlistAdd(rbu_file *p){
190824 rbu_vfs *pRbuVfs = p->pRbuVfs;
190825 rbu_file *pIter;
190826 assert( (p->openFlags & SQLITE_OPEN_MAIN_DB) );
190827 sqlite3_mutex_enter(pRbuVfs->mutex);
190828 if( p->pRbu==0 ){
190829 for(pIter=pRbuVfs->pMain; pIter; pIter=pIter->pMainNext);
190830 p->pMainNext = pRbuVfs->pMain;
190831 pRbuVfs->pMain = p;
190832 }else{
190833 for(pIter=pRbuVfs->pMainRbu; pIter && pIter!=p; pIter=pIter->pMainRbuNext){}
190834 if( pIter==0 ){
190835 p->pMainRbuNext = pRbuVfs->pMainRbu;
190836 pRbuVfs->pMainRbu = p;
190837 }
190838 }
190839 sqlite3_mutex_leave(pRbuVfs->mutex);
190840}
190841
190842/*
190843** Remove an item from the main-db lists.
190844*/
190845static void rbuMainlistRemove(rbu_file *p){
190846 rbu_file **pp;
190847 sqlite3_mutex_enter(p->pRbuVfs->mutex);
190848 for(pp=&p->pRbuVfs->pMain; *pp && *pp!=p; pp=&((*pp)->pMainNext)){}
190849 if( *pp ) *pp = p->pMainNext;
190850 p->pMainNext = 0;
190851 for(pp=&p->pRbuVfs->pMainRbu; *pp && *pp!=p; pp=&((*pp)->pMainRbuNext)){}
190852 if( *pp ) *pp = p->pMainRbuNext;
190853 p->pMainRbuNext = 0;
190854 sqlite3_mutex_leave(p->pRbuVfs->mutex);
190855}
190856
190857/*
190858** Given that zWal points to a buffer containing a wal file name passed to
190859** either the xOpen() or xAccess() VFS method, search the main-db list for
190860** a file-handle opened by the same database connection on the corresponding
190861** database file.
190862**
190863** If parameter bRbu is true, only search for file-descriptors with
190864** rbu_file.pDb!=0.
190865*/
190866static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal, int bRbu){
190867 rbu_file *pDb;
190868 sqlite3_mutex_enter(pRbuVfs->mutex);
190869 if( bRbu ){
190870 for(pDb=pRbuVfs->pMainRbu; pDb && pDb->zWal!=zWal; pDb=pDb->pMainRbuNext){}
190871 }else{
190872 for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext){}
190873 }
190874 sqlite3_mutex_leave(pRbuVfs->mutex);
190875 return pDb;
190876}
190877
190878/*
190879** Close an rbu file.
190880*/
190881static int rbuVfsClose(sqlite3_file *pFile){
190882 rbu_file *p = (rbu_file*)pFile;
190883 int rc;
190884 int i;
190885
190886 /* Free the contents of the apShm[] array. And the array itself. */
190887 for(i=0; i<p->nShm; i++){
190888 sqlite3_free(p->apShm[i]);
190889 }
190890 sqlite3_free(p->apShm);
190891 p->apShm = 0;
190892 sqlite3_free(p->zDel);
190893
190894 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
190895 rbuMainlistRemove(p);
190896 rbuUnlockShm(p);
190897 p->pReal->pMethods->xShmUnmap(p->pReal, 0);
190898 }
190899 else if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE) && p->pRbu ){
190900 rbuUpdateTempSize(p, 0);
190901 }
190902 assert( p->pMainNext==0 && p->pRbuVfs->pMain!=p );
190903
190904 /* Close the underlying file handle */
190905 rc = p->pReal->pMethods->xClose(p->pReal);
190906 return rc;
190907}
190908
190909
190910/*
190911** Read and return an unsigned 32-bit big-endian integer from the buffer
190912** passed as the only argument.
190913*/
190914static u32 rbuGetU32(u8 *aBuf){
190915 return ((u32)aBuf[0] << 24)
190916 + ((u32)aBuf[1] << 16)
190917 + ((u32)aBuf[2] << 8)
190918 + ((u32)aBuf[3]);
190919}
190920
190921/*
190922** Write an unsigned 32-bit value in big-endian format to the supplied
190923** buffer.
190924*/
190925static void rbuPutU32(u8 *aBuf, u32 iVal){
190926 aBuf[0] = (iVal >> 24) & 0xFF;
190927 aBuf[1] = (iVal >> 16) & 0xFF;
190928 aBuf[2] = (iVal >> 8) & 0xFF;
190929 aBuf[3] = (iVal >> 0) & 0xFF;
190930}
190931
190932static void rbuPutU16(u8 *aBuf, u16 iVal){
190933 aBuf[0] = (iVal >> 8) & 0xFF;
190934 aBuf[1] = (iVal >> 0) & 0xFF;
190935}
190936
190937/*
190938** Read data from an rbuVfs-file.
190939*/
190940static int rbuVfsRead(
190941 sqlite3_file *pFile,
190942 void *zBuf,
190943 int iAmt,
190944 sqlite_int64 iOfst
190945){
190946 rbu_file *p = (rbu_file*)pFile;
190947 sqlite3rbu *pRbu = p->pRbu;
190948 int rc;
190949
190950 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
190951 assert( p->openFlags & SQLITE_OPEN_WAL );
190952 rc = rbuCaptureWalRead(p->pRbu, iOfst, iAmt);
190953 }else{
190954 if( pRbu && pRbu->eStage==RBU_STAGE_OAL
190955 && (p->openFlags & SQLITE_OPEN_WAL)
190956 && iOfst>=pRbu->iOalSz
190957 ){
190958 rc = SQLITE_OK;
190959 memset(zBuf, 0, iAmt);
190960 }else{
190961 rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
190962#if 1
190963 /* If this is being called to read the first page of the target
190964 ** database as part of an rbu vacuum operation, synthesize the
190965 ** contents of the first page if it does not yet exist. Otherwise,
190966 ** SQLite will not check for a *-wal file. */
190967 if( pRbu && rbuIsVacuum(pRbu)
190968 && rc==SQLITE_IOERR_SHORT_READ && iOfst==0
190969 && (p->openFlags & SQLITE_OPEN_MAIN_DB)
190970 && pRbu->rc==SQLITE_OK
190971 ){
190972 sqlite3_file *pFd = (sqlite3_file*)pRbu->pRbuFd;
190973 rc = pFd->pMethods->xRead(pFd, zBuf, iAmt, iOfst);
190974 if( rc==SQLITE_OK ){
190975 u8 *aBuf = (u8*)zBuf;
190976 u32 iRoot = rbuGetU32(&aBuf[52]) ? 1 : 0;
190977 rbuPutU32(&aBuf[52], iRoot); /* largest root page number */
190978 rbuPutU32(&aBuf[36], 0); /* number of free pages */
190979 rbuPutU32(&aBuf[32], 0); /* first page on free list trunk */
190980 rbuPutU32(&aBuf[28], 1); /* size of db file in pages */
190981 rbuPutU32(&aBuf[24], pRbu->pRbuFd->iCookie+1); /* Change counter */
190982
190983 if( iAmt>100 ){
190984 memset(&aBuf[100], 0, iAmt-100);
190985 rbuPutU16(&aBuf[105], iAmt & 0xFFFF);
190986 aBuf[100] = 0x0D;
190987 }
190988 }
190989 }
190990#endif
190991 }
190992 if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
190993 /* These look like magic numbers. But they are stable, as they are part
190994 ** of the definition of the SQLite file format, which may not change. */
190995 u8 *pBuf = (u8*)zBuf;
190996 p->iCookie = rbuGetU32(&pBuf[24]);
190997 p->iWriteVer = pBuf[19];
190998 }
190999 }
191000 return rc;
191001}
191002
191003/*
191004** Write data to an rbuVfs-file.
191005*/
191006static int rbuVfsWrite(
191007 sqlite3_file *pFile,
191008 const void *zBuf,
191009 int iAmt,
191010 sqlite_int64 iOfst
191011){
191012 rbu_file *p = (rbu_file*)pFile;
191013 sqlite3rbu *pRbu = p->pRbu;
191014 int rc;
191015
191016 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
191017 assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
191018 rc = rbuCaptureDbWrite(p->pRbu, iOfst);
191019 }else{
191020 if( pRbu ){
191021 if( pRbu->eStage==RBU_STAGE_OAL
191022 && (p->openFlags & SQLITE_OPEN_WAL)
191023 && iOfst>=pRbu->iOalSz
191024 ){
191025 pRbu->iOalSz = iAmt + iOfst;
191026 }else if( p->openFlags & SQLITE_OPEN_DELETEONCLOSE ){
191027 i64 szNew = iAmt+iOfst;
191028 if( szNew>p->sz ){
191029 rc = rbuUpdateTempSize(p, szNew);
191030 if( rc!=SQLITE_OK ) return rc;
191031 }
191032 }
191033 }
191034 rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
191035 if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
191036 /* These look like magic numbers. But they are stable, as they are part
191037 ** of the definition of the SQLite file format, which may not change. */
191038 u8 *pBuf = (u8*)zBuf;
191039 p->iCookie = rbuGetU32(&pBuf[24]);
191040 p->iWriteVer = pBuf[19];
191041 }
191042 }
191043 return rc;
191044}
191045
191046/*
191047** Truncate an rbuVfs-file.
191048*/
191049static int rbuVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
191050 rbu_file *p = (rbu_file*)pFile;
191051 if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE) && p->pRbu ){
191052 int rc = rbuUpdateTempSize(p, size);
191053 if( rc!=SQLITE_OK ) return rc;
191054 }
191055 return p->pReal->pMethods->xTruncate(p->pReal, size);
191056}
191057
191058/*
191059** Sync an rbuVfs-file.
191060*/
191061static int rbuVfsSync(sqlite3_file *pFile, int flags){
191062 rbu_file *p = (rbu_file *)pFile;
191063 if( p->pRbu && p->pRbu->eStage==RBU_STAGE_CAPTURE ){
191064 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
191065 return SQLITE_INTERNAL;
191066 }
191067 return SQLITE_OK;
191068 }
191069 return p->pReal->pMethods->xSync(p->pReal, flags);
191070}
191071
191072/*
191073** Return the current file-size of an rbuVfs-file.
191074*/
191075static int rbuVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
191076 rbu_file *p = (rbu_file *)pFile;
191077 int rc;
191078 rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
191079
191080 /* If this is an RBU vacuum operation and this is the target database,
191081 ** pretend that it has at least one page. Otherwise, SQLite will not
191082 ** check for the existance of a *-wal file. rbuVfsRead() contains
191083 ** similar logic. */
191084 if( rc==SQLITE_OK && *pSize==0
191085 && p->pRbu && rbuIsVacuum(p->pRbu)
191086 && (p->openFlags & SQLITE_OPEN_MAIN_DB)
191087 ){
191088 *pSize = 1024;
191089 }
191090 return rc;
191091}
191092
191093/*
191094** Lock an rbuVfs-file.
191095*/
191096static int rbuVfsLock(sqlite3_file *pFile, int eLock){
191097 rbu_file *p = (rbu_file*)pFile;
191098 sqlite3rbu *pRbu = p->pRbu;
191099 int rc = SQLITE_OK;
191100
191101 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
191102 if( eLock==SQLITE_LOCK_EXCLUSIVE
191103 && (p->bNolock || (pRbu && pRbu->eStage!=RBU_STAGE_DONE))
191104 ){
191105 /* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this
191106 ** prevents it from checkpointing the database from sqlite3_close(). */
191107 rc = SQLITE_BUSY;
191108 }else{
191109 rc = p->pReal->pMethods->xLock(p->pReal, eLock);
191110 }
191111
191112 return rc;
191113}
191114
191115/*
191116** Unlock an rbuVfs-file.
191117*/
191118static int rbuVfsUnlock(sqlite3_file *pFile, int eLock){
191119 rbu_file *p = (rbu_file *)pFile;
191120 return p->pReal->pMethods->xUnlock(p->pReal, eLock);
191121}
191122
191123/*
191124** Check if another file-handle holds a RESERVED lock on an rbuVfs-file.
191125*/
191126static int rbuVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
191127 rbu_file *p = (rbu_file *)pFile;
191128 return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
191129}
191130
191131/*
191132** File control method. For custom operations on an rbuVfs-file.
191133*/
191134static int rbuVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
191135 rbu_file *p = (rbu_file *)pFile;
191136 int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
191137 int rc;
191138
191139 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB)
191140 || p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL)
191141 );
191142 if( op==SQLITE_FCNTL_RBU ){
191143 sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
191144
191145 /* First try to find another RBU vfs lower down in the vfs stack. If
191146 ** one is found, this vfs will operate in pass-through mode. The lower
191147 ** level vfs will do the special RBU handling. */
191148 rc = xControl(p->pReal, op, pArg);
191149
191150 if( rc==SQLITE_NOTFOUND ){
191151 /* Now search for a zipvfs instance lower down in the VFS stack. If
191152 ** one is found, this is an error. */
191153 void *dummy = 0;
191154 rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS, &dummy);
191155 if( rc==SQLITE_OK ){
191156 rc = SQLITE_ERROR;
191157 pRbu->zErrmsg = sqlite3_mprintf("rbu/zipvfs setup error");
191158 }else if( rc==SQLITE_NOTFOUND ){
191159 pRbu->pTargetFd = p;
191160 p->pRbu = pRbu;
191161 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
191162 rbuMainlistAdd(p);
191163 }
191164 if( p->pWalFd ) p->pWalFd->pRbu = pRbu;
191165 rc = SQLITE_OK;
191166 }
191167 }
191168 return rc;
191169 }
191170 else if( op==SQLITE_FCNTL_RBUCNT ){
191171 sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
191172 pRbu->nRbu++;
191173 pRbu->pRbuFd = p;
191174 p->bNolock = 1;
191175 }
191176
191177 rc = xControl(p->pReal, op, pArg);
191178 if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
191179 rbu_vfs *pRbuVfs = p->pRbuVfs;
191180 char *zIn = *(char**)pArg;
191181 char *zOut = sqlite3_mprintf("rbu(%s)/%z", pRbuVfs->base.zName, zIn);
191182 *(char**)pArg = zOut;
191183 if( zOut==0 ) rc = SQLITE_NOMEM;
191184 }
191185
191186 return rc;
191187}
191188
191189/*
191190** Return the sector-size in bytes for an rbuVfs-file.
191191*/
191192static int rbuVfsSectorSize(sqlite3_file *pFile){
191193 rbu_file *p = (rbu_file *)pFile;
191194 return p->pReal->pMethods->xSectorSize(p->pReal);
191195}
191196
191197/*
191198** Return the device characteristic flags supported by an rbuVfs-file.
191199*/
191200static int rbuVfsDeviceCharacteristics(sqlite3_file *pFile){
191201 rbu_file *p = (rbu_file *)pFile;
191202 return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
191203}
191204
191205/*
191206** Take or release a shared-memory lock.
191207*/
191208static int rbuVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
191209 rbu_file *p = (rbu_file*)pFile;
191210 sqlite3rbu *pRbu = p->pRbu;
191211 int rc = SQLITE_OK;
191212
191213#ifdef SQLITE_AMALGAMATION
191214 assert( WAL_CKPT_LOCK==1 );
191215#endif
191216
191217 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
191218 if( pRbu && (pRbu->eStage==RBU_STAGE_OAL || pRbu->eStage==RBU_STAGE_MOVE) ){
191219 /* Magic number 1 is the WAL_CKPT_LOCK lock. Preventing SQLite from
191220 ** taking this lock also prevents any checkpoints from occurring.
191221 ** todo: really, it's not clear why this might occur, as
191222 ** wal_autocheckpoint ought to be turned off. */
191223 if( ofst==WAL_LOCK_CKPT && n==1 ) rc = SQLITE_BUSY;
191224 }else{
191225 int bCapture = 0;
191226 if( n==1 && (flags & SQLITE_SHM_EXCLUSIVE)
191227 && pRbu && pRbu->eStage==RBU_STAGE_CAPTURE
191228 && (ofst==WAL_LOCK_WRITE || ofst==WAL_LOCK_CKPT || ofst==WAL_LOCK_READ0)
191229 ){
191230 bCapture = 1;
191231 }
191232
191233 if( bCapture==0 || 0==(flags & SQLITE_SHM_UNLOCK) ){
191234 rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
191235 if( bCapture && rc==SQLITE_OK ){
191236 pRbu->mLock |= (1 << ofst);
191237 }
191238 }
191239 }
191240
191241 return rc;
191242}
191243
191244/*
191245** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
191246*/
191247static int rbuVfsShmMap(
191248 sqlite3_file *pFile,
191249 int iRegion,
191250 int szRegion,
191251 int isWrite,
191252 void volatile **pp
191253){
191254 rbu_file *p = (rbu_file*)pFile;
191255 int rc = SQLITE_OK;
191256 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
191257
191258 /* If not in RBU_STAGE_OAL, allow this call to pass through. Or, if this
191259 ** rbu is in the RBU_STAGE_OAL state, use heap memory for *-shm space
191260 ** instead of a file on disk. */
191261 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
191262 if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
191263 if( iRegion<=p->nShm ){
191264 int nByte = (iRegion+1) * sizeof(char*);
191265 char **apNew = (char**)sqlite3_realloc64(p->apShm, nByte);
191266 if( apNew==0 ){
191267 rc = SQLITE_NOMEM;
191268 }else{
191269 memset(&apNew[p->nShm], 0, sizeof(char*) * (1 + iRegion - p->nShm));
191270 p->apShm = apNew;
191271 p->nShm = iRegion+1;
191272 }
191273 }
191274
191275 if( rc==SQLITE_OK && p->apShm[iRegion]==0 ){
191276 char *pNew = (char*)sqlite3_malloc64(szRegion);
191277 if( pNew==0 ){
191278 rc = SQLITE_NOMEM;
191279 }else{
191280 memset(pNew, 0, szRegion);
191281 p->apShm[iRegion] = pNew;
191282 }
191283 }
191284
191285 if( rc==SQLITE_OK ){
191286 *pp = p->apShm[iRegion];
191287 }else{
191288 *pp = 0;
191289 }
191290 }else{
191291 assert( p->apShm==0 );
191292 rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
191293 }
191294
191295 return rc;
191296}
191297
191298/*
191299** Memory barrier.
191300*/
191301static void rbuVfsShmBarrier(sqlite3_file *pFile){
191302 rbu_file *p = (rbu_file *)pFile;
191303 p->pReal->pMethods->xShmBarrier(p->pReal);
191304}
191305
191306/*
191307** The xShmUnmap method.
191308*/
191309static int rbuVfsShmUnmap(sqlite3_file *pFile, int delFlag){
191310 rbu_file *p = (rbu_file*)pFile;
191311 int rc = SQLITE_OK;
191312 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
191313
191314 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
191315 if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
191316 /* no-op */
191317 }else{
191318 /* Release the checkpointer and writer locks */
191319 rbuUnlockShm(p);
191320 rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
191321 }
191322 return rc;
191323}
191324
191325/*
191326** A main database named zName has just been opened. The following
191327** function returns a pointer to a buffer owned by SQLite that contains
191328** the name of the *-wal file this db connection will use. SQLite
191329** happens to pass a pointer to this buffer when using xAccess()
191330** or xOpen() to operate on the *-wal file.
191331*/
191332static const char *rbuMainToWal(const char *zName, int flags){
191333 int n = (int)strlen(zName);
191334 const char *z = &zName[n];
191335 if( flags & SQLITE_OPEN_URI ){
191336 int odd = 0;
191337 while( 1 ){
191338 if( z[0]==0 ){
191339 odd = 1 - odd;
191340 if( odd && z[1]==0 ) break;
191341 }
191342 z++;
191343 }
191344 z += 2;
191345 }else{
191346 while( *z==0 ) z++;
191347 }
191348 z += (n + 8 + 1);
191349 return z;
191350}
191351
191352/*
191353** Open an rbu file handle.
191354*/
191355static int rbuVfsOpen(
191356 sqlite3_vfs *pVfs,
191357 const char *zName,
191358 sqlite3_file *pFile,
191359 int flags,
191360 int *pOutFlags
191361){
191362 static sqlite3_io_methods rbuvfs_io_methods = {
191363 2, /* iVersion */
191364 rbuVfsClose, /* xClose */
191365 rbuVfsRead, /* xRead */
191366 rbuVfsWrite, /* xWrite */
191367 rbuVfsTruncate, /* xTruncate */
191368 rbuVfsSync, /* xSync */
191369 rbuVfsFileSize, /* xFileSize */
191370 rbuVfsLock, /* xLock */
191371 rbuVfsUnlock, /* xUnlock */
191372 rbuVfsCheckReservedLock, /* xCheckReservedLock */
191373 rbuVfsFileControl, /* xFileControl */
191374 rbuVfsSectorSize, /* xSectorSize */
191375 rbuVfsDeviceCharacteristics, /* xDeviceCharacteristics */
191376 rbuVfsShmMap, /* xShmMap */
191377 rbuVfsShmLock, /* xShmLock */
191378 rbuVfsShmBarrier, /* xShmBarrier */
191379 rbuVfsShmUnmap, /* xShmUnmap */
191380 0, 0 /* xFetch, xUnfetch */
191381 };
191382 rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
191383 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
191384 rbu_file *pFd = (rbu_file *)pFile;
191385 int rc = SQLITE_OK;
191386 const char *zOpen = zName;
191387 int oflags = flags;
191388
191389 memset(pFd, 0, sizeof(rbu_file));
191390 pFd->pReal = (sqlite3_file*)&pFd[1];
191391 pFd->pRbuVfs = pRbuVfs;
191392 pFd->openFlags = flags;
191393 if( zName ){
191394 if( flags & SQLITE_OPEN_MAIN_DB ){
191395 /* A main database has just been opened. The following block sets
191396 ** (pFd->zWal) to point to a buffer owned by SQLite that contains
191397 ** the name of the *-wal file this db connection will use. SQLite
191398 ** happens to pass a pointer to this buffer when using xAccess()
191399 ** or xOpen() to operate on the *-wal file. */
191400 pFd->zWal = rbuMainToWal(zName, flags);
191401 }
191402 else if( flags & SQLITE_OPEN_WAL ){
191403 rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName, 0);
191404 if( pDb ){
191405 if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
191406 /* This call is to open a *-wal file. Intead, open the *-oal. This
191407 ** code ensures that the string passed to xOpen() is terminated by a
191408 ** pair of '\0' bytes in case the VFS attempts to extract a URI
191409 ** parameter from it. */
191410 const char *zBase = zName;
191411 size_t nCopy;
191412 char *zCopy;
191413 if( rbuIsVacuum(pDb->pRbu) ){
191414 zBase = sqlite3_db_filename(pDb->pRbu->dbRbu, "main");
191415 zBase = rbuMainToWal(zBase, SQLITE_OPEN_URI);
191416 }
191417 nCopy = strlen(zBase);
191418 zCopy = sqlite3_malloc64(nCopy+2);
191419 if( zCopy ){
191420 memcpy(zCopy, zBase, nCopy);
191421 zCopy[nCopy-3] = 'o';
191422 zCopy[nCopy] = '\0';
191423 zCopy[nCopy+1] = '\0';
191424 zOpen = (const char*)(pFd->zDel = zCopy);
191425 }else{
191426 rc = SQLITE_NOMEM;
191427 }
191428 pFd->pRbu = pDb->pRbu;
191429 }
191430 pDb->pWalFd = pFd;
191431 }
191432 }
191433 }else{
191434 pFd->pRbu = pRbuVfs->pRbu;
191435 }
191436
191437 if( oflags & SQLITE_OPEN_MAIN_DB
191438 && sqlite3_uri_boolean(zName, "rbu_memory", 0)
191439 ){
191440 assert( oflags & SQLITE_OPEN_MAIN_DB );
191441 oflags = SQLITE_OPEN_TEMP_DB | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
191442 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
191443 zOpen = 0;
191444 }
191445
191446 if( rc==SQLITE_OK ){
191447 rc = pRealVfs->xOpen(pRealVfs, zOpen, pFd->pReal, oflags, pOutFlags);
191448 }
191449 if( pFd->pReal->pMethods ){
191450 /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
191451 ** pointer and, if the file is a main database file, link it into the
191452 ** mutex protected linked list of all such files. */
191453 pFile->pMethods = &rbuvfs_io_methods;
191454 if( flags & SQLITE_OPEN_MAIN_DB ){
191455 rbuMainlistAdd(pFd);
191456 }
191457 }else{
191458 sqlite3_free(pFd->zDel);
191459 }
191460
191461 return rc;
191462}
191463
191464/*
191465** Delete the file located at zPath.
191466*/
191467static int rbuVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
191468 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
191469 return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
191470}
191471
191472/*
191473** Test for access permissions. Return true if the requested permission
191474** is available, or false otherwise.
191475*/
191476static int rbuVfsAccess(
191477 sqlite3_vfs *pVfs,
191478 const char *zPath,
191479 int flags,
191480 int *pResOut
191481){
191482 rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
191483 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
191484 int rc;
191485
191486 rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
191487
191488 /* If this call is to check if a *-wal file associated with an RBU target
191489 ** database connection exists, and the RBU update is in RBU_STAGE_OAL,
191490 ** the following special handling is activated:
191491 **
191492 ** a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
191493 ** ensures that the RBU extension never tries to update a database
191494 ** in wal mode, even if the first page of the database file has
191495 ** been damaged.
191496 **
191497 ** b) if the *-wal file does not exist, claim that it does anyway,
191498 ** causing SQLite to call xOpen() to open it. This call will also
191499 ** be intercepted (see the rbuVfsOpen() function) and the *-oal
191500 ** file opened instead.
191501 */
191502 if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
191503 rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath, 1);
191504 if( pDb && pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
191505 if( *pResOut ){
191506 rc = SQLITE_CANTOPEN;
191507 }else{
191508 sqlite3_int64 sz = 0;
191509 rc = rbuVfsFileSize(&pDb->base, &sz);
191510 *pResOut = (sz>0);
191511 }
191512 }
191513 }
191514
191515 return rc;
191516}
191517
191518/*
191519** Populate buffer zOut with the full canonical pathname corresponding
191520** to the pathname in zPath. zOut is guaranteed to point to a buffer
191521** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
191522*/
191523static int rbuVfsFullPathname(
191524 sqlite3_vfs *pVfs,
191525 const char *zPath,
191526 int nOut,
191527 char *zOut
191528){
191529 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
191530 return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
191531}
191532
191533#ifndef SQLITE_OMIT_LOAD_EXTENSION
191534/*
191535** Open the dynamic library located at zPath and return a handle.
191536*/
191537static void *rbuVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
191538 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
191539 return pRealVfs->xDlOpen(pRealVfs, zPath);
191540}
191541
191542/*
191543** Populate the buffer zErrMsg (size nByte bytes) with a human readable
191544** utf-8 string describing the most recent error encountered associated
191545** with dynamic libraries.
191546*/
191547static void rbuVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
191548 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
191549 pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
191550}
191551
191552/*
191553** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
191554*/
191555static void (*rbuVfsDlSym(
191556 sqlite3_vfs *pVfs,
191557 void *pArg,
191558 const char *zSym
191559))(void){
191560 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
191561 return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
191562}
191563
191564/*
191565** Close the dynamic library handle pHandle.
191566*/
191567static void rbuVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
191568 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
191569 pRealVfs->xDlClose(pRealVfs, pHandle);
191570}
191571#endif /* SQLITE_OMIT_LOAD_EXTENSION */
191572
191573/*
191574** Populate the buffer pointed to by zBufOut with nByte bytes of
191575** random data.
191576*/
191577static int rbuVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
191578 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
191579 return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
191580}
191581
191582/*
191583** Sleep for nMicro microseconds. Return the number of microseconds
191584** actually slept.
191585*/
191586static int rbuVfsSleep(sqlite3_vfs *pVfs, int nMicro){
191587 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
191588 return pRealVfs->xSleep(pRealVfs, nMicro);
191589}
191590
191591/*
191592** Return the current time as a Julian Day number in *pTimeOut.
191593*/
191594static int rbuVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
191595 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
191596 return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
191597}
191598
191599/*
191600** No-op.
191601*/
191602static int rbuVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
191603 return 0;
191604}
191605
191606/*
191607** Deregister and destroy an RBU vfs created by an earlier call to
191608** sqlite3rbu_create_vfs().
191609*/
191610SQLITE_API void sqlite3rbu_destroy_vfs(const char *zName){
191611 sqlite3_vfs *pVfs = sqlite3_vfs_find(zName);
191612 if( pVfs && pVfs->xOpen==rbuVfsOpen ){
191613 sqlite3_mutex_free(((rbu_vfs*)pVfs)->mutex);
191614 sqlite3_vfs_unregister(pVfs);
191615 sqlite3_free(pVfs);
191616 }
191617}
191618
191619/*
191620** Create an RBU VFS named zName that accesses the underlying file-system
191621** via existing VFS zParent. The new object is registered as a non-default
191622** VFS with SQLite before returning.
191623*/
191624SQLITE_API int sqlite3rbu_create_vfs(const char *zName, const char *zParent){
191625
191626 /* Template for VFS */
191627 static sqlite3_vfs vfs_template = {
191628 1, /* iVersion */
191629 0, /* szOsFile */
191630 0, /* mxPathname */
191631 0, /* pNext */
191632 0, /* zName */
191633 0, /* pAppData */
191634 rbuVfsOpen, /* xOpen */
191635 rbuVfsDelete, /* xDelete */
191636 rbuVfsAccess, /* xAccess */
191637 rbuVfsFullPathname, /* xFullPathname */
191638
191639#ifndef SQLITE_OMIT_LOAD_EXTENSION
191640 rbuVfsDlOpen, /* xDlOpen */
191641 rbuVfsDlError, /* xDlError */
191642 rbuVfsDlSym, /* xDlSym */
191643 rbuVfsDlClose, /* xDlClose */
191644#else
191645 0, 0, 0, 0,
191646#endif
191647
191648 rbuVfsRandomness, /* xRandomness */
191649 rbuVfsSleep, /* xSleep */
191650 rbuVfsCurrentTime, /* xCurrentTime */
191651 rbuVfsGetLastError, /* xGetLastError */
191652 0, /* xCurrentTimeInt64 (version 2) */
191653 0, 0, 0 /* Unimplemented version 3 methods */
191654 };
191655
191656 rbu_vfs *pNew = 0; /* Newly allocated VFS */
191657 int rc = SQLITE_OK;
191658 size_t nName;
191659 size_t nByte;
191660
191661 nName = strlen(zName);
191662 nByte = sizeof(rbu_vfs) + nName + 1;
191663 pNew = (rbu_vfs*)sqlite3_malloc64(nByte);
191664 if( pNew==0 ){
191665 rc = SQLITE_NOMEM;
191666 }else{
191667 sqlite3_vfs *pParent; /* Parent VFS */
191668 memset(pNew, 0, nByte);
191669 pParent = sqlite3_vfs_find(zParent);
191670 if( pParent==0 ){
191671 rc = SQLITE_NOTFOUND;
191672 }else{
191673 char *zSpace;
191674 memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
191675 pNew->base.mxPathname = pParent->mxPathname;
191676 pNew->base.szOsFile = sizeof(rbu_file) + pParent->szOsFile;
191677 pNew->pRealVfs = pParent;
191678 pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
191679 memcpy(zSpace, zName, nName);
191680
191681 /* Allocate the mutex and register the new VFS (not as the default) */
191682 pNew->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
191683 if( pNew->mutex==0 ){
191684 rc = SQLITE_NOMEM;
191685 }else{
191686 rc = sqlite3_vfs_register(&pNew->base, 0);
191687 }
191688 }
191689
191690 if( rc!=SQLITE_OK ){
191691 sqlite3_mutex_free(pNew->mutex);
191692 sqlite3_free(pNew);
191693 }
191694 }
191695
191696 return rc;
191697}
191698
191699/*
191700** Configure the aggregate temp file size limit for this RBU handle.
191701*/
191702SQLITE_API sqlite3_int64 sqlite3rbu_temp_size_limit(sqlite3rbu *pRbu, sqlite3_int64 n){
191703 if( n>=0 ){
191704 pRbu->szTempLimit = n;
191705 }
191706 return pRbu->szTempLimit;
191707}
191708
191709SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu *pRbu){
191710 return pRbu->szTemp;
191711}
191712
191713
191714/**************************************************************************/
191715
191716#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU) */
191717
191718/************** End of sqlite3rbu.c ******************************************/
191719/************** Begin file dbstat.c ******************************************/
191720/*
191721** 2010 July 12
191722**
191723** The author disclaims copyright to this source code. In place of
191724** a legal notice, here is a blessing:
191725**
191726** May you do good and not evil.
191727** May you find forgiveness for yourself and forgive others.
191728** May you share freely, never taking more than you give.
191729**
191730******************************************************************************
191731**
191732** This file contains an implementation of the "dbstat" virtual table.
191733**
191734** The dbstat virtual table is used to extract low-level formatting
191735** information from an SQLite database in order to implement the
191736** "sqlite3_analyzer" utility. See the ../tool/spaceanal.tcl script
191737** for an example implementation.
191738**
191739** Additional information is available on the "dbstat.html" page of the
191740** official SQLite documentation.
191741*/
191742
191743/* #include "sqliteInt.h" ** Requires access to internal data structures ** */
191744#if (defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)) \
191745 && !defined(SQLITE_OMIT_VIRTUALTABLE)
191746
191747/*
191748** Page paths:
191749**
191750** The value of the 'path' column describes the path taken from the
191751** root-node of the b-tree structure to each page. The value of the
191752** root-node path is '/'.
191753**
191754** The value of the path for the left-most child page of the root of
191755** a b-tree is '/000/'. (Btrees store content ordered from left to right
191756** so the pages to the left have smaller keys than the pages to the right.)
191757** The next to left-most child of the root page is
191758** '/001', and so on, each sibling page identified by a 3-digit hex
191759** value. The children of the 451st left-most sibling have paths such
191760** as '/1c2/000/, '/1c2/001/' etc.
191761**
191762** Overflow pages are specified by appending a '+' character and a
191763** six-digit hexadecimal value to the path to the cell they are linked
191764** from. For example, the three overflow pages in a chain linked from
191765** the left-most cell of the 450th child of the root page are identified
191766** by the paths:
191767**
191768** '/1c2/000+000000' // First page in overflow chain
191769** '/1c2/000+000001' // Second page in overflow chain
191770** '/1c2/000+000002' // Third page in overflow chain
191771**
191772** If the paths are sorted using the BINARY collation sequence, then
191773** the overflow pages associated with a cell will appear earlier in the
191774** sort-order than its child page:
191775**
191776** '/1c2/000/' // Left-most child of 451st child of root
191777*/
191778#define VTAB_SCHEMA \
191779 "CREATE TABLE xx( " \
191780 " name TEXT, /* Name of table or index */" \
191781 " path TEXT, /* Path to page from root */" \
191782 " pageno INTEGER, /* Page number */" \
191783 " pagetype TEXT, /* 'internal', 'leaf' or 'overflow' */" \
191784 " ncell INTEGER, /* Cells on page (0 for overflow) */" \
191785 " payload INTEGER, /* Bytes of payload on this page */" \
191786 " unused INTEGER, /* Bytes of unused space on this page */" \
191787 " mx_payload INTEGER, /* Largest payload size of all cells */" \
191788 " pgoffset INTEGER, /* Offset of page in file */" \
191789 " pgsize INTEGER, /* Size of the page */" \
191790 " schema TEXT HIDDEN /* Database schema being analyzed */" \
191791 ");"
191792
191793
191794typedef struct StatTable StatTable;
191795typedef struct StatCursor StatCursor;
191796typedef struct StatPage StatPage;
191797typedef struct StatCell StatCell;
191798
191799struct StatCell {
191800 int nLocal; /* Bytes of local payload */
191801 u32 iChildPg; /* Child node (or 0 if this is a leaf) */
191802 int nOvfl; /* Entries in aOvfl[] */
191803 u32 *aOvfl; /* Array of overflow page numbers */
191804 int nLastOvfl; /* Bytes of payload on final overflow page */
191805 int iOvfl; /* Iterates through aOvfl[] */
191806};
191807
191808struct StatPage {
191809 u32 iPgno;
191810 DbPage *pPg;
191811 int iCell;
191812
191813 char *zPath; /* Path to this page */
191814
191815 /* Variables populated by statDecodePage(): */
191816 u8 flags; /* Copy of flags byte */
191817 int nCell; /* Number of cells on page */
191818 int nUnused; /* Number of unused bytes on page */
191819 StatCell *aCell; /* Array of parsed cells */
191820 u32 iRightChildPg; /* Right-child page number (or 0) */
191821 int nMxPayload; /* Largest payload of any cell on this page */
191822};
191823
191824struct StatCursor {
191825 sqlite3_vtab_cursor base;
191826 sqlite3_stmt *pStmt; /* Iterates through set of root pages */
191827 int isEof; /* After pStmt has returned SQLITE_DONE */
191828 int iDb; /* Schema used for this query */
191829
191830 StatPage aPage[32];
191831 int iPage; /* Current entry in aPage[] */
191832
191833 /* Values to return. */
191834 char *zName; /* Value of 'name' column */
191835 char *zPath; /* Value of 'path' column */
191836 u32 iPageno; /* Value of 'pageno' column */
191837 char *zPagetype; /* Value of 'pagetype' column */
191838 int nCell; /* Value of 'ncell' column */
191839 int nPayload; /* Value of 'payload' column */
191840 int nUnused; /* Value of 'unused' column */
191841 int nMxPayload; /* Value of 'mx_payload' column */
191842 i64 iOffset; /* Value of 'pgOffset' column */
191843 int szPage; /* Value of 'pgSize' column */
191844};
191845
191846struct StatTable {
191847 sqlite3_vtab base;
191848 sqlite3 *db;
191849 int iDb; /* Index of database to analyze */
191850};
191851
191852#ifndef get2byte
191853# define get2byte(x) ((x)[0]<<8 | (x)[1])
191854#endif
191855
191856/*
191857** Connect to or create a statvfs virtual table.
191858*/
191859static int statConnect(
191860 sqlite3 *db,
191861 void *pAux,
191862 int argc, const char *const*argv,
191863 sqlite3_vtab **ppVtab,
191864 char **pzErr
191865){
191866 StatTable *pTab = 0;
191867 int rc = SQLITE_OK;
191868 int iDb;
191869
191870 if( argc>=4 ){
191871 Token nm;
191872 sqlite3TokenInit(&nm, (char*)argv[3]);
191873 iDb = sqlite3FindDb(db, &nm);
191874 if( iDb<0 ){
191875 *pzErr = sqlite3_mprintf("no such database: %s", argv[3]);
191876 return SQLITE_ERROR;
191877 }
191878 }else{
191879 iDb = 0;
191880 }
191881 rc = sqlite3_declare_vtab(db, VTAB_SCHEMA);
191882 if( rc==SQLITE_OK ){
191883 pTab = (StatTable *)sqlite3_malloc64(sizeof(StatTable));
191884 if( pTab==0 ) rc = SQLITE_NOMEM_BKPT;
191885 }
191886
191887 assert( rc==SQLITE_OK || pTab==0 );
191888 if( rc==SQLITE_OK ){
191889 memset(pTab, 0, sizeof(StatTable));
191890 pTab->db = db;
191891 pTab->iDb = iDb;
191892 }
191893
191894 *ppVtab = (sqlite3_vtab*)pTab;
191895 return rc;
191896}
191897
191898/*
191899** Disconnect from or destroy a statvfs virtual table.
191900*/
191901static int statDisconnect(sqlite3_vtab *pVtab){
191902 sqlite3_free(pVtab);
191903 return SQLITE_OK;
191904}
191905
191906/*
191907** There is no "best-index". This virtual table always does a linear
191908** scan. However, a schema=? constraint should cause this table to
191909** operate on a different database schema, so check for it.
191910**
191911** idxNum is normally 0, but will be 1 if a schema=? constraint exists.
191912*/
191913static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
191914 int i;
191915
191916 /* Look for a valid schema=? constraint. If found, change the idxNum to
191917 ** 1 and request the value of that constraint be sent to xFilter. And
191918 ** lower the cost estimate to encourage the constrained version to be
191919 ** used.
191920 */
191921 for(i=0; i<pIdxInfo->nConstraint; i++){
191922 if( pIdxInfo->aConstraint[i].iColumn!=10 ) continue;
191923 if( pIdxInfo->aConstraint[i].usable==0 ) return SQLITE_CONSTRAINT;
191924 if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
191925 pIdxInfo->idxNum = 1;
191926 pIdxInfo->estimatedCost = 1.0;
191927 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
191928 pIdxInfo->aConstraintUsage[i].omit = 1;
191929 break;
191930 }
191931
191932
191933 /* Records are always returned in ascending order of (name, path).
191934 ** If this will satisfy the client, set the orderByConsumed flag so that
191935 ** SQLite does not do an external sort.
191936 */
191937 if( ( pIdxInfo->nOrderBy==1
191938 && pIdxInfo->aOrderBy[0].iColumn==0
191939 && pIdxInfo->aOrderBy[0].desc==0
191940 ) ||
191941 ( pIdxInfo->nOrderBy==2
191942 && pIdxInfo->aOrderBy[0].iColumn==0
191943 && pIdxInfo->aOrderBy[0].desc==0
191944 && pIdxInfo->aOrderBy[1].iColumn==1
191945 && pIdxInfo->aOrderBy[1].desc==0
191946 )
191947 ){
191948 pIdxInfo->orderByConsumed = 1;
191949 }
191950
191951 return SQLITE_OK;
191952}
191953
191954/*
191955** Open a new statvfs cursor.
191956*/
191957static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
191958 StatTable *pTab = (StatTable *)pVTab;
191959 StatCursor *pCsr;
191960
191961 pCsr = (StatCursor *)sqlite3_malloc64(sizeof(StatCursor));
191962 if( pCsr==0 ){
191963 return SQLITE_NOMEM_BKPT;
191964 }else{
191965 memset(pCsr, 0, sizeof(StatCursor));
191966 pCsr->base.pVtab = pVTab;
191967 pCsr->iDb = pTab->iDb;
191968 }
191969
191970 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
191971 return SQLITE_OK;
191972}
191973
191974static void statClearCells(StatPage *p){
191975 int i;
191976 if( p->aCell ){
191977 for(i=0; i<p->nCell; i++){
191978 sqlite3_free(p->aCell[i].aOvfl);
191979 }
191980 sqlite3_free(p->aCell);
191981 }
191982 p->nCell = 0;
191983 p->aCell = 0;
191984}
191985
191986static void statClearPage(StatPage *p){
191987 statClearCells(p);
191988 sqlite3PagerUnref(p->pPg);
191989 sqlite3_free(p->zPath);
191990 memset(p, 0, sizeof(StatPage));
191991}
191992
191993static void statResetCsr(StatCursor *pCsr){
191994 int i;
191995 sqlite3_reset(pCsr->pStmt);
191996 for(i=0; i<ArraySize(pCsr->aPage); i++){
191997 statClearPage(&pCsr->aPage[i]);
191998 }
191999 pCsr->iPage = 0;
192000 sqlite3_free(pCsr->zPath);
192001 pCsr->zPath = 0;
192002 pCsr->isEof = 0;
192003}
192004
192005/*
192006** Close a statvfs cursor.
192007*/
192008static int statClose(sqlite3_vtab_cursor *pCursor){
192009 StatCursor *pCsr = (StatCursor *)pCursor;
192010 statResetCsr(pCsr);
192011 sqlite3_finalize(pCsr->pStmt);
192012 sqlite3_free(pCsr);
192013 return SQLITE_OK;
192014}
192015
192016static void getLocalPayload(
192017 int nUsable, /* Usable bytes per page */
192018 u8 flags, /* Page flags */
192019 int nTotal, /* Total record (payload) size */
192020 int *pnLocal /* OUT: Bytes stored locally */
192021){
192022 int nLocal;
192023 int nMinLocal;
192024 int nMaxLocal;
192025
192026 if( flags==0x0D ){ /* Table leaf node */
192027 nMinLocal = (nUsable - 12) * 32 / 255 - 23;
192028 nMaxLocal = nUsable - 35;
192029 }else{ /* Index interior and leaf nodes */
192030 nMinLocal = (nUsable - 12) * 32 / 255 - 23;
192031 nMaxLocal = (nUsable - 12) * 64 / 255 - 23;
192032 }
192033
192034 nLocal = nMinLocal + (nTotal - nMinLocal) % (nUsable - 4);
192035 if( nLocal>nMaxLocal ) nLocal = nMinLocal;
192036 *pnLocal = nLocal;
192037}
192038
192039static int statDecodePage(Btree *pBt, StatPage *p){
192040 int nUnused;
192041 int iOff;
192042 int nHdr;
192043 int isLeaf;
192044 int szPage;
192045
192046 u8 *aData = sqlite3PagerGetData(p->pPg);
192047 u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0];
192048
192049 p->flags = aHdr[0];
192050 if( p->flags==0x0A || p->flags==0x0D ){
192051 isLeaf = 1;
192052 nHdr = 8;
192053 }else if( p->flags==0x05 || p->flags==0x02 ){
192054 isLeaf = 0;
192055 nHdr = 12;
192056 }else{
192057 goto statPageIsCorrupt;
192058 }
192059 if( p->iPgno==1 ) nHdr += 100;
192060 p->nCell = get2byte(&aHdr[3]);
192061 p->nMxPayload = 0;
192062 szPage = sqlite3BtreeGetPageSize(pBt);
192063
192064 nUnused = get2byte(&aHdr[5]) - nHdr - 2*p->nCell;
192065 nUnused += (int)aHdr[7];
192066 iOff = get2byte(&aHdr[1]);
192067 while( iOff ){
192068 int iNext;
192069 if( iOff>=szPage ) goto statPageIsCorrupt;
192070 nUnused += get2byte(&aData[iOff+2]);
192071 iNext = get2byte(&aData[iOff]);
192072 if( iNext<iOff+4 && iNext>0 ) goto statPageIsCorrupt;
192073 iOff = iNext;
192074 }
192075 p->nUnused = nUnused;
192076 p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]);
192077
192078 if( p->nCell ){
192079 int i; /* Used to iterate through cells */
192080 int nUsable; /* Usable bytes per page */
192081
192082 sqlite3BtreeEnter(pBt);
192083 nUsable = szPage - sqlite3BtreeGetReserveNoMutex(pBt);
192084 sqlite3BtreeLeave(pBt);
192085 p->aCell = sqlite3_malloc64((p->nCell+1) * sizeof(StatCell));
192086 if( p->aCell==0 ) return SQLITE_NOMEM_BKPT;
192087 memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell));
192088
192089 for(i=0; i<p->nCell; i++){
192090 StatCell *pCell = &p->aCell[i];
192091
192092 iOff = get2byte(&aData[nHdr+i*2]);
192093 if( iOff<nHdr || iOff>=szPage ) goto statPageIsCorrupt;
192094 if( !isLeaf ){
192095 pCell->iChildPg = sqlite3Get4byte(&aData[iOff]);
192096 iOff += 4;
192097 }
192098 if( p->flags==0x05 ){
192099 /* A table interior node. nPayload==0. */
192100 }else{
192101 u32 nPayload; /* Bytes of payload total (local+overflow) */
192102 int nLocal; /* Bytes of payload stored locally */
192103 iOff += getVarint32(&aData[iOff], nPayload);
192104 if( p->flags==0x0D ){
192105 u64 dummy;
192106 iOff += sqlite3GetVarint(&aData[iOff], &dummy);
192107 }
192108 if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload;
192109 getLocalPayload(nUsable, p->flags, nPayload, &nLocal);
192110 if( nLocal<0 ) goto statPageIsCorrupt;
192111 pCell->nLocal = nLocal;
192112 assert( nPayload>=(u32)nLocal );
192113 assert( nLocal<=(nUsable-35) );
192114 if( nPayload>(u32)nLocal ){
192115 int j;
192116 int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
192117 if( iOff+nLocal>nUsable ) goto statPageIsCorrupt;
192118 pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
192119 pCell->nOvfl = nOvfl;
192120 pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl);
192121 if( pCell->aOvfl==0 ) return SQLITE_NOMEM_BKPT;
192122 pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
192123 for(j=1; j<nOvfl; j++){
192124 int rc;
192125 u32 iPrev = pCell->aOvfl[j-1];
192126 DbPage *pPg = 0;
192127 rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg, 0);
192128 if( rc!=SQLITE_OK ){
192129 assert( pPg==0 );
192130 return rc;
192131 }
192132 pCell->aOvfl[j] = sqlite3Get4byte(sqlite3PagerGetData(pPg));
192133 sqlite3PagerUnref(pPg);
192134 }
192135 }
192136 }
192137 }
192138 }
192139
192140 return SQLITE_OK;
192141
192142statPageIsCorrupt:
192143 p->flags = 0;
192144 statClearCells(p);
192145 return SQLITE_OK;
192146}
192147
192148/*
192149** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on
192150** the current value of pCsr->iPageno.
192151*/
192152static void statSizeAndOffset(StatCursor *pCsr){
192153 StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab;
192154 Btree *pBt = pTab->db->aDb[pTab->iDb].pBt;
192155 Pager *pPager = sqlite3BtreePager(pBt);
192156 sqlite3_file *fd;
192157 sqlite3_int64 x[2];
192158
192159 /* The default page size and offset */
192160 pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
192161 pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1);
192162
192163 /* If connected to a ZIPVFS backend, override the page size and
192164 ** offset with actual values obtained from ZIPVFS.
192165 */
192166 fd = sqlite3PagerFile(pPager);
192167 x[0] = pCsr->iPageno;
192168 if( sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){
192169 pCsr->iOffset = x[0];
192170 pCsr->szPage = (int)x[1];
192171 }
192172}
192173
192174/*
192175** Move a statvfs cursor to the next entry in the file.
192176*/
192177static int statNext(sqlite3_vtab_cursor *pCursor){
192178 int rc;
192179 int nPayload;
192180 char *z;
192181 StatCursor *pCsr = (StatCursor *)pCursor;
192182 StatTable *pTab = (StatTable *)pCursor->pVtab;
192183 Btree *pBt = pTab->db->aDb[pCsr->iDb].pBt;
192184 Pager *pPager = sqlite3BtreePager(pBt);
192185
192186 sqlite3_free(pCsr->zPath);
192187 pCsr->zPath = 0;
192188
192189statNextRestart:
192190 if( pCsr->aPage[0].pPg==0 ){
192191 rc = sqlite3_step(pCsr->pStmt);
192192 if( rc==SQLITE_ROW ){
192193 int nPage;
192194 u32 iRoot = (u32)sqlite3_column_int64(pCsr->pStmt, 1);
192195 sqlite3PagerPagecount(pPager, &nPage);
192196 if( nPage==0 ){
192197 pCsr->isEof = 1;
192198 return sqlite3_reset(pCsr->pStmt);
192199 }
192200 rc = sqlite3PagerGet(pPager, iRoot, &pCsr->aPage[0].pPg, 0);
192201 pCsr->aPage[0].iPgno = iRoot;
192202 pCsr->aPage[0].iCell = 0;
192203 pCsr->aPage[0].zPath = z = sqlite3_mprintf("/");
192204 pCsr->iPage = 0;
192205 if( z==0 ) rc = SQLITE_NOMEM_BKPT;
192206 }else{
192207 pCsr->isEof = 1;
192208 return sqlite3_reset(pCsr->pStmt);
192209 }
192210 }else{
192211
192212 /* Page p itself has already been visited. */
192213 StatPage *p = &pCsr->aPage[pCsr->iPage];
192214
192215 while( p->iCell<p->nCell ){
192216 StatCell *pCell = &p->aCell[p->iCell];
192217 if( pCell->iOvfl<pCell->nOvfl ){
192218 int nUsable;
192219 sqlite3BtreeEnter(pBt);
192220 nUsable = sqlite3BtreeGetPageSize(pBt) -
192221 sqlite3BtreeGetReserveNoMutex(pBt);
192222 sqlite3BtreeLeave(pBt);
192223 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
192224 pCsr->iPageno = pCell->aOvfl[pCell->iOvfl];
192225 pCsr->zPagetype = "overflow";
192226 pCsr->nCell = 0;
192227 pCsr->nMxPayload = 0;
192228 pCsr->zPath = z = sqlite3_mprintf(
192229 "%s%.3x+%.6x", p->zPath, p->iCell, pCell->iOvfl
192230 );
192231 if( pCell->iOvfl<pCell->nOvfl-1 ){
192232 pCsr->nUnused = 0;
192233 pCsr->nPayload = nUsable - 4;
192234 }else{
192235 pCsr->nPayload = pCell->nLastOvfl;
192236 pCsr->nUnused = nUsable - 4 - pCsr->nPayload;
192237 }
192238 pCell->iOvfl++;
192239 statSizeAndOffset(pCsr);
192240 return z==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
192241 }
192242 if( p->iRightChildPg ) break;
192243 p->iCell++;
192244 }
192245
192246 if( !p->iRightChildPg || p->iCell>p->nCell ){
192247 statClearPage(p);
192248 if( pCsr->iPage==0 ) return statNext(pCursor);
192249 pCsr->iPage--;
192250 goto statNextRestart; /* Tail recursion */
192251 }
192252 pCsr->iPage++;
192253 assert( p==&pCsr->aPage[pCsr->iPage-1] );
192254
192255 if( p->iCell==p->nCell ){
192256 p[1].iPgno = p->iRightChildPg;
192257 }else{
192258 p[1].iPgno = p->aCell[p->iCell].iChildPg;
192259 }
192260 rc = sqlite3PagerGet(pPager, p[1].iPgno, &p[1].pPg, 0);
192261 p[1].iCell = 0;
192262 p[1].zPath = z = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell);
192263 p->iCell++;
192264 if( z==0 ) rc = SQLITE_NOMEM_BKPT;
192265 }
192266
192267
192268 /* Populate the StatCursor fields with the values to be returned
192269 ** by the xColumn() and xRowid() methods.
192270 */
192271 if( rc==SQLITE_OK ){
192272 int i;
192273 StatPage *p = &pCsr->aPage[pCsr->iPage];
192274 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
192275 pCsr->iPageno = p->iPgno;
192276
192277 rc = statDecodePage(pBt, p);
192278 if( rc==SQLITE_OK ){
192279 statSizeAndOffset(pCsr);
192280
192281 switch( p->flags ){
192282 case 0x05: /* table internal */
192283 case 0x02: /* index internal */
192284 pCsr->zPagetype = "internal";
192285 break;
192286 case 0x0D: /* table leaf */
192287 case 0x0A: /* index leaf */
192288 pCsr->zPagetype = "leaf";
192289 break;
192290 default:
192291 pCsr->zPagetype = "corrupted";
192292 break;
192293 }
192294 pCsr->nCell = p->nCell;
192295 pCsr->nUnused = p->nUnused;
192296 pCsr->nMxPayload = p->nMxPayload;
192297 pCsr->zPath = z = sqlite3_mprintf("%s", p->zPath);
192298 if( z==0 ) rc = SQLITE_NOMEM_BKPT;
192299 nPayload = 0;
192300 for(i=0; i<p->nCell; i++){
192301 nPayload += p->aCell[i].nLocal;
192302 }
192303 pCsr->nPayload = nPayload;
192304 }
192305 }
192306
192307 return rc;
192308}
192309
192310static int statEof(sqlite3_vtab_cursor *pCursor){
192311 StatCursor *pCsr = (StatCursor *)pCursor;
192312 return pCsr->isEof;
192313}
192314
192315static int statFilter(
192316 sqlite3_vtab_cursor *pCursor,
192317 int idxNum, const char *idxStr,
192318 int argc, sqlite3_value **argv
192319){
192320 StatCursor *pCsr = (StatCursor *)pCursor;
192321 StatTable *pTab = (StatTable*)(pCursor->pVtab);
192322 char *zSql;
192323 int rc = SQLITE_OK;
192324 char *zMaster;
192325
192326 if( idxNum==1 ){
192327 const char *zDbase = (const char*)sqlite3_value_text(argv[0]);
192328 pCsr->iDb = sqlite3FindDbName(pTab->db, zDbase);
192329 if( pCsr->iDb<0 ){
192330 sqlite3_free(pCursor->pVtab->zErrMsg);
192331 pCursor->pVtab->zErrMsg = sqlite3_mprintf("no such schema: %s", zDbase);
192332 return pCursor->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM_BKPT;
192333 }
192334 }else{
192335 pCsr->iDb = pTab->iDb;
192336 }
192337 statResetCsr(pCsr);
192338 sqlite3_finalize(pCsr->pStmt);
192339 pCsr->pStmt = 0;
192340 zMaster = pCsr->iDb==1 ? "sqlite_temp_master" : "sqlite_master";
192341 zSql = sqlite3_mprintf(
192342 "SELECT 'sqlite_master' AS name, 1 AS rootpage, 'table' AS type"
192343 " UNION ALL "
192344 "SELECT name, rootpage, type"
192345 " FROM \"%w\".%s WHERE rootpage!=0"
192346 " ORDER BY name", pTab->db->aDb[pCsr->iDb].zDbSName, zMaster);
192347 if( zSql==0 ){
192348 return SQLITE_NOMEM_BKPT;
192349 }else{
192350 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
192351 sqlite3_free(zSql);
192352 }
192353
192354 if( rc==SQLITE_OK ){
192355 rc = statNext(pCursor);
192356 }
192357 return rc;
192358}
192359
192360static int statColumn(
192361 sqlite3_vtab_cursor *pCursor,
192362 sqlite3_context *ctx,
192363 int i
192364){
192365 StatCursor *pCsr = (StatCursor *)pCursor;
192366 switch( i ){
192367 case 0: /* name */
192368 sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_TRANSIENT);
192369 break;
192370 case 1: /* path */
192371 sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT);
192372 break;
192373 case 2: /* pageno */
192374 sqlite3_result_int64(ctx, pCsr->iPageno);
192375 break;
192376 case 3: /* pagetype */
192377 sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC);
192378 break;
192379 case 4: /* ncell */
192380 sqlite3_result_int(ctx, pCsr->nCell);
192381 break;
192382 case 5: /* payload */
192383 sqlite3_result_int(ctx, pCsr->nPayload);
192384 break;
192385 case 6: /* unused */
192386 sqlite3_result_int(ctx, pCsr->nUnused);
192387 break;
192388 case 7: /* mx_payload */
192389 sqlite3_result_int(ctx, pCsr->nMxPayload);
192390 break;
192391 case 8: /* pgoffset */
192392 sqlite3_result_int64(ctx, pCsr->iOffset);
192393 break;
192394 case 9: /* pgsize */
192395 sqlite3_result_int(ctx, pCsr->szPage);
192396 break;
192397 default: { /* schema */
192398 sqlite3 *db = sqlite3_context_db_handle(ctx);
192399 int iDb = pCsr->iDb;
192400 sqlite3_result_text(ctx, db->aDb[iDb].zDbSName, -1, SQLITE_STATIC);
192401 break;
192402 }
192403 }
192404 return SQLITE_OK;
192405}
192406
192407static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
192408 StatCursor *pCsr = (StatCursor *)pCursor;
192409 *pRowid = pCsr->iPageno;
192410 return SQLITE_OK;
192411}
192412
192413/*
192414** Invoke this routine to register the "dbstat" virtual table module
192415*/
192416SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){
192417 static sqlite3_module dbstat_module = {
192418 0, /* iVersion */
192419 statConnect, /* xCreate */
192420 statConnect, /* xConnect */
192421 statBestIndex, /* xBestIndex */
192422 statDisconnect, /* xDisconnect */
192423 statDisconnect, /* xDestroy */
192424 statOpen, /* xOpen - open a cursor */
192425 statClose, /* xClose - close a cursor */
192426 statFilter, /* xFilter - configure scan constraints */
192427 statNext, /* xNext - advance a cursor */
192428 statEof, /* xEof - check for end of scan */
192429 statColumn, /* xColumn - read data */
192430 statRowid, /* xRowid - read data */
192431 0, /* xUpdate */
192432 0, /* xBegin */
192433 0, /* xSync */
192434 0, /* xCommit */
192435 0, /* xRollback */
192436 0, /* xFindMethod */
192437 0, /* xRename */
192438 0, /* xSavepoint */
192439 0, /* xRelease */
192440 0, /* xRollbackTo */
192441 0 /* xShadowName */
192442 };
192443 return sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
192444}
192445#elif defined(SQLITE_ENABLE_DBSTAT_VTAB)
192446SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){ return SQLITE_OK; }
192447#endif /* SQLITE_ENABLE_DBSTAT_VTAB */
192448
192449/************** End of dbstat.c **********************************************/
192450/************** Begin file dbpage.c ******************************************/
192451/*
192452** 2017-10-11
192453**
192454** The author disclaims copyright to this source code. In place of
192455** a legal notice, here is a blessing:
192456**
192457** May you do good and not evil.
192458** May you find forgiveness for yourself and forgive others.
192459** May you share freely, never taking more than you give.
192460**
192461******************************************************************************
192462**
192463** This file contains an implementation of the "sqlite_dbpage" virtual table.
192464**
192465** The sqlite_dbpage virtual table is used to read or write whole raw
192466** pages of the database file. The pager interface is used so that
192467** uncommitted changes and changes recorded in the WAL file are correctly
192468** retrieved.
192469**
192470** Usage example:
192471**
192472** SELECT data FROM sqlite_dbpage('aux1') WHERE pgno=123;
192473**
192474** This is an eponymous virtual table so it does not need to be created before
192475** use. The optional argument to the sqlite_dbpage() table name is the
192476** schema for the database file that is to be read. The default schema is
192477** "main".
192478**
192479** The data field of sqlite_dbpage table can be updated. The new
192480** value must be a BLOB which is the correct page size, otherwise the
192481** update fails. Rows may not be deleted or inserted.
192482*/
192483
192484/* #include "sqliteInt.h" ** Requires access to internal data structures ** */
192485#if (defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)) \
192486 && !defined(SQLITE_OMIT_VIRTUALTABLE)
192487
192488typedef struct DbpageTable DbpageTable;
192489typedef struct DbpageCursor DbpageCursor;
192490
192491struct DbpageCursor {
192492 sqlite3_vtab_cursor base; /* Base class. Must be first */
192493 int pgno; /* Current page number */
192494 int mxPgno; /* Last page to visit on this scan */
192495 Pager *pPager; /* Pager being read/written */
192496 DbPage *pPage1; /* Page 1 of the database */
192497 int iDb; /* Index of database to analyze */
192498 int szPage; /* Size of each page in bytes */
192499};
192500
192501struct DbpageTable {
192502 sqlite3_vtab base; /* Base class. Must be first */
192503 sqlite3 *db; /* The database */
192504};
192505
192506/* Columns */
192507#define DBPAGE_COLUMN_PGNO 0
192508#define DBPAGE_COLUMN_DATA 1
192509#define DBPAGE_COLUMN_SCHEMA 2
192510
192511
192512
192513/*
192514** Connect to or create a dbpagevfs virtual table.
192515*/
192516static int dbpageConnect(
192517 sqlite3 *db,
192518 void *pAux,
192519 int argc, const char *const*argv,
192520 sqlite3_vtab **ppVtab,
192521 char **pzErr
192522){
192523 DbpageTable *pTab = 0;
192524 int rc = SQLITE_OK;
192525
192526 rc = sqlite3_declare_vtab(db,
192527 "CREATE TABLE x(pgno INTEGER PRIMARY KEY, data BLOB, schema HIDDEN)");
192528 if( rc==SQLITE_OK ){
192529 pTab = (DbpageTable *)sqlite3_malloc64(sizeof(DbpageTable));
192530 if( pTab==0 ) rc = SQLITE_NOMEM_BKPT;
192531 }
192532
192533 assert( rc==SQLITE_OK || pTab==0 );
192534 if( rc==SQLITE_OK ){
192535 memset(pTab, 0, sizeof(DbpageTable));
192536 pTab->db = db;
192537 }
192538
192539 *ppVtab = (sqlite3_vtab*)pTab;
192540 return rc;
192541}
192542
192543/*
192544** Disconnect from or destroy a dbpagevfs virtual table.
192545*/
192546static int dbpageDisconnect(sqlite3_vtab *pVtab){
192547 sqlite3_free(pVtab);
192548 return SQLITE_OK;
192549}
192550
192551/*
192552** idxNum:
192553**
192554** 0 schema=main, full table scan
192555** 1 schema=main, pgno=?1
192556** 2 schema=?1, full table scan
192557** 3 schema=?1, pgno=?2
192558*/
192559static int dbpageBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
192560 int i;
192561 int iPlan = 0;
192562
192563 /* If there is a schema= constraint, it must be honored. Report a
192564 ** ridiculously large estimated cost if the schema= constraint is
192565 ** unavailable
192566 */
192567 for(i=0; i<pIdxInfo->nConstraint; i++){
192568 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
192569 if( p->iColumn!=DBPAGE_COLUMN_SCHEMA ) continue;
192570 if( p->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
192571 if( !p->usable ){
192572 /* No solution. */
192573 return SQLITE_CONSTRAINT;
192574 }
192575 iPlan = 2;
192576 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
192577 pIdxInfo->aConstraintUsage[i].omit = 1;
192578 break;
192579 }
192580
192581 /* If we reach this point, it means that either there is no schema=
192582 ** constraint (in which case we use the "main" schema) or else the
192583 ** schema constraint was accepted. Lower the estimated cost accordingly
192584 */
192585 pIdxInfo->estimatedCost = 1.0e6;
192586
192587 /* Check for constraints against pgno */
192588 for(i=0; i<pIdxInfo->nConstraint; i++){
192589 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
192590 if( p->usable && p->iColumn<=0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
192591 pIdxInfo->estimatedRows = 1;
192592 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
192593 pIdxInfo->estimatedCost = 1.0;
192594 pIdxInfo->aConstraintUsage[i].argvIndex = iPlan ? 2 : 1;
192595 pIdxInfo->aConstraintUsage[i].omit = 1;
192596 iPlan |= 1;
192597 break;
192598 }
192599 }
192600 pIdxInfo->idxNum = iPlan;
192601
192602 if( pIdxInfo->nOrderBy>=1
192603 && pIdxInfo->aOrderBy[0].iColumn<=0
192604 && pIdxInfo->aOrderBy[0].desc==0
192605 ){
192606 pIdxInfo->orderByConsumed = 1;
192607 }
192608 return SQLITE_OK;
192609}
192610
192611/*
192612** Open a new dbpagevfs cursor.
192613*/
192614static int dbpageOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
192615 DbpageCursor *pCsr;
192616
192617 pCsr = (DbpageCursor *)sqlite3_malloc64(sizeof(DbpageCursor));
192618 if( pCsr==0 ){
192619 return SQLITE_NOMEM_BKPT;
192620 }else{
192621 memset(pCsr, 0, sizeof(DbpageCursor));
192622 pCsr->base.pVtab = pVTab;
192623 pCsr->pgno = -1;
192624 }
192625
192626 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
192627 return SQLITE_OK;
192628}
192629
192630/*
192631** Close a dbpagevfs cursor.
192632*/
192633static int dbpageClose(sqlite3_vtab_cursor *pCursor){
192634 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
192635 if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
192636 sqlite3_free(pCsr);
192637 return SQLITE_OK;
192638}
192639
192640/*
192641** Move a dbpagevfs cursor to the next entry in the file.
192642*/
192643static int dbpageNext(sqlite3_vtab_cursor *pCursor){
192644 int rc = SQLITE_OK;
192645 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
192646 pCsr->pgno++;
192647 return rc;
192648}
192649
192650static int dbpageEof(sqlite3_vtab_cursor *pCursor){
192651 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
192652 return pCsr->pgno > pCsr->mxPgno;
192653}
192654
192655/*
192656** idxNum:
192657**
192658** 0 schema=main, full table scan
192659** 1 schema=main, pgno=?1
192660** 2 schema=?1, full table scan
192661** 3 schema=?1, pgno=?2
192662**
192663** idxStr is not used
192664*/
192665static int dbpageFilter(
192666 sqlite3_vtab_cursor *pCursor,
192667 int idxNum, const char *idxStr,
192668 int argc, sqlite3_value **argv
192669){
192670 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
192671 DbpageTable *pTab = (DbpageTable *)pCursor->pVtab;
192672 int rc;
192673 sqlite3 *db = pTab->db;
192674 Btree *pBt;
192675
192676 /* Default setting is no rows of result */
192677 pCsr->pgno = 1;
192678 pCsr->mxPgno = 0;
192679
192680 if( idxNum & 2 ){
192681 const char *zSchema;
192682 assert( argc>=1 );
192683 zSchema = (const char*)sqlite3_value_text(argv[0]);
192684 pCsr->iDb = sqlite3FindDbName(db, zSchema);
192685 if( pCsr->iDb<0 ) return SQLITE_OK;
192686 }else{
192687 pCsr->iDb = 0;
192688 }
192689 pBt = db->aDb[pCsr->iDb].pBt;
192690 if( pBt==0 ) return SQLITE_OK;
192691 pCsr->pPager = sqlite3BtreePager(pBt);
192692 pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
192693 pCsr->mxPgno = sqlite3BtreeLastPage(pBt);
192694 if( idxNum & 1 ){
192695 assert( argc>(idxNum>>1) );
192696 pCsr->pgno = sqlite3_value_int(argv[idxNum>>1]);
192697 if( pCsr->pgno<1 || pCsr->pgno>pCsr->mxPgno ){
192698 pCsr->pgno = 1;
192699 pCsr->mxPgno = 0;
192700 }else{
192701 pCsr->mxPgno = pCsr->pgno;
192702 }
192703 }else{
192704 assert( pCsr->pgno==1 );
192705 }
192706 if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
192707 rc = sqlite3PagerGet(pCsr->pPager, 1, &pCsr->pPage1, 0);
192708 return rc;
192709}
192710
192711static int dbpageColumn(
192712 sqlite3_vtab_cursor *pCursor,
192713 sqlite3_context *ctx,
192714 int i
192715){
192716 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
192717 int rc = SQLITE_OK;
192718 switch( i ){
192719 case 0: { /* pgno */
192720 sqlite3_result_int(ctx, pCsr->pgno);
192721 break;
192722 }
192723 case 1: { /* data */
192724 DbPage *pDbPage = 0;
192725 rc = sqlite3PagerGet(pCsr->pPager, pCsr->pgno, (DbPage**)&pDbPage, 0);
192726 if( rc==SQLITE_OK ){
192727 sqlite3_result_blob(ctx, sqlite3PagerGetData(pDbPage), pCsr->szPage,
192728 SQLITE_TRANSIENT);
192729 }
192730 sqlite3PagerUnref(pDbPage);
192731 break;
192732 }
192733 default: { /* schema */
192734 sqlite3 *db = sqlite3_context_db_handle(ctx);
192735 sqlite3_result_text(ctx, db->aDb[pCsr->iDb].zDbSName, -1, SQLITE_STATIC);
192736 break;
192737 }
192738 }
192739 return SQLITE_OK;
192740}
192741
192742static int dbpageRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
192743 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
192744 *pRowid = pCsr->pgno;
192745 return SQLITE_OK;
192746}
192747
192748static int dbpageUpdate(
192749 sqlite3_vtab *pVtab,
192750 int argc,
192751 sqlite3_value **argv,
192752 sqlite_int64 *pRowid
192753){
192754 DbpageTable *pTab = (DbpageTable *)pVtab;
192755 Pgno pgno;
192756 DbPage *pDbPage = 0;
192757 int rc = SQLITE_OK;
192758 char *zErr = 0;
192759 const char *zSchema;
192760 int iDb;
192761 Btree *pBt;
192762 Pager *pPager;
192763 int szPage;
192764
192765 if( pTab->db->flags & SQLITE_Defensive ){
192766 zErr = "read-only";
192767 goto update_fail;
192768 }
192769 if( argc==1 ){
192770 zErr = "cannot delete";
192771 goto update_fail;
192772 }
192773 pgno = sqlite3_value_int(argv[0]);
192774 if( (Pgno)sqlite3_value_int(argv[1])!=pgno ){
192775 zErr = "cannot insert";
192776 goto update_fail;
192777 }
192778 zSchema = (const char*)sqlite3_value_text(argv[4]);
192779 iDb = zSchema ? sqlite3FindDbName(pTab->db, zSchema) : -1;
192780 if( iDb<0 ){
192781 zErr = "no such schema";
192782 goto update_fail;
192783 }
192784 pBt = pTab->db->aDb[iDb].pBt;
192785 if( pgno<1 || pBt==0 || pgno>(int)sqlite3BtreeLastPage(pBt) ){
192786 zErr = "bad page number";
192787 goto update_fail;
192788 }
192789 szPage = sqlite3BtreeGetPageSize(pBt);
192790 if( sqlite3_value_type(argv[3])!=SQLITE_BLOB
192791 || sqlite3_value_bytes(argv[3])!=szPage
192792 ){
192793 zErr = "bad page value";
192794 goto update_fail;
192795 }
192796 pPager = sqlite3BtreePager(pBt);
192797 rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pDbPage, 0);
192798 if( rc==SQLITE_OK ){
192799 rc = sqlite3PagerWrite(pDbPage);
192800 if( rc==SQLITE_OK ){
192801 memcpy(sqlite3PagerGetData(pDbPage),
192802 sqlite3_value_blob(argv[3]),
192803 szPage);
192804 }
192805 }
192806 sqlite3PagerUnref(pDbPage);
192807 return rc;
192808
192809update_fail:
192810 sqlite3_free(pVtab->zErrMsg);
192811 pVtab->zErrMsg = sqlite3_mprintf("%s", zErr);
192812 return SQLITE_ERROR;
192813}
192814
192815/* Since we do not know in advance which database files will be
192816** written by the sqlite_dbpage virtual table, start a write transaction
192817** on them all.
192818*/
192819static int dbpageBegin(sqlite3_vtab *pVtab){
192820 DbpageTable *pTab = (DbpageTable *)pVtab;
192821 sqlite3 *db = pTab->db;
192822 int i;
192823 for(i=0; i<db->nDb; i++){
192824 Btree *pBt = db->aDb[i].pBt;
192825 if( pBt ) sqlite3BtreeBeginTrans(pBt, 1, 0);
192826 }
192827 return SQLITE_OK;
192828}
192829
192830
192831/*
192832** Invoke this routine to register the "dbpage" virtual table module
192833*/
192834SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3 *db){
192835 static sqlite3_module dbpage_module = {
192836 0, /* iVersion */
192837 dbpageConnect, /* xCreate */
192838 dbpageConnect, /* xConnect */
192839 dbpageBestIndex, /* xBestIndex */
192840 dbpageDisconnect, /* xDisconnect */
192841 dbpageDisconnect, /* xDestroy */
192842 dbpageOpen, /* xOpen - open a cursor */
192843 dbpageClose, /* xClose - close a cursor */
192844 dbpageFilter, /* xFilter - configure scan constraints */
192845 dbpageNext, /* xNext - advance a cursor */
192846 dbpageEof, /* xEof - check for end of scan */
192847 dbpageColumn, /* xColumn - read data */
192848 dbpageRowid, /* xRowid - read data */
192849 dbpageUpdate, /* xUpdate */
192850 dbpageBegin, /* xBegin */
192851 0, /* xSync */
192852 0, /* xCommit */
192853 0, /* xRollback */
192854 0, /* xFindMethod */
192855 0, /* xRename */
192856 0, /* xSavepoint */
192857 0, /* xRelease */
192858 0, /* xRollbackTo */
192859 0 /* xShadowName */
192860 };
192861 return sqlite3_create_module(db, "sqlite_dbpage", &dbpage_module, 0);
192862}
192863#elif defined(SQLITE_ENABLE_DBPAGE_VTAB)
192864SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3 *db){ return SQLITE_OK; }
192865#endif /* SQLITE_ENABLE_DBSTAT_VTAB */
192866
192867/************** End of dbpage.c **********************************************/
192868/************** Begin file sqlite3session.c **********************************/
192869
192870#if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK)
192871/* #include "sqlite3session.h" */
192872/* #include <assert.h> */
192873/* #include <string.h> */
192874
192875#ifndef SQLITE_AMALGAMATION
192876/* # include "sqliteInt.h" */
192877/* # include "vdbeInt.h" */
192878#endif
192879
192880typedef struct SessionTable SessionTable;
192881typedef struct SessionChange SessionChange;
192882typedef struct SessionBuffer SessionBuffer;
192883typedef struct SessionInput SessionInput;
192884
192885/*
192886** Minimum chunk size used by streaming versions of functions.
192887*/
192888#ifndef SESSIONS_STRM_CHUNK_SIZE
192889# ifdef SQLITE_TEST
192890# define SESSIONS_STRM_CHUNK_SIZE 64
192891# else
192892# define SESSIONS_STRM_CHUNK_SIZE 1024
192893# endif
192894#endif
192895
192896static int sessions_strm_chunk_size = SESSIONS_STRM_CHUNK_SIZE;
192897
192898typedef struct SessionHook SessionHook;
192899struct SessionHook {
192900 void *pCtx;
192901 int (*xOld)(void*,int,sqlite3_value**);
192902 int (*xNew)(void*,int,sqlite3_value**);
192903 int (*xCount)(void*);
192904 int (*xDepth)(void*);
192905};
192906
192907/*
192908** Session handle structure.
192909*/
192910struct sqlite3_session {
192911 sqlite3 *db; /* Database handle session is attached to */
192912 char *zDb; /* Name of database session is attached to */
192913 int bEnable; /* True if currently recording */
192914 int bIndirect; /* True if all changes are indirect */
192915 int bAutoAttach; /* True to auto-attach tables */
192916 int rc; /* Non-zero if an error has occurred */
192917 void *pFilterCtx; /* First argument to pass to xTableFilter */
192918 int (*xTableFilter)(void *pCtx, const char *zTab);
192919 sqlite3_value *pZeroBlob; /* Value containing X'' */
192920 sqlite3_session *pNext; /* Next session object on same db. */
192921 SessionTable *pTable; /* List of attached tables */
192922 SessionHook hook; /* APIs to grab new and old data with */
192923};
192924
192925/*
192926** Instances of this structure are used to build strings or binary records.
192927*/
192928struct SessionBuffer {
192929 u8 *aBuf; /* Pointer to changeset buffer */
192930 int nBuf; /* Size of buffer aBuf */
192931 int nAlloc; /* Size of allocation containing aBuf */
192932};
192933
192934/*
192935** An object of this type is used internally as an abstraction for
192936** input data. Input data may be supplied either as a single large buffer
192937** (e.g. sqlite3changeset_start()) or using a stream function (e.g.
192938** sqlite3changeset_start_strm()).
192939*/
192940struct SessionInput {
192941 int bNoDiscard; /* If true, do not discard in InputBuffer() */
192942 int iCurrent; /* Offset in aData[] of current change */
192943 int iNext; /* Offset in aData[] of next change */
192944 u8 *aData; /* Pointer to buffer containing changeset */
192945 int nData; /* Number of bytes in aData */
192946
192947 SessionBuffer buf; /* Current read buffer */
192948 int (*xInput)(void*, void*, int*); /* Input stream call (or NULL) */
192949 void *pIn; /* First argument to xInput */
192950 int bEof; /* Set to true after xInput finished */
192951};
192952
192953/*
192954** Structure for changeset iterators.
192955*/
192956struct sqlite3_changeset_iter {
192957 SessionInput in; /* Input buffer or stream */
192958 SessionBuffer tblhdr; /* Buffer to hold apValue/zTab/abPK/ */
192959 int bPatchset; /* True if this is a patchset */
192960 int bInvert; /* True to invert changeset */
192961 int rc; /* Iterator error code */
192962 sqlite3_stmt *pConflict; /* Points to conflicting row, if any */
192963 char *zTab; /* Current table */
192964 int nCol; /* Number of columns in zTab */
192965 int op; /* Current operation */
192966 int bIndirect; /* True if current change was indirect */
192967 u8 *abPK; /* Primary key array */
192968 sqlite3_value **apValue; /* old.* and new.* values */
192969};
192970
192971/*
192972** Each session object maintains a set of the following structures, one
192973** for each table the session object is monitoring. The structures are
192974** stored in a linked list starting at sqlite3_session.pTable.
192975**
192976** The keys of the SessionTable.aChange[] hash table are all rows that have
192977** been modified in any way since the session object was attached to the
192978** table.
192979**
192980** The data associated with each hash-table entry is a structure containing
192981** a subset of the initial values that the modified row contained at the
192982** start of the session. Or no initial values if the row was inserted.
192983*/
192984struct SessionTable {
192985 SessionTable *pNext;
192986 char *zName; /* Local name of table */
192987 int nCol; /* Number of columns in table zName */
192988 int bStat1; /* True if this is sqlite_stat1 */
192989 const char **azCol; /* Column names */
192990 u8 *abPK; /* Array of primary key flags */
192991 int nEntry; /* Total number of entries in hash table */
192992 int nChange; /* Size of apChange[] array */
192993 SessionChange **apChange; /* Hash table buckets */
192994};
192995
192996/*
192997** RECORD FORMAT:
192998**
192999** The following record format is similar to (but not compatible with) that
193000** used in SQLite database files. This format is used as part of the
193001** change-set binary format, and so must be architecture independent.
193002**
193003** Unlike the SQLite database record format, each field is self-contained -
193004** there is no separation of header and data. Each field begins with a
193005** single byte describing its type, as follows:
193006**
193007** 0x00: Undefined value.
193008** 0x01: Integer value.
193009** 0x02: Real value.
193010** 0x03: Text value.
193011** 0x04: Blob value.
193012** 0x05: SQL NULL value.
193013**
193014** Note that the above match the definitions of SQLITE_INTEGER, SQLITE_TEXT
193015** and so on in sqlite3.h. For undefined and NULL values, the field consists
193016** only of the single type byte. For other types of values, the type byte
193017** is followed by:
193018**
193019** Text values:
193020** A varint containing the number of bytes in the value (encoded using
193021** UTF-8). Followed by a buffer containing the UTF-8 representation
193022** of the text value. There is no nul terminator.
193023**
193024** Blob values:
193025** A varint containing the number of bytes in the value, followed by
193026** a buffer containing the value itself.
193027**
193028** Integer values:
193029** An 8-byte big-endian integer value.
193030**
193031** Real values:
193032** An 8-byte big-endian IEEE 754-2008 real value.
193033**
193034** Varint values are encoded in the same way as varints in the SQLite
193035** record format.
193036**
193037** CHANGESET FORMAT:
193038**
193039** A changeset is a collection of DELETE, UPDATE and INSERT operations on
193040** one or more tables. Operations on a single table are grouped together,
193041** but may occur in any order (i.e. deletes, updates and inserts are all
193042** mixed together).
193043**
193044** Each group of changes begins with a table header:
193045**
193046** 1 byte: Constant 0x54 (capital 'T')
193047** Varint: Number of columns in the table.
193048** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
193049** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
193050**
193051** Followed by one or more changes to the table.
193052**
193053** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
193054** 1 byte: The "indirect-change" flag.
193055** old.* record: (delete and update only)
193056** new.* record: (insert and update only)
193057**
193058** The "old.*" and "new.*" records, if present, are N field records in the
193059** format described above under "RECORD FORMAT", where N is the number of
193060** columns in the table. The i'th field of each record is associated with
193061** the i'th column of the table, counting from left to right in the order
193062** in which columns were declared in the CREATE TABLE statement.
193063**
193064** The new.* record that is part of each INSERT change contains the values
193065** that make up the new row. Similarly, the old.* record that is part of each
193066** DELETE change contains the values that made up the row that was deleted
193067** from the database. In the changeset format, the records that are part
193068** of INSERT or DELETE changes never contain any undefined (type byte 0x00)
193069** fields.
193070**
193071** Within the old.* record associated with an UPDATE change, all fields
193072** associated with table columns that are not PRIMARY KEY columns and are
193073** not modified by the UPDATE change are set to "undefined". Other fields
193074** are set to the values that made up the row before the UPDATE that the
193075** change records took place. Within the new.* record, fields associated
193076** with table columns modified by the UPDATE change contain the new
193077** values. Fields associated with table columns that are not modified
193078** are set to "undefined".
193079**
193080** PATCHSET FORMAT:
193081**
193082** A patchset is also a collection of changes. It is similar to a changeset,
193083** but leaves undefined those fields that are not useful if no conflict
193084** resolution is required when applying the changeset.
193085**
193086** Each group of changes begins with a table header:
193087**
193088** 1 byte: Constant 0x50 (capital 'P')
193089** Varint: Number of columns in the table.
193090** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
193091** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
193092**
193093** Followed by one or more changes to the table.
193094**
193095** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
193096** 1 byte: The "indirect-change" flag.
193097** single record: (PK fields for DELETE, PK and modified fields for UPDATE,
193098** full record for INSERT).
193099**
193100** As in the changeset format, each field of the single record that is part
193101** of a patchset change is associated with the correspondingly positioned
193102** table column, counting from left to right within the CREATE TABLE
193103** statement.
193104**
193105** For a DELETE change, all fields within the record except those associated
193106** with PRIMARY KEY columns are omitted. The PRIMARY KEY fields contain the
193107** values identifying the row to delete.
193108**
193109** For an UPDATE change, all fields except those associated with PRIMARY KEY
193110** columns and columns that are modified by the UPDATE are set to "undefined".
193111** PRIMARY KEY fields contain the values identifying the table row to update,
193112** and fields associated with modified columns contain the new column values.
193113**
193114** The records associated with INSERT changes are in the same format as for
193115** changesets. It is not possible for a record associated with an INSERT
193116** change to contain a field set to "undefined".
193117**
193118** REBASE BLOB FORMAT:
193119**
193120** A rebase blob may be output by sqlite3changeset_apply_v2() and its
193121** streaming equivalent for use with the sqlite3_rebaser APIs to rebase
193122** existing changesets. A rebase blob contains one entry for each conflict
193123** resolved using either the OMIT or REPLACE strategies within the apply_v2()
193124** call.
193125**
193126** The format used for a rebase blob is very similar to that used for
193127** changesets. All entries related to a single table are grouped together.
193128**
193129** Each group of entries begins with a table header in changeset format:
193130**
193131** 1 byte: Constant 0x54 (capital 'T')
193132** Varint: Number of columns in the table.
193133** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
193134** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
193135**
193136** Followed by one or more entries associated with the table.
193137**
193138** 1 byte: Either SQLITE_INSERT (0x12), DELETE (0x09).
193139** 1 byte: Flag. 0x01 for REPLACE, 0x00 for OMIT.
193140** record: (in the record format defined above).
193141**
193142** In a rebase blob, the first field is set to SQLITE_INSERT if the change
193143** that caused the conflict was an INSERT or UPDATE, or to SQLITE_DELETE if
193144** it was a DELETE. The second field is set to 0x01 if the conflict
193145** resolution strategy was REPLACE, or 0x00 if it was OMIT.
193146**
193147** If the change that caused the conflict was a DELETE, then the single
193148** record is a copy of the old.* record from the original changeset. If it
193149** was an INSERT, then the single record is a copy of the new.* record. If
193150** the conflicting change was an UPDATE, then the single record is a copy
193151** of the new.* record with the PK fields filled in based on the original
193152** old.* record.
193153*/
193154
193155/*
193156** For each row modified during a session, there exists a single instance of
193157** this structure stored in a SessionTable.aChange[] hash table.
193158*/
193159struct SessionChange {
193160 int op; /* One of UPDATE, DELETE, INSERT */
193161 int bIndirect; /* True if this change is "indirect" */
193162 int nRecord; /* Number of bytes in buffer aRecord[] */
193163 u8 *aRecord; /* Buffer containing old.* record */
193164 SessionChange *pNext; /* For hash-table collisions */
193165};
193166
193167/*
193168** Write a varint with value iVal into the buffer at aBuf. Return the
193169** number of bytes written.
193170*/
193171static int sessionVarintPut(u8 *aBuf, int iVal){
193172 return putVarint32(aBuf, iVal);
193173}
193174
193175/*
193176** Return the number of bytes required to store value iVal as a varint.
193177*/
193178static int sessionVarintLen(int iVal){
193179 return sqlite3VarintLen(iVal);
193180}
193181
193182/*
193183** Read a varint value from aBuf[] into *piVal. Return the number of
193184** bytes read.
193185*/
193186static int sessionVarintGet(u8 *aBuf, int *piVal){
193187 return getVarint32(aBuf, *piVal);
193188}
193189
193190/* Load an unaligned and unsigned 32-bit integer */
193191#define SESSION_UINT32(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
193192
193193/*
193194** Read a 64-bit big-endian integer value from buffer aRec[]. Return
193195** the value read.
193196*/
193197static sqlite3_int64 sessionGetI64(u8 *aRec){
193198 u64 x = SESSION_UINT32(aRec);
193199 u32 y = SESSION_UINT32(aRec+4);
193200 x = (x<<32) + y;
193201 return (sqlite3_int64)x;
193202}
193203
193204/*
193205** Write a 64-bit big-endian integer value to the buffer aBuf[].
193206*/
193207static void sessionPutI64(u8 *aBuf, sqlite3_int64 i){
193208 aBuf[0] = (i>>56) & 0xFF;
193209 aBuf[1] = (i>>48) & 0xFF;
193210 aBuf[2] = (i>>40) & 0xFF;
193211 aBuf[3] = (i>>32) & 0xFF;
193212 aBuf[4] = (i>>24) & 0xFF;
193213 aBuf[5] = (i>>16) & 0xFF;
193214 aBuf[6] = (i>> 8) & 0xFF;
193215 aBuf[7] = (i>> 0) & 0xFF;
193216}
193217
193218/*
193219** This function is used to serialize the contents of value pValue (see
193220** comment titled "RECORD FORMAT" above).
193221**
193222** If it is non-NULL, the serialized form of the value is written to
193223** buffer aBuf. *pnWrite is set to the number of bytes written before
193224** returning. Or, if aBuf is NULL, the only thing this function does is
193225** set *pnWrite.
193226**
193227** If no error occurs, SQLITE_OK is returned. Or, if an OOM error occurs
193228** within a call to sqlite3_value_text() (may fail if the db is utf-16))
193229** SQLITE_NOMEM is returned.
193230*/
193231static int sessionSerializeValue(
193232 u8 *aBuf, /* If non-NULL, write serialized value here */
193233 sqlite3_value *pValue, /* Value to serialize */
193234 int *pnWrite /* IN/OUT: Increment by bytes written */
193235){
193236 int nByte; /* Size of serialized value in bytes */
193237
193238 if( pValue ){
193239 int eType; /* Value type (SQLITE_NULL, TEXT etc.) */
193240
193241 eType = sqlite3_value_type(pValue);
193242 if( aBuf ) aBuf[0] = eType;
193243
193244 switch( eType ){
193245 case SQLITE_NULL:
193246 nByte = 1;
193247 break;
193248
193249 case SQLITE_INTEGER:
193250 case SQLITE_FLOAT:
193251 if( aBuf ){
193252 /* TODO: SQLite does something special to deal with mixed-endian
193253 ** floating point values (e.g. ARM7). This code probably should
193254 ** too. */
193255 u64 i;
193256 if( eType==SQLITE_INTEGER ){
193257 i = (u64)sqlite3_value_int64(pValue);
193258 }else{
193259 double r;
193260 assert( sizeof(double)==8 && sizeof(u64)==8 );
193261 r = sqlite3_value_double(pValue);
193262 memcpy(&i, &r, 8);
193263 }
193264 sessionPutI64(&aBuf[1], i);
193265 }
193266 nByte = 9;
193267 break;
193268
193269 default: {
193270 u8 *z;
193271 int n;
193272 int nVarint;
193273
193274 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
193275 if( eType==SQLITE_TEXT ){
193276 z = (u8 *)sqlite3_value_text(pValue);
193277 }else{
193278 z = (u8 *)sqlite3_value_blob(pValue);
193279 }
193280 n = sqlite3_value_bytes(pValue);
193281 if( z==0 && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
193282 nVarint = sessionVarintLen(n);
193283
193284 if( aBuf ){
193285 sessionVarintPut(&aBuf[1], n);
193286 if( n ) memcpy(&aBuf[nVarint + 1], z, n);
193287 }
193288
193289 nByte = 1 + nVarint + n;
193290 break;
193291 }
193292 }
193293 }else{
193294 nByte = 1;
193295 if( aBuf ) aBuf[0] = '\0';
193296 }
193297
193298 if( pnWrite ) *pnWrite += nByte;
193299 return SQLITE_OK;
193300}
193301
193302
193303/*
193304** This macro is used to calculate hash key values for data structures. In
193305** order to use this macro, the entire data structure must be represented
193306** as a series of unsigned integers. In order to calculate a hash-key value
193307** for a data structure represented as three such integers, the macro may
193308** then be used as follows:
193309**
193310** int hash_key_value;
193311** hash_key_value = HASH_APPEND(0, <value 1>);
193312** hash_key_value = HASH_APPEND(hash_key_value, <value 2>);
193313** hash_key_value = HASH_APPEND(hash_key_value, <value 3>);
193314**
193315** In practice, the data structures this macro is used for are the primary
193316** key values of modified rows.
193317*/
193318#define HASH_APPEND(hash, add) ((hash) << 3) ^ (hash) ^ (unsigned int)(add)
193319
193320/*
193321** Append the hash of the 64-bit integer passed as the second argument to the
193322** hash-key value passed as the first. Return the new hash-key value.
193323*/
193324static unsigned int sessionHashAppendI64(unsigned int h, i64 i){
193325 h = HASH_APPEND(h, i & 0xFFFFFFFF);
193326 return HASH_APPEND(h, (i>>32)&0xFFFFFFFF);
193327}
193328
193329/*
193330** Append the hash of the blob passed via the second and third arguments to
193331** the hash-key value passed as the first. Return the new hash-key value.
193332*/
193333static unsigned int sessionHashAppendBlob(unsigned int h, int n, const u8 *z){
193334 int i;
193335 for(i=0; i<n; i++) h = HASH_APPEND(h, z[i]);
193336 return h;
193337}
193338
193339/*
193340** Append the hash of the data type passed as the second argument to the
193341** hash-key value passed as the first. Return the new hash-key value.
193342*/
193343static unsigned int sessionHashAppendType(unsigned int h, int eType){
193344 return HASH_APPEND(h, eType);
193345}
193346
193347/*
193348** This function may only be called from within a pre-update callback.
193349** It calculates a hash based on the primary key values of the old.* or
193350** new.* row currently available and, assuming no error occurs, writes it to
193351** *piHash before returning. If the primary key contains one or more NULL
193352** values, *pbNullPK is set to true before returning.
193353**
193354** If an error occurs, an SQLite error code is returned and the final values
193355** of *piHash asn *pbNullPK are undefined. Otherwise, SQLITE_OK is returned
193356** and the output variables are set as described above.
193357*/
193358static int sessionPreupdateHash(
193359 sqlite3_session *pSession, /* Session object that owns pTab */
193360 SessionTable *pTab, /* Session table handle */
193361 int bNew, /* True to hash the new.* PK */
193362 int *piHash, /* OUT: Hash value */
193363 int *pbNullPK /* OUT: True if there are NULL values in PK */
193364){
193365 unsigned int h = 0; /* Hash value to return */
193366 int i; /* Used to iterate through columns */
193367
193368 assert( *pbNullPK==0 );
193369 assert( pTab->nCol==pSession->hook.xCount(pSession->hook.pCtx) );
193370 for(i=0; i<pTab->nCol; i++){
193371 if( pTab->abPK[i] ){
193372 int rc;
193373 int eType;
193374 sqlite3_value *pVal;
193375
193376 if( bNew ){
193377 rc = pSession->hook.xNew(pSession->hook.pCtx, i, &pVal);
193378 }else{
193379 rc = pSession->hook.xOld(pSession->hook.pCtx, i, &pVal);
193380 }
193381 if( rc!=SQLITE_OK ) return rc;
193382
193383 eType = sqlite3_value_type(pVal);
193384 h = sessionHashAppendType(h, eType);
193385 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
193386 i64 iVal;
193387 if( eType==SQLITE_INTEGER ){
193388 iVal = sqlite3_value_int64(pVal);
193389 }else{
193390 double rVal = sqlite3_value_double(pVal);
193391 assert( sizeof(iVal)==8 && sizeof(rVal)==8 );
193392 memcpy(&iVal, &rVal, 8);
193393 }
193394 h = sessionHashAppendI64(h, iVal);
193395 }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
193396 const u8 *z;
193397 int n;
193398 if( eType==SQLITE_TEXT ){
193399 z = (const u8 *)sqlite3_value_text(pVal);
193400 }else{
193401 z = (const u8 *)sqlite3_value_blob(pVal);
193402 }
193403 n = sqlite3_value_bytes(pVal);
193404 if( !z && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
193405 h = sessionHashAppendBlob(h, n, z);
193406 }else{
193407 assert( eType==SQLITE_NULL );
193408 assert( pTab->bStat1==0 || i!=1 );
193409 *pbNullPK = 1;
193410 }
193411 }
193412 }
193413
193414 *piHash = (h % pTab->nChange);
193415 return SQLITE_OK;
193416}
193417
193418/*
193419** The buffer that the argument points to contains a serialized SQL value.
193420** Return the number of bytes of space occupied by the value (including
193421** the type byte).
193422*/
193423static int sessionSerialLen(u8 *a){
193424 int e = *a;
193425 int n;
193426 if( e==0 || e==0xFF ) return 1;
193427 if( e==SQLITE_NULL ) return 1;
193428 if( e==SQLITE_INTEGER || e==SQLITE_FLOAT ) return 9;
193429 return sessionVarintGet(&a[1], &n) + 1 + n;
193430}
193431
193432/*
193433** Based on the primary key values stored in change aRecord, calculate a
193434** hash key. Assume the has table has nBucket buckets. The hash keys
193435** calculated by this function are compatible with those calculated by
193436** sessionPreupdateHash().
193437**
193438** The bPkOnly argument is non-zero if the record at aRecord[] is from
193439** a patchset DELETE. In this case the non-PK fields are omitted entirely.
193440*/
193441static unsigned int sessionChangeHash(
193442 SessionTable *pTab, /* Table handle */
193443 int bPkOnly, /* Record consists of PK fields only */
193444 u8 *aRecord, /* Change record */
193445 int nBucket /* Assume this many buckets in hash table */
193446){
193447 unsigned int h = 0; /* Value to return */
193448 int i; /* Used to iterate through columns */
193449 u8 *a = aRecord; /* Used to iterate through change record */
193450
193451 for(i=0; i<pTab->nCol; i++){
193452 int eType = *a;
193453 int isPK = pTab->abPK[i];
193454 if( bPkOnly && isPK==0 ) continue;
193455
193456 /* It is not possible for eType to be SQLITE_NULL here. The session
193457 ** module does not record changes for rows with NULL values stored in
193458 ** primary key columns. */
193459 assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
193460 || eType==SQLITE_TEXT || eType==SQLITE_BLOB
193461 || eType==SQLITE_NULL || eType==0
193462 );
193463 assert( !isPK || (eType!=0 && eType!=SQLITE_NULL) );
193464
193465 if( isPK ){
193466 a++;
193467 h = sessionHashAppendType(h, eType);
193468 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
193469 h = sessionHashAppendI64(h, sessionGetI64(a));
193470 a += 8;
193471 }else{
193472 int n;
193473 a += sessionVarintGet(a, &n);
193474 h = sessionHashAppendBlob(h, n, a);
193475 a += n;
193476 }
193477 }else{
193478 a += sessionSerialLen(a);
193479 }
193480 }
193481 return (h % nBucket);
193482}
193483
193484/*
193485** Arguments aLeft and aRight are pointers to change records for table pTab.
193486** This function returns true if the two records apply to the same row (i.e.
193487** have the same values stored in the primary key columns), or false
193488** otherwise.
193489*/
193490static int sessionChangeEqual(
193491 SessionTable *pTab, /* Table used for PK definition */
193492 int bLeftPkOnly, /* True if aLeft[] contains PK fields only */
193493 u8 *aLeft, /* Change record */
193494 int bRightPkOnly, /* True if aRight[] contains PK fields only */
193495 u8 *aRight /* Change record */
193496){
193497 u8 *a1 = aLeft; /* Cursor to iterate through aLeft */
193498 u8 *a2 = aRight; /* Cursor to iterate through aRight */
193499 int iCol; /* Used to iterate through table columns */
193500
193501 for(iCol=0; iCol<pTab->nCol; iCol++){
193502 if( pTab->abPK[iCol] ){
193503 int n1 = sessionSerialLen(a1);
193504 int n2 = sessionSerialLen(a2);
193505
193506 if( n1!=n2 || memcmp(a1, a2, n1) ){
193507 return 0;
193508 }
193509 a1 += n1;
193510 a2 += n2;
193511 }else{
193512 if( bLeftPkOnly==0 ) a1 += sessionSerialLen(a1);
193513 if( bRightPkOnly==0 ) a2 += sessionSerialLen(a2);
193514 }
193515 }
193516
193517 return 1;
193518}
193519
193520/*
193521** Arguments aLeft and aRight both point to buffers containing change
193522** records with nCol columns. This function "merges" the two records into
193523** a single records which is written to the buffer at *paOut. *paOut is
193524** then set to point to one byte after the last byte written before
193525** returning.
193526**
193527** The merging of records is done as follows: For each column, if the
193528** aRight record contains a value for the column, copy the value from
193529** their. Otherwise, if aLeft contains a value, copy it. If neither
193530** record contains a value for a given column, then neither does the
193531** output record.
193532*/
193533static void sessionMergeRecord(
193534 u8 **paOut,
193535 int nCol,
193536 u8 *aLeft,
193537 u8 *aRight
193538){
193539 u8 *a1 = aLeft; /* Cursor used to iterate through aLeft */
193540 u8 *a2 = aRight; /* Cursor used to iterate through aRight */
193541 u8 *aOut = *paOut; /* Output cursor */
193542 int iCol; /* Used to iterate from 0 to nCol */
193543
193544 for(iCol=0; iCol<nCol; iCol++){
193545 int n1 = sessionSerialLen(a1);
193546 int n2 = sessionSerialLen(a2);
193547 if( *a2 ){
193548 memcpy(aOut, a2, n2);
193549 aOut += n2;
193550 }else{
193551 memcpy(aOut, a1, n1);
193552 aOut += n1;
193553 }
193554 a1 += n1;
193555 a2 += n2;
193556 }
193557
193558 *paOut = aOut;
193559}
193560
193561/*
193562** This is a helper function used by sessionMergeUpdate().
193563**
193564** When this function is called, both *paOne and *paTwo point to a value
193565** within a change record. Before it returns, both have been advanced so
193566** as to point to the next value in the record.
193567**
193568** If, when this function is called, *paTwo points to a valid value (i.e.
193569** *paTwo[0] is not 0x00 - the "no value" placeholder), a copy of the *paTwo
193570** pointer is returned and *pnVal is set to the number of bytes in the
193571** serialized value. Otherwise, a copy of *paOne is returned and *pnVal
193572** set to the number of bytes in the value at *paOne. If *paOne points
193573** to the "no value" placeholder, *pnVal is set to 1. In other words:
193574**
193575** if( *paTwo is valid ) return *paTwo;
193576** return *paOne;
193577**
193578*/
193579static u8 *sessionMergeValue(
193580 u8 **paOne, /* IN/OUT: Left-hand buffer pointer */
193581 u8 **paTwo, /* IN/OUT: Right-hand buffer pointer */
193582 int *pnVal /* OUT: Bytes in returned value */
193583){
193584 u8 *a1 = *paOne;
193585 u8 *a2 = *paTwo;
193586 u8 *pRet = 0;
193587 int n1;
193588
193589 assert( a1 );
193590 if( a2 ){
193591 int n2 = sessionSerialLen(a2);
193592 if( *a2 ){
193593 *pnVal = n2;
193594 pRet = a2;
193595 }
193596 *paTwo = &a2[n2];
193597 }
193598
193599 n1 = sessionSerialLen(a1);
193600 if( pRet==0 ){
193601 *pnVal = n1;
193602 pRet = a1;
193603 }
193604 *paOne = &a1[n1];
193605
193606 return pRet;
193607}
193608
193609/*
193610** This function is used by changeset_concat() to merge two UPDATE changes
193611** on the same row.
193612*/
193613static int sessionMergeUpdate(
193614 u8 **paOut, /* IN/OUT: Pointer to output buffer */
193615 SessionTable *pTab, /* Table change pertains to */
193616 int bPatchset, /* True if records are patchset records */
193617 u8 *aOldRecord1, /* old.* record for first change */
193618 u8 *aOldRecord2, /* old.* record for second change */
193619 u8 *aNewRecord1, /* new.* record for first change */
193620 u8 *aNewRecord2 /* new.* record for second change */
193621){
193622 u8 *aOld1 = aOldRecord1;
193623 u8 *aOld2 = aOldRecord2;
193624 u8 *aNew1 = aNewRecord1;
193625 u8 *aNew2 = aNewRecord2;
193626
193627 u8 *aOut = *paOut;
193628 int i;
193629
193630 if( bPatchset==0 ){
193631 int bRequired = 0;
193632
193633 assert( aOldRecord1 && aNewRecord1 );
193634
193635 /* Write the old.* vector first. */
193636 for(i=0; i<pTab->nCol; i++){
193637 int nOld;
193638 u8 *aOld;
193639 int nNew;
193640 u8 *aNew;
193641
193642 aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
193643 aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
193644 if( pTab->abPK[i] || nOld!=nNew || memcmp(aOld, aNew, nNew) ){
193645 if( pTab->abPK[i]==0 ) bRequired = 1;
193646 memcpy(aOut, aOld, nOld);
193647 aOut += nOld;
193648 }else{
193649 *(aOut++) = '\0';
193650 }
193651 }
193652
193653 if( !bRequired ) return 0;
193654 }
193655
193656 /* Write the new.* vector */
193657 aOld1 = aOldRecord1;
193658 aOld2 = aOldRecord2;
193659 aNew1 = aNewRecord1;
193660 aNew2 = aNewRecord2;
193661 for(i=0; i<pTab->nCol; i++){
193662 int nOld;
193663 u8 *aOld;
193664 int nNew;
193665 u8 *aNew;
193666
193667 aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
193668 aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
193669 if( bPatchset==0
193670 && (pTab->abPK[i] || (nOld==nNew && 0==memcmp(aOld, aNew, nNew)))
193671 ){
193672 *(aOut++) = '\0';
193673 }else{
193674 memcpy(aOut, aNew, nNew);
193675 aOut += nNew;
193676 }
193677 }
193678
193679 *paOut = aOut;
193680 return 1;
193681}
193682
193683/*
193684** This function is only called from within a pre-update-hook callback.
193685** It determines if the current pre-update-hook change affects the same row
193686** as the change stored in argument pChange. If so, it returns true. Otherwise
193687** if the pre-update-hook does not affect the same row as pChange, it returns
193688** false.
193689*/
193690static int sessionPreupdateEqual(
193691 sqlite3_session *pSession, /* Session object that owns SessionTable */
193692 SessionTable *pTab, /* Table associated with change */
193693 SessionChange *pChange, /* Change to compare to */
193694 int op /* Current pre-update operation */
193695){
193696 int iCol; /* Used to iterate through columns */
193697 u8 *a = pChange->aRecord; /* Cursor used to scan change record */
193698
193699 assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE );
193700 for(iCol=0; iCol<pTab->nCol; iCol++){
193701 if( !pTab->abPK[iCol] ){
193702 a += sessionSerialLen(a);
193703 }else{
193704 sqlite3_value *pVal; /* Value returned by preupdate_new/old */
193705 int rc; /* Error code from preupdate_new/old */
193706 int eType = *a++; /* Type of value from change record */
193707
193708 /* The following calls to preupdate_new() and preupdate_old() can not
193709 ** fail. This is because they cache their return values, and by the
193710 ** time control flows to here they have already been called once from
193711 ** within sessionPreupdateHash(). The first two asserts below verify
193712 ** this (that the method has already been called). */
193713 if( op==SQLITE_INSERT ){
193714 /* assert( db->pPreUpdate->pNewUnpacked || db->pPreUpdate->aNew ); */
193715 rc = pSession->hook.xNew(pSession->hook.pCtx, iCol, &pVal);
193716 }else{
193717 /* assert( db->pPreUpdate->pUnpacked ); */
193718 rc = pSession->hook.xOld(pSession->hook.pCtx, iCol, &pVal);
193719 }
193720 assert( rc==SQLITE_OK );
193721 if( sqlite3_value_type(pVal)!=eType ) return 0;
193722
193723 /* A SessionChange object never has a NULL value in a PK column */
193724 assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
193725 || eType==SQLITE_BLOB || eType==SQLITE_TEXT
193726 );
193727
193728 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
193729 i64 iVal = sessionGetI64(a);
193730 a += 8;
193731 if( eType==SQLITE_INTEGER ){
193732 if( sqlite3_value_int64(pVal)!=iVal ) return 0;
193733 }else{
193734 double rVal;
193735 assert( sizeof(iVal)==8 && sizeof(rVal)==8 );
193736 memcpy(&rVal, &iVal, 8);
193737 if( sqlite3_value_double(pVal)!=rVal ) return 0;
193738 }
193739 }else{
193740 int n;
193741 const u8 *z;
193742 a += sessionVarintGet(a, &n);
193743 if( sqlite3_value_bytes(pVal)!=n ) return 0;
193744 if( eType==SQLITE_TEXT ){
193745 z = sqlite3_value_text(pVal);
193746 }else{
193747 z = sqlite3_value_blob(pVal);
193748 }
193749 if( n>0 && memcmp(a, z, n) ) return 0;
193750 a += n;
193751 }
193752 }
193753 }
193754
193755 return 1;
193756}
193757
193758/*
193759** If required, grow the hash table used to store changes on table pTab
193760** (part of the session pSession). If a fatal OOM error occurs, set the
193761** session object to failed and return SQLITE_ERROR. Otherwise, return
193762** SQLITE_OK.
193763**
193764** It is possible that a non-fatal OOM error occurs in this function. In
193765** that case the hash-table does not grow, but SQLITE_OK is returned anyway.
193766** Growing the hash table in this case is a performance optimization only,
193767** it is not required for correct operation.
193768*/
193769static int sessionGrowHash(int bPatchset, SessionTable *pTab){
193770 if( pTab->nChange==0 || pTab->nEntry>=(pTab->nChange/2) ){
193771 int i;
193772 SessionChange **apNew;
193773 int nNew = (pTab->nChange ? pTab->nChange : 128) * 2;
193774
193775 apNew = (SessionChange **)sqlite3_malloc(sizeof(SessionChange *) * nNew);
193776 if( apNew==0 ){
193777 if( pTab->nChange==0 ){
193778 return SQLITE_ERROR;
193779 }
193780 return SQLITE_OK;
193781 }
193782 memset(apNew, 0, sizeof(SessionChange *) * nNew);
193783
193784 for(i=0; i<pTab->nChange; i++){
193785 SessionChange *p;
193786 SessionChange *pNext;
193787 for(p=pTab->apChange[i]; p; p=pNext){
193788 int bPkOnly = (p->op==SQLITE_DELETE && bPatchset);
193789 int iHash = sessionChangeHash(pTab, bPkOnly, p->aRecord, nNew);
193790 pNext = p->pNext;
193791 p->pNext = apNew[iHash];
193792 apNew[iHash] = p;
193793 }
193794 }
193795
193796 sqlite3_free(pTab->apChange);
193797 pTab->nChange = nNew;
193798 pTab->apChange = apNew;
193799 }
193800
193801 return SQLITE_OK;
193802}
193803
193804/*
193805** This function queries the database for the names of the columns of table
193806** zThis, in schema zDb.
193807**
193808** Otherwise, if they are not NULL, variable *pnCol is set to the number
193809** of columns in the database table and variable *pzTab is set to point to a
193810** nul-terminated copy of the table name. *pazCol (if not NULL) is set to
193811** point to an array of pointers to column names. And *pabPK (again, if not
193812** NULL) is set to point to an array of booleans - true if the corresponding
193813** column is part of the primary key.
193814**
193815** For example, if the table is declared as:
193816**
193817** CREATE TABLE tbl1(w, x, y, z, PRIMARY KEY(w, z));
193818**
193819** Then the four output variables are populated as follows:
193820**
193821** *pnCol = 4
193822** *pzTab = "tbl1"
193823** *pazCol = {"w", "x", "y", "z"}
193824** *pabPK = {1, 0, 0, 1}
193825**
193826** All returned buffers are part of the same single allocation, which must
193827** be freed using sqlite3_free() by the caller
193828*/
193829static int sessionTableInfo(
193830 sqlite3 *db, /* Database connection */
193831 const char *zDb, /* Name of attached database (e.g. "main") */
193832 const char *zThis, /* Table name */
193833 int *pnCol, /* OUT: number of columns */
193834 const char **pzTab, /* OUT: Copy of zThis */
193835 const char ***pazCol, /* OUT: Array of column names for table */
193836 u8 **pabPK /* OUT: Array of booleans - true for PK col */
193837){
193838 char *zPragma;
193839 sqlite3_stmt *pStmt;
193840 int rc;
193841 int nByte;
193842 int nDbCol = 0;
193843 int nThis;
193844 int i;
193845 u8 *pAlloc = 0;
193846 char **azCol = 0;
193847 u8 *abPK = 0;
193848
193849 assert( pazCol && pabPK );
193850
193851 nThis = sqlite3Strlen30(zThis);
193852 if( nThis==12 && 0==sqlite3_stricmp("sqlite_stat1", zThis) ){
193853 rc = sqlite3_table_column_metadata(db, zDb, zThis, 0, 0, 0, 0, 0, 0);
193854 if( rc==SQLITE_OK ){
193855 /* For sqlite_stat1, pretend that (tbl,idx) is the PRIMARY KEY. */
193856 zPragma = sqlite3_mprintf(
193857 "SELECT 0, 'tbl', '', 0, '', 1 UNION ALL "
193858 "SELECT 1, 'idx', '', 0, '', 2 UNION ALL "
193859 "SELECT 2, 'stat', '', 0, '', 0"
193860 );
193861 }else if( rc==SQLITE_ERROR ){
193862 zPragma = sqlite3_mprintf("");
193863 }else{
193864 return rc;
193865 }
193866 }else{
193867 zPragma = sqlite3_mprintf("PRAGMA '%q'.table_info('%q')", zDb, zThis);
193868 }
193869 if( !zPragma ) return SQLITE_NOMEM;
193870
193871 rc = sqlite3_prepare_v2(db, zPragma, -1, &pStmt, 0);
193872 sqlite3_free(zPragma);
193873 if( rc!=SQLITE_OK ) return rc;
193874
193875 nByte = nThis + 1;
193876 while( SQLITE_ROW==sqlite3_step(pStmt) ){
193877 nByte += sqlite3_column_bytes(pStmt, 1);
193878 nDbCol++;
193879 }
193880 rc = sqlite3_reset(pStmt);
193881
193882 if( rc==SQLITE_OK ){
193883 nByte += nDbCol * (sizeof(const char *) + sizeof(u8) + 1);
193884 pAlloc = sqlite3_malloc(nByte);
193885 if( pAlloc==0 ){
193886 rc = SQLITE_NOMEM;
193887 }
193888 }
193889 if( rc==SQLITE_OK ){
193890 azCol = (char **)pAlloc;
193891 pAlloc = (u8 *)&azCol[nDbCol];
193892 abPK = (u8 *)pAlloc;
193893 pAlloc = &abPK[nDbCol];
193894 if( pzTab ){
193895 memcpy(pAlloc, zThis, nThis+1);
193896 *pzTab = (char *)pAlloc;
193897 pAlloc += nThis+1;
193898 }
193899
193900 i = 0;
193901 while( SQLITE_ROW==sqlite3_step(pStmt) ){
193902 int nName = sqlite3_column_bytes(pStmt, 1);
193903 const unsigned char *zName = sqlite3_column_text(pStmt, 1);
193904 if( zName==0 ) break;
193905 memcpy(pAlloc, zName, nName+1);
193906 azCol[i] = (char *)pAlloc;
193907 pAlloc += nName+1;
193908 abPK[i] = sqlite3_column_int(pStmt, 5);
193909 i++;
193910 }
193911 rc = sqlite3_reset(pStmt);
193912
193913 }
193914
193915 /* If successful, populate the output variables. Otherwise, zero them and
193916 ** free any allocation made. An error code will be returned in this case.
193917 */
193918 if( rc==SQLITE_OK ){
193919 *pazCol = (const char **)azCol;
193920 *pabPK = abPK;
193921 *pnCol = nDbCol;
193922 }else{
193923 *pazCol = 0;
193924 *pabPK = 0;
193925 *pnCol = 0;
193926 if( pzTab ) *pzTab = 0;
193927 sqlite3_free(azCol);
193928 }
193929 sqlite3_finalize(pStmt);
193930 return rc;
193931}
193932
193933/*
193934** This function is only called from within a pre-update handler for a
193935** write to table pTab, part of session pSession. If this is the first
193936** write to this table, initalize the SessionTable.nCol, azCol[] and
193937** abPK[] arrays accordingly.
193938**
193939** If an error occurs, an error code is stored in sqlite3_session.rc and
193940** non-zero returned. Or, if no error occurs but the table has no primary
193941** key, sqlite3_session.rc is left set to SQLITE_OK and non-zero returned to
193942** indicate that updates on this table should be ignored. SessionTable.abPK
193943** is set to NULL in this case.
193944*/
193945static int sessionInitTable(sqlite3_session *pSession, SessionTable *pTab){
193946 if( pTab->nCol==0 ){
193947 u8 *abPK;
193948 assert( pTab->azCol==0 || pTab->abPK==0 );
193949 pSession->rc = sessionTableInfo(pSession->db, pSession->zDb,
193950 pTab->zName, &pTab->nCol, 0, &pTab->azCol, &abPK
193951 );
193952 if( pSession->rc==SQLITE_OK ){
193953 int i;
193954 for(i=0; i<pTab->nCol; i++){
193955 if( abPK[i] ){
193956 pTab->abPK = abPK;
193957 break;
193958 }
193959 }
193960 if( 0==sqlite3_stricmp("sqlite_stat1", pTab->zName) ){
193961 pTab->bStat1 = 1;
193962 }
193963 }
193964 }
193965 return (pSession->rc || pTab->abPK==0);
193966}
193967
193968/*
193969** Versions of the four methods in object SessionHook for use with the
193970** sqlite_stat1 table. The purpose of this is to substitute a zero-length
193971** blob each time a NULL value is read from the "idx" column of the
193972** sqlite_stat1 table.
193973*/
193974typedef struct SessionStat1Ctx SessionStat1Ctx;
193975struct SessionStat1Ctx {
193976 SessionHook hook;
193977 sqlite3_session *pSession;
193978};
193979static int sessionStat1Old(void *pCtx, int iCol, sqlite3_value **ppVal){
193980 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
193981 sqlite3_value *pVal = 0;
193982 int rc = p->hook.xOld(p->hook.pCtx, iCol, &pVal);
193983 if( rc==SQLITE_OK && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL ){
193984 pVal = p->pSession->pZeroBlob;
193985 }
193986 *ppVal = pVal;
193987 return rc;
193988}
193989static int sessionStat1New(void *pCtx, int iCol, sqlite3_value **ppVal){
193990 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
193991 sqlite3_value *pVal = 0;
193992 int rc = p->hook.xNew(p->hook.pCtx, iCol, &pVal);
193993 if( rc==SQLITE_OK && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL ){
193994 pVal = p->pSession->pZeroBlob;
193995 }
193996 *ppVal = pVal;
193997 return rc;
193998}
193999static int sessionStat1Count(void *pCtx){
194000 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
194001 return p->hook.xCount(p->hook.pCtx);
194002}
194003static int sessionStat1Depth(void *pCtx){
194004 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
194005 return p->hook.xDepth(p->hook.pCtx);
194006}
194007
194008
194009/*
194010** This function is only called from with a pre-update-hook reporting a
194011** change on table pTab (attached to session pSession). The type of change
194012** (UPDATE, INSERT, DELETE) is specified by the first argument.
194013**
194014** Unless one is already present or an error occurs, an entry is added
194015** to the changed-rows hash table associated with table pTab.
194016*/
194017static void sessionPreupdateOneChange(
194018 int op, /* One of SQLITE_UPDATE, INSERT, DELETE */
194019 sqlite3_session *pSession, /* Session object pTab is attached to */
194020 SessionTable *pTab /* Table that change applies to */
194021){
194022 int iHash;
194023 int bNull = 0;
194024 int rc = SQLITE_OK;
194025 SessionStat1Ctx stat1 = {0};
194026
194027 if( pSession->rc ) return;
194028
194029 /* Load table details if required */
194030 if( sessionInitTable(pSession, pTab) ) return;
194031
194032 /* Check the number of columns in this xPreUpdate call matches the
194033 ** number of columns in the table. */
194034 if( pTab->nCol!=pSession->hook.xCount(pSession->hook.pCtx) ){
194035 pSession->rc = SQLITE_SCHEMA;
194036 return;
194037 }
194038
194039 /* Grow the hash table if required */
194040 if( sessionGrowHash(0, pTab) ){
194041 pSession->rc = SQLITE_NOMEM;
194042 return;
194043 }
194044
194045 if( pTab->bStat1 ){
194046 stat1.hook = pSession->hook;
194047 stat1.pSession = pSession;
194048 pSession->hook.pCtx = (void*)&stat1;
194049 pSession->hook.xNew = sessionStat1New;
194050 pSession->hook.xOld = sessionStat1Old;
194051 pSession->hook.xCount = sessionStat1Count;
194052 pSession->hook.xDepth = sessionStat1Depth;
194053 if( pSession->pZeroBlob==0 ){
194054 sqlite3_value *p = sqlite3ValueNew(0);
194055 if( p==0 ){
194056 rc = SQLITE_NOMEM;
194057 goto error_out;
194058 }
194059 sqlite3ValueSetStr(p, 0, "", 0, SQLITE_STATIC);
194060 pSession->pZeroBlob = p;
194061 }
194062 }
194063
194064 /* Calculate the hash-key for this change. If the primary key of the row
194065 ** includes a NULL value, exit early. Such changes are ignored by the
194066 ** session module. */
194067 rc = sessionPreupdateHash(pSession, pTab, op==SQLITE_INSERT, &iHash, &bNull);
194068 if( rc!=SQLITE_OK ) goto error_out;
194069
194070 if( bNull==0 ){
194071 /* Search the hash table for an existing record for this row. */
194072 SessionChange *pC;
194073 for(pC=pTab->apChange[iHash]; pC; pC=pC->pNext){
194074 if( sessionPreupdateEqual(pSession, pTab, pC, op) ) break;
194075 }
194076
194077 if( pC==0 ){
194078 /* Create a new change object containing all the old values (if
194079 ** this is an SQLITE_UPDATE or SQLITE_DELETE), or just the PK
194080 ** values (if this is an INSERT). */
194081 SessionChange *pChange; /* New change object */
194082 int nByte; /* Number of bytes to allocate */
194083 int i; /* Used to iterate through columns */
194084
194085 assert( rc==SQLITE_OK );
194086 pTab->nEntry++;
194087
194088 /* Figure out how large an allocation is required */
194089 nByte = sizeof(SessionChange);
194090 for(i=0; i<pTab->nCol; i++){
194091 sqlite3_value *p = 0;
194092 if( op!=SQLITE_INSERT ){
194093 TESTONLY(int trc = ) pSession->hook.xOld(pSession->hook.pCtx, i, &p);
194094 assert( trc==SQLITE_OK );
194095 }else if( pTab->abPK[i] ){
194096 TESTONLY(int trc = ) pSession->hook.xNew(pSession->hook.pCtx, i, &p);
194097 assert( trc==SQLITE_OK );
194098 }
194099
194100 /* This may fail if SQLite value p contains a utf-16 string that must
194101 ** be converted to utf-8 and an OOM error occurs while doing so. */
194102 rc = sessionSerializeValue(0, p, &nByte);
194103 if( rc!=SQLITE_OK ) goto error_out;
194104 }
194105
194106 /* Allocate the change object */
194107 pChange = (SessionChange *)sqlite3_malloc(nByte);
194108 if( !pChange ){
194109 rc = SQLITE_NOMEM;
194110 goto error_out;
194111 }else{
194112 memset(pChange, 0, sizeof(SessionChange));
194113 pChange->aRecord = (u8 *)&pChange[1];
194114 }
194115
194116 /* Populate the change object. None of the preupdate_old(),
194117 ** preupdate_new() or SerializeValue() calls below may fail as all
194118 ** required values and encodings have already been cached in memory.
194119 ** It is not possible for an OOM to occur in this block. */
194120 nByte = 0;
194121 for(i=0; i<pTab->nCol; i++){
194122 sqlite3_value *p = 0;
194123 if( op!=SQLITE_INSERT ){
194124 pSession->hook.xOld(pSession->hook.pCtx, i, &p);
194125 }else if( pTab->abPK[i] ){
194126 pSession->hook.xNew(pSession->hook.pCtx, i, &p);
194127 }
194128 sessionSerializeValue(&pChange->aRecord[nByte], p, &nByte);
194129 }
194130
194131 /* Add the change to the hash-table */
194132 if( pSession->bIndirect || pSession->hook.xDepth(pSession->hook.pCtx) ){
194133 pChange->bIndirect = 1;
194134 }
194135 pChange->nRecord = nByte;
194136 pChange->op = op;
194137 pChange->pNext = pTab->apChange[iHash];
194138 pTab->apChange[iHash] = pChange;
194139
194140 }else if( pC->bIndirect ){
194141 /* If the existing change is considered "indirect", but this current
194142 ** change is "direct", mark the change object as direct. */
194143 if( pSession->hook.xDepth(pSession->hook.pCtx)==0
194144 && pSession->bIndirect==0
194145 ){
194146 pC->bIndirect = 0;
194147 }
194148 }
194149 }
194150
194151 /* If an error has occurred, mark the session object as failed. */
194152 error_out:
194153 if( pTab->bStat1 ){
194154 pSession->hook = stat1.hook;
194155 }
194156 if( rc!=SQLITE_OK ){
194157 pSession->rc = rc;
194158 }
194159}
194160
194161static int sessionFindTable(
194162 sqlite3_session *pSession,
194163 const char *zName,
194164 SessionTable **ppTab
194165){
194166 int rc = SQLITE_OK;
194167 int nName = sqlite3Strlen30(zName);
194168 SessionTable *pRet;
194169
194170 /* Search for an existing table */
194171 for(pRet=pSession->pTable; pRet; pRet=pRet->pNext){
194172 if( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) ) break;
194173 }
194174
194175 if( pRet==0 && pSession->bAutoAttach ){
194176 /* If there is a table-filter configured, invoke it. If it returns 0,
194177 ** do not automatically add the new table. */
194178 if( pSession->xTableFilter==0
194179 || pSession->xTableFilter(pSession->pFilterCtx, zName)
194180 ){
194181 rc = sqlite3session_attach(pSession, zName);
194182 if( rc==SQLITE_OK ){
194183 for(pRet=pSession->pTable; pRet->pNext; pRet=pRet->pNext);
194184 assert( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) );
194185 }
194186 }
194187 }
194188
194189 assert( rc==SQLITE_OK || pRet==0 );
194190 *ppTab = pRet;
194191 return rc;
194192}
194193
194194/*
194195** The 'pre-update' hook registered by this module with SQLite databases.
194196*/
194197static void xPreUpdate(
194198 void *pCtx, /* Copy of third arg to preupdate_hook() */
194199 sqlite3 *db, /* Database handle */
194200 int op, /* SQLITE_UPDATE, DELETE or INSERT */
194201 char const *zDb, /* Database name */
194202 char const *zName, /* Table name */
194203 sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */
194204 sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */
194205){
194206 sqlite3_session *pSession;
194207 int nDb = sqlite3Strlen30(zDb);
194208
194209 assert( sqlite3_mutex_held(db->mutex) );
194210
194211 for(pSession=(sqlite3_session *)pCtx; pSession; pSession=pSession->pNext){
194212 SessionTable *pTab;
194213
194214 /* If this session is attached to a different database ("main", "temp"
194215 ** etc.), or if it is not currently enabled, there is nothing to do. Skip
194216 ** to the next session object attached to this database. */
194217 if( pSession->bEnable==0 ) continue;
194218 if( pSession->rc ) continue;
194219 if( sqlite3_strnicmp(zDb, pSession->zDb, nDb+1) ) continue;
194220
194221 pSession->rc = sessionFindTable(pSession, zName, &pTab);
194222 if( pTab ){
194223 assert( pSession->rc==SQLITE_OK );
194224 sessionPreupdateOneChange(op, pSession, pTab);
194225 if( op==SQLITE_UPDATE ){
194226 sessionPreupdateOneChange(SQLITE_INSERT, pSession, pTab);
194227 }
194228 }
194229 }
194230}
194231
194232/*
194233** The pre-update hook implementations.
194234*/
194235static int sessionPreupdateOld(void *pCtx, int iVal, sqlite3_value **ppVal){
194236 return sqlite3_preupdate_old((sqlite3*)pCtx, iVal, ppVal);
194237}
194238static int sessionPreupdateNew(void *pCtx, int iVal, sqlite3_value **ppVal){
194239 return sqlite3_preupdate_new((sqlite3*)pCtx, iVal, ppVal);
194240}
194241static int sessionPreupdateCount(void *pCtx){
194242 return sqlite3_preupdate_count((sqlite3*)pCtx);
194243}
194244static int sessionPreupdateDepth(void *pCtx){
194245 return sqlite3_preupdate_depth((sqlite3*)pCtx);
194246}
194247
194248/*
194249** Install the pre-update hooks on the session object passed as the only
194250** argument.
194251*/
194252static void sessionPreupdateHooks(
194253 sqlite3_session *pSession
194254){
194255 pSession->hook.pCtx = (void*)pSession->db;
194256 pSession->hook.xOld = sessionPreupdateOld;
194257 pSession->hook.xNew = sessionPreupdateNew;
194258 pSession->hook.xCount = sessionPreupdateCount;
194259 pSession->hook.xDepth = sessionPreupdateDepth;
194260}
194261
194262typedef struct SessionDiffCtx SessionDiffCtx;
194263struct SessionDiffCtx {
194264 sqlite3_stmt *pStmt;
194265 int nOldOff;
194266};
194267
194268/*
194269** The diff hook implementations.
194270*/
194271static int sessionDiffOld(void *pCtx, int iVal, sqlite3_value **ppVal){
194272 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
194273 *ppVal = sqlite3_column_value(p->pStmt, iVal+p->nOldOff);
194274 return SQLITE_OK;
194275}
194276static int sessionDiffNew(void *pCtx, int iVal, sqlite3_value **ppVal){
194277 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
194278 *ppVal = sqlite3_column_value(p->pStmt, iVal);
194279 return SQLITE_OK;
194280}
194281static int sessionDiffCount(void *pCtx){
194282 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
194283 return p->nOldOff ? p->nOldOff : sqlite3_column_count(p->pStmt);
194284}
194285static int sessionDiffDepth(void *pCtx){
194286 return 0;
194287}
194288
194289/*
194290** Install the diff hooks on the session object passed as the only
194291** argument.
194292*/
194293static void sessionDiffHooks(
194294 sqlite3_session *pSession,
194295 SessionDiffCtx *pDiffCtx
194296){
194297 pSession->hook.pCtx = (void*)pDiffCtx;
194298 pSession->hook.xOld = sessionDiffOld;
194299 pSession->hook.xNew = sessionDiffNew;
194300 pSession->hook.xCount = sessionDiffCount;
194301 pSession->hook.xDepth = sessionDiffDepth;
194302}
194303
194304static char *sessionExprComparePK(
194305 int nCol,
194306 const char *zDb1, const char *zDb2,
194307 const char *zTab,
194308 const char **azCol, u8 *abPK
194309){
194310 int i;
194311 const char *zSep = "";
194312 char *zRet = 0;
194313
194314 for(i=0; i<nCol; i++){
194315 if( abPK[i] ){
194316 zRet = sqlite3_mprintf("%z%s\"%w\".\"%w\".\"%w\"=\"%w\".\"%w\".\"%w\"",
194317 zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
194318 );
194319 zSep = " AND ";
194320 if( zRet==0 ) break;
194321 }
194322 }
194323
194324 return zRet;
194325}
194326
194327static char *sessionExprCompareOther(
194328 int nCol,
194329 const char *zDb1, const char *zDb2,
194330 const char *zTab,
194331 const char **azCol, u8 *abPK
194332){
194333 int i;
194334 const char *zSep = "";
194335 char *zRet = 0;
194336 int bHave = 0;
194337
194338 for(i=0; i<nCol; i++){
194339 if( abPK[i]==0 ){
194340 bHave = 1;
194341 zRet = sqlite3_mprintf(
194342 "%z%s\"%w\".\"%w\".\"%w\" IS NOT \"%w\".\"%w\".\"%w\"",
194343 zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
194344 );
194345 zSep = " OR ";
194346 if( zRet==0 ) break;
194347 }
194348 }
194349
194350 if( bHave==0 ){
194351 assert( zRet==0 );
194352 zRet = sqlite3_mprintf("0");
194353 }
194354
194355 return zRet;
194356}
194357
194358static char *sessionSelectFindNew(
194359 int nCol,
194360 const char *zDb1, /* Pick rows in this db only */
194361 const char *zDb2, /* But not in this one */
194362 const char *zTbl, /* Table name */
194363 const char *zExpr
194364){
194365 char *zRet = sqlite3_mprintf(
194366 "SELECT * FROM \"%w\".\"%w\" WHERE NOT EXISTS ("
194367 " SELECT 1 FROM \"%w\".\"%w\" WHERE %s"
194368 ")",
194369 zDb1, zTbl, zDb2, zTbl, zExpr
194370 );
194371 return zRet;
194372}
194373
194374static int sessionDiffFindNew(
194375 int op,
194376 sqlite3_session *pSession,
194377 SessionTable *pTab,
194378 const char *zDb1,
194379 const char *zDb2,
194380 char *zExpr
194381){
194382 int rc = SQLITE_OK;
194383 char *zStmt = sessionSelectFindNew(pTab->nCol, zDb1, zDb2, pTab->zName,zExpr);
194384
194385 if( zStmt==0 ){
194386 rc = SQLITE_NOMEM;
194387 }else{
194388 sqlite3_stmt *pStmt;
194389 rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
194390 if( rc==SQLITE_OK ){
194391 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
194392 pDiffCtx->pStmt = pStmt;
194393 pDiffCtx->nOldOff = 0;
194394 while( SQLITE_ROW==sqlite3_step(pStmt) ){
194395 sessionPreupdateOneChange(op, pSession, pTab);
194396 }
194397 rc = sqlite3_finalize(pStmt);
194398 }
194399 sqlite3_free(zStmt);
194400 }
194401
194402 return rc;
194403}
194404
194405static int sessionDiffFindModified(
194406 sqlite3_session *pSession,
194407 SessionTable *pTab,
194408 const char *zFrom,
194409 const char *zExpr
194410){
194411 int rc = SQLITE_OK;
194412
194413 char *zExpr2 = sessionExprCompareOther(pTab->nCol,
194414 pSession->zDb, zFrom, pTab->zName, pTab->azCol, pTab->abPK
194415 );
194416 if( zExpr2==0 ){
194417 rc = SQLITE_NOMEM;
194418 }else{
194419 char *zStmt = sqlite3_mprintf(
194420 "SELECT * FROM \"%w\".\"%w\", \"%w\".\"%w\" WHERE %s AND (%z)",
194421 pSession->zDb, pTab->zName, zFrom, pTab->zName, zExpr, zExpr2
194422 );
194423 if( zStmt==0 ){
194424 rc = SQLITE_NOMEM;
194425 }else{
194426 sqlite3_stmt *pStmt;
194427 rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
194428
194429 if( rc==SQLITE_OK ){
194430 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
194431 pDiffCtx->pStmt = pStmt;
194432 pDiffCtx->nOldOff = pTab->nCol;
194433 while( SQLITE_ROW==sqlite3_step(pStmt) ){
194434 sessionPreupdateOneChange(SQLITE_UPDATE, pSession, pTab);
194435 }
194436 rc = sqlite3_finalize(pStmt);
194437 }
194438 sqlite3_free(zStmt);
194439 }
194440 }
194441
194442 return rc;
194443}
194444
194445SQLITE_API int sqlite3session_diff(
194446 sqlite3_session *pSession,
194447 const char *zFrom,
194448 const char *zTbl,
194449 char **pzErrMsg
194450){
194451 const char *zDb = pSession->zDb;
194452 int rc = pSession->rc;
194453 SessionDiffCtx d;
194454
194455 memset(&d, 0, sizeof(d));
194456 sessionDiffHooks(pSession, &d);
194457
194458 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
194459 if( pzErrMsg ) *pzErrMsg = 0;
194460 if( rc==SQLITE_OK ){
194461 char *zExpr = 0;
194462 sqlite3 *db = pSession->db;
194463 SessionTable *pTo; /* Table zTbl */
194464
194465 /* Locate and if necessary initialize the target table object */
194466 rc = sessionFindTable(pSession, zTbl, &pTo);
194467 if( pTo==0 ) goto diff_out;
194468 if( sessionInitTable(pSession, pTo) ){
194469 rc = pSession->rc;
194470 goto diff_out;
194471 }
194472
194473 /* Check the table schemas match */
194474 if( rc==SQLITE_OK ){
194475 int bHasPk = 0;
194476 int bMismatch = 0;
194477 int nCol; /* Columns in zFrom.zTbl */
194478 u8 *abPK;
194479 const char **azCol = 0;
194480 rc = sessionTableInfo(db, zFrom, zTbl, &nCol, 0, &azCol, &abPK);
194481 if( rc==SQLITE_OK ){
194482 if( pTo->nCol!=nCol ){
194483 bMismatch = 1;
194484 }else{
194485 int i;
194486 for(i=0; i<nCol; i++){
194487 if( pTo->abPK[i]!=abPK[i] ) bMismatch = 1;
194488 if( sqlite3_stricmp(azCol[i], pTo->azCol[i]) ) bMismatch = 1;
194489 if( abPK[i] ) bHasPk = 1;
194490 }
194491 }
194492 }
194493 sqlite3_free((char*)azCol);
194494 if( bMismatch ){
194495 *pzErrMsg = sqlite3_mprintf("table schemas do not match");
194496 rc = SQLITE_SCHEMA;
194497 }
194498 if( bHasPk==0 ){
194499 /* Ignore tables with no primary keys */
194500 goto diff_out;
194501 }
194502 }
194503
194504 if( rc==SQLITE_OK ){
194505 zExpr = sessionExprComparePK(pTo->nCol,
194506 zDb, zFrom, pTo->zName, pTo->azCol, pTo->abPK
194507 );
194508 }
194509
194510 /* Find new rows */
194511 if( rc==SQLITE_OK ){
194512 rc = sessionDiffFindNew(SQLITE_INSERT, pSession, pTo, zDb, zFrom, zExpr);
194513 }
194514
194515 /* Find old rows */
194516 if( rc==SQLITE_OK ){
194517 rc = sessionDiffFindNew(SQLITE_DELETE, pSession, pTo, zFrom, zDb, zExpr);
194518 }
194519
194520 /* Find modified rows */
194521 if( rc==SQLITE_OK ){
194522 rc = sessionDiffFindModified(pSession, pTo, zFrom, zExpr);
194523 }
194524
194525 sqlite3_free(zExpr);
194526 }
194527
194528 diff_out:
194529 sessionPreupdateHooks(pSession);
194530 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
194531 return rc;
194532}
194533
194534/*
194535** Create a session object. This session object will record changes to
194536** database zDb attached to connection db.
194537*/
194538SQLITE_API int sqlite3session_create(
194539 sqlite3 *db, /* Database handle */
194540 const char *zDb, /* Name of db (e.g. "main") */
194541 sqlite3_session **ppSession /* OUT: New session object */
194542){
194543 sqlite3_session *pNew; /* Newly allocated session object */
194544 sqlite3_session *pOld; /* Session object already attached to db */
194545 int nDb = sqlite3Strlen30(zDb); /* Length of zDb in bytes */
194546
194547 /* Zero the output value in case an error occurs. */
194548 *ppSession = 0;
194549
194550 /* Allocate and populate the new session object. */
194551 pNew = (sqlite3_session *)sqlite3_malloc(sizeof(sqlite3_session) + nDb + 1);
194552 if( !pNew ) return SQLITE_NOMEM;
194553 memset(pNew, 0, sizeof(sqlite3_session));
194554 pNew->db = db;
194555 pNew->zDb = (char *)&pNew[1];
194556 pNew->bEnable = 1;
194557 memcpy(pNew->zDb, zDb, nDb+1);
194558 sessionPreupdateHooks(pNew);
194559
194560 /* Add the new session object to the linked list of session objects
194561 ** attached to database handle $db. Do this under the cover of the db
194562 ** handle mutex. */
194563 sqlite3_mutex_enter(sqlite3_db_mutex(db));
194564 pOld = (sqlite3_session*)sqlite3_preupdate_hook(db, xPreUpdate, (void*)pNew);
194565 pNew->pNext = pOld;
194566 sqlite3_mutex_leave(sqlite3_db_mutex(db));
194567
194568 *ppSession = pNew;
194569 return SQLITE_OK;
194570}
194571
194572/*
194573** Free the list of table objects passed as the first argument. The contents
194574** of the changed-rows hash tables are also deleted.
194575*/
194576static void sessionDeleteTable(SessionTable *pList){
194577 SessionTable *pNext;
194578 SessionTable *pTab;
194579
194580 for(pTab=pList; pTab; pTab=pNext){
194581 int i;
194582 pNext = pTab->pNext;
194583 for(i=0; i<pTab->nChange; i++){
194584 SessionChange *p;
194585 SessionChange *pNextChange;
194586 for(p=pTab->apChange[i]; p; p=pNextChange){
194587 pNextChange = p->pNext;
194588 sqlite3_free(p);
194589 }
194590 }
194591 sqlite3_free((char*)pTab->azCol); /* cast works around VC++ bug */
194592 sqlite3_free(pTab->apChange);
194593 sqlite3_free(pTab);
194594 }
194595}
194596
194597/*
194598** Delete a session object previously allocated using sqlite3session_create().
194599*/
194600SQLITE_API void sqlite3session_delete(sqlite3_session *pSession){
194601 sqlite3 *db = pSession->db;
194602 sqlite3_session *pHead;
194603 sqlite3_session **pp;
194604
194605 /* Unlink the session from the linked list of sessions attached to the
194606 ** database handle. Hold the db mutex while doing so. */
194607 sqlite3_mutex_enter(sqlite3_db_mutex(db));
194608 pHead = (sqlite3_session*)sqlite3_preupdate_hook(db, 0, 0);
194609 for(pp=&pHead; ALWAYS((*pp)!=0); pp=&((*pp)->pNext)){
194610 if( (*pp)==pSession ){
194611 *pp = (*pp)->pNext;
194612 if( pHead ) sqlite3_preupdate_hook(db, xPreUpdate, (void*)pHead);
194613 break;
194614 }
194615 }
194616 sqlite3_mutex_leave(sqlite3_db_mutex(db));
194617 sqlite3ValueFree(pSession->pZeroBlob);
194618
194619 /* Delete all attached table objects. And the contents of their
194620 ** associated hash-tables. */
194621 sessionDeleteTable(pSession->pTable);
194622
194623 /* Free the session object itself. */
194624 sqlite3_free(pSession);
194625}
194626
194627/*
194628** Set a table filter on a Session Object.
194629*/
194630SQLITE_API void sqlite3session_table_filter(
194631 sqlite3_session *pSession,
194632 int(*xFilter)(void*, const char*),
194633 void *pCtx /* First argument passed to xFilter */
194634){
194635 pSession->bAutoAttach = 1;
194636 pSession->pFilterCtx = pCtx;
194637 pSession->xTableFilter = xFilter;
194638}
194639
194640/*
194641** Attach a table to a session. All subsequent changes made to the table
194642** while the session object is enabled will be recorded.
194643**
194644** Only tables that have a PRIMARY KEY defined may be attached. It does
194645** not matter if the PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias)
194646** or not.
194647*/
194648SQLITE_API int sqlite3session_attach(
194649 sqlite3_session *pSession, /* Session object */
194650 const char *zName /* Table name */
194651){
194652 int rc = SQLITE_OK;
194653 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
194654
194655 if( !zName ){
194656 pSession->bAutoAttach = 1;
194657 }else{
194658 SessionTable *pTab; /* New table object (if required) */
194659 int nName; /* Number of bytes in string zName */
194660
194661 /* First search for an existing entry. If one is found, this call is
194662 ** a no-op. Return early. */
194663 nName = sqlite3Strlen30(zName);
194664 for(pTab=pSession->pTable; pTab; pTab=pTab->pNext){
194665 if( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ) break;
194666 }
194667
194668 if( !pTab ){
194669 /* Allocate new SessionTable object. */
194670 pTab = (SessionTable *)sqlite3_malloc(sizeof(SessionTable) + nName + 1);
194671 if( !pTab ){
194672 rc = SQLITE_NOMEM;
194673 }else{
194674 /* Populate the new SessionTable object and link it into the list.
194675 ** The new object must be linked onto the end of the list, not
194676 ** simply added to the start of it in order to ensure that tables
194677 ** appear in the correct order when a changeset or patchset is
194678 ** eventually generated. */
194679 SessionTable **ppTab;
194680 memset(pTab, 0, sizeof(SessionTable));
194681 pTab->zName = (char *)&pTab[1];
194682 memcpy(pTab->zName, zName, nName+1);
194683 for(ppTab=&pSession->pTable; *ppTab; ppTab=&(*ppTab)->pNext);
194684 *ppTab = pTab;
194685 }
194686 }
194687 }
194688
194689 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
194690 return rc;
194691}
194692
194693/*
194694** Ensure that there is room in the buffer to append nByte bytes of data.
194695** If not, use sqlite3_realloc() to grow the buffer so that there is.
194696**
194697** If successful, return zero. Otherwise, if an OOM condition is encountered,
194698** set *pRc to SQLITE_NOMEM and return non-zero.
194699*/
194700static int sessionBufferGrow(SessionBuffer *p, int nByte, int *pRc){
194701 if( *pRc==SQLITE_OK && p->nAlloc-p->nBuf<nByte ){
194702 u8 *aNew;
194703 i64 nNew = p->nAlloc ? p->nAlloc : 128;
194704 do {
194705 nNew = nNew*2;
194706 }while( (nNew-p->nBuf)<nByte );
194707
194708 aNew = (u8 *)sqlite3_realloc64(p->aBuf, nNew);
194709 if( 0==aNew ){
194710 *pRc = SQLITE_NOMEM;
194711 }else{
194712 p->aBuf = aNew;
194713 p->nAlloc = nNew;
194714 }
194715 }
194716 return (*pRc!=SQLITE_OK);
194717}
194718
194719/*
194720** Append the value passed as the second argument to the buffer passed
194721** as the first.
194722**
194723** This function is a no-op if *pRc is non-zero when it is called.
194724** Otherwise, if an error occurs, *pRc is set to an SQLite error code
194725** before returning.
194726*/
194727static void sessionAppendValue(SessionBuffer *p, sqlite3_value *pVal, int *pRc){
194728 int rc = *pRc;
194729 if( rc==SQLITE_OK ){
194730 int nByte = 0;
194731 rc = sessionSerializeValue(0, pVal, &nByte);
194732 sessionBufferGrow(p, nByte, &rc);
194733 if( rc==SQLITE_OK ){
194734 rc = sessionSerializeValue(&p->aBuf[p->nBuf], pVal, 0);
194735 p->nBuf += nByte;
194736 }else{
194737 *pRc = rc;
194738 }
194739 }
194740}
194741
194742/*
194743** This function is a no-op if *pRc is other than SQLITE_OK when it is
194744** called. Otherwise, append a single byte to the buffer.
194745**
194746** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
194747** returning.
194748*/
194749static void sessionAppendByte(SessionBuffer *p, u8 v, int *pRc){
194750 if( 0==sessionBufferGrow(p, 1, pRc) ){
194751 p->aBuf[p->nBuf++] = v;
194752 }
194753}
194754
194755/*
194756** This function is a no-op if *pRc is other than SQLITE_OK when it is
194757** called. Otherwise, append a single varint to the buffer.
194758**
194759** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
194760** returning.
194761*/
194762static void sessionAppendVarint(SessionBuffer *p, int v, int *pRc){
194763 if( 0==sessionBufferGrow(p, 9, pRc) ){
194764 p->nBuf += sessionVarintPut(&p->aBuf[p->nBuf], v);
194765 }
194766}
194767
194768/*
194769** This function is a no-op if *pRc is other than SQLITE_OK when it is
194770** called. Otherwise, append a blob of data to the buffer.
194771**
194772** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
194773** returning.
194774*/
194775static void sessionAppendBlob(
194776 SessionBuffer *p,
194777 const u8 *aBlob,
194778 int nBlob,
194779 int *pRc
194780){
194781 if( nBlob>0 && 0==sessionBufferGrow(p, nBlob, pRc) ){
194782 memcpy(&p->aBuf[p->nBuf], aBlob, nBlob);
194783 p->nBuf += nBlob;
194784 }
194785}
194786
194787/*
194788** This function is a no-op if *pRc is other than SQLITE_OK when it is
194789** called. Otherwise, append a string to the buffer. All bytes in the string
194790** up to (but not including) the nul-terminator are written to the buffer.
194791**
194792** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
194793** returning.
194794*/
194795static void sessionAppendStr(
194796 SessionBuffer *p,
194797 const char *zStr,
194798 int *pRc
194799){
194800 int nStr = sqlite3Strlen30(zStr);
194801 if( 0==sessionBufferGrow(p, nStr, pRc) ){
194802 memcpy(&p->aBuf[p->nBuf], zStr, nStr);
194803 p->nBuf += nStr;
194804 }
194805}
194806
194807/*
194808** This function is a no-op if *pRc is other than SQLITE_OK when it is
194809** called. Otherwise, append the string representation of integer iVal
194810** to the buffer. No nul-terminator is written.
194811**
194812** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
194813** returning.
194814*/
194815static void sessionAppendInteger(
194816 SessionBuffer *p, /* Buffer to append to */
194817 int iVal, /* Value to write the string rep. of */
194818 int *pRc /* IN/OUT: Error code */
194819){
194820 char aBuf[24];
194821 sqlite3_snprintf(sizeof(aBuf)-1, aBuf, "%d", iVal);
194822 sessionAppendStr(p, aBuf, pRc);
194823}
194824
194825/*
194826** This function is a no-op if *pRc is other than SQLITE_OK when it is
194827** called. Otherwise, append the string zStr enclosed in quotes (") and
194828** with any embedded quote characters escaped to the buffer. No
194829** nul-terminator byte is written.
194830**
194831** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
194832** returning.
194833*/
194834static void sessionAppendIdent(
194835 SessionBuffer *p, /* Buffer to a append to */
194836 const char *zStr, /* String to quote, escape and append */
194837 int *pRc /* IN/OUT: Error code */
194838){
194839 int nStr = sqlite3Strlen30(zStr)*2 + 2 + 1;
194840 if( 0==sessionBufferGrow(p, nStr, pRc) ){
194841 char *zOut = (char *)&p->aBuf[p->nBuf];
194842 const char *zIn = zStr;
194843 *zOut++ = '"';
194844 while( *zIn ){
194845 if( *zIn=='"' ) *zOut++ = '"';
194846 *zOut++ = *(zIn++);
194847 }
194848 *zOut++ = '"';
194849 p->nBuf = (int)((u8 *)zOut - p->aBuf);
194850 }
194851}
194852
194853/*
194854** This function is a no-op if *pRc is other than SQLITE_OK when it is
194855** called. Otherwse, it appends the serialized version of the value stored
194856** in column iCol of the row that SQL statement pStmt currently points
194857** to to the buffer.
194858*/
194859static void sessionAppendCol(
194860 SessionBuffer *p, /* Buffer to append to */
194861 sqlite3_stmt *pStmt, /* Handle pointing to row containing value */
194862 int iCol, /* Column to read value from */
194863 int *pRc /* IN/OUT: Error code */
194864){
194865 if( *pRc==SQLITE_OK ){
194866 int eType = sqlite3_column_type(pStmt, iCol);
194867 sessionAppendByte(p, (u8)eType, pRc);
194868 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
194869 sqlite3_int64 i;
194870 u8 aBuf[8];
194871 if( eType==SQLITE_INTEGER ){
194872 i = sqlite3_column_int64(pStmt, iCol);
194873 }else{
194874 double r = sqlite3_column_double(pStmt, iCol);
194875 memcpy(&i, &r, 8);
194876 }
194877 sessionPutI64(aBuf, i);
194878 sessionAppendBlob(p, aBuf, 8, pRc);
194879 }
194880 if( eType==SQLITE_BLOB || eType==SQLITE_TEXT ){
194881 u8 *z;
194882 int nByte;
194883 if( eType==SQLITE_BLOB ){
194884 z = (u8 *)sqlite3_column_blob(pStmt, iCol);
194885 }else{
194886 z = (u8 *)sqlite3_column_text(pStmt, iCol);
194887 }
194888 nByte = sqlite3_column_bytes(pStmt, iCol);
194889 if( z || (eType==SQLITE_BLOB && nByte==0) ){
194890 sessionAppendVarint(p, nByte, pRc);
194891 sessionAppendBlob(p, z, nByte, pRc);
194892 }else{
194893 *pRc = SQLITE_NOMEM;
194894 }
194895 }
194896 }
194897}
194898
194899/*
194900**
194901** This function appends an update change to the buffer (see the comments
194902** under "CHANGESET FORMAT" at the top of the file). An update change
194903** consists of:
194904**
194905** 1 byte: SQLITE_UPDATE (0x17)
194906** n bytes: old.* record (see RECORD FORMAT)
194907** m bytes: new.* record (see RECORD FORMAT)
194908**
194909** The SessionChange object passed as the third argument contains the
194910** values that were stored in the row when the session began (the old.*
194911** values). The statement handle passed as the second argument points
194912** at the current version of the row (the new.* values).
194913**
194914** If all of the old.* values are equal to their corresponding new.* value
194915** (i.e. nothing has changed), then no data at all is appended to the buffer.
194916**
194917** Otherwise, the old.* record contains all primary key values and the
194918** original values of any fields that have been modified. The new.* record
194919** contains the new values of only those fields that have been modified.
194920*/
194921static int sessionAppendUpdate(
194922 SessionBuffer *pBuf, /* Buffer to append to */
194923 int bPatchset, /* True for "patchset", 0 for "changeset" */
194924 sqlite3_stmt *pStmt, /* Statement handle pointing at new row */
194925 SessionChange *p, /* Object containing old values */
194926 u8 *abPK /* Boolean array - true for PK columns */
194927){
194928 int rc = SQLITE_OK;
194929 SessionBuffer buf2 = {0,0,0}; /* Buffer to accumulate new.* record in */
194930 int bNoop = 1; /* Set to zero if any values are modified */
194931 int nRewind = pBuf->nBuf; /* Set to zero if any values are modified */
194932 int i; /* Used to iterate through columns */
194933 u8 *pCsr = p->aRecord; /* Used to iterate through old.* values */
194934
194935 sessionAppendByte(pBuf, SQLITE_UPDATE, &rc);
194936 sessionAppendByte(pBuf, p->bIndirect, &rc);
194937 for(i=0; i<sqlite3_column_count(pStmt); i++){
194938 int bChanged = 0;
194939 int nAdvance;
194940 int eType = *pCsr;
194941 switch( eType ){
194942 case SQLITE_NULL:
194943 nAdvance = 1;
194944 if( sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
194945 bChanged = 1;
194946 }
194947 break;
194948
194949 case SQLITE_FLOAT:
194950 case SQLITE_INTEGER: {
194951 nAdvance = 9;
194952 if( eType==sqlite3_column_type(pStmt, i) ){
194953 sqlite3_int64 iVal = sessionGetI64(&pCsr[1]);
194954 if( eType==SQLITE_INTEGER ){
194955 if( iVal==sqlite3_column_int64(pStmt, i) ) break;
194956 }else{
194957 double dVal;
194958 memcpy(&dVal, &iVal, 8);
194959 if( dVal==sqlite3_column_double(pStmt, i) ) break;
194960 }
194961 }
194962 bChanged = 1;
194963 break;
194964 }
194965
194966 default: {
194967 int n;
194968 int nHdr = 1 + sessionVarintGet(&pCsr[1], &n);
194969 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
194970 nAdvance = nHdr + n;
194971 if( eType==sqlite3_column_type(pStmt, i)
194972 && n==sqlite3_column_bytes(pStmt, i)
194973 && (n==0 || 0==memcmp(&pCsr[nHdr], sqlite3_column_blob(pStmt, i), n))
194974 ){
194975 break;
194976 }
194977 bChanged = 1;
194978 }
194979 }
194980
194981 /* If at least one field has been modified, this is not a no-op. */
194982 if( bChanged ) bNoop = 0;
194983
194984 /* Add a field to the old.* record. This is omitted if this modules is
194985 ** currently generating a patchset. */
194986 if( bPatchset==0 ){
194987 if( bChanged || abPK[i] ){
194988 sessionAppendBlob(pBuf, pCsr, nAdvance, &rc);
194989 }else{
194990 sessionAppendByte(pBuf, 0, &rc);
194991 }
194992 }
194993
194994 /* Add a field to the new.* record. Or the only record if currently
194995 ** generating a patchset. */
194996 if( bChanged || (bPatchset && abPK[i]) ){
194997 sessionAppendCol(&buf2, pStmt, i, &rc);
194998 }else{
194999 sessionAppendByte(&buf2, 0, &rc);
195000 }
195001
195002 pCsr += nAdvance;
195003 }
195004
195005 if( bNoop ){
195006 pBuf->nBuf = nRewind;
195007 }else{
195008 sessionAppendBlob(pBuf, buf2.aBuf, buf2.nBuf, &rc);
195009 }
195010 sqlite3_free(buf2.aBuf);
195011
195012 return rc;
195013}
195014
195015/*
195016** Append a DELETE change to the buffer passed as the first argument. Use
195017** the changeset format if argument bPatchset is zero, or the patchset
195018** format otherwise.
195019*/
195020static int sessionAppendDelete(
195021 SessionBuffer *pBuf, /* Buffer to append to */
195022 int bPatchset, /* True for "patchset", 0 for "changeset" */
195023 SessionChange *p, /* Object containing old values */
195024 int nCol, /* Number of columns in table */
195025 u8 *abPK /* Boolean array - true for PK columns */
195026){
195027 int rc = SQLITE_OK;
195028
195029 sessionAppendByte(pBuf, SQLITE_DELETE, &rc);
195030 sessionAppendByte(pBuf, p->bIndirect, &rc);
195031
195032 if( bPatchset==0 ){
195033 sessionAppendBlob(pBuf, p->aRecord, p->nRecord, &rc);
195034 }else{
195035 int i;
195036 u8 *a = p->aRecord;
195037 for(i=0; i<nCol; i++){
195038 u8 *pStart = a;
195039 int eType = *a++;
195040
195041 switch( eType ){
195042 case 0:
195043 case SQLITE_NULL:
195044 assert( abPK[i]==0 );
195045 break;
195046
195047 case SQLITE_FLOAT:
195048 case SQLITE_INTEGER:
195049 a += 8;
195050 break;
195051
195052 default: {
195053 int n;
195054 a += sessionVarintGet(a, &n);
195055 a += n;
195056 break;
195057 }
195058 }
195059 if( abPK[i] ){
195060 sessionAppendBlob(pBuf, pStart, (int)(a-pStart), &rc);
195061 }
195062 }
195063 assert( (a - p->aRecord)==p->nRecord );
195064 }
195065
195066 return rc;
195067}
195068
195069/*
195070** Formulate and prepare a SELECT statement to retrieve a row from table
195071** zTab in database zDb based on its primary key. i.e.
195072**
195073** SELECT * FROM zDb.zTab WHERE pk1 = ? AND pk2 = ? AND ...
195074*/
195075static int sessionSelectStmt(
195076 sqlite3 *db, /* Database handle */
195077 const char *zDb, /* Database name */
195078 const char *zTab, /* Table name */
195079 int nCol, /* Number of columns in table */
195080 const char **azCol, /* Names of table columns */
195081 u8 *abPK, /* PRIMARY KEY array */
195082 sqlite3_stmt **ppStmt /* OUT: Prepared SELECT statement */
195083){
195084 int rc = SQLITE_OK;
195085 char *zSql = 0;
195086 int nSql = -1;
195087
195088 if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){
195089 zSql = sqlite3_mprintf(
195090 "SELECT tbl, ?2, stat FROM %Q.sqlite_stat1 WHERE tbl IS ?1 AND "
195091 "idx IS (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", zDb
195092 );
195093 if( zSql==0 ) rc = SQLITE_NOMEM;
195094 }else{
195095 int i;
195096 const char *zSep = "";
195097 SessionBuffer buf = {0, 0, 0};
195098
195099 sessionAppendStr(&buf, "SELECT * FROM ", &rc);
195100 sessionAppendIdent(&buf, zDb, &rc);
195101 sessionAppendStr(&buf, ".", &rc);
195102 sessionAppendIdent(&buf, zTab, &rc);
195103 sessionAppendStr(&buf, " WHERE ", &rc);
195104 for(i=0; i<nCol; i++){
195105 if( abPK[i] ){
195106 sessionAppendStr(&buf, zSep, &rc);
195107 sessionAppendIdent(&buf, azCol[i], &rc);
195108 sessionAppendStr(&buf, " IS ?", &rc);
195109 sessionAppendInteger(&buf, i+1, &rc);
195110 zSep = " AND ";
195111 }
195112 }
195113 zSql = (char*)buf.aBuf;
195114 nSql = buf.nBuf;
195115 }
195116
195117 if( rc==SQLITE_OK ){
195118 rc = sqlite3_prepare_v2(db, zSql, nSql, ppStmt, 0);
195119 }
195120 sqlite3_free(zSql);
195121 return rc;
195122}
195123
195124/*
195125** Bind the PRIMARY KEY values from the change passed in argument pChange
195126** to the SELECT statement passed as the first argument. The SELECT statement
195127** is as prepared by function sessionSelectStmt().
195128**
195129** Return SQLITE_OK if all PK values are successfully bound, or an SQLite
195130** error code (e.g. SQLITE_NOMEM) otherwise.
195131*/
195132static int sessionSelectBind(
195133 sqlite3_stmt *pSelect, /* SELECT from sessionSelectStmt() */
195134 int nCol, /* Number of columns in table */
195135 u8 *abPK, /* PRIMARY KEY array */
195136 SessionChange *pChange /* Change structure */
195137){
195138 int i;
195139 int rc = SQLITE_OK;
195140 u8 *a = pChange->aRecord;
195141
195142 for(i=0; i<nCol && rc==SQLITE_OK; i++){
195143 int eType = *a++;
195144
195145 switch( eType ){
195146 case 0:
195147 case SQLITE_NULL:
195148 assert( abPK[i]==0 );
195149 break;
195150
195151 case SQLITE_INTEGER: {
195152 if( abPK[i] ){
195153 i64 iVal = sessionGetI64(a);
195154 rc = sqlite3_bind_int64(pSelect, i+1, iVal);
195155 }
195156 a += 8;
195157 break;
195158 }
195159
195160 case SQLITE_FLOAT: {
195161 if( abPK[i] ){
195162 double rVal;
195163 i64 iVal = sessionGetI64(a);
195164 memcpy(&rVal, &iVal, 8);
195165 rc = sqlite3_bind_double(pSelect, i+1, rVal);
195166 }
195167 a += 8;
195168 break;
195169 }
195170
195171 case SQLITE_TEXT: {
195172 int n;
195173 a += sessionVarintGet(a, &n);
195174 if( abPK[i] ){
195175 rc = sqlite3_bind_text(pSelect, i+1, (char *)a, n, SQLITE_TRANSIENT);
195176 }
195177 a += n;
195178 break;
195179 }
195180
195181 default: {
195182 int n;
195183 assert( eType==SQLITE_BLOB );
195184 a += sessionVarintGet(a, &n);
195185 if( abPK[i] ){
195186 rc = sqlite3_bind_blob(pSelect, i+1, a, n, SQLITE_TRANSIENT);
195187 }
195188 a += n;
195189 break;
195190 }
195191 }
195192 }
195193
195194 return rc;
195195}
195196
195197/*
195198** This function is a no-op if *pRc is set to other than SQLITE_OK when it
195199** is called. Otherwise, append a serialized table header (part of the binary
195200** changeset format) to buffer *pBuf. If an error occurs, set *pRc to an
195201** SQLite error code before returning.
195202*/
195203static void sessionAppendTableHdr(
195204 SessionBuffer *pBuf, /* Append header to this buffer */
195205 int bPatchset, /* Use the patchset format if true */
195206 SessionTable *pTab, /* Table object to append header for */
195207 int *pRc /* IN/OUT: Error code */
195208){
195209 /* Write a table header */
195210 sessionAppendByte(pBuf, (bPatchset ? 'P' : 'T'), pRc);
195211 sessionAppendVarint(pBuf, pTab->nCol, pRc);
195212 sessionAppendBlob(pBuf, pTab->abPK, pTab->nCol, pRc);
195213 sessionAppendBlob(pBuf, (u8 *)pTab->zName, (int)strlen(pTab->zName)+1, pRc);
195214}
195215
195216/*
195217** Generate either a changeset (if argument bPatchset is zero) or a patchset
195218** (if it is non-zero) based on the current contents of the session object
195219** passed as the first argument.
195220**
195221** If no error occurs, SQLITE_OK is returned and the new changeset/patchset
195222** stored in output variables *pnChangeset and *ppChangeset. Or, if an error
195223** occurs, an SQLite error code is returned and both output variables set
195224** to 0.
195225*/
195226static int sessionGenerateChangeset(
195227 sqlite3_session *pSession, /* Session object */
195228 int bPatchset, /* True for patchset, false for changeset */
195229 int (*xOutput)(void *pOut, const void *pData, int nData),
195230 void *pOut, /* First argument for xOutput */
195231 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
195232 void **ppChangeset /* OUT: Buffer containing changeset */
195233){
195234 sqlite3 *db = pSession->db; /* Source database handle */
195235 SessionTable *pTab; /* Used to iterate through attached tables */
195236 SessionBuffer buf = {0,0,0}; /* Buffer in which to accumlate changeset */
195237 int rc; /* Return code */
195238
195239 assert( xOutput==0 || (pnChangeset==0 && ppChangeset==0 ) );
195240
195241 /* Zero the output variables in case an error occurs. If this session
195242 ** object is already in the error state (sqlite3_session.rc != SQLITE_OK),
195243 ** this call will be a no-op. */
195244 if( xOutput==0 ){
195245 *pnChangeset = 0;
195246 *ppChangeset = 0;
195247 }
195248
195249 if( pSession->rc ) return pSession->rc;
195250 rc = sqlite3_exec(pSession->db, "SAVEPOINT changeset", 0, 0, 0);
195251 if( rc!=SQLITE_OK ) return rc;
195252
195253 sqlite3_mutex_enter(sqlite3_db_mutex(db));
195254
195255 for(pTab=pSession->pTable; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
195256 if( pTab->nEntry ){
195257 const char *zName = pTab->zName;
195258 int nCol; /* Number of columns in table */
195259 u8 *abPK; /* Primary key array */
195260 const char **azCol = 0; /* Table columns */
195261 int i; /* Used to iterate through hash buckets */
195262 sqlite3_stmt *pSel = 0; /* SELECT statement to query table pTab */
195263 int nRewind = buf.nBuf; /* Initial size of write buffer */
195264 int nNoop; /* Size of buffer after writing tbl header */
195265
195266 /* Check the table schema is still Ok. */
195267 rc = sessionTableInfo(db, pSession->zDb, zName, &nCol, 0, &azCol, &abPK);
195268 if( !rc && (pTab->nCol!=nCol || memcmp(abPK, pTab->abPK, nCol)) ){
195269 rc = SQLITE_SCHEMA;
195270 }
195271
195272 /* Write a table header */
195273 sessionAppendTableHdr(&buf, bPatchset, pTab, &rc);
195274
195275 /* Build and compile a statement to execute: */
195276 if( rc==SQLITE_OK ){
195277 rc = sessionSelectStmt(
195278 db, pSession->zDb, zName, nCol, azCol, abPK, &pSel);
195279 }
195280
195281 nNoop = buf.nBuf;
195282 for(i=0; i<pTab->nChange && rc==SQLITE_OK; i++){
195283 SessionChange *p; /* Used to iterate through changes */
195284
195285 for(p=pTab->apChange[i]; rc==SQLITE_OK && p; p=p->pNext){
195286 rc = sessionSelectBind(pSel, nCol, abPK, p);
195287 if( rc!=SQLITE_OK ) continue;
195288 if( sqlite3_step(pSel)==SQLITE_ROW ){
195289 if( p->op==SQLITE_INSERT ){
195290 int iCol;
195291 sessionAppendByte(&buf, SQLITE_INSERT, &rc);
195292 sessionAppendByte(&buf, p->bIndirect, &rc);
195293 for(iCol=0; iCol<nCol; iCol++){
195294 sessionAppendCol(&buf, pSel, iCol, &rc);
195295 }
195296 }else{
195297 rc = sessionAppendUpdate(&buf, bPatchset, pSel, p, abPK);
195298 }
195299 }else if( p->op!=SQLITE_INSERT ){
195300 rc = sessionAppendDelete(&buf, bPatchset, p, nCol, abPK);
195301 }
195302 if( rc==SQLITE_OK ){
195303 rc = sqlite3_reset(pSel);
195304 }
195305
195306 /* If the buffer is now larger than sessions_strm_chunk_size, pass
195307 ** its contents to the xOutput() callback. */
195308 if( xOutput
195309 && rc==SQLITE_OK
195310 && buf.nBuf>nNoop
195311 && buf.nBuf>sessions_strm_chunk_size
195312 ){
195313 rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
195314 nNoop = -1;
195315 buf.nBuf = 0;
195316 }
195317
195318 }
195319 }
195320
195321 sqlite3_finalize(pSel);
195322 if( buf.nBuf==nNoop ){
195323 buf.nBuf = nRewind;
195324 }
195325 sqlite3_free((char*)azCol); /* cast works around VC++ bug */
195326 }
195327 }
195328
195329 if( rc==SQLITE_OK ){
195330 if( xOutput==0 ){
195331 *pnChangeset = buf.nBuf;
195332 *ppChangeset = buf.aBuf;
195333 buf.aBuf = 0;
195334 }else if( buf.nBuf>0 ){
195335 rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
195336 }
195337 }
195338
195339 sqlite3_free(buf.aBuf);
195340 sqlite3_exec(db, "RELEASE changeset", 0, 0, 0);
195341 sqlite3_mutex_leave(sqlite3_db_mutex(db));
195342 return rc;
195343}
195344
195345/*
195346** Obtain a changeset object containing all changes recorded by the
195347** session object passed as the first argument.
195348**
195349** It is the responsibility of the caller to eventually free the buffer
195350** using sqlite3_free().
195351*/
195352SQLITE_API int sqlite3session_changeset(
195353 sqlite3_session *pSession, /* Session object */
195354 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
195355 void **ppChangeset /* OUT: Buffer containing changeset */
195356){
195357 return sessionGenerateChangeset(pSession, 0, 0, 0, pnChangeset, ppChangeset);
195358}
195359
195360/*
195361** Streaming version of sqlite3session_changeset().
195362*/
195363SQLITE_API int sqlite3session_changeset_strm(
195364 sqlite3_session *pSession,
195365 int (*xOutput)(void *pOut, const void *pData, int nData),
195366 void *pOut
195367){
195368 return sessionGenerateChangeset(pSession, 0, xOutput, pOut, 0, 0);
195369}
195370
195371/*
195372** Streaming version of sqlite3session_patchset().
195373*/
195374SQLITE_API int sqlite3session_patchset_strm(
195375 sqlite3_session *pSession,
195376 int (*xOutput)(void *pOut, const void *pData, int nData),
195377 void *pOut
195378){
195379 return sessionGenerateChangeset(pSession, 1, xOutput, pOut, 0, 0);
195380}
195381
195382/*
195383** Obtain a patchset object containing all changes recorded by the
195384** session object passed as the first argument.
195385**
195386** It is the responsibility of the caller to eventually free the buffer
195387** using sqlite3_free().
195388*/
195389SQLITE_API int sqlite3session_patchset(
195390 sqlite3_session *pSession, /* Session object */
195391 int *pnPatchset, /* OUT: Size of buffer at *ppChangeset */
195392 void **ppPatchset /* OUT: Buffer containing changeset */
195393){
195394 return sessionGenerateChangeset(pSession, 1, 0, 0, pnPatchset, ppPatchset);
195395}
195396
195397/*
195398** Enable or disable the session object passed as the first argument.
195399*/
195400SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable){
195401 int ret;
195402 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
195403 if( bEnable>=0 ){
195404 pSession->bEnable = bEnable;
195405 }
195406 ret = pSession->bEnable;
195407 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
195408 return ret;
195409}
195410
195411/*
195412** Enable or disable the session object passed as the first argument.
195413*/
195414SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect){
195415 int ret;
195416 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
195417 if( bIndirect>=0 ){
195418 pSession->bIndirect = bIndirect;
195419 }
195420 ret = pSession->bIndirect;
195421 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
195422 return ret;
195423}
195424
195425/*
195426** Return true if there have been no changes to monitored tables recorded
195427** by the session object passed as the only argument.
195428*/
195429SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession){
195430 int ret = 0;
195431 SessionTable *pTab;
195432
195433 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
195434 for(pTab=pSession->pTable; pTab && ret==0; pTab=pTab->pNext){
195435 ret = (pTab->nEntry>0);
195436 }
195437 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
195438
195439 return (ret==0);
195440}
195441
195442/*
195443** Do the work for either sqlite3changeset_start() or start_strm().
195444*/
195445static int sessionChangesetStart(
195446 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
195447 int (*xInput)(void *pIn, void *pData, int *pnData),
195448 void *pIn,
195449 int nChangeset, /* Size of buffer pChangeset in bytes */
195450 void *pChangeset, /* Pointer to buffer containing changeset */
195451 int bInvert /* True to invert changeset */
195452){
195453 sqlite3_changeset_iter *pRet; /* Iterator to return */
195454 int nByte; /* Number of bytes to allocate for iterator */
195455
195456 assert( xInput==0 || (pChangeset==0 && nChangeset==0) );
195457
195458 /* Zero the output variable in case an error occurs. */
195459 *pp = 0;
195460
195461 /* Allocate and initialize the iterator structure. */
195462 nByte = sizeof(sqlite3_changeset_iter);
195463 pRet = (sqlite3_changeset_iter *)sqlite3_malloc(nByte);
195464 if( !pRet ) return SQLITE_NOMEM;
195465 memset(pRet, 0, sizeof(sqlite3_changeset_iter));
195466 pRet->in.aData = (u8 *)pChangeset;
195467 pRet->in.nData = nChangeset;
195468 pRet->in.xInput = xInput;
195469 pRet->in.pIn = pIn;
195470 pRet->in.bEof = (xInput ? 0 : 1);
195471 pRet->bInvert = bInvert;
195472
195473 /* Populate the output variable and return success. */
195474 *pp = pRet;
195475 return SQLITE_OK;
195476}
195477
195478/*
195479** Create an iterator used to iterate through the contents of a changeset.
195480*/
195481SQLITE_API int sqlite3changeset_start(
195482 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
195483 int nChangeset, /* Size of buffer pChangeset in bytes */
195484 void *pChangeset /* Pointer to buffer containing changeset */
195485){
195486 return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, 0);
195487}
195488SQLITE_API int sqlite3changeset_start_v2(
195489 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
195490 int nChangeset, /* Size of buffer pChangeset in bytes */
195491 void *pChangeset, /* Pointer to buffer containing changeset */
195492 int flags
195493){
195494 int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT);
195495 return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, bInvert);
195496}
195497
195498/*
195499** Streaming version of sqlite3changeset_start().
195500*/
195501SQLITE_API int sqlite3changeset_start_strm(
195502 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
195503 int (*xInput)(void *pIn, void *pData, int *pnData),
195504 void *pIn
195505){
195506 return sessionChangesetStart(pp, xInput, pIn, 0, 0, 0);
195507}
195508SQLITE_API int sqlite3changeset_start_v2_strm(
195509 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
195510 int (*xInput)(void *pIn, void *pData, int *pnData),
195511 void *pIn,
195512 int flags
195513){
195514 int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT);
195515 return sessionChangesetStart(pp, xInput, pIn, 0, 0, bInvert);
195516}
195517
195518/*
195519** If the SessionInput object passed as the only argument is a streaming
195520** object and the buffer is full, discard some data to free up space.
195521*/
195522static void sessionDiscardData(SessionInput *pIn){
195523 if( pIn->xInput && pIn->iNext>=sessions_strm_chunk_size ){
195524 int nMove = pIn->buf.nBuf - pIn->iNext;
195525 assert( nMove>=0 );
195526 if( nMove>0 ){
195527 memmove(pIn->buf.aBuf, &pIn->buf.aBuf[pIn->iNext], nMove);
195528 }
195529 pIn->buf.nBuf -= pIn->iNext;
195530 pIn->iNext = 0;
195531 pIn->nData = pIn->buf.nBuf;
195532 }
195533}
195534
195535/*
195536** Ensure that there are at least nByte bytes available in the buffer. Or,
195537** if there are not nByte bytes remaining in the input, that all available
195538** data is in the buffer.
195539**
195540** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
195541*/
195542static int sessionInputBuffer(SessionInput *pIn, int nByte){
195543 int rc = SQLITE_OK;
195544 if( pIn->xInput ){
195545 while( !pIn->bEof && (pIn->iNext+nByte)>=pIn->nData && rc==SQLITE_OK ){
195546 int nNew = sessions_strm_chunk_size;
195547
195548 if( pIn->bNoDiscard==0 ) sessionDiscardData(pIn);
195549 if( SQLITE_OK==sessionBufferGrow(&pIn->buf, nNew, &rc) ){
195550 rc = pIn->xInput(pIn->pIn, &pIn->buf.aBuf[pIn->buf.nBuf], &nNew);
195551 if( nNew==0 ){
195552 pIn->bEof = 1;
195553 }else{
195554 pIn->buf.nBuf += nNew;
195555 }
195556 }
195557
195558 pIn->aData = pIn->buf.aBuf;
195559 pIn->nData = pIn->buf.nBuf;
195560 }
195561 }
195562 return rc;
195563}
195564
195565/*
195566** When this function is called, *ppRec points to the start of a record
195567** that contains nCol values. This function advances the pointer *ppRec
195568** until it points to the byte immediately following that record.
195569*/
195570static void sessionSkipRecord(
195571 u8 **ppRec, /* IN/OUT: Record pointer */
195572 int nCol /* Number of values in record */
195573){
195574 u8 *aRec = *ppRec;
195575 int i;
195576 for(i=0; i<nCol; i++){
195577 int eType = *aRec++;
195578 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
195579 int nByte;
195580 aRec += sessionVarintGet((u8*)aRec, &nByte);
195581 aRec += nByte;
195582 }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
195583 aRec += 8;
195584 }
195585 }
195586
195587 *ppRec = aRec;
195588}
195589
195590/*
195591** This function sets the value of the sqlite3_value object passed as the
195592** first argument to a copy of the string or blob held in the aData[]
195593** buffer. SQLITE_OK is returned if successful, or SQLITE_NOMEM if an OOM
195594** error occurs.
195595*/
195596static int sessionValueSetStr(
195597 sqlite3_value *pVal, /* Set the value of this object */
195598 u8 *aData, /* Buffer containing string or blob data */
195599 int nData, /* Size of buffer aData[] in bytes */
195600 u8 enc /* String encoding (0 for blobs) */
195601){
195602 /* In theory this code could just pass SQLITE_TRANSIENT as the final
195603 ** argument to sqlite3ValueSetStr() and have the copy created
195604 ** automatically. But doing so makes it difficult to detect any OOM
195605 ** error. Hence the code to create the copy externally. */
195606 u8 *aCopy = sqlite3_malloc(nData+1);
195607 if( aCopy==0 ) return SQLITE_NOMEM;
195608 memcpy(aCopy, aData, nData);
195609 sqlite3ValueSetStr(pVal, nData, (char*)aCopy, enc, sqlite3_free);
195610 return SQLITE_OK;
195611}
195612
195613/*
195614** Deserialize a single record from a buffer in memory. See "RECORD FORMAT"
195615** for details.
195616**
195617** When this function is called, *paChange points to the start of the record
195618** to deserialize. Assuming no error occurs, *paChange is set to point to
195619** one byte after the end of the same record before this function returns.
195620** If the argument abPK is NULL, then the record contains nCol values. Or,
195621** if abPK is other than NULL, then the record contains only the PK fields
195622** (in other words, it is a patchset DELETE record).
195623**
195624** If successful, each element of the apOut[] array (allocated by the caller)
195625** is set to point to an sqlite3_value object containing the value read
195626** from the corresponding position in the record. If that value is not
195627** included in the record (i.e. because the record is part of an UPDATE change
195628** and the field was not modified), the corresponding element of apOut[] is
195629** set to NULL.
195630**
195631** It is the responsibility of the caller to free all sqlite_value structures
195632** using sqlite3_free().
195633**
195634** If an error occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
195635** The apOut[] array may have been partially populated in this case.
195636*/
195637static int sessionReadRecord(
195638 SessionInput *pIn, /* Input data */
195639 int nCol, /* Number of values in record */
195640 u8 *abPK, /* Array of primary key flags, or NULL */
195641 sqlite3_value **apOut /* Write values to this array */
195642){
195643 int i; /* Used to iterate through columns */
195644 int rc = SQLITE_OK;
195645
195646 for(i=0; i<nCol && rc==SQLITE_OK; i++){
195647 int eType = 0; /* Type of value (SQLITE_NULL, TEXT etc.) */
195648 if( abPK && abPK[i]==0 ) continue;
195649 rc = sessionInputBuffer(pIn, 9);
195650 if( rc==SQLITE_OK ){
195651 if( pIn->iNext>=pIn->nData ){
195652 rc = SQLITE_CORRUPT_BKPT;
195653 }else{
195654 eType = pIn->aData[pIn->iNext++];
195655 assert( apOut[i]==0 );
195656 if( eType ){
195657 apOut[i] = sqlite3ValueNew(0);
195658 if( !apOut[i] ) rc = SQLITE_NOMEM;
195659 }
195660 }
195661 }
195662
195663 if( rc==SQLITE_OK ){
195664 u8 *aVal = &pIn->aData[pIn->iNext];
195665 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
195666 int nByte;
195667 pIn->iNext += sessionVarintGet(aVal, &nByte);
195668 rc = sessionInputBuffer(pIn, nByte);
195669 if( rc==SQLITE_OK ){
195670 if( nByte<0 || nByte>pIn->nData-pIn->iNext ){
195671 rc = SQLITE_CORRUPT_BKPT;
195672 }else{
195673 u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
195674 rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
195675 pIn->iNext += nByte;
195676 }
195677 }
195678 }
195679 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
195680 sqlite3_int64 v = sessionGetI64(aVal);
195681 if( eType==SQLITE_INTEGER ){
195682 sqlite3VdbeMemSetInt64(apOut[i], v);
195683 }else{
195684 double d;
195685 memcpy(&d, &v, 8);
195686 sqlite3VdbeMemSetDouble(apOut[i], d);
195687 }
195688 pIn->iNext += 8;
195689 }
195690 }
195691 }
195692
195693 return rc;
195694}
195695
195696/*
195697** The input pointer currently points to the second byte of a table-header.
195698** Specifically, to the following:
195699**
195700** + number of columns in table (varint)
195701** + array of PK flags (1 byte per column),
195702** + table name (nul terminated).
195703**
195704** This function ensures that all of the above is present in the input
195705** buffer (i.e. that it can be accessed without any calls to xInput()).
195706** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
195707** The input pointer is not moved.
195708*/
195709static int sessionChangesetBufferTblhdr(SessionInput *pIn, int *pnByte){
195710 int rc = SQLITE_OK;
195711 int nCol = 0;
195712 int nRead = 0;
195713
195714 rc = sessionInputBuffer(pIn, 9);
195715 if( rc==SQLITE_OK ){
195716 nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
195717 /* The hard upper limit for the number of columns in an SQLite
195718 ** database table is, according to sqliteLimit.h, 32676. So
195719 ** consider any table-header that purports to have more than 65536
195720 ** columns to be corrupt. This is convenient because otherwise,
195721 ** if the (nCol>65536) condition below were omitted, a sufficiently
195722 ** large value for nCol may cause nRead to wrap around and become
195723 ** negative. Leading to a crash. */
195724 if( nCol<0 || nCol>65536 ){
195725 rc = SQLITE_CORRUPT_BKPT;
195726 }else{
195727 rc = sessionInputBuffer(pIn, nRead+nCol+100);
195728 nRead += nCol;
195729 }
195730 }
195731
195732 while( rc==SQLITE_OK ){
195733 while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
195734 nRead++;
195735 }
195736 if( (pIn->iNext + nRead)<pIn->nData ) break;
195737 rc = sessionInputBuffer(pIn, nRead + 100);
195738 }
195739 *pnByte = nRead+1;
195740 return rc;
195741}
195742
195743/*
195744** The input pointer currently points to the first byte of the first field
195745** of a record consisting of nCol columns. This function ensures the entire
195746** record is buffered. It does not move the input pointer.
195747**
195748** If successful, SQLITE_OK is returned and *pnByte is set to the size of
195749** the record in bytes. Otherwise, an SQLite error code is returned. The
195750** final value of *pnByte is undefined in this case.
195751*/
195752static int sessionChangesetBufferRecord(
195753 SessionInput *pIn, /* Input data */
195754 int nCol, /* Number of columns in record */
195755 int *pnByte /* OUT: Size of record in bytes */
195756){
195757 int rc = SQLITE_OK;
195758 int nByte = 0;
195759 int i;
195760 for(i=0; rc==SQLITE_OK && i<nCol; i++){
195761 int eType;
195762 rc = sessionInputBuffer(pIn, nByte + 10);
195763 if( rc==SQLITE_OK ){
195764 eType = pIn->aData[pIn->iNext + nByte++];
195765 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
195766 int n;
195767 nByte += sessionVarintGet(&pIn->aData[pIn->iNext+nByte], &n);
195768 nByte += n;
195769 rc = sessionInputBuffer(pIn, nByte);
195770 }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
195771 nByte += 8;
195772 }
195773 }
195774 }
195775 *pnByte = nByte;
195776 return rc;
195777}
195778
195779/*
195780** The input pointer currently points to the second byte of a table-header.
195781** Specifically, to the following:
195782**
195783** + number of columns in table (varint)
195784** + array of PK flags (1 byte per column),
195785** + table name (nul terminated).
195786**
195787** This function decodes the table-header and populates the p->nCol,
195788** p->zTab and p->abPK[] variables accordingly. The p->apValue[] array is
195789** also allocated or resized according to the new value of p->nCol. The
195790** input pointer is left pointing to the byte following the table header.
195791**
195792** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code
195793** is returned and the final values of the various fields enumerated above
195794** are undefined.
195795*/
195796static int sessionChangesetReadTblhdr(sqlite3_changeset_iter *p){
195797 int rc;
195798 int nCopy;
195799 assert( p->rc==SQLITE_OK );
195800
195801 rc = sessionChangesetBufferTblhdr(&p->in, &nCopy);
195802 if( rc==SQLITE_OK ){
195803 int nByte;
195804 int nVarint;
195805 nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol);
195806 if( p->nCol>0 ){
195807 nCopy -= nVarint;
195808 p->in.iNext += nVarint;
195809 nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
195810 p->tblhdr.nBuf = 0;
195811 sessionBufferGrow(&p->tblhdr, nByte, &rc);
195812 }else{
195813 rc = SQLITE_CORRUPT_BKPT;
195814 }
195815 }
195816
195817 if( rc==SQLITE_OK ){
195818 int iPK = sizeof(sqlite3_value*)*p->nCol*2;
195819 memset(p->tblhdr.aBuf, 0, iPK);
195820 memcpy(&p->tblhdr.aBuf[iPK], &p->in.aData[p->in.iNext], nCopy);
195821 p->in.iNext += nCopy;
195822 }
195823
195824 p->apValue = (sqlite3_value**)p->tblhdr.aBuf;
195825 p->abPK = (u8*)&p->apValue[p->nCol*2];
195826 p->zTab = (char*)&p->abPK[p->nCol];
195827 return (p->rc = rc);
195828}
195829
195830/*
195831** Advance the changeset iterator to the next change.
195832**
195833** If both paRec and pnRec are NULL, then this function works like the public
195834** API sqlite3changeset_next(). If SQLITE_ROW is returned, then the
195835** sqlite3changeset_new() and old() APIs may be used to query for values.
195836**
195837** Otherwise, if paRec and pnRec are not NULL, then a pointer to the change
195838** record is written to *paRec before returning and the number of bytes in
195839** the record to *pnRec.
195840**
195841** Either way, this function returns SQLITE_ROW if the iterator is
195842** successfully advanced to the next change in the changeset, an SQLite
195843** error code if an error occurs, or SQLITE_DONE if there are no further
195844** changes in the changeset.
195845*/
195846static int sessionChangesetNext(
195847 sqlite3_changeset_iter *p, /* Changeset iterator */
195848 u8 **paRec, /* If non-NULL, store record pointer here */
195849 int *pnRec, /* If non-NULL, store size of record here */
195850 int *pbNew /* If non-NULL, true if new table */
195851){
195852 int i;
195853 u8 op;
195854
195855 assert( (paRec==0 && pnRec==0) || (paRec && pnRec) );
195856
195857 /* If the iterator is in the error-state, return immediately. */
195858 if( p->rc!=SQLITE_OK ) return p->rc;
195859
195860 /* Free the current contents of p->apValue[], if any. */
195861 if( p->apValue ){
195862 for(i=0; i<p->nCol*2; i++){
195863 sqlite3ValueFree(p->apValue[i]);
195864 }
195865 memset(p->apValue, 0, sizeof(sqlite3_value*)*p->nCol*2);
195866 }
195867
195868 /* Make sure the buffer contains at least 10 bytes of input data, or all
195869 ** remaining data if there are less than 10 bytes available. This is
195870 ** sufficient either for the 'T' or 'P' byte and the varint that follows
195871 ** it, or for the two single byte values otherwise. */
195872 p->rc = sessionInputBuffer(&p->in, 2);
195873 if( p->rc!=SQLITE_OK ) return p->rc;
195874
195875 /* If the iterator is already at the end of the changeset, return DONE. */
195876 if( p->in.iNext>=p->in.nData ){
195877 return SQLITE_DONE;
195878 }
195879
195880 sessionDiscardData(&p->in);
195881 p->in.iCurrent = p->in.iNext;
195882
195883 op = p->in.aData[p->in.iNext++];
195884 while( op=='T' || op=='P' ){
195885 if( pbNew ) *pbNew = 1;
195886 p->bPatchset = (op=='P');
195887 if( sessionChangesetReadTblhdr(p) ) return p->rc;
195888 if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
195889 p->in.iCurrent = p->in.iNext;
195890 if( p->in.iNext>=p->in.nData ) return SQLITE_DONE;
195891 op = p->in.aData[p->in.iNext++];
195892 }
195893
195894 if( p->zTab==0 || (p->bPatchset && p->bInvert) ){
195895 /* The first record in the changeset is not a table header. Must be a
195896 ** corrupt changeset. */
195897 assert( p->in.iNext==1 || p->zTab );
195898 return (p->rc = SQLITE_CORRUPT_BKPT);
195899 }
195900
195901 p->op = op;
195902 p->bIndirect = p->in.aData[p->in.iNext++];
195903 if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){
195904 return (p->rc = SQLITE_CORRUPT_BKPT);
195905 }
195906
195907 if( paRec ){
195908 int nVal; /* Number of values to buffer */
195909 if( p->bPatchset==0 && op==SQLITE_UPDATE ){
195910 nVal = p->nCol * 2;
195911 }else if( p->bPatchset && op==SQLITE_DELETE ){
195912 nVal = 0;
195913 for(i=0; i<p->nCol; i++) if( p->abPK[i] ) nVal++;
195914 }else{
195915 nVal = p->nCol;
195916 }
195917 p->rc = sessionChangesetBufferRecord(&p->in, nVal, pnRec);
195918 if( p->rc!=SQLITE_OK ) return p->rc;
195919 *paRec = &p->in.aData[p->in.iNext];
195920 p->in.iNext += *pnRec;
195921 }else{
195922 sqlite3_value **apOld = (p->bInvert ? &p->apValue[p->nCol] : p->apValue);
195923 sqlite3_value **apNew = (p->bInvert ? p->apValue : &p->apValue[p->nCol]);
195924
195925 /* If this is an UPDATE or DELETE, read the old.* record. */
195926 if( p->op!=SQLITE_INSERT && (p->bPatchset==0 || p->op==SQLITE_DELETE) ){
195927 u8 *abPK = p->bPatchset ? p->abPK : 0;
195928 p->rc = sessionReadRecord(&p->in, p->nCol, abPK, apOld);
195929 if( p->rc!=SQLITE_OK ) return p->rc;
195930 }
195931
195932 /* If this is an INSERT or UPDATE, read the new.* record. */
195933 if( p->op!=SQLITE_DELETE ){
195934 p->rc = sessionReadRecord(&p->in, p->nCol, 0, apNew);
195935 if( p->rc!=SQLITE_OK ) return p->rc;
195936 }
195937
195938 if( (p->bPatchset || p->bInvert) && p->op==SQLITE_UPDATE ){
195939 /* If this is an UPDATE that is part of a patchset, then all PK and
195940 ** modified fields are present in the new.* record. The old.* record
195941 ** is currently completely empty. This block shifts the PK fields from
195942 ** new.* to old.*, to accommodate the code that reads these arrays. */
195943 for(i=0; i<p->nCol; i++){
195944 assert( p->bPatchset==0 || p->apValue[i]==0 );
195945 if( p->abPK[i] ){
195946 assert( p->apValue[i]==0 );
195947 p->apValue[i] = p->apValue[i+p->nCol];
195948 if( p->apValue[i]==0 ) return (p->rc = SQLITE_CORRUPT_BKPT);
195949 p->apValue[i+p->nCol] = 0;
195950 }
195951 }
195952 }else if( p->bInvert ){
195953 if( p->op==SQLITE_INSERT ) p->op = SQLITE_DELETE;
195954 else if( p->op==SQLITE_DELETE ) p->op = SQLITE_INSERT;
195955 }
195956 }
195957
195958 return SQLITE_ROW;
195959}
195960
195961/*
195962** Advance an iterator created by sqlite3changeset_start() to the next
195963** change in the changeset. This function may return SQLITE_ROW, SQLITE_DONE
195964** or SQLITE_CORRUPT.
195965**
195966** This function may not be called on iterators passed to a conflict handler
195967** callback by changeset_apply().
195968*/
195969SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *p){
195970 return sessionChangesetNext(p, 0, 0, 0);
195971}
195972
195973/*
195974** The following function extracts information on the current change
195975** from a changeset iterator. It may only be called after changeset_next()
195976** has returned SQLITE_ROW.
195977*/
195978SQLITE_API int sqlite3changeset_op(
195979 sqlite3_changeset_iter *pIter, /* Iterator handle */
195980 const char **pzTab, /* OUT: Pointer to table name */
195981 int *pnCol, /* OUT: Number of columns in table */
195982 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
195983 int *pbIndirect /* OUT: True if change is indirect */
195984){
195985 *pOp = pIter->op;
195986 *pnCol = pIter->nCol;
195987 *pzTab = pIter->zTab;
195988 if( pbIndirect ) *pbIndirect = pIter->bIndirect;
195989 return SQLITE_OK;
195990}
195991
195992/*
195993** Return information regarding the PRIMARY KEY and number of columns in
195994** the database table affected by the change that pIter currently points
195995** to. This function may only be called after changeset_next() returns
195996** SQLITE_ROW.
195997*/
195998SQLITE_API int sqlite3changeset_pk(
195999 sqlite3_changeset_iter *pIter, /* Iterator object */
196000 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
196001 int *pnCol /* OUT: Number of entries in output array */
196002){
196003 *pabPK = pIter->abPK;
196004 if( pnCol ) *pnCol = pIter->nCol;
196005 return SQLITE_OK;
196006}
196007
196008/*
196009** This function may only be called while the iterator is pointing to an
196010** SQLITE_UPDATE or SQLITE_DELETE change (see sqlite3changeset_op()).
196011** Otherwise, SQLITE_MISUSE is returned.
196012**
196013** It sets *ppValue to point to an sqlite3_value structure containing the
196014** iVal'th value in the old.* record. Or, if that particular value is not
196015** included in the record (because the change is an UPDATE and the field
196016** was not modified and is not a PK column), set *ppValue to NULL.
196017**
196018** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
196019** not modified. Otherwise, SQLITE_OK.
196020*/
196021SQLITE_API int sqlite3changeset_old(
196022 sqlite3_changeset_iter *pIter, /* Changeset iterator */
196023 int iVal, /* Index of old.* value to retrieve */
196024 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
196025){
196026 if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_DELETE ){
196027 return SQLITE_MISUSE;
196028 }
196029 if( iVal<0 || iVal>=pIter->nCol ){
196030 return SQLITE_RANGE;
196031 }
196032 *ppValue = pIter->apValue[iVal];
196033 return SQLITE_OK;
196034}
196035
196036/*
196037** This function may only be called while the iterator is pointing to an
196038** SQLITE_UPDATE or SQLITE_INSERT change (see sqlite3changeset_op()).
196039** Otherwise, SQLITE_MISUSE is returned.
196040**
196041** It sets *ppValue to point to an sqlite3_value structure containing the
196042** iVal'th value in the new.* record. Or, if that particular value is not
196043** included in the record (because the change is an UPDATE and the field
196044** was not modified), set *ppValue to NULL.
196045**
196046** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
196047** not modified. Otherwise, SQLITE_OK.
196048*/
196049SQLITE_API int sqlite3changeset_new(
196050 sqlite3_changeset_iter *pIter, /* Changeset iterator */
196051 int iVal, /* Index of new.* value to retrieve */
196052 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
196053){
196054 if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_INSERT ){
196055 return SQLITE_MISUSE;
196056 }
196057 if( iVal<0 || iVal>=pIter->nCol ){
196058 return SQLITE_RANGE;
196059 }
196060 *ppValue = pIter->apValue[pIter->nCol+iVal];
196061 return SQLITE_OK;
196062}
196063
196064/*
196065** The following two macros are used internally. They are similar to the
196066** sqlite3changeset_new() and sqlite3changeset_old() functions, except that
196067** they omit all error checking and return a pointer to the requested value.
196068*/
196069#define sessionChangesetNew(pIter, iVal) (pIter)->apValue[(pIter)->nCol+(iVal)]
196070#define sessionChangesetOld(pIter, iVal) (pIter)->apValue[(iVal)]
196071
196072/*
196073** This function may only be called with a changeset iterator that has been
196074** passed to an SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT
196075** conflict-handler function. Otherwise, SQLITE_MISUSE is returned.
196076**
196077** If successful, *ppValue is set to point to an sqlite3_value structure
196078** containing the iVal'th value of the conflicting record.
196079**
196080** If value iVal is out-of-range or some other error occurs, an SQLite error
196081** code is returned. Otherwise, SQLITE_OK.
196082*/
196083SQLITE_API int sqlite3changeset_conflict(
196084 sqlite3_changeset_iter *pIter, /* Changeset iterator */
196085 int iVal, /* Index of conflict record value to fetch */
196086 sqlite3_value **ppValue /* OUT: Value from conflicting row */
196087){
196088 if( !pIter->pConflict ){
196089 return SQLITE_MISUSE;
196090 }
196091 if( iVal<0 || iVal>=pIter->nCol ){
196092 return SQLITE_RANGE;
196093 }
196094 *ppValue = sqlite3_column_value(pIter->pConflict, iVal);
196095 return SQLITE_OK;
196096}
196097
196098/*
196099** This function may only be called with an iterator passed to an
196100** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
196101** it sets the output variable to the total number of known foreign key
196102** violations in the destination database and returns SQLITE_OK.
196103**
196104** In all other cases this function returns SQLITE_MISUSE.
196105*/
196106SQLITE_API int sqlite3changeset_fk_conflicts(
196107 sqlite3_changeset_iter *pIter, /* Changeset iterator */
196108 int *pnOut /* OUT: Number of FK violations */
196109){
196110 if( pIter->pConflict || pIter->apValue ){
196111 return SQLITE_MISUSE;
196112 }
196113 *pnOut = pIter->nCol;
196114 return SQLITE_OK;
196115}
196116
196117
196118/*
196119** Finalize an iterator allocated with sqlite3changeset_start().
196120**
196121** This function may not be called on iterators passed to a conflict handler
196122** callback by changeset_apply().
196123*/
196124SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *p){
196125 int rc = SQLITE_OK;
196126 if( p ){
196127 int i; /* Used to iterate through p->apValue[] */
196128 rc = p->rc;
196129 if( p->apValue ){
196130 for(i=0; i<p->nCol*2; i++) sqlite3ValueFree(p->apValue[i]);
196131 }
196132 sqlite3_free(p->tblhdr.aBuf);
196133 sqlite3_free(p->in.buf.aBuf);
196134 sqlite3_free(p);
196135 }
196136 return rc;
196137}
196138
196139static int sessionChangesetInvert(
196140 SessionInput *pInput, /* Input changeset */
196141 int (*xOutput)(void *pOut, const void *pData, int nData),
196142 void *pOut,
196143 int *pnInverted, /* OUT: Number of bytes in output changeset */
196144 void **ppInverted /* OUT: Inverse of pChangeset */
196145){
196146 int rc = SQLITE_OK; /* Return value */
196147 SessionBuffer sOut; /* Output buffer */
196148 int nCol = 0; /* Number of cols in current table */
196149 u8 *abPK = 0; /* PK array for current table */
196150 sqlite3_value **apVal = 0; /* Space for values for UPDATE inversion */
196151 SessionBuffer sPK = {0, 0, 0}; /* PK array for current table */
196152
196153 /* Initialize the output buffer */
196154 memset(&sOut, 0, sizeof(SessionBuffer));
196155
196156 /* Zero the output variables in case an error occurs. */
196157 if( ppInverted ){
196158 *ppInverted = 0;
196159 *pnInverted = 0;
196160 }
196161
196162 while( 1 ){
196163 u8 eType;
196164
196165 /* Test for EOF. */
196166 if( (rc = sessionInputBuffer(pInput, 2)) ) goto finished_invert;
196167 if( pInput->iNext>=pInput->nData ) break;
196168 eType = pInput->aData[pInput->iNext];
196169
196170 switch( eType ){
196171 case 'T': {
196172 /* A 'table' record consists of:
196173 **
196174 ** * A constant 'T' character,
196175 ** * Number of columns in said table (a varint),
196176 ** * An array of nCol bytes (sPK),
196177 ** * A nul-terminated table name.
196178 */
196179 int nByte;
196180 int nVar;
196181 pInput->iNext++;
196182 if( (rc = sessionChangesetBufferTblhdr(pInput, &nByte)) ){
196183 goto finished_invert;
196184 }
196185 nVar = sessionVarintGet(&pInput->aData[pInput->iNext], &nCol);
196186 sPK.nBuf = 0;
196187 sessionAppendBlob(&sPK, &pInput->aData[pInput->iNext+nVar], nCol, &rc);
196188 sessionAppendByte(&sOut, eType, &rc);
196189 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
196190 if( rc ) goto finished_invert;
196191
196192 pInput->iNext += nByte;
196193 sqlite3_free(apVal);
196194 apVal = 0;
196195 abPK = sPK.aBuf;
196196 break;
196197 }
196198
196199 case SQLITE_INSERT:
196200 case SQLITE_DELETE: {
196201 int nByte;
196202 int bIndirect = pInput->aData[pInput->iNext+1];
196203 int eType2 = (eType==SQLITE_DELETE ? SQLITE_INSERT : SQLITE_DELETE);
196204 pInput->iNext += 2;
196205 assert( rc==SQLITE_OK );
196206 rc = sessionChangesetBufferRecord(pInput, nCol, &nByte);
196207 sessionAppendByte(&sOut, eType2, &rc);
196208 sessionAppendByte(&sOut, bIndirect, &rc);
196209 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
196210 pInput->iNext += nByte;
196211 if( rc ) goto finished_invert;
196212 break;
196213 }
196214
196215 case SQLITE_UPDATE: {
196216 int iCol;
196217
196218 if( 0==apVal ){
196219 apVal = (sqlite3_value **)sqlite3_malloc(sizeof(apVal[0])*nCol*2);
196220 if( 0==apVal ){
196221 rc = SQLITE_NOMEM;
196222 goto finished_invert;
196223 }
196224 memset(apVal, 0, sizeof(apVal[0])*nCol*2);
196225 }
196226
196227 /* Write the header for the new UPDATE change. Same as the original. */
196228 sessionAppendByte(&sOut, eType, &rc);
196229 sessionAppendByte(&sOut, pInput->aData[pInput->iNext+1], &rc);
196230
196231 /* Read the old.* and new.* records for the update change. */
196232 pInput->iNext += 2;
196233 rc = sessionReadRecord(pInput, nCol, 0, &apVal[0]);
196234 if( rc==SQLITE_OK ){
196235 rc = sessionReadRecord(pInput, nCol, 0, &apVal[nCol]);
196236 }
196237
196238 /* Write the new old.* record. Consists of the PK columns from the
196239 ** original old.* record, and the other values from the original
196240 ** new.* record. */
196241 for(iCol=0; iCol<nCol; iCol++){
196242 sqlite3_value *pVal = apVal[iCol + (abPK[iCol] ? 0 : nCol)];
196243 sessionAppendValue(&sOut, pVal, &rc);
196244 }
196245
196246 /* Write the new new.* record. Consists of a copy of all values
196247 ** from the original old.* record, except for the PK columns, which
196248 ** are set to "undefined". */
196249 for(iCol=0; iCol<nCol; iCol++){
196250 sqlite3_value *pVal = (abPK[iCol] ? 0 : apVal[iCol]);
196251 sessionAppendValue(&sOut, pVal, &rc);
196252 }
196253
196254 for(iCol=0; iCol<nCol*2; iCol++){
196255 sqlite3ValueFree(apVal[iCol]);
196256 }
196257 memset(apVal, 0, sizeof(apVal[0])*nCol*2);
196258 if( rc!=SQLITE_OK ){
196259 goto finished_invert;
196260 }
196261
196262 break;
196263 }
196264
196265 default:
196266 rc = SQLITE_CORRUPT_BKPT;
196267 goto finished_invert;
196268 }
196269
196270 assert( rc==SQLITE_OK );
196271 if( xOutput && sOut.nBuf>=sessions_strm_chunk_size ){
196272 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
196273 sOut.nBuf = 0;
196274 if( rc!=SQLITE_OK ) goto finished_invert;
196275 }
196276 }
196277
196278 assert( rc==SQLITE_OK );
196279 if( pnInverted ){
196280 *pnInverted = sOut.nBuf;
196281 *ppInverted = sOut.aBuf;
196282 sOut.aBuf = 0;
196283 }else if( sOut.nBuf>0 ){
196284 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
196285 }
196286
196287 finished_invert:
196288 sqlite3_free(sOut.aBuf);
196289 sqlite3_free(apVal);
196290 sqlite3_free(sPK.aBuf);
196291 return rc;
196292}
196293
196294
196295/*
196296** Invert a changeset object.
196297*/
196298SQLITE_API int sqlite3changeset_invert(
196299 int nChangeset, /* Number of bytes in input */
196300 const void *pChangeset, /* Input changeset */
196301 int *pnInverted, /* OUT: Number of bytes in output changeset */
196302 void **ppInverted /* OUT: Inverse of pChangeset */
196303){
196304 SessionInput sInput;
196305
196306 /* Set up the input stream */
196307 memset(&sInput, 0, sizeof(SessionInput));
196308 sInput.nData = nChangeset;
196309 sInput.aData = (u8*)pChangeset;
196310
196311 return sessionChangesetInvert(&sInput, 0, 0, pnInverted, ppInverted);
196312}
196313
196314/*
196315** Streaming version of sqlite3changeset_invert().
196316*/
196317SQLITE_API int sqlite3changeset_invert_strm(
196318 int (*xInput)(void *pIn, void *pData, int *pnData),
196319 void *pIn,
196320 int (*xOutput)(void *pOut, const void *pData, int nData),
196321 void *pOut
196322){
196323 SessionInput sInput;
196324 int rc;
196325
196326 /* Set up the input stream */
196327 memset(&sInput, 0, sizeof(SessionInput));
196328 sInput.xInput = xInput;
196329 sInput.pIn = pIn;
196330
196331 rc = sessionChangesetInvert(&sInput, xOutput, pOut, 0, 0);
196332 sqlite3_free(sInput.buf.aBuf);
196333 return rc;
196334}
196335
196336typedef struct SessionApplyCtx SessionApplyCtx;
196337struct SessionApplyCtx {
196338 sqlite3 *db;
196339 sqlite3_stmt *pDelete; /* DELETE statement */
196340 sqlite3_stmt *pUpdate; /* UPDATE statement */
196341 sqlite3_stmt *pInsert; /* INSERT statement */
196342 sqlite3_stmt *pSelect; /* SELECT statement */
196343 int nCol; /* Size of azCol[] and abPK[] arrays */
196344 const char **azCol; /* Array of column names */
196345 u8 *abPK; /* Boolean array - true if column is in PK */
196346 int bStat1; /* True if table is sqlite_stat1 */
196347 int bDeferConstraints; /* True to defer constraints */
196348 SessionBuffer constraints; /* Deferred constraints are stored here */
196349 SessionBuffer rebase; /* Rebase information (if any) here */
196350 u8 bRebaseStarted; /* If table header is already in rebase */
196351 u8 bRebase; /* True to collect rebase information */
196352};
196353
196354/*
196355** Formulate a statement to DELETE a row from database db. Assuming a table
196356** structure like this:
196357**
196358** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
196359**
196360** The DELETE statement looks like this:
196361**
196362** DELETE FROM x WHERE a = :1 AND c = :3 AND (:5 OR b IS :2 AND d IS :4)
196363**
196364** Variable :5 (nCol+1) is a boolean. It should be set to 0 if we require
196365** matching b and d values, or 1 otherwise. The second case comes up if the
196366** conflict handler is invoked with NOTFOUND and returns CHANGESET_REPLACE.
196367**
196368** If successful, SQLITE_OK is returned and SessionApplyCtx.pDelete is left
196369** pointing to the prepared version of the SQL statement.
196370*/
196371static int sessionDeleteRow(
196372 sqlite3 *db, /* Database handle */
196373 const char *zTab, /* Table name */
196374 SessionApplyCtx *p /* Session changeset-apply context */
196375){
196376 int i;
196377 const char *zSep = "";
196378 int rc = SQLITE_OK;
196379 SessionBuffer buf = {0, 0, 0};
196380 int nPk = 0;
196381
196382 sessionAppendStr(&buf, "DELETE FROM ", &rc);
196383 sessionAppendIdent(&buf, zTab, &rc);
196384 sessionAppendStr(&buf, " WHERE ", &rc);
196385
196386 for(i=0; i<p->nCol; i++){
196387 if( p->abPK[i] ){
196388 nPk++;
196389 sessionAppendStr(&buf, zSep, &rc);
196390 sessionAppendIdent(&buf, p->azCol[i], &rc);
196391 sessionAppendStr(&buf, " = ?", &rc);
196392 sessionAppendInteger(&buf, i+1, &rc);
196393 zSep = " AND ";
196394 }
196395 }
196396
196397 if( nPk<p->nCol ){
196398 sessionAppendStr(&buf, " AND (?", &rc);
196399 sessionAppendInteger(&buf, p->nCol+1, &rc);
196400 sessionAppendStr(&buf, " OR ", &rc);
196401
196402 zSep = "";
196403 for(i=0; i<p->nCol; i++){
196404 if( !p->abPK[i] ){
196405 sessionAppendStr(&buf, zSep, &rc);
196406 sessionAppendIdent(&buf, p->azCol[i], &rc);
196407 sessionAppendStr(&buf, " IS ?", &rc);
196408 sessionAppendInteger(&buf, i+1, &rc);
196409 zSep = "AND ";
196410 }
196411 }
196412 sessionAppendStr(&buf, ")", &rc);
196413 }
196414
196415 if( rc==SQLITE_OK ){
196416 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pDelete, 0);
196417 }
196418 sqlite3_free(buf.aBuf);
196419
196420 return rc;
196421}
196422
196423/*
196424** Formulate and prepare a statement to UPDATE a row from database db.
196425** Assuming a table structure like this:
196426**
196427** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
196428**
196429** The UPDATE statement looks like this:
196430**
196431** UPDATE x SET
196432** a = CASE WHEN ?2 THEN ?3 ELSE a END,
196433** b = CASE WHEN ?5 THEN ?6 ELSE b END,
196434** c = CASE WHEN ?8 THEN ?9 ELSE c END,
196435** d = CASE WHEN ?11 THEN ?12 ELSE d END
196436** WHERE a = ?1 AND c = ?7 AND (?13 OR
196437** (?5==0 OR b IS ?4) AND (?11==0 OR d IS ?10) AND
196438** )
196439**
196440** For each column in the table, there are three variables to bind:
196441**
196442** ?(i*3+1) The old.* value of the column, if any.
196443** ?(i*3+2) A boolean flag indicating that the value is being modified.
196444** ?(i*3+3) The new.* value of the column, if any.
196445**
196446** Also, a boolean flag that, if set to true, causes the statement to update
196447** a row even if the non-PK values do not match. This is required if the
196448** conflict-handler is invoked with CHANGESET_DATA and returns
196449** CHANGESET_REPLACE. This is variable "?(nCol*3+1)".
196450**
196451** If successful, SQLITE_OK is returned and SessionApplyCtx.pUpdate is left
196452** pointing to the prepared version of the SQL statement.
196453*/
196454static int sessionUpdateRow(
196455 sqlite3 *db, /* Database handle */
196456 const char *zTab, /* Table name */
196457 SessionApplyCtx *p /* Session changeset-apply context */
196458){
196459 int rc = SQLITE_OK;
196460 int i;
196461 const char *zSep = "";
196462 SessionBuffer buf = {0, 0, 0};
196463
196464 /* Append "UPDATE tbl SET " */
196465 sessionAppendStr(&buf, "UPDATE ", &rc);
196466 sessionAppendIdent(&buf, zTab, &rc);
196467 sessionAppendStr(&buf, " SET ", &rc);
196468
196469 /* Append the assignments */
196470 for(i=0; i<p->nCol; i++){
196471 sessionAppendStr(&buf, zSep, &rc);
196472 sessionAppendIdent(&buf, p->azCol[i], &rc);
196473 sessionAppendStr(&buf, " = CASE WHEN ?", &rc);
196474 sessionAppendInteger(&buf, i*3+2, &rc);
196475 sessionAppendStr(&buf, " THEN ?", &rc);
196476 sessionAppendInteger(&buf, i*3+3, &rc);
196477 sessionAppendStr(&buf, " ELSE ", &rc);
196478 sessionAppendIdent(&buf, p->azCol[i], &rc);
196479 sessionAppendStr(&buf, " END", &rc);
196480 zSep = ", ";
196481 }
196482
196483 /* Append the PK part of the WHERE clause */
196484 sessionAppendStr(&buf, " WHERE ", &rc);
196485 for(i=0; i<p->nCol; i++){
196486 if( p->abPK[i] ){
196487 sessionAppendIdent(&buf, p->azCol[i], &rc);
196488 sessionAppendStr(&buf, " = ?", &rc);
196489 sessionAppendInteger(&buf, i*3+1, &rc);
196490 sessionAppendStr(&buf, " AND ", &rc);
196491 }
196492 }
196493
196494 /* Append the non-PK part of the WHERE clause */
196495 sessionAppendStr(&buf, " (?", &rc);
196496 sessionAppendInteger(&buf, p->nCol*3+1, &rc);
196497 sessionAppendStr(&buf, " OR 1", &rc);
196498 for(i=0; i<p->nCol; i++){
196499 if( !p->abPK[i] ){
196500 sessionAppendStr(&buf, " AND (?", &rc);
196501 sessionAppendInteger(&buf, i*3+2, &rc);
196502 sessionAppendStr(&buf, "=0 OR ", &rc);
196503 sessionAppendIdent(&buf, p->azCol[i], &rc);
196504 sessionAppendStr(&buf, " IS ?", &rc);
196505 sessionAppendInteger(&buf, i*3+1, &rc);
196506 sessionAppendStr(&buf, ")", &rc);
196507 }
196508 }
196509 sessionAppendStr(&buf, ")", &rc);
196510
196511 if( rc==SQLITE_OK ){
196512 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pUpdate, 0);
196513 }
196514 sqlite3_free(buf.aBuf);
196515
196516 return rc;
196517}
196518
196519
196520/*
196521** Formulate and prepare an SQL statement to query table zTab by primary
196522** key. Assuming the following table structure:
196523**
196524** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
196525**
196526** The SELECT statement looks like this:
196527**
196528** SELECT * FROM x WHERE a = ?1 AND c = ?3
196529**
196530** If successful, SQLITE_OK is returned and SessionApplyCtx.pSelect is left
196531** pointing to the prepared version of the SQL statement.
196532*/
196533static int sessionSelectRow(
196534 sqlite3 *db, /* Database handle */
196535 const char *zTab, /* Table name */
196536 SessionApplyCtx *p /* Session changeset-apply context */
196537){
196538 return sessionSelectStmt(
196539 db, "main", zTab, p->nCol, p->azCol, p->abPK, &p->pSelect);
196540}
196541
196542/*
196543** Formulate and prepare an INSERT statement to add a record to table zTab.
196544** For example:
196545**
196546** INSERT INTO main."zTab" VALUES(?1, ?2, ?3 ...);
196547**
196548** If successful, SQLITE_OK is returned and SessionApplyCtx.pInsert is left
196549** pointing to the prepared version of the SQL statement.
196550*/
196551static int sessionInsertRow(
196552 sqlite3 *db, /* Database handle */
196553 const char *zTab, /* Table name */
196554 SessionApplyCtx *p /* Session changeset-apply context */
196555){
196556 int rc = SQLITE_OK;
196557 int i;
196558 SessionBuffer buf = {0, 0, 0};
196559
196560 sessionAppendStr(&buf, "INSERT INTO main.", &rc);
196561 sessionAppendIdent(&buf, zTab, &rc);
196562 sessionAppendStr(&buf, "(", &rc);
196563 for(i=0; i<p->nCol; i++){
196564 if( i!=0 ) sessionAppendStr(&buf, ", ", &rc);
196565 sessionAppendIdent(&buf, p->azCol[i], &rc);
196566 }
196567
196568 sessionAppendStr(&buf, ") VALUES(?", &rc);
196569 for(i=1; i<p->nCol; i++){
196570 sessionAppendStr(&buf, ", ?", &rc);
196571 }
196572 sessionAppendStr(&buf, ")", &rc);
196573
196574 if( rc==SQLITE_OK ){
196575 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pInsert, 0);
196576 }
196577 sqlite3_free(buf.aBuf);
196578 return rc;
196579}
196580
196581static int sessionPrepare(sqlite3 *db, sqlite3_stmt **pp, const char *zSql){
196582 return sqlite3_prepare_v2(db, zSql, -1, pp, 0);
196583}
196584
196585/*
196586** Prepare statements for applying changes to the sqlite_stat1 table.
196587** These are similar to those created by sessionSelectRow(),
196588** sessionInsertRow(), sessionUpdateRow() and sessionDeleteRow() for
196589** other tables.
196590*/
196591static int sessionStat1Sql(sqlite3 *db, SessionApplyCtx *p){
196592 int rc = sessionSelectRow(db, "sqlite_stat1", p);
196593 if( rc==SQLITE_OK ){
196594 rc = sessionPrepare(db, &p->pInsert,
196595 "INSERT INTO main.sqlite_stat1 VALUES(?1, "
196596 "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END, "
196597 "?3)"
196598 );
196599 }
196600 if( rc==SQLITE_OK ){
196601 rc = sessionPrepare(db, &p->pUpdate,
196602 "UPDATE main.sqlite_stat1 SET "
196603 "tbl = CASE WHEN ?2 THEN ?3 ELSE tbl END, "
196604 "idx = CASE WHEN ?5 THEN ?6 ELSE idx END, "
196605 "stat = CASE WHEN ?8 THEN ?9 ELSE stat END "
196606 "WHERE tbl=?1 AND idx IS "
196607 "CASE WHEN length(?4)=0 AND typeof(?4)='blob' THEN NULL ELSE ?4 END "
196608 "AND (?10 OR ?8=0 OR stat IS ?7)"
196609 );
196610 }
196611 if( rc==SQLITE_OK ){
196612 rc = sessionPrepare(db, &p->pDelete,
196613 "DELETE FROM main.sqlite_stat1 WHERE tbl=?1 AND idx IS "
196614 "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END "
196615 "AND (?4 OR stat IS ?3)"
196616 );
196617 }
196618 return rc;
196619}
196620
196621/*
196622** A wrapper around sqlite3_bind_value() that detects an extra problem.
196623** See comments in the body of this function for details.
196624*/
196625static int sessionBindValue(
196626 sqlite3_stmt *pStmt, /* Statement to bind value to */
196627 int i, /* Parameter number to bind to */
196628 sqlite3_value *pVal /* Value to bind */
196629){
196630 int eType = sqlite3_value_type(pVal);
196631 /* COVERAGE: The (pVal->z==0) branch is never true using current versions
196632 ** of SQLite. If a malloc fails in an sqlite3_value_xxx() function, either
196633 ** the (pVal->z) variable remains as it was or the type of the value is
196634 ** set to SQLITE_NULL. */
196635 if( (eType==SQLITE_TEXT || eType==SQLITE_BLOB) && pVal->z==0 ){
196636 /* This condition occurs when an earlier OOM in a call to
196637 ** sqlite3_value_text() or sqlite3_value_blob() (perhaps from within
196638 ** a conflict-handler) has zeroed the pVal->z pointer. Return NOMEM. */
196639 return SQLITE_NOMEM;
196640 }
196641 return sqlite3_bind_value(pStmt, i, pVal);
196642}
196643
196644/*
196645** Iterator pIter must point to an SQLITE_INSERT entry. This function
196646** transfers new.* values from the current iterator entry to statement
196647** pStmt. The table being inserted into has nCol columns.
196648**
196649** New.* value $i from the iterator is bound to variable ($i+1) of
196650** statement pStmt. If parameter abPK is NULL, all values from 0 to (nCol-1)
196651** are transfered to the statement. Otherwise, if abPK is not NULL, it points
196652** to an array nCol elements in size. In this case only those values for
196653** which abPK[$i] is true are read from the iterator and bound to the
196654** statement.
196655**
196656** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK.
196657*/
196658static int sessionBindRow(
196659 sqlite3_changeset_iter *pIter, /* Iterator to read values from */
196660 int(*xValue)(sqlite3_changeset_iter *, int, sqlite3_value **),
196661 int nCol, /* Number of columns */
196662 u8 *abPK, /* If not NULL, bind only if true */
196663 sqlite3_stmt *pStmt /* Bind values to this statement */
196664){
196665 int i;
196666 int rc = SQLITE_OK;
196667
196668 /* Neither sqlite3changeset_old or sqlite3changeset_new can fail if the
196669 ** argument iterator points to a suitable entry. Make sure that xValue
196670 ** is one of these to guarantee that it is safe to ignore the return
196671 ** in the code below. */
196672 assert( xValue==sqlite3changeset_old || xValue==sqlite3changeset_new );
196673
196674 for(i=0; rc==SQLITE_OK && i<nCol; i++){
196675 if( !abPK || abPK[i] ){
196676 sqlite3_value *pVal;
196677 (void)xValue(pIter, i, &pVal);
196678 if( pVal==0 ){
196679 /* The value in the changeset was "undefined". This indicates a
196680 ** corrupt changeset blob. */
196681 rc = SQLITE_CORRUPT_BKPT;
196682 }else{
196683 rc = sessionBindValue(pStmt, i+1, pVal);
196684 }
196685 }
196686 }
196687 return rc;
196688}
196689
196690/*
196691** SQL statement pSelect is as generated by the sessionSelectRow() function.
196692** This function binds the primary key values from the change that changeset
196693** iterator pIter points to to the SELECT and attempts to seek to the table
196694** entry. If a row is found, the SELECT statement left pointing at the row
196695** and SQLITE_ROW is returned. Otherwise, if no row is found and no error
196696** has occured, the statement is reset and SQLITE_OK is returned. If an
196697** error occurs, the statement is reset and an SQLite error code is returned.
196698**
196699** If this function returns SQLITE_ROW, the caller must eventually reset()
196700** statement pSelect. If any other value is returned, the statement does
196701** not require a reset().
196702**
196703** If the iterator currently points to an INSERT record, bind values from the
196704** new.* record to the SELECT statement. Or, if it points to a DELETE or
196705** UPDATE, bind values from the old.* record.
196706*/
196707static int sessionSeekToRow(
196708 sqlite3 *db, /* Database handle */
196709 sqlite3_changeset_iter *pIter, /* Changeset iterator */
196710 u8 *abPK, /* Primary key flags array */
196711 sqlite3_stmt *pSelect /* SELECT statement from sessionSelectRow() */
196712){
196713 int rc; /* Return code */
196714 int nCol; /* Number of columns in table */
196715 int op; /* Changset operation (SQLITE_UPDATE etc.) */
196716 const char *zDummy; /* Unused */
196717
196718 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
196719 rc = sessionBindRow(pIter,
196720 op==SQLITE_INSERT ? sqlite3changeset_new : sqlite3changeset_old,
196721 nCol, abPK, pSelect
196722 );
196723
196724 if( rc==SQLITE_OK ){
196725 rc = sqlite3_step(pSelect);
196726 if( rc!=SQLITE_ROW ) rc = sqlite3_reset(pSelect);
196727 }
196728
196729 return rc;
196730}
196731
196732/*
196733** This function is called from within sqlite3changset_apply_v2() when
196734** a conflict is encountered and resolved using conflict resolution
196735** mode eType (either SQLITE_CHANGESET_OMIT or SQLITE_CHANGESET_REPLACE)..
196736** It adds a conflict resolution record to the buffer in
196737** SessionApplyCtx.rebase, which will eventually be returned to the caller
196738** of apply_v2() as the "rebase" buffer.
196739**
196740** Return SQLITE_OK if successful, or an SQLite error code otherwise.
196741*/
196742static int sessionRebaseAdd(
196743 SessionApplyCtx *p, /* Apply context */
196744 int eType, /* Conflict resolution (OMIT or REPLACE) */
196745 sqlite3_changeset_iter *pIter /* Iterator pointing at current change */
196746){
196747 int rc = SQLITE_OK;
196748 if( p->bRebase ){
196749 int i;
196750 int eOp = pIter->op;
196751 if( p->bRebaseStarted==0 ){
196752 /* Append a table-header to the rebase buffer */
196753 const char *zTab = pIter->zTab;
196754 sessionAppendByte(&p->rebase, 'T', &rc);
196755 sessionAppendVarint(&p->rebase, p->nCol, &rc);
196756 sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc);
196757 sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc);
196758 p->bRebaseStarted = 1;
196759 }
196760
196761 assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT );
196762 assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE );
196763
196764 sessionAppendByte(&p->rebase,
196765 (eOp==SQLITE_DELETE ? SQLITE_DELETE : SQLITE_INSERT), &rc
196766 );
196767 sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE), &rc);
196768 for(i=0; i<p->nCol; i++){
196769 sqlite3_value *pVal = 0;
196770 if( eOp==SQLITE_DELETE || (eOp==SQLITE_UPDATE && p->abPK[i]) ){
196771 sqlite3changeset_old(pIter, i, &pVal);
196772 }else{
196773 sqlite3changeset_new(pIter, i, &pVal);
196774 }
196775 sessionAppendValue(&p->rebase, pVal, &rc);
196776 }
196777 }
196778 return rc;
196779}
196780
196781/*
196782** Invoke the conflict handler for the change that the changeset iterator
196783** currently points to.
196784**
196785** Argument eType must be either CHANGESET_DATA or CHANGESET_CONFLICT.
196786** If argument pbReplace is NULL, then the type of conflict handler invoked
196787** depends solely on eType, as follows:
196788**
196789** eType value Value passed to xConflict
196790** -------------------------------------------------
196791** CHANGESET_DATA CHANGESET_NOTFOUND
196792** CHANGESET_CONFLICT CHANGESET_CONSTRAINT
196793**
196794** Or, if pbReplace is not NULL, then an attempt is made to find an existing
196795** record with the same primary key as the record about to be deleted, updated
196796** or inserted. If such a record can be found, it is available to the conflict
196797** handler as the "conflicting" record. In this case the type of conflict
196798** handler invoked is as follows:
196799**
196800** eType value PK Record found? Value passed to xConflict
196801** ----------------------------------------------------------------
196802** CHANGESET_DATA Yes CHANGESET_DATA
196803** CHANGESET_DATA No CHANGESET_NOTFOUND
196804** CHANGESET_CONFLICT Yes CHANGESET_CONFLICT
196805** CHANGESET_CONFLICT No CHANGESET_CONSTRAINT
196806**
196807** If pbReplace is not NULL, and a record with a matching PK is found, and
196808** the conflict handler function returns SQLITE_CHANGESET_REPLACE, *pbReplace
196809** is set to non-zero before returning SQLITE_OK.
196810**
196811** If the conflict handler returns SQLITE_CHANGESET_ABORT, SQLITE_ABORT is
196812** returned. Or, if the conflict handler returns an invalid value,
196813** SQLITE_MISUSE. If the conflict handler returns SQLITE_CHANGESET_OMIT,
196814** this function returns SQLITE_OK.
196815*/
196816static int sessionConflictHandler(
196817 int eType, /* Either CHANGESET_DATA or CONFLICT */
196818 SessionApplyCtx *p, /* changeset_apply() context */
196819 sqlite3_changeset_iter *pIter, /* Changeset iterator */
196820 int(*xConflict)(void *, int, sqlite3_changeset_iter*),
196821 void *pCtx, /* First argument for conflict handler */
196822 int *pbReplace /* OUT: Set to true if PK row is found */
196823){
196824 int res = 0; /* Value returned by conflict handler */
196825 int rc;
196826 int nCol;
196827 int op;
196828 const char *zDummy;
196829
196830 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
196831
196832 assert( eType==SQLITE_CHANGESET_CONFLICT || eType==SQLITE_CHANGESET_DATA );
196833 assert( SQLITE_CHANGESET_CONFLICT+1==SQLITE_CHANGESET_CONSTRAINT );
196834 assert( SQLITE_CHANGESET_DATA+1==SQLITE_CHANGESET_NOTFOUND );
196835
196836 /* Bind the new.* PRIMARY KEY values to the SELECT statement. */
196837 if( pbReplace ){
196838 rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect);
196839 }else{
196840 rc = SQLITE_OK;
196841 }
196842
196843 if( rc==SQLITE_ROW ){
196844 /* There exists another row with the new.* primary key. */
196845 pIter->pConflict = p->pSelect;
196846 res = xConflict(pCtx, eType, pIter);
196847 pIter->pConflict = 0;
196848 rc = sqlite3_reset(p->pSelect);
196849 }else if( rc==SQLITE_OK ){
196850 if( p->bDeferConstraints && eType==SQLITE_CHANGESET_CONFLICT ){
196851 /* Instead of invoking the conflict handler, append the change blob
196852 ** to the SessionApplyCtx.constraints buffer. */
196853 u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent];
196854 int nBlob = pIter->in.iNext - pIter->in.iCurrent;
196855 sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc);
196856 return SQLITE_OK;
196857 }else{
196858 /* No other row with the new.* primary key. */
196859 res = xConflict(pCtx, eType+1, pIter);
196860 if( res==SQLITE_CHANGESET_REPLACE ) rc = SQLITE_MISUSE;
196861 }
196862 }
196863
196864 if( rc==SQLITE_OK ){
196865 switch( res ){
196866 case SQLITE_CHANGESET_REPLACE:
196867 assert( pbReplace );
196868 *pbReplace = 1;
196869 break;
196870
196871 case SQLITE_CHANGESET_OMIT:
196872 break;
196873
196874 case SQLITE_CHANGESET_ABORT:
196875 rc = SQLITE_ABORT;
196876 break;
196877
196878 default:
196879 rc = SQLITE_MISUSE;
196880 break;
196881 }
196882 if( rc==SQLITE_OK ){
196883 rc = sessionRebaseAdd(p, res, pIter);
196884 }
196885 }
196886
196887 return rc;
196888}
196889
196890/*
196891** Attempt to apply the change that the iterator passed as the first argument
196892** currently points to to the database. If a conflict is encountered, invoke
196893** the conflict handler callback.
196894**
196895** If argument pbRetry is NULL, then ignore any CHANGESET_DATA conflict. If
196896** one is encountered, update or delete the row with the matching primary key
196897** instead. Or, if pbRetry is not NULL and a CHANGESET_DATA conflict occurs,
196898** invoke the conflict handler. If it returns CHANGESET_REPLACE, set *pbRetry
196899** to true before returning. In this case the caller will invoke this function
196900** again, this time with pbRetry set to NULL.
196901**
196902** If argument pbReplace is NULL and a CHANGESET_CONFLICT conflict is
196903** encountered invoke the conflict handler with CHANGESET_CONSTRAINT instead.
196904** Or, if pbReplace is not NULL, invoke it with CHANGESET_CONFLICT. If such
196905** an invocation returns SQLITE_CHANGESET_REPLACE, set *pbReplace to true
196906** before retrying. In this case the caller attempts to remove the conflicting
196907** row before invoking this function again, this time with pbReplace set
196908** to NULL.
196909**
196910** If any conflict handler returns SQLITE_CHANGESET_ABORT, this function
196911** returns SQLITE_ABORT. Otherwise, if no error occurs, SQLITE_OK is
196912** returned.
196913*/
196914static int sessionApplyOneOp(
196915 sqlite3_changeset_iter *pIter, /* Changeset iterator */
196916 SessionApplyCtx *p, /* changeset_apply() context */
196917 int(*xConflict)(void *, int, sqlite3_changeset_iter *),
196918 void *pCtx, /* First argument for the conflict handler */
196919 int *pbReplace, /* OUT: True to remove PK row and retry */
196920 int *pbRetry /* OUT: True to retry. */
196921){
196922 const char *zDummy;
196923 int op;
196924 int nCol;
196925 int rc = SQLITE_OK;
196926
196927 assert( p->pDelete && p->pUpdate && p->pInsert && p->pSelect );
196928 assert( p->azCol && p->abPK );
196929 assert( !pbReplace || *pbReplace==0 );
196930
196931 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
196932
196933 if( op==SQLITE_DELETE ){
196934
196935 /* Bind values to the DELETE statement. If conflict handling is required,
196936 ** bind values for all columns and set bound variable (nCol+1) to true.
196937 ** Or, if conflict handling is not required, bind just the PK column
196938 ** values and, if it exists, set (nCol+1) to false. Conflict handling
196939 ** is not required if:
196940 **
196941 ** * this is a patchset, or
196942 ** * (pbRetry==0), or
196943 ** * all columns of the table are PK columns (in this case there is
196944 ** no (nCol+1) variable to bind to).
196945 */
196946 u8 *abPK = (pIter->bPatchset ? p->abPK : 0);
196947 rc = sessionBindRow(pIter, sqlite3changeset_old, nCol, abPK, p->pDelete);
196948 if( rc==SQLITE_OK && sqlite3_bind_parameter_count(p->pDelete)>nCol ){
196949 rc = sqlite3_bind_int(p->pDelete, nCol+1, (pbRetry==0 || abPK));
196950 }
196951 if( rc!=SQLITE_OK ) return rc;
196952
196953 sqlite3_step(p->pDelete);
196954 rc = sqlite3_reset(p->pDelete);
196955 if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
196956 rc = sessionConflictHandler(
196957 SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
196958 );
196959 }else if( (rc&0xff)==SQLITE_CONSTRAINT ){
196960 rc = sessionConflictHandler(
196961 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
196962 );
196963 }
196964
196965 }else if( op==SQLITE_UPDATE ){
196966 int i;
196967
196968 /* Bind values to the UPDATE statement. */
196969 for(i=0; rc==SQLITE_OK && i<nCol; i++){
196970 sqlite3_value *pOld = sessionChangesetOld(pIter, i);
196971 sqlite3_value *pNew = sessionChangesetNew(pIter, i);
196972
196973 sqlite3_bind_int(p->pUpdate, i*3+2, !!pNew);
196974 if( pOld ){
196975 rc = sessionBindValue(p->pUpdate, i*3+1, pOld);
196976 }
196977 if( rc==SQLITE_OK && pNew ){
196978 rc = sessionBindValue(p->pUpdate, i*3+3, pNew);
196979 }
196980 }
196981 if( rc==SQLITE_OK ){
196982 sqlite3_bind_int(p->pUpdate, nCol*3+1, pbRetry==0 || pIter->bPatchset);
196983 }
196984 if( rc!=SQLITE_OK ) return rc;
196985
196986 /* Attempt the UPDATE. In the case of a NOTFOUND or DATA conflict,
196987 ** the result will be SQLITE_OK with 0 rows modified. */
196988 sqlite3_step(p->pUpdate);
196989 rc = sqlite3_reset(p->pUpdate);
196990
196991 if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
196992 /* A NOTFOUND or DATA error. Search the table to see if it contains
196993 ** a row with a matching primary key. If so, this is a DATA conflict.
196994 ** Otherwise, if there is no primary key match, it is a NOTFOUND. */
196995
196996 rc = sessionConflictHandler(
196997 SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
196998 );
196999
197000 }else if( (rc&0xff)==SQLITE_CONSTRAINT ){
197001 /* This is always a CONSTRAINT conflict. */
197002 rc = sessionConflictHandler(
197003 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
197004 );
197005 }
197006
197007 }else{
197008 assert( op==SQLITE_INSERT );
197009 if( p->bStat1 ){
197010 /* Check if there is a conflicting row. For sqlite_stat1, this needs
197011 ** to be done using a SELECT, as there is no PRIMARY KEY in the
197012 ** database schema to throw an exception if a duplicate is inserted. */
197013 rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect);
197014 if( rc==SQLITE_ROW ){
197015 rc = SQLITE_CONSTRAINT;
197016 sqlite3_reset(p->pSelect);
197017 }
197018 }
197019
197020 if( rc==SQLITE_OK ){
197021 rc = sessionBindRow(pIter, sqlite3changeset_new, nCol, 0, p->pInsert);
197022 if( rc!=SQLITE_OK ) return rc;
197023
197024 sqlite3_step(p->pInsert);
197025 rc = sqlite3_reset(p->pInsert);
197026 }
197027
197028 if( (rc&0xff)==SQLITE_CONSTRAINT ){
197029 rc = sessionConflictHandler(
197030 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, pbReplace
197031 );
197032 }
197033 }
197034
197035 return rc;
197036}
197037
197038/*
197039** Attempt to apply the change that the iterator passed as the first argument
197040** currently points to to the database. If a conflict is encountered, invoke
197041** the conflict handler callback.
197042**
197043** The difference between this function and sessionApplyOne() is that this
197044** function handles the case where the conflict-handler is invoked and
197045** returns SQLITE_CHANGESET_REPLACE - indicating that the change should be
197046** retried in some manner.
197047*/
197048static int sessionApplyOneWithRetry(
197049 sqlite3 *db, /* Apply change to "main" db of this handle */
197050 sqlite3_changeset_iter *pIter, /* Changeset iterator to read change from */
197051 SessionApplyCtx *pApply, /* Apply context */
197052 int(*xConflict)(void*, int, sqlite3_changeset_iter*),
197053 void *pCtx /* First argument passed to xConflict */
197054){
197055 int bReplace = 0;
197056 int bRetry = 0;
197057 int rc;
197058
197059 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, &bReplace, &bRetry);
197060 if( rc==SQLITE_OK ){
197061 /* If the bRetry flag is set, the change has not been applied due to an
197062 ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and
197063 ** a row with the correct PK is present in the db, but one or more other
197064 ** fields do not contain the expected values) and the conflict handler
197065 ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation,
197066 ** but pass NULL as the final argument so that sessionApplyOneOp() ignores
197067 ** the SQLITE_CHANGESET_DATA problem. */
197068 if( bRetry ){
197069 assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE );
197070 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
197071 }
197072
197073 /* If the bReplace flag is set, the change is an INSERT that has not
197074 ** been performed because the database already contains a row with the
197075 ** specified primary key and the conflict handler returned
197076 ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row
197077 ** before reattempting the INSERT. */
197078 else if( bReplace ){
197079 assert( pIter->op==SQLITE_INSERT );
197080 rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
197081 if( rc==SQLITE_OK ){
197082 rc = sessionBindRow(pIter,
197083 sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
197084 sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
197085 }
197086 if( rc==SQLITE_OK ){
197087 sqlite3_step(pApply->pDelete);
197088 rc = sqlite3_reset(pApply->pDelete);
197089 }
197090 if( rc==SQLITE_OK ){
197091 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
197092 }
197093 if( rc==SQLITE_OK ){
197094 rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
197095 }
197096 }
197097 }
197098
197099 return rc;
197100}
197101
197102/*
197103** Retry the changes accumulated in the pApply->constraints buffer.
197104*/
197105static int sessionRetryConstraints(
197106 sqlite3 *db,
197107 int bPatchset,
197108 const char *zTab,
197109 SessionApplyCtx *pApply,
197110 int(*xConflict)(void*, int, sqlite3_changeset_iter*),
197111 void *pCtx /* First argument passed to xConflict */
197112){
197113 int rc = SQLITE_OK;
197114
197115 while( pApply->constraints.nBuf ){
197116 sqlite3_changeset_iter *pIter2 = 0;
197117 SessionBuffer cons = pApply->constraints;
197118 memset(&pApply->constraints, 0, sizeof(SessionBuffer));
197119
197120 rc = sessionChangesetStart(&pIter2, 0, 0, cons.nBuf, cons.aBuf, 0);
197121 if( rc==SQLITE_OK ){
197122 int nByte = 2*pApply->nCol*sizeof(sqlite3_value*);
197123 int rc2;
197124 pIter2->bPatchset = bPatchset;
197125 pIter2->zTab = (char*)zTab;
197126 pIter2->nCol = pApply->nCol;
197127 pIter2->abPK = pApply->abPK;
197128 sessionBufferGrow(&pIter2->tblhdr, nByte, &rc);
197129 pIter2->apValue = (sqlite3_value**)pIter2->tblhdr.aBuf;
197130 if( rc==SQLITE_OK ) memset(pIter2->apValue, 0, nByte);
197131
197132 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter2) ){
197133 rc = sessionApplyOneWithRetry(db, pIter2, pApply, xConflict, pCtx);
197134 }
197135
197136 rc2 = sqlite3changeset_finalize(pIter2);
197137 if( rc==SQLITE_OK ) rc = rc2;
197138 }
197139 assert( pApply->bDeferConstraints || pApply->constraints.nBuf==0 );
197140
197141 sqlite3_free(cons.aBuf);
197142 if( rc!=SQLITE_OK ) break;
197143 if( pApply->constraints.nBuf>=cons.nBuf ){
197144 /* No progress was made on the last round. */
197145 pApply->bDeferConstraints = 0;
197146 }
197147 }
197148
197149 return rc;
197150}
197151
197152/*
197153** Argument pIter is a changeset iterator that has been initialized, but
197154** not yet passed to sqlite3changeset_next(). This function applies the
197155** changeset to the main database attached to handle "db". The supplied
197156** conflict handler callback is invoked to resolve any conflicts encountered
197157** while applying the change.
197158*/
197159static int sessionChangesetApply(
197160 sqlite3 *db, /* Apply change to "main" db of this handle */
197161 sqlite3_changeset_iter *pIter, /* Changeset to apply */
197162 int(*xFilter)(
197163 void *pCtx, /* Copy of sixth arg to _apply() */
197164 const char *zTab /* Table name */
197165 ),
197166 int(*xConflict)(
197167 void *pCtx, /* Copy of fifth arg to _apply() */
197168 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
197169 sqlite3_changeset_iter *p /* Handle describing change and conflict */
197170 ),
197171 void *pCtx, /* First argument passed to xConflict */
197172 void **ppRebase, int *pnRebase, /* OUT: Rebase information */
197173 int flags /* SESSION_APPLY_XXX flags */
197174){
197175 int schemaMismatch = 0;
197176 int rc = SQLITE_OK; /* Return code */
197177 const char *zTab = 0; /* Name of current table */
197178 int nTab = 0; /* Result of sqlite3Strlen30(zTab) */
197179 SessionApplyCtx sApply; /* changeset_apply() context object */
197180 int bPatchset;
197181
197182 assert( xConflict!=0 );
197183
197184 pIter->in.bNoDiscard = 1;
197185 memset(&sApply, 0, sizeof(sApply));
197186 sApply.bRebase = (ppRebase && pnRebase);
197187 sqlite3_mutex_enter(sqlite3_db_mutex(db));
197188 if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
197189 rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0);
197190 }
197191 if( rc==SQLITE_OK ){
197192 rc = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 1", 0, 0, 0);
197193 }
197194 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter) ){
197195 int nCol;
197196 int op;
197197 const char *zNew;
197198
197199 sqlite3changeset_op(pIter, &zNew, &nCol, &op, 0);
197200
197201 if( zTab==0 || sqlite3_strnicmp(zNew, zTab, nTab+1) ){
197202 u8 *abPK;
197203
197204 rc = sessionRetryConstraints(
197205 db, pIter->bPatchset, zTab, &sApply, xConflict, pCtx
197206 );
197207 if( rc!=SQLITE_OK ) break;
197208
197209 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
197210 sqlite3_finalize(sApply.pDelete);
197211 sqlite3_finalize(sApply.pUpdate);
197212 sqlite3_finalize(sApply.pInsert);
197213 sqlite3_finalize(sApply.pSelect);
197214 sApply.db = db;
197215 sApply.pDelete = 0;
197216 sApply.pUpdate = 0;
197217 sApply.pInsert = 0;
197218 sApply.pSelect = 0;
197219 sApply.nCol = 0;
197220 sApply.azCol = 0;
197221 sApply.abPK = 0;
197222 sApply.bStat1 = 0;
197223 sApply.bDeferConstraints = 1;
197224 sApply.bRebaseStarted = 0;
197225 memset(&sApply.constraints, 0, sizeof(SessionBuffer));
197226
197227 /* If an xFilter() callback was specified, invoke it now. If the
197228 ** xFilter callback returns zero, skip this table. If it returns
197229 ** non-zero, proceed. */
197230 schemaMismatch = (xFilter && (0==xFilter(pCtx, zNew)));
197231 if( schemaMismatch ){
197232 zTab = sqlite3_mprintf("%s", zNew);
197233 if( zTab==0 ){
197234 rc = SQLITE_NOMEM;
197235 break;
197236 }
197237 nTab = (int)strlen(zTab);
197238 sApply.azCol = (const char **)zTab;
197239 }else{
197240 int nMinCol = 0;
197241 int i;
197242
197243 sqlite3changeset_pk(pIter, &abPK, 0);
197244 rc = sessionTableInfo(
197245 db, "main", zNew, &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK
197246 );
197247 if( rc!=SQLITE_OK ) break;
197248 for(i=0; i<sApply.nCol; i++){
197249 if( sApply.abPK[i] ) nMinCol = i+1;
197250 }
197251
197252 if( sApply.nCol==0 ){
197253 schemaMismatch = 1;
197254 sqlite3_log(SQLITE_SCHEMA,
197255 "sqlite3changeset_apply(): no such table: %s", zTab
197256 );
197257 }
197258 else if( sApply.nCol<nCol ){
197259 schemaMismatch = 1;
197260 sqlite3_log(SQLITE_SCHEMA,
197261 "sqlite3changeset_apply(): table %s has %d columns, "
197262 "expected %d or more",
197263 zTab, sApply.nCol, nCol
197264 );
197265 }
197266 else if( nCol<nMinCol || memcmp(sApply.abPK, abPK, nCol)!=0 ){
197267 schemaMismatch = 1;
197268 sqlite3_log(SQLITE_SCHEMA, "sqlite3changeset_apply(): "
197269 "primary key mismatch for table %s", zTab
197270 );
197271 }
197272 else{
197273 sApply.nCol = nCol;
197274 if( 0==sqlite3_stricmp(zTab, "sqlite_stat1") ){
197275 if( (rc = sessionStat1Sql(db, &sApply) ) ){
197276 break;
197277 }
197278 sApply.bStat1 = 1;
197279 }else{
197280 if((rc = sessionSelectRow(db, zTab, &sApply))
197281 || (rc = sessionUpdateRow(db, zTab, &sApply))
197282 || (rc = sessionDeleteRow(db, zTab, &sApply))
197283 || (rc = sessionInsertRow(db, zTab, &sApply))
197284 ){
197285 break;
197286 }
197287 sApply.bStat1 = 0;
197288 }
197289 }
197290 nTab = sqlite3Strlen30(zTab);
197291 }
197292 }
197293
197294 /* If there is a schema mismatch on the current table, proceed to the
197295 ** next change. A log message has already been issued. */
197296 if( schemaMismatch ) continue;
197297
197298 rc = sessionApplyOneWithRetry(db, pIter, &sApply, xConflict, pCtx);
197299 }
197300
197301 bPatchset = pIter->bPatchset;
197302 if( rc==SQLITE_OK ){
197303 rc = sqlite3changeset_finalize(pIter);
197304 }else{
197305 sqlite3changeset_finalize(pIter);
197306 }
197307
197308 if( rc==SQLITE_OK ){
197309 rc = sessionRetryConstraints(db, bPatchset, zTab, &sApply, xConflict, pCtx);
197310 }
197311
197312 if( rc==SQLITE_OK ){
197313 int nFk, notUsed;
197314 sqlite3_db_status(db, SQLITE_DBSTATUS_DEFERRED_FKS, &nFk, &notUsed, 0);
197315 if( nFk!=0 ){
197316 int res = SQLITE_CHANGESET_ABORT;
197317 sqlite3_changeset_iter sIter;
197318 memset(&sIter, 0, sizeof(sIter));
197319 sIter.nCol = nFk;
197320 res = xConflict(pCtx, SQLITE_CHANGESET_FOREIGN_KEY, &sIter);
197321 if( res!=SQLITE_CHANGESET_OMIT ){
197322 rc = SQLITE_CONSTRAINT;
197323 }
197324 }
197325 }
197326 sqlite3_exec(db, "PRAGMA defer_foreign_keys = 0", 0, 0, 0);
197327
197328 if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
197329 if( rc==SQLITE_OK ){
197330 rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
197331 }else{
197332 sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
197333 sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
197334 }
197335 }
197336
197337 assert( sApply.bRebase || sApply.rebase.nBuf==0 );
197338 if( rc==SQLITE_OK && bPatchset==0 && sApply.bRebase ){
197339 *ppRebase = (void*)sApply.rebase.aBuf;
197340 *pnRebase = sApply.rebase.nBuf;
197341 sApply.rebase.aBuf = 0;
197342 }
197343 sqlite3_finalize(sApply.pInsert);
197344 sqlite3_finalize(sApply.pDelete);
197345 sqlite3_finalize(sApply.pUpdate);
197346 sqlite3_finalize(sApply.pSelect);
197347 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
197348 sqlite3_free((char*)sApply.constraints.aBuf);
197349 sqlite3_free((char*)sApply.rebase.aBuf);
197350 sqlite3_mutex_leave(sqlite3_db_mutex(db));
197351 return rc;
197352}
197353
197354/*
197355** Apply the changeset passed via pChangeset/nChangeset to the main
197356** database attached to handle "db".
197357*/
197358SQLITE_API int sqlite3changeset_apply_v2(
197359 sqlite3 *db, /* Apply change to "main" db of this handle */
197360 int nChangeset, /* Size of changeset in bytes */
197361 void *pChangeset, /* Changeset blob */
197362 int(*xFilter)(
197363 void *pCtx, /* Copy of sixth arg to _apply() */
197364 const char *zTab /* Table name */
197365 ),
197366 int(*xConflict)(
197367 void *pCtx, /* Copy of sixth arg to _apply() */
197368 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
197369 sqlite3_changeset_iter *p /* Handle describing change and conflict */
197370 ),
197371 void *pCtx, /* First argument passed to xConflict */
197372 void **ppRebase, int *pnRebase,
197373 int flags
197374){
197375 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
197376 int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
197377 int rc = sessionChangesetStart(&pIter, 0, 0, nChangeset, pChangeset,bInverse);
197378 if( rc==SQLITE_OK ){
197379 rc = sessionChangesetApply(
197380 db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
197381 );
197382 }
197383 return rc;
197384}
197385
197386/*
197387** Apply the changeset passed via pChangeset/nChangeset to the main database
197388** attached to handle "db". Invoke the supplied conflict handler callback
197389** to resolve any conflicts encountered while applying the change.
197390*/
197391SQLITE_API int sqlite3changeset_apply(
197392 sqlite3 *db, /* Apply change to "main" db of this handle */
197393 int nChangeset, /* Size of changeset in bytes */
197394 void *pChangeset, /* Changeset blob */
197395 int(*xFilter)(
197396 void *pCtx, /* Copy of sixth arg to _apply() */
197397 const char *zTab /* Table name */
197398 ),
197399 int(*xConflict)(
197400 void *pCtx, /* Copy of fifth arg to _apply() */
197401 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
197402 sqlite3_changeset_iter *p /* Handle describing change and conflict */
197403 ),
197404 void *pCtx /* First argument passed to xConflict */
197405){
197406 return sqlite3changeset_apply_v2(
197407 db, nChangeset, pChangeset, xFilter, xConflict, pCtx, 0, 0, 0
197408 );
197409}
197410
197411/*
197412** Apply the changeset passed via xInput/pIn to the main database
197413** attached to handle "db". Invoke the supplied conflict handler callback
197414** to resolve any conflicts encountered while applying the change.
197415*/
197416SQLITE_API int sqlite3changeset_apply_v2_strm(
197417 sqlite3 *db, /* Apply change to "main" db of this handle */
197418 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
197419 void *pIn, /* First arg for xInput */
197420 int(*xFilter)(
197421 void *pCtx, /* Copy of sixth arg to _apply() */
197422 const char *zTab /* Table name */
197423 ),
197424 int(*xConflict)(
197425 void *pCtx, /* Copy of sixth arg to _apply() */
197426 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
197427 sqlite3_changeset_iter *p /* Handle describing change and conflict */
197428 ),
197429 void *pCtx, /* First argument passed to xConflict */
197430 void **ppRebase, int *pnRebase,
197431 int flags
197432){
197433 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
197434 int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
197435 int rc = sessionChangesetStart(&pIter, xInput, pIn, 0, 0, bInverse);
197436 if( rc==SQLITE_OK ){
197437 rc = sessionChangesetApply(
197438 db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
197439 );
197440 }
197441 return rc;
197442}
197443SQLITE_API int sqlite3changeset_apply_strm(
197444 sqlite3 *db, /* Apply change to "main" db of this handle */
197445 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
197446 void *pIn, /* First arg for xInput */
197447 int(*xFilter)(
197448 void *pCtx, /* Copy of sixth arg to _apply() */
197449 const char *zTab /* Table name */
197450 ),
197451 int(*xConflict)(
197452 void *pCtx, /* Copy of sixth arg to _apply() */
197453 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
197454 sqlite3_changeset_iter *p /* Handle describing change and conflict */
197455 ),
197456 void *pCtx /* First argument passed to xConflict */
197457){
197458 return sqlite3changeset_apply_v2_strm(
197459 db, xInput, pIn, xFilter, xConflict, pCtx, 0, 0, 0
197460 );
197461}
197462
197463/*
197464** sqlite3_changegroup handle.
197465*/
197466struct sqlite3_changegroup {
197467 int rc; /* Error code */
197468 int bPatch; /* True to accumulate patchsets */
197469 SessionTable *pList; /* List of tables in current patch */
197470};
197471
197472/*
197473** This function is called to merge two changes to the same row together as
197474** part of an sqlite3changeset_concat() operation. A new change object is
197475** allocated and a pointer to it stored in *ppNew.
197476*/
197477static int sessionChangeMerge(
197478 SessionTable *pTab, /* Table structure */
197479 int bRebase, /* True for a rebase hash-table */
197480 int bPatchset, /* True for patchsets */
197481 SessionChange *pExist, /* Existing change */
197482 int op2, /* Second change operation */
197483 int bIndirect, /* True if second change is indirect */
197484 u8 *aRec, /* Second change record */
197485 int nRec, /* Number of bytes in aRec */
197486 SessionChange **ppNew /* OUT: Merged change */
197487){
197488 SessionChange *pNew = 0;
197489 int rc = SQLITE_OK;
197490
197491 if( !pExist ){
197492 pNew = (SessionChange *)sqlite3_malloc(sizeof(SessionChange) + nRec);
197493 if( !pNew ){
197494 return SQLITE_NOMEM;
197495 }
197496 memset(pNew, 0, sizeof(SessionChange));
197497 pNew->op = op2;
197498 pNew->bIndirect = bIndirect;
197499 pNew->aRecord = (u8*)&pNew[1];
197500 if( bIndirect==0 || bRebase==0 ){
197501 pNew->nRecord = nRec;
197502 memcpy(pNew->aRecord, aRec, nRec);
197503 }else{
197504 int i;
197505 u8 *pIn = aRec;
197506 u8 *pOut = pNew->aRecord;
197507 for(i=0; i<pTab->nCol; i++){
197508 int nIn = sessionSerialLen(pIn);
197509 if( *pIn==0 ){
197510 *pOut++ = 0;
197511 }else if( pTab->abPK[i]==0 ){
197512 *pOut++ = 0xFF;
197513 }else{
197514 memcpy(pOut, pIn, nIn);
197515 pOut += nIn;
197516 }
197517 pIn += nIn;
197518 }
197519 pNew->nRecord = pOut - pNew->aRecord;
197520 }
197521 }else if( bRebase ){
197522 if( pExist->op==SQLITE_DELETE && pExist->bIndirect ){
197523 *ppNew = pExist;
197524 }else{
197525 int nByte = nRec + pExist->nRecord + sizeof(SessionChange);
197526 pNew = (SessionChange*)sqlite3_malloc(nByte);
197527 if( pNew==0 ){
197528 rc = SQLITE_NOMEM;
197529 }else{
197530 int i;
197531 u8 *a1 = pExist->aRecord;
197532 u8 *a2 = aRec;
197533 u8 *pOut;
197534
197535 memset(pNew, 0, nByte);
197536 pNew->bIndirect = bIndirect || pExist->bIndirect;
197537 pNew->op = op2;
197538 pOut = pNew->aRecord = (u8*)&pNew[1];
197539
197540 for(i=0; i<pTab->nCol; i++){
197541 int n1 = sessionSerialLen(a1);
197542 int n2 = sessionSerialLen(a2);
197543 if( *a1==0xFF || (pTab->abPK[i]==0 && bIndirect) ){
197544 *pOut++ = 0xFF;
197545 }else if( *a2==0 ){
197546 memcpy(pOut, a1, n1);
197547 pOut += n1;
197548 }else{
197549 memcpy(pOut, a2, n2);
197550 pOut += n2;
197551 }
197552 a1 += n1;
197553 a2 += n2;
197554 }
197555 pNew->nRecord = pOut - pNew->aRecord;
197556 }
197557 sqlite3_free(pExist);
197558 }
197559 }else{
197560 int op1 = pExist->op;
197561
197562 /*
197563 ** op1=INSERT, op2=INSERT -> Unsupported. Discard op2.
197564 ** op1=INSERT, op2=UPDATE -> INSERT.
197565 ** op1=INSERT, op2=DELETE -> (none)
197566 **
197567 ** op1=UPDATE, op2=INSERT -> Unsupported. Discard op2.
197568 ** op1=UPDATE, op2=UPDATE -> UPDATE.
197569 ** op1=UPDATE, op2=DELETE -> DELETE.
197570 **
197571 ** op1=DELETE, op2=INSERT -> UPDATE.
197572 ** op1=DELETE, op2=UPDATE -> Unsupported. Discard op2.
197573 ** op1=DELETE, op2=DELETE -> Unsupported. Discard op2.
197574 */
197575 if( (op1==SQLITE_INSERT && op2==SQLITE_INSERT)
197576 || (op1==SQLITE_UPDATE && op2==SQLITE_INSERT)
197577 || (op1==SQLITE_DELETE && op2==SQLITE_UPDATE)
197578 || (op1==SQLITE_DELETE && op2==SQLITE_DELETE)
197579 ){
197580 pNew = pExist;
197581 }else if( op1==SQLITE_INSERT && op2==SQLITE_DELETE ){
197582 sqlite3_free(pExist);
197583 assert( pNew==0 );
197584 }else{
197585 u8 *aExist = pExist->aRecord;
197586 int nByte;
197587 u8 *aCsr;
197588
197589 /* Allocate a new SessionChange object. Ensure that the aRecord[]
197590 ** buffer of the new object is large enough to hold any record that
197591 ** may be generated by combining the input records. */
197592 nByte = sizeof(SessionChange) + pExist->nRecord + nRec;
197593 pNew = (SessionChange *)sqlite3_malloc(nByte);
197594 if( !pNew ){
197595 sqlite3_free(pExist);
197596 return SQLITE_NOMEM;
197597 }
197598 memset(pNew, 0, sizeof(SessionChange));
197599 pNew->bIndirect = (bIndirect && pExist->bIndirect);
197600 aCsr = pNew->aRecord = (u8 *)&pNew[1];
197601
197602 if( op1==SQLITE_INSERT ){ /* INSERT + UPDATE */
197603 u8 *a1 = aRec;
197604 assert( op2==SQLITE_UPDATE );
197605 pNew->op = SQLITE_INSERT;
197606 if( bPatchset==0 ) sessionSkipRecord(&a1, pTab->nCol);
197607 sessionMergeRecord(&aCsr, pTab->nCol, aExist, a1);
197608 }else if( op1==SQLITE_DELETE ){ /* DELETE + INSERT */
197609 assert( op2==SQLITE_INSERT );
197610 pNew->op = SQLITE_UPDATE;
197611 if( bPatchset ){
197612 memcpy(aCsr, aRec, nRec);
197613 aCsr += nRec;
197614 }else{
197615 if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aExist, 0,aRec,0) ){
197616 sqlite3_free(pNew);
197617 pNew = 0;
197618 }
197619 }
197620 }else if( op2==SQLITE_UPDATE ){ /* UPDATE + UPDATE */
197621 u8 *a1 = aExist;
197622 u8 *a2 = aRec;
197623 assert( op1==SQLITE_UPDATE );
197624 if( bPatchset==0 ){
197625 sessionSkipRecord(&a1, pTab->nCol);
197626 sessionSkipRecord(&a2, pTab->nCol);
197627 }
197628 pNew->op = SQLITE_UPDATE;
197629 if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aRec, aExist,a1,a2) ){
197630 sqlite3_free(pNew);
197631 pNew = 0;
197632 }
197633 }else{ /* UPDATE + DELETE */
197634 assert( op1==SQLITE_UPDATE && op2==SQLITE_DELETE );
197635 pNew->op = SQLITE_DELETE;
197636 if( bPatchset ){
197637 memcpy(aCsr, aRec, nRec);
197638 aCsr += nRec;
197639 }else{
197640 sessionMergeRecord(&aCsr, pTab->nCol, aRec, aExist);
197641 }
197642 }
197643
197644 if( pNew ){
197645 pNew->nRecord = (int)(aCsr - pNew->aRecord);
197646 }
197647 sqlite3_free(pExist);
197648 }
197649 }
197650
197651 *ppNew = pNew;
197652 return rc;
197653}
197654
197655/*
197656** Add all changes in the changeset traversed by the iterator passed as
197657** the first argument to the changegroup hash tables.
197658*/
197659static int sessionChangesetToHash(
197660 sqlite3_changeset_iter *pIter, /* Iterator to read from */
197661 sqlite3_changegroup *pGrp, /* Changegroup object to add changeset to */
197662 int bRebase /* True if hash table is for rebasing */
197663){
197664 u8 *aRec;
197665 int nRec;
197666 int rc = SQLITE_OK;
197667 SessionTable *pTab = 0;
197668
197669 while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, 0) ){
197670 const char *zNew;
197671 int nCol;
197672 int op;
197673 int iHash;
197674 int bIndirect;
197675 SessionChange *pChange;
197676 SessionChange *pExist = 0;
197677 SessionChange **pp;
197678
197679 if( pGrp->pList==0 ){
197680 pGrp->bPatch = pIter->bPatchset;
197681 }else if( pIter->bPatchset!=pGrp->bPatch ){
197682 rc = SQLITE_ERROR;
197683 break;
197684 }
197685
197686 sqlite3changeset_op(pIter, &zNew, &nCol, &op, &bIndirect);
197687 if( !pTab || sqlite3_stricmp(zNew, pTab->zName) ){
197688 /* Search the list for a matching table */
197689 int nNew = (int)strlen(zNew);
197690 u8 *abPK;
197691
197692 sqlite3changeset_pk(pIter, &abPK, 0);
197693 for(pTab = pGrp->pList; pTab; pTab=pTab->pNext){
197694 if( 0==sqlite3_strnicmp(pTab->zName, zNew, nNew+1) ) break;
197695 }
197696 if( !pTab ){
197697 SessionTable **ppTab;
197698
197699 pTab = sqlite3_malloc(sizeof(SessionTable) + nCol + nNew+1);
197700 if( !pTab ){
197701 rc = SQLITE_NOMEM;
197702 break;
197703 }
197704 memset(pTab, 0, sizeof(SessionTable));
197705 pTab->nCol = nCol;
197706 pTab->abPK = (u8*)&pTab[1];
197707 memcpy(pTab->abPK, abPK, nCol);
197708 pTab->zName = (char*)&pTab->abPK[nCol];
197709 memcpy(pTab->zName, zNew, nNew+1);
197710
197711 /* The new object must be linked on to the end of the list, not
197712 ** simply added to the start of it. This is to ensure that the
197713 ** tables within the output of sqlite3changegroup_output() are in
197714 ** the right order. */
197715 for(ppTab=&pGrp->pList; *ppTab; ppTab=&(*ppTab)->pNext);
197716 *ppTab = pTab;
197717 }else if( pTab->nCol!=nCol || memcmp(pTab->abPK, abPK, nCol) ){
197718 rc = SQLITE_SCHEMA;
197719 break;
197720 }
197721 }
197722
197723 if( sessionGrowHash(pIter->bPatchset, pTab) ){
197724 rc = SQLITE_NOMEM;
197725 break;
197726 }
197727 iHash = sessionChangeHash(
197728 pTab, (pIter->bPatchset && op==SQLITE_DELETE), aRec, pTab->nChange
197729 );
197730
197731 /* Search for existing entry. If found, remove it from the hash table.
197732 ** Code below may link it back in.
197733 */
197734 for(pp=&pTab->apChange[iHash]; *pp; pp=&(*pp)->pNext){
197735 int bPkOnly1 = 0;
197736 int bPkOnly2 = 0;
197737 if( pIter->bPatchset ){
197738 bPkOnly1 = (*pp)->op==SQLITE_DELETE;
197739 bPkOnly2 = op==SQLITE_DELETE;
197740 }
197741 if( sessionChangeEqual(pTab, bPkOnly1, (*pp)->aRecord, bPkOnly2, aRec) ){
197742 pExist = *pp;
197743 *pp = (*pp)->pNext;
197744 pTab->nEntry--;
197745 break;
197746 }
197747 }
197748
197749 rc = sessionChangeMerge(pTab, bRebase,
197750 pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
197751 );
197752 if( rc ) break;
197753 if( pChange ){
197754 pChange->pNext = pTab->apChange[iHash];
197755 pTab->apChange[iHash] = pChange;
197756 pTab->nEntry++;
197757 }
197758 }
197759
197760 if( rc==SQLITE_OK ) rc = pIter->rc;
197761 return rc;
197762}
197763
197764/*
197765** Serialize a changeset (or patchset) based on all changesets (or patchsets)
197766** added to the changegroup object passed as the first argument.
197767**
197768** If xOutput is not NULL, then the changeset/patchset is returned to the
197769** user via one or more calls to xOutput, as with the other streaming
197770** interfaces.
197771**
197772** Or, if xOutput is NULL, then (*ppOut) is populated with a pointer to a
197773** buffer containing the output changeset before this function returns. In
197774** this case (*pnOut) is set to the size of the output buffer in bytes. It
197775** is the responsibility of the caller to free the output buffer using
197776** sqlite3_free() when it is no longer required.
197777**
197778** If successful, SQLITE_OK is returned. Or, if an error occurs, an SQLite
197779** error code. If an error occurs and xOutput is NULL, (*ppOut) and (*pnOut)
197780** are both set to 0 before returning.
197781*/
197782static int sessionChangegroupOutput(
197783 sqlite3_changegroup *pGrp,
197784 int (*xOutput)(void *pOut, const void *pData, int nData),
197785 void *pOut,
197786 int *pnOut,
197787 void **ppOut
197788){
197789 int rc = SQLITE_OK;
197790 SessionBuffer buf = {0, 0, 0};
197791 SessionTable *pTab;
197792 assert( xOutput==0 || (ppOut==0 && pnOut==0) );
197793
197794 /* Create the serialized output changeset based on the contents of the
197795 ** hash tables attached to the SessionTable objects in list p->pList.
197796 */
197797 for(pTab=pGrp->pList; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
197798 int i;
197799 if( pTab->nEntry==0 ) continue;
197800
197801 sessionAppendTableHdr(&buf, pGrp->bPatch, pTab, &rc);
197802 for(i=0; i<pTab->nChange; i++){
197803 SessionChange *p;
197804 for(p=pTab->apChange[i]; p; p=p->pNext){
197805 sessionAppendByte(&buf, p->op, &rc);
197806 sessionAppendByte(&buf, p->bIndirect, &rc);
197807 sessionAppendBlob(&buf, p->aRecord, p->nRecord, &rc);
197808 if( rc==SQLITE_OK && xOutput && buf.nBuf>=sessions_strm_chunk_size ){
197809 rc = xOutput(pOut, buf.aBuf, buf.nBuf);
197810 buf.nBuf = 0;
197811 }
197812 }
197813 }
197814 }
197815
197816 if( rc==SQLITE_OK ){
197817 if( xOutput ){
197818 if( buf.nBuf>0 ) rc = xOutput(pOut, buf.aBuf, buf.nBuf);
197819 }else{
197820 *ppOut = buf.aBuf;
197821 *pnOut = buf.nBuf;
197822 buf.aBuf = 0;
197823 }
197824 }
197825 sqlite3_free(buf.aBuf);
197826
197827 return rc;
197828}
197829
197830/*
197831** Allocate a new, empty, sqlite3_changegroup.
197832*/
197833SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp){
197834 int rc = SQLITE_OK; /* Return code */
197835 sqlite3_changegroup *p; /* New object */
197836 p = (sqlite3_changegroup*)sqlite3_malloc(sizeof(sqlite3_changegroup));
197837 if( p==0 ){
197838 rc = SQLITE_NOMEM;
197839 }else{
197840 memset(p, 0, sizeof(sqlite3_changegroup));
197841 }
197842 *pp = p;
197843 return rc;
197844}
197845
197846/*
197847** Add the changeset currently stored in buffer pData, size nData bytes,
197848** to changeset-group p.
197849*/
197850SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup *pGrp, int nData, void *pData){
197851 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
197852 int rc; /* Return code */
197853
197854 rc = sqlite3changeset_start(&pIter, nData, pData);
197855 if( rc==SQLITE_OK ){
197856 rc = sessionChangesetToHash(pIter, pGrp, 0);
197857 }
197858 sqlite3changeset_finalize(pIter);
197859 return rc;
197860}
197861
197862/*
197863** Obtain a buffer containing a changeset representing the concatenation
197864** of all changesets added to the group so far.
197865*/
197866SQLITE_API int sqlite3changegroup_output(
197867 sqlite3_changegroup *pGrp,
197868 int *pnData,
197869 void **ppData
197870){
197871 return sessionChangegroupOutput(pGrp, 0, 0, pnData, ppData);
197872}
197873
197874/*
197875** Streaming versions of changegroup_add().
197876*/
197877SQLITE_API int sqlite3changegroup_add_strm(
197878 sqlite3_changegroup *pGrp,
197879 int (*xInput)(void *pIn, void *pData, int *pnData),
197880 void *pIn
197881){
197882 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
197883 int rc; /* Return code */
197884
197885 rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
197886 if( rc==SQLITE_OK ){
197887 rc = sessionChangesetToHash(pIter, pGrp, 0);
197888 }
197889 sqlite3changeset_finalize(pIter);
197890 return rc;
197891}
197892
197893/*
197894** Streaming versions of changegroup_output().
197895*/
197896SQLITE_API int sqlite3changegroup_output_strm(
197897 sqlite3_changegroup *pGrp,
197898 int (*xOutput)(void *pOut, const void *pData, int nData),
197899 void *pOut
197900){
197901 return sessionChangegroupOutput(pGrp, xOutput, pOut, 0, 0);
197902}
197903
197904/*
197905** Delete a changegroup object.
197906*/
197907SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){
197908 if( pGrp ){
197909 sessionDeleteTable(pGrp->pList);
197910 sqlite3_free(pGrp);
197911 }
197912}
197913
197914/*
197915** Combine two changesets together.
197916*/
197917SQLITE_API int sqlite3changeset_concat(
197918 int nLeft, /* Number of bytes in lhs input */
197919 void *pLeft, /* Lhs input changeset */
197920 int nRight /* Number of bytes in rhs input */,
197921 void *pRight, /* Rhs input changeset */
197922 int *pnOut, /* OUT: Number of bytes in output changeset */
197923 void **ppOut /* OUT: changeset (left <concat> right) */
197924){
197925 sqlite3_changegroup *pGrp;
197926 int rc;
197927
197928 rc = sqlite3changegroup_new(&pGrp);
197929 if( rc==SQLITE_OK ){
197930 rc = sqlite3changegroup_add(pGrp, nLeft, pLeft);
197931 }
197932 if( rc==SQLITE_OK ){
197933 rc = sqlite3changegroup_add(pGrp, nRight, pRight);
197934 }
197935 if( rc==SQLITE_OK ){
197936 rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
197937 }
197938 sqlite3changegroup_delete(pGrp);
197939
197940 return rc;
197941}
197942
197943/*
197944** Streaming version of sqlite3changeset_concat().
197945*/
197946SQLITE_API int sqlite3changeset_concat_strm(
197947 int (*xInputA)(void *pIn, void *pData, int *pnData),
197948 void *pInA,
197949 int (*xInputB)(void *pIn, void *pData, int *pnData),
197950 void *pInB,
197951 int (*xOutput)(void *pOut, const void *pData, int nData),
197952 void *pOut
197953){
197954 sqlite3_changegroup *pGrp;
197955 int rc;
197956
197957 rc = sqlite3changegroup_new(&pGrp);
197958 if( rc==SQLITE_OK ){
197959 rc = sqlite3changegroup_add_strm(pGrp, xInputA, pInA);
197960 }
197961 if( rc==SQLITE_OK ){
197962 rc = sqlite3changegroup_add_strm(pGrp, xInputB, pInB);
197963 }
197964 if( rc==SQLITE_OK ){
197965 rc = sqlite3changegroup_output_strm(pGrp, xOutput, pOut);
197966 }
197967 sqlite3changegroup_delete(pGrp);
197968
197969 return rc;
197970}
197971
197972/*
197973** Changeset rebaser handle.
197974*/
197975struct sqlite3_rebaser {
197976 sqlite3_changegroup grp; /* Hash table */
197977};
197978
197979/*
197980** Buffers a1 and a2 must both contain a sessions module record nCol
197981** fields in size. This function appends an nCol sessions module
197982** record to buffer pBuf that is a copy of a1, except that for
197983** each field that is undefined in a1[], swap in the field from a2[].
197984*/
197985static void sessionAppendRecordMerge(
197986 SessionBuffer *pBuf, /* Buffer to append to */
197987 int nCol, /* Number of columns in each record */
197988 u8 *a1, int n1, /* Record 1 */
197989 u8 *a2, int n2, /* Record 2 */
197990 int *pRc /* IN/OUT: error code */
197991){
197992 sessionBufferGrow(pBuf, n1+n2, pRc);
197993 if( *pRc==SQLITE_OK ){
197994 int i;
197995 u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
197996 for(i=0; i<nCol; i++){
197997 int nn1 = sessionSerialLen(a1);
197998 int nn2 = sessionSerialLen(a2);
197999 if( *a1==0 || *a1==0xFF ){
198000 memcpy(pOut, a2, nn2);
198001 pOut += nn2;
198002 }else{
198003 memcpy(pOut, a1, nn1);
198004 pOut += nn1;
198005 }
198006 a1 += nn1;
198007 a2 += nn2;
198008 }
198009
198010 pBuf->nBuf = pOut-pBuf->aBuf;
198011 assert( pBuf->nBuf<=pBuf->nAlloc );
198012 }
198013}
198014
198015/*
198016** This function is called when rebasing a local UPDATE change against one
198017** or more remote UPDATE changes. The aRec/nRec buffer contains the current
198018** old.* and new.* records for the change. The rebase buffer (a single
198019** record) is in aChange/nChange. The rebased change is appended to buffer
198020** pBuf.
198021**
198022** Rebasing the UPDATE involves:
198023**
198024** * Removing any changes to fields for which the corresponding field
198025** in the rebase buffer is set to "replaced" (type 0xFF). If this
198026** means the UPDATE change updates no fields, nothing is appended
198027** to the output buffer.
198028**
198029** * For each field modified by the local change for which the
198030** corresponding field in the rebase buffer is not "undefined" (0x00)
198031** or "replaced" (0xFF), the old.* value is replaced by the value
198032** in the rebase buffer.
198033*/
198034static void sessionAppendPartialUpdate(
198035 SessionBuffer *pBuf, /* Append record here */
198036 sqlite3_changeset_iter *pIter, /* Iterator pointed at local change */
198037 u8 *aRec, int nRec, /* Local change */
198038 u8 *aChange, int nChange, /* Record to rebase against */
198039 int *pRc /* IN/OUT: Return Code */
198040){
198041 sessionBufferGrow(pBuf, 2+nRec+nChange, pRc);
198042 if( *pRc==SQLITE_OK ){
198043 int bData = 0;
198044 u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
198045 int i;
198046 u8 *a1 = aRec;
198047 u8 *a2 = aChange;
198048
198049 *pOut++ = SQLITE_UPDATE;
198050 *pOut++ = pIter->bIndirect;
198051 for(i=0; i<pIter->nCol; i++){
198052 int n1 = sessionSerialLen(a1);
198053 int n2 = sessionSerialLen(a2);
198054 if( pIter->abPK[i] || a2[0]==0 ){
198055 if( !pIter->abPK[i] ) bData = 1;
198056 memcpy(pOut, a1, n1);
198057 pOut += n1;
198058 }else if( a2[0]!=0xFF ){
198059 bData = 1;
198060 memcpy(pOut, a2, n2);
198061 pOut += n2;
198062 }else{
198063 *pOut++ = '\0';
198064 }
198065 a1 += n1;
198066 a2 += n2;
198067 }
198068 if( bData ){
198069 a2 = aChange;
198070 for(i=0; i<pIter->nCol; i++){
198071 int n1 = sessionSerialLen(a1);
198072 int n2 = sessionSerialLen(a2);
198073 if( pIter->abPK[i] || a2[0]!=0xFF ){
198074 memcpy(pOut, a1, n1);
198075 pOut += n1;
198076 }else{
198077 *pOut++ = '\0';
198078 }
198079 a1 += n1;
198080 a2 += n2;
198081 }
198082 pBuf->nBuf = (pOut - pBuf->aBuf);
198083 }
198084 }
198085}
198086
198087/*
198088** pIter is configured to iterate through a changeset. This function rebases
198089** that changeset according to the current configuration of the rebaser
198090** object passed as the first argument. If no error occurs and argument xOutput
198091** is not NULL, then the changeset is returned to the caller by invoking
198092** xOutput zero or more times and SQLITE_OK returned. Or, if xOutput is NULL,
198093** then (*ppOut) is set to point to a buffer containing the rebased changeset
198094** before this function returns. In this case (*pnOut) is set to the size of
198095** the buffer in bytes. It is the responsibility of the caller to eventually
198096** free the (*ppOut) buffer using sqlite3_free().
198097**
198098** If an error occurs, an SQLite error code is returned. If ppOut and
198099** pnOut are not NULL, then the two output parameters are set to 0 before
198100** returning.
198101*/
198102static int sessionRebase(
198103 sqlite3_rebaser *p, /* Rebaser hash table */
198104 sqlite3_changeset_iter *pIter, /* Input data */
198105 int (*xOutput)(void *pOut, const void *pData, int nData),
198106 void *pOut, /* Context for xOutput callback */
198107 int *pnOut, /* OUT: Number of bytes in output changeset */
198108 void **ppOut /* OUT: Inverse of pChangeset */
198109){
198110 int rc = SQLITE_OK;
198111 u8 *aRec = 0;
198112 int nRec = 0;
198113 int bNew = 0;
198114 SessionTable *pTab = 0;
198115 SessionBuffer sOut = {0,0,0};
198116
198117 while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, &bNew) ){
198118 SessionChange *pChange = 0;
198119 int bDone = 0;
198120
198121 if( bNew ){
198122 const char *zTab = pIter->zTab;
198123 for(pTab=p->grp.pList; pTab; pTab=pTab->pNext){
198124 if( 0==sqlite3_stricmp(pTab->zName, zTab) ) break;
198125 }
198126 bNew = 0;
198127
198128 /* A patchset may not be rebased */
198129 if( pIter->bPatchset ){
198130 rc = SQLITE_ERROR;
198131 }
198132
198133 /* Append a table header to the output for this new table */
198134 sessionAppendByte(&sOut, pIter->bPatchset ? 'P' : 'T', &rc);
198135 sessionAppendVarint(&sOut, pIter->nCol, &rc);
198136 sessionAppendBlob(&sOut, pIter->abPK, pIter->nCol, &rc);
198137 sessionAppendBlob(&sOut,(u8*)pIter->zTab,(int)strlen(pIter->zTab)+1,&rc);
198138 }
198139
198140 if( pTab && rc==SQLITE_OK ){
198141 int iHash = sessionChangeHash(pTab, 0, aRec, pTab->nChange);
198142
198143 for(pChange=pTab->apChange[iHash]; pChange; pChange=pChange->pNext){
198144 if( sessionChangeEqual(pTab, 0, aRec, 0, pChange->aRecord) ){
198145 break;
198146 }
198147 }
198148 }
198149
198150 if( pChange ){
198151 assert( pChange->op==SQLITE_DELETE || pChange->op==SQLITE_INSERT );
198152 switch( pIter->op ){
198153 case SQLITE_INSERT:
198154 if( pChange->op==SQLITE_INSERT ){
198155 bDone = 1;
198156 if( pChange->bIndirect==0 ){
198157 sessionAppendByte(&sOut, SQLITE_UPDATE, &rc);
198158 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
198159 sessionAppendBlob(&sOut, pChange->aRecord, pChange->nRecord, &rc);
198160 sessionAppendBlob(&sOut, aRec, nRec, &rc);
198161 }
198162 }
198163 break;
198164
198165 case SQLITE_UPDATE:
198166 bDone = 1;
198167 if( pChange->op==SQLITE_DELETE ){
198168 if( pChange->bIndirect==0 ){
198169 u8 *pCsr = aRec;
198170 sessionSkipRecord(&pCsr, pIter->nCol);
198171 sessionAppendByte(&sOut, SQLITE_INSERT, &rc);
198172 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
198173 sessionAppendRecordMerge(&sOut, pIter->nCol,
198174 pCsr, nRec-(pCsr-aRec),
198175 pChange->aRecord, pChange->nRecord, &rc
198176 );
198177 }
198178 }else{
198179 sessionAppendPartialUpdate(&sOut, pIter,
198180 aRec, nRec, pChange->aRecord, pChange->nRecord, &rc
198181 );
198182 }
198183 break;
198184
198185 default:
198186 assert( pIter->op==SQLITE_DELETE );
198187 bDone = 1;
198188 if( pChange->op==SQLITE_INSERT ){
198189 sessionAppendByte(&sOut, SQLITE_DELETE, &rc);
198190 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
198191 sessionAppendRecordMerge(&sOut, pIter->nCol,
198192 pChange->aRecord, pChange->nRecord, aRec, nRec, &rc
198193 );
198194 }
198195 break;
198196 }
198197 }
198198
198199 if( bDone==0 ){
198200 sessionAppendByte(&sOut, pIter->op, &rc);
198201 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
198202 sessionAppendBlob(&sOut, aRec, nRec, &rc);
198203 }
198204 if( rc==SQLITE_OK && xOutput && sOut.nBuf>sessions_strm_chunk_size ){
198205 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
198206 sOut.nBuf = 0;
198207 }
198208 if( rc ) break;
198209 }
198210
198211 if( rc!=SQLITE_OK ){
198212 sqlite3_free(sOut.aBuf);
198213 memset(&sOut, 0, sizeof(sOut));
198214 }
198215
198216 if( rc==SQLITE_OK ){
198217 if( xOutput ){
198218 if( sOut.nBuf>0 ){
198219 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
198220 }
198221 }else{
198222 *ppOut = (void*)sOut.aBuf;
198223 *pnOut = sOut.nBuf;
198224 sOut.aBuf = 0;
198225 }
198226 }
198227 sqlite3_free(sOut.aBuf);
198228 return rc;
198229}
198230
198231/*
198232** Create a new rebaser object.
198233*/
198234SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew){
198235 int rc = SQLITE_OK;
198236 sqlite3_rebaser *pNew;
198237
198238 pNew = sqlite3_malloc(sizeof(sqlite3_rebaser));
198239 if( pNew==0 ){
198240 rc = SQLITE_NOMEM;
198241 }else{
198242 memset(pNew, 0, sizeof(sqlite3_rebaser));
198243 }
198244 *ppNew = pNew;
198245 return rc;
198246}
198247
198248/*
198249** Call this one or more times to configure a rebaser.
198250*/
198251SQLITE_API int sqlite3rebaser_configure(
198252 sqlite3_rebaser *p,
198253 int nRebase, const void *pRebase
198254){
198255 sqlite3_changeset_iter *pIter = 0; /* Iterator opened on pData/nData */
198256 int rc; /* Return code */
198257 rc = sqlite3changeset_start(&pIter, nRebase, (void*)pRebase);
198258 if( rc==SQLITE_OK ){
198259 rc = sessionChangesetToHash(pIter, &p->grp, 1);
198260 }
198261 sqlite3changeset_finalize(pIter);
198262 return rc;
198263}
198264
198265/*
198266** Rebase a changeset according to current rebaser configuration
198267*/
198268SQLITE_API int sqlite3rebaser_rebase(
198269 sqlite3_rebaser *p,
198270 int nIn, const void *pIn,
198271 int *pnOut, void **ppOut
198272){
198273 sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */
198274 int rc = sqlite3changeset_start(&pIter, nIn, (void*)pIn);
198275
198276 if( rc==SQLITE_OK ){
198277 rc = sessionRebase(p, pIter, 0, 0, pnOut, ppOut);
198278 sqlite3changeset_finalize(pIter);
198279 }
198280
198281 return rc;
198282}
198283
198284/*
198285** Rebase a changeset according to current rebaser configuration
198286*/
198287SQLITE_API int sqlite3rebaser_rebase_strm(
198288 sqlite3_rebaser *p,
198289 int (*xInput)(void *pIn, void *pData, int *pnData),
198290 void *pIn,
198291 int (*xOutput)(void *pOut, const void *pData, int nData),
198292 void *pOut
198293){
198294 sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */
198295 int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
198296
198297 if( rc==SQLITE_OK ){
198298 rc = sessionRebase(p, pIter, xOutput, pOut, 0, 0);
198299 sqlite3changeset_finalize(pIter);
198300 }
198301
198302 return rc;
198303}
198304
198305/*
198306** Destroy a rebaser object
198307*/
198308SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p){
198309 if( p ){
198310 sessionDeleteTable(p->grp.pList);
198311 sqlite3_free(p);
198312 }
198313}
198314
198315/*
198316** Global configuration
198317*/
198318SQLITE_API int sqlite3session_config(int op, void *pArg){
198319 int rc = SQLITE_OK;
198320 switch( op ){
198321 case SQLITE_SESSION_CONFIG_STRMSIZE: {
198322 int *pInt = (int*)pArg;
198323 if( *pInt>0 ){
198324 sessions_strm_chunk_size = *pInt;
198325 }
198326 *pInt = sessions_strm_chunk_size;
198327 break;
198328 }
198329 default:
198330 rc = SQLITE_MISUSE;
198331 break;
198332 }
198333 return rc;
198334}
198335
198336#endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */
198337
198338/************** End of sqlite3session.c **************************************/
198339/************** Begin file fts5.c ********************************************/
198340
198341
198342#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5)
198343
198344#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
198345# define NDEBUG 1
198346#endif
198347#if defined(NDEBUG) && defined(SQLITE_DEBUG)
198348# undef NDEBUG
198349#endif
198350
198351/*
198352** 2014 May 31
198353**
198354** The author disclaims copyright to this source code. In place of
198355** a legal notice, here is a blessing:
198356**
198357** May you do good and not evil.
198358** May you find forgiveness for yourself and forgive others.
198359** May you share freely, never taking more than you give.
198360**
198361******************************************************************************
198362**
198363** Interfaces to extend FTS5. Using the interfaces defined in this file,
198364** FTS5 may be extended with:
198365**
198366** * custom tokenizers, and
198367** * custom auxiliary functions.
198368*/
198369
198370
198371#ifndef _FTS5_H
198372#define _FTS5_H
198373
198374/* #include "sqlite3.h" */
198375
198376#if 0
198377extern "C" {
198378#endif
198379
198380/*************************************************************************
198381** CUSTOM AUXILIARY FUNCTIONS
198382**
198383** Virtual table implementations may overload SQL functions by implementing
198384** the sqlite3_module.xFindFunction() method.
198385*/
198386
198387typedef struct Fts5ExtensionApi Fts5ExtensionApi;
198388typedef struct Fts5Context Fts5Context;
198389typedef struct Fts5PhraseIter Fts5PhraseIter;
198390
198391typedef void (*fts5_extension_function)(
198392 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
198393 Fts5Context *pFts, /* First arg to pass to pApi functions */
198394 sqlite3_context *pCtx, /* Context for returning result/error */
198395 int nVal, /* Number of values in apVal[] array */
198396 sqlite3_value **apVal /* Array of trailing arguments */
198397);
198398
198399struct Fts5PhraseIter {
198400 const unsigned char *a;
198401 const unsigned char *b;
198402};
198403
198404/*
198405** EXTENSION API FUNCTIONS
198406**
198407** xUserData(pFts):
198408** Return a copy of the context pointer the extension function was
198409** registered with.
198410**
198411** xColumnTotalSize(pFts, iCol, pnToken):
198412** If parameter iCol is less than zero, set output variable *pnToken
198413** to the total number of tokens in the FTS5 table. Or, if iCol is
198414** non-negative but less than the number of columns in the table, return
198415** the total number of tokens in column iCol, considering all rows in
198416** the FTS5 table.
198417**
198418** If parameter iCol is greater than or equal to the number of columns
198419** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
198420** an OOM condition or IO error), an appropriate SQLite error code is
198421** returned.
198422**
198423** xColumnCount(pFts):
198424** Return the number of columns in the table.
198425**
198426** xColumnSize(pFts, iCol, pnToken):
198427** If parameter iCol is less than zero, set output variable *pnToken
198428** to the total number of tokens in the current row. Or, if iCol is
198429** non-negative but less than the number of columns in the table, set
198430** *pnToken to the number of tokens in column iCol of the current row.
198431**
198432** If parameter iCol is greater than or equal to the number of columns
198433** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
198434** an OOM condition or IO error), an appropriate SQLite error code is
198435** returned.
198436**
198437** This function may be quite inefficient if used with an FTS5 table
198438** created with the "columnsize=0" option.
198439**
198440** xColumnText:
198441** This function attempts to retrieve the text of column iCol of the
198442** current document. If successful, (*pz) is set to point to a buffer
198443** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
198444** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
198445** if an error occurs, an SQLite error code is returned and the final values
198446** of (*pz) and (*pn) are undefined.
198447**
198448** xPhraseCount:
198449** Returns the number of phrases in the current query expression.
198450**
198451** xPhraseSize:
198452** Returns the number of tokens in phrase iPhrase of the query. Phrases
198453** are numbered starting from zero.
198454**
198455** xInstCount:
198456** Set *pnInst to the total number of occurrences of all phrases within
198457** the query within the current row. Return SQLITE_OK if successful, or
198458** an error code (i.e. SQLITE_NOMEM) if an error occurs.
198459**
198460** This API can be quite slow if used with an FTS5 table created with the
198461** "detail=none" or "detail=column" option. If the FTS5 table is created
198462** with either "detail=none" or "detail=column" and "content=" option
198463** (i.e. if it is a contentless table), then this API always returns 0.
198464**
198465** xInst:
198466** Query for the details of phrase match iIdx within the current row.
198467** Phrase matches are numbered starting from zero, so the iIdx argument
198468** should be greater than or equal to zero and smaller than the value
198469** output by xInstCount().
198470**
198471** Usually, output parameter *piPhrase is set to the phrase number, *piCol
198472** to the column in which it occurs and *piOff the token offset of the
198473** first token of the phrase. The exception is if the table was created
198474** with the offsets=0 option specified. In this case *piOff is always
198475** set to -1.
198476**
198477** Returns SQLITE_OK if successful, or an error code (i.e. SQLITE_NOMEM)
198478** if an error occurs.
198479**
198480** This API can be quite slow if used with an FTS5 table created with the
198481** "detail=none" or "detail=column" option.
198482**
198483** xRowid:
198484** Returns the rowid of the current row.
198485**
198486** xTokenize:
198487** Tokenize text using the tokenizer belonging to the FTS5 table.
198488**
198489** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
198490** This API function is used to query the FTS table for phrase iPhrase
198491** of the current query. Specifically, a query equivalent to:
198492**
198493** ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
198494**
198495** with $p set to a phrase equivalent to the phrase iPhrase of the
198496** current query is executed. Any column filter that applies to
198497** phrase iPhrase of the current query is included in $p. For each
198498** row visited, the callback function passed as the fourth argument
198499** is invoked. The context and API objects passed to the callback
198500** function may be used to access the properties of each matched row.
198501** Invoking Api.xUserData() returns a copy of the pointer passed as
198502** the third argument to pUserData.
198503**
198504** If the callback function returns any value other than SQLITE_OK, the
198505** query is abandoned and the xQueryPhrase function returns immediately.
198506** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
198507** Otherwise, the error code is propagated upwards.
198508**
198509** If the query runs to completion without incident, SQLITE_OK is returned.
198510** Or, if some error occurs before the query completes or is aborted by
198511** the callback, an SQLite error code is returned.
198512**
198513**
198514** xSetAuxdata(pFts5, pAux, xDelete)
198515**
198516** Save the pointer passed as the second argument as the extension functions
198517** "auxiliary data". The pointer may then be retrieved by the current or any
198518** future invocation of the same fts5 extension function made as part of
198519** of the same MATCH query using the xGetAuxdata() API.
198520**
198521** Each extension function is allocated a single auxiliary data slot for
198522** each FTS query (MATCH expression). If the extension function is invoked
198523** more than once for a single FTS query, then all invocations share a
198524** single auxiliary data context.
198525**
198526** If there is already an auxiliary data pointer when this function is
198527** invoked, then it is replaced by the new pointer. If an xDelete callback
198528** was specified along with the original pointer, it is invoked at this
198529** point.
198530**
198531** The xDelete callback, if one is specified, is also invoked on the
198532** auxiliary data pointer after the FTS5 query has finished.
198533**
198534** If an error (e.g. an OOM condition) occurs within this function, an
198535** the auxiliary data is set to NULL and an error code returned. If the
198536** xDelete parameter was not NULL, it is invoked on the auxiliary data
198537** pointer before returning.
198538**
198539**
198540** xGetAuxdata(pFts5, bClear)
198541**
198542** Returns the current auxiliary data pointer for the fts5 extension
198543** function. See the xSetAuxdata() method for details.
198544**
198545** If the bClear argument is non-zero, then the auxiliary data is cleared
198546** (set to NULL) before this function returns. In this case the xDelete,
198547** if any, is not invoked.
198548**
198549**
198550** xRowCount(pFts5, pnRow)
198551**
198552** This function is used to retrieve the total number of rows in the table.
198553** In other words, the same value that would be returned by:
198554**
198555** SELECT count(*) FROM ftstable;
198556**
198557** xPhraseFirst()
198558** This function is used, along with type Fts5PhraseIter and the xPhraseNext
198559** method, to iterate through all instances of a single query phrase within
198560** the current row. This is the same information as is accessible via the
198561** xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient
198562** to use, this API may be faster under some circumstances. To iterate
198563** through instances of phrase iPhrase, use the following code:
198564**
198565** Fts5PhraseIter iter;
198566** int iCol, iOff;
198567** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
198568** iCol>=0;
198569** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
198570** ){
198571** // An instance of phrase iPhrase at offset iOff of column iCol
198572** }
198573**
198574** The Fts5PhraseIter structure is defined above. Applications should not
198575** modify this structure directly - it should only be used as shown above
198576** with the xPhraseFirst() and xPhraseNext() API methods (and by
198577** xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below).
198578**
198579** This API can be quite slow if used with an FTS5 table created with the
198580** "detail=none" or "detail=column" option. If the FTS5 table is created
198581** with either "detail=none" or "detail=column" and "content=" option
198582** (i.e. if it is a contentless table), then this API always iterates
198583** through an empty set (all calls to xPhraseFirst() set iCol to -1).
198584**
198585** xPhraseNext()
198586** See xPhraseFirst above.
198587**
198588** xPhraseFirstColumn()
198589** This function and xPhraseNextColumn() are similar to the xPhraseFirst()
198590** and xPhraseNext() APIs described above. The difference is that instead
198591** of iterating through all instances of a phrase in the current row, these
198592** APIs are used to iterate through the set of columns in the current row
198593** that contain one or more instances of a specified phrase. For example:
198594**
198595** Fts5PhraseIter iter;
198596** int iCol;
198597** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
198598** iCol>=0;
198599** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
198600** ){
198601** // Column iCol contains at least one instance of phrase iPhrase
198602** }
198603**
198604** This API can be quite slow if used with an FTS5 table created with the
198605** "detail=none" option. If the FTS5 table is created with either
198606** "detail=none" "content=" option (i.e. if it is a contentless table),
198607** then this API always iterates through an empty set (all calls to
198608** xPhraseFirstColumn() set iCol to -1).
198609**
198610** The information accessed using this API and its companion
198611** xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext
198612** (or xInst/xInstCount). The chief advantage of this API is that it is
198613** significantly more efficient than those alternatives when used with
198614** "detail=column" tables.
198615**
198616** xPhraseNextColumn()
198617** See xPhraseFirstColumn above.
198618*/
198619struct Fts5ExtensionApi {
198620 int iVersion; /* Currently always set to 3 */
198621
198622 void *(*xUserData)(Fts5Context*);
198623
198624 int (*xColumnCount)(Fts5Context*);
198625 int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
198626 int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
198627
198628 int (*xTokenize)(Fts5Context*,
198629 const char *pText, int nText, /* Text to tokenize */
198630 void *pCtx, /* Context passed to xToken() */
198631 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
198632 );
198633
198634 int (*xPhraseCount)(Fts5Context*);
198635 int (*xPhraseSize)(Fts5Context*, int iPhrase);
198636
198637 int (*xInstCount)(Fts5Context*, int *pnInst);
198638 int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
198639
198640 sqlite3_int64 (*xRowid)(Fts5Context*);
198641 int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
198642 int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
198643
198644 int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
198645 int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
198646 );
198647 int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
198648 void *(*xGetAuxdata)(Fts5Context*, int bClear);
198649
198650 int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
198651 void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
198652
198653 int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
198654 void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
198655};
198656
198657/*
198658** CUSTOM AUXILIARY FUNCTIONS
198659*************************************************************************/
198660
198661/*************************************************************************
198662** CUSTOM TOKENIZERS
198663**
198664** Applications may also register custom tokenizer types. A tokenizer
198665** is registered by providing fts5 with a populated instance of the
198666** following structure. All structure methods must be defined, setting
198667** any member of the fts5_tokenizer struct to NULL leads to undefined
198668** behaviour. The structure methods are expected to function as follows:
198669**
198670** xCreate:
198671** This function is used to allocate and initialize a tokenizer instance.
198672** A tokenizer instance is required to actually tokenize text.
198673**
198674** The first argument passed to this function is a copy of the (void*)
198675** pointer provided by the application when the fts5_tokenizer object
198676** was registered with FTS5 (the third argument to xCreateTokenizer()).
198677** The second and third arguments are an array of nul-terminated strings
198678** containing the tokenizer arguments, if any, specified following the
198679** tokenizer name as part of the CREATE VIRTUAL TABLE statement used
198680** to create the FTS5 table.
198681**
198682** The final argument is an output variable. If successful, (*ppOut)
198683** should be set to point to the new tokenizer handle and SQLITE_OK
198684** returned. If an error occurs, some value other than SQLITE_OK should
198685** be returned. In this case, fts5 assumes that the final value of *ppOut
198686** is undefined.
198687**
198688** xDelete:
198689** This function is invoked to delete a tokenizer handle previously
198690** allocated using xCreate(). Fts5 guarantees that this function will
198691** be invoked exactly once for each successful call to xCreate().
198692**
198693** xTokenize:
198694** This function is expected to tokenize the nText byte string indicated
198695** by argument pText. pText may or may not be nul-terminated. The first
198696** argument passed to this function is a pointer to an Fts5Tokenizer object
198697** returned by an earlier call to xCreate().
198698**
198699** The second argument indicates the reason that FTS5 is requesting
198700** tokenization of the supplied text. This is always one of the following
198701** four values:
198702**
198703** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
198704** or removed from the FTS table. The tokenizer is being invoked to
198705** determine the set of tokens to add to (or delete from) the
198706** FTS index.
198707**
198708** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
198709** against the FTS index. The tokenizer is being called to tokenize
198710** a bareword or quoted string specified as part of the query.
198711**
198712** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
198713** FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is
198714** followed by a "*" character, indicating that the last token
198715** returned by the tokenizer will be treated as a token prefix.
198716**
198717** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
198718** satisfy an fts5_api.xTokenize() request made by an auxiliary
198719** function. Or an fts5_api.xColumnSize() request made by the same
198720** on a columnsize=0 database.
198721** </ul>
198722**
198723** For each token in the input string, the supplied callback xToken() must
198724** be invoked. The first argument to it should be a copy of the pointer
198725** passed as the second argument to xTokenize(). The third and fourth
198726** arguments are a pointer to a buffer containing the token text, and the
198727** size of the token in bytes. The 4th and 5th arguments are the byte offsets
198728** of the first byte of and first byte immediately following the text from
198729** which the token is derived within the input.
198730**
198731** The second argument passed to the xToken() callback ("tflags") should
198732** normally be set to 0. The exception is if the tokenizer supports
198733** synonyms. In this case see the discussion below for details.
198734**
198735** FTS5 assumes the xToken() callback is invoked for each token in the
198736** order that they occur within the input text.
198737**
198738** If an xToken() callback returns any value other than SQLITE_OK, then
198739** the tokenization should be abandoned and the xTokenize() method should
198740** immediately return a copy of the xToken() return value. Or, if the
198741** input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally,
198742** if an error occurs with the xTokenize() implementation itself, it
198743** may abandon the tokenization and return any error code other than
198744** SQLITE_OK or SQLITE_DONE.
198745**
198746** SYNONYM SUPPORT
198747**
198748** Custom tokenizers may also support synonyms. Consider a case in which a
198749** user wishes to query for a phrase such as "first place". Using the
198750** built-in tokenizers, the FTS5 query 'first + place' will match instances
198751** of "first place" within the document set, but not alternative forms
198752** such as "1st place". In some applications, it would be better to match
198753** all instances of "first place" or "1st place" regardless of which form
198754** the user specified in the MATCH query text.
198755**
198756** There are several ways to approach this in FTS5:
198757**
198758** <ol><li> By mapping all synonyms to a single token. In this case, the
198759** In the above example, this means that the tokenizer returns the
198760** same token for inputs "first" and "1st". Say that token is in
198761** fact "first", so that when the user inserts the document "I won
198762** 1st place" entries are added to the index for tokens "i", "won",
198763** "first" and "place". If the user then queries for '1st + place',
198764** the tokenizer substitutes "first" for "1st" and the query works
198765** as expected.
198766**
198767** <li> By adding multiple synonyms for a single term to the FTS index.
198768** In this case, when tokenizing query text, the tokenizer may
198769** provide multiple synonyms for a single term within the document.
198770** FTS5 then queries the index for each synonym individually. For
198771** example, faced with the query:
198772**
198773** <codeblock>
198774** ... MATCH 'first place'</codeblock>
198775**
198776** the tokenizer offers both "1st" and "first" as synonyms for the
198777** first token in the MATCH query and FTS5 effectively runs a query
198778** similar to:
198779**
198780** <codeblock>
198781** ... MATCH '(first OR 1st) place'</codeblock>
198782**
198783** except that, for the purposes of auxiliary functions, the query
198784** still appears to contain just two phrases - "(first OR 1st)"
198785** being treated as a single phrase.
198786**
198787** <li> By adding multiple synonyms for a single term to the FTS index.
198788** Using this method, when tokenizing document text, the tokenizer
198789** provides multiple synonyms for each token. So that when a
198790** document such as "I won first place" is tokenized, entries are
198791** added to the FTS index for "i", "won", "first", "1st" and
198792** "place".
198793**
198794** This way, even if the tokenizer does not provide synonyms
198795** when tokenizing query text (it should not - to do would be
198796** inefficient), it doesn't matter if the user queries for
198797** 'first + place' or '1st + place', as there are entries in the
198798** FTS index corresponding to both forms of the first token.
198799** </ol>
198800**
198801** Whether it is parsing document or query text, any call to xToken that
198802** specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
198803** is considered to supply a synonym for the previous token. For example,
198804** when parsing the document "I won first place", a tokenizer that supports
198805** synonyms would call xToken() 5 times, as follows:
198806**
198807** <codeblock>
198808** xToken(pCtx, 0, "i", 1, 0, 1);
198809** xToken(pCtx, 0, "won", 3, 2, 5);
198810** xToken(pCtx, 0, "first", 5, 6, 11);
198811** xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3, 6, 11);
198812** xToken(pCtx, 0, "place", 5, 12, 17);
198813**</codeblock>
198814**
198815** It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time
198816** xToken() is called. Multiple synonyms may be specified for a single token
198817** by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence.
198818** There is no limit to the number of synonyms that may be provided for a
198819** single token.
198820**
198821** In many cases, method (1) above is the best approach. It does not add
198822** extra data to the FTS index or require FTS5 to query for multiple terms,
198823** so it is efficient in terms of disk space and query speed. However, it
198824** does not support prefix queries very well. If, as suggested above, the
198825** token "first" is substituted for "1st" by the tokenizer, then the query:
198826**
198827** <codeblock>
198828** ... MATCH '1s*'</codeblock>
198829**
198830** will not match documents that contain the token "1st" (as the tokenizer
198831** will probably not map "1s" to any prefix of "first").
198832**
198833** For full prefix support, method (3) may be preferred. In this case,
198834** because the index contains entries for both "first" and "1st", prefix
198835** queries such as 'fi*' or '1s*' will match correctly. However, because
198836** extra entries are added to the FTS index, this method uses more space
198837** within the database.
198838**
198839** Method (2) offers a midpoint between (1) and (3). Using this method,
198840** a query such as '1s*' will match documents that contain the literal
198841** token "1st", but not "first" (assuming the tokenizer is not able to
198842** provide synonyms for prefixes). However, a non-prefix query like '1st'
198843** will match against "1st" and "first". This method does not require
198844** extra disk space, as no extra entries are added to the FTS index.
198845** On the other hand, it may require more CPU cycles to run MATCH queries,
198846** as separate queries of the FTS index are required for each synonym.
198847**
198848** When using methods (2) or (3), it is important that the tokenizer only
198849** provide synonyms when tokenizing document text (method (2)) or query
198850** text (method (3)), not both. Doing so will not cause any errors, but is
198851** inefficient.
198852*/
198853typedef struct Fts5Tokenizer Fts5Tokenizer;
198854typedef struct fts5_tokenizer fts5_tokenizer;
198855struct fts5_tokenizer {
198856 int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
198857 void (*xDelete)(Fts5Tokenizer*);
198858 int (*xTokenize)(Fts5Tokenizer*,
198859 void *pCtx,
198860 int flags, /* Mask of FTS5_TOKENIZE_* flags */
198861 const char *pText, int nText,
198862 int (*xToken)(
198863 void *pCtx, /* Copy of 2nd argument to xTokenize() */
198864 int tflags, /* Mask of FTS5_TOKEN_* flags */
198865 const char *pToken, /* Pointer to buffer containing token */
198866 int nToken, /* Size of token in bytes */
198867 int iStart, /* Byte offset of token within input text */
198868 int iEnd /* Byte offset of end of token within input text */
198869 )
198870 );
198871};
198872
198873/* Flags that may be passed as the third argument to xTokenize() */
198874#define FTS5_TOKENIZE_QUERY 0x0001
198875#define FTS5_TOKENIZE_PREFIX 0x0002
198876#define FTS5_TOKENIZE_DOCUMENT 0x0004
198877#define FTS5_TOKENIZE_AUX 0x0008
198878
198879/* Flags that may be passed by the tokenizer implementation back to FTS5
198880** as the third argument to the supplied xToken callback. */
198881#define FTS5_TOKEN_COLOCATED 0x0001 /* Same position as prev. token */
198882
198883/*
198884** END OF CUSTOM TOKENIZERS
198885*************************************************************************/
198886
198887/*************************************************************************
198888** FTS5 EXTENSION REGISTRATION API
198889*/
198890typedef struct fts5_api fts5_api;
198891struct fts5_api {
198892 int iVersion; /* Currently always set to 2 */
198893
198894 /* Create a new tokenizer */
198895 int (*xCreateTokenizer)(
198896 fts5_api *pApi,
198897 const char *zName,
198898 void *pContext,
198899 fts5_tokenizer *pTokenizer,
198900 void (*xDestroy)(void*)
198901 );
198902
198903 /* Find an existing tokenizer */
198904 int (*xFindTokenizer)(
198905 fts5_api *pApi,
198906 const char *zName,
198907 void **ppContext,
198908 fts5_tokenizer *pTokenizer
198909 );
198910
198911 /* Create a new auxiliary function */
198912 int (*xCreateFunction)(
198913 fts5_api *pApi,
198914 const char *zName,
198915 void *pContext,
198916 fts5_extension_function xFunction,
198917 void (*xDestroy)(void*)
198918 );
198919};
198920
198921/*
198922** END OF REGISTRATION API
198923*************************************************************************/
198924
198925#if 0
198926} /* end of the 'extern "C"' block */
198927#endif
198928
198929#endif /* _FTS5_H */
198930
198931/*
198932** 2014 May 31
198933**
198934** The author disclaims copyright to this source code. In place of
198935** a legal notice, here is a blessing:
198936**
198937** May you do good and not evil.
198938** May you find forgiveness for yourself and forgive others.
198939** May you share freely, never taking more than you give.
198940**
198941******************************************************************************
198942**
198943*/
198944#ifndef _FTS5INT_H
198945#define _FTS5INT_H
198946
198947/* #include "fts5.h" */
198948/* #include "sqlite3ext.h" */
198949SQLITE_EXTENSION_INIT1
198950
198951/* #include <string.h> */
198952/* #include <assert.h> */
198953
198954#ifndef SQLITE_AMALGAMATION
198955
198956typedef unsigned char u8;
198957typedef unsigned int u32;
198958typedef unsigned short u16;
198959typedef short i16;
198960typedef sqlite3_int64 i64;
198961typedef sqlite3_uint64 u64;
198962
198963#ifndef ArraySize
198964# define ArraySize(x) ((int)(sizeof(x) / sizeof(x[0])))
198965#endif
198966
198967#define testcase(x)
198968#define ALWAYS(x) 1
198969#define NEVER(x) 0
198970
198971#define MIN(x,y) (((x) < (y)) ? (x) : (y))
198972#define MAX(x,y) (((x) > (y)) ? (x) : (y))
198973
198974/*
198975** Constants for the largest and smallest possible 64-bit signed integers.
198976*/
198977# define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
198978# define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
198979
198980#endif
198981
198982/* Truncate very long tokens to this many bytes. Hard limit is
198983** (65536-1-1-4-9)==65521 bytes. The limiting factor is the 16-bit offset
198984** field that occurs at the start of each leaf page (see fts5_index.c). */
198985#define FTS5_MAX_TOKEN_SIZE 32768
198986
198987/*
198988** Maximum number of prefix indexes on single FTS5 table. This must be
198989** less than 32. If it is set to anything large than that, an #error
198990** directive in fts5_index.c will cause the build to fail.
198991*/
198992#define FTS5_MAX_PREFIX_INDEXES 31
198993
198994#define FTS5_DEFAULT_NEARDIST 10
198995#define FTS5_DEFAULT_RANK "bm25"
198996
198997/* Name of rank and rowid columns */
198998#define FTS5_RANK_NAME "rank"
198999#define FTS5_ROWID_NAME "rowid"
199000
199001#ifdef SQLITE_DEBUG
199002# define FTS5_CORRUPT sqlite3Fts5Corrupt()
199003static int sqlite3Fts5Corrupt(void);
199004#else
199005# define FTS5_CORRUPT SQLITE_CORRUPT_VTAB
199006#endif
199007
199008/*
199009** The assert_nc() macro is similar to the assert() macro, except that it
199010** is used for assert() conditions that are true only if it can be
199011** guranteed that the database is not corrupt.
199012*/
199013#ifdef SQLITE_DEBUG
199014SQLITE_API extern int sqlite3_fts5_may_be_corrupt;
199015# define assert_nc(x) assert(sqlite3_fts5_may_be_corrupt || (x))
199016#else
199017# define assert_nc(x) assert(x)
199018#endif
199019
199020/* Mark a function parameter as unused, to suppress nuisance compiler
199021** warnings. */
199022#ifndef UNUSED_PARAM
199023# define UNUSED_PARAM(X) (void)(X)
199024#endif
199025
199026#ifndef UNUSED_PARAM2
199027# define UNUSED_PARAM2(X, Y) (void)(X), (void)(Y)
199028#endif
199029
199030typedef struct Fts5Global Fts5Global;
199031typedef struct Fts5Colset Fts5Colset;
199032
199033/* If a NEAR() clump or phrase may only match a specific set of columns,
199034** then an object of the following type is used to record the set of columns.
199035** Each entry in the aiCol[] array is a column that may be matched.
199036**
199037** This object is used by fts5_expr.c and fts5_index.c.
199038*/
199039struct Fts5Colset {
199040 int nCol;
199041 int aiCol[1];
199042};
199043
199044
199045
199046/**************************************************************************
199047** Interface to code in fts5_config.c. fts5_config.c contains contains code
199048** to parse the arguments passed to the CREATE VIRTUAL TABLE statement.
199049*/
199050
199051typedef struct Fts5Config Fts5Config;
199052
199053/*
199054** An instance of the following structure encodes all information that can
199055** be gleaned from the CREATE VIRTUAL TABLE statement.
199056**
199057** And all information loaded from the %_config table.
199058**
199059** nAutomerge:
199060** The minimum number of segments that an auto-merge operation should
199061** attempt to merge together. A value of 1 sets the object to use the
199062** compile time default. Zero disables auto-merge altogether.
199063**
199064** zContent:
199065**
199066** zContentRowid:
199067** The value of the content_rowid= option, if one was specified. Or
199068** the string "rowid" otherwise. This text is not quoted - if it is
199069** used as part of an SQL statement it needs to be quoted appropriately.
199070**
199071** zContentExprlist:
199072**
199073** pzErrmsg:
199074** This exists in order to allow the fts5_index.c module to return a
199075** decent error message if it encounters a file-format version it does
199076** not understand.
199077**
199078** bColumnsize:
199079** True if the %_docsize table is created.
199080**
199081** bPrefixIndex:
199082** This is only used for debugging. If set to false, any prefix indexes
199083** are ignored. This value is configured using:
199084**
199085** INSERT INTO tbl(tbl, rank) VALUES('prefix-index', $bPrefixIndex);
199086**
199087*/
199088struct Fts5Config {
199089 sqlite3 *db; /* Database handle */
199090 char *zDb; /* Database holding FTS index (e.g. "main") */
199091 char *zName; /* Name of FTS index */
199092 int nCol; /* Number of columns */
199093 char **azCol; /* Column names */
199094 u8 *abUnindexed; /* True for unindexed columns */
199095 int nPrefix; /* Number of prefix indexes */
199096 int *aPrefix; /* Sizes in bytes of nPrefix prefix indexes */
199097 int eContent; /* An FTS5_CONTENT value */
199098 char *zContent; /* content table */
199099 char *zContentRowid; /* "content_rowid=" option value */
199100 int bColumnsize; /* "columnsize=" option value (dflt==1) */
199101 int eDetail; /* FTS5_DETAIL_XXX value */
199102 char *zContentExprlist;
199103 Fts5Tokenizer *pTok;
199104 fts5_tokenizer *pTokApi;
199105
199106 /* Values loaded from the %_config table */
199107 int iCookie; /* Incremented when %_config is modified */
199108 int pgsz; /* Approximate page size used in %_data */
199109 int nAutomerge; /* 'automerge' setting */
199110 int nCrisisMerge; /* Maximum allowed segments per level */
199111 int nUsermerge; /* 'usermerge' setting */
199112 int nHashSize; /* Bytes of memory for in-memory hash */
199113 char *zRank; /* Name of rank function */
199114 char *zRankArgs; /* Arguments to rank function */
199115
199116 /* If non-NULL, points to sqlite3_vtab.base.zErrmsg. Often NULL. */
199117 char **pzErrmsg;
199118
199119#ifdef SQLITE_DEBUG
199120 int bPrefixIndex; /* True to use prefix-indexes */
199121#endif
199122};
199123
199124/* Current expected value of %_config table 'version' field */
199125#define FTS5_CURRENT_VERSION 4
199126
199127#define FTS5_CONTENT_NORMAL 0
199128#define FTS5_CONTENT_NONE 1
199129#define FTS5_CONTENT_EXTERNAL 2
199130
199131#define FTS5_DETAIL_FULL 0
199132#define FTS5_DETAIL_NONE 1
199133#define FTS5_DETAIL_COLUMNS 2
199134
199135
199136
199137static int sqlite3Fts5ConfigParse(
199138 Fts5Global*, sqlite3*, int, const char **, Fts5Config**, char**
199139);
199140static void sqlite3Fts5ConfigFree(Fts5Config*);
199141
199142static int sqlite3Fts5ConfigDeclareVtab(Fts5Config *pConfig);
199143
199144static int sqlite3Fts5Tokenize(
199145 Fts5Config *pConfig, /* FTS5 Configuration object */
199146 int flags, /* FTS5_TOKENIZE_* flags */
199147 const char *pText, int nText, /* Text to tokenize */
199148 void *pCtx, /* Context passed to xToken() */
199149 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
199150);
199151
199152static void sqlite3Fts5Dequote(char *z);
199153
199154/* Load the contents of the %_config table */
199155static int sqlite3Fts5ConfigLoad(Fts5Config*, int);
199156
199157/* Set the value of a single config attribute */
199158static int sqlite3Fts5ConfigSetValue(Fts5Config*, const char*, sqlite3_value*, int*);
199159
199160static int sqlite3Fts5ConfigParseRank(const char*, char**, char**);
199161
199162/*
199163** End of interface to code in fts5_config.c.
199164**************************************************************************/
199165
199166/**************************************************************************
199167** Interface to code in fts5_buffer.c.
199168*/
199169
199170/*
199171** Buffer object for the incremental building of string data.
199172*/
199173typedef struct Fts5Buffer Fts5Buffer;
199174struct Fts5Buffer {
199175 u8 *p;
199176 int n;
199177 int nSpace;
199178};
199179
199180static int sqlite3Fts5BufferSize(int*, Fts5Buffer*, u32);
199181static void sqlite3Fts5BufferAppendVarint(int*, Fts5Buffer*, i64);
199182static void sqlite3Fts5BufferAppendBlob(int*, Fts5Buffer*, u32, const u8*);
199183static void sqlite3Fts5BufferAppendString(int *, Fts5Buffer*, const char*);
199184static void sqlite3Fts5BufferFree(Fts5Buffer*);
199185static void sqlite3Fts5BufferZero(Fts5Buffer*);
199186static void sqlite3Fts5BufferSet(int*, Fts5Buffer*, int, const u8*);
199187static void sqlite3Fts5BufferAppendPrintf(int *, Fts5Buffer*, char *zFmt, ...);
199188
199189static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...);
199190
199191#define fts5BufferZero(x) sqlite3Fts5BufferZero(x)
199192#define fts5BufferAppendVarint(a,b,c) sqlite3Fts5BufferAppendVarint(a,b,c)
199193#define fts5BufferFree(a) sqlite3Fts5BufferFree(a)
199194#define fts5BufferAppendBlob(a,b,c,d) sqlite3Fts5BufferAppendBlob(a,b,c,d)
199195#define fts5BufferSet(a,b,c,d) sqlite3Fts5BufferSet(a,b,c,d)
199196
199197#define fts5BufferGrow(pRc,pBuf,nn) ( \
199198 (u32)((pBuf)->n) + (u32)(nn) <= (u32)((pBuf)->nSpace) ? 0 : \
199199 sqlite3Fts5BufferSize((pRc),(pBuf),(nn)+(pBuf)->n) \
199200)
199201
199202/* Write and decode big-endian 32-bit integer values */
199203static void sqlite3Fts5Put32(u8*, int);
199204static int sqlite3Fts5Get32(const u8*);
199205
199206#define FTS5_POS2COLUMN(iPos) (int)(iPos >> 32)
199207#define FTS5_POS2OFFSET(iPos) (int)(iPos & 0xFFFFFFFF)
199208
199209typedef struct Fts5PoslistReader Fts5PoslistReader;
199210struct Fts5PoslistReader {
199211 /* Variables used only by sqlite3Fts5PoslistIterXXX() functions. */
199212 const u8 *a; /* Position list to iterate through */
199213 int n; /* Size of buffer at a[] in bytes */
199214 int i; /* Current offset in a[] */
199215
199216 u8 bFlag; /* For client use (any custom purpose) */
199217
199218 /* Output variables */
199219 u8 bEof; /* Set to true at EOF */
199220 i64 iPos; /* (iCol<<32) + iPos */
199221};
199222static int sqlite3Fts5PoslistReaderInit(
199223 const u8 *a, int n, /* Poslist buffer to iterate through */
199224 Fts5PoslistReader *pIter /* Iterator object to initialize */
199225);
199226static int sqlite3Fts5PoslistReaderNext(Fts5PoslistReader*);
199227
199228typedef struct Fts5PoslistWriter Fts5PoslistWriter;
199229struct Fts5PoslistWriter {
199230 i64 iPrev;
199231};
199232static int sqlite3Fts5PoslistWriterAppend(Fts5Buffer*, Fts5PoslistWriter*, i64);
199233static void sqlite3Fts5PoslistSafeAppend(Fts5Buffer*, i64*, i64);
199234
199235static int sqlite3Fts5PoslistNext64(
199236 const u8 *a, int n, /* Buffer containing poslist */
199237 int *pi, /* IN/OUT: Offset within a[] */
199238 i64 *piOff /* IN/OUT: Current offset */
199239);
199240
199241/* Malloc utility */
199242static void *sqlite3Fts5MallocZero(int *pRc, int nByte);
199243static char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn);
199244
199245/* Character set tests (like isspace(), isalpha() etc.) */
199246static int sqlite3Fts5IsBareword(char t);
199247
199248
199249/* Bucket of terms object used by the integrity-check in offsets=0 mode. */
199250typedef struct Fts5Termset Fts5Termset;
199251static int sqlite3Fts5TermsetNew(Fts5Termset**);
199252static int sqlite3Fts5TermsetAdd(Fts5Termset*, int, const char*, int, int *pbPresent);
199253static void sqlite3Fts5TermsetFree(Fts5Termset*);
199254
199255/*
199256** End of interface to code in fts5_buffer.c.
199257**************************************************************************/
199258
199259/**************************************************************************
199260** Interface to code in fts5_index.c. fts5_index.c contains contains code
199261** to access the data stored in the %_data table.
199262*/
199263
199264typedef struct Fts5Index Fts5Index;
199265typedef struct Fts5IndexIter Fts5IndexIter;
199266
199267struct Fts5IndexIter {
199268 i64 iRowid;
199269 const u8 *pData;
199270 int nData;
199271 u8 bEof;
199272};
199273
199274#define sqlite3Fts5IterEof(x) ((x)->bEof)
199275
199276/*
199277** Values used as part of the flags argument passed to IndexQuery().
199278*/
199279#define FTS5INDEX_QUERY_PREFIX 0x0001 /* Prefix query */
199280#define FTS5INDEX_QUERY_DESC 0x0002 /* Docs in descending rowid order */
199281#define FTS5INDEX_QUERY_TEST_NOIDX 0x0004 /* Do not use prefix index */
199282#define FTS5INDEX_QUERY_SCAN 0x0008 /* Scan query (fts5vocab) */
199283
199284/* The following are used internally by the fts5_index.c module. They are
199285** defined here only to make it easier to avoid clashes with the flags
199286** above. */
199287#define FTS5INDEX_QUERY_SKIPEMPTY 0x0010
199288#define FTS5INDEX_QUERY_NOOUTPUT 0x0020
199289
199290/*
199291** Create/destroy an Fts5Index object.
199292*/
199293static int sqlite3Fts5IndexOpen(Fts5Config *pConfig, int bCreate, Fts5Index**, char**);
199294static int sqlite3Fts5IndexClose(Fts5Index *p);
199295
199296/*
199297** Return a simple checksum value based on the arguments.
199298*/
199299static u64 sqlite3Fts5IndexEntryCksum(
199300 i64 iRowid,
199301 int iCol,
199302 int iPos,
199303 int iIdx,
199304 const char *pTerm,
199305 int nTerm
199306);
199307
199308/*
199309** Argument p points to a buffer containing utf-8 text that is n bytes in
199310** size. Return the number of bytes in the nChar character prefix of the
199311** buffer, or 0 if there are less than nChar characters in total.
199312*/
199313static int sqlite3Fts5IndexCharlenToBytelen(
199314 const char *p,
199315 int nByte,
199316 int nChar
199317);
199318
199319/*
199320** Open a new iterator to iterate though all rowids that match the
199321** specified token or token prefix.
199322*/
199323static int sqlite3Fts5IndexQuery(
199324 Fts5Index *p, /* FTS index to query */
199325 const char *pToken, int nToken, /* Token (or prefix) to query for */
199326 int flags, /* Mask of FTS5INDEX_QUERY_X flags */
199327 Fts5Colset *pColset, /* Match these columns only */
199328 Fts5IndexIter **ppIter /* OUT: New iterator object */
199329);
199330
199331/*
199332** The various operations on open token or token prefix iterators opened
199333** using sqlite3Fts5IndexQuery().
199334*/
199335static int sqlite3Fts5IterNext(Fts5IndexIter*);
199336static int sqlite3Fts5IterNextFrom(Fts5IndexIter*, i64 iMatch);
199337
199338/*
199339** Close an iterator opened by sqlite3Fts5IndexQuery().
199340*/
199341static void sqlite3Fts5IterClose(Fts5IndexIter*);
199342
199343/*
199344** This interface is used by the fts5vocab module.
199345*/
199346static const char *sqlite3Fts5IterTerm(Fts5IndexIter*, int*);
199347static int sqlite3Fts5IterNextScan(Fts5IndexIter*);
199348
199349
199350/*
199351** Insert or remove data to or from the index. Each time a document is
199352** added to or removed from the index, this function is called one or more
199353** times.
199354**
199355** For an insert, it must be called once for each token in the new document.
199356** If the operation is a delete, it must be called (at least) once for each
199357** unique token in the document with an iCol value less than zero. The iPos
199358** argument is ignored for a delete.
199359*/
199360static int sqlite3Fts5IndexWrite(
199361 Fts5Index *p, /* Index to write to */
199362 int iCol, /* Column token appears in (-ve -> delete) */
199363 int iPos, /* Position of token within column */
199364 const char *pToken, int nToken /* Token to add or remove to or from index */
199365);
199366
199367/*
199368** Indicate that subsequent calls to sqlite3Fts5IndexWrite() pertain to
199369** document iDocid.
199370*/
199371static int sqlite3Fts5IndexBeginWrite(
199372 Fts5Index *p, /* Index to write to */
199373 int bDelete, /* True if current operation is a delete */
199374 i64 iDocid /* Docid to add or remove data from */
199375);
199376
199377/*
199378** Flush any data stored in the in-memory hash tables to the database.
199379** Also close any open blob handles.
199380*/
199381static int sqlite3Fts5IndexSync(Fts5Index *p);
199382
199383/*
199384** Discard any data stored in the in-memory hash tables. Do not write it
199385** to the database. Additionally, assume that the contents of the %_data
199386** table may have changed on disk. So any in-memory caches of %_data
199387** records must be invalidated.
199388*/
199389static int sqlite3Fts5IndexRollback(Fts5Index *p);
199390
199391/*
199392** Get or set the "averages" values.
199393*/
199394static int sqlite3Fts5IndexGetAverages(Fts5Index *p, i64 *pnRow, i64 *anSize);
199395static int sqlite3Fts5IndexSetAverages(Fts5Index *p, const u8*, int);
199396
199397/*
199398** Functions called by the storage module as part of integrity-check.
199399*/
199400static int sqlite3Fts5IndexIntegrityCheck(Fts5Index*, u64 cksum);
199401
199402/*
199403** Called during virtual module initialization to register UDF
199404** fts5_decode() with SQLite
199405*/
199406static int sqlite3Fts5IndexInit(sqlite3*);
199407
199408static int sqlite3Fts5IndexSetCookie(Fts5Index*, int);
199409
199410/*
199411** Return the total number of entries read from the %_data table by
199412** this connection since it was created.
199413*/
199414static int sqlite3Fts5IndexReads(Fts5Index *p);
199415
199416static int sqlite3Fts5IndexReinit(Fts5Index *p);
199417static int sqlite3Fts5IndexOptimize(Fts5Index *p);
199418static int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge);
199419static int sqlite3Fts5IndexReset(Fts5Index *p);
199420
199421static int sqlite3Fts5IndexLoadConfig(Fts5Index *p);
199422
199423/*
199424** End of interface to code in fts5_index.c.
199425**************************************************************************/
199426
199427/**************************************************************************
199428** Interface to code in fts5_varint.c.
199429*/
199430static int sqlite3Fts5GetVarint32(const unsigned char *p, u32 *v);
199431static int sqlite3Fts5GetVarintLen(u32 iVal);
199432static u8 sqlite3Fts5GetVarint(const unsigned char*, u64*);
199433static int sqlite3Fts5PutVarint(unsigned char *p, u64 v);
199434
199435#define fts5GetVarint32(a,b) sqlite3Fts5GetVarint32(a,(u32*)&b)
199436#define fts5GetVarint sqlite3Fts5GetVarint
199437
199438#define fts5FastGetVarint32(a, iOff, nVal) { \
199439 nVal = (a)[iOff++]; \
199440 if( nVal & 0x80 ){ \
199441 iOff--; \
199442 iOff += fts5GetVarint32(&(a)[iOff], nVal); \
199443 } \
199444}
199445
199446
199447/*
199448** End of interface to code in fts5_varint.c.
199449**************************************************************************/
199450
199451
199452/**************************************************************************
199453** Interface to code in fts5.c.
199454*/
199455
199456static int sqlite3Fts5GetTokenizer(
199457 Fts5Global*,
199458 const char **azArg,
199459 int nArg,
199460 Fts5Tokenizer**,
199461 fts5_tokenizer**,
199462 char **pzErr
199463);
199464
199465static Fts5Index *sqlite3Fts5IndexFromCsrid(Fts5Global*, i64, Fts5Config **);
199466
199467/*
199468** End of interface to code in fts5.c.
199469**************************************************************************/
199470
199471/**************************************************************************
199472** Interface to code in fts5_hash.c.
199473*/
199474typedef struct Fts5Hash Fts5Hash;
199475
199476/*
199477** Create a hash table, free a hash table.
199478*/
199479static int sqlite3Fts5HashNew(Fts5Config*, Fts5Hash**, int *pnSize);
199480static void sqlite3Fts5HashFree(Fts5Hash*);
199481
199482static int sqlite3Fts5HashWrite(
199483 Fts5Hash*,
199484 i64 iRowid, /* Rowid for this entry */
199485 int iCol, /* Column token appears in (-ve -> delete) */
199486 int iPos, /* Position of token within column */
199487 char bByte,
199488 const char *pToken, int nToken /* Token to add or remove to or from index */
199489);
199490
199491/*
199492** Empty (but do not delete) a hash table.
199493*/
199494static void sqlite3Fts5HashClear(Fts5Hash*);
199495
199496static int sqlite3Fts5HashQuery(
199497 Fts5Hash*, /* Hash table to query */
199498 const char *pTerm, int nTerm, /* Query term */
199499 const u8 **ppDoclist, /* OUT: Pointer to doclist for pTerm */
199500 int *pnDoclist /* OUT: Size of doclist in bytes */
199501);
199502
199503static int sqlite3Fts5HashScanInit(
199504 Fts5Hash*, /* Hash table to query */
199505 const char *pTerm, int nTerm /* Query prefix */
199506);
199507static void sqlite3Fts5HashScanNext(Fts5Hash*);
199508static int sqlite3Fts5HashScanEof(Fts5Hash*);
199509static void sqlite3Fts5HashScanEntry(Fts5Hash *,
199510 const char **pzTerm, /* OUT: term (nul-terminated) */
199511 const u8 **ppDoclist, /* OUT: pointer to doclist */
199512 int *pnDoclist /* OUT: size of doclist in bytes */
199513);
199514
199515
199516/*
199517** End of interface to code in fts5_hash.c.
199518**************************************************************************/
199519
199520/**************************************************************************
199521** Interface to code in fts5_storage.c. fts5_storage.c contains contains
199522** code to access the data stored in the %_content and %_docsize tables.
199523*/
199524
199525#define FTS5_STMT_SCAN_ASC 0 /* SELECT rowid, * FROM ... ORDER BY 1 ASC */
199526#define FTS5_STMT_SCAN_DESC 1 /* SELECT rowid, * FROM ... ORDER BY 1 DESC */
199527#define FTS5_STMT_LOOKUP 2 /* SELECT rowid, * FROM ... WHERE rowid=? */
199528
199529typedef struct Fts5Storage Fts5Storage;
199530
199531static int sqlite3Fts5StorageOpen(Fts5Config*, Fts5Index*, int, Fts5Storage**, char**);
199532static int sqlite3Fts5StorageClose(Fts5Storage *p);
199533static int sqlite3Fts5StorageRename(Fts5Storage*, const char *zName);
199534
199535static int sqlite3Fts5DropAll(Fts5Config*);
199536static int sqlite3Fts5CreateTable(Fts5Config*, const char*, const char*, int, char **);
199537
199538static int sqlite3Fts5StorageDelete(Fts5Storage *p, i64, sqlite3_value**);
199539static int sqlite3Fts5StorageContentInsert(Fts5Storage *p, sqlite3_value**, i64*);
199540static int sqlite3Fts5StorageIndexInsert(Fts5Storage *p, sqlite3_value**, i64);
199541
199542static int sqlite3Fts5StorageIntegrity(Fts5Storage *p);
199543
199544static int sqlite3Fts5StorageStmt(Fts5Storage *p, int eStmt, sqlite3_stmt**, char**);
199545static void sqlite3Fts5StorageStmtRelease(Fts5Storage *p, int eStmt, sqlite3_stmt*);
199546
199547static int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol);
199548static int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnAvg);
199549static int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow);
199550
199551static int sqlite3Fts5StorageSync(Fts5Storage *p);
199552static int sqlite3Fts5StorageRollback(Fts5Storage *p);
199553
199554static int sqlite3Fts5StorageConfigValue(
199555 Fts5Storage *p, const char*, sqlite3_value*, int
199556);
199557
199558static int sqlite3Fts5StorageDeleteAll(Fts5Storage *p);
199559static int sqlite3Fts5StorageRebuild(Fts5Storage *p);
199560static int sqlite3Fts5StorageOptimize(Fts5Storage *p);
199561static int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge);
199562static int sqlite3Fts5StorageReset(Fts5Storage *p);
199563
199564/*
199565** End of interface to code in fts5_storage.c.
199566**************************************************************************/
199567
199568
199569/**************************************************************************
199570** Interface to code in fts5_expr.c.
199571*/
199572typedef struct Fts5Expr Fts5Expr;
199573typedef struct Fts5ExprNode Fts5ExprNode;
199574typedef struct Fts5Parse Fts5Parse;
199575typedef struct Fts5Token Fts5Token;
199576typedef struct Fts5ExprPhrase Fts5ExprPhrase;
199577typedef struct Fts5ExprNearset Fts5ExprNearset;
199578
199579struct Fts5Token {
199580 const char *p; /* Token text (not NULL terminated) */
199581 int n; /* Size of buffer p in bytes */
199582};
199583
199584/* Parse a MATCH expression. */
199585static int sqlite3Fts5ExprNew(
199586 Fts5Config *pConfig,
199587 int iCol, /* Column on LHS of MATCH operator */
199588 const char *zExpr,
199589 Fts5Expr **ppNew,
199590 char **pzErr
199591);
199592
199593/*
199594** for(rc = sqlite3Fts5ExprFirst(pExpr, pIdx, bDesc);
199595** rc==SQLITE_OK && 0==sqlite3Fts5ExprEof(pExpr);
199596** rc = sqlite3Fts5ExprNext(pExpr)
199597** ){
199598** // The document with rowid iRowid matches the expression!
199599** i64 iRowid = sqlite3Fts5ExprRowid(pExpr);
199600** }
199601*/
199602static int sqlite3Fts5ExprFirst(Fts5Expr*, Fts5Index *pIdx, i64 iMin, int bDesc);
199603static int sqlite3Fts5ExprNext(Fts5Expr*, i64 iMax);
199604static int sqlite3Fts5ExprEof(Fts5Expr*);
199605static i64 sqlite3Fts5ExprRowid(Fts5Expr*);
199606
199607static void sqlite3Fts5ExprFree(Fts5Expr*);
199608
199609/* Called during startup to register a UDF with SQLite */
199610static int sqlite3Fts5ExprInit(Fts5Global*, sqlite3*);
199611
199612static int sqlite3Fts5ExprPhraseCount(Fts5Expr*);
199613static int sqlite3Fts5ExprPhraseSize(Fts5Expr*, int iPhrase);
199614static int sqlite3Fts5ExprPoslist(Fts5Expr*, int, const u8 **);
199615
199616typedef struct Fts5PoslistPopulator Fts5PoslistPopulator;
199617static Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr*, int);
199618static int sqlite3Fts5ExprPopulatePoslists(
199619 Fts5Config*, Fts5Expr*, Fts5PoslistPopulator*, int, const char*, int
199620);
199621static void sqlite3Fts5ExprCheckPoslists(Fts5Expr*, i64);
199622
199623static int sqlite3Fts5ExprClonePhrase(Fts5Expr*, int, Fts5Expr**);
199624
199625static int sqlite3Fts5ExprPhraseCollist(Fts5Expr *, int, const u8 **, int *);
199626
199627/*******************************************
199628** The fts5_expr.c API above this point is used by the other hand-written
199629** C code in this module. The interfaces below this point are called by
199630** the parser code in fts5parse.y. */
199631
199632static void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...);
199633
199634static Fts5ExprNode *sqlite3Fts5ParseNode(
199635 Fts5Parse *pParse,
199636 int eType,
199637 Fts5ExprNode *pLeft,
199638 Fts5ExprNode *pRight,
199639 Fts5ExprNearset *pNear
199640);
199641
199642static Fts5ExprNode *sqlite3Fts5ParseImplicitAnd(
199643 Fts5Parse *pParse,
199644 Fts5ExprNode *pLeft,
199645 Fts5ExprNode *pRight
199646);
199647
199648static Fts5ExprPhrase *sqlite3Fts5ParseTerm(
199649 Fts5Parse *pParse,
199650 Fts5ExprPhrase *pPhrase,
199651 Fts5Token *pToken,
199652 int bPrefix
199653);
199654
199655static void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase*);
199656
199657static Fts5ExprNearset *sqlite3Fts5ParseNearset(
199658 Fts5Parse*,
199659 Fts5ExprNearset*,
199660 Fts5ExprPhrase*
199661);
199662
199663static Fts5Colset *sqlite3Fts5ParseColset(
199664 Fts5Parse*,
199665 Fts5Colset*,
199666 Fts5Token *
199667);
199668
199669static void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase*);
199670static void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset*);
199671static void sqlite3Fts5ParseNodeFree(Fts5ExprNode*);
199672
199673static void sqlite3Fts5ParseSetDistance(Fts5Parse*, Fts5ExprNearset*, Fts5Token*);
199674static void sqlite3Fts5ParseSetColset(Fts5Parse*, Fts5ExprNode*, Fts5Colset*);
199675static Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse*, Fts5Colset*);
199676static void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p);
199677static void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token*);
199678
199679/*
199680** End of interface to code in fts5_expr.c.
199681**************************************************************************/
199682
199683
199684
199685/**************************************************************************
199686** Interface to code in fts5_aux.c.
199687*/
199688
199689static int sqlite3Fts5AuxInit(fts5_api*);
199690/*
199691** End of interface to code in fts5_aux.c.
199692**************************************************************************/
199693
199694/**************************************************************************
199695** Interface to code in fts5_tokenizer.c.
199696*/
199697
199698static int sqlite3Fts5TokenizerInit(fts5_api*);
199699/*
199700** End of interface to code in fts5_tokenizer.c.
199701**************************************************************************/
199702
199703/**************************************************************************
199704** Interface to code in fts5_vocab.c.
199705*/
199706
199707static int sqlite3Fts5VocabInit(Fts5Global*, sqlite3*);
199708
199709/*
199710** End of interface to code in fts5_vocab.c.
199711**************************************************************************/
199712
199713
199714/**************************************************************************
199715** Interface to automatically generated code in fts5_unicode2.c.
199716*/
199717static int sqlite3Fts5UnicodeIsdiacritic(int c);
199718static int sqlite3Fts5UnicodeFold(int c, int bRemoveDiacritic);
199719
199720static int sqlite3Fts5UnicodeCatParse(const char*, u8*);
199721static int sqlite3Fts5UnicodeCategory(int iCode);
199722static void sqlite3Fts5UnicodeAscii(u8*, u8*);
199723/*
199724** End of interface to code in fts5_unicode2.c.
199725**************************************************************************/
199726
199727#endif
199728
199729#define FTS5_OR 1
199730#define FTS5_AND 2
199731#define FTS5_NOT 3
199732#define FTS5_TERM 4
199733#define FTS5_COLON 5
199734#define FTS5_MINUS 6
199735#define FTS5_LCP 7
199736#define FTS5_RCP 8
199737#define FTS5_STRING 9
199738#define FTS5_LP 10
199739#define FTS5_RP 11
199740#define FTS5_CARET 12
199741#define FTS5_COMMA 13
199742#define FTS5_PLUS 14
199743#define FTS5_STAR 15
199744
199745/*
199746** 2000-05-29
199747**
199748** The author disclaims copyright to this source code. In place of
199749** a legal notice, here is a blessing:
199750**
199751** May you do good and not evil.
199752** May you find forgiveness for yourself and forgive others.
199753** May you share freely, never taking more than you give.
199754**
199755*************************************************************************
199756** Driver template for the LEMON parser generator.
199757**
199758** The "lemon" program processes an LALR(1) input grammar file, then uses
199759** this template to construct a parser. The "lemon" program inserts text
199760** at each "%%" line. Also, any "P-a-r-s-e" identifer prefix (without the
199761** interstitial "-" characters) contained in this template is changed into
199762** the value of the %name directive from the grammar. Otherwise, the content
199763** of this template is copied straight through into the generate parser
199764** source file.
199765**
199766** The following is the concatenation of all %include directives from the
199767** input grammar file:
199768*/
199769/* #include <stdio.h> */
199770/* #include <assert.h> */
199771/************ Begin %include sections from the grammar ************************/
199772
199773/* #include "fts5Int.h" */
199774/* #include "fts5parse.h" */
199775
199776/*
199777** Disable all error recovery processing in the parser push-down
199778** automaton.
199779*/
199780#define fts5YYNOERRORRECOVERY 1
199781
199782/*
199783** Make fts5yytestcase() the same as testcase()
199784*/
199785#define fts5yytestcase(X) testcase(X)
199786
199787/*
199788** Indicate that sqlite3ParserFree() will never be called with a null
199789** pointer.
199790*/
199791#define fts5YYPARSEFREENOTNULL 1
199792
199793/*
199794** Alternative datatype for the argument to the malloc() routine passed
199795** into sqlite3ParserAlloc(). The default is size_t.
199796*/
199797#define fts5YYMALLOCARGTYPE u64
199798
199799/**************** End of %include directives **********************************/
199800/* These constants specify the various numeric values for terminal symbols
199801** in a format understandable to "makeheaders". This section is blank unless
199802** "lemon" is run with the "-m" command-line option.
199803***************** Begin makeheaders token definitions *************************/
199804/**************** End makeheaders token definitions ***************************/
199805
199806/* The next sections is a series of control #defines.
199807** various aspects of the generated parser.
199808** fts5YYCODETYPE is the data type used to store the integer codes
199809** that represent terminal and non-terminal symbols.
199810** "unsigned char" is used if there are fewer than
199811** 256 symbols. Larger types otherwise.
199812** fts5YYNOCODE is a number of type fts5YYCODETYPE that is not used for
199813** any terminal or nonterminal symbol.
199814** fts5YYFALLBACK If defined, this indicates that one or more tokens
199815** (also known as: "terminal symbols") have fall-back
199816** values which should be used if the original symbol
199817** would not parse. This permits keywords to sometimes
199818** be used as identifiers, for example.
199819** fts5YYACTIONTYPE is the data type used for "action codes" - numbers
199820** that indicate what to do in response to the next
199821** token.
199822** sqlite3Fts5ParserFTS5TOKENTYPE is the data type used for minor type for terminal
199823** symbols. Background: A "minor type" is a semantic
199824** value associated with a terminal or non-terminal
199825** symbols. For example, for an "ID" terminal symbol,
199826** the minor type might be the name of the identifier.
199827** Each non-terminal can have a different minor type.
199828** Terminal symbols all have the same minor type, though.
199829** This macros defines the minor type for terminal
199830** symbols.
199831** fts5YYMINORTYPE is the data type used for all minor types.
199832** This is typically a union of many types, one of
199833** which is sqlite3Fts5ParserFTS5TOKENTYPE. The entry in the union
199834** for terminal symbols is called "fts5yy0".
199835** fts5YYSTACKDEPTH is the maximum depth of the parser's stack. If
199836** zero the stack is dynamically sized using realloc()
199837** sqlite3Fts5ParserARG_SDECL A static variable declaration for the %extra_argument
199838** sqlite3Fts5ParserARG_PDECL A parameter declaration for the %extra_argument
199839** sqlite3Fts5ParserARG_PARAM Code to pass %extra_argument as a subroutine parameter
199840** sqlite3Fts5ParserARG_STORE Code to store %extra_argument into fts5yypParser
199841** sqlite3Fts5ParserARG_FETCH Code to extract %extra_argument from fts5yypParser
199842** sqlite3Fts5ParserCTX_* As sqlite3Fts5ParserARG_ except for %extra_context
199843** fts5YYERRORSYMBOL is the code number of the error symbol. If not
199844** defined, then do no error processing.
199845** fts5YYNSTATE the combined number of states.
199846** fts5YYNRULE the number of rules in the grammar
199847** fts5YYNFTS5TOKEN Number of terminal symbols
199848** fts5YY_MAX_SHIFT Maximum value for shift actions
199849** fts5YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
199850** fts5YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
199851** fts5YY_ERROR_ACTION The fts5yy_action[] code for syntax error
199852** fts5YY_ACCEPT_ACTION The fts5yy_action[] code for accept
199853** fts5YY_NO_ACTION The fts5yy_action[] code for no-op
199854** fts5YY_MIN_REDUCE Minimum value for reduce actions
199855** fts5YY_MAX_REDUCE Maximum value for reduce actions
199856*/
199857#ifndef INTERFACE
199858# define INTERFACE 1
199859#endif
199860/************* Begin control #defines *****************************************/
199861#define fts5YYCODETYPE unsigned char
199862#define fts5YYNOCODE 27
199863#define fts5YYACTIONTYPE unsigned char
199864#define sqlite3Fts5ParserFTS5TOKENTYPE Fts5Token
199865typedef union {
199866 int fts5yyinit;
199867 sqlite3Fts5ParserFTS5TOKENTYPE fts5yy0;
199868 int fts5yy4;
199869 Fts5Colset* fts5yy11;
199870 Fts5ExprNode* fts5yy24;
199871 Fts5ExprNearset* fts5yy46;
199872 Fts5ExprPhrase* fts5yy53;
199873} fts5YYMINORTYPE;
199874#ifndef fts5YYSTACKDEPTH
199875#define fts5YYSTACKDEPTH 100
199876#endif
199877#define sqlite3Fts5ParserARG_SDECL Fts5Parse *pParse;
199878#define sqlite3Fts5ParserARG_PDECL ,Fts5Parse *pParse
199879#define sqlite3Fts5ParserARG_PARAM ,pParse
199880#define sqlite3Fts5ParserARG_FETCH Fts5Parse *pParse=fts5yypParser->pParse;
199881#define sqlite3Fts5ParserARG_STORE fts5yypParser->pParse=pParse;
199882#define sqlite3Fts5ParserCTX_SDECL
199883#define sqlite3Fts5ParserCTX_PDECL
199884#define sqlite3Fts5ParserCTX_PARAM
199885#define sqlite3Fts5ParserCTX_FETCH
199886#define sqlite3Fts5ParserCTX_STORE
199887#define fts5YYNSTATE 35
199888#define fts5YYNRULE 28
199889#define fts5YYNFTS5TOKEN 16
199890#define fts5YY_MAX_SHIFT 34
199891#define fts5YY_MIN_SHIFTREDUCE 52
199892#define fts5YY_MAX_SHIFTREDUCE 79
199893#define fts5YY_ERROR_ACTION 80
199894#define fts5YY_ACCEPT_ACTION 81
199895#define fts5YY_NO_ACTION 82
199896#define fts5YY_MIN_REDUCE 83
199897#define fts5YY_MAX_REDUCE 110
199898/************* End control #defines *******************************************/
199899#define fts5YY_NLOOKAHEAD ((int)(sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0])))
199900
199901/* Define the fts5yytestcase() macro to be a no-op if is not already defined
199902** otherwise.
199903**
199904** Applications can choose to define fts5yytestcase() in the %include section
199905** to a macro that can assist in verifying code coverage. For production
199906** code the fts5yytestcase() macro should be turned off. But it is useful
199907** for testing.
199908*/
199909#ifndef fts5yytestcase
199910# define fts5yytestcase(X)
199911#endif
199912
199913
199914/* Next are the tables used to determine what action to take based on the
199915** current state and lookahead token. These tables are used to implement
199916** functions that take a state number and lookahead value and return an
199917** action integer.
199918**
199919** Suppose the action integer is N. Then the action is determined as
199920** follows
199921**
199922** 0 <= N <= fts5YY_MAX_SHIFT Shift N. That is, push the lookahead
199923** token onto the stack and goto state N.
199924**
199925** N between fts5YY_MIN_SHIFTREDUCE Shift to an arbitrary state then
199926** and fts5YY_MAX_SHIFTREDUCE reduce by rule N-fts5YY_MIN_SHIFTREDUCE.
199927**
199928** N == fts5YY_ERROR_ACTION A syntax error has occurred.
199929**
199930** N == fts5YY_ACCEPT_ACTION The parser accepts its input.
199931**
199932** N == fts5YY_NO_ACTION No such action. Denotes unused
199933** slots in the fts5yy_action[] table.
199934**
199935** N between fts5YY_MIN_REDUCE Reduce by rule N-fts5YY_MIN_REDUCE
199936** and fts5YY_MAX_REDUCE
199937**
199938** The action table is constructed as a single large table named fts5yy_action[].
199939** Given state S and lookahead X, the action is computed as either:
199940**
199941** (A) N = fts5yy_action[ fts5yy_shift_ofst[S] + X ]
199942** (B) N = fts5yy_default[S]
199943**
199944** The (A) formula is preferred. The B formula is used instead if
199945** fts5yy_lookahead[fts5yy_shift_ofst[S]+X] is not equal to X.
199946**
199947** The formulas above are for computing the action when the lookahead is
199948** a terminal symbol. If the lookahead is a non-terminal (as occurs after
199949** a reduce action) then the fts5yy_reduce_ofst[] array is used in place of
199950** the fts5yy_shift_ofst[] array.
199951**
199952** The following are the tables generated in this section:
199953**
199954** fts5yy_action[] A single table containing all actions.
199955** fts5yy_lookahead[] A table containing the lookahead for each entry in
199956** fts5yy_action. Used to detect hash collisions.
199957** fts5yy_shift_ofst[] For each state, the offset into fts5yy_action for
199958** shifting terminals.
199959** fts5yy_reduce_ofst[] For each state, the offset into fts5yy_action for
199960** shifting non-terminals after a reduce.
199961** fts5yy_default[] Default action for each state.
199962**
199963*********** Begin parsing tables **********************************************/
199964#define fts5YY_ACTTAB_COUNT (105)
199965static const fts5YYACTIONTYPE fts5yy_action[] = {
199966 /* 0 */ 81, 20, 96, 6, 28, 99, 98, 26, 26, 18,
199967 /* 10 */ 96, 6, 28, 17, 98, 56, 26, 19, 96, 6,
199968 /* 20 */ 28, 14, 98, 14, 26, 31, 92, 96, 6, 28,
199969 /* 30 */ 108, 98, 25, 26, 21, 96, 6, 28, 78, 98,
199970 /* 40 */ 58, 26, 29, 96, 6, 28, 107, 98, 22, 26,
199971 /* 50 */ 24, 16, 12, 11, 1, 13, 13, 24, 16, 23,
199972 /* 60 */ 11, 33, 34, 13, 97, 8, 27, 32, 98, 7,
199973 /* 70 */ 26, 3, 4, 5, 3, 4, 5, 3, 83, 4,
199974 /* 80 */ 5, 3, 63, 5, 3, 62, 12, 2, 86, 13,
199975 /* 90 */ 9, 30, 10, 10, 54, 57, 75, 78, 78, 53,
199976 /* 100 */ 57, 15, 82, 82, 71,
199977};
199978static const fts5YYCODETYPE fts5yy_lookahead[] = {
199979 /* 0 */ 16, 17, 18, 19, 20, 22, 22, 24, 24, 17,
199980 /* 10 */ 18, 19, 20, 7, 22, 9, 24, 17, 18, 19,
199981 /* 20 */ 20, 9, 22, 9, 24, 13, 17, 18, 19, 20,
199982 /* 30 */ 26, 22, 24, 24, 17, 18, 19, 20, 15, 22,
199983 /* 40 */ 9, 24, 17, 18, 19, 20, 26, 22, 21, 24,
199984 /* 50 */ 6, 7, 9, 9, 10, 12, 12, 6, 7, 21,
199985 /* 60 */ 9, 24, 25, 12, 18, 5, 20, 14, 22, 5,
199986 /* 70 */ 24, 3, 1, 2, 3, 1, 2, 3, 0, 1,
199987 /* 80 */ 2, 3, 11, 2, 3, 11, 9, 10, 5, 12,
199988 /* 90 */ 23, 24, 10, 10, 8, 9, 9, 15, 15, 8,
199989 /* 100 */ 9, 9, 27, 27, 11, 27, 27, 27, 27, 27,
199990 /* 110 */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
199991 /* 120 */ 27,
199992};
199993#define fts5YY_SHIFT_COUNT (34)
199994#define fts5YY_SHIFT_MIN (0)
199995#define fts5YY_SHIFT_MAX (93)
199996static const unsigned char fts5yy_shift_ofst[] = {
199997 /* 0 */ 44, 44, 44, 44, 44, 44, 51, 77, 43, 12,
199998 /* 10 */ 14, 83, 82, 14, 23, 23, 31, 31, 71, 74,
199999 /* 20 */ 78, 81, 86, 91, 6, 53, 53, 60, 64, 68,
200000 /* 30 */ 53, 87, 92, 53, 93,
200001};
200002#define fts5YY_REDUCE_COUNT (17)
200003#define fts5YY_REDUCE_MIN (-17)
200004#define fts5YY_REDUCE_MAX (67)
200005static const signed char fts5yy_reduce_ofst[] = {
200006 /* 0 */ -16, -8, 0, 9, 17, 25, 46, -17, -17, 37,
200007 /* 10 */ 67, 4, 4, 8, 4, 20, 27, 38,
200008};
200009static const fts5YYACTIONTYPE fts5yy_default[] = {
200010 /* 0 */ 80, 80, 80, 80, 80, 80, 95, 80, 80, 105,
200011 /* 10 */ 80, 110, 110, 80, 110, 110, 80, 80, 80, 80,
200012 /* 20 */ 80, 91, 80, 80, 80, 101, 100, 80, 80, 90,
200013 /* 30 */ 103, 80, 80, 104, 80,
200014};
200015/********** End of lemon-generated parsing tables *****************************/
200016
200017/* The next table maps tokens (terminal symbols) into fallback tokens.
200018** If a construct like the following:
200019**
200020** %fallback ID X Y Z.
200021**
200022** appears in the grammar, then ID becomes a fallback token for X, Y,
200023** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
200024** but it does not parse, the type of the token is changed to ID and
200025** the parse is retried before an error is thrown.
200026**
200027** This feature can be used, for example, to cause some keywords in a language
200028** to revert to identifiers if they keyword does not apply in the context where
200029** it appears.
200030*/
200031#ifdef fts5YYFALLBACK
200032static const fts5YYCODETYPE fts5yyFallback[] = {
200033};
200034#endif /* fts5YYFALLBACK */
200035
200036/* The following structure represents a single element of the
200037** parser's stack. Information stored includes:
200038**
200039** + The state number for the parser at this level of the stack.
200040**
200041** + The value of the token stored at this level of the stack.
200042** (In other words, the "major" token.)
200043**
200044** + The semantic value stored at this level of the stack. This is
200045** the information used by the action routines in the grammar.
200046** It is sometimes called the "minor" token.
200047**
200048** After the "shift" half of a SHIFTREDUCE action, the stateno field
200049** actually contains the reduce action for the second half of the
200050** SHIFTREDUCE.
200051*/
200052struct fts5yyStackEntry {
200053 fts5YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */
200054 fts5YYCODETYPE major; /* The major token value. This is the code
200055 ** number for the token at this stack level */
200056 fts5YYMINORTYPE minor; /* The user-supplied minor token value. This
200057 ** is the value of the token */
200058};
200059typedef struct fts5yyStackEntry fts5yyStackEntry;
200060
200061/* The state of the parser is completely contained in an instance of
200062** the following structure */
200063struct fts5yyParser {
200064 fts5yyStackEntry *fts5yytos; /* Pointer to top element of the stack */
200065#ifdef fts5YYTRACKMAXSTACKDEPTH
200066 int fts5yyhwm; /* High-water mark of the stack */
200067#endif
200068#ifndef fts5YYNOERRORRECOVERY
200069 int fts5yyerrcnt; /* Shifts left before out of the error */
200070#endif
200071 sqlite3Fts5ParserARG_SDECL /* A place to hold %extra_argument */
200072 sqlite3Fts5ParserCTX_SDECL /* A place to hold %extra_context */
200073#if fts5YYSTACKDEPTH<=0
200074 int fts5yystksz; /* Current side of the stack */
200075 fts5yyStackEntry *fts5yystack; /* The parser's stack */
200076 fts5yyStackEntry fts5yystk0; /* First stack entry */
200077#else
200078 fts5yyStackEntry fts5yystack[fts5YYSTACKDEPTH]; /* The parser's stack */
200079 fts5yyStackEntry *fts5yystackEnd; /* Last entry in the stack */
200080#endif
200081};
200082typedef struct fts5yyParser fts5yyParser;
200083
200084#ifndef NDEBUG
200085/* #include <stdio.h> */
200086static FILE *fts5yyTraceFILE = 0;
200087static char *fts5yyTracePrompt = 0;
200088#endif /* NDEBUG */
200089
200090#ifndef NDEBUG
200091/*
200092** Turn parser tracing on by giving a stream to which to write the trace
200093** and a prompt to preface each trace message. Tracing is turned off
200094** by making either argument NULL
200095**
200096** Inputs:
200097** <ul>
200098** <li> A FILE* to which trace output should be written.
200099** If NULL, then tracing is turned off.
200100** <li> A prefix string written at the beginning of every
200101** line of trace output. If NULL, then tracing is
200102** turned off.
200103** </ul>
200104**
200105** Outputs:
200106** None.
200107*/
200108static void sqlite3Fts5ParserTrace(FILE *TraceFILE, char *zTracePrompt){
200109 fts5yyTraceFILE = TraceFILE;
200110 fts5yyTracePrompt = zTracePrompt;
200111 if( fts5yyTraceFILE==0 ) fts5yyTracePrompt = 0;
200112 else if( fts5yyTracePrompt==0 ) fts5yyTraceFILE = 0;
200113}
200114#endif /* NDEBUG */
200115
200116#if defined(fts5YYCOVERAGE) || !defined(NDEBUG)
200117/* For tracing shifts, the names of all terminals and nonterminals
200118** are required. The following table supplies these names */
200119static const char *const fts5yyTokenName[] = {
200120 /* 0 */ "$",
200121 /* 1 */ "OR",
200122 /* 2 */ "AND",
200123 /* 3 */ "NOT",
200124 /* 4 */ "TERM",
200125 /* 5 */ "COLON",
200126 /* 6 */ "MINUS",
200127 /* 7 */ "LCP",
200128 /* 8 */ "RCP",
200129 /* 9 */ "STRING",
200130 /* 10 */ "LP",
200131 /* 11 */ "RP",
200132 /* 12 */ "CARET",
200133 /* 13 */ "COMMA",
200134 /* 14 */ "PLUS",
200135 /* 15 */ "STAR",
200136 /* 16 */ "input",
200137 /* 17 */ "expr",
200138 /* 18 */ "cnearset",
200139 /* 19 */ "exprlist",
200140 /* 20 */ "colset",
200141 /* 21 */ "colsetlist",
200142 /* 22 */ "nearset",
200143 /* 23 */ "nearphrases",
200144 /* 24 */ "phrase",
200145 /* 25 */ "neardist_opt",
200146 /* 26 */ "star_opt",
200147};
200148#endif /* defined(fts5YYCOVERAGE) || !defined(NDEBUG) */
200149
200150#ifndef NDEBUG
200151/* For tracing reduce actions, the names of all rules are required.
200152*/
200153static const char *const fts5yyRuleName[] = {
200154 /* 0 */ "input ::= expr",
200155 /* 1 */ "colset ::= MINUS LCP colsetlist RCP",
200156 /* 2 */ "colset ::= LCP colsetlist RCP",
200157 /* 3 */ "colset ::= STRING",
200158 /* 4 */ "colset ::= MINUS STRING",
200159 /* 5 */ "colsetlist ::= colsetlist STRING",
200160 /* 6 */ "colsetlist ::= STRING",
200161 /* 7 */ "expr ::= expr AND expr",
200162 /* 8 */ "expr ::= expr OR expr",
200163 /* 9 */ "expr ::= expr NOT expr",
200164 /* 10 */ "expr ::= colset COLON LP expr RP",
200165 /* 11 */ "expr ::= LP expr RP",
200166 /* 12 */ "expr ::= exprlist",
200167 /* 13 */ "exprlist ::= cnearset",
200168 /* 14 */ "exprlist ::= exprlist cnearset",
200169 /* 15 */ "cnearset ::= nearset",
200170 /* 16 */ "cnearset ::= colset COLON nearset",
200171 /* 17 */ "nearset ::= phrase",
200172 /* 18 */ "nearset ::= CARET phrase",
200173 /* 19 */ "nearset ::= STRING LP nearphrases neardist_opt RP",
200174 /* 20 */ "nearphrases ::= phrase",
200175 /* 21 */ "nearphrases ::= nearphrases phrase",
200176 /* 22 */ "neardist_opt ::=",
200177 /* 23 */ "neardist_opt ::= COMMA STRING",
200178 /* 24 */ "phrase ::= phrase PLUS STRING star_opt",
200179 /* 25 */ "phrase ::= STRING star_opt",
200180 /* 26 */ "star_opt ::= STAR",
200181 /* 27 */ "star_opt ::=",
200182};
200183#endif /* NDEBUG */
200184
200185
200186#if fts5YYSTACKDEPTH<=0
200187/*
200188** Try to increase the size of the parser stack. Return the number
200189** of errors. Return 0 on success.
200190*/
200191static int fts5yyGrowStack(fts5yyParser *p){
200192 int newSize;
200193 int idx;
200194 fts5yyStackEntry *pNew;
200195
200196 newSize = p->fts5yystksz*2 + 100;
200197 idx = p->fts5yytos ? (int)(p->fts5yytos - p->fts5yystack) : 0;
200198 if( p->fts5yystack==&p->fts5yystk0 ){
200199 pNew = malloc(newSize*sizeof(pNew[0]));
200200 if( pNew ) pNew[0] = p->fts5yystk0;
200201 }else{
200202 pNew = realloc(p->fts5yystack, newSize*sizeof(pNew[0]));
200203 }
200204 if( pNew ){
200205 p->fts5yystack = pNew;
200206 p->fts5yytos = &p->fts5yystack[idx];
200207#ifndef NDEBUG
200208 if( fts5yyTraceFILE ){
200209 fprintf(fts5yyTraceFILE,"%sStack grows from %d to %d entries.\n",
200210 fts5yyTracePrompt, p->fts5yystksz, newSize);
200211 }
200212#endif
200213 p->fts5yystksz = newSize;
200214 }
200215 return pNew==0;
200216}
200217#endif
200218
200219/* Datatype of the argument to the memory allocated passed as the
200220** second argument to sqlite3Fts5ParserAlloc() below. This can be changed by
200221** putting an appropriate #define in the %include section of the input
200222** grammar.
200223*/
200224#ifndef fts5YYMALLOCARGTYPE
200225# define fts5YYMALLOCARGTYPE size_t
200226#endif
200227
200228/* Initialize a new parser that has already been allocated.
200229*/
200230static void sqlite3Fts5ParserInit(void *fts5yypRawParser sqlite3Fts5ParserCTX_PDECL){
200231 fts5yyParser *fts5yypParser = (fts5yyParser*)fts5yypRawParser;
200232 sqlite3Fts5ParserCTX_STORE
200233#ifdef fts5YYTRACKMAXSTACKDEPTH
200234 fts5yypParser->fts5yyhwm = 0;
200235#endif
200236#if fts5YYSTACKDEPTH<=0
200237 fts5yypParser->fts5yytos = NULL;
200238 fts5yypParser->fts5yystack = NULL;
200239 fts5yypParser->fts5yystksz = 0;
200240 if( fts5yyGrowStack(fts5yypParser) ){
200241 fts5yypParser->fts5yystack = &fts5yypParser->fts5yystk0;
200242 fts5yypParser->fts5yystksz = 1;
200243 }
200244#endif
200245#ifndef fts5YYNOERRORRECOVERY
200246 fts5yypParser->fts5yyerrcnt = -1;
200247#endif
200248 fts5yypParser->fts5yytos = fts5yypParser->fts5yystack;
200249 fts5yypParser->fts5yystack[0].stateno = 0;
200250 fts5yypParser->fts5yystack[0].major = 0;
200251#if fts5YYSTACKDEPTH>0
200252 fts5yypParser->fts5yystackEnd = &fts5yypParser->fts5yystack[fts5YYSTACKDEPTH-1];
200253#endif
200254}
200255
200256#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
200257/*
200258** This function allocates a new parser.
200259** The only argument is a pointer to a function which works like
200260** malloc.
200261**
200262** Inputs:
200263** A pointer to the function used to allocate memory.
200264**
200265** Outputs:
200266** A pointer to a parser. This pointer is used in subsequent calls
200267** to sqlite3Fts5Parser and sqlite3Fts5ParserFree.
200268*/
200269static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(fts5YYMALLOCARGTYPE) sqlite3Fts5ParserCTX_PDECL){
200270 fts5yyParser *fts5yypParser;
200271 fts5yypParser = (fts5yyParser*)(*mallocProc)( (fts5YYMALLOCARGTYPE)sizeof(fts5yyParser) );
200272 if( fts5yypParser ){
200273 sqlite3Fts5ParserCTX_STORE
200274 sqlite3Fts5ParserInit(fts5yypParser sqlite3Fts5ParserCTX_PARAM);
200275 }
200276 return (void*)fts5yypParser;
200277}
200278#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
200279
200280
200281/* The following function deletes the "minor type" or semantic value
200282** associated with a symbol. The symbol can be either a terminal
200283** or nonterminal. "fts5yymajor" is the symbol code, and "fts5yypminor" is
200284** a pointer to the value to be deleted. The code used to do the
200285** deletions is derived from the %destructor and/or %token_destructor
200286** directives of the input grammar.
200287*/
200288static void fts5yy_destructor(
200289 fts5yyParser *fts5yypParser, /* The parser */
200290 fts5YYCODETYPE fts5yymajor, /* Type code for object to destroy */
200291 fts5YYMINORTYPE *fts5yypminor /* The object to be destroyed */
200292){
200293 sqlite3Fts5ParserARG_FETCH
200294 sqlite3Fts5ParserCTX_FETCH
200295 switch( fts5yymajor ){
200296 /* Here is inserted the actions which take place when a
200297 ** terminal or non-terminal is destroyed. This can happen
200298 ** when the symbol is popped from the stack during a
200299 ** reduce or during error processing or when a parser is
200300 ** being destroyed before it is finished parsing.
200301 **
200302 ** Note: during a reduce, the only symbols destroyed are those
200303 ** which appear on the RHS of the rule, but which are *not* used
200304 ** inside the C code.
200305 */
200306/********* Begin destructor definitions ***************************************/
200307 case 16: /* input */
200308{
200309 (void)pParse;
200310}
200311 break;
200312 case 17: /* expr */
200313 case 18: /* cnearset */
200314 case 19: /* exprlist */
200315{
200316 sqlite3Fts5ParseNodeFree((fts5yypminor->fts5yy24));
200317}
200318 break;
200319 case 20: /* colset */
200320 case 21: /* colsetlist */
200321{
200322 sqlite3_free((fts5yypminor->fts5yy11));
200323}
200324 break;
200325 case 22: /* nearset */
200326 case 23: /* nearphrases */
200327{
200328 sqlite3Fts5ParseNearsetFree((fts5yypminor->fts5yy46));
200329}
200330 break;
200331 case 24: /* phrase */
200332{
200333 sqlite3Fts5ParsePhraseFree((fts5yypminor->fts5yy53));
200334}
200335 break;
200336/********* End destructor definitions *****************************************/
200337 default: break; /* If no destructor action specified: do nothing */
200338 }
200339}
200340
200341/*
200342** Pop the parser's stack once.
200343**
200344** If there is a destructor routine associated with the token which
200345** is popped from the stack, then call it.
200346*/
200347static void fts5yy_pop_parser_stack(fts5yyParser *pParser){
200348 fts5yyStackEntry *fts5yytos;
200349 assert( pParser->fts5yytos!=0 );
200350 assert( pParser->fts5yytos > pParser->fts5yystack );
200351 fts5yytos = pParser->fts5yytos--;
200352#ifndef NDEBUG
200353 if( fts5yyTraceFILE ){
200354 fprintf(fts5yyTraceFILE,"%sPopping %s\n",
200355 fts5yyTracePrompt,
200356 fts5yyTokenName[fts5yytos->major]);
200357 }
200358#endif
200359 fts5yy_destructor(pParser, fts5yytos->major, &fts5yytos->minor);
200360}
200361
200362/*
200363** Clear all secondary memory allocations from the parser
200364*/
200365static void sqlite3Fts5ParserFinalize(void *p){
200366 fts5yyParser *pParser = (fts5yyParser*)p;
200367 while( pParser->fts5yytos>pParser->fts5yystack ) fts5yy_pop_parser_stack(pParser);
200368#if fts5YYSTACKDEPTH<=0
200369 if( pParser->fts5yystack!=&pParser->fts5yystk0 ) free(pParser->fts5yystack);
200370#endif
200371}
200372
200373#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
200374/*
200375** Deallocate and destroy a parser. Destructors are called for
200376** all stack elements before shutting the parser down.
200377**
200378** If the fts5YYPARSEFREENEVERNULL macro exists (for example because it
200379** is defined in a %include section of the input grammar) then it is
200380** assumed that the input pointer is never NULL.
200381*/
200382static void sqlite3Fts5ParserFree(
200383 void *p, /* The parser to be deleted */
200384 void (*freeProc)(void*) /* Function used to reclaim memory */
200385){
200386#ifndef fts5YYPARSEFREENEVERNULL
200387 if( p==0 ) return;
200388#endif
200389 sqlite3Fts5ParserFinalize(p);
200390 (*freeProc)(p);
200391}
200392#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
200393
200394/*
200395** Return the peak depth of the stack for a parser.
200396*/
200397#ifdef fts5YYTRACKMAXSTACKDEPTH
200398static int sqlite3Fts5ParserStackPeak(void *p){
200399 fts5yyParser *pParser = (fts5yyParser*)p;
200400 return pParser->fts5yyhwm;
200401}
200402#endif
200403
200404/* This array of booleans keeps track of the parser statement
200405** coverage. The element fts5yycoverage[X][Y] is set when the parser
200406** is in state X and has a lookahead token Y. In a well-tested
200407** systems, every element of this matrix should end up being set.
200408*/
200409#if defined(fts5YYCOVERAGE)
200410static unsigned char fts5yycoverage[fts5YYNSTATE][fts5YYNFTS5TOKEN];
200411#endif
200412
200413/*
200414** Write into out a description of every state/lookahead combination that
200415**
200416** (1) has not been used by the parser, and
200417** (2) is not a syntax error.
200418**
200419** Return the number of missed state/lookahead combinations.
200420*/
200421#if defined(fts5YYCOVERAGE)
200422static int sqlite3Fts5ParserCoverage(FILE *out){
200423 int stateno, iLookAhead, i;
200424 int nMissed = 0;
200425 for(stateno=0; stateno<fts5YYNSTATE; stateno++){
200426 i = fts5yy_shift_ofst[stateno];
200427 for(iLookAhead=0; iLookAhead<fts5YYNFTS5TOKEN; iLookAhead++){
200428 if( fts5yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
200429 if( fts5yycoverage[stateno][iLookAhead]==0 ) nMissed++;
200430 if( out ){
200431 fprintf(out,"State %d lookahead %s %s\n", stateno,
200432 fts5yyTokenName[iLookAhead],
200433 fts5yycoverage[stateno][iLookAhead] ? "ok" : "missed");
200434 }
200435 }
200436 }
200437 return nMissed;
200438}
200439#endif
200440
200441/*
200442** Find the appropriate action for a parser given the terminal
200443** look-ahead token iLookAhead.
200444*/
200445static fts5YYACTIONTYPE fts5yy_find_shift_action(
200446 fts5YYCODETYPE iLookAhead, /* The look-ahead token */
200447 fts5YYACTIONTYPE stateno /* Current state number */
200448){
200449 int i;
200450
200451 if( stateno>fts5YY_MAX_SHIFT ) return stateno;
200452 assert( stateno <= fts5YY_SHIFT_COUNT );
200453#if defined(fts5YYCOVERAGE)
200454 fts5yycoverage[stateno][iLookAhead] = 1;
200455#endif
200456 do{
200457 i = fts5yy_shift_ofst[stateno];
200458 assert( i>=0 );
200459 /* assert( i+fts5YYNFTS5TOKEN<=(int)fts5YY_NLOOKAHEAD ); */
200460 assert( iLookAhead!=fts5YYNOCODE );
200461 assert( iLookAhead < fts5YYNFTS5TOKEN );
200462 i += iLookAhead;
200463 if( i>=fts5YY_NLOOKAHEAD || fts5yy_lookahead[i]!=iLookAhead ){
200464#ifdef fts5YYFALLBACK
200465 fts5YYCODETYPE iFallback; /* Fallback token */
200466 if( iLookAhead<sizeof(fts5yyFallback)/sizeof(fts5yyFallback[0])
200467 && (iFallback = fts5yyFallback[iLookAhead])!=0 ){
200468#ifndef NDEBUG
200469 if( fts5yyTraceFILE ){
200470 fprintf(fts5yyTraceFILE, "%sFALLBACK %s => %s\n",
200471 fts5yyTracePrompt, fts5yyTokenName[iLookAhead], fts5yyTokenName[iFallback]);
200472 }
200473#endif
200474 assert( fts5yyFallback[iFallback]==0 ); /* Fallback loop must terminate */
200475 iLookAhead = iFallback;
200476 continue;
200477 }
200478#endif
200479#ifdef fts5YYWILDCARD
200480 {
200481 int j = i - iLookAhead + fts5YYWILDCARD;
200482 if(
200483#if fts5YY_SHIFT_MIN+fts5YYWILDCARD<0
200484 j>=0 &&
200485#endif
200486#if fts5YY_SHIFT_MAX+fts5YYWILDCARD>=fts5YY_ACTTAB_COUNT
200487 j<fts5YY_ACTTAB_COUNT &&
200488#endif
200489 j<(int)(sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0])) &&
200490 fts5yy_lookahead[j]==fts5YYWILDCARD && iLookAhead>0
200491 ){
200492#ifndef NDEBUG
200493 if( fts5yyTraceFILE ){
200494 fprintf(fts5yyTraceFILE, "%sWILDCARD %s => %s\n",
200495 fts5yyTracePrompt, fts5yyTokenName[iLookAhead],
200496 fts5yyTokenName[fts5YYWILDCARD]);
200497 }
200498#endif /* NDEBUG */
200499 return fts5yy_action[j];
200500 }
200501 }
200502#endif /* fts5YYWILDCARD */
200503 return fts5yy_default[stateno];
200504 }else{
200505 return fts5yy_action[i];
200506 }
200507 }while(1);
200508}
200509
200510/*
200511** Find the appropriate action for a parser given the non-terminal
200512** look-ahead token iLookAhead.
200513*/
200514static fts5YYACTIONTYPE fts5yy_find_reduce_action(
200515 fts5YYACTIONTYPE stateno, /* Current state number */
200516 fts5YYCODETYPE iLookAhead /* The look-ahead token */
200517){
200518 int i;
200519#ifdef fts5YYERRORSYMBOL
200520 if( stateno>fts5YY_REDUCE_COUNT ){
200521 return fts5yy_default[stateno];
200522 }
200523#else
200524 assert( stateno<=fts5YY_REDUCE_COUNT );
200525#endif
200526 i = fts5yy_reduce_ofst[stateno];
200527 assert( iLookAhead!=fts5YYNOCODE );
200528 i += iLookAhead;
200529#ifdef fts5YYERRORSYMBOL
200530 if( i<0 || i>=fts5YY_ACTTAB_COUNT || fts5yy_lookahead[i]!=iLookAhead ){
200531 return fts5yy_default[stateno];
200532 }
200533#else
200534 assert( i>=0 && i<fts5YY_ACTTAB_COUNT );
200535 assert( fts5yy_lookahead[i]==iLookAhead );
200536#endif
200537 return fts5yy_action[i];
200538}
200539
200540/*
200541** The following routine is called if the stack overflows.
200542*/
200543static void fts5yyStackOverflow(fts5yyParser *fts5yypParser){
200544 sqlite3Fts5ParserARG_FETCH
200545 sqlite3Fts5ParserCTX_FETCH
200546#ifndef NDEBUG
200547 if( fts5yyTraceFILE ){
200548 fprintf(fts5yyTraceFILE,"%sStack Overflow!\n",fts5yyTracePrompt);
200549 }
200550#endif
200551 while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(fts5yypParser);
200552 /* Here code is inserted which will execute if the parser
200553 ** stack every overflows */
200554/******** Begin %stack_overflow code ******************************************/
200555
200556 sqlite3Fts5ParseError(pParse, "fts5: parser stack overflow");
200557/******** End %stack_overflow code ********************************************/
200558 sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument var */
200559 sqlite3Fts5ParserCTX_STORE
200560}
200561
200562/*
200563** Print tracing information for a SHIFT action
200564*/
200565#ifndef NDEBUG
200566static void fts5yyTraceShift(fts5yyParser *fts5yypParser, int fts5yyNewState, const char *zTag){
200567 if( fts5yyTraceFILE ){
200568 if( fts5yyNewState<fts5YYNSTATE ){
200569 fprintf(fts5yyTraceFILE,"%s%s '%s', go to state %d\n",
200570 fts5yyTracePrompt, zTag, fts5yyTokenName[fts5yypParser->fts5yytos->major],
200571 fts5yyNewState);
200572 }else{
200573 fprintf(fts5yyTraceFILE,"%s%s '%s', pending reduce %d\n",
200574 fts5yyTracePrompt, zTag, fts5yyTokenName[fts5yypParser->fts5yytos->major],
200575 fts5yyNewState - fts5YY_MIN_REDUCE);
200576 }
200577 }
200578}
200579#else
200580# define fts5yyTraceShift(X,Y,Z)
200581#endif
200582
200583/*
200584** Perform a shift action.
200585*/
200586static void fts5yy_shift(
200587 fts5yyParser *fts5yypParser, /* The parser to be shifted */
200588 fts5YYACTIONTYPE fts5yyNewState, /* The new state to shift in */
200589 fts5YYCODETYPE fts5yyMajor, /* The major token to shift in */
200590 sqlite3Fts5ParserFTS5TOKENTYPE fts5yyMinor /* The minor token to shift in */
200591){
200592 fts5yyStackEntry *fts5yytos;
200593 fts5yypParser->fts5yytos++;
200594#ifdef fts5YYTRACKMAXSTACKDEPTH
200595 if( (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack)>fts5yypParser->fts5yyhwm ){
200596 fts5yypParser->fts5yyhwm++;
200597 assert( fts5yypParser->fts5yyhwm == (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack) );
200598 }
200599#endif
200600#if fts5YYSTACKDEPTH>0
200601 if( fts5yypParser->fts5yytos>fts5yypParser->fts5yystackEnd ){
200602 fts5yypParser->fts5yytos--;
200603 fts5yyStackOverflow(fts5yypParser);
200604 return;
200605 }
200606#else
200607 if( fts5yypParser->fts5yytos>=&fts5yypParser->fts5yystack[fts5yypParser->fts5yystksz] ){
200608 if( fts5yyGrowStack(fts5yypParser) ){
200609 fts5yypParser->fts5yytos--;
200610 fts5yyStackOverflow(fts5yypParser);
200611 return;
200612 }
200613 }
200614#endif
200615 if( fts5yyNewState > fts5YY_MAX_SHIFT ){
200616 fts5yyNewState += fts5YY_MIN_REDUCE - fts5YY_MIN_SHIFTREDUCE;
200617 }
200618 fts5yytos = fts5yypParser->fts5yytos;
200619 fts5yytos->stateno = fts5yyNewState;
200620 fts5yytos->major = fts5yyMajor;
200621 fts5yytos->minor.fts5yy0 = fts5yyMinor;
200622 fts5yyTraceShift(fts5yypParser, fts5yyNewState, "Shift");
200623}
200624
200625/* The following table contains information about every rule that
200626** is used during the reduce.
200627*/
200628static const struct {
200629 fts5YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */
200630 signed char nrhs; /* Negative of the number of RHS symbols in the rule */
200631} fts5yyRuleInfo[] = {
200632 { 16, -1 }, /* (0) input ::= expr */
200633 { 20, -4 }, /* (1) colset ::= MINUS LCP colsetlist RCP */
200634 { 20, -3 }, /* (2) colset ::= LCP colsetlist RCP */
200635 { 20, -1 }, /* (3) colset ::= STRING */
200636 { 20, -2 }, /* (4) colset ::= MINUS STRING */
200637 { 21, -2 }, /* (5) colsetlist ::= colsetlist STRING */
200638 { 21, -1 }, /* (6) colsetlist ::= STRING */
200639 { 17, -3 }, /* (7) expr ::= expr AND expr */
200640 { 17, -3 }, /* (8) expr ::= expr OR expr */
200641 { 17, -3 }, /* (9) expr ::= expr NOT expr */
200642 { 17, -5 }, /* (10) expr ::= colset COLON LP expr RP */
200643 { 17, -3 }, /* (11) expr ::= LP expr RP */
200644 { 17, -1 }, /* (12) expr ::= exprlist */
200645 { 19, -1 }, /* (13) exprlist ::= cnearset */
200646 { 19, -2 }, /* (14) exprlist ::= exprlist cnearset */
200647 { 18, -1 }, /* (15) cnearset ::= nearset */
200648 { 18, -3 }, /* (16) cnearset ::= colset COLON nearset */
200649 { 22, -1 }, /* (17) nearset ::= phrase */
200650 { 22, -2 }, /* (18) nearset ::= CARET phrase */
200651 { 22, -5 }, /* (19) nearset ::= STRING LP nearphrases neardist_opt RP */
200652 { 23, -1 }, /* (20) nearphrases ::= phrase */
200653 { 23, -2 }, /* (21) nearphrases ::= nearphrases phrase */
200654 { 25, 0 }, /* (22) neardist_opt ::= */
200655 { 25, -2 }, /* (23) neardist_opt ::= COMMA STRING */
200656 { 24, -4 }, /* (24) phrase ::= phrase PLUS STRING star_opt */
200657 { 24, -2 }, /* (25) phrase ::= STRING star_opt */
200658 { 26, -1 }, /* (26) star_opt ::= STAR */
200659 { 26, 0 }, /* (27) star_opt ::= */
200660};
200661
200662static void fts5yy_accept(fts5yyParser*); /* Forward Declaration */
200663
200664/*
200665** Perform a reduce action and the shift that must immediately
200666** follow the reduce.
200667**
200668** The fts5yyLookahead and fts5yyLookaheadToken parameters provide reduce actions
200669** access to the lookahead token (if any). The fts5yyLookahead will be fts5YYNOCODE
200670** if the lookahead token has already been consumed. As this procedure is
200671** only called from one place, optimizing compilers will in-line it, which
200672** means that the extra parameters have no performance impact.
200673*/
200674static fts5YYACTIONTYPE fts5yy_reduce(
200675 fts5yyParser *fts5yypParser, /* The parser */
200676 unsigned int fts5yyruleno, /* Number of the rule by which to reduce */
200677 int fts5yyLookahead, /* Lookahead token, or fts5YYNOCODE if none */
200678 sqlite3Fts5ParserFTS5TOKENTYPE fts5yyLookaheadToken /* Value of the lookahead token */
200679 sqlite3Fts5ParserCTX_PDECL /* %extra_context */
200680){
200681 int fts5yygoto; /* The next state */
200682 fts5YYACTIONTYPE fts5yyact; /* The next action */
200683 fts5yyStackEntry *fts5yymsp; /* The top of the parser's stack */
200684 int fts5yysize; /* Amount to pop the stack */
200685 sqlite3Fts5ParserARG_FETCH
200686 (void)fts5yyLookahead;
200687 (void)fts5yyLookaheadToken;
200688 fts5yymsp = fts5yypParser->fts5yytos;
200689#ifndef NDEBUG
200690 if( fts5yyTraceFILE && fts5yyruleno<(int)(sizeof(fts5yyRuleName)/sizeof(fts5yyRuleName[0])) ){
200691 fts5yysize = fts5yyRuleInfo[fts5yyruleno].nrhs;
200692 if( fts5yysize ){
200693 fprintf(fts5yyTraceFILE, "%sReduce %d [%s], go to state %d.\n",
200694 fts5yyTracePrompt,
200695 fts5yyruleno, fts5yyRuleName[fts5yyruleno], fts5yymsp[fts5yysize].stateno);
200696 }else{
200697 fprintf(fts5yyTraceFILE, "%sReduce %d [%s].\n",
200698 fts5yyTracePrompt, fts5yyruleno, fts5yyRuleName[fts5yyruleno]);
200699 }
200700 }
200701#endif /* NDEBUG */
200702
200703 /* Check that the stack is large enough to grow by a single entry
200704 ** if the RHS of the rule is empty. This ensures that there is room
200705 ** enough on the stack to push the LHS value */
200706 if( fts5yyRuleInfo[fts5yyruleno].nrhs==0 ){
200707#ifdef fts5YYTRACKMAXSTACKDEPTH
200708 if( (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack)>fts5yypParser->fts5yyhwm ){
200709 fts5yypParser->fts5yyhwm++;
200710 assert( fts5yypParser->fts5yyhwm == (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack));
200711 }
200712#endif
200713#if fts5YYSTACKDEPTH>0
200714 if( fts5yypParser->fts5yytos>=fts5yypParser->fts5yystackEnd ){
200715 fts5yyStackOverflow(fts5yypParser);
200716 /* The call to fts5yyStackOverflow() above pops the stack until it is
200717 ** empty, causing the main parser loop to exit. So the return value
200718 ** is never used and does not matter. */
200719 return 0;
200720 }
200721#else
200722 if( fts5yypParser->fts5yytos>=&fts5yypParser->fts5yystack[fts5yypParser->fts5yystksz-1] ){
200723 if( fts5yyGrowStack(fts5yypParser) ){
200724 fts5yyStackOverflow(fts5yypParser);
200725 /* The call to fts5yyStackOverflow() above pops the stack until it is
200726 ** empty, causing the main parser loop to exit. So the return value
200727 ** is never used and does not matter. */
200728 return 0;
200729 }
200730 fts5yymsp = fts5yypParser->fts5yytos;
200731 }
200732#endif
200733 }
200734
200735 switch( fts5yyruleno ){
200736 /* Beginning here are the reduction cases. A typical example
200737 ** follows:
200738 ** case 0:
200739 ** #line <lineno> <grammarfile>
200740 ** { ... } // User supplied code
200741 ** #line <lineno> <thisfile>
200742 ** break;
200743 */
200744/********** Begin reduce actions **********************************************/
200745 fts5YYMINORTYPE fts5yylhsminor;
200746 case 0: /* input ::= expr */
200747{ sqlite3Fts5ParseFinished(pParse, fts5yymsp[0].minor.fts5yy24); }
200748 break;
200749 case 1: /* colset ::= MINUS LCP colsetlist RCP */
200750{
200751 fts5yymsp[-3].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
200752}
200753 break;
200754 case 2: /* colset ::= LCP colsetlist RCP */
200755{ fts5yymsp[-2].minor.fts5yy11 = fts5yymsp[-1].minor.fts5yy11; }
200756 break;
200757 case 3: /* colset ::= STRING */
200758{
200759 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
200760}
200761 fts5yymsp[0].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
200762 break;
200763 case 4: /* colset ::= MINUS STRING */
200764{
200765 fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
200766 fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
200767}
200768 break;
200769 case 5: /* colsetlist ::= colsetlist STRING */
200770{
200771 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, fts5yymsp[-1].minor.fts5yy11, &fts5yymsp[0].minor.fts5yy0); }
200772 fts5yymsp[-1].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
200773 break;
200774 case 6: /* colsetlist ::= STRING */
200775{
200776 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
200777}
200778 fts5yymsp[0].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
200779 break;
200780 case 7: /* expr ::= expr AND expr */
200781{
200782 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_AND, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
200783}
200784 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
200785 break;
200786 case 8: /* expr ::= expr OR expr */
200787{
200788 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_OR, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
200789}
200790 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
200791 break;
200792 case 9: /* expr ::= expr NOT expr */
200793{
200794 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_NOT, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
200795}
200796 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
200797 break;
200798 case 10: /* expr ::= colset COLON LP expr RP */
200799{
200800 sqlite3Fts5ParseSetColset(pParse, fts5yymsp[-1].minor.fts5yy24, fts5yymsp[-4].minor.fts5yy11);
200801 fts5yylhsminor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;
200802}
200803 fts5yymsp[-4].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
200804 break;
200805 case 11: /* expr ::= LP expr RP */
200806{fts5yymsp[-2].minor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;}
200807 break;
200808 case 12: /* expr ::= exprlist */
200809 case 13: /* exprlist ::= cnearset */ fts5yytestcase(fts5yyruleno==13);
200810{fts5yylhsminor.fts5yy24 = fts5yymsp[0].minor.fts5yy24;}
200811 fts5yymsp[0].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
200812 break;
200813 case 14: /* exprlist ::= exprlist cnearset */
200814{
200815 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseImplicitAnd(pParse, fts5yymsp[-1].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24);
200816}
200817 fts5yymsp[-1].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
200818 break;
200819 case 15: /* cnearset ::= nearset */
200820{
200821 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_STRING, 0, 0, fts5yymsp[0].minor.fts5yy46);
200822}
200823 fts5yymsp[0].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
200824 break;
200825 case 16: /* cnearset ::= colset COLON nearset */
200826{
200827 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_STRING, 0, 0, fts5yymsp[0].minor.fts5yy46);
200828 sqlite3Fts5ParseSetColset(pParse, fts5yylhsminor.fts5yy24, fts5yymsp[-2].minor.fts5yy11);
200829}
200830 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
200831 break;
200832 case 17: /* nearset ::= phrase */
200833{ fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53); }
200834 fts5yymsp[0].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
200835 break;
200836 case 18: /* nearset ::= CARET phrase */
200837{
200838 sqlite3Fts5ParseSetCaret(fts5yymsp[0].minor.fts5yy53);
200839 fts5yymsp[-1].minor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53);
200840}
200841 break;
200842 case 19: /* nearset ::= STRING LP nearphrases neardist_opt RP */
200843{
200844 sqlite3Fts5ParseNear(pParse, &fts5yymsp[-4].minor.fts5yy0);
200845 sqlite3Fts5ParseSetDistance(pParse, fts5yymsp[-2].minor.fts5yy46, &fts5yymsp[-1].minor.fts5yy0);
200846 fts5yylhsminor.fts5yy46 = fts5yymsp[-2].minor.fts5yy46;
200847}
200848 fts5yymsp[-4].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
200849 break;
200850 case 20: /* nearphrases ::= phrase */
200851{
200852 fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53);
200853}
200854 fts5yymsp[0].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
200855 break;
200856 case 21: /* nearphrases ::= nearphrases phrase */
200857{
200858 fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, fts5yymsp[-1].minor.fts5yy46, fts5yymsp[0].minor.fts5yy53);
200859}
200860 fts5yymsp[-1].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
200861 break;
200862 case 22: /* neardist_opt ::= */
200863{ fts5yymsp[1].minor.fts5yy0.p = 0; fts5yymsp[1].minor.fts5yy0.n = 0; }
200864 break;
200865 case 23: /* neardist_opt ::= COMMA STRING */
200866{ fts5yymsp[-1].minor.fts5yy0 = fts5yymsp[0].minor.fts5yy0; }
200867 break;
200868 case 24: /* phrase ::= phrase PLUS STRING star_opt */
200869{
200870 fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, fts5yymsp[-3].minor.fts5yy53, &fts5yymsp[-1].minor.fts5yy0, fts5yymsp[0].minor.fts5yy4);
200871}
200872 fts5yymsp[-3].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
200873 break;
200874 case 25: /* phrase ::= STRING star_opt */
200875{
200876 fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, 0, &fts5yymsp[-1].minor.fts5yy0, fts5yymsp[0].minor.fts5yy4);
200877}
200878 fts5yymsp[-1].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
200879 break;
200880 case 26: /* star_opt ::= STAR */
200881{ fts5yymsp[0].minor.fts5yy4 = 1; }
200882 break;
200883 case 27: /* star_opt ::= */
200884{ fts5yymsp[1].minor.fts5yy4 = 0; }
200885 break;
200886 default:
200887 break;
200888/********** End reduce actions ************************************************/
200889 };
200890 assert( fts5yyruleno<sizeof(fts5yyRuleInfo)/sizeof(fts5yyRuleInfo[0]) );
200891 fts5yygoto = fts5yyRuleInfo[fts5yyruleno].lhs;
200892 fts5yysize = fts5yyRuleInfo[fts5yyruleno].nrhs;
200893 fts5yyact = fts5yy_find_reduce_action(fts5yymsp[fts5yysize].stateno,(fts5YYCODETYPE)fts5yygoto);
200894
200895 /* There are no SHIFTREDUCE actions on nonterminals because the table
200896 ** generator has simplified them to pure REDUCE actions. */
200897 assert( !(fts5yyact>fts5YY_MAX_SHIFT && fts5yyact<=fts5YY_MAX_SHIFTREDUCE) );
200898
200899 /* It is not possible for a REDUCE to be followed by an error */
200900 assert( fts5yyact!=fts5YY_ERROR_ACTION );
200901
200902 fts5yymsp += fts5yysize+1;
200903 fts5yypParser->fts5yytos = fts5yymsp;
200904 fts5yymsp->stateno = (fts5YYACTIONTYPE)fts5yyact;
200905 fts5yymsp->major = (fts5YYCODETYPE)fts5yygoto;
200906 fts5yyTraceShift(fts5yypParser, fts5yyact, "... then shift");
200907 return fts5yyact;
200908}
200909
200910/*
200911** The following code executes when the parse fails
200912*/
200913#ifndef fts5YYNOERRORRECOVERY
200914static void fts5yy_parse_failed(
200915 fts5yyParser *fts5yypParser /* The parser */
200916){
200917 sqlite3Fts5ParserARG_FETCH
200918 sqlite3Fts5ParserCTX_FETCH
200919#ifndef NDEBUG
200920 if( fts5yyTraceFILE ){
200921 fprintf(fts5yyTraceFILE,"%sFail!\n",fts5yyTracePrompt);
200922 }
200923#endif
200924 while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(fts5yypParser);
200925 /* Here code is inserted which will be executed whenever the
200926 ** parser fails */
200927/************ Begin %parse_failure code ***************************************/
200928/************ End %parse_failure code *****************************************/
200929 sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
200930 sqlite3Fts5ParserCTX_STORE
200931}
200932#endif /* fts5YYNOERRORRECOVERY */
200933
200934/*
200935** The following code executes when a syntax error first occurs.
200936*/
200937static void fts5yy_syntax_error(
200938 fts5yyParser *fts5yypParser, /* The parser */
200939 int fts5yymajor, /* The major type of the error token */
200940 sqlite3Fts5ParserFTS5TOKENTYPE fts5yyminor /* The minor type of the error token */
200941){
200942 sqlite3Fts5ParserARG_FETCH
200943 sqlite3Fts5ParserCTX_FETCH
200944#define FTS5TOKEN fts5yyminor
200945/************ Begin %syntax_error code ****************************************/
200946
200947 UNUSED_PARAM(fts5yymajor); /* Silence a compiler warning */
200948 sqlite3Fts5ParseError(
200949 pParse, "fts5: syntax error near \"%.*s\"",FTS5TOKEN.n,FTS5TOKEN.p
200950 );
200951/************ End %syntax_error code ******************************************/
200952 sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
200953 sqlite3Fts5ParserCTX_STORE
200954}
200955
200956/*
200957** The following is executed when the parser accepts
200958*/
200959static void fts5yy_accept(
200960 fts5yyParser *fts5yypParser /* The parser */
200961){
200962 sqlite3Fts5ParserARG_FETCH
200963 sqlite3Fts5ParserCTX_FETCH
200964#ifndef NDEBUG
200965 if( fts5yyTraceFILE ){
200966 fprintf(fts5yyTraceFILE,"%sAccept!\n",fts5yyTracePrompt);
200967 }
200968#endif
200969#ifndef fts5YYNOERRORRECOVERY
200970 fts5yypParser->fts5yyerrcnt = -1;
200971#endif
200972 assert( fts5yypParser->fts5yytos==fts5yypParser->fts5yystack );
200973 /* Here code is inserted which will be executed whenever the
200974 ** parser accepts */
200975/*********** Begin %parse_accept code *****************************************/
200976/*********** End %parse_accept code *******************************************/
200977 sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
200978 sqlite3Fts5ParserCTX_STORE
200979}
200980
200981/* The main parser program.
200982** The first argument is a pointer to a structure obtained from
200983** "sqlite3Fts5ParserAlloc" which describes the current state of the parser.
200984** The second argument is the major token number. The third is
200985** the minor token. The fourth optional argument is whatever the
200986** user wants (and specified in the grammar) and is available for
200987** use by the action routines.
200988**
200989** Inputs:
200990** <ul>
200991** <li> A pointer to the parser (an opaque structure.)
200992** <li> The major token number.
200993** <li> The minor token number.
200994** <li> An option argument of a grammar-specified type.
200995** </ul>
200996**
200997** Outputs:
200998** None.
200999*/
201000static void sqlite3Fts5Parser(
201001 void *fts5yyp, /* The parser */
201002 int fts5yymajor, /* The major token code number */
201003 sqlite3Fts5ParserFTS5TOKENTYPE fts5yyminor /* The value for the token */
201004 sqlite3Fts5ParserARG_PDECL /* Optional %extra_argument parameter */
201005){
201006 fts5YYMINORTYPE fts5yyminorunion;
201007 fts5YYACTIONTYPE fts5yyact; /* The parser action. */
201008#if !defined(fts5YYERRORSYMBOL) && !defined(fts5YYNOERRORRECOVERY)
201009 int fts5yyendofinput; /* True if we are at the end of input */
201010#endif
201011#ifdef fts5YYERRORSYMBOL
201012 int fts5yyerrorhit = 0; /* True if fts5yymajor has invoked an error */
201013#endif
201014 fts5yyParser *fts5yypParser = (fts5yyParser*)fts5yyp; /* The parser */
201015 sqlite3Fts5ParserCTX_FETCH
201016 sqlite3Fts5ParserARG_STORE
201017
201018 assert( fts5yypParser->fts5yytos!=0 );
201019#if !defined(fts5YYERRORSYMBOL) && !defined(fts5YYNOERRORRECOVERY)
201020 fts5yyendofinput = (fts5yymajor==0);
201021#endif
201022
201023 fts5yyact = fts5yypParser->fts5yytos->stateno;
201024#ifndef NDEBUG
201025 if( fts5yyTraceFILE ){
201026 if( fts5yyact < fts5YY_MIN_REDUCE ){
201027 fprintf(fts5yyTraceFILE,"%sInput '%s' in state %d\n",
201028 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor],fts5yyact);
201029 }else{
201030 fprintf(fts5yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
201031 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor],fts5yyact-fts5YY_MIN_REDUCE);
201032 }
201033 }
201034#endif
201035
201036 do{
201037 assert( fts5yyact==fts5yypParser->fts5yytos->stateno );
201038 fts5yyact = fts5yy_find_shift_action((fts5YYCODETYPE)fts5yymajor,fts5yyact);
201039 if( fts5yyact >= fts5YY_MIN_REDUCE ){
201040 fts5yyact = fts5yy_reduce(fts5yypParser,fts5yyact-fts5YY_MIN_REDUCE,fts5yymajor,
201041 fts5yyminor sqlite3Fts5ParserCTX_PARAM);
201042 }else if( fts5yyact <= fts5YY_MAX_SHIFTREDUCE ){
201043 fts5yy_shift(fts5yypParser,fts5yyact,(fts5YYCODETYPE)fts5yymajor,fts5yyminor);
201044#ifndef fts5YYNOERRORRECOVERY
201045 fts5yypParser->fts5yyerrcnt--;
201046#endif
201047 break;
201048 }else if( fts5yyact==fts5YY_ACCEPT_ACTION ){
201049 fts5yypParser->fts5yytos--;
201050 fts5yy_accept(fts5yypParser);
201051 return;
201052 }else{
201053 assert( fts5yyact == fts5YY_ERROR_ACTION );
201054 fts5yyminorunion.fts5yy0 = fts5yyminor;
201055#ifdef fts5YYERRORSYMBOL
201056 int fts5yymx;
201057#endif
201058#ifndef NDEBUG
201059 if( fts5yyTraceFILE ){
201060 fprintf(fts5yyTraceFILE,"%sSyntax Error!\n",fts5yyTracePrompt);
201061 }
201062#endif
201063#ifdef fts5YYERRORSYMBOL
201064 /* A syntax error has occurred.
201065 ** The response to an error depends upon whether or not the
201066 ** grammar defines an error token "ERROR".
201067 **
201068 ** This is what we do if the grammar does define ERROR:
201069 **
201070 ** * Call the %syntax_error function.
201071 **
201072 ** * Begin popping the stack until we enter a state where
201073 ** it is legal to shift the error symbol, then shift
201074 ** the error symbol.
201075 **
201076 ** * Set the error count to three.
201077 **
201078 ** * Begin accepting and shifting new tokens. No new error
201079 ** processing will occur until three tokens have been
201080 ** shifted successfully.
201081 **
201082 */
201083 if( fts5yypParser->fts5yyerrcnt<0 ){
201084 fts5yy_syntax_error(fts5yypParser,fts5yymajor,fts5yyminor);
201085 }
201086 fts5yymx = fts5yypParser->fts5yytos->major;
201087 if( fts5yymx==fts5YYERRORSYMBOL || fts5yyerrorhit ){
201088#ifndef NDEBUG
201089 if( fts5yyTraceFILE ){
201090 fprintf(fts5yyTraceFILE,"%sDiscard input token %s\n",
201091 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor]);
201092 }
201093#endif
201094 fts5yy_destructor(fts5yypParser, (fts5YYCODETYPE)fts5yymajor, &fts5yyminorunion);
201095 fts5yymajor = fts5YYNOCODE;
201096 }else{
201097 while( fts5yypParser->fts5yytos >= fts5yypParser->fts5yystack
201098 && (fts5yyact = fts5yy_find_reduce_action(
201099 fts5yypParser->fts5yytos->stateno,
201100 fts5YYERRORSYMBOL)) > fts5YY_MAX_SHIFTREDUCE
201101 ){
201102 fts5yy_pop_parser_stack(fts5yypParser);
201103 }
201104 if( fts5yypParser->fts5yytos < fts5yypParser->fts5yystack || fts5yymajor==0 ){
201105 fts5yy_destructor(fts5yypParser,(fts5YYCODETYPE)fts5yymajor,&fts5yyminorunion);
201106 fts5yy_parse_failed(fts5yypParser);
201107#ifndef fts5YYNOERRORRECOVERY
201108 fts5yypParser->fts5yyerrcnt = -1;
201109#endif
201110 fts5yymajor = fts5YYNOCODE;
201111 }else if( fts5yymx!=fts5YYERRORSYMBOL ){
201112 fts5yy_shift(fts5yypParser,fts5yyact,fts5YYERRORSYMBOL,fts5yyminor);
201113 }
201114 }
201115 fts5yypParser->fts5yyerrcnt = 3;
201116 fts5yyerrorhit = 1;
201117 if( fts5yymajor==fts5YYNOCODE ) break;
201118 fts5yyact = fts5yypParser->fts5yytos->stateno;
201119#elif defined(fts5YYNOERRORRECOVERY)
201120 /* If the fts5YYNOERRORRECOVERY macro is defined, then do not attempt to
201121 ** do any kind of error recovery. Instead, simply invoke the syntax
201122 ** error routine and continue going as if nothing had happened.
201123 **
201124 ** Applications can set this macro (for example inside %include) if
201125 ** they intend to abandon the parse upon the first syntax error seen.
201126 */
201127 fts5yy_syntax_error(fts5yypParser,fts5yymajor, fts5yyminor);
201128 fts5yy_destructor(fts5yypParser,(fts5YYCODETYPE)fts5yymajor,&fts5yyminorunion);
201129 break;
201130#else /* fts5YYERRORSYMBOL is not defined */
201131 /* This is what we do if the grammar does not define ERROR:
201132 **
201133 ** * Report an error message, and throw away the input token.
201134 **
201135 ** * If the input token is $, then fail the parse.
201136 **
201137 ** As before, subsequent error messages are suppressed until
201138 ** three input tokens have been successfully shifted.
201139 */
201140 if( fts5yypParser->fts5yyerrcnt<=0 ){
201141 fts5yy_syntax_error(fts5yypParser,fts5yymajor, fts5yyminor);
201142 }
201143 fts5yypParser->fts5yyerrcnt = 3;
201144 fts5yy_destructor(fts5yypParser,(fts5YYCODETYPE)fts5yymajor,&fts5yyminorunion);
201145 if( fts5yyendofinput ){
201146 fts5yy_parse_failed(fts5yypParser);
201147#ifndef fts5YYNOERRORRECOVERY
201148 fts5yypParser->fts5yyerrcnt = -1;
201149#endif
201150 }
201151 break;
201152#endif
201153 }
201154 }while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack );
201155#ifndef NDEBUG
201156 if( fts5yyTraceFILE ){
201157 fts5yyStackEntry *i;
201158 char cDiv = '[';
201159 fprintf(fts5yyTraceFILE,"%sReturn. Stack=",fts5yyTracePrompt);
201160 for(i=&fts5yypParser->fts5yystack[1]; i<=fts5yypParser->fts5yytos; i++){
201161 fprintf(fts5yyTraceFILE,"%c%s", cDiv, fts5yyTokenName[i->major]);
201162 cDiv = ' ';
201163 }
201164 fprintf(fts5yyTraceFILE,"]\n");
201165 }
201166#endif
201167 return;
201168}
201169
201170/*
201171** Return the fallback token corresponding to canonical token iToken, or
201172** 0 if iToken has no fallback.
201173*/
201174static int sqlite3Fts5ParserFallback(int iToken){
201175#ifdef fts5YYFALLBACK
201176 if( iToken<(int)(sizeof(fts5yyFallback)/sizeof(fts5yyFallback[0])) ){
201177 return fts5yyFallback[iToken];
201178 }
201179#else
201180 (void)iToken;
201181#endif
201182 return 0;
201183}
201184
201185/*
201186** 2014 May 31
201187**
201188** The author disclaims copyright to this source code. In place of
201189** a legal notice, here is a blessing:
201190**
201191** May you do good and not evil.
201192** May you find forgiveness for yourself and forgive others.
201193** May you share freely, never taking more than you give.
201194**
201195******************************************************************************
201196*/
201197
201198
201199/* #include "fts5Int.h" */
201200#include <math.h> /* amalgamator: keep */
201201
201202/*
201203** Object used to iterate through all "coalesced phrase instances" in
201204** a single column of the current row. If the phrase instances in the
201205** column being considered do not overlap, this object simply iterates
201206** through them. Or, if they do overlap (share one or more tokens in
201207** common), each set of overlapping instances is treated as a single
201208** match. See documentation for the highlight() auxiliary function for
201209** details.
201210**
201211** Usage is:
201212**
201213** for(rc = fts5CInstIterNext(pApi, pFts, iCol, &iter);
201214** (rc==SQLITE_OK && 0==fts5CInstIterEof(&iter);
201215** rc = fts5CInstIterNext(&iter)
201216** ){
201217** printf("instance starts at %d, ends at %d\n", iter.iStart, iter.iEnd);
201218** }
201219**
201220*/
201221typedef struct CInstIter CInstIter;
201222struct CInstIter {
201223 const Fts5ExtensionApi *pApi; /* API offered by current FTS version */
201224 Fts5Context *pFts; /* First arg to pass to pApi functions */
201225 int iCol; /* Column to search */
201226 int iInst; /* Next phrase instance index */
201227 int nInst; /* Total number of phrase instances */
201228
201229 /* Output variables */
201230 int iStart; /* First token in coalesced phrase instance */
201231 int iEnd; /* Last token in coalesced phrase instance */
201232};
201233
201234/*
201235** Advance the iterator to the next coalesced phrase instance. Return
201236** an SQLite error code if an error occurs, or SQLITE_OK otherwise.
201237*/
201238static int fts5CInstIterNext(CInstIter *pIter){
201239 int rc = SQLITE_OK;
201240 pIter->iStart = -1;
201241 pIter->iEnd = -1;
201242
201243 while( rc==SQLITE_OK && pIter->iInst<pIter->nInst ){
201244 int ip; int ic; int io;
201245 rc = pIter->pApi->xInst(pIter->pFts, pIter->iInst, &ip, &ic, &io);
201246 if( rc==SQLITE_OK ){
201247 if( ic==pIter->iCol ){
201248 int iEnd = io - 1 + pIter->pApi->xPhraseSize(pIter->pFts, ip);
201249 if( pIter->iStart<0 ){
201250 pIter->iStart = io;
201251 pIter->iEnd = iEnd;
201252 }else if( io<=pIter->iEnd ){
201253 if( iEnd>pIter->iEnd ) pIter->iEnd = iEnd;
201254 }else{
201255 break;
201256 }
201257 }
201258 pIter->iInst++;
201259 }
201260 }
201261
201262 return rc;
201263}
201264
201265/*
201266** Initialize the iterator object indicated by the final parameter to
201267** iterate through coalesced phrase instances in column iCol.
201268*/
201269static int fts5CInstIterInit(
201270 const Fts5ExtensionApi *pApi,
201271 Fts5Context *pFts,
201272 int iCol,
201273 CInstIter *pIter
201274){
201275 int rc;
201276
201277 memset(pIter, 0, sizeof(CInstIter));
201278 pIter->pApi = pApi;
201279 pIter->pFts = pFts;
201280 pIter->iCol = iCol;
201281 rc = pApi->xInstCount(pFts, &pIter->nInst);
201282
201283 if( rc==SQLITE_OK ){
201284 rc = fts5CInstIterNext(pIter);
201285 }
201286
201287 return rc;
201288}
201289
201290
201291
201292/*************************************************************************
201293** Start of highlight() implementation.
201294*/
201295typedef struct HighlightContext HighlightContext;
201296struct HighlightContext {
201297 CInstIter iter; /* Coalesced Instance Iterator */
201298 int iPos; /* Current token offset in zIn[] */
201299 int iRangeStart; /* First token to include */
201300 int iRangeEnd; /* If non-zero, last token to include */
201301 const char *zOpen; /* Opening highlight */
201302 const char *zClose; /* Closing highlight */
201303 const char *zIn; /* Input text */
201304 int nIn; /* Size of input text in bytes */
201305 int iOff; /* Current offset within zIn[] */
201306 char *zOut; /* Output value */
201307};
201308
201309/*
201310** Append text to the HighlightContext output string - p->zOut. Argument
201311** z points to a buffer containing n bytes of text to append. If n is
201312** negative, everything up until the first '\0' is appended to the output.
201313**
201314** If *pRc is set to any value other than SQLITE_OK when this function is
201315** called, it is a no-op. If an error (i.e. an OOM condition) is encountered,
201316** *pRc is set to an error code before returning.
201317*/
201318static void fts5HighlightAppend(
201319 int *pRc,
201320 HighlightContext *p,
201321 const char *z, int n
201322){
201323 if( *pRc==SQLITE_OK ){
201324 if( n<0 ) n = (int)strlen(z);
201325 p->zOut = sqlite3_mprintf("%z%.*s", p->zOut, n, z);
201326 if( p->zOut==0 ) *pRc = SQLITE_NOMEM;
201327 }
201328}
201329
201330/*
201331** Tokenizer callback used by implementation of highlight() function.
201332*/
201333static int fts5HighlightCb(
201334 void *pContext, /* Pointer to HighlightContext object */
201335 int tflags, /* Mask of FTS5_TOKEN_* flags */
201336 const char *pToken, /* Buffer containing token */
201337 int nToken, /* Size of token in bytes */
201338 int iStartOff, /* Start offset of token */
201339 int iEndOff /* End offset of token */
201340){
201341 HighlightContext *p = (HighlightContext*)pContext;
201342 int rc = SQLITE_OK;
201343 int iPos;
201344
201345 UNUSED_PARAM2(pToken, nToken);
201346
201347 if( tflags & FTS5_TOKEN_COLOCATED ) return SQLITE_OK;
201348 iPos = p->iPos++;
201349
201350 if( p->iRangeEnd>0 ){
201351 if( iPos<p->iRangeStart || iPos>p->iRangeEnd ) return SQLITE_OK;
201352 if( p->iRangeStart && iPos==p->iRangeStart ) p->iOff = iStartOff;
201353 }
201354
201355 if( iPos==p->iter.iStart ){
201356 fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iStartOff - p->iOff);
201357 fts5HighlightAppend(&rc, p, p->zOpen, -1);
201358 p->iOff = iStartOff;
201359 }
201360
201361 if( iPos==p->iter.iEnd ){
201362 if( p->iRangeEnd && p->iter.iStart<p->iRangeStart ){
201363 fts5HighlightAppend(&rc, p, p->zOpen, -1);
201364 }
201365 fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff);
201366 fts5HighlightAppend(&rc, p, p->zClose, -1);
201367 p->iOff = iEndOff;
201368 if( rc==SQLITE_OK ){
201369 rc = fts5CInstIterNext(&p->iter);
201370 }
201371 }
201372
201373 if( p->iRangeEnd>0 && iPos==p->iRangeEnd ){
201374 fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff);
201375 p->iOff = iEndOff;
201376 if( iPos>=p->iter.iStart && iPos<p->iter.iEnd ){
201377 fts5HighlightAppend(&rc, p, p->zClose, -1);
201378 }
201379 }
201380
201381 return rc;
201382}
201383
201384/*
201385** Implementation of highlight() function.
201386*/
201387static void fts5HighlightFunction(
201388 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
201389 Fts5Context *pFts, /* First arg to pass to pApi functions */
201390 sqlite3_context *pCtx, /* Context for returning result/error */
201391 int nVal, /* Number of values in apVal[] array */
201392 sqlite3_value **apVal /* Array of trailing arguments */
201393){
201394 HighlightContext ctx;
201395 int rc;
201396 int iCol;
201397
201398 if( nVal!=3 ){
201399 const char *zErr = "wrong number of arguments to function highlight()";
201400 sqlite3_result_error(pCtx, zErr, -1);
201401 return;
201402 }
201403
201404 iCol = sqlite3_value_int(apVal[0]);
201405 memset(&ctx, 0, sizeof(HighlightContext));
201406 ctx.zOpen = (const char*)sqlite3_value_text(apVal[1]);
201407 ctx.zClose = (const char*)sqlite3_value_text(apVal[2]);
201408 rc = pApi->xColumnText(pFts, iCol, &ctx.zIn, &ctx.nIn);
201409
201410 if( ctx.zIn ){
201411 if( rc==SQLITE_OK ){
201412 rc = fts5CInstIterInit(pApi, pFts, iCol, &ctx.iter);
201413 }
201414
201415 if( rc==SQLITE_OK ){
201416 rc = pApi->xTokenize(pFts, ctx.zIn, ctx.nIn, (void*)&ctx,fts5HighlightCb);
201417 }
201418 fts5HighlightAppend(&rc, &ctx, &ctx.zIn[ctx.iOff], ctx.nIn - ctx.iOff);
201419
201420 if( rc==SQLITE_OK ){
201421 sqlite3_result_text(pCtx, (const char*)ctx.zOut, -1, SQLITE_TRANSIENT);
201422 }
201423 sqlite3_free(ctx.zOut);
201424 }
201425 if( rc!=SQLITE_OK ){
201426 sqlite3_result_error_code(pCtx, rc);
201427 }
201428}
201429/*
201430** End of highlight() implementation.
201431**************************************************************************/
201432
201433/*
201434** Context object passed to the fts5SentenceFinderCb() function.
201435*/
201436typedef struct Fts5SFinder Fts5SFinder;
201437struct Fts5SFinder {
201438 int iPos; /* Current token position */
201439 int nFirstAlloc; /* Allocated size of aFirst[] */
201440 int nFirst; /* Number of entries in aFirst[] */
201441 int *aFirst; /* Array of first token in each sentence */
201442 const char *zDoc; /* Document being tokenized */
201443};
201444
201445/*
201446** Add an entry to the Fts5SFinder.aFirst[] array. Grow the array if
201447** necessary. Return SQLITE_OK if successful, or SQLITE_NOMEM if an
201448** error occurs.
201449*/
201450static int fts5SentenceFinderAdd(Fts5SFinder *p, int iAdd){
201451 if( p->nFirstAlloc==p->nFirst ){
201452 int nNew = p->nFirstAlloc ? p->nFirstAlloc*2 : 64;
201453 int *aNew;
201454
201455 aNew = (int*)sqlite3_realloc(p->aFirst, nNew*sizeof(int));
201456 if( aNew==0 ) return SQLITE_NOMEM;
201457 p->aFirst = aNew;
201458 p->nFirstAlloc = nNew;
201459 }
201460 p->aFirst[p->nFirst++] = iAdd;
201461 return SQLITE_OK;
201462}
201463
201464/*
201465** This function is an xTokenize() callback used by the auxiliary snippet()
201466** function. Its job is to identify tokens that are the first in a sentence.
201467** For each such token, an entry is added to the SFinder.aFirst[] array.
201468*/
201469static int fts5SentenceFinderCb(
201470 void *pContext, /* Pointer to HighlightContext object */
201471 int tflags, /* Mask of FTS5_TOKEN_* flags */
201472 const char *pToken, /* Buffer containing token */
201473 int nToken, /* Size of token in bytes */
201474 int iStartOff, /* Start offset of token */
201475 int iEndOff /* End offset of token */
201476){
201477 int rc = SQLITE_OK;
201478
201479 UNUSED_PARAM2(pToken, nToken);
201480 UNUSED_PARAM(iEndOff);
201481
201482 if( (tflags & FTS5_TOKEN_COLOCATED)==0 ){
201483 Fts5SFinder *p = (Fts5SFinder*)pContext;
201484 if( p->iPos>0 ){
201485 int i;
201486 char c = 0;
201487 for(i=iStartOff-1; i>=0; i--){
201488 c = p->zDoc[i];
201489 if( c!=' ' && c!='\t' && c!='\n' && c!='\r' ) break;
201490 }
201491 if( i!=iStartOff-1 && (c=='.' || c==':') ){
201492 rc = fts5SentenceFinderAdd(p, p->iPos);
201493 }
201494 }else{
201495 rc = fts5SentenceFinderAdd(p, 0);
201496 }
201497 p->iPos++;
201498 }
201499 return rc;
201500}
201501
201502static int fts5SnippetScore(
201503 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
201504 Fts5Context *pFts, /* First arg to pass to pApi functions */
201505 int nDocsize, /* Size of column in tokens */
201506 unsigned char *aSeen, /* Array with one element per query phrase */
201507 int iCol, /* Column to score */
201508 int iPos, /* Starting offset to score */
201509 int nToken, /* Max tokens per snippet */
201510 int *pnScore, /* OUT: Score */
201511 int *piPos /* OUT: Adjusted offset */
201512){
201513 int rc;
201514 int i;
201515 int ip = 0;
201516 int ic = 0;
201517 int iOff = 0;
201518 int iFirst = -1;
201519 int nInst;
201520 int nScore = 0;
201521 int iLast = 0;
201522
201523 rc = pApi->xInstCount(pFts, &nInst);
201524 for(i=0; i<nInst && rc==SQLITE_OK; i++){
201525 rc = pApi->xInst(pFts, i, &ip, &ic, &iOff);
201526 if( rc==SQLITE_OK && ic==iCol && iOff>=iPos && iOff<(iPos+nToken) ){
201527 nScore += (aSeen[ip] ? 1 : 1000);
201528 aSeen[ip] = 1;
201529 if( iFirst<0 ) iFirst = iOff;
201530 iLast = iOff + pApi->xPhraseSize(pFts, ip);
201531 }
201532 }
201533
201534 *pnScore = nScore;
201535 if( piPos ){
201536 int iAdj = iFirst - (nToken - (iLast-iFirst)) / 2;
201537 if( (iAdj+nToken)>nDocsize ) iAdj = nDocsize - nToken;
201538 if( iAdj<0 ) iAdj = 0;
201539 *piPos = iAdj;
201540 }
201541
201542 return rc;
201543}
201544
201545/*
201546** Return the value in pVal interpreted as utf-8 text. Except, if pVal
201547** contains a NULL value, return a pointer to a static string zero
201548** bytes in length instead of a NULL pointer.
201549*/
201550static const char *fts5ValueToText(sqlite3_value *pVal){
201551 const char *zRet = (const char*)sqlite3_value_text(pVal);
201552 return zRet ? zRet : "";
201553}
201554
201555/*
201556** Implementation of snippet() function.
201557*/
201558static void fts5SnippetFunction(
201559 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
201560 Fts5Context *pFts, /* First arg to pass to pApi functions */
201561 sqlite3_context *pCtx, /* Context for returning result/error */
201562 int nVal, /* Number of values in apVal[] array */
201563 sqlite3_value **apVal /* Array of trailing arguments */
201564){
201565 HighlightContext ctx;
201566 int rc = SQLITE_OK; /* Return code */
201567 int iCol; /* 1st argument to snippet() */
201568 const char *zEllips; /* 4th argument to snippet() */
201569 int nToken; /* 5th argument to snippet() */
201570 int nInst = 0; /* Number of instance matches this row */
201571 int i; /* Used to iterate through instances */
201572 int nPhrase; /* Number of phrases in query */
201573 unsigned char *aSeen; /* Array of "seen instance" flags */
201574 int iBestCol; /* Column containing best snippet */
201575 int iBestStart = 0; /* First token of best snippet */
201576 int nBestScore = 0; /* Score of best snippet */
201577 int nColSize = 0; /* Total size of iBestCol in tokens */
201578 Fts5SFinder sFinder; /* Used to find the beginnings of sentences */
201579 int nCol;
201580
201581 if( nVal!=5 ){
201582 const char *zErr = "wrong number of arguments to function snippet()";
201583 sqlite3_result_error(pCtx, zErr, -1);
201584 return;
201585 }
201586
201587 nCol = pApi->xColumnCount(pFts);
201588 memset(&ctx, 0, sizeof(HighlightContext));
201589 iCol = sqlite3_value_int(apVal[0]);
201590 ctx.zOpen = fts5ValueToText(apVal[1]);
201591 ctx.zClose = fts5ValueToText(apVal[2]);
201592 zEllips = fts5ValueToText(apVal[3]);
201593 nToken = sqlite3_value_int(apVal[4]);
201594
201595 iBestCol = (iCol>=0 ? iCol : 0);
201596 nPhrase = pApi->xPhraseCount(pFts);
201597 aSeen = sqlite3_malloc(nPhrase);
201598 if( aSeen==0 ){
201599 rc = SQLITE_NOMEM;
201600 }
201601 if( rc==SQLITE_OK ){
201602 rc = pApi->xInstCount(pFts, &nInst);
201603 }
201604
201605 memset(&sFinder, 0, sizeof(Fts5SFinder));
201606 for(i=0; i<nCol; i++){
201607 if( iCol<0 || iCol==i ){
201608 int nDoc;
201609 int nDocsize;
201610 int ii;
201611 sFinder.iPos = 0;
201612 sFinder.nFirst = 0;
201613 rc = pApi->xColumnText(pFts, i, &sFinder.zDoc, &nDoc);
201614 if( rc!=SQLITE_OK ) break;
201615 rc = pApi->xTokenize(pFts,
201616 sFinder.zDoc, nDoc, (void*)&sFinder,fts5SentenceFinderCb
201617 );
201618 if( rc!=SQLITE_OK ) break;
201619 rc = pApi->xColumnSize(pFts, i, &nDocsize);
201620 if( rc!=SQLITE_OK ) break;
201621
201622 for(ii=0; rc==SQLITE_OK && ii<nInst; ii++){
201623 int ip, ic, io;
201624 int iAdj;
201625 int nScore;
201626 int jj;
201627
201628 rc = pApi->xInst(pFts, ii, &ip, &ic, &io);
201629 if( ic!=i || rc!=SQLITE_OK ) continue;
201630 memset(aSeen, 0, nPhrase);
201631 rc = fts5SnippetScore(pApi, pFts, nDocsize, aSeen, i,
201632 io, nToken, &nScore, &iAdj
201633 );
201634 if( rc==SQLITE_OK && nScore>nBestScore ){
201635 nBestScore = nScore;
201636 iBestCol = i;
201637 iBestStart = iAdj;
201638 nColSize = nDocsize;
201639 }
201640
201641 if( rc==SQLITE_OK && sFinder.nFirst && nDocsize>nToken ){
201642 for(jj=0; jj<(sFinder.nFirst-1); jj++){
201643 if( sFinder.aFirst[jj+1]>io ) break;
201644 }
201645
201646 if( sFinder.aFirst[jj]<io ){
201647 memset(aSeen, 0, nPhrase);
201648 rc = fts5SnippetScore(pApi, pFts, nDocsize, aSeen, i,
201649 sFinder.aFirst[jj], nToken, &nScore, 0
201650 );
201651
201652 nScore += (sFinder.aFirst[jj]==0 ? 120 : 100);
201653 if( rc==SQLITE_OK && nScore>nBestScore ){
201654 nBestScore = nScore;
201655 iBestCol = i;
201656 iBestStart = sFinder.aFirst[jj];
201657 nColSize = nDocsize;
201658 }
201659 }
201660 }
201661 }
201662 }
201663 }
201664
201665 if( rc==SQLITE_OK ){
201666 rc = pApi->xColumnText(pFts, iBestCol, &ctx.zIn, &ctx.nIn);
201667 }
201668 if( rc==SQLITE_OK && nColSize==0 ){
201669 rc = pApi->xColumnSize(pFts, iBestCol, &nColSize);
201670 }
201671 if( ctx.zIn ){
201672 if( rc==SQLITE_OK ){
201673 rc = fts5CInstIterInit(pApi, pFts, iBestCol, &ctx.iter);
201674 }
201675
201676 ctx.iRangeStart = iBestStart;
201677 ctx.iRangeEnd = iBestStart + nToken - 1;
201678
201679 if( iBestStart>0 ){
201680 fts5HighlightAppend(&rc, &ctx, zEllips, -1);
201681 }
201682
201683 /* Advance iterator ctx.iter so that it points to the first coalesced
201684 ** phrase instance at or following position iBestStart. */
201685 while( ctx.iter.iStart>=0 && ctx.iter.iStart<iBestStart && rc==SQLITE_OK ){
201686 rc = fts5CInstIterNext(&ctx.iter);
201687 }
201688
201689 if( rc==SQLITE_OK ){
201690 rc = pApi->xTokenize(pFts, ctx.zIn, ctx.nIn, (void*)&ctx,fts5HighlightCb);
201691 }
201692 if( ctx.iRangeEnd>=(nColSize-1) ){
201693 fts5HighlightAppend(&rc, &ctx, &ctx.zIn[ctx.iOff], ctx.nIn - ctx.iOff);
201694 }else{
201695 fts5HighlightAppend(&rc, &ctx, zEllips, -1);
201696 }
201697 }
201698 if( rc==SQLITE_OK ){
201699 sqlite3_result_text(pCtx, (const char*)ctx.zOut, -1, SQLITE_TRANSIENT);
201700 }else{
201701 sqlite3_result_error_code(pCtx, rc);
201702 }
201703 sqlite3_free(ctx.zOut);
201704 sqlite3_free(aSeen);
201705 sqlite3_free(sFinder.aFirst);
201706}
201707
201708/************************************************************************/
201709
201710/*
201711** The first time the bm25() function is called for a query, an instance
201712** of the following structure is allocated and populated.
201713*/
201714typedef struct Fts5Bm25Data Fts5Bm25Data;
201715struct Fts5Bm25Data {
201716 int nPhrase; /* Number of phrases in query */
201717 double avgdl; /* Average number of tokens in each row */
201718 double *aIDF; /* IDF for each phrase */
201719 double *aFreq; /* Array used to calculate phrase freq. */
201720};
201721
201722/*
201723** Callback used by fts5Bm25GetData() to count the number of rows in the
201724** table matched by each individual phrase within the query.
201725*/
201726static int fts5CountCb(
201727 const Fts5ExtensionApi *pApi,
201728 Fts5Context *pFts,
201729 void *pUserData /* Pointer to sqlite3_int64 variable */
201730){
201731 sqlite3_int64 *pn = (sqlite3_int64*)pUserData;
201732 UNUSED_PARAM2(pApi, pFts);
201733 (*pn)++;
201734 return SQLITE_OK;
201735}
201736
201737/*
201738** Set *ppData to point to the Fts5Bm25Data object for the current query.
201739** If the object has not already been allocated, allocate and populate it
201740** now.
201741*/
201742static int fts5Bm25GetData(
201743 const Fts5ExtensionApi *pApi,
201744 Fts5Context *pFts,
201745 Fts5Bm25Data **ppData /* OUT: bm25-data object for this query */
201746){
201747 int rc = SQLITE_OK; /* Return code */
201748 Fts5Bm25Data *p; /* Object to return */
201749
201750 p = pApi->xGetAuxdata(pFts, 0);
201751 if( p==0 ){
201752 int nPhrase; /* Number of phrases in query */
201753 sqlite3_int64 nRow = 0; /* Number of rows in table */
201754 sqlite3_int64 nToken = 0; /* Number of tokens in table */
201755 int nByte; /* Bytes of space to allocate */
201756 int i;
201757
201758 /* Allocate the Fts5Bm25Data object */
201759 nPhrase = pApi->xPhraseCount(pFts);
201760 nByte = sizeof(Fts5Bm25Data) + nPhrase*2*sizeof(double);
201761 p = (Fts5Bm25Data*)sqlite3_malloc(nByte);
201762 if( p==0 ){
201763 rc = SQLITE_NOMEM;
201764 }else{
201765 memset(p, 0, nByte);
201766 p->nPhrase = nPhrase;
201767 p->aIDF = (double*)&p[1];
201768 p->aFreq = &p->aIDF[nPhrase];
201769 }
201770
201771 /* Calculate the average document length for this FTS5 table */
201772 if( rc==SQLITE_OK ) rc = pApi->xRowCount(pFts, &nRow);
201773 if( rc==SQLITE_OK ) rc = pApi->xColumnTotalSize(pFts, -1, &nToken);
201774 if( rc==SQLITE_OK ) p->avgdl = (double)nToken / (double)nRow;
201775
201776 /* Calculate an IDF for each phrase in the query */
201777 for(i=0; rc==SQLITE_OK && i<nPhrase; i++){
201778 sqlite3_int64 nHit = 0;
201779 rc = pApi->xQueryPhrase(pFts, i, (void*)&nHit, fts5CountCb);
201780 if( rc==SQLITE_OK ){
201781 /* Calculate the IDF (Inverse Document Frequency) for phrase i.
201782 ** This is done using the standard BM25 formula as found on wikipedia:
201783 **
201784 ** IDF = log( (N - nHit + 0.5) / (nHit + 0.5) )
201785 **
201786 ** where "N" is the total number of documents in the set and nHit
201787 ** is the number that contain at least one instance of the phrase
201788 ** under consideration.
201789 **
201790 ** The problem with this is that if (N < 2*nHit), the IDF is
201791 ** negative. Which is undesirable. So the mimimum allowable IDF is
201792 ** (1e-6) - roughly the same as a term that appears in just over
201793 ** half of set of 5,000,000 documents. */
201794 double idf = log( (nRow - nHit + 0.5) / (nHit + 0.5) );
201795 if( idf<=0.0 ) idf = 1e-6;
201796 p->aIDF[i] = idf;
201797 }
201798 }
201799
201800 if( rc!=SQLITE_OK ){
201801 sqlite3_free(p);
201802 }else{
201803 rc = pApi->xSetAuxdata(pFts, p, sqlite3_free);
201804 }
201805 if( rc!=SQLITE_OK ) p = 0;
201806 }
201807 *ppData = p;
201808 return rc;
201809}
201810
201811/*
201812** Implementation of bm25() function.
201813*/
201814static void fts5Bm25Function(
201815 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
201816 Fts5Context *pFts, /* First arg to pass to pApi functions */
201817 sqlite3_context *pCtx, /* Context for returning result/error */
201818 int nVal, /* Number of values in apVal[] array */
201819 sqlite3_value **apVal /* Array of trailing arguments */
201820){
201821 const double k1 = 1.2; /* Constant "k1" from BM25 formula */
201822 const double b = 0.75; /* Constant "b" from BM25 formula */
201823 int rc = SQLITE_OK; /* Error code */
201824 double score = 0.0; /* SQL function return value */
201825 Fts5Bm25Data *pData; /* Values allocated/calculated once only */
201826 int i; /* Iterator variable */
201827 int nInst = 0; /* Value returned by xInstCount() */
201828 double D = 0.0; /* Total number of tokens in row */
201829 double *aFreq = 0; /* Array of phrase freq. for current row */
201830
201831 /* Calculate the phrase frequency (symbol "f(qi,D)" in the documentation)
201832 ** for each phrase in the query for the current row. */
201833 rc = fts5Bm25GetData(pApi, pFts, &pData);
201834 if( rc==SQLITE_OK ){
201835 aFreq = pData->aFreq;
201836 memset(aFreq, 0, sizeof(double) * pData->nPhrase);
201837 rc = pApi->xInstCount(pFts, &nInst);
201838 }
201839 for(i=0; rc==SQLITE_OK && i<nInst; i++){
201840 int ip; int ic; int io;
201841 rc = pApi->xInst(pFts, i, &ip, &ic, &io);
201842 if( rc==SQLITE_OK ){
201843 double w = (nVal > ic) ? sqlite3_value_double(apVal[ic]) : 1.0;
201844 aFreq[ip] += w;
201845 }
201846 }
201847
201848 /* Figure out the total size of the current row in tokens. */
201849 if( rc==SQLITE_OK ){
201850 int nTok;
201851 rc = pApi->xColumnSize(pFts, -1, &nTok);
201852 D = (double)nTok;
201853 }
201854
201855 /* Determine the BM25 score for the current row. */
201856 for(i=0; rc==SQLITE_OK && i<pData->nPhrase; i++){
201857 score += pData->aIDF[i] * (
201858 ( aFreq[i] * (k1 + 1.0) ) /
201859 ( aFreq[i] + k1 * (1 - b + b * D / pData->avgdl) )
201860 );
201861 }
201862
201863 /* If no error has occurred, return the calculated score. Otherwise,
201864 ** throw an SQL exception. */
201865 if( rc==SQLITE_OK ){
201866 sqlite3_result_double(pCtx, -1.0 * score);
201867 }else{
201868 sqlite3_result_error_code(pCtx, rc);
201869 }
201870}
201871
201872static int sqlite3Fts5AuxInit(fts5_api *pApi){
201873 struct Builtin {
201874 const char *zFunc; /* Function name (nul-terminated) */
201875 void *pUserData; /* User-data pointer */
201876 fts5_extension_function xFunc;/* Callback function */
201877 void (*xDestroy)(void*); /* Destructor function */
201878 } aBuiltin [] = {
201879 { "snippet", 0, fts5SnippetFunction, 0 },
201880 { "highlight", 0, fts5HighlightFunction, 0 },
201881 { "bm25", 0, fts5Bm25Function, 0 },
201882 };
201883 int rc = SQLITE_OK; /* Return code */
201884 int i; /* To iterate through builtin functions */
201885
201886 for(i=0; rc==SQLITE_OK && i<ArraySize(aBuiltin); i++){
201887 rc = pApi->xCreateFunction(pApi,
201888 aBuiltin[i].zFunc,
201889 aBuiltin[i].pUserData,
201890 aBuiltin[i].xFunc,
201891 aBuiltin[i].xDestroy
201892 );
201893 }
201894
201895 return rc;
201896}
201897
201898
201899
201900/*
201901** 2014 May 31
201902**
201903** The author disclaims copyright to this source code. In place of
201904** a legal notice, here is a blessing:
201905**
201906** May you do good and not evil.
201907** May you find forgiveness for yourself and forgive others.
201908** May you share freely, never taking more than you give.
201909**
201910******************************************************************************
201911*/
201912
201913
201914
201915/* #include "fts5Int.h" */
201916
201917static int sqlite3Fts5BufferSize(int *pRc, Fts5Buffer *pBuf, u32 nByte){
201918 if( (u32)pBuf->nSpace<nByte ){
201919 u32 nNew = pBuf->nSpace ? pBuf->nSpace : 64;
201920 u8 *pNew;
201921 while( nNew<nByte ){
201922 nNew = nNew * 2;
201923 }
201924 pNew = sqlite3_realloc(pBuf->p, nNew);
201925 if( pNew==0 ){
201926 *pRc = SQLITE_NOMEM;
201927 return 1;
201928 }else{
201929 pBuf->nSpace = nNew;
201930 pBuf->p = pNew;
201931 }
201932 }
201933 return 0;
201934}
201935
201936
201937/*
201938** Encode value iVal as an SQLite varint and append it to the buffer object
201939** pBuf. If an OOM error occurs, set the error code in p.
201940*/
201941static void sqlite3Fts5BufferAppendVarint(int *pRc, Fts5Buffer *pBuf, i64 iVal){
201942 if( fts5BufferGrow(pRc, pBuf, 9) ) return;
201943 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iVal);
201944}
201945
201946static void sqlite3Fts5Put32(u8 *aBuf, int iVal){
201947 aBuf[0] = (iVal>>24) & 0x00FF;
201948 aBuf[1] = (iVal>>16) & 0x00FF;
201949 aBuf[2] = (iVal>> 8) & 0x00FF;
201950 aBuf[3] = (iVal>> 0) & 0x00FF;
201951}
201952
201953static int sqlite3Fts5Get32(const u8 *aBuf){
201954 return (aBuf[0] << 24) + (aBuf[1] << 16) + (aBuf[2] << 8) + aBuf[3];
201955}
201956
201957/*
201958** Append buffer nData/pData to buffer pBuf. If an OOM error occurs, set
201959** the error code in p. If an error has already occurred when this function
201960** is called, it is a no-op.
201961*/
201962static void sqlite3Fts5BufferAppendBlob(
201963 int *pRc,
201964 Fts5Buffer *pBuf,
201965 u32 nData,
201966 const u8 *pData
201967){
201968 assert_nc( *pRc || nData>=0 );
201969 if( nData ){
201970 if( fts5BufferGrow(pRc, pBuf, nData) ) return;
201971 memcpy(&pBuf->p[pBuf->n], pData, nData);
201972 pBuf->n += nData;
201973 }
201974}
201975
201976/*
201977** Append the nul-terminated string zStr to the buffer pBuf. This function
201978** ensures that the byte following the buffer data is set to 0x00, even
201979** though this byte is not included in the pBuf->n count.
201980*/
201981static void sqlite3Fts5BufferAppendString(
201982 int *pRc,
201983 Fts5Buffer *pBuf,
201984 const char *zStr
201985){
201986 int nStr = (int)strlen(zStr);
201987 sqlite3Fts5BufferAppendBlob(pRc, pBuf, nStr+1, (const u8*)zStr);
201988 pBuf->n--;
201989}
201990
201991/*
201992** Argument zFmt is a printf() style format string. This function performs
201993** the printf() style processing, then appends the results to buffer pBuf.
201994**
201995** Like sqlite3Fts5BufferAppendString(), this function ensures that the byte
201996** following the buffer data is set to 0x00, even though this byte is not
201997** included in the pBuf->n count.
201998*/
201999static void sqlite3Fts5BufferAppendPrintf(
202000 int *pRc,
202001 Fts5Buffer *pBuf,
202002 char *zFmt, ...
202003){
202004 if( *pRc==SQLITE_OK ){
202005 char *zTmp;
202006 va_list ap;
202007 va_start(ap, zFmt);
202008 zTmp = sqlite3_vmprintf(zFmt, ap);
202009 va_end(ap);
202010
202011 if( zTmp==0 ){
202012 *pRc = SQLITE_NOMEM;
202013 }else{
202014 sqlite3Fts5BufferAppendString(pRc, pBuf, zTmp);
202015 sqlite3_free(zTmp);
202016 }
202017 }
202018}
202019
202020static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...){
202021 char *zRet = 0;
202022 if( *pRc==SQLITE_OK ){
202023 va_list ap;
202024 va_start(ap, zFmt);
202025 zRet = sqlite3_vmprintf(zFmt, ap);
202026 va_end(ap);
202027 if( zRet==0 ){
202028 *pRc = SQLITE_NOMEM;
202029 }
202030 }
202031 return zRet;
202032}
202033
202034
202035/*
202036** Free any buffer allocated by pBuf. Zero the structure before returning.
202037*/
202038static void sqlite3Fts5BufferFree(Fts5Buffer *pBuf){
202039 sqlite3_free(pBuf->p);
202040 memset(pBuf, 0, sizeof(Fts5Buffer));
202041}
202042
202043/*
202044** Zero the contents of the buffer object. But do not free the associated
202045** memory allocation.
202046*/
202047static void sqlite3Fts5BufferZero(Fts5Buffer *pBuf){
202048 pBuf->n = 0;
202049}
202050
202051/*
202052** Set the buffer to contain nData/pData. If an OOM error occurs, leave an
202053** the error code in p. If an error has already occurred when this function
202054** is called, it is a no-op.
202055*/
202056static void sqlite3Fts5BufferSet(
202057 int *pRc,
202058 Fts5Buffer *pBuf,
202059 int nData,
202060 const u8 *pData
202061){
202062 pBuf->n = 0;
202063 sqlite3Fts5BufferAppendBlob(pRc, pBuf, nData, pData);
202064}
202065
202066static int sqlite3Fts5PoslistNext64(
202067 const u8 *a, int n, /* Buffer containing poslist */
202068 int *pi, /* IN/OUT: Offset within a[] */
202069 i64 *piOff /* IN/OUT: Current offset */
202070){
202071 int i = *pi;
202072 if( i>=n ){
202073 /* EOF */
202074 *piOff = -1;
202075 return 1;
202076 }else{
202077 i64 iOff = *piOff;
202078 int iVal;
202079 fts5FastGetVarint32(a, i, iVal);
202080 if( iVal==1 ){
202081 fts5FastGetVarint32(a, i, iVal);
202082 iOff = ((i64)iVal) << 32;
202083 fts5FastGetVarint32(a, i, iVal);
202084 }
202085 *piOff = iOff + (iVal-2);
202086 *pi = i;
202087 return 0;
202088 }
202089}
202090
202091
202092/*
202093** Advance the iterator object passed as the only argument. Return true
202094** if the iterator reaches EOF, or false otherwise.
202095*/
202096static int sqlite3Fts5PoslistReaderNext(Fts5PoslistReader *pIter){
202097 if( sqlite3Fts5PoslistNext64(pIter->a, pIter->n, &pIter->i, &pIter->iPos) ){
202098 pIter->bEof = 1;
202099 }
202100 return pIter->bEof;
202101}
202102
202103static int sqlite3Fts5PoslistReaderInit(
202104 const u8 *a, int n, /* Poslist buffer to iterate through */
202105 Fts5PoslistReader *pIter /* Iterator object to initialize */
202106){
202107 memset(pIter, 0, sizeof(*pIter));
202108 pIter->a = a;
202109 pIter->n = n;
202110 sqlite3Fts5PoslistReaderNext(pIter);
202111 return pIter->bEof;
202112}
202113
202114/*
202115** Append position iPos to the position list being accumulated in buffer
202116** pBuf, which must be already be large enough to hold the new data.
202117** The previous position written to this list is *piPrev. *piPrev is set
202118** to iPos before returning.
202119*/
202120static void sqlite3Fts5PoslistSafeAppend(
202121 Fts5Buffer *pBuf,
202122 i64 *piPrev,
202123 i64 iPos
202124){
202125 static const i64 colmask = ((i64)(0x7FFFFFFF)) << 32;
202126 if( (iPos & colmask) != (*piPrev & colmask) ){
202127 pBuf->p[pBuf->n++] = 1;
202128 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos>>32));
202129 *piPrev = (iPos & colmask);
202130 }
202131 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos-*piPrev)+2);
202132 *piPrev = iPos;
202133}
202134
202135static int sqlite3Fts5PoslistWriterAppend(
202136 Fts5Buffer *pBuf,
202137 Fts5PoslistWriter *pWriter,
202138 i64 iPos
202139){
202140 int rc = 0; /* Initialized only to suppress erroneous warning from Clang */
202141 if( fts5BufferGrow(&rc, pBuf, 5+5+5) ) return rc;
202142 sqlite3Fts5PoslistSafeAppend(pBuf, &pWriter->iPrev, iPos);
202143 return SQLITE_OK;
202144}
202145
202146static void *sqlite3Fts5MallocZero(int *pRc, int nByte){
202147 void *pRet = 0;
202148 if( *pRc==SQLITE_OK ){
202149 pRet = sqlite3_malloc(nByte);
202150 if( pRet==0 ){
202151 if( nByte>0 ) *pRc = SQLITE_NOMEM;
202152 }else{
202153 memset(pRet, 0, nByte);
202154 }
202155 }
202156 return pRet;
202157}
202158
202159/*
202160** Return a nul-terminated copy of the string indicated by pIn. If nIn
202161** is non-negative, then it is the length of the string in bytes. Otherwise,
202162** the length of the string is determined using strlen().
202163**
202164** It is the responsibility of the caller to eventually free the returned
202165** buffer using sqlite3_free(). If an OOM error occurs, NULL is returned.
202166*/
202167static char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn){
202168 char *zRet = 0;
202169 if( *pRc==SQLITE_OK ){
202170 if( nIn<0 ){
202171 nIn = (int)strlen(pIn);
202172 }
202173 zRet = (char*)sqlite3_malloc(nIn+1);
202174 if( zRet ){
202175 memcpy(zRet, pIn, nIn);
202176 zRet[nIn] = '\0';
202177 }else{
202178 *pRc = SQLITE_NOMEM;
202179 }
202180 }
202181 return zRet;
202182}
202183
202184
202185/*
202186** Return true if character 't' may be part of an FTS5 bareword, or false
202187** otherwise. Characters that may be part of barewords:
202188**
202189** * All non-ASCII characters,
202190** * The 52 upper and lower case ASCII characters, and
202191** * The 10 integer ASCII characters.
202192** * The underscore character "_" (0x5F).
202193** * The unicode "subsitute" character (0x1A).
202194*/
202195static int sqlite3Fts5IsBareword(char t){
202196 u8 aBareword[128] = {
202197 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 .. 0x0F */
202198 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, /* 0x10 .. 0x1F */
202199 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 .. 0x2F */
202200 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 0x30 .. 0x3F */
202201 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 .. 0x4F */
202202 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 0x50 .. 0x5F */
202203 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 .. 0x6F */
202204 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 /* 0x70 .. 0x7F */
202205 };
202206
202207 return (t & 0x80) || aBareword[(int)t];
202208}
202209
202210
202211/*************************************************************************
202212*/
202213typedef struct Fts5TermsetEntry Fts5TermsetEntry;
202214struct Fts5TermsetEntry {
202215 char *pTerm;
202216 int nTerm;
202217 int iIdx; /* Index (main or aPrefix[] entry) */
202218 Fts5TermsetEntry *pNext;
202219};
202220
202221struct Fts5Termset {
202222 Fts5TermsetEntry *apHash[512];
202223};
202224
202225static int sqlite3Fts5TermsetNew(Fts5Termset **pp){
202226 int rc = SQLITE_OK;
202227 *pp = sqlite3Fts5MallocZero(&rc, sizeof(Fts5Termset));
202228 return rc;
202229}
202230
202231static int sqlite3Fts5TermsetAdd(
202232 Fts5Termset *p,
202233 int iIdx,
202234 const char *pTerm, int nTerm,
202235 int *pbPresent
202236){
202237 int rc = SQLITE_OK;
202238 *pbPresent = 0;
202239 if( p ){
202240 int i;
202241 u32 hash = 13;
202242 Fts5TermsetEntry *pEntry;
202243
202244 /* Calculate a hash value for this term. This is the same hash checksum
202245 ** used by the fts5_hash.c module. This is not important for correct
202246 ** operation of the module, but is necessary to ensure that some tests
202247 ** designed to produce hash table collisions really do work. */
202248 for(i=nTerm-1; i>=0; i--){
202249 hash = (hash << 3) ^ hash ^ pTerm[i];
202250 }
202251 hash = (hash << 3) ^ hash ^ iIdx;
202252 hash = hash % ArraySize(p->apHash);
202253
202254 for(pEntry=p->apHash[hash]; pEntry; pEntry=pEntry->pNext){
202255 if( pEntry->iIdx==iIdx
202256 && pEntry->nTerm==nTerm
202257 && memcmp(pEntry->pTerm, pTerm, nTerm)==0
202258 ){
202259 *pbPresent = 1;
202260 break;
202261 }
202262 }
202263
202264 if( pEntry==0 ){
202265 pEntry = sqlite3Fts5MallocZero(&rc, sizeof(Fts5TermsetEntry) + nTerm);
202266 if( pEntry ){
202267 pEntry->pTerm = (char*)&pEntry[1];
202268 pEntry->nTerm = nTerm;
202269 pEntry->iIdx = iIdx;
202270 memcpy(pEntry->pTerm, pTerm, nTerm);
202271 pEntry->pNext = p->apHash[hash];
202272 p->apHash[hash] = pEntry;
202273 }
202274 }
202275 }
202276
202277 return rc;
202278}
202279
202280static void sqlite3Fts5TermsetFree(Fts5Termset *p){
202281 if( p ){
202282 u32 i;
202283 for(i=0; i<ArraySize(p->apHash); i++){
202284 Fts5TermsetEntry *pEntry = p->apHash[i];
202285 while( pEntry ){
202286 Fts5TermsetEntry *pDel = pEntry;
202287 pEntry = pEntry->pNext;
202288 sqlite3_free(pDel);
202289 }
202290 }
202291 sqlite3_free(p);
202292 }
202293}
202294
202295/*
202296** 2014 Jun 09
202297**
202298** The author disclaims copyright to this source code. In place of
202299** a legal notice, here is a blessing:
202300**
202301** May you do good and not evil.
202302** May you find forgiveness for yourself and forgive others.
202303** May you share freely, never taking more than you give.
202304**
202305******************************************************************************
202306**
202307** This is an SQLite module implementing full-text search.
202308*/
202309
202310
202311/* #include "fts5Int.h" */
202312
202313#define FTS5_DEFAULT_PAGE_SIZE 4050
202314#define FTS5_DEFAULT_AUTOMERGE 4
202315#define FTS5_DEFAULT_USERMERGE 4
202316#define FTS5_DEFAULT_CRISISMERGE 16
202317#define FTS5_DEFAULT_HASHSIZE (1024*1024)
202318
202319/* Maximum allowed page size */
202320#define FTS5_MAX_PAGE_SIZE (128*1024)
202321
202322static int fts5_iswhitespace(char x){
202323 return (x==' ');
202324}
202325
202326static int fts5_isopenquote(char x){
202327 return (x=='"' || x=='\'' || x=='[' || x=='`');
202328}
202329
202330/*
202331** Argument pIn points to a character that is part of a nul-terminated
202332** string. Return a pointer to the first character following *pIn in
202333** the string that is not a white-space character.
202334*/
202335static const char *fts5ConfigSkipWhitespace(const char *pIn){
202336 const char *p = pIn;
202337 if( p ){
202338 while( fts5_iswhitespace(*p) ){ p++; }
202339 }
202340 return p;
202341}
202342
202343/*
202344** Argument pIn points to a character that is part of a nul-terminated
202345** string. Return a pointer to the first character following *pIn in
202346** the string that is not a "bareword" character.
202347*/
202348static const char *fts5ConfigSkipBareword(const char *pIn){
202349 const char *p = pIn;
202350 while ( sqlite3Fts5IsBareword(*p) ) p++;
202351 if( p==pIn ) p = 0;
202352 return p;
202353}
202354
202355static int fts5_isdigit(char a){
202356 return (a>='0' && a<='9');
202357}
202358
202359
202360
202361static const char *fts5ConfigSkipLiteral(const char *pIn){
202362 const char *p = pIn;
202363 switch( *p ){
202364 case 'n': case 'N':
202365 if( sqlite3_strnicmp("null", p, 4)==0 ){
202366 p = &p[4];
202367 }else{
202368 p = 0;
202369 }
202370 break;
202371
202372 case 'x': case 'X':
202373 p++;
202374 if( *p=='\'' ){
202375 p++;
202376 while( (*p>='a' && *p<='f')
202377 || (*p>='A' && *p<='F')
202378 || (*p>='0' && *p<='9')
202379 ){
202380 p++;
202381 }
202382 if( *p=='\'' && 0==((p-pIn)%2) ){
202383 p++;
202384 }else{
202385 p = 0;
202386 }
202387 }else{
202388 p = 0;
202389 }
202390 break;
202391
202392 case '\'':
202393 p++;
202394 while( p ){
202395 if( *p=='\'' ){
202396 p++;
202397 if( *p!='\'' ) break;
202398 }
202399 p++;
202400 if( *p==0 ) p = 0;
202401 }
202402 break;
202403
202404 default:
202405 /* maybe a number */
202406 if( *p=='+' || *p=='-' ) p++;
202407 while( fts5_isdigit(*p) ) p++;
202408
202409 /* At this point, if the literal was an integer, the parse is
202410 ** finished. Or, if it is a floating point value, it may continue
202411 ** with either a decimal point or an 'E' character. */
202412 if( *p=='.' && fts5_isdigit(p[1]) ){
202413 p += 2;
202414 while( fts5_isdigit(*p) ) p++;
202415 }
202416 if( p==pIn ) p = 0;
202417
202418 break;
202419 }
202420
202421 return p;
202422}
202423
202424/*
202425** The first character of the string pointed to by argument z is guaranteed
202426** to be an open-quote character (see function fts5_isopenquote()).
202427**
202428** This function searches for the corresponding close-quote character within
202429** the string and, if found, dequotes the string in place and adds a new
202430** nul-terminator byte.
202431**
202432** If the close-quote is found, the value returned is the byte offset of
202433** the character immediately following it. Or, if the close-quote is not
202434** found, -1 is returned. If -1 is returned, the buffer is left in an
202435** undefined state.
202436*/
202437static int fts5Dequote(char *z){
202438 char q;
202439 int iIn = 1;
202440 int iOut = 0;
202441 q = z[0];
202442
202443 /* Set stack variable q to the close-quote character */
202444 assert( q=='[' || q=='\'' || q=='"' || q=='`' );
202445 if( q=='[' ) q = ']';
202446
202447 while( ALWAYS(z[iIn]) ){
202448 if( z[iIn]==q ){
202449 if( z[iIn+1]!=q ){
202450 /* Character iIn was the close quote. */
202451 iIn++;
202452 break;
202453 }else{
202454 /* Character iIn and iIn+1 form an escaped quote character. Skip
202455 ** the input cursor past both and copy a single quote character
202456 ** to the output buffer. */
202457 iIn += 2;
202458 z[iOut++] = q;
202459 }
202460 }else{
202461 z[iOut++] = z[iIn++];
202462 }
202463 }
202464
202465 z[iOut] = '\0';
202466 return iIn;
202467}
202468
202469/*
202470** Convert an SQL-style quoted string into a normal string by removing
202471** the quote characters. The conversion is done in-place. If the
202472** input does not begin with a quote character, then this routine
202473** is a no-op.
202474**
202475** Examples:
202476**
202477** "abc" becomes abc
202478** 'xyz' becomes xyz
202479** [pqr] becomes pqr
202480** `mno` becomes mno
202481*/
202482static void sqlite3Fts5Dequote(char *z){
202483 char quote; /* Quote character (if any ) */
202484
202485 assert( 0==fts5_iswhitespace(z[0]) );
202486 quote = z[0];
202487 if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
202488 fts5Dequote(z);
202489 }
202490}
202491
202492
202493struct Fts5Enum {
202494 const char *zName;
202495 int eVal;
202496};
202497typedef struct Fts5Enum Fts5Enum;
202498
202499static int fts5ConfigSetEnum(
202500 const Fts5Enum *aEnum,
202501 const char *zEnum,
202502 int *peVal
202503){
202504 int nEnum = (int)strlen(zEnum);
202505 int i;
202506 int iVal = -1;
202507
202508 for(i=0; aEnum[i].zName; i++){
202509 if( sqlite3_strnicmp(aEnum[i].zName, zEnum, nEnum)==0 ){
202510 if( iVal>=0 ) return SQLITE_ERROR;
202511 iVal = aEnum[i].eVal;
202512 }
202513 }
202514
202515 *peVal = iVal;
202516 return iVal<0 ? SQLITE_ERROR : SQLITE_OK;
202517}
202518
202519/*
202520** Parse a "special" CREATE VIRTUAL TABLE directive and update
202521** configuration object pConfig as appropriate.
202522**
202523** If successful, object pConfig is updated and SQLITE_OK returned. If
202524** an error occurs, an SQLite error code is returned and an error message
202525** may be left in *pzErr. It is the responsibility of the caller to
202526** eventually free any such error message using sqlite3_free().
202527*/
202528static int fts5ConfigParseSpecial(
202529 Fts5Global *pGlobal,
202530 Fts5Config *pConfig, /* Configuration object to update */
202531 const char *zCmd, /* Special command to parse */
202532 const char *zArg, /* Argument to parse */
202533 char **pzErr /* OUT: Error message */
202534){
202535 int rc = SQLITE_OK;
202536 int nCmd = (int)strlen(zCmd);
202537 if( sqlite3_strnicmp("prefix", zCmd, nCmd)==0 ){
202538 const int nByte = sizeof(int) * FTS5_MAX_PREFIX_INDEXES;
202539 const char *p;
202540 int bFirst = 1;
202541 if( pConfig->aPrefix==0 ){
202542 pConfig->aPrefix = sqlite3Fts5MallocZero(&rc, nByte);
202543 if( rc ) return rc;
202544 }
202545
202546 p = zArg;
202547 while( 1 ){
202548 int nPre = 0;
202549
202550 while( p[0]==' ' ) p++;
202551 if( bFirst==0 && p[0]==',' ){
202552 p++;
202553 while( p[0]==' ' ) p++;
202554 }else if( p[0]=='\0' ){
202555 break;
202556 }
202557 if( p[0]<'0' || p[0]>'9' ){
202558 *pzErr = sqlite3_mprintf("malformed prefix=... directive");
202559 rc = SQLITE_ERROR;
202560 break;
202561 }
202562
202563 if( pConfig->nPrefix==FTS5_MAX_PREFIX_INDEXES ){
202564 *pzErr = sqlite3_mprintf(
202565 "too many prefix indexes (max %d)", FTS5_MAX_PREFIX_INDEXES
202566 );
202567 rc = SQLITE_ERROR;
202568 break;
202569 }
202570
202571 while( p[0]>='0' && p[0]<='9' && nPre<1000 ){
202572 nPre = nPre*10 + (p[0] - '0');
202573 p++;
202574 }
202575
202576 if( nPre<=0 || nPre>=1000 ){
202577 *pzErr = sqlite3_mprintf("prefix length out of range (max 999)");
202578 rc = SQLITE_ERROR;
202579 break;
202580 }
202581
202582 pConfig->aPrefix[pConfig->nPrefix] = nPre;
202583 pConfig->nPrefix++;
202584 bFirst = 0;
202585 }
202586 assert( pConfig->nPrefix<=FTS5_MAX_PREFIX_INDEXES );
202587 return rc;
202588 }
202589
202590 if( sqlite3_strnicmp("tokenize", zCmd, nCmd)==0 ){
202591 const char *p = (const char*)zArg;
202592 int nArg = (int)strlen(zArg) + 1;
202593 char **azArg = sqlite3Fts5MallocZero(&rc, sizeof(char*) * nArg);
202594 char *pDel = sqlite3Fts5MallocZero(&rc, nArg * 2);
202595 char *pSpace = pDel;
202596
202597 if( azArg && pSpace ){
202598 if( pConfig->pTok ){
202599 *pzErr = sqlite3_mprintf("multiple tokenize=... directives");
202600 rc = SQLITE_ERROR;
202601 }else{
202602 for(nArg=0; p && *p; nArg++){
202603 const char *p2 = fts5ConfigSkipWhitespace(p);
202604 if( *p2=='\'' ){
202605 p = fts5ConfigSkipLiteral(p2);
202606 }else{
202607 p = fts5ConfigSkipBareword(p2);
202608 }
202609 if( p ){
202610 memcpy(pSpace, p2, p-p2);
202611 azArg[nArg] = pSpace;
202612 sqlite3Fts5Dequote(pSpace);
202613 pSpace += (p - p2) + 1;
202614 p = fts5ConfigSkipWhitespace(p);
202615 }
202616 }
202617 if( p==0 ){
202618 *pzErr = sqlite3_mprintf("parse error in tokenize directive");
202619 rc = SQLITE_ERROR;
202620 }else{
202621 rc = sqlite3Fts5GetTokenizer(pGlobal,
202622 (const char**)azArg, nArg, &pConfig->pTok, &pConfig->pTokApi,
202623 pzErr
202624 );
202625 }
202626 }
202627 }
202628
202629 sqlite3_free(azArg);
202630 sqlite3_free(pDel);
202631 return rc;
202632 }
202633
202634 if( sqlite3_strnicmp("content", zCmd, nCmd)==0 ){
202635 if( pConfig->eContent!=FTS5_CONTENT_NORMAL ){
202636 *pzErr = sqlite3_mprintf("multiple content=... directives");
202637 rc = SQLITE_ERROR;
202638 }else{
202639 if( zArg[0] ){
202640 pConfig->eContent = FTS5_CONTENT_EXTERNAL;
202641 pConfig->zContent = sqlite3Fts5Mprintf(&rc, "%Q.%Q", pConfig->zDb,zArg);
202642 }else{
202643 pConfig->eContent = FTS5_CONTENT_NONE;
202644 }
202645 }
202646 return rc;
202647 }
202648
202649 if( sqlite3_strnicmp("content_rowid", zCmd, nCmd)==0 ){
202650 if( pConfig->zContentRowid ){
202651 *pzErr = sqlite3_mprintf("multiple content_rowid=... directives");
202652 rc = SQLITE_ERROR;
202653 }else{
202654 pConfig->zContentRowid = sqlite3Fts5Strndup(&rc, zArg, -1);
202655 }
202656 return rc;
202657 }
202658
202659 if( sqlite3_strnicmp("columnsize", zCmd, nCmd)==0 ){
202660 if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){
202661 *pzErr = sqlite3_mprintf("malformed columnsize=... directive");
202662 rc = SQLITE_ERROR;
202663 }else{
202664 pConfig->bColumnsize = (zArg[0]=='1');
202665 }
202666 return rc;
202667 }
202668
202669 if( sqlite3_strnicmp("detail", zCmd, nCmd)==0 ){
202670 const Fts5Enum aDetail[] = {
202671 { "none", FTS5_DETAIL_NONE },
202672 { "full", FTS5_DETAIL_FULL },
202673 { "columns", FTS5_DETAIL_COLUMNS },
202674 { 0, 0 }
202675 };
202676
202677 if( (rc = fts5ConfigSetEnum(aDetail, zArg, &pConfig->eDetail)) ){
202678 *pzErr = sqlite3_mprintf("malformed detail=... directive");
202679 }
202680 return rc;
202681 }
202682
202683 *pzErr = sqlite3_mprintf("unrecognized option: \"%.*s\"", nCmd, zCmd);
202684 return SQLITE_ERROR;
202685}
202686
202687/*
202688** Allocate an instance of the default tokenizer ("simple") at
202689** Fts5Config.pTokenizer. Return SQLITE_OK if successful, or an SQLite error
202690** code if an error occurs.
202691*/
202692static int fts5ConfigDefaultTokenizer(Fts5Global *pGlobal, Fts5Config *pConfig){
202693 assert( pConfig->pTok==0 && pConfig->pTokApi==0 );
202694 return sqlite3Fts5GetTokenizer(
202695 pGlobal, 0, 0, &pConfig->pTok, &pConfig->pTokApi, 0
202696 );
202697}
202698
202699/*
202700** Gobble up the first bareword or quoted word from the input buffer zIn.
202701** Return a pointer to the character immediately following the last in
202702** the gobbled word if successful, or a NULL pointer otherwise (failed
202703** to find close-quote character).
202704**
202705** Before returning, set pzOut to point to a new buffer containing a
202706** nul-terminated, dequoted copy of the gobbled word. If the word was
202707** quoted, *pbQuoted is also set to 1 before returning.
202708**
202709** If *pRc is other than SQLITE_OK when this function is called, it is
202710** a no-op (NULL is returned). Otherwise, if an OOM occurs within this
202711** function, *pRc is set to SQLITE_NOMEM before returning. *pRc is *not*
202712** set if a parse error (failed to find close quote) occurs.
202713*/
202714static const char *fts5ConfigGobbleWord(
202715 int *pRc, /* IN/OUT: Error code */
202716 const char *zIn, /* Buffer to gobble string/bareword from */
202717 char **pzOut, /* OUT: malloc'd buffer containing str/bw */
202718 int *pbQuoted /* OUT: Set to true if dequoting required */
202719){
202720 const char *zRet = 0;
202721
202722 int nIn = (int)strlen(zIn);
202723 char *zOut = sqlite3_malloc(nIn+1);
202724
202725 assert( *pRc==SQLITE_OK );
202726 *pbQuoted = 0;
202727 *pzOut = 0;
202728
202729 if( zOut==0 ){
202730 *pRc = SQLITE_NOMEM;
202731 }else{
202732 memcpy(zOut, zIn, nIn+1);
202733 if( fts5_isopenquote(zOut[0]) ){
202734 int ii = fts5Dequote(zOut);
202735 zRet = &zIn[ii];
202736 *pbQuoted = 1;
202737 }else{
202738 zRet = fts5ConfigSkipBareword(zIn);
202739 if( zRet ){
202740 zOut[zRet-zIn] = '\0';
202741 }
202742 }
202743 }
202744
202745 if( zRet==0 ){
202746 sqlite3_free(zOut);
202747 }else{
202748 *pzOut = zOut;
202749 }
202750
202751 return zRet;
202752}
202753
202754static int fts5ConfigParseColumn(
202755 Fts5Config *p,
202756 char *zCol,
202757 char *zArg,
202758 char **pzErr
202759){
202760 int rc = SQLITE_OK;
202761 if( 0==sqlite3_stricmp(zCol, FTS5_RANK_NAME)
202762 || 0==sqlite3_stricmp(zCol, FTS5_ROWID_NAME)
202763 ){
202764 *pzErr = sqlite3_mprintf("reserved fts5 column name: %s", zCol);
202765 rc = SQLITE_ERROR;
202766 }else if( zArg ){
202767 if( 0==sqlite3_stricmp(zArg, "unindexed") ){
202768 p->abUnindexed[p->nCol] = 1;
202769 }else{
202770 *pzErr = sqlite3_mprintf("unrecognized column option: %s", zArg);
202771 rc = SQLITE_ERROR;
202772 }
202773 }
202774
202775 p->azCol[p->nCol++] = zCol;
202776 return rc;
202777}
202778
202779/*
202780** Populate the Fts5Config.zContentExprlist string.
202781*/
202782static int fts5ConfigMakeExprlist(Fts5Config *p){
202783 int i;
202784 int rc = SQLITE_OK;
202785 Fts5Buffer buf = {0, 0, 0};
202786
202787 sqlite3Fts5BufferAppendPrintf(&rc, &buf, "T.%Q", p->zContentRowid);
202788 if( p->eContent!=FTS5_CONTENT_NONE ){
202789 for(i=0; i<p->nCol; i++){
202790 if( p->eContent==FTS5_CONTENT_EXTERNAL ){
202791 sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.%Q", p->azCol[i]);
202792 }else{
202793 sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.c%d", i);
202794 }
202795 }
202796 }
202797
202798 assert( p->zContentExprlist==0 );
202799 p->zContentExprlist = (char*)buf.p;
202800 return rc;
202801}
202802
202803/*
202804** Arguments nArg/azArg contain the string arguments passed to the xCreate
202805** or xConnect method of the virtual table. This function attempts to
202806** allocate an instance of Fts5Config containing the results of parsing
202807** those arguments.
202808**
202809** If successful, SQLITE_OK is returned and *ppOut is set to point to the
202810** new Fts5Config object. If an error occurs, an SQLite error code is
202811** returned, *ppOut is set to NULL and an error message may be left in
202812** *pzErr. It is the responsibility of the caller to eventually free any
202813** such error message using sqlite3_free().
202814*/
202815static int sqlite3Fts5ConfigParse(
202816 Fts5Global *pGlobal,
202817 sqlite3 *db,
202818 int nArg, /* Number of arguments */
202819 const char **azArg, /* Array of nArg CREATE VIRTUAL TABLE args */
202820 Fts5Config **ppOut, /* OUT: Results of parse */
202821 char **pzErr /* OUT: Error message */
202822){
202823 int rc = SQLITE_OK; /* Return code */
202824 Fts5Config *pRet; /* New object to return */
202825 int i;
202826 int nByte;
202827
202828 *ppOut = pRet = (Fts5Config*)sqlite3_malloc(sizeof(Fts5Config));
202829 if( pRet==0 ) return SQLITE_NOMEM;
202830 memset(pRet, 0, sizeof(Fts5Config));
202831 pRet->db = db;
202832 pRet->iCookie = -1;
202833
202834 nByte = nArg * (sizeof(char*) + sizeof(u8));
202835 pRet->azCol = (char**)sqlite3Fts5MallocZero(&rc, nByte);
202836 pRet->abUnindexed = (u8*)&pRet->azCol[nArg];
202837 pRet->zDb = sqlite3Fts5Strndup(&rc, azArg[1], -1);
202838 pRet->zName = sqlite3Fts5Strndup(&rc, azArg[2], -1);
202839 pRet->bColumnsize = 1;
202840 pRet->eDetail = FTS5_DETAIL_FULL;
202841#ifdef SQLITE_DEBUG
202842 pRet->bPrefixIndex = 1;
202843#endif
202844 if( rc==SQLITE_OK && sqlite3_stricmp(pRet->zName, FTS5_RANK_NAME)==0 ){
202845 *pzErr = sqlite3_mprintf("reserved fts5 table name: %s", pRet->zName);
202846 rc = SQLITE_ERROR;
202847 }
202848
202849 for(i=3; rc==SQLITE_OK && i<nArg; i++){
202850 const char *zOrig = azArg[i];
202851 const char *z;
202852 char *zOne = 0;
202853 char *zTwo = 0;
202854 int bOption = 0;
202855 int bMustBeCol = 0;
202856
202857 z = fts5ConfigGobbleWord(&rc, zOrig, &zOne, &bMustBeCol);
202858 z = fts5ConfigSkipWhitespace(z);
202859 if( z && *z=='=' ){
202860 bOption = 1;
202861 z++;
202862 if( bMustBeCol ) z = 0;
202863 }
202864 z = fts5ConfigSkipWhitespace(z);
202865 if( z && z[0] ){
202866 int bDummy;
202867 z = fts5ConfigGobbleWord(&rc, z, &zTwo, &bDummy);
202868 if( z && z[0] ) z = 0;
202869 }
202870
202871 if( rc==SQLITE_OK ){
202872 if( z==0 ){
202873 *pzErr = sqlite3_mprintf("parse error in \"%s\"", zOrig);
202874 rc = SQLITE_ERROR;
202875 }else{
202876 if( bOption ){
202877 rc = fts5ConfigParseSpecial(pGlobal, pRet, zOne, zTwo?zTwo:"", pzErr);
202878 }else{
202879 rc = fts5ConfigParseColumn(pRet, zOne, zTwo, pzErr);
202880 zOne = 0;
202881 }
202882 }
202883 }
202884
202885 sqlite3_free(zOne);
202886 sqlite3_free(zTwo);
202887 }
202888
202889 /* If a tokenizer= option was successfully parsed, the tokenizer has
202890 ** already been allocated. Otherwise, allocate an instance of the default
202891 ** tokenizer (unicode61) now. */
202892 if( rc==SQLITE_OK && pRet->pTok==0 ){
202893 rc = fts5ConfigDefaultTokenizer(pGlobal, pRet);
202894 }
202895
202896 /* If no zContent option was specified, fill in the default values. */
202897 if( rc==SQLITE_OK && pRet->zContent==0 ){
202898 const char *zTail = 0;
202899 assert( pRet->eContent==FTS5_CONTENT_NORMAL
202900 || pRet->eContent==FTS5_CONTENT_NONE
202901 );
202902 if( pRet->eContent==FTS5_CONTENT_NORMAL ){
202903 zTail = "content";
202904 }else if( pRet->bColumnsize ){
202905 zTail = "docsize";
202906 }
202907
202908 if( zTail ){
202909 pRet->zContent = sqlite3Fts5Mprintf(
202910 &rc, "%Q.'%q_%s'", pRet->zDb, pRet->zName, zTail
202911 );
202912 }
202913 }
202914
202915 if( rc==SQLITE_OK && pRet->zContentRowid==0 ){
202916 pRet->zContentRowid = sqlite3Fts5Strndup(&rc, "rowid", -1);
202917 }
202918
202919 /* Formulate the zContentExprlist text */
202920 if( rc==SQLITE_OK ){
202921 rc = fts5ConfigMakeExprlist(pRet);
202922 }
202923
202924 if( rc!=SQLITE_OK ){
202925 sqlite3Fts5ConfigFree(pRet);
202926 *ppOut = 0;
202927 }
202928 return rc;
202929}
202930
202931/*
202932** Free the configuration object passed as the only argument.
202933*/
202934static void sqlite3Fts5ConfigFree(Fts5Config *pConfig){
202935 if( pConfig ){
202936 int i;
202937 if( pConfig->pTok ){
202938 pConfig->pTokApi->xDelete(pConfig->pTok);
202939 }
202940 sqlite3_free(pConfig->zDb);
202941 sqlite3_free(pConfig->zName);
202942 for(i=0; i<pConfig->nCol; i++){
202943 sqlite3_free(pConfig->azCol[i]);
202944 }
202945 sqlite3_free(pConfig->azCol);
202946 sqlite3_free(pConfig->aPrefix);
202947 sqlite3_free(pConfig->zRank);
202948 sqlite3_free(pConfig->zRankArgs);
202949 sqlite3_free(pConfig->zContent);
202950 sqlite3_free(pConfig->zContentRowid);
202951 sqlite3_free(pConfig->zContentExprlist);
202952 sqlite3_free(pConfig);
202953 }
202954}
202955
202956/*
202957** Call sqlite3_declare_vtab() based on the contents of the configuration
202958** object passed as the only argument. Return SQLITE_OK if successful, or
202959** an SQLite error code if an error occurs.
202960*/
202961static int sqlite3Fts5ConfigDeclareVtab(Fts5Config *pConfig){
202962 int i;
202963 int rc = SQLITE_OK;
202964 char *zSql;
202965
202966 zSql = sqlite3Fts5Mprintf(&rc, "CREATE TABLE x(");
202967 for(i=0; zSql && i<pConfig->nCol; i++){
202968 const char *zSep = (i==0?"":", ");
202969 zSql = sqlite3Fts5Mprintf(&rc, "%z%s%Q", zSql, zSep, pConfig->azCol[i]);
202970 }
202971 zSql = sqlite3Fts5Mprintf(&rc, "%z, %Q HIDDEN, %s HIDDEN)",
202972 zSql, pConfig->zName, FTS5_RANK_NAME
202973 );
202974
202975 assert( zSql || rc==SQLITE_NOMEM );
202976 if( zSql ){
202977 rc = sqlite3_declare_vtab(pConfig->db, zSql);
202978 sqlite3_free(zSql);
202979 }
202980
202981 return rc;
202982}
202983
202984/*
202985** Tokenize the text passed via the second and third arguments.
202986**
202987** The callback is invoked once for each token in the input text. The
202988** arguments passed to it are, in order:
202989**
202990** void *pCtx // Copy of 4th argument to sqlite3Fts5Tokenize()
202991** const char *pToken // Pointer to buffer containing token
202992** int nToken // Size of token in bytes
202993** int iStart // Byte offset of start of token within input text
202994** int iEnd // Byte offset of end of token within input text
202995** int iPos // Position of token in input (first token is 0)
202996**
202997** If the callback returns a non-zero value the tokenization is abandoned
202998** and no further callbacks are issued.
202999**
203000** This function returns SQLITE_OK if successful or an SQLite error code
203001** if an error occurs. If the tokenization was abandoned early because
203002** the callback returned SQLITE_DONE, this is not an error and this function
203003** still returns SQLITE_OK. Or, if the tokenization was abandoned early
203004** because the callback returned another non-zero value, it is assumed
203005** to be an SQLite error code and returned to the caller.
203006*/
203007static int sqlite3Fts5Tokenize(
203008 Fts5Config *pConfig, /* FTS5 Configuration object */
203009 int flags, /* FTS5_TOKENIZE_* flags */
203010 const char *pText, int nText, /* Text to tokenize */
203011 void *pCtx, /* Context passed to xToken() */
203012 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
203013){
203014 if( pText==0 ) return SQLITE_OK;
203015 return pConfig->pTokApi->xTokenize(
203016 pConfig->pTok, pCtx, flags, pText, nText, xToken
203017 );
203018}
203019
203020/*
203021** Argument pIn points to the first character in what is expected to be
203022** a comma-separated list of SQL literals followed by a ')' character.
203023** If it actually is this, return a pointer to the ')'. Otherwise, return
203024** NULL to indicate a parse error.
203025*/
203026static const char *fts5ConfigSkipArgs(const char *pIn){
203027 const char *p = pIn;
203028
203029 while( 1 ){
203030 p = fts5ConfigSkipWhitespace(p);
203031 p = fts5ConfigSkipLiteral(p);
203032 p = fts5ConfigSkipWhitespace(p);
203033 if( p==0 || *p==')' ) break;
203034 if( *p!=',' ){
203035 p = 0;
203036 break;
203037 }
203038 p++;
203039 }
203040
203041 return p;
203042}
203043
203044/*
203045** Parameter zIn contains a rank() function specification. The format of
203046** this is:
203047**
203048** + Bareword (function name)
203049** + Open parenthesis - "("
203050** + Zero or more SQL literals in a comma separated list
203051** + Close parenthesis - ")"
203052*/
203053static int sqlite3Fts5ConfigParseRank(
203054 const char *zIn, /* Input string */
203055 char **pzRank, /* OUT: Rank function name */
203056 char **pzRankArgs /* OUT: Rank function arguments */
203057){
203058 const char *p = zIn;
203059 const char *pRank;
203060 char *zRank = 0;
203061 char *zRankArgs = 0;
203062 int rc = SQLITE_OK;
203063
203064 *pzRank = 0;
203065 *pzRankArgs = 0;
203066
203067 if( p==0 ){
203068 rc = SQLITE_ERROR;
203069 }else{
203070 p = fts5ConfigSkipWhitespace(p);
203071 pRank = p;
203072 p = fts5ConfigSkipBareword(p);
203073
203074 if( p ){
203075 zRank = sqlite3Fts5MallocZero(&rc, 1 + p - pRank);
203076 if( zRank ) memcpy(zRank, pRank, p-pRank);
203077 }else{
203078 rc = SQLITE_ERROR;
203079 }
203080
203081 if( rc==SQLITE_OK ){
203082 p = fts5ConfigSkipWhitespace(p);
203083 if( *p!='(' ) rc = SQLITE_ERROR;
203084 p++;
203085 }
203086 if( rc==SQLITE_OK ){
203087 const char *pArgs;
203088 p = fts5ConfigSkipWhitespace(p);
203089 pArgs = p;
203090 if( *p!=')' ){
203091 p = fts5ConfigSkipArgs(p);
203092 if( p==0 ){
203093 rc = SQLITE_ERROR;
203094 }else{
203095 zRankArgs = sqlite3Fts5MallocZero(&rc, 1 + p - pArgs);
203096 if( zRankArgs ) memcpy(zRankArgs, pArgs, p-pArgs);
203097 }
203098 }
203099 }
203100 }
203101
203102 if( rc!=SQLITE_OK ){
203103 sqlite3_free(zRank);
203104 assert( zRankArgs==0 );
203105 }else{
203106 *pzRank = zRank;
203107 *pzRankArgs = zRankArgs;
203108 }
203109 return rc;
203110}
203111
203112static int sqlite3Fts5ConfigSetValue(
203113 Fts5Config *pConfig,
203114 const char *zKey,
203115 sqlite3_value *pVal,
203116 int *pbBadkey
203117){
203118 int rc = SQLITE_OK;
203119
203120 if( 0==sqlite3_stricmp(zKey, "pgsz") ){
203121 int pgsz = 0;
203122 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
203123 pgsz = sqlite3_value_int(pVal);
203124 }
203125 if( pgsz<=0 || pgsz>FTS5_MAX_PAGE_SIZE ){
203126 *pbBadkey = 1;
203127 }else{
203128 pConfig->pgsz = pgsz;
203129 }
203130 }
203131
203132 else if( 0==sqlite3_stricmp(zKey, "hashsize") ){
203133 int nHashSize = -1;
203134 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
203135 nHashSize = sqlite3_value_int(pVal);
203136 }
203137 if( nHashSize<=0 ){
203138 *pbBadkey = 1;
203139 }else{
203140 pConfig->nHashSize = nHashSize;
203141 }
203142 }
203143
203144 else if( 0==sqlite3_stricmp(zKey, "automerge") ){
203145 int nAutomerge = -1;
203146 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
203147 nAutomerge = sqlite3_value_int(pVal);
203148 }
203149 if( nAutomerge<0 || nAutomerge>64 ){
203150 *pbBadkey = 1;
203151 }else{
203152 if( nAutomerge==1 ) nAutomerge = FTS5_DEFAULT_AUTOMERGE;
203153 pConfig->nAutomerge = nAutomerge;
203154 }
203155 }
203156
203157 else if( 0==sqlite3_stricmp(zKey, "usermerge") ){
203158 int nUsermerge = -1;
203159 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
203160 nUsermerge = sqlite3_value_int(pVal);
203161 }
203162 if( nUsermerge<2 || nUsermerge>16 ){
203163 *pbBadkey = 1;
203164 }else{
203165 pConfig->nUsermerge = nUsermerge;
203166 }
203167 }
203168
203169 else if( 0==sqlite3_stricmp(zKey, "crisismerge") ){
203170 int nCrisisMerge = -1;
203171 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
203172 nCrisisMerge = sqlite3_value_int(pVal);
203173 }
203174 if( nCrisisMerge<0 ){
203175 *pbBadkey = 1;
203176 }else{
203177 if( nCrisisMerge<=1 ) nCrisisMerge = FTS5_DEFAULT_CRISISMERGE;
203178 pConfig->nCrisisMerge = nCrisisMerge;
203179 }
203180 }
203181
203182 else if( 0==sqlite3_stricmp(zKey, "rank") ){
203183 const char *zIn = (const char*)sqlite3_value_text(pVal);
203184 char *zRank;
203185 char *zRankArgs;
203186 rc = sqlite3Fts5ConfigParseRank(zIn, &zRank, &zRankArgs);
203187 if( rc==SQLITE_OK ){
203188 sqlite3_free(pConfig->zRank);
203189 sqlite3_free(pConfig->zRankArgs);
203190 pConfig->zRank = zRank;
203191 pConfig->zRankArgs = zRankArgs;
203192 }else if( rc==SQLITE_ERROR ){
203193 rc = SQLITE_OK;
203194 *pbBadkey = 1;
203195 }
203196 }else{
203197 *pbBadkey = 1;
203198 }
203199 return rc;
203200}
203201
203202/*
203203** Load the contents of the %_config table into memory.
203204*/
203205static int sqlite3Fts5ConfigLoad(Fts5Config *pConfig, int iCookie){
203206 const char *zSelect = "SELECT k, v FROM %Q.'%q_config'";
203207 char *zSql;
203208 sqlite3_stmt *p = 0;
203209 int rc = SQLITE_OK;
203210 int iVersion = 0;
203211
203212 /* Set default values */
203213 pConfig->pgsz = FTS5_DEFAULT_PAGE_SIZE;
203214 pConfig->nAutomerge = FTS5_DEFAULT_AUTOMERGE;
203215 pConfig->nUsermerge = FTS5_DEFAULT_USERMERGE;
203216 pConfig->nCrisisMerge = FTS5_DEFAULT_CRISISMERGE;
203217 pConfig->nHashSize = FTS5_DEFAULT_HASHSIZE;
203218
203219 zSql = sqlite3Fts5Mprintf(&rc, zSelect, pConfig->zDb, pConfig->zName);
203220 if( zSql ){
203221 rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &p, 0);
203222 sqlite3_free(zSql);
203223 }
203224
203225 assert( rc==SQLITE_OK || p==0 );
203226 if( rc==SQLITE_OK ){
203227 while( SQLITE_ROW==sqlite3_step(p) ){
203228 const char *zK = (const char*)sqlite3_column_text(p, 0);
203229 sqlite3_value *pVal = sqlite3_column_value(p, 1);
203230 if( 0==sqlite3_stricmp(zK, "version") ){
203231 iVersion = sqlite3_value_int(pVal);
203232 }else{
203233 int bDummy = 0;
203234 sqlite3Fts5ConfigSetValue(pConfig, zK, pVal, &bDummy);
203235 }
203236 }
203237 rc = sqlite3_finalize(p);
203238 }
203239
203240 if( rc==SQLITE_OK && iVersion!=FTS5_CURRENT_VERSION ){
203241 rc = SQLITE_ERROR;
203242 if( pConfig->pzErrmsg ){
203243 assert( 0==*pConfig->pzErrmsg );
203244 *pConfig->pzErrmsg = sqlite3_mprintf(
203245 "invalid fts5 file format (found %d, expected %d) - run 'rebuild'",
203246 iVersion, FTS5_CURRENT_VERSION
203247 );
203248 }
203249 }
203250
203251 if( rc==SQLITE_OK ){
203252 pConfig->iCookie = iCookie;
203253 }
203254 return rc;
203255}
203256
203257/*
203258** 2014 May 31
203259**
203260** The author disclaims copyright to this source code. In place of
203261** a legal notice, here is a blessing:
203262**
203263** May you do good and not evil.
203264** May you find forgiveness for yourself and forgive others.
203265** May you share freely, never taking more than you give.
203266**
203267******************************************************************************
203268**
203269*/
203270
203271
203272
203273/* #include "fts5Int.h" */
203274/* #include "fts5parse.h" */
203275
203276/*
203277** All token types in the generated fts5parse.h file are greater than 0.
203278*/
203279#define FTS5_EOF 0
203280
203281#define FTS5_LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
203282
203283typedef struct Fts5ExprTerm Fts5ExprTerm;
203284
203285/*
203286** Functions generated by lemon from fts5parse.y.
203287*/
203288static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(u64));
203289static void sqlite3Fts5ParserFree(void*, void (*freeProc)(void*));
203290static void sqlite3Fts5Parser(void*, int, Fts5Token, Fts5Parse*);
203291#ifndef NDEBUG
203292/* #include <stdio.h> */
203293static void sqlite3Fts5ParserTrace(FILE*, char*);
203294#endif
203295static int sqlite3Fts5ParserFallback(int);
203296
203297
203298struct Fts5Expr {
203299 Fts5Index *pIndex;
203300 Fts5Config *pConfig;
203301 Fts5ExprNode *pRoot;
203302 int bDesc; /* Iterate in descending rowid order */
203303 int nPhrase; /* Number of phrases in expression */
203304 Fts5ExprPhrase **apExprPhrase; /* Pointers to phrase objects */
203305};
203306
203307/*
203308** eType:
203309** Expression node type. Always one of:
203310**
203311** FTS5_AND (nChild, apChild valid)
203312** FTS5_OR (nChild, apChild valid)
203313** FTS5_NOT (nChild, apChild valid)
203314** FTS5_STRING (pNear valid)
203315** FTS5_TERM (pNear valid)
203316*/
203317struct Fts5ExprNode {
203318 int eType; /* Node type */
203319 int bEof; /* True at EOF */
203320 int bNomatch; /* True if entry is not a match */
203321
203322 /* Next method for this node. */
203323 int (*xNext)(Fts5Expr*, Fts5ExprNode*, int, i64);
203324
203325 i64 iRowid; /* Current rowid */
203326 Fts5ExprNearset *pNear; /* For FTS5_STRING - cluster of phrases */
203327
203328 /* Child nodes. For a NOT node, this array always contains 2 entries. For
203329 ** AND or OR nodes, it contains 2 or more entries. */
203330 int nChild; /* Number of child nodes */
203331 Fts5ExprNode *apChild[1]; /* Array of child nodes */
203332};
203333
203334#define Fts5NodeIsString(p) ((p)->eType==FTS5_TERM || (p)->eType==FTS5_STRING)
203335
203336/*
203337** Invoke the xNext method of an Fts5ExprNode object. This macro should be
203338** used as if it has the same signature as the xNext() methods themselves.
203339*/
203340#define fts5ExprNodeNext(a,b,c,d) (b)->xNext((a), (b), (c), (d))
203341
203342/*
203343** An instance of the following structure represents a single search term
203344** or term prefix.
203345*/
203346struct Fts5ExprTerm {
203347 u8 bPrefix; /* True for a prefix term */
203348 u8 bFirst; /* True if token must be first in column */
203349 char *zTerm; /* nul-terminated term */
203350 Fts5IndexIter *pIter; /* Iterator for this term */
203351 Fts5ExprTerm *pSynonym; /* Pointer to first in list of synonyms */
203352};
203353
203354/*
203355** A phrase. One or more terms that must appear in a contiguous sequence
203356** within a document for it to match.
203357*/
203358struct Fts5ExprPhrase {
203359 Fts5ExprNode *pNode; /* FTS5_STRING node this phrase is part of */
203360 Fts5Buffer poslist; /* Current position list */
203361 int nTerm; /* Number of entries in aTerm[] */
203362 Fts5ExprTerm aTerm[1]; /* Terms that make up this phrase */
203363};
203364
203365/*
203366** One or more phrases that must appear within a certain token distance of
203367** each other within each matching document.
203368*/
203369struct Fts5ExprNearset {
203370 int nNear; /* NEAR parameter */
203371 Fts5Colset *pColset; /* Columns to search (NULL -> all columns) */
203372 int nPhrase; /* Number of entries in aPhrase[] array */
203373 Fts5ExprPhrase *apPhrase[1]; /* Array of phrase pointers */
203374};
203375
203376
203377/*
203378** Parse context.
203379*/
203380struct Fts5Parse {
203381 Fts5Config *pConfig;
203382 char *zErr;
203383 int rc;
203384 int nPhrase; /* Size of apPhrase array */
203385 Fts5ExprPhrase **apPhrase; /* Array of all phrases */
203386 Fts5ExprNode *pExpr; /* Result of a successful parse */
203387};
203388
203389static void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...){
203390 va_list ap;
203391 va_start(ap, zFmt);
203392 if( pParse->rc==SQLITE_OK ){
203393 pParse->zErr = sqlite3_vmprintf(zFmt, ap);
203394 pParse->rc = SQLITE_ERROR;
203395 }
203396 va_end(ap);
203397}
203398
203399static int fts5ExprIsspace(char t){
203400 return t==' ' || t=='\t' || t=='\n' || t=='\r';
203401}
203402
203403/*
203404** Read the first token from the nul-terminated string at *pz.
203405*/
203406static int fts5ExprGetToken(
203407 Fts5Parse *pParse,
203408 const char **pz, /* IN/OUT: Pointer into buffer */
203409 Fts5Token *pToken
203410){
203411 const char *z = *pz;
203412 int tok;
203413
203414 /* Skip past any whitespace */
203415 while( fts5ExprIsspace(*z) ) z++;
203416
203417 pToken->p = z;
203418 pToken->n = 1;
203419 switch( *z ){
203420 case '(': tok = FTS5_LP; break;
203421 case ')': tok = FTS5_RP; break;
203422 case '{': tok = FTS5_LCP; break;
203423 case '}': tok = FTS5_RCP; break;
203424 case ':': tok = FTS5_COLON; break;
203425 case ',': tok = FTS5_COMMA; break;
203426 case '+': tok = FTS5_PLUS; break;
203427 case '*': tok = FTS5_STAR; break;
203428 case '-': tok = FTS5_MINUS; break;
203429 case '^': tok = FTS5_CARET; break;
203430 case '\0': tok = FTS5_EOF; break;
203431
203432 case '"': {
203433 const char *z2;
203434 tok = FTS5_STRING;
203435
203436 for(z2=&z[1]; 1; z2++){
203437 if( z2[0]=='"' ){
203438 z2++;
203439 if( z2[0]!='"' ) break;
203440 }
203441 if( z2[0]=='\0' ){
203442 sqlite3Fts5ParseError(pParse, "unterminated string");
203443 return FTS5_EOF;
203444 }
203445 }
203446 pToken->n = (z2 - z);
203447 break;
203448 }
203449
203450 default: {
203451 const char *z2;
203452 if( sqlite3Fts5IsBareword(z[0])==0 ){
203453 sqlite3Fts5ParseError(pParse, "fts5: syntax error near \"%.1s\"", z);
203454 return FTS5_EOF;
203455 }
203456 tok = FTS5_STRING;
203457 for(z2=&z[1]; sqlite3Fts5IsBareword(*z2); z2++);
203458 pToken->n = (z2 - z);
203459 if( pToken->n==2 && memcmp(pToken->p, "OR", 2)==0 ) tok = FTS5_OR;
203460 if( pToken->n==3 && memcmp(pToken->p, "NOT", 3)==0 ) tok = FTS5_NOT;
203461 if( pToken->n==3 && memcmp(pToken->p, "AND", 3)==0 ) tok = FTS5_AND;
203462 break;
203463 }
203464 }
203465
203466 *pz = &pToken->p[pToken->n];
203467 return tok;
203468}
203469
203470static void *fts5ParseAlloc(u64 t){ return sqlite3_malloc((int)t); }
203471static void fts5ParseFree(void *p){ sqlite3_free(p); }
203472
203473static int sqlite3Fts5ExprNew(
203474 Fts5Config *pConfig, /* FTS5 Configuration */
203475 int iCol,
203476 const char *zExpr, /* Expression text */
203477 Fts5Expr **ppNew,
203478 char **pzErr
203479){
203480 Fts5Parse sParse;
203481 Fts5Token token;
203482 const char *z = zExpr;
203483 int t; /* Next token type */
203484 void *pEngine;
203485 Fts5Expr *pNew;
203486
203487 *ppNew = 0;
203488 *pzErr = 0;
203489 memset(&sParse, 0, sizeof(sParse));
203490 pEngine = sqlite3Fts5ParserAlloc(fts5ParseAlloc);
203491 if( pEngine==0 ){ return SQLITE_NOMEM; }
203492 sParse.pConfig = pConfig;
203493
203494 do {
203495 t = fts5ExprGetToken(&sParse, &z, &token);
203496 sqlite3Fts5Parser(pEngine, t, token, &sParse);
203497 }while( sParse.rc==SQLITE_OK && t!=FTS5_EOF );
203498 sqlite3Fts5ParserFree(pEngine, fts5ParseFree);
203499
203500 /* If the LHS of the MATCH expression was a user column, apply the
203501 ** implicit column-filter. */
203502 if( iCol<pConfig->nCol && sParse.pExpr && sParse.rc==SQLITE_OK ){
203503 int n = sizeof(Fts5Colset);
203504 Fts5Colset *pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&sParse.rc, n);
203505 if( pColset ){
203506 pColset->nCol = 1;
203507 pColset->aiCol[0] = iCol;
203508 sqlite3Fts5ParseSetColset(&sParse, sParse.pExpr, pColset);
203509 }
203510 }
203511
203512 assert( sParse.rc!=SQLITE_OK || sParse.zErr==0 );
203513 if( sParse.rc==SQLITE_OK ){
203514 *ppNew = pNew = sqlite3_malloc(sizeof(Fts5Expr));
203515 if( pNew==0 ){
203516 sParse.rc = SQLITE_NOMEM;
203517 sqlite3Fts5ParseNodeFree(sParse.pExpr);
203518 }else{
203519 if( !sParse.pExpr ){
203520 const int nByte = sizeof(Fts5ExprNode);
203521 pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&sParse.rc, nByte);
203522 if( pNew->pRoot ){
203523 pNew->pRoot->bEof = 1;
203524 }
203525 }else{
203526 pNew->pRoot = sParse.pExpr;
203527 }
203528 pNew->pIndex = 0;
203529 pNew->pConfig = pConfig;
203530 pNew->apExprPhrase = sParse.apPhrase;
203531 pNew->nPhrase = sParse.nPhrase;
203532 sParse.apPhrase = 0;
203533 }
203534 }else{
203535 sqlite3Fts5ParseNodeFree(sParse.pExpr);
203536 }
203537
203538 sqlite3_free(sParse.apPhrase);
203539 *pzErr = sParse.zErr;
203540 return sParse.rc;
203541}
203542
203543/*
203544** Free the expression node object passed as the only argument.
203545*/
203546static void sqlite3Fts5ParseNodeFree(Fts5ExprNode *p){
203547 if( p ){
203548 int i;
203549 for(i=0; i<p->nChild; i++){
203550 sqlite3Fts5ParseNodeFree(p->apChild[i]);
203551 }
203552 sqlite3Fts5ParseNearsetFree(p->pNear);
203553 sqlite3_free(p);
203554 }
203555}
203556
203557/*
203558** Free the expression object passed as the only argument.
203559*/
203560static void sqlite3Fts5ExprFree(Fts5Expr *p){
203561 if( p ){
203562 sqlite3Fts5ParseNodeFree(p->pRoot);
203563 sqlite3_free(p->apExprPhrase);
203564 sqlite3_free(p);
203565 }
203566}
203567
203568/*
203569** Argument pTerm must be a synonym iterator. Return the current rowid
203570** that it points to.
203571*/
203572static i64 fts5ExprSynonymRowid(Fts5ExprTerm *pTerm, int bDesc, int *pbEof){
203573 i64 iRet = 0;
203574 int bRetValid = 0;
203575 Fts5ExprTerm *p;
203576
203577 assert( pTerm->pSynonym );
203578 assert( bDesc==0 || bDesc==1 );
203579 for(p=pTerm; p; p=p->pSynonym){
203580 if( 0==sqlite3Fts5IterEof(p->pIter) ){
203581 i64 iRowid = p->pIter->iRowid;
203582 if( bRetValid==0 || (bDesc!=(iRowid<iRet)) ){
203583 iRet = iRowid;
203584 bRetValid = 1;
203585 }
203586 }
203587 }
203588
203589 if( pbEof && bRetValid==0 ) *pbEof = 1;
203590 return iRet;
203591}
203592
203593/*
203594** Argument pTerm must be a synonym iterator.
203595*/
203596static int fts5ExprSynonymList(
203597 Fts5ExprTerm *pTerm,
203598 i64 iRowid,
203599 Fts5Buffer *pBuf, /* Use this buffer for space if required */
203600 u8 **pa, int *pn
203601){
203602 Fts5PoslistReader aStatic[4];
203603 Fts5PoslistReader *aIter = aStatic;
203604 int nIter = 0;
203605 int nAlloc = 4;
203606 int rc = SQLITE_OK;
203607 Fts5ExprTerm *p;
203608
203609 assert( pTerm->pSynonym );
203610 for(p=pTerm; p; p=p->pSynonym){
203611 Fts5IndexIter *pIter = p->pIter;
203612 if( sqlite3Fts5IterEof(pIter)==0 && pIter->iRowid==iRowid ){
203613 if( pIter->nData==0 ) continue;
203614 if( nIter==nAlloc ){
203615 int nByte = sizeof(Fts5PoslistReader) * nAlloc * 2;
203616 Fts5PoslistReader *aNew = (Fts5PoslistReader*)sqlite3_malloc(nByte);
203617 if( aNew==0 ){
203618 rc = SQLITE_NOMEM;
203619 goto synonym_poslist_out;
203620 }
203621 memcpy(aNew, aIter, sizeof(Fts5PoslistReader) * nIter);
203622 nAlloc = nAlloc*2;
203623 if( aIter!=aStatic ) sqlite3_free(aIter);
203624 aIter = aNew;
203625 }
203626 sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &aIter[nIter]);
203627 assert( aIter[nIter].bEof==0 );
203628 nIter++;
203629 }
203630 }
203631
203632 if( nIter==1 ){
203633 *pa = (u8*)aIter[0].a;
203634 *pn = aIter[0].n;
203635 }else{
203636 Fts5PoslistWriter writer = {0};
203637 i64 iPrev = -1;
203638 fts5BufferZero(pBuf);
203639 while( 1 ){
203640 int i;
203641 i64 iMin = FTS5_LARGEST_INT64;
203642 for(i=0; i<nIter; i++){
203643 if( aIter[i].bEof==0 ){
203644 if( aIter[i].iPos==iPrev ){
203645 if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) continue;
203646 }
203647 if( aIter[i].iPos<iMin ){
203648 iMin = aIter[i].iPos;
203649 }
203650 }
203651 }
203652 if( iMin==FTS5_LARGEST_INT64 || rc!=SQLITE_OK ) break;
203653 rc = sqlite3Fts5PoslistWriterAppend(pBuf, &writer, iMin);
203654 iPrev = iMin;
203655 }
203656 if( rc==SQLITE_OK ){
203657 *pa = pBuf->p;
203658 *pn = pBuf->n;
203659 }
203660 }
203661
203662 synonym_poslist_out:
203663 if( aIter!=aStatic ) sqlite3_free(aIter);
203664 return rc;
203665}
203666
203667
203668/*
203669** All individual term iterators in pPhrase are guaranteed to be valid and
203670** pointing to the same rowid when this function is called. This function
203671** checks if the current rowid really is a match, and if so populates
203672** the pPhrase->poslist buffer accordingly. Output parameter *pbMatch
203673** is set to true if this is really a match, or false otherwise.
203674**
203675** SQLITE_OK is returned if an error occurs, or an SQLite error code
203676** otherwise. It is not considered an error code if the current rowid is
203677** not a match.
203678*/
203679static int fts5ExprPhraseIsMatch(
203680 Fts5ExprNode *pNode, /* Node pPhrase belongs to */
203681 Fts5ExprPhrase *pPhrase, /* Phrase object to initialize */
203682 int *pbMatch /* OUT: Set to true if really a match */
203683){
203684 Fts5PoslistWriter writer = {0};
203685 Fts5PoslistReader aStatic[4];
203686 Fts5PoslistReader *aIter = aStatic;
203687 int i;
203688 int rc = SQLITE_OK;
203689 int bFirst = pPhrase->aTerm[0].bFirst;
203690
203691 fts5BufferZero(&pPhrase->poslist);
203692
203693 /* If the aStatic[] array is not large enough, allocate a large array
203694 ** using sqlite3_malloc(). This approach could be improved upon. */
203695 if( pPhrase->nTerm>ArraySize(aStatic) ){
203696 int nByte = sizeof(Fts5PoslistReader) * pPhrase->nTerm;
203697 aIter = (Fts5PoslistReader*)sqlite3_malloc(nByte);
203698 if( !aIter ) return SQLITE_NOMEM;
203699 }
203700 memset(aIter, 0, sizeof(Fts5PoslistReader) * pPhrase->nTerm);
203701
203702 /* Initialize a term iterator for each term in the phrase */
203703 for(i=0; i<pPhrase->nTerm; i++){
203704 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
203705 int n = 0;
203706 int bFlag = 0;
203707 u8 *a = 0;
203708 if( pTerm->pSynonym ){
203709 Fts5Buffer buf = {0, 0, 0};
203710 rc = fts5ExprSynonymList(pTerm, pNode->iRowid, &buf, &a, &n);
203711 if( rc ){
203712 sqlite3_free(a);
203713 goto ismatch_out;
203714 }
203715 if( a==buf.p ) bFlag = 1;
203716 }else{
203717 a = (u8*)pTerm->pIter->pData;
203718 n = pTerm->pIter->nData;
203719 }
203720 sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]);
203721 aIter[i].bFlag = (u8)bFlag;
203722 if( aIter[i].bEof ) goto ismatch_out;
203723 }
203724
203725 while( 1 ){
203726 int bMatch;
203727 i64 iPos = aIter[0].iPos;
203728 do {
203729 bMatch = 1;
203730 for(i=0; i<pPhrase->nTerm; i++){
203731 Fts5PoslistReader *pPos = &aIter[i];
203732 i64 iAdj = iPos + i;
203733 if( pPos->iPos!=iAdj ){
203734 bMatch = 0;
203735 while( pPos->iPos<iAdj ){
203736 if( sqlite3Fts5PoslistReaderNext(pPos) ) goto ismatch_out;
203737 }
203738 if( pPos->iPos>iAdj ) iPos = pPos->iPos-i;
203739 }
203740 }
203741 }while( bMatch==0 );
203742
203743 /* Append position iPos to the output */
203744 if( bFirst==0 || FTS5_POS2OFFSET(iPos)==0 ){
203745 rc = sqlite3Fts5PoslistWriterAppend(&pPhrase->poslist, &writer, iPos);
203746 if( rc!=SQLITE_OK ) goto ismatch_out;
203747 }
203748
203749 for(i=0; i<pPhrase->nTerm; i++){
203750 if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) goto ismatch_out;
203751 }
203752 }
203753
203754 ismatch_out:
203755 *pbMatch = (pPhrase->poslist.n>0);
203756 for(i=0; i<pPhrase->nTerm; i++){
203757 if( aIter[i].bFlag ) sqlite3_free((u8*)aIter[i].a);
203758 }
203759 if( aIter!=aStatic ) sqlite3_free(aIter);
203760 return rc;
203761}
203762
203763typedef struct Fts5LookaheadReader Fts5LookaheadReader;
203764struct Fts5LookaheadReader {
203765 const u8 *a; /* Buffer containing position list */
203766 int n; /* Size of buffer a[] in bytes */
203767 int i; /* Current offset in position list */
203768 i64 iPos; /* Current position */
203769 i64 iLookahead; /* Next position */
203770};
203771
203772#define FTS5_LOOKAHEAD_EOF (((i64)1) << 62)
203773
203774static int fts5LookaheadReaderNext(Fts5LookaheadReader *p){
203775 p->iPos = p->iLookahead;
203776 if( sqlite3Fts5PoslistNext64(p->a, p->n, &p->i, &p->iLookahead) ){
203777 p->iLookahead = FTS5_LOOKAHEAD_EOF;
203778 }
203779 return (p->iPos==FTS5_LOOKAHEAD_EOF);
203780}
203781
203782static int fts5LookaheadReaderInit(
203783 const u8 *a, int n, /* Buffer to read position list from */
203784 Fts5LookaheadReader *p /* Iterator object to initialize */
203785){
203786 memset(p, 0, sizeof(Fts5LookaheadReader));
203787 p->a = a;
203788 p->n = n;
203789 fts5LookaheadReaderNext(p);
203790 return fts5LookaheadReaderNext(p);
203791}
203792
203793typedef struct Fts5NearTrimmer Fts5NearTrimmer;
203794struct Fts5NearTrimmer {
203795 Fts5LookaheadReader reader; /* Input iterator */
203796 Fts5PoslistWriter writer; /* Writer context */
203797 Fts5Buffer *pOut; /* Output poslist */
203798};
203799
203800/*
203801** The near-set object passed as the first argument contains more than
203802** one phrase. All phrases currently point to the same row. The
203803** Fts5ExprPhrase.poslist buffers are populated accordingly. This function
203804** tests if the current row contains instances of each phrase sufficiently
203805** close together to meet the NEAR constraint. Non-zero is returned if it
203806** does, or zero otherwise.
203807**
203808** If in/out parameter (*pRc) is set to other than SQLITE_OK when this
203809** function is called, it is a no-op. Or, if an error (e.g. SQLITE_NOMEM)
203810** occurs within this function (*pRc) is set accordingly before returning.
203811** The return value is undefined in both these cases.
203812**
203813** If no error occurs and non-zero (a match) is returned, the position-list
203814** of each phrase object is edited to contain only those entries that
203815** meet the constraint before returning.
203816*/
203817static int fts5ExprNearIsMatch(int *pRc, Fts5ExprNearset *pNear){
203818 Fts5NearTrimmer aStatic[4];
203819 Fts5NearTrimmer *a = aStatic;
203820 Fts5ExprPhrase **apPhrase = pNear->apPhrase;
203821
203822 int i;
203823 int rc = *pRc;
203824 int bMatch;
203825
203826 assert( pNear->nPhrase>1 );
203827
203828 /* If the aStatic[] array is not large enough, allocate a large array
203829 ** using sqlite3_malloc(). This approach could be improved upon. */
203830 if( pNear->nPhrase>ArraySize(aStatic) ){
203831 int nByte = sizeof(Fts5NearTrimmer) * pNear->nPhrase;
203832 a = (Fts5NearTrimmer*)sqlite3Fts5MallocZero(&rc, nByte);
203833 }else{
203834 memset(aStatic, 0, sizeof(aStatic));
203835 }
203836 if( rc!=SQLITE_OK ){
203837 *pRc = rc;
203838 return 0;
203839 }
203840
203841 /* Initialize a lookahead iterator for each phrase. After passing the
203842 ** buffer and buffer size to the lookaside-reader init function, zero
203843 ** the phrase poslist buffer. The new poslist for the phrase (containing
203844 ** the same entries as the original with some entries removed on account
203845 ** of the NEAR constraint) is written over the original even as it is
203846 ** being read. This is safe as the entries for the new poslist are a
203847 ** subset of the old, so it is not possible for data yet to be read to
203848 ** be overwritten. */
203849 for(i=0; i<pNear->nPhrase; i++){
203850 Fts5Buffer *pPoslist = &apPhrase[i]->poslist;
203851 fts5LookaheadReaderInit(pPoslist->p, pPoslist->n, &a[i].reader);
203852 pPoslist->n = 0;
203853 a[i].pOut = pPoslist;
203854 }
203855
203856 while( 1 ){
203857 int iAdv;
203858 i64 iMin;
203859 i64 iMax;
203860
203861 /* This block advances the phrase iterators until they point to a set of
203862 ** entries that together comprise a match. */
203863 iMax = a[0].reader.iPos;
203864 do {
203865 bMatch = 1;
203866 for(i=0; i<pNear->nPhrase; i++){
203867 Fts5LookaheadReader *pPos = &a[i].reader;
203868 iMin = iMax - pNear->apPhrase[i]->nTerm - pNear->nNear;
203869 if( pPos->iPos<iMin || pPos->iPos>iMax ){
203870 bMatch = 0;
203871 while( pPos->iPos<iMin ){
203872 if( fts5LookaheadReaderNext(pPos) ) goto ismatch_out;
203873 }
203874 if( pPos->iPos>iMax ) iMax = pPos->iPos;
203875 }
203876 }
203877 }while( bMatch==0 );
203878
203879 /* Add an entry to each output position list */
203880 for(i=0; i<pNear->nPhrase; i++){
203881 i64 iPos = a[i].reader.iPos;
203882 Fts5PoslistWriter *pWriter = &a[i].writer;
203883 if( a[i].pOut->n==0 || iPos!=pWriter->iPrev ){
203884 sqlite3Fts5PoslistWriterAppend(a[i].pOut, pWriter, iPos);
203885 }
203886 }
203887
203888 iAdv = 0;
203889 iMin = a[0].reader.iLookahead;
203890 for(i=0; i<pNear->nPhrase; i++){
203891 if( a[i].reader.iLookahead < iMin ){
203892 iMin = a[i].reader.iLookahead;
203893 iAdv = i;
203894 }
203895 }
203896 if( fts5LookaheadReaderNext(&a[iAdv].reader) ) goto ismatch_out;
203897 }
203898
203899 ismatch_out: {
203900 int bRet = a[0].pOut->n>0;
203901 *pRc = rc;
203902 if( a!=aStatic ) sqlite3_free(a);
203903 return bRet;
203904 }
203905}
203906
203907/*
203908** Advance iterator pIter until it points to a value equal to or laster
203909** than the initial value of *piLast. If this means the iterator points
203910** to a value laster than *piLast, update *piLast to the new lastest value.
203911**
203912** If the iterator reaches EOF, set *pbEof to true before returning. If
203913** an error occurs, set *pRc to an error code. If either *pbEof or *pRc
203914** are set, return a non-zero value. Otherwise, return zero.
203915*/
203916static int fts5ExprAdvanceto(
203917 Fts5IndexIter *pIter, /* Iterator to advance */
203918 int bDesc, /* True if iterator is "rowid DESC" */
203919 i64 *piLast, /* IN/OUT: Lastest rowid seen so far */
203920 int *pRc, /* OUT: Error code */
203921 int *pbEof /* OUT: Set to true if EOF */
203922){
203923 i64 iLast = *piLast;
203924 i64 iRowid;
203925
203926 iRowid = pIter->iRowid;
203927 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
203928 int rc = sqlite3Fts5IterNextFrom(pIter, iLast);
203929 if( rc || sqlite3Fts5IterEof(pIter) ){
203930 *pRc = rc;
203931 *pbEof = 1;
203932 return 1;
203933 }
203934 iRowid = pIter->iRowid;
203935 assert( (bDesc==0 && iRowid>=iLast) || (bDesc==1 && iRowid<=iLast) );
203936 }
203937 *piLast = iRowid;
203938
203939 return 0;
203940}
203941
203942static int fts5ExprSynonymAdvanceto(
203943 Fts5ExprTerm *pTerm, /* Term iterator to advance */
203944 int bDesc, /* True if iterator is "rowid DESC" */
203945 i64 *piLast, /* IN/OUT: Lastest rowid seen so far */
203946 int *pRc /* OUT: Error code */
203947){
203948 int rc = SQLITE_OK;
203949 i64 iLast = *piLast;
203950 Fts5ExprTerm *p;
203951 int bEof = 0;
203952
203953 for(p=pTerm; rc==SQLITE_OK && p; p=p->pSynonym){
203954 if( sqlite3Fts5IterEof(p->pIter)==0 ){
203955 i64 iRowid = p->pIter->iRowid;
203956 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
203957 rc = sqlite3Fts5IterNextFrom(p->pIter, iLast);
203958 }
203959 }
203960 }
203961
203962 if( rc!=SQLITE_OK ){
203963 *pRc = rc;
203964 bEof = 1;
203965 }else{
203966 *piLast = fts5ExprSynonymRowid(pTerm, bDesc, &bEof);
203967 }
203968 return bEof;
203969}
203970
203971
203972static int fts5ExprNearTest(
203973 int *pRc,
203974 Fts5Expr *pExpr, /* Expression that pNear is a part of */
203975 Fts5ExprNode *pNode /* The "NEAR" node (FTS5_STRING) */
203976){
203977 Fts5ExprNearset *pNear = pNode->pNear;
203978 int rc = *pRc;
203979
203980 if( pExpr->pConfig->eDetail!=FTS5_DETAIL_FULL ){
203981 Fts5ExprTerm *pTerm;
203982 Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
203983 pPhrase->poslist.n = 0;
203984 for(pTerm=&pPhrase->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
203985 Fts5IndexIter *pIter = pTerm->pIter;
203986 if( sqlite3Fts5IterEof(pIter)==0 ){
203987 if( pIter->iRowid==pNode->iRowid && pIter->nData>0 ){
203988 pPhrase->poslist.n = 1;
203989 }
203990 }
203991 }
203992 return pPhrase->poslist.n;
203993 }else{
203994 int i;
203995
203996 /* Check that each phrase in the nearset matches the current row.
203997 ** Populate the pPhrase->poslist buffers at the same time. If any
203998 ** phrase is not a match, break out of the loop early. */
203999 for(i=0; rc==SQLITE_OK && i<pNear->nPhrase; i++){
204000 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
204001 if( pPhrase->nTerm>1 || pPhrase->aTerm[0].pSynonym
204002 || pNear->pColset || pPhrase->aTerm[0].bFirst
204003 ){
204004 int bMatch = 0;
204005 rc = fts5ExprPhraseIsMatch(pNode, pPhrase, &bMatch);
204006 if( bMatch==0 ) break;
204007 }else{
204008 Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
204009 fts5BufferSet(&rc, &pPhrase->poslist, pIter->nData, pIter->pData);
204010 }
204011 }
204012
204013 *pRc = rc;
204014 if( i==pNear->nPhrase && (i==1 || fts5ExprNearIsMatch(pRc, pNear)) ){
204015 return 1;
204016 }
204017 return 0;
204018 }
204019}
204020
204021
204022/*
204023** Initialize all term iterators in the pNear object. If any term is found
204024** to match no documents at all, return immediately without initializing any
204025** further iterators.
204026**
204027** If an error occurs, return an SQLite error code. Otherwise, return
204028** SQLITE_OK. It is not considered an error if some term matches zero
204029** documents.
204030*/
204031static int fts5ExprNearInitAll(
204032 Fts5Expr *pExpr,
204033 Fts5ExprNode *pNode
204034){
204035 Fts5ExprNearset *pNear = pNode->pNear;
204036 int i;
204037
204038 assert( pNode->bNomatch==0 );
204039 for(i=0; i<pNear->nPhrase; i++){
204040 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
204041 if( pPhrase->nTerm==0 ){
204042 pNode->bEof = 1;
204043 return SQLITE_OK;
204044 }else{
204045 int j;
204046 for(j=0; j<pPhrase->nTerm; j++){
204047 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
204048 Fts5ExprTerm *p;
204049 int bHit = 0;
204050
204051 for(p=pTerm; p; p=p->pSynonym){
204052 int rc;
204053 if( p->pIter ){
204054 sqlite3Fts5IterClose(p->pIter);
204055 p->pIter = 0;
204056 }
204057 rc = sqlite3Fts5IndexQuery(
204058 pExpr->pIndex, p->zTerm, (int)strlen(p->zTerm),
204059 (pTerm->bPrefix ? FTS5INDEX_QUERY_PREFIX : 0) |
204060 (pExpr->bDesc ? FTS5INDEX_QUERY_DESC : 0),
204061 pNear->pColset,
204062 &p->pIter
204063 );
204064 assert( (rc==SQLITE_OK)==(p->pIter!=0) );
204065 if( rc!=SQLITE_OK ) return rc;
204066 if( 0==sqlite3Fts5IterEof(p->pIter) ){
204067 bHit = 1;
204068 }
204069 }
204070
204071 if( bHit==0 ){
204072 pNode->bEof = 1;
204073 return SQLITE_OK;
204074 }
204075 }
204076 }
204077 }
204078
204079 pNode->bEof = 0;
204080 return SQLITE_OK;
204081}
204082
204083/*
204084** If pExpr is an ASC iterator, this function returns a value with the
204085** same sign as:
204086**
204087** (iLhs - iRhs)
204088**
204089** Otherwise, if this is a DESC iterator, the opposite is returned:
204090**
204091** (iRhs - iLhs)
204092*/
204093static int fts5RowidCmp(
204094 Fts5Expr *pExpr,
204095 i64 iLhs,
204096 i64 iRhs
204097){
204098 assert( pExpr->bDesc==0 || pExpr->bDesc==1 );
204099 if( pExpr->bDesc==0 ){
204100 if( iLhs<iRhs ) return -1;
204101 return (iLhs > iRhs);
204102 }else{
204103 if( iLhs>iRhs ) return -1;
204104 return (iLhs < iRhs);
204105 }
204106}
204107
204108static void fts5ExprSetEof(Fts5ExprNode *pNode){
204109 int i;
204110 pNode->bEof = 1;
204111 pNode->bNomatch = 0;
204112 for(i=0; i<pNode->nChild; i++){
204113 fts5ExprSetEof(pNode->apChild[i]);
204114 }
204115}
204116
204117static void fts5ExprNodeZeroPoslist(Fts5ExprNode *pNode){
204118 if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){
204119 Fts5ExprNearset *pNear = pNode->pNear;
204120 int i;
204121 for(i=0; i<pNear->nPhrase; i++){
204122 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
204123 pPhrase->poslist.n = 0;
204124 }
204125 }else{
204126 int i;
204127 for(i=0; i<pNode->nChild; i++){
204128 fts5ExprNodeZeroPoslist(pNode->apChild[i]);
204129 }
204130 }
204131}
204132
204133
204134
204135/*
204136** Compare the values currently indicated by the two nodes as follows:
204137**
204138** res = (*p1) - (*p2)
204139**
204140** Nodes that point to values that come later in the iteration order are
204141** considered to be larger. Nodes at EOF are the largest of all.
204142**
204143** This means that if the iteration order is ASC, then numerically larger
204144** rowids are considered larger. Or if it is the default DESC, numerically
204145** smaller rowids are larger.
204146*/
204147static int fts5NodeCompare(
204148 Fts5Expr *pExpr,
204149 Fts5ExprNode *p1,
204150 Fts5ExprNode *p2
204151){
204152 if( p2->bEof ) return -1;
204153 if( p1->bEof ) return +1;
204154 return fts5RowidCmp(pExpr, p1->iRowid, p2->iRowid);
204155}
204156
204157/*
204158** All individual term iterators in pNear are guaranteed to be valid when
204159** this function is called. This function checks if all term iterators
204160** point to the same rowid, and if not, advances them until they do.
204161** If an EOF is reached before this happens, *pbEof is set to true before
204162** returning.
204163**
204164** SQLITE_OK is returned if an error occurs, or an SQLite error code
204165** otherwise. It is not considered an error code if an iterator reaches
204166** EOF.
204167*/
204168static int fts5ExprNodeTest_STRING(
204169 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
204170 Fts5ExprNode *pNode
204171){
204172 Fts5ExprNearset *pNear = pNode->pNear;
204173 Fts5ExprPhrase *pLeft = pNear->apPhrase[0];
204174 int rc = SQLITE_OK;
204175 i64 iLast; /* Lastest rowid any iterator points to */
204176 int i, j; /* Phrase and token index, respectively */
204177 int bMatch; /* True if all terms are at the same rowid */
204178 const int bDesc = pExpr->bDesc;
204179
204180 /* Check that this node should not be FTS5_TERM */
204181 assert( pNear->nPhrase>1
204182 || pNear->apPhrase[0]->nTerm>1
204183 || pNear->apPhrase[0]->aTerm[0].pSynonym
204184 || pNear->apPhrase[0]->aTerm[0].bFirst
204185 );
204186
204187 /* Initialize iLast, the "lastest" rowid any iterator points to. If the
204188 ** iterator skips through rowids in the default ascending order, this means
204189 ** the maximum rowid. Or, if the iterator is "ORDER BY rowid DESC", then it
204190 ** means the minimum rowid. */
204191 if( pLeft->aTerm[0].pSynonym ){
204192 iLast = fts5ExprSynonymRowid(&pLeft->aTerm[0], bDesc, 0);
204193 }else{
204194 iLast = pLeft->aTerm[0].pIter->iRowid;
204195 }
204196
204197 do {
204198 bMatch = 1;
204199 for(i=0; i<pNear->nPhrase; i++){
204200 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
204201 for(j=0; j<pPhrase->nTerm; j++){
204202 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
204203 if( pTerm->pSynonym ){
204204 i64 iRowid = fts5ExprSynonymRowid(pTerm, bDesc, 0);
204205 if( iRowid==iLast ) continue;
204206 bMatch = 0;
204207 if( fts5ExprSynonymAdvanceto(pTerm, bDesc, &iLast, &rc) ){
204208 pNode->bNomatch = 0;
204209 pNode->bEof = 1;
204210 return rc;
204211 }
204212 }else{
204213 Fts5IndexIter *pIter = pPhrase->aTerm[j].pIter;
204214 if( pIter->iRowid==iLast || pIter->bEof ) continue;
204215 bMatch = 0;
204216 if( fts5ExprAdvanceto(pIter, bDesc, &iLast, &rc, &pNode->bEof) ){
204217 return rc;
204218 }
204219 }
204220 }
204221 }
204222 }while( bMatch==0 );
204223
204224 pNode->iRowid = iLast;
204225 pNode->bNomatch = ((0==fts5ExprNearTest(&rc, pExpr, pNode)) && rc==SQLITE_OK);
204226 assert( pNode->bEof==0 || pNode->bNomatch==0 );
204227
204228 return rc;
204229}
204230
204231/*
204232** Advance the first term iterator in the first phrase of pNear. Set output
204233** variable *pbEof to true if it reaches EOF or if an error occurs.
204234**
204235** Return SQLITE_OK if successful, or an SQLite error code if an error
204236** occurs.
204237*/
204238static int fts5ExprNodeNext_STRING(
204239 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
204240 Fts5ExprNode *pNode, /* FTS5_STRING or FTS5_TERM node */
204241 int bFromValid,
204242 i64 iFrom
204243){
204244 Fts5ExprTerm *pTerm = &pNode->pNear->apPhrase[0]->aTerm[0];
204245 int rc = SQLITE_OK;
204246
204247 pNode->bNomatch = 0;
204248 if( pTerm->pSynonym ){
204249 int bEof = 1;
204250 Fts5ExprTerm *p;
204251
204252 /* Find the firstest rowid any synonym points to. */
204253 i64 iRowid = fts5ExprSynonymRowid(pTerm, pExpr->bDesc, 0);
204254
204255 /* Advance each iterator that currently points to iRowid. Or, if iFrom
204256 ** is valid - each iterator that points to a rowid before iFrom. */
204257 for(p=pTerm; p; p=p->pSynonym){
204258 if( sqlite3Fts5IterEof(p->pIter)==0 ){
204259 i64 ii = p->pIter->iRowid;
204260 if( ii==iRowid
204261 || (bFromValid && ii!=iFrom && (ii>iFrom)==pExpr->bDesc)
204262 ){
204263 if( bFromValid ){
204264 rc = sqlite3Fts5IterNextFrom(p->pIter, iFrom);
204265 }else{
204266 rc = sqlite3Fts5IterNext(p->pIter);
204267 }
204268 if( rc!=SQLITE_OK ) break;
204269 if( sqlite3Fts5IterEof(p->pIter)==0 ){
204270 bEof = 0;
204271 }
204272 }else{
204273 bEof = 0;
204274 }
204275 }
204276 }
204277
204278 /* Set the EOF flag if either all synonym iterators are at EOF or an
204279 ** error has occurred. */
204280 pNode->bEof = (rc || bEof);
204281 }else{
204282 Fts5IndexIter *pIter = pTerm->pIter;
204283
204284 assert( Fts5NodeIsString(pNode) );
204285 if( bFromValid ){
204286 rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
204287 }else{
204288 rc = sqlite3Fts5IterNext(pIter);
204289 }
204290
204291 pNode->bEof = (rc || sqlite3Fts5IterEof(pIter));
204292 }
204293
204294 if( pNode->bEof==0 ){
204295 assert( rc==SQLITE_OK );
204296 rc = fts5ExprNodeTest_STRING(pExpr, pNode);
204297 }
204298
204299 return rc;
204300}
204301
204302
204303static int fts5ExprNodeTest_TERM(
204304 Fts5Expr *pExpr, /* Expression that pNear is a part of */
204305 Fts5ExprNode *pNode /* The "NEAR" node (FTS5_TERM) */
204306){
204307 /* As this "NEAR" object is actually a single phrase that consists
204308 ** of a single term only, grab pointers into the poslist managed by the
204309 ** fts5_index.c iterator object. This is much faster than synthesizing
204310 ** a new poslist the way we have to for more complicated phrase or NEAR
204311 ** expressions. */
204312 Fts5ExprPhrase *pPhrase = pNode->pNear->apPhrase[0];
204313 Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
204314
204315 assert( pNode->eType==FTS5_TERM );
204316 assert( pNode->pNear->nPhrase==1 && pPhrase->nTerm==1 );
204317 assert( pPhrase->aTerm[0].pSynonym==0 );
204318
204319 pPhrase->poslist.n = pIter->nData;
204320 if( pExpr->pConfig->eDetail==FTS5_DETAIL_FULL ){
204321 pPhrase->poslist.p = (u8*)pIter->pData;
204322 }
204323 pNode->iRowid = pIter->iRowid;
204324 pNode->bNomatch = (pPhrase->poslist.n==0);
204325 return SQLITE_OK;
204326}
204327
204328/*
204329** xNext() method for a node of type FTS5_TERM.
204330*/
204331static int fts5ExprNodeNext_TERM(
204332 Fts5Expr *pExpr,
204333 Fts5ExprNode *pNode,
204334 int bFromValid,
204335 i64 iFrom
204336){
204337 int rc;
204338 Fts5IndexIter *pIter = pNode->pNear->apPhrase[0]->aTerm[0].pIter;
204339
204340 assert( pNode->bEof==0 );
204341 if( bFromValid ){
204342 rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
204343 }else{
204344 rc = sqlite3Fts5IterNext(pIter);
204345 }
204346 if( rc==SQLITE_OK && sqlite3Fts5IterEof(pIter)==0 ){
204347 rc = fts5ExprNodeTest_TERM(pExpr, pNode);
204348 }else{
204349 pNode->bEof = 1;
204350 pNode->bNomatch = 0;
204351 }
204352 return rc;
204353}
204354
204355static void fts5ExprNodeTest_OR(
204356 Fts5Expr *pExpr, /* Expression of which pNode is a part */
204357 Fts5ExprNode *pNode /* Expression node to test */
204358){
204359 Fts5ExprNode *pNext = pNode->apChild[0];
204360 int i;
204361
204362 for(i=1; i<pNode->nChild; i++){
204363 Fts5ExprNode *pChild = pNode->apChild[i];
204364 int cmp = fts5NodeCompare(pExpr, pNext, pChild);
204365 if( cmp>0 || (cmp==0 && pChild->bNomatch==0) ){
204366 pNext = pChild;
204367 }
204368 }
204369 pNode->iRowid = pNext->iRowid;
204370 pNode->bEof = pNext->bEof;
204371 pNode->bNomatch = pNext->bNomatch;
204372}
204373
204374static int fts5ExprNodeNext_OR(
204375 Fts5Expr *pExpr,
204376 Fts5ExprNode *pNode,
204377 int bFromValid,
204378 i64 iFrom
204379){
204380 int i;
204381 i64 iLast = pNode->iRowid;
204382
204383 for(i=0; i<pNode->nChild; i++){
204384 Fts5ExprNode *p1 = pNode->apChild[i];
204385 assert( p1->bEof || fts5RowidCmp(pExpr, p1->iRowid, iLast)>=0 );
204386 if( p1->bEof==0 ){
204387 if( (p1->iRowid==iLast)
204388 || (bFromValid && fts5RowidCmp(pExpr, p1->iRowid, iFrom)<0)
204389 ){
204390 int rc = fts5ExprNodeNext(pExpr, p1, bFromValid, iFrom);
204391 if( rc!=SQLITE_OK ){
204392 pNode->bNomatch = 0;
204393 return rc;
204394 }
204395 }
204396 }
204397 }
204398
204399 fts5ExprNodeTest_OR(pExpr, pNode);
204400 return SQLITE_OK;
204401}
204402
204403/*
204404** Argument pNode is an FTS5_AND node.
204405*/
204406static int fts5ExprNodeTest_AND(
204407 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
204408 Fts5ExprNode *pAnd /* FTS5_AND node to advance */
204409){
204410 int iChild;
204411 i64 iLast = pAnd->iRowid;
204412 int rc = SQLITE_OK;
204413 int bMatch;
204414
204415 assert( pAnd->bEof==0 );
204416 do {
204417 pAnd->bNomatch = 0;
204418 bMatch = 1;
204419 for(iChild=0; iChild<pAnd->nChild; iChild++){
204420 Fts5ExprNode *pChild = pAnd->apChild[iChild];
204421 int cmp = fts5RowidCmp(pExpr, iLast, pChild->iRowid);
204422 if( cmp>0 ){
204423 /* Advance pChild until it points to iLast or laster */
204424 rc = fts5ExprNodeNext(pExpr, pChild, 1, iLast);
204425 if( rc!=SQLITE_OK ){
204426 pAnd->bNomatch = 0;
204427 return rc;
204428 }
204429 }
204430
204431 /* If the child node is now at EOF, so is the parent AND node. Otherwise,
204432 ** the child node is guaranteed to have advanced at least as far as
204433 ** rowid iLast. So if it is not at exactly iLast, pChild->iRowid is the
204434 ** new lastest rowid seen so far. */
204435 assert( pChild->bEof || fts5RowidCmp(pExpr, iLast, pChild->iRowid)<=0 );
204436 if( pChild->bEof ){
204437 fts5ExprSetEof(pAnd);
204438 bMatch = 1;
204439 break;
204440 }else if( iLast!=pChild->iRowid ){
204441 bMatch = 0;
204442 iLast = pChild->iRowid;
204443 }
204444
204445 if( pChild->bNomatch ){
204446 pAnd->bNomatch = 1;
204447 }
204448 }
204449 }while( bMatch==0 );
204450
204451 if( pAnd->bNomatch && pAnd!=pExpr->pRoot ){
204452 fts5ExprNodeZeroPoslist(pAnd);
204453 }
204454 pAnd->iRowid = iLast;
204455 return SQLITE_OK;
204456}
204457
204458static int fts5ExprNodeNext_AND(
204459 Fts5Expr *pExpr,
204460 Fts5ExprNode *pNode,
204461 int bFromValid,
204462 i64 iFrom
204463){
204464 int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
204465 if( rc==SQLITE_OK ){
204466 rc = fts5ExprNodeTest_AND(pExpr, pNode);
204467 }else{
204468 pNode->bNomatch = 0;
204469 }
204470 return rc;
204471}
204472
204473static int fts5ExprNodeTest_NOT(
204474 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
204475 Fts5ExprNode *pNode /* FTS5_NOT node to advance */
204476){
204477 int rc = SQLITE_OK;
204478 Fts5ExprNode *p1 = pNode->apChild[0];
204479 Fts5ExprNode *p2 = pNode->apChild[1];
204480 assert( pNode->nChild==2 );
204481
204482 while( rc==SQLITE_OK && p1->bEof==0 ){
204483 int cmp = fts5NodeCompare(pExpr, p1, p2);
204484 if( cmp>0 ){
204485 rc = fts5ExprNodeNext(pExpr, p2, 1, p1->iRowid);
204486 cmp = fts5NodeCompare(pExpr, p1, p2);
204487 }
204488 assert( rc!=SQLITE_OK || cmp<=0 );
204489 if( cmp || p2->bNomatch ) break;
204490 rc = fts5ExprNodeNext(pExpr, p1, 0, 0);
204491 }
204492 pNode->bEof = p1->bEof;
204493 pNode->bNomatch = p1->bNomatch;
204494 pNode->iRowid = p1->iRowid;
204495 if( p1->bEof ){
204496 fts5ExprNodeZeroPoslist(p2);
204497 }
204498 return rc;
204499}
204500
204501static int fts5ExprNodeNext_NOT(
204502 Fts5Expr *pExpr,
204503 Fts5ExprNode *pNode,
204504 int bFromValid,
204505 i64 iFrom
204506){
204507 int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
204508 if( rc==SQLITE_OK ){
204509 rc = fts5ExprNodeTest_NOT(pExpr, pNode);
204510 }
204511 if( rc!=SQLITE_OK ){
204512 pNode->bNomatch = 0;
204513 }
204514 return rc;
204515}
204516
204517/*
204518** If pNode currently points to a match, this function returns SQLITE_OK
204519** without modifying it. Otherwise, pNode is advanced until it does point
204520** to a match or EOF is reached.
204521*/
204522static int fts5ExprNodeTest(
204523 Fts5Expr *pExpr, /* Expression of which pNode is a part */
204524 Fts5ExprNode *pNode /* Expression node to test */
204525){
204526 int rc = SQLITE_OK;
204527 if( pNode->bEof==0 ){
204528 switch( pNode->eType ){
204529
204530 case FTS5_STRING: {
204531 rc = fts5ExprNodeTest_STRING(pExpr, pNode);
204532 break;
204533 }
204534
204535 case FTS5_TERM: {
204536 rc = fts5ExprNodeTest_TERM(pExpr, pNode);
204537 break;
204538 }
204539
204540 case FTS5_AND: {
204541 rc = fts5ExprNodeTest_AND(pExpr, pNode);
204542 break;
204543 }
204544
204545 case FTS5_OR: {
204546 fts5ExprNodeTest_OR(pExpr, pNode);
204547 break;
204548 }
204549
204550 default: assert( pNode->eType==FTS5_NOT ); {
204551 rc = fts5ExprNodeTest_NOT(pExpr, pNode);
204552 break;
204553 }
204554 }
204555 }
204556 return rc;
204557}
204558
204559
204560/*
204561** Set node pNode, which is part of expression pExpr, to point to the first
204562** match. If there are no matches, set the Node.bEof flag to indicate EOF.
204563**
204564** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
204565** It is not an error if there are no matches.
204566*/
204567static int fts5ExprNodeFirst(Fts5Expr *pExpr, Fts5ExprNode *pNode){
204568 int rc = SQLITE_OK;
204569 pNode->bEof = 0;
204570 pNode->bNomatch = 0;
204571
204572 if( Fts5NodeIsString(pNode) ){
204573 /* Initialize all term iterators in the NEAR object. */
204574 rc = fts5ExprNearInitAll(pExpr, pNode);
204575 }else if( pNode->xNext==0 ){
204576 pNode->bEof = 1;
204577 }else{
204578 int i;
204579 int nEof = 0;
204580 for(i=0; i<pNode->nChild && rc==SQLITE_OK; i++){
204581 Fts5ExprNode *pChild = pNode->apChild[i];
204582 rc = fts5ExprNodeFirst(pExpr, pNode->apChild[i]);
204583 assert( pChild->bEof==0 || pChild->bEof==1 );
204584 nEof += pChild->bEof;
204585 }
204586 pNode->iRowid = pNode->apChild[0]->iRowid;
204587
204588 switch( pNode->eType ){
204589 case FTS5_AND:
204590 if( nEof>0 ) fts5ExprSetEof(pNode);
204591 break;
204592
204593 case FTS5_OR:
204594 if( pNode->nChild==nEof ) fts5ExprSetEof(pNode);
204595 break;
204596
204597 default:
204598 assert( pNode->eType==FTS5_NOT );
204599 pNode->bEof = pNode->apChild[0]->bEof;
204600 break;
204601 }
204602 }
204603
204604 if( rc==SQLITE_OK ){
204605 rc = fts5ExprNodeTest(pExpr, pNode);
204606 }
204607 return rc;
204608}
204609
204610
204611/*
204612** Begin iterating through the set of documents in index pIdx matched by
204613** the MATCH expression passed as the first argument. If the "bDesc"
204614** parameter is passed a non-zero value, iteration is in descending rowid
204615** order. Or, if it is zero, in ascending order.
204616**
204617** If iterating in ascending rowid order (bDesc==0), the first document
204618** visited is that with the smallest rowid that is larger than or equal
204619** to parameter iFirst. Or, if iterating in ascending order (bDesc==1),
204620** then the first document visited must have a rowid smaller than or
204621** equal to iFirst.
204622**
204623** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
204624** is not considered an error if the query does not match any documents.
204625*/
204626static int sqlite3Fts5ExprFirst(Fts5Expr *p, Fts5Index *pIdx, i64 iFirst, int bDesc){
204627 Fts5ExprNode *pRoot = p->pRoot;
204628 int rc; /* Return code */
204629
204630 p->pIndex = pIdx;
204631 p->bDesc = bDesc;
204632 rc = fts5ExprNodeFirst(p, pRoot);
204633
204634 /* If not at EOF but the current rowid occurs earlier than iFirst in
204635 ** the iteration order, move to document iFirst or later. */
204636 if( rc==SQLITE_OK
204637 && 0==pRoot->bEof
204638 && fts5RowidCmp(p, pRoot->iRowid, iFirst)<0
204639 ){
204640 rc = fts5ExprNodeNext(p, pRoot, 1, iFirst);
204641 }
204642
204643 /* If the iterator is not at a real match, skip forward until it is. */
204644 while( pRoot->bNomatch ){
204645 assert( pRoot->bEof==0 && rc==SQLITE_OK );
204646 rc = fts5ExprNodeNext(p, pRoot, 0, 0);
204647 }
204648 return rc;
204649}
204650
204651/*
204652** Move to the next document
204653**
204654** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
204655** is not considered an error if the query does not match any documents.
204656*/
204657static int sqlite3Fts5ExprNext(Fts5Expr *p, i64 iLast){
204658 int rc;
204659 Fts5ExprNode *pRoot = p->pRoot;
204660 assert( pRoot->bEof==0 && pRoot->bNomatch==0 );
204661 do {
204662 rc = fts5ExprNodeNext(p, pRoot, 0, 0);
204663 assert( pRoot->bNomatch==0 || (rc==SQLITE_OK && pRoot->bEof==0) );
204664 }while( pRoot->bNomatch );
204665 if( fts5RowidCmp(p, pRoot->iRowid, iLast)>0 ){
204666 pRoot->bEof = 1;
204667 }
204668 return rc;
204669}
204670
204671static int sqlite3Fts5ExprEof(Fts5Expr *p){
204672 return p->pRoot->bEof;
204673}
204674
204675static i64 sqlite3Fts5ExprRowid(Fts5Expr *p){
204676 return p->pRoot->iRowid;
204677}
204678
204679static int fts5ParseStringFromToken(Fts5Token *pToken, char **pz){
204680 int rc = SQLITE_OK;
204681 *pz = sqlite3Fts5Strndup(&rc, pToken->p, pToken->n);
204682 return rc;
204683}
204684
204685/*
204686** Free the phrase object passed as the only argument.
204687*/
204688static void fts5ExprPhraseFree(Fts5ExprPhrase *pPhrase){
204689 if( pPhrase ){
204690 int i;
204691 for(i=0; i<pPhrase->nTerm; i++){
204692 Fts5ExprTerm *pSyn;
204693 Fts5ExprTerm *pNext;
204694 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
204695 sqlite3_free(pTerm->zTerm);
204696 sqlite3Fts5IterClose(pTerm->pIter);
204697 for(pSyn=pTerm->pSynonym; pSyn; pSyn=pNext){
204698 pNext = pSyn->pSynonym;
204699 sqlite3Fts5IterClose(pSyn->pIter);
204700 fts5BufferFree((Fts5Buffer*)&pSyn[1]);
204701 sqlite3_free(pSyn);
204702 }
204703 }
204704 if( pPhrase->poslist.nSpace>0 ) fts5BufferFree(&pPhrase->poslist);
204705 sqlite3_free(pPhrase);
204706 }
204707}
204708
204709/*
204710** Set the "bFirst" flag on the first token of the phrase passed as the
204711** only argument.
204712*/
204713static void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase *pPhrase){
204714 if( pPhrase && pPhrase->nTerm ){
204715 pPhrase->aTerm[0].bFirst = 1;
204716 }
204717}
204718
204719/*
204720** If argument pNear is NULL, then a new Fts5ExprNearset object is allocated
204721** and populated with pPhrase. Or, if pNear is not NULL, phrase pPhrase is
204722** appended to it and the results returned.
204723**
204724** If an OOM error occurs, both the pNear and pPhrase objects are freed and
204725** NULL returned.
204726*/
204727static Fts5ExprNearset *sqlite3Fts5ParseNearset(
204728 Fts5Parse *pParse, /* Parse context */
204729 Fts5ExprNearset *pNear, /* Existing nearset, or NULL */
204730 Fts5ExprPhrase *pPhrase /* Recently parsed phrase */
204731){
204732 const int SZALLOC = 8;
204733 Fts5ExprNearset *pRet = 0;
204734
204735 if( pParse->rc==SQLITE_OK ){
204736 if( pPhrase==0 ){
204737 return pNear;
204738 }
204739 if( pNear==0 ){
204740 int nByte = sizeof(Fts5ExprNearset) + SZALLOC * sizeof(Fts5ExprPhrase*);
204741 pRet = sqlite3_malloc(nByte);
204742 if( pRet==0 ){
204743 pParse->rc = SQLITE_NOMEM;
204744 }else{
204745 memset(pRet, 0, nByte);
204746 }
204747 }else if( (pNear->nPhrase % SZALLOC)==0 ){
204748 int nNew = pNear->nPhrase + SZALLOC;
204749 int nByte = sizeof(Fts5ExprNearset) + nNew * sizeof(Fts5ExprPhrase*);
204750
204751 pRet = (Fts5ExprNearset*)sqlite3_realloc(pNear, nByte);
204752 if( pRet==0 ){
204753 pParse->rc = SQLITE_NOMEM;
204754 }
204755 }else{
204756 pRet = pNear;
204757 }
204758 }
204759
204760 if( pRet==0 ){
204761 assert( pParse->rc!=SQLITE_OK );
204762 sqlite3Fts5ParseNearsetFree(pNear);
204763 sqlite3Fts5ParsePhraseFree(pPhrase);
204764 }else{
204765 if( pRet->nPhrase>0 ){
204766 Fts5ExprPhrase *pLast = pRet->apPhrase[pRet->nPhrase-1];
204767 assert( pLast==pParse->apPhrase[pParse->nPhrase-2] );
204768 if( pPhrase->nTerm==0 ){
204769 fts5ExprPhraseFree(pPhrase);
204770 pRet->nPhrase--;
204771 pParse->nPhrase--;
204772 pPhrase = pLast;
204773 }else if( pLast->nTerm==0 ){
204774 fts5ExprPhraseFree(pLast);
204775 pParse->apPhrase[pParse->nPhrase-2] = pPhrase;
204776 pParse->nPhrase--;
204777 pRet->nPhrase--;
204778 }
204779 }
204780 pRet->apPhrase[pRet->nPhrase++] = pPhrase;
204781 }
204782 return pRet;
204783}
204784
204785typedef struct TokenCtx TokenCtx;
204786struct TokenCtx {
204787 Fts5ExprPhrase *pPhrase;
204788 int rc;
204789};
204790
204791/*
204792** Callback for tokenizing terms used by ParseTerm().
204793*/
204794static int fts5ParseTokenize(
204795 void *pContext, /* Pointer to Fts5InsertCtx object */
204796 int tflags, /* Mask of FTS5_TOKEN_* flags */
204797 const char *pToken, /* Buffer containing token */
204798 int nToken, /* Size of token in bytes */
204799 int iUnused1, /* Start offset of token */
204800 int iUnused2 /* End offset of token */
204801){
204802 int rc = SQLITE_OK;
204803 const int SZALLOC = 8;
204804 TokenCtx *pCtx = (TokenCtx*)pContext;
204805 Fts5ExprPhrase *pPhrase = pCtx->pPhrase;
204806
204807 UNUSED_PARAM2(iUnused1, iUnused2);
204808
204809 /* If an error has already occurred, this is a no-op */
204810 if( pCtx->rc!=SQLITE_OK ) return pCtx->rc;
204811 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
204812
204813 if( pPhrase && pPhrase->nTerm>0 && (tflags & FTS5_TOKEN_COLOCATED) ){
204814 Fts5ExprTerm *pSyn;
204815 int nByte = sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer) + nToken+1;
204816 pSyn = (Fts5ExprTerm*)sqlite3_malloc(nByte);
204817 if( pSyn==0 ){
204818 rc = SQLITE_NOMEM;
204819 }else{
204820 memset(pSyn, 0, nByte);
204821 pSyn->zTerm = ((char*)pSyn) + sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer);
204822 memcpy(pSyn->zTerm, pToken, nToken);
204823 pSyn->pSynonym = pPhrase->aTerm[pPhrase->nTerm-1].pSynonym;
204824 pPhrase->aTerm[pPhrase->nTerm-1].pSynonym = pSyn;
204825 }
204826 }else{
204827 Fts5ExprTerm *pTerm;
204828 if( pPhrase==0 || (pPhrase->nTerm % SZALLOC)==0 ){
204829 Fts5ExprPhrase *pNew;
204830 int nNew = SZALLOC + (pPhrase ? pPhrase->nTerm : 0);
204831
204832 pNew = (Fts5ExprPhrase*)sqlite3_realloc(pPhrase,
204833 sizeof(Fts5ExprPhrase) + sizeof(Fts5ExprTerm) * nNew
204834 );
204835 if( pNew==0 ){
204836 rc = SQLITE_NOMEM;
204837 }else{
204838 if( pPhrase==0 ) memset(pNew, 0, sizeof(Fts5ExprPhrase));
204839 pCtx->pPhrase = pPhrase = pNew;
204840 pNew->nTerm = nNew - SZALLOC;
204841 }
204842 }
204843
204844 if( rc==SQLITE_OK ){
204845 pTerm = &pPhrase->aTerm[pPhrase->nTerm++];
204846 memset(pTerm, 0, sizeof(Fts5ExprTerm));
204847 pTerm->zTerm = sqlite3Fts5Strndup(&rc, pToken, nToken);
204848 }
204849 }
204850
204851 pCtx->rc = rc;
204852 return rc;
204853}
204854
204855
204856/*
204857** Free the phrase object passed as the only argument.
204858*/
204859static void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase *pPhrase){
204860 fts5ExprPhraseFree(pPhrase);
204861}
204862
204863/*
204864** Free the phrase object passed as the second argument.
204865*/
204866static void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset *pNear){
204867 if( pNear ){
204868 int i;
204869 for(i=0; i<pNear->nPhrase; i++){
204870 fts5ExprPhraseFree(pNear->apPhrase[i]);
204871 }
204872 sqlite3_free(pNear->pColset);
204873 sqlite3_free(pNear);
204874 }
204875}
204876
204877static void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p){
204878 assert( pParse->pExpr==0 );
204879 pParse->pExpr = p;
204880}
204881
204882/*
204883** This function is called by the parser to process a string token. The
204884** string may or may not be quoted. In any case it is tokenized and a
204885** phrase object consisting of all tokens returned.
204886*/
204887static Fts5ExprPhrase *sqlite3Fts5ParseTerm(
204888 Fts5Parse *pParse, /* Parse context */
204889 Fts5ExprPhrase *pAppend, /* Phrase to append to */
204890 Fts5Token *pToken, /* String to tokenize */
204891 int bPrefix /* True if there is a trailing "*" */
204892){
204893 Fts5Config *pConfig = pParse->pConfig;
204894 TokenCtx sCtx; /* Context object passed to callback */
204895 int rc; /* Tokenize return code */
204896 char *z = 0;
204897
204898 memset(&sCtx, 0, sizeof(TokenCtx));
204899 sCtx.pPhrase = pAppend;
204900
204901 rc = fts5ParseStringFromToken(pToken, &z);
204902 if( rc==SQLITE_OK ){
204903 int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_PREFIX : 0);
204904 int n;
204905 sqlite3Fts5Dequote(z);
204906 n = (int)strlen(z);
204907 rc = sqlite3Fts5Tokenize(pConfig, flags, z, n, &sCtx, fts5ParseTokenize);
204908 }
204909 sqlite3_free(z);
204910 if( rc || (rc = sCtx.rc) ){
204911 pParse->rc = rc;
204912 fts5ExprPhraseFree(sCtx.pPhrase);
204913 sCtx.pPhrase = 0;
204914 }else{
204915
204916 if( pAppend==0 ){
204917 if( (pParse->nPhrase % 8)==0 ){
204918 int nByte = sizeof(Fts5ExprPhrase*) * (pParse->nPhrase + 8);
204919 Fts5ExprPhrase **apNew;
204920 apNew = (Fts5ExprPhrase**)sqlite3_realloc(pParse->apPhrase, nByte);
204921 if( apNew==0 ){
204922 pParse->rc = SQLITE_NOMEM;
204923 fts5ExprPhraseFree(sCtx.pPhrase);
204924 return 0;
204925 }
204926 pParse->apPhrase = apNew;
204927 }
204928 pParse->nPhrase++;
204929 }
204930
204931 if( sCtx.pPhrase==0 ){
204932 /* This happens when parsing a token or quoted phrase that contains
204933 ** no token characters at all. (e.g ... MATCH '""'). */
204934 sCtx.pPhrase = sqlite3Fts5MallocZero(&pParse->rc, sizeof(Fts5ExprPhrase));
204935 }else if( sCtx.pPhrase->nTerm ){
204936 sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = (u8)bPrefix;
204937 }
204938 pParse->apPhrase[pParse->nPhrase-1] = sCtx.pPhrase;
204939 }
204940
204941 return sCtx.pPhrase;
204942}
204943
204944/*
204945** Create a new FTS5 expression by cloning phrase iPhrase of the
204946** expression passed as the second argument.
204947*/
204948static int sqlite3Fts5ExprClonePhrase(
204949 Fts5Expr *pExpr,
204950 int iPhrase,
204951 Fts5Expr **ppNew
204952){
204953 int rc = SQLITE_OK; /* Return code */
204954 Fts5ExprPhrase *pOrig; /* The phrase extracted from pExpr */
204955 Fts5Expr *pNew = 0; /* Expression to return via *ppNew */
204956 TokenCtx sCtx = {0,0}; /* Context object for fts5ParseTokenize */
204957
204958 pOrig = pExpr->apExprPhrase[iPhrase];
204959 pNew = (Fts5Expr*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Expr));
204960 if( rc==SQLITE_OK ){
204961 pNew->apExprPhrase = (Fts5ExprPhrase**)sqlite3Fts5MallocZero(&rc,
204962 sizeof(Fts5ExprPhrase*));
204963 }
204964 if( rc==SQLITE_OK ){
204965 pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&rc,
204966 sizeof(Fts5ExprNode));
204967 }
204968 if( rc==SQLITE_OK ){
204969 pNew->pRoot->pNear = (Fts5ExprNearset*)sqlite3Fts5MallocZero(&rc,
204970 sizeof(Fts5ExprNearset) + sizeof(Fts5ExprPhrase*));
204971 }
204972 if( rc==SQLITE_OK ){
204973 Fts5Colset *pColsetOrig = pOrig->pNode->pNear->pColset;
204974 if( pColsetOrig ){
204975 int nByte = sizeof(Fts5Colset) + (pColsetOrig->nCol-1) * sizeof(int);
204976 Fts5Colset *pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&rc, nByte);
204977 if( pColset ){
204978 memcpy(pColset, pColsetOrig, nByte);
204979 }
204980 pNew->pRoot->pNear->pColset = pColset;
204981 }
204982 }
204983
204984 if( pOrig->nTerm ){
204985 int i; /* Used to iterate through phrase terms */
204986 for(i=0; rc==SQLITE_OK && i<pOrig->nTerm; i++){
204987 int tflags = 0;
204988 Fts5ExprTerm *p;
204989 for(p=&pOrig->aTerm[i]; p && rc==SQLITE_OK; p=p->pSynonym){
204990 const char *zTerm = p->zTerm;
204991 rc = fts5ParseTokenize((void*)&sCtx, tflags, zTerm, (int)strlen(zTerm),
204992 0, 0);
204993 tflags = FTS5_TOKEN_COLOCATED;
204994 }
204995 if( rc==SQLITE_OK ){
204996 sCtx.pPhrase->aTerm[i].bPrefix = pOrig->aTerm[i].bPrefix;
204997 sCtx.pPhrase->aTerm[i].bFirst = pOrig->aTerm[i].bFirst;
204998 }
204999 }
205000 }else{
205001 /* This happens when parsing a token or quoted phrase that contains
205002 ** no token characters at all. (e.g ... MATCH '""'). */
205003 sCtx.pPhrase = sqlite3Fts5MallocZero(&rc, sizeof(Fts5ExprPhrase));
205004 }
205005
205006 if( rc==SQLITE_OK ){
205007 /* All the allocations succeeded. Put the expression object together. */
205008 pNew->pIndex = pExpr->pIndex;
205009 pNew->pConfig = pExpr->pConfig;
205010 pNew->nPhrase = 1;
205011 pNew->apExprPhrase[0] = sCtx.pPhrase;
205012 pNew->pRoot->pNear->apPhrase[0] = sCtx.pPhrase;
205013 pNew->pRoot->pNear->nPhrase = 1;
205014 sCtx.pPhrase->pNode = pNew->pRoot;
205015
205016 if( pOrig->nTerm==1
205017 && pOrig->aTerm[0].pSynonym==0
205018 && pOrig->aTerm[0].bFirst==0
205019 ){
205020 pNew->pRoot->eType = FTS5_TERM;
205021 pNew->pRoot->xNext = fts5ExprNodeNext_TERM;
205022 }else{
205023 pNew->pRoot->eType = FTS5_STRING;
205024 pNew->pRoot->xNext = fts5ExprNodeNext_STRING;
205025 }
205026 }else{
205027 sqlite3Fts5ExprFree(pNew);
205028 fts5ExprPhraseFree(sCtx.pPhrase);
205029 pNew = 0;
205030 }
205031
205032 *ppNew = pNew;
205033 return rc;
205034}
205035
205036
205037/*
205038** Token pTok has appeared in a MATCH expression where the NEAR operator
205039** is expected. If token pTok does not contain "NEAR", store an error
205040** in the pParse object.
205041*/
205042static void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token *pTok){
205043 if( pTok->n!=4 || memcmp("NEAR", pTok->p, 4) ){
205044 sqlite3Fts5ParseError(
205045 pParse, "fts5: syntax error near \"%.*s\"", pTok->n, pTok->p
205046 );
205047 }
205048}
205049
205050static void sqlite3Fts5ParseSetDistance(
205051 Fts5Parse *pParse,
205052 Fts5ExprNearset *pNear,
205053 Fts5Token *p
205054){
205055 if( pNear ){
205056 int nNear = 0;
205057 int i;
205058 if( p->n ){
205059 for(i=0; i<p->n; i++){
205060 char c = (char)p->p[i];
205061 if( c<'0' || c>'9' ){
205062 sqlite3Fts5ParseError(
205063 pParse, "expected integer, got \"%.*s\"", p->n, p->p
205064 );
205065 return;
205066 }
205067 nNear = nNear * 10 + (p->p[i] - '0');
205068 }
205069 }else{
205070 nNear = FTS5_DEFAULT_NEARDIST;
205071 }
205072 pNear->nNear = nNear;
205073 }
205074}
205075
205076/*
205077** The second argument passed to this function may be NULL, or it may be
205078** an existing Fts5Colset object. This function returns a pointer to
205079** a new colset object containing the contents of (p) with new value column
205080** number iCol appended.
205081**
205082** If an OOM error occurs, store an error code in pParse and return NULL.
205083** The old colset object (if any) is not freed in this case.
205084*/
205085static Fts5Colset *fts5ParseColset(
205086 Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */
205087 Fts5Colset *p, /* Existing colset object */
205088 int iCol /* New column to add to colset object */
205089){
205090 int nCol = p ? p->nCol : 0; /* Num. columns already in colset object */
205091 Fts5Colset *pNew; /* New colset object to return */
205092
205093 assert( pParse->rc==SQLITE_OK );
205094 assert( iCol>=0 && iCol<pParse->pConfig->nCol );
205095
205096 pNew = sqlite3_realloc(p, sizeof(Fts5Colset) + sizeof(int)*nCol);
205097 if( pNew==0 ){
205098 pParse->rc = SQLITE_NOMEM;
205099 }else{
205100 int *aiCol = pNew->aiCol;
205101 int i, j;
205102 for(i=0; i<nCol; i++){
205103 if( aiCol[i]==iCol ) return pNew;
205104 if( aiCol[i]>iCol ) break;
205105 }
205106 for(j=nCol; j>i; j--){
205107 aiCol[j] = aiCol[j-1];
205108 }
205109 aiCol[i] = iCol;
205110 pNew->nCol = nCol+1;
205111
205112#ifndef NDEBUG
205113 /* Check that the array is in order and contains no duplicate entries. */
205114 for(i=1; i<pNew->nCol; i++) assert( pNew->aiCol[i]>pNew->aiCol[i-1] );
205115#endif
205116 }
205117
205118 return pNew;
205119}
205120
205121/*
205122** Allocate and return an Fts5Colset object specifying the inverse of
205123** the colset passed as the second argument. Free the colset passed
205124** as the second argument before returning.
205125*/
205126static Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse *pParse, Fts5Colset *p){
205127 Fts5Colset *pRet;
205128 int nCol = pParse->pConfig->nCol;
205129
205130 pRet = (Fts5Colset*)sqlite3Fts5MallocZero(&pParse->rc,
205131 sizeof(Fts5Colset) + sizeof(int)*nCol
205132 );
205133 if( pRet ){
205134 int i;
205135 int iOld = 0;
205136 for(i=0; i<nCol; i++){
205137 if( iOld>=p->nCol || p->aiCol[iOld]!=i ){
205138 pRet->aiCol[pRet->nCol++] = i;
205139 }else{
205140 iOld++;
205141 }
205142 }
205143 }
205144
205145 sqlite3_free(p);
205146 return pRet;
205147}
205148
205149static Fts5Colset *sqlite3Fts5ParseColset(
205150 Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */
205151 Fts5Colset *pColset, /* Existing colset object */
205152 Fts5Token *p
205153){
205154 Fts5Colset *pRet = 0;
205155 int iCol;
205156 char *z; /* Dequoted copy of token p */
205157
205158 z = sqlite3Fts5Strndup(&pParse->rc, p->p, p->n);
205159 if( pParse->rc==SQLITE_OK ){
205160 Fts5Config *pConfig = pParse->pConfig;
205161 sqlite3Fts5Dequote(z);
205162 for(iCol=0; iCol<pConfig->nCol; iCol++){
205163 if( 0==sqlite3_stricmp(pConfig->azCol[iCol], z) ) break;
205164 }
205165 if( iCol==pConfig->nCol ){
205166 sqlite3Fts5ParseError(pParse, "no such column: %s", z);
205167 }else{
205168 pRet = fts5ParseColset(pParse, pColset, iCol);
205169 }
205170 sqlite3_free(z);
205171 }
205172
205173 if( pRet==0 ){
205174 assert( pParse->rc!=SQLITE_OK );
205175 sqlite3_free(pColset);
205176 }
205177
205178 return pRet;
205179}
205180
205181/*
205182** If argument pOrig is NULL, or if (*pRc) is set to anything other than
205183** SQLITE_OK when this function is called, NULL is returned.
205184**
205185** Otherwise, a copy of (*pOrig) is made into memory obtained from
205186** sqlite3Fts5MallocZero() and a pointer to it returned. If the allocation
205187** fails, (*pRc) is set to SQLITE_NOMEM and NULL is returned.
205188*/
205189static Fts5Colset *fts5CloneColset(int *pRc, Fts5Colset *pOrig){
205190 Fts5Colset *pRet;
205191 if( pOrig ){
205192 int nByte = sizeof(Fts5Colset) + (pOrig->nCol-1) * sizeof(int);
205193 pRet = (Fts5Colset*)sqlite3Fts5MallocZero(pRc, nByte);
205194 if( pRet ){
205195 memcpy(pRet, pOrig, nByte);
205196 }
205197 }else{
205198 pRet = 0;
205199 }
205200 return pRet;
205201}
205202
205203/*
205204** Remove from colset pColset any columns that are not also in colset pMerge.
205205*/
205206static void fts5MergeColset(Fts5Colset *pColset, Fts5Colset *pMerge){
205207 int iIn = 0; /* Next input in pColset */
205208 int iMerge = 0; /* Next input in pMerge */
205209 int iOut = 0; /* Next output slot in pColset */
205210
205211 while( iIn<pColset->nCol && iMerge<pMerge->nCol ){
205212 int iDiff = pColset->aiCol[iIn] - pMerge->aiCol[iMerge];
205213 if( iDiff==0 ){
205214 pColset->aiCol[iOut++] = pMerge->aiCol[iMerge];
205215 iMerge++;
205216 iIn++;
205217 }else if( iDiff>0 ){
205218 iMerge++;
205219 }else{
205220 iIn++;
205221 }
205222 }
205223 pColset->nCol = iOut;
205224}
205225
205226/*
205227** Recursively apply colset pColset to expression node pNode and all of
205228** its decendents. If (*ppFree) is not NULL, it contains a spare copy
205229** of pColset. This function may use the spare copy and set (*ppFree) to
205230** zero, or it may create copies of pColset using fts5CloneColset().
205231*/
205232static void fts5ParseSetColset(
205233 Fts5Parse *pParse,
205234 Fts5ExprNode *pNode,
205235 Fts5Colset *pColset,
205236 Fts5Colset **ppFree
205237){
205238 if( pParse->rc==SQLITE_OK ){
205239 assert( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING
205240 || pNode->eType==FTS5_AND || pNode->eType==FTS5_OR
205241 || pNode->eType==FTS5_NOT || pNode->eType==FTS5_EOF
205242 );
205243 if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){
205244 Fts5ExprNearset *pNear = pNode->pNear;
205245 if( pNear->pColset ){
205246 fts5MergeColset(pNear->pColset, pColset);
205247 if( pNear->pColset->nCol==0 ){
205248 pNode->eType = FTS5_EOF;
205249 pNode->xNext = 0;
205250 }
205251 }else if( *ppFree ){
205252 pNear->pColset = pColset;
205253 *ppFree = 0;
205254 }else{
205255 pNear->pColset = fts5CloneColset(&pParse->rc, pColset);
205256 }
205257 }else{
205258 int i;
205259 assert( pNode->eType!=FTS5_EOF || pNode->nChild==0 );
205260 for(i=0; i<pNode->nChild; i++){
205261 fts5ParseSetColset(pParse, pNode->apChild[i], pColset, ppFree);
205262 }
205263 }
205264 }
205265}
205266
205267/*
205268** Apply colset pColset to expression node pExpr and all of its descendents.
205269*/
205270static void sqlite3Fts5ParseSetColset(
205271 Fts5Parse *pParse,
205272 Fts5ExprNode *pExpr,
205273 Fts5Colset *pColset
205274){
205275 Fts5Colset *pFree = pColset;
205276 if( pParse->pConfig->eDetail==FTS5_DETAIL_NONE ){
205277 pParse->rc = SQLITE_ERROR;
205278 pParse->zErr = sqlite3_mprintf(
205279 "fts5: column queries are not supported (detail=none)"
205280 );
205281 }else{
205282 fts5ParseSetColset(pParse, pExpr, pColset, &pFree);
205283 }
205284 sqlite3_free(pFree);
205285}
205286
205287static void fts5ExprAssignXNext(Fts5ExprNode *pNode){
205288 switch( pNode->eType ){
205289 case FTS5_STRING: {
205290 Fts5ExprNearset *pNear = pNode->pNear;
205291 if( pNear->nPhrase==1 && pNear->apPhrase[0]->nTerm==1
205292 && pNear->apPhrase[0]->aTerm[0].pSynonym==0
205293 && pNear->apPhrase[0]->aTerm[0].bFirst==0
205294 ){
205295 pNode->eType = FTS5_TERM;
205296 pNode->xNext = fts5ExprNodeNext_TERM;
205297 }else{
205298 pNode->xNext = fts5ExprNodeNext_STRING;
205299 }
205300 break;
205301 };
205302
205303 case FTS5_OR: {
205304 pNode->xNext = fts5ExprNodeNext_OR;
205305 break;
205306 };
205307
205308 case FTS5_AND: {
205309 pNode->xNext = fts5ExprNodeNext_AND;
205310 break;
205311 };
205312
205313 default: assert( pNode->eType==FTS5_NOT ); {
205314 pNode->xNext = fts5ExprNodeNext_NOT;
205315 break;
205316 };
205317 }
205318}
205319
205320static void fts5ExprAddChildren(Fts5ExprNode *p, Fts5ExprNode *pSub){
205321 if( p->eType!=FTS5_NOT && pSub->eType==p->eType ){
205322 int nByte = sizeof(Fts5ExprNode*) * pSub->nChild;
205323 memcpy(&p->apChild[p->nChild], pSub->apChild, nByte);
205324 p->nChild += pSub->nChild;
205325 sqlite3_free(pSub);
205326 }else{
205327 p->apChild[p->nChild++] = pSub;
205328 }
205329}
205330
205331/*
205332** Allocate and return a new expression object. If anything goes wrong (i.e.
205333** OOM error), leave an error code in pParse and return NULL.
205334*/
205335static Fts5ExprNode *sqlite3Fts5ParseNode(
205336 Fts5Parse *pParse, /* Parse context */
205337 int eType, /* FTS5_STRING, AND, OR or NOT */
205338 Fts5ExprNode *pLeft, /* Left hand child expression */
205339 Fts5ExprNode *pRight, /* Right hand child expression */
205340 Fts5ExprNearset *pNear /* For STRING expressions, the near cluster */
205341){
205342 Fts5ExprNode *pRet = 0;
205343
205344 if( pParse->rc==SQLITE_OK ){
205345 int nChild = 0; /* Number of children of returned node */
205346 int nByte; /* Bytes of space to allocate for this node */
205347
205348 assert( (eType!=FTS5_STRING && !pNear)
205349 || (eType==FTS5_STRING && !pLeft && !pRight)
205350 );
205351 if( eType==FTS5_STRING && pNear==0 ) return 0;
205352 if( eType!=FTS5_STRING && pLeft==0 ) return pRight;
205353 if( eType!=FTS5_STRING && pRight==0 ) return pLeft;
205354
205355 if( eType==FTS5_NOT ){
205356 nChild = 2;
205357 }else if( eType==FTS5_AND || eType==FTS5_OR ){
205358 nChild = 2;
205359 if( pLeft->eType==eType ) nChild += pLeft->nChild-1;
205360 if( pRight->eType==eType ) nChild += pRight->nChild-1;
205361 }
205362
205363 nByte = sizeof(Fts5ExprNode) + sizeof(Fts5ExprNode*)*(nChild-1);
205364 pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte);
205365
205366 if( pRet ){
205367 pRet->eType = eType;
205368 pRet->pNear = pNear;
205369 fts5ExprAssignXNext(pRet);
205370 if( eType==FTS5_STRING ){
205371 int iPhrase;
205372 for(iPhrase=0; iPhrase<pNear->nPhrase; iPhrase++){
205373 pNear->apPhrase[iPhrase]->pNode = pRet;
205374 if( pNear->apPhrase[iPhrase]->nTerm==0 ){
205375 pRet->xNext = 0;
205376 pRet->eType = FTS5_EOF;
205377 }
205378 }
205379
205380 if( pParse->pConfig->eDetail!=FTS5_DETAIL_FULL ){
205381 Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
205382 if( pNear->nPhrase!=1
205383 || pPhrase->nTerm>1
205384 || (pPhrase->nTerm>0 && pPhrase->aTerm[0].bFirst)
205385 ){
205386 assert( pParse->rc==SQLITE_OK );
205387 pParse->rc = SQLITE_ERROR;
205388 assert( pParse->zErr==0 );
205389 pParse->zErr = sqlite3_mprintf(
205390 "fts5: %s queries are not supported (detail!=full)",
205391 pNear->nPhrase==1 ? "phrase": "NEAR"
205392 );
205393 sqlite3_free(pRet);
205394 pRet = 0;
205395 }
205396 }
205397 }else{
205398 fts5ExprAddChildren(pRet, pLeft);
205399 fts5ExprAddChildren(pRet, pRight);
205400 }
205401 }
205402 }
205403
205404 if( pRet==0 ){
205405 assert( pParse->rc!=SQLITE_OK );
205406 sqlite3Fts5ParseNodeFree(pLeft);
205407 sqlite3Fts5ParseNodeFree(pRight);
205408 sqlite3Fts5ParseNearsetFree(pNear);
205409 }
205410 return pRet;
205411}
205412
205413static Fts5ExprNode *sqlite3Fts5ParseImplicitAnd(
205414 Fts5Parse *pParse, /* Parse context */
205415 Fts5ExprNode *pLeft, /* Left hand child expression */
205416 Fts5ExprNode *pRight /* Right hand child expression */
205417){
205418 Fts5ExprNode *pRet = 0;
205419 Fts5ExprNode *pPrev;
205420
205421 if( pParse->rc ){
205422 sqlite3Fts5ParseNodeFree(pLeft);
205423 sqlite3Fts5ParseNodeFree(pRight);
205424 }else{
205425
205426 assert( pLeft->eType==FTS5_STRING
205427 || pLeft->eType==FTS5_TERM
205428 || pLeft->eType==FTS5_EOF
205429 || pLeft->eType==FTS5_AND
205430 );
205431 assert( pRight->eType==FTS5_STRING
205432 || pRight->eType==FTS5_TERM
205433 || pRight->eType==FTS5_EOF
205434 );
205435
205436 if( pLeft->eType==FTS5_AND ){
205437 pPrev = pLeft->apChild[pLeft->nChild-1];
205438 }else{
205439 pPrev = pLeft;
205440 }
205441 assert( pPrev->eType==FTS5_STRING
205442 || pPrev->eType==FTS5_TERM
205443 || pPrev->eType==FTS5_EOF
205444 );
205445
205446 if( pRight->eType==FTS5_EOF ){
205447 assert( pParse->apPhrase[pParse->nPhrase-1]==pRight->pNear->apPhrase[0] );
205448 sqlite3Fts5ParseNodeFree(pRight);
205449 pRet = pLeft;
205450 pParse->nPhrase--;
205451 }
205452 else if( pPrev->eType==FTS5_EOF ){
205453 Fts5ExprPhrase **ap;
205454
205455 if( pPrev==pLeft ){
205456 pRet = pRight;
205457 }else{
205458 pLeft->apChild[pLeft->nChild-1] = pRight;
205459 pRet = pLeft;
205460 }
205461
205462 ap = &pParse->apPhrase[pParse->nPhrase-1-pRight->pNear->nPhrase];
205463 assert( ap[0]==pPrev->pNear->apPhrase[0] );
205464 memmove(ap, &ap[1], sizeof(Fts5ExprPhrase*)*pRight->pNear->nPhrase);
205465 pParse->nPhrase--;
205466
205467 sqlite3Fts5ParseNodeFree(pPrev);
205468 }
205469 else{
205470 pRet = sqlite3Fts5ParseNode(pParse, FTS5_AND, pLeft, pRight, 0);
205471 }
205472 }
205473
205474 return pRet;
205475}
205476
205477static char *fts5ExprTermPrint(Fts5ExprTerm *pTerm){
205478 int nByte = 0;
205479 Fts5ExprTerm *p;
205480 char *zQuoted;
205481
205482 /* Determine the maximum amount of space required. */
205483 for(p=pTerm; p; p=p->pSynonym){
205484 nByte += (int)strlen(pTerm->zTerm) * 2 + 3 + 2;
205485 }
205486 zQuoted = sqlite3_malloc(nByte);
205487
205488 if( zQuoted ){
205489 int i = 0;
205490 for(p=pTerm; p; p=p->pSynonym){
205491 char *zIn = p->zTerm;
205492 zQuoted[i++] = '"';
205493 while( *zIn ){
205494 if( *zIn=='"' ) zQuoted[i++] = '"';
205495 zQuoted[i++] = *zIn++;
205496 }
205497 zQuoted[i++] = '"';
205498 if( p->pSynonym ) zQuoted[i++] = '|';
205499 }
205500 if( pTerm->bPrefix ){
205501 zQuoted[i++] = ' ';
205502 zQuoted[i++] = '*';
205503 }
205504 zQuoted[i++] = '\0';
205505 }
205506 return zQuoted;
205507}
205508
205509static char *fts5PrintfAppend(char *zApp, const char *zFmt, ...){
205510 char *zNew;
205511 va_list ap;
205512 va_start(ap, zFmt);
205513 zNew = sqlite3_vmprintf(zFmt, ap);
205514 va_end(ap);
205515 if( zApp && zNew ){
205516 char *zNew2 = sqlite3_mprintf("%s%s", zApp, zNew);
205517 sqlite3_free(zNew);
205518 zNew = zNew2;
205519 }
205520 sqlite3_free(zApp);
205521 return zNew;
205522}
205523
205524/*
205525** Compose a tcl-readable representation of expression pExpr. Return a
205526** pointer to a buffer containing that representation. It is the
205527** responsibility of the caller to at some point free the buffer using
205528** sqlite3_free().
205529*/
205530static char *fts5ExprPrintTcl(
205531 Fts5Config *pConfig,
205532 const char *zNearsetCmd,
205533 Fts5ExprNode *pExpr
205534){
205535 char *zRet = 0;
205536 if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){
205537 Fts5ExprNearset *pNear = pExpr->pNear;
205538 int i;
205539 int iTerm;
205540
205541 zRet = fts5PrintfAppend(zRet, "%s ", zNearsetCmd);
205542 if( zRet==0 ) return 0;
205543 if( pNear->pColset ){
205544 int *aiCol = pNear->pColset->aiCol;
205545 int nCol = pNear->pColset->nCol;
205546 if( nCol==1 ){
205547 zRet = fts5PrintfAppend(zRet, "-col %d ", aiCol[0]);
205548 }else{
205549 zRet = fts5PrintfAppend(zRet, "-col {%d", aiCol[0]);
205550 for(i=1; i<pNear->pColset->nCol; i++){
205551 zRet = fts5PrintfAppend(zRet, " %d", aiCol[i]);
205552 }
205553 zRet = fts5PrintfAppend(zRet, "} ");
205554 }
205555 if( zRet==0 ) return 0;
205556 }
205557
205558 if( pNear->nPhrase>1 ){
205559 zRet = fts5PrintfAppend(zRet, "-near %d ", pNear->nNear);
205560 if( zRet==0 ) return 0;
205561 }
205562
205563 zRet = fts5PrintfAppend(zRet, "--");
205564 if( zRet==0 ) return 0;
205565
205566 for(i=0; i<pNear->nPhrase; i++){
205567 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
205568
205569 zRet = fts5PrintfAppend(zRet, " {");
205570 for(iTerm=0; zRet && iTerm<pPhrase->nTerm; iTerm++){
205571 char *zTerm = pPhrase->aTerm[iTerm].zTerm;
205572 zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" ", zTerm);
205573 if( pPhrase->aTerm[iTerm].bPrefix ){
205574 zRet = fts5PrintfAppend(zRet, "*");
205575 }
205576 }
205577
205578 if( zRet ) zRet = fts5PrintfAppend(zRet, "}");
205579 if( zRet==0 ) return 0;
205580 }
205581
205582 }else{
205583 char const *zOp = 0;
205584 int i;
205585 switch( pExpr->eType ){
205586 case FTS5_AND: zOp = "AND"; break;
205587 case FTS5_NOT: zOp = "NOT"; break;
205588 default:
205589 assert( pExpr->eType==FTS5_OR );
205590 zOp = "OR";
205591 break;
205592 }
205593
205594 zRet = sqlite3_mprintf("%s", zOp);
205595 for(i=0; zRet && i<pExpr->nChild; i++){
205596 char *z = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->apChild[i]);
205597 if( !z ){
205598 sqlite3_free(zRet);
205599 zRet = 0;
205600 }else{
205601 zRet = fts5PrintfAppend(zRet, " [%z]", z);
205602 }
205603 }
205604 }
205605
205606 return zRet;
205607}
205608
205609static char *fts5ExprPrint(Fts5Config *pConfig, Fts5ExprNode *pExpr){
205610 char *zRet = 0;
205611 if( pExpr->eType==0 ){
205612 return sqlite3_mprintf("\"\"");
205613 }else
205614 if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){
205615 Fts5ExprNearset *pNear = pExpr->pNear;
205616 int i;
205617 int iTerm;
205618
205619 if( pNear->pColset ){
205620 int iCol = pNear->pColset->aiCol[0];
205621 zRet = fts5PrintfAppend(zRet, "%s : ", pConfig->azCol[iCol]);
205622 if( zRet==0 ) return 0;
205623 }
205624
205625 if( pNear->nPhrase>1 ){
205626 zRet = fts5PrintfAppend(zRet, "NEAR(");
205627 if( zRet==0 ) return 0;
205628 }
205629
205630 for(i=0; i<pNear->nPhrase; i++){
205631 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
205632 if( i!=0 ){
205633 zRet = fts5PrintfAppend(zRet, " ");
205634 if( zRet==0 ) return 0;
205635 }
205636 for(iTerm=0; iTerm<pPhrase->nTerm; iTerm++){
205637 char *zTerm = fts5ExprTermPrint(&pPhrase->aTerm[iTerm]);
205638 if( zTerm ){
205639 zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" + ", zTerm);
205640 sqlite3_free(zTerm);
205641 }
205642 if( zTerm==0 || zRet==0 ){
205643 sqlite3_free(zRet);
205644 return 0;
205645 }
205646 }
205647 }
205648
205649 if( pNear->nPhrase>1 ){
205650 zRet = fts5PrintfAppend(zRet, ", %d)", pNear->nNear);
205651 if( zRet==0 ) return 0;
205652 }
205653
205654 }else{
205655 char const *zOp = 0;
205656 int i;
205657
205658 switch( pExpr->eType ){
205659 case FTS5_AND: zOp = " AND "; break;
205660 case FTS5_NOT: zOp = " NOT "; break;
205661 default:
205662 assert( pExpr->eType==FTS5_OR );
205663 zOp = " OR ";
205664 break;
205665 }
205666
205667 for(i=0; i<pExpr->nChild; i++){
205668 char *z = fts5ExprPrint(pConfig, pExpr->apChild[i]);
205669 if( z==0 ){
205670 sqlite3_free(zRet);
205671 zRet = 0;
205672 }else{
205673 int e = pExpr->apChild[i]->eType;
205674 int b = (e!=FTS5_STRING && e!=FTS5_TERM && e!=FTS5_EOF);
205675 zRet = fts5PrintfAppend(zRet, "%s%s%z%s",
205676 (i==0 ? "" : zOp),
205677 (b?"(":""), z, (b?")":"")
205678 );
205679 }
205680 if( zRet==0 ) break;
205681 }
205682 }
205683
205684 return zRet;
205685}
205686
205687/*
205688** The implementation of user-defined scalar functions fts5_expr() (bTcl==0)
205689** and fts5_expr_tcl() (bTcl!=0).
205690*/
205691static void fts5ExprFunction(
205692 sqlite3_context *pCtx, /* Function call context */
205693 int nArg, /* Number of args */
205694 sqlite3_value **apVal, /* Function arguments */
205695 int bTcl
205696){
205697 Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx);
205698 sqlite3 *db = sqlite3_context_db_handle(pCtx);
205699 const char *zExpr = 0;
205700 char *zErr = 0;
205701 Fts5Expr *pExpr = 0;
205702 int rc;
205703 int i;
205704
205705 const char **azConfig; /* Array of arguments for Fts5Config */
205706 const char *zNearsetCmd = "nearset";
205707 int nConfig; /* Size of azConfig[] */
205708 Fts5Config *pConfig = 0;
205709 int iArg = 1;
205710
205711 if( nArg<1 ){
205712 zErr = sqlite3_mprintf("wrong number of arguments to function %s",
205713 bTcl ? "fts5_expr_tcl" : "fts5_expr"
205714 );
205715 sqlite3_result_error(pCtx, zErr, -1);
205716 sqlite3_free(zErr);
205717 return;
205718 }
205719
205720 if( bTcl && nArg>1 ){
205721 zNearsetCmd = (const char*)sqlite3_value_text(apVal[1]);
205722 iArg = 2;
205723 }
205724
205725 nConfig = 3 + (nArg-iArg);
205726 azConfig = (const char**)sqlite3_malloc(sizeof(char*) * nConfig);
205727 if( azConfig==0 ){
205728 sqlite3_result_error_nomem(pCtx);
205729 return;
205730 }
205731 azConfig[0] = 0;
205732 azConfig[1] = "main";
205733 azConfig[2] = "tbl";
205734 for(i=3; iArg<nArg; iArg++){
205735 azConfig[i++] = (const char*)sqlite3_value_text(apVal[iArg]);
205736 }
205737
205738 zExpr = (const char*)sqlite3_value_text(apVal[0]);
205739
205740 rc = sqlite3Fts5ConfigParse(pGlobal, db, nConfig, azConfig, &pConfig, &zErr);
205741 if( rc==SQLITE_OK ){
205742 rc = sqlite3Fts5ExprNew(pConfig, pConfig->nCol, zExpr, &pExpr, &zErr);
205743 }
205744 if( rc==SQLITE_OK ){
205745 char *zText;
205746 if( pExpr->pRoot->xNext==0 ){
205747 zText = sqlite3_mprintf("");
205748 }else if( bTcl ){
205749 zText = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->pRoot);
205750 }else{
205751 zText = fts5ExprPrint(pConfig, pExpr->pRoot);
205752 }
205753 if( zText==0 ){
205754 rc = SQLITE_NOMEM;
205755 }else{
205756 sqlite3_result_text(pCtx, zText, -1, SQLITE_TRANSIENT);
205757 sqlite3_free(zText);
205758 }
205759 }
205760
205761 if( rc!=SQLITE_OK ){
205762 if( zErr ){
205763 sqlite3_result_error(pCtx, zErr, -1);
205764 sqlite3_free(zErr);
205765 }else{
205766 sqlite3_result_error_code(pCtx, rc);
205767 }
205768 }
205769 sqlite3_free((void *)azConfig);
205770 sqlite3Fts5ConfigFree(pConfig);
205771 sqlite3Fts5ExprFree(pExpr);
205772}
205773
205774static void fts5ExprFunctionHr(
205775 sqlite3_context *pCtx, /* Function call context */
205776 int nArg, /* Number of args */
205777 sqlite3_value **apVal /* Function arguments */
205778){
205779 fts5ExprFunction(pCtx, nArg, apVal, 0);
205780}
205781static void fts5ExprFunctionTcl(
205782 sqlite3_context *pCtx, /* Function call context */
205783 int nArg, /* Number of args */
205784 sqlite3_value **apVal /* Function arguments */
205785){
205786 fts5ExprFunction(pCtx, nArg, apVal, 1);
205787}
205788
205789/*
205790** The implementation of an SQLite user-defined-function that accepts a
205791** single integer as an argument. If the integer is an alpha-numeric
205792** unicode code point, 1 is returned. Otherwise 0.
205793*/
205794static void fts5ExprIsAlnum(
205795 sqlite3_context *pCtx, /* Function call context */
205796 int nArg, /* Number of args */
205797 sqlite3_value **apVal /* Function arguments */
205798){
205799 int iCode;
205800 u8 aArr[32];
205801 if( nArg!=1 ){
205802 sqlite3_result_error(pCtx,
205803 "wrong number of arguments to function fts5_isalnum", -1
205804 );
205805 return;
205806 }
205807 memset(aArr, 0, sizeof(aArr));
205808 sqlite3Fts5UnicodeCatParse("L*", aArr);
205809 sqlite3Fts5UnicodeCatParse("N*", aArr);
205810 sqlite3Fts5UnicodeCatParse("Co", aArr);
205811 iCode = sqlite3_value_int(apVal[0]);
205812 sqlite3_result_int(pCtx, aArr[sqlite3Fts5UnicodeCategory(iCode)]);
205813}
205814
205815static void fts5ExprFold(
205816 sqlite3_context *pCtx, /* Function call context */
205817 int nArg, /* Number of args */
205818 sqlite3_value **apVal /* Function arguments */
205819){
205820 if( nArg!=1 && nArg!=2 ){
205821 sqlite3_result_error(pCtx,
205822 "wrong number of arguments to function fts5_fold", -1
205823 );
205824 }else{
205825 int iCode;
205826 int bRemoveDiacritics = 0;
205827 iCode = sqlite3_value_int(apVal[0]);
205828 if( nArg==2 ) bRemoveDiacritics = sqlite3_value_int(apVal[1]);
205829 sqlite3_result_int(pCtx, sqlite3Fts5UnicodeFold(iCode, bRemoveDiacritics));
205830 }
205831}
205832
205833/*
205834** This is called during initialization to register the fts5_expr() scalar
205835** UDF with the SQLite handle passed as the only argument.
205836*/
205837static int sqlite3Fts5ExprInit(Fts5Global *pGlobal, sqlite3 *db){
205838 struct Fts5ExprFunc {
205839 const char *z;
205840 void (*x)(sqlite3_context*,int,sqlite3_value**);
205841 } aFunc[] = {
205842 { "fts5_expr", fts5ExprFunctionHr },
205843 { "fts5_expr_tcl", fts5ExprFunctionTcl },
205844 { "fts5_isalnum", fts5ExprIsAlnum },
205845 { "fts5_fold", fts5ExprFold },
205846 };
205847 int i;
205848 int rc = SQLITE_OK;
205849 void *pCtx = (void*)pGlobal;
205850
205851 for(i=0; rc==SQLITE_OK && i<ArraySize(aFunc); i++){
205852 struct Fts5ExprFunc *p = &aFunc[i];
205853 rc = sqlite3_create_function(db, p->z, -1, SQLITE_UTF8, pCtx, p->x, 0, 0);
205854 }
205855
205856 /* Avoid warnings indicating that sqlite3Fts5ParserTrace() and
205857 ** sqlite3Fts5ParserFallback() are unused */
205858#ifndef NDEBUG
205859 (void)sqlite3Fts5ParserTrace;
205860#endif
205861 (void)sqlite3Fts5ParserFallback;
205862
205863 return rc;
205864}
205865
205866/*
205867** Return the number of phrases in expression pExpr.
205868*/
205869static int sqlite3Fts5ExprPhraseCount(Fts5Expr *pExpr){
205870 return (pExpr ? pExpr->nPhrase : 0);
205871}
205872
205873/*
205874** Return the number of terms in the iPhrase'th phrase in pExpr.
205875*/
205876static int sqlite3Fts5ExprPhraseSize(Fts5Expr *pExpr, int iPhrase){
205877 if( iPhrase<0 || iPhrase>=pExpr->nPhrase ) return 0;
205878 return pExpr->apExprPhrase[iPhrase]->nTerm;
205879}
205880
205881/*
205882** This function is used to access the current position list for phrase
205883** iPhrase.
205884*/
205885static int sqlite3Fts5ExprPoslist(Fts5Expr *pExpr, int iPhrase, const u8 **pa){
205886 int nRet;
205887 Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
205888 Fts5ExprNode *pNode = pPhrase->pNode;
205889 if( pNode->bEof==0 && pNode->iRowid==pExpr->pRoot->iRowid ){
205890 *pa = pPhrase->poslist.p;
205891 nRet = pPhrase->poslist.n;
205892 }else{
205893 *pa = 0;
205894 nRet = 0;
205895 }
205896 return nRet;
205897}
205898
205899struct Fts5PoslistPopulator {
205900 Fts5PoslistWriter writer;
205901 int bOk; /* True if ok to populate */
205902 int bMiss;
205903};
205904
205905static Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr *pExpr, int bLive){
205906 Fts5PoslistPopulator *pRet;
205907 pRet = sqlite3_malloc(sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
205908 if( pRet ){
205909 int i;
205910 memset(pRet, 0, sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
205911 for(i=0; i<pExpr->nPhrase; i++){
205912 Fts5Buffer *pBuf = &pExpr->apExprPhrase[i]->poslist;
205913 Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
205914 assert( pExpr->apExprPhrase[i]->nTerm==1 );
205915 if( bLive &&
205916 (pBuf->n==0 || pNode->iRowid!=pExpr->pRoot->iRowid || pNode->bEof)
205917 ){
205918 pRet[i].bMiss = 1;
205919 }else{
205920 pBuf->n = 0;
205921 }
205922 }
205923 }
205924 return pRet;
205925}
205926
205927struct Fts5ExprCtx {
205928 Fts5Expr *pExpr;
205929 Fts5PoslistPopulator *aPopulator;
205930 i64 iOff;
205931};
205932typedef struct Fts5ExprCtx Fts5ExprCtx;
205933
205934/*
205935** TODO: Make this more efficient!
205936*/
205937static int fts5ExprColsetTest(Fts5Colset *pColset, int iCol){
205938 int i;
205939 for(i=0; i<pColset->nCol; i++){
205940 if( pColset->aiCol[i]==iCol ) return 1;
205941 }
205942 return 0;
205943}
205944
205945static int fts5ExprPopulatePoslistsCb(
205946 void *pCtx, /* Copy of 2nd argument to xTokenize() */
205947 int tflags, /* Mask of FTS5_TOKEN_* flags */
205948 const char *pToken, /* Pointer to buffer containing token */
205949 int nToken, /* Size of token in bytes */
205950 int iUnused1, /* Byte offset of token within input text */
205951 int iUnused2 /* Byte offset of end of token within input text */
205952){
205953 Fts5ExprCtx *p = (Fts5ExprCtx*)pCtx;
205954 Fts5Expr *pExpr = p->pExpr;
205955 int i;
205956
205957 UNUSED_PARAM2(iUnused1, iUnused2);
205958
205959 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
205960 if( (tflags & FTS5_TOKEN_COLOCATED)==0 ) p->iOff++;
205961 for(i=0; i<pExpr->nPhrase; i++){
205962 Fts5ExprTerm *pTerm;
205963 if( p->aPopulator[i].bOk==0 ) continue;
205964 for(pTerm=&pExpr->apExprPhrase[i]->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
205965 int nTerm = (int)strlen(pTerm->zTerm);
205966 if( (nTerm==nToken || (nTerm<nToken && pTerm->bPrefix))
205967 && memcmp(pTerm->zTerm, pToken, nTerm)==0
205968 ){
205969 int rc = sqlite3Fts5PoslistWriterAppend(
205970 &pExpr->apExprPhrase[i]->poslist, &p->aPopulator[i].writer, p->iOff
205971 );
205972 if( rc ) return rc;
205973 break;
205974 }
205975 }
205976 }
205977 return SQLITE_OK;
205978}
205979
205980static int sqlite3Fts5ExprPopulatePoslists(
205981 Fts5Config *pConfig,
205982 Fts5Expr *pExpr,
205983 Fts5PoslistPopulator *aPopulator,
205984 int iCol,
205985 const char *z, int n
205986){
205987 int i;
205988 Fts5ExprCtx sCtx;
205989 sCtx.pExpr = pExpr;
205990 sCtx.aPopulator = aPopulator;
205991 sCtx.iOff = (((i64)iCol) << 32) - 1;
205992
205993 for(i=0; i<pExpr->nPhrase; i++){
205994 Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
205995 Fts5Colset *pColset = pNode->pNear->pColset;
205996 if( (pColset && 0==fts5ExprColsetTest(pColset, iCol))
205997 || aPopulator[i].bMiss
205998 ){
205999 aPopulator[i].bOk = 0;
206000 }else{
206001 aPopulator[i].bOk = 1;
206002 }
206003 }
206004
206005 return sqlite3Fts5Tokenize(pConfig,
206006 FTS5_TOKENIZE_DOCUMENT, z, n, (void*)&sCtx, fts5ExprPopulatePoslistsCb
206007 );
206008}
206009
206010static void fts5ExprClearPoslists(Fts5ExprNode *pNode){
206011 if( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING ){
206012 pNode->pNear->apPhrase[0]->poslist.n = 0;
206013 }else{
206014 int i;
206015 for(i=0; i<pNode->nChild; i++){
206016 fts5ExprClearPoslists(pNode->apChild[i]);
206017 }
206018 }
206019}
206020
206021static int fts5ExprCheckPoslists(Fts5ExprNode *pNode, i64 iRowid){
206022 pNode->iRowid = iRowid;
206023 pNode->bEof = 0;
206024 switch( pNode->eType ){
206025 case FTS5_TERM:
206026 case FTS5_STRING:
206027 return (pNode->pNear->apPhrase[0]->poslist.n>0);
206028
206029 case FTS5_AND: {
206030 int i;
206031 for(i=0; i<pNode->nChild; i++){
206032 if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid)==0 ){
206033 fts5ExprClearPoslists(pNode);
206034 return 0;
206035 }
206036 }
206037 break;
206038 }
206039
206040 case FTS5_OR: {
206041 int i;
206042 int bRet = 0;
206043 for(i=0; i<pNode->nChild; i++){
206044 if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid) ){
206045 bRet = 1;
206046 }
206047 }
206048 return bRet;
206049 }
206050
206051 default: {
206052 assert( pNode->eType==FTS5_NOT );
206053 if( 0==fts5ExprCheckPoslists(pNode->apChild[0], iRowid)
206054 || 0!=fts5ExprCheckPoslists(pNode->apChild[1], iRowid)
206055 ){
206056 fts5ExprClearPoslists(pNode);
206057 return 0;
206058 }
206059 break;
206060 }
206061 }
206062 return 1;
206063}
206064
206065static void sqlite3Fts5ExprCheckPoslists(Fts5Expr *pExpr, i64 iRowid){
206066 fts5ExprCheckPoslists(pExpr->pRoot, iRowid);
206067}
206068
206069/*
206070** This function is only called for detail=columns tables.
206071*/
206072static int sqlite3Fts5ExprPhraseCollist(
206073 Fts5Expr *pExpr,
206074 int iPhrase,
206075 const u8 **ppCollist,
206076 int *pnCollist
206077){
206078 Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
206079 Fts5ExprNode *pNode = pPhrase->pNode;
206080 int rc = SQLITE_OK;
206081
206082 assert( iPhrase>=0 && iPhrase<pExpr->nPhrase );
206083 assert( pExpr->pConfig->eDetail==FTS5_DETAIL_COLUMNS );
206084
206085 if( pNode->bEof==0
206086 && pNode->iRowid==pExpr->pRoot->iRowid
206087 && pPhrase->poslist.n>0
206088 ){
206089 Fts5ExprTerm *pTerm = &pPhrase->aTerm[0];
206090 if( pTerm->pSynonym ){
206091 Fts5Buffer *pBuf = (Fts5Buffer*)&pTerm->pSynonym[1];
206092 rc = fts5ExprSynonymList(
206093 pTerm, pNode->iRowid, pBuf, (u8**)ppCollist, pnCollist
206094 );
206095 }else{
206096 *ppCollist = pPhrase->aTerm[0].pIter->pData;
206097 *pnCollist = pPhrase->aTerm[0].pIter->nData;
206098 }
206099 }else{
206100 *ppCollist = 0;
206101 *pnCollist = 0;
206102 }
206103
206104 return rc;
206105}
206106
206107
206108/*
206109** 2014 August 11
206110**
206111** The author disclaims copyright to this source code. In place of
206112** a legal notice, here is a blessing:
206113**
206114** May you do good and not evil.
206115** May you find forgiveness for yourself and forgive others.
206116** May you share freely, never taking more than you give.
206117**
206118******************************************************************************
206119**
206120*/
206121
206122
206123
206124/* #include "fts5Int.h" */
206125
206126typedef struct Fts5HashEntry Fts5HashEntry;
206127
206128/*
206129** This file contains the implementation of an in-memory hash table used
206130** to accumuluate "term -> doclist" content before it is flused to a level-0
206131** segment.
206132*/
206133
206134
206135struct Fts5Hash {
206136 int eDetail; /* Copy of Fts5Config.eDetail */
206137 int *pnByte; /* Pointer to bytes counter */
206138 int nEntry; /* Number of entries currently in hash */
206139 int nSlot; /* Size of aSlot[] array */
206140 Fts5HashEntry *pScan; /* Current ordered scan item */
206141 Fts5HashEntry **aSlot; /* Array of hash slots */
206142};
206143
206144/*
206145** Each entry in the hash table is represented by an object of the
206146** following type. Each object, its key (a nul-terminated string) and
206147** its current data are stored in a single memory allocation. The
206148** key immediately follows the object in memory. The position list
206149** data immediately follows the key data in memory.
206150**
206151** The data that follows the key is in a similar, but not identical format
206152** to the doclist data stored in the database. It is:
206153**
206154** * Rowid, as a varint
206155** * Position list, without 0x00 terminator.
206156** * Size of previous position list and rowid, as a 4 byte
206157** big-endian integer.
206158**
206159** iRowidOff:
206160** Offset of last rowid written to data area. Relative to first byte of
206161** structure.
206162**
206163** nData:
206164** Bytes of data written since iRowidOff.
206165*/
206166struct Fts5HashEntry {
206167 Fts5HashEntry *pHashNext; /* Next hash entry with same hash-key */
206168 Fts5HashEntry *pScanNext; /* Next entry in sorted order */
206169
206170 int nAlloc; /* Total size of allocation */
206171 int iSzPoslist; /* Offset of space for 4-byte poslist size */
206172 int nData; /* Total bytes of data (incl. structure) */
206173 int nKey; /* Length of key in bytes */
206174 u8 bDel; /* Set delete-flag @ iSzPoslist */
206175 u8 bContent; /* Set content-flag (detail=none mode) */
206176 i16 iCol; /* Column of last value written */
206177 int iPos; /* Position of last value written */
206178 i64 iRowid; /* Rowid of last value written */
206179};
206180
206181/*
206182** Eqivalent to:
206183**
206184** char *fts5EntryKey(Fts5HashEntry *pEntry){ return zKey; }
206185*/
206186#define fts5EntryKey(p) ( ((char *)(&(p)[1])) )
206187
206188
206189/*
206190** Allocate a new hash table.
206191*/
206192static int sqlite3Fts5HashNew(Fts5Config *pConfig, Fts5Hash **ppNew, int *pnByte){
206193 int rc = SQLITE_OK;
206194 Fts5Hash *pNew;
206195
206196 *ppNew = pNew = (Fts5Hash*)sqlite3_malloc(sizeof(Fts5Hash));
206197 if( pNew==0 ){
206198 rc = SQLITE_NOMEM;
206199 }else{
206200 int nByte;
206201 memset(pNew, 0, sizeof(Fts5Hash));
206202 pNew->pnByte = pnByte;
206203 pNew->eDetail = pConfig->eDetail;
206204
206205 pNew->nSlot = 1024;
206206 nByte = sizeof(Fts5HashEntry*) * pNew->nSlot;
206207 pNew->aSlot = (Fts5HashEntry**)sqlite3_malloc(nByte);
206208 if( pNew->aSlot==0 ){
206209 sqlite3_free(pNew);
206210 *ppNew = 0;
206211 rc = SQLITE_NOMEM;
206212 }else{
206213 memset(pNew->aSlot, 0, nByte);
206214 }
206215 }
206216 return rc;
206217}
206218
206219/*
206220** Free a hash table object.
206221*/
206222static void sqlite3Fts5HashFree(Fts5Hash *pHash){
206223 if( pHash ){
206224 sqlite3Fts5HashClear(pHash);
206225 sqlite3_free(pHash->aSlot);
206226 sqlite3_free(pHash);
206227 }
206228}
206229
206230/*
206231** Empty (but do not delete) a hash table.
206232*/
206233static void sqlite3Fts5HashClear(Fts5Hash *pHash){
206234 int i;
206235 for(i=0; i<pHash->nSlot; i++){
206236 Fts5HashEntry *pNext;
206237 Fts5HashEntry *pSlot;
206238 for(pSlot=pHash->aSlot[i]; pSlot; pSlot=pNext){
206239 pNext = pSlot->pHashNext;
206240 sqlite3_free(pSlot);
206241 }
206242 }
206243 memset(pHash->aSlot, 0, pHash->nSlot * sizeof(Fts5HashEntry*));
206244 pHash->nEntry = 0;
206245}
206246
206247static unsigned int fts5HashKey(int nSlot, const u8 *p, int n){
206248 int i;
206249 unsigned int h = 13;
206250 for(i=n-1; i>=0; i--){
206251 h = (h << 3) ^ h ^ p[i];
206252 }
206253 return (h % nSlot);
206254}
206255
206256static unsigned int fts5HashKey2(int nSlot, u8 b, const u8 *p, int n){
206257 int i;
206258 unsigned int h = 13;
206259 for(i=n-1; i>=0; i--){
206260 h = (h << 3) ^ h ^ p[i];
206261 }
206262 h = (h << 3) ^ h ^ b;
206263 return (h % nSlot);
206264}
206265
206266/*
206267** Resize the hash table by doubling the number of slots.
206268*/
206269static int fts5HashResize(Fts5Hash *pHash){
206270 int nNew = pHash->nSlot*2;
206271 int i;
206272 Fts5HashEntry **apNew;
206273 Fts5HashEntry **apOld = pHash->aSlot;
206274
206275 apNew = (Fts5HashEntry**)sqlite3_malloc(nNew*sizeof(Fts5HashEntry*));
206276 if( !apNew ) return SQLITE_NOMEM;
206277 memset(apNew, 0, nNew*sizeof(Fts5HashEntry*));
206278
206279 for(i=0; i<pHash->nSlot; i++){
206280 while( apOld[i] ){
206281 unsigned int iHash;
206282 Fts5HashEntry *p = apOld[i];
206283 apOld[i] = p->pHashNext;
206284 iHash = fts5HashKey(nNew, (u8*)fts5EntryKey(p),
206285 (int)strlen(fts5EntryKey(p)));
206286 p->pHashNext = apNew[iHash];
206287 apNew[iHash] = p;
206288 }
206289 }
206290
206291 sqlite3_free(apOld);
206292 pHash->nSlot = nNew;
206293 pHash->aSlot = apNew;
206294 return SQLITE_OK;
206295}
206296
206297static void fts5HashAddPoslistSize(Fts5Hash *pHash, Fts5HashEntry *p){
206298 if( p->iSzPoslist ){
206299 u8 *pPtr = (u8*)p;
206300 if( pHash->eDetail==FTS5_DETAIL_NONE ){
206301 assert( p->nData==p->iSzPoslist );
206302 if( p->bDel ){
206303 pPtr[p->nData++] = 0x00;
206304 if( p->bContent ){
206305 pPtr[p->nData++] = 0x00;
206306 }
206307 }
206308 }else{
206309 int nSz = (p->nData - p->iSzPoslist - 1); /* Size in bytes */
206310 int nPos = nSz*2 + p->bDel; /* Value of nPos field */
206311
206312 assert( p->bDel==0 || p->bDel==1 );
206313 if( nPos<=127 ){
206314 pPtr[p->iSzPoslist] = (u8)nPos;
206315 }else{
206316 int nByte = sqlite3Fts5GetVarintLen((u32)nPos);
206317 memmove(&pPtr[p->iSzPoslist + nByte], &pPtr[p->iSzPoslist + 1], nSz);
206318 sqlite3Fts5PutVarint(&pPtr[p->iSzPoslist], nPos);
206319 p->nData += (nByte-1);
206320 }
206321 }
206322
206323 p->iSzPoslist = 0;
206324 p->bDel = 0;
206325 p->bContent = 0;
206326 }
206327}
206328
206329/*
206330** Add an entry to the in-memory hash table. The key is the concatenation
206331** of bByte and (pToken/nToken). The value is (iRowid/iCol/iPos).
206332**
206333** (bByte || pToken) -> (iRowid,iCol,iPos)
206334**
206335** Or, if iCol is negative, then the value is a delete marker.
206336*/
206337static int sqlite3Fts5HashWrite(
206338 Fts5Hash *pHash,
206339 i64 iRowid, /* Rowid for this entry */
206340 int iCol, /* Column token appears in (-ve -> delete) */
206341 int iPos, /* Position of token within column */
206342 char bByte, /* First byte of token */
206343 const char *pToken, int nToken /* Token to add or remove to or from index */
206344){
206345 unsigned int iHash;
206346 Fts5HashEntry *p;
206347 u8 *pPtr;
206348 int nIncr = 0; /* Amount to increment (*pHash->pnByte) by */
206349 int bNew; /* If non-delete entry should be written */
206350
206351 bNew = (pHash->eDetail==FTS5_DETAIL_FULL);
206352
206353 /* Attempt to locate an existing hash entry */
206354 iHash = fts5HashKey2(pHash->nSlot, (u8)bByte, (const u8*)pToken, nToken);
206355 for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){
206356 char *zKey = fts5EntryKey(p);
206357 if( zKey[0]==bByte
206358 && p->nKey==nToken
206359 && memcmp(&zKey[1], pToken, nToken)==0
206360 ){
206361 break;
206362 }
206363 }
206364
206365 /* If an existing hash entry cannot be found, create a new one. */
206366 if( p==0 ){
206367 /* Figure out how much space to allocate */
206368 char *zKey;
206369 int nByte = sizeof(Fts5HashEntry) + (nToken+1) + 1 + 64;
206370 if( nByte<128 ) nByte = 128;
206371
206372 /* Grow the Fts5Hash.aSlot[] array if necessary. */
206373 if( (pHash->nEntry*2)>=pHash->nSlot ){
206374 int rc = fts5HashResize(pHash);
206375 if( rc!=SQLITE_OK ) return rc;
206376 iHash = fts5HashKey2(pHash->nSlot, (u8)bByte, (const u8*)pToken, nToken);
206377 }
206378
206379 /* Allocate new Fts5HashEntry and add it to the hash table. */
206380 p = (Fts5HashEntry*)sqlite3_malloc(nByte);
206381 if( !p ) return SQLITE_NOMEM;
206382 memset(p, 0, sizeof(Fts5HashEntry));
206383 p->nAlloc = nByte;
206384 zKey = fts5EntryKey(p);
206385 zKey[0] = bByte;
206386 memcpy(&zKey[1], pToken, nToken);
206387 assert( iHash==fts5HashKey(pHash->nSlot, (u8*)zKey, nToken+1) );
206388 p->nKey = nToken;
206389 zKey[nToken+1] = '\0';
206390 p->nData = nToken+1 + 1 + sizeof(Fts5HashEntry);
206391 p->pHashNext = pHash->aSlot[iHash];
206392 pHash->aSlot[iHash] = p;
206393 pHash->nEntry++;
206394
206395 /* Add the first rowid field to the hash-entry */
206396 p->nData += sqlite3Fts5PutVarint(&((u8*)p)[p->nData], iRowid);
206397 p->iRowid = iRowid;
206398
206399 p->iSzPoslist = p->nData;
206400 if( pHash->eDetail!=FTS5_DETAIL_NONE ){
206401 p->nData += 1;
206402 p->iCol = (pHash->eDetail==FTS5_DETAIL_FULL ? 0 : -1);
206403 }
206404
206405 nIncr += p->nData;
206406 }else{
206407
206408 /* Appending to an existing hash-entry. Check that there is enough
206409 ** space to append the largest possible new entry. Worst case scenario
206410 ** is:
206411 **
206412 ** + 9 bytes for a new rowid,
206413 ** + 4 byte reserved for the "poslist size" varint.
206414 ** + 1 byte for a "new column" byte,
206415 ** + 3 bytes for a new column number (16-bit max) as a varint,
206416 ** + 5 bytes for the new position offset (32-bit max).
206417 */
206418 if( (p->nAlloc - p->nData) < (9 + 4 + 1 + 3 + 5) ){
206419 int nNew = p->nAlloc * 2;
206420 Fts5HashEntry *pNew;
206421 Fts5HashEntry **pp;
206422 pNew = (Fts5HashEntry*)sqlite3_realloc(p, nNew);
206423 if( pNew==0 ) return SQLITE_NOMEM;
206424 pNew->nAlloc = nNew;
206425 for(pp=&pHash->aSlot[iHash]; *pp!=p; pp=&(*pp)->pHashNext);
206426 *pp = pNew;
206427 p = pNew;
206428 }
206429 nIncr -= p->nData;
206430 }
206431 assert( (p->nAlloc - p->nData) >= (9 + 4 + 1 + 3 + 5) );
206432
206433 pPtr = (u8*)p;
206434
206435 /* If this is a new rowid, append the 4-byte size field for the previous
206436 ** entry, and the new rowid for this entry. */
206437 if( iRowid!=p->iRowid ){
206438 fts5HashAddPoslistSize(pHash, p);
206439 p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iRowid - p->iRowid);
206440 p->iRowid = iRowid;
206441 bNew = 1;
206442 p->iSzPoslist = p->nData;
206443 if( pHash->eDetail!=FTS5_DETAIL_NONE ){
206444 p->nData += 1;
206445 p->iCol = (pHash->eDetail==FTS5_DETAIL_FULL ? 0 : -1);
206446 p->iPos = 0;
206447 }
206448 }
206449
206450 if( iCol>=0 ){
206451 if( pHash->eDetail==FTS5_DETAIL_NONE ){
206452 p->bContent = 1;
206453 }else{
206454 /* Append a new column value, if necessary */
206455 assert( iCol>=p->iCol );
206456 if( iCol!=p->iCol ){
206457 if( pHash->eDetail==FTS5_DETAIL_FULL ){
206458 pPtr[p->nData++] = 0x01;
206459 p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iCol);
206460 p->iCol = (i16)iCol;
206461 p->iPos = 0;
206462 }else{
206463 bNew = 1;
206464 p->iCol = (i16)(iPos = iCol);
206465 }
206466 }
206467
206468 /* Append the new position offset, if necessary */
206469 if( bNew ){
206470 p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iPos - p->iPos + 2);
206471 p->iPos = iPos;
206472 }
206473 }
206474 }else{
206475 /* This is a delete. Set the delete flag. */
206476 p->bDel = 1;
206477 }
206478
206479 nIncr += p->nData;
206480 *pHash->pnByte += nIncr;
206481 return SQLITE_OK;
206482}
206483
206484
206485/*
206486** Arguments pLeft and pRight point to linked-lists of hash-entry objects,
206487** each sorted in key order. This function merges the two lists into a
206488** single list and returns a pointer to its first element.
206489*/
206490static Fts5HashEntry *fts5HashEntryMerge(
206491 Fts5HashEntry *pLeft,
206492 Fts5HashEntry *pRight
206493){
206494 Fts5HashEntry *p1 = pLeft;
206495 Fts5HashEntry *p2 = pRight;
206496 Fts5HashEntry *pRet = 0;
206497 Fts5HashEntry **ppOut = &pRet;
206498
206499 while( p1 || p2 ){
206500 if( p1==0 ){
206501 *ppOut = p2;
206502 p2 = 0;
206503 }else if( p2==0 ){
206504 *ppOut = p1;
206505 p1 = 0;
206506 }else{
206507 int i = 0;
206508 char *zKey1 = fts5EntryKey(p1);
206509 char *zKey2 = fts5EntryKey(p2);
206510 while( zKey1[i]==zKey2[i] ) i++;
206511
206512 if( ((u8)zKey1[i])>((u8)zKey2[i]) ){
206513 /* p2 is smaller */
206514 *ppOut = p2;
206515 ppOut = &p2->pScanNext;
206516 p2 = p2->pScanNext;
206517 }else{
206518 /* p1 is smaller */
206519 *ppOut = p1;
206520 ppOut = &p1->pScanNext;
206521 p1 = p1->pScanNext;
206522 }
206523 *ppOut = 0;
206524 }
206525 }
206526
206527 return pRet;
206528}
206529
206530/*
206531** Extract all tokens from hash table iHash and link them into a list
206532** in sorted order. The hash table is cleared before returning. It is
206533** the responsibility of the caller to free the elements of the returned
206534** list.
206535*/
206536static int fts5HashEntrySort(
206537 Fts5Hash *pHash,
206538 const char *pTerm, int nTerm, /* Query prefix, if any */
206539 Fts5HashEntry **ppSorted
206540){
206541 const int nMergeSlot = 32;
206542 Fts5HashEntry **ap;
206543 Fts5HashEntry *pList;
206544 int iSlot;
206545 int i;
206546
206547 *ppSorted = 0;
206548 ap = sqlite3_malloc(sizeof(Fts5HashEntry*) * nMergeSlot);
206549 if( !ap ) return SQLITE_NOMEM;
206550 memset(ap, 0, sizeof(Fts5HashEntry*) * nMergeSlot);
206551
206552 for(iSlot=0; iSlot<pHash->nSlot; iSlot++){
206553 Fts5HashEntry *pIter;
206554 for(pIter=pHash->aSlot[iSlot]; pIter; pIter=pIter->pHashNext){
206555 if( pTerm==0 || 0==memcmp(fts5EntryKey(pIter), pTerm, nTerm) ){
206556 Fts5HashEntry *pEntry = pIter;
206557 pEntry->pScanNext = 0;
206558 for(i=0; ap[i]; i++){
206559 pEntry = fts5HashEntryMerge(pEntry, ap[i]);
206560 ap[i] = 0;
206561 }
206562 ap[i] = pEntry;
206563 }
206564 }
206565 }
206566
206567 pList = 0;
206568 for(i=0; i<nMergeSlot; i++){
206569 pList = fts5HashEntryMerge(pList, ap[i]);
206570 }
206571
206572 pHash->nEntry = 0;
206573 sqlite3_free(ap);
206574 *ppSorted = pList;
206575 return SQLITE_OK;
206576}
206577
206578/*
206579** Query the hash table for a doclist associated with term pTerm/nTerm.
206580*/
206581static int sqlite3Fts5HashQuery(
206582 Fts5Hash *pHash, /* Hash table to query */
206583 const char *pTerm, int nTerm, /* Query term */
206584 const u8 **ppDoclist, /* OUT: Pointer to doclist for pTerm */
206585 int *pnDoclist /* OUT: Size of doclist in bytes */
206586){
206587 unsigned int iHash = fts5HashKey(pHash->nSlot, (const u8*)pTerm, nTerm);
206588 char *zKey = 0;
206589 Fts5HashEntry *p;
206590
206591 for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){
206592 zKey = fts5EntryKey(p);
206593 if( memcmp(zKey, pTerm, nTerm)==0 && zKey[nTerm]==0 ) break;
206594 }
206595
206596 if( p ){
206597 fts5HashAddPoslistSize(pHash, p);
206598 *ppDoclist = (const u8*)&zKey[nTerm+1];
206599 *pnDoclist = p->nData - (sizeof(Fts5HashEntry) + nTerm + 1);
206600 }else{
206601 *ppDoclist = 0;
206602 *pnDoclist = 0;
206603 }
206604
206605 return SQLITE_OK;
206606}
206607
206608static int sqlite3Fts5HashScanInit(
206609 Fts5Hash *p, /* Hash table to query */
206610 const char *pTerm, int nTerm /* Query prefix */
206611){
206612 return fts5HashEntrySort(p, pTerm, nTerm, &p->pScan);
206613}
206614
206615static void sqlite3Fts5HashScanNext(Fts5Hash *p){
206616 assert( !sqlite3Fts5HashScanEof(p) );
206617 p->pScan = p->pScan->pScanNext;
206618}
206619
206620static int sqlite3Fts5HashScanEof(Fts5Hash *p){
206621 return (p->pScan==0);
206622}
206623
206624static void sqlite3Fts5HashScanEntry(
206625 Fts5Hash *pHash,
206626 const char **pzTerm, /* OUT: term (nul-terminated) */
206627 const u8 **ppDoclist, /* OUT: pointer to doclist */
206628 int *pnDoclist /* OUT: size of doclist in bytes */
206629){
206630 Fts5HashEntry *p;
206631 if( (p = pHash->pScan) ){
206632 char *zKey = fts5EntryKey(p);
206633 int nTerm = (int)strlen(zKey);
206634 fts5HashAddPoslistSize(pHash, p);
206635 *pzTerm = zKey;
206636 *ppDoclist = (const u8*)&zKey[nTerm+1];
206637 *pnDoclist = p->nData - (sizeof(Fts5HashEntry) + nTerm + 1);
206638 }else{
206639 *pzTerm = 0;
206640 *ppDoclist = 0;
206641 *pnDoclist = 0;
206642 }
206643}
206644
206645
206646/*
206647** 2014 May 31
206648**
206649** The author disclaims copyright to this source code. In place of
206650** a legal notice, here is a blessing:
206651**
206652** May you do good and not evil.
206653** May you find forgiveness for yourself and forgive others.
206654** May you share freely, never taking more than you give.
206655**
206656******************************************************************************
206657**
206658** Low level access to the FTS index stored in the database file. The
206659** routines in this file file implement all read and write access to the
206660** %_data table. Other parts of the system access this functionality via
206661** the interface defined in fts5Int.h.
206662*/
206663
206664
206665/* #include "fts5Int.h" */
206666
206667/*
206668** Overview:
206669**
206670** The %_data table contains all the FTS indexes for an FTS5 virtual table.
206671** As well as the main term index, there may be up to 31 prefix indexes.
206672** The format is similar to FTS3/4, except that:
206673**
206674** * all segment b-tree leaf data is stored in fixed size page records
206675** (e.g. 1000 bytes). A single doclist may span multiple pages. Care is
206676** taken to ensure it is possible to iterate in either direction through
206677** the entries in a doclist, or to seek to a specific entry within a
206678** doclist, without loading it into memory.
206679**
206680** * large doclists that span many pages have associated "doclist index"
206681** records that contain a copy of the first rowid on each page spanned by
206682** the doclist. This is used to speed up seek operations, and merges of
206683** large doclists with very small doclists.
206684**
206685** * extra fields in the "structure record" record the state of ongoing
206686** incremental merge operations.
206687**
206688*/
206689
206690
206691#define FTS5_OPT_WORK_UNIT 1000 /* Number of leaf pages per optimize step */
206692#define FTS5_WORK_UNIT 64 /* Number of leaf pages in unit of work */
206693
206694#define FTS5_MIN_DLIDX_SIZE 4 /* Add dlidx if this many empty pages */
206695
206696#define FTS5_MAIN_PREFIX '0'
206697
206698#if FTS5_MAX_PREFIX_INDEXES > 31
206699# error "FTS5_MAX_PREFIX_INDEXES is too large"
206700#endif
206701
206702/*
206703** Details:
206704**
206705** The %_data table managed by this module,
206706**
206707** CREATE TABLE %_data(id INTEGER PRIMARY KEY, block BLOB);
206708**
206709** , contains the following 5 types of records. See the comments surrounding
206710** the FTS5_*_ROWID macros below for a description of how %_data rowids are
206711** assigned to each fo them.
206712**
206713** 1. Structure Records:
206714**
206715** The set of segments that make up an index - the index structure - are
206716** recorded in a single record within the %_data table. The record consists
206717** of a single 32-bit configuration cookie value followed by a list of
206718** SQLite varints. If the FTS table features more than one index (because
206719** there are one or more prefix indexes), it is guaranteed that all share
206720** the same cookie value.
206721**
206722** Immediately following the configuration cookie, the record begins with
206723** three varints:
206724**
206725** + number of levels,
206726** + total number of segments on all levels,
206727** + value of write counter.
206728**
206729** Then, for each level from 0 to nMax:
206730**
206731** + number of input segments in ongoing merge.
206732** + total number of segments in level.
206733** + for each segment from oldest to newest:
206734** + segment id (always > 0)
206735** + first leaf page number (often 1, always greater than 0)
206736** + final leaf page number
206737**
206738** 2. The Averages Record:
206739**
206740** A single record within the %_data table. The data is a list of varints.
206741** The first value is the number of rows in the index. Then, for each column
206742** from left to right, the total number of tokens in the column for all
206743** rows of the table.
206744**
206745** 3. Segment leaves:
206746**
206747** TERM/DOCLIST FORMAT:
206748**
206749** Most of each segment leaf is taken up by term/doclist data. The
206750** general format of term/doclist, starting with the first term
206751** on the leaf page, is:
206752**
206753** varint : size of first term
206754** blob: first term data
206755** doclist: first doclist
206756** zero-or-more {
206757** varint: number of bytes in common with previous term
206758** varint: number of bytes of new term data (nNew)
206759** blob: nNew bytes of new term data
206760** doclist: next doclist
206761** }
206762**
206763** doclist format:
206764**
206765** varint: first rowid
206766** poslist: first poslist
206767** zero-or-more {
206768** varint: rowid delta (always > 0)
206769** poslist: next poslist
206770** }
206771**
206772** poslist format:
206773**
206774** varint: size of poslist in bytes multiplied by 2, not including
206775** this field. Plus 1 if this entry carries the "delete" flag.
206776** collist: collist for column 0
206777** zero-or-more {
206778** 0x01 byte
206779** varint: column number (I)
206780** collist: collist for column I
206781** }
206782**
206783** collist format:
206784**
206785** varint: first offset + 2
206786** zero-or-more {
206787** varint: offset delta + 2
206788** }
206789**
206790** PAGE FORMAT
206791**
206792** Each leaf page begins with a 4-byte header containing 2 16-bit
206793** unsigned integer fields in big-endian format. They are:
206794**
206795** * The byte offset of the first rowid on the page, if it exists
206796** and occurs before the first term (otherwise 0).
206797**
206798** * The byte offset of the start of the page footer. If the page
206799** footer is 0 bytes in size, then this field is the same as the
206800** size of the leaf page in bytes.
206801**
206802** The page footer consists of a single varint for each term located
206803** on the page. Each varint is the byte offset of the current term
206804** within the page, delta-compressed against the previous value. In
206805** other words, the first varint in the footer is the byte offset of
206806** the first term, the second is the byte offset of the second less that
206807** of the first, and so on.
206808**
206809** The term/doclist format described above is accurate if the entire
206810** term/doclist data fits on a single leaf page. If this is not the case,
206811** the format is changed in two ways:
206812**
206813** + if the first rowid on a page occurs before the first term, it
206814** is stored as a literal value:
206815**
206816** varint: first rowid
206817**
206818** + the first term on each page is stored in the same way as the
206819** very first term of the segment:
206820**
206821** varint : size of first term
206822** blob: first term data
206823**
206824** 5. Segment doclist indexes:
206825**
206826** Doclist indexes are themselves b-trees, however they usually consist of
206827** a single leaf record only. The format of each doclist index leaf page
206828** is:
206829**
206830** * Flags byte. Bits are:
206831** 0x01: Clear if leaf is also the root page, otherwise set.
206832**
206833** * Page number of fts index leaf page. As a varint.
206834**
206835** * First rowid on page indicated by previous field. As a varint.
206836**
206837** * A list of varints, one for each subsequent termless page. A
206838** positive delta if the termless page contains at least one rowid,
206839** or an 0x00 byte otherwise.
206840**
206841** Internal doclist index nodes are:
206842**
206843** * Flags byte. Bits are:
206844** 0x01: Clear for root page, otherwise set.
206845**
206846** * Page number of first child page. As a varint.
206847**
206848** * Copy of first rowid on page indicated by previous field. As a varint.
206849**
206850** * A list of delta-encoded varints - the first rowid on each subsequent
206851** child page.
206852**
206853*/
206854
206855/*
206856** Rowids for the averages and structure records in the %_data table.
206857*/
206858#define FTS5_AVERAGES_ROWID 1 /* Rowid used for the averages record */
206859#define FTS5_STRUCTURE_ROWID 10 /* The structure record */
206860
206861/*
206862** Macros determining the rowids used by segment leaves and dlidx leaves
206863** and nodes. All nodes and leaves are stored in the %_data table with large
206864** positive rowids.
206865**
206866** Each segment has a unique non-zero 16-bit id.
206867**
206868** The rowid for each segment leaf is found by passing the segment id and
206869** the leaf page number to the FTS5_SEGMENT_ROWID macro. Leaves are numbered
206870** sequentially starting from 1.
206871*/
206872#define FTS5_DATA_ID_B 16 /* Max seg id number 65535 */
206873#define FTS5_DATA_DLI_B 1 /* Doclist-index flag (1 bit) */
206874#define FTS5_DATA_HEIGHT_B 5 /* Max dlidx tree height of 32 */
206875#define FTS5_DATA_PAGE_B 31 /* Max page number of 2147483648 */
206876
206877#define fts5_dri(segid, dlidx, height, pgno) ( \
206878 ((i64)(segid) << (FTS5_DATA_PAGE_B+FTS5_DATA_HEIGHT_B+FTS5_DATA_DLI_B)) + \
206879 ((i64)(dlidx) << (FTS5_DATA_PAGE_B + FTS5_DATA_HEIGHT_B)) + \
206880 ((i64)(height) << (FTS5_DATA_PAGE_B)) + \
206881 ((i64)(pgno)) \
206882)
206883
206884#define FTS5_SEGMENT_ROWID(segid, pgno) fts5_dri(segid, 0, 0, pgno)
206885#define FTS5_DLIDX_ROWID(segid, height, pgno) fts5_dri(segid, 1, height, pgno)
206886
206887/*
206888** Maximum segments permitted in a single index
206889*/
206890#define FTS5_MAX_SEGMENT 2000
206891
206892#ifdef SQLITE_DEBUG
206893static int sqlite3Fts5Corrupt() { return SQLITE_CORRUPT_VTAB; }
206894#endif
206895
206896
206897/*
206898** Each time a blob is read from the %_data table, it is padded with this
206899** many zero bytes. This makes it easier to decode the various record formats
206900** without overreading if the records are corrupt.
206901*/
206902#define FTS5_DATA_ZERO_PADDING 8
206903#define FTS5_DATA_PADDING 20
206904
206905typedef struct Fts5Data Fts5Data;
206906typedef struct Fts5DlidxIter Fts5DlidxIter;
206907typedef struct Fts5DlidxLvl Fts5DlidxLvl;
206908typedef struct Fts5DlidxWriter Fts5DlidxWriter;
206909typedef struct Fts5Iter Fts5Iter;
206910typedef struct Fts5PageWriter Fts5PageWriter;
206911typedef struct Fts5SegIter Fts5SegIter;
206912typedef struct Fts5DoclistIter Fts5DoclistIter;
206913typedef struct Fts5SegWriter Fts5SegWriter;
206914typedef struct Fts5Structure Fts5Structure;
206915typedef struct Fts5StructureLevel Fts5StructureLevel;
206916typedef struct Fts5StructureSegment Fts5StructureSegment;
206917
206918struct Fts5Data {
206919 u8 *p; /* Pointer to buffer containing record */
206920 int nn; /* Size of record in bytes */
206921 int szLeaf; /* Size of leaf without page-index */
206922};
206923
206924/*
206925** One object per %_data table.
206926*/
206927struct Fts5Index {
206928 Fts5Config *pConfig; /* Virtual table configuration */
206929 char *zDataTbl; /* Name of %_data table */
206930 int nWorkUnit; /* Leaf pages in a "unit" of work */
206931
206932 /*
206933 ** Variables related to the accumulation of tokens and doclists within the
206934 ** in-memory hash tables before they are flushed to disk.
206935 */
206936 Fts5Hash *pHash; /* Hash table for in-memory data */
206937 int nPendingData; /* Current bytes of pending data */
206938 i64 iWriteRowid; /* Rowid for current doc being written */
206939 int bDelete; /* Current write is a delete */
206940
206941 /* Error state. */
206942 int rc; /* Current error code */
206943
206944 /* State used by the fts5DataXXX() functions. */
206945 sqlite3_blob *pReader; /* RO incr-blob open on %_data table */
206946 sqlite3_stmt *pWriter; /* "INSERT ... %_data VALUES(?,?)" */
206947 sqlite3_stmt *pDeleter; /* "DELETE FROM %_data ... id>=? AND id<=?" */
206948 sqlite3_stmt *pIdxWriter; /* "INSERT ... %_idx VALUES(?,?,?,?)" */
206949 sqlite3_stmt *pIdxDeleter; /* "DELETE FROM %_idx WHERE segid=? */
206950 sqlite3_stmt *pIdxSelect;
206951 int nRead; /* Total number of blocks read */
206952
206953 sqlite3_stmt *pDataVersion;
206954 i64 iStructVersion; /* data_version when pStruct read */
206955 Fts5Structure *pStruct; /* Current db structure (or NULL) */
206956};
206957
206958struct Fts5DoclistIter {
206959 u8 *aEof; /* Pointer to 1 byte past end of doclist */
206960
206961 /* Output variables. aPoslist==0 at EOF */
206962 i64 iRowid;
206963 u8 *aPoslist;
206964 int nPoslist;
206965 int nSize;
206966};
206967
206968/*
206969** The contents of the "structure" record for each index are represented
206970** using an Fts5Structure record in memory. Which uses instances of the
206971** other Fts5StructureXXX types as components.
206972*/
206973struct Fts5StructureSegment {
206974 int iSegid; /* Segment id */
206975 int pgnoFirst; /* First leaf page number in segment */
206976 int pgnoLast; /* Last leaf page number in segment */
206977};
206978struct Fts5StructureLevel {
206979 int nMerge; /* Number of segments in incr-merge */
206980 int nSeg; /* Total number of segments on level */
206981 Fts5StructureSegment *aSeg; /* Array of segments. aSeg[0] is oldest. */
206982};
206983struct Fts5Structure {
206984 int nRef; /* Object reference count */
206985 u64 nWriteCounter; /* Total leaves written to level 0 */
206986 int nSegment; /* Total segments in this structure */
206987 int nLevel; /* Number of levels in this index */
206988 Fts5StructureLevel aLevel[1]; /* Array of nLevel level objects */
206989};
206990
206991/*
206992** An object of type Fts5SegWriter is used to write to segments.
206993*/
206994struct Fts5PageWriter {
206995 int pgno; /* Page number for this page */
206996 int iPrevPgidx; /* Previous value written into pgidx */
206997 Fts5Buffer buf; /* Buffer containing leaf data */
206998 Fts5Buffer pgidx; /* Buffer containing page-index */
206999 Fts5Buffer term; /* Buffer containing previous term on page */
207000};
207001struct Fts5DlidxWriter {
207002 int pgno; /* Page number for this page */
207003 int bPrevValid; /* True if iPrev is valid */
207004 i64 iPrev; /* Previous rowid value written to page */
207005 Fts5Buffer buf; /* Buffer containing page data */
207006};
207007struct Fts5SegWriter {
207008 int iSegid; /* Segid to write to */
207009 Fts5PageWriter writer; /* PageWriter object */
207010 i64 iPrevRowid; /* Previous rowid written to current leaf */
207011 u8 bFirstRowidInDoclist; /* True if next rowid is first in doclist */
207012 u8 bFirstRowidInPage; /* True if next rowid is first in page */
207013 /* TODO1: Can use (writer.pgidx.n==0) instead of bFirstTermInPage */
207014 u8 bFirstTermInPage; /* True if next term will be first in leaf */
207015 int nLeafWritten; /* Number of leaf pages written */
207016 int nEmpty; /* Number of contiguous term-less nodes */
207017
207018 int nDlidx; /* Allocated size of aDlidx[] array */
207019 Fts5DlidxWriter *aDlidx; /* Array of Fts5DlidxWriter objects */
207020
207021 /* Values to insert into the %_idx table */
207022 Fts5Buffer btterm; /* Next term to insert into %_idx table */
207023 int iBtPage; /* Page number corresponding to btterm */
207024};
207025
207026typedef struct Fts5CResult Fts5CResult;
207027struct Fts5CResult {
207028 u16 iFirst; /* aSeg[] index of firstest iterator */
207029 u8 bTermEq; /* True if the terms are equal */
207030};
207031
207032/*
207033** Object for iterating through a single segment, visiting each term/rowid
207034** pair in the segment.
207035**
207036** pSeg:
207037** The segment to iterate through.
207038**
207039** iLeafPgno:
207040** Current leaf page number within segment.
207041**
207042** iLeafOffset:
207043** Byte offset within the current leaf that is the first byte of the
207044** position list data (one byte passed the position-list size field).
207045** rowid field of the current entry. Usually this is the size field of the
207046** position list data. The exception is if the rowid for the current entry
207047** is the last thing on the leaf page.
207048**
207049** pLeaf:
207050** Buffer containing current leaf page data. Set to NULL at EOF.
207051**
207052** iTermLeafPgno, iTermLeafOffset:
207053** Leaf page number containing the last term read from the segment. And
207054** the offset immediately following the term data.
207055**
207056** flags:
207057** Mask of FTS5_SEGITER_XXX values. Interpreted as follows:
207058**
207059** FTS5_SEGITER_ONETERM:
207060** If set, set the iterator to point to EOF after the current doclist
207061** has been exhausted. Do not proceed to the next term in the segment.
207062**
207063** FTS5_SEGITER_REVERSE:
207064** This flag is only ever set if FTS5_SEGITER_ONETERM is also set. If
207065** it is set, iterate through rowid in descending order instead of the
207066** default ascending order.
207067**
207068** iRowidOffset/nRowidOffset/aRowidOffset:
207069** These are used if the FTS5_SEGITER_REVERSE flag is set.
207070**
207071** For each rowid on the page corresponding to the current term, the
207072** corresponding aRowidOffset[] entry is set to the byte offset of the
207073** start of the "position-list-size" field within the page.
207074**
207075** iTermIdx:
207076** Index of current term on iTermLeafPgno.
207077*/
207078struct Fts5SegIter {
207079 Fts5StructureSegment *pSeg; /* Segment to iterate through */
207080 int flags; /* Mask of configuration flags */
207081 int iLeafPgno; /* Current leaf page number */
207082 Fts5Data *pLeaf; /* Current leaf data */
207083 Fts5Data *pNextLeaf; /* Leaf page (iLeafPgno+1) */
207084 int iLeafOffset; /* Byte offset within current leaf */
207085
207086 /* Next method */
207087 void (*xNext)(Fts5Index*, Fts5SegIter*, int*);
207088
207089 /* The page and offset from which the current term was read. The offset
207090 ** is the offset of the first rowid in the current doclist. */
207091 int iTermLeafPgno;
207092 int iTermLeafOffset;
207093
207094 int iPgidxOff; /* Next offset in pgidx */
207095 int iEndofDoclist;
207096
207097 /* The following are only used if the FTS5_SEGITER_REVERSE flag is set. */
207098 int iRowidOffset; /* Current entry in aRowidOffset[] */
207099 int nRowidOffset; /* Allocated size of aRowidOffset[] array */
207100 int *aRowidOffset; /* Array of offset to rowid fields */
207101
207102 Fts5DlidxIter *pDlidx; /* If there is a doclist-index */
207103
207104 /* Variables populated based on current entry. */
207105 Fts5Buffer term; /* Current term */
207106 i64 iRowid; /* Current rowid */
207107 int nPos; /* Number of bytes in current position list */
207108 u8 bDel; /* True if the delete flag is set */
207109};
207110
207111/*
207112** Argument is a pointer to an Fts5Data structure that contains a
207113** leaf page.
207114*/
207115#define ASSERT_SZLEAF_OK(x) assert( \
207116 (x)->szLeaf==(x)->nn || (x)->szLeaf==fts5GetU16(&(x)->p[2]) \
207117)
207118
207119#define FTS5_SEGITER_ONETERM 0x01
207120#define FTS5_SEGITER_REVERSE 0x02
207121
207122/*
207123** Argument is a pointer to an Fts5Data structure that contains a leaf
207124** page. This macro evaluates to true if the leaf contains no terms, or
207125** false if it contains at least one term.
207126*/
207127#define fts5LeafIsTermless(x) ((x)->szLeaf >= (x)->nn)
207128
207129#define fts5LeafTermOff(x, i) (fts5GetU16(&(x)->p[(x)->szLeaf + (i)*2]))
207130
207131#define fts5LeafFirstRowidOff(x) (fts5GetU16((x)->p))
207132
207133/*
207134** Object for iterating through the merged results of one or more segments,
207135** visiting each term/rowid pair in the merged data.
207136**
207137** nSeg is always a power of two greater than or equal to the number of
207138** segments that this object is merging data from. Both the aSeg[] and
207139** aFirst[] arrays are sized at nSeg entries. The aSeg[] array is padded
207140** with zeroed objects - these are handled as if they were iterators opened
207141** on empty segments.
207142**
207143** The results of comparing segments aSeg[N] and aSeg[N+1], where N is an
207144** even number, is stored in aFirst[(nSeg+N)/2]. The "result" of the
207145** comparison in this context is the index of the iterator that currently
207146** points to the smaller term/rowid combination. Iterators at EOF are
207147** considered to be greater than all other iterators.
207148**
207149** aFirst[1] contains the index in aSeg[] of the iterator that points to
207150** the smallest key overall. aFirst[0] is unused.
207151**
207152** poslist:
207153** Used by sqlite3Fts5IterPoslist() when the poslist needs to be buffered.
207154** There is no way to tell if this is populated or not.
207155*/
207156struct Fts5Iter {
207157 Fts5IndexIter base; /* Base class containing output vars */
207158
207159 Fts5Index *pIndex; /* Index that owns this iterator */
207160 Fts5Structure *pStruct; /* Database structure for this iterator */
207161 Fts5Buffer poslist; /* Buffer containing current poslist */
207162 Fts5Colset *pColset; /* Restrict matches to these columns */
207163
207164 /* Invoked to set output variables. */
207165 void (*xSetOutputs)(Fts5Iter*, Fts5SegIter*);
207166
207167 int nSeg; /* Size of aSeg[] array */
207168 int bRev; /* True to iterate in reverse order */
207169 u8 bSkipEmpty; /* True to skip deleted entries */
207170
207171 i64 iSwitchRowid; /* Firstest rowid of other than aFirst[1] */
207172 Fts5CResult *aFirst; /* Current merge state (see above) */
207173 Fts5SegIter aSeg[1]; /* Array of segment iterators */
207174};
207175
207176
207177/*
207178** An instance of the following type is used to iterate through the contents
207179** of a doclist-index record.
207180**
207181** pData:
207182** Record containing the doclist-index data.
207183**
207184** bEof:
207185** Set to true once iterator has reached EOF.
207186**
207187** iOff:
207188** Set to the current offset within record pData.
207189*/
207190struct Fts5DlidxLvl {
207191 Fts5Data *pData; /* Data for current page of this level */
207192 int iOff; /* Current offset into pData */
207193 int bEof; /* At EOF already */
207194 int iFirstOff; /* Used by reverse iterators */
207195
207196 /* Output variables */
207197 int iLeafPgno; /* Page number of current leaf page */
207198 i64 iRowid; /* First rowid on leaf iLeafPgno */
207199};
207200struct Fts5DlidxIter {
207201 int nLvl;
207202 int iSegid;
207203 Fts5DlidxLvl aLvl[1];
207204};
207205
207206static void fts5PutU16(u8 *aOut, u16 iVal){
207207 aOut[0] = (iVal>>8);
207208 aOut[1] = (iVal&0xFF);
207209}
207210
207211static u16 fts5GetU16(const u8 *aIn){
207212 return ((u16)aIn[0] << 8) + aIn[1];
207213}
207214
207215/*
207216** Allocate and return a buffer at least nByte bytes in size.
207217**
207218** If an OOM error is encountered, return NULL and set the error code in
207219** the Fts5Index handle passed as the first argument.
207220*/
207221static void *fts5IdxMalloc(Fts5Index *p, int nByte){
207222 return sqlite3Fts5MallocZero(&p->rc, nByte);
207223}
207224
207225/*
207226** Compare the contents of the pLeft buffer with the pRight/nRight blob.
207227**
207228** Return -ve if pLeft is smaller than pRight, 0 if they are equal or
207229** +ve if pRight is smaller than pLeft. In other words:
207230**
207231** res = *pLeft - *pRight
207232*/
207233#ifdef SQLITE_DEBUG
207234static int fts5BufferCompareBlob(
207235 Fts5Buffer *pLeft, /* Left hand side of comparison */
207236 const u8 *pRight, int nRight /* Right hand side of comparison */
207237){
207238 int nCmp = MIN(pLeft->n, nRight);
207239 int res = memcmp(pLeft->p, pRight, nCmp);
207240 return (res==0 ? (pLeft->n - nRight) : res);
207241}
207242#endif
207243
207244/*
207245** Compare the contents of the two buffers using memcmp(). If one buffer
207246** is a prefix of the other, it is considered the lesser.
207247**
207248** Return -ve if pLeft is smaller than pRight, 0 if they are equal or
207249** +ve if pRight is smaller than pLeft. In other words:
207250**
207251** res = *pLeft - *pRight
207252*/
207253static int fts5BufferCompare(Fts5Buffer *pLeft, Fts5Buffer *pRight){
207254 int nCmp = MIN(pLeft->n, pRight->n);
207255 int res = memcmp(pLeft->p, pRight->p, nCmp);
207256 return (res==0 ? (pLeft->n - pRight->n) : res);
207257}
207258
207259static int fts5LeafFirstTermOff(Fts5Data *pLeaf){
207260 int ret;
207261 fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf], ret);
207262 return ret;
207263}
207264
207265/*
207266** Close the read-only blob handle, if it is open.
207267*/
207268static void fts5CloseReader(Fts5Index *p){
207269 if( p->pReader ){
207270 sqlite3_blob *pReader = p->pReader;
207271 p->pReader = 0;
207272 sqlite3_blob_close(pReader);
207273 }
207274}
207275
207276/*
207277** Retrieve a record from the %_data table.
207278**
207279** If an error occurs, NULL is returned and an error left in the
207280** Fts5Index object.
207281*/
207282static Fts5Data *fts5DataRead(Fts5Index *p, i64 iRowid){
207283 Fts5Data *pRet = 0;
207284 if( p->rc==SQLITE_OK ){
207285 int rc = SQLITE_OK;
207286
207287 if( p->pReader ){
207288 /* This call may return SQLITE_ABORT if there has been a savepoint
207289 ** rollback since it was last used. In this case a new blob handle
207290 ** is required. */
207291 sqlite3_blob *pBlob = p->pReader;
207292 p->pReader = 0;
207293 rc = sqlite3_blob_reopen(pBlob, iRowid);
207294 assert( p->pReader==0 );
207295 p->pReader = pBlob;
207296 if( rc!=SQLITE_OK ){
207297 fts5CloseReader(p);
207298 }
207299 if( rc==SQLITE_ABORT ) rc = SQLITE_OK;
207300 }
207301
207302 /* If the blob handle is not open at this point, open it and seek
207303 ** to the requested entry. */
207304 if( p->pReader==0 && rc==SQLITE_OK ){
207305 Fts5Config *pConfig = p->pConfig;
207306 rc = sqlite3_blob_open(pConfig->db,
207307 pConfig->zDb, p->zDataTbl, "block", iRowid, 0, &p->pReader
207308 );
207309 }
207310
207311 /* If either of the sqlite3_blob_open() or sqlite3_blob_reopen() calls
207312 ** above returned SQLITE_ERROR, return SQLITE_CORRUPT_VTAB instead.
207313 ** All the reasons those functions might return SQLITE_ERROR - missing
207314 ** table, missing row, non-blob/text in block column - indicate
207315 ** backing store corruption. */
207316 if( rc==SQLITE_ERROR ) rc = FTS5_CORRUPT;
207317
207318 if( rc==SQLITE_OK ){
207319 u8 *aOut = 0; /* Read blob data into this buffer */
207320 int nByte = sqlite3_blob_bytes(p->pReader);
207321 int nAlloc = sizeof(Fts5Data) + nByte + FTS5_DATA_PADDING;
207322 pRet = (Fts5Data*)sqlite3_malloc(nAlloc);
207323 if( pRet ){
207324 pRet->nn = nByte;
207325 aOut = pRet->p = (u8*)&pRet[1];
207326 }else{
207327 rc = SQLITE_NOMEM;
207328 }
207329
207330 if( rc==SQLITE_OK ){
207331 rc = sqlite3_blob_read(p->pReader, aOut, nByte, 0);
207332 }
207333 if( rc!=SQLITE_OK ){
207334 sqlite3_free(pRet);
207335 pRet = 0;
207336 }else{
207337 /* TODO1: Fix this */
207338 pRet->szLeaf = fts5GetU16(&pRet->p[2]);
207339 }
207340 }
207341 p->rc = rc;
207342 p->nRead++;
207343 }
207344
207345 assert( (pRet==0)==(p->rc!=SQLITE_OK) );
207346 return pRet;
207347}
207348
207349/*
207350** Release a reference to data record returned by an earlier call to
207351** fts5DataRead().
207352*/
207353static void fts5DataRelease(Fts5Data *pData){
207354 sqlite3_free(pData);
207355}
207356
207357static Fts5Data *fts5LeafRead(Fts5Index *p, i64 iRowid){
207358 Fts5Data *pRet = fts5DataRead(p, iRowid);
207359 if( pRet ){
207360 if( pRet->szLeaf>pRet->nn ){
207361 p->rc = FTS5_CORRUPT;
207362 fts5DataRelease(pRet);
207363 pRet = 0;
207364 }
207365 }
207366 return pRet;
207367}
207368
207369static int fts5IndexPrepareStmt(
207370 Fts5Index *p,
207371 sqlite3_stmt **ppStmt,
207372 char *zSql
207373){
207374 if( p->rc==SQLITE_OK ){
207375 if( zSql ){
207376 p->rc = sqlite3_prepare_v3(p->pConfig->db, zSql, -1,
207377 SQLITE_PREPARE_PERSISTENT, ppStmt, 0);
207378 }else{
207379 p->rc = SQLITE_NOMEM;
207380 }
207381 }
207382 sqlite3_free(zSql);
207383 return p->rc;
207384}
207385
207386
207387/*
207388** INSERT OR REPLACE a record into the %_data table.
207389*/
207390static void fts5DataWrite(Fts5Index *p, i64 iRowid, const u8 *pData, int nData){
207391 if( p->rc!=SQLITE_OK ) return;
207392
207393 if( p->pWriter==0 ){
207394 Fts5Config *pConfig = p->pConfig;
207395 fts5IndexPrepareStmt(p, &p->pWriter, sqlite3_mprintf(
207396 "REPLACE INTO '%q'.'%q_data'(id, block) VALUES(?,?)",
207397 pConfig->zDb, pConfig->zName
207398 ));
207399 if( p->rc ) return;
207400 }
207401
207402 sqlite3_bind_int64(p->pWriter, 1, iRowid);
207403 sqlite3_bind_blob(p->pWriter, 2, pData, nData, SQLITE_STATIC);
207404 sqlite3_step(p->pWriter);
207405 p->rc = sqlite3_reset(p->pWriter);
207406 sqlite3_bind_null(p->pWriter, 2);
207407}
207408
207409/*
207410** Execute the following SQL:
207411**
207412** DELETE FROM %_data WHERE id BETWEEN $iFirst AND $iLast
207413*/
207414static void fts5DataDelete(Fts5Index *p, i64 iFirst, i64 iLast){
207415 if( p->rc!=SQLITE_OK ) return;
207416
207417 if( p->pDeleter==0 ){
207418 int rc;
207419 Fts5Config *pConfig = p->pConfig;
207420 char *zSql = sqlite3_mprintf(
207421 "DELETE FROM '%q'.'%q_data' WHERE id>=? AND id<=?",
207422 pConfig->zDb, pConfig->zName
207423 );
207424 if( zSql==0 ){
207425 rc = SQLITE_NOMEM;
207426 }else{
207427 rc = sqlite3_prepare_v3(pConfig->db, zSql, -1,
207428 SQLITE_PREPARE_PERSISTENT, &p->pDeleter, 0);
207429 sqlite3_free(zSql);
207430 }
207431 if( rc!=SQLITE_OK ){
207432 p->rc = rc;
207433 return;
207434 }
207435 }
207436
207437 sqlite3_bind_int64(p->pDeleter, 1, iFirst);
207438 sqlite3_bind_int64(p->pDeleter, 2, iLast);
207439 sqlite3_step(p->pDeleter);
207440 p->rc = sqlite3_reset(p->pDeleter);
207441}
207442
207443/*
207444** Remove all records associated with segment iSegid.
207445*/
207446static void fts5DataRemoveSegment(Fts5Index *p, int iSegid){
207447 i64 iFirst = FTS5_SEGMENT_ROWID(iSegid, 0);
207448 i64 iLast = FTS5_SEGMENT_ROWID(iSegid+1, 0)-1;
207449 fts5DataDelete(p, iFirst, iLast);
207450 if( p->pIdxDeleter==0 ){
207451 Fts5Config *pConfig = p->pConfig;
207452 fts5IndexPrepareStmt(p, &p->pIdxDeleter, sqlite3_mprintf(
207453 "DELETE FROM '%q'.'%q_idx' WHERE segid=?",
207454 pConfig->zDb, pConfig->zName
207455 ));
207456 }
207457 if( p->rc==SQLITE_OK ){
207458 sqlite3_bind_int(p->pIdxDeleter, 1, iSegid);
207459 sqlite3_step(p->pIdxDeleter);
207460 p->rc = sqlite3_reset(p->pIdxDeleter);
207461 }
207462}
207463
207464/*
207465** Release a reference to an Fts5Structure object returned by an earlier
207466** call to fts5StructureRead() or fts5StructureDecode().
207467*/
207468static void fts5StructureRelease(Fts5Structure *pStruct){
207469 if( pStruct && 0>=(--pStruct->nRef) ){
207470 int i;
207471 assert( pStruct->nRef==0 );
207472 for(i=0; i<pStruct->nLevel; i++){
207473 sqlite3_free(pStruct->aLevel[i].aSeg);
207474 }
207475 sqlite3_free(pStruct);
207476 }
207477}
207478
207479static void fts5StructureRef(Fts5Structure *pStruct){
207480 pStruct->nRef++;
207481}
207482
207483/*
207484** Deserialize and return the structure record currently stored in serialized
207485** form within buffer pData/nData.
207486**
207487** The Fts5Structure.aLevel[] and each Fts5StructureLevel.aSeg[] array
207488** are over-allocated by one slot. This allows the structure contents
207489** to be more easily edited.
207490**
207491** If an error occurs, *ppOut is set to NULL and an SQLite error code
207492** returned. Otherwise, *ppOut is set to point to the new object and
207493** SQLITE_OK returned.
207494*/
207495static int fts5StructureDecode(
207496 const u8 *pData, /* Buffer containing serialized structure */
207497 int nData, /* Size of buffer pData in bytes */
207498 int *piCookie, /* Configuration cookie value */
207499 Fts5Structure **ppOut /* OUT: Deserialized object */
207500){
207501 int rc = SQLITE_OK;
207502 int i = 0;
207503 int iLvl;
207504 int nLevel = 0;
207505 int nSegment = 0;
207506 int nByte; /* Bytes of space to allocate at pRet */
207507 Fts5Structure *pRet = 0; /* Structure object to return */
207508
207509 /* Grab the cookie value */
207510 if( piCookie ) *piCookie = sqlite3Fts5Get32(pData);
207511 i = 4;
207512
207513 /* Read the total number of levels and segments from the start of the
207514 ** structure record. */
207515 i += fts5GetVarint32(&pData[i], nLevel);
207516 i += fts5GetVarint32(&pData[i], nSegment);
207517 nByte = (
207518 sizeof(Fts5Structure) + /* Main structure */
207519 sizeof(Fts5StructureLevel) * (nLevel-1) /* aLevel[] array */
207520 );
207521 pRet = (Fts5Structure*)sqlite3Fts5MallocZero(&rc, nByte);
207522
207523 if( pRet ){
207524 pRet->nRef = 1;
207525 pRet->nLevel = nLevel;
207526 pRet->nSegment = nSegment;
207527 i += sqlite3Fts5GetVarint(&pData[i], &pRet->nWriteCounter);
207528
207529 for(iLvl=0; rc==SQLITE_OK && iLvl<nLevel; iLvl++){
207530 Fts5StructureLevel *pLvl = &pRet->aLevel[iLvl];
207531 int nTotal = 0;
207532 int iSeg;
207533
207534 if( i>=nData ){
207535 rc = FTS5_CORRUPT;
207536 }else{
207537 i += fts5GetVarint32(&pData[i], pLvl->nMerge);
207538 i += fts5GetVarint32(&pData[i], nTotal);
207539 assert( nTotal>=pLvl->nMerge );
207540 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&rc,
207541 nTotal * sizeof(Fts5StructureSegment)
207542 );
207543 }
207544
207545 if( rc==SQLITE_OK ){
207546 pLvl->nSeg = nTotal;
207547 for(iSeg=0; iSeg<nTotal; iSeg++){
207548 if( i>=nData ){
207549 rc = FTS5_CORRUPT;
207550 break;
207551 }
207552 i += fts5GetVarint32(&pData[i], pLvl->aSeg[iSeg].iSegid);
207553 i += fts5GetVarint32(&pData[i], pLvl->aSeg[iSeg].pgnoFirst);
207554 i += fts5GetVarint32(&pData[i], pLvl->aSeg[iSeg].pgnoLast);
207555 }
207556 }
207557 }
207558 if( rc!=SQLITE_OK ){
207559 fts5StructureRelease(pRet);
207560 pRet = 0;
207561 }
207562 }
207563
207564 *ppOut = pRet;
207565 return rc;
207566}
207567
207568/*
207569**
207570*/
207571static void fts5StructureAddLevel(int *pRc, Fts5Structure **ppStruct){
207572 if( *pRc==SQLITE_OK ){
207573 Fts5Structure *pStruct = *ppStruct;
207574 int nLevel = pStruct->nLevel;
207575 int nByte = (
207576 sizeof(Fts5Structure) + /* Main structure */
207577 sizeof(Fts5StructureLevel) * (nLevel+1) /* aLevel[] array */
207578 );
207579
207580 pStruct = sqlite3_realloc(pStruct, nByte);
207581 if( pStruct ){
207582 memset(&pStruct->aLevel[nLevel], 0, sizeof(Fts5StructureLevel));
207583 pStruct->nLevel++;
207584 *ppStruct = pStruct;
207585 }else{
207586 *pRc = SQLITE_NOMEM;
207587 }
207588 }
207589}
207590
207591/*
207592** Extend level iLvl so that there is room for at least nExtra more
207593** segments.
207594*/
207595static void fts5StructureExtendLevel(
207596 int *pRc,
207597 Fts5Structure *pStruct,
207598 int iLvl,
207599 int nExtra,
207600 int bInsert
207601){
207602 if( *pRc==SQLITE_OK ){
207603 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
207604 Fts5StructureSegment *aNew;
207605 int nByte;
207606
207607 nByte = (pLvl->nSeg + nExtra) * sizeof(Fts5StructureSegment);
207608 aNew = sqlite3_realloc(pLvl->aSeg, nByte);
207609 if( aNew ){
207610 if( bInsert==0 ){
207611 memset(&aNew[pLvl->nSeg], 0, sizeof(Fts5StructureSegment) * nExtra);
207612 }else{
207613 int nMove = pLvl->nSeg * sizeof(Fts5StructureSegment);
207614 memmove(&aNew[nExtra], aNew, nMove);
207615 memset(aNew, 0, sizeof(Fts5StructureSegment) * nExtra);
207616 }
207617 pLvl->aSeg = aNew;
207618 }else{
207619 *pRc = SQLITE_NOMEM;
207620 }
207621 }
207622}
207623
207624static Fts5Structure *fts5StructureReadUncached(Fts5Index *p){
207625 Fts5Structure *pRet = 0;
207626 Fts5Config *pConfig = p->pConfig;
207627 int iCookie; /* Configuration cookie */
207628 Fts5Data *pData;
207629
207630 pData = fts5DataRead(p, FTS5_STRUCTURE_ROWID);
207631 if( p->rc==SQLITE_OK ){
207632 /* TODO: Do we need this if the leaf-index is appended? Probably... */
207633 memset(&pData->p[pData->nn], 0, FTS5_DATA_PADDING);
207634 p->rc = fts5StructureDecode(pData->p, pData->nn, &iCookie, &pRet);
207635 if( p->rc==SQLITE_OK && pConfig->iCookie!=iCookie ){
207636 p->rc = sqlite3Fts5ConfigLoad(pConfig, iCookie);
207637 }
207638 fts5DataRelease(pData);
207639 if( p->rc!=SQLITE_OK ){
207640 fts5StructureRelease(pRet);
207641 pRet = 0;
207642 }
207643 }
207644
207645 return pRet;
207646}
207647
207648static i64 fts5IndexDataVersion(Fts5Index *p){
207649 i64 iVersion = 0;
207650
207651 if( p->rc==SQLITE_OK ){
207652 if( p->pDataVersion==0 ){
207653 p->rc = fts5IndexPrepareStmt(p, &p->pDataVersion,
207654 sqlite3_mprintf("PRAGMA %Q.data_version", p->pConfig->zDb)
207655 );
207656 if( p->rc ) return 0;
207657 }
207658
207659 if( SQLITE_ROW==sqlite3_step(p->pDataVersion) ){
207660 iVersion = sqlite3_column_int64(p->pDataVersion, 0);
207661 }
207662 p->rc = sqlite3_reset(p->pDataVersion);
207663 }
207664
207665 return iVersion;
207666}
207667
207668/*
207669** Read, deserialize and return the structure record.
207670**
207671** The Fts5Structure.aLevel[] and each Fts5StructureLevel.aSeg[] array
207672** are over-allocated as described for function fts5StructureDecode()
207673** above.
207674**
207675** If an error occurs, NULL is returned and an error code left in the
207676** Fts5Index handle. If an error has already occurred when this function
207677** is called, it is a no-op.
207678*/
207679static Fts5Structure *fts5StructureRead(Fts5Index *p){
207680
207681 if( p->pStruct==0 ){
207682 p->iStructVersion = fts5IndexDataVersion(p);
207683 if( p->rc==SQLITE_OK ){
207684 p->pStruct = fts5StructureReadUncached(p);
207685 }
207686 }
207687
207688#if 0
207689 else{
207690 Fts5Structure *pTest = fts5StructureReadUncached(p);
207691 if( pTest ){
207692 int i, j;
207693 assert_nc( p->pStruct->nSegment==pTest->nSegment );
207694 assert_nc( p->pStruct->nLevel==pTest->nLevel );
207695 for(i=0; i<pTest->nLevel; i++){
207696 assert_nc( p->pStruct->aLevel[i].nMerge==pTest->aLevel[i].nMerge );
207697 assert_nc( p->pStruct->aLevel[i].nSeg==pTest->aLevel[i].nSeg );
207698 for(j=0; j<pTest->aLevel[i].nSeg; j++){
207699 Fts5StructureSegment *p1 = &pTest->aLevel[i].aSeg[j];
207700 Fts5StructureSegment *p2 = &p->pStruct->aLevel[i].aSeg[j];
207701 assert_nc( p1->iSegid==p2->iSegid );
207702 assert_nc( p1->pgnoFirst==p2->pgnoFirst );
207703 assert_nc( p1->pgnoLast==p2->pgnoLast );
207704 }
207705 }
207706 fts5StructureRelease(pTest);
207707 }
207708 }
207709#endif
207710
207711 if( p->rc!=SQLITE_OK ) return 0;
207712 assert( p->iStructVersion!=0 );
207713 assert( p->pStruct!=0 );
207714 fts5StructureRef(p->pStruct);
207715 return p->pStruct;
207716}
207717
207718static void fts5StructureInvalidate(Fts5Index *p){
207719 if( p->pStruct ){
207720 fts5StructureRelease(p->pStruct);
207721 p->pStruct = 0;
207722 }
207723}
207724
207725/*
207726** Return the total number of segments in index structure pStruct. This
207727** function is only ever used as part of assert() conditions.
207728*/
207729#ifdef SQLITE_DEBUG
207730static int fts5StructureCountSegments(Fts5Structure *pStruct){
207731 int nSegment = 0; /* Total number of segments */
207732 if( pStruct ){
207733 int iLvl; /* Used to iterate through levels */
207734 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
207735 nSegment += pStruct->aLevel[iLvl].nSeg;
207736 }
207737 }
207738
207739 return nSegment;
207740}
207741#endif
207742
207743#define fts5BufferSafeAppendBlob(pBuf, pBlob, nBlob) { \
207744 assert( (pBuf)->nSpace>=((pBuf)->n+nBlob) ); \
207745 memcpy(&(pBuf)->p[(pBuf)->n], pBlob, nBlob); \
207746 (pBuf)->n += nBlob; \
207747}
207748
207749#define fts5BufferSafeAppendVarint(pBuf, iVal) { \
207750 (pBuf)->n += sqlite3Fts5PutVarint(&(pBuf)->p[(pBuf)->n], (iVal)); \
207751 assert( (pBuf)->nSpace>=(pBuf)->n ); \
207752}
207753
207754
207755/*
207756** Serialize and store the "structure" record.
207757**
207758** If an error occurs, leave an error code in the Fts5Index object. If an
207759** error has already occurred, this function is a no-op.
207760*/
207761static void fts5StructureWrite(Fts5Index *p, Fts5Structure *pStruct){
207762 if( p->rc==SQLITE_OK ){
207763 Fts5Buffer buf; /* Buffer to serialize record into */
207764 int iLvl; /* Used to iterate through levels */
207765 int iCookie; /* Cookie value to store */
207766
207767 assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) );
207768 memset(&buf, 0, sizeof(Fts5Buffer));
207769
207770 /* Append the current configuration cookie */
207771 iCookie = p->pConfig->iCookie;
207772 if( iCookie<0 ) iCookie = 0;
207773
207774 if( 0==sqlite3Fts5BufferSize(&p->rc, &buf, 4+9+9+9) ){
207775 sqlite3Fts5Put32(buf.p, iCookie);
207776 buf.n = 4;
207777 fts5BufferSafeAppendVarint(&buf, pStruct->nLevel);
207778 fts5BufferSafeAppendVarint(&buf, pStruct->nSegment);
207779 fts5BufferSafeAppendVarint(&buf, (i64)pStruct->nWriteCounter);
207780 }
207781
207782 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
207783 int iSeg; /* Used to iterate through segments */
207784 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
207785 fts5BufferAppendVarint(&p->rc, &buf, pLvl->nMerge);
207786 fts5BufferAppendVarint(&p->rc, &buf, pLvl->nSeg);
207787 assert( pLvl->nMerge<=pLvl->nSeg );
207788
207789 for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
207790 fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].iSegid);
207791 fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].pgnoFirst);
207792 fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].pgnoLast);
207793 }
207794 }
207795
207796 fts5DataWrite(p, FTS5_STRUCTURE_ROWID, buf.p, buf.n);
207797 fts5BufferFree(&buf);
207798 }
207799}
207800
207801#if 0
207802static void fts5DebugStructure(int*,Fts5Buffer*,Fts5Structure*);
207803static void fts5PrintStructure(const char *zCaption, Fts5Structure *pStruct){
207804 int rc = SQLITE_OK;
207805 Fts5Buffer buf;
207806 memset(&buf, 0, sizeof(buf));
207807 fts5DebugStructure(&rc, &buf, pStruct);
207808 fprintf(stdout, "%s: %s\n", zCaption, buf.p);
207809 fflush(stdout);
207810 fts5BufferFree(&buf);
207811}
207812#else
207813# define fts5PrintStructure(x,y)
207814#endif
207815
207816static int fts5SegmentSize(Fts5StructureSegment *pSeg){
207817 return 1 + pSeg->pgnoLast - pSeg->pgnoFirst;
207818}
207819
207820/*
207821** Return a copy of index structure pStruct. Except, promote as many
207822** segments as possible to level iPromote. If an OOM occurs, NULL is
207823** returned.
207824*/
207825static void fts5StructurePromoteTo(
207826 Fts5Index *p,
207827 int iPromote,
207828 int szPromote,
207829 Fts5Structure *pStruct
207830){
207831 int il, is;
207832 Fts5StructureLevel *pOut = &pStruct->aLevel[iPromote];
207833
207834 if( pOut->nMerge==0 ){
207835 for(il=iPromote+1; il<pStruct->nLevel; il++){
207836 Fts5StructureLevel *pLvl = &pStruct->aLevel[il];
207837 if( pLvl->nMerge ) return;
207838 for(is=pLvl->nSeg-1; is>=0; is--){
207839 int sz = fts5SegmentSize(&pLvl->aSeg[is]);
207840 if( sz>szPromote ) return;
207841 fts5StructureExtendLevel(&p->rc, pStruct, iPromote, 1, 1);
207842 if( p->rc ) return;
207843 memcpy(pOut->aSeg, &pLvl->aSeg[is], sizeof(Fts5StructureSegment));
207844 pOut->nSeg++;
207845 pLvl->nSeg--;
207846 }
207847 }
207848 }
207849}
207850
207851/*
207852** A new segment has just been written to level iLvl of index structure
207853** pStruct. This function determines if any segments should be promoted
207854** as a result. Segments are promoted in two scenarios:
207855**
207856** a) If the segment just written is smaller than one or more segments
207857** within the previous populated level, it is promoted to the previous
207858** populated level.
207859**
207860** b) If the segment just written is larger than the newest segment on
207861** the next populated level, then that segment, and any other adjacent
207862** segments that are also smaller than the one just written, are
207863** promoted.
207864**
207865** If one or more segments are promoted, the structure object is updated
207866** to reflect this.
207867*/
207868static void fts5StructurePromote(
207869 Fts5Index *p, /* FTS5 backend object */
207870 int iLvl, /* Index level just updated */
207871 Fts5Structure *pStruct /* Index structure */
207872){
207873 if( p->rc==SQLITE_OK ){
207874 int iTst;
207875 int iPromote = -1;
207876 int szPromote = 0; /* Promote anything this size or smaller */
207877 Fts5StructureSegment *pSeg; /* Segment just written */
207878 int szSeg; /* Size of segment just written */
207879 int nSeg = pStruct->aLevel[iLvl].nSeg;
207880
207881 if( nSeg==0 ) return;
207882 pSeg = &pStruct->aLevel[iLvl].aSeg[pStruct->aLevel[iLvl].nSeg-1];
207883 szSeg = (1 + pSeg->pgnoLast - pSeg->pgnoFirst);
207884
207885 /* Check for condition (a) */
207886 for(iTst=iLvl-1; iTst>=0 && pStruct->aLevel[iTst].nSeg==0; iTst--);
207887 if( iTst>=0 ){
207888 int i;
207889 int szMax = 0;
207890 Fts5StructureLevel *pTst = &pStruct->aLevel[iTst];
207891 assert( pTst->nMerge==0 );
207892 for(i=0; i<pTst->nSeg; i++){
207893 int sz = pTst->aSeg[i].pgnoLast - pTst->aSeg[i].pgnoFirst + 1;
207894 if( sz>szMax ) szMax = sz;
207895 }
207896 if( szMax>=szSeg ){
207897 /* Condition (a) is true. Promote the newest segment on level
207898 ** iLvl to level iTst. */
207899 iPromote = iTst;
207900 szPromote = szMax;
207901 }
207902 }
207903
207904 /* If condition (a) is not met, assume (b) is true. StructurePromoteTo()
207905 ** is a no-op if it is not. */
207906 if( iPromote<0 ){
207907 iPromote = iLvl;
207908 szPromote = szSeg;
207909 }
207910 fts5StructurePromoteTo(p, iPromote, szPromote, pStruct);
207911 }
207912}
207913
207914
207915/*
207916** Advance the iterator passed as the only argument. If the end of the
207917** doclist-index page is reached, return non-zero.
207918*/
207919static int fts5DlidxLvlNext(Fts5DlidxLvl *pLvl){
207920 Fts5Data *pData = pLvl->pData;
207921
207922 if( pLvl->iOff==0 ){
207923 assert( pLvl->bEof==0 );
207924 pLvl->iOff = 1;
207925 pLvl->iOff += fts5GetVarint32(&pData->p[1], pLvl->iLeafPgno);
207926 pLvl->iOff += fts5GetVarint(&pData->p[pLvl->iOff], (u64*)&pLvl->iRowid);
207927 pLvl->iFirstOff = pLvl->iOff;
207928 }else{
207929 int iOff;
207930 for(iOff=pLvl->iOff; iOff<pData->nn; iOff++){
207931 if( pData->p[iOff] ) break;
207932 }
207933
207934 if( iOff<pData->nn ){
207935 i64 iVal;
207936 pLvl->iLeafPgno += (iOff - pLvl->iOff) + 1;
207937 iOff += fts5GetVarint(&pData->p[iOff], (u64*)&iVal);
207938 pLvl->iRowid += iVal;
207939 pLvl->iOff = iOff;
207940 }else{
207941 pLvl->bEof = 1;
207942 }
207943 }
207944
207945 return pLvl->bEof;
207946}
207947
207948/*
207949** Advance the iterator passed as the only argument.
207950*/
207951static int fts5DlidxIterNextR(Fts5Index *p, Fts5DlidxIter *pIter, int iLvl){
207952 Fts5DlidxLvl *pLvl = &pIter->aLvl[iLvl];
207953
207954 assert( iLvl<pIter->nLvl );
207955 if( fts5DlidxLvlNext(pLvl) ){
207956 if( (iLvl+1) < pIter->nLvl ){
207957 fts5DlidxIterNextR(p, pIter, iLvl+1);
207958 if( pLvl[1].bEof==0 ){
207959 fts5DataRelease(pLvl->pData);
207960 memset(pLvl, 0, sizeof(Fts5DlidxLvl));
207961 pLvl->pData = fts5DataRead(p,
207962 FTS5_DLIDX_ROWID(pIter->iSegid, iLvl, pLvl[1].iLeafPgno)
207963 );
207964 if( pLvl->pData ) fts5DlidxLvlNext(pLvl);
207965 }
207966 }
207967 }
207968
207969 return pIter->aLvl[0].bEof;
207970}
207971static int fts5DlidxIterNext(Fts5Index *p, Fts5DlidxIter *pIter){
207972 return fts5DlidxIterNextR(p, pIter, 0);
207973}
207974
207975/*
207976** The iterator passed as the first argument has the following fields set
207977** as follows. This function sets up the rest of the iterator so that it
207978** points to the first rowid in the doclist-index.
207979**
207980** pData:
207981** pointer to doclist-index record,
207982**
207983** When this function is called pIter->iLeafPgno is the page number the
207984** doclist is associated with (the one featuring the term).
207985*/
207986static int fts5DlidxIterFirst(Fts5DlidxIter *pIter){
207987 int i;
207988 for(i=0; i<pIter->nLvl; i++){
207989 fts5DlidxLvlNext(&pIter->aLvl[i]);
207990 }
207991 return pIter->aLvl[0].bEof;
207992}
207993
207994
207995static int fts5DlidxIterEof(Fts5Index *p, Fts5DlidxIter *pIter){
207996 return p->rc!=SQLITE_OK || pIter->aLvl[0].bEof;
207997}
207998
207999static void fts5DlidxIterLast(Fts5Index *p, Fts5DlidxIter *pIter){
208000 int i;
208001
208002 /* Advance each level to the last entry on the last page */
208003 for(i=pIter->nLvl-1; p->rc==SQLITE_OK && i>=0; i--){
208004 Fts5DlidxLvl *pLvl = &pIter->aLvl[i];
208005 while( fts5DlidxLvlNext(pLvl)==0 );
208006 pLvl->bEof = 0;
208007
208008 if( i>0 ){
208009 Fts5DlidxLvl *pChild = &pLvl[-1];
208010 fts5DataRelease(pChild->pData);
208011 memset(pChild, 0, sizeof(Fts5DlidxLvl));
208012 pChild->pData = fts5DataRead(p,
208013 FTS5_DLIDX_ROWID(pIter->iSegid, i-1, pLvl->iLeafPgno)
208014 );
208015 }
208016 }
208017}
208018
208019/*
208020** Move the iterator passed as the only argument to the previous entry.
208021*/
208022static int fts5DlidxLvlPrev(Fts5DlidxLvl *pLvl){
208023 int iOff = pLvl->iOff;
208024
208025 assert( pLvl->bEof==0 );
208026 if( iOff<=pLvl->iFirstOff ){
208027 pLvl->bEof = 1;
208028 }else{
208029 u8 *a = pLvl->pData->p;
208030 i64 iVal;
208031 int iLimit;
208032 int ii;
208033 int nZero = 0;
208034
208035 /* Currently iOff points to the first byte of a varint. This block
208036 ** decrements iOff until it points to the first byte of the previous
208037 ** varint. Taking care not to read any memory locations that occur
208038 ** before the buffer in memory. */
208039 iLimit = (iOff>9 ? iOff-9 : 0);
208040 for(iOff--; iOff>iLimit; iOff--){
208041 if( (a[iOff-1] & 0x80)==0 ) break;
208042 }
208043
208044 fts5GetVarint(&a[iOff], (u64*)&iVal);
208045 pLvl->iRowid -= iVal;
208046 pLvl->iLeafPgno--;
208047
208048 /* Skip backwards past any 0x00 varints. */
208049 for(ii=iOff-1; ii>=pLvl->iFirstOff && a[ii]==0x00; ii--){
208050 nZero++;
208051 }
208052 if( ii>=pLvl->iFirstOff && (a[ii] & 0x80) ){
208053 /* The byte immediately before the last 0x00 byte has the 0x80 bit
208054 ** set. So the last 0x00 is only a varint 0 if there are 8 more 0x80
208055 ** bytes before a[ii]. */
208056 int bZero = 0; /* True if last 0x00 counts */
208057 if( (ii-8)>=pLvl->iFirstOff ){
208058 int j;
208059 for(j=1; j<=8 && (a[ii-j] & 0x80); j++);
208060 bZero = (j>8);
208061 }
208062 if( bZero==0 ) nZero--;
208063 }
208064 pLvl->iLeafPgno -= nZero;
208065 pLvl->iOff = iOff - nZero;
208066 }
208067
208068 return pLvl->bEof;
208069}
208070
208071static int fts5DlidxIterPrevR(Fts5Index *p, Fts5DlidxIter *pIter, int iLvl){
208072 Fts5DlidxLvl *pLvl = &pIter->aLvl[iLvl];
208073
208074 assert( iLvl<pIter->nLvl );
208075 if( fts5DlidxLvlPrev(pLvl) ){
208076 if( (iLvl+1) < pIter->nLvl ){
208077 fts5DlidxIterPrevR(p, pIter, iLvl+1);
208078 if( pLvl[1].bEof==0 ){
208079 fts5DataRelease(pLvl->pData);
208080 memset(pLvl, 0, sizeof(Fts5DlidxLvl));
208081 pLvl->pData = fts5DataRead(p,
208082 FTS5_DLIDX_ROWID(pIter->iSegid, iLvl, pLvl[1].iLeafPgno)
208083 );
208084 if( pLvl->pData ){
208085 while( fts5DlidxLvlNext(pLvl)==0 );
208086 pLvl->bEof = 0;
208087 }
208088 }
208089 }
208090 }
208091
208092 return pIter->aLvl[0].bEof;
208093}
208094static int fts5DlidxIterPrev(Fts5Index *p, Fts5DlidxIter *pIter){
208095 return fts5DlidxIterPrevR(p, pIter, 0);
208096}
208097
208098/*
208099** Free a doclist-index iterator object allocated by fts5DlidxIterInit().
208100*/
208101static void fts5DlidxIterFree(Fts5DlidxIter *pIter){
208102 if( pIter ){
208103 int i;
208104 for(i=0; i<pIter->nLvl; i++){
208105 fts5DataRelease(pIter->aLvl[i].pData);
208106 }
208107 sqlite3_free(pIter);
208108 }
208109}
208110
208111static Fts5DlidxIter *fts5DlidxIterInit(
208112 Fts5Index *p, /* Fts5 Backend to iterate within */
208113 int bRev, /* True for ORDER BY ASC */
208114 int iSegid, /* Segment id */
208115 int iLeafPg /* Leaf page number to load dlidx for */
208116){
208117 Fts5DlidxIter *pIter = 0;
208118 int i;
208119 int bDone = 0;
208120
208121 for(i=0; p->rc==SQLITE_OK && bDone==0; i++){
208122 int nByte = sizeof(Fts5DlidxIter) + i * sizeof(Fts5DlidxLvl);
208123 Fts5DlidxIter *pNew;
208124
208125 pNew = (Fts5DlidxIter*)sqlite3_realloc(pIter, nByte);
208126 if( pNew==0 ){
208127 p->rc = SQLITE_NOMEM;
208128 }else{
208129 i64 iRowid = FTS5_DLIDX_ROWID(iSegid, i, iLeafPg);
208130 Fts5DlidxLvl *pLvl = &pNew->aLvl[i];
208131 pIter = pNew;
208132 memset(pLvl, 0, sizeof(Fts5DlidxLvl));
208133 pLvl->pData = fts5DataRead(p, iRowid);
208134 if( pLvl->pData && (pLvl->pData->p[0] & 0x0001)==0 ){
208135 bDone = 1;
208136 }
208137 pIter->nLvl = i+1;
208138 }
208139 }
208140
208141 if( p->rc==SQLITE_OK ){
208142 pIter->iSegid = iSegid;
208143 if( bRev==0 ){
208144 fts5DlidxIterFirst(pIter);
208145 }else{
208146 fts5DlidxIterLast(p, pIter);
208147 }
208148 }
208149
208150 if( p->rc!=SQLITE_OK ){
208151 fts5DlidxIterFree(pIter);
208152 pIter = 0;
208153 }
208154
208155 return pIter;
208156}
208157
208158static i64 fts5DlidxIterRowid(Fts5DlidxIter *pIter){
208159 return pIter->aLvl[0].iRowid;
208160}
208161static int fts5DlidxIterPgno(Fts5DlidxIter *pIter){
208162 return pIter->aLvl[0].iLeafPgno;
208163}
208164
208165/*
208166** Load the next leaf page into the segment iterator.
208167*/
208168static void fts5SegIterNextPage(
208169 Fts5Index *p, /* FTS5 backend object */
208170 Fts5SegIter *pIter /* Iterator to advance to next page */
208171){
208172 Fts5Data *pLeaf;
208173 Fts5StructureSegment *pSeg = pIter->pSeg;
208174 fts5DataRelease(pIter->pLeaf);
208175 pIter->iLeafPgno++;
208176 if( pIter->pNextLeaf ){
208177 pIter->pLeaf = pIter->pNextLeaf;
208178 pIter->pNextLeaf = 0;
208179 }else if( pIter->iLeafPgno<=pSeg->pgnoLast ){
208180 pIter->pLeaf = fts5LeafRead(p,
208181 FTS5_SEGMENT_ROWID(pSeg->iSegid, pIter->iLeafPgno)
208182 );
208183 }else{
208184 pIter->pLeaf = 0;
208185 }
208186 pLeaf = pIter->pLeaf;
208187
208188 if( pLeaf ){
208189 pIter->iPgidxOff = pLeaf->szLeaf;
208190 if( fts5LeafIsTermless(pLeaf) ){
208191 pIter->iEndofDoclist = pLeaf->nn+1;
208192 }else{
208193 pIter->iPgidxOff += fts5GetVarint32(&pLeaf->p[pIter->iPgidxOff],
208194 pIter->iEndofDoclist
208195 );
208196 }
208197 }
208198}
208199
208200/*
208201** Argument p points to a buffer containing a varint to be interpreted as a
208202** position list size field. Read the varint and return the number of bytes
208203** read. Before returning, set *pnSz to the number of bytes in the position
208204** list, and *pbDel to true if the delete flag is set, or false otherwise.
208205*/
208206static int fts5GetPoslistSize(const u8 *p, int *pnSz, int *pbDel){
208207 int nSz;
208208 int n = 0;
208209 fts5FastGetVarint32(p, n, nSz);
208210 assert_nc( nSz>=0 );
208211 *pnSz = nSz/2;
208212 *pbDel = nSz & 0x0001;
208213 return n;
208214}
208215
208216/*
208217** Fts5SegIter.iLeafOffset currently points to the first byte of a
208218** position-list size field. Read the value of the field and store it
208219** in the following variables:
208220**
208221** Fts5SegIter.nPos
208222** Fts5SegIter.bDel
208223**
208224** Leave Fts5SegIter.iLeafOffset pointing to the first byte of the
208225** position list content (if any).
208226*/
208227static void fts5SegIterLoadNPos(Fts5Index *p, Fts5SegIter *pIter){
208228 if( p->rc==SQLITE_OK ){
208229 int iOff = pIter->iLeafOffset; /* Offset to read at */
208230 ASSERT_SZLEAF_OK(pIter->pLeaf);
208231 if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
208232 int iEod = MIN(pIter->iEndofDoclist, pIter->pLeaf->szLeaf);
208233 pIter->bDel = 0;
208234 pIter->nPos = 1;
208235 if( iOff<iEod && pIter->pLeaf->p[iOff]==0 ){
208236 pIter->bDel = 1;
208237 iOff++;
208238 if( iOff<iEod && pIter->pLeaf->p[iOff]==0 ){
208239 pIter->nPos = 1;
208240 iOff++;
208241 }else{
208242 pIter->nPos = 0;
208243 }
208244 }
208245 }else{
208246 int nSz;
208247 fts5FastGetVarint32(pIter->pLeaf->p, iOff, nSz);
208248 pIter->bDel = (nSz & 0x0001);
208249 pIter->nPos = nSz>>1;
208250 assert_nc( pIter->nPos>=0 );
208251 }
208252 pIter->iLeafOffset = iOff;
208253 }
208254}
208255
208256static void fts5SegIterLoadRowid(Fts5Index *p, Fts5SegIter *pIter){
208257 u8 *a = pIter->pLeaf->p; /* Buffer to read data from */
208258 int iOff = pIter->iLeafOffset;
208259
208260 ASSERT_SZLEAF_OK(pIter->pLeaf);
208261 if( iOff>=pIter->pLeaf->szLeaf ){
208262 fts5SegIterNextPage(p, pIter);
208263 if( pIter->pLeaf==0 ){
208264 if( p->rc==SQLITE_OK ) p->rc = FTS5_CORRUPT;
208265 return;
208266 }
208267 iOff = 4;
208268 a = pIter->pLeaf->p;
208269 }
208270 iOff += sqlite3Fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
208271 pIter->iLeafOffset = iOff;
208272}
208273
208274/*
208275** Fts5SegIter.iLeafOffset currently points to the first byte of the
208276** "nSuffix" field of a term. Function parameter nKeep contains the value
208277** of the "nPrefix" field (if there was one - it is passed 0 if this is
208278** the first term in the segment).
208279**
208280** This function populates:
208281**
208282** Fts5SegIter.term
208283** Fts5SegIter.rowid
208284**
208285** accordingly and leaves (Fts5SegIter.iLeafOffset) set to the content of
208286** the first position list. The position list belonging to document
208287** (Fts5SegIter.iRowid).
208288*/
208289static void fts5SegIterLoadTerm(Fts5Index *p, Fts5SegIter *pIter, int nKeep){
208290 u8 *a = pIter->pLeaf->p; /* Buffer to read data from */
208291 int iOff = pIter->iLeafOffset; /* Offset to read at */
208292 int nNew; /* Bytes of new data */
208293
208294 iOff += fts5GetVarint32(&a[iOff], nNew);
208295 if( iOff+nNew>pIter->pLeaf->nn ){
208296 p->rc = FTS5_CORRUPT;
208297 return;
208298 }
208299 pIter->term.n = nKeep;
208300 fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff]);
208301 iOff += nNew;
208302 pIter->iTermLeafOffset = iOff;
208303 pIter->iTermLeafPgno = pIter->iLeafPgno;
208304 pIter->iLeafOffset = iOff;
208305
208306 if( pIter->iPgidxOff>=pIter->pLeaf->nn ){
208307 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
208308 }else{
208309 int nExtra;
208310 pIter->iPgidxOff += fts5GetVarint32(&a[pIter->iPgidxOff], nExtra);
208311 pIter->iEndofDoclist += nExtra;
208312 }
208313
208314 fts5SegIterLoadRowid(p, pIter);
208315}
208316
208317static void fts5SegIterNext(Fts5Index*, Fts5SegIter*, int*);
208318static void fts5SegIterNext_Reverse(Fts5Index*, Fts5SegIter*, int*);
208319static void fts5SegIterNext_None(Fts5Index*, Fts5SegIter*, int*);
208320
208321static void fts5SegIterSetNext(Fts5Index *p, Fts5SegIter *pIter){
208322 if( pIter->flags & FTS5_SEGITER_REVERSE ){
208323 pIter->xNext = fts5SegIterNext_Reverse;
208324 }else if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
208325 pIter->xNext = fts5SegIterNext_None;
208326 }else{
208327 pIter->xNext = fts5SegIterNext;
208328 }
208329}
208330
208331/*
208332** Initialize the iterator object pIter to iterate through the entries in
208333** segment pSeg. The iterator is left pointing to the first entry when
208334** this function returns.
208335**
208336** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
208337** an error has already occurred when this function is called, it is a no-op.
208338*/
208339static void fts5SegIterInit(
208340 Fts5Index *p, /* FTS index object */
208341 Fts5StructureSegment *pSeg, /* Description of segment */
208342 Fts5SegIter *pIter /* Object to populate */
208343){
208344 if( pSeg->pgnoFirst==0 ){
208345 /* This happens if the segment is being used as an input to an incremental
208346 ** merge and all data has already been "trimmed". See function
208347 ** fts5TrimSegments() for details. In this case leave the iterator empty.
208348 ** The caller will see the (pIter->pLeaf==0) and assume the iterator is
208349 ** at EOF already. */
208350 assert( pIter->pLeaf==0 );
208351 return;
208352 }
208353
208354 if( p->rc==SQLITE_OK ){
208355 memset(pIter, 0, sizeof(*pIter));
208356 fts5SegIterSetNext(p, pIter);
208357 pIter->pSeg = pSeg;
208358 pIter->iLeafPgno = pSeg->pgnoFirst-1;
208359 fts5SegIterNextPage(p, pIter);
208360 }
208361
208362 if( p->rc==SQLITE_OK ){
208363 pIter->iLeafOffset = 4;
208364 assert_nc( pIter->pLeaf->nn>4 );
208365 assert( fts5LeafFirstTermOff(pIter->pLeaf)==4 );
208366 pIter->iPgidxOff = pIter->pLeaf->szLeaf+1;
208367 fts5SegIterLoadTerm(p, pIter, 0);
208368 fts5SegIterLoadNPos(p, pIter);
208369 }
208370}
208371
208372/*
208373** This function is only ever called on iterators created by calls to
208374** Fts5IndexQuery() with the FTS5INDEX_QUERY_DESC flag set.
208375**
208376** The iterator is in an unusual state when this function is called: the
208377** Fts5SegIter.iLeafOffset variable is set to the offset of the start of
208378** the position-list size field for the first relevant rowid on the page.
208379** Fts5SegIter.rowid is set, but nPos and bDel are not.
208380**
208381** This function advances the iterator so that it points to the last
208382** relevant rowid on the page and, if necessary, initializes the
208383** aRowidOffset[] and iRowidOffset variables. At this point the iterator
208384** is in its regular state - Fts5SegIter.iLeafOffset points to the first
208385** byte of the position list content associated with said rowid.
208386*/
208387static void fts5SegIterReverseInitPage(Fts5Index *p, Fts5SegIter *pIter){
208388 int eDetail = p->pConfig->eDetail;
208389 int n = pIter->pLeaf->szLeaf;
208390 int i = pIter->iLeafOffset;
208391 u8 *a = pIter->pLeaf->p;
208392 int iRowidOffset = 0;
208393
208394 if( n>pIter->iEndofDoclist ){
208395 n = pIter->iEndofDoclist;
208396 }
208397
208398 ASSERT_SZLEAF_OK(pIter->pLeaf);
208399 while( 1 ){
208400 i64 iDelta = 0;
208401
208402 if( eDetail==FTS5_DETAIL_NONE ){
208403 /* todo */
208404 if( i<n && a[i]==0 ){
208405 i++;
208406 if( i<n && a[i]==0 ) i++;
208407 }
208408 }else{
208409 int nPos;
208410 int bDummy;
208411 i += fts5GetPoslistSize(&a[i], &nPos, &bDummy);
208412 i += nPos;
208413 }
208414 if( i>=n ) break;
208415 i += fts5GetVarint(&a[i], (u64*)&iDelta);
208416 pIter->iRowid += iDelta;
208417
208418 /* If necessary, grow the pIter->aRowidOffset[] array. */
208419 if( iRowidOffset>=pIter->nRowidOffset ){
208420 int nNew = pIter->nRowidOffset + 8;
208421 int *aNew = (int*)sqlite3_realloc(pIter->aRowidOffset, nNew*sizeof(int));
208422 if( aNew==0 ){
208423 p->rc = SQLITE_NOMEM;
208424 break;
208425 }
208426 pIter->aRowidOffset = aNew;
208427 pIter->nRowidOffset = nNew;
208428 }
208429
208430 pIter->aRowidOffset[iRowidOffset++] = pIter->iLeafOffset;
208431 pIter->iLeafOffset = i;
208432 }
208433 pIter->iRowidOffset = iRowidOffset;
208434 fts5SegIterLoadNPos(p, pIter);
208435}
208436
208437/*
208438**
208439*/
208440static void fts5SegIterReverseNewPage(Fts5Index *p, Fts5SegIter *pIter){
208441 assert( pIter->flags & FTS5_SEGITER_REVERSE );
208442 assert( pIter->flags & FTS5_SEGITER_ONETERM );
208443
208444 fts5DataRelease(pIter->pLeaf);
208445 pIter->pLeaf = 0;
208446 while( p->rc==SQLITE_OK && pIter->iLeafPgno>pIter->iTermLeafPgno ){
208447 Fts5Data *pNew;
208448 pIter->iLeafPgno--;
208449 pNew = fts5DataRead(p, FTS5_SEGMENT_ROWID(
208450 pIter->pSeg->iSegid, pIter->iLeafPgno
208451 ));
208452 if( pNew ){
208453 /* iTermLeafOffset may be equal to szLeaf if the term is the last
208454 ** thing on the page - i.e. the first rowid is on the following page.
208455 ** In this case leave pIter->pLeaf==0, this iterator is at EOF. */
208456 if( pIter->iLeafPgno==pIter->iTermLeafPgno ){
208457 assert( pIter->pLeaf==0 );
208458 if( pIter->iTermLeafOffset<pNew->szLeaf ){
208459 pIter->pLeaf = pNew;
208460 pIter->iLeafOffset = pIter->iTermLeafOffset;
208461 }
208462 }else{
208463 int iRowidOff;
208464 iRowidOff = fts5LeafFirstRowidOff(pNew);
208465 if( iRowidOff ){
208466 pIter->pLeaf = pNew;
208467 pIter->iLeafOffset = iRowidOff;
208468 }
208469 }
208470
208471 if( pIter->pLeaf ){
208472 u8 *a = &pIter->pLeaf->p[pIter->iLeafOffset];
208473 pIter->iLeafOffset += fts5GetVarint(a, (u64*)&pIter->iRowid);
208474 break;
208475 }else{
208476 fts5DataRelease(pNew);
208477 }
208478 }
208479 }
208480
208481 if( pIter->pLeaf ){
208482 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
208483 fts5SegIterReverseInitPage(p, pIter);
208484 }
208485}
208486
208487/*
208488** Return true if the iterator passed as the second argument currently
208489** points to a delete marker. A delete marker is an entry with a 0 byte
208490** position-list.
208491*/
208492static int fts5MultiIterIsEmpty(Fts5Index *p, Fts5Iter *pIter){
208493 Fts5SegIter *pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
208494 return (p->rc==SQLITE_OK && pSeg->pLeaf && pSeg->nPos==0);
208495}
208496
208497/*
208498** Advance iterator pIter to the next entry.
208499**
208500** This version of fts5SegIterNext() is only used by reverse iterators.
208501*/
208502static void fts5SegIterNext_Reverse(
208503 Fts5Index *p, /* FTS5 backend object */
208504 Fts5SegIter *pIter, /* Iterator to advance */
208505 int *pbUnused /* Unused */
208506){
208507 assert( pIter->flags & FTS5_SEGITER_REVERSE );
208508 assert( pIter->pNextLeaf==0 );
208509 UNUSED_PARAM(pbUnused);
208510
208511 if( pIter->iRowidOffset>0 ){
208512 u8 *a = pIter->pLeaf->p;
208513 int iOff;
208514 i64 iDelta;
208515
208516 pIter->iRowidOffset--;
208517 pIter->iLeafOffset = pIter->aRowidOffset[pIter->iRowidOffset];
208518 fts5SegIterLoadNPos(p, pIter);
208519 iOff = pIter->iLeafOffset;
208520 if( p->pConfig->eDetail!=FTS5_DETAIL_NONE ){
208521 iOff += pIter->nPos;
208522 }
208523 fts5GetVarint(&a[iOff], (u64*)&iDelta);
208524 pIter->iRowid -= iDelta;
208525 }else{
208526 fts5SegIterReverseNewPage(p, pIter);
208527 }
208528}
208529
208530/*
208531** Advance iterator pIter to the next entry.
208532**
208533** This version of fts5SegIterNext() is only used if detail=none and the
208534** iterator is not a reverse direction iterator.
208535*/
208536static void fts5SegIterNext_None(
208537 Fts5Index *p, /* FTS5 backend object */
208538 Fts5SegIter *pIter, /* Iterator to advance */
208539 int *pbNewTerm /* OUT: Set for new term */
208540){
208541 int iOff;
208542
208543 assert( p->rc==SQLITE_OK );
208544 assert( (pIter->flags & FTS5_SEGITER_REVERSE)==0 );
208545 assert( p->pConfig->eDetail==FTS5_DETAIL_NONE );
208546
208547 ASSERT_SZLEAF_OK(pIter->pLeaf);
208548 iOff = pIter->iLeafOffset;
208549
208550 /* Next entry is on the next page */
208551 if( pIter->pSeg && iOff>=pIter->pLeaf->szLeaf ){
208552 fts5SegIterNextPage(p, pIter);
208553 if( p->rc || pIter->pLeaf==0 ) return;
208554 pIter->iRowid = 0;
208555 iOff = 4;
208556 }
208557
208558 if( iOff<pIter->iEndofDoclist ){
208559 /* Next entry is on the current page */
208560 i64 iDelta;
208561 iOff += sqlite3Fts5GetVarint(&pIter->pLeaf->p[iOff], (u64*)&iDelta);
208562 pIter->iLeafOffset = iOff;
208563 pIter->iRowid += iDelta;
208564 }else if( (pIter->flags & FTS5_SEGITER_ONETERM)==0 ){
208565 if( pIter->pSeg ){
208566 int nKeep = 0;
208567 if( iOff!=fts5LeafFirstTermOff(pIter->pLeaf) ){
208568 iOff += fts5GetVarint32(&pIter->pLeaf->p[iOff], nKeep);
208569 }
208570 pIter->iLeafOffset = iOff;
208571 fts5SegIterLoadTerm(p, pIter, nKeep);
208572 }else{
208573 const u8 *pList = 0;
208574 const char *zTerm = 0;
208575 int nList;
208576 sqlite3Fts5HashScanNext(p->pHash);
208577 sqlite3Fts5HashScanEntry(p->pHash, &zTerm, &pList, &nList);
208578 if( pList==0 ) goto next_none_eof;
208579 pIter->pLeaf->p = (u8*)pList;
208580 pIter->pLeaf->nn = nList;
208581 pIter->pLeaf->szLeaf = nList;
208582 pIter->iEndofDoclist = nList;
208583 sqlite3Fts5BufferSet(&p->rc,&pIter->term, (int)strlen(zTerm), (u8*)zTerm);
208584 pIter->iLeafOffset = fts5GetVarint(pList, (u64*)&pIter->iRowid);
208585 }
208586
208587 if( pbNewTerm ) *pbNewTerm = 1;
208588 }else{
208589 goto next_none_eof;
208590 }
208591
208592 fts5SegIterLoadNPos(p, pIter);
208593
208594 return;
208595 next_none_eof:
208596 fts5DataRelease(pIter->pLeaf);
208597 pIter->pLeaf = 0;
208598}
208599
208600
208601/*
208602** Advance iterator pIter to the next entry.
208603**
208604** If an error occurs, Fts5Index.rc is set to an appropriate error code. It
208605** is not considered an error if the iterator reaches EOF. If an error has
208606** already occurred when this function is called, it is a no-op.
208607*/
208608static void fts5SegIterNext(
208609 Fts5Index *p, /* FTS5 backend object */
208610 Fts5SegIter *pIter, /* Iterator to advance */
208611 int *pbNewTerm /* OUT: Set for new term */
208612){
208613 Fts5Data *pLeaf = pIter->pLeaf;
208614 int iOff;
208615 int bNewTerm = 0;
208616 int nKeep = 0;
208617 u8 *a;
208618 int n;
208619
208620 assert( pbNewTerm==0 || *pbNewTerm==0 );
208621 assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE );
208622
208623 /* Search for the end of the position list within the current page. */
208624 a = pLeaf->p;
208625 n = pLeaf->szLeaf;
208626
208627 ASSERT_SZLEAF_OK(pLeaf);
208628 iOff = pIter->iLeafOffset + pIter->nPos;
208629
208630 if( iOff<n ){
208631 /* The next entry is on the current page. */
208632 assert_nc( iOff<=pIter->iEndofDoclist );
208633 if( iOff>=pIter->iEndofDoclist ){
208634 bNewTerm = 1;
208635 if( iOff!=fts5LeafFirstTermOff(pLeaf) ){
208636 iOff += fts5GetVarint32(&a[iOff], nKeep);
208637 }
208638 }else{
208639 u64 iDelta;
208640 iOff += sqlite3Fts5GetVarint(&a[iOff], &iDelta);
208641 pIter->iRowid += iDelta;
208642 assert_nc( iDelta>0 );
208643 }
208644 pIter->iLeafOffset = iOff;
208645
208646 }else if( pIter->pSeg==0 ){
208647 const u8 *pList = 0;
208648 const char *zTerm = 0;
208649 int nList = 0;
208650 assert( (pIter->flags & FTS5_SEGITER_ONETERM) || pbNewTerm );
208651 if( 0==(pIter->flags & FTS5_SEGITER_ONETERM) ){
208652 sqlite3Fts5HashScanNext(p->pHash);
208653 sqlite3Fts5HashScanEntry(p->pHash, &zTerm, &pList, &nList);
208654 }
208655 if( pList==0 ){
208656 fts5DataRelease(pIter->pLeaf);
208657 pIter->pLeaf = 0;
208658 }else{
208659 pIter->pLeaf->p = (u8*)pList;
208660 pIter->pLeaf->nn = nList;
208661 pIter->pLeaf->szLeaf = nList;
208662 pIter->iEndofDoclist = nList+1;
208663 sqlite3Fts5BufferSet(&p->rc, &pIter->term, (int)strlen(zTerm),
208664 (u8*)zTerm);
208665 pIter->iLeafOffset = fts5GetVarint(pList, (u64*)&pIter->iRowid);
208666 *pbNewTerm = 1;
208667 }
208668 }else{
208669 iOff = 0;
208670 /* Next entry is not on the current page */
208671 while( iOff==0 ){
208672 fts5SegIterNextPage(p, pIter);
208673 pLeaf = pIter->pLeaf;
208674 if( pLeaf==0 ) break;
208675 ASSERT_SZLEAF_OK(pLeaf);
208676 if( (iOff = fts5LeafFirstRowidOff(pLeaf)) && iOff<pLeaf->szLeaf ){
208677 iOff += sqlite3Fts5GetVarint(&pLeaf->p[iOff], (u64*)&pIter->iRowid);
208678 pIter->iLeafOffset = iOff;
208679
208680 if( pLeaf->nn>pLeaf->szLeaf ){
208681 pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32(
208682 &pLeaf->p[pLeaf->szLeaf], pIter->iEndofDoclist
208683 );
208684 }
208685 }
208686 else if( pLeaf->nn>pLeaf->szLeaf ){
208687 pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32(
208688 &pLeaf->p[pLeaf->szLeaf], iOff
208689 );
208690 pIter->iLeafOffset = iOff;
208691 pIter->iEndofDoclist = iOff;
208692 bNewTerm = 1;
208693 }
208694 assert_nc( iOff<pLeaf->szLeaf );
208695 if( iOff>pLeaf->szLeaf ){
208696 p->rc = FTS5_CORRUPT;
208697 return;
208698 }
208699 }
208700 }
208701
208702 /* Check if the iterator is now at EOF. If so, return early. */
208703 if( pIter->pLeaf ){
208704 if( bNewTerm ){
208705 if( pIter->flags & FTS5_SEGITER_ONETERM ){
208706 fts5DataRelease(pIter->pLeaf);
208707 pIter->pLeaf = 0;
208708 }else{
208709 fts5SegIterLoadTerm(p, pIter, nKeep);
208710 fts5SegIterLoadNPos(p, pIter);
208711 if( pbNewTerm ) *pbNewTerm = 1;
208712 }
208713 }else{
208714 /* The following could be done by calling fts5SegIterLoadNPos(). But
208715 ** this block is particularly performance critical, so equivalent
208716 ** code is inlined.
208717 **
208718 ** Later: Switched back to fts5SegIterLoadNPos() because it supports
208719 ** detail=none mode. Not ideal.
208720 */
208721 int nSz;
208722 assert( p->rc==SQLITE_OK );
208723 assert( pIter->iLeafOffset<=pIter->pLeaf->nn );
208724 fts5FastGetVarint32(pIter->pLeaf->p, pIter->iLeafOffset, nSz);
208725 pIter->bDel = (nSz & 0x0001);
208726 pIter->nPos = nSz>>1;
208727 assert_nc( pIter->nPos>=0 );
208728 }
208729 }
208730}
208731
208732#define SWAPVAL(T, a, b) { T tmp; tmp=a; a=b; b=tmp; }
208733
208734#define fts5IndexSkipVarint(a, iOff) { \
208735 int iEnd = iOff+9; \
208736 while( (a[iOff++] & 0x80) && iOff<iEnd ); \
208737}
208738
208739/*
208740** Iterator pIter currently points to the first rowid in a doclist. This
208741** function sets the iterator up so that iterates in reverse order through
208742** the doclist.
208743*/
208744static void fts5SegIterReverse(Fts5Index *p, Fts5SegIter *pIter){
208745 Fts5DlidxIter *pDlidx = pIter->pDlidx;
208746 Fts5Data *pLast = 0;
208747 int pgnoLast = 0;
208748
208749 if( pDlidx ){
208750 int iSegid = pIter->pSeg->iSegid;
208751 pgnoLast = fts5DlidxIterPgno(pDlidx);
208752 pLast = fts5DataRead(p, FTS5_SEGMENT_ROWID(iSegid, pgnoLast));
208753 }else{
208754 Fts5Data *pLeaf = pIter->pLeaf; /* Current leaf data */
208755
208756 /* Currently, Fts5SegIter.iLeafOffset points to the first byte of
208757 ** position-list content for the current rowid. Back it up so that it
208758 ** points to the start of the position-list size field. */
208759 int iPoslist;
208760 if( pIter->iTermLeafPgno==pIter->iLeafPgno ){
208761 iPoslist = pIter->iTermLeafOffset;
208762 }else{
208763 iPoslist = 4;
208764 }
208765 fts5IndexSkipVarint(pLeaf->p, iPoslist);
208766 pIter->iLeafOffset = iPoslist;
208767
208768 /* If this condition is true then the largest rowid for the current
208769 ** term may not be stored on the current page. So search forward to
208770 ** see where said rowid really is. */
208771 if( pIter->iEndofDoclist>=pLeaf->szLeaf ){
208772 int pgno;
208773 Fts5StructureSegment *pSeg = pIter->pSeg;
208774
208775 /* The last rowid in the doclist may not be on the current page. Search
208776 ** forward to find the page containing the last rowid. */
208777 for(pgno=pIter->iLeafPgno+1; !p->rc && pgno<=pSeg->pgnoLast; pgno++){
208778 i64 iAbs = FTS5_SEGMENT_ROWID(pSeg->iSegid, pgno);
208779 Fts5Data *pNew = fts5DataRead(p, iAbs);
208780 if( pNew ){
208781 int iRowid, bTermless;
208782 iRowid = fts5LeafFirstRowidOff(pNew);
208783 bTermless = fts5LeafIsTermless(pNew);
208784 if( iRowid ){
208785 SWAPVAL(Fts5Data*, pNew, pLast);
208786 pgnoLast = pgno;
208787 }
208788 fts5DataRelease(pNew);
208789 if( bTermless==0 ) break;
208790 }
208791 }
208792 }
208793 }
208794
208795 /* If pLast is NULL at this point, then the last rowid for this doclist
208796 ** lies on the page currently indicated by the iterator. In this case
208797 ** pIter->iLeafOffset is already set to point to the position-list size
208798 ** field associated with the first relevant rowid on the page.
208799 **
208800 ** Or, if pLast is non-NULL, then it is the page that contains the last
208801 ** rowid. In this case configure the iterator so that it points to the
208802 ** first rowid on this page.
208803 */
208804 if( pLast ){
208805 int iOff;
208806 fts5DataRelease(pIter->pLeaf);
208807 pIter->pLeaf = pLast;
208808 pIter->iLeafPgno = pgnoLast;
208809 iOff = fts5LeafFirstRowidOff(pLast);
208810 iOff += fts5GetVarint(&pLast->p[iOff], (u64*)&pIter->iRowid);
208811 pIter->iLeafOffset = iOff;
208812
208813 if( fts5LeafIsTermless(pLast) ){
208814 pIter->iEndofDoclist = pLast->nn+1;
208815 }else{
208816 pIter->iEndofDoclist = fts5LeafFirstTermOff(pLast);
208817 }
208818
208819 }
208820
208821 fts5SegIterReverseInitPage(p, pIter);
208822}
208823
208824/*
208825** Iterator pIter currently points to the first rowid of a doclist.
208826** There is a doclist-index associated with the final term on the current
208827** page. If the current term is the last term on the page, load the
208828** doclist-index from disk and initialize an iterator at (pIter->pDlidx).
208829*/
208830static void fts5SegIterLoadDlidx(Fts5Index *p, Fts5SegIter *pIter){
208831 int iSeg = pIter->pSeg->iSegid;
208832 int bRev = (pIter->flags & FTS5_SEGITER_REVERSE);
208833 Fts5Data *pLeaf = pIter->pLeaf; /* Current leaf data */
208834
208835 assert( pIter->flags & FTS5_SEGITER_ONETERM );
208836 assert( pIter->pDlidx==0 );
208837
208838 /* Check if the current doclist ends on this page. If it does, return
208839 ** early without loading the doclist-index (as it belongs to a different
208840 ** term. */
208841 if( pIter->iTermLeafPgno==pIter->iLeafPgno
208842 && pIter->iEndofDoclist<pLeaf->szLeaf
208843 ){
208844 return;
208845 }
208846
208847 pIter->pDlidx = fts5DlidxIterInit(p, bRev, iSeg, pIter->iTermLeafPgno);
208848}
208849
208850/*
208851** The iterator object passed as the second argument currently contains
208852** no valid values except for the Fts5SegIter.pLeaf member variable. This
208853** function searches the leaf page for a term matching (pTerm/nTerm).
208854**
208855** If the specified term is found on the page, then the iterator is left
208856** pointing to it. If argument bGe is zero and the term is not found,
208857** the iterator is left pointing at EOF.
208858**
208859** If bGe is non-zero and the specified term is not found, then the
208860** iterator is left pointing to the smallest term in the segment that
208861** is larger than the specified term, even if this term is not on the
208862** current page.
208863*/
208864static void fts5LeafSeek(
208865 Fts5Index *p, /* Leave any error code here */
208866 int bGe, /* True for a >= search */
208867 Fts5SegIter *pIter, /* Iterator to seek */
208868 const u8 *pTerm, int nTerm /* Term to search for */
208869){
208870 int iOff;
208871 const u8 *a = pIter->pLeaf->p;
208872 int szLeaf = pIter->pLeaf->szLeaf;
208873 int n = pIter->pLeaf->nn;
208874
208875 int nMatch = 0;
208876 int nKeep = 0;
208877 int nNew = 0;
208878 int iTermOff;
208879 int iPgidx; /* Current offset in pgidx */
208880 int bEndOfPage = 0;
208881
208882 assert( p->rc==SQLITE_OK );
208883
208884 iPgidx = szLeaf;
208885 iPgidx += fts5GetVarint32(&a[iPgidx], iTermOff);
208886 iOff = iTermOff;
208887 if( iOff>n ){
208888 p->rc = FTS5_CORRUPT;
208889 return;
208890 }
208891
208892 while( 1 ){
208893
208894 /* Figure out how many new bytes are in this term */
208895 fts5FastGetVarint32(a, iOff, nNew);
208896 if( nKeep<nMatch ){
208897 goto search_failed;
208898 }
208899
208900 assert( nKeep>=nMatch );
208901 if( nKeep==nMatch ){
208902 int nCmp;
208903 int i;
208904 nCmp = MIN(nNew, nTerm-nMatch);
208905 for(i=0; i<nCmp; i++){
208906 if( a[iOff+i]!=pTerm[nMatch+i] ) break;
208907 }
208908 nMatch += i;
208909
208910 if( nTerm==nMatch ){
208911 if( i==nNew ){
208912 goto search_success;
208913 }else{
208914 goto search_failed;
208915 }
208916 }else if( i<nNew && a[iOff+i]>pTerm[nMatch] ){
208917 goto search_failed;
208918 }
208919 }
208920
208921 if( iPgidx>=n ){
208922 bEndOfPage = 1;
208923 break;
208924 }
208925
208926 iPgidx += fts5GetVarint32(&a[iPgidx], nKeep);
208927 iTermOff += nKeep;
208928 iOff = iTermOff;
208929
208930 if( iOff>=n ){
208931 p->rc = FTS5_CORRUPT;
208932 return;
208933 }
208934
208935 /* Read the nKeep field of the next term. */
208936 fts5FastGetVarint32(a, iOff, nKeep);
208937 }
208938
208939 search_failed:
208940 if( bGe==0 ){
208941 fts5DataRelease(pIter->pLeaf);
208942 pIter->pLeaf = 0;
208943 return;
208944 }else if( bEndOfPage ){
208945 do {
208946 fts5SegIterNextPage(p, pIter);
208947 if( pIter->pLeaf==0 ) return;
208948 a = pIter->pLeaf->p;
208949 if( fts5LeafIsTermless(pIter->pLeaf)==0 ){
208950 iPgidx = pIter->pLeaf->szLeaf;
208951 iPgidx += fts5GetVarint32(&pIter->pLeaf->p[iPgidx], iOff);
208952 if( iOff<4 || iOff>=pIter->pLeaf->szLeaf ){
208953 p->rc = FTS5_CORRUPT;
208954 }else{
208955 nKeep = 0;
208956 iTermOff = iOff;
208957 n = pIter->pLeaf->nn;
208958 iOff += fts5GetVarint32(&a[iOff], nNew);
208959 break;
208960 }
208961 }
208962 }while( 1 );
208963 }
208964
208965 search_success:
208966
208967 pIter->iLeafOffset = iOff + nNew;
208968 pIter->iTermLeafOffset = pIter->iLeafOffset;
208969 pIter->iTermLeafPgno = pIter->iLeafPgno;
208970
208971 fts5BufferSet(&p->rc, &pIter->term, nKeep, pTerm);
208972 fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff]);
208973
208974 if( iPgidx>=n ){
208975 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
208976 }else{
208977 int nExtra;
208978 iPgidx += fts5GetVarint32(&a[iPgidx], nExtra);
208979 pIter->iEndofDoclist = iTermOff + nExtra;
208980 }
208981 pIter->iPgidxOff = iPgidx;
208982
208983 fts5SegIterLoadRowid(p, pIter);
208984 fts5SegIterLoadNPos(p, pIter);
208985}
208986
208987static sqlite3_stmt *fts5IdxSelectStmt(Fts5Index *p){
208988 if( p->pIdxSelect==0 ){
208989 Fts5Config *pConfig = p->pConfig;
208990 fts5IndexPrepareStmt(p, &p->pIdxSelect, sqlite3_mprintf(
208991 "SELECT pgno FROM '%q'.'%q_idx' WHERE "
208992 "segid=? AND term<=? ORDER BY term DESC LIMIT 1",
208993 pConfig->zDb, pConfig->zName
208994 ));
208995 }
208996 return p->pIdxSelect;
208997}
208998
208999/*
209000** Initialize the object pIter to point to term pTerm/nTerm within segment
209001** pSeg. If there is no such term in the index, the iterator is set to EOF.
209002**
209003** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
209004** an error has already occurred when this function is called, it is a no-op.
209005*/
209006static void fts5SegIterSeekInit(
209007 Fts5Index *p, /* FTS5 backend */
209008 const u8 *pTerm, int nTerm, /* Term to seek to */
209009 int flags, /* Mask of FTS5INDEX_XXX flags */
209010 Fts5StructureSegment *pSeg, /* Description of segment */
209011 Fts5SegIter *pIter /* Object to populate */
209012){
209013 int iPg = 1;
209014 int bGe = (flags & FTS5INDEX_QUERY_SCAN);
209015 int bDlidx = 0; /* True if there is a doclist-index */
209016 sqlite3_stmt *pIdxSelect = 0;
209017
209018 assert( bGe==0 || (flags & FTS5INDEX_QUERY_DESC)==0 );
209019 assert( pTerm && nTerm );
209020 memset(pIter, 0, sizeof(*pIter));
209021 pIter->pSeg = pSeg;
209022
209023 /* This block sets stack variable iPg to the leaf page number that may
209024 ** contain term (pTerm/nTerm), if it is present in the segment. */
209025 pIdxSelect = fts5IdxSelectStmt(p);
209026 if( p->rc ) return;
209027 sqlite3_bind_int(pIdxSelect, 1, pSeg->iSegid);
209028 sqlite3_bind_blob(pIdxSelect, 2, pTerm, nTerm, SQLITE_STATIC);
209029 if( SQLITE_ROW==sqlite3_step(pIdxSelect) ){
209030 i64 val = sqlite3_column_int(pIdxSelect, 0);
209031 iPg = (int)(val>>1);
209032 bDlidx = (val & 0x0001);
209033 }
209034 p->rc = sqlite3_reset(pIdxSelect);
209035 sqlite3_bind_null(pIdxSelect, 2);
209036
209037 if( iPg<pSeg->pgnoFirst ){
209038 iPg = pSeg->pgnoFirst;
209039 bDlidx = 0;
209040 }
209041
209042 pIter->iLeafPgno = iPg - 1;
209043 fts5SegIterNextPage(p, pIter);
209044
209045 if( pIter->pLeaf ){
209046 fts5LeafSeek(p, bGe, pIter, pTerm, nTerm);
209047 }
209048
209049 if( p->rc==SQLITE_OK && bGe==0 ){
209050 pIter->flags |= FTS5_SEGITER_ONETERM;
209051 if( pIter->pLeaf ){
209052 if( flags & FTS5INDEX_QUERY_DESC ){
209053 pIter->flags |= FTS5_SEGITER_REVERSE;
209054 }
209055 if( bDlidx ){
209056 fts5SegIterLoadDlidx(p, pIter);
209057 }
209058 if( flags & FTS5INDEX_QUERY_DESC ){
209059 fts5SegIterReverse(p, pIter);
209060 }
209061 }
209062 }
209063
209064 fts5SegIterSetNext(p, pIter);
209065
209066 /* Either:
209067 **
209068 ** 1) an error has occurred, or
209069 ** 2) the iterator points to EOF, or
209070 ** 3) the iterator points to an entry with term (pTerm/nTerm), or
209071 ** 4) the FTS5INDEX_QUERY_SCAN flag was set and the iterator points
209072 ** to an entry with a term greater than or equal to (pTerm/nTerm).
209073 */
209074 assert( p->rc!=SQLITE_OK /* 1 */
209075 || pIter->pLeaf==0 /* 2 */
209076 || fts5BufferCompareBlob(&pIter->term, pTerm, nTerm)==0 /* 3 */
209077 || (bGe && fts5BufferCompareBlob(&pIter->term, pTerm, nTerm)>0) /* 4 */
209078 );
209079}
209080
209081/*
209082** Initialize the object pIter to point to term pTerm/nTerm within the
209083** in-memory hash table. If there is no such term in the hash-table, the
209084** iterator is set to EOF.
209085**
209086** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
209087** an error has already occurred when this function is called, it is a no-op.
209088*/
209089static void fts5SegIterHashInit(
209090 Fts5Index *p, /* FTS5 backend */
209091 const u8 *pTerm, int nTerm, /* Term to seek to */
209092 int flags, /* Mask of FTS5INDEX_XXX flags */
209093 Fts5SegIter *pIter /* Object to populate */
209094){
209095 const u8 *pList = 0;
209096 int nList = 0;
209097 const u8 *z = 0;
209098 int n = 0;
209099
209100 assert( p->pHash );
209101 assert( p->rc==SQLITE_OK );
209102
209103 if( pTerm==0 || (flags & FTS5INDEX_QUERY_SCAN) ){
209104 p->rc = sqlite3Fts5HashScanInit(p->pHash, (const char*)pTerm, nTerm);
209105 sqlite3Fts5HashScanEntry(p->pHash, (const char**)&z, &pList, &nList);
209106 n = (z ? (int)strlen((const char*)z) : 0);
209107 }else{
209108 pIter->flags |= FTS5_SEGITER_ONETERM;
209109 sqlite3Fts5HashQuery(p->pHash, (const char*)pTerm, nTerm, &pList, &nList);
209110 z = pTerm;
209111 n = nTerm;
209112 }
209113
209114 if( pList ){
209115 Fts5Data *pLeaf;
209116 sqlite3Fts5BufferSet(&p->rc, &pIter->term, n, z);
209117 pLeaf = fts5IdxMalloc(p, sizeof(Fts5Data));
209118 if( pLeaf==0 ) return;
209119 pLeaf->p = (u8*)pList;
209120 pLeaf->nn = pLeaf->szLeaf = nList;
209121 pIter->pLeaf = pLeaf;
209122 pIter->iLeafOffset = fts5GetVarint(pLeaf->p, (u64*)&pIter->iRowid);
209123 pIter->iEndofDoclist = pLeaf->nn;
209124
209125 if( flags & FTS5INDEX_QUERY_DESC ){
209126 pIter->flags |= FTS5_SEGITER_REVERSE;
209127 fts5SegIterReverseInitPage(p, pIter);
209128 }else{
209129 fts5SegIterLoadNPos(p, pIter);
209130 }
209131 }
209132
209133 fts5SegIterSetNext(p, pIter);
209134}
209135
209136/*
209137** Zero the iterator passed as the only argument.
209138*/
209139static void fts5SegIterClear(Fts5SegIter *pIter){
209140 fts5BufferFree(&pIter->term);
209141 fts5DataRelease(pIter->pLeaf);
209142 fts5DataRelease(pIter->pNextLeaf);
209143 fts5DlidxIterFree(pIter->pDlidx);
209144 sqlite3_free(pIter->aRowidOffset);
209145 memset(pIter, 0, sizeof(Fts5SegIter));
209146}
209147
209148#ifdef SQLITE_DEBUG
209149
209150/*
209151** This function is used as part of the big assert() procedure implemented by
209152** fts5AssertMultiIterSetup(). It ensures that the result currently stored
209153** in *pRes is the correct result of comparing the current positions of the
209154** two iterators.
209155*/
209156static void fts5AssertComparisonResult(
209157 Fts5Iter *pIter,
209158 Fts5SegIter *p1,
209159 Fts5SegIter *p2,
209160 Fts5CResult *pRes
209161){
209162 int i1 = p1 - pIter->aSeg;
209163 int i2 = p2 - pIter->aSeg;
209164
209165 if( p1->pLeaf || p2->pLeaf ){
209166 if( p1->pLeaf==0 ){
209167 assert( pRes->iFirst==i2 );
209168 }else if( p2->pLeaf==0 ){
209169 assert( pRes->iFirst==i1 );
209170 }else{
209171 int nMin = MIN(p1->term.n, p2->term.n);
209172 int res = memcmp(p1->term.p, p2->term.p, nMin);
209173 if( res==0 ) res = p1->term.n - p2->term.n;
209174
209175 if( res==0 ){
209176 assert( pRes->bTermEq==1 );
209177 assert( p1->iRowid!=p2->iRowid );
209178 res = ((p1->iRowid > p2->iRowid)==pIter->bRev) ? -1 : 1;
209179 }else{
209180 assert( pRes->bTermEq==0 );
209181 }
209182
209183 if( res<0 ){
209184 assert( pRes->iFirst==i1 );
209185 }else{
209186 assert( pRes->iFirst==i2 );
209187 }
209188 }
209189 }
209190}
209191
209192/*
209193** This function is a no-op unless SQLITE_DEBUG is defined when this module
209194** is compiled. In that case, this function is essentially an assert()
209195** statement used to verify that the contents of the pIter->aFirst[] array
209196** are correct.
209197*/
209198static void fts5AssertMultiIterSetup(Fts5Index *p, Fts5Iter *pIter){
209199 if( p->rc==SQLITE_OK ){
209200 Fts5SegIter *pFirst = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
209201 int i;
209202
209203 assert( (pFirst->pLeaf==0)==pIter->base.bEof );
209204
209205 /* Check that pIter->iSwitchRowid is set correctly. */
209206 for(i=0; i<pIter->nSeg; i++){
209207 Fts5SegIter *p1 = &pIter->aSeg[i];
209208 assert( p1==pFirst
209209 || p1->pLeaf==0
209210 || fts5BufferCompare(&pFirst->term, &p1->term)
209211 || p1->iRowid==pIter->iSwitchRowid
209212 || (p1->iRowid<pIter->iSwitchRowid)==pIter->bRev
209213 );
209214 }
209215
209216 for(i=0; i<pIter->nSeg; i+=2){
209217 Fts5SegIter *p1 = &pIter->aSeg[i];
209218 Fts5SegIter *p2 = &pIter->aSeg[i+1];
209219 Fts5CResult *pRes = &pIter->aFirst[(pIter->nSeg + i) / 2];
209220 fts5AssertComparisonResult(pIter, p1, p2, pRes);
209221 }
209222
209223 for(i=1; i<(pIter->nSeg / 2); i+=2){
209224 Fts5SegIter *p1 = &pIter->aSeg[ pIter->aFirst[i*2].iFirst ];
209225 Fts5SegIter *p2 = &pIter->aSeg[ pIter->aFirst[i*2+1].iFirst ];
209226 Fts5CResult *pRes = &pIter->aFirst[i];
209227 fts5AssertComparisonResult(pIter, p1, p2, pRes);
209228 }
209229 }
209230}
209231#else
209232# define fts5AssertMultiIterSetup(x,y)
209233#endif
209234
209235/*
209236** Do the comparison necessary to populate pIter->aFirst[iOut].
209237**
209238** If the returned value is non-zero, then it is the index of an entry
209239** in the pIter->aSeg[] array that is (a) not at EOF, and (b) pointing
209240** to a key that is a duplicate of another, higher priority,
209241** segment-iterator in the pSeg->aSeg[] array.
209242*/
209243static int fts5MultiIterDoCompare(Fts5Iter *pIter, int iOut){
209244 int i1; /* Index of left-hand Fts5SegIter */
209245 int i2; /* Index of right-hand Fts5SegIter */
209246 int iRes;
209247 Fts5SegIter *p1; /* Left-hand Fts5SegIter */
209248 Fts5SegIter *p2; /* Right-hand Fts5SegIter */
209249 Fts5CResult *pRes = &pIter->aFirst[iOut];
209250
209251 assert( iOut<pIter->nSeg && iOut>0 );
209252 assert( pIter->bRev==0 || pIter->bRev==1 );
209253
209254 if( iOut>=(pIter->nSeg/2) ){
209255 i1 = (iOut - pIter->nSeg/2) * 2;
209256 i2 = i1 + 1;
209257 }else{
209258 i1 = pIter->aFirst[iOut*2].iFirst;
209259 i2 = pIter->aFirst[iOut*2+1].iFirst;
209260 }
209261 p1 = &pIter->aSeg[i1];
209262 p2 = &pIter->aSeg[i2];
209263
209264 pRes->bTermEq = 0;
209265 if( p1->pLeaf==0 ){ /* If p1 is at EOF */
209266 iRes = i2;
209267 }else if( p2->pLeaf==0 ){ /* If p2 is at EOF */
209268 iRes = i1;
209269 }else{
209270 int res = fts5BufferCompare(&p1->term, &p2->term);
209271 if( res==0 ){
209272 assert( i2>i1 );
209273 assert( i2!=0 );
209274 pRes->bTermEq = 1;
209275 if( p1->iRowid==p2->iRowid ){
209276 p1->bDel = p2->bDel;
209277 return i2;
209278 }
209279 res = ((p1->iRowid > p2->iRowid)==pIter->bRev) ? -1 : +1;
209280 }
209281 assert( res!=0 );
209282 if( res<0 ){
209283 iRes = i1;
209284 }else{
209285 iRes = i2;
209286 }
209287 }
209288
209289 pRes->iFirst = (u16)iRes;
209290 return 0;
209291}
209292
209293/*
209294** Move the seg-iter so that it points to the first rowid on page iLeafPgno.
209295** It is an error if leaf iLeafPgno does not exist or contains no rowids.
209296*/
209297static void fts5SegIterGotoPage(
209298 Fts5Index *p, /* FTS5 backend object */
209299 Fts5SegIter *pIter, /* Iterator to advance */
209300 int iLeafPgno
209301){
209302 assert( iLeafPgno>pIter->iLeafPgno );
209303
209304 if( iLeafPgno>pIter->pSeg->pgnoLast ){
209305 p->rc = FTS5_CORRUPT;
209306 }else{
209307 fts5DataRelease(pIter->pNextLeaf);
209308 pIter->pNextLeaf = 0;
209309 pIter->iLeafPgno = iLeafPgno-1;
209310 fts5SegIterNextPage(p, pIter);
209311 assert( p->rc!=SQLITE_OK || pIter->iLeafPgno==iLeafPgno );
209312
209313 if( p->rc==SQLITE_OK ){
209314 int iOff;
209315 u8 *a = pIter->pLeaf->p;
209316 int n = pIter->pLeaf->szLeaf;
209317
209318 iOff = fts5LeafFirstRowidOff(pIter->pLeaf);
209319 if( iOff<4 || iOff>=n ){
209320 p->rc = FTS5_CORRUPT;
209321 }else{
209322 iOff += fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
209323 pIter->iLeafOffset = iOff;
209324 fts5SegIterLoadNPos(p, pIter);
209325 }
209326 }
209327 }
209328}
209329
209330/*
209331** Advance the iterator passed as the second argument until it is at or
209332** past rowid iFrom. Regardless of the value of iFrom, the iterator is
209333** always advanced at least once.
209334*/
209335static void fts5SegIterNextFrom(
209336 Fts5Index *p, /* FTS5 backend object */
209337 Fts5SegIter *pIter, /* Iterator to advance */
209338 i64 iMatch /* Advance iterator at least this far */
209339){
209340 int bRev = (pIter->flags & FTS5_SEGITER_REVERSE);
209341 Fts5DlidxIter *pDlidx = pIter->pDlidx;
209342 int iLeafPgno = pIter->iLeafPgno;
209343 int bMove = 1;
209344
209345 assert( pIter->flags & FTS5_SEGITER_ONETERM );
209346 assert( pIter->pDlidx );
209347 assert( pIter->pLeaf );
209348
209349 if( bRev==0 ){
209350 while( !fts5DlidxIterEof(p, pDlidx) && iMatch>fts5DlidxIterRowid(pDlidx) ){
209351 iLeafPgno = fts5DlidxIterPgno(pDlidx);
209352 fts5DlidxIterNext(p, pDlidx);
209353 }
209354 assert_nc( iLeafPgno>=pIter->iLeafPgno || p->rc );
209355 if( iLeafPgno>pIter->iLeafPgno ){
209356 fts5SegIterGotoPage(p, pIter, iLeafPgno);
209357 bMove = 0;
209358 }
209359 }else{
209360 assert( pIter->pNextLeaf==0 );
209361 assert( iMatch<pIter->iRowid );
209362 while( !fts5DlidxIterEof(p, pDlidx) && iMatch<fts5DlidxIterRowid(pDlidx) ){
209363 fts5DlidxIterPrev(p, pDlidx);
209364 }
209365 iLeafPgno = fts5DlidxIterPgno(pDlidx);
209366
209367 assert( fts5DlidxIterEof(p, pDlidx) || iLeafPgno<=pIter->iLeafPgno );
209368
209369 if( iLeafPgno<pIter->iLeafPgno ){
209370 pIter->iLeafPgno = iLeafPgno+1;
209371 fts5SegIterReverseNewPage(p, pIter);
209372 bMove = 0;
209373 }
209374 }
209375
209376 do{
209377 if( bMove && p->rc==SQLITE_OK ) pIter->xNext(p, pIter, 0);
209378 if( pIter->pLeaf==0 ) break;
209379 if( bRev==0 && pIter->iRowid>=iMatch ) break;
209380 if( bRev!=0 && pIter->iRowid<=iMatch ) break;
209381 bMove = 1;
209382 }while( p->rc==SQLITE_OK );
209383}
209384
209385
209386/*
209387** Free the iterator object passed as the second argument.
209388*/
209389static void fts5MultiIterFree(Fts5Iter *pIter){
209390 if( pIter ){
209391 int i;
209392 for(i=0; i<pIter->nSeg; i++){
209393 fts5SegIterClear(&pIter->aSeg[i]);
209394 }
209395 fts5StructureRelease(pIter->pStruct);
209396 fts5BufferFree(&pIter->poslist);
209397 sqlite3_free(pIter);
209398 }
209399}
209400
209401static void fts5MultiIterAdvanced(
209402 Fts5Index *p, /* FTS5 backend to iterate within */
209403 Fts5Iter *pIter, /* Iterator to update aFirst[] array for */
209404 int iChanged, /* Index of sub-iterator just advanced */
209405 int iMinset /* Minimum entry in aFirst[] to set */
209406){
209407 int i;
209408 for(i=(pIter->nSeg+iChanged)/2; i>=iMinset && p->rc==SQLITE_OK; i=i/2){
209409 int iEq;
209410 if( (iEq = fts5MultiIterDoCompare(pIter, i)) ){
209411 Fts5SegIter *pSeg = &pIter->aSeg[iEq];
209412 assert( p->rc==SQLITE_OK );
209413 pSeg->xNext(p, pSeg, 0);
209414 i = pIter->nSeg + iEq;
209415 }
209416 }
209417}
209418
209419/*
209420** Sub-iterator iChanged of iterator pIter has just been advanced. It still
209421** points to the same term though - just a different rowid. This function
209422** attempts to update the contents of the pIter->aFirst[] accordingly.
209423** If it does so successfully, 0 is returned. Otherwise 1.
209424**
209425** If non-zero is returned, the caller should call fts5MultiIterAdvanced()
209426** on the iterator instead. That function does the same as this one, except
209427** that it deals with more complicated cases as well.
209428*/
209429static int fts5MultiIterAdvanceRowid(
209430 Fts5Iter *pIter, /* Iterator to update aFirst[] array for */
209431 int iChanged, /* Index of sub-iterator just advanced */
209432 Fts5SegIter **ppFirst
209433){
209434 Fts5SegIter *pNew = &pIter->aSeg[iChanged];
209435
209436 if( pNew->iRowid==pIter->iSwitchRowid
209437 || (pNew->iRowid<pIter->iSwitchRowid)==pIter->bRev
209438 ){
209439 int i;
209440 Fts5SegIter *pOther = &pIter->aSeg[iChanged ^ 0x0001];
209441 pIter->iSwitchRowid = pIter->bRev ? SMALLEST_INT64 : LARGEST_INT64;
209442 for(i=(pIter->nSeg+iChanged)/2; 1; i=i/2){
209443 Fts5CResult *pRes = &pIter->aFirst[i];
209444
209445 assert( pNew->pLeaf );
209446 assert( pRes->bTermEq==0 || pOther->pLeaf );
209447
209448 if( pRes->bTermEq ){
209449 if( pNew->iRowid==pOther->iRowid ){
209450 return 1;
209451 }else if( (pOther->iRowid>pNew->iRowid)==pIter->bRev ){
209452 pIter->iSwitchRowid = pOther->iRowid;
209453 pNew = pOther;
209454 }else if( (pOther->iRowid>pIter->iSwitchRowid)==pIter->bRev ){
209455 pIter->iSwitchRowid = pOther->iRowid;
209456 }
209457 }
209458 pRes->iFirst = (u16)(pNew - pIter->aSeg);
209459 if( i==1 ) break;
209460
209461 pOther = &pIter->aSeg[ pIter->aFirst[i ^ 0x0001].iFirst ];
209462 }
209463 }
209464
209465 *ppFirst = pNew;
209466 return 0;
209467}
209468
209469/*
209470** Set the pIter->bEof variable based on the state of the sub-iterators.
209471*/
209472static void fts5MultiIterSetEof(Fts5Iter *pIter){
209473 Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
209474 pIter->base.bEof = pSeg->pLeaf==0;
209475 pIter->iSwitchRowid = pSeg->iRowid;
209476}
209477
209478/*
209479** Move the iterator to the next entry.
209480**
209481** If an error occurs, an error code is left in Fts5Index.rc. It is not
209482** considered an error if the iterator reaches EOF, or if it is already at
209483** EOF when this function is called.
209484*/
209485static void fts5MultiIterNext(
209486 Fts5Index *p,
209487 Fts5Iter *pIter,
209488 int bFrom, /* True if argument iFrom is valid */
209489 i64 iFrom /* Advance at least as far as this */
209490){
209491 int bUseFrom = bFrom;
209492 assert( pIter->base.bEof==0 );
209493 while( p->rc==SQLITE_OK ){
209494 int iFirst = pIter->aFirst[1].iFirst;
209495 int bNewTerm = 0;
209496 Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
209497 assert( p->rc==SQLITE_OK );
209498 if( bUseFrom && pSeg->pDlidx ){
209499 fts5SegIterNextFrom(p, pSeg, iFrom);
209500 }else{
209501 pSeg->xNext(p, pSeg, &bNewTerm);
209502 }
209503
209504 if( pSeg->pLeaf==0 || bNewTerm
209505 || fts5MultiIterAdvanceRowid(pIter, iFirst, &pSeg)
209506 ){
209507 fts5MultiIterAdvanced(p, pIter, iFirst, 1);
209508 fts5MultiIterSetEof(pIter);
209509 pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
209510 if( pSeg->pLeaf==0 ) return;
209511 }
209512
209513 fts5AssertMultiIterSetup(p, pIter);
209514 assert( pSeg==&pIter->aSeg[pIter->aFirst[1].iFirst] && pSeg->pLeaf );
209515 if( pIter->bSkipEmpty==0 || pSeg->nPos ){
209516 pIter->xSetOutputs(pIter, pSeg);
209517 return;
209518 }
209519 bUseFrom = 0;
209520 }
209521}
209522
209523static void fts5MultiIterNext2(
209524 Fts5Index *p,
209525 Fts5Iter *pIter,
209526 int *pbNewTerm /* OUT: True if *might* be new term */
209527){
209528 assert( pIter->bSkipEmpty );
209529 if( p->rc==SQLITE_OK ){
209530 *pbNewTerm = 0;
209531 do{
209532 int iFirst = pIter->aFirst[1].iFirst;
209533 Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
209534 int bNewTerm = 0;
209535
209536 assert( p->rc==SQLITE_OK );
209537 pSeg->xNext(p, pSeg, &bNewTerm);
209538 if( pSeg->pLeaf==0 || bNewTerm
209539 || fts5MultiIterAdvanceRowid(pIter, iFirst, &pSeg)
209540 ){
209541 fts5MultiIterAdvanced(p, pIter, iFirst, 1);
209542 fts5MultiIterSetEof(pIter);
209543 *pbNewTerm = 1;
209544 }
209545 fts5AssertMultiIterSetup(p, pIter);
209546
209547 }while( fts5MultiIterIsEmpty(p, pIter) );
209548 }
209549}
209550
209551static void fts5IterSetOutputs_Noop(Fts5Iter *pUnused1, Fts5SegIter *pUnused2){
209552 UNUSED_PARAM2(pUnused1, pUnused2);
209553}
209554
209555static Fts5Iter *fts5MultiIterAlloc(
209556 Fts5Index *p, /* FTS5 backend to iterate within */
209557 int nSeg
209558){
209559 Fts5Iter *pNew;
209560 int nSlot; /* Power of two >= nSeg */
209561
209562 for(nSlot=2; nSlot<nSeg; nSlot=nSlot*2);
209563 pNew = fts5IdxMalloc(p,
209564 sizeof(Fts5Iter) + /* pNew */
209565 sizeof(Fts5SegIter) * (nSlot-1) + /* pNew->aSeg[] */
209566 sizeof(Fts5CResult) * nSlot /* pNew->aFirst[] */
209567 );
209568 if( pNew ){
209569 pNew->nSeg = nSlot;
209570 pNew->aFirst = (Fts5CResult*)&pNew->aSeg[nSlot];
209571 pNew->pIndex = p;
209572 pNew->xSetOutputs = fts5IterSetOutputs_Noop;
209573 }
209574 return pNew;
209575}
209576
209577static void fts5PoslistCallback(
209578 Fts5Index *pUnused,
209579 void *pContext,
209580 const u8 *pChunk, int nChunk
209581){
209582 UNUSED_PARAM(pUnused);
209583 assert_nc( nChunk>=0 );
209584 if( nChunk>0 ){
209585 fts5BufferSafeAppendBlob((Fts5Buffer*)pContext, pChunk, nChunk);
209586 }
209587}
209588
209589typedef struct PoslistCallbackCtx PoslistCallbackCtx;
209590struct PoslistCallbackCtx {
209591 Fts5Buffer *pBuf; /* Append to this buffer */
209592 Fts5Colset *pColset; /* Restrict matches to this column */
209593 int eState; /* See above */
209594};
209595
209596typedef struct PoslistOffsetsCtx PoslistOffsetsCtx;
209597struct PoslistOffsetsCtx {
209598 Fts5Buffer *pBuf; /* Append to this buffer */
209599 Fts5Colset *pColset; /* Restrict matches to this column */
209600 int iRead;
209601 int iWrite;
209602};
209603
209604/*
209605** TODO: Make this more efficient!
209606*/
209607static int fts5IndexColsetTest(Fts5Colset *pColset, int iCol){
209608 int i;
209609 for(i=0; i<pColset->nCol; i++){
209610 if( pColset->aiCol[i]==iCol ) return 1;
209611 }
209612 return 0;
209613}
209614
209615static void fts5PoslistOffsetsCallback(
209616 Fts5Index *pUnused,
209617 void *pContext,
209618 const u8 *pChunk, int nChunk
209619){
209620 PoslistOffsetsCtx *pCtx = (PoslistOffsetsCtx*)pContext;
209621 UNUSED_PARAM(pUnused);
209622 assert_nc( nChunk>=0 );
209623 if( nChunk>0 ){
209624 int i = 0;
209625 while( i<nChunk ){
209626 int iVal;
209627 i += fts5GetVarint32(&pChunk[i], iVal);
209628 iVal += pCtx->iRead - 2;
209629 pCtx->iRead = iVal;
209630 if( fts5IndexColsetTest(pCtx->pColset, iVal) ){
209631 fts5BufferSafeAppendVarint(pCtx->pBuf, iVal + 2 - pCtx->iWrite);
209632 pCtx->iWrite = iVal;
209633 }
209634 }
209635 }
209636}
209637
209638static void fts5PoslistFilterCallback(
209639 Fts5Index *pUnused,
209640 void *pContext,
209641 const u8 *pChunk, int nChunk
209642){
209643 PoslistCallbackCtx *pCtx = (PoslistCallbackCtx*)pContext;
209644 UNUSED_PARAM(pUnused);
209645 assert_nc( nChunk>=0 );
209646 if( nChunk>0 ){
209647 /* Search through to find the first varint with value 1. This is the
209648 ** start of the next columns hits. */
209649 int i = 0;
209650 int iStart = 0;
209651
209652 if( pCtx->eState==2 ){
209653 int iCol;
209654 fts5FastGetVarint32(pChunk, i, iCol);
209655 if( fts5IndexColsetTest(pCtx->pColset, iCol) ){
209656 pCtx->eState = 1;
209657 fts5BufferSafeAppendVarint(pCtx->pBuf, 1);
209658 }else{
209659 pCtx->eState = 0;
209660 }
209661 }
209662
209663 do {
209664 while( i<nChunk && pChunk[i]!=0x01 ){
209665 while( pChunk[i] & 0x80 ) i++;
209666 i++;
209667 }
209668 if( pCtx->eState ){
209669 fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart);
209670 }
209671 if( i<nChunk ){
209672 int iCol;
209673 iStart = i;
209674 i++;
209675 if( i>=nChunk ){
209676 pCtx->eState = 2;
209677 }else{
209678 fts5FastGetVarint32(pChunk, i, iCol);
209679 pCtx->eState = fts5IndexColsetTest(pCtx->pColset, iCol);
209680 if( pCtx->eState ){
209681 fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart);
209682 iStart = i;
209683 }
209684 }
209685 }
209686 }while( i<nChunk );
209687 }
209688}
209689
209690static void fts5ChunkIterate(
209691 Fts5Index *p, /* Index object */
209692 Fts5SegIter *pSeg, /* Poslist of this iterator */
209693 void *pCtx, /* Context pointer for xChunk callback */
209694 void (*xChunk)(Fts5Index*, void*, const u8*, int)
209695){
209696 int nRem = pSeg->nPos; /* Number of bytes still to come */
209697 Fts5Data *pData = 0;
209698 u8 *pChunk = &pSeg->pLeaf->p[pSeg->iLeafOffset];
209699 int nChunk = MIN(nRem, pSeg->pLeaf->szLeaf - pSeg->iLeafOffset);
209700 int pgno = pSeg->iLeafPgno;
209701 int pgnoSave = 0;
209702
209703 /* This function does notmwork with detail=none databases. */
209704 assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE );
209705
209706 if( (pSeg->flags & FTS5_SEGITER_REVERSE)==0 ){
209707 pgnoSave = pgno+1;
209708 }
209709
209710 while( 1 ){
209711 xChunk(p, pCtx, pChunk, nChunk);
209712 nRem -= nChunk;
209713 fts5DataRelease(pData);
209714 if( nRem<=0 ){
209715 break;
209716 }else{
209717 pgno++;
209718 pData = fts5LeafRead(p, FTS5_SEGMENT_ROWID(pSeg->pSeg->iSegid, pgno));
209719 if( pData==0 ) break;
209720 pChunk = &pData->p[4];
209721 nChunk = MIN(nRem, pData->szLeaf - 4);
209722 if( pgno==pgnoSave ){
209723 assert( pSeg->pNextLeaf==0 );
209724 pSeg->pNextLeaf = pData;
209725 pData = 0;
209726 }
209727 }
209728 }
209729}
209730
209731/*
209732** Iterator pIter currently points to a valid entry (not EOF). This
209733** function appends the position list data for the current entry to
209734** buffer pBuf. It does not make a copy of the position-list size
209735** field.
209736*/
209737static void fts5SegiterPoslist(
209738 Fts5Index *p,
209739 Fts5SegIter *pSeg,
209740 Fts5Colset *pColset,
209741 Fts5Buffer *pBuf
209742){
209743 if( 0==fts5BufferGrow(&p->rc, pBuf, pSeg->nPos) ){
209744 if( pColset==0 ){
209745 fts5ChunkIterate(p, pSeg, (void*)pBuf, fts5PoslistCallback);
209746 }else{
209747 if( p->pConfig->eDetail==FTS5_DETAIL_FULL ){
209748 PoslistCallbackCtx sCtx;
209749 sCtx.pBuf = pBuf;
209750 sCtx.pColset = pColset;
209751 sCtx.eState = fts5IndexColsetTest(pColset, 0);
209752 assert( sCtx.eState==0 || sCtx.eState==1 );
209753 fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistFilterCallback);
209754 }else{
209755 PoslistOffsetsCtx sCtx;
209756 memset(&sCtx, 0, sizeof(sCtx));
209757 sCtx.pBuf = pBuf;
209758 sCtx.pColset = pColset;
209759 fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistOffsetsCallback);
209760 }
209761 }
209762 }
209763}
209764
209765/*
209766** IN/OUT parameter (*pa) points to a position list n bytes in size. If
209767** the position list contains entries for column iCol, then (*pa) is set
209768** to point to the sub-position-list for that column and the number of
209769** bytes in it returned. Or, if the argument position list does not
209770** contain any entries for column iCol, return 0.
209771*/
209772static int fts5IndexExtractCol(
209773 const u8 **pa, /* IN/OUT: Pointer to poslist */
209774 int n, /* IN: Size of poslist in bytes */
209775 int iCol /* Column to extract from poslist */
209776){
209777 int iCurrent = 0; /* Anything before the first 0x01 is col 0 */
209778 const u8 *p = *pa;
209779 const u8 *pEnd = &p[n]; /* One byte past end of position list */
209780
209781 while( iCol>iCurrent ){
209782 /* Advance pointer p until it points to pEnd or an 0x01 byte that is
209783 ** not part of a varint. Note that it is not possible for a negative
209784 ** or extremely large varint to occur within an uncorrupted position
209785 ** list. So the last byte of each varint may be assumed to have a clear
209786 ** 0x80 bit. */
209787 while( *p!=0x01 ){
209788 while( *p++ & 0x80 );
209789 if( p>=pEnd ) return 0;
209790 }
209791 *pa = p++;
209792 iCurrent = *p++;
209793 if( iCurrent & 0x80 ){
209794 p--;
209795 p += fts5GetVarint32(p, iCurrent);
209796 }
209797 }
209798 if( iCol!=iCurrent ) return 0;
209799
209800 /* Advance pointer p until it points to pEnd or an 0x01 byte that is
209801 ** not part of a varint */
209802 while( p<pEnd && *p!=0x01 ){
209803 while( *p++ & 0x80 );
209804 }
209805
209806 return p - (*pa);
209807}
209808
209809static void fts5IndexExtractColset(
209810 int *pRc,
209811 Fts5Colset *pColset, /* Colset to filter on */
209812 const u8 *pPos, int nPos, /* Position list */
209813 Fts5Buffer *pBuf /* Output buffer */
209814){
209815 if( *pRc==SQLITE_OK ){
209816 int i;
209817 fts5BufferZero(pBuf);
209818 for(i=0; i<pColset->nCol; i++){
209819 const u8 *pSub = pPos;
209820 int nSub = fts5IndexExtractCol(&pSub, nPos, pColset->aiCol[i]);
209821 if( nSub ){
209822 fts5BufferAppendBlob(pRc, pBuf, nSub, pSub);
209823 }
209824 }
209825 }
209826}
209827
209828/*
209829** xSetOutputs callback used by detail=none tables.
209830*/
209831static void fts5IterSetOutputs_None(Fts5Iter *pIter, Fts5SegIter *pSeg){
209832 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_NONE );
209833 pIter->base.iRowid = pSeg->iRowid;
209834 pIter->base.nData = pSeg->nPos;
209835}
209836
209837/*
209838** xSetOutputs callback used by detail=full and detail=col tables when no
209839** column filters are specified.
209840*/
209841static void fts5IterSetOutputs_Nocolset(Fts5Iter *pIter, Fts5SegIter *pSeg){
209842 pIter->base.iRowid = pSeg->iRowid;
209843 pIter->base.nData = pSeg->nPos;
209844
209845 assert( pIter->pIndex->pConfig->eDetail!=FTS5_DETAIL_NONE );
209846 assert( pIter->pColset==0 );
209847
209848 if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
209849 /* All data is stored on the current page. Populate the output
209850 ** variables to point into the body of the page object. */
209851 pIter->base.pData = &pSeg->pLeaf->p[pSeg->iLeafOffset];
209852 }else{
209853 /* The data is distributed over two or more pages. Copy it into the
209854 ** Fts5Iter.poslist buffer and then set the output pointer to point
209855 ** to this buffer. */
209856 fts5BufferZero(&pIter->poslist);
209857 fts5SegiterPoslist(pIter->pIndex, pSeg, 0, &pIter->poslist);
209858 pIter->base.pData = pIter->poslist.p;
209859 }
209860}
209861
209862/*
209863** xSetOutputs callback used when the Fts5Colset object has nCol==0 (match
209864** against no columns at all).
209865*/
209866static void fts5IterSetOutputs_ZeroColset(Fts5Iter *pIter, Fts5SegIter *pSeg){
209867 UNUSED_PARAM(pSeg);
209868 pIter->base.nData = 0;
209869}
209870
209871/*
209872** xSetOutputs callback used by detail=col when there is a column filter
209873** and there are 100 or more columns. Also called as a fallback from
209874** fts5IterSetOutputs_Col100 if the column-list spans more than one page.
209875*/
209876static void fts5IterSetOutputs_Col(Fts5Iter *pIter, Fts5SegIter *pSeg){
209877 fts5BufferZero(&pIter->poslist);
209878 fts5SegiterPoslist(pIter->pIndex, pSeg, pIter->pColset, &pIter->poslist);
209879 pIter->base.iRowid = pSeg->iRowid;
209880 pIter->base.pData = pIter->poslist.p;
209881 pIter->base.nData = pIter->poslist.n;
209882}
209883
209884/*
209885** xSetOutputs callback used when:
209886**
209887** * detail=col,
209888** * there is a column filter, and
209889** * the table contains 100 or fewer columns.
209890**
209891** The last point is to ensure all column numbers are stored as
209892** single-byte varints.
209893*/
209894static void fts5IterSetOutputs_Col100(Fts5Iter *pIter, Fts5SegIter *pSeg){
209895
209896 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_COLUMNS );
209897 assert( pIter->pColset );
209898
209899 if( pSeg->iLeafOffset+pSeg->nPos>pSeg->pLeaf->szLeaf ){
209900 fts5IterSetOutputs_Col(pIter, pSeg);
209901 }else{
209902 u8 *a = (u8*)&pSeg->pLeaf->p[pSeg->iLeafOffset];
209903 u8 *pEnd = (u8*)&a[pSeg->nPos];
209904 int iPrev = 0;
209905 int *aiCol = pIter->pColset->aiCol;
209906 int *aiColEnd = &aiCol[pIter->pColset->nCol];
209907
209908 u8 *aOut = pIter->poslist.p;
209909 int iPrevOut = 0;
209910
209911 pIter->base.iRowid = pSeg->iRowid;
209912
209913 while( a<pEnd ){
209914 iPrev += (int)a++[0] - 2;
209915 while( *aiCol<iPrev ){
209916 aiCol++;
209917 if( aiCol==aiColEnd ) goto setoutputs_col_out;
209918 }
209919 if( *aiCol==iPrev ){
209920 *aOut++ = (u8)((iPrev - iPrevOut) + 2);
209921 iPrevOut = iPrev;
209922 }
209923 }
209924
209925setoutputs_col_out:
209926 pIter->base.pData = pIter->poslist.p;
209927 pIter->base.nData = aOut - pIter->poslist.p;
209928 }
209929}
209930
209931/*
209932** xSetOutputs callback used by detail=full when there is a column filter.
209933*/
209934static void fts5IterSetOutputs_Full(Fts5Iter *pIter, Fts5SegIter *pSeg){
209935 Fts5Colset *pColset = pIter->pColset;
209936 pIter->base.iRowid = pSeg->iRowid;
209937
209938 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_FULL );
209939 assert( pColset );
209940
209941 if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
209942 /* All data is stored on the current page. Populate the output
209943 ** variables to point into the body of the page object. */
209944 const u8 *a = &pSeg->pLeaf->p[pSeg->iLeafOffset];
209945 if( pColset->nCol==1 ){
209946 pIter->base.nData = fts5IndexExtractCol(&a, pSeg->nPos,pColset->aiCol[0]);
209947 pIter->base.pData = a;
209948 }else{
209949 int *pRc = &pIter->pIndex->rc;
209950 fts5BufferZero(&pIter->poslist);
209951 fts5IndexExtractColset(pRc, pColset, a, pSeg->nPos, &pIter->poslist);
209952 pIter->base.pData = pIter->poslist.p;
209953 pIter->base.nData = pIter->poslist.n;
209954 }
209955 }else{
209956 /* The data is distributed over two or more pages. Copy it into the
209957 ** Fts5Iter.poslist buffer and then set the output pointer to point
209958 ** to this buffer. */
209959 fts5BufferZero(&pIter->poslist);
209960 fts5SegiterPoslist(pIter->pIndex, pSeg, pColset, &pIter->poslist);
209961 pIter->base.pData = pIter->poslist.p;
209962 pIter->base.nData = pIter->poslist.n;
209963 }
209964}
209965
209966static void fts5IterSetOutputCb(int *pRc, Fts5Iter *pIter){
209967 if( *pRc==SQLITE_OK ){
209968 Fts5Config *pConfig = pIter->pIndex->pConfig;
209969 if( pConfig->eDetail==FTS5_DETAIL_NONE ){
209970 pIter->xSetOutputs = fts5IterSetOutputs_None;
209971 }
209972
209973 else if( pIter->pColset==0 ){
209974 pIter->xSetOutputs = fts5IterSetOutputs_Nocolset;
209975 }
209976
209977 else if( pIter->pColset->nCol==0 ){
209978 pIter->xSetOutputs = fts5IterSetOutputs_ZeroColset;
209979 }
209980
209981 else if( pConfig->eDetail==FTS5_DETAIL_FULL ){
209982 pIter->xSetOutputs = fts5IterSetOutputs_Full;
209983 }
209984
209985 else{
209986 assert( pConfig->eDetail==FTS5_DETAIL_COLUMNS );
209987 if( pConfig->nCol<=100 ){
209988 pIter->xSetOutputs = fts5IterSetOutputs_Col100;
209989 sqlite3Fts5BufferSize(pRc, &pIter->poslist, pConfig->nCol);
209990 }else{
209991 pIter->xSetOutputs = fts5IterSetOutputs_Col;
209992 }
209993 }
209994 }
209995}
209996
209997
209998/*
209999** Allocate a new Fts5Iter object.
210000**
210001** The new object will be used to iterate through data in structure pStruct.
210002** If iLevel is -ve, then all data in all segments is merged. Or, if iLevel
210003** is zero or greater, data from the first nSegment segments on level iLevel
210004** is merged.
210005**
210006** The iterator initially points to the first term/rowid entry in the
210007** iterated data.
210008*/
210009static void fts5MultiIterNew(
210010 Fts5Index *p, /* FTS5 backend to iterate within */
210011 Fts5Structure *pStruct, /* Structure of specific index */
210012 int flags, /* FTS5INDEX_QUERY_XXX flags */
210013 Fts5Colset *pColset, /* Colset to filter on (or NULL) */
210014 const u8 *pTerm, int nTerm, /* Term to seek to (or NULL/0) */
210015 int iLevel, /* Level to iterate (-1 for all) */
210016 int nSegment, /* Number of segments to merge (iLevel>=0) */
210017 Fts5Iter **ppOut /* New object */
210018){
210019 int nSeg = 0; /* Number of segment-iters in use */
210020 int iIter = 0; /* */
210021 int iSeg; /* Used to iterate through segments */
210022 Fts5StructureLevel *pLvl;
210023 Fts5Iter *pNew;
210024
210025 assert( (pTerm==0 && nTerm==0) || iLevel<0 );
210026
210027 /* Allocate space for the new multi-seg-iterator. */
210028 if( p->rc==SQLITE_OK ){
210029 if( iLevel<0 ){
210030 assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) );
210031 nSeg = pStruct->nSegment;
210032 nSeg += (p->pHash ? 1 : 0);
210033 }else{
210034 nSeg = MIN(pStruct->aLevel[iLevel].nSeg, nSegment);
210035 }
210036 }
210037 *ppOut = pNew = fts5MultiIterAlloc(p, nSeg);
210038 if( pNew==0 ) return;
210039 pNew->bRev = (0!=(flags & FTS5INDEX_QUERY_DESC));
210040 pNew->bSkipEmpty = (0!=(flags & FTS5INDEX_QUERY_SKIPEMPTY));
210041 pNew->pStruct = pStruct;
210042 pNew->pColset = pColset;
210043 fts5StructureRef(pStruct);
210044 if( (flags & FTS5INDEX_QUERY_NOOUTPUT)==0 ){
210045 fts5IterSetOutputCb(&p->rc, pNew);
210046 }
210047
210048 /* Initialize each of the component segment iterators. */
210049 if( p->rc==SQLITE_OK ){
210050 if( iLevel<0 ){
210051 Fts5StructureLevel *pEnd = &pStruct->aLevel[pStruct->nLevel];
210052 if( p->pHash ){
210053 /* Add a segment iterator for the current contents of the hash table. */
210054 Fts5SegIter *pIter = &pNew->aSeg[iIter++];
210055 fts5SegIterHashInit(p, pTerm, nTerm, flags, pIter);
210056 }
210057 for(pLvl=&pStruct->aLevel[0]; pLvl<pEnd; pLvl++){
210058 for(iSeg=pLvl->nSeg-1; iSeg>=0; iSeg--){
210059 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
210060 Fts5SegIter *pIter = &pNew->aSeg[iIter++];
210061 if( pTerm==0 ){
210062 fts5SegIterInit(p, pSeg, pIter);
210063 }else{
210064 fts5SegIterSeekInit(p, pTerm, nTerm, flags, pSeg, pIter);
210065 }
210066 }
210067 }
210068 }else{
210069 pLvl = &pStruct->aLevel[iLevel];
210070 for(iSeg=nSeg-1; iSeg>=0; iSeg--){
210071 fts5SegIterInit(p, &pLvl->aSeg[iSeg], &pNew->aSeg[iIter++]);
210072 }
210073 }
210074 assert( iIter==nSeg );
210075 }
210076
210077 /* If the above was successful, each component iterators now points
210078 ** to the first entry in its segment. In this case initialize the
210079 ** aFirst[] array. Or, if an error has occurred, free the iterator
210080 ** object and set the output variable to NULL. */
210081 if( p->rc==SQLITE_OK ){
210082 for(iIter=pNew->nSeg-1; iIter>0; iIter--){
210083 int iEq;
210084 if( (iEq = fts5MultiIterDoCompare(pNew, iIter)) ){
210085 Fts5SegIter *pSeg = &pNew->aSeg[iEq];
210086 if( p->rc==SQLITE_OK ) pSeg->xNext(p, pSeg, 0);
210087 fts5MultiIterAdvanced(p, pNew, iEq, iIter);
210088 }
210089 }
210090 fts5MultiIterSetEof(pNew);
210091 fts5AssertMultiIterSetup(p, pNew);
210092
210093 if( pNew->bSkipEmpty && fts5MultiIterIsEmpty(p, pNew) ){
210094 fts5MultiIterNext(p, pNew, 0, 0);
210095 }else if( pNew->base.bEof==0 ){
210096 Fts5SegIter *pSeg = &pNew->aSeg[pNew->aFirst[1].iFirst];
210097 pNew->xSetOutputs(pNew, pSeg);
210098 }
210099
210100 }else{
210101 fts5MultiIterFree(pNew);
210102 *ppOut = 0;
210103 }
210104}
210105
210106/*
210107** Create an Fts5Iter that iterates through the doclist provided
210108** as the second argument.
210109*/
210110static void fts5MultiIterNew2(
210111 Fts5Index *p, /* FTS5 backend to iterate within */
210112 Fts5Data *pData, /* Doclist to iterate through */
210113 int bDesc, /* True for descending rowid order */
210114 Fts5Iter **ppOut /* New object */
210115){
210116 Fts5Iter *pNew;
210117 pNew = fts5MultiIterAlloc(p, 2);
210118 if( pNew ){
210119 Fts5SegIter *pIter = &pNew->aSeg[1];
210120
210121 pIter->flags = FTS5_SEGITER_ONETERM;
210122 if( pData->szLeaf>0 ){
210123 pIter->pLeaf = pData;
210124 pIter->iLeafOffset = fts5GetVarint(pData->p, (u64*)&pIter->iRowid);
210125 pIter->iEndofDoclist = pData->nn;
210126 pNew->aFirst[1].iFirst = 1;
210127 if( bDesc ){
210128 pNew->bRev = 1;
210129 pIter->flags |= FTS5_SEGITER_REVERSE;
210130 fts5SegIterReverseInitPage(p, pIter);
210131 }else{
210132 fts5SegIterLoadNPos(p, pIter);
210133 }
210134 pData = 0;
210135 }else{
210136 pNew->base.bEof = 1;
210137 }
210138 fts5SegIterSetNext(p, pIter);
210139
210140 *ppOut = pNew;
210141 }
210142
210143 fts5DataRelease(pData);
210144}
210145
210146/*
210147** Return true if the iterator is at EOF or if an error has occurred.
210148** False otherwise.
210149*/
210150static int fts5MultiIterEof(Fts5Index *p, Fts5Iter *pIter){
210151 assert( p->rc
210152 || (pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf==0)==pIter->base.bEof
210153 );
210154 return (p->rc || pIter->base.bEof);
210155}
210156
210157/*
210158** Return the rowid of the entry that the iterator currently points
210159** to. If the iterator points to EOF when this function is called the
210160** results are undefined.
210161*/
210162static i64 fts5MultiIterRowid(Fts5Iter *pIter){
210163 assert( pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf );
210164 return pIter->aSeg[ pIter->aFirst[1].iFirst ].iRowid;
210165}
210166
210167/*
210168** Move the iterator to the next entry at or following iMatch.
210169*/
210170static void fts5MultiIterNextFrom(
210171 Fts5Index *p,
210172 Fts5Iter *pIter,
210173 i64 iMatch
210174){
210175 while( 1 ){
210176 i64 iRowid;
210177 fts5MultiIterNext(p, pIter, 1, iMatch);
210178 if( fts5MultiIterEof(p, pIter) ) break;
210179 iRowid = fts5MultiIterRowid(pIter);
210180 if( pIter->bRev==0 && iRowid>=iMatch ) break;
210181 if( pIter->bRev!=0 && iRowid<=iMatch ) break;
210182 }
210183}
210184
210185/*
210186** Return a pointer to a buffer containing the term associated with the
210187** entry that the iterator currently points to.
210188*/
210189static const u8 *fts5MultiIterTerm(Fts5Iter *pIter, int *pn){
210190 Fts5SegIter *p = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
210191 *pn = p->term.n;
210192 return p->term.p;
210193}
210194
210195/*
210196** Allocate a new segment-id for the structure pStruct. The new segment
210197** id must be between 1 and 65335 inclusive, and must not be used by
210198** any currently existing segment. If a free segment id cannot be found,
210199** SQLITE_FULL is returned.
210200**
210201** If an error has already occurred, this function is a no-op. 0 is
210202** returned in this case.
210203*/
210204static int fts5AllocateSegid(Fts5Index *p, Fts5Structure *pStruct){
210205 int iSegid = 0;
210206
210207 if( p->rc==SQLITE_OK ){
210208 if( pStruct->nSegment>=FTS5_MAX_SEGMENT ){
210209 p->rc = SQLITE_FULL;
210210 }else{
210211 /* FTS5_MAX_SEGMENT is currently defined as 2000. So the following
210212 ** array is 63 elements, or 252 bytes, in size. */
210213 u32 aUsed[(FTS5_MAX_SEGMENT+31) / 32];
210214 int iLvl, iSeg;
210215 int i;
210216 u32 mask;
210217 memset(aUsed, 0, sizeof(aUsed));
210218 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
210219 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
210220 int iId = pStruct->aLevel[iLvl].aSeg[iSeg].iSegid;
210221 if( iId<=FTS5_MAX_SEGMENT ){
210222 aUsed[(iId-1) / 32] |= 1 << ((iId-1) % 32);
210223 }
210224 }
210225 }
210226
210227 for(i=0; aUsed[i]==0xFFFFFFFF; i++);
210228 mask = aUsed[i];
210229 for(iSegid=0; mask & (1 << iSegid); iSegid++);
210230 iSegid += 1 + i*32;
210231
210232#ifdef SQLITE_DEBUG
210233 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
210234 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
210235 assert( iSegid!=pStruct->aLevel[iLvl].aSeg[iSeg].iSegid );
210236 }
210237 }
210238 assert( iSegid>0 && iSegid<=FTS5_MAX_SEGMENT );
210239
210240 {
210241 sqlite3_stmt *pIdxSelect = fts5IdxSelectStmt(p);
210242 if( p->rc==SQLITE_OK ){
210243 u8 aBlob[2] = {0xff, 0xff};
210244 sqlite3_bind_int(pIdxSelect, 1, iSegid);
210245 sqlite3_bind_blob(pIdxSelect, 2, aBlob, 2, SQLITE_STATIC);
210246 assert( sqlite3_step(pIdxSelect)!=SQLITE_ROW );
210247 p->rc = sqlite3_reset(pIdxSelect);
210248 sqlite3_bind_null(pIdxSelect, 2);
210249 }
210250 }
210251#endif
210252 }
210253 }
210254
210255 return iSegid;
210256}
210257
210258/*
210259** Discard all data currently cached in the hash-tables.
210260*/
210261static void fts5IndexDiscardData(Fts5Index *p){
210262 assert( p->pHash || p->nPendingData==0 );
210263 if( p->pHash ){
210264 sqlite3Fts5HashClear(p->pHash);
210265 p->nPendingData = 0;
210266 }
210267}
210268
210269/*
210270** Return the size of the prefix, in bytes, that buffer
210271** (pNew/<length-unknown>) shares with buffer (pOld/nOld).
210272**
210273** Buffer (pNew/<length-unknown>) is guaranteed to be greater
210274** than buffer (pOld/nOld).
210275*/
210276static int fts5PrefixCompress(int nOld, const u8 *pOld, const u8 *pNew){
210277 int i;
210278 for(i=0; i<nOld; i++){
210279 if( pOld[i]!=pNew[i] ) break;
210280 }
210281 return i;
210282}
210283
210284static void fts5WriteDlidxClear(
210285 Fts5Index *p,
210286 Fts5SegWriter *pWriter,
210287 int bFlush /* If true, write dlidx to disk */
210288){
210289 int i;
210290 assert( bFlush==0 || (pWriter->nDlidx>0 && pWriter->aDlidx[0].buf.n>0) );
210291 for(i=0; i<pWriter->nDlidx; i++){
210292 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[i];
210293 if( pDlidx->buf.n==0 ) break;
210294 if( bFlush ){
210295 assert( pDlidx->pgno!=0 );
210296 fts5DataWrite(p,
210297 FTS5_DLIDX_ROWID(pWriter->iSegid, i, pDlidx->pgno),
210298 pDlidx->buf.p, pDlidx->buf.n
210299 );
210300 }
210301 sqlite3Fts5BufferZero(&pDlidx->buf);
210302 pDlidx->bPrevValid = 0;
210303 }
210304}
210305
210306/*
210307** Grow the pWriter->aDlidx[] array to at least nLvl elements in size.
210308** Any new array elements are zeroed before returning.
210309*/
210310static int fts5WriteDlidxGrow(
210311 Fts5Index *p,
210312 Fts5SegWriter *pWriter,
210313 int nLvl
210314){
210315 if( p->rc==SQLITE_OK && nLvl>=pWriter->nDlidx ){
210316 Fts5DlidxWriter *aDlidx = (Fts5DlidxWriter*)sqlite3_realloc(
210317 pWriter->aDlidx, sizeof(Fts5DlidxWriter) * nLvl
210318 );
210319 if( aDlidx==0 ){
210320 p->rc = SQLITE_NOMEM;
210321 }else{
210322 int nByte = sizeof(Fts5DlidxWriter) * (nLvl - pWriter->nDlidx);
210323 memset(&aDlidx[pWriter->nDlidx], 0, nByte);
210324 pWriter->aDlidx = aDlidx;
210325 pWriter->nDlidx = nLvl;
210326 }
210327 }
210328 return p->rc;
210329}
210330
210331/*
210332** If the current doclist-index accumulating in pWriter->aDlidx[] is large
210333** enough, flush it to disk and return 1. Otherwise discard it and return
210334** zero.
210335*/
210336static int fts5WriteFlushDlidx(Fts5Index *p, Fts5SegWriter *pWriter){
210337 int bFlag = 0;
210338
210339 /* If there were FTS5_MIN_DLIDX_SIZE or more empty leaf pages written
210340 ** to the database, also write the doclist-index to disk. */
210341 if( pWriter->aDlidx[0].buf.n>0 && pWriter->nEmpty>=FTS5_MIN_DLIDX_SIZE ){
210342 bFlag = 1;
210343 }
210344 fts5WriteDlidxClear(p, pWriter, bFlag);
210345 pWriter->nEmpty = 0;
210346 return bFlag;
210347}
210348
210349/*
210350** This function is called whenever processing of the doclist for the
210351** last term on leaf page (pWriter->iBtPage) is completed.
210352**
210353** The doclist-index for that term is currently stored in-memory within the
210354** Fts5SegWriter.aDlidx[] array. If it is large enough, this function
210355** writes it out to disk. Or, if it is too small to bother with, discards
210356** it.
210357**
210358** Fts5SegWriter.btterm currently contains the first term on page iBtPage.
210359*/
210360static void fts5WriteFlushBtree(Fts5Index *p, Fts5SegWriter *pWriter){
210361 int bFlag;
210362
210363 assert( pWriter->iBtPage || pWriter->nEmpty==0 );
210364 if( pWriter->iBtPage==0 ) return;
210365 bFlag = fts5WriteFlushDlidx(p, pWriter);
210366
210367 if( p->rc==SQLITE_OK ){
210368 const char *z = (pWriter->btterm.n>0?(const char*)pWriter->btterm.p:"");
210369 /* The following was already done in fts5WriteInit(): */
210370 /* sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid); */
210371 sqlite3_bind_blob(p->pIdxWriter, 2, z, pWriter->btterm.n, SQLITE_STATIC);
210372 sqlite3_bind_int64(p->pIdxWriter, 3, bFlag + ((i64)pWriter->iBtPage<<1));
210373 sqlite3_step(p->pIdxWriter);
210374 p->rc = sqlite3_reset(p->pIdxWriter);
210375 sqlite3_bind_null(p->pIdxWriter, 2);
210376 }
210377 pWriter->iBtPage = 0;
210378}
210379
210380/*
210381** This is called once for each leaf page except the first that contains
210382** at least one term. Argument (nTerm/pTerm) is the split-key - a term that
210383** is larger than all terms written to earlier leaves, and equal to or
210384** smaller than the first term on the new leaf.
210385**
210386** If an error occurs, an error code is left in Fts5Index.rc. If an error
210387** has already occurred when this function is called, it is a no-op.
210388*/
210389static void fts5WriteBtreeTerm(
210390 Fts5Index *p, /* FTS5 backend object */
210391 Fts5SegWriter *pWriter, /* Writer object */
210392 int nTerm, const u8 *pTerm /* First term on new page */
210393){
210394 fts5WriteFlushBtree(p, pWriter);
210395 fts5BufferSet(&p->rc, &pWriter->btterm, nTerm, pTerm);
210396 pWriter->iBtPage = pWriter->writer.pgno;
210397}
210398
210399/*
210400** This function is called when flushing a leaf page that contains no
210401** terms at all to disk.
210402*/
210403static void fts5WriteBtreeNoTerm(
210404 Fts5Index *p, /* FTS5 backend object */
210405 Fts5SegWriter *pWriter /* Writer object */
210406){
210407 /* If there were no rowids on the leaf page either and the doclist-index
210408 ** has already been started, append an 0x00 byte to it. */
210409 if( pWriter->bFirstRowidInPage && pWriter->aDlidx[0].buf.n>0 ){
210410 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[0];
210411 assert( pDlidx->bPrevValid );
210412 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, 0);
210413 }
210414
210415 /* Increment the "number of sequential leaves without a term" counter. */
210416 pWriter->nEmpty++;
210417}
210418
210419static i64 fts5DlidxExtractFirstRowid(Fts5Buffer *pBuf){
210420 i64 iRowid;
210421 int iOff;
210422
210423 iOff = 1 + fts5GetVarint(&pBuf->p[1], (u64*)&iRowid);
210424 fts5GetVarint(&pBuf->p[iOff], (u64*)&iRowid);
210425 return iRowid;
210426}
210427
210428/*
210429** Rowid iRowid has just been appended to the current leaf page. It is the
210430** first on the page. This function appends an appropriate entry to the current
210431** doclist-index.
210432*/
210433static void fts5WriteDlidxAppend(
210434 Fts5Index *p,
210435 Fts5SegWriter *pWriter,
210436 i64 iRowid
210437){
210438 int i;
210439 int bDone = 0;
210440
210441 for(i=0; p->rc==SQLITE_OK && bDone==0; i++){
210442 i64 iVal;
210443 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[i];
210444
210445 if( pDlidx->buf.n>=p->pConfig->pgsz ){
210446 /* The current doclist-index page is full. Write it to disk and push
210447 ** a copy of iRowid (which will become the first rowid on the next
210448 ** doclist-index leaf page) up into the next level of the b-tree
210449 ** hierarchy. If the node being flushed is currently the root node,
210450 ** also push its first rowid upwards. */
210451 pDlidx->buf.p[0] = 0x01; /* Not the root node */
210452 fts5DataWrite(p,
210453 FTS5_DLIDX_ROWID(pWriter->iSegid, i, pDlidx->pgno),
210454 pDlidx->buf.p, pDlidx->buf.n
210455 );
210456 fts5WriteDlidxGrow(p, pWriter, i+2);
210457 pDlidx = &pWriter->aDlidx[i];
210458 if( p->rc==SQLITE_OK && pDlidx[1].buf.n==0 ){
210459 i64 iFirst = fts5DlidxExtractFirstRowid(&pDlidx->buf);
210460
210461 /* This was the root node. Push its first rowid up to the new root. */
210462 pDlidx[1].pgno = pDlidx->pgno;
210463 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, 0);
210464 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, pDlidx->pgno);
210465 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, iFirst);
210466 pDlidx[1].bPrevValid = 1;
210467 pDlidx[1].iPrev = iFirst;
210468 }
210469
210470 sqlite3Fts5BufferZero(&pDlidx->buf);
210471 pDlidx->bPrevValid = 0;
210472 pDlidx->pgno++;
210473 }else{
210474 bDone = 1;
210475 }
210476
210477 if( pDlidx->bPrevValid ){
210478 iVal = iRowid - pDlidx->iPrev;
210479 }else{
210480 i64 iPgno = (i==0 ? pWriter->writer.pgno : pDlidx[-1].pgno);
210481 assert( pDlidx->buf.n==0 );
210482 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, !bDone);
210483 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, iPgno);
210484 iVal = iRowid;
210485 }
210486
210487 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, iVal);
210488 pDlidx->bPrevValid = 1;
210489 pDlidx->iPrev = iRowid;
210490 }
210491}
210492
210493static void fts5WriteFlushLeaf(Fts5Index *p, Fts5SegWriter *pWriter){
210494 static const u8 zero[] = { 0x00, 0x00, 0x00, 0x00 };
210495 Fts5PageWriter *pPage = &pWriter->writer;
210496 i64 iRowid;
210497
210498 assert( (pPage->pgidx.n==0)==(pWriter->bFirstTermInPage) );
210499
210500 /* Set the szLeaf header field. */
210501 assert( 0==fts5GetU16(&pPage->buf.p[2]) );
210502 fts5PutU16(&pPage->buf.p[2], (u16)pPage->buf.n);
210503
210504 if( pWriter->bFirstTermInPage ){
210505 /* No term was written to this page. */
210506 assert( pPage->pgidx.n==0 );
210507 fts5WriteBtreeNoTerm(p, pWriter);
210508 }else{
210509 /* Append the pgidx to the page buffer. Set the szLeaf header field. */
210510 fts5BufferAppendBlob(&p->rc, &pPage->buf, pPage->pgidx.n, pPage->pgidx.p);
210511 }
210512
210513 /* Write the page out to disk */
210514 iRowid = FTS5_SEGMENT_ROWID(pWriter->iSegid, pPage->pgno);
210515 fts5DataWrite(p, iRowid, pPage->buf.p, pPage->buf.n);
210516
210517 /* Initialize the next page. */
210518 fts5BufferZero(&pPage->buf);
210519 fts5BufferZero(&pPage->pgidx);
210520 fts5BufferAppendBlob(&p->rc, &pPage->buf, 4, zero);
210521 pPage->iPrevPgidx = 0;
210522 pPage->pgno++;
210523
210524 /* Increase the leaves written counter */
210525 pWriter->nLeafWritten++;
210526
210527 /* The new leaf holds no terms or rowids */
210528 pWriter->bFirstTermInPage = 1;
210529 pWriter->bFirstRowidInPage = 1;
210530}
210531
210532/*
210533** Append term pTerm/nTerm to the segment being written by the writer passed
210534** as the second argument.
210535**
210536** If an error occurs, set the Fts5Index.rc error code. If an error has
210537** already occurred, this function is a no-op.
210538*/
210539static void fts5WriteAppendTerm(
210540 Fts5Index *p,
210541 Fts5SegWriter *pWriter,
210542 int nTerm, const u8 *pTerm
210543){
210544 int nPrefix; /* Bytes of prefix compression for term */
210545 Fts5PageWriter *pPage = &pWriter->writer;
210546 Fts5Buffer *pPgidx = &pWriter->writer.pgidx;
210547
210548 assert( p->rc==SQLITE_OK );
210549 assert( pPage->buf.n>=4 );
210550 assert( pPage->buf.n>4 || pWriter->bFirstTermInPage );
210551
210552 /* If the current leaf page is full, flush it to disk. */
210553 if( (pPage->buf.n + pPgidx->n + nTerm + 2)>=p->pConfig->pgsz ){
210554 if( pPage->buf.n>4 ){
210555 fts5WriteFlushLeaf(p, pWriter);
210556 }
210557 fts5BufferGrow(&p->rc, &pPage->buf, nTerm+FTS5_DATA_PADDING);
210558 }
210559
210560 /* TODO1: Updating pgidx here. */
210561 pPgidx->n += sqlite3Fts5PutVarint(
210562 &pPgidx->p[pPgidx->n], pPage->buf.n - pPage->iPrevPgidx
210563 );
210564 pPage->iPrevPgidx = pPage->buf.n;
210565#if 0
210566 fts5PutU16(&pPgidx->p[pPgidx->n], pPage->buf.n);
210567 pPgidx->n += 2;
210568#endif
210569
210570 if( pWriter->bFirstTermInPage ){
210571 nPrefix = 0;
210572 if( pPage->pgno!=1 ){
210573 /* This is the first term on a leaf that is not the leftmost leaf in
210574 ** the segment b-tree. In this case it is necessary to add a term to
210575 ** the b-tree hierarchy that is (a) larger than the largest term
210576 ** already written to the segment and (b) smaller than or equal to
210577 ** this term. In other words, a prefix of (pTerm/nTerm) that is one
210578 ** byte longer than the longest prefix (pTerm/nTerm) shares with the
210579 ** previous term.
210580 **
210581 ** Usually, the previous term is available in pPage->term. The exception
210582 ** is if this is the first term written in an incremental-merge step.
210583 ** In this case the previous term is not available, so just write a
210584 ** copy of (pTerm/nTerm) into the parent node. This is slightly
210585 ** inefficient, but still correct. */
210586 int n = nTerm;
210587 if( pPage->term.n ){
210588 n = 1 + fts5PrefixCompress(pPage->term.n, pPage->term.p, pTerm);
210589 }
210590 fts5WriteBtreeTerm(p, pWriter, n, pTerm);
210591 pPage = &pWriter->writer;
210592 }
210593 }else{
210594 nPrefix = fts5PrefixCompress(pPage->term.n, pPage->term.p, pTerm);
210595 fts5BufferAppendVarint(&p->rc, &pPage->buf, nPrefix);
210596 }
210597
210598 /* Append the number of bytes of new data, then the term data itself
210599 ** to the page. */
210600 fts5BufferAppendVarint(&p->rc, &pPage->buf, nTerm - nPrefix);
210601 fts5BufferAppendBlob(&p->rc, &pPage->buf, nTerm - nPrefix, &pTerm[nPrefix]);
210602
210603 /* Update the Fts5PageWriter.term field. */
210604 fts5BufferSet(&p->rc, &pPage->term, nTerm, pTerm);
210605 pWriter->bFirstTermInPage = 0;
210606
210607 pWriter->bFirstRowidInPage = 0;
210608 pWriter->bFirstRowidInDoclist = 1;
210609
210610 assert( p->rc || (pWriter->nDlidx>0 && pWriter->aDlidx[0].buf.n==0) );
210611 pWriter->aDlidx[0].pgno = pPage->pgno;
210612}
210613
210614/*
210615** Append a rowid and position-list size field to the writers output.
210616*/
210617static void fts5WriteAppendRowid(
210618 Fts5Index *p,
210619 Fts5SegWriter *pWriter,
210620 i64 iRowid
210621){
210622 if( p->rc==SQLITE_OK ){
210623 Fts5PageWriter *pPage = &pWriter->writer;
210624
210625 if( (pPage->buf.n + pPage->pgidx.n)>=p->pConfig->pgsz ){
210626 fts5WriteFlushLeaf(p, pWriter);
210627 }
210628
210629 /* If this is to be the first rowid written to the page, set the
210630 ** rowid-pointer in the page-header. Also append a value to the dlidx
210631 ** buffer, in case a doclist-index is required. */
210632 if( pWriter->bFirstRowidInPage ){
210633 fts5PutU16(pPage->buf.p, (u16)pPage->buf.n);
210634 fts5WriteDlidxAppend(p, pWriter, iRowid);
210635 }
210636
210637 /* Write the rowid. */
210638 if( pWriter->bFirstRowidInDoclist || pWriter->bFirstRowidInPage ){
210639 fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid);
210640 }else{
210641 assert( p->rc || iRowid>pWriter->iPrevRowid );
210642 fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid - pWriter->iPrevRowid);
210643 }
210644 pWriter->iPrevRowid = iRowid;
210645 pWriter->bFirstRowidInDoclist = 0;
210646 pWriter->bFirstRowidInPage = 0;
210647 }
210648}
210649
210650static void fts5WriteAppendPoslistData(
210651 Fts5Index *p,
210652 Fts5SegWriter *pWriter,
210653 const u8 *aData,
210654 int nData
210655){
210656 Fts5PageWriter *pPage = &pWriter->writer;
210657 const u8 *a = aData;
210658 int n = nData;
210659
210660 assert( p->pConfig->pgsz>0 );
210661 while( p->rc==SQLITE_OK
210662 && (pPage->buf.n + pPage->pgidx.n + n)>=p->pConfig->pgsz
210663 ){
210664 int nReq = p->pConfig->pgsz - pPage->buf.n - pPage->pgidx.n;
210665 int nCopy = 0;
210666 while( nCopy<nReq ){
210667 i64 dummy;
210668 nCopy += fts5GetVarint(&a[nCopy], (u64*)&dummy);
210669 }
210670 fts5BufferAppendBlob(&p->rc, &pPage->buf, nCopy, a);
210671 a += nCopy;
210672 n -= nCopy;
210673 fts5WriteFlushLeaf(p, pWriter);
210674 }
210675 if( n>0 ){
210676 fts5BufferAppendBlob(&p->rc, &pPage->buf, n, a);
210677 }
210678}
210679
210680/*
210681** Flush any data cached by the writer object to the database. Free any
210682** allocations associated with the writer.
210683*/
210684static void fts5WriteFinish(
210685 Fts5Index *p,
210686 Fts5SegWriter *pWriter, /* Writer object */
210687 int *pnLeaf /* OUT: Number of leaf pages in b-tree */
210688){
210689 int i;
210690 Fts5PageWriter *pLeaf = &pWriter->writer;
210691 if( p->rc==SQLITE_OK ){
210692 assert( pLeaf->pgno>=1 );
210693 if( pLeaf->buf.n>4 ){
210694 fts5WriteFlushLeaf(p, pWriter);
210695 }
210696 *pnLeaf = pLeaf->pgno-1;
210697 if( pLeaf->pgno>1 ){
210698 fts5WriteFlushBtree(p, pWriter);
210699 }
210700 }
210701 fts5BufferFree(&pLeaf->term);
210702 fts5BufferFree(&pLeaf->buf);
210703 fts5BufferFree(&pLeaf->pgidx);
210704 fts5BufferFree(&pWriter->btterm);
210705
210706 for(i=0; i<pWriter->nDlidx; i++){
210707 sqlite3Fts5BufferFree(&pWriter->aDlidx[i].buf);
210708 }
210709 sqlite3_free(pWriter->aDlidx);
210710}
210711
210712static void fts5WriteInit(
210713 Fts5Index *p,
210714 Fts5SegWriter *pWriter,
210715 int iSegid
210716){
210717 const int nBuffer = p->pConfig->pgsz + FTS5_DATA_PADDING;
210718
210719 memset(pWriter, 0, sizeof(Fts5SegWriter));
210720 pWriter->iSegid = iSegid;
210721
210722 fts5WriteDlidxGrow(p, pWriter, 1);
210723 pWriter->writer.pgno = 1;
210724 pWriter->bFirstTermInPage = 1;
210725 pWriter->iBtPage = 1;
210726
210727 assert( pWriter->writer.buf.n==0 );
210728 assert( pWriter->writer.pgidx.n==0 );
210729
210730 /* Grow the two buffers to pgsz + padding bytes in size. */
210731 sqlite3Fts5BufferSize(&p->rc, &pWriter->writer.pgidx, nBuffer);
210732 sqlite3Fts5BufferSize(&p->rc, &pWriter->writer.buf, nBuffer);
210733
210734 if( p->pIdxWriter==0 ){
210735 Fts5Config *pConfig = p->pConfig;
210736 fts5IndexPrepareStmt(p, &p->pIdxWriter, sqlite3_mprintf(
210737 "INSERT INTO '%q'.'%q_idx'(segid,term,pgno) VALUES(?,?,?)",
210738 pConfig->zDb, pConfig->zName
210739 ));
210740 }
210741
210742 if( p->rc==SQLITE_OK ){
210743 /* Initialize the 4-byte leaf-page header to 0x00. */
210744 memset(pWriter->writer.buf.p, 0, 4);
210745 pWriter->writer.buf.n = 4;
210746
210747 /* Bind the current output segment id to the index-writer. This is an
210748 ** optimization over binding the same value over and over as rows are
210749 ** inserted into %_idx by the current writer. */
210750 sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid);
210751 }
210752}
210753
210754/*
210755** Iterator pIter was used to iterate through the input segments of on an
210756** incremental merge operation. This function is called if the incremental
210757** merge step has finished but the input has not been completely exhausted.
210758*/
210759static void fts5TrimSegments(Fts5Index *p, Fts5Iter *pIter){
210760 int i;
210761 Fts5Buffer buf;
210762 memset(&buf, 0, sizeof(Fts5Buffer));
210763 for(i=0; i<pIter->nSeg; i++){
210764 Fts5SegIter *pSeg = &pIter->aSeg[i];
210765 if( pSeg->pSeg==0 ){
210766 /* no-op */
210767 }else if( pSeg->pLeaf==0 ){
210768 /* All keys from this input segment have been transfered to the output.
210769 ** Set both the first and last page-numbers to 0 to indicate that the
210770 ** segment is now empty. */
210771 pSeg->pSeg->pgnoLast = 0;
210772 pSeg->pSeg->pgnoFirst = 0;
210773 }else{
210774 int iOff = pSeg->iTermLeafOffset; /* Offset on new first leaf page */
210775 i64 iLeafRowid;
210776 Fts5Data *pData;
210777 int iId = pSeg->pSeg->iSegid;
210778 u8 aHdr[4] = {0x00, 0x00, 0x00, 0x00};
210779
210780 iLeafRowid = FTS5_SEGMENT_ROWID(iId, pSeg->iTermLeafPgno);
210781 pData = fts5DataRead(p, iLeafRowid);
210782 if( pData ){
210783 fts5BufferZero(&buf);
210784 fts5BufferGrow(&p->rc, &buf, pData->nn);
210785 fts5BufferAppendBlob(&p->rc, &buf, sizeof(aHdr), aHdr);
210786 fts5BufferAppendVarint(&p->rc, &buf, pSeg->term.n);
210787 fts5BufferAppendBlob(&p->rc, &buf, pSeg->term.n, pSeg->term.p);
210788 fts5BufferAppendBlob(&p->rc, &buf, pData->szLeaf-iOff, &pData->p[iOff]);
210789 if( p->rc==SQLITE_OK ){
210790 /* Set the szLeaf field */
210791 fts5PutU16(&buf.p[2], (u16)buf.n);
210792 }
210793
210794 /* Set up the new page-index array */
210795 fts5BufferAppendVarint(&p->rc, &buf, 4);
210796 if( pSeg->iLeafPgno==pSeg->iTermLeafPgno
210797 && pSeg->iEndofDoclist<pData->szLeaf
210798 ){
210799 int nDiff = pData->szLeaf - pSeg->iEndofDoclist;
210800 fts5BufferAppendVarint(&p->rc, &buf, buf.n - 1 - nDiff - 4);
210801 fts5BufferAppendBlob(&p->rc, &buf,
210802 pData->nn - pSeg->iPgidxOff, &pData->p[pSeg->iPgidxOff]
210803 );
210804 }
210805
210806 fts5DataRelease(pData);
210807 pSeg->pSeg->pgnoFirst = pSeg->iTermLeafPgno;
210808 fts5DataDelete(p, FTS5_SEGMENT_ROWID(iId, 1), iLeafRowid);
210809 fts5DataWrite(p, iLeafRowid, buf.p, buf.n);
210810 }
210811 }
210812 }
210813 fts5BufferFree(&buf);
210814}
210815
210816static void fts5MergeChunkCallback(
210817 Fts5Index *p,
210818 void *pCtx,
210819 const u8 *pChunk, int nChunk
210820){
210821 Fts5SegWriter *pWriter = (Fts5SegWriter*)pCtx;
210822 fts5WriteAppendPoslistData(p, pWriter, pChunk, nChunk);
210823}
210824
210825/*
210826**
210827*/
210828static void fts5IndexMergeLevel(
210829 Fts5Index *p, /* FTS5 backend object */
210830 Fts5Structure **ppStruct, /* IN/OUT: Stucture of index */
210831 int iLvl, /* Level to read input from */
210832 int *pnRem /* Write up to this many output leaves */
210833){
210834 Fts5Structure *pStruct = *ppStruct;
210835 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
210836 Fts5StructureLevel *pLvlOut;
210837 Fts5Iter *pIter = 0; /* Iterator to read input data */
210838 int nRem = pnRem ? *pnRem : 0; /* Output leaf pages left to write */
210839 int nInput; /* Number of input segments */
210840 Fts5SegWriter writer; /* Writer object */
210841 Fts5StructureSegment *pSeg; /* Output segment */
210842 Fts5Buffer term;
210843 int bOldest; /* True if the output segment is the oldest */
210844 int eDetail = p->pConfig->eDetail;
210845 const int flags = FTS5INDEX_QUERY_NOOUTPUT;
210846 int bTermWritten = 0; /* True if current term already output */
210847
210848 assert( iLvl<pStruct->nLevel );
210849 assert( pLvl->nMerge<=pLvl->nSeg );
210850
210851 memset(&writer, 0, sizeof(Fts5SegWriter));
210852 memset(&term, 0, sizeof(Fts5Buffer));
210853 if( pLvl->nMerge ){
210854 pLvlOut = &pStruct->aLevel[iLvl+1];
210855 assert( pLvlOut->nSeg>0 );
210856 nInput = pLvl->nMerge;
210857 pSeg = &pLvlOut->aSeg[pLvlOut->nSeg-1];
210858
210859 fts5WriteInit(p, &writer, pSeg->iSegid);
210860 writer.writer.pgno = pSeg->pgnoLast+1;
210861 writer.iBtPage = 0;
210862 }else{
210863 int iSegid = fts5AllocateSegid(p, pStruct);
210864
210865 /* Extend the Fts5Structure object as required to ensure the output
210866 ** segment exists. */
210867 if( iLvl==pStruct->nLevel-1 ){
210868 fts5StructureAddLevel(&p->rc, ppStruct);
210869 pStruct = *ppStruct;
210870 }
210871 fts5StructureExtendLevel(&p->rc, pStruct, iLvl+1, 1, 0);
210872 if( p->rc ) return;
210873 pLvl = &pStruct->aLevel[iLvl];
210874 pLvlOut = &pStruct->aLevel[iLvl+1];
210875
210876 fts5WriteInit(p, &writer, iSegid);
210877
210878 /* Add the new segment to the output level */
210879 pSeg = &pLvlOut->aSeg[pLvlOut->nSeg];
210880 pLvlOut->nSeg++;
210881 pSeg->pgnoFirst = 1;
210882 pSeg->iSegid = iSegid;
210883 pStruct->nSegment++;
210884
210885 /* Read input from all segments in the input level */
210886 nInput = pLvl->nSeg;
210887 }
210888 bOldest = (pLvlOut->nSeg==1 && pStruct->nLevel==iLvl+2);
210889
210890 assert( iLvl>=0 );
210891 for(fts5MultiIterNew(p, pStruct, flags, 0, 0, 0, iLvl, nInput, &pIter);
210892 fts5MultiIterEof(p, pIter)==0;
210893 fts5MultiIterNext(p, pIter, 0, 0)
210894 ){
210895 Fts5SegIter *pSegIter = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
210896 int nPos; /* position-list size field value */
210897 int nTerm;
210898 const u8 *pTerm;
210899
210900 pTerm = fts5MultiIterTerm(pIter, &nTerm);
210901 if( nTerm!=term.n || memcmp(pTerm, term.p, nTerm) ){
210902 if( pnRem && writer.nLeafWritten>nRem ){
210903 break;
210904 }
210905 fts5BufferSet(&p->rc, &term, nTerm, pTerm);
210906 bTermWritten =0;
210907 }
210908
210909 /* Check for key annihilation. */
210910 if( pSegIter->nPos==0 && (bOldest || pSegIter->bDel==0) ) continue;
210911
210912 if( p->rc==SQLITE_OK && bTermWritten==0 ){
210913 /* This is a new term. Append a term to the output segment. */
210914 fts5WriteAppendTerm(p, &writer, nTerm, pTerm);
210915 bTermWritten = 1;
210916 }
210917
210918 /* Append the rowid to the output */
210919 /* WRITEPOSLISTSIZE */
210920 fts5WriteAppendRowid(p, &writer, fts5MultiIterRowid(pIter));
210921
210922 if( eDetail==FTS5_DETAIL_NONE ){
210923 if( pSegIter->bDel ){
210924 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, 0);
210925 if( pSegIter->nPos>0 ){
210926 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, 0);
210927 }
210928 }
210929 }else{
210930 /* Append the position-list data to the output */
210931 nPos = pSegIter->nPos*2 + pSegIter->bDel;
210932 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, nPos);
210933 fts5ChunkIterate(p, pSegIter, (void*)&writer, fts5MergeChunkCallback);
210934 }
210935 }
210936
210937 /* Flush the last leaf page to disk. Set the output segment b-tree height
210938 ** and last leaf page number at the same time. */
210939 fts5WriteFinish(p, &writer, &pSeg->pgnoLast);
210940
210941 if( fts5MultiIterEof(p, pIter) ){
210942 int i;
210943
210944 /* Remove the redundant segments from the %_data table */
210945 for(i=0; i<nInput; i++){
210946 fts5DataRemoveSegment(p, pLvl->aSeg[i].iSegid);
210947 }
210948
210949 /* Remove the redundant segments from the input level */
210950 if( pLvl->nSeg!=nInput ){
210951 int nMove = (pLvl->nSeg - nInput) * sizeof(Fts5StructureSegment);
210952 memmove(pLvl->aSeg, &pLvl->aSeg[nInput], nMove);
210953 }
210954 pStruct->nSegment -= nInput;
210955 pLvl->nSeg -= nInput;
210956 pLvl->nMerge = 0;
210957 if( pSeg->pgnoLast==0 ){
210958 pLvlOut->nSeg--;
210959 pStruct->nSegment--;
210960 }
210961 }else{
210962 assert( pSeg->pgnoLast>0 );
210963 fts5TrimSegments(p, pIter);
210964 pLvl->nMerge = nInput;
210965 }
210966
210967 fts5MultiIterFree(pIter);
210968 fts5BufferFree(&term);
210969 if( pnRem ) *pnRem -= writer.nLeafWritten;
210970}
210971
210972/*
210973** Do up to nPg pages of automerge work on the index.
210974**
210975** Return true if any changes were actually made, or false otherwise.
210976*/
210977static int fts5IndexMerge(
210978 Fts5Index *p, /* FTS5 backend object */
210979 Fts5Structure **ppStruct, /* IN/OUT: Current structure of index */
210980 int nPg, /* Pages of work to do */
210981 int nMin /* Minimum number of segments to merge */
210982){
210983 int nRem = nPg;
210984 int bRet = 0;
210985 Fts5Structure *pStruct = *ppStruct;
210986 while( nRem>0 && p->rc==SQLITE_OK ){
210987 int iLvl; /* To iterate through levels */
210988 int iBestLvl = 0; /* Level offering the most input segments */
210989 int nBest = 0; /* Number of input segments on best level */
210990
210991 /* Set iBestLvl to the level to read input segments from. */
210992 assert( pStruct->nLevel>0 );
210993 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
210994 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
210995 if( pLvl->nMerge ){
210996 if( pLvl->nMerge>nBest ){
210997 iBestLvl = iLvl;
210998 nBest = pLvl->nMerge;
210999 }
211000 break;
211001 }
211002 if( pLvl->nSeg>nBest ){
211003 nBest = pLvl->nSeg;
211004 iBestLvl = iLvl;
211005 }
211006 }
211007
211008 /* If nBest is still 0, then the index must be empty. */
211009#ifdef SQLITE_DEBUG
211010 for(iLvl=0; nBest==0 && iLvl<pStruct->nLevel; iLvl++){
211011 assert( pStruct->aLevel[iLvl].nSeg==0 );
211012 }
211013#endif
211014
211015 if( nBest<nMin && pStruct->aLevel[iBestLvl].nMerge==0 ){
211016 break;
211017 }
211018 bRet = 1;
211019 fts5IndexMergeLevel(p, &pStruct, iBestLvl, &nRem);
211020 if( p->rc==SQLITE_OK && pStruct->aLevel[iBestLvl].nMerge==0 ){
211021 fts5StructurePromote(p, iBestLvl+1, pStruct);
211022 }
211023 }
211024 *ppStruct = pStruct;
211025 return bRet;
211026}
211027
211028/*
211029** A total of nLeaf leaf pages of data has just been flushed to a level-0
211030** segment. This function updates the write-counter accordingly and, if
211031** necessary, performs incremental merge work.
211032**
211033** If an error occurs, set the Fts5Index.rc error code. If an error has
211034** already occurred, this function is a no-op.
211035*/
211036static void fts5IndexAutomerge(
211037 Fts5Index *p, /* FTS5 backend object */
211038 Fts5Structure **ppStruct, /* IN/OUT: Current structure of index */
211039 int nLeaf /* Number of output leaves just written */
211040){
211041 if( p->rc==SQLITE_OK && p->pConfig->nAutomerge>0 ){
211042 Fts5Structure *pStruct = *ppStruct;
211043 u64 nWrite; /* Initial value of write-counter */
211044 int nWork; /* Number of work-quanta to perform */
211045 int nRem; /* Number of leaf pages left to write */
211046
211047 /* Update the write-counter. While doing so, set nWork. */
211048 nWrite = pStruct->nWriteCounter;
211049 nWork = (int)(((nWrite + nLeaf) / p->nWorkUnit) - (nWrite / p->nWorkUnit));
211050 pStruct->nWriteCounter += nLeaf;
211051 nRem = (int)(p->nWorkUnit * nWork * pStruct->nLevel);
211052
211053 fts5IndexMerge(p, ppStruct, nRem, p->pConfig->nAutomerge);
211054 }
211055}
211056
211057static void fts5IndexCrisismerge(
211058 Fts5Index *p, /* FTS5 backend object */
211059 Fts5Structure **ppStruct /* IN/OUT: Current structure of index */
211060){
211061 const int nCrisis = p->pConfig->nCrisisMerge;
211062 Fts5Structure *pStruct = *ppStruct;
211063 int iLvl = 0;
211064
211065 assert( p->rc!=SQLITE_OK || pStruct->nLevel>0 );
211066 while( p->rc==SQLITE_OK && pStruct->aLevel[iLvl].nSeg>=nCrisis ){
211067 fts5IndexMergeLevel(p, &pStruct, iLvl, 0);
211068 assert( p->rc!=SQLITE_OK || pStruct->nLevel>(iLvl+1) );
211069 fts5StructurePromote(p, iLvl+1, pStruct);
211070 iLvl++;
211071 }
211072 *ppStruct = pStruct;
211073}
211074
211075static int fts5IndexReturn(Fts5Index *p){
211076 int rc = p->rc;
211077 p->rc = SQLITE_OK;
211078 return rc;
211079}
211080
211081typedef struct Fts5FlushCtx Fts5FlushCtx;
211082struct Fts5FlushCtx {
211083 Fts5Index *pIdx;
211084 Fts5SegWriter writer;
211085};
211086
211087/*
211088** Buffer aBuf[] contains a list of varints, all small enough to fit
211089** in a 32-bit integer. Return the size of the largest prefix of this
211090** list nMax bytes or less in size.
211091*/
211092static int fts5PoslistPrefix(const u8 *aBuf, int nMax){
211093 int ret;
211094 u32 dummy;
211095 ret = fts5GetVarint32(aBuf, dummy);
211096 if( ret<nMax ){
211097 while( 1 ){
211098 int i = fts5GetVarint32(&aBuf[ret], dummy);
211099 if( (ret + i) > nMax ) break;
211100 ret += i;
211101 }
211102 }
211103 return ret;
211104}
211105
211106/*
211107** Flush the contents of in-memory hash table iHash to a new level-0
211108** segment on disk. Also update the corresponding structure record.
211109**
211110** If an error occurs, set the Fts5Index.rc error code. If an error has
211111** already occurred, this function is a no-op.
211112*/
211113static void fts5FlushOneHash(Fts5Index *p){
211114 Fts5Hash *pHash = p->pHash;
211115 Fts5Structure *pStruct;
211116 int iSegid;
211117 int pgnoLast = 0; /* Last leaf page number in segment */
211118
211119 /* Obtain a reference to the index structure and allocate a new segment-id
211120 ** for the new level-0 segment. */
211121 pStruct = fts5StructureRead(p);
211122 iSegid = fts5AllocateSegid(p, pStruct);
211123 fts5StructureInvalidate(p);
211124
211125 if( iSegid ){
211126 const int pgsz = p->pConfig->pgsz;
211127 int eDetail = p->pConfig->eDetail;
211128 Fts5StructureSegment *pSeg; /* New segment within pStruct */
211129 Fts5Buffer *pBuf; /* Buffer in which to assemble leaf page */
211130 Fts5Buffer *pPgidx; /* Buffer in which to assemble pgidx */
211131
211132 Fts5SegWriter writer;
211133 fts5WriteInit(p, &writer, iSegid);
211134
211135 pBuf = &writer.writer.buf;
211136 pPgidx = &writer.writer.pgidx;
211137
211138 /* fts5WriteInit() should have initialized the buffers to (most likely)
211139 ** the maximum space required. */
211140 assert( p->rc || pBuf->nSpace>=(pgsz + FTS5_DATA_PADDING) );
211141 assert( p->rc || pPgidx->nSpace>=(pgsz + FTS5_DATA_PADDING) );
211142
211143 /* Begin scanning through hash table entries. This loop runs once for each
211144 ** term/doclist currently stored within the hash table. */
211145 if( p->rc==SQLITE_OK ){
211146 p->rc = sqlite3Fts5HashScanInit(pHash, 0, 0);
211147 }
211148 while( p->rc==SQLITE_OK && 0==sqlite3Fts5HashScanEof(pHash) ){
211149 const char *zTerm; /* Buffer containing term */
211150 const u8 *pDoclist; /* Pointer to doclist for this term */
211151 int nDoclist; /* Size of doclist in bytes */
211152
211153 /* Write the term for this entry to disk. */
211154 sqlite3Fts5HashScanEntry(pHash, &zTerm, &pDoclist, &nDoclist);
211155 fts5WriteAppendTerm(p, &writer, (int)strlen(zTerm), (const u8*)zTerm);
211156
211157 assert( writer.bFirstRowidInPage==0 );
211158 if( pgsz>=(pBuf->n + pPgidx->n + nDoclist + 1) ){
211159 /* The entire doclist will fit on the current leaf. */
211160 fts5BufferSafeAppendBlob(pBuf, pDoclist, nDoclist);
211161 }else{
211162 i64 iRowid = 0;
211163 i64 iDelta = 0;
211164 int iOff = 0;
211165
211166 /* The entire doclist will not fit on this leaf. The following
211167 ** loop iterates through the poslists that make up the current
211168 ** doclist. */
211169 while( p->rc==SQLITE_OK && iOff<nDoclist ){
211170 iOff += fts5GetVarint(&pDoclist[iOff], (u64*)&iDelta);
211171 iRowid += iDelta;
211172
211173 if( writer.bFirstRowidInPage ){
211174 fts5PutU16(&pBuf->p[0], (u16)pBuf->n); /* first rowid on page */
211175 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iRowid);
211176 writer.bFirstRowidInPage = 0;
211177 fts5WriteDlidxAppend(p, &writer, iRowid);
211178 }else{
211179 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iDelta);
211180 }
211181 assert( pBuf->n<=pBuf->nSpace );
211182
211183 if( eDetail==FTS5_DETAIL_NONE ){
211184 if( iOff<nDoclist && pDoclist[iOff]==0 ){
211185 pBuf->p[pBuf->n++] = 0;
211186 iOff++;
211187 if( iOff<nDoclist && pDoclist[iOff]==0 ){
211188 pBuf->p[pBuf->n++] = 0;
211189 iOff++;
211190 }
211191 }
211192 if( (pBuf->n + pPgidx->n)>=pgsz ){
211193 fts5WriteFlushLeaf(p, &writer);
211194 }
211195 }else{
211196 int bDummy;
211197 int nPos;
211198 int nCopy = fts5GetPoslistSize(&pDoclist[iOff], &nPos, &bDummy);
211199 nCopy += nPos;
211200 if( (pBuf->n + pPgidx->n + nCopy) <= pgsz ){
211201 /* The entire poslist will fit on the current leaf. So copy
211202 ** it in one go. */
211203 fts5BufferSafeAppendBlob(pBuf, &pDoclist[iOff], nCopy);
211204 }else{
211205 /* The entire poslist will not fit on this leaf. So it needs
211206 ** to be broken into sections. The only qualification being
211207 ** that each varint must be stored contiguously. */
211208 const u8 *pPoslist = &pDoclist[iOff];
211209 int iPos = 0;
211210 while( p->rc==SQLITE_OK ){
211211 int nSpace = pgsz - pBuf->n - pPgidx->n;
211212 int n = 0;
211213 if( (nCopy - iPos)<=nSpace ){
211214 n = nCopy - iPos;
211215 }else{
211216 n = fts5PoslistPrefix(&pPoslist[iPos], nSpace);
211217 }
211218 assert( n>0 );
211219 fts5BufferSafeAppendBlob(pBuf, &pPoslist[iPos], n);
211220 iPos += n;
211221 if( (pBuf->n + pPgidx->n)>=pgsz ){
211222 fts5WriteFlushLeaf(p, &writer);
211223 }
211224 if( iPos>=nCopy ) break;
211225 }
211226 }
211227 iOff += nCopy;
211228 }
211229 }
211230 }
211231
211232 /* TODO2: Doclist terminator written here. */
211233 /* pBuf->p[pBuf->n++] = '\0'; */
211234 assert( pBuf->n<=pBuf->nSpace );
211235 sqlite3Fts5HashScanNext(pHash);
211236 }
211237 sqlite3Fts5HashClear(pHash);
211238 fts5WriteFinish(p, &writer, &pgnoLast);
211239
211240 /* Update the Fts5Structure. It is written back to the database by the
211241 ** fts5StructureRelease() call below. */
211242 if( pStruct->nLevel==0 ){
211243 fts5StructureAddLevel(&p->rc, &pStruct);
211244 }
211245 fts5StructureExtendLevel(&p->rc, pStruct, 0, 1, 0);
211246 if( p->rc==SQLITE_OK ){
211247 pSeg = &pStruct->aLevel[0].aSeg[ pStruct->aLevel[0].nSeg++ ];
211248 pSeg->iSegid = iSegid;
211249 pSeg->pgnoFirst = 1;
211250 pSeg->pgnoLast = pgnoLast;
211251 pStruct->nSegment++;
211252 }
211253 fts5StructurePromote(p, 0, pStruct);
211254 }
211255
211256 fts5IndexAutomerge(p, &pStruct, pgnoLast);
211257 fts5IndexCrisismerge(p, &pStruct);
211258 fts5StructureWrite(p, pStruct);
211259 fts5StructureRelease(pStruct);
211260}
211261
211262/*
211263** Flush any data stored in the in-memory hash tables to the database.
211264*/
211265static void fts5IndexFlush(Fts5Index *p){
211266 /* Unless it is empty, flush the hash table to disk */
211267 if( p->nPendingData ){
211268 assert( p->pHash );
211269 p->nPendingData = 0;
211270 fts5FlushOneHash(p);
211271 }
211272}
211273
211274static Fts5Structure *fts5IndexOptimizeStruct(
211275 Fts5Index *p,
211276 Fts5Structure *pStruct
211277){
211278 Fts5Structure *pNew = 0;
211279 int nByte = sizeof(Fts5Structure);
211280 int nSeg = pStruct->nSegment;
211281 int i;
211282
211283 /* Figure out if this structure requires optimization. A structure does
211284 ** not require optimization if either:
211285 **
211286 ** + it consists of fewer than two segments, or
211287 ** + all segments are on the same level, or
211288 ** + all segments except one are currently inputs to a merge operation.
211289 **
211290 ** In the first case, return NULL. In the second, increment the ref-count
211291 ** on *pStruct and return a copy of the pointer to it.
211292 */
211293 if( nSeg<2 ) return 0;
211294 for(i=0; i<pStruct->nLevel; i++){
211295 int nThis = pStruct->aLevel[i].nSeg;
211296 if( nThis==nSeg || (nThis==nSeg-1 && pStruct->aLevel[i].nMerge==nThis) ){
211297 fts5StructureRef(pStruct);
211298 return pStruct;
211299 }
211300 assert( pStruct->aLevel[i].nMerge<=nThis );
211301 }
211302
211303 nByte += (pStruct->nLevel+1) * sizeof(Fts5StructureLevel);
211304 pNew = (Fts5Structure*)sqlite3Fts5MallocZero(&p->rc, nByte);
211305
211306 if( pNew ){
211307 Fts5StructureLevel *pLvl;
211308 nByte = nSeg * sizeof(Fts5StructureSegment);
211309 pNew->nLevel = pStruct->nLevel+1;
211310 pNew->nRef = 1;
211311 pNew->nWriteCounter = pStruct->nWriteCounter;
211312 pLvl = &pNew->aLevel[pStruct->nLevel];
211313 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&p->rc, nByte);
211314 if( pLvl->aSeg ){
211315 int iLvl, iSeg;
211316 int iSegOut = 0;
211317 /* Iterate through all segments, from oldest to newest. Add them to
211318 ** the new Fts5Level object so that pLvl->aSeg[0] is the oldest
211319 ** segment in the data structure. */
211320 for(iLvl=pStruct->nLevel-1; iLvl>=0; iLvl--){
211321 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
211322 pLvl->aSeg[iSegOut] = pStruct->aLevel[iLvl].aSeg[iSeg];
211323 iSegOut++;
211324 }
211325 }
211326 pNew->nSegment = pLvl->nSeg = nSeg;
211327 }else{
211328 sqlite3_free(pNew);
211329 pNew = 0;
211330 }
211331 }
211332
211333 return pNew;
211334}
211335
211336static int sqlite3Fts5IndexOptimize(Fts5Index *p){
211337 Fts5Structure *pStruct;
211338 Fts5Structure *pNew = 0;
211339
211340 assert( p->rc==SQLITE_OK );
211341 fts5IndexFlush(p);
211342 pStruct = fts5StructureRead(p);
211343 fts5StructureInvalidate(p);
211344
211345 if( pStruct ){
211346 pNew = fts5IndexOptimizeStruct(p, pStruct);
211347 }
211348 fts5StructureRelease(pStruct);
211349
211350 assert( pNew==0 || pNew->nSegment>0 );
211351 if( pNew ){
211352 int iLvl;
211353 for(iLvl=0; pNew->aLevel[iLvl].nSeg==0; iLvl++){}
211354 while( p->rc==SQLITE_OK && pNew->aLevel[iLvl].nSeg>0 ){
211355 int nRem = FTS5_OPT_WORK_UNIT;
211356 fts5IndexMergeLevel(p, &pNew, iLvl, &nRem);
211357 }
211358
211359 fts5StructureWrite(p, pNew);
211360 fts5StructureRelease(pNew);
211361 }
211362
211363 return fts5IndexReturn(p);
211364}
211365
211366/*
211367** This is called to implement the special "VALUES('merge', $nMerge)"
211368** INSERT command.
211369*/
211370static int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge){
211371 Fts5Structure *pStruct = fts5StructureRead(p);
211372 if( pStruct ){
211373 int nMin = p->pConfig->nUsermerge;
211374 fts5StructureInvalidate(p);
211375 if( nMerge<0 ){
211376 Fts5Structure *pNew = fts5IndexOptimizeStruct(p, pStruct);
211377 fts5StructureRelease(pStruct);
211378 pStruct = pNew;
211379 nMin = 2;
211380 nMerge = nMerge*-1;
211381 }
211382 if( pStruct && pStruct->nLevel ){
211383 if( fts5IndexMerge(p, &pStruct, nMerge, nMin) ){
211384 fts5StructureWrite(p, pStruct);
211385 }
211386 }
211387 fts5StructureRelease(pStruct);
211388 }
211389 return fts5IndexReturn(p);
211390}
211391
211392static void fts5AppendRowid(
211393 Fts5Index *p,
211394 i64 iDelta,
211395 Fts5Iter *pUnused,
211396 Fts5Buffer *pBuf
211397){
211398 UNUSED_PARAM(pUnused);
211399 fts5BufferAppendVarint(&p->rc, pBuf, iDelta);
211400}
211401
211402static void fts5AppendPoslist(
211403 Fts5Index *p,
211404 i64 iDelta,
211405 Fts5Iter *pMulti,
211406 Fts5Buffer *pBuf
211407){
211408 int nData = pMulti->base.nData;
211409 assert( nData>0 );
211410 if( p->rc==SQLITE_OK && 0==fts5BufferGrow(&p->rc, pBuf, nData+9+9) ){
211411 fts5BufferSafeAppendVarint(pBuf, iDelta);
211412 fts5BufferSafeAppendVarint(pBuf, nData*2);
211413 fts5BufferSafeAppendBlob(pBuf, pMulti->base.pData, nData);
211414 }
211415}
211416
211417
211418static void fts5DoclistIterNext(Fts5DoclistIter *pIter){
211419 u8 *p = pIter->aPoslist + pIter->nSize + pIter->nPoslist;
211420
211421 assert( pIter->aPoslist );
211422 if( p>=pIter->aEof ){
211423 pIter->aPoslist = 0;
211424 }else{
211425 i64 iDelta;
211426
211427 p += fts5GetVarint(p, (u64*)&iDelta);
211428 pIter->iRowid += iDelta;
211429
211430 /* Read position list size */
211431 if( p[0] & 0x80 ){
211432 int nPos;
211433 pIter->nSize = fts5GetVarint32(p, nPos);
211434 pIter->nPoslist = (nPos>>1);
211435 }else{
211436 pIter->nPoslist = ((int)(p[0])) >> 1;
211437 pIter->nSize = 1;
211438 }
211439
211440 pIter->aPoslist = p;
211441 }
211442}
211443
211444static void fts5DoclistIterInit(
211445 Fts5Buffer *pBuf,
211446 Fts5DoclistIter *pIter
211447){
211448 memset(pIter, 0, sizeof(*pIter));
211449 pIter->aPoslist = pBuf->p;
211450 pIter->aEof = &pBuf->p[pBuf->n];
211451 fts5DoclistIterNext(pIter);
211452}
211453
211454#if 0
211455/*
211456** Append a doclist to buffer pBuf.
211457**
211458** This function assumes that space within the buffer has already been
211459** allocated.
211460*/
211461static void fts5MergeAppendDocid(
211462 Fts5Buffer *pBuf, /* Buffer to write to */
211463 i64 *piLastRowid, /* IN/OUT: Previous rowid written (if any) */
211464 i64 iRowid /* Rowid to append */
211465){
211466 assert( pBuf->n!=0 || (*piLastRowid)==0 );
211467 fts5BufferSafeAppendVarint(pBuf, iRowid - *piLastRowid);
211468 *piLastRowid = iRowid;
211469}
211470#endif
211471
211472#define fts5MergeAppendDocid(pBuf, iLastRowid, iRowid) { \
211473 assert( (pBuf)->n!=0 || (iLastRowid)==0 ); \
211474 fts5BufferSafeAppendVarint((pBuf), (iRowid) - (iLastRowid)); \
211475 (iLastRowid) = (iRowid); \
211476}
211477
211478/*
211479** Swap the contents of buffer *p1 with that of *p2.
211480*/
211481static void fts5BufferSwap(Fts5Buffer *p1, Fts5Buffer *p2){
211482 Fts5Buffer tmp = *p1;
211483 *p1 = *p2;
211484 *p2 = tmp;
211485}
211486
211487static void fts5NextRowid(Fts5Buffer *pBuf, int *piOff, i64 *piRowid){
211488 int i = *piOff;
211489 if( i>=pBuf->n ){
211490 *piOff = -1;
211491 }else{
211492 u64 iVal;
211493 *piOff = i + sqlite3Fts5GetVarint(&pBuf->p[i], &iVal);
211494 *piRowid += iVal;
211495 }
211496}
211497
211498/*
211499** This is the equivalent of fts5MergePrefixLists() for detail=none mode.
211500** In this case the buffers consist of a delta-encoded list of rowids only.
211501*/
211502static void fts5MergeRowidLists(
211503 Fts5Index *p, /* FTS5 backend object */
211504 Fts5Buffer *p1, /* First list to merge */
211505 Fts5Buffer *p2 /* Second list to merge */
211506){
211507 int i1 = 0;
211508 int i2 = 0;
211509 i64 iRowid1 = 0;
211510 i64 iRowid2 = 0;
211511 i64 iOut = 0;
211512
211513 Fts5Buffer out;
211514 memset(&out, 0, sizeof(out));
211515 sqlite3Fts5BufferSize(&p->rc, &out, p1->n + p2->n);
211516 if( p->rc ) return;
211517
211518 fts5NextRowid(p1, &i1, &iRowid1);
211519 fts5NextRowid(p2, &i2, &iRowid2);
211520 while( i1>=0 || i2>=0 ){
211521 if( i1>=0 && (i2<0 || iRowid1<iRowid2) ){
211522 assert( iOut==0 || iRowid1>iOut );
211523 fts5BufferSafeAppendVarint(&out, iRowid1 - iOut);
211524 iOut = iRowid1;
211525 fts5NextRowid(p1, &i1, &iRowid1);
211526 }else{
211527 assert( iOut==0 || iRowid2>iOut );
211528 fts5BufferSafeAppendVarint(&out, iRowid2 - iOut);
211529 iOut = iRowid2;
211530 if( i1>=0 && iRowid1==iRowid2 ){
211531 fts5NextRowid(p1, &i1, &iRowid1);
211532 }
211533 fts5NextRowid(p2, &i2, &iRowid2);
211534 }
211535 }
211536
211537 fts5BufferSwap(&out, p1);
211538 fts5BufferFree(&out);
211539}
211540
211541/*
211542** Buffers p1 and p2 contain doclists. This function merges the content
211543** of the two doclists together and sets buffer p1 to the result before
211544** returning.
211545**
211546** If an error occurs, an error code is left in p->rc. If an error has
211547** already occurred, this function is a no-op.
211548*/
211549static void fts5MergePrefixLists(
211550 Fts5Index *p, /* FTS5 backend object */
211551 Fts5Buffer *p1, /* First list to merge */
211552 Fts5Buffer *p2 /* Second list to merge */
211553){
211554 if( p2->n ){
211555 i64 iLastRowid = 0;
211556 Fts5DoclistIter i1;
211557 Fts5DoclistIter i2;
211558 Fts5Buffer out = {0, 0, 0};
211559 Fts5Buffer tmp = {0, 0, 0};
211560
211561 /* The maximum size of the output is equal to the sum of the two
211562 ** input sizes + 1 varint (9 bytes). The extra varint is because if the
211563 ** first rowid in one input is a large negative number, and the first in
211564 ** the other a non-negative number, the delta for the non-negative
211565 ** number will be larger on disk than the literal integer value
211566 ** was. */
211567 if( sqlite3Fts5BufferSize(&p->rc, &out, p1->n + p2->n + 9) ) return;
211568 fts5DoclistIterInit(p1, &i1);
211569 fts5DoclistIterInit(p2, &i2);
211570
211571 while( 1 ){
211572 if( i1.iRowid<i2.iRowid ){
211573 /* Copy entry from i1 */
211574 fts5MergeAppendDocid(&out, iLastRowid, i1.iRowid);
211575 fts5BufferSafeAppendBlob(&out, i1.aPoslist, i1.nPoslist+i1.nSize);
211576 fts5DoclistIterNext(&i1);
211577 if( i1.aPoslist==0 ) break;
211578 }
211579 else if( i2.iRowid!=i1.iRowid ){
211580 /* Copy entry from i2 */
211581 fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
211582 fts5BufferSafeAppendBlob(&out, i2.aPoslist, i2.nPoslist+i2.nSize);
211583 fts5DoclistIterNext(&i2);
211584 if( i2.aPoslist==0 ) break;
211585 }
211586 else{
211587 /* Merge the two position lists. */
211588 i64 iPos1 = 0;
211589 i64 iPos2 = 0;
211590 int iOff1 = 0;
211591 int iOff2 = 0;
211592 u8 *a1 = &i1.aPoslist[i1.nSize];
211593 u8 *a2 = &i2.aPoslist[i2.nSize];
211594
211595 i64 iPrev = 0;
211596 Fts5PoslistWriter writer;
211597 memset(&writer, 0, sizeof(writer));
211598
211599 fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
211600 fts5BufferZero(&tmp);
211601 sqlite3Fts5BufferSize(&p->rc, &tmp, i1.nPoslist + i2.nPoslist);
211602 if( p->rc ) break;
211603
211604 sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
211605 sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
211606 assert( iPos1>=0 && iPos2>=0 );
211607
211608 if( iPos1<iPos2 ){
211609 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
211610 sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
211611 }else{
211612 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2);
211613 sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
211614 }
211615
211616 if( iPos1>=0 && iPos2>=0 ){
211617 while( 1 ){
211618 if( iPos1<iPos2 ){
211619 if( iPos1!=iPrev ){
211620 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
211621 }
211622 sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
211623 if( iPos1<0 ) break;
211624 }else{
211625 assert( iPos2!=iPrev );
211626 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2);
211627 sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
211628 if( iPos2<0 ) break;
211629 }
211630 }
211631 }
211632
211633 if( iPos1>=0 ){
211634 if( iPos1!=iPrev ){
211635 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
211636 }
211637 fts5BufferSafeAppendBlob(&tmp, &a1[iOff1], i1.nPoslist-iOff1);
211638 }else{
211639 assert( iPos2>=0 && iPos2!=iPrev );
211640 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2);
211641 fts5BufferSafeAppendBlob(&tmp, &a2[iOff2], i2.nPoslist-iOff2);
211642 }
211643
211644 /* WRITEPOSLISTSIZE */
211645 fts5BufferSafeAppendVarint(&out, tmp.n * 2);
211646 fts5BufferSafeAppendBlob(&out, tmp.p, tmp.n);
211647 fts5DoclistIterNext(&i1);
211648 fts5DoclistIterNext(&i2);
211649 if( i1.aPoslist==0 || i2.aPoslist==0 ) break;
211650 }
211651 }
211652
211653 if( i1.aPoslist ){
211654 fts5MergeAppendDocid(&out, iLastRowid, i1.iRowid);
211655 fts5BufferSafeAppendBlob(&out, i1.aPoslist, i1.aEof - i1.aPoslist);
211656 }
211657 else if( i2.aPoslist ){
211658 fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
211659 fts5BufferSafeAppendBlob(&out, i2.aPoslist, i2.aEof - i2.aPoslist);
211660 }
211661 assert( out.n<=(p1->n+p2->n+9) );
211662
211663 fts5BufferSet(&p->rc, p1, out.n, out.p);
211664 fts5BufferFree(&tmp);
211665 fts5BufferFree(&out);
211666 }
211667}
211668
211669static void fts5SetupPrefixIter(
211670 Fts5Index *p, /* Index to read from */
211671 int bDesc, /* True for "ORDER BY rowid DESC" */
211672 const u8 *pToken, /* Buffer containing prefix to match */
211673 int nToken, /* Size of buffer pToken in bytes */
211674 Fts5Colset *pColset, /* Restrict matches to these columns */
211675 Fts5Iter **ppIter /* OUT: New iterator */
211676){
211677 Fts5Structure *pStruct;
211678 Fts5Buffer *aBuf;
211679 const int nBuf = 32;
211680
211681 void (*xMerge)(Fts5Index*, Fts5Buffer*, Fts5Buffer*);
211682 void (*xAppend)(Fts5Index*, i64, Fts5Iter*, Fts5Buffer*);
211683 if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
211684 xMerge = fts5MergeRowidLists;
211685 xAppend = fts5AppendRowid;
211686 }else{
211687 xMerge = fts5MergePrefixLists;
211688 xAppend = fts5AppendPoslist;
211689 }
211690
211691 aBuf = (Fts5Buffer*)fts5IdxMalloc(p, sizeof(Fts5Buffer)*nBuf);
211692 pStruct = fts5StructureRead(p);
211693
211694 if( aBuf && pStruct ){
211695 const int flags = FTS5INDEX_QUERY_SCAN
211696 | FTS5INDEX_QUERY_SKIPEMPTY
211697 | FTS5INDEX_QUERY_NOOUTPUT;
211698 int i;
211699 i64 iLastRowid = 0;
211700 Fts5Iter *p1 = 0; /* Iterator used to gather data from index */
211701 Fts5Data *pData;
211702 Fts5Buffer doclist;
211703 int bNewTerm = 1;
211704
211705 memset(&doclist, 0, sizeof(doclist));
211706 fts5MultiIterNew(p, pStruct, flags, pColset, pToken, nToken, -1, 0, &p1);
211707 fts5IterSetOutputCb(&p->rc, p1);
211708 for( /* no-op */ ;
211709 fts5MultiIterEof(p, p1)==0;
211710 fts5MultiIterNext2(p, p1, &bNewTerm)
211711 ){
211712 Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ];
211713 int nTerm = pSeg->term.n;
211714 const u8 *pTerm = pSeg->term.p;
211715 p1->xSetOutputs(p1, pSeg);
211716
211717 assert_nc( memcmp(pToken, pTerm, MIN(nToken, nTerm))<=0 );
211718 if( bNewTerm ){
211719 if( nTerm<nToken || memcmp(pToken, pTerm, nToken) ) break;
211720 }
211721
211722 if( p1->base.nData==0 ) continue;
211723
211724 if( p1->base.iRowid<=iLastRowid && doclist.n>0 ){
211725 for(i=0; p->rc==SQLITE_OK && doclist.n; i++){
211726 assert( i<nBuf );
211727 if( aBuf[i].n==0 ){
211728 fts5BufferSwap(&doclist, &aBuf[i]);
211729 fts5BufferZero(&doclist);
211730 }else{
211731 xMerge(p, &doclist, &aBuf[i]);
211732 fts5BufferZero(&aBuf[i]);
211733 }
211734 }
211735 iLastRowid = 0;
211736 }
211737
211738 xAppend(p, p1->base.iRowid-iLastRowid, p1, &doclist);
211739 iLastRowid = p1->base.iRowid;
211740 }
211741
211742 for(i=0; i<nBuf; i++){
211743 if( p->rc==SQLITE_OK ){
211744 xMerge(p, &doclist, &aBuf[i]);
211745 }
211746 fts5BufferFree(&aBuf[i]);
211747 }
211748 fts5MultiIterFree(p1);
211749
211750 pData = fts5IdxMalloc(p, sizeof(Fts5Data) + doclist.n);
211751 if( pData ){
211752 pData->p = (u8*)&pData[1];
211753 pData->nn = pData->szLeaf = doclist.n;
211754 if( doclist.n ) memcpy(pData->p, doclist.p, doclist.n);
211755 fts5MultiIterNew2(p, pData, bDesc, ppIter);
211756 }
211757 fts5BufferFree(&doclist);
211758 }
211759
211760 fts5StructureRelease(pStruct);
211761 sqlite3_free(aBuf);
211762}
211763
211764
211765/*
211766** Indicate that all subsequent calls to sqlite3Fts5IndexWrite() pertain
211767** to the document with rowid iRowid.
211768*/
211769static int sqlite3Fts5IndexBeginWrite(Fts5Index *p, int bDelete, i64 iRowid){
211770 assert( p->rc==SQLITE_OK );
211771
211772 /* Allocate the hash table if it has not already been allocated */
211773 if( p->pHash==0 ){
211774 p->rc = sqlite3Fts5HashNew(p->pConfig, &p->pHash, &p->nPendingData);
211775 }
211776
211777 /* Flush the hash table to disk if required */
211778 if( iRowid<p->iWriteRowid
211779 || (iRowid==p->iWriteRowid && p->bDelete==0)
211780 || (p->nPendingData > p->pConfig->nHashSize)
211781 ){
211782 fts5IndexFlush(p);
211783 }
211784
211785 p->iWriteRowid = iRowid;
211786 p->bDelete = bDelete;
211787 return fts5IndexReturn(p);
211788}
211789
211790/*
211791** Commit data to disk.
211792*/
211793static int sqlite3Fts5IndexSync(Fts5Index *p){
211794 assert( p->rc==SQLITE_OK );
211795 fts5IndexFlush(p);
211796 fts5CloseReader(p);
211797 return fts5IndexReturn(p);
211798}
211799
211800/*
211801** Discard any data stored in the in-memory hash tables. Do not write it
211802** to the database. Additionally, assume that the contents of the %_data
211803** table may have changed on disk. So any in-memory caches of %_data
211804** records must be invalidated.
211805*/
211806static int sqlite3Fts5IndexRollback(Fts5Index *p){
211807 fts5CloseReader(p);
211808 fts5IndexDiscardData(p);
211809 fts5StructureInvalidate(p);
211810 /* assert( p->rc==SQLITE_OK ); */
211811 return SQLITE_OK;
211812}
211813
211814/*
211815** The %_data table is completely empty when this function is called. This
211816** function populates it with the initial structure objects for each index,
211817** and the initial version of the "averages" record (a zero-byte blob).
211818*/
211819static int sqlite3Fts5IndexReinit(Fts5Index *p){
211820 Fts5Structure s;
211821 fts5StructureInvalidate(p);
211822 memset(&s, 0, sizeof(Fts5Structure));
211823 fts5DataWrite(p, FTS5_AVERAGES_ROWID, (const u8*)"", 0);
211824 fts5StructureWrite(p, &s);
211825 return fts5IndexReturn(p);
211826}
211827
211828/*
211829** Open a new Fts5Index handle. If the bCreate argument is true, create
211830** and initialize the underlying %_data table.
211831**
211832** If successful, set *pp to point to the new object and return SQLITE_OK.
211833** Otherwise, set *pp to NULL and return an SQLite error code.
211834*/
211835static int sqlite3Fts5IndexOpen(
211836 Fts5Config *pConfig,
211837 int bCreate,
211838 Fts5Index **pp,
211839 char **pzErr
211840){
211841 int rc = SQLITE_OK;
211842 Fts5Index *p; /* New object */
211843
211844 *pp = p = (Fts5Index*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Index));
211845 if( rc==SQLITE_OK ){
211846 p->pConfig = pConfig;
211847 p->nWorkUnit = FTS5_WORK_UNIT;
211848 p->zDataTbl = sqlite3Fts5Mprintf(&rc, "%s_data", pConfig->zName);
211849 if( p->zDataTbl && bCreate ){
211850 rc = sqlite3Fts5CreateTable(
211851 pConfig, "data", "id INTEGER PRIMARY KEY, block BLOB", 0, pzErr
211852 );
211853 if( rc==SQLITE_OK ){
211854 rc = sqlite3Fts5CreateTable(pConfig, "idx",
211855 "segid, term, pgno, PRIMARY KEY(segid, term)",
211856 1, pzErr
211857 );
211858 }
211859 if( rc==SQLITE_OK ){
211860 rc = sqlite3Fts5IndexReinit(p);
211861 }
211862 }
211863 }
211864
211865 assert( rc!=SQLITE_OK || p->rc==SQLITE_OK );
211866 if( rc ){
211867 sqlite3Fts5IndexClose(p);
211868 *pp = 0;
211869 }
211870 return rc;
211871}
211872
211873/*
211874** Close a handle opened by an earlier call to sqlite3Fts5IndexOpen().
211875*/
211876static int sqlite3Fts5IndexClose(Fts5Index *p){
211877 int rc = SQLITE_OK;
211878 if( p ){
211879 assert( p->pReader==0 );
211880 fts5StructureInvalidate(p);
211881 sqlite3_finalize(p->pWriter);
211882 sqlite3_finalize(p->pDeleter);
211883 sqlite3_finalize(p->pIdxWriter);
211884 sqlite3_finalize(p->pIdxDeleter);
211885 sqlite3_finalize(p->pIdxSelect);
211886 sqlite3_finalize(p->pDataVersion);
211887 sqlite3Fts5HashFree(p->pHash);
211888 sqlite3_free(p->zDataTbl);
211889 sqlite3_free(p);
211890 }
211891 return rc;
211892}
211893
211894/*
211895** Argument p points to a buffer containing utf-8 text that is n bytes in
211896** size. Return the number of bytes in the nChar character prefix of the
211897** buffer, or 0 if there are less than nChar characters in total.
211898*/
211899static int sqlite3Fts5IndexCharlenToBytelen(
211900 const char *p,
211901 int nByte,
211902 int nChar
211903){
211904 int n = 0;
211905 int i;
211906 for(i=0; i<nChar; i++){
211907 if( n>=nByte ) return 0; /* Input contains fewer than nChar chars */
211908 if( (unsigned char)p[n++]>=0xc0 ){
211909 while( (p[n] & 0xc0)==0x80 ){
211910 n++;
211911 if( n>=nByte ) break;
211912 }
211913 }
211914 }
211915 return n;
211916}
211917
211918/*
211919** pIn is a UTF-8 encoded string, nIn bytes in size. Return the number of
211920** unicode characters in the string.
211921*/
211922static int fts5IndexCharlen(const char *pIn, int nIn){
211923 int nChar = 0;
211924 int i = 0;
211925 while( i<nIn ){
211926 if( (unsigned char)pIn[i++]>=0xc0 ){
211927 while( i<nIn && (pIn[i] & 0xc0)==0x80 ) i++;
211928 }
211929 nChar++;
211930 }
211931 return nChar;
211932}
211933
211934/*
211935** Insert or remove data to or from the index. Each time a document is
211936** added to or removed from the index, this function is called one or more
211937** times.
211938**
211939** For an insert, it must be called once for each token in the new document.
211940** If the operation is a delete, it must be called (at least) once for each
211941** unique token in the document with an iCol value less than zero. The iPos
211942** argument is ignored for a delete.
211943*/
211944static int sqlite3Fts5IndexWrite(
211945 Fts5Index *p, /* Index to write to */
211946 int iCol, /* Column token appears in (-ve -> delete) */
211947 int iPos, /* Position of token within column */
211948 const char *pToken, int nToken /* Token to add or remove to or from index */
211949){
211950 int i; /* Used to iterate through indexes */
211951 int rc = SQLITE_OK; /* Return code */
211952 Fts5Config *pConfig = p->pConfig;
211953
211954 assert( p->rc==SQLITE_OK );
211955 assert( (iCol<0)==p->bDelete );
211956
211957 /* Add the entry to the main terms index. */
211958 rc = sqlite3Fts5HashWrite(
211959 p->pHash, p->iWriteRowid, iCol, iPos, FTS5_MAIN_PREFIX, pToken, nToken
211960 );
211961
211962 for(i=0; i<pConfig->nPrefix && rc==SQLITE_OK; i++){
211963 const int nChar = pConfig->aPrefix[i];
211964 int nByte = sqlite3Fts5IndexCharlenToBytelen(pToken, nToken, nChar);
211965 if( nByte ){
211966 rc = sqlite3Fts5HashWrite(p->pHash,
211967 p->iWriteRowid, iCol, iPos, (char)(FTS5_MAIN_PREFIX+i+1), pToken,
211968 nByte
211969 );
211970 }
211971 }
211972
211973 return rc;
211974}
211975
211976/*
211977** Open a new iterator to iterate though all rowid that match the
211978** specified token or token prefix.
211979*/
211980static int sqlite3Fts5IndexQuery(
211981 Fts5Index *p, /* FTS index to query */
211982 const char *pToken, int nToken, /* Token (or prefix) to query for */
211983 int flags, /* Mask of FTS5INDEX_QUERY_X flags */
211984 Fts5Colset *pColset, /* Match these columns only */
211985 Fts5IndexIter **ppIter /* OUT: New iterator object */
211986){
211987 Fts5Config *pConfig = p->pConfig;
211988 Fts5Iter *pRet = 0;
211989 Fts5Buffer buf = {0, 0, 0};
211990
211991 /* If the QUERY_SCAN flag is set, all other flags must be clear. */
211992 assert( (flags & FTS5INDEX_QUERY_SCAN)==0 || flags==FTS5INDEX_QUERY_SCAN );
211993
211994 if( sqlite3Fts5BufferSize(&p->rc, &buf, nToken+1)==0 ){
211995 int iIdx = 0; /* Index to search */
211996 if( nToken ) memcpy(&buf.p[1], pToken, nToken);
211997
211998 /* Figure out which index to search and set iIdx accordingly. If this
211999 ** is a prefix query for which there is no prefix index, set iIdx to
212000 ** greater than pConfig->nPrefix to indicate that the query will be
212001 ** satisfied by scanning multiple terms in the main index.
212002 **
212003 ** If the QUERY_TEST_NOIDX flag was specified, then this must be a
212004 ** prefix-query. Instead of using a prefix-index (if one exists),
212005 ** evaluate the prefix query using the main FTS index. This is used
212006 ** for internal sanity checking by the integrity-check in debug
212007 ** mode only. */
212008#ifdef SQLITE_DEBUG
212009 if( pConfig->bPrefixIndex==0 || (flags & FTS5INDEX_QUERY_TEST_NOIDX) ){
212010 assert( flags & FTS5INDEX_QUERY_PREFIX );
212011 iIdx = 1+pConfig->nPrefix;
212012 }else
212013#endif
212014 if( flags & FTS5INDEX_QUERY_PREFIX ){
212015 int nChar = fts5IndexCharlen(pToken, nToken);
212016 for(iIdx=1; iIdx<=pConfig->nPrefix; iIdx++){
212017 if( pConfig->aPrefix[iIdx-1]==nChar ) break;
212018 }
212019 }
212020
212021 if( iIdx<=pConfig->nPrefix ){
212022 /* Straight index lookup */
212023 Fts5Structure *pStruct = fts5StructureRead(p);
212024 buf.p[0] = (u8)(FTS5_MAIN_PREFIX + iIdx);
212025 if( pStruct ){
212026 fts5MultiIterNew(p, pStruct, flags | FTS5INDEX_QUERY_SKIPEMPTY,
212027 pColset, buf.p, nToken+1, -1, 0, &pRet
212028 );
212029 fts5StructureRelease(pStruct);
212030 }
212031 }else{
212032 /* Scan multiple terms in the main index */
212033 int bDesc = (flags & FTS5INDEX_QUERY_DESC)!=0;
212034 buf.p[0] = FTS5_MAIN_PREFIX;
212035 fts5SetupPrefixIter(p, bDesc, buf.p, nToken+1, pColset, &pRet);
212036 assert( p->rc!=SQLITE_OK || pRet->pColset==0 );
212037 fts5IterSetOutputCb(&p->rc, pRet);
212038 if( p->rc==SQLITE_OK ){
212039 Fts5SegIter *pSeg = &pRet->aSeg[pRet->aFirst[1].iFirst];
212040 if( pSeg->pLeaf ) pRet->xSetOutputs(pRet, pSeg);
212041 }
212042 }
212043
212044 if( p->rc ){
212045 sqlite3Fts5IterClose((Fts5IndexIter*)pRet);
212046 pRet = 0;
212047 fts5CloseReader(p);
212048 }
212049
212050 *ppIter = (Fts5IndexIter*)pRet;
212051 sqlite3Fts5BufferFree(&buf);
212052 }
212053 return fts5IndexReturn(p);
212054}
212055
212056/*
212057** Return true if the iterator passed as the only argument is at EOF.
212058*/
212059/*
212060** Move to the next matching rowid.
212061*/
212062static int sqlite3Fts5IterNext(Fts5IndexIter *pIndexIter){
212063 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
212064 assert( pIter->pIndex->rc==SQLITE_OK );
212065 fts5MultiIterNext(pIter->pIndex, pIter, 0, 0);
212066 return fts5IndexReturn(pIter->pIndex);
212067}
212068
212069/*
212070** Move to the next matching term/rowid. Used by the fts5vocab module.
212071*/
212072static int sqlite3Fts5IterNextScan(Fts5IndexIter *pIndexIter){
212073 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
212074 Fts5Index *p = pIter->pIndex;
212075
212076 assert( pIter->pIndex->rc==SQLITE_OK );
212077
212078 fts5MultiIterNext(p, pIter, 0, 0);
212079 if( p->rc==SQLITE_OK ){
212080 Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
212081 if( pSeg->pLeaf && pSeg->term.p[0]!=FTS5_MAIN_PREFIX ){
212082 fts5DataRelease(pSeg->pLeaf);
212083 pSeg->pLeaf = 0;
212084 pIter->base.bEof = 1;
212085 }
212086 }
212087
212088 return fts5IndexReturn(pIter->pIndex);
212089}
212090
212091/*
212092** Move to the next matching rowid that occurs at or after iMatch. The
212093** definition of "at or after" depends on whether this iterator iterates
212094** in ascending or descending rowid order.
212095*/
212096static int sqlite3Fts5IterNextFrom(Fts5IndexIter *pIndexIter, i64 iMatch){
212097 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
212098 fts5MultiIterNextFrom(pIter->pIndex, pIter, iMatch);
212099 return fts5IndexReturn(pIter->pIndex);
212100}
212101
212102/*
212103** Return the current term.
212104*/
212105static const char *sqlite3Fts5IterTerm(Fts5IndexIter *pIndexIter, int *pn){
212106 int n;
212107 const char *z = (const char*)fts5MultiIterTerm((Fts5Iter*)pIndexIter, &n);
212108 *pn = n-1;
212109 return &z[1];
212110}
212111
212112/*
212113** Close an iterator opened by an earlier call to sqlite3Fts5IndexQuery().
212114*/
212115static void sqlite3Fts5IterClose(Fts5IndexIter *pIndexIter){
212116 if( pIndexIter ){
212117 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
212118 Fts5Index *pIndex = pIter->pIndex;
212119 fts5MultiIterFree(pIter);
212120 fts5CloseReader(pIndex);
212121 }
212122}
212123
212124/*
212125** Read and decode the "averages" record from the database.
212126**
212127** Parameter anSize must point to an array of size nCol, where nCol is
212128** the number of user defined columns in the FTS table.
212129*/
212130static int sqlite3Fts5IndexGetAverages(Fts5Index *p, i64 *pnRow, i64 *anSize){
212131 int nCol = p->pConfig->nCol;
212132 Fts5Data *pData;
212133
212134 *pnRow = 0;
212135 memset(anSize, 0, sizeof(i64) * nCol);
212136 pData = fts5DataRead(p, FTS5_AVERAGES_ROWID);
212137 if( p->rc==SQLITE_OK && pData->nn ){
212138 int i = 0;
212139 int iCol;
212140 i += fts5GetVarint(&pData->p[i], (u64*)pnRow);
212141 for(iCol=0; i<pData->nn && iCol<nCol; iCol++){
212142 i += fts5GetVarint(&pData->p[i], (u64*)&anSize[iCol]);
212143 }
212144 }
212145
212146 fts5DataRelease(pData);
212147 return fts5IndexReturn(p);
212148}
212149
212150/*
212151** Replace the current "averages" record with the contents of the buffer
212152** supplied as the second argument.
212153*/
212154static int sqlite3Fts5IndexSetAverages(Fts5Index *p, const u8 *pData, int nData){
212155 assert( p->rc==SQLITE_OK );
212156 fts5DataWrite(p, FTS5_AVERAGES_ROWID, pData, nData);
212157 return fts5IndexReturn(p);
212158}
212159
212160/*
212161** Return the total number of blocks this module has read from the %_data
212162** table since it was created.
212163*/
212164static int sqlite3Fts5IndexReads(Fts5Index *p){
212165 return p->nRead;
212166}
212167
212168/*
212169** Set the 32-bit cookie value stored at the start of all structure
212170** records to the value passed as the second argument.
212171**
212172** Return SQLITE_OK if successful, or an SQLite error code if an error
212173** occurs.
212174*/
212175static int sqlite3Fts5IndexSetCookie(Fts5Index *p, int iNew){
212176 int rc; /* Return code */
212177 Fts5Config *pConfig = p->pConfig; /* Configuration object */
212178 u8 aCookie[4]; /* Binary representation of iNew */
212179 sqlite3_blob *pBlob = 0;
212180
212181 assert( p->rc==SQLITE_OK );
212182 sqlite3Fts5Put32(aCookie, iNew);
212183
212184 rc = sqlite3_blob_open(pConfig->db, pConfig->zDb, p->zDataTbl,
212185 "block", FTS5_STRUCTURE_ROWID, 1, &pBlob
212186 );
212187 if( rc==SQLITE_OK ){
212188 sqlite3_blob_write(pBlob, aCookie, 4, 0);
212189 rc = sqlite3_blob_close(pBlob);
212190 }
212191
212192 return rc;
212193}
212194
212195static int sqlite3Fts5IndexLoadConfig(Fts5Index *p){
212196 Fts5Structure *pStruct;
212197 pStruct = fts5StructureRead(p);
212198 fts5StructureRelease(pStruct);
212199 return fts5IndexReturn(p);
212200}
212201
212202
212203/*************************************************************************
212204**************************************************************************
212205** Below this point is the implementation of the integrity-check
212206** functionality.
212207*/
212208
212209/*
212210** Return a simple checksum value based on the arguments.
212211*/
212212static u64 sqlite3Fts5IndexEntryCksum(
212213 i64 iRowid,
212214 int iCol,
212215 int iPos,
212216 int iIdx,
212217 const char *pTerm,
212218 int nTerm
212219){
212220 int i;
212221 u64 ret = iRowid;
212222 ret += (ret<<3) + iCol;
212223 ret += (ret<<3) + iPos;
212224 if( iIdx>=0 ) ret += (ret<<3) + (FTS5_MAIN_PREFIX + iIdx);
212225 for(i=0; i<nTerm; i++) ret += (ret<<3) + pTerm[i];
212226 return ret;
212227}
212228
212229#ifdef SQLITE_DEBUG
212230/*
212231** This function is purely an internal test. It does not contribute to
212232** FTS functionality, or even the integrity-check, in any way.
212233**
212234** Instead, it tests that the same set of pgno/rowid combinations are
212235** visited regardless of whether the doclist-index identified by parameters
212236** iSegid/iLeaf is iterated in forwards or reverse order.
212237*/
212238static void fts5TestDlidxReverse(
212239 Fts5Index *p,
212240 int iSegid, /* Segment id to load from */
212241 int iLeaf /* Load doclist-index for this leaf */
212242){
212243 Fts5DlidxIter *pDlidx = 0;
212244 u64 cksum1 = 13;
212245 u64 cksum2 = 13;
212246
212247 for(pDlidx=fts5DlidxIterInit(p, 0, iSegid, iLeaf);
212248 fts5DlidxIterEof(p, pDlidx)==0;
212249 fts5DlidxIterNext(p, pDlidx)
212250 ){
212251 i64 iRowid = fts5DlidxIterRowid(pDlidx);
212252 int pgno = fts5DlidxIterPgno(pDlidx);
212253 assert( pgno>iLeaf );
212254 cksum1 += iRowid + ((i64)pgno<<32);
212255 }
212256 fts5DlidxIterFree(pDlidx);
212257 pDlidx = 0;
212258
212259 for(pDlidx=fts5DlidxIterInit(p, 1, iSegid, iLeaf);
212260 fts5DlidxIterEof(p, pDlidx)==0;
212261 fts5DlidxIterPrev(p, pDlidx)
212262 ){
212263 i64 iRowid = fts5DlidxIterRowid(pDlidx);
212264 int pgno = fts5DlidxIterPgno(pDlidx);
212265 assert( fts5DlidxIterPgno(pDlidx)>iLeaf );
212266 cksum2 += iRowid + ((i64)pgno<<32);
212267 }
212268 fts5DlidxIterFree(pDlidx);
212269 pDlidx = 0;
212270
212271 if( p->rc==SQLITE_OK && cksum1!=cksum2 ) p->rc = FTS5_CORRUPT;
212272}
212273
212274static int fts5QueryCksum(
212275 Fts5Index *p, /* Fts5 index object */
212276 int iIdx,
212277 const char *z, /* Index key to query for */
212278 int n, /* Size of index key in bytes */
212279 int flags, /* Flags for Fts5IndexQuery */
212280 u64 *pCksum /* IN/OUT: Checksum value */
212281){
212282 int eDetail = p->pConfig->eDetail;
212283 u64 cksum = *pCksum;
212284 Fts5IndexIter *pIter = 0;
212285 int rc = sqlite3Fts5IndexQuery(p, z, n, flags, 0, &pIter);
212286
212287 while( rc==SQLITE_OK && 0==sqlite3Fts5IterEof(pIter) ){
212288 i64 rowid = pIter->iRowid;
212289
212290 if( eDetail==FTS5_DETAIL_NONE ){
212291 cksum ^= sqlite3Fts5IndexEntryCksum(rowid, 0, 0, iIdx, z, n);
212292 }else{
212293 Fts5PoslistReader sReader;
212294 for(sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &sReader);
212295 sReader.bEof==0;
212296 sqlite3Fts5PoslistReaderNext(&sReader)
212297 ){
212298 int iCol = FTS5_POS2COLUMN(sReader.iPos);
212299 int iOff = FTS5_POS2OFFSET(sReader.iPos);
212300 cksum ^= sqlite3Fts5IndexEntryCksum(rowid, iCol, iOff, iIdx, z, n);
212301 }
212302 }
212303 if( rc==SQLITE_OK ){
212304 rc = sqlite3Fts5IterNext(pIter);
212305 }
212306 }
212307 sqlite3Fts5IterClose(pIter);
212308
212309 *pCksum = cksum;
212310 return rc;
212311}
212312
212313
212314/*
212315** This function is also purely an internal test. It does not contribute to
212316** FTS functionality, or even the integrity-check, in any way.
212317*/
212318static void fts5TestTerm(
212319 Fts5Index *p,
212320 Fts5Buffer *pPrev, /* Previous term */
212321 const char *z, int n, /* Possibly new term to test */
212322 u64 expected,
212323 u64 *pCksum
212324){
212325 int rc = p->rc;
212326 if( pPrev->n==0 ){
212327 fts5BufferSet(&rc, pPrev, n, (const u8*)z);
212328 }else
212329 if( rc==SQLITE_OK && (pPrev->n!=n || memcmp(pPrev->p, z, n)) ){
212330 u64 cksum3 = *pCksum;
212331 const char *zTerm = (const char*)&pPrev->p[1]; /* term sans prefix-byte */
212332 int nTerm = pPrev->n-1; /* Size of zTerm in bytes */
212333 int iIdx = (pPrev->p[0] - FTS5_MAIN_PREFIX);
212334 int flags = (iIdx==0 ? 0 : FTS5INDEX_QUERY_PREFIX);
212335 u64 ck1 = 0;
212336 u64 ck2 = 0;
212337
212338 /* Check that the results returned for ASC and DESC queries are
212339 ** the same. If not, call this corruption. */
212340 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, flags, &ck1);
212341 if( rc==SQLITE_OK ){
212342 int f = flags|FTS5INDEX_QUERY_DESC;
212343 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
212344 }
212345 if( rc==SQLITE_OK && ck1!=ck2 ) rc = FTS5_CORRUPT;
212346
212347 /* If this is a prefix query, check that the results returned if the
212348 ** the index is disabled are the same. In both ASC and DESC order.
212349 **
212350 ** This check may only be performed if the hash table is empty. This
212351 ** is because the hash table only supports a single scan query at
212352 ** a time, and the multi-iter loop from which this function is called
212353 ** is already performing such a scan. */
212354 if( p->nPendingData==0 ){
212355 if( iIdx>0 && rc==SQLITE_OK ){
212356 int f = flags|FTS5INDEX_QUERY_TEST_NOIDX;
212357 ck2 = 0;
212358 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
212359 if( rc==SQLITE_OK && ck1!=ck2 ) rc = FTS5_CORRUPT;
212360 }
212361 if( iIdx>0 && rc==SQLITE_OK ){
212362 int f = flags|FTS5INDEX_QUERY_TEST_NOIDX|FTS5INDEX_QUERY_DESC;
212363 ck2 = 0;
212364 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
212365 if( rc==SQLITE_OK && ck1!=ck2 ) rc = FTS5_CORRUPT;
212366 }
212367 }
212368
212369 cksum3 ^= ck1;
212370 fts5BufferSet(&rc, pPrev, n, (const u8*)z);
212371
212372 if( rc==SQLITE_OK && cksum3!=expected ){
212373 rc = FTS5_CORRUPT;
212374 }
212375 *pCksum = cksum3;
212376 }
212377 p->rc = rc;
212378}
212379
212380#else
212381# define fts5TestDlidxReverse(x,y,z)
212382# define fts5TestTerm(u,v,w,x,y,z)
212383#endif
212384
212385/*
212386** Check that:
212387**
212388** 1) All leaves of pSeg between iFirst and iLast (inclusive) exist and
212389** contain zero terms.
212390** 2) All leaves of pSeg between iNoRowid and iLast (inclusive) exist and
212391** contain zero rowids.
212392*/
212393static void fts5IndexIntegrityCheckEmpty(
212394 Fts5Index *p,
212395 Fts5StructureSegment *pSeg, /* Segment to check internal consistency */
212396 int iFirst,
212397 int iNoRowid,
212398 int iLast
212399){
212400 int i;
212401
212402 /* Now check that the iter.nEmpty leaves following the current leaf
212403 ** (a) exist and (b) contain no terms. */
212404 for(i=iFirst; p->rc==SQLITE_OK && i<=iLast; i++){
212405 Fts5Data *pLeaf = fts5DataRead(p, FTS5_SEGMENT_ROWID(pSeg->iSegid, i));
212406 if( pLeaf ){
212407 if( !fts5LeafIsTermless(pLeaf) ) p->rc = FTS5_CORRUPT;
212408 if( i>=iNoRowid && 0!=fts5LeafFirstRowidOff(pLeaf) ) p->rc = FTS5_CORRUPT;
212409 }
212410 fts5DataRelease(pLeaf);
212411 }
212412}
212413
212414static void fts5IntegrityCheckPgidx(Fts5Index *p, Fts5Data *pLeaf){
212415 int iTermOff = 0;
212416 int ii;
212417
212418 Fts5Buffer buf1 = {0,0,0};
212419 Fts5Buffer buf2 = {0,0,0};
212420
212421 ii = pLeaf->szLeaf;
212422 while( ii<pLeaf->nn && p->rc==SQLITE_OK ){
212423 int res;
212424 int iOff;
212425 int nIncr;
212426
212427 ii += fts5GetVarint32(&pLeaf->p[ii], nIncr);
212428 iTermOff += nIncr;
212429 iOff = iTermOff;
212430
212431 if( iOff>=pLeaf->szLeaf ){
212432 p->rc = FTS5_CORRUPT;
212433 }else if( iTermOff==nIncr ){
212434 int nByte;
212435 iOff += fts5GetVarint32(&pLeaf->p[iOff], nByte);
212436 if( (iOff+nByte)>pLeaf->szLeaf ){
212437 p->rc = FTS5_CORRUPT;
212438 }else{
212439 fts5BufferSet(&p->rc, &buf1, nByte, &pLeaf->p[iOff]);
212440 }
212441 }else{
212442 int nKeep, nByte;
212443 iOff += fts5GetVarint32(&pLeaf->p[iOff], nKeep);
212444 iOff += fts5GetVarint32(&pLeaf->p[iOff], nByte);
212445 if( nKeep>buf1.n || (iOff+nByte)>pLeaf->szLeaf ){
212446 p->rc = FTS5_CORRUPT;
212447 }else{
212448 buf1.n = nKeep;
212449 fts5BufferAppendBlob(&p->rc, &buf1, nByte, &pLeaf->p[iOff]);
212450 }
212451
212452 if( p->rc==SQLITE_OK ){
212453 res = fts5BufferCompare(&buf1, &buf2);
212454 if( res<=0 ) p->rc = FTS5_CORRUPT;
212455 }
212456 }
212457 fts5BufferSet(&p->rc, &buf2, buf1.n, buf1.p);
212458 }
212459
212460 fts5BufferFree(&buf1);
212461 fts5BufferFree(&buf2);
212462}
212463
212464static void fts5IndexIntegrityCheckSegment(
212465 Fts5Index *p, /* FTS5 backend object */
212466 Fts5StructureSegment *pSeg /* Segment to check internal consistency */
212467){
212468 Fts5Config *pConfig = p->pConfig;
212469 sqlite3_stmt *pStmt = 0;
212470 int rc2;
212471 int iIdxPrevLeaf = pSeg->pgnoFirst-1;
212472 int iDlidxPrevLeaf = pSeg->pgnoLast;
212473
212474 if( pSeg->pgnoFirst==0 ) return;
212475
212476 fts5IndexPrepareStmt(p, &pStmt, sqlite3_mprintf(
212477 "SELECT segid, term, (pgno>>1), (pgno&1) FROM %Q.'%q_idx' WHERE segid=%d",
212478 pConfig->zDb, pConfig->zName, pSeg->iSegid
212479 ));
212480
212481 /* Iterate through the b-tree hierarchy. */
212482 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
212483 i64 iRow; /* Rowid for this leaf */
212484 Fts5Data *pLeaf; /* Data for this leaf */
212485
212486 int nIdxTerm = sqlite3_column_bytes(pStmt, 1);
212487 const char *zIdxTerm = (const char*)sqlite3_column_text(pStmt, 1);
212488 int iIdxLeaf = sqlite3_column_int(pStmt, 2);
212489 int bIdxDlidx = sqlite3_column_int(pStmt, 3);
212490
212491 /* If the leaf in question has already been trimmed from the segment,
212492 ** ignore this b-tree entry. Otherwise, load it into memory. */
212493 if( iIdxLeaf<pSeg->pgnoFirst ) continue;
212494 iRow = FTS5_SEGMENT_ROWID(pSeg->iSegid, iIdxLeaf);
212495 pLeaf = fts5LeafRead(p, iRow);
212496 if( pLeaf==0 ) break;
212497
212498 /* Check that the leaf contains at least one term, and that it is equal
212499 ** to or larger than the split-key in zIdxTerm. Also check that if there
212500 ** is also a rowid pointer within the leaf page header, it points to a
212501 ** location before the term. */
212502 if( pLeaf->nn<=pLeaf->szLeaf ){
212503 p->rc = FTS5_CORRUPT;
212504 }else{
212505 int iOff; /* Offset of first term on leaf */
212506 int iRowidOff; /* Offset of first rowid on leaf */
212507 int nTerm; /* Size of term on leaf in bytes */
212508 int res; /* Comparison of term and split-key */
212509
212510 iOff = fts5LeafFirstTermOff(pLeaf);
212511 iRowidOff = fts5LeafFirstRowidOff(pLeaf);
212512 if( iRowidOff>=iOff ){
212513 p->rc = FTS5_CORRUPT;
212514 }else{
212515 iOff += fts5GetVarint32(&pLeaf->p[iOff], nTerm);
212516 res = memcmp(&pLeaf->p[iOff], zIdxTerm, MIN(nTerm, nIdxTerm));
212517 if( res==0 ) res = nTerm - nIdxTerm;
212518 if( res<0 ) p->rc = FTS5_CORRUPT;
212519 }
212520
212521 fts5IntegrityCheckPgidx(p, pLeaf);
212522 }
212523 fts5DataRelease(pLeaf);
212524 if( p->rc ) break;
212525
212526 /* Now check that the iter.nEmpty leaves following the current leaf
212527 ** (a) exist and (b) contain no terms. */
212528 fts5IndexIntegrityCheckEmpty(
212529 p, pSeg, iIdxPrevLeaf+1, iDlidxPrevLeaf+1, iIdxLeaf-1
212530 );
212531 if( p->rc ) break;
212532
212533 /* If there is a doclist-index, check that it looks right. */
212534 if( bIdxDlidx ){
212535 Fts5DlidxIter *pDlidx = 0; /* For iterating through doclist index */
212536 int iPrevLeaf = iIdxLeaf;
212537 int iSegid = pSeg->iSegid;
212538 int iPg = 0;
212539 i64 iKey;
212540
212541 for(pDlidx=fts5DlidxIterInit(p, 0, iSegid, iIdxLeaf);
212542 fts5DlidxIterEof(p, pDlidx)==0;
212543 fts5DlidxIterNext(p, pDlidx)
212544 ){
212545
212546 /* Check any rowid-less pages that occur before the current leaf. */
212547 for(iPg=iPrevLeaf+1; iPg<fts5DlidxIterPgno(pDlidx); iPg++){
212548 iKey = FTS5_SEGMENT_ROWID(iSegid, iPg);
212549 pLeaf = fts5DataRead(p, iKey);
212550 if( pLeaf ){
212551 if( fts5LeafFirstRowidOff(pLeaf)!=0 ) p->rc = FTS5_CORRUPT;
212552 fts5DataRelease(pLeaf);
212553 }
212554 }
212555 iPrevLeaf = fts5DlidxIterPgno(pDlidx);
212556
212557 /* Check that the leaf page indicated by the iterator really does
212558 ** contain the rowid suggested by the same. */
212559 iKey = FTS5_SEGMENT_ROWID(iSegid, iPrevLeaf);
212560 pLeaf = fts5DataRead(p, iKey);
212561 if( pLeaf ){
212562 i64 iRowid;
212563 int iRowidOff = fts5LeafFirstRowidOff(pLeaf);
212564 ASSERT_SZLEAF_OK(pLeaf);
212565 if( iRowidOff>=pLeaf->szLeaf ){
212566 p->rc = FTS5_CORRUPT;
212567 }else{
212568 fts5GetVarint(&pLeaf->p[iRowidOff], (u64*)&iRowid);
212569 if( iRowid!=fts5DlidxIterRowid(pDlidx) ) p->rc = FTS5_CORRUPT;
212570 }
212571 fts5DataRelease(pLeaf);
212572 }
212573 }
212574
212575 iDlidxPrevLeaf = iPg;
212576 fts5DlidxIterFree(pDlidx);
212577 fts5TestDlidxReverse(p, iSegid, iIdxLeaf);
212578 }else{
212579 iDlidxPrevLeaf = pSeg->pgnoLast;
212580 /* TODO: Check there is no doclist index */
212581 }
212582
212583 iIdxPrevLeaf = iIdxLeaf;
212584 }
212585
212586 rc2 = sqlite3_finalize(pStmt);
212587 if( p->rc==SQLITE_OK ) p->rc = rc2;
212588
212589 /* Page iter.iLeaf must now be the rightmost leaf-page in the segment */
212590#if 0
212591 if( p->rc==SQLITE_OK && iter.iLeaf!=pSeg->pgnoLast ){
212592 p->rc = FTS5_CORRUPT;
212593 }
212594#endif
212595}
212596
212597
212598/*
212599** Run internal checks to ensure that the FTS index (a) is internally
212600** consistent and (b) contains entries for which the XOR of the checksums
212601** as calculated by sqlite3Fts5IndexEntryCksum() is cksum.
212602**
212603** Return SQLITE_CORRUPT if any of the internal checks fail, or if the
212604** checksum does not match. Return SQLITE_OK if all checks pass without
212605** error, or some other SQLite error code if another error (e.g. OOM)
212606** occurs.
212607*/
212608static int sqlite3Fts5IndexIntegrityCheck(Fts5Index *p, u64 cksum){
212609 int eDetail = p->pConfig->eDetail;
212610 u64 cksum2 = 0; /* Checksum based on contents of indexes */
212611 Fts5Buffer poslist = {0,0,0}; /* Buffer used to hold a poslist */
212612 Fts5Iter *pIter; /* Used to iterate through entire index */
212613 Fts5Structure *pStruct; /* Index structure */
212614
212615#ifdef SQLITE_DEBUG
212616 /* Used by extra internal tests only run if NDEBUG is not defined */
212617 u64 cksum3 = 0; /* Checksum based on contents of indexes */
212618 Fts5Buffer term = {0,0,0}; /* Buffer used to hold most recent term */
212619#endif
212620 const int flags = FTS5INDEX_QUERY_NOOUTPUT;
212621
212622 /* Load the FTS index structure */
212623 pStruct = fts5StructureRead(p);
212624
212625 /* Check that the internal nodes of each segment match the leaves */
212626 if( pStruct ){
212627 int iLvl, iSeg;
212628 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
212629 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
212630 Fts5StructureSegment *pSeg = &pStruct->aLevel[iLvl].aSeg[iSeg];
212631 fts5IndexIntegrityCheckSegment(p, pSeg);
212632 }
212633 }
212634 }
212635
212636 /* The cksum argument passed to this function is a checksum calculated
212637 ** based on all expected entries in the FTS index (including prefix index
212638 ** entries). This block checks that a checksum calculated based on the
212639 ** actual contents of FTS index is identical.
212640 **
212641 ** Two versions of the same checksum are calculated. The first (stack
212642 ** variable cksum2) based on entries extracted from the full-text index
212643 ** while doing a linear scan of each individual index in turn.
212644 **
212645 ** As each term visited by the linear scans, a separate query for the
212646 ** same term is performed. cksum3 is calculated based on the entries
212647 ** extracted by these queries.
212648 */
212649 for(fts5MultiIterNew(p, pStruct, flags, 0, 0, 0, -1, 0, &pIter);
212650 fts5MultiIterEof(p, pIter)==0;
212651 fts5MultiIterNext(p, pIter, 0, 0)
212652 ){
212653 int n; /* Size of term in bytes */
212654 i64 iPos = 0; /* Position read from poslist */
212655 int iOff = 0; /* Offset within poslist */
212656 i64 iRowid = fts5MultiIterRowid(pIter);
212657 char *z = (char*)fts5MultiIterTerm(pIter, &n);
212658
212659 /* If this is a new term, query for it. Update cksum3 with the results. */
212660 fts5TestTerm(p, &term, z, n, cksum2, &cksum3);
212661
212662 if( eDetail==FTS5_DETAIL_NONE ){
212663 if( 0==fts5MultiIterIsEmpty(p, pIter) ){
212664 cksum2 ^= sqlite3Fts5IndexEntryCksum(iRowid, 0, 0, -1, z, n);
212665 }
212666 }else{
212667 poslist.n = 0;
212668 fts5SegiterPoslist(p, &pIter->aSeg[pIter->aFirst[1].iFirst], 0, &poslist);
212669 while( 0==sqlite3Fts5PoslistNext64(poslist.p, poslist.n, &iOff, &iPos) ){
212670 int iCol = FTS5_POS2COLUMN(iPos);
212671 int iTokOff = FTS5_POS2OFFSET(iPos);
212672 cksum2 ^= sqlite3Fts5IndexEntryCksum(iRowid, iCol, iTokOff, -1, z, n);
212673 }
212674 }
212675 }
212676 fts5TestTerm(p, &term, 0, 0, cksum2, &cksum3);
212677
212678 fts5MultiIterFree(pIter);
212679 if( p->rc==SQLITE_OK && cksum!=cksum2 ) p->rc = FTS5_CORRUPT;
212680
212681 fts5StructureRelease(pStruct);
212682#ifdef SQLITE_DEBUG
212683 fts5BufferFree(&term);
212684#endif
212685 fts5BufferFree(&poslist);
212686 return fts5IndexReturn(p);
212687}
212688
212689/*************************************************************************
212690**************************************************************************
212691** Below this point is the implementation of the fts5_decode() scalar
212692** function only.
212693*/
212694
212695/*
212696** Decode a segment-data rowid from the %_data table. This function is
212697** the opposite of macro FTS5_SEGMENT_ROWID().
212698*/
212699static void fts5DecodeRowid(
212700 i64 iRowid, /* Rowid from %_data table */
212701 int *piSegid, /* OUT: Segment id */
212702 int *pbDlidx, /* OUT: Dlidx flag */
212703 int *piHeight, /* OUT: Height */
212704 int *piPgno /* OUT: Page number */
212705){
212706 *piPgno = (int)(iRowid & (((i64)1 << FTS5_DATA_PAGE_B) - 1));
212707 iRowid >>= FTS5_DATA_PAGE_B;
212708
212709 *piHeight = (int)(iRowid & (((i64)1 << FTS5_DATA_HEIGHT_B) - 1));
212710 iRowid >>= FTS5_DATA_HEIGHT_B;
212711
212712 *pbDlidx = (int)(iRowid & 0x0001);
212713 iRowid >>= FTS5_DATA_DLI_B;
212714
212715 *piSegid = (int)(iRowid & (((i64)1 << FTS5_DATA_ID_B) - 1));
212716}
212717
212718static void fts5DebugRowid(int *pRc, Fts5Buffer *pBuf, i64 iKey){
212719 int iSegid, iHeight, iPgno, bDlidx; /* Rowid compenents */
212720 fts5DecodeRowid(iKey, &iSegid, &bDlidx, &iHeight, &iPgno);
212721
212722 if( iSegid==0 ){
212723 if( iKey==FTS5_AVERAGES_ROWID ){
212724 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{averages} ");
212725 }else{
212726 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{structure}");
212727 }
212728 }
212729 else{
212730 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{%ssegid=%d h=%d pgno=%d}",
212731 bDlidx ? "dlidx " : "", iSegid, iHeight, iPgno
212732 );
212733 }
212734}
212735
212736static void fts5DebugStructure(
212737 int *pRc, /* IN/OUT: error code */
212738 Fts5Buffer *pBuf,
212739 Fts5Structure *p
212740){
212741 int iLvl, iSeg; /* Iterate through levels, segments */
212742
212743 for(iLvl=0; iLvl<p->nLevel; iLvl++){
212744 Fts5StructureLevel *pLvl = &p->aLevel[iLvl];
212745 sqlite3Fts5BufferAppendPrintf(pRc, pBuf,
212746 " {lvl=%d nMerge=%d nSeg=%d", iLvl, pLvl->nMerge, pLvl->nSeg
212747 );
212748 for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
212749 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
212750 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " {id=%d leaves=%d..%d}",
212751 pSeg->iSegid, pSeg->pgnoFirst, pSeg->pgnoLast
212752 );
212753 }
212754 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "}");
212755 }
212756}
212757
212758/*
212759** This is part of the fts5_decode() debugging aid.
212760**
212761** Arguments pBlob/nBlob contain a serialized Fts5Structure object. This
212762** function appends a human-readable representation of the same object
212763** to the buffer passed as the second argument.
212764*/
212765static void fts5DecodeStructure(
212766 int *pRc, /* IN/OUT: error code */
212767 Fts5Buffer *pBuf,
212768 const u8 *pBlob, int nBlob
212769){
212770 int rc; /* Return code */
212771 Fts5Structure *p = 0; /* Decoded structure object */
212772
212773 rc = fts5StructureDecode(pBlob, nBlob, 0, &p);
212774 if( rc!=SQLITE_OK ){
212775 *pRc = rc;
212776 return;
212777 }
212778
212779 fts5DebugStructure(pRc, pBuf, p);
212780 fts5StructureRelease(p);
212781}
212782
212783/*
212784** This is part of the fts5_decode() debugging aid.
212785**
212786** Arguments pBlob/nBlob contain an "averages" record. This function
212787** appends a human-readable representation of record to the buffer passed
212788** as the second argument.
212789*/
212790static void fts5DecodeAverages(
212791 int *pRc, /* IN/OUT: error code */
212792 Fts5Buffer *pBuf,
212793 const u8 *pBlob, int nBlob
212794){
212795 int i = 0;
212796 const char *zSpace = "";
212797
212798 while( i<nBlob ){
212799 u64 iVal;
212800 i += sqlite3Fts5GetVarint(&pBlob[i], &iVal);
212801 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "%s%d", zSpace, (int)iVal);
212802 zSpace = " ";
212803 }
212804}
212805
212806/*
212807** Buffer (a/n) is assumed to contain a list of serialized varints. Read
212808** each varint and append its string representation to buffer pBuf. Return
212809** after either the input buffer is exhausted or a 0 value is read.
212810**
212811** The return value is the number of bytes read from the input buffer.
212812*/
212813static int fts5DecodePoslist(int *pRc, Fts5Buffer *pBuf, const u8 *a, int n){
212814 int iOff = 0;
212815 while( iOff<n ){
212816 int iVal;
212817 iOff += fts5GetVarint32(&a[iOff], iVal);
212818 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " %d", iVal);
212819 }
212820 return iOff;
212821}
212822
212823/*
212824** The start of buffer (a/n) contains the start of a doclist. The doclist
212825** may or may not finish within the buffer. This function appends a text
212826** representation of the part of the doclist that is present to buffer
212827** pBuf.
212828**
212829** The return value is the number of bytes read from the input buffer.
212830*/
212831static int fts5DecodeDoclist(int *pRc, Fts5Buffer *pBuf, const u8 *a, int n){
212832 i64 iDocid = 0;
212833 int iOff = 0;
212834
212835 if( n>0 ){
212836 iOff = sqlite3Fts5GetVarint(a, (u64*)&iDocid);
212837 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " id=%lld", iDocid);
212838 }
212839 while( iOff<n ){
212840 int nPos;
212841 int bDel;
212842 iOff += fts5GetPoslistSize(&a[iOff], &nPos, &bDel);
212843 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " nPos=%d%s", nPos, bDel?"*":"");
212844 iOff += fts5DecodePoslist(pRc, pBuf, &a[iOff], MIN(n-iOff, nPos));
212845 if( iOff<n ){
212846 i64 iDelta;
212847 iOff += sqlite3Fts5GetVarint(&a[iOff], (u64*)&iDelta);
212848 iDocid += iDelta;
212849 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " id=%lld", iDocid);
212850 }
212851 }
212852
212853 return iOff;
212854}
212855
212856/*
212857** This function is part of the fts5_decode() debugging function. It is
212858** only ever used with detail=none tables.
212859**
212860** Buffer (pData/nData) contains a doclist in the format used by detail=none
212861** tables. This function appends a human-readable version of that list to
212862** buffer pBuf.
212863**
212864** If *pRc is other than SQLITE_OK when this function is called, it is a
212865** no-op. If an OOM or other error occurs within this function, *pRc is
212866** set to an SQLite error code before returning. The final state of buffer
212867** pBuf is undefined in this case.
212868*/
212869static void fts5DecodeRowidList(
212870 int *pRc, /* IN/OUT: Error code */
212871 Fts5Buffer *pBuf, /* Buffer to append text to */
212872 const u8 *pData, int nData /* Data to decode list-of-rowids from */
212873){
212874 int i = 0;
212875 i64 iRowid = 0;
212876
212877 while( i<nData ){
212878 const char *zApp = "";
212879 u64 iVal;
212880 i += sqlite3Fts5GetVarint(&pData[i], &iVal);
212881 iRowid += iVal;
212882
212883 if( i<nData && pData[i]==0x00 ){
212884 i++;
212885 if( i<nData && pData[i]==0x00 ){
212886 i++;
212887 zApp = "+";
212888 }else{
212889 zApp = "*";
212890 }
212891 }
212892
212893 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " %lld%s", iRowid, zApp);
212894 }
212895}
212896
212897/*
212898** The implementation of user-defined scalar function fts5_decode().
212899*/
212900static void fts5DecodeFunction(
212901 sqlite3_context *pCtx, /* Function call context */
212902 int nArg, /* Number of args (always 2) */
212903 sqlite3_value **apVal /* Function arguments */
212904){
212905 i64 iRowid; /* Rowid for record being decoded */
212906 int iSegid,iHeight,iPgno,bDlidx;/* Rowid components */
212907 const u8 *aBlob; int n; /* Record to decode */
212908 u8 *a = 0;
212909 Fts5Buffer s; /* Build up text to return here */
212910 int rc = SQLITE_OK; /* Return code */
212911 int nSpace = 0;
212912 int eDetailNone = (sqlite3_user_data(pCtx)!=0);
212913
212914 assert( nArg==2 );
212915 UNUSED_PARAM(nArg);
212916 memset(&s, 0, sizeof(Fts5Buffer));
212917 iRowid = sqlite3_value_int64(apVal[0]);
212918
212919 /* Make a copy of the second argument (a blob) in aBlob[]. The aBlob[]
212920 ** copy is followed by FTS5_DATA_ZERO_PADDING 0x00 bytes, which prevents
212921 ** buffer overreads even if the record is corrupt. */
212922 n = sqlite3_value_bytes(apVal[1]);
212923 aBlob = sqlite3_value_blob(apVal[1]);
212924 nSpace = n + FTS5_DATA_ZERO_PADDING;
212925 a = (u8*)sqlite3Fts5MallocZero(&rc, nSpace);
212926 if( a==0 ) goto decode_out;
212927 memcpy(a, aBlob, n);
212928
212929
212930 fts5DecodeRowid(iRowid, &iSegid, &bDlidx, &iHeight, &iPgno);
212931
212932 fts5DebugRowid(&rc, &s, iRowid);
212933 if( bDlidx ){
212934 Fts5Data dlidx;
212935 Fts5DlidxLvl lvl;
212936
212937 dlidx.p = a;
212938 dlidx.nn = n;
212939
212940 memset(&lvl, 0, sizeof(Fts5DlidxLvl));
212941 lvl.pData = &dlidx;
212942 lvl.iLeafPgno = iPgno;
212943
212944 for(fts5DlidxLvlNext(&lvl); lvl.bEof==0; fts5DlidxLvlNext(&lvl)){
212945 sqlite3Fts5BufferAppendPrintf(&rc, &s,
212946 " %d(%lld)", lvl.iLeafPgno, lvl.iRowid
212947 );
212948 }
212949 }else if( iSegid==0 ){
212950 if( iRowid==FTS5_AVERAGES_ROWID ){
212951 fts5DecodeAverages(&rc, &s, a, n);
212952 }else{
212953 fts5DecodeStructure(&rc, &s, a, n);
212954 }
212955 }else if( eDetailNone ){
212956 Fts5Buffer term; /* Current term read from page */
212957 int szLeaf;
212958 int iPgidxOff = szLeaf = fts5GetU16(&a[2]);
212959 int iTermOff;
212960 int nKeep = 0;
212961 int iOff;
212962
212963 memset(&term, 0, sizeof(Fts5Buffer));
212964
212965 /* Decode any entries that occur before the first term. */
212966 if( szLeaf<n ){
212967 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], iTermOff);
212968 }else{
212969 iTermOff = szLeaf;
212970 }
212971 fts5DecodeRowidList(&rc, &s, &a[4], iTermOff-4);
212972
212973 iOff = iTermOff;
212974 while( iOff<szLeaf ){
212975 int nAppend;
212976
212977 /* Read the term data for the next term*/
212978 iOff += fts5GetVarint32(&a[iOff], nAppend);
212979 term.n = nKeep;
212980 fts5BufferAppendBlob(&rc, &term, nAppend, &a[iOff]);
212981 sqlite3Fts5BufferAppendPrintf(
212982 &rc, &s, " term=%.*s", term.n, (const char*)term.p
212983 );
212984 iOff += nAppend;
212985
212986 /* Figure out where the doclist for this term ends */
212987 if( iPgidxOff<n ){
212988 int nIncr;
212989 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], nIncr);
212990 iTermOff += nIncr;
212991 }else{
212992 iTermOff = szLeaf;
212993 }
212994
212995 fts5DecodeRowidList(&rc, &s, &a[iOff], iTermOff-iOff);
212996 iOff = iTermOff;
212997 if( iOff<szLeaf ){
212998 iOff += fts5GetVarint32(&a[iOff], nKeep);
212999 }
213000 }
213001
213002 fts5BufferFree(&term);
213003 }else{
213004 Fts5Buffer term; /* Current term read from page */
213005 int szLeaf; /* Offset of pgidx in a[] */
213006 int iPgidxOff;
213007 int iPgidxPrev = 0; /* Previous value read from pgidx */
213008 int iTermOff = 0;
213009 int iRowidOff = 0;
213010 int iOff;
213011 int nDoclist;
213012
213013 memset(&term, 0, sizeof(Fts5Buffer));
213014
213015 if( n<4 ){
213016 sqlite3Fts5BufferSet(&rc, &s, 7, (const u8*)"corrupt");
213017 goto decode_out;
213018 }else{
213019 iRowidOff = fts5GetU16(&a[0]);
213020 iPgidxOff = szLeaf = fts5GetU16(&a[2]);
213021 if( iPgidxOff<n ){
213022 fts5GetVarint32(&a[iPgidxOff], iTermOff);
213023 }
213024 }
213025
213026 /* Decode the position list tail at the start of the page */
213027 if( iRowidOff!=0 ){
213028 iOff = iRowidOff;
213029 }else if( iTermOff!=0 ){
213030 iOff = iTermOff;
213031 }else{
213032 iOff = szLeaf;
213033 }
213034 fts5DecodePoslist(&rc, &s, &a[4], iOff-4);
213035
213036 /* Decode any more doclist data that appears on the page before the
213037 ** first term. */
213038 nDoclist = (iTermOff ? iTermOff : szLeaf) - iOff;
213039 fts5DecodeDoclist(&rc, &s, &a[iOff], nDoclist);
213040
213041 while( iPgidxOff<n ){
213042 int bFirst = (iPgidxOff==szLeaf); /* True for first term on page */
213043 int nByte; /* Bytes of data */
213044 int iEnd;
213045
213046 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], nByte);
213047 iPgidxPrev += nByte;
213048 iOff = iPgidxPrev;
213049
213050 if( iPgidxOff<n ){
213051 fts5GetVarint32(&a[iPgidxOff], nByte);
213052 iEnd = iPgidxPrev + nByte;
213053 }else{
213054 iEnd = szLeaf;
213055 }
213056
213057 if( bFirst==0 ){
213058 iOff += fts5GetVarint32(&a[iOff], nByte);
213059 term.n = nByte;
213060 }
213061 iOff += fts5GetVarint32(&a[iOff], nByte);
213062 fts5BufferAppendBlob(&rc, &term, nByte, &a[iOff]);
213063 iOff += nByte;
213064
213065 sqlite3Fts5BufferAppendPrintf(
213066 &rc, &s, " term=%.*s", term.n, (const char*)term.p
213067 );
213068 iOff += fts5DecodeDoclist(&rc, &s, &a[iOff], iEnd-iOff);
213069 }
213070
213071 fts5BufferFree(&term);
213072 }
213073
213074 decode_out:
213075 sqlite3_free(a);
213076 if( rc==SQLITE_OK ){
213077 sqlite3_result_text(pCtx, (const char*)s.p, s.n, SQLITE_TRANSIENT);
213078 }else{
213079 sqlite3_result_error_code(pCtx, rc);
213080 }
213081 fts5BufferFree(&s);
213082}
213083
213084/*
213085** The implementation of user-defined scalar function fts5_rowid().
213086*/
213087static void fts5RowidFunction(
213088 sqlite3_context *pCtx, /* Function call context */
213089 int nArg, /* Number of args (always 2) */
213090 sqlite3_value **apVal /* Function arguments */
213091){
213092 const char *zArg;
213093 if( nArg==0 ){
213094 sqlite3_result_error(pCtx, "should be: fts5_rowid(subject, ....)", -1);
213095 }else{
213096 zArg = (const char*)sqlite3_value_text(apVal[0]);
213097 if( 0==sqlite3_stricmp(zArg, "segment") ){
213098 i64 iRowid;
213099 int segid, pgno;
213100 if( nArg!=3 ){
213101 sqlite3_result_error(pCtx,
213102 "should be: fts5_rowid('segment', segid, pgno))", -1
213103 );
213104 }else{
213105 segid = sqlite3_value_int(apVal[1]);
213106 pgno = sqlite3_value_int(apVal[2]);
213107 iRowid = FTS5_SEGMENT_ROWID(segid, pgno);
213108 sqlite3_result_int64(pCtx, iRowid);
213109 }
213110 }else{
213111 sqlite3_result_error(pCtx,
213112 "first arg to fts5_rowid() must be 'segment'" , -1
213113 );
213114 }
213115 }
213116}
213117
213118/*
213119** This is called as part of registering the FTS5 module with database
213120** connection db. It registers several user-defined scalar functions useful
213121** with FTS5.
213122**
213123** If successful, SQLITE_OK is returned. If an error occurs, some other
213124** SQLite error code is returned instead.
213125*/
213126static int sqlite3Fts5IndexInit(sqlite3 *db){
213127 int rc = sqlite3_create_function(
213128 db, "fts5_decode", 2, SQLITE_UTF8, 0, fts5DecodeFunction, 0, 0
213129 );
213130
213131 if( rc==SQLITE_OK ){
213132 rc = sqlite3_create_function(
213133 db, "fts5_decode_none", 2,
213134 SQLITE_UTF8, (void*)db, fts5DecodeFunction, 0, 0
213135 );
213136 }
213137
213138 if( rc==SQLITE_OK ){
213139 rc = sqlite3_create_function(
213140 db, "fts5_rowid", -1, SQLITE_UTF8, 0, fts5RowidFunction, 0, 0
213141 );
213142 }
213143 return rc;
213144}
213145
213146
213147static int sqlite3Fts5IndexReset(Fts5Index *p){
213148 assert( p->pStruct==0 || p->iStructVersion!=0 );
213149 if( fts5IndexDataVersion(p)!=p->iStructVersion ){
213150 fts5StructureInvalidate(p);
213151 }
213152 return fts5IndexReturn(p);
213153}
213154
213155/*
213156** 2014 Jun 09
213157**
213158** The author disclaims copyright to this source code. In place of
213159** a legal notice, here is a blessing:
213160**
213161** May you do good and not evil.
213162** May you find forgiveness for yourself and forgive others.
213163** May you share freely, never taking more than you give.
213164**
213165******************************************************************************
213166**
213167** This is an SQLite module implementing full-text search.
213168*/
213169
213170
213171/* #include "fts5Int.h" */
213172
213173/*
213174** This variable is set to false when running tests for which the on disk
213175** structures should not be corrupt. Otherwise, true. If it is false, extra
213176** assert() conditions in the fts5 code are activated - conditions that are
213177** only true if it is guaranteed that the fts5 database is not corrupt.
213178*/
213179SQLITE_API int sqlite3_fts5_may_be_corrupt = 1;
213180
213181
213182typedef struct Fts5Auxdata Fts5Auxdata;
213183typedef struct Fts5Auxiliary Fts5Auxiliary;
213184typedef struct Fts5Cursor Fts5Cursor;
213185typedef struct Fts5Sorter Fts5Sorter;
213186typedef struct Fts5Table Fts5Table;
213187typedef struct Fts5TokenizerModule Fts5TokenizerModule;
213188
213189/*
213190** NOTES ON TRANSACTIONS:
213191**
213192** SQLite invokes the following virtual table methods as transactions are
213193** opened and closed by the user:
213194**
213195** xBegin(): Start of a new transaction.
213196** xSync(): Initial part of two-phase commit.
213197** xCommit(): Final part of two-phase commit.
213198** xRollback(): Rollback the transaction.
213199**
213200** Anything that is required as part of a commit that may fail is performed
213201** in the xSync() callback. Current versions of SQLite ignore any errors
213202** returned by xCommit().
213203**
213204** And as sub-transactions are opened/closed:
213205**
213206** xSavepoint(int S): Open savepoint S.
213207** xRelease(int S): Commit and close savepoint S.
213208** xRollbackTo(int S): Rollback to start of savepoint S.
213209**
213210** During a write-transaction the fts5_index.c module may cache some data
213211** in-memory. It is flushed to disk whenever xSync(), xRelease() or
213212** xSavepoint() is called. And discarded whenever xRollback() or xRollbackTo()
213213** is called.
213214**
213215** Additionally, if SQLITE_DEBUG is defined, an instance of the following
213216** structure is used to record the current transaction state. This information
213217** is not required, but it is used in the assert() statements executed by
213218** function fts5CheckTransactionState() (see below).
213219*/
213220struct Fts5TransactionState {
213221 int eState; /* 0==closed, 1==open, 2==synced */
213222 int iSavepoint; /* Number of open savepoints (0 -> none) */
213223};
213224
213225/*
213226** A single object of this type is allocated when the FTS5 module is
213227** registered with a database handle. It is used to store pointers to
213228** all registered FTS5 extensions - tokenizers and auxiliary functions.
213229*/
213230struct Fts5Global {
213231 fts5_api api; /* User visible part of object (see fts5.h) */
213232 sqlite3 *db; /* Associated database connection */
213233 i64 iNextId; /* Used to allocate unique cursor ids */
213234 Fts5Auxiliary *pAux; /* First in list of all aux. functions */
213235 Fts5TokenizerModule *pTok; /* First in list of all tokenizer modules */
213236 Fts5TokenizerModule *pDfltTok; /* Default tokenizer module */
213237 Fts5Cursor *pCsr; /* First in list of all open cursors */
213238};
213239
213240/*
213241** Each auxiliary function registered with the FTS5 module is represented
213242** by an object of the following type. All such objects are stored as part
213243** of the Fts5Global.pAux list.
213244*/
213245struct Fts5Auxiliary {
213246 Fts5Global *pGlobal; /* Global context for this function */
213247 char *zFunc; /* Function name (nul-terminated) */
213248 void *pUserData; /* User-data pointer */
213249 fts5_extension_function xFunc; /* Callback function */
213250 void (*xDestroy)(void*); /* Destructor function */
213251 Fts5Auxiliary *pNext; /* Next registered auxiliary function */
213252};
213253
213254/*
213255** Each tokenizer module registered with the FTS5 module is represented
213256** by an object of the following type. All such objects are stored as part
213257** of the Fts5Global.pTok list.
213258*/
213259struct Fts5TokenizerModule {
213260 char *zName; /* Name of tokenizer */
213261 void *pUserData; /* User pointer passed to xCreate() */
213262 fts5_tokenizer x; /* Tokenizer functions */
213263 void (*xDestroy)(void*); /* Destructor function */
213264 Fts5TokenizerModule *pNext; /* Next registered tokenizer module */
213265};
213266
213267/*
213268** Virtual-table object.
213269*/
213270struct Fts5Table {
213271 sqlite3_vtab base; /* Base class used by SQLite core */
213272 Fts5Config *pConfig; /* Virtual table configuration */
213273 Fts5Index *pIndex; /* Full-text index */
213274 Fts5Storage *pStorage; /* Document store */
213275 Fts5Global *pGlobal; /* Global (connection wide) data */
213276 Fts5Cursor *pSortCsr; /* Sort data from this cursor */
213277#ifdef SQLITE_DEBUG
213278 struct Fts5TransactionState ts;
213279#endif
213280};
213281
213282struct Fts5MatchPhrase {
213283 Fts5Buffer *pPoslist; /* Pointer to current poslist */
213284 int nTerm; /* Size of phrase in terms */
213285};
213286
213287/*
213288** pStmt:
213289** SELECT rowid, <fts> FROM <fts> ORDER BY +rank;
213290**
213291** aIdx[]:
213292** There is one entry in the aIdx[] array for each phrase in the query,
213293** the value of which is the offset within aPoslist[] following the last
213294** byte of the position list for the corresponding phrase.
213295*/
213296struct Fts5Sorter {
213297 sqlite3_stmt *pStmt;
213298 i64 iRowid; /* Current rowid */
213299 const u8 *aPoslist; /* Position lists for current row */
213300 int nIdx; /* Number of entries in aIdx[] */
213301 int aIdx[1]; /* Offsets into aPoslist for current row */
213302};
213303
213304
213305/*
213306** Virtual-table cursor object.
213307**
213308** iSpecial:
213309** If this is a 'special' query (refer to function fts5SpecialMatch()),
213310** then this variable contains the result of the query.
213311**
213312** iFirstRowid, iLastRowid:
213313** These variables are only used for FTS5_PLAN_MATCH cursors. Assuming the
213314** cursor iterates in ascending order of rowids, iFirstRowid is the lower
213315** limit of rowids to return, and iLastRowid the upper. In other words, the
213316** WHERE clause in the user's query might have been:
213317**
213318** <tbl> MATCH <expr> AND rowid BETWEEN $iFirstRowid AND $iLastRowid
213319**
213320** If the cursor iterates in descending order of rowid, iFirstRowid
213321** is the upper limit (i.e. the "first" rowid visited) and iLastRowid
213322** the lower.
213323*/
213324struct Fts5Cursor {
213325 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
213326 Fts5Cursor *pNext; /* Next cursor in Fts5Cursor.pCsr list */
213327 int *aColumnSize; /* Values for xColumnSize() */
213328 i64 iCsrId; /* Cursor id */
213329
213330 /* Zero from this point onwards on cursor reset */
213331 int ePlan; /* FTS5_PLAN_XXX value */
213332 int bDesc; /* True for "ORDER BY rowid DESC" queries */
213333 i64 iFirstRowid; /* Return no rowids earlier than this */
213334 i64 iLastRowid; /* Return no rowids later than this */
213335 sqlite3_stmt *pStmt; /* Statement used to read %_content */
213336 Fts5Expr *pExpr; /* Expression for MATCH queries */
213337 Fts5Sorter *pSorter; /* Sorter for "ORDER BY rank" queries */
213338 int csrflags; /* Mask of cursor flags (see below) */
213339 i64 iSpecial; /* Result of special query */
213340
213341 /* "rank" function. Populated on demand from vtab.xColumn(). */
213342 char *zRank; /* Custom rank function */
213343 char *zRankArgs; /* Custom rank function args */
213344 Fts5Auxiliary *pRank; /* Rank callback (or NULL) */
213345 int nRankArg; /* Number of trailing arguments for rank() */
213346 sqlite3_value **apRankArg; /* Array of trailing arguments */
213347 sqlite3_stmt *pRankArgStmt; /* Origin of objects in apRankArg[] */
213348
213349 /* Auxiliary data storage */
213350 Fts5Auxiliary *pAux; /* Currently executing extension function */
213351 Fts5Auxdata *pAuxdata; /* First in linked list of saved aux-data */
213352
213353 /* Cache used by auxiliary functions xInst() and xInstCount() */
213354 Fts5PoslistReader *aInstIter; /* One for each phrase */
213355 int nInstAlloc; /* Size of aInst[] array (entries / 3) */
213356 int nInstCount; /* Number of phrase instances */
213357 int *aInst; /* 3 integers per phrase instance */
213358};
213359
213360/*
213361** Bits that make up the "idxNum" parameter passed indirectly by
213362** xBestIndex() to xFilter().
213363*/
213364#define FTS5_BI_MATCH 0x0001 /* <tbl> MATCH ? */
213365#define FTS5_BI_RANK 0x0002 /* rank MATCH ? */
213366#define FTS5_BI_ROWID_EQ 0x0004 /* rowid == ? */
213367#define FTS5_BI_ROWID_LE 0x0008 /* rowid <= ? */
213368#define FTS5_BI_ROWID_GE 0x0010 /* rowid >= ? */
213369
213370#define FTS5_BI_ORDER_RANK 0x0020
213371#define FTS5_BI_ORDER_ROWID 0x0040
213372#define FTS5_BI_ORDER_DESC 0x0080
213373
213374/*
213375** Values for Fts5Cursor.csrflags
213376*/
213377#define FTS5CSR_EOF 0x01
213378#define FTS5CSR_REQUIRE_CONTENT 0x02
213379#define FTS5CSR_REQUIRE_DOCSIZE 0x04
213380#define FTS5CSR_REQUIRE_INST 0x08
213381#define FTS5CSR_FREE_ZRANK 0x10
213382#define FTS5CSR_REQUIRE_RESEEK 0x20
213383#define FTS5CSR_REQUIRE_POSLIST 0x40
213384
213385#define BitFlagAllTest(x,y) (((x) & (y))==(y))
213386#define BitFlagTest(x,y) (((x) & (y))!=0)
213387
213388
213389/*
213390** Macros to Set(), Clear() and Test() cursor flags.
213391*/
213392#define CsrFlagSet(pCsr, flag) ((pCsr)->csrflags |= (flag))
213393#define CsrFlagClear(pCsr, flag) ((pCsr)->csrflags &= ~(flag))
213394#define CsrFlagTest(pCsr, flag) ((pCsr)->csrflags & (flag))
213395
213396struct Fts5Auxdata {
213397 Fts5Auxiliary *pAux; /* Extension to which this belongs */
213398 void *pPtr; /* Pointer value */
213399 void(*xDelete)(void*); /* Destructor */
213400 Fts5Auxdata *pNext; /* Next object in linked list */
213401};
213402
213403#ifdef SQLITE_DEBUG
213404#define FTS5_BEGIN 1
213405#define FTS5_SYNC 2
213406#define FTS5_COMMIT 3
213407#define FTS5_ROLLBACK 4
213408#define FTS5_SAVEPOINT 5
213409#define FTS5_RELEASE 6
213410#define FTS5_ROLLBACKTO 7
213411static void fts5CheckTransactionState(Fts5Table *p, int op, int iSavepoint){
213412 switch( op ){
213413 case FTS5_BEGIN:
213414 assert( p->ts.eState==0 );
213415 p->ts.eState = 1;
213416 p->ts.iSavepoint = -1;
213417 break;
213418
213419 case FTS5_SYNC:
213420 assert( p->ts.eState==1 );
213421 p->ts.eState = 2;
213422 break;
213423
213424 case FTS5_COMMIT:
213425 assert( p->ts.eState==2 );
213426 p->ts.eState = 0;
213427 break;
213428
213429 case FTS5_ROLLBACK:
213430 assert( p->ts.eState==1 || p->ts.eState==2 || p->ts.eState==0 );
213431 p->ts.eState = 0;
213432 break;
213433
213434 case FTS5_SAVEPOINT:
213435 assert( p->ts.eState==1 );
213436 assert( iSavepoint>=0 );
213437 assert( iSavepoint>=p->ts.iSavepoint );
213438 p->ts.iSavepoint = iSavepoint;
213439 break;
213440
213441 case FTS5_RELEASE:
213442 assert( p->ts.eState==1 );
213443 assert( iSavepoint>=0 );
213444 assert( iSavepoint<=p->ts.iSavepoint );
213445 p->ts.iSavepoint = iSavepoint-1;
213446 break;
213447
213448 case FTS5_ROLLBACKTO:
213449 assert( p->ts.eState==1 );
213450 assert( iSavepoint>=0 );
213451 assert( iSavepoint<=p->ts.iSavepoint );
213452 p->ts.iSavepoint = iSavepoint;
213453 break;
213454 }
213455}
213456#else
213457# define fts5CheckTransactionState(x,y,z)
213458#endif
213459
213460/*
213461** Return true if pTab is a contentless table.
213462*/
213463static int fts5IsContentless(Fts5Table *pTab){
213464 return pTab->pConfig->eContent==FTS5_CONTENT_NONE;
213465}
213466
213467/*
213468** Delete a virtual table handle allocated by fts5InitVtab().
213469*/
213470static void fts5FreeVtab(Fts5Table *pTab){
213471 if( pTab ){
213472 sqlite3Fts5IndexClose(pTab->pIndex);
213473 sqlite3Fts5StorageClose(pTab->pStorage);
213474 sqlite3Fts5ConfigFree(pTab->pConfig);
213475 sqlite3_free(pTab);
213476 }
213477}
213478
213479/*
213480** The xDisconnect() virtual table method.
213481*/
213482static int fts5DisconnectMethod(sqlite3_vtab *pVtab){
213483 fts5FreeVtab((Fts5Table*)pVtab);
213484 return SQLITE_OK;
213485}
213486
213487/*
213488** The xDestroy() virtual table method.
213489*/
213490static int fts5DestroyMethod(sqlite3_vtab *pVtab){
213491 Fts5Table *pTab = (Fts5Table*)pVtab;
213492 int rc = sqlite3Fts5DropAll(pTab->pConfig);
213493 if( rc==SQLITE_OK ){
213494 fts5FreeVtab((Fts5Table*)pVtab);
213495 }
213496 return rc;
213497}
213498
213499/*
213500** This function is the implementation of both the xConnect and xCreate
213501** methods of the FTS3 virtual table.
213502**
213503** The argv[] array contains the following:
213504**
213505** argv[0] -> module name ("fts5")
213506** argv[1] -> database name
213507** argv[2] -> table name
213508** argv[...] -> "column name" and other module argument fields.
213509*/
213510static int fts5InitVtab(
213511 int bCreate, /* True for xCreate, false for xConnect */
213512 sqlite3 *db, /* The SQLite database connection */
213513 void *pAux, /* Hash table containing tokenizers */
213514 int argc, /* Number of elements in argv array */
213515 const char * const *argv, /* xCreate/xConnect argument array */
213516 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
213517 char **pzErr /* Write any error message here */
213518){
213519 Fts5Global *pGlobal = (Fts5Global*)pAux;
213520 const char **azConfig = (const char**)argv;
213521 int rc = SQLITE_OK; /* Return code */
213522 Fts5Config *pConfig = 0; /* Results of parsing argc/argv */
213523 Fts5Table *pTab = 0; /* New virtual table object */
213524
213525 /* Allocate the new vtab object and parse the configuration */
213526 pTab = (Fts5Table*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Table));
213527 if( rc==SQLITE_OK ){
213528 rc = sqlite3Fts5ConfigParse(pGlobal, db, argc, azConfig, &pConfig, pzErr);
213529 assert( (rc==SQLITE_OK && *pzErr==0) || pConfig==0 );
213530 }
213531 if( rc==SQLITE_OK ){
213532 pTab->pConfig = pConfig;
213533 pTab->pGlobal = pGlobal;
213534 }
213535
213536 /* Open the index sub-system */
213537 if( rc==SQLITE_OK ){
213538 rc = sqlite3Fts5IndexOpen(pConfig, bCreate, &pTab->pIndex, pzErr);
213539 }
213540
213541 /* Open the storage sub-system */
213542 if( rc==SQLITE_OK ){
213543 rc = sqlite3Fts5StorageOpen(
213544 pConfig, pTab->pIndex, bCreate, &pTab->pStorage, pzErr
213545 );
213546 }
213547
213548 /* Call sqlite3_declare_vtab() */
213549 if( rc==SQLITE_OK ){
213550 rc = sqlite3Fts5ConfigDeclareVtab(pConfig);
213551 }
213552
213553 /* Load the initial configuration */
213554 if( rc==SQLITE_OK ){
213555 assert( pConfig->pzErrmsg==0 );
213556 pConfig->pzErrmsg = pzErr;
213557 rc = sqlite3Fts5IndexLoadConfig(pTab->pIndex);
213558 sqlite3Fts5IndexRollback(pTab->pIndex);
213559 pConfig->pzErrmsg = 0;
213560 }
213561
213562 if( rc!=SQLITE_OK ){
213563 fts5FreeVtab(pTab);
213564 pTab = 0;
213565 }else if( bCreate ){
213566 fts5CheckTransactionState(pTab, FTS5_BEGIN, 0);
213567 }
213568 *ppVTab = (sqlite3_vtab*)pTab;
213569 return rc;
213570}
213571
213572/*
213573** The xConnect() and xCreate() methods for the virtual table. All the
213574** work is done in function fts5InitVtab().
213575*/
213576static int fts5ConnectMethod(
213577 sqlite3 *db, /* Database connection */
213578 void *pAux, /* Pointer to tokenizer hash table */
213579 int argc, /* Number of elements in argv array */
213580 const char * const *argv, /* xCreate/xConnect argument array */
213581 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
213582 char **pzErr /* OUT: sqlite3_malloc'd error message */
213583){
213584 return fts5InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
213585}
213586static int fts5CreateMethod(
213587 sqlite3 *db, /* Database connection */
213588 void *pAux, /* Pointer to tokenizer hash table */
213589 int argc, /* Number of elements in argv array */
213590 const char * const *argv, /* xCreate/xConnect argument array */
213591 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
213592 char **pzErr /* OUT: sqlite3_malloc'd error message */
213593){
213594 return fts5InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
213595}
213596
213597/*
213598** The different query plans.
213599*/
213600#define FTS5_PLAN_MATCH 1 /* (<tbl> MATCH ?) */
213601#define FTS5_PLAN_SOURCE 2 /* A source cursor for SORTED_MATCH */
213602#define FTS5_PLAN_SPECIAL 3 /* An internal query */
213603#define FTS5_PLAN_SORTED_MATCH 4 /* (<tbl> MATCH ? ORDER BY rank) */
213604#define FTS5_PLAN_SCAN 5 /* No usable constraint */
213605#define FTS5_PLAN_ROWID 6 /* (rowid = ?) */
213606
213607/*
213608** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
213609** extension is currently being used by a version of SQLite too old to
213610** support index-info flags. In that case this function is a no-op.
213611*/
213612static void fts5SetUniqueFlag(sqlite3_index_info *pIdxInfo){
213613#if SQLITE_VERSION_NUMBER>=3008012
213614#ifndef SQLITE_CORE
213615 if( sqlite3_libversion_number()>=3008012 )
213616#endif
213617 {
213618 pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE;
213619 }
213620#endif
213621}
213622
213623/*
213624** Implementation of the xBestIndex method for FTS5 tables. Within the
213625** WHERE constraint, it searches for the following:
213626**
213627** 1. A MATCH constraint against the special column.
213628** 2. A MATCH constraint against the "rank" column.
213629** 3. An == constraint against the rowid column.
213630** 4. A < or <= constraint against the rowid column.
213631** 5. A > or >= constraint against the rowid column.
213632**
213633** Within the ORDER BY, either:
213634**
213635** 5. ORDER BY rank [ASC|DESC]
213636** 6. ORDER BY rowid [ASC|DESC]
213637**
213638** Costs are assigned as follows:
213639**
213640** a) If an unusable MATCH operator is present in the WHERE clause, the
213641** cost is unconditionally set to 1e50 (a really big number).
213642**
213643** a) If a MATCH operator is present, the cost depends on the other
213644** constraints also present. As follows:
213645**
213646** * No other constraints: cost=1000.0
213647** * One rowid range constraint: cost=750.0
213648** * Both rowid range constraints: cost=500.0
213649** * An == rowid constraint: cost=100.0
213650**
213651** b) Otherwise, if there is no MATCH:
213652**
213653** * No other constraints: cost=1000000.0
213654** * One rowid range constraint: cost=750000.0
213655** * Both rowid range constraints: cost=250000.0
213656** * An == rowid constraint: cost=10.0
213657**
213658** Costs are not modified by the ORDER BY clause.
213659*/
213660static int fts5BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
213661 Fts5Table *pTab = (Fts5Table*)pVTab;
213662 Fts5Config *pConfig = pTab->pConfig;
213663 const int nCol = pConfig->nCol;
213664 int idxFlags = 0; /* Parameter passed through to xFilter() */
213665 int bHasMatch;
213666 int iNext;
213667 int i;
213668
213669 struct Constraint {
213670 int op; /* Mask against sqlite3_index_constraint.op */
213671 int fts5op; /* FTS5 mask for idxFlags */
213672 int iCol; /* 0==rowid, 1==tbl, 2==rank */
213673 int omit; /* True to omit this if found */
213674 int iConsIndex; /* Index in pInfo->aConstraint[] */
213675 } aConstraint[] = {
213676 {SQLITE_INDEX_CONSTRAINT_MATCH|SQLITE_INDEX_CONSTRAINT_EQ,
213677 FTS5_BI_MATCH, 1, 1, -1},
213678 {SQLITE_INDEX_CONSTRAINT_MATCH|SQLITE_INDEX_CONSTRAINT_EQ,
213679 FTS5_BI_RANK, 2, 1, -1},
213680 {SQLITE_INDEX_CONSTRAINT_EQ, FTS5_BI_ROWID_EQ, 0, 0, -1},
213681 {SQLITE_INDEX_CONSTRAINT_LT|SQLITE_INDEX_CONSTRAINT_LE,
213682 FTS5_BI_ROWID_LE, 0, 0, -1},
213683 {SQLITE_INDEX_CONSTRAINT_GT|SQLITE_INDEX_CONSTRAINT_GE,
213684 FTS5_BI_ROWID_GE, 0, 0, -1},
213685 };
213686
213687 int aColMap[3];
213688 aColMap[0] = -1;
213689 aColMap[1] = nCol;
213690 aColMap[2] = nCol+1;
213691
213692 assert( SQLITE_INDEX_CONSTRAINT_EQ<SQLITE_INDEX_CONSTRAINT_MATCH );
213693 assert( SQLITE_INDEX_CONSTRAINT_GT<SQLITE_INDEX_CONSTRAINT_MATCH );
213694 assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH );
213695 assert( SQLITE_INDEX_CONSTRAINT_GE<SQLITE_INDEX_CONSTRAINT_MATCH );
213696 assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH );
213697
213698 /* Set idxFlags flags for all WHERE clause terms that will be used. */
213699 for(i=0; i<pInfo->nConstraint; i++){
213700 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
213701 int iCol = p->iColumn;
213702
213703 if( (p->op==SQLITE_INDEX_CONSTRAINT_MATCH && iCol>=0 && iCol<=nCol)
213704 || (p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol==nCol)
213705 ){
213706 /* A MATCH operator or equivalent */
213707 if( p->usable ){
213708 idxFlags = (idxFlags & 0xFFFF) | FTS5_BI_MATCH | (iCol << 16);
213709 aConstraint[0].iConsIndex = i;
213710 }else{
213711 /* As there exists an unusable MATCH constraint this is an
213712 ** unusable plan. Set a prohibitively high cost. */
213713 pInfo->estimatedCost = 1e50;
213714 return SQLITE_OK;
213715 }
213716 }else if( p->op<=SQLITE_INDEX_CONSTRAINT_MATCH ){
213717 int j;
213718 for(j=1; j<ArraySize(aConstraint); j++){
213719 struct Constraint *pC = &aConstraint[j];
213720 if( iCol==aColMap[pC->iCol] && (p->op & pC->op) && p->usable ){
213721 pC->iConsIndex = i;
213722 idxFlags |= pC->fts5op;
213723 }
213724 }
213725 }
213726 }
213727
213728 /* Set idxFlags flags for the ORDER BY clause */
213729 if( pInfo->nOrderBy==1 ){
213730 int iSort = pInfo->aOrderBy[0].iColumn;
213731 if( iSort==(pConfig->nCol+1) && BitFlagTest(idxFlags, FTS5_BI_MATCH) ){
213732 idxFlags |= FTS5_BI_ORDER_RANK;
213733 }else if( iSort==-1 ){
213734 idxFlags |= FTS5_BI_ORDER_ROWID;
213735 }
213736 if( BitFlagTest(idxFlags, FTS5_BI_ORDER_RANK|FTS5_BI_ORDER_ROWID) ){
213737 pInfo->orderByConsumed = 1;
213738 if( pInfo->aOrderBy[0].desc ){
213739 idxFlags |= FTS5_BI_ORDER_DESC;
213740 }
213741 }
213742 }
213743
213744 /* Calculate the estimated cost based on the flags set in idxFlags. */
213745 bHasMatch = BitFlagTest(idxFlags, FTS5_BI_MATCH);
213746 if( BitFlagTest(idxFlags, FTS5_BI_ROWID_EQ) ){
213747 pInfo->estimatedCost = bHasMatch ? 100.0 : 10.0;
213748 if( bHasMatch==0 ) fts5SetUniqueFlag(pInfo);
213749 }else if( BitFlagAllTest(idxFlags, FTS5_BI_ROWID_LE|FTS5_BI_ROWID_GE) ){
213750 pInfo->estimatedCost = bHasMatch ? 500.0 : 250000.0;
213751 }else if( BitFlagTest(idxFlags, FTS5_BI_ROWID_LE|FTS5_BI_ROWID_GE) ){
213752 pInfo->estimatedCost = bHasMatch ? 750.0 : 750000.0;
213753 }else{
213754 pInfo->estimatedCost = bHasMatch ? 1000.0 : 1000000.0;
213755 }
213756
213757 /* Assign argvIndex values to each constraint in use. */
213758 iNext = 1;
213759 for(i=0; i<ArraySize(aConstraint); i++){
213760 struct Constraint *pC = &aConstraint[i];
213761 if( pC->iConsIndex>=0 ){
213762 pInfo->aConstraintUsage[pC->iConsIndex].argvIndex = iNext++;
213763 pInfo->aConstraintUsage[pC->iConsIndex].omit = (unsigned char)pC->omit;
213764 }
213765 }
213766
213767 pInfo->idxNum = idxFlags;
213768 return SQLITE_OK;
213769}
213770
213771static int fts5NewTransaction(Fts5Table *pTab){
213772 Fts5Cursor *pCsr;
213773 for(pCsr=pTab->pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
213774 if( pCsr->base.pVtab==(sqlite3_vtab*)pTab ) return SQLITE_OK;
213775 }
213776 return sqlite3Fts5StorageReset(pTab->pStorage);
213777}
213778
213779/*
213780** Implementation of xOpen method.
213781*/
213782static int fts5OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
213783 Fts5Table *pTab = (Fts5Table*)pVTab;
213784 Fts5Config *pConfig = pTab->pConfig;
213785 Fts5Cursor *pCsr = 0; /* New cursor object */
213786 int nByte; /* Bytes of space to allocate */
213787 int rc; /* Return code */
213788
213789 rc = fts5NewTransaction(pTab);
213790 if( rc==SQLITE_OK ){
213791 nByte = sizeof(Fts5Cursor) + pConfig->nCol * sizeof(int);
213792 pCsr = (Fts5Cursor*)sqlite3_malloc(nByte);
213793 if( pCsr ){
213794 Fts5Global *pGlobal = pTab->pGlobal;
213795 memset(pCsr, 0, nByte);
213796 pCsr->aColumnSize = (int*)&pCsr[1];
213797 pCsr->pNext = pGlobal->pCsr;
213798 pGlobal->pCsr = pCsr;
213799 pCsr->iCsrId = ++pGlobal->iNextId;
213800 }else{
213801 rc = SQLITE_NOMEM;
213802 }
213803 }
213804 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
213805 return rc;
213806}
213807
213808static int fts5StmtType(Fts5Cursor *pCsr){
213809 if( pCsr->ePlan==FTS5_PLAN_SCAN ){
213810 return (pCsr->bDesc) ? FTS5_STMT_SCAN_DESC : FTS5_STMT_SCAN_ASC;
213811 }
213812 return FTS5_STMT_LOOKUP;
213813}
213814
213815/*
213816** This function is called after the cursor passed as the only argument
213817** is moved to point at a different row. It clears all cached data
213818** specific to the previous row stored by the cursor object.
213819*/
213820static void fts5CsrNewrow(Fts5Cursor *pCsr){
213821 CsrFlagSet(pCsr,
213822 FTS5CSR_REQUIRE_CONTENT
213823 | FTS5CSR_REQUIRE_DOCSIZE
213824 | FTS5CSR_REQUIRE_INST
213825 | FTS5CSR_REQUIRE_POSLIST
213826 );
213827}
213828
213829static void fts5FreeCursorComponents(Fts5Cursor *pCsr){
213830 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
213831 Fts5Auxdata *pData;
213832 Fts5Auxdata *pNext;
213833
213834 sqlite3_free(pCsr->aInstIter);
213835 sqlite3_free(pCsr->aInst);
213836 if( pCsr->pStmt ){
213837 int eStmt = fts5StmtType(pCsr);
213838 sqlite3Fts5StorageStmtRelease(pTab->pStorage, eStmt, pCsr->pStmt);
213839 }
213840 if( pCsr->pSorter ){
213841 Fts5Sorter *pSorter = pCsr->pSorter;
213842 sqlite3_finalize(pSorter->pStmt);
213843 sqlite3_free(pSorter);
213844 }
213845
213846 if( pCsr->ePlan!=FTS5_PLAN_SOURCE ){
213847 sqlite3Fts5ExprFree(pCsr->pExpr);
213848 }
213849
213850 for(pData=pCsr->pAuxdata; pData; pData=pNext){
213851 pNext = pData->pNext;
213852 if( pData->xDelete ) pData->xDelete(pData->pPtr);
213853 sqlite3_free(pData);
213854 }
213855
213856 sqlite3_finalize(pCsr->pRankArgStmt);
213857 sqlite3_free(pCsr->apRankArg);
213858
213859 if( CsrFlagTest(pCsr, FTS5CSR_FREE_ZRANK) ){
213860 sqlite3_free(pCsr->zRank);
213861 sqlite3_free(pCsr->zRankArgs);
213862 }
213863
213864 memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan - (u8*)pCsr));
213865}
213866
213867
213868/*
213869** Close the cursor. For additional information see the documentation
213870** on the xClose method of the virtual table interface.
213871*/
213872static int fts5CloseMethod(sqlite3_vtab_cursor *pCursor){
213873 if( pCursor ){
213874 Fts5Table *pTab = (Fts5Table*)(pCursor->pVtab);
213875 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
213876 Fts5Cursor **pp;
213877
213878 fts5FreeCursorComponents(pCsr);
213879 /* Remove the cursor from the Fts5Global.pCsr list */
213880 for(pp=&pTab->pGlobal->pCsr; (*pp)!=pCsr; pp=&(*pp)->pNext);
213881 *pp = pCsr->pNext;
213882
213883 sqlite3_free(pCsr);
213884 }
213885 return SQLITE_OK;
213886}
213887
213888static int fts5SorterNext(Fts5Cursor *pCsr){
213889 Fts5Sorter *pSorter = pCsr->pSorter;
213890 int rc;
213891
213892 rc = sqlite3_step(pSorter->pStmt);
213893 if( rc==SQLITE_DONE ){
213894 rc = SQLITE_OK;
213895 CsrFlagSet(pCsr, FTS5CSR_EOF);
213896 }else if( rc==SQLITE_ROW ){
213897 const u8 *a;
213898 const u8 *aBlob;
213899 int nBlob;
213900 int i;
213901 int iOff = 0;
213902 rc = SQLITE_OK;
213903
213904 pSorter->iRowid = sqlite3_column_int64(pSorter->pStmt, 0);
213905 nBlob = sqlite3_column_bytes(pSorter->pStmt, 1);
213906 aBlob = a = sqlite3_column_blob(pSorter->pStmt, 1);
213907
213908 /* nBlob==0 in detail=none mode. */
213909 if( nBlob>0 ){
213910 for(i=0; i<(pSorter->nIdx-1); i++){
213911 int iVal;
213912 a += fts5GetVarint32(a, iVal);
213913 iOff += iVal;
213914 pSorter->aIdx[i] = iOff;
213915 }
213916 pSorter->aIdx[i] = &aBlob[nBlob] - a;
213917 pSorter->aPoslist = a;
213918 }
213919
213920 fts5CsrNewrow(pCsr);
213921 }
213922
213923 return rc;
213924}
213925
213926
213927/*
213928** Set the FTS5CSR_REQUIRE_RESEEK flag on all FTS5_PLAN_MATCH cursors
213929** open on table pTab.
213930*/
213931static void fts5TripCursors(Fts5Table *pTab){
213932 Fts5Cursor *pCsr;
213933 for(pCsr=pTab->pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
213934 if( pCsr->ePlan==FTS5_PLAN_MATCH
213935 && pCsr->base.pVtab==(sqlite3_vtab*)pTab
213936 ){
213937 CsrFlagSet(pCsr, FTS5CSR_REQUIRE_RESEEK);
213938 }
213939 }
213940}
213941
213942/*
213943** If the REQUIRE_RESEEK flag is set on the cursor passed as the first
213944** argument, close and reopen all Fts5IndexIter iterators that the cursor
213945** is using. Then attempt to move the cursor to a rowid equal to or laster
213946** (in the cursors sort order - ASC or DESC) than the current rowid.
213947**
213948** If the new rowid is not equal to the old, set output parameter *pbSkip
213949** to 1 before returning. Otherwise, leave it unchanged.
213950**
213951** Return SQLITE_OK if successful or if no reseek was required, or an
213952** error code if an error occurred.
213953*/
213954static int fts5CursorReseek(Fts5Cursor *pCsr, int *pbSkip){
213955 int rc = SQLITE_OK;
213956 assert( *pbSkip==0 );
213957 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_RESEEK) ){
213958 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
213959 int bDesc = pCsr->bDesc;
213960 i64 iRowid = sqlite3Fts5ExprRowid(pCsr->pExpr);
213961
213962 rc = sqlite3Fts5ExprFirst(pCsr->pExpr, pTab->pIndex, iRowid, bDesc);
213963 if( rc==SQLITE_OK && iRowid!=sqlite3Fts5ExprRowid(pCsr->pExpr) ){
213964 *pbSkip = 1;
213965 }
213966
213967 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_RESEEK);
213968 fts5CsrNewrow(pCsr);
213969 if( sqlite3Fts5ExprEof(pCsr->pExpr) ){
213970 CsrFlagSet(pCsr, FTS5CSR_EOF);
213971 *pbSkip = 1;
213972 }
213973 }
213974 return rc;
213975}
213976
213977
213978/*
213979** Advance the cursor to the next row in the table that matches the
213980** search criteria.
213981**
213982** Return SQLITE_OK if nothing goes wrong. SQLITE_OK is returned
213983** even if we reach end-of-file. The fts5EofMethod() will be called
213984** subsequently to determine whether or not an EOF was hit.
213985*/
213986static int fts5NextMethod(sqlite3_vtab_cursor *pCursor){
213987 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
213988 int rc;
213989
213990 assert( (pCsr->ePlan<3)==
213991 (pCsr->ePlan==FTS5_PLAN_MATCH || pCsr->ePlan==FTS5_PLAN_SOURCE)
213992 );
213993 assert( !CsrFlagTest(pCsr, FTS5CSR_EOF) );
213994
213995 if( pCsr->ePlan<3 ){
213996 int bSkip = 0;
213997 if( (rc = fts5CursorReseek(pCsr, &bSkip)) || bSkip ) return rc;
213998 rc = sqlite3Fts5ExprNext(pCsr->pExpr, pCsr->iLastRowid);
213999 CsrFlagSet(pCsr, sqlite3Fts5ExprEof(pCsr->pExpr));
214000 fts5CsrNewrow(pCsr);
214001 }else{
214002 switch( pCsr->ePlan ){
214003 case FTS5_PLAN_SPECIAL: {
214004 CsrFlagSet(pCsr, FTS5CSR_EOF);
214005 rc = SQLITE_OK;
214006 break;
214007 }
214008
214009 case FTS5_PLAN_SORTED_MATCH: {
214010 rc = fts5SorterNext(pCsr);
214011 break;
214012 }
214013
214014 default:
214015 rc = sqlite3_step(pCsr->pStmt);
214016 if( rc!=SQLITE_ROW ){
214017 CsrFlagSet(pCsr, FTS5CSR_EOF);
214018 rc = sqlite3_reset(pCsr->pStmt);
214019 }else{
214020 rc = SQLITE_OK;
214021 }
214022 break;
214023 }
214024 }
214025
214026 return rc;
214027}
214028
214029
214030static int fts5PrepareStatement(
214031 sqlite3_stmt **ppStmt,
214032 Fts5Config *pConfig,
214033 const char *zFmt,
214034 ...
214035){
214036 sqlite3_stmt *pRet = 0;
214037 int rc;
214038 char *zSql;
214039 va_list ap;
214040
214041 va_start(ap, zFmt);
214042 zSql = sqlite3_vmprintf(zFmt, ap);
214043 if( zSql==0 ){
214044 rc = SQLITE_NOMEM;
214045 }else{
214046 rc = sqlite3_prepare_v3(pConfig->db, zSql, -1,
214047 SQLITE_PREPARE_PERSISTENT, &pRet, 0);
214048 if( rc!=SQLITE_OK ){
214049 *pConfig->pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(pConfig->db));
214050 }
214051 sqlite3_free(zSql);
214052 }
214053
214054 va_end(ap);
214055 *ppStmt = pRet;
214056 return rc;
214057}
214058
214059static int fts5CursorFirstSorted(Fts5Table *pTab, Fts5Cursor *pCsr, int bDesc){
214060 Fts5Config *pConfig = pTab->pConfig;
214061 Fts5Sorter *pSorter;
214062 int nPhrase;
214063 int nByte;
214064 int rc;
214065 const char *zRank = pCsr->zRank;
214066 const char *zRankArgs = pCsr->zRankArgs;
214067
214068 nPhrase = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
214069 nByte = sizeof(Fts5Sorter) + sizeof(int) * (nPhrase-1);
214070 pSorter = (Fts5Sorter*)sqlite3_malloc(nByte);
214071 if( pSorter==0 ) return SQLITE_NOMEM;
214072 memset(pSorter, 0, nByte);
214073 pSorter->nIdx = nPhrase;
214074
214075 /* TODO: It would be better to have some system for reusing statement
214076 ** handles here, rather than preparing a new one for each query. But that
214077 ** is not possible as SQLite reference counts the virtual table objects.
214078 ** And since the statement required here reads from this very virtual
214079 ** table, saving it creates a circular reference.
214080 **
214081 ** If SQLite a built-in statement cache, this wouldn't be a problem. */
214082 rc = fts5PrepareStatement(&pSorter->pStmt, pConfig,
214083 "SELECT rowid, rank FROM %Q.%Q ORDER BY %s(%s%s%s) %s",
214084 pConfig->zDb, pConfig->zName, zRank, pConfig->zName,
214085 (zRankArgs ? ", " : ""),
214086 (zRankArgs ? zRankArgs : ""),
214087 bDesc ? "DESC" : "ASC"
214088 );
214089
214090 pCsr->pSorter = pSorter;
214091 if( rc==SQLITE_OK ){
214092 assert( pTab->pSortCsr==0 );
214093 pTab->pSortCsr = pCsr;
214094 rc = fts5SorterNext(pCsr);
214095 pTab->pSortCsr = 0;
214096 }
214097
214098 if( rc!=SQLITE_OK ){
214099 sqlite3_finalize(pSorter->pStmt);
214100 sqlite3_free(pSorter);
214101 pCsr->pSorter = 0;
214102 }
214103
214104 return rc;
214105}
214106
214107static int fts5CursorFirst(Fts5Table *pTab, Fts5Cursor *pCsr, int bDesc){
214108 int rc;
214109 Fts5Expr *pExpr = pCsr->pExpr;
214110 rc = sqlite3Fts5ExprFirst(pExpr, pTab->pIndex, pCsr->iFirstRowid, bDesc);
214111 if( sqlite3Fts5ExprEof(pExpr) ){
214112 CsrFlagSet(pCsr, FTS5CSR_EOF);
214113 }
214114 fts5CsrNewrow(pCsr);
214115 return rc;
214116}
214117
214118/*
214119** Process a "special" query. A special query is identified as one with a
214120** MATCH expression that begins with a '*' character. The remainder of
214121** the text passed to the MATCH operator are used as the special query
214122** parameters.
214123*/
214124static int fts5SpecialMatch(
214125 Fts5Table *pTab,
214126 Fts5Cursor *pCsr,
214127 const char *zQuery
214128){
214129 int rc = SQLITE_OK; /* Return code */
214130 const char *z = zQuery; /* Special query text */
214131 int n; /* Number of bytes in text at z */
214132
214133 while( z[0]==' ' ) z++;
214134 for(n=0; z[n] && z[n]!=' '; n++);
214135
214136 assert( pTab->base.zErrMsg==0 );
214137 pCsr->ePlan = FTS5_PLAN_SPECIAL;
214138
214139 if( 0==sqlite3_strnicmp("reads", z, n) ){
214140 pCsr->iSpecial = sqlite3Fts5IndexReads(pTab->pIndex);
214141 }
214142 else if( 0==sqlite3_strnicmp("id", z, n) ){
214143 pCsr->iSpecial = pCsr->iCsrId;
214144 }
214145 else{
214146 /* An unrecognized directive. Return an error message. */
214147 pTab->base.zErrMsg = sqlite3_mprintf("unknown special query: %.*s", n, z);
214148 rc = SQLITE_ERROR;
214149 }
214150
214151 return rc;
214152}
214153
214154/*
214155** Search for an auxiliary function named zName that can be used with table
214156** pTab. If one is found, return a pointer to the corresponding Fts5Auxiliary
214157** structure. Otherwise, if no such function exists, return NULL.
214158*/
214159static Fts5Auxiliary *fts5FindAuxiliary(Fts5Table *pTab, const char *zName){
214160 Fts5Auxiliary *pAux;
214161
214162 for(pAux=pTab->pGlobal->pAux; pAux; pAux=pAux->pNext){
214163 if( sqlite3_stricmp(zName, pAux->zFunc)==0 ) return pAux;
214164 }
214165
214166 /* No function of the specified name was found. Return 0. */
214167 return 0;
214168}
214169
214170
214171static int fts5FindRankFunction(Fts5Cursor *pCsr){
214172 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
214173 Fts5Config *pConfig = pTab->pConfig;
214174 int rc = SQLITE_OK;
214175 Fts5Auxiliary *pAux = 0;
214176 const char *zRank = pCsr->zRank;
214177 const char *zRankArgs = pCsr->zRankArgs;
214178
214179 if( zRankArgs ){
214180 char *zSql = sqlite3Fts5Mprintf(&rc, "SELECT %s", zRankArgs);
214181 if( zSql ){
214182 sqlite3_stmt *pStmt = 0;
214183 rc = sqlite3_prepare_v3(pConfig->db, zSql, -1,
214184 SQLITE_PREPARE_PERSISTENT, &pStmt, 0);
214185 sqlite3_free(zSql);
214186 assert( rc==SQLITE_OK || pCsr->pRankArgStmt==0 );
214187 if( rc==SQLITE_OK ){
214188 if( SQLITE_ROW==sqlite3_step(pStmt) ){
214189 int nByte;
214190 pCsr->nRankArg = sqlite3_column_count(pStmt);
214191 nByte = sizeof(sqlite3_value*)*pCsr->nRankArg;
214192 pCsr->apRankArg = (sqlite3_value**)sqlite3Fts5MallocZero(&rc, nByte);
214193 if( rc==SQLITE_OK ){
214194 int i;
214195 for(i=0; i<pCsr->nRankArg; i++){
214196 pCsr->apRankArg[i] = sqlite3_column_value(pStmt, i);
214197 }
214198 }
214199 pCsr->pRankArgStmt = pStmt;
214200 }else{
214201 rc = sqlite3_finalize(pStmt);
214202 assert( rc!=SQLITE_OK );
214203 }
214204 }
214205 }
214206 }
214207
214208 if( rc==SQLITE_OK ){
214209 pAux = fts5FindAuxiliary(pTab, zRank);
214210 if( pAux==0 ){
214211 assert( pTab->base.zErrMsg==0 );
214212 pTab->base.zErrMsg = sqlite3_mprintf("no such function: %s", zRank);
214213 rc = SQLITE_ERROR;
214214 }
214215 }
214216
214217 pCsr->pRank = pAux;
214218 return rc;
214219}
214220
214221
214222static int fts5CursorParseRank(
214223 Fts5Config *pConfig,
214224 Fts5Cursor *pCsr,
214225 sqlite3_value *pRank
214226){
214227 int rc = SQLITE_OK;
214228 if( pRank ){
214229 const char *z = (const char*)sqlite3_value_text(pRank);
214230 char *zRank = 0;
214231 char *zRankArgs = 0;
214232
214233 if( z==0 ){
214234 if( sqlite3_value_type(pRank)==SQLITE_NULL ) rc = SQLITE_ERROR;
214235 }else{
214236 rc = sqlite3Fts5ConfigParseRank(z, &zRank, &zRankArgs);
214237 }
214238 if( rc==SQLITE_OK ){
214239 pCsr->zRank = zRank;
214240 pCsr->zRankArgs = zRankArgs;
214241 CsrFlagSet(pCsr, FTS5CSR_FREE_ZRANK);
214242 }else if( rc==SQLITE_ERROR ){
214243 pCsr->base.pVtab->zErrMsg = sqlite3_mprintf(
214244 "parse error in rank function: %s", z
214245 );
214246 }
214247 }else{
214248 if( pConfig->zRank ){
214249 pCsr->zRank = (char*)pConfig->zRank;
214250 pCsr->zRankArgs = (char*)pConfig->zRankArgs;
214251 }else{
214252 pCsr->zRank = (char*)FTS5_DEFAULT_RANK;
214253 pCsr->zRankArgs = 0;
214254 }
214255 }
214256 return rc;
214257}
214258
214259static i64 fts5GetRowidLimit(sqlite3_value *pVal, i64 iDefault){
214260 if( pVal ){
214261 int eType = sqlite3_value_numeric_type(pVal);
214262 if( eType==SQLITE_INTEGER ){
214263 return sqlite3_value_int64(pVal);
214264 }
214265 }
214266 return iDefault;
214267}
214268
214269/*
214270** This is the xFilter interface for the virtual table. See
214271** the virtual table xFilter method documentation for additional
214272** information.
214273**
214274** There are three possible query strategies:
214275**
214276** 1. Full-text search using a MATCH operator.
214277** 2. A by-rowid lookup.
214278** 3. A full-table scan.
214279*/
214280static int fts5FilterMethod(
214281 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
214282 int idxNum, /* Strategy index */
214283 const char *zUnused, /* Unused */
214284 int nVal, /* Number of elements in apVal */
214285 sqlite3_value **apVal /* Arguments for the indexing scheme */
214286){
214287 Fts5Table *pTab = (Fts5Table*)(pCursor->pVtab);
214288 Fts5Config *pConfig = pTab->pConfig;
214289 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
214290 int rc = SQLITE_OK; /* Error code */
214291 int iVal = 0; /* Counter for apVal[] */
214292 int bDesc; /* True if ORDER BY [rank|rowid] DESC */
214293 int bOrderByRank; /* True if ORDER BY rank */
214294 sqlite3_value *pMatch = 0; /* <tbl> MATCH ? expression (or NULL) */
214295 sqlite3_value *pRank = 0; /* rank MATCH ? expression (or NULL) */
214296 sqlite3_value *pRowidEq = 0; /* rowid = ? expression (or NULL) */
214297 sqlite3_value *pRowidLe = 0; /* rowid <= ? expression (or NULL) */
214298 sqlite3_value *pRowidGe = 0; /* rowid >= ? expression (or NULL) */
214299 int iCol; /* Column on LHS of MATCH operator */
214300 char **pzErrmsg = pConfig->pzErrmsg;
214301
214302 UNUSED_PARAM(zUnused);
214303 UNUSED_PARAM(nVal);
214304
214305 if( pCsr->ePlan ){
214306 fts5FreeCursorComponents(pCsr);
214307 memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan-(u8*)pCsr));
214308 }
214309
214310 assert( pCsr->pStmt==0 );
214311 assert( pCsr->pExpr==0 );
214312 assert( pCsr->csrflags==0 );
214313 assert( pCsr->pRank==0 );
214314 assert( pCsr->zRank==0 );
214315 assert( pCsr->zRankArgs==0 );
214316
214317 assert( pzErrmsg==0 || pzErrmsg==&pTab->base.zErrMsg );
214318 pConfig->pzErrmsg = &pTab->base.zErrMsg;
214319
214320 /* Decode the arguments passed through to this function.
214321 **
214322 ** Note: The following set of if(...) statements must be in the same
214323 ** order as the corresponding entries in the struct at the top of
214324 ** fts5BestIndexMethod(). */
214325 if( BitFlagTest(idxNum, FTS5_BI_MATCH) ) pMatch = apVal[iVal++];
214326 if( BitFlagTest(idxNum, FTS5_BI_RANK) ) pRank = apVal[iVal++];
214327 if( BitFlagTest(idxNum, FTS5_BI_ROWID_EQ) ) pRowidEq = apVal[iVal++];
214328 if( BitFlagTest(idxNum, FTS5_BI_ROWID_LE) ) pRowidLe = apVal[iVal++];
214329 if( BitFlagTest(idxNum, FTS5_BI_ROWID_GE) ) pRowidGe = apVal[iVal++];
214330 iCol = (idxNum>>16);
214331 assert( iCol>=0 && iCol<=pConfig->nCol );
214332 assert( iVal==nVal );
214333 bOrderByRank = ((idxNum & FTS5_BI_ORDER_RANK) ? 1 : 0);
214334 pCsr->bDesc = bDesc = ((idxNum & FTS5_BI_ORDER_DESC) ? 1 : 0);
214335
214336 /* Set the cursor upper and lower rowid limits. Only some strategies
214337 ** actually use them. This is ok, as the xBestIndex() method leaves the
214338 ** sqlite3_index_constraint.omit flag clear for range constraints
214339 ** on the rowid field. */
214340 if( pRowidEq ){
214341 pRowidLe = pRowidGe = pRowidEq;
214342 }
214343 if( bDesc ){
214344 pCsr->iFirstRowid = fts5GetRowidLimit(pRowidLe, LARGEST_INT64);
214345 pCsr->iLastRowid = fts5GetRowidLimit(pRowidGe, SMALLEST_INT64);
214346 }else{
214347 pCsr->iLastRowid = fts5GetRowidLimit(pRowidLe, LARGEST_INT64);
214348 pCsr->iFirstRowid = fts5GetRowidLimit(pRowidGe, SMALLEST_INT64);
214349 }
214350
214351 if( pTab->pSortCsr ){
214352 /* If pSortCsr is non-NULL, then this call is being made as part of
214353 ** processing for a "... MATCH <expr> ORDER BY rank" query (ePlan is
214354 ** set to FTS5_PLAN_SORTED_MATCH). pSortCsr is the cursor that will
214355 ** return results to the user for this query. The current cursor
214356 ** (pCursor) is used to execute the query issued by function
214357 ** fts5CursorFirstSorted() above. */
214358 assert( pRowidEq==0 && pRowidLe==0 && pRowidGe==0 && pRank==0 );
214359 assert( nVal==0 && pMatch==0 && bOrderByRank==0 && bDesc==0 );
214360 assert( pCsr->iLastRowid==LARGEST_INT64 );
214361 assert( pCsr->iFirstRowid==SMALLEST_INT64 );
214362 if( pTab->pSortCsr->bDesc ){
214363 pCsr->iLastRowid = pTab->pSortCsr->iFirstRowid;
214364 pCsr->iFirstRowid = pTab->pSortCsr->iLastRowid;
214365 }else{
214366 pCsr->iLastRowid = pTab->pSortCsr->iLastRowid;
214367 pCsr->iFirstRowid = pTab->pSortCsr->iFirstRowid;
214368 }
214369 pCsr->ePlan = FTS5_PLAN_SOURCE;
214370 pCsr->pExpr = pTab->pSortCsr->pExpr;
214371 rc = fts5CursorFirst(pTab, pCsr, bDesc);
214372 }else if( pMatch ){
214373 const char *zExpr = (const char*)sqlite3_value_text(apVal[0]);
214374 if( zExpr==0 ) zExpr = "";
214375
214376 rc = fts5CursorParseRank(pConfig, pCsr, pRank);
214377 if( rc==SQLITE_OK ){
214378 if( zExpr[0]=='*' ){
214379 /* The user has issued a query of the form "MATCH '*...'". This
214380 ** indicates that the MATCH expression is not a full text query,
214381 ** but a request for an internal parameter. */
214382 rc = fts5SpecialMatch(pTab, pCsr, &zExpr[1]);
214383 }else{
214384 char **pzErr = &pTab->base.zErrMsg;
214385 rc = sqlite3Fts5ExprNew(pConfig, iCol, zExpr, &pCsr->pExpr, pzErr);
214386 if( rc==SQLITE_OK ){
214387 if( bOrderByRank ){
214388 pCsr->ePlan = FTS5_PLAN_SORTED_MATCH;
214389 rc = fts5CursorFirstSorted(pTab, pCsr, bDesc);
214390 }else{
214391 pCsr->ePlan = FTS5_PLAN_MATCH;
214392 rc = fts5CursorFirst(pTab, pCsr, bDesc);
214393 }
214394 }
214395 }
214396 }
214397 }else if( pConfig->zContent==0 ){
214398 *pConfig->pzErrmsg = sqlite3_mprintf(
214399 "%s: table does not support scanning", pConfig->zName
214400 );
214401 rc = SQLITE_ERROR;
214402 }else{
214403 /* This is either a full-table scan (ePlan==FTS5_PLAN_SCAN) or a lookup
214404 ** by rowid (ePlan==FTS5_PLAN_ROWID). */
214405 pCsr->ePlan = (pRowidEq ? FTS5_PLAN_ROWID : FTS5_PLAN_SCAN);
214406 rc = sqlite3Fts5StorageStmt(
214407 pTab->pStorage, fts5StmtType(pCsr), &pCsr->pStmt, &pTab->base.zErrMsg
214408 );
214409 if( rc==SQLITE_OK ){
214410 if( pCsr->ePlan==FTS5_PLAN_ROWID ){
214411 sqlite3_bind_value(pCsr->pStmt, 1, apVal[0]);
214412 }else{
214413 sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iFirstRowid);
214414 sqlite3_bind_int64(pCsr->pStmt, 2, pCsr->iLastRowid);
214415 }
214416 rc = fts5NextMethod(pCursor);
214417 }
214418 }
214419
214420 pConfig->pzErrmsg = pzErrmsg;
214421 return rc;
214422}
214423
214424/*
214425** This is the xEof method of the virtual table. SQLite calls this
214426** routine to find out if it has reached the end of a result set.
214427*/
214428static int fts5EofMethod(sqlite3_vtab_cursor *pCursor){
214429 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
214430 return (CsrFlagTest(pCsr, FTS5CSR_EOF) ? 1 : 0);
214431}
214432
214433/*
214434** Return the rowid that the cursor currently points to.
214435*/
214436static i64 fts5CursorRowid(Fts5Cursor *pCsr){
214437 assert( pCsr->ePlan==FTS5_PLAN_MATCH
214438 || pCsr->ePlan==FTS5_PLAN_SORTED_MATCH
214439 || pCsr->ePlan==FTS5_PLAN_SOURCE
214440 );
214441 if( pCsr->pSorter ){
214442 return pCsr->pSorter->iRowid;
214443 }else{
214444 return sqlite3Fts5ExprRowid(pCsr->pExpr);
214445 }
214446}
214447
214448/*
214449** This is the xRowid method. The SQLite core calls this routine to
214450** retrieve the rowid for the current row of the result set. fts5
214451** exposes %_content.rowid as the rowid for the virtual table. The
214452** rowid should be written to *pRowid.
214453*/
214454static int fts5RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
214455 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
214456 int ePlan = pCsr->ePlan;
214457
214458 assert( CsrFlagTest(pCsr, FTS5CSR_EOF)==0 );
214459 switch( ePlan ){
214460 case FTS5_PLAN_SPECIAL:
214461 *pRowid = 0;
214462 break;
214463
214464 case FTS5_PLAN_SOURCE:
214465 case FTS5_PLAN_MATCH:
214466 case FTS5_PLAN_SORTED_MATCH:
214467 *pRowid = fts5CursorRowid(pCsr);
214468 break;
214469
214470 default:
214471 *pRowid = sqlite3_column_int64(pCsr->pStmt, 0);
214472 break;
214473 }
214474
214475 return SQLITE_OK;
214476}
214477
214478/*
214479** If the cursor requires seeking (bSeekRequired flag is set), seek it.
214480** Return SQLITE_OK if no error occurs, or an SQLite error code otherwise.
214481**
214482** If argument bErrormsg is true and an error occurs, an error message may
214483** be left in sqlite3_vtab.zErrMsg.
214484*/
214485static int fts5SeekCursor(Fts5Cursor *pCsr, int bErrormsg){
214486 int rc = SQLITE_OK;
214487
214488 /* If the cursor does not yet have a statement handle, obtain one now. */
214489 if( pCsr->pStmt==0 ){
214490 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
214491 int eStmt = fts5StmtType(pCsr);
214492 rc = sqlite3Fts5StorageStmt(
214493 pTab->pStorage, eStmt, &pCsr->pStmt, (bErrormsg?&pTab->base.zErrMsg:0)
214494 );
214495 assert( rc!=SQLITE_OK || pTab->base.zErrMsg==0 );
214496 assert( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_CONTENT) );
214497 }
214498
214499 if( rc==SQLITE_OK && CsrFlagTest(pCsr, FTS5CSR_REQUIRE_CONTENT) ){
214500 assert( pCsr->pExpr );
214501 sqlite3_reset(pCsr->pStmt);
214502 sqlite3_bind_int64(pCsr->pStmt, 1, fts5CursorRowid(pCsr));
214503 rc = sqlite3_step(pCsr->pStmt);
214504 if( rc==SQLITE_ROW ){
214505 rc = SQLITE_OK;
214506 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_CONTENT);
214507 }else{
214508 rc = sqlite3_reset(pCsr->pStmt);
214509 if( rc==SQLITE_OK ){
214510 rc = FTS5_CORRUPT;
214511 }
214512 }
214513 }
214514 return rc;
214515}
214516
214517static void fts5SetVtabError(Fts5Table *p, const char *zFormat, ...){
214518 va_list ap; /* ... printf arguments */
214519 va_start(ap, zFormat);
214520 assert( p->base.zErrMsg==0 );
214521 p->base.zErrMsg = sqlite3_vmprintf(zFormat, ap);
214522 va_end(ap);
214523}
214524
214525/*
214526** This function is called to handle an FTS INSERT command. In other words,
214527** an INSERT statement of the form:
214528**
214529** INSERT INTO fts(fts) VALUES($pCmd)
214530** INSERT INTO fts(fts, rank) VALUES($pCmd, $pVal)
214531**
214532** Argument pVal is the value assigned to column "fts" by the INSERT
214533** statement. This function returns SQLITE_OK if successful, or an SQLite
214534** error code if an error occurs.
214535**
214536** The commands implemented by this function are documented in the "Special
214537** INSERT Directives" section of the documentation. It should be updated if
214538** more commands are added to this function.
214539*/
214540static int fts5SpecialInsert(
214541 Fts5Table *pTab, /* Fts5 table object */
214542 const char *zCmd, /* Text inserted into table-name column */
214543 sqlite3_value *pVal /* Value inserted into rank column */
214544){
214545 Fts5Config *pConfig = pTab->pConfig;
214546 int rc = SQLITE_OK;
214547 int bError = 0;
214548
214549 if( 0==sqlite3_stricmp("delete-all", zCmd) ){
214550 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
214551 fts5SetVtabError(pTab,
214552 "'delete-all' may only be used with a "
214553 "contentless or external content fts5 table"
214554 );
214555 rc = SQLITE_ERROR;
214556 }else{
214557 rc = sqlite3Fts5StorageDeleteAll(pTab->pStorage);
214558 }
214559 }else if( 0==sqlite3_stricmp("rebuild", zCmd) ){
214560 if( pConfig->eContent==FTS5_CONTENT_NONE ){
214561 fts5SetVtabError(pTab,
214562 "'rebuild' may not be used with a contentless fts5 table"
214563 );
214564 rc = SQLITE_ERROR;
214565 }else{
214566 rc = sqlite3Fts5StorageRebuild(pTab->pStorage);
214567 }
214568 }else if( 0==sqlite3_stricmp("optimize", zCmd) ){
214569 rc = sqlite3Fts5StorageOptimize(pTab->pStorage);
214570 }else if( 0==sqlite3_stricmp("merge", zCmd) ){
214571 int nMerge = sqlite3_value_int(pVal);
214572 rc = sqlite3Fts5StorageMerge(pTab->pStorage, nMerge);
214573 }else if( 0==sqlite3_stricmp("integrity-check", zCmd) ){
214574 rc = sqlite3Fts5StorageIntegrity(pTab->pStorage);
214575#ifdef SQLITE_DEBUG
214576 }else if( 0==sqlite3_stricmp("prefix-index", zCmd) ){
214577 pConfig->bPrefixIndex = sqlite3_value_int(pVal);
214578#endif
214579 }else{
214580 rc = sqlite3Fts5IndexLoadConfig(pTab->pIndex);
214581 if( rc==SQLITE_OK ){
214582 rc = sqlite3Fts5ConfigSetValue(pTab->pConfig, zCmd, pVal, &bError);
214583 }
214584 if( rc==SQLITE_OK ){
214585 if( bError ){
214586 rc = SQLITE_ERROR;
214587 }else{
214588 rc = sqlite3Fts5StorageConfigValue(pTab->pStorage, zCmd, pVal, 0);
214589 }
214590 }
214591 }
214592 return rc;
214593}
214594
214595static int fts5SpecialDelete(
214596 Fts5Table *pTab,
214597 sqlite3_value **apVal
214598){
214599 int rc = SQLITE_OK;
214600 int eType1 = sqlite3_value_type(apVal[1]);
214601 if( eType1==SQLITE_INTEGER ){
214602 sqlite3_int64 iDel = sqlite3_value_int64(apVal[1]);
214603 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iDel, &apVal[2]);
214604 }
214605 return rc;
214606}
214607
214608static void fts5StorageInsert(
214609 int *pRc,
214610 Fts5Table *pTab,
214611 sqlite3_value **apVal,
214612 i64 *piRowid
214613){
214614 int rc = *pRc;
214615 if( rc==SQLITE_OK ){
214616 rc = sqlite3Fts5StorageContentInsert(pTab->pStorage, apVal, piRowid);
214617 }
214618 if( rc==SQLITE_OK ){
214619 rc = sqlite3Fts5StorageIndexInsert(pTab->pStorage, apVal, *piRowid);
214620 }
214621 *pRc = rc;
214622}
214623
214624/*
214625** This function is the implementation of the xUpdate callback used by
214626** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
214627** inserted, updated or deleted.
214628**
214629** A delete specifies a single argument - the rowid of the row to remove.
214630**
214631** Update and insert operations pass:
214632**
214633** 1. The "old" rowid, or NULL.
214634** 2. The "new" rowid.
214635** 3. Values for each of the nCol matchable columns.
214636** 4. Values for the two hidden columns (<tablename> and "rank").
214637*/
214638static int fts5UpdateMethod(
214639 sqlite3_vtab *pVtab, /* Virtual table handle */
214640 int nArg, /* Size of argument array */
214641 sqlite3_value **apVal, /* Array of arguments */
214642 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
214643){
214644 Fts5Table *pTab = (Fts5Table*)pVtab;
214645 Fts5Config *pConfig = pTab->pConfig;
214646 int eType0; /* value_type() of apVal[0] */
214647 int rc = SQLITE_OK; /* Return code */
214648
214649 /* A transaction must be open when this is called. */
214650 assert( pTab->ts.eState==1 );
214651
214652 assert( pVtab->zErrMsg==0 );
214653 assert( nArg==1 || nArg==(2+pConfig->nCol+2) );
214654 assert( nArg==1
214655 || sqlite3_value_type(apVal[1])==SQLITE_INTEGER
214656 || sqlite3_value_type(apVal[1])==SQLITE_NULL
214657 );
214658 assert( pTab->pConfig->pzErrmsg==0 );
214659 pTab->pConfig->pzErrmsg = &pTab->base.zErrMsg;
214660
214661 /* Put any active cursors into REQUIRE_SEEK state. */
214662 fts5TripCursors(pTab);
214663
214664 eType0 = sqlite3_value_type(apVal[0]);
214665 if( eType0==SQLITE_NULL
214666 && sqlite3_value_type(apVal[2+pConfig->nCol])!=SQLITE_NULL
214667 ){
214668 /* A "special" INSERT op. These are handled separately. */
214669 const char *z = (const char*)sqlite3_value_text(apVal[2+pConfig->nCol]);
214670 if( pConfig->eContent!=FTS5_CONTENT_NORMAL
214671 && 0==sqlite3_stricmp("delete", z)
214672 ){
214673 rc = fts5SpecialDelete(pTab, apVal);
214674 }else{
214675 rc = fts5SpecialInsert(pTab, z, apVal[2 + pConfig->nCol + 1]);
214676 }
214677 }else{
214678 /* A regular INSERT, UPDATE or DELETE statement. The trick here is that
214679 ** any conflict on the rowid value must be detected before any
214680 ** modifications are made to the database file. There are 4 cases:
214681 **
214682 ** 1) DELETE
214683 ** 2) UPDATE (rowid not modified)
214684 ** 3) UPDATE (rowid modified)
214685 ** 4) INSERT
214686 **
214687 ** Cases 3 and 4 may violate the rowid constraint.
214688 */
214689 int eConflict = SQLITE_ABORT;
214690 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
214691 eConflict = sqlite3_vtab_on_conflict(pConfig->db);
214692 }
214693
214694 assert( eType0==SQLITE_INTEGER || eType0==SQLITE_NULL );
214695 assert( nArg!=1 || eType0==SQLITE_INTEGER );
214696
214697 /* Filter out attempts to run UPDATE or DELETE on contentless tables.
214698 ** This is not suported. */
214699 if( eType0==SQLITE_INTEGER && fts5IsContentless(pTab) ){
214700 pTab->base.zErrMsg = sqlite3_mprintf(
214701 "cannot %s contentless fts5 table: %s",
214702 (nArg>1 ? "UPDATE" : "DELETE from"), pConfig->zName
214703 );
214704 rc = SQLITE_ERROR;
214705 }
214706
214707 /* DELETE */
214708 else if( nArg==1 ){
214709 i64 iDel = sqlite3_value_int64(apVal[0]); /* Rowid to delete */
214710 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iDel, 0);
214711 }
214712
214713 /* INSERT */
214714 else if( eType0!=SQLITE_INTEGER ){
214715 /* If this is a REPLACE, first remove the current entry (if any) */
214716 if( eConflict==SQLITE_REPLACE
214717 && sqlite3_value_type(apVal[1])==SQLITE_INTEGER
214718 ){
214719 i64 iNew = sqlite3_value_int64(apVal[1]); /* Rowid to delete */
214720 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iNew, 0);
214721 }
214722 fts5StorageInsert(&rc, pTab, apVal, pRowid);
214723 }
214724
214725 /* UPDATE */
214726 else{
214727 i64 iOld = sqlite3_value_int64(apVal[0]); /* Old rowid */
214728 i64 iNew = sqlite3_value_int64(apVal[1]); /* New rowid */
214729 if( iOld!=iNew ){
214730 if( eConflict==SQLITE_REPLACE ){
214731 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0);
214732 if( rc==SQLITE_OK ){
214733 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iNew, 0);
214734 }
214735 fts5StorageInsert(&rc, pTab, apVal, pRowid);
214736 }else{
214737 rc = sqlite3Fts5StorageContentInsert(pTab->pStorage, apVal, pRowid);
214738 if( rc==SQLITE_OK ){
214739 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0);
214740 }
214741 if( rc==SQLITE_OK ){
214742 rc = sqlite3Fts5StorageIndexInsert(pTab->pStorage, apVal, *pRowid);
214743 }
214744 }
214745 }else{
214746 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0);
214747 fts5StorageInsert(&rc, pTab, apVal, pRowid);
214748 }
214749 }
214750 }
214751
214752 pTab->pConfig->pzErrmsg = 0;
214753 return rc;
214754}
214755
214756/*
214757** Implementation of xSync() method.
214758*/
214759static int fts5SyncMethod(sqlite3_vtab *pVtab){
214760 int rc;
214761 Fts5Table *pTab = (Fts5Table*)pVtab;
214762 fts5CheckTransactionState(pTab, FTS5_SYNC, 0);
214763 pTab->pConfig->pzErrmsg = &pTab->base.zErrMsg;
214764 fts5TripCursors(pTab);
214765 rc = sqlite3Fts5StorageSync(pTab->pStorage);
214766 pTab->pConfig->pzErrmsg = 0;
214767 return rc;
214768}
214769
214770/*
214771** Implementation of xBegin() method.
214772*/
214773static int fts5BeginMethod(sqlite3_vtab *pVtab){
214774 fts5CheckTransactionState((Fts5Table*)pVtab, FTS5_BEGIN, 0);
214775 fts5NewTransaction((Fts5Table*)pVtab);
214776 return SQLITE_OK;
214777}
214778
214779/*
214780** Implementation of xCommit() method. This is a no-op. The contents of
214781** the pending-terms hash-table have already been flushed into the database
214782** by fts5SyncMethod().
214783*/
214784static int fts5CommitMethod(sqlite3_vtab *pVtab){
214785 UNUSED_PARAM(pVtab); /* Call below is a no-op for NDEBUG builds */
214786 fts5CheckTransactionState((Fts5Table*)pVtab, FTS5_COMMIT, 0);
214787 return SQLITE_OK;
214788}
214789
214790/*
214791** Implementation of xRollback(). Discard the contents of the pending-terms
214792** hash-table. Any changes made to the database are reverted by SQLite.
214793*/
214794static int fts5RollbackMethod(sqlite3_vtab *pVtab){
214795 int rc;
214796 Fts5Table *pTab = (Fts5Table*)pVtab;
214797 fts5CheckTransactionState(pTab, FTS5_ROLLBACK, 0);
214798 rc = sqlite3Fts5StorageRollback(pTab->pStorage);
214799 return rc;
214800}
214801
214802static int fts5CsrPoslist(Fts5Cursor*, int, const u8**, int*);
214803
214804static void *fts5ApiUserData(Fts5Context *pCtx){
214805 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
214806 return pCsr->pAux->pUserData;
214807}
214808
214809static int fts5ApiColumnCount(Fts5Context *pCtx){
214810 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
214811 return ((Fts5Table*)(pCsr->base.pVtab))->pConfig->nCol;
214812}
214813
214814static int fts5ApiColumnTotalSize(
214815 Fts5Context *pCtx,
214816 int iCol,
214817 sqlite3_int64 *pnToken
214818){
214819 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
214820 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
214821 return sqlite3Fts5StorageSize(pTab->pStorage, iCol, pnToken);
214822}
214823
214824static int fts5ApiRowCount(Fts5Context *pCtx, i64 *pnRow){
214825 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
214826 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
214827 return sqlite3Fts5StorageRowCount(pTab->pStorage, pnRow);
214828}
214829
214830static int fts5ApiTokenize(
214831 Fts5Context *pCtx,
214832 const char *pText, int nText,
214833 void *pUserData,
214834 int (*xToken)(void*, int, const char*, int, int, int)
214835){
214836 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
214837 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
214838 return sqlite3Fts5Tokenize(
214839 pTab->pConfig, FTS5_TOKENIZE_AUX, pText, nText, pUserData, xToken
214840 );
214841}
214842
214843static int fts5ApiPhraseCount(Fts5Context *pCtx){
214844 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
214845 return sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
214846}
214847
214848static int fts5ApiPhraseSize(Fts5Context *pCtx, int iPhrase){
214849 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
214850 return sqlite3Fts5ExprPhraseSize(pCsr->pExpr, iPhrase);
214851}
214852
214853static int fts5ApiColumnText(
214854 Fts5Context *pCtx,
214855 int iCol,
214856 const char **pz,
214857 int *pn
214858){
214859 int rc = SQLITE_OK;
214860 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
214861 if( fts5IsContentless((Fts5Table*)(pCsr->base.pVtab)) ){
214862 *pz = 0;
214863 *pn = 0;
214864 }else{
214865 rc = fts5SeekCursor(pCsr, 0);
214866 if( rc==SQLITE_OK ){
214867 *pz = (const char*)sqlite3_column_text(pCsr->pStmt, iCol+1);
214868 *pn = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
214869 }
214870 }
214871 return rc;
214872}
214873
214874static int fts5CsrPoslist(
214875 Fts5Cursor *pCsr,
214876 int iPhrase,
214877 const u8 **pa,
214878 int *pn
214879){
214880 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
214881 int rc = SQLITE_OK;
214882 int bLive = (pCsr->pSorter==0);
214883
214884 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_POSLIST) ){
214885
214886 if( pConfig->eDetail!=FTS5_DETAIL_FULL ){
214887 Fts5PoslistPopulator *aPopulator;
214888 int i;
214889 aPopulator = sqlite3Fts5ExprClearPoslists(pCsr->pExpr, bLive);
214890 if( aPopulator==0 ) rc = SQLITE_NOMEM;
214891 for(i=0; i<pConfig->nCol && rc==SQLITE_OK; i++){
214892 int n; const char *z;
214893 rc = fts5ApiColumnText((Fts5Context*)pCsr, i, &z, &n);
214894 if( rc==SQLITE_OK ){
214895 rc = sqlite3Fts5ExprPopulatePoslists(
214896 pConfig, pCsr->pExpr, aPopulator, i, z, n
214897 );
214898 }
214899 }
214900 sqlite3_free(aPopulator);
214901
214902 if( pCsr->pSorter ){
214903 sqlite3Fts5ExprCheckPoslists(pCsr->pExpr, pCsr->pSorter->iRowid);
214904 }
214905 }
214906 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_POSLIST);
214907 }
214908
214909 if( pCsr->pSorter && pConfig->eDetail==FTS5_DETAIL_FULL ){
214910 Fts5Sorter *pSorter = pCsr->pSorter;
214911 int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
214912 *pn = pSorter->aIdx[iPhrase] - i1;
214913 *pa = &pSorter->aPoslist[i1];
214914 }else{
214915 *pn = sqlite3Fts5ExprPoslist(pCsr->pExpr, iPhrase, pa);
214916 }
214917
214918 return rc;
214919}
214920
214921/*
214922** Ensure that the Fts5Cursor.nInstCount and aInst[] variables are populated
214923** correctly for the current view. Return SQLITE_OK if successful, or an
214924** SQLite error code otherwise.
214925*/
214926static int fts5CacheInstArray(Fts5Cursor *pCsr){
214927 int rc = SQLITE_OK;
214928 Fts5PoslistReader *aIter; /* One iterator for each phrase */
214929 int nIter; /* Number of iterators/phrases */
214930
214931 nIter = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
214932 if( pCsr->aInstIter==0 ){
214933 int nByte = sizeof(Fts5PoslistReader) * nIter;
214934 pCsr->aInstIter = (Fts5PoslistReader*)sqlite3Fts5MallocZero(&rc, nByte);
214935 }
214936 aIter = pCsr->aInstIter;
214937
214938 if( aIter ){
214939 int nInst = 0; /* Number instances seen so far */
214940 int i;
214941
214942 /* Initialize all iterators */
214943 for(i=0; i<nIter && rc==SQLITE_OK; i++){
214944 const u8 *a;
214945 int n;
214946 rc = fts5CsrPoslist(pCsr, i, &a, &n);
214947 if( rc==SQLITE_OK ){
214948 sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]);
214949 }
214950 }
214951
214952 if( rc==SQLITE_OK ){
214953 while( 1 ){
214954 int *aInst;
214955 int iBest = -1;
214956 for(i=0; i<nIter; i++){
214957 if( (aIter[i].bEof==0)
214958 && (iBest<0 || aIter[i].iPos<aIter[iBest].iPos)
214959 ){
214960 iBest = i;
214961 }
214962 }
214963 if( iBest<0 ) break;
214964
214965 nInst++;
214966 if( nInst>=pCsr->nInstAlloc ){
214967 pCsr->nInstAlloc = pCsr->nInstAlloc ? pCsr->nInstAlloc*2 : 32;
214968 aInst = (int*)sqlite3_realloc(
214969 pCsr->aInst, pCsr->nInstAlloc*sizeof(int)*3
214970 );
214971 if( aInst ){
214972 pCsr->aInst = aInst;
214973 }else{
214974 rc = SQLITE_NOMEM;
214975 break;
214976 }
214977 }
214978
214979 aInst = &pCsr->aInst[3 * (nInst-1)];
214980 aInst[0] = iBest;
214981 aInst[1] = FTS5_POS2COLUMN(aIter[iBest].iPos);
214982 aInst[2] = FTS5_POS2OFFSET(aIter[iBest].iPos);
214983 sqlite3Fts5PoslistReaderNext(&aIter[iBest]);
214984 }
214985 }
214986
214987 pCsr->nInstCount = nInst;
214988 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_INST);
214989 }
214990 return rc;
214991}
214992
214993static int fts5ApiInstCount(Fts5Context *pCtx, int *pnInst){
214994 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
214995 int rc = SQLITE_OK;
214996 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_INST)==0
214997 || SQLITE_OK==(rc = fts5CacheInstArray(pCsr)) ){
214998 *pnInst = pCsr->nInstCount;
214999 }
215000 return rc;
215001}
215002
215003static int fts5ApiInst(
215004 Fts5Context *pCtx,
215005 int iIdx,
215006 int *piPhrase,
215007 int *piCol,
215008 int *piOff
215009){
215010 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
215011 int rc = SQLITE_OK;
215012 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_INST)==0
215013 || SQLITE_OK==(rc = fts5CacheInstArray(pCsr))
215014 ){
215015 if( iIdx<0 || iIdx>=pCsr->nInstCount ){
215016 rc = SQLITE_RANGE;
215017#if 0
215018 }else if( fts5IsOffsetless((Fts5Table*)pCsr->base.pVtab) ){
215019 *piPhrase = pCsr->aInst[iIdx*3];
215020 *piCol = pCsr->aInst[iIdx*3 + 2];
215021 *piOff = -1;
215022#endif
215023 }else{
215024 *piPhrase = pCsr->aInst[iIdx*3];
215025 *piCol = pCsr->aInst[iIdx*3 + 1];
215026 *piOff = pCsr->aInst[iIdx*3 + 2];
215027 }
215028 }
215029 return rc;
215030}
215031
215032static sqlite3_int64 fts5ApiRowid(Fts5Context *pCtx){
215033 return fts5CursorRowid((Fts5Cursor*)pCtx);
215034}
215035
215036static int fts5ColumnSizeCb(
215037 void *pContext, /* Pointer to int */
215038 int tflags,
215039 const char *pUnused, /* Buffer containing token */
215040 int nUnused, /* Size of token in bytes */
215041 int iUnused1, /* Start offset of token */
215042 int iUnused2 /* End offset of token */
215043){
215044 int *pCnt = (int*)pContext;
215045 UNUSED_PARAM2(pUnused, nUnused);
215046 UNUSED_PARAM2(iUnused1, iUnused2);
215047 if( (tflags & FTS5_TOKEN_COLOCATED)==0 ){
215048 (*pCnt)++;
215049 }
215050 return SQLITE_OK;
215051}
215052
215053static int fts5ApiColumnSize(Fts5Context *pCtx, int iCol, int *pnToken){
215054 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
215055 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
215056 Fts5Config *pConfig = pTab->pConfig;
215057 int rc = SQLITE_OK;
215058
215059 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_DOCSIZE) ){
215060 if( pConfig->bColumnsize ){
215061 i64 iRowid = fts5CursorRowid(pCsr);
215062 rc = sqlite3Fts5StorageDocsize(pTab->pStorage, iRowid, pCsr->aColumnSize);
215063 }else if( pConfig->zContent==0 ){
215064 int i;
215065 for(i=0; i<pConfig->nCol; i++){
215066 if( pConfig->abUnindexed[i]==0 ){
215067 pCsr->aColumnSize[i] = -1;
215068 }
215069 }
215070 }else{
215071 int i;
215072 for(i=0; rc==SQLITE_OK && i<pConfig->nCol; i++){
215073 if( pConfig->abUnindexed[i]==0 ){
215074 const char *z; int n;
215075 void *p = (void*)(&pCsr->aColumnSize[i]);
215076 pCsr->aColumnSize[i] = 0;
215077 rc = fts5ApiColumnText(pCtx, i, &z, &n);
215078 if( rc==SQLITE_OK ){
215079 rc = sqlite3Fts5Tokenize(
215080 pConfig, FTS5_TOKENIZE_AUX, z, n, p, fts5ColumnSizeCb
215081 );
215082 }
215083 }
215084 }
215085 }
215086 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_DOCSIZE);
215087 }
215088 if( iCol<0 ){
215089 int i;
215090 *pnToken = 0;
215091 for(i=0; i<pConfig->nCol; i++){
215092 *pnToken += pCsr->aColumnSize[i];
215093 }
215094 }else if( iCol<pConfig->nCol ){
215095 *pnToken = pCsr->aColumnSize[iCol];
215096 }else{
215097 *pnToken = 0;
215098 rc = SQLITE_RANGE;
215099 }
215100 return rc;
215101}
215102
215103/*
215104** Implementation of the xSetAuxdata() method.
215105*/
215106static int fts5ApiSetAuxdata(
215107 Fts5Context *pCtx, /* Fts5 context */
215108 void *pPtr, /* Pointer to save as auxdata */
215109 void(*xDelete)(void*) /* Destructor for pPtr (or NULL) */
215110){
215111 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
215112 Fts5Auxdata *pData;
215113
215114 /* Search through the cursors list of Fts5Auxdata objects for one that
215115 ** corresponds to the currently executing auxiliary function. */
215116 for(pData=pCsr->pAuxdata; pData; pData=pData->pNext){
215117 if( pData->pAux==pCsr->pAux ) break;
215118 }
215119
215120 if( pData ){
215121 if( pData->xDelete ){
215122 pData->xDelete(pData->pPtr);
215123 }
215124 }else{
215125 int rc = SQLITE_OK;
215126 pData = (Fts5Auxdata*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Auxdata));
215127 if( pData==0 ){
215128 if( xDelete ) xDelete(pPtr);
215129 return rc;
215130 }
215131 pData->pAux = pCsr->pAux;
215132 pData->pNext = pCsr->pAuxdata;
215133 pCsr->pAuxdata = pData;
215134 }
215135
215136 pData->xDelete = xDelete;
215137 pData->pPtr = pPtr;
215138 return SQLITE_OK;
215139}
215140
215141static void *fts5ApiGetAuxdata(Fts5Context *pCtx, int bClear){
215142 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
215143 Fts5Auxdata *pData;
215144 void *pRet = 0;
215145
215146 for(pData=pCsr->pAuxdata; pData; pData=pData->pNext){
215147 if( pData->pAux==pCsr->pAux ) break;
215148 }
215149
215150 if( pData ){
215151 pRet = pData->pPtr;
215152 if( bClear ){
215153 pData->pPtr = 0;
215154 pData->xDelete = 0;
215155 }
215156 }
215157
215158 return pRet;
215159}
215160
215161static void fts5ApiPhraseNext(
215162 Fts5Context *pUnused,
215163 Fts5PhraseIter *pIter,
215164 int *piCol, int *piOff
215165){
215166 UNUSED_PARAM(pUnused);
215167 if( pIter->a>=pIter->b ){
215168 *piCol = -1;
215169 *piOff = -1;
215170 }else{
215171 int iVal;
215172 pIter->a += fts5GetVarint32(pIter->a, iVal);
215173 if( iVal==1 ){
215174 pIter->a += fts5GetVarint32(pIter->a, iVal);
215175 *piCol = iVal;
215176 *piOff = 0;
215177 pIter->a += fts5GetVarint32(pIter->a, iVal);
215178 }
215179 *piOff += (iVal-2);
215180 }
215181}
215182
215183static int fts5ApiPhraseFirst(
215184 Fts5Context *pCtx,
215185 int iPhrase,
215186 Fts5PhraseIter *pIter,
215187 int *piCol, int *piOff
215188){
215189 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
215190 int n;
215191 int rc = fts5CsrPoslist(pCsr, iPhrase, &pIter->a, &n);
215192 if( rc==SQLITE_OK ){
215193 pIter->b = &pIter->a[n];
215194 *piCol = 0;
215195 *piOff = 0;
215196 fts5ApiPhraseNext(pCtx, pIter, piCol, piOff);
215197 }
215198 return rc;
215199}
215200
215201static void fts5ApiPhraseNextColumn(
215202 Fts5Context *pCtx,
215203 Fts5PhraseIter *pIter,
215204 int *piCol
215205){
215206 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
215207 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
215208
215209 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
215210 if( pIter->a>=pIter->b ){
215211 *piCol = -1;
215212 }else{
215213 int iIncr;
215214 pIter->a += fts5GetVarint32(&pIter->a[0], iIncr);
215215 *piCol += (iIncr-2);
215216 }
215217 }else{
215218 while( 1 ){
215219 int dummy;
215220 if( pIter->a>=pIter->b ){
215221 *piCol = -1;
215222 return;
215223 }
215224 if( pIter->a[0]==0x01 ) break;
215225 pIter->a += fts5GetVarint32(pIter->a, dummy);
215226 }
215227 pIter->a += 1 + fts5GetVarint32(&pIter->a[1], *piCol);
215228 }
215229}
215230
215231static int fts5ApiPhraseFirstColumn(
215232 Fts5Context *pCtx,
215233 int iPhrase,
215234 Fts5PhraseIter *pIter,
215235 int *piCol
215236){
215237 int rc = SQLITE_OK;
215238 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
215239 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
215240
215241 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
215242 Fts5Sorter *pSorter = pCsr->pSorter;
215243 int n;
215244 if( pSorter ){
215245 int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
215246 n = pSorter->aIdx[iPhrase] - i1;
215247 pIter->a = &pSorter->aPoslist[i1];
215248 }else{
215249 rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, iPhrase, &pIter->a, &n);
215250 }
215251 if( rc==SQLITE_OK ){
215252 pIter->b = &pIter->a[n];
215253 *piCol = 0;
215254 fts5ApiPhraseNextColumn(pCtx, pIter, piCol);
215255 }
215256 }else{
215257 int n;
215258 rc = fts5CsrPoslist(pCsr, iPhrase, &pIter->a, &n);
215259 if( rc==SQLITE_OK ){
215260 pIter->b = &pIter->a[n];
215261 if( n<=0 ){
215262 *piCol = -1;
215263 }else if( pIter->a[0]==0x01 ){
215264 pIter->a += 1 + fts5GetVarint32(&pIter->a[1], *piCol);
215265 }else{
215266 *piCol = 0;
215267 }
215268 }
215269 }
215270
215271 return rc;
215272}
215273
215274
215275static int fts5ApiQueryPhrase(Fts5Context*, int, void*,
215276 int(*)(const Fts5ExtensionApi*, Fts5Context*, void*)
215277);
215278
215279static const Fts5ExtensionApi sFts5Api = {
215280 2, /* iVersion */
215281 fts5ApiUserData,
215282 fts5ApiColumnCount,
215283 fts5ApiRowCount,
215284 fts5ApiColumnTotalSize,
215285 fts5ApiTokenize,
215286 fts5ApiPhraseCount,
215287 fts5ApiPhraseSize,
215288 fts5ApiInstCount,
215289 fts5ApiInst,
215290 fts5ApiRowid,
215291 fts5ApiColumnText,
215292 fts5ApiColumnSize,
215293 fts5ApiQueryPhrase,
215294 fts5ApiSetAuxdata,
215295 fts5ApiGetAuxdata,
215296 fts5ApiPhraseFirst,
215297 fts5ApiPhraseNext,
215298 fts5ApiPhraseFirstColumn,
215299 fts5ApiPhraseNextColumn,
215300};
215301
215302/*
215303** Implementation of API function xQueryPhrase().
215304*/
215305static int fts5ApiQueryPhrase(
215306 Fts5Context *pCtx,
215307 int iPhrase,
215308 void *pUserData,
215309 int(*xCallback)(const Fts5ExtensionApi*, Fts5Context*, void*)
215310){
215311 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
215312 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
215313 int rc;
215314 Fts5Cursor *pNew = 0;
215315
215316 rc = fts5OpenMethod(pCsr->base.pVtab, (sqlite3_vtab_cursor**)&pNew);
215317 if( rc==SQLITE_OK ){
215318 pNew->ePlan = FTS5_PLAN_MATCH;
215319 pNew->iFirstRowid = SMALLEST_INT64;
215320 pNew->iLastRowid = LARGEST_INT64;
215321 pNew->base.pVtab = (sqlite3_vtab*)pTab;
215322 rc = sqlite3Fts5ExprClonePhrase(pCsr->pExpr, iPhrase, &pNew->pExpr);
215323 }
215324
215325 if( rc==SQLITE_OK ){
215326 for(rc = fts5CursorFirst(pTab, pNew, 0);
215327 rc==SQLITE_OK && CsrFlagTest(pNew, FTS5CSR_EOF)==0;
215328 rc = fts5NextMethod((sqlite3_vtab_cursor*)pNew)
215329 ){
215330 rc = xCallback(&sFts5Api, (Fts5Context*)pNew, pUserData);
215331 if( rc!=SQLITE_OK ){
215332 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
215333 break;
215334 }
215335 }
215336 }
215337
215338 fts5CloseMethod((sqlite3_vtab_cursor*)pNew);
215339 return rc;
215340}
215341
215342static void fts5ApiInvoke(
215343 Fts5Auxiliary *pAux,
215344 Fts5Cursor *pCsr,
215345 sqlite3_context *context,
215346 int argc,
215347 sqlite3_value **argv
215348){
215349 assert( pCsr->pAux==0 );
215350 pCsr->pAux = pAux;
215351 pAux->xFunc(&sFts5Api, (Fts5Context*)pCsr, context, argc, argv);
215352 pCsr->pAux = 0;
215353}
215354
215355static Fts5Cursor *fts5CursorFromCsrid(Fts5Global *pGlobal, i64 iCsrId){
215356 Fts5Cursor *pCsr;
215357 for(pCsr=pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
215358 if( pCsr->iCsrId==iCsrId ) break;
215359 }
215360 return pCsr;
215361}
215362
215363static void fts5ApiCallback(
215364 sqlite3_context *context,
215365 int argc,
215366 sqlite3_value **argv
215367){
215368
215369 Fts5Auxiliary *pAux;
215370 Fts5Cursor *pCsr;
215371 i64 iCsrId;
215372
215373 assert( argc>=1 );
215374 pAux = (Fts5Auxiliary*)sqlite3_user_data(context);
215375 iCsrId = sqlite3_value_int64(argv[0]);
215376
215377 pCsr = fts5CursorFromCsrid(pAux->pGlobal, iCsrId);
215378 if( pCsr==0 ){
215379 char *zErr = sqlite3_mprintf("no such cursor: %lld", iCsrId);
215380 sqlite3_result_error(context, zErr, -1);
215381 sqlite3_free(zErr);
215382 }else{
215383 fts5ApiInvoke(pAux, pCsr, context, argc-1, &argv[1]);
215384 }
215385}
215386
215387
215388/*
215389** Given cursor id iId, return a pointer to the corresponding Fts5Index
215390** object. Or NULL If the cursor id does not exist.
215391**
215392** If successful, set *ppConfig to point to the associated config object
215393** before returning.
215394*/
215395static Fts5Index *sqlite3Fts5IndexFromCsrid(
215396 Fts5Global *pGlobal, /* FTS5 global context for db handle */
215397 i64 iCsrId, /* Id of cursor to find */
215398 Fts5Config **ppConfig /* OUT: Configuration object */
215399){
215400 Fts5Cursor *pCsr;
215401 Fts5Table *pTab;
215402
215403 pCsr = fts5CursorFromCsrid(pGlobal, iCsrId);
215404 pTab = (Fts5Table*)pCsr->base.pVtab;
215405 *ppConfig = pTab->pConfig;
215406
215407 return pTab->pIndex;
215408}
215409
215410/*
215411** Return a "position-list blob" corresponding to the current position of
215412** cursor pCsr via sqlite3_result_blob(). A position-list blob contains
215413** the current position-list for each phrase in the query associated with
215414** cursor pCsr.
215415**
215416** A position-list blob begins with (nPhrase-1) varints, where nPhrase is
215417** the number of phrases in the query. Following the varints are the
215418** concatenated position lists for each phrase, in order.
215419**
215420** The first varint (if it exists) contains the size of the position list
215421** for phrase 0. The second (same disclaimer) contains the size of position
215422** list 1. And so on. There is no size field for the final position list,
215423** as it can be derived from the total size of the blob.
215424*/
215425static int fts5PoslistBlob(sqlite3_context *pCtx, Fts5Cursor *pCsr){
215426 int i;
215427 int rc = SQLITE_OK;
215428 int nPhrase = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
215429 Fts5Buffer val;
215430
215431 memset(&val, 0, sizeof(Fts5Buffer));
215432 switch( ((Fts5Table*)(pCsr->base.pVtab))->pConfig->eDetail ){
215433 case FTS5_DETAIL_FULL:
215434
215435 /* Append the varints */
215436 for(i=0; i<(nPhrase-1); i++){
215437 const u8 *dummy;
215438 int nByte = sqlite3Fts5ExprPoslist(pCsr->pExpr, i, &dummy);
215439 sqlite3Fts5BufferAppendVarint(&rc, &val, nByte);
215440 }
215441
215442 /* Append the position lists */
215443 for(i=0; i<nPhrase; i++){
215444 const u8 *pPoslist;
215445 int nPoslist;
215446 nPoslist = sqlite3Fts5ExprPoslist(pCsr->pExpr, i, &pPoslist);
215447 sqlite3Fts5BufferAppendBlob(&rc, &val, nPoslist, pPoslist);
215448 }
215449 break;
215450
215451 case FTS5_DETAIL_COLUMNS:
215452
215453 /* Append the varints */
215454 for(i=0; rc==SQLITE_OK && i<(nPhrase-1); i++){
215455 const u8 *dummy;
215456 int nByte;
215457 rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, i, &dummy, &nByte);
215458 sqlite3Fts5BufferAppendVarint(&rc, &val, nByte);
215459 }
215460
215461 /* Append the position lists */
215462 for(i=0; rc==SQLITE_OK && i<nPhrase; i++){
215463 const u8 *pPoslist;
215464 int nPoslist;
215465 rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, i, &pPoslist, &nPoslist);
215466 sqlite3Fts5BufferAppendBlob(&rc, &val, nPoslist, pPoslist);
215467 }
215468 break;
215469
215470 default:
215471 break;
215472 }
215473
215474 sqlite3_result_blob(pCtx, val.p, val.n, sqlite3_free);
215475 return rc;
215476}
215477
215478/*
215479** This is the xColumn method, called by SQLite to request a value from
215480** the row that the supplied cursor currently points to.
215481*/
215482static int fts5ColumnMethod(
215483 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
215484 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
215485 int iCol /* Index of column to read value from */
215486){
215487 Fts5Table *pTab = (Fts5Table*)(pCursor->pVtab);
215488 Fts5Config *pConfig = pTab->pConfig;
215489 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
215490 int rc = SQLITE_OK;
215491
215492 assert( CsrFlagTest(pCsr, FTS5CSR_EOF)==0 );
215493
215494 if( pCsr->ePlan==FTS5_PLAN_SPECIAL ){
215495 if( iCol==pConfig->nCol ){
215496 sqlite3_result_int64(pCtx, pCsr->iSpecial);
215497 }
215498 }else
215499
215500 if( iCol==pConfig->nCol ){
215501 /* User is requesting the value of the special column with the same name
215502 ** as the table. Return the cursor integer id number. This value is only
215503 ** useful in that it may be passed as the first argument to an FTS5
215504 ** auxiliary function. */
215505 sqlite3_result_int64(pCtx, pCsr->iCsrId);
215506 }else if( iCol==pConfig->nCol+1 ){
215507
215508 /* The value of the "rank" column. */
215509 if( pCsr->ePlan==FTS5_PLAN_SOURCE ){
215510 fts5PoslistBlob(pCtx, pCsr);
215511 }else if(
215512 pCsr->ePlan==FTS5_PLAN_MATCH
215513 || pCsr->ePlan==FTS5_PLAN_SORTED_MATCH
215514 ){
215515 if( pCsr->pRank || SQLITE_OK==(rc = fts5FindRankFunction(pCsr)) ){
215516 fts5ApiInvoke(pCsr->pRank, pCsr, pCtx, pCsr->nRankArg, pCsr->apRankArg);
215517 }
215518 }
215519 }else if( !fts5IsContentless(pTab) ){
215520 rc = fts5SeekCursor(pCsr, 1);
215521 if( rc==SQLITE_OK ){
215522 sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1));
215523 }
215524 }
215525 return rc;
215526}
215527
215528
215529/*
215530** This routine implements the xFindFunction method for the FTS3
215531** virtual table.
215532*/
215533static int fts5FindFunctionMethod(
215534 sqlite3_vtab *pVtab, /* Virtual table handle */
215535 int nUnused, /* Number of SQL function arguments */
215536 const char *zName, /* Name of SQL function */
215537 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
215538 void **ppArg /* OUT: User data for *pxFunc */
215539){
215540 Fts5Table *pTab = (Fts5Table*)pVtab;
215541 Fts5Auxiliary *pAux;
215542
215543 UNUSED_PARAM(nUnused);
215544 pAux = fts5FindAuxiliary(pTab, zName);
215545 if( pAux ){
215546 *pxFunc = fts5ApiCallback;
215547 *ppArg = (void*)pAux;
215548 return 1;
215549 }
215550
215551 /* No function of the specified name was found. Return 0. */
215552 return 0;
215553}
215554
215555/*
215556** Implementation of FTS5 xRename method. Rename an fts5 table.
215557*/
215558static int fts5RenameMethod(
215559 sqlite3_vtab *pVtab, /* Virtual table handle */
215560 const char *zName /* New name of table */
215561){
215562 Fts5Table *pTab = (Fts5Table*)pVtab;
215563 return sqlite3Fts5StorageRename(pTab->pStorage, zName);
215564}
215565
215566/*
215567** The xSavepoint() method.
215568**
215569** Flush the contents of the pending-terms table to disk.
215570*/
215571static int fts5SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
215572 Fts5Table *pTab = (Fts5Table*)pVtab;
215573 UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */
215574 fts5CheckTransactionState(pTab, FTS5_SAVEPOINT, iSavepoint);
215575 fts5TripCursors(pTab);
215576 return sqlite3Fts5StorageSync(pTab->pStorage);
215577}
215578
215579/*
215580** The xRelease() method.
215581**
215582** This is a no-op.
215583*/
215584static int fts5ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
215585 Fts5Table *pTab = (Fts5Table*)pVtab;
215586 UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */
215587 fts5CheckTransactionState(pTab, FTS5_RELEASE, iSavepoint);
215588 fts5TripCursors(pTab);
215589 return sqlite3Fts5StorageSync(pTab->pStorage);
215590}
215591
215592/*
215593** The xRollbackTo() method.
215594**
215595** Discard the contents of the pending terms table.
215596*/
215597static int fts5RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
215598 Fts5Table *pTab = (Fts5Table*)pVtab;
215599 UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */
215600 fts5CheckTransactionState(pTab, FTS5_ROLLBACKTO, iSavepoint);
215601 fts5TripCursors(pTab);
215602 return sqlite3Fts5StorageRollback(pTab->pStorage);
215603}
215604
215605/*
215606** Register a new auxiliary function with global context pGlobal.
215607*/
215608static int fts5CreateAux(
215609 fts5_api *pApi, /* Global context (one per db handle) */
215610 const char *zName, /* Name of new function */
215611 void *pUserData, /* User data for aux. function */
215612 fts5_extension_function xFunc, /* Aux. function implementation */
215613 void(*xDestroy)(void*) /* Destructor for pUserData */
215614){
215615 Fts5Global *pGlobal = (Fts5Global*)pApi;
215616 int rc = sqlite3_overload_function(pGlobal->db, zName, -1);
215617 if( rc==SQLITE_OK ){
215618 Fts5Auxiliary *pAux;
215619 int nName; /* Size of zName in bytes, including \0 */
215620 int nByte; /* Bytes of space to allocate */
215621
215622 nName = (int)strlen(zName) + 1;
215623 nByte = sizeof(Fts5Auxiliary) + nName;
215624 pAux = (Fts5Auxiliary*)sqlite3_malloc(nByte);
215625 if( pAux ){
215626 memset(pAux, 0, nByte);
215627 pAux->zFunc = (char*)&pAux[1];
215628 memcpy(pAux->zFunc, zName, nName);
215629 pAux->pGlobal = pGlobal;
215630 pAux->pUserData = pUserData;
215631 pAux->xFunc = xFunc;
215632 pAux->xDestroy = xDestroy;
215633 pAux->pNext = pGlobal->pAux;
215634 pGlobal->pAux = pAux;
215635 }else{
215636 rc = SQLITE_NOMEM;
215637 }
215638 }
215639
215640 return rc;
215641}
215642
215643/*
215644** Register a new tokenizer. This is the implementation of the
215645** fts5_api.xCreateTokenizer() method.
215646*/
215647static int fts5CreateTokenizer(
215648 fts5_api *pApi, /* Global context (one per db handle) */
215649 const char *zName, /* Name of new function */
215650 void *pUserData, /* User data for aux. function */
215651 fts5_tokenizer *pTokenizer, /* Tokenizer implementation */
215652 void(*xDestroy)(void*) /* Destructor for pUserData */
215653){
215654 Fts5Global *pGlobal = (Fts5Global*)pApi;
215655 Fts5TokenizerModule *pNew;
215656 int nName; /* Size of zName and its \0 terminator */
215657 int nByte; /* Bytes of space to allocate */
215658 int rc = SQLITE_OK;
215659
215660 nName = (int)strlen(zName) + 1;
215661 nByte = sizeof(Fts5TokenizerModule) + nName;
215662 pNew = (Fts5TokenizerModule*)sqlite3_malloc(nByte);
215663 if( pNew ){
215664 memset(pNew, 0, nByte);
215665 pNew->zName = (char*)&pNew[1];
215666 memcpy(pNew->zName, zName, nName);
215667 pNew->pUserData = pUserData;
215668 pNew->x = *pTokenizer;
215669 pNew->xDestroy = xDestroy;
215670 pNew->pNext = pGlobal->pTok;
215671 pGlobal->pTok = pNew;
215672 if( pNew->pNext==0 ){
215673 pGlobal->pDfltTok = pNew;
215674 }
215675 }else{
215676 rc = SQLITE_NOMEM;
215677 }
215678
215679 return rc;
215680}
215681
215682static Fts5TokenizerModule *fts5LocateTokenizer(
215683 Fts5Global *pGlobal,
215684 const char *zName
215685){
215686 Fts5TokenizerModule *pMod = 0;
215687
215688 if( zName==0 ){
215689 pMod = pGlobal->pDfltTok;
215690 }else{
215691 for(pMod=pGlobal->pTok; pMod; pMod=pMod->pNext){
215692 if( sqlite3_stricmp(zName, pMod->zName)==0 ) break;
215693 }
215694 }
215695
215696 return pMod;
215697}
215698
215699/*
215700** Find a tokenizer. This is the implementation of the
215701** fts5_api.xFindTokenizer() method.
215702*/
215703static int fts5FindTokenizer(
215704 fts5_api *pApi, /* Global context (one per db handle) */
215705 const char *zName, /* Name of new function */
215706 void **ppUserData,
215707 fts5_tokenizer *pTokenizer /* Populate this object */
215708){
215709 int rc = SQLITE_OK;
215710 Fts5TokenizerModule *pMod;
215711
215712 pMod = fts5LocateTokenizer((Fts5Global*)pApi, zName);
215713 if( pMod ){
215714 *pTokenizer = pMod->x;
215715 *ppUserData = pMod->pUserData;
215716 }else{
215717 memset(pTokenizer, 0, sizeof(fts5_tokenizer));
215718 rc = SQLITE_ERROR;
215719 }
215720
215721 return rc;
215722}
215723
215724static int sqlite3Fts5GetTokenizer(
215725 Fts5Global *pGlobal,
215726 const char **azArg,
215727 int nArg,
215728 Fts5Tokenizer **ppTok,
215729 fts5_tokenizer **ppTokApi,
215730 char **pzErr
215731){
215732 Fts5TokenizerModule *pMod;
215733 int rc = SQLITE_OK;
215734
215735 pMod = fts5LocateTokenizer(pGlobal, nArg==0 ? 0 : azArg[0]);
215736 if( pMod==0 ){
215737 assert( nArg>0 );
215738 rc = SQLITE_ERROR;
215739 *pzErr = sqlite3_mprintf("no such tokenizer: %s", azArg[0]);
215740 }else{
215741 rc = pMod->x.xCreate(pMod->pUserData, &azArg[1], (nArg?nArg-1:0), ppTok);
215742 *ppTokApi = &pMod->x;
215743 if( rc!=SQLITE_OK && pzErr ){
215744 *pzErr = sqlite3_mprintf("error in tokenizer constructor");
215745 }
215746 }
215747
215748 if( rc!=SQLITE_OK ){
215749 *ppTokApi = 0;
215750 *ppTok = 0;
215751 }
215752
215753 return rc;
215754}
215755
215756static void fts5ModuleDestroy(void *pCtx){
215757 Fts5TokenizerModule *pTok, *pNextTok;
215758 Fts5Auxiliary *pAux, *pNextAux;
215759 Fts5Global *pGlobal = (Fts5Global*)pCtx;
215760
215761 for(pAux=pGlobal->pAux; pAux; pAux=pNextAux){
215762 pNextAux = pAux->pNext;
215763 if( pAux->xDestroy ) pAux->xDestroy(pAux->pUserData);
215764 sqlite3_free(pAux);
215765 }
215766
215767 for(pTok=pGlobal->pTok; pTok; pTok=pNextTok){
215768 pNextTok = pTok->pNext;
215769 if( pTok->xDestroy ) pTok->xDestroy(pTok->pUserData);
215770 sqlite3_free(pTok);
215771 }
215772
215773 sqlite3_free(pGlobal);
215774}
215775
215776static void fts5Fts5Func(
215777 sqlite3_context *pCtx, /* Function call context */
215778 int nArg, /* Number of args */
215779 sqlite3_value **apArg /* Function arguments */
215780){
215781 Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx);
215782 fts5_api **ppApi;
215783 UNUSED_PARAM(nArg);
215784 assert( nArg==1 );
215785 ppApi = (fts5_api**)sqlite3_value_pointer(apArg[0], "fts5_api_ptr");
215786 if( ppApi ) *ppApi = &pGlobal->api;
215787}
215788
215789/*
215790** Implementation of fts5_source_id() function.
215791*/
215792static void fts5SourceIdFunc(
215793 sqlite3_context *pCtx, /* Function call context */
215794 int nArg, /* Number of args */
215795 sqlite3_value **apUnused /* Function arguments */
215796){
215797 assert( nArg==0 );
215798 UNUSED_PARAM2(nArg, apUnused);
215799 sqlite3_result_text(pCtx, "fts5: 2018-12-01 12:34:55 bf8c1b2b7a5960c282e543b9c293686dccff272512d08865f4600fb58238b4f9", -1, SQLITE_TRANSIENT);
215800}
215801
215802/*
215803** Return true if zName is the extension on one of the shadow tables used
215804** by this module.
215805*/
215806static int fts5ShadowName(const char *zName){
215807 static const char *azName[] = {
215808 "config", "content", "data", "docsize", "idx"
215809 };
215810 unsigned int i;
215811 for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
215812 if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
215813 }
215814 return 0;
215815}
215816
215817static int fts5Init(sqlite3 *db){
215818 static const sqlite3_module fts5Mod = {
215819 /* iVersion */ 3,
215820 /* xCreate */ fts5CreateMethod,
215821 /* xConnect */ fts5ConnectMethod,
215822 /* xBestIndex */ fts5BestIndexMethod,
215823 /* xDisconnect */ fts5DisconnectMethod,
215824 /* xDestroy */ fts5DestroyMethod,
215825 /* xOpen */ fts5OpenMethod,
215826 /* xClose */ fts5CloseMethod,
215827 /* xFilter */ fts5FilterMethod,
215828 /* xNext */ fts5NextMethod,
215829 /* xEof */ fts5EofMethod,
215830 /* xColumn */ fts5ColumnMethod,
215831 /* xRowid */ fts5RowidMethod,
215832 /* xUpdate */ fts5UpdateMethod,
215833 /* xBegin */ fts5BeginMethod,
215834 /* xSync */ fts5SyncMethod,
215835 /* xCommit */ fts5CommitMethod,
215836 /* xRollback */ fts5RollbackMethod,
215837 /* xFindFunction */ fts5FindFunctionMethod,
215838 /* xRename */ fts5RenameMethod,
215839 /* xSavepoint */ fts5SavepointMethod,
215840 /* xRelease */ fts5ReleaseMethod,
215841 /* xRollbackTo */ fts5RollbackToMethod,
215842 /* xShadowName */ fts5ShadowName
215843 };
215844
215845 int rc;
215846 Fts5Global *pGlobal = 0;
215847
215848 pGlobal = (Fts5Global*)sqlite3_malloc(sizeof(Fts5Global));
215849 if( pGlobal==0 ){
215850 rc = SQLITE_NOMEM;
215851 }else{
215852 void *p = (void*)pGlobal;
215853 memset(pGlobal, 0, sizeof(Fts5Global));
215854 pGlobal->db = db;
215855 pGlobal->api.iVersion = 2;
215856 pGlobal->api.xCreateFunction = fts5CreateAux;
215857 pGlobal->api.xCreateTokenizer = fts5CreateTokenizer;
215858 pGlobal->api.xFindTokenizer = fts5FindTokenizer;
215859 rc = sqlite3_create_module_v2(db, "fts5", &fts5Mod, p, fts5ModuleDestroy);
215860 if( rc==SQLITE_OK ) rc = sqlite3Fts5IndexInit(db);
215861 if( rc==SQLITE_OK ) rc = sqlite3Fts5ExprInit(pGlobal, db);
215862 if( rc==SQLITE_OK ) rc = sqlite3Fts5AuxInit(&pGlobal->api);
215863 if( rc==SQLITE_OK ) rc = sqlite3Fts5TokenizerInit(&pGlobal->api);
215864 if( rc==SQLITE_OK ) rc = sqlite3Fts5VocabInit(pGlobal, db);
215865 if( rc==SQLITE_OK ){
215866 rc = sqlite3_create_function(
215867 db, "fts5", 1, SQLITE_UTF8, p, fts5Fts5Func, 0, 0
215868 );
215869 }
215870 if( rc==SQLITE_OK ){
215871 rc = sqlite3_create_function(
215872 db, "fts5_source_id", 0, SQLITE_UTF8, p, fts5SourceIdFunc, 0, 0
215873 );
215874 }
215875 }
215876
215877 /* If SQLITE_FTS5_ENABLE_TEST_MI is defined, assume that the file
215878 ** fts5_test_mi.c is compiled and linked into the executable. And call
215879 ** its entry point to enable the matchinfo() demo. */
215880#ifdef SQLITE_FTS5_ENABLE_TEST_MI
215881 if( rc==SQLITE_OK ){
215882 extern int sqlite3Fts5TestRegisterMatchinfo(sqlite3*);
215883 rc = sqlite3Fts5TestRegisterMatchinfo(db);
215884 }
215885#endif
215886
215887 return rc;
215888}
215889
215890/*
215891** The following functions are used to register the module with SQLite. If
215892** this module is being built as part of the SQLite core (SQLITE_CORE is
215893** defined), then sqlite3_open() will call sqlite3Fts5Init() directly.
215894**
215895** Or, if this module is being built as a loadable extension,
215896** sqlite3Fts5Init() is omitted and the two standard entry points
215897** sqlite3_fts_init() and sqlite3_fts5_init() defined instead.
215898*/
215899#ifndef SQLITE_CORE
215900#ifdef _WIN32
215901__declspec(dllexport)
215902#endif
215903SQLITE_API int sqlite3_fts_init(
215904 sqlite3 *db,
215905 char **pzErrMsg,
215906 const sqlite3_api_routines *pApi
215907){
215908 SQLITE_EXTENSION_INIT2(pApi);
215909 (void)pzErrMsg; /* Unused parameter */
215910 return fts5Init(db);
215911}
215912
215913#ifdef _WIN32
215914__declspec(dllexport)
215915#endif
215916SQLITE_API int sqlite3_fts5_init(
215917 sqlite3 *db,
215918 char **pzErrMsg,
215919 const sqlite3_api_routines *pApi
215920){
215921 SQLITE_EXTENSION_INIT2(pApi);
215922 (void)pzErrMsg; /* Unused parameter */
215923 return fts5Init(db);
215924}
215925#else
215926SQLITE_PRIVATE int sqlite3Fts5Init(sqlite3 *db){
215927 return fts5Init(db);
215928}
215929#endif
215930
215931/*
215932** 2014 May 31
215933**
215934** The author disclaims copyright to this source code. In place of
215935** a legal notice, here is a blessing:
215936**
215937** May you do good and not evil.
215938** May you find forgiveness for yourself and forgive others.
215939** May you share freely, never taking more than you give.
215940**
215941******************************************************************************
215942**
215943*/
215944
215945
215946
215947/* #include "fts5Int.h" */
215948
215949struct Fts5Storage {
215950 Fts5Config *pConfig;
215951 Fts5Index *pIndex;
215952 int bTotalsValid; /* True if nTotalRow/aTotalSize[] are valid */
215953 i64 nTotalRow; /* Total number of rows in FTS table */
215954 i64 *aTotalSize; /* Total sizes of each column */
215955 sqlite3_stmt *aStmt[11];
215956};
215957
215958
215959#if FTS5_STMT_SCAN_ASC!=0
215960# error "FTS5_STMT_SCAN_ASC mismatch"
215961#endif
215962#if FTS5_STMT_SCAN_DESC!=1
215963# error "FTS5_STMT_SCAN_DESC mismatch"
215964#endif
215965#if FTS5_STMT_LOOKUP!=2
215966# error "FTS5_STMT_LOOKUP mismatch"
215967#endif
215968
215969#define FTS5_STMT_INSERT_CONTENT 3
215970#define FTS5_STMT_REPLACE_CONTENT 4
215971#define FTS5_STMT_DELETE_CONTENT 5
215972#define FTS5_STMT_REPLACE_DOCSIZE 6
215973#define FTS5_STMT_DELETE_DOCSIZE 7
215974#define FTS5_STMT_LOOKUP_DOCSIZE 8
215975#define FTS5_STMT_REPLACE_CONFIG 9
215976#define FTS5_STMT_SCAN 10
215977
215978/*
215979** Prepare the two insert statements - Fts5Storage.pInsertContent and
215980** Fts5Storage.pInsertDocsize - if they have not already been prepared.
215981** Return SQLITE_OK if successful, or an SQLite error code if an error
215982** occurs.
215983*/
215984static int fts5StorageGetStmt(
215985 Fts5Storage *p, /* Storage handle */
215986 int eStmt, /* FTS5_STMT_XXX constant */
215987 sqlite3_stmt **ppStmt, /* OUT: Prepared statement handle */
215988 char **pzErrMsg /* OUT: Error message (if any) */
215989){
215990 int rc = SQLITE_OK;
215991
215992 /* If there is no %_docsize table, there should be no requests for
215993 ** statements to operate on it. */
215994 assert( p->pConfig->bColumnsize || (
215995 eStmt!=FTS5_STMT_REPLACE_DOCSIZE
215996 && eStmt!=FTS5_STMT_DELETE_DOCSIZE
215997 && eStmt!=FTS5_STMT_LOOKUP_DOCSIZE
215998 ));
215999
216000 assert( eStmt>=0 && eStmt<ArraySize(p->aStmt) );
216001 if( p->aStmt[eStmt]==0 ){
216002 const char *azStmt[] = {
216003 "SELECT %s FROM %s T WHERE T.%Q >= ? AND T.%Q <= ? ORDER BY T.%Q ASC",
216004 "SELECT %s FROM %s T WHERE T.%Q <= ? AND T.%Q >= ? ORDER BY T.%Q DESC",
216005 "SELECT %s FROM %s T WHERE T.%Q=?", /* LOOKUP */
216006
216007 "INSERT INTO %Q.'%q_content' VALUES(%s)", /* INSERT_CONTENT */
216008 "REPLACE INTO %Q.'%q_content' VALUES(%s)", /* REPLACE_CONTENT */
216009 "DELETE FROM %Q.'%q_content' WHERE id=?", /* DELETE_CONTENT */
216010 "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)", /* REPLACE_DOCSIZE */
216011 "DELETE FROM %Q.'%q_docsize' WHERE id=?", /* DELETE_DOCSIZE */
216012
216013 "SELECT sz FROM %Q.'%q_docsize' WHERE id=?", /* LOOKUP_DOCSIZE */
216014
216015 "REPLACE INTO %Q.'%q_config' VALUES(?,?)", /* REPLACE_CONFIG */
216016 "SELECT %s FROM %s AS T", /* SCAN */
216017 };
216018 Fts5Config *pC = p->pConfig;
216019 char *zSql = 0;
216020
216021 switch( eStmt ){
216022 case FTS5_STMT_SCAN:
216023 zSql = sqlite3_mprintf(azStmt[eStmt],
216024 pC->zContentExprlist, pC->zContent
216025 );
216026 break;
216027
216028 case FTS5_STMT_SCAN_ASC:
216029 case FTS5_STMT_SCAN_DESC:
216030 zSql = sqlite3_mprintf(azStmt[eStmt], pC->zContentExprlist,
216031 pC->zContent, pC->zContentRowid, pC->zContentRowid,
216032 pC->zContentRowid
216033 );
216034 break;
216035
216036 case FTS5_STMT_LOOKUP:
216037 zSql = sqlite3_mprintf(azStmt[eStmt],
216038 pC->zContentExprlist, pC->zContent, pC->zContentRowid
216039 );
216040 break;
216041
216042 case FTS5_STMT_INSERT_CONTENT:
216043 case FTS5_STMT_REPLACE_CONTENT: {
216044 int nCol = pC->nCol + 1;
216045 char *zBind;
216046 int i;
216047
216048 zBind = sqlite3_malloc(1 + nCol*2);
216049 if( zBind ){
216050 for(i=0; i<nCol; i++){
216051 zBind[i*2] = '?';
216052 zBind[i*2 + 1] = ',';
216053 }
216054 zBind[i*2-1] = '\0';
216055 zSql = sqlite3_mprintf(azStmt[eStmt], pC->zDb, pC->zName, zBind);
216056 sqlite3_free(zBind);
216057 }
216058 break;
216059 }
216060
216061 default:
216062 zSql = sqlite3_mprintf(azStmt[eStmt], pC->zDb, pC->zName);
216063 break;
216064 }
216065
216066 if( zSql==0 ){
216067 rc = SQLITE_NOMEM;
216068 }else{
216069 rc = sqlite3_prepare_v3(pC->db, zSql, -1,
216070 SQLITE_PREPARE_PERSISTENT, &p->aStmt[eStmt], 0);
216071 sqlite3_free(zSql);
216072 if( rc!=SQLITE_OK && pzErrMsg ){
216073 *pzErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pC->db));
216074 }
216075 }
216076 }
216077
216078 *ppStmt = p->aStmt[eStmt];
216079 sqlite3_reset(*ppStmt);
216080 return rc;
216081}
216082
216083
216084static int fts5ExecPrintf(
216085 sqlite3 *db,
216086 char **pzErr,
216087 const char *zFormat,
216088 ...
216089){
216090 int rc;
216091 va_list ap; /* ... printf arguments */
216092 char *zSql;
216093
216094 va_start(ap, zFormat);
216095 zSql = sqlite3_vmprintf(zFormat, ap);
216096
216097 if( zSql==0 ){
216098 rc = SQLITE_NOMEM;
216099 }else{
216100 rc = sqlite3_exec(db, zSql, 0, 0, pzErr);
216101 sqlite3_free(zSql);
216102 }
216103
216104 va_end(ap);
216105 return rc;
216106}
216107
216108/*
216109** Drop all shadow tables. Return SQLITE_OK if successful or an SQLite error
216110** code otherwise.
216111*/
216112static int sqlite3Fts5DropAll(Fts5Config *pConfig){
216113 int rc = fts5ExecPrintf(pConfig->db, 0,
216114 "DROP TABLE IF EXISTS %Q.'%q_data';"
216115 "DROP TABLE IF EXISTS %Q.'%q_idx';"
216116 "DROP TABLE IF EXISTS %Q.'%q_config';",
216117 pConfig->zDb, pConfig->zName,
216118 pConfig->zDb, pConfig->zName,
216119 pConfig->zDb, pConfig->zName
216120 );
216121 if( rc==SQLITE_OK && pConfig->bColumnsize ){
216122 rc = fts5ExecPrintf(pConfig->db, 0,
216123 "DROP TABLE IF EXISTS %Q.'%q_docsize';",
216124 pConfig->zDb, pConfig->zName
216125 );
216126 }
216127 if( rc==SQLITE_OK && pConfig->eContent==FTS5_CONTENT_NORMAL ){
216128 rc = fts5ExecPrintf(pConfig->db, 0,
216129 "DROP TABLE IF EXISTS %Q.'%q_content';",
216130 pConfig->zDb, pConfig->zName
216131 );
216132 }
216133 return rc;
216134}
216135
216136static void fts5StorageRenameOne(
216137 Fts5Config *pConfig, /* Current FTS5 configuration */
216138 int *pRc, /* IN/OUT: Error code */
216139 const char *zTail, /* Tail of table name e.g. "data", "config" */
216140 const char *zName /* New name of FTS5 table */
216141){
216142 if( *pRc==SQLITE_OK ){
216143 *pRc = fts5ExecPrintf(pConfig->db, 0,
216144 "ALTER TABLE %Q.'%q_%s' RENAME TO '%q_%s';",
216145 pConfig->zDb, pConfig->zName, zTail, zName, zTail
216146 );
216147 }
216148}
216149
216150static int sqlite3Fts5StorageRename(Fts5Storage *pStorage, const char *zName){
216151 Fts5Config *pConfig = pStorage->pConfig;
216152 int rc = sqlite3Fts5StorageSync(pStorage);
216153
216154 fts5StorageRenameOne(pConfig, &rc, "data", zName);
216155 fts5StorageRenameOne(pConfig, &rc, "idx", zName);
216156 fts5StorageRenameOne(pConfig, &rc, "config", zName);
216157 if( pConfig->bColumnsize ){
216158 fts5StorageRenameOne(pConfig, &rc, "docsize", zName);
216159 }
216160 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
216161 fts5StorageRenameOne(pConfig, &rc, "content", zName);
216162 }
216163 return rc;
216164}
216165
216166/*
216167** Create the shadow table named zPost, with definition zDefn. Return
216168** SQLITE_OK if successful, or an SQLite error code otherwise.
216169*/
216170static int sqlite3Fts5CreateTable(
216171 Fts5Config *pConfig, /* FTS5 configuration */
216172 const char *zPost, /* Shadow table to create (e.g. "content") */
216173 const char *zDefn, /* Columns etc. for shadow table */
216174 int bWithout, /* True for without rowid */
216175 char **pzErr /* OUT: Error message */
216176){
216177 int rc;
216178 char *zErr = 0;
216179
216180 rc = fts5ExecPrintf(pConfig->db, &zErr, "CREATE TABLE %Q.'%q_%q'(%s)%s",
216181 pConfig->zDb, pConfig->zName, zPost, zDefn,
216182#ifndef SQLITE_FTS5_NO_WITHOUT_ROWID
216183 bWithout?" WITHOUT ROWID":
216184#endif
216185 ""
216186 );
216187 if( zErr ){
216188 *pzErr = sqlite3_mprintf(
216189 "fts5: error creating shadow table %q_%s: %s",
216190 pConfig->zName, zPost, zErr
216191 );
216192 sqlite3_free(zErr);
216193 }
216194
216195 return rc;
216196}
216197
216198/*
216199** Open a new Fts5Index handle. If the bCreate argument is true, create
216200** and initialize the underlying tables
216201**
216202** If successful, set *pp to point to the new object and return SQLITE_OK.
216203** Otherwise, set *pp to NULL and return an SQLite error code.
216204*/
216205static int sqlite3Fts5StorageOpen(
216206 Fts5Config *pConfig,
216207 Fts5Index *pIndex,
216208 int bCreate,
216209 Fts5Storage **pp,
216210 char **pzErr /* OUT: Error message */
216211){
216212 int rc = SQLITE_OK;
216213 Fts5Storage *p; /* New object */
216214 int nByte; /* Bytes of space to allocate */
216215
216216 nByte = sizeof(Fts5Storage) /* Fts5Storage object */
216217 + pConfig->nCol * sizeof(i64); /* Fts5Storage.aTotalSize[] */
216218 *pp = p = (Fts5Storage*)sqlite3_malloc(nByte);
216219 if( !p ) return SQLITE_NOMEM;
216220
216221 memset(p, 0, nByte);
216222 p->aTotalSize = (i64*)&p[1];
216223 p->pConfig = pConfig;
216224 p->pIndex = pIndex;
216225
216226 if( bCreate ){
216227 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
216228 int nDefn = 32 + pConfig->nCol*10;
216229 char *zDefn = sqlite3_malloc(32 + pConfig->nCol * 10);
216230 if( zDefn==0 ){
216231 rc = SQLITE_NOMEM;
216232 }else{
216233 int i;
216234 int iOff;
216235 sqlite3_snprintf(nDefn, zDefn, "id INTEGER PRIMARY KEY");
216236 iOff = (int)strlen(zDefn);
216237 for(i=0; i<pConfig->nCol; i++){
216238 sqlite3_snprintf(nDefn-iOff, &zDefn[iOff], ", c%d", i);
216239 iOff += (int)strlen(&zDefn[iOff]);
216240 }
216241 rc = sqlite3Fts5CreateTable(pConfig, "content", zDefn, 0, pzErr);
216242 }
216243 sqlite3_free(zDefn);
216244 }
216245
216246 if( rc==SQLITE_OK && pConfig->bColumnsize ){
216247 rc = sqlite3Fts5CreateTable(
216248 pConfig, "docsize", "id INTEGER PRIMARY KEY, sz BLOB", 0, pzErr
216249 );
216250 }
216251 if( rc==SQLITE_OK ){
216252 rc = sqlite3Fts5CreateTable(
216253 pConfig, "config", "k PRIMARY KEY, v", 1, pzErr
216254 );
216255 }
216256 if( rc==SQLITE_OK ){
216257 rc = sqlite3Fts5StorageConfigValue(p, "version", 0, FTS5_CURRENT_VERSION);
216258 }
216259 }
216260
216261 if( rc ){
216262 sqlite3Fts5StorageClose(p);
216263 *pp = 0;
216264 }
216265 return rc;
216266}
216267
216268/*
216269** Close a handle opened by an earlier call to sqlite3Fts5StorageOpen().
216270*/
216271static int sqlite3Fts5StorageClose(Fts5Storage *p){
216272 int rc = SQLITE_OK;
216273 if( p ){
216274 int i;
216275
216276 /* Finalize all SQL statements */
216277 for(i=0; i<ArraySize(p->aStmt); i++){
216278 sqlite3_finalize(p->aStmt[i]);
216279 }
216280
216281 sqlite3_free(p);
216282 }
216283 return rc;
216284}
216285
216286typedef struct Fts5InsertCtx Fts5InsertCtx;
216287struct Fts5InsertCtx {
216288 Fts5Storage *pStorage;
216289 int iCol;
216290 int szCol; /* Size of column value in tokens */
216291};
216292
216293/*
216294** Tokenization callback used when inserting tokens into the FTS index.
216295*/
216296static int fts5StorageInsertCallback(
216297 void *pContext, /* Pointer to Fts5InsertCtx object */
216298 int tflags,
216299 const char *pToken, /* Buffer containing token */
216300 int nToken, /* Size of token in bytes */
216301 int iUnused1, /* Start offset of token */
216302 int iUnused2 /* End offset of token */
216303){
216304 Fts5InsertCtx *pCtx = (Fts5InsertCtx*)pContext;
216305 Fts5Index *pIdx = pCtx->pStorage->pIndex;
216306 UNUSED_PARAM2(iUnused1, iUnused2);
216307 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
216308 if( (tflags & FTS5_TOKEN_COLOCATED)==0 || pCtx->szCol==0 ){
216309 pCtx->szCol++;
216310 }
216311 return sqlite3Fts5IndexWrite(pIdx, pCtx->iCol, pCtx->szCol-1, pToken, nToken);
216312}
216313
216314/*
216315** If a row with rowid iDel is present in the %_content table, add the
216316** delete-markers to the FTS index necessary to delete it. Do not actually
216317** remove the %_content row at this time though.
216318*/
216319static int fts5StorageDeleteFromIndex(
216320 Fts5Storage *p,
216321 i64 iDel,
216322 sqlite3_value **apVal
216323){
216324 Fts5Config *pConfig = p->pConfig;
216325 sqlite3_stmt *pSeek = 0; /* SELECT to read row iDel from %_data */
216326 int rc; /* Return code */
216327 int rc2; /* sqlite3_reset() return code */
216328 int iCol;
216329 Fts5InsertCtx ctx;
216330
216331 if( apVal==0 ){
216332 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP, &pSeek, 0);
216333 if( rc!=SQLITE_OK ) return rc;
216334 sqlite3_bind_int64(pSeek, 1, iDel);
216335 if( sqlite3_step(pSeek)!=SQLITE_ROW ){
216336 return sqlite3_reset(pSeek);
216337 }
216338 }
216339
216340 ctx.pStorage = p;
216341 ctx.iCol = -1;
216342 rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 1, iDel);
216343 for(iCol=1; rc==SQLITE_OK && iCol<=pConfig->nCol; iCol++){
216344 if( pConfig->abUnindexed[iCol-1]==0 ){
216345 const char *zText;
216346 int nText;
216347 if( pSeek ){
216348 zText = (const char*)sqlite3_column_text(pSeek, iCol);
216349 nText = sqlite3_column_bytes(pSeek, iCol);
216350 }else{
216351 zText = (const char*)sqlite3_value_text(apVal[iCol-1]);
216352 nText = sqlite3_value_bytes(apVal[iCol-1]);
216353 }
216354 ctx.szCol = 0;
216355 rc = sqlite3Fts5Tokenize(pConfig, FTS5_TOKENIZE_DOCUMENT,
216356 zText, nText, (void*)&ctx, fts5StorageInsertCallback
216357 );
216358 p->aTotalSize[iCol-1] -= (i64)ctx.szCol;
216359 }
216360 }
216361 p->nTotalRow--;
216362
216363 rc2 = sqlite3_reset(pSeek);
216364 if( rc==SQLITE_OK ) rc = rc2;
216365 return rc;
216366}
216367
216368
216369/*
216370** Insert a record into the %_docsize table. Specifically, do:
216371**
216372** INSERT OR REPLACE INTO %_docsize(id, sz) VALUES(iRowid, pBuf);
216373**
216374** If there is no %_docsize table (as happens if the columnsize=0 option
216375** is specified when the FTS5 table is created), this function is a no-op.
216376*/
216377static int fts5StorageInsertDocsize(
216378 Fts5Storage *p, /* Storage module to write to */
216379 i64 iRowid, /* id value */
216380 Fts5Buffer *pBuf /* sz value */
216381){
216382 int rc = SQLITE_OK;
216383 if( p->pConfig->bColumnsize ){
216384 sqlite3_stmt *pReplace = 0;
216385 rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE, &pReplace, 0);
216386 if( rc==SQLITE_OK ){
216387 sqlite3_bind_int64(pReplace, 1, iRowid);
216388 sqlite3_bind_blob(pReplace, 2, pBuf->p, pBuf->n, SQLITE_STATIC);
216389 sqlite3_step(pReplace);
216390 rc = sqlite3_reset(pReplace);
216391 sqlite3_bind_null(pReplace, 2);
216392 }
216393 }
216394 return rc;
216395}
216396
216397/*
216398** Load the contents of the "averages" record from disk into the
216399** p->nTotalRow and p->aTotalSize[] variables. If successful, and if
216400** argument bCache is true, set the p->bTotalsValid flag to indicate
216401** that the contents of aTotalSize[] and nTotalRow are valid until
216402** further notice.
216403**
216404** Return SQLITE_OK if successful, or an SQLite error code if an error
216405** occurs.
216406*/
216407static int fts5StorageLoadTotals(Fts5Storage *p, int bCache){
216408 int rc = SQLITE_OK;
216409 if( p->bTotalsValid==0 ){
216410 rc = sqlite3Fts5IndexGetAverages(p->pIndex, &p->nTotalRow, p->aTotalSize);
216411 p->bTotalsValid = bCache;
216412 }
216413 return rc;
216414}
216415
216416/*
216417** Store the current contents of the p->nTotalRow and p->aTotalSize[]
216418** variables in the "averages" record on disk.
216419**
216420** Return SQLITE_OK if successful, or an SQLite error code if an error
216421** occurs.
216422*/
216423static int fts5StorageSaveTotals(Fts5Storage *p){
216424 int nCol = p->pConfig->nCol;
216425 int i;
216426 Fts5Buffer buf;
216427 int rc = SQLITE_OK;
216428 memset(&buf, 0, sizeof(buf));
216429
216430 sqlite3Fts5BufferAppendVarint(&rc, &buf, p->nTotalRow);
216431 for(i=0; i<nCol; i++){
216432 sqlite3Fts5BufferAppendVarint(&rc, &buf, p->aTotalSize[i]);
216433 }
216434 if( rc==SQLITE_OK ){
216435 rc = sqlite3Fts5IndexSetAverages(p->pIndex, buf.p, buf.n);
216436 }
216437 sqlite3_free(buf.p);
216438
216439 return rc;
216440}
216441
216442/*
216443** Remove a row from the FTS table.
216444*/
216445static int sqlite3Fts5StorageDelete(Fts5Storage *p, i64 iDel, sqlite3_value **apVal){
216446 Fts5Config *pConfig = p->pConfig;
216447 int rc;
216448 sqlite3_stmt *pDel = 0;
216449
216450 assert( pConfig->eContent!=FTS5_CONTENT_NORMAL || apVal==0 );
216451 rc = fts5StorageLoadTotals(p, 1);
216452
216453 /* Delete the index records */
216454 if( rc==SQLITE_OK ){
216455 rc = fts5StorageDeleteFromIndex(p, iDel, apVal);
216456 }
216457
216458 /* Delete the %_docsize record */
216459 if( rc==SQLITE_OK && pConfig->bColumnsize ){
216460 rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_DOCSIZE, &pDel, 0);
216461 if( rc==SQLITE_OK ){
216462 sqlite3_bind_int64(pDel, 1, iDel);
216463 sqlite3_step(pDel);
216464 rc = sqlite3_reset(pDel);
216465 }
216466 }
216467
216468 /* Delete the %_content record */
216469 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
216470 if( rc==SQLITE_OK ){
216471 rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_CONTENT, &pDel, 0);
216472 }
216473 if( rc==SQLITE_OK ){
216474 sqlite3_bind_int64(pDel, 1, iDel);
216475 sqlite3_step(pDel);
216476 rc = sqlite3_reset(pDel);
216477 }
216478 }
216479
216480 return rc;
216481}
216482
216483/*
216484** Delete all entries in the FTS5 index.
216485*/
216486static int sqlite3Fts5StorageDeleteAll(Fts5Storage *p){
216487 Fts5Config *pConfig = p->pConfig;
216488 int rc;
216489
216490 /* Delete the contents of the %_data and %_docsize tables. */
216491 rc = fts5ExecPrintf(pConfig->db, 0,
216492 "DELETE FROM %Q.'%q_data';"
216493 "DELETE FROM %Q.'%q_idx';",
216494 pConfig->zDb, pConfig->zName,
216495 pConfig->zDb, pConfig->zName
216496 );
216497 if( rc==SQLITE_OK && pConfig->bColumnsize ){
216498 rc = fts5ExecPrintf(pConfig->db, 0,
216499 "DELETE FROM %Q.'%q_docsize';",
216500 pConfig->zDb, pConfig->zName
216501 );
216502 }
216503
216504 /* Reinitialize the %_data table. This call creates the initial structure
216505 ** and averages records. */
216506 if( rc==SQLITE_OK ){
216507 rc = sqlite3Fts5IndexReinit(p->pIndex);
216508 }
216509 if( rc==SQLITE_OK ){
216510 rc = sqlite3Fts5StorageConfigValue(p, "version", 0, FTS5_CURRENT_VERSION);
216511 }
216512 return rc;
216513}
216514
216515static int sqlite3Fts5StorageRebuild(Fts5Storage *p){
216516 Fts5Buffer buf = {0,0,0};
216517 Fts5Config *pConfig = p->pConfig;
216518 sqlite3_stmt *pScan = 0;
216519 Fts5InsertCtx ctx;
216520 int rc;
216521
216522 memset(&ctx, 0, sizeof(Fts5InsertCtx));
216523 ctx.pStorage = p;
216524 rc = sqlite3Fts5StorageDeleteAll(p);
216525 if( rc==SQLITE_OK ){
216526 rc = fts5StorageLoadTotals(p, 1);
216527 }
216528
216529 if( rc==SQLITE_OK ){
216530 rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN, &pScan, 0);
216531 }
216532
216533 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pScan) ){
216534 i64 iRowid = sqlite3_column_int64(pScan, 0);
216535
216536 sqlite3Fts5BufferZero(&buf);
216537 rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 0, iRowid);
216538 for(ctx.iCol=0; rc==SQLITE_OK && ctx.iCol<pConfig->nCol; ctx.iCol++){
216539 ctx.szCol = 0;
216540 if( pConfig->abUnindexed[ctx.iCol]==0 ){
216541 rc = sqlite3Fts5Tokenize(pConfig,
216542 FTS5_TOKENIZE_DOCUMENT,
216543 (const char*)sqlite3_column_text(pScan, ctx.iCol+1),
216544 sqlite3_column_bytes(pScan, ctx.iCol+1),
216545 (void*)&ctx,
216546 fts5StorageInsertCallback
216547 );
216548 }
216549 sqlite3Fts5BufferAppendVarint(&rc, &buf, ctx.szCol);
216550 p->aTotalSize[ctx.iCol] += (i64)ctx.szCol;
216551 }
216552 p->nTotalRow++;
216553
216554 if( rc==SQLITE_OK ){
216555 rc = fts5StorageInsertDocsize(p, iRowid, &buf);
216556 }
216557 }
216558 sqlite3_free(buf.p);
216559
216560 /* Write the averages record */
216561 if( rc==SQLITE_OK ){
216562 rc = fts5StorageSaveTotals(p);
216563 }
216564 return rc;
216565}
216566
216567static int sqlite3Fts5StorageOptimize(Fts5Storage *p){
216568 return sqlite3Fts5IndexOptimize(p->pIndex);
216569}
216570
216571static int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge){
216572 return sqlite3Fts5IndexMerge(p->pIndex, nMerge);
216573}
216574
216575static int sqlite3Fts5StorageReset(Fts5Storage *p){
216576 return sqlite3Fts5IndexReset(p->pIndex);
216577}
216578
216579/*
216580** Allocate a new rowid. This is used for "external content" tables when
216581** a NULL value is inserted into the rowid column. The new rowid is allocated
216582** by inserting a dummy row into the %_docsize table. The dummy will be
216583** overwritten later.
216584**
216585** If the %_docsize table does not exist, SQLITE_MISMATCH is returned. In
216586** this case the user is required to provide a rowid explicitly.
216587*/
216588static int fts5StorageNewRowid(Fts5Storage *p, i64 *piRowid){
216589 int rc = SQLITE_MISMATCH;
216590 if( p->pConfig->bColumnsize ){
216591 sqlite3_stmt *pReplace = 0;
216592 rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE, &pReplace, 0);
216593 if( rc==SQLITE_OK ){
216594 sqlite3_bind_null(pReplace, 1);
216595 sqlite3_bind_null(pReplace, 2);
216596 sqlite3_step(pReplace);
216597 rc = sqlite3_reset(pReplace);
216598 }
216599 if( rc==SQLITE_OK ){
216600 *piRowid = sqlite3_last_insert_rowid(p->pConfig->db);
216601 }
216602 }
216603 return rc;
216604}
216605
216606/*
216607** Insert a new row into the FTS content table.
216608*/
216609static int sqlite3Fts5StorageContentInsert(
216610 Fts5Storage *p,
216611 sqlite3_value **apVal,
216612 i64 *piRowid
216613){
216614 Fts5Config *pConfig = p->pConfig;
216615 int rc = SQLITE_OK;
216616
216617 /* Insert the new row into the %_content table. */
216618 if( pConfig->eContent!=FTS5_CONTENT_NORMAL ){
216619 if( sqlite3_value_type(apVal[1])==SQLITE_INTEGER ){
216620 *piRowid = sqlite3_value_int64(apVal[1]);
216621 }else{
216622 rc = fts5StorageNewRowid(p, piRowid);
216623 }
216624 }else{
216625 sqlite3_stmt *pInsert = 0; /* Statement to write %_content table */
216626 int i; /* Counter variable */
216627 rc = fts5StorageGetStmt(p, FTS5_STMT_INSERT_CONTENT, &pInsert, 0);
216628 for(i=1; rc==SQLITE_OK && i<=pConfig->nCol+1; i++){
216629 rc = sqlite3_bind_value(pInsert, i, apVal[i]);
216630 }
216631 if( rc==SQLITE_OK ){
216632 sqlite3_step(pInsert);
216633 rc = sqlite3_reset(pInsert);
216634 }
216635 *piRowid = sqlite3_last_insert_rowid(pConfig->db);
216636 }
216637
216638 return rc;
216639}
216640
216641/*
216642** Insert new entries into the FTS index and %_docsize table.
216643*/
216644static int sqlite3Fts5StorageIndexInsert(
216645 Fts5Storage *p,
216646 sqlite3_value **apVal,
216647 i64 iRowid
216648){
216649 Fts5Config *pConfig = p->pConfig;
216650 int rc = SQLITE_OK; /* Return code */
216651 Fts5InsertCtx ctx; /* Tokenization callback context object */
216652 Fts5Buffer buf; /* Buffer used to build up %_docsize blob */
216653
216654 memset(&buf, 0, sizeof(Fts5Buffer));
216655 ctx.pStorage = p;
216656 rc = fts5StorageLoadTotals(p, 1);
216657
216658 if( rc==SQLITE_OK ){
216659 rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 0, iRowid);
216660 }
216661 for(ctx.iCol=0; rc==SQLITE_OK && ctx.iCol<pConfig->nCol; ctx.iCol++){
216662 ctx.szCol = 0;
216663 if( pConfig->abUnindexed[ctx.iCol]==0 ){
216664 rc = sqlite3Fts5Tokenize(pConfig,
216665 FTS5_TOKENIZE_DOCUMENT,
216666 (const char*)sqlite3_value_text(apVal[ctx.iCol+2]),
216667 sqlite3_value_bytes(apVal[ctx.iCol+2]),
216668 (void*)&ctx,
216669 fts5StorageInsertCallback
216670 );
216671 }
216672 sqlite3Fts5BufferAppendVarint(&rc, &buf, ctx.szCol);
216673 p->aTotalSize[ctx.iCol] += (i64)ctx.szCol;
216674 }
216675 p->nTotalRow++;
216676
216677 /* Write the %_docsize record */
216678 if( rc==SQLITE_OK ){
216679 rc = fts5StorageInsertDocsize(p, iRowid, &buf);
216680 }
216681 sqlite3_free(buf.p);
216682
216683 return rc;
216684}
216685
216686static int fts5StorageCount(Fts5Storage *p, const char *zSuffix, i64 *pnRow){
216687 Fts5Config *pConfig = p->pConfig;
216688 char *zSql;
216689 int rc;
216690
216691 zSql = sqlite3_mprintf("SELECT count(*) FROM %Q.'%q_%s'",
216692 pConfig->zDb, pConfig->zName, zSuffix
216693 );
216694 if( zSql==0 ){
216695 rc = SQLITE_NOMEM;
216696 }else{
216697 sqlite3_stmt *pCnt = 0;
216698 rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &pCnt, 0);
216699 if( rc==SQLITE_OK ){
216700 if( SQLITE_ROW==sqlite3_step(pCnt) ){
216701 *pnRow = sqlite3_column_int64(pCnt, 0);
216702 }
216703 rc = sqlite3_finalize(pCnt);
216704 }
216705 }
216706
216707 sqlite3_free(zSql);
216708 return rc;
216709}
216710
216711/*
216712** Context object used by sqlite3Fts5StorageIntegrity().
216713*/
216714typedef struct Fts5IntegrityCtx Fts5IntegrityCtx;
216715struct Fts5IntegrityCtx {
216716 i64 iRowid;
216717 int iCol;
216718 int szCol;
216719 u64 cksum;
216720 Fts5Termset *pTermset;
216721 Fts5Config *pConfig;
216722};
216723
216724
216725/*
216726** Tokenization callback used by integrity check.
216727*/
216728static int fts5StorageIntegrityCallback(
216729 void *pContext, /* Pointer to Fts5IntegrityCtx object */
216730 int tflags,
216731 const char *pToken, /* Buffer containing token */
216732 int nToken, /* Size of token in bytes */
216733 int iUnused1, /* Start offset of token */
216734 int iUnused2 /* End offset of token */
216735){
216736 Fts5IntegrityCtx *pCtx = (Fts5IntegrityCtx*)pContext;
216737 Fts5Termset *pTermset = pCtx->pTermset;
216738 int bPresent;
216739 int ii;
216740 int rc = SQLITE_OK;
216741 int iPos;
216742 int iCol;
216743
216744 UNUSED_PARAM2(iUnused1, iUnused2);
216745 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
216746
216747 if( (tflags & FTS5_TOKEN_COLOCATED)==0 || pCtx->szCol==0 ){
216748 pCtx->szCol++;
216749 }
216750
216751 switch( pCtx->pConfig->eDetail ){
216752 case FTS5_DETAIL_FULL:
216753 iPos = pCtx->szCol-1;
216754 iCol = pCtx->iCol;
216755 break;
216756
216757 case FTS5_DETAIL_COLUMNS:
216758 iPos = pCtx->iCol;
216759 iCol = 0;
216760 break;
216761
216762 default:
216763 assert( pCtx->pConfig->eDetail==FTS5_DETAIL_NONE );
216764 iPos = 0;
216765 iCol = 0;
216766 break;
216767 }
216768
216769 rc = sqlite3Fts5TermsetAdd(pTermset, 0, pToken, nToken, &bPresent);
216770 if( rc==SQLITE_OK && bPresent==0 ){
216771 pCtx->cksum ^= sqlite3Fts5IndexEntryCksum(
216772 pCtx->iRowid, iCol, iPos, 0, pToken, nToken
216773 );
216774 }
216775
216776 for(ii=0; rc==SQLITE_OK && ii<pCtx->pConfig->nPrefix; ii++){
216777 const int nChar = pCtx->pConfig->aPrefix[ii];
216778 int nByte = sqlite3Fts5IndexCharlenToBytelen(pToken, nToken, nChar);
216779 if( nByte ){
216780 rc = sqlite3Fts5TermsetAdd(pTermset, ii+1, pToken, nByte, &bPresent);
216781 if( bPresent==0 ){
216782 pCtx->cksum ^= sqlite3Fts5IndexEntryCksum(
216783 pCtx->iRowid, iCol, iPos, ii+1, pToken, nByte
216784 );
216785 }
216786 }
216787 }
216788
216789 return rc;
216790}
216791
216792/*
216793** Check that the contents of the FTS index match that of the %_content
216794** table. Return SQLITE_OK if they do, or SQLITE_CORRUPT if not. Return
216795** some other SQLite error code if an error occurs while attempting to
216796** determine this.
216797*/
216798static int sqlite3Fts5StorageIntegrity(Fts5Storage *p){
216799 Fts5Config *pConfig = p->pConfig;
216800 int rc; /* Return code */
216801 int *aColSize; /* Array of size pConfig->nCol */
216802 i64 *aTotalSize; /* Array of size pConfig->nCol */
216803 Fts5IntegrityCtx ctx;
216804 sqlite3_stmt *pScan;
216805
216806 memset(&ctx, 0, sizeof(Fts5IntegrityCtx));
216807 ctx.pConfig = p->pConfig;
216808 aTotalSize = (i64*)sqlite3_malloc(pConfig->nCol * (sizeof(int)+sizeof(i64)));
216809 if( !aTotalSize ) return SQLITE_NOMEM;
216810 aColSize = (int*)&aTotalSize[pConfig->nCol];
216811 memset(aTotalSize, 0, sizeof(i64) * pConfig->nCol);
216812
216813 /* Generate the expected index checksum based on the contents of the
216814 ** %_content table. This block stores the checksum in ctx.cksum. */
216815 rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN, &pScan, 0);
216816 if( rc==SQLITE_OK ){
216817 int rc2;
216818 while( SQLITE_ROW==sqlite3_step(pScan) ){
216819 int i;
216820 ctx.iRowid = sqlite3_column_int64(pScan, 0);
216821 ctx.szCol = 0;
216822 if( pConfig->bColumnsize ){
216823 rc = sqlite3Fts5StorageDocsize(p, ctx.iRowid, aColSize);
216824 }
216825 if( rc==SQLITE_OK && pConfig->eDetail==FTS5_DETAIL_NONE ){
216826 rc = sqlite3Fts5TermsetNew(&ctx.pTermset);
216827 }
216828 for(i=0; rc==SQLITE_OK && i<pConfig->nCol; i++){
216829 if( pConfig->abUnindexed[i] ) continue;
216830 ctx.iCol = i;
216831 ctx.szCol = 0;
216832 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
216833 rc = sqlite3Fts5TermsetNew(&ctx.pTermset);
216834 }
216835 if( rc==SQLITE_OK ){
216836 rc = sqlite3Fts5Tokenize(pConfig,
216837 FTS5_TOKENIZE_DOCUMENT,
216838 (const char*)sqlite3_column_text(pScan, i+1),
216839 sqlite3_column_bytes(pScan, i+1),
216840 (void*)&ctx,
216841 fts5StorageIntegrityCallback
216842 );
216843 }
216844 if( rc==SQLITE_OK && pConfig->bColumnsize && ctx.szCol!=aColSize[i] ){
216845 rc = FTS5_CORRUPT;
216846 }
216847 aTotalSize[i] += ctx.szCol;
216848 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
216849 sqlite3Fts5TermsetFree(ctx.pTermset);
216850 ctx.pTermset = 0;
216851 }
216852 }
216853 sqlite3Fts5TermsetFree(ctx.pTermset);
216854 ctx.pTermset = 0;
216855
216856 if( rc!=SQLITE_OK ) break;
216857 }
216858 rc2 = sqlite3_reset(pScan);
216859 if( rc==SQLITE_OK ) rc = rc2;
216860 }
216861
216862 /* Test that the "totals" (sometimes called "averages") record looks Ok */
216863 if( rc==SQLITE_OK ){
216864 int i;
216865 rc = fts5StorageLoadTotals(p, 0);
216866 for(i=0; rc==SQLITE_OK && i<pConfig->nCol; i++){
216867 if( p->aTotalSize[i]!=aTotalSize[i] ) rc = FTS5_CORRUPT;
216868 }
216869 }
216870
216871 /* Check that the %_docsize and %_content tables contain the expected
216872 ** number of rows. */
216873 if( rc==SQLITE_OK && pConfig->eContent==FTS5_CONTENT_NORMAL ){
216874 i64 nRow = 0;
216875 rc = fts5StorageCount(p, "content", &nRow);
216876 if( rc==SQLITE_OK && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT;
216877 }
216878 if( rc==SQLITE_OK && pConfig->bColumnsize ){
216879 i64 nRow = 0;
216880 rc = fts5StorageCount(p, "docsize", &nRow);
216881 if( rc==SQLITE_OK && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT;
216882 }
216883
216884 /* Pass the expected checksum down to the FTS index module. It will
216885 ** verify, amongst other things, that it matches the checksum generated by
216886 ** inspecting the index itself. */
216887 if( rc==SQLITE_OK ){
216888 rc = sqlite3Fts5IndexIntegrityCheck(p->pIndex, ctx.cksum);
216889 }
216890
216891 sqlite3_free(aTotalSize);
216892 return rc;
216893}
216894
216895/*
216896** Obtain an SQLite statement handle that may be used to read data from the
216897** %_content table.
216898*/
216899static int sqlite3Fts5StorageStmt(
216900 Fts5Storage *p,
216901 int eStmt,
216902 sqlite3_stmt **pp,
216903 char **pzErrMsg
216904){
216905 int rc;
216906 assert( eStmt==FTS5_STMT_SCAN_ASC
216907 || eStmt==FTS5_STMT_SCAN_DESC
216908 || eStmt==FTS5_STMT_LOOKUP
216909 );
216910 rc = fts5StorageGetStmt(p, eStmt, pp, pzErrMsg);
216911 if( rc==SQLITE_OK ){
216912 assert( p->aStmt[eStmt]==*pp );
216913 p->aStmt[eStmt] = 0;
216914 }
216915 return rc;
216916}
216917
216918/*
216919** Release an SQLite statement handle obtained via an earlier call to
216920** sqlite3Fts5StorageStmt(). The eStmt parameter passed to this function
216921** must match that passed to the sqlite3Fts5StorageStmt() call.
216922*/
216923static void sqlite3Fts5StorageStmtRelease(
216924 Fts5Storage *p,
216925 int eStmt,
216926 sqlite3_stmt *pStmt
216927){
216928 assert( eStmt==FTS5_STMT_SCAN_ASC
216929 || eStmt==FTS5_STMT_SCAN_DESC
216930 || eStmt==FTS5_STMT_LOOKUP
216931 );
216932 if( p->aStmt[eStmt]==0 ){
216933 sqlite3_reset(pStmt);
216934 p->aStmt[eStmt] = pStmt;
216935 }else{
216936 sqlite3_finalize(pStmt);
216937 }
216938}
216939
216940static int fts5StorageDecodeSizeArray(
216941 int *aCol, int nCol, /* Array to populate */
216942 const u8 *aBlob, int nBlob /* Record to read varints from */
216943){
216944 int i;
216945 int iOff = 0;
216946 for(i=0; i<nCol; i++){
216947 if( iOff>=nBlob ) return 1;
216948 iOff += fts5GetVarint32(&aBlob[iOff], aCol[i]);
216949 }
216950 return (iOff!=nBlob);
216951}
216952
216953/*
216954** Argument aCol points to an array of integers containing one entry for
216955** each table column. This function reads the %_docsize record for the
216956** specified rowid and populates aCol[] with the results.
216957**
216958** An SQLite error code is returned if an error occurs, or SQLITE_OK
216959** otherwise.
216960*/
216961static int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol){
216962 int nCol = p->pConfig->nCol; /* Number of user columns in table */
216963 sqlite3_stmt *pLookup = 0; /* Statement to query %_docsize */
216964 int rc; /* Return Code */
216965
216966 assert( p->pConfig->bColumnsize );
216967 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP_DOCSIZE, &pLookup, 0);
216968 if( rc==SQLITE_OK ){
216969 int bCorrupt = 1;
216970 sqlite3_bind_int64(pLookup, 1, iRowid);
216971 if( SQLITE_ROW==sqlite3_step(pLookup) ){
216972 const u8 *aBlob = sqlite3_column_blob(pLookup, 0);
216973 int nBlob = sqlite3_column_bytes(pLookup, 0);
216974 if( 0==fts5StorageDecodeSizeArray(aCol, nCol, aBlob, nBlob) ){
216975 bCorrupt = 0;
216976 }
216977 }
216978 rc = sqlite3_reset(pLookup);
216979 if( bCorrupt && rc==SQLITE_OK ){
216980 rc = FTS5_CORRUPT;
216981 }
216982 }
216983
216984 return rc;
216985}
216986
216987static int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnToken){
216988 int rc = fts5StorageLoadTotals(p, 0);
216989 if( rc==SQLITE_OK ){
216990 *pnToken = 0;
216991 if( iCol<0 ){
216992 int i;
216993 for(i=0; i<p->pConfig->nCol; i++){
216994 *pnToken += p->aTotalSize[i];
216995 }
216996 }else if( iCol<p->pConfig->nCol ){
216997 *pnToken = p->aTotalSize[iCol];
216998 }else{
216999 rc = SQLITE_RANGE;
217000 }
217001 }
217002 return rc;
217003}
217004
217005static int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow){
217006 int rc = fts5StorageLoadTotals(p, 0);
217007 if( rc==SQLITE_OK ){
217008 *pnRow = p->nTotalRow;
217009 }
217010 return rc;
217011}
217012
217013/*
217014** Flush any data currently held in-memory to disk.
217015*/
217016static int sqlite3Fts5StorageSync(Fts5Storage *p){
217017 int rc = SQLITE_OK;
217018 i64 iLastRowid = sqlite3_last_insert_rowid(p->pConfig->db);
217019 if( p->bTotalsValid ){
217020 rc = fts5StorageSaveTotals(p);
217021 p->bTotalsValid = 0;
217022 }
217023 if( rc==SQLITE_OK ){
217024 rc = sqlite3Fts5IndexSync(p->pIndex);
217025 }
217026 sqlite3_set_last_insert_rowid(p->pConfig->db, iLastRowid);
217027 return rc;
217028}
217029
217030static int sqlite3Fts5StorageRollback(Fts5Storage *p){
217031 p->bTotalsValid = 0;
217032 return sqlite3Fts5IndexRollback(p->pIndex);
217033}
217034
217035static int sqlite3Fts5StorageConfigValue(
217036 Fts5Storage *p,
217037 const char *z,
217038 sqlite3_value *pVal,
217039 int iVal
217040){
217041 sqlite3_stmt *pReplace = 0;
217042 int rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_CONFIG, &pReplace, 0);
217043 if( rc==SQLITE_OK ){
217044 sqlite3_bind_text(pReplace, 1, z, -1, SQLITE_STATIC);
217045 if( pVal ){
217046 sqlite3_bind_value(pReplace, 2, pVal);
217047 }else{
217048 sqlite3_bind_int(pReplace, 2, iVal);
217049 }
217050 sqlite3_step(pReplace);
217051 rc = sqlite3_reset(pReplace);
217052 sqlite3_bind_null(pReplace, 1);
217053 }
217054 if( rc==SQLITE_OK && pVal ){
217055 int iNew = p->pConfig->iCookie + 1;
217056 rc = sqlite3Fts5IndexSetCookie(p->pIndex, iNew);
217057 if( rc==SQLITE_OK ){
217058 p->pConfig->iCookie = iNew;
217059 }
217060 }
217061 return rc;
217062}
217063
217064/*
217065** 2014 May 31
217066**
217067** The author disclaims copyright to this source code. In place of
217068** a legal notice, here is a blessing:
217069**
217070** May you do good and not evil.
217071** May you find forgiveness for yourself and forgive others.
217072** May you share freely, never taking more than you give.
217073**
217074******************************************************************************
217075*/
217076
217077
217078/* #include "fts5Int.h" */
217079
217080/**************************************************************************
217081** Start of ascii tokenizer implementation.
217082*/
217083
217084/*
217085** For tokenizers with no "unicode" modifier, the set of token characters
217086** is the same as the set of ASCII range alphanumeric characters.
217087*/
217088static unsigned char aAsciiTokenChar[128] = {
217089 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00..0x0F */
217090 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10..0x1F */
217091 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20..0x2F */
217092 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 0x30..0x3F */
217093 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40..0x4F */
217094 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x50..0x5F */
217095 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60..0x6F */
217096 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x70..0x7F */
217097};
217098
217099typedef struct AsciiTokenizer AsciiTokenizer;
217100struct AsciiTokenizer {
217101 unsigned char aTokenChar[128];
217102};
217103
217104static void fts5AsciiAddExceptions(
217105 AsciiTokenizer *p,
217106 const char *zArg,
217107 int bTokenChars
217108){
217109 int i;
217110 for(i=0; zArg[i]; i++){
217111 if( (zArg[i] & 0x80)==0 ){
217112 p->aTokenChar[(int)zArg[i]] = (unsigned char)bTokenChars;
217113 }
217114 }
217115}
217116
217117/*
217118** Delete a "ascii" tokenizer.
217119*/
217120static void fts5AsciiDelete(Fts5Tokenizer *p){
217121 sqlite3_free(p);
217122}
217123
217124/*
217125** Create an "ascii" tokenizer.
217126*/
217127static int fts5AsciiCreate(
217128 void *pUnused,
217129 const char **azArg, int nArg,
217130 Fts5Tokenizer **ppOut
217131){
217132 int rc = SQLITE_OK;
217133 AsciiTokenizer *p = 0;
217134 UNUSED_PARAM(pUnused);
217135 if( nArg%2 ){
217136 rc = SQLITE_ERROR;
217137 }else{
217138 p = sqlite3_malloc(sizeof(AsciiTokenizer));
217139 if( p==0 ){
217140 rc = SQLITE_NOMEM;
217141 }else{
217142 int i;
217143 memset(p, 0, sizeof(AsciiTokenizer));
217144 memcpy(p->aTokenChar, aAsciiTokenChar, sizeof(aAsciiTokenChar));
217145 for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
217146 const char *zArg = azArg[i+1];
217147 if( 0==sqlite3_stricmp(azArg[i], "tokenchars") ){
217148 fts5AsciiAddExceptions(p, zArg, 1);
217149 }else
217150 if( 0==sqlite3_stricmp(azArg[i], "separators") ){
217151 fts5AsciiAddExceptions(p, zArg, 0);
217152 }else{
217153 rc = SQLITE_ERROR;
217154 }
217155 }
217156 if( rc!=SQLITE_OK ){
217157 fts5AsciiDelete((Fts5Tokenizer*)p);
217158 p = 0;
217159 }
217160 }
217161 }
217162
217163 *ppOut = (Fts5Tokenizer*)p;
217164 return rc;
217165}
217166
217167
217168static void asciiFold(char *aOut, const char *aIn, int nByte){
217169 int i;
217170 for(i=0; i<nByte; i++){
217171 char c = aIn[i];
217172 if( c>='A' && c<='Z' ) c += 32;
217173 aOut[i] = c;
217174 }
217175}
217176
217177/*
217178** Tokenize some text using the ascii tokenizer.
217179*/
217180static int fts5AsciiTokenize(
217181 Fts5Tokenizer *pTokenizer,
217182 void *pCtx,
217183 int iUnused,
217184 const char *pText, int nText,
217185 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
217186){
217187 AsciiTokenizer *p = (AsciiTokenizer*)pTokenizer;
217188 int rc = SQLITE_OK;
217189 int ie;
217190 int is = 0;
217191
217192 char aFold[64];
217193 int nFold = sizeof(aFold);
217194 char *pFold = aFold;
217195 unsigned char *a = p->aTokenChar;
217196
217197 UNUSED_PARAM(iUnused);
217198
217199 while( is<nText && rc==SQLITE_OK ){
217200 int nByte;
217201
217202 /* Skip any leading divider characters. */
217203 while( is<nText && ((pText[is]&0x80)==0 && a[(int)pText[is]]==0) ){
217204 is++;
217205 }
217206 if( is==nText ) break;
217207
217208 /* Count the token characters */
217209 ie = is+1;
217210 while( ie<nText && ((pText[ie]&0x80) || a[(int)pText[ie]] ) ){
217211 ie++;
217212 }
217213
217214 /* Fold to lower case */
217215 nByte = ie-is;
217216 if( nByte>nFold ){
217217 if( pFold!=aFold ) sqlite3_free(pFold);
217218 pFold = sqlite3_malloc(nByte*2);
217219 if( pFold==0 ){
217220 rc = SQLITE_NOMEM;
217221 break;
217222 }
217223 nFold = nByte*2;
217224 }
217225 asciiFold(pFold, &pText[is], nByte);
217226
217227 /* Invoke the token callback */
217228 rc = xToken(pCtx, 0, pFold, nByte, is, ie);
217229 is = ie+1;
217230 }
217231
217232 if( pFold!=aFold ) sqlite3_free(pFold);
217233 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
217234 return rc;
217235}
217236
217237/**************************************************************************
217238** Start of unicode61 tokenizer implementation.
217239*/
217240
217241
217242/*
217243** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
217244** from the sqlite3 source file utf.c. If this file is compiled as part
217245** of the amalgamation, they are not required.
217246*/
217247#ifndef SQLITE_AMALGAMATION
217248
217249static const unsigned char sqlite3Utf8Trans1[] = {
217250 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
217251 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
217252 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
217253 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
217254 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
217255 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
217256 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
217257 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
217258};
217259
217260#define READ_UTF8(zIn, zTerm, c) \
217261 c = *(zIn++); \
217262 if( c>=0xc0 ){ \
217263 c = sqlite3Utf8Trans1[c-0xc0]; \
217264 while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \
217265 c = (c<<6) + (0x3f & *(zIn++)); \
217266 } \
217267 if( c<0x80 \
217268 || (c&0xFFFFF800)==0xD800 \
217269 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
217270 }
217271
217272
217273#define WRITE_UTF8(zOut, c) { \
217274 if( c<0x00080 ){ \
217275 *zOut++ = (unsigned char)(c&0xFF); \
217276 } \
217277 else if( c<0x00800 ){ \
217278 *zOut++ = 0xC0 + (unsigned char)((c>>6)&0x1F); \
217279 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
217280 } \
217281 else if( c<0x10000 ){ \
217282 *zOut++ = 0xE0 + (unsigned char)((c>>12)&0x0F); \
217283 *zOut++ = 0x80 + (unsigned char)((c>>6) & 0x3F); \
217284 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
217285 }else{ \
217286 *zOut++ = 0xF0 + (unsigned char)((c>>18) & 0x07); \
217287 *zOut++ = 0x80 + (unsigned char)((c>>12) & 0x3F); \
217288 *zOut++ = 0x80 + (unsigned char)((c>>6) & 0x3F); \
217289 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
217290 } \
217291}
217292
217293#endif /* ifndef SQLITE_AMALGAMATION */
217294
217295typedef struct Unicode61Tokenizer Unicode61Tokenizer;
217296struct Unicode61Tokenizer {
217297 unsigned char aTokenChar[128]; /* ASCII range token characters */
217298 char *aFold; /* Buffer to fold text into */
217299 int nFold; /* Size of aFold[] in bytes */
217300 int bRemoveDiacritic; /* True if remove_diacritics=1 is set */
217301 int nException;
217302 int *aiException;
217303
217304 unsigned char aCategory[32]; /* True for token char categories */
217305};
217306
217307static int fts5UnicodeAddExceptions(
217308 Unicode61Tokenizer *p, /* Tokenizer object */
217309 const char *z, /* Characters to treat as exceptions */
217310 int bTokenChars /* 1 for 'tokenchars', 0 for 'separators' */
217311){
217312 int rc = SQLITE_OK;
217313 int n = (int)strlen(z);
217314 int *aNew;
217315
217316 if( n>0 ){
217317 aNew = (int*)sqlite3_realloc(p->aiException, (n+p->nException)*sizeof(int));
217318 if( aNew ){
217319 int nNew = p->nException;
217320 const unsigned char *zCsr = (const unsigned char*)z;
217321 const unsigned char *zTerm = (const unsigned char*)&z[n];
217322 while( zCsr<zTerm ){
217323 int iCode;
217324 int bToken;
217325 READ_UTF8(zCsr, zTerm, iCode);
217326 if( iCode<128 ){
217327 p->aTokenChar[iCode] = (unsigned char)bTokenChars;
217328 }else{
217329 bToken = p->aCategory[sqlite3Fts5UnicodeCategory(iCode)];
217330 assert( (bToken==0 || bToken==1) );
217331 assert( (bTokenChars==0 || bTokenChars==1) );
217332 if( bToken!=bTokenChars && sqlite3Fts5UnicodeIsdiacritic(iCode)==0 ){
217333 int i;
217334 for(i=0; i<nNew; i++){
217335 if( aNew[i]>iCode ) break;
217336 }
217337 memmove(&aNew[i+1], &aNew[i], (nNew-i)*sizeof(int));
217338 aNew[i] = iCode;
217339 nNew++;
217340 }
217341 }
217342 }
217343 p->aiException = aNew;
217344 p->nException = nNew;
217345 }else{
217346 rc = SQLITE_NOMEM;
217347 }
217348 }
217349
217350 return rc;
217351}
217352
217353/*
217354** Return true if the p->aiException[] array contains the value iCode.
217355*/
217356static int fts5UnicodeIsException(Unicode61Tokenizer *p, int iCode){
217357 if( p->nException>0 ){
217358 int *a = p->aiException;
217359 int iLo = 0;
217360 int iHi = p->nException-1;
217361
217362 while( iHi>=iLo ){
217363 int iTest = (iHi + iLo) / 2;
217364 if( iCode==a[iTest] ){
217365 return 1;
217366 }else if( iCode>a[iTest] ){
217367 iLo = iTest+1;
217368 }else{
217369 iHi = iTest-1;
217370 }
217371 }
217372 }
217373
217374 return 0;
217375}
217376
217377/*
217378** Delete a "unicode61" tokenizer.
217379*/
217380static void fts5UnicodeDelete(Fts5Tokenizer *pTok){
217381 if( pTok ){
217382 Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTok;
217383 sqlite3_free(p->aiException);
217384 sqlite3_free(p->aFold);
217385 sqlite3_free(p);
217386 }
217387 return;
217388}
217389
217390static int unicodeSetCategories(Unicode61Tokenizer *p, const char *zCat){
217391 const char *z = zCat;
217392
217393 while( *z ){
217394 while( *z==' ' || *z=='\t' ) z++;
217395 if( *z && sqlite3Fts5UnicodeCatParse(z, p->aCategory) ){
217396 return SQLITE_ERROR;
217397 }
217398 while( *z!=' ' && *z!='\t' && *z!='\0' ) z++;
217399 }
217400
217401 sqlite3Fts5UnicodeAscii(p->aCategory, p->aTokenChar);
217402 return SQLITE_OK;
217403}
217404
217405/*
217406** Create a "unicode61" tokenizer.
217407*/
217408static int fts5UnicodeCreate(
217409 void *pUnused,
217410 const char **azArg, int nArg,
217411 Fts5Tokenizer **ppOut
217412){
217413 int rc = SQLITE_OK; /* Return code */
217414 Unicode61Tokenizer *p = 0; /* New tokenizer object */
217415
217416 UNUSED_PARAM(pUnused);
217417
217418 if( nArg%2 ){
217419 rc = SQLITE_ERROR;
217420 }else{
217421 p = (Unicode61Tokenizer*)sqlite3_malloc(sizeof(Unicode61Tokenizer));
217422 if( p ){
217423 const char *zCat = "L* N* Co";
217424 int i;
217425 memset(p, 0, sizeof(Unicode61Tokenizer));
217426
217427 p->bRemoveDiacritic = 1;
217428 p->nFold = 64;
217429 p->aFold = sqlite3_malloc(p->nFold * sizeof(char));
217430 if( p->aFold==0 ){
217431 rc = SQLITE_NOMEM;
217432 }
217433
217434 /* Search for a "categories" argument */
217435 for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
217436 if( 0==sqlite3_stricmp(azArg[i], "categories") ){
217437 zCat = azArg[i+1];
217438 }
217439 }
217440
217441 if( rc==SQLITE_OK ){
217442 rc = unicodeSetCategories(p, zCat);
217443 }
217444
217445 for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
217446 const char *zArg = azArg[i+1];
217447 if( 0==sqlite3_stricmp(azArg[i], "remove_diacritics") ){
217448 if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1] ){
217449 rc = SQLITE_ERROR;
217450 }
217451 p->bRemoveDiacritic = (zArg[0]=='1');
217452 }else
217453 if( 0==sqlite3_stricmp(azArg[i], "tokenchars") ){
217454 rc = fts5UnicodeAddExceptions(p, zArg, 1);
217455 }else
217456 if( 0==sqlite3_stricmp(azArg[i], "separators") ){
217457 rc = fts5UnicodeAddExceptions(p, zArg, 0);
217458 }else
217459 if( 0==sqlite3_stricmp(azArg[i], "categories") ){
217460 /* no-op */
217461 }else{
217462 rc = SQLITE_ERROR;
217463 }
217464 }
217465
217466 }else{
217467 rc = SQLITE_NOMEM;
217468 }
217469 if( rc!=SQLITE_OK ){
217470 fts5UnicodeDelete((Fts5Tokenizer*)p);
217471 p = 0;
217472 }
217473 *ppOut = (Fts5Tokenizer*)p;
217474 }
217475 return rc;
217476}
217477
217478/*
217479** Return true if, for the purposes of tokenizing with the tokenizer
217480** passed as the first argument, codepoint iCode is considered a token
217481** character (not a separator).
217482*/
217483static int fts5UnicodeIsAlnum(Unicode61Tokenizer *p, int iCode){
217484 return (
217485 p->aCategory[sqlite3Fts5UnicodeCategory(iCode)]
217486 ^ fts5UnicodeIsException(p, iCode)
217487 );
217488}
217489
217490static int fts5UnicodeTokenize(
217491 Fts5Tokenizer *pTokenizer,
217492 void *pCtx,
217493 int iUnused,
217494 const char *pText, int nText,
217495 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
217496){
217497 Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTokenizer;
217498 int rc = SQLITE_OK;
217499 unsigned char *a = p->aTokenChar;
217500
217501 unsigned char *zTerm = (unsigned char*)&pText[nText];
217502 unsigned char *zCsr = (unsigned char *)pText;
217503
217504 /* Output buffer */
217505 char *aFold = p->aFold;
217506 int nFold = p->nFold;
217507 const char *pEnd = &aFold[nFold-6];
217508
217509 UNUSED_PARAM(iUnused);
217510
217511 /* Each iteration of this loop gobbles up a contiguous run of separators,
217512 ** then the next token. */
217513 while( rc==SQLITE_OK ){
217514 int iCode; /* non-ASCII codepoint read from input */
217515 char *zOut = aFold;
217516 int is;
217517 int ie;
217518
217519 /* Skip any separator characters. */
217520 while( 1 ){
217521 if( zCsr>=zTerm ) goto tokenize_done;
217522 if( *zCsr & 0x80 ) {
217523 /* A character outside of the ascii range. Skip past it if it is
217524 ** a separator character. Or break out of the loop if it is not. */
217525 is = zCsr - (unsigned char*)pText;
217526 READ_UTF8(zCsr, zTerm, iCode);
217527 if( fts5UnicodeIsAlnum(p, iCode) ){
217528 goto non_ascii_tokenchar;
217529 }
217530 }else{
217531 if( a[*zCsr] ){
217532 is = zCsr - (unsigned char*)pText;
217533 goto ascii_tokenchar;
217534 }
217535 zCsr++;
217536 }
217537 }
217538
217539 /* Run through the tokenchars. Fold them into the output buffer along
217540 ** the way. */
217541 while( zCsr<zTerm ){
217542
217543 /* Grow the output buffer so that there is sufficient space to fit the
217544 ** largest possible utf-8 character. */
217545 if( zOut>pEnd ){
217546 aFold = sqlite3_malloc(nFold*2);
217547 if( aFold==0 ){
217548 rc = SQLITE_NOMEM;
217549 goto tokenize_done;
217550 }
217551 zOut = &aFold[zOut - p->aFold];
217552 memcpy(aFold, p->aFold, nFold);
217553 sqlite3_free(p->aFold);
217554 p->aFold = aFold;
217555 p->nFold = nFold = nFold*2;
217556 pEnd = &aFold[nFold-6];
217557 }
217558
217559 if( *zCsr & 0x80 ){
217560 /* An non-ascii-range character. Fold it into the output buffer if
217561 ** it is a token character, or break out of the loop if it is not. */
217562 READ_UTF8(zCsr, zTerm, iCode);
217563 if( fts5UnicodeIsAlnum(p,iCode)||sqlite3Fts5UnicodeIsdiacritic(iCode) ){
217564 non_ascii_tokenchar:
217565 iCode = sqlite3Fts5UnicodeFold(iCode, p->bRemoveDiacritic);
217566 if( iCode ) WRITE_UTF8(zOut, iCode);
217567 }else{
217568 break;
217569 }
217570 }else if( a[*zCsr]==0 ){
217571 /* An ascii-range separator character. End of token. */
217572 break;
217573 }else{
217574 ascii_tokenchar:
217575 if( *zCsr>='A' && *zCsr<='Z' ){
217576 *zOut++ = *zCsr + 32;
217577 }else{
217578 *zOut++ = *zCsr;
217579 }
217580 zCsr++;
217581 }
217582 ie = zCsr - (unsigned char*)pText;
217583 }
217584
217585 /* Invoke the token callback */
217586 rc = xToken(pCtx, 0, aFold, zOut-aFold, is, ie);
217587 }
217588
217589 tokenize_done:
217590 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
217591 return rc;
217592}
217593
217594/**************************************************************************
217595** Start of porter stemmer implementation.
217596*/
217597
217598/* Any tokens larger than this (in bytes) are passed through without
217599** stemming. */
217600#define FTS5_PORTER_MAX_TOKEN 64
217601
217602typedef struct PorterTokenizer PorterTokenizer;
217603struct PorterTokenizer {
217604 fts5_tokenizer tokenizer; /* Parent tokenizer module */
217605 Fts5Tokenizer *pTokenizer; /* Parent tokenizer instance */
217606 char aBuf[FTS5_PORTER_MAX_TOKEN + 64];
217607};
217608
217609/*
217610** Delete a "porter" tokenizer.
217611*/
217612static void fts5PorterDelete(Fts5Tokenizer *pTok){
217613 if( pTok ){
217614 PorterTokenizer *p = (PorterTokenizer*)pTok;
217615 if( p->pTokenizer ){
217616 p->tokenizer.xDelete(p->pTokenizer);
217617 }
217618 sqlite3_free(p);
217619 }
217620}
217621
217622/*
217623** Create a "porter" tokenizer.
217624*/
217625static int fts5PorterCreate(
217626 void *pCtx,
217627 const char **azArg, int nArg,
217628 Fts5Tokenizer **ppOut
217629){
217630 fts5_api *pApi = (fts5_api*)pCtx;
217631 int rc = SQLITE_OK;
217632 PorterTokenizer *pRet;
217633 void *pUserdata = 0;
217634 const char *zBase = "unicode61";
217635
217636 if( nArg>0 ){
217637 zBase = azArg[0];
217638 }
217639
217640 pRet = (PorterTokenizer*)sqlite3_malloc(sizeof(PorterTokenizer));
217641 if( pRet ){
217642 memset(pRet, 0, sizeof(PorterTokenizer));
217643 rc = pApi->xFindTokenizer(pApi, zBase, &pUserdata, &pRet->tokenizer);
217644 }else{
217645 rc = SQLITE_NOMEM;
217646 }
217647 if( rc==SQLITE_OK ){
217648 int nArg2 = (nArg>0 ? nArg-1 : 0);
217649 const char **azArg2 = (nArg2 ? &azArg[1] : 0);
217650 rc = pRet->tokenizer.xCreate(pUserdata, azArg2, nArg2, &pRet->pTokenizer);
217651 }
217652
217653 if( rc!=SQLITE_OK ){
217654 fts5PorterDelete((Fts5Tokenizer*)pRet);
217655 pRet = 0;
217656 }
217657 *ppOut = (Fts5Tokenizer*)pRet;
217658 return rc;
217659}
217660
217661typedef struct PorterContext PorterContext;
217662struct PorterContext {
217663 void *pCtx;
217664 int (*xToken)(void*, int, const char*, int, int, int);
217665 char *aBuf;
217666};
217667
217668typedef struct PorterRule PorterRule;
217669struct PorterRule {
217670 const char *zSuffix;
217671 int nSuffix;
217672 int (*xCond)(char *zStem, int nStem);
217673 const char *zOutput;
217674 int nOutput;
217675};
217676
217677#if 0
217678static int fts5PorterApply(char *aBuf, int *pnBuf, PorterRule *aRule){
217679 int ret = -1;
217680 int nBuf = *pnBuf;
217681 PorterRule *p;
217682
217683 for(p=aRule; p->zSuffix; p++){
217684 assert( strlen(p->zSuffix)==p->nSuffix );
217685 assert( strlen(p->zOutput)==p->nOutput );
217686 if( nBuf<p->nSuffix ) continue;
217687 if( 0==memcmp(&aBuf[nBuf - p->nSuffix], p->zSuffix, p->nSuffix) ) break;
217688 }
217689
217690 if( p->zSuffix ){
217691 int nStem = nBuf - p->nSuffix;
217692 if( p->xCond==0 || p->xCond(aBuf, nStem) ){
217693 memcpy(&aBuf[nStem], p->zOutput, p->nOutput);
217694 *pnBuf = nStem + p->nOutput;
217695 ret = p - aRule;
217696 }
217697 }
217698
217699 return ret;
217700}
217701#endif
217702
217703static int fts5PorterIsVowel(char c, int bYIsVowel){
217704 return (
217705 c=='a' || c=='e' || c=='i' || c=='o' || c=='u' || (bYIsVowel && c=='y')
217706 );
217707}
217708
217709static int fts5PorterGobbleVC(char *zStem, int nStem, int bPrevCons){
217710 int i;
217711 int bCons = bPrevCons;
217712
217713 /* Scan for a vowel */
217714 for(i=0; i<nStem; i++){
217715 if( 0==(bCons = !fts5PorterIsVowel(zStem[i], bCons)) ) break;
217716 }
217717
217718 /* Scan for a consonent */
217719 for(i++; i<nStem; i++){
217720 if( (bCons = !fts5PorterIsVowel(zStem[i], bCons)) ) return i+1;
217721 }
217722 return 0;
217723}
217724
217725/* porter rule condition: (m > 0) */
217726static int fts5Porter_MGt0(char *zStem, int nStem){
217727 return !!fts5PorterGobbleVC(zStem, nStem, 0);
217728}
217729
217730/* porter rule condition: (m > 1) */
217731static int fts5Porter_MGt1(char *zStem, int nStem){
217732 int n;
217733 n = fts5PorterGobbleVC(zStem, nStem, 0);
217734 if( n && fts5PorterGobbleVC(&zStem[n], nStem-n, 1) ){
217735 return 1;
217736 }
217737 return 0;
217738}
217739
217740/* porter rule condition: (m = 1) */
217741static int fts5Porter_MEq1(char *zStem, int nStem){
217742 int n;
217743 n = fts5PorterGobbleVC(zStem, nStem, 0);
217744 if( n && 0==fts5PorterGobbleVC(&zStem[n], nStem-n, 1) ){
217745 return 1;
217746 }
217747 return 0;
217748}
217749
217750/* porter rule condition: (*o) */
217751static int fts5Porter_Ostar(char *zStem, int nStem){
217752 if( zStem[nStem-1]=='w' || zStem[nStem-1]=='x' || zStem[nStem-1]=='y' ){
217753 return 0;
217754 }else{
217755 int i;
217756 int mask = 0;
217757 int bCons = 0;
217758 for(i=0; i<nStem; i++){
217759 bCons = !fts5PorterIsVowel(zStem[i], bCons);
217760 assert( bCons==0 || bCons==1 );
217761 mask = (mask << 1) + bCons;
217762 }
217763 return ((mask & 0x0007)==0x0005);
217764 }
217765}
217766
217767/* porter rule condition: (m > 1 and (*S or *T)) */
217768static int fts5Porter_MGt1_and_S_or_T(char *zStem, int nStem){
217769 assert( nStem>0 );
217770 return (zStem[nStem-1]=='s' || zStem[nStem-1]=='t')
217771 && fts5Porter_MGt1(zStem, nStem);
217772}
217773
217774/* porter rule condition: (*v*) */
217775static int fts5Porter_Vowel(char *zStem, int nStem){
217776 int i;
217777 for(i=0; i<nStem; i++){
217778 if( fts5PorterIsVowel(zStem[i], i>0) ){
217779 return 1;
217780 }
217781 }
217782 return 0;
217783}
217784
217785
217786/**************************************************************************
217787***************************************************************************
217788** GENERATED CODE STARTS HERE (mkportersteps.tcl)
217789*/
217790
217791static int fts5PorterStep4(char *aBuf, int *pnBuf){
217792 int ret = 0;
217793 int nBuf = *pnBuf;
217794 switch( aBuf[nBuf-2] ){
217795
217796 case 'a':
217797 if( nBuf>2 && 0==memcmp("al", &aBuf[nBuf-2], 2) ){
217798 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
217799 *pnBuf = nBuf - 2;
217800 }
217801 }
217802 break;
217803
217804 case 'c':
217805 if( nBuf>4 && 0==memcmp("ance", &aBuf[nBuf-4], 4) ){
217806 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
217807 *pnBuf = nBuf - 4;
217808 }
217809 }else if( nBuf>4 && 0==memcmp("ence", &aBuf[nBuf-4], 4) ){
217810 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
217811 *pnBuf = nBuf - 4;
217812 }
217813 }
217814 break;
217815
217816 case 'e':
217817 if( nBuf>2 && 0==memcmp("er", &aBuf[nBuf-2], 2) ){
217818 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
217819 *pnBuf = nBuf - 2;
217820 }
217821 }
217822 break;
217823
217824 case 'i':
217825 if( nBuf>2 && 0==memcmp("ic", &aBuf[nBuf-2], 2) ){
217826 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
217827 *pnBuf = nBuf - 2;
217828 }
217829 }
217830 break;
217831
217832 case 'l':
217833 if( nBuf>4 && 0==memcmp("able", &aBuf[nBuf-4], 4) ){
217834 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
217835 *pnBuf = nBuf - 4;
217836 }
217837 }else if( nBuf>4 && 0==memcmp("ible", &aBuf[nBuf-4], 4) ){
217838 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
217839 *pnBuf = nBuf - 4;
217840 }
217841 }
217842 break;
217843
217844 case 'n':
217845 if( nBuf>3 && 0==memcmp("ant", &aBuf[nBuf-3], 3) ){
217846 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
217847 *pnBuf = nBuf - 3;
217848 }
217849 }else if( nBuf>5 && 0==memcmp("ement", &aBuf[nBuf-5], 5) ){
217850 if( fts5Porter_MGt1(aBuf, nBuf-5) ){
217851 *pnBuf = nBuf - 5;
217852 }
217853 }else if( nBuf>4 && 0==memcmp("ment", &aBuf[nBuf-4], 4) ){
217854 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
217855 *pnBuf = nBuf - 4;
217856 }
217857 }else if( nBuf>3 && 0==memcmp("ent", &aBuf[nBuf-3], 3) ){
217858 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
217859 *pnBuf = nBuf - 3;
217860 }
217861 }
217862 break;
217863
217864 case 'o':
217865 if( nBuf>3 && 0==memcmp("ion", &aBuf[nBuf-3], 3) ){
217866 if( fts5Porter_MGt1_and_S_or_T(aBuf, nBuf-3) ){
217867 *pnBuf = nBuf - 3;
217868 }
217869 }else if( nBuf>2 && 0==memcmp("ou", &aBuf[nBuf-2], 2) ){
217870 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
217871 *pnBuf = nBuf - 2;
217872 }
217873 }
217874 break;
217875
217876 case 's':
217877 if( nBuf>3 && 0==memcmp("ism", &aBuf[nBuf-3], 3) ){
217878 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
217879 *pnBuf = nBuf - 3;
217880 }
217881 }
217882 break;
217883
217884 case 't':
217885 if( nBuf>3 && 0==memcmp("ate", &aBuf[nBuf-3], 3) ){
217886 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
217887 *pnBuf = nBuf - 3;
217888 }
217889 }else if( nBuf>3 && 0==memcmp("iti", &aBuf[nBuf-3], 3) ){
217890 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
217891 *pnBuf = nBuf - 3;
217892 }
217893 }
217894 break;
217895
217896 case 'u':
217897 if( nBuf>3 && 0==memcmp("ous", &aBuf[nBuf-3], 3) ){
217898 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
217899 *pnBuf = nBuf - 3;
217900 }
217901 }
217902 break;
217903
217904 case 'v':
217905 if( nBuf>3 && 0==memcmp("ive", &aBuf[nBuf-3], 3) ){
217906 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
217907 *pnBuf = nBuf - 3;
217908 }
217909 }
217910 break;
217911
217912 case 'z':
217913 if( nBuf>3 && 0==memcmp("ize", &aBuf[nBuf-3], 3) ){
217914 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
217915 *pnBuf = nBuf - 3;
217916 }
217917 }
217918 break;
217919
217920 }
217921 return ret;
217922}
217923
217924
217925static int fts5PorterStep1B2(char *aBuf, int *pnBuf){
217926 int ret = 0;
217927 int nBuf = *pnBuf;
217928 switch( aBuf[nBuf-2] ){
217929
217930 case 'a':
217931 if( nBuf>2 && 0==memcmp("at", &aBuf[nBuf-2], 2) ){
217932 memcpy(&aBuf[nBuf-2], "ate", 3);
217933 *pnBuf = nBuf - 2 + 3;
217934 ret = 1;
217935 }
217936 break;
217937
217938 case 'b':
217939 if( nBuf>2 && 0==memcmp("bl", &aBuf[nBuf-2], 2) ){
217940 memcpy(&aBuf[nBuf-2], "ble", 3);
217941 *pnBuf = nBuf - 2 + 3;
217942 ret = 1;
217943 }
217944 break;
217945
217946 case 'i':
217947 if( nBuf>2 && 0==memcmp("iz", &aBuf[nBuf-2], 2) ){
217948 memcpy(&aBuf[nBuf-2], "ize", 3);
217949 *pnBuf = nBuf - 2 + 3;
217950 ret = 1;
217951 }
217952 break;
217953
217954 }
217955 return ret;
217956}
217957
217958
217959static int fts5PorterStep2(char *aBuf, int *pnBuf){
217960 int ret = 0;
217961 int nBuf = *pnBuf;
217962 switch( aBuf[nBuf-2] ){
217963
217964 case 'a':
217965 if( nBuf>7 && 0==memcmp("ational", &aBuf[nBuf-7], 7) ){
217966 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
217967 memcpy(&aBuf[nBuf-7], "ate", 3);
217968 *pnBuf = nBuf - 7 + 3;
217969 }
217970 }else if( nBuf>6 && 0==memcmp("tional", &aBuf[nBuf-6], 6) ){
217971 if( fts5Porter_MGt0(aBuf, nBuf-6) ){
217972 memcpy(&aBuf[nBuf-6], "tion", 4);
217973 *pnBuf = nBuf - 6 + 4;
217974 }
217975 }
217976 break;
217977
217978 case 'c':
217979 if( nBuf>4 && 0==memcmp("enci", &aBuf[nBuf-4], 4) ){
217980 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
217981 memcpy(&aBuf[nBuf-4], "ence", 4);
217982 *pnBuf = nBuf - 4 + 4;
217983 }
217984 }else if( nBuf>4 && 0==memcmp("anci", &aBuf[nBuf-4], 4) ){
217985 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
217986 memcpy(&aBuf[nBuf-4], "ance", 4);
217987 *pnBuf = nBuf - 4 + 4;
217988 }
217989 }
217990 break;
217991
217992 case 'e':
217993 if( nBuf>4 && 0==memcmp("izer", &aBuf[nBuf-4], 4) ){
217994 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
217995 memcpy(&aBuf[nBuf-4], "ize", 3);
217996 *pnBuf = nBuf - 4 + 3;
217997 }
217998 }
217999 break;
218000
218001 case 'g':
218002 if( nBuf>4 && 0==memcmp("logi", &aBuf[nBuf-4], 4) ){
218003 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
218004 memcpy(&aBuf[nBuf-4], "log", 3);
218005 *pnBuf = nBuf - 4 + 3;
218006 }
218007 }
218008 break;
218009
218010 case 'l':
218011 if( nBuf>3 && 0==memcmp("bli", &aBuf[nBuf-3], 3) ){
218012 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
218013 memcpy(&aBuf[nBuf-3], "ble", 3);
218014 *pnBuf = nBuf - 3 + 3;
218015 }
218016 }else if( nBuf>4 && 0==memcmp("alli", &aBuf[nBuf-4], 4) ){
218017 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
218018 memcpy(&aBuf[nBuf-4], "al", 2);
218019 *pnBuf = nBuf - 4 + 2;
218020 }
218021 }else if( nBuf>5 && 0==memcmp("entli", &aBuf[nBuf-5], 5) ){
218022 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
218023 memcpy(&aBuf[nBuf-5], "ent", 3);
218024 *pnBuf = nBuf - 5 + 3;
218025 }
218026 }else if( nBuf>3 && 0==memcmp("eli", &aBuf[nBuf-3], 3) ){
218027 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
218028 memcpy(&aBuf[nBuf-3], "e", 1);
218029 *pnBuf = nBuf - 3 + 1;
218030 }
218031 }else if( nBuf>5 && 0==memcmp("ousli", &aBuf[nBuf-5], 5) ){
218032 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
218033 memcpy(&aBuf[nBuf-5], "ous", 3);
218034 *pnBuf = nBuf - 5 + 3;
218035 }
218036 }
218037 break;
218038
218039 case 'o':
218040 if( nBuf>7 && 0==memcmp("ization", &aBuf[nBuf-7], 7) ){
218041 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
218042 memcpy(&aBuf[nBuf-7], "ize", 3);
218043 *pnBuf = nBuf - 7 + 3;
218044 }
218045 }else if( nBuf>5 && 0==memcmp("ation", &aBuf[nBuf-5], 5) ){
218046 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
218047 memcpy(&aBuf[nBuf-5], "ate", 3);
218048 *pnBuf = nBuf - 5 + 3;
218049 }
218050 }else if( nBuf>4 && 0==memcmp("ator", &aBuf[nBuf-4], 4) ){
218051 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
218052 memcpy(&aBuf[nBuf-4], "ate", 3);
218053 *pnBuf = nBuf - 4 + 3;
218054 }
218055 }
218056 break;
218057
218058 case 's':
218059 if( nBuf>5 && 0==memcmp("alism", &aBuf[nBuf-5], 5) ){
218060 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
218061 memcpy(&aBuf[nBuf-5], "al", 2);
218062 *pnBuf = nBuf - 5 + 2;
218063 }
218064 }else if( nBuf>7 && 0==memcmp("iveness", &aBuf[nBuf-7], 7) ){
218065 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
218066 memcpy(&aBuf[nBuf-7], "ive", 3);
218067 *pnBuf = nBuf - 7 + 3;
218068 }
218069 }else if( nBuf>7 && 0==memcmp("fulness", &aBuf[nBuf-7], 7) ){
218070 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
218071 memcpy(&aBuf[nBuf-7], "ful", 3);
218072 *pnBuf = nBuf - 7 + 3;
218073 }
218074 }else if( nBuf>7 && 0==memcmp("ousness", &aBuf[nBuf-7], 7) ){
218075 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
218076 memcpy(&aBuf[nBuf-7], "ous", 3);
218077 *pnBuf = nBuf - 7 + 3;
218078 }
218079 }
218080 break;
218081
218082 case 't':
218083 if( nBuf>5 && 0==memcmp("aliti", &aBuf[nBuf-5], 5) ){
218084 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
218085 memcpy(&aBuf[nBuf-5], "al", 2);
218086 *pnBuf = nBuf - 5 + 2;
218087 }
218088 }else if( nBuf>5 && 0==memcmp("iviti", &aBuf[nBuf-5], 5) ){
218089 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
218090 memcpy(&aBuf[nBuf-5], "ive", 3);
218091 *pnBuf = nBuf - 5 + 3;
218092 }
218093 }else if( nBuf>6 && 0==memcmp("biliti", &aBuf[nBuf-6], 6) ){
218094 if( fts5Porter_MGt0(aBuf, nBuf-6) ){
218095 memcpy(&aBuf[nBuf-6], "ble", 3);
218096 *pnBuf = nBuf - 6 + 3;
218097 }
218098 }
218099 break;
218100
218101 }
218102 return ret;
218103}
218104
218105
218106static int fts5PorterStep3(char *aBuf, int *pnBuf){
218107 int ret = 0;
218108 int nBuf = *pnBuf;
218109 switch( aBuf[nBuf-2] ){
218110
218111 case 'a':
218112 if( nBuf>4 && 0==memcmp("ical", &aBuf[nBuf-4], 4) ){
218113 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
218114 memcpy(&aBuf[nBuf-4], "ic", 2);
218115 *pnBuf = nBuf - 4 + 2;
218116 }
218117 }
218118 break;
218119
218120 case 's':
218121 if( nBuf>4 && 0==memcmp("ness", &aBuf[nBuf-4], 4) ){
218122 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
218123 *pnBuf = nBuf - 4;
218124 }
218125 }
218126 break;
218127
218128 case 't':
218129 if( nBuf>5 && 0==memcmp("icate", &aBuf[nBuf-5], 5) ){
218130 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
218131 memcpy(&aBuf[nBuf-5], "ic", 2);
218132 *pnBuf = nBuf - 5 + 2;
218133 }
218134 }else if( nBuf>5 && 0==memcmp("iciti", &aBuf[nBuf-5], 5) ){
218135 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
218136 memcpy(&aBuf[nBuf-5], "ic", 2);
218137 *pnBuf = nBuf - 5 + 2;
218138 }
218139 }
218140 break;
218141
218142 case 'u':
218143 if( nBuf>3 && 0==memcmp("ful", &aBuf[nBuf-3], 3) ){
218144 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
218145 *pnBuf = nBuf - 3;
218146 }
218147 }
218148 break;
218149
218150 case 'v':
218151 if( nBuf>5 && 0==memcmp("ative", &aBuf[nBuf-5], 5) ){
218152 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
218153 *pnBuf = nBuf - 5;
218154 }
218155 }
218156 break;
218157
218158 case 'z':
218159 if( nBuf>5 && 0==memcmp("alize", &aBuf[nBuf-5], 5) ){
218160 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
218161 memcpy(&aBuf[nBuf-5], "al", 2);
218162 *pnBuf = nBuf - 5 + 2;
218163 }
218164 }
218165 break;
218166
218167 }
218168 return ret;
218169}
218170
218171
218172static int fts5PorterStep1B(char *aBuf, int *pnBuf){
218173 int ret = 0;
218174 int nBuf = *pnBuf;
218175 switch( aBuf[nBuf-2] ){
218176
218177 case 'e':
218178 if( nBuf>3 && 0==memcmp("eed", &aBuf[nBuf-3], 3) ){
218179 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
218180 memcpy(&aBuf[nBuf-3], "ee", 2);
218181 *pnBuf = nBuf - 3 + 2;
218182 }
218183 }else if( nBuf>2 && 0==memcmp("ed", &aBuf[nBuf-2], 2) ){
218184 if( fts5Porter_Vowel(aBuf, nBuf-2) ){
218185 *pnBuf = nBuf - 2;
218186 ret = 1;
218187 }
218188 }
218189 break;
218190
218191 case 'n':
218192 if( nBuf>3 && 0==memcmp("ing", &aBuf[nBuf-3], 3) ){
218193 if( fts5Porter_Vowel(aBuf, nBuf-3) ){
218194 *pnBuf = nBuf - 3;
218195 ret = 1;
218196 }
218197 }
218198 break;
218199
218200 }
218201 return ret;
218202}
218203
218204/*
218205** GENERATED CODE ENDS HERE (mkportersteps.tcl)
218206***************************************************************************
218207**************************************************************************/
218208
218209static void fts5PorterStep1A(char *aBuf, int *pnBuf){
218210 int nBuf = *pnBuf;
218211 if( aBuf[nBuf-1]=='s' ){
218212 if( aBuf[nBuf-2]=='e' ){
218213 if( (nBuf>4 && aBuf[nBuf-4]=='s' && aBuf[nBuf-3]=='s')
218214 || (nBuf>3 && aBuf[nBuf-3]=='i' )
218215 ){
218216 *pnBuf = nBuf-2;
218217 }else{
218218 *pnBuf = nBuf-1;
218219 }
218220 }
218221 else if( aBuf[nBuf-2]!='s' ){
218222 *pnBuf = nBuf-1;
218223 }
218224 }
218225}
218226
218227static int fts5PorterCb(
218228 void *pCtx,
218229 int tflags,
218230 const char *pToken,
218231 int nToken,
218232 int iStart,
218233 int iEnd
218234){
218235 PorterContext *p = (PorterContext*)pCtx;
218236
218237 char *aBuf;
218238 int nBuf;
218239
218240 if( nToken>FTS5_PORTER_MAX_TOKEN || nToken<3 ) goto pass_through;
218241 aBuf = p->aBuf;
218242 nBuf = nToken;
218243 memcpy(aBuf, pToken, nBuf);
218244
218245 /* Step 1. */
218246 fts5PorterStep1A(aBuf, &nBuf);
218247 if( fts5PorterStep1B(aBuf, &nBuf) ){
218248 if( fts5PorterStep1B2(aBuf, &nBuf)==0 ){
218249 char c = aBuf[nBuf-1];
218250 if( fts5PorterIsVowel(c, 0)==0
218251 && c!='l' && c!='s' && c!='z' && c==aBuf[nBuf-2]
218252 ){
218253 nBuf--;
218254 }else if( fts5Porter_MEq1(aBuf, nBuf) && fts5Porter_Ostar(aBuf, nBuf) ){
218255 aBuf[nBuf++] = 'e';
218256 }
218257 }
218258 }
218259
218260 /* Step 1C. */
218261 if( aBuf[nBuf-1]=='y' && fts5Porter_Vowel(aBuf, nBuf-1) ){
218262 aBuf[nBuf-1] = 'i';
218263 }
218264
218265 /* Steps 2 through 4. */
218266 fts5PorterStep2(aBuf, &nBuf);
218267 fts5PorterStep3(aBuf, &nBuf);
218268 fts5PorterStep4(aBuf, &nBuf);
218269
218270 /* Step 5a. */
218271 assert( nBuf>0 );
218272 if( aBuf[nBuf-1]=='e' ){
218273 if( fts5Porter_MGt1(aBuf, nBuf-1)
218274 || (fts5Porter_MEq1(aBuf, nBuf-1) && !fts5Porter_Ostar(aBuf, nBuf-1))
218275 ){
218276 nBuf--;
218277 }
218278 }
218279
218280 /* Step 5b. */
218281 if( nBuf>1 && aBuf[nBuf-1]=='l'
218282 && aBuf[nBuf-2]=='l' && fts5Porter_MGt1(aBuf, nBuf-1)
218283 ){
218284 nBuf--;
218285 }
218286
218287 return p->xToken(p->pCtx, tflags, aBuf, nBuf, iStart, iEnd);
218288
218289 pass_through:
218290 return p->xToken(p->pCtx, tflags, pToken, nToken, iStart, iEnd);
218291}
218292
218293/*
218294** Tokenize using the porter tokenizer.
218295*/
218296static int fts5PorterTokenize(
218297 Fts5Tokenizer *pTokenizer,
218298 void *pCtx,
218299 int flags,
218300 const char *pText, int nText,
218301 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
218302){
218303 PorterTokenizer *p = (PorterTokenizer*)pTokenizer;
218304 PorterContext sCtx;
218305 sCtx.xToken = xToken;
218306 sCtx.pCtx = pCtx;
218307 sCtx.aBuf = p->aBuf;
218308 return p->tokenizer.xTokenize(
218309 p->pTokenizer, (void*)&sCtx, flags, pText, nText, fts5PorterCb
218310 );
218311}
218312
218313/*
218314** Register all built-in tokenizers with FTS5.
218315*/
218316static int sqlite3Fts5TokenizerInit(fts5_api *pApi){
218317 struct BuiltinTokenizer {
218318 const char *zName;
218319 fts5_tokenizer x;
218320 } aBuiltin[] = {
218321 { "unicode61", {fts5UnicodeCreate, fts5UnicodeDelete, fts5UnicodeTokenize}},
218322 { "ascii", {fts5AsciiCreate, fts5AsciiDelete, fts5AsciiTokenize }},
218323 { "porter", {fts5PorterCreate, fts5PorterDelete, fts5PorterTokenize }},
218324 };
218325
218326 int rc = SQLITE_OK; /* Return code */
218327 int i; /* To iterate through builtin functions */
218328
218329 for(i=0; rc==SQLITE_OK && i<ArraySize(aBuiltin); i++){
218330 rc = pApi->xCreateTokenizer(pApi,
218331 aBuiltin[i].zName,
218332 (void*)pApi,
218333 &aBuiltin[i].x,
218334 0
218335 );
218336 }
218337
218338 return rc;
218339}
218340
218341
218342
218343/*
218344** 2012 May 25
218345**
218346** The author disclaims copyright to this source code. In place of
218347** a legal notice, here is a blessing:
218348**
218349** May you do good and not evil.
218350** May you find forgiveness for yourself and forgive others.
218351** May you share freely, never taking more than you give.
218352**
218353******************************************************************************
218354*/
218355
218356/*
218357** DO NOT EDIT THIS MACHINE GENERATED FILE.
218358*/
218359
218360
218361/* #include <assert.h> */
218362
218363
218364
218365/*
218366** If the argument is a codepoint corresponding to a lowercase letter
218367** in the ASCII range with a diacritic added, return the codepoint
218368** of the ASCII letter only. For example, if passed 235 - "LATIN
218369** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
218370** E"). The resuls of passing a codepoint that corresponds to an
218371** uppercase letter are undefined.
218372*/
218373static int fts5_remove_diacritic(int c){
218374 unsigned short aDia[] = {
218375 0, 1797, 1848, 1859, 1891, 1928, 1940, 1995,
218376 2024, 2040, 2060, 2110, 2168, 2206, 2264, 2286,
218377 2344, 2383, 2472, 2488, 2516, 2596, 2668, 2732,
218378 2782, 2842, 2894, 2954, 2984, 3000, 3028, 3336,
218379 3456, 3696, 3712, 3728, 3744, 3896, 3912, 3928,
218380 3968, 4008, 4040, 4106, 4138, 4170, 4202, 4234,
218381 4266, 4296, 4312, 4344, 4408, 4424, 4472, 4504,
218382 6148, 6198, 6264, 6280, 6360, 6429, 6505, 6529,
218383 61448, 61468, 61534, 61592, 61642, 61688, 61704, 61726,
218384 61784, 61800, 61836, 61880, 61914, 61948, 61998, 62122,
218385 62154, 62200, 62218, 62302, 62364, 62442, 62478, 62536,
218386 62554, 62584, 62604, 62640, 62648, 62656, 62664, 62730,
218387 62924, 63050, 63082, 63274, 63390,
218388 };
218389 char aChar[] = {
218390 '\0', 'a', 'c', 'e', 'i', 'n', 'o', 'u', 'y', 'y', 'a', 'c',
218391 'd', 'e', 'e', 'g', 'h', 'i', 'j', 'k', 'l', 'n', 'o', 'r',
218392 's', 't', 'u', 'u', 'w', 'y', 'z', 'o', 'u', 'a', 'i', 'o',
218393 'u', 'g', 'k', 'o', 'j', 'g', 'n', 'a', 'e', 'i', 'o', 'r',
218394 'u', 's', 't', 'h', 'a', 'e', 'o', 'y', '\0', '\0', '\0', '\0',
218395 '\0', '\0', '\0', '\0', 'a', 'b', 'd', 'd', 'e', 'f', 'g', 'h',
218396 'h', 'i', 'k', 'l', 'l', 'm', 'n', 'p', 'r', 'r', 's', 't',
218397 'u', 'v', 'w', 'w', 'x', 'y', 'z', 'h', 't', 'w', 'y', 'a',
218398 'e', 'i', 'o', 'u', 'y',
218399 };
218400
218401 unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
218402 int iRes = 0;
218403 int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
218404 int iLo = 0;
218405 while( iHi>=iLo ){
218406 int iTest = (iHi + iLo) / 2;
218407 if( key >= aDia[iTest] ){
218408 iRes = iTest;
218409 iLo = iTest+1;
218410 }else{
218411 iHi = iTest-1;
218412 }
218413 }
218414 assert( key>=aDia[iRes] );
218415 return ((c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : (int)aChar[iRes]);
218416}
218417
218418
218419/*
218420** Return true if the argument interpreted as a unicode codepoint
218421** is a diacritical modifier character.
218422*/
218423static int sqlite3Fts5UnicodeIsdiacritic(int c){
218424 unsigned int mask0 = 0x08029FDF;
218425 unsigned int mask1 = 0x000361F8;
218426 if( c<768 || c>817 ) return 0;
218427 return (c < 768+32) ?
218428 (mask0 & (1 << (c-768))) :
218429 (mask1 & (1 << (c-768-32)));
218430}
218431
218432
218433/*
218434** Interpret the argument as a unicode codepoint. If the codepoint
218435** is an upper case character that has a lower case equivalent,
218436** return the codepoint corresponding to the lower case version.
218437** Otherwise, return a copy of the argument.
218438**
218439** The results are undefined if the value passed to this function
218440** is less than zero.
218441*/
218442static int sqlite3Fts5UnicodeFold(int c, int bRemoveDiacritic){
218443 /* Each entry in the following array defines a rule for folding a range
218444 ** of codepoints to lower case. The rule applies to a range of nRange
218445 ** codepoints starting at codepoint iCode.
218446 **
218447 ** If the least significant bit in flags is clear, then the rule applies
218448 ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
218449 ** need to be folded). Or, if it is set, then the rule only applies to
218450 ** every second codepoint in the range, starting with codepoint C.
218451 **
218452 ** The 7 most significant bits in flags are an index into the aiOff[]
218453 ** array. If a specific codepoint C does require folding, then its lower
218454 ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
218455 **
218456 ** The contents of this array are generated by parsing the CaseFolding.txt
218457 ** file distributed as part of the "Unicode Character Database". See
218458 ** http://www.unicode.org for details.
218459 */
218460 static const struct TableEntry {
218461 unsigned short iCode;
218462 unsigned char flags;
218463 unsigned char nRange;
218464 } aEntry[] = {
218465 {65, 14, 26}, {181, 64, 1}, {192, 14, 23},
218466 {216, 14, 7}, {256, 1, 48}, {306, 1, 6},
218467 {313, 1, 16}, {330, 1, 46}, {376, 116, 1},
218468 {377, 1, 6}, {383, 104, 1}, {385, 50, 1},
218469 {386, 1, 4}, {390, 44, 1}, {391, 0, 1},
218470 {393, 42, 2}, {395, 0, 1}, {398, 32, 1},
218471 {399, 38, 1}, {400, 40, 1}, {401, 0, 1},
218472 {403, 42, 1}, {404, 46, 1}, {406, 52, 1},
218473 {407, 48, 1}, {408, 0, 1}, {412, 52, 1},
218474 {413, 54, 1}, {415, 56, 1}, {416, 1, 6},
218475 {422, 60, 1}, {423, 0, 1}, {425, 60, 1},
218476 {428, 0, 1}, {430, 60, 1}, {431, 0, 1},
218477 {433, 58, 2}, {435, 1, 4}, {439, 62, 1},
218478 {440, 0, 1}, {444, 0, 1}, {452, 2, 1},
218479 {453, 0, 1}, {455, 2, 1}, {456, 0, 1},
218480 {458, 2, 1}, {459, 1, 18}, {478, 1, 18},
218481 {497, 2, 1}, {498, 1, 4}, {502, 122, 1},
218482 {503, 134, 1}, {504, 1, 40}, {544, 110, 1},
218483 {546, 1, 18}, {570, 70, 1}, {571, 0, 1},
218484 {573, 108, 1}, {574, 68, 1}, {577, 0, 1},
218485 {579, 106, 1}, {580, 28, 1}, {581, 30, 1},
218486 {582, 1, 10}, {837, 36, 1}, {880, 1, 4},
218487 {886, 0, 1}, {902, 18, 1}, {904, 16, 3},
218488 {908, 26, 1}, {910, 24, 2}, {913, 14, 17},
218489 {931, 14, 9}, {962, 0, 1}, {975, 4, 1},
218490 {976, 140, 1}, {977, 142, 1}, {981, 146, 1},
218491 {982, 144, 1}, {984, 1, 24}, {1008, 136, 1},
218492 {1009, 138, 1}, {1012, 130, 1}, {1013, 128, 1},
218493 {1015, 0, 1}, {1017, 152, 1}, {1018, 0, 1},
218494 {1021, 110, 3}, {1024, 34, 16}, {1040, 14, 32},
218495 {1120, 1, 34}, {1162, 1, 54}, {1216, 6, 1},
218496 {1217, 1, 14}, {1232, 1, 88}, {1329, 22, 38},
218497 {4256, 66, 38}, {4295, 66, 1}, {4301, 66, 1},
218498 {7680, 1, 150}, {7835, 132, 1}, {7838, 96, 1},
218499 {7840, 1, 96}, {7944, 150, 8}, {7960, 150, 6},
218500 {7976, 150, 8}, {7992, 150, 8}, {8008, 150, 6},
218501 {8025, 151, 8}, {8040, 150, 8}, {8072, 150, 8},
218502 {8088, 150, 8}, {8104, 150, 8}, {8120, 150, 2},
218503 {8122, 126, 2}, {8124, 148, 1}, {8126, 100, 1},
218504 {8136, 124, 4}, {8140, 148, 1}, {8152, 150, 2},
218505 {8154, 120, 2}, {8168, 150, 2}, {8170, 118, 2},
218506 {8172, 152, 1}, {8184, 112, 2}, {8186, 114, 2},
218507 {8188, 148, 1}, {8486, 98, 1}, {8490, 92, 1},
218508 {8491, 94, 1}, {8498, 12, 1}, {8544, 8, 16},
218509 {8579, 0, 1}, {9398, 10, 26}, {11264, 22, 47},
218510 {11360, 0, 1}, {11362, 88, 1}, {11363, 102, 1},
218511 {11364, 90, 1}, {11367, 1, 6}, {11373, 84, 1},
218512 {11374, 86, 1}, {11375, 80, 1}, {11376, 82, 1},
218513 {11378, 0, 1}, {11381, 0, 1}, {11390, 78, 2},
218514 {11392, 1, 100}, {11499, 1, 4}, {11506, 0, 1},
218515 {42560, 1, 46}, {42624, 1, 24}, {42786, 1, 14},
218516 {42802, 1, 62}, {42873, 1, 4}, {42877, 76, 1},
218517 {42878, 1, 10}, {42891, 0, 1}, {42893, 74, 1},
218518 {42896, 1, 4}, {42912, 1, 10}, {42922, 72, 1},
218519 {65313, 14, 26},
218520 };
218521 static const unsigned short aiOff[] = {
218522 1, 2, 8, 15, 16, 26, 28, 32,
218523 37, 38, 40, 48, 63, 64, 69, 71,
218524 79, 80, 116, 202, 203, 205, 206, 207,
218525 209, 210, 211, 213, 214, 217, 218, 219,
218526 775, 7264, 10792, 10795, 23228, 23256, 30204, 54721,
218527 54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274,
218528 57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406,
218529 65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462,
218530 65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511,
218531 65514, 65521, 65527, 65528, 65529,
218532 };
218533
218534 int ret = c;
218535
218536 assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 );
218537
218538 if( c<128 ){
218539 if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
218540 }else if( c<65536 ){
218541 const struct TableEntry *p;
218542 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
218543 int iLo = 0;
218544 int iRes = -1;
218545
218546 assert( c>aEntry[0].iCode );
218547 while( iHi>=iLo ){
218548 int iTest = (iHi + iLo) / 2;
218549 int cmp = (c - aEntry[iTest].iCode);
218550 if( cmp>=0 ){
218551 iRes = iTest;
218552 iLo = iTest+1;
218553 }else{
218554 iHi = iTest-1;
218555 }
218556 }
218557
218558 assert( iRes>=0 && c>=aEntry[iRes].iCode );
218559 p = &aEntry[iRes];
218560 if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
218561 ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
218562 assert( ret>0 );
218563 }
218564
218565 if( bRemoveDiacritic ) ret = fts5_remove_diacritic(ret);
218566 }
218567
218568 else if( c>=66560 && c<66600 ){
218569 ret = c + 40;
218570 }
218571
218572 return ret;
218573}
218574
218575
218576#if 0
218577static int sqlite3Fts5UnicodeNCat(void) {
218578 return 32;
218579}
218580#endif
218581
218582static int sqlite3Fts5UnicodeCatParse(const char *zCat, u8 *aArray){
218583 aArray[0] = 1;
218584 switch( zCat[0] ){
218585 case 'C':
218586 switch( zCat[1] ){
218587 case 'c': aArray[1] = 1; break;
218588 case 'f': aArray[2] = 1; break;
218589 case 'n': aArray[3] = 1; break;
218590 case 's': aArray[4] = 1; break;
218591 case 'o': aArray[31] = 1; break;
218592 case '*':
218593 aArray[1] = 1;
218594 aArray[2] = 1;
218595 aArray[3] = 1;
218596 aArray[4] = 1;
218597 aArray[31] = 1;
218598 break;
218599 default: return 1; }
218600 break;
218601
218602 case 'L':
218603 switch( zCat[1] ){
218604 case 'l': aArray[5] = 1; break;
218605 case 'm': aArray[6] = 1; break;
218606 case 'o': aArray[7] = 1; break;
218607 case 't': aArray[8] = 1; break;
218608 case 'u': aArray[9] = 1; break;
218609 case 'C': aArray[30] = 1; break;
218610 case '*':
218611 aArray[5] = 1;
218612 aArray[6] = 1;
218613 aArray[7] = 1;
218614 aArray[8] = 1;
218615 aArray[9] = 1;
218616 aArray[30] = 1;
218617 break;
218618 default: return 1; }
218619 break;
218620
218621 case 'M':
218622 switch( zCat[1] ){
218623 case 'c': aArray[10] = 1; break;
218624 case 'e': aArray[11] = 1; break;
218625 case 'n': aArray[12] = 1; break;
218626 case '*':
218627 aArray[10] = 1;
218628 aArray[11] = 1;
218629 aArray[12] = 1;
218630 break;
218631 default: return 1; }
218632 break;
218633
218634 case 'N':
218635 switch( zCat[1] ){
218636 case 'd': aArray[13] = 1; break;
218637 case 'l': aArray[14] = 1; break;
218638 case 'o': aArray[15] = 1; break;
218639 case '*':
218640 aArray[13] = 1;
218641 aArray[14] = 1;
218642 aArray[15] = 1;
218643 break;
218644 default: return 1; }
218645 break;
218646
218647 case 'P':
218648 switch( zCat[1] ){
218649 case 'c': aArray[16] = 1; break;
218650 case 'd': aArray[17] = 1; break;
218651 case 'e': aArray[18] = 1; break;
218652 case 'f': aArray[19] = 1; break;
218653 case 'i': aArray[20] = 1; break;
218654 case 'o': aArray[21] = 1; break;
218655 case 's': aArray[22] = 1; break;
218656 case '*':
218657 aArray[16] = 1;
218658 aArray[17] = 1;
218659 aArray[18] = 1;
218660 aArray[19] = 1;
218661 aArray[20] = 1;
218662 aArray[21] = 1;
218663 aArray[22] = 1;
218664 break;
218665 default: return 1; }
218666 break;
218667
218668 case 'S':
218669 switch( zCat[1] ){
218670 case 'c': aArray[23] = 1; break;
218671 case 'k': aArray[24] = 1; break;
218672 case 'm': aArray[25] = 1; break;
218673 case 'o': aArray[26] = 1; break;
218674 case '*':
218675 aArray[23] = 1;
218676 aArray[24] = 1;
218677 aArray[25] = 1;
218678 aArray[26] = 1;
218679 break;
218680 default: return 1; }
218681 break;
218682
218683 case 'Z':
218684 switch( zCat[1] ){
218685 case 'l': aArray[27] = 1; break;
218686 case 'p': aArray[28] = 1; break;
218687 case 's': aArray[29] = 1; break;
218688 case '*':
218689 aArray[27] = 1;
218690 aArray[28] = 1;
218691 aArray[29] = 1;
218692 break;
218693 default: return 1; }
218694 break;
218695
218696 }
218697 return 0;
218698}
218699
218700static u16 aFts5UnicodeBlock[] = {
218701 0, 1471, 1753, 1760, 1760, 1760, 1760, 1760, 1760, 1760,
218702 1760, 1760, 1760, 1760, 1760, 1763, 1765,
218703 };
218704static u16 aFts5UnicodeMap[] = {
218705 0, 32, 33, 36, 37, 40, 41, 42, 43, 44,
218706 45, 46, 48, 58, 60, 63, 65, 91, 92, 93,
218707 94, 95, 96, 97, 123, 124, 125, 126, 127, 160,
218708 161, 162, 166, 167, 168, 169, 170, 171, 172, 173,
218709 174, 175, 176, 177, 178, 180, 181, 182, 184, 185,
218710 186, 187, 188, 191, 192, 215, 216, 223, 247, 248,
218711 256, 312, 313, 329, 330, 377, 383, 385, 387, 388,
218712 391, 394, 396, 398, 402, 403, 405, 406, 409, 412,
218713 414, 415, 417, 418, 423, 427, 428, 431, 434, 436,
218714 437, 440, 442, 443, 444, 446, 448, 452, 453, 454,
218715 455, 456, 457, 458, 459, 460, 461, 477, 478, 496,
218716 497, 498, 499, 500, 503, 505, 506, 564, 570, 572,
218717 573, 575, 577, 580, 583, 584, 592, 660, 661, 688,
218718 706, 710, 722, 736, 741, 748, 749, 750, 751, 768,
218719 880, 884, 885, 886, 890, 891, 894, 900, 902, 903,
218720 904, 908, 910, 912, 913, 931, 940, 975, 977, 978,
218721 981, 984, 1008, 1012, 1014, 1015, 1018, 1020, 1021, 1072,
218722 1120, 1154, 1155, 1160, 1162, 1217, 1231, 1232, 1329, 1369,
218723 1370, 1377, 1417, 1418, 1423, 1425, 1470, 1471, 1472, 1473,
218724 1475, 1476, 1478, 1479, 1488, 1520, 1523, 1536, 1542, 1545,
218725 1547, 1548, 1550, 1552, 1563, 1566, 1568, 1600, 1601, 1611,
218726 1632, 1642, 1646, 1648, 1649, 1748, 1749, 1750, 1757, 1758,
218727 1759, 1765, 1767, 1769, 1770, 1774, 1776, 1786, 1789, 1791,
218728 1792, 1807, 1808, 1809, 1810, 1840, 1869, 1958, 1969, 1984,
218729 1994, 2027, 2036, 2038, 2039, 2042, 2048, 2070, 2074, 2075,
218730 2084, 2085, 2088, 2089, 2096, 2112, 2137, 2142, 2208, 2210,
218731 2276, 2304, 2307, 2308, 2362, 2363, 2364, 2365, 2366, 2369,
218732 2377, 2381, 2382, 2384, 2385, 2392, 2402, 2404, 2406, 2416,
218733 2417, 2418, 2425, 2433, 2434, 2437, 2447, 2451, 2474, 2482,
218734 2486, 2492, 2493, 2494, 2497, 2503, 2507, 2509, 2510, 2519,
218735 2524, 2527, 2530, 2534, 2544, 2546, 2548, 2554, 2555, 2561,
218736 2563, 2565, 2575, 2579, 2602, 2610, 2613, 2616, 2620, 2622,
218737 2625, 2631, 2635, 2641, 2649, 2654, 2662, 2672, 2674, 2677,
218738 2689, 2691, 2693, 2703, 2707, 2730, 2738, 2741, 2748, 2749,
218739 2750, 2753, 2759, 2761, 2763, 2765, 2768, 2784, 2786, 2790,
218740 2800, 2801, 2817, 2818, 2821, 2831, 2835, 2858, 2866, 2869,
218741 2876, 2877, 2878, 2879, 2880, 2881, 2887, 2891, 2893, 2902,
218742 2903, 2908, 2911, 2914, 2918, 2928, 2929, 2930, 2946, 2947,
218743 2949, 2958, 2962, 2969, 2972, 2974, 2979, 2984, 2990, 3006,
218744 3008, 3009, 3014, 3018, 3021, 3024, 3031, 3046, 3056, 3059,
218745 3065, 3066, 3073, 3077, 3086, 3090, 3114, 3125, 3133, 3134,
218746 3137, 3142, 3146, 3157, 3160, 3168, 3170, 3174, 3192, 3199,
218747 3202, 3205, 3214, 3218, 3242, 3253, 3260, 3261, 3262, 3263,
218748 3264, 3270, 3271, 3274, 3276, 3285, 3294, 3296, 3298, 3302,
218749 3313, 3330, 3333, 3342, 3346, 3389, 3390, 3393, 3398, 3402,
218750 3405, 3406, 3415, 3424, 3426, 3430, 3440, 3449, 3450, 3458,
218751 3461, 3482, 3507, 3517, 3520, 3530, 3535, 3538, 3542, 3544,
218752 3570, 3572, 3585, 3633, 3634, 3636, 3647, 3648, 3654, 3655,
218753 3663, 3664, 3674, 3713, 3716, 3719, 3722, 3725, 3732, 3737,
218754 3745, 3749, 3751, 3754, 3757, 3761, 3762, 3764, 3771, 3773,
218755 3776, 3782, 3784, 3792, 3804, 3840, 3841, 3844, 3859, 3860,
218756 3861, 3864, 3866, 3872, 3882, 3892, 3893, 3894, 3895, 3896,
218757 3897, 3898, 3899, 3900, 3901, 3902, 3904, 3913, 3953, 3967,
218758 3968, 3973, 3974, 3976, 3981, 3993, 4030, 4038, 4039, 4046,
218759 4048, 4053, 4057, 4096, 4139, 4141, 4145, 4146, 4152, 4153,
218760 4155, 4157, 4159, 4160, 4170, 4176, 4182, 4184, 4186, 4190,
218761 4193, 4194, 4197, 4199, 4206, 4209, 4213, 4226, 4227, 4229,
218762 4231, 4237, 4238, 4239, 4240, 4250, 4253, 4254, 4256, 4295,
218763 4301, 4304, 4347, 4348, 4349, 4682, 4688, 4696, 4698, 4704,
218764 4746, 4752, 4786, 4792, 4800, 4802, 4808, 4824, 4882, 4888,
218765 4957, 4960, 4969, 4992, 5008, 5024, 5120, 5121, 5741, 5743,
218766 5760, 5761, 5787, 5788, 5792, 5867, 5870, 5888, 5902, 5906,
218767 5920, 5938, 5941, 5952, 5970, 5984, 5998, 6002, 6016, 6068,
218768 6070, 6071, 6078, 6086, 6087, 6089, 6100, 6103, 6104, 6107,
218769 6108, 6109, 6112, 6128, 6144, 6150, 6151, 6155, 6158, 6160,
218770 6176, 6211, 6212, 6272, 6313, 6314, 6320, 6400, 6432, 6435,
218771 6439, 6441, 6448, 6450, 6451, 6457, 6464, 6468, 6470, 6480,
218772 6512, 6528, 6576, 6593, 6600, 6608, 6618, 6622, 6656, 6679,
218773 6681, 6686, 6688, 6741, 6742, 6743, 6744, 6752, 6753, 6754,
218774 6755, 6757, 6765, 6771, 6783, 6784, 6800, 6816, 6823, 6824,
218775 6912, 6916, 6917, 6964, 6965, 6966, 6971, 6972, 6973, 6978,
218776 6979, 6981, 6992, 7002, 7009, 7019, 7028, 7040, 7042, 7043,
218777 7073, 7074, 7078, 7080, 7082, 7083, 7084, 7086, 7088, 7098,
218778 7142, 7143, 7144, 7146, 7149, 7150, 7151, 7154, 7164, 7168,
218779 7204, 7212, 7220, 7222, 7227, 7232, 7245, 7248, 7258, 7288,
218780 7294, 7360, 7376, 7379, 7380, 7393, 7394, 7401, 7405, 7406,
218781 7410, 7412, 7413, 7424, 7468, 7531, 7544, 7545, 7579, 7616,
218782 7676, 7680, 7830, 7838, 7936, 7944, 7952, 7960, 7968, 7976,
218783 7984, 7992, 8000, 8008, 8016, 8025, 8027, 8029, 8031, 8033,
218784 8040, 8048, 8064, 8072, 8080, 8088, 8096, 8104, 8112, 8118,
218785 8120, 8124, 8125, 8126, 8127, 8130, 8134, 8136, 8140, 8141,
218786 8144, 8150, 8152, 8157, 8160, 8168, 8173, 8178, 8182, 8184,
218787 8188, 8189, 8192, 8203, 8208, 8214, 8216, 8217, 8218, 8219,
218788 8221, 8222, 8223, 8224, 8232, 8233, 8234, 8239, 8240, 8249,
218789 8250, 8251, 8255, 8257, 8260, 8261, 8262, 8263, 8274, 8275,
218790 8276, 8277, 8287, 8288, 8298, 8304, 8305, 8308, 8314, 8317,
218791 8318, 8319, 8320, 8330, 8333, 8334, 8336, 8352, 8400, 8413,
218792 8417, 8418, 8421, 8448, 8450, 8451, 8455, 8456, 8458, 8459,
218793 8462, 8464, 8467, 8468, 8469, 8470, 8472, 8473, 8478, 8484,
218794 8485, 8486, 8487, 8488, 8489, 8490, 8494, 8495, 8496, 8500,
218795 8501, 8505, 8506, 8508, 8510, 8512, 8517, 8519, 8522, 8523,
218796 8524, 8526, 8527, 8528, 8544, 8579, 8581, 8585, 8592, 8597,
218797 8602, 8604, 8608, 8609, 8611, 8612, 8614, 8615, 8622, 8623,
218798 8654, 8656, 8658, 8659, 8660, 8661, 8692, 8960, 8968, 8972,
218799 8992, 8994, 9001, 9002, 9003, 9084, 9085, 9115, 9140, 9180,
218800 9186, 9216, 9280, 9312, 9372, 9450, 9472, 9655, 9656, 9665,
218801 9666, 9720, 9728, 9839, 9840, 9985, 10088, 10089, 10090, 10091,
218802 10092, 10093, 10094, 10095, 10096, 10097, 10098, 10099, 10100, 10101,
218803 10102, 10132, 10176, 10181, 10182, 10183, 10214, 10215, 10216, 10217,
218804 10218, 10219, 10220, 10221, 10222, 10223, 10224, 10240, 10496, 10627,
218805 10628, 10629, 10630, 10631, 10632, 10633, 10634, 10635, 10636, 10637,
218806 10638, 10639, 10640, 10641, 10642, 10643, 10644, 10645, 10646, 10647,
218807 10648, 10649, 10712, 10713, 10714, 10715, 10716, 10748, 10749, 10750,
218808 11008, 11056, 11077, 11079, 11088, 11264, 11312, 11360, 11363, 11365,
218809 11367, 11374, 11377, 11378, 11380, 11381, 11383, 11388, 11390, 11393,
218810 11394, 11492, 11493, 11499, 11503, 11506, 11513, 11517, 11518, 11520,
218811 11559, 11565, 11568, 11631, 11632, 11647, 11648, 11680, 11688, 11696,
218812 11704, 11712, 11720, 11728, 11736, 11744, 11776, 11778, 11779, 11780,
218813 11781, 11782, 11785, 11786, 11787, 11788, 11789, 11790, 11799, 11800,
218814 11802, 11803, 11804, 11805, 11806, 11808, 11809, 11810, 11811, 11812,
218815 11813, 11814, 11815, 11816, 11817, 11818, 11823, 11824, 11834, 11904,
218816 11931, 12032, 12272, 12288, 12289, 12292, 12293, 12294, 12295, 12296,
218817 12297, 12298, 12299, 12300, 12301, 12302, 12303, 12304, 12305, 12306,
218818 12308, 12309, 12310, 12311, 12312, 12313, 12314, 12315, 12316, 12317,
218819 12318, 12320, 12321, 12330, 12334, 12336, 12337, 12342, 12344, 12347,
218820 12348, 12349, 12350, 12353, 12441, 12443, 12445, 12447, 12448, 12449,
218821 12539, 12540, 12543, 12549, 12593, 12688, 12690, 12694, 12704, 12736,
218822 12784, 12800, 12832, 12842, 12872, 12880, 12881, 12896, 12928, 12938,
218823 12977, 12992, 13056, 13312, 19893, 19904, 19968, 40908, 40960, 40981,
218824 40982, 42128, 42192, 42232, 42238, 42240, 42508, 42509, 42512, 42528,
218825 42538, 42560, 42606, 42607, 42608, 42611, 42612, 42622, 42623, 42624,
218826 42655, 42656, 42726, 42736, 42738, 42752, 42775, 42784, 42786, 42800,
218827 42802, 42864, 42865, 42873, 42878, 42888, 42889, 42891, 42896, 42912,
218828 43000, 43002, 43003, 43010, 43011, 43014, 43015, 43019, 43020, 43043,
218829 43045, 43047, 43048, 43056, 43062, 43064, 43065, 43072, 43124, 43136,
218830 43138, 43188, 43204, 43214, 43216, 43232, 43250, 43256, 43259, 43264,
218831 43274, 43302, 43310, 43312, 43335, 43346, 43359, 43360, 43392, 43395,
218832 43396, 43443, 43444, 43446, 43450, 43452, 43453, 43457, 43471, 43472,
218833 43486, 43520, 43561, 43567, 43569, 43571, 43573, 43584, 43587, 43588,
218834 43596, 43597, 43600, 43612, 43616, 43632, 43633, 43639, 43642, 43643,
218835 43648, 43696, 43697, 43698, 43701, 43703, 43705, 43710, 43712, 43713,
218836 43714, 43739, 43741, 43742, 43744, 43755, 43756, 43758, 43760, 43762,
218837 43763, 43765, 43766, 43777, 43785, 43793, 43808, 43816, 43968, 44003,
218838 44005, 44006, 44008, 44009, 44011, 44012, 44013, 44016, 44032, 55203,
218839 55216, 55243, 55296, 56191, 56319, 57343, 57344, 63743, 63744, 64112,
218840 64256, 64275, 64285, 64286, 64287, 64297, 64298, 64312, 64318, 64320,
218841 64323, 64326, 64434, 64467, 64830, 64831, 64848, 64914, 65008, 65020,
218842 65021, 65024, 65040, 65047, 65048, 65049, 65056, 65072, 65073, 65075,
218843 65077, 65078, 65079, 65080, 65081, 65082, 65083, 65084, 65085, 65086,
218844 65087, 65088, 65089, 65090, 65091, 65092, 65093, 65095, 65096, 65097,
218845 65101, 65104, 65108, 65112, 65113, 65114, 65115, 65116, 65117, 65118,
218846 65119, 65122, 65123, 65124, 65128, 65129, 65130, 65136, 65142, 65279,
218847 65281, 65284, 65285, 65288, 65289, 65290, 65291, 65292, 65293, 65294,
218848 65296, 65306, 65308, 65311, 65313, 65339, 65340, 65341, 65342, 65343,
218849 65344, 65345, 65371, 65372, 65373, 65374, 65375, 65376, 65377, 65378,
218850 65379, 65380, 65382, 65392, 65393, 65438, 65440, 65474, 65482, 65490,
218851 65498, 65504, 65506, 65507, 65508, 65509, 65512, 65513, 65517, 65529,
218852 65532, 0, 13, 40, 60, 63, 80, 128, 256, 263,
218853 311, 320, 373, 377, 394, 400, 464, 509, 640, 672,
218854 768, 800, 816, 833, 834, 842, 896, 927, 928, 968,
218855 976, 977, 1024, 1064, 1104, 1184, 2048, 2056, 2058, 2103,
218856 2108, 2111, 2135, 2136, 2304, 2326, 2335, 2336, 2367, 2432,
218857 2494, 2560, 2561, 2565, 2572, 2576, 2581, 2585, 2616, 2623,
218858 2624, 2640, 2656, 2685, 2687, 2816, 2873, 2880, 2904, 2912,
218859 2936, 3072, 3680, 4096, 4097, 4098, 4099, 4152, 4167, 4178,
218860 4198, 4224, 4226, 4227, 4272, 4275, 4279, 4281, 4283, 4285,
218861 4286, 4304, 4336, 4352, 4355, 4391, 4396, 4397, 4406, 4416,
218862 4480, 4482, 4483, 4531, 4534, 4543, 4545, 4549, 4560, 5760,
218863 5803, 5804, 5805, 5806, 5808, 5814, 5815, 5824, 8192, 9216,
218864 9328, 12288, 26624, 28416, 28496, 28497, 28559, 28563, 45056, 53248,
218865 53504, 53545, 53605, 53607, 53610, 53613, 53619, 53627, 53635, 53637,
218866 53644, 53674, 53678, 53760, 53826, 53829, 54016, 54112, 54272, 54298,
218867 54324, 54350, 54358, 54376, 54402, 54428, 54430, 54434, 54437, 54441,
218868 54446, 54454, 54459, 54461, 54469, 54480, 54506, 54532, 54535, 54541,
218869 54550, 54558, 54584, 54587, 54592, 54598, 54602, 54610, 54636, 54662,
218870 54688, 54714, 54740, 54766, 54792, 54818, 54844, 54870, 54896, 54922,
218871 54952, 54977, 54978, 55003, 55004, 55010, 55035, 55036, 55061, 55062,
218872 55068, 55093, 55094, 55119, 55120, 55126, 55151, 55152, 55177, 55178,
218873 55184, 55209, 55210, 55235, 55236, 55242, 55246, 60928, 60933, 60961,
218874 60964, 60967, 60969, 60980, 60985, 60987, 60994, 60999, 61001, 61003,
218875 61005, 61009, 61012, 61015, 61017, 61019, 61021, 61023, 61025, 61028,
218876 61031, 61036, 61044, 61049, 61054, 61056, 61067, 61089, 61093, 61099,
218877 61168, 61440, 61488, 61600, 61617, 61633, 61649, 61696, 61712, 61744,
218878 61808, 61926, 61968, 62016, 62032, 62208, 62256, 62263, 62336, 62368,
218879 62406, 62432, 62464, 62528, 62530, 62713, 62720, 62784, 62800, 62971,
218880 63045, 63104, 63232, 0, 42710, 42752, 46900, 46912, 47133, 63488,
218881 1, 32, 256, 0, 65533,
218882 };
218883static u16 aFts5UnicodeData[] = {
218884 1025, 61, 117, 55, 117, 54, 50, 53, 57, 53,
218885 49, 85, 333, 85, 121, 85, 841, 54, 53, 50,
218886 56, 48, 56, 837, 54, 57, 50, 57, 1057, 61,
218887 53, 151, 58, 53, 56, 58, 39, 52, 57, 34,
218888 58, 56, 58, 57, 79, 56, 37, 85, 56, 47,
218889 39, 51, 111, 53, 745, 57, 233, 773, 57, 261,
218890 1822, 37, 542, 37, 1534, 222, 69, 73, 37, 126,
218891 126, 73, 69, 137, 37, 73, 37, 105, 101, 73,
218892 37, 73, 37, 190, 158, 37, 126, 126, 73, 37,
218893 126, 94, 37, 39, 94, 69, 135, 41, 40, 37,
218894 41, 40, 37, 41, 40, 37, 542, 37, 606, 37,
218895 41, 40, 37, 126, 73, 37, 1886, 197, 73, 37,
218896 73, 69, 126, 105, 37, 286, 2181, 39, 869, 582,
218897 152, 390, 472, 166, 248, 38, 56, 38, 568, 3596,
218898 158, 38, 56, 94, 38, 101, 53, 88, 41, 53,
218899 105, 41, 73, 37, 553, 297, 1125, 94, 37, 105,
218900 101, 798, 133, 94, 57, 126, 94, 37, 1641, 1541,
218901 1118, 58, 172, 75, 1790, 478, 37, 2846, 1225, 38,
218902 213, 1253, 53, 49, 55, 1452, 49, 44, 53, 76,
218903 53, 76, 53, 44, 871, 103, 85, 162, 121, 85,
218904 55, 85, 90, 364, 53, 85, 1031, 38, 327, 684,
218905 333, 149, 71, 44, 3175, 53, 39, 236, 34, 58,
218906 204, 70, 76, 58, 140, 71, 333, 103, 90, 39,
218907 469, 34, 39, 44, 967, 876, 2855, 364, 39, 333,
218908 1063, 300, 70, 58, 117, 38, 711, 140, 38, 300,
218909 38, 108, 38, 172, 501, 807, 108, 53, 39, 359,
218910 876, 108, 42, 1735, 44, 42, 44, 39, 106, 268,
218911 138, 44, 74, 39, 236, 327, 76, 85, 333, 53,
218912 38, 199, 231, 44, 74, 263, 71, 711, 231, 39,
218913 135, 44, 39, 106, 140, 74, 74, 44, 39, 42,
218914 71, 103, 76, 333, 71, 87, 207, 58, 55, 76,
218915 42, 199, 71, 711, 231, 71, 71, 71, 44, 106,
218916 76, 76, 108, 44, 135, 39, 333, 76, 103, 44,
218917 76, 42, 295, 103, 711, 231, 71, 167, 44, 39,
218918 106, 172, 76, 42, 74, 44, 39, 71, 76, 333,
218919 53, 55, 44, 74, 263, 71, 711, 231, 71, 167,
218920 44, 39, 42, 44, 42, 140, 74, 74, 44, 44,
218921 42, 71, 103, 76, 333, 58, 39, 207, 44, 39,
218922 199, 103, 135, 71, 39, 71, 71, 103, 391, 74,
218923 44, 74, 106, 106, 44, 39, 42, 333, 111, 218,
218924 55, 58, 106, 263, 103, 743, 327, 167, 39, 108,
218925 138, 108, 140, 76, 71, 71, 76, 333, 239, 58,
218926 74, 263, 103, 743, 327, 167, 44, 39, 42, 44,
218927 170, 44, 74, 74, 76, 74, 39, 71, 76, 333,
218928 71, 74, 263, 103, 1319, 39, 106, 140, 106, 106,
218929 44, 39, 42, 71, 76, 333, 207, 58, 199, 74,
218930 583, 775, 295, 39, 231, 44, 106, 108, 44, 266,
218931 74, 53, 1543, 44, 71, 236, 55, 199, 38, 268,
218932 53, 333, 85, 71, 39, 71, 39, 39, 135, 231,
218933 103, 39, 39, 71, 135, 44, 71, 204, 76, 39,
218934 167, 38, 204, 333, 135, 39, 122, 501, 58, 53,
218935 122, 76, 218, 333, 335, 58, 44, 58, 44, 58,
218936 44, 54, 50, 54, 50, 74, 263, 1159, 460, 42,
218937 172, 53, 76, 167, 364, 1164, 282, 44, 218, 90,
218938 181, 154, 85, 1383, 74, 140, 42, 204, 42, 76,
218939 74, 76, 39, 333, 213, 199, 74, 76, 135, 108,
218940 39, 106, 71, 234, 103, 140, 423, 44, 74, 76,
218941 202, 44, 39, 42, 333, 106, 44, 90, 1225, 41,
218942 41, 1383, 53, 38, 10631, 135, 231, 39, 135, 1319,
218943 135, 1063, 135, 231, 39, 135, 487, 1831, 135, 2151,
218944 108, 309, 655, 519, 346, 2727, 49, 19847, 85, 551,
218945 61, 839, 54, 50, 2407, 117, 110, 423, 135, 108,
218946 583, 108, 85, 583, 76, 423, 103, 76, 1671, 76,
218947 42, 236, 266, 44, 74, 364, 117, 38, 117, 55,
218948 39, 44, 333, 335, 213, 49, 149, 108, 61, 333,
218949 1127, 38, 1671, 1319, 44, 39, 2247, 935, 108, 138,
218950 76, 106, 74, 44, 202, 108, 58, 85, 333, 967,
218951 167, 1415, 554, 231, 74, 333, 47, 1114, 743, 76,
218952 106, 85, 1703, 42, 44, 42, 236, 44, 42, 44,
218953 74, 268, 202, 332, 44, 333, 333, 245, 38, 213,
218954 140, 42, 1511, 44, 42, 172, 42, 44, 170, 44,
218955 74, 231, 333, 245, 346, 300, 314, 76, 42, 967,
218956 42, 140, 74, 76, 42, 44, 74, 71, 333, 1415,
218957 44, 42, 76, 106, 44, 42, 108, 74, 149, 1159,
218958 266, 268, 74, 76, 181, 333, 103, 333, 967, 198,
218959 85, 277, 108, 53, 428, 42, 236, 135, 44, 135,
218960 74, 44, 71, 1413, 2022, 421, 38, 1093, 1190, 1260,
218961 140, 4830, 261, 3166, 261, 265, 197, 201, 261, 265,
218962 261, 265, 197, 201, 261, 41, 41, 41, 94, 229,
218963 265, 453, 261, 264, 261, 264, 261, 264, 165, 69,
218964 137, 40, 56, 37, 120, 101, 69, 137, 40, 120,
218965 133, 69, 137, 120, 261, 169, 120, 101, 69, 137,
218966 40, 88, 381, 162, 209, 85, 52, 51, 54, 84,
218967 51, 54, 52, 277, 59, 60, 162, 61, 309, 52,
218968 51, 149, 80, 117, 57, 54, 50, 373, 57, 53,
218969 48, 341, 61, 162, 194, 47, 38, 207, 121, 54,
218970 50, 38, 335, 121, 54, 50, 422, 855, 428, 139,
218971 44, 107, 396, 90, 41, 154, 41, 90, 37, 105,
218972 69, 105, 37, 58, 41, 90, 57, 169, 218, 41,
218973 58, 41, 58, 41, 58, 137, 58, 37, 137, 37,
218974 135, 37, 90, 69, 73, 185, 94, 101, 58, 57,
218975 90, 37, 58, 527, 1134, 94, 142, 47, 185, 186,
218976 89, 154, 57, 90, 57, 90, 57, 250, 57, 1018,
218977 89, 90, 57, 58, 57, 1018, 8601, 282, 153, 666,
218978 89, 250, 54, 50, 2618, 57, 986, 825, 1306, 217,
218979 602, 1274, 378, 1935, 2522, 719, 5882, 57, 314, 57,
218980 1754, 281, 3578, 57, 4634, 3322, 54, 50, 54, 50,
218981 54, 50, 54, 50, 54, 50, 54, 50, 54, 50,
218982 975, 1434, 185, 54, 50, 1017, 54, 50, 54, 50,
218983 54, 50, 54, 50, 54, 50, 537, 8218, 4217, 54,
218984 50, 54, 50, 54, 50, 54, 50, 54, 50, 54,
218985 50, 54, 50, 54, 50, 54, 50, 54, 50, 54,
218986 50, 2041, 54, 50, 54, 50, 1049, 54, 50, 8281,
218987 1562, 697, 90, 217, 346, 1513, 1509, 126, 73, 69,
218988 254, 105, 37, 94, 37, 94, 165, 70, 105, 37,
218989 3166, 37, 218, 158, 108, 94, 149, 47, 85, 1221,
218990 37, 37, 1799, 38, 53, 44, 743, 231, 231, 231,
218991 231, 231, 231, 231, 231, 1036, 85, 52, 51, 52,
218992 51, 117, 52, 51, 53, 52, 51, 309, 49, 85,
218993 49, 53, 52, 51, 85, 52, 51, 54, 50, 54,
218994 50, 54, 50, 54, 50, 181, 38, 341, 81, 858,
218995 2874, 6874, 410, 61, 117, 58, 38, 39, 46, 54,
218996 50, 54, 50, 54, 50, 54, 50, 54, 50, 90,
218997 54, 50, 54, 50, 54, 50, 54, 50, 49, 54,
218998 82, 58, 302, 140, 74, 49, 166, 90, 110, 38,
218999 39, 53, 90, 2759, 76, 88, 70, 39, 49, 2887,
219000 53, 102, 39, 1319, 3015, 90, 143, 346, 871, 1178,
219001 519, 1018, 335, 986, 271, 58, 495, 1050, 335, 1274,
219002 495, 2042, 8218, 39, 39, 2074, 39, 39, 679, 38,
219003 36583, 1786, 1287, 198, 85, 8583, 38, 117, 519, 333,
219004 71, 1502, 39, 44, 107, 53, 332, 53, 38, 798,
219005 44, 2247, 334, 76, 213, 760, 294, 88, 478, 69,
219006 2014, 38, 261, 190, 350, 38, 88, 158, 158, 382,
219007 70, 37, 231, 44, 103, 44, 135, 44, 743, 74,
219008 76, 42, 154, 207, 90, 55, 58, 1671, 149, 74,
219009 1607, 522, 44, 85, 333, 588, 199, 117, 39, 333,
219010 903, 268, 85, 743, 364, 74, 53, 935, 108, 42,
219011 1511, 44, 74, 140, 74, 44, 138, 437, 38, 333,
219012 85, 1319, 204, 74, 76, 74, 76, 103, 44, 263,
219013 44, 42, 333, 149, 519, 38, 199, 122, 39, 42,
219014 1543, 44, 39, 108, 71, 76, 167, 76, 39, 44,
219015 39, 71, 38, 85, 359, 42, 76, 74, 85, 39,
219016 70, 42, 44, 199, 199, 199, 231, 231, 1127, 74,
219017 44, 74, 44, 74, 53, 42, 44, 333, 39, 39,
219018 743, 1575, 36, 68, 68, 36, 63, 63, 11719, 3399,
219019 229, 165, 39, 44, 327, 57, 423, 167, 39, 71,
219020 71, 3463, 536, 11623, 54, 50, 2055, 1735, 391, 55,
219021 58, 524, 245, 54, 50, 53, 236, 53, 81, 80,
219022 54, 50, 54, 50, 54, 50, 54, 50, 54, 50,
219023 54, 50, 54, 50, 54, 50, 85, 54, 50, 149,
219024 112, 117, 149, 49, 54, 50, 54, 50, 54, 50,
219025 117, 57, 49, 121, 53, 55, 85, 167, 4327, 34,
219026 117, 55, 117, 54, 50, 53, 57, 53, 49, 85,
219027 333, 85, 121, 85, 841, 54, 53, 50, 56, 48,
219028 56, 837, 54, 57, 50, 57, 54, 50, 53, 54,
219029 50, 85, 327, 38, 1447, 70, 999, 199, 199, 199,
219030 103, 87, 57, 56, 58, 87, 58, 153, 90, 98,
219031 90, 391, 839, 615, 71, 487, 455, 3943, 117, 1455,
219032 314, 1710, 143, 570, 47, 410, 1466, 44, 935, 1575,
219033 999, 143, 551, 46, 263, 46, 967, 53, 1159, 263,
219034 53, 174, 1289, 1285, 2503, 333, 199, 39, 1415, 71,
219035 39, 743, 53, 271, 711, 207, 53, 839, 53, 1799,
219036 71, 39, 108, 76, 140, 135, 103, 871, 108, 44,
219037 271, 309, 935, 79, 53, 1735, 245, 711, 271, 615,
219038 271, 2343, 1007, 42, 44, 42, 1703, 492, 245, 655,
219039 333, 76, 42, 1447, 106, 140, 74, 76, 85, 34,
219040 149, 807, 333, 108, 1159, 172, 42, 268, 333, 149,
219041 76, 42, 1543, 106, 300, 74, 135, 149, 333, 1383,
219042 44, 42, 44, 74, 204, 42, 44, 333, 28135, 3182,
219043 149, 34279, 18215, 2215, 39, 1482, 140, 422, 71, 7898,
219044 1274, 1946, 74, 108, 122, 202, 258, 268, 90, 236,
219045 986, 140, 1562, 2138, 108, 58, 2810, 591, 841, 837,
219046 841, 229, 581, 841, 837, 41, 73, 41, 73, 137,
219047 265, 133, 37, 229, 357, 841, 837, 73, 137, 265,
219048 233, 837, 73, 137, 169, 41, 233, 837, 841, 837,
219049 841, 837, 841, 837, 841, 837, 841, 837, 841, 901,
219050 809, 57, 805, 57, 197, 809, 57, 805, 57, 197,
219051 809, 57, 805, 57, 197, 809, 57, 805, 57, 197,
219052 809, 57, 805, 57, 197, 94, 1613, 135, 871, 71,
219053 39, 39, 327, 135, 39, 39, 39, 39, 39, 39,
219054 103, 71, 39, 39, 39, 39, 39, 39, 71, 39,
219055 135, 231, 135, 135, 39, 327, 551, 103, 167, 551,
219056 89, 1434, 3226, 506, 474, 506, 506, 367, 1018, 1946,
219057 1402, 954, 1402, 314, 90, 1082, 218, 2266, 666, 1210,
219058 186, 570, 2042, 58, 5850, 154, 2010, 154, 794, 2266,
219059 378, 2266, 3738, 39, 39, 39, 39, 39, 39, 17351,
219060 34, 3074, 7692, 63, 63,
219061 };
219062
219063static int sqlite3Fts5UnicodeCategory(int iCode) {
219064 int iRes = -1;
219065 int iHi;
219066 int iLo;
219067 int ret;
219068 u16 iKey;
219069
219070 if( iCode>=(1<<20) ){
219071 return 0;
219072 }
219073 iLo = aFts5UnicodeBlock[(iCode>>16)];
219074 iHi = aFts5UnicodeBlock[1+(iCode>>16)];
219075 iKey = (iCode & 0xFFFF);
219076 while( iHi>iLo ){
219077 int iTest = (iHi + iLo) / 2;
219078 assert( iTest>=iLo && iTest<iHi );
219079 if( iKey>=aFts5UnicodeMap[iTest] ){
219080 iRes = iTest;
219081 iLo = iTest+1;
219082 }else{
219083 iHi = iTest;
219084 }
219085 }
219086
219087 if( iRes<0 ) return 0;
219088 if( iKey>=(aFts5UnicodeMap[iRes]+(aFts5UnicodeData[iRes]>>5)) ) return 0;
219089 ret = aFts5UnicodeData[iRes] & 0x1F;
219090 if( ret!=30 ) return ret;
219091 return ((iKey - aFts5UnicodeMap[iRes]) & 0x01) ? 5 : 9;
219092}
219093
219094static void sqlite3Fts5UnicodeAscii(u8 *aArray, u8 *aAscii){
219095 int i = 0;
219096 int iTbl = 0;
219097 while( i<128 ){
219098 int bToken = aArray[ aFts5UnicodeData[iTbl] & 0x1F ];
219099 int n = (aFts5UnicodeData[iTbl] >> 5) + i;
219100 for(; i<128 && i<n; i++){
219101 aAscii[i] = (u8)bToken;
219102 }
219103 iTbl++;
219104 }
219105}
219106
219107
219108/*
219109** 2015 May 30
219110**
219111** The author disclaims copyright to this source code. In place of
219112** a legal notice, here is a blessing:
219113**
219114** May you do good and not evil.
219115** May you find forgiveness for yourself and forgive others.
219116** May you share freely, never taking more than you give.
219117**
219118******************************************************************************
219119**
219120** Routines for varint serialization and deserialization.
219121*/
219122
219123
219124/* #include "fts5Int.h" */
219125
219126/*
219127** This is a copy of the sqlite3GetVarint32() routine from the SQLite core.
219128** Except, this version does handle the single byte case that the core
219129** version depends on being handled before its function is called.
219130*/
219131static int sqlite3Fts5GetVarint32(const unsigned char *p, u32 *v){
219132 u32 a,b;
219133
219134 /* The 1-byte case. Overwhelmingly the most common. */
219135 a = *p;
219136 /* a: p0 (unmasked) */
219137 if (!(a&0x80))
219138 {
219139 /* Values between 0 and 127 */
219140 *v = a;
219141 return 1;
219142 }
219143
219144 /* The 2-byte case */
219145 p++;
219146 b = *p;
219147 /* b: p1 (unmasked) */
219148 if (!(b&0x80))
219149 {
219150 /* Values between 128 and 16383 */
219151 a &= 0x7f;
219152 a = a<<7;
219153 *v = a | b;
219154 return 2;
219155 }
219156
219157 /* The 3-byte case */
219158 p++;
219159 a = a<<14;
219160 a |= *p;
219161 /* a: p0<<14 | p2 (unmasked) */
219162 if (!(a&0x80))
219163 {
219164 /* Values between 16384 and 2097151 */
219165 a &= (0x7f<<14)|(0x7f);
219166 b &= 0x7f;
219167 b = b<<7;
219168 *v = a | b;
219169 return 3;
219170 }
219171
219172 /* A 32-bit varint is used to store size information in btrees.
219173 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
219174 ** A 3-byte varint is sufficient, for example, to record the size
219175 ** of a 1048569-byte BLOB or string.
219176 **
219177 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
219178 ** rare larger cases can be handled by the slower 64-bit varint
219179 ** routine.
219180 */
219181 {
219182 u64 v64;
219183 u8 n;
219184 p -= 2;
219185 n = sqlite3Fts5GetVarint(p, &v64);
219186 *v = (u32)v64;
219187 assert( n>3 && n<=9 );
219188 return n;
219189 }
219190}
219191
219192
219193/*
219194** Bitmasks used by sqlite3GetVarint(). These precomputed constants
219195** are defined here rather than simply putting the constant expressions
219196** inline in order to work around bugs in the RVT compiler.
219197**
219198** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
219199**
219200** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
219201*/
219202#define SLOT_2_0 0x001fc07f
219203#define SLOT_4_2_0 0xf01fc07f
219204
219205/*
219206** Read a 64-bit variable-length integer from memory starting at p[0].
219207** Return the number of bytes read. The value is stored in *v.
219208*/
219209static u8 sqlite3Fts5GetVarint(const unsigned char *p, u64 *v){
219210 u32 a,b,s;
219211
219212 a = *p;
219213 /* a: p0 (unmasked) */
219214 if (!(a&0x80))
219215 {
219216 *v = a;
219217 return 1;
219218 }
219219
219220 p++;
219221 b = *p;
219222 /* b: p1 (unmasked) */
219223 if (!(b&0x80))
219224 {
219225 a &= 0x7f;
219226 a = a<<7;
219227 a |= b;
219228 *v = a;
219229 return 2;
219230 }
219231
219232 /* Verify that constants are precomputed correctly */
219233 assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
219234 assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
219235
219236 p++;
219237 a = a<<14;
219238 a |= *p;
219239 /* a: p0<<14 | p2 (unmasked) */
219240 if (!(a&0x80))
219241 {
219242 a &= SLOT_2_0;
219243 b &= 0x7f;
219244 b = b<<7;
219245 a |= b;
219246 *v = a;
219247 return 3;
219248 }
219249
219250 /* CSE1 from below */
219251 a &= SLOT_2_0;
219252 p++;
219253 b = b<<14;
219254 b |= *p;
219255 /* b: p1<<14 | p3 (unmasked) */
219256 if (!(b&0x80))
219257 {
219258 b &= SLOT_2_0;
219259 /* moved CSE1 up */
219260 /* a &= (0x7f<<14)|(0x7f); */
219261 a = a<<7;
219262 a |= b;
219263 *v = a;
219264 return 4;
219265 }
219266
219267 /* a: p0<<14 | p2 (masked) */
219268 /* b: p1<<14 | p3 (unmasked) */
219269 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
219270 /* moved CSE1 up */
219271 /* a &= (0x7f<<14)|(0x7f); */
219272 b &= SLOT_2_0;
219273 s = a;
219274 /* s: p0<<14 | p2 (masked) */
219275
219276 p++;
219277 a = a<<14;
219278 a |= *p;
219279 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
219280 if (!(a&0x80))
219281 {
219282 /* we can skip these cause they were (effectively) done above in calc'ing s */
219283 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
219284 /* b &= (0x7f<<14)|(0x7f); */
219285 b = b<<7;
219286 a |= b;
219287 s = s>>18;
219288 *v = ((u64)s)<<32 | a;
219289 return 5;
219290 }
219291
219292 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
219293 s = s<<7;
219294 s |= b;
219295 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
219296
219297 p++;
219298 b = b<<14;
219299 b |= *p;
219300 /* b: p1<<28 | p3<<14 | p5 (unmasked) */
219301 if (!(b&0x80))
219302 {
219303 /* we can skip this cause it was (effectively) done above in calc'ing s */
219304 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
219305 a &= SLOT_2_0;
219306 a = a<<7;
219307 a |= b;
219308 s = s>>18;
219309 *v = ((u64)s)<<32 | a;
219310 return 6;
219311 }
219312
219313 p++;
219314 a = a<<14;
219315 a |= *p;
219316 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
219317 if (!(a&0x80))
219318 {
219319 a &= SLOT_4_2_0;
219320 b &= SLOT_2_0;
219321 b = b<<7;
219322 a |= b;
219323 s = s>>11;
219324 *v = ((u64)s)<<32 | a;
219325 return 7;
219326 }
219327
219328 /* CSE2 from below */
219329 a &= SLOT_2_0;
219330 p++;
219331 b = b<<14;
219332 b |= *p;
219333 /* b: p3<<28 | p5<<14 | p7 (unmasked) */
219334 if (!(b&0x80))
219335 {
219336 b &= SLOT_4_2_0;
219337 /* moved CSE2 up */
219338 /* a &= (0x7f<<14)|(0x7f); */
219339 a = a<<7;
219340 a |= b;
219341 s = s>>4;
219342 *v = ((u64)s)<<32 | a;
219343 return 8;
219344 }
219345
219346 p++;
219347 a = a<<15;
219348 a |= *p;
219349 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
219350
219351 /* moved CSE2 up */
219352 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
219353 b &= SLOT_2_0;
219354 b = b<<8;
219355 a |= b;
219356
219357 s = s<<4;
219358 b = p[-4];
219359 b &= 0x7f;
219360 b = b>>3;
219361 s |= b;
219362
219363 *v = ((u64)s)<<32 | a;
219364
219365 return 9;
219366}
219367
219368/*
219369** The variable-length integer encoding is as follows:
219370**
219371** KEY:
219372** A = 0xxxxxxx 7 bits of data and one flag bit
219373** B = 1xxxxxxx 7 bits of data and one flag bit
219374** C = xxxxxxxx 8 bits of data
219375**
219376** 7 bits - A
219377** 14 bits - BA
219378** 21 bits - BBA
219379** 28 bits - BBBA
219380** 35 bits - BBBBA
219381** 42 bits - BBBBBA
219382** 49 bits - BBBBBBA
219383** 56 bits - BBBBBBBA
219384** 64 bits - BBBBBBBBC
219385*/
219386
219387#ifdef SQLITE_NOINLINE
219388# define FTS5_NOINLINE SQLITE_NOINLINE
219389#else
219390# define FTS5_NOINLINE
219391#endif
219392
219393/*
219394** Write a 64-bit variable-length integer to memory starting at p[0].
219395** The length of data write will be between 1 and 9 bytes. The number
219396** of bytes written is returned.
219397**
219398** A variable-length integer consists of the lower 7 bits of each byte
219399** for all bytes that have the 8th bit set and one byte with the 8th
219400** bit clear. Except, if we get to the 9th byte, it stores the full
219401** 8 bits and is the last byte.
219402*/
219403static int FTS5_NOINLINE fts5PutVarint64(unsigned char *p, u64 v){
219404 int i, j, n;
219405 u8 buf[10];
219406 if( v & (((u64)0xff000000)<<32) ){
219407 p[8] = (u8)v;
219408 v >>= 8;
219409 for(i=7; i>=0; i--){
219410 p[i] = (u8)((v & 0x7f) | 0x80);
219411 v >>= 7;
219412 }
219413 return 9;
219414 }
219415 n = 0;
219416 do{
219417 buf[n++] = (u8)((v & 0x7f) | 0x80);
219418 v >>= 7;
219419 }while( v!=0 );
219420 buf[0] &= 0x7f;
219421 assert( n<=9 );
219422 for(i=0, j=n-1; j>=0; j--, i++){
219423 p[i] = buf[j];
219424 }
219425 return n;
219426}
219427
219428static int sqlite3Fts5PutVarint(unsigned char *p, u64 v){
219429 if( v<=0x7f ){
219430 p[0] = v&0x7f;
219431 return 1;
219432 }
219433 if( v<=0x3fff ){
219434 p[0] = ((v>>7)&0x7f)|0x80;
219435 p[1] = v&0x7f;
219436 return 2;
219437 }
219438 return fts5PutVarint64(p,v);
219439}
219440
219441
219442static int sqlite3Fts5GetVarintLen(u32 iVal){
219443#if 0
219444 if( iVal<(1 << 7 ) ) return 1;
219445#endif
219446 assert( iVal>=(1 << 7) );
219447 if( iVal<(1 << 14) ) return 2;
219448 if( iVal<(1 << 21) ) return 3;
219449 if( iVal<(1 << 28) ) return 4;
219450 return 5;
219451}
219452
219453
219454/*
219455** 2015 May 08
219456**
219457** The author disclaims copyright to this source code. In place of
219458** a legal notice, here is a blessing:
219459**
219460** May you do good and not evil.
219461** May you find forgiveness for yourself and forgive others.
219462** May you share freely, never taking more than you give.
219463**
219464******************************************************************************
219465**
219466** This is an SQLite virtual table module implementing direct access to an
219467** existing FTS5 index. The module may create several different types of
219468** tables:
219469**
219470** col:
219471** CREATE TABLE vocab(term, col, doc, cnt, PRIMARY KEY(term, col));
219472**
219473** One row for each term/column combination. The value of $doc is set to
219474** the number of fts5 rows that contain at least one instance of term
219475** $term within column $col. Field $cnt is set to the total number of
219476** instances of term $term in column $col (in any row of the fts5 table).
219477**
219478** row:
219479** CREATE TABLE vocab(term, doc, cnt, PRIMARY KEY(term));
219480**
219481** One row for each term in the database. The value of $doc is set to
219482** the number of fts5 rows that contain at least one instance of term
219483** $term. Field $cnt is set to the total number of instances of term
219484** $term in the database.
219485**
219486** instance:
219487** CREATE TABLE vocab(term, doc, col, offset, PRIMARY KEY(<all-fields>));
219488**
219489** One row for each term instance in the database.
219490*/
219491
219492
219493/* #include "fts5Int.h" */
219494
219495
219496typedef struct Fts5VocabTable Fts5VocabTable;
219497typedef struct Fts5VocabCursor Fts5VocabCursor;
219498
219499struct Fts5VocabTable {
219500 sqlite3_vtab base;
219501 char *zFts5Tbl; /* Name of fts5 table */
219502 char *zFts5Db; /* Db containing fts5 table */
219503 sqlite3 *db; /* Database handle */
219504 Fts5Global *pGlobal; /* FTS5 global object for this database */
219505 int eType; /* FTS5_VOCAB_COL, ROW or INSTANCE */
219506};
219507
219508struct Fts5VocabCursor {
219509 sqlite3_vtab_cursor base;
219510 sqlite3_stmt *pStmt; /* Statement holding lock on pIndex */
219511 Fts5Index *pIndex; /* Associated FTS5 index */
219512
219513 int bEof; /* True if this cursor is at EOF */
219514 Fts5IndexIter *pIter; /* Term/rowid iterator object */
219515
219516 int nLeTerm; /* Size of zLeTerm in bytes */
219517 char *zLeTerm; /* (term <= $zLeTerm) paramater, or NULL */
219518
219519 /* These are used by 'col' tables only */
219520 Fts5Config *pConfig; /* Fts5 table configuration */
219521 int iCol;
219522 i64 *aCnt;
219523 i64 *aDoc;
219524
219525 /* Output values used by all tables. */
219526 i64 rowid; /* This table's current rowid value */
219527 Fts5Buffer term; /* Current value of 'term' column */
219528
219529 /* Output values Used by 'instance' tables only */
219530 i64 iInstPos;
219531 int iInstOff;
219532};
219533
219534#define FTS5_VOCAB_COL 0
219535#define FTS5_VOCAB_ROW 1
219536#define FTS5_VOCAB_INSTANCE 2
219537
219538#define FTS5_VOCAB_COL_SCHEMA "term, col, doc, cnt"
219539#define FTS5_VOCAB_ROW_SCHEMA "term, doc, cnt"
219540#define FTS5_VOCAB_INST_SCHEMA "term, doc, col, offset"
219541
219542/*
219543** Bits for the mask used as the idxNum value by xBestIndex/xFilter.
219544*/
219545#define FTS5_VOCAB_TERM_EQ 0x01
219546#define FTS5_VOCAB_TERM_GE 0x02
219547#define FTS5_VOCAB_TERM_LE 0x04
219548
219549
219550/*
219551** Translate a string containing an fts5vocab table type to an
219552** FTS5_VOCAB_XXX constant. If successful, set *peType to the output
219553** value and return SQLITE_OK. Otherwise, set *pzErr to an error message
219554** and return SQLITE_ERROR.
219555*/
219556static int fts5VocabTableType(const char *zType, char **pzErr, int *peType){
219557 int rc = SQLITE_OK;
219558 char *zCopy = sqlite3Fts5Strndup(&rc, zType, -1);
219559 if( rc==SQLITE_OK ){
219560 sqlite3Fts5Dequote(zCopy);
219561 if( sqlite3_stricmp(zCopy, "col")==0 ){
219562 *peType = FTS5_VOCAB_COL;
219563 }else
219564
219565 if( sqlite3_stricmp(zCopy, "row")==0 ){
219566 *peType = FTS5_VOCAB_ROW;
219567 }else
219568 if( sqlite3_stricmp(zCopy, "instance")==0 ){
219569 *peType = FTS5_VOCAB_INSTANCE;
219570 }else
219571 {
219572 *pzErr = sqlite3_mprintf("fts5vocab: unknown table type: %Q", zCopy);
219573 rc = SQLITE_ERROR;
219574 }
219575 sqlite3_free(zCopy);
219576 }
219577
219578 return rc;
219579}
219580
219581
219582/*
219583** The xDisconnect() virtual table method.
219584*/
219585static int fts5VocabDisconnectMethod(sqlite3_vtab *pVtab){
219586 Fts5VocabTable *pTab = (Fts5VocabTable*)pVtab;
219587 sqlite3_free(pTab);
219588 return SQLITE_OK;
219589}
219590
219591/*
219592** The xDestroy() virtual table method.
219593*/
219594static int fts5VocabDestroyMethod(sqlite3_vtab *pVtab){
219595 Fts5VocabTable *pTab = (Fts5VocabTable*)pVtab;
219596 sqlite3_free(pTab);
219597 return SQLITE_OK;
219598}
219599
219600/*
219601** This function is the implementation of both the xConnect and xCreate
219602** methods of the FTS3 virtual table.
219603**
219604** The argv[] array contains the following:
219605**
219606** argv[0] -> module name ("fts5vocab")
219607** argv[1] -> database name
219608** argv[2] -> table name
219609**
219610** then:
219611**
219612** argv[3] -> name of fts5 table
219613** argv[4] -> type of fts5vocab table
219614**
219615** or, for tables in the TEMP schema only.
219616**
219617** argv[3] -> name of fts5 tables database
219618** argv[4] -> name of fts5 table
219619** argv[5] -> type of fts5vocab table
219620*/
219621static int fts5VocabInitVtab(
219622 sqlite3 *db, /* The SQLite database connection */
219623 void *pAux, /* Pointer to Fts5Global object */
219624 int argc, /* Number of elements in argv array */
219625 const char * const *argv, /* xCreate/xConnect argument array */
219626 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
219627 char **pzErr /* Write any error message here */
219628){
219629 const char *azSchema[] = {
219630 "CREATE TABlE vocab(" FTS5_VOCAB_COL_SCHEMA ")",
219631 "CREATE TABlE vocab(" FTS5_VOCAB_ROW_SCHEMA ")",
219632 "CREATE TABlE vocab(" FTS5_VOCAB_INST_SCHEMA ")"
219633 };
219634
219635 Fts5VocabTable *pRet = 0;
219636 int rc = SQLITE_OK; /* Return code */
219637 int bDb;
219638
219639 bDb = (argc==6 && strlen(argv[1])==4 && memcmp("temp", argv[1], 4)==0);
219640
219641 if( argc!=5 && bDb==0 ){
219642 *pzErr = sqlite3_mprintf("wrong number of vtable arguments");
219643 rc = SQLITE_ERROR;
219644 }else{
219645 int nByte; /* Bytes of space to allocate */
219646 const char *zDb = bDb ? argv[3] : argv[1];
219647 const char *zTab = bDb ? argv[4] : argv[3];
219648 const char *zType = bDb ? argv[5] : argv[4];
219649 int nDb = (int)strlen(zDb)+1;
219650 int nTab = (int)strlen(zTab)+1;
219651 int eType = 0;
219652
219653 rc = fts5VocabTableType(zType, pzErr, &eType);
219654 if( rc==SQLITE_OK ){
219655 assert( eType>=0 && eType<ArraySize(azSchema) );
219656 rc = sqlite3_declare_vtab(db, azSchema[eType]);
219657 }
219658
219659 nByte = sizeof(Fts5VocabTable) + nDb + nTab;
219660 pRet = sqlite3Fts5MallocZero(&rc, nByte);
219661 if( pRet ){
219662 pRet->pGlobal = (Fts5Global*)pAux;
219663 pRet->eType = eType;
219664 pRet->db = db;
219665 pRet->zFts5Tbl = (char*)&pRet[1];
219666 pRet->zFts5Db = &pRet->zFts5Tbl[nTab];
219667 memcpy(pRet->zFts5Tbl, zTab, nTab);
219668 memcpy(pRet->zFts5Db, zDb, nDb);
219669 sqlite3Fts5Dequote(pRet->zFts5Tbl);
219670 sqlite3Fts5Dequote(pRet->zFts5Db);
219671 }
219672 }
219673
219674 *ppVTab = (sqlite3_vtab*)pRet;
219675 return rc;
219676}
219677
219678
219679/*
219680** The xConnect() and xCreate() methods for the virtual table. All the
219681** work is done in function fts5VocabInitVtab().
219682*/
219683static int fts5VocabConnectMethod(
219684 sqlite3 *db, /* Database connection */
219685 void *pAux, /* Pointer to tokenizer hash table */
219686 int argc, /* Number of elements in argv array */
219687 const char * const *argv, /* xCreate/xConnect argument array */
219688 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
219689 char **pzErr /* OUT: sqlite3_malloc'd error message */
219690){
219691 return fts5VocabInitVtab(db, pAux, argc, argv, ppVtab, pzErr);
219692}
219693static int fts5VocabCreateMethod(
219694 sqlite3 *db, /* Database connection */
219695 void *pAux, /* Pointer to tokenizer hash table */
219696 int argc, /* Number of elements in argv array */
219697 const char * const *argv, /* xCreate/xConnect argument array */
219698 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
219699 char **pzErr /* OUT: sqlite3_malloc'd error message */
219700){
219701 return fts5VocabInitVtab(db, pAux, argc, argv, ppVtab, pzErr);
219702}
219703
219704/*
219705** Implementation of the xBestIndex method.
219706**
219707** Only constraints of the form:
219708**
219709** term <= ?
219710** term == ?
219711** term >= ?
219712**
219713** are interpreted. Less-than and less-than-or-equal are treated
219714** identically, as are greater-than and greater-than-or-equal.
219715*/
219716static int fts5VocabBestIndexMethod(
219717 sqlite3_vtab *pUnused,
219718 sqlite3_index_info *pInfo
219719){
219720 int i;
219721 int iTermEq = -1;
219722 int iTermGe = -1;
219723 int iTermLe = -1;
219724 int idxNum = 0;
219725 int nArg = 0;
219726
219727 UNUSED_PARAM(pUnused);
219728
219729 for(i=0; i<pInfo->nConstraint; i++){
219730 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
219731 if( p->usable==0 ) continue;
219732 if( p->iColumn==0 ){ /* term column */
219733 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ) iTermEq = i;
219734 if( p->op==SQLITE_INDEX_CONSTRAINT_LE ) iTermLe = i;
219735 if( p->op==SQLITE_INDEX_CONSTRAINT_LT ) iTermLe = i;
219736 if( p->op==SQLITE_INDEX_CONSTRAINT_GE ) iTermGe = i;
219737 if( p->op==SQLITE_INDEX_CONSTRAINT_GT ) iTermGe = i;
219738 }
219739 }
219740
219741 if( iTermEq>=0 ){
219742 idxNum |= FTS5_VOCAB_TERM_EQ;
219743 pInfo->aConstraintUsage[iTermEq].argvIndex = ++nArg;
219744 pInfo->estimatedCost = 100;
219745 }else{
219746 pInfo->estimatedCost = 1000000;
219747 if( iTermGe>=0 ){
219748 idxNum |= FTS5_VOCAB_TERM_GE;
219749 pInfo->aConstraintUsage[iTermGe].argvIndex = ++nArg;
219750 pInfo->estimatedCost = pInfo->estimatedCost / 2;
219751 }
219752 if( iTermLe>=0 ){
219753 idxNum |= FTS5_VOCAB_TERM_LE;
219754 pInfo->aConstraintUsage[iTermLe].argvIndex = ++nArg;
219755 pInfo->estimatedCost = pInfo->estimatedCost / 2;
219756 }
219757 }
219758
219759 /* This virtual table always delivers results in ascending order of
219760 ** the "term" column (column 0). So if the user has requested this
219761 ** specifically - "ORDER BY term" or "ORDER BY term ASC" - set the
219762 ** sqlite3_index_info.orderByConsumed flag to tell the core the results
219763 ** are already in sorted order. */
219764 if( pInfo->nOrderBy==1
219765 && pInfo->aOrderBy[0].iColumn==0
219766 && pInfo->aOrderBy[0].desc==0
219767 ){
219768 pInfo->orderByConsumed = 1;
219769 }
219770
219771 pInfo->idxNum = idxNum;
219772 return SQLITE_OK;
219773}
219774
219775/*
219776** Implementation of xOpen method.
219777*/
219778static int fts5VocabOpenMethod(
219779 sqlite3_vtab *pVTab,
219780 sqlite3_vtab_cursor **ppCsr
219781){
219782 Fts5VocabTable *pTab = (Fts5VocabTable*)pVTab;
219783 Fts5Index *pIndex = 0;
219784 Fts5Config *pConfig = 0;
219785 Fts5VocabCursor *pCsr = 0;
219786 int rc = SQLITE_OK;
219787 sqlite3_stmt *pStmt = 0;
219788 char *zSql = 0;
219789
219790 zSql = sqlite3Fts5Mprintf(&rc,
219791 "SELECT t.%Q FROM %Q.%Q AS t WHERE t.%Q MATCH '*id'",
219792 pTab->zFts5Tbl, pTab->zFts5Db, pTab->zFts5Tbl, pTab->zFts5Tbl
219793 );
219794 if( zSql ){
219795 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
219796 }
219797 sqlite3_free(zSql);
219798 assert( rc==SQLITE_OK || pStmt==0 );
219799 if( rc==SQLITE_ERROR ) rc = SQLITE_OK;
219800
219801 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
219802 i64 iId = sqlite3_column_int64(pStmt, 0);
219803 pIndex = sqlite3Fts5IndexFromCsrid(pTab->pGlobal, iId, &pConfig);
219804 }
219805
219806 if( rc==SQLITE_OK && pIndex==0 ){
219807 rc = sqlite3_finalize(pStmt);
219808 pStmt = 0;
219809 if( rc==SQLITE_OK ){
219810 pVTab->zErrMsg = sqlite3_mprintf(
219811 "no such fts5 table: %s.%s", pTab->zFts5Db, pTab->zFts5Tbl
219812 );
219813 rc = SQLITE_ERROR;
219814 }
219815 }
219816
219817 if( rc==SQLITE_OK ){
219818 int nByte = pConfig->nCol * sizeof(i64) * 2 + sizeof(Fts5VocabCursor);
219819 pCsr = (Fts5VocabCursor*)sqlite3Fts5MallocZero(&rc, nByte);
219820 }
219821
219822 if( pCsr ){
219823 pCsr->pIndex = pIndex;
219824 pCsr->pStmt = pStmt;
219825 pCsr->pConfig = pConfig;
219826 pCsr->aCnt = (i64*)&pCsr[1];
219827 pCsr->aDoc = &pCsr->aCnt[pConfig->nCol];
219828 }else{
219829 sqlite3_finalize(pStmt);
219830 }
219831
219832 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
219833 return rc;
219834}
219835
219836static void fts5VocabResetCursor(Fts5VocabCursor *pCsr){
219837 pCsr->rowid = 0;
219838 sqlite3Fts5IterClose(pCsr->pIter);
219839 pCsr->pIter = 0;
219840 sqlite3_free(pCsr->zLeTerm);
219841 pCsr->nLeTerm = -1;
219842 pCsr->zLeTerm = 0;
219843}
219844
219845/*
219846** Close the cursor. For additional information see the documentation
219847** on the xClose method of the virtual table interface.
219848*/
219849static int fts5VocabCloseMethod(sqlite3_vtab_cursor *pCursor){
219850 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
219851 fts5VocabResetCursor(pCsr);
219852 sqlite3Fts5BufferFree(&pCsr->term);
219853 sqlite3_finalize(pCsr->pStmt);
219854 sqlite3_free(pCsr);
219855 return SQLITE_OK;
219856}
219857
219858static int fts5VocabInstanceNewTerm(Fts5VocabCursor *pCsr){
219859 int rc = SQLITE_OK;
219860
219861 if( sqlite3Fts5IterEof(pCsr->pIter) ){
219862 pCsr->bEof = 1;
219863 }else{
219864 const char *zTerm;
219865 int nTerm;
219866 zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
219867 if( pCsr->nLeTerm>=0 ){
219868 int nCmp = MIN(nTerm, pCsr->nLeTerm);
219869 int bCmp = memcmp(pCsr->zLeTerm, zTerm, nCmp);
219870 if( bCmp<0 || (bCmp==0 && pCsr->nLeTerm<nTerm) ){
219871 pCsr->bEof = 1;
219872 }
219873 }
219874
219875 sqlite3Fts5BufferSet(&rc, &pCsr->term, nTerm, (const u8*)zTerm);
219876 }
219877 return rc;
219878}
219879
219880static int fts5VocabInstanceNext(Fts5VocabCursor *pCsr){
219881 int eDetail = pCsr->pConfig->eDetail;
219882 int rc = SQLITE_OK;
219883 Fts5IndexIter *pIter = pCsr->pIter;
219884 i64 *pp = &pCsr->iInstPos;
219885 int *po = &pCsr->iInstOff;
219886
219887 assert( sqlite3Fts5IterEof(pIter)==0 );
219888 assert( pCsr->bEof==0 );
219889 while( eDetail==FTS5_DETAIL_NONE
219890 || sqlite3Fts5PoslistNext64(pIter->pData, pIter->nData, po, pp)
219891 ){
219892 pCsr->iInstPos = 0;
219893 pCsr->iInstOff = 0;
219894
219895 rc = sqlite3Fts5IterNextScan(pCsr->pIter);
219896 if( rc==SQLITE_OK ){
219897 rc = fts5VocabInstanceNewTerm(pCsr);
219898 if( pCsr->bEof || eDetail==FTS5_DETAIL_NONE ) break;
219899 }
219900 if( rc ){
219901 pCsr->bEof = 1;
219902 break;
219903 }
219904 }
219905
219906 return rc;
219907}
219908
219909/*
219910** Advance the cursor to the next row in the table.
219911*/
219912static int fts5VocabNextMethod(sqlite3_vtab_cursor *pCursor){
219913 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
219914 Fts5VocabTable *pTab = (Fts5VocabTable*)pCursor->pVtab;
219915 int rc = SQLITE_OK;
219916 int nCol = pCsr->pConfig->nCol;
219917
219918 pCsr->rowid++;
219919
219920 if( pTab->eType==FTS5_VOCAB_INSTANCE ){
219921 return fts5VocabInstanceNext(pCsr);
219922 }
219923
219924 if( pTab->eType==FTS5_VOCAB_COL ){
219925 for(pCsr->iCol++; pCsr->iCol<nCol; pCsr->iCol++){
219926 if( pCsr->aDoc[pCsr->iCol] ) break;
219927 }
219928 }
219929
219930 if( pTab->eType!=FTS5_VOCAB_COL || pCsr->iCol>=nCol ){
219931 if( sqlite3Fts5IterEof(pCsr->pIter) ){
219932 pCsr->bEof = 1;
219933 }else{
219934 const char *zTerm;
219935 int nTerm;
219936
219937 zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
219938 if( pCsr->nLeTerm>=0 ){
219939 int nCmp = MIN(nTerm, pCsr->nLeTerm);
219940 int bCmp = memcmp(pCsr->zLeTerm, zTerm, nCmp);
219941 if( bCmp<0 || (bCmp==0 && pCsr->nLeTerm<nTerm) ){
219942 pCsr->bEof = 1;
219943 return SQLITE_OK;
219944 }
219945 }
219946
219947 sqlite3Fts5BufferSet(&rc, &pCsr->term, nTerm, (const u8*)zTerm);
219948 memset(pCsr->aCnt, 0, nCol * sizeof(i64));
219949 memset(pCsr->aDoc, 0, nCol * sizeof(i64));
219950 pCsr->iCol = 0;
219951
219952 assert( pTab->eType==FTS5_VOCAB_COL || pTab->eType==FTS5_VOCAB_ROW );
219953 while( rc==SQLITE_OK ){
219954 int eDetail = pCsr->pConfig->eDetail;
219955 const u8 *pPos; int nPos; /* Position list */
219956 i64 iPos = 0; /* 64-bit position read from poslist */
219957 int iOff = 0; /* Current offset within position list */
219958
219959 pPos = pCsr->pIter->pData;
219960 nPos = pCsr->pIter->nData;
219961
219962 switch( pTab->eType ){
219963 case FTS5_VOCAB_ROW:
219964 if( eDetail==FTS5_DETAIL_FULL ){
219965 while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){
219966 pCsr->aCnt[0]++;
219967 }
219968 }
219969 pCsr->aDoc[0]++;
219970 break;
219971
219972 case FTS5_VOCAB_COL:
219973 if( eDetail==FTS5_DETAIL_FULL ){
219974 int iCol = -1;
219975 while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){
219976 int ii = FTS5_POS2COLUMN(iPos);
219977 pCsr->aCnt[ii]++;
219978 if( iCol!=ii ){
219979 if( ii>=nCol ){
219980 rc = FTS5_CORRUPT;
219981 break;
219982 }
219983 pCsr->aDoc[ii]++;
219984 iCol = ii;
219985 }
219986 }
219987 }else if( eDetail==FTS5_DETAIL_COLUMNS ){
219988 while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff,&iPos) ){
219989 assert_nc( iPos>=0 && iPos<nCol );
219990 if( iPos>=nCol ){
219991 rc = FTS5_CORRUPT;
219992 break;
219993 }
219994 pCsr->aDoc[iPos]++;
219995 }
219996 }else{
219997 assert( eDetail==FTS5_DETAIL_NONE );
219998 pCsr->aDoc[0]++;
219999 }
220000 break;
220001
220002 default:
220003 assert( pTab->eType==FTS5_VOCAB_INSTANCE );
220004 break;
220005 }
220006
220007 if( rc==SQLITE_OK ){
220008 rc = sqlite3Fts5IterNextScan(pCsr->pIter);
220009 }
220010 if( pTab->eType==FTS5_VOCAB_INSTANCE ) break;
220011
220012 if( rc==SQLITE_OK ){
220013 zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
220014 if( nTerm!=pCsr->term.n || memcmp(zTerm, pCsr->term.p, nTerm) ){
220015 break;
220016 }
220017 if( sqlite3Fts5IterEof(pCsr->pIter) ) break;
220018 }
220019 }
220020 }
220021 }
220022
220023 if( rc==SQLITE_OK && pCsr->bEof==0 && pTab->eType==FTS5_VOCAB_COL ){
220024 while( pCsr->aDoc[pCsr->iCol]==0 ) pCsr->iCol++;
220025 assert( pCsr->iCol<pCsr->pConfig->nCol );
220026 }
220027 return rc;
220028}
220029
220030/*
220031** This is the xFilter implementation for the virtual table.
220032*/
220033static int fts5VocabFilterMethod(
220034 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
220035 int idxNum, /* Strategy index */
220036 const char *zUnused, /* Unused */
220037 int nUnused, /* Number of elements in apVal */
220038 sqlite3_value **apVal /* Arguments for the indexing scheme */
220039){
220040 Fts5VocabTable *pTab = (Fts5VocabTable*)pCursor->pVtab;
220041 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
220042 int eType = pTab->eType;
220043 int rc = SQLITE_OK;
220044
220045 int iVal = 0;
220046 int f = FTS5INDEX_QUERY_SCAN;
220047 const char *zTerm = 0;
220048 int nTerm = 0;
220049
220050 sqlite3_value *pEq = 0;
220051 sqlite3_value *pGe = 0;
220052 sqlite3_value *pLe = 0;
220053
220054 UNUSED_PARAM2(zUnused, nUnused);
220055
220056 fts5VocabResetCursor(pCsr);
220057 if( idxNum & FTS5_VOCAB_TERM_EQ ) pEq = apVal[iVal++];
220058 if( idxNum & FTS5_VOCAB_TERM_GE ) pGe = apVal[iVal++];
220059 if( idxNum & FTS5_VOCAB_TERM_LE ) pLe = apVal[iVal++];
220060
220061 if( pEq ){
220062 zTerm = (const char *)sqlite3_value_text(pEq);
220063 nTerm = sqlite3_value_bytes(pEq);
220064 f = 0;
220065 }else{
220066 if( pGe ){
220067 zTerm = (const char *)sqlite3_value_text(pGe);
220068 nTerm = sqlite3_value_bytes(pGe);
220069 }
220070 if( pLe ){
220071 const char *zCopy = (const char *)sqlite3_value_text(pLe);
220072 pCsr->nLeTerm = sqlite3_value_bytes(pLe);
220073 pCsr->zLeTerm = sqlite3_malloc(pCsr->nLeTerm+1);
220074 if( pCsr->zLeTerm==0 ){
220075 rc = SQLITE_NOMEM;
220076 }else{
220077 memcpy(pCsr->zLeTerm, zCopy, pCsr->nLeTerm+1);
220078 }
220079 }
220080 }
220081
220082 if( rc==SQLITE_OK ){
220083 rc = sqlite3Fts5IndexQuery(pCsr->pIndex, zTerm, nTerm, f, 0, &pCsr->pIter);
220084 }
220085 if( rc==SQLITE_OK && eType==FTS5_VOCAB_INSTANCE ){
220086 rc = fts5VocabInstanceNewTerm(pCsr);
220087 }
220088 if( rc==SQLITE_OK
220089 && !pCsr->bEof
220090 && (eType!=FTS5_VOCAB_INSTANCE || pCsr->pConfig->eDetail!=FTS5_DETAIL_NONE)
220091 ){
220092 rc = fts5VocabNextMethod(pCursor);
220093 }
220094
220095 return rc;
220096}
220097
220098/*
220099** This is the xEof method of the virtual table. SQLite calls this
220100** routine to find out if it has reached the end of a result set.
220101*/
220102static int fts5VocabEofMethod(sqlite3_vtab_cursor *pCursor){
220103 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
220104 return pCsr->bEof;
220105}
220106
220107static int fts5VocabColumnMethod(
220108 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
220109 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
220110 int iCol /* Index of column to read value from */
220111){
220112 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
220113 int eDetail = pCsr->pConfig->eDetail;
220114 int eType = ((Fts5VocabTable*)(pCursor->pVtab))->eType;
220115 i64 iVal = 0;
220116
220117 if( iCol==0 ){
220118 sqlite3_result_text(
220119 pCtx, (const char*)pCsr->term.p, pCsr->term.n, SQLITE_TRANSIENT
220120 );
220121 }else if( eType==FTS5_VOCAB_COL ){
220122 assert( iCol==1 || iCol==2 || iCol==3 );
220123 if( iCol==1 ){
220124 if( eDetail!=FTS5_DETAIL_NONE ){
220125 const char *z = pCsr->pConfig->azCol[pCsr->iCol];
220126 sqlite3_result_text(pCtx, z, -1, SQLITE_STATIC);
220127 }
220128 }else if( iCol==2 ){
220129 iVal = pCsr->aDoc[pCsr->iCol];
220130 }else{
220131 iVal = pCsr->aCnt[pCsr->iCol];
220132 }
220133 }else if( eType==FTS5_VOCAB_ROW ){
220134 assert( iCol==1 || iCol==2 );
220135 if( iCol==1 ){
220136 iVal = pCsr->aDoc[0];
220137 }else{
220138 iVal = pCsr->aCnt[0];
220139 }
220140 }else{
220141 assert( eType==FTS5_VOCAB_INSTANCE );
220142 switch( iCol ){
220143 case 1:
220144 sqlite3_result_int64(pCtx, pCsr->pIter->iRowid);
220145 break;
220146 case 2: {
220147 int ii = -1;
220148 if( eDetail==FTS5_DETAIL_FULL ){
220149 ii = FTS5_POS2COLUMN(pCsr->iInstPos);
220150 }else if( eDetail==FTS5_DETAIL_COLUMNS ){
220151 ii = (int)pCsr->iInstPos;
220152 }
220153 if( ii>=0 && ii<pCsr->pConfig->nCol ){
220154 const char *z = pCsr->pConfig->azCol[ii];
220155 sqlite3_result_text(pCtx, z, -1, SQLITE_STATIC);
220156 }
220157 break;
220158 }
220159 default: {
220160 assert( iCol==3 );
220161 if( eDetail==FTS5_DETAIL_FULL ){
220162 int ii = FTS5_POS2OFFSET(pCsr->iInstPos);
220163 sqlite3_result_int(pCtx, ii);
220164 }
220165 break;
220166 }
220167 }
220168 }
220169
220170 if( iVal>0 ) sqlite3_result_int64(pCtx, iVal);
220171 return SQLITE_OK;
220172}
220173
220174/*
220175** This is the xRowid method. The SQLite core calls this routine to
220176** retrieve the rowid for the current row of the result set. The
220177** rowid should be written to *pRowid.
220178*/
220179static int fts5VocabRowidMethod(
220180 sqlite3_vtab_cursor *pCursor,
220181 sqlite_int64 *pRowid
220182){
220183 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
220184 *pRowid = pCsr->rowid;
220185 return SQLITE_OK;
220186}
220187
220188static int sqlite3Fts5VocabInit(Fts5Global *pGlobal, sqlite3 *db){
220189 static const sqlite3_module fts5Vocab = {
220190 /* iVersion */ 2,
220191 /* xCreate */ fts5VocabCreateMethod,
220192 /* xConnect */ fts5VocabConnectMethod,
220193 /* xBestIndex */ fts5VocabBestIndexMethod,
220194 /* xDisconnect */ fts5VocabDisconnectMethod,
220195 /* xDestroy */ fts5VocabDestroyMethod,
220196 /* xOpen */ fts5VocabOpenMethod,
220197 /* xClose */ fts5VocabCloseMethod,
220198 /* xFilter */ fts5VocabFilterMethod,
220199 /* xNext */ fts5VocabNextMethod,
220200 /* xEof */ fts5VocabEofMethod,
220201 /* xColumn */ fts5VocabColumnMethod,
220202 /* xRowid */ fts5VocabRowidMethod,
220203 /* xUpdate */ 0,
220204 /* xBegin */ 0,
220205 /* xSync */ 0,
220206 /* xCommit */ 0,
220207 /* xRollback */ 0,
220208 /* xFindFunction */ 0,
220209 /* xRename */ 0,
220210 /* xSavepoint */ 0,
220211 /* xRelease */ 0,
220212 /* xRollbackTo */ 0,
220213 /* xShadowName */ 0
220214 };
220215 void *p = (void*)pGlobal;
220216
220217 return sqlite3_create_module_v2(db, "fts5vocab", &fts5Vocab, p, 0);
220218}
220219
220220
220221
220222#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5) */
220223
220224/************** End of fts5.c ************************************************/
220225/************** Begin file stmt.c ********************************************/
220226/*
220227** 2017-05-31
220228**
220229** The author disclaims copyright to this source code. In place of
220230** a legal notice, here is a blessing:
220231**
220232** May you do good and not evil.
220233** May you find forgiveness for yourself and forgive others.
220234** May you share freely, never taking more than you give.
220235**
220236*************************************************************************
220237**
220238** This file demonstrates an eponymous virtual table that returns information
220239** about all prepared statements for the database connection.
220240**
220241** Usage example:
220242**
220243** .load ./stmt
220244** .mode line
220245** .header on
220246** SELECT * FROM stmt;
220247*/
220248#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB)
220249#if !defined(SQLITEINT_H)
220250/* #include "sqlite3ext.h" */
220251#endif
220252SQLITE_EXTENSION_INIT1
220253/* #include <assert.h> */
220254/* #include <string.h> */
220255
220256#ifndef SQLITE_OMIT_VIRTUALTABLE
220257
220258/* stmt_vtab is a subclass of sqlite3_vtab which will
220259** serve as the underlying representation of a stmt virtual table
220260*/
220261typedef struct stmt_vtab stmt_vtab;
220262struct stmt_vtab {
220263 sqlite3_vtab base; /* Base class - must be first */
220264 sqlite3 *db; /* Database connection for this stmt vtab */
220265};
220266
220267/* stmt_cursor is a subclass of sqlite3_vtab_cursor which will
220268** serve as the underlying representation of a cursor that scans
220269** over rows of the result
220270*/
220271typedef struct stmt_cursor stmt_cursor;
220272struct stmt_cursor {
220273 sqlite3_vtab_cursor base; /* Base class - must be first */
220274 sqlite3 *db; /* Database connection for this cursor */
220275 sqlite3_stmt *pStmt; /* Statement cursor is currently pointing at */
220276 sqlite3_int64 iRowid; /* The rowid */
220277};
220278
220279/*
220280** The stmtConnect() method is invoked to create a new
220281** stmt_vtab that describes the stmt virtual table.
220282**
220283** Think of this routine as the constructor for stmt_vtab objects.
220284**
220285** All this routine needs to do is:
220286**
220287** (1) Allocate the stmt_vtab object and initialize all fields.
220288**
220289** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
220290** result set of queries against stmt will look like.
220291*/
220292static int stmtConnect(
220293 sqlite3 *db,
220294 void *pAux,
220295 int argc, const char *const*argv,
220296 sqlite3_vtab **ppVtab,
220297 char **pzErr
220298){
220299 stmt_vtab *pNew;
220300 int rc;
220301
220302/* Column numbers */
220303#define STMT_COLUMN_SQL 0 /* SQL for the statement */
220304#define STMT_COLUMN_NCOL 1 /* Number of result columns */
220305#define STMT_COLUMN_RO 2 /* True if read-only */
220306#define STMT_COLUMN_BUSY 3 /* True if currently busy */
220307#define STMT_COLUMN_NSCAN 4 /* SQLITE_STMTSTATUS_FULLSCAN_STEP */
220308#define STMT_COLUMN_NSORT 5 /* SQLITE_STMTSTATUS_SORT */
220309#define STMT_COLUMN_NAIDX 6 /* SQLITE_STMTSTATUS_AUTOINDEX */
220310#define STMT_COLUMN_NSTEP 7 /* SQLITE_STMTSTATUS_VM_STEP */
220311#define STMT_COLUMN_REPREP 8 /* SQLITE_STMTSTATUS_REPREPARE */
220312#define STMT_COLUMN_RUN 9 /* SQLITE_STMTSTATUS_RUN */
220313#define STMT_COLUMN_MEM 10 /* SQLITE_STMTSTATUS_MEMUSED */
220314
220315
220316 rc = sqlite3_declare_vtab(db,
220317 "CREATE TABLE x(sql,ncol,ro,busy,nscan,nsort,naidx,nstep,"
220318 "reprep,run,mem)");
220319 if( rc==SQLITE_OK ){
220320 pNew = sqlite3_malloc( sizeof(*pNew) );
220321 *ppVtab = (sqlite3_vtab*)pNew;
220322 if( pNew==0 ) return SQLITE_NOMEM;
220323 memset(pNew, 0, sizeof(*pNew));
220324 pNew->db = db;
220325 }
220326 return rc;
220327}
220328
220329/*
220330** This method is the destructor for stmt_cursor objects.
220331*/
220332static int stmtDisconnect(sqlite3_vtab *pVtab){
220333 sqlite3_free(pVtab);
220334 return SQLITE_OK;
220335}
220336
220337/*
220338** Constructor for a new stmt_cursor object.
220339*/
220340static int stmtOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
220341 stmt_cursor *pCur;
220342 pCur = sqlite3_malloc( sizeof(*pCur) );
220343 if( pCur==0 ) return SQLITE_NOMEM;
220344 memset(pCur, 0, sizeof(*pCur));
220345 pCur->db = ((stmt_vtab*)p)->db;
220346 *ppCursor = &pCur->base;
220347 return SQLITE_OK;
220348}
220349
220350/*
220351** Destructor for a stmt_cursor.
220352*/
220353static int stmtClose(sqlite3_vtab_cursor *cur){
220354 sqlite3_free(cur);
220355 return SQLITE_OK;
220356}
220357
220358
220359/*
220360** Advance a stmt_cursor to its next row of output.
220361*/
220362static int stmtNext(sqlite3_vtab_cursor *cur){
220363 stmt_cursor *pCur = (stmt_cursor*)cur;
220364 pCur->iRowid++;
220365 pCur->pStmt = sqlite3_next_stmt(pCur->db, pCur->pStmt);
220366 return SQLITE_OK;
220367}
220368
220369/*
220370** Return values of columns for the row at which the stmt_cursor
220371** is currently pointing.
220372*/
220373static int stmtColumn(
220374 sqlite3_vtab_cursor *cur, /* The cursor */
220375 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
220376 int i /* Which column to return */
220377){
220378 stmt_cursor *pCur = (stmt_cursor*)cur;
220379 switch( i ){
220380 case STMT_COLUMN_SQL: {
220381 sqlite3_result_text(ctx, sqlite3_sql(pCur->pStmt), -1, SQLITE_TRANSIENT);
220382 break;
220383 }
220384 case STMT_COLUMN_NCOL: {
220385 sqlite3_result_int(ctx, sqlite3_column_count(pCur->pStmt));
220386 break;
220387 }
220388 case STMT_COLUMN_RO: {
220389 sqlite3_result_int(ctx, sqlite3_stmt_readonly(pCur->pStmt));
220390 break;
220391 }
220392 case STMT_COLUMN_BUSY: {
220393 sqlite3_result_int(ctx, sqlite3_stmt_busy(pCur->pStmt));
220394 break;
220395 }
220396 case STMT_COLUMN_MEM: {
220397 i = SQLITE_STMTSTATUS_MEMUSED +
220398 STMT_COLUMN_NSCAN - SQLITE_STMTSTATUS_FULLSCAN_STEP;
220399 /* Fall thru */
220400 }
220401 case STMT_COLUMN_NSCAN:
220402 case STMT_COLUMN_NSORT:
220403 case STMT_COLUMN_NAIDX:
220404 case STMT_COLUMN_NSTEP:
220405 case STMT_COLUMN_REPREP:
220406 case STMT_COLUMN_RUN: {
220407 sqlite3_result_int(ctx, sqlite3_stmt_status(pCur->pStmt,
220408 i-STMT_COLUMN_NSCAN+SQLITE_STMTSTATUS_FULLSCAN_STEP, 0));
220409 break;
220410 }
220411 }
220412 return SQLITE_OK;
220413}
220414
220415/*
220416** Return the rowid for the current row. In this implementation, the
220417** rowid is the same as the output value.
220418*/
220419static int stmtRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
220420 stmt_cursor *pCur = (stmt_cursor*)cur;
220421 *pRowid = pCur->iRowid;
220422 return SQLITE_OK;
220423}
220424
220425/*
220426** Return TRUE if the cursor has been moved off of the last
220427** row of output.
220428*/
220429static int stmtEof(sqlite3_vtab_cursor *cur){
220430 stmt_cursor *pCur = (stmt_cursor*)cur;
220431 return pCur->pStmt==0;
220432}
220433
220434/*
220435** This method is called to "rewind" the stmt_cursor object back
220436** to the first row of output. This method is always called at least
220437** once prior to any call to stmtColumn() or stmtRowid() or
220438** stmtEof().
220439*/
220440static int stmtFilter(
220441 sqlite3_vtab_cursor *pVtabCursor,
220442 int idxNum, const char *idxStr,
220443 int argc, sqlite3_value **argv
220444){
220445 stmt_cursor *pCur = (stmt_cursor *)pVtabCursor;
220446 pCur->pStmt = 0;
220447 pCur->iRowid = 0;
220448 return stmtNext(pVtabCursor);
220449}
220450
220451/*
220452** SQLite will invoke this method one or more times while planning a query
220453** that uses the stmt virtual table. This routine needs to create
220454** a query plan for each invocation and compute an estimated cost for that
220455** plan.
220456*/
220457static int stmtBestIndex(
220458 sqlite3_vtab *tab,
220459 sqlite3_index_info *pIdxInfo
220460){
220461 pIdxInfo->estimatedCost = (double)500;
220462 pIdxInfo->estimatedRows = 500;
220463 return SQLITE_OK;
220464}
220465
220466/*
220467** This following structure defines all the methods for the
220468** stmt virtual table.
220469*/
220470static sqlite3_module stmtModule = {
220471 0, /* iVersion */
220472 0, /* xCreate */
220473 stmtConnect, /* xConnect */
220474 stmtBestIndex, /* xBestIndex */
220475 stmtDisconnect, /* xDisconnect */
220476 0, /* xDestroy */
220477 stmtOpen, /* xOpen - open a cursor */
220478 stmtClose, /* xClose - close a cursor */
220479 stmtFilter, /* xFilter - configure scan constraints */
220480 stmtNext, /* xNext - advance a cursor */
220481 stmtEof, /* xEof - check for end of scan */
220482 stmtColumn, /* xColumn - read data */
220483 stmtRowid, /* xRowid - read data */
220484 0, /* xUpdate */
220485 0, /* xBegin */
220486 0, /* xSync */
220487 0, /* xCommit */
220488 0, /* xRollback */
220489 0, /* xFindMethod */
220490 0, /* xRename */
220491 0, /* xSavepoint */
220492 0, /* xRelease */
220493 0, /* xRollbackTo */
220494 0, /* xShadowName */
220495};
220496
220497#endif /* SQLITE_OMIT_VIRTUALTABLE */
220498
220499SQLITE_PRIVATE int sqlite3StmtVtabInit(sqlite3 *db){
220500 int rc = SQLITE_OK;
220501#ifndef SQLITE_OMIT_VIRTUALTABLE
220502 rc = sqlite3_create_module(db, "sqlite_stmt", &stmtModule, 0);
220503#endif
220504 return rc;
220505}
220506
220507#ifndef SQLITE_CORE
220508#ifdef _WIN32
220509__declspec(dllexport)
220510#endif
220511SQLITE_API int sqlite3_stmt_init(
220512 sqlite3 *db,
220513 char **pzErrMsg,
220514 const sqlite3_api_routines *pApi
220515){
220516 int rc = SQLITE_OK;
220517 SQLITE_EXTENSION_INIT2(pApi);
220518#ifndef SQLITE_OMIT_VIRTUALTABLE
220519 rc = sqlite3StmtVtabInit(db);
220520#endif
220521 return rc;
220522}
220523#endif /* SQLITE_CORE */
220524#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
220525
220526/************** End of stmt.c ************************************************/
220527#if __LINE__!=220527
220528#undef SQLITE_SOURCE_ID
220529#define SQLITE_SOURCE_ID "2018-12-01 12:34:55 bf8c1b2b7a5960c282e543b9c293686dccff272512d08865f4600fb58238alt2"
220530#endif
220531/* Return the source-id for this library */
220532SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
220533/************************** End of sqlite3.c ******************************/
220534